Page 1 of 1

LuaGameScript.evaluate_noise_expression(Position, NoiseExpression, MapGenSettings)

Posted: Sat Mar 16, 2019 1:58 pm
by Earendel
It would be useful to have the ability to evaluate a noise expression for a given position.

The proposed format is:

LuaGameScript.evaluate_noise_expression(Position, NoiseExpression, MapGenSettings)

MapGenSettings would be optional and default to LuaGameScript.default_map_gen_settings

We could then do things like:

Code: Select all

game.evaluate_noise_expression(
    {10,-53}, 
    game.tile_prototypes["water"].autoplace_specification.probability_expression, 
    game.surfaces[2].map_gen_settings)

Code: Select all

game.evaluate_noise_expression(
    {0,0}, 
    {expression_id = "variable:elevation", type = "variable", variable_name = "elevation" }, 
    {seed = 1, water=1.01})
We could use this for things like:
  • Debugging terrain generation, the actual climate value at a location.
  • Gradually changing the terrain - use elevation values to determines what gets flooded or dried up.
  • Evaluating an arbitrary expression for script-generated stuff in noise patterns.

Re: LuaGameScript.evaluate_noise_expression(Position, NoiseExpression, MapGenSettings)

Posted: Thu Jun 20, 2019 6:07 pm
by TOGoS
Earendel wrote:
Sat Mar 16, 2019 1:58 pm
The proposed format is:

LuaGameScript.evaluate_noise_expression(Position, NoiseExpression, MapGenSettings)

MapGenSettings would be optional and default to LuaGameScript.default_map_gen_settings
I've been thinking on this subject this week. I had a slightly different API in mind, though it would accomplish the exact same thing:

Code: Select all

surface:vector_evaluate_noise_expression(noise_expression, input_vectors}
Where 'input_vectors' would be something like

Code: Select all

{x={1,2,3,4}, y={5,6,7,8)
The advantage being that the surface has an already-compiled version of the map gen settings, and we can amortize the cost of recompiling the expression argument over a bunch of x, y inputs. But that's all kind of internal details about the system that I don't want mods to have to care about. And also it would be nice to have a function to evaluate self-contained expressions without needing to reference the surface, as your proposal allows.

Which is all to say I'll need to prototype something.

Re: LuaGameScript.evaluate_noise_expression(Position, NoiseExpression, MapGenSettings)

Posted: Fri Jun 21, 2019 1:24 am
by Earendel
Yeah that would work.

Re: LuaGameScript.evaluate_noise_expression(Position, NoiseExpression, MapGenSettings)

Posted: Mon Jun 24, 2019 10:16 pm
by TOGoS
Coming up with an API for this that is clean, relatively efficient, and not totally at odds with how things work internally is turning out to be a challenge. The easiest thing to implement would be a method on LuaSurface that takes a list of property names and X,Y inputs. This would sort of allow the 'changing elevation' case you mention (by actually changing the water level on a surface, and then going back over existing chunks and moving water tiles around), but would not allow arbitrary generated-at-runtime noise expressions to be evaluated. Maybe there should be a separate, known-to-be-more-expensive API for that? Or maybe we should (WARNING: QUESTIONABLE HACK) allow noise expressions to be named by their JSON representation, which would allow one to create dummy surfaces with those noise expressions and then query them for their tile properties.

Re: LuaGameScript.evaluate_noise_expression(Position, NoiseExpression, MapGenSettings)

Posted: Tue Jun 25, 2019 11:16 pm
by Earendel
TOGoS wrote:
Mon Jun 24, 2019 10:16 pm
a method on LuaSurface that takes a list of property names and X,Y inputs.
I'm not sure what exactly the property names would refer to so it is difficult to say, but if it allows reading of elevation, temperature, plus things like autoplace probability_expression, richness_expression for a specific prototype then I think that is all that is needed.
TOGoS wrote:
Mon Jun 24, 2019 10:16 pm
allow noise expressions to be named by their JSON representation, which would allow one to create dummy surfaces with those noise expressions and then query them for their tile properties.
That sounds like a good option.

The 2 types of things that will probably be accessed are:
A) climate-type values such as elevation, moisture, temperature. These could be specified by string or maybe by defines.
B) NoiseExpressions that are already defined as part of autoplace specifications https://lua-api.factorio.com/latest/Con ... cification so we'd probably feed that into a function by specifying something like game.tile_prototypes["water"].autoplace_specification.probability_expression or game.tile_prototypes["water"].autoplace_specification.probability_expression_name (does not exist yet) if a different type of name is needed.

Type B is more important because if needed a prototype autoplace expression could be created that just returns elevation, moisture or temperature.

Re: LuaGameScript.evaluate_noise_expression(Position, NoiseExpression, MapGenSettings)

Posted: Wed Jun 26, 2019 11:12 pm
by TOGoS
If all goes well this should be available in 0.17.53.

Probability and richness of any tile, entity, or decorative, and any tile property referenced by them (e.g. elevation, temperature, moisture, cliffiness) or explicitly listed in map gen settings.property_expression_names is available to be queried using surface.calculate_tile_properties, like so:

Code: Select all

local positions = {{1.5,1.7}, {2.0, 2.5}, {34.8, -20.8}}
local property_names = {
  "elevation",
  "temperature",
  "tile:deepwater:probability",
  "tile:grass:probability"
}
local calcresult = game.surfaces[1].calculate_tile_properties(property_names, positions)
for propname,values in pairs(calcresult) do
  game.player.print(propname..":")
  for i,v in ipairs(values) do
    game.player.print("  "..positions[i][1]..","..positions[i][2]..": "..v)
  end
end

Re: LuaGameScript.evaluate_noise_expression(Position, NoiseExpression, MapGenSettings)

Posted: Thu Jun 27, 2019 9:25 am
by Earendel
That's amazing, it will make so many new things possible.

Thanks.