The Battle for Wesnoth  1.17.4+dev
animation_component.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2022
3  by Chris Beck <render787@gmail.com>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
17 
18 #include "config.hpp"
19 #include "display.hpp"
20 #include "map/map.hpp"
21 #include "preferences/display.hpp"
22 #include "preferences/general.hpp"
23 #include "random.hpp"
24 #include "units/unit.hpp"
25 #include "units/types.hpp"
26 
27 #include <set>
28 
29 const unit_animation* unit_animation_component::choose_animation(const map_location& loc,const std::string& event,
30  const map_location& second_loc,const int value,const strike_result::type hit,
31  const_attack_ptr attack, const_attack_ptr second_attack, int swing_num)
32 {
33  // Select one of the matching animations at random
34  std::vector<const unit_animation*> options;
35  int max_val = unit_animation::MATCH_FAIL;
36  for(const unit_animation& anim : animations_) {
37  int matching = anim.matches(loc,second_loc,u_.shared_from_this(),event,value,hit,attack,second_attack,swing_num);
38  if(matching > unit_animation::MATCH_FAIL && matching == max_val) {
39  options.push_back(&anim);
40  } else if(matching > max_val) {
41  max_val = matching;
42  options.clear();
43  options.push_back(&anim);
44  }
45  }
46 
47  if(max_val == unit_animation::MATCH_FAIL) {
48  return nullptr;
49  }
50  return options[randomness::rng::default_instance().get_random_int(0, options.size()-1)];
51 }
52 
54 {
56  start_animation(INT_MAX, choose_animation(u_.loc_, "standing"),
57  with_bars, "", {0,0,0}, STATE_STANDING);
58  } else {
59  start_animation(INT_MAX, choose_animation(u_.loc_, "_disabled_"),
60  with_bars, "", {0,0,0}, STATE_STANDING);
61  }
62 }
63 
65 {
66  start_animation(INT_MAX, choose_animation(u_.loc_, "_ghosted_"),
67  with_bars);
68  anim_->pause_animation();
69 }
70 
72 {
73  start_animation(INT_MAX, choose_animation(u_.loc_, "_disabled_ghosted_"),
74  with_bars);
75 }
76 
78 {
79  start_animation(INT_MAX, choose_animation(u_.loc_, "idling"),
80  true, "", {0,0,0}, STATE_FORGET);
81 }
82 
84 {
86  start_animation(INT_MAX, choose_animation(u_.loc_, "selected"),
87  true, "", {0,0,0}, STATE_FORGET);
88  } else {
89  start_animation(INT_MAX, choose_animation(u_.loc_, "_disabled_selected_"),
90  true, "", {0,0,0}, STATE_FORGET);
91  }
92 }
93 
94 void unit_animation_component::start_animation (int start_time, const unit_animation *animation,
95  bool with_bars, const std::string &text, color_t text_color, STATE state)
96 {
97  if (!animation) {
98  if (state == STATE_STANDING)
99  state_ = state;
100  if (!anim_ && state_ != STATE_STANDING)
101  set_standing(with_bars);
102  return ;
103  }
104  state_ = state;
105  // everything except standing select and idle
106  bool accelerate = (state != STATE_FORGET && state != STATE_STANDING);
107  draw_bars_ = with_bars;
108  anim_.reset(new unit_animation(*animation));
109  const int real_start_time = start_time == INT_MAX ? anim_->get_begin_time() : start_time;
110  anim_->start_animation(real_start_time, u_.loc_, u_.loc_.get_direction(u_.facing_),
111  text, text_color, accelerate);
112  frame_begin_time_ = anim_->get_begin_time() -1;
113  if (preferences::idle_anim()) {
115  + static_cast<int>(randomness::rng::default_instance().get_random_int(20000, 39999) * preferences::idle_anim_rate());
116  } else {
117  next_idling_ = INT_MAX;
118  }
119 }
120 
122 {
123  if (state_ == STATE_FORGET && anim_ && anim_->animation_finished_potential())
124  {
125  set_standing();
126  return;
127  }
128  display &disp = *display::get_singleton();
131  {
132  return;
133  }
135  {
136  // prevent all units animating at the same time
137  if (preferences::idle_anim()) {
139  + static_cast<int>(randomness::rng::default_instance().get_random_int(20000, 39999) * preferences::idle_anim_rate());
140  } else {
141  next_idling_ = INT_MAX;
142  }
143  } else {
144  set_idling();
145  }
146 }
147 
149 {
150  unit_halo_.reset();
151  if(anim_ ) anim_->clear_haloes();
152 }
153 
155 {
156  bool result = false;
157 
158  // Very early calls, anim not initialized yet
159  if(get_animation()) {
160  frame_parameters params;
161  const gamemap & map = disp.get_map();
162  const t_translation::terrain_code terrain = map.get_terrain(u_.loc_);
163  const terrain_type& terrain_info = map.get_terrain_info(terrain);
164 
165  int height_adjust = static_cast<int>(terrain_info.unit_height_adjust() * disp.get_zoom_factor());
166  if (u_.is_flying() && height_adjust < 0) {
167  height_adjust = 0;
168  }
169  params.y -= height_adjust;
170  params.halo_y -= height_adjust;
171  params.image_mod = u_.image_mods();
172  params.halo_mod = u_.TC_image_mods();
173  params.image= u_.default_anim_image();
174 
175  result |= get_animation()->invalidate(params);
176  }
177 
178  return result;
179 
180 }
181 
183 {
184  if (newtype) {
185  animations_ = newtype->animations();
186  }
187 
188  refreshing_ = false;
189  anim_.reset();
190 }
191 
193  if(effect["id"].empty()) {
195  } else {
196  static std::map< std::string, std::vector<unit_animation>> animation_cache;
197  std::vector<unit_animation> &built = animation_cache[effect["id"]];
198  if(built.empty()) {
199  unit_animation::add_anims(built, effect);
200  }
201  animations_.insert(animations_.end(),built.begin(),built.end());
202  }
203 }
204 
205 std::vector<std::string> unit_animation_component::get_flags() {
206  std::set<std::string> result;
207  for(const auto& anim : animations_) {
208  const std::vector<std::string>& flags = anim.get_flags();
209  std::copy_if(flags.begin(), flags.end(), std::inserter(result, result.begin()), [](const std::string flag) {
210  return !(flag.empty() || (flag.front() == '_' && flag.back() == '_'));
211  });
212  }
213  return std::vector<std::string>(result.begin(), result.end());
214 }
void start_animation(int start_time, const unit_animation *animation, bool with_bars, const std::string &text="", color_t text_color={}, STATE state=STATE_ANIM)
Begin an animation.
bool refreshing_
avoid infinite recursion.
All parameters from a frame at a given instant.
Definition: frame.hpp:35
image::locator image
Definition: frame.hpp:41
static display * get_singleton()
Returns the display object if a display object exists.
Definition: display.hpp:96
const unit & u_
A reference to the unit that owns this object.
void set_disabled_ghosted(bool with_bars=true)
Whiteboard related somehow.
bool is_flying() const
Check if the unit is a flying unit.
Definition: unit.hpp:1454
static double get_zoom_factor()
Returns the current zoom factor.
Definition: display.hpp:264
const unit_animation * choose_animation(const map_location &loc, const std::string &event, const map_location &second_loc=map_location::null_location(), const int damage=0, const strike_result::type hit_type=strike_result::type::invalid, const_attack_ptr attack=nullptr, const_attack_ptr second_attack=nullptr, int swing_num=0)
Chooses an appropriate animation from the list of known animations.
std::unique_ptr< unit_animation > anim_
The current animation.
int get_current_animation_tick()
Definition: animated.cpp:36
static void add_anims(std::vector< unit_animation > &animations, const config &cfg)
Definition: animation.cpp:629
map_location loc_
Definition: unit.hpp:1838
void set_selecting()
Sets the animation state to that when the unit is selected.
bool idle_anim()
Definition: general.cpp:512
A terrain string which is converted to a terrain is a string with 1 or 2 layers the layers are separa...
Definition: translation.hpp:49
const terrain_type & get_terrain_info(const t_translation::terrain_code &terrain) const
Definition: map.cpp:98
bool draw_bars_
bool indicating whether to draw bars with the unit
Definitions for the interface to Wesnoth Markup Language (WML).
void refresh()
Intermittently activates the idling animations in place of the standing animations.
unit_animation * get_animation() const
Get a pointer to the current animation.
int frame_begin_time_
time for the frame to begin
A single unit type that the player may recruit.
Definition: types.hpp:45
map_location get_direction(DIRECTION dir, unsigned int n=1u) const
Definition: location.cpp:360
const config & options()
Definition: game.cpp:555
void clear_haloes()
Clear the haloes associated to the unit.
int unit_height_adjust() const
Definition: terrain.hpp:137
std::string halo_mod
Definition: frame.hpp:50
const std::vector< unit_animation > & animations() const
Definition: types.cpp:535
terrain_code get_terrain(const map_location &loc) const
Looks up terrain at a particular location.
Definition: map.cpp:302
Encapsulates the map of the game.
Definition: map.hpp:171
STATE state_
animation state
bool invalidate(const display &disp)
Invalidates an animation with respect to a display object, preparing it for redraw.
map_display and display: classes which take care of displaying the map and game-data on the screen...
std::string default_anim_image() const
The default image to use for animation frames with no defined image.
Definition: unit.cpp:2421
bool show_standing_animations()
Definition: display.cpp:51
STATE
States for animation.
Encapsulates the map of the game.
Definition: location.hpp:38
void set_idling()
Sets the animation state to idling.
std::vector< std::string > get_flags()
Get the flags of all registered animations.
double idle_anim_rate()
Definition: general.cpp:522
std::string image_mod
Definition: frame.hpp:44
bool tile_nearly_on_screen(const map_location &loc) const
Checks if location loc or one of the adjacent tiles is visible on screen.
Definition: display.cpp:2054
int get_random_int(int min, int max)
This helper method provides a random int from the underlying generator, using results of next_random...
Definition: random.hpp:52
void apply_new_animation_effect(const config &effect)
Adds an animation described by a config.
std::vector< unit_animation > animations_
List of registered animations for this unit.
const gamemap & get_map() const
Definition: display.hpp:100
bool incapacitated() const
Check if the unit has been petrified.
Definition: unit.hpp:893
halo::handle unit_halo_
handle to the halo of this unit
void set_standing(bool with_bars=true)
Sets the animation state to standing.
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:60
std::shared_ptr< const attack_type > const_attack_ptr
Definition: ptr.hpp:34
std::string TC_image_mods() const
Constructs a recolor (RC) IPF string for this unit&#39;s team color.
Definition: unit.cpp:2590
static rng & default_instance()
Definition: random.cpp:74
void set_ghosted(bool with_bars=true)
Sets the animation state to ghosted.
std::string image_mods() const
Gets an IPF string containing all IPF image mods.
Definition: unit.cpp:2595
void reset_after_advance(const unit_type *newtype=nullptr)
Resets the animations list after the unit is advanced.
int next_idling_
time for next idle animation
bool invalidate(frame_parameters &value)
Definition: animation.cpp:1107
map_location::DIRECTION facing_
Definition: unit.hpp:1916