Ticket #2062: watersink.2.patch

File watersink.2.patch, 7.5 KB (added by scythetwirler, 11 years ago)
  • binaries/data/mods/public/simulation/components/UnitMotionFlying.js

     
    11// (A serious implementation of this might want to use C++ instead of JS
    22// for performance; this is just for fun.)
    3 const shortFinal = 2.5;
     3const SHORT_FINAL = 2.5;
    44function UnitMotionFlying() {}
    55
    66UnitMotionFlying.prototype.Schema =
     
    5858    var cmpPosition = Engine.QueryInterface(this.entity, IID_Position);
    5959    var pos = cmpPosition.GetPosition();
    6060    var angle = cmpPosition.GetRotation().y;
    61 
     61    var cmpTerrain = Engine.QueryInterface(SYSTEM_ENTITY, IID_Terrain);
     62    var ground = cmpTerrain.GetGroundLevel(pos.x, pos.z);
     63    var cmpWaterManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_WaterManager);
     64    var ground = Math.max(cmpTerrain.GetGroundLevel(pos.x, pos.z), cmpWaterManager.GetWaterLevel(pos.x, pos.z));
    6265    var canTurn = true;
    6366
    64     if (!this.landing)
     67    if (this.landing)
    6568    {
    66         // If we haven't reached max speed yet then we're still on the ground;
    67         // otherwise we're taking off or flying
    68         // this.onGround in case of a go-around after landing (but not fully stopped)
    69         var cmpTerrain = Engine.QueryInterface(SYSTEM_ENTITY, IID_Terrain);
    70         var ground = cmpTerrain.GetGroundLevel(pos.x, pos.z);
    71         if (this.speed < this.template.TakeoffSpeed && this.onGround)
    72         {
    73             // Accelerate forwards
    74             this.speed = Math.min(this.template.MaxSpeed, this.speed + turnLength * this.template.AccelRate);
    75             canTurn = false;
    76             // Clamp to ground if below it, or descend if above
    77             if (pos.y < ground)
    78                 pos.y = ground;
    79             else if (pos.y > ground)
    80                 pos.y = Math.max(ground, pos.y - turnLength * this.template.ClimbRate);
    81         }
    82         else
    83         {
    84             this.onGround = false;
    85             // Climb/sink to max height above ground
    86             this.speed = Math.min(this.template.MaxSpeed, this.speed + turnLength * this.template.AccelRate);
    87             var targetHeight = ground + (+this.template.FlyingHeight);
    88             if (pos.y < targetHeight)
    89                 pos.y = Math.min(targetHeight, pos.y + turnLength * this.template.ClimbRate);
    90             else if (pos.y > targetHeight)
    91                 pos.y = Math.max(targetHeight, pos.y - turnLength * this.template.ClimbRate);           
    92         }
    93         cmpPosition.SetHeightFixed(pos.y);
    94     }
    95     else
    96     {
    9769        if (this.speed > 0 && this.onGround)
    9870        {
    9971            // Deaccelerate forwards...at a very reduced pace.
    10072            this.speed = Math.max(0, this.speed - turnLength * this.template.BrakingRate);
    10173            canTurn = false;
    102             // Clamp to ground if below it, or descend if above
    103             var cmpTerrain = Engine.QueryInterface(SYSTEM_ENTITY, IID_Terrain);
    104             var ground = cmpTerrain.GetGroundLevel(pos.x, pos.z);
     74            // Clamp to ground if below it, or descend if above                     
    10575            if (pos.y < ground)
    10676                pos.y = ground;
    10777            else if (pos.y > ground)
    108                 pos.y = Math.max(ground, pos.y - turnLength * this.template.ClimbRate);
    109             cmpPosition.SetHeightFixed(pos.y);         
     78                pos.y = Math.max(ground, pos.y - turnLength * this.template.ClimbRate);         
    11079        }
    11180        else if (this.speed == 0)
    11281        {
     
    12190            // We need to slow down to land!
    12291            this.speed = Math.max(this.template.LandingSpeed, this.speed - turnLength * this.template.SlowingRate);
    12392            canTurn = false;
    124             var cmpTerrain = Engine.QueryInterface(SYSTEM_ENTITY, IID_Terrain);
    125             var ground = cmpTerrain.GetGroundLevel(pos.x, pos.z);
    12693            var targetHeight = ground;
    12794            // Steep, then gradual descent.
    128             var descentRate = ((pos.y - targetHeight) / this.template.FlyingHeight * this.template.ClimbRate + shortFinal) * shortFinal;
     95            var descentRate = ((pos.y - targetHeight) / this.template.FlyingHeight * this.template.ClimbRate + SHORT_FINAL) * SHORT_FINAL;
    12996            if (pos.y < targetHeight)
    13097                pos.y = Math.max(targetHeight, pos.y + turnLength * descentRate);
    13198            else if (pos.y > targetHeight)
    13299                pos.y = Math.max(targetHeight, pos.y - turnLength * descentRate);
    133100            if (targetHeight == pos.y)
    134101                this.onGround = true;
    135             cmpPosition.SetHeightFixed(pos.y);
    136102        }       
    137103    }
     104    else
     105    {
     106        // If we haven't reached max speed yet then we're still on the ground;
     107        // otherwise we're taking off or flying
     108        // this.onGround in case of a go-around after landing (but not fully stopped)
     109        if (this.speed < this.template.TakeoffSpeed && this.onGround)
     110        {
     111            // Accelerate forwards
     112            this.speed = Math.min(this.template.MaxSpeed, this.speed + turnLength * this.template.AccelRate);
     113            canTurn = false;
     114            // Clamp to ground if below it, or descend if above
     115            if (pos.y < ground)
     116                pos.y = ground;
     117            else if (pos.y > ground)
     118                pos.y = Math.max(ground, pos.y - turnLength * this.template.ClimbRate);
     119        }
     120        else
     121        {
     122            this.onGround = false;
     123            // Climb/sink to max height above ground
     124            this.speed = Math.min(this.template.MaxSpeed, this.speed + turnLength * this.template.AccelRate);
     125            var targetHeight = ground + (+this.template.FlyingHeight);
     126            if (pos.y < targetHeight)
     127                pos.y = Math.min(targetHeight, pos.y + turnLength * this.template.ClimbRate);
     128            else if (pos.y > targetHeight)
     129                pos.y = Math.max(targetHeight, pos.y - turnLength * this.template.ClimbRate);           
     130        }
     131    }   
    138132
    139133    // If we're in range of the target then tell people that we've reached it
    140134    // (TODO: quantisation breaks this)
     
    144138        this.reachedTarget = true;
    145139        Engine.PostMessage(this.entity, MT_MotionChanged, { "starting": false, "error": false });
    146140    }
    147 
    148141    // If we're facing away from the target, and are still fairly close to it,
    149142    // then carry on going straight so we overshoot in a straight line
    150143    var isBehindTarget = ((this.targetX - pos.x) * Math.sin(angle) + (this.targetZ - pos.z) * Math.cos(angle) < 0);
    151144    // Overshoot the target: carry on straight
    152145    if (isBehindTarget && distFromTarget < this.template.MaxSpeed * this.template.OvershootTime)
    153146        canTurn = false;
    154 
    155147    if (canTurn)
    156148    {
    157149        // Turn towards the target
     
    170162
    171163    pos.x += this.speed * turnLength * Math.sin(angle);
    172164    pos.z += this.speed * turnLength * Math.cos(angle);
    173 
     165    cmpPosition.SetHeightFixed(pos.y);
    174166    cmpPosition.TurnTo(angle);
    175167    cmpPosition.MoveTo(pos.x, pos.z);
    176168};
  • binaries/data/mods/public/simulation/templates/other/plane.xml

     
    1313      <Spread>1.5</Spread>
    1414    </Ranged>
    1515  </Attack>
     16  <BuildingAI>
     17    <DefaultArrowCount>3</DefaultArrowCount>
     18    <GarrisonArrowMultiplier>1</GarrisonArrowMultiplier>
     19    <GarrisonArrowClasses>Infantry</GarrisonArrowClasses>
     20  </BuildingAI>
     21  <GarrisonHolder>
     22    <Max>1</Max>
     23    <EjectHealth>0</EjectHealth>
     24    <List datatype="tokens">Support Infantry</List>
     25    <BuffHeal>1</BuffHeal>
     26    <LoadingRange>5</LoadingRange>
     27  </GarrisonHolder>
    1628  <Identity>
    1729    <Civ>hele</Civ>
    1830    <SpecificName>P-51 Mustang</SpecificName>
     
    2133    <Icon>units/global_mustang.png</Icon>
    2234    <Formations datatype="tokens" replace=""/>
    2335  </Identity>
     36  <Health>
     37     <Max>100</Max>
     38    <Unhealable>true</Unhealable>
     39    <Repairable>true</Repairable>
     40  </Health>
    2441  <Obstruction disable=""/>
    2542  <Position>
    2643    <TurnRate>1.0</TurnRate>
     
    3653    <TurnRate>1.0</TurnRate>
    3754    <OvershootTime>2.0</OvershootTime>
    3855    <FlyingHeight>50.0</FlyingHeight>
    39     <ClimbRate>5.0</ClimbRate>
     56    <ClimbRate>15.0</ClimbRate>
    4057  </UnitMotionFlying>
    4158  <Vision>
    4259    <Range>100</Range>