Ticket #1044: autogarrison-3.diff

File autogarrison-3.diff, 8.6 KB (added by mimo, 11 years ago)
  • binaries/data/mods/public/gui/session/input.js

     
    295295            }
    296296        }
    297297
    298         // Don't allow the rally point to be set on any of the currently selected entities
     298        // Don't allow the rally point to be set on any of the currently selected entities (used for unset)
     299        // except if the target is a productionQueue and the autorallypoint hotkey is pressed
    299300        for (var i = 0; i < selection.length; i++)
     301        {
    300302            if (target === selection[i])
     303            {
     304                if (Engine.HotkeyIsPressed("session.autorallypoint") && targetState.production && targetState.production.entities.length)
     305                    break;
    301306                return {"possible": false};
     307            }
     308        }
    302309
    303310        return {"possible": true, "data": data, "position": targetState.position, "cursor": cursor, "tooltip": tooltip};
    304311    }
     
    17881795    if (entity)
    17891796    {
    17901797        var entState = GetEntityState(entity);
    1791         var template = GetTemplateData(entState.template);
    1792         var unitName = getEntityName(template);
    17931798
    17941799        var playerID = Engine.GetPlayerID();
    17951800        var simState = GetSimState();
  • binaries/data/mods/public/simulation/components/ProductionQueue.js

     
    442442};
    443443
    444444/*
    445  * This function creates the entities and places them in world if possible.
    446  * returns the number of successfully spawned entities.
     445 * This function creates the entities and places them in world if possible
     446 * and returns the number of successfully created entities.
     447 * (some of these entities may be garrisoned directly if autogarrison, the others are spawned).
    447448 */
    448449ProductionQueue.prototype.SpawnUnits = function(templateName, count, metadata)
    449450{
     
    452453    var cmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership);
    453454    var cmpRallyPoint = Engine.QueryInterface(this.entity, IID_RallyPoint);
    454455   
     456    var createdEnts = [];
    455457    var spawnedEnts = [];
    456458   
    457459    if (this.entityCache.length == 0)
     
    476478        }
    477479    }
    478480
     481    var cmpAutoGarrison = undefined;
     482    if (cmpRallyPoint)
     483    {
     484        var data = cmpRallyPoint.GetData()[0];
     485        if (data && data.target == this.entity && data.command == "garrison")
     486            cmpAutoGarrison = Engine.QueryInterface(this.entity, IID_GarrisonHolder);
     487    }
     488
    479489    for (var i = 0; i < count; ++i)
    480490    {
    481491        var ent = this.entityCache[0];
    482         var pos = cmpFootprint.PickSpawnPoint(ent);
    483         if (pos.y < 0)
     492        var cmpNewOwnership = Engine.QueryInterface(ent, IID_Ownership);
     493        cmpNewOwnership.SetOwner(cmpOwnership.GetOwner());
     494
     495        if (cmpAutoGarrison && cmpAutoGarrison.PerformGarrison(ent))
    484496        {
    485             // Fail: there wasn't any space to spawn the unit
    486             break;
     497            var cmpUnitAI = Engine.QueryInterface(ent, IID_UnitAI);
     498            cmpUnitAI.Autogarrison();
    487499        }
    488500        else
    489501        {
    490             // Successfully spawned
    491             var cmpNewPosition = Engine.QueryInterface(ent, IID_Position);
    492             cmpNewPosition.JumpTo(pos.x, pos.z);
    493             // TODO: what direction should they face in?
     502            var pos = cmpFootprint.PickSpawnPoint(ent);
     503            if (pos.y < 0)
     504            {
     505                // Fail: there wasn't any space to spawn the unit
     506                break;
     507            }
     508            else
     509            {
     510                // Successfully spawned
     511                var cmpNewPosition = Engine.QueryInterface(ent, IID_Position);
     512                cmpNewPosition.JumpTo(pos.x, pos.z);
     513                // TODO: what direction should they face in?
     514                spawnedEnts.push(ent);
     515            }
     516        }
    494517
    495             var cmpNewOwnership = Engine.QueryInterface(ent, IID_Ownership);
    496             cmpNewOwnership.SetOwner(cmpOwnership.GetOwner());
    497            
    498             var cmpPlayerStatisticsTracker = QueryOwnerInterface(this.entity, IID_StatisticsTracker);
    499             cmpPlayerStatisticsTracker.IncreaseTrainedUnitsCounter();
     518        var cmpPlayerStatisticsTracker = QueryOwnerInterface(this.entity, IID_StatisticsTracker);
     519        cmpPlayerStatisticsTracker.IncreaseTrainedUnitsCounter();
    500520
    501             // Play a sound, but only for the first in the batch (to avoid nasty phasing effects)
    502             if (spawnedEnts.length == 0)
    503                 PlaySound("trained", ent);
     521        // Play a sound, but only for the first in the batch (to avoid nasty phasing effects)
     522        if (createdEnts.length == 0)
     523            PlaySound("trained", ent);
    504524           
    505             this.entityCache.shift();
    506             spawnedEnts.push(ent);
    507         }
     525        this.entityCache.shift();
     526        createdEnts.push(ent);
    508527    }
    509528
    510529    if (spawnedEnts.length > 0)
     
    518537            {
    519538                var commands = GetRallyPointCommands(cmpRallyPoint, spawnedEnts);
    520539                for each(var com in commands)
    521                 {
    522540                    ProcessCommand(cmpOwnership.GetOwner(), com);
    523                 }
    524541            }
    525542        }
     543    }
    526544
     545    if (createdEnts.length > 0)
     546    {
    527547        Engine.PostMessage(this.entity, MT_TrainingFinished, {
    528             "entities": spawnedEnts,
     548            "entities": createdEnts,
    529549            "owner": cmpOwnership.GetOwner(),
    530550            "metadata": metadata,
    531551        });
    532552    }
    533553   
    534     return spawnedEnts.length;
     554    return createdEnts.length;
    535555};
    536556
    537557/*
  • binaries/data/mods/public/simulation/components/UnitAI.js

     
    620620        }
    621621    },
    622622
     623    "Order.Autogarrison": function(msg) {
     624        this.SetNextState("INDIVIDUAL.AUTOGARRISON");
     625    },
     626
    623627    "Order.Cheering": function(msg) {
    624628        this.SetNextState("INDIVIDUAL.CHEERING");
    625629    },
     
    23312335            },
    23322336        },
    23332337
     2338        "AUTOGARRISON": {
     2339            "enter": function() {
     2340                this.isGarrisoned = true;
     2341                return false;
     2342            },
     2343               
     2344            "Order.Ungarrison": function() {
     2345                if (this.FinishOrder())
     2346                    return;
     2347            },
     2348
     2349            "leave": function() {
     2350                this.isGarrisoned = false;
     2351            }
     2352        },
     2353
    23342354        "CHEERING": {
    23352355            "enter": function() {
    23362356                // Unit is invulnerable while cheering
     
    39804000};
    39814001
    39824002/**
     4003 * Adds autogarrison order to the queue (only used by ProductionQueue for auto-garrisoning)
     4004 */
     4005UnitAI.prototype.Autogarrison = function()
     4006{
     4007    this.AddOrder("Autogarrison", null, false);
     4008};
     4009
     4010/**
    39834011 * Adds gather order to the queue, forced by the player
    39844012 * until the target is reached
    39854013 */
  • binaries/data/mods/public/simulation/components/GarrisonHolder.js

     
    157157 */
    158158GarrisonHolder.prototype.Garrison = function(entity)
    159159{
     160    var cmpPosition = Engine.QueryInterface(entity, IID_Position);
     161    if (!cmpPosition)
     162        return false;
     163
     164    if (!this.PerformGarrison(entity))
     165        return false;
     166
     167    cmpPosition.MoveOutOfWorld();
     168    return true;
     169};
     170
     171GarrisonHolder.prototype.PerformGarrison = function(entity)
     172{
    160173    if (!this.HasEnoughHealth())
    161174        return false;
    162175
     
    167180    if (this.entities.length >= this.GetCapacity())
    168181        return false;
    169182
    170     var cmpPosition = Engine.QueryInterface(entity, IID_Position);
    171     if (!cmpPosition)
    172         return false;
    173 
    174183    if (!this.timer && this.GetHealRate() > 0)
    175184    {
    176185        var cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer);
     
    179188
    180189    // Actual garrisoning happens here
    181190    this.entities.push(entity);
    182     cmpPosition.MoveOutOfWorld();
    183191    this.UpdateGarrisonFlag();
    184192    var cmpProductionQueue = Engine.QueryInterface(entity, IID_ProductionQueue);
    185193    if (cmpProductionQueue)
     
    260268        if (rallyPos)
    261269        {
    262270            var commands = GetRallyPointCommands(cmpRallyPoint, entities);
     271            // ignore the rally point if it is autogarrison
     272            if (commands[0].type == "garrison" && commands[0].target == this.entity)
     273                return;
    263274            for each (var com in commands)
    264275            {
    265276                ProcessCommand(cmpOwnership.GetOwner(), com);
  • binaries/data/config/default.cfg

     
    276276hotkey.session.massbarter = Shift           ; Modifier to barter bunch of resources
    277277hotkey.session.masstribute = Shift          ; Modifier to tribute bunch of resources
    278278hotkey.session.unloadtype = Shift           ; Modifier to unload all units of type
     279hotkey.session.autorallypoint = Ctrl        ; Modifier to set the rally point of one building on itself
    279280hotkey.session.deselectgroup = Ctrl         ; Modifier to deselect units when clicking group icon, instead of selecting
    280281hotkey.session.rotate.cw = RightBracket     ; Rotate building placement preview clockwise
    281282hotkey.session.rotate.ccw = LeftBracket     ; Rotate building placement preview anticlockwise