Ticket #643: gatherer-count-2.patch

File gatherer-count-2.patch, 11.0 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..64162a0 100644
    a b function updatePlayerDisplay(simState)  
    406406    if (!playerState)
    407407        return;
    408408
    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;
     409    getGUIObjectByName("resourceFood").caption = playerState.resourceCounts.food +
     410            " (" + playerState.resourceGatherers.count.food + ")";
     411    getGUIObjectByName("resourceWood").caption = playerState.resourceCounts.wood +
     412            " (" + playerState.resourceGatherers.count.wood + ")";
     413    getGUIObjectByName("resourceStone").caption = playerState.resourceCounts.stone +
     414            " (" + playerState.resourceGatherers.count.stone + ")";
     415    getGUIObjectByName("resourceMetal").caption = playerState.resourceCounts.metal +
     416            " (" + playerState.resourceGatherers.count.metal + ")";
    413417    getGUIObjectByName("resourcePop").caption = playerState.popCount + "/" + playerState.popLimit;
    414418
    415419    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..4f64329 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(),
  • 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..f7e6c89 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        "count": {
     28            "food": 0,
     29            "wood": 0,
     30            "metal": 0,
     31            "stone": 0
     32        }
     33    };
    2234
    2335    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.
    2436    this.state = "active"; // game state - one of "active", "defeated", "won"
    Player.prototype.GetResourceCounts = function()  
    137149    return this.resourceCount;
    138150};
    139151
     152Player.prototype.RemoveResourceGatherer = function(gatherer, force)
     153{
     154    var cmpResourceGatherer = Engine.QueryInterface(gatherer, IID_ResourceGatherer);
     155    if (!cmpResourceGatherer)
     156        return;
     157
     158    var resource = cmpResourceGatherer.GetLastGathered();
     159    if (!resource)
     160        return;
     161
     162    var cmpUnitAI = Engine.QueryInterface(gatherer, IID_UnitAI);
     163    var gatheredResource = undefined;
     164    for (var j = 0; j < cmpUnitAI.orderQueue.length; j++ )
     165    {
     166        var order = cmpUnitAI.orderQueue[j];
     167        if (order.type == "Gather" && !order.data.force)
     168            gatheredResource = order.data.type.generic;
     169    }
     170
     171    // Prune entities if they're no longer gathering or gathering a different resource.
     172    if (!gatheredResource || gatheredResource != resource.generic || force)
     173    {
     174        cmpResourceGatherer.lastGathered = undefined;
     175        var resourceType = (resource.generic == "treasure") ? resource.specific : resource.generic;
     176        delete this.resourceGatherers[resourceType][gatherer];
     177        this.resourceGatherers.count[resourceType]--;
     178    }
     179};
     180
     181Player.prototype.GetResourceGatherers = function()
     182{
     183    // Define if necessary, for older saves.
     184    if (typeof this.resourceGatherers === "undefined")
     185    {
     186        this.resourceGatherers = {
     187            "food": {},
     188            "wood": {},
     189            "metal": {},
     190            "stone": {},
     191            "count": {
     192                "food": 0,
     193                "wood": 0,
     194                "metal": 0,
     195                "stone": 0
     196            }
     197        };
     198    }
     199
     200    return this.resourceGatherers;
     201};
     202
     203Player.prototype.AddResourceGatherer = function(gatherer, type)
     204{
     205    // We want to add units that are gathering a new resource.
     206    var cmpResourceGatherer = Engine.QueryInterface(gatherer, IID_ResourceGatherer);
     207    if (cmpResourceGatherer)
     208    {
     209        var lastGathered = cmpResourceGatherer.GetLastGathered();
     210        if (!lastGathered || lastGathered.generic != type.generic)
     211        {
     212            cmpResourceGatherer.lastGathered = type;
     213            var resourceType = (type.generic == "treasure") ? type.specific : type.generic;
     214            this.resourceGatherers[resourceType][gatherer] = true;
     215            this.resourceGatherers.count[resourceType]++;
     216        }
     217    }
     218};
     219
    140220/**
    141221 * Add resource of specified type to player
    142222 * @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..47559d5 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;
     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..ef9cd3d 100644
    a b var UnitFsmSpec = {  
    10221022                        return;
    10231023                    }
    10241024
     1025                    var cmpPlayer = this.GetOwner(this.entity);
     1026                    if (cmpPlayer)
     1027                        cmpPlayer.AddResourceGatherer(this.entity, this.order.data.type);
     1028
    10251029                    // Scale timing interval based on rate, and start timer
    10261030                    // The offset should be at least as long as the repeat time so we use the same value for both.
    10271031                    var offset = 1000/rate;
    var UnitFsmSpec = {  
    10411045                },
    10421046
    10431047                "leave": function() {
     1048                    var cmpPlayer = this.GetOwner(this.entity);
     1049                    if (cmpPlayer)
     1050                        cmpPlayer.RemoveResourceGatherer(this.entity, false);
     1051
    10441052                    this.StopTimer();
    10451053                },
    10461054
    UnitAI.prototype.OnOwnershipChanged = function(msg)  
    18121820    this.SetupRangeQuery();
    18131821    if (this.IsHealer())
    18141822        this.SetupHealRangeQuery();
     1823
     1824    // Ensure the entity is no longer counted as gathering.
     1825    if (msg.from != -1)
     1826    {
     1827        var cmpPlayerMan = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager);
     1828        var cmpPlayer = Engine.QueryInterface(cmpPlayerMan.GetPlayerByID(msg.from), IID_Player);
     1829        if (cmpPlayer)
     1830            cmpPlayer.RemoveResourceGatherer(this.entity, true);
     1831    }
    18151832};
    18161833
    18171834UnitAI.prototype.OnDestroy = function()
    UnitAI.prototype.FsmStateNameChanged = function(state)  
    19271944    Engine.PostMessage(this.entity, MT_UnitAIStateChanged, { "to": state });
    19281945};
    19291946
     1947UnitAI.prototype.GetOwner = function(entity)
     1948{
     1949    var cmpOwnership = Engine.QueryInterface(entity, IID_Ownership);
     1950    if (!cmpOwnership)
     1951        return undefined;
     1952
     1953    var owner = cmpOwnership.GetOwner();
     1954    if (owner == -1)
     1955        return undefined;
     1956
     1957    var cmpPlayerMan = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager);
     1958    var cmpPlayer = Engine.QueryInterface(cmpPlayerMan.GetPlayerByID(owner), IID_Player);
     1959    if (!cmpPlayer)
     1960        return undefined;
     1961
     1962    return cmpPlayer;
     1963};
     1964
    19301965/**
    19311966 * Call when the current order has been completed (or failed).
    19321967 * Removes the current order from the queue, and processes the