View difference between Paste ID: BDfbN83d and cjNyJDDE
SHOW: | | - or go back to the newest paste.
1-
local robot = require("robot")
1+
local movement={}
2-
2+
local robot = require("robot")
3-
-- Angles: 1=North; 2=West; 3=South; 4=East (Relative to robot placement! Faceing when placed will allways be North!).
3+
 
4-
local angle = 1
4+
-- Angles: 1=North; 2=West; 3=South; 4=East (Relative to robot placement! Faceing when placed will allways be North!).
5-
5+
local angle = 1
6-
-- tries to move the Robot forward by @times times, if there is a obstacle, swing the current Tool.
6+
 
7-
function tryForward(times)
7+
-- tries to move the Robot forward by @times times, if there is a obstacle, swing the current Tool.
8-
  for i=1, times do
8+
function movement.tryForward(times)
9-
    while not robot.forward() do
9+
  for i=1, times do
10-
      robot.swing()
10+
    while not robot.forward() do
11-
    end
11+
      robot.swing()
12-
  end
12+
    end
13-
end
13+
  end
14-
14+
end
15-
-- tries to move the Robot upwards by @times times, if there is an obstacle, swing the current Tool.
15+
 
16-
function tryUp(times)
16+
-- tries to move the Robot upwards by @times times, if there is an obstacle, swing the current Tool.
17-
  for i=1, times do
17+
function movement.tryUp(times)
18-
    while not robot.up() do
18+
  for i=1, times do
19-
      robot.swingUp()
19+
    while not robot.up() do
20-
    end
20+
      robot.swingUp()
21-
  end
21+
    end
22-
end
22+
  end
23-
23+
end
24-
--tries to move the Robot downwards by @times times, if there is an obstacle, swing the current Tool.
24+
 
25-
function tryDown(times)
25+
--tries to move the Robot downwards by @times times, if there is an obstacle, swing the current Tool.
26-
  for i=1, times do
26+
function movement.tryDown(times)
27-
    while not robot.down() do
27+
  for i=1, times do
28-
      robot.swingDown()
28+
    while not robot.down() do
29-
    end
29+
      robot.swingDown()
30-
  end
30+
    end
31-
end
31+
  end
32-
32+
end
33-
-- turns the robot by @degree times in the Direction of @dir<1=Left; 2=Right>
33+
 
34-
-- Possible degrees: 1=90°; 2=180°; 3=270°; 4=360°; 5=450°...
34+
-- turns the robot by @degree times in the Direction of @dir<1=Left; 2=Right>
35-
-- keeps track of the robots current Angle.
35+
-- Possible degrees: 1=90°; 2=180°; 3=270°; 4=360°; 5=450°...
36-
function turn(degree, dir)
36+
-- keeps track of the robots current Angle.
37-
  local Degree = degree
37+
function movement.turn(degree, dir)
38-
  local Direction = dir
38+
  local Degree = degree
39-
  for i=1, Degree do
39+
  local Direction = dir
40-
    if Direction==1 then
40+
  for i=1, Degree do
41-
      robot.turnLeft()
41+
    if Direction==1 then
42-
      if angle<4 then
42+
      robot.turnLeft()
43-
        angle=angle+1
43+
      if angle<4 then
44-
      else
44+
        angle=angle+1
45-
        angle=1
45+
      else
46-
      end
46+
        angle=1
47-
    end
47+
      end
48-
    if Direction==2 then
48+
    end
49-
      robot.turnRight()
49+
    if Direction==2 then
50-
      if angle>1 then
50+
      robot.turnRight()
51-
        angle=angle-1
51+
      if angle>1 then
52-
      else
52+
        angle=angle-1
53-
        angle=4
53+
      else
54-
      end
54+
        angle=4
55-
    end
55+
      end
56-
  end
56+
    end
57-
  print(angle)
57+
  end
58-
end
58+
  print(angle)
59-
59+
end
60-
60+
 
61-
function returnHome(posX, posY, posZ, pointX, pointY, pointZ)
61+
 
62-
  local diffX = posX-pointX
62+
function movement.returnHome(posX, posY, posZ, pointX, pointY, pointZ)
63-
  local diffY = posY-pointY
63+
  local diffX = posX-pointX
64-
  local diffZ = posZ-pointZ
64+
  local diffY = posY-pointY
65-
  
65+
  local diffZ = posZ-pointZ
66-
  gotoY(diffY)
66+
 
67-
  sleep(1)
67+
  gotoY(diffY)
68-
  turnToAngle(4)
68+
  sleep(1)
69-
  gotoXZ(diffZ)
69+
  turnToAngle(4)
70-
  sleep(1)
70+
  gotoXZ(diffZ)
71-
  turnToAngle(3)
71+
  sleep(1)
72-
  gotoXZ(diffX)
72+
  turnToAngle(3)
73-
end
73+
  gotoXZ(diffX)
74-
  
74+
end
75-
-- Goes forward a given amount of @times. Warning! Does not set the Angle automatically!
75+
 
76-
local function gotoXZ(times)
76+
-- Goes forward a given amount of @times. Warning! Does not set the Angle automatically!
77-
  if times==0 then
77+
local function gotoXZ(times)
78-
    return true
78+
  if times==0 then
79-
  elseif times>0 then
79+
    return true
80-
    tryForward(times)
80+
  elseif times>0 then
81-
    return true
81+
    movement.tryForward(times)
82-
  end
82+
    return true
83-
end
83+
  end
84-
84+
end
85-
-- Goes up or down a set amount of @times, depending on if @times is pos. or neg.
85+
 
86-
local function gotoY(times)
86+
-- Goes up or down a set amount of @times, depending on if @times is pos. or neg.
87-
  if times==0 then
87+
local function gotoY(times)
88-
    return true
88+
  if times==0 then
89-
  elseif times>0 then
89+
    return true
90-
    tryDown(times)
90+
  elseif times>0 then
91-
    return true
91+
    movement.tryDown(times)
92-
  elseif times<0 then
92+
    return true
93-
    local _times = (-1*times)
93+
  elseif times<0 then
94-
    tryUp(_times)
94+
    local _times = (-1*times)
95-
    return true
95+
    movement.tryUp(_times)
96-
  end
96+
    return true
97-
end
97+
  end
98-
98+
end
99-
-- Turns form the current Angle towards @target Angle.
99+
 
100-
local function turnToAngle(target)
100+
-- Turns form the current Angle towards @target Angle.
101-
  if angle~=nil then
101+
local function turnToAngle(target)
102-
    if angle~=target then
102+
  if angle~=nil then
103-
      if angle>target then
103+
    if angle~=target then
104-
        turn((angle-target), 2)
104+
      if angle>target then
105-
        return true
105+
        movement.turn((angle-target), 2)
106-
      end
106+
        return true
107-
      if angle<target then
107+
      end
108-
        turn((target-angle), 1)
108+
      if angle<target then
109-
        return true
109+
        movement.turn((target-angle), 1)
110-
      end
110+
        return true
111-
    else
111+
      end
112-
      return true
112+
    else
113-
    end
113+
      return true
114-
  else
114+
    end
115-
    return false
115+
  else
116-
  end
116+
    return false
117-
end
117+
  end
118-
118+
end
119-
-- Forces to set the current Angle.(Not recommended!)
119+
 
120-
function setAngle(_angle)
120+
-- Forces to set the current Angle.(Not recommended!)
121-
  angle=_angle
121+
function movement.setAngle(_angle)
122-
end
122+
  angle=_angle
123
end
124
125
return movement