API hooks into train pathfinding

Ideas that are too old (too many things have changed since) and ones which won't be implemented for certain reasons or if there are obviously better suggestions.

Moderator: ickputzdirwech

Post Reply
Joriom
Burner Inserter
Burner Inserter
Posts: 10
Joined: Wed May 07, 2014 9:39 pm
Contact:

API hooks into train pathfinding

Post by Joriom »

TL;DR
Way for modders to inject their pieces of code into train pathfinder or into the way train pathfinder graph is being built.
What ?
Most important case for me in this regard is adding ability for modders to convince pathfinding algorythm that two pieces of rail not connected physically are actually connected in some third party way - for example a teleport by an entity overseeing that piece of rail.
Why ?
There is already mod (although looks abbandoned) called Train Tunnels which works on basis of additional ("underground") game layer and "Tunel Entrances/Exits" (further called Interfaces) that teleport pieces of train between layers. I believe this mod is not without its flaws but with some improvements has potential to be great and fulfill dreams of many Factorio players about tunnels/viaducts. One of annoyances is inability of pahtfinder to actually consider those tunnels on its own. Currtnely, the Tunnel Interfaces are implemented as train stations that have to be added to train shedule without wait condition. Train reaches the Tunnel Interface and seeing lack of further tracks slows down but is still teleported to another surface. If modders would be a ble to convince pathfinder graph that end pieces of rail on two different layers are actually connected, it would be possible to make trains cross those tunnels without making a wierd trainstation workaround and would also allow trains to dynamically choose any tunnel, not just he one hardcoded in train shedule.
Video example
3:00 - 3:20 : working train tunnel (manual mode)
3:20 - 4:10 : explanation of automatic mode trainstation workaround for tunnels + display of working underground tunnel in automated mode
https://youtu.be/yVZi97ZCw4M?t=180

User avatar
boskid
Factorio Staff
Factorio Staff
Posts: 2248
Joined: Thu Dec 14, 2017 6:56 pm
Contact:

Re: API hools into train pathfinding

Post by boskid »

It is not going to be implemented, ever.
1/ Adding extra hooks to pathfinder would reduce performance a lot.
2/ Train pathfinder is limited to single surface.
3/ Nodes and edges are implicit by all entities placed on world, there is no simple way to attach here. It would require some rails to be marked as special and then it would complicate pathfinder logic a lot.
4/ Trains can not travel cross-surface
5/ Weird placement of nodes cross surface would violate distance heuristic and longer paths could be taken.

Joriom
Burner Inserter
Burner Inserter
Posts: 10
Joined: Wed May 07, 2014 9:39 pm
Contact:

Re: API hools into train pathfinding

Post by Joriom »

boskid wrote:
Thu Jan 30, 2020 9:51 am
It is not going to be implemented, ever.
4/ Trains can not travel cross-surface
I already see thats not true as per working mod Train Tunnels (also presented in a video I've attached).
https://mods.factorio.com/mod/traintunnels

This idea was already working, just needs some polishing.

User avatar
boskid
Factorio Staff
Factorio Staff
Posts: 2248
Joined: Thu Dec 14, 2017 6:56 pm
Contact:

Re: API hooks into train pathfinding

Post by boskid »

Quick look into code of linked mod. teleport.lua:

Code: Select all

local function teleportCarriage(trainToObserve, carriageIndex, sourceStop, targetStop, distance)
    local carriage = trainToObserve.carriages[tonumber(carriageIndex)]
    
    -- ...

    local entity = game.surfaces[targetStop.surface.index].create_entity{
        name = data.name,
        force = game.forces.player,
        snap_to_train_stop = false,
        position = {x=(sp.x + ox), y=sp.y + oy},
        direction = (targetStop.direction + data.is_flipped * 4) % 8
    }
    
    -- ...

    if entity ~= nil then
        -- game.print(entity.unit_number .. " - really am: " .. (entity.position.x) .. " direction: " .. entity.orientation)

--        entity.connect_rolling_stock(defines.rail_direction.front)
--        entity.connect_rolling_stock(defines.rail_direction.back)
        if data.driver ~= nil then
            local driver = carriage.get_driver().player.index;
            if entity.surface.index ~= data.driver.surface.index then
                game.players[driver].teleport(game.players[driver].position, entity.surface.index)
            end
            entity.set_driver(game.players[driver])
        end

        if data.color then
            entity.color = data.color
        end

        if data.health then
            entity.health = data.health
        end

        for inventory_id, inventory_data in pairs(data.inventories) do
            deserialize_inventory(entity.get_inventory(inventory_id), inventory_data)
        end

        if data.fluids then
            local fluidbox = entity.fluidbox
            for i = 1, #data.fluids do
                fluidbox[i] = data.fluids[i]
            end
        end

        if data.energy > 0 then
            entity.energy = data.energy
            if entity.burner then
                entity.burner.currently_burning = data.currently_burning
                entity.burner.remaining_burning_fuel = data.remaining_burning_fuel
            end
        end

        entity.train.schedule = trainToObserve.schedule

        local train = carriage.train
        local trainId = carriage.train.id
        carriage.destroy()
        script.raise_event(defines.events.script_raised_destroy, {train=train, trainId=trainId})

        --trainToObserve.carriages[tonumber(carriageIndex)] = entity
    else
        -- game.print("cant teleport")
        return false
    end

    return entity
end
They do not teleport. They are destroyed on old surface and created on new surface. This is illusion and you were fooled by it :)

Joriom
Burner Inserter
Burner Inserter
Posts: 10
Joined: Wed May 07, 2014 9:39 pm
Contact:

Re: API hooks into train pathfinding

Post by Joriom »

boskid wrote:
Thu Jan 30, 2020 9:58 am
They do not teleport. They are destroyed on old surface and created on new surface. This is illusion and you were fooled by it :)
What is teleportation if not desintegration and rebuilding exact copy at other location in real time? (thats why I would never want to be teleported in real life as its basically murder + clone)
Computer science is all about those "illusions". You're seeing numbers on your screen represented by colorfull lights and you are fooled you see a living world in front of your face. I find your argument invalid ab initio.
I actually don't mind if object is being destroyed and recreated, teleported, shifted to the dark realm where it fights for right to live and then moved back to different place in game... as long as it achieves certain objective of fooling the game that there is a train tunnel happening. Thats why in my suggestion I'm talking about "convincing" pathfinder (aka. cheating it) to think something is possible.

User avatar
boskid
Factorio Staff
Factorio Staff
Posts: 2248
Joined: Thu Dec 14, 2017 6:56 pm
Contact:

Re: API hooks into train pathfinding

Post by boskid »

Joriom wrote:
Thu Jan 30, 2020 10:09 am
What is teleportation if not desintegration and rebuilding exact copy at other location in real time? (thats why I would never want to be teleported in real life as its basically murder + clone)
Computer science is all about those "illusions". You're seeing numbers on your screen represented by colorfull lights and you are fooled you see a living world in front of your face. I find your argument invalid ab initio.
I actually don't mind if object is being destroyed and recreated, teleported, shifted to the dark realm where it fights for right to live and then moved back to different place in game... as long as it achieves certain objective of fooling the game that there is a train tunnel happening. Thats why in my suggestion I'm talking about "convincing" pathfinder (aka. cheating it) to think something is possible.
teleport would preserve entityId, trainId, targeters and some other things. clone+destroy does not do that.

There is no really good reason for us to implement cross surface train pathfinding. It is a lot of complications everywhere and rewriting half of trains code base only to consider trains could change surface during its movement. It is not going to be implemented.

Post Reply

Return to “Outdated/Not implemented”