Skip to content

Commit

Permalink
Add end mission script
Browse files Browse the repository at this point in the history
  • Loading branch information
Yankes committed Aug 16, 2018
1 parent 632ede5 commit 22e1f34
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/Battlescape/DebriefingState.cpp
Expand Up @@ -1216,7 +1216,7 @@ void DebriefingState::prepareDebriefing()
if ((((*j)->isInExitArea(START_POINT) || (*j)->getStatus() == STATUS_IGNORE_ME) && (battle->getMissionType() != "STR_BASE_DEFENSE" || success)) || !aborted || (aborted && (*j)->isInExitArea(END_POINT)))
{ // so game is not aborted or aborted and unit is on exit area
UnitStats statIncrease;
(*j)->postMissionProcedures(save, statIncrease);
(*j)->postMissionProcedures(save, battle, statIncrease);
if ((*j)->getGeoscapeSoldier())
_soldierStats.push_back(std::pair<std::wstring, UnitStats>((*j)->getGeoscapeSoldier()->getName(), statIncrease));
playersInExitArea++;
Expand Down
6 changes: 6 additions & 0 deletions src/Mod/ModScript.h
Expand Up @@ -117,6 +117,10 @@ class ModScript
{
NewTurnUnitParser(ScriptGlobal* shared, const std::string& name, Mod* mod);
};
struct ReturnFromMissionUnitParser : ScriptParserEvents<ScriptOutputArgs<int&, int>, BattleUnit*, SavedBattleGame*, Soldier*>
{
ReturnFromMissionUnitParser(ScriptGlobal* shared, const std::string& name, Mod* mod);
};

struct CreateItemParser : ScriptParserEvents<ScriptOutputArgs<>, BattleItem*, SavedBattleGame*, int>
{
Expand Down Expand Up @@ -155,6 +159,7 @@ class ModScript
using DamageUnit = MACRO_NAMED_SCRIPT("damageUnit", DamageUnitParser);
using CreateUnit = MACRO_NAMED_SCRIPT("createUnit", CreateUnitParser);
using NewTurnUnit = MACRO_NAMED_SCRIPT("newTurnUnit", NewTurnUnitParser);
using ReturnFromMissionUnit = MACRO_NAMED_SCRIPT("returnFromMissionUnit", ReturnFromMissionUnitParser);

using AwardExperience = MACRO_NAMED_SCRIPT("awardExperience", AwardExperienceParser);

Expand Down Expand Up @@ -187,6 +192,7 @@ class ModScript
DamageUnit,
CreateUnit,
NewTurnUnit,
ReturnFromMissionUnit,

AwardExperience,

Expand Down
32 changes: 28 additions & 4 deletions src/Savegame/BattleUnit.cpp
Expand Up @@ -2711,7 +2711,7 @@ void BattleUnit::updateGeoscapeStats(Soldier *soldier) const
* @param statsDiff (out) The passed UnitStats struct will be filled with the stats differences.
* @return True if the soldier was eligible for squaddie promotion.
*/
bool BattleUnit::postMissionProcedures(SavedGame *geoscape, UnitStats &statsDiff)
bool BattleUnit::postMissionProcedures(SavedGame *geoscape, SavedBattleGame *battle, UnitStats &statsDiff)
{
Soldier *s = geoscape->getSoldier(_id);
if (s == 0)
Expand All @@ -2726,11 +2726,18 @@ bool BattleUnit::postMissionProcedures(SavedGame *geoscape, UnitStats &statsDiff
const UnitStats caps = s->getRules()->getStatCaps();
int healthLoss = _stats.health - _health;

if (healthLoss < 0)
auto recovery = RNG::generate((healthLoss*0.5),(healthLoss*1.5));

{
healthLoss = 0;
ModScript::ReturnFromMissionUnit::Output arg{ recovery, healthLoss };
ModScript::ReturnFromMissionUnit::Worker work{ this, battle, s };

work.execute(getArmor()->getScript<ModScript::ReturnFromMissionUnit>(), arg);

recovery = arg.getFirst();
}
s->setWoundRecovery(RNG::generate((healthLoss*0.5),(healthLoss*1.5)));

s->setWoundRecovery(recovery);

if (_expBravery && stats->bravery < caps.bravery)
{
Expand Down Expand Up @@ -2780,6 +2787,11 @@ bool BattleUnit::postMissionProcedures(SavedGame *geoscape, UnitStats &statsDiff

statsDiff += *stats; // add new stat

if (s->getWoundRecovery() > 0)
{
s->setCraft(nullptr);
}

return hasImproved;
}

Expand Down Expand Up @@ -4581,6 +4593,18 @@ ModScript::NewTurnUnitParser::NewTurnUnitParser(ScriptGlobal* shared, const std:
b.addCustomPtr<const Mod>("rules", mod);
}

ModScript::ReturnFromMissionUnitParser::ReturnFromMissionUnitParser(ScriptGlobal* shared, const std::string& name, Mod* mod) : ScriptParserEvents{ shared, name,
"recovery_time",
"health_loss",
"unit", "battle_game", "soldier", }
{
BindBase b { this };

b.addCustomPtr<const Mod>("rules", mod);

setEmptyReturn();
}

ModScript::AwardExperienceParser::AwardExperienceParser(ScriptGlobal* shared, const std::string& name, Mod* mod) : ScriptParserEvents{ shared, name, "experience_multipler", "experience_type", "attacker", "unit", "weapon", }
{
BindBase b { this };
Expand Down
2 changes: 1 addition & 1 deletion src/Savegame/BattleUnit.h
Expand Up @@ -390,7 +390,7 @@ class BattleUnit
/// Updates the stats of a Geoscape soldier.
void updateGeoscapeStats(Soldier *soldier) const;
/// Check if unit eligible for squaddie promotion.
bool postMissionProcedures(SavedGame *geoscape, UnitStats &statsDiff);
bool postMissionProcedures(SavedGame *geoscape, SavedBattleGame *battle, UnitStats &statsDiff);
/// Get the sprite index for the minimap
int getMiniMapSpriteIndex() const;
/// Set the turret type. -1 is no turret.
Expand Down
8 changes: 1 addition & 7 deletions src/Savegame/Soldier.cpp
Expand Up @@ -573,13 +573,7 @@ int Soldier::getWoundRecovery() const
*/
void Soldier::setWoundRecovery(int recovery)
{
_recovery = recovery;

// dismiss from craft
if (_recovery > 0)
{
_craft = 0;
}
_recovery = std::max(recovery, 0);
}

/**
Expand Down

0 comments on commit 22e1f34

Please sign in to comment.