post-crate pathfinding [exists_crate to detect destroyed crates + oprimize pathfinding to not softlock

This commit is contained in:
Alexandre 2024-12-11 22:29:57 +01:00
parent 3a1f76cb01
commit fff0250bb4
8 changed files with 158 additions and 117 deletions

BIN
again

Binary file not shown.

BIN
again.cmi

Binary file not shown.

BIN
again.cmx

Binary file not shown.

231
again.ml
View File

@ -433,7 +433,7 @@ let generate_gain_map (gd : game_data) =
if (w > 0) || dir = 0 then begin if (w > 0) || dir = 0 then begin
let nx = l + w * (fst order.(dir)) let nx = l + w * (fst order.(dir))
and ny = c + w * (snd order.(dir)) in and ny = c + w * (snd order.(dir)) in
if is_valid nx ny lines cols then begin if is_valid nx ny lines cols && gd.laby.(l).(c) <> 3+gd.player_id then begin
res.(nx).(ny) <- res.(nx).(ny) + 1; res.(nx).(ny) <- res.(nx).(ny) + 1;
if (gd.laby.(l).(c) >= 3) then if (gd.laby.(l).(c) >= 3) then
res.(nx).(ny) <- res.(nx).(ny) + 1; res.(nx).(ny) <- res.(nx).(ny) + 1;
@ -684,59 +684,6 @@ let is_dead_all (dgs : danger_map) (x : int) (y : int) (t : float) (dt : float)
(* ---------------------------------------------------------------------------------------------------------------------------------------------------- *) (* ---------------------------------------------------------------------------------------------------------------------------------------------------- *)
(* ---------------------------------------------------------------------------------------------------------------------------------------------------- *) (* ---------------------------------------------------------------------------------------------------------------------------------------------------- *)
(*let goto_tile (gd : game_data) (dgs : danger_map) (x0 : int) (y0 : int) (end_x : int) (end_y : int) (stime : float) =
let lines = Array.length gd.laby
and cols = Array.length gd.laby.(0) in
let pid = gd.player_id in
let visited = Hashtbl.create 100 in
let q = Queue.create () in
let interval = Float.pow (0.9) (float_of_int gd.players.(gd.player_id).nspeed) in
(*Queue.add (x0, y0, stime +. interval, 4, 1) q ;*)
Queue.add (x0+1, y0, stime +. interval, 2) q ;
Queue.add (x0-1, y0, stime +. interval, 0) q ;
Queue.add (x0, y0+1, stime +. interval, 1) q ;
Queue.add (x0, y0-1, stime +. interval, 3) q ;
try
while not (Queue.is_empty q) do
let (x, y, ct, direction) = Queue.pop q in
if is_valid x y lines cols && gd.laby.(x).(y) <> 1 && gd.laby.(x).(y) <> 2 then begin (* within the map *)
if Hashtbl.find_opt visited (x, y) = None then begin (* has not been visited yet *)
Hashtbl.add visited (x, y) 1 ;
if
not (is_dead_all dgs x y ct interval false) &&
ct < stime +. 70. *. interval &&
not (Array.fold_left (fun acc (b : bomb) -> acc || (b.xy.x = x && b.xy.y = y)) false gd.bombs)
then begin (* is not lethal *)
if
(is_empty_lst dgs.explosionTimes.(x).(y)) && (* safe *)
(not (is_player_nearby gd x y 3 && amt_free_adj_spaces gd (gd.players.(pid).xy.x + fst order.(direction)) (gd.players.(pid).xy.y + snd order.(direction)) = 1)) &&
(direction = 4 || (advanced_pathfind gd dgs (gd.players.(pid).xy.x + fst order.(direction)) (gd.players.(pid).xy.y + snd order.(direction)) gd.players.(pid).xy.x gd.players.(pid).xy.y)) &&
(x = end_x && y = end_y)
then begin
raise (ReturnInt direction)
end;
(*Queue.add (x, y, ct +. interval, direction) q ;*)
if not (is_player_nearby gd x y 3 && amt_free_adj_spaces gd (gd.players.(pid).xy.x + fst order.(direction)) (gd.players.(pid).xy.y + snd order.(direction)) = 1) && not (x0 == x && y0 == y) then begin
for dir = 0 to 3 do
Queue.add (x + (fst order.(dir)), y + (snd order.(dir)), ct +. interval, direction) q ;
done;
end
end
end
end
done;
4 ;
with
| ReturnInt k -> k ;;*)
(* ---------------------------------------------------------------------------------------------------------------------------------------------------- *)
(* ---------------------------------------------------------------------------------------------------------------------------------------------------- *)
let contains_crate (gd : game_data) = let contains_crate (gd : game_data) =
Array.fold_left Array.fold_left
(fun b1 lst -> b1 || (Array.fold_left (fun b2 tile -> b2 || (tile = 2)) false lst)) false gd.laby ;; (fun b1 lst -> b1 || (Array.fold_left (fun b2 tile -> b2 || (tile = 2)) false lst)) false gd.laby ;;
@ -964,56 +911,148 @@ let move_crate (gd : game_data) (dgs : danger_map) =
(* ---------------------------------------------------------------------------------------------------------------------------------------------------- *) (* ---------------------------------------------------------------------------------------------------------------------------------------------------- *)
(* ---------------------------------------------------------------------------------------------------------------------------------------------------- *) (* ---------------------------------------------------------------------------------------------------------------------------------------------------- *)
(* let goto_tile (gd : game_data) (dgs : danger_map) (x0 : int) (y0 : int) (end_x : int) (end_y : int) (stime : float) = *) let bfs_for_land ?return_x:(retx=fodder) ?return_y:(rety=fodder) ?return_ok:(retfl=bodder) (skip_near : bool) (gd : game_data) (dgs : danger_map) (x0 : int) (y0 : int) (target_x : int) (target_y : int) ?leniency:(lenc=1) (stime : float) (minDist : int) (maxDist : int) =
(*let draw_random (gd : game_data) (infx : int) (infy : int) (lines : int) (cols : int) (dmin : int) =
(* get a random, valid tile *)
let xres = ref (-1)
and yres = ref (-1) in
while not (is_valid !xres !yres lines cols && gd.laby.(!xres).(!yres) >= 3 && gd.laby.(!xres).(!yres) <> 3 + gd.player_id && not (Array.fold_left (fun acc (b : bomb) -> acc || (b.xy.x = !xres && b.xy.y = !yres)) false gd.bombs)) do
xres := infx + Random.int (lines - infx) ;
yres := infy + Random.int (cols - infy) ;
done;
(!xres, !yres) ;;
let nloops = 70 ;;
let move_land_2 (gd : game_data) (dgs : danger_map) (gns : int array array) =
let lines = Array.length gd.laby let lines = Array.length gd.laby
and cols = Array.length gd.laby.(0) in and cols = Array.length gd.laby.(0) in
let visited = Hashtbl.create 100 in
let q = Queue.create () in
let interval = Float.pow (0.9) (float_of_int gd.players.(gd.player_id).nspeed) in
let pid = gd.player_id in
let needs_gtfo = not (is_empty_lst dgs.explosionTimes.(x0).(y0)) in
(*let nearest_player = min_dist_with_dash gd gd.players.(pid).xy.x gd.players.(pid).xy.y in*)
let nearest_player = min_dist_from_player gd gd.players.(pid).xy.x gd.players.(pid).xy.y in
let undead_end_tiles = generate_dead_end_map gd in
if gd.player_id = 4 then begin
for l = 0 to Array.length undead_end_tiles -1 do
for c = 0 to Array.length undead_end_tiles.(l) -1 do
if undead_end_tiles.(l).(c) >= 727 then
Printf.fprintf stderr "- "
else
Printf.fprintf stderr "%d " undead_end_tiles.(l).(c);
done;
Printf.fprintf stderr "\n";
done
end ;
Queue.add (x0, y0, stime +. interval, 4, 1) q ;
Queue.add (x0+1, y0, stime +. interval, 2, 1) q ;
Queue.add (x0-1, y0, stime +. interval, 0, 1) q ;
Queue.add (x0, y0+1, stime +. interval, 1, 1) q ;
Queue.add (x0, y0-1, stime +. interval, 3, 1) q ;
try
while not (Queue.is_empty q) do
let (x, y, ct, direction, polar) = Queue.pop q in
(*Printf.fprintf stderr "at (%d %d)\n" x y;*)
if is_valid x y lines cols && gd.laby.(x).(y) <> 1 && gd.laby.(x).(y) <> 2 then begin (* within the map *)
if Hashtbl.find_opt visited (x, y, polar) = None then begin (* has not been visited yet *)
Hashtbl.add visited (x, y, polar) 1 ;
if
not (is_dead_all dgs x y ct interval false) &&
ct < stime +. (float_of_int maxDist) *. interval &&
not (Array.fold_left (fun acc (b : bomb) -> acc || (b.xy.x = x && b.xy.y = y)) false gd.bombs) &&
polar <= 4
then begin (* is not lethal *)
if false && ct <= stime +. 3. *. interval then begin
Printf.fprintf stderr "(at %d %d) %b %b %b\n" x y
(ct >= stime +. (float_of_int minDist) *. interval) (* not too deep *)
(is_empty_lst dgs.explosionTimes.(x).(y)) (* safe *)
(needs_gtfo || undead_end_tiles.(x).(y) * 2 <= nearest_player) (* is not going to be an ez kill *)
end;
if
(ct >= stime +. (float_of_int minDist) *. interval) && (* not too deep *)
(is_empty_lst dgs.explosionTimes.(x).(y)) && (* safe *)
(needs_gtfo || undead_end_tiles.(x).(y) * 2 <= nearest_player) && (* is not going to be an ez kill *)
(skip_near || tile_distance gd x y target_x target_y <= lenc) (* close enough to target *)
then begin
retx := x ;
rety := y ;
raise (ReturnInt direction)
end;
if not (x0 = x && y0 = y) && (needs_gtfo || undead_end_tiles.(x).(y) * 2 <= nearest_player) then begin
for dir = 0 to 3 do
Queue.add (x + (fst order.(dir)), y + (snd order.(dir)), ct +. interval, direction, polar) q ;
done;
Queue.add (x, y, ct +. interval, direction, polar+1) q
end
end
end
end
done;
retfl := false ;
4 ;
with
| ReturnInt k ->
retfl := true ;
k ;;
let move_land (gd : game_data) (dgs : danger_map) (gn : int array array) =
let max_cols = Array.mapi
(fun i lne ->
Array.fold_left
(fun acc (idc, sco) ->
if sco > snd acc then
(idc, sco)
else
acc
)
(0, 0)
(Array.mapi (fun j v -> (j, v)) lne)
)
gn
in
let (xmax, ymax, _) = Array.fold_left
(fun (cxm, cym, cvm) (nx, ny, nv) ->
if nv > cvm then
(nx, ny, nv)
else
(cxm, cym, cvm)
)
(0, 0, 0)
(Array.mapi (fun i (j, v) -> (i, j, v)) max_cols)
in
let pid = gd.player_id in let pid = gd.player_id in
let cxi = gd.players.(pid).xy.x let cxi = gd.players.(pid).xy.x
and cyi = gd.players.(pid).xy.y in and cyi = gd.players.(pid).xy.y in
try if logg then Printf.fprintf stderr "going at (%d, %d) [score=%d]\n" xmax ymax gn.(xmax).(ymax);
for azertyiop = 0 to nloops -1 do
let (x, y) = draw_random gd 0 0 lines cols 4 in (* try to place a bomb *)
let result_0 = goto_tile gd dgs cxi cyi x y gd.dt in let is_safe = is_empty_lst dgs.explosionTimes.(cxi).(cyi) in
if result_0 <> 4 then begin let saved_p = simulate_bomb_deconstruct gd dgs cxi cyi gd.players.(pid).bomb_radius (gd.dt +. 5.5) in
if let is_good = ref false in
gd.players.(pid).bomb_to_place > 0 &&
not (Array.fold_left (fun acc (b : bomb) -> acc || (b.xy.x = cxi && b.xy.y = cyi)) false gd.bombs) && let result_bomb = bfs_for_land ~return_ok:is_good true gd dgs cxi cyi xmax ymax gd.dt 1 80 in
(is_empty_lst dgs.explosionTimes.(cxi).(cyi)) if is_safe && !is_good then begin
then begin if logg then Printf.fprintf stderr "kaboom\n" ;
if logg then Printf.fprintf stderr "trying...\n" ; action := 1 ;
let _ = simulate_bomb_deconstruct gd dgs cxi cyi gd.players.(pid).bomb_radius (gd.dt +. 5.5) in result_bomb
let result = goto_tile gd dgs cxi cyi x y gd.dt in end
if result <> 4 then begin else begin
action := 1 ; reverse_simulate_bomb dgs saved_p ;
raise (ReturnInt result)
end let res = bfs_for_land ~return_ok:is_good false gd dgs cxi cyi xmax ymax gd.dt 1 80 in
end; if !is_good then begin
raise (ReturnInt result_0) if logg then Printf.fprintf stderr "going to kaboom\n" ;
end res
done; end
4 else begin
with if logg then Printf.fprintf stderr "E\n" ;
| ReturnInt k -> k ;;*) let res = bfs_for_land ~leniency:20 false gd dgs cxi cyi xmax ymax gd.dt 1 80 in
res
end
end ;;
(* ---------------------------------------------------------------------------------------------------------------------------------------------------- *) (* ---------------------------------------------------------------------------------------------------------------------------------------------------- *)
(* ---------------------------------------------------------------------------------------------------------------------------------------------------- *) (* ---------------------------------------------------------------------------------------------------------------------------------------------------- *)
let exists_crate (gd : game_data) = let exists_crate (gd : game_data) (dgs : danger_map) =
Array.exists Array.exists
(fun line -> Array.exists (fun tile -> tile = 2) line) (fun line -> Array.exists (fun tile -> tile = 2) line)
gd.laby ;; gd.laby ;;
@ -1035,15 +1074,13 @@ print_dangers danger_data ;;*)
print_dangers danger_data ;;*) print_dangers danger_data ;;*)
let direction = ref 4 ;; let direction = ref 4 ;;
if exists_crate game_map then begin if exists_crate game_map danger_data then begin
if logg then Printf.fprintf stderr "Crates\n" ; if logg then Printf.fprintf stderr "Crates\n" ;
direction := move_crate game_map danger_data direction := move_crate game_map danger_data
end end
else begin else begin
if logg then Printf.fprintf stderr "No crates\n" ; if logg then Printf.fprintf stderr "No crates\n" ;
Printf.fprintf stderr "WIN\n" ; direction := move_land game_map danger_data gain_map
(*direction := move_land game_map danger_data gain_map *)
(*direction := move_land_2 game_map danger_data gain_map *)
end ;; end ;;
Printf.printf "%d %d" !direction !action ; Printf.printf "%d %d" !direction !action ;

BIN
again.o

Binary file not shown.

View File

@ -1,22 +1,25 @@
164.0 192.1625000000009
2 1
13 21 13 21
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 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1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
0 4
3 7 16 3 192.41370000000083
3 10 0 4 3 2 3 1 6 15 3 193.72590000000085
5 10 2 0 2 3 1 4 5 14 3 195.03810000000087
7 10 3 3 4 6 2 1 10 7 4 195.5400000000004
2
7 12 1 4 1 3 2 3
11 5 2 2 0 4 1 3
0 0

View File

@ -454,6 +454,7 @@ def execute_evenement(evenements, evenement, plateau, plateauCouleur, bombes, jo
ip, jp = prochain(i,j,direction) ip, jp = prochain(i,j,direction)
ajoute_evenement(evenements, [evenement[0]+TEMPS_PROPAGATION, EVENEMENT_PROPAGATION, ip, jp, direction, longueurFlammes-1, indJoueur]) ajoute_evenement(evenements, [evenement[0]+TEMPS_PROPAGATION, EVENEMENT_PROPAGATION, ip, jp, direction, longueurFlammes-1, indJoueur])
NB_TROUS = 20
TAILLE_TUILE = 40 TAILLE_TUILE = 40
HAUTEUR_JOUEUR = TAILLE_TUILE HAUTEUR_JOUEUR = TAILLE_TUILE
@ -528,7 +529,7 @@ def simulation(strategies):
dimensions = 13,21 dimensions = 13,21
positionsInitiales=[(1, 1), (dimensions[0]-2, dimensions[1]-2), (1, dimensions[1]-2), (dimensions[0]-2, 1)] positionsInitiales=[(1, 1), (dimensions[0]-2, dimensions[1]-2), (1, dimensions[1]-2), (dimensions[0]-2, 1)]
plateau = cree_plateau_initial(dimensions[0]-2, dimensions[1]-2, 20) plateau = cree_plateau_initial(dimensions[0]-2, dimensions[1]-2, NB_TROUS)
plateauCouleur = [[-1 for j in range(dimensions[1])] for i in range(dimensions[0])] plateauCouleur = [[-1 for j in range(dimensions[1])] for i in range(dimensions[0])]
evenements = [] evenements = []

View File

@ -1 +1 @@
4 0 3 1