Advertisement
ZNZNCOOP

RobotAPI

Dec 1st, 2014
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.49 KB | None | 0 0
  1. R= {}
  2. R.status= false
  3. R.X= 0
  4. R.Y= 0
  5. R.Z= 0
  6. R.Side= 0
  7.  
  8. R.SOUTH= 0 -- юг +z
  9. R.EAST= 3 -- восток +x
  10. R.NORTH= 2 -- север -z
  11. R.WEST= 1 -- запад  -x
  12.  
  13. function new(x,y,z,side)
  14.    R.X= x
  15.    R.Y= y
  16.    R.Z= z
  17.    R.Side= side
  18.    R.f= turtle.forward
  19.    R.tr= turtle.turnRight
  20.    R.tl= turtle.turnLeft
  21.    R.tu= turtle.up
  22.    R.td= turtle.down
  23.    turtle.forward= R.forward
  24.    turtle.turnRight= R.tRight
  25.    turtle.turnLeft= R.tLeft
  26.    turtle.up= R.tUp
  27.    turtle.down= R.tDown
  28.    R.status= true
  29.    return R
  30. end
  31.  
  32. function R.draw()
  33.    if (R.status == false) then error('initialize object') end
  34.    print(R.X,' ',R.Y,' ',R.Z,' ',R.Side)
  35. end
  36.  
  37. function R.forward()
  38.    if (R.status == false) then error('initialize object') end
  39.    if (R.f()) then
  40.       if (Side == 0) then
  41.          R.Z= R.Z+1
  42.       end
  43.       if (R.Side == 1) then
  44.          R.X= R.X-1
  45.       end
  46.       if (R.Side == 2) then
  47.          R.Z= R.Z-1
  48.       end
  49.       if (R.Side == 3) then
  50.          R.X= R.X+1
  51.       end
  52.    end
  53. end
  54.  
  55. function R.tRight()
  56.    if (R.status == false) then error('initialize object') end
  57.    R.Side= R.Side+1
  58.    R.tr()
  59.    if (R.Side > 3) then R.Side= 0 end
  60. end
  61.  
  62.  
  63. function R.tLeft()
  64.    if (R.status == false) then error('initialize object') end
  65.    R.Side= Side-1
  66.    R.tl()
  67.    if (R.Side < 0) then R.Side= 3 end
  68. end
  69.  
  70. function R.tUp()
  71.    if (R.status == false) then error('initialize object') end
  72.    if (R.tu()) then
  73.       R.Y= R.Y+1
  74.    end
  75. end
  76.  
  77. function R.tDown()
  78.    if (R.status == false) then error('initialize object') end
  79.    if (R.td()) then
  80.       R.Y= R.Y-1
  81.    end
  82. end
  83.  
  84. function R.getX()
  85.    if (R.status == false) then error('initialize object') end
  86.    if (R.X ~= nil) then return R.X else return 0 end
  87. end
  88.  
  89. function R.getY()
  90.    if (R.status == false) then error('initialize object') end
  91.    if (R.Y ~= nil) then return R.Y else return 0 end
  92. end
  93.  
  94. function R.getZ()
  95.    if (R.status == false) then error('initialize object') end
  96.    if (R.Z ~= nil) then return R.Z else return 0 end
  97. end
  98.  
  99. function R.goto(tox,toy,toz,side)
  100.    if (R.status == false) then error('initialize object') end
  101.    while true do
  102.       R.draw()
  103.       if (toy > R.Y) then turtle.digUp() turtle.up() end
  104.       if (toy < R.Y) then turtle.digDown() turtle.down() end
  105.       if (toy == R.Y) then break end
  106.    end
  107.    while true do
  108.       if (tox > R.X) then R.turnSide(3) turtle.dig() turtle.forward() end
  109.       if (tox < R.X) then R.turnSide(1) turtle.dig() turtle.forward() end
  110.       if (tox == R.X) then break end
  111.    end
  112.    while true do
  113.       if (toz > R.Z) then R.turnSide(0) turtle.dig() turtle.forward() end
  114.       if (toz < R.Z) then R.turnSide(2) turtle.dig() turtle.forward() end
  115.       if (toz == R.Z) then break end
  116.    end
  117.    R.turnSide(side)
  118. end
  119.  
  120. function R.digLeft()
  121.    if (R.status == false) then error('initialize object') end
  122.    turtle.turnLeft()
  123.    turtle.dig()
  124.    turtle.turnRight()
  125. end
  126.  
  127. function R.digRight()
  128.    if (R.status == false) then error('initialize object') end
  129.    turtle.turnRight()
  130.    turtle.dig()
  131.    turtle.turnLeft()
  132. end
  133.  
  134. function R.turnSide(sides)
  135.    if (R.status == false) then error('initialize object') end
  136.    while true do
  137.       if (R.Side == sides) then
  138.          break
  139.       end
  140.       turtle.turnRight()
  141.    end
  142. end
  143.  
  144. function R.close()
  145.    if (R.status == false) then error('initialize object') end
  146.    turtle.forward= R.f
  147.    turtle.turnRight= R.tr
  148.    turtle.turnLeft= R.tl
  149.    turtle.up= R.tu
  150.    turtle.down= R.td
  151.    R.status= false
  152. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement