Advertisement
Doob

[ComputerCraft] Rubik's Cube

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