Advertisement
sumguytwitches

Fuel Functions

Feb 16th, 2021 (edited)
2,404
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.53 KB | None | 0 0
  1. // !runscript mMcreu59
  2.  
  3. function transferFuelAmount {
  4.  
  5. parameter srcEl to 0.
  6. parameter destEl to 1.
  7. parameter resType to "liquidfuel".
  8. parameter amount to 1.
  9.  
  10. set srcTanks to list().
  11. for elRes in ship:elements[srcEl]:resources
  12. if elRes:name = resType
  13. for p in elRes:parts
  14. for res in p:resources
  15. if res:Name = resType and res:amount > 0 and res:enabled {
  16. srcTanks:add(p).
  17. }
  18.  
  19.  
  20. set destTanks to list().
  21. for elRes in ship:elements[destEl]:resources
  22. if elRes:name = resType
  23. for p in elRes:parts
  24. for res in p:resources
  25. if res:Name = resType and res:amount + 1 < res:capacity and res:enabled = 1 {
  26. destTanks:add(p).
  27. }
  28. if srcTanks:length < 1 print "No Source Tanks with " + resType.
  29. if destTanks:length < 1 print "No destination tanks with room for " + resType.
  30. if srcTanks:length > 0 and destTanks:length > 0 {
  31. local trans is transfer(resType, srcTanks, destTanks, amount).
  32. set trans:active to true.
  33. on trans:status {print restype + " transfer is " + trans:status.}
  34. return trans.
  35. }
  36. }
  37. function transferfuel {
  38.  
  39. parameter srcEl to 0.
  40. parameter destEl to 1.
  41. parameter resType to "liquidfuel".
  42.  
  43. set srcTanks to list().
  44. for elRes in ship:elements[srcEl]:resources
  45. if elRes:name = resType
  46. for p in elRes:parts
  47. for res in p:resources
  48. if res:Name = resType and res:amount > 0 and res:enabled = 1{
  49. srcTanks:add(p).
  50. }
  51.  
  52.  
  53. set destTanks to list().
  54. for elRes in ship:elements[destEl]:resources
  55. if elRes:name = resType
  56. for p in elRes:parts
  57. for res in p:resources
  58. if res:Name = resType and res:amount + 1 < res:capacity and res:enabled = 1 {
  59. destTanks:add(p).
  60. }
  61.  
  62. if srcTanks:length < 1 print "No unlocked Source Tanks with " + resType.
  63. if destTanks:length < 1 print "No unlocked destination tanks with room for " + resType.
  64. if srcTanks:length > 0 and destTanks:length > 0 {
  65. local trans is transferAll(resType, srcTanks, destTanks).
  66. set trans:active to true.
  67. on trans:status {print restype + " transfer is " + trans:status. }
  68. return trans.
  69. }
  70. }
  71.  
  72. function FillMeUpScotty {
  73. parameter srcEl to 1.
  74. parameter destEl to 0.
  75. parameter resourceList to List("liquidfuel","oxidizer").
  76.  
  77. set transfers to list().
  78. for res in resourceList {
  79. set trans to transferfuel(srcEl, destEl, res).
  80. transfers:add(trans).
  81. }
  82. }
  83.  
  84. function ShowResources {
  85. set elcount to 0.
  86. for el in ship:elements {
  87. print "[" + elcount + "] " + el:name.
  88. set elcount to elcount + 1.
  89. set rescount to 0.
  90. for res in el:resources {
  91. print (" [" + rescount + "] " + res:name):padright(21)
  92. + round(res:amount / res:capacity * 100):tostring():padright(4)
  93. + round(res:amount,1):tostring():padright(8)
  94. + "/ "
  95. + round(res:capacity,1):tostring().
  96. set rescount to rescount + 1.
  97. }
  98. print "-".
  99. }
  100. }
  101.  
  102. function balanceResource {
  103. parameter resourceName is "liquidfuel".
  104.  
  105. for res in ship:resources
  106. if res:name = resourceName
  107. set shipResource to res.
  108.  
  109. set targetPercent to shipResource:amount / shipResource:capacity.
  110. print "Targeting " + round(targetPercent*100, 2):tostring() + "% in each tank".
  111.  
  112. set remainingTanks to shipResource:parts.
  113. set transfer to list().
  114. until remainingTanks:length = 1 {
  115. print remainingTanks:length + " remaining tanks".
  116. set activeTank to remainingTanks[0].
  117. remainingTanks:remove(0).
  118. for tankRes in activeTank:resources
  119. if tankRes:name = resourceName
  120. set activeTankResource to tankRes.
  121.  
  122.  
  123. set targetAmount to targetPercent * activeTankResource:capacity.
  124. set transferAmount to targetAmount - activeTankResource:amount.
  125. // if positive transfer in
  126. if(transferAmount > 0)
  127. set resTransfer to transfer(resourceName, remainingTanks, list(activeTank), transferAmount).
  128.  
  129. // if negative transfer out
  130. if(transferamount < 0)
  131. set resTransfer to transfer(resourceName, list(activeTank), remainingTanks, -transferAmount).
  132.  
  133. set resTransfer:active to true.
  134. wait until resTransfer:status <> "Transferring" or abs(targetAmount - activeTankResource:amount) < 0.1.
  135. set resTransfer:active to false.
  136.  
  137. if resTransfer:status <> "Finished" and resTransfer:status <> "Inactive" print resTransfer:status.
  138. }
  139. list engines in es. set totalTorque to v(0,0,0). for e in es set totalTorque to totalTorque + vcrs(e:position, e:facing:vector) * e:availablethrust.
  140. print "Complete. Torque at " + round(totalTorque:mag,4).
  141. }
  142.  
  143. function transferDv {
  144. parameter srcEl.
  145. parameter destEl.
  146. parameter isp.
  147. parameter targetdv.
  148.  
  149. set elementdrymass to 0.
  150. for p in ship:elements[destEl]:parts
  151. set elementdrymass to elementdrymass + p:drymass.
  152.  
  153. //add non fuel mass back into drymass
  154. set fuellist to list("liquidfuel","oxidizer"). //todo:switch for different engine fuel requirements
  155.  
  156. for elres in ship:elements[destEl]:resources {
  157. if not fuellist:contains(elres:name)
  158. set elementdrymass to elementdrymass + elres:amount * elres:density.
  159.  
  160. if elres:name = "liquidfuel"
  161. set currLiquid to elres:amount.
  162.  
  163. if elres:name = "oxidizer"
  164. set currOx to elres:amount.
  165. }
  166. set finalmass to elementdrymass * constant:e^(targetdv / isp / constant:g0).
  167.  
  168. set liquidtarget to 9 / 20 * (finalmass - elementdrymass).
  169. set oxtarget to 11 / 20 * (finalmass - elementdrymass).
  170.  
  171. set liquidunits to liquidtarget / 0.005.
  172. set oxunits to oxtarget / 0.005.
  173.  
  174. //remove existing fuel amounts
  175. set liquidunits to liquidunits - currLiquid.
  176. set oxunits to oxunits - currOx.
  177.  
  178.  
  179. set lox to transferFuelAmount (srcEl, destEl, "liquidfuel", liquidunits).
  180. set ox to transferFuelAmount (srcEl, destEl, "oxidizer", oxunits).
  181.  
  182.  
  183. }
  184. function getsubtree {
  185. parameter p.
  186. parameter parts is list().
  187. for x in p:children {
  188. parts:add(x).
  189. getsubtree(x, parts).
  190. }
  191. return parts.
  192. }
  193. showresources().
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement