anvils.lua 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. local MAX_NAME_LENGTH = 30
  2. local MAX_WEAR = 65535
  3. local SAME_TOOL_REPAIR_BOOST = math.ceil(MAX_WEAR * 0.12) -- 12%
  4. --[[local MATERIAL_TOOL_REPAIR_BOOST = {
  5. math.ceil(MAX_WEAR * 0.34), -- 25%
  6. math.ceil(MAX_WEAR * 0.68), -- 50%
  7. math.ceil(MAX_WEAR * 1), -- 75%
  8. MAX_WEAR, -- 100%
  9. }]]
  10. local MATERIAL_TOOL_REPAIR_BOOST = {
  11. math.ceil(MAX_WEAR * 0.68), -- 50%
  12. math.ceil(MAX_WEAR * 1), -- 75%
  13. MAX_WEAR, -- 100%
  14. }
  15. local NAME_COLOR = "#FFFF4C"
  16. local function get_anvil_formspec(set_name)
  17. if not set_name then
  18. set_name = ""
  19. end
  20. return "size[8,8.5]"..
  21. "background[1,1;1,1;gui_formbg2.png;true]"..
  22. "bgcolor[#080808BB;true]"..
  23. "list[current_player;main;0,4.5;8,4]"..
  24. "list[context;input;1,2.5;1,1;]"..
  25. "list[context;input;3.5,2.5;1,1;1]"..
  26. "list[context;output;6,2.5;1,1;]"..
  27. "image[2.35,2.6;0.8,0.8;plus.png]" ..
  28. "image[4.75,2.5;1,1;arrow0.png]" ..
  29. "image[2.6,0.65;3,1;rep.png]" ..
  30. "image[5.3,0.5;1.3,1.3;ham.png]" ..
  31. "image[1.3,0.5;1.3,1.3;pick.png]" ..
  32. "bgcolor[#080808BB;true]" ..
  33. default.gui_slots..
  34. "field_close_on_enter[name;false]"..
  35. "listring[context;input]"..
  36. "listring[context;output]"..
  37. "listring[current_player;main]"..
  38. "listring[current_player;main]"
  39. end
  40. -- Given a tool and material stack, returns how many items of the material stack
  41. -- needs to be used up to repair the tool.
  42. local function get_consumed_materials(tool, material)
  43. local wear = tool:get_wear()
  44. if wear == 0 then
  45. return 0
  46. end
  47. local health = (MAX_WEAR - wear)
  48. local matsize = material:get_count()
  49. local materials_used = 0
  50. for m=1, math.min(3, matsize) do
  51. materials_used = materials_used + 1
  52. if (wear - MATERIAL_TOOL_REPAIR_BOOST[m]) <= 0 then
  53. break
  54. end
  55. end
  56. return materials_used
  57. end
  58. -- Given 2 input stacks, tells you which is the tool and which is the material.
  59. -- Returns ("tool", input1, input2) if input1 is tool and input2 is material.
  60. -- Returns ("material", input2, input1) if input1 is material and input2 is tool.
  61. -- Returns nil otherwise.
  62. local function distinguish_tool_and_material(input1, input2)
  63. local def1 = input1:get_definition()
  64. local def2 = input2:get_definition()
  65. if def1.type == "tool" and def1._repair_material then
  66. return "tool", input1, input2
  67. elseif def2.type == "tool" and def2._repair_material then
  68. return "material", input2, input1
  69. else
  70. return nil
  71. end
  72. end
  73. -- Update the inventory slots of an anvil node.
  74. -- meta: Metadata of anvil node
  75. local function update_anvil_slots(meta)
  76. local inv = meta:get_inventory()
  77. local new_name = meta:get_string("set_name")
  78. local input1, input2, output
  79. input1 = inv:get_stack("input", 1)
  80. input2 = inv:get_stack("input", 2)
  81. output = inv:get_stack("output", 1)
  82. local new_output, name_item
  83. local just_rename = false
  84. -- Both input slots occupied
  85. if (not input1:is_empty() and not input2:is_empty()) then
  86. -- Repair, if tool
  87. local def1 = input1:get_definition()
  88. local def2 = input2:get_definition()
  89. -- Repair calculation helper.
  90. -- Adds the “inverse” values of wear1 and wear2.
  91. -- Then adds a boost health value directly.
  92. -- Returns the resulting (capped) wear.
  93. local function calculate_repair(wear1, wear2, boost)
  94. local new_health = (MAX_WEAR - wear1) + (MAX_WEAR - wear2)
  95. if boost then
  96. new_health = new_health + boost
  97. end
  98. return math.max(0, math.min(MAX_WEAR, MAX_WEAR - new_health))
  99. end
  100. -- Same tool twice
  101. if input1:get_name() == input2:get_name() and def1.type == "tool" and (input1:get_wear() > 0 or input2:get_wear() > 0) then
  102. -- Add tool health together plus a small bonus
  103. -- TODO: Combine tool enchantments
  104. local new_wear = calculate_repair(input1:get_wear(), input2:get_wear(), SAME_TOOL_REPAIR_BOOST)
  105. input1:set_wear(new_wear)
  106. name_item = input1
  107. new_output = name_item
  108. -- Tool + repair item
  109. else
  110. -- Any tool can have a repair item. This may be defined in the tool's item definition
  111. -- as an itemstring in the field `_repair_material`. Only if this field is set, the
  112. -- tool can be repaired with a material item.
  113. -- Example: Steel Pickaxe + Steel Ingot. `_repair_material = default:steel_ingot`
  114. -- Big repair bonus
  115. -- TODO: Combine tool enchantments
  116. local distinguished, tool, material = distinguish_tool_and_material(input1, input2)
  117. if distinguished then
  118. local tooldef = tool:get_definition()
  119. local has_correct_material = false
  120. if string.sub(tooldef._repair_material, 1, 6) == "group:" then
  121. has_correct_material = minetest.get_item_group(material:get_name(), string.sub(tooldef._repair_material, 7)) ~= 0
  122. elseif material:get_name() == tooldef._repair_material then
  123. has_correct_material = true
  124. end
  125. if has_correct_material and tool:get_wear() > 0 then
  126. local materials_used = get_consumed_materials(tool, material)
  127. local new_wear = calculate_repair(tool:get_wear(), MAX_WEAR, MATERIAL_TOOL_REPAIR_BOOST[materials_used])
  128. tool:set_wear(new_wear)
  129. name_item = tool
  130. new_output = name_item
  131. else
  132. new_output = ""
  133. end
  134. else
  135. new_output = ""
  136. end
  137. end
  138. -- Exactly 1 input slot occupied
  139. elseif (not input1:is_empty() and input2:is_empty()) or (input1:is_empty() and not input2:is_empty()) then
  140. -- Just rename item
  141. if input1:is_empty() then
  142. name_item = input2
  143. else
  144. name_item = input1
  145. end
  146. just_rename = true
  147. else
  148. new_output = ""
  149. end
  150. -- Rename handling
  151. if name_item then
  152. -- No renaming allowed with group no_rename=1
  153. if minetest.get_item_group(name_item:get_name(), "no_rename") == 1 then
  154. new_output = ""
  155. else
  156. if new_name == nil then
  157. new_name = ""
  158. end
  159. local meta = name_item:get_meta()
  160. local old_name = meta:get_string("name")
  161. -- Limit name length
  162. new_name = string.sub(new_name, 1, MAX_NAME_LENGTH)
  163. -- Don't rename if names are identical
  164. if new_name ~= old_name then
  165. -- Rename item
  166. if new_name == "" then
  167. -- Empty name
  168. if name_item:get_definition()._generate_description then
  169. -- _generate_description(itemstack): If defined, set custom item description of itemstack.
  170. name_item:get_definition()._generate_description(name_item)
  171. else
  172. -- Otherwise, just clear description
  173. meta:set_string("description", "")
  174. end
  175. else
  176. -- Custom name set. Colorize it!
  177. -- This makes the name visually different from unnamed items
  178. meta:set_string("description", core.colorize(NAME_COLOR, new_name))
  179. end
  180. -- Save the raw name internally, too
  181. meta:set_string("name", new_name)
  182. new_output = name_item
  183. elseif just_rename then
  184. new_output = ""
  185. end
  186. end
  187. end
  188. -- Set the new output slot
  189. if new_output ~= nil then
  190. inv:set_stack("output", 1, new_output)
  191. end
  192. end
  193. -- Drop input items of anvil at pos with metadata meta
  194. local function drop_anvil_items(pos, meta)
  195. local inv = meta:get_inventory()
  196. for i=1, inv:get_size("input") do
  197. local stack = inv:get_stack("input", i)
  198. if not stack:is_empty() then
  199. local p = {x=pos.x+math.random(0, 10)/10-0.5, y=pos.y, z=pos.z+math.random(0, 10)/10-0.5}
  200. minetest.add_item(p, stack)
  201. end
  202. end
  203. end
  204. local anvildef = {
  205. groups = {cracky = 1},
  206. tiles = {'stations_anvil.png'},
  207. use_texture_alpha = 'opaque',
  208. paramtype = "light",
  209. sunlight_propagates = true,
  210. is_ground_content = false,
  211. paramtype2 = "facedir",
  212. drawtype = "mesh",
  213. mesh = 'anvils_anvil.obj',
  214. sounds = default.node_sound_metal_defaults(),
  215. after_place_node = function(pos, placer)
  216. local name = placer:get_player_name()
  217. local pinv = placer:get_inventory()
  218. local meta = minetest.get_meta(pos);
  219. local owner = placer:get_player_name() or "";
  220. local privs = minetest.get_player_privs(owner);
  221. meta:set_string("owner",owner);
  222. meta:set_string("infotext","Anvil (owned by " .. owner .. "): ");
  223. end,
  224. after_dig_node = function(pos, oldnode, oldmetadata, digger)
  225. local meta = minetest.get_meta(pos)
  226. local meta2 = meta
  227. meta:from_table(oldmetadata)
  228. drop_anvil_items(pos, meta)
  229. meta:from_table(meta2:to_table())
  230. end,
  231. allow_metadata_inventory_put = function(pos, listname, index, stack, player)
  232. local meta = minetest.get_meta(pos);
  233. local privs = minetest.get_player_privs(player:get_player_name());
  234. if meta:get_string("owner")~=player:get_player_name() and not privs.privs then return 0 end
  235. if listname == "output" then
  236. return 0
  237. else
  238. return stack:get_count()
  239. end
  240. end,
  241. allow_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player)
  242. local meta = minetest.get_meta(pos);
  243. local privs = minetest.get_player_privs(player:get_player_name());
  244. if meta:get_string("owner")~=player:get_player_name() and not privs.privs then return 0 end
  245. if to_list == "output" then
  246. return 0
  247. elseif from_list == "output" and to_list == "input" then
  248. local meta = minetest.get_meta(pos)
  249. local inv = meta:get_inventory()
  250. if inv:get_stack(to_list, to_index):is_empty() then
  251. return count
  252. else
  253. return 0
  254. end
  255. else
  256. return count
  257. end
  258. end,
  259. on_metadata_inventory_put = function(pos, listname, index, stack, player)
  260. local meta = minetest.get_meta(pos)
  261. update_anvil_slots(meta)
  262. end,
  263. on_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player)
  264. local meta = minetest.get_meta(pos)
  265. if from_list == "output" and to_list == "input" then
  266. local inv = meta:get_inventory()
  267. for i=1, inv:get_size("input") do
  268. if i ~= to_index then
  269. local istack = inv:get_stack("input", i)
  270. istack:set_count(math.max(0, istack:get_count() - count))
  271. inv:set_stack("input", i, istack)
  272. end
  273. end
  274. end
  275. update_anvil_slots(meta)
  276. end,
  277. allow_metadata_inventory_take = function(pos, listname, index, stack, player)
  278. local meta = minetest.get_meta(pos);
  279. local privs = minetest.get_player_privs(player:get_player_name());
  280. if meta:get_string("owner")~=player:get_player_name() and not privs.privs then return 0 end
  281. return stack:get_count();
  282. end,
  283. on_metadata_inventory_take = function(pos, listname, index, stack, player)
  284. local meta = minetest.get_meta(pos)
  285. if listname == "output" then
  286. local inv = meta:get_inventory()
  287. local input1 = inv:get_stack("input", 1)
  288. local input2 = inv:get_stack("input", 2)
  289. -- Both slots occupied?
  290. if not input1:is_empty() and not input2:is_empty() then
  291. -- Take as many items as needed
  292. local distinguished, tool, material = distinguish_tool_and_material(input1, input2)
  293. if distinguished then
  294. -- Tool + material: Take tool and as many materials as needed
  295. local materials_used = get_consumed_materials(tool, material)
  296. material:set_count(material:get_count() - materials_used)
  297. tool:take_item()
  298. local player_name = player:get_player_name()
  299. minetest.sound_play("anvil_use", {to_player=player_name, gain = 1.0})
  300. if distinguished == "tool" then
  301. input1, input2 = tool, material
  302. else
  303. input1, input2 = material, tool
  304. end
  305. inv:set_stack("input", 1, input1)
  306. inv:set_stack("input", 2, input2)
  307. else
  308. -- Else take 1 item from each stack
  309. input1:take_item()
  310. input2:take_item()
  311. inv:set_stack("input", 1, input1)
  312. inv:set_stack("input", 2, input2)
  313. end
  314. else
  315. -- Otherwise: Rename mode. Remove the same amount of items from input
  316. -- as has been taken from output
  317. if not input1:is_empty() then
  318. input1:set_count(math.max(0, input1:get_count() - stack:get_count()))
  319. inv:set_stack("input", 1, input1)
  320. end
  321. if not input2:is_empty() then
  322. input2:set_count(math.max(0, input2:get_count() - stack:get_count()))
  323. inv:set_stack("input", 2, input2)
  324. end
  325. end
  326. elseif listname == "input" then
  327. update_anvil_slots(meta)
  328. end
  329. end,
  330. on_construct = function(pos)
  331. local meta = minetest.get_meta(pos)
  332. local inv = meta:get_inventory()
  333. inv:set_size("input", 2)
  334. inv:set_size("output", 1)
  335. local form = get_anvil_formspec()
  336. meta:set_string("formspec", form)
  337. end,
  338. on_receive_fields = function(pos, formname, fields, sender)
  339. if fields.name_button or fields.name then
  340. local set_name
  341. if fields.name == nil then
  342. set_name = ""
  343. else
  344. set_name = fields.name
  345. end
  346. local meta = minetest.get_meta(pos)
  347. -- Limit name length
  348. set_name = string.sub(set_name, 1, MAX_NAME_LENGTH)
  349. meta:set_string("set_name", set_name)
  350. update_anvil_slots(meta)
  351. meta:set_string("formspec", get_anvil_formspec(set_name))
  352. end
  353. end,
  354. }
  355. if minetest.get_modpath("screwdriver") then
  356. anvildef.on_rotate = screwdriver.rotate_simple
  357. end
  358. local anvildef0 = table.copy(anvildef)
  359. anvildef0.description = "Anvil"
  360. anvildef0._doc_items_longdesc =
  361. [[The anvil allows you to repair tools and armor, and to give names to items.]]
  362. anvildef0._doc_items_usagehelp =
  363. "To use an anvil, rightclick it. An anvil has 2 input slots (on the left) and one output slot.\n"..
  364. "To rename items, put an item stack in one of the item slots while keeping the other input slot empty. Type in a name, hit enter or “Set Name”, then take the renamed item from the output slot.\n"..
  365. "There are two possibilities to repair tools (and armor):\n"..
  366. "• Tool + Tool: Place two tools of the same type in the input slots. The “health” of the repaired tool is the sum of the “health” of both input tools, plus a 12% bonus.\n"..
  367. "• Tool + Material: Some tools can also be repaired by combining them with an item that it's made of. For example, iron pickaxes can be repaired with iron ingots. This repairs the tool by 25%.\n"..
  368. "Armor counts as a tool. It is possible to repair and rename a tool in a single step."
  369. minetest.register_node("anvils:anvil", anvildef0)
  370. minetest.register_craft({
  371. output = "anvils:anvil",
  372. recipe = {
  373. { "default:steelblock", "default:steelblock", "default:steelblock" },
  374. { "", "default:steel_ingot", "" },
  375. { "default:steel_ingot", "default:steel_ingot", "default:steel_ingot" },
  376. }
  377. })