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

Place to get help with not working mods / modding interface.
Post Reply
User avatar
Impatient
Filter Inserter
Filter Inserter
Posts: 882
Joined: Sun Mar 20, 2016 2:51 am
Contact:

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

Post 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.

User avatar
Impatient
Filter Inserter
Filter Inserter
Posts: 882
Joined: Sun Mar 20, 2016 2:51 am
Contact:

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

Post 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?

User avatar
Impatient
Filter Inserter
Filter Inserter
Posts: 882
Joined: Sun Mar 20, 2016 2:51 am
Contact:

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

Post 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?

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

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

Post 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")
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.

User avatar
Impatient
Filter Inserter
Filter Inserter
Posts: 882
Joined: Sun Mar 20, 2016 2:51 am
Contact:

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

Post 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.

User avatar
Impatient
Filter Inserter
Filter Inserter
Posts: 882
Joined: Sun Mar 20, 2016 2:51 am
Contact:

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

Post 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

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

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

Post 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.
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.

Post Reply

Return to “Modding help”