First off, here is my source code - this is just a test.
I got a array that handles all the units and stuff like that, and there is no comments and blah blah - what im trying to say is;
sorry for a messy source code ;p
sync on
sync rate 0
set window on
set window title "RTS v1"
set display mode 800 , 600 , 32 , 0
`Inits the AI
ai start
ai set radius 25 / 2
type Point
x as integer
y as integer
endtype
type UnitType
Movespeed as float
HP as integer
Range as float
MaxDamage as integer
MinDamage as integer
endtype
dim UnitTypes(2) as UnitType
UnitTypes(0).Movespeed = 3
UnitTypes(0).HP = 100
UnitTypes(0).MaxDamage = 10
UnitTypes(1).Movespeed = 50
UnitTypes(1).HP = 150
UnitTypes(0).MaxDamage = 20
type Unit
Owner as integer
ID as integer
UType as UnitType
CurHP as integer
t as integer
endtype
dim Units() as Unit
`Inits the terrain
TerrainID = FreeObject()
make object plane TerrainID , 1000 , 1000
xrotate object TerrainID , 90
`Creates 5 friendly units
for i = 0 to 5
add_unit( 1 , 1 )
next i
`Creates 5 enemy units
for i = 0 to 5
add_unit( 2 , 1 )
next i
`Inits the camera
autocam off
position camera 0 , 1000 , -25
point camera 0 , 0 , 0
dim selectedUnits() as Unit
dim unitsToDelete() as integer
selectingOngoing = 0
selectingStart as Point
curUnitByID as Unit
do
` Some simple debug data
text 0 , 00 , "# of units: " + str$(array count(Units(0)))
text 0 , 15 , "# of selected units: " + str$(array count(selectedUnits(0)))
text 0 , 45 , "Mouse X: " + str$(mousex())
text 0 , 60 , "Mouse Y: " + str$(mousey())
` Handles selecting units
if selectingOngoing = 0 and mouseclick()=1
selectingOngoing = 1
selectingStart.x = mousex()
selectingStart.y = mousey()
endif
if selectingOngoing = 1 and mouseclick()=1
x1 = 0
y1 = 0
x2 = 0
y2 = 0
if mousex() > selectingStart.x
x1 = selectingStart.x
x2 = mousex()
else
x1 = mousex()
x2 = selectingStart.x
endif
if mousey() > selectingStart.y
y1 = selectingStart.y
y2 = mousey()
else
y1 = mousey()
y2 = selectingStart.y
endif
line x1,y1,x2,y1
line x1,y2,x2,y2
line x1,y1,x1,y2
line x2,y2,x2,y1
endif
if selectingOngoing = 1 and mouseclick()=0
x1 = 0
x2 = 0
y1 = 0
y2 = 0
if mousex() > selectingStart.x
x1 = selectingStart.x
x2 = mousex()
else
x1 = mousex()
x2 = selectingStart.x
endif
if mousey() > selectingStart.y
y1 = selectingStart.y
y2 = mousey()
else
y1 = mousey()
y2 = selectingStart.y
endif
empty array selectedUnits()
selectingOngoing = 0
for i = 0 to array count(Units(0))
if Units(i).Owner = 1
if object in screen(Units(i).ID)
if object screen x(Units(i).ID) > x1 and object screen x(Units(i).ID) < x2
if object screen y(Units(i).ID) > y1 and object screen y(Units(i).ID) < y2
array insert at bottom selectedUnits(0)
selectedUnits() = Units(i)
endif
endif
endif
endif
next i
endif
` Handles movement of units
pickObject = pick object( mousex() , mousey() , 1 , 999999999 )
if pickObject = 1 and mouseclick() = 2
pick screen mousex() , mousey() , get pick distance()
x# = get pick vector x() + camera position x()
z# = get pick vector z() + camera position z()
for i = 0 to array count(selectedUnits(0))
ai entity go to position selectedUnits(i).ID , x# , z#
next i
endif
`Paints a list of the selected units, if any
if array count(selectedUnits(0)) > -1
for i = 0 to array count(selectedUnits(0))
text 100 , 100 + ( i * 15 ) , "HP: " + str$(selectedUnits(i).CurHP) + " / " + str$(selectedUnits(i).UType.HP)
text 0 , 300 + ( i * 15 ) , "Target ID: " + str$( ai get entity target id( selectedUnits(i).ID , 1 ) )
next i
endif
`Handles visualization of the attacking
for i = 0 to array count(Units(0))
if ai get entity can fire(Units(i).ID) and Units(i).t < timer()
entity_id = ai get entity target id( Units(i).ID , 1 )
if entity_id > 0
for q = 0 to array count(Units(0))
if Units(q).ID = entity_id
Units(q).CurHP = 0
endif
next q
endif
Units(i).t = timer() + 2000
endif
if Units(i).t > timer()
tx# = ai get entity target x(Units(i).ID)
tz# = ai get entity target z(Units(i).ID)
x# = object position x(Units(i).ID)
z# = object position z(Units(i).ID)
d3d_line3d x# , 12.5 , z# , tx# , 12.5 , tz#
endif
if Units(i).CurHP <= 0
array insert at bottom unitsToDelete(0)
unitsToDelete() = i
endif
next i
for i = 0 to array count(unitsToDelete(0))
`ai kill entity Units(unitsToDelete(i)).ID
`delete object Units(unitsToDelete(i)).ID
`array delete element Units(0), unitsToDelete(i)
hide object Units(unitsToDelete(i)).ID
next i
ai update
sync
loop
function get_unit_by_objectid(objectid)
for i = 0 to array count(Units(0))
if Units(i).ID = objectid
curUnitByID = Units(i)
exitfunction
endif
next i
endfunction
function FreeObject()
i = 1
while object exist(i)
inc i , 1
endwhile
endfunction i
function add_unit(player,utype)
local tUnit as Unit
tUnit.Owner = player
tUnit.ID = FreeObject()
tUnit.UType = UnitTypes(utype)
tUnit.CurHP = UnitTypes(utype).HP
make object sphere tUnit.ID , 25
position object tUnit.ID , rnd(500) , 12.5 , rnd(500)
if player = 1
ai add friendly tUnit.ID
color object tUnit.ID , rgb(000,255,000)
else
ai add enemy tUnit.ID
color object tUnit.ID , rgb(255,000,000)
endif
AI Set Entity View Arc tUnit.ID , 90 , 180
AI Set Entity Aggressive tUnit.ID
ai set entity speed tUnit.ID , tUnit.UType.Movespeed
ai set entity attack distance tUnit.ID , 100
array insert at bottom Units()
Units() = tUnit
endfunction
at line 204 to 209 i delete the array elements of the units which are dead, the problem is that this causes a "array out side of bounds" error.. And i can't seem to figure out which loop that causes this error, can anyone help?
( its 3'oclock at night here btw )
Keep it simple.
Questions? Mail me
