You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

convertval.lua 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. --
  2. -- money3
  3. --
  4. -- Copyright © 2012 Bad_Command
  5. -- Copyright © 2012 kotolegokot
  6. -- Copyright © 2019 by luk3yx
  7. --
  8. -- This library is free software; you can redistribute it and/or
  9. -- modify it under the terms of the GNU Lesser General Public
  10. -- License as published by the Free Software Foundation; either
  11. -- version 2.1 of the License, or (at your option) any later version.
  12. --
  13. -- This program is distributed in the hope that it will be useful,
  14. -- but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. -- GNU General Public License for more details.
  17. --
  18. -- You should have received a copy of the GNU Lesser General Public
  19. -- License along with this library; if not, write to the Free Software
  20. -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. --
  22. local storage = assert(...)
  23. local stat_file = minetest.get_worldpath() .. "/convert_stats"
  24. minetest.register_chatcommand("convertval", {
  25. privs = {money = true},
  26. params = "",
  27. description = "Shows the current value of convertable items",
  28. func = function(name, param)
  29. minetest.chat_send_player(name, "Convertable item values:")
  30. for k, v in pairs(money3.stats) do
  31. minetest.chat_send_player(name, money3.convert_items[k].desc ..
  32. ": "..money3.format(money3.stats[k].running_value) .. " each.")
  33. end
  34. return true, "Done!"
  35. end
  36. })
  37. function money3.dignode(pos, node, player)
  38. for k,v in pairs(money3.convert_items) do
  39. if ( node.name == money3.convert_items[k].dig_block ) then
  40. money3.stats[k].running_dug = money3.stats[k].running_dug + 1
  41. end
  42. end
  43. end
  44. function money3.calcConvertValues()
  45. core.log('warn',
  46. '[money3] Another mod tried to access money3.calcConvertValues.')
  47. end
  48. local function calcConvertValues()
  49. for k, stats in pairs(money3.stats) do
  50. stats.running_time = stats.running_time + 1
  51. stats.running_value = math.floor(
  52. money3.convert_items[k].amount
  53. + ((stats.running_time / 960)
  54. + (money3.stats[k].running_dug / 22)
  55. - (money3.stats[k].running_converted / 11))
  56. )
  57. if ( money3.stats[k].running_value < money3.convert_items[k].minval ) then
  58. money3.stats[k].running_val = money3.convert_items[k].minval
  59. end
  60. minetest.log("verbose", "Calculated " .. money3.convert_items[k].desc ..
  61. " value at "..tostring(money3.stats[k].running_value))
  62. end
  63. money3.save_stats()
  64. minetest.after(60, calcConvertValues)
  65. end
  66. function money3.save_stats()
  67. minetest.log("verbose", "Saving convert stats")
  68. local f = io.open(stat_file, "w")
  69. f:write(minetest.serialize(money3.stats))
  70. f:close()
  71. end
  72. local function load_old_stats()
  73. local f = io.open(stat_file, "r")
  74. if not f then return false end
  75. minetest.log("verbose", "[money3] Migrating legacy stats file.")
  76. local t = f:read("*all")
  77. f:close()
  78. os.remove(stat_file)
  79. if not t or t == "" then return false end
  80. storage:set_string("stats2", t)
  81. return minetest.deserialize(t)
  82. end
  83. function money3.load_stats()
  84. minetest.log("verbose", "[money3] Loading convert stats.")
  85. local t = storage:get_string("stats2")
  86. if not t or t == "" then return load_old_stats() end
  87. return minetest.deserialize(t)
  88. end
  89. money3.stats = money3.load_stats()
  90. if not money3.stats then
  91. money3.stats = {}
  92. for key, val in pairs(money3.convert_items) do
  93. minetest.log("action", "[money3] Initial Convert Stats Setup for " ..
  94. money3.convert_items[key].desc)
  95. money3.stats[key] = {
  96. running_time = 0,
  97. running_dug = 0,
  98. running_converted = 0,
  99. running_value = money3.convert_items[key].amount
  100. }
  101. end
  102. end
  103. minetest.register_on_dignode(money3.dignode)
  104. minetest.after(5, calcConvertValues)
  105. local convert_options = {}
  106. for k,v in pairs(money3.convert_items) do
  107. table.insert(convert_options, k)
  108. end
  109. minetest.register_chatcommand("convert", {
  110. privs = {money = true},
  111. params = "<" .. table.concat(convert_options, ", ") .. ">",
  112. description = "Converts certain ores to credits",
  113. func = function(name, param)
  114. -- check the parameters
  115. param = param:lower()
  116. if not money3.convert_items[param] then
  117. return false, "Invalid item!"
  118. end
  119. local item = money3.convert_items[param].item
  120. local amount = money3.stats[param].running_value
  121. local totalAmount = 0
  122. local totalItems = 0
  123. -- Look through their inventory for the item they chose.
  124. local player = minetest.get_player_by_name(name)
  125. local inventory = player:get_inventory()
  126. for i=1,inventory:get_size("main") do
  127. local inv_item = inventory:get_stack("main",i)
  128. if ( inv_item:get_name() == item ) then
  129. local add_amount = inv_item:get_count() * amount
  130. money3.add(name,add_amount)
  131. totalAmount = totalAmount + add_amount
  132. totalItems = totalItems + inv_item:get_count()
  133. inventory:set_stack("main",i,nil)
  134. end
  135. end
  136. money3.stats[param].running_converted =
  137. money3.stats[param].running_converted + totalItems
  138. return true, "You converted " .. tostring(totalItems) .. " " ..
  139. param .. " into "..tostring(totalAmount)..money3.currency_name
  140. end
  141. })
  142. convert_options = nil