[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Wesnoth-cvs-commits] wesnoth/src random.cpp random.hpp actions.cpp
From: |
Guillaume Melquiond |
Subject: |
[Wesnoth-cvs-commits] wesnoth/src random.cpp random.hpp actions.cpp |
Date: |
Sat, 30 Apr 2005 08:31:22 -0400 |
CVSROOT: /cvsroot/wesnoth
Module name: wesnoth
Branch:
Changes by: Guillaume Melquiond <address@hidden> 05/04/30 12:31:21
Modified files:
src : random.cpp random.hpp actions.cpp
Log message:
Flat random generator: only one [random] structure with a
comma-separated list in 'value', instead of nested structures. There is still
some nesting when a [results] structure is needed. This new scheme should
reduce the size of the replay and avoid hitting the nesting limit. It is
supposed to be backward-compatible, hence a bit of complexity.
CVSWeb URLs:
http://savannah.gnu.org/cgi-bin/viewcvs/wesnoth/wesnoth/src/random.cpp.diff?tr1=1.3&tr2=1.4&r1=text&r2=text
http://savannah.gnu.org/cgi-bin/viewcvs/wesnoth/wesnoth/src/random.hpp.diff?tr1=1.2&tr2=1.3&r1=text&r2=text
http://savannah.gnu.org/cgi-bin/viewcvs/wesnoth/wesnoth/src/actions.cpp.diff?tr1=1.212&tr2=1.213&r1=text&r2=text
Patches:
Index: wesnoth/src/actions.cpp
diff -u wesnoth/src/actions.cpp:1.212 wesnoth/src/actions.cpp:1.213
--- wesnoth/src/actions.cpp:1.212 Wed Apr 27 19:34:08 2005
+++ wesnoth/src/actions.cpp Sat Apr 30 12:31:20 2005
@@ -1,4 +1,4 @@
-/* $Id: actions.cpp,v 1.212 2005/04/27 19:34:08 silene Exp $ */
+/* $Id: actions.cpp,v 1.213 2005/04/30 12:31:20 silene Exp $ */
/*
Copyright (C) 2003 by David White <address@hidden>
Part of the Battle for Wesnoth Project http://wesnoth.whitevine.net
@@ -625,6 +625,7 @@
LOG_NG << "start of attack loop...\n";
if(stats.nattacks > 0 && stats.defender_strikes_first == false)
{
+ add_random_separator();
const int ran_num = get_random();
bool hits = (ran_num%100) <
stats.chance_to_hit_defender;
@@ -794,6 +795,7 @@
if(stats.ndefends > 0) {
LOG_NG << "doing defender attack...\n";
+ add_random_separator();
const int ran_num = get_random();
bool hits = (ran_num%100) <
stats.chance_to_hit_attacker;
Index: wesnoth/src/random.cpp
diff -u wesnoth/src/random.cpp:1.3 wesnoth/src/random.cpp:1.4
--- wesnoth/src/random.cpp:1.3 Sat Apr 30 07:56:44 2005
+++ wesnoth/src/random.cpp Sat Apr 30 12:31:20 2005
@@ -1,4 +1,4 @@
-/* $Id: random.cpp,v 1.3 2005/04/30 07:56:44 silene Exp $ */
+/* $Id: random.cpp,v 1.4 2005/04/30 12:31:20 silene Exp $ */
/*
Copyright (C) 2003 by David White <address@hidden>
Copyright (C) 2005 by Yann Dirson <address@hidden>
@@ -14,49 +14,66 @@
#include "config.hpp"
#include "random.hpp"
#include "wassert.hpp"
+#include <sstream>
-rng::rng() : random_(NULL), separator_(false)
+rng::rng() : random_(NULL)
{}
-int rng::get_random(int value)
+int rng::get_random()
{
- separator_ = false;
+ if (!random_)
+ return rand();
- if(random_ == NULL) {
- return value >= 0 ? value : rand();
+ config *random;
+ if (!started_ || separator_) {
+ // setup the first [random] or find a nested [random]
+ started_ = true;
+ separator_ = false;
+ random = random_->child("random");
+ if (random == NULL) {
+ random_ = &random_->add_child("random");
+ remaining_values_ = "";
+ goto new_value;
+ }
+ random_ = random;
+ remaining_values_ = (*random_)["value"];
}
- //random numbers are in a 'list' meaning that each random
- //number contains another random numbers unless it's at
- //the end of the list. Generating a new random number means
- //nesting a new node inside the current node, and making
- //the current node the new node
- config* const random = random_->child("random");
- if(random == NULL) {
- int res = value;
- if(value < 0)
- res = rand() & 0x7FFFFFFF;
- random_ = &random_->add_child("random");
-
- char buf[100];
- sprintf(buf,"%d",res);
- (*random_)["value"] = buf;
-
- return res;
- } else {
- const int res = atoi((*random)["value"].c_str());
+ // find some remaining value
+ while (remaining_values_.empty()) {
+ random = random_->child("random");
+ if (random == NULL) {
+ // no remaining value nor child
+ // create a new value and store it, then return it
+ new_value:
+ int res = rand() & 0x7FFFFFFF;
+ std::ostringstream tmp;
+ if (!(*random_)["value"].empty())
+ tmp << ',';
+ tmp << res;
+ (*random_)["value"] += tmp.str();
+ return res;
+ }
random_ = random;
- return res;
+ remaining_values_ = (*random_)["value"];
}
+
+ // read the first remaining value and erase it
+ int res = atoi(remaining_values_.c_str()); // atoi stops at the comma
+ std::string::size_type pos = remaining_values_.find(',');
+ if (pos != std::string::npos)
+ remaining_values_.erase(0, pos + 1);
+ else
+ remaining_values_ = "";
+ return res;
}
const config* rng::get_random_results()
{
wassert(random_ != NULL);
- if(separator_) {
- get_random(0);
- }
+ if (separator_)
+ get_random();
return random_->child("results");
}
@@ -64,9 +81,8 @@
{
wassert(random_ != NULL);
- if(separator_) {
- get_random(0);
- }
+ if (separator_)
+ get_random();
random_->clear_children("results");
random_->add_child("results",cfg);
}
@@ -85,6 +101,7 @@
{
config* old = random_;
random_ = random;
+ started_ = false;
separator_ = false;
return old;
}
Index: wesnoth/src/random.hpp
diff -u wesnoth/src/random.hpp:1.2 wesnoth/src/random.hpp:1.3
--- wesnoth/src/random.hpp:1.2 Sat Apr 23 12:13:10 2005
+++ wesnoth/src/random.hpp Sat Apr 30 12:31:20 2005
@@ -1,4 +1,4 @@
-/* $Id: random.hpp,v 1.2 2005/04/23 12:13:10 gruikya Exp $ */
+/* $Id: random.hpp,v 1.3 2005/04/30 12:31:20 silene Exp $ */
/*
Copyright (C) 2003 by David White <address@hidden>
Copyright (C) 2005 by Yann Dirson <address@hidden>
@@ -29,7 +29,7 @@
{
public:
rng();
- int get_random(int value=-1);
+ int get_random();
const config* get_random_results();
void set_random_results(const config& cfg);
@@ -41,7 +41,8 @@
private:
config* random_;
- bool separator_;
+ bool separator_, started_;
+ std::string remaining_values_;
};
struct set_random_generator {
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [Wesnoth-cvs-commits] wesnoth/src random.cpp random.hpp actions.cpp,
Guillaume Melquiond <=