Ticket #799: patch_backtowork.3.diff

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

     
    18481848                    Engine.CameraMoveTo(focusTarget.x, focusTarget.z);
    18491849               
    18501850                break;
     1851            case "back-to-work":
     1852                backToWork();
     1853                break;
     1854            case "unload-all-back-to-work":
     1855                unloadAllBackToWork();
     1856                break;
    18511857            default:
    18521858                break;
    18531859            }
     
    21102116    Engine.PostNetworkCommand({"type": "unload-all", "garrisonHolders": garrisonHolders});
    21112117}
    21122118
     2119function backToWork()
     2120{
     2121    // Filter out all entities that can't go back to work.
     2122    var workers = g_Selection.toList().filter(function(e) {
     2123        var state = GetEntityState(e);
     2124        if (state && (hasClass(state, "Worker") || hasClass(state, "Trader")))
     2125            return true;
     2126        return false;
     2127    });
     2128   
     2129    Engine.PostNetworkCommand({"type": "back-to-work", "workers": workers});
     2130   
     2131}
     2132
     2133function unloadAllBackToWork()
     2134{
     2135    // Filter out all entities that aren't garrisonable.
     2136    var garrisonHolders = g_Selection.toList().filter(function(e) {
     2137        var state = GetEntityState(e);
     2138        if (state && state.garrisonHolder)
     2139            return true;
     2140        return false;
     2141    });
     2142
     2143    Engine.PostNetworkCommand({"type": "unload-all-back-to-work", "garrisonHolders": garrisonHolders});
     2144}
     2145
    21132146function clearSelection()
    21142147{
    21152148    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 (hasClass(entState, "Worker") || hasClass(entState, "Trader"))
     298    {
     299        commands.push({
     300            "name": "back-to-work",
     301            "tooltip": "Back to Work",
     302            "icon": "production.png"
     303        });
     304    }
     305   
     306    if (entState.garrisonHolder)
     307    {
     308        commands.push({
     309            "name": "unload-all-back-to-work",
     310            "tooltip": "Unload All Units and Send the Workers Back to Work",
     311            "icon": "production.png"
     312        });
     313    }
    296314
    297315    return commands;
    298316}
  • binaries/data/mods/public/simulation/components/GarrisonHolder.js

     
    263263 * Ejects units and orders them to move to the Rally Point.
    264264 * Returns true if successful, false if not
    265265 */
    266 GarrisonHolder.prototype.PerformEject = function(entities, forced)
     266GarrisonHolder.prototype.PerformEject = function(entities, forced, backToWork)
    267267{
    268268    if (!this.IsGarrisoningAllowed() && !forced)
    269269        return false;
     
    283283            success = false;
    284284    }
    285285
    286     this.OrderWalkToRallyPoint(ejectedEntities);
     286    if (backToWork)
     287    {
     288        // Filter the entities who shall go back to work and those who shall not.
     289        var workers = [];
     290        var nonWorkers = [];
     291       
     292        ejectedEntities.forEach(function(e) {
     293        var cmpIdentity = Engine.QueryInterface(e, IID_Identity);
     294        var classes = cmpIdentity.GetClassesList();
     295        if (classes.some(function(c) {return (c == "Worker" || c == "Trader");}))
     296            workers.push(e);
     297        else
     298            nonWorkers.push(e);
     299        });
     300       
     301        var cmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership);
     302        ProcessCommand(cmpOwnership.GetOwner(), {"type": "back-to-work", "workers": workers});
     303       
     304        this.OrderWalkToRallyPoint(nonWorkers);
     305    }
     306    else
     307        this.OrderWalkToRallyPoint(ejectedEntities);
     308   
    287309    this.UpdateGarrisonFlag();
    288310
    289311    return success;
     
    369391 * and order them to move to the Rally Point
    370392 * Returns true if all successful, false if not
    371393 */
    372 GarrisonHolder.prototype.UnloadAll = function(forced)
     394GarrisonHolder.prototype.UnloadAll = function(forced, backToWork)
    373395{
    374396    var entities = this.entities.slice(0);
    375     return this.PerformEject(entities, forced);
     397    return this.PerformEject(entities, forced, backToWork);
    376398};
    377399
    378400/**
  • binaries/data/mods/public/simulation/components/UnitAI.js

     
    25232523    this.isIdle = false;
    25242524    this.lastFormationName = "";
    25252525    this.finishedOrder = false; // used to find if all formation members finished the order
     2526   
     2527    // To go back to work later
     2528    this.lastWorkOrder = undefined;
    25262529
    25272530    // For preventing increased action rate due to Stop orders or target death.
    25282531    this.lastAttacked = undefined;
     
    27992802    Engine.PostMessage(this.entity, MT_UnitAIStateChanged, { "to": state });
    28002803};
    28012804
     2805UnitAI.prototype.BackToWork = function()
     2806{
     2807    if(this.lastWorkOrder)
     2808    {
     2809        this.ReplaceOrder(this.lastWorkOrder.type, this.lastWorkOrder.data);
     2810        return true;
     2811    }
     2812    else
     2813        return false;
     2814};
     2815
    28022816/**
    28032817 * Call when the current order has been completed (or failed).
    28042818 * Removes the current order from the queue, and processes the
     
    28672881{
    28682882    var order = { "type": type, "data": data };
    28692883    this.orderQueue.push(order);
     2884   
     2885    if(type == "Gather" || type == "Trade" || type == "Repair")
     2886        this.lastWorkOrder = order;
    28702887
    28712888    // If we didn't already have an order, then process this new one
    28722889    if (this.orderQueue.length == 1)
  • binaries/data/mods/public/simulation/helpers/Commands.js

     
    386386        for each (var garrisonHolder in entities)
    387387        {
    388388            var cmpGarrisonHolder = Engine.QueryInterface(garrisonHolder, IID_GarrisonHolder);
    389             if (!cmpGarrisonHolder || !cmpGarrisonHolder.UnloadAll())
     389            if (!cmpGarrisonHolder || !cmpGarrisonHolder.UnloadAll(false, false))
    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;
     403   
     404    case "unload-all-back-to-work":
     405        var entities = FilterEntityList(cmd.garrisonHolders, player, controlAllUnits);
     406        for each (var garrisonHolder in entities)
     407        {
     408            var cmpGarrisonHolder = Engine.QueryInterface(garrisonHolder, IID_GarrisonHolder);
     409            if (!cmpGarrisonHolder || !cmpGarrisonHolder.UnloadAll(false, true))
     410                notifyUnloadFailure(player, garrisonHolder)
     411        }
     412        break;
    393413
    394414    case "formation":
    395415        GetFormationUnitAIs(entities, player, cmd.name).forEach(function(cmpUnitAI) {
     
    518538}
    519539
    520540/**
     541 * Sends a GUI notification about worker(s) that failed to go back to work.
     542 */
     543function notifyBackToWorkFailure(player, worker)
     544{
     545    var cmpPlayer = QueryPlayerIDInterface(player, IID_Player);
     546    var notification = {"player": cmpPlayer.GetPlayerID(), "message": "Some unit(s) can't go back to work" };
     547    var cmpGUIInterface = Engine.QueryInterface(SYSTEM_ENTITY, IID_GuiInterface);
     548    cmpGUIInterface.PushNotification(notification);
     549}
     550
     551/**
    521552 * Get some information about the formations used by entities.
    522553 * The entities must have a UnitAI component.
    523554 */