LUAdev

Mob destroyer

Feb 16th, 2013
3,348
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.27 KB | None | 0 0
  1. if not turtle then
  2.     printError("This is a turtle program")
  3.     return
  4. end
  5.  
  6. local w, h = term.getSize()
  7. local junk = true
  8. local junkside = "top"
  9. local lootchest = "bottom"
  10.  
  11. if fs.exists( "melee_turtle" ) then
  12.     local file = fs.open( "melee_turtle", "r" )
  13.     local content = textutils.unserialize( file.readAll() )
  14.     file.close()
  15.     junk = content.junk
  16.     junkside = content.junkside
  17.     lootchest = content.lootchest
  18. end
  19.  
  20. local popupSides = function()
  21.     term.setCursorPos( w - 14, 1 )
  22.     term.write( "+" )
  23.    
  24.     for i = 1, 6 do
  25.         term.setCursorPos( w - 14, i + 1 )
  26.         term.write( "|" )
  27.     end
  28.    
  29.     term.setCursorPos( w - 14, 8 )
  30.     term.write( "+" .. string.rep( "-", 13 ) .. "+" )
  31.    
  32.     for k, v in pairs( rs.getSides() ) do
  33.         term.setCursorPos( w - 13 , k + 1 )
  34.         term.write( "[" .. k .. "] = " .. v )
  35.     end
  36.     local event, char = nil
  37.     repeat
  38.         event, char = os.pullEvent("char")
  39.     until char:find("[1-6]")
  40.    
  41.     term.setCursorPos( w - 14, 1 )
  42.     term.write( "-" )
  43.    
  44.     for i = 1, 6 do
  45.         term.setCursorPos( w - 14, i + 1 )
  46.         term.write( string.rep( " ", 14 ) .. "|" )
  47.     end
  48.    
  49.     term.setCursorPos( w - 14, 8 )
  50.     term.write( string.rep( " ", 14 ) .. "|" )
  51.    
  52.     return char
  53. end
  54.  
  55. local screenManagement = function()
  56.     while true do
  57.         term.setCursorPos( 1, 1 )
  58.         term.write( "+" .. string.rep("-", w - 2) .. "+" )
  59.         term.setCursorPos( 1, h )
  60.         term.write( "+" .. string.rep("-", w - 2) .. "+" )
  61.  
  62.         for i = 1, h - 2 do
  63.             term.setCursorPos( 1, i + 1 )
  64.             term.write( "|" .. string.rep(" ", w - 2) .. "|" )
  65.         end
  66.        
  67.         term.setCursorPos( 2, 2 )
  68.         term.write( "[j] = " .. ( junk and "Enable junkloot" or "Disable junkloot" ) )
  69.         if junk then
  70.             term.setCursorPos( 2, 3 )
  71.             term.write( "[k] = Current junk side: " .. junkside )
  72.         end
  73.         term.setCursorPos( 2, junk and 4 or 3 )
  74.         term.write( "[c] = Current loot chest: ".. lootchest )
  75.         term.setCursorPos( 2, junk and 5 or 4 )
  76.         print( "\n[s] = Save settings" )
  77.         local _, c = os.pullEvent("char")
  78.         if c == "j" then
  79.             junk = not junk
  80.         elseif c == "k" then
  81.             junkside = rs.getSides()[tonumber(popupSides())]
  82.         elseif c == "c" then
  83.             lootchest = rs.getSides()[tonumber(popupSides())]
  84.         elseif c == "s" then
  85.             local file = fs.open( "melee_turtle", "w" )
  86.             file.write( textutils.serialize({ junk = junk; lootchest = lootchest; junkside = junkside;}))
  87.             file.close()
  88.         end
  89.     end
  90. end
  91.  
  92. local fight = function()
  93.     local turn = function( s, x )
  94.         if s == "right" then
  95.             if x then
  96.                 turtle.turnRight()
  97.             else
  98.                 turtle.turnLeft()
  99.             end
  100.         elseif s == "left" then
  101.             if x then
  102.                 turtle.turnLeft()
  103.             else
  104.                 turtle.turnRight()
  105.             end
  106.         elseif s == "back" then
  107.             turtle.turnRight()
  108.             turtle.turnRight()
  109.         end
  110.     end
  111.     local drop = function( s )
  112.         if s == "top" then
  113.             turtle.dropUp()
  114.         elseif s == "down" then
  115.             turtle.dropDown()
  116.         else
  117.             turtle.drop()
  118.         end
  119.     end
  120.     while true do
  121.         turtle.attack()
  122.         if turtle.getItemCount( 16 ) > 0 then
  123.             if junk then
  124.                 turn( junkside )
  125.                 for i = 1, 16 do
  126.                     turtle.select(i)
  127.                     if turtle.compareTo( 1 ) then
  128.                         drop( junkside )
  129.                     end
  130.                 end
  131.                 turn( junkside, true )
  132.             end
  133.             turn( lootchest )
  134.             for i = 1, 16 do
  135.                 turtle.select( i )
  136.                 drop( lootchest )
  137.             end
  138.             turn( lootchest, true )
  139.         end
  140.     end
  141. end
  142.  
  143. parallel.waitForAny( fight, screenManagement )
Advertisement
Add Comment
Please, Sign In to add comment