Ticket #4102: 4102.3.diff

File 4102.3.diff, 22.8 KB (added by Stan, 8 years ago)

Should be nice

  • binaries/data/mods/public/simulation/components/GarrisonHolder.js

     
    6565    this.allowGarrisoning = {};
    6666    this.visibleGarrisonPoints = [];
    6767    if (this.template.VisibleGarrisonPoints)
    68     {
    69         for each (var offset in this.template.VisibleGarrisonPoints)
    70         {
    71             var o = {};
    72             o.x = +offset.X;
    73             o.y = +offset.Y;
    74             o.z = +offset.Z;
    75             this.visibleGarrisonPoints.push({"offset":o, "entity": null});
    76         }
    77     }
     68        for (let name in cmpGarrisonHolder.template.VisibleGarrisonPoints)
     69            cmpGarrisonHolder.visibleGarrisonPoints.push({
     70                "offset": {
     71                    "x": +cmpGarrisonHolder.template.VisibleGarrisonPoints[name].X,
     72                    "y": +cmpGarrisonHolder.template.VisibleGarrisonPoints[name].Y,
     73                    "z": +cmpGarrisonHolder.template.VisibleGarrisonPoints[name].Z
     74                },
     75                "entity": null
     76            });
    7877};
    7978
    8079/**
     
    8281 */
    8382GarrisonHolder.prototype.GetLoadingRange = function()
    8483{
    85     var max = +this.template.LoadingRange;
    86     return { "max": max, "min": 0 };
     84    return { "max": +this.template.LoadingRange, "min": 0 };
    8785};
    8886
    8987/**
     
    9391{
    9492    if (!this.template.Pickup || this.IsFull())
    9593        return false;
    96     var cmpOwner = Engine.QueryInterface(this.entity, IID_Ownership);
     94    let cmpOwner = Engine.QueryInterface(this.entity, IID_Ownership);
    9795    if (!cmpOwner)
    9896        return false;
    99     var player = cmpOwner.GetOwner();
    100     return IsOwnedByPlayer(player, ent);
     97    return IsOwnedByPlayer(cmpOwner.GetOwner(), ent);
    10198};
    10299
    103100
     
    163160 */
    164161GarrisonHolder.prototype.IsGarrisoningAllowed = function()
    165162{
    166     for each (var allow in this.allowGarrisoning)
    167     {
    168         if (!allow)
     163    for (let allow in this.allowGarrisoning)
     164        if (!this.allowGarrisoning[allow])
    169165            return false;
    170     }
     166
    171167    return true;
    172168};
    173169
     
    176172 */
    177173GarrisonHolder.prototype.GetGarrisonedEntitiesCount = function()
    178174{
    179     var count = 0;
    180     for each (var ent in this.entities)
     175    let count = 0;
     176    for (let ent of this.entities)
    181177    {
    182         count++;
    183         var cmpGarrisonHolder = Engine.QueryInterface(ent, IID_GarrisonHolder);
     178        ++count;
     179        let cmpGarrisonHolder = Engine.QueryInterface(ent, IID_GarrisonHolder);
    184180        if (cmpGarrisonHolder)
    185181            count += cmpGarrisonHolder.GetGarrisonedEntitiesCount();
    186182    }
     
    196192    if (!this.IsGarrisoningAllowed())
    197193        return false;
    198194
    199     var cmpIdentity = Engine.QueryInterface(entity, IID_Identity);
     195    let cmpIdentity = Engine.QueryInterface(entity, IID_Identity);
    200196    if (!cmpIdentity)
    201197        return false;
    202     var entityClasses = cmpIdentity.GetClassesList();
    203     return MatchesClassList(entityClasses, this.template.List._string);
     198    return MatchesClassList(cmpIdentity.GetClassesList(), this.template.List._string);
    204199};
    205200
    206201/**
     
    211206 */
    212207GarrisonHolder.prototype.Garrison = function(entity, vgpEntity)
    213208{
    214     var cmpPosition = Engine.QueryInterface(entity, IID_Position);
     209    let cmpPosition = Engine.QueryInterface(entity, IID_Position);
    215210    if (!cmpPosition)
    216211        return false;
    217212
     
    254249        return false;
    255250
    256251    // check the capacity
    257     var extraCount = 0;
    258     var cmpGarrisonHolder = Engine.QueryInterface(entity, IID_GarrisonHolder);
     252    let extraCount = 0;
     253    let cmpGarrisonHolder = Engine.QueryInterface(entity, IID_GarrisonHolder);
    259254    if (cmpGarrisonHolder)
    260255        extraCount += cmpGarrisonHolder.GetGarrisonedEntitiesCount();
    261256    if (this.GetGarrisonedEntitiesCount() + extraCount >= this.GetCapacity())
     
    263258
    264259    if (!this.timer && this.GetHealRate() > 0)
    265260    {
    266         var cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer);
    267         this.timer = cmpTimer.SetTimeout(this.entity, IID_GarrisonHolder, "HealTimeout", 1000, {});
     261        let cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer);
     262        if(cmpTimer)
     263            this.timer = cmpTimer.SetTimeout(this.entity, IID_GarrisonHolder, "HealTimeout", 1000, {});
    268264    }
    269265
    270266    // Actual garrisoning happens here
    271267    this.entities.push(entity);
    272268    this.UpdateGarrisonFlag();
    273     var cmpProductionQueue = Engine.QueryInterface(entity, IID_ProductionQueue);
     269    let cmpProductionQueue = Engine.QueryInterface(entity, IID_ProductionQueue);
    274270    if (cmpProductionQueue)
    275271        cmpProductionQueue.PauseProduction();
    276272
    277     var cmpAura = Engine.QueryInterface(entity, IID_Auras);
     273    let cmpAura = Engine.QueryInterface(entity, IID_Auras);
    278274    if (cmpAura && cmpAura.HasGarrisonAura())
    279275        cmpAura.ApplyGarrisonBonus(this.entity);
    280276
    281     Engine.PostMessage(this.entity, MT_GarrisonedUnitsChanged, { "added" : [entity], "removed": [] });
     277    Engine.PostMessage(this.entity, MT_GarrisonedUnitsChanged, { "added": [entity], "removed": [] });
    282278
    283     var cmpUnitAI = Engine.QueryInterface(entity, IID_UnitAI);
     279    let cmpUnitAI = Engine.QueryInterface(entity, IID_UnitAI);
    284280    if (cmpUnitAI && cmpUnitAI.IsUnderAlert())
    285         Engine.PostMessage(cmpUnitAI.GetAlertRaiser(), MT_UnitGarrisonedAfterAlert, {"holder": this.entity, "unit": entity});
     281        Engine.PostMessage(cmpUnitAI.GetAlertRaiser(), MT_UnitGarrisonedAfterAlert, { "holder": this.entity, "unit": entity });
    286282
    287283    return true;
    288284};
     
    294290 */
    295291GarrisonHolder.prototype.Eject = function(entity, forced)
    296292{
    297     var entityIndex = this.entities.indexOf(entity);
     293    let entityIndex = this.entities.indexOf(entity);
    298294    // Error: invalid entity ID, usually it's already been ejected
    299295    if (entityIndex == -1)
    300296        return false; // Fail
    301297
    302298    // Find spawning location
    303     var cmpFootprint = Engine.QueryInterface(this.entity, IID_Footprint);
    304     var cmpHealth = Engine.QueryInterface(this.entity, IID_Health);
    305     var cmpIdentity = Engine.QueryInterface(this.entity, IID_Identity);
     299    let cmpFootprint = Engine.QueryInterface(this.entity, IID_Footprint);
     300    let cmpHealth = Engine.QueryInterface(this.entity, IID_Health);
     301    let cmpIdentity = Engine.QueryInterface(this.entity, IID_Identity);
    306302    // If the garrisonHolder is a sinking ship, restrict the location to the intersection of both passabilities
    307303    // TODO: should use passability classes to be more generic
    308     if ((!cmpHealth || cmpHealth.GetHitpoints() == 0) && cmpIdentity && cmpIdentity.HasClass("Ship"))
    309         var pos = cmpFootprint.PickSpawnPointBothPass(entity);
    310     else
    311         var pos = cmpFootprint.PickSpawnPoint(entity);
     304    let pos = (!cmpHealth || cmpHealth.GetHitpoints() === 0) && cmpIdentity && cmpIdentity.HasClass("Ship") ?
     305        cmpFootprint.PickSpawnPointBothPass(entity) :
     306        cmpFootprint.PickSpawnPoint(entity);
    312307
    313308    if (pos.y < 0)
    314309    {
     
    315310        // Error: couldn't find space satisfying the unit's passability criteria
    316311        if (forced)
    317312        {   // If ejection is forced, we need to continue, so use center of the building
    318             var cmpPosition = Engine.QueryInterface(this.entity, IID_Position);
     313            let cmpPosition = Engine.QueryInterface(this.entity, IID_Position);
    319314            pos = cmpPosition.GetPosition();
    320315        }
    321316        else
     
    324319        }
    325320    }
    326321
    327     var cmpNewPosition = Engine.QueryInterface(entity, IID_Position);
     322    let cmpNewPosition = Engine.QueryInterface(entity, IID_Position);
    328323    this.entities.splice(entityIndex, 1);
    329324
    330     for (var vgp of this.visibleGarrisonPoints)
     325    let cmpUnitAI = Engine.QueryInterface(entity, IID_UnitAI);
     326    for (let vgp of this.visibleGarrisonPoints)
    331327    {
    332328        if (vgp.entity != entity)
    333329            continue;
    334330        cmpNewPosition.SetTurretParent(INVALID_ENTITY, new Vector3D());
    335         var cmpUnitAI = Engine.QueryInterface(entity, IID_UnitAI);
    336331        if (cmpUnitAI)
    337332            cmpUnitAI.ResetTurretStance();
    338333        vgp.entity = null;
     
    339334        break;
    340335    }
    341336
    342     var cmpUnitAI = Engine.QueryInterface(entity, IID_UnitAI);
    343337    if (cmpUnitAI)
    344338        cmpUnitAI.Ungarrison();
    345339
    346     var cmpProductionQueue = Engine.QueryInterface(entity, IID_ProductionQueue);
     340    let cmpProductionQueue = Engine.QueryInterface(entity, IID_ProductionQueue);
    347341    if (cmpProductionQueue)
    348342        cmpProductionQueue.UnpauseProduction();
    349343
    350     var cmpAura = Engine.QueryInterface(entity, IID_Auras);
     344    let cmpAura = Engine.QueryInterface(entity, IID_Auras);
    351345    if (cmpAura && cmpAura.HasGarrisonAura())
    352346        cmpAura.RemoveGarrisonBonus(this.entity);
    353347
     
    366360 */
    367361GarrisonHolder.prototype.OrderWalkToRallyPoint = function(entities)
    368362{
    369     var cmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership);
    370     var cmpRallyPoint = Engine.QueryInterface(this.entity, IID_RallyPoint);
     363    let cmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership);
     364    let cmpRallyPoint = Engine.QueryInterface(this.entity, IID_RallyPoint);
    371365    if (cmpRallyPoint)
    372366    {
    373         var rallyPos = cmpRallyPoint.GetPositions()[0];
     367        let rallyPos = cmpRallyPoint.GetPositions()[0];
    374368        if (rallyPos)
    375369        {
    376             var commands = GetRallyPointCommands(cmpRallyPoint, entities);
     370            let commands = GetRallyPointCommands(cmpRallyPoint, entities);
    377371            // ignore the rally point if it is autogarrison
    378372            if (commands[0].type == "garrison" && commands[0].target == this.entity)
    379373                return;
    380             for each (var com in commands)
    381             {
    382                 ProcessCommand(cmpOwnership.GetOwner(), com);
    383             }
     374            for (let command of commands)
     375                ProcessCommand(cmpOwnership.GetOwner(), command);
    384376        }
    385377    }
    386378};
     
    396388    if (!this.IsGarrisoningAllowed() && !forced)
    397389        return false;
    398390
    399     var ejectedEntities = [];
    400     var success = true;
    401     var failedRadius;
    402     for (var entity of entities)
     391    let ejectedEntities = [];
     392    let success = true;
     393    let failedRadius;
     394    for (let entity of entities)
    403395    {
    404396        if (failedRadius !== undefined)
    405397        {
    406             var cmpObstruction = Engine.QueryInterface(entity, IID_Obstruction);
    407             var radius = cmpObstruction ? cmpObstruction.GetUnitRadius() : 0;
     398            let cmpObstruction = Engine.QueryInterface(entity, IID_Obstruction);
     399            let radius = cmpObstruction ? cmpObstruction.GetUnitRadius() : 0;
    408400            if (radius >= failedRadius)
    409401                continue;
    410402        }
     
    411403
    412404        if (this.Eject(entity, forced))
    413405        {
    414             var cmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership);
    415             var cmpEntOwnership = Engine.QueryInterface(entity, IID_Ownership);
     406            let cmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership);
     407            let cmpEntOwnership = Engine.QueryInterface(entity, IID_Ownership);
    416408            if (cmpOwnership && cmpEntOwnership && cmpOwnership.GetOwner() == cmpEntOwnership.GetOwner())
    417409                ejectedEntities.push(entity);
    418410        }
     
    423415                failedRadius = Math.min(failedRadius, radius);
    424416            else
    425417            {
    426                 var cmpObstruction = Engine.QueryInterface(entity, IID_Obstruction);
     418                let cmpObstruction = Engine.QueryInterface(entity, IID_Obstruction);
    427419                failedRadius = cmpObstruction ? cmpObstruction.GetUnitRadius() : 0;
    428420            }
    429421        }
     
    454446 */
    455447GarrisonHolder.prototype.UnloadTemplate = function(extendedTemplate, all, forced)
    456448{
    457     var index = extendedTemplate.indexOf("&");
     449    let index = extendedTemplate.indexOf("&");
    458450    if (index == -1)
    459451        return false;
    460452
    461     var owner = +extendedTemplate.slice(1,index);
    462     var template = extendedTemplate.slice(index+1);
     453    let owner = +extendedTemplate.slice(1,index);
     454    let template = extendedTemplate.slice(index+1);
    463455
    464     var entities = [];
    465     var cmpTemplateManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_TemplateManager);
    466     for each (var entity in this.entities)
     456    let entities = [];
     457    let cmpTemplateManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_TemplateManager);
     458    for (let entity of this.entities)
    467459    {
    468         var cmpIdentity = Engine.QueryInterface(entity, IID_Identity);
     460        let cmpIdentity = Engine.QueryInterface(entity, IID_Identity);
    469461
    470462        // Units with multiple ranks are grouped together.
    471         var name = cmpIdentity.GetSelectionGroupName()
    472                    || cmpTemplateManager.GetCurrentTemplateName(entity);
     463        let name = cmpIdentity.GetSelectionGroupName() || cmpTemplateManager.GetCurrentTemplateName(entity);
    473464
    474465        if (name != template)
    475466            continue;
     
    493484 */
    494485GarrisonHolder.prototype.UnloadAllByOwner = function(owner, forced)
    495486{
    496     var entities = this.entities.filter(ent => {
    497         var cmpOwnership = Engine.QueryInterface(ent, IID_Ownership);
     487    let entities = this.entities.filter(ent => {
     488        let cmpOwnership = Engine.QueryInterface(ent, IID_Ownership);
    498489        return cmpOwnership && cmpOwnership.GetOwner() == owner;
    499490    });
    500491    return this.PerformEject(entities, forced);
     
    507498 */
    508499GarrisonHolder.prototype.UnloadAll = function(forced)
    509500{
    510     var entities = this.entities.slice();
    511     return this.PerformEject(entities, forced);
     501    return this.PerformEject(this.entities.slice(), forced);
    512502};
    513503
    514504/**
     
    518508GarrisonHolder.prototype.OnHealthChanged = function(msg)
    519509{
    520510    if (!this.HasEnoughHealth() && this.entities.length)
    521     {
    522         var entities = this.entities.slice();
    523         this.EjectOrKill(entities);
    524     }
     511        this.EjectOrKill(this.entities.slice());
    525512};
    526513
    527514/**
     
    529516 */
    530517GarrisonHolder.prototype.HasEnoughHealth = function()
    531518{
    532     var cmpHealth = Engine.QueryInterface(this.entity, IID_Health);
    533     var hitpoints = cmpHealth.GetHitpoints();
    534     var maxHitpoints = cmpHealth.GetMaxHitpoints();
    535     var ejectHitpoints = Math.floor((+this.template.EjectHealth) * maxHitpoints);
     519    let cmpHealth = Engine.QueryInterface(this.entity, IID_Health);
     520    let hitpoints = cmpHealth.GetHitpoints();
     521    let maxHitpoints = cmpHealth.GetMaxHitpoints();
     522    let ejectHitpoints = Math.floor((+this.template.EjectHealth) * maxHitpoints);
    536523    return hitpoints > ejectHitpoints;
    537524};
    538525
     
    541528 */
    542529GarrisonHolder.prototype.HealTimeout = function(data)
    543530{
    544     if (this.entities.length == 0)
     531    let cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer);
     532    if (!this.entities.length)
    545533    {
    546         var cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer);
    547534        cmpTimer.CancelTimer(this.timer);
    548535        this.timer = undefined;
     536        return;
    549537    }
    550     else
     538
     539    for (let entity of this.entities)
    551540    {
    552         for each (var entity in this.entities)
     541        let cmpHealth = Engine.QueryInterface(entity, IID_Health);
     542        if (cmpHealth)
    553543        {
    554             var cmpHealth = Engine.QueryInterface(entity, IID_Health);
    555             if (cmpHealth)
    556             {
    557                 // We do not want to heal unhealable units
    558                 if (!cmpHealth.IsUnhealable())
    559                     cmpHealth.Increase(this.GetHealRate());
    560             }
     544            // We do not want to heal unhealable units
     545            if (!cmpHealth.IsUnhealable())
     546                cmpHealth.Increase(this.GetHealRate());
    561547        }
    562         var cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer);
    563         this.timer = cmpTimer.SetTimeout(this.entity, IID_GarrisonHolder, "HealTimeout", 1000, {});
    564548    }
     549    this.timer = cmpTimer.SetTimeout(this.entity, IID_GarrisonHolder, "HealTimeout", 1000, {});
    565550};
    566551
    567552GarrisonHolder.prototype.UpdateGarrisonFlag = function()
     
    580565{
    581566    if (this.timer)
    582567    {
    583         var cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer);
     568        let cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer);
    584569        cmpTimer.CancelTimer(this.timer);
    585570    }
    586571};
     
    595580    // the ownership change may be on the garrisonholder
    596581    if (this.entity == msg.entity)
    597582    {
    598         var entities = [];
    599         for each (var entity in this.entities)
     583        let entities = [];
     584        for (let entity of this.entities)
    600585        {
    601586            if (msg.to == -1 || !IsOwnedByMutualAllyOfEntity(this.entity, entity))
    602587                entities.push(entity);
     
    607592    }
    608593
    609594    // or on some of its garrisoned units
    610     var entityIndex = this.entities.indexOf(msg.entity);
     595    let entityIndex = this.entities.indexOf(msg.entity);
    611596    if (entityIndex != -1)
    612597    {
    613598        // If the entity is dead, remove it directly instead of ejecting the corpse
    614         var cmpHealth = Engine.QueryInterface(msg.entity, IID_Health);
    615         if (cmpHealth && cmpHealth.GetHitpoints() == 0)
     599        let cmpHealth = Engine.QueryInterface(msg.entity, IID_Health);
     600        if (cmpHealth && cmpHealth.GetHitpoints() === 0)
    616601        {
    617602            this.entities.splice(entityIndex, 1);
    618603            Engine.PostMessage(this.entity, MT_GarrisonedUnitsChanged, { "added" : [], "removed": [msg.entity] });
    619604            this.UpdateGarrisonFlag();
    620605
    621             for (var pt of this.visibleGarrisonPoints)
     606            for (let pt of this.visibleGarrisonPoints)
    622607                if (pt.entity == msg.entity)
    623608                    pt.entity = null;
    624609        }
     
    632617 */
    633618GarrisonHolder.prototype.OnGlobalEntityRenamed = function(msg)
    634619{
    635     var entityIndex = this.entities.indexOf(msg.entity);
     620    let entityIndex = this.entities.indexOf(msg.entity);
    636621    if (entityIndex != -1)
    637622    {
    638623        let vgpRenamed;
     
    671656 */
    672657GarrisonHolder.prototype.OnDiplomacyChanged = function()
    673658{
    674     var entities = this.entities.filter(ent => !IsOwnedByMutualAllyOfEntity(this.entity, ent));
     659    let entities = this.entities.filter(ent => !IsOwnedByMutualAllyOfEntity(this.entity, ent));
    675660    this.EjectOrKill(entities);
    676661};
    677662
     
    686671    // is inside a holder which kills its entities, so do not eject)
    687672    if (cmpPosition.IsInWorld())
    688673    {
    689         var cmpGarrisonHolder = this;
    690         var ejectables = entities.filter(function(ent) { return cmpGarrisonHolder.IsEjectable(ent); });
     674        let ejectables = entities.filter(ent => this.IsEjectable(ent), this);
    691675        if (ejectables.length)
    692676            this.PerformEject(ejectables, false);
    693677    }
    694678
    695679    // And destroy all remaining entities
    696     var killedEntities = [];
    697     for each (var entity in entities)
     680    let killedEntities = [];
     681    for (let entity of entities)
    698682    {
    699         var entityIndex = this.entities.indexOf(entity);
     683        let entityIndex = this.entities.indexOf(entity);
    700684        if (entityIndex == -1)
    701685            continue;
    702         var cmpHealth = Engine.QueryInterface(entity, IID_Health);
     686        let cmpHealth = Engine.QueryInterface(entity, IID_Health);
    703687        if (cmpHealth)
    704688            cmpHealth.Kill();
    705689        this.entities.splice(entityIndex, 1);
     
    707691    }
    708692
    709693    if (killedEntities.length > 0)
    710         Engine.PostMessage(this.entity, MT_GarrisonedUnitsChanged, { "added" : [], "removed" : killedEntities });
     694        Engine.PostMessage(this.entity, MT_GarrisonedUnitsChanged, { "added": [], "removed": killedEntities });
    711695    this.UpdateGarrisonFlag();
    712696};
    713697
     
    716700 */
    717701GarrisonHolder.prototype.IsEjectable = function(entity)
    718702{
    719     var ejectableClasses = this.template.EjectClassesOnDestroy._string;
     703    let ejectableClasses = this.template.EjectClassesOnDestroy._string;
    720704    ejectableClasses = ejectableClasses ? ejectableClasses.split(/\s+/) : [];
    721     var entityClasses = (Engine.QueryInterface(entity, IID_Identity)).GetClassesList();
    722     for each (var ejectableClass in ejectableClasses)
    723         if (entityClasses.indexOf(ejectableClass) != -1)
    724             return true;
    725 
    726     return false;
     705    let entityClasses = (Engine.QueryInterface(entity, IID_Identity)).GetClassesList();
     706    return ejectableClasses.some(ejectableClass => entityClasses.indexOf(ejectableClass) != -1);
    727707};
    728708
    729709/**
     
    744724};
    745725
    746726Engine.RegisterComponentType(IID_GarrisonHolder, "GarrisonHolder", GarrisonHolder);
    747 
  • binaries/data/mods/public/simulation/components/tests/test_GarrisonHolder.js

     
     1Engine.LoadHelperScript("ValueModification.js");
     2Engine.LoadHelperScript("Player.js");
     3Engine.LoadComponentScript("interfaces/GarrisonHolder.js");
     4Engine.LoadComponentScript("GarrisonHolder.js");
     5Engine.LoadComponentScript("interfaces/AuraManager.js");
     6Engine.LoadComponentScript("interfaces/Auras.js");
     7Engine.LoadComponentScript("interfaces/Heal.js");
     8Engine.LoadComponentScript("interfaces/Health.js");
     9Engine.LoadComponentScript("interfaces/ProductionQueue.js")
     10Engine.LoadComponentScript("interfaces/TechnologyManager.js");
     11Engine.LoadComponentScript("interfaces/Timer.js");
     12Engine.LoadComponentScript("interfaces/UnitAI.js")
     13Engine.LoadComponentScript("interfaces/RallyPoint.js");
     14
     15const garrisonedEntitiesList = [25, 26, 27];
     16const garrisonHolderId = 15;
     17const unitToGarrisonId = 24;
     18const classes = "Infantry+Cavalry";
     19
     20ResetState();
     21
     22let holder = garrisonHolderId;
     23let unit = unitToGarrisonId;
     24
     25AddMock(holder, IID_Footprint, {
     26    "PickSpawnPointBothPass": () => ({"x": 4, "y": 3}),
     27    "PickSpawnPoint": () => ({"x": 4, "y": 3}),
     28});
     29
     30AddMock(holder, IID_Health, {
     31    "GetHitpoints": () => 500,
     32    "GetMaxHitpoints": () => 600,
     33});
     34AddMock(holder, IID_Position, {
     35    "SetTurretParent": () => {},
     36    "GetTurretParent": () => INVALID_ENTITY,
     37    "GetPosition": () => new Vector3D(),
     38    "GetPosition2D": () => new Vector2D(),
     39    "GetRotation": () => ({ "y": 0 }),
     40    "IsInWorld": () => true,
     41    "GetHeightOffset": () => 0,
     42});
     43AddMock(holder, IID_Obstruction, {
     44    "GetUnitRadius": () => 0,
     45});
     46AddMock(SYSTEM_ENTITY, IID_Timer, {
     47    GetTime: () => 0,
     48    SetTimeout: (ent, iid, funcname, time, data) => 0,
     49});
     50
     51for (let i = 24; i < 28; ++i)
     52{
     53    AddMock(holder, IID_Health, {
     54        "GetHitpoints": () => 190,
     55        "GetMaxHitpoints": () => 200,
     56    });
     57    AddMock(i, IID_Identity, {
     58        "GetClassesList": () => "Infantry+Cavalry",
     59    });
     60    AddMock(i, IID_Ownership, {
     61        "GetOwner": () => 1,
     62    });
     63    AddMock(i, IID_UnitAI, {
     64        "SetTurretStance": () => {},
     65        "CanGarrison": () => true,
     66        "IsUnderAlert": () => false,
     67        "ResetTurretStance": () => {},
     68        "Ungarrison" : () => {},
     69    });
     70    AddMock(i, IID_Position, {
     71        "GetHeightOffset": () => 0,
     72        "GetPosition": () => new Vector3D(),
     73        "GetPosition2D": () => new Vector2D(),
     74        "GetRotation": () => ({ "y": 0 }),
     75        "GetTurretParent": () => INVALID_ENTITY,
     76        "IsInWorld": () => true,
     77        "JumpTo": () => {},
     78        "MoveOutOfWorld": () => {},
     79        "SetHeightOffset":() => {},
     80        "SetTurretParent": () => {},
     81    });
     82
     83}
     84
     85
     86
     87
     88let cmpGarrisonHolder = ConstructComponent(holder, "GarrisonHolder", {
     89    "Max" : 4,
     90    "List": {"_string": "Infantry+Cavalry"},
     91    "EjectHealth": 0.1,
     92    "EjectClassesOnDestroy": {"_string": "Infantry"},
     93    "BuffHeal": 1,
     94    "LoadingRange": 2.1,
     95    "Pickup": false,
     96    "VisibleGarrisonPoints": {
     97        "archer1": {
     98            "X": 12,
     99            "Y": 5,
     100            "Z": 6
     101        },
     102        "archer2": {
     103            "X": 15,
     104            "Y": 5,
     105            "Z": 6
     106        }
     107    }
     108});
     109
     110cmpGarrisonHolder.Init();
     111
     112
     113TS_ASSERT_UNEVAL_EQUALS(cmpGarrisonHolder.GetLoadingRange(), { "max": 2.1, "min": 0 });
     114TS_ASSERT_UNEVAL_EQUALS(cmpGarrisonHolder.GetEntities(), []);
     115TS_ASSERT_UNEVAL_EQUALS(cmpGarrisonHolder.GetHealRate(), 1);
     116TS_ASSERT_UNEVAL_EQUALS(cmpGarrisonHolder.GetAllowedClasses(), classes);
     117TS_ASSERT_EQUALS(cmpGarrisonHolder.GetCapacity(), 4);
     118TS_ASSERT_EQUALS(cmpGarrisonHolder.GetGarrisonedEntitiesCount(), 0);
     119TS_ASSERT_EQUALS(cmpGarrisonHolder.CanPickup(unitToGarrisonId), false);
     120TS_ASSERT_EQUALS(cmpGarrisonHolder.IsFull(), false);
     121TS_ASSERT_EQUALS(cmpGarrisonHolder.Garrison(unitToGarrisonId), true);
     122for (let entity of garrisonedEntitiesList)
     123    TS_ASSERT_EQUALS(cmpGarrisonHolder.Garrison(entity), true);
     124TS_ASSERT_EQUALS(cmpGarrisonHolder.IsFull(), true);
     125TS_ASSERT_UNEVAL_EQUALS(cmpGarrisonHolder.GetEntities(), [24,25,26,27]);
     126TS_ASSERT_EQUALS(cmpGarrisonHolder.GetGarrisonedEntitiesCount(), 4);
     127TS_ASSERT_EQUALS(cmpGarrisonHolder.Unload(25), true);
     128TS_ASSERT_UNEVAL_EQUALS(cmpGarrisonHolder.GetEntities(), [24,26,27]);
     129TS_ASSERT_EQUALS(cmpGarrisonHolder.GetGarrisonedEntitiesCount(), 3);
     130TS_ASSERT_EQUALS(cmpGarrisonHolder.IsFull(), false);
     131TS_ASSERT_EQUALS(cmpGarrisonHolder.UnloadAll(), true);
     132TS_ASSERT_UNEVAL_EQUALS(cmpGarrisonHolder.GetEntities(), []);
     133
     134