Ticket #1387: GathererLimits.patch
File GathererLimits.patch, 20.6 KB (added by , 11 years ago) |
---|
-
binaries/data/mods/public/simulation/components/ResourceSupply.js
29 29 "<value>treasure.metal</value>" + 30 30 "<value>treasure.food</value>" + 31 31 "</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'/>" + 32 35 "</element>"; 33 36 34 37 ResourceSupply.prototype.Init = function() 35 38 { 36 39 // Current resource amount (non-negative) 37 40 this.amount = this.GetMaxAmount(); 41 this.gatherers = []; // list of IDs 38 42 }; 39 43 40 44 ResourceSupply.prototype.GetKillBeforeGather = function() … … 52 56 return this.amount; 53 57 }; 54 58 59 ResourceSupply.prototype.GetMaxGatherers = function() 60 { 61 return +this.template.MaxGatherers; 62 }; 63 ResourceSupply.prototype.GetGatherers = function() 64 { 65 return this.gatherers; 66 }; 67 55 68 ResourceSupply.prototype.TakeResources = function(rate) 56 69 { 57 70 // 'rate' should be a non-negative integer … … 77 90 return { "generic": type, "specific": subtype }; 78 91 }; 79 92 93 ResourceSupply.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 100 ResourceSupply.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? 115 ResourceSupply.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 80 124 Engine.RegisterComponentType(IID_ResourceSupply, "ResourceSupply", ResourceSupply); -
binaries/data/mods/public/simulation/components/GuiInterface.js
261 261 "max": cmpResourceSupply.GetMaxAmount(), 262 262 "amount": cmpResourceSupply.GetCurrentAmount(), 263 263 "type": cmpResourceSupply.GetType(), 264 "killBeforeGather": cmpResourceSupply.GetKillBeforeGather() 264 "killBeforeGather": cmpResourceSupply.GetKillBeforeGather(), 265 "maxGatherers": cmpResourceSupply.GetMaxGatherers(), 266 "gatherers": cmpResourceSupply.GetGatherers() 265 267 }; 266 268 } 267 269 -
binaries/data/mods/public/simulation/components/AIProxy.js
141 141 this.changes.resourceSupplyAmount = msg.to; 142 142 }; 143 143 144 AIProxy.prototype.ResourceSupplyGatherersChanged = function(msg) 145 { 146 this.NotifyChange(); 147 this.changes.resourceSupplyGatherers = msg.to; 148 }; 149 144 150 AIProxy.prototype.OnResourceCarryingChanged = function(msg) 145 151 { 146 152 this.NotifyChange(); … … 225 231 { 226 232 // Updated by OnResourceSupplyChanged 227 233 ret.resourceSupplyAmount = cmpResourceSupply.GetCurrentAmount(); 234 ret.resourceSupplyGatherers = cmpResourceSupply.GetGatherers(); 228 235 } 229 236 230 237 var cmpResourceGatherer = Engine.QueryInterface(this.entity, IID_ResourceGatherer); -
binaries/data/mods/public/simulation/components/interfaces/ResourceSupply.js
3 3 // Message of the form { "from": 100, "to", 90 }, 4 4 // sent whenever supply level changes. 5 5 Engine.RegisterMessageType("ResourceSupplyChanged"); 6 7 // Message of the form { "to", [array of gatherers ID] }, 8 // sent whenever the number of gatherer changes 9 Engine.RegisterMessageType("ResourceSupplyGatherersChanged"); -
binaries/data/mods/public/simulation/components/UnitAI.js
1539 1539 "APPROACHING": { 1540 1540 "enter": function() { 1541 1541 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; 1542 1584 }, 1543 1585 1544 1586 "MoveCompleted": function(msg) { … … 1546 1588 { 1547 1589 // We failed to reach the target 1548 1590 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 1549 1596 // Save the current order's data in case we need it later 1550 1597 var oldType = this.order.data.type; 1551 1598 var oldTarget = this.order.data.target; … … 1580 1627 // We reached the target - start gathering from it now 1581 1628 this.SetNextState("GATHERING"); 1582 1629 }, 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 }, 1583 1637 }, 1584 1638 1585 1639 // Walking to a good place to gather resources near, used by GatherNearPosition … … 1617 1671 "GATHERING": { 1618 1672 "enter": function() { 1619 1673 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 } 1621 1687 // If this order was forced, the player probably gave it, but now we've reached the target 1622 1688 // switch to an unforced order (can be interrupted by attacks) 1623 1689 this.order.data.force = false; … … 1664 1730 1665 1731 "leave": function() { 1666 1732 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 1668 1739 // Show the carried resource, if we've gathered anything. 1669 1740 this.SetGathererAnimationOverride(); 1670 1741 }, … … 1673 1744 var target = this.order.data.target; 1674 1745 var resourceTemplate = this.order.data.template; 1675 1746 var resourceType = this.order.data.type; 1747 1748 var cmpSupply = Engine.QueryInterface(this.gatheringTarget, IID_ResourceSupply); 1749 1676 1750 // 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)) 1678 1752 { 1679 1753 // Gather the resources: 1680 1754 … … 1714 1788 if (!status.exhausted) 1715 1789 return; 1716 1790 } 1717 else 1791 else if (cmpSupply && cmpSupply.IsAvailable(this.entity)) 1718 1792 { 1719 1793 // Try to follow the target 1720 1794 if (this.MoveToTargetRange(target, IID_ResourceGatherer)) … … 2997 3071 if (template.indexOf("resource|") != -1) 2998 3072 template = template.slice(9); 2999 3073 3000 if (amount > 0 && filter(ent, type, template))3074 if (amount > 0 && cmpResourceSupply.IsAvailable(this.entity) && filter(ent, type, template)) 3001 3075 return ent; 3002 3076 } 3003 3077 -
binaries/data/mods/public/simulation/ai/qbot/worker.js
168 168 if (!supply.position()){ 169 169 return; 170 170 } 171 172 if (supply.isFull === true) { 173 return; 174 } 171 175 172 176 // measure the distance to the resource 173 177 var dist = VectorDistance(supply.position(), ent.position()); -
binaries/data/mods/public/simulation/ai/qbot-wc/worker.js
312 312 return; 313 313 } 314 314 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) { 318 316 return; 319 317 } 320 318 -
binaries/data/mods/public/simulation/ai/common-api-v2/entity.js
161 161 return undefined; 162 162 return +this._template.ResourceSupply.Amount; 163 163 }, 164 164 165 maxGatherers: function() 166 { 167 if (this._template.ResourceSupply !== undefined) 168 return this._template.ResourceSupply.MaxGatherers; 169 return 0; 170 }, 171 165 172 resourceGatherRates: function() { 166 173 if (!this._template.ResourceGatherer) 167 174 return undefined; … … 339 346 return this._entity.resourceSupplyAmount; 340 347 }, 341 348 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 342 363 resourceCarrying: function() { 343 364 if(this._entity.resourceCarrying === undefined) 344 365 return undefined; -
binaries/data/mods/public/simulation/ai/common-api-v3/entity.js
326 326 return +this._template.ResourceSupply.Amount; 327 327 }, 328 328 329 maxGatherers: function() 330 { 331 if (this._template.ResourceSupply !== undefined) 332 return this._template.ResourceSupply.MaxGatherers; 333 return 0; 334 }, 335 329 336 resourceGatherRates: function() { 330 337 if (!this._template.ResourceGatherer) 331 338 return undefined; … … 523 530 return undefined; 524 531 return this._entity.resourceSupplyAmount; 525 532 }, 533 534 resourceSupplyGatherers: function() 535 { 536 if (this._entity.resourceSupplyGatherers !== undefined) 537 return this._entity.resourceSupplyGatherers; 538 return []; 539 }, 526 540 541 isFull: function() 542 { 543 if (this._entity.resourceSupplyGatherers !== undefined) 544 return (this.maxGatherers === this._entity.resourceSupplyGatherers.length); 545 return undefined; 546 }, 547 527 548 resourceCarrying: function() { 528 549 if(this._entity.resourceCarrying === undefined) 529 550 return undefined; -
binaries/data/mods/public/simulation/templates/template_gaia_treasure.xml
21 21 <KillBeforeGather>false</KillBeforeGather> 22 22 <Amount>300</Amount> 23 23 <Type>treasure.metal</Type> 24 <MaxGatherers>1</MaxGatherers> 24 25 </ResourceSupply> 25 26 <Sound> 26 27 <SoundGroups> -
binaries/data/mods/public/simulation/templates/template_gaia_flora_bush_berry.xml
19 19 <KillBeforeGather>false</KillBeforeGather> 20 20 <Amount>200</Amount> 21 21 <Type>food.fruit</Type> 22 <MaxGatherers>6</MaxGatherers> 22 23 </ResourceSupply> 23 24 <Selectable> 24 25 <EditorOnly disable=""/> -
binaries/data/mods/public/simulation/templates/template_structure_resource_corral.xml
60 60 <KillBeforeGather>false</KillBeforeGather> 61 61 <Amount>200</Amount> 62 62 <Type>food.milk</Type> 63 <MaxGatherers>6</MaxGatherers> 63 64 </ResourceSupply> 64 65 <Sound> 65 66 <SoundGroups> -
binaries/data/mods/public/simulation/templates/template_unit_fauna_hunt.xml
8 8 <KillBeforeGather>true</KillBeforeGather> 9 9 <Amount>100</Amount> 10 10 <Type>food.meat</Type> 11 <MaxGatherers>8</MaxGatherers> 11 12 </ResourceSupply> 12 13 </Entity> -
binaries/data/mods/public/simulation/templates/template_unit_fauna_hunt_whale.xml
14 14 <KillBeforeGather>true</KillBeforeGather> 15 15 <Amount>500</Amount> 16 16 <Type>food.meat</Type> 17 </ResourceSupply> 17 <MaxGatherers>4</MaxGatherers> 18 </ResourceSupply> 18 19 </Entity> -
binaries/data/mods/public/simulation/templates/template_unit_fauna_fish.xml
22 22 <KillBeforeGather>false</KillBeforeGather> 23 23 <Amount>1000</Amount> 24 24 <Type>food.fish</Type> 25 <MaxGatherers>4</MaxGatherers> 25 26 </ResourceSupply> 26 27 <Selectable> 27 28 <Overlay> -
binaries/data/mods/public/simulation/templates/template_unit_fauna_herd.xml
13 13 <KillBeforeGather>true</KillBeforeGather> 14 14 <Amount>100</Amount> 15 15 <Type>food.meat</Type> 16 <MaxGatherers>8</MaxGatherers> 16 17 </ResourceSupply> 17 18 <Vision> 18 19 <Range>20</Range> -
binaries/data/mods/public/simulation/templates/template_gaia_ruins.xml
21 21 <KillBeforeGather>false</KillBeforeGather> 22 22 <Amount>500</Amount> 23 23 <Type>stone.ruins</Type> 24 <MaxGatherers>1</MaxGatherers> 24 25 </ResourceSupply> 25 26 <Selectable> 26 27 <EditorOnly disable=""/> -
binaries/data/mods/public/simulation/templates/template_gaia_geo_rock_slabs.xml
12 12 </Position> 13 13 <ResourceSupply> 14 14 <Amount>5000</Amount> 15 <MaxGatherers>16</MaxGatherers> 15 16 </ResourceSupply> 16 17 <Sound> 17 18 <SoundGroups> -
binaries/data/mods/public/simulation/templates/template_gaia_geo_mineral.xml
14 14 <KillBeforeGather>false</KillBeforeGather> 15 15 <Amount>1000</Amount> 16 16 <Type>metal.ore</Type> 17 <MaxGatherers>8</MaxGatherers> 17 18 </ResourceSupply> 18 19 <Sound> 19 20 <SoundGroups> -
binaries/data/mods/public/simulation/templates/template_gaia_geo_rock.xml
14 14 <KillBeforeGather>false</KillBeforeGather> 15 15 <Amount>1000</Amount> 16 16 <Type>stone.rock</Type> 17 <MaxGatherers>8</MaxGatherers> 17 18 </ResourceSupply> 18 19 <Sound> 19 20 <SoundGroups> -
binaries/data/mods/public/simulation/templates/template_structure_resource_field.xml
53 53 <KillBeforeGather>false</KillBeforeGather> 54 54 <Amount>2000</Amount> 55 55 <Type>food.grain</Type> 56 <MaxGatherers>20</MaxGatherers> 56 57 </ResourceSupply> 57 58 <Sound> 58 59 <SoundGroups> -
binaries/data/mods/public/simulation/templates/template_gaia_geo_mineral_slabs.xml
12 12 </Position> 13 13 <ResourceSupply> 14 14 <Amount>5000</Amount> 15 <MaxGatherers>16</MaxGatherers> 15 16 </ResourceSupply> 16 17 <Sound> 17 18 <SoundGroups> -
binaries/data/mods/public/simulation/templates/template_gaia_flora_tree.xml
14 14 <KillBeforeGather>false</KillBeforeGather> 15 15 <Amount>200</Amount> 16 16 <Type>wood.tree</Type> 17 <MaxGatherers>6</MaxGatherers> 17 18 </ResourceSupply> 18 19 <Selectable> 19 20 <EditorOnly disable=""/>