Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- event = require("event")
- os = require("os")
- io = require("io")
- dist = {} -- ra, rb, rc
- satellite_positions = {
- ["663ae55c"]= {532, -742}, -- xa, ya
- ["3ea5849e"]= {533, -731}, -- xb, yb
- ["f4960e56"]= {540, -732} -- xc, yc
- }
- function get_position(address)
- for k, v in pairs(satellite_positions) do
- start_pos, end_pos = string.find(address, k, 0, 1)
- if start_pos ~= nil and end_pos ~= nil then
- return v
- end
- end
- end
- function print_dict(d)
- for k, v in pairs(d) do
- io.write(k)
- io.write(" ")
- io.write(v)
- io.write(" ")
- end
- io.write("\n")
- end
- function update_distance(code, address, side, old, new)
- distance = 0
- if old == 0 and new == 15 then
- distance = (side+1) * 3
- elseif old == 15 and new == 0 then
- distance = side * 3
- end
- dist[address] = distance
- pos_approx = calculate_position()
- io.write("\r")
- io.write(pos_approx.x)
- io.write(" ")
- io.write(pos_approx.y)
- end
- function calculate_position()
- tower_info = {}
- tower_order = {
- "663ae55c-7911-4c2f-8459-7b266bb2424c",
- "3ea5849e-8ff7-44c2-878b-dd99dcc0de0e",
- "f4960e56-4b89-4957-a460-c8f7c23d11b5"
- }
- for i, v in ipairs(tower_order) do
- if dist[v] == nil then
- return
- end
- tower_info[i] = {tower_id = v, tower_pos = get_position(v), distance = dist[v]}
- end
- ax1 = tower_info[1].tower_pos[1] - tower_info[1].distance -- ax1
- ax2 = tower_info[1].tower_pos[1] + tower_info[1].distance -- ax2
- ay1 = tower_info[1].tower_pos[2] - tower_info[1].distance -- ay1
- ay2 = tower_info[1].tower_pos[2] + tower_info[1].distance -- ay2
- bx1 = tower_info[2].tower_pos[1] - tower_info[2].distance -- bx1
- bx2 = tower_info[2].tower_pos[1] + tower_info[2].distance -- bx2
- by1 = tower_info[2].tower_pos[2] - tower_info[2].distance -- by1
- by2 = tower_info[2].tower_pos[2] + tower_info[2].distance -- by2
- cx1 = tower_info[3].tower_pos[1] - tower_info[3].distance -- cx1
- cx2 = tower_info[3].tower_pos[1] + tower_info[3].distance -- cx2
- cy1 = tower_info[3].tower_pos[2] - tower_info[3].distance -- cy1
- cy2 = tower_info[3].tower_pos[2] + tower_info[3].distance -- cy2
- metrics = {
- {diff = (ax1 - bx1), val = (ax1 + bx1)/2},
- {diff = (ax1 - bx2), val = (ax1 + bx2)/2},
- {diff = (ax2 - bx1), val = (ax2 + bx1)/2},
- {diff = (ax2 - bx2), val = (ax2 + bx2)/2},
- {diff = (ay1 - by1), val = (ay1 + by1)/2},
- {diff = (ay1 - by2), val = (ay1 + by2)/2},
- {diff = (ay2 - by1), val = (ay2 + by1)/2},
- {diff = (ay2 - by2), val = (ay2 + by2)/2}
- }
- t1t2_dx = math.abs(tower_info[1].tower_pos[1] - tower_info[2].tower_pos[1])
- t1t2_dy = math.abs(tower_info[1].tower_pos[2] - tower_info[2].tower_pos[2])
- prefer_x = t1t2_dx > t1t2_dy
- -- print(prefer_x and "prefer" or "don't prefer")
- -- print(tower_info[1].tower_id)
- best_reading = 0
- for k, v in ipairs(metrics) do
- if k <= 4 and prefer_x then
- if best_reading == 0 or math.abs(v.diff) < math.abs(metrics[best_reading].diff) then
- best_reading = k
- end
- elseif k > 4 and (not prefer_x) then
- if best_reading == 0 or math.abs(v.diff) < math.abs(metrics[best_reading].diff) then
- best_reading = k
- end
- end
- end
- measurement_precision_1 = math.abs(metrics[best_reading].diff)
- estimated_position_1 = metrics[best_reading].val
- metrics2 = {
- {diff = (ax1 - cx1), val = (ax1 + cx1)/2},
- {diff = (ax1 - cx2), val = (ax1 + cx2)/2},
- {diff = (ax2 - cx1), val = (ax2 + cx1)/2},
- {diff = (ax2 - cx2), val = (ax2 + cx2)/2},
- {diff = (ay1 - cy1), val = (ay1 + cy1)/2},
- {diff = (ay1 - cy2), val = (ay1 + cy2)/2},
- {diff = (ay2 - cy1), val = (ay2 + cy1)/2},
- {diff = (ay2 - cy2), val = (ay2 + cy2)/2}
- }
- best_reading = 0
- for k, v in ipairs(metrics2) do
- if k > 4 and prefer_x then
- -- previous best reading was looking for x, now look for y
- if best_reading == 0 or math.abs(v.diff) < math.abs(metrics2[best_reading].diff) then
- best_reading = k
- end
- elseif k <= 4 and (not prefer_x) then
- -- previous best reading was looking for y, now look for x
- if best_reading == 0 or math.abs(v.diff) < math.abs(metrics2[best_reading].diff) then
- best_reading = k
- end
- end
- end
- measurement_precision_2 = math.abs(metrics2[best_reading].diff)
- estimated_position_2 = metrics2[best_reading].val
- if prefer_x then
- -- x first
- return {x=estimated_position_1, y=estimated_position_2}
- else
- -- y first
- return {x=estimated_position_2, y=estimated_position_1}
- end
- end
- event.listen("redstone_changed", update_distance)
- while 1 do
- os.sleep(1)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement