;-----------------------------------------------------------
; PATROL.TXT
;-----------------------------------------------------------
; This tank will pick an (x,y) location, and move to it, as
; quickly as possable. Once there, it will show a little
; fireworks display.
;-----------------------------------------------------------
; There are quicker ways to write this, this is just one of
; many available.
;
; The tank uses AUTOmatic tank movement (which is fast, but
; not accurate) to get in the general area of the destination.
; Once there it uses the MOVE command for exact placement, if
; that is possible (IE: the destination is not covered
; by an unmovable object like a tree).
; If the tank gets caught on an unmovable object, it will pick
; a new location.
;-----------------------------------------------------------
; This program shows some advanced tank programming stratagies, so
; examine it closely, and modify it freely!
;               - Sandy Walsh, May 16/89
;-----------------------------------------------------------


;---------------------
; Variable definitions
;---------------------

make x                  ; This is the destination X location
make y                  ; and the destination Y location
make d                  ; Direction required to reach X,Y
make look               ; Response from the scan command. Used to check
                        ; if there is an enemy tank in the direction the
                        ; tank is currently looking.
make bump               ; Counter for how many times we hit an unmoveable object
make count              ; Number of move-loops count
make scrap              ; Junk variable for simple loops, etc

;---------------------
; Start of program
;---------------------

select 3                ; Select the Cannon as a weapon

:align                  ; Make the Tank and the Turrent face the same direction
aim ctdir
jneq ctdir,cgdir,:align
lock 1                  ; Now that they face the same way, lock them together

:loop           ;       ->>>- Main Loop ->>>+
                ;                           |
rand x,100      ;        Pick a Destination |
rand y,70       ;                           V
                ;                           V
call :getthere  ;        Call subroutine    |
                ;                           |
call :display   ;        WE'RE HERE!        |
                ;        Make a display!!   |
goto :loop      ;       --------<<<---------+

;---------------------
; End of Main program
;---------------------

;-------------------------------------
:getthere
;-------------------------------------
; Move Tank currently at (whereX, whereY) to (X,Y)
;-------------------------------------
set count,0               ; Reset the loop counter

:loop2
auto 0                    ; Start the tank moving automatically
jeq x,wherex,:check       ; Does the X axis match up?? If so go to :check
:nope
auto 1

conv x,y,d                ; Convert the destination to a direction from 1-8

jeq ctdir,d,:skip1        ; If we don't have to change from our current direction
                          ; Go to :skip1
auto 0
face d

add count,1               ; Increase the loop counter by 1
jgt count,8,:FineMove     ; If we have to turn more than 3 times (Which we shouldn't!)
                          ; Go to the "Fine Movement" routine
:skip1
auto 0
jeq blockage,1,:Check_Object  ; If we are blocked by an unmoveable object. Check it out
auto 1
set bump,0                    ; Otherwise, Reset Bump to zero

goto :loop2               ; Keep moving towards destination

;---------------------------------------------------------------

:check
jneq y,wherey,:nope       ; Does the y-axis also match? If not, go back to loop
                          ; If yes ...
:goback
return                    ; Go back to calling function

:Check_Object
add bump,1                ; increase the number of times we hit an
                          ; unmovable object in a row.

jlt bump,10,:loop2        ; If not 10 times, go back to movement loop
goto :goback              ; If we hit the object 10 times, go somewhere else.

;----------------------------------------------------------------
:FineMove
;-------------------------------------
; Move Tank currently at (whereX, whereY) to (X,Y), using fine movement
;-------------------------------------

auto 0                    ; Automatic tank movement is ALWAYS OFF in fine move

:loop3
jeq x,wherex,:check1      ; Does the X axis match up?? If so go to :check1
:nope1

conv x,y,d                ; Convert the destination to a direction from 1-8
face d                    ; Desired direction (d)
move                      ; Move a single step in the desired direction.

jeq blockage,1,:Check_Object1    ; If we are blocked by an unmoveable object. Check it out
set bump,0                       ; Otherwise, Reset Bump to zero

goto :loop3               ; Keep moving towards destination

;---------------------------------------------------------------

:check1
jneq y,wherey,:nope1      ; Does the y-axis also match? If not, go back to loop
                          ; If yes ...
goto :goback

:Check_Object1
add bump,1                ; increase the number of times we hit an
                          ; unmovable object in a row.

jlt bump,10,:loop3        ; If not 10 times, go back to movement loop
goto :goback              ; If we hit the object 10 times, go somewhere else.

;----------------------------------------------
:Display
;----------------------------------------------
; This routine simply spins the tank around and shoots all over the place.
; It is used to show that we have reached our destination.
; It could, however, do something useful like:
;       - Radio other tanks of the location
;       - Sit for a little while, and track other tanks
;       - Plant mines
;       - Or all of the above!!
;----------------------------------------------

set scrap,0

:dispLoop

fire            ; Fire cannon
right           ; Spin clockwise
add scrap,1     ; Increment counter

jlt scrap,8,:dispLoop   ; Turn 8 times
return                  ; Go back and do it again!

;-----------------------
; End of PATROL.TXT
;-----------------------