Ticket #745: rallypoint_on_entities_2011_12_10.patch
File rallypoint_on_entities_2011_12_10.patch, 12.2 KB (added by , 12 years ago) |
---|
-
gui/session/input.js
138 138 if (!target) 139 139 { 140 140 if (action == "set-rallypoint" && haveRallyPoints) 141 return {"possible": true };141 return {"possible": true, "command": "walk"}; 142 142 else if (action == "move") 143 143 return {"possible": true}; 144 144 else … … 162 162 // Check if any entities in the selection can gather the requested resource, 163 163 // can return to the dropsite, can build the foundation, or can attack the enemy 164 164 var simState = Engine.GuiInterfaceCall("GetSimulationState"); 165 166 // Look to see what type of command units going to the rally point should use 167 if (haveRallyPoints && action == "set-rallypoint"){ 168 // haveRallyPoints ensures all selected entities can have rally points. 169 // We assume that all entities are owned by the same player. 170 var entState = GetEntityState(selection[0]); 171 172 var playerState = simState.players[entState.player]; 173 var playerOwned = (targetState.player == entState.player); 174 var allyOwned = playerState.isAlly[targetState.player]; 175 var enemyOwned = playerState.isEnemy[targetState.player]; 176 var gaiaOwned = (targetState.player == 0); 177 178 var cursor = ""; 179 180 // default to walking there 181 var data = {command: "walk"}; 182 if (targetState.garrisonHolder && playerOwned) 183 { 184 data.command = "garrison"; 185 data.target = target; 186 cursor = "action-garrison"; 187 } 188 else if (targetState.resourceSupply && (playerOwned || gaiaOwned)) 189 { 190 var resourceType = targetState.resourceSupply.type.generic; 191 if (resourceType === "treasure") 192 { 193 resourceType = targetState.resourceSupply.type.specific; 194 cursor = "action-gather-" + targetState.resourceSupply.type.generic; 195 } 196 else 197 { 198 cursor = "action-gather-" + targetState.resourceSupply.type.specific; 199 } 200 data.command = "gather"; 201 data.resourceType = resourceType; 202 } 203 else if (targetState.foundation && entState.buildEntities) 204 { 205 data.command = "build"; 206 data.target = target; 207 cursor = "action-build"; 208 } 209 else if (targetState.needsRepair && allyOwned) 210 { 211 data.command = "repair"; 212 data.target = target; 213 cursor = "action-repair"; 214 } 215 216 return {"possible": true, "data": data, "position": targetState.position, "cursor": cursor}; 217 } 165 218 166 219 for each (var entityID in selection) 167 220 { … … 309 362 return {"type": "build", "cursor": "action-build", "target": target}; 310 363 else if (getActionInfo("repair", target).possible) 311 364 return {"type": "build", "cursor": "action-repair", "target": target}; 365 else if((actionInfo = getActionInfo("set-rallypoint", target)).possible) 366 return {"type": "set-rallypoint", "cursor": actionInfo.cursor, "data": actionInfo.data, "position": actionInfo.position}; 312 367 else if (getActionInfo("attack", target).possible) 313 368 return {"type": "attack", "cursor": "action-attack", "target": target}; 314 else if(getActionInfo("set-rallypoint", target).possible)315 return {"type": "set-rallypoint"};316 369 else if(getActionInfo("unset-rallypoint", target).possible) 317 370 return {"type": "unset-rallypoint"}; 318 371 else if (getActionInfo("move", target).possible) 319 372 return {"type": "move"}; 320 373 } … … 931 984 return true; 932 985 933 986 case "set-rallypoint": 934 var target = Engine.GetTerrainAtPoint(ev.x, ev.y); 935 Engine.PostNetworkCommand({"type": "set-rallypoint", "entities": selection, "x": target.x, "z": target.z}); 987 var pos = undefined; 988 // if there is a position set in the action then use this so that when setting a 989 // rally point on an entity it is centered on that entity 990 if (action.position) 991 { 992 pos = action.position; 993 } 994 else 995 { 996 pos = Engine.GetTerrainAtPoint(ev.x, ev.y); 997 } 998 Engine.PostNetworkCommand({"type": "set-rallypoint", "entities": selection, "x": pos.x, "z": pos.z, "data": action.data}); 936 999 // Display rally point at the new coordinates, to avoid display lag 937 1000 Engine.GuiInterfaceCall("DisplayRallyPoint", { 938 1001 "entities": selection, 939 "x": target.x,940 "z": target.z1002 "x": pos.x, 1003 "z": pos.z 941 1004 }); 942 1005 return true; 943 1006 -
simulation/components/GarrisonHolder.js
191 191 var rallyPos = cmpRallyPoint.GetPosition(); 192 192 if (rallyPos) 193 193 { 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)); 201 195 } 202 196 } 203 197 }; -
simulation/components/RallyPoint.js
16 16 }; 17 17 }; 18 18 19 RallyPoint.prototype. Unset= function()19 RallyPoint.prototype.GetPosition = function() 20 20 { 21 this.pos = undefined;21 return this.pos; 22 22 }; 23 23 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 26 RallyPoint.prototype.SetData = function(data) 25 27 { 26 return this.pos;28 this.data = data; 27 29 }; 28 30 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. 34 RallyPoint.prototype.GetData = function() 35 { 36 return this.data; 37 }; 38 39 RallyPoint.prototype.Unset = function() 40 { 41 this.pos = undefined; 42 this.data = undefined; 43 }; 44 45 29 46 Engine.RegisterComponentType(IID_RallyPoint, "RallyPoint", RallyPoint); -
simulation/components/TrainingQueue.js
280 280 281 281 if (spawnedEnts.length > 0) 282 282 { 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. 284 285 if (cmpRallyPoint) 285 286 { 286 287 var rallyPos = cmpRallyPoint.GetPosition(); 287 288 if (rallyPos) 288 289 { 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)); 296 291 } 297 292 } 298 293 -
simulation/components/UnitAI.js
284 284 }, 285 285 286 286 "Order.Gather": function(msg) { 287 287 288 // If the target is still alive, we need to kill it first 288 289 if (this.MustKillGatherTarget(this.order.data.target) && this.CheckTargetVisible(this.order.data.target)) 289 290 { … … 315 316 this.SetNextState("INDIVIDUAL.GATHER.GATHERING"); 316 317 } 317 318 }, 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 }, 318 325 319 326 "Order.ReturnResource": function(msg) { 320 327 // Try to move to the dropsite … … 408 415 cmpFormation.CallMemberFunction("Gather", [msg.data.target, false]); 409 416 cmpFormation.Disband(); 410 417 }, 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 }, 411 424 412 425 "Order.ReturnResource": function(msg) { 413 426 // TODO: see notes in Order.Attack … … 815 828 this.SetNextState("GATHERING"); 816 829 }, 817 830 }, 831 832 // Walking to a good place to gather resoruce near, ued by gatherNearPosition 833 "WALKING": { 834 "enter": function() { 835 this.SelectAnimation("move"); 836 }, 818 837 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 819 862 "GATHERING": { 820 863 "enter": function() { 821 864 this.StartTimer(1000, 1000); … … 2179 2222 switch (order.type) 2180 2223 { 2181 2224 case "Walk": 2225 case "GatherNearPosition": 2182 2226 // Add the distance to the target point 2183 2227 var dx = order.data.x - pos.x; 2184 2228 var dz = order.data.z - pos.z; … … 2307 2351 this.AddOrder("Gather", { "target": target, "type": type, "lastPos": lastPos }, queued); 2308 2352 }; 2309 2353 2354 UnitAI.prototype.GatherNearPosition = function(position, type, queued){ 2355 this.AddOrder("GatherNearPosition", { "type": type, "x": position[0], "z": position[1] }, queued); 2356 } 2357 2310 2358 UnitAI.prototype.ReturnResource = function(target, queued) 2311 2359 { 2312 2360 if (!this.CanReturnResource(target, true)) -
simulation/helpers/Commands.js
94 94 cmpUnitAI.Gather(cmd.target, cmd.queued); 95 95 }); 96 96 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; 97 104 98 105 case "returnresource": 99 106 // Check dropsite is owned by player … … 288 295 { 289 296 var cmpRallyPoint = Engine.QueryInterface(ent, IID_RallyPoint); 290 297 if (cmpRallyPoint) 298 { 291 299 cmpRallyPoint.SetPosition(cmd.x, cmd.z); 300 cmpRallyPoint.SetData(cmd.data); 301 } 292 302 } 293 303 break; 294 304 -
simulation/helpers/RallyPointCommands.js
1 // Returns an command suitable for ProcessCommand() based on the rally point data. 2 // This assumes that the rally point has a valid position. 3 function getRallyPointCommand(cmpRallyPoint, spawnedEnts) 4 { 5 // Look and see if there is a command in the rally point data, otherwise just walk there. 6 var data = cmpRallyPoint.GetData(); 7 var rallyPos = cmpRallyPoint.GetPosition(); 8 var command = undefined; 9 if (data && data.command) 10 { 11 command = data.command; 12 } 13 else 14 { 15 command = "walk"; 16 } 17 18 // If a target was set and the target no longer exists them just walk to the rally point. 19 if (data && data.target) 20 { 21 if (! Engine.QueryInterface(data.target, IID_Position)) 22 { 23 command = "walk"; 24 } 25 } 26 27 switch (command) 28 { 29 case "gather": 30 return { 31 "type": "gatherNearPosition", 32 "entities": spawnedEnts, 33 "x": rallyPos.x, 34 "z": rallyPos.z, 35 "resourceType": data.resourceType, 36 "queued": false 37 }; 38 break; 39 case "repair": 40 case "build": 41 return { 42 "type": "repair", 43 "entities": spawnedEnts, 44 "target": data.target, 45 "queued": false, 46 "autocontinue": true 47 }; 48 break; 49 case "garrison": 50 return { 51 "type": "garrison", 52 "entities": spawnedEnts, 53 "target": data.target, 54 "queued": false 55 }; 56 } 57 58 // default return value 59 return { 60 "type": "walk", 61 "entities": spawnedEnts, 62 "x": rallyPos.x, 63 "z": rallyPos.z, 64 "queued": false 65 }; 66 } 67 68 Engine.RegisterGlobal("getRallyPointCommand", getRallyPointCommand);