Ticket #1387: GathererLimits.patch

File GathererLimits.patch, 20.6 KB (added by wraitii, 11 years ago)
  • 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.gatherers = [];    // list of IDs
    3842};
    3943
    4044ResourceSupply.prototype.GetKillBeforeGather = function()
     
    5256    return this.amount;
    5357};
    5458
     59ResourceSupply.prototype.GetMaxGatherers = function()
     60{
     61    return +this.template.MaxGatherers;
     62};
     63ResourceSupply.prototype.GetGatherers = function()
     64{
     65    return this.gatherers;
     66};
     67
    5568ResourceSupply.prototype.TakeResources = function(rate)
    5669{
    5770    // 'rate' should be a non-negative integer
     
    7790    return { "generic": type, "specific": subtype };
    7891};
    7992
     93ResourceSupply.prototype.IsAvailable = function(gathererID)
     94{
     95    if (this.gatherers.length < this.GetMaxGatherers() || this.gatherers.indexOf(gathererID) !== -1)
     96        return true;
     97    return false;
     98};
     99
     100ResourceSupply.prototype.AddGatherer = function(gathererID)
     101{
     102    if (!this.IsAvailable(gathererID))
     103        return false;
     104   
     105    if (this.gatherers.indexOf(gathererID) === -1)
     106        this.gatherers.push(gathererID);
     107
     108    // broadcast message, mainly useful for the AIs.
     109    Engine.PostMessage(this.entity, MT_ResourceSupplyGatherersChanged, { "to": this.gatherers });
     110
     111    return true;
     112};
     113
     114// should this return false if the gatherer didn't gather from said resource?
     115ResourceSupply.prototype.RemoveGatherer = function(gathererID)
     116{
     117    if (this.gatherers.indexOf(gathererID) !== -1)
     118        this.gatherers.splice(this.gatherers.indexOf(gathererID),1);
     119   
     120    // broadcast message, mainly useful for the AIs.
     121    Engine.PostMessage(this.entity, MT_ResourceSupplyGatherersChanged, { "to": this.gatherers });
     122};
     123
    80124Engine.RegisterComponentType(IID_ResourceSupply, "ResourceSupply", ResourceSupply);
  • binaries/data/mods/public/simulation/components/GuiInterface.js

     
    261261            "max": cmpResourceSupply.GetMaxAmount(),
    262262            "amount": cmpResourceSupply.GetCurrentAmount(),
    263263            "type": cmpResourceSupply.GetType(),
    264             "killBeforeGather": cmpResourceSupply.GetKillBeforeGather()
     264            "killBeforeGather": cmpResourceSupply.GetKillBeforeGather(),
     265            "maxGatherers": cmpResourceSupply.GetMaxGatherers(),
     266            "gatherers": cmpResourceSupply.GetGatherers()
    265267        };
    266268    }
    267269
  • binaries/data/mods/public/simulation/components/AIProxy.js

     
    141141    this.changes.resourceSupplyAmount = msg.to;
    142142};
    143143
     144AIProxy.prototype.ResourceSupplyGatherersChanged = function(msg)
     145{
     146    this.NotifyChange();
     147    this.changes.resourceSupplyGatherers = msg.to;
     148};
     149
    144150AIProxy.prototype.OnResourceCarryingChanged = function(msg)
    145151{
    146152    this.NotifyChange();
     
    225231    {
    226232        // Updated by OnResourceSupplyChanged
    227233        ret.resourceSupplyAmount = cmpResourceSupply.GetCurrentAmount();
     234        ret.resourceSupplyGatherers = cmpResourceSupply.GetGatherers();
    228235    }
    229236
    230237    var cmpResourceGatherer = Engine.QueryInterface(this.entity, IID_ResourceGatherer);
  • binaries/data/mods/public/simulation/components/interfaces/ResourceSupply.js

     
    33// Message of the form { "from": 100, "to", 90 },
    44// sent whenever supply level changes.
    55Engine.RegisterMessageType("ResourceSupplyChanged");
     6
     7// Message of the form { "to", [array of gatherers ID] },
     8// sent whenever the number of gatherer changes
     9Engine.RegisterMessageType("ResourceSupplyGatherersChanged");
  • binaries/data/mods/public/simulation/components/UnitAI.js

     
    15391539            "APPROACHING": {
    15401540                "enter": function() {
    15411541                    this.SelectAnimation("move");
     1542                   
     1543                    this.gatheringTarget = this.order.data.target;  // temporary, deleted in "leave".
     1544
     1545                    // check that we can gather from the resource we're supposed to gather from.
     1546                    var cmpSupply = Engine.QueryInterface(this.order.data.target, IID_ResourceSupply);
     1547                    if (!cmpSupply || !cmpSupply.AddGatherer(this.entity))
     1548                    {
     1549                        // Save the current order's data in case we need it later
     1550                        var oldType = this.order.data.type;
     1551                        var oldTarget = this.order.data.target;
     1552                        var oldTemplate = this.order.data.template;
     1553
     1554                        // Try the next queued order if there is any
     1555                        if (this.FinishOrder())
     1556                            return true;
     1557                       
     1558                        // Try to find another nearby target of the same specific type
     1559                        // Also don't switch to a different type of huntable animal
     1560                        var nearby = this.FindNearbyResource(function (ent, type, template) {
     1561                            return (
     1562                                ent != oldTarget
     1563                                 && ((type.generic == "treasure" && oldType.generic == "treasure")
     1564                                 || (type.specific == oldType.specific
     1565                                 && (type.specific != "meat" || oldTemplate == template)))
     1566                            );
     1567                        });
     1568                        if (nearby)
     1569                        {
     1570                            this.PerformGather(nearby, false, false);
     1571                            return true;
     1572                        }
     1573
     1574                        var nearby = this.FindNearestDropsite(oldType.generic);
     1575                        if (nearby)
     1576                        {
     1577                            this.PushOrderFront("ReturnResource", { "target": nearby, "force": false });
     1578                            return true;
     1579                        }
     1580                       
     1581                        return true;
     1582                    }
     1583                    return false;
    15421584                },
    15431585
    15441586                "MoveCompleted": function(msg) {
     
    15461588                    {
    15471589                        // We failed to reach the target
    15481590
     1591                        // remove us from the list of entities gathering from Resource.
     1592                        var cmpSupply = Engine.QueryInterface(this.gatheringTarget, IID_ResourceSupply);
     1593                        if (cmpSupply)
     1594                            cmpSupply.RemoveGatherer(this.entity);
     1595
    15491596                        // Save the current order's data in case we need it later
    15501597                        var oldType = this.order.data.type;
    15511598                        var oldTarget = this.order.data.target;
     
    15801627                    // We reached the target - start gathering from it now
    15811628                    this.SetNextState("GATHERING");
    15821629                },
     1630               
     1631                "leave": function() {
     1632                    var cmpSupply = Engine.QueryInterface(this.gatheringTarget, IID_ResourceSupply);
     1633                    if (cmpSupply)
     1634                        cmpSupply.RemoveGatherer(this.entity);
     1635                    delete this.gatheringTarget;
     1636                },
    15831637            },
    15841638           
    15851639            // Walking to a good place to gather resources near, used by GatherNearPosition
     
    16171671            "GATHERING": {
    16181672                "enter": function() {
    16191673                    var target = this.order.data.target;
    1620 
     1674                    if (target)
     1675                    {
     1676                        this.gatheringTarget = target;  // temporary, deleted in "leave".
     1677                       
     1678                        // Check that we can gather from the resource we're supposed to gather from.
     1679                        // Will only be added if we're not already in.
     1680                        var cmpSupply = Engine.QueryInterface(this.gatheringTarget, IID_ResourceSupply);
     1681                        if (!cmpSupply || !cmpSupply.AddGatherer(this.entity))
     1682                        {
     1683                            this.StartTimer(0);
     1684                            return false;
     1685                        }
     1686                    }
    16211687                    // If this order was forced, the player probably gave it, but now we've reached the target
    16221688                    //  switch to an unforced order (can be interrupted by attacks)
    16231689                    this.order.data.force = false;
     
    16641730
    16651731                "leave": function() {
    16661732                    this.StopTimer();
    1667 
     1733                   
     1734                    var cmpSupply = Engine.QueryInterface(this.gatheringTarget, IID_ResourceSupply);
     1735                    if (cmpSupply)
     1736                        cmpSupply.RemoveGatherer(this.entity);
     1737                    delete this.gatheringTarget;
     1738                   
    16681739                    // Show the carried resource, if we've gathered anything.
    16691740                    this.SetGathererAnimationOverride();
    16701741                },
     
    16731744                    var target = this.order.data.target;
    16741745                    var resourceTemplate = this.order.data.template;
    16751746                    var resourceType = this.order.data.type;
     1747                   
     1748                    var cmpSupply = Engine.QueryInterface(this.gatheringTarget, IID_ResourceSupply);
     1749                       
    16761750                    // Check we can still reach and gather from the target
    1677                     if (this.CheckTargetRange(target, IID_ResourceGatherer) && this.CanGather(target))
     1751                    if (this.CheckTargetRange(target, IID_ResourceGatherer) && this.CanGather(target) && cmpSupply && cmpSupply.IsAvailable(this.entity))
    16781752                    {
    16791753                        // Gather the resources:
    16801754
     
    17141788                        if (!status.exhausted)
    17151789                            return;
    17161790                    }
    1717                     else
     1791                    else if (cmpSupply && cmpSupply.IsAvailable(this.entity))
    17181792                    {
    17191793                        // Try to follow the target
    17201794                        if (this.MoveToTargetRange(target, IID_ResourceGatherer))
     
    29973071        if (template.indexOf("resource|") != -1)
    29983072            template = template.slice(9);
    29993073
    3000         if (amount > 0 && filter(ent, type, template))
     3074        if (amount > 0 && cmpResourceSupply.IsAvailable(this.entity) && filter(ent, type, template))
    30013075            return ent;
    30023076    }
    30033077
  • binaries/data/mods/public/simulation/ai/qbot/worker.js

     
    168168        if (!supply.position()){
    169169            return;
    170170        }
     171                             
     172        if (supply.isFull === true) {
     173            return;
     174        }
    171175       
    172176        // measure the distance to the resource
    173177        var dist = VectorDistance(supply.position(), ent.position());
  • binaries/data/mods/public/simulation/ai/qbot-wc/worker.js

     
    312312            return;
    313313        }
    314314                             
    315         // too many workers trying to gather from this resource
    316         if (supply.getMetadata(PlayerID, "full") === true) {
    317             //debug ("full");
     315        if (supply.isFull === true) {
    318316            return;
    319317        }
    320318                             
  • binaries/data/mods/public/simulation/ai/common-api-v2/entity.js

     
    161161            return undefined;
    162162        return +this._template.ResourceSupply.Amount;
    163163    },
    164    
     164                           
     165    maxGatherers: function()
     166    {
     167        if (this._template.ResourceSupply !== undefined)
     168            return this._template.ResourceSupply.MaxGatherers;
     169        return 0;
     170    },
     171
    165172    resourceGatherRates: function() {
    166173        if (!this._template.ResourceGatherer)
    167174            return undefined;
     
    339346        return this._entity.resourceSupplyAmount;
    340347    },
    341348
     349    resourceSupplyGatherers: function()
     350    {
     351        if (this._entity.resourceSupplyGatherers !== undefined)
     352            return this._entity.resourceSupplyGatherers;
     353        return [];
     354    },
     355   
     356    isFull: function()
     357    {
     358        if (this._entity.resourceSupplyGatherers !== undefined)
     359            return (this.maxGatherers === this._entity.resourceSupplyGatherers.length);
     360        return undefined;
     361    },
     362
    342363    resourceCarrying: function() {
    343364        if(this._entity.resourceCarrying === undefined)
    344365            return undefined;
  • binaries/data/mods/public/simulation/ai/common-api-v3/entity.js

     
    326326        return +this._template.ResourceSupply.Amount;
    327327    },
    328328   
     329    maxGatherers: function()
     330    {
     331        if (this._template.ResourceSupply !== undefined)
     332            return this._template.ResourceSupply.MaxGatherers;
     333        return 0;
     334    },
     335
    329336    resourceGatherRates: function() {
    330337        if (!this._template.ResourceGatherer)
    331338            return undefined;
     
    523530            return undefined;
    524531        return this._entity.resourceSupplyAmount;
    525532    },
     533   
     534    resourceSupplyGatherers: function()
     535    {
     536        if (this._entity.resourceSupplyGatherers !== undefined)
     537            return this._entity.resourceSupplyGatherers;
     538        return [];
     539    },
    526540
     541    isFull: function()
     542    {
     543        if (this._entity.resourceSupplyGatherers !== undefined)
     544            return (this.maxGatherers === this._entity.resourceSupplyGatherers.length);
     545        return undefined;
     546    },
     547
    527548    resourceCarrying: function() {
    528549        if(this._entity.resourceCarrying === undefined)
    529550            return undefined;
  • binaries/data/mods/public/simulation/templates/template_gaia_treasure.xml

     
    2121    <KillBeforeGather>false</KillBeforeGather>
    2222    <Amount>300</Amount>
    2323    <Type>treasure.metal</Type>
     24    <MaxGatherers>1</MaxGatherers>
    2425  </ResourceSupply>
    2526  <Sound>
    2627    <SoundGroups>
  • binaries/data/mods/public/simulation/templates/template_gaia_flora_bush_berry.xml

     
    1919    <KillBeforeGather>false</KillBeforeGather>
    2020    <Amount>200</Amount>
    2121    <Type>food.fruit</Type>
     22    <MaxGatherers>6</MaxGatherers>
    2223  </ResourceSupply>
    2324  <Selectable>
    2425    <EditorOnly disable=""/>
  • binaries/data/mods/public/simulation/templates/template_structure_resource_corral.xml

     
    6060    <KillBeforeGather>false</KillBeforeGather>
    6161    <Amount>200</Amount>
    6262    <Type>food.milk</Type>
     63    <MaxGatherers>6</MaxGatherers>
    6364  </ResourceSupply>
    6465  <Sound>
    6566    <SoundGroups>
  • binaries/data/mods/public/simulation/templates/template_unit_fauna_hunt.xml

     
    88    <KillBeforeGather>true</KillBeforeGather>
    99    <Amount>100</Amount>
    1010    <Type>food.meat</Type>
     11    <MaxGatherers>8</MaxGatherers>
    1112  </ResourceSupply>
    1213</Entity>
  • binaries/data/mods/public/simulation/templates/template_unit_fauna_hunt_whale.xml

     
    1414    <KillBeforeGather>true</KillBeforeGather>
    1515    <Amount>500</Amount>
    1616    <Type>food.meat</Type>
    17   </ResourceSupply>
     17    <MaxGatherers>4</MaxGatherers>
     18 </ResourceSupply>
    1819</Entity>
  • binaries/data/mods/public/simulation/templates/template_unit_fauna_fish.xml

     
    2222    <KillBeforeGather>false</KillBeforeGather>
    2323    <Amount>1000</Amount>
    2424    <Type>food.fish</Type>
     25    <MaxGatherers>4</MaxGatherers>
    2526  </ResourceSupply>
    2627  <Selectable>
    2728    <Overlay>
  • binaries/data/mods/public/simulation/templates/template_unit_fauna_herd.xml

     
    1313    <KillBeforeGather>true</KillBeforeGather>
    1414    <Amount>100</Amount>
    1515    <Type>food.meat</Type>
     16    <MaxGatherers>8</MaxGatherers>
    1617  </ResourceSupply>
    1718  <Vision>
    1819    <Range>20</Range>
  • binaries/data/mods/public/simulation/templates/template_gaia_ruins.xml

     
    2121    <KillBeforeGather>false</KillBeforeGather>
    2222    <Amount>500</Amount>
    2323    <Type>stone.ruins</Type>
     24    <MaxGatherers>1</MaxGatherers>
    2425  </ResourceSupply>
    2526  <Selectable>
    2627    <EditorOnly disable=""/>
  • binaries/data/mods/public/simulation/templates/template_gaia_geo_rock_slabs.xml

     
    1212  </Position>
    1313  <ResourceSupply>
    1414    <Amount>5000</Amount>
     15    <MaxGatherers>16</MaxGatherers>
    1516  </ResourceSupply>
    1617  <Sound>
    1718    <SoundGroups>
  • binaries/data/mods/public/simulation/templates/template_gaia_geo_mineral.xml

     
    1414    <KillBeforeGather>false</KillBeforeGather>
    1515    <Amount>1000</Amount>
    1616    <Type>metal.ore</Type>
     17    <MaxGatherers>8</MaxGatherers>
    1718  </ResourceSupply>
    1819  <Sound>
    1920    <SoundGroups>
  • binaries/data/mods/public/simulation/templates/template_gaia_geo_rock.xml

     
    1414    <KillBeforeGather>false</KillBeforeGather>
    1515    <Amount>1000</Amount>
    1616    <Type>stone.rock</Type>
     17    <MaxGatherers>8</MaxGatherers>
    1718  </ResourceSupply>
    1819  <Sound>
    1920    <SoundGroups>
  • 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>20</MaxGatherers>
    5657  </ResourceSupply>
    5758  <Sound>
    5859    <SoundGroups>
  • binaries/data/mods/public/simulation/templates/template_gaia_geo_mineral_slabs.xml

     
    1212  </Position>
    1313  <ResourceSupply>
    1414    <Amount>5000</Amount>
     15    <MaxGatherers>16</MaxGatherers>
    1516  </ResourceSupply>
    1617  <Sound>
    1718    <SoundGroups>
  • binaries/data/mods/public/simulation/templates/template_gaia_flora_tree.xml

     
    1414    <KillBeforeGather>false</KillBeforeGather>
    1515    <Amount>200</Amount>
    1616    <Type>wood.tree</Type>
     17    <MaxGatherers>6</MaxGatherers>
    1718  </ResourceSupply>
    1819  <Selectable>
    1920    <EditorOnly disable=""/>