******************************************************
******************************************************
*
*
*    ROBOTRON     8-JUNE-83
*                16-JUNE-83
*                18-JULY-83
*                24-AUGUST-83            3:30
*
*
*         RMAIN.S
*
******************************************************


**********
*
*         STUFF FROM LINKER
*
          EXTRN   STAMPHGH,CRETODST,DIRTOSTE
          EXTRN   STAMPL,STAMPS,PALNWID,STAMPPWD
*

******************************************
******************************************
*                                        *
*    ROBOTRON RAM AND MAIN ROUTINES      *
*                                        *
******************************************


******************************************
*
*         ZERO PAGE LOCATIONS
*
******************************************
*

TEMP0     EQU     $A0                    ;TEMPS
TEMP1     EQU     $A1
TEMP2     EQU     $A2
TEMP3     EQU     $A3
TEMP4     EQU     $A4
TEMP5     EQU     $A5
TEMP6     EQU     $A6
TEMP7     EQU     $A7
TEMP8     EQU     $A8
TEMP9     EQU     $A9
TEMP10    EQU     $AA
TEMP11    EQU     $AB
TEMP12    EQU     $AC
TEMP13    EQU     $AD
TEMP14    EQU     $AE
TEMP15    EQU     $AF
*
*
*
TEMP16    EQU     $B0
TEMP17    EQU     $B1
TEMP18    EQU     $B2
TEMP19    EQU     $B3
TEMP20    EQU     $B4
TEMP21    EQU     $B5
TEMP22    EQU     $B6


FRMCNT    EQU     $B7                    ;FRAME COUNT

TEMPX     EQU     $B8                    ;MISCELLANEOUS TEMPS
TEMPY     EQU     $B9

TADDRL    EQU     $BA                    ;TEMPS USED TO INDEX THRU TABLES
TADDRH    EQU     $BB
TADDR1L   EQU     $BC
TADDR1H   EQU     $BD

XORIG     EQU     $BE            ;ORIGINAL XPOS OF AN OBJECT (BEFORE A MOVE)
YORIG     EQU     $BF
XINTEND   EQU     $C0    ;INTENDED XPOS OF AN OBJECT (WHERE IT WANTS TO MOVE)
YINTEND   EQU     $C1
XXINTEND  EQU     $BE
YXINTEND  EQU     $BF
*
*
TEMPZON   EQU     $C2                    ;TEMP VARIABLE USED TO HOLD A ZONE #

RANDOMX   EQU     $C3                    ;RANDOM VALID X AND Y POSITIONS
RANDOMY   EQU     $C4                    ;GENERATED BY RANDXY AND RANDXYBX

RNDM      EQU     $C5                    ;FOR RANDOM NUMBER GENERATOR
*         ALSO NEEDS $C6


*         OTHER MISCELLANEOUS VARIABLES
TEMPCOL   EQU     $C7                    ;TEMP VARIABLE FOR COLOR CYCLING
MCCTMR    EQU     $C8                    ;TIMER FOR MC COLLISIONS

CRELEFT   EQU     $C9                    ;COUNT OF CREATURES LEFT ALIVE
                  ; INC WHEN ONE BORN, DEC WHEN ONE DIES
*
*         VARIABLES FOR SOUND GENERATION
SOUNDZP   EQU     $CA                    ;2 BYTES - ZERO PAGE FOR SOUND
          ;ALSO USES $CB

*         OBJECT SPEED, STEP AND OTHER LOCATIONS
*           SPEEDS, STEPS, ETC. WHICH CHANGE OVER TIME OR FROM
*           RACK TO RACK ARE HERE AS RAM LOCATIONS
*                 CONSTANTS ARE IN ASSEMBLY CONSTANTS SECTION
*
GSPEED    EQU     $CC                    ;# OF FRAMES BETWEEN GRUNT MOVES
FSPEED    EQU     $CD                    ;# FRAMES BETWEEN FAMILY MOVES
HSPEED    EQU     $CE                    ;# OF FRAMES BETWEEN HULK MOVES
SQBTIME   EQU     $CF                    ;BASE TIME UNTIL FIRST BIRTH IN A WAVE
QSPEED    EQU     $D0                    ;# OF FRAMES BETWEEN QUARK MOVES
TSPEED    EQU     $D1                    ;# OF FRAMES BETWEEN TANK MOVES
BSPEED    EQU     $D2                    ;# OF FRAMES BETWEEN BRAIN MOVES
BSTIME    EQU     $D3                    ;BASE # OF FRAMES BETWEEN BRAIN SHOTS

*         OTHER GLOBAL VARIABLES:
FAMLEVEL  EQU     $D4                    ;SCORE LEVEL FOR FAMILY PICKING UP
PLAYER    EQU     $D5                    ;0 OR 1 FOR PLAYER 1 OR 2
WAVENUM   EQU     $D6                    ;THE NUMBER WAVE WE ARE CURRENTLY ON

*      THESE VARIABLES MUST BE SEQUENTIAL IN RAM
*           THERE ARE STARTNUM OF THESE LOCATIONS - PRESERVE CURRENT STATUS
*      USED AT WAVE START AND BETWEEN TURNS
*      THESE MAY BE WIPED OUT BY WAVE START ROUTINES - DO NOT USE DURING WAVE
*      THESE SHOULD BE RE-COMPUTED FROM THE OBJECT DATA TABLES AT THE END
*                 OF A TURN OR WAVE
*                              THE 14 OBJECT TYPES:
GNUM      EQU     $D7                    ;# GRUNTS
MONUM     EQU     $D8                    ;# MOMMIES
DNUM      EQU     $D9                    ;# DADDIES
MINUM     EQU     $DA                    ;# MIKEYS
HNUM      EQU     $DB                    ;# HULKS
SNUM      EQU     $DC                    ;# SPHEROIDS
QNUM      EQU     $DD                    ;# QUARKS
ENUM      EQU     $DE                    ;# ENFORCERS (NULL, AT WAVE START)
TNUM      EQU     $DF                    ;# TANKS
BNUM      EQU     $E0                    ;# BRAINS
PNUM      EQU     $E1                    ;# PROGS (NULL, AT WAVE START)
EMNUM     EQU     $E2                    ;# ENFORCER MISSILES (NULL, AT WAVEST)
CMNUM     EQU     $E3                    ;# CRUISE MISSILES (NULL, AT WAVE ST)
TMNUM     EQU     $E4                    ;# TANK MISSILES (NULL, AT WAVE START)
*                              OTHER WAVE-DEPENDANT NUMBERS:
ELECNUM   EQU     $E5                    ;# ELECTRODES


*         POINTERS TO SEGMENTS WITHIN OBJECT DATA TABLES
*         THESE CONTAIN INDICES TO THE STARTS OF VARIOUS SECTIONS
                     ;GRUNTS START AT FIRST BYTE, INDEX=1 (NOTE: NOT 0)
FPTR      EQU     $E6                    ;START OF FAMILY (NO SEPERATE MO,D,MI)
HPTR      EQU     $E7                    ;         HULKS
SPTR      EQU     $E8                    ;         SPHEROIDS
QPTR      EQU     $E9                    ;         QUARKS
EPTR      EQU     $EA                    ;         ENFORCERS
TPTR      EQU     $EB                    ;         TANKS
BPTR      EQU     $EC                    ;         BRAINS
PPTR      EQU     $ED                    ;         PROGS
MPTR      EQU     $EE                    ;         MISSILES (NO SEPERATE E,C,T)




******************************************
*
*         OTHER LOCATIONS
*
******************************************
*

*         NOT-ZERO-PAGE RAM LOCATIONS

*
*         SCORE VARIABLES IN BCD FORMAT
*                 USE START OF FREE RAM NOT SHADOWED ANYWHERE
SCORE1L   EQU     $1000                    ;PLAYER 1 LOWER 2 DIGITS
SCORE1M   EQU     $1001                    ;         MIDDLE 2 DIGITS
SCORE1H   EQU     $1002                    ;         HIGH 2 DIGITS
SCORE1V   EQU     $1003                    ;         VERY HIGH 2 DIGITS

SCORE2L   EQU     $1004                    ;PLAYER 2 LOWER 2 DIGITS
SCORE2M   EQU     $1005                    ;         MIDDLE 2 DIGITS
SCORE2H   EQU     $1006                    ;         HIGH 2 DIGITS
SCORE2V   EQU     $1007                    ;         VERY HIGH 2 DIGITS

*

*         SOUND ROUTINE VARIABLES
TUNON     EQU     $1300                  ;2 BYTES - WHETHER TUNE IS ACTIVE
TUNINDEX  EQU     $1302                  ;2 BYTES - WHAT TUNE IS PLAYING
TUNPRIOR  EQU     $1304                  ;2 BYTES - WHAT THE PRIORITY OF TUNE IS
TUNBASE   EQU     $1306                  ;2 BYTES - BASE ADDRESS OF TUNE DATA
TUNBASE1  EQU     $1308                  ;2 BYTES - HI BYTE OF BASE ADDRESS
FREQOFF   EQU     $130A                  ;2 BYTES - OFFSET INTO DATA FOR FREQ'S
CTLOFF    EQU     $130C                  ;2 BYTES - OFFSET INTO DATA FOR CTL'S
VOLOFF    EQU     $130E                  ;2 BYTES - OFFSET INTO DATA FOR VOL'S
FREQTIME  EQU     $1310                  ;2 BYTES - NUMBER FRAMES TILL NEXT FREQ
CTLTIME   EQU     $1312                  ;2 BYTES - NUMBER FRAMES TILL NEXT CTL
VOLTIME   EQU     $1314                  ;2 BYTES - NUMBER FRAMES TILL NEXT VOL
TUNNUM    EQU     $1316                  ;WHAT TUNE YOU WANT - PARAMETER
TUNTEMP0  EQU     $1317                  ;TEMP VALUE FOR TUNE DRIVER
TUNTEMP1  EQU     $1318                  ;TEMP VALUE FOR TUNE DRIVER

********

*         OBJECT DATA TABLES
*             THESE ARE PARALLEL TABLES WHICH HAVE VARIOUS STATE VARIABLES
*         FOR EACH OBJECT.  ONE INDEX (OBJECT INDEX) IS USED TO INDEX INTO
*         ANY ONE OF THESE TABLES TO FIND INFO FOR A SPECIFIC OBJECT.
*         THE OBJECT INDEX IS A ONE-BYTE QUANTITY FROM 1 TO MAXOBJS-1
*         OBJECT 0 IS THE MUTANT CLONE,
*                 OBJECTS MAXOBJS-4 THROUGH MAXOBJS-1 ARE THE 4 MC SHOTS.

MAXOBJS   EQU     80
NUMTBLS   EQU     16                     ;NUMBER OF OBJECT DATA TABLES

          ORG     $1319
XTBL      DS      MAXOBJS                ;X POSITION
YTBL      DS      MAXOBJS                ;Y POSITION
MTTBL     DS      MAXOBJS                ;MOVE TIMER - # FRAMES TIL NEXT MOVE
DXTBL     DS      MAXOBJS                ;X-COMP OF CURRENT DIR, SPEED MOVING
DYTBL     DS      MAXOBJS                ;Y-COMP OF CURRENT DIR, SPEED MOVING
DTTBL     DS      MAXOBJS                ;DIR TIMER - # MOVES TIL DIR CHANGE
SATBL     DS      MAXOBJS                ;STEP IN ANIMATION + TYPE DATA
MISCTBL   DS      MAXOBJS                ;MISCELLANEOUS USE
DLPHTBL   DS      MAXOBJS                ;HIGH BYTE OF ABSOLUTE ADDR OF DL ENTRY
DLPLTBL   DS      MAXOBJS                ;LOW BYTE OF ABSOLUTE ADDR OF DL ENTRY
DL2PTBL   DS      MAXOBJS                ;DIFFERENCE BETWEEN 2 DL ENTRIES
CRTBL     DS      MAXOBJS                ;CREATURE TYPE
XEXTBL    DS      MAXOBJS                ;X-POSITION OF HORIZONTAL EXTENT
YEXTBL    DS      MAXOBJS                ;Y-POSITION OF VERTICAL EXTENT
STTBL     DS      MAXOBJS                ;STATUS TABLE
ZONTBL    DS      MAXOBJS                ;ZONE - USED BY THE UNLOADER

*         MUTANT CLONE STATISTICS:       ZEROTH ENTRIES IN OBJECT DATA TABLES
MCXPOS    EQU     XTBL                   ;X POSITION
MCYPOS    EQU     YTBL                   ;Y POSITION
MCXEX     EQU     XEXTBL                 ;X EXTENT (ABSOLUTE POSITION)
MCYEX     EQU     YEXTBL                 ;Y EXTENT (ABSOLUTE POSITION)
MCMTMR    EQU     MTTBL                  ;MC MOVE TIMER - # FRAMES TIL MOVE
MCSTMR    EQU     MISCTBL                ;MC SHOT TIMER-MC CAN FIRE IF NEGATIVE
MCDIR     EQU     DXTBL                  ;CURRENT DIRECTION OF MC
MCSA      EQU     SATBL                  ;CURRENT MC STEP IN ANIMATION

*         TABLE OF THE 4 MC SHOTS:    **** SHOT TABLES ****
*                 THESE ARE PARALLEL DATA TABLES, INDEX INTO THEM WITH THE
*            SHOT NUMBER, 0 TO 3.
*         THESE TABLES USE THE LAST 4 ENTRIES IN THE OBJECT DATA TABLES
SDIRTBL   EQU     DXTBL+MAXOBJS-4        ;SHOT DIR (4-BIT), 0 = NULL
SXTBL     EQU     XTBL+MAXOBJS-4         ;X POS
SYTBL     EQU     YTBL+MAXOBJS-4         ;Y POS
SXEXTBL   EQU     XEXTBL+MAXOBJS-4       ;X EXTENT
SYEXTBL   EQU     YEXTBL+MAXOBJS-4       ;Y EXTENT
SSATBL    EQU     SATBL+MAXOBJS-4        ;ANIMATION STEP - ALWAYS 0
SSTTBL    EQU     STTBL+MAXOBJS-4        ;STATUS
SCRTBL    EQU     CRTBL+MAXOBJS-4        ;CREATURE TYPE - ALWAYS #MCSCODE

*         OBJECT DATA TABLES EXTEND UP TO $1319+NUMTBLS*MAXOBJS



*
*
ZONOBJC   EQU     $18A4                   ;ZONE OBJECT COUNTS
*
*
ZONOBJL   EQU     $18B0                   ;ZONE OBJECT COUNT (TO 1A00)
*
*
*
*   DISPLAY LIST
*
*
*
DL        EQU     $1A00                  ;MARIA DISPLAY LIST

*
*THE DEFINITIONS OF DLZONE0 ETC. ARE FOR THE MARIA 1 KERNEL'S CONVENIENCE
*

DLZONE0   EQU     DL+$80*0
DLZONE1   EQU     DL+$80*1
DLZONE2   EQU     DL+$80*2
DLZONE3   EQU     DL+$80*3
DLZONE4   EQU     DL+$80*4
DLZONE5   EQU     DL+$80*5
DLZONE6   EQU     DL+$80*6
DLZONE7   EQU     DL+$80*7
DLZONE8   EQU     DL+$80*8
DLZONE9   EQU     DL+$80*9
DLZONE10  EQU     DL+$80*10
DLZONE11  EQU     DL+$80*11
DLZONE12  EQU     DL+$80*12



******************************************
*
*         ASSEMBLY CONSTANTS
*
******************************************
*

*         CONSTANTS DEALING WITH SCREEN BOUNDARIES
MINX      EQU     02                     ;MINIMUM X VALUE TO APPEAR ON SCREEN
MAXX      EQU     159-3         ;MAX X ON SCREEN - FOR UPPER LEFT OF STAMP
MINY      EQU     18                     ;MIN Y ON SCREEN
MAXY      EQU     191-3         ;MAX Y ON SCREEN FOR UPPER LEFT OF STAMP
SCENTERX  EQU     80                     ;X SCREEN CENTER
SCENTERY  EQU     96                     ;Y SCREEN CENTER
SBOXMAXX  EQU     80+30
SBOXMINX  EQU     80-30                  ;EDGES OF BOX IN CENTER OF SCREEN
SBOXMAXY  EQU     96+25                  ;INSIDE WHICH OBJECTS CAN'T APPEAR
SBOXMINY  EQU     96-25

MCWID     EQU     $05                    ;MC WIDTH
MCHEIGHT  EQU     $0B                    ;MC HEIGHT

MCXINIT   EQU     SCENTERX-MCWID/2       ;STARTING MC X POSITION
MCYINIT   EQU     SCENTERY-MCHEIGHT/2    ;STARTING MC Y POSITION

MASKL     EQU     00001111B              ;SAVE LOWER HALF-BYTE
MASKH     EQU     11110000B              ;SAVE UPPER HALF-BYTE
MASK1     EQU     00000001B              ;SAVE LOWEST BIT
MASK2     EQU     00000011B              ;SAVE LOWER 2 BITS
MASK3     EQU     00000111B              ;SAVE LOWER 3 BITS
MASK5     EQU     00011111B              ;SAVE LOWER 5 BITS


MCSDELAY  EQU     $04                    ;# FRAMES BETWEEN MC SHOTS
SHOTSTX   EQU     $07                    ;MC SHOT STEP IN X DIRECTION
SHOTSTY   EQU     $0E                    ;MC SHOT STEP IN Y DIRECTION
SHOTWID   EQU     $04                    ;MC SHOT WIDTH (EXCEPT VERTICAL SHOTS)
SHOTHT    EQU     $08                    ;MC SHOT HEIGHT (EXCEPT HORIZONTAL)

MAXINZON  EQU     20                     ;MAXIMUM NUMBER OF OBJECTS IN A ZONE


*         OBJECT CODES:  FOUND IN CRTBL
NULLCODE  EQU     $0
GCODE     EQU     $1
MOCODE    EQU     $2
DCODE     EQU     $3
MICODE    EQU     $4
HCODE     EQU     $5
SCODE     EQU     $6
QCODE     EQU     $7
ECODE     EQU     $8
TCODE     EQU     $9
BCODE     EQU     $A
PCODE     EQU     $B
EMCODE    EQU     $C
CMCODE    EQU     $D
TMCODE    EQU     $E
MCSCODE   EQU     $F
*         NOTE THAT THERE ARE SPECIAL CODES WHICH WAY BE FOUND IN CRTBL:
*                 $00:  NULL OBJECT
*                 $FF:  END OF OBJECT DATA TABLES


*         NOMINAL OBJECT DIMENSIONS  - USE TO COMPUTE EXTENTS WHEN SETTING UP
GWID      EQU     7
GHEIGHT   EQU     12
FWID      EQU     4
FHEIGHT   EQU     11
HWID      EQU     7
HHEIGHT   EQU     15
SQWID     EQU     9
SQHEIGHT  EQU     9
EWID      EQU     8
EHEIGHT   EQU     8
TWID      EQU     10
THEIGHT   EQU     10
BWID      EQU     9
BHEIGHT   EQU     9

*
*         OBJECT SPEED, STEP AND OTHER CONSTANTS
*           SPEEDS, STEPS, ETC. WHICH DO NOT CHANGE OVER TIME OR FROM
*           RACK TO RACK ARE HERE AS ASSEMBLY CONSTANTS
*                 RAM VARIABLES ARE IN THE ZERO PAGE SECTION
*
GSTEPX    EQU     $2                     ;# OF PIXELS IN A HORIZONTAL GRUNT STEP
GSTEPY    EQU     $4                     ;# OF PIXELS IN A VERTICAL GRUNT STEP
SQSTEP1   EQU     1                      ;STARTING ANIMATION STEP FOR S + Q
SQBIRTHS  EQU     4                      ;STARTING NUMBER OF BIRTHS LEFT
MAXSSPD   EQU     $7    ;MAX SPH SPEED AT WAVE START - USED AS PARM TO RANDPM
TSTIME    EQU     8                      ;BASE # OF FRAMES BETWEEN TANK SHOTS


FDIEWAIT  EQU     60        ;# FRAMES TO KEEP UP A SKULL OR FAMILY SCORE

STARTNUM  EQU     15                     ;NUMBER OF BYTES TO GET FROM WAVETBL
                                             ; WHEN SETTING UP FOR A NEW WAVE

WSWAIT    EQU     $13       ;# OF FRAMES AT WAVE START BEFORE ACTION STARTS

STACK     EQU     $FF                    ;INITIAL VALUE FOR STACK POINTER



******************************************
******************************************
*                                        *
*         ROBOTRON CODE:                 *
*                                        *
*            MAIN ROUTINES               *
*                                        *
******************************************


******************************************
*
*         VECTORS
*
          ORG     $FFFC
          DB      L(INIT),H(INIT)
          DB      L(KERNEL),H(KERNEL)
*
          ORG     $C000                  ;START OF PROGRAM ROM

******************************************
*
*         INIT    --  FIRST ROUTINE IN CARTRIDGE - SETS UP SYSTEM
*
******************************************
*
INIT      SEI
          CLD
          LDX     #STACK
          TXS                            ;SET UP STACK POINTER
*         OTHER INITIALIZATIONS MIGHT GO HERE                          *********


*         DO TITLE PAGE, OPTION SELECT, ETC.                           *********

          JSR     MARINIT                ;SETUP SOME MARIA STUFF @@@@@@@@@@@@@@@
          JSR     CONV                   ;CONV STAMPS FOR SI MARIA@@@@@@@@@@@@@@

          JMP     GAMESTRT
*

******************************************
*
*         GAMESTRT   -- VERY FIRST ROUTINE IN A GAME, INITIALIZES VARIOUS THING
*
******************************************
*
GAMESTRT
          JSR     CLEARTUN               ;TURN OFF SOUNDS

          LDA     #$00
          STA     WAVENUM

          JSR     RESETSC                ;RESET SCORES

* INITIALIZE RANDOM NUMBER GENERATOR  -  THIS SHOULD BE DONE EVERY SO OFTEN
          LDA     FRMCNT                 ;PSEUDO-RANDOM AT THIS POINT
          EOR     RNDM+1                 ;RANDOM NUMBER REGISTERS
          STA     RNDM

          JSR     WAVESTRT               ;SET UP FOR START OF PLAY
          JSR     DISPINIT
          JSR     DISPLOAD

MAIN2     BIT     MSTAT                  ;INIT KERNEL
          BVS     MAIN2
          BRK                            ;ENTER KERNEL
          NOP
          CLI                            ;TURN ON INTERRUPTS

*         MAKE CREATURES APPEAR, MAYBE MOVE, BUT DON'T START NORMAL ACTION
          SEI                            ;NO INTERRUPTS
          LDX     #WSWAIT                ;LOOP FOR WSWAIT FRAMES
WSWLOOP   STX     TEMP16                 ;SAVE X
*         NOW DO SOME COLOR CYCLING OF VARIOUS PALETTES
          JSR     CHKOBJ

          LDX     TEMP16                 ;IT WAS SAVED HERE
          CPX     #$10
          BPL     WSWLOOP1
*         WE ARE LESS THAN $10 FRAMES TILL ACTION
          JSR     WAVESTMC
          TXA                            ;A HAS A NUMBER F TO 0
          SBC     #$10
          EOR     #$FF
          CLC
          ADC     #$01
          STA     P0C1
          STA     P0C2
          STA     P0C3

WSWLOOP1  JSR     DISPINIT
          JSR     DISPLOAD
          BRK
          NOP

          LDX     TEMP16                 ;RESTORE X
          DEX
          BPL     WSWLOOP

*         NOW START ACTION
          JSR     WAVESTMC               ;SET UP MC
          JSR     MARINIT                ;RESET PALETTES

          JMP     MAIN                   ;GO!
*

******************************************
*
*         MAIN    -- MASTER LOOP   -   LOOPS ONCE EACH FRAME DURING PLAY
*
******************************************
*
MAIN
          SEI                            ;DISABLE INTERRUPTS
*         MAYBE CHANGE PALETTE HERE FOR SOME COLOR CYCLING             *********
          JSR     PALINIT                ;REFRESH PALETTES

          LDA     TEMPCOL                ;USE TO HOLD MC COLOR 3
          CLC                            ; SINCE PALETTES ARE UNREADABLE
          ADC     #$01
          STA     TEMPCOL
          STA     P0C3

          LDA     TEMPCOL
          ASL     A
          AND     #MASKL
          ORA     #$30                   ;MAKE THIS INTO A RED
          STA     P5C3                   ;ELECTRODES/SKULL EYES/SCORE NUMBERS
          STA     P6C1                   ;BRAIN EYES AND PART OF HEAD

          LDA     TEMPCOL
          ASL     A
          EOR     #$FF
          AND     #MASKL
          ORA     #$90                   ;MAKE INTO A BLUE
          STA     P5C2                   ;PALETTE 5 COLOR CYCLING

          LDA     TEMPCOL
          ASL     A
          AND     #MASKL
          STA     P4C3                   ;CYCLE HULK ARMS


          JSR     MCSHOOT                ;MOVE MC SHOTS, CHECK FOR HITS
          JSR     MCMOV                  ;MOVE MAN, CHECK FOR COLLISIONS
          JSR     CHKOBJ                 ;CHECK EACH OBJECT, POSSIBLY ACT

*         CHECK FOR WAVE END
          LDA     CRELEFT
          BNE     KEEPGOIN
*         WAVE IS OVER
          JSR     WAVEEND                ;DO SOMETHING FANCY

          JMP     INIT                   ;FOR NOW, RESTART@@@@@@@@@@@@@

*

KEEPGOIN                                 ;WAVE CONTINUES INTO NEXT FRAME
*         UPDATE GLOBAL VARIABLES IF NECESSARY



*         FOR NOW:@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
          JSR     DISPINIT               ;RELOAD
          JSR     DISPLOAD
          CLI                            ;INTERRUPTS OK

          BRK                            ;GO TO KERNEL
          NOP

          JMP     MAIN
*


******************************************
*
*         MCSHOOT -- MOVE MC SHOTS, CHECK FOR ANY COLLISIONS
*                    ALSO ADD A NEW SHOT IN DIRECTION OF FIRE CONTROL
*                      IF LESS THAN 4 ARE OUT AND PLAYER IS FIRING
*              NOTE THAT SHOTS USE 4-BIT DIR CODE, WITH 0 IN SDIRTBL = NULL
*
******************************************
*
MCSHOOT   LDX     #$00                   ;INITIALIZE X TO START AT FIRST SHOT
                                 ; X POINTS TO SHOTS 0 THRU 3 IN THIS LOOP

          DEC     MCSTMR                 ;DECREMENT MC SHOT TIMER
          BPL     MCSLOOP                ;IF IT DOESN'T GO TO ZERO START LOOPING
          STX     MCSTMR                 ;IF HE IS READY TO SHOOT, KEEP
                                         ;  MCSTMR AT ZERO

MCSLOOP   TXA
          CMP     #$04                   ;DONE MOVING SHOTS?
          BMI     MCSL1
          JMP     MCSEND                 ;YES, IF Y IS PAST 4TH SHOT

MCSL1     LDA     SDIRTBL,X              ;LOAD DIRECTION OF THIS SHOT
          BNE     MCSGO                  ;IF DIR NOT 00, DON'T ADD ANOTHER SHOT

*         NOW THERE IS A NULL SHOT IN THE TABLE.  ADD ANOTHER IF MCSTMR IS ZERO
          LDA     MCSTMR
          BEQ     MCSL2
          JMP     MCSCONT                ;IF NOT ZERO, CAN'T ADD A SHOT

MCSL2     LDA     #MCSDELAY              ;ADDING A NEW SHOT SO RESET TIMER
          STA     MCSTMR

          JSR     CRESHOT  ;THIS SETS UP SHOT DATA IN TABLES IF NECESSARY
*         NOW GO ON AND MOVE THIS SHOT
          JMP     MCS1

*         NOW CHECK FOR COLLISIONS WITH CURRENT SHOT
MCSGO     JSR     CHKSHOT                ;ROUTINE USES X AND SHOT TABLES;;;;;;;

*         MUST MOVE THIS SHOT
MCS1      JSR     MOVESHOT  ;MOVE SHOT POS'S AND EXTENTS, REMOVE IF OFFSCREEN


*         NOW JSR TO A ROUTINE WHICH UPDATES THE SHOT IN DL AND ZONOBJC ********
*         X POINTS TO SHOT IN SHOT TABLES                               ********
*         NEW SHOT POSITIONS AND EXTENTS IN SHOT DATA TABLES           *********
*         IF SDIRTBL,X IF ZERO, AND SHOT HAS NO DL ENTRY, LEAVE IT ALONE.*******
*         IF SDIRTBL,X IS ZERO, AND SHOT HAS SPACE IN DL, REMOVE IT. ***********
*
*         SHOT DATA SHOULD BE UNCHANGED IN SHOT DATA TABLES

*         NOW ADVANCE POINTER INTO SHOT TABLES
MCSCONT   INX                            ;POINT TO NEXT SHOT
          JMP     MCSLOOP                ;ON TO NEXT SHOT

*         DONE WITH LOOPING THROUGH SHOTS 0 - 3
MCSEND
*         LOAD SHOTS INTO END OF OBJECT DATA TABLES   @@@@@@@@@@@@@@@@@@@@@@@
          LDX     #$03
MCSTEMP1
          LDA     #$01
          STA     SSTTBL,X
          LDA     SDIRTBL,X
          BNE     MCSTEMP2               ;BRANCH IF SHOT EXISTS
          STA     SSTTBL,X              ;STORE ZERO IN STTBL ENTRY
MCSTEMP2
          DEX
          BPL     MCSTEMP1

          RTS                            ;BACK TO MAIN LOOP
*
******************************************
*
*         SUBROUTINES USED BY MCSHOOT:
*
******************************************

*******************
*
*         CRESHOT -- ROUTINE TO CREATE (POSSIBLY) A NEW SHOT
*
*         GIVEN SHOT NUMBER IN X, PRESERVES X
*         SDIRTBL,X SHOULD BE 0 FOR THIS TO BE CALLED - A NULL SHOT DOES EXIST
*         IF A NEW SHOT IS CALLED FOR, SETS EACH SHOT TABLE,X
*         ENDS WITH RTS, AFTER SETTING UP SHOT TABLES FOR SHOT NUMBER X
*
CRESHOT
          LDA     SWCHA                  ;LOOK AT JOYSTICK TO FIND DIRECTION
          AND     #MASKL
          CMP     #$0F                   ;WILL BE 0F IF NOT FIRING
          BNE     CRESHOT1
          RTS                            ;LEAVE THIS SHOT NULL, CONTINUE
                  ; SHOT MOVE ROUTINE WILL NOT MOVE THIS SINCE IT IS NULL

CRESHOT1  STA     SDIRTBL,X
          LDA     CRETODST+$0F     ;BYPASS GETSTAMPS FOR SPEED. F IS THE TYPE
          CLC                      ;THAT LOAD JUST GOT THE TYPE POINTER
          ADC     SDIRTBL,X        ;ADD IN THE DIRECTION FOR POINTERS TO EXTENTS
          TAY
          LDA     MCXPOS
          CLC
          ADC     #MCWID/2               ;COMPUTE CENTER OF MC'S BODY
          STA     SXTBL,X                ;STORE SHOT XPOS = CENTER OF MC
          CLC
          ADC     STAMPPWD,Y             ;COMPUTE EXTENT BY GOING STAIGHT TO THE
          STA     SXEXTBL,X              ;ANIMATION TABLES

          LDA     MCYPOS
          CLC
          ADC     #MCHEIGHT/2            ;COMPUTE CENTER OF MC'S BODY
          STA     SYTBL,X                ;STORE SHOT YPOS = CENTER OF MC
          CLC
          ADC     STAMPHGH,Y             ;COMPUTE EXTENT - SHOULD BE IMPROVED***
          STA     SYEXTBL,X

           ; NOW SHOT EXISTS, AND HAS THE DIRECTION OF THE MC'S FIRING CONTROL
*         START MC SHOOTING SOUND
          LDA     #SMCS
          JSR     DOTUNE
                  ; NOW RETURN, AND THIS SHOT SHOULD THEN BE MOVED
          RTS

*******************
*
*         CHKSHOT -- CHECK FOR COLLISIONS VERSUS ONE SHOT
*                 SHOT NUMBER GIVEN IN X, USE SHOT DATA TABLES
*                 END WITH AN RTS, PRESERVE X
*                 IF NO COLLISION, DON'T CHANGE ANYTHING
*                 IF A COLLISION, REMOVE SHOT BY NULLING OUT SDIRTBL,X
*                   AND MODIFY OBJECT DATA TABLES TO SHOW CREATURE
*                   WHICH WAS HIT AS DYING
*
CHKSHOT
*         GET SHOT POSITION AND EXTENTS
          LDA     SXTBL,X                ;GET SHOT XPOS
          STA     TEMPX
          LDA     SXEXTBL,X              ;GET SHOT X EXTENT
          STA     TEMP4

          LDA     SYTBL,X                ;GET SHOT YPOS
          STA     TEMPY
          LDA     SYEXTBL,X              ;GET SHOT Y EXTENT
          STA     TEMP5


*         LOOP THROUGH OBJECTS TO CHECK FOR COLLISIONS WITH A SHOT
*         SEARCH THROUGH OBJECTS IN THE SHOT'S ZONE
* SET UP TEMP0 AND TEMP1 TO BE THE ADDRESS OF THE SHOT'S ZONE'S ENTRY IN ZONOBJC
*         (THE FIRST IF SHOT IS IN 2 ZONES).  THEN SET Y TO 27 (OR 55 IF SHOT
* IS IN 2 ZONES)  AND LOOP THRU ZONOBJC, DECREMENTING Y UNTIL IT GOES TO 0.

          LDY     #27                    ;SET UP Y FOR 1-ZONE CASE
*    COMPUTE SHOT'S FIRST ZONE, CHECK IF 1 OR 2 ZONES, SET UP Y

          LDA     SYTBL,X                ;LOAD SHOT Y POSITION
          LSR     A
          LSR     A
          LSR     A
          LSR     A                      ;GET ZONE # FROM Y POSITION
          STA     TEMPZON
*         CHECK IF IN 2 ZONES
          LDA     SYEXTBL,X              ;Y POSITION OF SHOT LOWER EDGE
          LSR     A
          LSR     A
          LSR     A
          LSR     A                      ;GET ZONE # OF SHOT'S LOWER EDGE
          CMP     TEMPZON                ;IS IT SAME AS SHOT TOP ZONE?
          BEQ     MCSC1                  ;SHOT IN ONLY 1 ZONE, LEAVE Y AS 31
          LDY     #55             ;LOAD Y WITH 63 - WE WILL INDEX THRU 2 ZONES
MCSC1             ;NOW Y IS SET UP
*         GET ABSOLUTE ADDRESS OF START OF SHOT'S ZONE IN ZONOBJC
*                 ZONE NUMBER IS IN TEMPZON
          STY     TEMP2                  ;PUT Y AWAY FOR NOW
          LDY     TEMPZON
          LDA     ZONOBJLH,Y             ;GET HIGH BYTE
          STA     TEMP1
          LDA     ZONOBJLL,Y             ;GET LOW BYTE OF ABS ADDRESS
          STA     TEMP0
          LDY     TEMP2                  ;RECOVER Y - IT IS INDEX THRU ZONOBJC

*         NOW WE HAVE THE ABSOLUTE ADDRESS OF THE ZONOBJC LISTING
*                 FOR THE SHOT'S (FIRST) ZONE IN TEMP0 AND TEMP1

*         SAVE X  -  IT CURRENTLY POINTS TO CURRENT SHOT IN SHOT TABLES
          STX     TEMP8

MCSCLOOP  LDA     (TEMP0),Y              ;GET OBJECT NUMBER FROM ZONOBJC
          BEQ     MCSCNEXT               ;IF 0, A NULL ENTRY
          TAX                            ;PUT OBJECT NUMBER IN X
          LDA     CRTBL,X                ;GET CREATURE CODE
          BEQ     MCSCNEXT               ;IF THIS IS 0, ALSO A NULL OBJECT
          CMP     #MCSCODE               ;IS IT AN MC SHOT?
          BEQ     MCSCNEXT               ;SHOT CAN'T COLLIDE WITH A SHOT
          LDA     STTBL,X
          AND     #MASKL
          BEQ     MCSCNEXT               ;DON'T COLLIDE WITH A DEAD OBJECT
          CMP     #$03                   ;CODE IN STTBL FOR 'DYING' OBJECT
          BEQ     MCSCNEXT               ;DON'T COLLIDE WITH DYING OBJECT


*    HERE CHECK FOR A COLLISION WITH OBJECT NUMBER X
*         MC SHOT X,Y ARE IN TEMPX AND TEMPY
*         MC SHOT X,Y EXTENTS ARE IN TEMP4 AND TEMP5
*         OBJECT X,Y ARE IN XTBL,X AND YTBL,X
*         OBJECT EXTENTS ARE IN XEXTBL,X AND YEXTBL,X
*         IF A HIT, FALL THRU TO THE JSR MCSHIT,
*                 OTHERWISE BRANCH TO MCSCNEXT
*         Y IS AN INDEX THRU ZONOBJC - PRESERVE IT

MCSCL1    LDA     TEMP4
          CMP     XTBL,X
          BCC     MCSCNEXT               ;BRANCH IF LESS THAN
          LDA     TEMPX
          CMP     XEXTBL,X
          BCS     MCSCNEXT               ;BRANCH IF > OR =

          LDA     TEMP5
          CMP     YTBL,X
          BCC     MCSCNEXT               ;BRANCH IF LESS THAN
          LDA     TEMPY
          CMP     YEXTBL,X
          BCS     MCSCNEXT               ;BRANCH IF > OR =


*         ****************** HIT! ******************
          JSR     MCSHIT                 ;AN OBJECT WAS HIT - POSSIBLY REMOVE
                                         ; IT AND/OR SHOT
                  ;SHOT # IN TEMP8, OBJECT INDEX IN X

*         BACK FROM MC SHOT HIT ROUTINE
          LDX     TEMP8                  ;X WAS SAVED HERE - CURRENT SHOT #
          LDA     SDIRTBL,X              ;GET DIR OF CURRENT SHOT
          BEQ     CHKSEND  ;DON'T BOTHER WITH THIS SHOT ANYMORE - IT IS GONE


*         WE ARE HERE IF THERE WAS NOT A DESTRUCTIVE SHOT COLLISION
MCSCNEXT
          LDX     TEMP8                  ;RESTORE X - IT IS SHOT NUMBER
          DEY                            ; ON TO NEXT ENTRY IN ZONOBJC
          BPL     MCSCLOOP               ;IF Y NON-NEGATIVE, KEEP GOING

CHKSEND   RTS     ;BACK TO MASTER MCSHOOT ROUTINE
*

*******************
*
*         MCSHIT  -  MC SHOT HIT SOMETHING
*         A COLLISION! REPLACE OBJECT WITH NULL OBJECT
*                    AND SHOT WITH A NULL SHOT IF A LEGITIMATE COLLISION.
*         THE SHOT CAN BE FOUND FROM TEMP8.  THE OBJECT THAT WAS HIT IS
*          INDEXED BY X.  DO AN STZ CRTBL,X TO ZERO THE OBJECT CODE.
*
*******************
*
MCSHIT
          LDA     CRTBL,X                ;GET OBJECT CODE THAT SHOT HIT
*         CHECK IF COLLISION WITH FAMILY. IF SO, PRETEND AS IF NO COLLISION
          CMP     #MOCODE                ;IS IT MOMMY?
          BEQ     MCSHITNO               ;SHOT GOES THROUGH HER
          CMP     #DCODE                 ;IS IT DADDY?
          BEQ     MCSHITNO               ;SHOT GOES THROUGH HIM
          CMP     #MICODE                ;IS IT MIKEY?
          BEQ     MCSHITNO               ;SHOT GOES THROUGH THE LITTLE TWERP

*         SOMETHING WAS REALLY SHOT...
*         FIRST REMOVE MC SHOT WHICH CAUSED THIS
          STY     TEMP9                  ;TEMP9 NOW HOLDS INDEX INTO ZONOBJC
          LDY     TEMP8                  ;SHOT NUMBER WAS SAVED HERE
          LDA     SDIRTBL,Y
          STA     TEMP0                  ;PUT DIR AWAY IN CASE A HULK IS HIT
          LDA     #$00
          STA     SDIRTBL,Y              ;ZERO THIS SHOT'S DIRECTION
                                         ;A NEW SHOT WILL SOON BE ADDED

*         UPDATE SCORE AND REMOVE OBJECT IF OBJECT IS DESTRUCTABLE
          LDA     CRTBL,X                ;GET CREATURE OBJECT CODE
          CMP     #HCODE                 ;IS IT A HULK?
          BNE     KILLOBJ                ;IF NOT, GET RID OF THE OBJECT

*         ITS A HULK                     ;HULK WAS SHOT
          LDA     TEMP0                  ;LOAD ORIGINAL DIR OF CURRENT SHOT
          STA     MISCTBL,X              ;SET HULK'S DIR-TO-JUMP TO SHOT DIR
          LDA     #$00
          STA     MTTBL,X                ;FORCE HULK TO MOVE NEXT FRAME
          JMP     MCSHIT1                ;ON TO NEXT SHOT

*         NOW DEFINITELY UPDATE SCORE AND ELIMINATE OBJECT
KILLOBJ                                  ;OBJECT CODE IS IN A
          TAY                            ;Y NOW HAS OBJECT CODE
          CLC
          SED
          LDA     SCORETBL+1,Y           ;GET LOW 2 DIGITS OF OBJECT'S VALUE
          ADC     SCORE1L
          STA     SCORE1L
          LDA     SCORETBL,Y             ;HIGH 2 DIGITS OF OBJECT'S VALUE
          ADC     SCORE1M
          STA     SCORE1M
******************
* THIS ROUTINE SHOULD BE IMPROVED TO COMPLETELY UPDATE ALL SCORE DIGITS*********
*  AND TO ADD TO THE SCORE OF THE PLAYER (1 OR 2) WHO IS PLAYING       *********
******************
          CLD

*

*         NOW REMOVE OBJECT, OBJECT INDEX TO REMOVE IS IN X
*         SET BIT 1 OF STTBL,X, WHICH MEANS OBJECT IS DYING
          LDA     #$02                   ;BIT 1 IS ON
          ORA     STTBL,X
          STA     STTBL,X                ;SAVE NEW STATUS
*         SET OBJECT TO MOVE NEXT FRAME SO IT CAN START DYING
          LDA     #$00
          STA     MTTBL,X                ;ZERO # OF FRAMES UNTIL NEXT MOVE

*         HIT ROUTINE IS FINISHED.  WE MAY HAVE ZEROED THE CURRENT SHOT DIR

MCSHIT1   LDY     TEMP9                  ;RESTORE ZONOBJC TO Y
                  ; CODE AFTER THE JSR TO MCSHIT WILL RESTORE X AS SHOT INDEX

MCSHITNO  RTS
*

*******************
*
*         MOVESHOT  - MOVE AN MC SHOT
*                 GIVEN SHOT NUMBER IN X
*                 THIS CHANGES THE SHOT DATA IN THE SHOT TABLES
*                 STORE NEW X,Y POSITIONS, THEN COMPUTE AND STORE EXTENTS
*                 WILL NULL OUT DIR CODE IF A BOUNDARY IS ENCOUNTERED
*
*******************
*
MOVESHOT
          LDY     SDIRTBL,X              ;X HAS DIRECTION CODE
          BNE     MOVES0                 ;MOVE THIS SHOT; IT IS ALIVE
          RTS                            ;DON'T MOVE THIS SHOT; IT IS NULL
MOVES0    LDA     XDIRTBL4,Y             ;A HAS 1, 0, OR FF FOR X CHANGE
          BEQ     MCSY                   ;DON'T CHANGE X POSITION
          BMI     MCSXMI                 ;BRANCH ON NEGATIVE DELTA X
          LDA     SXTBL,X                ;LOAD SHOT X POSITION
          CLC
          ADC     #SHOTSTX               ;INCREMENT X POS BY X SHOTSTEP
          STA     SXTBL,X
          CMP     #MAXX-SHOTWID
          BCS     SHOTEND                ;IF SHOT IS OUT OF BOUNDS DESTROY IT
          LDA     SXEXTBL,X              ;GET  THE EXTENT
          ADC     #SHOTSTX               ;MOVE THE OTHER EDGE
          STA     SXEXTBL,X
          JMP     MCSY
MCSXMI    LDA     SXTBL,X                ;LOAD SHOT X POSITION
          SEC
          SBC     #SHOTSTX               ;DECREMENT X POS BY X SHOTSTEP
          STA     SXTBL,X                ;STORE NEW POS IN TABLES
          CLC
          ADC     #$10                   ;ADD FOR BORDER CHECKING
          CMP     #MINX+$10
          BCC     SHOTEND                ;IF SHOT IS OUT OF BOUNDS DESTROY IT
          LDA     SXEXTBL,X              ;GET  THE EXTENT
          SBC     #SHOTSTX               ;MOVE THE OTHER EDGE
          STA     SXEXTBL,X

*         NOW HANDLE Y CHANGE
MCSY      LDA     YDIRTBL4,Y             ;GET 0, POSITIVE OR NEG # FOR DELTA Y
          BEQ     MOVESD                 ;NO Y CHANGE, GO ON AND DO EXTENTS
          BMI     MCSYMI                 ;BRANCH IF NEGATIVE DELTA Y
          LDA     SYTBL,X                ;GET SHOT Y POSITION
          CLC
          ADC     #SHOTSTY               ;INCREMENT Y POS BY Y SHOTSTEP
          STA     SYTBL,X
          CMP     #MAXY-SHOTHT
          BCS     SHOTEND                ;IF SHOT IS OUT OF BOUNDS DESTROY IT
          LDA     SYEXTBL,X              ;GET  THE EXTENT
          ADC     #SHOTSTY               ;MOVE THE OTHER EDGE
          STA     SYEXTBL,X
          JMP     MOVESD
MCSYMI    LDA     SYTBL,X                ;LOAD SHOT Y POSITION
          SEC
          SBC     #SHOTSTY               ;DECREMENT Y POS BY Y SHOTSTEP
          STA     SYTBL,X                ;SAVE NEW POS IN TABLES
          CLC
          ADC     #$10                   ;ADD FOR BORDER CHECKING
          CMP     #MINY+$10
          BCC     SHOTEND
          LDA     SYEXTBL,X              ;GET  THE EXTENT
          SBC     #SHOTSTY               ;MOVE THE OTHER EDGE
          STA     SYEXTBL,X
          JMP     MOVESD                 ;DONE WITH MOVING SHOT

*         NOTE: IN THE BORDER CHECKING ABOVE IT IS OK TO USE SHOTWID AND SHOTHT
*      INSTEAD OF ACTUAL EXTENTS - SHOTS ARE NEVER PARALLEL TO BORDERS THEY HIT

*         A SHOT HIT A WALL  --  REMOVE IT
SHOTEND   LDA     #$00
          STA     SDIRTBL,X              ;ZERO ITS DIRECTION
*         NOW GO ON, AND THE SHOT LOADING ROUTINE MUST REMOVE THIS SINCE
*                 ITS DIRECTION IS 0.

*         CONTINUE AFTER MOVING SHOT...
MOVESD    RTS
*




******************************************
*
*         MCMOV   --  MOVE MUTANT CLONE ACCORDING TO MOVEMENT JOYSTICK
*                     ALSO CHECK FOR COLLISIONS
*
******************************************
*
MCMOV     DEC     MCMTMR                 ;CHECK IF TIME TO MOVE
          BPL     MCMOV1                 ;SKIP PAST IF TOO EARLY
          INC     MCMTMR                 ;RESET MCMTMR
;         INC     MCMTMR      ;NOW, HAVE MC MOVE EVERY FRAME     @@@@@@
          CLC
          LDA     SWCHA                  ;GET MOVE CONTROL
          AND     #MASKH                 ;GET CORRECT BITS
          LSR     A
          LSR     A
          LSR     A
          LSR     A                      ;GET IT INTO BOTTOM BITS
          CMP     #$0F                   ;WILL BE $F IF MC ISN'T MOVING
          BEQ     MCMOV1                 ;SKIP MOVING MC

*         MC IS MOVING THIS FRAME
          STA     MCDIR                  ;PUT AWAY MOVEMENT DIR FROM JOYSTICK
*         CHANGE ANIMATION STEP
          DEC     MCSA                   ;DECREMENT STEP
          BPL     MCMOV2                 ;BRANCH IF STILL A VALID STEP
          LDA     #$03           ;RESET ANIMATION - LOAD HIGHEST ANIMATION STEP
          STA     MCSA

MCMOV2    LDA     MCDIR                  ;RESTORE MOVEMENT DIRECTION
*         COMPUTE CHANGES IN MC'S POSITION
          TAX                            ;PUT IT IN X TO INDEX
          LDA     XDIRTBL4,X             ;RETURNS 0, +STEP OR -STEP FROM 4-BITS
          ADC     MCXPOS                 ;MOVE 1 STEP
          STA     MCXPOS                 ;STORE NEW POSITION
          CLC
          ADC     #MCWID
          STA     MCXEX                  ;STORE NEW X EXTENT

          LDA     YDIRTBL4,X             ;RETURN CHANGE IN Y POSITION
          CLC
          ADC     MCYPOS                 ;MOVE 1 STEP IN  Y DIRECTION
          STA     MCYPOS                 ; STORE NEW Y POSITION
          CLC
          ADC     #MCHEIGHT
          STA     MCYEX                  ;STORE NEW Y EXTENT

          LDX     #$00                   ;LOOK AT MC ENTRIES IN OBJECT TABLES
          JSR     CHKOBJBD               ;CHECK IF OFF SCREEN

MCMOV1                                   ;LOOK FOR MC COLLIDING WITH AN OBJECT
          DEC     MCCTMR                 ;DECREMENT COLLISION TIMER
          BMI     MCMOV11                ;MUST CHECK COLLISIONS NOW
          JMP     MCOK                   ;SKIP COLLISIONS THIS FRAME
MCMOV11   LDA     #$02
          STA     MCCTMR                 ;RESET COLLISION TIMER

*         CHECK FOR COLLISIONS

***********HERE NEED TO CHECK THROUGH ELECTRODES FOR COLLISIONS        *********

*         SEARCH THROUGH OBJECTS IN MC'S ZONE
*  SET UP TEMP0 AND TEMP1 TO BE THE ADDRESS OF THE MC'S ZONE'S ENTRY IN ZONOBJC
*         (THE FIRST IF MC IS IN 2 ZONES).  THEN SET Y TO 27 (OR 55 IF MC
*  IS IN 2 ZONES)  AND LOOP THRU ZONOBJC, DECREMENTING Y UNTIL IT GOES TO 0.

          LDY     #27                    ;SET UP Y FOR 1-ZONE CASE
*         COMPUTE MC'S FIRST ZONE, CHECK IF IN 1 OR 2 ZONES, SET UP Y

          LDA     MCYPOS                 ;LOAD MC Y POSITION
          LSR     A
          LSR     A
          LSR     A
          LSR     A                      ;GET ZONE # FROM Y POSITION
          STA     TEMPZON
*         CHECK IF IN 2 ZONES
          LDA     MCYEX                  ;Y POSITION OF MC LOWER EDGE
          LSR     A
          LSR     A
          LSR     A
          LSR     A                      ;GET ZONE # OF MC'S LOWER EDGE
          CMP     TEMPZON                ;IS IT SAME AS MC TOP ZONE?
          BEQ     MCC1                   ;MC IN ONLY 1 ZONE, LEAVE Y AS 31
          LDY     #55             ;LOAD Y WITH 55 - WE WILL INDEX THRU 2 ZONES
MCC1              ;NOW Y IS SET UP
*         GET ABSOLUTE ADDRESS OF START OF MC'S ZONE IN ZONOBJC
*                 ZONE NUMBER IS IN TEMPZON
          STY     TEMP2                  ;PUT Y AWAY FOR NOW
          LDY     TEMPZON                ;ZONE NUMBER WILL BE USED TO INDEX
          LDA     ZONOBJLH,Y             ;GET HIGH BYTE OF ABS ADDRESS
          STA     TEMP1
          LDA     ZONOBJLL,Y             ;GET LOW BYTE OF ABS ADDRESS
          STA     TEMP0
          LDY     TEMP2                  ;RECOVER Y - IT WILL INDEX THRU ZONOBJC

*         NOW WE HAVE THE ABSOLUTE ADDRESS OF THE ZONOBJC LISTING
*                 FOR THE MC'S (FIRST) ZONE IN TEMP0 AND TEMP1

MCCLOOP   LDA     (TEMP0),Y      ;GET OBJECT # FROM CORRECT PART OF ZONOBJC
          BEQ     MCCNEXT                ;IF 0, A NULL ENTRY
          TAX                            ;PUT OBJECT INDEX IN X
          LDA     CRTBL,X                ;LOAD CREATURE TYPE
          BEQ     MCCNEXT                ;IF THIS IS 0, ALSO A NULL OBJECT
          CMP     #MCSCODE               ;CHECK IF AN MC SHOT
          BEQ     MCCNEXT                ;MC CAN'T HIT HIS OWN SHOT
          LDA     STTBL,X
          BEQ     MCCNEXT                ;CAN'T HIT DEAD OBJECT
          AND     #MASKL
          CMP     #$03
          BEQ     MCCNEXT                ;CAN'T HIT DYING OBJECT

*         CHECK FOR A COLLISION WITH THE OBJECT POINTED TO BY X.
*                 IF A FAMILY, HANDLE IT AND THEN CONTINUE
*         IF A COLLISION, THE MC BITES IT
*
*         MC X,Y ARE IN MCXPOS AND MCYPOS
*         MC X,Y EXTENTS ARE IN MCXEX AND MCYEX
*         OBJECT X,Y ARE IN XTBL,X AND YTBL,X
*         OBJECT EXTENTS ARE IN XEXTBL,X AND YEXTBL,X
*                  IF A MISS, BRANCH TO MCCNEXT.  IF A HIT, FALL THROUGH
          CLC
          LDA     MCXEX
          CMP     XTBL,X
          BCC     MCCNEXT                ;BRANCH IF LESS THAN
          LDA     MCXPOS
          CMP     XEXTBL,X
          BCS     MCCNEXT                ;BRANCH IF > OR =

          LDA     MCYEX
          CMP     YTBL,X
          BCC     MCCNEXT                ;BRANCH ON LESS THAN
          LDA     MCYPOS
          CMP     YEXTBL,X
          BCS     MCCNEXT                ;BRANCH ON GREATER THAN

*         HIT!    CHECK IF WITH FAMILY

          LDA     CRTBL,X                ;GET CREATURE TYPE
          CMP     #MOCODE
          BEQ     MCCF
          CMP     #DCODE
          BEQ     MCCF
          CMP     #MICODE
          BNE     MCC2
MCCF              ;MC COLLIDED WITH A FAMILY MEMBER
*         NOW:  ENTER PICKUP SOUND INTO QUEUE.  UPDATE SCORE AND SET FAMILY
*    ANIMATION TO THE CORRECT NUMBER (1,2,3,4 OR 5) WITH THE HIGH BIT SET
*    SET THE OBJECT CODE TO #MOCODE.  SET THE DYING BIT IN STTBL.
*    SET THE OBJECT DIRECTION CODE TO 8.  FINALLY, RESET MTTBL.

*         HERE ENTER PICKUP SOUND INTO SOUND QUEUE                      ********
*         HERE CALL THE ROUTINE TO UPDATE SCORE - IT SHOULD RETURN WITH
*                 THE CORRECT NUMBER ANIMATION IN A  (1 - 5 FOR 1000 - 5000 )
          JSR     FSCORE                 ;THIS RETURNS WITH 1 - 5 IN A
          ORA     #$80                   ;SET HIGH BIT
          STA     SATBL,X                ;STORE NEW ANIMATION STEP
          LDA     #MOCODE
          STA     CRTBL,X                ;STORE NEW CREATURE TYPE
          LDA     #$02                   ;BIT 1 IS ON
          ORA     STTBL,X
          STA     STTBL,X                ;SET BIT 1 IN STATUS ENTRY
          LDA     #$08
          STA     DXTBL,X                ;SET DIR CODE
          LDA     #$00
          STA     MTTBL,X                ;RESET MOVEMENT TIMER

          JMP     MCCNEXT                ;CONTINUE COLLISION CHECKING

MCC2             ;MC DIDN'T HIT A FAMILY, SO HE HIT SOMETHING HE DIDN'T WANT TO
          JMP     MCDEATH                ;MC DEATH ROUTINE

*         CONTINUE HERE IF NO COLLISION WITH CURRENT OBJECT
MCCNEXT   DEY                            ;ON TO NEXT ENTRY IN ZONE LIST
          BPL     MCCLOOP                ;IF Y NON-NEGATIVE, KEEP GOING

*         WE HAVE CHECKED EVERYTHING IN THE MC'S ZONE(S)
MCOK                                     ;NO COLLISION WITH MC THIS TIME
          RTS                            ;MC MAKES IT THROUGH YET ANOTHER FRAME

*

******************************************
*
*         CHKOBJ  -- CHECK OBJECTS LOOP
*                 LOOK AT EACH OBJECT, AND IF NECESSARY, MOVE IT
*
******************************************
*
CHKOBJ    LDX     #$01                   ;OBJECT INDEX FOR FIRST OBJECT (GRUNT)

*         X IS THE OBJECT INDEX: A COUNTER INTO THE OBJECT DATA TABLES
*
LOOP      LDA     CRTBL,X                ;GET CREATURE CODE
          BEQ     OBJCONT                ;NULL OBJECT IF 0
          CMP     #$FF
          BEQ     DONE                   ;END OF TABLE IF $FF
*
          DEC     MTTBL,X                ;DEC  MOVEMENT TIMER
          BMI     MOVE                   ;TIME HAS COME TO MOVE

*                               MOVE RETURNS TO HERE:
OBJCONT   INX                            ;INCREMENT OBJECT INDEX: NEXT OBJECT
          JMP     LOOP                   ;CHECK THE NEXT OBJECT

*
DONE      RTS
*

******************************************
*
*         MOVE    -- CALLED BY CHKOBJ
*
******************************************
*
MOVE                                     ;X IS OBJECT INDEX
          LDA     CRTBL,X                ;GET OBJECT TYPE
*         WE NOW HAVE A NUMBER FROM 1 TO $E DENOTING OBJECT TYPE

          SEC
          SBC     #$01                   ;GET A NUMBER FROM 0 TO $D
          ASL     A                      ;MULTIPLY BY 2, HAVE 0 TO $1A
          TAY
          LDA     MOVTBL,Y               ;LOAD TEMP0 AND TEMP1
          STA     TEMP0                  ; WITH ADDRESS OF OBJECT HANDLING
          LDA     MOVTBL+1,Y             ; ROUTINE LOOKED UP IN MOVTBL
          STA     TEMP1                  ;JUMP TO CORRECT ROUTINE TO HANDLE
          JMP     (TEMP0)                ;    EACH TYPE OF OBJECT
*
**********************************************************************

******************
*         THROWAWAY GARBAGE:
*
*         MARINIT - INIT MARIA STUFF
MARINIT   LDA     #$FF                   ;SET UP GRAPHICS MODE
          STA     CTRL
M1        EQU     MARINIT                ;CODE WHICH MAY HAVE TO BE CHANGED
                          ;IN THE EMULATOR DUE TO MARIA DIFFERENCES

          LDA     #$00
          STA     CTLSWA                 ;INIT JOYSTICK PORT

          LDA     #$00                   ;BACKGROUND COLOR
          STA     BACKGRND

          JSR     PALINIT                ;SET UP PALETTES

          RTS
*

*******************
*
*         PALINIT - INIT PALETTES
*
*******************
*

PALINIT
*         PALETTE 0 - MUTANT CLONE
          LDA     #$0F                   ;WHITE
          STA     P0C1                   ;BODY AND FEET
          LDA     #$69                   ;PURPLE
          STA     P0C2                   ;ARMS AND HEAD (BEANIE)
*         LEAVE COLOR 3 ALONE - IT CYCLES

*         PALETTE 1 - GRUNT AND MIKEY
          LDA     #$0F                   ;WHITE
          STA     P1C1                   ;NECK STRIPE
          LDA     #$36                   ;RED
          STA     P1C2                   ;BODY
          LDA     #$1F                   ;YELLOW
          STA     P1C3                   ;HEAD AND FEET

*         PALETTE 2 - MOMMY
          LDA     #$FF                   ;BLONDE
          STA     P2C1                   ;HAIR AND LEGS
          LDA     #$4F                   ;HOT PINK
          STA     P2C2                   ;DRESS
          LDA     #$DF                   ;POCKETBOOK GREEN
          STA     P2C3                   ;POCKETBOOK

*         PALETTE 3 - DADDY
          LDA     #$99                   ;BLUE
          STA     P3C1                   ;SUIT
          LDA     #$1A                   ;DIRTY BLONDE
          STA     P3C2                   ;HAIR
          LDA     #$14                   ;BROWNISH
          STA     P3C3                   ;FACE AND ATTACHE CASE

*         PALETTE 4 - HULKS
          LDA     #$35                   ;RED
          STA     P4C1                   ;HEAD AND LEGS
          LDA     #$ED                   ;LIGHT GREEN
          STA     P4C2                   ;BODY
          LDA     #$00                   ;UNIMPORTANT
          STA     P4C3                   ;ARMS - COLOR CYCLE

*         PALETTE 5 - SKULL AND CROSSBONES, SCORE NUMBERS, ELECTRODES
          LDA     #$0F                   ;WHITE
          STA     P5C1                   ;SKULL
          LDA     #$00                   ;UNIMPORTANT
          STA     P5C2                   ;THIS COLOR CYCLES
          LDA     #$00                   ;UNIMPORTANT
          STA     P5C3                   ;THIS COLOR CYCLES

*         PALETTE 6 - BRAINS
          LDA     #$00                   ;UNIMPORTANT
          STA     P6C1                   ;EYES AND SOME OF BRAIN - COLOR CYCLES
          LDA     #$97                   ;BLUE
          STA     P6C2                   ;BRAIN
          LDA     #$EB                   ;GREEN
          STA     P6C3                   ;BRAINS' FEET

          RTS
*

*****************************************
*
*         CONV- CONVERT DATA INTO Si MARIA FORM  (MUNG-O-RAMA)          ********
*
*****************************************

;         CONVERTS GRAPHICS DATA FROM $4000 TO $5FFF
;         FROM THE FORMAT 76 54 32 10     TO        10 32 54 76

GRAPH     EQU     $4300
END       EQU     $6200                  ;SHOULD BE ENLARGED LATER

CONV      NOP                            ;FOR SI MARIA, SHOULD BE A NOP
          LDA     #$60
          STA     CONV                   ;RTS OUT THIS SUBROUTINE,
                                         ;   IT HAS BEEN USED ONCE
          LDX     #0
          LDA     #H(GRAPH)
          STA     LOAD+2
          STA     SAVE+2
LOAD      LDA     GRAPH,X
          STA     CONVTEMP
          AND     #$C0
          CLC
          ROL     A
          ROL     A
          ROL     A                      ;BITS 7,6 IN PLACE
          ASL     CONVTEMP
          ASL     CONVTEMP
SK2       ASL     CONVTEMP
          BCC     SK3
          ORA     #$8                    ;ADD BIT 5
SK3       ASL     CONVTEMP
          BCC     SK4
          ORA     #$4                    ;ADD BIT 4
SK4       ASL     CONVTEMP
          BCC     SK5
          ORA     #$20                   ;ADD BIT 3
SK5       ASL     CONVTEMP
          BCC     SK6
          ORA     #$10                   ;ADD BIT 2
SK6       ORA     CONVTEMP                   ;OR IN BITS 1 AND 0
SAVE      STA     GRAPH,X
          INX
          BNE     LOAD
          INC     LOAD+2
          INC     SAVE+2
          LDA     LOAD+2
          CMP     #H(END)
          BNE     LOAD
          RTS
CONVTEMP  DB      0                      ;TEMP, THROWAWAY VARIABLE




***********************************************
***********************************************
***********************************************


*         END OF RMAIN.S
          EJE
