Ticket #1387: Gatherers3.patch

File Gatherers3.patch, 4.1 KB (added by Maciej Piekarz, 11 years ago)
  • binaries/data/mods/public/simulation/components/ResourceGatherer.js

     
    190190    if (!this.GetTargetGatherRate(target))
    191191        return { "exhausted": true };
    192192
     193    var cmpResourceSupply = Engine.QueryInterface(target, IID_ResourceSupply);
     194    if (!cmpResourceSupply.AddGatherer(this.entity))
     195        return { "exhausted": true };
     196
    193197    var gatherAmount = 1;
    194 
    195     var cmpResourceSupply = Engine.QueryInterface(target, IID_ResourceSupply);
     198   
    196199    var type = cmpResourceSupply.GetType();
    197200
    198201    // Initialise the carried count if necessary
  • binaries/data/mods/public/simulation/components/ResourceSupply.js

     
    2929            "<value>treasure.metal</value>" +
    3030            "<value>treasure.food</value>" +
    3131        "</choice>" +
     32    "</element>" +
     33    "<element name='MaxGatherers' a:help='Amount of gatherers who can gather resources from this entity at the same time'>" +
     34        "<data type='nonNegativeInteger'/>" +
    3235    "</element>";
    3336
    3437ResourceSupply.prototype.Init = function()
    3538{
    3639    // Current resource amount (non-negative)
    3740    this.amount = this.GetMaxAmount();
     41    this.gatherersAmount = 0;
     42    this.gatherers = {};
    3843};
    3944
    4045ResourceSupply.prototype.GetKillBeforeGather = function()
     
    5257    return this.amount;
    5358};
    5459
     60ResourceSupply.prototype.GetMaxGatherers = function()
     61{
     62    return +this.template.MaxGatherers;
     63};
     64
    5565ResourceSupply.prototype.TakeResources = function(rate)
    5666{
    5767    // 'rate' should be a non-negative integer
     
    7787    return { "generic": type, "specific": subtype };
    7888};
    7989
     90ResourceSupply.prototype.RefreshGatherers = function()
     91{
     92    for (var gatherer in this.gatherers)
     93    {
     94        var cmpUnitAI = Engine.QueryInterface(+gatherer, IID_UnitAI);
     95        var orders = cmpUnitAI.GetOrders();
     96        if (orders.length == 0 || orders[0].type != 'Gather' || orders[0].data.target != this.entity)
     97        {
     98            delete this.gatherers[gatherer];
     99            this.gatherersAmount--;
     100        }
     101    }
     102};
     103
     104ResourceSupply.prototype.IsAvailable = function(gatherer)
     105{
     106    // Check if supply is available for gatherer
     107   
     108    if (gatherer in this.gatherers || this.gatherersAmount < this.GetMaxGatherers()) return true;
     109    this.RefreshGatherers();
     110    if (this.gatherersAmount < this.GetMaxGatherers()) return true;
     111    return false;
     112};
     113
     114ResourceSupply.prototype.AddGatherer = function(gatherer)
     115{
     116    if (!this.IsAvailable(gatherer)) return false;
     117   
     118    if (!(gatherer in this.gatherers)) this.gatherersAmount++;
     119    this.gatherers[gatherer] = true;
     120    return true;
     121};
     122
    80123Engine.RegisterComponentType(IID_ResourceSupply, "ResourceSupply", ResourceSupply);
  • binaries/data/mods/public/simulation/components/UnitAI.js

     
    29972997        if (template.indexOf("resource|") != -1)
    29982998            template = template.slice(9);
    29992999
    3000         if (amount > 0 && filter(ent, type, template))
     3000        if (amount > 0 && filter(ent, type, template) && cmpResourceSupply.IsAvailable(this.entity)) {
    30013001            return ent;
     3002        }
    30023003    }
    30033004
    30043005    return undefined;
  • binaries/data/mods/public/simulation/templates/template_structure_resource_field.xml

     
    5353    <KillBeforeGather>false</KillBeforeGather>
    5454    <Amount>2000</Amount>
    5555    <Type>food.grain</Type>
     56    <MaxGatherers>5</MaxGatherers>
    5657  </ResourceSupply>
    5758  <Sound>
    5859    <SoundGroups>