forked from mtcontrib/boost_cart
Punch cart if mesecon signal is received
This commit is contained in:
parent
df04aa2a4d
commit
159d4ec603
@ -36,7 +36,8 @@ function boost_cart:is_rail(pos, railtype)
|
|||||||
return minetest.get_item_group(node, "connect_to_raillike") == railtype
|
return minetest.get_item_group(node, "connect_to_raillike") == railtype
|
||||||
end
|
end
|
||||||
|
|
||||||
function boost_cart:check_front_up_down(pos, dir, onlyDown, railtype)
|
function boost_cart:check_front_up_down(pos, dir_, check_down, railtype)
|
||||||
|
local dir = vector.new(dir_)
|
||||||
local cur = nil
|
local cur = nil
|
||||||
|
|
||||||
-- Front
|
-- Front
|
||||||
@ -46,7 +47,7 @@ function boost_cart:check_front_up_down(pos, dir, onlyDown, railtype)
|
|||||||
return dir
|
return dir
|
||||||
end
|
end
|
||||||
-- Up
|
-- Up
|
||||||
if not onlyDown then
|
if check_down 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 boost_cart:is_rail(cur, railtype) then
|
||||||
@ -62,12 +63,10 @@ function boost_cart:check_front_up_down(pos, dir, onlyDown, railtype)
|
|||||||
return nil
|
return nil
|
||||||
end
|
end
|
||||||
|
|
||||||
function boost_cart:get_rail_direction(pos_, dir_, ctrl, old_switch, railtype)
|
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 cur = nil
|
local cur = nil
|
||||||
local left_check, right_check = true, true
|
local left_check, right_check = true, true
|
||||||
old_switch = old_switch or 0
|
|
||||||
|
|
||||||
-- Check left and right
|
-- Check left and right
|
||||||
local left = {x=0, y=0, z=0}
|
local left = {x=0, y=0, z=0}
|
||||||
@ -87,14 +86,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, true, railtype)
|
cur = boost_cart: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, true, railtype)
|
cur = boost_cart:check_front_up_down(pos, right, false, railtype)
|
||||||
if cur then
|
if cur then
|
||||||
return cur, 2
|
return cur, 2
|
||||||
end
|
end
|
||||||
@ -103,14 +102,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, false, railtype)
|
cur = boost_cart: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, true, railtype)
|
cur = boost_cart:check_front_up_down(pos, left, false, railtype)
|
||||||
if cur then
|
if cur then
|
||||||
return cur
|
return cur
|
||||||
end
|
end
|
||||||
@ -118,7 +117,19 @@ 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, true, railtype)
|
cur = boost_cart:check_front_up_down(pos, right, false, railtype)
|
||||||
|
if cur then
|
||||||
|
return cur
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Backwards
|
||||||
|
if not old_switch then
|
||||||
|
cur = boost_cart:check_front_up_down(pos, {
|
||||||
|
x = -dir.x,
|
||||||
|
y = dir.y,
|
||||||
|
z = -dir.z
|
||||||
|
}, true, railtype)
|
||||||
if cur then
|
if cur then
|
||||||
return cur
|
return cur
|
||||||
end
|
end
|
||||||
@ -127,3 +138,13 @@ function boost_cart:get_rail_direction(pos_, dir_, ctrl, old_switch, railtype)
|
|||||||
return {x=0, y=0, z=0}
|
return {x=0, y=0, z=0}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function boost_cart:boost_rail(pos, amount)
|
||||||
|
minetest.get_meta(pos):set_string("cart_acceleration", tostring(amount))
|
||||||
|
for _,obj_ in ipairs(minetest.get_objects_inside_radius(pos, 0.5)) do
|
||||||
|
if not obj_:is_player() and
|
||||||
|
obj_:get_luaentity() and
|
||||||
|
obj_:get_luaentity().name == "carts:cart" then
|
||||||
|
obj_:get_luaentity():on_punch()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
23
init.lua
23
init.lua
@ -29,7 +29,7 @@ boost_cart.cart = {
|
|||||||
velocity = {x=0, y=0, z=0}, -- only used on punch
|
velocity = {x=0, y=0, z=0}, -- only used on punch
|
||||||
old_dir = {x=0, y=0, z=0},
|
old_dir = {x=0, y=0, z=0},
|
||||||
old_pos = nil,
|
old_pos = nil,
|
||||||
old_switch = nil,
|
old_switch = 0,
|
||||||
railtype = nil,
|
railtype = nil,
|
||||||
attached_items = {}
|
attached_items = {}
|
||||||
}
|
}
|
||||||
@ -54,7 +54,20 @@ function boost_cart.cart:on_activate(staticdata, dtime_s)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function boost_cart.cart:on_punch(puncher, time_from_last_punch, tool_capabilities, direction)
|
function boost_cart.cart:on_punch(puncher, time_from_last_punch, tool_capabilities, direction)
|
||||||
|
local pos = self.object:getpos()
|
||||||
|
if not self.railtype then
|
||||||
|
local node = minetest.get_node(vector.floor(pos)).name
|
||||||
|
self.railtype = minetest.get_item_group(node, "connect_to_raillike")
|
||||||
|
end
|
||||||
|
|
||||||
if not puncher or not puncher:is_player() then
|
if not puncher or not puncher:is_player() then
|
||||||
|
local cart_dir = boost_cart:get_rail_direction(pos, {x=1, y=0, z=0}, nil, nil, self.railtype)
|
||||||
|
if vector.equals(cart_dir, {x=0, y=0, z=0}) then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
self.velocity = vector.multiply(cart_dir, 3)
|
||||||
|
self.old_pos = nil
|
||||||
|
self.punched = true
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -80,12 +93,6 @@ 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
|
||||||
@ -95,7 +102,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, nil, self.railtype)
|
local cart_dir = boost_cart:get_rail_direction(pos, punch_dir, nil, 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
|
||||||
|
@ -73,7 +73,7 @@ minetest.register_node(":carts:powerrail", {
|
|||||||
mesecons = {
|
mesecons = {
|
||||||
effector = {
|
effector = {
|
||||||
action_on = function(pos, node)
|
action_on = function(pos, node)
|
||||||
minetest.get_meta(pos):set_string("cart_acceleration", "0.5")
|
boost_cart:boost_rail(pos, 0.5)
|
||||||
end,
|
end,
|
||||||
|
|
||||||
action_off = function(pos, node)
|
action_off = function(pos, node)
|
||||||
@ -117,7 +117,7 @@ minetest.register_node(":carts:brakerail", {
|
|||||||
mesecons = {
|
mesecons = {
|
||||||
effector = {
|
effector = {
|
||||||
action_on = function(pos, node)
|
action_on = function(pos, node)
|
||||||
minetest.get_meta(pos):set_string("cart_acceleration", "-0.2")
|
boost_cart:boost_rail(pos, -0.2)
|
||||||
end,
|
end,
|
||||||
|
|
||||||
action_off = function(pos, node)
|
action_off = function(pos, node)
|
||||||
|
Loading…
Reference in New Issue
Block a user