Add formspec escaping to subgame list in create world dialog (#5808)
[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.settings: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         },
35
36         itemstring = '',
37         physical_state = true,
38         age = 0,
39
40         set_item = function(self, itemstring)
41                 self.itemstring = itemstring
42                 local stack = ItemStack(itemstring)
43                 local count = stack:get_count()
44                 local max_count = stack:get_stack_max()
45                 if count > max_count then
46                         count = max_count
47                         self.itemstring = stack:get_name().." "..max_count
48                 end
49                 local s = 0.2 + 0.1 * (count / max_count)
50                 local c = s
51                 local itemtable = stack:to_table()
52                 local itemname = nil
53                 if itemtable then
54                         itemname = stack:to_table().name
55                 end
56                 -- Backwards compatibility: old clients use the texture
57                 -- to get the type of the item
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                 end
64                 local prop = {
65                         is_visible = true,
66                         visual = "wielditem",
67                         textures = {itemname},
68                         visual_size = {x = s, y = s},
69                         collisionbox = {-c, -c, -c, c, c, c},
70                         automatic_rotate = math.pi * 0.5,
71                         wield_item = itemstring,
72                 }
73                 self.object:set_properties(prop)
74         end,
75
76         get_staticdata = function(self)
77                 return core.serialize({
78                         itemstring = self.itemstring,
79                         always_collect = self.always_collect,
80                         age = self.age,
81                         dropped_by = self.dropped_by
82                 })
83         end,
84
85         on_activate = function(self, staticdata, dtime_s)
86                 if string.sub(staticdata, 1, string.len("return")) == "return" then
87                         local data = core.deserialize(staticdata)
88                         if data and type(data) == "table" then
89                                 self.itemstring = data.itemstring
90                                 self.always_collect = data.always_collect
91                                 if data.age then
92                                         self.age = data.age + dtime_s
93                                 else
94                                         self.age = dtime_s
95                                 end
96                                 self.dropped_by = data.dropped_by
97                         end
98                 else
99                         self.itemstring = staticdata
100                 end
101                 self.object:set_armor_groups({immortal = 1})
102                 self.object:setvelocity({x = 0, y = 2, z = 0})
103                 self.object:setacceleration({x = 0, y = -10, z = 0})
104                 self:set_item(self.itemstring)
105         end,
106
107   -- moves items from this stack to an other stack
108         try_merge_with = function(self, own_stack, object, obj)
109           -- other item's stack
110                 local stack = ItemStack(obj.itemstring)
111                 -- only merge if items are the same
112                 if own_stack:get_name() == stack:get_name() and
113                                 own_stack:get_meta() == stack:get_meta() and
114                                 own_stack:get_wear() == stack:get_wear() and
115                                 stack:get_free_space() > 0 then
116                         local overflow = false
117                         local count = stack:get_count() + own_stack:get_count()
118                         local max_count = stack:get_stack_max()
119                         if count > max_count then
120                                 overflow = true
121                                 stack:set_count(max_count)
122                                 count = count - max_count
123                                 own_stack:set_count(count)
124                         else
125                                 self.itemstring = ''
126                                 stack:set_count(count)
127                         end
128                         local pos = object:getpos()
129                         pos.y = pos.y + (count - stack:get_count()) / max_count * 0.15
130                         object:moveto(pos, false)
131                         local s, c
132                         if not overflow then
133                                 obj.itemstring = stack:to_string()
134                                 s = 0.2 + 0.1 * (count / max_count)
135                                 c = s
136                                 object:set_properties({
137                                         visual_size = {x = s, y = s},
138                                         collisionbox = {-c, -c, -c, c, c, c},
139                                         wield_item = obj.itemstring
140                                 })
141                                 self.object:remove()
142                                 -- merging succeeded
143                                 return true
144                         else
145                                 s = 0.4
146                                 c = 0.3
147                                 obj.itemstring = stack:to_string()
148                                 object:set_properties({
149                                         visual_size = {x = s, y = s},
150                                         collisionbox = {-c, -c, -c, c, c, c},
151                                         wield_item = obj.itemstring
152                                 })
153                                 s = 0.2 + 0.1 * (count / max_count)
154                                 c = s
155                                 self.itemstring = own_stack:to_string()
156                                 self.object:set_properties({
157                                         visual_size = {x = s, y = s},
158                                         collisionbox = {-c, -c, -c, c, c, c},
159                                         wield_item = self.itemstring
160                                 })
161                         end
162                 end
163                 -- merging didn't succeed
164                 return false
165         end,
166
167         on_step = function(self, dtime)
168                 self.age = self.age + dtime
169                 if time_to_live > 0 and self.age > time_to_live then
170                         self.itemstring = ''
171                         self.object:remove()
172                         return
173                 end
174                 local p = self.object:getpos()
175                 p.y = p.y - 0.5
176                 local node = core.get_node_or_nil(p)
177                 local in_unloaded = (node == nil)
178                 if in_unloaded then
179                         -- Don't infinetly fall into unloaded map
180                         self.object:setvelocity({x = 0, y = 0, z = 0})
181                         self.object:setacceleration({x = 0, y = 0, z = 0})
182                         self.physical_state = false
183                         self.object:set_properties({physical = false})
184                         return
185                 end
186                 local nn = node.name
187                 -- If node is not registered or node is walkably solid and resting on nodebox
188                 local v = self.object:getvelocity()
189                 if not core.registered_nodes[nn] or core.registered_nodes[nn].walkable and v.y == 0 then
190                         if self.physical_state then
191                                 local own_stack = ItemStack(self.object:get_luaentity().itemstring)
192                                 -- Merge with close entities of the same item
193                                 for _, object in ipairs(core.get_objects_inside_radius(p, 0.8)) do
194                                         local obj = object:get_luaentity()
195                                         if obj and obj.name == "__builtin:item"
196                                                         and obj.physical_state == false then
197                                                 if self:try_merge_with(own_stack, object, obj) then
198                                                         return
199                                                 end
200                                         end
201                                 end
202                                 self.object:setvelocity({x = 0, y = 0, z = 0})
203                                 self.object:setacceleration({x = 0, y = 0, z = 0})
204                                 self.physical_state = false
205                                 self.object:set_properties({physical = false})
206                         end
207                 else
208                         if not self.physical_state then
209                                 self.object:setvelocity({x = 0, y = 0, z = 0})
210                                 self.object:setacceleration({x = 0, y = -10, z = 0})
211                                 self.physical_state = true
212                                 self.object:set_properties({physical = true})
213                         end
214                 end
215         end,
216
217         on_punch = function(self, hitter)
218                 local inv = hitter:get_inventory()
219                 if inv and self.itemstring ~= '' then
220                         local left = inv:add_item("main", self.itemstring)
221                         if left and not left:is_empty() then
222                                 self.itemstring = left:to_string()
223                                 return
224                         end
225                 end
226                 self.itemstring = ''
227                 self.object:remove()
228         end,
229 })