add new rng

the plan is to have random_new::generator which can be a rng_synced or not dependng on whether we are in a synced context (for example select events are no synced context)

this commit is part of pr 121.
This commit is contained in:
gfgtdf 2014-03-19 16:38:16 +01:00
parent 0f9ad019c6
commit bf4935da84
8 changed files with 298 additions and 1 deletions

View file

@ -844,7 +844,11 @@ set(wesnoth-main_SRC
playsingle_controller.cpp
playturn.cpp
portrait.cpp
random_new.cpp
random_new_deterministic.cpp
random_new_synced.cpp
replay.cpp
replay_helper.cpp
replay_controller.cpp
resources.cpp
save_blocker.cpp

View file

@ -478,7 +478,10 @@ wesnoth_sources = Split("""
playsingle_controller.cpp
playturn.cpp
portrait.cpp
replay.cpp
random_new.cpp
random_new_deterministic.cpp
random_new_synced.cpp
replay.cpp
replay_controller.cpp
resources.cpp
save_blocker.cpp

61
src/random_new.cpp Normal file
View file

@ -0,0 +1,61 @@
/*
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#include "random_new.hpp"
#include "log.hpp"
#include <stdlib.h>
static lg::log_domain log_random("random");
#define DBG_RND LOG_STREAM(debug, log_random)
#define LOG_RND LOG_STREAM(info, log_random)
#define WRN_RND LOG_STREAM(warn, log_random)
#define ERR_RND LOG_STREAM(err, log_random)
namespace random_new
{
rng* generator = new rng();
rng::rng()
: random_calls_(0)
{
}
rng::~rng()
{
}
unsigned int rng::get_random_calls()
{
return random_calls_;
}
int rng::next_random()
{
random_calls_++;
return next_random_impl();
}
int rng::next_random_impl()
{
//getting here means random was called form outsiude a synced context.
int retv = rand();
LOG_RND << "random_new::rng::next_random returned " << retv;
return retv;
}
}

37
src/random_new.hpp Normal file
View file

@ -0,0 +1,37 @@
/*
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#ifndef RANDOM_NEW_H_INCLUDED
#define RANDOM_NEW_H_INCLUDED
namespace random_new
{
//this class does NOT give synced random results.
class rng
{
public:
rng();
int next_random();
virtual ~rng();
unsigned int get_random_calls();
protected:
virtual int next_random_impl();
unsigned int random_calls_;
};
// this generator is autmatilcy synced during synced context.
// calling this should automaticly clear the undo stack.
//
extern rng* generator;
}
#endif

View file

@ -0,0 +1,46 @@
/*
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#include "random_new_deterministic.hpp"
namespace random_new
{
rng_deterministic::rng_deterministic(rand_rng::simple_rng& gen)
: generator_(gen)
{
}
rng_deterministic::~rng_deterministic()
{
}
int rng_deterministic::next_random_impl()
{
return generator_.get_next_random();
}
set_random_determinstic::set_random_determinstic(rand_rng::simple_rng& rng)
: old_rng_(generator), new_rng_(rng)
{
generator = &new_rng_;
}
set_random_determinstic::~set_random_determinstic()
{
generator = old_rng_;
}
}

View file

@ -0,0 +1,49 @@
/*
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#ifndef RANDOM_NEW_DETERMINISTIC_H_INCLUDED
#define RANDOM_NEW_DETERMINISTIC_H_INCLUDED
#include "random_new.hpp"
#include "simple_rng.hpp"
namespace random_new
{
/*
this is the determinstic random class, it behaves similar to the old random class.
it's only application is at the very start of the scneario.
or durign the "Deterministic SP mode"
*/
class rng_deterministic : public random_new::rng
{
public:
rng_deterministic(rand_rng::simple_rng& gen);
virtual ~rng_deterministic();
protected:
virtual int next_random_impl();
private:
rand_rng::simple_rng& generator_;
};
//RAII class
class set_random_determinstic
{
public:
set_random_determinstic(rand_rng::simple_rng& rng);
~set_random_determinstic();
private :
rng* old_rng_;
rng_deterministic new_rng_;
};
}
#endif

52
src/random_new_synced.cpp Normal file
View file

@ -0,0 +1,52 @@
/*
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#include "random_new_synced.hpp"
#include "log.hpp"
static lg::log_domain log_random("random");
#define DBG_RND LOG_STREAM(debug, log_random)
#define LOG_RND LOG_STREAM(info, log_random)
#define WRN_RND LOG_STREAM(warn, log_random)
#define ERR_RND LOG_STREAM(err, log_random)
namespace random_new
{
synced_rng::synced_rng(boost::function0<int> seed_generator)
: has_valid_seed_(false), seed_generator_(seed_generator), gen_()
{
}
int synced_rng::next_random_impl()
{
if(!has_valid_seed_)
{
initialize();
}
//getting here means random was called form inside a synced context.
int retv = gen_.get_next_random();
LOG_RND << "random_new::rng::next_random_impl returned " << retv;
return retv;
}
void synced_rng::initialize()
{
int new_seed = seed_generator_();
gen_.seed_random(new_seed, 0);
has_valid_seed_ = true;
}
synced_rng::~synced_rng()
{
}
}

45
src/random_new_synced.hpp Normal file
View file

@ -0,0 +1,45 @@
/*
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#ifndef RANDOM_NEW_SYNCED_H_INCLUDED
#define RANDOM_NEW_SYNCED_H_INCLUDED
#include "random_new.hpp"
#include "simple_rng.hpp"
#include <boost/function.hpp>
/*
todo: use a boost::random based solution.
*/
namespace random_new
{
class synced_rng : public random_new::rng
{
public:
synced_rng(boost::function0<int> seed_generator);
virtual ~synced_rng();
protected:
virtual int next_random_impl();
private:
void initialize();
bool has_valid_seed_;
boost::function0<int> seed_generator_;
//TODO: replayce this by boost::random::mt19937 or similar
rand_rng::simple_rng gen_;
};
}
#endif