   Proper way for Turn Based Battle System
  Ok I wrote writing a turn algorithm in PHP.If no one is over 1500, It runs an increment instruction till someone goes over say 1500 "move speed." However, multiple players can go over 1500. So, I take all the users who are over 1500 the moment someone is over 1500, put them into an array, and sort them by who has the most move speed, and then line them up to attack, and decrease there move speed by 1500 after they use their turn.It works great. Except in a battle where everyone has the same move speed.Example: player 1 and player 2 have the same move speed. After one player has gone over 1500, it selects a random player for the starting move. P1MS = 1523P2MS = 1523If it's player 1, he will attackP1MS = 23P2MS = 1523Then player 2 will attackP1MS = 23P1MS = 23Then they both will increase equally to say 1512P1MS = 1512P2MS = 1512Now here is where the issue occurs. When using the arsort method in PHP, the first player in the list will automatically be player two. Every time. Probably related to the turn they are added to the sorted array.So player two gets two attacks in a row, then it will automatically go back to  P1 attacks, p2 attacks, p1 attacks, etc... I want to eliminate that issue, so I thought I would write a check to make sure the next turn player isn't the same player from the last turn if there are two or more players in line with the highest move speed.However this fix would only work if it is just one on one. Two on one will encounter the same issue.How do I fix it without randomizing the two players turns every time? I'd just like it to be p1, p2, p1, p2, p1 or p3, p1, p2, p3, p1, p2, p3, p1, p2 if two or more people have the same stats.   
  Use the order they went in last time as a tie-breaker when you sort. That looks like it would do the job.   ;  How would I do that when if there is another player with not the same stats. :/   ;  Do you understand what "tie-breaker" means? If other players have different stats, the tie-breaker won't matter for comparing against them.   ;Quote:Original post by alvaroDo you understand what "tie-breaker" means? If other players have different stats, the tie-breaker won't matter for comparing against them.Do you understand how "a turn based system" works? Except the third player with the different stats can end up on the same movespeed and cause the same whole issue.   ;  It seems to me like this issue could be solved by using a stable sort, but unfortunately I'm not familiar enough with PHP to know if there is one already available :/   ;  I may not understand, but just keep track of who's turn it is and go in a circle? If the player doesn't have/want a move skip them.If player 3 never had a move it would alternate between 1 and 2. When player 3 has a move, he will always go after player 2 and before player 1.   ;  Calling the number you are tracking "move speed" is just confusing: actually you have one transient quantity (the number of available "action points" of each character at a given time) and a constant stat (how many action points, in the example 1500, each action costs for each character).At every round, the characters should receive priority (in a fixed round-robin order, or according to initiative-affecting stats and modifiers) to perform any action their action point total can pay (possibly nothing, effectively skipping a turn); then you go through the characters in the same order over and over until everyone passes; then everyone receives the same amount of action points to spend in future rounds.Instead of a small number, the accrued action points might be adjusted round by round to be enough for most character to afford most actions.For example, if actions cost 1000, 1200 or 1500 action points and characters are left between 0 and 300 action points at the end of a certain round, less than 700 points are useless (everybody necessarily skips the turn), more than 1500 (enough for everyone to perform any action) might be too much, and anything in between (perhaps a random choice, for the sake of unpredictability) is suitable.;  I would solve this at the start of each battle. Create a random priority list that decides who will go first in case of a tie.Create an array from 0 to n-1 where n = number of players in the battle, and [0] = 0, [1] = 1 and so on. Randomize the order of this array.Assign arrayWithRandomOrder[0] to player[0]->tiebreaker and so on, and usort the array of players with > than 1500 movespeed like this:function sortByMoveSpeed($a, $b){if($a->movespeed == $b->movespeed)return ($a->tiebreaker < $b->tiebreaker) ? -1 : 1; return ($a->movespeed < $b->movespeed) ? -1 : 1;}This should give a random order the first time and repeat that order every time the same two players have the same movespeed.   ;  OR... you could simply allow for simultaneous attacks. IE P1 and P2 both attack at the same time.Resolve the issue at a game play level first and the coding issues should resolve themselves.   