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:
parent
ff43812fcf
commit
14f985a62b
|
@ -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
|
||||||
|
|
189
init.lua
189
init.lua
|
@ -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
|
|
||||||
function core.get_gravity()
|
|
||||||
if not grav_updating then
|
|
||||||
gravity = tonumber(core.settings:get("movement_gravity")) or gravity
|
|
||||||
grav_updating = true
|
|
||||||
core.after(50, function()
|
|
||||||
grav_updating = false
|
|
||||||
end)
|
|
||||||
end
|
|
||||||
return gravity
|
|
||||||
end
|
end
|
||||||
|
cached_gravity = tonumber(core.settings:get("movement_gravity")) or 9.81
|
||||||
|
core.after(50, function()
|
||||||
|
cached_gravity = nil
|
||||||
|
end)
|
||||||
|
return cached_gravity
|
||||||
end
|
end
|
||||||
|
|
||||||
local function get_nodes(pos)
|
local neighbour_offsets = {
|
||||||
if not always_test then
|
{x=-1, y=0, z=0},
|
||||||
local t = minetest.get_us_time()
|
{x=1, y=0, z=0},
|
||||||
if vector.equals(pos, lastpos)
|
{x=0, y=0, z=-1},
|
||||||
and t-time < 10*1000000 then
|
{x=0, y=0, z=1}
|
||||||
return last_tab
|
}
|
||||||
end
|
local neighbours_cache = {}
|
||||||
time = t
|
setmetatable(neighbours_cache, {__mode = "kv"})
|
||||||
lastpos = pos
|
local function get_neighbour_nodes(pos)
|
||||||
local near_objects = minetest.get_objects_inside_radius(pos, 1)
|
-- Use previously found neighbours if they are not too old
|
||||||
if #near_objects >= 2 then
|
local vi = minetest.hash_node_position(pos)
|
||||||
always_test = true
|
local t = minetest.get_us_time()
|
||||||
minetest.after(10, function() always_test = false end)
|
if neighbours_cache[vi]
|
||||||
end
|
and t - neighbours_cache[vi][1] < 10 * 1000000 then
|
||||||
|
return neighbours_cache[vi][2]
|
||||||
end
|
end
|
||||||
local tab,n = {},1
|
local neighbours = {}
|
||||||
for i = -1,1,2 do
|
for n = 1, 4 do
|
||||||
for _,p in pairs({
|
local p = vector.add(pos, neighbour_offsets[n])
|
||||||
{x=pos.x+i, y=pos.y, z=pos.z},
|
neighbours[n] = p
|
||||||
{x=pos.x, y=pos.y, z=pos.z+i}
|
neighbours[n + 4] = minetest.get_node(p)
|
||||||
}) do
|
|
||||||
tab[n] = {p, minetest.get_node(p)}
|
|
||||||
n = n+1
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
if not always_test then
|
neighbours_cache[vi] = {t, neighbours}
|
||||||
last_tab = tab
|
return neighbours
|
||||||
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,40 +144,38 @@ 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
|
|
||||||
local v = vector.add(
|
|
||||||
self.object:get_velocity(),
|
|
||||||
vector.multiply(vector.subtract(vec, pos),.5)
|
|
||||||
)
|
|
||||||
self.bt_vel = v
|
|
||||||
self.object:set_velocity(v)
|
|
||||||
self.physical_state = true
|
|
||||||
self.bt_phys = true
|
|
||||||
self.object:set_properties({
|
|
||||||
physical = true
|
|
||||||
})
|
|
||||||
return
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
self.bt_vel = nil
|
local vec = get_flow_target(pos)
|
||||||
|
if not vec then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
local v = vector.add(
|
||||||
|
self.object:get_velocity(),
|
||||||
|
vector.multiply(vector.subtract(vec, pos),.5)
|
||||||
|
)
|
||||||
|
self.bt_vel = v
|
||||||
|
self.object:set_velocity(v)
|
||||||
|
self.physical_state = true
|
||||||
|
self.bt_phys = true
|
||||||
|
self.object:set_properties({
|
||||||
|
physical = true
|
||||||
|
})
|
||||||
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
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user