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 2 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" ]
}
]
4 changes: 2 additions & 2 deletions 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 Expand Up @@ -5624,7 +5624,7 @@
"city_distance": [ 5, -1 ],
"city_sizes": [ 4, -1 ],
"occurrences": [ 10, 100 ],
"flags": [ "MILITARY", "UNIQUE", "ELECTRIC_GRID" ]
"flags": [ "MILITARY", "UNIQUE", "ELECTRIC_GRID", "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 @@ -2840,7 +2840,7 @@
copy.insert( nested.second );
std::optional<overmap_special_id> recures = check_recursion( nested.second, copy );
if( recures ) {
return *recures;

Check warning on line 2843 in src/overmap.cpp

View workflow job for this annotation

GitHub Actions / build

conversion from 'std::optional<string_id<overmap_special>>' into 'string_id<overmap_special>' and back into 'std::optional<string_id<overmap_special>>', remove potentially error-prone optional dereference [bugprone-optional-value-conversion]
}
}
}
Expand Down Expand Up @@ -3290,7 +3290,7 @@
map_layer &this_layer = layer[z + OVERMAP_DEPTH];
for( const auto &extra : this_layer.extras ) {
const std::string extra_text = extra.id.c_str();
if( match_include_exclude( extra_text, text ) ) {

Check warning on line 3293 in src/overmap.cpp

View workflow job for this annotation

GitHub Actions / build

1st argument 'extra_text' (passed to 'text') looks like it might be swapped with the 2nd, 'text' (passed to 'filter') [readability-suspicious-call-argument]
extra_locations.push_back( project_combine( pos(), extra.p ) );
}
}
Expand Down 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 @@ -5573,7 +5582,7 @@
auto points = connection_cache->get_closests( elem.connection->id, rp.z(), rp.xy() );
int attempts = 0;
for( const point_om_omt &pos : points ) {
if( ( linked = build_connection( pos, rp.xy(), rp.z(), *elem.connection,

Check warning on line 5585 in src/overmap.cpp

View workflow job for this annotation

GitHub Actions / build

an assignment within an 'if' condition is bug-prone [bugprone-assignment-in-if-condition]
must_be_unexplored, initial_dir ) ) ||
++attempts > 10 ) {
break;
Expand All @@ -5590,7 +5599,7 @@
!check_ot( elem.connection->id->default_terrain.str(), ot_match_type::type, p ) ) {
continue;
}
if( ( linked = build_connection( p.xy(), rp.xy(), rp.z(), *elem.connection,

Check warning on line 5602 in src/overmap.cpp

View workflow job for this annotation

GitHub Actions / build

an assignment within an 'if' condition is bug-prone [bugprone-assignment-in-if-condition]
must_be_unexplored, initial_dir ) ) ||
++attempts > 10 ) {
break;
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 / build

unused variable 'constraints' [clang-diagnostic-unused-variable]

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, 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 / build

unused variable 'chance' [clang-diagnostic-unused-variable]

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, 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 @@
{
overmaps.clear();
known_non_existing.clear();
placed_unique_specials.clear();
last_requested_overmap = nullptr;
}

Expand Down Expand Up @@ -660,7 +661,7 @@
void overmapbuffer::ter_set( const tripoint_abs_omt &p, const oter_id &id )
{
const overmap_with_local_coords om_loc = get_om_global( p );
return om_loc.om->ter_set( om_loc.local, id );

Check warning on line 664 in src/overmapbuffer.cpp

View workflow job for this annotation

GitHub Actions / build

return statement within a void function should not have a specified return value [readability-avoid-return-with-void-value]
}

std::string *overmapbuffer::join_used_at( const std::pair<tripoint_abs_omt, cube_direction> &p )
Expand Down Expand Up @@ -947,6 +948,19 @@
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 Expand Up @@ -1189,7 +1203,7 @@
const point_rel_om offset = end - start;

std::vector<overmap *> result;
result.reserve( ( offset.x() + 1 ) * ( offset.y() + 1 ) );

Check warning on line 1206 in src/overmapbuffer.cpp

View workflow job for this annotation

GitHub Actions / build

performing an implicit widening conversion to type 'size_type' (aka 'unsigned long') of a multiplication performed in type 'int' [bugprone-implicit-widening-of-multiplication-result]

for( int x = start.x(); x <= end.x(); ++x ) {
for( int y = start.y(); y <= end.y(); ++y ) {
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