Ticket #643: gatherer-count.patch

File gatherer-count.patch, 11.2 KB (added by Deiz, 12 years ago)
  • binaries/data/mods/public/gui/session/session.js

    diff --git a/binaries/data/mods/public/gui/session/session.js b/binaries/data/mods/public/gui/session/session.js
    index 1204df5..b668ee8 100644
    a b var g_EntityStates = {}; // {id:entState}  
    2323// Whether the player has lost/won and reached the end of their game
    2424var g_GameEnded = false;
    2525
     26// The current ID of the timer that updates gatherer counters.
     27var g_GathererPruneTimerID = undefined;
     28
    2629// Colors to flash when pop limit reached
    2730const DEFAULT_POPULATION_COLOR = "white";
    2831const POPULATION_ALERT_COLOR = "orange";
    function updatePlayerDisplay(simState)  
    406409    if (!playerState)
    407410        return;
    408411
    409     getGUIObjectByName("resourceFood").caption = playerState.resourceCounts.food;
    410     getGUIObjectByName("resourceWood").caption = playerState.resourceCounts.wood;
    411     getGUIObjectByName("resourceStone").caption = playerState.resourceCounts.stone;
    412     getGUIObjectByName("resourceMetal").caption = playerState.resourceCounts.metal;
     412    // Remove dead or non-gathering workers from the gatherer arrays once per second.
     413    if (!g_GathererPruneTimerID)
     414    {
     415        g_GathererPruneTimerID = setTimeout(
     416                function() { Engine.GuiInterfaceCall("PruneResourceGatherers", Engine.GetPlayerID());
     417                g_GathererPruneTimerID = undefined; }, 1000
     418        );
     419    }
     420
     421    getGUIObjectByName("resourceFood").caption = playerState.resourceCounts.food +
     422            " (" + playerState.resourceGatherers.food.length + ")";
     423    getGUIObjectByName("resourceWood").caption = playerState.resourceCounts.wood +
     424            " (" + playerState.resourceGatherers.wood.length + ")";
     425    getGUIObjectByName("resourceStone").caption = playerState.resourceCounts.stone +
     426            " (" + playerState.resourceGatherers.stone.length + ")";
     427    getGUIObjectByName("resourceMetal").caption = playerState.resourceCounts.metal +
     428            " (" + playerState.resourceGatherers.metal.length + ")";
    413429    getGUIObjectByName("resourcePop").caption = playerState.popCount + "/" + playerState.popLimit;
    414430
    415431    g_IsTrainingBlocked = playerState.trainingBlocked;
  • binaries/data/mods/public/gui/session/session.xml

    diff --git a/binaries/data/mods/public/gui/session/session.xml b/binaries/data/mods/public/gui/session/session.xml
    index 8aedbf8..24f0273 100644
    a b  
    310310            <!-- Player resource bar -->
    311311            <!-- ================================  ================================ -->
    312312            <object
    313                 size="10 0 45% 100%"
     313                size="5 0 45%+5 100%"
    314314            >
    315315                <!-- Food -->
    316                 <object size="0 0 90 100%" type="image" style="resourceCounter" tooltip="Food" tooltip_style="sessionToolTipBold">
     316                <object size="0 0 100 100%" type="image" style="resourceCounter" tooltip="Food (Gatherers)" tooltip_style="sessionToolTipBold">
    317317                    <object size="0 -4 40 36" type="image" sprite="stretched:session/icons/resources/food.png"/>
    318318                    <object size="32 0 100% 100%-2" type="text" style="resourceText" name="resourceFood"/>
    319319                </object>
    320320
    321321                <!-- Wood -->
    322                 <object size="90 0 180 100%" type="image" style="resourceCounter" tooltip="Wood" tooltip_style="sessionToolTipBold">
     322                <object size="90 0 190 100%" type="image" style="resourceCounter" tooltip="Wood (Gatherers)" tooltip_style="sessionToolTipBold">
    323323                    <object size="0 -4 40 36" type="image" sprite="stretched:session/icons/resources/wood.png"/>
    324324                    <object size="32 0 100% 100%-2" type="text" style="resourceText" name="resourceWood"/>
    325325                </object>
    326326
    327327                <!-- Stone -->
    328                 <object size="180 0 270 100%" type="image" style="resourceCounter" tooltip="Stone" tooltip_style="sessionToolTipBold">
     328                <object size="180 0 280 100%" type="image" style="resourceCounter" tooltip="Stone (Gatherers)" tooltip_style="sessionToolTipBold">
    329329                    <object size="0 -4 40 36" type="image" sprite="stretched:session/icons/resources/stone.png"/>
    330330                    <object size="32 0 100% 100%-2" type="text" style="resourceText" name="resourceStone"/>
    331331                </object>
    332332
    333333                <!-- Metal -->
    334                 <object size="270 0 360 100%"  type="image" style="resourceCounter" tooltip="Metal" tooltip_style="sessionToolTipBold">
     334                <object size="270 0 370 100%" type="image" style="resourceCounter" tooltip="Metal (Gatherers)" tooltip_style="sessionToolTipBold">
    335335                    <object size="0 -4 40 36" type="image" sprite="stretched:session/icons/resources/metal.png"/>
    336336                    <object size="32 0 100% 100%-2" type="text" style="resourceText" name="resourceMetal"/>
    337337                </object>
    338338
    339339                <!-- Population -->
    340                 <object size="360 0 450 100%" type="image" style="resourceCounter" tooltip="Population (current / limit)" tooltip_style="sessionToolTipBold">
     340                <object size="365 0 450 100%" type="image" style="resourceCounter" tooltip="Population (current / limit)" tooltip_style="sessionToolTipBold">
    341341                    <object size="0 -4 40 34" type="image" sprite="stretched:session/icons/resources/population.png"/>
    342342                    <object size="32 0 100% 100%-2" type="text" style="resourceText" name="resourcePop"/>
    343343                </object>
  • binaries/data/mods/public/simulation/components/GuiInterface.js

    diff --git a/binaries/data/mods/public/simulation/components/GuiInterface.js b/binaries/data/mods/public/simulation/components/GuiInterface.js
    index 4ce8c71..346976a 100644
    a b GuiInterface.prototype.GetSimulationState = function(player)  
    7878            "popLimit": cmpPlayer.GetPopulationLimit(),
    7979            "popMax": cmpPlayer.GetMaxPopulation(),
    8080            "resourceCounts": cmpPlayer.GetResourceCounts(),
     81            "resourceGatherers": cmpPlayer.GetResourceGatherers(),
    8182            "trainingBlocked": cmpPlayer.IsTrainingBlocked(),
    8283            "state": cmpPlayer.GetState(),
    8384            "team": cmpPlayer.GetTeam(),
    GuiInterface.prototype.FindIdleUnit = function(player, data)  
    14521453    return 0;
    14531454};
    14541455
     1456GuiInterface.prototype.PruneResourceGatherers = function(player)
     1457{
     1458    var cmpPlayerMan = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager);
     1459    var cmpPlayer = Engine.QueryInterface(cmpPlayerMan.GetPlayerByID(player), IID_Player);
     1460    cmpPlayer.PruneResourceGatherers();
     1461};
     1462
    14551463GuiInterface.prototype.GetTradingDetails = function(player, data)
    14561464{
    14571465    var cmpEntityTrader = Engine.QueryInterface(data.trader, IID_Trader);
    var exposedFunctions = {  
    15721580    "GetFoundationSnapData": 1,
    15731581    "PlaySound": 1,
    15741582    "FindIdleUnit": 1,
     1583    "PruneResourceGatherers": 1,
    15751584    "GetTradingDetails": 1,
    15761585    "CanAttack": 1,
    15771586
  • binaries/data/mods/public/simulation/components/Player.js

    diff --git a/binaries/data/mods/public/simulation/components/Player.js b/binaries/data/mods/public/simulation/components/Player.js
    index cb73201..c3cd07e 100644
    a b Player.prototype.Init = function()  
    1919        "metal": 300,   
    2020        "stone": 300   
    2121    };
     22    this.resourceGatherers = {
     23        "food": [],
     24        "wood": [],
     25        "metal": [],
     26        "stone": []
     27    };
    2228
    2329    this.team = -1; // team number of the player, players on the same team will always have ally diplomatic status - also this is useful for team emblems, scoring, etc.
    2430    this.state = "active"; // game state - one of "active", "defeated", "won"
    Player.prototype.GetResourceCounts = function()  
    137143    return this.resourceCount;
    138144};
    139145
     146Player.prototype.PruneResourceGatherers = function()
     147{
     148    for (var resource in this.resourceGatherers)
     149    {
     150        for (var i = 0; i < this.resourceGatherers[resource].length; i++)
     151        {
     152            var gatherer = this.resourceGatherers[resource][i];
     153            var cmpTempMan = Engine.QueryInterface(SYSTEM_ENTITY, IID_TemplateManager);
     154            var entState = cmpTempMan.GetCurrentTemplateName(gatherer);
     155
     156            // Prune entities if they no longer exist.
     157            if (!entState)
     158            {
     159                delete this.resourceGatherers[resource][i];
     160                continue;
     161            }
     162
     163            var cmpResourceGatherer = Engine.QueryInterface(gatherer, IID_ResourceGatherer);
     164            var cmpUnitAI = Engine.QueryInterface(gatherer, IID_UnitAI);
     165            var gatheredResource = undefined;
     166            for (var j = 0; j < cmpUnitAI.orderQueue.length; j++ )
     167            {
     168                var order = cmpUnitAI.orderQueue[j];
     169                if (order.type == "Gather" && !order.data.force)
     170                    var gatheredResource = order.data.type.generic;
     171            }
     172
     173            // Prune entities if they're no longer gathering or gathering a different resource.
     174            if (!cmpResourceGatherer || !gatheredResource || gatheredResource != resource)
     175            {
     176                cmpResourceGatherer.lastGathered = undefined;
     177                delete this.resourceGatherers[resource][i];
     178            }
     179        }
     180
     181        // Remove undefined elements.
     182        this.resourceGatherers[resource] = this.resourceGatherers[resource].filter(Number);
     183    }
     184};
     185
     186Player.prototype.GetResourceGatherers = function()
     187{
     188    // Define if necessary, for older saves.
     189    if (typeof this.resourceGatherers === "undefined")
     190    {
     191        this.resourceGatherers = {
     192            "food": [],
     193            "wood": [],
     194            "metal": [],
     195            "stone": []
     196        };
     197    }
     198    return this.resourceGatherers;
     199};
     200
     201Player.prototype.AddResourceGatherer = function(gatherer, type)
     202{
     203    // We want to add units that are gathering a new resource.
     204    var cmpResourceGatherer = Engine.QueryInterface(gatherer, IID_ResourceGatherer);
     205    if (cmpResourceGatherer)
     206    {
     207        var lastGathered = cmpResourceGatherer.GetLastGathered();
     208        if (!lastGathered || lastGathered != type.generic)
     209        {
     210            cmpResourceGatherer.lastGathered = type;
     211            this.resourceGatherers[type.generic].push(gatherer);
     212        }
     213    }
     214};
     215
    140216/**
    141217 * Add resource of specified type to player
    142218 * @param type Generic type of resource (string)
  • binaries/data/mods/public/simulation/components/ResourceGatherer.js

    diff --git a/binaries/data/mods/public/simulation/components/ResourceGatherer.js b/binaries/data/mods/public/simulation/components/ResourceGatherer.js
    index 503c1b5..7df8ce2 100644
    a b ResourceGatherer.prototype.Init = function()  
    6565
    6666    // The last exact type gathered, so we can render appropriate props
    6767    this.lastCarriedType = undefined; // { generic, specific }
     68    this.lastGathered = undefined;
    6869};
    6970
    7071/**
    ResourceGatherer.prototype.GetLastCarriedType = function()  
    125126        return undefined;
    126127};
    127128
     129ResourceGatherer.prototype.GetLastGathered = function()
     130{
     131    if ( this.lastGathered )
     132        return this.lastGathered.generic;
     133    else
     134        return undefined;
     135}
     136
    128137ResourceGatherer.prototype.GetGatherRates = function()
    129138{
    130139    var ret = {};
  • binaries/data/mods/public/simulation/components/UnitAI.js

    diff --git a/binaries/data/mods/public/simulation/components/UnitAI.js b/binaries/data/mods/public/simulation/components/UnitAI.js
    index 2d38f61..fe18f7a 100644
    a b var UnitFsmSpec = {  
    10221022                        return;
    10231023                    }
    10241024
     1025                    // Attempt to put the gatherer into the player's gatherer array.
     1026                    var cmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership);
     1027                    if (cmpOwnership)
     1028                    {
     1029                        var owner = cmpOwnership.GetOwner();
     1030                        var cmpPlayerMan = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager);
     1031                        var cmpPlayer = Engine.QueryInterface(cmpPlayerMan.GetPlayerByID(owner), IID_Player);
     1032                        if (cmpPlayer)
     1033                            cmpPlayer.AddResourceGatherer(this.entity, this.order.data.type);
     1034                    }
     1035
    10251036                    // Scale timing interval based on rate, and start timer
    10261037                    // The offset should be at least as long as the repeat time so we use the same value for both.
    10271038                    var offset = 1000/rate;