Page 1 of 1
pick up an random player built entity on whole map ?
Posted: Sun Jun 19, 2016 12:26 pm
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 ?

Re: pick up an random player built entity on whole map ?
Posted: Sun Jun 19, 2016 12:50 pm
by Adil
During on player_built, randomly insert entity into list or don't. Then randomly select entity from that list.
Re: pick up an random player built entity on whole map ?
Posted: Sun Jun 19, 2016 1:07 pm
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.
Re: pick up an random player built entity on whole map ?
Posted: Mon Jun 20, 2016 7:52 pm
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.
Re: pick up an random player built entity on whole map ?
Posted: Mon Jun 20, 2016 8:42 pm
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)
Re: pick up an random player built entity on whole map ?
Posted: Tue Jun 21, 2016 2:50 pm
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?
Re: pick up an random player built entity on whole map ?
Posted: Tue Jun 21, 2016 3:15 pm
by binbinhfr
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.