Turn Order by Initiative
Turn order by initiative lets the fastest fighters act first. Give every fighter a score from its speed plus a roll, sort once, and keep that order for the whole round. Settle ties the same way every time, skip fighters who have fallen, and make late arrivals wait for the next round.

Paste this to your AI
Copy it into Claude, Cursor or ChatGPT. It already has good values and the usual mistakes in it.
Add "Turn Order by Initiative" to my game.
What it should do: A fight with several fighters needs a fair and readable answer to who goes next. A score from speed and a little luck makes fast fighters act earlier without making the order the same every fight, and a fixed order per round lets the player plan ahead.
Build it exactly as this state machine. First check that it fits my game, then build it like this:
```mermaid
stateDiagram-v2
[*] --> before
before: Before the fight
first: First turn
later: Later turn
over: Fight over
before --> first: fight begins
before --> over: only one team came
first --> later: turn ends or waits
first --> over: last enemy falls
later --> first: new round starts
later --> over: enemy falls or limit
over --> [*]
```
Use these starting values:
- rollRange: 6
- maxRounds: 30 rounds
- rerollEachRound: 0
Watch out for these common mistakes:
- Two fighters with the same score: Tell your AI: break ties in a fixed way. Here the faster fighter goes first, and after that whoever joined the fight first.
- Someone joins in the middle of a round: Tell your AI: put the newcomer in by score, but if its place has already passed this round, it waits for the next round.
- The acting fighter falls on their own turn: Tell your AI: keep fallen fighters in the list until the round is over and skip them, and pass the turn on at once if the acting fighter fell.
- Waiting without end: Tell your AI: allow one wait per fighter per round, and refuse it when nobody living is left to act after them.
- A roll outside the range: Tell your AI: hold every roll to a whole number between 0 and the highest roll, and count a broken roll as 0.
- A fight nobody can win: Tell your AI: set a round limit and call the fight a draw when it is reached.
Do not change anything else in the game. Do not claim anything without proof: model what it takes, and show me what your solution rests on and how I can see that it works. If you cannot show something, say you do not know. When you are done, tell me in plain words how to try it.Or skip this and connect the MCP: your AI then finds all of this by itself.Connect the MCP
What it does
A fight with several fighters needs a fair and readable answer to who goes next. A score from speed and a little luck makes fast fighters act earlier without making the order the same every fight, and a fixed order per round lets the player plan ahead.
How it runs, as a diagram
Everything it can be in, and what moves it on. Your AI builds from exactly this.
How it runs, as a diagram
- Before the fight → First turn: fight begins
- Before the fight → Fight over: only one team came
- First turn → Later turn: turn ends or waits
- First turn → Fight over: last enemy falls
- Later turn → First turn: new round starts
- Later turn → Fight over: enemy falls or limit
Good starting values
| Value | Start with | What it changes |
|---|---|---|
| rollRange | 6 | The highest roll that is added to speed. Six means luck can move a fighter up or down by a few places but a much faster fighter still mostly goes first. Zero removes luck completely. |
| maxRounds | 30 rounds | After this many rounds the fight is called a draw. Two fighters who cannot hurt each other would otherwise go on forever. |
| rerollEachRound | 0 | One rolls again at the start of every round, zero keeps the first rolls. Keeping them lets the player learn who is next and plan, so zero is the safer start. One makes every round a surprise. |
What usually goes wrong
Two fighters with the same score
If the sort only looks at the score, equal fighters can swap places from one run to the next, depending on how the list happened to be arranged. The player sees an order that changes for no reason.
Fix: Tell your AI: break ties in a fixed way. Here the faster fighter goes first, and after that whoever joined the fight first.
Someone joins in the middle of a round
A reinforcement with a high score is put at the front of the list, so it either acts twice or pushes the current fighter aside and makes them act again.
Fix: Tell your AI: put the newcomer in by score, but if its place has already passed this round, it waits for the next round.
The acting fighter falls on their own turn
A poison or a trap can knock the acting fighter out. If the game removes them from the list, everyone behind them moves one place forward and the next fighter is skipped.
Fix: Tell your AI: keep fallen fighters in the list until the round is over and skip them, and pass the turn on at once if the acting fighter fell.
Waiting without end
If a fighter can wait as often as they like, two fighters can wait for each other forever, and the fight never moves on.
Fix: Tell your AI: allow one wait per fighter per round, and refuse it when nobody living is left to act after them.
A roll outside the range
A roll that is negative, huge or not a number lets a fighter act first every time or breaks the sort.
Fix: Tell your AI: hold every roll to a whole number between 0 and the highest roll, and count a broken roll as 0.
A fight nobody can win
Two sides that cannot hurt each other go through rounds without end.
Fix: Tell your AI: set a round limit and call the fight a draw when it is reached.
How to check it works
- Five fighters with scores 10, 9, 8, 7 and 6. The fight begins: They act in the order 10, 9, 8, 7, 6.
- Two fighters with the same score of 9, one with speed 8 and one with speed 5. The fight begins: The one with speed 8 acts first.
- Three fighters with the same speed and the same roll. The fight begins, twice: They act in the order they joined, both times.
- A fight in round 1. Every fighter ends their turn: Everyone acted once in the starting order, and round 2 begins with the same order.
- A fight where the second fighter in the order has fallen. The first fighter ends their turn: The third fighter acts, and the fallen one is gone from the next round.
- The first fighter in the order is acting. They fall: The second fighter acts at once and no one is skipped.
- A round is under way and a newcomer's score is higher than the acting fighter's. The newcomer joins: The acting fighter stays, and the newcomer goes first in the next round.
- The first fighter in the order is acting. They wait: The next fighter acts and the waiting one goes last in this round, then back to their score in the next.
- A fighter who has already waited is acting again. They try to wait once more: It is refused and they still have to act.
- The last living fighter of the round is acting. They try to wait: It is refused.
- A fight between two teams. The last fighter of one team falls: The fight is over and the other team has won.
- A fight with a limit of 3 rounds where nobody falls. The third round ends: The fight is over as a draw.
- A roll of 500, a roll of -500 and a roll that is not a number. They are used: They count as 6, 0 and 0.
- Rolling again each round is on and two fighters have the same speed. A new round starts with new rolls: The order follows the new rolls.
- A fighter is already in the fight. The same name joins again, or a fighter with a broken speed joins: Nothing changes.
Works well with
- Turn-Based Combat: Turn-based combat decides what a turn can do. Initiative decides whose turn it is. Keep the order in one place and let the combat read from it. Sort the fighters first, then let each one act in that order.
- Action Points: A fighter who waits keeps the points for later in the round, so decide whether waiting costs some. Hand out the points when the fighter's turn starts.
Common questions
how do i add turn order to my game
Paste this: Add turn order by initiative. Each fighter gets a score of its speed plus a roll from 0 to 6, the fighters act from the highest score to the lowest, ties go to the faster fighter and then to whoever joined first, and the order stays the same for the whole round. Skip fighters who have fallen.
should initiative be rolled every round or once
Once is the safer start. The player can learn who comes next and plan around it. Rolling every round makes fights less predictable, but it is harder to read and to balance.
what happens when two fighters have the same initiative
Decide it by a rule and write it down. Here the faster fighter goes first, and after that whoever joined the fight first. Never leave it to the order the list happens to be in.
can a new enemy join in the middle of a round
Yes. Place it by its score, but if that place has already passed in this round, let it wait for the next round. Otherwise it acts out of turn or twice.
Where it comes from
We could not find a reliable origin for this one yet.
What was checked
- 18 tests pass on the reference code.
- The state diagram was checked against the code, at the default values and at every listed low and high value.
Your AI gets the code
Tested example code comes through the Spielerando connection, so your AI can use it right away. How to connect it