ID:269096
 
        orderturns()
src.turns=src.battlers.Copy()
for(var/mob/M in src.turns)
for(var/mob/Q in src.turns)
if(M==Q) continue
if(M.agi>Q.agi)
src.turns.Swap(M,Q)
else if(M.agi<Q.agi)
src.turns.Swap(Q,M)
else if(M.agi==Q.agi)
if(rand(1,2)==2)
src.turns.Swap(M,Q)
else
src.turns.Swap(Q,M)
for(var/a in src.turns)
world<<a

*cough*
I can't seem to get this right... =/
Sorry for being a nub...but, meh.
Thanks for reading...
You don't quite get how to use Swap(). It swaps two things. Calling Swap() with the two arguments in a different order makes no difference because it still swaps them. Moreover, it doesn't take mobs as arguments.

Also, I have no idea why you're using if(rand(1,2)==2) rather than if(prob(50)). Of course, neither is really right here. If you want all items with the same agi value to be randomly positioned in their part of the list, mere one-pass swaps won't do.

Lummox JR
In response to Lummox JR
Well, I'm a little shady on this (I even looked at the refrence example), and I don't get it. =/
Sorry for being the "bash in my brains until I learn" kind of person...but, I don't really get this...
In response to Hell Ramen
Hell Ramen wrote:
Well, I'm a little shady on this (I even looked at the refrence example), and I don't get it. =/

That sounds about right, since the reference very clearly says you need to give it indexes, not the actual items in the list.

But then it's also kind of obvious that swapping A and B is the same as swapping B and A, so I'm not sure where that could be losing you.

Lummox JR
In response to Lummox JR
Lummox JR wrote:
Hell Ramen wrote:
Well, I'm a little shady on this (I even looked at the refrence example), and I don't get it. =/

That sounds about right, since the reference very clearly says you need to give it indexes, not the actual items in the list.

But then it's also kind of obvious that swapping A and B is the same as swapping B and A, so I'm not sure where that could be losing you.

Lummox JR

I get that now, (since you said that, I should've been more specific), I just don't get how to give them indexes. =/

Would I go like...
mob/verb/addturn(mob/M as mob in world)
src.turns+=params2list("[M]=[src.turns.len+1]")

or something?
Then give the mob a var, such as position, and change the position according to that var and use M.pos and Q.pos instead?
In response to Hell Ramen
Hell Ramen wrote:
I get that now, (since you said that, I should've been more specific), I just don't get how to give them indexes. =/

Um... seriously?

Just by putting them in the list they have an index. That's what an index is. The index is the item's position in the list.

Would I go like...
mob/verb/addturn(mob/M as mob in world)
src.turns+=params2list("[M]=[src.turns.len+1]")
or something?
Then give the mob a var, such as position, and change the position according to that var and use M.pos and Q.pos instead?

No. Don't do anything like that.

Lummox JR
In response to Lummox JR
Thanks for the help so far...sorry for being a drag...but, I've got another stupid question... =/
Is there a way I can find the index of an item?
In response to Hell Ramen
Hell Ramen wrote:
Thanks for the help so far...sorry for being a drag...but, I've got another stupid question... =/
Is there a way I can find the index of an item?

Well, you could use Find(), but in fact you don't need to. If instead of doing for(A in list), for(B in list), etc., you used a var going from 1 to list.len, you could save a lot of hassle. What's more, the inner loop doesn't need to cover the entire loop.

What might help you is to read a good Web page on simple sorting algorithms.

Lummox JR
In response to Lummox JR
<_<
Okay, I've done what you said, and the only one I understand is the "Bubble Organization" thingie.
"It's the simplest and least efficient". x_x
Bah, I'll come back to this later.
In response to Hell Ramen
Hell Ramen wrote:
<_<
Okay, I've done what you said, and the only one I understand is the "Bubble Organization" thingie.
"It's the simplest and least efficient". x_x
Bah, I'll come back to this later.

A bubble sort is really the worst choice for most sorting operations. It's the simplest, it's true, but that's because it sucks. There are other sorts you can do that are much better. A simple one I often use, which is good for small lists, is selection sort:
var/i,j,k
for(i=1, i<L.len, ++i) // note: i goes from 1 to L.len-1
k = i
var/mob/Mk = L[k]
for(j=i+1, j<=L.len, ++j) // j goes i+1 to L.len
var/mob/Mj = L[j]
if(Mj.agi > Mk.agi) // sorting from highest agi to lowest
Mk = Mj; k = j
// if a more agile mob found (at k), swap position i with it
if(k != i) L.Swap(i, k)


Selection sort is pretty simple to implement, and less of a disappointment than the bubble sort, so I use it a lot. I think for the problem of simply organizing sorting mobs in battle according to agility, it's the best way to go. It works by finding the smallest or greatest value each time, whose position is stored in the var k, and choosing it as the first value in the list, then the second, then the third, and so on.

If you want to add the additional step of shuffling people in the list whose agility is the same, then you might want to do this each time in the battle routine that you get to the beginning of the list:
// After the list has been sorted, shuffle mobs with the same agility.
// It could be done while sorting, but this will do it each round, removing
// predictability and some luck-of-the-draw advantage.
var/i,j
while(L.Remove(null)) // clear list of nulls killed in battle
// do nothing in this while loop
for(i=1, i<L.len, ++i) // note this is similar to selection sort
var/mob/Mi = L[i]
for(j=i+1, j<=L.len, ++j)
var/mob/Mj = L[j]
if(Mj.agi < Mi.agi) break
if(--j > i) // if there's anything to shuffle
while(i < j)
L.Swap(i, rand(i, j))
++i


Of course if you ever need heavy-duty sorting of a lot of items quickly, then QuickSort is by far the best choice available.

Lummox JR
In response to Lummox JR
Lummox JR wrote:
What might help you is to read a good Web page on simple sorting algorithms.

Wikipedia has a good one at http://en.wikipedia.org/wiki/Sort_algorithm.
In response to Lummox JR
Thanks Lummox... ;o
I would've never have figured that out.