From b35342fa41d29524491106001e9361b145242377 Mon Sep 17 00:00:00 2001 From: Anton Simakov <67688115+GuardianDll@users.noreply.github.com> Date: Sat, 14 Sep 2024 16:13:16 +0200 Subject: [PATCH] add chance field for jmapgen_field (#76380) * add chance field for jmapgen_field * document field * astyle --- doc/MAPGEN.md | 1 + src/mapgen.cpp | 12 +++++++++--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/doc/MAPGEN.md b/doc/MAPGEN.md index 25f9739efbb94..faf79da78e8e2 100644 --- a/doc/MAPGEN.md +++ b/doc/MAPGEN.md @@ -856,6 +856,7 @@ Example: | intensity | (optional, integer, array ) how concentrated the field is, from 1 to 3 or more. Arrays are randomized. See `data/json/field_type.json` | age | (optional, integer) field age. Defaults to 0. | remove | (optional, bool) If true the given field will be removed rather than added. Defaults to false. +| chance | (optional, integer) chance to spawn field; default `100` as 100% ### Place NPCs with "npcs" diff --git a/src/mapgen.cpp b/src/mapgen.cpp index d7883c277c201..41a3e02dd8de9 100644 --- a/src/mapgen.cpp +++ b/src/mapgen.cpp @@ -1947,10 +1947,12 @@ class jmapgen_field : public jmapgen_piece mapgen_value ftype; std::vector intensities; time_duration age; + int chance; bool remove; jmapgen_field( const JsonObject &jsi, const std::string_view/*context*/ ) : ftype( jsi.get_member( "field" ) ) , age( time_duration::from_turns( jsi.get_int( "age", 0 ) ) ) + , chance( jsi.get_int( "chance", 100 ) ) , remove( jsi.get_bool( "remove", false ) ) { if( jsi.has_array( "intensity" ) ) { for( JsonValue jv : jsi.get_array( "intensity" ) ) { @@ -1968,10 +1970,14 @@ class jmapgen_field : public jmapgen_piece return; } if( remove ) { - dat.m.remove_field( tripoint_bub_ms( x.get(), y.get(), dat.zlevel() + z.get() ), chosen_id ); + if( x_in_y( chance, 100 ) ) { + dat.m.remove_field( tripoint_bub_ms( x.get(), y.get(), dat.zlevel() + z.get() ), chosen_id ); + } } else { - dat.m.add_field( tripoint_bub_ms( x.get(), y.get(), dat.zlevel() + z.get() ), chosen_id, - random_entry( intensities ), age ); + if( x_in_y( chance, 100 ) ) { + dat.m.add_field( tripoint_bub_ms( x.get(), y.get(), dat.zlevel() + z.get() ), chosen_id, + random_entry( intensities ), age ); + } } }