Merge the two codepaths for advancing a unit (regular & AMLA).

This commit is contained in:
J. Tyne 2012-08-30 18:28:47 +00:00
parent 23ccb12bf7
commit 4a91a59311
3 changed files with 44 additions and 34 deletions

View File

@ -1277,7 +1277,21 @@ unit get_advanced_unit(const unit &u, const std::string& advance_to)
return new_unit;
}
void advance_unit(map_location loc, const std::string &advance_to, const bool &fire_event)
/**
* Returns the AMLA-advanced version of a unit (with traits and items retained).
*/
unit get_amla_unit(const unit &u, const config &mod_option)
{
unit amla_unit(u);
amla_unit.set_experience(amla_unit.experience() - amla_unit.max_experience());
amla_unit.add_modification("advance", mod_option);
return amla_unit;
}
void advance_unit(map_location loc, const std::string &advance_to,
const bool &fire_event, const config * mod_option)
{
unit_map::unit_iterator u = resources::units->find(loc);
if(!u.valid()) {
@ -1286,31 +1300,37 @@ void advance_unit(map_location loc, const std::string &advance_to, const bool &f
// original_type is not a reference, since the unit may disappear at any moment.
std::string original_type = u->type_id();
// "advance" event.
if(fire_event)
{
LOG_NG << "firing advance event at " << loc <<"\n";
LOG_NG << "Firing advance event at " << loc <<".\n";
game_events::fire("advance",loc);
if (!u.valid() || u->experience() < u->max_experience() ||
u->type_id() != original_type)
{
LOG_NG << "WML has invalidated the advancing unit, abort\n";
LOG_NG << "WML has invalidated the advancing unit. Aborting.\n";
return;
}
}
// Create the advanced unit.
loc = u->get_location();
unit new_unit = get_advanced_unit(*u, advance_to);
statistics::advance_unit(new_unit);
preferences::encountered_units().insert(new_unit.type_id());
LOG_CF << "Added '" << new_unit.type_id() << "' to encountered units\n";
bool use_amla = mod_option != NULL;
unit new_unit = use_amla ? get_amla_unit(*u, *mod_option) :
get_advanced_unit(*u, advance_to);
if ( !use_amla )
{
statistics::advance_unit(new_unit);
preferences::encountered_units().insert(new_unit.type_id());
LOG_CF << "Added '" << new_unit.type_id() << "' to the encountered units.\n";
}
resources::units->replace(loc, new_unit);
// "post_advance" event.
if(fire_event)
{
LOG_NG << "firing post_advance event at " << loc << "\n";
LOG_NG << "Firing post_advance event at " << loc << ".\n";
game_events::fire("post_advance",loc);
}

View File

@ -148,17 +148,24 @@ void attack_unit(const map_location &attacker, const map_location &defender,
int attack_with, int defend_with, bool update_display = true);
/**
* Returns the advanced version of unit (with traits and items retained).
* Returns the advanced version of a unit (with traits and items retained).
*/
unit get_advanced_unit(const unit &u, const std::string &advance_to);
/**
* Returns the AMLA-advanced version of a unit (with traits and items retained).
*/
unit get_amla_unit(const unit &u, const config &mod_option);
/**
* Function which will advance the unit at @a loc to 'advance_to'.
* (Unless mod_option is supplied, in which case an AMLA is performed.)
* Note that 'loc' is not a reference, because if it were a reference,
* we couldn't safely pass in a reference to the item in the map
* that we're going to delete, since deletion would invalidate the reference.
*/
void advance_unit(map_location loc, const std::string &advance_to, const bool &fire_event = true);
void advance_unit(map_location loc, const std::string &advance_to,
const bool &fire_event = true, const config * mod_option = NULL);
/**
* function which tests if the unit at loc is currently affected by leadership.

View File

@ -173,11 +173,11 @@ void advance_unit(const map_location &loc, bool random_choice, bool add_replay_e
// doesn't handle cascading advancement since it just calls animate_unit_advancement().
advance_unit(loc, random_choice, true);
} else {
ERR_CF << "Unit has an too high amount of " << u->experience()
<< " XP left, cascade leveling disabled\n";
ERR_CF << "Unit has too many (" << u->experience()
<< ") XP left; cascade leveling disabled.\n";
}
} else {
ERR_NG << "Unit advanced no longer exists\n";
ERR_NG << "Unit advanced no longer exists.\n";
}
}
@ -218,25 +218,8 @@ bool animate_unit_advancement(const map_location &loc, size_t choice, const bool
std::string chosen_unit = options[choice];
::advance_unit(loc, chosen_unit, fire_event);
} else {
unit amla_unit(*u);
const config &mod_option = mod_options[choice - options.size()];
if(fire_event)
{
LOG_NG << "firing advance event (AMLA)\n";
game_events::fire("advance",loc);
}
amla_unit.set_experience(amla_unit.experience() - amla_unit.max_experience());
amla_unit.add_modification("advance",mod_option);
resources::units->replace(loc, amla_unit);
if(fire_event)
{
LOG_NG << "firing post_advance event (AMLA)\n";
game_events::fire("post_advance",loc);
}
::advance_unit(loc, "", fire_event, &mod_option);
}
u = resources::units->find(loc);