shop.lua 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. local function lobby_shop_priv(player)
  2. local player_attributes = player:get_meta()
  3. local xp = player_attributes:get_float('xp')
  4. local luck = player_attributes:get_int('luck')
  5. local health = player_attributes:get_int('hp')
  6. local formspec =
  7. 'formspec_version[3]'..
  8. 'size[12,12]'..
  9. 'bgcolor[;neither]'..
  10. 'background[-1,-1;14,14;lobby_shop.png]'..
  11. 'textarea[.5,.5;11,2;;; Spend your XP here! If you run low collect trash from the lobby, or play some levels with friends. '..
  12. 'Playing levels will revoke some privileges, but you can restore them here. If you want to build your own levels you\'ll need the builder priv. '..
  13. 'This will give you: fly, fast, and creative\n Luck will help you find stashed items. Higher HP will let you take more damage before dying.]'..
  14. 'textarea[1,3.75;5,1.5;;;Your Current Stats:\n XP: '..xp..'\n Max HP: '..health..'\n Luck: '..luck..']'..
  15. 'button[1,7;5,1;builder;Builder Priv (100XP)]'..
  16. 'button[6,7;5,1;toggle_build;Toggle builder privileges]'..
  17. 'button[6,8.5;5,1;health;Increase your HP ('..((health+1)*10)..' XP)]'
  18. if luck < 10 then
  19. formspec = formspec..
  20. 'button[1,8.5;5,1;luck;Increase your luck ('..((luck+1)*100)..' XP)]'
  21. else
  22. formspec = formspec..
  23. 'button[1,8.5;5,1;nil;Max luck ]'
  24. end
  25. return formspec
  26. end
  27. local col_box = {
  28. type = 'fixed',
  29. fixed = {{-.5, -.5, -.45, 1.5, .375, 0},
  30. {-.45, 1.5, -.2, 1.45, 2, -.1},}--Left, Bottom, Front, Right, Top, Back
  31. }
  32. minetest.register_node('lobby:shop_priv', {
  33. description = 'A place to buy privs',
  34. drawtype = 'mesh',
  35. mesh = 'lobby_shop_privs.obj',
  36. tiles = {'lobby_shop_privs.png'},
  37. groups = {breakable=1, not_in_creative_inventory=1},
  38. paramtype = 'light',
  39. paramtype2 = 'facedir',
  40. selection_box = col_box,
  41. collision_box = col_box,
  42. on_rightclick = function(pos, node, clicker, itemstack)
  43. local meta = minetest.get_meta(pos)
  44. local name = clicker:get_player_name()
  45. meta:set_string('infotext', 'Shop, spend XP here.')
  46. minetest.show_formspec(name, 'lobby:shop_priv', lobby_shop_priv(clicker))
  47. end,
  48. })
  49. minetest.register_on_player_receive_fields(function(player, formname, fields)
  50. local name = player:get_player_name()
  51. if formname == 'lobby:shop_priv' then
  52. local player_attributes = player:get_meta()
  53. if fields.builder then
  54. if lobby.take_xp(player, 100) and not lobby.is_builder(player) then
  55. minetest.chat_send_player(player:get_player_name(), 'You just bought the Builder Privilege!')
  56. local privs = minetest.get_player_privs(name)
  57. privs.builder = true
  58. privs.creative = true
  59. privs.fly = true
  60. privs.fast = true
  61. minetest.set_player_privs(name, privs)
  62. minetest.show_formspec(name, 'lobby:shop_priv', lobby_shop_priv(player))
  63. player_attributes:set_string('mode', 'builder')
  64. else
  65. minetest.chat_send_player(name, 'You either don\'t have 100 XP or you already have the priv.')
  66. end
  67. minetest.show_formspec(name, 'lobby:shop_priv', lobby_shop_priv(player))
  68. elseif fields.toggle_build then
  69. if lobby.is_builder(player) then
  70. local privs = minetest.get_player_privs(name)
  71. if privs.creative == true then
  72. lobby.builder_to_player(player)
  73. minetest.chat_send_player(name, 'Privs revoked! Play some games in the lobby')
  74. else
  75. lobby.player_to_builder(player)
  76. minetest.chat_send_player(name, 'Privs restored!')
  77. end
  78. else
  79. minetest.chat_send_player(name, 'You need to buy the Builder priv first!')
  80. end
  81. minetest.show_formspec(name, 'lobby:shop_priv', lobby_shop_priv(player))
  82. elseif fields.luck then
  83. local luck = player_attributes:get_int('luck') or 0
  84. local xp = (luck+1)*100
  85. if lobby.take_xp(player, xp) then
  86. player_attributes:set_int('luck', luck+1)
  87. else
  88. minetest.chat_send_player(name, 'You need more XP!')
  89. end
  90. minetest.show_formspec(name, 'lobby:shop_priv', lobby_shop_priv(player))
  91. elseif fields.health then
  92. local health = player_attributes:get_int('hp')
  93. local xp = (health+1)*10
  94. if lobby.take_xp(player, xp) then
  95. player:set_properties({hp_max = health+1})
  96. player_attributes:set_int('hp', health+1)
  97. else
  98. minetest.chat_send_player(name, 'You need more XP!')
  99. end
  100. minetest.show_formspec(name, 'lobby:shop_priv', lobby_shop_priv(player))
  101. end
  102. end
  103. end)