uxn

Varvara Ordinator, written in ANSI C(SDL2)
git clone https://git.eamoncaddigan.net/uxn.git
Log | Files | Refs | README | LICENSE

darena.tal (10663B)


      1 ( darena.tal )
      2 ( an open-ended game of rocks and sand )
      3 ( contributed by and cc0 sejo 12021 )
      4 
      5 ( parameters )
      6 %nrocks { #1f }
      7 %nrocks-1 { #1e }
      8 %nrocks_mask { #1f }
      9 %minposx { #0f }
     10 %minposy { #0f }
     11 %maxposx { #f1 }
     12 %maxposy { #f1 }
     13 %anispeedmask_normal { #03 }
     14 %anispeedmask_slow { #07 }
     15 
     16 %c_color_normal { #43 }
     17 %c_color_flipx { #53 }
     18 %index_norock { #ff }
     19 
     20 ( output macros )
     21 %out_screen_x { LDA #00 SWP .Screen/x DEO2 } ( ;addr )
     22 %out_screen_y { LDA #00 SWP .Screen/y DEO2 } ( ;addr )
     23 
     24 ( helper macros )
     25 %get_bit_n { SFT #01 AND }
     26 %get_nibble_h { #04 SFT #0f AND }
     27 %get_nibble_l { #0f AND }
     28 
     29 %is_bit_n_set { get_bit_n #01 EQU }
     30 
     31 %set_animate { #01 ;c_state LDA ORA ;c_state STA }
     32 %rst_animate { #00 ;c_state STA }
     33 
     34 ( devices )
     35 
     36 |00 @System     [ &vector $2 &wst      $1 &rst    $1 &pad   $4 &r      $2 &g     $2 &b      $2 ]
     37 |20 @Screen     [ &vector $2 &width    $2 &height $2 &pad   $2 &x      $2 &y      $2 &addr  $2 &pixel $1 &sprite $1 ]
     38 |80 @Controller [ &vector $2 &button   $1 &key    $1 ]
     39 
     40 ( variables )
     41 
     42 |0000
     43 
     44 @c_pos [ &x $1 &y $1 ] ( character position )
     45 @c_speed [ &x $1 &y $1 ] ( character speed )
     46 @c_color [ $1 ] ( character color )
     47 @c_sprite [ $2 ] ( character sprite addr )
     48 @c_state [ $1 ] ( high_nibble: animation pointer, bit0: is_animated )
     49 
     50 @f_count [ $1 ] ( frame counter )
     51 @ani_speedmask [ $1 ] ( animation speed mask )
     52 
     53 @r_speed_x [ $f ]
     54 @r_speed_y [ $f ]
     55 
     56 @tog [ &x $1 &y $1 &state $1 ] ( toggle station state )
     57 
     58 ( program )
     59 
     60 |0100 @reset ( -> )
     61 	#f396 .System/r DEO2
     62 	#e263 .System/g DEO2
     63 	#9030 .System/b DEO2
     64 
     65 	;on_frame .Screen/vector DEO2
     66 
     67 	( init character )
     68 	#50 ;c_pos/x STA
     69 	#10 ;c_pos/y STA
     70 	#00 ;c_speed/x STA
     71 	#00 ;c_speed/y STA
     72 	c_color_normal ;c_color STA
     73 	;s_monitx_stepfront0 ;c_sprite STA2
     74 	rst_animate
     75 
     76 	anispeedmask_normal ;ani_speedmask STA
     77 
     78 	( init toggler )
     79 	#27 ;tog/x STA
     80 	#27 ;tog/y STA
     81 	#00 ;tog/state STA
     82 
     83 
     84 	( init background )
     85 	;init_bg JSR2
     86 BRK
     87 
     88 
     89 @on_frame ( -> )
     90 	;f_count LDA INC DUP ;f_count STA ( increase frame counter )
     91 	;ani_speedmask LDA ( mask with animation speed mask )
     92 	AND #00 EQU ,update_frame JCN ( jump to update if it's time )
     93 BRK
     94 
     95 @update_frame
     96 	( check keyboard )
     97 	;check_keys JSR2
     98 
     99 	( animate character sprite )
    100 	;animate_c JSR2
    101 
    102 	( clear sprites )
    103 	;clear JSR2
    104 
    105 	( update character vars )
    106 	;update_c/run JSR2
    107 
    108 	( update rocks + stand )
    109 	;update_r/run JSR2
    110 
    111 	( draw )
    112 	;draw JSR2
    113 
    114 BRK
    115 
    116 @clear
    117 	( clear rocks )
    118 	;s_clear .Screen/addr DEO2
    119 
    120 	nrocks #00
    121 	&rocks_loop
    122 		DUP ( get rocks_x[i] )
    123 		;rocks_x ROT #00 SWP ADD2 out_screen_x
    124 
    125 		DUP ( get rocks_y[i] )
    126 		;rocks_y ROT #00 SWP ADD2 out_screen_y
    127 
    128 		#40 .Screen/sprite DEO
    129 
    130 		INC
    131 		DUP2
    132 		NEQ ,&rocks_loop JCN
    133 	POP2
    134 
    135 	( clear character )
    136 	;clear_c JSR2
    137 JMP2r
    138 
    139 @draw
    140 	( draw toggler )
    141 
    142 	;tog/x out_screen_x
    143 	;tog/x out_screen_y
    144 	;s_stand .Screen/addr DEO2
    145 	#03 .Screen/sprite DEO
    146 
    147 	( draw rocks )
    148 	;s_bola .Screen/addr DEO2
    149 
    150 	nrocks #00
    151 
    152 	&rocks_loop
    153 		DUP ( get rocks_x[i] )
    154 		;rocks_x ROT #00 SWP ADD2 out_screen_x
    155 
    156 		DUP ( get rocks_y[i] )
    157 		;rocks_y ROT #00 SWP ADD2 out_screen_y
    158 
    159 		DUP ( get color )
    160 		;r_color ROT #00 SWP ADD2 LDA #41 ADD .Screen/sprite DEO
    161 
    162 		INC
    163 
    164 		DUP2
    165 		NEQ ,&rocks_loop JCN
    166 	POP2
    167 
    168 	( draw character )
    169 	;draw_c JSR2
    170 JMP2r
    171 
    172 @check_keys
    173 	#00 ;c_speed/x STA
    174 	#00 ;c_speed/y STA
    175 
    176 	.Controller/button DEI #07 is_bit_n_set ,&der JCN
    177 	.Controller/button DEI #06 is_bit_n_set ,&izq JCN
    178 	.Controller/button DEI #05 is_bit_n_set ,&aba JCN
    179 	.Controller/button DEI #04 is_bit_n_set ,&arr JCN
    180 
    181 	rst_animate
    182 
    183 	JMP2r
    184 
    185 	&der
    186 		#01 ;c_speed/x STA
    187 		set_animate
    188 		c_color_normal ;c_color STA
    189 		;s_monitx_stepside0 ;c_sprite STA2
    190 	JMP2r
    191 
    192 	&izq
    193 		#ff ;c_speed/x STA
    194 		set_animate
    195 		c_color_flipx ;c_color STA
    196 		;s_monitx_stepside0 ;c_sprite STA2
    197 	JMP2r
    198 
    199 	&aba
    200 		#01 ;c_speed/y STA
    201 		set_animate
    202 		c_color_normal ;c_color STA
    203 		;s_monitx_stepfront0 ;c_sprite STA2
    204 	JMP2r
    205 
    206 	&arr
    207 		#ff ;c_speed/y STA
    208 		set_animate
    209 		c_color_normal ;c_color STA
    210 		;s_monitx_stepback0 ;c_sprite STA2
    211 	JMP2r
    212 
    213 	&end
    214 JMP2r
    215 
    216 ( sub-routines )
    217 
    218 ( in: sourcex, source y, index, rangex, rangey )
    219 ( puts in the stack the index of rock collisioned with )
    220 @collision_rocks
    221 	&range_y $1
    222 	&range_x $1
    223 	&src_i $1
    224 	&src_x  $1
    225 	&src_y $1
    226 
    227 	&rock_x $1
    228 	&rock_y $1
    229 
    230 	&run
    231 		,&range_y STR
    232 		,&range_x STR
    233 		,&src_i STR
    234 		,&src_y STR
    235 		,&src_x STR
    236 
    237 		( check collision with rocks )
    238 		( nrocks #00 )
    239 		,&src_i LDR nrocks_mask AND DUP INC nrocks_mask AND
    240 
    241 		&rocks_loop
    242 			DUP ( get rocks_x[i] )
    243 			;rocks_x ROT #00 SWP ADD2 LDA ,&rock_x STR
    244 
    245 			DUP ( get rocks_y[i] )
    246 			;rocks_y ROT #00 SWP ADD2 LDA ,&rock_y STR
    247 
    248 			,&src_x LDR ,&rock_x LDR ,&range_x LDR SUB GTH ( if sx > rx - 8  )
    249 			,&src_x LDR ,&rock_x LDR ,&range_x LDR ADD LTH ( if sx < rx + 8  )
    250 			,&src_y LDR ,&rock_y LDR ,&range_y LDR SUB GTH ( if sy > ry - 8  )
    251 			,&src_y LDR ,&rock_y LDR ,&range_y LDR ADD LTH ( if sy < ry + 8  )
    252 			ADD ADD ADD #04 EQU ,&found JCN
    253 
    254 			INC nrocks_mask AND
    255 			DUP2
    256 			NEQ ,&rocks_loop JCN
    257 		POP2
    258 		#ff
    259 		JMP2r
    260 	&found
    261 		NIP ( remove loop limit )
    262 		DUP ;&src_i LDA NEQ ,&end JCN ( check if result is the same as index )
    263 		POP #ff
    264 		JMP2r
    265 
    266 	&end
    267 
    268 JMP2r
    269 
    270 @update_c ( update character position )
    271 	&new_x $1
    272 	&new_y $1
    273 
    274 	&rock_i $1
    275 	&rock_x $1
    276 	&rock_y $1
    277 
    278 
    279 	&run
    280 		;c_speed/x LDA ;c_pos/x LDA ADD
    281 		,&new_x STR
    282 		;c_speed/y LDA ;c_pos/y LDA ADD
    283 		,&new_y STR
    284 
    285 		anispeedmask_normal ;ani_speedmask STA
    286 
    287 	&check_x
    288 		( check collision with borders )
    289 		,&new_x LDR minposx EQU ;&noup_x JCN2
    290 		,&new_x LDR maxposx EQU ;&noup_x JCN2
    291 
    292 
    293 		( check collision with rocks )
    294 		,&new_x LDR ,&new_y LDR index_norock #09 #06
    295 		;collision_rocks/run JSR2
    296 
    297 		( if it is colliding with rock, check further )
    298 		DUP #ff NEQ ,&check_x_collision JCN
    299 		POP
    300 		,&update_x JMP
    301 
    302 	&check_x_collision
    303 		( DUP DEBUG )
    304 		( slow down and save rock index )
    305 		anispeedmask_slow ;ani_speedmask STA
    306 		,&rock_i STR
    307 
    308 		( check if rock collides with others )
    309 		;rocks_x #00 ,&rock_i LDR ADD2 LDA ,&rock_x STR
    310 		;rocks_y #00 ,&rock_i LDR ADD2 LDA ,&rock_y STR
    311 
    312 		,&rock_x LDR ,&rock_y LDR ,&rock_i LDR #09 #06
    313 		;collision_rocks/run JSR2
    314 
    315 		( DUP DEBUG )
    316 
    317 		( if it is colliding, then skip adding x )
    318 		DUP #ff NEQ ,&check_y JCN
    319 		POP
    320 
    321 
    322 		( if not, check for borders )
    323 		;&rock_x LDA minposx EQU ;&noup_x JCN2
    324 		;&rock_x LDA maxposx EQU ;&noup_x JCN2
    325 
    326 		( move rock with same speed as c )
    327 		;&rock_x LDA ;c_speed/x LDA ADD
    328 		;rocks_x #00 ;&rock_i LDA ADD2
    329 		STA
    330 
    331 
    332 	&update_x
    333 		;&new_x LDA ;c_pos/x STA
    334 
    335 	,&check_y JMP
    336 
    337 	&noup_x
    338 
    339 	&check_y
    340 		( check collision with borders )
    341 		;&new_y LDA minposy EQU ;&noup_y JCN2
    342 		;&new_y LDA maxposy EQU ;&noup_y JCN2
    343 
    344 		( check collision with rocks )
    345 		;&new_x LDA ;&new_y LDA index_norock #06 #09
    346 		;collision_rocks/run JSR2
    347 
    348 		( if it is colliding with rock, check further )
    349 		DUP #ff NEQ ,&check_y_collision JCN
    350 		POP
    351 		,&update_y JMP
    352 
    353 	&check_y_collision
    354 		( DUP DEBUG )
    355 		anispeedmask_slow ;ani_speedmask STA
    356 		;&rock_i STA
    357 
    358 		( check if rock collides with others )
    359 		;rocks_x #00 ;&rock_i LDA ADD2 LDA ;&rock_x STA
    360 		;rocks_y #00 ;&rock_i LDA ADD2 LDA ;&rock_y STA
    361 
    362 		;&rock_x LDA ;&rock_y LDA ;&rock_i LDA #06 #09
    363 		;collision_rocks/run JSR2
    364 
    365 		( DUP DEBUG )
    366 
    367 		( if it is colliding, then skip adding y )
    368 		DUP #ff NEQ ,&noup_y JCN
    369 		POP
    370 
    371 		( if not, check for borders )
    372 		;&rock_y LDA minposx EQU ;&noup_y JCN2
    373 		;&rock_y LDA maxposx EQU ;&noup_y JCN2
    374 
    375 		( if not colliding, then move rock with same speed as c )
    376 		;&rock_y LDA ;c_speed/y LDA ADD
    377 		;rocks_y #00 ;&rock_i LDA ADD2
    378 		STA
    379 
    380 
    381 	&update_y
    382 		;&new_y LDA ;c_pos/y STA
    383 	JMP2r
    384 
    385 	&noup_y
    386 JMP2r
    387 
    388 @update_r
    389 	&rock_i $1
    390 
    391 	&run
    392 
    393 		( check collision with rocks )
    394 		;tog/x LDA ;tog/y LDA index_norock #02 #02
    395 		;collision_rocks/run JSR2
    396 
    397 		( if it is colliding with rock, check if it needs to change state )
    398 		DUP #ff NEQ ,&change_state JCN
    399 
    400 		( DUP DEBUG )
    401 
    402 		( if there's no collision, reset toggler )
    403 		POP
    404 		#00 ;tog/state STA
    405 		JMP2r
    406 
    407 	&change_state
    408 		( DUP DEBUG )
    409 		,&rock_i STR
    410 		;tog/state LDA ,&done JCN ( don't toggle if state is active )
    411 
    412 		;r_color #00 ,&rock_i LDR ADD2 DUP2 STH2
    413 		LDA #01 EOR STH2r STA
    414 		#01 ;tog/state STA
    415 	&done
    416 
    417 JMP2r
    418 
    419 @animate_c
    420 	( is bit0 -animate- on? )
    421 	;c_state LDA DUP #00 get_bit_n #01 NEQ ,&s_no_animate JCN
    422 
    423 	( increment and save animation pointer )
    424 	&s_animate
    425 		DUP
    426 		get_nibble_h INC #03 AND #40 SFT
    427 		SWP get_nibble_l ORA
    428 		;c_state STA
    429 	JMP2r
    430 
    431 	&s_no_animate
    432 		get_nibble_h #0f AND ;c_state STA
    433 JMP2r
    434 
    435 @draw_c ( draw character  )
    436 	#00 ;c_state LDA get_nibble_h #30 SFT
    437 	;c_sprite LDA2 ADD2 .Screen/addr DEO2
    438 	;c_pos/x out_screen_x
    439 	;c_pos/y out_screen_y
    440 	;c_color LDA .Screen/sprite DEO
    441 JMP2r
    442 
    443 @clear_c ( clear character )
    444 	;s_clear .Screen/addr DEO2
    445 	;c_pos/x out_screen_x
    446 	;c_pos/y out_screen_y
    447 	#40 .Screen/sprite DEO
    448 JMP2r
    449 
    450 @init_bg
    451 	( init bg )
    452 	;s_border .Screen/addr DEO2
    453 
    454 	.Screen/height DEI2 #0000 STH2
    455 	&vertical0loop
    456 		DUP2
    457 		STH2r
    458 		DUP2 .Screen/y DEO2
    459 
    460 
    461 		.Screen/width DEI2 #0000 STH2
    462 		&horizontal0loop
    463 			DUP2
    464 			STH2r
    465 			DUP2 .Screen/x DEO2
    466 
    467 			#03 .Screen/sprite DEO
    468 
    469 			#0008 ADD2 DUP2 STH2
    470 			GTH2 ,&horizontal0loop JCN
    471 
    472 		STH2r POP2 POP2
    473 
    474 
    475 		#0008 ADD2 DUP2 STH2
    476 		GTH2 ,&vertical0loop JCN
    477 	STH2r
    478 	POP2 POP2
    479 
    480 	( arena )
    481 
    482 	;s_clear .Screen/addr DEO2
    483 
    484 	 #00 maxposy  #00 minposy STH2
    485 	&vertical0loop_clear
    486 		DUP2
    487 		STH2r
    488 		DUP2 .Screen/y DEO2
    489 
    490 
    491 		 #00 maxposx  #00 minposx  STH2
    492 		&horizontal0loop_clear
    493 			DUP2
    494 			STH2r
    495 			DUP2 .Screen/x DEO2
    496 
    497 			#00 .Screen/sprite DEO
    498 
    499 			#0008 ADD2 DUP2 STH2
    500 			GTH2 ,&horizontal0loop_clear JCN
    501 
    502 		STH2r POP2 POP2
    503 
    504 		#0008 ADD2 DUP2 STH2 GTH2 ,&vertical0loop_clear JCN
    505 	STH2r
    506 	POP2 POP2
    507 
    508 JMP2r
    509 
    510 ( rocks )
    511 @rocks_x [ 25 30 42 50  67 90 98 e8  20 43 43 57  5a 7f bc a5
    512            e5 dd a2 20  b7 9b 38 e8  33 43 63 b7  aa cf bc    ]
    513 @rocks_y [ 60 48 34 56  23 65 65 65  ba e9 24 22  72 91 22 c5
    514            25 30 42 50  67 90 98 e8  20 43 43 57  5a 7f bc    ]
    515 @r_color [ 00 01 01 00  00 00 01 01  01 01 00 00  01 01 00 00
    516            01 00 01 00  00 01 00 01  01 01 01 01  00 00 00    ]
    517 
    518 ( sprites )
    519 
    520 @s_clear          [ 0000 0000 0000 0000 ]
    521 @s_border         [ 3288 7e83 780d e013 ]
    522 @s_bola           [ 3c4e 9ffd f962 3c00 ]
    523 @s_stand          [ 0000 0000 0024 7eff ]
    524 @s_stand_original [ 0000 0000 0000 3c7e ]
    525 
    526 @s_monitx      [ 3c7e 5a7f 1b3c 5a18 ]
    527 @s_monitx_back [ 3c7e 7efe d83c 5a18 ]
    528 
    529 @s_monitx_stepfront0 [ 3c7e 5a7f 1b3c 5a18 ]
    530 @s_monitx_stepfront1 [ 3c7e 5a7f 1b3c 5a10 ]
    531 @s_monitx_stepfront2 [ 3c7e 5a7f 1b3c 5a18 ]
    532 @s_monitx_stepfront3 [ 3c7e 5a7f 1b3c 5a08 ]
    533 
    534 @s_monitx_stepback0 [ 3c7e 7efe d83c 5a18 ]
    535 @s_monitx_stepback1 [ 3c7e 7efe d83c 5a10 ]
    536 @s_monitx_stepback2 [ 3c7e 7efe d83c 5a18 ]
    537 @s_monitx_stepback3 [ 3c7e 7efe d83c 5a08 ]
    538 
    539 @s_monitx_stepside0 [ 1c3c 7afc d81c 1818 ]
    540 @s_monitx_stepside1 [ 1c3c 7afc d81c 1828 ]
    541 @s_monitx_stepside2 [ 1c3c 7afc d81c 3810 ]
    542 @s_monitx_stepside3 [ 1c3c 7afc d81c 1814 ]
    543