Fixish reorganizing, initial train implementation

This commit is contained in:
teknomunk 2024-04-06 22:10:22 +00:00
parent 386bde698d
commit 24bf99df44
4 changed files with 76 additions and 23 deletions

View File

@ -27,21 +27,6 @@ local max_step_distance = 0.5
local MINECART_MAX_HP = 4
local PASSENGER_ATTACH_POSITION = vector.new(0, -1.75, 0)
local function detach_minecart(self)
local staticdata = self._staticdata
staticdata.connected_at = nil
self.object:set_velocity(staticdata.dir * staticdata.velocity)
end
local function try_detach_minecart(self)
local staticdata = self._staticdata
local node = minetest.get_node(staticdata.connected_at)
if minetest.get_item_group(node.name, "rail") == 0 then
detach_minecart(self)
end
end
local function detach_driver(self)
if not self._driver then
return

View File

@ -9,6 +9,6 @@ mcl_minecarts.speed_max = 10
mcl_minecarts.check_float_time = 15
mcl_minecarts.FRICTION = 0.4
for _,filename in pairs({"functions","rails","train","storage","carts"}) do
for _,filename in pairs({"storage","functions","rails","train","carts"}) do
dofile(modpath.."/"..filename..".lua")
end

View File

@ -10,6 +10,7 @@ local friction = mcl_minecarts.FRICTION
-- Imports
local train_length = mod.train_length
local update_train = mod.update_train
local link_cart_ahead = mod.link_cart_ahead
local update_cart_orientation = mod.update_cart_orientation
local get_cart_data = mod.get_cart_data
@ -141,10 +142,10 @@ local function handle_cart_collision(cart1, prev_pos, next_dir)
local m1 = cart1_staticdata.mass
local m2 = cart2_staticdata.mass
if u2 == 0 and u1 < 1 and train_length(cart1) < 3 then
-- Link carts
cart2_staticdata.behind = cart1_staticdata.uuid
cart1_staticdata.ahead = cart1_staticdata.uuid
print("u1="..tostring(u1)..",u2="..tostring(u2))
if u2 == 0 and u1 < 4 and train_length(cart1) < 3 then
link_cart_ahead(cart1, cart2)
cart2_staticdata.dir = mcl_minecarts:get_rail_direction(cart2_staticdata.connected_at, cart1_staticdata.dir)
cart2_staticdata.velocity = cart1_staticdata.velocity
return
end
@ -243,7 +244,7 @@ local function reverse_direction(self, staticdata)
-- Complete moving thru this block into the next, reverse direction, and put us back at the same position we were at
local next_dir = -staticdata.dir
staticdata.connected_at = staticdata.connected_at + staticdata.dir
staticdata.distance = 1 - staticdata.distance
staticdata.distance = 1 - (staticdata.distance or 0)
-- recalculate direction
local next_dir,_ = mcl_minecarts:get_rail_direction(staticdata.connected_at, next_dir, nil, nil, staticdata.railtype)

View File

@ -1,5 +1,72 @@
function mcl_minecarts.update_train(cart)
local modname = minetest.get_current_modname()
local modpath = minetest.get_modpath(modname)
local mod = mcl_minecarts
-- Imports
local get_cart_data = mod.get_cart_data
local function find_back(start)
while start.behind do
local nxt = get_cart_data(start.behind)
if not nxt then return start end
start = nxt
end
return start
end
function mcl_minecarts.train_length(cart)
local function train_cars(anchor)
local back = find_back(anchor._staticdata)
return function()
if not back then return end
local ret = back
if back.ahead then
back = get_cart_data(back.ahead)
else
back = nil
end
return ret
end
end
function mod.update_train(cart)
local sum_velocity = 0
local count = 0
for cart in train_cars(cart) do
count = count + 1
sum_velocity = sum_velocity + (cart.velocity or 0)
end
local avg_velocity = sum_velocity / count
if count == 0 then return end
print("Using velocity "..tostring(avg_velocity))
-- Set the entire train to the average velocity
for c in train_cars(cart) do
print(tostring(c.behind).."->"..c.uuid.."->"..tostring(c.ahead).." setting cart #"..c.uuid.." velocity to "..tostring(avg_velocity))
c.velocity = avg_velocity
end
end
function mod.train_length(cart)
local count = 0
for cart in train_cars(cart) do
count = count + 1
end
return count
end
function mod.link_cart_ahead(cart, cart_ahead)
local staticdata = cart._staticdata
local ca_staticdata = cart_ahead._staticdata
minetest.log("action","Linking cart #"..staticdata.uuid.." to cart #"..ca_staticdata.uuid)
staticdata.ahead = ca_staticdata.uuid
ca_staticdata.behind = staticdata.uuid
end
function mod.is_in_same_train(anchor, other)
for cart in train_cars(anchor) do
if cart.uuid == other.uuid then return true end
end
return false
end