1
0
mirror of https://github.com/HybridDog/builtin_item.git synced 2024-10-31 23:50:20 +01:00

General code cleanup

This commit is contained in:
HybridDog 2020-10-08 19:35:12 +02:00
parent ff43812fcf
commit 14f985a62b
2 changed files with 94 additions and 97 deletions

View File

@ -1,3 +1,5 @@
For a description of this Minetest mod, see For a description of this Minetest mod, see
https://forum.minetest.net/viewtopic.php?f=9&t=10271. https://forum.minetest.net/viewtopic.php?f=9&t=10271.
TODO:
* Support mods liquids

165
init.lua
View File

@ -1,85 +1,73 @@
local time = minetest.get_us_time()+10*1000000 -- Use the movement gravity for the downwards direction. Get the setting rarely
local lastpos = {x=0, y=0, z=0} local cached_gravity
local last_tab, always_test local function get_gravity()
if cached_gravity then
if not core.get_gravity then return cached_gravity
local gravity,grav_updating = 10 end
function core.get_gravity() cached_gravity = tonumber(core.settings:get("movement_gravity")) or 9.81
if not grav_updating then
gravity = tonumber(core.settings:get("movement_gravity")) or gravity
grav_updating = true
core.after(50, function() core.after(50, function()
grav_updating = false cached_gravity = nil
end) end)
end return cached_gravity
return gravity
end
end end
local function get_nodes(pos) local neighbour_offsets = {
if not always_test then {x=-1, y=0, z=0},
{x=1, y=0, z=0},
{x=0, y=0, z=-1},
{x=0, y=0, z=1}
}
local neighbours_cache = {}
setmetatable(neighbours_cache, {__mode = "kv"})
local function get_neighbour_nodes(pos)
-- Use previously found neighbours if they are not too old
local vi = minetest.hash_node_position(pos)
local t = minetest.get_us_time() local t = minetest.get_us_time()
if vector.equals(pos, lastpos) if neighbours_cache[vi]
and t-time < 10*1000000 then and t - neighbours_cache[vi][1] < 10 * 1000000 then
return last_tab return neighbours_cache[vi][2]
end end
time = t local neighbours = {}
lastpos = pos for n = 1, 4 do
local near_objects = minetest.get_objects_inside_radius(pos, 1) local p = vector.add(pos, neighbour_offsets[n])
if #near_objects >= 2 then neighbours[n] = p
always_test = true neighbours[n + 4] = minetest.get_node(p)
minetest.after(10, function() always_test = false end)
end end
end neighbours_cache[vi] = {t, neighbours}
local tab,n = {},1 return neighbours
for i = -1,1,2 do
for _,p in pairs({
{x=pos.x+i, y=pos.y, z=pos.z},
{x=pos.x, y=pos.y, z=pos.z+i}
}) do
tab[n] = {p, minetest.get_node(p)}
n = n+1
end
end
if not always_test then
last_tab = tab
end
return tab
end end
local function get_flowing_dir(pos) -- This function determines position to which the water flows
local data = get_nodes(pos) local function get_flow_target(pos)
local neighbours = get_neighbour_nodes(pos)
local param2 = minetest.get_node(pos).param2 local param2 = minetest.get_node(pos).param2
if param2 > 7 then if param2 > 7 then
return return
end end
for _,i in pairs(data) do for i = 1, 4 do
local nd = i[2] -- If a neighbour has a lower height, flow to it
local name = nd.name local node = neighbours[i + 4]
local par2 = nd.param2 if node.name == "default:water_flowing"
if name == "default:water_flowing" and node.param2 < param2 then
and par2 < param2 then return neighbours[i]
return i[1]
end end
end end
for _,i in pairs(data) do for i = 1, 4 do
local nd = i[2] -- TODO
local name = nd.name local node = neighbours[i + 4]
local par2 = nd.param2 if node.name == "default:water_flowing"
if name == "default:water_flowing" and node.param2 >= 11 then
and par2 >= 11 then return neighbours[i]
return i[1]
end end
end end
for _,i in pairs(data) do for i = 1, 4 do
local nd = i[2] -- TODO
local name = nd.name local node = neighbours[i + 4]
local par2 = nd.param2 if node.name ~= "default:water_flowing" then
local tmp = minetest.registered_nodes[name] local def = minetest.registered_nodes[node.name]
if tmp if def and not def.walkable then
and not tmp.walkable return neighbours[i]
and name ~= "default:water_flowing" then end
return i[1]
end end
end end
end end
@ -92,6 +80,11 @@ item_entity.bt_timer = 0
item_entity.on_step = function(self, dtime, ...) item_entity.on_step = function(self, dtime, ...)
old_on_step(self, dtime, ...) old_on_step(self, dtime, ...)
--~ if not self.physical_state then
--~ return
--~ end
-- Force-adjust an acceleration and/or velocity if needed
if self.bt_acc if self.bt_acc
and not vector.equals(self.object:get_acceleration(), self.bt_acc) then and not vector.equals(self.object:get_acceleration(), self.bt_acc) then
self.object:set_acceleration(self.bt_acc) self.object:set_acceleration(self.bt_acc)
@ -100,6 +93,8 @@ item_entity.on_step = function(self, dtime, ...)
and not vector.equals(self.object:get_velocity(), self.bt_vel) then and not vector.equals(self.object:get_velocity(), self.bt_vel) then
self.object:set_velocity(self.bt_vel) self.object:set_velocity(self.bt_vel)
end end
-- TODO: was ist pyhsical state?
if self.bt_phys ~= nil if self.bt_phys ~= nil
and self.physical_state ~= self.bt_phys then and self.physical_state ~= self.bt_phys then
self.physical_state = self.bt_phys self.physical_state = self.bt_phys
@ -108,7 +103,8 @@ item_entity.on_step = function(self, dtime, ...)
}) })
end end
self.bt_timer = self.bt_timer+dtime -- For performance reasons, skip the remaining code except every second
self.bt_timer = self.bt_timer + dtime
if self.bt_timer < 1 then if self.bt_timer < 1 then
return return
end end
@ -119,7 +115,8 @@ item_entity.on_step = function(self, dtime, ...)
local name = minetest.get_node(pos).name local name = minetest.get_node(pos).name
if name == "default:lava_flowing" if name == "default:lava_flowing"
or name == "default:lava_source" then -- update to newest default please or name == "default:lava_source" then
-- TODO: more generic burn cases
minetest.sound_play("builtin_item_lava", {pos=p}) minetest.sound_play("builtin_item_lava", {pos=p})
minetest.add_particlespawner({ minetest.add_particlespawner({
amount = 3, amount = 3,
@ -147,21 +144,27 @@ item_entity.on_step = function(self, dtime, ...)
return return
end end
local tmp = minetest.registered_nodes[name] local def = minetest.registered_nodes[name]
if not tmp then if not def then
return return
end end
local acc -- Adjust the acceleration in liquid nodes
if tmp.liquidtype then self.bt_acc = nil
acc = {x=0, y=core.get_gravity()*(((p.y-.5)%1)*.9-1), z=0} self.bt_vel = nil
if def.liquidtype then
-- Set the strongest acceleration when we are in the middle of the node
local acc_strength = 1.0 - ((p.y - 0.5) % 1.0) * 0.9
local acc = {x = 0, y = -acc_strength * get_gravity(), z = 0}
self.object:set_acceleration(acc) self.object:set_acceleration(acc)
self.bt_acc = acc self.bt_acc = acc
else
self.bt_acc = nil
end end
if tmp.liquidtype == "flowing" then if def.liquidtype ~= "flowing" then
local vec = get_flowing_dir(pos) return
if vec then end
local vec = get_flow_target(pos)
if not vec then
return
end
local v = vector.add( local v = vector.add(
self.object:get_velocity(), self.object:get_velocity(),
vector.multiply(vector.subtract(vec, pos),.5) vector.multiply(vector.subtract(vec, pos),.5)
@ -173,14 +176,6 @@ item_entity.on_step = function(self, dtime, ...)
self.object:set_properties({ self.object:set_properties({
physical = true physical = true
}) })
return
end
end
self.bt_vel = nil
end end
minetest.register_entity(":__builtin:item", item_entity) minetest.register_entity(":__builtin:item", item_entity)
if minetest.settings:get("log_mods") then
minetest.log("action", "builtin_item loaded")
end