Skip to content

Commit

Permalink
feat(port): Implement GLOBALLY_UNIQUE (#5723)
Browse files Browse the repository at this point in the history
* initial commit

Co-Authored-By: anothersimulacrum <[email protected]>

* style(autofix.ci): automated formatting

* Update specials.json

* Update overmap.cpp

* Update src/overmapbuffer.cpp

Co-authored-by: scarf <[email protected]>

---------

Co-authored-by: anothersimulacrum <[email protected]>
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: scarf <[email protected]>
  • Loading branch information
4 people authored Jan 8, 2025
1 parent 8bcd3b5 commit 591660d
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 4 deletions.
2 changes: 1 addition & 1 deletion data/json/overmap/overmap_special/aircraft_carrier.json
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,6 @@
"city_distance": [ -1, 1000 ],
"locations": [ "lake_surface" ],
"occurrences": [ 75, 100 ],
"flags": [ "CLASSIC", "LAKE", "MAN_MADE", "UNIQUE", "MILITARY", "ELECTRIC_GRID" ]
"flags": [ "CLASSIC", "LAKE", "MAN_MADE", "UNIQUE", "MILITARY", "ELECTRIC_GRID", "GLOBALLY_UNIQUE" ]
}
]
2 changes: 1 addition & 1 deletion data/json/overmap/overmap_special/specials.json
Original file line number Diff line number Diff line change
Expand Up @@ -1796,7 +1796,7 @@
"city_distance": [ 3, -1 ],
"city_sizes": [ 4, -1 ],
"occurrences": [ 50, 100 ],
"flags": [ "UNIQUE" ]
"flags": [ "ELECTRIC_GRID", "UNIQUE", "GLOBALLY_UNIQUE" ]
},
{
"type": "overmap_special",
Expand Down
20 changes: 18 additions & 2 deletions src/overmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5502,6 +5502,11 @@ bool overmap::can_place_special( const overmap_special &special, const tripoint_
return false;
}

if( special.has_flag( "GLOBALLY_UNIQUE" ) &&
overmap_buffer.contains_unique_special( special.id ) ) {
return false;
}

const std::vector<overmap_special_locations> fixed_terrains = special.required_locations();

return std::all_of( fixed_terrains.begin(), fixed_terrains.end(),
Expand Down Expand Up @@ -5539,6 +5544,10 @@ std::vector<tripoint_om_omt> overmap::place_special(
assert( can_place_special( special, p, dir, must_be_unexplored ) );
}

if( special.has_flag( "GLOBALLY_UNIQUE" ) ) {
overmap_buffer.add_unique_special( special.id );
}

const bool grid = special.has_flag( "ELECTRIC_GRID" );

special_placement_result result = special.place( *this, p, dir );
Expand Down Expand Up @@ -5942,10 +5951,17 @@ void overmap::place_specials( overmap_special_batch &enabled_specials )
const float rate = is_true_center && special.has_flag( "ENDGAME" ) ? 1 :
zone_ratio[current];

const bool unique = iter.special_details->has_flag( "UNIQUE" );
const bool globally_unique = iter.special_details->has_flag( "GLOBALLY_UNIQUE" );

int amount_to_place;
if( special.has_flag( "UNIQUE" ) ) {
if( unique || globally_unique ) {
const overmap_special_id &id = iter.special_details->id;

int chance = roll_remainder( min * rate );
amount_to_place = x_in_y( chance, max ) ? 1 : 0;
//FINGERS CROSSED EMOGI
amount_to_place = x_in_y( min, max ) && ( !globally_unique ||
!overmap_buffer.contains_unique_special( id ) ) ? 1 : 0;
} else {
// Number of instances normalized to terrain ratio
float real_max = std::max( static_cast<float>( min ), max * rate );
Expand Down
14 changes: 14 additions & 0 deletions src/overmapbuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ void overmapbuffer::clear()
{
overmaps.clear();
known_non_existing.clear();
placed_unique_specials.clear();
last_requested_overmap = nullptr;
}

Expand Down Expand Up @@ -939,6 +940,19 @@ bool overmapbuffer::check_overmap_special_type( const overmap_special_id &id,
return om_loc.om->check_overmap_special_type( id, om_loc.local );
}

void overmapbuffer::add_unique_special( const overmap_special_id &id )
{
if( contains_unique_special( id ) ) {
debugmsg( "Unique overmap special placed more than once: %s", id.str() );
}
placed_unique_specials.emplace( id );
}

bool overmapbuffer::contains_unique_special( const overmap_special_id &id ) const
{
return placed_unique_specials.contains( id );
}

static omt_find_params assign_params(
const std::string &type, int const radius, bool must_be_seen,
ot_match_type match_type, bool existing_overmaps_only,
Expand Down
23 changes: 23 additions & 0 deletions src/overmapbuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#include "coordinates.h"
#include "enums.h"
#include "json.h"
#include "memory_fast.h"
#include "overmap_types.h"
#include "point.h"
Expand Down Expand Up @@ -515,6 +517,9 @@ class overmapbuffer
// Cached result of previous call to overmapbuffer::get_existing
overmap mutable *last_requested_overmap;

// Set of globally unique overmap specials that have already been placed
std::unordered_set<overmap_special_id> placed_unique_specials;

/**
* Get a list of notes in the (loaded) overmaps.
* @param z only this specific z-level is search for notes.
Expand Down Expand Up @@ -548,6 +553,24 @@ class overmapbuffer
const tripoint_abs_omt &loc );
bool check_overmap_special_type_existing( const overmap_special_id &id,
const tripoint_abs_omt &loc );

/**
* Adds the given globally unique overmap special to the list of placed specials.
*/
void add_unique_special( const overmap_special_id &id );
/**
* Returns true if the given globally unique overmap special has already been placed.
*/
bool contains_unique_special( const overmap_special_id &id ) const;
/**
* Writes the placed unique specials as a JSON value.
*/
void serialize_placed_unique_specials( JsonOut &json ) const;
/**
* Reads placed unique specials from JSON and overwrites the global value.
*/
void deserialize_placed_unique_specials( JsonIn &jsin );

private:
/**
* Go thorough the monster groups of the overmap and move out-of-bounds
Expand Down
20 changes: 20 additions & 0 deletions src/savegame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "output.h"
#include "overmap.h"
#include "overmap_types.h"
#include "overmapbuffer.h"
#include "popup.h"
#include "regional_settings.h"
#include "scent_map.h"
Expand Down Expand Up @@ -1167,6 +1168,8 @@ void game::unserialize_master( std::istream &fin )
jsin.read( *faction_manager_ptr );
} else if( name == "seed" ) {
jsin.read( seed );
} else if( name == "placed_unique_specials" ) {
overmap_buffer.deserialize_placed_unique_specials( jsin );
} else if( name == "weather" ) {
JsonObject w = jsin.get_object();
w.read( "lightning", get_weather().lightning_active );
Expand Down Expand Up @@ -1202,6 +1205,9 @@ void game::serialize_master( std::ostream &fout )
json.member( "active_missions" );
mission::serialize_all( json );

json.member( "placed_unique_specials" );
overmap_buffer.serialize_placed_unique_specials( json );

json.member( "factions", *faction_manager_ptr );
json.member( "seed", seed );

Expand Down Expand Up @@ -1283,3 +1289,17 @@ void Creature_tracker::serialize( JsonOut &jsout ) const
}
jsout.end_array();
}

void overmapbuffer::serialize_placed_unique_specials( JsonOut &json ) const
{
json.write_as_array( placed_unique_specials );
}

void overmapbuffer::deserialize_placed_unique_specials( JsonIn &jsin )
{
placed_unique_specials.clear();
jsin.start_array();
while( !jsin.end_array() ) {
placed_unique_specials.emplace( jsin.get_string() );
}
}

0 comments on commit 591660d

Please sign in to comment.