*DECSUBR                                 JULY 25
          ZEROPAGE
          FLAGGLBL
          PUBLIC GETTARG
          GLBL    RANDOM                 extrn
          ELGLBL
          MAPVGLBL

******************************************
CODEF000  CSECT
ATOPSUBR

*
* THIS ROUTINE GETS THE NEXT FIXED GROUND TARGET TO BE DISPLAYED.  IT RETURNS
* THE ROW, COLUMN, AND TYPE OF THE NEXT FIXED GROUND TARGET.
* THE ARRAY TRCTYP CONTAINS A LIST OF (ROW, COLUMN, TARGET TYPE) TUPLES
* WHICH DETERMINE WHERE THE NEXT GROUND TARGET GOES AND WHAT IT IS.
* THE NEXT TUPLE IN THE ARRAY IS POINTED TO BY TRCTYPL[H].
* THE ROW IS AN ABSOLUTE STAMP POSITION WITHIN THE MAP AND OCCUPIES ONE BYTE.
* THE COLUMN IS WITH RESPECT TO THE DISPLAYED PART OF THE MAP SO IS IN THE RANGE
* [0, 15].  THE TYPE IS ALSO IN THE RANGE [0, 15] AND IS PACKED INTO THE SAME
* BYTE AS THE COLUMN.  THUS EACH TUPLE REQUIRES 2 BYTES.
* THERE ARE A NUMBER OF COMPLICATIONS DUE TO GROUND TARGETS THAT OCCUPY MORE
* THAN ONE ZONE.  FROM THE POINT OF VIEW OF THE PLAYFIELD BUILDING CODE EACH
* ROW OF A MULTI-ZONE GROUND TARGET IS A NEW GROUND TARGET, BECAUSE STAMPS MUST
* BE SET UP FOR IT.  FROM THE POINT OF VIEW OF PLAY CODE EACH MULTI-ZONE GROUND
* TARGET IS A SINGLE TARGET, EXCEPT FOR DOME COMPLEXES, WHICH ARE A WEIRD
* SPECIAL CASE (EACH DOME OF A DOME COMPLEX ACTS LIKE AN INDEPENDENT ENEMY FOR
* FIRING AND COLLISION PURPOSES EXCEPT THAT WHEN THE CENTER DOME IS HIT THE
* WHOLE THING BLOWS UP).
* THE MULTI-ZONE GROUND TARGETS ARE THE LARGE PYRAMID, LARGE DEFENDER SITE, AND
* DOME COMPLEX.  THE LARGE PYRAMID AND LARGE DEFENDER SITE ARE HANDLED IN THE
* SAME WAY.  EACH IS REPRESENTED BY A SINGLE TYPE CODE IN THE TTYPTR ARRAY.
* THIS SUBROUTINE SPLITS THEM UP INTO TWO TYPE CODES EACH WHICH ARE PASSED BACK
* ONE ROW AT A TIME TO THE PLAYFIELD BUILDING CODE.  (THAT IS, THE 2 ROW, 4
* STAMP LARGE DEFENDER SITE LOOKS LIKE 2 ONE ROW GROUND TARGETS TO THE PLAYFIELD
* BUILDING CODE.)  THE ARRAYS TARGCOL2 AND TARGTYP2 KEEP TRACK OF
* THE SECOND ROW INFORMATION FOR THESE LARGE TARGETS.  WHEN A NEW ROW IS REACHED
* THE INFORMATION IN THESE ARRAYS IS DUMPED BEFORE EXTRACTING ANY NEW GROUND
* TARGETS FROM THE TTYPTR AND TXYP ARRAYS.  THE SECOND ROW TYPE CODES PASSED TO
* THE PLAYFIELD BUILDING CODE ARE DERIVED FROM THE FIRST ROW TYPE CODES BY
* SUBTRACTING THE CONSTANT "TYPDELTA".
*      INFORMATION ABOUT THE GROUND TARGETS CURRENTLY ON THE SCREEN IS KEPT IN
* TABLES USED BY THE PLAY CODE.  TWO ROW GROUND TARGETS ARE GIVEN TWO ENTRIES IN
* IN THE TABLE, WITH THE TWO TYPE CODES PASSED BACK BY THE PLAYFIELD CODE.  THE
* PLAY CODE MUST KNOW TO TREAT THE TWO ENTRIES AS ONE OBJECT.  TO MAKE THIS
* POSSIBLE THE PLAYFIELD CODE PASSES BACK AN ID WHICH IS UNIQUE FOR DIFFERENT
* TARGETS DISPLAYED ON THE SAME SCREEN BUT THE SAME FOR THE TWO HALVES OF A
* TWO ROW TARGET.  THE ID IS PASSED BACK FOR ALL GROUND TARGETS BUT IS ONLY
* NEEDED FOR THE LARGE PYRAMIDS AND LARGE DEFENDER SITES.
* DOME COMPLEXES ARE HANDLED AS THOUGH THEY WERE 5 DISTINCT ENEMIES.  FIVE
* DIFFERENT TYPE CODES ARE PASSED TO THE PLAYFIELD BUILDING CODE BECAUSE EACH
* DOME USES A DIFFERENT STAMP.  THE PLAYFIELD CODE IN TURN PASSES BACK TO THE
* PLAY ROUTINES TWO DIFFERENT TYPE CODES -- ONE FOR EACH OF THE FOUR PERIPHERAL
* DOMES AND ONE FOR THE CENTER DOME.  THIS IS BECAUSE THE PERIPHERAL DOMES ACT
* THE SAME WAY WHILE THE CENTER DOME HAS THE SPECIAL PROPERTY THAT WHEN IT IS
* BLOWN UP ALL DOMES IN THE COMPLEX BLOW UP.
* EACH OF THE FIVE DOMES OF A DOME COMPLEX HAS A DIFFERENT ID.  WHEN THE WHOLE
* MESS MUST BE BLOWN UP THE CONVENTION IS TO BLOW UP ALL PERIPHERAL AND CENTER
* DOMES IN THE TABLE.  THIS WORKS BECAUSE WE ARE GUARANTEED THAT ONLY ONE
* DOME COMPLEX EVER APPEARS AT A GIVEN TIME.  (REGULAR DOMES HAVE A TYPE CODE
* DISTINCT FROM THE TYPE CODES USED FOR DOME COMPLEXES.)
*
GETTARG   LDY     #0
          LDA     (TRCTYPL),Y            ; IF THIS IS A NEW ROW AND TARG2CNT <>0
          CMP     TARGROW2               ; THEN THE SECOND ROW INFORMATION FOR
          BEQ     NEXTTARG               ; 2 ROW GROUND TARGETS MUST BE DUMPED.
          LDX     TARG2CNT
          BEQ     NEXTTARG
          DEX                            ; DUMP ROW 2 INFORMATION
          STX     TARG2CNT
DUMPROW2  LDA     TARGCOL2,X
          STA     TARGCOL
          LDA     TARGTYP2,X
          STA     TARGTYPE
          LDA     TARGROW2
          CLC
          ADC     #1
          STA     TARGROW
          LDA     TARGID2,X
          STA     TARGID
          RTS
NEXTTARG  STA     TARGROW                ; USUAL CASE, PULL TARGET OFF THE LIST
          INC     TARGIDCT
          LDA     TARGIDCT
          STA     TARGID
          INC     TRCTYPL                ; TRCTYPE ARRAY IS ALIGNED SO THAT A
          LDA     (TRCTYPL),Y            ; CARRY CAN'T OCCUR WITHIN A TUPLE
          TAX
          LSR     A                      ; THE COLUMN IS IN THE HIGH NIBBLE.
          LSR     A
          LSR     A
          LSR     A
          STA     TARGCOL
          TXA
          AND     #$0F                   ; TYPE IS IN LOW NIBBLE
          STA     TARGTYPE
          CMP     #$07
          BNE     FLAGNYET

          LDA     #$01
          STA     FLAGFLAG

          JSR     RANDOM
          ADC     ELYPOS
          AND     #$0F
          STA     TARGCOL
          LDA     TARGTYPE
FLAGNYET  CMP     #TARG2ROW
          BMI     INCTRCTY
          SEC                            ; IF TARGTYPE >= TARG2ROW THEN THIS IS
          SBC     #TYPDELTA              ; THE FIRST ROW OF A TWO ROW GROUND
          LDX     TARG2CNT               ; SO THE TARG2 ARRAYS MUST BE SET WITH
          STA     TARGTYP2,X             ; THE SECOND ROW INFORMATION.
          LDA     TARGCOL
          STA     TARGCOL2,X
          LDA     TARGROW
          STA     TARGROW2
          LDA     TARGID
          STA     TARGID2,X
          INX
          STX     TARG2CNT
INCTRCTY  LDA     TRCTYPL                ; BUMP TRCTYPL TO POINT TO NEXT TUPLE.
          CLC
          ADC     #1
          STA     TRCTYPL
          BCC     GTARGRET
          INC     TRCTYPH
GTARGRET  RTS

BOTSUBR
          END
