Ticket #799: patch_backtowork.5.diff

File patch_backtowork.5.diff, 6.8 KB (added by Itms, 11 years ago)
  • binaries/data/mods/public/gui/session/input.js

     
    18691869                    Engine.CameraMoveTo(focusTarget.x, focusTarget.z);
    18701870               
    18711871                break;
     1872            case "back-to-work":
     1873                backToWork();
     1874                break;
    18721875            default:
    18731876                break;
    18741877            }
     
    21312134    Engine.PostNetworkCommand({"type": "unload-all", "garrisonHolders": garrisonHolders});
    21322135}
    21332136
     2137function backToWork()
     2138{
     2139    // Filter out all entities that can't go back to work.
     2140    var workers = g_Selection.toList().filter(function(e) {
     2141        var state = GetEntityState(e);
     2142        if (state && state.unitAI && state.unitAI.lastWorkOrder)
     2143            return true;
     2144        return false;
     2145    });
     2146   
     2147    Engine.PostNetworkCommand({"type": "back-to-work", "workers": workers});
     2148   
     2149}
     2150
    21342151function clearSelection()
    21352152{
    21362153    if(inputState==INPUT_BUILDING_PLACEMENT || inputState==INPUT_BUILDING_WALL_PATHING)
  • binaries/data/mods/public/gui/session/utility_functions.js

     
    293293            "icon": "focus-rally.png"
    294294        });
    295295    }
     296   
     297    if (entState.unitAI && entState.unitAI.lastWorkOrder)
     298    {
     299        commands.push({
     300            "name": "back-to-work",
     301            "tooltip": "Back to Work",
     302            "icon": "production.png"
     303        });
     304    }
    296305
    297306    return commands;
    298307}
  • binaries/data/mods/public/simulation/components/GuiInterface.js

     
    348348        ret.unitAI = {
    349349            "state": cmpUnitAI.GetCurrentState(),
    350350            "orders": cmpUnitAI.GetOrders(),
     351            "lastWorkOrder": cmpUnitAI.GetLastWorkOrder(),
    351352        };
    352353        // Add some information needed for ungarrisoning
    353354        if (cmpUnitAI.isGarrisoned && ret.player !== undefined)
  • binaries/data/mods/public/simulation/components/Promotion.js

     
    7878    cmpPromotedUnitAI.Cheer();
    7979    var orders = cmpCurrentUnitAI.GetOrders();
    8080    cmpPromotedUnitAI.AddOrders(orders);
     81    var lastWorkOrder = cmpCurrentUnitAI.GetLastWorkOrder();
     82    cmpPromotedUnitAI.SetLastWorkOrder(lastWorkOrder);
    8183
    8284    Engine.BroadcastMessage(MT_EntityRenamed, { entity: this.entity, newentity: promotedUnitEntity });
    8385
  • binaries/data/mods/public/simulation/components/UnitAI.js

     
    25252525    this.isIdle = false;
    25262526    this.lastFormationName = "";
    25272527    this.finishedOrder = false; // used to find if all formation members finished the order
     2528   
     2529    // To go back to work later
     2530    this.lastWorkOrder = undefined;
    25282531
    25292532    // For preventing increased action rate due to Stop orders or target death.
    25302533    this.lastAttacked = undefined;
     
    28012804    Engine.PostMessage(this.entity, MT_UnitAIStateChanged, { "to": state });
    28022805};
    28032806
     2807UnitAI.prototype.BackToWork = function()
     2808{
     2809    if(this.lastWorkOrder)
     2810    {
     2811        this.ReplaceOrder(this.lastWorkOrder.type, this.lastWorkOrder.data);
     2812        return true;
     2813    }
     2814    else
     2815        return false;
     2816};
     2817
    28042818/**
    28052819 * Call when the current order has been completed (or failed).
    28062820 * Removes the current order from the queue, and processes the
     
    28172831        error("FinishOrder called for entity " + this.entity + " (" + template + ") when order queue is empty\n" + stack);
    28182832    }
    28192833
    2820     this.orderQueue.shift();
     2834    // Remove the order from the queue, then forget it if it was a work to avoid trying to go back to it later.
     2835    var finishedOrder = this.orderQueue.shift();
     2836    if(finishedOrder.type == "Gather" || finishedOrder.type == "Trade" || finishedOrder.type == "Repair")
     2837        this.lastWorkOrder = undefined;
     2838   
    28212839    this.order = this.orderQueue[0];
     2840    // If the worker was returning a resource, we shall
     2841    //  -> (if the worker is gathering) keep remembering the "Gather" order which is this.order
     2842    //  -> (if the worker was directly ordered to return a resource) forget the "ReturnResource" order which is finished
     2843    if(finishedOrder.type == "ReturnResource" && (!this.order || this.order.type != "Gather"))
     2844        this.lastWorkOrder = undefined;
    28222845
    28232846    if (this.orderQueue.length)
    28242847    {
     
    28692892{
    28702893    var order = { "type": type, "data": data };
    28712894    this.orderQueue.push(order);
     2895   
     2896    if(type == "Gather" || type == "Trade" || type == "Repair" || type == "ReturnResource")
     2897        this.lastWorkOrder = order;
    28722898
    28732899    // If we didn't already have an order, then process this new one
    28742900    if (this.orderQueue.length == 1)
     
    29722998    return orders;
    29732999};
    29743000
     3001UnitAI.prototype.GetLastWorkOrder = function()
     3002{
     3003    return this.lastWorkOrder;
     3004};
     3005
     3006UnitAI.prototype.SetLastWorkOrder = function(order)
     3007{
     3008    this.lastWorkOrder = order;
     3009};
     3010
    29753011UnitAI.prototype.TimerHandler = function(data, lateness)
    29763012{
    29773013    // Reset the timer
  • binaries/data/mods/public/simulation/helpers/Commands.js

     
    390390                notifyUnloadFailure(player, garrisonHolder)
    391391        }
    392392        break;
     393       
     394    case "back-to-work":
     395        var entities = FilterEntityList(cmd.workers, player, controlAllUnits);
     396        for each (var worker in entities)
     397        {
     398            var cmpUnitAI = Engine.QueryInterface(worker, IID_UnitAI);
     399            if (!cmpUnitAI || !cmpUnitAI.BackToWork())
     400                notifyBackToWorkFailure(player, worker)
     401        }
     402        break;
    393403
    394404    case "formation":
    395405        GetFormationUnitAIs(entities, player, cmd.name).forEach(function(cmpUnitAI) {
     
    518528}
    519529
    520530/**
     531 * Sends a GUI notification about worker(s) that failed to go back to work.
     532 */
     533function notifyBackToWorkFailure(player, worker)
     534{
     535    var cmpPlayer = QueryPlayerIDInterface(player, IID_Player);
     536    var notification = {"player": cmpPlayer.GetPlayerID(), "message": "Some unit(s) can't go back to work" };
     537    var cmpGUIInterface = Engine.QueryInterface(SYSTEM_ENTITY, IID_GuiInterface);
     538    cmpGUIInterface.PushNotification(notification);
     539}
     540
     541/**
    521542 * Get some information about the formations used by entities.
    522543 * The entities must have a UnitAI component.
    523544 */