Ticket #643: gatherer-count-3.patch

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

     
    416416    if (!playerState)
    417417        return;
    418418
    419     getGUIObjectByName("resourceFood").caption = playerState.resourceCounts.food;
    420     getGUIObjectByName("resourceWood").caption = playerState.resourceCounts.wood;
    421     getGUIObjectByName("resourceStone").caption = playerState.resourceCounts.stone;
    422     getGUIObjectByName("resourceMetal").caption = playerState.resourceCounts.metal;
     419    getGUIObjectByName("resourceFood").caption = playerState.resourceCounts.food +
     420            " (" + playerState.resourceGatherers.count.food + ")";
     421    getGUIObjectByName("resourceWood").caption = playerState.resourceCounts.wood +
     422            " (" + playerState.resourceGatherers.count.wood + ")";
     423    getGUIObjectByName("resourceStone").caption = playerState.resourceCounts.stone +
     424            " (" + playerState.resourceGatherers.count.stone + ")";
     425    getGUIObjectByName("resourceMetal").caption = playerState.resourceCounts.metal +
     426            " (" + playerState.resourceGatherers.count.metal + ")";
    423427    getGUIObjectByName("resourcePop").caption = playerState.popCount + "/" + playerState.popLimit;
    424428
    425429    g_IsTrainingBlocked = playerState.trainingBlocked;
  • binaries/data/mods/public/gui/session/session.xml

     
    311311        <!-- Player resource bar -->
    312312        <!-- ================================  ================================ -->
    313313        <object
    314         size="10 0 45% 100%"
     314        size="5 0 45%+5 100%"
    315315        >
    316316        <!-- Food -->
    317         <object size="0 0 90 100%" type="image" style="resourceCounter" tooltip="Food" tooltip_style="sessionToolTipBold">
     317        <object size="0 0 100 100%" type="image" style="resourceCounter" tooltip="Food (Gatherers)" tooltip_style="sessionToolTipBold">
    318318            <object size="0 -4 40 36" type="image" sprite="stretched:session/icons/resources/food.png"/>
    319319            <object size="32 0 100% 100%-2" type="text" style="resourceText" name="resourceFood"/>
    320320        </object>
    321321
    322322        <!-- Wood -->
    323         <object size="90 0 180 100%" type="image" style="resourceCounter" tooltip="Wood" tooltip_style="sessionToolTipBold">
     323        <object size="90 0 190 100%" type="image" style="resourceCounter" tooltip="Wood (Gatherers)" tooltip_style="sessionToolTipBold">
    324324            <object size="0 -4 40 36" type="image" sprite="stretched:session/icons/resources/wood.png"/>
    325325            <object size="32 0 100% 100%-2" type="text" style="resourceText" name="resourceWood"/>
    326326        </object>
    327327
    328328        <!-- Stone -->
    329         <object size="180 0 270 100%" type="image" style="resourceCounter" tooltip="Stone" tooltip_style="sessionToolTipBold">
     329        <object size="180 0 280 100%" type="image" style="resourceCounter" tooltip="Stone (Gatherers)" tooltip_style="sessionToolTipBold">
    330330            <object size="0 -4 40 36" type="image" sprite="stretched:session/icons/resources/stone.png"/>
    331331            <object size="32 0 100% 100%-2" type="text" style="resourceText" name="resourceStone"/>
    332332        </object>
    333333
    334334        <!-- Metal -->
    335         <object size="270 0 360 100%"  type="image" style="resourceCounter" tooltip="Metal" tooltip_style="sessionToolTipBold">
     335        <object size="270 0 370 100%" type="image" style="resourceCounter" tooltip="Metal (Gatherers)" tooltip_style="sessionToolTipBold">
    336336            <object size="0 -4 40 36" type="image" sprite="stretched:session/icons/resources/metal.png"/>
    337337            <object size="32 0 100% 100%-2" type="text" style="resourceText" name="resourceMetal"/>
    338338        </object>
    339339
    340340        <!-- Population -->
    341         <object size="360 0 450 100%" type="image" style="resourceCounter" tooltip="Population (current / limit)" tooltip_style="sessionToolTipBold">
     341        <object size="365 0 450 100%" type="image" style="resourceCounter" tooltip="Population (current / limit)" tooltip_style="sessionToolTipBold">
    342342            <object size="0 -4 40 34" type="image" sprite="stretched:session/icons/resources/population.png"/>
    343343            <object size="32 0 100% 100%-2" type="text" style="resourceText" name="resourcePop"/>
    344344        </object>
  • binaries/data/mods/public/simulation/components/UnitAI.js

     
    10431043                    {
    10441044                        var typename = "gather_" + this.order.data.type.specific;
    10451045                        this.SelectAnimation(typename, false, 1.0, typename);
     1046
     1047                        var cmpPlayer = this.GetOwner(this.entity);
     1048                        if (cmpPlayer)
     1049                            if (cmpPlayer.AddResourceGatherer(this.entity, this.order.data.type))
     1050                                this.isGatherer = true;
    10461051                    }
    10471052                },
    10481053
    10491054                "leave": function() {
     1055                    var cmpPlayer = this.GetOwner(this.entity);
     1056                    if (cmpPlayer && cmpPlayer.RemoveResourceGatherer(this.entity, false))
     1057                        this.isGatherer = false;
     1058
    10501059                    this.StopTimer();
    10511060                },
    10521061
     
    17661775    this.formationController = INVALID_ENTITY; // entity with IID_Formation that we belong to
    17671776    this.isGarrisoned = false;
    17681777    this.isIdle = false;
     1778    this.isGatherer = false;
    17691779    this.lastFormationName = "";
    17701780
    17711781    this.SetStance(this.template.DefaultStance);
     
    17991809    return this.isIdle;
    18001810};
    18011811
     1812UnitAI.prototype.SetGathering = function(gathering)
     1813{
     1814    this.isGatherer = gathering;
     1815};
     1816
     1817UnitAI.prototype.IsGatherer = function()
     1818{
     1819    return this.isGatherer;
     1820};
     1821
    18021822UnitAI.prototype.IsGarrisoned = function()
    18031823{
    18041824    return this.isGarrisoned;
     
    18191839    this.SetupRangeQuery();
    18201840    if (this.IsHealer())
    18211841        this.SetupHealRangeQuery();
     1842
     1843    // Ensure the entity is no longer counted as gathering.
     1844    if (msg.from != -1 && this.isGatherer && !this.IsFormationController())
     1845    {
     1846        var cmpPlayerMan = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager);
     1847        var cmpPlayer = Engine.QueryInterface(cmpPlayerMan.GetPlayerByID(msg.from), IID_Player);
     1848        if (cmpPlayer)
     1849            cmpPlayer.RemoveResourceGatherer(this.entity, true);
     1850    }
    18221851};
    18231852
    18241853UnitAI.prototype.OnDestroy = function()
     
    19341963    Engine.PostMessage(this.entity, MT_UnitAIStateChanged, { "to": state });
    19351964};
    19361965
     1966UnitAI.prototype.GetOwner = function(entity)
     1967{
     1968    var cmpOwnership = Engine.QueryInterface(entity, IID_Ownership);
     1969    if (!cmpOwnership)
     1970        return undefined;
     1971
     1972    var owner = cmpOwnership.GetOwner();
     1973    if (owner == -1)
     1974        return undefined;
     1975
     1976    var cmpPlayerMan = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager);
     1977    var cmpPlayer = Engine.QueryInterface(cmpPlayerMan.GetPlayerByID(owner), IID_Player);
     1978    if (!cmpPlayer)
     1979        return undefined;
     1980
     1981    return cmpPlayer;
     1982};
     1983
    19371984/**
    19381985 * Call when the current order has been completed (or failed).
    19391986 * Removes the current order from the queue, and processes the
     
    20492096    {
    20502097        this.orderQueue = [];
    20512098        this.PushOrder(type, data);
     2099
     2100        if (this.isGatherer)
     2101        {
     2102            var cmpPlayer = this.GetOwner(this.entity);
     2103            if (cmpPlayer && cmpPlayer.RemoveResourceGatherer(this.entity, true))
     2104                this.isGatherer = false;
     2105        }
    20522106    }
    20532107};
    20542108
  • binaries/data/mods/public/simulation/components/Player.js

     
    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"
     
    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 false;
     157
     158    var resource = cmpResourceGatherer.GetLastGathered();
     159    if (!resource)
     160        return false;
     161
     162    var cmpUnitAI = Engine.QueryInterface(gatherer, IID_UnitAI);
     163    var orders = cmpUnitAI.GetOrders();
     164
     165    var gatheredResource = undefined;
     166    for (var j = 0; j < orders.length; ++j)
     167    {
     168        var order = orders[j];
     169        if (order.type == "Gather" && !order.data.force)
     170            gatheredResource = order.data.type.generic;
     171    }
     172
     173    // Prune entities if they're no longer gathering or gathering a different resource.
     174    if (!gatheredResource || gatheredResource != resource.generic || force)
     175    {
     176        cmpResourceGatherer.SetLastGathered(undefined);
     177        var resourceType = (resource.generic == "treasure") ? resource.specific : resource.generic;
     178        delete this.resourceGatherers[resourceType][gatherer];
     179        this.resourceGatherers.count[resourceType]--;
     180        return true;
     181    }
     182
     183    return false;
     184};
     185
     186Player.prototype.GetResourceGatherers = function()
     187{
     188    return this.resourceGatherers;
     189};
     190
     191Player.prototype.AddResourceGatherer = function(gatherer, type)
     192{
     193    // We want to add units that are gathering a new resource.
     194    var cmpResourceGatherer = Engine.QueryInterface(gatherer, IID_ResourceGatherer);
     195    if (cmpResourceGatherer)
     196    {
     197        var lastGathered = cmpResourceGatherer.GetLastGathered();
     198        if (!lastGathered || lastGathered.generic != type.generic)
     199        {
     200            cmpResourceGatherer.SetLastGathered(type);
     201            var resourceType = (type.generic == "treasure") ? type.specific : type.generic;
     202            this.resourceGatherers[resourceType][gatherer] = true;
     203            this.resourceGatherers.count[resourceType]++;
     204            return true;
     205        }
     206    }
     207    return false;
     208};
     209
    140210/**
    141211 * Add resource of specified type to player
    142212 * @param type Generic type of resource (string)
  • binaries/data/mods/public/simulation/components/Promotion.js

     
    7474    {
    7575        var carriedResorces = cmpCurrentUnitResourceGatherer.GetCarryingStatus();
    7676        cmpPromotedUnitResourceGatherer.GiveResources(carriedResorces);
     77        cmpPromotedUnitResourceGatherer.SetLastCarriedType( cmpCurrentUnitResourceGatherer.GetLastCarriedType() );
    7778    }
    7879   
    7980    var cmpCurrentUnitAI = Engine.QueryInterface(this.entity, IID_UnitAI);
     
    8586    var orders = cmpCurrentUnitAI.GetOrders();
    8687    cmpPromotedUnitAI.AddOrders(orders);
    8788
     89    // Add the promoted entity to the gatherer counter if necessary. The
     90    // old entity will be removed from the counter upon its destruction.
     91    if (cmpCurrentUnitAI.IsGatherer() && cmpCurrentUnitResourceGatherer && cmpPromotedUnitResourceGatherer)
     92    {
     93        var cmpPlayer = cmpPromotedUnitAI.GetOwner(promotedUnitEntity);
     94        if (cmpPlayer)
     95            cmpPromotedUnitAI.SetGathering( cmpPlayer.AddResourceGatherer(promotedUnitEntity,
     96                    cmpCurrentUnitResourceGatherer.GetLastGathered()) );
     97    }
     98
    8899    Engine.BroadcastMessage(MT_EntityRenamed, { entity: this.entity, newentity: promotedUnitEntity });
    89100
    90101    // Destroy current entity
  • binaries/data/mods/public/simulation/components/GuiInterface.js

     
    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/ResourceGatherer.js

     
    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/**
     
    8788
    8889/**
    8990 * Used to instantly give resources to unit
    90  * @param resources The same structure as returned form GetCarryingStatus
     91 * @param resources The same structure as returned from GetCarryingStatus
    9192 */
    9293ResourceGatherer.prototype.GiveResources = function(resources)
    9394{
     
    125126        return undefined;
    126127};
    127128
     129ResourceGatherer.prototype.SetLastCarriedType = function(type)
     130{
     131    this.lastCarriedType = type;
     132};
     133
     134/**
     135 * Gets the type of the last resource that was gathered. Unlike
     136 * lastCarriedType, this is updated as soon as a unit initiates
     137 * gathering, not when it completes its first full action.
     138 */
     139ResourceGatherer.prototype.GetLastGathered = function()
     140{
     141    if (this.lastGathered)
     142        return this.lastGathered;
     143    else
     144        return undefined;
     145}
     146
     147ResourceGatherer.prototype.SetLastGathered = function(type)
     148{
     149    this.lastGathered = type;
     150}
     151
    128152ResourceGatherer.prototype.GetGatherRates = function()
    129153{
    130154    var ret = {};