Ticket #1432: one_hero_2012_05_24_v2.diff

File one_hero_2012_05_24_v2.diff, 11.9 KB (added by fcxSanya, 12 years ago)

the same old functionality, but updated to r11909

  • binaries/data/mods/public/gui/session/unit_commands.js

     
    145145 * @param items Panel-specific data to construct the icons with.
    146146 * @param callback Callback function to argument to execute when an item's icon gets clicked. Takes a single 'item' argument.
    147147 */
    148 function setupUnitPanel(guiName, usedPanels, unitEntState, items, callback)
     148function setupUnitPanel(guiName, usedPanels, unitEntState, playerState, items, callback)
    149149{
    150150    usedPanels[guiName] = 1;
    151151
     
    343343                    tooltip += "\n" + getEntitySpeed(template);
    344344
    345345                tooltip += "\n\n[font=\"serif-bold-13\"]Shift-click[/font][font=\"serif-13\"] to train " + trainNum + ".[/font]";
    346 
    347346                break;
    348347               
    349348            case RESEARCH:
     
    492491            icon.sprite = "stretched:session/icons/" + item.icon;
    493492
    494493        }
     494        else if (guiName == "Training")
     495        {
     496            if (template.icon)
     497            {
     498                icon.sprite = "stretched:session/portraits/" + template.icon;
     499            }
     500        }
    495501        else if (template.icon)
    496502        {
    497503            var grayscale = "";
     
    636642}
    637643
    638644// Sets up "unit barter panel" - special case for setupUnitPanel
    639 function setupUnitBarterPanel(unitEntState)
     645function setupUnitBarterPanel(unitEntState, playerState)
    640646{
    641647    // Amount of player's resource to exchange
    642648    var amountToSell = BARTER_RESOURCE_AMOUNT_TO_SELL;
     
    714720    var player = Engine.GetPlayerID();
    715721    if (entState.player == player || g_DevSettings.controlAll)
    716722    {
     723        // Get player state to check some constraints
     724        // e.g. presence of a hero or build limits
     725        var simState = Engine.GuiInterfaceCall("GetSimulationState");
     726        var playerState = simState.players[player];
     727
    717728        if (selection.length > 1)
    718             setupUnitPanel(SELECTION, usedPanels, entState, g_Selection.groups.getTemplateNames(),
     729            setupUnitPanel(SELECTION, usedPanels, entState, playerState, g_Selection.groups.getTemplateNames(),
    719730                function (entType) { changePrimarySelectionGroup(entType); } );
    720731
    721732        var commands = getEntityCommandsList(entState);
    722733        if (commands.length)
    723             setupUnitPanel(COMMAND, usedPanels, entState, commands,
     734            setupUnitPanel(COMMAND, usedPanels, entState, playerState, commands,
    724735                function (item) { performCommand(entState.id, item.name); } );
    725736
    726737        if (entState.garrisonHolder)
    727738        {
    728739            var groups = new EntityGroups();
    729740            groups.add(entState.garrisonHolder.entities);
    730             setupUnitPanel(GARRISON, usedPanels, entState, groups.getTemplateNames(),
     741            setupUnitPanel(GARRISON, usedPanels, entState, playerState, groups.getTemplateNames(),
    731742                function (item) { unload(entState.id, groups.getEntsByName(item)); } );
    732743        }
    733744
    734745        var formations = Engine.GuiInterfaceCall("GetAvailableFormations");
    735746        if (hasClass(entState, "Unit") && !hasClass(entState, "Animal") && !entState.garrisonHolder && formations.length)
    736747        {
    737             setupUnitPanel(FORMATION, usedPanels, entState, formations,
     748            setupUnitPanel(FORMATION, usedPanels, entState, playerState, formations,
    738749                function (item) { performFormation(entState.id, item); } );
    739750        }
    740751
     
    743754        var stances = ["violent", "aggressive", "passive", "defensive", "standground"];
    744755        if (hasClass(entState, "Unit") && !hasClass(entState, "Animal") && !entState.garrisonHolder && stances.length)
    745756        {
    746             setupUnitPanel(STANCE, usedPanels, entState, stances,
     757            setupUnitPanel(STANCE, usedPanels, entState, playerState, stances,
    747758                function (item) { performStance(entState.id, item); } );
    748759        }
    749760
     
    751762        if (entState.barterMarket)
    752763        {
    753764            usedPanels["Barter"] = 1;
    754             setupUnitBarterPanel(entState);
     765            setupUnitBarterPanel(entState, playerState);
    755766        }
    756767
    757768        var buildableEnts = [];
     
    771782        removeDupes(trainableEnts);
    772783
    773784        if (buildableEnts.length && ((trainableEnts.length && hasClass(entState, "Unit")) || !trainableEnts.length))
    774             setupUnitPanel(CONSTRUCTION, usedPanels, entState, buildableEnts, startBuildingPlacement);
     785            setupUnitPanel(CONSTRUCTION, usedPanels, entState, playerState, buildableEnts, startBuildingPlacement);
    775786        else if (trainableEnts.length)
    776             setupUnitPanel(TRAINING, usedPanels, entState, trainableEnts,
     787            setupUnitPanel(TRAINING, usedPanels, entState, playerState, trainableEnts,
    777788                function (trainEntType) { addTrainingToQueue(selection, trainEntType); } );
    778789       
    779790        if (entState.production && entState.production.technologies.length && selection.length == 1)
    780791        {
    781             setupUnitPanel(RESEARCH, usedPanels, entState, entState.production.technologies,
     792            setupUnitPanel(RESEARCH, usedPanels, entState, playerState, entState.production.technologies,
    782793                function (researchType) { addResearchToQueue(entState.id, researchType); } );
    783794        }
    784795
    785796        if (entState.production && entState.production.queue.length)
    786             setupUnitPanel(QUEUE, usedPanels, entState, entState.production.queue,
     797            setupUnitPanel(QUEUE, usedPanels, entState, playerState, entState.production.queue,
    787798                function (item) { removeFromProductionQueue(entState.id, item.id); } );
    788799
    789800        if (entState.trader)
  • binaries/data/mods/public/simulation/helpers/Templates.js

     
     1/**
     2 * Check whether template.Identity.Classes contains specified class
     3 */
     4function TemplateHasIdentityClass(template, className)
     5{
     6    var hasClass = template.Identity && template.Identity.Classes && "_string" in template.Identity.Classes && template.Identity.Classes._string.indexOf(className) != -1;
     7    return hasClass;
     8}
     9
     10Engine.RegisterGlobal("TemplateHasIdentityClass", TemplateHasIdentityClass);
     11
  • binaries/data/mods/public/simulation/helpers/Commands.js

     
    142142
    143143    case "train":
    144144        var entities = FilterEntityList(cmd.entities, player, controlAllUnits);
     145       
     146        // Check hero restrictions
     147        var cmpTempMan = Engine.QueryInterface(SYSTEM_ENTITY, IID_TemplateManager);
     148        var template = cmpTempMan.GetTemplate(cmd.template);
     149        var isHero = TemplateHasIdentityClass(template, "Hero");
     150        if (isHero && +cmd.count > 1)
     151        {
     152            if (g_DebugCommands)
     153            {
     154                warn("Invalid command: batch training isn't allowed for heroes: "+uneval(cmd));
     155            }
     156            return;
     157        }
     158   
    145159        // Verify that the building(s) can be controlled by the player
    146160        if (entities.length > 0)
    147161        {
    148162            for each (var ent in entities)
    149163            {
     164                if (isHero)
     165                {
     166                    var cmpPlayer = QueryOwnerInterface(ent, IID_Player);
     167                    if (cmpPlayer.HasHero())
     168                    {
     169                        if (g_DebugCommands)
     170                        {
     171                            warn("Invalid command: can't train hero because player already has one: "+uneval(cmd));
     172                        }
     173                        continue;
     174                    }
     175                }
     176
    150177                var cmpTechMan = QueryOwnerInterface(ent, IID_TechnologyManager);
    151178                // TODO: Enable this check once the AI gets technology support
    152179                if (cmpTechMan.CanProduce(cmd.template) || true)
  • binaries/data/mods/public/simulation/components/GuiInterface.js

     
    8282            "state": cmpPlayer.GetState(),
    8383            "team": cmpPlayer.GetTeam(),
    8484            "phase": phase,
     85            "hasHero": cmpPlayer.HasHero(),
    8586            "isAlly": allies,
    8687            "isEnemy": enemies,
    8788            "buildLimits": cmpPlayerBuildLimits.GetLimits(),
  • binaries/data/mods/public/simulation/components/ProductionQueue.js

     
    199199            if (!cmpPlayer.TrySubtractResources(totalCosts))
    200200                return;
    201201
     202            // If we train a hero, state this in player compoment
     203            var isHero = TemplateHasIdentityClass(template, "Hero");
     204            if (isHero)
     205                cmpPlayer.IncreaseHeroesCount();
     206
    202207            this.queue.push({
    203208                "id": this.nextID++,
    204209                "player": cmpPlayer.GetPlayerID(),
     
    292297       
    293298        var cmpPlayer = QueryPlayerIDInterface(item.player, IID_Player);
    294299
     300        // If we trained a hero, state this in player compoment
     301        if (item.unitTemplate)
     302        {
     303            var cmpTempMan = Engine.QueryInterface(SYSTEM_ENTITY, IID_TemplateManager);
     304            var template = cmpTempMan.GetTemplate(item.unitTemplate);
     305            var isHero = TemplateHasIdentityClass(template, "Hero");
     306            if (isHero)
     307                cmpPlayer.DecreaseHeroesCount();
     308        }
     309
    295310        // Refund the resource cost for this batch
    296311        var totalCosts = {};
    297312        for each (var r in ["food", "wood", "stone", "metal"])
     
    401416        //  so only create them once and use as needed
    402417        for (var i = 0; i < count; ++i)
    403418        {
    404             this.entityCache.push(Engine.AddEntity(templateName));
     419            var ent = Engine.AddEntity(templateName);
     420            this.entityCache.push(ent);
     421            // If we create a hero entity - decrease heroes count, since it will be
     422            // increased by Player.OnGlobalOwnershipChanged function,
     423            // i.e. we replace 'trained' hero to a 'alive' one
     424            var cmpIdentity = Engine.QueryInterface(ent, IID_Identity);
     425            var cmpPlayer = QueryOwnerInterface(this.entity, IID_Player);
     426            var isHero = cmpIdentity.HasClass('Hero');
     427            if (isHero)
     428                cmpPlayer.DecreaseHeroesCount();
    405429        }
    406430    }
    407431
  • binaries/data/mods/public/simulation/components/Player.js

     
    2424    this.state = "active"; // game state - one of "active", "defeated", "won"
    2525    this.diplomacy = [];    // array of diplomatic stances for this player with respect to other players (including gaia and self)
    2626    this.conquestCriticalEntitiesCount = 0; // number of owned units with ConquestCritical class
     27    this.heroesCount = 0; // number of owned units with Hero class
    2728    this.phase = "village";
    2829    this.formations = [];
    2930    this.startCam = undefined;
     
    328329Player.prototype.OnGlobalOwnershipChanged = function(msg)
    329330{
    330331    var isConquestCritical = false;
    331 
     332    var isHero = false;
    332333    // Load class list only if we're going to need it
    333334    if (msg.from == this.playerID || msg.to == this.playerID)
    334335    {
    335336        var cmpIdentity = Engine.QueryInterface(msg.entity, IID_Identity);
    336337        if (cmpIdentity)
    337338        {
    338             var classes = cmpIdentity.GetClassesList();
    339             isConquestCritical = classes.indexOf("ConquestCritical") != -1;
     339            isConquestCritical = cmpIdentity.HasClass("ConquestCritical");
     340            isHero = cmpIdentity.HasClass("Hero");
    340341        }
    341342    }
    342    
    343343    if (msg.from == this.playerID)
    344344    {
    345345        if (isConquestCritical)
    346             this.conquestCriticalEntitiesCount--;   
    347 
     346            this.conquestCriticalEntitiesCount--;
     347        if (isHero)
     348            this.heroesCount--;
    348349        var cost = Engine.QueryInterface(msg.entity, IID_Cost);
    349350        if (cost)
    350351        {
     
    352353            this.popBonuses -= cost.GetPopBonus();
    353354        }
    354355    }
    355    
    356356    if (msg.to == this.playerID)
    357357    {
    358358        if (isConquestCritical)
    359359            this.conquestCriticalEntitiesCount++;
    360            
     360        if (isHero)
     361            this.heroesCount++;
    361362        var cost = Engine.QueryInterface(msg.entity, IID_Cost);
    362363        if (cost)
    363364        {
     
    382383    }
    383384};
    384385
     386Player.prototype.IncreaseHeroesCount = function()
     387{
     388    this.heroesCount++;
     389}
     390
     391Player.prototype.DecreaseHeroesCount = function()
     392{
     393    this.heroesCount--;
     394}
     395
     396Player.prototype.HasHero = function()
     397{
     398    return this.heroesCount != 0;
     399}
     400
    385401Engine.RegisterComponentType(IID_Player, "Player", Player);