@BN2
Ok here's the code It should work without any media.
REM --------------------------------------------------------------
REM Tower AI
REM
REMSTART
Ashingda's Note:
The Media and LoadMediaImages.dba should be included in the zip.
You can load all the media I created calling that function.
For this Example to work I made some temporary subsroutines that
does basic things.
SpawnAI - Constantly spawns Foe every 10 count. Foe who are offscreen
or killed are reset and reused for spawning.
Creep - Moves every Foe from left to right until offscreen.
BuyTower - Leftclick will place a SingleShot Tower and Rightclick will
pace an 8WayShot Tower.
Other things that are not done are REMed where it's supose to be.
REMEND
REM
REM --------------------------------------------------------------
sync on
sync rate 80
create bitmap 1,640,480
dim c(50)
c(1) = rgb(0,255,0)
c(2) = rgb(255,255,0)
c(3) = rgb(155,155,0)
c(4) = rgb(255,0,0)
c(5) = rgb(255,0,255)
c(6) = rgb(0,0,255)
c(7) = rgb(80,80,80)
c(8) = rgb(200,200,200)
c(9) = rgb(155,255,255)
c(10) = rgb(255,155,255)
REM Loads up the Media
`#include "LoadMediaImages.dba"
gosub TowerAISetup
`LoadMediaImages()
REM --------------------------------------------------------------
REM Main Loop
REM --------------------------------------------------------------
do
REM Background Image
`paste image 51,0,0
gosub SpawnAI
gosub TowerAI
gosub Creep
gosub BuyTower
text 0,0,"FPS: "+str$(screen fps())
copy bitmap 1,0
sync : cls 0
loop
REM ***********************************************************
REM For this example to work I inserted a temporary Spawn AI
REM ***********************************************************
SpawnAI:
REM Increase Count
if Count < 1000 then Count = Count + 1
REM Spawns Foe every 10 Count
if Count > 80
for loops = 0 to MaxFoe
if Foe(loops,0) = 0
REM Set Foe Values
Foe(loops,0) = rnd(9)+1
Foe(loops,1) = 0
Foe(loops,2) = 280
Foe(loops,3) = 0
Foe(loops,4) = 0
Foe(loops,5) = 0
Foe(loops,6) = 0
Foe(loops,7) = 0
Foe(loops,8) = 0
Foe(loops,9) = 1
Loops = MaxFoe
endif
next loops
REM Reset the Count
Count = 0
endif
return
REM ***********************************************************
REM For this example to work I inserted a temporary Creep AI
REM ***********************************************************
Creep:
REM Checks if Foe exist
for loops = 0 to MaxFoe
if Foe(loops,0) > 0
REM Increase the X coord and Travled value.
Foe(loops,1) = Foe(loops,1) + Foe(loops,9)
Foe(loops,6) = Foe(loops,6) + Foe(loops,9)
REM Drawing the Face
x = Foe(loops,1)
y = Foe(loops,2)
`paste image 100+Foe(loops,0),x-13,y-13,1
ink c(Foe(loops,0)),0
box x-13,y-13,x+13,y+13
ink rgb(0,0,0),0
text x,y,str$(Foe(loops,0))
REM If this Enemy goes off screen then reset this Enemy.
if x > 640 then Foe(loops,0) = 0
endif
next loops
return
REM ***********************************************************
REM For this example to work I inserted a temporary Buy Tower
REM ***********************************************************
BuyTower:
mx = mousex()
my = mousey()
mc = mouseclick()
REM Checks for Mouse Click. MouseClick can either be 1 for leftclick
REM or 2 for rightclick and that's used to indicate which tower to
REM buy and put on the screen in the following code.
if mc > 0
REM The keyM variable flags 1 as the mouse is clicked and flags 0
REM when the mouse is not clicked. This flag makes it posible to
REM only run the following onces per click.
if keyM = 0
REM Searches all Foe index to see if it's unused.
for loops = 0 to MaxTower
REM When an unused index is found then it's used. Setting Loops
REM to MaxFoe will break out of there.
if Tower(loops,0) = 0
Type = mc
ShotSpeed = TowerStats(Type,0)
ShotRange = TowerStats(Type,1)
ShotReload = TowerStats(Type,2)
Tower(loops,0) = Type
Tower(loops,1) = mx
Tower(loops,2) = my
Tower(loops,3) = 0
Tower(loops,4) = ShotSpeed
Tower(loops,5) = ShotRange
Tower(loops,6) = ShotReload
Tower(loops,7) = 0
Tower(loops,8) = 0
loops = MaxTower
endif
next loops
endif
KeyM = 1
else
keyM = 0
endif
return
REM **************************************************************
REM Below is all in the TowerAI section.
REM **************************************************************
REM --------------------------------------------------------------
REM TowerAISetup
REM --------------------------------------------------------------
TowerAISetup:
REM Max amount of Towers and Foe
MaxTower = 50
MaxFoe = 50
REM Tower(number,0) = Type
REM Tower(number,1) = X
REM Tower(number,2) = Y
REM Tower(number,3) = Shooting
REM Tower(number,4) = Shot Speed
REM Tower(number,5) = Shot Range
REM Tower(number,6) = ReLoad Time
REM Tower(number,7) = Ready to shoot
REM Tower(number,8) = Targeting
REM Tower(number,9) = LockShotX
REM Tower(number,10) = LockShotY
dim Tower(MaxTower,10)
REM TowerStats(number,0) = ShotSpeed
REM TowerStats(number,0) = ShotRange
REM TowerStats(number,0) = ShotReload
dim TowerStats(5,2)
REM Default stats for the Tower1 the homming shooter
TowerStats(1,0) = 16
TowerStats(1,1) = 100
TowerStats(1,2) = 50
REM Default stats for the Tower2 the 8-way shooter
TowerStats(2,0) = 2
TowerStats(2,1) = 50
TowerStats(2,2) = 80
REM This is to store the Foe within range to check later with the arrows
REM used in the 8-way shot Tower. The use of this is connected with the
REM array Arrows()
dim InRange(MaxFoe)
REM This is for the 8-way shot Tower to keep track of 8 different arrows.
dim Arrows(MaxTower,7)
REM Makes sure that each towers can only hit a Foe once per shot.
REM This is important so the 8-way shooter only lands 1 arrow
REM on each Foe per shot.
dim HitLimit(MaxTower,MaxFoe)
REM Foe(index,0) = Face Type , Ranges from 1 to 10
REM Foe(index,1) = X
REM Foe(index,2) = Y
REM Foe(index,3) = Destination X
REM Foe(index,4) = Destination Y
REM Foe(index,5) = Current WayPoint
REM Foe(index,6) = Traveled , Default=0
REM Foe(index,7) = Targeted , Default=0
REM Foe(index,8) = Spawn Interval
REM Foe(index,9) = Travel Speed
dim Foe(MaxFoe,9)
return
REM --------------------------------------------------------------
REM TowerAI
REM --------------------------------------------------------------
TowerAI:
REM Checks each Tower to see if it exist on the map.
for loops = 0 to MaxTower
REM When a Tower exist on the map, run the apropriate subs
REM to handle that TowerType.
REM 1 is SingleShooter, 2 is 8WayShooter, 3 is Catapult
check = Tower(loops,0)
if check > 0
if check = 1 then gosub Tower1
if check = 2 then gosub Tower2
REM Check 3 is currently being worked on
endif
next loops
return
REM --------------------------------------------------------------
REM Tower1 - Single Shooter
REM --------------------------------------------------------------
Tower1:
REM Drawing the Tower
x = Tower(loops,1)
y = Tower(loops,2)
ink rgb(200,200,100),0
box x-16,y-16,x+16,y+16
`paste image 216,x-16,y-16,1
REM The Variable Mode is used the the InRangeCheck. I tried but
REM couldn't trun that sub into a Function as some variables
REM needed wasn't being carried over and couldn't be carried back.
mode = 1
gosub InRangeCheck
gosub ShootSingle
return
REM --------------------------------------------------------------
REM Tower2 - 8 Way Shooter
REM --------------------------------------------------------------
Tower2:
REM Drawing the Tower
x = Tower(loops,1)
y = Tower(loops,2)
ink rgb(100,100,200),0
box x-16,y-16,x+16,y+16
`paste image 217,x-16,y-16,1
REM The Variable Mode is used the the InRangeCheck. I tried but
REM couldn't trun that sub into a Function as some variables
REM needed wasn't being carried over and couldn't be carried back.
mode = 0
gosub InRangeCheck
gosub Shoot8Ways
return
REM --------------------------------------------------------------
REM Tower3 - Catapult
REM --------------------------------------------------------------
Tower3:
REM Still need to work on as I need a working WayPointAI for this one.
return
REM --------------------------------------------------------------
REM InRangeCheck
REM --------------------------------------------------------------
InRangeCheck:
REM Cool down time reseting.
if Tower(loops,7) > 0 then Tower(loops,7) = Tower(loops,7) - 1
if Tower(loops,7) = 0
REM Resetting Variables. Targeting, ArrowTravel, EnemyCheckTravel
Tower(loops,8) = -1
Tower(loops,3) = 0
et = 0
REM Reset the arrows for Towers
for lp2 = 0 to 7
Arrows(loops,lp2) = 1
next lp2
REM Reset the HitLimits to zero
for lp2 = 0 to MaxFoe
HitLimit(loops,lp2) = 0
next lp2
REM Loop 50 times
for lp = 0 to MaxFoe
REM Check Type, 0 means it's dead.
if Foe(lp,0) > 0
REM Input for use X,Y,Traveled
ix = Foe(lp,1)
iy = Foe(lp,2)
it = Foe(lp,6)
REM ****************************************************
REM This is a Substitute for the WayPointAI
REM ****************************************************
REM Mode1 Calculations for the Homming Tower. This section
REM is where I'm supose to call the WayPointAI and see where
REM the Foe's position is in x amount of Traveled.
if mode = 1
REM Calculate the furture X position. This works but it
REM also shorten up the shooting range so it's not a
REM good substitute. Turned it off.
`distance = abs(sqrt( (x-ix)^2 + (y-iy)^2 ))
`ix = ix + Foe(lp,9) * (distance/(Tower(loops,4)/2))
endif
distance = abs(sqrt( (x-ix)^2 + (y-iy)^2 ))
if distance < Tower(loops,5)
if it > et and Foe(lp,7) < Foe(lp,0)
et = it
Tower(loops,8) = lp
Tower(loops,7) = Tower(loops,6)
if mode = 0 then lp = 50
endif
endif
endif
next lp
REM Flag the Target element only if it's tower1
if Tower(loops,8) > -1 and mode > 0
Foe(Tower(loops,8),7) = Foe(Tower(loops,8),7) + 1
endif
REM Mode 2 is experiment for the Catapult.
REM This is currently NOT IN USE as I need to WayPointAI.
if mode = 2
lp = Tower(loops,8)
ix = Foe(lp,1)
iy = Foe(lp,2)
it = Foe(lp,6)
Tower(loops,9) = ix
Tower(loops,10) = iy
endif
endif
return
REM --------------------------------------------------------------
REM ShootSingle Homming
REM --------------------------------------------------------------
ShootSingle:
if Tower(loops,8) > -1
if Foe(Tower(loops,8),0) > 0
ex = Foe(Tower(loops,8),1)
ey = Foe(Tower(loops,8),2)
b = atanfull(x-ex,y-ey)
REM Get the distance of the Tower and Mouse
distance = abs(sqrt( (x-ex)^2 + (y-ey)^2 ))
REM If the mouse is within shooting range.
if distance < Tower(loops,5)+50
x1 = x-sin(b)*Tower(loops,3)
y1 = y-cos(b)*Tower(loops,3)
x2 = x-sin(b)*(Tower(loops,3)+8)
y2 = y-cos(b)*(Tower(loops,3)+8)
REM Update the shooting value
Tower(loops,3) = Tower(loops,3) + Tower(loops,4)
REM Get the current position of the Arrow/Shot
position = abs(sqrt( (x-x1)^2 + (y-y1)^2 ))
REM Draw the Arrow
ink rgb(255,255,255),0
line x1,y1,x2,y2
REM Kill the Targeted enemy
if position > distance
REM Clear Type
Foe(Tower(loops,8),0) = Foe(Tower(loops,8),0) - 1
REM Clear Targeted on Foe
Foe(Tower(loops,8),7) = Foe(Tower(loops,8),7) - 1
REM Out of bounce (negative) check making it zero
if Foe(Tower(loops,8),0) < 0 then Foe(Tower(loops,8),0) = 0
REM Clear Targeted on Tower
Tower(loops,8) = -1
endif
endif
endif
endif
return
REM --------------------------------------------------------------
REM Shoot8Ways
REM --------------------------------------------------------------
Shoot8Ways:
if Tower(loops,8) > -1
REM Checks and record the Foe who are within range.
REM This is so we dont have to loop 50 times to check
REM the Foes for each arrows for later.
REM The recorded Foe are stored into the InRange() array.
REM z is used to count how many Foe are within range and
REM to loop when checking for collision later.
z = -1
for lp2 = 0 to MaxFoe
if Foe(lp2,0) > 0
ix = Foe(lp2,1)
iy = Foe(lp2,2)
distance = abs(sqrt( (x-ix)^2 + (y-iy)^2 ))
REM If Foe is within range.
if distance < Tower(loops,5)
z = z + 1
InRange(z) = lp2
endif
endif
next lp2
REM Loops for 8 arrows
for lp = 0 to 7
if Arrows(loops,lp) > 0
a = 45 + lp*45
ex = x + sin(a)*Tower(loops,5)
ey = y + cos(a)*Tower(loops,5)
b = atanfull(x-ex,y-ey)
x1 = x-sin(b)*Tower(loops,3)
y1 = y-cos(b)*Tower(loops,3)
x2 = x-sin(b)*(Tower(loops,3)+5)
y2 = y-cos(b)*(Tower(loops,3)+5)
REM Update the shooting value
Tower(loops,3) = Tower(loops,3) + Tower(loops,4)
REM When the shooting value exceeds the shooting range
REM then reset the shooting value to zero.
if Tower(loops,3) > Tower(loops,5)
`Tower(loops,3) = 0
Tower(loops,8) = -1
endif
REM Draw the Arrow
ink rgb(255,255,255),0
line x1,y1,x2,y2
REM Checking the Foe getting hit.
for lp2 = 0 to z
Check = InRange(lp2)
ix = Foe(Check,1)
iy = Foe(Check,2)
if HitLimit(loops,Check) < 1
if CheckArea(x2,y2,ix,iy,13) = 1
REM DownGrade the Foe and prevent from going negative.
Foe(Check,0) = Foe(Check,0) - 1
if Foe(Check,0) < 0 then Foe(Check,0) = 0
REM Exhaust the arrow.
Arrows(loops,lp) = Arrows(loops,lp) - 1
REM Flaged that this Foe was hit by this tower once.
HitLimit(loops,Check) = HitLimit(loops,Check) + 1
lp2 = z
endif
endif
next lp2
endif
next lp
endif
return
REM --------------------------------------------------------------
REM CheckArea Function
REM --------------------------------------------------------------
Function CheckArea(px,py,x,y,size)
retval = 0
if px > x-size & px < x+size & py > y-size & py < y+size then retval = 1
EndFunction retval