Changeset View
Changeset View
Standalone View
Standalone View
binaries/data/mods/public/simulation/components/tests/test_Damage.js
Show First 20 Lines • Show All 180 Lines • ▼ Show 20 Lines | function TestLinearSplashDamage() | ||||
const origin = new Vector2D(0, 0); | const origin = new Vector2D(0, 0); | ||||
let data = { | let data = { | ||||
"type": "Ranged", | "type": "Ranged", | ||||
"attackData": { "Damage": { "Hack": 100, "Pierce": 0, "Crush": 0 } }, | "attackData": { "Damage": { "Hack": 100, "Pierce": 0, "Crush": 0 } }, | ||||
"attacker": attacker, | "attacker": attacker, | ||||
"attackerOwner": attackerOwner, | "attackerOwner": attackerOwner, | ||||
"origin": origin, | "origin": origin, | ||||
"radius": 10, | "minRange": 0, | ||||
"maxRange": 10, | |||||
"shape": "Linear", | "shape": "Linear", | ||||
"direction": new Vector3D(1, 747, 0), | "direction": new Vector3D(1, 747, 0), | ||||
"friendlyFire": false, | "friendlyFire": false, | ||||
}; | }; | ||||
let fallOff = function(x, y) | let fallOff = function(x, y) | ||||
{ | { | ||||
return (1 - x * x / (data.radius * data.radius)) * (1 - 25 * y * y / (data.radius * data.radius)); | return (1 - x * x / (data.maxRange * data.maxRange)) * (1 - 25 * y * y / (data.maxRange * data.maxRange)); | ||||
}; | }; | ||||
let hitEnts = new Set(); | let hitEnts = new Set(); | ||||
AddMock(attackerOwner, IID_Player, { | AddMock(attackerOwner, IID_Player, { | ||||
"GetEnemies": () => [2] | "GetEnemies": () => [2] | ||||
}); | }); | ||||
▲ Show 20 Lines • Show All 77 Lines • ▼ Show 20 Lines | |||||
TestLinearSplashDamage(); | TestLinearSplashDamage(); | ||||
function TestCircularSplashDamage() | function TestCircularSplashDamage() | ||||
{ | { | ||||
ResetState(); | ResetState(); | ||||
Engine.PostMessage = (ent, iid, message) => {}; | Engine.PostMessage = (ent, iid, message) => {}; | ||||
const radius = 10; | const radius = 10; | ||||
const minRange = 1; | |||||
let attackerOwner = 1; | let attackerOwner = 1; | ||||
let fallOff = function(r) | let fallOff = function(r) | ||||
{ | { | ||||
return 1 - r * r / (radius * radius); | return 1 - Math.square(r - minRange) / Math.square(radius - minRange); | ||||
bb: no need to call the square function twice
(Do note that calling extra function can have… | |||||
}; | }; | ||||
AddMock(attackerOwner, IID_Player, { | AddMock(attackerOwner, IID_Player, { | ||||
"GetEnemies": () => [2] | "GetEnemies": () => [2] | ||||
}); | }); | ||||
AddMock(SYSTEM_ENTITY, IID_PlayerManager, { | AddMock(SYSTEM_ENTITY, IID_PlayerManager, { | ||||
"GetPlayerByID": id => attackerOwner, | "GetPlayerByID": id => attackerOwner, | ||||
"GetAllPlayers": () => [0, 1, 2] | "GetAllPlayers": () => [0, 1, 2] | ||||
}); | }); | ||||
▲ Show 20 Lines • Show All 83 Lines • ▼ Show 20 Lines | function TestCircularSplashDamage() | ||||
let spy65 = new Spy(cmpHealth65, "TakeDamage"); | let spy65 = new Spy(cmpHealth65, "TakeDamage"); | ||||
AttackHelper.CauseDamageOverArea({ | AttackHelper.CauseDamageOverArea({ | ||||
"type": "Ranged", | "type": "Ranged", | ||||
"attackData": { "Damage": { "Hack": 100, "Pierce": 0, "Crush": 0 } }, | "attackData": { "Damage": { "Hack": 100, "Pierce": 0, "Crush": 0 } }, | ||||
"attacker": 50, | "attacker": 50, | ||||
"attackerOwner": attackerOwner, | "attackerOwner": attackerOwner, | ||||
"origin": new Vector2D(3, 4), | "origin": new Vector2D(3, 4), | ||||
"radius": radius, | "minRange": minRange, | ||||
"maxRange": radius, | |||||
"shape": "Circular", | "shape": "Circular", | ||||
"friendlyFire": false, | "friendlyFire": false, | ||||
}); | }); | ||||
TS_ASSERT_EQUALS(spy64._called, 1); | TS_ASSERT_EQUALS(spy64._called, 1); | ||||
TS_ASSERT_EQUALS(spy65._called, 1); | TS_ASSERT_EQUALS(spy65._called, 1); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 135 Lines • ▼ Show 20 Lines | function Test_MissileHit() | ||||
cmpDelayedDamage.Hit(data, 0); | cmpDelayedDamage.Hit(data, 0); | ||||
TS_ASSERT(hitEnts.has(61)); | TS_ASSERT(hitEnts.has(61)); | ||||
hitEnts.clear(); | hitEnts.clear(); | ||||
// Add a splash damage. | // Add a splash damage. | ||||
data.splash = {}; | data.splash = {}; | ||||
data.splash.friendlyFire = false; | data.splash.friendlyFire = false; | ||||
data.splash.radius = 10; | data.splash.minRange = 0; | ||||
data.splash.maxRange = 10; | |||||
data.splash.shape = "Circular"; | data.splash.shape = "Circular"; | ||||
data.splash.attackData = { "Damage": { "Hack": 0, "Pierce": 0, "Crush": 200 } }; | data.splash.attackData = { "Damage": { "Hack": 0, "Pierce": 0, "Crush": 200 } }; | ||||
AddMock(SYSTEM_ENTITY, IID_RangeManager, { | AddMock(SYSTEM_ENTITY, IID_RangeManager, { | ||||
"ExecuteQueryAroundPos": () => [61, 62] | "ExecuteQueryAroundPos": () => [61, 62] | ||||
}); | }); | ||||
AddMock(SYSTEM_ENTITY, IID_ObstructionManager, { | AddMock(SYSTEM_ENTITY, IID_ObstructionManager, { | ||||
▲ Show 20 Lines • Show All 90 Lines • ▼ Show 20 Lines | function Test_MissileHit() | ||||
TS_ASSERT(hitEnts.has(61)); | TS_ASSERT(hitEnts.has(61)); | ||||
TS_ASSERT_EQUALS(dealtDamage, 100 + 10000 * 200); | TS_ASSERT_EQUALS(dealtDamage, 100 + 10000 * 200); | ||||
dealtDamage = 0; | dealtDamage = 0; | ||||
hitEnts.clear(); | hitEnts.clear(); | ||||
// Test splash damage with friendly fire. | // Test splash damage with friendly fire. | ||||
data.splash = {}; | data.splash = {}; | ||||
data.splash.friendlyFire = true; | data.splash.friendlyFire = true; | ||||
data.splash.radius = 10; | data.splash.minRange = 0; | ||||
data.splash.maxRange = 10; | |||||
data.splash.shape = "Circular"; | data.splash.shape = "Circular"; | ||||
data.splash.attackData = { "Damage": { "Pierce": 0, "Crush": 200 } }; | data.splash.attackData = { "Damage": { "Pierce": 0, "Crush": 200 } }; | ||||
AddMock(SYSTEM_ENTITY, IID_RangeManager, { | AddMock(SYSTEM_ENTITY, IID_RangeManager, { | ||||
"ExecuteQueryAroundPos": () => [61, 62] | "ExecuteQueryAroundPos": () => [61, 62] | ||||
}); | }); | ||||
AddMock(SYSTEM_ENTITY, IID_ObstructionManager, { | AddMock(SYSTEM_ENTITY, IID_ObstructionManager, { | ||||
▲ Show 20 Lines • Show All 43 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator
no need to call the square function twice
(Do note that calling extra function can have performance influence, ofc these are tests so who cares and probably the best in this case too)