Changeset View
Changeset View
Standalone View
Standalone View
binaries/data/mods/public/simulation/components/tests/test_Attack.js
Engine.LoadHelperScript("Attacking.js"); | Engine.LoadHelperScript("Attacking.js"); | ||||
Engine.LoadHelperScript("Player.js"); | Engine.LoadHelperScript("Player.js"); | ||||
Engine.LoadHelperScript("ValueModification.js"); | Engine.LoadHelperScript("ValueModification.js"); | ||||
Engine.LoadComponentScript("interfaces/Auras.js"); | Engine.LoadComponentScript("interfaces/Auras.js"); | ||||
Engine.LoadComponentScript("interfaces/Capturable.js"); | Engine.LoadComponentScript("interfaces/Capturable.js"); | ||||
Engine.LoadComponentScript("interfaces/ModifiersManager.js"); | Engine.LoadComponentScript("interfaces/ModifiersManager.js"); | ||||
Engine.LoadComponentScript("interfaces/Formation.js"); | Engine.LoadComponentScript("interfaces/Formation.js"); | ||||
Engine.LoadComponentScript("interfaces/Health.js"); | Engine.LoadComponentScript("interfaces/Health.js"); | ||||
Engine.LoadComponentScript("interfaces/TechnologyManager.js"); | Engine.LoadComponentScript("interfaces/TechnologyManager.js"); | ||||
Engine.LoadComponentScript("Attack.js"); | Engine.LoadComponentScript("Attack.js"); | ||||
let entityID = 903; | let entityID = 903; | ||||
function attackComponentTest(defenderClass, isEnemy, test_function) | function attackComponentTest(defenderClass, isEnemy, test_function) | ||||
{ | { | ||||
ResetState(); | |||||
{ | |||||
let playerEnt1 = 5; | let playerEnt1 = 5; | ||||
AddMock(SYSTEM_ENTITY, IID_PlayerManager, { | AddMock(SYSTEM_ENTITY, IID_PlayerManager, { | ||||
"GetPlayerByID": () => playerEnt1 | "GetPlayerByID": () => playerEnt1 | ||||
}); | }); | ||||
AddMock(playerEnt1, IID_Player, { | AddMock(playerEnt1, IID_Player, { | ||||
"GetPlayerID": () => 1, | "GetPlayerID": () => 1, | ||||
"IsEnemy": () => isEnemy | "IsEnemy": () => isEnemy | ||||
}); | }); | ||||
} | |||||
let attacker = entityID; | let attacker = entityID; | ||||
AddMock(attacker, IID_Position, { | AddMock(attacker, IID_Position, { | ||||
"IsInWorld": () => true, | "IsInWorld": () => true, | ||||
"GetHeightOffset": () => 5, | "GetHeightOffset": () => 5, | ||||
"GetPosition2D": () => new Vector2D(1, 2) | "GetPosition2D": () => new Vector2D(1, 2) | ||||
}); | }); | ||||
▲ Show 20 Lines • Show All 298 Lines • ▼ Show 20 Lines | function testGetBestAttackAgainst(defenderClass, bestAttack, bestAllyAttack, isBuilding = false) | ||||
}); | }); | ||||
} | } | ||||
testGetBestAttackAgainst("FemaleCitizen", "Melee", undefined); | testGetBestAttackAgainst("FemaleCitizen", "Melee", undefined); | ||||
testGetBestAttackAgainst("Archer", "Ranged", undefined); | testGetBestAttackAgainst("Archer", "Ranged", undefined); | ||||
testGetBestAttackAgainst("Domestic", "Slaughter", "Slaughter"); | testGetBestAttackAgainst("Domestic", "Slaughter", "Slaughter"); | ||||
testGetBestAttackAgainst("Structure", "Capture", "Capture", true); | testGetBestAttackAgainst("Structure", "Capture", "Capture", true); | ||||
testGetBestAttackAgainst("Structure", "Ranged", undefined, false); | testGetBestAttackAgainst("Structure", "Ranged", undefined, false); | ||||
function testPredictTimeToTarget(selfPosition, horizSpeed, targetPosition, targetVelocity) | |||||
{ | |||||
ResetState(); | |||||
let cmpAttack = ConstructComponent(1, "Attack", {}); | |||||
let timeToTarget = cmpAttack.PredictTimeToTarget(selfPosition, horizSpeed, targetPosition, targetVelocity); | |||||
if (timeToTarget === false) | |||||
return; | |||||
// Position of the target after that time. | |||||
let targetPos = Vector3D.mult(targetVelocity, timeToTarget).add(targetPosition); | |||||
// Time that the projectile need to reach it. | |||||
let time = targetPos.horizDistanceTo(selfPosition) / horizSpeed; | |||||
TS_ASSERT_EQUALS(timeToTarget.toFixed(1), time.toFixed(1)); | |||||
} | |||||
testPredictTimeToTarget(new Vector3D(0, 0, 0), 4, new Vector3D(0, 0, 0), new Vector3D(0, 0, 0)); | |||||
testPredictTimeToTarget(new Vector3D(0, 0, 0), 4, new Vector3D(20, 0, 0), new Vector3D(0, 0, 0)); | |||||
testPredictTimeToTarget(new Vector3D(0, 0, 0), 4, new Vector3D(20, 0, 0), new Vector3D(1, 0, 0)); | |||||
testPredictTimeToTarget(new Vector3D(0, 0, 0), 4, new Vector3D(20, 0, 0), new Vector3D(4, 0, 0)); | |||||
testPredictTimeToTarget(new Vector3D(0, 0, 0), 4, new Vector3D(20, 0, 0), new Vector3D(16, 0, 0)); | |||||
testPredictTimeToTarget(new Vector3D(0, 0, 0), 4, new Vector3D(20, 0, 0), new Vector3D(-1, 0, 0)); | |||||
testPredictTimeToTarget(new Vector3D(0, 0, 0), 4, new Vector3D(20, 0, 0), new Vector3D(-4, 0, 0)); | |||||
testPredictTimeToTarget(new Vector3D(0, 0, 0), 4, new Vector3D(20, 0, 0), new Vector3D(-16, 0, 0)); | |||||
testPredictTimeToTarget(new Vector3D(0, 0, 0), 4, new Vector3D(20, 0, 0), new Vector3D(0, 0, 1)); | |||||
testPredictTimeToTarget(new Vector3D(0, 0, 0), 4, new Vector3D(20, 0, 0), new Vector3D(0, 0, 4)); | |||||
testPredictTimeToTarget(new Vector3D(0, 0, 0), 4, new Vector3D(20, 0, 0), new Vector3D(0, 0, 16)); | |||||
testPredictTimeToTarget(new Vector3D(0, 0, 0), 4, new Vector3D(20, 0, 0), new Vector3D(1, 0, 1)); | |||||
testPredictTimeToTarget(new Vector3D(0, 0, 0), 4, new Vector3D(20, 0, 0), new Vector3D(2, 0, 2)); | |||||
testPredictTimeToTarget(new Vector3D(0, 0, 0), 4, new Vector3D(20, 0, 0), new Vector3D(8, 0, 8)); | |||||
testPredictTimeToTarget(new Vector3D(0, 0, 0), 4, new Vector3D(20, 0, 0), new Vector3D(-1, 0, 1)); | |||||
testPredictTimeToTarget(new Vector3D(0, 0, 0), 4, new Vector3D(20, 0, 0), new Vector3D(-2, 0, 2)); | |||||
testPredictTimeToTarget(new Vector3D(0, 0, 0), 4, new Vector3D(20, 0, 0), new Vector3D(-8, 0, 8)); | |||||
testPredictTimeToTarget(new Vector3D(0, 0, 0), 4, new Vector3D(20, 0, 0), new Vector3D(4, 0, 2)); | |||||
testPredictTimeToTarget(new Vector3D(0, 0, 0), 4, new Vector3D(20, 0, 0), new Vector3D(-4, 0, 2)); |
Wildfire Games · Phabricator