Prince-of-Persia-Apple-II

A running-jumping-swordfighting game Jordan Mechner made on the Apple II from 1985-89
Log | Files | Refs | README | LICENSE

FRAMEADV.S (29360B)


      1 * frameadv
      2 EditorDisk = 0 ;1 = dunj, 2 = palace
      3 org = $1290
      4  lst off
      5  tr on
      6 *-------------------------------
      7  org org
      8 
      9  jmp SURE
     10  jmp FAST
     11  jmp GETINITOBJ
     12 
     13 *-------------------------------
     14  lst
     15  put eq
     16  lst
     17  put gameeq
     18  lst off
     19  put bgdata
     20  lst off
     21 
     22 initsettings
     23  db gmaxval,gminval
     24 
     25  dum locals
     26 index ds 1
     27 rowno ds 1
     28 colno ds 1
     29 yindex ds 1
     30 objid ds 1
     31 state ds 1
     32 Ay ds 1
     33 Dy ds 1
     34 gateposn ds 1
     35 gatebot ds 1
     36 xsave ds 1
     37 blockxco ds 1
     38 switches ds 1
     39 obj1 ds 1
     40 obj2 ds 1
     41 blockthr ds 1
     42  dend
     43 
     44 *-------------------------------
     45 *
     46 *  Draw entire 10 x 3 screen from scratch
     47 *
     48 *-------------------------------
     49 SURE
     50  lda #1
     51  sta genCLS ;clear screen
     52 
     53  jsr setback ;draw on bg plane
     54 
     55  jsr getprev ;get 3 rightmost blocks of screen to left
     56 
     57  lda SCRNUM
     58  jsr calcblue ;get blueprint base addr
     59 
     60 *  Draw 3 rows of 10 blocks (L-R, T-B)
     61 
     62  ldy #2
     63 :row sty rowno ;0 = top row, 2 = bottom row
     64 
     65  lda BlockBot+1,y
     66  sta Dy ;get Y-coord for bottom of D-section
     67  sec
     68  sbc #3
     69  sta Ay ;& A-section
     70 
     71  lda Mult10,y
     72  sta yindex ;block # (0-29)
     73 
     74  lda PREV,y
     75  sta PRECED
     76  lda sprev,y
     77  sta spreced ;get objid & state of preceding block
     78 
     79  jsr getbelow ;get 10 topmost blocks of screen below
     80 
     81  lda #0
     82  sta colno ;0 = leftmost column, 9 = rightmost
     83 :loop asl
     84  asl
     85  sta XCO
     86  sta blockxco ;get X-coord for A-section
     87 
     88  ldy yindex
     89  jsr getobjid
     90  sta objid ;get object id# of current block
     91 
     92  jsr RedBlockSure ;Redraw entire block
     93 
     94  lda objid
     95  sta PRECED
     96  lda state
     97  sta spreced ;Move on to next block
     98 
     99  inc yindex
    100  inc colno
    101 
    102  lda colno
    103  cmp #10
    104  bcc :loop ;...until we've done 10 blocks
    105 
    106 :nextln ldy rowno
    107  beq :done
    108  dey
    109  jmp :row ;...and 3 rows
    110 
    111 * Now draw bottom row of screen above (D-sections only)
    112 
    113 :done ldy #2 ;bottom row of scrn above
    114  sty rowno
    115 
    116  lda #2
    117  sta Dy
    118  lda #-1
    119  sta Ay ;get screen Y-coords
    120 
    121  lda Mult10,y
    122  sta yindex
    123 
    124  lda #0
    125  sta PRECED
    126 
    127  lda scrnBelow
    128  pha
    129  lda scrnBelowL
    130  pha ;save current values on stack
    131 
    132  lda SCRNUM
    133  sta scrnBelow
    134  lda scrnLeft
    135  sta scrnBelowL ;& pretend we're on screen above
    136 
    137 * Draw 10 blocks, L-R
    138 
    139  jsr getbelow
    140 
    141  lda scrnAbove
    142  jsr calcblue
    143 
    144  lda #0
    145  sta colno
    146 :dloop
    147  asl
    148  asl
    149  sta XCO
    150  sta blockxco
    151 
    152  lda scrnAbove
    153  bne :1
    154  lda #floor ;If screen above is null screen,
    155  bne :2 ;draw a row of solid floorpieces
    156 
    157 :1 ldy yindex
    158  jsr getobjid1
    159 :2 sta objid
    160 
    161  jsr RedDSure ;Draw D-section
    162 
    163  lda objid
    164  sta PRECED
    165  lda state
    166  sta spreced
    167 
    168  inc yindex
    169  inc colno
    170 
    171  lda colno
    172  cmp #10
    173  bcc :dloop
    174 
    175  pla ;Restore original screen values
    176  sta scrnBelowL
    177  pla
    178  sta scrnBelow
    179 ]rts rts
    180 
    181 *-------------------------------
    182 *
    183 *  Fast screen redraw
    184 *
    185 *  Same general structure as SURE, but redraws only those
    186 *  blocks specified by redraw buffers.
    187 *
    188 *-------------------------------
    189 FAST
    190  jsr getprev
    191 
    192  lda SCRNUM
    193  jsr calcblue
    194 
    195  lda #0
    196  ldy #20
    197  jsr metbufs3 ;If strength meter is in danger of
    198  sta redkidmeter ;being overwritten, mark it for redraw
    199 
    200  lda #0
    201  ldy #28
    202  jsr metbufs2
    203  sta redoppmeter ;opponent meter too
    204 
    205  lda #30
    206  sta yindex
    207  jsr drawobjs ;Draw o.s. characters first
    208 
    209 *  Draw 3 rows of 10 blocks (L-R, T-B)
    210 
    211  ldy #2
    212 :row sty rowno
    213 
    214  lda BlockBot+1,y
    215  sta Dy
    216  sec
    217  sbc #3
    218  sta Ay
    219 
    220  lda Mult10,y
    221  sta yindex
    222 
    223  lda PREV,y
    224  sta PRECED
    225  lda sprev,y
    226  sta spreced
    227 
    228  jsr getbelow
    229 
    230  lda #0
    231  sta colno
    232 
    233 :loop asl
    234  asl
    235  sta XCO
    236  sta blockxco
    237 
    238  ldy yindex
    239  jsr getobjid
    240  sta objid
    241 
    242  jsr RedBlockFast
    243 
    244  lda objid
    245  sta PRECED
    246  lda state
    247  sta spreced
    248 
    249  inc yindex
    250  inc colno
    251 
    252  lda colno
    253  cmp #10
    254  bcs :nextln
    255  jmp :loop
    256 
    257 :nextln ldy rowno
    258  beq :cont
    259  dey
    260  jmp :row
    261 
    262 * Now draw bottom row of screen above (D-sections only)
    263 
    264 :cont jsr setback
    265 
    266  ldy #2
    267  sty rowno
    268 
    269  lda #2
    270  sta Dy
    271  lda #-1
    272  sta Ay
    273 
    274  lda Mult10,y
    275  sta yindex
    276 
    277  lda #0
    278  sta PRECED
    279 
    280  lda scrnBelow
    281  pha
    282  lda scrnBelowL
    283  pha
    284 
    285  lda SCRNUM
    286  sta scrnBelow
    287  lda scrnLeft
    288  sta scrnBelowL
    289 
    290  jsr getbelow
    291 
    292  lda scrnAbove
    293  beq :done
    294  jsr calcblue
    295 
    296  lda #0
    297  sta colno
    298 :dloop
    299  asl
    300  asl
    301  sta blockxco
    302  sta XCO
    303 
    304  ldy yindex
    305  jsr getobjid1
    306  sta objid
    307 
    308  jsr RedDFast
    309 
    310  lda objid
    311  sta PRECED
    312  lda state
    313  sta spreced
    314 
    315  inc yindex
    316  inc colno
    317 
    318  lda colno
    319  cmp #10
    320  bcc :dloop
    321 
    322 :done
    323  pla
    324  sta scrnBelowL
    325  pla
    326  sta scrnBelow
    327 
    328 * Now draw comix (impact stars) & strength meters
    329 
    330  lda #$ff
    331  sta yindex
    332  jsr drawobjs ;draw comix (index = -1)
    333 
    334  do EditorDisk
    335  lda inbuilder
    336  bne ]rts
    337  fin
    338 
    339  jmp updatemeters
    340 
    341 *-------------------------------
    342 *
    343 *  Redraw entire block
    344 *
    345 *-------------------------------
    346 RedBlockSure
    347  jsr drawc ;C-section of piece below & to left
    348  jsr drawmc
    349 
    350  jsr drawb ;B-section of piece to left
    351  jsr drawmb
    352 
    353  jsr drawd ;D-section
    354  jsr drawmd
    355 
    356  jsr drawa ;A-section
    357  jsr drawma
    358 
    359  jmp drawfrnt ;A-section frontpiece
    360 ;(Note: This is necessary in case we do a
    361 ;layersave before we get to f.g. plane)
    362 
    363 *-------------------------------
    364 *
    365 * Redraw entire D-section
    366 *
    367 *-------------------------------
    368 RedDSure
    369  jsr drawc
    370  jsr drawmc
    371  jsr drawb
    372  jsr drawd
    373  jsr drawmd
    374  jmp drawfrnt
    375 
    376 *-------------------------------
    377 *
    378 *  Partial block redraw (as specified by redraw buffers)
    379 *
    380 *-------------------------------
    381 RedBlockFast
    382  lda wipebuf,y ;is wipebuf marked?
    383  beq :skipwipe ;no--skip it
    384  sec
    385  sbc #1
    386  sta wipebuf,y ;decrement wipebuf
    387 
    388  jsr wipesq ;& wipe this block!
    389 
    390  ldy yindex
    391 
    392 :skipwipe
    393  lda redbuf,y
    394  beq :skipred
    395  sec
    396  sbc #1
    397  sta redbuf,y
    398 
    399  jsr setback
    400  jsr RedBlockSure
    401 
    402  ldy yindex
    403  bpl :skipmove
    404 
    405 :skipred
    406  lda movebuf,y
    407  beq :skipmove
    408  sec
    409  sbc #1
    410  sta movebuf,y
    411 
    412  jsr setback
    413  jsr drawmc
    414  jsr drawmb
    415  jsr drawma
    416 
    417  ldy yindex
    418 
    419 :skipmove
    420  lda floorbuf,y
    421  beq :skipfloor
    422  sec
    423  sbc #1
    424  sta floorbuf,y
    425 
    426  jsr setmid
    427  jsr drawfloor
    428 
    429  ldy yindex
    430  bpl :skiphalf
    431 
    432 :skipfloor
    433  lda halfbuf,y
    434  beq :skiphalf
    435  sec
    436  sbc #1
    437  sta halfbuf,y
    438 
    439  jsr setmid
    440  jsr drawhalf
    441 
    442  ldy yindex
    443 
    444 :skiphalf
    445  lda objbuf,y
    446  beq :skipobj
    447 
    448  lda #0
    449  sta objbuf,y
    450 
    451  jsr drawobjs ;draw all objects in this block
    452 
    453  lda blockxco
    454  sta XCO
    455 
    456  ldy yindex
    457 
    458 :skipobj
    459  lda fredbuf,y
    460  beq :skipfred
    461  sec
    462  sbc #1
    463  sta fredbuf,y
    464 
    465  jsr drawfrnt
    466 
    467  ldy yindex
    468 
    469 :skipfred
    470 ]rts rts
    471 
    472 *-------------------------------
    473 *
    474 *  Partial D-section redraw
    475 *
    476 *-------------------------------
    477 RedDFast
    478  ldy colno
    479  lda topbuf,y ;is topbuf marked?
    480  beq :skip ;no--skip it
    481  sec
    482  sbc #1
    483  sta topbuf,y
    484 
    485  jsr wiped
    486  jsr drawc
    487  jsr drawmc
    488  jsr drawb
    489  jsr redrawd ;(both bg and fg)
    490  jsr drawmd
    491  jsr drawfrnt
    492 :skip
    493 ]rts rts
    494 
    495 *-------------------------------
    496 *
    497 *  Draw objects
    498 *
    499 *  Draw object/s with index # = yindex
    500 *  (Add appropriate images to mid list)
    501 *
    502 *-------------------------------
    503 drawobjs
    504 
    505 * Go through obj list looking for objINDX = yindex
    506 
    507  lda objX
    508  beq :rts
    509 
    510  ldy #0 ;y = sort list index
    511  ldx #1 ;x = object list index
    512 
    513 :loop lda objINDX,x
    514  cmp yindex
    515  bne :next
    516 ;Found a match--add object to sort list
    517  txa
    518  iny
    519  sta sortX,y
    520 
    521 :next inx
    522  cpx objX
    523  bcc :loop
    524  beq :loop
    525 
    526  cpy #0
    527  beq :rts
    528  sty sortX ;# of objects in sort list
    529 
    530 * Sort them into back-to-front order
    531 
    532  jsr sortlist
    533 
    534 * Transfer sorted objects from obj list to mid list
    535 
    536  ldx #1
    537 :loop2
    538  stx xsave
    539 
    540  lda sortX,x
    541  tax ;obj list index
    542 
    543  jsr drawobjx ;draw object #x
    544 
    545  ldx xsave
    546  inx
    547  cpx sortX
    548  bcc :loop2
    549  beq :loop2
    550 ;Done
    551 :rts rts
    552 
    553 *-------------------------------
    554 *
    555 *  Get objids & states of 3 rightmost blocks
    556 *  of left-neighboring screen
    557 *
    558 *  Out: PREV/sprev [0-2]
    559 *
    560 *-------------------------------
    561 getprev lda SCRNUM
    562  beq :null
    563 
    564  lda scrnLeft
    565  beq :blackscrn
    566 
    567 :cont jsr calcblue ;screen to left
    568 
    569  ldy #9
    570  jsr getobjid1
    571  sta PREV
    572  lda state
    573  sta sprev
    574 
    575  ldy #19
    576  jsr getobjid1
    577  sta PREV+1
    578  lda state
    579  sta sprev+1
    580 
    581  ldy #29
    582  jsr getobjid1
    583  sta PREV+2
    584  lda state
    585  sta sprev+2
    586 
    587  rts
    588 
    589 :null ;this scrn is null screen
    590  lda scrnLeft
    591  bne :cont
    592 
    593 :blackscrn ;screen to left is null scrn
    594  lda #block
    595  sta PREV
    596  sta PREV+1
    597  sta PREV+2
    598  lda #0
    599  sta sprev
    600  sta sprev+1
    601  sta sprev+2
    602  rts
    603 
    604 *-------------------------------
    605 *
    606 *  Get objids & states of 10 blocks in row below,
    607 *  1 block to left
    608 *
    609 *  In:  rowno
    610 *  Out: BELOW/SBELOW [0-9]
    611 *
    612 *  Use getbelow1 to look at screens other than scrnBelow
    613 *  (In: A = scrn #)
    614 *
    615 *-------------------------------
    616 getbelow
    617  ldx rowno
    618  cpx #2
    619  bcc :onscr
    620 
    621 * Looking below bottom row
    622 
    623  lda scrnBelow
    624  beq :belowblack ;screen below is black
    625  jsr calcblue
    626 
    627  ldy #8 ;skip rmost
    628 :loop
    629  jsr getobjid
    630 
    631  sta BELOW+1,y
    632  lda state
    633  sta SBELOW+1,y
    634  dey
    635  bpl :loop
    636 :cont1
    637  lda scrnBelowL
    638  beq :llblack ;screen to l.l. is black
    639  jsr calcblue
    640 
    641  ldy #9 ;u.r. block
    642  jsr getobjid
    643  sta BELOW
    644  lda state
    645  sta SBELOW
    646 :done
    647  lda SCRNUM
    648  jsr calcblue ;restore SCRNUM
    649  rts
    650 
    651 * "ONSCREEN": Looking below top or middle row
    652 
    653 :onscr lda PREV+1,x
    654  sta BELOW
    655  lda sprev+1,x
    656  sta SBELOW
    657 
    658  lda yindex
    659  clc
    660  adc #10
    661  tay
    662 
    663  ldx #1
    664 
    665 :loop1 stx xsave
    666  jsr getobjid
    667  ldx xsave
    668 
    669  sta BELOW,x
    670 
    671  lda state
    672  sta SBELOW,x
    673 
    674  iny
    675  inx
    676  cpx #10
    677  bcc :loop1
    678 
    679  rts
    680 
    681 * Look below null screen
    682 
    683 :belowblack
    684  lda #1
    685  tax
    686 :loop2 sta BELOW,x
    687  inx
    688  cpx #10
    689  bcc :loop2
    690  bcs :cont1
    691 
    692 :llblack
    693  lda level
    694  cmp #12
    695  beq :2 ;sorry Lance!
    696  lda #block
    697 :1 sta BELOW
    698  bpl :done
    699 :2 lda #space
    700  bpl :1
    701 
    702 *-------------------------------
    703 *
    704 *  L O A D   O B J E C T
    705 *
    706 *  Load vars with object data
    707 *
    708 *  In:  x = object table index
    709 *       X, OFF, Y, IMG, FACE, TYP, CU, CD, CL, CR, TAB
    710 *
    711 *  Out: XCO, OFFSET, YCO, IMAGE, TABLE
    712 *       FCharFace, FCharCU-CD-CL-CR
    713 *       A = objTYP
    714 *
    715 *-------------------------------
    716 loadobj
    717  lda objX,x
    718  sta FCharX
    719  sta XCO
    720 
    721  lda objOFF,x
    722  sta OFFSET
    723 
    724  lda objY,x
    725  sta FCharY
    726  sta YCO
    727 
    728  lda objIMG,x
    729  sta IMAGE
    730 
    731  lda objTAB,x
    732  sta TABLE
    733 
    734  lda objFACE,x
    735  sta FCharFace
    736 
    737  lda objCU,x
    738  sta FCharCU
    739  lda objCD,x
    740  sta FCharCD
    741  lda objCL,x
    742  sta FCharCL
    743  lda objCR,x
    744  sta FCharCR
    745 
    746  lda objTYP,x
    747 ]rts rts
    748 
    749 *-------------------------------
    750 *
    751 *  D R A W   F R O N T
    752 *
    753 *-------------------------------
    754 drawfrnt
    755  ldx PRECED
    756  cpx #gate
    757  bne :a
    758  jsr DrawGateBF? ;special case
    759 
    760 :a ldx objid
    761  cpx #slicer
    762  bne :11
    763  jmp drawslicerf
    764 :11 cpx #flask
    765  bne :1
    766  lda state
    767  and #%11100000
    768  cmp #%10100000 ;5
    769  beq :1
    770  cmp #%01000000 ;2
    771  bcc :1
    772  lda #specialflask
    773  bne :12
    774 
    775 :1 ldx objid
    776  lda fronti,x
    777  beq ]rts
    778 :12 sta IMAGE
    779 
    780  lda Ay
    781  clc
    782  adc fronty,x
    783  sta YCO
    784 
    785  lda blockxco
    786  clc
    787  adc frontx,x
    788  sta XCO
    789 
    790  cpx #archtop2
    791  bcs :sta
    792 
    793  do EditorDisk
    794  lda #EditorDisk
    795  cmp #2
    796  beq :ndunj
    797  fin
    798 
    799  lda BGset1
    800  cmp #1 ;pal
    801  beq :ndunj
    802 
    803  cpx #posts
    804  beq :sta ;for dungeon bg set
    805 :ndunj cpx #block
    806  beq :block
    807 
    808  jmp maddfore
    809 
    810 * Special handling for block
    811 
    812 :block ldy state
    813  cpy #numblox
    814  bcc :2
    815  ldy #0
    816 :2 lda blockfr,y
    817  sta IMAGE
    818 
    819 * Pieces that go to byte boundaries can be STA'd w/o masking
    820 
    821 :sta ldx #sta
    822  stx OPACITY
    823  jmp addfore
    824 
    825 *-------------------------------
    826 * Draw Gate B Front?
    827 * (only if kid is to the left of bars)
    828 *-------------------------------
    829 DrawGateBF?
    830  lda rowno
    831  cmp KidBlockY
    832  bne ]rts
    833 
    834  ldx colno
    835  dex
    836  cpx KidBlockX ;is kid in gate block?
    837  bne ]rts
    838  lda scrnRight
    839  cmp KidScrn
    840  beq ]rts
    841 
    842  jmp drawgatebf ;draw gate bars over char
    843 
    844 *-------------------------------
    845 *
    846 *  D R A W   M O V A B L E   ' B '
    847 *
    848 *-------------------------------
    849 drawmb
    850  lda PRECED
    851 
    852  cmp #gate ;check for special cases
    853  bne :1
    854  jmp drawgateb ;draw B-section of moving gate
    855 
    856 :1 cmp #spikes
    857  bne :2
    858  jmp drawspikeb
    859 
    860 :2 cmp #loose
    861  bne :3
    862  jmp drawlooseb
    863 
    864 :3 cmp #torch
    865  bne :4
    866  jmp drawtorchb
    867 :4
    868 :5 cmp #exit
    869  bne :6
    870  jmp drawexitb
    871 
    872 :6
    873 ]rts rts
    874 
    875 *-------------------------------
    876 *
    877 *  D R A W  M O V A B L E  ' C '
    878 *
    879 *-------------------------------
    880 drawmc
    881  lda objid ;is there a piece here?
    882  cmp #space
    883  beq :ok
    884  cmp #panelwof
    885  beq :ok
    886  cmp #pillartop
    887  beq :ok
    888 
    889  bne ]rts ;if yes, its A-section will cover up
    890 ;the C-section of the piece below
    891 :ok
    892  ldx colno
    893  lda BELOW,x ;objid of piece below & to left
    894 
    895  cmp #gate
    896  bne ]rts
    897 
    898  ;That piece is a gate--
    899  jmp drawgatec ;special case (movable c)
    900 
    901 *-------------------------------
    902 *
    903 *  Draw C-section (if visible)
    904 *
    905 *-------------------------------
    906 drawc
    907  jsr checkc
    908  bcc ]rts
    909  jsr dodrawc ;OR C-section of piece below & to left
    910  jmp domaskb ;Mask B-section of piece to left
    911 
    912 *-------------------------------
    913 *
    914 *  Return cs if C-section is visible, cc if hidden
    915 *
    916 *-------------------------------
    917 checkc
    918  lda objid ;Does this space contain solid floorpiece?
    919  beq :vis
    920  cmp #pillartop
    921  beq :vis
    922  cmp #panelwof
    923  beq :vis
    924  cmp #archtop1
    925  bcs :vis
    926  bcc ]rts ;C-section is hidden
    927 :vis sec ;C-section is visible
    928 ]rts rts
    929 
    930 *-------------------------------
    931 *
    932 *  Draw C-section of piece below & to left
    933 *
    934 *-------------------------------
    935 dodrawc
    936  ldx colno
    937  lda BELOW,x ;objid of piece below & to left
    938  tax
    939  cpx #block
    940  beq :block
    941  lda piecec,x
    942  beq ]rts ;piece has no c-section
    943  cmp #panelc0
    944  beq :panel ;special panel handling
    945 :cont sta IMAGE
    946  lda blockxco
    947  sta XCO
    948  lda Dy
    949  sta YCO
    950  lda #ora
    951  sta OPACITY
    952  jmp add
    953 
    954 * Special panel handling
    955 
    956 :panel ldx colno
    957  lda SBELOW,x
    958  tay
    959  cpy #numpans ;# of different panels
    960  bcs ]rts
    961  lda panelc,y
    962  bne :cont
    963  rts
    964 
    965 :block ldx colno
    966  lda SBELOW,x
    967  tay
    968  cpy #numblox
    969  bcc :1
    970  ldy #0
    971 :1 lda blockc,y
    972  bne :cont
    973 ]rts rts
    974 
    975 *-------------------------------
    976 *
    977 *  Mask B-section of piece to left
    978 *
    979 *-------------------------------
    980 domaskb
    981  ldx PRECED
    982  lda maskb,x
    983  beq ]rts
    984  sta IMAGE
    985 
    986  lda Dy
    987  sta YCO
    988  lda #and
    989  sta OPACITY
    990  jmp add
    991 
    992 *-------------------------------
    993 *
    994 *  Draw B-section of piece to left
    995 *
    996 *-------------------------------
    997 drawb
    998  lda objid
    999  cmp #block
   1000  beq ]rts ;B-section hidden by solid block
   1001 
   1002  ldx PRECED
   1003  cpx #space
   1004  beq :space
   1005  cpx #floor
   1006  beq :floor
   1007  cpx #block
   1008  beq :block
   1009  lda pieceb,x
   1010  beq :stripe
   1011  cmp #panelb0
   1012  beq :panel ;special panel handling
   1013 
   1014 * draw regular B-section
   1015 
   1016  jsr :cont1
   1017 
   1018 * Add stripe (palace bg set only)
   1019 
   1020 :stripe do EditorDisk
   1021  lda #EditorDisk
   1022  cmp #2
   1023  beq :stripe
   1024  fin
   1025 
   1026  lda BGset1
   1027  cmp #1 ;pal
   1028  bne ]rts
   1029 
   1030 :str1 ldx PRECED
   1031  lda bstripe,x
   1032  beq ]rts
   1033  sta IMAGE
   1034  lda Ay
   1035  sec
   1036  sbc #32
   1037  jmp :cont2
   1038 
   1039 * Special panel handling
   1040 
   1041 :panel ldy spreced
   1042  cpy #numpans
   1043  bcs ]rts
   1044  lda panelb,y
   1045  bne :cont1
   1046 ]rts rts
   1047 
   1048 :block ldy spreced
   1049  cpy #numblox
   1050  bcc :1
   1051  ldy #0
   1052 :1 lda blockb,y
   1053  bne :cont1
   1054 
   1055 :floor ldy spreced
   1056  cpy #numbpans+1
   1057  bcc :3
   1058  ldy #0
   1059 :3 lda floorb,y
   1060  beq ]rts
   1061  sta IMAGE
   1062  lda floorby,y
   1063  jmp :cont
   1064 
   1065 :space ldy spreced
   1066  cpy #numbpans+1
   1067  bcs ]rts
   1068  lda spaceb,y
   1069  beq ]rts
   1070  sta IMAGE
   1071  lda spaceby,y
   1072  jmp :cont
   1073 
   1074 * Draw regular B-section
   1075 
   1076 :cont1 sta IMAGE
   1077  lda pieceby,x
   1078 :cont clc
   1079  adc Ay
   1080 :cont2 sta YCO
   1081  lda blockxco
   1082  sta XCO
   1083  lda #ora
   1084  sta OPACITY
   1085  jmp add
   1086 
   1087 *-------------------------------
   1088 *
   1089 *  Draw D-section
   1090 *
   1091 *-------------------------------
   1092 redrawd jsr drawd
   1093  beq ]rts
   1094  jmp addfore
   1095 
   1096 drawd lda #sta
   1097  sta OPACITY
   1098  ldx objid
   1099  cpx #block
   1100  beq :block
   1101  cpx #panelwof ;Do we need to mask this D-section?
   1102  bne :cont ;no
   1103 :mask lda #ora
   1104  sta OPACITY
   1105 :cont lda pieced,x
   1106  beq ]rts
   1107 :cont1 sta IMAGE
   1108  lda blockxco
   1109  sta XCO
   1110  lda Dy
   1111  sta YCO
   1112  jsr add
   1113  lda #$ff
   1114 ]rts rts
   1115 
   1116 * Block handling
   1117 
   1118 :block ldy state
   1119  cpy #numblox
   1120  bcc :1
   1121  ldy #0
   1122 :1 lda blockd,y
   1123  bne :cont1
   1124 
   1125 *-------------------------------
   1126 *
   1127 *  D R A W   ' A '
   1128 *
   1129 *  (1) If piece to left has intrusive B-section (e.g., panel):
   1130 *      MASK A-section
   1131 *  (2) OR A-section
   1132 *
   1133 *-------------------------------
   1134 drawa
   1135  lda PRECED
   1136  cmp #archtop1
   1137  beq :special
   1138  cmp #panelwif
   1139  beq :needmask
   1140  cmp #panelwof
   1141  beq :needmask
   1142  cmp #pillartop
   1143  beq :needmask
   1144  cmp #block
   1145  bne :nomask
   1146 
   1147 :needmask jsr addamask
   1148 
   1149 :nomask jmp adda
   1150 
   1151 :special ldx objid
   1152  cpx #panelwof
   1153  bne :nomask
   1154  lda #archpanel ;arch ends to L of panel
   1155  jmp adda1
   1156 
   1157 *-------------------------------
   1158 addmidezfast
   1159  lda #UseFastlay
   1160  jmp addmidez
   1161 add
   1162 ]add jmp addback ;self-mod
   1163 
   1164 setback lda #addback
   1165  sta ]add+1
   1166  lda #>addback
   1167  sta ]add+2
   1168  rts
   1169 
   1170 setmid lda #addmidezfast
   1171  sta ]add+1
   1172  lda #>addmidezfast
   1173  sta ]add+2
   1174 ]rts rts
   1175 
   1176 maddfore ldx #mask
   1177  stx OPACITY
   1178  jsr addfore
   1179  ldx #ora
   1180  stx OPACITY
   1181  jmp addfore
   1182 
   1183 addamask ldx objid
   1184  lda maska,x
   1185  beq ]rts
   1186  sta IMAGE
   1187  lda blockxco
   1188  sta XCO
   1189  lda Ay
   1190  sta YCO
   1191  lda #and
   1192  sta OPACITY
   1193  jmp add
   1194 
   1195 adda ldx objid
   1196  jsr getpiecea
   1197  beq ]rts ;nothing here
   1198 adda1 sta IMAGE
   1199  lda blockxco
   1200  sta XCO
   1201  lda Ay
   1202  clc
   1203  adc pieceay,x
   1204  sta YCO
   1205  lda #ora
   1206  sta OPACITY
   1207  jmp add
   1208 
   1209 *-------------------------------
   1210 *
   1211 *  D R A W   M O V A B L E   ' A '
   1212 *
   1213 *-------------------------------
   1214 drawma
   1215  lda objid
   1216  cmp #spikes
   1217  bne :2
   1218  jmp drawspikea
   1219 
   1220 :2 cmp #slicer
   1221  bne :3
   1222  jmp drawslicera
   1223 
   1224 :3 cmp #flask
   1225  bne :4
   1226  jmp drawflaska
   1227 
   1228 :4 cmp #sword
   1229  bne :5
   1230  jmp drawsworda
   1231 :5
   1232 ]rts rts
   1233 
   1234 *-------------------------------
   1235 *
   1236 * D R A W   M O V A B L E  ' D '
   1237 *
   1238 *-------------------------------
   1239 drawmd
   1240  lda objid
   1241  cmp #loose
   1242  bne :1
   1243  jmp drawloosed
   1244 
   1245 :1
   1246 ]rts rts
   1247 *-------------------------------
   1248 *
   1249 *  D R A W   F L O O R
   1250 *
   1251 *-------------------------------
   1252 drawfloor
   1253  lda PRECED ;empty space to left?
   1254  bne ]rts
   1255 ]drawflr
   1256  jsr addamask
   1257  jsr adda
   1258  jsr drawma
   1259  jmp drawd
   1260 
   1261 *-------------------------------
   1262 *
   1263 *  D R A W   H A L F
   1264 *
   1265 *  Special version of "drawfloor" for climbup
   1266 *
   1267 *-------------------------------
   1268 drawhalf
   1269  lda PRECED
   1270  bne ]rts
   1271 
   1272 * empty space to left -- mask & draw "A" section
   1273 
   1274  ldx objid
   1275  cpx #floor
   1276  beq :flr
   1277  cpx #torch
   1278  beq :flr
   1279  cpx #dpressplate
   1280  beq :flr
   1281  cpx #exit
   1282  beq :flr
   1283 
   1284  lda BGset1
   1285  cmp #1 ;pal?
   1286  bne ]drawflr ;if there's no halfpiece for this objid,
   1287 ;redraw full floorpiece
   1288  cpx #posts
   1289  beq :post
   1290  cpx #archbot
   1291  bne ]drawflr
   1292 
   1293 :post jsr :sub
   1294  lda #CUpost
   1295  bne :cont
   1296 
   1297 :flr jsr :sub
   1298  lda #CUpiece
   1299 :cont sta IMAGE
   1300  lda #ora
   1301  sta OPACITY
   1302  jsr add
   1303  jmp drawd
   1304 
   1305 :sub lda #CUmask
   1306  sta IMAGE
   1307  lda blockxco
   1308  sta XCO
   1309  lda Ay
   1310  sta YCO
   1311  ldx objid
   1312  cpx #dpressplate
   1313  bne :1
   1314  inc YCO ;quick trick for dpressplate
   1315 :1 lda #and
   1316  sta OPACITY
   1317  jmp add
   1318 
   1319 *-------------------------------
   1320 *
   1321 *  S H O R T   W I P E
   1322 *
   1323 *  In: Y = buffer index
   1324 *
   1325 *-------------------------------
   1326 wipesq
   1327  lda whitebuf,y
   1328 ]wipe sta height
   1329 
   1330  lda #4
   1331  sta width
   1332 
   1333  lda blockxco
   1334  sta XCO
   1335 
   1336  lda Dy
   1337  sta YCO
   1338 
   1339  lda #$80
   1340  jmp addwipe
   1341 ]rts rts
   1342 
   1343 *-------------------------------
   1344 *
   1345 * Wipe D-section
   1346 *
   1347 *-------------------------------
   1348 wiped
   1349  lda objid
   1350  cmp #pillartop
   1351  beq ]rts
   1352  cmp #panelwif
   1353  beq ]rts
   1354  cmp #panelwof
   1355  beq ]rts
   1356  cmp #block
   1357  beq ]rts
   1358 
   1359  lda #3
   1360  jmp ]wipe
   1361 
   1362 *-------------------------------
   1363 *  D R A W  L O O S E  F L O O R  " D "
   1364 *-------------------------------
   1365 drawloosed
   1366  lda state
   1367  jsr getloosey
   1368 
   1369  lda loosed,y
   1370  beq :rts
   1371  sta IMAGE
   1372 
   1373  lda blockxco
   1374  sta XCO
   1375  lda Dy
   1376  sta YCO
   1377 
   1378  lda #sta
   1379  sta OPACITY
   1380 
   1381  jmp add
   1382 ]rts
   1383 :rts rts
   1384 
   1385 *-------------------------------
   1386 *  D R A W  L O O S E  F L O O R  " B "
   1387 *-------------------------------
   1388 drawlooseb
   1389  lda spreced
   1390  jsr getloosey
   1391 
   1392  lda #looseb
   1393  sta IMAGE
   1394 
   1395  lda Ay
   1396  clc
   1397  adc looseby,y
   1398  sta YCO
   1399 
   1400  lda #ora
   1401  sta OPACITY
   1402 
   1403  jmp add
   1404 
   1405 *-------------------------------
   1406 *
   1407 * Get piece "A"
   1408 *
   1409 * In: state; X = objid
   1410 * Out: A = A-section image #
   1411 *
   1412 *-------------------------------
   1413 getpiecea
   1414  cpx #loose
   1415  beq :loose
   1416 
   1417  lda piecea,x
   1418 ]rts rts
   1419 
   1420 :loose lda state
   1421  jsr getloosey
   1422  lda loosea,y
   1423  rts
   1424 
   1425 *-------------------------------
   1426 *
   1427 * Get loose floor index
   1428 *
   1429 * In: A = state
   1430 * Out: Y = index
   1431 *
   1432 *-------------------------------
   1433 getloosey
   1434  do EditorDisk
   1435  ldy inbuilder
   1436  beq :1
   1437  ldy #1
   1438  rts
   1439  fin
   1440 
   1441 :1 tay ;state
   1442  bpl ]rts
   1443  and #$7f
   1444  cmp #Ffalling+1
   1445  bcc :ok
   1446  lda #1
   1447 :ok tay
   1448 ]rts rts
   1449 
   1450 *-------------------------------
   1451 *  Draw spikes A
   1452 *-------------------------------
   1453 drawspikea
   1454  ldx state
   1455  bpl :1 ;hibit clear --> frame #
   1456  ldx #spikeExt ;hibit set --> spikes extended
   1457 :1 lda spikea,x
   1458  beq ]rts
   1459  sta IMAGE
   1460  lda blockxco
   1461  sta XCO
   1462  lda Ay
   1463  sec
   1464  sbc #1
   1465  sta YCO
   1466  lda #ora
   1467  sta OPACITY
   1468  jmp add
   1469 
   1470 *-------------------------------
   1471 *  Draw spikes B
   1472 *-------------------------------
   1473 drawspikeb
   1474  ldx spreced
   1475  bpl :1 ;hibit clear --> frame #
   1476  ldx #spikeExt ;hibit set --> spikes extended
   1477 :1 lda spikeb,x
   1478  beq ]rts
   1479  sta IMAGE
   1480  lda blockxco
   1481  sta XCO
   1482  lda Ay
   1483  sec
   1484  sbc #1
   1485  sta YCO
   1486  lda #ora
   1487  sta OPACITY
   1488  jmp add
   1489 
   1490 *-------------------------------
   1491 *  Draw torch B (flame)
   1492 *-------------------------------
   1493 drawtorchb
   1494  do EditorDisk
   1495  lda inbuilder
   1496  bne ]rts
   1497  fin
   1498 
   1499  lda blockxco
   1500  beq ]rts ;no flame on leftmost torch
   1501  sta XCO
   1502  lda Ay
   1503  sta YCO
   1504  ldx spreced
   1505  jsr setupflame ;in gamebg
   1506  jmp addback
   1507 ]rts rts
   1508 
   1509 *-------------------------------
   1510 *  Draw flask A (bubbles)
   1511 *-------------------------------
   1512 drawflaska
   1513  do EditorDisk
   1514  lda inbuilder
   1515  bne ]rts
   1516  fin
   1517 
   1518  lda blockxco
   1519  sta XCO
   1520  lda Ay
   1521  sta YCO
   1522  ldx state
   1523  jsr setupflask
   1524  lda #UseLay
   1525  jmp addmidezo
   1526 
   1527 *-------------------------------
   1528 *  Draw sword A
   1529 *-------------------------------
   1530 drawsworda
   1531  lda #swordgleam0
   1532  ldx state
   1533  cpx #1
   1534  bne :0
   1535  lda #swordgleam1
   1536 :0 sta IMAGE
   1537  lda blockxco
   1538  sta XCO
   1539  lda Ay
   1540  sta YCO
   1541  lda #sta
   1542  sta OPACITY
   1543  jmp add
   1544 
   1545 *-------------------------------
   1546 *  Draw slicer A
   1547 *-------------------------------
   1548 drawslicera
   1549  lda state
   1550  and #$7f
   1551  tax
   1552  cpx #slicerRet
   1553  bcc :1
   1554  ldx #slicerRet ;fully retracted
   1555 :1 lda slicerseq,x
   1556  tax
   1557  dex
   1558  stx xsave
   1559 
   1560  lda blockxco
   1561  sta XCO
   1562  lda Ay
   1563  sta YCO
   1564  lda state ;hibit set = smeared
   1565  bpl :clean
   1566  lda slicerbot2,x
   1567  bne :3
   1568 :clean lda slicerbot,x
   1569  beq :2
   1570 :3 sta IMAGE
   1571  lda #ora
   1572  sta OPACITY
   1573  jsr add
   1574  ldx xsave
   1575 
   1576 :2 lda slicertop,x
   1577  beq ]rts
   1578  sta IMAGE
   1579  lda Ay
   1580  sec
   1581  sbc slicergap,x
   1582  sta YCO
   1583  lda #ora
   1584  sta OPACITY
   1585  jmp add
   1586 
   1587 *-------------------------------
   1588 * Draw slicer front
   1589 *-------------------------------
   1590 drawslicerf
   1591  lda state
   1592  and #$7f
   1593  tax
   1594  cpx #slicerRet
   1595  bcc :1
   1596  ldx #slicerRet ;fully retracted
   1597 :1 lda slicerseq,x
   1598  tax
   1599  dex
   1600  stx xsave
   1601 
   1602  lda blockxco
   1603  sta XCO
   1604  lda Ay
   1605  sta YCO
   1606  lda slicerfrnt,x
   1607  beq :2
   1608  sta IMAGE
   1609  jmp maddfore
   1610 :2
   1611 ]rts rts
   1612 
   1613 *-------------------------------
   1614 * Draw exit "b" (stairs)
   1615 *-------------------------------
   1616 drawexitb
   1617  lda #stairs
   1618  sta IMAGE
   1619 
   1620  lda Ay
   1621  sec
   1622  sbc #12
   1623  sta YCO
   1624 
   1625  lda blockxco
   1626  cmp #36
   1627  bcs ]rts ;can't protrude off R
   1628  clc
   1629  adc #1
   1630  sta XCO
   1631 
   1632  lda #sta
   1633  sta OPACITY
   1634 
   1635  lda SCRNUM
   1636  cmp KidStartScrn
   1637  beq :nostairs ;assume it's an entrance
   1638  jsr add
   1639 :nostairs
   1640 
   1641 * draw door, bottom to top
   1642 
   1643  lda Dy
   1644  sec
   1645  sbc #67
   1646  cmp #192
   1647  bcs ]rts
   1648  sta blockthr ;topmost usable line
   1649 
   1650  lda spreced
   1651  lsr
   1652  lsr
   1653  sta gateposn ;gateposn := spreced/4
   1654 
   1655  lda Ay
   1656  sec
   1657  sbc #14
   1658  sbc gateposn
   1659  sta doortop ;for CROPCHAR
   1660 :loop sta YCO
   1661 
   1662  lda #doormask
   1663  sta IMAGE
   1664  lda #and
   1665  sta OPACITY
   1666  jsr add
   1667 
   1668  lda #door
   1669  sta IMAGE
   1670  lda #ora
   1671  sta OPACITY
   1672  jsr add
   1673 
   1674  lda YCO
   1675  sec
   1676  sbc #4
   1677  cmp blockthr
   1678  bcs :loop
   1679 
   1680 * repair top
   1681 
   1682  lda Ay
   1683  sec
   1684  sbc #64 ;Technically part of C-section
   1685  cmp #192 ;but who cares
   1686  bcs ]rts
   1687 
   1688  sta YCO
   1689 
   1690  lda #toprepair
   1691  sta IMAGE
   1692  lda #sta
   1693  sta OPACITY
   1694  jmp add
   1695 
   1696 ]rts rts
   1697 
   1698 *-------------------------------
   1699 *  D R A W   G A T E   " C "
   1700 *-------------------------------
   1701 drawgatec
   1702  lda Dy
   1703  sta YCO
   1704  lda #gatecmask
   1705  sta IMAGE
   1706  lda #and
   1707  sta OPACITY
   1708  jsr add ;mask out triangular area
   1709 
   1710  ldx colno
   1711  lda SBELOW,x ;gate state
   1712  cmp #gmaxval
   1713  bcc :1
   1714  lda #gmaxval
   1715 :1 lsr
   1716  lsr
   1717  sta gateposn
   1718  and #$f8
   1719  eor #$ff
   1720  clc
   1721  adc #1
   1722  clc
   1723  adc gateposn
   1724  tay ;Y:= (state/4) mod 8
   1725  lda gate8c,y
   1726  sta IMAGE
   1727 
   1728  lda #ora
   1729  sta OPACITY
   1730  jmp add
   1731 
   1732 *-------------------------------
   1733 *
   1734 *  D R A W   G A T E   " B "
   1735 *
   1736 *  Lay down (STA) the gate in sections, bottom
   1737 *  to top.  The bottom piece has two blank lines that
   1738 *  erase its trail as the gate rises.
   1739 *  Topmost section has 8 separate shapes, 1-8 pixels high.
   1740 *
   1741 *-------------------------------
   1742 setupdgb
   1743  lda Dy
   1744  sec
   1745  sbc #62
   1746  sta blockthr ;topmost line of B-section
   1747 
   1748  lda spreced
   1749  cmp #gmaxval
   1750  bcc :1
   1751  lda #gmaxval
   1752 :1 lsr
   1753  lsr
   1754  clc
   1755  adc #1
   1756  sta gateposn ;gateposn:= state/4 + 1
   1757 ;(gatebottom height off floor)
   1758  lda Ay
   1759  sec
   1760  sbc gateposn
   1761  sta gatebot ;gatebottom YCO
   1762 ]rts rts
   1763 
   1764 *-------------------------------
   1765 drawgatebf
   1766  jsr setupdgb
   1767 
   1768 * Gate bottom
   1769 
   1770  lda #ora
   1771  sta OPACITY
   1772 
   1773  lda gatebot
   1774  sec
   1775  sbc #2
   1776  sta YCO ;no 2 blank lines at bottom
   1777 
   1778  lda #gatebotORA
   1779  sta IMAGE
   1780 
   1781  jsr addfore
   1782 
   1783 * Middle pieces
   1784 
   1785 :cont
   1786  lda #gateB1
   1787  sta IMAGE
   1788 
   1789  lda gatebot
   1790  sec
   1791  sbc #12
   1792 
   1793 :loop sta YCO
   1794  cmp #192
   1795  bcs ]rts
   1796 
   1797  sec
   1798  sbc #7 ;grill mid piece is 8 lines high--
   1799  bcc :done ;will it stick up out of block area?
   1800  cmp blockthr
   1801  bcc :done
   1802 ;no, we're still safe--keep going
   1803  jsr addfore
   1804 
   1805  lda YCO
   1806  sec
   1807  sbc #8
   1808  bne :loop
   1809 :done
   1810  ;Skip top piece to save a little time
   1811 ]rts rts
   1812 
   1813 *-------------------------------
   1814 drawgateb
   1815  jsr setupdgb
   1816 
   1817 *  First, draw bottom piece
   1818 
   1819  clc
   1820  adc #12
   1821  cmp Ay ;over floor/wall boundary?
   1822  bcc :storit
   1823 
   1824 * Bottom piece is partly below floor line -- STA won't work.
   1825 * We need to redraw b.g., then OR gate bottom on top.
   1826 
   1827 :orit
   1828  jsr restorebot
   1829 
   1830  lda gatebot
   1831  sec
   1832  sbc #2
   1833  sta YCO ;no 2 blank lines at bottom
   1834 
   1835  lda #gatebotORA
   1836  sta IMAGE
   1837 
   1838  lda #ora
   1839  sta OPACITY
   1840 
   1841  jsr addback
   1842 
   1843  jmp :cont
   1844 
   1845 *  Gate is above floor line -- STA it
   1846 
   1847 :storit lda gatebot
   1848  sta YCO
   1849 
   1850  lda #gatebotSTA
   1851  sta IMAGE
   1852 
   1853  lda #sta
   1854  sta OPACITY
   1855 
   1856  jsr addback
   1857 
   1858 *  Next, draw as many middle pieces as we need to make
   1859 *  up rest of grill
   1860 
   1861 :cont
   1862  lda #sta
   1863  sta OPACITY
   1864 
   1865  lda #gateB1
   1866  sta IMAGE
   1867 
   1868  lda gatebot
   1869  sec
   1870  sbc #12
   1871 
   1872 :loop sta YCO
   1873  cmp #192
   1874  bcs :rts
   1875 
   1876  sec
   1877  sbc #7 ;grill mid piece is 8 lines high--
   1878  bcc :done ;will it stick up out of block area?
   1879  cmp blockthr
   1880  bcc :done
   1881 ;no, we're still safe--keep going
   1882  jsr addback
   1883 
   1884  lda YCO
   1885  sec
   1886  sbc #8
   1887  bne :loop
   1888 
   1889 * now add final piece at top
   1890 
   1891 :done lda YCO
   1892  sec
   1893  sbc blockthr
   1894  clc
   1895  adc #1 ;desired height (0-8 pixels)
   1896 
   1897  beq :rts
   1898  cmp #9
   1899  bcs :rts
   1900 
   1901  tay
   1902  lda gate8b-1,y
   1903  sta IMAGE
   1904 
   1905  jsr addback
   1906 
   1907 :rts rts
   1908 
   1909 *-------------------------------
   1910 restorebot
   1911  ldx #gate
   1912  lda pieceb,x
   1913  sta IMAGE
   1914  lda pieceby,x
   1915  clc
   1916  adc Ay
   1917  sta YCO
   1918  lda blockxco
   1919  sta XCO
   1920  lda #sta
   1921  sta OPACITY
   1922  jsr add
   1923 
   1924  jsr checkc
   1925  bcc :1
   1926  jsr dodrawc
   1927 :1 jmp drawa
   1928 
   1929 *-------------------------------
   1930 *
   1931 *  Draw object #x
   1932 *  (Add appropriate images to mid table)
   1933 *
   1934 *  In: x = object table index
   1935 *
   1936 *-------------------------------
   1937 drawobjx
   1938  jsr loadobj ;Load vars with object data
   1939 ;A = object type
   1940  cmp #TypeKid
   1941  beq :kid
   1942  cmp #TypeReflect
   1943  bne :1
   1944 :kid jmp DrawKid
   1945 
   1946 :1 cmp #TypeShad
   1947  bne :2
   1948  jmp DrawShad
   1949 
   1950 :2 cmp #TypeFF
   1951  bne :3
   1952  jmp DrawFF
   1953 
   1954 :3 cmp #TypeSword
   1955  beq :5
   1956  cmp #TypeComix
   1957  bne :4
   1958 :5 jmp DrawSword
   1959 
   1960 :4 cmp #TypeGd
   1961  bne :6
   1962  jmp DrawGuard
   1963 :6
   1964 ]rts rts
   1965 
   1966 *-------------------------------
   1967 * Draw Falling Floor
   1968 *-------------------------------
   1969 DrawFF
   1970  lda #-1 ;normal
   1971  sta FCharFace
   1972 
   1973  lda IMAGE ;mobframe #
   1974  sta FCharImage ;use as temp store
   1975 
   1976 * A-section
   1977 
   1978  lda FCharY
   1979  sec
   1980  sbc #3
   1981  sta YCO
   1982  ldx #floor
   1983  lda maska,x
   1984  sta IMAGE
   1985  lda #and
   1986  sta OPACITY
   1987  lda #UseLayrsave
   1988  jsr addmid
   1989 
   1990  ldx FCharImage
   1991  lda loosea,x
   1992  sta IMAGE
   1993  lda #ora
   1994  sta OPACITY
   1995  lda #UseLay
   1996  jsr addmid
   1997 
   1998 * D-section
   1999 
   2000  ldx FCharImage
   2001  lda loosed,x
   2002  sta IMAGE
   2003  lda FCharY
   2004  sta YCO
   2005  lda #sta
   2006  sta OPACITY
   2007  lda #UseLayrsave
   2008  jsr addmid
   2009 
   2010 * B-section
   2011 
   2012  lda FCharX
   2013  clc
   2014  adc #4
   2015  sta XCO
   2016 
   2017  lda FCharY
   2018  sec
   2019  sbc #4
   2020  sta YCO
   2021 
   2022  lda #looseb
   2023  sta IMAGE
   2024  lda #ora
   2025  sta OPACITY
   2026  lda #UseLayrsave
   2027  jmp addmid
   2028 
   2029 *-------------------------------
   2030 *
   2031 *  Get objid & state
   2032 *
   2033 *  In: BlueType/Spec,Y
   2034 *
   2035 *  Out: A = objid
   2036 *       state = state
   2037 *
   2038 *  Preserves X & Y
   2039 *
   2040 *-------------------------------
   2041 getobjid
   2042  lda SCRNUM
   2043  beq GOnull ;null scrn has no blueprint
   2044 
   2045 * Use getobjid1 for screen #s other than SCRNUM
   2046 
   2047 getobjid1
   2048  do EditorDisk
   2049  lda inbuilder
   2050  bne getobjbldr
   2051  fin
   2052 
   2053  lda (BlueSpec),y
   2054  sta state
   2055 
   2056  lda (BlueType),y
   2057  and #idmask
   2058 
   2059  cmp #pressplate
   2060  beq :plate
   2061 
   2062  cmp #upressplate
   2063  beq :upp
   2064 
   2065  rts
   2066 
   2067 * Handle depressed pressplate
   2068 
   2069 :plate lda state ;LINKLOC index
   2070  tax
   2071  lda LINKMAP,x
   2072 
   2073  and #%00011111 ;bits 0-4
   2074  cmp #2
   2075  bcc :up
   2076 
   2077  lda #dpressplate ;plate depressed
   2078  rts
   2079 
   2080 :up lda #pressplate ;plate up
   2081  rts
   2082 
   2083 * Handle depressed upressplate
   2084 
   2085 :upp lda state
   2086  tax
   2087  lda LINKMAP,x
   2088  and #%00011111
   2089  cmp #2
   2090  bcc :up1
   2091 
   2092  lda #0
   2093  sta state
   2094  lda #floor ;depressed upp looks just like floor
   2095  rts
   2096 
   2097 :up1 lda #upressplate
   2098  rts
   2099 
   2100 * Null screen is black
   2101 
   2102 GOnull
   2103  do EditorDisk
   2104  lda inmenu
   2105  bne getobjbldr
   2106  fin
   2107 
   2108  lda #space
   2109  rts
   2110 
   2111 *-------------------------------
   2112 *
   2113 * In builder: BlueSpec contains initial gadget settings
   2114 *
   2115 *-------------------------------
   2116  do EditorDisk
   2117 getobjbldr
   2118  lda (BlueType),y
   2119  and #idmask
   2120  pha
   2121  jsr getinitobj1
   2122  bcs :ok
   2123  lda (BlueSpec),y
   2124 :ok sta state
   2125  pla
   2126  rts
   2127  fin
   2128 
   2129 *-------------------------------
   2130 *
   2131 *  Sort objects in sort list into back-to-front order
   2132 *  (Foremost object should be at bottom of list)
   2133 *
   2134 *-------------------------------
   2135 sortlist
   2136 
   2137 :newpass
   2138  lda #0
   2139  sta switches ;no switches yet this pass
   2140 
   2141  ldx sortX ;start at bottom of list
   2142 
   2143 :loop cpx #1 ;at top of list?
   2144  beq :attop ;yes--pass complated
   2145 
   2146  stx xsave
   2147 
   2148  jsr compare ;Is obj[X] in front of obj[X-1]?
   2149 
   2150  ldx xsave
   2151 
   2152  bcc :ok ;Yes--continue
   2153 ;No--switch objects
   2154  lda sortX,x
   2155  pha
   2156  lda sortX-1,x
   2157  sta sortX,x
   2158  pla
   2159  sta sortX-1,x ;switch [X] with [X-1]
   2160 
   2161 :ok dex
   2162  bne :loop ;move up in list
   2163 
   2164 * At top of list--pass completed
   2165 
   2166 :attop
   2167  lda switches ;Any switches this pass?
   2168  bne :newpass ;Yes--do it again
   2169 
   2170 * No switches made--objects are in order
   2171 
   2172 :rts rts
   2173 
   2174 *-------------------------------
   2175 *
   2176 *  Compare object [xsave] with object [xsave-1]
   2177 *
   2178 *  If X is IN FRONT OF X-1, or if it doesn't matter, return cc;
   2179 *  If X is BEHIND X-1, return cs (switch 'em).
   2180 *
   2181 *-------------------------------
   2182 compare
   2183  lda sortX,x
   2184  sta obj1 ;obj index [X]
   2185  lda sortX-1,x
   2186  sta obj2 ;obj index [X-1]
   2187 
   2188  ldx obj1
   2189  ldy obj2
   2190 
   2191  lda objTYP,x
   2192  cmp #TypeShad
   2193  beq :xinfront ;enemy is always in front
   2194 
   2195  lda objY,x
   2196  cmp objY,y
   2197  beq :same
   2198  bcc :xinfront
   2199  bcs :yinfront
   2200 
   2201 :same
   2202 :xinfront clc
   2203  rts
   2204 
   2205 :yinfront sec
   2206  rts
   2207 
   2208 *-------------------------------
   2209 *
   2210 *  Get initial state of object
   2211 *
   2212 *  In: BlueType, BlueSpec, Y
   2213 *
   2214 *  Return cs if it matters, else cc
   2215 *
   2216 *-------------------------------
   2217 GETINITOBJ
   2218  lda (BlueType),y
   2219  and #idmask ;get objid
   2220 
   2221 getinitobj1
   2222  cmp #gate
   2223  beq :okgate
   2224  cmp #loose
   2225  beq :okloose
   2226  cmp #flask
   2227  beq :okflask
   2228  bne :skip ;if it isn't a gadget, leave it alone
   2229 
   2230 :okgate lda (BlueSpec),y ;1=gate up, 2=gate down, etc.
   2231  tax
   2232  lda initsettings-1,x
   2233  sec
   2234  rts
   2235 
   2236 :okloose lda #0 ;loose floor
   2237  rts
   2238 
   2239 :okflask lda (BlueSpec),y
   2240  asl
   2241  asl
   2242  asl
   2243  asl
   2244  asl ;5x
   2245  sec
   2246  rts
   2247 
   2248 :skip clc
   2249 ]rts rts
   2250 
   2251 *-------------------------------
   2252 metbufs3 jsr mbsub
   2253  iny
   2254 metbufs2 jsr mbsub
   2255  iny
   2256 mbsub ora redbuf,y
   2257  ora floorbuf,y
   2258  ora halfbuf,y
   2259  ora fredbuf,y
   2260  ora wipebuf,y
   2261  rts
   2262 
   2263 *-------------------------------
   2264  lst
   2265  ds 1
   2266  usr $a9,3,$490,*-org
   2267  lst off