Advertisement
meigrafd

Turtle Reactor Client (0.7)

Jul 9th, 2015
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.53 KB | None | 0 0
  1. --[[
  2.  
  3. .. Turtle file for Reactor-Control .. by meigrafd @ 09.07.2015
  4.  
  5.  No Fuel required coz Turtle doesnt move Up/Down/Forward/Backward.
  6.  
  7. --]]
  8.  
  9.  
  10. ---[[ CONFIG - START ]]
  11.  
  12.  
  13. -- Modem channel to listen for commands from Computer.
  14. local ModemChannel = 32768 -- Broadcast channel.
  15.  
  16. -- Chest Inventory Sides for which Items. Only valid sides: left, right, back, up
  17. -- NOTE: Fastest for Coolant will be "up"
  18. -- Only use one Chest for one Type of Item!
  19. -- (wireless modem is always on the left, but no problem)
  20. -- Put a Chest below the Turtle as Trash, where it drops all it takes out of Reactor.
  21. chestCoolant = "up"
  22. chestUran = "back"
  23. chestTrash = "down"
  24.  
  25. -- turtle must be placed so this fits!
  26. chestReactor = "front"
  27.  
  28. -- opposite direction from Reactor to Turtle, from the perspective of Chest to Turtle.
  29. -- Only valid: north, south, west, east, up, down
  30. -- Used for pushItem / pullItem (OpenPeripheral).
  31. directionReactor = "south"
  32.  
  33.  
  34. ---[[ CONFIG - END ]]
  35.  
  36.  
  37. ---[[ functions ]]
  38.  
  39.  
  40. -- http://stackoverflow.com/questions/1426954/split-string-in-lua
  41. function split(pString, pPattern)
  42.     local Table = {}  -- NOTE: use {n = 0} in Lua-5.0
  43.     local fpat = "(.-)" .. pPattern
  44.     local last_end = 1
  45.     local s, e, cap = pString:find(fpat, 1)
  46.     while s do
  47.         if s ~= 1 or cap ~= "" then
  48.             table.insert(Table, cap)
  49.         end
  50.         last_end = e+1
  51.         s, e, cap = pString:find(fpat, last_end)
  52.     end
  53.     if last_end <= #pString then
  54.         cap = pString:sub(last_end)
  55.         table.insert(Table, cap)
  56.     end
  57.     return Table
  58. end
  59.  
  60. function stringMatch(text, find)
  61.     if string.find(string.lower(text), string.lower(find)) then
  62.         return true
  63.     else
  64.         return false
  65.     end
  66. end
  67.  
  68. function getDeviceSide(deviceType)
  69.     -- loop through all the sides
  70.     for i,side in pairs(rs.getSides()) do
  71.         if (peripheral.isPresent(side)) then
  72.             -- Yup, there is something on this side
  73.             if (peripheral.getType(side) == string.lower(deviceType)) then
  74.                 -- Yes, this is the device type we need, so return the side
  75.                 return side;
  76.             end
  77.         end
  78.     end
  79.     --nothing found, return nil
  80.     return nil;
  81. end
  82.  
  83. function getChestInventory(side)
  84.     local List = ''
  85.     local chestobj = peripheral.wrap(side)
  86.     for i=0, chestobj.getSizeInventory()-1 do
  87.         if chestobj.getStackInSlot(i) ~= nil then
  88.             local slot = chestobj.getStackInSlot(i)
  89.             print(slot..": "..slot.name)
  90.             if List == '' then
  91.                 List = i..'='..slot.name
  92.             else
  93.                 List = List..'|'..i..'='..slot.name
  94.             end
  95.         else
  96.             print(slot..": empty")
  97.             if List == '' then
  98.                 List = i..',<empty>'
  99.             else
  100.                 List = List..'|'..i..',<empty>'
  101.             end
  102.         end
  103.     end
  104.     return List
  105. end
  106.  
  107. function changeReactorState(state, reason)
  108.     if reason ~= '' then
  109.         msg = "Changing Reactor State to: "..state.." coz: "..reason
  110.     else
  111.         msg = "Changing Reactor State to: "..state
  112.     end
  113.     print(msg)
  114.     if state == "off" then
  115.         myModem.transmit(ModemChannel, ModemChannel, "STOP:"..reason)
  116.     else
  117.         myModem.transmit(ModemChannel, ModemChannel, "READY")
  118.     end
  119.     reactorState = state
  120. end
  121.  
  122. function turn(side)
  123.     if side == "left" then
  124.         turtle.turnLeft()
  125.     elseif side == "right" then
  126.         turtle.turnRight()
  127.     elseif side == "front" then
  128.         turtle.turnRight()
  129.         turtle.turnRight()
  130.     elseif side == "back" then
  131.         turtle.turnLeft()
  132.         turtle.turnLeft()
  133.     end
  134. end
  135.  
  136. -- verify reactor in front
  137. function verifyReactor()
  138.     local success, data = turtle.inspect()
  139.     if stringMatch(data.name, "reactor") then
  140.         return true
  141.     else
  142.         return false
  143.     end
  144. end
  145.  
  146. function Coolant(toSlot)
  147.     local failed = false
  148.     if chestCoolant == "up" then
  149.         if not turtle.suckUp(1) then
  150.             failed = true
  151.         end
  152.     elseif chestCoolant == "down" then
  153.         if not turtle.suckDown(1) then
  154.             failed = true
  155.         end
  156.     else
  157.         turn(chestCoolant)
  158.         if not turtle.suck(1) then
  159.             failed = true
  160.         end
  161.         turn(chestReactor)
  162.     end
  163.     if failed == true then
  164.         print("WARNING: can't pick up Coolant!")
  165.         changeReactorState('off', 'Coolant')
  166.     else
  167.         -- verify reactor in front
  168.         while verifyReactor() == false do
  169.             turn('left')
  170.         end
  171.         --from turtle to reactor: myReactor.pullItem("<direction>",<fromSlot>,<amount>,<toSlot>)
  172.         success = myReactor.pullItem(directionReactor,1,1,toSlot)
  173.         -- change Reactor State back on?
  174.         if (success > 0) and reactorState == 'off' then
  175.             changeReactorState('on', '')
  176.         end
  177.         commandsexecdCount = commandsexecdCount + 1
  178.     end
  179. end
  180.  
  181. function Uran(toSlot)
  182.     local failed = false
  183.     if chestUran == "up" then
  184.         if not turtle.suckUp(1) then
  185.             failed = true
  186.         end
  187.     elseif chestUran == "down" then
  188.         if not turtle.suckDown(1) then
  189.             failed = true
  190.         end
  191.     else
  192.         turn(chestUran)
  193.         if not turtle.suck(1) then
  194.             failed = true
  195.         end
  196.         turn(chestReactor)
  197.     end
  198.     if failed == true then
  199.         print("WARNING: can't pick up Uran!")
  200.         changeReactorState('off', 'Uran')
  201.     else
  202.         -- verify reactor in front
  203.         while verifyReactor() == false do
  204.             turn('left')
  205.         end
  206.         --from turtle to reactor: myReactor.pullItem("<direction>",<fromSlot>,<amount>,<toSlot>)
  207.         success = myReactor.pullItem(directionReactor,1,1,toSlot)
  208.         -- change Reactor State back on?
  209.         if (success > 0) and reactorState == 'off' then
  210.             changeReactorState('on', '')
  211.         end
  212.         commandsexecdCount = commandsexecdCount + 1
  213.     end
  214. end
  215.  
  216. function Depleted_Uran(fromSlot, replace)
  217.     --from reactor to turtle: myReactor.pushItem("<direction>",<fromSlot>,<amount>,<toSlot>)
  218.     myReactor.pushItem(directionReactor,fromSlot,1,1)
  219.     if chestTrash == "down" then
  220.         success = turtle.dropDown()
  221.     elseif chestTrash == "up" then
  222.         success = turtle.dropUp()
  223.     elseif chestTrash == "left" then
  224.         turn('left')
  225.         success = turtle.drop()
  226.         turn(chestReactor)
  227.     elseif chestTrash == "right" then
  228.         turn('right')
  229.         success = turtle.drop()
  230.         turn(chestReactor)
  231.     end
  232.     -- only drop Uran from Reactor to Trash or replace it too?
  233.     if replace == true then
  234.         Uran(fromSlot)
  235.     end
  236. end
  237.  
  238. -- http://pastebin.com/Chyexhq8
  239. -- isValid, isActive, getInventoryName, getEUOutput, getHeat, getMaxHeat
  240. function getReactorStatus()
  241.  
  242.  
  243. end
  244.  
  245.  
  246. --[[ Internal values ]]
  247.  
  248.  
  249. local version = 0.7
  250. turtle.select(1)
  251. commandsexecdCount=1
  252. w,h = term.getSize()
  253. local loopT=0.5
  254.  
  255.  
  256. --[[ Main program ]]
  257.  
  258.  
  259. myReactor = peripheral.wrap(chestReactor)
  260. term.clear()
  261. -- detect Modem
  262. print('Connecting to modem...')
  263. sleep(0.5)
  264. side = getDeviceSide("modem")
  265. if side == nil then
  266.     error('Error: Could not connect to Modem!')
  267. else
  268.     myModem = peripheral.wrap(side)
  269.     myModem.open(ModemChannel)
  270.     if myModem.isWireless() then
  271.         print('success  (wireless)')
  272.     else
  273.         print('success')
  274.     end
  275.     sleep(0.5)
  276. end
  277. sleep(3)
  278. term.clear()
  279.  
  280. changeReactorState("on", '')
  281.  
  282.  
  283. --[[ sources:
  284.  http://computercraft.info/wiki/Modem_(API)
  285.  ab Zeile 534: http://pastebin.com/gTEBHv3D
  286. --]]
  287.  
  288. -- main loop
  289. while true do
  290.     -- Receive messages Event loop
  291.     timerID = os.startTimer(loopT) -- Wait for loopT secounds.
  292.     repeat
  293.         local event, modemSide, senderChannel, replyChannel, message, senderDistance = os.pullEvent()
  294.         if event == "modem_message" then
  295.             -- split message e.g. slot:item into a table
  296.             msgTable = split(message,"\:")
  297.  
  298.             -- Determine if first string is numeric (e.g. Slot#)..
  299.             if tonumber(msgTable[1]) ~= nil then
  300.                 --it's a number
  301.                 local Slot = msgTable[1]
  302.                 local Item = msgTable[2]
  303.                 print('('..commandsexecdCount..') slot#'..Slot..' -> '..Item)
  304.                 if stringMatch(Item, "coolant") == true then
  305.                     Coolant(Slot)
  306.                 elseif stringMatch(Item, "uran") == true then
  307.                     Uran(Slot)
  308.                 elseif stringMatch(Item, "depleted") == true then
  309.                     Depleted_Uran(Slot, "true")
  310.                 else
  311.                     print('ERROR: received unknown Item: "'..Item..'"')
  312.                 end
  313.  
  314.             else
  315.                 --it's not a number.. any other command?
  316.                 if msgTable[1] == "getInv" then
  317.                     if msgTable[2] == "Coolant" then
  318.                         local List = getChestInventory(chestCoolant)
  319.                         myModem.transmit(ModemChannel, ModemChannel, 'CoolantInv:'..List)
  320.                     elseif msgTable[2] == "Uran" then
  321.                         local List = getChestInventory(chestUran)
  322.                         myModem.transmit(ModemChannel, ModemChannel, 'UranInv:'..List)
  323.                     elseif msgTable[2] == "Trash" then
  324.                         local List = getChestInventory(chestTrash)
  325.                         myModem.transmit(ModemChannel, ModemChannel, 'TrashInv:'..List)
  326.                     elseif msgTable[2] == "Reactor" then
  327.                         local List = getChestInventory(chestReactor)
  328.                         myModem.transmit(ModemChannel, ModemChannel, 'ReactorInv:'..List)
  329.                     end
  330.                 elseif msgTable[1] == "DropUranSlots" then
  331.                     local dropTable = split(msgTable[2], ',')
  332.                     for i=1, #dropTable do
  333.                         Depleted_Uran(dropTable[i], "false")
  334.                     end
  335.                 end
  336.             end
  337.  
  338.         end
  339.     -- exit repeat loop on timer event
  340.     until (event == "timer" and modemSide == timerID)
  341. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement