pick up an random player built entity on whole map ?

Place to get help with not working mods / modding interface.
User avatar
binbinhfr
Smart Inserter
Smart Inserter
Posts: 1525
Joined: Sat Feb 27, 2016 7:37 pm
Contact:

pick up an random player built entity on whole map ?

Post by binbinhfr »

Hi,

I have a question of optimisation.
I want to write a function that picks up a random player built entity on the whole map, with a true random pattern (I mean : giving the same chance to any part of the map).
Of course, I want this function to be quicker as possible...
Complication (in a second time) : player built entity could be in other forces that the basic "player" force. So how to filter what is player built ?

We could use surface.get_chunks() and then surface.find_entities() but it always parse the chunks in the same order, and it could be quite CPU consuming on a big map...

Any ideas, my mod' brothers ? ;)
My mods on the Factorio Mod Portal :geek:
User avatar
Adil
Filter Inserter
Filter Inserter
Posts: 945
Joined: Fri Aug 15, 2014 8:36 pm
Contact:

Re: pick up an random player built entity on whole map ?

Post by Adil »

During on player_built, randomly insert entity into list or don't. Then randomly select entity from that list.
I do mods. Modding wiki is friend, it teaches how to mod. Api docs is friend too...
I also update mods, some of them even work.
Recently I did a mod tutorial.
User avatar
binbinhfr
Smart Inserter
Smart Inserter
Posts: 1525
Joined: Sat Feb 27, 2016 7:37 pm
Contact:

Re: pick up an random player built entity on whole map ?

Post by binbinhfr »

Adil wrote:During on player_built, randomly insert entity into list or don't. Then randomly select entity from that list.
Yes, nice idea, I thought about it, but when installing my mod on an existing map, already existing buildings won't be taken into account in next calls of find_random_entity()...
Moreover, if buildings of the random list are destroyed, and no more buildings created, the list will get shorter and shorter and maybe empty at the end...
So to work, this method should insert EVERY building in the "random" list... Which is heavy.
My mods on the Factorio Mod Portal :geek:
User avatar
Adil
Filter Inserter
Filter Inserter
Posts: 945
Joined: Fri Aug 15, 2014 8:36 pm
Contact:

Re: pick up an random player built entity on whole map ?

Post by Adil »

Well, that now depends on what exactly you plan to do with those. If it's some intensive destruction, then indeed list will get empty often.
0.13 will bring built_by (which will be nil if robots built that) field, but find_entities() over a sizable area is always slow.
If you want your mod be fast, you'll have to trade off with memory, speaking of which, an array of pointers is not that memory heavy (~64 bit per one), and you can do quite a lot with a list of thousand entities.

You won't be able to skip map scan if mod starts on game in process and you cannot make that search fast, unless you go make new interface request and that is granted.
(Well, you can also smear the search over several ticks, maybe that'll suffice to prevent stutter.)

Of course my in my previous post it is not finished solution, but by elaborating the addition algorithm and your list structure you may probably get something indistinguishable from the true random selection.
(For example, your addition algorithm may be more inclined to add new entity to the list when its short. Or you may separate your list into smaller ones on per-chunk (or few-chunk) basis and add entities more likely into the lists with fewer elements.)

May as well wrap that as separate standing mod for the case someone else is going to have randomly selected entities also.
I do mods. Modding wiki is friend, it teaches how to mod. Api docs is friend too...
I also update mods, some of them even work.
Recently I did a mod tutorial.
User avatar
binbinhfr
Smart Inserter
Smart Inserter
Posts: 1525
Joined: Sat Feb 27, 2016 7:37 pm
Contact:

Re: pick up an random player built entity on whole map ?

Post by binbinhfr »

Thx for your help. Infact I determined that what I was really needing is a list of locations where players built things (because I want to pop some events in these areas). So, to lighten things, I created a list of chunks that I populated with on_creation events. This is lighter than a list of all player/robots created entities.
But the fact is than I have to scan the existing map at the install of my mod, and that is quite long...

It's this damned find_entities. I wonder why there isn't a way to iterate through entities belonging to a force ?... (I mean without getting the whole list)
My mods on the Factorio Mod Portal :geek:
User avatar
DedlySpyder
Filter Inserter
Filter Inserter
Posts: 254
Joined: Fri Jun 20, 2014 11:42 am
Contact:

Re: pick up an random player built entity on whole map ?

Post by DedlySpyder »

binbinhfr wrote:It's this damned find_entities. I wonder why there isn't a way to iterate through entities belonging to a force ?... (I mean without getting the whole list)
You mean like find_entities_filtered?
User avatar
binbinhfr
Smart Inserter
Smart Inserter
Posts: 1525
Joined: Sat Feb 27, 2016 7:37 pm
Contact:

Re: pick up an random player built entity on whole map ?

Post by binbinhfr »

DedlySpyder wrote:You mean like find_entities_filtered?
Yes this one is the same : it gets the whole list at once, so it's CPU consumming. I'd prefer an iterator like surface.get_chunks for example.
My mods on the Factorio Mod Portal :geek:
Post Reply

Return to “Modding help”