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