Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ---------------------------------------------------------------------------------------------------------------
- -- Generic LUA library for ComputerCraft
- ---------------------------------------------------------------------------------------------------------------
- -- Written by Shikifuyin
- ---------------------------------------------------------------------------------------------------------------
- ---------------------------------------------------------------------------------------------------------------
- -- Includes
- ---------------------------------------------------------------------------------------------------------------
- -- Global definitions
- ---------------------------------------------------------------------------------------------------------------
- -- File I/O
- File = {
- -- Constants
- -- Members
- -- Internal members/methods
- -- Methods
- Exists = function( strName )
- return fs.exists( strName )
- end,
- LoadVar = function( strName )
- hFile = fs.open( strName, "r" )
- hValue = hFile.read()
- hFile.close()
- return hValue
- end,
- SaveVar = function( strName, hValue )
- hFile = fs.open( strName, "w" )
- hFile.write( hValue )
- hFile.close()
- end
- }
- ---------------------------------------------------------------------------------------------------------------
- -- Turtles
- TurtleInterfaces = {
- -- Methods
- -- GetModem : Retrieve face with modem interface
- GetModem = function()
- for X, strFacet in pairs(rs.getSides()) do
- if ( peripheral.getType(strFacet) == "modem" ) then
- return strFacet
- end
- end
- return nil
- end
- }
- TurtleController = {
- -- Constants
- MOVE_XYZ = 0,
- MOVE_XZY = 1,
- MOVE_YXZ = 2,
- MOVE_YZX = 3,
- MOVE_ZXY = 4,
- MOVE_ZYX = 5,
- ORIENTATION_NORTH = 0,
- ORIENTATION_EAST = 1,
- ORIENTATION_SOUTH = 2,
- ORIENTATION_WEST = 3,
- -- Members
- Position = { X = 0, Y = 0, Z = 0 }, -- Position relative to last set origin
- Orientation = ORIENTATION_NORTH, -- Orientation relative to last set origin
- -- Internal members/methods
- _Internal = {
- _Move_X = function( iDX, iOrientation )
- local iAmountX = abs( iDX )
- if ( iDX < 0 ) then -- Move backward
- turtle.turnRight()
- turtle.turnRight()
- iOrientation = iOrientation + 2
- while ( iOrientation > 3 ) do iOrientation = iOrientation - 4 end
- for i = 1, iAmountX do turtle.forward() end
- elseif ( iDX > 0 ) then -- Move forward
- for i = 1, iAmountX do turtle.forward() end
- end
- return iOrientation
- end,
- _Move_Y = function( iDY, iOrientation )
- local iAmountY = abs( iDY )
- if ( iDY < 0 ) then -- Move left
- turtle.turnLeft()
- iOrientation = iOrientation - 1
- while ( iOrientation < 0 ) do iOrientation = iOrientation + 4 end
- for i = 1, iAmountY do turtle.forward() end
- elseif ( iDY > 0 ) then -- Move right
- turtle.turnRight()
- iOrientation = iOrientation + 1
- while ( iOrientation > 3 ) do iOrientation = iOrientation - 4 end
- for i = 1, iAmountY do turtle.forward() end
- end
- return iOrientation
- end,
- _Move_Z = function( iDZ )
- local iAmountZ = abs( iDZ )
- if ( iDZ < 0 ) then -- Move down
- for i = 1, iAmountZ do turtle.down() end
- elseif ( iDZ > 0 ) then -- Move up
- for i = 1, iAmountZ do turtle.up() end
- end
- end
- },
- -- Methods
- -- Constructor
- New = function( this, hObject )
- hObject = hObject or {}
- setmetatable( hObject, this )
- this.__index = this
- return hObject
- end,
- -- Refuel : Search inventory for fuel and use it if needed
- Refuel = function( this )
- for i = 1, 16 do
- if ( turtle.getFuelLevel() >= 1 ) then
- return
- end
- turtle.select( i )
- turtle.refuel()
- turtle.select( 1 )
- end
- end,
- -- SetOrigin : Sets current turtle's position & orientation as absolute origin
- SetOrigin = function( this )
- this.Position.X = 0
- this.Position.Y = 0
- this.Position.Z = 0
- this.Orientation = ORIENTATION_NORTH
- end,
- -- Turn : Rotates the turtle along Z axis
- -- iOrientation = Target orientation relative to current orientation (relative, default) or tracked orientation (absolute)
- -- bAbsolute = Use origin-tracking system to specify absolute orientation else use relative orientation (default)
- Turn = function( this, iOrientation, bAbsolute )
- -- Default parameters
- bAbsolute = bAbsolute or false
- if ( bAbsolute ) then
- -- Clamp orientation value
- while ( iOrientation < 0 ) do iOrientation = iOrientation + 4 end
- while ( iOrientation > 3 ) do iOrientation = iOrientation - 4 end
- -- Absolute rotation case
- while ( this.Orientation > iOrientation ) do
- turtle.turnLeft()
- this.Orientation = this.Orientation - 1
- end
- while ( this.Orientation < iOrientation ) do
- turtle.turnRight()
- this.Orientation = this.Orientation + 1
- end
- else
- -- Clamp orientation value
- while ( iOrientation < -3 ) do iOrientation = iOrientation + 4 end
- while ( iOrientation > 3 ) do iOrientation = iOrientation - 4 end
- -- Relative rotation case
- while ( iOrientation < 0 ) do
- turtle.turnLeft()
- this.Orientation = this.Orientation - 1
- iOrientation = iOrientation + 1
- end
- while ( iOrientation > 0 ) do
- turtle.turnRight()
- this.Orientation = this.Orientation + 1
- iOrientation = iOrientation - 1
- end
- -- Clamp final value
- while ( this.Orientation < 0 ) do this.Orientation = this.Orientation + 4 end
- while ( this.Orientation > 3 ) do this.Orientation = this.Orientation - 4 end
- end
- end,
- -- Move : Moves the turtle.
- -- iX, iY, iZ = Target position relative to current position (relative, default) or tracked origin (absolute)
- -- iAxisOrdering = Use one of the above values to specify movement ordering (default = XYZ)
- -- bKeepOrientation = Restore pre-move orientation (no momentum) else use post-move orientation (keep momentum, default)
- -- bAbsolute = Use origin-tracking system to specify absolute coordinates else use relative coordinates (default)
- Move = function( this, iX, iY, iZ, iAxisOrdering, bKeepOrientation, bAbsolute )
- -- Default parameters
- iAxisOrdering = iAxisOrdering or this.MOVE_XYZ
- bKeepOrientation = bKeepOrientation or false
- bAbsolute = bAbsolute or false
- -- Save initial orientation
- local iInitialOrientation = this.Orientation
- -- Absolute movement case
- if ( bAbsolute ) then
- iX = ( iX - this.Position.X )
- iY = ( iY - this.Position.Y )
- iZ = ( iZ - this.Position.Z )
- end
- -- Switch with axis ordering
- if ( iAxisOrdering == this.MOVE_XYZ ) then
- this.Orientation = this._Internal._Move_X( iX, this.Orientation )
- this.Orientation = this._Internal._Move_Y( iY, this.Orientation )
- this._Internal._Move_Z( iZ )
- elseif ( iAxisOrdering == this.MOVE_XZY ) then
- this.Orientation = this._Internal._Move_X( iX, this.Orientation )
- this._Internal._Move_Z( iZ )
- this.Orientation = this._Internal._Move_Y( iY, this.Orientation )
- elseif ( iAxisOrdering == this.MOVE_YXZ ) then
- this.Orientation = this._Internal._Move_Y( iY, this.Orientation )
- this.Orientation = this._Internal._Move_X( iX, this.Orientation )
- this._Internal._Move_Z( iZ )
- elseif ( iAxisOrdering == this.MOVE_YZX ) then
- this.Orientation = this._Internal._Move_Y( iY, this.Orientation )
- this._Internal._Move_Z( iZ )
- this.Orientation = this._Internal._Move_X( iX, this.Orientation )
- elseif ( iAxisOrdering == this.MOVE_ZXY ) then
- this._Internal._Move_Z( iZ )
- this.Orientation = this._Internal._Move_X( iX, this.Orientation )
- this.Orientation = this._Internal._Move_Y( iY, this.Orientation )
- elseif ( iAxisOrdering == this.MOVE_ZYX ) then
- this._Internal._Move_Z( iZ )
- this.Orientation = this._Internal._Move_Y( iY, this.Orientation )
- this.Orientation = this._Internal._Move_X( iX, this.Orientation )
- end
- -- Correct orientation if asked to
- if ( bKeepOrientation ) then
- while ( this.Orientation > iInitialOrientation ) do
- turtle.turnLeft()
- this.Orientation = this.Orientation - 1
- end
- while ( this.Orientation < iInitialOrientation ) do
- turtle.turnRight()
- this.Orientation = this.Orientation + 1
- end
- end
- -- Update position
- this.Position.X = this.Position.X + iX
- this.Position.Y = this.Position.Y + iY
- this.Position.Z = this.Position.Z + iZ
- end
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement