From 51273aceb4967f84c0b759a8b65a8f053140776b Mon Sep 17 00:00:00 2001 From: "J. Tyne" Date: Fri, 15 Feb 2013 02:38:42 +0000 Subject: [PATCH] Eliminate some parameters that are always *resources::game_map. --- src/ai/default/ai.cpp | 9 ++++---- src/ai/formula/function_table.cpp | 16 +++++++------- src/ai/testing/ca.cpp | 10 ++++----- src/ai/testing/ca_testing_recruitment.cpp | 10 ++++----- src/help.cpp | 8 +++---- src/pathfind/pathfind.cpp | 2 +- src/unit.cpp | 12 ++++------- src/unit_types.cpp | 22 ++++++++++++------- src/unit_types.hpp | 26 +++++++++++------------ 9 files changed, 56 insertions(+), 59 deletions(-) diff --git a/src/ai/default/ai.cpp b/src/ai/default/ai.cpp index eebb27e8aee..e57731164a9 100644 --- a/src/ai/default/ai.cpp +++ b/src/ai/default/ai.cpp @@ -319,16 +319,15 @@ private: int ai_default_recruitment_stage::average_resistance_against(const unit_type& a, const unit_type& b) const { int weighting_sum = 0, defense = 0; - gamemap &map_ = *resources::game_map; const std::map& terrain = - map_.get_weighted_terrain_frequencies(); + resources::game_map->get_weighted_terrain_frequencies(); for (std::map::const_iterator j = terrain.begin(), j_end = terrain.end(); j != j_end; ++j) { // Use only reachable tiles when computing the average defense. - if (a.movement_type().movement_cost(map_, j->first) < unit_movement_type::UNREACHABLE) { - defense += a.movement_type().defense_modifier(map_, j->first) * j->second; + if (a.movement_type().movement_cost(j->first) < unit_movement_type::UNREACHABLE) { + defense += a.movement_type().defense_modifier(j->first) * j->second; weighting_sum += j->second; } } @@ -340,7 +339,7 @@ int ai_default_recruitment_stage::average_resistance_against(const unit_type& a, for (std::map::const_iterator jj = terrain.begin(), jj_end = terrain.end(); jj != jj_end; ++jj) { - defense += a.movement_type().defense_modifier(map_, jj->first) * jj->second; + defense += a.movement_type().defense_modifier(jj->first) * jj->second; weighting_sum += jj->second; } } diff --git a/src/ai/formula/function_table.cpp b/src/ai/formula/function_table.cpp index f6ff9704957..0eca893b63b 100644 --- a/src/ai/formula/function_table.cpp +++ b/src/ai/formula/function_table.cpp @@ -81,9 +81,9 @@ class unit_adapter { } } - int movement_cost(const gamemap& map, const t_translation::t_terrain & terrain) const { + int movement_cost(const t_translation::t_terrain & terrain) const { if(unit_type_ != NULL) { - return unit_type_->movement_type().movement_cost(map, terrain); + return unit_type_->movement_type().movement_cost(terrain); } else { return unit_->movement_cost(terrain); } @@ -232,7 +232,7 @@ private: // test if the current path to locs[i] is better than this one could possibly be. // we do this a couple more times below if (next_visited && !(n < next) ) continue; - const int move_cost = u.movement_cost(map, map[locs[i]]); + const int move_cost = u.movement_cost(map[locs[i]]); node t = node(n.movement_cost_ + move_cost, locs[i]); @@ -1536,14 +1536,14 @@ private: { const unit_type& un = u_type->get_unit_type(); - if( un.movement() < un.movement_type().movement_cost(*resources::game_map, (*resources::game_map)[loc]) ) - return variant(); + if( un.movement() < un.movement_type().movement_cost((*resources::game_map)[loc]) ) + return variant(); if(!resources::game_map->on_board(loc)) { return variant(); } - return variant(100 - un.movement_type().defense_modifier(*resources::game_map, (*resources::game_map)[loc])); + return variant(100 - un.movement_type().defense_modifier((*resources::game_map)[loc])); } return variant(); @@ -1587,7 +1587,7 @@ private: return variant(); } - return variant(un.movement_type().defense_modifier(*resources::game_map, (*resources::game_map)[loc])); + return variant(un.movement_type().defense_modifier((*resources::game_map)[loc])); } return variant(); @@ -1631,7 +1631,7 @@ private: return variant(); } - return variant(un.movement_type().movement_cost(*resources::game_map, (*resources::game_map)[loc])); + return variant(un.movement_type().movement_cost((*resources::game_map)[loc])); } return variant(); diff --git a/src/ai/testing/ca.cpp b/src/ai/testing/ca.cpp index 17acde26c07..fbed02fa2dc 100644 --- a/src/ai/testing/ca.cpp +++ b/src/ai/testing/ca.cpp @@ -363,18 +363,16 @@ bool recruitment_phase::recruit_usage(const std::string& usage) int recruitment_phase::average_resistance_against(const unit_type& a, const unit_type& b) const { - gamemap &map_ = *resources::game_map; - int weighting_sum = 0, defense = 0; const std::map& terrain = - map_.get_weighted_terrain_frequencies(); + resources::game_map->get_weighted_terrain_frequencies(); for (std::map::const_iterator j = terrain.begin(), j_end = terrain.end(); j != j_end; ++j) { // Use only reachable tiles when computing the average defense. - if (a.movement_type().movement_cost(map_, j->first) < unit_movement_type::UNREACHABLE) { - defense += a.movement_type().defense_modifier(map_, j->first) * j->second; + if (a.movement_type().movement_cost(j->first) < unit_movement_type::UNREACHABLE) { + defense += a.movement_type().defense_modifier(j->first) * j->second; weighting_sum += j->second; } } @@ -386,7 +384,7 @@ int recruitment_phase::average_resistance_against(const unit_type& a, const unit for (std::map::const_iterator jj = terrain.begin(), jj_end = terrain.end(); jj != jj_end; ++jj) { - defense += a.movement_type().defense_modifier(map_, jj->first) * jj->second; + defense += a.movement_type().defense_modifier(jj->first) * jj->second; weighting_sum += jj->second; } } diff --git a/src/ai/testing/ca_testing_recruitment.cpp b/src/ai/testing/ca_testing_recruitment.cpp index 6148e164b9c..e7b4cb45fcb 100644 --- a/src/ai/testing/ca_testing_recruitment.cpp +++ b/src/ai/testing/ca_testing_recruitment.cpp @@ -249,18 +249,16 @@ class fake_team static int average_resistance_against(const unit_type& a, const unit_type& b) { - gamemap &map_ = *resources::game_map; - int weighting_sum = 0, defense = 0; const std::map& terrain = - map_.get_weighted_terrain_frequencies(); + resources::game_map->get_weighted_terrain_frequencies(); for (std::map::const_iterator j = terrain.begin(), j_end = terrain.end(); j != j_end; ++j) { // Use only reachable tiles when computing the average defense. - if (a.movement_type().movement_cost(map_, j->first) < unit_movement_type::UNREACHABLE) { - defense += a.movement_type().defense_modifier(map_, j->first) * j->second; + if (a.movement_type().movement_cost(j->first) < unit_movement_type::UNREACHABLE) { + defense += a.movement_type().defense_modifier(j->first) * j->second; weighting_sum += j->second; } } @@ -272,7 +270,7 @@ static int average_resistance_against(const unit_type& a, const unit_type& b) for (std::map::const_iterator jj = terrain.begin(), jj_end = terrain.end(); jj != jj_end; ++jj) { - defense += a.movement_type().defense_modifier(map_, jj->first) * jj->second; + defense += a.movement_type().defense_modifier(jj->first) * jj->second; weighting_sum += jj->second; } } diff --git a/src/help.cpp b/src/help.cpp index 70f69116abb..27d4f916fa8 100644 --- a/src/help.cpp +++ b/src/help.cpp @@ -1630,9 +1630,9 @@ public: std::vector row; const std::string& name = info.name(); const std::string id = info.id(); - const int moves = movement_type.movement_cost(*map,terrain); - const int views = movement_type.vision_cost(*map,terrain); - const int jamms = movement_type.jamming_cost(*map,terrain); + const int moves = movement_type.movement_cost(terrain); + const int views = movement_type.vision_cost(terrain); + const int jamms = movement_type.jamming_cost(terrain); std::stringstream str; str << "text='" << escape(name) << "' dst='" << escape(std::string("terrain_") + id) << "'"; @@ -1642,7 +1642,7 @@ public: //defense - range: +10 % .. +70 % str.str(clear_stringstream); const int defense = - 100 - movement_type.defense_modifier(*map,terrain); + 100 - movement_type.defense_modifier(terrain); std::string color; if (defense <= 10) color = "red"; diff --git a/src/pathfind/pathfind.cpp b/src/pathfind/pathfind.cpp index 800e075c75b..cff111678dc 100644 --- a/src/pathfind/pathfind.cpp +++ b/src/pathfind/pathfind.cpp @@ -706,7 +706,7 @@ double pathfind::move_type_path_calculator::cost(const map_location& loc, const return getNoPathValue(); const t_translation::t_terrain terrain = map_[loc]; - int const terrain_cost = movement_type_.movement_cost(map_, terrain); + int const terrain_cost = movement_type_.movement_cost(terrain); if (total_movement_ < terrain_cost) return getNoPathValue(); diff --git a/src/unit.cpp b/src/unit.cpp index 240dbb13e8d..c140c5ec512 100644 --- a/src/unit.cpp +++ b/src/unit.cpp @@ -2213,9 +2213,8 @@ bool unit::loyal() const int unit::movement_cost(const t_translation::t_terrain & terrain) const { - assert(resources::game_map != NULL); const int res = movement_cost_internal(movement_costs_, - cfg_.child("movement_costs"), NULL, *resources::game_map, terrain); + cfg_.child("movement_costs"), NULL, terrain); if (res == unit_movement_type::UNREACHABLE) { return res; @@ -2229,9 +2228,8 @@ int unit::vision_cost(const t_translation::t_terrain & terrain) const { if (cfg_.child_count("vision_costs") == 0) return movement_cost(terrain); - assert(resources::game_map != NULL); const int res = movement_cost_internal(vision_costs_, - cfg_.child("vision_costs"), NULL, *resources::game_map, terrain); + cfg_.child("vision_costs"), NULL, terrain); if (res == unit_movement_type::UNREACHABLE) { return res; @@ -2245,9 +2243,8 @@ int unit::jamming_cost(const t_translation::t_terrain & terrain) const { // if (cfg_.child_count("jamming_costs") == 0) return movement_cost(terrain); - assert(resources::game_map != NULL); const int res = movement_cost_internal(jamming_costs_, - cfg_.child("jamming_costs"), NULL, *resources::game_map, terrain); + cfg_.child("jamming_costs"), NULL, terrain); if (res == unit_movement_type::UNREACHABLE) { return res; @@ -2259,8 +2256,7 @@ int unit::jamming_cost(const t_translation::t_terrain & terrain) const int unit::defense_modifier(const t_translation::t_terrain & terrain) const { - assert(resources::game_map != NULL); - int def = defense_modifier_internal(defense_mods_, cfg_, NULL, *resources::game_map, terrain); + int def = defense_modifier_internal(defense_mods_, cfg_, NULL, terrain); #if 0 // A [defense] ability is too costly and doesn't take into account target locations. // Left as a comment in case someone ever wonders why it isn't a good idea. diff --git a/src/unit_types.cpp b/src/unit_types.cpp index 63a0e929feb..29a79a2248a 100644 --- a/src/unit_types.cpp +++ b/src/unit_types.cpp @@ -27,6 +27,7 @@ #include "loadscreen.hpp" #include "log.hpp" #include "map.hpp" +#include "resources.hpp" #include @@ -432,8 +433,10 @@ bool unit_movement_type::is_flying() const int movement_cost_internal(std::map& move_costs, const config& cfg, const unit_movement_type* parent, - const gamemap& map, const t_translation::t_terrain & terrain, int recurse_count) + const t_translation::t_terrain & terrain, int recurse_count) { + assert(resources::game_map != NULL); + const gamemap & map = *resources::game_map; const int impassable = unit_movement_type::UNREACHABLE; const std::map::const_iterator i = move_costs.find(terrain); @@ -466,7 +469,7 @@ int movement_cost_internal(std::map& move_costs, continue; } const int value = movement_cost_internal(move_costs, cfg, - parent, map, *i, recurse_count + 1); + parent, *i, recurse_count + 1); if (value < ret_value && !revert) { ret_value = value; @@ -499,7 +502,7 @@ int movement_cost_internal(std::map& move_costs, } if (!result_found && parent != NULL) { - res = parent->movement_cost(map, terrain); + res = parent->movement_cost(terrain); } if (res <= 0) { @@ -514,8 +517,11 @@ int movement_cost_internal(std::map& move_costs, const defense_range &defense_range_modifier_internal(defense_cache &defense_mods, const config& cfg, const unit_movement_type* parent, - const gamemap& map, const t_translation::t_terrain & terrain, int recurse_count) + const t_translation::t_terrain & terrain, int recurse_count) { + assert(resources::game_map != NULL); + const gamemap & map = *resources::game_map; + defense_range dummy = { 0, 100 }; std::pair ib = defense_mods.insert(defense_cache::value_type(terrain, dummy)); @@ -554,7 +560,7 @@ const defense_range &defense_range_modifier_internal(defense_cache &defense_mods continue; } const defense_range &inh = defense_range_modifier_internal - (defense_mods, cfg, parent, map, *i, recurse_count + 1); + (defense_mods, cfg, parent, *i, recurse_count + 1); if (!revert) { if (inh.max_ < res.max_) res.max_ = inh.max_; @@ -581,7 +587,7 @@ const defense_range &defense_range_modifier_internal(defense_cache &defense_mods if (parent) { /* Assign to the reference res to put the value in the defense_cache. */ - res = parent->defense_range_modifier(map, terrain); + res = parent->defense_range_modifier(terrain); return res; } @@ -601,10 +607,10 @@ const defense_range &defense_range_modifier_internal(defense_cache &defense_mods int defense_modifier_internal(defense_cache &defense_mods, const config &cfg, const unit_movement_type *parent, - const gamemap &map, const t_translation::t_terrain & terrain, int recurse_count) + const t_translation::t_terrain & terrain, int recurse_count) { const defense_range &def = defense_range_modifier_internal(defense_mods, - cfg, parent, map, terrain, recurse_count); + cfg, parent, terrain, recurse_count); return (std::max)(def.max_, def.min_); } diff --git a/src/unit_types.hpp b/src/unit_types.hpp index ed7f4d8cb7f..288960ee4e0 100644 --- a/src/unit_types.hpp +++ b/src/unit_types.hpp @@ -119,15 +119,15 @@ typedef std::map defense_cache; const defense_range &defense_range_modifier_internal(defense_cache &defense_mods, const config &cfg, const unit_movement_type *parent, - const gamemap &map, const t_translation::t_terrain & terrain, int recurse_count = 0); + const t_translation::t_terrain & terrain, int recurse_count = 0); int defense_modifier_internal(defense_cache &defense_mods, const config &cfg, const unit_movement_type *parent, - const gamemap &map, const t_translation::t_terrain & terrain, int recurse_count = 0); + const t_translation::t_terrain & terrain, int recurse_count = 0); int movement_cost_internal(std::map &move_costs, const config &cfg, const unit_movement_type *parent, - const gamemap &map, const t_translation::t_terrain & terrain, int recurse_count = 0); + const t_translation::t_terrain & terrain, int recurse_count = 0); //the 'unit movement type' is the basic size of the unit - flying, small land, //large land, etc etc. @@ -145,16 +145,16 @@ public: unit_movement_type(); std::string name() const; - int movement_cost(const gamemap &map, const t_translation::t_terrain & terrain) const - { return movement_cost_internal(moveCosts_, cfg_.child("movement_costs"), parent_, map, terrain); } - int vision_cost(const gamemap &map, const t_translation::t_terrain & terrain) const - { return movement_cost_internal(visionCosts_, cfg_.child("vision_costs"), parent_, map, terrain); } - int jamming_cost(const gamemap &map, const t_translation::t_terrain & terrain) const - { return movement_cost_internal(jammingCosts_, cfg_.child("jamming_costs"), parent_, map, terrain); } - int defense_modifier(const gamemap &map, const t_translation::t_terrain & terrain) const - { return defense_modifier_internal(defenseMods_, cfg_, parent_, map, terrain); } - const defense_range &defense_range_modifier(const gamemap &map, const t_translation::t_terrain & terrain) const - { return defense_range_modifier_internal(defenseMods_, cfg_, parent_, map, terrain); } + int movement_cost(const t_translation::t_terrain & terrain) const + { return movement_cost_internal(moveCosts_, cfg_.child("movement_costs"), parent_, terrain); } + int vision_cost(const t_translation::t_terrain & terrain) const + { return movement_cost_internal(visionCosts_, cfg_.child("vision_costs"), parent_, terrain); } + int jamming_cost(const t_translation::t_terrain & terrain) const + { return movement_cost_internal(jammingCosts_, cfg_.child("jamming_costs"), parent_, terrain); } + int defense_modifier(const t_translation::t_terrain & terrain) const + { return defense_modifier_internal(defenseMods_, cfg_, parent_, terrain); } + const defense_range &defense_range_modifier(const t_translation::t_terrain & terrain) const + { return defense_range_modifier_internal(defenseMods_, cfg_, parent_, terrain); } int damage_against(const attack_type& attack) const { return resistance_against(attack); } int resistance_against(const attack_type& attack) const;