Ticket #797: units_on_walls.diff

File units_on_walls.diff, 6.7 KB (added by sanderd17, 11 years ago)
  • binaries/data/mods/public/simulation/components/BuildingWalker.js

     
     1
     2function BuildingWalker() {}
     3
     4BuildingWalker.prototype.Schema =
     5    "<element name='WalkingHeight'>" +
     6        "<ref name='nonNegativeDecimal'/>" +
     7    "</element>";
     8
     9BuildingWalker.prototype.Init = function()
     10{
     11    warn("created");
     12    this.buildingEntities = [];
     13    // TODO
     14};
     15
     16BuildingWalker.prototype.GetWalkingHeight = function()
     17{
     18    var walkingHeight = +this.template.WalkingHeight;
     19    return ApplyTechModificationsToEntity("BuildingWalker/WalkingHeight", walkingHeight, this.entity);
     20};
     21
     22BuildingWalker.prototype.AddEntity = function(entity)
     23{
     24    this.buildingEntities.push(entity);
     25    var cmpPosition = Engine.QueryInterface(entity, IID_Position);
     26    var thisPosition = Engine.QueryInterface(this.entity, IID_Position);
     27    cmpPosition.SetHeightFixed(this.GetWalkingHeight() + thisPosition.GetPosition().y);
     28}
     29
     30BuildingWalker.prototype.RemoveEntity = function(entity)
     31{
     32    var index = this.buildingEntities.indexOf(entity);
     33    if (index == -1)
     34        return;
     35    this.buildingEntities.splice(index,1);
     36    var cmpPosition = Engine.QueryInterface(entity, IID_Position);
     37    cmpPosition.SetHeightOffset(0.0);
     38   
     39}
     40
     41BuildingWalker.prototype.OnDestroy = function()
     42{
     43    for (var i = 0; i < this.buildingEntities.length; i++)
     44    {
     45        this.RemoveEntity(this.buildingEntities[i]);
     46        // TODO garison to neares tower, or unload next to gate
     47    }
     48}
     49
     50Engine.RegisterComponentType(IID_BuildingWalker, "BuildingWalker", BuildingWalker);
  • binaries/data/mods/public/simulation/components/GarrisonHolder.js

     
    197197   
    198198    var cmpNewPosition = Engine.QueryInterface(entity, IID_Position);
    199199    cmpNewPosition.JumpTo(pos.x, pos.z);
     200    // TODO notify walls units are garisoning, instead of just redefining the offset
     201    cmpNewPosition.SetHeightOffset(0.0);
    200202    // TODO: what direction should they face in?
    201203   
    202204    Engine.PostMessage(this.entity, MT_GarrisonedUnitsChanged, {});
     
    225227    }
    226228};
    227229
     230GarrisonHolder.prototype.EjectToWall = function(entities, forced, targetID)
     231{
     232    var buildingWalker = Engine.QueryInterface(targetID, IID_BuildingWalker);
     233    var targetPos = Engine.QueryInterface(targetID, IID_Position).GetPosition();
     234    var thisPos = Engine.QueryInterface(this.entity, IID_Position).GetPosition2D();
     235    var dv = {x: targetPos.x-thisPos.x, y:targetPos.z-thisPos.y};
     236    var l = Math.sqrt(dv.x * dv.x + dv.y * dv.y);
     237   
     238    for (var i = 0; i < entities.length; i++)
     239    {
     240
     241        // TODO only allow ranged units,
     242        // check if there is some space left,
     243        // only ungarison if you're on the target wall, not next to it
     244        // ...
     245
     246        var entityIndex = this.entities.indexOf(entities[i]);
     247
     248        this.spaceOccupied -= 1;
     249        this.entities.splice(entityIndex, 1);
     250       
     251        var cmpUnitAI = Engine.QueryInterface(entities[i], IID_UnitAI);
     252        if (cmpUnitAI)
     253            cmpUnitAI.Ungarrison();
     254
     255        var cmpProductionQueue = Engine.QueryInterface(entities[i], IID_ProductionQueue);
     256        if (cmpProductionQueue)
     257            cmpProductionQueue.UnpauseProduction();
     258       
     259        var cmpPosition = Engine.QueryInterface(entities[i], IID_Position);
     260        cmpPosition.JumpTo(thisPos.x + dv.x / l * (7+2*i), thisPos.y + dv.y / l * (7+2*i));
     261        buildingWalker.AddEntity(entities[i]);
     262   
     263        Engine.PostMessage(this.entity, MT_GarrisonedUnitsChanged, {});
     264       
     265    }
     266   
     267    this.UpdateGarrisonFlag();
     268    var success = true;
     269    return success;
     270
     271};
     272
    228273/**
    229274 * Ejects units and orders them to move to the Rally Point.
    230275 * Returns true if successful, false if not
     
    233278{
    234279    var ejectedEntities = [];
    235280    var success = true;
     281    var cmpTemplateManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_TemplateManager);
     282    var template = cmpTemplateManager.GetCurrentTemplateName(this.entity);
     283    if (template.indexOf("wall_tower") != -1)
     284    {
     285        // this is a wall tower, is it ejecting to a wall?
     286        var cmpRallyPoint = Engine.QueryInterface(this.entity, IID_RallyPoint);
     287        var commands = GetRallyPointCommands(cmpRallyPoint, entities);
     288        warn(cmpRallyPoint.GetPositions()[0].x+", "+cmpRallyPoint.GetPositions()[0].z);
     289   
     290        // TODO only works when wall is repairable
     291        // instead use the position query, and query for objectso n this position   
     292        if (commands.length > 0 && commands[0].target)
     293        {
     294            warn(commands[0].target.toString());
     295            var buildingWalker = Engine.QueryInterface(commands[0].target, IID_BuildingWalker);         if (buildingWalker)
     296                return this.EjectToWall(entities, forced, commands[0].target);
     297        }
     298       
     299    }
     300   
    236301    for each (var entity in entities)
    237302    {
    238303        if (this.Eject(entity, forced))
  • binaries/data/mods/public/simulation/templates/structures/iber_wall_long.xml

     
    11<?xml version="1.0" encoding="utf-8"?>
    22<Entity parent="template_structure_defense_wall_long">
     3  <BuildingWalker>
     4    <WalkingHeight>8.5</WalkingHeight>
     5  </BuildingWalker>
    36  <Footprint>
    4     <Square width="37" depth="9"/>
     7    <Square width="37" depth="11"/>
    58    <Height>9.0</Height>
    69  </Footprint>
    710  <Identity>
     
    1114    <History>High and strongly built defensive stone walls were a common structure of the Iberian Peninsula during the period, and for long thereafter.</History>
    1215  </Identity>
    1316  <Obstruction>
    14     <Static width="36.0" depth="8"/>
     17    <Obstructions>
     18        <Front width="36.0" depth="4.0" x="0.0" z="4.5"/>
     19        <Back width="36.0" depth="4.0" x="0.0" z="-4.5"/>
     20        <Left width="1.0" depth="10.0" x="-17.5" z="0.0"/>
     21        <Right width="1.0" depth="10.0" x="17.5" z="0.0"/>
     22    </Obstructions>
    1523  </Obstruction>
    1624  <VisualActor>
    1725    <Actor>structures/iberians/wall_long.xml</Actor>
  • binaries/data/mods/public/simulation/templates/template_structure_defense_wall.xml

     
    3131    <stone>15</stone>
    3232    <metal>0</metal>
    3333  </Loot>
    34   <Obstruction>
    35     <Static width="6.0" depth="6.0"/>
    36   </Obstruction>
    3734  <RallyPoint disable=""/>
    3835  <Sound>
    3936    <SoundGroups>