Ticket #4102: 4102.4.diff

File 4102.4.diff, 23.5 KB (added by Stan, 8 years ago)

Some other stuff elexis reported

  • 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 this.template.VisibleGarrisonPoints)
     69            this.visibleGarrisonPoints.push({
     70                "offset": {
     71                    "x": +this.template.VisibleGarrisonPoints[name].X,
     72                    "y": +this.template.VisibleGarrisonPoints[name].Y,
     73                    "z": +this.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
     
    250245        return false;
    251246
    252247    // Check if the unit is allowed to be garrisoned inside the building
    253     if(!this.AllowedToGarrison(entity))
     248    if (!this.AllowedToGarrison(entity))
    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
    316         if (forced)
    317         {   // If ejection is forced, we need to continue, so use center of the building
    318             var cmpPosition = Engine.QueryInterface(this.entity, IID_Position);
     311        if (!forced)
     312            return false;
     313        // If ejection is forced, we need to continue, so use center of the building
     314        let cmpPosition = Engine.QueryInterface(this.entity, IID_Position);
     315        if (cmpPosition)
    319316            pos = cmpPosition.GetPosition();
    320         }
    321         else
    322         {   // Fail
    323             return false;
    324         }
    325317    }
    326318
    327     var cmpNewPosition = Engine.QueryInterface(entity, IID_Position);
     319    let cmpNewPosition = Engine.QueryInterface(entity, IID_Position);
    328320    this.entities.splice(entityIndex, 1);
    329321
    330     for (var vgp of this.visibleGarrisonPoints)
     322    let cmpUnitAI = Engine.QueryInterface(entity, IID_UnitAI);
     323    for (let vgp of this.visibleGarrisonPoints)
    331324    {
    332325        if (vgp.entity != entity)
    333326            continue;
    334327        cmpNewPosition.SetTurretParent(INVALID_ENTITY, new Vector3D());
    335         var cmpUnitAI = Engine.QueryInterface(entity, IID_UnitAI);
    336328        if (cmpUnitAI)
    337329            cmpUnitAI.ResetTurretStance();
    338330        vgp.entity = null;
     
    339331        break;
    340332    }
    341333
    342     var cmpUnitAI = Engine.QueryInterface(entity, IID_UnitAI);
    343334    if (cmpUnitAI)
    344335        cmpUnitAI.Ungarrison();
    345336
    346     var cmpProductionQueue = Engine.QueryInterface(entity, IID_ProductionQueue);
     337    let cmpProductionQueue = Engine.QueryInterface(entity, IID_ProductionQueue);
    347338    if (cmpProductionQueue)
    348339        cmpProductionQueue.UnpauseProduction();
    349340
    350     var cmpAura = Engine.QueryInterface(entity, IID_Auras);
     341    let cmpAura = Engine.QueryInterface(entity, IID_Auras);
    351342    if (cmpAura && cmpAura.HasGarrisonAura())
    352343        cmpAura.RemoveGarrisonBonus(this.entity);
    353344
     
    356347    cmpNewPosition.SetHeightOffset(0);
    357348    // TODO: what direction should they face in?
    358349
    359     Engine.PostMessage(this.entity, MT_GarrisonedUnitsChanged, { "added" : [], "removed": [entity] });
     350    Engine.PostMessage(this.entity, MT_GarrisonedUnitsChanged, { "added": [], "removed": [entity] });
    360351
    361352    return true;
    362353};
     
    366357 */
    367358GarrisonHolder.prototype.OrderWalkToRallyPoint = function(entities)
    368359{
    369     var cmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership);
    370     var cmpRallyPoint = Engine.QueryInterface(this.entity, IID_RallyPoint);
     360    let cmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership);
     361    let cmpRallyPoint = Engine.QueryInterface(this.entity, IID_RallyPoint);
    371362    if (cmpRallyPoint)
    372363    {
    373         var rallyPos = cmpRallyPoint.GetPositions()[0];
     364        let rallyPos = cmpRallyPoint.GetPositions()[0];
    374365        if (rallyPos)
    375366        {
    376             var commands = GetRallyPointCommands(cmpRallyPoint, entities);
     367            let commands = GetRallyPointCommands(cmpRallyPoint, entities);
    377368            // ignore the rally point if it is autogarrison
    378369            if (commands[0].type == "garrison" && commands[0].target == this.entity)
    379370                return;
    380             for each (var com in commands)
    381             {
    382                 ProcessCommand(cmpOwnership.GetOwner(), com);
    383             }
     371            for (let command of commands)
     372                ProcessCommand(cmpOwnership.GetOwner(), command);
    384373        }
    385374    }
    386375};
     
    396385    if (!this.IsGarrisoningAllowed() && !forced)
    397386        return false;
    398387
    399     var ejectedEntities = [];
    400     var success = true;
    401     var failedRadius;
    402     for (var entity of entities)
     388    let ejectedEntities = [];
     389    let success = true;
     390    let failedRadius;
     391    for (let entity of entities)
    403392    {
    404393        if (failedRadius !== undefined)
    405394        {
    406             var cmpObstruction = Engine.QueryInterface(entity, IID_Obstruction);
    407             var radius = cmpObstruction ? cmpObstruction.GetUnitRadius() : 0;
     395            let cmpObstruction = Engine.QueryInterface(entity, IID_Obstruction);
     396            let radius = cmpObstruction ? cmpObstruction.GetUnitRadius() : 0;
    408397            if (radius >= failedRadius)
    409398                continue;
    410399        }
     
    411400
    412401        if (this.Eject(entity, forced))
    413402        {
    414             var cmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership);
    415             var cmpEntOwnership = Engine.QueryInterface(entity, IID_Ownership);
     403            let cmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership);
     404            let cmpEntOwnership = Engine.QueryInterface(entity, IID_Ownership);
    416405            if (cmpOwnership && cmpEntOwnership && cmpOwnership.GetOwner() == cmpEntOwnership.GetOwner())
    417406                ejectedEntities.push(entity);
    418407        }
     
    423412                failedRadius = Math.min(failedRadius, radius);
    424413            else
    425414            {
    426                 var cmpObstruction = Engine.QueryInterface(entity, IID_Obstruction);
     415                let cmpObstruction = Engine.QueryInterface(entity, IID_Obstruction);
    427416                failedRadius = cmpObstruction ? cmpObstruction.GetUnitRadius() : 0;
    428417            }
    429418        }
     
    454443 */
    455444GarrisonHolder.prototype.UnloadTemplate = function(extendedTemplate, all, forced)
    456445{
    457     var index = extendedTemplate.indexOf("&");
     446    let index = extendedTemplate.indexOf("&");
    458447    if (index == -1)
    459448        return false;
    460449
    461     var owner = +extendedTemplate.slice(1,index);
    462     var template = extendedTemplate.slice(index+1);
     450    let owner = +extendedTemplate.slice(1,index);
     451    let template = extendedTemplate.slice(index+1);
    463452
    464     var entities = [];
    465     var cmpTemplateManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_TemplateManager);
    466     for each (var entity in this.entities)
     453    let entities = [];
     454    let cmpTemplateManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_TemplateManager);
     455    for (let entity of this.entities)
    467456    {
    468         var cmpIdentity = Engine.QueryInterface(entity, IID_Identity);
     457        let cmpIdentity = Engine.QueryInterface(entity, IID_Identity);
    469458
    470459        // Units with multiple ranks are grouped together.
    471         var name = cmpIdentity.GetSelectionGroupName()
    472                    || cmpTemplateManager.GetCurrentTemplateName(entity);
     460        let name = cmpIdentity.GetSelectionGroupName() || cmpTemplateManager.GetCurrentTemplateName(entity);
    473461
    474462        if (name != template)
    475463            continue;
     
    493481 */
    494482GarrisonHolder.prototype.UnloadAllByOwner = function(owner, forced)
    495483{
    496     var entities = this.entities.filter(ent => {
    497         var cmpOwnership = Engine.QueryInterface(ent, IID_Ownership);
     484    let entities = this.entities.filter(ent => {
     485        let cmpOwnership = Engine.QueryInterface(ent, IID_Ownership);
    498486        return cmpOwnership && cmpOwnership.GetOwner() == owner;
    499487    });
    500488    return this.PerformEject(entities, forced);
     
    507495 */
    508496GarrisonHolder.prototype.UnloadAll = function(forced)
    509497{
    510     var entities = this.entities.slice();
    511     return this.PerformEject(entities, forced);
     498    return this.PerformEject(this.entities.slice(), forced);
    512499};
    513500
    514501/**
     
    518505GarrisonHolder.prototype.OnHealthChanged = function(msg)
    519506{
    520507    if (!this.HasEnoughHealth() && this.entities.length)
    521     {
    522         var entities = this.entities.slice();
    523         this.EjectOrKill(entities);
    524     }
     508        this.EjectOrKill(this.entities.slice());
    525509};
    526510
    527511/**
     
    529513 */
    530514GarrisonHolder.prototype.HasEnoughHealth = function()
    531515{
    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);
     516    let cmpHealth = Engine.QueryInterface(this.entity, IID_Health);
     517    let hitpoints = cmpHealth.GetHitpoints();
     518    let maxHitpoints = cmpHealth.GetMaxHitpoints();
     519    let ejectHitpoints = Math.floor((+this.template.EjectHealth) * maxHitpoints);
    536520    return hitpoints > ejectHitpoints;
    537521};
    538522
     
    541525 */
    542526GarrisonHolder.prototype.HealTimeout = function(data)
    543527{
    544     if (this.entities.length == 0)
     528    let cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer);
     529    if (!this.entities.length)
    545530    {
    546         var cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer);
    547531        cmpTimer.CancelTimer(this.timer);
    548532        this.timer = undefined;
     533        return;
    549534    }
    550     else
     535
     536    for (let entity of this.entities)
    551537    {
    552         for each (var entity in this.entities)
     538        let cmpHealth = Engine.QueryInterface(entity, IID_Health);
     539        if (cmpHealth)
    553540        {
    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             }
     541            // We do not want to heal unhealable units
     542            if (!cmpHealth.IsUnhealable())
     543                cmpHealth.Increase(this.GetHealRate());
    561544        }
    562         var cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer);
    563         this.timer = cmpTimer.SetTimeout(this.entity, IID_GarrisonHolder, "HealTimeout", 1000, {});
    564545    }
     546    this.timer = cmpTimer.SetTimeout(this.entity, IID_GarrisonHolder, "HealTimeout", 1000, {});
    565547};
    566548
    567549GarrisonHolder.prototype.UpdateGarrisonFlag = function()
     
    580562{
    581563    if (this.timer)
    582564    {
    583         var cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer);
    584         cmpTimer.CancelTimer(this.timer);
     565        let cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer);
     566        if (cmpTimer)
     567            cmpTimer.CancelTimer(this.timer);
    585568    }
    586569};
    587570
     
    595578    // the ownership change may be on the garrisonholder
    596579    if (this.entity == msg.entity)
    597580    {
    598         var entities = [];
    599         for each (var entity in this.entities)
     581        let entities = [];
     582        for (let entity of this.entities)
    600583        {
    601584            if (msg.to == -1 || !IsOwnedByMutualAllyOfEntity(this.entity, entity))
    602585                entities.push(entity);
     
    607590    }
    608591
    609592    // or on some of its garrisoned units
    610     var entityIndex = this.entities.indexOf(msg.entity);
     593    let entityIndex = this.entities.indexOf(msg.entity);
    611594    if (entityIndex != -1)
    612595    {
    613596        // 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)
     597        let cmpHealth = Engine.QueryInterface(msg.entity, IID_Health);
     598        if (cmpHealth && cmpHealth.GetHitpoints() === 0)
    616599        {
    617600            this.entities.splice(entityIndex, 1);
    618             Engine.PostMessage(this.entity, MT_GarrisonedUnitsChanged, { "added" : [], "removed": [msg.entity] });
     601            Engine.PostMessage(this.entity, MT_GarrisonedUnitsChanged, { "added": [], "removed": [msg.entity] });
    619602            this.UpdateGarrisonFlag();
    620603
    621             for (var pt of this.visibleGarrisonPoints)
     604            for (let pt of this.visibleGarrisonPoints)
    622605                if (pt.entity == msg.entity)
    623606                    pt.entity = null;
    624607        }
     
    632615 */
    633616GarrisonHolder.prototype.OnGlobalEntityRenamed = function(msg)
    634617{
    635     var entityIndex = this.entities.indexOf(msg.entity);
     618    let entityIndex = this.entities.indexOf(msg.entity);
    636619    if (entityIndex != -1)
    637620    {
    638621        let vgpRenamed;
     
    671654 */
    672655GarrisonHolder.prototype.OnDiplomacyChanged = function()
    673656{
    674     var entities = this.entities.filter(ent => !IsOwnedByMutualAllyOfEntity(this.entity, ent));
     657    let entities = this.entities.filter(ent => !IsOwnedByMutualAllyOfEntity(this.entity, ent));
    675658    this.EjectOrKill(entities);
    676659};
    677660
     
    686669    // is inside a holder which kills its entities, so do not eject)
    687670    if (cmpPosition.IsInWorld())
    688671    {
    689         var cmpGarrisonHolder = this;
    690         var ejectables = entities.filter(function(ent) { return cmpGarrisonHolder.IsEjectable(ent); });
     672        let ejectables = entities.filter(ent => this.IsEjectable(ent), this);
    691673        if (ejectables.length)
    692674            this.PerformEject(ejectables, false);
    693675    }
    694676
    695677    // And destroy all remaining entities
    696     var killedEntities = [];
    697     for each (var entity in entities)
     678    let killedEntities = [];
     679    for (let entity of entities)
    698680    {
    699         var entityIndex = this.entities.indexOf(entity);
     681        let entityIndex = this.entities.indexOf(entity);
    700682        if (entityIndex == -1)
    701683            continue;
    702         var cmpHealth = Engine.QueryInterface(entity, IID_Health);
     684        let cmpHealth = Engine.QueryInterface(entity, IID_Health);
    703685        if (cmpHealth)
    704686            cmpHealth.Kill();
    705687        this.entities.splice(entityIndex, 1);
     
    707689    }
    708690
    709691    if (killedEntities.length > 0)
    710         Engine.PostMessage(this.entity, MT_GarrisonedUnitsChanged, { "added" : [], "removed" : killedEntities });
     692        Engine.PostMessage(this.entity, MT_GarrisonedUnitsChanged, { "added": [], "removed": killedEntities });
    711693    this.UpdateGarrisonFlag();
    712694};
    713695
     
    716698 */
    717699GarrisonHolder.prototype.IsEjectable = function(entity)
    718700{
    719     var ejectableClasses = this.template.EjectClassesOnDestroy._string;
     701    let ejectableClasses = this.template.EjectClassesOnDestroy._string;
    720702    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;
     703    let entityClasses = (Engine.QueryInterface(entity, IID_Identity)).GetClassesList();
     704    return ejectableClasses.some(ejectableClass => entityClasses.indexOf(ejectableClass) != -1);
    727705};
    728706
    729707/**
     
    744722};
    745723
    746724Engine.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, "z": 30 }),
     27    "PickSpawnPoint": () => ({ "x": 4, "y": 3, "z": 30 }),
     28});
     29AddMock(holder, IID_Health, {
     30    "GetHitpoints": () => 500,
     31    "GetMaxHitpoints": () => 600,
     32});
     33AddMock(holder, IID_Obstruction, {
     34    "GetUnitRadius": () => 0,
     35});
     36AddMock(holder, IID_Position, {
     37    "SetTurretParent": () => {},
     38    "GetTurretParent": () => INVALID_ENTITY,
     39    "GetPosition": () => ({ "x": 4, "y": 3, "z": 25 }),
     40    "GetRotation": () => ({ "y": 0 }),
     41    "IsInWorld": () => true,
     42    "GetHeightOffset": () => 0,
     43});
     44
     45AddMock(SYSTEM_ENTITY, IID_Timer, {
     46    GetTime: () => 0,
     47    SetTimeout: (ent, iid, funcname, time, data) => 0,
     48});
     49
     50for (let i = 24; i < 28; ++i)
     51{
     52    AddMock(holder, IID_Health, {
     53        "GetHitpoints": () => 190,
     54        "GetMaxHitpoints": () => 200,
     55    });
     56    AddMock(i, IID_Identity, {
     57        "GetClassesList": () => "Infantry+Cavalry",
     58    });
     59    AddMock(i, IID_Ownership, {
     60        "GetOwner": () => 1,
     61    });
     62    AddMock(i, IID_UnitAI, {
     63        "SetTurretStance": () => {},
     64        "CanGarrison": () => true,
     65        "IsUnderAlert": () => false,
     66        "ResetTurretStance": () => {},
     67        "Ungarrison" : () => {},
     68    });
     69    AddMock(i, IID_Position, {
     70        "GetHeightOffset": () => 0,
     71        "GetPosition": () => ({ "x": 4, "y": 3, "z": 25 }),
     72        "GetRotation": () => ({ "y": 0 }),
     73        "GetTurretParent": () => INVALID_ENTITY,
     74        "IsInWorld": () => true,
     75        "JumpTo": () => {},
     76        "MoveOutOfWorld": () => {},
     77        "SetHeightOffset":() => {},
     78        "SetTurretParent": () => {},
     79    });
     80
     81}
     82
     83
     84
     85
     86let cmpGarrisonHolder = ConstructComponent(holder, "GarrisonHolder", {
     87    "Max" : 4,
     88    "List": {"_string": "Infantry+Cavalry"},
     89    "EjectHealth": 0.1,
     90    "EjectClassesOnDestroy": {"_string": "Infantry"},
     91    "BuffHeal": 1,
     92    "LoadingRange": 2.1,
     93    "Pickup": false,
     94    "VisibleGarrisonPoints": {
     95        "archer1": {
     96            "X": 12,
     97            "Y": 5,
     98            "Z": 6
     99        },
     100        "archer2": {
     101            "X": 15,
     102            "Y": 5,
     103            "Z": 6
     104        }
     105    }
     106});
     107
     108cmpGarrisonHolder.Init();
     109
     110
     111TS_ASSERT_UNEVAL_EQUALS(cmpGarrisonHolder.GetLoadingRange(), { "max": 2.1, "min": 0 });
     112TS_ASSERT_UNEVAL_EQUALS(cmpGarrisonHolder.GetEntities(), []);
     113TS_ASSERT_UNEVAL_EQUALS(cmpGarrisonHolder.GetHealRate(), 1);
     114TS_ASSERT_UNEVAL_EQUALS(cmpGarrisonHolder.GetAllowedClasses(), classes);
     115TS_ASSERT_EQUALS(cmpGarrisonHolder.GetCapacity(), 4);
     116TS_ASSERT_EQUALS(cmpGarrisonHolder.GetGarrisonedEntitiesCount(), 0);
     117TS_ASSERT_EQUALS(cmpGarrisonHolder.CanPickup(unitToGarrisonId), false);
     118TS_ASSERT_EQUALS(cmpGarrisonHolder.IsFull(), false);
     119TS_ASSERT_EQUALS(cmpGarrisonHolder.Garrison(unitToGarrisonId), true);
     120for (let entity of garrisonedEntitiesList)
     121    TS_ASSERT_EQUALS(cmpGarrisonHolder.Garrison(entity), true);
     122TS_ASSERT_EQUALS(cmpGarrisonHolder.IsFull(), true);
     123TS_ASSERT_UNEVAL_EQUALS(cmpGarrisonHolder.GetEntities(), [24,25,26,27]);
     124TS_ASSERT_EQUALS(cmpGarrisonHolder.GetGarrisonedEntitiesCount(), 4);
     125TS_ASSERT_EQUALS(cmpGarrisonHolder.Unload(25), true);
     126TS_ASSERT_UNEVAL_EQUALS(cmpGarrisonHolder.GetEntities(), [24,26,27]);
     127TS_ASSERT_EQUALS(cmpGarrisonHolder.GetGarrisonedEntitiesCount(), 3);
     128TS_ASSERT_EQUALS(cmpGarrisonHolder.IsFull(), false);
     129TS_ASSERT_EQUALS(cmpGarrisonHolder.UnloadAll(), true);
     130TS_ASSERT_UNEVAL_EQUALS(cmpGarrisonHolder.GetEntities(), []);
     131
     132