]> git.lizzy.rs Git - dragonfireclient.git/blob - builtin/game/item_entity.lua
Add search to advanced settings (#4806)
[dragonfireclient.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         },
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                 local item_texture = nil
57                 local item_type = ""
58                 if core.registered_items[itemname] then
59                         item_texture = core.registered_items[itemname].inventory_image
60                         item_type = core.registered_items[itemname].type
61                 end
62                 local prop = {
63                         is_visible = true,
64                         visual = "wielditem",
65                         textures = {itemname},
66                         visual_size = {x = s, y = s},
67                         collisionbox = {-c, -c, -c, c, c, c},
68                         automatic_rotate = math.pi * 0.5,
69                 }
70                 self.object:set_properties(prop)
71         end,
72
73         get_staticdata = function(self)
74                 return core.serialize({
75                         itemstring = self.itemstring,
76                         always_collect = self.always_collect,
77                         age = self.age,
78                         dropped_by = self.dropped_by
79                 })
80         end,
81
82         on_activate = function(self, staticdata, dtime_s)
83                 if string.sub(staticdata, 1, string.len("return")) == "return" then
84                         local data = core.deserialize(staticdata)
85                         if data and type(data) == "table" then
86                                 self.itemstring = data.itemstring
87                                 self.always_collect = data.always_collect
88                                 if data.age then
89                                         self.age = data.age + dtime_s
90                                 else
91                                         self.age = dtime_s
92                                 end
93                                 self.dropped_by = data.dropped_by
94                         end
95                 else
96                         self.itemstring = staticdata
97                 end
98                 self.object:set_armor_groups({immortal = 1})
99                 self.object:setvelocity({x = 0, y = 2, z = 0})
100                 self.object:setacceleration({x = 0, y = -10, z = 0})
101                 self:set_item(self.itemstring)
102         end,
103
104         try_merge_with = function(self, own_stack, object, obj)
105                 local stack = ItemStack(obj.itemstring)
106                 if own_stack:get_name() == stack:get_name() and stack:get_free_space() > 0 then
107                         local overflow = false
108                         local count = stack:get_count() + own_stack:get_count()
109                         local max_count = stack:get_stack_max()
110                         if count > max_count then
111                                 overflow = true
112                                 count = count - max_count
113                         else
114                                 self.itemstring = ''
115                         end
116                         local pos = object:getpos()
117                         pos.y = pos.y + (count - stack:get_count()) / max_count * 0.15
118                         object:moveto(pos, false)
119                         local s, c
120                         local max_count = stack:get_stack_max()
121                         local name = stack:get_name()
122                         if not overflow then
123                                 obj.itemstring = name .. " " .. count
124                                 s = 0.2 + 0.1 * (count / max_count)
125                                 c = s
126                                 object:set_properties({
127                                         visual_size = {x = s, y = s},
128                                         collisionbox = {-c, -c, -c, c, c, c}
129                                 })
130                                 self.object:remove()
131                                 -- merging succeeded
132                                 return true
133                         else
134                                 s = 0.4
135                                 c = 0.3
136                                 object:set_properties({
137                                         visual_size = {x = s, y = s},
138                                         collisionbox = {-c, -c, -c, c, c, c}
139                                 })
140                                 obj.itemstring = name .. " " .. max_count
141                                 s = 0.2 + 0.1 * (count / max_count)
142                                 c = s
143                                 self.object:set_properties({
144                                         visual_size = {x = s, y = s},
145                                         collisionbox = {-c, -c, -c, c, c, c}
146                                 })
147                                 self.itemstring = name .. " " .. count
148                         end
149                 end
150                 -- merging didn't succeed
151                 return false
152         end,
153
154         on_step = function(self, dtime)
155                 self.age = self.age + dtime
156                 if time_to_live > 0 and self.age > time_to_live then
157                         self.itemstring = ''
158                         self.object:remove()
159                         return
160                 end
161                 local p = self.object:getpos()
162                 p.y = p.y - 0.5
163                 local node = core.get_node_or_nil(p)
164                 local in_unloaded = (node == nil)
165                 if in_unloaded then
166                         -- Don't infinetly fall into unloaded map
167                         self.object:setvelocity({x = 0, y = 0, z = 0})
168                         self.object:setacceleration({x = 0, y = 0, z = 0})
169                         self.physical_state = false
170                         self.object:set_properties({physical = false})
171                         return
172                 end
173                 local nn = node.name
174                 -- If node is not registered or node is walkably solid and resting on nodebox
175                 local v = self.object:getvelocity()
176                 if not core.registered_nodes[nn] or core.registered_nodes[nn].walkable and v.y == 0 then
177                         if self.physical_state then
178                                 local own_stack = ItemStack(self.object:get_luaentity().itemstring)
179                                 -- Merge with close entities of the same item
180                                 for _, object in ipairs(core.get_objects_inside_radius(p, 0.8)) do
181                                         local obj = object:get_luaentity()
182                                         if obj and obj.name == "__builtin:item"
183                                                         and obj.physical_state == false then
184                                                 if self:try_merge_with(own_stack, object, obj) then
185                                                         return
186                                                 end
187                                         end
188                                 end
189                                 self.object:setvelocity({x = 0, y = 0, z = 0})
190                                 self.object:setacceleration({x = 0, y = 0, z = 0})
191                                 self.physical_state = false
192                                 self.object:set_properties({physical = false})
193                         end
194                 else
195                         if not self.physical_state then
196                                 self.object:setvelocity({x = 0, y = 0, z = 0})
197                                 self.object:setacceleration({x = 0, y = -10, z = 0})
198                                 self.physical_state = true
199                                 self.object:set_properties({physical = true})
200                         end
201                 end
202         end,
203
204         on_punch = function(self, hitter)
205                 local inv = hitter:get_inventory()
206                 if inv and self.itemstring ~= '' then
207                         local left = inv:add_item("main", self.itemstring)
208                         if left and not left:is_empty() then
209                                 self.itemstring = left:to_string()
210                                 return
211                         end
212                 end
213                 self.itemstring = ''
214                 self.object:remove()
215         end,
216 })