item_entity.lua 6.38 KB
Newer Older
1 2 3 4 5 6
-- Minetest: builtin/item_entity.lua

function core.spawn_item(pos, item)
	-- Take item in any format
	local stack = ItemStack(item)
	local obj = core.add_entity(pos, "__builtin:item")
7 8 9 10
	-- Don't use obj if it couldn't be added to the map.
	if obj then
		obj:get_luaentity():set_item(stack:to_string())
	end
11 12 13
	return obj
end

14
-- If item_entity_ttl is not set, enity will have default life time
15 16
-- Setting it to -1 disables the feature

17
local time_to_live = tonumber(core.settings:get("item_entity_ttl"))
18 19 20 21 22 23 24 25 26
if not time_to_live then
	time_to_live = 900
end

core.register_entity(":__builtin:item", {
	initial_properties = {
		hp_max = 1,
		physical = true,
		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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
		textures = {""},
		spritediv = {x = 1, y = 1},
		initial_sprite_basepos = {x = 0, y = 0},
		is_visible = false,
	},

	itemstring = '',
	physical_state = true,
	age = 0,

	set_item = function(self, itemstring)
		self.itemstring = itemstring
		local stack = ItemStack(itemstring)
		local count = stack:get_count()
		local max_count = stack:get_stack_max()
		if count > max_count then
			count = max_count
			self.itemstring = stack:get_name().." "..max_count
		end
49 50
		local s = 0.2 + 0.1 * (count / max_count)
		local c = s
51 52 53 54 55
		local itemtable = stack:to_table()
		local itemname = nil
		if itemtable then
			itemname = stack:to_table().name
		end
56 57
		-- Backwards compatibility: old clients use the texture
		-- to get the type of the item
58 59 60 61 62 63
		local item_texture = nil
		local item_type = ""
		if core.registered_items[itemname] then
			item_texture = core.registered_items[itemname].inventory_image
			item_type = core.registered_items[itemname].type
		end
64
		local prop = {
65 66 67 68 69
			is_visible = true,
			visual = "wielditem",
			textures = {itemname},
			visual_size = {x = s, y = s},
			collisionbox = {-c, -c, -c, c, c, c},
70
			automatic_rotate = math.pi * 0.5,
71
			wield_item = itemstring,
72 73 74 75 76 77 78 79
		}
		self.object:set_properties(prop)
	end,

	get_staticdata = function(self)
		return core.serialize({
			itemstring = self.itemstring,
			always_collect = self.always_collect,
80 81
			age = self.age,
			dropped_by = self.dropped_by
82 83 84 85 86 87 88 89 90
		})
	end,

	on_activate = function(self, staticdata, dtime_s)
		if string.sub(staticdata, 1, string.len("return")) == "return" then
			local data = core.deserialize(staticdata)
			if data and type(data) == "table" then
				self.itemstring = data.itemstring
				self.always_collect = data.always_collect
91
				if data.age then
92 93 94 95
					self.age = data.age + dtime_s
				else
					self.age = dtime_s
				end
96
				self.dropped_by = data.dropped_by
97 98 99 100 101 102 103 104 105 106
			end
		else
			self.itemstring = staticdata
		end
		self.object:set_armor_groups({immortal = 1})
		self.object:setvelocity({x = 0, y = 2, z = 0})
		self.object:setacceleration({x = 0, y = -10, z = 0})
		self:set_item(self.itemstring)
	end,

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 112 113 114 115
		-- only merge if items are the same
		if own_stack:get_name() == stack:get_name() and
				own_stack:get_meta() == stack:get_meta() and
				own_stack:get_wear() == stack:get_wear() and
				stack:get_free_space() > 0 then
116 117 118 119 120
			local overflow = false
			local count = stack:get_count() + own_stack:get_count()
			local max_count = stack:get_stack_max()
			if count > max_count then
				overflow = true
121
				stack:set_count(max_count)
122
				count = count - max_count
123
				own_stack:set_count(count)
124 125
			else
				self.itemstring = ''
126
				stack:set_count(count)
127 128 129 130 131 132
			end
			local pos = object:getpos()
			pos.y = pos.y + (count - stack:get_count()) / max_count * 0.15
			object:moveto(pos, false)
			local s, c
			if not overflow then
133
				obj.itemstring = stack:to_string()
134 135 136 137
				s = 0.2 + 0.1 * (count / max_count)
				c = s
				object:set_properties({
					visual_size = {x = s, y = s},
138 139
					collisionbox = {-c, -c, -c, c, c, c},
					wield_item = obj.itemstring
140 141 142 143 144 145 146
				})
				self.object:remove()
				-- merging succeeded
				return true
			else
				s = 0.4
				c = 0.3
147
				obj.itemstring = stack:to_string()
148 149
				object:set_properties({
					visual_size = {x = s, y = s},
150 151
					collisionbox = {-c, -c, -c, c, c, c},
					wield_item = obj.itemstring
152 153 154
				})
				s = 0.2 + 0.1 * (count / max_count)
				c = s
155
				self.itemstring = own_stack:to_string()
156 157
				self.object:set_properties({
					visual_size = {x = s, y = s},
158 159
					collisionbox = {-c, -c, -c, c, c, c},
					wield_item = self.itemstring
160 161 162 163 164 165 166
				})
			end
		end
		-- merging didn't succeed
		return false
	end,

167 168 169 170 171 172 173 174
	on_step = function(self, dtime)
		self.age = self.age + dtime
		if time_to_live > 0 and self.age > time_to_live then
			self.itemstring = ''
			self.object:remove()
			return
		end
		local p = self.object:getpos()
175
		p.y = p.y - 0.5
176
		local node = core.get_node_or_nil(p)
177 178 179 180
		-- Delete in 'ignore' nodes
		if node and node.name == "ignore" then
			self.itemstring = ""
			self.object:remove()
181 182
			return
		end
183 184 185

		-- If node is nil (unloaded area), or node is not registered, or node is
		-- walkably solid and item is resting on nodebox
186
		local v = self.object:getvelocity()
187 188
		if not node or not core.registered_nodes[node.name] or
				core.registered_nodes[node.name].walkable and v.y == 0 then
189 190
			if self.physical_state then
				local own_stack = ItemStack(self.object:get_luaentity().itemstring)
191 192
				-- Merge with close entities of the same item
				for _, object in ipairs(core.get_objects_inside_radius(p, 0.8)) do
193
					local obj = object:get_luaentity()
194 195 196 197
					if obj and obj.name == "__builtin:item"
							and obj.physical_state == false then
						if self:try_merge_with(own_stack, object, obj) then
							return
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
						end
					end
				end
				self.object:setvelocity({x = 0, y = 0, z = 0})
				self.object:setacceleration({x = 0, y = 0, z = 0})
				self.physical_state = false
				self.object:set_properties({physical = false})
			end
		else
			if not self.physical_state then
				self.object:setvelocity({x = 0, y = 0, z = 0})
				self.object:setacceleration({x = 0, y = -10, z = 0})
				self.physical_state = true
				self.object:set_properties({physical = true})
			end
		end
	end,

	on_punch = function(self, hitter)
217 218 219 220
		local inv = hitter:get_inventory()
		if inv and self.itemstring ~= '' then
			local left = inv:add_item("main", self.itemstring)
			if left and not left:is_empty() then
221 222 223 224 225 226 227 228
				self.itemstring = left:to_string()
				return
			end
		end
		self.itemstring = ''
		self.object:remove()
	end,
})