forked from mtcontrib/boost_cart
Fix different railtype problem
This commit is contained in:
parent
670f961d35
commit
df04aa2a4d
@ -14,7 +14,7 @@ function boost_cart:velocity_to_dir(v)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function boost_cart:is_rail(pos)
|
function boost_cart:is_rail(pos, railtype)
|
||||||
local node = minetest.get_node(pos).name
|
local node = minetest.get_node(pos).name
|
||||||
if node == "ignore" then
|
if node == "ignore" then
|
||||||
local vm = minetest.get_voxel_manip()
|
local vm = minetest.get_voxel_manip()
|
||||||
@ -30,41 +30,39 @@ function boost_cart:is_rail(pos)
|
|||||||
if minetest.get_item_group(node, "rail") == 0 then
|
if minetest.get_item_group(node, "rail") == 0 then
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
local group = minetest.get_item_group(node, "connect_to_raillike")
|
if not railtype then
|
||||||
if self.railtype == nil then
|
|
||||||
self.railtype = group
|
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
return group == self.railtype
|
return minetest.get_item_group(node, "connect_to_raillike") == railtype
|
||||||
end
|
end
|
||||||
|
|
||||||
function boost_cart:check_front_up_down(pos, dir, onlyDown)
|
function boost_cart:check_front_up_down(pos, dir, onlyDown, railtype)
|
||||||
local cur = nil
|
local cur = nil
|
||||||
|
|
||||||
-- Front
|
-- Front
|
||||||
dir.y = 0
|
dir.y = 0
|
||||||
cur = vector.add(pos, dir)
|
cur = vector.add(pos, dir)
|
||||||
if boost_cart:is_rail(cur) then
|
if boost_cart:is_rail(cur, railtype) then
|
||||||
return dir
|
return dir
|
||||||
end
|
end
|
||||||
-- Up
|
-- Up
|
||||||
if not onlyDown then
|
if not onlyDown then
|
||||||
dir.y = 1
|
dir.y = 1
|
||||||
cur = vector.add(pos, dir)
|
cur = vector.add(pos, dir)
|
||||||
if boost_cart:is_rail(cur) then
|
if boost_cart: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) then
|
if boost_cart:is_rail(cur, railtype) then
|
||||||
return dir
|
return dir
|
||||||
end
|
end
|
||||||
return nil
|
return nil
|
||||||
end
|
end
|
||||||
|
|
||||||
function boost_cart:get_rail_direction(pos_, dir_, ctrl, old_switch)
|
function boost_cart:get_rail_direction(pos_, dir_, ctrl, old_switch, railtype)
|
||||||
local pos = vector.round(pos_)
|
local pos = vector.round(pos_)
|
||||||
local dir = vector.new(dir_)
|
local dir = vector.new(dir_)
|
||||||
local cur = nil
|
local cur = nil
|
||||||
@ -89,14 +87,14 @@ function boost_cart:get_rail_direction(pos_, dir_, ctrl, old_switch)
|
|||||||
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, true)
|
cur = boost_cart:check_front_up_down(pos, left, true, 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, true)
|
cur = boost_cart:check_front_up_down(pos, right, true, railtype)
|
||||||
if cur then
|
if cur then
|
||||||
return cur, 2
|
return cur, 2
|
||||||
end
|
end
|
||||||
@ -105,14 +103,14 @@ function boost_cart:get_rail_direction(pos_, dir_, ctrl, old_switch)
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- Normal
|
-- Normal
|
||||||
cur = boost_cart:check_front_up_down(pos, dir)
|
cur = boost_cart:check_front_up_down(pos, dir, false, 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, true)
|
cur = boost_cart:check_front_up_down(pos, left, true, railtype)
|
||||||
if cur then
|
if cur then
|
||||||
return cur
|
return cur
|
||||||
end
|
end
|
||||||
@ -120,7 +118,7 @@ function boost_cart:get_rail_direction(pos_, dir_, ctrl, old_switch)
|
|||||||
|
|
||||||
-- 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, true)
|
cur = boost_cart:check_front_up_down(pos, right, true, railtype)
|
||||||
if cur then
|
if cur then
|
||||||
return cur
|
return cur
|
||||||
end
|
end
|
||||||
|
12
init.lua
12
init.lua
@ -80,6 +80,12 @@ function boost_cart.cart:on_punch(puncher, time_from_last_punch, tool_capabiliti
|
|||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if not self.railtype then
|
||||||
|
local pos = vector.floor(self.object:getpos())
|
||||||
|
local node = minetest.get_node(pos).name
|
||||||
|
self.railtype = minetest.get_item_group(node, "connect_to_raillike")
|
||||||
|
end
|
||||||
|
|
||||||
local vel = self.object:getvelocity()
|
local vel = self.object:getvelocity()
|
||||||
if puncher:get_player_name() == self.driver then
|
if puncher:get_player_name() == self.driver then
|
||||||
if math.abs(vel.x + vel.z) > 7 then
|
if math.abs(vel.x + vel.z) > 7 then
|
||||||
@ -89,7 +95,7 @@ function boost_cart.cart:on_punch(puncher, time_from_last_punch, tool_capabiliti
|
|||||||
|
|
||||||
local punch_dir = boost_cart:velocity_to_dir(puncher:get_look_dir())
|
local punch_dir = boost_cart:velocity_to_dir(puncher:get_look_dir())
|
||||||
punch_dir.y = 0
|
punch_dir.y = 0
|
||||||
local cart_dir = boost_cart:get_rail_direction(self.object:getpos(), punch_dir)
|
local cart_dir = boost_cart:get_rail_direction(self.object:getpos(), punch_dir, nil, self.railtype)
|
||||||
if vector.equals(cart_dir, {x=0, y=0, z=0}) then
|
if vector.equals(cart_dir, {x=0, y=0, z=0}) then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
@ -134,7 +140,7 @@ function boost_cart.cart:on_step(dtime)
|
|||||||
for _,v in ipairs({"x","y","z"}) do
|
for _,v in ipairs({"x","y","z"}) do
|
||||||
if math.abs(diff[v]) > 1.1 then
|
if math.abs(diff[v]) > 1.1 then
|
||||||
local expected_pos = vector.add(self.old_pos, self.old_dir)
|
local expected_pos = vector.add(self.old_pos, self.old_dir)
|
||||||
dir, last_switch = boost_cart:get_rail_direction(pos, self.old_dir, ctrl, self.old_switch)
|
dir, last_switch = boost_cart:get_rail_direction(pos, self.old_dir, ctrl, self.old_switch, self.railtype)
|
||||||
if vector.equals(dir, {x=0, y=0, z=0}) then
|
if vector.equals(dir, {x=0, y=0, z=0}) then
|
||||||
dir = false
|
dir = false
|
||||||
pos = vector.new(expected_pos)
|
pos = vector.new(expected_pos)
|
||||||
@ -157,7 +163,7 @@ function boost_cart.cart:on_step(dtime)
|
|||||||
local cart_dir = boost_cart:velocity_to_dir(vel)
|
local cart_dir = boost_cart:velocity_to_dir(vel)
|
||||||
local max_vel = boost_cart.speed_max
|
local max_vel = boost_cart.speed_max
|
||||||
if not dir then
|
if not dir then
|
||||||
dir, last_switch = boost_cart:get_rail_direction(pos, cart_dir, ctrl, self.old_switch)
|
dir, last_switch = boost_cart:get_rail_direction(pos, cart_dir, ctrl, self.old_switch, self.railtype)
|
||||||
end
|
end
|
||||||
|
|
||||||
local new_acc = {x=0, y=0, z=0}
|
local new_acc = {x=0, y=0, z=0}
|
||||||
|
Loading…
Reference in New Issue
Block a user