guitarplayer616

WeightedGridSplit

Jan 21st, 2017
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.13 KB | None | 0 0
  1. --[[if not fs.exists("villageHouse.blueprint") then
  2.     shell.run("pastebin get ggYJsi1l villageHouse.blueprint")
  3. end
  4. shell.run("villageHouse.blueprint")]]
  5.  
  6. if not fs.exists("textutilsFIX") then
  7.     shell.run("pastebin get 3wguFBXn textutilsFIX")
  8. end
  9.  
  10. os.loadAPI("textutilsFIX")
  11.  
  12.  
  13. function save(table,fileself)
  14.     local h = fs.open(fileself,"w")
  15.     h.write(textutilsFIX.serialize(table))
  16.     h.close()
  17. end
  18.  
  19. function printGrid(grid)
  20.     for y = 0,#grid do
  21.         for z = 0,#grid[y] do
  22.             textutils.slowWrite(grid[y][z],1000)
  23.         end
  24.         print()
  25.     end
  26. end
  27.  
  28. function createWeight(instructions,starty,startz,finaly,finalz)
  29.     local weightGrid,ygrid,zgrid = {},{},{}
  30.    
  31.     for y = starty,finaly do
  32.         ygrid[y] = 0
  33.         weightGrid[y] = {}
  34.         for z = startz,finalz do
  35.             zgrid[z] = 0
  36.             weightGrid[y][z] = 0
  37.         end
  38.     end
  39.    
  40.    
  41.     for n = 1,#instructions do
  42.         assert(instructions[n],instructions[n])
  43.         local y,z = instructions[n][2],instructions[n][3]
  44.         if weightGrid[y] and weightGrid[y][z] then
  45.             weightGrid[y][z] = weightGrid[y][z] + 1
  46.             ygrid[y] = ygrid[y] + 1
  47.             zgrid[z] = zgrid[z] + 1
  48.         end
  49.     end
  50.  
  51.  
  52.     return weightGrid,ygrid,zgrid
  53. end
  54.  
  55.  
  56. function Class_Grid(instructions,starty,startz,finaly,finalz)
  57.     local self = {}
  58.     self.starty = starty
  59.     self.startz = startz
  60.     self.finaly = finaly
  61.     self.finalz = finalz
  62.     self.grid,self.ygrid,self.zgrid = createWeight(instructions,starty,startz,finaly,finalz)
  63.     self.splity = function(self)
  64.         local n = split(self.ygrid,self.starty,self.finaly)
  65.         local A2 = Class_Grid(instructions,self.starty,self.startz,n,self.finalz)
  66.         local B2 = Class_Grid(instructions,n+1,self.startz,self.finaly,self.finalz)
  67.         return A2,B2
  68.     end
  69.     self.splitz = function(self)
  70.         local n = split(self.zgrid,self.startz,self.finalz)
  71.         local A2 = Class_Grid(instructions,self.starty,self.startz,self.finaly,n)
  72.         local B2 = Class_Grid(instructions,self.starty,n+1,self.finaly,self.finalz)
  73.         return A2,B2
  74.     end
  75.     self.splitLongestSide = function(self)
  76.         local rel_z_size = self.finalz - self.startz
  77.         local rel_y_size = self.finaly - self.starty
  78.         if rel_y_size >= rel_z_size then
  79.             return self:splity()
  80.         else
  81.             return self:splitz()
  82.         end
  83.     end
  84.     return self
  85. end
  86.  
  87. function split(zgrid,startz,finalz)
  88.     local prevleft,prevright
  89.     for n = startz,finalz do
  90.         local left,right = 0,0
  91.         for z = startz,n do
  92.             left = left + zgrid[z]
  93.         end
  94.         for z = finalz,n+1,-1 do
  95.             assert(zgrid[z],zgrid[z])
  96.             right = right + zgrid[z]
  97.         end
  98.         if left>=right then
  99.             --[[print("left: ",left)
  100.             print("right: ",right)
  101.             print("n: ",n)
  102.             print("prevleft: ",prevleft)
  103.             print("prevright: ",prevright)]]
  104.             local split
  105.             assert(left)
  106.             assert(right)
  107.             if not prevleft and not prevright then
  108.                 print("SHORTCUT")
  109.                 return n
  110.             end
  111.             local opta = math.abs(left - right)
  112.             local optb = math.abs(prevleft - prevright)
  113.             if opta <= optb then
  114.                 split = n
  115.             elseif opta > optb then
  116.                 split = n - 1
  117.             end
  118.             return split
  119.         end
  120.         prevleft,prevright = left,right
  121.     end
  122. end
  123.  
  124. function nTurtleSplitOLD(n,A1)
  125.     --n can == 1,2,4,8,16
  126.     if n == 1 then
  127.         return A1
  128.     elseif n == 2 then
  129.         return A1:splitLongestSide()
  130.     elseif n == 4 then
  131.         local A2,B2 = A1:splitLongestSide()
  132.         local A3,B3 = A2:splitLongestSide()
  133.         local C3,D3 = B2:splitLongestSide()
  134.         return A3,B3,C3,D3
  135.     elseif n == 8 then
  136.         local A2,B2 = A1:splitLongestSide()
  137.         local A3,B3 = A2:splitLongestSide()
  138.         local C3,D3 = B2:splitLongestSide()
  139.         local A4,B4 = A3:splitLongestSide()
  140.         local C4,D4 = B3:splitLongestSide()
  141.         local E4,F4 = C3:splitLongestSide()
  142.         local G4,H4 = D3:splitLongestSide()
  143.         return A4,B4,C4,D4,E4,F4,G4,H4
  144.     elseif n == 16 then
  145.         local A2,B2 = A1:splitLongestSide()
  146.         local A3,B3 = A2:splitLongestSide()
  147.         local C3,D3 = B2:splitLongestSide()
  148.         local A4,B4 = A3:splitLongestSide()
  149.         local C4,D4 = B3:splitLongestSide()
  150.         local E4,F4 = C3:splitLongestSide()
  151.         local G4,H4 = D3:splitLongestSide()
  152.         local A5,B5 = A4:splitLongestSide()
  153.         local C5,D5 = B4:splitLongestSide()
  154.         local E5,F5 = C4:splitLongestSide()
  155.         local G5,H5 = D4:splitLongestSide()
  156.         local I5,J5 = E4:splitLongestSide()
  157.         local K5,L5 = F4:splitLongestSide()
  158.         local M5,N5 = G4:splitLongestSide()
  159.         local O5,P5 = H4:splitLongestSide()
  160.         return A5,B5,C5,D5,E5,F5,G5,H5,I5,J5,K5,L5,M5,N5,O5,P5
  161.     end
  162. end
  163.  
  164. --local A1 = Class_Grid(instructions,reference.starty,reference.startz,reference.finaly,reference.finalz)
  165.  --local startLocations = {nTurtleSplit(8,A1)}
  166. --save(A1,"A1")
  167.  
  168. function splice(A1,tab)
  169.     local a,b = A1:splitLongestSide()
  170.     table.insert(tab,a)
  171.     table.insert(tab,b)
  172. end
  173.  
  174. --[[local tab = {}
  175. splice(A1,tab)
  176. local tab2 = {}
  177. for i,obj in pairs(tab) do
  178.     splice(obj,tab2)
  179. end
  180. for i,v in pairs(tab) do
  181.     --print()
  182. end]]
  183.  
  184. function nTurtleSplit(n,A1)
  185.     n = tonumber(n)
  186.     n = math.log(n)/math.log(2)
  187.     if n == 0 then
  188.         return A1
  189.     end
  190.     assert(n)
  191.     local tab = {}
  192.     for i = 1,n do
  193.         tab[i] = {}
  194.         if i == 1 then
  195.             splice(A1,tab[i])
  196.         else
  197.             for _,obj in pairs(tab[i-1]) do
  198.                 splice(obj,tab[i])
  199.             end
  200.         end
  201.     end
  202.     return unpack(tab[n])
  203. end
  204.  
  205.  
  206. --save(tab,"tab")
  207.  
  208.  
  209. --save(startLocations,"startLocations")
  210. --[[for i,v in pairs(startLocations) do
  211.     assert(v.starty,i)
  212.     print(v.starty," ",v.startz," ",v.finaly," ",v.finalz)
  213. end
  214. print(A1.starty," ",A1.startz," ",A1.finaly," ",A1.finalz)
  215. print(A2.starty," ",A2.startz," ",A2.finaly," ",A2.finalz)
  216. print(B2.starty," ",B2.startz," ",B2.finaly," ",B2.finalz)]]
Add Comment
Please, Sign In to add comment