LuaSurface.count_tiles_filtered{} return {name=count} mapping per tile type.

Place to ask discuss and request the modding support of Factorio. Don't request mods here.
Post Reply
User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

LuaSurface.count_tiles_filtered{} return {name=count} mapping per tile type.

Post by eradicator »

What?
A new option for LuaSurface.count_tiles_filtered{} that returns a list of counts by tile type in addition to the total count.

Details
For example LuaSurface.count_tiles_filtered{area=area,get_statistics=true} should return the total count as it does now but have a table as second return value:

Code: Select all

{
['grass-1'] = 411,
['sand-2'] = 95,
['water'] = 1254,
}
Usecases
a) Adapting newly placed tiles to their surroundings.
b) Modding machines that do different things depending on where they are placed. E.g. a ground extractor that extracts dirt, sand, rock etc depending on where it is placed. (i.e. an reskinned assembler with forced recipe.) or wind-turbines that work better near water, etc...

Why?
Currently the only way to get these numbers is to either call find_tiles_filtered and do the count myself, or to loop game.tile_prototypes and call count_tiles_filtered for each name. If count_tiles_filtered could directly return the numbers there would be less overhead.
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
Klonan
Factorio Staff
Factorio Staff
Posts: 5150
Joined: Sun Jan 11, 2015 2:09 pm
Contact:

Re: LuaSurface.count_tiles_filtered{} return {name=count} mapping per tile type.

Post by Klonan »

eradicator wrote:
Wed Nov 21, 2018 11:50 am

Why?
Currently the only way to get these numbers is to ... or to loop game.tile_prototypes and call count_tiles_filtered for each name.
Do you have some statistic on how long this takes?

Having a second return value that may not be used will slow it down for all usages

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

Re: LuaSurface.count_tiles_filtered{} return {name=count} mapping per tile type.

Post by eradicator »

Klonan wrote:
Wed Nov 21, 2018 1:44 pm
Having a second return value that may not be used will slow it down for all usages
I was hoping that if the second return value is only transfered to lua when requested it would be reasonably cheap. I've made some statistics and thought about it a bit more.

What i actually want/need:
a) A method to get per-type counts of all tiles in an area.
b) Same as a) but if a tile has a hidden_tile then the hidden_tile is counted instead of the surface tile, tiles without hidden_tile count normally.

The more i think about it the less sure i am what a good implementation would be (or if it's worth it to bother you :/). A completely new api function *cough*? Count_tiles with names={} instead of name=""?

Also for small radius scans manually counting find_tiles might be fast enough after all, and for large radius scans i only have one usecase so far (windpower) which could be tick-distributed (or ignore some tile types) to get it into "fast enough" regions.

Statistics Conclusion:
looping count_tiles:
+ fast with very large areas
- very slow with lots of modded tiles

manual counting find_tiles:
+ stable speed regardless of modded tiles
- slow for large areas in unmodded games

Data

Code: Select all

RESULTS (3 trials each,~5% spread):

  With alien biomes (192 tile types):
    1000 rounds, radius  6:
      manual_count_pairs  ~ 460ms --fastest   
      manual_count_forx   ~ 460ms  
      looped_count_pairs  ~1915ms 
    1000 rounds, radius 16:
      manual_count_pairs  ~3730ms --fastest
      manual_count_forx   ~3740ms
      looped_count_pairs  ~5300ms
    500  rounds, radius 32:
      manual_count_pairs  ~8620ms
      manual_count_forx   ~8650ms
      looped_count_pairs  ~8400ms --fastest
    100  rounds, radius 64:
      manual_count_pairs  ~7700ms
      manual_count_forx   ~7700ms
      looped_count_pairs  ~6270ms --fastest

  With base (35 tile types):
    1000 rounds, radius  6:
      manual_count_pairs  ~ 450ms
      manual_count_forx   ~ 460ms
      looped_count_pairs  ~ 344ms --fastest
    1000 rounds, radius 16:
      manual_count_pairs  ~3700ms
      manual_count_forx   ~3740ms
      looped_count_pairs  ~ 935ms --fastest
    500  rounds, radius 32:
      manual_count_pairs  ~8550ms
      manual_count_forx   ~8500ms
      looped_count_pairs  ~1505ms --fastest
    100  rounds, radius 64:
      manual_count_pairs  ~7520ms
      manual_count_forx   ~7620ms
      looped_count_pairs  ~1128ms --fastest

code
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 interface requests”