SHOW:
|
|
- or go back to the newest paste.
1 | - | local a = peripheral.wrap("top") |
1 | + | local kinetic = peripheral.find "plethora:kinetic" |
2 | - | local modem = peripheral.wrap("right") |
2 | + | local modem = peripheral.find "modem" |
3 | - | local entity = peripheral.wrap("left") |
3 | + | local laser = peripheral.find "plethora:laser" |
4 | local sensor = peripheral.find "plethora:sensor" | |
5 | modem.open(70) | |
6 | ||
7 | local p = true | |
8 | ||
9 | - | local curX, curY, curZ = gps.locate() |
9 | + | local target = vector.new(gps.locate()) |
10 | - | function fly() |
10 | + | |
11 | - | while true do |
11 | + | local function calc_yaw_pitch(v) |
12 | - | a.launch(0,270,0.320) |
12 | + | local pitch = -math.atan2(v.y, math.sqrt(v.x * v.x + v.z * v.z)) |
13 | - | sleep(0.2) |
13 | + | local yaw = math.atan2(-v.x, v.z) |
14 | - | end |
14 | + | return math.deg(yaw), math.deg(pitch) |
15 | end | |
16 | - | function trim(val, max) |
16 | + | |
17 | - | if val > max then |
17 | + | local mob_names = { "Creeper", "Zombie", "Skeleton", "Blaze" } |
18 | - | return max |
18 | + | local mob_lookup = {} |
19 | - | end |
19 | + | for _, mob in pairs(mob_names) do |
20 | - | return val |
20 | + | mob_lookup[mob] = true |
21 | end | |
22 | ||
23 | - | fly, |
23 | + | local function calc_distance(entity) |
24 | - | function() |
24 | + | return math.sqrt(entity.x * entity.x + entity.y * entity.y + entity.z * entity.z) |
25 | - | while true do |
25 | + | |
26 | - | if p then |
26 | + | |
27 | - | local x, y, z = gps.locate() |
27 | + | local function sentry() |
28 | - | local xDev = curX - x |
28 | + | while true do |
29 | - | local yDev = curY - y |
29 | + | local mobs = sensor.sense() |
30 | - | local zDev = curZ - z |
30 | + | local nearest |
31 | - | if yDev+0.1 < 0 then |
31 | + | for _, mob in pairs(mobs) do |
32 | - | local power = trim(math.abs(yDev) / 6 ,0.1) |
32 | + | if mob_lookup[mob.name] then |
33 | - | print(power, "down", y, curY, power, yDev) |
33 | + | mob.distance = calc_distance(mob) |
34 | - | a.launch(0,90, power) |
34 | + | if nearest == nil or mob.distance < nearest.distance then |
35 | - | end |
35 | + | nearest = mob |
36 | - | if yDev-0.1 > 0 then |
36 | + | end |
37 | - | local power = trim(math.abs(yDev) / 6 ,0.1) |
37 | + | end |
38 | - | print(power, "up", y, curY, power, yDev) |
38 | + | end |
39 | - | a.launch(0,270, power) |
39 | + | if nearest then |
40 | - | end |
40 | + | local y, p = calc_yaw_pitch(vector.new(nearest.x, nearest.y, nearest.z)) |
41 | - | if xDev+0.1 < 0 then |
41 | + | laser.fire(y, p, 0.5) |
42 | - | local power = trim(math.abs(xDev) / 8 ,0.33) |
42 | + | sleep(0.2) |
43 | - | print(power, "west", x, curX, power, xDev) |
43 | + | else |
44 | - | a.launch(90,0, power) |
44 | + | sleep(0.5) |
45 | - | sleep(0.1) |
45 | + | end |
46 | - | a.launch(270,0, power * 0.75) |
46 | + | end |
47 | - | end |
47 | + | |
48 | - | if xDev-0.1 > 0 then |
48 | + | |
49 | - | local power = trim(math.abs(xDev) / 8 ,0.33) |
49 | + | local function fly() |
50 | - | print(power, "east", x, curX, power, xDev) |
50 | + | while true do |
51 | - | a.launch(270,0, power) |
51 | + | kinetic.launch(0,270,0.320) |
52 | - | sleep(0.1) |
52 | + | sleep(0.2) |
53 | - | a.launch(90,0, power * 0.75) |
53 | + | end |
54 | - | end |
54 | + | |
55 | - | if zDev+0.1 < 0 then |
55 | + | |
56 | - | local power = trim(math.abs(zDev) / 8 ,0.33) |
56 | + | |
57 | - | print(power, "north", z, curZ, power, zDev) |
57 | + | fly, |
58 | - | a.launch(180,0, power) |
58 | + | function() |
59 | - | sleep(0.1) |
59 | + | while true do |
60 | - | a.launch(0,0, power * 0.75) |
60 | + | local current = vector.new(gps.locate()) |
61 | - | end |
61 | + | local displacement = target - current |
62 | - | if zDev-0.1 > 0 then |
62 | + | if displacement:length() > 0.1 then |
63 | - | local power = trim(math.abs(zDev) / 8 ,0.33) |
63 | + | displacement = displacement + vector.new(0, 0.3, 0) |
64 | - | print(power, "south", z, curZ, power, zDev) |
64 | + | local y, p = calc_yaw_pitch(displacement) |
65 | - | a.launch(0,0, power) |
65 | + | local pow = math.min(displacement:length() * 0.08, 2.0) |
66 | - | sleep(0.1) |
66 | + | print(y, p, pow) |
67 | - | a.launch(180,0, power * 0.75) |
67 | + | kinetic.launch(y, p, pow) |
68 | - | end |
68 | + | end |
69 | - | end |
69 | + | sleep(1) |
70 | - | sleep(2) |
70 | + | end |
71 | - | end |
71 | + | end, |
72 | - | end, |
72 | + | function() |
73 | - | function() |
73 | + | while true do |
74 | - | while true do |
74 | + | event, side, frequency, replyFrequency, message, distance = os.pullEvent("modem_message") |
75 | - | event, side, frequency, replyFrequency, message, distance = os.pullEvent("modem_message") |
75 | + | if message == "up" then |
76 | - | p = false |
76 | + | target.y = target.y + 1 |
77 | - | if message == "up" then |
77 | + | elseif message == "down" then |
78 | - | curY = curY + 1 |
78 | + | target.y = target.y - 1 |
79 | - | a.launch(0,270,0.4) |
79 | + | elseif message == "north" then |
80 | - | sleep(0.4) |
80 | + | target.z = target.z - 1 |
81 | - | a.launch(0,90, 0.3) |
81 | + | elseif message == "south" then |
82 | - | elseif message == "down" then |
82 | + | target.z = target.z + 1 |
83 | - | curY = curY - 1 |
83 | + | elseif message == "west" then |
84 | - | a.launch(0,90, 0.4) |
84 | + | target.x = target.x - 1 |
85 | - | sleep(0.4) |
85 | + | elseif message == "east" then |
86 | - | a.launch(0,270,0.3) |
86 | + | target.x = target.x + 1 |
87 | - | elseif message == "north" then |
87 | + | end |
88 | - | curZ = curZ - 1 |
88 | + | end |
89 | - | a.launch(180,0, 0.15) |
89 | + | end, |
90 | - | sleep(0.4) |
90 | + | sentry |
91 | - | a.launch(0,0, 0.1) |
91 | + | ) |
92 | - | elseif message == "south" then |
92 | + | |
93 | - | curZ = curZ + 1 |
93 | + | --[[ |
94 | - | a.launch(0,0, 0.15) |
94 | + | local xDev = tarX - x |
95 | - | sleep(0.4) |
95 | + | local yDev = tarY - y |
96 | - | a.launch(180,0, 0.1) |
96 | + | local zDev = tarZ - z |
97 | - | elseif message == "west" then |
97 | + | if yDev+0.1 < 0 then |
98 | - | curX = curX - 1 |
98 | + | local power = math.min(math.abs(yDev) / 6 ,0.1) |
99 | - | a.launch(90,0, 0.15) |
99 | + | print(power, "down", y, tarY, power, yDev) |
100 | - | sleep(0.4) |
100 | + | kinetic.launch(0,90, power) |
101 | - | a.launch(270,0, 0.1) |
101 | + | end |
102 | - | elseif message == "east" then |
102 | + | if yDev-0.1 > 0 then |
103 | - | curX = curX + 1 |
103 | + | local power = math.min(math.abs(yDev) / 6 ,0.1) |
104 | - | a.launch(270,0, 0.15) |
104 | + | print(power, "up", y, tarY, power, yDev) |
105 | - | sleep(0.4) |
105 | + | kinetic.launch(0,270, power) |
106 | - | a.launch(90,0, 0.1) |
106 | + | end |
107 | - | end |
107 | + | if xDev+0.1 < 0 then |
108 | - | p = true |
108 | + | local power = math.min(math.abs(xDev) / 8 ,0.33) |
109 | - | end |
109 | + | print(power, "west", x, tarX, power, xDev) |
110 | - | end |
110 | + | kinetic.launch(90,0, power) |
111 | - | ) |
111 | + | sleep(0.1) |
112 | kinetic.launch(270,0, power * 0.75) | |
113 | end | |
114 | if xDev-0.1 > 0 then | |
115 | local power = math.min(math.abs(xDev) / 8 ,0.33) | |
116 | print(power, "east", x, tarX, power, xDev) | |
117 | kinetic.launch(270,0, power) | |
118 | sleep(0.1) | |
119 | kinetic.launch(90,0, power * 0.75) | |
120 | end | |
121 | if zDev+0.1 < 0 then | |
122 | local power = math.min(math.abs(zDev) / 8 ,0.33) | |
123 | print(power, "north", z, tarZ, power, zDev) | |
124 | kinetic.launch(180,0, power) | |
125 | sleep(0.1) | |
126 | kinetic.launch(0,0, power * 0.75) | |
127 | end | |
128 | if zDev-0.1 > 0 then | |
129 | local power = math.min(math.abs(zDev) / 8 ,0.33) | |
130 | print(power, "south", z, tarZ, power, zDev) | |
131 | kinetic.launch(0,0, power) | |
132 | sleep(0.1) | |
133 | kinetic.launch(180,0, power * 0.75) | |
134 | end | |
135 | end | |
136 | ]] |