Ticket #799: patch_backtowork.2.diff

File patch_backtowork.2.diff, 5.9 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;
    18511854            default:
    18521855                break;
    18531856            }
     
    21102113    Engine.PostNetworkCommand({"type": "unload-all", "garrisonHolders": garrisonHolders});
    21112114}
    21122115
     2116function backToWork()
     2117{
     2118    // Filter out all entities that can't go back to work.
     2119    var workers = g_Selection.toList().filter(function(e) {
     2120        var state = GetEntityState(e);
     2121        if (state && hasClass(state, "Worker"))
     2122            return true;
     2123        return false;
     2124    });
     2125   
     2126    Engine.PostNetworkCommand({"type": "back-to-work", "workers": workers});
     2127   
     2128}
     2129
    21132130function clearSelection()
    21142131{
    21152132    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"))
     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/ResourceGatherer.js

     
    6363    // each with an independent capacity, but the rest of the game currently
    6464    // ensures and assumes we'll only be carrying one type at once)
    6565
    66     // The last exact type gathered, so we can render appropriate props
    67     this.lastCarriedType = undefined; // { generic, specific }
     66    // Informations needed to go back to work later and render appropriate props
     67    this.lastWorkInformation = undefined;
    6868};
    6969
    7070/**
     
    119119 */
    120120ResourceGatherer.prototype.GetLastCarriedType = function()
    121121{
    122     if (this.lastCarriedType && this.lastCarriedType.generic in this.carrying)
    123         return this.lastCarriedType;
     122    if (this.lastWorkInformation && this.lastWorkInformation.type && this.lastWorkInformation.type.generic in this.carrying)
     123        return this.lastWorkInformation.type;
    124124    else
    125125        return undefined;
    126126};
     
    194194
    195195    var gatherAmount = 1;
    196196
     197    // Remember what we are gathering to go back to it later   
    197198    var cmpResourceSupply = Engine.QueryInterface(target, IID_ResourceSupply);
    198199    var type = cmpResourceSupply.GetType();
    199 
     200   
     201    var cmpTemplateManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_TemplateManager);
     202    var template = cmpTemplateManager.GetCurrentTemplateName(target);
     203   
     204    if (template.indexOf("resource|") != -1)
     205        template = template.slice(9);
     206   
     207    var lastPos = undefined;
     208    var cmpPosition = Engine.QueryInterface(target, IID_Position);
     209    if (cmpPosition && cmpPosition.IsInWorld())
     210        lastPos = cmpPosition.GetPosition();
     211   
     212    // "force" will be used when giving the "back to work" order
     213    // We don't want to remember treasure collecting since it's not a work we can 'go back to'
     214    if (type.generic != "treasure")
     215        this.lastWorkInformation = {"target" : target, "type" : type, "template" : template, "lastPos" : lastPos, "force" : true};
     216   
    200217    // Initialise the carried count if necessary
    201218    if (!this.carrying[type.generic])
    202219        this.carrying[type.generic] = 0;
     
    208225
    209226    this.carrying[type.generic] += status.amount;
    210227
    211     this.lastCarriedType = type;
    212 
    213228    // Update stats of how much the player collected.
    214229    // (We have to do it here rather than at the dropsite, because we
    215230    // need to know what subtype it was)
     
    339354    Engine.PostMessage(this.entity, MT_ResourceCarryingChanged, { "to": this.GetCarryingStatus() });
    340355};
    341356
     357/**
     358 * Re-gather the last type of resource we carried
     359 */
     360ResourceGatherer.prototype.BackToWork = function()
     361{
     362    var cmpUnitAI = Engine.QueryInterface(this.entity, IID_UnitAI);
     363   
     364    if(cmpUnitAI && this.lastWorkInformation)
     365    {
     366        cmpUnitAI.AddOrder("Gather", this.lastWorkInformation, false);
     367        return true;
     368    }
     369    else
     370        return false;
     371}
     372   
     373
    342374Engine.RegisterComponentType(IID_ResourceGatherer, "ResourceGatherer", ResourceGatherer);
  • 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 cmpResourceGatherer = Engine.QueryInterface(worker, IID_ResourceGatherer);
     399            if (!cmpResourceGatherer || !cmpResourceGatherer.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": "This worker 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 */