Advertisement
MrDeeJayy

Untitled

Mar 30th, 2015
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.42 KB | None | 0 0
  1. --Changelog
  2. --  version 0.1
  3. --      First Version.
  4. --  version 0.2
  5. --      Fixed think function
  6. --      Added canThink()
  7. --      started on chest functions
  8. print("defining variables...")
  9. local storeSlots = {
  10.     livingwood = {1,2,3,4},
  11.     livingrock = {5,6,7,8}
  12. }
  13. local placeSlots = {
  14.     log = {9,10},
  15.     stone = {11,12}
  16. }
  17. local fuelSlots = {13,14,15,16}
  18. mineThese = {"livingwood","livingrock"}
  19. placeThese = {"log","stone"}
  20. fuelThese = {"minecraft:coal","minecraft:charcoal"}
  21.  
  22. local function movePreventObstruct(x)
  23.     print("movePreventObstruct(x) was called. x is ".. tostring(x))
  24.     for i = 1,x do
  25.         local hasMoved = false
  26.         while hasMoved == false do
  27.             print("Attempting to move forward...")
  28.             hasMoved = turtle.forward()
  29.             if hasMoved == false then
  30.                 print("Something got in my way! Attacking it now.")
  31.                 turtle.attack()
  32.             end
  33.         end
  34.     end
  35.     print("movePreventObstruct(x) has finished")
  36. end
  37.            
  38.  
  39.  
  40. local function refuel()
  41.     print("refuel() was called")
  42.     if type(turtle.getFuelLevel()) == "string" then return end -- fucking hell, why cant you just return -1 as an int?
  43.     while turtle.getFuelLevel() <= 9 do
  44.         print("refuel() while loop started!")
  45.         local fuelslots = {}
  46.         for k,v in pairs(fuelSlots) do --Reserve 13-16 for fuel to minimize calc time.
  47.             print("refuel() pairs loop started")
  48.             local itemDetail = turtle.getItemDetail(v)
  49.             for i = 1, #fuelThese do
  50.                 print("refuel() for i = 1 loop started")
  51.                 if itemDetail.name == fuelThese[i] then
  52.                     turtle.select(v) -- duh
  53.                     turtle.refuel(turtle.getItemCount(v)) -- DUh, count not space.
  54.                     print("I should be loading ".. turtle.getItemCount(v) .. " " .. itemDetail.name .. "  from slot " .. turtle.getSelectedSlot())
  55.                     break
  56.                 end
  57.             end
  58.         end
  59.     end
  60.     print("refuel() has finished")
  61. end
  62.  
  63. local function checkBlock()
  64.     print("checkBlock() called")
  65.     local success,block = turtle.inspectDown()
  66.     if success == true then
  67.         for i = 1, #mineThese do
  68.             if block.name == "Botania:" .. mineThese[i] then
  69.                 for k,v in pairs( storeSlots[mineThese[i]] ) do
  70.                     print("checkBlock() is performing its core task")
  71.                     turtle.select(v)
  72.                     turtle.digDown()
  73.                     --turtle.suck() only needed for chests.
  74.                 end
  75.                 for k,v in pairs( placeSlots[placeThese[i]] ) do
  76.                     print("checkBlock() is trying to place a block")
  77.                     turtle.select(v)
  78.                     local placed = nil
  79.                     if not turtle.getItemCount(v) == 0 then
  80.                         placed = turtle.placeDown()
  81.                     end
  82.                     if placed then break end
  83.                 end
  84.             end
  85.         end
  86.     end
  87.     print("checkBlock() finished")
  88. end
  89.  
  90. local function placeBlock()
  91.     local placed = false
  92.     print("placeBlock() called")
  93.     local success,block = turtle.inspectDown()
  94.     if success == false then
  95.         while placed == false do
  96.             print("No block detected below me, lets place something.")
  97.             -- We don't have any information on what block was originally there
  98.             -- so we shall math.random it.
  99.             -- Replacing seed, we do this every time because I don't trust Java as far as I can throw it...
  100.             -- and I know LUA already has a shitty random function in its math library.
  101.        
  102.             math.randomseed( tonumber(tostring(os.time()):reverse():sub(1,6)) )
  103.    
  104.             -- Waste some frames, but at least we will get a real random result.
  105.             -- I know I shouldn't care when its a range of 4 values, but when I ask for random, I expect random.
  106.             -- I also know I shouldn't expect good random from LUA emulated in Java on a Linux VPS. But screw you.     
  107.             -- And screw every cunt who thinks for a second that java is a good Langauge.
  108.             -- I mean come on, you may as well skip java and move straight onto a real (almost identical) language.
  109.  
  110.             -- Like C++.
  111.             -- Or C#
  112.             -- Or anything but JAVA mojang. I mean really. Fuck you guys.
  113.    
  114.             math.random(); math.random(); math.random() -- We do nothing with this but manipulate LUA.
  115.             -- For the actual random slot, we roll...
  116.             local slotToUse = math.random(8,12)
  117.             print("And our lucky number is " .. tostring(slotToUse) .. "!") -- hue hue aren't i fanny
  118.             turtle.select(slotToUse)
  119.             placed = turtle.placeDown() or turtle.inspectDown() -- Should return true if successful.
  120.         end
  121.     end
  122.     print("placeBlock() has finished")
  123. end
  124.  
  125.        
  126.        
  127.  
  128. local function canThink()
  129.     print("canThink() called")
  130.     if not turtle then error("This isn't a turtle!") return false end
  131.     print("canThink() finished")
  132.     return true -- todo: more
  133. end
  134. local function chestCheck()
  135.     --DEBUG PRINT
  136.     print("chestCheck() called")
  137.     local success, block = turtle.inspect()
  138.     if not success then return false end
  139.     if not block.name == "minecraft:chest" then return end
  140.     local initialSlot = turtle.getSelectedSlot()
  141.     for i=1,8 do
  142.         turtle.select(i)
  143.         turtle.drop(turtle.getItemCount(i))
  144.     end
  145.     for i=9,12 do
  146.         turtle.select(i)
  147.         turtle.suckUp()
  148.     end
  149.     turtle.turnLeft()
  150.     for i=13,16 do
  151.         turtle.select(i)
  152.         turtle.suck()
  153.     end
  154.     turtle.turnRight()
  155.     turtle.select(initialSlot)
  156.     --DEBUG PRINT
  157.     print("chestCheck() finished")
  158. end
  159. local function think()
  160.     print("think() called!")
  161.     while canThink() == true do
  162.         refuel()
  163.         checkBlock()
  164.         placeBlock()
  165.         movePreventObstruct(1)
  166.         checkBlock()
  167.         placeBlock()
  168.         movePreventObstruct(1)
  169.         checkBlock()
  170.         placeBlock()
  171.         chestCheck()
  172.         turtle.turnRight()
  173.     end
  174.     print("think() finished!")
  175. end
  176.  
  177. local function firstThink()
  178.     print("firstThink() called")
  179.     if not canThink() then return end -- the redundancy is real
  180.     think()
  181.     print("firstThink() finished")
  182. end
  183.  
  184. -- Lets start this all up.
  185. firstThink() -- therefor I am.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement