diamondmine.lua 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. -- title: Diamond Mine
  2. -- author: pixelbath
  3. -- desc: Unlike any match-3 game that ever existed! At least, I'm pretty sure...
  4. -- script: lua
  5. -- modes:
  6. -- - diamond mine (ofc)
  7. -- - poker?
  8. -- - classic
  9. local gems = {
  10. { spr=258 },
  11. { spr=260 },
  12. { spr=262 },
  13. { spr=264 },
  14. { spr=266 },
  15. { spr=268 },
  16. { spr=270 },
  17. }
  18. player = {
  19. x = 1, y = 1,
  20. cycle_t = 0, cycle_clr = 1, cycle_pal = { 1, 2, 3, 4, 5, 6, 7, 9, 10, 11, 13, 15, 14 },
  21. sel_x = 1, sel_y = 1, is_selected = false,
  22. }
  23. local board_w, board_h = 8, 8
  24. local board = {}
  25. local gem_sprites = {}
  26. local board_x, board_y = 50, 4
  27. function prt(text, x, y, color)
  28. print(text, x, y, color, true, 1, true)
  29. end
  30. function generate_gem(exclude_idx)
  31. -- exclude_idx = exclude_idx or 0
  32. local out_gem = math.random(1, #gems)
  33. if exclude_idx == out_gem then
  34. if out_gem + 1 > #gems then out_gem = 1 else outgem = out_gem + 1 end
  35. end
  36. return out_gem
  37. end
  38. function generate_gem_sprite(idx, xpos, ypos)
  39. return {
  40. spr = gems[idx].spr,
  41. x = xpos, y = ypos,
  42. dx = xpos, dy = ypos,
  43. t = 0,
  44. }
  45. end
  46. -- build the global board table from scratch
  47. function generate_board(allow_matches)
  48. allow_matches = allow_matches or 1
  49. for y = 1, board_h do
  50. board[y], gem_sprites[y] = {}, {}
  51. for x = 1, board_w do
  52. local gem_idx = generate_gem()
  53. board[y][x] = gem_idx
  54. gem_sprites[y][x] = generate_gem_sprite(gem_idx, (x-1)*16, (y-1)*16)
  55. end
  56. end
  57. end
  58. function swap_gems(src_x, src_y, dest_x, dest_y)
  59. -- local xdiff, ydiff = math.abs(dest_x-src_x), math.abs(dest_y-src_y)
  60. -- if xdiff ~= 1 or ydiff ~= 1 then return end
  61. -- if xdiff == 1 and ydiff == 1 then return end
  62. local src_gem = board[src_y][src_x]
  63. local dest_gem = board[dest_y][dest_x]
  64. board[src_y][src_x] = dest_gem
  65. board[dest_y][dest_x] = src_gem
  66. -- set the new destination for swap
  67. if src_x ~= dest_x then
  68. gem_sprites[src_y][src_x].dx = (dest_x - 1) * 16
  69. gem_sprites[dest_y][dest_x].dx = (src_x - 1) * 16
  70. elseif src_y ~= dest_y then
  71. gem_sprites[src_y][src_x].dy = (dest_y - 1) * 16
  72. gem_sprites[dest_y][dest_x].dy = (src_y - 1) * 16
  73. end
  74. end
  75. -- return: table of x,y pair tables, or nil
  76. function find_matches()
  77. local matches = {}
  78. -- brute force horizontal
  79. for y_test = 1, board_h do
  80. -- seed first gem
  81. local last_gem_x = board[y_test][1]
  82. local consecutive_x = 1
  83. for x_test = 2, board_w do
  84. local gem_idx = board[y_test][x_test]
  85. if last_gem_x == gem_idx then
  86. consecutive_x = consecutive_x + 1
  87. -- check once on the last column
  88. if x_test == board_w and consecutive_x >= 3 then
  89. for i = x_test - consecutive_x + 1, x_test do
  90. table.insert(matches, { i, y_test })
  91. end
  92. end
  93. else
  94. if consecutive_x >= 3 then
  95. for i = x_test - consecutive_x, x_test - 1 do
  96. table.insert(matches, { i, y_test })
  97. end
  98. end
  99. consecutive_x = 1
  100. end
  101. last_gem_x = gem_idx
  102. end
  103. end
  104. -- brute force vertical
  105. for x_test = 1, board_w do
  106. -- seed first gem
  107. local last_gem_y = board[1][x_test]
  108. local consecutive_y = 1
  109. for y_test = 2, board_h do
  110. local gem_idx = board[y_test][x_test]
  111. if last_gem_y == gem_idx then
  112. consecutive_y = consecutive_y + 1
  113. -- check once on the last row
  114. if y_test == board_h and consecutive_y >= 3 then
  115. for i = y_test - consecutive_y + 1, y_test do
  116. table.insert(matches, { x_test, i })
  117. end
  118. end
  119. else
  120. if consecutive_y >= 3 then
  121. for i = y_test - consecutive_y, y_test - 1 do
  122. table.insert(matches, { x_test, i })
  123. end
  124. end
  125. consecutive_y = 1
  126. end
  127. last_gem_y = gem_idx
  128. end
  129. end
  130. if #matches == 0 then
  131. return nil
  132. end
  133. return matches
  134. end
  135. function check_clear_matches()
  136. local matches = find_matches()
  137. if not matches then return end
  138. for i, gem in pairs(matches) do
  139. end
  140. end
  141. function board_draw()
  142. for y = 1, board_h do
  143. for x = 1, board_w do
  144. local sprite = gem_sprites[y][x]
  145. spr(sprite.spr, sprite.x + board_x, sprite.y + board_y, 0, 1, 0, 0, 2, 2)
  146. if math.abs(sprite.dx - sprite.x) < .01 then
  147. sprite.x = sprite.dx
  148. else
  149. sprite.x = sprite.x + (sprite.dx - sprite.x) / 2
  150. end
  151. if math.abs(sprite.dy - sprite.y) < .01 then
  152. sprite.y = sprite.dy
  153. else
  154. sprite.y = sprite.y + (sprite.dy - sprite.y) / 2
  155. end
  156. end
  157. end
  158. end
  159. function player_update()
  160. -- color cycle
  161. player.cycle_t = player.cycle_t + 1
  162. if player.cycle_t % 4 == 0 then player.cycle_clr = player.cycle_clr + 1 end
  163. if player.cycle_clr > #player.cycle_pal then player.cycle_clr = 1 end
  164. if btnp(0) then
  165. if player.y > 1 then
  166. player.y = player.y - 1
  167. end
  168. end
  169. if btnp(1) then
  170. if player.y < board_h then
  171. player.y = player.y + 1
  172. end
  173. end
  174. if btnp(2) then
  175. if player.x > 1 then
  176. player.x = player.x - 1
  177. end
  178. end
  179. if btnp(3) then
  180. if player.x < board_w then
  181. player.x = player.x + 1
  182. end
  183. end
  184. if btnp(4) then
  185. if not player.is_selected then
  186. player.is_selected = true
  187. player.sel_x = player.x
  188. player.sel_y = player.y
  189. else
  190. local x_mv = math.abs(player.x - player.sel_x) == 1
  191. local y_mv = math.abs(player.y - player.sel_y) == 1
  192. -- only move vertically and horizontally
  193. if (x_mv or y_mv) and not (x_mv and y_mv) then
  194. swap_gems(player.x, player.y, player.sel_x, player.sel_y)
  195. end
  196. player.is_selected = false
  197. end
  198. end
  199. end
  200. function player_draw()
  201. rectb((player.x-1)*16 + board_x, (player.y-1)*16 + board_y, 16, 16, player.cycle_pal[player.cycle_clr])
  202. if player.is_selected then
  203. rectb((player.sel_x-1)*16 + board_x, (player.sel_y-1)*16 + board_y, 16, 16, 12)
  204. end
  205. end
  206. function debug_match_draw()
  207. local matches = find_matches()
  208. if not matches then return end
  209. for i,_ in pairs(matches) do
  210. rectb((matches[i][1]-1)*16 + board_x, (matches[i][2]-1)*16 + board_y, 16, 16, 6)
  211. -- prt(matches[i][1] .. ', ' .. matches[i][2], 200, 7*i, 6)
  212. end
  213. end
  214. generate_board()
  215. function TIC()
  216. player_update()
  217. cls()
  218. board_draw()
  219. debug_match_draw()
  220. player_draw()
  221. end
  222. -- <SPRITES>
  223. -- 002:00000000000000ee0000e9ad000ebdbd00ebdbdb009dbccc08dbcccc0edccccc
  224. -- 003:00000000e8000000dae80000bdae8000dcd9e800bdcd9800cbcbe980cccded80
  225. -- 004:00000000000222220023cccc0334444401311121013111120131212201311212
  226. -- 005:0000000022222000cccc32004444411021222120123221102322212032222120
  227. -- 006:00000000000000010000001c000001cb00001cce0001cc3f001cc3ff01cc3fff
  228. -- 007:0000000010000000f1000000ff100000f4f10000eeff1000f3eef100abbeee10
  229. -- 008:0000000000000678000065670007567700766655076665550766555507775555
  230. -- 009:00000000876000007556000077557000556787005557867055c67670ccc57670
  231. -- 010:00000000000000010000011c00011e3c011e2244013224440133244401334444
  232. -- 011:0000000010000000c1100000cc311000cc3331104cc33c1044c34410444c4410
  233. -- 012:00000000000088880008a9ab0089a9bb08aaaabb008888990088889900088889
  234. -- 013:0000000088880000b9fe8000bb9f9800ccb999809baaa80099a9980099888000
  235. -- 014:0000000000000001000000010000001c000000c10000014300000c130000143f
  236. -- 015:0000000000000000100000001000000031000000410000003410000034100000
  237. -- 018:0edccccc08edcdcd008edcd90088ee9d00089ddd0000889b0000008800000000
  238. -- 019:ccdb9c80cdcadb80dcdcd800abcdb800bddb8000cc8800008800000000000000
  239. -- 020:0131212301311232013123220131322201221111002211110000000000000000
  240. -- 021:2222212022222120222221202222222011111210111121000000000000000000
  241. -- 022:01eeeffa001eeeab0001feeb00001ff2000001ff0000001f0000000100000000
  242. -- 023:bbfbdc10bfbdc100fbdc1000bdd10000dd100000d10000001000000000000000
  243. -- 024:076677770777666607776555007775550007565600007c660000077700000000
  244. -- 025:7777887066667670555765705557c700657c700066c700007770000000000000
  245. -- 026:0133444401343333014443330114443300011443000001130000000100000000
  246. -- 027:4442441033323410333224103322e11033e11000311000001000000000000000
  247. -- 028:000088890000888a000008890000088a0000008a000000980000000900000000
  248. -- 029:9989000098880000a890000098800000a9000000a80000008000000000000000
  249. -- 030:000041f300014f2e000412e20014fe2e004fffff01f18e8e0eeeeeee00000000
  250. -- 031:f34100002f310000e29310002e231000ffff2e008e8e8fe0eeeeee1000000000
  251. -- </SPRITES>
  252. -- <PALETTE>
  253. -- 000:1a1c2c5d275db13e55ef7d57ffcd75a7f07038b76425717929366f3b5dc941a6f673eff7f4f4f494b0c2612caace3ca1
  254. -- </PALETTE>