Page 1 of 1

[0.17.x] Determine if a ghost was on a tile when something was built

Posted: Mon May 13, 2019 2:26 am
by Impatient
Factorio is at version 0.17.38 and my question is this:

How can I determine if a ghost was on a tile, when something was built there? And what kind of ghost was that?

To detail and clarify the question: I am not interested in determining if an entity or tile was built BECAUSE of a ghost. I simply want to know if a ghost was on a tile before something was built there.

The context is this: When something is built I want to conditionally allow or disallow it. If it gets disallowed, I want to revert the condition of the tile to the state before something was built there - which also includes placing a ghost, if one was there before.

This question occoured to me here and I am interested in it because of this.

eradicator already gave me great advice, that when something was built by a robot (events on_robot_built_tile and on_robot_built_entity) there must have been a ghost of the same type there before. Naturally right? :-D

Now I am searching for a way to answer this question for the case when a player built something manually.

Re: [0.17.x] Determine if a ghost was on a tile when something was built

Posted: Mon May 13, 2019 2:53 am
by Impatient
An interesting event is on_put_item. Though from the API doc it seems it does not state what kind of item is being placed down. Which would be handy.

I am thinking of getting into the event chain before something is placed, use LuaSurface to find out if a and what kind of ghost exists where it is placed, remember that for a short while and use it for the case when building is disallowed and the state of the tile gets reverted.

What do you think?

Re: [0.17.x] Determine if a ghost was on a tile when something was built

Posted: Tue May 14, 2019 12:48 pm
by Impatient
find_entities_filtered
and
count_entities_filtered

can be used to find ghosts at a certain position, I guess.

so it could work like this:
1. subscribe to on_put_item and on_player_built_entity
2. On on_put_item event get the position, where something was put to, from it.
3. Use LuaSurface::find_entities_filtered(...) with that position, to find out if there is a ghost on that tile.
4. If YES, then store position and type of ghost in dictionary with position as key.
5. When on_player_built_entity is raised, do calculations to decide if building has to be reverted or not.
6. If YES, remove built entity.
7. Lookup if a ghost was stored for that position before. If YES, restore ghost.
8. Remove ghost type and position from dictionary.

Needs a test implementat now for proof of concept.

What search parameters do I have to use for find_entities_filtered if I want to find all ghosts at a position/in an area?

Re: [0.17.x] Determine if a ghost was on a tile when something was built

Posted: Tue May 14, 2019 12:55 pm
by eradicator
Impatient wrote: Tue May 14, 2019 12:48 pm What search parameters do I have to use for find_entities_filtered if I want to find all ghosts at a position/in an area?
type= "tile-ghost"
ghost_name="whatever your stuff is called" (i.e. "landfill")

Re: [0.17.x] Determine if a ghost was on a tile when something was built

Posted: Tue May 14, 2019 12:57 pm
by Impatient
Impatient wrote: Tue May 14, 2019 12:48 pm What search parameters do I have to use for find_entities_filtered if I want to find all ghosts at a position/in an area?
According to this very old post viewtopic.php?t=3875 , it needs

Code: Select all

type="ghost"
as parameter.

Re: [0.17.x] Determine if a ghost was on a tile when something was built

Posted: Tue May 14, 2019 12:59 pm
by Impatient
eradicator wrote: Tue May 14, 2019 12:55 pm
Impatient wrote: Tue May 14, 2019 12:48 pm What search parameters do I have to use for find_entities_filtered if I want to find all ghosts at a position/in an area?
type= "tile-ghost"
ghost_name="whatever your stuff is called" (i.e. "landfill")
++ and thx, like always eradicator

Re: [0.17.x] Determine if a ghost was on a tile when something was built

Posted: Tue May 14, 2019 1:05 pm
by eradicator
Impatient wrote: Tue May 14, 2019 12:59 pm
eradicator wrote: Tue May 14, 2019 12:55 pm
Impatient wrote: Tue May 14, 2019 12:48 pm What search parameters do I have to use for find_entities_filtered if I want to find all ghosts at a position/in an area?
type= "tile-ghost"
ghost_name="whatever your stuff is called" (i.e. "landfill")
++ and thx, like always eradicator
I was only 99% sure myself btw, so i just did "/sudo help whatis" and then "/sudo count_ents(10,it)". Because that's exactly the sort of thing i made /sudo for :p.