forked from CleverRaven/Cataclysm-DDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
magic_teleporter_list.cpp
218 lines (194 loc) · 7.08 KB
/
magic_teleporter_list.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#include "magic_teleporter_list.h"
#include <algorithm>
#include <cstddef>
#include <map>
#include <memory>
#include <new>
#include <string>
#include <utility>
#include "avatar.h"
#include "calendar.h"
#include "catacharset.h"
#include "character.h"
#include "color.h"
#include "coordinates.h"
#include "creature_tracker.h"
#include "cursesdef.h"
#include "enums.h"
#include "game.h"
#include "json.h"
#include "map.h"
#include "map_iterator.h"
#include "messages.h"
#include "output.h"
#include "panels.h"
#include "point.h"
#include "string_formatter.h"
#include "string_input_popup.h"
#include "translations.h"
#include "type_id.h"
#include "ui.h"
static const efftype_id effect_ignore_fall_damage( "ignore_fall_damage" );
static bool popup_string( std::string &result, std::string &title )
{
string_input_popup popup;
popup.title( title );
popup.text( "" ).only_digits( false );
popup.query();
if( popup.canceled() ) {
return false;
}
result = popup.text();
return true;
}
bool teleporter_list::activate_teleporter( const tripoint_abs_omt &omt_pt, const tripoint & )
{
std::string point_name;
std::string title = _( "Name this gate." );
popup_string( point_name, title );
return known_teleporters.emplace( omt_pt, point_name ).second;
}
void teleporter_list::deactivate_teleporter( const tripoint_abs_omt &omt_pt, const tripoint & )
{
known_teleporters.erase( omt_pt );
}
// returns the first valid teleport location near a teleporter
// returns map square (global coordinates)
static cata::optional<tripoint> find_valid_teleporters_omt( const tripoint_abs_omt &omt_pt )
{
// this is the top left hand square of the global absolute coordinate
// of the overmap terrain we want to try to teleport to.
// an OMT is SEEX * SEEY in size
const tripoint_abs_sm sm_pt = project_to<coords::sm>( omt_pt );
tinymap checker;
checker.load( sm_pt, true );
for( const tripoint &p : checker.points_on_zlevel() ) {
if( checker.has_flag_furn( ter_furn_flag::TFLAG_TRANSLOCATOR, p ) ) {
return checker.getabs( p );
}
}
return cata::nullopt;
}
bool teleporter_list::place_avatar_overmap( Character &you, const tripoint_abs_omt &omt_pt ) const
{
tinymap omt_dest;
tripoint_abs_sm sm_dest = project_to<coords::sm>( omt_pt );
omt_dest.load( sm_dest, true );
cata::optional<tripoint> global_dest = find_valid_teleporters_omt( omt_pt );
if( !global_dest ) {
return false;
}
tripoint local_dest = omt_dest.getlocal( *global_dest ) + point( 60, 60 );
you.add_effect( effect_ignore_fall_damage, 1_seconds, false, 0, true );
g->place_player_overmap( omt_pt );
g->place_player( local_dest );
return true;
}
void teleporter_list::translocate( const std::set<tripoint> &targets )
{
if( known_teleporters.empty() ) {
// we can't go somewhere if we don't know how to get there!
add_msg( m_bad, _( "No translocator target known." ) );
return;
}
cata::optional<tripoint_abs_omt> omt_dest = choose_teleport_location();
if( !omt_dest ) {
add_msg( _( "Teleport canceled." ) );
return;
}
bool valid_targets = false;
for( const tripoint &pt : targets ) {
Character *you = get_creature_tracker().creature_at<Character>( pt );
if( you && you->is_avatar() ) {
valid_targets = true;
if( !place_avatar_overmap( *you, *omt_dest ) ) {
add_msg( _( "Failed to teleport. Teleporter obstructed or destroyed." ) );
deactivate_teleporter( *omt_dest, pt );
}
}
}
if( !valid_targets ) {
add_msg( _( "No valid targets to teleport." ) );
}
}
bool teleporter_list::knows_translocator( const tripoint_abs_omt &omt_pos ) const
{
return known_teleporters.find( omt_pos ) != known_teleporters.end();
}
void teleporter_list::serialize( JsonOut &json ) const
{
json.start_object();
json.member( "known_teleporters" );
json.start_array();
for( std::pair<tripoint_abs_omt, std::string> pair : known_teleporters ) {
json.start_object();
json.member( "position", pair.first );
json.member( "name", pair.second );
json.end_object();
}
json.end_array();
json.end_object();
}
void teleporter_list::deserialize( const JsonObject &data )
{
for( JsonObject jo : data.get_array( "known_teleporters" ) ) {
tripoint_abs_omt temp_pos;
jo.read( "position", temp_pos );
std::string name;
jo.read( "name", name );
known_teleporters.emplace( temp_pos, name );
}
}
class teleporter_callback : public uilist_callback
{
private:
// to make it easier to get the callback from the known_teleporters
std::map<int, tripoint_abs_omt> index_pairs;
public:
explicit teleporter_callback( std::map<int, tripoint_abs_omt> &ip ) : index_pairs( ip ) {}
void refresh( uilist *menu ) override {
const int entnum = menu->selected;
const int start_x = menu->w_width - menu->pad_right;
mvwputch( menu->window, point( start_x, 0 ), c_magenta, LINE_OXXX );
mvwputch( menu->window, point( start_x, menu->w_height - 1 ), c_magenta, LINE_XXOX );
for( int i = 1; i < menu->w_height - 1; i++ ) {
mvwputch( menu->window, point( start_x, i ), c_magenta, LINE_XOXO );
}
if( entnum >= 0 && static_cast<size_t>( entnum ) < index_pairs.size() ) {
avatar &player_character = get_avatar();
overmap_ui::draw_overmap_chunk( menu->window, player_character, index_pairs[entnum],
point( start_x + 1, 1 ),
29, 21 );
int dist = rl_dist( player_character.global_omt_location(), index_pairs[entnum] );
mvwprintz( menu->window, point( start_x + 2, 1 ), c_white,
string_format( _( "Distance: %d %s" ), dist,
index_pairs[entnum].to_string() ) );
}
wnoutrefresh( menu->window );
}
};
cata::optional<tripoint_abs_omt> teleporter_list::choose_teleport_location()
{
cata::optional<tripoint_abs_omt> ret = cata::nullopt;
uilist teleport_selector;
teleport_selector.w_height_setup = 24;
int index = 0;
int column_width = 25;
std::map<int, tripoint_abs_omt> index_pairs;
for( const std::pair<const tripoint_abs_omt, std::string> &gate : known_teleporters ) {
teleport_selector.addentry( index, true, 0, gate.second );
column_width = std::max( column_width, utf8_width( gate.second ) );
index_pairs.emplace( index, gate.first );
index++;
}
teleporter_callback cb( index_pairs );
teleport_selector.callback = &cb;
teleport_selector.w_width_setup = 38 + column_width;
teleport_selector.pad_right_setup = 33;
teleport_selector.title = _( "Choose Translocator Gate" );
teleport_selector.query();
if( teleport_selector.ret >= 0 ) {
ret = index_pairs[teleport_selector.ret];
}
return ret;
}