Skip to content

Commit

Permalink
Make self destruct use real item
Browse files Browse the repository at this point in the history
  • Loading branch information
Yankes committed Oct 20, 2023
1 parent d13c4be commit b6c1eb0
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 31 deletions.
28 changes: 8 additions & 20 deletions src/Battlescape/ExplosionBState.cpp
Expand Up @@ -117,7 +117,14 @@ void ExplosionBState::init()
}

//testing if we hit target
if (type == BT_PSIAMP && !_hit)
if (action == BA_SELF_DESTRUCT)
{
if (!RNG::percent(itemRule->getSpecialChance()))
{
_power = 0;
}
}
else if (type == BT_PSIAMP && !_hit)
{
if (action != BA_USE)
{
Expand Down Expand Up @@ -177,25 +184,6 @@ void ExplosionBState::init()
_radius = _power /10;
_areaOfEffect = true;
}
else if (_attack.type == BA_SELF_DESTRUCT)
{
_areaOfEffect = true;
if (_attack.attacker)
{
itemRule = _attack.attacker->getArmor()->getSelfDestructItem();
_power = itemRule->getPowerBonus(_attack);
_damageType = itemRule->getDamageType();
_radius = itemRule->getExplosionRadius(_attack);
if (!RNG::percent(itemRule->getSpecialChance()))
{
_power = 0;
}
}
else
{
_power = 0;
}
}
else
{
_power = 120;
Expand Down
2 changes: 1 addition & 1 deletion src/Mod/Armor.h
Expand Up @@ -209,7 +209,7 @@ class Armor
/// Gets the Battlescape corpse item.
const std::vector<const RuleItem*> &getCorpseBattlescape() const;
/// Gets the Geoscape corpse item.
const RuleItem* getSelfDestructItem() const { return _selfDestructItem ? _selfDestructItem : _corpseGeo; }
const RuleItem* getSelfDestructItem() const { return _selfDestructItem ? _selfDestructItem : _corpseGeo && _corpseGeo->getPower() > 0 ? _corpseGeo : nullptr; }
/// Gets the stores item.
const RuleItem* getStoreItem() const;
/// Gets the special weapon type.
Expand Down
24 changes: 15 additions & 9 deletions src/Savegame/BattleUnit.cpp
Expand Up @@ -1874,12 +1874,13 @@ int BattleUnit::damage(Position relative, int damage, const RuleDamageType *type
}
}

auto* selfDestructItem = getSpecialWeapon(getArmor()->getSelfDestructItem());
if (rand.percent(std::get<arg_selfDestructChance>(args.data))
&& !hasAlreadyExploded())
&& !hasAlreadyExploded() && selfDestructItem)
{
setAlreadyExploded(true);
Position p = getPosition().toVoxel();
save->getBattleGame()->statePushNext(new ExplosionBState(save->getBattleGame(), p, BattleActionAttack{ BA_SELF_DESTRUCT, this, }, 0));
save->getBattleGame()->statePushNext(new ExplosionBState(save->getBattleGame(), p, BattleActionAttack{ BA_SELF_DESTRUCT, this, selfDestructItem, selfDestructItem }, 0));
}
}

Expand Down Expand Up @@ -4971,13 +4972,6 @@ void BattleUnit::setSpecialWeapon(SavedBattleGame *save, bool updateFromSave)
const Mod *mod = save->getMod();
int i = 0;

if (_specWeapon[0] && updateFromSave)
{
// new saves already contain special built-in weapons, we can stop here
return;
// old saves still need the below functionality to work properly
}

auto addItem = [&](const RuleItem *item)
{
if (item && i < SPEC_WEAPON_MAX)
Expand Down Expand Up @@ -5006,6 +5000,16 @@ void BattleUnit::setSpecialWeapon(SavedBattleGame *save, bool updateFromSave)
}
};

if (_specWeapon[0] && updateFromSave)
{
// for backward compatibility, we try add corpse explosion
addItem(getArmor()->getSelfDestructItem());

// new saves already contain special built-in weapons, we can stop here
return;
// old saves still need the below functionality to work properly
}

if (getUnitRules())
{
addItem(mod->getItem(getUnitRules()->getMeleeWeapon()));
Expand All @@ -5021,6 +5025,8 @@ void BattleUnit::setSpecialWeapon(SavedBattleGame *save, bool updateFromSave)
{
addItem(getGeoscapeSoldier()->getRules()->getSpecialWeapon());
}

addItem(getArmor()->getSelfDestructItem());
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Savegame/BattleUnit.h
Expand Up @@ -73,7 +73,7 @@ class BattleUnitVisibility
class BattleUnit
{
private:
static const int SPEC_WEAPON_MAX = 3;
static const int SPEC_WEAPON_MAX = 4;

UnitFaction _faction, _originalFaction;
UnitFaction _killedBy;
Expand Down

0 comments on commit b6c1eb0

Please sign in to comment.