Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- .. Turtle file for Reactor-Control .. by meigrafd @ 09.07.2015
- No Fuel required coz Turtle doesnt move Up/Down/Forward/Backward.
- --]]
- ---[[ CONFIG - START ]]
- -- Modem channel to listen for commands from Computer.
- local ModemChannel = 32768 -- Broadcast channel.
- -- Chest Inventory Sides for which Items. Only valid sides: left, right, back, up
- -- NOTE: Fastest for Coolant will be "up"
- -- Only use one Chest for one Type of Item!
- -- (wireless modem is always on the left, but no problem)
- -- Put a Chest below the Turtle as Trash, where it drops all it takes out of Reactor.
- chestCoolant = "up"
- chestUran = "back"
- chestTrash = "down"
- -- turtle must be placed so this fits!
- chestReactor = "front"
- -- opposite direction from Reactor to Turtle, from the perspective of Chest to Turtle.
- -- Only valid: north, south, west, east, up, down
- -- Used for pushItem / pullItem (OpenPeripheral).
- directionReactor = "south"
- ---[[ CONFIG - END ]]
- ---[[ functions ]]
- -- http://stackoverflow.com/questions/1426954/split-string-in-lua
- function split(pString, pPattern)
- local Table = {} -- NOTE: use {n = 0} in Lua-5.0
- local fpat = "(.-)" .. pPattern
- local last_end = 1
- local s, e, cap = pString:find(fpat, 1)
- while s do
- if s ~= 1 or cap ~= "" then
- table.insert(Table, cap)
- end
- last_end = e+1
- s, e, cap = pString:find(fpat, last_end)
- end
- if last_end <= #pString then
- cap = pString:sub(last_end)
- table.insert(Table, cap)
- end
- return Table
- end
- function stringMatch(text, find)
- if string.find(string.lower(text), string.lower(find)) then
- return true
- else
- return false
- end
- end
- function getDeviceSide(deviceType)
- -- loop through all the sides
- for i,side in pairs(rs.getSides()) do
- if (peripheral.isPresent(side)) then
- -- Yup, there is something on this side
- if (peripheral.getType(side) == string.lower(deviceType)) then
- -- Yes, this is the device type we need, so return the side
- return side;
- end
- end
- end
- --nothing found, return nil
- return nil;
- end
- function getChestInventory(side)
- local List = ''
- local chestobj = peripheral.wrap(side)
- for i=0, chestobj.getSizeInventory()-1 do
- if chestobj.getStackInSlot(i) ~= nil then
- local slot = chestobj.getStackInSlot(i)
- print(slot..": "..slot.name)
- if List == '' then
- List = i..'='..slot.name
- else
- List = List..'|'..i..'='..slot.name
- end
- else
- print(slot..": empty")
- if List == '' then
- List = i..',<empty>'
- else
- List = List..'|'..i..',<empty>'
- end
- end
- end
- return List
- end
- function changeReactorState(state, reason)
- if reason ~= '' then
- msg = "Changing Reactor State to: "..state.." coz: "..reason
- else
- msg = "Changing Reactor State to: "..state
- end
- print(msg)
- if state == "off" then
- myModem.transmit(ModemChannel, ModemChannel, "STOP:"..reason)
- else
- myModem.transmit(ModemChannel, ModemChannel, "READY")
- end
- reactorState = state
- end
- function turn(side)
- if side == "left" then
- turtle.turnLeft()
- elseif side == "right" then
- turtle.turnRight()
- elseif side == "front" then
- turtle.turnRight()
- turtle.turnRight()
- elseif side == "back" then
- turtle.turnLeft()
- turtle.turnLeft()
- end
- end
- -- verify reactor in front
- function verifyReactor()
- local success, data = turtle.inspect()
- if stringMatch(data.name, "reactor") then
- return true
- else
- return false
- end
- end
- function Coolant(toSlot)
- local failed = false
- if chestCoolant == "up" then
- if not turtle.suckUp(1) then
- failed = true
- end
- elseif chestCoolant == "down" then
- if not turtle.suckDown(1) then
- failed = true
- end
- else
- turn(chestCoolant)
- if not turtle.suck(1) then
- failed = true
- end
- turn(chestReactor)
- end
- if failed == true then
- print("WARNING: can't pick up Coolant!")
- changeReactorState('off', 'Coolant')
- else
- -- verify reactor in front
- while verifyReactor() == false do
- turn('left')
- end
- --from turtle to reactor: myReactor.pullItem("<direction>",<fromSlot>,<amount>,<toSlot>)
- success = myReactor.pullItem(directionReactor,1,1,toSlot)
- -- change Reactor State back on?
- if (success > 0) and reactorState == 'off' then
- changeReactorState('on', '')
- end
- commandsexecdCount = commandsexecdCount + 1
- end
- end
- function Uran(toSlot)
- local failed = false
- if chestUran == "up" then
- if not turtle.suckUp(1) then
- failed = true
- end
- elseif chestUran == "down" then
- if not turtle.suckDown(1) then
- failed = true
- end
- else
- turn(chestUran)
- if not turtle.suck(1) then
- failed = true
- end
- turn(chestReactor)
- end
- if failed == true then
- print("WARNING: can't pick up Uran!")
- changeReactorState('off', 'Uran')
- else
- -- verify reactor in front
- while verifyReactor() == false do
- turn('left')
- end
- --from turtle to reactor: myReactor.pullItem("<direction>",<fromSlot>,<amount>,<toSlot>)
- success = myReactor.pullItem(directionReactor,1,1,toSlot)
- -- change Reactor State back on?
- if (success > 0) and reactorState == 'off' then
- changeReactorState('on', '')
- end
- commandsexecdCount = commandsexecdCount + 1
- end
- end
- function Depleted_Uran(fromSlot, replace)
- --from reactor to turtle: myReactor.pushItem("<direction>",<fromSlot>,<amount>,<toSlot>)
- myReactor.pushItem(directionReactor,fromSlot,1,1)
- if chestTrash == "down" then
- success = turtle.dropDown()
- elseif chestTrash == "up" then
- success = turtle.dropUp()
- elseif chestTrash == "left" then
- turn('left')
- success = turtle.drop()
- turn(chestReactor)
- elseif chestTrash == "right" then
- turn('right')
- success = turtle.drop()
- turn(chestReactor)
- end
- -- only drop Uran from Reactor to Trash or replace it too?
- if replace == true then
- Uran(fromSlot)
- end
- end
- -- http://pastebin.com/Chyexhq8
- -- isValid, isActive, getInventoryName, getEUOutput, getHeat, getMaxHeat
- function getReactorStatus()
- end
- --[[ Internal values ]]
- local version = 0.7
- turtle.select(1)
- commandsexecdCount=1
- w,h = term.getSize()
- local loopT=0.5
- --[[ Main program ]]
- myReactor = peripheral.wrap(chestReactor)
- term.clear()
- -- detect Modem
- print('Connecting to modem...')
- sleep(0.5)
- side = getDeviceSide("modem")
- if side == nil then
- error('Error: Could not connect to Modem!')
- else
- myModem = peripheral.wrap(side)
- myModem.open(ModemChannel)
- if myModem.isWireless() then
- print('success (wireless)')
- else
- print('success')
- end
- sleep(0.5)
- end
- sleep(3)
- term.clear()
- changeReactorState("on", '')
- --[[ sources:
- http://computercraft.info/wiki/Modem_(API)
- ab Zeile 534: http://pastebin.com/gTEBHv3D
- --]]
- -- main loop
- while true do
- -- Receive messages Event loop
- timerID = os.startTimer(loopT) -- Wait for loopT secounds.
- repeat
- local event, modemSide, senderChannel, replyChannel, message, senderDistance = os.pullEvent()
- if event == "modem_message" then
- -- split message e.g. slot:item into a table
- msgTable = split(message,"\:")
- -- Determine if first string is numeric (e.g. Slot#)..
- if tonumber(msgTable[1]) ~= nil then
- --it's a number
- local Slot = msgTable[1]
- local Item = msgTable[2]
- print('('..commandsexecdCount..') slot#'..Slot..' -> '..Item)
- if stringMatch(Item, "coolant") == true then
- Coolant(Slot)
- elseif stringMatch(Item, "uran") == true then
- Uran(Slot)
- elseif stringMatch(Item, "depleted") == true then
- Depleted_Uran(Slot, "true")
- else
- print('ERROR: received unknown Item: "'..Item..'"')
- end
- else
- --it's not a number.. any other command?
- if msgTable[1] == "getInv" then
- if msgTable[2] == "Coolant" then
- local List = getChestInventory(chestCoolant)
- myModem.transmit(ModemChannel, ModemChannel, 'CoolantInv:'..List)
- elseif msgTable[2] == "Uran" then
- local List = getChestInventory(chestUran)
- myModem.transmit(ModemChannel, ModemChannel, 'UranInv:'..List)
- elseif msgTable[2] == "Trash" then
- local List = getChestInventory(chestTrash)
- myModem.transmit(ModemChannel, ModemChannel, 'TrashInv:'..List)
- elseif msgTable[2] == "Reactor" then
- local List = getChestInventory(chestReactor)
- myModem.transmit(ModemChannel, ModemChannel, 'ReactorInv:'..List)
- end
- elseif msgTable[1] == "DropUranSlots" then
- local dropTable = split(msgTable[2], ',')
- for i=1, #dropTable do
- Depleted_Uran(dropTable[i], "false")
- end
- end
- end
- end
- -- exit repeat loop on timer event
- until (event == "timer" and modemSide == timerID)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement