Ticket #2206: correct_backtowork.4.mod.diff

File correct_backtowork.4.mod.diff, 9.8 KB (added by mimo, 11 years ago)
  • binaries/data/mods/public/gui/session/input.js

     
    21082108    // Filter out all entities that can't go back to work.
    21092109    var workers = g_Selection.toList().filter(function(e) {
    21102110        var state = GetEntityState(e);
    2111         return (state && state.unitAI && state.unitAI.lastWorkOrder);
     2111        return (state && state.unitAI && state.unitAI.hasWorkOrders);
    21122112    });
    21132113   
    21142114    Engine.PostNetworkCommand({"type": "back-to-work", "workers": workers});
  • binaries/data/mods/public/gui/session/utility_functions.js

     
    294294        });
    295295    }
    296296   
    297     if (entState.unitAI && entState.unitAI.lastWorkOrder)
     297    if (entState.unitAI && entState.unitAI.hasWorkOrders)
    298298    {
    299299        commands.push({
    300300            "name": "back-to-work",
  • binaries/data/mods/public/simulation/components/Formation.js

     
    115115    for each (var ent in ents)
    116116    {
    117117        var cmpUnitAI = Engine.QueryInterface(ent, IID_UnitAI);
     118        cmpUnitAI.UpdateWorkOrders();
    118119        cmpUnitAI.SetFormationController(INVALID_ENTITY);
    119120    }
    120121
  • binaries/data/mods/public/simulation/components/GuiInterface.js

     
    348348        ret.unitAI = {
    349349            "state": cmpUnitAI.GetCurrentState(),
    350350            "orders": cmpUnitAI.GetOrders(),
    351             "lastWorkOrder": cmpUnitAI.GetLastWorkOrder(),
     351            "hasWorkOrders": cmpUnitAI.HasWorkOrders(),
    352352        };
    353353        // Add some information needed for ungarrisoning
    354354        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);
     81    var workOrders = cmpCurrentUnitAI.GetWorkOrders();
     82    cmpPromotedUnitAI.SetWorkOrders(workOrders);
    8383
    8484    Engine.BroadcastMessage(MT_EntityRenamed, { entity: this.entity, newentity: promotedUnitEntity });
    8585
  • binaries/data/mods/public/simulation/components/UnitAI.js

     
    25262526    this.lastFormationName = "";
    25272527    this.finishedOrder = false; // used to find if all formation members finished the order
    25282528   
    2529     // To go back to work later
    2530     this.lastWorkOrder = undefined;
     2529    // Queue of remembered works
     2530    this.workOrders = [];
    25312531
    25322532    // For preventing increased action rate due to Stop orders or target death.
    25332533    this.lastAttacked = undefined;
     
    28042804    Engine.PostMessage(this.entity, MT_UnitAIStateChanged, { "to": state });
    28052805};
    28062806
    2807 UnitAI.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 
    28182807/**
    28192808 * Call when the current order has been completed (or failed).
    28202809 * Removes the current order from the queue, and processes the
     
    28312820        error("FinishOrder called for entity " + this.entity + " (" + template + ") when order queue is empty\n" + stack);
    28322821    }
    28332822
    2834     // Remove the order from the queue, then forget it if it was a work to avoid trying to go back to it later.
     2823    // Remove the order from the queue
    28352824    var finishedOrder = this.orderQueue.shift();
    2836     if(finishedOrder.type == "Gather" || finishedOrder.type == "Trade" || finishedOrder.type == "Repair")
    2837         this.lastWorkOrder = undefined;
    2838    
    28392825    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;
    28452826
    28462827    if (this.orderQueue.length)
    28472828    {
     
    28922873{
    28932874    var order = { "type": type, "data": data };
    28942875    this.orderQueue.push(order);
    2895    
    2896     if(type == "Gather" || type == "Trade" || type == "Repair" || type == "ReturnResource")
    2897         this.lastWorkOrder = order;
    28982876
    28992877    // If we didn't already have an order, then process this new one
    29002878    if (this.orderQueue.length == 1)
     
    29522930
    29532931UnitAI.prototype.ReplaceOrder = function(type, data)
    29542932{
     2933    // Keep the previous orders if needed
     2934    if (data && data.force)
     2935    {
     2936        // Handle formations
     2937        if (this.IsFormationController())
     2938        {
     2939            var cmpFormation = Engine.QueryInterface(this.entity, IID_Formation);
     2940            cmpFormation.CallMemberFunction("UpdateWorkOrders", [type]);
     2941        }
     2942        else
     2943            this.UpdateWorkOrders(type);
     2944    }
     2945
    29552946    // Special cases of orders that shouldn't be replaced:
    29562947    // 1. Cheering - we're invulnerable, add order after we finish
    29572948    // 2. Packing/unpacking - we're immobile, add order after we finish (unless it's cancel)
     
    29982989    return orders;
    29992990};
    30002991
    3001 UnitAI.prototype.GetLastWorkOrder = function()
     2992UnitAI.prototype.UpdateWorkOrders = function(type)
    30022993{
    3003     return this.lastWorkOrder;
     2994    if (type == "Gather" || type == "Trade" || type == "Repair" || type == "ReturnResource")
     2995        this.workOrders = [];
     2996    else if (!this.workOrders.length)
     2997    {
     2998        // First if the unit is in a formation, get its workOrders from it
     2999        if (this.IsFormationMember())
     3000        {
     3001            var cmpUnitAI = Engine.QueryInterface(this.formationController, IID_UnitAI);
     3002            for (var i = 0; i < cmpUnitAI.orderQueue.length; ++i)
     3003            {
     3004                var ordertype = cmpUnitAI.orderQueue[i].type;
     3005                if (ordertype == "Gather" || ordertype == "Trade" || ordertype == "Repair" || ordertype == "ReturnResource")
     3006                {
     3007                    this.workOrders = cmpUnitAI.orderQueue.slice(i);
     3008                    break;
     3009                }
     3010            }
     3011        }
     3012
     3013        // If nothing found, take the unit orders
     3014        if (!this.workOrders.length)
     3015        {
     3016            for (var i = 0; i < this.orderQueue.length; ++i)
     3017            {
     3018                var ordertype = this.orderQueue[i].type;
     3019                if (ordertype == "Gather" || ordertype == "Trade" || ordertype == "Repair" || ordertype == "ReturnResource")
     3020                {
     3021                    this.workOrders = this.orderQueue.slice(i);
     3022                    break;
     3023                }
     3024            }
     3025        }
     3026    }
    30043027};
    30053028
    3006 UnitAI.prototype.SetLastWorkOrder = function(order)
     3029UnitAI.prototype.BackToWork = function()
     3030{   
     3031    if(this.workOrders.length > 0)
     3032    {
     3033        // Clear the order queue considering special orders not to avoid
     3034        if (this.order && this.order.type == "Cheering")
     3035        {
     3036            var cheeringOrder = this.orderQueue.shift();
     3037            this.orderQueue = [ cheeringOrder ];
     3038        }
     3039        else
     3040            this.orderQueue = [];
     3041       
     3042        this.AddOrders(this.workOrders);
     3043        this.workOrders = [];
     3044
     3045        // And if the unit is in a formation, remove it from the formation
     3046        if (this.IsFormationMember())
     3047            Engine.QueryInterface(this.formationController, IID_Formation).RemoveMembers([this.entity]);
     3048
     3049        return true;
     3050    }
     3051    else
     3052        return false;
     3053};
     3054
     3055UnitAI.prototype.HasWorkOrders = function()
    30073056{
    3008     this.lastWorkOrder = order;
     3057    return (this.workOrders.length > 0);
    30093058};
    30103059
     3060UnitAI.prototype.GetWorkOrders = function()
     3061{
     3062    return this.workOrders;
     3063};
     3064
     3065UnitAI.prototype.SetWorkOrders = function(orders)
     3066{
     3067    this.workOrders = orders;
     3068};
     3069
    30113070UnitAI.prototype.TimerHandler = function(data, lateness)
    30123071{
    30133072    // Reset the timer
  • binaries/data/mods/public/simulation/helpers/Commands.js

     
    170170            cmpUnitAI.ReturnResource(cmd.target, cmd.queued);
    171171        });
    172172        break;
     173       
     174    case "back-to-work":       
     175        var entities = FilterEntityList(cmd.workers, player, controlAllUnits);
     176        for each (var ent in entities)
     177        {
     178            var cmpUnitAI = Engine.QueryInterface(ent, IID_UnitAI);
     179            if(!cmpUnitAI || !cmpUnitAI.BackToWork())
     180                notifyBackToWorkFailure(player);
     181        }
     182        break;
    173183
    174184    case "train":
    175185        // Check entity limits
     
    390400                notifyUnloadFailure(player, garrisonHolder)
    391401        }
    392402        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;
    403403
    404404    case "formation":
    405405        GetFormationUnitAIs(entities, player, cmd.name).forEach(function(cmpUnitAI) {
     
    530530/**
    531531 * Sends a GUI notification about worker(s) that failed to go back to work.
    532532 */
    533 function notifyBackToWorkFailure(player, worker)
     533function notifyBackToWorkFailure(player)
    534534{
    535535    var cmpPlayer = QueryPlayerIDInterface(player, IID_Player);
    536536    var notification = {"player": cmpPlayer.GetPlayerID(), "message": "Some unit(s) can't go back to work" };