Advertisement
Guest User

ComputerCraftLib.lua

a guest
Nov 22nd, 2014
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.85 KB | None | 0 0
  1. ---------------------------------------------------------------------------------------------------------------
  2. -- Generic LUA library for ComputerCraft
  3. ---------------------------------------------------------------------------------------------------------------
  4. -- Written by Shikifuyin
  5. ---------------------------------------------------------------------------------------------------------------
  6.  
  7. ---------------------------------------------------------------------------------------------------------------
  8. -- Includes
  9.  
  10. ---------------------------------------------------------------------------------------------------------------
  11. -- Global definitions
  12.  
  13. ---------------------------------------------------------------------------------------------------------------
  14. -- File I/O
  15. File = {
  16.         -- Constants
  17.    
  18.         -- Members
  19.    
  20.         -- Internal members/methods
  21.        
  22.         -- Methods
  23.     Exists = function( strName )
  24.         return fs.exists( strName )
  25.     end,
  26.        
  27.     LoadVar = function( strName )
  28.         hFile = fs.open( strName, "r" )
  29.         hValue = hFile.read()
  30.         hFile.close()
  31.         return hValue
  32.     end,
  33.     SaveVar = function( strName, hValue )
  34.         hFile = fs.open( strName, "w" )
  35.         hFile.write( hValue )
  36.         hFile.close()
  37.     end
  38. }
  39.  
  40. ---------------------------------------------------------------------------------------------------------------
  41. -- Turtles
  42. TurtleInterfaces = {
  43.         -- Methods
  44.     -- GetModem : Retrieve face with modem interface
  45.     GetModem = function()
  46.         for X, strFacet in pairs(rs.getSides()) do
  47.             if ( peripheral.getType(strFacet) == "modem" ) then
  48.                 return strFacet
  49.             end
  50.         end
  51.         return nil
  52.     end
  53. }
  54.  
  55. TurtleController = {
  56.         -- Constants
  57.     MOVE_XYZ = 0,
  58.     MOVE_XZY = 1,
  59.     MOVE_YXZ = 2,
  60.     MOVE_YZX = 3,
  61.     MOVE_ZXY = 4,
  62.     MOVE_ZYX = 5,
  63.  
  64.     ORIENTATION_NORTH = 0,
  65.     ORIENTATION_EAST  = 1,
  66.     ORIENTATION_SOUTH = 2,
  67.     ORIENTATION_WEST  = 3,
  68.    
  69.         -- Members
  70.     Position = { X = 0, Y = 0, Z = 0 }, -- Position relative to last set origin
  71.     Orientation = ORIENTATION_NORTH,    -- Orientation relative to last set origin
  72.    
  73.         -- Internal members/methods
  74.     _Internal = {
  75.         _Move_X = function( iDX, iOrientation )
  76.             local iAmountX = abs( iDX )
  77.             if ( iDX < 0 ) then -- Move backward
  78.                 turtle.turnRight()
  79.                 turtle.turnRight()
  80.                 iOrientation = iOrientation + 2
  81.                 while ( iOrientation > 3 ) do iOrientation = iOrientation - 4 end
  82.                 for i = 1, iAmountX do turtle.forward() end
  83.             elseif ( iDX > 0 ) then -- Move forward
  84.                 for i = 1, iAmountX do turtle.forward() end
  85.             end
  86.             return iOrientation
  87.         end,
  88.         _Move_Y = function( iDY, iOrientation )
  89.             local iAmountY = abs( iDY )
  90.             if ( iDY < 0 ) then -- Move left
  91.                 turtle.turnLeft()
  92.                 iOrientation = iOrientation - 1
  93.                 while ( iOrientation < 0 ) do iOrientation = iOrientation + 4 end
  94.                 for i = 1, iAmountY do turtle.forward() end
  95.             elseif ( iDY > 0 ) then -- Move right
  96.                 turtle.turnRight()
  97.                 iOrientation = iOrientation + 1
  98.                 while ( iOrientation > 3 ) do iOrientation = iOrientation - 4 end
  99.                 for i = 1, iAmountY do turtle.forward() end
  100.             end
  101.             return iOrientation
  102.         end,
  103.         _Move_Z = function( iDZ )
  104.             local iAmountZ = abs( iDZ )
  105.             if ( iDZ < 0 ) then -- Move down
  106.                 for i = 1, iAmountZ do turtle.down() end
  107.             elseif ( iDZ > 0 ) then -- Move up
  108.                 for i = 1, iAmountZ do turtle.up() end
  109.             end
  110.         end
  111.     },
  112.    
  113.         -- Methods
  114.     -- Constructor
  115.     New = function( this, hObject )
  116.         hObject = hObject or {}
  117.         setmetatable( hObject, this )
  118.         this.__index = this
  119.         return hObject
  120.     end,
  121.  
  122.     -- Refuel : Search inventory for fuel and use it if needed
  123.     Refuel = function( this )
  124.         for i = 1, 16 do
  125.             if ( turtle.getFuelLevel() >= 1 ) then
  126.                 return
  127.             end    
  128.             turtle.select( i )
  129.             turtle.refuel()
  130.             turtle.select( 1 )
  131.         end
  132.     end,
  133.    
  134.     -- SetOrigin : Sets current turtle's position & orientation as absolute origin
  135.     SetOrigin = function( this )
  136.         this.Position.X = 0
  137.         this.Position.Y = 0
  138.         this.Position.Z = 0
  139.         this.Orientation = ORIENTATION_NORTH
  140.     end,
  141.  
  142.     -- Turn : Rotates the turtle along Z axis
  143.     -- iOrientation = Target orientation relative to current orientation (relative, default) or tracked orientation (absolute)
  144.     -- bAbsolute    = Use origin-tracking system to specify absolute orientation else use relative orientation (default)
  145.     Turn = function( this, iOrientation, bAbsolute )
  146.         -- Default parameters
  147.         bAbsolute = bAbsolute or false
  148.        
  149.         if ( bAbsolute ) then
  150.             -- Clamp orientation value
  151.             while ( iOrientation < 0 ) do iOrientation = iOrientation + 4 end
  152.             while ( iOrientation > 3 ) do iOrientation = iOrientation - 4 end
  153.            
  154.             -- Absolute rotation case
  155.             while ( this.Orientation > iOrientation ) do
  156.                 turtle.turnLeft()
  157.                 this.Orientation = this.Orientation - 1
  158.             end
  159.             while ( this.Orientation < iOrientation ) do
  160.                 turtle.turnRight()
  161.                 this.Orientation = this.Orientation + 1
  162.             end
  163.         else
  164.             -- Clamp orientation value
  165.             while ( iOrientation < -3 ) do iOrientation = iOrientation + 4 end
  166.             while ( iOrientation > 3 ) do iOrientation = iOrientation - 4 end
  167.            
  168.             -- Relative rotation case
  169.             while ( iOrientation < 0 ) do
  170.                 turtle.turnLeft()
  171.                 this.Orientation = this.Orientation - 1
  172.                 iOrientation = iOrientation + 1
  173.             end
  174.             while ( iOrientation > 0 ) do
  175.                 turtle.turnRight()
  176.                 this.Orientation = this.Orientation + 1
  177.                 iOrientation = iOrientation - 1
  178.             end
  179.            
  180.             -- Clamp final value
  181.             while ( this.Orientation < 0 ) do this.Orientation = this.Orientation + 4 end
  182.             while ( this.Orientation > 3 ) do this.Orientation = this.Orientation - 4 end
  183.         end
  184.     end,
  185.    
  186.     -- Move : Moves the turtle.
  187.     -- iX, iY, iZ       = Target position relative to current position (relative, default) or tracked origin (absolute)
  188.     -- iAxisOrdering    = Use one of the above values to specify movement ordering (default = XYZ)
  189.     -- bKeepOrientation = Restore pre-move orientation (no momentum) else use post-move orientation (keep momentum, default)
  190.     -- bAbsolute        = Use origin-tracking system to specify absolute coordinates else use relative coordinates (default)
  191.     Move = function( this, iX, iY, iZ, iAxisOrdering, bKeepOrientation, bAbsolute )
  192.         -- Default parameters
  193.         iAxisOrdering = iAxisOrdering or this.MOVE_XYZ
  194.         bKeepOrientation = bKeepOrientation or false
  195.         bAbsolute = bAbsolute or false
  196.        
  197.         -- Save initial orientation
  198.         local iInitialOrientation = this.Orientation
  199.        
  200.         -- Absolute movement case
  201.         if ( bAbsolute ) then
  202.             iX = ( iX - this.Position.X )
  203.             iY = ( iY - this.Position.Y )
  204.             iZ = ( iZ - this.Position.Z )
  205.         end
  206.        
  207.         -- Switch with axis ordering
  208.         if ( iAxisOrdering == this.MOVE_XYZ ) then
  209.             this.Orientation = this._Internal._Move_X( iX, this.Orientation )
  210.             this.Orientation = this._Internal._Move_Y( iY, this.Orientation )
  211.             this._Internal._Move_Z( iZ )
  212.         elseif ( iAxisOrdering == this.MOVE_XZY ) then
  213.             this.Orientation = this._Internal._Move_X( iX, this.Orientation )
  214.             this._Internal._Move_Z( iZ )
  215.             this.Orientation = this._Internal._Move_Y( iY, this.Orientation )
  216.         elseif ( iAxisOrdering == this.MOVE_YXZ ) then
  217.             this.Orientation = this._Internal._Move_Y( iY, this.Orientation )
  218.             this.Orientation = this._Internal._Move_X( iX, this.Orientation )
  219.             this._Internal._Move_Z( iZ )
  220.         elseif ( iAxisOrdering == this.MOVE_YZX ) then
  221.             this.Orientation = this._Internal._Move_Y( iY, this.Orientation )
  222.             this._Internal._Move_Z( iZ )
  223.             this.Orientation = this._Internal._Move_X( iX, this.Orientation )
  224.         elseif ( iAxisOrdering == this.MOVE_ZXY ) then
  225.             this._Internal._Move_Z( iZ )
  226.             this.Orientation = this._Internal._Move_X( iX, this.Orientation )
  227.             this.Orientation = this._Internal._Move_Y( iY, this.Orientation )
  228.         elseif ( iAxisOrdering == this.MOVE_ZYX ) then
  229.             this._Internal._Move_Z( iZ )
  230.             this.Orientation = this._Internal._Move_Y( iY, this.Orientation )
  231.             this.Orientation = this._Internal._Move_X( iX, this.Orientation )
  232.         end
  233.        
  234.         -- Correct orientation if asked to
  235.         if ( bKeepOrientation ) then
  236.             while ( this.Orientation > iInitialOrientation ) do
  237.                 turtle.turnLeft()
  238.                 this.Orientation = this.Orientation - 1
  239.             end
  240.             while ( this.Orientation < iInitialOrientation ) do
  241.                 turtle.turnRight()
  242.                 this.Orientation = this.Orientation + 1
  243.             end
  244.         end
  245.        
  246.         -- Update position
  247.         this.Position.X = this.Position.X + iX
  248.         this.Position.Y = this.Position.Y + iY
  249.         this.Position.Z = this.Position.Z + iZ
  250.     end
  251. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement