Continue to clean unit constructor:

Better control when we do random generation of traits, name and gender
(to avoid OoS)

Activate them for debug-created units.
This commit is contained in:
Ali El Gariani 2009-10-09 01:46:27 +00:00
parent 71a84f6823
commit da6dfa3b2b
4 changed files with 21 additions and 15 deletions

View file

@ -1433,7 +1433,9 @@ void ai_default_recruitment_stage::analyze_potential_recruit_movements()
continue;
}
const unit temp_unit(&get_info().units, &info->second, get_side());
//create unit without traits (no random calls)
//note that it make the AI never rely on 'quick'
const unit temp_unit(&get_info().units, &info->second, get_side(), false);
// since we now use the ignore_units switch, no need to use a empty unit_map
// unit_map units;
// const temporary_unit_placer placer(units,start,temp_unit);

View file

@ -1382,9 +1382,11 @@ void menu_handler::create_unit_2(mouse_handler& mousehandler)
gender = ut.genders().front();
}
unit chosen(&units_, &ut, 1, false, gender, "");
if(generate_name)
chosen.generate_name();
unit chosen(&units_, &ut, 1, true, gender, "");
//FIXME: the generate name option seems useless now, remove it
if(!generate_name)
chosen.set_name("");
chosen.new_turn();
@ -1476,7 +1478,7 @@ void menu_handler::create_unit(mouse_handler& mousehandler)
const unit_race::GENDER gender = random_gender ? unit_race::NUM_GENDERS : unit_race::MALE;
unit chosen(&units_, unit_choices[choice], 1, false, gender, "");
unit chosen(&units_, unit_choices[choice], 1, true, gender);
chosen.new_turn();
const map_location& loc = mousehandler.get_last_hex();
@ -3242,7 +3244,7 @@ void console_handler::do_create() {
menu_handler_.units_.erase(loc);
menu_handler_.units_.add(loc,
unit(&menu_handler_.units_, &i->second, 1, false));
unit(&menu_handler_.units_, &i->second, 1, true));
menu_handler_.gui_->invalidate(loc);
menu_handler_.gui_->invalidate_unit();
} else {

View file

@ -353,7 +353,7 @@ unit_race::GENDER unit::generate_gender(const unit_type& type, bool gen, game_st
}
unit::unit(unit_map *unitmap, const unit_type *t, int side,
bool use_traits, unit_race::GENDER gender, std::string variation) :
bool real_unit, unit_race::GENDER gender, std::string variation) :
cfg_(),
loc_(),
advances_to_(),
@ -375,7 +375,7 @@ unit::unit(unit_map *unitmap, const unit_type *t, int side,
image_mods_(),
unrenamable_(false),
side_(side),
gender_(gender != unit_race::NUM_GENDERS ? gender : generate_gender(*t,true)),
gender_(gender != unit_race::NUM_GENDERS ? gender : generate_gender(*t,real_unit)),
alpha_(),
unit_formula_(),
unit_loop_formula_(),
@ -425,13 +425,11 @@ unit::unit(unit_map *unitmap, const unit_type *t, int side,
cfg_["upkeep"]="full";
advance_to(t);
if(use_traits) {
// Units that don't have traits generated are just
// generic units, so they shouldn't get a description
// either.
if(real_unit) {
generate_name();
}
generate_traits(!use_traits);
// if not a real unit give only the 'musthave' traits
generate_traits(!real_unit);
reset_modifications();
apply_modifications();
set_underlying_id();

View file

@ -66,8 +66,12 @@ public:
unit(const config& cfg, bool use_traits=false);
unit(unit_map* unitmap,
const config& cfg, bool use_traits=false, game_state* state = 0);
/** Initializes a unit from a unit type */
unit(unit_map* unitmap, const unit_type* t, int side, bool use_traits=false, unit_race::GENDER gender=unit_race::NUM_GENDERS, std::string variation="");
/**
* Initializes a unit from a unit type
* only real_unit may have random traits, name and gender
* (to prevent OOS caused by RNG calls)
*/
unit(unit_map* unitmap, const unit_type* t, int side, bool real_unit, unit_race::GENDER gender=unit_race::NUM_GENDERS, std::string variation="");
virtual ~unit();
unit& operator=(const unit&);