2024-04-07 00:10:22 +02:00
|
|
|
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
|
2024-04-09 02:40:04 +02:00
|
|
|
local save_cart_data = mod.save_cart_data
|
2024-04-07 12:18:55 +02:00
|
|
|
local MAX_TRAIN_LENGTH = mod.MAX_TRAIN_LENGTH
|
2024-04-07 00:10:22 +02:00
|
|
|
|
2024-04-07 12:18:55 +02:00
|
|
|
-- Follow .behind to the back end of a train
|
2024-04-07 00:10:22 +02:00
|
|
|
local function find_back(start)
|
2024-04-09 13:59:45 +02:00
|
|
|
assert(start)
|
|
|
|
|
2024-04-07 00:10:22 +02:00
|
|
|
while start.behind do
|
|
|
|
local nxt = get_cart_data(start.behind)
|
|
|
|
if not nxt then return start end
|
|
|
|
start = nxt
|
|
|
|
end
|
|
|
|
return start
|
2024-04-06 19:09:54 +02:00
|
|
|
end
|
|
|
|
|
2024-04-07 12:18:55 +02:00
|
|
|
-- Iterate across all the cars in a train
|
2024-04-09 13:59:45 +02:00
|
|
|
local function train_cars(staticdata)
|
|
|
|
assert(staticdata)
|
|
|
|
|
|
|
|
local back = find_back(staticdata)
|
2024-04-07 12:18:55 +02:00
|
|
|
local limit = MAX_TRAIN_LENGTH
|
2024-04-07 00:10:22 +02:00
|
|
|
return function()
|
2024-04-07 12:18:55 +02:00
|
|
|
if not back or limit <= 0 then return end
|
|
|
|
limit = limit - 1
|
2024-04-07 00:10:22 +02:00
|
|
|
|
|
|
|
local ret = back
|
|
|
|
if back.ahead then
|
|
|
|
back = get_cart_data(back.ahead)
|
|
|
|
else
|
|
|
|
back = nil
|
|
|
|
end
|
|
|
|
return ret
|
|
|
|
end
|
|
|
|
end
|
2024-04-07 12:18:55 +02:00
|
|
|
function mod.train_length(cart)
|
2024-04-07 00:10:22 +02:00
|
|
|
local count = 0
|
|
|
|
for cart in train_cars(cart) do
|
|
|
|
count = count + 1
|
|
|
|
end
|
2024-04-07 12:18:55 +02:00
|
|
|
return count
|
|
|
|
end
|
2024-04-07 00:10:22 +02:00
|
|
|
|
2024-04-07 12:18:55 +02:00
|
|
|
function mod.is_in_same_train(anchor, other)
|
|
|
|
for cart in train_cars(anchor) do
|
|
|
|
if cart.uuid == other.uuid then return true end
|
2024-04-07 00:10:22 +02:00
|
|
|
end
|
2024-04-07 12:18:55 +02:00
|
|
|
return false
|
2024-04-07 00:10:22 +02:00
|
|
|
end
|
|
|
|
|
2024-04-07 12:18:55 +02:00
|
|
|
function mod.distance_between_cars(car1, car2)
|
|
|
|
if not car1.connected_at then return nil end
|
|
|
|
if not car2.connected_at then return nil end
|
|
|
|
|
|
|
|
if not car1.dir then car1.dir = vector.zero() end
|
|
|
|
if not car2.dir then car2.dir = vector.zero() end
|
|
|
|
|
|
|
|
local pos1 = vector.add(car1.connected_at, vector.multiply(car1.dir, car1.distance))
|
|
|
|
local pos2 = vector.add(car2.connected_at, vector.multiply(car2.dir, car2.distance))
|
|
|
|
|
|
|
|
return vector.distance(pos1, pos2)
|
|
|
|
end
|
|
|
|
local distance_between_cars = mod.distance_between_cars
|
|
|
|
|
2024-04-09 02:40:04 +02:00
|
|
|
local function break_train_at(cart)
|
|
|
|
if cart.ahead then
|
|
|
|
local ahead = get_cart_data(cart.ahead)
|
|
|
|
if ahead then
|
|
|
|
ahead.behind = nil
|
|
|
|
cart.ahead = nil
|
|
|
|
save_cart_data(ahead.uuid)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if cart.behind then
|
|
|
|
local behind = get_cart_data(cart.behind)
|
|
|
|
if behind then
|
|
|
|
behind.ahead = nil
|
|
|
|
cart.behind = nil
|
|
|
|
save_cart_data(behind.uuid)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
save_cart_data(cart.uuid)
|
|
|
|
end
|
|
|
|
mod.break_train_at = break_train_at
|
|
|
|
|
2024-04-09 13:59:45 +02:00
|
|
|
function mod.update_train(staticdata)
|
|
|
|
--local staticdata = cart._staticdata
|
2024-04-07 12:18:55 +02:00
|
|
|
|
|
|
|
-- Only update from the back
|
|
|
|
if staticdata.behind or not staticdata.ahead then return end
|
2024-04-09 09:35:57 +02:00
|
|
|
--print("\nUpdating train")
|
2024-04-07 12:18:55 +02:00
|
|
|
|
|
|
|
-- Do no special processing if the cart is not part of a train
|
|
|
|
if not staticdata.ahead and not staticdata.behind then return end
|
|
|
|
|
2024-04-08 21:38:13 +02:00
|
|
|
-- Calculate the maximum velocity of all train cars
|
|
|
|
local velocity = 0
|
|
|
|
local count = 0
|
2024-04-09 13:59:45 +02:00
|
|
|
for cart in train_cars(staticdata) do
|
2024-04-08 21:38:13 +02:00
|
|
|
velocity = velocity + (cart.velocity or 0)
|
|
|
|
count = count + 1
|
2024-04-07 12:18:55 +02:00
|
|
|
end
|
2024-04-08 21:38:13 +02:00
|
|
|
velocity = velocity / count
|
2024-04-09 09:35:57 +02:00
|
|
|
--print("Using velocity "..tostring(velocity))
|
2024-04-07 12:18:55 +02:00
|
|
|
|
|
|
|
-- Set the entire train to the average velocity
|
|
|
|
local behind = nil
|
2024-04-09 13:59:45 +02:00
|
|
|
for c in train_cars(staticdata) do
|
2024-04-07 12:18:55 +02:00
|
|
|
local e = 0
|
|
|
|
local separation
|
2024-04-08 21:38:13 +02:00
|
|
|
local cart_velocity = velocity
|
2024-04-09 02:40:04 +02:00
|
|
|
if not c.connected_at then
|
|
|
|
break_train_at(c)
|
|
|
|
elseif behind then
|
2024-04-07 12:18:55 +02:00
|
|
|
separation = distance_between_cars(behind, c)
|
|
|
|
local e = 0
|
2024-04-09 02:40:04 +02:00
|
|
|
if not separation then
|
|
|
|
break_train_at(c)
|
|
|
|
elseif separation > 1.6 then
|
2024-04-08 21:38:13 +02:00
|
|
|
cart_velocity = velocity * 0.9
|
2024-04-09 02:40:04 +02:00
|
|
|
elseif separation > 2.5 then
|
|
|
|
break_train_at(c)
|
2024-04-07 12:18:55 +02:00
|
|
|
elseif separation < 1.15 then
|
2024-04-08 21:38:13 +02:00
|
|
|
cart_velocity = velocity * 1.1
|
2024-04-07 12:18:55 +02:00
|
|
|
end
|
|
|
|
end
|
2024-04-09 09:35:57 +02:00
|
|
|
--[[
|
2024-04-08 21:38:13 +02:00
|
|
|
print(tostring(c.behind).."->"..c.uuid.."->"..tostring(c.ahead).."("..tostring(separation)..") setting cart #"..
|
|
|
|
c.uuid.." velocity from "..tostring(c.velocity).." to "..tostring(cart_velocity))
|
2024-04-09 09:35:57 +02:00
|
|
|
]]
|
2024-04-08 21:38:13 +02:00
|
|
|
c.velocity = cart_velocity
|
2024-04-07 12:18:55 +02:00
|
|
|
|
|
|
|
behind = c
|
2024-04-07 00:10:22 +02:00
|
|
|
end
|
|
|
|
end
|
2024-04-07 12:18:55 +02:00
|
|
|
|
2024-04-10 10:02:21 +02:00
|
|
|
function mod.link_cart_ahead(staticdata, ca_staticdata)
|
2024-04-07 00:10:22 +02:00
|
|
|
minetest.log("action","Linking cart #"..staticdata.uuid.." to cart #"..ca_staticdata.uuid)
|
|
|
|
|
|
|
|
staticdata.ahead = ca_staticdata.uuid
|
|
|
|
ca_staticdata.behind = staticdata.uuid
|
|
|
|
end
|
2024-04-08 23:37:54 +02:00
|
|
|
|
|
|
|
function mod.reverse_train(cart)
|
|
|
|
for c in train_cars(cart) do
|
|
|
|
mod.reverse_cart_direction(c)
|
|
|
|
c.behind,c.ahead = c.ahead,c.behind
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|