From d2e61fda55254478ae02cf690d2bcb3bc9e63c46 Mon Sep 17 00:00:00 2001
From: joris <joris.dauphin@gmail.com>
Date: Sat, 18 Feb 2012 13:43:16 +0100
Subject: [PATCH] [+]New parameter in the "spawn-missile"	spell, added
 "use-unit-var" which uses caster's damage parameters and ignores the "damage"
 field. [+]New parameter in the "polymorph" spell, added "player-caster"
 parameter, which converts target unit to caster's side.

Patch from Cybermind
---
 src/include/spells.h           |  7 ++++---
 src/stratagus/script_spell.cpp |  6 ++++++
 src/stratagus/spells.cpp       | 10 ++++++++--
 3 files changed, 18 insertions(+), 5 deletions(-)

diff --git a/src/include/spells.h b/src/include/spells.h
index 45286f97a..a2b63d468 100644
--- a/src/include/spells.h
+++ b/src/include/spells.h
@@ -148,14 +148,15 @@ public:
 
 class SpawnMissile : public SpellActionType {
 public:
-	SpawnMissile() : Damage(0), TTL(-1), Delay(0),
-		StartPoint(LocBaseCaster), EndPoint(LocBaseTarget), Missile(0) {};
+	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, int x, int y);
 
 	int Damage;                             /// Missile damage.
 	int TTL;                                /// Missile TTL.
 	int Delay;                              /// Missile original delay.
+	bool UseUnitVar;                        /// Use the caster's damage parameters
 	SpellActionMissileLocation StartPoint;  /// Start point description.
 	SpellActionMissileLocation EndPoint;    /// Start point description.
 	MissileType *Missile;                   /// Missile fired on cast
@@ -225,7 +226,7 @@ public:
 		CUnit *target, int x, int y);
 
 	CUnitType *NewForm;         /// The new form
-	int PlayerNeutral;          /// Convert the unit to the neutral player.
+	int PlayerNeutral;          /// Convert the unit to the neutral player, or to the caster's player.
 	// TODO: temporary polymorphs would be awesome, but hard to implement
 };
 
diff --git a/src/stratagus/script_spell.cpp b/src/stratagus/script_spell.cpp
index d2a330cef..fd1263bf6 100644
--- a/src/stratagus/script_spell.cpp
+++ b/src/stratagus/script_spell.cpp
@@ -144,6 +144,9 @@ static SpellActionType *CclSpellAction(lua_State *l)
 				lua_rawgeti(l, -1, j + 1);
 				spellaction->Damage = LuaToNumber(l, -1);
 				lua_pop(l, 1);
+			} else if (!strcmp(value, "use-unit-var")) {
+				spellaction->UseUnitVar = true;
+				--j;
 			} else if (!strcmp(value, "delay")) {
 				lua_rawgeti(l, -1, j + 1);
 				spellaction->Delay = LuaToNumber(l, -1);
@@ -425,6 +428,9 @@ static SpellActionType *CclSpellAction(lua_State *l)
 			} else if (!strcmp(value, "player-neutral")) {
 				spellaction->PlayerNeutral = 1;
 				--j;
+			} else if (!strcmp(value, "player-caster")) {
+				spellaction->PlayerNeutral = 2;
+				--j;
 			} else {
 				LuaError(l, "Unsupported polymorph tag: %s" _C_ value);
 			}
diff --git a/src/stratagus/spells.cpp b/src/stratagus/spells.cpp
index 295dd7b0c..27690fccc 100644
--- a/src/stratagus/spells.cpp
+++ b/src/stratagus/spells.cpp
@@ -349,7 +349,11 @@ int SpawnMissile::Cast(CUnit &caster, const SpellType *, CUnit *target, int x, i
 	missile->TTL = this->TTL;
 	missile->Delay = this->Delay;
 	missile->Damage = this->Damage;
-	if (missile->Damage != 0) {
+	if (this->UseUnitVar) {
+		missile->Damage = 0;
+		missile->SourceUnit = &caster;
+		caster.RefsIncrease();
+	} else if (missile->Damage != 0) {
 		missile->SourceUnit = &caster;
 		caster.RefsIncrease();
 	}
@@ -553,8 +557,10 @@ int Polymorph::Cast(CUnit &caster, const SpellType *spell, CUnit *target, int x,
 		}
 	}
 	caster.Variable[MANA_INDEX].Value -= spell->ManaCost;
-	if (this->PlayerNeutral) {
+	if (this->PlayerNeutral == 1) {
 		MakeUnitAndPlace(pos, type, Players + PlayerNumNeutral);
+	} else if (this->PlayerNeutral == 2) {
+		MakeUnitAndPlace(pos, type, caster.Player);
 	} else {
 		MakeUnitAndPlace(pos, type, target->Player);
 	}