order values from lowest to highest, or from highest to lowest


👉 BlitzCoder will be building a new platform and other plans to preserve and continue the Blitz legacy.

To be able to achieve this goal, we need your support by becoming a Patreon Paid Member 👈

 

Tweet blitz3d blitzbasic code-archives miscellaneous
RemiD
;order values from lowest to highest, or from highest to lowest by RemiD (20180328-1333)
;can be useful to determine which is the nearest entity around player (depending on distance), and target this entity in priority
;can be useful to determine which is the last updated entity around player (depending on millitime), and update this entity in priority

Graphics( 854, 480, 32, 2 )

SeedRnd( MilliSecs() )

;to order values (from lowest to highest or from heighest to lowest)
Global orderedscount%
Dim orderedh%(100) ; handle
Dim orderedvalue#(100) ; value

;a list of entities
Global entitiescount%
Type Tentity
 Field renderer
 Field d# ; distance from player to entity
 Field considered% ; false or true
End Type

;create some entities
For n% = 1 To 10 Step +1
 entitiescount = entitiescount + 1
 ent.Tentity = New Tentity
 ent\d# = Rnd( 10, 1000 )
Next
;debug each entity
For ent.Tentity = Each Tentity
 DebugLog( Handle(ent)+" | "+ent\d )
Next

DebugLog("")
;ORDER FROM LOWEST TO HIGHEST
;reset considered state of all entities
For ent.Tentity = Each Tentity
 ent\considered = False
Next
;reset ordereds
orderedscount = 0
;order the entities by distance (from lowest to highest)
maxloops% = entitiescount
loops% = 0
While( loops < maxloops )
 loops = loops + 1
 ;find the lowest value in the not considered entities
 svalue# = +1000000
 sh% = 0
 For ent.Tentity = Each Tentity
  If( ent\considered = False )
   If( ent\d < svalue )
    svalue = ent\d
    sh = Handle( ent )
   EndIf
  EndIf
 Next
 ;set the entity with the lowest value as considered
 ent.Tentity = Object.Tentity(sh) 
 ent\considered = True
 ;create a new ordered instance
 orderedscount = orderedscount + 1 : i% = orderedscount
 orderedvalue(i) = svalue
 orderedh(i) = sh
Wend
;choose the lowest distance value and the corresponding entity
svalue# = orderedvalue(1)
sh% = orderedh(1)
;update this entity
ent.Tentity = Object.Tentity(sh) 
;entitycolor( ent\renderer, 000, 250, 250 )
;debug nearest entity
DebugLog( entity+Str(sh)+" is the nearest, at "+ent\d+"units." )

DebugLog("")
;ORDER FROM HIGHEST TO LOWEST
;reset considered state of all entities
For ent.Tentity = Each Tentity
 ent\considered = False
Next
;reset ordereds
orderedscount = 0
;order the entities by distance (from highest to lowest)
maxloops% = entitiescount
loops% = 0
While( loops < maxloops )
 loops = loops + 1
 ;find the highest value in the not considered entities
 svalue# = -1000000
 sh% = 0
 For ent.Tentity = Each Tentity
  If( ent\considered = False )
   If( ent\d > svalue )
    svalue = ent\d
    sh = Handle( ent )
   EndIf
  EndIf
 Next
 ;set the entity with the highest value as considered
 ent.Tentity = Object.Tentity(sh) 
 ent\considered = True
 ;create a new ordered instance
 orderedscount = orderedscount + 1 : i% = orderedscount
 orderedvalue(i) = svalue
 orderedh(i) = sh
Wend
;choose the highest distance value and the corresponding entity
svalue# = orderedvalue(1)
sh% = orderedh(1)
;update this entity
ent.Tentity = Object.Tentity(sh) 
;entitycolor( ent\renderer, 000, 250, 250 )
;debug farest entity
DebugLog( entity+Str(sh)+" is the farest, at "+ent\d+"units." )

WaitKey()

End()

Reply To Topic (minimum 10 characters)

Please log in to reply