Show infotext with description for item entities
[oweals/minetest.git] / builtin / game / item_entity.lua
1 -- Minetest: builtin/item_entity.lua
2
3 function core.spawn_item(pos, item)
4         -- Take item in any format
5         local stack = ItemStack(item)
6         local obj = core.add_entity(pos, "__builtin:item")
7         -- Don't use obj if it couldn't be added to the map.
8         if obj then
9                 obj:get_luaentity():set_item(stack:to_string())
10         end
11         return obj
12 end
13
14 -- If item_entity_ttl is not set, enity will have default life time
15 -- Setting it to -1 disables the feature
16
17 local time_to_live = tonumber(core.setting_get("item_entity_ttl"))
18 if not time_to_live then
19         time_to_live = 900
20 end
21
22 core.register_entity(":__builtin:item", {
23         initial_properties = {
24                 hp_max = 1,
25                 physical = true,
26                 collide_with_objects = false,
27                 collisionbox = {-0.3, -0.3, -0.3, 0.3, 0.3, 0.3},
28                 visual = "wielditem",
29                 visual_size = {x = 0.4, y = 0.4},
30                 textures = {""},
31                 spritediv = {x = 1, y = 1},
32                 initial_sprite_basepos = {x = 0, y = 0},
33                 is_visible = false,
34                 infotext = "",
35         },
36
37         itemstring = '',
38         physical_state = true,
39         age = 0,
40
41         set_item = function(self, itemstring)
42                 self.itemstring = itemstring
43                 local stack = ItemStack(itemstring)
44                 local count = stack:get_count()
45                 local max_count = stack:get_stack_max()
46                 if count > max_count then
47                         count = max_count
48                         self.itemstring = stack:get_name().." "..max_count
49                 end
50                 local s = 0.2 + 0.1 * (count / max_count)
51                 local c = s
52                 local itemtable = stack:to_table()
53                 local itemname = nil
54                 local description = ""
55                 if itemtable then
56                         itemname = stack:to_table().name
57                 end
58                 local item_texture = nil
59                 local item_type = ""
60                 if core.registered_items[itemname] then
61                         item_texture = core.registered_items[itemname].inventory_image
62                         item_type = core.registered_items[itemname].type
63                         description = core.registered_items[itemname].description
64                 end
65                 local prop = {
66                         is_visible = true,
67                         visual = "wielditem",
68                         textures = {itemname},
69                         visual_size = {x = s, y = s},
70                         collisionbox = {-c, -c, -c, c, c, c},
71                         automatic_rotate = math.pi * 0.5,
72                         infotext = description,
73                 }
74                 self.object:set_properties(prop)
75         end,
76
77         get_staticdata = function(self)
78                 return core.serialize({
79                         itemstring = self.itemstring,
80                         always_collect = self.always_collect,
81                         age = self.age,
82                         dropped_by = self.dropped_by
83                 })
84         end,
85
86         on_activate = function(self, staticdata, dtime_s)
87                 if string.sub(staticdata, 1, string.len("return")) == "return" then
88                         local data = core.deserialize(staticdata)
89                         if data and type(data) == "table" then
90                                 self.itemstring = data.itemstring
91                                 self.always_collect = data.always_collect
92                                 if data.age then
93                                         self.age = data.age + dtime_s
94                                 else
95                                         self.age = dtime_s
96                                 end
97                                 self.dropped_by = data.dropped_by
98                         end
99                 else
100                         self.itemstring = staticdata
101                 end
102                 self.object:set_armor_groups({immortal = 1})
103                 self.object:setvelocity({x = 0, y = 2, z = 0})
104                 self.object:setacceleration({x = 0, y = -10, z = 0})
105                 self:set_item(self.itemstring)
106         end,
107
108         try_merge_with = function(self, own_stack, object, obj)
109                 local stack = ItemStack(obj.itemstring)
110                 if own_stack:get_name() == stack:get_name() and stack:get_free_space() > 0 then
111                         local overflow = false
112                         local count = stack:get_count() + own_stack:get_count()
113                         local max_count = stack:get_stack_max()
114                         if count > max_count then
115                                 overflow = true
116                                 count = count - max_count
117                         else
118                                 self.itemstring = ''
119                         end
120                         local pos = object:getpos()
121                         pos.y = pos.y + (count - stack:get_count()) / max_count * 0.15
122                         object:moveto(pos, false)
123                         local s, c
124                         local max_count = stack:get_stack_max()
125                         local name = stack:get_name()
126                         if not overflow then
127                                 obj.itemstring = name .. " " .. count
128                                 s = 0.2 + 0.1 * (count / max_count)
129                                 c = s
130                                 object:set_properties({
131                                         visual_size = {x = s, y = s},
132                                         collisionbox = {-c, -c, -c, c, c, c}
133                                 })
134                                 self.object:remove()
135                                 -- merging succeeded
136                                 return true
137                         else
138                                 s = 0.4
139                                 c = 0.3
140                                 object:set_properties({
141                                         visual_size = {x = s, y = s},
142                                         collisionbox = {-c, -c, -c, c, c, c}
143                                 })
144                                 obj.itemstring = name .. " " .. max_count
145                                 s = 0.2 + 0.1 * (count / max_count)
146                                 c = s
147                                 self.object:set_properties({
148                                         visual_size = {x = s, y = s},
149                                         collisionbox = {-c, -c, -c, c, c, c}
150                                 })
151                                 self.itemstring = name .. " " .. count
152                         end
153                 end
154                 -- merging didn't succeed
155                 return false
156         end,
157
158         on_step = function(self, dtime)
159                 self.age = self.age + dtime
160                 if time_to_live > 0 and self.age > time_to_live then
161                         self.itemstring = ''
162                         self.object:remove()
163                         return
164                 end
165                 local p = self.object:getpos()
166                 p.y = p.y - 0.5
167                 local node = core.get_node_or_nil(p)
168                 local in_unloaded = (node == nil)
169                 if in_unloaded then
170                         -- Don't infinetly fall into unloaded map
171                         self.object:setvelocity({x = 0, y = 0, z = 0})
172                         self.object:setacceleration({x = 0, y = 0, z = 0})
173                         self.physical_state = false
174                         self.object:set_properties({physical = false})
175                         return
176                 end
177                 local nn = node.name
178                 -- If node is not registered or node is walkably solid and resting on nodebox
179                 local v = self.object:getvelocity()
180                 if not core.registered_nodes[nn] or core.registered_nodes[nn].walkable and v.y == 0 then
181                         if self.physical_state then
182                                 local own_stack = ItemStack(self.object:get_luaentity().itemstring)
183                                 -- Merge with close entities of the same item
184                                 for _, object in ipairs(core.get_objects_inside_radius(p, 0.8)) do
185                                         local obj = object:get_luaentity()
186                                         if obj and obj.name == "__builtin:item"
187                                                         and obj.physical_state == false then
188                                                 if self:try_merge_with(own_stack, object, obj) then
189                                                         return
190                                                 end
191                                         end
192                                 end
193                                 self.object:setvelocity({x = 0, y = 0, z = 0})
194                                 self.object:setacceleration({x = 0, y = 0, z = 0})
195                                 self.physical_state = false
196                                 self.object:set_properties({physical = false})
197                         end
198                 else
199                         if not self.physical_state then
200                                 self.object:setvelocity({x = 0, y = 0, z = 0})
201                                 self.object:setacceleration({x = 0, y = -10, z = 0})
202                                 self.physical_state = true
203                                 self.object:set_properties({physical = true})
204                         end
205                 end
206         end,
207
208         on_punch = function(self, hitter)
209                 local inv = hitter:get_inventory()
210                 if inv and self.itemstring ~= '' then
211                         local left = inv:add_item("main", self.itemstring)
212                         if left and not left:is_empty() then
213                                 self.itemstring = left:to_string()
214                                 return
215                         end
216                 end
217                 self.itemstring = ''
218                 self.object:remove()
219         end,
220 })