int tod_manager::get_start_ToD(const config &level) const { const config::attribute_value& current_tod = level["current_tod"]; if (!current_tod.blank()) { return calculate_current_time(times_.size(), turn_, current_tod.to_int(0), true); } const int default_result = calculate_current_time(times_.size(), turn_, currentTime_); const config::attribute_value& cfg_random_start_time = level["random_start_time"]; if(!cfg_random_start_time.blank()) { const std::string& random_start_time = cfg_random_start_time.str(); //TODO: //Here there is danger of OOS (bug #15948) //But this randomization is needed on the other hand to make the "random start time" option //in the mp game selection screen work. //process the random_start_time string, which can be boolean yes/no true/false or a //comma-separated string of integers >= 1 referring to the times_ array indices const std::vector<std::string>& random_start_time_strings = utils::split(random_start_time); const int random_index = calculate_current_time(random_start_time_strings.size(), turn_, rand(), true); const int given_current_time = lexical_cast_default<int, std::string>(random_start_time_strings[random_index], 0) - 1; if(given_current_time >= 0) return calculate_current_time(times_.size(), turn_, given_current_time, true); if(cfg_random_start_time.to_bool(false)) return calculate_current_time(times_.size(), turn_, rand(), true); } return default_result; }
void tod_manager::set_new_current_times(const int new_current_turn_number) { currentTime_ = calculate_current_time(times_.size(), new_current_turn_number, currentTime_); BOOST_FOREACH(area_time_of_day& area, areas_) { area.currentTime = calculate_current_time( area.times.size(), new_current_turn_number, area.currentTime); }
void tod_manager::set_new_current_times(const int new_current_turn_number) { set_current_time(calculate_current_time(times_.size(), new_current_turn_number, currentTime_)); for (area_time_of_day& area : areas_) { set_current_time(calculate_current_time( area.times.size(), new_current_turn_number, area.currentTime), area); } }
void tod_manager::resolve_random(random_new::rng& r) { //process the random_start_time string, which can be boolean yes/no true/false or a //comma-separated string of integers >= 1 referring to the times_ array indices std::vector<std::string> output_strings = utils::split(random_tod_.str()); std::vector<int> output; try { std::transform(output_strings.begin(), output_strings.end(), std::back_inserter(output), [](const std::string& str) { return std::stoi(str); }); } catch (std::invalid_argument) { // This happens if the random_start_time string is a boolean. // Simply ignore the exception. } // Remove non-positive times output.erase( std::remove_if( output.begin(), output.end(), [](int time){ return time <= 0; }), output.end()); if(!output.empty()) { int chosen = output[r.next_random() % output.size()]; currentTime_ = calculate_current_time(times_.size(), turn_, chosen, true); r.next_random(); } else if (random_tod_.to_bool(false)) { currentTime_ = calculate_current_time(times_.size(), turn_, r.next_random(), true); } random_tod_ = false; }
void tod_manager::resolve_random(random_new::rng& r) { //process the random_start_time string, which can be boolean yes/no true/false or a //comma-separated string of integers >= 1 referring to the times_ array indices std::vector<int> output; boost::copy( utils::split(random_tod_.str()) | boost::adaptors::transformed(boost::bind(lexical_cast_default<int, std::string>, _1 , 0)) | boost::adaptors::filtered(greater<int>(0)) , std::back_inserter(output) ); if(!output.empty()) { int chosen = output[r.next_random() % output.size()]; currentTime_ = calculate_current_time(times_.size(), turn_, chosen, true); r.next_random(); } else if (random_tod_.to_bool(false)) { currentTime_ = calculate_current_time(times_.size(), turn_, r.next_random(), true); } random_tod_ = false; }
tod_manager::tod_manager(const config& scenario_cfg): savegame_config(), currentTime_(0), times_(), areas_(), turn_(scenario_cfg["turn_at"].to_int(1)), num_turns_(scenario_cfg["turns"].to_int(-1)) { // ? : operator doesn't work in this case. if (scenario_cfg["current_time"].to_int(-17403) == -17403) random_tod_ = scenario_cfg["random_start_time"]; else random_tod_ = false; time_of_day::parse_times(scenario_cfg,times_); //We need to call parse_times before calculate_current_time because otherwise the first parameter will always be 0. currentTime_ = calculate_current_time(times_.size(), turn_, scenario_cfg["current_time"].to_int(0), true); }
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]; }