happydude11209

hexagon and octagon constructors

Apr 7th, 2013
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.04 KB | None | 0 0
  1. -- The Hexagon code is still a little quirky when smaller than side length 10, but hey, it works
  2. -- I copy and pasted the movement functions from shape.lua so integrating should be a snap :)
  3. -- I am pretty happy I did this all in a day! I didn't include this in my local shape.lua and send a pull request
  4. -- I will do that in the morning unless you have put it into yours already
  5. -- Please note that round() is necessary to resolve irrational decimals to the minecraft block grid in both shapes
  6.  
  7. function round(num, idp)
  8.   local mult = 10^(idp or 0)
  9.   return math.floor(num * mult + 0.5) / mult
  10. end
  11.  
  12. function hexagon(sideLength)
  13.     local changex = sideLength / 2
  14.     local changey = round(math.sqrt(3) * changex, 0)
  15.     changex = round(changex, 0)
  16.     local counter = 0
  17.    
  18.     navigateTo(changex, 0)
  19.    
  20.     for currentSide = 1, 6 do
  21.         counter = 0
  22.        
  23.         if currentSide == 1 then
  24.             for placed = 1, sideLength do
  25.                 navigateTo(positionx + 1, positiony)
  26.                 placeBlock()
  27.             end
  28.         elseif currentSide == 2 then
  29.             navigateTo(positionx, positiony + 1)
  30.             while positiony <= changey do
  31.                 if counter == 0 or counter == 2 or counter == 4 then
  32.                     navigateTo(positionx + 1, positiony)
  33.                 end
  34.                 placeBlock()
  35.                 navigateTo(positionx, positiony + 1)
  36.                 counter = counter + 1
  37.                 if counter == 5 then
  38.                     counter = 0
  39.                 end
  40.             end
  41.         elseif currentSide == 3 then
  42.             while positiony <= (2 * changey) do
  43.                 if counter == 0 or counter == 2 or counter == 4 then
  44.                     navigateTo(positionx - 1, positiony)
  45.                 end
  46.                 placeBlock()
  47.                 navigateTo(positionx, positiony + 1)
  48.                 counter = counter + 1
  49.                 if counter == 5 then
  50.                     counter = 0
  51.                 end
  52.             end
  53.         elseif currentSide == 4 then
  54.             for placed = 1, sideLength do
  55.                 navigateTo(positionx - 1, positiony)
  56.                 placeBlock()
  57.             end
  58.         elseif currentSide == 5 then
  59.         navigateTo(positionx, positiony - 1)
  60.             while positiony >= changey do
  61.                 if counter == 0 or counter == 2 or counter == 4 then
  62.                     navigateTo(positionx - 1, positiony)
  63.                 end
  64.                 placeBlock()
  65.                 navigateTo(positionx, positiony - 1)
  66.                 counter = counter + 1
  67.                 if counter == 5 then
  68.                     counter = 0
  69.                 end
  70.             end
  71.         elseif currentSide == 6 then
  72.             while positiony >= 0 do
  73.                 if counter == 0 or counter == 2 or counter == 4 then
  74.                     navigateTo(positionx + 1, positiony)
  75.                 end
  76.                 placeBlock()
  77.                 navigateTo(positionx, positiony - 1)
  78.                 counter = counter + 1
  79.                 if counter == 5 then
  80.                     counter = 0
  81.                 end
  82.             end
  83.         end
  84.     end
  85.    
  86.     navigateTo(0, 0)
  87.     while facing ~= 0 do
  88.         turnLeftTrack()
  89.     end
  90. end
  91.  
  92. function octagon(sideLength)
  93.     local sideLength2 = sideLength - 1
  94.     local change = round(sideLength2 / math.sqrt(2), 0)
  95.    
  96.     navigateTo(change, 0)
  97.    
  98.     for currentSide = 1, 8 do
  99.         if currentSide == 1 then
  100.             for placed = 1, sideLength2 do
  101.                 navigateTo(positionx + 1, positiony)
  102.                 placeBlock()
  103.             end
  104.         elseif currentSide == 2 then
  105.             for placed = 1, change do
  106.                 navigateTo(positionx + 1, positiony + 1)
  107.                 placeBlock()
  108.             end
  109.         elseif currentSide == 3 then
  110.             for placed = 1, sideLength2 do
  111.                 navigateTo(positionx, positiony + 1)
  112.                 placeBlock()
  113.             end
  114.         elseif currentSide == 4 then
  115.             for placed = 1, change do
  116.                 navigateTo(positionx - 1, positiony + 1)
  117.                 placeBlock()
  118.             end
  119.         elseif currentSide == 5 then
  120.             for placed = 1, sideLength2 do
  121.                 navigateTo(positionx - 1, positiony)
  122.                 placeBlock()
  123.             end
  124.         elseif currentSide == 6 then
  125.             for placed = 1, change do
  126.                 navigateTo(positionx - 1, positiony - 1)
  127.                 placeBlock()
  128.             end
  129.         elseif currentSide == 7 then
  130.         for placed = 1, sideLength2 do
  131.                 navigateTo(positionx, positiony - 1)
  132.                 placeBlock()
  133.             end
  134.         elseif currentSide == 8 then
  135.             for placed = 1, change do
  136.                 navigateTo(positionx + 1, positiony - 1)
  137.                 placeBlock()
  138.             end
  139.         end
  140.     end
  141.    
  142.     navigateTo(0, 0)
  143.     while facing ~= 0 do
  144.     turnLeftTrack()
  145.     end
  146. end
  147.  
  148. function hexPrism(sideLength, height)
  149.     for z = 1, height do
  150.         hexagon(sideLength)
  151.         safeUp()
  152.     end
  153. end
  154.  
  155. function octPrism(sideLength, height)
  156.     for z = 1, height do
  157.         octagon(sideLength)
  158.         safeUp()
  159.     end
  160. end
Advertisement
Add Comment
Please, Sign In to add comment