drop_sokoban.p8 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. pico-8 cartridge // http://www.pico-8.com
  2. version 32
  3. __lua__
  4. function _init()
  5. u_state, d_state = u_waiting, d_waiting
  6. end
  7. function _update()
  8. u_state()
  9. end
  10. function _draw()
  11. d_state()
  12. end
  13. -->8
  14. -- waiting for a level
  15. -- 0x4300 - 0x5600
  16. function u_waiting()
  17. if (not stat(120)) return
  18. -- resetting the player
  19. player_x, player_y = nil, nil
  20. -- resetting the goal counter
  21. remaining_goals = 0
  22. -- read header
  23. local end_of_header = false
  24. local header_length = 0
  25. repeat
  26. local next_byte_addr = 0x4300 + header_length
  27. serial(0x800, next_byte_addr, 1)
  28. header_length += 1
  29. -- stop at newline
  30. end_of_header = @next_byte_addr == ord("\n")
  31. until end_of_header
  32. -- parse the string
  33. -- moved here for readability
  34. -- but could be in the header loading code
  35. local header_str = ""
  36. for i=0,header_length-1 do
  37. header_str ..= chr(@(i+0x4300))
  38. end
  39. -- map dimension extraction
  40. -- let's say there is only one space between width and height
  41. local map_width, map_height
  42. for i=1,#header_str do
  43. local c = sub(header_str,i,i)
  44. if c == " " then
  45. map_width = sub(header_str,1,i) + 0
  46. map_height = sub(header_str,i+1) + 0
  47. break
  48. end
  49. end
  50. -- map read
  51. for y=0,map_height - 1 do
  52. -- read a line
  53. -- map_width + 1 to read the newline character
  54. serial(0x800, 0x4300, map_width + 1)
  55. -- parse the read data into the map data
  56. for x=0,map_width - 1 do
  57. local val = @(0x4300 + x)
  58. if val == ord("#") then
  59. mset(x, y, 1)
  60. -- goal
  61. elseif val == ord(".") then
  62. mset(x, y, 2)
  63. remaining_goals += 1
  64. -- box
  65. elseif val == ord("$") then
  66. mset(x, y, 3)
  67. -- box on goal
  68. elseif val == ord("*") then
  69. mset(x, y, 4)
  70. -- player
  71. elseif val == ord("@") then
  72. player_x, player_y = x, y
  73. -- player on goal
  74. elseif val == ord("+") then
  75. player_x, player_y = x, y
  76. remaining_goals += 1
  77. mset(x, y, 4)
  78. end
  79. end
  80. end
  81. u_state, d_state = u_game, d_game
  82. end
  83. function d_waiting()
  84. cls()
  85. print("waiting for a file to drop", 7)
  86. local center_x, center_y = 64+32 * sin(t()/4), 96 + cos(t()/3)*16
  87. circfill(center_x-1, center_y-1, 7.5, 1)
  88. spr(t()%5, center_x-4, center_y-4)
  89. end
  90. -->8
  91. -- "game" state
  92. -- oh, you know what?
  93. -- let's do a sokoban
  94. function u_game()
  95. local move_x, move_y = 0, 0
  96. if(btnp(➡️)) move_x = 1
  97. if(btnp(⬅️)) move_x = -1
  98. if(btnp(⬇️)) move_y = 1
  99. if(btnp(⬆️)) move_y = -1
  100. if remaining_goals == 0 then
  101. u_state, d_state = u_waiting, d_waiting
  102. end
  103. if move_x ~= 0 or move_y ~= 0 then
  104. local next_x, next_y = player_x + move_x, player_y + move_y
  105. local has_box = fget(mget(next_x, next_y), 1)
  106. local has_wall = fget(mget(next_x, next_y), 2)
  107. local has_goal = fget(mget(next_x, next_y), 0)
  108. if has_box then
  109. next_box_x, next_box_y = player_x + move_x*2, player_y + move_y*2
  110. -- next box tile has box or wall? abort
  111. if fget(mget(next_box_x, next_box_y), 1) or fget(mget(next_box_x, next_box_y), 2) then
  112. return
  113. end
  114. mset(next_x, next_y, has_goal and 2 or 0)
  115. local has_next_goal = fget(mget(next_box_x, next_box_y), 0)
  116. mset(next_box_x, next_box_y, has_next_goal and 4 or 3)
  117. if(has_goal)remaining_goals += 1
  118. if(has_next_goal)remaining_goals -= 1
  119. end
  120. if not has_wall then
  121. player_x, player_y = next_x, next_y
  122. end
  123. end
  124. end
  125. function d_game()
  126. cls()
  127. map()
  128. spr(0, player_x*8, player_y*8)
  129. end
  130. __gfx__
  131. 0aaaaaa0666066665000000544444444dddddddd0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  132. aaaaaaaa666066665555555540044004dd0000dd0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  133. aa0aa0aa666066660005500044000044d00dd00d0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  134. aaaaaaaa000000000050050040044004dd0000dd0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  135. a000000a666660660500005044444444dddddddd0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  136. a000000a666660660500005040444404d0dddd0d0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  137. 0a0000a0666660665050050540000004d000000d0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  138. 00aaaa00000000005555555540444404d0dddd0d0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  139. __label__
  140. 88806666666066666660666666606666666066666660666666606666000000000000000000000000000000000000000000000000000000000000000000000000
  141. 66806666666066666660666666606666666066666660666666606666000000000000000000000000000000000000000000000000000000000000000000000000
  142. 88806666666066666660666666606666666066666660666666606666000000000000000000000000000000000000000000000000000000000000000000000000
  143. 80000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  144. 88866066666660666666606666666066666660666666606666666066000000000000000000000000000000000000000000000000000000000000000000000000
  145. 66666066666660666666606666666066666660666666606666666066000000000000000000000000000000000000000000000000000000000000000000000000
  146. 66666066666660666666606666666066666660666666606666666066000000000000000000000000000000000000000000000000000000000000000000000000
  147. 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  148. 66606666dddddddd0000000000000000666066660000000066606666000000000000000000000000000000000000000000000000000000000000000000000000
  149. 66606666dd0000dd0000000000000000666066660000000066606666000000000000000000000000000000000000000000000000000000000000000000000000
  150. 66606666d00dd00d0000000000000000666066660000000066606666000000000000000000000000000000000000000000000000000000000000000000000000
  151. 00000000dd0000dd0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  152. 66666066dddddddd0000000000000000666660660000000066666066000000000000000000000000000000000000000000000000000000000000000000000000
  153. 66666066d0dddd0d0000000000000000666660660000000066666066000000000000000000000000000000000000000000000000000000000000000000000000
  154. 66666066d000000d0000000000000000666660660000000066666066000000000000000000000000000000000000000000000000000000000000000000000000
  155. 00000000d0dddd0d0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  156. 6660666600000000dddddddd00000000444444440000000066606666000000000000000000000000000000000000000000000000000000000000000000000000
  157. 6660666600000000dd0000dd00000000400440040000000066606666000000000000000000000000000000000000000000000000000000000000000000000000
  158. 6660666600000000d00dd00d00000000440000440000000066606666000000000000000000000000000000000000000000000000000000000000000000000000
  159. 0000000000000000dd0000dd00000000400440040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  160. 6666606600000000dddddddd00000000444444440000000066666066000000000000000000000000000000000000000000000000000000000000000000000000
  161. 6666606600000000d0dddd0d00000000404444040000000066666066000000000000000000000000000000000000000000000000000000000000000000000000
  162. 6666606600000000d000000d00000000400000040000000066666066000000000000000000000000000000000000000000000000000000000000000000000000
  163. 0000000000000000d0dddd0d00000000404444040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  164. 6660666600000000000000000aaaaaa0444444440000000066606666000000000000000000000000000000000000000000000000000000000000000000000000
  165. 666066660000000000000000aaaaaaaa400440040000000066606666000000000000000000000000000000000000000000000000000000000000000000000000
  166. 666066660000000000000000aa0aa0aa440000440000000066606666000000000000000000000000000000000000000000000000000000000000000000000000
  167. 000000000000000000000000aaaaaaaa400440040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  168. 666660660000000000000000a000000a444444440000000066666066000000000000000000000000000000000000000000000000000000000000000000000000
  169. 666660660000000000000000a000000a404444040000000066666066000000000000000000000000000000000000000000000000000000000000000000000000
  170. 6666606600000000000000000a0000a0400000040000000066666066000000000000000000000000000000000000000000000000000000000000000000000000
  171. 00000000000000000000000000aaaa00404444040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  172. 66606666000000005000000550000005000000000000000066606666000000000000000000000000000000000000000000000000000000000000000000000000
  173. 66606666000000005555555555555555000000000000000066606666000000000000000000000000000000000000000000000000000000000000000000000000
  174. 66606666000000000005500000055000000000000000000066606666000000000000000000000000000000000000000000000000000000000000000000000000
  175. 00000000000000000050050000500500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  176. 66666066000000000500005005000050000000000000000066666066000000000000000000000000000000000000000000000000000000000000000000000000
  177. 66666066000000000500005005000050000000000000000066666066000000000000000000000000000000000000000000000000000000000000000000000000
  178. 66666066000000005050050550500505000000000000000066666066000000000000000000000000000000000000000000000000000000000000000000000000
  179. 00000000000000005555555555555555000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  180. 666066660000000000000000dddddddd000000000000000066606666000000000000000000000000000000000000000000000000000000000000000000000000
  181. 666066660000000000000000dd0000dd000000000000000066606666000000000000000000000000000000000000000000000000000000000000000000000000
  182. 666066660000000000000000d00dd00d000000000000000066606666000000000000000000000000000000000000000000000000000000000000000000000000
  183. 000000000000000000000000dd0000dd000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  184. 666660660000000000000000dddddddd000000000000000066666066000000000000000000000000000000000000000000000000000000000000000000000000
  185. 666660660000000000000000d0dddd0d000000000000000066666066000000000000000000000000000000000000000000000000000000000000000000000000
  186. 666660660000000000000000d000000d000000000000000066666066000000000000000000000000000000000000000000000000000000000000000000000000
  187. 000000000000000000000000d0dddd0d000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  188. 66606666666066666660666666606666666066666660666666606666000000000000000000000000000000000000000000000000000000000000000000000000
  189. 66606666666066666660666666606666666066666660666666606666000000000000000000000000000000000000000000000000000000000000000000000000
  190. 66606666666066666660666666606666666066666660666666606666000000000000000000000000000000000000000000000000000000000000000000000000
  191. 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  192. 66666066666660666666606666666066666660666666606666666066000000000000000000000000000000000000000000000000000000000000000000000000
  193. 66666066666660666666606666666066666660666666606666666066000000000000000000000000000000000000000000000000000000000000000000000000
  194. 66666066666660666666606666666066666660666666606666666066000000000000000000000000000000000000000000000000000000000000000000000000
  195. 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  196. 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  197. 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  198. 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  199. 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  200. 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  201. 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  202. 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  203. 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  204. 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  205. 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  206. 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  207. 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  208. 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  209. 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  210. 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  211. 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  212. 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  213. 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  214. 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  215. 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  216. 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  217. 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  218. 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  219. 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  220. 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  221. 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  222. 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  223. 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  224. 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  225. 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  226. 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  227. 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  228. 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  229. 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  230. 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  231. 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  232. 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  233. 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  234. 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  235. 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  236. 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  237. 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  238. 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  239. 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  240. 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  241. 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  242. 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  243. 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  244. 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  245. 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  246. 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  247. 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  248. 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  249. 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  250. 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  251. 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  252. 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  253. 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  254. 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  255. 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  256. 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  257. 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  258. 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  259. 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  260. 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  261. 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  262. 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  263. 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  264. 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  265. 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  266. 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  267. 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  268. __gff__
  269. 0004010203000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  270. 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000