# RMMVCTB Math (turn order)

#### Frostorm

##### []D[][]V[][]D
So I'm trying to learn the math/mechanics behind a CTB battle system. I found this Googling but found some of it confusing. Specifically, step 1 says that:
Calculate the average speed of each member of the combat, (sum all of the speeds of enemies and characters, then divide the members speed by this sum, you know, average)

TotalSpeed = SUM(Members)

Member.AverageSpeed = Member.Speed / TotalSpeed
However, shouldn't a unit's avg speed be `TotalSpeed` divided by # of units?

What should my approach be if my current battle system (which I'm trying to edit, not replace) currently manages turn order via an array in which the order of said array elements dictates the turn order? This is the function that handles turn order in LeTBS (Lecode's Tactical Battle System):
JavaScript:
``````BattleManagerTBS.determineTurnOrderSimple = function () {
var array = [];
this._turnOrder = [];
this._activeIndex = 0;

this.allPlayableEntities().forEach(function (entity) {
array.push(entity);
});
array = array.sort(function (a, b) {
return eval(Lecode.S_TBS.turnOrderSortMethod);
});
this._turnOrder = array;
this._turnOrderVisual.set(this._turnOrder);
};``````
Note that `return eval(Lecode.S_TBS.turnOrderSortMethod)` simply refers to: `b._battler.luk - a._battler.luk`
Basically, LUK is my Speed stat. Anyway, I'm simply trying to figure out how to manipulate the array so that the turn order can have a CTB mechanic instead of the basic "fastest to slowest" turn order.

Last edited:

#### ATT_Turan

##### Forewarner of the Black Wind
What should my approach be
I would download an existing CTB plugin (Yanfly's is free) and see how it's implemented. It's got a fair number of moving parts.

#### Alexandre

##### Veteran
However, shouldn't a unit's avg speed be `TotalSpeed` divided by # of units?

As the article itself says, it uses, I quote 'terrible pseudo code', so yes, it is good to keep your logical thinking while implementing it.

The article probably doesn't have in mind apparition of enemies mid-way during combat, so for it # of units basically a constant since it will be the same for everyone. You could replace it by any other constant, it wouldn't change the core thinking.

PS: Yep CTB implementation is closer to a weird ATB, I wouldn't take too much inspiration from it.

#### Frostorm

##### []D[][]V[][]D
Eureka! I believe this is the critical piece I was looking for!~
JavaScript:
``````BattleManager.ctbTurnOrder = function() {
var battlers = \$gameParty.aliveMembers().concat(\$gameTroop.aliveMembers());
battlers.sort(function(a, b) {
return 0;
});
return battlers;
};``````
...and here's how it compares to LeTBS:
JavaScript:
``````BattleManagerTBS.determineTurnOrderSimple = function () {
var array = [];
this._turnOrder = [];
this._activeIndex = 0;

this.allPlayableEntities().forEach(function (entity) {
array.push(entity);
});
array = array.sort(function (a, b) {
return eval(Lecode.S_TBS.turnOrderSortMethod);
});
this._turnOrder = array;
this._turnOrderVisual.set(this._turnOrder);
};``````
P.S. `Lecode.S_TBS.turnOrderSortMethod` = `b._battler.agi - a._battler.agi`

I'm just glad that in both cases (Yanfly & LeCode), the turn order is managed in an array. That makes it easier for me to integrate the CTB mechanic into LeTBS.

Edit: I noticed that in Yanfly's code, the sort function returns either -1, 0, or 1. However, in LeTBS, the sort function will usually return a much wider range of values. Is that something that makes a difference? Or is it perhaps just the positive/negative-ness of the returned value that matters?

Hmm, looks like this is where I should apply the "CTB math" in order to implement CTB in LeTBS:
JavaScript:
``````BattleManagerTBS.nextTurn = function () {
do {
this._activeIndex++;
if (this._activeIndex >= this._turnOrder.length) {
this._activeIndex = 0;
this.onTurnOrderEnd();
}
if (this.activeEntity().isTurnHidden()) {
this.nextTurn();
return;
}
this._turnOrderVisual.updateOnNextTurn(this._turnOrder, this._activeIndex);
this.startTurn();
};``````
...but instead of `this._activeIndex++;`, I need to add/subtract the units Speed stat...or is it supposed to be the average Speed?

Last edited:

#### Alexandre

##### Veteran
99% of the sorting methods only care about the sign of the comparaison.

### Latest Profile Posts

It has been brought to my attention that the name of my main robot protagonist in bio-Synthetica (R3-M1) is a Star Wars character LOL Woops. Always Google search names. The new name of my robot is now K0-R1.
I'm considering making my own battlers from scratch, it seems like a daunting task, but I like the look of it so much...
I picked up running again after a long time and managed to get from 2.25km to 7km in two weeks :3 Time to get rid of those covid kilos!
Have you ever wondered what the lives of the NPC's are like in your hero's story? Come an find out with us as we play, "A Story Beside" by Wayward Prophet

Never in my life did I dream that I would be the proud owner of 5 jellyfish.