Skip to content

Commit

Permalink
fix: allow elevator misalignment (#3181)
Browse files Browse the repository at this point in the history
* fix: elevator misalignment in sarcophagus

* fix: apartments_mod stair alignment

* fix: make finding elevators more forgiving

* refactor: move `and_then` to `cata_algo.h`

_rust itertools interop when_

see: #3181 (comment)

Co-authored-by: olanti-p <[email protected]>

---------

Co-authored-by: olanti-p <[email protected]>
  • Loading branch information
scarf005 and olanti-p authored Sep 24, 2023
1 parent fd9e894 commit 855e278
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 11 deletions.
4 changes: 2 additions & 2 deletions data/json/mapgen/apartment_mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
" RsswFFFF...^|.STb| |bTS.|...FFFF.wssR ",
" Rssw........+...b|-WW-|b...+........wssR ",
" RssX........|--|-|....|-|--|........XssR ",
" Rss|1.htth...oo|Y|<...|Y|..........1|ssR ",
" Rss|1.htth...oo|Y|....|Y|..........1|ssR ",
" Rss|e.htth.....+.|....|.+..........e|ssR ",
" |-----|2.........A|-|-..-|-|..hh......u|-----| ",
" |..BBd|uO3........|........|..tt....3O2|dBB..| ",
" |..BBd|uO3........|<.......|..tt....3O2|dBB..| ",
" w..BB.|--|+|......D........D..tt..|+|--|.BB..w ",
" |d....+.r|u|^....t|........|..hh.^|u|r.+....d| ",
" |r...||--|-|------|........|------|-|--||...r| ",
Expand Down
8 changes: 4 additions & 4 deletions data/json/mapgen/hazardous_waste_sarcophagus.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@
" f ////////////////////+///LLLLLLL/// f ",
" f /////////////////////.//6~~~~~~~./// f ",
" f //.......................~~~~~~~~..// f ",
" f //.......||||............~~~~~~~~~..// f ",
" f //........|EE.......A....~~~~~~~~~~..// f ",
" f //.......................~~~~~~~~~..// f ",
" f //........||||......A....~~~~~~~~~~..// f ",
" f //...S....|EE......AA...~~~~~~~~~~~.9// f ",
" f //...P....|e.|.....AA..~~~~~~~~~~~~.9// f ",
" f //.``P``...............~~~~~~~~~~~~.9// f ",
" f //...P....|EE......AA..~~~~~~~~~~~~.9// f ",
" f //.``P``..|e.|.........~~~~~~~~~~~~.9// f ",
" f //.`$$$`.......AAA.....~~~~~~~~~~~~.9// f ",
" f //.`$$$`6......AA......~~~~~~~~~~~~.9// f ",
" f //.`$$$`...............~~~~~~~~~~~~..// f ",
Expand Down
12 changes: 12 additions & 0 deletions src/cata_algo.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include <algorithm>
#include <cassert>
#include <optional>
#include <unordered_map>
#include <unordered_set>
#include <vector>
Expand Down Expand Up @@ -120,6 +121,17 @@ std::vector<std::vector<T>> find_cycles( const std::unordered_map<T, std::vector
return result;
}

/// poor person's https://en.cppreference.com/w/cpp/utility/optional/and_then
template <typename T, typename Fn>
auto and_then( std::optional<T> const &opt, Fn &&f ) -> std::optional<std::invoke_result_t<Fn, T>>
{
if( opt ) {
return std::optional{f( *opt )};
}
return std::nullopt;
}


} // namespace cata

#endif // CATA_SRC_CATA_ALGO_H
38 changes: 33 additions & 5 deletions src/iexamine_elevator.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#include <optional>

#include "cata_algo.h"
#include "game.h"
#include "iexamine.h"
#include "mapdata.h"
Expand Down Expand Up @@ -60,13 +63,27 @@ auto dest( const elevator::tiles &elevator_here,
return tiles;
}

/// allow using misaligned elevators.
/// doesn't prevent you being stuck in the wall tho cause i was lazy
auto find_elevators_nearby( const tripoint &pos ) -> std::optional<tripoint>
{
constexpr int max_misalign = 3;
map &here = get_map();

for( const auto &p : closest_points_first( pos, max_misalign ) ) {
if( here.has_flag( TFLAG_ELEVATOR, p ) ) {
return p;
}
}
return {};
}

auto choose_floor( const tripoint &examp, const tripoint_abs_omt &this_omt,
const tripoint &sm_orig ) -> int
{
constexpr int retval_offset = 10000; // workaround for uilist retval autoassign when retval == -1
const auto this_floor = _( " (this floor)" );

map &here = get_map();
uilist choice;
choice.title = _( "Please select destination floor" );
for( int z = OVERMAP_HEIGHT; z >= -OVERMAP_DEPTH; z-- ) {
Expand All @@ -75,7 +92,7 @@ auto choose_floor( const tripoint &examp, const tripoint_abs_omt &this_omt,
const tripoint zp =
rotate_point_sm( { examp.xy(), z }, sm_orig, turns );

if( here.ter( zp )->examine != &iexamine::elevator ) {
if( !find_elevators_nearby( zp ) ) {
continue;
}
const std::string omt_name = overmap_buffer.ter_existing( that_omt )->get_name();
Expand Down Expand Up @@ -199,6 +216,19 @@ auto move_vehicles( const elevator_vehicles &vehs, const tripoint &sm_orig, int
here.reset_vehicle_cache();
}

auto move_player( player &p, const int movez, tripoint_abs_ms old_abs_pos ) -> void
{
map &here = get_map();

g->vertical_shift( movez );
// yes, this is inefficient, but i'm lazy
cata::and_then( elevator::find_elevators_nearby( p.pos() ), []( const tripoint & pos ) {
return g->place_player( pos );
} );

cata_event_dispatch::avatar_moves( *p.as_avatar(), here, old_abs_pos.raw() );
}

} //namespace elevator

} // namespace
Expand Down Expand Up @@ -234,7 +264,5 @@ void iexamine::elevator( player &p, const tripoint &examp )
elevator::move_items( elevator_here, elevator_dest );
elevator::move_creatures( elevator_here, elevator_dest );
elevator::move_vehicles( vehs, sm_orig, movez, turns );

g->vertical_shift( movez );
cata_event_dispatch::avatar_moves( *p.as_avatar(), here, old_abs_pos.raw() );
elevator::move_player( p, movez, old_abs_pos );
}

0 comments on commit 855e278

Please sign in to comment.