Efficient RTS "fog of war" setup.

Blitz3D Forums/Blitz3D Programming/Efficient RTS "fog of war" setup.

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

The code above is very slow with many units.. Here is a new fog of war method, using Psuedo grid, and type structure that flags everything visable or not. Here it is.
NOTE:

I was able to make 500 enemies without any real noticable
slowdowns on my machine.. P4 2.4GHZ GeforceTI4200.
Could someone else with a lower spec machine test it out?

EDIT: I updated code with Banks, and it moves 2x fast. 1000 enemies moving around going in and out of fog of war. I now have it set so that the instant they are out of a "players units" area they fade into fog of war even if the area of terrain has been explored. All done via phsuedo map and banks.. No b3d collision or distance checking done.

Ill post more code if anyone is interested.


;the Psuedo Grid (500,500) is equal to real world 0,0
;VALUES if =0 then invisable area, =1 then visbale
Dim mapgrid(1000,1000)
;This is how large each grid square will be in real XZ world values, play with this to see how it works
Global gridindex=20

;tree type
Type tree
Field entity
Field woodleft
Field Visable
End Type


;all units have these properties
Type Unit
Field Friendly
Field Visable
Field Entity
End Type



Graphics3D 640,480

;create friendly unit
U.unit=New unit
u\friendly=True
u\visable=True
u\entity=CreateCube()
EntityColor u\entity,0,0,200
PositionEntity u\entity,Rand(-20,20),0,Rand(-20,20)

;create a few enemies
For i=1 To 5
U.unit=New unit
u\friendly=False
u\visable=False
u\entity=CreateCube()
EntityColor u\entity,200,0,0
PositionEntity u\entity,Rand(-35,35),0,Rand(-35,35)
Next



;put some ground underneath it all
ground=CreatePlane()
EntityColor ground,0,100,0


For I=1 To 100
t.tree=New tree
T\entity=CreateCone()
PositionEntity t\entity,Rand(-100,100),0,Rand(-100,100)
EntityAlpha t\entity,.2
EntityColor t\entity,0,200,0
ScaleEntity t\entity,4,4,4
Next

;our camera
camera=CreateCamera()
temp=CreatePivot()
PositionEntity camera,10,40,40
PointEntity camera,temp
FreeEntity temp
CameraFogMode camera,1
CameraFogColor camera,100,100,100
CameraFogRange camera,25,190

;main loop
While Not KeyDown(1)
For u.unit=Each unit
checkgridarea(u)
;if unit is friendly we change grid he is in to
;visable, if unit is enemy we just check to see if he is
;inside of a visable area or invisable area, in in visable
;we change his alpha and visable state.
If u\friendly=True Then
MoveEntity u\entity,0,0,.1
TurnEntity u\entity,0,-.35,0
Else
MoveEntity u\entity,0,0,-.1
TurnEntity u\entity,0,.35,0
EndIf
Next
RenderWorld()
UpdateWorld()
Flip
Wend


Function CheckGridArea(cu.unit)
entity=cu\entity
Px=EntityX(entity,1)
Pz=EntityZ(entity,1)
gridX=Int((px/gridindex))+500
gridZ=Int((pz/gridindex))+500
If cu\friendly=True Then
If MapGrid(Gridx,Gridz)=0 Then
mapgrid(gridx,gridz)=1
;tree check not neccessary in a rts game as
;you cn usually see the terrain. You could get
;by with only unit checks. wich are fast.
For t.tree=Each tree
If t\visable=0 Then
If EntityDistance(t\entity,entity)<=GridIndex Then
t\visable=1
EntityAlpha t\entity,1
EndIf
EndIf
Next
EndIf
Else
;enemy/unfriendly unit check.. does not reveal map
;only reveals unit if map area is already explored
If mapgrid(gridx,gridz)=1 Then
cu\visable=1
EntityAlpha cu\entity,1
Else
cu\visable=0
EntityAlpha cu\entity,.1
EndIf
EndIf
End Function