]> git.lizzy.rs Git - minetest.git/blob - builtin/game/item.lua
Automatic item and node colorization (#5640)
[minetest.git] / builtin / game / item.lua
1 -- Minetest: builtin/item.lua
2
3 local builtin_shared = ...
4
5 local function copy_pointed_thing(pointed_thing)
6         return {
7                 type  = pointed_thing.type,
8                 above = vector.new(pointed_thing.above),
9                 under = vector.new(pointed_thing.under),
10                 ref   = pointed_thing.ref,
11         }
12 end
13
14 --
15 -- Item definition helpers
16 --
17
18 function core.inventorycube(img1, img2, img3)
19         img2 = img2 or img1
20         img3 = img3 or img1
21         return "[inventorycube"
22                         .. "{" .. img1:gsub("%^", "&")
23                         .. "{" .. img2:gsub("%^", "&")
24                         .. "{" .. img3:gsub("%^", "&")
25 end
26
27 function core.get_pointed_thing_position(pointed_thing, above)
28         if pointed_thing.type == "node" then
29                 if above then
30                         -- The position where a node would be placed
31                         return pointed_thing.above
32                 end
33                 -- The position where a node would be dug
34                 return pointed_thing.under
35         elseif pointed_thing.type == "object" then
36                 return pointed_thing.ref and pointed_thing.ref:getpos()
37         end
38 end
39
40 function core.dir_to_facedir(dir, is6d)
41         --account for y if requested
42         if is6d and math.abs(dir.y) > math.abs(dir.x) and math.abs(dir.y) > math.abs(dir.z) then
43
44                 --from above
45                 if dir.y < 0 then
46                         if math.abs(dir.x) > math.abs(dir.z) then
47                                 if dir.x < 0 then
48                                         return 19
49                                 else
50                                         return 13
51                                 end
52                         else
53                                 if dir.z < 0 then
54                                         return 10
55                                 else
56                                         return 4
57                                 end
58                         end
59
60                 --from below
61                 else
62                         if math.abs(dir.x) > math.abs(dir.z) then
63                                 if dir.x < 0 then
64                                         return 15
65                                 else
66                                         return 17
67                                 end
68                         else
69                                 if dir.z < 0 then
70                                         return 6
71                                 else
72                                         return 8
73                                 end
74                         end
75                 end
76
77         --otherwise, place horizontally
78         elseif math.abs(dir.x) > math.abs(dir.z) then
79                 if dir.x < 0 then
80                         return 3
81                 else
82                         return 1
83                 end
84         else
85                 if dir.z < 0 then
86                         return 2
87                 else
88                         return 0
89                 end
90         end
91 end
92
93 -- Table of possible dirs
94 local facedir_to_dir = {
95         {x= 0, y=0,  z= 1},
96         {x= 1, y=0,  z= 0},
97         {x= 0, y=0,  z=-1},
98         {x=-1, y=0,  z= 0},
99         {x= 0, y=-1, z= 0},
100         {x= 0, y=1,  z= 0},
101 }
102 -- Mapping from facedir value to index in facedir_to_dir.
103 local facedir_to_dir_map = {
104         [0]=1, 2, 3, 4,
105         5, 2, 6, 4,
106         6, 2, 5, 4,
107         1, 5, 3, 6,
108         1, 6, 3, 5,
109         1, 4, 3, 2,
110 }
111 function core.facedir_to_dir(facedir)
112         return facedir_to_dir[facedir_to_dir_map[facedir % 32]]
113 end
114
115 function core.dir_to_wallmounted(dir)
116         if math.abs(dir.y) > math.max(math.abs(dir.x), math.abs(dir.z)) then
117                 if dir.y < 0 then
118                         return 1
119                 else
120                         return 0
121                 end
122         elseif math.abs(dir.x) > math.abs(dir.z) then
123                 if dir.x < 0 then
124                         return 3
125                 else
126                         return 2
127                 end
128         else
129                 if dir.z < 0 then
130                         return 5
131                 else
132                         return 4
133                 end
134         end
135 end
136
137 -- table of dirs in wallmounted order
138 local wallmounted_to_dir = {
139         [0] = {x = 0, y = 1, z = 0},
140         {x =  0, y = -1, z =  0},
141         {x =  1, y =  0, z =  0},
142         {x = -1, y =  0, z =  0},
143         {x =  0, y =  0, z =  1},
144         {x =  0, y =  0, z = -1},
145 }
146 function core.wallmounted_to_dir(wallmounted)
147         return wallmounted_to_dir[wallmounted % 8]
148 end
149
150 function core.dir_to_yaw(dir)
151         return -math.atan2(dir.x, dir.z)
152 end
153
154 function core.yaw_to_dir(yaw)
155         return {x = -math.sin(yaw), y = 0, z = math.cos(yaw)}
156 end
157
158 function core.get_node_drops(node, toolname)
159         -- Compatibility, if node is string
160         local nodename = node
161         local param2 = 0
162         -- New format, if node is table
163         if (type(node) == "table") then
164                 nodename = node.name
165                 param2 = node.param2
166         end
167         local def = core.registered_nodes[nodename]
168         local drop = def and def.drop
169         if drop == nil then
170                 -- default drop
171                 local stack = ItemStack(nodename)
172                 if def then
173                         local type = def.paramtype2
174                         if (type == "color") or (type == "colorfacedir") or
175                                         (type == "colorwallmounted") then
176                                 local meta = stack:get_meta()
177                                 local color_part = param2
178                                 if (type == "colorfacedir") then
179                                         color_part = math.floor(color_part / 32) * 32;
180                                 elseif (type == "colorwallmounted") then
181                                         color_part = math.floor(color_part / 8) * 8;
182                                 end
183                                 meta:set_int("palette_index", color_part)
184                         end
185                 end
186                 return {stack:to_string()}
187         elseif type(drop) == "string" then
188                 -- itemstring drop
189                 return {drop}
190         elseif drop.items == nil then
191                 -- drop = {} to disable default drop
192                 return {}
193         end
194
195         -- Extended drop table
196         local got_items = {}
197         local got_count = 0
198         local _, item, tool
199         for _, item in ipairs(drop.items) do
200                 local good_rarity = true
201                 local good_tool = true
202                 if item.rarity ~= nil then
203                         good_rarity = item.rarity < 1 or math.random(item.rarity) == 1
204                 end
205                 if item.tools ~= nil then
206                         good_tool = false
207                         for _, tool in ipairs(item.tools) do
208                                 if tool:sub(1, 1) == '~' then
209                                         good_tool = toolname:find(tool:sub(2)) ~= nil
210                                 else
211                                         good_tool = toolname == tool
212                                 end
213                                 if good_tool then
214                                         break
215                                 end
216                         end
217                 end
218                 if good_rarity and good_tool then
219                         got_count = got_count + 1
220                         for _, add_item in ipairs(item.items) do
221                                 got_items[#got_items+1] = add_item
222                         end
223                         if drop.max_items ~= nil and got_count == drop.max_items then
224                                 break
225                         end
226                 end
227         end
228         return got_items
229 end
230
231 function core.item_place_node(itemstack, placer, pointed_thing, param2)
232         local def = itemstack:get_definition()
233         if def.type ~= "node" or pointed_thing.type ~= "node" then
234                 return itemstack, false
235         end
236
237         local under = pointed_thing.under
238         local oldnode_under = core.get_node_or_nil(under)
239         local above = pointed_thing.above
240         local oldnode_above = core.get_node_or_nil(above)
241         local playername = placer:get_player_name()
242
243         if not oldnode_under or not oldnode_above then
244                 core.log("info", playername .. " tried to place"
245                         .. " node in unloaded position " .. core.pos_to_string(above))
246                 return itemstack, false
247         end
248
249         local olddef_under = core.registered_nodes[oldnode_under.name]
250         olddef_under = olddef_under or core.nodedef_default
251         local olddef_above = core.registered_nodes[oldnode_above.name]
252         olddef_above = olddef_above or core.nodedef_default
253
254         if not olddef_above.buildable_to and not olddef_under.buildable_to then
255                 core.log("info", playername .. " tried to place"
256                         .. " node in invalid position " .. core.pos_to_string(above)
257                         .. ", replacing " .. oldnode_above.name)
258                 return itemstack, false
259         end
260
261         -- Place above pointed node
262         local place_to = {x = above.x, y = above.y, z = above.z}
263
264         -- If node under is buildable_to, place into it instead (eg. snow)
265         if olddef_under.buildable_to then
266                 core.log("info", "node under is buildable to")
267                 place_to = {x = under.x, y = under.y, z = under.z}
268         end
269
270         if core.is_protected(place_to, playername) and
271                         not minetest.check_player_privs(placer, "protection_bypass") then
272                 core.log("action", playername
273                                 .. " tried to place " .. def.name
274                                 .. " at protected position "
275                                 .. core.pos_to_string(place_to))
276                 core.record_protection_violation(place_to, playername)
277                 return itemstack
278         end
279
280         core.log("action", playername .. " places node "
281                 .. def.name .. " at " .. core.pos_to_string(place_to))
282
283         local oldnode = core.get_node(place_to)
284         local newnode = {name = def.name, param1 = 0, param2 = param2 or 0}
285
286         -- Calculate direction for wall mounted stuff like torches and signs
287         if def.place_param2 ~= nil then
288                 newnode.param2 = def.place_param2
289         elseif (def.paramtype2 == "wallmounted" or
290                         def.paramtype2 == "colorwallmounted") and not param2 then
291                 local dir = {
292                         x = under.x - above.x,
293                         y = under.y - above.y,
294                         z = under.z - above.z
295                 }
296                 newnode.param2 = core.dir_to_wallmounted(dir)
297         -- Calculate the direction for furnaces and chests and stuff
298         elseif (def.paramtype2 == "facedir" or
299                         def.paramtype2 == "colorfacedir") and not param2 then
300                 local placer_pos = placer:getpos()
301                 if placer_pos then
302                         local dir = {
303                                 x = above.x - placer_pos.x,
304                                 y = above.y - placer_pos.y,
305                                 z = above.z - placer_pos.z
306                         }
307                         newnode.param2 = core.dir_to_facedir(dir)
308                         core.log("action", "facedir: " .. newnode.param2)
309                 end
310         end
311
312         local metatable = itemstack:get_meta():to_table().fields
313
314         -- Transfer color information
315         if metatable.palette_index and not def.place_param2 then
316                 local color_divisor = nil
317                 if def.paramtype2 == "color" then
318                         color_divisor = 1
319                 elseif def.paramtype2 == "colorwallmounted" then
320                         color_divisor = 8
321                 elseif def.paramtype2 == "colorfacedir" then
322                         color_divisor = 32
323                 end
324                 if color_divisor then
325                         local color = math.floor(metatable.palette_index / color_divisor)
326                         local other = newnode.param2 % color_divisor
327                         newnode.param2 = color * color_divisor + other
328                 end
329         end
330
331         -- Check if the node is attached and if it can be placed there
332         if core.get_item_group(def.name, "attached_node") ~= 0 and
333                 not builtin_shared.check_attached_node(place_to, newnode) then
334                 core.log("action", "attached node " .. def.name ..
335                         " can not be placed at " .. core.pos_to_string(place_to))
336                 return itemstack, false
337         end
338
339         -- Add node and update
340         core.add_node(place_to, newnode)
341
342         local take_item = true
343
344         -- Run callback
345         if def.after_place_node then
346                 -- Deepcopy place_to and pointed_thing because callback can modify it
347                 local place_to_copy = {x=place_to.x, y=place_to.y, z=place_to.z}
348                 local pointed_thing_copy = copy_pointed_thing(pointed_thing)
349                 if def.after_place_node(place_to_copy, placer, itemstack,
350                                 pointed_thing_copy) then
351                         take_item = false
352                 end
353         end
354
355         -- Run script hook
356         for _, callback in ipairs(core.registered_on_placenodes) do
357                 -- Deepcopy pos, node and pointed_thing because callback can modify them
358                 local place_to_copy = {x=place_to.x, y=place_to.y, z=place_to.z}
359                 local newnode_copy = {name=newnode.name, param1=newnode.param1, param2=newnode.param2}
360                 local oldnode_copy = {name=oldnode.name, param1=oldnode.param1, param2=oldnode.param2}
361                 local pointed_thing_copy = copy_pointed_thing(pointed_thing)
362                 if callback(place_to_copy, newnode_copy, placer, oldnode_copy, itemstack, pointed_thing_copy) then
363                         take_item = false
364                 end
365         end
366
367         if take_item then
368                 itemstack:take_item()
369         end
370         return itemstack, true
371 end
372
373 function core.item_place_object(itemstack, placer, pointed_thing)
374         local pos = core.get_pointed_thing_position(pointed_thing, true)
375         if pos ~= nil then
376                 local item = itemstack:take_item()
377                 core.add_item(pos, item)
378         end
379         return itemstack
380 end
381
382 function core.item_place(itemstack, placer, pointed_thing, param2)
383         -- Call on_rightclick if the pointed node defines it
384         if pointed_thing.type == "node" and placer and
385                         not placer:get_player_control().sneak then
386                 local n = core.get_node(pointed_thing.under)
387                 local nn = n.name
388                 if core.registered_nodes[nn] and core.registered_nodes[nn].on_rightclick then
389                         return core.registered_nodes[nn].on_rightclick(pointed_thing.under, n,
390                                         placer, itemstack, pointed_thing) or itemstack, false
391                 end
392         end
393
394         if itemstack:get_definition().type == "node" then
395                 return core.item_place_node(itemstack, placer, pointed_thing, param2)
396         end
397         return itemstack
398 end
399
400 function core.item_secondary_use(itemstack, placer)
401         return itemstack
402 end
403
404 function core.item_drop(itemstack, dropper, pos)
405         if dropper and dropper:is_player() then
406                 local v = dropper:get_look_dir()
407                 local p = {x=pos.x, y=pos.y+1.2, z=pos.z}
408                 local cs = itemstack:get_count()
409                 if dropper:get_player_control().sneak then
410                         cs = 1
411                 end
412                 local item = itemstack:take_item(cs)
413                 local obj = core.add_item(p, item)
414                 if obj then
415                         v.x = v.x*2
416                         v.y = v.y*2 + 2
417                         v.z = v.z*2
418                         obj:setvelocity(v)
419                         obj:get_luaentity().dropped_by = dropper:get_player_name()
420                         return itemstack
421                 end
422
423         else
424                 if core.add_item(pos, itemstack) then
425                         return itemstack
426                 end
427         end
428         -- If we reach this, adding the object to the
429         -- environment failed
430 end
431
432 function core.do_item_eat(hp_change, replace_with_item, itemstack, user, pointed_thing)
433         for _, callback in pairs(core.registered_on_item_eats) do
434                 local result = callback(hp_change, replace_with_item, itemstack, user, pointed_thing)
435                 if result then
436                         return result
437                 end
438         end
439         if itemstack:take_item() ~= nil then
440                 user:set_hp(user:get_hp() + hp_change)
441
442                 if replace_with_item then
443                         if itemstack:is_empty() then
444                                 itemstack:add_item(replace_with_item)
445                         else
446                                 local inv = user:get_inventory()
447                                 if inv:room_for_item("main", {name=replace_with_item}) then
448                                         inv:add_item("main", replace_with_item)
449                                 else
450                                         local pos = user:getpos()
451                                         pos.y = math.floor(pos.y + 0.5)
452                                         core.add_item(pos, replace_with_item)
453                                 end
454                         end
455                 end
456         end
457         return itemstack
458 end
459
460 function core.item_eat(hp_change, replace_with_item)
461         return function(itemstack, user, pointed_thing)  -- closure
462                 return core.do_item_eat(hp_change, replace_with_item, itemstack, user, pointed_thing)
463         end
464 end
465
466 function core.node_punch(pos, node, puncher, pointed_thing)
467         -- Run script hook
468         for _, callback in ipairs(core.registered_on_punchnodes) do
469                 -- Copy pos and node because callback can modify them
470                 local pos_copy = vector.new(pos)
471                 local node_copy = {name=node.name, param1=node.param1, param2=node.param2}
472                 local pointed_thing_copy = pointed_thing and copy_pointed_thing(pointed_thing) or nil
473                 callback(pos_copy, node_copy, puncher, pointed_thing_copy)
474         end
475 end
476
477 function core.handle_node_drops(pos, drops, digger)
478         -- Add dropped items to object's inventory
479         if digger:get_inventory() then
480                 local _, dropped_item
481                 for _, dropped_item in ipairs(drops) do
482                         local left = digger:get_inventory():add_item("main", dropped_item)
483                         if not left:is_empty() then
484                                 local p = {
485                                         x = pos.x + math.random()/2-0.25,
486                                         y = pos.y + math.random()/2-0.25,
487                                         z = pos.z + math.random()/2-0.25,
488                                 }
489                                 core.add_item(p, left)
490                         end
491                 end
492         end
493 end
494
495 function core.node_dig(pos, node, digger)
496         local def = core.registered_nodes[node.name]
497         if def and (not def.diggable or
498                         (def.can_dig and not def.can_dig(pos, digger))) then
499                 core.log("info", digger:get_player_name() .. " tried to dig "
500                         .. node.name .. " which is not diggable "
501                         .. core.pos_to_string(pos))
502                 return
503         end
504
505         if core.is_protected(pos, digger:get_player_name()) and
506                         not minetest.check_player_privs(digger, "protection_bypass") then
507                 core.log("action", digger:get_player_name()
508                                 .. " tried to dig " .. node.name
509                                 .. " at protected position "
510                                 .. core.pos_to_string(pos))
511                 core.record_protection_violation(pos, digger:get_player_name())
512                 return
513         end
514
515         core.log('action', digger:get_player_name() .. " digs "
516                 .. node.name .. " at " .. core.pos_to_string(pos))
517
518         local wielded = digger:get_wielded_item()
519         local drops = core.get_node_drops(node, wielded:get_name())
520
521         local wdef = wielded:get_definition()
522         local tp = wielded:get_tool_capabilities()
523         local dp = core.get_dig_params(def and def.groups, tp)
524         if wdef and wdef.after_use then
525                 wielded = wdef.after_use(wielded, digger, node, dp) or wielded
526         else
527                 -- Wear out tool
528                 if not core.settings:get_bool("creative_mode") then
529                         wielded:add_wear(dp.wear)
530                         if wielded:get_count() == 0 and wdef.sound and wdef.sound.breaks then
531                                 core.sound_play(wdef.sound.breaks, {pos = pos, gain = 0.5})
532                         end
533                 end
534         end
535         digger:set_wielded_item(wielded)
536
537         -- Handle drops
538         core.handle_node_drops(pos, drops, digger)
539
540         local oldmetadata = nil
541         if def and def.after_dig_node then
542                 oldmetadata = core.get_meta(pos):to_table()
543         end
544
545         -- Remove node and update
546         core.remove_node(pos)
547
548         -- Run callback
549         if def and def.after_dig_node then
550                 -- Copy pos and node because callback can modify them
551                 local pos_copy = {x=pos.x, y=pos.y, z=pos.z}
552                 local node_copy = {name=node.name, param1=node.param1, param2=node.param2}
553                 def.after_dig_node(pos_copy, node_copy, oldmetadata, digger)
554         end
555
556         -- Run script hook
557         local _, callback
558         for _, callback in ipairs(core.registered_on_dignodes) do
559                 local origin = core.callback_origins[callback]
560                 if origin then
561                         core.set_last_run_mod(origin.mod)
562                         --print("Running " .. tostring(callback) ..
563                         --      " (a " .. origin.name .. " callback in " .. origin.mod .. ")")
564                 else
565                         --print("No data associated with callback")
566                 end
567
568                 -- Copy pos and node because callback can modify them
569                 local pos_copy = {x=pos.x, y=pos.y, z=pos.z}
570                 local node_copy = {name=node.name, param1=node.param1, param2=node.param2}
571                 callback(pos_copy, node_copy, digger)
572         end
573 end
574
575 -- This is used to allow mods to redefine core.item_place and so on
576 -- NOTE: This is not the preferred way. Preferred way is to provide enough
577 --       callbacks to not require redefining global functions. -celeron55
578 local function redef_wrapper(table, name)
579         return function(...)
580                 return table[name](...)
581         end
582 end
583
584 --
585 -- Item definition defaults
586 --
587
588 core.nodedef_default = {
589         -- Item properties
590         type="node",
591         -- name intentionally not defined here
592         description = "",
593         groups = {},
594         inventory_image = "",
595         wield_image = "",
596         wield_scale = {x=1,y=1,z=1},
597         stack_max = 99,
598         usable = false,
599         liquids_pointable = false,
600         tool_capabilities = nil,
601         node_placement_prediction = nil,
602
603         -- Interaction callbacks
604         on_place = redef_wrapper(core, 'item_place'), -- core.item_place
605         on_drop = redef_wrapper(core, 'item_drop'), -- core.item_drop
606         on_use = nil,
607         can_dig = nil,
608
609         on_punch = redef_wrapper(core, 'node_punch'), -- core.node_punch
610         on_rightclick = nil,
611         on_dig = redef_wrapper(core, 'node_dig'), -- core.node_dig
612
613         on_receive_fields = nil,
614
615         on_metadata_inventory_move = core.node_metadata_inventory_move_allow_all,
616         on_metadata_inventory_offer = core.node_metadata_inventory_offer_allow_all,
617         on_metadata_inventory_take = core.node_metadata_inventory_take_allow_all,
618
619         -- Node properties
620         drawtype = "normal",
621         visual_scale = 1.0,
622         -- Don't define these because otherwise the old tile_images and
623         -- special_materials wouldn't be read
624         --tiles ={""},
625         --special_tiles = {
626         --      {name="", backface_culling=true},
627         --      {name="", backface_culling=true},
628         --},
629         alpha = 255,
630         post_effect_color = {a=0, r=0, g=0, b=0},
631         paramtype = "none",
632         paramtype2 = "none",
633         is_ground_content = true,
634         sunlight_propagates = false,
635         walkable = true,
636         pointable = true,
637         diggable = true,
638         climbable = false,
639         buildable_to = false,
640         floodable = false,
641         liquidtype = "none",
642         liquid_alternative_flowing = "",
643         liquid_alternative_source = "",
644         liquid_viscosity = 0,
645         drowning = 0,
646         light_source = 0,
647         damage_per_second = 0,
648         selection_box = {type="regular"},
649         legacy_facedir_simple = false,
650         legacy_wallmounted = false,
651 }
652
653 core.craftitemdef_default = {
654         type="craft",
655         -- name intentionally not defined here
656         description = "",
657         groups = {},
658         inventory_image = "",
659         wield_image = "",
660         wield_scale = {x=1,y=1,z=1},
661         stack_max = 99,
662         liquids_pointable = false,
663         tool_capabilities = nil,
664
665         -- Interaction callbacks
666         on_place = redef_wrapper(core, 'item_place'), -- core.item_place
667         on_drop = redef_wrapper(core, 'item_drop'), -- core.item_drop
668         on_secondary_use = redef_wrapper(core, 'item_secondary_use'),
669         on_use = nil,
670 }
671
672 core.tooldef_default = {
673         type="tool",
674         -- name intentionally not defined here
675         description = "",
676         groups = {},
677         inventory_image = "",
678         wield_image = "",
679         wield_scale = {x=1,y=1,z=1},
680         stack_max = 1,
681         liquids_pointable = false,
682         tool_capabilities = nil,
683
684         -- Interaction callbacks
685         on_place = redef_wrapper(core, 'item_place'), -- core.item_place
686         on_secondary_use = redef_wrapper(core, 'item_secondary_use'),
687         on_drop = redef_wrapper(core, 'item_drop'), -- core.item_drop
688         on_use = nil,
689 }
690
691 core.noneitemdef_default = {  -- This is used for the hand and unknown items
692         type="none",
693         -- name intentionally not defined here
694         description = "",
695         groups = {},
696         inventory_image = "",
697         wield_image = "",
698         wield_scale = {x=1,y=1,z=1},
699         stack_max = 99,
700         liquids_pointable = false,
701         tool_capabilities = nil,
702
703         -- Interaction callbacks
704         on_place = redef_wrapper(core, 'item_place'),
705         on_secondary_use = redef_wrapper(core, 'item_secondary_use'),
706         on_drop = nil,
707         on_use = nil,
708 }