match.lua 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. for i = 1,8 do
  2. minetest.register_craftitem('tasks:card_'..i, {
  3. description = 'Playing Card',
  4. inventory_image = 'tasks_playing_card.png',
  5. stack_max = 1,
  6. groups = {not_in_creative_inventory=1}
  7. })
  8. end
  9. local function match_formspec(pname, tries, card1, card2)
  10. local val1 = string.sub(card1, -1)
  11. local val2 = string.sub(card2, -1)
  12. local formspec =
  13. 'formspec_version[3]'..
  14. 'size[8,10]'..
  15. 'bgcolor[;neither]'..
  16. 'background[0,0;8,10;tasks_card_table_bg.png]'..
  17. 'hypertext[0,0;8,1;nil;<global halign=center valign=middle><style color=white size=20>Pick two matching cards.\n('..tries..' tries remaining.)<style>]'..
  18. 'hypertext[1,8.5;1,1;nil;<global halign=center valign=middle><style color=white size=35>'..val1..'<style>]'..
  19. 'hypertext[6,8.5;1,1;nil;<global halign=center valign=middle><style color=white size=35>'..val2..'<style>]'..
  20. 'style_type[list;size=1.5,1.5]'..
  21. 'listcolors[#10101020;#257C4020]'..
  22. 'list[detached:match_card_grid_'..pname..';cards;.625,1.25;4,4;]'..
  23. 'list[detached:match_card_slot_'..pname..';slots;2.375,8.25;2,1;]'
  24. return formspec
  25. end
  26. local deck = {'tasks:card_1', 'tasks:card_2', 'tasks:card_3', 'tasks:card_4', 'tasks:card_5', 'tasks:card_6', 'tasks:card_7', 'tasks:card_8',
  27. 'tasks:card_1', 'tasks:card_2', 'tasks:card_3', 'tasks:card_4', 'tasks:card_5', 'tasks:card_6', 'tasks:card_7', 'tasks:card_8'}
  28. local function shuffle_deck(table)
  29. for i = #table, 2 , -1 do
  30. local j = math.random(i)
  31. table[i], table[j] = table[j], table[i]
  32. end
  33. return table
  34. end
  35. local function new_game(pos, player)
  36. local player_attributes = player:get_meta()
  37. local luck = player_attributes:get_int('luck')
  38. local tries = (luck) + 6
  39. local pname = player:get_player_name()
  40. local meta = minetest.get_meta(pos)
  41. local xp = meta:get_int('xp')
  42. local grid_inv = minetest.create_detached_inventory('match_card_grid_'..pname, {})
  43. grid_inv:set_size('cards', 16)
  44. grid_inv:set_list('cards', shuffle_deck(deck))
  45. local slot_inv = minetest.create_detached_inventory('match_card_slot_'..pname, {
  46. on_put = function(inv, listname, index, stack, player)
  47. tries = tries - 1
  48. if tries == 0 then
  49. new_game(pos, player)
  50. else
  51. local inv = minetest.get_inventory({type = 'detached', name = 'match_card_slot_'..pname})
  52. local slot1 = inv:get_stack('slots', 1)
  53. local slot2 = inv:get_stack('slots', 2)
  54. local card1 = slot1:get_name()
  55. local card2 = slot2:get_name()
  56. minetest.show_formspec(pname, 'tasks:card_matching', match_formspec(pname, tries, card1, card2))
  57. if card1 == card2 then
  58. minetest.chat_send_player(pname, 'Nice work')
  59. tasks.only_add_xp(xp, pname)
  60. new_game(pos, player)
  61. end
  62. end
  63. end,
  64. })
  65. slot_inv:set_size('slots', 2)
  66. minetest.show_formspec(pname, 'tasks:card_matching', match_formspec(pname, tries, '?', '?'))
  67. end
  68. minetest.register_node('tasks:match', {
  69. description = 'Matching Game',
  70. drawtype = 'mesh',
  71. mesh = 'tasks_card_table.obj',
  72. tiles = {'tasks_card_table.png'},
  73. paramtype2 = 'facedir',
  74. groups = {breakable=1, tasks=1},
  75. on_construct = function(pos)
  76. local meta = minetest.get_meta(pos)
  77. meta:set_string('infotext', 'Matching')
  78. meta:set_int('xp', 5)
  79. end,
  80. on_rightclick = function(pos, node, clicker)
  81. new_game(pos, clicker)
  82. end
  83. })