#Elements in e_blockqueue and e_boardblock are in the form (rot<<3) + idx mkarray "e_blockqueue" store #(rot<<3) + idx @defun "e_init" { -1 ntiles_x ntiles_y * replicate ntiles_x ntiles_y * mkarrayp "e_boardblock" gstore #(rot<<3) + idx 0 ntiles_x ntiles_y * replicate ntiles_x ntiles_y * mkarrayp "e_boardfilled" gstore #booleans e_pushnewblock e_newcblock #initialises cblock vars sys_rawmode } @defun "e_teardown" { sys_unrawmode } @defun "e_test" { 0 "i" store i b_nblocks < while 0 "j" store j 4 < while io_clearscreen io_windowbox 0 0 i j b_drawblock 1000000 sleep j 1 + "j" store j 4 < end i 1 + "i" store i b_nblocks < end io_pos_after io_goto } @defun "e_nextblock" { "e_blockqueue" swapoutvar arrlen 1 < if "ERROR: blockqueue empty while requesting nextblock" print error end 0 arridx swap arrpopf "e_blockqueue" gstore e_pushnewblock } @defun "e_peekblock" { "e_blockqueue" swapoutvar arrlen 1 < if -1 else 0 arridx end swap "e_blockqueue" gstore } @defun "e_pushnewblock" { "e_blockqueue" swapoutvar 0 7 randr arrpush "e_blockqueue" gstore } @defun "e_newcblock" { e_nextblock "e_cblock" gstore ntiles_x 2 - 2 / "e_cblockpos" gstore } @defun "e_fullrender" { io_clearscreen io_windowbox io_printheader "e_boardblock" swapoutvar arrlen "sz" store 0 "i" store i sz < while i arridx dup -1 = ! if i ntiles_x % swap i ntiles_x / swap dup 8 % swap 8 / b_drawblock else pop end i 1 + "i" store i sz < end "e_boardblock" gstore io_pos_windowend pop 4 + 2 io_pos_windowstart add2 io_goto "Next block:" print ntiles_x 3 + 1 e_peekblock dup 8 % swap 8 / b_drawblock } @defun "e_validcbp" { #args: cblock cblockpos (compares purely to e_boardfilled) dup ntiles_x % swap ntiles_x / "cbposy" store "cbposx" store dup 8 % swap 8 / b_getblock "bits" store 1 "retval" store 0 "y" store bits while 0 "x" store x 4 < bits !! & while bits 1 & if cbposx x + 1 < cbposx x + ntiles_x 1 - > | cbposy y + ntiles_y 1 - > | if 0 "retval" store 0 "bits" store #make the loops break else "e_boardfilled" swapoutvar ntiles_x cbposy y + * cbposx x + + arridx swap "e_boardfilled" gstore if 0 "retval" store 0 "bits" store #make the loops break end end end bits 2 / "bits" store x 1 + "x" store x 4 < bits !! & end y 1 + "y" store bits end retval } @defun "e_applyblock" { #args: block blockpos dup "blockpos" store dup ntiles_x % swap ntiles_x / "blockposy" store "blockposx" store dup "block" store dup 8 % swap 8 / b_getblock "bits" store "e_boardblock" swapoutvar block blockpos arrset "e_boardblock" gstore "e_boardfilled" swapoutvar 0 "y" store y 4 < bits !! & while 0 "x" store x 4 < while bits 1 & if 1 blockposy y + ntiles_x * blockposx x + + arrset end bits 2 / "bits" store x 1 + "x" store x 4 < end y 1 + "y" store y 4 < bits !! & end "e_boardfilled" gstore } @defun "e_getuserinput" { getc dup "h" = if e_cblockpos 1 - "newcbp" store e_cblockpos ntiles_x % 0 > e_cblock newcbp e_validcbp & if newcbp "e_cblockpos" gstore end end dup "l" = if e_cblockpos 1 + "newcbp" store e_cblockpos ntiles_x % ntiles_x 1 - < e_cblock newcbp e_validcbp & if newcbp "e_cblockpos" gstore end end dup "j" = if e_cblock 8 % e_cblock 8 / 3 + 4 % 8 * + "newcb" store newcb e_cblockpos e_validcbp if newcb "e_cblock" gstore end end dup "j" = if e_cblock 8 % e_cblock 8 / 1 + 4 % 8 * + "newcb" store newcb e_cblockpos e_validcbp if newcb "e_cblock" gstore end end pop } @defun "e_sleep_userinput" { sleep_time "timeout" store 1 while 0 1 mkarrayp mkarray mkarray timeout sys_select "timeout" store pop pop arrlen if e_getuserinput stackdump end pop timeout end } @defun "e_runloop" { 0 "exitrunloop" store exitrunloop ! while e_fullrender e_cblockpos dup ntiles_x % swap ntiles_x / e_cblock dup 8 % swap 8 / dup4 b_drawblock io_pos_after io_goto #sleep_time sleep e_sleep_userinput b_undrawblock e_cblockpos ntiles_x + #new cblockpos dup e_cblock swap e_validcbp if "e_cblockpos" gstore else pop e_cblock e_cblockpos e_applyblock e_newcblock e_cblock e_cblockpos e_validcbp ! if 1 "exitrunloop" store e_blockqueue e_cblock arrpushf "e_blockqueue" gstore e_fullrender io_pos_after io_goto "GAME OVER!\n" print end end exitrunloop ! end }