use new synced_context: use new rng
the plan is, that random_new::generator will be a object that does synced random calls during a synced context and otherwise not, so that we cannot create OOS anymore by using random in unsynced actions like select event the intention is also to unfy random calls for attacks and random calls and for other actions like traits and [set_variable] rand= . the synced random generator will be set with the set_scontext_synced object. the new rng will be similar to the old rng used for attacks becasue it will ask the server for a new seed for every new user action (=recruit, attack...) that requires random numbers. Previously there were 2 different random generators, one for attacks, and one for other things, pr 121 is supposed to fix the problems with the "sending data over teh network first" an we try to throw the old rng for non attack things out. So that attacks and other tings use the same rng. We use a new random generator which is automaticly synced during the execution of scned_context::run_in_scned_context, and otherwise not. So we cannot cause oos anymore by using rand= from an unsnced command like "select" Alternativeley we can also set the new random synced by using the new RAII object set_sconext_sycned, this happens for example during prestart events. Unlike the old random generator, the new random generator is not determinstic by default because it asks the server (or itself in a SP game) for a new seed, at every side command (attack, move, recruit ...) that requires random, similar to how the previous rand for attacks worked (it doesn't ask at the beginning, just the first time it is needed unlinke how attacks worked before). That way i can also use the same rng for attacks and for other random choices. I also plan to move the code from set/get_random_results to syncec_checkup.cpp later. this commit is part of pr 121.
This commit is contained in:
parent
70d68e20f7
commit
75d81ee49c
6 changed files with 25 additions and 26 deletions
|
@ -32,9 +32,11 @@
|
|||
#include "../map.hpp"
|
||||
#include "../mouse_handler_base.hpp"
|
||||
#include "../random.hpp"
|
||||
#include "../random_new.hpp"
|
||||
#include "../replay.hpp"
|
||||
#include "../resources.hpp"
|
||||
#include "../statistics.hpp"
|
||||
#include "../synced_checkup.hpp"
|
||||
#include "../team.hpp"
|
||||
#include "../tod_manager.hpp"
|
||||
#include "../unit.hpp"
|
||||
|
@ -882,7 +884,7 @@ namespace {
|
|||
int &abs_n = *(attacker_turn ? &abs_n_attack_ : &abs_n_defend_);
|
||||
bool &update_fog = *(attacker_turn ? &update_def_fog_ : &update_att_fog_);
|
||||
|
||||
int ran_num = get_random();
|
||||
int ran_num = random_new::generator->next_random();
|
||||
bool hits = (ran_num % 100) < attacker.cth_;
|
||||
|
||||
int damage = 0;
|
||||
|
|
|
@ -46,6 +46,7 @@
|
|||
#include "../persist_var.hpp"
|
||||
#include "../play_controller.hpp"
|
||||
#include "../replay.hpp"
|
||||
#include "../random_new.hpp"
|
||||
#include "../resources.hpp"
|
||||
#include "../side_filter.hpp"
|
||||
#include "../sound.hpp"
|
||||
|
@ -2061,10 +2062,10 @@ WML_HANDLER_FUNCTION(set_variable, /*event_info*/, cfg)
|
|||
<< 0x3fffffff
|
||||
<< ".\n";
|
||||
}
|
||||
long choice = gameinfo->rng().get_next_random();
|
||||
long choice = random_new::generator->next_random();// gameinfo->rng().get_next_random();
|
||||
if(num_choices >= 32768) {
|
||||
choice <<= 15;
|
||||
choice += gameinfo->rng().get_next_random();
|
||||
choice += random_new::generator->next_random();//gameinfo->rng().get_next_random();
|
||||
}
|
||||
choice %= num_choices;
|
||||
long tmp = 0;
|
||||
|
|
11
src/race.cpp
11
src/race.cpp
|
@ -24,8 +24,7 @@
|
|||
#include "log.hpp"
|
||||
#include "random.hpp"
|
||||
#include "serialization/string_utils.hpp"
|
||||
#include "simple_rng.hpp"
|
||||
|
||||
#include "random_new.hpp"
|
||||
|
||||
/// Dummy race used when a race is not yet known.
|
||||
const unit_race unit_race::null_race;
|
||||
|
@ -67,7 +66,7 @@ static markov_prefix_map markov_prefixes(const std::vector<std::string>& items,
|
|||
}
|
||||
|
||||
static ucs4::string markov_generate_name(const markov_prefix_map& prefixes,
|
||||
size_t chain_size, size_t max_len, rand_rng::simple_rng* rng)
|
||||
size_t chain_size, size_t max_len)
|
||||
{
|
||||
if(chain_size == 0)
|
||||
return ucs4::string();
|
||||
|
@ -87,7 +86,7 @@ static ucs4::string markov_generate_name(const markov_prefix_map& prefixes,
|
|||
std::vector<int> random(max_len);
|
||||
size_t j = 0;
|
||||
for(; j < max_len; ++j) {
|
||||
random[j] = rng ? rng->get_next_random() : get_random_nocheck();
|
||||
random[j] = random_new::generator->next_random();
|
||||
}
|
||||
|
||||
j = 0;
|
||||
|
@ -201,10 +200,10 @@ unit_race::unit_race(const config& cfg) :
|
|||
}
|
||||
|
||||
std::string unit_race::generate_name(
|
||||
unit_race::GENDER gender, rand_rng::simple_rng* rng) const
|
||||
unit_race::GENDER gender) const
|
||||
{
|
||||
return unicode_cast<utf8::string>(
|
||||
markov_generate_name(next_[gender], chain_size_, 12, rng));
|
||||
markov_generate_name(next_[gender], chain_size_, 12));
|
||||
}
|
||||
|
||||
bool unit_race::uses_global_traits() const
|
||||
|
|
|
@ -18,9 +18,6 @@
|
|||
#include "config.hpp"
|
||||
#include "serialization/unicode.hpp"
|
||||
|
||||
namespace rand_rng {
|
||||
class simple_rng;
|
||||
} // namespace rand_rng
|
||||
|
||||
|
||||
typedef std::map<ucs4::string, ucs4::string > markov_prefix_map;
|
||||
|
@ -41,7 +38,7 @@ public:
|
|||
const t_string& plural_name() const { return plural_name_; }
|
||||
const t_string& description() const { return description_; }
|
||||
|
||||
std::string generate_name(GENDER gender, rand_rng::simple_rng* rng = 0) const;
|
||||
std::string generate_name(GENDER gender) const;
|
||||
|
||||
bool uses_global_traits() const;
|
||||
|
||||
|
|
24
src/unit.cpp
24
src/unit.cpp
|
@ -33,6 +33,7 @@
|
|||
#include "unit_abilities.hpp"
|
||||
#include "terrain_filter.hpp"
|
||||
#include "formula_string_utils.hpp"
|
||||
#include "random_new.hpp"
|
||||
#include "scripting/lua.hpp"
|
||||
#include "side_filter.hpp"
|
||||
#include "play_controller.hpp"
|
||||
|
@ -85,7 +86,7 @@ static const unit_type &get_unit_type(const std::string &type_id)
|
|||
return *i;
|
||||
}
|
||||
|
||||
static unit_race::GENDER generate_gender(const unit_type & type, bool random_gender, rand_rng::simple_rng* rng)
|
||||
static unit_race::GENDER generate_gender(const unit_type & type, bool random_gender)
|
||||
{
|
||||
const std::vector<unit_race::GENDER>& genders = type.genders();
|
||||
assert( genders.size() > 0 );
|
||||
|
@ -93,7 +94,7 @@ static unit_race::GENDER generate_gender(const unit_type & type, bool random_gen
|
|||
if ( random_gender == false || genders.size() == 1 ) {
|
||||
return genders.front();
|
||||
} else {
|
||||
int random = rng ? rng->get_next_random() : get_random_nocheck();
|
||||
int random = random_new::generator->next_random();
|
||||
return genders[random % genders.size()];
|
||||
// Note: genders is guaranteed to be non-empty, so this is not a
|
||||
// potential division by zero.
|
||||
|
@ -103,13 +104,13 @@ static unit_race::GENDER generate_gender(const unit_type & type, bool random_gen
|
|||
}
|
||||
}
|
||||
|
||||
static unit_race::GENDER generate_gender(const unit_type & u_type, const config &cfg, rand_rng::simple_rng* rng)
|
||||
static unit_race::GENDER generate_gender(const unit_type & u_type, const config &cfg)
|
||||
{
|
||||
const std::string& gender = cfg["gender"];
|
||||
if(!gender.empty())
|
||||
return string_gender(gender);
|
||||
|
||||
return generate_gender(u_type, cfg["random_gender"].to_bool(), rng);
|
||||
return generate_gender(u_type, cfg["random_gender"].to_bool());
|
||||
}
|
||||
|
||||
const std::string& unit::leader_crown()
|
||||
|
@ -233,7 +234,7 @@ unit::unit(const config &cfg, bool use_traits, const vconfig* vcfg) :
|
|||
image_mods_(),
|
||||
unrenamable_(false),
|
||||
side_(0),
|
||||
gender_(generate_gender(*type_, cfg, &resources::gamedata->rng())),
|
||||
gender_(generate_gender(*type_, cfg)),
|
||||
alpha_(),
|
||||
unit_formula_(),
|
||||
unit_loop_formula_(),
|
||||
|
@ -483,7 +484,7 @@ unit::unit(const config &cfg, bool use_traits, const vconfig* vcfg) :
|
|||
alignment_ = unit_type::NEUTRAL;
|
||||
}
|
||||
|
||||
generate_name(resources::gamedata ? &(resources::gamedata->rng()) : 0);
|
||||
generate_name();
|
||||
|
||||
// Make the default upkeep "full"
|
||||
if(cfg_["upkeep"].empty()) {
|
||||
|
@ -569,7 +570,7 @@ unit::unit(const unit_type &u_type, int side, bool real_unit,
|
|||
unrenamable_(false),
|
||||
side_(side),
|
||||
gender_(gender != unit_race::NUM_GENDERS ?
|
||||
gender : generate_gender(u_type, real_unit, resources::gamedata ? &(resources::gamedata->rng()) : NULL)),
|
||||
gender : generate_gender(u_type, real_unit)),
|
||||
alpha_(),
|
||||
unit_formula_(),
|
||||
unit_loop_formula_(),
|
||||
|
@ -622,7 +623,7 @@ unit::unit(const unit_type &u_type, int side, bool real_unit,
|
|||
advance_to(u_type, real_unit);
|
||||
|
||||
if(real_unit) {
|
||||
generate_name(resources::gamedata ? &(resources::gamedata->rng()) : NULL);
|
||||
generate_name();
|
||||
}
|
||||
set_underlying_id();
|
||||
|
||||
|
@ -669,11 +670,11 @@ unit& unit::operator=(const unit& u)
|
|||
}
|
||||
|
||||
|
||||
void unit::generate_name(rand_rng::simple_rng* rng)
|
||||
void unit::generate_name()
|
||||
{
|
||||
if (!name_.empty() || !cfg_["generate_name"].to_bool(true)) return;
|
||||
|
||||
name_ = race_->generate_name(gender_, rng);
|
||||
name_ = race_->generate_name(gender_);
|
||||
cfg_["generate_name"] = false;
|
||||
}
|
||||
|
||||
|
@ -737,8 +738,7 @@ void unit::generate_traits(bool musthaveonly)
|
|||
int max_traits = u_type.num_traits();
|
||||
for (; nb_traits < max_traits && !candidate_traits.empty(); ++nb_traits)
|
||||
{
|
||||
int num = (resources::gamedata ? resources::gamedata->rng().get_next_random() : get_random_nocheck())
|
||||
% candidate_traits.size();
|
||||
int num = random_new::generator->next_random() % candidate_traits.size();
|
||||
modifications_.add_child("trait", candidate_traits[num]);
|
||||
candidate_traits.erase(candidate_traits.begin() + num);
|
||||
}
|
||||
|
|
|
@ -384,7 +384,7 @@ public:
|
|||
void backup_state();
|
||||
void apply_modifications();
|
||||
void generate_traits(bool musthaveonly=false);
|
||||
void generate_name(rand_rng::simple_rng *rng = 0);
|
||||
void generate_name();
|
||||
|
||||
// Only see_all=true use caching
|
||||
bool invisible(const map_location& loc, bool see_all=true) const;
|
||||
|
|
Loading…
Add table
Reference in a new issue