Skip to content

Commit

Permalink
Merge pull request #53698 from jbytheway/misplaced_map_extras
Browse files Browse the repository at this point in the history
Avoid misplaced portal traps
  • Loading branch information
ZhilkinSerg authored Dec 28, 2021
2 parents 0b378df + 2499f17 commit b0acf70
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 16 deletions.
14 changes: 8 additions & 6 deletions src/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5231,8 +5231,9 @@ const trap &map::tr_at( const tripoint &p ) const
return tr_null.obj();
}

if( current_submap->get_ter( l ).obj().trap != tr_null ) {
return current_submap->get_ter( l ).obj().trap.obj();
const trap_id &builtin_trap = current_submap->get_ter( l )->trap;
if( builtin_trap != tr_null ) {
return *builtin_trap;
}

return current_submap->get_trap( l ).obj();
Expand Down Expand Up @@ -5295,13 +5296,14 @@ void map::trap_set( const tripoint &p, const trap_id &type )
point l;
submap *const current_submap = unsafe_get_submap_at( p, l );
if( current_submap == nullptr ) {
debugmsg( "Tried to set trap at (%d,%d) but the submap is not loaded", l.x, l.y );
debugmsg( "Tried to set trap at %s but the submap is not loaded", l.to_string() );
return;
}
const ter_t &ter = current_submap->get_ter( l ).obj();
if( ter.trap != tr_null ) {
debugmsg( "set trap %s on top of terrain %s which already has a built-in trap",
type.obj().name(), ter.name() );
debugmsg( "set trap %s (%s) at %s on top of terrain %s (%s) which already has a "
"built-in trap", type.id().str(), type->name(), p.to_string(),
ter.id.str(), ter.name() );
return;
}

Expand Down Expand Up @@ -6911,7 +6913,7 @@ void map::saven( const tripoint &grid )
}
if( submap_to_save->get_ter( point_zero ) == t_null ) {
// This is a serious error and should be signaled as soon as possible
debugmsg( "map::saven grid (%d,%d,%d) uninitialized!", grid.x, grid.y, grid.z );
debugmsg( "map::saven grid %s uninitialized!", grid.to_string() );
return;
}

Expand Down
10 changes: 7 additions & 3 deletions src/map_extras.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -925,9 +925,10 @@ static bool mx_portal( map &m, const tripoint &abs_sub )
m.points_in_rectangle( { 1, 1, abs_sub.z }, { SEEX * 2 - 2, SEEY * 2 - 2, abs_sub.z } );

// Get a random point in our collection that does not have a trap and does not have the NO_FLOOR flag.
const cata::optional<tripoint> portal_pos = random_point( points, [&]( const tripoint & p ) {
auto good_portal_pos = [&]( const tripoint & p ) {
return !m.has_flag_ter( ter_furn_flag::TFLAG_NO_FLOOR, p ) && m.tr_at( p ).is_null();
} );
};
const cata::optional<tripoint> portal_pos = random_point( points, good_portal_pos );

// If we can't get a point to spawn the portal (e.g. we're triggered in entirely open air) we're done here.
if( !portal_pos ) {
Expand All @@ -941,7 +942,10 @@ static bool mx_portal( map &m, const tripoint &abs_sub )
}
}

m.trap_set( *portal_pos, tr_portal );
// Creating rubble can change the terrain type so check that again
if( good_portal_pos( *portal_pos ) ) {
m.trap_set( *portal_pos, tr_portal );
}

// We'll make between 0 and 4 attempts to spawn monsters here.
int num_monsters = rng( 0, 4 );
Expand Down
31 changes: 24 additions & 7 deletions src/mapgen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2384,7 +2384,8 @@ class jmapgen_trap : public jmapgen_piece
{
public:
mapgen_value<trap_id> id;
bool remove;
bool remove = false;

jmapgen_trap( const JsonObject &jsi, const std::string &/*context*/ ) {
init( jsi.get_member( "trap" ) );
remove = jsi.get_bool( "remove", false );
Expand All @@ -2393,13 +2394,15 @@ class jmapgen_trap : public jmapgen_piece
explicit jmapgen_trap( const JsonValue &tid ) {
if( tid.test_object() ) {
JsonObject jo = tid.get_object();
remove = jo.get_bool( "remove", false );
if( jo.has_member( "trap" ) ) {
init( jo.get_member( "trap" ) );
return;
}
}
init( tid );
}

void apply( const mapgendata &dat, const jmapgen_int &x, const jmapgen_int &y
) const override {
trap_id chosen_id = id.get( dat );
Expand Down Expand Up @@ -4132,7 +4135,12 @@ void mapgen_function_json::generate( mapgendata &md )
const oter_t &ter = *md.terrain_type();

auto do_predecessor_mapgen = [&]( mapgendata & predecessor_md ) {
run_mapgen_func( predecessor_md.terrain_type().id().str(), predecessor_md );
const std::string function_key = predecessor_md.terrain_type()->get_mapgen_id();
bool success = run_mapgen_func( function_key, predecessor_md );

if( !success ) {
debugmsg( "predecessor mapgen with key %s failed", function_key );
}

// Now we have to do some rotation shenanigans. We need to ensure that
// our predecessor is not rotated out of alignment as part of rotating this location,
Expand Down Expand Up @@ -4883,7 +4891,16 @@ void map::draw_lab( mapgendata &dat )
}
// portal with an artifact effect.
case 5: {
tripoint center( rng( 6, SEEX * 2 - 7 ), rng( 6, SEEY * 2 - 7 ), abs_sub.z );
tripoint_range<tripoint> options =
points_in_rectangle( { 6, 6, abs_sub.z },
{ SEEX * 2 - 7, SEEY * 2 - 7, abs_sub.z } );
cata::optional<tripoint> center = random_point(
options, [&]( const tripoint & p ) {
return tr_at( p ).is_null();
} );
if( !center ) {
break;
}
std::vector<artifact_natural_property> valid_props = {
ARTPROP_BREATHING,
ARTPROP_CRACKLING,
Expand All @@ -4900,10 +4917,10 @@ void map::draw_lab( mapgendata &dat )
}
make_rubble( {p, abs_sub.z } );
ter_set( p, t_thconc_floor );
}, center.xy(), 4 );
furn_set( center.xy(), f_null );
trap_set( center, tr_portal );
create_anomaly( center, random_entry( valid_props ), false );
}, center->xy(), 4 );
furn_set( center->xy(), f_null );
trap_set( *center, tr_portal );
create_anomaly( *center, random_entry( valid_props ), false );
break;
}
// radioactive accident.
Expand Down

0 comments on commit b0acf70

Please sign in to comment.