no prob about Your messing... i think my code messes with your code- damn boxes aren't moving correctly
and i don't understand vectors and 3dmaths enough to fix it myself...
this is fuzzy's last code suggestion which is short, but unfortunately doesn't work:
Remstart
Today, February 01, 2011, 8:31:10 PM
Project: Terrain
*Load map
*Select objects
*Move camera
*Move objects to point on map
*Follow enemies and friendly differently
*Smooth rotation and speed until reach target
*Added 2 sprites and text over them
*different team object can be attacked
Remend
`--- Constants ---
#constant UnitCount 200 `maximum number of each type of objects
#constant VehiclesCount 100
#constant BuildingsCount 100
#constant ProjectilesCount 100
#constant OtherObjectsCount 100
#constant DestroyedUnitsCount 300
#constant FollowDistance 500.0 `this is squared (^2)
`because square root in get_distance function would slow down program a little
`--- Types ---
type Units `u can add more stats in here, atm for program work need only "target" variable
Stance as integer `0-passive, 2-defensive, 3-agresive
Name as string
Exist as integer `0-not egzist, 1-egzist
Team as integer
MaxHp as integer
Hp as integer
Dmg as integer
Def as integer
MaxSpeed as float
Speed as float
Traction as float `acelleration
RotationSpeed as float `how fast object rotates
ProjectileType as integer
LastShotTime as integer
ReloadTime as integer
Range as float `attack range- distance in which attacked object will be followed
MoveTarget as integer `Object to be followed
AttackTarget as integer `Object to be attacked
IsMoving as integer `if is moving
IsFollowing as integer `if is following
Dx as float `destination coordinates
Dy as float
Dz as float
endtype
type DestroyedUnits `u can add more stats in here, atm for program work need only "target" variable
exist as integer
Name as string
MaxHp as integer
Dmg as integer
MaxSpeed as float
Traction as float `acelleration
RotationSpeed as float `how fast object rotates
ProjectileType as integer
ReloadTime as integer
Range as float `attack range- distance in which attacked object will be followed
endtype
type Projectiles `projectiles for units to shoot with
Object as integer
type as integer `0-not exists, 1-bullet, 2-explosive, 3-rocket
dmg as integer
Speed as float
ParentObject as integer
AttackTarget as integer `Object to be followed if projectile is rocket
startX as float
startY as float
startZ as float
Range as float
endtype
`--- Variables ---
global activeID as integer `selected object number
global TotalUnitCount as integer `total number of Your controllable units
TotalUnitCount= UnitCount + VehiclesCount + BuildingsCount
global MovableUnitCount as integer `total number of Your movable units
MovableUnitCount= UnitCount + VehiclesCount
global failed as integer
failed=0
`--- Arrays ---
`array of all your controlable objects (units, soldiers, robots or whatever u use in game
dim Unit(TotalUnitCount+10) as units
dim Projectile(ProjectilesCount) as Projectiles
dim DestroyedUnit(destroyedUnitsCount) as DestroyedUnits
`------------------------------------------------------------------------------------------
`--- Program start ---
`------------------------------------------------------------------------------------------
Setup() `program setup
TerrainSetup() `make and set up terrain
LoadMenuPanel() `load sprites for in game menu
MakeBoxes() `make random objects just for testing, remove later
MakeSelectionIndicator() `make objects to be shown above selected objects
`and at their destination coordinates
`------------------------------------------------------------------------------------------
`--- Main Loop ---
`------------------------------------------------------------------------------------------
do
`camera movement
MoveCamera()
`left mouse click: select object
if mouseclick()= 1 then activeID= SelectObjects()
`place indicators over selected object and its target position
`(can load your own object like sword, target sign and etc.)
SelectionIndicator(activeID)
`right mouse click: set active object's target as a point on map or other object
if mouseclick()= 2 then MakeTarget()
`When all actions are made its time to move all the objects (if they should move ofc)
MoveObjects()
CheckNewTargets()
Attack()
MoveProjectiles()
`paste sprites and later print text, so text don't disappear behind sprites
`thx to Phaelax
`more about text over sprites here:
`http://forum.thegamecreators.com/?m=forum_view&t=171622&b=1
RefreshSprites()
`when u encounter problems with new functions added, print variables to see
`if your functions give right answers, very usefull for debugging
Info()
`Selected object/unit info
ObjectInfo()
update terrain `apply all the changes to terrain
sync `apply all the changes to screen
loop
`------------------------------------------------------------------------------------------
`--- Functions and procedures ---
`------------------------------------------------------------------------------------------
function Setup()`program setup
sync on `automatic screen refresh on
sync rate 500 `refresh rate (i think it is times per second)
`i set such a high rate to see what highest actual frame rate my pc can reach
`i dont know what rate is recomemnded, but mostly used in tutorials it is 60 or 90
`when new objects are created camera isnt positioned at coordinates of new object
autocam off
`width and height in pixels and how much colors will contain
`(not sure but should be 16 or 32 bits)
set display mode 1024,768,32
set camera range 10,10000 `how far objects will be visible from 10 units to 10000 units
Fog on `u know what fog is :D
Fog distance 15000 `this is distance in which u can't see anything through fog
`it makes less colorful objects in smaller distance too
`if u remove fog, skybox will be very bright
Fog color RGB(128,128,128) `fog color- grey
maximize window `make full screen
randomize timer() `set starting value for random() function
`every time u run program random numbers will be different
xrotate camera 20 `point camera little down
`hide mouse `U can hide mouse cursor if want. I like to see it :P
`later U can put some image instead of cursor i.e. target
`re-calculates the normals of each polygon in an object
`for the purpose of working out the rendering of each of the polygons
set normalization on
endfunction
`^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
function TerrainSetup() `make and set up terrain
set dir "media\DefaultMap\" ` set the directory to map's location (2 levels higher)
` load base and detail texture
load image "texture.bmp", 1
load image "detail.tga", 2
make object terrain 1 ` create the terrain object
set terrain heightmap 1, "map.bmp" ` set the heightmap
set terrain scale 1, 3, 0.1, 3 ` set the scale
set terrain split 1, 16 ` split value by 16 * 16
set terrain tiling 1, 4 ` detail map tiling
set terrain light 1, 1, -0.25, 0, 1, 1, 0.78, 0.5 ` light - xdir, ydir, zdir, red, green, blue, intensity
set terrain texture 1, 1, 2 ` base and detail texture
build terrain 1 ` finally build the terrain
position object 1, 0,0,0 ` position terrain at zero coordinates
sc_setupComplexObject 1,1,2 ` setup collision on the terrain
set dir ".." ` reset the directory (1 level lower to media\)
set dir "Skybox\" ` set the directory to skybox's location
`load our skybox (this is very low quality, u can find smth better)
`this file has images asociated (1-6 jpg images in media directory).
`U can change or edit them
load object "skybox.x", 6
set object light 6, 0
set object texture 6, 3, 1 `not sure what it should do, but it improves skybox texture :)
position object 6, 500, 50, 500 `~in middle of terrain
scale object 6, 10000, 10000, 10000 `make skybox big!
set dir ".." `reset the directory back to media\
endfunction
`^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
function MoveCamera()
`keyboard input for movement
if upkey()=1 then move camera 3
if downkey()=1 then move camera -3
if leftkey()=1 then yrotate camera wrapvalue(camera angle y()-2)
if rightkey()=1 then yrotate camera wrapvalue(camera angle y()+2)
`find out the camera height
x#=camera position x()
z#=camera position z()
y#=get terrain ground height(1,x#,z#)+30
`position the camera
position camera x#,y#,z#
endfunction
`^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
function SelectObjects()
`function to pick any object's whose number is between 10 and TotalUnitCount+10
`using screen coordinates
which= pick object(mousex(),mousey(),10,TotalUnitCount+10)
endfunction which `return value which object was selected, if 0 then none of objects
`^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
function MakeBoxes() `make 20 random boxes
make object box 8,10,7,15 `make object
make mesh from object 1, 8 `extract shape of object
make object box 9,5,4,7 `make object
make mesh from object 2, 9 `extract shape of object
delete object 8
delete object 9
a=10+UnitCount
b=a+10
for i= a to b `10 times
make object box i,10,3,15 `make box
add limb i, 1, 1 `add a part for object, parts shape is mesh 1
offset limb i, 1, 0, 0, 1 `position part of object in local coordinates
hide limb i, 1 `hide- make that part invisible
add limb i, 2, 2
offset limb i, 2, 0, 2, -1
x=rnd(200)+50 `get random x and z coordinates
z=rnd(200)+50
y=get terrain ground height (1,x,z) `get height in that point
position object i,x,y+4,z `position object +4 units above ground
rotate object i,0,rnd(100),0 `random rotate object object
color object i,rgb(0,255,0) `color object- green
Unit(i).Stance=1
Unit(i).Exist=1
Unit(i).LastShotTime=0
Unit(i).ReloadTime=200 `miliseconds
Unit(i).ProjectileType=1
Unit(i).Name="Machinegunner Box"
Unit(i).Hp=100
Unit(i).Dmg=6
Unit(i).Def=3
Unit(i).RotationSpeed=1.5
unit(i).maxspeed=0.6
Unit(i).Speed=0.0
Unit(i).IsMoving=0.0
Unit(i).IsFollowing=0.0
Unit(i).Traction=0.015
Unit(i).Team=1
Unit(i).Range=16000 `squared(^2)
next i
a=b+1
b=a+10
for i= a to b `make 10 more boxes in other team
make object box i,10,4,15
add limb i, 1, 1
offset limb i, 1, 0, 0, 1
hide limb i, 1
add limb i, 2, 2
offset limb i, 2, 0, 2, -1
x=rnd(200)+200
z=rnd(200)+200
y=get terrain ground height (1,x,z)
position object i,x,y+4,z
rotate object i,0,rnd(100),0
color object i,rgb(128,128,0) `color boxes "yellow-green" lol
Unit(i).Stance=2
Unit(i).Exist=1
Unit(i).LastShotTime=0
Unit(i).ReloadTime=2000 `miliseconds
Unit(i).ProjectileType=1
Unit(i).Name="Cannon Box"
Unit(i).Hp=120
Unit(i).Dmg=20
Unit(i).Def=5
Unit(i).RotationSpeed=1.0
unit(i).maxspeed=0.4
Unit(i).Speed=0.0
Unit(i).IsMoving=0.0
Unit(i).IsFollowing=0.0
Unit(i).Traction=0.01
Unit(i).Team=2
Unit(i).Range=16000 `this is squared (^2)
next i
a=10+MovableUnitCount
b=a+3
for i= a to b `make 3 more boxes as buildings
make object box i,15,10,20
x=rnd(200)+50
z=rnd(200)+50
y=get terrain ground height (1,x,z)
position object i,x,y+4,z
rotate object i,0,rnd(100),0
color object i,rgb(128,128,128) `color boxes "grey"
Unit(i).Exist=1
Unit(i).Name="Base"
Unit(i).Hp=500
Unit(i).Def=5
Unit(i).Team=1
next i
endfunction
`^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
function SelectionIndicator(obj) `indicator handler
if obj=0: `if no objects selected
if object visible(2)=1: `if indicator visible
hide object (2) `make it invisible
endif
else `if some object is selected
if object visible(2)=0: `if indicator is not visible
show object (2) `make it visible
endif
`position indicator +10 units above your selected object
if Object exist (obj) then position object 2,object position x (obj),object position y (obj)+10,object position z(obj)
endif
endfunction
`^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
function MakeSelectionIndicator() `make objects for indicators
make object cone 2, 5 `for active object
zrotate object 2, 180 `rotate/point down
set object 2, 1, 1, 0 `don't know what this does
color object 2,RGB(0, 255, 0) `green color
make object cone 3, 5 `to mark target-coordinates on map
zrotate object 3, 180 `rotate/point down
set object 3, 1, 1, 0
color object 3,RGB(255, 255, 0) `yellow color
endfunction
`^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
function Info()
`str$(a) function returns integer, float or other type variable as string
`"text x,y, string" function can't print other variables than strings
`strings can be added to each other i.e. "cat"+"dog"+str$(2)="catdog2"
text 10,20,"FPS :" + str$(Screen FPS()) `frames per second (how many times screen refreshes)
`determines how hard program is for pc, fps will go down if pc is too weak
`my 1.8Ghz, 1.5Gb Ram machine shows 80-90 in this program stage.
`as more functions are added it should slow down
text 10,40,"Left mouse click to select object"
text 10,55,"Right mouse click to place target/target object"
text 10,90,"Object selected: "+str$(activeID) `selected, active object
text 10,200,"Failed: "+str$(Failed)
endfunction
`^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
function object_distance_squared(a,b) `returns distance between objects squared
`(distance^2 or distance*distance)
w#= (object position x(a)-object position x(b))^2 `difference in x axis ^2
h#= (object position y(a)-object position y(b))^2 `difference in y axis ^2
l#= (object position z(a)-object position z(b))^2 `difference in z axis ^2
d#= l#+w#+h# `summ up all distances (like pitagores just in 3D)
`if u want to get real distance use d#= sqrt(l#+w#+h#)
`but square root is calculated long, so if u make many calculations
`program can slow down signicicantly
endfunction d#
`^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
function Projectile_distance(a) `similar to object_distance_squared
`remstart
w#= (object position x(projectile(a).Object)-projectile(a).StartX)^2 `difference in x axis ^2
h#= (object position y(projectile(a).Object)-projectile(a).StartY)^2 `difference in y axis ^2
l#= (object position z(projectile(a).Object)-projectile(a).StartZ)^2 `difference in z axis ^2
d#= l#+w#+h#
OutOfRange=0
if projectile(a).range<d# then OutOfRange=1
`remend
endfunction OutOfRange
`^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
function LoadMenuPanel() `this will load sprites for menu
set dir "Sprites\" `directory for sprites
load image "MiniMap.bmp",3 `minimap in left corner
sprite 1,0,600,3
hide sprite 1
load image "Panel.bmp",4 `will be info about selected unit/object
sprite 2,250,600,4
hide sprite 2
set dir ".." `reset directory to media\
endfunction
`^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
function MakeTarget() `place target indicator if needed (i cant comment this funtion well, because its no my made.
`forum link for it: http://forum.thegamecreators.com/?m=forum_view&t=79830&b=1
obj=SelectObjects() `targeted object number
if obj=0 `if none objects selected
mX = mouseX()
mY = mouseY()
cx# = camera position x()
cy# = camera position y()
cz# = camera position z()
pickState = Pick Object (mX, mY, 1, 1)
If pickState <> 0 `if nothing selected
Dist# = Get Pick Distance()
Pick Screen mX, mY, Dist#
indX# = cx# + get pick vector x()
indY# = cy# + get pick vector y()
indZ# = cz# + get pick vector z()
Position Object 3, indX#, indY#+3, indZ# `position indicator on terrain
Unit(ActiveID).Dx=indX# `set units destination coordinates
Unit(ActiveID).Dy=indY#
Unit(ActiveID).Dz=indZ#
Unit(ActiveID).IsFollowing=0 `no more folowing or attacking
unit(i).movetarget=0
unit(i).attacktarget=0
Unit(ActiveID).IsMoving=1 `start moving to destination coordinates
if unit(ActiveID).MoveTarget<>0 then unit(ActiveID).MoveTarget=0
if object visible(3)=0 then show object (3) `show destination point indicator
EndIf
else
unit(ActiveID).MoveTarget=obj `set units target value to selected object
`if target object is enemy, set attack target
if unit(ActiveID).MoveTarget>0 and unit(obj).Team<>unit(ActiveID).Team
unit(ActiveID).AttackTarget=obj
else
unit(ActiveID).AttackTarget=0
endif
Unit(ActiveID).IsMoving=0 `no more moving to destination coordinates
Unit(ActiveID).IsFollowing=1 `start folowing
if object visible(3)=1 then hide object (3) `hide destination point indicator
endif
endfunction
`^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
function ObjectInfo() `write selected object information
if activeID<>0
if activeID>=10+UnitCount and activeID<10+MovableUnitCount
text 610, 680, "Name: "+ Unit(ActiveID).Name
text 610, 695, "Team: "+ str$(Unit(ActiveID).Team)
text 610, 710, "Hp: "+ str$(Unit(ActiveID).Hp)
text 610, 725, "Dmg: "+ str$(Unit(ActiveID).Dmg)
endif
if activeID>=10+MovableUnitCount and activeID<10+TotalUnitCount
text 610, 680, "Name: "+ Unit(ActiveID).Name
text 610, 695, "Team: "+ str$(Unit(ActiveID).Team)
text 610, 710, "Hp: "+ str$(Unit(ActiveID).Hp)
endif
endif
endfunction
`^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
function RefreshSprites()
paste sprite 1,0,600
paste sprite 2,250,600
endfunction
`^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
function Attack()
for i=10 to MovableUnitCount+10 `for all movable objects
if unit(i).AttackTarget<>0
if object_distance_squared(i,unit(i).AttackTarget)<unit(i).range
Time=timer()
if Unit(i).LastShotTime+Unit(i).ReloadTime< Time
MakeProjectile(i)
Unit(i).LastShotTime=Time
endif
endif
endif
next i
endfunction
`^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
function MakeProjectile(ID)
found=0
i=0
while found=0 and i<ProjectilesCount-1
inc i, 1
if projectile(i).type=0
found=i
endif
endwhile
projectile(i).type=1
n=i+TotalUnitCount+10
if object exist (n) then delete object n
Make object sphere n, 10
color object n,rgb(128,128,128)
projectile(i).Object=n
scale object n, 10,10,50
x=object position x(ID)
y=object position y(ID)
z=object position z(ID)
projectile(i).Startx=x
projectile(i).Starty=y
projectile(i).Startz=z
Position Object n,x,y,z
Point Object n, object position x(Unit(ID).attacktarget),object position y(Unit(ID).attacktarget),object position z(Unit(ID).attacktarget)
projectile(i).Range=Unit(ID).Range
projectile(i).dmg=Unit(ID).dmg
projectile(i).speed=5.0
projectile(i).ParentObject=ID `object who shot projectile- not to check collision with it
endfunction
`^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
function MoveProjectiles()
col=0
for i= 0 to ProjectilesCount
if projectile(i).type>0
if Projectile(i).type=1
Move Object projectile(i).object, projectile(i).speed
col=OBJECT COLLISION(projectile(i).object, 0)
if col>=10 and col<=TotalUnitCount and col<>projectile(i).ParentObject
a=projectile(i).dmg-unit(col).Def
if a>0 then dec unit(col).Hp,a
DestroyProjectile(i)
ProjectileDestroyed=1
if unit(col).Hp<1 then DestroyObject(col)
else
if Projectile_distance(i)=1 then DestroyProjectile(i)
endif
endif
endif
next i
endfunction
`^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Function DestroyObject(ID)
for i=10 to TotalUnitCount+10
if Unit(i).Movetarget=ID
Unit(i).IsFollowing=0
Unit(i).Movetarget=0
endif
if Unit(i).Attacktarget=ID then Unit(i).Attacktarget=0
next i
found=0
i=0
while found=0 and i<DestroyedUnitsCount-10
inc i, 1
if DestroyedUnit(i).exist=0
found=i
endif
endwhile
if limb exist (ID, 1) then remove limb ID, 1
make mesh from object 1, ID
m=10+TotalUnitCount+OtherObjectsCount+i
make object m,1,0
color object m, RGB (10,10,0)
position object m, Object position x (ID), Object position y (ID), Object position z (ID)
rotate object m, Object angle x (ID), Object angle y (ID),Object angle z (ID)
DestroyedUnit(i).exist =1
DestroyedUnit(i).Name = Unit(ID).Name
DestroyedUnit(i).MaxHp =Unit(ID).Maxhp
DestroyedUnit(i).Dmg =Unit(ID).dmg
DestroyedUnit(i).MaxSpeed =Unit(ID).MaxSpeed
DestroyedUnit(i).Traction =Unit(ID).traction
DestroyedUnit(i).RotationSpeed =Unit(ID).RotationSpeed
DestroyedUnit(i).ProjectileType = Unit(ID).ProjectileType
DestroyedUnit(i).ReloadTime =Unit(ID).ReloadTime
DestroyedUnit(i).Range =Unit(ID).Range
Unit(ID).Name=""
Unit(ID).Exist=0
Unit(ID).Team=0
Unit(ID).Maxhp=0
Unit(ID).hp=0
Unit(ID).dmg=0
Unit(ID).MaxSpeed=0
Unit(ID).Speed=0
Unit(ID).traction=0
Unit(ID).RotationSpeed=0
Unit(ID).Range=0
Unit(ID).ProjectileType=0
Unit(ID).LastShotTime=0
Unit(ID).ReloadTime=0
Unit(ID).MoveTarget=0
Unit(ID).AttackTarget=0
Unit(ID).IsMoving=0
Unit(ID).IsFollowing=0
Unit(ID).Dx=0
Unit(ID).Dy=0
Unit(ID).Dz=0
Delete Object ID
endfunction
`^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Function DestroyProjectile(ID)
delete object (projectile(ID).object)
projectile(ID).Object=0
projectile(ID).type=0
projectile(ID).dmg=0
projectile(ID).Speed=0
projectile(ID).ParentObject=0
projectile(ID).AttackTarget=0
endfunction
`^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Function CheckNewTargets()
found=0
b=10+TotalUnitCount
for i=10 to b
if unit(i).exist=1
if unit(i).attacktarget=0 and unit(i).stance>0
for n=10 to b
while n<b and found=0
inc n
if n<>i and unit(n).exist=1 and unit(n).team<>unit(i).team
if object_distance_squared(i,n)<unit(i).range
found=1
unit(i).attacktarget=n
if unit(i).stance=2 then unit(i).movetarget=n
endif
endif
endwhile
next n
endif
endif
next i
endfunction
`^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
function MoveObjects()
for i=10 to MovableUnitCount+9 `for all controllable objects
if unit(i).exist=1
if unit(i).IsMoving <>0 `if object is moving to destination point
MoveObject(i) `then move it
if abs(unit(i).dx-object position x(i))<5 and abs(unit(i).dz-object position z(i))<5 `if at destination. stop object
unit(i).Ismoving=0 `if reached destination point then stop
endif
endif
if unit(i).IsFollowing <>0 `if object is folowing other object
if unit(unit(i).MoveTarget).Team=unit(i).Team
if object_distance_squared (i,Unit(i).MoveTarget)>FollowDistance `if is too far
FollowObject(i) `then move it towards target
endif
else
if object_distance_squared (i,Unit(i).MoveTarget)>Unit(i).Range-10 then FollowObject(i)
endif
endif
endif
next i
endfunction
`^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
function MoveObject(ID) `made by Neuro Fuzzy, this is little edited to suit this program
`link to His tutorial: http://www.neurofuzzydev.com/V/D/43a.htm
`link to download full vectors tutorial:
`http://forum.thegamecreators.com/?m=forum_view&t=133126&b=7
Angle# = GetShortestAngle(object position x(ID), object position z(ID), object angle y(ID), Unit(ID).dx, Unit(ID).dy)
Rem PJY - if the selected cube is not pointing at the targeted cube, turn it towards the targeted cube
if abs(angle#) > 0.5
Rem PJY - select whether to turn right or left
if Angle# <= -0.5 then turn object right ID, Unit(ID).RotationSpeed
if Angle# >= 0.5 then turn object left ID, Unit(ID).RotationSpeed
endif
Rem PJY - now depending on the angle and the distance between the cubes
Rem PJY - we move the selected cube. The reason for the following code is that
Rem PJY - without it in certain situations the seleceted cube could begin
Rem PJY - "orbiting" the targeted cube, i.e. where it is moving so much per turn
Rem PJY - that it is not decreasing the angle
`all of the following code meaning is to slow down object
`when it is near or in big angle with it destination point
`the best way to set angles, distances and slow down/acelerate values
`is to experiment.
`all these values varie with different box sizes, speeds, stop distances
`if object is pointing ~ at its target then acelerate till max speed
if angle# < 1
if unit(ID).speed<unit(ID).maxspeed then unit(ID).speed=unit(ID).speed+unit(ID).traction
endif
`if object is pointing sharp angle at its target then acelerate till max speed/1.5
if angle# => 1 AND angle# < 10
if unit(ID).speed<unit(ID).maxspeed/1.5
unit(ID).speed=unit(ID).speed+unit(ID).traction
else
dec unit(ID).speed, 0.01 `if too fast, decrease speed
endif
endif
`if object is pointing bigger angle at its target then acelerate till max speed/2
if angle# => 10 AND angle# < 90 AND distance# > 20
if unit(ID).speed<unit(ID).maxspeed/2
unit(ID).speed=unit(ID).speed+unit(ID).traction
else
dec unit(ID).speed, 0.01 `if too fast, decrease speed
endif
endif
`if object is pointing away from its target then acelerate till max speed/8
if angle# => 90 AND angle# < 120 AND distance# > 20
if unit(ID).speed<unit(ID).maxspeed/8
unit(ID).speed=unit(ID).speed+unit(ID).traction
else
dec unit(ID).speed, 0.01 `if too fast, decrease speed
endif
endif
`if object is pointing away from its target and distance is small
`then acelerate till max speed/10
if angle# => 120 AND distance# > 10
if unit(ID).speed<unit(ID).maxspeed/10
unit(ID).speed=unit(ID).speed+unit(ID).traction
else
dec unit(ID).speed, 0.01 `if too fast, slow down
endif
endif
`i added this by experimenting with boxes, to slow down even more
if angle# => 80 AND distance# < 20
if unit(ID).speed<unit(ID).maxspeed/20
unit(ID).speed=unit(ID).speed+unit(ID).traction
else
dec unit(ID).speed, 0.02 `if too fast, slow down
endif
endif
move object ID, unit(ID).speed `when we set units speed, now can finally move it, YeY!
`get coordinates of object
Ox#=object position x(ID)
Oz#=object position z(ID)
Oy#=get terrain ground height (1,Ox#,Oz#)+4
`and place it on terrain
position object ID,Ox#,Oy#,Oz#
endfunction
`^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
function FollowObject(ID) `this is a simple function,
`which changes object destination coordinates into followed object coordinates,
Unit(ID).dx=object Position X(Unit(ID).Movetarget)
Unit(ID).dz=object Position Z(Unit(ID).Movetarget)
`and then calls MoveObject() function to move object
MoveObject(ID)
endfunction
`^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
`made by Neuro Fuzzy
function GetShortestAngle(x as float, y as float, angle as float, x2 as float, y2 as float)
ang# = wrapvalue(angle-atanfull(x2-x,y2-y))
if ang#>180 then ang#=ang#-360
endfunction ang#
`^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
I use unit(ID).dx and unit(ID).dz to store move target coordinates for any movable object, first object's coordinates and angle is taken directly with "object angle..." and "object position..." built in functions. have no clue what could be giving wrong answer from GetShortestAngle function
this is sven's code that doesn't work too:
Remstart
Today, February 01, 2011, 8:31:10 PM
Project: Terrain
*Load map
*Select objects
*Move camera
*Move objects to point on map
*Follow enemies and friendly differently
*Smooth rotation and speed until reach target
*Added 2 sprites and text over them
*different team object can be attacked
Remend
`--- Constants ---
#constant UnitCount 200 `maximum number of each type of objects
#constant VehiclesCount 100
#constant ProjectilesCount 100
#constant OtherObjectsCount 100
#constant DestroyedUnitsCount 300
#constant FollowDistance 500.0 `this is squared (^2)
`because square root in get_distance function would slow down program a little
`--- Types ---
type Units `u can add more stats in here, atm for program work need only "target" variable
Stance as integer `0-passive, 2-defensive, 3-agresive
Name as string
Exist as integer `0-not egzist, 1-egzist
Team as integer
MaxHp as integer
Hp as integer
Dmg as integer
MaxSpeed as float
Speed as float
Traction as float `acelleration
RotationSpeed as float `how fast object rotates
ProjectileType as integer
LastShotTime as integer
ReloadTime as integer
Range as float `attack range- distance in which attacked object will be followed
MoveTarget as integer `Object to be followed
AttackTarget as integer `Object to be attacked
IsMoving as integer `if is moving
IsFollowing as integer `if is following
Dx as float `destination coordinates
Dy as float
Dz as float
endtype
type DestroyedUnits `u can add more stats in here, atm for program work need only "target" variable
exist as integer
Name as string
MaxHp as integer
Dmg as integer
MaxSpeed as float
Traction as float `acelleration
RotationSpeed as float `how fast object rotates
ProjectileType as integer
ReloadTime as integer
Range as float `attack range- distance in which attacked object will be followed
endtype
type Projectiles `projectiles for units to shoot with
Object as integer
type as integer `0-not exists, 1-bullet, 2-explosive, 3-rocket
dmg as integer
Speed as float
ParentObject as integer
AttackTarget as integer `Object to be followed if projectile is rocket
startX as float
startY as float
startZ as float
Range as float
endtype
`--- Variables ---
global activeID as integer `selected object number
global TotalUnitCount as integer `total number of Your controllable units
TotalUnitCount= UnitCount + VehiclesCount
r = make vector2(1)
r = make vector2(2)
`--- Arrays ---
`array of all your controlable objects (units, soldiers, robots or whatever u use in game
dim Unit(TotalUnitCount+10) as units
dim Projectile(ProjectilesCount) as Projectiles
dim DestroyedUnit(destroyedUnitsCount) as DestroyedUnits
`------------------------------------------------------------------------------------------
`--- Program start ---
`------------------------------------------------------------------------------------------
Setup() `program setup
TerrainSetup() `make and set up terrain
LoadMenuPanel() `load sprites for in game menu
MakeBoxes() `make random objects just for testing, remove later
MakeSelectionIndicator() `make objects to be shown above selected objects
`and at their destination coordinates
`------------------------------------------------------------------------------------------
`--- Main Loop ---
`------------------------------------------------------------------------------------------
do
`camera movement
MoveCamera()
`left mouse click: select object
if mouseclick()= 1 then activeID= SelectObjects()
`place indicators over selected object and its target position
`(can load your own object like sword, target sign and etc.)
SelectionIndicator(activeID)
`right mouse click: set active object's target as a point on map or other object
if mouseclick()= 2 then MakeTarget()
`When all actions are made its time to move all the objects (if they should move ofc)
MoveObjects()
CheckNewTargets()
`RotateTurrets()
Attack()
MoveProjectiles()
`paste sprites and later print text, so text don't disappear behind sprites
`thx to Phaelax
`more about text over sprites here:
`http://forum.thegamecreators.com/?m=forum_view&t=171622&b=1
RefreshSprites()
`when u encounter problems with new functions added, print variables to see
`if your functions give right answers, very usefull for debugging
Info()
`Selected object/unit info
ObjectInfo()
update terrain `apply all the changes to terrain
sync `apply all the changes to screen
loop
`------------------------------------------------------------------------------------------
`--- Functions and procedures ---
`------------------------------------------------------------------------------------------
function Setup()`program setup
sync on `automatic screen refresh on
sync rate 500 `refresh rate (i think it is times per second)
`i set such a high rate to see what highest actual frame rate my pc can reach
`i dont know what rate is recomemnded, but mostly used in tutorials it is 60 or 90
`when new objects are created camera isnt positioned at coordinates of new object
autocam off
`width and height in pixels and how much colors will contain
`(not sure but should be 16 or 32 bits)
set display mode 1024,768,32
set camera range 10,10000 `how far objects will be visible from 10 units to 10000 units
Fog on `u know what fog is :D
Fog distance 15000 `this is distance in which u can't see anything through fog
`it makes less colorful objects in smaller distance too
`if u remove fog, skybox will be very bright
Fog color RGB(128,128,128) `fog color- grey
maximize window `make full screen
randomize timer() `set starting value for random() function
`every time u run program random numbers will be different
xrotate camera 20 `point camera little down
`hide mouse `U can hide mouse cursor if want. I like to see it :P
`later U can put some image instead of cursor i.e. target
`re-calculates the normals of each polygon in an object
`for the purpose of working out the rendering of each of the polygons
set normalization on
endfunction
`^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
function TerrainSetup() `make and set up terrain
set dir "media\DefaultMap\" ` set the directory to map's location (2 levels higher)
` load base and detail texture
load image "texture.bmp", 1
load image "detail.tga", 2
make object terrain 1 ` create the terrain object
set terrain heightmap 1, "map.bmp" ` set the heightmap
set terrain scale 1, 3, 0.1, 3 ` set the scale
set terrain split 1, 16 ` split value by 16 * 16
set terrain tiling 1, 4 ` detail map tiling
set terrain light 1, 1, -0.25, 0, 1, 1, 0.78, 0.5 ` light - xdir, ydir, zdir, red, green, blue, intensity
set terrain texture 1, 1, 2 ` base and detail texture
build terrain 1 ` finally build the terrain
position object 1, 0,0,0 ` position terrain at zero coordinates
sc_setupComplexObject 1,1,2 ` setup collision on the terrain
set dir ".." ` reset the directory (1 level lower to media\)
set dir "Skybox\" ` set the directory to skybox's location
`load our skybox (this is very low quality, u can find smth better)
`this file has images asociated (1-6 jpg images in media directory).
`U can change or edit them
load object "skybox.x", 6
set object light 6, 0
set object texture 6, 3, 1 `not sure what it should do, but it improves skybox texture :)
position object 6, 500, 50, 500 `~in middle of terrain
scale object 6, 10000, 10000, 10000 `make skybox big!
set dir ".." `reset the directory back to media\
endfunction
`^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
function MoveCamera()
`keyboard input for movement
if upkey()=1 then move camera 3
if downkey()=1 then move camera -3
if leftkey()=1 then yrotate camera wrapvalue(camera angle y()-2)
if rightkey()=1 then yrotate camera wrapvalue(camera angle y()+2)
`find out the camera height
x#=camera position x()
z#=camera position z()
y#=get terrain ground height(1,x#,z#)+30
`position the camera
position camera x#,y#,z#
endfunction
`^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
function SelectObjects()
`function to pick any object's whose number is between 10 and TotalUnitCount+10
`using screen coordinates
which= pick object(mousex(),mousey(),10,TotalUnitCount+10)
endfunction which `return value which object was selected, if 0 then none of objects
`^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
function MakeBoxes() `make 20 random boxes
make object box 8,10,7,15 `make object
make mesh from object 1, 8 `extract shape of object
make object box 9,5,4,7 `make object
make mesh from object 2, 9 `extract shape of object
delete object 8
delete object 9
a=10+UnitCount
b=a+10
for i= a to b `10 times
make object box i,10,3,15 `make box
add limb i, 1, 1 `add a part for object, parts shape is mesh 1
offset limb i, 1, 0, 0, 1 `position part of object in local coordinates
hide limb i, 1 `hide- make that part invisible
add limb i, 2, 2
offset limb i, 2, 0, 2, -1
x=rnd(200)+50 `get random x and z coordinates
z=rnd(200)+50
y=get terrain ground height (1,x,z) `get height in that point
position object i,x,y+4,z `position object +4 units above ground
rotate object i,0,rnd(100),0 `random rotate object object
color object i,rgb(0,255,0) `color object- green
Unit(i).Stance=1
Unit(i).Exist=1
Unit(i).LastShotTime=0
Unit(i).ReloadTime=100 `miliseconds
Unit(i).ProjectileType=1
Unit(i).Name="Machinegunner Box"
Unit(i).Hp=100
Unit(i).Dmg=1
Unit(i).RotationSpeed=1.5
unit(i).maxspeed=0.6
Unit(i).Speed=0.0
Unit(i).IsMoving=0.0
Unit(i).IsFollowing=0.0
Unit(i).Traction=0.015
Unit(i).Team=1
Unit(i).Range=16000 `squared(^2)
next i
a=b+1
b=a+10
for i= a to b `make 10 more boxes in other team
make object box i,10,4,15
add limb i, 1, 1
offset limb i, 1, 0, 0, 1
hide limb i, 1
add limb i, 2, 2
offset limb i, 2, 0, 2, -1
x=rnd(200)+200
z=rnd(200)+200
y=get terrain ground height (1,x,z)
position object i,x,y+4,z
rotate object i,0,rnd(100),0
color object i,rgb(128,128,0) `color boxes "yellow-green" lol
Unit(i).Stance=2
Unit(i).Exist=1
Unit(i).LastShotTime=0
Unit(i).ReloadTime=2000 `miliseconds
Unit(i).ProjectileType=1
Unit(i).Name="Cannon Box"
Unit(i).Hp=120
Unit(i).Dmg=20
Unit(i).RotationSpeed=1.0
unit(i).maxspeed=0.4
Unit(i).Speed=0.0
Unit(i).IsMoving=0.0
Unit(i).IsFollowing=0.0
Unit(i).Traction=0.01
Unit(i).Team=2
Unit(i).Range=16000 `this is squared (^2)
next i
endfunction
`^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
function SelectionIndicator(obj) `indicator handler
if obj=0: `if no objects selected
if object visible(2)=1: `if indicator visible
hide object (2) `make it invisible
endif
else `if some object is selected
if object visible(2)=0: `if indicator is not visible
show object (2) `make it visible
endif
`position indicator +10 units above your selected object
if Object exist (obj) then position object 2,object position x (obj),object position y (obj)+10,object position z(obj)
endif
endfunction
`^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
function MakeSelectionIndicator() `make objects for indicators
make object cone 2, 5 `for active object
zrotate object 2, 180 `rotate/point down
set object 2, 1, 1, 0 `don't know what this does
color object 2,RGB(0, 255, 0) `green color
make object cone 3, 5 `to mark target-coordinates on map
zrotate object 3, 180 `rotate/point down
set object 3, 1, 1, 0
color object 3,RGB(255, 255, 0) `yellow color
endfunction
`^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
function Info()
`str$(a) function returns integer, float or other type variable as string
`"text x,y, string" function can't print other variables than strings
`strings can be added to each other i.e. "cat"+"dog"+str$(2)="catdog2"
text 10,10,"Object selected :"+str$(activeID) `selected, active object
text 10,30,"FPS :" + str$(Screen FPS()) `frames per second (how many times screen refreshes)
`determines how hard program is for pc, fps will go down if pc is too weak
`my 1.8Ghz, 1.5Gb Ram machine shows 80-90 in this program stage.
`as more functions are added it should slow down
`text 10,50,"Left mouse click to select object"
`text 10,70,"Right mouse click to place target/target object"
endfunction
`^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
function object_distance_squared(a,b) `returns distance between objects squared
`(distance^2 or distance*distance)
w#= (object position x(a)-object position x(b))^2 `difference in x axis ^2
h#= (object position y(a)-object position y(b))^2 `difference in y axis ^2
l#= (object position z(a)-object position z(b))^2 `difference in z axis ^2
d#= l#+w#+h# `summ up all distances (like pitagores just in 3D)
`if u want to get real distance use d#= sqrt(l#+w#+h#)
`but square root is calculated long, so if u make many calculations
`program can slow down signicicantly
endfunction d#
`^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
function Projectile_distance(a) `similar to object_distance_squared
`remstart
w#= (object position x(projectile(a).Object)-projectile(a).StartX)^2 `difference in x axis ^2
h#= (object position y(projectile(a).Object)-projectile(a).StartY)^2 `difference in y axis ^2
l#= (object position z(projectile(a).Object)-projectile(a).StartZ)^2 `difference in z axis ^2
d#= l#+w#+h#
OutOfRange=0
if projectile(a).range<d# then OutOfRange=1
`remend
endfunction OutOfRange
`^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
function LoadMenuPanel() `this will load sprites for menu
set dir "Sprites\" `directory for sprites
load image "MiniMap.bmp",3 `minimap in left corner
sprite 1,0,600,3
hide sprite 1
load image "Panel.bmp",4 `will be info about selected unit/object
sprite 2,250,600,4
hide sprite 2
set dir ".." `reset directory to media\
endfunction
`^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
function MakeTarget() `place target indicator if needed (i cant comment this funtion well, because its no my made.
`forum link for it: http://forum.thegamecreators.com/?m=forum_view&t=79830&b=1
obj=SelectObjects() `targeted object number
if obj=0 `if none objects selected
mX = mouseX()
mY = mouseY()
cx# = camera position x()
cy# = camera position y()
cz# = camera position z()
pickState = Pick Object (mX, mY, 1, 1)
If pickState <> 0 `if nothing selected
Dist# = Get Pick Distance()
Pick Screen mX, mY, Dist#
indX# = cx# + get pick vector x()
indY# = cy# + get pick vector y()
indZ# = cz# + get pick vector z()
Position Object 3, indX#, indY#+3, indZ# `position indicator on terrain
Unit(ActiveID).Dx=indX# `set units destination coordinates
Unit(ActiveID).Dy=indY#
Unit(ActiveID).Dz=indZ#
Unit(ActiveID).IsFollowing=0 `no more folowing or attacking
unit(i).movetarget=0
unit(i).attacktarget=0
Unit(ActiveID).IsMoving=1 `start moving to destination coordinates
if unit(ActiveID).MoveTarget<>0 then unit(ActiveID).MoveTarget=0
if object visible(3)=0 then show object (3) `show destination point indicator
EndIf
else
unit(ActiveID).MoveTarget=obj `set units target value to selected object
`if target object is enemy, set attack target
if unit(ActiveID).MoveTarget>0 and unit(obj).Team<>unit(ActiveID).Team
unit(ActiveID).AttackTarget=obj
else
unit(ActiveID).AttackTarget=0
endif
Unit(ActiveID).IsMoving=0 `no more moving to destination coordinates
Unit(ActiveID).IsFollowing=1 `start folowing
if object visible(3)=1 then hide object (3) `hide destination point indicator
endif
endfunction
`^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
function MoveObjects()
for i=10 to TotalUnitCount `for all controllable objects
if unit(i).exist=1
if unit(i).IsMoving <>0 `if object is moving to destination point
MoveObject(i) `then move it
if abs(unit(i).dx-object position x(i))<5 and abs(unit(i).dz-object position z(i))<5 `if at destination. stop object
unit(i).Ismoving=0 `if reached destination point then stop
endif
endif
if unit(i).IsFollowing <>0 `if object is folowing other object
if unit(unit(i).MoveTarget).Team=unit(i).Team
if object_distance_squared (i,Unit(i).MoveTarget)>FollowDistance `if is too far
FollowObject(i) `then move it towards target
endif
else
if object_distance_squared (i,Unit(i).MoveTarget)>Unit(i).Range-10 then FollowObject(i)
endif
endif
endif
next i
endfunction
`^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
function MoveObject(ID) `made by Neuro Fuzzy, this is little edited to suit this program
`link to His tutorial: http://www.neurofuzzydev.com/V/D/43a.htm
`link to download full vectors tutorial:
`http://forum.thegamecreators.com/?m=forum_view&t=133126&b=7
Angle# = GetShortestAngle(object position x(ID), object position z(ID), object angle y(ID),unit(ID).dx, unit(ID).dz)
if Angle# <= -1.0 then turn object left ID, Unit(ID).RotationSpeed
if Angle# >= 1.0 then turn object right ID, Unit(ID).RotationSpeed
text 200,(ID-unitCount-10)*10, str$(angle#)
Rem PJY - now depending on the angle and the distance between the cubes
Rem PJY - we move the selected cube. The reason for the following code is that
Rem PJY - without it in certain situations the seleceted cube could begin
Rem PJY - "orbiting" the targeted cube, i.e. where it is moving so much per turn
Rem PJY - that it is not decreasing the angle
`all of the following code meaning is to slow down object
`when it is near or in big angle with it destination point
`the best way to set angles, distances and slow down/acelerate values
`is to experiment.
`all these values varie with different box sizes, speeds, stop distances
`if object is pointing ~ at its target then acelerate till max speed
if abs(angle#) < 1
if unit(ID).speed<unit(ID).maxspeed then unit(ID).speed=unit(ID).speed+unit(ID).traction
endif
`if object is pointing sharp angle at its target then acelerate till max speed/1.5
if abs(angle#) => 1 AND angle# < 10
if unit(ID).speed<unit(ID).maxspeed/1.5
unit(ID).speed=unit(ID).speed+unit(ID).traction
else
dec unit(ID).speed, 0.01 `if too fast, decrease speed
endif
endif
`if object is pointing bigger angle at its target then acelerate till max speed/2
if abs(angle#) => 10 AND angle# < 90 AND distance# > 20
if unit(ID).speed<unit(ID).maxspeed/2
unit(ID).speed=unit(ID).speed+unit(ID).traction
else
dec unit(ID).speed, 0.01 `if too fast, decrease speed
endif
endif
`if object is pointing away from its target then acelerate till max speed/8
if abs(angle#) => 90 AND angle# < 120 AND distance# > 20
if unit(ID).speed<unit(ID).maxspeed/8
unit(ID).speed=unit(ID).speed+unit(ID).traction
else
dec unit(ID).speed, 0.01 `if too fast, decrease speed
endif
endif
`if object is pointing away from its target and distance is small
`then acelerate till max speed/10
if abs(angle#) => 120 AND distance# > 10
if unit(ID).speed<unit(ID).maxspeed/10
unit(ID).speed=unit(ID).speed+unit(ID).traction
else
dec unit(ID).speed, 0.01 `if too fast, slow down
endif
endif
`i added this by experimenting with boxes, to slow down even more
if abs(angle#) => 80 AND distance# < 20
if unit(ID).speed<unit(ID).maxspeed/20
unit(ID).speed=unit(ID).speed+unit(ID).traction
else
dec unit(ID).speed, 0.02 `if too fast, slow down
endif
endif
move object ID, unit(ID).speed `when we set units speed, now can finally move it, YeY!
`get coordinates of object
Ox#=object position x(ID)
Oz#=object position z(ID)
Oy#=get terrain ground height (1,Ox#,Oz#)+4
`and place it on terrain
position object ID,Ox#,Oy#,Oz#
endfunction
`^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
function FollowObject(ID) `this is a simple function,
`which changes object destination coordinates into followed object coordinates,
Unit(ID).dx=object Position X(Unit(ID).Movetarget)
Unit(ID).dz=object Position Z(Unit(ID).Movetarget)
`and then calls MoveObject() function to move object
MoveObject(ID)
endfunction
`^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
function ObjectInfo() `write selected object information
if activeID<>0
text 610, 680, "Name: "+ Unit(ActiveID).Name
text 610, 695, "Hp: "+ str$(Unit(ActiveID).Hp)
text 610, 710, "Dmg: "+ str$(Unit(ActiveID).Dmg)
endif
endfunction
`^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
function RefreshSprites()
paste sprite 1,0,600
paste sprite 2,250,600
endfunction
`^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
function Attack()
for i=10 to TotalUnitCount+10 `for all controllable objects
if unit(i).AttackTarget<>0
if object_distance_squared(i,unit(i).AttackTarget)<unit(i).range
Time=timer()
if Unit(i).LastShotTime+Unit(i).ReloadTime< Time
MakeProjectile(i)
Unit(i).LastShotTime=Time
endif
endif
endif
next i
endfunction
`^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
function MakeProjectile(ID)
found=0
i=0
while found=0 and i<ProjectilesCount-1
inc i, 1
if projectile(i).type=0
found=i
endif
endwhile
projectile(i).type=1
n=i+TotalUnitCount+10
if object exist (n) then delete object n
Make object sphere n, 10
color object n,rgb(128,128,128)
projectile(i).Object=n
scale object n, 10,10,30
x=object position x(ID)
y=object position y(ID)
z=object position z(ID)
projectile(i).Startx=x
projectile(i).Starty=y
projectile(i).Startz=z
Position Object n,x,y,z
Point Object n, object position x(Unit(ID).attacktarget),object position y(Unit(ID).attacktarget),object position z(Unit(ID).attacktarget)
projectile(i).Range=Unit(ID).Range
projectile(i).dmg=Unit(ID).dmg
projectile(i).speed=2.0
projectile(i).ParentObject=ID `object who shot projectile- not to check collision with it
endfunction
`^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
function MoveProjectiles()
col=0
for i= 0 to ProjectilesCount
if projectile(i).type>0
if Projectile(i).type=1
Move Object projectile(i).object, projectile(i).speed
col=OBJECT COLLISION(projectile(i).object, 0)
if col>=10 and col<=TotalUnitCount and col<>projectile(i).ParentObject
dec unit(col).Hp,projectile(i).dmg
DestroyProjectile(i)
ProjectileDestroyed=1
if unit(col).Hp<1 then DestroyObject(col)
else
if Projectile_distance(i)=1 then DestroyProjectile(i)
endif
endif
endif
next i
endfunction
`^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Function DestroyObject(ID)
for i=10 to TotalUnitCount+10
if Unit(i).Movetarget=ID
Unit(i).IsFollowing=0
Unit(i).Movetarget=0
endif
if Unit(i).Attacktarget=ID then Unit(i).Attacktarget=0
next i
found=0
i=0
while found=0 and i<DestroyedUnitsCount-10
inc i, 1
if DestroyedUnit(i).exist=0
found=i
endif
endwhile
make mesh from object 1, ID
m=10+TotalUnitCount+OtherObjectsCount+i
make object m,1,0
color object m, RGB (0,0,0)
position object m, Object position x (ID), Object position y (ID), Object position z (ID)
rotate object m, Object angle x (ID), Object angle y (ID),Object angle z (ID)
DestroyedUnit(i).exist =1
DestroyedUnit(i).Name =Unit(ID).Name
DestroyedUnit(i).MaxHp =Unit(ID).Maxhp
DestroyedUnit(i).Dmg =Unit(ID).dmg
DestroyedUnit(i).MaxSpeed =Unit(ID).MaxSpeed
DestroyedUnit(i).Traction =Unit(ID).traction
DestroyedUnit(i).RotationSpeed =Unit(ID).RotationSpeed
DestroyedUnit(i).ProjectileType = Unit(ID).ProjectileType
DestroyedUnit(i).ReloadTime =Unit(ID).ReloadTime
DestroyedUnit(i).Range =Unit(ID).Range
Unit(ID).Name=""
Unit(ID).Exist=0
Unit(ID).Team=0
Unit(ID).Maxhp=0
Unit(ID).hp=0
Unit(ID).dmg=0
Unit(ID).MaxSpeed=0
Unit(ID).Speed=0
Unit(ID).traction=0
Unit(ID).RotationSpeed=0
Unit(ID).Range=0
Unit(ID).ProjectileType=0
Unit(ID).LastShotTime=0
Unit(ID).ReloadTime=0
Unit(ID).MoveTarget=0
Unit(ID).AttackTarget=0
Unit(ID).IsMoving=0
Unit(ID).IsFollowing=0
Unit(ID).Dx=0
Unit(ID).Dy=0
Unit(ID).Dz=0
Delete Object ID
endfunction
`^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Function DestroyProjectile(ID)
delete object (projectile(ID).object)
projectile(ID).Object=0
projectile(ID).type=0
projectile(ID).dmg=0
projectile(ID).Speed=0
projectile(ID).ParentObject=0
projectile(ID).AttackTarget=0
endfunction
`^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Function RotateTurrets()
a=10+UnitCount
b=a+vehiclesCount
for i=a to b
`lets do the same to find out which direction should vehicles turret turn
if unit(i).AttackTarget<>0
text 400, (i-a)*10,str$(unit(i).attacktarget)
turretangle#=wrapvalue(object angle y(i)+ limb angle y (i,2))
Angle# = GetShortestAngle(object position x(i), object position z(i), turretangle#, object position x(unit(i).attacktarget), object position z(unit(i).attacktarget))
text 400, (i-a)*10, str$(Angle#)
if abs (angle#)>0.1
text 500, (i-a)*10, "shouldrotate"
Rem PJY - select whether to turn right or left
if angle#<0.0
rotate limb i, 2, limb angle x(i,2), limb angle y(i,2)-1, limb angle z(i,2)
else
rotate limb i, 2, limb angle x(i,2), limb angle y(i,2)+1, limb angle z(i,2)
endif
endif
endif
next i
endfunction
`^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Function CheckNewTargets()
found=0
b=10+TotalUnitCount
for i=10 to b
if unit(i).exist=1
if unit(i).attacktarget=0 and unit(i).stance>0
for n=10 to b
while n<b and found=0
inc n
if n<>i and unit(n).exist=1 and unit(n).team<>unit(i).team
if object_distance_squared(i,n)<unit(i).range
found=1
unit(i).attacktarget=n
if unit(i).stance=2 then unit(i).movetarget=n
endif
endif
endwhile
next n
endif
endif
next i
endfunction
`^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
function GetShortestAngle(x#, y#, ay#, tx#, ty#)
text 500, 10, str$(x#)
text 500, 20, str$(y#)
text 500, 30, str$(ay#)
text 500, 40, str$(tx#)
text 500, 50, str$(ty#)
`set vectors
set vector2 1, newxvalue(x#, ay#, 1.0), newzvalue(y#, ay#, 1.0)
set vector2 2, tx# - x#, ty# - y#
normalize vector2 1, 1
normalize vector2 2, 2
`Find data
ang# = acos(dot product vector2(1, 2))
text 500, 70, str$(ang#)
if ccw vector2(1, 2) < 0.0 then ang# = -ang#
text 500, 80, str$(ang#)
endfunction ang#
i would prefer to use as economical function as possible, coz now have only 60 FPS after adding function to check distances between objects- for automatic enemy targeting.
all this program will be rewrited and structure changed later, as now i only learn and want to have "standalone" functions for everything: will make get distance function to use vectors, maybe 3dimensional matrix for holding all object-to-object distances if they aren't moving and much more
added media too -->
Join The dark Side! We have cookies