PonyKuu

Mastermine 0.3

May 8th, 2013
827
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.75 KB | None | 0 0
  1. -- Mastermine is a Master part of turtle swarm quarry made by PonyKuu
  2. -- It uses my master API v0.2a
  3.  
  4. -- Version 0.3
  5.  
  6. os.loadAPI ("master")
  7.  
  8. -- *******************************************************************************************
  9. -- * Module Distribution *
  10. -- *******************************************************************************************
  11.  
  12. -- Plain distribution just assigning the mining locations side-by-side.
  13. -- It tries to pack more modules in the hole, so it distributes all the modules along the longest direction
  14. local function plainDistribution (start, dimensions, taskTable)
  15. if dimensions.x < dimensions.z then
  16. for i = 0, dimensions.z-1 do
  17. local mineLocation = {x = start.x, y = start.y, z = start.z + i, f = 0}
  18. table.insert (taskTable, master.makeTask ("Mine", mineLocation, {length = dimensions.x, depth = dimensions.y}))
  19. end
  20. else
  21. for i = 0, dimensions.x-1 do
  22. local mineLocation = {x = start.x + i, y = start.y, z = start.z, f = 1}
  23. table.insert (taskTable, master.makeTask ("Mine", mineLocation, {length = dimensions.z, depth = dimensions.y}))
  24. end
  25. end
  26. end
  27.  
  28.  
  29. -- this one is splits the area to two areas. axis is the axis that would be split
  30. local function splitArea (start, dimensions, axis)
  31. assert (type(start) == "table", "Bad starting coordinates: Table required, got "..type(start), 2)
  32. assert (type(dimensions) == "table", "Bad quarry dimensions: Table required, got "..type(dimensions), 2)
  33. assert (type(axis) == "string", "Bad axis: String required, got "..type(axis), 2)
  34. -- a little function to copy a table
  35. local function tCopy (tTable)
  36. local newTable = {}
  37. for k, v in pairs (tTable) do
  38. newTable[k] = v
  39. end
  40. return newTable
  41. end
  42. -- make two copies of each table and change the corresponding fields.
  43. local startOne = tCopy (start)
  44. local dimensionsOne = tCopy (dimensions)
  45. dimensionsOne[axis] = math.floor(dimensions[axis]/2)
  46.  
  47. local startTwo = tCopy (start)
  48. startTwo[axis] = start[axis] + math.floor(dimensions[axis]/2)
  49. local dimensionsTwo = tCopy (dimensions)
  50. dimensionsTwo[axis] = math.ceil(dimensions[axis]/2)
  51.  
  52. return startOne, dimensionsOne, startTwo, dimensionsTwo
  53. end
  54.  
  55. -- This one tries to distribute all the available modules. Minimum slice length is 5
  56. local function distribute (start, dimensions, moduleCount, taskTable)
  57. -- if we can pack all the modules in one layer, just do it!
  58. if moduleCount <= dimensions.x or moduleCount <= dimensions.z then
  59. plainDistribution (start, dimensions, taskTable)
  60. -- Also use plain distribution if quarry is less than 8x8
  61. elseif dimensions.x <= 10 and dimensions.z <= 10 then
  62. plainDistribution (start, dimensions, taskTable)
  63. -- in any other case, divide the area into two pieces and call distribute again
  64. -- division is done by the longest dimension
  65. elseif dimensions.z < dimensions.x then
  66. local startOne, dimensionsOne, startTwo, dimensionsTwo = splitArea (start, dimensions, "x")
  67. distribute (startOne, dimensionsOne, math.floor(moduleCount/2), taskTable)
  68. distribute (startTwo, dimensionsTwo, math.ceil(moduleCount/2), taskTable)
  69. else
  70. local startOne, dimensionsOne, startTwo, dimensionsTwo = splitArea (start, dimensions, "z")
  71. distribute (startOne, dimensionsOne, math.floor(moduleCount/2), taskTable)
  72. distribute (startTwo, dimensionsTwo, math.ceil(moduleCount/2), taskTable)
  73. end
  74. end
  75.  
  76. -- *******************************************************************************************
  77. -- * User Input Validation *
  78. -- *******************************************************************************************
  79.  
  80. local tArgs = {...}
  81.  
  82. if #tArgs ~= 6 then
  83. print ("Usage: mastermine <xstart> <ystart> <zstart> <xsize> <ysize> <zsize>")
  84. return
  85. end
  86.  
  87. -- convert all the parameters into numbers
  88. for index, value in ipairs (tArgs) do
  89. tArgs[index] = tonumber (value)
  90. assert (type(tArgs[index] == "number", "Usage: mastermine <xstart> <ystart> <zstart> <ysize> <xsize> <zsize>", -1))
  91. end
  92.  
  93. -- and insert them into tables
  94. local quarryStart = {
  95. x = tArgs[1],
  96. y = tArgs[2],
  97. z = tArgs[3]
  98. }
  99. local quarryDimensions = {
  100. x = tArgs[4],
  101. y = tArgs[5],
  102. z = tArgs[6]
  103. }
  104.  
  105. -- *******************************************************************************************
  106. -- * Initialization *
  107. -- *******************************************************************************************
  108.  
  109. -- Let's set up the equipment configuration (it is default, but meh) We need to know how many modules we have before distributing them
  110. master.setEquipment {Fuel = 1, Stuff = 2, Turtle = 3}
  111. local modules = math.min (turtle.getItemCount(1), turtle.getItemCount(2), turtle.getItemCount(3))
  112.  
  113.  -- Calculate the navigation zones.
  114. local naviX = quarryStart.x + math.ceil(quarryDimensions.x / 2)
  115. local naviZ = quarryStart.z + math.ceil(quarryDimensions.z / 2)
  116. local naviHeight = quarryStart.y + 3
  117. master.setNavigation {x = naviX, z = naviZ, height = naviHeight}
  118.  
  119. -- set up a table with all the mining tasks
  120. local mineTasks = {}
  121. -- Distribute all the modules
  122. distribute (quarryStart, quarryDimensions, modules, mineTasks)
  123.  
  124. local function mineMachine (ID)
  125. -- If there are tasks in the task table
  126. if #mineTasks > 0 then
  127. master.setState (ID, "Mining")
  128. -- remove one from table
  129. local nextTask = table.remove (mineTasks)
  130. -- Some debug output
  131. print ("Module ", ID, " is mining at {", nextTask.x, " ,", nextTask.y, " ,", nextTask.z, "}")
  132. -- And send that task
  133. return nextTask
  134. else
  135. -- No locations. Return home.
  136. master.setState(ID, "Returning")
  137. print ("Module ", ID, " is returning to Master")
  138. return master.makeReturnTask ()
  139. end
  140. end
  141.  
  142. -- Associate this function with type "Miner"
  143. master.setType("Miner", mineMachine)
  144. -- And initialize the master
  145. master.init("modulemine", 100, 1000, #mineTasks)
  146.  
  147. local function stateFunction ()
  148. return #mineTasks > 0
  149. end
  150. master.operate (stateFunction)
  151. print "Mining finished! Have a nice day!"
Advertisement
Add Comment
Please, Sign In to add comment