Skip to content

Commit

Permalink
mapgen/zones: allow placing vehicle zones
Browse files Browse the repository at this point in the history
  • Loading branch information
andrei8l committed Apr 12, 2022
1 parent e13989e commit 6c263e6
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 17 deletions.
29 changes: 16 additions & 13 deletions src/clzones.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -683,10 +683,10 @@ void zone_manager::cache_avatar_location()
}
}

void zone_manager::cache_vzones()
void zone_manager::cache_vzones( map *pmap )
{
vzone_cache.clear();
map &here = get_map();
map &here = pmap == nullptr ? get_map() : *pmap;
auto vzones = here.get_vehicle_zones( here.get_abs_sub().z() );
for( zone_data *elem : vzones ) {
if( !elem->get_enabled() ) {
Expand Down Expand Up @@ -1052,38 +1052,41 @@ const zone_data *zone_manager::get_bottom_zone(
// 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,
zone_data &new_zone )
zone_data &new_zone, map *pmap )
{
//create a vehicle loot zone
new_zone.set_is_vehicle( true );
auto nz = vehicle.loot_zones.emplace( mount_point, new_zone );
map &here = get_map();
map &here = pmap == nullptr ? get_map() : *pmap;
here.register_vehicle_zone( &vehicle, here.get_abs_sub().z() );
vehicle.zones_dirty = false;
added_vzones.push_back( &nz->second );
cache_vzones();
cache_vzones( pmap );
}

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 tripoint &end, const shared_ptr_fast<zone_options> &options, const bool personal,
bool silent, map *pmap )
{
map &here = get_map();
map &here = pmap == nullptr ? get_map() : *pmap;
zone_data new_zone = zone_data( name, type, fac, invert, enabled, start, end, options, personal );
// only non personal zones can be vehicle zones
if( !personal ) {
//the start is a vehicle tile with cargo space
if( const cata::optional<vpart_reference> vp = here.veh_at( here.getlocal(
start ) ).part_with_feature( "CARGO", false ) ) {
optional_vpart_position const vp = here.veh_at( here.getlocal( start ) );
if( vp and ( !vp->vehicle().has_owner() or vp->vehicle().get_owner() == fac ) and
vp.part_with_feature( "CARGO", false ) ) {
// TODO:Allow for loot zones on vehicles to be larger than 1x1
if( start == end && query_yn( _( "Bind this zone to the cargo part here?" ) ) ) {
if( start == end &&
( silent || query_yn( _( "Bind this zone to the cargo part here?" ) ) ) ) {
// TODO: refactor zone options for proper validation code
if( type == zone_type_FARM_PLOT || type == zone_type_CONSTRUCTION_BLUEPRINT ) {
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(), new_zone );
create_vehicle_loot_zone( vp->vehicle(), vp->mount(), new_zone, pmap );
return;
}
}
Expand Down
7 changes: 4 additions & 3 deletions src/clzones.h
Original file line number Diff line number Diff line change
Expand Up @@ -432,10 +432,11 @@ class zone_manager
void add( const std::string &name, const zone_type_id &type, const faction_id &faction,
bool invert, bool enabled,
const tripoint &start, const tripoint &end,
const shared_ptr_fast<zone_options> &options = nullptr, const bool personal = false );
const shared_ptr_fast<zone_options> &options = nullptr, const bool personal = false,
bool mapgen = false, map *pmap = nullptr );
const zone_data *get_zone_at( const tripoint_abs_ms &where, const zone_type_id &type ) const;
void create_vehicle_loot_zone( class vehicle &vehicle, const point &mount_point,
zone_data &new_zone );
zone_data &new_zone, map *pmap = nullptr );

bool remove( zone_data &zone );

Expand All @@ -451,7 +452,7 @@ class zone_manager
void cache_data( bool update_avatar = true );
void reset_disabled();
void cache_avatar_location();
void cache_vzones();
void cache_vzones( map *pmap = nullptr );
bool has( const zone_type_id &type, const tripoint_abs_ms &where,
const faction_id &fac = your_fac ) const;
bool has_near( const zone_type_id &type, const tripoint_abs_ms &where,
Expand Down
7 changes: 6 additions & 1 deletion src/mapgen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2963,6 +2963,9 @@ class jmapgen_zone : public jmapgen_piece
filter = jsi.get_string( "filter" );
}
}
mapgen_phase phase() const override {
return mapgen_phase::zones;
}
void apply( const mapgendata &dat, const jmapgen_int &x, const jmapgen_int &y,
const std::string &/*context*/ ) const override {
zone_type_id chosen_zone_type = zone_type.get( dat );
Expand All @@ -2974,7 +2977,8 @@ class jmapgen_zone : public jmapgen_piece
if( chosen_zone_type == zone_type_LOOT_CUSTOM ) {
dynamic_cast<loot_options *>( &*options )->set_mark( filter );
}
mgr.add( name, chosen_zone_type, chosen_faction, false, true, start, end, options );
mgr.add( name, chosen_zone_type, chosen_faction, false, true, start, end, options,
false, true, &dat.m );
}

void check( const std::string &oter_name, const mapgen_parameters &parameters,
Expand Down Expand Up @@ -6680,6 +6684,7 @@ vehicle *map::add_vehicle( const vproto_id &type, const tripoint &p, const units
add_vehicle_to_cache( placed_vehicle );

rebuild_vehicle_level_caches();
placed_vehicle->place_zones( *this );
//debugmsg ("grid[%d]->vehicles.size=%d veh.parts.size=%d", nonant, grid[nonant]->vehicles.size(),veh.parts.size());
}
return placed_vehicle;
Expand Down
1 change: 1 addition & 0 deletions src/mapgen.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ enum class mapgen_phase {
nested_mapgen,
transform,
faction_ownership,
zones,
last
};

Expand Down
16 changes: 16 additions & 0 deletions src/veh_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1193,6 +1193,22 @@ void vehicle_prototype::load( const JsonObject &jo )
}
vproto.item_spawns.push_back( std::move( next_spawn ) );
}

for( JsonObject jzi : jo.get_array( "zones" ) ) {
zone_type_id zone_type( jzi.get_member( "type" ).get_string() );
faction_id faction( jzi.get_member( "faction" ).get_string() );
std::string name;
std::string filter;
point pt( jzi.get_member( "x" ).get_int(), jzi.get_member( "y" ).get_int() );

if( jzi.has_string( "name" ) ) {
name = jzi.get_string( "name" );
}
if( jzi.has_string( "filter" ) ) {
filter = jzi.get_string( "filter" );
}
vproto.zone_defs.emplace_back( zone_def{ zone_type, faction, name, filter, pt } );
}
}

void vehicle_prototype::reset()
Expand Down
9 changes: 9 additions & 0 deletions src/veh_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,14 @@ struct vehicle_prototype {
itype_id fuel = itype_id::NULL_ID();
};

struct zone_def {
zone_type_id zone_type;
faction_id faction;
std::string name;
std::string filter;
point pt;
};

vehicle_prototype();
vehicle_prototype( vehicle_prototype && ) noexcept;
~vehicle_prototype();
Expand All @@ -513,6 +521,7 @@ struct vehicle_prototype {
translation name;
std::vector<part_def> parts;
std::vector<vehicle_item_spawn> item_spawns;
std::vector<zone_def> zone_defs;

std::unique_ptr<vehicle> blueprint;

Expand Down
19 changes: 19 additions & 0 deletions src/vehicle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ static const proficiency_id proficiency_prof_aircraft_mechanic( "prof_aircraft_m

static const vproto_id vehicle_prototype_none( "none" );

static const zone_type_id zone_type_LOOT_CUSTOM( "LOOT_CUSTOM" );
static const zone_type_id zone_type_LOOT_ITEM_GROUP( "LOOT_ITEM_GROUP" );
static const zone_type_id zone_type_VEHICLE_PATROL( "VEHICLE_PATROL" );

static const std::string flag_E_COMBUSTION( "E_COMBUSTION" );
Expand Down Expand Up @@ -5538,6 +5540,23 @@ void vehicle::place_spawn_items()
}
}

void vehicle::place_zones( map &pmap ) const
{
if( !type.is_valid() ) {
return;
}
zone_manager &mgr = zone_manager::get_manager();

for( vehicle_prototype::zone_def const &d : type->zone_defs ) {
const tripoint pt = pmap.getabs( tripoint( pos + d.pt, pmap.get_abs_sub().z() ) );
auto options = zone_options::create( d.zone_type );
if( d.zone_type == zone_type_LOOT_CUSTOM or d.zone_type == zone_type_LOOT_ITEM_GROUP ) {
dynamic_cast<loot_options *>( &*options )->set_mark( d.filter );
}
mgr.add( name, d.zone_type, d.faction, false, true, pt, pt, options, false, true, &pmap );
}
}

void vehicle::gain_moves()
{
fuel_used_last_turn.clear();
Expand Down
2 changes: 2 additions & 0 deletions src/vehicle.h
Original file line number Diff line number Diff line change
Expand Up @@ -1572,6 +1572,8 @@ class vehicle
// Generates starting items in the car, should only be called when placed on the map
void place_spawn_items();

void place_zones( map &pmap ) const;

void gain_moves();

// if its a summoned vehicle - its gotta disappear at some point, return true if destroyed
Expand Down

0 comments on commit 6c263e6

Please sign in to comment.