;ChoongHong Oh

TITLE PGM8: GENERATE ENEMY
        EXTRN GENERATE_DELAY : BYTE
        EXTRN NO_F_ENE : BYTE 
        EXTRN F_ENE1 : WORD, F_ENE2 : WORD, F_ENE3 : WORD
        EXTRN F_ENE4 : WORD, F_ENE5 : WORD
        PUBLIC GENERATE_ENE

.MODEL SMALL

IF1
        INCLUDE G_MACRO.ASM
ENDIF

.CODE

SET_COORDINATES MACRO X, Y
;this macro generates a random X from 132 to 432, using INT 62h random generator
;and gives back a coordiantes (X, 0)
        PUSH AX
        MOV AX, 290      ;generate X_coordiante randomly
        INT 62h          ;random function
        ADD AX, 132
        MOV X, AX        ;X_coordinate gets a random number from 132 to 422
        MOV Y, 0         ;Y_coordinate gets 0
        POP AX
        ENDM

GENERATE_ENE PROC
;this procedure looks for a falling enemy which is not used,
;and gives it a new coordinate (X, 0) with X from 132 to 432
;and increments the number of falling enemies

;search fo F_ENE which is not used currently
;check F_ENE1
        CMP F_ENE1, 0FFFFh    ;if F_ENE1 is used,
        JNZ CHECK_F_ENE2      ;then, check F_ENE2
        SET_COORDINATES F_ENE1, F_ENE1+2 ;else assign X and Y coordinates for F_ENE1
        JMP DONE              ;now, this procedure is done
CHECK_F_ENE2:
        CMP F_ENE2, 0FFFFh    ;if F_ENE2 is used,
        JNZ CHECK_F_ENE3      ;then, check F_ENE3
        SET_COORDINATES F_ENE2, F_ENE2+2 ;else assign X and Y coordinates for F_ENE2
        JMP DONE              ;now, this procedure is done
CHECK_F_ENE3:
        CMP F_ENE3, 0FFFFh    ;if F_ENE3 is used,
        JNZ CHECK_F_ENE4      ;then, check F_ENE4
        SET_COORDINATES F_ENE3, F_ENE3+2 ;else assign X and Y coordinates for F_ENE3
        JMP DONE              ;now, this procedure is done
CHECK_F_ENE4:
        CMP F_ENE4, 0FFFFh    ;if F_ENE4 is used,
        JNZ CHECK_F_ENE5      ;then, check F_ENE5
        SET_COORDINATES F_ENE4, F_ENE4+2 ;else assign X and Y coordinates for F_ENE4
        JMP DONE              ;now, this procedure is done
CHECK_F_ENE5:
        SET_COORDINATES F_ENE5, F_ENE5+2 ;assign X and Y coordinates for F_ENE5
DONE:
        MOV GENERATE_DELAY, 0  ;reset the time counter for generator
        INC NO_F_ENE         ;increment the number of falling enemy 
        RET
GENERATE_ENE ENDP
        END
