SHOW:
|
|
- or go back to the newest paste.
1 | local modules = peripheral.wrap "back" | |
2 | ||
3 | local function length(x, y, z) | |
4 | return math.sqrt(x * x + y * y + z * z) | |
5 | end | |
6 | ||
7 | local function round(num, dp) | |
8 | local mult = 10^(dp or 0) | |
9 | return math.floor(num * mult + 0.5) / mult | |
10 | end | |
11 | ||
12 | local canvas = modules.canvas() | |
13 | ||
14 | -- Backported from Opus Neural ElytraFly since it has a nicer implementation than I do | |
15 | local function display(meta) | |
16 | if canvas then | |
17 | local w, h = canvas.getSize() | |
18 | if not canvas.group then | |
19 | canvas.group = canvas.addGroup({ w - 80, 15 }) | |
20 | canvas.group.addRectangle(0, 0, 60, 30, 0x00000033) | |
21 | canvas.pitch = canvas.group.addText({ 4, 5 }, '') -- , 0x202020FF) | |
22 | canvas.pitch.setShadow(true) | |
23 | canvas.pitch.setScale(.75) | |
24 | canvas.group2 = canvas.addGroup({ w - 10, 15 }) | |
25 | canvas.group2.addLines( | |
26 | { 0, 0 }, | |
27 | { 0, 180 }, | |
28 | { 5, 180 }, | |
29 | { 5, 0 }, | |
30 | 0x202020FF, | |
31 | 2) | |
32 | canvas.meter = canvas.group2.addRectangle(0, 0, 5, 1) | |
33 | end | |
34 | local size = math.abs(meta.pitch) -- math.ceil(math.abs(meta.pitch) / 9) | |
35 | local y = 0 | |
36 | local color = 0x202020FF | |
37 | if meta.pitch < 0 then | |
38 | y = size | |
39 | color = 0x808080FF | |
40 | end | |
41 | canvas.meter.setPosition(0, 90 - y) | |
42 | canvas.meter.setSize(5, size) | |
43 | canvas.meter.setColor(color) | |
44 | local my = meta.motionY | |
45 | if not meta.isInWater then my = my + 0.0784 end | |
46 | - | canvas.pitch.setText(string.format('Pitch: %s\nMotion Y: %s\nSpeed: %s', |
46 | + | canvas.pitch.setText(string.format('%s\nMotion Y: %s\nSpeed: %s', |
47 | - | math.floor(-meta.pitch), |
47 | + | _G.flight_mode or "normal", |
48 | round(my, 2), | |
49 | round(length(meta.motionX, my, meta.motionZ), 2))) | |
50 | end | |
51 | end | |
52 | ||
53 | --[[ | |
54 | local function pad(s, i) | |
55 | return ("%s %s%.1f"):format(s, i >= 0 and "+" or "", i) | |
56 | end | |
57 | ||
58 | local overlay = { | |
59 | function(meta) return pad("X:", meta.motionX) end, | |
60 | function(meta) return pad("Y:", meta.motionY) end, | |
61 | function(meta) return pad("Z:", meta.motionZ) end, | |
62 | function(meta) return pad(" ", length(meta.motionX, meta.motionY, meta.motionZ)) end, | |
63 | function(meta) return pad("P:", meta.power) end | |
64 | } | |
65 | ||
66 | local objects | |
67 | local function draw_overlay(meta) | |
68 | if not objects then | |
69 | objects = {} | |
70 | local w, h = canv.getSize() | |
71 | for ix in pairs(overlay) do | |
72 | objects[ix] = canv.addText({w - 40, ix * 10 + 5}, "") | |
73 | objects[ix].setColor(0xFFFFFFFF) | |
74 | end | |
75 | end | |
76 | for ix, f in pairs(overlay) do | |
77 | objects[ix].setText(f(meta)) | |
78 | end | |
79 | end | |
80 | ]] | |
81 | ||
82 | local function get_power(meta) | |
83 | local power = 4 | |
84 | if meta.isElytraFlying or meta.isFlying then power = 1 end | |
85 | if meta.isSneaking then power = 4 end | |
86 | if _G.tps then | |
87 | power = power * (20 / _G.tps) | |
88 | end | |
89 | return math.min(power, 4) | |
90 | end | |
91 | ||
92 | local function get_meta() | |
93 | local meta = modules.getMetaByName "gollark" | |
94 | meta.power = get_power(meta) | |
95 | display(meta) | |
96 | return meta | |
97 | end | |
98 | ||
99 | local function calc_yaw_pitch(v) | |
100 | local x, y, z = v.x, v.y, v.z | |
101 | local pitch = -math.atan2(y, math.sqrt(x * x + z * z)) | |
102 | local yaw = math.atan2(-x, z) | |
103 | - | modules.launch(meta.yaw, meta.pitch, meta.power) |
103 | + | return math.deg(yaw), math.deg(pitch) |
104 | end | |
105 | ||
106 | local x, y, z | |
107 | ||
108 | local function run() | |
109 | while true do | |
110 | local meta = get_meta() | |
111 | ||
112 | while (not _G.stop_flight) and (meta.isSneaking or meta.isFlying or meta.isElytraFlying) do | |
113 | - | end |
113 | + | local mode = _G.flight_mode or "normal" |
114 | print(mode) | |
115 | if mode == "normal" then | |
116 | modules.launch(meta.yaw, meta.pitch, meta.power) | |
117 | elseif mode == "brake" then | |
118 | local vel = vector.new(meta.motionX, meta.motionY, meta.motionZ) | |
119 | local y, p = calc_yaw_pitch(-vel) | |
120 | modules.launch(y, p, math.min(vel:length(), 4)) | |
121 | elseif mode == "align" and y then | |
122 | print(256 - y) | |
123 | local tdir = vector.new(-math.sin(math.rad(meta.yaw)), 0.1 * math.pow(256 - y, 3) / math.abs(256 - y), math.cos(math.rad(meta.yaw))) | |
124 | print(tdir) | |
125 | local y, p = calc_yaw_pitch(tdir) | |
126 | modules.launch(y, p, meta.power) | |
127 | end | |
128 | sleep(0.1) | |
129 | meta = get_meta() | |
130 | end | |
131 | ||
132 | if meta.motionY < -0.4 then | |
133 | modules.launch(0, 270, meta.power / 2) | |
134 | print("boost") | |
135 | end | |
136 | ||
137 | sleep(0.4) | |
138 | end | |
139 | end | |
140 | ||
141 | local function do_gps() | |
142 | while true do | |
143 | x, y, z = gps.locate() | |
144 | sleep(0.2) | |
145 | end | |
146 | end | |
147 | ||
148 | parallel.waitForAll(do_gps, run) |