mirror of
https://github.com/SmallJoker/boost_cart.git
synced 2024-11-23 23:23:44 +01:00
Fix rail counting in on_rail_step, localize API functions for faster access
This commit is contained in:
parent
ad93d338b3
commit
5502952bbd
@ -1,17 +1,17 @@
|
|||||||
|
|
||||||
local HAVE_MESECONS_ENABLED = minetest.global_exists("mesecon")
|
local HAVE_MESECONS_ENABLED = minetest.global_exists("mesecon")
|
||||||
|
|
||||||
function boost_cart:on_rail_step(pos)
|
function boost_cart:on_rail_step(entity, pos, distance)
|
||||||
-- Play rail sound
|
-- Play rail sound
|
||||||
if self.sound_counter <= 0 then
|
if entity.sound_counter <= 0 then
|
||||||
minetest.sound_play("cart_rail", {
|
minetest.sound_play("cart_rail", {
|
||||||
pos = pos,
|
pos = pos,
|
||||||
max_hear_distance = 40,
|
max_hear_distance = 40,
|
||||||
gain = 0.5
|
gain = 0.5
|
||||||
})
|
})
|
||||||
self.sound_counter = math.random(4, 15)
|
entity.sound_counter = math.random(4, 15)
|
||||||
end
|
end
|
||||||
self.sound_counter = self.sound_counter - 1
|
entity.sound_counter = entity.sound_counter - distance
|
||||||
|
|
||||||
if HAVE_MESECONS_ENABLED then
|
if HAVE_MESECONS_ENABLED then
|
||||||
boost_cart:signal_detector_rail(pos)
|
boost_cart:signal_detector_rail(pos)
|
||||||
@ -65,7 +65,7 @@ function cart_entity:on_activate(staticdata, dtime_s)
|
|||||||
return
|
return
|
||||||
end
|
end
|
||||||
local data = minetest.deserialize(staticdata)
|
local data = minetest.deserialize(staticdata)
|
||||||
if not data or type(data) ~= "table" then
|
if type(data) ~= "table" then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
self.railtype = data.railtype
|
self.railtype = data.railtype
|
||||||
@ -149,6 +149,7 @@ function cart_entity:on_punch(puncher, time_from_last_punch, tool_capabilities,
|
|||||||
self.punched = true
|
self.punched = true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local v3_len = vector.length
|
||||||
function cart_entity:on_step(dtime)
|
function cart_entity:on_step(dtime)
|
||||||
local vel = self.object:get_velocity()
|
local vel = self.object:get_velocity()
|
||||||
if self.punched then
|
if self.punched then
|
||||||
@ -161,10 +162,10 @@ function cart_entity:on_step(dtime)
|
|||||||
|
|
||||||
local pos = self.object:get_pos()
|
local pos = self.object:get_pos()
|
||||||
local cart_dir = boost_cart:velocity_to_dir(vel)
|
local cart_dir = boost_cart:velocity_to_dir(vel)
|
||||||
|
local same_dir = vector.equals(cart_dir, self.old_dir)
|
||||||
local update = {}
|
local update = {}
|
||||||
|
|
||||||
if self.old_pos and not self.punched
|
if self.old_pos and not self.punched and same_dir then
|
||||||
and vector.equals(cart_dir, self.old_dir) then
|
|
||||||
local flo_pos = vector.round(pos)
|
local flo_pos = vector.round(pos)
|
||||||
local flo_old = vector.round(self.old_pos)
|
local flo_old = vector.round(self.old_pos)
|
||||||
if vector.equals(flo_pos, flo_old) then
|
if vector.equals(flo_pos, flo_old) then
|
||||||
@ -174,6 +175,7 @@ function cart_entity:on_step(dtime)
|
|||||||
end
|
end
|
||||||
|
|
||||||
local ctrl, player
|
local ctrl, player
|
||||||
|
local distance = 1
|
||||||
|
|
||||||
-- Get player controls
|
-- Get player controls
|
||||||
if self.driver then
|
if self.driver then
|
||||||
@ -184,12 +186,11 @@ function cart_entity:on_step(dtime)
|
|||||||
end
|
end
|
||||||
|
|
||||||
local stop_wiggle = false
|
local stop_wiggle = false
|
||||||
if self.old_pos and vector.equals(cart_dir, self.old_dir) then
|
if self.old_pos and same_dir then
|
||||||
-- Detection for "skipping" nodes (perhaps use average dtime?)
|
-- Detection for "skipping" nodes (perhaps use average dtime?)
|
||||||
-- It's sophisticated enough to take the acceleration in account
|
-- It's sophisticated enough to take the acceleration in account
|
||||||
local acc = self.object:get_acceleration()
|
local acc = self.object:get_acceleration()
|
||||||
local distance = dtime * (vector.length(vel) +
|
distance = dtime * (v3_len(vel) + 0.5 * dtime * v3_len(acc))
|
||||||
0.5 * dtime * vector.length(acc))
|
|
||||||
|
|
||||||
local new_pos, new_dir = boost_cart:pathfinder(
|
local new_pos, new_dir = boost_cart:pathfinder(
|
||||||
pos, self.old_pos, self.old_dir, distance, ctrl,
|
pos, self.old_pos, self.old_dir, distance, ctrl,
|
||||||
@ -230,7 +231,7 @@ function cart_entity:on_step(dtime)
|
|||||||
else
|
else
|
||||||
-- Direction change detected
|
-- Direction change detected
|
||||||
if not vector.equals(dir, self.old_dir) then
|
if not vector.equals(dir, self.old_dir) then
|
||||||
vel = vector.multiply(dir, vector.length(vel))
|
vel = vector.multiply(dir, v3_len(vel))
|
||||||
update.vel = true
|
update.vel = true
|
||||||
if dir.y ~= self.old_dir.y then
|
if dir.y ~= self.old_dir.y then
|
||||||
pos = vector.round(pos)
|
pos = vector.round(pos)
|
||||||
@ -293,7 +294,6 @@ function cart_entity:on_step(dtime)
|
|||||||
|
|
||||||
new_acc = vector.multiply(dir, acc)
|
new_acc = vector.multiply(dir, acc)
|
||||||
end
|
end
|
||||||
boost_cart.on_rail_step(self, vector.round(pos))
|
|
||||||
|
|
||||||
-- Limits
|
-- Limits
|
||||||
local max_vel = boost_cart.speed_max
|
local max_vel = boost_cart.speed_max
|
||||||
@ -312,6 +312,7 @@ function cart_entity:on_step(dtime)
|
|||||||
end
|
end
|
||||||
self.old_switch = switch_keys
|
self.old_switch = switch_keys
|
||||||
|
|
||||||
|
boost_cart:on_rail_step(self, self.old_pos, distance)
|
||||||
|
|
||||||
if self.punched then
|
if self.punched then
|
||||||
-- Collect dropped items
|
-- Collect dropped items
|
||||||
|
@ -30,14 +30,16 @@ end
|
|||||||
|
|
||||||
function boost_cart:velocity_to_dir(v)
|
function boost_cart:velocity_to_dir(v)
|
||||||
if math.abs(v.x) > math.abs(v.z) then
|
if math.abs(v.x) > math.abs(v.z) then
|
||||||
return {x=boost_cart:get_sign(v.x), y=boost_cart:get_sign(v.y), z=0}
|
return {x=self:get_sign(v.x), y=self:get_sign(v.y), z=0}
|
||||||
else
|
else
|
||||||
return {x=0, y=boost_cart:get_sign(v.y), z=boost_cart:get_sign(v.z)}
|
return {x=0, y=self:get_sign(v.y), z=self:get_sign(v.z)}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local get_node = minetest.get_node
|
||||||
|
local get_item_group = minetest.get_item_group
|
||||||
function boost_cart:is_rail(pos, railtype)
|
function boost_cart:is_rail(pos, railtype)
|
||||||
local node = minetest.get_node(pos).name
|
local node = get_node(pos).name
|
||||||
if node == "ignore" then
|
if node == "ignore" then
|
||||||
local vm = minetest.get_voxel_manip()
|
local vm = minetest.get_voxel_manip()
|
||||||
local emin, emax = vm:read_from_map(pos, pos)
|
local emin, emax = vm:read_from_map(pos, pos)
|
||||||
@ -49,13 +51,13 @@ function boost_cart:is_rail(pos, railtype)
|
|||||||
local vi = area:indexp(pos)
|
local vi = area:indexp(pos)
|
||||||
node = minetest.get_name_from_content_id(data[vi])
|
node = minetest.get_name_from_content_id(data[vi])
|
||||||
end
|
end
|
||||||
if minetest.get_item_group(node, "rail") == 0 then
|
if get_item_group(node, "rail") == 0 then
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
if not railtype then
|
if not railtype then
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
return minetest.get_item_group(node, "connect_to_raillike") == railtype
|
return get_item_group(node, "connect_to_raillike") == railtype
|
||||||
end
|
end
|
||||||
|
|
||||||
function boost_cart:check_front_up_down(pos, dir_, check_up, railtype)
|
function boost_cart:check_front_up_down(pos, dir_, check_up, railtype)
|
||||||
@ -65,21 +67,21 @@ function boost_cart:check_front_up_down(pos, dir_, check_up, railtype)
|
|||||||
-- Front
|
-- Front
|
||||||
dir.y = 0
|
dir.y = 0
|
||||||
cur = vector.add(pos, dir)
|
cur = vector.add(pos, dir)
|
||||||
if boost_cart:is_rail(cur, railtype) then
|
if self:is_rail(cur, railtype) then
|
||||||
return dir
|
return dir
|
||||||
end
|
end
|
||||||
-- Up
|
-- Up
|
||||||
if check_up then
|
if check_up then
|
||||||
dir.y = 1
|
dir.y = 1
|
||||||
cur = vector.add(pos, dir)
|
cur = vector.add(pos, dir)
|
||||||
if boost_cart:is_rail(cur, railtype) then
|
if self:is_rail(cur, railtype) then
|
||||||
return dir
|
return dir
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
-- Down
|
-- Down
|
||||||
dir.y = -1
|
dir.y = -1
|
||||||
cur = vector.add(pos, dir)
|
cur = vector.add(pos, dir)
|
||||||
if boost_cart:is_rail(cur, railtype) then
|
if self:is_rail(cur, railtype) then
|
||||||
return dir
|
return dir
|
||||||
end
|
end
|
||||||
return nil
|
return nil
|
||||||
@ -108,14 +110,14 @@ function boost_cart:get_rail_direction(pos_, dir, ctrl, old_switch, railtype)
|
|||||||
right_check = false
|
right_check = false
|
||||||
end
|
end
|
||||||
if ctrl.left and left_check then
|
if ctrl.left and left_check then
|
||||||
cur = boost_cart:check_front_up_down(pos, left, false, railtype)
|
cur = self:check_front_up_down(pos, left, false, railtype)
|
||||||
if cur then
|
if cur then
|
||||||
return cur, 1
|
return cur, 1
|
||||||
end
|
end
|
||||||
left_check = false
|
left_check = false
|
||||||
end
|
end
|
||||||
if ctrl.right and right_check then
|
if ctrl.right and right_check then
|
||||||
cur = boost_cart:check_front_up_down(pos, right, false, railtype)
|
cur = self:check_front_up_down(pos, right, false, railtype)
|
||||||
if cur then
|
if cur then
|
||||||
return cur, 2
|
return cur, 2
|
||||||
end
|
end
|
||||||
@ -124,14 +126,14 @@ function boost_cart:get_rail_direction(pos_, dir, ctrl, old_switch, railtype)
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- Normal
|
-- Normal
|
||||||
cur = boost_cart:check_front_up_down(pos, dir, true, railtype)
|
cur = self:check_front_up_down(pos, dir, true, railtype)
|
||||||
if cur then
|
if cur then
|
||||||
return cur
|
return cur
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Left, if not already checked
|
-- Left, if not already checked
|
||||||
if left_check then
|
if left_check then
|
||||||
cur = boost_cart:check_front_up_down(pos, left, false, railtype)
|
cur = self:check_front_up_down(pos, left, false, railtype)
|
||||||
if cur then
|
if cur then
|
||||||
return cur
|
return cur
|
||||||
end
|
end
|
||||||
@ -139,7 +141,7 @@ function boost_cart:get_rail_direction(pos_, dir, ctrl, old_switch, railtype)
|
|||||||
|
|
||||||
-- Right, if not already checked
|
-- Right, if not already checked
|
||||||
if right_check then
|
if right_check then
|
||||||
cur = boost_cart:check_front_up_down(pos, right, false, railtype)
|
cur = self:check_front_up_down(pos, right, false, railtype)
|
||||||
if cur then
|
if cur then
|
||||||
return cur
|
return cur
|
||||||
end
|
end
|
||||||
@ -147,7 +149,7 @@ function boost_cart:get_rail_direction(pos_, dir, ctrl, old_switch, railtype)
|
|||||||
|
|
||||||
-- Backwards
|
-- Backwards
|
||||||
if not old_switch then
|
if not old_switch then
|
||||||
cur = boost_cart:check_front_up_down(pos, {
|
cur = self:check_front_up_down(pos, {
|
||||||
x = -dir.x,
|
x = -dir.x,
|
||||||
y = dir.y,
|
y = dir.y,
|
||||||
z = -dir.z
|
z = -dir.z
|
||||||
@ -174,7 +176,7 @@ function boost_cart:pathfinder(pos_, old_pos, old_dir, distance, ctrl,
|
|||||||
math.floor(distance + 1))
|
math.floor(distance + 1))
|
||||||
|
|
||||||
for i = 1, distance do
|
for i = 1, distance do
|
||||||
pf_dir, pf_switch = boost_cart:get_rail_direction(
|
pf_dir, pf_switch = self:get_rail_direction(
|
||||||
pf_pos, pf_dir, ctrl, pf_switch, railtype)
|
pf_pos, pf_dir, ctrl, pf_switch, railtype)
|
||||||
|
|
||||||
if vector.equals(pf_dir, {x=0, y=0, z=0}) then
|
if vector.equals(pf_dir, {x=0, y=0, z=0}) then
|
||||||
|
Loading…
Reference in New Issue
Block a user