Changeset View
Changeset View
Standalone View
Standalone View
binaries/data/mods/public/simulation/components/tests/test_UnitAI.js
Show All 30 Lines | function TestFormationExiting(mode) | ||||
var unit = 10; | var unit = 10; | ||||
var enemy = 20; | var enemy = 20; | ||||
var controller = 30; | var controller = 30; | ||||
AddMock(SYSTEM_ENTITY, IID_Timer, { | AddMock(SYSTEM_ENTITY, IID_Timer, { | ||||
SetInterval: function() { }, | SetInterval: function() { }, | ||||
SetTimeout: function() { }, | SetTimeout: function() { }, | ||||
GetTime: function() { return 0; }, | |||||
}); | }); | ||||
AddMock(SYSTEM_ENTITY, IID_RangeManager, { | AddMock(SYSTEM_ENTITY, IID_RangeManager, { | ||||
CreateActiveQuery: function(ent, minRange, maxRange, players, iid, flags) { | CreateActiveQuery: function(ent, minRange, maxRange, players, iid, flags) { | ||||
return 1; | return 1; | ||||
}, | }, | ||||
EnableActiveQuery: function(id) { }, | EnableActiveQuery: function(id) { }, | ||||
ResetActiveQuery: function(id) { if (mode == 0) return []; else return [enemy]; }, | ResetActiveQuery: function(id) { if (mode == 0) return []; else return [enemy]; }, | ||||
▲ Show 20 Lines • Show All 130 Lines • ▼ Show 20 Lines | function TestMoveIntoFormationWhileAttacking() | ||||
var unit = 30; | var unit = 30; | ||||
var units = []; | var units = []; | ||||
var unitCount = 8; | var unitCount = 8; | ||||
var unitAIs = []; | var unitAIs = []; | ||||
AddMock(SYSTEM_ENTITY, IID_Timer, { | AddMock(SYSTEM_ENTITY, IID_Timer, { | ||||
SetInterval: function() { }, | SetInterval: function() { }, | ||||
SetTimeout: function() { }, | SetTimeout: function() { }, | ||||
GetTime: function() { return 0; }, | |||||
}); | }); | ||||
AddMock(SYSTEM_ENTITY, IID_RangeManager, { | AddMock(SYSTEM_ENTITY, IID_RangeManager, { | ||||
CreateActiveQuery: function(ent, minRange, maxRange, players, iid, flags) { | CreateActiveQuery: function(ent, minRange, maxRange, players, iid, flags) { | ||||
return 1; | return 1; | ||||
}, | }, | ||||
EnableActiveQuery: function(id) { }, | EnableActiveQuery: function(id) { }, | ||||
▲ Show 20 Lines • Show All 100 Lines • ▼ Show 20 Lines | function TestMoveIntoFormationWhileAttacking() | ||||
controllerAI.OnCreate(); | controllerAI.OnCreate(); | ||||
controllerFormation.SetMembers(units); | controllerFormation.SetMembers(units); | ||||
controllerAI.Attack(enemy, []); | controllerAI.Attack(enemy, []); | ||||
for (var ent of unitAIs) | for (var ent of unitAIs) | ||||
TS_ASSERT_EQUALS(unitAI.fsmStateName, "INDIVIDUAL.COMBAT.ATTACKING"); | TS_ASSERT_EQUALS(ent.fsmStateName, "INDIVIDUAL.COMBAT.ATTACKING"); | ||||
controllerAI.MoveIntoFormation({"name": "Circle"}); | controllerAI.MoveIntoFormation({"name": "Circle"}); | ||||
// let all units be in position | // let all units be in position | ||||
for (var ent of unitAIs) | for (var ent of unitAIs) | ||||
controllerFormation.SetInPosition(ent); | controllerFormation.SetInPosition(ent); | ||||
for (var ent of unitAIs) | for (var ent of unitAIs) | ||||
TS_ASSERT_EQUALS(unitAI.fsmStateName, "INDIVIDUAL.COMBAT.ATTACKING"); | TS_ASSERT_EQUALS(ent.fsmStateName, "INDIVIDUAL.COMBAT.ATTACKING"); | ||||
controllerFormation.Disband(); | controllerFormation.Disband(); | ||||
} | } | ||||
function TestRepeatTime() | |||||
{ | |||||
ResetState(); | |||||
let playerEnt = 10; | |||||
let unitEnt = 20; | |||||
let enemyEnt = 30; | |||||
AddMock(SYSTEM_ENTITY, IID_Timer, { | |||||
SetInterval: function() { }, | |||||
SetTimeout: function() { }, | |||||
GetTime: function() { return 2000; }, | |||||
}); | |||||
AddMock(SYSTEM_ENTITY, IID_PlayerManager, { | |||||
GetPlayerByID: function(id) { return playerEnt; }, | |||||
}); | |||||
AddMock(playerEnt, IID_Player, { | |||||
IsEnemy: function() { return true; }, | |||||
GetEnemies: function() { return []; }, | |||||
}); | |||||
AddMock(unitEnt, IID_Ownership, { | |||||
GetOwner: function() { return 1; }, | |||||
}); | |||||
AddMock(unitEnt, IID_UnitMotion, { | |||||
IsInTargetRange: function(target, min, max) { return true; }, | |||||
StopMoving: function() { }, | |||||
}); | |||||
AddMock(unitEnt, IID_Attack, { | |||||
CanAttack: function(v) { return true; }, | |||||
GetRange: function() { return { "max": 10, "min": 0}; }, | |||||
GetBestAttackAgainst: function(t) { return "Melee"; }, | |||||
GetTimers: function() { return { "prepare": 300, "repeat": 1000 }; }, | |||||
}); | |||||
AddMock(enemyEnt, IID_Health, { | |||||
GetHitpoints: function() { return 40; }, | |||||
}); | |||||
let cmpUnitAI = ConstructComponent(unitEnt, "UnitAI", { "DefaultStance": "aggressive", "FormationController": false }); | |||||
cmpUnitAI.OnCreate(); | |||||
cmpUnitAI.Attack(enemyEnt, []); | |||||
TS_ASSERT_EQUALS(cmpUnitAI.fsmStateName, "INDIVIDUAL.COMBAT.ATTACKING"); | |||||
TS_ASSERT_UNEVAL_EQUALS(cmpUnitAI.attackTimers, { "prepare": 300, "repeat": 1000 }); | |||||
TS_ASSERT_EQUALS(cmpUnitAI.lastAttacked, 2000 - 700); | |||||
AddMock(unitEnt, IID_Attack, { | |||||
GetTimers: function() { return { "prepare": 300, "repeat": 800 }; }, | |||||
}); | |||||
cmpUnitAI.OnValueModification({ "entities": [20], "component": "Attack", "valueNames": ["Attack/Melee/RepeatTime"] }); | |||||
TS_ASSERT_EQUALS(cmpUnitAI.fsmStateName, "INDIVIDUAL.COMBAT.ATTACKING"); | |||||
TS_ASSERT_UNEVAL_EQUALS(cmpUnitAI.attackTimers, { "prepare": 300, "repeat": 800 }); | |||||
TS_ASSERT_EQUALS(cmpUnitAI.lastAttacked, 2000 - 700 * 800 / 1000); | |||||
} | |||||
TestFormationExiting(0); | TestFormationExiting(0); | ||||
TestFormationExiting(1); | TestFormationExiting(1); | ||||
TestFormationExiting(2); | TestFormationExiting(2); | ||||
TestMoveIntoFormationWhileAttacking(); | TestMoveIntoFormationWhileAttacking(); | ||||
TestRepeatTime(); |
Wildfire Games · Phabricator