forked from mtcontrib/builtin_item
Compare commits
27 Commits
Author | SHA1 | Date | |
---|---|---|---|
c4a8ed4cd3 | |||
0f81edd278 | |||
4f85670e78 | |||
66c222e93b | |||
5636a2bb94 | |||
89f02386bc | |||
14f985a62b | |||
ff43812fcf | |||
c475185ea4 | |||
e6dfd9dce8 | |||
aa1b3a7142 | |||
1c7473dc54 | |||
143ab1f8dd | |||
2cea32d4d7 | |||
19c8b65f67 | |||
bb90a38c2e | |||
bd97071dc6 | |||
72f75d0417 | |||
08935daaf4 | |||
f43e39bbdf | |||
3ba5c899cb | |||
c41e61e346 | |||
f6629da1ba | |||
06cfcc90fe | |||
13aecb026d | |||
50176917f9 | |||
64e2561c60 |
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
## Generic ignorable patterns and files
|
||||||
|
*~
|
||||||
|
.*.swp
|
||||||
|
debug.txt
|
12
.luacheckrc
Normal file
12
.luacheckrc
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
read_globals = {
|
||||||
|
minetest = {
|
||||||
|
fields = {
|
||||||
|
registered_entities = {
|
||||||
|
read_only = false,
|
||||||
|
other_fields = true
|
||||||
|
}
|
||||||
|
},
|
||||||
|
other_fields = true
|
||||||
|
},
|
||||||
|
"vector"
|
||||||
|
}
|
2
LICENSE.txt
Normal file
2
LICENSE.txt
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
MIT for the code
|
||||||
|
The original mod was licensed under WTFPL.
|
2
README.md
Normal file
2
README.md
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
For a description of this Minetest mod, see
|
||||||
|
https://forum.minetest.net/viewtopic.php?f=9&t=10271.
|
38
README.txt
38
README.txt
@ -1,38 +0,0 @@
|
|||||||
=== BUILTIN_ITEM MOD for MINETEST-C55 ===
|
|
||||||
by PilzAdam
|
|
||||||
|
|
||||||
Features:
|
|
||||||
This mod adds some new features to the builtin items:
|
|
||||||
- The items are pushed by flowing water
|
|
||||||
- The items are destroyed by lava
|
|
||||||
- The items are removed after 300 seconds or the time that is specified by
|
|
||||||
remove_items in minetest.conf (0 disables it)
|
|
||||||
|
|
||||||
How to install:
|
|
||||||
Unzip the archive an place it in minetest-base-directory/mods/minetest/
|
|
||||||
if you have a windows client or a linux run-in-place client. If you have
|
|
||||||
a linux system-wide instalation place it in ~/.minetest/mods/minetest/.
|
|
||||||
If you want to install this mod only in one world create the folder
|
|
||||||
worldmods/ in your worlddirectory.
|
|
||||||
For further information or help see:
|
|
||||||
http://wiki.minetest.com/wiki/Installing_Mods
|
|
||||||
|
|
||||||
License:
|
|
||||||
WTFPL (see below)
|
|
||||||
|
|
||||||
See also:
|
|
||||||
http://minetest.net/
|
|
||||||
|
|
||||||
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
|
|
||||||
Version 2, December 2004
|
|
||||||
|
|
||||||
Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
|
|
||||||
|
|
||||||
Everyone is permitted to copy and distribute verbatim or modified
|
|
||||||
copies of this license document, and changing it is allowed as long
|
|
||||||
as the name is changed.
|
|
||||||
|
|
||||||
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
|
|
||||||
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
|
|
||||||
|
|
||||||
0. You just DO WHAT THE FUCK YOU WANT TO.
|
|
305
init.lua
305
init.lua
@ -1,186 +1,127 @@
|
|||||||
minetest.register_entity(":__builtin:item", {
|
-- Use the movement gravity for the downwards acceleration.
|
||||||
initial_properties = {
|
-- The setting may change in-game but for simplicity we don't support this.
|
||||||
hp_max = 1,
|
local movement_gravity = tonumber(minetest.settings:get("movement_gravity"))
|
||||||
physical = true,
|
or 9.81
|
||||||
collisionbox = {-0.17,-0.17,-0.17, 0.17,0.17,0.17},
|
|
||||||
visual = "sprite",
|
|
||||||
visual_size = {x=0.5, y=0.5},
|
|
||||||
textures = {""},
|
|
||||||
spritediv = {x=1, y=1},
|
|
||||||
initial_sprite_basepos = {x=0, y=0},
|
|
||||||
is_visible = false,
|
|
||||||
timer = 0,
|
|
||||||
},
|
|
||||||
|
|
||||||
itemstring = '',
|
-- get_flow_raw determines the horizontal flow vector for a flowing liquid node,
|
||||||
physical_state = true,
|
-- or returns nothing if the flow is zero
|
||||||
|
local neighbour_offsets = {
|
||||||
set_item = function(self, itemstring)
|
{x=-1, y=0, z=0},
|
||||||
self.itemstring = itemstring
|
{x=1, y=0, z=0},
|
||||||
local stack = ItemStack(itemstring)
|
{x=0, y=0, z=-1},
|
||||||
local itemtable = stack:to_table()
|
{x=0, y=0, z=1}
|
||||||
local itemname = nil
|
}
|
||||||
if itemtable then
|
local function get_flow_raw(pos)
|
||||||
itemname = stack:to_table().name
|
-- FIXME: Do we need to treat nodes with special liquid_range differently?
|
||||||
end
|
local param2 = minetest.get_node(pos).param2
|
||||||
local item_texture = nil
|
if param2 == 15 then
|
||||||
local item_type = ""
|
-- The liquid has full height and flows downwards
|
||||||
if minetest.registered_items[itemname] then
|
return
|
||||||
item_texture = minetest.registered_items[itemname].inventory_image
|
end
|
||||||
item_type = minetest.registered_items[itemname].type
|
local flow = {x = 0, y = 0, z = 0}
|
||||||
end
|
local height = param2 % 8
|
||||||
prop = {
|
for n = 1, 4 do
|
||||||
is_visible = true,
|
local node = minetest.get_node(vector.add(pos, neighbour_offsets[n]))
|
||||||
visual = "sprite",
|
local def = minetest.registered_nodes[node.name]
|
||||||
textures = {"unknown_item.png"}
|
local height_other
|
||||||
}
|
if not def or def.walkable then
|
||||||
if item_texture and item_texture ~= "" then
|
-- A solid node, so no flow happens
|
||||||
prop.visual = "sprite"
|
height_other = height
|
||||||
prop.textures = {item_texture}
|
elseif def.liquidtype == "source" then
|
||||||
prop.visual_size = {x=0.50, y=0.50}
|
-- Assume that relevant liquid comes from this source
|
||||||
|
height_other = 8
|
||||||
|
elseif def.liquidtype == "flowing" then
|
||||||
|
-- This neighbour is also a flowing liquid
|
||||||
|
height_other = node.param2 % 8
|
||||||
else
|
else
|
||||||
prop.visual = "wielditem"
|
-- There is a free space, e.g. air or a plant
|
||||||
prop.textures = {itemname}
|
height_other = 0
|
||||||
prop.visual_size = {x=0.20, y=0.20}
|
|
||||||
prop.automatic_rotate = math.pi * 0.25
|
|
||||||
end
|
end
|
||||||
self.object:set_properties(prop)
|
local fl = vector.multiply(neighbour_offsets[n], height - height_other)
|
||||||
end,
|
flow = vector.add(flow, fl)
|
||||||
|
end
|
||||||
get_staticdata = function(self)
|
if vector.equals(flow, {x = 0, y = 0, z = 0}) then
|
||||||
--return self.itemstring
|
return
|
||||||
return minetest.serialize({
|
end
|
||||||
itemstring = self.itemstring,
|
return vector.normalize(flow)
|
||||||
always_collect = self.always_collect,
|
|
||||||
timer = self.timer,
|
|
||||||
})
|
|
||||||
end,
|
|
||||||
|
|
||||||
on_activate = function(self, staticdata, dtime_s)
|
|
||||||
if string.sub(staticdata, 1, string.len("return")) == "return" then
|
|
||||||
local data = minetest.deserialize(staticdata)
|
|
||||||
if data and type(data) == "table" then
|
|
||||||
self.itemstring = data.itemstring
|
|
||||||
self.always_collect = data.always_collect
|
|
||||||
self.timer = data.timer
|
|
||||||
if not self.timer then
|
|
||||||
self.timer = 0
|
|
||||||
end
|
|
||||||
self.timer = self.timer+dtime_s
|
|
||||||
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,
|
|
||||||
|
|
||||||
on_step = function(self, dtime)
|
|
||||||
local time = tonumber(minetest.setting_get("remove_items"))
|
|
||||||
if not time then
|
|
||||||
time = 300
|
|
||||||
end
|
|
||||||
if not self.timer then
|
|
||||||
self.timer = 0
|
|
||||||
end
|
|
||||||
self.timer = self.timer + dtime
|
|
||||||
if time ~= 0 and (self.timer > time) then
|
|
||||||
self.object:remove()
|
|
||||||
end
|
|
||||||
|
|
||||||
local p = self.object:getpos()
|
|
||||||
|
|
||||||
local name = minetest.env:get_node(p).name
|
|
||||||
if name == "default:lava_flowing" or name == "default:lava_source" then
|
|
||||||
minetest.sound_play("builtin_item_lava", {pos=self.object:getpos()})
|
|
||||||
self.object:remove()
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
if minetest.registered_nodes[name].liquidtype == "flowing" then
|
|
||||||
get_flowing_dir = function(self)
|
|
||||||
local pos = self.object:getpos()
|
|
||||||
local param2 = minetest.env:get_node(pos).param2
|
|
||||||
for i,d in ipairs({-1, 1, -1, 1}) do
|
|
||||||
if i<3 then
|
|
||||||
pos.x = pos.x+d
|
|
||||||
else
|
|
||||||
pos.z = pos.z+d
|
|
||||||
end
|
|
||||||
|
|
||||||
local name = minetest.env:get_node(pos).name
|
|
||||||
local par2 = minetest.env:get_node(pos).param2
|
|
||||||
if name == "default:water_flowing" and par2 < param2 then
|
|
||||||
return pos
|
|
||||||
end
|
|
||||||
|
|
||||||
if i<3 then
|
|
||||||
pos.x = pos.x-d
|
|
||||||
else
|
|
||||||
pos.z = pos.z-d
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
local vec = get_flowing_dir(self)
|
|
||||||
if vec then
|
|
||||||
local v = self.object:getvelocity()
|
|
||||||
if vec and vec.x-p.x > 0 then
|
|
||||||
self.object:setvelocity({x=0.5,y=v.y,z=0})
|
|
||||||
elseif vec and vec.x-p.x < 0 then
|
|
||||||
self.object:setvelocity({x=-0.5,y=v.y,z=0})
|
|
||||||
elseif vec and vec.z-p.z > 0 then
|
|
||||||
self.object:setvelocity({x=0,y=v.y,z=0.5})
|
|
||||||
elseif vec and vec.z-p.z < 0 then
|
|
||||||
self.object:setvelocity({x=0,y=v.y,z=-0.5})
|
|
||||||
end
|
|
||||||
self.object:setacceleration({x=0, y=-10, z=0})
|
|
||||||
self.physical_state = true
|
|
||||||
self.object:set_properties({
|
|
||||||
physical = true
|
|
||||||
})
|
|
||||||
return
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
p.y = p.y - 0.3
|
|
||||||
local nn = minetest.env:get_node(p).name
|
|
||||||
-- If node is not registered or node is walkably solid
|
|
||||||
if not minetest.registered_nodes[nn] or minetest.registered_nodes[nn].walkable then
|
|
||||||
if self.physical_state then
|
|
||||||
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)
|
|
||||||
if self.itemstring ~= '' then
|
|
||||||
local left = hitter:get_inventory():add_item("main", self.itemstring)
|
|
||||||
if not left:is_empty() then
|
|
||||||
self.itemstring = left:to_string()
|
|
||||||
return
|
|
||||||
end
|
|
||||||
end
|
|
||||||
self.object:remove()
|
|
||||||
end,
|
|
||||||
})
|
|
||||||
|
|
||||||
if minetest.setting_get("log_mods") then
|
|
||||||
minetest.log("action", "builtin_item loaded")
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- get_flow caches the results from get_flow_raw for 10 s
|
||||||
|
local flow_cache = {}
|
||||||
|
setmetatable(flow_cache, {__mode = "kv"})
|
||||||
|
local function get_flow(pos)
|
||||||
|
local vi = minetest.hash_node_position(pos)
|
||||||
|
local t = minetest.get_us_time()
|
||||||
|
if flow_cache[vi]
|
||||||
|
and t - flow_cache[vi][1] < 10 * 1000000 then
|
||||||
|
return flow_cache[vi][2]
|
||||||
|
end
|
||||||
|
local flow = get_flow_raw(pos)
|
||||||
|
flow_cache[vi] = {t, flow}
|
||||||
|
return flow
|
||||||
|
end
|
||||||
|
|
||||||
|
local item_entity = minetest.registered_entities["__builtin:item"]
|
||||||
|
local old_on_step = item_entity.on_step
|
||||||
|
|
||||||
|
item_entity.makes_footstep_sound = true
|
||||||
|
-- The "bt_" prefix shows that the value comes from builtin_item
|
||||||
|
item_entity.bt_timer = 0
|
||||||
|
item_entity.on_step = function(self, dtime, ...)
|
||||||
|
-- Remember the velocity before an original on_step can change it
|
||||||
|
local vel_desired
|
||||||
|
if self.bt_reset_velocity then
|
||||||
|
vel_desired = self.object:get_velocity()
|
||||||
|
end
|
||||||
|
|
||||||
|
old_on_step(self, dtime, ...)
|
||||||
|
|
||||||
|
-- Ignore the item if it should not interact with physics
|
||||||
|
if not self.physical_state then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Reset the velocity if needed
|
||||||
|
if vel_desired
|
||||||
|
and not vector.equals(self.object:get_velocity(), vel_desired) then
|
||||||
|
self.object:set_velocity(vel_desired)
|
||||||
|
end
|
||||||
|
|
||||||
|
-- For performance reasons, skip the remaining code in frequent steps
|
||||||
|
self.bt_timer = self.bt_timer + dtime
|
||||||
|
if self.bt_timer < 0.1 then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
self.bt_timer = 0
|
||||||
|
|
||||||
|
local p = self.object:get_pos()
|
||||||
|
local pos = vector.round(p)
|
||||||
|
local nodename = minetest.get_node(pos).name
|
||||||
|
|
||||||
|
if self.bt_reset_velocity then
|
||||||
|
-- Set the item acceleration to its default (changed again below)
|
||||||
|
self.object:set_acceleration({x=0, y=-movement_gravity, z=0})
|
||||||
|
self.bt_reset_velocity = nil
|
||||||
|
end
|
||||||
|
local def = minetest.registered_nodes[nodename]
|
||||||
|
if not def or not def.liquidtype or def.liquidtype ~= "flowing" then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
local flow = get_flow(pos)
|
||||||
|
if not flow then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
local vel_current = vel_desired or self.object:get_velocity()
|
||||||
|
local acc
|
||||||
|
if vector.dot(vel_current, flow) < 1.0 then
|
||||||
|
acc = vector.multiply(flow, 2.0)
|
||||||
|
else
|
||||||
|
-- The item is already accelerated by the fluids
|
||||||
|
acc = {x = 0, y = 0, z = 0}
|
||||||
|
end
|
||||||
|
acc.y = -movement_gravity
|
||||||
|
self.object:set_acceleration(acc)
|
||||||
|
self.bt_reset_velocity = true
|
||||||
|
end
|
||||||
|
|
||||||
|
minetest.register_entity(":__builtin:item", item_entity)
|
||||||
|
Binary file not shown.
Reference in New Issue
Block a user