Advertisement
jille_Jr

CC: Treefarm - main.lua

May 29th, 2014
324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.24 KB | None | 0 0
  1.  
  2. --(( Settings ))--
  3.  
  4. -- wether or not if the turtle should
  5. -- attack mobs if they are in their way
  6. local killMobs = true
  7.  
  8. --
  9. local destroyBlocks = false
  10.  
  11. -- apis to load, file path
  12. -- will be loaded in ascending order, top to bottom
  13. -- will ignore file extension when loading
  14. -- ex: 'dig.lua' can be refered as 'dig'
  15. local apis = {
  16.     "dig.lua",
  17.     "gps.lua",
  18.     "farm.lua",
  19.     "item.lua",
  20. }
  21.  
  22. _G.oldAPISyntax = "_old"
  23.  
  24. --(( Variables ))--
  25.  
  26. -- the directory of the program running
  27. local programDir = shell.getRunningProgram() -- get the full path
  28. programDir = programDir:match("^.+/") or "" -- get the folder
  29.  
  30. -- load apis
  31.  
  32.  
  33. --(( Functions ))--
  34.  
  35. -- table input should be indexed by integers
  36. -- example input: {"john","brad","tony","steve"}   ", "   ", and "
  37. -- example output: "john, brad, tony, and steve"
  38. local function combine( t,seperator,lastSeperator )
  39.     local str = ""
  40.  
  41.     for index,value in ipairs(t) do
  42.         if index == 1 then
  43.             str = str .. value
  44.         elseif index > 1 and index < #t then
  45.             str = str .. seperator .. value
  46.         elseif index == #t then
  47.             str = str .. lastSeperator .. value
  48.         end
  49.     end
  50.  
  51.     return str
  52. end
  53.  
  54. local function loadAPIs( t )
  55.     local failed = {} -- apis that failed to load
  56.     -- loop through all apis
  57.     for _,path in ipairs(apis) do
  58.         local fullPath = programDir .. path
  59.         local fileName = fs.getName(fullPath)-- remove directory
  60.  
  61.         local apiName = fileName
  62.         apiName = apiName:match("^.+%.") or apiName.."." -- remove the file extension
  63.         apiName = apiName:sub(0,-2) -- remove the dot at the end
  64.  
  65.         -- if its already loaded; unload it first
  66.         --if _G[apiName] then
  67.         --  os.unloadAPI(apiName)
  68.         --end
  69.  
  70.         -- load api, move its global position
  71.         local oldvar = _G[fileName]
  72.         if os.loadAPI(fullPath) then
  73.             -- in case theres an api already loaded
  74.             if _G[apiName] ~= nil and _G[apiName.._G.oldAPISyntax]==nil then
  75.                 _G[apiName.._G.oldAPISyntax] = _G[apiName]
  76.             end
  77.  
  78.             _G[apiName] = _G[fileName]
  79.         else
  80.             table.insert(failed,fullPath)
  81.         end
  82.         -- in case of overriding, reset
  83.         _G[fileName] = oldvar
  84.  
  85.         -- give it functions
  86.         if type(_G[apiName]) == "table" then
  87.             if type(_G[apiName].init) == "function" then
  88.                 local state,err = pcall(_G[apiName].init,_G)
  89.                 if not state then print(apiName..": "..err)table.insert(failed,fullPath) end
  90.             end
  91.         end
  92.     end
  93.  
  94.     if #failed == 1 then
  95.         error("Unable to load API "..failed[1].."!",0)
  96.     elseif #failed > 1 then
  97.         error("Unable to load APIs "..combine(failed,", ",", and ").."!",0)
  98.     end
  99. end
  100.  
  101. function _G.mult( num )
  102.     if num > 0 then return 1 end
  103.     if num < 0 then return -1 end
  104.     return 0
  105. end
  106.  
  107. --(( Error checking ))--
  108.  
  109. -- load apis
  110. loadAPIs()
  111.  
  112. --(( Main program ))--
  113. print("main step 1")
  114. farm.setOffset( 3,4,1 )print("main step 2")
  115. farm.setSize( 7,-5,4 )print("main step 3")
  116. if not gps.autoSetPosition() then error("unable to locate position via gps",0) end print("main step 4")
  117. gps.setHome(245,60,-765,gps.east)print("main step 5")
  118. gps.goto(gps.getHome())
  119.  
  120. item.addStoragePathCheckpoint(246,60,-765)
  121. item.addStoragePathCheckpoint(244,60,-765)
  122. item.setItemAccess(item.types.dirt,244,60,-766,gps.down)
  123. item.setItemAccess(item.types.sapling,244,60,-765,gps.down)
  124. item.setItemAccess(item.types.dropoff,244,60,-764,gps.down)
  125.  
  126.  
  127. --(( EOF ))--
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement