Index: 0ad/binaries/data/mods/public/simulation/helpers/Damage.js
===================================================================
--- 0ad/binaries/data/mods/public/simulation/helpers/Damage.js	(revision 0)
+++ 0ad/binaries/data/mods/public/simulation/helpers/Damage.js	(working copy)
@@ -0,0 +1,155 @@
+//Create global Damage object.
+var Damage = new Object()
+
+/****************************************
+ * Damages units around a given origin.
+ * data.attacker = <entity id>
+ * data.origin = {'x':<int>, 'z':<int>}
+ * data.radius = <int>
+ * data.shape = <string>
+ * data.strengths = {'hack':<float>, 'pierce':<float>, 'crush':<float>}
+ * ***Optional Variables***
+ * data.direction = <unit vector>
+ * data.playersToDamage = <array of player ids>
+ */
+Damage.CauseSplashDamage = function(data)
+{
+	// Get nearby entities and define variables
+	var nearEnts = Damage.EntitiesNearPoint(data.origin, data.radius, data.playersToDamage);
+	var damageMultiplier = 1;
+	// Cycle through all the nearby entities and damage it appropriately based on its distance from the origin.
+	for each (var entity in nearEnts)
+	{
+		var entityPosition = Engine.QueryInterface(entity, IID_Position).GetPosition();
+		if(data.shape=='Circular') // circular effect with quadratic falloff in every direction
+		{
+			var squaredDistanceFromOrigin = Damage.VectorDistanceSquared(data.origin, entityPosition);
+			damageMultiplier == 1 - ((squaredDistanceFromOrigin) / (data.radius * data.radius));
+		}
+		else if(data.shape=='Linear') // linear effect with quadratic falloff in two directions (only used for certain missiles)
+		{
+			// Get position of entity relative to splash origin.
+			var relativePos = {"x": entityPosition.x - data.origin.x, "z": entityPosition.z - data.origin.z};
+			
+			// The width of linear splash is one fifth of the normal splash radius.
+			var width = data.radius/5;
+			
+			// Effectivly rotate the axis to align with the missile direction.
+			var parallelDist = Damage.VectorDot(relativePos, data.direction);//x axis
+			var perpDist = Math.abs(Damage.VectorCross(relativePos, data.direction));//y axis
+			
+			// Check that the unit is within the distance at which it will get damaged.
+			if (parallelDist > -width && perpDist < width) // If in radius, quadratic falloff in both directions
+				multiplier = (data.radius*data.radius - parallelDist*parallelDist) / (data.radius*data.radius)
+						             * (width*width - perpDist*perpDist) / (width*width);
+			else
+				multiplier = 0;
+		}
+		else // In case someone calls this function with an invalid shape.
+		{
+			warn("The "+data.shape+" splash damage shape is not implemented!");
+		}
+		// Call CauseDamage which reduces the hitpoints, posts network command, plays sounds....
+		Damage.CauseDamage({"strengths":data.strengths, "target":entity, "attacker":data.attacker, "multiplier":damageMultiplier, "type":"Splash"})
+	}
+};
+
+/****************************************
+ * Causes damage on a given unit
+ * data.strengths = {'hack':<float>, 'pierce':<float>, 'crush':<float>}
+ * data.target = <entity id>
+ * data.attacker = <entity id>
+ * data.multiplier = <float between 1 and 0>
+ * data.type = <string>
+ */
+Damage.CauseDamage = function(data)
+{
+	// Check the target can be damaged otherwise don't do anything.
+	var cmpDamageReceiver = Engine.QueryInterface(data.target, IID_DamageReceiver);
+	if (!cmpDamageReceiver)
+		return;
+
+	// Damage the target
+	var targetState = cmpDamageReceiver.TakeDamage(data.strengths.hack * data.multiplier, data.strengths.pierce * data.multiplier, data.strengths.crush * data.multiplier);
+
+	// If the target was killed run some cleanup
+	if (targetState.killed == true)
+		Damage.TargetKilled(data.attacker, data.target);
+
+	// Post the network command (make it work in multiplayer)
+	Engine.PostMessage(data.target, MT_Attacked,{ "attacker": data.attacker, "target": data.target, "type": data.type, "damage": -targetState.change });
+
+	// Play attacking sounds
+	PlaySound("attack_impact", data.attacker);
+};
+
+/****************************************
+ * Gets entities near a give point for given players.
+ * origin = {'x':<int>, 'z':<int>}
+ * radius = <int>
+ * players = <array>
+ * If players is not included, entities from all players are used.
+ */
+Damage.EntitiesNearPoint = function(origin, radius, players)
+{
+	// If there is insufficient data return an empty array.
+	if(!origin || !radius)	
+		return [];
+	//Create the dummy entity used for range calculations if it doesn't exist.
+	if(!Damage.dummyTargetEntity)
+		Damage.dummyTargetEntity = Engine.AddEntity('special/dummy');
+	// Move the dummy entity to the origin of the query.
+	var cmpDummyPosition = Engine.QueryInterface(Damage.dummyTargetEntity, IID_Position);
+	cmpDummyPosition.JumpTo(origin.x, origin.z);
+	
+	// If the players parameter is not specified use all players.
+	if(!players)
+		players = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager).GetAllPlayers();
+
+	// Call RangeManager with dummy entity and return the result.
+	var rangeManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager);
+	var rangeQuery = rangeManager.ExecuteQuery(Damage.dummyTargetEntity, 0, radius, players, IID_DamageReceiver);
+	return rangeQuery;
+};
+
+/****************************************
+ * Called when some units kills something (another unit, building, animal etc)
+ * killerEntity = <entity id>
+ * targetEntity = <entity id>
+ */
+Damage.TargetKilled = function(killerEntity, targetEntity)
+{
+	// Add to killer statistics.
+	var cmpKillerPlayerStatisticsTracker = QueryOwnerInterface(killerEntity, IID_StatisticsTracker);
+	if (cmpKillerPlayerStatisticsTracker)
+		cmpKillerPlayerStatisticsTracker.KilledEntity(targetEntity);
+	// Add to loser statistics.
+	var cmpTargetPlayerStatisticsTracker = QueryOwnerInterface(targetEntity, IID_StatisticsTracker);
+	if (cmpTargetPlayerStatisticsTracker)
+		cmpTargetPlayerStatisticsTracker.LostEntity(targetEntity);
+
+	// If killer can collect loot, lets try to collect it.
+	var cmpLooter = Engine.QueryInterface(killerEntity, IID_Looter);
+	if (cmpLooter)
+		cmpLooter.Collect(targetEntity);
+};
+
+// Gets the straight line distance between p1 and p2
+Damage.VectorDistanceSquared = function(p1, p2)
+{
+	return ((p1.x - p2.x)*(p1.x - p2.x) + (p1.z - p2.z)*(p1.z - p2.z));
+};
+
+// Gets the dot product of two vectors.
+Damage.VectorDot = function(p1, p2)
+{
+    return (p1.x * p2.x + p1.z * p2.z);
+};
+
+// Gets the 2D interpreted version of the cross product of two vectors.
+Damage.VectorCross = function(p1, p2)
+{
+    return (p1.x * p2.z - p1.z * p2.x);
+};
+
+Engine.RegisterGlobal("Damage", Damage);
Index: 0ad/binaries/data/mods/public/simulation/components/Attack.js
===================================================================
--- 0ad/binaries/data/mods/public/simulation/components/Attack.js	(revision 13543)
+++ 0ad/binaries/data/mods/public/simulation/components/Attack.js	(working copy)
@@ -488,29 +488,11 @@
 	else
 	{
 		// Melee attack - hurt the target immediately
-		this.CauseDamage({"type": type, "target": target});
+		Damage.CauseDamage({"strengths":this.GetAttackStrengths(type), "target":target, "attacker":this.entity, "multiplier":this.GetAttackBonus(type, target), "type":type});
 	}
 	// TODO: charge attacks (need to design how they work)
 };
 
-/**
- * Called when some units kills something (another unit, building, animal etc)
- */
-Attack.prototype.TargetKilled = function(killerEntity, targetEntity)
-{
-	var cmpKillerPlayerStatisticsTracker = QueryOwnerInterface(killerEntity, IID_StatisticsTracker);
-	if (cmpKillerPlayerStatisticsTracker) cmpKillerPlayerStatisticsTracker.KilledEntity(targetEntity);
-	var cmpTargetPlayerStatisticsTracker = QueryOwnerInterface(targetEntity, IID_StatisticsTracker);
-	if (cmpTargetPlayerStatisticsTracker) cmpTargetPlayerStatisticsTracker.LostEntity(targetEntity);
-
-	// if unit can collect loot, lets try to collect it
-	var cmpLooter = Engine.QueryInterface(killerEntity, IID_Looter);
-	if (cmpLooter)
-	{
-		cmpLooter.Collect(targetEntity);
-	}
-};
-
 Attack.prototype.InterpolatedLocation = function(ent, lateness)
 {
 	var cmpTargetPosition = Engine.QueryInterface(ent, IID_Position);
@@ -533,11 +515,6 @@
 	return (p1.x * p2.x + p1.z * p2.z);
 };
 
-Attack.prototype.VectorCross = function(p1, p2)
-{
-	return (p1.x * p2.z - p1.z * p2.x);
-};
-
 Attack.prototype.VectorLength = function(p)
 {
 	return Math.sqrt(p.x*p.x + p.z*p.z);
@@ -586,54 +563,16 @@
 		var friendlyFire = this.template.Ranged.Splash.FriendlyFire;
 		var splashRadius = this.template.Ranged.Splash.Range;
 		var splashShape = this.template.Ranged.Splash.Shape;
-		
-		var ents = this.GetNearbyEntities(data.target, this.VectorDistance(data.position, targetPosition) * 2 + splashRadius, friendlyFire);
-		ents.push(data.target); // Add the original unit to the list of splash damage targets
-		
-		for (var i = 0; i < ents.length; i++)
-		{
-			var entityPosition = this.InterpolatedLocation(ents[i], lateness);
-			var radius = this.VectorDistance(data.position, entityPosition);
-			
-			if (radius < splashRadius)
-			{
-				var multiplier = 1;
-				if (splashShape == "Circular") // quadratic falloff
-				{
-					multiplier *= 1 - ((radius * radius) / (splashRadius * splashRadius));
-				}
-				else if (splashShape == "Linear")
-				{
-					// position of entity relative to where the missile hit
-					var relPos = {"x": entityPosition.x - data.position.x, "z": entityPosition.z - data.position.z};
-					
-					var splashWidth = splashRadius / 5;
-					var parallelDist = this.VectorDot(relPos, data.direction);
-					var perpDist = Math.abs(this.VectorCross(relPos, data.direction));
-					
-					// Check that the unit is within the distance splashWidth of the line starting at the missile's 
-					// landing point which extends in the direction of the missile for length splashRadius.
-					if (parallelDist > -splashWidth && perpDist < splashWidth)
-					{
-						// Use a quadratic falloff in both directions
-						multiplier = (splashRadius*splashRadius - parallelDist*parallelDist) / (splashRadius*splashRadius)
-						             * (splashWidth*splashWidth - perpDist*perpDist) / (splashWidth*splashWidth);
-					}
-					else
-					{
-						multiplier = 0;
-					}
-				}
-				var newData = {"type": data.type + ".Splash", "target": ents[i], "damageMultiplier": multiplier};
-				this.CauseDamage(newData);
-			}
-		}
+		Damage.CauseSplashDamage({"attacker":this.entity, "origin":data.position, "radius":splashRadius, "shape":splashShape, "strengths":this.GetAttackStrengths(data.type), "direction":data.direction, "playersToDamage":undefined});
 	}
 	
 	if (this.testCollision(data.target, data.position, lateness))
 	{
+		data.attacker=this.entity
+		data.multiplier=this.GetAttackBonus(data.type, data.target)
+		data.strengths=this.GetAttackStrengths(data.type)
 		// Hit the primary target
-		this.CauseDamage(data);
+		Damage.CauseDamage(data);
 		
 		// Remove the projectile
 		var cmpProjectileManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_ProjectileManager);
@@ -642,14 +581,14 @@
 	else
 	{
 		// If we didn't hit the main target look for nearby units
-		var ents = this.GetNearbyEntities(data.target, this.VectorDistance(data.position, targetPosition) * 2);
+		var ents = Damage.EntitiesNearPoint(data.position, this.VectorDistance(data.position, targetPosition) * 2);
 		
 		for (var i = 0; i < ents.length; i++)
 		{
 			if (this.testCollision(ents[i], data.position, lateness))
 			{
-				var newData = {"type": data.type, "target": ents[i]};
-				this.CauseDamage(newData);
+				var newData = {"strengths":this.GetAttackStrengths(data.type), "target":ents[i], "attacker":this.entity, "multiplier":this.GetAttackBonus(data.type, ents[i]), "type":data.type};
+				Damage.CauseDamage(newData);
 				
 				// Remove the projectile
 				var cmpProjectileManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_ProjectileManager);
@@ -659,53 +598,6 @@
 	}
 };
 
-Attack.prototype.GetNearbyEntities = function(startEnt, range, friendlyFire)
-{
-	var cmpPlayerManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager);
-	var cmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership);
-	var owner = cmpOwnership.GetOwner();
-	var cmpPlayer = Engine.QueryInterface(cmpPlayerManager.GetPlayerByID(owner), IID_Player);
-	var numPlayers = cmpPlayerManager.GetNumPlayers();
-	var players = [];
-	
-	for (var i = 1; i < numPlayers; ++i)
-	{	
-		// Only target enemies unless friendly fire is on
-		if (cmpPlayer.IsEnemy(i) || friendlyFire)
-			players.push(i);
-	}
-	
-	var rangeManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager);
-	return rangeManager.ExecuteQuery(startEnt, 0, range, players, IID_DamageReceiver);
-}
-
-/**
- * Inflict damage on the target
- */
-Attack.prototype.CauseDamage = function(data)
-{
-	var strengths = this.GetAttackStrengths(data.type);
-	
-	var damageMultiplier = this.GetAttackBonus(data.type, data.target);
-	if (data.damageMultiplier !== undefined)
-		damageMultiplier *= data.damageMultiplier;
-	
-	var cmpDamageReceiver = Engine.QueryInterface(data.target, IID_DamageReceiver);
-	if (!cmpDamageReceiver)
-		return;
-	var targetState = cmpDamageReceiver.TakeDamage(strengths.hack * damageMultiplier, strengths.pierce * damageMultiplier, strengths.crush * damageMultiplier);
-	// if target killed pick up loot and credit experience
-	if (targetState.killed == true)
-	{
-		this.TargetKilled(this.entity, data.target);
-	}
-
-	Engine.PostMessage(data.target, MT_Attacked,
-		{ "attacker": this.entity, "target": data.target, "type": data.type, "damage": -targetState.change });
-
-	PlaySound("attack_impact", this.entity);
-};
-
 Attack.prototype.OnUpdate = function(msg)
 {
 	this.turnLength = msg.turnLength;
Index: 0ad/binaries/data/mods/public/simulation/components/PlayerManager.js
===================================================================
--- 0ad/binaries/data/mods/public/simulation/components/PlayerManager.js	(revision 13543)
+++ 0ad/binaries/data/mods/public/simulation/components/PlayerManager.js	(working copy)
@@ -46,4 +46,8 @@
 	this.playerEntities = [];
 };
 
+PlayerManager.prototype.GetAllPlayers = function()
+{
+	return this.playerEntities;
+};
 Engine.RegisterComponentType(IID_PlayerManager, "PlayerManager", PlayerManager);
