Ticket #643: gatherer-count-11052012.patch

File gatherer-count-11052012.patch, 13.3 KB (added by historic_bruno, 11 years ago)
  • binaries/data/mods/public/gui/session/session.js

     
    443443    if (!playerState)
    444444        return;
    445445
    446     getGUIObjectByName("resourceFood").caption = playerState.resourceCounts.food;
    447     getGUIObjectByName("resourceWood").caption = playerState.resourceCounts.wood;
    448     getGUIObjectByName("resourceStone").caption = playerState.resourceCounts.stone;
    449     getGUIObjectByName("resourceMetal").caption = playerState.resourceCounts.metal;
     446    getGUIObjectByName("resourceFood").caption = playerState.resourceCounts.food +
     447            " (" + playerState.resourceGatherers.count.food + ")";
     448    getGUIObjectByName("resourceWood").caption = playerState.resourceCounts.wood +
     449            " (" + playerState.resourceGatherers.count.wood + ")";
     450    getGUIObjectByName("resourceStone").caption = playerState.resourceCounts.stone +
     451            " (" + playerState.resourceGatherers.count.stone + ")";
     452    getGUIObjectByName("resourceMetal").caption = playerState.resourceCounts.metal +
     453            " (" + playerState.resourceGatherers.count.metal + ")";
    450454    getGUIObjectByName("resourcePop").caption = playerState.popCount + "/" + playerState.popLimit;
    451455
    452456    g_IsTrainingBlocked = playerState.trainingBlocked;
  • binaries/data/mods/public/gui/session/session.xml

     
    422422        <!-- Player resource bar -->
    423423        <!-- ================================  ================================ -->
    424424        <object
    425         size="10 0 45% 100%"
     425        size="5 0 45%+5 100%"
    426426        >
    427427        <!-- Food -->
    428         <object size="0 0 90 100%" type="image" style="resourceCounter" tooltip="Food" tooltip_style="sessionToolTipBold">
     428        <object size="0 0 20% 100%" type="image" style="resourceCounter" tooltip="Food (Gatherers)" tooltip_style="sessionToolTipBold">
    429429            <object size="0 -4 40 36" type="image" sprite="stretched:session/icons/resources/food.png" ghost="true"/>
    430430            <object size="32 0 100% 100%-2" type="text" style="resourceText" name="resourceFood"/>
    431431        </object>
    432432
    433433        <!-- Wood -->
    434         <object size="90 0 180 100%" type="image" style="resourceCounter" tooltip="Wood" tooltip_style="sessionToolTipBold">
     434        <object size="20% 0 40% 100%" type="image" style="resourceCounter" tooltip="Wood (Gatherers)" tooltip_style="sessionToolTipBold">
    435435            <object size="0 -4 40 36" type="image" sprite="stretched:session/icons/resources/wood.png" ghost="true"/>
    436436            <object size="32 0 100% 100%-2" type="text" style="resourceText" name="resourceWood"/>
    437437        </object>
    438438
    439439        <!-- Stone -->
    440         <object size="180 0 270 100%" type="image" style="resourceCounter" tooltip="Stone" tooltip_style="sessionToolTipBold">
     440        <object size="40% 0 60% 100%" type="image" style="resourceCounter" tooltip="Stone (Gatherers)" tooltip_style="sessionToolTipBold">
    441441            <object size="0 -4 40 36" type="image" sprite="stretched:session/icons/resources/stone.png" ghost="true"/>
    442442            <object size="32 0 100% 100%-2" type="text" style="resourceText" name="resourceStone"/>
    443443        </object>
    444444
    445445        <!-- Metal -->
    446         <object size="270 0 360 100%"  type="image" style="resourceCounter" tooltip="Metal" tooltip_style="sessionToolTipBold">
     446        <object size="60% 0 80% 100%" type="image" style="resourceCounter" tooltip="Metal (Gatherers)" tooltip_style="sessionToolTipBold">
    447447            <object size="0 -4 40 36" type="image" sprite="stretched:session/icons/resources/metal.png" ghost="true"/>
    448448            <object size="32 0 100% 100%-2" type="text" style="resourceText" name="resourceMetal"/>
    449449        </object>
    450450
    451451        <!-- Population -->
    452         <object size="360 0 450 100%" type="image" style="resourceCounter" tooltip="Population (current / limit)" tooltip_style="sessionToolTipBold">
     452        <object size="80% 0 100% 100%" type="image" style="resourceCounter" tooltip="Population (current / limit)" tooltip_style="sessionToolTipBold">
    453453            <object size="0 -4 40 34" type="image" sprite="stretched:session/icons/resources/population.png" ghost="true"/>
    454454            <object size="32 0 100% 100%-2" type="text" style="resourceText" name="resourcePop"/>
    455455        </object>
  • binaries/data/mods/public/simulation/components/GuiInterface.js

     
    8585            "popLimit": cmpPlayer.GetPopulationLimit(),
    8686            "popMax": cmpPlayer.GetMaxPopulation(),
    8787            "resourceCounts": cmpPlayer.GetResourceCounts(),
     88            "resourceGatherers": cmpPlayer.GetResourceGatherers(),
    8889            "trainingBlocked": cmpPlayer.IsTrainingBlocked(),
    8990            "state": cmpPlayer.GetState(),
    9091            "team": cmpPlayer.GetTeam(),
  • 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.teamsLocked = false;
     
    144156    return this.resourceCount;
    145157};
    146158
     159Player.prototype.RemoveResourceGatherer = function(gatherer, force)
     160{
     161    var cmpResourceGatherer = Engine.QueryInterface(gatherer, IID_ResourceGatherer);
     162    if (!cmpResourceGatherer)
     163        return false;
     164
     165    var resource = cmpResourceGatherer.GetLastGathered();
     166    if (!resource)
     167        return false;
     168
     169    var cmpUnitAI = Engine.QueryInterface(gatherer, IID_UnitAI);
     170    var orders = cmpUnitAI.GetOrders();
     171
     172    var gatheredResource = undefined;
     173    for (var j = 0; j < orders.length; ++j)
     174    {
     175        var order = orders[j];
     176        if (order.type == "Gather" && !order.data.force)
     177            gatheredResource = order.data.type.generic;
     178    }
     179
     180    // Prune entities if they're no longer gathering or gathering a different resource.
     181    if (!gatheredResource || gatheredResource != resource.generic || force)
     182    {
     183        cmpResourceGatherer.SetLastGathered(undefined);
     184        var resourceType = (resource.generic == "treasure") ? resource.specific : resource.generic;
     185        delete this.resourceGatherers[resourceType][gatherer];
     186        this.resourceGatherers.count[resourceType]--;
     187        return true;
     188    }
     189
     190    return false;
     191};
     192
     193Player.prototype.GetResourceGatherers = function()
     194{
     195    return this.resourceGatherers;
     196};
     197
     198Player.prototype.AddResourceGatherer = function(gatherer, type)
     199{
     200    // We want to add units that are gathering a new resource.
     201    var cmpResourceGatherer = Engine.QueryInterface(gatherer, IID_ResourceGatherer);
     202    if (cmpResourceGatherer)
     203    {
     204        var lastGathered = cmpResourceGatherer.GetLastGathered();
     205        if (!lastGathered || lastGathered.generic != type.generic)
     206        {
     207            cmpResourceGatherer.SetLastGathered(type);
     208            var resourceType = (type.generic == "treasure") ? type.specific : type.generic;
     209            this.resourceGatherers[resourceType][gatherer] = true;
     210            this.resourceGatherers.count[resourceType]++;
     211            return true;
     212        }
     213    }
     214    return false;
     215};
     216
    147217/**
    148218 * Add resource of specified type to player
    149219 * @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/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 = {};
  • binaries/data/mods/public/simulation/components/UnitAI.js

     
    11431143                    {
    11441144                        var typename = "gather_" + this.order.data.type.specific;
    11451145                        this.SelectAnimation(typename, false, 1.0, typename);
     1146
     1147                        var cmpPlayer = this.GetOwnerPlayer(this.entity);
     1148                        if (cmpPlayer && cmpPlayer.AddResourceGatherer(this.entity, this.order.data.type))
     1149                            this.isGatherer = true;
    11461150                    }
    11471151                    return false;
    11481152                },
    11491153
    11501154                "leave": function() {
     1155                    var cmpPlayer = this.GetOwnerPlayer(this.entity);
     1156                    if (cmpPlayer && cmpPlayer.RemoveResourceGatherer(this.entity, false))
     1157                        this.isGatherer = false;
     1158
    11511159                    this.StopTimer();
    11521160                },
    11531161
     
    18481856    this.formationController = INVALID_ENTITY; // entity with IID_Formation that we belong to
    18491857    this.isGarrisoned = false;
    18501858    this.isIdle = false;
     1859    this.isGatherer = false;
    18511860    this.lastFormationName = "";
    18521861
    18531862    // For preventing increased action rate due to Stop orders or target death.
     
    18911900    return this.isIdle;
    18921901};
    18931902
     1903UnitAI.prototype.SetGathering = function(gathering)
     1904{
     1905    this.isGatherer = gathering;
     1906};
     1907
     1908UnitAI.prototype.IsGatherer = function()
     1909{
     1910    return this.isGatherer;
     1911};
     1912
    18941913UnitAI.prototype.IsGarrisoned = function()
    18951914{
    18961915    return this.isGarrisoned;
     
    19431962        this.SetStance(this.template.DefaultStance);
    19441963        this.Stop(false);
    19451964    }
     1965   
     1966    // Ensure the entity is no longer counted as gathering.
     1967    if (msg.from != -1 && this.isGatherer && !this.IsFormationController())
     1968    {
     1969        var cmpPlayerManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager);
     1970        var cmpPlayer = Engine.QueryInterface(cmpPlayerManager.GetPlayerByID(msg.from), IID_Player);
     1971        if (cmpPlayer)
     1972            cmpPlayer.RemoveResourceGatherer(this.entity, true);
     1973    }
    19461974};
    19471975
    19481976UnitAI.prototype.OnDestroy = function()
     
    21092137    Engine.PostMessage(this.entity, MT_UnitAIStateChanged, { "to": state });
    21102138};
    21112139
     2140UnitAI.prototype.GetOwnerPlayer = function(entity)
     2141{
     2142    var cmpOwnership = Engine.QueryInterface(entity, IID_Ownership);
     2143    if (!cmpOwnership)
     2144        return undefined;
     2145
     2146    var owner = cmpOwnership.GetOwner();
     2147    if (owner == -1)
     2148        return undefined;
     2149
     2150    var cmpPlayerManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager);
     2151    var cmpPlayer = Engine.QueryInterface(cmpPlayerManager.GetPlayerByID(owner), IID_Player);
     2152    if (!cmpPlayer)
     2153        return undefined;
     2154
     2155    return cmpPlayer;
     2156};
     2157
    21122158/**
    21132159 * Call when the current order has been completed (or failed).
    21142160 * Removes the current order from the queue, and processes the
     
    22292275    {
    22302276        this.orderQueue = [];
    22312277        this.PushOrder(type, data);
     2278
     2279        if (this.isGatherer)
     2280        {
     2281            var cmpPlayer = this.GetOwnerPlayer(this.entity);
     2282            if (cmpPlayer && cmpPlayer.RemoveResourceGatherer(this.entity, true))
     2283                this.isGatherer = false;
     2284        }
    22322285    }
    22332286};
    22342287