From d302ea8f3467ba8f00f42d1dc5125ae1acc125e6 Mon Sep 17 00:00:00 2001 From: Patrick Parker Date: Sun, 17 Aug 2008 03:34:52 +0000 Subject: [PATCH] cleaner(?) implementation of unit_animation::all_tag_names --- src/game.cpp | 31 ++- src/unit.cpp | 428 ++++++++++++++++++++--------------------- src/unit_animation.cpp | 49 +++-- src/unit_animation.hpp | 4 +- 4 files changed, 258 insertions(+), 254 deletions(-) diff --git a/src/game.cpp b/src/game.cpp index 5162e8c801a..bb049aaa5c6 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -216,10 +216,10 @@ private: }; game_controller::game_controller(int argc, char** argv) : - argc_(argc), - arg_(1), + argc_(argc), + arg_(1), argv_(argv), - thread_manager(), + thread_manager(), video_(), font_manager_(), prefs_manager_(), @@ -230,26 +230,26 @@ game_controller::game_controller(int argc, char** argv) : music_thinker_(), resize_monitor_(), paths_manager_(), - test_scenario_("test"), - test_mode_(false), - multiplayer_mode_(false), - no_gui_(false), - use_caching_(true), - force_valid_cache_(false), - force_bpp_(-1), + test_scenario_("test"), + test_mode_(false), + multiplayer_mode_(false), + no_gui_(false), + use_caching_(true), + force_valid_cache_(false), + force_bpp_(-1), game_config_(), - disp_(NULL), + disp_(NULL), state_(), resolution(), bpp(0), video_flags(0), loaded_game_(), - loaded_game_show_replay_(false), - loaded_game_cancel_orders_(false), + loaded_game_show_replay_(false), + loaded_game_cancel_orders_(false), defines_map_(), old_defines_map_(), multiplayer_server_(), - jump_to_campaign_(false), + jump_to_campaign_(false), jump_to_multiplayer_(false) #ifdef USE_EDITOR2 ,jump_to_editor_(false) @@ -2303,9 +2303,6 @@ int main(int argc, char** argv) } try { - //trigger any one-time static initializations - unit_animation::init_tag_names(); - /** * @todo We try to guess the name of the server from the name of the * binary started. This is very fragile it breaks in at least the diff --git a/src/unit.cpp b/src/unit.cpp index a4455ec1004..84d6706fa78 100644 --- a/src/unit.cpp +++ b/src/unit.cpp @@ -174,8 +174,8 @@ unit::unit(const unit& o): { } -unit::unit(unit_map* unitmap, const gamemap* map, const gamestatus* game_status, - const std::vector* teams,const config& cfg, +unit::unit(unit_map* unitmap, const gamemap* map, const gamestatus* game_status, + const std::vector* teams,const config& cfg, bool use_traits, game_state* state) : cfg_(), advances_to_(), @@ -254,7 +254,7 @@ unit::unit(unit_map* unitmap, const gamemap* map, const gamestatus* game_status, game_config::add_color_info(cfg); } -unit::unit(const config& cfg,bool use_traits) : +unit::unit(const config& cfg,bool use_traits) : cfg_(), advances_to_(), type_(), @@ -284,211 +284,6 @@ unit::unit(const config& cfg,bool use_traits) : max_movement_(0), movement_costs_(), defense_mods_(), - hold_position_(false), - end_turn_(false), - resting_(false), - attacks_left_(0), - max_attacks_(0), - states_(), - variables_(), - emit_zoc_(0), - state_(STATE_STANDING), - overlays_(), - role_(), - ai_special_(), - attacks_(), - facing_(gamemap::location::SOUTH_EAST), - traits_description_(), - unit_value_(), - goto_(), - interrupted_move_(), - flying_(false), - is_fearless_(false), - is_healthy_(false), - modification_descriptions_(), - animations_(), - anim_(NULL), - next_idling_(0), - frame_begin_time_(0), - unit_halo_(halo::NO_HALO), - unit_anim_halo_(halo::NO_HALO), - getsHit_(0), - refreshing_(false), - hidden_(false), - draw_bars_(false), - modifications_(), - units_(NULL), - map_(NULL), - gamestatus_(NULL), - teams_(NULL), - invisibility_cache_() -{ - read(cfg,use_traits); - /** @todo Are these modified by read? if not they can be removed. */ - getsHit_=0; - end_turn_ = false; - refreshing_ = false; - hidden_ = false; -} - -void unit::clear_status_caches() -{ - for(std::vector::const_iterator itor = units_with_cache.begin(); - itor != units_with_cache.end(); ++itor) { - (*itor)->clear_visibility_cache(); - } - - units_with_cache.clear(); -} - -unit_race::GENDER unit::generate_gender(const unit_type& type, bool gen, game_state* state) -{ - const std::vector& genders = type.genders(); - // Once random gender is used, don't do it again. - // Such as when restoring a saved character. - cfg_["random_gender"] = "no"; - if(genders.empty() == false) { - if(state) { - return gen ? genders[state->rng().get_random() % genders.size()] : genders.front(); - } else { - return gen ? genders[get_random() % genders.size()] : genders.front(); - } - } else { - return unit_race::MALE; - } -} - -unit::unit(unit_map* unitmap, const gamemap* map, const gamestatus* game_status, - const std::vector* teams, const unit_type* t, int side, - bool use_traits, bool dummy_unit, unit_race::GENDER gender, std::string variation) : - cfg_(), - advances_to_(), - type_(), - race_(NULL), - id_(), - name_(), - underlying_id_(), - type_name_(), - undead_variation_(), - variation_(variation), - hit_points_(0), - max_hit_points_(0), - experience_(0), - max_experience_(0), - level_(0), - alignment_(), - flag_rgb_(), - image_mods_(), - unrenamable_(false), - side_(side), - gender_(dummy_unit ? gender : generate_gender(*t,use_traits)), - alpha_(), - unit_formula_(), - formula_vars_(), - recruits_(), - movement_(0), - max_movement_(0), - movement_costs_(), - defense_mods_(), - hold_position_(false), - end_turn_(false), - resting_(false), - attacks_left_(0), - max_attacks_(0), - states_(), - variables_(), - emit_zoc_(0), - state_(STATE_STANDING), - overlays_(), - role_(), - ai_special_(), - attacks_(), - facing_(gamemap::location::SOUTH_EAST), - traits_description_(), - unit_value_(), - goto_(), - interrupted_move_(), - flying_(false), - is_fearless_(false), - is_healthy_(false), - modification_descriptions_(), - animations_(), - anim_(NULL), - next_idling_(0), - frame_begin_time_(0), - unit_halo_(halo::NO_HALO), - unit_anim_halo_(halo::NO_HALO), - getsHit_(0), - refreshing_(false), - hidden_(false), - draw_bars_(false), - modifications_(), - units_(unitmap), - map_(map), - gamestatus_(game_status), - teams_(teams), - invisibility_cache_() -{ - cfg_["upkeep"]="full"; - advance_to(t); - if(dummy_unit == false) validate_side(side_); - if(use_traits) { - // Units that don't have traits generated are just - // generic units, so they shouldn't get a description - // either. - name_ = generate_name(); - } - generate_traits(!use_traits); - reset_modifications(); - apply_modifications(); - set_underlying_id(); - - /** - * @todo Test whether the calls above modify these values if not they can - * removed, since already set in the initialization list. - */ - unrenamable_ = false; - anim_ = NULL; - getsHit_=0; - end_turn_ = false; - hold_position_ = false; - next_idling_ = 0; - frame_begin_time_ = 0; - unit_halo_ = halo::NO_HALO; - unit_anim_halo_ = halo::NO_HALO; -} - -unit::unit(const unit_type* t, int side, bool use_traits, bool dummy_unit, - unit_race::GENDER gender, std::string variation) : - cfg_(), - advances_to_(), - type_(), - race_(NULL), - id_(), - name_(), - underlying_id_(), - type_name_(), - undead_variation_(), - variation_(variation), - hit_points_(0), - max_hit_points_(0), - experience_(0), - max_experience_(0), - level_(0), - alignment_(), - flag_rgb_(), - image_mods_(), - unrenamable_(false), - side_(side), - gender_(dummy_unit ? gender : generate_gender(*t,use_traits)), - alpha_(), - unit_formula_(), - formula_vars_(), - recruits_(), - movement_(0), - max_movement_(0), - movement_costs_(), - defense_mods_(), hold_position_(false), end_turn_(false), resting_(false), @@ -520,7 +315,212 @@ unit::unit(const unit_type* t, int side, bool use_traits, bool dummy_unit, getsHit_(0), refreshing_(false), hidden_(false), - draw_bars_(false), + draw_bars_(false), + modifications_(), + units_(NULL), + map_(NULL), + gamestatus_(NULL), + teams_(NULL), + invisibility_cache_() +{ + read(cfg,use_traits); + /** @todo Are these modified by read? if not they can be removed. */ + getsHit_=0; + end_turn_ = false; + refreshing_ = false; + hidden_ = false; +} + +void unit::clear_status_caches() +{ + for(std::vector::const_iterator itor = units_with_cache.begin(); + itor != units_with_cache.end(); ++itor) { + (*itor)->clear_visibility_cache(); + } + + units_with_cache.clear(); +} + +unit_race::GENDER unit::generate_gender(const unit_type& type, bool gen, game_state* state) +{ + const std::vector& genders = type.genders(); + // Once random gender is used, don't do it again. + // Such as when restoring a saved character. + cfg_["random_gender"] = "no"; + if(genders.empty() == false) { + if(state) { + return gen ? genders[state->rng().get_random() % genders.size()] : genders.front(); + } else { + return gen ? genders[get_random() % genders.size()] : genders.front(); + } + } else { + return unit_race::MALE; + } +} + +unit::unit(unit_map* unitmap, const gamemap* map, const gamestatus* game_status, + const std::vector* teams, const unit_type* t, int side, + bool use_traits, bool dummy_unit, unit_race::GENDER gender, std::string variation) : + cfg_(), + advances_to_(), + type_(), + race_(NULL), + id_(), + name_(), + underlying_id_(), + type_name_(), + undead_variation_(), + variation_(variation), + hit_points_(0), + max_hit_points_(0), + experience_(0), + max_experience_(0), + level_(0), + alignment_(), + flag_rgb_(), + image_mods_(), + unrenamable_(false), + side_(side), + gender_(dummy_unit ? gender : generate_gender(*t,use_traits)), + alpha_(), + unit_formula_(), + formula_vars_(), + recruits_(), + movement_(0), + max_movement_(0), + movement_costs_(), + defense_mods_(), + hold_position_(false), + end_turn_(false), + resting_(false), + attacks_left_(0), + max_attacks_(0), + states_(), + variables_(), + emit_zoc_(0), + state_(STATE_STANDING), + overlays_(), + role_(), + ai_special_(), + attacks_(), + facing_(gamemap::location::SOUTH_EAST), + traits_description_(), + unit_value_(), + goto_(), + interrupted_move_(), + flying_(false), + is_fearless_(false), + is_healthy_(false), + modification_descriptions_(), + animations_(), + anim_(NULL), + next_idling_(0), + frame_begin_time_(0), + unit_halo_(halo::NO_HALO), + unit_anim_halo_(halo::NO_HALO), + getsHit_(0), + refreshing_(false), + hidden_(false), + draw_bars_(false), + modifications_(), + units_(unitmap), + map_(map), + gamestatus_(game_status), + teams_(teams), + invisibility_cache_() +{ + cfg_["upkeep"]="full"; + advance_to(t); + if(dummy_unit == false) validate_side(side_); + if(use_traits) { + // Units that don't have traits generated are just + // generic units, so they shouldn't get a description + // either. + name_ = generate_name(); + } + generate_traits(!use_traits); + reset_modifications(); + apply_modifications(); + set_underlying_id(); + + /** + * @todo Test whether the calls above modify these values if not they can + * removed, since already set in the initialization list. + */ + unrenamable_ = false; + anim_ = NULL; + getsHit_=0; + end_turn_ = false; + hold_position_ = false; + next_idling_ = 0; + frame_begin_time_ = 0; + unit_halo_ = halo::NO_HALO; + unit_anim_halo_ = halo::NO_HALO; +} + +unit::unit(const unit_type* t, int side, bool use_traits, bool dummy_unit, + unit_race::GENDER gender, std::string variation) : + cfg_(), + advances_to_(), + type_(), + race_(NULL), + id_(), + name_(), + underlying_id_(), + type_name_(), + undead_variation_(), + variation_(variation), + hit_points_(0), + max_hit_points_(0), + experience_(0), + max_experience_(0), + level_(0), + alignment_(), + flag_rgb_(), + image_mods_(), + unrenamable_(false), + side_(side), + gender_(dummy_unit ? gender : generate_gender(*t,use_traits)), + alpha_(), + unit_formula_(), + formula_vars_(), + recruits_(), + movement_(0), + max_movement_(0), + movement_costs_(), + defense_mods_(), + hold_position_(false), + end_turn_(false), + resting_(false), + attacks_left_(0), + max_attacks_(0), + states_(), + variables_(), + emit_zoc_(0), + state_(STATE_STANDING), + overlays_(), + role_(), + ai_special_(), + attacks_(), + facing_(gamemap::location::SOUTH_EAST), + traits_description_(), + unit_value_(), + goto_(), + interrupted_move_(), + flying_(false), + is_fearless_(false), + is_healthy_(false), + modification_descriptions_(), + animations_(), + anim_(NULL), + next_idling_(0), + frame_begin_time_(0), + unit_halo_(halo::NO_HALO), + unit_anim_halo_(halo::NO_HALO), + getsHit_(0), + refreshing_(false), + hidden_(false), + draw_bars_(false), modifications_(), units_(NULL), map_(NULL), @@ -541,8 +541,8 @@ unit::unit(const unit_type* t, int side, bool use_traits, bool dummy_unit, reset_modifications(); apply_modifications(); set_underlying_id(); - - /** + + /** * @todo Test whether the calls above modify these values if not they can * removed, since already set in the initialization list. */ @@ -713,7 +713,7 @@ void unit::advance_to(const unit_type* t, bool use_traits, game_state* state) reset_modifications(); // Remove old animations - foreach(const std::string& tag_name, unit_animation::all_tag_names) { + foreach(const std::string& tag_name, unit_animation::all_tag_names()) { cfg_.clear_children(tag_name); } @@ -1594,7 +1594,7 @@ void unit::read(const config& cfg, bool use_traits, game_state* state) // Attach animations for this unit to the in-core object unit_animation::fill_initial_animations(animations_,cfg_); // Remove animations from private cfg, they're not needed there now - foreach(const std::string& tag_name, unit_animation::all_tag_names) { + foreach(const std::string& tag_name, unit_animation::all_tag_names()) { cfg_.clear_children(tag_name); } @@ -3337,7 +3337,7 @@ std::string get_checksum(const unit& u) { unit_config["overlays"] = ""; // Non-critical tags to ignore. unit_config.clear_children("comment"); - foreach(const std::string& tag_name, unit_animation::all_tag_names) { + foreach(const std::string& tag_name, unit_animation::all_tag_names()) { unit_config.clear_children(tag_name); } diff --git a/src/unit_animation.cpp b/src/unit_animation.cpp index f9db9f7bde4..da1e99d8adb 100644 --- a/src/unit_animation.cpp +++ b/src/unit_animation.cpp @@ -34,27 +34,34 @@ #include #include -//static initialization -std::vector unit_animation::all_tag_names; -void unit_animation::init_tag_names() { - unit_animation::all_tag_names.clear(); - unit_animation::all_tag_names.push_back("animation"); - unit_animation::all_tag_names.push_back("attack_anim"); - unit_animation::all_tag_names.push_back("death"); - unit_animation::all_tag_names.push_back("defend"); - unit_animation::all_tag_names.push_back("extra_anim"); - unit_animation::all_tag_names.push_back("healed_anim"); - unit_animation::all_tag_names.push_back("healing_anim"); - unit_animation::all_tag_names.push_back("idle_anim"); - unit_animation::all_tag_names.push_back("leading_anim"); - unit_animation::all_tag_names.push_back("levelin_anim"); - unit_animation::all_tag_names.push_back("levelout_anim"); - unit_animation::all_tag_names.push_back("movement_anim"); - unit_animation::all_tag_names.push_back("poison_anim"); - unit_animation::all_tag_names.push_back("recruit_anim"); - unit_animation::all_tag_names.push_back("standing_anim"); - unit_animation::all_tag_names.push_back("teleport_anim"); - unit_animation::all_tag_names.push_back("victory_anim"); +struct tag_name_manager { + tag_name_manager() : names() { + names.push_back("animation"); + names.push_back("attack_anim"); + names.push_back("death"); + names.push_back("defend"); + names.push_back("extra_anim"); + names.push_back("healed_anim"); + names.push_back("healing_anim"); + names.push_back("idle_anim"); + names.push_back("leading_anim"); + names.push_back("levelin_anim"); + names.push_back("levelout_anim"); + names.push_back("movement_anim"); + names.push_back("poison_anim"); + names.push_back("recruit_anim"); + names.push_back("standing_anim"); + names.push_back("teleport_anim"); + names.push_back("victory_anim"); + } + std::vector names; +}; +namespace { + tag_name_manager anim_tags; +} //end anonymous namespace + +const std::vector& unit_animation::all_tag_names() { + return anim_tags.names; } config unit_animation::prepare_animation(const config &cfg,const std::string animation_tag) diff --git a/src/unit_animation.hpp b/src/unit_animation.hpp index 0cc14f3589f..c51b42c2d9b 100644 --- a/src/unit_animation.hpp +++ b/src/unit_animation.hpp @@ -33,8 +33,8 @@ class unit_animation public: typedef enum { MATCH_FAIL=-2 , DEFAULT_ANIM=-1} variation_type; typedef enum { HIT, MISS, KILL, INVALID} hit_type; - static std::vector all_tag_names; - static void init_tag_names(); + + static const std::vector& all_tag_names(); static void fill_initial_animations( std::vector & animations, const config & cfg); static void add_anims( std::vector & animations, const config & cfg);