improvshark

Untitled

Mar 26th, 2022
1,522
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.10 KB | None | 0 0
  1. function Spaceship.stop_integrity_check(spaceship)
  2.   spaceship.check_flash_alpha = nil
  3.   spaceship.is_doing_check = nil
  4.   spaceship.is_doing_check_slowly = nil
  5.   spaceship.check_stage = nil
  6.   spaceship.pending_tiles = nil
  7.   spaceship.streamline = nil
  8.   if spaceship.integrity_valid and spaceship.check_tiles then
  9.     -- success
  10.     spaceship.count_empty_tiles = spaceship.check_count_empty_tiles
  11.     spaceship.check_count_empty_tiles = nil
  12.  
  13.     spaceship.known_tiles = table.deepcopy(spaceship.check_tiles)
  14.     spaceship.check_tiles = nil
  15.  
  16.     -- get the average for surface transfer
  17.     local min_x = nil
  18.     local max_x = nil
  19.     local min_y = nil
  20.     local max_y = nil
  21.     local floor_tiles = 0
  22.     local bulkhead_tiles = 0
  23.     local front_tiles = {} -- x:y
  24.     for x, x_tiles in pairs(spaceship.known_tiles) do
  25.       if min_x == nil or x < min_x then min_x = x end
  26.       if max_x == nil or x > max_x then max_x = x end
  27.       for y, status in pairs(x_tiles) do
  28.         if status == Spaceship.tile_status.floor_console_connected
  29.           or status == Spaceship.tile_status.bulkhead_console_connected then
  30.             if min_y == nil or y < min_y then min_y = y end
  31.             if max_y == nil or y > max_y then max_y = y end
  32.             if status == Spaceship.tile_status.floor_console_connected then
  33.               floor_tiles = floor_tiles + 1
  34.             else
  35.               bulkhead_tiles = bulkhead_tiles + 1
  36.             end
  37.             if (not front_tiles[x]) or y < front_tiles[x] then
  38.                front_tiles[x] = y
  39.             end
  40.         end
  41.       end
  42.     end
  43.     max_x = max_x + 1 -- whole tile
  44.     max_y = max_y + 1 -- whole tile
  45.     spaceship.known_floor_tiles = floor_tiles
  46.     spaceship.known_bulkhead_tiles = bulkhead_tiles
  47.     spaceship.known_bounds = {left_top = {x = min_x, y = min_y}, right_bottom={x = max_x, y = max_y}}
  48.     spaceship.known_tiles_average_x = math.floor((min_x + max_x)/2)
  49.     spaceship.known_tiles_average_y = math.floor((min_y + max_y)/2)
  50.     local front_tiles_by_y = {} -- y:count
  51.     local front_tiles_by_y_left = {} -- y:count
  52.     local front_tiles_by_y_right = {} -- y:count
  53.     local max_flat = 0
  54.     for x, y in pairs(front_tiles) do
  55.       front_tiles_by_y[y] = (front_tiles_by_y[y] or 0) + 1
  56.       if front_tiles_by_y[y] > max_flat then
  57.         max_flat = front_tiles_by_y[y]
  58.       end
  59.       if x < spaceship.known_tiles_average_x then
  60.         front_tiles_by_y_left[y] = (front_tiles_by_y_left[y] or 0) + 1
  61.       else
  62.         front_tiles_by_y_right[y] = (front_tiles_by_y_right[y] or 0) + 1
  63.       end
  64.     end
  65.     local width = max_x - min_x
  66.     -- max_flat == width = 0
  67.     -- max_flat == width / 3 = 1
  68.     local streamline_flatness = math.min(1, (1 - (max_flat-2) / (width-2)) * 1.5)
  69.     local streamline_left = math.min(1, 2 * (table_size(front_tiles_by_y_left)-1) / (math.max(0.5, (width-1) / 2.5)))
  70.     local streamline_right = math.min(1, 2 * (table_size(front_tiles_by_y_right)-1) / (math.max(0.5, (width-1) / 2.5)))
  71.     spaceship.streamline = (streamline_flatness + streamline_left + streamline_right
  72.       + 3 * math.min(streamline_flatness, math.min(streamline_left, streamline_right))) / 6
  73.     --spaceship.streamline = math.min(1, 3.5 * (table_size(front_tiles_by_y)-1) / width)
  74.     -- if it is symetrical then 1/2 would be max (excluding the -1)
  75.     -- use 1/3.5 as max so there can be a few flat areas
  76.     Log.trace("streamline flat "..streamline_flatness.." left "..streamline_left.." right "..streamline_right)
  77.     --Log.trace("streamline "..spaceship.streamline )
  78.     if Spaceship.is_on_own_surface(spaceship) then
  79.       Spaceship.find_own_surface_engines(spaceship)
  80.     end
  81.   else
  82.     spaceship.integrity_valid = false
  83.     spaceship.check_tiles = nil
  84.     spaceship.check_message = {"space-exploration.spaceship-check-message-failed-empty"}
  85.   end
  86.  
  87.   --spaceship.check_message = nil
  88.   if spaceship.console and spaceship.console.valid then
  89.     --spaceship.console.force.print("Spaceship integrity check complete.")
  90.   end
  91.  
  92.   Spaceship.check_integrity_stress(spaceship)
  93.  
  94.   if spaceship.is_launching then
  95.     Spaceship.launch(spaceship)
  96.   end
  97. end
Advertisement
Add Comment
Please, Sign In to add comment