-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathanvils.lua
507 lines (460 loc) · 17.3 KB
/
anvils.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
local MAX_NAME_LENGTH = 30
local MAX_WEAR = 65535
local SAME_TOOL_REPAIR_BOOST = math.ceil(MAX_WEAR * 0.12) -- 12%
local MATERIAL_TOOL_REPAIR_BOOST = {
math.ceil(MAX_WEAR * 0.34), -- 25%
math.ceil(MAX_WEAR * 0.68), -- 50%
math.ceil(MAX_WEAR * 1), -- 75%
MAX_WEAR, -- 100%
}
local NAME_COLOR = "#FFFF4C"
local function get_anvil_formspec(set_name)
if not set_name then
set_name = ""
end
return "size[8,8.5]"..
"background[1,1;1,1;gui_formbg2.png;true]"..
"bgcolor[#080808BB;true]"..
"list[current_player;main;0,4.5;8,4]"..
"list[context;input;1,2.5;1,1;]"..
"list[context;input;3.5,2.5;1,1;1]"..
"list[context;output;6,2.5;1,1;]"..
"image[2.35,2.6;0.8,0.8;plus.png]" ..
"image[4.75,2.5;1,1;arrow0.png]" ..
"image[2.6,0.65;3,1;rep.png]" ..
"image[5.3,0.5;1.3,1.3;ham.png]" ..
"image[1.3,0.5;1.3,1.3;pick.png]" ..
"bgcolor[#080808BB;true]" ..
--default.gui_bg..
--default.gui_bg_img..
default.gui_slots..
"field_close_on_enter[name;false]"..
"listring[context;input]"..
"listring[context;output]"..
"listring[current_player;main]"..
"listring[current_player;main]"
end
-- Given a tool and material stack, returns how many items of the material stack
-- needs to be used up to repair the tool.
local function get_consumed_materials(tool, material)
local wear = tool:get_wear()
if wear == 0 then
return 0
end
local health = (MAX_WEAR - wear)
local matsize = material:get_count()
local materials_used = 0
for m=1, math.min(4, matsize) do
materials_used = materials_used + 1
if (wear - MATERIAL_TOOL_REPAIR_BOOST[m]) <= 0 then
break
end
end
return materials_used
end
-- Given 2 input stacks, tells you which is the tool and which is the material.
-- Returns ("tool", input1, input2) if input1 is tool and input2 is material.
-- Returns ("material", input2, input1) if input1 is material and input2 is tool.
-- Returns nil otherwise.
local function distinguish_tool_and_material(input1, input2)
local def1 = input1:get_definition()
local def2 = input2:get_definition()
if def1.type == "tool" and def1._repair_material then
return "tool", input1, input2
elseif def2.type == "tool" and def2._repair_material then
return "material", input2, input1
else
return nil
end
end
-- Update the inventory slots of an anvil node.
-- meta: Metadata of anvil node
local function update_anvil_slots(meta)
local inv = meta:get_inventory()
local new_name = meta:get_string("set_name")
local input1, input2, output
input1 = inv:get_stack("input", 1)
input2 = inv:get_stack("input", 2)
output = inv:get_stack("output", 1)
local new_output, name_item
local just_rename = false
-- Both input slots occupied
if (not input1:is_empty() and not input2:is_empty()) then
-- Repair, if tool
local def1 = input1:get_definition()
local def2 = input2:get_definition()
-- Repair calculation helper.
-- Adds the “inverse” values of wear1 and wear2.
-- Then adds a boost health value directly.
-- Returns the resulting (capped) wear.
local function calculate_repair(wear1, wear2, boost)
local new_health = (MAX_WEAR - wear1) + (MAX_WEAR - wear2)
if boost then
new_health = new_health + boost
end
return math.max(0, math.min(MAX_WEAR, MAX_WEAR - new_health))
end
-- Same tool twice
if input1:get_name() == input2:get_name() and def1.type == "tool" and (input1:get_wear() > 0 or input2:get_wear() > 0) then
-- Add tool health together plus a small bonus
-- TODO: Combine tool enchantments
local new_wear = calculate_repair(input1:get_wear(), input2:get_wear(), SAME_TOOL_REPAIR_BOOST)
input1:set_wear(new_wear)
name_item = input1
new_output = name_item
-- Tool + repair item
else
-- Any tool can have a repair item. This may be defined in the tool's item definition
-- as an itemstring in the field `_repair_material`. Only if this field is set, the
-- tool can be repaired with a material item.
-- Example: Steel Pickaxe + Steel Ingot. `_repair_material = default:steel_ingot`
-- Big repair bonus
-- TODO: Combine tool enchantments
local distinguished, tool, material = distinguish_tool_and_material(input1, input2)
if distinguished then
local tooldef = tool:get_definition()
local has_correct_material = false
if string.sub(tooldef._repair_material, 1, 6) == "group:" then
has_correct_material = minetest.get_item_group(material:get_name(), string.sub(tooldef._repair_material, 7)) ~= 0
elseif material:get_name() == tooldef._repair_material then
has_correct_material = true
end
if has_correct_material and tool:get_wear() > 0 then
local materials_used = get_consumed_materials(tool, material)
local new_wear = calculate_repair(tool:get_wear(), MAX_WEAR, MATERIAL_TOOL_REPAIR_BOOST[materials_used])
tool:set_wear(new_wear)
name_item = tool
new_output = name_item
else
new_output = ""
end
else
new_output = ""
end
end
-- Exactly 1 input slot occupied
elseif (not input1:is_empty() and input2:is_empty()) or (input1:is_empty() and not input2:is_empty()) then
-- Just rename item
if input1:is_empty() then
name_item = input2
else
name_item = input1
end
just_rename = true
else
new_output = ""
end
-- Rename handling
if name_item then
-- No renaming allowed with group no_rename=1
if minetest.get_item_group(name_item:get_name(), "no_rename") == 1 then
new_output = ""
else
if new_name == nil then
new_name = ""
end
local meta = name_item:get_meta()
local old_name = meta:get_string("name")
-- Limit name length
new_name = string.sub(new_name, 1, MAX_NAME_LENGTH)
-- Don't rename if names are identical
if new_name ~= old_name then
-- Rename item
if new_name == "" then
-- Empty name
if name_item:get_definition()._generate_description then
-- _generate_description(itemstack): If defined, set custom item description of itemstack.
name_item:get_definition()._generate_description(name_item)
else
-- Otherwise, just clear description
meta:set_string("description", "")
end
else
-- Custom name set. Colorize it!
-- This makes the name visually different from unnamed items
meta:set_string("description", core.colorize(NAME_COLOR, new_name))
end
-- Save the raw name internally, too
meta:set_string("name", new_name)
new_output = name_item
elseif just_rename then
new_output = ""
end
end
end
-- Set the new output slot
if new_output ~= nil then
inv:set_stack("output", 1, new_output)
end
end
-- Drop input items of anvil at pos with metadata meta
local function drop_anvil_items(pos, meta)
local inv = meta:get_inventory()
for i=1, inv:get_size("input") do
local stack = inv:get_stack("input", i)
if not stack:is_empty() then
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}
minetest.add_item(p, stack)
end
end
end
-- Damage the anvil by 1 level.
-- Destroy anvil when at highest damage level.
-- Returns true if anvil was destroyed.
local function damage_anvil(pos)
local node = minetest.get_node(pos)
local new
if node.name == "anvils:anvil" then
minetest.swap_node(pos, {name="anvils:anvil_damage_1", param2=node.param2})
minetest.sound_play(default.node_sound_metal_defaults().dig, {pos=pos, max_hear_distance=16})
return false
elseif node.name == "anvils:anvil_damage_1" then
minetest.swap_node(pos, {name="anvils:anvil_damage_2", param2=node.param2})
minetest.sound_play(default.node_sound_metal_defaults().dig, {pos=pos, max_hear_distance=16})
return false
elseif node.name == "anvils:anvil_damage_2" then
-- Destroy anvil
local meta = minetest.get_meta(pos)
drop_anvil_items(pos, meta)
minetest.sound_play(default.node_sound_metal_defaults().dug, {pos=pos, max_hear_distance=16})
minetest.remove_node(pos)
return true
end
end
-- Roll a virtual dice and damage anvil at a low chance.
local function damage_anvil_by_using(pos)
local r = math.random(1, 100)
-- 12% chance
if r <= 12 then
return damage_anvil(pos)
else
return false
end
end
local function damage_anvil_by_falling(pos, distance)
local chance
local r = math.random(1, 100)
if distance > 1 then
if r <= (5*distance) then
damage_anvil(pos)
end
end
end
local anvildef = {
groups = {cracky = 1, level = 2, pickaxey=1, falling_node=1, falling_node_damage=1, crush_after_fall=1, deco_block=1, anvil=1},
tiles = {"anvils_anvil_top_damaged_0.png^[transformR90", "anvils_anvil_base.png", "anvils_anvil_side.png"},
paramtype = "light",
sunlight_propagates = true,
is_ground_content = false,
paramtype2 = "facedir",
drawtype = "nodebox",
node_box = {
type = "fixed",
fixed = {
{-8/16, 2/16, -5/16, 8/16, 8/16, 5/16}, -- top
{-5/16, -4/16, -2/16, 5/16, 5/16, 2/16}, -- middle
{-8/16, -8/16, -5/16, 8/16, -4/16, 5/16}, -- base
}
},
sounds = default.node_sound_metal_defaults(),
_blast_resistance = 6000,
hardness = 5,
_after_falling = damage_anvil_by_falling,
after_place_node = function(pos, placer)
local name = placer:get_player_name()
local pinv = placer:get_inventory()
local meta = minetest.get_meta(pos);
local owner = placer:get_player_name() or "";
local privs = minetest.get_player_privs(owner);
meta:set_string("owner",owner);
meta:set_string("infotext","Anvil (owned by " .. owner .. "): ");
end,
after_dig_node = function(pos, oldnode, oldmetadata, digger)
local meta = minetest.get_meta(pos)
local meta2 = meta
meta:from_table(oldmetadata)
drop_anvil_items(pos, meta)
meta:from_table(meta2:to_table())
end,
allow_metadata_inventory_put = function(pos, listname, index, stack, player)
local meta = minetest.get_meta(pos);
local privs = minetest.get_player_privs(player:get_player_name());
if meta:get_string("owner")~=player:get_player_name() and not privs.privs then return 0 end
if listname == "output" then
return 0
else
return stack:get_count()
end
end,
allow_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player)
local meta = minetest.get_meta(pos);
local privs = minetest.get_player_privs(player:get_player_name());
if meta:get_string("owner")~=player:get_player_name() and not privs.privs then return 0 end
if to_list == "output" then
return 0
elseif from_list == "output" and to_list == "input" then
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
if inv:get_stack(to_list, to_index):is_empty() then
return count
else
return 0
end
else
return count
end
end,
on_metadata_inventory_put = function(pos, listname, index, stack, player)
local meta = minetest.get_meta(pos)
update_anvil_slots(meta)
end,
on_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player)
local meta = minetest.get_meta(pos)
if from_list == "output" and to_list == "input" then
local inv = meta:get_inventory()
for i=1, inv:get_size("input") do
if i ~= to_index then
local istack = inv:get_stack("input", i)
istack:set_count(math.max(0, istack:get_count() - count))
inv:set_stack("input", i, istack)
end
end
end
update_anvil_slots(meta)
if from_list == "output" then
local destroyed = damage_anvil_by_using(pos)
-- Close formspec if anvil was destroyed
if destroyed then
--[[ Closing the formspec w/ emptyformname is discouraged. But this is justified
because node formspecs seem to only have an empty formname in MT 0.4.16.
Also, sice this is on_metadata_inventory_take, we KNOW which formspec has
been opened by the player. So this should be safe nonetheless.
TODO: Update this line when node formspecs get proper identifiers in Minetest. ]]
minetest.close_formspec(player:get_player_name(), "")
end
end
end,
allow_metadata_inventory_take = function(pos, listname, index, stack, player)
local meta = minetest.get_meta(pos);
local privs = minetest.get_player_privs(player:get_player_name());
if meta:get_string("owner")~=player:get_player_name() and not privs.privs then return 0 end
return stack:get_count();
end,
on_metadata_inventory_take = function(pos, listname, index, stack, player)
local meta = minetest.get_meta(pos)
if listname == "output" then
local inv = meta:get_inventory()
local input1 = inv:get_stack("input", 1)
local input2 = inv:get_stack("input", 2)
-- Both slots occupied?
if not input1:is_empty() and not input2:is_empty() then
-- Take as many items as needed
local distinguished, tool, material = distinguish_tool_and_material(input1, input2)
if distinguished then
-- Tool + material: Take tool and as many materials as needed
local materials_used = get_consumed_materials(tool, material)
material:set_count(material:get_count() - materials_used)
tool:take_item()
local player_name = player:get_player_name()
minetest.sound_play("anvil_use", {to_player=player_name, gain = 1.0})
if distinguished == "tool" then
input1, input2 = tool, material
else
input1, input2 = material, tool
end
inv:set_stack("input", 1, input1)
inv:set_stack("input", 2, input2)
else
-- Else take 1 item from each stack
input1:take_item()
input2:take_item()
inv:set_stack("input", 1, input1)
inv:set_stack("input", 2, input2)
end
else
-- Otherwise: Rename mode. Remove the same amount of items from input
-- as has been taken from output
if not input1:is_empty() then
input1:set_count(math.max(0, input1:get_count() - stack:get_count()))
inv:set_stack("input", 1, input1)
end
if not input2:is_empty() then
input2:set_count(math.max(0, input2:get_count() - stack:get_count()))
inv:set_stack("input", 2, input2)
end
end
local destroyed = damage_anvil_by_using(pos)
-- Close formspec if anvil was destroyed
if destroyed then
-- See above for justification.
minetest.close_formspec(player:get_player_name(), "")
end
elseif listname == "input" then
update_anvil_slots(meta)
end
end,
on_construct = function(pos)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
inv:set_size("input", 2)
inv:set_size("output", 1)
local form = get_anvil_formspec()
meta:set_string("formspec", form)
end,
on_receive_fields = function(pos, formname, fields, sender)
if fields.name_button or fields.name then
local set_name
if fields.name == nil then
set_name = ""
else
set_name = fields.name
end
local meta = minetest.get_meta(pos)
-- Limit name length
set_name = string.sub(set_name, 1, MAX_NAME_LENGTH)
meta:set_string("set_name", set_name)
update_anvil_slots(meta)
meta:set_string("formspec", get_anvil_formspec(set_name))
end
end,
}
if minetest.get_modpath("screwdriver") then
anvildef.on_rotate = screwdriver.rotate_simple
end
local anvildef0 = table.copy(anvildef)
anvildef0.description = "Anvil"
anvildef0._doc_items_longdesc =
[[The anvil allows you to repair tools and armor, and to give names to items. It has a limited durability, however. Don't let it fall on your head, it could be quite painful!]]
anvildef0._doc_items_usagehelp =
"To use an anvil, rightclick it. An anvil has 2 input slots (on the left) and one output slot.".."\n"..
"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"..
"There are two possibilities to repair tools (and armor):".."\n"..
"• 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"..
"• 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"..
"Armor counts as a tool. It is possible to repair and rename a tool in a single step.".."\n\n"..
"The anvil has limited durability and 3 damage levels: undamaged, slightly damaged and very damaged. Each time you repair or rename something, there is a 12% chance the anvil gets damaged. Anvils also have a chance of being damaged when they fall by more than 1 block. If a very damaged anvil is damaged again, it is destroyed."
local anvildef1 = table.copy(anvildef)
anvildef1.description = "Slightly Damaged Anvil"
anvildef1._doc_items_create_entry = false
anvildef1.groups.not_in_creative_inventory = 1
anvildef1.groups.anvil = 2
anvildef1._doc_items_create_entry = false
anvildef1.tiles = {"anvils_anvil_top_damaged_1.png^[transformR90", "anvils_anvil_base.png", "anvils_anvil_side.png"}
local anvildef2 = table.copy(anvildef)
anvildef2.description = "Very Damaged Anvil"
anvildef2._doc_items_create_entry = false
anvildef2.groups.not_in_creative_inventory = 1
anvildef2.groups.anvil = 3
anvildef2._doc_items_create_entry = false
anvildef2.tiles = {"anvils_anvil_top_damaged_2.png^[transformR90", "anvils_anvil_base.png", "anvils_anvil_side.png"}
minetest.register_node("anvils:anvil", anvildef0)
minetest.register_node("anvils:anvil_damage_1", anvildef1)
minetest.register_node("anvils:anvil_damage_2", anvildef2)
minetest.register_craft({
output = "anvils:anvil",
recipe = {
{ "default:steelblock", "default:steelblock", "default:steelblock" },
{ "", "default:steel_ingot", "" },
{ "default:steel_ingot", "default:steel_ingot", "default:steel_ingot" },
}
})