Advertisement
NixillUmbreon

grid.lua

Sep 14th, 2014
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.96 KB | None | 0 0
  1. term = require("term")
  2. comp = require("component")
  3. w = comp.debug.getWorld()
  4.  
  5. function quit(msg)
  6.   print(msg)
  7.   os.exit()
  8. end
  9.  
  10. function getNumber(msg)
  11.   while (not value) do
  12.     print("Enter a number: " .. msg)
  13.     value = tonumber(term.read())
  14.   end
  15.   return math.floor(value)
  16. end
  17.  
  18. function getYesNo(msg)
  19.   while (value == nil) do
  20.     print("Yes or no: " .. msg)
  21.     v = string.lower(io.read())
  22.     if (v == "yes" or v == "y" or v == "true" or v == "t" or v == "1") then value = true
  23.     elseif (v == "no" or v == "n" or v == "false" or v == "f" or v == "0") then value = false end
  24.   end
  25.   return value
  26. end
  27.  
  28. -- Find starting coordinate
  29. sx = getNumber("X-coordinate of grid origin.")
  30. sy = getNumber("Y-coordinate of grid origin.")
  31. sz = getNumber("Z-coordinate of grid origin.")
  32.  
  33. -- Find grid size
  34. print("For the following three numbers, enter the number of points, not the number of blocks.")
  35. print("Use a negative number to go the opposite direction. Use 0 to not extend the grid this way.")
  36. print("Note that the program will exit if you enter zero for all three numbers.")
  37. gx = getNumber("Length to extend grid east.")
  38. gy = getNumber("Length to extend grid up.")
  39. gz = getNumber("Length to extend grid south.")
  40. if (gx == 0 and gy == 0 and gz == 0) then os.exit() end
  41.  
  42. -- Find blocks and grid lengths
  43. print("Now enter the block ID *number* and data *byte* to set the blocks at the points to.")
  44. bld = {id = getNumber("Block ID"), data = getNumber("Block data")}
  45. blx = {}
  46. bly = {}
  47. blz = {}
  48. dx = 0
  49. dy = 0
  50. dz = 0
  51. if gx ~= 0 then
  52.   print("Now enter the id and data for blocks along lines on the X axis.")
  53.   blx = {id = getNumber("Block ID"), data = getNumber("Block data")}
  54.   print("Now the distance between block centers of two consecutive intersecting points. It can't be 0 or negative.")
  55.   while dx < 1 do dx = getNumber("X distance") end
  56. end
  57. if gy ~= 0 then
  58.   print("Now enter the id and data for blocks along lines on the Y axis.")
  59.   bly = {id = getNumber("Block ID"), data = getNumber("Block data")}
  60.   print("Now the distance between block centers of two consecutive intersecting points. It can't be 0 or negative.")
  61.   while dy < 1 do dy = getNumber("Y distance") end
  62. end
  63. if gz ~= 0 then
  64.   print("Now enter the id and data for blocks along lines on the Z axis.")
  65.   blz = {id = getNumber("Block ID"), data = getNumber("Block data")}
  66.   print("Now the distance between block centers of two consecutive intersecting points. It can't be 0 or negative.")
  67.   while dz < 1 do dz = getNumber("Z distance") end
  68. end
  69.  
  70. -- Find total grid lengths
  71. lx = gx * dx
  72. ly = gy * dy
  73. lz = gz * dz
  74.  
  75. -- Start on the bottom northwest corner regardless of input values
  76. if (gx < 0) then
  77.   sx = sx + lx
  78.   gx = -gx
  79.   lx = -lx
  80. end
  81. if (gy < 0) then
  82.   sy = sy + ly
  83.   gy = -gy
  84.   ly = -ly
  85. end
  86. if (gz < 0) then
  87.   sz = sz + lz
  88.   gz = -gz
  89.   lz = -lz
  90. end
  91.  
  92. -- Draw x gridlines
  93. if (gx ~= 0) then
  94.   for cy = 0, gy do
  95.     y = sy + cy * dy
  96.     for cz = 0, gz do
  97.       z = sz + cz * dz
  98.       for cx = 0, gx-1 do
  99.         for x = sx + cx * dx, sx + (cx + 1) * dx - 1 do
  100.           w.setBlock(x, y, z, blx.id, blx.data)
  101.         end
  102.       end
  103.     end
  104.   end
  105. end
  106.  
  107. -- Draw y gridlines
  108. if (gy ~= 0) then
  109.   for cx = 0, gx do
  110.     x = sx + cx * dx
  111.     for cz = 0, gz do
  112.       z = sz + cz * dz
  113.       for cy = 0, gy-1 do
  114.         for y = sy + cy * dy, sy + (cy + 1) * dy - 1 do
  115.           w.setBlock(x, y, z, bly.id, bly.data)
  116.         end
  117.       end
  118.     end
  119.   end
  120. end
  121.  
  122. -- Draw z gridlines
  123. if (gz ~= 0) then
  124.   for cy = 0, gy do
  125.     y = sy + cy * dy
  126.     for cx = 0, gx do
  127.       x = sx + cx * dx
  128.       for cz = 0, gz-1 do
  129.         for z = sz + cz * dz, sz + (cz + 1) * dz - 1 do
  130.           w.setBlock(x, y, z, blz.id, blz.data)
  131.         end
  132.       end
  133.     end
  134.   end
  135. end
  136.  
  137. -- Draw grid intersections
  138. for cx = 0, gx do
  139.   x = sx + cx * dx
  140.   for cy = 0, gy do
  141.     y = sy + cy * dy
  142.     for cz = 0, gz do
  143.       z = sz + cz * dz
  144.       w.setBlock(x, y, z, bld.id, bld.data)
  145.     end
  146.   end
  147. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement