54 #define ERR_NG LOG_STREAM(err, log_engine) 55 #define LOG_NG LOG_STREAM(info, log_engine) 58 #define ERR_WML LOG_STREAM(err, log_wml) 67 , previous_free_hex_()
72 , unselected_paths_(false)
73 , unselected_reach_(false)
77 , reachmap_invalid_(false)
78 , show_partial_move_(false)
79 , preventing_units_highlight_(false)
115 if(mini_loc.
valid()) {
134 const double drag_distance = std::pow(static_cast<double>(
drag_from_x_- mx), 2)
144 bool selected_hex_has_my_unit = found_unit.valid() && found_unit.get_shared_ptr()->side() ==
side_num_;
194 selected_hex_has_my_unit = found_unit.valid();
233 selected_unit = found_unit;
246 !selected_unit->incapacitated() && !browse)
248 if (attack_from.
valid()) {
251 else if (!mouseover_unit &&
264 && mouseover_unit->side() ==
side_num_ ) {
289 if (attack_from.
valid()) {
305 if (selected_unit && !selected_unit->incapacitated()) {
319 && mouseover_unit.
valid()
329 }
else if (!selected_unit) {
371 std::unique_ptr<unit_movement_resetter> move_reset;
373 move_reset = std::make_unique<unit_movement_resetter>(*un);
412 lk->mouse_over_hex_callback(new_hex);
445 bool selected_hex_has_unit;
499 if(selected_unit && selected_unit->side() ==
side_num_ && !selected_unit->incapacitated() && !browse) {
500 if(attack_from.
valid()) {
537 if(attack_from.
valid()) {
550 if(selected_unit && !selected_unit->incapacitated()) {
571 }
else if(!selected_unit) {
628 return it.
valid() ? &*it :
nullptr;
634 return it.
valid() ? &*it :
nullptr;
664 bool source_eligible = source_unit.
valid();
665 if(!source_eligible) {
671 if(!source_eligible) {
680 if(!source_eligible) {
686 source_eligible &= source_unit->attacks_left() != 0;
687 if(!source_eligible) {
698 bool target_eligible = target_unit.
valid();
699 if(!target_eligible) {
704 target_eligible &= viewer.
is_enemy(target_unit->side());
705 if(!target_eligible) {
710 assert(source_unit->side() != target_unit->side());
712 target_eligible &= !target_unit->incapacitated();
713 if(!target_eligible) {
721 int best_rating = 100;
726 for(std::size_t
n = 0;
n < adj.size(); ++
n) {
738 unsigned int difference = std::abs(static_cast<int>(preferred - n));
739 if(difference > NDIRECTIONS / 2) {
740 difference = NDIRECTIONS - difference;
743 unsigned int second_difference = std::abs(static_cast<int>(second_preferred - n));
744 if(second_difference > NDIRECTIONS / 2) {
745 second_difference = NDIRECTIONS - second_difference;
748 const int rating = difference * 2 + (second_difference > difference);
749 if(rating < best_rating || res.
valid() ==
false) {
750 best_rating = rating;
771 un->
get_location(), go_to, 10000.0, calc, board.
map().
w(), board.
map().
h(), &allowed_teleports);
803 if(clicked_u && (!selected_u || selected_u->side() !=
side_num_ ||
804 (clicked_u->side() ==
side_num_ && clicked_u->id() != selected_u->id()))
831 const unit* clicked_u =
nullptr;
1007 if (touched_hex.
valid() && unit.
valid() && !unit->get_hidden()) {
1050 unit->anim_comp().set_selecting();
1076 bool invisible = u->invisible(u->get_location());
1078 if(!
gui_->
fogged(u->get_location()) && !u->incapacitated() && !invisible) {
1127 bool interrupted =
false;
1128 if(steps.size() > 1) {
1131 interrupted = interrupted || num_moves + 1 < steps.size();
1139 return !interrupted;
1163 interrupted =
false;
1178 _(
"You cannot move your leader away from the keep with some planned recruits or recalls left."));
1183 LOG_NG <<
"move unit along route from " << steps.front() <<
" to " << steps.back();
1192 if(interrupted && moves + 1 < steps.size()) {
1194 select_hex(steps[moves],
false,
false,
false);
1228 pc_.
get_whiteboard()->save_temp_attack(attacker_loc, defender_loc, weapon_choice);
1235 for(
unsigned int i = 0;
i < attacker->attacks().size();
i++) {
1237 if(attacker->attacks()[
i].attack_weight() > 0) {
1241 if(bc.get_attacker_stats().disable) {
1245 if(!bc_vector.empty() && bc.better_attack(bc_vector[best], 0.5)) {
1247 best = bc_vector.size();
1250 bc_vector.emplace_back(std::move(bc));
1264 if(!attacker || !defender) {
1265 ERR_NG <<
"One fighter is missing, can't attack";
1269 std::vector<battle_context> bc_vector;
1272 if(bc_vector.empty()) {
1291 }
catch(
const std::bad_alloc&) {
1292 lg::log_to_chat() <<
"Memory exhausted a unit has either a lot hitpoints or a negative amount.\n";
1293 ERR_WML <<
"Memory exhausted a unit has either a lot hitpoints or a negative amount.";
1305 unit* attacker =
nullptr;
1306 const unit* defender =
nullptr;
1307 std::vector<battle_context> bc_vector;
1311 if(!attacker_it || attacker_it->side() !=
side_num_ || attacker_it->incapacitated()) {
1316 if(!defender_it ||
current_team().is_enemy(defender_it->side()) ==
false || defender_it->incapacitated()) {
1322 attacker = &*attacker_it;
1323 defender = &*defender_it;
1326 if(std::size_t(choice) >= bc_vector.size()) {
1370 std::set<map_location> res;
1377 if(i && uteam.
is_enemy(i->side())) {
1394 if(u->attacks_left() == 0) {
1411 if(!i || !i->is_visible_to_team(cur_team,
false)) {
1415 const unit& target = *
i;
1440 if(it->get_hidden()) {
1464 if(it == units.
begin()) {
1470 if(it == units.
end()) {
pathfind::marked_route get_route(const unit *un, map_location go_to, team &team) const
bool mouse_motion_default(int x, int y, bool update)
This handles minimap scrolling and click-drag.
int drag_from_x_
Drag start position x.
bool is_keep(const map_location &loc) const
void set_current_paths(const pathfind::paths &new_paths)
marked_route mark_route(const plain_route &rt, bool update_move_cost)
Add marks on a route rt assuming that the unit located at the first hex of rt travels along it...
virtual const std::vector< team > & teams() const override
int fill_weapon_choices(std::vector< battle_context > &bc_vector, unit_map::iterator attacker, unit_map::iterator defender)
void invalidate_game_status()
Function to invalidate the game status displayed on the sidebar.
std::unique_ptr< game_lua_kernel > lua_kernel_
void get_adjacent_tiles(const map_location &a, map_location *res)
Function which, given a location, will place all adjacent locations in res.
virtual const unit_map & units() const override
void set(CURSOR_TYPE type)
Use the default parameter to reset cursors.
DIRECTION get_relative_dir(const map_location &loc, map_location::RELATIVE_DIR_MODE mode) const
This class represents a single unit of a specific type.
const std::string & type_id() const
The id of this unit's type.
const unit_map & get_units() const
bool dragging_touch_
Finger drag init flag.
unit_map::const_iterator find_unit(const map_location &hex) const
const map_location hex_clicked_on(int x, int y) const
given x,y co-ordinates of an onscreen pixel, will return the location of the hex that this pixel corr...
map_location selected_hex_
bool dragging_started_
Actual drag flag.
Various functions that implement attacks and attack calculations.
pathfind::paths current_paths_
If non-empty, current_paths_.destinations contains a cache of highlighted hexes, likely the movement ...
const map_location hovered_hex() const
Uses SDL and game_display::hex_clicked_on to fetch the hex the mouse is hovering, if applicable...
can_move_result unit_can_move(const unit &u) const
Work out what u can do - this does not check which player's turn is currently active, the result is calculated assuming that the unit's owner is currently active.
game_events::wml_event_pump & pump()
bool preventing_units_highlight_
void show_reach_for_unit(const unit_ptr &un)
Highlight the hexes that a unit can move to.
map_location minimap_location_on(int x, int y)
given x,y co-ordinates of the mouse, will return the location of the hex in the minimap that the mous...
virtual const gamemap & map() const override
std::shared_ptr< bool > whiteboard_lock
std::size_t move_unit_and_record(const std::vector< map_location > &steps, undo_list *undo_stack, bool continued_move, bool show_move, bool *interrupted, move_unit_spectator *move_spectator)
Moves a unit across the board.
const gamemap & get_map() const
bool move_unit_along_current_route()
Moves a unit along the currently cached route.
bool unit_in_cycle(unit_map::const_iterator it)
void touch_motion(int x, int y, const bool browse, bool update=false, map_location loc=map_location::null_location())
rect map_area() const
Returns the area used for the map.
map_location previous_hex_
static std::string _(const char *str)
bool show(const unsigned auto_close_time=0)
Shows the window.
void mouse_motion(int x, int y, const bool browse, bool update=false, map_location loc=map_location::null_location())
Use update to force an update of the mouse state.
void select_hex(const map_location &hex, const bool browse, const bool highlight=true, const bool fire_event=true)
Definitions for the interface to Wesnoth Markup Language (WML).
std::shared_ptr< unit > unit_ptr
bool contains(int x, int y) const
Whether the given point lies within the rectangle.
static mouse_handler * singleton_
bool contains(const Container &container, const Value &value)
Returns true iff value is found in container.
void highlight_another_reach(const pathfind::paths &paths_list, const map_location &goal=map_location::null_location())
Add more paths to highlight.
void touch_action(const map_location hex, bool browse)
bool fire_event(const ui_event event, const std::vector< std::pair< widget *, ui_event >> &event_chain, widget *dispatcher, widget *w, F &&... params)
Helper function for fire_event.
This class stores all the data for a single 'side' (in game nomenclature).
bool minimap_scrolling_
minimap scrolling (scroll-drag) state flag
bool hex_hosts_unit(const map_location &hex) const
Unit exists on the hex, no matter if friend or foe.
void disable_units_highlight()
Use this to disable hovering an unit from highlighting its movement range.
int w() const
Effective map width.
Structure which holds a single route between one location and another.
This file contains the settings handling of the widget library.
int drag_threshold() const
Minimum dragging distance to fire the drag&drop.
std::size_t move_unit_along_route(const std::vector< map_location > &steps, bool &interrupted)
Moves a unit across the board for a player.
void show_transient_message(const std::string &title, const std::string &message, const std::string &image, const bool message_use_markup, const bool title_use_markup)
Shows a transient message to the user.
const time_of_day & get_time_of_day(int for_turn=0) const
Returns global time of day for the passed turn.
Contains typedefs for the whiteboard.
std::vector< team > & get_teams()
Computes the statistics of a battle between an attacker and a defender unit.
int get_selected_weapon() const
bool is_enemy(int n) const
void highlight_reach(const pathfind::paths &paths_list)
Sets the paths that are currently displayed as available for the unit to move along.
std::set< map_location > get_adj_enemies(const map_location &loc, int side) const
int attack_num
Index into unit->attacks() or -1 for none.
Structure describing the statistics of a unit involved in the battle.
Structure which holds a single route and marks for special events.
std::shared_ptr< wb::manager > get_whiteboard() const
bool fogged(const map_location &loc) const
Returns true if location (x,y) is covered in fog.
static config get_attack(const map_location &a, const map_location &b, int att_weapon, int def_weapon, const std::string &attacker_type_id, const std::string &defender_type_id, int attacker_lvl, int defender_lvl, const std::size_t turn, const time_of_day &t)
bool right_click_show_menu(int x, int y, const bool browse)
Called in the default right_click when the context menu is about to be shown, can be used for preproc...
Encapsulates the map of the game.
Various functions related to moving units.
void enable_units_highlight()
When unit highlighting is disabled, call this when the mouse no longer hovers any unit to enable high...
unit_iterator find(std::size_t id)
actions::undo_list & get_undo_stack()
static lg::log_domain log_engine("engine")
pointer get_shared_ptr() const
This is exactly the same as operator-> but it's slightly more readable, and can replace &*iter syntax...
map_location current_unit_attacks_from(const map_location &loc) const
void scroll_to_tile(const map_location &loc, SCROLL_TYPE scroll_type=ONSCREEN, bool check_fogged=true, bool force=true)
Scroll such that location loc is on-screen.
virtual void highlight_hex(map_location hex) override
Function to highlight a location.
void attack_enemy(const map_location &attacker_loc, const map_location &defender_loc, int choice)
unit_map::iterator selected_unit()
void save_whiteboard_attack(const map_location &attacker_loc, const map_location &defender_loc, int weapon_choice)
virtual void select_hex(map_location hex) override
Function to display a location as selected.
Define the game's event mechanism.
map_location previous_free_hex_
ONLY IF whiteboard is currently active, applies the planned unit map for the duration of the struct's...
bool on_board(const map_location &loc) const
Tell if a location is on the map.
DIRECTION
Valid directions which can be moved in our hexagonal world.
mouse_handler(game_display *gui, play_controller &pc)
void set_goto(const map_location &new_goto)
Sets this unit's long term destination.
void cycle_units(const bool browse, const bool reverse=false)
pump_result_t fire(const std::string &event, const entity_location &loc1=entity_location::null_entity, const entity_location &loc2=entity_location::null_entity, const config &data=config())
Function to fire an event.
Handling of system events.
static bool run_and_throw(const std::string &commandname, const config &data, bool use_undo=true, bool show=true, synced_command::error_handler_function error_handler=default_error_function)
map_location last_hex_
last highlighted hex
void show_attack_options(const unit_map::const_iterator &u)
Causes attackable hexes to be highlighted.
bool unhighlight_reach()
Reset highlighting of paths.
const tod_manager & get_tod_manager() const
This shows the dialog for attacking units.
const map_location & get_location() const
The current map location this unit is at.
uint32_t get_mouse_state(int *x, int *y)
A wrapper for SDL_GetMouseState that gives coordinates in draw space.
std::size_t viewing_team() const
The viewing team is the team currently viewing the game.
Various functions that implement the undoing (and redoing) of in-game commands.
bool contains(const map_location &) const
void play_UI_sound(const std::string &files)
void set_action_bonus_count(const int count)
Standard logging facilities (interface).
int current_side() const
Returns the number of the side whose turn it is.
pathfind::marked_route current_route_
const teleport_map get_teleport_locations(const unit &u, const team &viewing_team, bool see_all, bool ignore_units, bool check_vision)
Object which contains all the possible locations a unit can move to, with associated best routes to t...
static const map_location & null_location()
Container associating units to locations.
unit * find_unit_nonowning(const map_location &hex)
bool incapacitated() const
Check if the unit has been petrified.
int side() const
The side this unit belongs to.
const_iterator find(const map_location &) const
void set_side(int side_number)
Ensures that the real unit map is active for the duration of the struct's life.
void attack_enemy_(const map_location &attacker_loc, const map_location &defender_loc, int choice)
plain_route a_star_search(const map_location &src, const map_location &dst, double stop_at, const cost_calculator &calc, const std::size_t width, const std::size_t height, const teleport_map *teleports, bool border)
void set_route(const pathfind::marked_route *route)
Sets the route along which footsteps are drawn to show movement of a unit.
static lg::log_domain log_wml("wml")
game_display & gui()
Due to the way this class is constructed we can assume that the display* gui_ member actually points ...
static map_location::DIRECTION n
void clear_attack_indicator()
int h() const
Effective map height.
bool scroll(int xmov, int ymov, bool force=false)
Scrolls the display by xmov,ymov pixels.
void display_unit_hex(map_location hex)
Change the unit to be displayed in the sidebar.
unit_map::iterator find_visible_unit(const map_location &loc, const team ¤t_team, bool see_all=false)
bool simple_warp_
MMB click (on game map) state flag.
void set_attack_indicator(const map_location &src, const map_location &dst)
Set the attack direction indicator.
std::vector< map_location > & steps
void insert(const map_location &)
This object is used to temporary move a unit in the unit map, swapping out any unit that is already t...
int show_attack_dialog(const map_location &attacker_loc, const map_location &defender_loc)
int drag_from_y_
Drag start position y.
void move_action(bool browse)
Overridden in derived class.
std::stringstream & log_to_chat()
Use this to show WML errors in the ingame chat.
void select_or_action(bool browse)