ID:166131
 
I was planning on making battle round's attack sequence based on how high your agility is determining who attacks first out of up to 4 party members and 8 enemies. Does anyone have any suggestions on how to do this in an efficent manner?
Hmm... You could try looping through the party members and recording there agility with a variable.

e.g.
var/cur = 1
var/turn_num = 1
for(member in party)
if(member.agility > cur)
turns[++turn_num] = member


And then use the turns[] list for the rest of the battle.
If the list won't be too big, you can use a simple sorting algorithm like bubblesort or selection sort to sort the players by how high their agility is.

battle
var/list/allmembers = list()


proc/SortPlayersByAgility()
for(var/i=L.len,i>0,i--)
for(var/j=1,j<i,j++)
var/mob/A = L[j]
var/mob/B = L[j+1]
if(A.agility > B.agility)
L.Swap(j,j+1) // swap if the first character's agility is higher


~~> Unknown Person
In response to DivineO'peanut
but what if by some off chance the agl changes during the battle?
like say do to a player ability or a monster spell.
*just curious mostly for my own info :D*
In response to Unknown Person
I'm already sure the list will never be higher than 12, 4 PCs and 8 monsters max but 8 monster parties are rare. How big is big when it comes to bubblesort?
In response to Hork
For 12 things, bubblesort will be plenty fine. I'd suggest selection sort, which is basically bubblesort with intermediate steps removed, but eh.

Bubblesort only starts getting slow when you're talking, say, 100 items. Until then, pretty much all sorting algorithms run in about the same time. And, in fact, the complex ones can often run slower, because of setting-up-time they might require.