DECLARE SUB messages ()
DECLARE SUB addbullet (startx%, starty%, endx%, endy%, strength%, owner%, Speed%)
DECLARE SUB addchatline (txt$)
DECLARE FUNCTION checkenemy% (index%)
DECLARE FUNCTION calcnextc% (index%, movec%)
DECLARE SUB checkenemies (index%)
DECLARE SUB checkways (index%)
DECLARE SUB changefacing (index%, HOR%, VER%, flag%)
DECLARE SUB buyai (index%)
'Part 2 - Elite Forces
'$DYNAMIC
'$INCLUDE: 'declars.bas'

SUB buyai (index)
'AI can buy each frame, not everything at a time, thats not fair!
armor = unita(index)
ammo = unitam(index)
money = unitm(index)
weapon = unitw(index)
IF weapon = 0 THEN
'Ok we have no weapon so first we buy a weapon
'The AI should have WeaponPrice+200 so it has a weapon and 100 rounds
 IF money >= 3500 THEN
 'Can we buy a sniper gun?
 'Probably we can!
 unitm(index) = unitm(index) - 3500
 unitw(index) = 3
  ' Buy sound of weapon on channel ??
   DQBplaySound 12, 2, 22050, 0 'Click sound of weapon!
 unitf(index) = INT(RND * 8)
 EXIT SUB
 END IF

 IF money >= 2300 THEN
 'Can we buy a Shot gun?
 'Probably we can!
 unitm(index) = unitm(index) - 2300
 unitw(index) = 4
  ' Buy sound of weapon on channel ??
   DQBplaySound 12, 2, 22050, 0 'Click sound of weapon!
 unitf(index) = INT(RND * 8)
 EXIT SUB
 END IF

 IF money >= 1700 THEN
 'Can we buy a Rifle gun?
 'Probably we can!
 unitm(index) = unitm(index) - 1700
 unitw(index) = 2
  ' Buy sound of weapon on channel ??
   DQBplaySound 12, 2, 22050, 0 'Click sound of weapon!
 unitf(index) = INT(RND * 8)
 EXIT SUB
 END IF

 IF money >= 700 THEN
 'Can we buy a mini gun?
 'Probably we can!
 unitm(index) = unitm(index) - 700
 unitw(index) = 1
  ' Buy sound of weapon on channel ??
   DQBplaySound 12, 2, 22050, 0 'Click sound of weapon!
 unitf(index) = INT(RND * 8)
 EXIT SUB
 END IF

END IF                  'BOUGHT WEAPON!!!!!!!!!!!!!!

'Ok now ammo
IF ammo < 150 AND money > 100 THEN
DQBplaySound 1, 3, 22050, 0
unitam(index) = unitam(index) + 50
unitm(index) = unitm(index) - 100
unitf(index) = INT(RND * 8)
EXIT SUB
END IF

IF armor < 50 AND money > 700 THEN
DQBplaySound 13, 15, 22050, 0
unita(index) = unita(index) + 100
IF unita(index) > 100 THEN unita(index) = 100
unitm(index) = unitm(index) - 700
unitf(index) = INT(RND * 8)
EXIT SUB
END IF
'checkways index
END SUB

REM $STATIC
FUNCTION calcnextc (index, movec)
'Set variables for next cell
y1 = INT(unitc(index) / 64)
x1 = unitc(index) - INT(y1 * 64)

y2 = INT(movec / 64)
x2 = movec - INT(y2 * 64)

NEWX = x1
NEWY = y1
NEWC = unitc(index)

IF x2 < x1 THEN NEWC = NEWC - 1
IF x2 > x1 THEN NEWC = NEWC + 1
IF y2 < y1 THEN NEWC = NEWC - 64
IF y2 > y1 THEN NEWC = NEWC + 64

calcnextc = NEWC

END FUNCTION

FUNCTION checkenemy (index)
checkenemy = -1

IF index <= 7 THEN ourteam = 0 ELSE ourteam = 1' CT or T

y11 = INT(unitc(index) / 64)
x11 = unitc(index) - INT(y11 * 64)
xb1 = x11 - 3
xb2 = x11 + 3
yb1 = y11 - 3
yb2 = y11 + 3
IF xb1 < 0 THEN xb1 = 0
IF xb2 > 63 THEN xb2 = 63
IF yb1 < 0 THEN yb1 = 0
IF yb2 > 63 THEN yb2 = 63
FOR cx = xb1 TO xb2
FOR cy = yb1 TO yb2
cll = cy * 64 + cx
playernr = mapplayers(cll)
IF playernr > -1 THEN
'ok we found a player

'Now we check what kind of team it is
id = playernr
IF id <= 7 THEN thatteam = 0 ' CT
IF id > 7 AND id <= 15 THEN thatteam = 1 ' T
IF id > 15 THEN thatteam = 2 ' Hostage!

IF ourteam <> thatteam AND thatteam < 2 THEN
'found an enemy
'for random we return this nr as enemyfound so it will be the ID we return!
'IF INT(RND * 2) = 1 THEN checkenemy = id: EXIT FUNCTION
checkenemy = id: EXIT FUNCTION
END IF



END IF
     
NEXT cy
NEXT cx

END FUNCTION

REM $DYNAMIC
'NO MORE
SUB checkways (index)
'This will find a route for a bot and will let it move till the end of it
'when it has found the end of a route and chooses that route, then it will
'move to a specific waypoint
gevondenway = 4
y11 = INT(unitc(index) / 64)
x11 = unitc(index) - INT(y11 * 64)
xb1 = x11 - 4
xb2 = x11 + 4
yb1 = y11 - 4
yb2 = y11 + 4
IF xb1 < 0 THEN xb1 = 0
IF xb2 > 63 THEN xb2 = 63
IF yb1 < 0 THEN yb1 = 0
IF yb2 > 63 THEN yb2 = 63
FOR cx = xb1 TO xb2
FOR cy = yb1 TO yb2
cll = cy * 64 + cx
routenr = mapways(cll)
IF routenr > -1 THEN
'ok we found a route nr!
'Well we found it, so the bot should know now what waypoint to choose

'To find the waypoint nr...
FOR rnr = 0 TO 40
IF rway(routenr, rnr) = cll THEN waypointnr = rnr: EXIT FOR
NEXT rnr

'BUT we do it randomly or else all bots will follow the same route
IF INT(RND * 5) = 1 THEN
unitr(index) = routenr
'IF waypointnr < gevondenway THEN unitrw(index) = waypointnr
unitrw(index) = waypointnr
gevondenway = waypointnr
END IF

END IF
      
NEXT cy
NEXT cx



END SUB

SUB DoPlayers
'AI!!!!!!!!!!!!!!!!!!!!!!!!!!!
foundterror = 0
foundcounter = 0
FOR i = 0 TO 30
mapplayers(unitc(i)) = -1 ' Clear our positions
NEXT i

FOR i = 0 TO 30
'Draws players
IF unitd(i) <> 0 THEN
IF i <= 7 THEN foundcounter = 1
IF i > 7 THEN foundterror = 1
IF unith(i) <= 0 THEN unitd(i) = 0
makestepsound = FALSE
'If a player (bot) lives
mapplayers(unitc(i)) = i
IF unitm(i) < 0 THEN unitm(i) = 0
IF i = 0 THEN
'Player
IF PlayerMoney < 0 THEN PlayerMoney = 0
unitm(i) = PlayerMoney
unith(i) = PlayerHealth
unitl(i) = 1 ' We are the leader!
unitac(i) = -1  ' No action
unita(i) = PlayerArmor
unitc(i) = playercell
unitx(i) = 8
unity(i) = 6
unitds(i) = PlayerDeaths
unitki(i) = PlayerKills
unitname$(i) = Playername$
mapplayers(unitc(i)) = i
GOTO thatsall
END IF
'Bot chat
luckynumber = INT(RND * 1000)

IF luckynumber < 1 AND i < 15 THEN
ln$ = unitname$(i) + " : " + chatlines$(INT(RND * 30))
addchatline ln$
END IF

'Basic rules
'when UNITH <= 0 then DIE!
IF unith(i) <= 0 THEN unitd(i) = 0

'AI!
IF i > 0 AND i <= 15 THEN
'Only bots buy stuff and whatever
uniti4(i) = uniti4(i) + 1
g = 20 + INT(RND * 50) + 10 + INT(RND * 40) + 5
 IF uniti4(i) > g THEN
  buyai i
  uniti4(i) = 0
 END IF

foundenemy = -1
foundenemy = checkenemy(i)

'If our foundenemy is not false then we probably found an enemy
'we will shoot at the target!
IF foundenemy > -1 THEN
id = foundenemy
enemycell = unitc(id)
EY1 = INT(unitc(id) / 64)
EX1 = unitc(id) - INT(EY1 * 64)

eplx = ((EX1 - MX) * 24) + PMX + unitx(id)
eply = ((EY1 - MY) * 24) + PMY + unity(id)
eplx = eplx + 8
eply = eply + 6

'Ok we have our enemy coordinations...
'And now we need ours!
y1 = INT(unitc(i) / 64)
x1 = unitc(i) - INT(y1 * 64)

plx = ((x1 - MX) * 24) + PMX + unitx(i)
ply = ((y1 - MY) * 24) + PMY + unity(i)
plx = plx + 8
ply = ply + 6
lengx = plx - eplx
lengy = ply - eply
IF lengx < -100 OR lengx > 100 THEN uniti3(i) = 0
IF lengy < -100 OR lengy > 100 THEN uniti3(i) = 0

'Good...
'now add a bullet and we will test it...
uniti3(i) = uniti3(i) + 1


'Well now some more smarter AI for our bots
'The problem is however that we also have to avoid the coming bullets
'


 IF uniti3(i) = 5 THEN
 uniti3(i) = 0
 addbullet plx, ply, eplx, eply, 2, i, 2
' DQBboxf 1, 1, 1, 21, 21, 0
 END IF

END IF


'Simple waypoint moving.
 IF tijd > freezetime THEN
  'When we may walk...
  'Really simpel
  'if we haven't found a waypoint yet (thats only at start!)
  'we find one..
 'Randomly we wanna choose an another waypoint
 '1 on 1000 chance!
 IF unitr(i) = -1 THEN checkways i
 'IF INT(RND * 500) = INT(RND * 500) THEN unitr(i) = -1: unitrw(i) = -1: unitway(i) = INT(RND * 2): GOTO nomove
 
  'Ok
  'It should find one
  'now simple, it will move to the waypoint nr cell
  'When our route nr <> -1 then we should MOVE to that cell!
  'Doesnt matter the facing right now... ALSO THE WAYPOINT MAY NOT BE -1
 IF unitr(i) > -1 AND unitrw(i) > -1 AND i > 0 THEN
 'IF i = 1 THEN dqbprint 1, STR$(rway(unitr(i), unitrw(i))) + "-:" + STR$(unitway(i)), 100, 100, 15
 movecell = rway(unitr(i), unitrw(i))
 IF movecell = -1 THEN
 unitrw(i) = unitrw(i) - 2
 IF unitway(i) = 1 THEN unitway(i) = 0: GOTO la
 IF unitway(i) = 0 THEN unitway(i) = 1

la:
 IF INT(RND * 3) = 1 THEN unitr(i) = -1: unitrw(i) = -1: unitway(i) = 0
 GOTO nomove
 END IF


 y1 = INT(unitc(i) / 64)
 x1 = unitc(i) - INT(y1 * 64)
 y2 = INT(movecell / 64)
 x2 = movecell - INT(y2 * 64)
 
 NEXTC = unitc(i)
 links = 0: omhoog = 0
 IF x2 < x1 THEN unitx(i) = unitx(i) - 1: NEXTC = NEXTC - 1: links = 1
 IF y2 < y1 THEN unity(i) = unity(i) - 1: NEXTC = NEXTC - 64: omhoog = 1
 IF y2 > y1 THEN unity(i) = unity(i) + 1: NEXTC = NEXTC + 64: omhoog = 2
 IF x2 > x1 THEN unitx(i) = unitx(i) + 1: NEXTC = NEXTC + 1: links = 2

 IF omhoog = 1 AND links = 0 THEN unitf(i) = 0
 IF omhoog = 1 AND links = 1 THEN unitf(i) = 1
 IF omhoog = 0 AND links = 1 THEN unitf(i) = 2
 IF omhoog = 2 AND links = 1 THEN unitf(i) = 3
 IF omhoog = 2 AND links = 0 THEN unitf(i) = 4
 IF omhoog = 2 AND links = 2 THEN unitf(i) = 5
 IF omhoog = 0 AND links = 2 THEN unitf(i) = 6
 IF omhoog = 1 AND links = 2 THEN unitf(i) = 7
 
 uniti5(i) = uniti5(i) + 1
 IF uniti5(i) > 15 THEN
 unitf2(i) = unitf2(i) + 1: uniti5(i) = 0
 steptype = 0
 IF maptile(unitc(i)) = 2 THEN steptype = 1
 IF maptile(unitc(i)) = 31 THEN steptype = 2
 IF maptile(unitc(i)) = 32 THEN steptype = 2

 IF steptype = 1 THEN t2 = 7
 IF steptype = 2 THEN t2 = 13
 makestepsound = TRUE
 
 END IF

 IF unitf2(i) > 4 THEN unitf2(i) = 0

 IF NEXTC < 0 OR NEXTC > 4095 THEN unitr(i) = -1: unitrw(i) = -1: unitway(i) = INT(RND * 2): GOTO nomove
 IF mapcol(NEXTC) <> 0 THEN unitr(i) = -1: unitrw(i) = -1: unitway(i) = INT(RND * 2): GOTO nomove

 IF unitx(i) < -23 THEN unitx(i) = 0: unitc(i) = unitc(i) - 1
 IF unity(i) < -23 THEN unity(i) = 0: unitc(i) = unitc(i) - 64
 IF unitx(i) > 23 THEN unitx(i) = 0: unitc(i) = unitc(i) + 1
 IF unity(i) > 23 THEN unity(i) = 0: unitc(i) = unitc(i) + 64

 IF unitc(i) = movecell THEN
  IF unitway(i) = 0 THEN unitrw(i) = unitrw(i) + 1
  IF unitway(i) = 1 THEN unitrw(i) = unitrw(i) - 1

  IF unitrw(i) > 40 OR unitrw(i) = -1 OR unitrw(i) < 0 THEN
 
 IF unitway(i) = 1 THEN unitway(i) = 0: unitrw(i) = unitrw(i) + 2: GOTO la2
 IF unitway(i) = 0 THEN unitway(i) = 1: unitrw(i) = unitrw(i) - 2

la2:
 
  END IF
 END IF

nomove:
 END IF
ELSE
checkways i

END IF







END IF





'File we use.
IF i <= 7 THEN body = 1         'COUNTER TERRORIST
IF i > 7 AND i <= 15 THEN body = 2 'TERRORIST
IF i > 15 THEN body = 3 ' HOSTAGE!

y1 = INT(unitc(i) / 64)
x1 = unitc(i) - INT(y1 * 64)

plx = ((x1 - MX) * 24) + PMX + unitx(i)
ply = ((y1 - MY) * 24) + PMY + unity(i)

fy = unitf(i) * 12 + unitf(i)
fx = unitf2(i) * 8 + unitf2(i)
plx = plx + 8
ply = ply + 6
IF i < 8 AND i > 0 OR i > 15 THEN
px = 0: px2 = 0
IF unith(i) > 0 THEN px = INT((unith(i) / 100) * 9)
IF unita(i) > 0 THEN px2 = INT((unita(i) / 100) * 9)
IF plx >= 0 AND (plx + 9) <= 320 AND ply - 3 >= 0 AND ply - 1 <= 200 THEN
DQBboxf 1, plx, ply - 3, plx + px, ply - 3, 124
DQBboxf 1, plx, ply - 4, plx + px2, ply - 4, 156
END IF

END IF
IF i > 0 THEN
DQBxPut 3 + body, fx, fy, fx + 7, fy + 11, 1, plx, ply
IF i <= 7 THEN
money = unitm(i)
'dqbprint 1, STR$(money), plx, ply - 10, 0
END IF

END IF

IF makestepsound = TRUE THEN
IF plx >= 0 AND plx <= 319 AND ply >= 0 AND ply <= 199 THEN DQBplaySound 7 + INT(RND * 3) + t2, 7, 22050, 0
END IF

thatsall:
mapplayers(unitc(i)) = i
END IF
NEXT i

IF foundterror = 0 THEN
message$ = "Counter Terrorist Win!": messagecounter = 15
messages
dqbcopylayer 1, 0
SLEEP 1
FOR i = 0 TO 15
unitd(i) = 0
unitr(i) = -1
unitrw(i) = -1
NEXT i
loadmap "test", 1
END IF
IF foundcounter = 0 THEN
message$ = "Terrorist Win!": messagecounter = 15
messages
dqbcopylayer 1, 0
SLEEP 1
FOR i = 0 TO 15
unitd(i) = 0
unitr(i) = -1
unitrw(i) = -1
NEXT i
loadmap "test", 1
END IF



END SUB

REM $STATIC
SUB drawbullets
FOR i = 0 TO 300
IF bulletd(i) <> 0 THEN
bx = bulletx!(i) - ((MX * 24) - PMX)
by = bullety!(i) - ((MY * 24) - PMY)

DQBpset 1, startx + incx! * t2, starty + incy! * t2, 15

bulletx!(i) = bulletx!(i) + COS(bulleta!(i)) * bullets2(i)
bullety!(i) = bullety!(i) + SIN(bulleta!(i)) * bullets2(i)


IF bulletx!(i) < 0 THEN bulletd(i) = 0
IF bulletx!(i) > 1536 THEN bulletd(i) = 0
IF bullety!(i) < 0 THEN bulletd(i) = 0
IF bullety!(i) > 1536 THEN bulletd(i) = 0

bcellx = ((bulletx!(i) - PMX) / 24)
bcelly = ((bullety!(i) - PMX) / 24)
bcell = bcelly * 64 + bcellx

IF bcell < 0 THEN bulletd(i) = 0: bcell = 0
IF bcell > 4095 THEN bulletd(i) = 0: bcell = 4095
idowner = bulleto(i)


FOR j = 0 TO 30
 IF unitd(j) <> 0 THEN

  IF idowner = j THEN GOTO volgend      'You can't get killed by your own bullets, since the bullet start excactly at the bots / players position, so else you would should yourself

 y1 = INT(unitc(j) / 64)
 x1 = unitc(j) - INT(y1 * 64)

 plx = ((x1 - MX) * 24) + PMX + unitx(j)
 ply = ((y1 - MY) * 24) + PMY + unity(j)
 plx = plx + 8
 ply = ply + 6
  IF DQBcollide(plx, ply, VARSEG(plmask(0)), VARPTR(plmask(0)), bx, by, VARSEG(blmask(0)), VARPTR(blmask(0))) = TRUE THEN
   armorused = 0        'Armor
   poff = bullets(i)    'The damageof our bullet

   IF unita(j) > 0 THEN poff = poff - INT(bullets(i) / 2): armorused = bullets(i)
   IF j = 0 THEN PlayerHealth = PlayerHealth - poff: PlayerArmor = PlayerArmor - armorused
   IF j > 0 THEN unith(j) = unith(j) - poff: unita(j) = unita(j) - armorused
   IF unita(j) < 0 THEN unita(j) = 0
   IF PlayerArmor < 0 THEN PlayerArmor = 0
  
   'When we kill a bot...
   prijs = 0
   IF unith(j) <= 0 THEN
   mapplayers(unitc(j)) = -1
    IF j > 0 THEN
     unitds(j) = unitds(j) + 1
     IF j <= 7 THEN
      message$ = "Teammate Down!"
      DQBplaySound 34, 7, 22050, 0
      messagecounter = 15
     END IF
    ELSE
     PlayerDeaths = PlayerDeaths + 1:
     message$ = "You are dead!"
     messagecounter = 15
    END IF

   IF j <= 7 AND idowner > 7 AND idowner < 15 THEN prijs = 300
   IF j > 7 AND idowner <= 7 AND idowner < 15 THEN prijs = 300
   IF j <= 7 AND idowner <= 7 THEN prijs = -1500
   IF j >= 7 AND j < 15 AND idowner >= 7 AND idowner < 15 THEN prijs = -1500
   IF j > 15 THEN prijs = -2000

    IF idowner = 0 THEN
     PlayerMoney = PlayerMoney + prijs
      IF prijs > 0 THEN
       PlayerKills = PlayerKills + 1
       message$ = "You killed an enemy!"
       messagecounter = 15
      ELSE
     PlayerKills = PlayerKills - 1
     message$ = "You killed a friend!"
     messagecounter = 15
    END IF

   END IF

   IF idowner > 0 THEN
    unitm(idowner) = unitm(idowner) + prijs
     IF prijs > 0 THEN
      unitki(idowner) = unitki(idowner) + 1
   ELSE
    unitki(idowner) = unitki(idowner) - 1
   END IF

  END IF

END IF
bulletd(i) = 0
notvalid:

IF j <= 7 AND j > 0 THEN
'Little chance but else the guy says I'm hit!
IF INT(RND * 100 < 25) THEN DQBplaySound 29 + INT(RND * 2), 14, 22050, 0
END IF

IF INT(RND * 100 < 50) THEN
'Ouch!
DQBsetVoiceVol 16, 63
DQBplaySound 31 + INT(RND * 3), 16, 22050, 0
END IF

END IF

END IF
volgend:

NEXT j


IF mapcol(bcell) <> 0 THEN
 bulletd(i) = 0
 a = 1
 IF bx < 0 OR bx > 320 OR by < 0 OR by > 200 THEN a = 0

 'DQBsetVoiceVol 6, INT(RND * a)
 IF a = 1 THEN DQBplaySound 17 + INT(RND * 3), 6, 22050, 0
END IF

'DQBbox 1, (bcellx - MX) * 24 + PMX, (bcelly - MY) * 24 + PMY, (bcellx - MX) * 24 + 24 + PMX, (bcelly - MY) * 24 + 24 + PMY, 15
IF bx >= 0 AND bx <= 320 AND by >= 0 AND by <= 200 THEN DQBpset 1, bx, by, bulleto(i)'was 13

END IF
NEXT i


END SUB

REM $DYNAMIC
SUB drawhud
s$ = "None"
IF PlayerWeapon = -1 THEN s$ = "Mini Gun"
IF PlayerWeapon = 0 THEN s$ = "MP5 Navy"
IF PlayerWeapon = 1 THEN s$ = "Sniper Gun"
IF PlayerWeapon = 2 THEN s$ = "ShotGun"
DQBprint 1, "$" + STR$(PlayerMoney), 270, 0, 229 + fade

DQBprint 1, CHR$(3) + STR$(PlayerHealth), 0, 192, 234
DQBprint 1, CHR$(5) + STR$(PlayerArmor), 50, 192, 234
DQBprint 1, CHR$(4) + STR$(PlayerAmmo), 110, 184, 234
DQBprint 1, STR$(PlayerMaxCardrige) + "/" + STR$(PlayerUsedCardrige), 100, 192, 234
DQBprint 1, "K" + STR$(PlayerKills), 150, 192, 234
DQBprint 1, "D" + STR$(PlayerDeaths), 200, 192, 234
DQBprint 1, "WEAPON:" + s$, 0, 184, 234
IF tijd < freezetime THEN DQBprint 1, "TIME:" + STR$(freezetime - tijd) + "sec.", 160, 184, 234
mins = INT(roundtime / 60)
secs = roundtime - (mins * 60)

IF tijd >= freezetime THEN DQBprint 1, "TIME:" + STR$(mins) + ":" + STR$(secs), 160, 184, 234

IF fadeway = 0 THEN fade = fade + 1
IF fadeway = 1 THEN fade = fade - 1
IF fade >= 10 THEN fadeway = 1
IF fade <= 0 THEN fadeway = 0
END SUB

SUB drawitems
FOR x = MX - 1 TO MX + 14
FOR y = MY - 1 TO MY + 9

cell = y * 64 + x
IF cell < 0 THEN cell = 0
IF cell > 4095 THEN cell = 4095
py = 0
t = mapitems(cell)
IF t > -1 THEN
r = INT(t / 12)    ' Rows
l = (t - (r * 12)) ' Lengths

px = l * 24 + l    ' Xpos
py = r * 24 + r    ' Ypos

DQBxPut 3, px, py, px + 23, py + 23, 1, (x - MX) * 24 + PMX, (y - MY) * 24 + PMY
IF blocked = FALSE THEN
blocked = DQBcollide(playerx, playery, VARSEG(plmask(0)), VARPTR(plmask(0)), (x - MX) * 24 + PMX, (y - MY) * 24 + PMY, VARSEG(plsprite(0)), VARPTR(plsprite(0)))
END IF

END IF

NEXT y
NEXT x
END SUB

SUB drawplayer
links = 0
IF DQBmouseX < playerx THEN PlayerFacing = 2: links = 1
IF DQBmouseX > playerx THEN PlayerFacing = 6: links = 2
IF DQBmouseY < playery THEN
PlayerFacing = 0
IF links = 1 THEN PlayerFacing = 1
IF links = 2 THEN PlayerFacing = 7
END IF
IF DQBmouseY > playery THEN
PlayerFacing = 4
IF links = 1 THEN PlayerFacing = 3
IF links = 2 THEN PlayerFacing = 5
END IF

mapplayers(playercell) = -1
IF PlayerMoney < 0 THEN PlayerMoney = 0
walk = 0
IF DQBkey(KEYUP) THEN
walk = 1
END IF

IF DQBmouseLB = TRUE AND PlayerWeapon >= -1 AND PlayerReload = FALSE THEN
PlayerWeaponCount = PlayerWeaponCount + 1
IF PlayerWeaponCount > PlayerWeaponDelay THEN
PlayerWeaponCount = 0

shoot = 1: walk = 0:
IF PlayerWeapon <= 1 THEN
PlayerUsedCardrige = PlayerUsedCardrige - 1
IF PlayerUsedCardrige < 0 THEN
IF INT(RND * 10) = 1 THEN
 DQBplaySound 12, 8, 22050, 0
 PlayerReload = TRUE
END IF
GOTO noshoot
END IF

END IF

IF PlayerWeapon > 1 THEN
PlayerUsedCardrige = PlayerUsedCardrige - 3
IF PlayerUsedCardrige < 0 THEN
IF INT(RND * 10) = 1 THEN
 DQBplaySound 12, 8, 22050, 0
 PlayerReload = TRUE
END IF
GOTO noshoot
END IF

END IF


IF PlayerWeapon = -1 THEN DQBplaySound 2, 11, 22050, 0
IF PlayerWeapon = 0 THEN DQBplaySound 3 + INT(RND * 2), 11, 22050, 0
IF PlayerWeapon = 1 THEN DQBplaySound 10, 11, 22050, 0
IF PlayerWeapon = 2 THEN DQBplaySound 5, 11, 22050, 0

'Ok shoots...
a = 2 + PlayerWeapon
IF PlayerWeapon = -1 THEN b = 25: Speed = 2 'Minigun (since it shoots slow it does great damage)
IF PlayerWeapon = 0 THEN b = 14: Speed = 4  ' Rifle (fast shooter  so this is respectable)
IF PlayerWeapon = 1 THEN b = 35: Speed = 4 'Sniper gun
IF PlayerWeapon = 2 THEN b = 17: Speed = 3 ' damage per bullet so around 21

mousex = DQBmouseX
mousey = DQBmouseY

addbullet playerx, playery, mousex, mousey, b, 0, Speed

IF PlayerWeapon = 2 THEN
addbullet playerx, playery, mousex, mousey, b, 0, Speed
addbullet playerx, playery, mousex, mousey, b, 0, Speed
END IF

END IF
noshoot:
IF PlayerUsedCardrige < 0 THEN
'Click sound
PlayerUsedCardrige = 0
DQBplaySound 11, 9, 22050, 0
END IF
END IF

IF DQBmouseRB = TRUE AND PlayerReload = FALSE AND PlayerUsedCardrige < PlayerMaxCardrige THEN
DQBplaySound 12, 8, 22050, 0
PlayerReload = TRUE
END IF


IF DQBinUse(8) = FALSE AND PlayerReload = TRUE THEN
'First we give the resting ammo 'back'
PlayerAmmo = PlayerAmmo + PlayerUsedCardrige    'Now we have all available bullets
cangive = PlayerAmmo - PlayerMaxCardrige
IF cangive >= 0 THEN
gives = PlayerMaxCardrige: PlayerAmmo = PlayerAmmo - PlayerMaxCardrige
ELSE
gives = PlayerMaxCardrige - (-cangive): PlayerAmmo = 0
END IF

PlayerUsedCardrige = gives

IF PlayerUsedCardrige > PlayerMaxCardrige THEN PlayerUsedCardrige = PlayerMaxCardrige
PlayerReload = FALSE
END IF


'Cell of player
cpx = INT(playerx / 24) + MX
cpy = INT(playery / 24) + MY
playercell = cpy * 64 + cpx
steptype = 0
IF maptile(playercell) = 2 THEN steptype = 1
IF maptile(playercell) = 31 THEN steptype = 2
IF maptile(playercell) = 32 THEN steptype = 2
nextcell = playercell

IF walk <> 0 AND tijd >= freezetime THEN
IF PlayerFacing = 0 THEN movey = -1
IF PlayerFacing = 1 THEN movey = -1: movex = -1
IF PlayerFacing = 2 THEN movex = -1
IF PlayerFacing = 3 THEN movey = 1: movex = -1
IF PlayerFacing = 4 THEN movey = 1
IF PlayerFacing = 5 THEN movey = 1: movex = 1
IF PlayerFacing = 6 THEN movex = 1
IF PlayerFacing = 7 THEN movex = 1: movey = -1

erbij = 0
PlayerStepCounter = PlayerStepCounter + 1: walkit = walkit + 1
IF DQBkey(56) THEN PlayerWalkDelay = 10: erbij = 2 ELSE PlayerWalkDelay = 0
IF PlayerStepCounter > (15 + PlayerWalkDelay) THEN

PlayerFrame = PlayerFrame + 1
'Play Step sounds
IF steptype = 1 THEN t2 = 7
IF steptype = 2 THEN t2 = 13
IF PlayerWalkDelay = 0 THEN DQBplaySound 7 + INT(RND * 3) + t2, 9, 22050, 0
PlayerStepCounter = 0
END IF

IF PMX < -23 THEN PMX = 0: MX = MX + 1
IF PMX > 23 THEN PMX = 0: MX = MX - 1
IF PMY < -23 THEN PMY = 0: MY = MY + 1
IF PMY > 23 THEN PMY = 0: MY = MY - 1

IF movex = 1 THEN nextcell = nextcell + 1
IF movex = -1 THEN nextcell = nextcell - 1
IF movey = 1 THEN nextcell = nextcell + 64
IF movey = -1 THEN nextcell = nextcell - 64

IF nextcell > 4095 THEN nextcell = 4095
IF nextcell < 0 THEN nextcell = 0

'Ok we move the player, but at the end we decide if it really moves!

'Ok now get the 4 coordinates of the next cell


IF maptile(nextcell) <= 2 THEN a = FALSE

IF blocked = FALSE AND walkit > (0 + erbij) THEN
walkit = 0
playerx = playerx + movex
playery = playery + movey
ELSEIF blocked = TRUE THEN
IF movex = -1 THEN corx = 1
IF movex = 1 THEN corx = -1
IF movey = 1 THEN cory = -1
IF movey = -1 THEN cory = 1
playerx = playerx + corx
playery = playery + cory
movex = 0
movey = 0
GOTO nomoremove
END IF

IF playerx < 0 THEN playerx = 0
IF playery < 0 THEN playery = 0
IF playerx > 308 THEN playerx = 308
IF playery > 188 THEN playery = 188


IF playerx > 165 AND MX < 50 THEN playerx = 165: PMX = PMX - 1
IF playerx < 155 AND MX > 0 THEN playerx = 155: PMX = PMX + 1
IF playery > 105 AND MY < MaxMY THEN playery = 105: PMY = PMY - 1
IF playery < 95 AND MY > 0 THEN playery = 95: PMY = PMY + 1

END IF
nomoremove:
IF movex = 0 AND movey = 0 THEN PlayerFrame = 0
IF shoot = 1 THEN PlayerFrame = 5 + INT(RND * 2)
IF PlayerFrame > 4 AND shoot = 0 THEN PlayerFrame = 1


fy = PlayerFacing * 12 + PlayerFacing
fx = PlayerFrame * 8 + PlayerFrame
DQBxPut 4, fx, fy, fx + 7, fy + 11, 1, playerx, playery


mapplayers(playercell) = 0
'DQBprint 1, STR$(Playercell), 100, 90, 15
'DQBprint 1, STR$(mapways(Playercell)), 160, 90, 14

END SUB

REM $STATIC
DEFSNG A-Z
SUB drawwindow (breedte)
beginnen = INT(160 - (breedte / 2))
DQBboxf 1, beginnen, 14, beginnen + breedte, 185, 174
FOR dil = -2 TO 172 STEP 2
DQBline 1, beginnen, 14 + dil + 2, beginnen + breedte, 14 + dil + 2, 175
NEXT dil
DQBbox 1, beginnen - 1, 13, beginnen + breedte + 1, 185 + 2, 170
DQBbox 1, beginnen - 2, 12, beginnen + breedte + 2, 185 + 3, 168
DQBbox 1, beginnen - 3, 11, beginnen + breedte + 3, 185 + 4, 169
dqbcopylayer 1, 0
END SUB

