Advertisement
Ulthean

api_sharedFunctions

Jan 12th, 2013
12,064
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 24.47 KB | None | 0 0
  1. -- VALUES FOR THE DIRECTIONS
  2. local up="up"
  3. local down="down"
  4. local forward="forward"
  5. local back ="back"
  6. local right="right"
  7. local left="left"
  8.  
  9. -- LOAD THE API OFFERING EXTENDED TURTLE MOVEMENT OPTIONS
  10. os.loadAPI("api_turtleExt")
  11.  
  12. -- ------------------------------------------------------------------------------ --
  13. -- START OF THE FUNCTION HANDLING THE FILE INPUT AND OUTPUT AND STRING OPERATIONS --
  14. -- ------------------------------------------------------------------------------ --
  15.  
  16. -- CHECK IF A FILE EXISTS
  17. local function fileExists(filename)
  18.   local file=io.open(filename, "rb")
  19.   if file then
  20.     file:close()
  21.   end
  22.   return file~=nil
  23. end
  24.  
  25.  
  26. -- GETS ALL THE INPUT FROM A FILE
  27. local function getFileContent(filename, skipLines)
  28.   skipLines=skipLines or 0
  29.   if not fileExists(filename) then
  30.     return {}
  31.   end
  32.   local file=io.open(filename, "r")
  33.   local lines={}
  34.   while true do
  35.     for i=1, skipLines do
  36.       file:read()
  37.     end
  38.     local line=file:read()
  39.     if line==nil then
  40.       break
  41.     end
  42.     lines[#lines+1]=line
  43.   end
  44.   file:close()
  45.   return lines
  46. end
  47.  
  48.  
  49. -- SPLITS A TABLE OF STRINGS INTO THEIR KEYS AND VALUES
  50. local function splitInput(table)
  51.   local results={}
  52.   for k,v in pairs(table) do
  53.     local key=v:sub(0, (v:find('='))-1)
  54.     local value=v:sub((v:find('='))+1)
  55.     results[key]=value
  56.   end
  57.   return results
  58. end
  59.  
  60. -- ---------------------------------------------------------------------------- --
  61. -- END OF THE FUNCTION HANDLING THE FILE INPUT AND OUTPUT AND STRING OPERATIONS --
  62. -- ---------------------------------------------------------------------------- --
  63.  
  64. -- ------------------------------------------------------------ --
  65. -- START OF THE FUNCTIONS RELATING TO THE CONFIG OF THE PROGRAM --
  66. -- ------------------------------------------------------------ --
  67.  
  68. -- RETURNS IF A THE VALUE GIVEN PARAMETER IS A NUMBER
  69. local function isNumberInput(param)
  70.   if ((param=="numL") or (param=="numT") or (param=="numS") or (param=="skipL") or (param=="skipT") or (param=="skipS")
  71.       or (param=="height") or (param=="blocksBetweenShafts") or (param=="firstShaftOffset") or (param=="evenLevelOffset")
  72.       or (param=="centerRadius") or (param=="numIgnoreBlocks") or (param=="departureFuel") or (param=="returnFuel")) then
  73.     return true
  74.   else
  75.     return false
  76.   end
  77. end
  78.  
  79.  
  80. -- RETURNS IF THE VALUE OF A GIVEN PARAMETER IS A DIRECTION
  81. local function isDirectionInput(param)
  82.   if ((param=="nextDirL") or (param=="nextDirT") or (param=="fuelSuckDir") or (param=="fuelDropDir") or (param=="itemDropDir")
  83.       or (param=="torchSuckDir") or (param=="ignoreDir")) then
  84.     return true
  85.   else
  86.     return false
  87.   end
  88. end
  89.  
  90.  
  91. -- RETURNS IF THE VALUE OF A GIVEN PARAMETER IS A BOOLEAN
  92. local function isBooleanInput(param)
  93.   if ((param=="placeTorches") or (param=="singleChest") or (param=="requiresFuel") or (param=="digSidesToo")) then
  94.     return true
  95.   else
  96.     return false
  97.   end
  98. end
  99.  
  100. -- CHECKS IF A STRING DENOTES A DIRECTION
  101. function isDirection(dir)
  102.   if dir=="forward" or dir=="back" or dir=="left" or dir=="right" or dir=="up" or dir=="down" then
  103.     return true
  104.   end
  105.   return false
  106. end
  107.  
  108. -- PRINTS THE MANUAL TO THE TERMINAL IN CASE THE USER SPECIFIED INCORRECT VALUES
  109. local function printManual()
  110.   term.clear()
  111.   print("Command Line Arguments (1/3)")
  112.   print("----------------------------")
  113.   print(" -numL <num>     (=nr. of levels)")
  114.   print(" -numT <num>     (=nr. of tunnels)")
  115.   print(" -numS <num>     (=nr. of shafts)")
  116.   print(" -skipL <num>    (=skip # levels)")
  117.   print(" -skipT <num>    (=skip # tunnels)")
  118.   print(" -skipS <num>    (=skip # shafts)")
  119.   print(" -dirNextL <dir> (=next lev. U/D)")
  120.   print(" -dirNextT <dir> (=next tun. L/R)")
  121.   print("PRESS ANY KEY TO CONTINUE")
  122.   print("")
  123.   read()
  124.   term.clear()
  125.   print("Command Line Arguments (2/3)")
  126.   print("----------------------------")
  127.   print(" -height <num>   (=level height 2/3)")
  128.   print(" -betw <num>     (=blocks betw shafts)")
  129.   print(" -firstOffs <num>(=first shaft offset)")
  130.   print(" -evenOffs <num> (=even level offset)")
  131.   print(" -torches        (=place torches)")
  132.   print(" -oneChest       (=single drop chest)")
  133.   print(" -rad <num>      (=center radius)")
  134.   print(" -sides          (=dig out the sides)")
  135.   print(" -numIgnore <num>(=num ignore blocks)")
  136.   print("PRESS ANY KEY TO CONTINUE")
  137.   read()
  138.   term.clear()
  139.   print("Command Line Arguments (3/3)")
  140.   print("----------------------------")
  141.   print(" -noFuel         (=no need for fuel)")
  142.   print(" -depFuel <num>  (=departure fuel)")
  143.   print(" -retFuel <num>  (=return fuel)")
  144.   print(" -fuelS <dir>    (=fuel suck dir)")
  145.   print(" -fuelD <dir>    (=fuel drop dir)")
  146.   print(" -torchS <dir>   (=torch suck dir)")
  147.   print(" -itemD <dir>    (=item drop dir)")
  148.   print(" -ignoreS <dir>  (=ignore block dir)")
  149.   print("")
  150.   print("")
  151. end
  152.  
  153. -- PRINTS THE POSSIBLE DIRECTION VALUES TO THE TERMINAL
  154. local function printDirs(source, attribute, value)
  155.   term.clear()
  156.   print("Invalid direction chosen:")
  157.   print("------------------------------------")
  158.   print("_SOURCE= "..source)
  159.   print("_ATTRIB= "..attribute)
  160.   print("_VALUE="..value)
  161.   print("------------------------------------")
  162.   print(" up")
  163.   print(" down")
  164.   print(" forward")
  165.   print(" back")
  166.   print(" right")
  167.   print(" left")
  168.   sleep(2)
  169. end
  170.  
  171. -- RETURNS THE DEFAULT CONFIGURATION OF THE PROGRAMS
  172. local function getDefaultConfig()
  173.   local configuration={}
  174.   -- THE NUMBER OF LEVELS
  175.   configuration.numL=1
  176.   -- THE NUMBER OF TUNNELS
  177.   configuration.numT=4
  178.   -- THE NUMBER OF SHAFTS TO EXCAVATE
  179.   configuration.numS=16
  180.   -- HOW MANY LEVELS TO SKIP
  181.   configuration.skipL=0
  182.   -- HOW MANY TUNNELS TO SKIP
  183.   configuration.skipT=0
  184.   -- HOW MANY SHAFTS TO SKIP
  185.   configuration.skipS=0
  186.   -- THE DIRECTION OF THE NEXT LEVEL (UP or DOWN)
  187.   configuration.nextDirL=up
  188.   -- THE DIRECTION OF THE NEXT TUNNEL (LEFT or RIGHT)
  189.   configuration.nextDirT=left
  190.   -- THE HEIGHT OF THE LEVELS/TUNNELS
  191.   configuration.height=3
  192.   -- THE NUMBER OF BLOCKS BETWEEN EACH SHAFT
  193.   configuration.blocksBetweenShafts=3
  194.   -- HOW MANY BLOCKS WILL THE TURTLE MOVE BEFORE DIGGING THE FIRST SHAFT
  195.   configuration.firstShaftOffset=4
  196.   -- THE NUMBER OF BLOCKS THE EVEN LEVEL SHAFTS WILL BE OFFSET
  197.   configuration.evenLevelOffset=2
  198.   -- DOES THE TURTLE ALSO PLACE TORCHES
  199.   configuration.placeTorches=false
  200.   -- IS THERE A SINGLE DROPOFF STATION (true) OR ONE PER DIRECTION, PER LEVEL (false)
  201.   configuration.singleChest=false
  202.   -- THE RADIUS OF THE CIRCLE ON WHICH THE REFUELING STATIONS ARE POSITIONED
  203.   configuration.centerRadius=5
  204.   -- DOES THE TURTLE NEED TO DIG OUT THE SIDES OF THE TUNNEL TOO
  205.   configuration.digSidesToo=false
  206.   -- THE NUMBER OF BLOCK TYPES THE TURTLE WILL _NOT_ DIG UNLESS NEEDED
  207.   -- DEFAULT CONSIDERS: Smooth stone, Dirt, Gravel, Marble (Tekkit), Wood, stone bricks
  208.   configuration.numIgnoreBlocks=6
  209.   -- DOES THE TURTLE REQUIRE FUEL TO OPERATE?
  210.   configuration.requiresFuel=true
  211.   -- THE AMOUNT OF FUEL THE TURTLE WILL STORE BEFORE HEADING OF TO A SHAFT
  212.   configuration.departureFuel=1200
  213.   -- THE AMOUNT OF FUEL AT WHICH THE TURTLE WILL RETURN TO DROPOFF
  214.   configuration.returnFuel=200
  215.   -- THE DIRECTION OF THE FUEL ITEMS (IN THE REFUELING STATION)
  216.   configuration.fuelSuckDir=up
  217.   -- THE DIRECTION WHERE THE TURTLE WILL DROP LEFTOVERS (BUCKETS)
  218.   configuration.fuelDropDir=down
  219.   -- THE DIRECTION WHERE THE TURTLE WILL DROP ANY MINED ITEMS
  220.   configuration.itemDropDir=down
  221.   -- THE DIRECTION OF THE TORCHES
  222.   configuration.torchSuckDir=right
  223.   -- THE DIRECTION OF THE IGNORE BLOCKS
  224.   configuration.ignoreDir=left
  225.   -- RETURN THE CONFIGURATION
  226.   return configuration
  227. end
  228.  
  229.  
  230. -- CHANGES THE GIVEN CONFIGURATION TO ACCOUNT FOR THE CONFIG FILE
  231. local function processConfigFile(configuration, filename)
  232.   local lines=getFileContent(filename, 1)
  233.   local parsed=splitInput(lines)
  234.   for k,v in pairs(parsed) do
  235.     -- IF IT IS A NUMBER INPUT
  236.     if isNumberInput(k) then
  237.       configuration[k]=tonumber(v)
  238.     end
  239.     -- IF IT IS A DIRECTION INPUT
  240.     if isDirectionInput(k) then
  241.       if not isDirection(v) then
  242.         printDirs("Config File: "..filename, k, v)
  243.         return false
  244.       end
  245.       configuration[k]=v
  246.     end
  247.     -- IF IT IS A BOOLEAN INPUT
  248.     if isBooleanInput(k) then
  249.       if v=="true" then
  250.         configuration[k]=true
  251.       else
  252.         configuration[k]=false
  253.       end
  254.     end
  255.   end
  256.   return true
  257. end
  258.  
  259.  
  260. -- CHANGES THE GIVEN CONFIGURATION TO ACCOUNT FOR THE COMMAND LINE ARGUMENTS
  261. local function processCommandLine(configuration, args)
  262.   readParams=0
  263.   while readParams < #args do
  264.     if string.lower(args[readParams+1])=="-numl" then
  265.       if not (readParams+1 < #args) then
  266.         printManual()
  267.         return false
  268.       end
  269.       configuration.numL=tonumber(args[readParams+2])
  270.       readParams=readParams+2
  271.     elseif string.lower(args[readParams+1])=="-numt" then
  272.       if not (readParams+1 < #args) then
  273.         printManual()
  274.         return false
  275.       end
  276.       configuration.numT=tonumber(args[readParams+2])
  277.       readParams=readParams+2
  278.     elseif string.lower(args[readParams+1])=="-nums" then
  279.       if not (readParams+1 < #args) then
  280.         printManual()
  281.         return false
  282.       end
  283.       configuration.numS=tonumber(args[readParams+2])
  284.       readParams=readParams+2
  285.     elseif string.lower(args[readParams+1])=="-skipl" then
  286.       if not (readParams+1 < #args) then
  287.         printManual()
  288.         return false
  289.       end
  290.       configuration.skipL=tonumber(args[readParams+2])
  291.       readParams=readParams+2
  292.     elseif string.lower(args[readParams+1])=="-skipt" then
  293.       if not (readParams+1 < #args) then
  294.         printManual()
  295.         return false
  296.       end
  297.       configuration.skipT=tonumber(args[readParams+2])
  298.       readParams=readParams+2
  299.     elseif string.lower(args[readParams+1])=="-skips" then
  300.       if not (readParams+1 < #args) then
  301.         printManual()
  302.         return false
  303.       end
  304.       configuration.skipS=tonumber(args[readParams+2])
  305.       readParams=readParams+2
  306.     elseif string.lower(args[readParams+1])=="-dirnextl" then
  307.       if not (readParams+1 < #args) then
  308.         printManual()
  309.         return false
  310.       end
  311.       if not isDirection(args[readParams+2]) then
  312.         printDirs("Command Line Arguments", args[readParams+1], args[readParams+2])
  313.         return false
  314.       end
  315.       configuration.nextDirL=args[readParams+2]
  316.       readParams=readParams+2
  317.     elseif string.lower(args[readParams+1])=="-dirnextt" then
  318.       if not (readParams+1 < #args) then
  319.         printManual()
  320.         return false
  321.       end
  322.       if not isDirection(args[readParams+2]) then
  323.         printDirs("Command Line Arguments", args[readParams+1], args[readParams+2])
  324.         return false
  325.       end
  326.       configuration.nextDirT=args[readParams+2]
  327.       readParams=readParams+2
  328.     elseif string.lower(args[readParams+1])=="-height" then
  329.       if not (readParams+1 < #args) then
  330.         printManual()
  331.         return false
  332.       end
  333.       configuration.height=tonumber(args[readParams+2])
  334.       readParams=readParams+2
  335.     elseif string.lower(args[readParams+1])=="-betw" then
  336.       if not (readParams+1 < #args) then
  337.         printManual()
  338.         return false
  339.       end
  340.       configuration.blocksBetweenShafts=tonumber(args[readParams+2])
  341.       readParams=readParams+2
  342.     elseif string.lower(args[readParams+1])=="-firstoffs" then
  343.       if not (readParams+1 < #args) then
  344.         printManual()
  345.         return false
  346.       end
  347.       configuration.firstShaftOffset=tonumber(args[readParams+2])
  348.       readParams=readParams+2
  349.     elseif string.lower(args[readParams+1])=="-evenoffs" then
  350.       if not (readParams+1 < #args) then
  351.         printManual()
  352.         return false
  353.       end
  354.       configuration.evenLevelOffset=tonumber(args[readParams+2])
  355.       readParams=readParams+2
  356.     elseif string.lower(args[readParams+1])=="-torches" then
  357.       configuration.placeTorches=true
  358.       readParams=readParams+1
  359.     elseif string.lower(args[readParams+1])=="-onechest" then
  360.       configuration.singleChest=true
  361.       readParams=readParams+1
  362.     elseif string.lower(args[readParams+1])=="-rad" then
  363.       if not (readParams+1 < #args) then
  364.         printManual()
  365.         return false
  366.       end
  367.       configuration.centerRadius=tonumber(args[readParams+2])
  368.       readParams=readParams+2
  369.     elseif string.lower(args[readParams+1])=="-sides" then
  370.       configuration.digSidesToo=true
  371.       readParams=readParams+1
  372.     elseif string.lower(args[readParams+1])=="-numignore" then
  373.       if not (readParams+1 < #args) then
  374.         printManual()
  375.         return false
  376.       end
  377.       configuration.numIgnoreBlocks=tonumber(args[readParams+2])
  378.       readParams=readParams+2
  379.     elseif string.lower(args[readParams+1])=="-nofuel" then
  380.       configuration.requiresFuel=false
  381.       readParams=readParams+1
  382.     elseif string.lower(args[readParams+1])=="-depfuel" then
  383.       if not (readParams+1 < #args) then
  384.         printManual()
  385.         return false
  386.       end
  387.       configuration.departureFuel=tonumber(args[readParams+2])
  388.       readParams=readParams+2
  389.     elseif string.lower(args[readParams+1])=="-retfuel" then
  390.       if not (readParams+1 < #args) then
  391.         printManual()
  392.         return false
  393.       end
  394.       configuration.returnFuel=tonumber(args[readParams+2])
  395.       readParams=readParams+2
  396.     elseif string.lower(args[readParams+1])=="-fuels" then
  397.       if not (readParams+1 < #args) then
  398.         printManual()
  399.         return false
  400.       end
  401.       if not isDirection(args[readParams+2]) then
  402.         printDirs("Command Line Arguments", args[readParams+1], args[readParams+2])
  403.         return false
  404.       end
  405.       configuration.fuelSuckDir=args[readParams+2]
  406.       readParams=readParams+2
  407.     elseif string.lower(args[readParams+1])=="-fueld" then
  408.       if not (readParams+1 < #args) then
  409.         printManual()
  410.         return false
  411.       end
  412.       if not isDirection(args[readParams+2]) then
  413.         printDirs("Command Line Arguments", args[readParams+1], args[readParams+2])
  414.         return false
  415.       end
  416.       configuration.fuelDropDir=args[readParams+2]
  417.       readParams=readParams+2
  418.     elseif string.lower(args[readParams+1])=="-torchs" then
  419.       if not (readParams+1 < #args) then
  420.         printManual()
  421.         return false
  422.       end
  423.       if not isDirection(args[readParams+2]) then
  424.         printDirs("Command Line Arguments", args[readParams+1], args[readParams+2])
  425.         return false
  426.       end
  427.       configuration.torchSuckDir=args[readParams+2]
  428.       readParams=readParams+2
  429.     elseif string.lower(args[readParams+1])=="-itemd" then
  430.       if not (readParams+1 < #args) then
  431.         printManual()
  432.         return false
  433.       end
  434.       if not isDirection(args[readParams+2]) then
  435.         printDirs("Command Line Arguments", args[readParams+1], args[readParams+2])
  436.         return false
  437.       end
  438.       configuration.itemDropDir=args[readParams+2]
  439.       readParams=readParams+2
  440.     elseif string.lower(args[readParams+1])=="-ignores" then
  441.       if not (readParams+1 < #args) then
  442.         printManual()
  443.         return false
  444.       end
  445.       if not isDirection(args[readParams+2]) then
  446.         printDirs("Command Line Arguments", args[readParams+1], args[readParams+2])
  447.         return false
  448.       end
  449.       configuration.ignoreDir=args[readParams+2]
  450.       readParams=readParams+2
  451.     else
  452.       printManual()
  453.       return false
  454.     end
  455.   end
  456.   return true
  457. end
  458.  
  459.  
  460. -- RETURNS THE CONFIGURATION OF THE PROGRAM, ACCOUNTING FOR THE CONFIG FILE AND THE COMMAND LINE ARGUMENTS
  461. function getConfig(filename, args)
  462.   local configuration=getDefaultConfig()
  463.   -- TRY TO PROCESS THE CONFIG FILE, ABORT IF THIS FAILS (THE RETURN VALUE IS FALSE)
  464.   if not processConfigFile(configuration, filename) then
  465.     return nil
  466.   end
  467.   -- TRY TO PROCESS THE COMMAND LINE ARGUMENTS, ABORT IF THIS FAILS (THE RETURN VALUE IS FALSE)
  468.   if not processCommandLine(configuration, args) then
  469.     return nil
  470.   end
  471.   return configuration
  472. end
  473.  
  474. -- ---------------------------------------------------------- --
  475. -- END OF THE FUNCTIONS RELATING TO THE CONFIG OF THE PROGRAM --
  476. -- ---------------------------------------------------------- --
  477.  
  478. -- -------------------------------------------------- --
  479. -- START OF THE FUNCTIONS SHARED BETWEEN ALL PROGRAMS --
  480. -- -------------------------------------------------- --
  481.  
  482. -- CHECKS IF THE TURTLE NEEDS RESTOCKING
  483. function needsRestocking(configuration)
  484.   if configuration.requiresFuel then
  485.     if turtle.getFuelLevel() < configuration.returnFuel then
  486.       return true
  487.     end
  488.   end
  489.   if turtle.getItemCount(15)~=0 or turtle.getItemCount(16)~=0 then
  490.     return true
  491.   end
  492.   if configuration.placeTorches and turtle.getItemCount(1)==0 then
  493.     return true
  494.   end
  495.   return false
  496. end
  497.  
  498.  
  499. -- EMPTIES THE INVENTORY AND RESTOCKS THE INVENTORY
  500. function dropoffAndRestock(configuration, ignoreBlocks, restockIgnore, dropTorches, dropIgnore)
  501.   tempL=configuration.currentL
  502.   tempT=configuration.currentT
  503.   tempS=configuration.currentS
  504.   if configuration.singleChest then
  505.     moveToLocation(configuration, 1, 1, 0)
  506.   else
  507.     moveToLocation(configuration, tempL, tempT, 0)
  508.   end
  509.   if restockIgnore then
  510.     dropoff(configuration, 0, dropTorches, dropIgnore)
  511.   else
  512.     dropoff(configuration, ignoreBlocks, dropTorches, dropIgnore)
  513.   end
  514.   restock(configuration, ignoreBlocks, restockIgnore)
  515.   moveToLocation(configuration, tempL, tempT, tempS)
  516. end
  517.  
  518.  
  519. -- EMPTIES THE INVENTORY
  520. function dropoff(configuration, ignoreBlocks, dropTorches, dropIgnore)
  521.   -- DROP THE TORCHES
  522.   turtle.select(1)
  523.   local torchSlot=0
  524.   if configuration.placeTorches then
  525.     torchSlot=1
  526.     if dropTorches then
  527.       api_turtleExt.drop(configuration.torchSuckDir)
  528.     end
  529.   end
  530.   -- DROP THE EXTRAS OF THE IGNORE BLOCKS
  531.   api_turtleExt.turnTo(configuration.itemDropDir)
  532.   for i=1+torchSlot,torchSlot+ignoreBlocks do
  533.     turtle.select(i)
  534.     api_turtleExt.drop(api_turtleExt.turnedDir(configuration.itemDropDir), math.max(turtle.getItemCount(i)-1, 0))
  535.   end
  536.   -- DROP THE OTHER ITEMS
  537.   for i=torchSlot+ignoreBlocks+1,16 do
  538.     turtle.select(i)
  539.     api_turtleExt.drop(api_turtleExt.turnedDir(configuration.itemDropDir), turtle.getItemCount(i))
  540.   end
  541.   api_turtleExt.turnFrom(configuration.itemDropDir)
  542.   turtle.select(1)
  543.   -- DROP THE IGNORE BLOCKS
  544.   if dropIgnore then
  545.     api_turtleExt.turnTo(configuration.ignoreDir)
  546.     for i=1+torchSlot,torchSlot+ignoreBlocks do
  547.       turtle.select(i)
  548.       api_turtleExt.drop(api_turtleExt.turnedDir(configuration.ignoreDir), 1)
  549.     end
  550.     api_turtleExt.turnFrom(configuration.ignoreDir)
  551.   end
  552. end
  553.  
  554.  
  555. -- RESTOCKS THE TORCHES
  556. function restockTorches(configuration)
  557.   turtle.select(1)
  558.   local dispMsg=true
  559.   local firstEmpty=2
  560.   -- CALCULATE THE FIRST EMPTY SLOT
  561.   while ((turtle.getItemCount(firstEmpty)>0) and (firstEmpty<16)) do
  562.     firstEmpty = firstEmpty+1
  563.   end
  564.   -- ALIGN WITH THE TORCH CHEST AND SUCK A STACK
  565.   api_turtleExt.turnTo(configuration.torchSuckDir)
  566.   local tDir=api_turtleExt.turnedDir(configuration.torchSuckDir)
  567.   while turtle.getItemCount(firstEmpty)==0 do
  568.     if not api_turtleExt.suck(tDir) then
  569.       if dispMsg then
  570.         term.clear()
  571.         term.setCursorPos(1,1)
  572.         print("---------------------------------")
  573.         print("Please place some torches in a chest")
  574.         print("Chest Direction:  "..configuration.torchSuckDir)
  575.         print("Torches Required: "..(turtle.getItemSpace(1)+1))
  576.         print("---------------------------------")
  577.         dispMsg=false
  578.       end
  579.     else
  580.       dispMsg=true
  581.     end
  582.     sleep(0)
  583.   end
  584.   term.clear()
  585.   -- CHECK IF THERE ARE ACTUALLY TORCHES IN SLOT 1 AND DUMP ANY EXCESS TORCHES
  586.   if turtle.compareTo(firstEmpty) then
  587.     turtle.select(firstEmpty)
  588.     api_turtleExt.drop(tDir)
  589.     api_turtleExt.turnFrom(configuration.torchSuckDir)
  590.   else
  591.     turtle.select(firstEmpty)
  592.     api_turtleExt.drop(tDir)
  593.     turtle.select(1)
  594.     api_turtleExt.turnFrom(configuration.torchSuckDir)
  595.     api_turtleExt.drop(configuration.itemDropDir)
  596.     restockTorches(configuration)
  597.   end
  598.   turtle.select(1)
  599. end
  600.  
  601.  
  602. -- RESTOCKS THE INVENTORY
  603. function restock(configuration, ignoreBlocks, restockIgnore)
  604.   turtle.select(1)
  605.   local torchSlot=0
  606.   -- RESTOCK THE TORCHES
  607.   if configuration.placeTorches then
  608.     torchSlot=1
  609.     restockTorches(configuration)
  610.   end
  611.   -- RESTOCK THE IGNORE BLOCKS
  612.   if restockIgnore then
  613.     api_turtleExt.turnTo(configuration.ignoreDir)
  614.     local tDir=api_turtleExt.turnedDir(configuration.ignoreDir)
  615.     for i=1+torchSlot,ignoreBlocks+torchSlot do
  616.       turtle.select(i)
  617.       api_turtleExt.suck(tDir)
  618.     end
  619.     api_turtleExt.turnFrom(configuration.ignoreDir)
  620.   end
  621.   -- RESTOCK THE FUEL
  622.   if configuration.requiresFuel then
  623.     api_turtleExt.refuel(configuration.fuelSuckDir, configuration.fuelDropDir, 16, configuration.departureFuel)
  624.   end
  625.   turtle.select(1)
  626. end
  627.  
  628.  
  629. -- CALCULATES HOW MUCH MOVES NEED TO BE MADE TO GO FROM ONE SHAFT TO ANOTHER
  630. function calculateMoves(configuration, numL, fromS, toS)
  631.   local lowS=fromS
  632.   local highS=toS
  633.   if highS < lowS then
  634.     lowS=toS
  635.     highS=fromS
  636.   end
  637.   if lowS==highS then
  638.     return 0
  639.   end
  640.   local requiredMoves=0
  641.   if highS==(configuration.numS+1) then
  642.     if numL % 2==1 then
  643.       requiredMoves=requiredMoves+configuration.evenLevelOffset
  644.     end
  645.     highS=highS-1
  646.   end
  647.   if lowS==0 then
  648.     requiredMoves=requiredMoves+configuration.firstShaftOffset
  649.     if numL % 2==0 then
  650.       requiredMoves=requiredMoves+configuration.evenLevelOffset
  651.     end
  652.     requiredMoves=requiredMoves+((configuration.blocksBetweenShafts+1)*(highS-1))
  653.   else
  654.     requiredMoves=requiredMoves+((configuration.blocksBetweenShafts+1)*(highS-lowS))
  655.   end
  656.   return requiredMoves
  657. end
  658.  
  659.  
  660. -- MOVES FROM THE CURRENT LOCATION TO ANOTHER
  661. function moveToLocation(configuration, newL, newT, newS)
  662.   if newL==configuration.currentL and newT==configuration.currentT then
  663.     if configuration.currentS < newS then
  664.       api_turtleExt.digAndMove(forward, calculateMoves(configuration, newL, configuration.currentS, newS), 0)
  665.     elseif newS < configuration.currentS then
  666.       api_turtleExt.digAndMove(api_turtleExt.reverseDir(forward), calculateMoves(configuration, newL, configuration.currentS, newS), 0)
  667.     end
  668.   else
  669.     local turnDir=configuration.nextDirT
  670.     local moves=newT-configuration.currentT
  671.     if configuration.currentT > newT then
  672.       turnDir=api_turtleExt.reverseDir(turnDir)
  673.       moves=configuration.currentT-newT
  674.     end
  675.     if configuration.currentS==0 then
  676.       api_turtleExt.digAndMove(forward, 1, 0)
  677.       api_turtleExt.turnTo(turnDir)
  678.     else
  679.       api_turtleExt.turnTo(back)
  680.       api_turtleExt.digAndMove(forward, calculateMoves(configuration, configuration.currentL, 0, configuration.currentS)-1, 0)
  681.       api_turtleExt.turnFrom(turnDir)
  682.     end
  683.     api_turtleExt.digAndMove(forward, configuration.centerRadius+1, 0)
  684.     if configuration.currentL < newL then
  685.       api_turtleExt.digAndMove(configuration.nextDirL, (newL-configuration.currentL)*(configuration.height+1), 0)
  686.     end
  687.     if configuration.currentL > newL then
  688.       api_turtleExt.digAndMove(api_turtleExt.reverseDir(configuration.nextDirL), (configuration.currentL-newL)*(configuration.height+1), 0)
  689.     end
  690.     if newT==configuration.currentT then
  691.       api_turtleExt.turnTo(back)
  692.     else
  693.       api_turtleExt.turnTo(turnDir)
  694.       for i=2, moves do
  695.         api_turtleExt.digAndMove(forward, 2*(configuration.centerRadius+1), 0)
  696.         api_turtleExt.turnTo(turnDir)
  697.       end
  698.     end
  699.     if newT==configuration.currentT then
  700.       turnDir=api_turtleExt.reverseDir(turnDir)
  701.     end
  702.     api_turtleExt.digAndMove(forward, configuration.centerRadius+1, 0)
  703.     if newS==0 then
  704.       api_turtleExt.turnTo(turnDir)
  705.       api_turtleExt.digAndMove(forward, 1, 0)
  706.       api_turtleExt.turnTo(back)
  707.     else
  708.       api_turtleExt.turnFrom(turnDir)
  709.       api_turtleExt.digAndMove(forward, calculateMoves(configuration, newL, 0, newS)-1, 0)
  710.     end
  711.   end
  712.   configuration.currentL=newL
  713.   configuration.currentT=newT
  714.   configuration.currentS=newS
  715. end
  716.  
  717.  
  718. -- REPORTS AND OBSTRUCTION IN ONE OF THE SHAFTS
  719. function reportObstruction(configuration, dir)
  720.   print("Encountered obstruction in:")
  721.   print(" LEV: "..configuration.currentL)
  722.   print(" TUN: "..configuration.currentT)
  723.   print(" SH : "..configuration.currentS.." ("..dir..")")
  724. end
  725.  
  726. -- ------------------------------------------------ --
  727. -- END OF THE FUNCTIONS SHARED BETWEEN ALL PROGRAMS --
  728. -- ------------------------------------------------ --
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement