Ticket #4193: 4193-loot-tooltip.7.patch

File 4193-loot-tooltip.7.patch, 10.1 KB (added by Mate-86, 8 years ago)
  • binaries/data/mods/public/globalscripts/LootHelper.js

     
     1/*
     2Helper to calculate the loot used by gui and simulation.
     3*/
     4
     5function calculateResourcesCarried(carriedResources, carriedGoods)
     6{
     7    var resources = {};
     8    // Loot resources workers carried
     9    if (carriedResources)
     10        for (let resource of carriedResources)
     11            resources[resource.type] = (resources[resource.type] || 0) + resource.amount;
     12
     13    // Loot resources traders carried
     14    if (carriedGoods && carriedGoods.amount)
     15        resources[carriedGoods.type] =
     16            (resources[carriedGoods.type] || 0)
     17            + (carriedGoods.amount.traderGain || 0)
     18            + (carriedGoods.amount.market1Gain || 0)
     19            + (carriedGoods.amount.market2Gain || 0);
     20
     21    return resources;
     22}
     23 No newline at end of file
  • binaries/data/mods/public/globalscripts/Templates.js

     
     1const lootTypes = ["xp", "food", "wood", "stone", "metal"];
     2
    13/**
    24 * Gets an array of all classes for this identity template
    35 */
     
    244246            "rate": getEntityValue("Heal/Rate")
    245247        };
    246248
     249    if (template.Loot)
     250    {
     251        ret.loot = {};
     252        for (let type in template.Loot)
     253            ret.loot[type] = getEntityValue("Loot/"+ type);
     254    }
     255
    247256    if (template.Obstruction)
    248257    {
    249258        ret.obstruction = {
  • binaries/data/mods/public/gui/common/setup_resources.xml

     
    2525        sprite="stretched:session/icons/resources/time_small.png"
    2626        size="16 16"
    2727    />
     28    <icon name="icon_xp"
     29        sprite="stretched:session/icons/resources/xp_small.png"
     30        size="16 16"
     31    />
    2832</setup>
  • binaries/data/mods/public/gui/common/tooltips.js

     
    575575    return headerFont(translate("Classes:")) + ' ' +
    576576        bodyFont(template.visibleIdentityClasses.map(c => translate(c)).join(translate(", ")));
    577577}
     578
     579function getLootTooltip(template)
     580{
     581    if (!template.loot) //TODO: what if a worker/trader is carring resources but do not have base loot defined?
     582        return "";
     583
     584    let loots = [];
     585    let resourcesCarried = [];
     586    const lootTypes = ["xp", "food", "wood", "stone", "metal"];
     587
     588    if ("resourceCarrying" in template)
     589        resourcesCarried = calculateResourcesCarried(template.resourceCarrying, template.trader ? template.trader.goods : null);
     590
     591    for (let type of lootTypes)
     592        loots[type] = (loots[type] || 0) + (template.loot[type] || 0) + (resourcesCarried[type] || 0);
     593
     594    for (let type of lootTypes)
     595        loots.push(sprintf(translate("%(component)s %(loot)s"), {
     596            "component": costIcon(type),
     597            "loot": loots[type]
     598    }));
     599
     600    return headerFont(translate("Loot:")) + " " + loots.join(" ");
     601}
  • binaries/data/mods/public/gui/session/selection_details.js

     
    291291        getBuildRateTooltip,
    292292        getSpeedTooltip,
    293293        getGarrisonTooltip,
    294         getProjectilesTooltip
     294        getProjectilesTooltip,
     295        getLootTooltip
    295296    ].map(func => func(entState)).filter(tip => tip).join("\n");
    296297
    297298    let iconTooltips = [];
     
    320321    let capturePoints = (new Array(g_MaxPlayers + 1)).fill(0);
    321322    let playerID = 0;
    322323    let totalResourcesCarried = {};
     324    let totalLoot = {};
    323325
    324326    for (let i = 0; i < selection.length; ++i)
    325327    {
    326328        let entState = GetEntityState(selection[i]);
     329        let extEntState = GetExtendedEntityState(selection[i]);
    327330        if (!entState)
    328331            continue;
    329332        playerID = entState.player; // trust that all selected entities have the same owner
     
    338341            capturePoints = entState.capturePoints.map((v, i) => v + capturePoints[i]);
    339342        }
    340343
    341         if (entState.resourceCarrying && entState.resourceCarrying.length)
    342         {
    343             let carrying = entState.resourceCarrying[0];
    344             totalResourcesCarried[carrying.type] = (totalResourcesCarried[carrying.type] || 0) + carrying.amount;
    345         }
     344        let resourcesCarried = calculateResourcesCarried(entState.resourceCarrying, entState.trader ? entState.trader.goods : null);
     345
     346        const lootTypes = ["xp", "food", "wood", "stone", "metal"];
     347
     348        for (let type of lootTypes)
     349            totalLoot[type] = (totalLoot[type] || 0) + (extEntState.loot[type] || 0) + (resourcesCarried[type] || 0);
     350
     351        for (let type in resourcesCarried)
     352            totalResourcesCarried[type] = (totalResourcesCarried[type] || 0) + resourcesCarried[type];
    346353    }
    347354
    348355    Engine.GetGUIObjectByName("healthMultiple").hidden = averageHealth <= 0;
     
    394401
    395402    let numberOfUnits = Engine.GetGUIObjectByName("numberOfUnits");
    396403    numberOfUnits.caption = selection.length;
    397     numberOfUnits.tooltip = Object.keys(totalResourcesCarried).map(res =>
    398         costIcon(res) + totalResourcesCarried[res]
    399     ).join(" ");
    400404
     405    numberOfUnits.tooltip = "";
     406
     407    if (Object.keys(totalResourcesCarried).length > 0)
     408        numberOfUnits.tooltip = translate("Carrying") + ": " + Object.keys(totalResourcesCarried).map(res =>
     409            costIcon(res) + totalResourcesCarried[res]).join(" ") + "\n";
     410
     411    numberOfUnits.tooltip += translate("Loot") + ": " + Object.keys(totalLoot).map(res =>
     412        costIcon(res) + totalLoot[res]).join(" ");
     413
    401414    // Unhide Details Area
    402415    Engine.GetGUIObjectByName("detailsAreaMultiple").hidden = false;
    403416    Engine.GetGUIObjectByName("detailsAreaSingle").hidden = true;
  • binaries/data/mods/public/gui/structree/draw.js

     
    1414    getProjectilesTooltip,
    1515    getSpeedTooltip,
    1616    getGatherTooltip,
    17     getPopulationBonusTooltip
     17    getPopulationBonusTooltip,
     18    getLootTooltip
    1819];
    1920
    2021/**
  • binaries/data/mods/public/simulation/components/GuiInterface.js

     
    421421        "barterMarket": null,
    422422        "buildingAI": null,
    423423        "heal": null,
     424        "loot": null,
    424425        "obstruction": null,
    425426        "turretParent":null,
    426427        "promotion": null,
     
    568569            "healableClasses": cmpHeal.GetHealableClasses(),
    569570        };
    570571
     572    let cmpLoot = Engine.QueryInterface(ent, IID_Loot);
     573    if (cmpLoot)
     574    {
     575        let resources = cmpLoot.GetResources();
     576        ret.loot = {
     577            "xp": cmpLoot.GetXp(),
     578            "food": resources.food,
     579            "wood": resources.wood,
     580            "stone": resources.stone,
     581            "metal": resources.metal
     582        };
     583    }
     584
    571585    let cmpUnitMotion = Engine.QueryInterface(ent, IID_UnitMotion);
    572586    if (cmpUnitMotion)
    573587        ret.speed = {
  • binaries/data/mods/public/simulation/components/Looter.js

     
    1414    if (!cmpLoot)
    1515        return;
    1616
     17    // Collect resources carried by workers and traders
     18    var cmpResourceGatherer = Engine.QueryInterface(targetEntity, IID_ResourceGatherer);
     19    var cmpTrader = Engine.QueryInterface(targetEntity, IID_Trader);
     20
     21    let resourcesCarried = calculateResourcesCarried(
     22        cmpResourceGatherer ? cmpResourceGatherer.GetCarryingStatus() : null,
     23        cmpTrader ? cmpTrader.goods : null
     24    );
     25
    1726    // Loot resources as defined in the templates
    1827    var resources = cmpLoot.GetResources();
    1928    for (let type in resources)
    20         resources[type] = ApplyValueModificationsToEntity("Looter/Resource/"+type, resources[type], this.entity);
     29        resources[type] = ApplyValueModificationsToEntity("Looter/Resource/"+type, resources[type], this.entity)
     30            + (resourcesCarried[type] || 0);
    2131
    22     // TODO: stop assuming that cmpLoot.GetResources() delivers all resource types (by defining them in a central location)
    23 
    24     // Loot resources that killed enemies carried
    25     var cmpResourceGatherer = Engine.QueryInterface(targetEntity, IID_ResourceGatherer);
    26     if (cmpResourceGatherer)
    27         for (let resource of cmpResourceGatherer.GetCarryingStatus())
    28             resources[resource.type] += resource.amount;
    29 
    30     // Loot resources traders carry
    31     var cmpTrader = Engine.QueryInterface(targetEntity, IID_Trader);
    32     if (cmpTrader)
    33     {
    34         let carriedGoods = cmpTrader.GetGoods();
    35         if (carriedGoods.amount)
    36         {
    37             resources[carriedGoods.type] +=
    38                 + (carriedGoods.amount.traderGain || 0)
    39                 + (carriedGoods.amount.market1Gain || 0)
    40                 + (carriedGoods.amount.market2Gain || 0);
    41         }
    42     }
    43 
    4432    // Transfer resources
    4533    var cmpPlayer = QueryOwnerInterface(this.entity);
    4634    cmpPlayer.AddResources(resources);
  • binaries/data/mods/public/simulation/components/tests/test_GuiInterface.js

     
    1515Engine.LoadComponentScript("interfaces/Guard.js");
    1616Engine.LoadComponentScript("interfaces/Heal.js");
    1717Engine.LoadComponentScript("interfaces/Health.js");
     18Engine.LoadComponentScript("interfaces/Loot.js");
    1819Engine.LoadComponentScript("interfaces/Market.js");
    1920Engine.LoadComponentScript("interfaces/Pack.js");
    2021Engine.LoadComponentScript("interfaces/ProductionQueue.js");
     
    535536    },
    536537    buildingAI: null,
    537538    heal: null,
     539    loot: null,
    538540    obstruction: null,
    539541    turretParent: null,
    540542    promotion: null,