Ok, I broke down the code you gave above, which was very helpful, and attempted to put it into a basic RTS engine. The full code is below:
SYNC ON
SYNC RATE 60
RANDOMIZE TIMER()
AUTOCAM OFF
SET DISPLAY MODE 1024,768,32
#INCLUDE "a star library.dba"
GLOBAL G_formationX
GLOBAL G_formationZ
GLOBAL G_middleX#
GLOBAL G_middleZ#
GLOBAL squareSize# = 40.0
mapX = 64
mapZ = 64
DIM map(mapX,mapZ)
terrainX# = mapX * squareSize#
terrainZ# = mapZ * squareSize#
MatrixTilesAcross = mapX * squareSize#: MatrixTilesDown = mapZ * squareSize#
TileSizeX = terrainX#
TileSizeZ = terrainZ#
MatrixWidth = terrainX#
MatrixHeight = terrainZ#
Make Object Cube 100,10: Color Object 100,RGB(255,0,0)
PointerX = (MatrixTilesAcross/2)*TileSizeX: PointerZ = (MatrixTilesDown/2)*TileSizeZ
Position Object 100,PointerX,5,PointerZ
Position Camera MatrixWidth/2,200,MatrixHeight/2-200
Point Camera PointerX,0,PointerZ
rem set up functions for our pathfinding
CreateSearchMap(mapX, mapZ)
CreateSearchPathLists(0)
screenwidth2# = SCREEN WIDTH() / 2.0
screenheight2# = SCREEN HEIGHT() / 2.0
scalefactor# = SCREEN HEIGHT() / 1.2
type basicUnitActions
isMoving as boolean
isAttacking as boolean
isSelected as boolean
endtype
type basicUnitAnimations
idleStart as integer
idleEnd as integer
walkStart as integer
walkEnd as integer
action1Start as integer
action1End as integer
action2Start as integer
action2End as integer
endtype
type basicUnitProperties
name as string
maxLife as integer
armor as integer
range as integer
speed as float
animations as basicUnitAnimations
endtype
dim characterType(2) as basicUnitProperties
#constant SOLDIER = 1
#constant WORKER = 2
#constant TREE = 1
#constant ANIMATE_IDLE = 1
#constant ANIMATE_WALK = 2
#constant ANIMATE_ACTION1 = 3
#constant ANIMATE_ACTION2 = 4
#constant FORMATION_BLOCK = 1
characterType(SOLDIER).name = "Grunt"
characterType(SOLDIER).maxLife = 80
characterType(SOLDIER).armor = 4
characterType(SOLDIER).range = 0
characterType(SOLDIER).speed = 3.5
characterType(WORKER).name = "Peasant"
characterType(WORKER).maxLife = 45
characterType(WORKER).armor = 0
characterType(WORKER).range = 0
characterType(WORKER).speed = 2.25
characterType(WORKER).animations.idleStart = 326
characterType(WORKER).animations.idleEnd = 359
characterType(WORKER).animations.walkStart = 1
characterType(WORKER).animations.walkEnd = 13
characterType(WORKER).animations.action1Start = 111
characterType(WORKER).animations.action1End = 125
type character
object as integer
action as basicUnitActions
isHarvesting as integer
targetX as float
targetZ as float
tempX as float
tempZ as float
angle as float
group as integer
unit as integer
life as integer
resourceQuantity as integer
lastAction as integer
stage as integer
formation as integer
endtype
dim characters(0) as character
type resource
object as integer
quantityLeft as integer
resourceType as integer
endtype
dim resources(0) as resource
remstart
rem create 5 soldiers
for t = 10 to 14
make object cube t, 10
array insert at bottom characters(0)
index = array count(characters(0))
characters(index).object = t
characters(index).action.isMoving = 0
characters(index).action.isAttacking = 0
characters(index).action.isSelected = 0
characters(index).isHarvesting = 0
characters(index).group = -1
characters(index).unit = SOLDIER
characters(index).life = characterType(SOLDIER).maxLife
position object t, rnd(1000),0,rnd(100)
next t
remend
`load image "images\shadow.jpg", 2
rem create 4 workers
for t = 10 to 13
load object "models\dwarf1.x", t
make object plain t+4,60,60
`ghost object on t+4
`xrotate object t+4,-90
`texture object t+4,2
scale object t, 1000, 1000, 1000
set object ambient t, 0
set object speed t, 30
loop object t, 327, 360
array insert at bottom characters(0)
index = array count(characters(0))
characters(index).object = t
characters(index).action.isMoving = 0
characters(index).action.isAttacking = 0
characters(index).action.isSelected = 0
characters(index).isHarvesting = 0
characters(index).group = -1
characters(index).unit = WORKER
characters(index).life = characterType(WORKER).maxLife
characters(index).stage = ANIMATE_IDLE
characters(index).tempX = -1
characters(index).tempZ = -1
position object t, rnd(1000),0,rnd(100)
next t
rem camera x & y movement speed
cspd# as float = 6.0
rem camera height movement speed
cyspd# as float = 4.0
rem starting camera height position
cy# as float = 350.0
rem make our terrain
make matrix 1, terrainX#, terrainZ#, mapX/2, mapZ/2
load image "images\grass.bmp", 1
prepare matrix texture 1, 1, 1, 1
rem make 10 trees
for t = 20 to 40
load object "models\tree.x", t
`load object "cedartrunk.x", t
`load object "cedarbranches.x", t+100
`scale object t,50,50,50
`scale object t+100,50,50,50
`set object ambient t, 0
ang = rnd(360)
yrotate object t, ang
`yrotate object t+100,ang
`set object transparency t+100,1
`set object cull t+100,0
x = rnd(mapX-1)
z = rnd(mapZ-1)
SetSearchMap(x,z,1)
rem position the tree inside the center of grid square (x,z)
position object t, x*squareSize#+(squareSize#/2), 0, z*squareSize#+(squareSize#/2)
`position object t+100, x*squareSize#+(squareSize#/2), 0, z*squareSize#+(squareSize#/2)
rem update map array so it knows a tree is at (8,3)
map(x,z) = TREE
rem update resource array list
array insert at bottom resources(0)
index = array count(resources(0))
resources(index).object = t
rem give resource a quantity between 100 to 200
resources(index).quantityLeft = rnd(100)+100
resources(index).resourceType = TREE
next t
REM ============= load sounds ==============
load sound "sounds\Hit tree.wav", 1
make object sphere 1, 10
make object sphere 2, 10 : color object 2, rgb(255,0,0)
for t = 1000 to 1050
make object sphere t, 10
next t
REM ========== Main loop ===================
DO
rem camera controls
gosub _Control_Camera
rem define a variable to hold mouse click status so only 1 system call is made each loop
_mouse_click = mouseclick()
rem get scancode of key pressed
_code = scancode()
rem reset variable
_resource = 0
_selected_count = 0
_formation = 0
if spacekey()=1 then _formation = FORMATION_BLOCK : getMeetingCoordinates()
rem if user right-clicks
if _mouse_click = 2 and mouseFlag = 0
mouseFlag = 1
gosub _Click_Matrix_Position
_resource = pickResource()
rem a new target position has been selected
rem switch flag on to let selected characters know they need to update their target position
assignTargetPositionFlag = 1
endif
rem if user has is not right-clicking, switch off mouseFlag(flag prevents multiple clicks in a row)
if _mouse_click <> 2 then mouseFlag = 0
rem if user left-clicks for first time, switch selectionFlag
if _mouse_click = 1 and selectionFlag = 0
selectionFlag = 1
bx = mouseX()
by = mouseY()
endif
rem if user left-clicks and has started a selection box
if _mouse_click = 1 and selectionFlag = 1
bx2 = mouseX()
by2 = mouseY()
endif
rem if user has stopped drawing a selection box, switch off selectionFlag
if _mouse_click <> 1 then selectionFlag = 0
rem if user is currently drawing a selection box
if selectionFlag = 1
rem sort the coordinates
gosub _sort_selection_coordinates
rem draw the box
drawSelectionBox(selectionX1, selectionY1, selectionX2, selectionY2)
endif
rem group assignment shortcut key listener
gosub _check_for_group_assignment
rem group selection shortcut key listener
gosub _check_for_group_selection
rem loop through all characters
for char = 1 to array count(characters(0))
rem reset variable to default animation stage
stage = ANIMATE_IDLE
rem get object number of this character for future object related actions
obj = characters(char).object
rem if user is currently drawing a selection box
if selectionFlag = 1
rem make sure unit is visibly on the screen
if object in screen(obj) = 1
rem get the character's 2D screen coordinates
cx = object screen x(obj)
cy = object screen y(obj)
rem if character is within the selection box, update isSelected variable
if cx > selectionX1 and cx < selectionX2 and cy > selectionY1 and cy < selectionY2
characters(char).action.isSelected = 1
else
characters(char).action.isSelected = 0
endif
endif
else
rem if user is trying to get a previously assigned group
if getGroupFlag = 1
rem check to see if this character is part of the group we want
rem if not, deselect it incase it's currently selected
if characters(char).group = _group_number
characters(char).action.isSelected = 1
else
characters(char).action.isSelected = 0
endif
endif
endif
rem if this character is selected
if characters(char).action.isSelected = 1
rem keep count of how many units are currently selected
inc _selected_count, 1
if _formation > 0
rem assign formation to character
characters(char).formation = _formation
rem call the construction of the formation
getFormation(_selected_count, _formation)
rem throw flag so that character gets assigned its new target coordinates
assignTargetPositionFlag = 1
rem assign new target coordinates for this character
targetX# = G_middleX#
targetZ# = G_middleZ#
rem reset previous orders
characters(char).isHarvesting = 0
characters(char).action.isAttacking = 0
endif
_selected_unit = char
rem if player has selected a resource
if _resource > 0
rem if this selected character is a worker
if characters(char).unit = WORKER
rem assign this character the resource
characters(char).isHarvesting = _resource
endif
endif
if assignGroupFlag = 1
characters(char).group = _group_number
endif
rem displays the group number of the character, if one has been assigned
if characters(char).group > -1 then text object screen x(obj), object screen y(obj), str$(characters(char).group)
rem if the user has right-clicked during this game loop..
if assignTargetPositionFlag = 1
characters(char).tempX = -1
characters(char).tempZ = -1
rem tell character to move
characters(char).action.isMoving = 1
rem check to see if character belongs to a formation
offsetX# = 0
offsetZ# = 0
if characters(char).formation > 0
getFormation(_selected_count, characters(char).formation)
offsetX# = G_formationX*squareSize#
offsetZ# = G_formationZ*squareSize#
endif
rem assign the target coordinates to character
characters(char).targetX = targetX#+offsetX#
characters(char).targetZ = targetZ#+offsetZ#
rem if character was told to move someplace else, and a resource was not targeted, unassigned its resource target (if it had any)
if _resource = 0 then characters(char).isHarvesting = 0
endif
else
rem if character is not selected and user is assigning a group number,
rem then reset this character's unit value to -1(no group) if it belonged to the current group assignment
if assignGroupFlag = 1
if characters(char).group = _group_number then characters(char).group = -1
endif
endif
rem if character is moving and hasn't reached its destination yet
if characters(char).action.isMoving = 1
rem get the target of this character
tx# = characters(char).targetX
tz# = characters(char).targetZ
rem if character has no path or has reached next step of path, find a new path and calculate the next tile to move to
if characters(char).tempX = -1
rem find which tiles the target and current position are in
targetTileX = int(tx#/squareSize#)
targetTileZ = int(tz#/squareSize#)
currentX = int(object position x(obj)/squareSize#)
currentZ = int(object position z(obj)/squareSize#)
rem run the map search
pathFound = SearchMapAStar8(0,currentX, currentZ, targetTileX, targetTileZ)
if pathFound > 0
rem assign the temporary target (the next tile found in the path)
characters(char).tempX = GetSearchPathX(0,1)*squareSize# + squareSize#/2
characters(char).tempZ = GetSearchPathY(0,1)*squareSize# + squareSize#/2
rem rotate character to point towards target
characters(char).angle = atanfull((characters(char).tempX - object position x(obj)), (characters(char).tempZ - object position z(obj)))
yrotate object obj, characters(char).angle
thing = 1000
for r = 1 to searchPathSize(0)
position object thing+r,GetSearchPathX(0,r)*squareSize# + squareSize#/2,0,GetSearchPathY(0,r)*squareSize# + squareSize#/2
next r
else
characters(char).action.isMoving = 0
endif
endif
rem if no path was found, then character's move status has changed and we must check for that
if characters(char).action.isMoving = 1
stage = ANIMATE_WALK
rem get current angle of character
angle# = characters(char).angle
rem get speed of character
speed# = characterType(characters(char).unit).speed
rem determine new position of character
x# = newxvalue(object position x(obj), angle#, speed#)
z# = newzvalue(object position z(obj), angle#, speed#)
y# = get ground height(1,x#,z#)
rem position character at its new coordinates
position object obj, x#, y#, z#
`position object obj+4,x#,y#+0.5,z#
rem if character is within range of targeted location
if getSquaredDistance#(x#,z#,characters(char).tempX,characters(char).tempZ) < (speed# * speed#)
targetTileX = int(characters(char).targetX/squareSize#)
targetTileZ = int(characters(char).targetZ/squareSize#)
currentX = int(object position x(obj)/squareSize#)
currentZ = int(object position z(obj)/squareSize#)
rem is this tile the tile of the final targeted destination?
if (targetTileX = currentX) AND (targetTileZ = currentZ)
characters(char).action.isMoving = 0
else
characters(char).tempX = -1
characters(char).tempZ = -1
endif
endif
endif
endif
rem if character has been assigned a resource
resID = characters(char).isHarvesting
if resID > 0
if resources(resID).quantityLeft > 0
rem get object number of resource
res = resources(resID).object
rem if character is within range of resource (within 40 units)
if getSquaredDistance#(object position x(obj), object position z(obj),object position x(res), object position z(res)) < (squareSize#*squareSize#)
rem wood chopping animation
stage = ANIMATE_ACTION1
rem make sure character hasn't already gathered from this resource within the last 0.75 second
if characters(char).lastAction+750 < timer()
play sound 1
rem increase character's gathered resource quantity
characters(char).resourceQuantity = characters(char).resourceQuantity + 1
rem reset character's time of last action it did (harvested)
characters(char).lastAction = timer()
rem decrease quantity of resource
resources(resID).quantityLeft = resources(resID).quantityLeft - 1
rem if resource object has no more resources
if resources(resID).quantityLeft <= 0
characters(char).isHarvesting = 0
endif
endif
endif
endif
endif
rem set object's animation to match its current action
setAnimation(char,stage)
NEXT char
rem positions from new target have been assigned, switch flag off
assignTargetPositionFlag = 0
SET CURSOR 0,0
PRINT "FPS: ", SCREEN FPS()
REM if only 1 unit is selected, then display its properties
IF _selected_Count = 1
PRINT characterType(characters(_selected_unit).unit).name
PRINT "Health: ",characters(_selected_unit).life,"/",characterType(characters(_selected_unit).unit).maxLife
PRINT "Armor: ", characterType(characters(_selected_unit).unit).armor
PRINT "Gathered: ",characters(_selected_unit).resourceQuantity
ENDIF
MMx=MouseMoveX(): MMy=MouseMoveY(): Mc=MouseClick()
Inc PointerX,MMx: TileX = PointerX/TileSizeX: ActPointerX = TileX*TileSizeX
Dec PointerZ,MMy: TileZ = PointerZ/TileSizeZ: ActPointerZ = TileZ*TileSizeZ
PointerY = Get Ground Height(1,ActPointerX,ActPointerZ)
Position Object 100,ActPointerX,PointerY+5,ActPointerZ
Point Camera ActPointerX,PointerY+5,ActPointerZ
If Mc=1
Repeat
MMy=MouseMoveY(): Mc=MouseClick()
Dec PointerY,MMy
Position Object 100,ActPointerX,PointerY+5,ActPointerZ
Point Camera ActPointerX,PointerY+5,ActPointerZ
Set Matrix Height 1,TileX,TileZ,PointerY
Update Matrix 1
Sync
Until MouseClick()=0
Endif
SYNC
LOOP
REM controls camera movement
_Control_Camera:
IF UPKEY() THEN INC cz#,cspd#
IF DOWNKEY() THEN DEC cz#,cspd#
IF RIGHTKEY() THEN INC cx#,cspd#
IF LEFTKEY() THEN DEC cx#,cspd#
IF SCANCODE()=30 THEN INC cy#,cyspd#
IF SCANCODE()=44 THEN INC cy#,cyspd#
IF MOUSEX() > 1000 THEN INC cx#,cspd#
IF MOUSEX() < 1 THEN DEC cx#,cspd#
IF MOUSEZ() > 700 THEN INC cz#,cspd#
IF MOUSEX() > 1000 AND MOUSECLICK()=2 THEN INC cx#,cspd# : CHANGE MOUSE 3
IF MOUSEX() < 1 AND MOUSECLICK()=2 THEN DEC cx#,cspd# : CHANGE MOUSE 3
IF cy#>400 THEN cy#=400
IF cy#<200 THEN cy#=200
offsetx# = cx#
offsety# = 0
offsetz# = cz#+200
POSITION CAMERA cx#,cy#,cz#
POINT CAMERA offsetx#,offsety#,offsetz#
RETURN
rem sorts the coordinates of selection box so that (X1,y1)
rem is smaller than (X2,Y2)
_sort_selection_coordinates:
IF bx < bx2
selectionX1 = bx
selectionX2 = bx2
ELSE
selectionX1 = bx2
selectionX2 = bx
ENDIF
IF by < by2
selectionY1 = by
selectionY2 = by2
ELSE
selectionY1 = by2
selectionY2 = by
ENDIF
RETURN
rem translates 2D mouse coordinates into 3D world coordinates
_Click_Matrix_Position:
mouseposx#=MOUSEX() - screenwidth2#
mouseposy#=screenheight2# - mousey()
dist#=SQRT((camera position x()-offsetx#)^2+(camera position y()-offsety#)^2+(camera position z()-offsetz#)^2)
IF mouseposy#<>0
vectorang#=ATANFULL(scalefactor#,mouseposy#)
ELSE
vectorang#=90.0
endif
IF vectorang#+CAMERA ANGLE X()>90.965
ratio#=mouseposy#/(SIN((vectorang#+CAMERA ANGLE X() )-90.0))
cursorposy#=ratio#*SIN(180.0-vectorang#)
ELSE
cursorposy#=1000000.0
ENDIF
hyplength#=scalefactor#/sin(vectorang#)
cursorposx#=(((ratio#*sin(90.0-camera angle x() ))/hyplength#)+1.0)*mouseposx#
if mouseposx#<>0
angtotal#=wrapvalue(camera angle y()-atanfull(mouseposy#,mouseposx#))
else
if mouseposy#<0
angtotal#=wrapvalue(camera angle y()+90.0)
else
angtotal#=wrapvalue(camera angle y()-90.0)
endif
endif
cameraang#=wrapvalue(360.0-camera angle y() )
cosang#=cos(cameraang#)
sinang#=sin(cameraang#)
movex# = (dist#/scalefactor#)*((cosang#*cursorposx#)+((-1*sinang#)*cursorposy#))
movez# = (dist#/scalefactor#)*((sinang#*cursorposx#)+(cosang#*cursorposy#))
rem 3D position stored into these variables
targetX#=movex#+offsetx#
targetZ#=movez#+offsetz#
movey#=0
RETURN
rem listens for user to press the shortcut for assigning character group numbers
_check_for_group_assignment:
rem reset flag
assignGroupFlag = 0
rem if user pressed control key, then check scancode of other key they pressed
if controlkey() = 1
select _code
case 2 : _group_number = 1 : endcase
case 3 : _group_number = 2 : endcase
case 4 : _group_number = 3 : endcase
case 5 : _group_number = 4 : endcase
case 6 : _group_number = 5 : endcase
case 7 : _group_number = 6 : endcase
case 8 : _group_number = 7 : endcase
case 9 : _group_number = 8 : endcase
case 10 : _group_number = 9 : endcase
case default : _group_number = -1 : endcase
endselect
rem switch on flag so the character loop knows to assign a group
if _group_number > -1 then assignGroupFlag = 1
endif
RETURN
rem checks to see user has pressed a key referring to a group number
_check_for_group_selection:
rem reset flag
getGroupFlag = 0
rem make sure user isn't pressing control key and trying to actually assign groups rather than select
if controlkey() = 0
select _code
case 2 : _group_number = 1 : endcase
case 3 : _group_number = 2 : endcase
case 4 : _group_number = 3 : endcase
case 5 : _group_number = 4 : endcase
case 6 : _group_number = 5 : endcase
case 7 : _group_number = 6 : endcase
case 8 : _group_number = 7 : endcase
case 9 : _group_number = 8 : endcase
case 10 : _group_number = 9 : endcase
case default : _group_number = -1 : endcase
endselect
rem switch on flag so character loops knows to select that group
if _group_number > -1 then getGroupFlag = 1
endif
RETURN
rem draws a box with top left corner at (x1,y1)
rem and bottom right corner at (x2,y2)
function drawSelectionBox(x1 as integer, y1 as integer, x2 as integer, y2 as integer)
line x1, y1, x2, y1
line x1, y2, x2, y2
line x1, y1, x1, y2
line x2, y1, x2, y2
endfunction
rem Given the character's number in the formation
rem function stores the column and row into the global
rem variables G_formationX and G_formationZ
function blockFormation(n as integer)
dec n, 1
ring = sqrt(n)
idx = n - (ring*ring)
if idx >= ring
G_formationZ = ring
G_formationX = 2*ring - idx
else
G_formationX = ring
G_formationZ = idx
endif
endfunction
rem calls the proper function for character formation
function getFormation(unit as integer, formation as integer)
if formation = FORMATION_BLOCK then blockFormation(unit)
endfunction
rem finds the coordinates in the middle of all selected units.
function getMeetingCoordinates()
lowX#=999999
lowZ#=999999
highX#=0
highZ#=0
rem loop through all characters
for char = 1 to array count(characters(0))
if characters(char).action.isSelected = 1
obj = characters(char).object
if object position x(obj) < lowX# then lowX# = object position x(obj)
if object position z(obj) < lowZ# then lowZ# = object position z(obj)
if object position x(obj) > highX# then highX# = object position x(obj)
if object position z(obj) < highZ# then highZ# = object position z(obj)
endif
next char
rem assign coordinates to the global variables
G_middleX# = lowX# + (highX#-lowX#)/2.0
G_middleZ# = lowZ# + (highZ#-lowZ#)/2.0
endfunction
rem returns the squared distance between two points
function getSquaredDistance#(x1 as float, y1 as float, x2 as float, y2 as float)
d# = (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2)
endfunction d#
rem returns the index to the resource the mouse is on
function pickResource()
for t = 1 to array count(resources(0))
obj = resources(t).object
check = pick object(mousex(), mousey(),obj,obj)
if check > 0
exitfunction t
endif
next t
endfunction 0
rem set object's animation loop, if it has changed states
function setAnimation(char as integer, stage as integer)
rem if object's animation is already set for this stage, then exit
if characters(char).stage = stage then exitfunction
unit = characters(char).unit
if stage = ANIMATE_IDLE
loop object characters(char).object,characterType(unit).animations.idleStart, characterType(unit).animations.idleEnd
characters(char).stage = stage
endif
if stage = ANIMATE_WALK
loop object characters(char).object,characterType(unit).animations.walkStart, characterType(unit).animations.walkEnd
characters(char).stage = stage
endif
if stage = ANIMATE_ACTION1
loop object characters(char).object,characterType(unit).animations.action1Start, characterType(unit).animations.action1End
characters(char).stage = stage
endif
if stage = ANIMATE_ACTION2
loop object characters(char).object,characterType(unit).animations.action2Start, characterType(unit).animations.action2End
characters(char).stage = stage
endif
endfunction
But, whenever I compile the code above, it runs fine, until I try to raise or lower the terrain, which gives me the error:
Runtime Error 7017 - Matrix coordinates illegal at line 523
I'm fairly sure the error is to do with the size of the matrix at lines 23-26. How would I fix this?
I've got another question, however, and it's sort of related to matrices, but regarding the viewpoint of them.
What i've benn trying to do is make it so if the mouse wheel is scrolled up, then the view height, or angle increases, making the view more top-down, and if the mouse wheel is scrolled down, the angle is decreased, bringing the camera closer to the ground, or even level to the ground.
Thanks for all your help so far.