IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
|
|
|
5 | 5 | "<text/>" + |
6 | 6 | "</element>"; |
7 | 7 | |
| 8 | Player.prototype.resourceTypes = ["food", "wood", "metal", "stone"]; |
| 9 | |
8 | 10 | Player.prototype.Init = function() |
9 | 11 | { |
10 | 12 | this.playerID = undefined; |
… |
… |
|
15 | 17 | this.popBonuses = 0; // sum of population bonuses of player's entities |
16 | 18 | this.maxPop = 300; // maximum population |
17 | 19 | this.trainingBlocked = false; // indicates whether any training queue is currently blocked |
18 | | this.resourceCount = { |
19 | | "food": 300, |
20 | | "wood": 300, |
21 | | "metal": 300, |
22 | | "stone": 300 |
23 | | }; |
| 20 | this.resourceCount = []; |
| 21 | for (let resourceType of Player.prototype.resourceTypes) |
| 22 | { |
| 23 | this.resourceCount[resourceType] = 300; |
| 24 | } |
24 | 25 | // goods for next trade-route and its proba in % (the sum of probas must be 100) |
25 | 26 | this.tradingGoods = [ |
26 | 27 | { "goods": "wood", "proba": 30 }, |
… |
… |
|
176 | 177 | this.trainingBlocked = false; |
177 | 178 | }; |
178 | 179 | |
| 180 | /** |
| 181 | * Set the quantities for each resource type |
| 182 | * @param resources - an array from resource type to quantity {@see Player.prototype.resourceTypes} |
| 183 | */ |
179 | 184 | Player.prototype.SetResourceCounts = function(resources) |
180 | 185 | { |
181 | | if (resources.food !== undefined) |
182 | | this.resourceCount.food = resources.food; |
183 | | if (resources.wood !== undefined) |
184 | | this.resourceCount.wood = resources.wood; |
185 | | if (resources.stone !== undefined) |
186 | | this.resourceCount.stone = resources.stone; |
187 | | if (resources.metal !== undefined) |
188 | | this.resourceCount.metal = resources.metal; |
| 186 | for (let resourceType of Player.prototype.resourceTypes) |
| 187 | { |
| 188 | if (resources[resourceType] !== undefined) |
| 189 | this.resourceCount[resourceType] = resources[resourceType]; |
| 190 | } |
189 | 191 | }; |
190 | 192 | |
191 | 193 | Player.prototype.GetResourceCounts = function() |
… |
… |
|
340 | 342 | return this.state; |
341 | 343 | }; |
342 | 344 | |
| 345 | /** |
| 346 | * This function will check if the player is still active |
| 347 | * @returns {boolean} - true if player is active, false otherwise (resigned, quit, defeated) |
| 348 | */ |
| 349 | Player.prototype.IsActive = function() |
| 350 | { |
| 351 | return this.state == "active"; |
| 352 | }; |
| 353 | |
343 | 354 | Player.prototype.SetState = function(newState) |
344 | 355 | { |
345 | 356 | this.state = newState; |
… |
… |
|
558 | 569 | }; |
559 | 570 | |
560 | 571 | /** |
| 572 | * Get all the allies' ids of a given player. |
| 573 | * @returns {Array} - All the allies' ids, regardless of state. |
| 574 | */ |
| 575 | Player.prototype.GetAllies = function() |
| 576 | { |
| 577 | var allies = []; |
| 578 | for (let i = 0; i < this.diplomacy.length; i++) |
| 579 | if (this.diplomacy[i] > 0 && i != this.playerID) |
| 580 | allies.push(i); |
| 581 | return allies; |
| 582 | }; |
| 583 | |
| 584 | /** |
| 585 | * Get all the active allies ids of a given player. |
| 586 | * @returns {Array} - All the active allies as actual player objects, not IDs |
| 587 | */ |
| 588 | Player.prototype.GetActiveAlliedPlayers = function() |
| 589 | { |
| 590 | var allies = this.GetAllies(); |
| 591 | var activeAllies = []; |
| 592 | |
| 593 | var cmpPlayerManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager); |
| 594 | |
| 595 | for (var allyId of allies) |
| 596 | { |
| 597 | let cmpPlayer = Engine.QueryInterface(cmpPlayerManager.GetPlayerByID(allyId), IID_Player); |
| 598 | if (cmpPlayer.IsActive()) { |
| 599 | activeAllies.push(cmpPlayer); |
| 600 | } |
| 601 | } |
| 602 | return activeAllies; |
| 603 | }; |
| 604 | |
| 605 | /** |
561 | 606 | * Check if given player is our enemy |
562 | 607 | */ |
563 | 608 | Player.prototype.IsEnemy = function(id) |
… |
… |
|
636 | 681 | { |
637 | 682 | this.state = "defeated"; |
638 | 683 | |
639 | | // TODO: Tribute all resources to this player's active allies (if any) |
| 684 | this.tributeResourcesToAllies(); |
640 | 685 | |
641 | 686 | // Reassign all player's entities to Gaia |
642 | 687 | var cmpRangeManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager); |
… |
… |
|
663 | 708 | var notification = {"type": "defeat", "players": [this.playerID]}; |
664 | 709 | var cmpGUIInterface = Engine.QueryInterface(SYSTEM_ENTITY, IID_GuiInterface); |
665 | 710 | cmpGUIInterface.PushNotification(notification); |
| 711 | }; |
| 712 | |
| 713 | /** |
| 714 | * When a player is defeated, his resources will be split among the still active allies. |
| 715 | */ |
| 716 | Player.prototype.tributeResourcesToAllies = function() |
| 717 | { |
| 718 | var activeAllies = this.GetActiveAlliedPlayers(); |
| 719 | var alliesNumber = activeAllies.length; |
| 720 | |
| 721 | if (alliesNumber == 0 || this.team == -1) { |
| 722 | return; |
| 723 | } |
| 724 | |
| 725 | var resourceForEach = []; |
| 726 | |
| 727 | for (let resourceType of Player.prototype.resourceTypes) |
| 728 | { |
| 729 | resourceForEach[resourceType] = Math.floor(this.resourceCount[resourceType] / alliesNumber); |
| 730 | } |
| 731 | for (let ally of activeAllies) |
| 732 | { |
| 733 | ally.AddResources(resourceForEach); |
| 734 | } |
| 735 | |
| 736 | this.SubtractResourcesOrNotify(this.resourceCount); |
666 | 737 | }; |
667 | 738 | |
668 | 739 | Player.prototype.OnResearchFinished = function(msg) |