Advertisement
GRxRedZero

Computercraft - Goxel Builder

Jan 24th, 2021
1,303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.40 KB | None | 0 0
  1. filename = ("/goxels/converted.txt")
  2. mx = 0
  3. my = 0
  4. mz = 1
  5. cx = {}
  6. cy = {}
  7. cz = {}
  8. -- Holt die Länge aller Seiten X, Y, Z
  9. function readGoxelFile()
  10.   file = fs.open(filename, "r")
  11.   local abgerufen = false
  12.   repeat -- Schleife bis zum Dateiende
  13.     local line = file.readLine()
  14.     local x = 1
  15.     if abgerufen == false then
  16.       mx = (string.len(line))
  17.       abgerufen = true
  18.     end
  19.     if line then --Ist noch was in der Line
  20.       -- ist die Zeilenlänge größer als X
  21.       if string.len(line) > x then
  22.         if line ~= nil then
  23.           --print(string.len(line))
  24.           my = my + 1
  25.         end
  26.       else
  27.         mz = mz + 1    
  28.       end
  29.       x = 1
  30.     end
  31.   until line == nil
  32. end
  33.  
  34. function getLuaList(nx, ny, nz)
  35.   file = fs.open(filename, "r")  
  36.   --liest den ersten 2-Dim Block
  37.   counter_z= 1
  38.   for z = 1, nz, 1 do
  39.   cz[counter_z] = {}
  40.   counter_y = 1
  41.   for y = 1, ny, 1 do
  42.     cy[counter_y] = {}
  43.     local line = file.readLine()
  44.     counter_x = 1
  45.     for x = 1, nx*2, 2 do  
  46.       cx[counter_x] = {}
  47.       -- Fügt X Werte in die Tabelle
  48.       cx[counter_x] = string.sub(line,x,x)
  49.       counter_x = counter_x + 1
  50.     end
  51.     table.insert(cy[counter_y], cx) -- Fügt cy in cx
  52.   end
  53.   -- Es muss eine Zeile übersprungen werden
  54.   table.insert(cz[counter_z], cy)
  55.   local line = file.readLine()
  56.   end
  57. end
  58.  
  59. readGoxelFile()
  60. mx = ((mx) / 2)
  61. mz = (mz)
  62. my = my / mz
  63. -- Richtigen Werte
  64. real_x = mx
  65. real_y = my
  66. real_z = mz
  67.  
  68. -- Größe der Matrix wird festgelegt
  69. local n_x, n_y, n_z = real_x+1, real_y+1, real_z+1
  70. local n_xy = n_x * n_y
  71.  
  72. -- Setzt an die gewünschte Position einen Wert
  73. function setValue(t, x, y, z, value)
  74.   assert(x > 0 and x < n_x)
  75.   assert(y > 0 and y < n_y)
  76.   assert(z > 0 and z < n_z)
  77.   t[((z-1) * n_xy) + ((y-1) * n_z) + x] = value
  78. end
  79.  
  80. -- Holt sich den Wert aus einer Position
  81. function getValue(t, x, y, z)
  82.   assert(x > 0 and x < n_x)
  83.   assert(y > 0 and y < n_y)
  84.   assert(z > 0 and z < n_z)
  85.   return t[((z-1) * n_xy) + ((y-1) * n_z) + x]
  86. end
  87.  
  88. -- Initialisiert und alloziert die 3-Dimensionale Matrix
  89. t = { }
  90. -- Fügt in den 3-Dimensionalen Würfel 0 auf jede Position
  91. for z = 1, real_z, 1 do
  92.   for y = 1, real_y, 1 do
  93.     for x = 1, real_x, 1 do
  94.       setValue(t, x, y, z, "0")
  95.     end
  96.   end
  97. end
  98.  
  99. mc = {}
  100. -- Ab jetzt exisiert die 3-Dimensionale Matrix
  101. -- Jetzt muss die Information wo ein block plaziert wird in die Matrix
  102.  
  103. function changeToRightTurtle()
  104.   turtle.turnRight()
  105.   turtle.dig()
  106.   turtle.forward()
  107.   turtle.turnRight()
  108.   turtle.dig()
  109. end
  110.  
  111. function changeToLeftTurtle()
  112.   turtle.turnLeft()
  113.   turtle.dig()
  114.   turtle.forward()
  115.   turtle.turnLeft()
  116.   turtle.dig()
  117. end
  118.  
  119. function goBackTurtle(mystatus)
  120.   --print("Goback")
  121.   -- -- -- Zurück zum endpunkt Anfang -- -- --
  122.   -- Y-Ebene beendet
  123.   -- Entweder die Y richtung zurück...
  124.   if mystatus == false then
  125.     -- wenn false dann y
  126.     turtle.turnLeft()
  127.     for y = 1, real_y, 1 do
  128.       turtle.dig()
  129.       turtle.forward()
  130.     end
  131.     turtle.turnRight()
  132.   end
  133.  
  134.   -- Oder die Y UND X Richtung zurück
  135.   if mystatus == true then
  136.     -- wenn true dann y und x
  137.     turtle.turnRight()
  138.     for y = 1, real_y, 1 do
  139.       turtle.dig()
  140.       turtle.forward()
  141.     end
  142.     turtle.turnLeft()
  143.     for x = 1, real_x-1, 1 do
  144.       turtle.dig()
  145.       turtle.forward()
  146.     end
  147.     turtle.turnLeft()
  148.     turtle.turnLeft()
  149.   end
  150. end
  151.  
  152. function setPixelInMatrix(nx, ny, nz)
  153.   local x_coord = 1
  154.   local y_coord = 1
  155.   local z_coord = 1
  156.   file = fs.open(filename, "r")
  157.   switch_direction = false
  158.   richtungswechsel = false
  159.   turtle.up()
  160.   repeat -- Schleife bis zum Dateiende
  161.  
  162.     local line = file.readLine()
  163.     local x = 1
  164.     if line then --Ist noch was in der Line
  165.       -- ist die Zeilenlänge größer als X
  166.       if string.len(line) > x then
  167.         if line ~= nil then
  168.           if y_coord > ny then
  169.             y_coord = 1
  170.           end
  171.           if x_coord > nx then
  172.             x_coord = 1
  173.           end
  174.  
  175.           -- RICHTUNG 1 ------
  176.           if richtungswechsel == false then
  177.             for x = 1, nx*2, 2 do  
  178.               --print("false",x+1, nx*2)
  179.               if tonumber(string.sub(line,x,x)) == 1 then
  180.                 --setValue(mc, x_coord, y_coord, z_coord, "1")
  181.                 -- Turtle Algo Start X --
  182.                 turtle.dig()
  183.                 turtle.placeDown()
  184.                 turtle.dig()
  185.                 if x+1 ~= nx*2 then
  186.                   turtle.forward()
  187.                 end
  188.                 -- Turtle Algo Ende X--
  189.               else
  190.                 --setValue(mc, x_coord, y_coord, z_coord, "0")
  191.                 turtle.dig()
  192.                 if x+1 ~= nx*2 then
  193.                   turtle.forward()
  194.                 end
  195.               end
  196.               x_coord = x_coord + 1
  197.             end
  198.           else
  199.             -- RICHTUNG 2 ------
  200.             for x = nx*2-1, 0, -2 do  
  201.               --print("true",x+1, nx*2-1)
  202.               if tonumber(string.sub(line,x,x)) == 1 then
  203.                 --setValue(mc, x_coord, y_coord, z_coord, "1")
  204.                 -- Turtle Algo Start X --
  205.                 turtle.dig()
  206.                 turtle.placeDown()
  207.                 turtle.dig()
  208.                 if 2 < x then
  209.                   turtle.forward()
  210.                 end
  211.                 -- Turtle Algo Ende X--
  212.               else
  213.                 --setValue(mc, x_coord, y_coord, z_coord, "0")
  214.                 turtle.dig()
  215.                 if 2 < x then
  216.                   turtle.forward()
  217.                 end
  218.               end
  219.               x_coord = x_coord + 1
  220.             end
  221.           end
  222.           if richtungswechsel == false then
  223.             richtungswechsel = true
  224.           else
  225.             richtungswechsel = false
  226.           end
  227.           -- Turtle Algo Start Y --
  228.           if switch_direction == false then
  229.             changeToRightTurtle()
  230.             switch_direction = true
  231.           else
  232.             changeToLeftTurtle()
  233.             switch_direction = false
  234.           end
  235.           -- Turtle Algo Ende Y --
  236.           y_coord = y_coord + 1      
  237.         end
  238.      
  239.       else
  240.         goBackTurtle(switch_direction)
  241.         z_coord = z_coord  + 1
  242.         print(z_coord)
  243.         -- Tuttle einen Hoch Start Z --
  244.         turtle.up()
  245.         switch_direction = false
  246.         -- Tuttle einen Hoch Ende  Z --
  247.       end
  248.      
  249.       x = 1
  250.     end
  251.   --
  252.   until line == nil
  253.   --
  254. end
  255.  
  256.  
  257. -- Setze die Blöcke in die Matrix
  258. setPixelInMatrix(real_x, real_y, real_z)
  259.  
  260.  
  261.  
  262. -- Nun beginnt das eigentliche Turtleprogramm
  263. function gogoturtle()
  264.   for z = 1, real_z, 1 do
  265.     -- Tuttle einen Hoch Start Z --
  266.     turtle.up()
  267.     switch_direction = false
  268.     -- Tuttle einen Hoch Ende  Z --
  269.     for y = 1, real_y, 1 do
  270.       for x = 1, real_x, 1 do
  271.         -- Turtle Algo Start X --
  272.         turtle.dig()
  273.         --print((getValue(mc, x, y, z)))
  274.         if tonumber(getValue(mc, x, y, z)) == 1 then
  275.           turtle.placeDown()
  276.         end
  277.         turtle.dig()
  278.         turtle.forward()
  279.         -- Turtle Algo Ende X--
  280.       end
  281.       -- Turtle Algo Start Y --
  282.       if switch_direction == false then
  283.         changeToRightTurtle()
  284.         switch_direction = true
  285.       else
  286.         changeToLeftTurtle()
  287.         switch_direction = false
  288.       end
  289.       -- Turtle Algo Ende Y --
  290.     end
  291.    
  292.     goBackTurtle(switch_direction)
  293.     -- -- -- Zurück zum endpunkt Ende -- -- --
  294.   end
  295. end
  296.  
  297. --gogoturtle()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement