Ticket #521: RallyPoints.diff

File RallyPoints.diff, 7.8 KB (added by evans, 22 months ago)
  • binaries/data/mods/public/gui/session_new/input.js

     
    6262} 
    6363 
    6464/** 
     65 * Checks if all the given entities are buildings. 
     66 * @param entities 
     67 */ 
     68function areAllBuildings(entities) 
     69{ 
     70    for each (var ent in entities) 
     71    { 
     72        var entState = Engine.GuiInterfaceCall("GetEntityState", ent); 
     73        //If the entity does not have a RallyPoint defined, 
     74        if (!entState.isABuilding) 
     75        { 
     76            return false; 
     77        } 
     78             
     79    } 
     80    return true; 
     81} 
     82 
     83/** 
    6584 * Determine the context-sensitive action that should be performed when the mouse is at (x,y) 
    6685 */ 
    6786function determineAction(x, y) 
     
    86105 
    87106    // If there's no unit, just walk 
    88107    if (!targets.length) 
    89         return {"type": "move"}; 
    90  
     108    { 
     109        //If all selected entities are buildings, 
     110        //set gather points, else make them walk 
     111        if (areAllBuildings(selection)) 
     112        { 
     113            return {"type": "set-rallypoint"}; 
     114        } 
     115        else 
     116        { 
     117            return {"type": "move"}; 
     118        } 
     119    } 
    91120    // Look at the first targeted entity 
    92121    // (TODO: maybe we eventually want to look at more, and be more context-sensitive? 
    93122    // e.g. prefer to attack an enemy unit, even if some friendly units are closer to the mouse) 
     
    266295                g_Selection.reset(); 
    267296                g_Selection.addList(ents); 
    268297 
     298                Engine.GuiInterfaceCall("DisplayRallyPoint", { 
     299                    "entities": ents[0] 
     300                }); 
     301                 
    269302                // Create the selection groups 
    270303                g_Selection.groups.createGroups(ents); 
    271304 
     
    463496                case "gather": 
    464497                    Engine.PostNetworkCommand({"type": "gather", "entities": selection, "target": action.target, "queued": queued}); 
    465498                    return true; 
    466  
     499                case "set-rallypoint": 
     500                    var target = Engine.GetTerrainAtPoint(ev.x, ev.y); 
     501                    Engine.PostNetworkCommand({"type": "set-rallypoint", "entities": selection, "x": target.x, "z": target.z}); 
     502                    //Display rally point passing just the coordinates 
     503                    Engine.GuiInterfaceCall("SetRallyPoint", { 
     504                        "x": target.x, 
     505                        "z": target.z 
     506                    }); 
     507                    return true; 
    467508                default: 
    468509                    throw new Error("Invalid action.type "+action.type); 
    469510                } 
     
    497538                if (!ents.length) 
    498539                { 
    499540                    g_Selection.reset(); 
    500                      
    501541                    inputState = INPUT_NORMAL; 
     542                    //Clear Rally Points 
     543                    Engine.GuiInterfaceCall("DisplayRallyPoint", { 
     544                        "entities": undefined 
     545                    }); 
    502546                    return true; 
    503547                } 
    504  
     548                //Display Rally Point 
     549                Engine.GuiInterfaceCall("DisplayRallyPoint", { 
     550                    "entities": [ents[0]] 
     551                }); 
    505552                g_Selection.reset(); 
    506553                g_Selection.addList([ents[0]]); 
    507  
    508554                inputState = INPUT_NORMAL; 
    509555                return true; 
    510556            } 
  • binaries/data/mods/public/simulation/helpers/Commands.js

     
    123123        }); 
    124124 
    125125        break; 
    126  
     126    case "set-rallypoint": 
     127        for each (var ent in cmd.entities) 
     128        { 
     129            var cmpRallyPoint = Engine.QueryInterface(ent, IID_RallyPoint); 
     130            if (cmpRallyPoint) 
     131                cmpRallyPoint.SetPosition(cmd.x, cmd.z); 
     132        } 
     133        break; 
    127134    default: 
    128135        error("Ignoring unrecognised command type '" + cmd.type + "'"); 
    129136    } 
  • binaries/data/mods/public/simulation/components/GuiInterface.js

     
    1111GuiInterface.prototype.Init = function() 
    1212{ 
    1313    this.placementEntity = undefined; // = undefined or [templateName, entityID] 
     14    this.rallyPoints = undefined; 
    1415}; 
    1516 
    1617GuiInterface.prototype.GetSimulationState = function(player) 
     
    124125        ret.resourceGatherRates = cmpResourceGatherer.GetGatherRates(); 
    125126    } 
    126127 
     128    var cmpRallyPoint = Engine.QueryInterface(ent, IID_RallyPoint); 
     129    if(cmpRallyPoint) 
     130    { 
     131        ret.isABuilding = true; 
     132        if (cmpRallyPoint.GetPosition() != undefined) 
     133        { 
     134            ret.isRallyPointSet = true; 
     135        } 
     136        else 
     137        { 
     138            ret.isRallyPointSet = false; 
     139        } 
     140    } 
    127141    return ret; 
    128142}; 
    129143 
     
    170184}; 
    171185 
    172186/** 
     187 * Displays the rally point of a building 
     188 */ 
     189GuiInterface.prototype.DisplayRallyPoint = function(player, cmd) 
     190{ 
     191    var positions; 
     192    if (this.rallyPoints) 
     193    { 
     194        //If there are rally points already displayed, destroy them 
     195        for each (var rallyPoint in this.rallyPoints) 
     196        { 
     197            Engine.DestroyEntity(rallyPoint); 
     198        } 
     199    }    
     200    if (cmd.entities) 
     201    { 
     202        //DisplayRallyPoints is called passing a list of entities for which 
     203        //Rally Points must be displayed 
     204        positions = new Array(); 
     205        for each (var ent in cmd.entities) 
     206        { 
     207            var cmpRallyPoint = Engine.QueryInterface(ent, IID_RallyPoint); 
     208            if (cmpRallyPoint && cmpRallyPoint.GetPosition()) 
     209            { 
     210                positions.push(cmpRallyPoint.GetPosition()); 
     211            } 
     212        } 
     213    } 
     214    else if (cmd.x && cmd.z) 
     215    { 
     216        //Sometimes DisplayRallyPoint() is called specifying just a coordinate. 
     217        positions = [{"x": cmd.x, "z": cmd.z}]; 
     218    } 
     219    if (positions) 
     220    { 
     221        this.rallyPoints = new Array(); 
     222        //Add Rally Point entitied for each building 
     223        for each (var pos in positions) 
     224        { 
     225            var rallyPoint = Engine.AddLocalEntity("actor|props/special/common/waypoint_flag.xml"); 
     226            var cmpPosition = Engine.QueryInterface(rallyPoint, IID_Position); 
     227            cmpPosition.JumpTo(pos.x, pos.z); 
     228            this.rallyPoints.push(rallyPoint); 
     229        } 
     230    } 
     231     
     232} 
     233 
     234/** 
    173235 * Display the building placement preview. 
    174236 * cmd.template is the name of the entity template, or "" to disable the preview. 
    175237 * cmd.x, cmd.z, cmd.angle give the location. 
     
    248310    } 
    249311}; 
    250312 
    251 GuiInterface.prototype.SetRangeDebugOverlay = function(player, enabled) 
    252 { 
    253     var cmpRangeManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager); 
    254     cmpRangeManager.SetDebugOverlay(enabled); 
    255 }; 
    256  
    257313// List the GuiInterface functions that can be safely called by GUI scripts. 
    258314// (GUI scripts are non-deterministic and untrusted, so these functions must be 
    259315// appropriately careful. They are called with a first argument "player", which is 
     
    268324    "SetPathfinderDebugOverlay": 1, 
    269325    "SetObstructionDebugOverlay": 1, 
    270326    "SetMotionDebugOverlay": 1, 
    271     "SetRangeDebugOverlay": 1, 
     327    "DisplayRallyPoint": 1 
    272328}; 
    273329 
    274330GuiInterface.prototype.ScriptCall = function(player, name, args) 
  • binaries/data/mods/public/simulation/components/TrainingQueue.js

     
    158158    var cmpFootprint = Engine.QueryInterface(this.entity, IID_Footprint); 
    159159    var cmpPosition = Engine.QueryInterface(this.entity, IID_Position); 
    160160    var cmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership); 
    161  
     161    var cmpRallyPoint = Engine.QueryInterface(this.entity, IID_RallyPoint); 
     162     
    162163    for (var i = 0; i < count; ++i) 
    163164    { 
    164165        var ent = Engine.AddEntity(templateName); 
     
    181182        cmpNewOwnership.SetOwner(cmpOwnership.GetOwner()); 
    182183 
    183184        // TODO: move to rally points 
     185        var cmpUnitAI = Engine.QueryInterface(ent, IID_UnitAI); 
     186        if (cmpUnitAI && cmpRallyPoint.GetPosition() != undefined) 
     187            cmpUnitAI.Walk(cmpRallyPoint.GetPosition().x, cmpRallyPoint.GetPosition().z, false); 
    184188    } 
    185189}; 
    186190 
  • binaries/data/mods/public/simulation/templates/template_structure.xml

     
    3636    <Range>36</Range> 
    3737    <RetainInFog>true</RetainInFog> 
    3838  </Vision> 
     39  <RallyPoint> 
     40  </RallyPoint> 
    3941</Entity>