9 Replies Latest reply on Dec 30, 2006 12:48 PM by kglad

# RPG Battle System Order

I am trying to develop a battle system order engine which determines the order of turns according to player(s) and enemy(s) speed. For example, let's say that player A has a speed of 4 and player B has a speed of 2. The order of battle would be like this: Player A, Player A, Player B, Player A, Player A, Player B, Player B, etc. Since Player A has twice as much speed has Player B, player A goes twice for every turn that Player B makes. Does this make since?

I have absolutly no clue how to do this (though I am experienced in AS, I haven't ever came across this kind of scenario - so I have absolutly NO clue where/how to begin and debug it. If isn't to much to ask, can someone provide a small snippet of code for me. Please?

*Also, if it isn't too much to ask, can you make flexible enough so that there would be more players in battle (Player C with a speed of 1, Player D with a speed of 5)? This isn't a biggy right now, but would be very nice. Thanks
• ###### 1. Re: RPG Battle System Order
Another way of thinking about it: If the current turn has a number n, then an avatar with speed c will act on turns c(1), c(2), c(3), and so on. You can freely extend this to as many players as necessary by having a turn object which contains all avatars which should act during that turn. If you have an array of these Turn objects, you can insert avatar turns into the Turns at indices which are multiples of c, where c is that avatar's speed. I'm sorry for being so incoherent; did that make any sense at all?
• ###### 2. Re: RPG Battle System Order
to be honest, I don't. But I almost am catching what you are putting down.

Again, if you or anyone can provide some code for this (again, I have never done anything of this sort before) little problem I am struggling to understand, it would be great. Thanks
• ###### 3. Re: RPG Battle System Order
unless you can make some assumptions about that player scheme you have a tough problem on your hands:

the difficult part of your scheme is finding the least common multiple of all the speeds and there's no easy way for flash to do that. however, if you can assume all speeds will be less than some maximum number (like 20 or 100) the problem is more easily solved. or if player A's speed is 6 and play B's speed is 2 and you can accept player A having 6 turns followed by player B's 2 turns, the problem is easier to solve.
• ###### 4. RPG Battle System Order
Thinking about it further, all you would have to do would be to track the turn number (call it n). If a given character's speed (call it c) divides evenly into n (if n%c==0) then that character has an action during that turn. Test all characters in each turn, and then figure out order of execution. That solves the LCD issue, and conserves memory because you don't need to have a lot of Turn objects in which everyone is just waiting.
• ###### 5. Re: RPG Battle System Order
that's not correct, bob r.
• ###### 6. RPG Battle System Order
Bob R, that sounds really good to be honest. I actually talked to an my friend, who is an really experience programmer, and that is what he suggested as well - counting the number of turns that has passed during battle and checking the speed for each character using the modulous (% - if I spelled that correctly).

Again, can you pelase provide the AS to accomplish this? I have asked my friend, but unfortually, he doesn't know how to this with AS (it's not one of his favs). No graphics or anything, just the AS. I hope this doesn't seem to much to ask but again, I have never done this before and I don't have an idea on how to begin the AS. (if this isn't too much to ask)

*Sorry for the long reply, but thats what happens when you travel
• ###### 7. Re: RPG Battle System Order
if your players have instance names tl["player"+i] where i=1,..,playerNum, then you can use the following to calculate a turnDivisor for each player. if the turn number divided by that player's turnDivisor has remainder zero, that player has a turn on that turn number. if the remainder is not zero, the player has no turn on that turn number:

• ###### 8. Re: RPG Battle System Order
What does 'commonMultiple' do?

This is what I have:

playerNum = Number(2);

for(var i = 1; i <= 2; i++) {
_root['player_'+i] = new Object();
_root['player_'+i].speed = random(3)+1;
trace('speed: '+_root['player_'+i].speed);
}

function computeTurnNumbers() {
commonMultiple = 1;
for (var i = 1; i<=playerNum; i++) {
//commonMultiple *= tl["player"+i].speed;
commonMultiple *= _root["player_"+i].speed;

trace('commonMultiple: '+commonMultiple);
trace('divided: '+commonMultiple/_root["player_"+i].speed);

}
//tl["player"+i].turnDivisor = commonMultiple/tl["player"+i].speed;
}

computeTurnNumbers();

trace('');trace('');
• ###### 9. Re: RPG Battle System Order
commonMultiple is used to compute each player's turnDivisor. commonMultiple is the product of all player speeds. ie, it is a common multiple of the player speeds.

it's not the least common multiple but i don't think you (nor anyone else that's posted on this thread) understand why the least common multiple of the player speeds might be preferred and, in fact, it might not be important if you have just a few players and none have high speeds.

so, you can use a commonMultiple to compute turnDivisor and you can then use