Ticket #3189: QueryMiragedInterface.diff

File QueryMiragedInterface.diff, 20.6 KB (added by leper, 9 years ago)
  • binaries/data/mods/public/gui/session/selection_details.js

     
    206206        Engine.GetGUIObjectByName("resourceCarryingIcon").hidden = false;
    207207        Engine.GetGUIObjectByName("resourceCarryingText").hidden = false;
    208208        Engine.GetGUIObjectByName("resourceCarryingIcon").sprite = "stretched:session/icons/repair.png";
    209         Engine.GetGUIObjectByName("resourceCarryingText").caption = sprintf(translate("%(amount)s / %(max)s"), { amount: entState.resourceSupply.gatherers.length, max: entState.resourceSupply.maxGatherers }) + "    ";
     209        Engine.GetGUIObjectByName("resourceCarryingText").caption = sprintf(translate("%(amount)s / %(max)s"), { amount: entState.resourceSupply.numGatherers, max: entState.resourceSupply.maxGatherers }) + "    ";
    210210        Engine.GetGUIObjectByName("resourceCarryingIcon").tooltip = translate("Current/max gatherers");
    211211    }
    212212    else
  • binaries/data/mods/public/simulation/components/Fogging.js

     
    115115    // Store valuable information into the mirage component (especially for the GUI)
    116116    var cmpFoundation = Engine.QueryInterface(this.entity, IID_Foundation);
    117117    if (cmpFoundation)
    118         cmpMirage.CopyFoundation(cmpFoundation.GetBuildPercentage());
     118        cmpMirage.CopyFoundation(cmpFoundation);
    119119
    120120    var cmpHealth = Engine.QueryInterface(this.entity, IID_Health);
    121121    if (cmpHealth)
    122         cmpMirage.CopyHealth(
    123             cmpHealth.GetMaxHitpoints(),
    124             cmpHealth.GetHitpoints(),
    125             cmpHealth.IsRepairable() && (cmpHealth.GetHitpoints() < cmpHealth.GetMaxHitpoints())
    126         );
     122        cmpMirage.CopyHealth(cmpHealth);
    127123
    128124    var cmpCapturable = Engine.QueryInterface(this.entity, IID_Capturable);
    129125    if (cmpCapturable)
    130         cmpMirage.CopyCapturable(
    131             cmpCapturable.GetCapturePoints(),
    132             cmpCapturable.GetMaxCapturePoints()
    133         );
     126        cmpMirage.CopyCapturable(cmpCapturable);
    134127
    135128    var cmpResourceSupply = Engine.QueryInterface(this.entity, IID_ResourceSupply);
    136129    if (cmpResourceSupply)
    137         cmpMirage.CopyResourceSupply(
    138             cmpResourceSupply.GetMaxAmount(),
    139             cmpResourceSupply.GetCurrentAmount(),
    140             cmpResourceSupply.GetType(),
    141             cmpResourceSupply.IsInfinite()
    142         );
     130        cmpMirage.CopyResourceSupply(cmpResourceSupply);
    143131
    144132    // Notify the GUI the entity has been replaced by a mirage, in case it is selected at this moment
    145133    var cmpGuiInterface = Engine.QueryInterface(SYSTEM_ENTITY, IID_GuiInterface);
  • binaries/data/mods/public/simulation/components/GuiInterface.js

     
    258258        ret.rotation = cmpPosition.GetRotation();
    259259    }
    260260
    261     var cmpHealth = Engine.QueryInterface(ent, IID_Health);
     261    var cmpHealth = QueryMiragedInterface(ent, IID_Health);
    262262    if (cmpHealth)
    263263    {
    264264        ret.hitpoints = Math.ceil(cmpHealth.GetHitpoints());
     
    266266        ret.needsRepair = cmpHealth.IsRepairable() && (cmpHealth.GetHitpoints() < cmpHealth.GetMaxHitpoints());
    267267        ret.needsHeal = !cmpHealth.IsUnhealable();
    268268    }
    269     if (cmpMirage && cmpMirage.Health())
    270     {
    271         ret.hitpoints = cmpMirage.GetHitpoints();
    272         ret.maxHitpoints = cmpMirage.GetMaxHitpoints();
    273         ret.needsRepair = cmpMirage.NeedsRepair();
    274     }
    275269
    276     var cmpCapturable = Engine.QueryInterface(ent, IID_Capturable);
     270    var cmpCapturable = QueryMiragedInterface(ent, IID_Capturable);
    277271    if (cmpCapturable)
    278272    {
    279273        ret.capturePoints = cmpCapturable.GetCapturePoints();
    280274        ret.maxCapturePoints = cmpCapturable.GetMaxCapturePoints();
    281275    }
    282     if (cmpMirage && cmpMirage.Capturable())
    283     {
    284         ret.capturePoints = cmpMirage.GetCapturePoints();
    285         ret.maxCapturePoints = cmpMirage.GetMaxCapturePoints();
    286     }
    287276
    288277    var cmpBuilder = Engine.QueryInterface(ent, IID_Builder);
    289278    if (cmpBuilder)
     
    326315            ret.fogging = {"mirage": null};
    327316    }
    328317
    329     var cmpFoundation = Engine.QueryInterface(ent, IID_Foundation);
     318    var cmpFoundation = QueryMiragedInterface(ent, IID_Foundation);
    330319    if (cmpFoundation)
    331320    {
    332321        ret.foundation = {
     
    334323            "numBuilders": cmpFoundation.GetNumBuilders()
    335324        };
    336325    }
    337     if (cmpMirage && cmpMirage.Foundation())
    338     {
    339         ret.foundation = {
    340             "progress": cmpMirage.GetBuildPercentage()
    341         };
    342     }
    343326
    344327    var cmpOwnership = Engine.QueryInterface(ent, IID_Ownership);
    345328    if (cmpOwnership)
     
    523506    if (cmpPosition && cmpPosition.GetTurretParent() != INVALID_ENTITY)
    524507        ret.turretParent = cmpPosition.GetTurretParent();
    525508
    526     var cmpResourceSupply = Engine.QueryInterface(ent, IID_ResourceSupply);
     509    var cmpResourceSupply = QueryMiragedInterface(ent, IID_ResourceSupply);
    527510    if (cmpResourceSupply)
    528511    {
    529512        ret.resourceSupply = {
     
    533516            "type": cmpResourceSupply.GetType(),
    534517            "killBeforeGather": cmpResourceSupply.GetKillBeforeGather(),
    535518            "maxGatherers": cmpResourceSupply.GetMaxGatherers(),
    536             "gatherers": cmpResourceSupply.GetGatherers()
    537         };
    538     }
    539     if (cmpMirage && cmpMirage.ResourceSupply())
    540     {
    541         ret.resourceSupply = {
    542             "max": cmpMirage.GetMaxAmount(),
    543             "amount": cmpMirage.GetAmount(),
    544             "type": cmpMirage.GetType(),
    545             "isInfinite": cmpMirage.IsInfinite()
     519            "numGatherers": cmpResourceSupply.GetNumGatherers()
    546520        };
    547521    }
    548522
     
    17121686
    17131687    var owner = QueryOwnerInterface(data.entity).GetPlayerID();
    17141688
    1715     var cmp = Engine.QueryInterface(data.target, IID_Mirage);
    1716     if (cmp && !cmp.Capturable())
    1717         return false
    1718     else if (!cmp)
    1719         var cmp = Engine.QueryInterface(data.target, IID_Capturable);
    1720 
    1721     if (cmp && cmp.CanCapture(owner) && cmpAttack.GetAttackTypes().indexOf("Capture") != -1)
     1689    var cmpCapturable = QueryMiragedInterface(data.target, IID_Capturable);
     1690    if (cmpCapturable && cmpCapturable.CanCapture(owner) && cmpAttack.GetAttackTypes().indexOf("Capture") != -1)
    17221691        return cmpAttack.CanAttack(data.target);
    17231692
    17241693    return false;
  • binaries/data/mods/public/simulation/components/Mirage.js

     
    1313    this.player = null;
    1414    this.parent = INVALID_ENTITY;
    1515
    16     this.foundation = false;
    17     this.buildPercentage = null;
     16    this.miraged_iids = new Set();
     17
     18    this.buildPercentage = 0;
     19    this.numBuilders = 0;
    1820
    19     this.health = false;
    2021    this.maxHitpoints = null;
    2122    this.hitpoints = null;
    22     this.needsRepair = null;
     23    this.repairable = null;
     24    this.unhealable = null;
    2325
    24     this.capturable = false;
    2526    this.capturePoints = [];
    2627    this.maxCapturePoints = 0;
    2728
    28     this.resourceSupply = false;
    2929    this.maxAmount = null;
    3030    this.amount = null;
    3131    this.type = null;
    3232    this.isInfinite = null;
     33    this.killBeforeGather = null;
     34    this.maxGatherers = null;
     35    this.numGatherers = null;
    3336};
    3437
    3538Mirage.prototype.SetParent = function(ent)
     
    4750    this.player = player;
    4851};
    4952
     53Mirage.prototype.Mirages = function(iid)
     54{
     55    return this.miraged_iids.has(iid);
     56};
     57
    5058// ============================
    5159// Parent entity data
    5260
    5361// Foundation data
    5462
    55 Mirage.prototype.CopyFoundation = function(buildPercentage)
    56 {
    57     this.foundation = true;
    58     this.buildPercentage = buildPercentage;
    59 };
    60 
    61 Mirage.prototype.Foundation = function()
     63Mirage.prototype.CopyFoundation = function(cmpFoundation)
    6264{
    63     return this.foundation;
     65    this.miraged_iids.add(IID_Foundation);
     66    this.buildPercentage = cmpFoundation.GetBuildPercentage();
     67    this.numBuilders = cmpFoundation.GetNumBuilders();
    6468};
    6569
    66 Mirage.prototype.GetBuildPercentage = function()
    67 {
    68     return this.buildPercentage;
    69 };
     70Mirage.prototype.GetBuildPercentage = function() { return this.buildPercentage; };
     71Mirage.prototype.GetNumBuilders = function() { return this.numBuilders; };
    7072
    7173// Health data
    7274
    73 Mirage.prototype.CopyHealth = function(maxHitpoints, hitpoints, needsRepair)
     75Mirage.prototype.CopyHealth = function(cmpHealth)
    7476{
    75     this.health = true;
    76     this.maxHitpoints = maxHitpoints;
    77     this.hitpoints = Math.ceil(hitpoints);
    78     this.needsRepair = needsRepair;
     77    this.miraged_iids.add(IID_Health);
     78    this.maxHitpoints = cmpHealth.GetMaxHitpoints();
     79    this.hitpoints = cmpHealth.GetHitpoints();
     80    this.repairable = cmpHealth.IsRepairable();
     81    this.unhealable = cmpHealth.IsUnhealable();
    7982};
    8083
    81 Mirage.prototype.Health = function()
    82 {
    83     return this.health;
    84 };
    85 
    86 Mirage.prototype.GetMaxHitpoints = function()
    87 {
    88     return this.maxHitpoints;
    89 };
    90 
    91 Mirage.prototype.GetHitpoints = function()
    92 {
    93     return this.hitpoints;
    94 };
    95 
    96 Mirage.prototype.NeedsRepair = function()
    97 {
    98     return this.needsRepair;
    99 };
     84Mirage.prototype.GetMaxHitpoints = function() { return this.maxHitpoints; };
     85Mirage.prototype.GetHitpoints = function() { return this.hitpoints; };
     86Mirage.prototype.IsRepairable = function() { return this.repairable; };
     87Mirage.prototype.IsUnhealable = function() { return this.unhealable; };
    10088
    10189// Capture data
    10290
    103 Mirage.prototype.CopyCapturable = function(capturePoints, maxCapturePoints)
     91Mirage.prototype.CopyCapturable = function(cmpCapturable)
    10492{
    105     this.capturable = true;
    106     this.capturePoints = capturePoints;
    107     this.maxCapturePoints = maxCapturePoints;
     93    this.miraged_iids.add(IID_Capturable);
     94    this.capturePoints = cmpCapturable.GetCapturePoints();
     95    this.maxCapturePoints = cmpCapturable.GetMaxCapturePoints();
    10896};
    10997
    110 Mirage.prototype.Capturable = function()
    111 {
    112     return this.capturable;
    113 };
    114 
    115 Mirage.prototype.GetMaxCapturePoints = function()
    116 {
    117     return this.maxCapturePoints;
    118 };
    119 
    120 Mirage.prototype.GetCapturePoints = function()
    121 {
    122     return this.capturePoints;
    123 };
     98Mirage.prototype.GetMaxCapturePoints = function() { return this.maxCapturePoints; };
     99Mirage.prototype.GetCapturePoints = function() { return this.capturePoints; };
    124100
    125101Mirage.prototype.CanCapture = Capturable.prototype.CanCapture;
    126102
    127103// ResourceSupply data
    128104
    129 Mirage.prototype.CopyResourceSupply = function(maxAmount, amount, type, isInfinite)
    130 {
    131     this.resourceSupply = true;
    132     this.maxAmount = maxAmount;
    133     this.amount = amount;
    134     this.type = type;
    135     this.isInfinite = isInfinite;
    136 };
    137 
    138 Mirage.prototype.ResourceSupply = function()
    139 {
    140     return this.resourceSupply;
    141 };
    142 
    143 Mirage.prototype.GetMaxAmount = function()
    144 {
    145     return this.maxAmount;
    146 };
    147 
    148 Mirage.prototype.GetAmount = function()
    149 {
    150     return this.amount;
    151 };
    152 
    153 Mirage.prototype.GetType = function()
    154 {
    155     return this.type;
    156 };
    157 
    158 Mirage.prototype.IsInfinite = function()
    159 {
    160     return this.isInfinite;
    161 };
     105Mirage.prototype.CopyResourceSupply = function(cmpResourceSupply)
     106{
     107    this.miraged_iids.add(IID_ResourceSupply);
     108    this.maxAmount = cmpResourceSupply.GetMaxAmount();
     109    this.amount = cmpResourceSupply.GetCurrentAmount();
     110    this.type = cmpResourceSupply.GetType();
     111    this.isInfinite = cmpResourceSupply.IsInfinite();
     112    this.killBeforeGather = cmpResourceSupply.GetKillBeforeGather();
     113    this.maxGatherers = cmpResourceSupply.GetMaxGatherers();
     114    this.numGatherers = cmpResourceSupply.GetNumGatherers();
     115};
     116
     117Mirage.prototype.GetMaxAmount = function() { return this.maxAmount; };
     118Mirage.prototype.GetCurrentAmount = function() { return this.amount; };
     119Mirage.prototype.GetType = function() { return this.type; };
     120Mirage.prototype.IsInfinite = function() { return this.isInfinite; };
     121Mirage.prototype.GetKillBeforeGather = function() { return this.killBeforeGather; };
     122Mirage.prototype.GetMaxGatherers = function() { return this.maxGatherers; };
     123Mirage.prototype.GetNumGatherers = function() { return this.numGatherers; };
    162124
    163125// ============================
    164126
  • binaries/data/mods/public/simulation/components/ResourceGatherer.js

     
    253253 */
    254254ResourceGatherer.prototype.GetTargetGatherRate = function(target)
    255255{
    256     let type;
    257     let cmpResourceSupply = Engine.QueryInterface(target, IID_ResourceSupply);
    258     let cmpMirage = Engine.QueryInterface(target, IID_Mirage);
    259     if (cmpResourceSupply)
    260         type = cmpResourceSupply.GetType();
    261     else if (cmpMirage && cmpMirage.ResourceSupply())
    262         type = cmpMirage.GetType();
    263     else
     256    let cmpResourceSupply = QueryMiragedInterface(target, IID_ResourceSupply);
     257    if (!cmpResourceSupply)
    264258        return 0;
    265259
     260    let type = cmpResourceSupply.GetType();
     261
    266262    let rate = 0;
    267263    if (type.specific)
    268264        rate = this.GetGatherRate(type.generic+"."+type.specific);
     
    273269    let cheatMultiplier = cmpPlayer ? cmpPlayer.GetCheatTimeMultiplier() : 1;
    274270    rate = rate / cheatMultiplier;
    275271
    276     if (cmpMirage)
     272    if ("Mirages" in cmpResourceSupply)
    277273        return rate;
    278274
    279275    // Apply diminishing returns with more gatherers, for e.g. infinite farms. For most resources this has no effect. (GetDiminishingReturns will return null.)
     
    294290    // The cosine function is an oscillating curve, normally between -1 and 1. Multiplying by 0.5 squishes that down to
    295291    // between -0.5 and 0.5. Adding 0.5 to that changes the range to 0 to 1. The diminishingReturns constant
    296292    // adjusts the period of the curve.
    297     // Alternatively, just find scythetwirler (who came up with the math here) or alpha123 (who wrote the code) on IRC.
    298     let cmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership);
    299293    let diminishingReturns = cmpResourceSupply.GetDiminishingReturns();
    300294    if (diminishingReturns)
    301         rate = (0.5 * Math.cos((cmpResourceSupply.GetGatherers().length - 1) * Math.PI / diminishingReturns) + 0.5) * rate;
     295        rate = (0.5 * Math.cos((cmpResourceSupply.GetNumGatherers() - 1) * Math.PI / diminishingReturns) + 0.5) * rate;
    302296
    303297    return rate;
    304298};
  • binaries/data/mods/public/simulation/components/ResourceSupply.js

     
    7878    return +this.template.MaxGatherers;
    7979};
    8080
     81ResourceSupply.prototype.GetNumGatherers = function()
     82{
     83    return this.gatherers.map(p => p.length).reduce((a, b) => a+b);
     84};
     85
    8186ResourceSupply.prototype.GetGatherers = function()
    8287{
    83     var numPlayers = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager).GetNumPlayers();
    84     var total = [];
    85     for (var playerid = 0; playerid <= numPlayers; playerid++)
    86         for (var gatherer = 0; gatherer < this.gatherers[playerid].length; gatherer++)
    87             total.push(this.gatherers[playerid][gatherer]);
    88     return total;
     88    return this.gatherers.reduce((a, b) => a.concat(b));
    8989};
    9090
    9191ResourceSupply.prototype.GetDiminishingReturns = function()
     
    130130
    131131ResourceSupply.prototype.IsAvailable = function(player, gathererID)
    132132{
    133     if (this.GetGatherers().length < this.GetMaxGatherers() || this.gatherers[player].indexOf(gathererID) !== -1)
     133    if (this.GetNumGatherers() < this.GetMaxGatherers() || this.gatherers[player].indexOf(gathererID) !== -1)
    134134        return true;
    135135    return false;
    136136};
  • binaries/data/mods/public/simulation/components/StatusBars.js

     
    101101        yoffset -= height * 1.2;
    102102    };
    103103
    104     var cmpMirage = Engine.QueryInterface(this.entity, IID_Mirage);
    105 
    106104    var cmpPack = Engine.QueryInterface(this.entity, IID_Pack);
    107105    if (cmpPack && cmpPack.IsPacking())
    108106        AddBar("pack", cmpPack.GetProgress());
    109107
    110     var cmpHealth = Engine.QueryInterface(this.entity, IID_Health);
     108    var cmpHealth = QueryMiragedInterface(this.entity, IID_Health);
    111109    if (cmpHealth && cmpHealth.GetHitpoints() > 0)
    112110        AddBar("health", cmpHealth.GetHitpoints() / cmpHealth.GetMaxHitpoints());
    113     else if (cmpMirage && cmpMirage.Health())
    114         AddBar("health", cmpMirage.GetHitpoints() / cmpMirage.GetMaxHitpoints());
    115111
    116     var cmpResourceSupply = Engine.QueryInterface(this.entity, IID_ResourceSupply);
     112    var cmpResourceSupply = QueryMiragedInterface(this.entity, IID_ResourceSupply);
    117113    if (cmpResourceSupply)
    118114        AddBar("supply", cmpResourceSupply.IsInfinite() ? 1 : cmpResourceSupply.GetCurrentAmount() / cmpResourceSupply.GetMaxAmount());
    119     else if (cmpMirage && cmpMirage.ResourceSupply())
    120         AddBar("supply", cmpMirage.IsInfinite() ? 1 : cmpMirage.GetAmount() / cmpMirage.GetMaxAmount());
    121115
    122116    /*
    123117    // Rank icon disabled for now - see discussion around
  • binaries/data/mods/public/simulation/components/UnitAI.js

     
    19871987                    var cmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership);
    19881988                    var cmpSupply = Engine.QueryInterface(this.gatheringTarget, IID_ResourceSupply);
    19891989                    var cmpMirage = Engine.QueryInterface(this.gatheringTarget, IID_Mirage);
    1990                     if ((!cmpMirage || !cmpMirage.ResourceSupply()) &&
     1990                    if ((!cmpMirage || !cmpMirage.Mirages(IID_ResourceSupply)) &&
    19911991                        (!cmpSupply || !cmpSupply.AddGatherer(cmpOwnership.GetOwner(), this.entity)))
    19921992                    {
    19931993                        // Save the current order's data in case we need it later
     
    39033903    if (cmpFormation)
    39043904        return true;
    39053905
    3906     var cmpMirage = Engine.QueryInterface(ent, IID_Mirage);
    3907     if (cmpMirage)
    3908         return true;
    3909 
    3910     var cmpHealth = Engine.QueryInterface(ent, IID_Health);
    3911     if (!cmpHealth)
    3912         return false;
    3913 
    3914     return (cmpHealth.GetHitpoints() != 0);
     3906    var cmpHealth = QueryMiragedInterface(ent, IID_Health);
     3907    return cmpHealth && cmpHealth.GetHitpoints() != 0;
    39153908};
    39163909
    39173910/**
     
    50195012    // before we process the order then we still know what resource
    50205013    // type to look for more of
    50215014    var type;
    5022     var cmpResourceSupply = Engine.QueryInterface(target, IID_ResourceSupply);
    5023     var cmpMirage = Engine.QueryInterface(target, IID_Mirage);
     5015    var cmpResourceSupply = QueryMiragedInterface(target, IID_ResourceSupply);
    50245016    if (cmpResourceSupply)
    50255017        type = cmpResourceSupply.GetType();
    5026     else if (cmpMirage && cmpMirage.ResourceSupply())
    5027         type = cmpMirage.GetType();
    50285018    else
    50295019        error("CanGather allowed gathering from invalid entity");
    50305020
     
    56305620    if (this.IsTurret())
    56315621        return false;
    56325622    // The target must be a valid resource supply, or the mirage of one.
    5633     var cmpResourceSupply = Engine.QueryInterface(target, IID_ResourceSupply);
    5634     var cmpMirage = Engine.QueryInterface(target, IID_Mirage);
    5635     if (!cmpResourceSupply && !(cmpMirage && cmpMirage.ResourceSupply()))
     5623    var cmpResourceSupply = QueryMiragedInterface(target, IID_ResourceSupply);
     5624    if (!cmpResourceSupply)
    56365625        return false;
    56375626
    56385627    // Formation controllers should always respond to commands
  • binaries/data/mods/public/simulation/components/tests/test_GuiInterface.js

     
     1Engine.LoadHelperScript("Player.js");
    12Engine.LoadComponentScript("interfaces/Attack.js");
    23Engine.LoadComponentScript("interfaces/AlertRaiser.js");
    34Engine.LoadComponentScript("interfaces/Auras.js");
  • binaries/data/mods/public/simulation/helpers/Player.js

     
    223223}
    224224
    225225/**
     226 * Similar to Engine.QueryInterface but first checks if the entity
     227 * mirages the interface.
     228 */
     229function QueryMiragedInterface(ent, iid)
     230{
     231    var cmp = Engine.QueryInterface(ent, IID_Mirage);
     232    if (cmp && !cmp.Mirages(iid))
     233        return null;
     234    else if (!cmp)
     235        cmp = Engine.QueryInterface(ent, iid);
     236
     237    return cmp;
     238}
     239
     240/**
    226241 * Returns true if the entity 'target' is owned by an ally of
    227242 * the owner of 'entity'.
    228243 */
     
    379394Engine.RegisterGlobal("LoadPlayerSettings", LoadPlayerSettings);
    380395Engine.RegisterGlobal("QueryOwnerInterface", QueryOwnerInterface);
    381396Engine.RegisterGlobal("QueryPlayerIDInterface", QueryPlayerIDInterface);
     397Engine.RegisterGlobal("QueryMiragedInterface", QueryMiragedInterface);
    382398Engine.RegisterGlobal("IsOwnedByAllyOfEntity", IsOwnedByAllyOfEntity);
    383399Engine.RegisterGlobal("IsOwnedByMutualAllyOfEntity", IsOwnedByMutualAllyOfEntity);
    384400Engine.RegisterGlobal("IsOwnedByPlayer", IsOwnedByPlayer);