Ticket #745: rallypoint_on_entities2011_12_5.diff

File rallypoint_on_entities2011_12_5.diff, 10.8 KB (added by Jonathan Waller, 12 years ago)
  • binaries/data/mods/public/gui/session/input.js

     
    139139    if (!target)
    140140    {
    141141        if (action == "set-rallypoint" && haveRallyPoints)
    142             return {"possible": true};
     142            return {"possible": true, "command": "walk"};
    143143        else if (action == "move")
    144144            return {"possible": true};
    145145        else
     
    163163    // Check if any entities in the selection can gather the requested resource,
    164164    // can return to the dropsite, can build the foundation, or can attack the enemy
    165165    var simState = Engine.GuiInterfaceCall("GetSimulationState");
     166   
     167    // Look to see what type of command units going to the rally point should use
     168    if (haveRallyPoints && action == "set-rallypoint"){
     169        // haveRallyPoints ensures all selected entities can have rally points.
     170        // We assume that all entities are owned by the same player.
     171        var entState = GetEntityState(selection[0]);
     172       
     173        var playerState = simState.players[entState.player];
     174        var playerOwned = (targetState.player == entState.player);
     175        var allyOwned = playerState.isAlly[targetState.player];
     176        var enemyOwned = playerState.isEnemy[targetState.player];
     177        var gaiaOwned = (targetState.player == 0);
     178       
     179        var cursor = "";
     180       
     181        // default to walking there
     182        var data = {command: "walk"};
     183        if (targetState.garrisonHolder && playerOwned)
     184        {
     185            data.command = "garrison";
     186            data.target = target;
     187            cursor = "action-garrison";
     188        }
     189        else if (targetState.resourceSupply && (playerOwned || gaiaOwned))
     190        {
     191            var resourceType = targetState.resourceSupply.type.generic;
     192            if (resourceType === "treasure")
     193            {
     194                resourceType = targetState.resourceSupply.type.specific;
     195                cursor = "action-gather-" + targetState.resourceSupply.type.generic;
     196            }
     197            else
     198            {
     199                cursor = "action-gather-" + targetState.resourceSupply.type.specific;
     200            }
     201            data.command = "gather";
     202            data.resourceType = resourceType;
     203        }
     204        else if (targetState.foundation && entState.buildEntities)
     205        {
     206            data.command = "build";
     207            data.target = target;
     208            cursor = "action-build";
     209        }
     210        else if (targetState.needsRepair && allyOwned)
     211        {
     212            data.command = "repair";
     213            data.target = target;
     214            cursor = "action-repair";
     215        }
     216       
     217        return {"possible": true, "data": data, "position": targetState.position, "cursor": cursor};
     218    }
    166219
    167220    for each (var entityID in selection)
    168221    {
     
    312365            return {"type": "build", "cursor": "action-repair", "target": target};
    313366        else if (getActionInfo("attack", target).possible)
    314367            return {"type": "attack", "cursor": "action-attack", "target": target};
    315         else if(getActionInfo("set-rallypoint", target).possible)
    316             return {"type": "set-rallypoint"};
     368        else if((actionInfo = getActionInfo("set-rallypoint", target)).possible)
     369            return {"type": "set-rallypoint", "cursor": actionInfo.cursor, "data": actionInfo.data, "position": actionInfo.position};
    317370        else if(getActionInfo("unset-rallypoint", target).possible)
    318                 return {"type": "unset-rallypoint"};
     371            return {"type": "unset-rallypoint"};
    319372        else if (getActionInfo("move", target).possible)
    320373            return {"type": "move"};
    321374    }
     
    932985        return true;
    933986
    934987    case "set-rallypoint":
    935         var target = Engine.GetTerrainAtPoint(ev.x, ev.y);
    936         Engine.PostNetworkCommand({"type": "set-rallypoint", "entities": selection, "x": target.x, "z": target.z});
     988        var pos = undefined;
     989        // if there is a position set in the action then use this so that when setting a
     990        // rally point on an entity it is centered on that entity
     991        if (action.position)
     992        {
     993            pos = action.position;
     994        }
     995        else
     996        {
     997            pos = Engine.GetTerrainAtPoint(ev.x, ev.y);
     998        }
     999        Engine.PostNetworkCommand({"type": "set-rallypoint", "entities": selection, "x": pos.x, "z": pos.z, "data": action.data});
    9371000        // Display rally point at the new coordinates, to avoid display lag
    9381001        Engine.GuiInterfaceCall("DisplayRallyPoint", {
    9391002            "entities": selection,
    940             "x": target.x,
    941             "z": target.z
     1003            "x": pos.x,
     1004            "z": pos.z
    9421005        });
    9431006        return true;
    9441007
  • binaries/data/mods/public/simulation/helpers/Commands.js

     
    9494            cmpUnitAI.Gather(cmd.target, cmd.queued);
    9595        });
    9696        break;
     97       
     98    case "gatherNearPosition":
     99        var entities = FilterEntityList(cmd.entities, player, controlAllUnits);
     100        GetFormationUnitAIs(entities).forEach(function(cmpUnitAI) {
     101            cmpUnitAI.GatherNearPosition([cmd.x, cmd.z], cmd.resourceType, cmd.queued);
     102        });
     103        break;
    97104
    98105    case "returnresource":
    99106        // Check dropsite is owned by player
     
    288295        {
    289296            var cmpRallyPoint = Engine.QueryInterface(ent, IID_RallyPoint);
    290297            if (cmpRallyPoint)
     298            {
    291299                cmpRallyPoint.SetPosition(cmd.x, cmd.z);
     300                cmpRallyPoint.SetData(cmd.data);
     301            }
    292302        }
    293303        break;
    294304
  • binaries/data/mods/public/simulation/components/TrainingQueue.js

     
    280280
    281281    if (spawnedEnts.length > 0)
    282282    {
    283         // If a rally point is set, walk towards it (in formation)
     283        // If a rally point is set, walk towards it (in formation) using a suitable command based on where the
     284        // rally point is placed.
    284285        if (cmpRallyPoint)
    285286        {
    286287            var rallyPos = cmpRallyPoint.GetPosition();
    287288            if (rallyPos)
    288289            {
    289                 ProcessCommand(cmpOwnership.GetOwner(), {
    290                     "type": "walk",
    291                     "entities": spawnedEnts,
    292                     "x": rallyPos.x,
    293                     "z": rallyPos.z,
    294                     "queued": false
    295                 });
     290                ProcessCommand(cmpOwnership.GetOwner(), getRallyPointCommand(cmpRallyPoint, spawnedEnts));
    296291            }
    297292        }
    298293
  • binaries/data/mods/public/simulation/components/GarrisonHolder.js

     
    191191        var rallyPos = cmpRallyPoint.GetPosition();
    192192        if (rallyPos)
    193193        {
    194             ProcessCommand(cmpOwnership.GetOwner(), {
    195                 "type": "walk",
    196                 "entities": entities,
    197                 "x": rallyPos.x,
    198                 "z": rallyPos.z,
    199                 "queued": false
    200             });
     194            ProcessCommand(cmpOwnership.GetOwner(), getRallyPointCommand(cmpRallyPoint, entities));
    201195        }
    202196    }
    203197};
  • binaries/data/mods/public/simulation/components/UnitAI.js

     
    284284    },
    285285
    286286    "Order.Gather": function(msg) {
     287       
    287288        // If the target is still alive, we need to kill it first
    288289        if (this.MustKillGatherTarget(this.order.data.target) && this.CheckTargetVisible(this.order.data.target))
    289290        {
     
    315316            this.SetNextState("INDIVIDUAL.GATHER.GATHERING");
    316317        }
    317318    },
     319   
     320    "Order.GatherNearPosition": function(msg){
     321        // Move the unit to the position to gather from.
     322        this.MoveToPoint(this.order.data.x, this.order.data.z);
     323        this.SetNextState("INDIVIDUAL.GATHER.WALKING");
     324    },
    318325
    319326    "Order.ReturnResource": function(msg) {
    320327        // Try to move to the dropsite
     
    408415            cmpFormation.CallMemberFunction("Gather", [msg.data.target, false]);
    409416            cmpFormation.Disband();
    410417        },
     418       
     419        "Order.GatherNearPosition": function(msg){
     420            var cmpFormation = Engine.QueryInterface(this.entity, IID_Formation);
     421            cmpFormation.CallMemberFunction("GatherNearPosition", [[msg.data.x, msg.data.z], msg.data.type, false]);
     422            cmpFormation.Disband();
     423        },
    411424
    412425        "Order.ReturnResource": function(msg) {
    413426            // TODO: see notes in Order.Attack
     
    815828                    this.SetNextState("GATHERING");
    816829                },
    817830            },
     831           
     832            // Walking to a good place to gather resoruce near, ued by gatherNearPosition
     833            "WALKING": {
     834                "enter": function() {
     835                    this.SelectAnimation("move");
     836                },
    818837
     838                "MoveCompleted": function(msg) {
     839                        var resourceType = this.order.data.type;
     840                       
     841                        // Try to find another nearby target of the same specific type
     842                        var nearby = this.FindNearbyResource(function (ent, type) {
     843                            if (type.generic === "treasure"){
     844                                return (type.specific == resourceType);
     845                            }
     846                            return (type.generic == resourceType);
     847                        });
     848                       
     849                        // If there is a nearby resource start gathering
     850                        if (nearby)
     851                        {
     852                            this.Gather(nearby, false);
     853                            return;
     854                        }
     855                       
     856                        // Couldn't find nearby resources, so give up
     857                        this.FinishOrder();
     858
     859                },
     860            },
     861
    819862            "GATHERING": {
    820863                "enter": function() {
    821864                    this.StartTimer(1000, 1000);
     
    21792222        switch (order.type)
    21802223        {
    21812224        case "Walk":
     2225        case "GatherNearPosition":
    21822226            // Add the distance to the target point
    21832227            var dx = order.data.x - pos.x;
    21842228            var dz = order.data.z - pos.z;
     
    23072351    this.AddOrder("Gather", { "target": target, "type": type, "lastPos": lastPos }, queued);
    23082352};
    23092353
     2354UnitAI.prototype.GatherNearPosition = function(position, type, queued){
     2355    this.AddOrder("GatherNearPosition", { "type": type, "x": position[0], "z": position[1] }, queued);
     2356}
     2357
    23102358UnitAI.prototype.ReturnResource = function(target, queued)
    23112359{
    23122360    if (!this.CanReturnResource(target, true))
  • binaries/data/mods/public/simulation/components/RallyPoint.js

     
    1616    }
    1717};
    1818
    19 RallyPoint.prototype.Unset = function()
     19RallyPoint.prototype.GetPosition = function()
    2020{
    21     this.pos = undefined;
     21    return this.pos;
    2222};
    2323
    24 RallyPoint.prototype.GetPosition = function()
     24// Extra data for the rally point, should have a command property and then helpful data for that command
     25// See getActionInfo in gui/input.js
     26RallyPoint.prototype.SetData = function(data)
    2527{
    26     return this.pos;
     28    this.data = data;
    2729};
    2830
     31// Returns the data associated with this rally point.  Uses the data structure:
     32// {"type": "walk/gather/garrison/...", "target": targetEntityId, "resourceType": "wood/food/metal/stone"}
     33// where target and resourceType are optional, also target may be an invalid entity, check for existance.
     34RallyPoint.prototype.GetData = function()
     35{
     36    return this.data;
     37};
     38
     39RallyPoint.prototype.Unset = function()
     40{
     41    this.pos = undefined;
     42    this.data = undefined;
     43};
     44
     45
    2946Engine.RegisterComponentType(IID_RallyPoint, "RallyPoint", RallyPoint);