Advertisement
omr__

LAMA API

Sep 11th, 2019
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.61 KB | None | 0 0
  1. -- LAMA API available under the MIT license from "https://raw.githubusercontent.com/KingofGamesYami/LAMA/master/lama.lua"
  2.  
  3. --[[ The MIT License (MIT)
  4.  
  5. -- Copyright (c) 2015 KingofGamesYami
  6.  
  7. -- Permission is hereby granted, free of charge, to any person obtaining a copy
  8. -- of this software and associated documentation files (the "Software"), to deal
  9. -- in the Software without restriction, including without limitation the rights
  10. -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. -- copies of the Software, and to permit persons to whom the Software is
  12. -- furnished to do so, subject to the following conditions:
  13.  
  14. -- The above copyright notice and this permission notice shall be included in all
  15. -- copies or substantial portions of the Software.
  16.  
  17. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. -- SOFTWARE.
  24. --]]
  25.  
  26. -- Converted to be Require compatible by Lupus590 and released under the same MIT license.
  27. -- Look for REQUIRE_COMPAT in comments, the connected multi line comments are removed original stuff with the replacement under that.
  28.  
  29. --Copy the default turtle directory
  30. local turtle = {}
  31. for k, v in pairs( _G.turtle ) do
  32.     turtle[ k ] = v
  33. end
  34.  
  35. --copy default gps
  36. local gps = {}
  37. for k, v in pairs( _G.gps ) do
  38.     gps[ k ] = v
  39. end
  40.  
  41. if not fs.isDir( ".lama" ) then
  42.     fs.makeDir( ".lama" )
  43. end
  44.  
  45. -- REQUIRE_COMPAT
  46. --[[
  47. --get the current env
  48. --anything declared as env.name will end up in the loaded API
  49. local env = getfenv() ]]
  50. local env = shell and {} or (_ENV or getfenv()) -- see https://github.com/lupus590/CC-Random-Code/blob/8cb3bd9b6e54c0176ff3e9418fc96b7866d3c963/src/dofile%20and%20loadAPI%20compatable%20API's.lua#L3
  51.  
  52. local fuel = {}
  53. local facing = {}
  54. local position = {}
  55.  
  56. --Fuel tracking
  57. fuel.load = function() --loading fuel data
  58.     if fs.exists( ".lama/fuel" ) then --if we've got previous data, we want to use it
  59.         local file = fs.open( ".lama/fuel", "r" )
  60.         fuel.amount = tonumber( file.readAll() )
  61.         file.close()
  62.     else --otherwise, use the current fuel level
  63.         fuel.amount = turtle.getFuelLevel()
  64.     end
  65. end
  66.  
  67. fuel.save = function() --save fuel data
  68.     local file = fs.open( ".lama/fuel", "w" )
  69.     file.write( fuel.amount )
  70.     file.close()
  71. end
  72.  
  73. --facing tracking
  74. facing.turnRight = function() --changes the facing clockwise (on a compass) once
  75.     if facing.face == "north" then
  76.         facing.face = "east"
  77.     elseif facing.face == "east" then
  78.         facing.face = "south"
  79.     elseif facing.face == "south" then
  80.         facing.face = "west"
  81.     elseif facing.face == "west" then
  82.         facing.face = "north"
  83.     end
  84. end
  85.  
  86. facing.save = function() --saves facing and current movement direction
  87.     local file = fs.open( ".lama/facing", "w" )
  88.     file.write( textutils.serialize( {facing.face, facing.direction} ) )
  89.     file.close()
  90. end
  91.  
  92. facing.load = function() --loads facing / current movement direction
  93.     if fs.exists( ".lama/facing" ) then --if we have previous data, we use it
  94.         local file = fs.open( ".lama/facing", "r" )
  95.         facing.face, facing.direction = unpack( textutils.unserialize( file.readAll() ) )
  96.         file.close()
  97.     else --otherwise, try to locate via gps
  98.         local x, y, z = gps.locate(1)
  99.         if x and turtle.forward() then
  100.             local newx, newy, newz = gps.locate(1)
  101.             if not newx then --we didn't get a location
  102.                 facing.face = "north" --default
  103.             elseif newx > x then
  104.                 facing.face = "east"
  105.             elseif newx < x then
  106.                 facing.face = "west"
  107.             elseif newz > z then
  108.                 facing.face = "south"
  109.             elseif newz < z then
  110.                 facing.face = "north"
  111.             end
  112.         else
  113.             facing.face = "north" --we couldn't move forward, something was obstructing
  114.         end
  115.     end
  116. end
  117.  
  118. --position tracking
  119. position.save = function() --saves position (x, y, z)
  120.     position.update() --update the position based on direction and fuel level, then save it to a file
  121.     local file = fs.open( ".lama/position", "w" )
  122.     file.write( textutils.serialize( { position.x, position.y, position.z } ) )
  123.     file.close()
  124. end
  125.  
  126. position.load = function() --loads position (x, y z)
  127.     if fs.exists( ".lama/position" ) then --if we have previous data, use it
  128.         local file = fs.open( ".lama/position", "r" )
  129.         position.x, position.y, position.z = unpack( textutils.unserialize( file.readAll() ) )
  130.         file.close()
  131.     else --otherwise try for gps coords
  132.         local x, y, z = gps.locate(1)
  133.         if x then
  134.             position.x, position.y, position.z = x, y, z
  135.         else --now we assume 1,1,1
  136.             position.x, position.y, position.z = 1, 1, 1
  137.         end
  138.     end
  139. end
  140.  
  141. position.update = function() --updates the position of the turtle
  142.     local diff = fuel.amount - turtle.getFuelLevel()
  143.     if diff > 0 then --if we've spent fuel (ei moved), we'll need to move that number in a direction
  144.         if facing.direction == 'east' then
  145.             position.x = position.x + diff
  146.         elseif facing.direction == "west" then
  147.             position.x = position.x - diff
  148.         elseif facing.direction == "south" then
  149.             position.z = position.z + diff
  150.         elseif facing.direction == "north" then
  151.             position.z = position.z - diff
  152.         elseif facing.direction == "up" then
  153.             position.y = position.y + diff
  154.         elseif facing.direction == "down" then
  155.             position.y = position.y - diff
  156.         end
  157.     end
  158.     fuel.amount = turtle.getFuelLevel() --update the fuel amount
  159.     fuel.save() --save the fuel amount
  160. end
  161.  
  162. --direct opposite compass values, mainly for env.back
  163. local opposite = {
  164.     ["north"] = "south",
  165.     ["south"] = "north",
  166.     ["east"] = "west",
  167.     ["west"] = "east",
  168. }
  169.  
  170. env.forward = function() --basically, turtle.forward
  171.     if facing.direction ~= facing.face then --if we were going a different direction before
  172.         position.save() --save out position
  173.         facing.direction = facing.face --update the direction
  174.         facing.save() --save the direction
  175.     end
  176.     return turtle.forward() --go forward, return result
  177. end
  178.  
  179. env.back = function() --same as env.forward, but going backwards
  180.     if facing.direction ~= opposite[ facing.face ] then
  181.         position.save()
  182.         facing.direction = opposite[ facing.face ]
  183.         facing.save()
  184.     end
  185.     return turtle.back()
  186. end
  187.  
  188. env.up = function() --turtle.up
  189.     if facing.direction ~= "up" then --if we were going a different direction
  190.         position.save() --save our position
  191.         facing.direction = "up" --set the direction to up
  192.         facing.save() --save the direction
  193.     end
  194.     return turtle.up() --go up, return result
  195. end
  196.  
  197. env.down = function() --env.up, but for going down
  198.     if facing.direction ~= "down" then
  199.         position.save()
  200.         facing.direction = "down"
  201.         facing.save()
  202.     end
  203.     return turtle.down()
  204. end
  205.  
  206. env.turnRight = function() --turtle.turnRight
  207.     position.save() --save the position (x,y,z)
  208.     facing.turnRight() --update our compass direction
  209.     facing.save() --save it
  210.     return turtle.turnRight() --return the result
  211. end
  212.  
  213. env.turnLeft = function() --env.turnRight, but the other direction
  214.     position.save()
  215.     facing.turnRight() --going clockwise 3 times is the same as
  216.     facing.turnRight() --going counterclockwise once
  217.     facing.turnRight()
  218.     facing.save()
  219.     return turtle.turnLeft()
  220. end
  221.  
  222. env.refuel = function( n ) --needed because we depend on fuel level
  223.     position.update() --update our position
  224.     if turtle.refuel( n ) then --if we refueled then
  225.         fuel.amount = turtle.getFuelLevel() --set our amount to the current level
  226.         fuel.save() --save that amount
  227.         return true
  228.     end
  229.     return false --otherwise, return false
  230. end
  231.  
  232. env.overwrite = function( t ) --writes env values into the table given
  233.     t = t or _G.turtle    --or, if no value was given, _G.turtle
  234.     for k, v in pairs( env ) do
  235.         t[ k ] = v
  236.     end
  237. end
  238.  
  239. env.getPosition = function() --returns the current position of the turtle
  240.     position.update() --first we should update the position (otherwise it'll give coords of the last time we did this)
  241.     return position.x, position.y, position.z, facing.face
  242. end
  243.  
  244. env.setPosition = function( x, y, z, face ) --sets the current position of the turtle
  245.     position.x = x
  246.     position.y = y
  247.     position.z = z
  248.     facing.face = face or facing.face --default the the current facing if it's not provided
  249.     position.save() --save our new position
  250.     facing.save() --save the way we are facing
  251. end
  252.  
  253. --overwrite gps.locate
  254. _G.gps.locate = function( n, b )
  255.     local x, y, z, facing = env.getPosition()
  256.     return x, y, z
  257. end
  258.  
  259. facing.load()
  260. position.load()
  261. fuel.load()
  262.  
  263. fuel.save()
  264. position.save()
  265. facing.save()
  266.  
  267. -- REQUIRE_COMPAT
  268. return env
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement