ID:2384364
 
(See the best response by Kaiochao.)
I'm working on a new combat system that will be able to play host to multiple people. I also would like for it to allow newcomers to enter the fray should they stumble upon the battle. So I need to design a proc that will be able to index and re-index combatants within the turn order list based on their speed. Is this possible? Or is there perhaps a more efficient and practical way to go about this? Any prod in the right direction would be really helpful.

Best response
There are some "list sorting" libraries out there that let you sort lists according to whatever ordering you want. Swapping elements in a list by index is done with list.Swap().
In response to Kaiochao
Kaiochao wrote:
There are some "list sorting" libraries out there that let you sort lists according to whatever ordering you want. Swapping elements in a list by index is done with list.Swap().

Thank you so much. I shall look into this.
So I've been toying with deadron's "List" library as it was the simplest for me to grasp, but it sorts in an ascending manner. I need the list to descend based on their speed. Is there a quick way for me to inverse the list returned by his proc, or do I need to re-write his proc alltogether?

Edit:This is currently what I'm working with, but it seems to only transfer the indices of the list and not the actual mobs within.

obj/engagement
var/list/combatants = new/list()
var/list/upcomingturns = new/list()
density = 0
icon = 'Engagement Flag.dmi'
New()
loc = locate(27,20,1)
for(var/mob/P in view())
combatants += P
for(var/mob/P in combatants)//Display combatants for testing purposes
world << P
TURNORDER()
for(var/P in upcomingturns) //Display the order for testing purposes
world << P
..()




obj/engagement/proc

TURNORDER()
var/list/C = new/list()
C += dd_sortedObjectList(combatants)
for(var/i = C.len to 1 step -1)
upcomingturns += i
Deadron's List library adds a dd_SortValue() proc to all datums that you can override to give things a different order. In your case, you'd override that on your mobs to return their speed. Something like this:
mob/dd_SortValue() return speed

Of course, this is just an example, and it's very limiting because mobs can only be sorted by speed.

I recommend Theodis's QuickSort library, which includes a sorting proc that takes another proc as an argument that is used for comparing two objects. For example:
// Some proc to compare two mobs by speed:
// (this proc can be defined anywhere, as it doesn't use src)
mob
proc
SortBySpeed(mob/a, mob/b)
return a.speed - b.speed

// Sort a list by speed
upcomingturns = combatants.Copy()
QuickSort(upcomingturns, /mob/proc/SortBySpeed)
In response to Kaiochao
Kaiochao wrote:
Deadron's List library adds a dd_SortValue() proc to all datums that you can override to give things a different order. In your case, you'd override that on your mobs to return their speed. Something like this:
> mob/dd_SortValue() return speed
>

Of course, this is just an example, and it's very limiting because mobs can only be sorted by speed.

I recommend Theodis's QuickSort library, which includes a sorting proc that takes another proc as an argument that is used for comparing two objects. For example:
> // Some proc to compare two mobs by speed:
> // (this proc can be defined anywhere, as it doesn't use src)
> mob
> proc
> SortBySpeed(mob/a, mob/b)
> return a.speed - b.speed
>
> // Sort a list by speed
> upcomingturns = combatants.Copy()
> QuickSort(upcomingturns, /mob/proc/SortBySpeed)
>


This helped me a ton, thank you.