Hello,
I apologize for the lengthy post, but I am trying to be as clear as possible.
I am creating an RPG and encountered a problem that I cannot figure out a mathematical solution to. I would like to implement a turn-based battle system that would include attack speed of players when determining the order of turns, however I cannot figure out an algorithm for this. Here are some examples of what I mean: (attack speed is a decimal value greater or equal to 1)
If player A has attack speed 2 and player B has attack speed 1, then player A will have two turns before player B gets one.
If player A has attack speed 9 and player B has attack speed 3, then player A will have three turns before player B gets one.
If player A has attack speed 3 and player B has attack speed 2, then player A will get one turn, player B - one turn, then player A will get two turns, player B - one turn and it loops from here.
Hopefully I explained this clearly.
My current algorithm works only in 1 on 1 battles: both players have a variable called remainingAttackValue initialized to their attack speed. When player attacks, his remainingAttackValue is decreased by the smaller value of attack speed of these two players. He can have turns until his remainingAttackValue is greater than that smaller attack speed value. Taking the last example from the above situations:
Player A's attack speed = 3, player B's attack speed = 2. Therefore, we will be subtracting the value 2 from the currently attacking unit's remainingAttackValue.
- A's remainingAttackValue = 3, B's remainingAttackValue = 2. Player A attacks because he has higher attack speed. We subtract 2 from his remainingAttackValue.
- A's remainingAttackValue = 1, B's remainingAttackValue = 2. Player B attacks, because player A has not enough points in remainingAttackValue.
- A's remainingAttackValue = 1, B's remainingAttackValue = 0. Noone can make a move, therefore every player has their remainingAttackValue increased by value of attack speed.
- A's remainingAttackValue = 4, B's remainingAttackValue = 2. Player A attacks. Subtract 2.
- A's remainingAttackValue = 2, B's remainingAttackValue = 2. Player A still attacks, because he has enough points in remainingAttackValue.
- A's remainingAttackValue = 0, B's remainingAttackValue = 2. Player B attacks.
- A's remainingAttackValue = 0, B's remainingAttackValue = 0. Noone can make a move.
- A's remainingAttackValue = 3, B's remainingAttackValue = 2. And the situation loops.
The problem appears when I have battles that involve more than two players. For example, if I have three players, I can't just pick the smallest value and subtract that because that would lead to some strange cases like player A having attack speed 1, player B -- 50, player C -- 60 and then player C does 60 turns before player B even gets one.
I can't think of any algorithm that would help me determine the turn order in such cases. It is probably something trivial which I just simply cannot figure out. Could you please give me some advice regarding this problem?