A82: Subhunt routine help


[Prev][Next][Index][Thread]

A82: Subhunt routine help




    I worked on Subhunt for the first time in awhile today. I decided that 
in the interest of speed and simplicity, enemy sprites should be stored in 
APD_BUF. That way score values wouldn't have to be written every time an 
enemy sprite was display. Basically, I write the generic sprite, then use 
set/res 7(iy+$14) to write the HP value to the sprite. Then I copy that area 
of GRAPH_MEM to a location in APD_BUF, where it is later used for the 
movement routine.

  Too bad, it doesn't work :(

  I've been working on a fix for the past hour, but I'm not really getting 
anywhere. Also, the routine is fairly large for such a simple task 
(initialize a new enemy). I've put the routine below, and it's commented for 
the most part. Any optimization or bug fix help would be greatly 
appreciated.


NewEnemy:
ld a,15           ;15 enemies max
ld hl,enemy1y     ;Y coordinate used to determine if enemy already out there
ld de,enemies     ; DE = Pointer to enemy sprites
ld bc,enemy1hp    ; BC = Pointer to enemy HP and enemy speed
SearchEmpty:
push af           ;Save the no. of loops remaining
ld a,(hl)         ;Load Y coord to A
or a              ;Activate Z or NZ flag
jr z,NoE          ;If Y = 0, then there's no enemy in this slot
pop af            ;Restore loop counter

inc bc            ;Go to next HP entry

push hl
ld h,d
ld l,e
ld de,25
add hl,de
ld d,h
ld e,l
pop hl         ;Go to next sprite entry (8 lines of height*3 sprites 
width+direction header)

inc hl
inc hl             ;Go to next set of enemy coords
dec a              ;Decrease A
jr nz,SearchEmpty  ;If not zero, then keep looping
ret                ;none empty then screen full and return

NoE:
pop af               ;Pop the loop counter, because it needs to be ;)

push de              ;Save the sprite pointer
push hl              ;Save Y coord

push bc
pop hl               ;Load BC to HL
ld de,10
add hl,de            ;Add 10 to HL to point to the enemy speed data
ld a,5
call RANDOM          ;Get a random # between 0-4
ld (hl),a            ;Load that to current enemy speed
inc a
inc a                ;Increase that number twice (2-6)
ld h,a               ;Load that to H
ld a,8
call RANDOM
inc a                ;random number (1-8)
push af              ;Save AF
ld l,a               ;Load A to L
call MUL_HL          ;Multiply H*L = HL
ld a,(hl)            ;Load HL to A
ld (bc),a            ;Load A to the enemy HP pointer
pop af               ;Pop AF
inc a                ;Increase it again
ld h,a               ;Load it to H
ld l,6               ;Load 6 to L
call MUL_HL          ;HL = H*L
ld a,(hl)            ;Load answer to A
pop hl               ;Restore Y coord pointer
ld (hl),a            ;Sub depth stored!

dec hl               ;Decrease HL to point to current X coord

ld a,(bc)            ;Load enemy HP to A

SetESprite:
push hl              ;Save X coord pointer
push af              ;Save enemy HP pointer
ld a,2
call RANDOM          ;Random number (0-1)
ld (temp),a          ;Load it to temp space for use later (I'm tired of 
PUSH/POP's ;)
jr nz,GoLeft        ;If 1, then ship moves left

GoRight:                ;If zero, then go here
pop hl                 ;Restore X coord
ld a,-15               ;Load -15 to X coord, to put ship on left edge
ld (hl),a              ;set coords
call _grbufclr         ;Clear Graph_mem, and trash all registers
pop af                 ;Restore enemyHP
set 7,(iy+$14)         ;Write text to graph_mem
set 3,(iy+5)           ;Write inverse text
ld l,a                 ;Load enemy HP to A
ld h,0                 ;Load 0 to HL
ld b,2                 ;HL is a 2 digit number
ld de,$0005            ;Write to these coords
call DM_HL_DECI        ;Use Mardell's DM_HL_DECI routine
res 3,(iy+5)
res 7,(iy+$14)         ;restore flags

ld bc,$0000
ld hl,SubRight
jr PutNumber           ;Now copy the stuff to the right place

GoLeft2:
pop hl      ;Pop X coord
ld a,95      ;set to Right edge
ld (hl),a    ;set coords
call _grbufclr         ;Clear Graph_mem, and trash all registers
pop af         ;Pop enemyHP
set 7,(iy+$14)  ;Write to graph_mem
set 3,(iy+5)  ;Write inverse text
ld l,a       ;Load enemyHP to L
ld h,0       ; Load zero to H
ld b,2          ;2 digits
ld de,$0004    ;Write to these coords
call DM_HL_DECI  ;Mardell's DECI routine
res 3,(iy+5)
res 7,(iy+$14) ;restore flags

ld bc,$0000
ld hl,SubLeft

PutNumber:
call PutSprite3   ;Now draw the appropriate sprite to the screen
                   ;Using a hacked version of Hideaki's SprClp v3.0
ld a,(temp)       ;finally the temp data is used
pop de            ;Pop the sprite pointer
ld (de),a         ;Load the sprite direction to the sprite data (used for 
movement routines)
inc de            ;Go to actual sprite data
ld hl,GRAPH_MEM   ;Copy first byte of first 8 lines of graph_mem
call copy_G_A     ;Do it

ld de,8           ;Sprite pointer switched to HL by copy_G_A
add hl,de         ;Increase that by 8
ex de,hl          ;Switch HL and DE

ld hl,GRAPH_MEM+1   ;Now copy second byte of graph_mem
copy_G_A:
push de            ;Save SpritePointer
ld b,8             ;8 lines to copy
cclop
ld a,(hl)          ;ld (HL) to A
ld (de),a          ;ld A to (de)
inc de             ;Increase DE once
push de            ;Save DE
ld de,12
add hl,de          ;Increase HL 12 times to get to the next row
pop de             ;Restore DE
djnz cclop         ;Decrease B, if not zero then loop
pop hl             ;Restore SpritePointer as HL
ret                ;The x,y coords are set, the sprite is copied,
                    ;speed and HP and now defined, so return to the          
            ;main loop!

MUL_HL:
push bc
push de
ROM_CALL($1F18-$1A)    ;This is just in case this ROM_CALL corrupts some 
registers other than
pop de                 ;H and L.
pop bc
ret

RANDOM:    ;Mardell random number generator of 0 to A minus 1
push hl
push de
push bc
ld b,a
ld a,r
add a,a
ld hl,0
ld d,0
ld e,a
RMul:
add hl,de
djnz RMul
ld a,h
or a          ;Set flags
pop bc
pop de
pop hl			;Restore all values other than AF
ret


______________________________________________________
Get Your Private, Free Email at http://www.hotmail.com