Skip to content

Commit

Permalink
made zones typed
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrikLundell committed Dec 20, 2024
1 parent 41efa55 commit 1bdfcd0
Show file tree
Hide file tree
Showing 13 changed files with 301 additions and 157 deletions.
6 changes: 2 additions & 4 deletions src/activity_item_handling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1500,8 +1500,7 @@ static std::vector<std::tuple<tripoint_bub_ms, itype_id, int>> requirements_map(
already_there_spots.push_back( elem );
combined_spots.push_back( elem );
}
// TODO: fix point types
for( const tripoint &elem : mgr.get_point_set_loot(
for( const tripoint_bub_ms &elem : mgr.get_point_set_loot(
you.get_location(), distance, you.is_npc(), _fac_id( you ) ) ) {
// if there is a loot zone that's already near the work spot, we don't want it to be added twice.
if( std::find( already_there_spots.begin(), already_there_spots.end(),
Expand Down Expand Up @@ -2804,8 +2803,7 @@ static requirement_check_result generic_multi_activity_check_requirement(
requirement_id what_we_need;
std::vector<tripoint_bub_ms> loot_zone_spots;
std::vector<tripoint_bub_ms> combined_spots;
// TODO: fix point types
for( const tripoint &elem : mgr.get_point_set_loot(
for( const tripoint_bub_ms &elem : mgr.get_point_set_loot(
abspos, ACTIVITY_SEARCH_DISTANCE, you.is_npc(), _fac_id( you ) ) ) {
loot_zone_spots.emplace_back( elem );
combined_spots.emplace_back( elem );
Expand Down
182 changes: 124 additions & 58 deletions src/clzones.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -670,13 +670,19 @@ bool zone_data::set_type()
return false;
}

void zone_data::set_position( const std::pair<tripoint, tripoint> &position,
void zone_data::set_position( const std::pair<tripoint_abs_ms, tripoint_abs_ms> &position,
const bool manual, bool update_avatar, bool skip_cache_update, bool suppress_display_update )
{
if( is_vehicle && manual ) {
debugmsg( "Tried moving a lootzone bound to a vehicle part" );
return;
}

if( is_personal ) {
debugmsg( "Tried moving a personal lootzone to absolute location" );
return;
}

bool adjust_display = is_displayed && !suppress_display_update;

if( adjust_display ) {
Expand All @@ -695,6 +701,37 @@ void zone_data::set_position( const std::pair<tripoint, tripoint> &position,
}
}

void zone_data::set_position( const std::pair<tripoint_rel_ms, tripoint_rel_ms> &position,
const bool manual, bool update_avatar, bool skip_cache_update, bool suppress_display_update )
{
if( is_vehicle && manual ) {
debugmsg( "Tried moving a lootzone bound to a vehicle part" );
return;
}

if( !is_personal ) {
debugmsg( "Tried moving a normal lootzone to relative location" );
return;
}

bool adjust_display = is_displayed && !suppress_display_update;

if( adjust_display ) {
toggle_display();
}

personal_start = position.first;
personal_end = position.second;

if( adjust_display ) {
toggle_display();
}

if( !skip_cache_update ) {
zone_manager::get_manager().cache_data( update_avatar );
}
}

void zone_data::set_enabled( const bool enabled_arg )
{
zone_manager::get_manager().zone_edited( *this );
Expand Down Expand Up @@ -792,28 +829,32 @@ void zone_data::toggle_display()
// Take care of the situation where parts of overlapping zones were erased by the toggling.
if( !is_displayed ) {

const point_abs_ms start = point_abs_ms( std::min( this->start.x, this->end.x ),
std::min( this->start.y, this->end.y ) );
const point_abs_ms start = { std::min( this->start.x(), this->end.x() ),
std::min( this->start.y(), this->end.y() )
};

const point_abs_ms end = point_abs_ms( std::max( this->start.x, this->end.x ),
std::max( this->start.y, this->end.y ) );
const point_abs_ms end = { std::max( this->start.x(), this->end.x() ),
std::max( this->start.y(), this->end.y() )
};

const inclusive_rectangle<point_abs_ms> zone_rectangle( start, end );

for( zone_manager::ref_zone_data zone : zone_manager::get_manager().get_zones() ) {
// Assumes zones are a single Z level only. Also, inclusive_cuboid doesn't have an overlap function...
if( zone.get().get_is_displayed() && zone.get().get_type() == this->get_type() &&
this->start.z == zone.get().get_start_point().z() ) {
this->start.z() == zone.get().get_start_point().z() ) {
const point_abs_ms candidate_begin = zone.get().get_start_point().xy();
const point_abs_ms candidate_stop = zone.get().get_end_point().xy();

const point_abs_ms candidate_start = point_abs_ms( std::min( candidate_begin.x(),
candidate_stop.x() ),
std::min( candidate_begin.y(), candidate_stop.y() ) );
const point_abs_ms candidate_start = { std::min( candidate_begin.x(),
candidate_stop.x() ),
std::min( candidate_begin.y(), candidate_stop.y() )
};

const point_abs_ms candidate_end = point_abs_ms( std::max( candidate_begin.x(),
candidate_stop.x() ),
std::max( candidate_begin.y(), candidate_stop.y() ) );
const point_abs_ms candidate_end = { std::max( candidate_begin.x(),
candidate_stop.x() ),
std::max( candidate_begin.y(), candidate_stop.y() )
};

const inclusive_rectangle<point_abs_ms> candidate_rectangle( candidate_start, candidate_end );

Expand Down Expand Up @@ -945,24 +986,24 @@ std::unordered_set<tripoint_abs_ms> zone_manager::get_point_set( const zone_type
return type_iter->second;
}

std::unordered_set<tripoint> zone_manager::get_point_set_loot( const tripoint_abs_ms &where,
std::unordered_set<tripoint_bub_ms> zone_manager::get_point_set_loot( const tripoint_abs_ms &where,
int radius, const faction_id &fac ) const
{
return get_point_set_loot( where, radius, false, fac );
}

std::unordered_set<tripoint> zone_manager::get_point_set_loot( const tripoint_abs_ms &where,
std::unordered_set<tripoint_bub_ms> zone_manager::get_point_set_loot( const tripoint_abs_ms &where,
int radius, bool npc_search, const faction_id &fac ) const
{
std::unordered_set<tripoint> res;
std::unordered_set<tripoint_bub_ms> res;
map &here = get_map();
for( const std::pair<std::string, std::unordered_set<tripoint_abs_ms>> cache : area_cache ) {
zone_type_id type = zone_data::unhash_type( cache.first );
faction_id z_fac = zone_data::unhash_fac( cache.first );
if( fac == z_fac && type.str().substr( 0, 4 ) == "LOOT" ) {
for( tripoint_abs_ms point : cache.second ) {
if( square_dist( where, point ) <= radius ) {
res.emplace( here.bub_from_abs( point ).raw() );
res.emplace( here.bub_from_abs( point ) );
}
}
}
Expand All @@ -973,7 +1014,7 @@ std::unordered_set<tripoint> zone_manager::get_point_set_loot( const tripoint_ab
if( fac == z_fac && type.str().substr( 0, 4 ) == "LOOT" ) {
for( tripoint_abs_ms point : cache.second ) {
if( square_dist( where, point ) <= radius ) {
res.emplace( here.bub_from_abs( point ).raw() );
res.emplace( here.bub_from_abs( point ) );
}
}
}
Expand All @@ -984,7 +1025,7 @@ std::unordered_set<tripoint> zone_manager::get_point_set_loot( const tripoint_ab
zone_type_id type = zone_data::unhash_type( cache.first );
if( type == zone_type_NO_NPC_PICKUP ) {
for( tripoint_abs_ms point : cache.second ) {
res.erase( here.bub_from_abs( point ).raw() );
res.erase( here.bub_from_abs( point ) );
}
}
}
Expand Down Expand Up @@ -1375,7 +1416,7 @@ const zone_data *zone_manager::get_bottom_zone(
// which constructor of the key-value pair we use which depends on new_zone being an rvalue or lvalue and constness.
// If you are passing new_zone from a non-const iterator, be prepared for a move! This
// may break some iterators like map iterators if you are less specific!
void zone_manager::create_vehicle_loot_zone( vehicle &vehicle, const point &mount_point,
void zone_manager::create_vehicle_loot_zone( vehicle &vehicle, const point_rel_ms &mount_point,
zone_data &new_zone, map *pmap )
{
//create a vehicle loot zone
Expand All @@ -1388,38 +1429,47 @@ void zone_manager::create_vehicle_loot_zone( vehicle &vehicle, const point &moun
}

void zone_manager::add( const std::string &name, const zone_type_id &type, const faction_id &fac,
const bool invert, const bool enabled, const tripoint &start,
const tripoint &end, const shared_ptr_fast<zone_options> &options, const bool personal,
const bool invert, const bool enabled, const tripoint_abs_ms &start,
const tripoint_abs_ms &end, const shared_ptr_fast<zone_options> &options,
bool silent, map *pmap )
{
map &here = pmap == nullptr ? get_map() : *pmap;
zone_data new_zone = zone_data( name, type, fac, invert, enabled, start, end, options, personal );
zone_data new_zone = zone_data( name, type, fac, invert, enabled, start, end, options );
// only non personal zones can be vehicle zones
if( !personal ) {
optional_vpart_position const vp = here.veh_at( here.bub_from_abs( tripoint_abs_ms( start ) ) );
if( vp && vp->vehicle().get_owner() == fac && vp.cargo() ) {
// TODO:Allow for loot zones on vehicles to be larger than 1x1
if( start == end &&
( silent || query_yn( _( "Bind this zone to the cargo part here?" ) ) ) ) {
// TODO: refactor zone options for proper validation code
if( !silent &&
( type == zone_type_FARM_PLOT || type == zone_type_CONSTRUCTION_BLUEPRINT ) ) {
popup( _( "You cannot add that type of zone to a vehicle." ), PF_NONE );
return;
}

create_vehicle_loot_zone( vp->vehicle(), vp->mount_pos().raw(), new_zone, pmap );
optional_vpart_position const vp = here.veh_at( here.bub_from_abs( start ) );
if( vp && vp->vehicle().get_owner() == fac && vp.cargo() ) {
// TODO:Allow for loot zones on vehicles to be larger than 1x1
if( start == end &&
( silent || query_yn( _( "Bind this zone to the cargo part here?" ) ) ) ) {
// TODO: refactor zone options for proper validation code
if( !silent &&
( type == zone_type_FARM_PLOT || type == zone_type_CONSTRUCTION_BLUEPRINT ) ) {
popup( _( "You cannot add that type of zone to a vehicle." ), PF_NONE );
return;
}

create_vehicle_loot_zone( vp->vehicle(), vp->mount_pos(), new_zone, pmap );
return;
}
}

//Create a regular zone
zones.push_back( new_zone );

if( personal ) {
num_personal_zones++;
}
cache_data();
}

void zone_manager::add( const std::string &name, const zone_type_id &type, const faction_id &fac,
const bool invert, const bool enabled, const tripoint_rel_ms &start,
const tripoint_rel_ms &end, const shared_ptr_fast<zone_options> &options,
bool silent, map *pmap )

Check failure on line 1465 in src/clzones.cpp

View workflow job for this annotation

GitHub Actions / Basic Build and Test (Clang 10, Ubuntu, Curses)

unused parameter 'silent' [-Werror,-Wunused-parameter]
{
map &here = pmap == nullptr ? get_map() : *pmap;

Check failure on line 1467 in src/clzones.cpp

View workflow job for this annotation

GitHub Actions / Basic Build and Test (Clang 10, Ubuntu, Curses)

unused variable 'here' [-Werror,-Wunused-variable]
zone_data new_zone = zone_data( name, type, fac, invert, enabled, start, end, options );

//Create a regular zone
zones.push_back( new_zone );
num_personal_zones++;
cache_data();
}

Expand Down Expand Up @@ -1507,7 +1557,7 @@ void _rotate_zone( map &target_map, zone_data &zone, int turns )
target_map.getglobal( tripoint_bub_ms( std::max( z_l_start.x(), z_l_end.x() ),
std::max( z_l_start.y(), z_l_end.y() ),
z_end.z() ) );
zone.set_position( std::make_pair( first.raw(), second.raw() ), false, true, false, true );
zone.set_position( std::make_pair( first, second ), false, true, false, true );
}
}

Expand Down Expand Up @@ -1622,8 +1672,13 @@ void zone_data::serialize( JsonOut &json ) const
json.member( "is_vehicle", is_vehicle );
json.member( "is_personal", is_personal );
json.member( "cached_shift", cached_shift );
json.member( "start", start );
json.member( "end", end );
if( is_personal ) {
json.member( "start", personal_start );
json.member( "end", personal_end );
} else {
json.member( "start", start );
json.member( "end", end );
}
json.member( "is_displayed", is_displayed );
options->serialize( json );
json.end_object();
Expand Down Expand Up @@ -1664,19 +1719,29 @@ void zone_data::deserialize( const JsonObject &data )
}
//Legacy support
if( data.has_member( "start_x" ) ) {
tripoint s;
tripoint e;
data.read( "start_x", s.x );
data.read( "start_y", s.y );
data.read( "start_z", s.z );
data.read( "end_x", e.x );
data.read( "end_y", e.y );
data.read( "end_z", e.z );
start = s;
end = e;
tripoint_abs_ms s;
tripoint_abs_ms e;
data.read( "start_x", s.x() );
data.read( "start_y", s.y() );
data.read( "start_z", s.z() );
data.read( "end_x", e.x() );
data.read( "end_y", e.y() );
data.read( "end_z", e.z() );
if( is_personal ) {
personal_start = tripoint_rel_ms( s.raw() );
personal_end = tripoint_rel_ms( e.raw() );
} else {
start = s;
end = e;
}
} else {
data.read( "start", start );
data.read( "end", end );
if( is_personal ) {
data.read( "start", personal_start );
data.read( "end", personal_end );
} else {
data.read( "start", start );
data.read( "end", end );
}
}
if( data.has_member( "is_displayed" ) ) {
data.read( "is_displayed", is_displayed );
Expand Down Expand Up @@ -1804,18 +1869,19 @@ void zone_manager::revert_vzones()
}
}

void mapgen_place_zone( tripoint const &start, tripoint const &end, zone_type_id const &type,
void mapgen_place_zone( tripoint_abs_ms const &start, tripoint_abs_ms const &end,
zone_type_id const &type,
faction_id const &fac, std::string const &name, std::string const &filter,
map *pmap )
{
zone_manager &mgr = zone_manager::get_manager();
auto options = zone_options::create( type );
tripoint const s_ = std::min( start, end );
tripoint const e_ = std::max( start, end );
tripoint_abs_ms const s_ = std::min( start, end );
tripoint_abs_ms const e_ = std::max( start, end );
if( type == zone_type_LOOT_CUSTOM || type == zone_type_LOOT_ITEM_GROUP ) {
if( dynamic_cast<loot_options *>( &*options ) != nullptr ) {
dynamic_cast<loot_options *>( &*options )->set_mark( filter );
}
}
mgr.add( name, type, fac, false, true, s_, e_, options, false, true, pmap );
mgr.add( name, type, fac, false, true, s_, e_, options, true, pmap );
}
Loading

0 comments on commit 1bdfcd0

Please sign in to comment.