Advertisement
Guest User

startup

a guest
Oct 21st, 2014
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.47 KB | None | 0 0
  1. function refuel()
  2.   count = turtle.getItemCount(16)
  3.   print("Got "..tostring(count).." coal blocks")
  4.   if count < 64 then
  5.     toGet = 64 - count
  6.     print("Getting "..tostring(toGet).." coal blocks")
  7.     turtle.select(16)
  8.     turtle.suck(toGet)
  9.   end
  10.   if turtle.getFuelLevel() < 1000 then
  11.     print("Low on fuel --> refueling")
  12.     turtle.select(16)
  13.     turtle.refuel(64)
  14.     refuel()
  15.   end
  16. end
  17.  
  18. refuel()
  19. print("Initial refil finished")
  20. rednet.open("left")
  21. h = fs.open("inventory","r")
  22. inventory = textutils.unserialize(h.readAll())
  23. h.close()
  24.  
  25. inventoryState = {}
  26. for i=1,15 do
  27.   inventoryState[i] = (turtle.getItemCount(i) > 0)
  28. end
  29.  
  30. while true do
  31.   event,id,msg = os.pullEvent()
  32.   if event == "turtle_inventory" then
  33.     for i=1,15 do
  34.       if (turtle.getItemCount(i) > 0) ~= inventoryState[i] then
  35.         inventoryState[i] = not inventoryState[i]
  36.         newDestination = i
  37.       end
  38.     end
  39.     rednet.send(5,"received","nexus_portal")
  40.     id,msg=rednet.receive("nexus_portal")
  41.     inventory[newDestination] = msg
  42.     fs.delete("inventory")
  43.     h = fs.open("inventory","w")
  44.     h.write(textutils.serialize(inventory))
  45.     h.close()
  46.   elseif event == "rednet_message" and msg ~= "drawbridges" then
  47.     msg = textutils.unserialize(msg)
  48.     print("Looking for "..msg[2])
  49.     local i = 1
  50.     while inventory[i] ~= msg[2] and i < 16 do
  51.       print("Not in slot "..tostring(i))
  52.       i = i + 1
  53.     end
  54.     if i < 16 then
  55.       selection = i
  56.       print(msg[2].." is in slot "..tostring(selection))
  57.       if msg[1] == "select" then
  58.         refuel()
  59.         print("Selecting "..msg[2].." in slot "..tostring(selection))
  60.         turtle.select(selection)
  61.         turtle.back()
  62.         while turtle.up() do break end
  63.         turtle.forward()
  64.         turtle.dropUp()
  65.         rednet.broadcast("drawbridges","nexus_portal")
  66.         sleep(5)
  67.         rednet.broadcast("drawbridges","nexus_portal")
  68.         turtle.suckUp()
  69.         turtle.back()
  70.         while turtle.detect() do
  71.           turtle.down()
  72.         end
  73.         turtle.forward()
  74.       elseif msg[1] == "remove" then
  75.         print("Removing "..msg[2].." in slot "..tostring(selection))
  76.         turtle.select(selection)
  77.         turtle.turnRight()
  78.         turtle.drop()
  79.         turtle.turnLeft()
  80.         table.remove(inventory,selection)
  81.         fs.delete("inventory")
  82.         h = fs.open("inventory","w")
  83.         h.write(textutils.serialize(inventory))
  84.         h.close()
  85.       end
  86.     end
  87.   end
  88. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement