Fix rail counting in on_rail_step, localize API functions for faster access

This commit is contained in:
SmallJoker 2017-10-13 15:20:39 +02:00
parent ad93d338b3
commit 5502952bbd
2 changed files with 30 additions and 27 deletions

View File

@ -1,17 +1,17 @@
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
if self.sound_counter <= 0 then
if entity.sound_counter <= 0 then
minetest.sound_play("cart_rail", {
pos = pos,
max_hear_distance = 40,
gain = 0.5
})
self.sound_counter = math.random(4, 15)
entity.sound_counter = math.random(4, 15)
end
self.sound_counter = self.sound_counter - 1
entity.sound_counter = entity.sound_counter - distance
if HAVE_MESECONS_ENABLED then
boost_cart:signal_detector_rail(pos)
@ -65,7 +65,7 @@ function cart_entity:on_activate(staticdata, dtime_s)
return
end
local data = minetest.deserialize(staticdata)
if not data or type(data) ~= "table" then
if type(data) ~= "table" then
return
end
self.railtype = data.railtype
@ -149,6 +149,7 @@ function cart_entity:on_punch(puncher, time_from_last_punch, tool_capabilities,
self.punched = true
end
local v3_len = vector.length
function cart_entity:on_step(dtime)
local vel = self.object:get_velocity()
if self.punched then
@ -161,10 +162,10 @@ function cart_entity:on_step(dtime)
local pos = self.object:get_pos()
local cart_dir = boost_cart:velocity_to_dir(vel)
local same_dir = vector.equals(cart_dir, self.old_dir)
local update = {}
if self.old_pos and not self.punched
and vector.equals(cart_dir, self.old_dir) then
if self.old_pos and not self.punched and same_dir then
local flo_pos = vector.round(pos)
local flo_old = vector.round(self.old_pos)
if vector.equals(flo_pos, flo_old) then
@ -174,6 +175,7 @@ function cart_entity:on_step(dtime)
end
local ctrl, player
local distance = 1
-- Get player controls
if self.driver then
@ -184,12 +186,11 @@ function cart_entity:on_step(dtime)
end
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?)
-- It's sophisticated enough to take the acceleration in account
local acc = self.object:get_acceleration()
local distance = dtime * (vector.length(vel) +
0.5 * dtime * vector.length(acc))
distance = dtime * (v3_len(vel) + 0.5 * dtime * v3_len(acc))
local new_pos, new_dir = boost_cart:pathfinder(
pos, self.old_pos, self.old_dir, distance, ctrl,
@ -230,7 +231,7 @@ function cart_entity:on_step(dtime)
else
-- Direction change detected
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
if dir.y ~= self.old_dir.y then
pos = vector.round(pos)
@ -293,7 +294,6 @@ function cart_entity:on_step(dtime)
new_acc = vector.multiply(dir, acc)
end
boost_cart.on_rail_step(self, vector.round(pos))
-- Limits
local max_vel = boost_cart.speed_max
@ -312,6 +312,7 @@ function cart_entity:on_step(dtime)
end
self.old_switch = switch_keys
boost_cart:on_rail_step(self, self.old_pos, distance)
if self.punched then
-- Collect dropped items

View File

@ -30,14 +30,16 @@ end
function boost_cart:velocity_to_dir(v)
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
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
local get_node = minetest.get_node
local get_item_group = minetest.get_item_group
function boost_cart:is_rail(pos, railtype)
local node = minetest.get_node(pos).name
local node = get_node(pos).name
if node == "ignore" then
local vm = minetest.get_voxel_manip()
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)
node = minetest.get_name_from_content_id(data[vi])
end
if minetest.get_item_group(node, "rail") == 0 then
if get_item_group(node, "rail") == 0 then
return false
end
if not railtype then
return true
end
return minetest.get_item_group(node, "connect_to_raillike") == railtype
return get_item_group(node, "connect_to_raillike") == railtype
end
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
dir.y = 0
cur = vector.add(pos, dir)
if boost_cart:is_rail(cur, railtype) then
if self:is_rail(cur, railtype) then
return dir
end
-- Up
if check_up then
dir.y = 1
cur = vector.add(pos, dir)
if boost_cart:is_rail(cur, railtype) then
if self:is_rail(cur, railtype) then
return dir
end
end
-- Down
dir.y = -1
cur = vector.add(pos, dir)
if boost_cart:is_rail(cur, railtype) then
if self:is_rail(cur, railtype) then
return dir
end
return nil
@ -108,14 +110,14 @@ function boost_cart:get_rail_direction(pos_, dir, ctrl, old_switch, railtype)
right_check = false
end
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
return cur, 1
end
left_check = false
end
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
return cur, 2
end
@ -124,14 +126,14 @@ function boost_cart:get_rail_direction(pos_, dir, ctrl, old_switch, railtype)
end
-- 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
return cur
end
-- Left, if not already checked
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
return cur
end
@ -139,7 +141,7 @@ function boost_cart:get_rail_direction(pos_, dir, ctrl, old_switch, railtype)
-- Right, if not already checked
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
return cur
end
@ -147,7 +149,7 @@ function boost_cart:get_rail_direction(pos_, dir, ctrl, old_switch, railtype)
-- Backwards
if not old_switch then
cur = boost_cart:check_front_up_down(pos, {
cur = self:check_front_up_down(pos, {
x = -dir.x,
y = dir.y,
z = -dir.z
@ -174,7 +176,7 @@ function boost_cart:pathfinder(pos_, old_pos, old_dir, distance, ctrl,
math.floor(distance + 1))
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)
if vector.equals(pf_dir, {x=0, y=0, z=0}) then