-- new stuff script.on_init(function () if not global then global = {} end if not global.terrains then global.terrains = {} end if not global.terrain_names then global.terrain_names = {} end if not global.tree_names then global.tree_names = {} end if not global.export_must_be_updated then global.export_must_be_updated = false end end) script.on_event(defines.events.on_chunk_generated, function (event) chunk_generated_handler(event) end) function chunk_generated_handler(event) --area :: BoundingBox: Area of the chunk --surface :: LuaSurface: The surface the chunk is on --Area example: {{-2, -3}, {5, 8}} is left_top and right_bottom position local area = event.area local surface = event.surface local entities = surface.find_entities_filtered{area=area, type="tree"} -- don't forget surface! if (not entities) or (#entities == 0) then return end for i, entity in pairs (entities) do search_tiles_by_this_entity_handler(entity) end --printAll(global.terrains) --printAll("We have " .. table_size (global.terrains) .. " terrains") if global.export_must_be_updated then global.export_must_be_updated = false export_iterator() end end function export_iterator() -- reset file game.write_file("terrains.txt", "Table of terrains \n", false) for i, terrain in pairs (global.terrains) do string_to_file(terrain, true) end end function search_tiles_by_this_entity_handler(entity) local tile = entity.surface.get_tile (entity.position) add_tree_name_to_terrain_table (global.terrains, entity.name, tile.name) end function add_tree_name_to_terrain_table (table, tree_name, terrain_name) -- table must be global.terrains or what you use if table[terrain_name] then local was_added = add_tree_name_to_table_as_last (table[terrain_name].trees, tree_name) -- (table, value) if was_added then -- example of process, no writing to the file, but printing string_to_file(table[terrain_name], false) global.export_must_be_updated = true end else printAll("New terrain! It is " .. terrain_name) table[terrain_name] = {name = terrain_name, trees = {tree_name}} -- example of process, no writing to the file, but printing string_to_file(table[terrain_name], false) global.export_must_be_updated = true end end function add_tree_name_to_table_as_last (table, value) if (not table) or (not value) then return false end local cash_i = 0 for i, v in pairs (table) do cash_i = i if v == value then return false -- we have this already! end end table[cash_i + 1] = value return true end function table_size (table) if (not table) then return 0 end local cash_i = 0 for i, v in pairs (table) do cash_i = cash_i + 1 end return cash_i end function string_to_file(terrain_tabl, bool) -- terrains["grass-medium"] = {name = "grass-medium", trees = {"tree-04"}} local prefix = 'terrains["' .. terrain_tabl.name .. '"]' local suffix = serpent.block(terrain_tabl, {comment = false}) local text_to_export = prefix .. " = " .. suffix .. ", \n" if not bool then printAll(text_to_export) end if bool then game.write_file("terrains.txt", text_to_export, true) end end function printAll(text) if type(text) == "table" then text = serpent.block(text, {comment = false}) end for player_index, player in pairs (game.players) do game.players[player_index].print (text) end end