MegiRicky

EMBTLoader

Mar 18th, 2025
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.90 KB | None | 0 0
  1. -- Define the peripheral devices
  2. local barrel = peripheral.find("minecraft:barrel") -- Find the barrel
  3. local deployer = peripheral.find("create:deployer") -- Find the deployer
  4. local unscrewMotor = peripheral.wrap("back")
  5. local screwRemoveMotor = peripheral.wrap("front")
  6. local modem = peripheral.wrap("right")
  7.  
  8. local loadingMotor
  9. local nilCount = 0
  10. local i = 0
  11.  
  12.  
  13.  
  14. -- Loop to find loading motor through modem (searching "electric_motor_X")d
  15. while nilCount < 200 do
  16. local motorName = "electric_motor_" .. tostring(i)
  17. loadingMotor = peripheral.wrap(motorName)
  18.  
  19. if loadingMotor then
  20. print("Found loading motor:", motorName)
  21. break
  22. else
  23. nilCount = nilCount + 1
  24. end
  25. i = i + 1
  26. end
  27.  
  28. unscrewMotor.setSpeed(0)
  29. loadingMotor.setSpeed(0)
  30.  
  31. local function askUser(prompt, defaultValue)
  32. print(prompt .. " (default: " .. defaultValue .. ")")
  33. local input = io.read()
  34. if input == "" then
  35. return defaultValue
  36. else
  37. return input
  38. end
  39. end
  40.  
  41. EMBTControlChannel = tonumber(askUser("Input the EMBT channel","2000"))
  42. reloadChannel = EMBTControlChannel + 20
  43. modem.open(reloadChannel)
  44. modem.open(EMBTControlChannel)
  45. local reloadMessage_recived = {}
  46. local reloadMessage_send = {}
  47.  
  48. -- Finds an item in the barrel that exactly matches the given display name.
  49. local function findItemByDisplayName(displayName)
  50. -- get a quick list of everything in the barrel
  51. local items = barrel.list()
  52. for slot, data in pairs(items) do
  53. -- get full detail for the item in this slot
  54. local detail = barrel.getItemDetail(slot)
  55. if detail and detail.displayName == displayName then
  56. -- If matched, return slot and quantity
  57. return true, slot, detail.count
  58. end
  59. end
  60. return false
  61. end
  62.  
  63. local function pullItemByDisplayName(displayName)
  64. local found, slot, count = findItemByDisplayName(displayName)
  65. if found then
  66. print("Found item '" .. displayName .. "' in slot ".. slot .." (count = "..count..")")
  67. deployer.pullItems(peripheral.getName(barrel), slot, 1)
  68. print("Pulled 1x '" .. displayName .. "' into deployer.")
  69. else
  70. print("No item named '" .. displayName .. "' in the barrel.")
  71. end
  72. end
  73.  
  74. local function hasItem(itemName)
  75. local items = barrel.list() -- Get the list of items in the barrel
  76. for slot, data in pairs(items) do
  77. print(data.name)
  78. if data.name == itemName then
  79. return true, slot, data.count -- If we find the item, return true, its slot number, and item count
  80. end
  81. end
  82. return false -- No item found
  83. end
  84.  
  85. -- Function to pull the item from the barrel to the deployer
  86. local function pullItem(itemName)
  87. local success, slot, count = hasItem(itemName)
  88. if success then
  89. print("Found " .. itemName .. " in slot " .. slot .. " with quantity " .. count)
  90. -- Pull the item from the barrel and place it in the deployer’s inventory
  91. deployer.pullItems(peripheral.getName(barrel), slot, 1) -- Pull 1 item from the specified slot
  92. print(itemName .. " successfully transferred to the deployer!")
  93. else
  94. print("Item not found in the barrel.")
  95. end
  96. end
  97.  
  98. local function main()
  99. while true do
  100. if reloadMessage_recived and reloadMessage_recived.startReload then
  101. reloadMessage_send = {reloaded = false}
  102. reloadMessage_recived.startReload = false
  103. modem.transmit(reloadChannel,reloadChannel,reloadMessage_send)
  104. local startTime = os.clock() -- Start the timer
  105.  
  106. -- Unscrew
  107. unscrewMotor.setSpeed(256)
  108. sleep(0.2)
  109. unscrewMotor.setSpeed(0)
  110.  
  111. -- Remove screw
  112. screwRemoveMotor.rotate(35,1)
  113. sleep(0.1)
  114.  
  115. -- Add shell
  116. if controls and controls.shellMode == "APHE_Proximity" then
  117. pullItemByDisplayName("APHE_Proximity")
  118. elseif controls and controls.shellMode == "APHE_Delayed" then
  119. pullItemByDisplayName("APHE_Delayed")
  120. end
  121. sleep(0.25)
  122.  
  123. -- Load shell
  124. loadingMotor.setSpeed(-256)
  125. sleep(0.35)
  126. loadingMotor.setSpeed(256)
  127. sleep(0.2)
  128.  
  129. -- Load powder charge 8 times
  130. for i = 1, 8 do
  131. print("Loading powder charge " .. i .. "...")
  132. pullItem("createbigcannons:powder_charge")
  133. sleep(0.45)
  134.  
  135. -- Load powder charge
  136. loadingMotor.setSpeed(-256)
  137. sleep(0.35)
  138. loadingMotor.setSpeed(256)
  139. sleep(0.05)
  140. end
  141.  
  142. -- Place screw
  143. sleep(0.35)
  144. loadingMotor.setSpeed(0)
  145. screwRemoveMotor.rotate(25,-1)
  146. sleep(0.5)
  147.  
  148. -- Screw it tight
  149. unscrewMotor.setSpeed(-256)
  150. sleep(0.3)
  151. unscrewMotor.setSpeed(0)
  152. local endTime = os.clock() -- End the timer
  153. local reloadTime = endTime - startTime -- Calculate elapsed time
  154.  
  155. print(string.format("Reload completed in %.2f seconds.", reloadTime))
  156.  
  157. reloadMessage_send = {reloaded = true}
  158. modem.transmit(reloadChannel,reloadChannel,reloadMessage_send)
  159. end
  160. sleep() -- Reduce CPU usage
  161. end
  162. end
  163.  
  164. local function modemMessage()
  165. while true do
  166. if modem then
  167. local event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message")
  168. if channel == reloadChannel then
  169. reloadMessage_recived = message
  170. elseif channel == EMBTControlChannel then
  171. controls = message
  172. end
  173. else
  174. sleep()
  175. end
  176. end
  177. end
  178.  
  179. parallel.waitForAny(
  180. main,
  181. modemMessage
  182. )
Advertisement
Add Comment
Please, Sign In to add comment