I started my first RTS project in DBP yesterday and have a few questions. First, I've made a media-free version of it to post here.
REM Project: rts
REM Created: 7/7/2007 3:32:34 PM
REM
REM ***** Main Source File *****
REM
`Setup
set display mode 1024,768,32
sync on
sync rate 60
`Show when loading
sync : center text screen width()/2, screen height()/2, "Loading" : sync
`Global variables and constants
#constant TRUE 1
#constant FALSE 0
`Camera
cam = 1
camSpeed# = 2.5
camSpeedFast# = 5
global camX# = 0
global camY# = 50.0
global camZ# = 0
make camera cam
position camera cam,camX#,camY#,camZ#
rotate camera cam,35,0,0
`Terrain
terrainMat=1
terrainSizeX=1000
terrainSizeZ=1000
make matrix terrainMat,terrainSizeX,terrainSizeZ,80,80
`Temperary units
maxUnits=9
dim unit(maxUnits)
dim selected(maxUnits)
dim targetX(maxUnits)
dim targetZ(maxUnits)
dim speed#(maxUnits)
dim marker(maxUnits)
for i=0 to maxUnits
`Make unit
unit(i)=10000+i
if (i<((maxUnits/2)+1))
make object cube unit(i),6
speed#(i)=0.2
else
make object cube unit(i),3
speed#(i)=0.5
endif
position object unit(i),rnd(200),0,rnd(200)
yrotate object unit(i),180
fix object pivot unit(i)
selected(i)=FALSE
targetX(i)=object position x(unit(i))
targetZ(i)=object position z(unit(i))
`Make "selected unit" marker
marker(i)=20000+i
make object sphere marker(i),10
color object marker(i),rgb(100,100,155)
position object marker(i),object position x(unit(i)),object position y(unit(i)),object position z(unit(i))
ghost object on marker(i)
hide object marker(i)
next i
`Main loop
do
`Information
ink rgb(255,255,255),0
set cursor 0,0
print "Screen FPS: "+str$(screen fps())
`Move camera
camX#=camera position x(cam)
camY#=camera position y(cam)
camZ#=camera position z(cam)
zMouseMove# = mousemovez()
camY# = camY# - zMouseMove# / 50
camZ# = camZ# + zMouseMove# / 50
position camera cam,camX#,camY#,camZ#
if (mousex()<16) then position camera cam,camX#-camSpeed#,camY#,camZ# `Left
if (mousex()>screen width()-16) then position camera cam,camX#+camSpeed#,camY#,camZ# `Right
if (mousey()<16) then position camera cam,camX#,camY#,camZ#+camSpeed# `Up
if (mousey()>screen height()-16) then position camera cam,camX#,camY#,camZ#-camSpeed# `Down
if (mousex()<16 and mousey()<16) then position camera cam,camX#-camSpeed#,camY#,camZ#+camSpeed# `Up-left
if (mousex()<16 and mousey()>screen height()-16) then position camera cam,camX#-camSpeed#,camY#,camZ#-camSpeed# `Down-left
if (mousex()>screen width()-16 and mousey()<16) then position camera cam,camX#+camSpeed#,camY#,camZ#+camSpeed# `Up-right
if (mousex()>screen width()-16 and mousey()>screen height()-16) then position camera cam,camX#+camSpeed#,camY#,camZ#-camSpeed# `Down-right
`Select unit(s)
gosub _drawbox
`Position unit and marker ; Show marker if unit is selected
for i=0 to maxUnits
position object unit(i), object position x(unit(i)), get ground height(terrainMat,object position x(unit(i)),object position z(unit(i))), object position z(unit(i))
position object marker(i),object position x(unit(i)),object position y(unit(i)),object position z(unit(i))
if (selected(i)=TRUE)
show object marker(i)
else
hide object marker(i)
endif
next i
for i=0 to maxUnits
`Set target
if (mouseclick()=2 and selected(i)=TRUE)
targetX(i)=convertCoordX()
targetZ(i)=convertCoordZ()
endif
`Stop selected units by pressing "S"
if (keystate(31)>0 and selected(i)=TRUE)
targetX(i)=object position x(unit(i))
targetZ(i)=object position z(unit(i))
endif
`Move selected units to their targets ; Make them stop within 1 coordinate of their target since they rarely end up exactly on target
if ((object position x(unit(i))>targetX(i)+1 or object position x(unit(i))<targetX(i)-1) and (object position z(unit(i))>targetZ(i)+1 or object position z(unit(i))<targetZ(i)-1))
point object unit(i),targetX(i),get ground height(terrainMat,object position x(unit(i)),object position z(unit(i))),targetZ(i)
move object unit(i),speed#(i)
else
if (object position x(unit(i))>targetX(i)+1 or object position x(unit(i))<targetX(i)-1)
point object unit(i),targetX(i),get ground height(terrainMat,object position x(unit(i)),object position z(unit(i))),targetZ(i)
move object unit(i),speed#(i)
endif
if (object position z(unit(i))>targetZ(i)+1 or object position z(unit(i))<targetZ(i)-1)
point object unit(i),targetX(i),get ground height(terrainMat,object position x(unit(i)),object position z(unit(i))),targetZ(i)
move object unit(i),speed#(i)
endif
endif
next i
`Syncronize
sync
loop
_drawbox:
for i=0 to maxUnits
_mouse_click = mouseclick()
if _mouse_click = 1 and selectionFlag = 0
selectionFlag = 1
bx = mouseX()
by = mouseY()
endif
if _mouse_click = 1 and selectionFlag = 1
bx2 = mouseX()
by2 = mouseY()
endif
if _mouse_click <> 1 then selectionFlag = 0
if selectionFlag = 1
if bx < bx2
bx0 = bx
bx1 = bx2
else
bx0 = bx2
bx1 = bx
endif
if by < by2
by0 = by
by1 = by2
else
by0 = by2
by1 = by
endif
line bx0, by0, bx1, by0
line bx0, by1, bx1, by1
line bx0, by0, bx0, by1
line bx1, by0, bx1, by1
cx = object screen x(unit(i))
cy = object screen y(unit(i))
if cx > bx0 and cx < bx1 and cy > by0 and cy < by1
selected(i) = TRUE
else
selected(i) = FALSE
endif
endif
next i
return
function convertCoordX()
one = 1
two = 2
three = 3
four = 4
null = make vector3(one)
null = make vector3(two)
null = make vector3(three)
null = make vector3(four)
set vector3 one, 0, -1, 0
pick screen mousex(), mousey(), 1
px# = get pick vector x()
py# = get pick vector y()
pz# = get pick vector z()
set vector3 two, px#, py#, pz#
angle# = acos(dot product vector3(two, one))
cos_angle# = cos(angle#)
hypo_length# = camY# / cos_angle#
normalize vector3 two, two
scale vector3 two, two, hypo_length#
set vector3 one, camX#, camY#, camZ#
add vector3 four, one, two
convertion=x vector3(four)
endfunction convertion
function convertCoordZ()
one = 1
two = 2
three = 3
four = 4
null = make vector3(one)
null = make vector3(two)
null = make vector3(three)
null = make vector3(four)
set vector3 one, 0, -1, 0
pick screen mousex(), mousey(), 1
px# = get pick vector x()
py# = get pick vector y()
pz# = get pick vector z()
set vector3 two, px#, py#, pz#
angle# = acos(dot product vector3(two, one))
cos_angle# = cos(angle#)
hypo_length# = camY# / cos_angle#
normalize vector3 two, two
scale vector3 two, two, hypo_length#
set vector3 one, camX#, camY#, camZ#
add vector3 four, one, two
convertion=z vector3(four)
endfunction convertion
First of all, is there a way to make the unit(s) turn before moving in a direction instead of just instantly pointing in that direction?
Secondly, currently if you move multiple units at the same time, they stack on top of each other when they reach the target, thus making them hard to individually select. Can someone suggest a way to make them avoid each other. This may be hard to do because, as a solution to the fact that the units rarely reach their target down to the exact coordinate thus move back and forth in their target area, I made them stop within 1 coordinate of their target.
Thirdly, how do I make you can also select a unit simply by clicking on it? Right now, the only way is to drag a box over the unit(s) you want to select.
Finally, I'd appreciate any suggestions of a better way to write this code.
Thanks
Windows Vista Home Premium :: Intel Core 2 Duo - 2.2Ghz :: 2GB System RAM :: nVidia GeForce 8600M GS - 511MB VRAM