Ticket #3265: [PATCH] 3265_-_Tribute_all_resources_to_active_allies_before_resigning.patch

File [PATCH] 3265_-_Tribute_all_resources_to_active_allies_before_resigning.patch, 4.7 KB (added by Vlad Topala, 8 years ago)

[PATCH]

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

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
    55        "<text/>" +
    66    "</element>";
    77
     8Player.prototype.resourceTypes = ["food", "wood", "metal", "stone"];
     9
    810Player.prototype.Init = function()
    911{
    1012    this.playerID = undefined;
     
    1517    this.popBonuses = 0; // sum of population bonuses of player's entities
    1618    this.maxPop = 300; // maximum population
    1719    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    }
    2425    // goods for next trade-route and its proba in % (the sum of probas must be 100)
    2526    this.tradingGoods = [
    2627        { "goods":  "wood", "proba": 30 },
     
    176177    this.trainingBlocked = false;
    177178};
    178179
     180/**
     181 * Set the quantities for each resource type
     182 * @param resources - an array from resource type to quantity {@see Player.prototype.resourceTypes}
     183 */
    179184Player.prototype.SetResourceCounts = function(resources)
    180185{
    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    }
    189191};
    190192
    191193Player.prototype.GetResourceCounts = function()
     
    340342    return this.state;
    341343};
    342344
     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 */
     349Player.prototype.IsActive = function()
     350{
     351    return this.state == "active";
     352};
     353
    343354Player.prototype.SetState = function(newState)
    344355{
    345356    this.state = newState;
     
    558569};
    559570
    560571/**
     572 * Get all the allies' ids of a given player.
     573 * @returns {Array} - All the allies' ids, regardless of state.
     574 */
     575Player.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 */
     588Player.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/**
    561606 * Check if given player is our enemy
    562607 */
    563608Player.prototype.IsEnemy = function(id)
     
    636681{
    637682    this.state = "defeated";
    638683
    639     // TODO: Tribute all resources to this player's active allies (if any)
     684    this.tributeResourcesToAllies();
    640685
    641686    // Reassign all player's entities to Gaia
    642687    var cmpRangeManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager);
     
    663708    var notification = {"type": "defeat", "players": [this.playerID]};
    664709    var cmpGUIInterface = Engine.QueryInterface(SYSTEM_ENTITY, IID_GuiInterface);
    665710    cmpGUIInterface.PushNotification(notification);
     711};
     712
     713/**
     714 * When a player is defeated, his resources will be split among the still active allies.
     715 */
     716Player.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);
    666737};
    667738
    668739Player.prototype.OnResearchFinished = function(msg)