From 0279cbffa2a18a2af8a544db6947c6261464b61e Mon Sep 17 00:00:00 2001
From: Tim Felgentreff <timfelgentreff@gmail.com>
Date: Sat, 3 Nov 2018 13:16:13 +0000
Subject: [PATCH] pass reference to spell target, so that summon can set the
 target and subsequent spell actions can access it

---
 src/include/spell/spell_adjustvariable.h  |  2 +-
 src/include/spell/spell_adjustvital.h     |  2 +-
 src/include/spell/spell_areaadjustvital.h |  2 +-
 src/include/spell/spell_areabombardment.h |  2 +-
 src/include/spell/spell_capture.h         |  2 +-
 src/include/spell/spell_demolish.h        |  2 +-
 src/include/spell/spell_luacallback.h     |  2 +-
 src/include/spell/spell_polymorph.h       |  2 +-
 src/include/spell/spell_spawnmissile.h    |  2 +-
 src/include/spell/spell_spawnportal.h     |  2 +-
 src/include/spell/spell_summon.h          |  2 +-
 src/include/spell/spell_teleport.h        |  2 +-
 src/include/spells.h                      |  2 +-
 src/spell/spell_adjustvariable.cpp        |  2 +-
 src/spell/spell_adjustvital.cpp           |  2 +-
 src/spell/spell_areaadjustvital.cpp       | 21 +++++++++++----------
 src/spell/spell_areabombardment.cpp       |  2 +-
 src/spell/spell_capture.cpp               |  2 +-
 src/spell/spell_demolish.cpp              |  2 +-
 src/spell/spell_luacallback.cpp           |  2 +-
 src/spell/spell_polymorph.cpp             |  2 +-
 src/spell/spell_spawnmissile.cpp          |  2 +-
 src/spell/spell_spawnportal.cpp           |  2 +-
 src/spell/spell_summon.cpp                |  2 +-
 src/spell/spell_teleport.cpp              |  2 +-
 25 files changed, 35 insertions(+), 34 deletions(-)

diff --git a/src/include/spell/spell_adjustvariable.h b/src/include/spell/spell_adjustvariable.h
index d952e1c24..2e1233547 100644
--- a/src/include/spell/spell_adjustvariable.h
+++ b/src/include/spell/spell_adjustvariable.h
@@ -71,7 +71,7 @@ public:
 	Spell_AdjustVariable() : Var(NULL) {};
 	~Spell_AdjustVariable() { delete [](this->Var); };
 	virtual int Cast(CUnit &caster, const SpellType &spell,
-					 CUnit *target, const Vec2i &goalPos);
+					 CUnit *&target, const Vec2i &goalPos);
 	virtual void Parse(lua_State *l, int startIndex, int endIndex);
 
 private:
diff --git a/src/include/spell/spell_adjustvital.h b/src/include/spell/spell_adjustvital.h
index 461ef172c..4e2df8833 100644
--- a/src/include/spell/spell_adjustvital.h
+++ b/src/include/spell/spell_adjustvital.h
@@ -43,7 +43,7 @@ class Spell_AdjustVital : public SpellActionType
 public:
 	Spell_AdjustVital() : SpellActionType(1), HP(0), Mana(0), Shield(0), MaxMultiCast(0) {};
 	virtual int Cast(CUnit &caster, const SpellType &spell,
-					 CUnit *target, const Vec2i &goalPos);
+					 CUnit *&target, const Vec2i &goalPos);
 	virtual void Parse(lua_State *l, int startIndex, int endIndex);
 
 private:
diff --git a/src/include/spell/spell_areaadjustvital.h b/src/include/spell/spell_areaadjustvital.h
index 53144166e..34da89447 100644
--- a/src/include/spell/spell_areaadjustvital.h
+++ b/src/include/spell/spell_areaadjustvital.h
@@ -43,7 +43,7 @@ class Spell_AreaAdjustVital : public SpellActionType
 public:
 	Spell_AreaAdjustVital() : HP(0), Mana(0), Shield(0), Range(1), UseMana(false) {};
 	virtual int Cast(CUnit &caster, const SpellType &spell,
-					 CUnit *target, const Vec2i &goalPos);
+					 CUnit *&target, const Vec2i &goalPos);
 	virtual void Parse(lua_State *l, int startIndex, int endIndex);
 
 private:
diff --git a/src/include/spell/spell_areabombardment.h b/src/include/spell/spell_areabombardment.h
index a681ab20d..e157cb2be 100644
--- a/src/include/spell/spell_areabombardment.h
+++ b/src/include/spell/spell_areabombardment.h
@@ -44,7 +44,7 @@ public:
 	Spell_AreaBombardment() : Fields(0), Shards(0), Damage(0),
 		StartOffsetX(0), StartOffsetY(0), Missile(NULL) {};
 	virtual int Cast(CUnit &caster, const SpellType &spell,
-					 CUnit *target, const Vec2i &goalPos);
+					 CUnit *&target, const Vec2i &goalPos);
 	virtual void Parse(lua_State *l, int startIndex, int endIndex);
 
 private:
diff --git a/src/include/spell/spell_capture.h b/src/include/spell/spell_capture.h
index 477c33f00..dcfa5a19f 100644
--- a/src/include/spell/spell_capture.h
+++ b/src/include/spell/spell_capture.h
@@ -43,7 +43,7 @@ class Spell_Capture : public SpellActionType
 public:
 	Spell_Capture() : SacrificeEnable(false), JoinToAIForce(false), Damage(0), DamagePercent(0) {};
 	virtual int Cast(CUnit &caster, const SpellType &spell,
-					 CUnit *target, const Vec2i &goalPos);
+					 CUnit *&target, const Vec2i &goalPos);
 	virtual void Parse(lua_State *l, int startIndex, int endIndex);
 
 private:
diff --git a/src/include/spell/spell_demolish.h b/src/include/spell/spell_demolish.h
index d41ac6a58..48dbcb202 100644
--- a/src/include/spell/spell_demolish.h
+++ b/src/include/spell/spell_demolish.h
@@ -43,7 +43,7 @@ class Spell_Demolish : public SpellActionType
 public:
 	Spell_Demolish() : Damage(0), Range(0) {};
 	virtual int Cast(CUnit &caster, const SpellType &spell,
-					 CUnit *target, const Vec2i &goalPos);
+					 CUnit *&target, const Vec2i &goalPos);
 	virtual void Parse(lua_State *l, int startIndex, int endIndex);
 
 private:
diff --git a/src/include/spell/spell_luacallback.h b/src/include/spell/spell_luacallback.h
index 8d4d98775..2253596f2 100644
--- a/src/include/spell/spell_luacallback.h
+++ b/src/include/spell/spell_luacallback.h
@@ -45,7 +45,7 @@ public:
 	Spell_LuaCallback() : Func(NULL) {};
 	~Spell_LuaCallback() { delete Func; };
 	virtual int Cast(CUnit &caster, const SpellType &spell,
-					 CUnit *target, const Vec2i &goalPos);
+					 CUnit *&target, const Vec2i &goalPos);
 	virtual void Parse(lua_State *l, int startIndex, int endIndex);
 
 private:
diff --git a/src/include/spell/spell_polymorph.h b/src/include/spell/spell_polymorph.h
index 769afd7b3..cc7ec1102 100644
--- a/src/include/spell/spell_polymorph.h
+++ b/src/include/spell/spell_polymorph.h
@@ -43,7 +43,7 @@ class Spell_Polymorph : public SpellActionType
 public:
 	Spell_Polymorph() : SpellActionType(1), NewForm(NULL), PlayerNeutral(0) {};
 	virtual int Cast(CUnit &caster, const SpellType &spell,
-					 CUnit *target, const Vec2i &goalPos);
+					 CUnit *&target, const Vec2i &goalPos);
 	virtual void Parse(lua_State *l, int startIndex, int endIndex);
 
 private:
diff --git a/src/include/spell/spell_spawnmissile.h b/src/include/spell/spell_spawnmissile.h
index 98815b7c4..b661a2c4f 100644
--- a/src/include/spell/spell_spawnmissile.h
+++ b/src/include/spell/spell_spawnmissile.h
@@ -71,7 +71,7 @@ public:
 	Spell_SpawnMissile() : Damage(0), TTL(-1), Delay(0), UseUnitVar(false),
 		StartPoint(LocBaseCaster), EndPoint(LocBaseTarget), Missile(0) {}
 	virtual int Cast(CUnit &caster, const SpellType &spell,
-					 CUnit *target, const Vec2i &goalPos);
+					 CUnit *&target, const Vec2i &goalPos);
 	virtual void Parse(lua_State *lua, int startIndex, int endIndex);
 
 private:
diff --git a/src/include/spell/spell_spawnportal.h b/src/include/spell/spell_spawnportal.h
index fb354baf1..ada3282c5 100644
--- a/src/include/spell/spell_spawnportal.h
+++ b/src/include/spell/spell_spawnportal.h
@@ -43,7 +43,7 @@ class Spell_SpawnPortal : public SpellActionType
 public:
 	Spell_SpawnPortal() : PortalType(0), TTL(0), CurrentPlayer(false) {};
 	virtual int Cast(CUnit &caster, const SpellType &spell,
-					 CUnit *target, const Vec2i &goalPos);
+					 CUnit *&target, const Vec2i &goalPos);
 	virtual void Parse(lua_State *l, int startIndex, int endIndex);
 
 private:
diff --git a/src/include/spell/spell_summon.h b/src/include/spell/spell_summon.h
index b1e416646..d5bd80525 100644
--- a/src/include/spell/spell_summon.h
+++ b/src/include/spell/spell_summon.h
@@ -44,7 +44,7 @@ public:
 	Spell_Summon() : SpellActionType(1), UnitType(NULL), TTL(0),
 		RequireCorpse(false), JoinToAiForce(false) {};
 	virtual int Cast(CUnit &caster, const SpellType &spell,
-					 CUnit *target, const Vec2i &goalPos);
+					 CUnit *&target, const Vec2i &goalPos);
 	virtual void Parse(lua_State *l, int startIndex, int endIndex);
 
 private:
diff --git a/src/include/spell/spell_teleport.h b/src/include/spell/spell_teleport.h
index 3f15e1881..2bfb84254 100644
--- a/src/include/spell/spell_teleport.h
+++ b/src/include/spell/spell_teleport.h
@@ -42,7 +42,7 @@ class Spell_Teleport : public SpellActionType
 public:
 	Spell_Teleport() : SpellActionType(0) {}
 	virtual int Cast(CUnit &caster, const SpellType &spell,
-					 CUnit *target, const Vec2i &goalPos);
+					 CUnit *&target, const Vec2i &goalPos);
 	virtual void Parse(lua_State *l, int startIndex, int endIndex);
 };
 
diff --git a/src/include/spells.h b/src/include/spells.h
index fe5a1b475..e7724d883 100644
--- a/src/include/spells.h
+++ b/src/include/spells.h
@@ -67,7 +67,7 @@ public:
 	virtual ~SpellActionType() {};
 
 	virtual int Cast(CUnit &caster, const SpellType &spell,
-					 CUnit *target, const Vec2i &goalPos) = 0;
+					 CUnit* &target, const Vec2i &goalPos) = 0;
 	virtual void Parse(lua_State *l, int startIndex, int endIndex) = 0;
 
 	const int ModifyManaCaster;
diff --git a/src/spell/spell_adjustvariable.cpp b/src/spell/spell_adjustvariable.cpp
index 782fb889b..bc6654646 100644
--- a/src/spell/spell_adjustvariable.cpp
+++ b/src/spell/spell_adjustvariable.cpp
@@ -116,7 +116,7 @@
 **
 **  @return        =!0 if spell should be repeated, 0 if not
 */
-/* virtual */ int Spell_AdjustVariable::Cast(CUnit &caster, const SpellType &, CUnit *target, const Vec2i &/*goalPos*/)
+/* virtual */ int Spell_AdjustVariable::Cast(CUnit &caster, const SpellType &, CUnit *&target, const Vec2i &/*goalPos*/)
 {
 	for (unsigned int i = 0; i < UnitTypeVar.GetNumberVariable(); ++i) {
 		CUnit *unit = (this->Var[i].TargetIsCaster) ? &caster : target;
diff --git a/src/spell/spell_adjustvital.cpp b/src/spell/spell_adjustvital.cpp
index ffb4819bd..d1b9af200 100644
--- a/src/spell/spell_adjustvital.cpp
+++ b/src/spell/spell_adjustvital.cpp
@@ -67,7 +67,7 @@
 **
 **  @return             =!0 if spell should be repeated, 0 if not
 */
-/* virtual */ int Spell_AdjustVital::Cast(CUnit &caster, const SpellType &spell, CUnit *target, const Vec2i &/*goalPos*/)
+/* virtual */ int Spell_AdjustVital::Cast(CUnit &caster, const SpellType &spell, CUnit *&target, const Vec2i &/*goalPos*/)
 {
 	if (!target) {
 		return 0;
diff --git a/src/spell/spell_areaadjustvital.cpp b/src/spell/spell_areaadjustvital.cpp
index f44e7b3a6..991eb4306 100644
--- a/src/spell/spell_areaadjustvital.cpp
+++ b/src/spell/spell_areaadjustvital.cpp
@@ -69,7 +69,7 @@
 **
 **  @return             =!0 if spell should be repeated, 0 if not
 */
-/* virtual */ int Spell_AreaAdjustVital::Cast(CUnit &caster, const SpellType &spell, CUnit *target, const Vec2i &goalPos)
+/* virtual */ int Spell_AreaAdjustVital::Cast(CUnit &caster, const SpellType &spell, CUnit *&target, const Vec2i &goalPos)
 {
 	const Vec2i range(this->Range, this->Range);
 	const Vec2i typeSize(caster.Type->TileWidth, caster.Type->TileHeight);
@@ -80,22 +80,23 @@
 	int hp = this->HP;
 	int mana = this->Mana;
 	int shield = this->Shield;
+	CUnit* currentTarget;
 	for (size_t j = 0; j != units.size(); ++j) {
-		target = units[j];
+		currentTarget = units[j];
 		// if (!PassCondition(caster, spell, target, goalPos) {
-		if (!CanCastSpell(caster, spell, target, goalPos)) {
+		if (!CanCastSpell(caster, spell, currentTarget, goalPos)) {
 			continue;
 		}
 		if (hp < 0) {
-			HitUnit(&caster, *target, -hp);
+			HitUnit(&caster, *currentTarget, -hp);
 		} else {
-			target->Variable[HP_INDEX].Value += hp;
-			target->Variable[HP_INDEX].Value = std::min(target->Variable[HP_INDEX].Max, target->Variable[HP_INDEX].Value);
+			currentTarget->Variable[HP_INDEX].Value += hp;
+			currentTarget->Variable[HP_INDEX].Value = std::min(currentTarget->Variable[HP_INDEX].Max, currentTarget->Variable[HP_INDEX].Value);
 		}
-		target->Variable[MANA_INDEX].Value += mana;
-		clamp(&target->Variable[MANA_INDEX].Value, 0, target->Variable[MANA_INDEX].Max);
-		target->Variable[SHIELD_INDEX].Value += shield;
-		clamp(&target->Variable[SHIELD_INDEX].Value, 0, target->Variable[SHIELD_INDEX].Max);
+		currentTarget->Variable[MANA_INDEX].Value += mana;
+		clamp(&currentTarget->Variable[MANA_INDEX].Value, 0, currentTarget->Variable[MANA_INDEX].Max);
+		currentTarget->Variable[SHIELD_INDEX].Value += shield;
+		clamp(&currentTarget->Variable[SHIELD_INDEX].Value, 0, currentTarget->Variable[SHIELD_INDEX].Max);
 	}
 	if (UseMana) {
 		caster.Variable[MANA_INDEX].Value -= spell.ManaCost;
diff --git a/src/spell/spell_areabombardment.cpp b/src/spell/spell_areabombardment.cpp
index ee26c831f..f89fb78fc 100644
--- a/src/spell/spell_areabombardment.cpp
+++ b/src/spell/spell_areabombardment.cpp
@@ -84,7 +84,7 @@
 **  @internal: vladi: blizzard differs than original in this way:
 **   original: launches 50 shards at 5 random spots x 10 for 25 mana.
 */
-/* virtual */ int Spell_AreaBombardment::Cast(CUnit &caster, const SpellType &, CUnit *, const Vec2i &goalPos)
+/* virtual */ int Spell_AreaBombardment::Cast(CUnit &caster, const SpellType &, CUnit *&, const Vec2i &goalPos)
 {
 	int fields = this->Fields;
 	const int shards = this->Shards;
diff --git a/src/spell/spell_capture.cpp b/src/spell/spell_capture.cpp
index d1b05a535..27b86e10c 100644
--- a/src/spell/spell_capture.cpp
+++ b/src/spell/spell_capture.cpp
@@ -70,7 +70,7 @@
 **
 **  @return             =!0 if spell should be repeated, 0 if not
 */
-/* virtual */ int Spell_Capture::Cast(CUnit &caster, const SpellType &spell, CUnit *target, const Vec2i &/*goalPos*/)
+/* virtual */ int Spell_Capture::Cast(CUnit &caster, const SpellType &spell, CUnit *&target, const Vec2i &/*goalPos*/)
 {
 	if (!target || caster.Player == target->Player) {
 		return 0;
diff --git a/src/spell/spell_demolish.cpp b/src/spell/spell_demolish.cpp
index ca40f7274..4745b32e4 100644
--- a/src/spell/spell_demolish.cpp
+++ b/src/spell/spell_demolish.cpp
@@ -63,7 +63,7 @@
 **
 **  @return             =!0 if spell should be repeated, 0 if not
 */
-/* virtual */ int Spell_Demolish::Cast(CUnit &caster, const SpellType &, CUnit *, const Vec2i &goalPos)
+/* virtual */ int Spell_Demolish::Cast(CUnit &caster, const SpellType &, CUnit *&, const Vec2i &goalPos)
 {
 	// Allow error margins. (Lame, I know)
 	const Vec2i offset(this->Range + 2, this->Range + 2);
diff --git a/src/spell/spell_luacallback.cpp b/src/spell/spell_luacallback.cpp
index 2b30de393..e7576795a 100644
--- a/src/spell/spell_luacallback.cpp
+++ b/src/spell/spell_luacallback.cpp
@@ -56,7 +56,7 @@
 **
 **  @return        =!0 if spell should be repeated, 0 if not
 */
-/* virtual */ int Spell_LuaCallback::Cast(CUnit &caster, const SpellType &spell, CUnit *target, const Vec2i &goalPos)
+/* virtual */ int Spell_LuaCallback::Cast(CUnit &caster, const SpellType &spell, CUnit *&target, const Vec2i &goalPos)
 {
 	if (this->Func) {
 		this->Func->pushPreamble();
diff --git a/src/spell/spell_polymorph.cpp b/src/spell/spell_polymorph.cpp
index 7b5f05d1a..8d8a09a58 100644
--- a/src/spell/spell_polymorph.cpp
+++ b/src/spell/spell_polymorph.cpp
@@ -78,7 +78,7 @@
 **
 **  @return             =!0 if spell should be repeated, 0 if not
 */
-/* virtual */ int Spell_Polymorph::Cast(CUnit &caster, const SpellType &spell, CUnit *target, const Vec2i &goalPos)
+/* virtual */ int Spell_Polymorph::Cast(CUnit &caster, const SpellType &spell, CUnit *&target, const Vec2i &goalPos)
 {
 	if (!target) {
 		return 0;
diff --git a/src/spell/spell_spawnmissile.cpp b/src/spell/spell_spawnmissile.cpp
index 8ddb138f9..30d6eb7d6 100644
--- a/src/spell/spell_spawnmissile.cpp
+++ b/src/spell/spell_spawnmissile.cpp
@@ -179,7 +179,7 @@ static void EvaluateMissileLocation(const SpellActionMissileLocation &location,
 **
 **  @return             =!0 if spell should be repeated, 0 if not
 */
-/* virtual */ int Spell_SpawnMissile::Cast(CUnit &caster, const SpellType &, CUnit *target, const Vec2i &goalPos)
+/* virtual */ int Spell_SpawnMissile::Cast(CUnit &caster, const SpellType &, CUnit *&target, const Vec2i &goalPos)
 {
 	PixelPos startPos;
 	PixelPos endPos;
diff --git a/src/spell/spell_spawnportal.cpp b/src/spell/spell_spawnportal.cpp
index 606ef7db2..c20659a55 100644
--- a/src/spell/spell_spawnportal.cpp
+++ b/src/spell/spell_spawnportal.cpp
@@ -74,7 +74,7 @@
 **
 **  @return             =!0 if spell should be repeated, 0 if not
 */
-/* virtual */ int Spell_SpawnPortal::Cast(CUnit &caster, const SpellType &, CUnit *, const Vec2i &goalPos)
+/* virtual */ int Spell_SpawnPortal::Cast(CUnit &caster, const SpellType &, CUnit *&, const Vec2i &goalPos)
 {
 	// FIXME: vladi: cop should be placed only on explored land
 	CUnit *portal = caster.Goal;
diff --git a/src/spell/spell_summon.cpp b/src/spell/spell_summon.cpp
index 2c2c45232..faa112995 100644
--- a/src/spell/spell_summon.cpp
+++ b/src/spell/spell_summon.cpp
@@ -94,7 +94,7 @@ public:
 **
 **  @return             =!0 if spell should be repeated, 0 if not
 */
-/* virtual */ int Spell_Summon::Cast(CUnit &caster, const SpellType &spell, CUnit *target, const Vec2i &goalPos)
+/* virtual */ int Spell_Summon::Cast(CUnit &caster, const SpellType &spell, CUnit *&target, const Vec2i &goalPos)
 {
 	Vec2i pos = goalPos;
 	bool cansummon;
diff --git a/src/spell/spell_teleport.cpp b/src/spell/spell_teleport.cpp
index a9164b03b..07abe0f1b 100644
--- a/src/spell/spell_teleport.cpp
+++ b/src/spell/spell_teleport.cpp
@@ -56,7 +56,7 @@
 **
 **  @return             =!0 if spell should be repeated, 0 if not
 */
-/* virtual */ int Spell_Teleport::Cast(CUnit &caster, const SpellType &spell, CUnit * /*target*/, const Vec2i &goalPos)
+/* virtual */ int Spell_Teleport::Cast(CUnit &caster, const SpellType &spell, CUnit *&, const Vec2i &goalPos)
 {
 	if (Map.Info.IsPointOnMap(goalPos)) {
 		unsigned int selected = caster.Selected;