[Solved]How to create cargo pod?
Posted: Sat May 03, 2025 10:10 am
As you can see in the picture, the cargo is clearly "not reseved", but the next step of the game is to report an error that it's already reseved. Why?
Or is there a better way to launch a rocket (only need space platform -> surface)?
Key Code:
---- in function Public.examine_cargo_pods(platform, cargo_pods) ----
---- function get_available_cargo_pod_destination(pod) ----
---- function is_hatch_not_reserved(hatch) ----
Or is there a better way to launch a rocket (only need space platform -> surface)?
Key Code:
---- in function Public.examine_cargo_pods(platform, cargo_pods) ----
Code: Select all
available_cargo_pod_destination = Public.get_available_cargo_pod_destination(pod)
game.print("目的地状态:reserved=" .. tostring(available_cargo_pod_destination.hatch.reserved)
.. ", busy=" .. tostring(available_cargo_pod_destination.hatch.busy))
multi_pod.cargo_pod_destination = available_cargo_pod_destination -- it's line 184
Code: Select all
function Public.get_available_cargo_pod_destination(pod)
local pod_unit = "pod" .. tostring(pod.unit_number)
-- 火箭去处,CargoDestination
local pod_to_table = pod.cargo_pod_destination
-- 火箭目标物流接驳站,LuaEntity
local cargo_landing_pad = pod_to_table["station"]
-- 火箭目标类型,station=1,space_platform=2,surface=3,往星球落只有1或3
if pod_to_table["type"] == defines.cargo_destination.station then
if cargo_landing_pad and cargo_landing_pad.cargo_hatches then
game.print(pod_unit .. "目的地station,正常")
-- 如果目标为接驳站,那么需要遍历所有hatch以找到不是reserved状态的
-- 接驳站所有的货物接口(物流接驳站3个,扩展接驳站1个),array[LuaCargoHatch]
for _, hatch in pairs(cargo_landing_pad.cargo_hatches) do
-- 使用帧内预约系统
if hatch.valid and is_hatch_not_reserved(hatch) then
return {
type = defines.cargo_destination.station,
station = pod.cargo_pod_destination.station,
hatch = hatch,
transform_launch_products = pod.cargo_pod_destination.transform_launch_products,
surface = pod.cargo_pod_destination.surface,
position = pod.cargo_pod_destination.position,
land_at_exact_position = pod.cargo_pod_destination.land_at_exact_position,
space_platform = pod.cargo_pod_destination.space_platform,
}
end
end
game.print(pod_unit .. "找不到可用的接驳口")
return nil
else
game.print(pod_unit .. "目的地station,但cargo_landing_pad或cargo_landing_pad.cargo_hatches异常")
return nil
end
elseif pod_to_table["type"] == defines.cargo_destination.surface then
game.print(pod_unit .. "目的地surface,正常")
-- 如果目标为表面,那么直接使用旧pod的目标点位
return {
type = defines.cargo_destination.surface,
station = pod.cargo_pod_destination.station,
hatch = pod.cargo_pod_destination.hatch,
transform_launch_products = pod.cargo_pod_destination.transform_launch_products,
surface = pod.cargo_pod_destination.surface,
position = pod.cargo_pod_destination.position,
land_at_exact_position = pod.cargo_pod_destination.land_at_exact_position,
space_platform = pod.cargo_pod_destination.space_platform,
}
else
game.print(pod_unit .. "目的地" .. tostring(pod_to_table["type"] .. ",异常!"))
return nil
end
end
Code: Select all
function is_hatch_not_reserved(hatch)
if not hatch.valid or hatch.reserved or hatch.busy then
return false
end
local frame_reservations = storage.multiple_spaceship.frame_reservations
local key = hatch.owner.position
local val = game.tick
if frame_reservations[key] and frame_reservations[hatch.owner.position] > val - 60 then
return false
end
frame_reservations[key] = val
game.print("已预定位于(" .. key.x .. ", " .. key.y .. ")的货仓")
return true
end