#### Archived

This topic is now archived and is closed to further replies.

# Need a battle routine

This topic is 5012 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

hi all Basically i need a routine for automatically determining a battle outcome in a calculated combat arena for a game im making (www.3202ad.com) i need to know (just the pseudo code will be fine) the way to do the loop for the attack structure. ill explain what i want and ive even prepared a diargram of how i want it to go what i want is this. A number of players all enter an arena, then i need to loop through these players and in turn each player automatically attacks another player until there are no players left (i already have done the attack code so dont worry about that) basically lets say there are 4 players in the arena, my initial idea was to have loop until only 1 player is left { player 1 attacks player 2, then p3, then p4. then player 2 attacks p3,p4,1 p3 a p4, p1, p2 p4 a p1, p2, p3 } if a player is killed in this attack then they are not attacked as they are no longer in the running. The loop goes again until there is only 1 player left. This isnt very fair though as if ur player 1 then u either kill everyone first go or u get wiped out straight away. what i want is a code structure for something like this p1 attacks p2, p2 attacks p3, p3 attacks p4, p4 attacks p1 p1 a p3, p2 a p4, p3 a p1, p4 a p2 p1 a p4, p2 a p1, p3 a p2, p4 a p3 this then allows each player to get an attack in with each loop but i also need to take into consideration if the player is destroyed so if p1 is destroyed after the first wave then it would go like this maybe (second wave) p2 a p4, p3 a p2, p4 a p2. p2 gets attacked twice now though, hmm maybe thats just gonna be the way it has to be unless u can think of a solution. anyway here is my lovely diagram to show it if that doesnt work then goto http://www.sysdia.net/pattern.gif Ok so this is my pseudo code for my basic attack lovely eh anyway so what i would just love is someone to help me in figuring out how to come up with the theory for it (im sure its easy, im thinking bubble sort methods for some reason, something i remember from a long time ago) so for my first idea the code would be while user count>1 { loop user array (stores all users) { with user user is in arena { get all users into new array (exclude current user from first array) other user is in arena { attack (this user, other user) } } } } by the way when a user is killed (this is done in the attack bit, they will no longer be in the arena so they will be skipped). anyway thats for my original idea, any ideas for my more fairer way, or even a better way? oh and the game is done in php and mysql if thats any help but as i say pseudo code is fine There are only 10 kinds of people in this world, those who understand binary and those who do not.

##### Share on other sites
why not just code up some good ol AI for the units. each unit will pick as it's target the best target for that unit. it will continue attacking that target until either it dies, it kills its target or a better target comes around.

as a first pass just have each unit target the closest enemy unit and attack it. every 10 frames or so, have that unit recheck to see which unit is closest now, then attack that one. you'll obviously end up with situations in which several units are attacking one other unit, but that's more or less how deathmatches play out in the real world anyway.

something like this:

//every 10 frames or 5 seconds or some amount of timefor (int i = 0; i < numUnits; ++i){    units[i].pickClosestTarget();    units[i].beginAttackMyTarget();}

after you make this first pass, try splitting the behavior of your units into attack & evade behaviors. perhaps as their health drops lower they will decide to evade rather than to attack. during the "evade" behavior they will be more likely to hide than to shoot.

anyway this will certainly be more fun and lead to more emergent behaviors in your units.

-me

[edited by - Palidine on March 25, 2004 1:24:56 PM]

##### Share on other sites
hi, i like your idea about picking a target and attacking till the death.

I should point out this is a web based game that is ticker based (ie when a timer expires a bunch of updates are done)

all of the players in the arena are just basically statistics, they have no real position to each other to determine distance.

They just need to attack each other till a victor is found

There are only 10 kinds of people in this world, those who understand binary and those who do not.

##### Share on other sites
hmm.. so basically something like this

for i=1 to count-1 {  for each user {    target=user+i;    if (target > count) target=target-count    attack(user, target)  }  remove dead  i=i-#dead}

but why not do more of a simulation using initiative and a more strategic target selection?

##### Share on other sites
yeah thats exactly what i want :D

thanks

but as u say add more of a simulation. As you can see i couldnt figure that simple one out for myself so any ideas on how to make it better? :D

what happens is this

a number of players enter an arena. They all have the statistics of defence and attack points

basically an attack is the attackers attack points are taken away from the defenders defence points (and in the attack the attacker loses some of their defence points from the defenders attack points but not using their full attack points)

so say a group of 5 people go into an arena, the computer calculates who wins by deciding each player attacking each other until only 1 is left (or a timer expires)

and on top of that some members of the arena may be on the same team so they wont attack each other (i was just going to skip the attack if this was the case) although this could mean there are 3 people left lets say never attacking each other, this is why i thought of the timer to make sure that if theyre just going round a loop it will end eventually and then i will use lets say damage sustained to determine the winner.

any suggestions on how to make this work really well?

the game is at www.3202ad.com if you want to check it out, the arenas at the moment are just two players go in and run a the attack routine. but it would be loads better if a load of people could go in the arena and battle it out (but remember it cant be too intense as the calculations have to be done through a php script and mysql updates when a user calls the update command at the end of the battle so it cant take too long otherwise the user will be stuck on a page just running a huge loop)

##### Share on other sites
just posted this elsewhere so i thought id put it up here too

ok a run down of the game ships and battle aspects as simply as i can

the ships have spec types such as engine speed, hyperdrive speed, gun mounts and mass

none of these are actually used in battles, but speed and mass could be as you pointed out.

users also have a rating which is how many kills they have, this is used to boost their attack power in a battle but could be used to determine something else as well.

anyway the ships have a shield on board (they dont have to but if no shield then 1 hit and theyre dead)
The shield has 2 ratings a defence points (the total hits it can take before its not operating) and a recharge rate (this isnt used in an instant battle and i cant be bothered to use the recharge really unless u can think of how to do it)

Each gun on the ship has a power rating and a max power rating, if the power rating is near to the max power rating of the weapon then the weapon is less accurate ie it can miss. so if a gun is a power rating of 500 and a max power of 500 then it has a 50/50 chance of hitting its target.

All of the guns are tested for accuracy and the total accuracy is used to test whether the total power of all the weapons either hits or misses the ship (weapons arent individually tested, u either miss or u hit with ur weapon power)

an attack basically goes like this

the attacker gun power is taken off the defenders shield power if the weapons are on target
and the defender does some damage to the attacker but not as much as if they were initiating the attack.

it would be great if all of this could be incorporated into an attack strategy but the problem is that it is a web based game written in php and mysql (which is where all the player data is stored)

so the arenas will go like this, a player enters an arena and clicks a button to say they are waiting for other players to join. When some players have joined (max of 8 lets say although can be les more than 1 though) the person who created the arena clicks another button to say go. This button click is what calls the arena attack code, and it cant take too long to compute as the user doesnt want to be waiting around calculating attack codes because they want to be carrying on doing other things in the game but the user has to initiate the code call because there is no other way of doing it as its just hosted on a normal web server (which i dont have physical access to, just ftp)

also players who are in the arena can be in the same clan and if they are then they wont attack each other, but it would be good if they did gang up on other players. this would mean however that it cant be last man standing as if u have 3 from the same clan then there are going to be 3 left, so id need some other loop limiter like max attack waves, say 5 attacks, then a way of determining a winner, such as who inflicted most amount of damage minus the amount of damage they recieved, sort this, who ever is top is the winner.

At the end of the battle code i want a write up of what happened in the battle, who was destroyed, who destroyed them, etc. It would be great if it was in depth like player 1 attacked player 2, player 3 came to help out player 2, player 1 destroyed by player 3, something like that maybe.

now its getting more in depth :s lol

##### Share on other sites
For every unit you specify a 'attackcount' and 'defendcount' every match. initialize it to 0, or weight it to give handicaps or something.

While Combatants > 1 // These two steps could be skipped if all units have // same attack/defend count, which they should ... Get list of attackers with lowest attackcount Get list of defenders with lowest defendcount while (attackers > 0) and (defenders > 0) randomly select attacker from list of attackers randomly select defender from list of defenders increment attacker attackcount increment defender defendcount Attacker attacks defender endLoop

You might make a unit go evasive as suggested... add 1 to his attackcount, and double his "defense" rating, or add some modifier to his defense rating for that combat round.

[edited by - PyroSA on March 25, 2004 5:00:16 PM]

##### Share on other sites
ok ive got my routine done

if you wanna check it out then visit the game at www.3202ad.com amd look at the arenas. Any suggestions to make it better would be greatly appreciated.

• ### Forum Statistics

• Total Topics
628722
• Total Posts
2984397

• 25
• 11
• 10
• 16
• 14