Heracles421

FtD Submarine Control

Dec 15th, 2016
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.56 KB | None | 0 0
  1. TerrainAdjustmentTime = 10;
  2. CruiseDepth = 150;
  3. MaxDepth = 400;
  4. Clearance = 30;
  5.  
  6. function Update(I)
  7.     I:ClearLogs();
  8.     DepthCorrect(I);
  9.     PitchCorrect(I);
  10. end
  11.  
  12. function TerrainPrediction(I)
  13.     -- Set return value to lowest possible altitude
  14.     local ReturnValue = 500;
  15.     if I:GetForwardsVelocityMagnitude() > 0 then
  16.         for zIndex = I:GetConstructMinDimensions().z, I:GetConstructMaxDimensions().z + (I:GetForwardsVelocityMagnitude()*TerrainAdjustmentTime), 1 do
  17.             -- Use highest terrain from front of ship to speed*TerrainAdjustmentTime distance ahead
  18.             if ReturnValue > -I:GetTerrainAltitudeForLocalPosition(0,I:GetConstructPosition().y,zIndex) then
  19.                 ReturnValue = -I:GetTerrainAltitudeForLocalPosition(0,I:GetConstructPosition().y,zIndex);
  20.             end
  21.         end
  22.     else
  23.         for zIndex = I:GetConstructMaxDimensions().z, I:GetConstructMinDimensions().z - (I:GetForwardsVelocityMagnitude()*TerrainAdjustmentTime), -1 do
  24.             -- Use highest terrain from rear of ship to speed*TerrainAdjustmentTime distance behind
  25.             if ReturnValue > -I:GetTerrainAltitudeForLocalPosition(0,I:GetConstructPosition().y,zIndex) then
  26.                 ReturnValue = -I:GetTerrainAltitudeForLocalPosition(0,I:GetConstructPosition().y,zIndex);
  27.             end
  28.         end
  29.     end
  30.     return ReturnValue;
  31. end
  32.  
  33. function DepthCorrect(I)
  34.     local maxDepth = TerrainPrediction(I);
  35.     I:Log('Terrain depth at prediction point: '.. math.floor(maxDepth));
  36.     I:LogToHud('Terrain depth at prediction point: '.. math.floor(maxDepth));
  37.     local CClearance = 500;
  38.     if CruiseDepth + I:GetConstructPosition().y > maxDepth + I:GetConstructPosition().y then
  39.         CClearance = maxDepth + I:GetConstructPosition().y;
  40.     else
  41.         CClearance = CruiseDepth + I:GetConstructPosition().y;
  42.         Clearance = 0;
  43.     end
  44.    
  45.     if CClearance < Clearance then
  46.         I:RequestComplexControllerStimulus(1);
  47.         I:Log('Clearance too small! '.. math.floor(CClearance) ..'<'..Clearance ..' --> Engaging thrusters!');
  48.         I:LogToHud('Clearance too small! Engaging thrusters!');
  49.     end
  50.    
  51.     if CClearance > Clearance then
  52.         I:RequestComplexControllerStimulus(2);
  53.         I:Log('Clearance OK! Distance to terrain: '.. math.floor(CClearance)..'>'..Clearance);
  54.         I:LogToHud('Clearance OK! Descending!');
  55.     else
  56.         I:Log('Desired depth reached! Depth ' .. math.floor(-I:GetConstructPosition().y));
  57.         I:LogToHud('Desired depth reached!');
  58.     end
  59. end
  60.  
  61. function PitchCorrect(I)
  62.     pitch = I:GetConstructPitch();
  63.     if pitch > 180 then
  64.         pitch = pitch - 360;
  65.     end
  66.     --I:Log(pitch);
  67.  
  68.     if pitch > 1 then
  69.         I:RequestComplexControllerStimulus(11);
  70.     end
  71.  
  72.     if pitch < -1 then
  73.         I:RequestComplexControllerStimulus(12);
  74.     end
  75. end
Add Comment
Please, Sign In to add comment