+;--------------------------- display ----------------------------------------
+
+Display_Screen:
+ ld hl,dispbuffer ;from buffer (top left)
+ ld de,VIDEO_MEM ;to real screen (top left)
+ ld c,56 ;display height = 64 bytes (minus 8 for bar)
+displayloop:
+ ld b,16 ;display width = 16 bytes (16*8bits=256pixels)
+displaytloop:
+ ld a,(hl) ;copy byte from (hl)
+_invert: ;SMC: cpl <-> or a
+ cpl ;xor $ff: invert byte (white<=>black)
+ ld (de),a ;to (de)
+ inc hl \ inc de ;next byte
+ dnz displaytloop ;16x hl >> de
+ dec c ;next line
+ jr nz,displayloop ;loop 56x
+
+ ld hl,time2invert
+ ld a,(hl)
+ or a ;(time2invert)=0:
+ jr z,noinvert ; do nothing
+ dec a ;otherwise decrease
+ cal z,do_invert ;if it became 0 then invert
+ ld (hl),a ;save new value
+noinvert:
+
+ ld hl,$396b ;Display Score
+ ld (_penCol),hl ;bottom right of screen
+ ld hl,(your_score)
+
+_D_HL_DECI: ;------- display 5-digit value -------
+ ld de,savestr+4 ;savenr saves number string
+ ld b,5 ;five digits
+ldhld: cal UNPACK_HL ;one digit of hl
+ add a,'0' ;make number
+ ld (de),a ;save into savenr
+ dec de ;point to next digit
+ dnz ldhld ;repeat for all digits
+
+ ld hl,savestr ;we (the program) saved the value righthere
+ jp _vputs ;the only thing left to do is to display it
+
+savestr: ;@here the score will be stored
+ .db "00000",0 ;don't worry, it's just temporary
+
+;------------------------- handle ship --------------------------------------
+
+Handle_Ship:
+ ld a,(your_occ) ;are
+ or a ;you
+ jr z,ok ;ok?
+
+ inc a ;no! next (explosion)frame
+ ld (your_occ),a ;save
+
+ cp 64+1 ;last explosion frame? (1-16=1st;49-64=4th)
+ jp c,exploding_you ;not yet: display explosion
+ cp 64+16 ;delay finished?
+ jp z,You_die ;yes = game over
+ ret ;don't display anything
+
+;----move----
+ok: ;we are
+ ld a,%01111110 ;get arrow keys
+ out (1),a ;it's cold outside
+ ld hl,y ;instead of nop\nop do something usefull
+ in a,(1) ;come back in
+
+ ld b,a ;psh a (keys)
+ xor -1 ;inverted a: 0 if arrow-key has been pressed
+ ld a,(your_multiples) ;(btw: CPL doesn't set any flags)
+ res 7,a ;reset move bit (no flags changed)
+ jr z,adv_ok ;if so, leave the multiples where they are
+ set 7,a ;set move bit
+adv_ok: ld (your_multiples),a
+
+ ld a,(timer) ;framecounter
+ and %1 ;switches 0<>1 each frame
+ inc a ;a = 1 or 2 (1.5 avg)
+ ld c,a ;c = your_speed
+
+ ld a,b ;pop a (keys)
+ rra ;rotate right (put last bit in c)
+ ld b,a ;we need a later
+
+ jr c,no_down
+ ld a,(hl)
+ add a,c
+ cp 50 ;56-6 = bottom of screen
+ jr nc,no_down
+ ld (hl),a
+no_down:
+ dec hl
+ rr b ;because we now use b, it's rr instead of rra
+ jr c,no_left
+ ld a,(hl)
+ sub c ;<dec a> doesn't affect c-flag
+ jr c,no_left ;-1 = left side
+ ld (hl),a
+no_left:
+ rr b
+ jr c,no_right
+ ld a,(hl)
+ add a,c
+ cp 122 ;128-6 = right side
+ jr nc,no_right
+ ld (hl),a
+no_right:
+ ld d,(hl) ;d=x
+ inc hl
+ rr b
+ jr c,no_up
+ ld a,(hl)
+ sub c ;<dec a> doesn't affect carry-flag
+ jr c,no_up ;-1 = top of screen
+ ld (hl),a ;save new y
+
+no_up: ld e,(hl) ;e=y
+ ld ix,spr_ship01 ;normal ship sprite
+your_shipspr =$-2
+ ld hl,your_inv ;invulnerable?
+ ld a,(hl) ;load time in a
+ or a ;is it 0?
+ jr z,disp_ship ;yes so ship = normal (display \ continue)
+
+ ld a,(timer) ;load frame nr.
+ and %00000111 ;a=0 once every four frames
+ jr nz,not_time ;a<>0 = not time to update counter
+ dec (hl) ;decrease inv-time left
+not_time:
+ and %00000100 ;a switches 0<->1 every 2 frames
+ jr z,disp_ship ;show normal ship
+inv_flicker:
+ ld bc,spr_ship01i-spr_ship01
+ add ix,bc ;display invulnerable ship
+disp_ship:
+ cal safeputsprite ;display your ship; save de
+
+;----multiples----
+
+handle_multiples:
+ ld a,(your_multiples) ;do you have multiples
+ ld b,a ;save a for 2nd check
+ and %111 ;no? (last two bits = nr of multiples)
+ ret z ;then don't handle them either
+ bit 7,b ;move the multiples??? (=move bit set?)
+ jr z,mult_adv ;nope, just let them (saves (y)in y, (x)in x)
+
+ psh de ;current position = needed later
+ ld hl,mm*14+1+your_prevpos ;previous positions
+ ld de,mm*14+3+your_prevpos ;move all positions one back
+ ld bc,mm*14+2
+ lddr ;change 0-57 -> 2-59 (if mm=4 that is)
+ inc hl ;your_prevpos+0
+ pop de
+ ld (hl),d ;x-pos
+ inc hl ;=current position
+ ld (hl),e ;y-pos
+
+mult_adv:
+ ld hl,your_prevpos+16 ;first pos.
+disp_multiples:
+ psh af
+ psh hl
+ ld d,(hl) ;load coords
+ inc hl
+ ld e,(hl)
+ ld ix,spr_multiple ;sprite
+ cal putsprite ;display
+ pop hl
+ ld de,14
+ add hl,de ;next multiple
+ pop af ;counter
+ dec a
+ ret z ;return if all done
+ jr disp_multiples ;loop
+
+;----explode----
+
+exploding_you:
+ srl a ;half the framerate
+ srl a ;half that framerate
+ srl a ;and half again that framerate
+ ld hl,x-1
+ ld ix,spr_yexplosion ;base sprite
+
+explosion_stuff: ;in:a=frame*2+(0 to 1); (hl)=xpos-- ix=sprite
+ and %11111110
+ add a,a
+ add a,a ;frame*8
+ ld c,a
+ ld b,0 ;bc=a
+ add ix,bc ;go to correct sprite (each spr. is 8 bytes)
+ inc hl
+ ld d,(hl) ;load xpos
+ inc hl
+ ld e,(hl) ;and y
+ jp putsprite ;and display it too
+
+;----hit----
+
+damage_you: ;damages you B points
+ ld a,(your_inv) ;shield left?
+ or a
+ jr z,dothadamage ;no shield
+ srl b ;shield: half the damage
+dothadamage:
+ ld hl,time2invert
+ xor a ;a=0
+ cp (hl) ;no already inverted?
+ cal z,do_invert ;then invert screen
+ ld a,2
+ ld (hl),a ;change back 2 frames from now
+
+ ld hl,your_armor ;armor left
+ ld a,(hl) ;load hp in A
+ sub b ;decrease hp by B
+ jp m,no_armor ;<0hp left so explode
+ ld (hl),a ;no, so save decreased hp
+ jp disp_armor ;and display new value
+no_armor:
+ ld a,%01 ;occ %xxxxxx01 = explode
+ ld (your_occ),a ;too bad, you're dead meat
+ ret
+
+;------------------------- place multiples ----------------------------------
+
+Place_multiples:
+ ld hl,your_prevpos ;place all previous positions
+ ld b,mm*7+2 ;all saved positions of them (14 per multiple)
+place_multiples:
+ ld (hl),e ;set prev-x to d
+ inc hl ;next
+ ld (hl),d ;set prev-y to e
+ inc hl ;next
+ dnz place_multiples ;repeat
+ ret
+
+;------------------------- select upgrade -----------------------------------
+
+inc_armor:
+ ld a,(your_armor) ;load current armor
+ cp 25-6 ;may not become >=25
+ jr c,doincarmor ;ok then just add 6
+ ld a,24-6 ;set to maximum (6 will be added below)
+doincarmor:
+ add a,6 ;add 6 to armor
+ ld (your_armor),a ;change armor
+ ret
+
+select:
+ ld hl,your_pickup ;select pickups
+ ld a,(hl) ;load pickups taken so far
+ dec a ;is it 1?
+ ret m ;return if it's 0 (no pickups)
+ jr nz,select2 ;no, carry on
+select1:
+ ld (hl),a ;reset pickups
+ cal inc_armor
+ jp disp_icons ;display and return
+select2:
+ dec a ;is it 2?
+ jr nz,select3 ;no, carry on
+ ld (hl),a ;reset pickups
+ inc a ;a=1
+ ld (your_tail),a ;ready tail beam
+ jp disp_icons ;display 'n return
+select3:
+ dec a ;is it 3?
+ jr nz,select4 ;no, carry on
+ ld (hl),a ;reset pickups
+ ld hl,your_weapon
+ ld a,(hl)
+ inc a
+ cp maxweapon
+ jp nc,disp_icons ;weapon maxed out
+ ld (hl),a ;set new weapon
+ cal loadweapon ;load it (damage and stuff)
+ jp disp_icons ;display n return
+select4:
+ dec a ;is it 4?
+ jr nz,select5 ;no, carry on again
+ ld (hl),a ;reset pickups
+ ld hl,your_weapon
+ ld a,(hl)
+ cp maxweapon ;upgrade from bullet
+ jr nc,upgradelaser ;nope, just upgrade
+ ld a,maxweapon-1 ;yes, set laser #1
+upgradelaser:
+ inc a ;next laser
+ cp maxlaser
+ jp nc,disp_icons ;laser maxed out
+ ld (hl),a
+ cal loadweapon
+ jp disp_icons ;display + return
+select5:
+ dec a ;is it 5?
+ jr nz,select6 ;no, carry on once more
+ ld (hl),a ;reset pickups
+ ld hl,your_multiples
+ ld a,(hl) ;multiples you already got
+ inc a ;one more
+ cp mm+1
+ jr nc,enoughmultiples ;maxed out
+ ld (hl),a
+enoughmultiples:
+ ld de,(x)
+ dec a ;if this is your first multiple then...
+ cal z,Place_multiples ;reset multiples positions
+ jp disp_icons ;display, return
+select6:
+ ld (hl),0 ;reset pickups
+ jp disp_icons ;display/return
+
+;------------------------- fire bullet --------------------------------------
+
+fire_multiples:
+ ld hl,(your_prevpos+16);then, fire from multiple position
+ psh af
+ dec h ;one up (-2 height: keeps weapons centered)
+ cal fireany ;fire from multiple position
+ pop af ;number of multiples
+ dec a ;one just displayed
+ ret z ;return if none left
+ ld hl,(your_prevpos+30)
+ psh af
+ dec h
+ cal fireany
+ pop af
+ dec a
+ ret z
+ ld hl,(your_prevpos+44)
+ psh af
+ dec h
+ cal fireany
+ pop af
+ dec a
+ ret z
+ ld hl,(your_prevpos+58)
+ psh af
+ dec h
+ cal fireany
+ pop af
+ dec a
+ ret
+
+Fire_bullet:
+ ld hl,just_fired
+ ld a,(hl) ;just_fired
+ cp 5 ;already pressed?
+ ret z ;return when already pressed (=5)
+
+ inc (hl) ;otherwise increase counter (0 to 4 >> 1 to 5)
+ ld a,(your_weapon) ;if you have bullets.....
+ cp maxweapon
+ jr nc,fireOK ;>weapons = laser
+ ld (hl),5 ;.....then can't fire next turn (go to 5 imm.)
+
+fireOK:
+ ld a,(your_multiples) ;any multiples?
+ and %111 ;nr. of multiples
+ cal nz,fire_multiples ;if >0 then fire them too
+ ld hl,(x) ;fire from ship position (x)
+fireany: ;HL=(x,y)
+ ld (firex),hl ;set position to fire from
+ ld a,(your_weapon)
+ ld ix,weapondata+2-(256*3)
+ add a,a ;weap*2
+ add a,a ; *4
+ add a,a ; *8
+ ld c,a
+ ld b,3 ;go to current weapon (bc=a), b=3 :P
+ add ix,bc
+
+fire_weapon: ;b=3
+ psh bc ;save counter
+ ld a,(ix) ;load this weapon
+ cp %11100000 ;%11110000=laser
+ cal z,fire_laser ;fire laser (will set a=0 when done)
+ or a ;<>0=bullet
+ cal nz,fire_ybullet
+ inc ix
+ inc ix
+ pop bc ;weapon counter (do 3 weapons)
+ dnz fire_weapon
+
+fire_tail:
+ ld hl,your_tail
+ ld a,(hl)
+ dec a
+ ret nz
+ ld a,(ix-2) ;last weapon fired
+ cp %11100000 ;issit laser
+ ret z ;then return
+ xor %11111 ;smart way of going left instead of right :P
+ jr fire_ybullet ;fire tail bullet and return
+
+;-----fire LASER-----
+
+fire_laser:
+ ld b,0 ;overflow counter
+ ld hl,firex
+ ld d,(hl) ;d = your x-pos
+ inc hl
+
+ ld a,(hl) ;base y-coord (firey)
+ add a,(ix+1) ;at specified offset (most likely the middle)
+ ld e,a ;save laser-y in e
+ psh de ;save unmodified (x,y)
+ add a,a ;y*2
+ add a,a ;y*4
+ add a,a ;y*8
+ rl b ;b (=0) =b*2+overflow (if y>32 then bc=bc+256)
+ add a,a ;y*16 (width of screen)
+ rl b ;b=b*2+overflow (if y>64 then bc=bc+512)
+ inc a ;8 pixels to right (a=even so no overflow)
+
+ srl d ;X/2
+ srl d ;X/4
+ srl d ;X/8
+ add a,d ;a = (Y*16+X/8) mod 256 (c set on overflow)
+
+ jr nc,_nolc ;jump if no carry = no overflow = a<=255
+ inc b ;a>255 so increase bc by 256
+_nolc: ld c,a ;c = (Y*16+X/8) mod 256
+ ld hl,dispbuffer ;save-location
+ add hl,bc ;bc = Y*16+X/8: hl=screen address
+ ld a,15 ;128/8=16=screen width ** minus one (inc a ^^)
+ sub d ;minus x-start (d=X/8)
+ ld b,a
+drawlaser:
+ ld (hl),%11111111
+ inc hl ;Go to next byte
+ dnz drawlaser
+handle_laser:
+ pop de ;de=(firex): x-pos unmodified
+
+check_laserhits: ;de = (x,y)
+ psh ix
+ ld b,nrenemies ;check all enemies
+ ld hl,enemies+1 ;enemy#1+occ/hp00
+laserhits: ;hits with normal enemies
+ psh hl
+ ld a,(hl) ;occ+hp00
+ and %00000010 ;normal/moving occ.=%1x
+ jr z,nolashit ;no hit when enemy_occ <> 2/3
+ inc hl ;enemy type
+ ld a,(hl)
+ or a ;enemy #0 = pickup
+ jr z,nolashit ;yes: don't destroy
+
+ cal find_sprite ;ix=sprite to enemy (hl)
+ inc hl
+ ld a,(hl) ;check x
+ sub d
+ jp m,nolashit ;no hit when enemy is left of you
+ inc hl
+ ld a,(hl) ;check y
+ sub e
+ jr z,enemy_lashit ;a-e=0 = laser on top line of enemy = hit
+ jr nc,nolashit ;a-e>0 = enemy above laser = no hit
+ dec a ;minus one
+ add a,(ix+1) ;add enemy height (according to sprite @ix)
+ jp m,nolashit ;a-e>0 = hit
+enemy_lashit:
+ ld a,(curweapdamage) ;damage
+ cal enemy_hit ;hl=enemy+y
+nolashit:
+ pop hl ;enemy+1
+ ld a,b ;psh bc
+ ld bc,enemysize
+ add hl,bc ;go to next enemy
+ ld b,a ;pop bc
+ dnz laserhits ;check all enemies
+ xor a ;a=0 otherwise weird things might happen :P
+ ld (weapincs),a ;reset damage
+ pop ix
+ ret
+
+;-----fire BULLETs-----
+
+fire_ybullet: ;fire bullet type=C dam=(curweapdamage) at (firex/y)
+ ld c,a ;save bulletType in c
+ ld hl,ybullets ;check for unused bullet
+ ld de,4
+ ld b,nrybuls
+find_ybullet:
+ ld a,(hl)
+ or a
+ jr z,found_ybullet ;0 = no bullet here
+ add hl,de
+ dnz find_ybullet ;look next bullet
+ ret ;none found, return don't fire
+
+found_ybullet:
+ ld (hl),c ;use the bullet and set correct bullet-type
+ inc hl ;@damage
+ ld (hl),1 ;set bullet damage
+curweapdamage =$-1
+ ld a,(firex) ;your x-pos
+ add a,5 ;place bullet in front of you
+ inc hl ;go to bullet-x
+ ld (hl),a ;set x
+
+ ld a,(firey) ;your y-pos
+ add a,(ix+1) ;place bullet at the middle of your ship
+ inc hl ;go to bullet-y
+ ld (hl),a ;set y
+
+ xor a
+ ld (weapincs),a ;reset damage
+ ret
+
+;------------------------ handle bullets ------------------------------------
+
+bullet_left:
+ ld c,a ;c=type
+ and %11111 ;pixels to move
+ add a,(hl) ;a = X + (hl) to the right
+ sub 16 ;and 16 to the left (so -16..+15)
+ jr c,remove_bullet ;remove if x<0
+ cp 128
+ jr nc,remove_bullet ;or x>=128
+ ld (hl),a ;save new pos.
+ ld d,a ;d = X
+
+ inc hl ;@y-pos
+ ld a,c
+ cal _shracc ;%11110000->1111
+ srl a ;%1110->111
+ dec a
+ jr z,bullet_noymove ;1=straight forward
+ dec a
+ jr z,bullet_up ;2=up
+ dec a
+ jr z,bullet_halfup ;3=1/2up
+ dec a
+ jr z,bullet_down ;4=down
+
+bullet_halfdown: ;5=1/2down
+ ld a,(timer)
+ and 1
+ jr z,bullet_noymove
+bullet_down:
+ ld a,(hl)
+ inc a
+ cp 55
+ jr z,bullet_noymove
+ ld (hl),a
+bullet_halfup:
+ ld a,(timer)
+ and 1
+ jr z,bullet_noymove
+bullet_up:
+ ld a,(hl)
+ dec a
+ jr z,bullet_noymove
+ ld (hl),a
+bullet_noymove:
+ ld e,(hl) ;e = Y
+ ret
+
+remove_bullet:
+ pop hl ;cal bullet_left
+ pop hl ;enemy+type
+ ld (hl),0 ;dump this bullet!
+ jr next_ybullet+1 ;+1:skip pop hl at next_ybullet
+
+Handle_bullets:
+ ld hl,ybullets
+ ld b,nrybuls
+scan_bullets:
+ psh bc ;bullet counter
+ psh hl ;save enemy+type
+ ld (temp1),hl ;needed for check_bullethits
+ ld a,(hl) ;@bulletType
+ inc hl ;@damage
+ inc hl ;@x
+
+ or a
+ jp z,next_ybullet ;bulletType=0 >> no bullet
+ cal bullet_left ;move bullet left
+
+display_bullet:
+ psh de ;save de =position
+ dec hl ;@x
+ dec hl ;@damage
+ ld a,(hl) ;bullet damage=size
+ ld hl,bullettable ;pointer to first bullet
+ srl a
+ srl a ;per 4
+ ld d,0
+ ld e,a ;->16bit (de=a)
+ add hl,de ;point to correct bullet offset
+ ld a,(hl) ;load bullet offset
+ ld e,a ;convert to 16bit (d=0)
+ ld ix,spr_bullet01 ;first sprite
+ add ix,de ;add offset (go to correct sprite)
+ pop de ;saved position
+
+ ld a,(ix) ;bullet x-size
+ ld (bulletxsize),a ;used at check_bullethits
+ ld a,(ix+1) ;bullet y-size...
+ ld (bulletysize),a ;...too
+
+ cal safeputsprite ;display bullet; DE used for check_bullethits
+
+ cal check_bullethits
+
+next_ybullet:
+ pop hl ;restore enemy+type
+ inc hl
+ inc hl
+ inc hl
+ inc hl ;skip type,dam,x,y: next enemy+type
+ pop bc ;b=counter
+ dnz scan_bullets ;next bullet (loop)
+ ret
+
+;--------------------------- check bullethits -------------------------------
+
+check_bullethits: ;INPUT: de=X,Y; (temp1)=bullet
+ ld b,nrenemies
+ ld hl,enemies+1
+
+hit_enemies: ;Hits with normal enemies
+ psh bc ;enemy counter
+ psh hl
+
+ ld a,(hl)
+ and %00000010
+ jr z,nohit ;no hit when enemy_occ <> 2/3
+
+ inc hl ;enemy type
+ ld a,(hl)
+ or a ;enemy #0 = pickup
+ jr z,nohit ;yes: don't destroy
+
+ cal find_sprite ;set ix to the sprite of this enemy
+
+ inc hl ;@x
+ ld a,(hl) ;check x
+ sub d ;minus bullet x-position
+ ld b,a ;psh a
+ sub 5 ;minus bullet x-size
+bulletxsize =$-1
+ jp p,nohit ;miss
+ ld a,b ;pop a
+ add a,(ix) ;add enemy width
+ jp m,nohit ;miss
+
+ inc hl ;@y
+ ld a,(hl) ;check y
+ sub e ;minus bullet y-position
+ ld b,a ;psh a
+ sub 3 ;substract bullet height
+bulletysize =$-1
+ jp p,nohit ;nope, missed it
+ ld a,b ;pop a
+ add a,(ix+1) ;add enemy height
+ dec a ;minus one
+ jp m,nohit ;missed after all
+
+ ;---bullet hits enemy (auch-time!)---
+ psh hl
+ ld hl,0 ;@bulletType
+temp1 =$-2
+ ld (hl),0 ;remove bullet
+ inc hl ;@damage
+ ld a,(hl) ;set damage
+ pop hl ;enemy+y
+ cal enemy_hit
+nohit:
+ pop hl
+ ld bc,enemysize
+ add hl,bc
+ pop bc
+ dnz hit_enemies ;check next enemy
+ ret
+
+enemy_hit: ;*in:a=damage;hl=enemy+y
+ add a,a ;a=damage to inflict
+ add a,a ;first 2 bits used for occ.
+ ld b,a
+
+ dec hl ;@x
+ dec hl ;@type
+ dec hl ;@hp00 (occ)
+ ld a,(hl) ;load hp00
+ sub b ;decrease HP (if <0xx then c is set)
+ ld (hl),a ;save (no flag-changes)
+ dec hl ;@hp64; no change in c
+ ld a,(hl) ;load; no c-change
+ sbc a,0 ;if cf then decrease a
+ ld (hl),a ;save back the new value
+ ret nc ;if a>=0 then return, otherwise explode
+
+ inc hl ;goto occ again
+ ld (hl),%01 ;set to explode
+ ld a,(pickuptimer) ;counts enemies destroyed
+ dec a ;enough destroyed for a pickup?
+ jr nz,pickupdone ;otherwise just explode
+ ld (hl),%110 ;change it into a pickup (with 2 HP)
+ ld a,18 ;reset enemies counter (18 hits = next)
+pickupdone:
+ ld (pickuptimer),a ;save new enemiescounter value
+ inc hl
+ ld (hl),$00 ;explosionFrame 0
+
+ ld hl,1 ;increase score by one
+ jp scoreInc ;+ret
+
+;--------------------------- level events -----------------------------------
+
+Level_event:
+ ld hl,nextevent ;time to next event <ld a,(nextevent)
+ dec (hl) ;decrease counter <dec a
+ ld a,(hl) ;look at counter <ld (nextevent),a
+ or a ;has it reached zero?
+ ret nz ;nope: get outta here!
+
+ ld bc,0 ;enemy frequency (lvl)
+eventtime =$-2
+ cal Random
+ ld (nextevent),a ;set time to next event
+ ld hl,eventleft
+ dec (hl) ;update enemy-counter
+
+ ld a,(hl) ;look at counter
+ or a ;has it reached 0?
+ jp z,Next_level ;yes: level finished
+ dec a ;has it reached 1?
+ jr z,standby_event ;yes: wait until no enemies present/left
+ dec a ;has it reached 2?
+ jr z,place_boss ;yep: place the BigBossTM!
+ dec a ;has it reached 3?
+ jr nz,do_event ;nope: >3 = place an enemy
+ inc hl ;nextevent located behind eventleft
+ ld (hl),123 ;set delay
+ ret ;don't place any more enemies
+
+standby_event:
+ ld b,nrenemies
+ ld hl,enemies+1-enemysize
+ ld de,enemysize
+chk_enemyleft:
+ add hl,de
+ ld a,(hl)
+ or a ;0 = no enemy present
+ jr nz,enemyleft
+ dnz chk_enemyleft
+ ret
+enemyleft:
+ ld hl,eventleft
+ inc (hl)
+ ret
+
+
+place_boss:
+ ld a,1
+ ld (nrlvlenemies),a ;just one enemy: the BOSS
+ ld hl,(levelp) ;the leveldata (including the boss)
+ dec hl ;points to leveldata\boss\enemynr
+ ld a,(hl) ;load it
+ ld (lvlenemies),a ;set new enemy (boss)
+
+do_event:
+ ld hl,enemies+1-enemysize
+ ld bc,enemysize
+ xor a ;a=0
+chk_noenemy:
+ add hl,bc
+ cp (hl) ;(hl) = 0 ??
+ jr nz,chk_noenemy ;jump if enemy present (non-0)
+ ex de,hl ;de=hl=usable enemy
+
+place_enemy: ;de = enemy+1
+ ld bc,0 ;0..nrlvlenemies
+nrlvlenemies =$-1 ;=nr of enemies minus 1
+ cal Random ;random enemy b..b+c = 0..nrenemies-1
+ ld b,0
+ ld c,a ;bc=a
+ ld hl,lvlenemies
+ add hl,bc ;go to a random enemy
+ ld a,(hl) ;load enemy nr of this mysterious random enemy
+ cal findenemyspecs ;hl = enemy #a specs
+
+ dec de ;goto hp64 (before occ)
+ ldi ;set hp64
+ ldi ;set hitpoints+occ of enemy class
+ ld a,(hl) ;save sprite-offset/2 (ldi decs bc so in a)
+ ldi ;set sprite
+ ldi ;set x-position
+
+ ld c,a ;c=sprite
+ ld a,(hl) ;load placeInfo
+ inc hl
+ dec a ;is it 1?
+ jr z,random_enemy ;yes: create random value <51 in a
+ dec a ;is it 2?
+ jr z,lure_enemy ;yes: create a 100% luring enemy
+ ;otherwise?
+halflure_enemy: ;yes (of course it is): pick one (50% lure)
+ ld a,(timer) ;look at frame-number
+ and %00000001 ;make random if odd frame nr.
+ jr nz,random_enemy ;1st possibility: random enemy
+lure_enemy: ;2nd possibility: luring enemy
+ ld a,(y) ;place at same y-pos as YOUR ship
+ jr ypos_OK
+random_enemy:
+ ld b,0 ;bc = enemy sprite offset / 2
+ ld ix,spr_enemy00 ;first enemy sprite
+ add ix,bc ;add offset for current enemy
+ add ix,bc ;twice (offset stored as offset/2)
+ ld a,64-8 ;=57=screen height (8 is scorebar)
+ sub (ix+1) ;minus sprite height=bottom
+ ld c,b ;range=0 to...
+ ld b,a ;...57-y
+ cal Random ;random value on screen
+ypos_OK: ;random value successfully created
+ ld (de),a ;save y-position
+ inc de ;@movecounter
+ ldi ;set move-type
+ ld a,1 ;movecounter = 1
+ ld (de),a ;set
+ inc de ;@firecounter
+ ldi ;set time-to-1st-fire
+ ldi ;set firefreq
+ ret ;return
+
+;--------------------------- enemy fires ------------------------------------
+
+Enemy_fires: ;de = x,y
+ dec d
+ dec d ;d = x-2
+ inc e ;e = y+1
+
+ ld b,nrebuls
+ ld hl,ebullets
+find_ebullet:
+ ld a,(hl)
+ or a
+ jr z,found_ebullet ;0 = not used
+ inc hl \ inc hl \ inc hl
+ dnz find_ebullet ;look next bullet
+ ret
+
+found_ebullet:
+ ld b,%1100
+ ld a,(level_info)
+ and %00001000
+ jr z,bulletok
+
+ ld a,(y)
+ sub e
+ add a,10
+ jp p,bulletnotup
+ ld b,%1011 ;yourY-bulY = negative (=bullet below you)
+ add a,10
+ jp p,bulletnotup
+ ld b,%1001 ;yourY-bulY = even more negative (going up)
+
+bulletnotup:
+ sub 20
+ jp m,bulletok
+ ld b,%1010 ;bullet going down
+ sub 10
+ jp m,bulletok ;even more going down
+ ld b,%1000
+
+bulletok:
+ ld a,(level_info)
+ and %11110000
+ or b
+ ld (hl),a ;set bullet direction
+ inc hl
+ ld (hl),d ;set x-pos
+ inc hl
+ ld (hl),e ;set y-pos
+ ret
+
+;----------------------------- enemy bullets --------------------------------
+
+Enemy_bullets:
+ ld hl,ebullets
+ ld b,nrebuls
+handle_bullet:
+ psh bc
+ psh hl
+ ld a,(hl) ;load bulletType in a
+ and %1111 ;select direction-bits
+ jr nz,enemy_bullet ;non-0: handle bullet
+next_bullet:
+ pop hl ;do not move the <pop hl>
+ pop bc
+ inc hl \ inc hl \ inc hl
+ dnz handle_bullet
+ ret
+
+enemy_bullet:
+ ld b,a ;save type&%1111
+ inc hl ;bullet x
+ ld a,(hl) ;check if it has reached the left side of scrn
+ bit 7,a ;x<0?
+ jr nz,remove_ebullet ;yes, remove bullet
+ dec (hl) ;move one left
+ dec (hl) ;and another one
+ ld d,(hl) ;d=x
+ inc hl ;@y
+
+ ld a,b ;restore type
+ cp %1100 ;is it a normal bullet? (cp = faster than bit)
+ jr z,ebullet_common ;type %1100: normal bullet
+ and %111 ;isolate important bits
+ jr z,ebullet_down ;type %1000: moving down
+ dec a
+ jr z,ebullet_up ;type %1001: moving up
+ ld b,a
+
+ ld a,(timer)
+ rra
+ jr c,ebullet_common
+
+ ld a,b
+ dec a
+ jr z,ebullet_down ;type %1010: moving down 50%
+ ;type %1011: moving up 50%
+ebullet_up:
+ ld a,(hl)
+ dec a
+ jp m,ebullet_common
+ ld (hl),a
+ jr ebullet_common
+
+ebullet_down:
+ ld a,(hl)
+ inc a
+ cp 55
+ jr z,ebullet_common
+ ld (hl),a
+
+ebullet_common:
+ ld e,(hl) ;e=y
+ ld ix,spr_bullete1 ;display enemy bullet
+ cal putsprite
+
+ebullet_hits:
+ ld a,(your_occ)
+ or a
+ jr nz,next_bullet ;0 = you're normal
+
+ pop hl
+ psh hl
+ inc hl ;check x
+ ld a,(x)
+ sub (hl)
+ add a,6
+ jp m,next_bullet
+ cp 9
+ jr nc,next_bullet
+
+ inc hl ;check y
+ ld a,(y)
+ sub (hl)
+ add a,6
+ jp m,next_bullet
+ cp 9
+ jr nc,next_bullet
+
+ pop hl ;points to bullettype again
+ psh hl ;and save it again (ivm call to damage_you)
+ ld a,(hl) ;load bullettype
+ cal _shracc ;isolate damage-bits (%1111???? -> %00001111)
+ ld b,a ;set damage-amount
+ cal damage_you ;HIT!!
+remove_ebullet:
+ pop hl ;hl could be destroyed by damage_you
+ ld (hl),0 ;bullet > unused
+ jr next_bullet+1 ;next bullet (SKIP THE <POP HL> = one byte)
+
+;--------------------------- handle enemies ---------------------------------
+
+Handle_enemies:
+ ld hl,enemies+1
+ ld b,nrenemies ;handle all enemies
+
+handle_enemy:
+ psh bc
+ psh hl
+
+ ld a,(hl)
+ and %00000011
+ jr z,next_enemy ;occ "no enemy" 0
+ dec a
+ jr z,exploding_enemy ;occ "exploding" 1
+
+normal_enemy:
+ inc hl
+ ld c,(hl) ;c = enemy type = de
+ cal find_sprite
+
+ inc hl
+ ld d,(hl) ;x
+ inc hl
+ ld e,(hl) ;y
+
+ inc hl ;@movetype
+ cal moving_enemy
+ dec hl
+
+ ld a,e ;new y value
+ cp 57
+ jr c,enemyonscreenY ;=on screen
+ cp -20 ;moved off at top
+ ld e,0 ;reset to top
+ jr nc,enemyonscreenY
+ ld e,57 ;otherwise reset to bottom
+enemyonscreenY:
+ ld (hl),e ;store new y
+ dec hl ;@x
+
+ ld a,d ;new x value
+ cp 128 ;x<128
+ jr c,enemyonscreenX ;=on screen
+ cp -7 ;x<=-8
+ jr c,remove_enemy ;=off screen
+enemyonscreenX:
+ ld (hl),d ;store new x
+ ld a,c ;a = enemy type
+ or a ;type 0? (pickup)
+ jr nz,check_enemyfire ;no, a normal enemy; let em fire
+ ld a,(timer) ;load time
+ and %1 ;move left once every 2 turns
+ jr z,firing_done ;don't move now
+ inc d ;increase x-position (don't move this turn)
+ inc (hl) ;and save it
+ jr firing_done ;continue
+
+check_enemyfire:
+ inc hl ;@y
+ inc hl ;@movetype
+ inc hl ;@movecount
+ inc hl ;@firecount
+ dec (hl) ;decrease counter till next blast
+ ld a,(hl) ;&&&doesn't seem efficient to me
+ or a ;has it reached zero?
+ jr nz,firing_done ;finished if not
+
+ inc hl ;@firefreq
+ ld a,(hl)
+ dec hl
+ ld (hl),a ;reset counter for next blast
+ psh de ;save registers for firing-use
+ cal Enemy_fires ;fires bullet
+ pop de ;restore (destroyed by Enemy_fires)
+firing_done:
+ cal putwidesprite ;display sprite @ix
+
+next_enemy:
+ pop hl
+ ld bc,enemysize
+ add hl,bc
+ pop bc
+ dnz handle_enemy
+ ret
+
+remove_enemy:
+ pop hl
+ ld (hl),$0000 ;bye bye enemy
+ jr next_enemy+1 ;continue AFTER pop hl (already done)
+
+exploding_enemy:
+ inc hl
+ ld a,(hl)
+ cp 16
+ jr z,remove_enemy ;remove when at last frame
+ inc a
+ ld (hl),a ;next frame
+ dec a ;1-16 -> 0-15
+ ld ix,spr_explosion ;base sprite
+ cal explosion_stuff ;display explosion
+ jr next_enemy
+
+;--------------------------- moving enemies ---------------------------------
+
+moving_enemy:
+ ld a,(hl) ;how does this enemy move?
+ bit 7,a ;direction indicator
+ jr nz,moveright
+moveleft:
+ dec d
+ jr moveXdone
+moveright:
+ inc d
+moveXdone:
+ res 7,a ;01111111=movetype:
+ and a
+ ret z ;0 = don't move
+ dec a
+ jr z,movetype_updown ;1 = 0 >< up / down
+ dec a
+ jr z,movetype_vslow ;2 = .75 >>
+ dec a
+ jr z,movetype_slow ;3 = .5 >>
+ dec a
+ jr z,movetype_fast ;4 = .5 <<
+ dec a
+ jr z,movetype_vfast ;5 = 1 <<
+ dec a
+ jr z,movetype_smart ;6
+ dec a
+ jr z,movetype_lure ;7 = 1 >> move y towards you
+ dec a
+ jr z,movetype_slowlure ;8 = 1 >> lure 1/2 speed
+ dec a
+; jr z,movetype_stoplure ;9 = 1 >> slowlure; stop at x=99
+; dec a
+; jr z,movetype_fulllure ;10 = x+y towards you 1/2 speed
+
+movetype_fulllure:
+ ld a,(timer)
+ and 1
+ ret z
+ cal movetype_lure
+ ld a,(x)
+ cp d
+ ret c ;move left (already did)
+lure_right:
+ inc d ;already moved left, so move right
+moverightonce:
+ inc d ;twice (=+1)
+ ret
+
+movetype_slowlure:
+ ld a,(timer)
+ and 1 ;half the time
+ jr z,moverightonce ;dont move at all (compensate move left +ret)
+movetype_lure:
+ ld a,104
+ cp d
+ jr c,dothelurethingy
+ inc d ;x<106: full stop
+dothelurethingy:
+ ld a,(y)
+ cp e
+ ret z ;don't move if equal
+ jr c,lure_up ;below you then move up
+lure_down: ;above then move down
+ inc e
+ ret
+lure_up:
+ dec e
+ ret
+
+movetype_smart:
+ inc hl ;hl =@ <move>
+ ld a,(timer)
+ and %1111 ; |
+ ld a,(hl) ;&&& \|/
+ jr nz,smartupdate
+ inc a
+smartupdate:
+ ld (hl),a
+
+ or a ;reset carry flag
+ dec hl ;reset hl to <y>
+ and %11111100
+ jr z,movetype_fast
+
+movetype_slow:
+ ld a,(timer)
+ and %1
+ ret z
+movetype_vslow:
+ ld a,(timer)
+ and %11
+ ret z
+ inc d
+ ret
+
+movetype_fast:
+ ld a,(timer)
+ and %1
+ ret z
+movetype_vfast:
+ dec d ;move left
+ ret nz ;finished
+ pop hl ;restore stack (no ret used)
+ jp remove_enemy ;remove this enemy (off screen)
+
+movetype_updown:
+ inc hl ;@ <move>
+ ld a,(hl)
+ dec a
+ jr nz,move_updated
+ add a,128
+move_updated:
+ ld (hl),a
+
+ or a ;reset carry flag
+ dec hl ;@ <y>
+ and %00100000
+ ld a,(hl);&&&ld a,e ;load current y-position
+ jr z,movedown
+
+moveup: dec a ;decrease y-pos (=move up)
+ ret m ;don't move off the screen (y<0)
+ dec e ;save new y-pos
+ ret ;finish
+movedown:
+ inc a ;increase y-pos
+ cp 55 ;compare with bottom
+ ret nc ;return if it has passed that line (>40)
+ inc e ;otherwise save new position
+ ret ;and return
+
+;--------------------------- check collision --------------------------------
+
+Enemies_hit:
+ ld hl,(x) ;e = X, d = Y
+ ld de,$0707 ;add 7 to both d and e
+ add hl,de
+ ld d,h
+ ld e,l ;e = X+7, d = Y+7
+
+ ld hl,enemies+1
+ ld b,nrenemies ;check all 20 enemies
+check_collision:
+ psh hl
+ ld a,(hl)
+ and %00000010
+ jr z,check_next ;2 or 3 = ok
+ inc hl
+
+collide_enemy: ;&&& include in Handle_enemy proc
+ cal find_sprite
+
+ inc hl
+ ld a,(hl) ;check x match
+ sub e ;enemy position minus yours minus 7
+ jp p,check_next
+ add a,6
+ add a,(ix) ;enemy width
+ jp m,check_next
+
+ inc hl
+ ld a,(hl) ;check y match
+ sub d ;same as with x-check
+ jp p,check_next
+ add a,6
+ add a,(ix+1) ;enemy height
+ jp m,check_next
+ dec hl
+ dec hl
+
+take_pickup:
+ psh hl ;we need hl
+ ld hl,2 ;increase score by 2
+ cal scoreInc
+ pop hl ;we're done
+
+ ld a,(hl) ;load enemy type
+ or a
+ jr nz,collide ;enemy when <>0
+
+ psh hl
+ ld hl,your_pickup ;your pickups
+ ld a,(hl) ;current
+ inc a ;go to next
+ cp 6 ;pickups >=6
+ jr c,not_maxpickup
+ ld a,1 ;yes: reset to pickup 1
+not_maxpickup:
+ ld (hl),a ;save new
+ cal disp_icons ;display altered pickupicons
+ pop hl
+
+ dec hl ;to enemy occ
+ xor a ;set to 0 = gone
+ ld (hl),a ;remove
+ jr check_next ;all done, next..
+
+destroy_enemy:
+ ld (hl),%01 ;set to explode
+ inc hl
+ ld (hl),0 ;explosionFrame 0
+ jr collide_done
+
+collide:
+ dec hl
+ ld a,(hl)
+ and %11111100
+ jr z,destroy_enemy
+ ld a,(hl)
+ sub collidedamage
+ ld (hl),a
+collide_done:
+ ld b,collidedamage ;damage
+ cal damage_you
+
+check_next:
+ pop hl
+ ld a,b ;psh bc
+ ld bc,enemysize
+ add hl,bc
+ ld b,a ;pop bc
+ dnz check_collision
+ ret
+
+;--------------------------- story ------------------------------------------
+
+storyPage:
+ psh hl
+ cal _clrLCD
+ pop hl
+storyLine:
+ inc hl
+ ld e,(hl)
+ inc hl
+ ld d,(hl)
+ ld (_penCol),de
+ inc hl
+ cal _vputs
+
+ ld a,(hl)
+ dec a
+ jr z,storyLine
+
+ psh hl
+ ld hl,VIDEO_MEM ;copy text
+ ld de,dispbuffer ;to GRAPH_MEM
+ ld bc,1024 ;entire screen
+ ldir
+ cal _clrLCD
+ pop hl
+
+ inc hl
+ ld a,(hl)
+ inc hl
+ ld b,(hl)
+ psh hl
+ cal DoSFX
+ cal _getkey
+ pop hl
+ ret
+
+dostory:
+ cal storyPage ;do some story
+ inc hl ;look at next hl
+ ld a,(hl) ;load in a
+ dec hl ;restory hl
+ inc a ;set z-flag if a = $ff
+ jr nz,dostory ;otherwise loop
+
+ ld bc,2+1 ;story ends
+ add hl,bc ;set hl to beginning of the level
+ ld (levelp),hl ;set the level-pointer
+ ret ;and return
+
+;--------------------------- SFX --------------------------------------------
+
+DoSFX: ;in:a=beginLine;b=nrOfLines
+ ld (curline),a
+SFXframe:
+ psh bc
+
+ ld a,0 ;get line number
+curline =$-1
+ inc a ;go to the next line
+ ld (curline),a ;update
+
+ ld l,a
+ ld h,0 ;hl=a
+ add hl,hl
+ add hl,hl
+ add hl,hl
+ add hl,hl ;*16 (a pixels down=a*16)
+
+ ld b,h ;save hl for later
+ ld c,l
+
+ ld de,VIDEO_MEM ;where to put sfx
+ add hl,de ;go to ymin
+ ex de,hl ;put into de again
+
+ ld hl,dispbuffer
+ add hl,bc ;hl->logo
+
+ sub 64 ;a=a-64
+ neg ;a=64-a (lines from bottom)
+
+; ld c,a ;c=a=(curline)
+; ld a,64
+; sub c ;lines from bottom
+
+SFXdisp: ;display this frame on screen
+ ld bc,16 ;one line (=16 bytes, you'd know by now)
+ ldir ;display (copy actually)
+ ld bc,-16 ;go up one line (not on screen)
+ add hl,bc ;so the same line will be displayed
+ dec a ;counter
+ jr nz,SFXdisp ;repeat until whole screen is displayed
+
+ ld b,8
+SFXdelay:
+ halt ;delay
+ dnz SFXdelay ;8x
+
+ pop bc ;counter
+ dnz SFXframe
+ ret
+
+;--------------------------- show icon --------------------------------------
+
+drawline:
+ ld (hl),a ;draw one piece of the divider-line
+ inc hl ;move right (8 pixels = 1 byte)
+ dnz drawline ;repeat (16bytes * 8pixels =128= screen width)
+ ret
+
+disp_icons:
+ psh bc \ psh de \ psh hl \ psh ix ;&&&
+
+ ld hl,VIDEO_MEM+(16*56);56 rows down = eight rows from bottom
+ ld (PutWhere),hl ;place icons at bottom of normal screen
+ ld b,16 ;draw 16x (screen width)
+ ld a,%11111111 ;horizontal line mask
+ cal drawline ;draw divider-line
+
+ ld b,16*7 ;draw 16x (screen width) 7x (height)
+ xor a ;blank line mask
+ cal drawline ;clear scorebar
+
+disp_lives:
+ ld de,5 ;(0,5)
+ ld a,(your_lives) ;nr of lives
+ or a
+ jr z,displivesdone ;no lives
+ ld b,a
+displivesloop:
+ psh bc
+ ld ix,spr_lship
+ cal safeputsprite ;put li'l ship
+ ld a,lshipsize+1
+ add a,d
+ ld d,a ;x=x+5
+ pop bc
+ dnz displivesloop ;one ship per life
+displivesdone:
+
+ ld ix,spr_icon01 ;armorIcon
+ ld de,$1901 ;icon #1
+ cal putwidesprite ;display icon
+ cal disp_armor ;display bar
+
+ ld ix,spr_icon00
+ ld a,(your_weapon) ;ur weapon
+ cp maxweapon ;laser?
+ psh af ;(your_weapon)
+ jr nc,no_tail ;if laser (nc) then tail ain't fired
+ ld a,(your_tail)
+ or a
+ jr z,no_tail
+ ld ix,spr_icon02 ;tailbeamIcon
+no_tail:
+ ld de,$2901 ;icon #2
+ cal putwidesprite ;display
+
+ ld ix,spr_icon00
+ pop af ;a=(your_weapon); cf=bullets
+ psh af
+ jr nc,no_bullets ;=laser
+ ld hl,$3945 ;position to display bullet-type digit
+ pop af ;digit=(your_weapon)
+ psh af
+ inc a ;1 = weapon #1 (=0)
+ ld (_penCol),hl ;set location
+ add a,'0' ;make digit
+ cal _vputmap ;display char
+ ld ix,spr_icon03 ;bulletIcon
+no_bullets:
+ ld de,$3901 ;icon #3
+ cal putwidesprite ;display icon
+
+ ld ix,spr_icon00 ;emptyIcon
+ pop af ;ld a,(your_weapon)
+ ld b,a
+ jr c,no_laser ;popped carry
+ ld hl,$3955 ;position to display bullet-type digit
+ ld (_penCol),hl ;set location
+ ld a,b ;(your_weapon) ;load = faster than push
+ sub maxweapon-1 ;1 = laser #1 (=maxweapon)
+ add a,'0' ;make digit
+ cal _vputmap ;display char
+ ld ix,spr_icon04 ;laserIcon
+no_laser:
+ ld de,$4901 ;icon #4
+ cal putwidesprite
+
+ ld ix,spr_icon00 ;emptyIcon
+ ld a,(your_multiples)
+ and %111
+ jr z,no_multiples
+ ld ix,spr_icon05
+no_multiples:
+ ld de,$5901 ;icon #5
+ cal putwidesprite
+
+ ld ix,spr_dividerline
+ ld de,$6901
+ cal putwidesprite
+
+ ld a,(your_pickup) ;pickups taken
+ add a,a ;picks*2 (sets z-flag)
+ jr z,iconsdone ;return if no pickups
+ add a,a ;picks*4
+ add a,a ;picks*8
+ add a,a ;picks*$10
+ add a,$09 ;add 0ah
+ ld d,a ;y-pos = picks * $10 + $0a (19,29,39,49,59)
+ ld e,$01 ;x-pos = bottom (1a01,2a01,3a01,4a01,5a01)
+
+ ld ix,spr_icon
+ cal putwidesprite
+iconsdone:
+ ld hl,dispbuffer ;normal game-screen
+ ld (PutWhere),hl ;set sprite-position to normal screen
+
+ pop ix \ pop hl \ pop de \ pop bc
+ ret
+
+disp_armor:
+ ld de,16 ;line size
+ ld hl,(57*16)+VIDEO_MEM+3
+ ld b,3
+armorbarclr:
+ dec hl
+ ld (hl),0
+ add hl,de
+ ld (hl),0
+ sbc hl,de
+ dnz armorbarclr
+
+ ld a,(your_armor) ;load your armor (<25)
+ ld c,a ;psh a
+ srl a ;/2
+ srl a ;/4
+ srl a ;/8: don't display last 3 bits of a (later)
+ jr z,noarmorbar ;if a=0 then it would loop 256x so skip it
+ ld b,a ;loop b=a times
+armorbar: ;starting at ($39*16)+VIDEO_MEM
+ ld (hl),%11111111 ;draw a piece of the bar
+ add hl,de ;one down (resets carry)
+ ld (hl),%11111111 ;same piece
+ sbc hl,de ;up again
+ inc hl ;next position
+ dnz armorbar ;loop it b times
+
+noarmorbar:
+ ld a,c ;pop a
+ and %111 ;display last bits of armor
+ ret z ;if armor=0 then bit = %00000000 (don't disp)
+ ld b,a ;into B
+ xor a ;bit = %00000000
+armorbarbit:
+ scf ;set carry flag
+ rra ;rotates A right and sets bit 7 (c-flag)
+ dnz armorbarbit ;repeat B times (so if B=6 then a=%11111100)
+armorbarready: ; (an if B=3 then a=%11100000)
+ ld (hl),a ;draw this last byte
+ add hl,de
+ ld (hl),a ;and just below
+ ret
+
+disp_charge:
+ ld hl,(59*16)+VIDEO_MEM+3
+ ld b,3
+chargebarclr:
+ dec hl
+ ld (hl),0
+ dnz chargebarclr
+
+ ld a,(weapincs) ;load bar size (0-80)
+ srl a ;half the size (0-40)
+ srl a ;again half that size (0-20 pixels)
+ ld c,a ;psh a
+ srl a ;/2
+ srl a ;/4
+ srl a ;/8: don't display last 3 bits of a (later)
+ jr z,nochargebar ;if a=0 then it would loop 256x so skip it
+ ld b,a ;loop b=a times
+chargebar: ;starting at ($39*16)+VIDEO_MEM
+ ld (hl),%11111111 ;draw a piece of the bar
+ inc hl ;next position
+ dnz chargebar ;loop it b times
+nochargebar:
+ ld a,c ;pop a
+ and %111 ;display last bits of chargebar
+ ret z ;if armor=0 then bit = %00000000 (don't disp)
+ ld b,a ;into B
+ xor a ;bit = %00000000
+chargebarbit:
+ scf ;set carry flag
+ rra ;rotates A right and sets bit 7 (c-flag)
+ dnz chargebarbit ;repeat B times (so if B=6 then a=%11111100)
+chargebarready: ; (an if B=3 then a=%11100000)
+ ld (hl),a ;draw this last byte
+ ret
+
+;--------------------------- proc -------------------------------------------
+
+Random: ;a=c<random<b+c; destr:none
+ psh hl
+ ld hl,rancount ;amount to increase with (0-255)
+randomloop:
+ inc (hl) ;change for next time
+ ld a,r ;value $0-7F (can be _anything_ so watch out!)
+ add a,0 ;add to last random value
+ranseed =$-1 ;SMC :P
+ add a,(hl) ;add the changing increase value
+ ;(this is because R can be anything;
+ ; ie always be even so freeze when a must be 1<=a<=1)
+ ld (ranseed),a ;save for next time
+ cp b ;a>=b
+ jr nc,randomloop ;then add again
+ add a,c ;a<b; a=a+c
+ pop hl
+ ret
+rancount: .db 0
+
+RandomY: ;HL = random Y 0..50 right side ((1..51)*16-1)
+ psh bc
+ ld bc,50*256+1 ;range=1..51
+ cal Random ;a = 1..51
+ ld h,0
+ ld l,a ;hl = 1..51
+ add hl,hl
+ add hl,hl
+ add hl,hl
+ add hl,hl ;hl = 1..51 * 16 (left side at random y)
+ dec hl ;hl = 0..50 * 16 (" at right side of screen)
+ ld de,dispbuffer
+ add hl,de ;position on screen
+ pop bc
+ ret
+
+scoreInc:
+ psh bc
+ ld bc,(your_score)
+ add hl,bc
+ ld (your_score),hl
+ pop bc
+ ret
+
+find_sprite: ;in:hl=enemy+type | out:ix=sprite to enemy
+ psh de
+ psh hl
+ ld e,(hl) ;e = enemy offset/2
+ ld d,0 ;de = e
+ ld ix,spr_enemy00 ;first enemy sprite
+ add ix,de ;add offset for current enemy
+ add ix,de ;twice (offset stored as offset/2)
+ pop hl
+ pop de
+ ret