-
-
Notifications
You must be signed in to change notification settings - Fork 991
/
mouse_events.cpp
1291 lines (1078 loc) · 40.4 KB
/
mouse_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.
*/
#include "mouse_events.hpp"
#include "global.hpp"
#include "actions/attack.hpp" // for battle_context, etc
#include "actions/move.hpp" // for move_and_record
#include "actions/undo.hpp" // for undo_list
#include "attack_prediction_display.hpp"
#include "config.hpp" // for config
#include "cursor.hpp" // for set, CURSOR_TYPE::NORMAL, etc
#include "dialogs.hpp" // for units_list_preview_pane, etc
#include "game_board.hpp" // for game_board, etc
#include "game_config.hpp" // for red_to_green
#include "game_events/manager.hpp"
#include "game_events/pump.hpp" // for fire
#include "gettext.hpp" // for _
#include "gui/dialogs/transient_message.hpp" // for show_transient_message
#include "gui/dialogs/unit_attack.hpp" // for tunit_attack
#include "gui/widgets/settings.hpp" // for new_widgets
#include "gui/widgets/window.hpp" // for enum
#include "language.hpp" // for string_table, symbol_table
#include "log.hpp" // for LOG_STREAM, logger, etc
#include "map.hpp" // for gamemap
#include "marked-up_text.hpp" // for color2markup, BOLD_TEXT, etc
#include "pathfind/teleport.hpp" // for get_teleport_locations, etc
#include "play_controller.hpp" // for playing_side, set_button_state
#include "replay_helper.hpp"
#include "sdl/utils.hpp" // for int_to_color
#include "serialization/string_utils.hpp" // for unicode_em_dash
#include "show_dialog.hpp" // for dialog_button_info, etc
#include "sound.hpp"
#include "synced_context.hpp"
#include "team.hpp" // for team
#include "tod_manager.hpp"
#include "tstring.hpp" // for t_string
#include "unit.hpp" // for unit, intrusive_ptr_add_ref
#include "unit_animation_component.hpp"
#include "unit_ptr.hpp" // for unit_const_ptr
#include "unit_types.hpp" // for attack_type
#include "whiteboard/manager.hpp" // for manager, etc
#include "whiteboard/typedefs.hpp" // for whiteboard_lock
#include "wml_separators.hpp" // for COLUMN_SEPARATOR, etc
#include <boost/foreach.hpp> // for auto_any_base, etc
#include <boost/intrusive_ptr.hpp> // for intrusive_ptr
#include <boost/shared_ptr.hpp> // for shared_ptr
#include <cassert> // for assert
#include <cstddef> // for NULL
#include <new> // for bad_alloc
#include <ostream> // for operator<<, basic_ostream, etc
#include <string> // for string, operator<<, etc
#include "SDL_mouse.h" // for SDL_GetMouseState
#include "SDL_video.h" // for SDL_Color
namespace gui { class slider; }
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{
mouse_handler::mouse_handler(game_display* gui, play_controller & pc) :
mouse_handler_base(),
gui_(gui),
pc_(pc),
previous_hex_(),
previous_free_hex_(),
selected_hex_(),
next_unit_(),
current_route_(),
current_paths_(),
unselected_paths_(false),
path_turns_(0),
side_num_(1),
over_route_(false),
reachmap_invalid_(false),
show_partial_move_(false)
{
singleton_ = this;
}
mouse_handler::~mouse_handler()
{
singleton_ = NULL;
}
void mouse_handler::set_side(int side_number)
{
side_num_ = side_number;
}
int mouse_handler::drag_threshold() const
{
return 14;
}
void mouse_handler::mouse_motion(int x, int y, const bool browse, bool update, map_location new_hex)
{
// we ignore the position coming from event handler
// because it's always a little obsolete and we don't need
// to highlight all the hexes where the mouse passed.
// Also, sometimes it seems to have one *very* obsolete
// and isolated mouse motion event when using drag&drop
SDL_GetMouseState(&x,&y); // <-- modify x and y
if (mouse_handler_base::mouse_motion_default(x, y, update)) return;
game_board & board = pc_.gamestate().board_;
if (new_hex == map_location::null_location())
new_hex = gui().hex_clicked_on(x,y);
if(new_hex != last_hex_) {
update = true;
if ( pc_.get_map_const().on_board(last_hex_) ) {
// we store the previous hexes used to propose attack direction
previous_hex_ = last_hex_;
// the hex of the selected unit is also "free"
{ // start planned unit map scope
wb::future_map_if_active raii;
if (last_hex_ == selected_hex_ || !find_unit(last_hex_)) {
previous_free_hex_ = last_hex_;
}
} // end planned unit map scope
}
last_hex_ = new_hex;
}
if (reachmap_invalid_) update = true;
if (!update) return;
if (reachmap_invalid_) {
reachmap_invalid_ = false;
if (!current_paths_.destinations.empty() && !show_partial_move_) {
bool selected_hex_has_unit;
{ // start planned unit map scope
wb::future_map_if_active planned_unit_map;
selected_hex_has_unit = find_unit(selected_hex_).valid();
} // end planned unit map scope
if(selected_hex_.valid() && selected_hex_has_unit ) {
// reselect the unit without firing events (updates current_paths_)
select_hex(selected_hex_, true);
}
// we do never deselect here, mainly because of canceled attack-move
}
}
// reset current_route_ and current_paths if not valid anymore
// we do it before cursor selection, because it uses current_paths_
if( !pc_.get_map_const().on_board(new_hex) ) {
current_route_.steps.clear();
gui().set_route(NULL);
pc_.get_whiteboard()->erase_temp_move();
}
if(unselected_paths_) {
unselected_paths_ = false;
current_paths_ = pathfind::paths();
gui().unhighlight_reach();
} else if(over_route_) {
over_route_ = false;
current_route_.steps.clear();
gui().set_route(NULL);
pc_.get_whiteboard()->erase_temp_move();
}
gui().highlight_hex(new_hex);
pc_.get_whiteboard()->on_mouseover_change(new_hex);
unit_map::iterator selected_unit;
unit_map::iterator mouseover_unit;
map_location attack_from;
{ // start planned unit map scope
wb::future_map_if_active planned_unit_map;
selected_unit = find_unit(selected_hex_);
mouseover_unit = find_unit(new_hex);
// we search if there is an attack possibility and where
attack_from = current_unit_attacks_from(new_hex);
//see if we should show the normal cursor, the movement cursor, or
//the attack cursor
//If the cursor is on WAIT, we don't change it and let the setter
//of this state end it
if (cursor::get() != cursor::WAIT) {
if (selected_unit &&
selected_unit->side() == side_num_ &&
!selected_unit->incapacitated() && !browse)
{
if (attack_from.valid()) {
cursor::set(dragging_started_ ? cursor::ATTACK_DRAG : cursor::ATTACK);
}
else if (!mouseover_unit &&
current_paths_.destinations.contains(new_hex))
{
cursor::set(dragging_started_ ? cursor::MOVE_DRAG : cursor::MOVE);
} else {
// selected unit can't attack or move there
cursor::set(cursor::NORMAL);
}
} else {
// no selected unit or we can't move it
if ( selected_hex_.valid() && mouseover_unit
&& mouseover_unit->side() == side_num_ ) {
// empty hex field selected and unit on our site under the cursor
cursor::set(dragging_started_ ? cursor::MOVE_DRAG : cursor::MOVE);
} else {
cursor::set(cursor::NORMAL);
}
}
}
} // end planned unit map scope
// show (or cancel) the attack direction indicator
if (attack_from.valid() && (!browse || pc_.get_whiteboard()->is_active())) {
gui().set_attack_indicator(attack_from, new_hex);
} else {
gui().clear_attack_indicator();
}
unit_ptr un; //will later point to unit at mouseover_hex_
// the destination is the pointed hex or the adjacent hex
// used to attack it
map_location dest;
unit_map::const_iterator dest_un;
{ // start planned unit map scope
wb::future_map_if_active raii;
if (attack_from.valid()) {
dest = attack_from;
dest_un = find_unit(dest);
} else {
dest = new_hex;
dest_un = find_unit(new_hex);
}
if(dest == selected_hex_ || dest_un) {
current_route_.steps.clear();
gui().set_route(NULL);
pc_.get_whiteboard()->erase_temp_move();
}
else if (!current_paths_.destinations.empty() &&
board.map().on_board(selected_hex_) && board.map().on_board(new_hex))
{
if (selected_unit && !selected_unit->incapacitated()) {
// Show the route from selected unit to mouseover hex
current_route_ = get_route(&*selected_unit, dest, viewing_team());
pc_.get_whiteboard()->create_temp_move();
if(!browse) {
gui().set_route(¤t_route_);
}
}
}
if(board.map().on_board(selected_hex_)
&& !selected_unit
&& mouseover_unit.valid()
&& mouseover_unit) {
// Show the route from selected hex to mouseover unit
current_route_ = get_route(&*mouseover_unit, selected_hex_, viewing_team());
pc_.get_whiteboard()->create_temp_move();
if(!browse) {
gui().set_route(¤t_route_);
}
} else if (!selected_unit) {
current_route_.steps.clear();
gui().set_route(NULL);
pc_.get_whiteboard()->erase_temp_move();
}
unit_map::iterator iter = mouseover_unit;
if (iter)
un = iter.get_shared_ptr();
else
un.reset();
} //end planned unit map scope
if ( (!selected_hex_.valid()) && un && current_paths_.destinations.empty() &&
!gui().fogged(un->get_location()))
{
if (un->side() == side_num_) {
//unit is on our team, show path if the unit has one
const map_location go_to = un->get_goto();
if(board.map().on_board(go_to)) {
pathfind::marked_route route;
{ // start planned unit map scope
wb::future_map_if_active raii;
route = get_route(un.get(), go_to, current_team());
} // end planned unit map scope
gui().set_route(&route);
}
over_route_ = true;
wb::future_map_if_active raii;
current_paths_ = pathfind::paths(*un, false, true,
viewing_team(), path_turns_);
} else {
//unit under cursor is not on our team
//Note: planned unit map must be activated after this is done,
//since the future state includes changes to units' movement.
unit_movement_resetter move_reset(*un);
wb::future_map_if_active raii;
current_paths_ = pathfind::paths(*un, false, true,
viewing_team(), path_turns_);
}
unselected_paths_ = true;
gui().highlight_reach(current_paths_);
}
}
unit_map::iterator mouse_handler::selected_unit()
{
unit_map::iterator res = find_unit(selected_hex_);
if(res) {
return res;
} else {
return find_unit(last_hex_);
}
}
unit_map::iterator mouse_handler::find_unit(const map_location& hex)
{
unit_map::iterator it = pc_.gamestate().board_.find_visible_unit(hex, viewing_team());
if (it.valid())
return it;
else
return pc_.gamestate().board_.units_.end();
}
unit_map::const_iterator mouse_handler::find_unit(const map_location& hex) const
{
return pc_.gamestate().board_.find_visible_unit(hex, viewing_team());
}
map_location mouse_handler::current_unit_attacks_from(const map_location& loc) const
{
if(loc == selected_hex_)
return map_location();
bool wb_active = pc_.get_whiteboard()->is_active();
{
// Check the unit SOURCE of the attack
// Check that there's a selected unit
const unit_map::const_iterator source_unit = find_unit(selected_hex_);
bool source_eligible = source_unit.valid();
if (!source_eligible) return map_location();
// The selected unit must at least belong to the player currently controlling this client.
source_eligible &= source_unit->side() == gui_->viewing_side();
if (!source_eligible) return map_location();
// In addition:
// - If whiteboard is enabled, we allow planning attacks outside of player's turn
// - If whiteboard is disabled, it must be the turn of the player controlling this client
if(!wb_active) {
source_eligible &= gui_->viewing_side() == pc_.current_side();
if (!source_eligible) return map_location();
}
// Unit must have attacks left
source_eligible &= source_unit->attacks_left() != 0;
if (!source_eligible) return map_location();
// Check the unit TARGET of the attack
team const& viewer = viewing_team();
// Check that there's a unit at the target location
const unit_map::const_iterator target_unit = find_unit(loc);
bool target_eligible = target_unit.valid();
if (!target_eligible) return map_location();
// The player controlling this client must be an enemy of the target unit's side
target_eligible &= viewer.is_enemy(target_unit->side());
if (!target_eligible) return map_location();
// Sanity check: source and target of the attack shouldn't be on the same team
assert(source_unit->side() != target_unit->side());
target_eligible &= !target_unit->incapacitated();
if (!target_eligible) return map_location();
}
const map_location::DIRECTION preferred = loc.get_relative_dir(previous_hex_);
const map_location::DIRECTION second_preferred = loc.get_relative_dir(previous_free_hex_);
int best_rating = 100;//smaller is better
map_location res;
map_location adj[6];
get_adjacent_tiles(loc,adj);
for(size_t n = 0; n != 6; ++n) {
if(pc_.gamestate().board_.map().on_board(adj[n]) == false) {
continue;
}
if(adj[n] != selected_hex_ && find_unit(adj[n])) {
continue;
}
if (current_paths_.destinations.contains(adj[n]))
{
static const size_t NDIRECTIONS = map_location::NDIRECTIONS;
unsigned int difference = abs(int(preferred - n));
if(difference > NDIRECTIONS/2) {
difference = NDIRECTIONS - difference;
}
unsigned int second_difference = abs(int(second_preferred - n));
if(second_difference > NDIRECTIONS/2) {
second_difference = NDIRECTIONS - second_difference;
}
const int rating = difference * 2 + (second_difference > difference);
if(rating < best_rating || res.valid() == false) {
best_rating = rating;
res = adj[n];
}
}
}
return res;
}
pathfind::marked_route mouse_handler::get_route(const unit* un, map_location go_to, team &team) const
{
game_board & board = pc_.gamestate().board_;
// The pathfinder will check unit visibility (fogged/stealthy).
const pathfind::shortest_path_calculator calc(*un, team, board.teams(), board.map());
pathfind::teleport_map allowed_teleports = pathfind::get_teleport_locations(*un, viewing_team());
pathfind::plain_route route;
route = pathfind::a_star_search(un->get_location(), go_to, 10000.0, &calc, board.map().w(), board.map().h(), &allowed_teleports);
return mark_route(route);
}
void mouse_handler::mouse_press(const SDL_MouseButtonEvent& event, const bool browse)
{
mouse_handler_base::mouse_press(event, browse);
}
bool mouse_handler::right_click_show_menu(int x, int y, const bool /*browse*/)
{
return ( selected_hex_.valid() ? false :
sdl::point_in_rect(x, y, gui().map_area()) );
}
void mouse_handler::left_mouse_up(int /*x*/, int /*y*/, const bool /*browse*/)
{
gui::slider* s = gui_->find_slider("map-zoom-slider");
if (s && s->value_change())
if (gui_->set_zoom(s->value(), true))
pc_.get_hotkey_command_executor()->set_button_state(*gui_);
}
void mouse_handler::mouse_wheel_up(int /*x*/, int /*y*/, const bool /*browse*/)
{
gui::slider* s = gui_->find_slider("map-zoom-slider");
if (s && s->value_change())
if (gui_->set_zoom(s->value(), true))
pc_.get_hotkey_command_executor()->set_button_state(*gui_);
}
void mouse_handler::mouse_wheel_down(int /*x*/, int /*y*/, const bool /*browse*/)
{
gui::slider* s = gui_->find_slider("map-zoom-slider");
if (s && s->value_change())
if (gui_->set_zoom(s->value(), true))
pc_.get_hotkey_command_executor()->set_button_state(*gui_);
}
void mouse_handler::mouse_wheel_left(int /*x*/, int /*y*/, const bool /*browse*/)
{
gui::slider* s = gui_->find_slider("map-zoom-slider");
if (s && s->value_change())
if (gui_->set_zoom(s->value(), true))
pc_.get_hotkey_command_executor()->set_button_state(*gui_);
}
void mouse_handler::mouse_wheel_right(int /*x*/, int /*y*/, const bool /*browse*/)
{
gui::slider* s = gui_->find_slider("map-zoom-slider");
if (s && s->value_change())
if (gui_->set_zoom(s->value(), true))
pc_.get_hotkey_command_executor()->set_button_state(*gui_);
}
void mouse_handler::select_or_action(bool browse)
{
if (!pc_.get_map_const().on_board(last_hex_))
return;
// Load whiteboard partial moves
wb::future_map_if_active planned_unit_map;
unit_map::iterator clicked_u = find_unit(last_hex_);
unit_map::iterator selected_u = find_unit(selected_hex_);
if ( clicked_u &&
(!selected_u ||
selected_u->side() != side_num_ ||
(clicked_u->side() == side_num_ && clicked_u->id() != selected_u->id())) )
{
select_hex(last_hex_, false);
}
else
{
move_action(browse);
}
}
void mouse_handler::move_action(bool browse)
{
// Lock whiteboard activation state to avoid problems due to
// its changing while an animation takes place.
wb::whiteboard_lock wb_lock = pc_.get_whiteboard()->get_activation_state_lock();
//we use the last registered highlighted hex
//since it's what update our global state
map_location hex = last_hex_;
// TODO
// // Clicks on border hexes mean to deselect.
// // (Check this before doing processing that might not be needed.)
// if ( !pc_.get_map_const().on_board(hex) ) {
// deselect_hex();
// return false;
// }
unit_map::iterator u;
unit_map::iterator clicked_u;
map_location src;
pathfind::paths orig_paths;
map_location attack_from;
{ // start planned unit map scope
wb::future_map_if_active planned_unit_map;
u = find_unit(selected_hex_);
//if the unit is selected and then itself clicked on,
//any goto command is canceled
if (u && !browse && selected_hex_ == hex && u->side() == side_num_) {
u->set_goto(map_location());
}
clicked_u = find_unit(hex);
src = selected_hex_;
orig_paths = current_paths_;
attack_from = current_unit_attacks_from(hex);
} // end planned unit map scope
//see if we're trying to do a attack or move-and-attack
if((!browse || pc_.get_whiteboard()->is_active()) && attack_from.valid()) {
// Ignore this command if commands are disabled.
if ( commands_disabled )
return;
if (((u.valid() && u->side() == side_num_) || pc_.get_whiteboard()->is_active()) && clicked_u.valid() ) {
if (attack_from == selected_hex_) { //no move needed
int choice = -1;
{ wb::future_map_if_active planned_unit_map; //start planned unit map scope
choice = show_attack_dialog(attack_from, clicked_u->get_location());
} // end planned unit map scope
if (choice >=0 ) {
if (pc_.get_whiteboard()->is_active()) {
save_whiteboard_attack(attack_from, clicked_u->get_location(), choice);
} else {
// clear current unit selection so that any other unit selected
// triggers a new selection
selected_hex_ = map_location();
attack_enemy(u->get_location(), clicked_u->get_location(), choice);
}
}
return;
}
else {
int choice = -1; //for the attack dialog
{ wb::future_map_if_active planned_unit_map; //start planned unit map scope
// we will now temporary move next to the enemy
pathfind::paths::dest_vect::const_iterator itor =
current_paths_.destinations.find(attack_from);
if(itor == current_paths_.destinations.end()) {
// can't reach the attacking location
// not supposed to happen, so abort
return;
}
// block where we temporary move the unit
{
temporary_unit_mover temp_mover(pc_.gamestate().board_.units_, src, attack_from,
itor->move_left);
choice = show_attack_dialog(attack_from, clicked_u->get_location());
}
if (choice < 0) {
// user hit cancel, don't start move+attack
return;
}
} // end planned unit map scope
if (pc_.get_whiteboard()->is_active()) {
save_whiteboard_attack(attack_from, hex, choice);
}
else {
bool not_interrupted = move_unit_along_current_route();
bool alt_unit_selected = (selected_hex_ != src);
src = selected_hex_;
// clear current unit selection so that any other unit selected
// triggers a new selection
selected_hex_ = map_location();
if (not_interrupted)
attack_enemy(attack_from, hex, choice); // Fight !!
//TODO: Maybe store the attack choice so "press t to continue"
// can also continue the attack?
if (alt_unit_selected && !selected_hex_.valid()) {
//reselect other unit if selected during movement animation
select_hex(src, browse);
}
}
return;
}
}
}
//otherwise we're trying to move to a hex
else if (
// the old use case: move selected unit to mouse hex field
( (!browse || pc_.get_whiteboard()->is_active()) &&
selected_hex_.valid() && selected_hex_ != hex &&
u.valid() &&
(u->side() == side_num_ || pc_.get_whiteboard()->is_active()) &&
!clicked_u &&
!current_route_.steps.empty() &&
current_route_.steps.front() == selected_hex_
)
|| // the new use case: move mouse unit to selected hex field
( (!browse || pc_.get_whiteboard()->is_active()) &&
selected_hex_.valid() && selected_hex_ != hex &&
clicked_u &&
!current_route_.steps.empty() &&
current_route_.steps.back() == selected_hex_
&& !u
&& clicked_u->side() == side_num_
)
) {
// Ignore this command if commands are disabled.
if ( commands_disabled )
return;
// If the whiteboard is active, it intercepts any unit movement
if (pc_.get_whiteboard()->is_active()) {
// deselect the current hex, and create planned move for whiteboard
selected_hex_ = map_location();
gui().select_hex(map_location());
gui().clear_attack_indicator();
gui().set_route(NULL);
show_partial_move_ = false;
gui().unhighlight_reach();
current_paths_ = pathfind::paths();
current_route_.steps.clear();
pc_.get_whiteboard()->save_temp_move();
// Otherwise proceed to normal unit movement
} else {
//Don't move if the unit already has actions
//from the whiteboard.
if (pc_.get_whiteboard()->unit_has_actions(
u ? &*u : &*clicked_u )) {
return;
}
move_unit_along_current_route();
// during the move, we may have selected another unit
// (but without triggering a select event (command was disabled)
// in that case reselect it now to fire the event (+ anim & sound)
if (selected_hex_ != src) {
select_hex(selected_hex_, browse);
}
}
return;
}
}
void mouse_handler::select_hex(const map_location& hex, const bool browse, const bool highlight, const bool fire_event) {
selected_hex_ = hex;
gui().select_hex(selected_hex_);
gui().clear_attack_indicator();
gui().set_route(NULL);
show_partial_move_ = false;
wb::future_map_if_active planned_unit_map; //lasts for whole method
unit_map::iterator u = find_unit(selected_hex_);
if (selected_hex_.valid() && u.valid() && !u->get_hidden()) {
next_unit_ = u->get_location();
{
current_paths_ = pathfind::paths(*u, false, true, viewing_team(), path_turns_);
}
if(highlight) {
show_attack_options(u);
gui().highlight_reach(current_paths_);
}
// the highlight now comes from selection
// and not from the mouseover on an enemy
unselected_paths_ = false;
gui().set_route(NULL);
// selection have impact only if we are not observing and it's our unit
if ((!commands_disabled || pc_.get_whiteboard()->is_active()) && u->side() == gui().viewing_side()) {
if (!(browse || pc_.get_whiteboard()->unit_has_actions(&*u)))
{
sound::play_UI_sound("select-unit.wav");
u->anim_comp().set_selecting();
if(fire_event) {
// ensure unit map is back to normal while event is fired
wb::real_map srum;
pc_.pump().fire("select", hex);
//end forced real unit map
}
}
}
return;
}
if (selected_hex_.valid() && !u) {
// compute unit in range of the empty selected_hex field
gui_->unhighlight_reach();
pathfind::paths reaching_unit_locations;
pathfind::paths clicked_location;
clicked_location.destinations.insert(hex);
for(unit_map::iterator u = pc_.gamestate().board_.units_.begin(); u != pc_.gamestate().board_.units_.end(); ++u) {
bool invisible = u->invisible(u->get_location());
if (!gui_->fogged(u->get_location()) && !u->incapacitated() && !invisible)
{
const pathfind::paths& path = pathfind::paths(*u, false, true,
viewing_team(), path_turns_, false, false);
if (path.destinations.find(hex) != path.destinations.end()) {
reaching_unit_locations.destinations.insert(u->get_location());
gui_->highlight_another_reach(clicked_location);
}
}
}
gui_->highlight_another_reach(reaching_unit_locations);
} else {
if (!pc_.gamestate().board_.units_.find(last_hex_)) {
gui_->unhighlight_reach();
}
current_paths_ = pathfind::paths();
current_route_.steps.clear();
pc_.get_whiteboard()->on_deselect_hex();
}
}
void mouse_handler::deselect_hex() {
select_hex(map_location(), true);
}
/**
* Moves a unit along the currently cached route.
*
* @returns true if the end of the route was reached and no information was
* uncovered that would warrant interrupting a chain of actions;
* false otherwise.
*/
bool mouse_handler::move_unit_along_current_route()
{
// Copy the current route to ensure it remains valid throughout the animation.
const std::vector<map_location> steps = current_route_.steps;
// do not show footsteps during movement
gui().set_route(NULL);
gui().unhighlight_reach();
// do not keep the hex highlighted that we started from
selected_hex_ = map_location();
gui().select_hex(map_location());
bool interrupted = false;
if ( steps.size() > 1 )
{
size_t num_moves = move_unit_along_route(steps, interrupted);
interrupted = interrupted || num_moves + 1 < steps.size();
next_unit_ = steps[num_moves];
}
// invalid after the move
current_paths_ = pathfind::paths();
current_route_.steps.clear();
return !interrupted;
}
/**
* Moves a unit across the board for a player.
* This is specifically for movement at the time it is initiated by a player,
* whether via a mouse click or executing whiteboard actions. Continued moves
* (including goto execution) can bypass this and call actions::move_unit() directly.
* This function call may include time for an animation, so make sure the
* provided route will remain unchanged (the caller should probably make a local
* copy).
*
* @param[in] steps The route to be traveled. The unit to be moved is at the beginning of this route.
* @param[out] interrupted This is set to true if information was uncovered that warrants interrupting a chain of actions (and set to false otherwise).
*
* @returns The number of hexes entered. This can safely be used as an index
* into steps to get the location where movement ended, provided
* steps is not empty (the return value is guaranteed to be less
* than steps.size() ).
*/
size_t mouse_handler::move_unit_along_route(const std::vector<map_location> & steps,
bool & interrupted)
{
if(steps.empty()) {
interrupted = false;
return 0;
}
// Default return value.
interrupted = true;
//If this is a leader on a keep, ask permission to the whiteboard to move it
//since otherwise it may cause planned recruits to be erased.
if ( pc_.get_map_const().is_keep(steps.front()) )
{
unit_map::const_iterator const u = pc_.gamestate().board_.units().find(steps.front());
if ( u && u->can_recruit() &&
u->side() == gui().viewing_side() &&
!pc_.get_whiteboard()->allow_leader_to_move(*u) )
{
gui2::show_transient_message(gui_->video(), "",
_("You cannot move your leader away from the keep with some planned recruits or recalls left."));
return 0;
}
}
LOG_NG << "move unit along route from " << steps.front() << " to " << steps.back() << "\n";
size_t moves = actions::move_unit_and_record(steps, &pc_.get_undo_stack(), false, true, &interrupted);
cursor::set(cursor::NORMAL);
gui().invalidate_game_status();
if(moves == 0)
return 0;
if ( interrupted && moves + 1 < steps.size() ) {
// reselect the unit (for "press t to continue")
select_hex(steps[moves], false, false, false);
// the new discovery is more important than the new movement range
show_partial_move_ = true;
}
return moves;
}
void mouse_handler::save_whiteboard_attack(const map_location& attacker_loc, const map_location& defender_loc, int weapon_choice)
{
{
// @todo Fix flickering/reach highlight anomaly after the weapon choice dialog is closed
// This method should do the cleanup of highlights and selection but it doesn't work properly
// gui().highlight_hex(map_location());
gui().draw();
gui().unhighlight_reach();
gui().clear_attack_indicator();
// remove footsteps if any - useless for whiteboard as of now
gui().set_route(NULL);
// do not keep the hex that we started from highlighted
selected_hex_ = map_location();
gui().select_hex(map_location());
show_partial_move_ = false;
// invalid after saving the move
current_paths_ = pathfind::paths();
current_route_.steps.clear();
}
//create planned attack for whiteboard
pc_.get_whiteboard()->save_temp_attack(attacker_loc, defender_loc, weapon_choice);
}
int mouse_handler::fill_weapon_choices(std::vector<battle_context>& bc_vector, unit_map::iterator attacker, unit_map::iterator defender)
{
int best = 0;
for (unsigned int i = 0; i < attacker->attacks().size(); i++) {
// skip weapons with attack_weight=0
if (attacker->attacks()[i].attack_weight() > 0) {
battle_context bc(pc_.gamestate().board_.units_, attacker->get_location(), defender->get_location(), i);
bc_vector.push_back(bc);
if (bc.better_attack(bc_vector[best], 0.5)) {
// as some weapons can be hidden, i is not a valid index into the resulting vector
best = bc_vector.size() - 1;
}
}
}
return best;
}
int mouse_handler::show_attack_dialog(const map_location& attacker_loc, const map_location& defender_loc)
{
game_board & board = pc_.gamestate().board_;
unit_map::iterator attacker = board.units_.find(attacker_loc);
unit_map::iterator defender = board.units_.find(defender_loc);
if(!attacker || !defender) {
ERR_NG << "One fighter is missing, can't attack";
return -1; // abort, click will do nothing
}
std::vector<battle_context> bc_vector;
const int best = fill_weapon_choices(bc_vector, attacker, defender);
if(gui2::new_widgets) {
gui2::tunit_attack dlg(
attacker
, defender
, bc_vector
, best);
dlg.show(gui_->video());
if(dlg.get_retval() == gui2::twindow::OK) {
return dlg.get_selected_weapon();
} else {
return -1;
}
}
std::vector<int> disable_items_skip;
std::vector<std::string> items;
{
const config tmp_config;
const attack_type no_weapon(tmp_config);
for (unsigned int i = 0; i < bc_vector.size(); i++) {
const battle_context_unit_stats& att = bc_vector[i].get_attacker_stats();
const battle_context_unit_stats& def = bc_vector[i].get_defender_stats();
const attack_type& attw = *att.weapon;
const attack_type& defw = def.weapon ? *def.weapon : no_weapon;
attw.set_specials_context(attacker_loc, defender_loc, true, def.weapon);