Changeset View
Changeset View
Standalone View
Standalone View
binaries/data/mods/public/simulation/helpers/Attacking.js
Show First 20 Lines • Show All 173 Lines • ▼ Show 20 Lines | |||||
/** | /** | ||||
* Damages units around a given origin. | * Damages units around a given origin. | ||||
* @param {Object} data - The data sent by the caller. | * @param {Object} data - The data sent by the caller. | ||||
* @param {string} data.type - The type of damage. | * @param {string} data.type - The type of damage. | ||||
* @param {Object} data.attackData - The attack data. | * @param {Object} data.attackData - The attack data. | ||||
* @param {number} data.attacker - The entity id of the attacker. | * @param {number} data.attacker - The entity id of the attacker. | ||||
* @param {number} data.attackerOwner - The player id of the attacker. | * @param {number} data.attackerOwner - The player id of the attacker. | ||||
* @param {Vector2D} data.origin - The origin of the projectile hit. | * @param {Vector2D} data.origin - The origin of the projectile hit. | ||||
* @param {number} data.radius - The radius of the splash damage. | * @param {number} data.minRange - The minimal radius of the area damage. | ||||
* @param {number} data.maxRange - The maximal radius of the area damage. | |||||
* @param {string} data.shape - The shape of the radius. | * @param {string} data.shape - The shape of the radius. | ||||
* @param {Vector3D} [data.direction] - The unit vector defining the direction. Needed for linear splash damage. | * @param {Vector3D} [data.direction] - The unit vector defining the direction. Needed for linear splash damage. | ||||
* @param {number[]} data.playersToDamage - The array of player id's to damage. | * @param {number[]} data.playersToDamage - The array of player id's to damage. | ||||
*/ | */ | ||||
Attacking.prototype.CauseDamageOverArea = function(data) | Attacking.prototype.CauseDamageOverArea = function(data) | ||||
{ | { | ||||
// Get nearby entities and define variables | // Get nearby entities and define variables | ||||
let nearEnts = this.EntitiesNearPoint(data.origin, data.radius, data.playersToDamage); | let nearEnts = this.EntitiesNearPoint(data.origin, data.minRange, data.maxRange, data.playersToDamage); | ||||
let damageMultiplier = 1; | let damageMultiplier = 1; | ||||
// Radius used for calculations. | |||||
let radius = data.maxRange; | |||||
// Cycle through all the nearby entities and damage it appropriately based on its distance from the origin. | // Cycle through all the nearby entities and damage it appropriately based on its distance from the origin. | ||||
for (let ent of nearEnts) | for (let ent of nearEnts) | ||||
{ | { | ||||
let entityPosition = Engine.QueryInterface(ent, IID_Position).GetPosition2D(); | // Do not damage self when causing proximity damage. | ||||
if (data.shape == 'Circular') // circular effect with quadratic falloff in every direction | if (ent == data.attacker && data.type == "Proximity") | ||||
damageMultiplier = 1 - data.origin.distanceToSquared(entityPosition) / (data.radius * data.radius); | continue; | ||||
else if (data.shape == 'Linear') // linear effect with quadratic falloff in two directions (only used for certain missiles) | |||||
let cmpPosition = Engine.QueryInterface(ent, IID_Position); | |||||
if (!cmpPosition || !cmpPosition.IsInWorld()) | |||||
continue; | |||||
let entityPosition = cmpPosition.GetPosition2D(); | |||||
// Circular effect with quadratic falloff in every direction. | |||||
if (data.shape == 'Circular') | |||||
damageMultiplier = 1 - data.origin.distanceToSquared(entityPosition) / (radius * radius); | |||||
// Linear effect with quadratic falloff in two directions (only used for certain missiles). | |||||
else if (data.shape == 'Linear' && data.direction) | |||||
{ | { | ||||
// Get position of entity relative to splash origin. | // Get position of entity relative to splash origin. | ||||
let relativePos = entityPosition.sub(data.origin); | let relativePos = entityPosition.sub(data.origin); | ||||
// Get the position relative to the missile direction. | // Get the position relative to the missile direction. | ||||
let direction = Vector2D.from3D(data.direction); | let direction = Vector2D.from3D(data.direction); | ||||
let parallelPos = relativePos.dot(direction); | let parallelPos = relativePos.dot(direction); | ||||
let perpPos = relativePos.cross(direction); | let perpPos = relativePos.cross(direction); | ||||
// The width of linear splash is one fifth of the normal splash radius. | // The width of linear splash is one fifth of the normal splash radius. | ||||
let width = data.radius / 5; | let width = radius / 5; | ||||
// Check that the unit is within the distance splash width of the line starting at the missile's | // Check that the unit is within the distance splash width of the line starting at the missile's | ||||
// landing point which extends in the direction of the missile for length splash radius. | // landing point which extends in the direction of the missile for length splash radius. | ||||
if (parallelPos >= 0 && Math.abs(perpPos) < width) // If in radius, quadratic falloff in both directions | if (parallelPos >= 0 && Math.abs(perpPos) < width) // If in radius, quadratic falloff in both directions | ||||
damageMultiplier = (1 - parallelPos * parallelPos / (data.radius * data.radius)) * | damageMultiplier = (1 - parallelPos * parallelPos / (radius * radius)) * | ||||
(1 - perpPos * perpPos / (width * width)); | (1 - perpPos * perpPos / (width * width)); | ||||
else | else | ||||
damageMultiplier = 0; | damageMultiplier = 0; | ||||
} | } | ||||
else // In case someone calls this function with an invalid shape. | else | ||||
{ | { | ||||
warn("The " + data.shape + " splash damage shape is not implemented!"); | warn("The " + data.shape + " splash damage shape is not implemented!"); | ||||
} | } | ||||
this.HandleAttackEffects(data.type + ".Splash", data.attackData, ent, data.attacker, data.attackerOwner, damageMultiplier); | this.HandleAttackEffects(data.type + ".Splash", data.attackData, ent, data.attacker, data.attackerOwner, damageMultiplier); | ||||
} | } | ||||
}; | }; | ||||
Show All 31 Lines | Engine.PostMessage(target, MT_Attacked, { | ||||
"capture": targetState.captureChange || 0, | "capture": targetState.captureChange || 0, | ||||
"statusEffects": targetState.inflictedStatuses || [], | "statusEffects": targetState.inflictedStatuses || [], | ||||
}); | }); | ||||
}; | }; | ||||
/** | /** | ||||
* Gets entities near a give point for given players. | * Gets entities near a give point for given players. | ||||
* @param {Vector2D} origin - The point to check around. | * @param {Vector2D} origin - The point to check around. | ||||
* @param {number} radius - The radius around the point to check. | * @param {number} minRange - The maximum distance the entities may have from the point to check. | ||||
* @param {number} maxRange - The minumum distance the entities may have from the point to check. | |||||
* @param {number[]} players - The players of which we need to check entities. | * @param {number[]} players - The players of which we need to check entities. | ||||
* @return {number[]} The id's of the entities in range of the given point. | * @return {number[]} The id's of the entities in range of the given point. | ||||
*/ | */ | ||||
Attacking.prototype.EntitiesNearPoint = function(origin, radius, players) | Attacking.prototype.EntitiesNearPoint = function(origin, minRange, maxRange, players) | ||||
{ | { | ||||
// If there is insufficient data return an empty array. | // If there is insufficient data return an empty array. | ||||
if (!origin || !radius || !players) | if (!origin || !maxRange || !players) | ||||
return []; | return []; | ||||
return Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager).ExecuteQueryAroundPos(origin, 0, radius, players, IID_Resistance); | return Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager).ExecuteQueryAroundPos(origin, minRange || 0, maxRange, players, IID_Resistance); | ||||
}; | }; | ||||
/** | /** | ||||
* Called when a unit kills something (another unit, building, animal etc). | * Called when a unit kills something (another unit, building, animal etc). | ||||
* @param {number} attacker - The entity id of the killer. | * @param {number} attacker - The entity id of the killer. | ||||
* @param {number} target - The entity id of the target. | * @param {number} target - The entity id of the target. | ||||
* @param {number} attackerOwner - The player id of the attacker. | * @param {number} attackerOwner - The player id of the attacker. | ||||
*/ | */ | ||||
Show All 22 Lines |
Wildfire Games · Phabricator