Break too long lines and simplify the code

- Add boost_cart:get_rail_groups to get the default rail groups
- Remove duplicated code
- Do not boost the cart backwards when the brake rail gets activated
- Attempt to fix the cart swing problem
- Trim tailings and tidy up code
- TODO: Document all the stuff
This commit is contained in:
SmallJoker 2016-07-22 11:06:28 +02:00
parent d166b7ea68
commit eb6a833b66
4 changed files with 79 additions and 45 deletions

View File

@ -15,7 +15,7 @@ function boost_cart:signal_detector_rail(pos)
if minetest.get_item_group(node.name, "detector_rail") ~= 1 then
return
end
--minetest.log("action", "Signaling detector at " .. minetest.pos_to_string(pos))
if node.name == "boost_cart:detectorrail" then
minetest.swap_node(pos, {name = "boost_cart:detectorrail_on", param2=node.param2})
end
@ -25,16 +25,24 @@ end
boost_cart:register_rail("boost_cart:detectorrail", {
description = "Detector rail",
tiles = {"carts_rail_dtc.png", "carts_rail_curved_dtc.png", "carts_rail_t_junction_dtc.png", "carts_rail_crossing_dtc.png"},
groups = {dig_immediate = 2, attached_node = 1, rail = 1, connect_to_raillike = 1, detector_rail = 1},
tiles = {
"carts_rail_dtc.png", "carts_rail_curved_dtc.png",
"carts_rail_t_junction_dtc.png", "carts_rail_crossing_dtc.png"
},
groups = boost_cart:get_rail_groups({detector_rail = 1}),
mesecons = {receptor = {state = "off", rules = mesecons_rules}},
})
boost_cart:register_rail("boost_cart:detectorrail_on", {
description = "Detector rail ON (you hacker you)",
tiles = {"carts_rail_dtc_on.png", "carts_rail_curved_dtc_on.png", "carts_rail_t_junction_dtc_on.png", "carts_rail_crossing_dtc_on.png"},
groups = {dig_immediate = 2, attached_node = 1, rail = 1, connect_to_raillike = 1, detector_rail = 1, not_in_creative_inventory = 1},
tiles = {
"carts_rail_dtc_on.png", "carts_rail_curved_dtc_on.png",
"carts_rail_t_junction_dtc_on.png", "carts_rail_crossing_dtc_on.png"
},
groups = boost_cart:get_rail_groups({
detector_rail = 1, not_in_creative_inventory = 1
}),
drop = "boost_cart:detectorrail",
mesecons = {receptor = {state = "on", rules = mesecons_rules}},

View File

@ -213,3 +213,14 @@ function boost_cart:register_rail(name, def)
minetest.register_node(name, def)
end
function boost_cart:get_rail_groups(additional_groups)
-- Get the default rail groups and add more when a table is given
local groups = {dig_immediate = 2, attached_node = 1, rail = 1, connect_to_raillike = 1}
if type(additional_groups) == "table" then
for k, v in pairs(additional_groups) do
groups[k] = v
end
end
return groups
end

View File

@ -194,7 +194,9 @@ function boost_cart.cart:on_step(dtime)
if self.old_pos then
-- Detection for "skipping" nodes
local expected_pos = vector.add(self.old_pos, self.old_dir)
local found_path = boost_cart:pathfinder(pos, expected_pos, self.old_dir, ctrl, self.old_switch, self.railtype)
local found_path = boost_cart:pathfinder(
pos, expected_pos, self.old_dir, ctrl, self.old_switch, self.railtype
)
if not found_path then
-- No rail found: reset back to the expected position
@ -203,20 +205,12 @@ function boost_cart.cart:on_step(dtime)
end
end
if vel.y == 0 then
-- Stop cart completely (do not swing)
for _,v in ipairs({"x", "z"}) do
if vel[v] ~= 0 and math.abs(vel[v]) < 0.9 then
vel[v] = 0
update.vel = true
end
end
end
local cart_dir = boost_cart:velocity_to_dir(vel)
local max_vel = boost_cart.speed_max
if not dir then
dir, last_switch = boost_cart:get_rail_direction(pos, cart_dir, ctrl, self.old_switch, self.railtype)
dir, last_switch = boost_cart:get_rail_direction(
pos, cart_dir, ctrl, self.old_switch, self.railtype
)
end
local new_acc = {x=0, y=0, z=0}
@ -253,30 +247,35 @@ function boost_cart.cart:on_step(dtime)
local speed_mod = tonumber(speed_mod_string)
if speed_mod_string == "halt" then
vel = {x=0, y=0, z=0}
acc = {x=0, y=0, z=0}
dir = {x=0, y=0, z=0}
acc = 0
pos = vector.round(pos)
update.pos = true
update.vel = true
elseif speed_mod and speed_mod ~= 0 then
if speed_mod > 0 then
for _,v in ipairs({"x","y","z"}) do
if math.abs(vel[v]) >= max_vel then
speed_mod = 0
break
end
end
end
-- Try to make it similar to the original carts mod
acc = acc + (speed_mod * 10)
else
acc = acc - 0.4
-- Handbrake
if ctrl and ctrl.down and math.abs(vel.x + vel.z) > 1.2 then
if ctrl and ctrl.down then
acc = acc - 1.2
end
end
if self.old_dir.y == 0 and not self.punched then
-- Stop the cart swing between two rail parts (handbrake)
if vector.equals(vector.multiply(self.old_dir, -1), dir) then
vel = {x=0, y=0, z=0}
acc = 0
if self.old_pos then
pos = vector.new(self.old_pos)
update.pos = true
end
dir = vector.new(self.old_dir)
update.vel = true
end
end
new_acc = vector.multiply(dir, acc)
end
@ -308,6 +307,7 @@ function boost_cart.cart:on_step(dtime)
obj_:get_luaentity() and
not obj_:get_luaentity().physical_state and
obj_:get_luaentity().name == "__builtin:item" then
obj_:set_attach(self.object, "", {x=0, y=0, z=0}, {x=0, y=0, z=0})
self.attached_items[#self.attached_items + 1] = obj_
end

View File

@ -1,7 +1,10 @@
minetest.register_node(":default:rail", {
description = "Rail",
drawtype = "raillike",
tiles = {"default_rail.png", "default_rail_curved.png", "default_rail_t_junction.png", "default_rail_crossing.png"},
tiles = {
"default_rail.png", "default_rail_curved.png",
"default_rail_t_junction.png", "default_rail_crossing.png"
},
inventory_image = "default_rail.png",
wield_image = "default_rail.png",
paramtype = "light",
@ -12,7 +15,7 @@ minetest.register_node(":default:rail", {
type = "fixed",
fixed = {-1/2, -1/2, -1/2, 1/2, -1/2+1/16, 1/2},
},
groups = {dig_immediate = 2, attached_node = 1, rail = 1, connect_to_raillike = 1},
groups = boost_cart:get_rail_groups(),
})
if minetest.get_modpath("moreores") then
@ -21,8 +24,11 @@ if minetest.get_modpath("moreores") then
else
boost_cart:register_rail(":carts:copperrail", {
description = "Copper rail",
tiles = {"carts_rail_cp.png", "carts_rail_curved_cp.png", "carts_rail_t_junction_cp.png", "carts_rail_crossing_cp.png"},
groups = {dig_immediate = 2, attached_node = 1, rail = 1, connect_to_raillike = 1},
tiles = {
"carts_rail_cp.png", "carts_rail_curved_cp.png",
"carts_rail_t_junction_cp.png", "carts_rail_crossing_cp.png"
},
groups = boost_cart:get_rail_groups(),
})
minetest.register_craft({
@ -39,8 +45,11 @@ end
boost_cart:register_rail(":carts:powerrail", {
description = "Powered rail",
tiles = {"carts_rail_pwr.png", "carts_rail_curved_pwr.png", "carts_rail_t_junction_pwr.png", "carts_rail_crossing_pwr.png"},
groups = {dig_immediate = 2, attached_node = 1, rail = 1, connect_to_raillike = 1},
tiles = {
"carts_rail_pwr.png", "carts_rail_curved_pwr.png",
"carts_rail_t_junction_pwr.png", "carts_rail_crossing_pwr.png"
},
groups = boost_cart:get_rail_groups(),
after_place_node = function(pos, placer, itemstack)
if not mesecon then
@ -72,19 +81,22 @@ minetest.register_craft({
boost_cart:register_rail(":carts:brakerail", {
description = "Brake rail",
tiles = {"carts_rail_brk.png", "carts_rail_curved_brk.png", "carts_rail_t_junction_brk.png", "carts_rail_crossing_brk.png"},
groups = {dig_immediate = 2, attached_node = 1, rail = 1, connect_to_raillike = 1},
tiles = {
"carts_rail_brk.png", "carts_rail_curved_brk.png",
"carts_rail_t_junction_brk.png", "carts_rail_crossing_brk.png"
},
groups = boost_cart:get_rail_groups(),
after_place_node = function(pos, placer, itemstack)
if not mesecon then
minetest.get_meta(pos):set_string("cart_acceleration", "-0.2")
minetest.get_meta(pos):set_string("cart_acceleration", "-0.3")
end
end,
mesecons = {
effector = {
action_on = function(pos, node)
boost_cart:boost_rail(pos, -0.2)
minetest.get_meta(pos):set_string("cart_acceleration", "-0.3")
end,
action_off = function(pos, node)
@ -103,10 +115,13 @@ minetest.register_craft({
}
})
boost_cart:register_rail(":boost_cart:startstoprail", {
boost_cart:register_rail("boost_cart:startstoprail", {
description = "Start-stop rail",
tiles = {"carts_rail_ss.png", "carts_rail_curved_ss.png", "carts_rail_t_junction_ss.png", "carts_rail_crossing_ss.png"},
groups = {dig_immediate = 2, attached_node = 1, rail = 1, connect_to_raillike = 1},
tiles = {
"carts_rail_ss.png", "carts_rail_curved_ss.png",
"carts_rail_t_junction_ss.png", "carts_rail_crossing_ss.png"
},
groups = boost_cart:get_rail_groups(),
after_place_node = function(pos, placer, itemstack)
if not mesecon then