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

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

     
    244244            "rate": getEntityValue("Heal/Rate")
    245245        };
    246246
     247    if (template.Loot)
     248    {
     249        ret.loot = {};
     250        for (let type in template.Loot)
     251            ret.loot[type] = getEntityValue("Loot/"+ type);
     252    }
     253
    247254    if (template.Obstruction)
    248255    {
    249256        ret.obstruction = {
     
    381388
    382389    return ret;
    383390}
     391
     392/**
     393 * Helper to calculate the loot used by gui and simulation.
     394 * @param carriedResources resources carried by the looted worker
     395 * @param trader the looted trader
     396 * @return the sum of carried resources and goods
     397 */
     398function calculateResourcesAndGoodsCarried(carriedResources, trader)
     399{
     400    var resources = {};
     401    // Loot resources workers carried
     402    if (carriedResources)
     403        for (let resource of carriedResources)
     404            resources[resource.type] = (resources[resource.type] || 0) + resource.amount;
     405
     406    // Loot resources traders carried
     407    if (trader)
     408        resources[trader.goods.type] =
     409            (resources[trader.goods.type] || 0) +
     410            (trader.goods.amount.traderGain || 0) +
     411            (trader.goods.amount.market1Gain || 0) +
     412            (trader.goods.amount.market2Gain || 0);
     413
     414    return resources;
     415}
  • 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 = calculateResourcesAndGoodsCarried(template.resourceCarrying, template.trader);
     590
     591    for (let type of lootTypes)
     592    {
     593        let loot = (loots[type] || 0) + (template.loot[type] || 0) + (resourcesCarried[type] || 0);
     594        if (loot > 0)
     595            loots[type] = loot;
     596    }
     597
     598    for (let type of Object.keys(loots))
     599        loots.push(sprintf(translate("%(component)s %(loot)s"), {
     600            "component": costIcon(type),
     601            "loot": loots[type]
     602    }));
     603
     604    return sprintf(translate("%(label)s %(details)s"), {
     605        "label": headerFont(translate("Loot:")),
     606        "details": loots.join("  ")
     607    });
     608}
  • 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 = [];
     
    319320    let maxCapturePoints = 0;
    320321    let capturePoints = (new Array(g_MaxPlayers + 1)).fill(0);
    321322    let playerID = 0;
    322     let totalResourcesCarried = {};
     323    let totalResourcesAndGoodsCarried = {};
     324    let totalLoot = {};
    323325
    324326    for (let i = 0; i < selection.length; ++i)
    325327    {
    326         let entState = GetEntityState(selection[i]);
     328        let entState = GetExtendedEntityState(selection[i]);
    327329        if (!entState)
    328330            continue;
    329331        playerID = entState.player; // trust that all selected entities have the same owner
     
    338340            capturePoints = entState.capturePoints.map((v, i) => v + capturePoints[i]);
    339341        }
    340342
    341         if (entState.resourceCarrying && entState.resourceCarrying.length)
     343        let resourcesAndGoodsCarried = calculateResourcesAndGoodsCarried(entState.resourceCarrying, entState.trader);
     344
     345        const lootTypes = ["xp", "food", "wood", "stone", "metal"];
     346
     347        for (let type of lootTypes)
    342348        {
    343             let carrying = entState.resourceCarrying[0];
    344             totalResourcesCarried[carrying.type] = (totalResourcesCarried[carrying.type] || 0) + carrying.amount;
     349            let loot = (totalLoot[type] || 0) + (entState.loot[type] || 0) + (resourcesAndGoodsCarried[type] || 0);
     350            if (loot > 0)
     351                totalLoot[type] = loot;
    345352        }
     353
     354        for (let type in resourcesAndGoodsCarried)
     355            totalResourcesAndGoodsCarried[type] = (totalResourcesAndGoodsCarried[type] || 0) + resourcesAndGoodsCarried[type];
    346356    }
    347357
    348358    Engine.GetGUIObjectByName("healthMultiple").hidden = averageHealth <= 0;
     
    394404
    395405    let numberOfUnits = Engine.GetGUIObjectByName("numberOfUnits");
    396406    numberOfUnits.caption = selection.length;
    397     numberOfUnits.tooltip = Object.keys(totalResourcesCarried).map(res =>
    398         costIcon(res) + totalResourcesCarried[res]
    399     ).join(" ");
    400407
     408    numberOfUnits.tooltip = "";
     409
     410    if (Object.keys(totalResourcesAndGoodsCarried).length)
     411        numberOfUnits.tooltip = sprintf(translate("%(label)s %(details)s\n"), {
     412            "label": headerFont(translate("Carrying:")),
     413            "details": bodyFont(Object.keys(totalResourcesAndGoodsCarried).map(
     414                res => sprintf(translate("%(type)s %(amount)s"),
     415                    {"type" : costIcon(res), "amount" : totalResourcesAndGoodsCarried[res]})).join("  "))
     416        });
     417
     418    if (Object.keys(totalLoot).length)
     419        numberOfUnits.tooltip += sprintf(translate("%(label)s %(details)s"), {
     420            "label": headerFont(translate("Loot:")),
     421            "details": bodyFont(Object.keys(totalLoot).map(
     422                res => sprintf(translate("%(type)s %(amount)s"),
     423                    {"type" : costIcon(res), "amount" : totalLoot[res]})).join("  "))
     424        });
     425
    401426    // Unhide Details Area
    402427    Engine.GetGUIObjectByName("detailsAreaMultiple").hidden = false;
    403428    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 = calculateResourcesAndGoodsCarried(
     22        cmpResourceGatherer.GetCarryingStatus() || null,
     23        cmpTrader
     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
    2232    // TODO: stop assuming that cmpLoot.GetResources() delivers all resource types (by defining them in a central location)
    2333
    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 
    4434    // Transfer resources
    4535    var cmpPlayer = QueryOwnerInterface(this.entity);
    4636    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,