Ticket #1353: 1353_InconsistentGathering.patch

File 1353_InconsistentGathering.patch, 5.0 KB (added by mattlott, 13 months ago)

This updates how gather rate is applied in order to avoid starving gatherers.

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

     
    187187/** 
    188188 * Gather from the target entity. This should only be called after a successful range check, 
    189189 * and if the target has a compatible ResourceSupply. 
    190  * It should be called at a rate of once per second. 
    191190 */ 
    192191ResourceGatherer.prototype.PerformGather = function(target) 
    193192{ 
    194     var rate = this.GetTargetGatherRate(target); 
    195     if (!rate) 
    196         return { "exhausted": true }; 
     193    // PerformGather call interval will be determined by gather rate, so always gather 1 amount here. 
     194    var gatherAmount = 1; 
    197195 
    198196    var cmpResourceSupply = Engine.QueryInterface(target, IID_ResourceSupply); 
    199197    var type = cmpResourceSupply.GetType(); 
     
    205203    // Find the maximum so we won't exceed our capacity 
    206204    var maxGathered = this.GetCapacities()[type.generic] - this.carrying[type.generic]; 
    207205 
    208     var status = cmpResourceSupply.TakeResources(Math.min(rate, maxGathered)); 
     206    var status = cmpResourceSupply.TakeResources(Math.min(gatherAmount, maxGathered)); 
    209207 
    210208    this.carrying[type.generic] += status.amount; 
    211209 
     
    244242 
    245243    var type = cmpResourceSupply.GetType(); 
    246244 
     245    var rates = this.GetGatherRates(); 
     246 
    247247    var rate; 
    248     if (type.specific && this.GetGatherRates()[type.generic+"."+type.specific]) 
    249         rate = this.GetGatherRates()[type.generic+"."+type.specific]; 
     248    if (type.specific && rates[type.generic+"."+type.specific]) 
     249        rate = rates[type.generic+"."+type.specific]; 
    250250    else 
    251         rate = this.GetGatherRates()[type.generic]; 
    252  
     251        rate = rates[type.generic]; 
     252     
    253253    return (rate || 0); 
    254254}; 
    255255 
  • binaries/data/mods/public/simulation/components/UnitAI.js

     
    944944 
    945945            "GATHERING": { 
    946946                "enter": function() { 
    947                     this.StartTimer(1000, 1000); 
     947                    var target = this.order.data.target; 
     948                     
     949                    // Calculate timing based on gather rates 
     950                    // This allows the gather rate to control how often we gather, instead of how much. 
     951                    var cmpResourceGatherer = Engine.QueryInterface(this.entity, IID_ResourceGatherer); 
     952                    var rate = cmpResourceGatherer.GetTargetGatherRate(target); 
    948953 
    949                     // We want to start the gather animation as soon as possible, 
    950                     // but only if we're actually at the target and it's still alive 
    951                     // (else it'll look like we're chopping empty air). 
    952                     // (If it's not alive, the Timer handler will deal with sending us 
    953                     // off to a different target.) 
    954                     if (this.CheckTargetRange(this.order.data.target, IID_ResourceGatherer)) 
    955                     { 
    956                         var typename = "gather_" + this.order.data.type.specific; 
    957                         this.SelectAnimation(typename, false, 1.0, typename); 
    958                     } 
     954                    if (rate) 
     955                    { 
     956                        // Scale timing interval based on rate, and start timer 
     957                        var offset = 0; 
     958                        var repeat = 1000 / rate;   
     959                        this.StartTimer(offset, repeat); 
     960 
     961                        // We want to start the gather animation as soon as possible, 
     962                        // but only if we're actually at the target and it's still alive 
     963                        // (else it'll look like we're chopping empty air). 
     964                        // (If it's not alive, the Timer handler will deal with sending us 
     965                        // off to a different target.) 
     966                        if (this.CheckTargetRange(target, IID_ResourceGatherer)) 
     967                        { 
     968                            var typename = "gather_" + this.order.data.type.specific; 
     969 
     970                            // TODO: Update animation speed based on gather rate?  (1000 / repeat) yields exaggerated results. 
     971                            this.SelectAnimation(typename, false, 1.0, typename); 
     972                        } 
     973                    } 
     974                    else 
     975                    { 
     976                        // No rate, give up on gathering 
     977                        this.FinishOrder(); 
     978                    } 
    959979                }, 
    960980 
    961981                "leave": function() { 
     
    19701990    { 
    19711991        this.timer = undefined; 
    19721992    } 
    1973     else 
    1974     { 
    1975         var cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer); 
    1976         this.timer = cmpTimer.SetTimeout(this.entity, IID_UnitAI, "TimerHandler", data.timerRepeat - lateness, data); 
    1977     } 
    19781993 
    19791994    UnitFsm.ProcessMessage(this, {"type": "Timer", "data": data, "lateness": lateness}); 
    19801995}; 
     
    19922007    var data = { "timerRepeat": repeat }; 
    19932008 
    19942009    var cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer); 
    1995     this.timer = cmpTimer.SetTimeout(this.entity, IID_UnitAI, "TimerHandler", offset, data); 
     2010    if (repeat === undefined) 
     2011        this.timer = cmpTimer.SetTimeout(this.entity, IID_UnitAI, "TimerHandler", offset, data); 
     2012    else 
     2013        this.timer = cmpTimer.SetInterval(this.entity, IID_UnitAI, "TimerHandler", offset, repeat, data); 
    19962014}; 
    19972015 
    19982016/**