Skip to content

Commit

Permalink
Refactor overmap-scale code to new point types (#41693)
Browse files Browse the repository at this point in the history
Put the new point types through their paces by changing the types for
overmap and overmapbuffer, and dealing with all the consequent fallout.

Involves more changes than I originally anticipated, but serves as a
good demonstration of the new point type features.

Fixed some bugs along the way that were discovered by this new point
type safety.  The ones I remember are:
* The evacuation shelter now correctly calculates the distance and
  direction to the refugee center, previously it was mostly nonsense
  (this only affects the message it prints out).
* Weather conditions for NPC-planted crops are calculated more
  reasonably.  Previously they were passing an overmap location as a
  map-square location, which could have caused all sorts of strangeness.
* Translocators were similarly confusing map_square and overmap_terrain
  scales, so they might not have been working properly.
* Two places were using map::points_in_radius which truncates to the map
  boundaries, but were using it for overmap-scale points, so it would
  only have worked correctly for some regions of the overmap.
  • Loading branch information
jbytheway authored Jul 20, 2020
1 parent e28e48a commit 3c67e80
Show file tree
Hide file tree
Showing 90 changed files with 2,200 additions and 1,895 deletions.
14 changes: 8 additions & 6 deletions src/activity_handlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3006,7 +3006,9 @@ void activity_handlers::travel_do_turn( player_activity *act, player *p )
return;
}
map &here = get_map();
tripoint sm_tri = here.getlocal( sm_to_ms_copy( omt_to_sm_copy( p->omt_path.back() ) ) );
// TODO: fix point types
tripoint sm_tri = here.getlocal(
project_to<coords::scale::map_square>( p->omt_path.back() ).raw() );
tripoint centre_sub = sm_tri + point( SEEX, SEEY );
if( !here.passable( centre_sub ) ) {
tripoint_range<tripoint> candidates = here.points_in_radius( centre_sub, 2 );
Expand Down Expand Up @@ -4336,16 +4338,16 @@ void activity_handlers::tree_communion_do_turn( player_activity *act, player *p
return;
}
// Breadth-first search forest tiles until one reveals new overmap tiles.
std::queue<tripoint> q;
std::unordered_set<tripoint> seen;
tripoint loc = p->global_omt_location();
std::queue<tripoint_abs_omt> q;
std::unordered_set<tripoint_abs_omt> seen;
tripoint_abs_omt loc = p->global_omt_location();
q.push( loc );
seen.insert( loc );
const std::function<bool( const oter_id & )> filter = []( const oter_id & ter ) {
return ter.obj().is_wooded() || ter.obj().get_name() == "field";
};
while( !q.empty() ) {
tripoint tpt = q.front();
tripoint_abs_omt tpt = q.front();
if( overmap_buffer.reveal( tpt, 3, filter ) ) {
if( p->has_trait( trait_SPIRITUAL ) ) {
p->add_morale( MORALE_TREE_COMMUNION, 2, 30, 8_hours, 6_hours );
Expand All @@ -4358,7 +4360,7 @@ void activity_handlers::tree_communion_do_turn( player_activity *act, player *p
}
return;
}
for( const tripoint &neighbor : points_in_radius( tpt, 1 ) ) {
for( const tripoint_abs_omt &neighbor : points_in_radius( tpt, 1 ) ) {
if( seen.find( neighbor ) != seen.end() ) {
continue;
}
Expand Down
2 changes: 1 addition & 1 deletion src/avatar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ void avatar::reset_all_misions()
failed_missions.clear();
}

tripoint avatar::get_active_mission_target() const
tripoint_abs_omt avatar::get_active_mission_target() const
{
if( active_mission == nullptr ) {
return overmap::invalid_tripoint;
Expand Down
2 changes: 1 addition & 1 deletion src/avatar.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ class avatar : public player
* Returns the target of the active mission or @ref overmap::invalid_tripoint if there is
* no active mission.
*/
tripoint get_active_mission_target() const;
tripoint_abs_omt get_active_mission_target() const;
/**
* Set which mission is active. The mission must be listed in @ref active_missions.
*/
Expand Down
18 changes: 10 additions & 8 deletions src/basecamp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ int base_camps::max_upgrade_by_type( const std::string &type )

basecamp::basecamp() = default;

basecamp::basecamp( const std::string &name_, const tripoint &omt_pos_ ): name( name_ ),
basecamp::basecamp( const std::string &name_, const tripoint_abs_omt &omt_pos_ ): name( name_ ),
omt_pos( omt_pos_ )
{
}
Expand All @@ -138,7 +138,8 @@ void basecamp::set_by_radio( bool access_by_radio )
// find the last underbar, strip off the prefix of faction_base_ (which is 13 chars),
// and the pull out the $TYPE and $CURLEVEL
// This is legacy support for existing camps; future camps don't use cur_level at all
expansion_data basecamp::parse_expansion( const std::string &terrain, const tripoint &new_pos )
expansion_data basecamp::parse_expansion( const std::string &terrain,
const tripoint_abs_omt &new_pos )
{
expansion_data e;
int last_bar = terrain.find_last_of( '_' );
Expand All @@ -148,7 +149,7 @@ expansion_data basecamp::parse_expansion( const std::string &terrain, const trip
return e;
}

void basecamp::add_expansion( const std::string &terrain, const tripoint &new_pos )
void basecamp::add_expansion( const std::string &terrain, const tripoint_abs_omt &new_pos )
{
if( terrain.find( base_camps::prefix ) == std::string::npos ) {
return;
Expand All @@ -162,7 +163,7 @@ void basecamp::add_expansion( const std::string &terrain, const tripoint &new_po
directions.push_back( dir );
}

void basecamp::add_expansion( const std::string &bldg, const tripoint &new_pos,
void basecamp::add_expansion( const std::string &bldg, const tripoint_abs_omt &new_pos,
const point &dir )
{
expansion_data e;
Expand All @@ -175,13 +176,13 @@ void basecamp::add_expansion( const std::string &bldg, const tripoint &new_pos,
update_resources( bldg );
}

void basecamp::define_camp( const tripoint &p, const std::string &camp_type )
void basecamp::define_camp( const tripoint_abs_omt &p, const std::string &camp_type )
{
query_new_name();
omt_pos = p;
const oter_id &omt_ref = overmap_buffer.ter( omt_pos );
// purging the regions guarantees all entries will start with faction_base_
for( const std::pair<std::string, tripoint> &expansion :
for( const std::pair<std::string, tripoint_abs_omt> &expansion :
talk_function::om_building_region( omt_pos, 1, true ) ) {
add_expansion( expansion.first, expansion.second );
}
Expand Down Expand Up @@ -665,7 +666,7 @@ void basecamp::form_crafting_inventory()
{
if( by_radio ) {
tinymap target_map;
target_map.load( tripoint( omt_pos.x * 2, omt_pos.y * 2, omt_pos.z ), false );
target_map.load( project_to<coords::scale::submap>( omt_pos ), false );
form_crafting_inventory( target_map );
} else {
form_crafting_inventory( get_map() );
Expand Down Expand Up @@ -749,7 +750,8 @@ void basecamp_action_components::consume_components()
map *target_map = &get_map();
if( base_.by_radio ) {
map_ = std::make_unique<tinymap>();
map_->load( omt_to_sm_copy( base_.camp_omt_pos() ), false );
// TODO: fix point types
map_->load( project_to<coords::scale::submap>( base_.camp_omt_pos() ).raw(), false );
target_map = map_.get();
}
const tripoint &origin = target_map->getlocal( base_.get_dumping_spot() );
Expand Down
31 changes: 16 additions & 15 deletions src/basecamp.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ struct expansion_data {
std::string type;
std::map<std::string, int> provides;
std::map<std::string, int> in_progress;
tripoint pos;
tripoint_abs_omt pos;
// legacy camp level, replaced by provides map and set to -1
int cur_level;

Expand Down Expand Up @@ -116,21 +116,21 @@ class basecamp
{
public:
basecamp();
basecamp( const std::string &name_, const tripoint &omt_pos );
basecamp( const std::string &name_, const tripoint_abs_omt &omt_pos );
basecamp( const std::string &name_, const tripoint &bb_pos_,
const std::vector<point> &directions_,
const std::map<point, expansion_data> &expansions_ );

inline bool is_valid() const {
return !name.empty() && omt_pos != tripoint_zero;
return !name.empty() && omt_pos != tripoint_abs_omt();
}
inline int board_x() const {
return bb_pos.x;
}
inline int board_y() const {
return bb_pos.y;
}
inline tripoint camp_omt_pos() const {
inline tripoint_abs_omt camp_omt_pos() const {
return omt_pos;
}
inline const std::string &camp_name() const {
Expand All @@ -151,18 +151,18 @@ class basecamp

std::string board_name() const;
std::vector<point> directions;
std::vector<tripoint> fortifications;
std::vector<tripoint_abs_omt> fortifications;
std::string name;
void faction_display( const catacurses::window &fac_w, int width ) const;

//change name of camp
void set_name( const std::string &new_name );
void query_new_name();
void abandon_camp();
void add_expansion( const std::string &terrain, const tripoint &new_pos );
void add_expansion( const std::string &bldg, const tripoint &new_pos,
void add_expansion( const std::string &terrain, const tripoint_abs_omt &new_pos );
void add_expansion( const std::string &bldg, const tripoint_abs_omt &new_pos,
const point &dir );
void define_camp( const tripoint &p, const std::string &camp_type = "default" );
void define_camp( const tripoint_abs_omt &p, const std::string &camp_type = "default" );

std::string expansion_tab( const point &dir ) const;
// upgrade levels
Expand All @@ -185,7 +185,8 @@ class basecamp
// confirm there is at least 1 loot destination and 1 unsorted loot zone in the camp
bool validate_sort_points();
// Validates the expansion data
expansion_data parse_expansion( const std::string &terrain, const tripoint &new_pos );
expansion_data parse_expansion( const std::string &terrain,
const tripoint_abs_omt &new_pos );
/**
* Invokes the zone manager and validates that the necessary sort zones exist.
*/
Expand Down Expand Up @@ -243,9 +244,9 @@ class basecamp
/// Provides a "guess" for some of the things your gatherers will return with
/// to upgrade the camp
std::string gathering_description( const std::string &bldg );
/// Returns a string for the number of plants that are harvestable, plots ready to plany,
/// Returns a string for the number of plants that are harvestable, plots ready to plant,
/// and ground that needs tilling
std::string farm_description( const tripoint &farm_pos, size_t &plots_count,
std::string farm_description( const tripoint_abs_omt &farm_pos, size_t &plots_count,
farm_ops operation );
/// Returns the description of a camp crafting options. converts fire charges to charcoal,
/// allows dark crafting
Expand Down Expand Up @@ -287,8 +288,8 @@ class basecamp
void start_fortifications( std::string &bldg_exp );
void start_combat_mission( const std::string &miss );
/// Called when a companion starts a chop shop @ref task mission
bool start_garage_chop( const point &dir, const tripoint &omt_tgt );
void start_farm_op( const point &dir, const tripoint &omt_tgt, farm_ops op );
bool start_garage_chop( const point &dir, const tripoint_abs_omt &omt_tgt );
void start_farm_op( const point &dir, const tripoint_abs_omt &omt_tgt, farm_ops op );
///Display items listed in @ref equipment to let the player pick what to give the departing
///NPC, loops until quit or empty.
std::vector<item *> give_equipment( std::vector<item *> equipment, const std::string &msg );
Expand Down Expand Up @@ -325,7 +326,7 @@ class basecamp
* @param omt_tgt the overmap pos3 of the farm_ops
* @param op whether to plow, plant, or harvest
*/
bool farm_return( const std::string &task, const tripoint &omt_tgt, farm_ops op );
bool farm_return( const std::string &task, const tripoint_abs_omt &omt_tgt, farm_ops op );
void fortifications_return();

void combat_mission_return( const std::string &miss );
Expand All @@ -347,7 +348,7 @@ class basecamp
void add_resource( const itype_id &camp_resource );
bool resources_updated = false;
// omt pos
tripoint omt_pos;
tripoint_abs_omt omt_pos;
std::vector<npc_ptr> assigned_npcs;
// location of associated bulletin board in abs coords
tripoint bb_pos;
Expand Down
28 changes: 16 additions & 12 deletions src/character.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "colony.h"
#include "construction.h"
#include "coordinate_conversions.h"
#include "coordinates.h"
#include "debug.h"
#include "disease.h"
#include "effect.h"
Expand Down Expand Up @@ -754,19 +755,21 @@ int Character::unimpaired_range() const
return std::min( sight_max, 60 );
}

bool Character::overmap_los( const tripoint &omt, int sight_points )
bool Character::overmap_los( const tripoint_abs_omt &omt, int sight_points )
{
const tripoint ompos = global_omt_location();
if( omt.x < ompos.x - sight_points || omt.x > ompos.x + sight_points ||
omt.y < ompos.y - sight_points || omt.y > ompos.y + sight_points ) {
const tripoint_abs_omt ompos = global_omt_location();
const point_rel_omt offset = omt.xy() - ompos.xy();
if( offset.x() < -sight_points || offset.x() > sight_points ||
offset.y() < -sight_points || offset.y() > sight_points ) {
// Outside maximum sight range
return false;
}

const std::vector<tripoint> line = line_to( ompos, omt, 0, 0 );
// TODO: fix point types
const std::vector<tripoint> line = line_to( ompos.raw(), omt.raw(), 0, 0 );
for( size_t i = 0; i < line.size() && sight_points >= 0; i++ ) {
const tripoint &pt = line[i];
const oter_id &ter = overmap_buffer.ter( pt );
const oter_id &ter = overmap_buffer.ter( tripoint_abs_omt( pt ) );
sight_points -= static_cast<int>( ter->get_see_cost() );
if( sight_points < 0 ) {
return false;
Expand Down Expand Up @@ -6782,9 +6785,10 @@ tripoint Character::global_sm_location() const
return ms_to_sm_copy( global_square_location() );
}

tripoint Character::global_omt_location() const
tripoint_abs_omt Character::global_omt_location() const
{
return ms_to_omt_copy( global_square_location() );
// TODO: fix point types
return tripoint_abs_omt( ms_to_omt_copy( global_square_location() ) );
}

bool Character::is_blind() const
Expand Down Expand Up @@ -9640,11 +9644,11 @@ void Character::apply_persistent_morale()
}
// Nomads get a morale penalty if they stay near the same overmap tiles too long.
if( has_trait( trait_NOMAD ) || has_trait( trait_NOMAD2 ) || has_trait( trait_NOMAD3 ) ) {
const tripoint ompos = global_omt_location();
const tripoint_abs_omt ompos = global_omt_location();
float total_time = 0;
// Check how long we've stayed in any overmap tile within 5 of us.
const int max_dist = 5;
for( const tripoint &pos : points_in_radius( ompos, max_dist ) ) {
for( const tripoint_abs_omt &pos : points_in_radius( ompos, max_dist ) ) {
const float dist = rl_dist( ompos, pos );
if( dist > max_dist ) {
continue;
Expand Down Expand Up @@ -10823,10 +10827,10 @@ void Character::place_corpse()
here.add_item_or_charges( pos(), body );
}

void Character::place_corpse( const tripoint &om_target )
void Character::place_corpse( const tripoint_abs_omt &om_target )
{
tinymap bay;
bay.load( tripoint( om_target.x * 2, om_target.y * 2, om_target.z ), false );
bay.load( project_to<coords::scale::submap>( om_target ), false );
point fin( rng( 1, SEEX * 2 - 2 ), rng( 1, SEEX * 2 - 2 ) );
// This makes no sense at all. It may find a random tile without furniture, but
// if the first try to find one fails, it will go through all tiles of the map
Expand Down
13 changes: 7 additions & 6 deletions src/character.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "character_id.h"
#include "character_martial_arts.h"
#include "color.h"
#include "coordinates.h"
#include "creature.h"
#include "damage.h"
#include "enums.h"
Expand Down Expand Up @@ -468,7 +469,7 @@ class Character : public Creature, public visitable<Character>
/** Returns the player maximum vision range factoring in mutations, diseases, and other effects */
int unimpaired_range() const;
/** Returns true if overmap tile is within player line-of-sight */
bool overmap_los( const tripoint &omt, int sight_points );
bool overmap_los( const tripoint_abs_omt &omt, int sight_points );
/** Returns the distance the player can see on the overmap */
int overmap_sight_range( int light_level ) const;
/** Returns the distance the player can see through walls */
Expand Down Expand Up @@ -931,7 +932,7 @@ class Character : public Creature, public visitable<Character>
/**
* Returns the location of the player in global overmap terrain coordinates.
*/
tripoint global_omt_location() const;
tripoint_abs_omt global_omt_location() const;

private:
/** Retrieves a stat mod of a mutation. */
Expand Down Expand Up @@ -1089,7 +1090,7 @@ class Character : public Creature, public visitable<Character>
/**Get stat bonus from bionic*/
int get_mod_stat_from_bionic( const character_stat &Stat ) const;
// route for overmap-scale traveling
std::vector<tripoint> omt_path;
std::vector<tripoint_abs_omt> omt_path;

/** Handles bionic effects over time of the entered bionic */
void process_bionic( int b );
Expand Down Expand Up @@ -1728,7 +1729,7 @@ class Character : public Creature, public visitable<Character>
cata::optional<tripoint> last_target_pos;
// Save favorite ammo location
item_location ammo_location;
std::set<tripoint> camps;
std::set<tripoint_abs_omt> camps;
/* crafting inventory cached time */
time_point cached_time;

Expand Down Expand Up @@ -1966,7 +1967,7 @@ class Character : public Creature, public visitable<Character>
// Put corpse+inventory on map at the place where this is.
void place_corpse();
// Put corpse+inventory on defined om tile
void place_corpse( const tripoint &om_target );
void place_corpse( const tripoint_abs_omt &om_target );

/** Returns the player's modified base movement cost */
int run_cost( int base_cost, bool diag = false ) const;
Expand Down Expand Up @@ -2568,7 +2569,7 @@ class Character : public Creature, public visitable<Character>
mutable decltype( _skills ) valid_autolearn_skills;

/** Amount of time the player has spent in each overmap tile. */
std::unordered_map<point, time_duration> overmap_time;
std::unordered_map<point_abs_omt, time_duration> overmap_time;

public:
time_point next_climate_control_check;
Expand Down
Loading

0 comments on commit 3c67e80

Please sign in to comment.