;ÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜ
;Û                                                                           Û
;Û                              P/MG  PROCEDURES                             Û
;Û                                                                           Û
;Û                    These procedures are not optimized.                    Û
;Û                                                                           Û
;Û       NOTE: '&' - this char can use only with PROC/PROCS and MACRO.       Û
;Û                                                                           Û
;ßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßß
;-----------------------------------------------------------------------------
; Initialize P/MG.
; in: address - reserved memory for P/MG.
;-----------------------------------------------------------------------------
PMGBase MACRO address
 lda >address
 sta 54279
 sta 9 ;_pmg_base_
ENDM
;-----------------------------------------------------------------------------
; Set single/duble resolution.
;-----------------------------------------------------------------------------
DoubleSprite&ON MACRO
 LDA 559
 and #%11101111
 sta 559
ENDM
DoubleSprite&OFF MACRO
 lda 559
 ora #%00010000
 sta 559
ENDM
;-----------------------------------------------------------------------------
; Turn on/off special sprite.
;-----------------------------------------------------------------------------
SpecialSprite&ON MACRO
 lda 623
 ora #%00010000
 sta 623
ENDM
SpecialSprite&OFF MACRO
 lda 623
 and #%11101111
 sta 623
ENDM
;-----------------------------------------------------------------------------
; Turn on/off colored sprites.
;-----------------------------------------------------------------------------
MultiSprite&ON MACRO
 lda 623
 ora #%00100000
 sta 623
ENDM
MultiSprite&OFF MACRO
 lda 623
 and #%11011111
 sta 623
ENDM
;-----------------------------------------------------------------------------
; Erase the memory of P/MG.
;-----------------------------------------------------------------------------
PMGClear PROC
 lda 559
 and #%00010000
 beq _pmgclear1_
 lda #7
 bne _pmgclear2_
_pmgclear1_:
 lda #3
_pmgclear2_:
 ldy #0
 sty 0
 tax
 clc
 adc 9 ;_pmg_base_
 sta 1
 tya
 dey
_pmgclear3_:
 sta (0),y
 dey
 bne _pmgclear3_
 sta (0),y
 dec 1
 dey
 dex
 bpl _pmgclear3_
 rts
ENDP
;-----------------------------------------------------------------------------
; Set the mask of player.
; in:     A - number of player. (0-3)
;     ((0)) - address of mask.
;         Y - high of player.
;-----------------------------------------------------------------------------
SetPlayer PROC A,((0)),Y
 and #3
 sta 4
 tax
 tya
 sta _high_player_,x
 bne _set_play1_
 lda 0
 sta 53261,x
 rts
_set_play1_:
 lda 559
 and #%00010000
 lsr @
 clc
 adc 4
 TAX
 lda _pmg_tab_l_,x
 sta 2
 lda _pmg_tab_h_,x
 clc
 adc 9  ;_pmg_base_
 sta 3
 lda 2
 ldx 4
 clc
 adc _last_pos_y_,x
 sta 2
 lda 3
 adc #0
 sta 3
_set_play2_:
 lda (0),y
 sta (2),y
 dey
 bne _set_play2_
 lda (0),y
 sta (2),y
 RTS
;zmienne
 ifndef _pmg_tab_l_
_pmg_tab_l_:
 db 0,128,0,128
_pmg_tab_h_:
 db 2,2,3,3
 db 0,0,0,0
 db 4,5,6,7
 endndef
 ifndef _high_player_
_high_player_:
 db 0,0,0,0
 endndef
 ifndef _last_pos_x_
_last_pos_x_:
 db 0,0,0,0
_last_pos_y_:
 db 0,0,0,0
 endndef
ENDP
;-----------------------------------------------------------------------------
; Set the mask of missile.
; in:     A - number of missile. (0-3)
;     ((0)) - address of mask.
;         Y - high of missile.
;-----------------------------------------------------------------------------
SetMissiles MACRO address,high
  SetMissile 0,address,high
  SetMissile 1,address,high
  SetMissile 2,address,high
  SetMissile 3,address,high
ENDM

;
; sty 8
; lda #3
; sta 7
;_set_misses_:
; ldy 8
; lda 7
; jsr _set_miss0_
; dec 7
; bpl _set_misses_
; rts

SetMissile PROC A,((0)),Y
_SETMISSILE_:
 stx 0
_set_miss0_:
 and #3
 tax
 tya
 sta _high_missile_,x
 bne _set_miss1_

; high=0
 lda _graf_miss_
 and _mask_pattern_,x
 sta 4
 lda 0
 and _mask_missile_,x
 clc
 adc 4
 sta 53265
 sta _graf_miss_
 rts

; high>0
_set_miss1_:
 lda 559
 and #%00010000
 beq _set_miss2_
 lda <768
 pha
 lda >768
 jmp _set_miss3_
_set_miss2_:
 lda <384
 pha
 lda >384
_set_miss3_:
 clc
 adc 9
 sta 3
 pla
 clc
 adc _last_mis_y_,x
 sta 2
 bcc *+4
 inc 3
 LDA _mask_missile_,X
 sta 4
 lda _mask_pattern_,x
 sta 5
_set_miss4_:
 lda (2),y
 and 5
 sta 6
 lda (0),y
 and 4
 clc
 adc 6
 sta (2),y
 dey
 bne _set_miss4_
;jeszcze dla Y=0!
 lda (2),y
 and 5
 sta 6
 lda (0),y
 and 4
 clc
 ADC 6
 sta (2),y
 rts

_graf_miss_:
 db 0
 ifndef _mask_missile_
_mask_missile_:
 db 3,12,48,192
 endndef
 ifndef _mask_pattern_
_mask_pattern_:
 db 252,243,207,63
 endndef
 ifndef _high_missile_
_high_missile_:
 db 0,0,0,0
 endndef
 ifndef _last_mis_x_
_last_mis_x_:
 db 0,0,0,0
_last_mis_y_:
 DB 0,0,0,0
 endndef
ENDP
;-----------------------------------------------------------------------------
; Show/hide players.
;-----------------------------------------------------------------------------
ShowPlayer&ON MACRO
 lda 53277
 ora #%00000010
 sta 53277
ENDM
ShowPlayer&OFF MACRO
 lda 53277
 and #%11111101
 sta 53277
ENDM
;-----------------------------------------------------------------------------
; Show/hide missiles.
;-----------------------------------------------------------------------------
ShowMissile&ON MACRO
 lda 53277
 ora #%00000001
 STA 53277
ENDM
ShowMissile&OFF MACRO
 lda 53277
 and #%11111110
 sta 53277
ENDM
;-----------------------------------------------------------------------------
;Turn on/off DMA for players.
;-----------------------------------------------------------------------------
DMAPlayer&ON MACRO
 lda 559
 ora #%00000100
 sta 559
ENDM
DMAPlayer&OFF MACRO
 lda 559
 and #%11111011
 sta 559
ENDM
;-----------------------------------------------------------------------------
;Turn on/off DMA for missiles.
;-----------------------------------------------------------------------------
DMAMissile&ON MACRO
 lda 559
 ora #%00001000
 sta 559
ENDM
DMAMissile&OFF MACRO
 lda 559
 and #%11110111
 sta 559
ENDM
;-----------------------------------------------------------------------------
; Set the position of player.
; in: A - number of player.
;     X - coordinate X. (0-255)
;     Y - coordinate Y. (0-255)
;-----------------------------------------------------------------------------
PosPlayer PROC A,X,Y
 stx 5
 and #3
 sta 4
 sta 2
 TAX
 lda 5
 sta 53248,x
 sta _last_pos_x_,x
 lda 559
 and #%00010000
 lsr @
 clc
 adc 2
 tax
 lda _pmg_tab_l_,x
 sta 0
 sta 2
 lda _pmg_tab_h_,x
 clc
 adc 9  ;_pmg_base_
 sta 1
 sta 3
;old positon of player
 ldx 4
 lda _last_pos_y_,x
 clc
 ADC 2
 sta 2
 tya
 sta _last_pos_y_,x
;new position of player
 clc
 adc 0
 sta 0

 ldy _high_player_,x
_posplay1_:
 lda (2),y
 sta _buffor_pmg_,y
 dey
 bne _posplay1_
 lda (2),y
 sta _buffor_pmg_,y

 ldy _high_player_,x
 lda #0
_posplay2_:
 sta (2),y
 dey
 bne _posplay2_
 STA (2),Y

 ldy _high_player_,x
_posplay3_:
 lda _buffor_pmg_,y
 sta (0),y
 dey
 bne _posplay3_
 lda _buffor_pmg_,y
 sta (0),y
 rts

;zmienne
 ifndef _pmg_tab_l_
_pmg_tab_l_:
 db 0,128,0,128
_pmg_tab_h_:
 db 2,2,3,3
 db 0,0,0,0
 db 4,5,6,7
 endndef
 ifndef _high_player_
_high_player_:
 db 0,0,0,0
 endndef
 ifndef _last_pos_x_
_last_pos_x_:
 db 0,0,0,0
_last_pos_y_:
 db 0,0,0,0
 endndef
 ifndef _buffor_pmg_
_buffor_pmg_ org *+256
 endndef
ENDP
;-----------------------------------------------------------------------------
; Set the position of missile/missiles.
; in: A - number of missile.
;     X - coordinate X. (0-255)
;     Y - coordinate Y. (0-255)
;-----------------------------------------------------------------------------
PosMissiles PROC X,Y
 stx 8
 sty 7
 lda #3
_pos_misses_:
 ldy 7
 ldx 8
 pha
 jsr _posmissile_
 inc 8
 inc 8
 pla
 tay
 dey
 tya
 bpl _pos_misses_
 RTS
 ifndef _posmiss1_
   PosMissile 0,0,0
 endndef
ENDP

PosMissile PROC A,X,Y
_POSMISSILE_:
 stx 5
_posmiss1_:
 and #3
 tax
 lda 5
 sta 53252,x
 sta _last_mis_x_,x
 lda 559
 and #%00010000
 beq _posmissile2_
 lda <768
 pha
 lda >768
 jmp _posmissile3_
_posmissile2_:
 lda <384
 pha
 lda >384
_posmissile3_:
 clc
 adc 9 ;_pmg_base_
 sta 1
 sta 3
 pla
 sta 0
;old positon of missile
 clc
 adc _last_mis_y_,x
 sta 2
 lda 3
 adc #0
 sta 3
;new position of missile
 tya
 sta _last_mis_y_,x
 clc
 ADC 0
 sta 0
 lda 1
 adc #0
 sta 1

 ldy _high_missile_,x
 bne *+3
 rts
 lda _mask_missile_,x
 sta 4
 lda _mask_pattern_,x
 sta 5
 tya
 pha
_posmissile4_:
 lda (0),y
 and 5
 sta 6
 lda (2),y
 and 4
 clc
 ADC 6
 sta (0),y
 dey
 bne _posmissile4_
;jeszcze dla Y=0
 lda (0),y
 and 5
 sta 6
 lda (2),y
 and 4
 clc
 adc 6
 sta (0),y

;wyzeruj star¥ pozycj©
 pla
 tay
_posmissile5_:
 lda (2),y
 and 5
 sta (2),y
 dey
 BNE _posmissile5_
;jeszcze dla Y=0
 lda (2),y
 and 5
 sta (2),y
 rts

;zmienne
 ifndef _mask_missile_
_mask_missile_:
 db 3,12,48,192
 endndef
 ifndef _mask_pattern_
_mask_pattern_:
 db 252,243,207,63
 endndef
 ifndef _high_missile_
_high_missile_:
 db 0,0,0,0
 endndef
 ifndef _last_mis_x_
_last_mis_x_:
 DB 0,0,0,0
_last_mis_y_:
 db 0,0,0,0
 endndef
 ifndef _buffor_pmg_
_buffor_pmg org *+256
 endndef
ENDP
;-----------------------------------------------------------------------------
; Set the size of player.
; in: A - number of player.
;     Y - width.
;-----------------------------------------------------------------------------
SizePlayer PROC A,Y
 and #3
 tax
 tya
 sta 53256,x
 rts
ENDP
;-----------------------------------------------------------------------------
; Set the size of missile/missiles.
; in: A - number of missile.
;     Y - width.
;-----------------------------------------------------------------------------
SizeMissiles MACRO Y
 SizeMissile 4,Y
ENDM

SizeMissile PROC A,Y
_SIZEMISSILE_:
 and #7
 cmp #4
 beq _sizemis1_
 and #3
_sizemis1_:
 tax
 tya
 and #3
 tay
 lda _width_miss_,y
 and _mask_missile_,x
 sta 53260
 rts
;zmienne
 ifndef _mask_missile_
_mask_missile_:
 DB 3,12,48,192
 endndef
_width_miss_:
 db 0,%01010101,%10101010
 db %11111111,255
ENDP
;-----------------------------------------------------------------------------
; Set the colors of sprites.
; in: X - number of sprite.
;     Y - color.
;     A - shade.
;-----------------------------------------------------------------------------
ColorSprite PROC X,Y,A
 and #15
 sta 4
 txa
 and #3
 tax
 tya
 asl @
 asl @
 asl @
 asl @
 clc
 adc 4
 sta 704,x
 rts
ENDP
;-----------------------------------------------------------------------------
; Reset the collision registers.
;-----------------------------------------------------------------------------
HITClear MACRO
 sta 53278
ENDM
;-----------------------------------------------------------------------------
; Set the priority.
; in: A - priority.
;-----------------------------------------------------------------------------
PrioritySprite PROC A
 and #15
 sta 0
 lda 623
 and #255-15
 clc
 adc 0
 sta 623
 rts
ENDP
