fixed a major bug when parsing maze would kill any first chunk's cube

This commit is contained in:
Alexandre 2024-07-02 18:46:43 +02:00
parent 6da4f7853a
commit a92fdaaa2d
4 changed files with 190 additions and 7 deletions

BIN
a.out

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -124,7 +124,7 @@ type pt_2d = {mutable x : float ; mutable y : float} ;;
type coloredCube = {cube : pt_3d array ; red : int ; green : int ; blue : int} ;; type coloredCube = {cube : pt_3d array ; red : int ; green : int ; blue : int} ;;
let dyn_create i = let dyn_create i =
{tab = Array.make 10 i ; len = 0 ; memlen = 10} ;; {tab = Array.make 100 i ; len = 0 ; memlen = 100} ;;
let dyn_append arr elt = let dyn_append arr elt =
let fct x = let fct x =
@ -134,6 +134,8 @@ let dyn_append arr elt =
arr.tab.(0) arr.tab.(0)
in in
if arr.len = arr.memlen then begin if arr.len = arr.memlen then begin
Stdlib.print_endline "resized";
Unix.sleepf 0.5;
let newarr = Array.init (2 * arr.memlen) fct in let newarr = Array.init (2 * arr.memlen) fct in
arr.memlen <- 2 * arr.memlen; arr.memlen <- 2 * arr.memlen;
arr.tab <- newarr arr.tab <- newarr
@ -453,7 +455,7 @@ let draw_multiples_cubes_colored_hash (dyna : coloredCube dynamic) screen_wd scr
swap dyna.tab i !idmax; swap dyna.tab i !idmax;
done; done;
for i = 0 to n-1 do for i = 0 to n-1 do
(*Printf.printf "(%d, %d, %d)" (dyna.tab.(i).red) (dyna.tab.(i).green) (dyna.tab.(i).blue); (*Printf.printf "drawing (%f, %f, %f)" (dyna.tab.(i).cube.(0).x) (dyna.tab.(i).cube.(0).y) (dyna.tab.(i).cube.(0).z);
Stdlib.print_endline " ";*) Stdlib.print_endline " ";*)
draw_cube_p (dyna.tab.(i).cube) screen_wd screen_ht fov (dyna.tab.(i).red) (dyna.tab.(i).green) (dyna.tab.(i).blue) draw_cube_p (dyna.tab.(i).cube) screen_wd screen_ht fov (dyna.tab.(i).red) (dyna.tab.(i).green) (dyna.tab.(i).blue)
done ;; done ;;
@ -543,13 +545,13 @@ done ;;
let n_walls = ref (width*height*depth) ;; let n_walls = ref (width*height*depth) ;;
let ctc_one x = let ctc_one x =
if x > 0 then if x >= 0 then
x / chunk_size x / chunk_size
else else
x / chunk_size -1 ;; x / chunk_size -1 ;;
let ctcf_one x = let ctcf_one x =
if x > 0. then if x >= 0. then
int_of_float (x /. chunk_size_f) int_of_float (x /. chunk_size_f)
else else
int_of_float (x /. chunk_size_f) -1 ;; int_of_float (x /. chunk_size_f) -1 ;;
@ -600,6 +602,25 @@ let is_collision_hash (cam_coords : pt_3d) (cubes : coloredCube dynamic) =
done; done;
!res ;; !res ;;
let is_collision_hash_2 (cam_coords : pt_3d) (rcubes : (coloredCube dynamic) option) = match rcubes with
| None -> false
| Some cubes -> begin
let res = ref false in
let n = cubes.len in
let distances = Array.make n 0. in
for i = 0 to n-1 do
distances.(i) <- cube_dist cubes.tab.(i).cube;
done ;
for i = 0 to n-1 do
if not !res && distances.(i) < 2. then
res := is_collision_cube cam_coords cubes.tab.(i).cube
done;
!res
end ;;
let convert_laby laby = let convert_laby laby =
let width = Array.length laby let width = Array.length laby
and height = Array.length laby.(0) and height = Array.length laby.(0)
@ -626,6 +647,64 @@ let convert_laby laby =
done; done;
(cubes, reds, greens, blues) ;; (cubes, reds, greens, blues) ;;
let chunkify_2 laby sz =
let width = Array.length laby
and height = Array.length laby.(0)
and depth = Array.length laby.(0).(0) in
let cubes = Hashtbl.create 300 in
let add_to_table w h d r g b=
(*Printf.printf "(%d, %d, %d) (%d, %d, %d)\n" w h d cw ch cd;*)
let (cw, ch, cd) = coords_to_chunk (w*sz) (h*sz) (d*sz) in
match Hashtbl.find_opt cubes (cw, ch, cd) with
| None -> begin
Printf.printf "created cube (%d, %d, %d) (%d)\n" (w*sz) (h*sz) (d*sz) sz;
Printf.printf "in chunk (%d, %d, %d)\n" cw ch cd;
Stdlib.print_endline " ";
let dyna = dyn_create {cube = create_cube (w*sz) (h*sz) (d*sz) sz; red = r; green = g; blue = b} in
dyn_append dyna {cube = create_cube (w*sz) (h*sz) (d*sz) sz; red = r; green = g; blue = b};
Hashtbl.add cubes (cw, ch, cd) dyna
end
| Some dyna -> begin
Printf.printf "created cube (%d, %d, %d) (%d)\n" (w*sz) (h*sz) (d*sz) sz;
Printf.printf "in chunk (%d, %d, %d)\n" cw ch cd;
Stdlib.print_endline " ";
Hashtbl.remove cubes (cw, ch, cd);
dyn_append dyna {cube = create_cube (w*sz) (h*sz) (d*sz) sz; red = r; green = g; blue = b};
Hashtbl.add cubes (cw, ch, cd) dyna
end
in
for w = 0 to width-1 do
for h = 0 to height-1 do
for d = 0 to depth-1 do
if laby.(w).(h).(d) <> Free then begin
add_to_table w h d 220 220 220
end
done
done
done;
(*
let s = ref 0 in
for i = 0 to 2 do
for j = 0 to 2 do
for k = 0 to 2 do
let nc = ((Hashtbl.find cubes (i, j, k)).len) in
Printf.printf "len for (%d, %d, %d) : %d" i j k nc;
s := !s + nc;
Stdlib.print_endline " "
done
done
done;
Printf.printf "s = %d" !s;
Stdlib.print_endline " ";
*)
(*Unix.sleepf 100.0;*)
cubes ;;
let chunkify laby sz = let chunkify laby sz =
let width = Array.length laby let width = Array.length laby
and height = Array.length laby.(0) and height = Array.length laby.(0)
@ -780,13 +859,116 @@ let rec move_cam_hash (cubes : coloredCube dynamic) b c =(* Printf.printf "[%b]"
| 'e' -> camera_angle_y := !camera_angle_y - 30 | 'e' -> camera_angle_y := !camera_angle_y - 30
| _ -> () ;; | _ -> () ;;
let rec move_cam_hash_2 hash cx cy cz b c =(* Printf.printf "[%b]" b; Stdlib.print_endline " " ; *)match c with
| 'z' ->
camera_xyz.z <- camera_xyz.z +. Float.cos ((float_of_int !camera_angle_y) *. 3.1415926535 /. 180.);
camera_xyz.x <- camera_xyz.x +. Float.sin ((float_of_int !camera_angle_y) *. 3.1415926535 /. 180.);
if b && (
(is_collision_hash_2 camera_xyz (Hashtbl.find_opt hash (cx,cy,cz))) ||
(is_collision_hash_2 camera_xyz (Hashtbl.find_opt hash (cx+1,cy,cz))) ||
(is_collision_hash_2 camera_xyz (Hashtbl.find_opt hash (cx-1,cy,cz))) ||
(is_collision_hash_2 camera_xyz (Hashtbl.find_opt hash (cx,cy+1,cz))) ||
(is_collision_hash_2 camera_xyz (Hashtbl.find_opt hash (cx,cy-1,cz))) ||
(is_collision_hash_2 camera_xyz (Hashtbl.find_opt hash (cx,cy,cz+1))) ||
(is_collision_hash_2 camera_xyz (Hashtbl.find_opt hash (cx,cy,cz-1)))
) then move_cam_hash_2 hash cx cy cz false 's'
| 'q' ->
camera_xyz.z <- camera_xyz.z +. Float.cos (((float_of_int !camera_angle_y) +. 90.) *. 3.1415926535 /. 180.);
camera_xyz.x <- camera_xyz.x +. Float.sin (((float_of_int !camera_angle_y) +. 90.) *. 3.1415926535 /. 180.);
if b && (
(is_collision_hash_2 camera_xyz (Hashtbl.find_opt hash (cx,cy,cz))) ||
(is_collision_hash_2 camera_xyz (Hashtbl.find_opt hash (cx+1,cy,cz))) ||
(is_collision_hash_2 camera_xyz (Hashtbl.find_opt hash (cx-1,cy,cz))) ||
(is_collision_hash_2 camera_xyz (Hashtbl.find_opt hash (cx,cy+1,cz))) ||
(is_collision_hash_2 camera_xyz (Hashtbl.find_opt hash (cx,cy-1,cz))) ||
(is_collision_hash_2 camera_xyz (Hashtbl.find_opt hash (cx,cy,cz+1))) ||
(is_collision_hash_2 camera_xyz (Hashtbl.find_opt hash (cx,cy,cz-1)))
) then move_cam_hash_2 hash cx cy cz false 'q'
| 's' ->
camera_xyz.z <- camera_xyz.z -. Float.cos ((float_of_int !camera_angle_y) *. 3.1415926535 /. 180.);
camera_xyz.x <- camera_xyz.x -. Float.sin ((float_of_int !camera_angle_y) *. 3.1415926535 /. 180.);
if b && (
(is_collision_hash_2 camera_xyz (Hashtbl.find_opt hash (cx,cy,cz))) ||
(is_collision_hash_2 camera_xyz (Hashtbl.find_opt hash (cx+1,cy,cz))) ||
(is_collision_hash_2 camera_xyz (Hashtbl.find_opt hash (cx-1,cy,cz))) ||
(is_collision_hash_2 camera_xyz (Hashtbl.find_opt hash (cx,cy+1,cz))) ||
(is_collision_hash_2 camera_xyz (Hashtbl.find_opt hash (cx,cy-1,cz))) ||
(is_collision_hash_2 camera_xyz (Hashtbl.find_opt hash (cx,cy,cz+1))) ||
(is_collision_hash_2 camera_xyz (Hashtbl.find_opt hash (cx,cy,cz-1)))
) then move_cam_hash_2 hash cx cy cz false 'z'
| 'd' ->
camera_xyz.z <- camera_xyz.z +. Float.cos (((float_of_int !camera_angle_y) -. 90.) *. 3.1415926535 /. 180.);
camera_xyz.x <- camera_xyz.x +. Float.sin (((float_of_int !camera_angle_y) -. 90.) *. 3.1415926535 /. 180.);
if b && (
(is_collision_hash_2 camera_xyz (Hashtbl.find_opt hash (cx,cy,cz))) ||
(is_collision_hash_2 camera_xyz (Hashtbl.find_opt hash (cx+1,cy,cz))) ||
(is_collision_hash_2 camera_xyz (Hashtbl.find_opt hash (cx-1,cy,cz))) ||
(is_collision_hash_2 camera_xyz (Hashtbl.find_opt hash (cx,cy+1,cz))) ||
(is_collision_hash_2 camera_xyz (Hashtbl.find_opt hash (cx,cy-1,cz))) ||
(is_collision_hash_2 camera_xyz (Hashtbl.find_opt hash (cx,cy,cz+1))) ||
(is_collision_hash_2 camera_xyz (Hashtbl.find_opt hash (cx,cy,cz-1)))
) then move_cam_hash_2 hash cx cy cz false 'q'
| 'p' ->
camera_xyz.y <- camera_xyz.y -. 1.;
if b && (
(is_collision_hash_2 camera_xyz (Hashtbl.find_opt hash (cx,cy,cz))) ||
(is_collision_hash_2 camera_xyz (Hashtbl.find_opt hash (cx+1,cy,cz))) ||
(is_collision_hash_2 camera_xyz (Hashtbl.find_opt hash (cx-1,cy,cz))) ||
(is_collision_hash_2 camera_xyz (Hashtbl.find_opt hash (cx,cy+1,cz))) ||
(is_collision_hash_2 camera_xyz (Hashtbl.find_opt hash (cx,cy-1,cz))) ||
(is_collision_hash_2 camera_xyz (Hashtbl.find_opt hash (cx,cy,cz+1))) ||
(is_collision_hash_2 camera_xyz (Hashtbl.find_opt hash (cx,cy,cz-1)))
) then move_cam_hash_2 hash cx cy cz false 'm'
| 'm' ->
camera_xyz.y <- camera_xyz.y +. 1.;
if b && (
(is_collision_hash_2 camera_xyz (Hashtbl.find_opt hash (cx,cy,cz))) ||
(is_collision_hash_2 camera_xyz (Hashtbl.find_opt hash (cx+1,cy,cz))) ||
(is_collision_hash_2 camera_xyz (Hashtbl.find_opt hash (cx-1,cy,cz))) ||
(is_collision_hash_2 camera_xyz (Hashtbl.find_opt hash (cx,cy+1,cz))) ||
(is_collision_hash_2 camera_xyz (Hashtbl.find_opt hash (cx,cy-1,cz))) ||
(is_collision_hash_2 camera_xyz (Hashtbl.find_opt hash (cx,cy,cz+1))) ||
(is_collision_hash_2 camera_xyz (Hashtbl.find_opt hash (cx,cy,cz-1)))
) then move_cam_hash_2 hash cx cy cz false 'p'
| 'a' -> camera_angle_y := !camera_angle_y + 30
| 'e' -> camera_angle_y := !camera_angle_y - 30
| _ -> () ;;
let manage_unexisting_chunk hash ch_x ch_y ch_z screen_wd screen_ht fov =
try
draw_multiples_cubes_colored_hash (Hashtbl.find hash (ch_x, ch_y, ch_z)) screen_wd screen_ht fov
with
| Not_found -> () ;;
let render_chunks hash camx camy camz ch_distance screen_wd screen_ht fov =
let arr = Array.make ((2*ch_distance + 1)*(2*ch_distance + 1)*(2*ch_distance + 1)) ((0, 0), (0, 99)) in
let id = ref 0 in
for i = -ch_distance to ch_distance do
for j = -ch_distance to ch_distance do
for k = -ch_distance to ch_distance do
arr.(!id) <- ((camx+i, camy+j), (camz+k, (abs i) + (abs j) + (abs k)));
incr id
done
done
done;
let sort_fct elt1 elt2 =
(snd (snd elt2)) - (snd (snd elt1))
in
Array.sort sort_fct arr ;
for i = 0 to (Array.length arr -1) do
(*Printf.printf "[%d, %d, %d] (%d)" (fst (fst arr.(i))) (snd (fst arr.(i))) (fst (snd arr.(i))) (snd (snd arr.(i)));
Stdlib.print_endline " ";*)
manage_unexisting_chunk hash (fst (fst arr.(i))) (snd (fst arr.(i))) (fst (snd arr.(i))) screen_wd screen_ht fov ;
done ;;
let play_dos laby = let play_dos laby =
try try
Stdlib.print_endline "Building terrain..."; Stdlib.print_endline "Building terrain...";
cheesify laby; cheesify laby;
Stdlib.print_endline "Converting terrain..."; Stdlib.print_endline "Converting terrain...";
let hash = chunkify laby 2 in let hash = chunkify_2 laby 2 in
camera_xyz.z <- -. (1.5) ; camera_xyz.z <- -. (1.5) ;
camera_xyz.x <- -. (float_of_int width) /. 2. ; camera_xyz.x <- -. (float_of_int width) /. 2. ;
@ -805,7 +987,8 @@ let play_dos laby =
let (ch_x, ch_y, ch_z) = coords_to_chunk_f (-. camera_xyz.x) (-. camera_xyz.y) camera_xyz.z in let (ch_x, ch_y, ch_z) = coords_to_chunk_f (-. camera_xyz.x) (-. camera_xyz.y) camera_xyz.z in
draw_multiples_cubes_colored_hash (Hashtbl.find hash (ch_x, ch_y, ch_z)) __width__ __height__ fov ; (*draw_multiples_cubes_colored_hash (Hashtbl.find hash (ch_x, ch_y, ch_z)) __width__ __height__ fov ;*)
render_chunks hash ch_x ch_y ch_z chunk_dist __width__ __height__ fov ;
auto_synchronize true; auto_synchronize true;
@ -814,7 +997,7 @@ let play_dos laby =
Stdlib.print_endline " "; Stdlib.print_endline " ";
let usr_input = get1char () in let usr_input = get1char () in
move_cam_hash (Hashtbl.find hash (ch_x, ch_y, ch_z)) true usr_input move_cam_hash_2 hash ch_x ch_y ch_z true usr_input
done ; done ;
() ()
with with