Author: anonymissimus
Date: Sun May 22 23:23:17 2011
New Revision: 49631

URL: http://svn.gna.org/viewcvs/wesnoth?rev=49631&view=rev
Log:
introduced member variable currentTime for all time areas, refactored 
functions, comments
(fix for bug #17543 and bug #16584)

Modified:
    trunk/src/tod_manager.cpp
    trunk/src/tod_manager.hpp

Modified: trunk/src/tod_manager.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/tod_manager.cpp?rev=49631&r1=49630&r2=49631&view=diff
==============================================================================
--- trunk/src/tod_manager.cpp (original)
+++ trunk/src/tod_manager.cpp Sun May 22 23:23:17 2011
@@ -91,6 +91,7 @@
                for(t = i->times.begin(); t != i->times.end(); ++t) {
                        t->write(area.add_child("time"));
                }
+               area["current_time"] = i->currentTime;
        }
        return cfg;
 }
@@ -100,19 +101,9 @@
        return times_[currentTime_];
 }
 
-void tod_manager::set_time_of_day(int newTime)
-{
-       newTime = newTime % times_.size();
-       while(newTime < 0) {
-               newTime += times_.size();
-       }
-
-       currentTime_ = newTime;
-}
-
 const time_of_day& tod_manager::get_previous_time_of_day() const
 {
-       return get_time_of_day_turn(times_, turn_ - 1);
+       return get_time_of_day_turn(times_, turn_ - 1, currentTime_);
 }
 
 
@@ -128,7 +119,7 @@
        {
                return get_time_of_day(loc, nturn);
        }
-       return get_time_of_day_turn(times_, nturn);
+       return get_time_of_day_turn(times_, nturn, currentTime_);
 }
 
 const time_of_day& tod_manager::get_time_of_day(const map_location& loc, int 
n_turn) const
@@ -141,10 +132,10 @@
        {
                if (i->hexes.count(loc) != 1) continue;
                VALIDATE(i->times.size(), _("No time of day has been 
defined."));
-               return get_time_of_day_turn(i->times, n_turn);
-       }
-
-       return get_time_of_day_turn(times_, n_turn);
+               return get_time_of_day_turn(i->times, n_turn, i->currentTime);
+       }
+
+       return get_time_of_day_turn(times_, n_turn, currentTime_);
 }
 
 bool tod_manager::is_start_ToD(const std::string& random_start_time)
@@ -157,6 +148,7 @@
 {
        times_.clear();
        time_of_day::parse_times(time_cfg,times_);
+       currentTime_ = 0;
 }
 
 void tod_manager::add_time_area(const config& cfg)
@@ -166,6 +158,7 @@
        area.id = cfg["id"].str();
        area.xsrc = cfg["x"].str();
        area.ysrc = cfg["y"].str();
+       area.currentTime = cfg["current_time"].to_int(0);
        std::vector<map_location> const& locs = parse_location_range(area.xsrc, 
area.ysrc, true);
        std::copy(locs.begin(), locs.end(), std::inserter(area.hexes, 
area.hexes.end()));
        time_of_day::parse_times(cfg, area.times);
@@ -202,7 +195,7 @@
 {
        if (!level["current_tod"].empty())
        {
-               set_time_of_day(level["current_tod"]);
+               currentTime_ = calculate_current_time(times_.size(), 
current_turn, level["current_tod"], true);
                return;
        }
        std::string random_start_time = level["random_start_time"];
@@ -214,18 +207,24 @@
                if (utils::string_bool(random_start_time,false))
                {
                        // We had boolean value
-                       set_time_of_day(rand()%times_.size());
+                       currentTime_ = calculate_current_time(times_.size(), 
current_turn, rand(), true);
                }
                else
                {
-                       
set_time_of_day(atoi(start_strings[rand()%start_strings.size()].c_str()) - 1);
+                       const int index = 
calculate_current_time(start_strings.size(),
+                               current_turn, rand(), true);
+                       currentTime_ = calculate_current_time(
+                                       times_.size(),
+                                       current_turn,
+                                       lexical_cast_default<int, 
std::string>(start_strings[index], 1) - 1,
+                                       true
+                               );
                }
        }
        else
        {
                // We have to set right ToD for oldsaves
-
-               set_time_of_day((current_turn - 1) % times_.size());
+               currentTime_ = calculate_current_time(times_.size(), 
current_turn, currentTime_);
        }
        // Setting ToD to level data
 
@@ -233,14 +232,9 @@
 
 }
 
-const time_of_day& tod_manager::get_time_of_day_turn(const 
std::vector<time_of_day>& times, int nturn) const
-{
-       int time = (currentTime_ + nturn  - turn_) % times.size();
-
-       while(time < 0) {
-               time += times.size();
-       }
-
+const time_of_day& tod_manager::get_time_of_day_turn(const 
std::vector<time_of_day>& times, int nturn, const int current_time) const
+{
+       const int time = calculate_current_time(times.size(), nturn, 
current_time);
        return times[time];
 }
 
@@ -299,27 +293,46 @@
        num_turns_ = std::max<int>(num, -1);
 }
 
-void tod_manager::set_turn(unsigned int num)
-{
-       const unsigned int old_num = turn_;
+void tod_manager::set_turn(const int num)
+{
+       const int new_turn = std::max<int>(num, 1);
+       LOG_NG << "changing current turn number from " << turn_ << " to " << 
new_turn << '\n';
        // Correct ToD
-       int current_time = (currentTime_ + num - old_num) % times_.size();
-       if (current_time < 0) {
-               current_time += times_.size();
-       }
-       set_time_of_day(current_time);
-
-       if(static_cast<int>(num) > num_turns_ && num_turns_ != -1) {
-               set_number_of_turns(num);
-       }
-       turn_ = num;
-
-       LOG_NG << "changed current turn number from " << old_num << " to " << 
num << '\n';
+       set_new_current_times(new_turn);
+
+       if((new_turn > num_turns_) && num_turns_ != -1) {
+               set_number_of_turns(new_turn);
+       }
+       turn_ = new_turn;
+}
+
+void tod_manager::set_new_current_times(const int new_current_turn_number)
+{
+       currentTime_ = calculate_current_time(times_.size(), 
new_current_turn_number, currentTime_);
+       foreach(area_time_of_day& area, areas_) {
+               area.currentTime = calculate_current_time(
+                       area.times.size(),
+                       new_current_turn_number,
+                       area.currentTime);
+       }
+}
+
+const int tod_manager::calculate_current_time(
+       const int number_of_times,
+       const int for_turn_number,
+       const int current_time,
+       const bool only_to_allowed_range) const
+{
+       int new_current_time = 0;
+       if(only_to_allowed_range) new_current_time = current_time % 
number_of_times;
+       else new_current_time = (current_time + for_turn_number - turn_) % 
number_of_times;
+       while(new_current_time < 0) { new_current_time += number_of_times; }
+       return new_current_time;
 }
 
 bool tod_manager::next_turn()
 {
-       currentTime_ = (currentTime_ + 1)%times_.size();
+       set_new_current_times(turn_ + 1);
        ++turn_;
        return is_time_left();
 }

Modified: trunk/src/tod_manager.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/tod_manager.hpp?rev=49631&r1=49630&r2=49631&view=diff
==============================================================================
--- trunk/src/tod_manager.hpp (original)
+++ trunk/src/tod_manager.hpp Sun May 22 23:23:17 2011
@@ -47,10 +47,6 @@
                 * If nturn = 0 use current turn
                 */
                const time_of_day& get_time_of_day(const map_location& loc, int 
n_turn = 0) const;
-               /**
-                * Sets global time of day in this turn.
-                */
-               void set_time_of_day(int newTime);
 
                static bool is_start_ToD(const std::string&);
 
@@ -103,7 +99,7 @@
                void set_number_of_turns(int num);
 
                /** Dynamically change the current turn number. */
-               void set_turn(unsigned int num);
+               void set_turn(const int num);
 
                /**
                 * Function to move to the next turn.
@@ -122,11 +118,30 @@
                void set_start_ToD(config&, int current_turn);
 
                /**
-                * Returns time of day object in the turn.
+                * Returns time of day object in the turn "nturn".
                 *
                 * Correct time is calculated from current time.
                 */
-               const time_of_day& get_time_of_day_turn(const 
std::vector<time_of_day>& times, int nturn) const;
+               const time_of_day& get_time_of_day_turn(const 
std::vector<time_of_day>& times, int nturn, const int current_time) const;
+
+               /**
+                * Computes for the main time or a time area the index of its 
times where we're currently at.
+                * number_of_times: size of that main time or time area's times 
vector
+                * for_turn_number: for which current turn
+                * current_time: the main or time area's current time
+                */
+               const int calculate_current_time(
+                       const int number_of_times,
+                       const int for_turn_number,
+                       const int current_time,
+                       const bool only_to_allowed_range = false) const;
+
+               /**
+                * For a change of the current turn number, sets the current 
times of the main time
+                * and all time areas.
+                */
+               void set_new_current_times(const int new_current_turn_number);
+
 
                struct area_time_of_day {
                        area_time_of_day() :
@@ -134,20 +149,25 @@
                                ysrc(),
                                id(),
                                times(),
-                               hexes()
+                               hexes(),
+                               currentTime(0)
                        {}
 
                        std::string xsrc, ysrc;
                        std::string id;
                        std::vector<time_of_day> times;
                        std::set<map_location> hexes;
+                       int currentTime;
                };
 
+               //index of the times vector of the main time where we're 
currently at
                int currentTime_;
                std::vector<time_of_day> times_;
                std::vector<area_time_of_day> areas_;
 
+               // current turn
                int turn_;
+               //turn limit
                int num_turns_;
 };
 #endif


_______________________________________________
Wesnoth-commits mailing list
[email protected]
https://mail.gna.org/listinfo/wesnoth-commits

Reply via email to