/
menu_events.cpp
3297 lines (2944 loc) · 105 KB
/
menu_events.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (C) 2006 - 2015 by Joerg Hinrichs <joerg.hinrichs@alice-dsl.de>
wesnoth playturn Copyright (C) 2003 by David White <dave@whitevine.net>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
/**
* @file
* Operations activated from menus/hotkeys while playing a game.
* E.g. Unitlist, status_table, save_game, save_map, chat, show_help, etc.
*/
#include "global.hpp"
#include "actions/attack.hpp"
#include "actions/create.hpp"
#include "actions/move.hpp"
#include "actions/undo.hpp"
#include "actions/vision.hpp"
#include "ai/manager.hpp"
#include "config_assign.hpp"
#include "dialogs.hpp"
#include "display_chat_manager.hpp"
#include "filechooser.hpp"
#include "formatter.hpp"
#include "formula_string_utils.hpp"
#include "game_board.hpp"
#include "game_end_exceptions.hpp"
#include "game_events/manager.hpp"
#include "game_events/pump.hpp"
#include "game_preferences.hpp"
#include "game_state.hpp"
#include "gettext.hpp"
#include "gui/dialogs/chat_log.hpp"
#include "gui/dialogs/edit_label.hpp"
#include "gui/dialogs/message.hpp"
#include "gui/dialogs/transient_message.hpp"
#include "gui/dialogs/wml_message.hpp"
#include "gui/dialogs/gamestate_inspector.hpp"
#include "gui/dialogs/mp_change_control.hpp"
#include "gui/dialogs/data_manage.hpp"
#include "gui/dialogs/simple_item_selector.hpp"
#include "gui/dialogs/edit_text.hpp"
#include "gui/dialogs/unit_create.hpp"
#include "gui/widgets/settings.hpp"
#include "gui/widgets/window.hpp"
#include "help/help.hpp"
#include "log.hpp"
#include "map.hpp"
#include "map_label.hpp"
#include "marked-up_text.hpp"
#include "menu_events.hpp"
#include "mouse_events.hpp"
#include "play_controller.hpp"
#include "playsingle_controller.hpp"
#include "preferences_display.hpp"
#include "replay.hpp"
#include "replay_helper.hpp"
#include "resources.hpp"
#include "savegame.hpp"
#include "save_index.hpp"
#include "scripting/game_lua_kernel.hpp"
#include "scripting/plugins/manager.hpp"
#include "sound.hpp"
#include "statistics_dialog.hpp"
#include "synced_context.hpp"
#include "terrain_builder.hpp"
#include "unit.hpp"
#include "unit_display.hpp"
#include "wml_separators.hpp"
#include "whiteboard/manager.hpp"
#include "widgets/combo.hpp"
#include <boost/foreach.hpp>
#include <boost/range/algorithm/find_if.hpp>
static lg::log_domain log_engine("engine");
#define ERR_NG LOG_STREAM(err, log_engine)
#define LOG_NG LOG_STREAM(info, log_engine)
namespace events{
menu_handler::menu_handler(game_display* gui, play_controller & pc, const config& game_config) :
gui_(gui),
pc_(pc),
game_config_(game_config),
textbox_info_(),
last_search_(),
last_search_hit_()
{
}
menu_handler::~menu_handler()
{
}
game_state & menu_handler::gamestate() const { return pc_.gamestate(); }
game_data & menu_handler::gamedata() { return gamestate().gamedata_; }
game_board & menu_handler::board() const { return gamestate().board_; }
unit_map& menu_handler::units() { return gamestate().board_.units_; }
std::vector<team>& menu_handler::teams() const { return gamestate().board_.teams_; }
const gamemap& menu_handler::map() { return gamestate().board_.map(); }
gui::floating_textbox& menu_handler::get_textbox(){
return textbox_info_;
}
std::string menu_handler::get_title_suffix(int side_num)
{
int controlled_recruiters = 0;
for(size_t i = 0; i < teams().size(); ++i) {
if(teams()[i].is_local_human() && !teams()[i].recruits().empty()
&& units().find_leader(i + 1) != units().end()) {
++controlled_recruiters;
}
}
std::stringstream msg;
if(controlled_recruiters >= 2) {
unit_map::const_iterator leader = units().find_leader(side_num);
if (leader != units().end() && !leader->name().empty()) {
msg << " (" << leader->name(); msg << ")";
}
}
return msg.str();
}
void menu_handler::objectives(int side_num)
{
if (!gamestate().lua_kernel_) {
return ;
}
config cfg;
cfg["side"] = str_cast(side_num);
gamestate().lua_kernel_->run_wml_action("show_objectives", vconfig(cfg),
game_events::queued_event("_from_interface", map_location(),
map_location(), config()));
team ¤t_team = teams()[side_num - 1];
dialogs::show_objectives(pc_.get_scenario_name(), current_team.objectives());
current_team.reset_objectives_changed();
}
void menu_handler::show_statistics(int side_num)
{
team ¤t_team = teams()[side_num - 1];
// Current Player name
const std::string &player = current_team.current_player();
//add player's name to title of dialog
std::stringstream title_str;
title_str << _("Statistics") << " (" << player << ")";
statistics_dialog stats_dialog(*gui_, title_str.str(),
side_num, current_team.save_id(), player);
stats_dialog.show();
}
void menu_handler::unit_list()
{
dialogs::show_unit_list(*gui_);
}
namespace {
class leader_scroll_dialog : public gui::dialog {
public:
leader_scroll_dialog(display &disp, const std::string &title,
std::vector<bool> &leader_bools, int selected,
gui::DIALOG_RESULT extra_result) :
dialog(disp, title, "", gui::NULL_DIALOG),
scroll_btn_(new gui::standard_dialog_button(disp.video(), _("Scroll To"), 0, false)),
leader_bools_(leader_bools),
extra_result_(extra_result)
{
scroll_btn_->enable(leader_bools[selected]);
add_button(scroll_btn_, gui::dialog::BUTTON_STANDARD);
add_button(new gui::standard_dialog_button(disp.video(),
_("Close"), 1, true), gui::dialog::BUTTON_STANDARD);
}
void action(gui::dialog_process_info &info) {
const bool leader_bool = leader_bools_[get_menu().selection()];
scroll_btn_->enable(leader_bool);
if(leader_bool && (info.double_clicked || (!info.key_down
&& (info.key[SDLK_RETURN] || info.key[SDLK_KP_ENTER])))) {
set_result(get_menu().selection());
} else if(!info.key_down && info.key[SDLK_ESCAPE]) {
set_result(gui::CLOSE_DIALOG);
} else if(!info.key_down && info.key[SDLK_SPACE]) {
set_result(extra_result_);
} else if(result() == gui::CONTINUE_DIALOG) {
dialog::action(info);
}
}
private:
gui::standard_dialog_button *scroll_btn_;
std::vector<bool> &leader_bools_;
gui::DIALOG_RESULT extra_result_;
};
} //end anonymous namespace
void menu_handler::status_table(int selected)
{
std::stringstream heading;
heading << HEADING_PREFIX << _("Leader") << COLUMN_SEPARATOR << ' ' << COLUMN_SEPARATOR
<< _("Team") << COLUMN_SEPARATOR
<< _("Gold") << COLUMN_SEPARATOR
<< _("Villages") << COLUMN_SEPARATOR
<< _("status^Units") << COLUMN_SEPARATOR
<< _("Upkeep") << COLUMN_SEPARATOR
<< _("Income");
gui::menu::basic_sorter sorter;
sorter.set_redirect_sort(0,1).set_alpha_sort(1).set_alpha_sort(2).set_numeric_sort(3)
.set_numeric_sort(4).set_numeric_sort(5).set_numeric_sort(6).set_numeric_sort(7);
std::vector<std::string> items;
std::vector<bool> leader_bools;
items.push_back(heading.str());
const team& viewing_team = teams()[gui_->viewing_team()];
unsigned total_villages = 0;
// a variable to check if there are any teams to show in the table
bool status_table_empty = true;
//if the player is under shroud or fog, they don't get
//to see details about the other sides, only their own
//side, allied sides and a ??? is shown to demonstrate
//lack of information about the other sides But he see
//all names with in colors
for(size_t n = 0; n != teams().size(); ++n) {
if(teams()[n].hidden()) {
continue;
}
status_table_empty=false;
const bool known = viewing_team.knows_about_team(n, network::nconnections() > 0);
const bool enemy = viewing_team.is_enemy(n+1);
std::stringstream str;
const team_data data = board().calculate_team_data(teams()[n],n+1);
unit_map::const_iterator leader = units().find_leader(n + 1);
std::string leader_name;
//output the number of the side first, and this will
//cause it to be displayed in the correct color
if(leader != units().end()) {
const bool fogged = viewing_team.fogged(leader->get_location());
// Add leader image. If it's fogged
// show only a random leader image.
if (!fogged || known || game_config::debug) {
str << IMAGE_PREFIX << leader->absolute_image();
#ifndef LOW_MEM
str << leader->image_mods();
#endif
leader_bools.push_back(true);
leader_name = leader->name();
} else {
str << IMAGE_PREFIX << std::string("units/unknown-unit.png");
#ifndef LOW_MEM
str << "~RC(magenta>" << teams()[n].color() << ")";
#endif
leader_bools.push_back(false);
leader_name = "Unknown";
}
if (pc_.get_classification().campaign_type == game_classification::CAMPAIGN_TYPE::MULTIPLAYER)
leader_name = teams()[n].current_player();
} else {
leader_bools.push_back(false);
}
str << COLUMN_SEPARATOR << team::get_side_highlight(n)
<< leader_name << COLUMN_SEPARATOR
<< (data.teamname.empty() ? teams()[n].team_name() : data.teamname)
<< COLUMN_SEPARATOR;
if(!known && !game_config::debug) {
// We don't spare more info (only name)
// so let's go on next side ...
items.push_back(str.str());
continue;
}
if(game_config::debug) {
str << utils::half_signed_value(data.gold) << COLUMN_SEPARATOR;
} else if(enemy && viewing_team.uses_fog()) {
str << ' ' << COLUMN_SEPARATOR;
} else {
str << utils::half_signed_value(data.gold) << COLUMN_SEPARATOR;
}
str << data.villages;
if(!(viewing_team.uses_fog() || viewing_team.uses_shroud())) {
str << "/" << map().villages().size();
}
str << COLUMN_SEPARATOR
<< data.units << COLUMN_SEPARATOR << data.upkeep << COLUMN_SEPARATOR
<< (data.net_income < 0 ? font::BAD_TEXT : font::NULL_MARKUP) << utils::signed_value(data.net_income);
total_villages += data.villages;
items.push_back(str.str());
}
if (total_villages > map().villages().size()) {
ERR_NG << "Logic error: map has " << map().villages().size() << " villages but status table shows " << total_villages << " owned in total" << std::endl;
}
if (status_table_empty)
{
// no sides to show - display empty table
std::stringstream str;
str << " ";
for (int i=0;i<7;++i)
str << COLUMN_SEPARATOR << " ";
leader_bools.push_back(false);
items.push_back(str.str());
}
int result = 0;
{
leader_scroll_dialog slist(*gui_, _("Current Status"), leader_bools, selected, gui::DIALOG_FORWARD);
slist.add_button(new gui::dialog_button(gui_->video(), _("More >"),
gui::button::TYPE_PRESS, gui::DIALOG_FORWARD),
gui::dialog::BUTTON_EXTRA_LEFT);
slist.set_menu(items, &sorter);
slist.get_menu().move_selection(selected);
result = slist.show();
selected = slist.get_menu().selection();
} // this will kill the dialog before scrolling
if (result >= 0)
gui_->scroll_to_leader(selected+1);
else if (result == gui::DIALOG_FORWARD)
scenario_settings_table(selected);
}
void menu_handler::scenario_settings_table(int selected)
{
std::stringstream heading;
heading << HEADING_PREFIX << _("scenario settings^Leader") << COLUMN_SEPARATOR
<< COLUMN_SEPARATOR
<< _("scenario settings^Side") << COLUMN_SEPARATOR
<< _("scenario settings^Start\nGold") << COLUMN_SEPARATOR
<< _("scenario settings^Base\nIncome") << COLUMN_SEPARATOR
<< _("scenario settings^Gold Per\nVillage") << COLUMN_SEPARATOR
<< _("scenario settings^Support Per\nVillage") << COLUMN_SEPARATOR
<< _("scenario settings^Fog") << COLUMN_SEPARATOR
<< _("scenario settings^Shroud");
gui::menu::basic_sorter sorter;
sorter.set_redirect_sort(0,1).set_alpha_sort(1).set_numeric_sort(2)
.set_numeric_sort(3).set_numeric_sort(4).set_numeric_sort(5)
.set_numeric_sort(6).set_alpha_sort(7).set_alpha_sort(8);
std::vector<std::string> items;
std::vector<bool> leader_bools;
items.push_back(heading.str());
const team& viewing_team = teams()[gui_->viewing_team()];
bool settings_table_empty = true;
bool fogged;
for(size_t n = 0; n != teams().size(); ++n) {
if(teams()[n].hidden()) {
continue;
}
settings_table_empty = false;
std::stringstream str;
unit_map::const_iterator leader = units().find_leader(n + 1);
if(leader != units().end()) {
// Add leader image. If it's fogged
// show only a random leader image.
fogged=viewing_team.fogged(leader->get_location());
if (!fogged || viewing_team.knows_about_team(n, network::nconnections() > 0) || game_config::debug) {
str << IMAGE_PREFIX << leader->absolute_image();
leader_bools.push_back(true);
} else {
str << IMAGE_PREFIX << std::string("units/unknown-unit.png");
leader_bools.push_back(false);
}
#ifndef LOW_MEM
str << "~RC(" << leader->team_color() << '>'
<< team::get_side_color_index(n+1) << ")";
#endif
} else {
leader_bools.push_back(false);
}
str << COLUMN_SEPARATOR << team::get_side_highlight(n)
<< teams()[n].current_player() << COLUMN_SEPARATOR
<< n + 1 << COLUMN_SEPARATOR
<< teams()[n].start_gold() << COLUMN_SEPARATOR
<< teams()[n].base_income() << COLUMN_SEPARATOR
<< teams()[n].village_gold() << COLUMN_SEPARATOR
<< teams()[n].village_support() << COLUMN_SEPARATOR
<< (teams()[n].uses_fog() ? _("yes") : _("no")) << COLUMN_SEPARATOR
<< (teams()[n].uses_shroud() ? _("yes") : _("no")) << COLUMN_SEPARATOR;
items.push_back(str.str());
}
if (settings_table_empty)
{
// no sides to show - display empty table
std::stringstream str;
for (int i=0;i<8;++i)
str << " " << COLUMN_SEPARATOR;
leader_bools.push_back(false);
items.push_back(str.str());
}
int result = 0;
{
leader_scroll_dialog slist(*gui_, _("Scenario Settings"), leader_bools, selected, gui::DIALOG_BACK);
slist.set_menu(items, &sorter);
slist.get_menu().move_selection(selected);
slist.add_button(new gui::dialog_button(gui_->video(), _(" < Back"),
gui::button::TYPE_PRESS, gui::DIALOG_BACK),
gui::dialog::BUTTON_EXTRA_LEFT);
result = slist.show();
selected = slist.get_menu().selection();
} // this will kill the dialog before scrolling
if (result >= 0)
gui_->scroll_to_leader(selected+1);
else if (result == gui::DIALOG_BACK)
status_table(selected);
}
void menu_handler::save_map()
{
std::string input_name = filesystem::get_dir(filesystem::get_dir(filesystem::get_user_data_dir() + "/editor") + "/maps/");
int res = 0;
int overwrite = 1;
do {
res = dialogs::show_file_chooser_dialog_save(*gui_, input_name, _("Save the Map As"), ".map");
if (res == 0) {
if (filesystem::file_exists(input_name)) {
const int res = gui2::show_message((*gui_).video(), "", _("The map already exists. Do you want to overwrite it?"), gui2::tmessage::yes_no_buttons);
overwrite = res == gui2::twindow::CANCEL ? 1 : 0;
}
else
overwrite = 0;
}
} while (res == 0 && overwrite != 0);
// Try to save the map, if it fails we reset the filename.
if (res == 0) {
try {
filesystem::write_file(input_name, map().write());
gui2::show_transient_message(gui_->video(), "", _("Map saved."));
} catch (filesystem::io_exception& e) {
utils::string_map symbols;
symbols["msg"] = e.what();
const std::string msg = vgettext("Could not save the map: $msg",symbols);
gui2::show_transient_message(gui_->video(), "", msg);
}
}
/*
std::string input_name = filesystem::get_dir(filesystem::get_dir(filesystem::get_user_data_dir() + "/editor") + "/maps/");
int res = 0;
int overwrite = 1;
do {
res = dialogs::show_file_chooser_dialog_save(*gui_, input_name, _("Save the Map As"));
if (res == 0) {
if (filesystem::file_exists(input_name)) {
const int res = gui2::show_message((*gui_).video(), "", _("The map already exists. Do you want to overwrite it?"), gui2::tmessage::yes_no_buttons);
overwrite = res == gui2::twindow::CANCEL ? 1 : 0;
}
else
overwrite = 0;
}
} while (res == 0 && overwrite != 0);
// Try to save the map, if it fails we reset the filename.
if (res == 0) {
try {
config file;
config& map = file.add_child("map");
map().write(map);
std::stringstream str;
{
config_writer writer(str, false);
writer.write(file);
}
filesystem::write_file(input_name, str.str());
gui2::show_transient_message(gui_->video(), "", _("Map saved."));
} catch (filesystem::io_exception& e) {
utils::string_map symbols;
symbols["msg"] = e.what();
const std::string msg = vgettext("Could not save the map: $msg",symbols);
gui2::show_transient_message(gui_->video(), "", msg);
}
}
*/
}
void menu_handler::preferences()
{
preferences::show_preferences_dialog(*gui_, game_config_);
// Needed after changing fullscreen/windowed mode or display resolution
gui_->redraw_everything();
}
void menu_handler::show_chat_log()
{
config c;
c["name"] = "prototype of chat log";
gui2::tchat_log chat_log_dialog(vconfig(c), resources::recorder);
chat_log_dialog.show(gui_->video());
//std::string text = resources::recorder->build_chat_log();
//gui::show_dialog(*gui_,NULL,_("Chat Log"),"",gui::CLOSE_ONLY,NULL,NULL,"",&text);
}
void menu_handler::show_help()
{
help::show_help(*gui_);
}
void menu_handler::speak()
{
textbox_info_.show(gui::TEXTBOX_MESSAGE,_("Message:"),
has_friends() ? board().is_observer() ? _("Send to observers only") : _("Send to allies only")
: "", preferences::message_private(), *gui_);
}
void menu_handler::whisper()
{
preferences::set_message_private(true);
speak();
}
void menu_handler::shout()
{
preferences::set_message_private(false);
speak();
}
bool menu_handler::has_friends() const
{
if(board().is_observer()) {
return !gui_->observers().empty();
}
for(size_t n = 0; n != teams().size(); ++n) {
if(n != gui_->viewing_team() && teams()[gui_->viewing_team()].team_name() == teams()[n].team_name() && teams()[n].is_network()) {
return true;
}
}
return false;
}
void menu_handler::recruit(int side_num, const map_location &last_hex)
{
team ¤t_team = teams()[side_num - 1];
std::vector<const unit_type*> sample_units;
gui_->draw(); //clear the old menu
std::vector<std::string> item_keys;
std::vector<std::string> items;
std::set<std::string> recruits = actions::get_recruits(side_num, last_hex);
for(std::set<std::string>::const_iterator it = recruits.begin(); it != recruits.end(); ++it) {
const unit_type *type = unit_types.find(*it);
if (!type) {
ERR_NG << "could not find unit '" << *it << "'" << std::endl;
return;
}
item_keys.push_back(*it);
char prefix;
int wb_gold = 0;
if (const boost::shared_ptr<wb::manager> & whiteb = pc_.get_whiteboard())
{ wb::future_map future; // so gold takes into account planned spending
wb_gold = whiteb->get_spent_gold_for(side_num);
//display units that we can't afford to recruit in red
} // end planned unit map scope
prefix = (type->cost() > current_team.gold() - wb_gold
? font::BAD_TEXT : font::NULL_MARKUP);
std::stringstream description;
description << font::IMAGE << type->image();
#ifndef LOW_MEM
description << "~RC(" << type->flag_rgb() << '>'
<< team::get_side_color_index(side_num) << ')';
#endif
description << COLUMN_SEPARATOR << font::LARGE_TEXT << prefix << type->type_name() << "\n"
<< prefix << type->cost() << " " << translation::sngettext("unit^Gold", "Gold", type->cost());
items.push_back(description.str());
sample_units.push_back(type);
}
if(sample_units.empty()) {
gui2::show_transient_message(gui_->video(),"",_("You have no units available to recruit."));
return;
}
int recruit_res = dialogs::recruit_dialog(*gui_, sample_units, items, side_num, get_title_suffix(side_num));
if(recruit_res != -1) {
do_recruit(item_keys[recruit_res], side_num, last_hex);
}
}
void menu_handler::repeat_recruit(int side_num, const map_location &last_hex)
{
const std::string & last_recruit = teams()[side_num - 1].last_recruit();
if ( last_recruit.empty() == false )
do_recruit(last_recruit, side_num, last_hex);
}
bool menu_handler::do_recruit(const std::string &name, int side_num,
const map_location &last_hex)
{
team ¤t_team = teams()[side_num - 1];
//search for the unit to be recruited in recruits
if ( !util::contains(actions::get_recruits(side_num, last_hex), name) )
return false;
const unit_type *u_type = unit_types.find(name);
assert(u_type);
if (u_type->cost() > current_team.gold() - (pc_.get_whiteboard() ? pc_.get_whiteboard()->get_spent_gold_for(side_num) : 0)) {
gui2::show_transient_message(gui_->video(), "",
_("You don’t have enough gold to recruit that unit"));
return false;
}
current_team.last_recruit(name);
const events::command_disabler disable_commands;
map_location loc = last_hex;
map_location recruited_from = map_location::null_location();
std::string msg;
{ wb::future_map_if_active future; /* start planned unit map scope if in planning mode */
msg = actions::find_recruit_location(side_num, loc, recruited_from, name);
} // end planned unit map scope
if (!msg.empty()) {
gui2::show_transient_message(gui_->video(), "", msg);
return false;
}
if (!pc_.get_whiteboard() || !pc_.get_whiteboard()->save_recruit(name, side_num, loc)) {
//MP_COUNTDOWN grant time bonus for recruiting
current_team.set_action_bonus_count(1 + current_team.action_bonus_count());
// Do the recruiting.
synced_context::run_and_throw("recruit", replay_helper::get_recruit(u_type->id(), loc, recruited_from));
return true;
}
return false;
}
void menu_handler::recall(int side_num, const map_location &last_hex)
{
if (pc_.get_disallow_recall()) {
gui2::show_transient_message(gui_->video(),"",_("You are separated from your soldiers and may not recall them"));
return;
}
team ¤t_team = teams()[side_num - 1];
boost::shared_ptr<std::vector<unit_const_ptr > > recall_list_team = boost::make_shared<std::vector<unit_const_ptr> >();
{ wb::future_map future; // ensures recall list has planned recalls removed
*recall_list_team = actions::get_recalls(side_num, last_hex);
}
gui_->draw(); //clear the old menu
DBG_WB <<"menu_handler::recall: Contents of wb-modified recall list:\n";
BOOST_FOREACH(const unit_const_ptr & unit, *recall_list_team)
{
DBG_WB << unit->name() << " [" << unit->id() <<"]\n";
}
if(current_team.recall_list().empty()) {
gui2::show_transient_message(gui_->video(), "",
_("There are no troops available to recall\n(You must have"
" veteran survivors from a previous scenario)"));
return;
}
if(recall_list_team->empty()) {
gui2::show_transient_message(gui_->video(), "",
_("You currently can't recall at the highlighted location"));
return;
}
int res = dialogs::recall_dialog(*gui_, recall_list_team, side_num, get_title_suffix(side_num), current_team.recall_cost());
int unit_cost = current_team.recall_cost();
if (res < 0) {
return;
}
// we need to check if unit has a specific recall cost
// if it does we use it elsewise we use the team.recall_cost()
// the magic number -1 is what it gets set to if the unit doesn't
// have a special recall_cost of its own.
else if(recall_list_team->at(res)->recall_cost() > -1) {
unit_cost = recall_list_team->at(res)->recall_cost();
}
int wb_gold = pc_.get_whiteboard() ? pc_.get_whiteboard()->get_spent_gold_for(side_num) : 0;
if (current_team.gold() - wb_gold < unit_cost) {
utils::string_map i18n_symbols;
i18n_symbols["cost"] = lexical_cast<std::string>(unit_cost);
std::string msg = vngettext(
"You must have at least 1 gold piece to recall a unit",
"You must have at least $cost gold pieces to recall this unit",
unit_cost, i18n_symbols);
gui2::show_transient_message(gui_->video(), "", msg);
return;
}
LOG_NG << "recall index: " << res << "\n";
const events::command_disabler disable_commands;
map_location recall_location = last_hex;
map_location recall_from = map_location::null_location();
std::string err;
{ wb::future_map_if_active future; // future unit map removes invisible units from map, don't do this outside of planning mode
err = actions::find_recall_location(side_num, recall_location, recall_from, *(recall_list_team->at(res)));
} // end planned unit map scope
if(!err.empty()) {
gui2::show_transient_message(gui_->video(), "", err);
return;
}
if (!pc_.get_whiteboard() || !pc_.get_whiteboard()->save_recall(*recall_list_team->at(res), side_num, recall_location)) {
bool success = synced_context::run_and_throw("recall",
replay_helper::get_recall(recall_list_team->at(res)->id(), recall_location, recall_from),
true,
true,
synced_context::ignore_error_function);
if(!success)
{
ERR_NG << "menu_handler::recall(): Unit does not exist in the recall list." << std::endl;
}
}
}
// Highlights squares that an enemy could move to on their turn, showing how many can reach each square.
void menu_handler::show_enemy_moves(bool ignore_units, int side_num)
{
wb::future_map future; // use unit positions as if all planned actions were executed
gui_->unhighlight_reach();
// Compute enemy movement positions
for(unit_map::iterator u = units().begin(); u != units().end(); ++u) {
bool invisible = u->invisible(u->get_location());
if (teams()[side_num - 1].is_enemy(u->side()) &&
!gui_->fogged(u->get_location()) && !u->incapacitated() && !invisible)
{
const unit_movement_resetter move_reset(*u);
const pathfind::paths& path = pathfind::paths(*u, false, true,
teams()[gui_->viewing_team()], 0, false, ignore_units);
gui_->highlight_another_reach(path);
}
}
}
void menu_handler::toggle_shroud_updates(int side_num)
{
team ¤t_team = teams()[side_num - 1];
bool auto_shroud = current_team.auto_shroud_updates();
// If we're turning automatic shroud updates on, then commit all moves
if (!auto_shroud) update_shroud_now(side_num);
// Toggle the setting and record this.
synced_context::run_and_throw("auto_shroud", replay_helper::get_auto_shroud(!auto_shroud));
}
void menu_handler::update_shroud_now(int /* side_num */)
{
synced_context::run_and_throw("update_shroud", replay_helper::get_update_shroud());
}
namespace { // Helpers for menu_handler::end_turn()
/**
* Returns true if @a side_num has at least one living unit.
*/
bool units_alive(int side_num, const unit_map & units)
{
for ( unit_map::const_iterator un = units.begin(); un != units.end(); ++un ) {
if ( un->side() == side_num )
return true;
}
return false;
}
/**
* Returns true if @a side_num has at least one unit that can still move.
*/
bool partmoved_units(int side_num, const unit_map & units, const game_board & board, const boost::shared_ptr<wb::manager> & whiteb)
{
for ( unit_map::const_iterator un = units.begin(); un != units.end(); ++un ) {
if ( un->side() == side_num ) {
// @todo whiteboard should take into consideration units that have
// a planned move but can still plan more movement in the same turn
if ( board.unit_can_move(*un) && !un->user_end_turn()
&& (!whiteb || !whiteb->unit_has_actions(&*un)) )
return true;
}
}
return false;
}
/**
* Returns true if @a side_num has at least one unit that (can but) has not
* moved.
*/
bool unmoved_units(int side_num, const unit_map & units, const game_board & board, const boost::shared_ptr<wb::manager> & whiteb)
{
for ( unit_map::const_iterator un = units.begin(); un != units.end(); ++un ) {
if ( un->side() == side_num ) {
if ( board.unit_can_move(*un) && !un->has_moved() && !un->user_end_turn()
&& (!whiteb || !whiteb->unit_has_actions(&*un)) )
return true;
}
}
return false;
}
}
bool menu_handler::end_turn(int side_num)
{
if(!gamedata().allow_end_turn()) {
gui2::show_message((*gui_).video(), "", _("You cannot end your turn yet!"), gui2::tmessage::ok_button);
return false;
}
size_t team_num = static_cast<size_t>(side_num - 1);
if ( team_num < teams().size() && teams()[team_num].no_turn_confirmation() ) {
// Skip the confirmations that follow.
}
// Ask for confirmation if the player hasn't made any moves.
else if ( preferences::confirm_no_moves() &&
!pc_.get_undo_stack().player_acted() &&
(!pc_.get_whiteboard() || !pc_.get_whiteboard()->current_side_has_actions()) &&
units_alive(side_num, units()) )
{
const int res = gui2::show_message((*gui_).video(), "", _("You have not started your turn yet. Do you really want to end your turn?"), gui2::tmessage::yes_no_buttons);
if(res == gui2::twindow::CANCEL) {
return false;
}
}
// Ask for confirmation if units still have some movement left.
else if ( preferences::yellow_confirm() && partmoved_units(side_num, units(), board(), pc_.get_whiteboard()) ) {
const int res = gui2::show_message((*gui_).video(), "", _("Some units have movement left. Do you really want to end your turn?"), gui2::tmessage::yes_no_buttons);
if(res == gui2::twindow::CANCEL) {
return false;
}
}
// Ask for confirmation if units still have all movement left.
else if ( preferences::green_confirm() && unmoved_units(side_num, units(), board(), pc_.get_whiteboard()) ) {
const int res = gui2::show_message((*gui_).video(), "", _("Some units have not moved. Do you really want to end your turn?"), gui2::tmessage::yes_no_buttons);
if(res == gui2::twindow::CANCEL) {
return false;
}
}
// Auto-execute remaining whiteboard planned actions
// Only finish turn if they all execute successfully, i.e. no ambush, etc.
if (pc_.get_whiteboard() && !pc_.get_whiteboard()->allow_end_turn()) {
return false;
}
return true;
}
void menu_handler::goto_leader(int side_num)
{
unit_map::const_iterator i = units().find_leader(side_num);
if(i != units().end()) {
gui_->scroll_to_tile(i->get_location(), game_display::WARP);
}
}
void menu_handler::unit_description()
{
const unit_map::const_iterator un = current_unit();
if(un != units().end()) {
help::show_unit_description(*un);
}
}
void menu_handler::terrain_description(mouse_handler& mousehandler)
{
const map_location& loc = mousehandler.get_last_hex();
if (map().on_board(loc) == false || gui_->shrouded(loc)) {
return;
}
const terrain_type& type = map().get_terrain_info(loc);
//const terrain_type& info = board().map().get_terrain_info(terrain);
help::show_terrain_description(type);
}
void menu_handler::rename_unit()
{
const unit_map::iterator un = current_unit();
if (un == units().end() || gui_->viewing_side() != un->side())
return;
if (un->unrenamable())
return;
std::string name = un->name();
const std::string title(N_("Rename Unit"));
const std::string label(N_("Name:"));
if(gui2::tedit_text::execute(title, label, name, gui_->video())) {
resources::recorder->add_rename(name, un->get_location());
un->rename(name);
gui_->invalidate_unit();
}
}
unit_map::iterator menu_handler::current_unit()
{
const mouse_handler& mousehandler = pc_.get_mouse_handler_base();
unit_map::iterator res = board().find_visible_unit(mousehandler.get_last_hex(),
teams()[gui_->viewing_team()]);
if(res != units().end()) {
return res;
} else {
return board().find_visible_unit(mousehandler.get_selected_hex(),
teams()[gui_->viewing_team()]);
}
}
namespace { // Helpers for create_unit()
/// Allows a function to return both a type and a gender.
typedef std::pair<const unit_type *, unit_race::GENDER> type_and_gender;
/**
* Allows the user to select a type of unit, using GUI2.
* (Intended for use when a unit is created in debug mode via hotkey or
* context menu.)
* @returns the selected type and gender. If this is canceled, the
* returned type is NULL.
*
* @todo Replace choose_unit() when complete.
*/
type_and_gender choose_unit_2(game_display& gui)
{
//
// The unit creation dialog makes sure unit types
// are properly cached.
//
gui2::tunit_create create_dlg;
create_dlg.show(gui.video());
if(create_dlg.no_choice()) {
// the static cast fixes http://connect.microsoft.com/VisualStudio/feedback/details/520043/
// c++11's nullptr would be a better solution as soon as we support it.
return type_and_gender(static_cast<const unit_type *>(NULL), unit_race::NUM_GENDERS);
}
const std::string& ut_id = create_dlg.choice();
const unit_type *utp = unit_types.find(ut_id);
if (!utp) {
ERR_NG << "Create unit dialog returned nonexistent or unusable unit_type id '" << ut_id << "'." << std::endl;
return type_and_gender(static_cast<const unit_type *>(NULL), unit_race::NUM_GENDERS);
}
const unit_type &ut = *utp;
unit_race::GENDER gender = create_dlg.gender();
// Do not try to set bad genders, may mess up l10n
/// @todo Is this actually necessary?
/// (Maybe create_dlg can enforce proper gender selection?)
if(ut.genders().end() == std::find(ut.genders().begin(), ut.genders().end(), gender)) {
gender = ut.genders().front();
}
return type_and_gender(utp, gender);
}