Page 1 of 1

How to find the neighbours of a ghost underground belt and how to get the player who created the ghost-entity?

Posted: Sun Mar 10, 2019 1:52 pm
by realvictorprm
Hi :)

I'm trying to fix some issues around placing the underground belts of the mod subterrain.

Some small explanation prior.
Subterrain belt's are belts which could be indefinitely long currently limited to 250 tiles.
If you place them the mod removes the amount of belts needed for the distance, e.g. if it's 50 tiles long you'll loose 50 belts (of the same tier as the belt is).
Moreover if you remove one part of the underground belt you'll regain the belts you spend for building it.

Right now this works only well if the player places the underground belts, I did not find a way for doing this with robots yet because I ran into some issues.

If such belts are placeable as ghost the question is now, when do we remove the belts?
Two solutions I can think of are:
1. Somehow tell factorio that the entity requires not only the subterrain belt, it requires belts too.
2. Claim the belts even if it's only placed as ghost

However in both solutions I ran into the issues:
1. I can't acquire the player who "made" the ghost entity
2. I can't acquire the player from who the robot is from so I could claim belts or give back belts.
3. I can't acquire the neighbour of a ghost underground belt (accessing the field results in a runtime error).

Is there anything you can think of as possible solution to this?

Re: How to find the neighbours of a ghost underground belt and how to get the player who created the ghost-entity?

Posted: Fri Apr 05, 2019 3:40 am
by Therax
The on_built_entity event has player_index when an entity-ghost is created, so you should be able to instantly charge the cost. However, this is pretty inconsistent with how ghosts normally work, so I'd suggest an alternate approach.

I did something like this for an early version of Beltlayer, by using an invisible chest and an item-request-proxy. The upside is that the entire job of bringing materials for construction is handled by bots, and you don't have to worry about tracking the player who built the ghost. On the other hand, it was really complicated and prone to breaking when the player did unexpected things. It would take a lot of work to make it foolproof.

When an underground belt ghost is created:
1) Find its other end, which probably will have to be done manually using find_entities_filtered, since the other end may be a ghost, not yet exist, or be a real underground belt entity.
2) Calculate length, and replace the underground-belt ghost with a ghost for a container entity.
3) Set the container entity ghost's item_requests for the correct number of belts. Make sure the custom container has enough space.

When a ghost for the container entity is revived (on_built_entity, script_raised_revive, etc.)
4) Make a record of the container in global somewhere.
5) In an on_nth_tick handler, periodically check if the container has enough belts in it to "complete" the underground connection.
6) If it does, destroy the container and its contents, and replace it with an actual underground-belt entity connected with the other end.

Like I said, this is all fairly messy and complicated, and there are other weird corner cases that arise. What if the player fills the chest with wood? What if the player fills the chest with 10x the amount of belts needed?