mail[Wesnoth-commits] r40329 - in /trunk/src: ./ ai/default/ ai/testing/


Others Months | Index by Date | Thread Index
>>   [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Header


Content

Posted by guillaume . melquiond on December 22, 2009 - 14:52:
Author: silene
Date: Tue Dec 22 14:52:50 2009
New Revision: 40329

URL: http://svn.gna.org/viewcvs/wesnoth?rev=40329&view=rev
Log:
Manipulated unit states as booleans for improved performances.
Flag 'healable' needs some special casing: it is the only state in the game 
with a reverted semantic (unset when present).

Modified:
    trunk/src/actions.cpp
    trunk/src/ai/default/move.cpp
    trunk/src/ai/default/village.cpp
    trunk/src/ai/testing/ca.cpp
    trunk/src/ai/testing/ca_default_move_to_targets.cpp
    trunk/src/ai/testing/ca_testing_move_to_targets.cpp
    trunk/src/callable_objects.cpp
    trunk/src/unit.cpp
    trunk/src/unit.hpp

Modified: trunk/src/actions.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/actions.cpp?rev=40329&r1=40328&r2=40329&view=diff
==============================================================================
--- trunk/src/actions.cpp (original)
+++ trunk/src/actions.cpp Tue Dec 22 14:52:50 2009
@@ -689,24 +689,24 @@
                weapon->set_specials_context(*aloc, *dloc, units, attacking, 
opp_weapon);
                if (opp_weapon)
                        opp_weapon->set_specials_context(*aloc, *dloc, units, 
!attacking, weapon);
+               bool not_living = opp.get_state("not_living");
                slows = weapon->get_special_bool("slow");
-               drains = weapon->get_special_bool("drains") && 
!utils::string_bool(opp.get_state("not_living"));
+               drains = !not_living && weapon->get_special_bool("drains");
                petrifies = weapon->get_special_bool("petrifies");
-               poisons = weapon->get_special_bool("poison") && 
utils::string_bool(opp.get_state("not_living")) != true && 
opp.get_state(unit::STATE_POISONED) != true;
+               poisons = !not_living && weapon->get_special_bool("poison") 
&& !opp.get_state(unit::STATE_POISONED);
                backstab_pos = is_attacker && backstab_check(u_loc, opp_loc, 
units, *resources::teams);
                rounds = weapon->get_specials("berserk").highest("value", 
1).first;
                firststrike = weapon->get_special_bool("firststrike");
 
                // Handle plague.
                unit_ability_list plague_specials = 
weapon->get_specials("plague");
-               plagues = !plague_specials.empty() && 
opp.get_state("not_living") != "yes" &&
+               plagues = !not_living && !plague_specials.empty() &&
                        strcmp(opp.undead_variation().c_str(), "null") && 
!resources::game_map->is_village(opp_loc);
 
-               if (!plague_specials.empty()) {
-                       if((*plague_specials.cfgs.front().first)["type"] == 
"")
+               if (plagues) {
+                       plague_type = 
(*plague_specials.cfgs.front().first)["type"];
+                       if (plague_type.empty())
                                plague_type = u.type_id();
-                       else
-                               plague_type = 
(*plague_specials.cfgs.front().first)["type"];
                }
 
                // Compute chance to hit.
@@ -1554,8 +1554,7 @@
        // We look for all allied units, then we see if our healer is near 
them.
        for (unit_map::iterator i = units.begin(); i != units.end(); ++i) {
 
-               if (!utils::string_bool(i->second.get_state("healable"),true) 
||
-                       i->second.incapacitated())
+               if (i->second.get_state("not_healable") || 
i->second.incapacitated())
                        continue;
 
                DBG_NG << "found healable unit at (" << i->first << ")\n";

Modified: trunk/src/ai/default/move.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/ai/default/move.cpp?rev=40329&r1=40328&r2=40329&view=diff
==============================================================================
--- trunk/src/ai/default/move.cpp (original)
+++ trunk/src/ai/default/move.cpp Tue Dec 22 14:52:50 2009
@@ -374,7 +374,7 @@
        }
 
        //guardian units stay put
-       if(utils::string_bool(u->second.get_state("guardian"))) {
+       if (u->second.get_state("guardian")) {
                LOG_AI << u->second.type_id() << " is guardian, staying 
still\n";
                return std::pair<location,location>(u->first,u->first);
        }
@@ -467,8 +467,10 @@
                LOG_AI << "complex targeting...\n";
                //now see if any other unit can put a better bid forward
                for(++u; u != units_.end(); ++u) {
-                       if(u->second.side() != get_side() || 
u->second.can_recruit() ||
-                          u->second.movement_left() <= 0 || 
utils::string_bool(u->second.get_state("guardian")) || 
u->second.incapacitated()) {
+                       if (u->second.side() != get_side() || 
u->second.can_recruit() ||
+                           u->second.movement_left() <= 0 || 
u->second.get_state("guardian") ||
+                           u->second.incapacitated())
+                       {
                                continue;
                        }
 

Modified: trunk/src/ai/default/village.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/ai/default/village.cpp?rev=40329&r1=40328&r2=40329&view=diff
==============================================================================
--- trunk/src/ai/default/village.cpp (original)
+++ trunk/src/ai/default/village.cpp Tue Dec 22 14:52:50 2009
@@ -350,7 +350,7 @@
                }
 
                const unit_map::const_iterator u = units_.find(j->second);
-               if(u == units_.end() || 
utils::string_bool(u->second.get_state("guardian"))) {
+               if (u == units_.end() || u->second.get_state("guardian")) {
                        continue;
                }
 

Modified: trunk/src/ai/testing/ca.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/ai/testing/ca.cpp?rev=40329&r1=40328&r2=40329&view=diff
==============================================================================
--- trunk/src/ai/testing/ca.cpp (original)
+++ trunk/src/ai/testing/ca.cpp Tue Dec 22 14:52:50 2009
@@ -1156,7 +1156,7 @@
                }
 
                const unit_map::const_iterator u = 
get_info().units.find(j->second);
-               if(u == get_info().units.end() || 
utils::string_bool(u->second.get_state("guardian"))) {
+               if (u == get_info().units.end() || 
u->second.get_state("guardian")) {
                        continue;
                }
 

Modified: trunk/src/ai/testing/ca_default_move_to_targets.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/ai/testing/ca_default_move_to_targets.cpp?rev=40329&r1=40328&r2=40329&view=diff
==============================================================================
--- trunk/src/ai/testing/ca_default_move_to_targets.cpp (original)
+++ trunk/src/ai/testing/ca_default_move_to_targets.cpp Tue Dec 22 14:52:50 
2009
@@ -291,7 +291,7 @@
        }
 
        //guardian units stay put
-       if(utils::string_bool(u->second.get_state("guardian"))) {
+       if (u->second.get_state("guardian")) {
                LOG_AI << u->second.type_id() << " is guardian, staying 
still\n";
                return 
std::pair<map_location,map_location>(u->first,u->first);
        }
@@ -384,8 +384,10 @@
                LOG_AI << "complex targeting...\n";
                //now see if any other unit can put a better bid forward
                for(++u; u != units_.end(); ++u) {
-                       if(u->second.side() != get_side() || 
u->second.can_recruit() ||
-                          u->second.movement_left() <= 0 || 
utils::string_bool(u->second.get_state("guardian")) || 
u->second.incapacitated()) {
+                       if (u->second.side() != get_side() || 
u->second.can_recruit() ||
+                           u->second.movement_left() <= 0 || 
u->second.get_state("guardian") ||
+                           u->second.incapacitated())
+                       {
                                continue;
                        }
 

Modified: trunk/src/ai/testing/ca_testing_move_to_targets.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/ai/testing/ca_testing_move_to_targets.cpp?rev=40329&r1=40328&r2=40329&view=diff
==============================================================================
--- trunk/src/ai/testing/ca_testing_move_to_targets.cpp (original)
+++ trunk/src/ai/testing/ca_testing_move_to_targets.cpp Tue Dec 22 14:52:50 
2009
@@ -291,7 +291,7 @@
        }
 
        //guardian units stay put
-       if(utils::string_bool(u->second.get_state("guardian"))) {
+       if (u->second.get_state("guardian")) {
                LOG_AI << u->second.type_id() << " is guardian, staying 
still\n";
                return 
std::pair<map_location,map_location>(u->first,u->first);
        }
@@ -384,8 +384,10 @@
                LOG_AI << "complex targeting...\n";
                //now see if any other unit can put a better bid forward
                for(++u; u != units_.end(); ++u) {
-                       if(u->second.side() != get_side() || 
u->second.can_recruit() ||
-                          u->second.movement_left() <= 0 || 
utils::string_bool(u->second.get_state("guardian")) || 
u->second.incapacitated()) {
+                       if (u->second.side() != get_side() || 
u->second.can_recruit() ||
+                           u->second.movement_left() <= 0 || 
u->second.get_state("guardian") ||
+                           u->second.incapacitated())
+                       {
                                continue;
                        }
 

Modified: trunk/src/callable_objects.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/callable_objects.cpp?rev=40329&r1=40328&r2=40329&view=diff
==============================================================================
--- trunk/src/callable_objects.cpp (original)
+++ trunk/src/callable_objects.cpp Tue Dec 22 14:52:50 2009
@@ -170,10 +170,7 @@
        } else if(key == "leader") {
                return variant(u_.can_recruit());
        } else if(key == "undead") {
-               if ( utils::string_bool(u_.get_state("not_living"))  )
-                    return variant( 1 );
-
-               return variant( 0 );
+               return variant(u_.get_state("not_living") ? 1 : 0);
        } else if(key == "attacks") {
                const std::vector<attack_type>& att = u_.attacks();
                std::vector<variant> res;

Modified: trunk/src/unit.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/unit.cpp?rev=40329&r1=40328&r2=40329&view=diff
==============================================================================
--- trunk/src/unit.cpp (original)
+++ trunk/src/unit.cpp Tue Dec 22 14:52:50 2009
@@ -414,12 +414,17 @@
        if (const config &status_flags = cfg.child("status"))
        {
                foreach (const config::attribute &st, 
status_flags.attribute_range()) {
-                       set_state(st.first,st.second);
+                       if (st.first == "healable") {
+                               if (!utils::string_bool(st.second, true))
+                                       set_state("not_healable", true);
+                       } else if (utils::string_bool(st.second)) {
+                               set_state(st.first, true);
+                       }
                }
                cfg_.remove_child("status",0);
        }
        if(cfg["ai_special"] == "guardian") {
-               set_state("guardian","yes");
+               set_state("guardian", true);
        }
 
        // Remove animations from private cfg, they're not needed there now
@@ -1037,7 +1042,11 @@
 
 const std::map<std::string,std::string> unit::get_states() const
 {
-       std::map<std::string,std::string> all_states = states_;
+       std::map<std::string, std::string> all_states;
+       foreach (std::string const &s, states_) {
+               if (s == "not_healable") all_states["healable"] = "no";
+               else all_states[s] = "yes";
+       }
        for (std::map<std::string, state_t>::const_iterator i = 
known_boolean_state_names_.begin(),
             i_end = known_boolean_state_names_.end(); i != i_end; ++i)
        {
@@ -1049,17 +1058,13 @@
        return all_states;
 }
 
-std::string unit::get_state(const std::string& state) const
+bool unit::get_state(const std::string &state) const
 {
        state_t known_boolean_state_id = get_known_boolean_state_id(state);
        if (known_boolean_state_id!=STATE_UNKNOWN){
-               return get_state(known_boolean_state_id) ? "yes" : "";
-       }
-       std::map<std::string,std::string>::const_iterator i = 
states_.find(state);
-       if(i != states_.end()) {
-               return i->second;
-       }
-       return "";
+               return get_state(known_boolean_state_id);
+       }
+       return states_.find(state) != states_.end();
 }
 
 void unit::set_state(state_t state, bool value)
@@ -1095,25 +1100,17 @@
        return known_boolean_state_names_map;
 }
 
-void unit::set_state(const std::string& state, const std::string& value)
+void unit::set_state(const std::string &state, bool value)
 {
        state_t known_boolean_state_id = get_known_boolean_state_id(state);
-       if (value.empty()) {
-               if (known_boolean_state_id!=STATE_UNKNOWN){
-                       set_state(known_boolean_state_id,false);
-                       return;
-               }
-               std::map<std::string,std::string>::iterator i = 
states_.find(state);
-               if(i != states_.end()) {
-                       states_.erase(i);
-               }
-       } else {
-               if (known_boolean_state_id!=STATE_UNKNOWN){
-                       
set_state(known_boolean_state_id,utils::string_bool(value));
-                       return;
-               }
-               states_[state] = value;
-       }
+       if (known_boolean_state_id != STATE_UNKNOWN) {
+               set_state(known_boolean_state_id, value);
+               return;
+       }
+       if (value)
+               states_.insert(state);
+       else
+               states_.erase(state);
 }
 
 

Modified: trunk/src/unit.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/unit.hpp?rev=40329&r1=40328&r2=40329&view=diff
==============================================================================
--- trunk/src/unit.hpp (original)
+++ trunk/src/unit.hpp Tue Dec 22 14:52:50 2009
@@ -157,8 +157,8 @@
        void set_resting(bool rest) { resting_ = rest; }
 
        const std::map<std::string,std::string> get_states() const;
-       std::string get_state(const std::string& state) const;
-       void set_state(const std::string& state, const std::string& value);
+       bool get_state(const std::string& state) const;
+       void set_state(const std::string &state, bool value);
        enum state_t { STATE_SLOWED = 0, STATE_POISONED, STATE_PETRIFIED,
                STATE_HIDDEN, STATE_NOT_MOVED, STATE_UNKNOWN = -1 };
        void set_state(state_t state, bool value);
@@ -400,7 +400,7 @@
        int attacks_left_;
        int max_attacks_;
 
-       std::map<std::string,std::string> states_;
+       std::set<std::string> states_;
        std::vector<bool> known_boolean_states_;
        static std::map<std::string, state_t> known_boolean_state_names_;
        config variables_;




Related Messages


Powered by MHonArc, Updated Tue Dec 22 15:02:46 2009