open Types open Board let fast_board = ref [| |] let make_fastmap () = let m = make_fastlocarray (!board_width + 2) (!board_height + 2) Wall in for y = 1 to !board_height do for x = 1 to !board_width do let loc = fastloc_of_loc (x, y) in m.(loc) <- !board.(y).(x) done done; fast_board := m let is_blocked = function Wall | Water -> true | _ -> false let calculate_block_pts p = (* 0--10 *) let map = !fast_board in let b11 = is_blocked map.(p - fastloc_y_mult - 1) in let b12 = is_blocked map.(p - fastloc_y_mult ) in let b13 = is_blocked map.(p - fastloc_y_mult + 1) in let b21 = is_blocked map.(p - 1) in let b23 = is_blocked map.(p + 1) in let b31 = is_blocked map.(p + fastloc_y_mult - 1) in let b32 = is_blocked map.(p + fastloc_y_mult ) in let b33 = is_blocked map.(p + fastloc_y_mult + 1) in (* F0: #.# F1: #&# F2: # F3: #. & & &# # *) let f0 = b11 && b13 && not b12 || b11 && b31 && not b21 || b33 && b13 && not b23 || b33 && b31 && not b32 in let f1 = b21 && b23 || b12 && b32 in let f2 = b11 && b33 || b31 && b13 in let f3 = b12 && b31 && not b21 || b12 && b33 && not b23 || b21 && b13 && not b12 || b21 && b33 && not b32 || b23 && b31 && not b32 || b23 && b11 && not b12 || b32 && b13 && not b23 || b32 && b11 && not b21 in (if f0 then 1 else 0) + (if f1 then 4 else 0) + (if f2 then 3 else 0) + (if f3 then 2 else 0) let calculate_water_pts loc = (* 0--4 *) let map = !fast_board in let f dz = if not (is_blocked map.(loc + dz)) && map.(loc - dz) == Water then 1 else 0 in f 1 + f (-1) + f fastloc_y_mult + f (-fastloc_y_mult) let weight_map = ref ([||] : float fast_locarray) let map_weight loc = 1.0 +. (float (calculate_water_pts loc)) *. 3.0 +. (float (calculate_block_pts loc)) *. 0.2 let calculate_map_weight () = make_fastmap (); let m = make_fastlocarray (!board_width + 2) (!board_height + 2) 1e+100 in for y = 1 to !board_height do for x = 1 to !board_width do let loc = fastloc_of_loc (x, y) in m.(loc) <- map_weight loc done done; weight_map := m