Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
kphoenix137 committed Aug 24, 2024
1 parent 9cadba7 commit 3b6d83d
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 78 deletions.
5 changes: 2 additions & 3 deletions Source/misdat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ tl::expected<MissileData::AddFn, std::string> ParseMissileAddFn(std::string_view
if (value == "AddHealOther") return AddHealOther;
if (value == "AddElemental") return AddElemental;
if (value == "AddIdentify") return AddIdentify;
if (value == "AddFireWallControl") return AddFireWallControl;
if (value == "AddWallControl") return AddWallControl;
if (value == "AddInfravision") return AddInfravision;
if (value == "AddFlameWaveControl") return AddFlameWaveControl;
if (value == "AddNova") return AddNova;
Expand Down Expand Up @@ -265,7 +265,6 @@ tl::expected<MissileData::ProcessFn, std::string> ParseMissileProcessFn(std::str
if (value == "ProcessLightningBow") return ProcessLightningBow;
if (value == "ProcessRingOfFire") return ProcessRingOfFire;
if (value == "ProcessSearch") return ProcessSearch;
if (value == "ProcessLightningWallControl") return ProcessLightningWallControl;
if (value == "ProcessImmolation") return ProcessImmolation;
if (value == "ProcessSpectralArrow") return ProcessSpectralArrow;
if (value == "ProcessLightningControl") return ProcessLightningControl;
Expand All @@ -283,7 +282,7 @@ tl::expected<MissileData::ProcessFn, std::string> ParseMissileProcessFn(std::str
if (value == "ProcessStoneCurse") return ProcessStoneCurse;
if (value == "ProcessApocalypseBoom") return ProcessApocalypseBoom;
if (value == "ProcessRhino") return ProcessRhino;
if (value == "ProcessFireWallControl") return ProcessFireWallControl;
if (value == "ProcessWallControl") return ProcessWallControl;
if (value == "ProcessInfravision") return ProcessInfravision;
if (value == "ProcessApocalypse") return ProcessApocalypse;
if (value == "ProcessFlameWaveControl") return ProcessFlameWaveControl;
Expand Down
129 changes: 59 additions & 70 deletions Source/missiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,7 @@ bool GuardianTryFireAt(Missile &missile, Point target)
return true;
}

bool GrowWall(int id, Point position, Point target, MissileID type, int spellLevel, int damage, bool skip = false)
bool GrowWall(int id, Point position, Point target, MissileID type, int spellLevel, int damage, bool skipTile = false)
{
[[maybe_unused]] int dp = dPiece[position.x][position.y];
assert(dp <= MAXTILES && dp >= 0);
Expand All @@ -699,7 +699,7 @@ bool GrowWall(int id, Point position, Point target, MissileID type, int spellLev
return false;
}

if (!skip)
if (!skipTile)
AddMissile(position, position, Direction::South, type, TARGET_BOTH, id, damage, spellLevel);
return true;
}
Expand Down Expand Up @@ -2434,7 +2434,7 @@ void AddIdentify(Missile &missile, AddMissileParameter & /*parameter*/)
}
}

void AddFireWallControl(Missile &missile, AddMissileParameter &parameter)
void AddWallControl(Missile &missile, AddMissileParameter &parameter)
{
std::optional<Point> spreadPosition = FindClosestValidPosition(
[start = missile.position.start](Point target) {
Expand All @@ -2456,6 +2456,7 @@ void AddFireWallControl(Missile &missile, AddMissileParameter &parameter)
missile.var3 = static_cast<int>(Left(Left(parameter.midir)));
missile.var4 = static_cast<int>(Right(Right(parameter.midir)));
missile._mirange = 7;
missile._mitype = missile._miAnimType == MissileGraphicID::FireWall ? MissileID::FireWall : MissileID::LightningWall;
}

void AddInfravision(Missile &missile, AddMissileParameter & /*parameter*/)
Expand Down Expand Up @@ -3177,47 +3178,6 @@ void ProcessSearch(Missile &missile)
AutoMapShowItems = false;
}

void ProcessLightningWallControl(Missile &missile)
{
missile._mirange--;
if (missile._mirange == 0) {
missile._miDelFlag = true;
return;
}

int id = missile._misource;
int lvl = !missile.IsTrap() ? Players[id].getCharacterLevel() : 0;
int dmg = 16 * (GenerateRndSum(10, 2) + lvl + 2);

// Defines the current position of the control missile moving towards var3 direction.
// This is used to make sure the opposite side does not create walls where walls have already been created.
const Point currentPosition = { missile.var1, missile.var2 };

{
Point position = { missile.var1, missile.var2 };
Point target = position + static_cast<Direction>(missile.var3);

if (!missile.limitReached && GrowWall(id, position, target, MissileID::LightningWall, missile._mispllvl, dmg)) {
missile.var1 = target.x;
missile.var2 = target.y;
} else {
missile.limitReached = true;
}
}

{
Point position = { missile.var5, missile.var6 };
Point target = position + static_cast<Direction>(missile.var4);

if (missile.var7 == 0 && GrowWall(id, position, target, MissileID::LightningWall, missile._mispllvl, dmg, position == currentPosition)) {
missile.var5 = target.x;
missile.var6 = target.y;
} else {
missile.var7 = 1;
}
}
}

void ProcessNovaCommon(Missile &missile, MissileID projectileType)
{
int id = missile._misource;
Expand Down Expand Up @@ -3729,43 +3689,72 @@ void ProcessRhino(Missile &missile)
PutMissile(missile);
}

void ProcessFireWallControl(Missile &missile)
/*
* @brief Moves the control missile towards the right and grows walls.
* @param missile The control missile.
* @param sourceIdx The source index of the wall missile.
* @param type The missile ID of the wall missile.
* @param dmg The damage of the wall missile.
*/
static void ProcessWallControlLeft(Missile &missile, const int sourceIdx, const MissileID type, const int dmg)
{
const Point leftPosition = { missile.var1, missile.var2 };
const Point target = leftPosition + static_cast<Direction>(missile.var3);

if (!missile.limitReached && GrowWall(sourceIdx, leftPosition, target, type, missile._mispllvl, dmg)) {
missile.var1 = target.x;
missile.var2 = target.y;
} else {
missile.limitReached = true;
}
}

/*
* @brief Moves the control missile towards the right and grows walls.
* @param missile The control missile.
* @param sourceIdx The source index of the wall missile.
* @param type The missile ID of the wall missile.
* @param dmg The damage of the wall missile.
* @param skipTile Should the tile be skipped.
*/
static void ProcessWallControlRight(Missile &missile, const int sourceIdx, const MissileID type, const int dmg, const bool skipTile = false)
{
const Point rightPosition = { missile.var5, missile.var6 };
const Point target = rightPosition + static_cast<Direction>(missile.var4);

if (missile.var7 == 0 && GrowWall(sourceIdx, rightPosition, target, type, missile._mispllvl, dmg, skipTile)) {
missile.var5 = target.x;
missile.var6 = target.y;
} else {
missile.var7 = 1;
}
}

void ProcessWallControl(Missile &missile)
{
missile._mirange--;
if (missile._mirange == 0) {
missile._miDelFlag = true;
return;
}

int id = missile._misource;

// Defines the current position of the control missile moving towards var3 direction.
// This is used to make sure the opposite side does not create walls where walls have already been created.
Point currentPosition = { missile.var1, missile.var2 };
MissileID type = missile._mitype;
type = MissileID::FireWall;
const int sourceIdx = missile._misource;
int lvl = 0;
int dmg = 0;

{
Point position = { missile.var1, missile.var2 };
Point target = position + static_cast<Direction>(missile.var3);

if (!missile.limitReached && GrowWall(id, position, target, MissileID::FireWall, missile._mispllvl, 0)) {
missile.var1 = target.x;
missile.var2 = target.y;
} else {
missile.limitReached = true;
}
if (type == MissileID::LightningWall) {
lvl = !missile.IsTrap() ? Players[sourceIdx].getCharacterLevel() : 0;
dmg = 16 * (GenerateRndSum(10, 2) + lvl + 2);
}

{
Point position = { missile.var5, missile.var6 };
Point target = position + static_cast<Direction>(missile.var4);
ProcessWallControlLeft(missile, sourceIdx, type, dmg);

if (missile.var7 == 0 && GrowWall(id, position, target, MissileID::FireWall, missile._mispllvl, 0, position == currentPosition)) {
missile.var5 = target.x;
missile.var6 = target.y;
} else {
missile.var7 = 1;
}
}
Point leftPosition = { missile.var1, missile.var2 };
Point rightPosition = { missile.var3, missile.var4 };

ProcessWallControlRight(missile, sourceIdx, type, dmg, leftPosition == rightPosition);
}

void ProcessInfravision(Missile &missile)
Expand Down
5 changes: 2 additions & 3 deletions Source/missiles.h
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ void AddIdentify(Missile &missile, AddMissileParameter &parameter);
* var5: X coordinate of the second wave
* var6: Y coordinate of the second wave
*/
void AddFireWallControl(Missile &missile, AddMissileParameter &parameter);
void AddWallControl(Missile &missile, AddMissileParameter &parameter);
void AddInfravision(Missile &missile, AddMissileParameter &parameter);

/**
Expand Down Expand Up @@ -421,7 +421,6 @@ void ProcessBigExplosion(Missile &missile);
void ProcessLightningBow(Missile &missile);
void ProcessRingOfFire(Missile &missile);
void ProcessSearch(Missile &missile);
void ProcessLightningWallControl(Missile &missile);
void ProcessImmolation(Missile &missile);
void ProcessSpectralArrow(Missile &missile);
void ProcessLightningControl(Missile &missile);
Expand All @@ -439,7 +438,7 @@ void ProcessTeleport(Missile &missile);
void ProcessStoneCurse(Missile &missile);
void ProcessApocalypseBoom(Missile &missile);
void ProcessRhino(Missile &missile);
void ProcessFireWallControl(Missile &missile);
void ProcessWallControl(Missile &missile);
void ProcessInfravision(Missile &missile);
void ProcessApocalypse(Missile &missile);
void ProcessFlameWaveControl(Missile &missile);
Expand Down
4 changes: 2 additions & 2 deletions assets/txtdata/missiles/misdat.tsv
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Etherealize SpellEtherealize Etherealize Physical
Spurt Spurt Physical
ApocalypseBoom AddApocalypseBoom ProcessApocalypseBoom ApocalypseBoom Physical
Healing AddHealing Physical,Invisible
FireWallControl AddFireWallControl ProcessFireWallControl FireWall Fire,Invisible
FireWallControl AddWallControl ProcessWallControl FireWall Fire,Invisible
Infravision AddInfravision ProcessInfravision SpellInfravision Physical,Invisible
Identify AddIdentify Physical,Invisible
FlameWaveControl AddFlameWaveControl ProcessFlameWaveControl SpellFlameWave FireWall Fire
Expand Down Expand Up @@ -70,7 +70,7 @@ DiabloApocalypse AddDiabloApocalypse Physical,Invisible
Mana AddMana Physical,Invisible
Magi AddMagi Physical,Invisible
LightningWall AddLightningWall ProcessLightningWall SpellLightningWall SpellLightningHit Lightning Lightning
LightningWallControl AddFireWallControl ProcessLightningWallControl Lightning Lightning,Invisible
LightningWallControl AddWallControl ProcessWallControl Lightning Lightning,Invisible
Immolation AddNova ProcessImmolation SpellFirebolt SpellFireHit Fireball Fire
SpectralArrow AddSpectralArrow ProcessSpectralArrow Arrow Physical,Arrow
FireballBow AddImmolation ProcessFireball ShootFireballBow SpellFireHit Fireball Fire Blockable
Expand Down

0 comments on commit 3b6d83d

Please sign in to comment.