Advertisement
Espen

Turtle Events API

Mar 20th, 2012
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.61 KB | None | 0 0
  1.  
  2. -- Local aggregate-function for every turtle action except 'drop'.
  3. local function executeOperation( sOperation, ... )
  4.   local bSuccess = turtle[ sOperation ]( arg[1] )  -- As of CC 1.31, arg[1] is for 'slot' in turtle.select( slot )
  5.   os.queueEvent( "turtle", sOperation, bSuccess, arg[1] )
  6.   return bSuccess  -- Returning the success is sufficient for direct function calls.
  7. end
  8.  
  9. -- Moving
  10. function forward() return executeOperation( "forward" ) end
  11. function back() return executeOperation( "back" ) end
  12. function up() return executeOperation( "up" ) end
  13. function down() return executeOperation( "down" ) end
  14. function turnLeft() return executeOperation( "turnLeft" ) end
  15. function turnRight() return executeOperation( "turnRight" ) end
  16.  
  17. -- Placing
  18. function placeUp() return executeOperation( "placeUp" ) end
  19. function place() return executeOperation( "place" ) end
  20. function placeDown() return executeOperation( "placeDown" ) end
  21.  
  22. -- Detecting
  23. function detectUp() return executeOperation( "detectUp" ) end
  24. function detect() return executeOperation( "detect" ) end
  25. function detectDown() return executeOperation( "detectDown" ) end
  26.  
  27. -- Digging
  28. function digUp() return executeOperation( "digUp" ) end
  29. function dig() return executeOperation( "dig" ) end
  30. function digDown() return executeOperation( "digDown" ) end
  31.  
  32. -- Comparing
  33. function compareUp() return executeOperation( "compareUp" ) end
  34. function compare() return executeOperation( "compare" ) end
  35. function compareDown() return executeOperation( "compareDown" ) end
  36.  
  37. -- Slot Selecting
  38. function select( nSlot ) return executeOperation( "select", nSlot ) end
  39.  
  40. -- Dropping
  41. function drop( quantity )
  42.   local bSuccess
  43.   local nSlot = 1  -- Init not necessary in this case, but just to be safe.
  44.   local nAmountDropped = 0  -- Init not necessary in this case, but just to be safe.
  45.   local tOldItemSpace = {}
  46.   -- Get current slot-space amount for all slots.
  47.   for i = 1, 9 do
  48.     tOldItemSpace[i] = turtle.getItemSpace(i)
  49.   end
  50.   -- Execute drop.
  51.   if quantity then
  52.     bSuccess = turtle.drop( quantity )
  53.   else
  54.     bSuccess = turtle.drop()
  55.   end
  56.   -- Check which slot dropped the items and how many were dropped.
  57.   for i = 1, 9 do
  58.     nSlot = i
  59.     nAmountDropped = turtle.getItemSpace(i) - tOldItemSpace[i]
  60.     if nAmountDropped > 0 then break end
  61.   end
  62.   -- Don't queue these parameters if the drop was unsuccessful.
  63.   if not bSuccess then
  64.     nSlot = nil
  65.     nAmountDropped = nil
  66.   end
  67.   -- Queue event with the previously assembled information.
  68.   os.queueEvent( "turtle", "drop", bSuccess, nSlot, nAmountDropped )
  69.   return bSuccess, nSlot, nAmountDropped
  70. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement