diamondmine.lua 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. board = {}
  25. function generate_gem(exclude_idx)
  26. -- exclude_idx = exclude_idx or 0
  27. local out_gem = math.random(1, #gems)
  28. if exclude_idx == out_gem then
  29. if out_gem + 1 > #gems then out_gem = 1 else outgem = out_gem + 1 end
  30. end
  31. return out_gem
  32. end
  33. -- build the global board table from scratch
  34. function generate_board(allow_matches)
  35. allow_matches = allow_matches or 1
  36. for y = 1, board_h do
  37. board[y] = {}
  38. for x = 1, board_w do
  39. -- TODO: use allow_matches
  40. local value = generate_gem()
  41. if not allow_matches then
  42. end
  43. board[y][x] = value
  44. end
  45. end
  46. end
  47. function swap_gems(src_x, src_y, dest_x, dest_y)
  48. local src_gem = board[src_y][src_x]
  49. local dest_gem = board[dest_y][dest_x]
  50. board[src_y][src_x] = dest_gem
  51. board[dest_y][dest_x] = src_gem
  52. end
  53. -- return: table of x,y pair tables, or nil
  54. function find_matches()
  55. local matches = {}
  56. -- brute force horizontal
  57. for y_test = 1, board_h do
  58. -- seed first gem
  59. local last_gem = board[y_test][1]
  60. local consecutive_gems = 1
  61. for x_test = 1, board_w do
  62. local gem_idx = board[y_test][x_test]
  63. if last_gem == gem_idx then
  64. consecutive_gems = consecutive_gems + 1
  65. else
  66. if consecutive_gems >= 3 then
  67. for i = x_test - consecutive_gems, x_test do
  68. table.insert(matches, { x_test-1, y_test-1 })
  69. end
  70. end
  71. consecutive_gems = 1
  72. end
  73. last_gem = gem_idx
  74. print(consecutive_gems, (x_test-1) * 16, (y_test-1) * 16)
  75. end
  76. end
  77. -- print(#matches)
  78. if #matches == 0 then
  79. return nil
  80. end
  81. return matches
  82. end
  83. function draw_gem(piece_type, xpos, ypos)
  84. spr(gems[piece_type].spr, xpos*16, ypos*16, 0, 1, 0, 0, 2, 2)
  85. end
  86. function board_draw()
  87. for y = 1, board_h do
  88. for x = 1, board_w do
  89. draw_gem(board[y][x], x - 1, y - 1)
  90. end
  91. end
  92. end
  93. function player_update()
  94. -- color cycle
  95. player.cycle_t = player.cycle_t + 1
  96. if player.cycle_t % 4 == 0 then player.cycle_clr = player.cycle_clr + 1 end
  97. if player.cycle_clr > #player.cycle_pal then player.cycle_clr = 1 end
  98. if btnp(0) then
  99. if player.y > 1 then
  100. player.y = player.y - 1
  101. end
  102. end
  103. if btnp(1) then
  104. if player.y < board_h then
  105. player.y = player.y + 1
  106. end
  107. end
  108. if btnp(2) then
  109. if player.x > 1 then
  110. player.x = player.x - 1
  111. end
  112. end
  113. if btnp(3) then
  114. if player.x < board_w then
  115. player.x = player.x + 1
  116. end
  117. end
  118. if btnp(4) then
  119. if not player.is_selected then
  120. player.is_selected = true
  121. player.sel_x = player.x
  122. player.sel_y = player.y
  123. else
  124. local x_mv = math.abs(player.x - player.sel_x) == 1
  125. local y_mv = math.abs(player.y - player.sel_y) == 1
  126. -- only move vertically and horizontally
  127. if (x_mv or y_mv) and not (x_mv and y_mv) then
  128. swap_gems(player.x, player.y, player.sel_x, player.sel_y)
  129. end
  130. player.is_selected = false
  131. end
  132. end
  133. end
  134. function player_draw()
  135. rectb((player.x-1)*16, (player.y-1)*16, 16, 16, player.cycle_pal[player.cycle_clr])
  136. if player.is_selected then
  137. rectb((player.sel_x-1)*16, (player.sel_y-1)*16, 16, 16, 12)
  138. end
  139. end
  140. function debug_match_draw()
  141. local matches = find_matches()
  142. if not matches then return end
  143. for i = 1, #matches do
  144. rectb((matches[i][1]-1)*16, (matches[i][2]-1)*16, 16, 16, 6)
  145. end
  146. end
  147. generate_board()
  148. function TIC()
  149. player_update()
  150. cls()
  151. board_draw()
  152. player_draw()
  153. debug_match_draw()
  154. end
  155. -- <SPRITES>
  156. -- 002:00000000000000ee0000e9ad000ebdbd00ebdbdb009dbccc08dbcccc0edccccc
  157. -- 003:00000000e8000000dae80000bdae8000dcd9e800bdcd9800cbcbe980cccded80
  158. -- 004:00000000000222220023cccc0334444401311121013111120131212201311212
  159. -- 005:0000000022222000cccc32004444411021222120123221102322212032222120
  160. -- 006:00000000000000010000001c000001cb00001cce0001cc3f001cc3ff01cc3fff
  161. -- 007:0000000010000000f1000000ff100000f4f10000eeff1000f3eef100abbeee10
  162. -- 008:0000000000000678000065670007567700766655076665550766555507775555
  163. -- 009:00000000876000007556000077557000556787005557867055c67670ccc57670
  164. -- 010:00000000000000010000011c00011e3c011e2244013224440133244401334444
  165. -- 011:0000000010000000c1100000cc311000cc3331104cc33c1044c34410444c4410
  166. -- 012:00000000000088880008a9ab0089a9bb08aaaabb008888990088889900088889
  167. -- 013:0000000088880000b9fe8000bb9f9800ccb999809baaa80099a9980099888000
  168. -- 014:0000000000000001000000010000001c000000c10000014300000c130000143f
  169. -- 015:0000000000000000100000001000000031000000410000003410000034100000
  170. -- 018:0edccccc08edcdcd008edcd90088ee9d00089ddd0000889b0000008800000000
  171. -- 019:ccdb9c80cdcadb80dcdcd800abcdb800bddb8000cc8800008800000000000000
  172. -- 020:0131212301311232013123220131322201221111002211110000000000000000
  173. -- 021:2222212022222120222221202222222011111210111121000000000000000000
  174. -- 022:01eeeffa001eeeab0001feeb00001ff2000001ff0000001f0000000100000000
  175. -- 023:bbfbdc10bfbdc100fbdc1000bdd10000dd100000d10000001000000000000000
  176. -- 024:076677770777666607776555007775550007565600007c660000077700000000
  177. -- 025:7777887066667670555765705557c700657c700066c700007770000000000000
  178. -- 026:0133444401343333014443330114443300011443000001130000000100000000
  179. -- 027:4442441033323410333224103322e11033e11000311000001000000000000000
  180. -- 028:000088890000888a000008890000088a0000008a000000980000000900000000
  181. -- 029:9989000098880000a890000098800000a9000000a80000008000000000000000
  182. -- 030:000041f300014f2e000412e20014fe2e004fffff01f18e8e0eeeeeee00000000
  183. -- 031:f34100002f310000e29310002e231000ffff2e008e8e8fe0eeeeee1000000000
  184. -- </SPRITES>
  185. -- <PALETTE>
  186. -- 000:1a1c2c5d275db13e55ef7d57ffcd75a7f07038b76425717929366f3b5dc941a6f673eff7f4f4f494b0c2612caace3ca1
  187. -- </PALETTE>