Ticket #3424: 3424.diff

File 3424.diff, 3.0 KB (added by Stan, 9 years ago)

Initial Elexis's patch

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

     
    55
    66/**
    77 * Try to collect loot from target entity
    88 */
    99Looter.prototype.Collect = function(targetEntity)
    1010{
    1111    var cmpLoot = Engine.QueryInterface(targetEntity, IID_Loot);
    1212    if (!cmpLoot)
    1313        return;
    1414
     15    // Add experience points as defined in the template
    1516    var xp = cmpLoot.GetXp();
    1617    if (xp > 0)
    1718    {
    1819        var cmpPromotion = Engine.QueryInterface(this.entity, IID_Promotion);
    1920        if (cmpPromotion)
    2021            cmpPromotion.IncreaseXp(xp);
    2122    }
    22     var cmpPlayer = QueryOwnerInterface(this.entity);
     23
     24    // Add resources as defined in the templates
    2325    var resources = cmpLoot.GetResources();
    24     for (var type in resources)
    25     {
     26    // TODO: could we do this conversion more transparently?
     27    for (let type in resources)
    2628        resources[type] = ApplyValueModificationsToEntity("Looter/Resource/"+type, resources[type], this.entity);
    27     }
    28     cmpPlayer.AddResources(resources);
    29 
    30     let cmpStatisticsTracker = QueryOwnerInterface(this.entity, IID_StatisticsTracker);
    31     if (cmpStatisticsTracker)
    32         cmpStatisticsTracker.IncreaseLootCollectedCounter(resources);
    3329
    34     // If target entity has trader component, add carried goods to loot too
     30    // Loot resources of traders
    3531    var cmpTrader = Engine.QueryInterface(targetEntity, IID_Trader);
    3632    if (cmpTrader)
    3733    {
    3834        var carriedGoods = cmpTrader.GetGoods();
    3935        if (carriedGoods.amount && carriedGoods.amount.traderGain)
    4036        {
    41             // Convert from {type:<type>,amount:<amount>} to {<type>:<amount>}
    42             var resourcesToAdd = {};
    43             resourcesToAdd[carriedGoods.type] = carriedGoods.amount.traderGain;
    44             if (carriedGoods.amount.market1Gain)
    45                 resourcesToAdd[carriedGoods.type] += carriedGoods.amount.market1Gain;
    46             if (carriedGoods.amount.market2Gain)
    47                 resourcesToAdd[carriedGoods.type] += carriedGoods.amount.market2Gain;
    48             cmpPlayer.AddResources(resourcesToAdd);
    49 
    50             let cmpStatisticsTracker = QueryOwnerInterface(this.entity, IID_StatisticsTracker);
    51             if (cmpStatisticsTracker)
    52                 cmpStatisticsTracker.IncreaseLootCollectedCounter(resourcesToAdd);
     37            resources[carriedGoods.type] += carriedGoods.amount.traderGain;
     38            resources[carriedGoods.type] += carriedGoods.amount.market1Gain ? carriedGoods.amount.market1Gain : 0;
     39            resources[carriedGoods.type] += carriedGoods.amount.market2Gain ? carriedGoods.amount.market2Gain : 0;
    5340        }
    5441    }
     42
     43    // Transfer resources
     44    var cmpPlayer = QueryOwnerInterface(this.entity);
     45    cmpPlayer.AddResources(resources);
     46
     47    // Update statistics
     48    let cmpStatisticsTracker = QueryOwnerInterface(this.entity, IID_StatisticsTracker);
     49    if (cmpStatisticsTracker)
     50        cmpStatisticsTracker.IncreaseLootCollectedCounter(resources);
    5551};
    5652
    5753Engine.RegisterComponentType(IID_Looter, "Looter", Looter);