Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(port): Implement GLOBALLY_UNIQUE #5723

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
21 changes: 19 additions & 2 deletions src/overmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5501,6 +5501,11 @@
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 @@ -5538,6 +5543,10 @@
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 @@ -5941,10 +5950,18 @@
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;
const overmap_special_placement_constraints &constraints = iter.special_details->get_constraints();

Check warning on line 5959 in src/overmap.cpp

View workflow job for this annotation

GitHub Actions / GCC 12, Ubuntu, Curses

unused variable ‘constraints’ [-Wunused-variable]

Check warning on line 5959 in src/overmap.cpp

View workflow job for this annotation

GitHub Actions / GCC 12, Ubuntu, Tiles, Sound, Lua, CMake, Languages

unused variable ‘constraints’ [-Wunused-variable]

Check warning on line 5959 in src/overmap.cpp

View workflow job for this annotation

GitHub Actions / GCC 12, Ubuntu, Tiles, Sound, Lua

unused variable ‘constraints’ [-Wunused-variable]
Copy link
Collaborator

@KheirFerrum KheirFerrum Nov 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where do these variables come into play? Remove, or incorporate them into the code. Check the Files Changed tab to check for these variables (I count at least 4).

It shouldn't really even have passed tests with unused variables. Did we disable that?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It shouldn't really even have passed tests with unused variables. Did we disable that?

lint for unused variables exists but it only emits warning because it often crashed on unrelated existing issues

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

speaking of "unrelated", should make it error again since #5548 is merged and there probably won't be unrelated changes anymore


int chance = roll_remainder( min * rate );

Check warning on line 5961 in src/overmap.cpp

View workflow job for this annotation

GitHub Actions / GCC 12, Ubuntu, Curses

unused variable ‘chance’ [-Wunused-variable]

Check warning on line 5961 in src/overmap.cpp

View workflow job for this annotation

GitHub Actions / GCC 12, Ubuntu, Tiles, Sound, Lua, CMake, Languages

unused variable ‘chance’ [-Wunused-variable]

Check warning on line 5961 in src/overmap.cpp

View workflow job for this annotation

GitHub Actions / GCC 12, Ubuntu, Tiles, Sound, Lua

unused variable ‘chance’ [-Wunused-variable]
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 @@ -208,6 +208,7 @@ void overmapbuffer::clear()
{
overmaps.clear();
known_non_existing.clear();
placed_unique_specials.clear();
last_requested_overmap = nullptr;
}

Expand Down Expand Up @@ -947,6 +948,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.find( id ) != placed_unique_specials.end();
}

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 @@ -518,6 +520,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 @@ -551,6 +556,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() );
}
}
Loading