Advertisement
Guest User

Untitled

a guest
Jan 16th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.54 KB | None | 0 0
  1.  
  2. double forwardSpeed = 0;
  3. double horizontalSpeed = 0;
  4. double verticalSpeed = 0;
  5.  
  6. Vector3D position = new Vector3D(0,0,0);
  7. Vector3D oldPosition = new Vector3D(0,0,0);
  8. long oldTick=0;
  9. double frameTime = 0;
  10. double simSpeed = 1d;
  11.  
  12. bool isSetup = false;
  13.  
  14. float keyThrustForward = 0;
  15. float keyThrustRight = 0;
  16. float keyThrustUp = 0;
  17.  
  18. float dampForward = 0;
  19. float dampRight = 0;
  20. float dampUp = 0;
  21.  
  22. List<IMyGravityGenerator> gravsForward = new List<IMyGravityGenerator>();
  23. List<IMyGravityGenerator> gravsBackward = new List<IMyGravityGenerator>();
  24. List<IMyGravityGenerator> gravsRight = new List<IMyGravityGenerator>();
  25. List<IMyGravityGenerator> gravsLeft = new List<IMyGravityGenerator>();
  26. List<IMyGravityGenerator> gravsUp = new List<IMyGravityGenerator>();
  27. List<IMyGravityGenerator> gravsDown = new List<IMyGravityGenerator>();
  28.  
  29. bool isInput = false;
  30. bool isMoving = false;
  31.  
  32. IMyShipController controller = null;
  33.  
  34.  
  35. public void Main(string argument)
  36. {
  37.  
  38. if(!isSetup)
  39. {
  40. Setup();
  41. }
  42.  
  43. InitializeVectors();
  44. ReadInputs();
  45.  
  46. isInput = (keyThrustForward+keyThrustRight+keyThrustUp != 0);
  47. isMoving = (Math.Abs(forwardSpeed+horizontalSpeed+verticalSpeed) > 0.1f);
  48.  
  49. if(isMoving || isInput)
  50. {
  51. GetDamps();
  52. AdjustGravs();
  53. }
  54.  
  55. SwitchGravs();
  56.  
  57. }
  58.  
  59.  
  60.  
  61. private void ReadInputs()
  62. {
  63. keyThrustForward = -controller.MoveIndicator.Z * 9.81f;
  64. keyThrustRight = controller.MoveIndicator.X * 9.81f;
  65. keyThrustUp = controller.MoveIndicator.Y * 9.81f;
  66. }
  67.  
  68.  
  69. private void GetDamps()
  70. {
  71. dampForward = 0;
  72. dampRight = 0;
  73. dampUp = 0;
  74.  
  75. if(keyThrustForward == 0)
  76. {
  77. dampForward = (float)forwardSpeed/(float)simSpeed * 0.5f;
  78. }
  79. if(keyThrustRight == 0)
  80. {
  81. dampRight = (float)horizontalSpeed/(float)simSpeed * 0.5f;
  82. }
  83. if(keyThrustUp == 0)
  84. {
  85. dampUp = (float)verticalSpeed/(float)simSpeed * 0.5f;
  86. }
  87. }
  88.  
  89.  
  90. private void AdjustGravs()
  91. {
  92. for (int i=0; i<gravsForward.Count; ++i)
  93. {
  94. gravsForward[i].GravityAcceleration = -dampForward + keyThrustForward;
  95. }
  96. for (int i=0; i<gravsBackward.Count; ++i)
  97. {
  98. gravsBackward[i].GravityAcceleration = dampForward - keyThrustForward;
  99. }
  100. for (int i=0; i<gravsRight.Count; ++i)
  101. {
  102. gravsRight[i].GravityAcceleration = -dampRight + keyThrustRight;
  103. }
  104. for (int i=0; i<gravsLeft.Count; ++i)
  105. {
  106. gravsLeft[i].GravityAcceleration = dampRight - keyThrustRight;
  107. }
  108. for (int i=0; i<gravsUp.Count; ++i)
  109. {
  110. gravsUp[i].GravityAcceleration = -dampUp + keyThrustUp;
  111. }
  112. for (int i=0; i<gravsDown.Count; ++i)
  113. {
  114. gravsDown[i].GravityAcceleration = dampUp - keyThrustUp;
  115. }
  116. }
  117.  
  118.  
  119. private void SwitchGravs()
  120. {
  121. List<IMyTerminalBlock> list = new List<IMyTerminalBlock>();
  122.  
  123. if(isInput || isMoving)
  124. {
  125. GridTerminalSystem.GetBlocksOfType<IMyVirtualMass>(list);
  126. for (int i=0; i<list.Count; ++i)
  127. if(list[i].CubeGrid == Me.CubeGrid)
  128. {
  129. list[i].ApplyAction("OnOff_On");
  130. }
  131. GridTerminalSystem.GetBlocksOfType<IMyGravityGenerator>(list);
  132. for (int i=0; i<list.Count; ++i)
  133. {
  134. list[i].ApplyAction("OnOff_On");
  135. }
  136. }
  137. else
  138. {
  139. GridTerminalSystem.GetBlocksOfType<IMyVirtualMass>(list);
  140. for (int i=0; i<list.Count; ++i)
  141. if(list[i].CubeGrid == Me.CubeGrid)
  142. {
  143. list[i].ApplyAction("OnOff_Off");
  144. }
  145. GridTerminalSystem.GetBlocksOfType<IMyGravityGenerator>(list);
  146. for (int i=0; i<list.Count; ++i)
  147. {
  148. list[i].ApplyAction("OnOff_Off");
  149. }
  150. }
  151. }
  152.  
  153.  
  154. private void Setup()
  155. {
  156. isSetup = true;
  157. controller = MyController();
  158.  
  159. gravsForward.Clear();
  160. gravsBackward.Clear();
  161. gravsRight.Clear();
  162. gravsLeft.Clear();
  163. gravsUp.Clear();
  164. gravsDown.Clear();
  165.  
  166. List<IMyTerminalBlock> list = new List<IMyTerminalBlock>();
  167. GridTerminalSystem.GetBlocksOfType<IMyGravityGenerator>(list);
  168. for (int i=0; i<list.Count; ++i)
  169. {
  170. IMyGravityGenerator gravgen = list[i] as IMyGravityGenerator;
  171. gravgen.FieldSize = new Vector3(30,30,30);
  172. Vector3D gravDirection = gravgen.WorldMatrix.Down;
  173. if(gravDirection == controller.WorldMatrix.Forward)
  174. {
  175. gravsForward.Add(gravgen);
  176. }
  177. if(gravDirection == controller.WorldMatrix.Backward)
  178. {
  179. gravsBackward.Add(gravgen);
  180. }
  181. if(gravDirection == controller.WorldMatrix.Right)
  182. {
  183. gravsRight.Add(gravgen);
  184. }
  185. if(gravDirection == controller.WorldMatrix.Left)
  186. {
  187. gravsLeft.Add(gravgen);
  188. }
  189. if(gravDirection == controller.WorldMatrix.Up)
  190. {
  191. gravsUp.Add(gravgen);
  192. }
  193. if(gravDirection == controller.WorldMatrix.Down)
  194. {
  195. gravsDown.Add(gravgen);
  196. }
  197. }
  198. }
  199.  
  200.  
  201. private IMyShipController MyController()
  202. {
  203. List<IMyTerminalBlock> list = new List<IMyTerminalBlock>();
  204. GridTerminalSystem.GetBlocksOfType<IMyShipController>(list);
  205. for(int i=0; i<list.Count; i++)
  206. {
  207. if(list[i].CubeGrid == Me.CubeGrid && !list[i].CustomName.ToLower().Contains("cryo") && !list[i].CustomName.ToLower().Contains("passenger"))
  208. {
  209. IMyShipController controller = list[i] as IMyShipController;
  210. return controller;
  211. }
  212. }
  213. return null;
  214. }
  215.  
  216.  
  217. private void InitializeVectors()
  218. {
  219. position = controller.GetPosition();
  220. long tick = DateTime.Now.Ticks /10000;
  221. frameTime = frameTime*0.9d + Math.Max(1,(double)(tick-oldTick)) * 0.1d;
  222. simSpeed = (1000d/60d) / frameTime;
  223. oldTick = tick;
  224. forwardSpeed = (position-oldPosition).Dot(controller.WorldMatrix.Forward) / frameTime * 1021d;
  225. horizontalSpeed = (position-oldPosition).Dot(controller.WorldMatrix.Right) / frameTime * 1021d;
  226. verticalSpeed = (position-oldPosition).Dot(controller.WorldMatrix.Up) / frameTime * 1021d;
  227. oldPosition = position;
  228. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement