Ticket #745: rallypoint_on_entities.diff

File rallypoint_on_entities.diff, 10.9 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        // default to walking there
     180        var data = {command: "walk"};
     181        if (targetState.garrisonHolder && playerOwned)
     182        {
     183            data.command = "garrison";
     184            data.target = target;
     185        }
     186        else if (targetState.resourceSupply && (playerOwned || gaiaOwned))
     187        {
     188            var resourceType = targetState.resourceSupply.type.generic;
     189            if (resourceType === "treasure")
     190            {
     191                resourceType = targetState.resourceSupply.type.specific;
     192            }
     193            data.command = "gather";
     194            data.resourceType = resourceType;
     195        }
     196        else if (targetState.foundation && entState.buildEntities)
     197        {
     198            data.command = "build";
     199            data.target = target;
     200        }
     201        else if (targetState.needsRepair && allyOwned)
     202        {
     203            data.command = "repair";
     204            data.target = target;
     205        }
     206        else if (targetState.hitpoints && enemyOwned){
     207            data.command = "attack";
     208            data.target = target;
     209        }
     210       
     211        return {"possible": true, "data": data, "position": targetState.position};
     212    }
    166213
    167214    for each (var entityID in selection)
    168215    {
     
    312359            return {"type": "build", "cursor": "action-repair", "target": target};
    313360        else if (getActionInfo("attack", target).possible)
    314361            return {"type": "attack", "cursor": "action-attack", "target": target};
    315         else if(getActionInfo("set-rallypoint", target).possible)
    316             return {"type": "set-rallypoint"};
     362        else if((actionInfo = getActionInfo("set-rallypoint", target)).possible)
     363            return {"type": "set-rallypoint", "data": actionInfo.data, "position": actionInfo.position};
    317364        else if(getActionInfo("unset-rallypoint", target).possible)
    318365                return {"type": "unset-rallypoint"};
    319366        else if (getActionInfo("move", target).possible)
     
    932979        return true;
    933980
    934981    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});
     982        var pos = undefined;
     983        // if there is a position set in the action then use this so that when setting a
     984        // rally point on an entity it is centered on that entity
     985        if (action.position)
     986        {
     987            pos = action.position;
     988        }
     989        else
     990        {
     991            pos = Engine.GetTerrainAtPoint(ev.x, ev.y);
     992        }
     993        Engine.PostNetworkCommand({"type": "set-rallypoint", "entities": selection, "x": pos.x, "z": pos.z, "data": action.data});
    937994        // Display rally point at the new coordinates, to avoid display lag
    938995        Engine.GuiInterfaceCall("DisplayRallyPoint", {
    939996            "entities": selection,
    940             "x": target.x,
    941             "z": target.z
     997            "x": pos.x,
     998            "z": pos.z
    942999        });
    9431000        return true;
    9441001
  • binaries/data/mods/public/simulation/helpers/Commands.js

  • binaries/data/mods/public/simulation/components/TrainingQueue.js

     function ProcessCommand(player, cmd)
     {
    @@ -94,6 +94,13 @@
     			cmpUnitAI.Gather(cmd.target, cmd.queued);
     		});
     		break;
    +		
    +	case "gatherNearPosition":
    +		var entities = FilterEntityList(cmd.entities, player, controlAllUnits);
    +		GetFormationUnitAIs(entities).forEach(function(cmpUnitAI) {
    +			cmpUnitAI.GatherNearPosition([cmd.x, cmd.z], cmd.resourceType, cmd.queued);
    +		});
    +		break;
     
     	case "returnresource":
     		// Check dropsite is owned by player
    @@ -288,7 +295,10 @@
     		{
     			var cmpRallyPoint = Engine.QueryInterface(ent, IID_RallyPoint);
     			if (cmpRallyPoint)
    +			{
     				cmpRallyPoint.SetPosition(cmd.x, cmd.z);
    +				cmpRallyPoint.SetData(cmd.data);
    +			}
     		}
     		break;
     
     
    286286            var rallyPos = cmpRallyPoint.GetPosition();
    287287            if (rallyPos)
    288288            {
    289                 ProcessCommand(cmpOwnership.GetOwner(), {
    290                     "type": "walk",
    291                     "entities": spawnedEnts,
    292                     "x": rallyPos.x,
    293                     "z": rallyPos.z,
    294                     "queued": false
    295                 });
     289                // Look and see if there is a command in the rally point data, otherwise just walk there.
     290                var data = cmpRallyPoint.GetData();
     291                var command = undefined;
     292                if (data && data.command){
     293                    command = data.command;
     294                }else{
     295                    command = "walk";
     296                }
     297               
     298                // If a target was set and the target no longer exists them just walk to the rally point.
     299                if (data && data.target){
     300                    if (! Engine.QueryInterface(data.target, IID_Position)){
     301                        command = "walk";
     302                    }
     303                }
     304               
     305                switch (command)
     306                {
     307                case "gather":
     308                    ProcessCommand(cmpOwnership.GetOwner(), {
     309                        "type": "gatherNearPosition",
     310                        "entities": spawnedEnts,
     311                        "x": rallyPos.x,
     312                        "z": rallyPos.z,
     313                        "resourceType": data.resourceType,
     314                        "queued": false
     315                    });
     316                    break;
     317                case "repair":
     318                case "build":
     319                    ProcessCommand(cmpOwnership.GetOwner(), {
     320                        "type": "repair",
     321                        "entities": spawnedEnts,
     322                        "target": data.target,
     323                        "queued": false,
     324                        "autocontinue": true
     325                    });
     326                    break;
     327                case "garrison":
     328                    ProcessCommand(cmpOwnership.GetOwner(), {
     329                        "type": "garrison",
     330                        "entities": spawnedEnts,
     331                        "target": data.target,
     332                        "queued": false
     333                    });
     334                    break;
     335                case "attack":
     336                    ProcessCommand(cmpOwnership.GetOwner(), {
     337                        "type": "attack",
     338                        "entities": spawnedEnts,
     339                        "target": data.target,
     340                        "queued": false
     341                    });
     342                    break;
     343                default:
     344                    ProcessCommand(cmpOwnership.GetOwner(), {
     345                        "type": "walk",
     346                        "entities": spawnedEnts,
     347                        "x": rallyPos.x,
     348                        "z": rallyPos.z,
     349                        "queued": false
     350                    });
     351                }
    296352            }
    297353        }
    298354
  • 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
     31RallyPoint.prototype.GetData = function()
     32{
     33    return this.data;
     34};
     35
     36RallyPoint.prototype.Unset = function()
     37{
     38    this.pos = undefined;
     39    this.data = undefined;
     40};
     41
     42
    2943Engine.RegisterComponentType(IID_RallyPoint, "RallyPoint", RallyPoint);