Advertisement
Dumfing

Scanner

Jan 8th, 2021
1,018
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. event = require("event")
  2. os = require("os")
  3. io = require("io")
  4.  
  5. dist = {} -- ra, rb, rc
  6.  
  7. satellite_positions = {
  8.   ["663ae55c"]= {532, -742}, -- xa, ya
  9.   ["3ea5849e"]= {533, -731}, -- xb, yb
  10.   ["f4960e56"]= {540, -732}  -- xc, yc
  11. }
  12.  
  13. function get_position(address)
  14.   for k, v in pairs(satellite_positions) do
  15.     start_pos, end_pos = string.find(address, k, 0, 1)
  16.     if start_pos ~= nil and end_pos ~= nil then
  17.       return v
  18.     end
  19.   end
  20. end
  21.  
  22.  
  23. function print_dict(d)
  24.   for k, v in pairs(d) do
  25.     io.write(k)
  26.     io.write(" ")
  27.     io.write(v)
  28.     io.write(" ")
  29.   end
  30.   io.write("\n")
  31. end
  32.  
  33. function update_distance(code, address, side, old, new)
  34.   distance = 0
  35.   if old == 0 and new == 15 then
  36.     distance = (side+1) * 3
  37.   elseif old == 15 and new == 0 then
  38.     distance = side * 3
  39.   end
  40.  
  41.   dist[address] = distance
  42.   pos_approx = calculate_position()
  43.   io.write("\r")
  44.   io.write(pos_approx.x)
  45.   io.write(" ")
  46.   io.write(pos_approx.y)
  47.  
  48. end
  49.  
  50. function calculate_position()
  51.   tower_info = {}
  52.  
  53.   tower_order = {
  54.     "663ae55c-7911-4c2f-8459-7b266bb2424c",
  55.     "3ea5849e-8ff7-44c2-878b-dd99dcc0de0e",
  56.     "f4960e56-4b89-4957-a460-c8f7c23d11b5"
  57.   }
  58.  
  59.  
  60.   for i, v in ipairs(tower_order) do
  61.     if dist[v] == nil then
  62.       return
  63.     end
  64.     tower_info[i] = {tower_id = v, tower_pos = get_position(v), distance = dist[v]}
  65.   end
  66.  
  67.   ax1 = tower_info[1].tower_pos[1] - tower_info[1].distance  -- ax1
  68.   ax2 = tower_info[1].tower_pos[1] + tower_info[1].distance  -- ax2
  69.   ay1 = tower_info[1].tower_pos[2] - tower_info[1].distance  -- ay1
  70.   ay2 = tower_info[1].tower_pos[2] + tower_info[1].distance  -- ay2
  71.   bx1 = tower_info[2].tower_pos[1] - tower_info[2].distance  -- bx1
  72.   bx2 = tower_info[2].tower_pos[1] + tower_info[2].distance  -- bx2
  73.   by1 = tower_info[2].tower_pos[2] - tower_info[2].distance  -- by1
  74.   by2 = tower_info[2].tower_pos[2] + tower_info[2].distance  -- by2
  75.   cx1 = tower_info[3].tower_pos[1] - tower_info[3].distance  -- cx1
  76.   cx2 = tower_info[3].tower_pos[1] + tower_info[3].distance  -- cx2
  77.   cy1 = tower_info[3].tower_pos[2] - tower_info[3].distance  -- cy1
  78.   cy2 = tower_info[3].tower_pos[2] + tower_info[3].distance  -- cy2
  79.  
  80.   metrics = {
  81.     {diff = (ax1 - bx1), val = (ax1 + bx1)/2},
  82.     {diff = (ax1 - bx2), val = (ax1 + bx2)/2},
  83.     {diff = (ax2 - bx1), val = (ax2 + bx1)/2},
  84.     {diff = (ax2 - bx2), val = (ax2 + bx2)/2},
  85.     {diff = (ay1 - by1), val = (ay1 + by1)/2},
  86.     {diff = (ay1 - by2), val = (ay1 + by2)/2},
  87.     {diff = (ay2 - by1), val = (ay2 + by1)/2},
  88.     {diff = (ay2 - by2), val = (ay2 + by2)/2}
  89.   }
  90.   t1t2_dx = math.abs(tower_info[1].tower_pos[1] - tower_info[2].tower_pos[1])
  91.   t1t2_dy = math.abs(tower_info[1].tower_pos[2] - tower_info[2].tower_pos[2])
  92.   prefer_x = t1t2_dx > t1t2_dy
  93.   -- print(prefer_x and "prefer" or "don't prefer")
  94.   -- print(tower_info[1].tower_id)
  95.   best_reading = 0
  96.   for k, v in ipairs(metrics) do
  97.     if k <= 4 and prefer_x then
  98.       if best_reading == 0 or math.abs(v.diff) < math.abs(metrics[best_reading].diff) then
  99.         best_reading = k
  100.       end
  101.     elseif k > 4 and (not prefer_x) then
  102.       if best_reading == 0 or math.abs(v.diff) < math.abs(metrics[best_reading].diff) then
  103.         best_reading = k
  104.       end
  105.     end
  106.   end
  107.   measurement_precision_1 = math.abs(metrics[best_reading].diff)
  108.   estimated_position_1 = metrics[best_reading].val
  109.  
  110.   metrics2 = {
  111.     {diff = (ax1 - cx1), val = (ax1 + cx1)/2},
  112.     {diff = (ax1 - cx2), val = (ax1 + cx2)/2},
  113.     {diff = (ax2 - cx1), val = (ax2 + cx1)/2},
  114.     {diff = (ax2 - cx2), val = (ax2 + cx2)/2},
  115.     {diff = (ay1 - cy1), val = (ay1 + cy1)/2},
  116.     {diff = (ay1 - cy2), val = (ay1 + cy2)/2},
  117.     {diff = (ay2 - cy1), val = (ay2 + cy1)/2},
  118.     {diff = (ay2 - cy2), val = (ay2 + cy2)/2}
  119.   }
  120.   best_reading = 0
  121.  
  122.   for k, v in ipairs(metrics2) do
  123.     if k > 4 and prefer_x then
  124.       -- previous best reading was looking for x, now look for y
  125.       if best_reading == 0 or math.abs(v.diff) < math.abs(metrics2[best_reading].diff) then
  126.         best_reading = k
  127.       end
  128.     elseif k <= 4 and (not prefer_x) then
  129.       -- previous best reading was looking for y, now look for x
  130.       if best_reading == 0 or math.abs(v.diff) < math.abs(metrics2[best_reading].diff) then
  131.         best_reading = k
  132.       end
  133.     end
  134.   end
  135.   measurement_precision_2 = math.abs(metrics2[best_reading].diff)
  136.   estimated_position_2 = metrics2[best_reading].val
  137.   if prefer_x then
  138.     -- x first
  139.     return {x=estimated_position_1, y=estimated_position_2}
  140.   else
  141.     -- y first
  142.     return {x=estimated_position_2, y=estimated_position_1}
  143.   end
  144. end
  145.  
  146. event.listen("redstone_changed", update_distance)
  147. while 1 do
  148.   os.sleep(1)
  149. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement