exchange.lua 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. exchange = exchange or {}
  2. exchange.modpath = minetest.get_modpath("easyvend")
  3. -- Note: exchange rate is doubled because only lumps are accepted now, not ingots.
  4. -- (Previously most folks would double their ingots before exchanging them.)
  5. -- Note: must not include infinitely-renewable commodities, like mese.
  6. exchange.types = {
  7. {name = "Gold", key = "gold", rate = 62, image = "default_gold_lump.png", item = "default:gold_lump", block = "default:goldblock"},
  8. {name = "Silver", key = "silver", rate = 25, image = "moreores_silver_lump.png", item = "moreores:silver_lump", block = "moreores:silver_block"},
  9. {name = "Copper", key = "copper", rate = 16, image = "default_copper_lump.png", item = "default:copper_lump", block = "default:copperblock"},
  10. {name = "Iron", key = "iron", rate = 20, image = "default_iron_lump.png", item = "default:iron_lump", block = "default:steelblock"},
  11. {name = "Coal", key = "coal", rate = 10, image = "default_coal_lump.png", item = "default:coal_lump", block = "default:coalblock"},
  12. {name = "Pearl", key = "pearl", rate = 700, image = "starpearl_pearl.png", item = "starpearl:pearl", block = "starpearl:pearl"},
  13. {name = "Rack", key = "rack", rate = 650, image = "moreores_mithril_lump.png", item = "rackstone:bluerack", block = "rackstone:bluerack"},
  14. }
  15. for k, v in ipairs(exchange.types) do
  16. exchange["on_punch_" .. v.key] = function(pos, node, puncher, pt)
  17. local pname = puncher:get_player_name()
  18. local inv = puncher:get_inventory()
  19. local altact = puncher:get_player_control().sneak
  20. local mult = puncher:get_player_control().aux1
  21. local rate = v.rate
  22. local item = ItemStack(v.item)
  23. item:set_count(1)
  24. if mult then
  25. rate = v.rate * 10
  26. item:set_count(10)
  27. end
  28. if altact then
  29. -- Take money, give player ore.
  30. local cash = currency.tell(pname)
  31. if cash >= rate then
  32. local safe = currency.safe_to_remove_cash(inv, "main", rate)
  33. local itemstack = ItemStack(item)
  34. if inv:room_for_item("main", itemstack) and safe then
  35. currency.remove(pname, rate)
  36. local leftover = inv:add_item("main", itemstack)
  37. local rem = currency.tell(pname)
  38. minetest.chat_send_player(pname, "# Server: Commodity exchanged: you receive " .. item:get_count() .. " " .. v.key .. " lump(s) for " .. rate .. " MG. You now have " .. rem .. " MG.")
  39. if leftover:get_count() > 0 then
  40. minetest.item_drop(leftover, puncher, puncher:get_pos())
  41. end
  42. easyvend.sound_vend(pos)
  43. else
  44. minetest.chat_send_player(pname, "# Server: Not enough room in your inventory to receive commodity.")
  45. end
  46. else
  47. minetest.chat_send_player(pname, "# Server: You don't have enough MG to exchange for a commodity.")
  48. end
  49. else
  50. -- Take ore, give player money.
  51. if inv:contains_item("main", item) then
  52. if currency.room(pname, rate) then
  53. currency.add(pname, rate)
  54. inv:remove_item("main", item)
  55. local cash = currency.tell(pname)
  56. minetest.chat_send_player(pname, "# Server: Commodity exchanged: you receive " .. rate .. " minegeld. You now have " .. cash .. " MG.")
  57. easyvend.sound_deposit(pos)
  58. else
  59. local cash = currency.tell(pname)
  60. minetest.chat_send_player(pname, "# Server: Not enough room in your inventory. You currently have " .. cash .. " MG.")
  61. end
  62. else
  63. local cash = currency.tell(pname)
  64. minetest.chat_send_player(pname, "# Server: You don't have enough unrefined " .. v.key .. ". You currently have " .. cash .. " MG.")
  65. end
  66. end
  67. end
  68. exchange["on_construct_" .. v.key] = function(pos, node, puncher, pt)
  69. local meta = minetest.get_meta(pos)
  70. meta:set_string("infotext",
  71. v.name .. " Commodity Exchange\nPunch to exchange " .. v.key ..
  72. " for MG\nHold shift to get " .. v.key .. " (paying MG)\nRate: 1 unrefined " .. v.key .. " = " .. v.rate .. " MG" ..
  73. "\nHold 'E' to multiply x10")
  74. end
  75. end
  76. if not exchange.run_once then
  77. minetest.register_alias("exchange:kiosk_mese", "exchange:kiosk_gold")
  78. for k, v in ipairs(exchange.types) do
  79. minetest.register_node(":exchange:kiosk_" .. v.key, {
  80. description = v.name .. " Commodity Exchange\n\nPunch to obtain MG in exchange for commodities.\nShift-punch to redeem commodities for money.\nHold 'E' to multiply exchange by 10.",
  81. tiles = {
  82. "easyvend_vendor_side.png^" .. v.image,
  83. "easyvend_vendor_side.png",
  84. "easyvend_ad_booth.png",
  85. "easyvend_ad_booth.png",
  86. "easyvend_ad_booth.png",
  87. "easyvend_ad_booth.png",
  88. },
  89. paramtype2 = "facedir",
  90. groups = utility.dig_groups("furniture", {flammable = 2}),
  91. sounds = default.node_sound_wood_defaults(),
  92. on_construct = function(pos, node, puncher, pt)
  93. (exchange["on_construct_" .. v.key])(pos, node, puncher, pt)
  94. end,
  95. on_punch = function(pos, node, puncher, pt)
  96. (exchange["on_punch_" .. v.key])(pos, node, puncher, pt)
  97. end,
  98. })
  99. end
  100. for k, v in ipairs(exchange.types) do
  101. minetest.register_craft({
  102. output = "exchange:kiosk_" .. v.key,
  103. recipe = {
  104. {'', 'passport:passport_adv', ''},
  105. {"market:booth", "voidchest:voidchest_closed", v.block},
  106. {'', v.block, ''},
  107. },
  108. })
  109. end
  110. local c = "exchange:core"
  111. local f = exchange.modpath .. "/exchange.lua"
  112. reload.register_file(c, f, false)
  113. exchange.run_once = true
  114. end