wesnoth-cvs-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Wesnoth-cvs-commits] wesnoth/src unit.cpp


From: David White
Subject: [Wesnoth-cvs-commits] wesnoth/src unit.cpp
Date: Thu, 23 Dec 2004 18:46:05 -0500

CVSROOT:        /cvsroot/wesnoth
Module name:    wesnoth
Branch:         
Changes by:     David White <address@hidden>    04/12/23 23:34:39

Modified files:
        src            : unit.cpp 

Log message:
        fixed bug where unit traits would have a poor distribution

CVSWeb URLs:
http://savannah.gnu.org/cgi-bin/viewcvs/wesnoth/wesnoth/src/unit.cpp.diff?tr1=1.106&tr2=1.107&r1=text&r2=text

Patches:
Index: wesnoth/src/unit.cpp
diff -u wesnoth/src/unit.cpp:1.106 wesnoth/src/unit.cpp:1.107
--- wesnoth/src/unit.cpp:1.106  Tue Nov 30 21:14:13 2004
+++ wesnoth/src/unit.cpp        Thu Dec 23 23:34:38 2004
@@ -1,4 +1,4 @@
-/* $Id: unit.cpp,v 1.106 2004/11/30 21:14:13 silene Exp $ */
+/* $Id: unit.cpp,v 1.107 2004/12/23 23:34:38 Sirp Exp $ */
 /*
    Copyright (C) 2003 by David White <address@hidden>
    Part of the Battle for Wesnoth Project http://wesnoth.whitevine.net
@@ -150,35 +150,34 @@
                return;
 
        //calculate the unit's traits
-       const std::vector<config*> traits = type().possible_traits();
-
-       const size_t num_traits = type().num_traits();
-       if(traits.size() >= num_traits) {
-               std::set<int> chosen_traits;
-               for(size_t i = 0; i != num_traits; ++i) {
-                       int num = get_random()%(traits.size()-i);
-                       while(chosen_traits.count(num)) {
-                               ++num;
-                       }
-
-                       chosen_traits.insert(num);
-
-                       add_modification("trait",*traits[num]);
-               }
-
-               //build the traits description, making sure the traits are 
always
-               //in the same order.
-               for(std::set<int>::const_iterator itor = chosen_traits.begin();
-                   itor != chosen_traits.end(); ++itor) {
-                       const std::string& trait_name = 
(*traits[*itor])["name"];
-                       traitsDescription_ += gettext(trait_name.c_str());
-                       traitsDescription_ += ",";
-               }
-
-               //get rid of the trailing comma
-               if(!traitsDescription_.empty())
-                       traitsDescription_.resize(traitsDescription_.size()-1);
-       }
+       std::vector<config*> candidate_traits = type().possible_traits();
+       std::vector<config*> traits;
+
+       const size_t num_traits = type().num_traits();
+       for(size_t n = 0; n != num_traits && candidate_traits.empty() == false; 
++n) {
+               const int num = get_random()%candidate_traits.size();
+               traits.push_back(candidate_traits[num]);
+               candidate_traits.erase(candidate_traits.begin()+num);
+       }
+
+       std::vector<std::string> description;
+       
+       for(std::vector<config*>::const_iterator j = traits.begin(); j != 
traits.end(); ++j) {
+               add_modification("trait",**j);
+               description.push_back((**j)["name"]);
+       }
+
+       traitsDescription_ = "";
+
+       //we want to make sure the description always has a consistent ordering
+       std::sort(description.begin(),description.end());
+       for(std::vector<std::string>::const_iterator i = description.begin(); i 
!= description.end(); ++i) {
+               if(i != description.begin()) {
+                       traitsDescription_ += ",";
+               }
+
+               traitsDescription_ += gettext(i->c_str());
+       }
 }
 
 const unit_type& unit::type() const
@@ -599,7 +598,8 @@
 }
 
 void unit::read(const game_data& data, const config& cfg)
-{
+{
+       const std::string& type = cfg["type"];
        std::map<std::string,unit_type>::const_iterator i = 
data.unit_types.find(cfg["type"]);
        if(i != data.unit_types.end())
                type_ = &i->second;




reply via email to

[Prev in Thread] Current Thread [Next in Thread]