[Solved]How to create cargo pod?

Place to get help with not working mods / modding interface.
MengLei
Burner Inserter
Burner Inserter
Posts: 5
Joined: Sun Nov 12, 2023 1:49 pm
Contact:

[Solved]How to create cargo pod?

Post by MengLei »

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) ----

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
---- function get_available_cargo_pod_destination(pod) ----

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
---- function is_hatch_not_reserved(hatch) ----

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
Attachments
83958962-6e31-4aff-ad03-2dbc1f5d37ba.png
83958962-6e31-4aff-ad03-2dbc1f5d37ba.png (7.36 MiB) Viewed 106 times
2832314bc5d1b269edc7f3f9b2819089.png
2832314bc5d1b269edc7f3f9b2819089.png (432.11 KiB) Viewed 106 times
7f2214b6e14099f24b14925ec25451fc.png
7f2214b6e14099f24b14925ec25451fc.png (1.62 MiB) Viewed 106 times
Last edited by MengLei on Mon May 05, 2025 2:26 am, edited 1 time in total.
MengLei
Burner Inserter
Burner Inserter
Posts: 5
Joined: Sun Nov 12, 2023 1:49 pm
Contact:

Re: How to create cargo pod?

Post by MengLei »

It's been resolved.
Turns out you don't need to specify hatch at all, just station or surface!

If spaceship to surface:

Code: Select all

                        local multi_pod_destination = nil
                        if pod.cargo_pod_destination["type"] == defines.cargo_destination.station then
                            multi_pod_destination = {
                                type = defines.cargo_destination.station,
                                station = pod.cargo_pod_destination.station,
                                hatch = nil, -- do NOT set hatch!!!
                                transform_launch_products = pod.cargo_pod_destination.transform_launch_products,
                            }
                            -- or you can use surface:
                            -- multi_pod_destination = {
                            --     type = defines.cargo_destination.surface,
                            --     transform_launch_products = false,
                            --     surface = pod.cargo_pod_destination.station.surface,
                            --     position = nil,
                            --     land_at_exact_position = false,
                            -- }
                        else
                            multi_pod_destination = {
                                type = defines.cargo_destination.surface,
                                transform_launch_products = false,
                                surface = pod.cargo_pod_destination.surface,
                                position = nil,
                                land_at_exact_position = false,
                            }
                        end
Post Reply

Return to “Modding help”