Advertisement
Doob

[OpenComputers] Rubik's Cube

Jan 26th, 2016
552
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.43 KB | None | 0 0
  1. local deb = component.proxy(component.list('debug')())
  2. local mod = component.proxy(component.list('modem')())
  3. local x, y, z, x1, y1, z1, x2, y2, z2, a, b, c, e, m
  4. local port, distance = 9991, 20
  5. local posX, posY, posZ = deb.getX(), deb.getY(), deb.getZ()
  6. local tSound = {'tile.piston.in', 'tile.piston.out'}
  7. local tCube = {}
  8.  
  9. local function fill(x1, y1, z1, x2, y2, z2, c) -- заполнение указанного региона определенным цветом
  10.   for x = x1, x2 do
  11.     for y = y1, y2 do
  12.       for z = z1, z2 do
  13.         if not tCube[x] then
  14.           tCube[x] = {}
  15.         end
  16.         if not tCube[x][y] then
  17.           tCube[x][y] = {}
  18.         end
  19.         tCube[x][y][z] = c
  20.       end
  21.     end
  22.   end
  23. end
  24.  
  25. local function draw(x1, y1, z1, x2, y2, z2) -- отрисовка региона
  26.   for x = x1, x2 do
  27.     for y = y1, y2 do
  28.       for z = z1, z2 do
  29.         if tCube[x][y][z] then
  30.           deb.runCommand('setblock ~'.. x ..' ~'.. y ..' ~'.. z ..' wool '.. tCube[x][y][z])
  31.         end
  32.       end
  33.     end
  34.   end
  35.   deb.runCommand('playsound '.. tSound[math.random(1, #tSound)] ..' @a '.. posX ..' '.. posY ..' '.. posZ ..' 2 '.. 1/math.random(1, 3))
  36. end
  37.  
  38. function rotator(x, y) -- принимает координаты и направление, возвращает новые координаты
  39.   x = x-(x*2)
  40.   return y, x
  41. end
  42.  
  43. local function action(n)
  44.   local tbl = {} -- копирование содержимого таблицы, лол
  45.   for x = -2, 2 do
  46.     for y = -2, 2 do
  47.       for z = -2, 2 do
  48.         if not tbl[x] then
  49.           tbl[x] = {}
  50.         end
  51.         if not tbl[x][y] then
  52.           tbl[x][y] = {}
  53.         end
  54.         tbl[x][y][z] = tCube[x][y][z]
  55.       end
  56.     end
  57.   end
  58.   -- команды вращения
  59.   if n%2 == 1 then
  60.     y1 = 1
  61.   else
  62.     y1 = -2
  63.   end
  64.   for y = y1, y1+1 do
  65.     for x = -2, 2 do
  66.       for z = -2, 2 do
  67.         x1, z1 = rotator(x, z)
  68.         if n == 1 or n == 2 then -- U, D
  69.           tCube[x][y][z] = tbl[x1][y][z1]
  70.         elseif n == 3 or n == 4 then -- R, L
  71.           tCube[x][z][y] = tbl[x1][z1][y]
  72.         elseif n == 5 or n == 6 then -- F, B
  73.           tCube[y][x][z] = tbl[y][x1][z1]
  74.         end
  75.       end
  76.     end
  77.   end
  78. end
  79.  
  80. local function shft(a, b)
  81.   for i = 1, b do
  82.     action(a)
  83.   end
  84. end
  85.  
  86. fill(-2, -2, -2, 2, 2, 2, 15)
  87. fill(-1, -1, -1, 1, 1, 1, nil)
  88. fill(-1, 2, -1, 1, 2, 1, 14)
  89. fill(-1, -2, -1, 1, -2, 1, 1)
  90. fill(2, -1, -1, 2, 1, 1, 5)
  91. fill(-2, -1, -1, -2, 1, 1, 11)
  92. fill(-1, -1, 2, 1, 1, 2, 0)
  93. fill(-1, -1, -2, 1, 1, -2, 4)
  94.  
  95. draw(-2, -2, -2, 2, 2, 2)
  96.  
  97. mod.open(port)
  98.  
  99. while true do
  100.   e = {computer.pullSignal()}
  101.   if e[5] < distance then
  102.     m = e[6]:sub(1, 1)
  103.     if e[6]:sub(2, 2) == "'" then
  104.       c = 3
  105.     else
  106.       c = 1
  107.     end
  108.     if m == "U" then
  109.       shft(1, c)
  110.       draw(-2, 1, -2, 2, 2, 2)
  111.     elseif m == "D" then
  112.       shft(2, c)
  113.       draw(-2, -2, -2, 2, -1, 2)
  114.     elseif m == "L" then
  115.       shft(3, c)
  116.       draw(-2, -2, 1, 2, 2, 2)
  117.     elseif m == "R" then
  118.       shft(4, c)
  119.       draw(-2, -2, -2, 2, 2, -1)
  120.     elseif m == "F" then
  121.       shft(5, c)
  122.       draw(1, -2, -2, 2, 2, 2)
  123.     elseif m == "B" then
  124.       shft(6, c)
  125.       draw(-2, -2, -2, -1, 2, 2)
  126.     elseif m == "C" then
  127.       computer.shutdown(true)
  128.     elseif m == "S" then
  129.       for i = 1, 25 do
  130.         shft(math.random(1, 6), math.random(1, 3))
  131.       end
  132.       draw(-2, -2, -2, 2, 2, 2)
  133.     end
  134.   end
  135. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement