Advertisement
HPWebcamAble

[CC] Turtle Tweaks

Jul 18th, 2015
1,120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.79 KB | None | 0 0
  1. -- Coded by HPWebcamAble --
  2. -- Simple API that lets ComputerCraft turtle accept sides in most functions
  3. -- See the forum post for more:
  4. -- http://www.computercraft.info/forums2/index.php?/topic/24030-turtle-tweaks-api/
  5.  
  6. if not turtle then error("Can only load on turtles") end
  7.  
  8. local sideConversion = {
  9.   ["right"] = "Right",
  10.   ["left"] = "Left",
  11.   ["top"] = "Up",
  12.   ["bottom"] = "Down",
  13.   ["back"] = "back",
  14.   ["front"] = ""
  15. }
  16. local directionConversions = {
  17.   top = "up", bottom = "down", right = "turnRight",
  18.   left = "turnLeft", back = "back", front = "forward"
  19. }
  20.  
  21. local oldFuncs = {}
  22. local toReplace = {
  23.   ["detect"] = true,
  24.   ["inspect"] = true,
  25.   ["place"] = true,
  26.   ["suck"] = true,
  27.   ["compare"] = true,
  28.   ["attack"] = true,
  29.   ["dig"] = true,
  30.   ["drop"] = true
  31. }
  32. local directionsToReplace = {"up","down","turnRight","turnLeft","forward","back"}
  33.  
  34. for a,b in pairs(turtle) do
  35.   if toReplace[a] then
  36.     oldFuncs[a] = b
  37.     _G["turtle"][a]  = function(side)
  38.       if not side then return oldFuncs[a]() end
  39.       if _G["turtle"][a..tostring(sideConversion[side])] then
  40.         return _G["turtle"][a..sideConversion[side]]()
  41.       else
  42.         error(side.." is not a valid side",2)
  43.       end
  44.     end
  45.   end
  46. end
  47.  
  48. turtle.move = function(sideOrDirection)
  49.   if _G["turtle"][directionConversions[sideOrDirection]] then
  50.     return _G["turtle"][directionConversions[sideOrDirection]]()
  51.   elseif _G["turtle"][sideOrDirection] then
  52.     return _G["turtle"][sideOrDirection]()
  53.   else
  54.     error("Not a valid side or direction",2)
  55.   end
  56. end
  57.  
  58. turtle.equip = function(side)
  59.   side = string.lower(tostring(side))
  60.   if side == "left" then return turtle.equipLeft()
  61.   elseif side == "right" then return turtle.equipRight()
  62.   else error(tostring(side).." is not a valid side",2) end
  63. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement