Ticket #1924: build limits v1.patch

File build limits v1.patch, 16.1 KB (added by Josh, 11 years ago)

Version 1 patch to show build limits for buildings in GUI.

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

     
    14641464
    14651465// Called by GUI when user clicks construction button
    14661466// @param buildTemplate Template name of the entity the user wants to build
    1467 function startBuildingPlacement(buildTemplate)
     1467function startBuildingPlacement(buildTemplate, playerState)
    14681468{
    1469     // TODO: we should clear any highlight selection rings here. If the mouse was over an entity before going onto the GUI
    1470     // to start building a structure, then the highlight selection rings are kept during the construction of the building.
    1471     // Gives the impression that somehow the hovered-over entity has something to do with the building you're constructing.
     1469    if(getEntityLimitAndCount(playerState, buildTemplate)[2]!=0)
     1470    {
     1471        // TODO: we should clear any highlight selection rings here. If the mouse was over an entity before going onto the GUI
     1472        // to start building a structure, then the highlight selection rings are kept during the construction of the building.
     1473        // Gives the impression that somehow the hovered-over entity has something to do with the building you're constructing.
    14721474   
    1473     placementSupport.Reset();
     1475        placementSupport.Reset();
    14741476   
    1475     // find out if we're building a wall, and change the entity appropriately if so
    1476     var templateData = GetTemplateData(buildTemplate);
    1477     if (templateData.wallSet)
    1478     {
    1479         placementSupport.mode = "wall";
    1480         placementSupport.wallSet = templateData.wallSet;
    1481         inputState = INPUT_BUILDING_PLACEMENT;
    1482     }
    1483     else
    1484     {
    1485         placementSupport.mode = "building";
    1486         placementSupport.template = buildTemplate;
    1487         inputState = INPUT_BUILDING_PLACEMENT;
    1488     }
     1477        // find out if we're building a wall, and change the entity appropriately if so
     1478        var templateData = GetTemplateData(buildTemplate);
     1479        if (templateData.wallSet)
     1480        {
     1481            placementSupport.mode = "wall";
     1482            placementSupport.wallSet = templateData.wallSet;
     1483            inputState = INPUT_BUILDING_PLACEMENT;
     1484        }
     1485        else
     1486        {
     1487            placementSupport.mode = "building";
     1488            placementSupport.template = buildTemplate;
     1489            inputState = INPUT_BUILDING_PLACEMENT;
     1490        }
     1491    }
    14891492}
    14901493
    14911494// Called by GUI when user changes preferred trading goods
     
    15481551function getEntityLimitAndCount(playerState, entType)
    15491552{
    15501553    var template = GetTemplateData(entType);
    1551     var trainingCategory = null;
     1554    var entCategory = null;
    15521555    if (template.trainingRestrictions)
    1553         trainingCategory = template.trainingRestrictions.category;
    1554     var trainEntLimit = undefined;
    1555     var trainEntCount = undefined;
    1556     var canBeTrainedCount = undefined;
    1557     if (trainingCategory && playerState.entityLimits[trainingCategory])
     1556        entCategory = template.trainingRestrictions.category;
     1557    else if (template.buildRestrictions)
     1558        entCategory = template.buildRestrictions.category;
     1559    var entLimit = undefined;
     1560    var entCount = undefined;
     1561    var canBeAddedCount = undefined;
     1562    if (entCategory && playerState.entityLimits[entCategory])
    15581563    {
    1559         trainEntLimit = playerState.entityLimits[trainingCategory];
    1560         trainEntCount = playerState.entityCounts[trainingCategory];
    1561         canBeTrainedCount = Math.max(trainEntLimit - trainEntCount, 0);
     1564        entLimit = playerState.entityLimits[entCategory];
     1565        entCount = playerState.entityCounts[entCategory];
     1566        canBeAddedCount = Math.max(entLimit - entCount, 0);
    15621567    }
    1563     return [trainEntLimit, trainEntCount, canBeTrainedCount];
     1568    return [entLimit, entCount, canBeAddedCount];
    15641569}
    15651570
    15661571// Add the unit shown at position to the training queue for all entities in the selection
  • binaries/data/mods/public/gui/session/unit_commands.js

     
    146146{
    147147    if (trainEntLimit == undefined)
    148148        return "";
    149     var text = "\n\nCurrent count: " + trainEntCount + ", limit: " + trainEntLimit + ".";
     149    var text = "\n\nCurrent Count: " + trainEntCount + ", Limit: " + trainEntLimit + ".";
    150150    if (trainEntCount >= trainEntLimit)
    151151        text = "[color=\"red\"]" + text + "[/color]";
    152152    return text;
     
    376376
    377377        switch (guiName)
    378378        {
    379             case SELECTION:
    380                 var name = getEntityNames(template);
    381                 var tooltip = name;
    382                 var count = g_Selection.groups.getCount(item);
    383                 getGUIObjectByName("unit"+guiName+"Count["+i+"]").caption = (count > 1 ? count : "");
    384                 break;
     379            case SELECTION:
     380                var name = getEntityNames(template);
     381                var tooltip = name;
     382                var count = g_Selection.groups.getCount(item);
     383                getGUIObjectByName("unit"+guiName+"Count["+i+"]").caption = (count > 1 ? count : "");
     384                break;
    385385
    386             case QUEUE:
    387                 var tooltip = getEntityNames(template);
    388                 if (item.neededSlots)
    389                     tooltip += "\n[color=\"red\"]Insufficient population capacity:\n[/color]"+getCostComponentDisplayName("population")+" "+item.neededSlots;
     386            case QUEUE:
     387                var tooltip = getEntityNames(template);
     388                if (item.neededSlots)
     389                    tooltip += "\n[color=\"red\"]Insufficient population capacity:\n[/color]"+getCostComponentDisplayName("population")+" "+item.neededSlots;
    390390
    391                 var progress = Math.round(item.progress*100) + "%";
    392                 getGUIObjectByName("unit"+guiName+"Count["+i+"]").caption = (item.count > 1 ? item.count : "");
     391                var progress = Math.round(item.progress*100) + "%";
     392                getGUIObjectByName("unit"+guiName+"Count["+i+"]").caption = (item.count > 1 ? item.count : "");
    393393
    394                 if (i == 0)
    395                 {
    396                     getGUIObjectByName("queueProgress").caption = (item.progress ? progress : "");
    397                     var size = getGUIObjectByName("unit"+guiName+"ProgressSlider["+i+"]").size;
     394                if (i == 0)
     395                {
     396                    getGUIObjectByName("queueProgress").caption = (item.progress ? progress : "");
     397                    var size = getGUIObjectByName("unit"+guiName+"ProgressSlider["+i+"]").size;
    398398
    399                     // Buttons are assumed to be square, so left/right offsets can be used for top/bottom.
    400                     size.top = size.left + Math.round(item.progress * (size.right - size.left));
    401                     getGUIObjectByName("unit"+guiName+"ProgressSlider["+i+"]").size = size;
    402                 }
    403                 break;
     399                    // Buttons are assumed to be square, so left/right offsets can be used for top/bottom.
     400                    size.top = size.left + Math.round(item.progress * (size.right - size.left));
     401                    getGUIObjectByName("unit"+guiName+"ProgressSlider["+i+"]").size = size;
     402                }
     403                break;
    404404
    405             case GARRISON:
    406                 var name = getEntityNames(template);
    407                 var tooltip = "Unload " + name + "\nSingle-click to unload 1. Shift-click to unload all of this type.";
    408                 var count = garrisonGroups.getCount(item);
    409                 getGUIObjectByName("unit"+guiName+"Count["+i+"]").caption = (count > 1 ? count : "");
    410                 break;
     405            case GARRISON:
     406                var name = getEntityNames(template);
     407                var tooltip = "Unload " + name + "\nSingle-click to unload 1. Shift-click to unload all of this type.";
     408                var count = garrisonGroups.getCount(item);
     409                getGUIObjectByName("unit"+guiName+"Count["+i+"]").caption = (count > 1 ? count : "");
     410                break;
    411411
    412             case GATE:
    413                 var tooltip = item.tooltip;
    414                 if (item.template)
    415                 {
    416                     var template = GetTemplateData(item.template);
    417                     tooltip += "\n" + getEntityCostTooltip(template);
     412            case GATE:
     413                var tooltip = item.tooltip;
     414                if (item.template)
     415                {
     416                    var template = GetTemplateData(item.template);
     417                    tooltip += "\n" + getEntityCostTooltip(template);
    418418
    419                     var affordableMask = getGUIObjectByName("unitGateUnaffordable["+i+"]");
    420                     affordableMask.hidden = true;
     419                    var affordableMask = getGUIObjectByName("unitGateUnaffordable["+i+"]");
     420                    affordableMask.hidden = true;
    421421
    422                     var neededResources = Engine.GuiInterfaceCall("GetNeededResources", template.cost);
    423                     if (neededResources)
    424                     {
    425                         affordableMask.hidden = false;
    426                         tooltip += getNeededResourcesTooltip(neededResources);
    427                     }
    428                 }
    429                 break;
     422                    var neededResources = Engine.GuiInterfaceCall("GetNeededResources", template.cost);
     423                    if (neededResources)
     424                    {
     425                        affordableMask.hidden = false;
     426                        tooltip += getNeededResourcesTooltip(neededResources);
     427                    }
     428                }
     429                break;
    430430
    431             case PACK:
    432                 var tooltip = item.tooltip;
    433                 break;
     431            case PACK:
     432                var tooltip = item.tooltip;
     433                break;
    434434
    435             case STANCE:
    436             case FORMATION:
    437                 var tooltip = toTitleCase(item);
    438                 break;
     435            case STANCE:
     436            case FORMATION:
     437                var tooltip = toTitleCase(item);
     438                break;
    439439
    440             case TRAINING:
    441                 var tooltip = getEntityNamesFormatted(template);
     440            case TRAINING:
     441                var tooltip = getEntityNamesFormatted(template);
    442442
    443                 if (template.tooltip)
    444                     tooltip += "\n[font=\"serif-13\"]" + template.tooltip + "[/font]";
     443                if (template.tooltip)
     444                    tooltip += "\n[font=\"serif-13\"]" + template.tooltip + "[/font]";
    445445
    446                 var [buildingsCountToTrainFullBatch, fullBatchSize, remainderBatch] =
    447                     getTrainingBatchStatus(playerState, unitEntState.id, entType, selection);
    448                 if (Engine.HotkeyIsPressed("session.batchtrain"))
    449                 {
    450                     trainNum = buildingsCountToTrainFullBatch * fullBatchSize + remainderBatch;
    451                 }
     446                var [buildingsCountToTrainFullBatch, fullBatchSize, remainderBatch] =
     447                getTrainingBatchStatus(playerState, unitEntState.id, entType, selection);
     448                if (Engine.HotkeyIsPressed("session.batchtrain"))
     449                {
     450                    trainNum = buildingsCountToTrainFullBatch * fullBatchSize + remainderBatch;
     451                }
    452452
    453                 tooltip += "\n" + getEntityCostTooltip(template, trainNum, unitEntState.id);
     453                tooltip += "\n" + getEntityCostTooltip(template, trainNum, unitEntState.id);
    454454
    455                 if (template.health)
    456                     tooltip += "\n[font=\"serif-bold-13\"]Health:[/font] " + template.health;
    457                 if (template.armour)
    458                     tooltip += "\n[font=\"serif-bold-13\"]Armour:[/font] " + armorTypesToText(template.armour);
    459                 if (template.attack)
    460                     tooltip += "\n" + getEntityAttack(template);
    461                 if (template.speed)
    462                     tooltip += "\n" + getEntitySpeed(template);
     455                if (template.health)
     456                    tooltip += "\n[font=\"serif-bold-13\"]Health:[/font] " + template.health;
     457                if (template.armour)
     458                    tooltip += "\n[font=\"serif-bold-13\"]Armour:[/font] " + armorTypesToText(template.armour);
     459                if (template.attack)
     460                    tooltip += "\n" + getEntityAttack(template);
     461                if (template.speed)
     462                    tooltip += "\n" + getEntitySpeed(template);
    463463
    464                 var [trainEntLimit, trainEntCount, canBeTrainedCount] =
    465                     getEntityLimitAndCount(playerState, entType)
    466                 tooltip += formatLimitString(trainEntLimit, trainEntCount);
     464                var [trainEntLimit, trainEntCount, canBeTrainedCount] =
     465                getEntityLimitAndCount(playerState, entType)
     466                tooltip += formatLimitString(trainEntLimit, trainEntCount);
    467467
    468                 tooltip += formatBatchTrainingString(buildingsCountToTrainFullBatch, fullBatchSize, remainderBatch);
    469                 var key = g_ConfigDB.system["hotkey.session.queueunit." + (i+1)];
    470                 if (key !== undefined)
    471                 {
    472                     tooltip += "\n[font=\"serif-bold-13\"]HotKey (" + key  + ").[/font]";
    473                 }
    474                 break;
     468                tooltip += formatBatchTrainingString(buildingsCountToTrainFullBatch, fullBatchSize, remainderBatch);
     469                var key = g_ConfigDB.system["hotkey.session.queueunit." + (i+1)];
     470                if (key !== undefined)
     471                {
     472                    tooltip += "\n[font=\"serif-bold-13\"]HotKey (" + key  + ").[/font]";
     473                }
     474                break;
    475475               
    476             case RESEARCH:
    477                 var tooltip = getEntityNamesFormatted(template);
    478                 if (template.tooltip)
    479                     tooltip += "\n[font=\"serif-13\"]" + template.tooltip + "[/font]";
     476            case RESEARCH:
     477                var tooltip = getEntityNamesFormatted(template);
     478                if (template.tooltip)
     479                    tooltip += "\n[font=\"serif-13\"]" + template.tooltip + "[/font]";
    480480
    481                 tooltip += "\n" + getEntityCostTooltip(template);
     481                tooltip += "\n" + getEntityCostTooltip(template);
    482482
    483                 if (item.pair)
    484                 {
    485                     var tooltip1 = getEntityNamesFormatted(template1);
    486                     if (template1.tooltip)
    487                         tooltip1 += "\n[font=\"serif-13\"]" + template1.tooltip + "[/font]";
     483                if (item.pair)
     484                {
     485                    var tooltip1 = getEntityNamesFormatted(template1);
     486                    if (template1.tooltip)
     487                        tooltip1 += "\n[font=\"serif-13\"]" + template1.tooltip + "[/font]";
    488488
    489                     tooltip1 += "\n" + getEntityCostTooltip(template1);
    490                 }
    491                 break;
     489                    tooltip1 += "\n" + getEntityCostTooltip(template1);
     490                }
     491                break;
    492492
    493             case CONSTRUCTION:
    494                 var tooltip = getEntityNamesFormatted(template);
    495                 if (template.tooltip)
    496                     tooltip += "\n[font=\"serif-13\"]" + template.tooltip + "[/font]";
     493            case CONSTRUCTION:
     494                var tooltip = getEntityNamesFormatted(template);
     495                if (template.tooltip)
     496                    tooltip += "\n[font=\"serif-13\"]" + template.tooltip + "[/font]";
    497497
    498                 tooltip += "\n" + getEntityCostTooltip(template); // see utility_functions.js
    499                 tooltip += getPopulationBonusTooltip(template); // see utility_functions.js
     498                tooltip += "\n" + getEntityCostTooltip(template); // see utility_functions.js
     499                tooltip += getPopulationBonusTooltip(template); // see utility_functions.js
     500                if (template.health)
     501                    tooltip += "\n[font=\"serif-bold-13\"]Health:[/font] " + template.health;
     502               
     503                var [entLimit, entCount, canBeAddeddCount] =
     504                getEntityLimitAndCount(playerState, entType) // see input.js
     505                tooltip += formatLimitString(entLimit, entCount);
    500506
    501                 if (template.health)
    502                     tooltip += "\n[font=\"serif-bold-13\"]Health:[/font] " + template.health;
    503 
    504507                break;
    505508
    506509            case COMMAND:
     
    777780                var totalCosts = {};
    778781                var trainNum = 1;
    779782                var button_disableable = true;
     783                if (getEntityLimitAndCount(playerState, entType)[2]==0)
     784                {
     785                    grayscale = "grayscale:";
     786                    affordableMask.hidden = false;
     787                    affordableMask.sprite = "colour: 0 0 0 100";
     788                }
     789                icon.sprite = "stretched:" + grayscale + "session/portraits/" + template.icon;
    780790                if (guiName == TRAINING)
    781791                {
    782                     var trainingCategory = null;
    783                     if (template.trainingRestrictions)
    784                         trainingCategory = template.trainingRestrictions.category;
    785                     if (trainingCategory && playerState.entityLimits[trainingCategory] &&
    786                         playerState.entityCounts[trainingCategory] >= playerState.entityLimits[trainingCategory])
    787                     {
    788                         grayscale = "grayscale:";
    789                         affordableMask.hidden = false;
    790                         affordableMask.sprite = "colour: 0 0 0 100";
    791                     }
    792                     icon.sprite = "stretched:" + grayscale + "session/portraits/" + template.icon;
    793 
    794792                    if (Engine.HotkeyIsPressed("session.batchtrain"))
    795793                    {
    796794                        var [buildingsCountToTrainFullBatch, fullBatchSize, remainderBatch, batchTrainingCount] =
     
    10821080
    10831081        // The first selected entity's type has priority.
    10841082        if (entState.buildEntities)
    1085             setupUnitPanel(CONSTRUCTION, usedPanels, entState, playerState, buildableEnts, startBuildingPlacement);
     1083            setupUnitPanel(CONSTRUCTION, usedPanels, entState, playerState, buildableEnts,
     1084            function (trainEntType) { startBuildingPlacement(trainEntType, playerState); } );
    10861085        else if (entState.production && entState.production.entities)
    10871086            setupUnitPanel(TRAINING, usedPanels, entState, playerState, trainableEnts,
    10881087                function (trainEntType) { addTrainingToQueue(selection, trainEntType, playerState); } );