Changeset View
Changeset View
Standalone View
Standalone View
binaries/data/mods/public/simulation/helpers/Attacking.js
Show First 20 Lines • Show All 199 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 maximum 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 {boolean} data.friendlyFire - A flag indicating if allied entities also ought to be damaged. | * @param {boolean} data.friendlyFire - A flag indicating if allied entities also ought to be damaged. | ||||
*/ | */ | ||||
Attacking.prototype.CauseDamageOverArea = function(data) | Attacking.prototype.CauseDamageOverArea = function(data) | ||||
{ | { | ||||
let nearEnts = this.EntitiesNearPoint(data.origin, data.radius, | let nearEnts = this.EntitiesNearPoint(data.origin, data.minRange, data.maxRange, | ||||
this.GetPlayersToDamage(data.attackerOwner, data.friendlyFire)); | this.GetPlayersToDamage(data.attackerOwner, data.friendlyFire)); | ||||
let damageMultiplier = 1; | let damageMultiplier = 1; | ||||
// 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(); | let entityPosition = Engine.QueryInterface(ent, IID_Position).GetPosition2D(); | ||||
if (data.shape == 'Circular') // circular effect with quadratic falloff in every direction | if (data.shape == 'Circular') // circular effect with quadratic falloff in every direction | ||||
damageMultiplier = 1 - data.origin.distanceToSquared(entityPosition) / (data.radius * data.radius); | damageMultiplier = 1 - data.origin.distanceToSquared(entityPosition) / (data.maxRange * data.maxRange); | ||||
wraitii: probably want to scale between min-range and max-range? | |||||
FreagarachAuthorUnsubmitted Done Inline ActionsWould be nice indeed :) Freagarach: Would be nice indeed :) | |||||
Done Inline ActionsMy math teacher in high-school taught me that x^2/y^2==(x/y)^2 Considering D2759, this number can get less than 0. Maybe add a safeguard for that. bb: My math teacher in high-school taught me that x^2/y^2==(x/y)^2
Considering D2759, this number… | |||||
else if (data.shape == 'Linear') // linear effect with quadratic falloff in two directions (only used for certain missiles) | 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. | // 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 = data.maxRange / 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 / (data.maxRange * data.maxRange)) * | ||||
Done Inline ActionsIf you wish, you can do the same type of falloff here. bb: If you wish, you can do the same type of falloff here. | |||||
Done Inline ActionsBut this doesn't use the minRange? Freagarach: But this doesn't use the `minRange`? | |||||
Done Inline ActionsI think bb's point is that you don't support minRange here. wraitii: I think bb's point is that you don't support minRange here. | |||||
(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 // In case someone calls this function with an invalid shape. | ||||
{ | { | ||||
warn("The " + data.shape + " splash damage shape is not implemented!"); | warn("The " + data.shape + " splash damage shape is not implemented!"); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 41 Lines • ▼ Show 20 Lines | Attacking.prototype.HandleAttackEffects = function(attackType, attackData, target, attacker, attackerOwner, bonusMultiplier = 1) | ||||
let cmpPromotion = Engine.QueryInterface(attacker, IID_Promotion); | let cmpPromotion = Engine.QueryInterface(attacker, IID_Promotion); | ||||
if (cmpPromotion && targetState.xp) | if (cmpPromotion && targetState.xp) | ||||
cmpPromotion.IncreaseXp(targetState.xp); | cmpPromotion.IncreaseXp(targetState.xp); | ||||
}; | }; | ||||
/** | /** | ||||
* 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 minimum distance the entities may have from the point to check. | ||||
* @param {number} maxRange - The maximum 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 || !players.length) | if (!origin || !maxRange || !players || !players.length) | ||||
return []; | return []; | ||||
let cmpRangeManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager); | let cmpRangeManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager); | ||||
// Return all entities, except for Gaia: IID_Health is required to avoid returning trees and such. | // Return all entities, except for Gaia: IID_Health is required to avoid returning trees and such. | ||||
let gaiaEntities = []; | let gaiaEntities = []; | ||||
let gaiaIndex = players.indexOf(0); | let gaiaIndex = players.indexOf(0); | ||||
if (gaiaIndex !== -1) | if (gaiaIndex !== -1) | ||||
{ | { | ||||
// splice() modifies players in-place and returns [0] | // splice() modifies players in-place and returns [0] | ||||
gaiaEntities = gaiaEntities.concat(cmpRangeManager.ExecuteQueryAroundPos(origin, 0, radius, players.splice(gaiaIndex, 1), IID_Health)); | gaiaEntities = gaiaEntities.concat(cmpRangeManager.ExecuteQueryAroundPos(origin, minRange, maxRange, players.splice(gaiaIndex, 1), IID_Health)); | ||||
if (!players.length) | if (!players.length) | ||||
return gaiaEntities; | return gaiaEntities; | ||||
} | } | ||||
return cmpRangeManager.ExecuteQueryAroundPos(origin, 0, radius, players, 0).concat(gaiaEntities); | return cmpRangeManager.ExecuteQueryAroundPos(origin, minRange, maxRange, players, 0).concat(gaiaEntities); | ||||
}; | }; | ||||
/** | /** | ||||
* 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
probably want to scale between min-range and max-range?