Sorting Types Instead of Arrays

Blitz3D Forums/Blitz3D Programming/Sorting Types Instead of Arrays

Canana(Posted 2016) [#1]
Hi guys! Haven't been on in a while and I was doing a 7day Challenge(TODAY IS THE LAST DAY PLEASE RESPOND SOON) to program a Plants Vs Zombies clone(Im pretty much done just a few graphical errors) (plus its called ZomBz vs Plantz and you are playing as the zombies)

Anyway I was going in my code and instead of the zombies being rendered by how high they are, they were being drawn in the order they were created(should have expected that) So I needed a sorter that would sort the Zombies by their Y values(Y=0 is first, Y=700 is last)

I know how to sort arrays but with types, im lost XD
so any help would be appreciated :D

-Canana


ps for the For loop on the Zombie type This is what I use

;>---TYPE LOOP---<
For Z.Zombie=Each Zombie
Z\Y=Z\y ; This is the zombies Y value
Next
;---END LOOP---<


Canana(Posted 2016) [#2]
Nevermind about the respond soon, I created a loophole XD

For Row_Selected=0 to 6 ;(seven rows= 0,1,2,3,4,5,6)

For Z.Zombie=Each Zombie

If Z\Y=Row_Selected
;..do zombie logic(run forward, target healers, etc)
RenderZombie(z\x,z\y,z\_type,z\health,z\power,z\armor)
Endif

Next

Next


It may make the game a bit slower, so if you can figure out a way to actually sort the type Zombie that would be great :D (They only can have 7 Y Values from 0 to 6)


Stevie G(Posted 2016) [#3]
The overheads for the above will be almost nil ... but the code below will sort the list based on Y field.




RemiD(Posted 2016) [#4]
You could also create another type list

Type Ordered
 Field H% ;the instance handle
End Type


And then you analyze all instances of your initial type list and depending on the value, get the instance handle and add it to the ordered type list.

MaxCount% = 100 ;number of instances in your list
For ti.Thing = Each Thing
 ti\Processed = False
Next
ProcessedsCount% = 0
LoopState = True
Repeat
 CurValue = 1000.0 ;the max value possible
 CurH% = 0
 For ti.Thing = Each Thing
  If( ti\Processed = False )
   If( ti\Life <= CurValue )
    CurValue = ti\Life
    CurH = Handle(ti)
   EndIf
  EndIf
 Next
 ProcessedsCount = ProcessedsCount + 1
 ti.Thing = Object.Thing(CurH)
 oi.Ordered = New Ordered
 oi\H = Handle(ti)
 ti\Processed = True
 If( ProcessedsCount = MaxCount )
  LoopState = False
 EndIf
Until( LoopState = False )


Then you can browse you ordered type list to get the instances ordered by the value you chose...


Floyd(Posted 2016) [#5]
Yikes! My code archive entries are almost fourteen years old.

Insertion sort with type list. http://www.blitzbasic.com/codearcs/codearcs.php?code=220

There is also an improved version using a sentinel. This is a dummy item which marks the beginning of the list. With this you no longer have to check for "running off the end" with every insertion.

Sentinel version. http://www.blitzbasic.com/codearcs/codearcs.php?code=221

Note these are for integers. For floats you have to make small changes. Index% would change to Index# and temp% would be temp#. If using the sentinel version then compare to MinFlt instead of MinInt.