MegiRicky

USS Newjersey engine control

Aug 8th, 2024 (edited)
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.50 KB | None | 0 0
  1. -- Define default base channel
  2. local defaultBaseChannel = 200
  3.  
  4. -- Prompt user for base channel input
  5. print("Enter base channel (press Enter to use default: " .. defaultBaseChannel .. "):")
  6. local inputChannel = read()
  7.  
  8. -- If the user inputs a number, use it as the base channel, otherwise use the default
  9. local baseChannel = tonumber(inputChannel) or defaultBaseChannel
  10.  
  11. -- Calculate channels based on the base channel
  12. local stressChannel = baseChannel
  13. local leftEngineChannel = baseChannel + 1
  14. local rightEngineChannel = baseChannel + 2
  15. local fuelChannel = baseChannel + 3
  16. local accChannel = baseChannel + 4
  17.  
  18. local engineLeft = peripheral.wrap("left")
  19. local engineRight = peripheral.wrap("right")
  20. local fuel = peripheral.find("create_connected:item_silo")
  21. local stress = peripheral.find("Create_Stressometer")
  22. local acc = peripheral.find("modular_accumulator")
  23. local modem = peripheral.wrap("front")
  24.  
  25. if not engineLeft then
  26. error("Left rotation controller not found")
  27. elseif not engineRight then
  28. error("Right rotation controller not found")
  29. elseif not modem then
  30. error("modem not found")
  31. end
  32.  
  33. if stress then
  34. modem.open(stressChannel)
  35. else
  36. print("Warning: Stressometer not found. Skipping stress monitoring.")
  37. end
  38.  
  39. if fuel then
  40. modem.open(fuelChannel)
  41. else
  42. print("Warning: Fuel silo not found. Skipping fuel monitoring.")
  43. end
  44.  
  45. if acc then
  46. modem.open(accChannel)
  47. else
  48. print("Warning: Accumulator not found. Skipping accumulator monitoring.")
  49. end
  50.  
  51. modem.open(leftEngineChannel)
  52. modem.open(rightEngineChannel)
  53.  
  54.  
  55. local totalBlazeCakes = 0
  56. local secretKey = "YourSecretKey123"
  57. local leftSpeed = 0
  58. local rightSpeed = 0
  59.  
  60. local function isValidMessage(message)
  61. local key, msg = tostring(message):match("^(%w+)%:(.+)$")
  62. return key == secretKey, msg
  63. end
  64.  
  65. local function listenForCoordinates()
  66. while true do
  67. local event, side, senderChannel, replyChannel, message, senderDistance = os.pullEvent("modem_message")
  68. local valid, validatedMessage = isValidMessage(message)
  69. if valid then
  70. if senderChannel == leftEngineChannel then
  71. leftSpeed = tonumber(validatedMessage)
  72. elseif senderChannel == rightEngineChannel then
  73. rightSpeed = tonumber(validatedMessage)
  74. elseif senderChannel == fuelChannel and fuel then
  75. totalBlazeCakes = tonumber(validatedMessage)
  76. elseif senderChannel == stressChannel and stress then
  77. usedSU, SUCapacity = validatedMessage:match("^(%d+)%:(%d+%.?%d*)$")
  78. elseif senderChannel == accChannel and acc then
  79. accEnergy, accCapacity = validatedMessage:match("^(%d+)%:(%d+%.?%d*)$")
  80. end
  81. end
  82. end
  83. end
  84.  
  85. parallel.waitForAny(
  86. listenForCoordinates,
  87. function()
  88. while true do
  89. -- Fuel monitoring
  90. if fuel then
  91. local totalBlazeCakes = 0
  92. local total = fuel.size() * 64
  93. local items = fuel.list()
  94. for slot, itemDetail in pairs(items) do
  95. if itemDetail.name == "create:blaze_cake" then
  96. totalBlazeCakes = totalBlazeCakes + itemDetail.count
  97. end
  98. end
  99. modem.transmit(fuelChannel, 0, secretKey..":"..totalBlazeCakes..":"..total)
  100. print("Total number of blaze cakes: ", totalBlazeCakes.." / "..total)
  101. end
  102.  
  103. -- Stress monitoring
  104. if stress then
  105. local usedSU = stress.getStress()
  106. local SUCapacity = stress.getStressCapacity()
  107. modem.transmit(stressChannel, 0, secretKey..":"..usedSU..":"..SUCapacity)
  108. print("Used SU: ", usedSU)
  109. print("SU Capacity: ", SUCapacity)
  110. end
  111.  
  112. -- Accumulator monitoring
  113. if acc then
  114. local accCapacity = acc.getCapacity()
  115. local accEnergy = acc.getEnergy()
  116. modem.transmit(accChannel, 0, secretKey..":"..accEnergy..":"..accCapacity)
  117. print("Accumulator Capacity: ", accCapacity)
  118. print("Accumulator Energy: ", accEnergy)
  119. end
  120.  
  121. -- Engine control
  122. print("Left speed: ", leftSpeed)
  123. print("Right speed: ", rightSpeed)
  124. engineLeft.setTargetSpeed(leftSpeed)
  125. engineRight.setTargetSpeed(rightSpeed)
  126.  
  127. sleep(0.2)
  128. end
  129. end
  130. )
  131.  
Advertisement
Add Comment
Please, Sign In to add comment