added infinite maze generation and coord display

This commit is contained in:
Alexandre 2024-07-07 09:46:29 +02:00
parent 8a88f07366
commit 60ae801f1b
5 changed files with 90 additions and 106 deletions

BIN
a.out

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -7,9 +7,9 @@ and __height__ = 1000 ;;
type tile = Free | Wall | Crate | Exit | Craxit | Camera ;;
let width = 15
and height = 15
and depth = 15 ;;
let width = 4
and height = 4
and depth = 4 ;;
(* dimensions *)
let render_distance = 7 ;;
@ -18,6 +18,10 @@ let chunk_dist = 2 ;;
let chunk_size = 4 ;;
let chunk_size_f = float_of_int chunk_size ;;
let density = 50 ;;
let speed_multiplier = 1.5 ;;
(* -------------------------------------------------------------------------------------------------------- *)
(* -------------------------------------------------------------------------------------------------------- *)
(* -------------------------------------------------------------------------------------------------------- *)
@ -175,11 +179,51 @@ let camera_angle_z = ref 0 ;;
(* in degrees *)
(* ------------------------------------------------------------- *)
(*
let should_be_drawn (pt : pt_3d) =
let translated = {x = pt.x -. camera_xyz.x; y = pt.y -. camera_xyz.y; z = pt.z +. camera_xyz.z} in
(translated.z *. Float.cos ((float_of_int !camera_angle_y) *. 3.14159255358 /. 180.) -. translated.x *. Float.sin ((float_of_int !camera_angle_y) *. 3.14159255358 /. 180.)) > 0. ;;
*)
let rec ln10 n = match n with
| k when k < 0 -> failwith "Are you sure about that ?"
| k when k < 10 -> 0
| k -> 1 + ln10 (k/10) ;;
let draw_integer_alignedleft x0 y n0 len =
(* 7-seg display 2 *)
set_line_width (max 1 (len/4));
let n = ref n0 in
let size = ln10 (abs n0) in
let cur_x = ref (x0 + size*(len*11/7)) in
if !n < 0 then begin
n := !n * (-1);
draw_poly_line [|(x0, y); (x0+len, y)|];
cur_x := !cur_x + (len*11/7)
end;
for i = 0 to size do
let x = !cur_x in
if Array.mem (!n mod 10) [|0; 4; 5; 6; 7; 8; 9|] then
draw_poly_line [|(x, y+len); (x, y)|];
if Array.mem (!n mod 10) [|0; 2; 3; 5; 6; 7; 8; 9|] then
draw_poly_line [|(x, y+len); (x+len, y+len)|];
if Array.mem (!n mod 10) [|0; 1; 2; 3; 4; 7; 8; 9|] then
draw_poly_line [|(x+len, y+len); (x+len, y)|];
if Array.mem (!n mod 10) [|2; 3; 4; 5; 6; 8; 9|] then
draw_poly_line [|(x, y); (x+len, y)|];
if Array.mem (!n mod 10) [|0; 1; 3; 4; 5; 6; 7; 8; 9|] then
draw_poly_line [|(x+len, y-len); (x+len, y)|];
if Array.mem (!n mod 10) [|0; 2; 3; 5; 6; 8; 9|] then
draw_poly_line [|(x, y-len); (x+len, y-len)|];
if Array.mem (!n mod 10) [|0; 2; 6; 8|] then
draw_poly_line [|(x, y-len); (x, y)|];
n := !n/10;
cur_x := !cur_x - (len*11/7);
done ;;
let should_be_drawn_gr (pt : pt_3d) =
pt.z > 0.4 ;;
@ -724,7 +768,7 @@ let is_collision_hash_2 (cam_coords : pt_3d) (rcubes : (coloredCube dynamic) opt
done ;
for i = 0 to n-1 do
if not !res && distances.(i) < 2. then
if not !res && distances.(i) < 6. then
res := is_collision_cube cam_coords cubes.tab.(i).cube
done;
!res
@ -797,56 +841,6 @@ let chunkify_2 laby sz =
cubes ;;
let chunkify 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 cw ch cd r g b=
(*Printf.printf "(%d, %d, %d) (%d, %d, %d)\n" w h d cw ch cd;*)
match Hashtbl.find_opt cubes (cw, ch, cd) with
| None -> begin
let dyna = dyn_create {cube = create_cube (w*sz) (h*sz) (d*sz) sz; red = r; green = g; blue = b} in
Hashtbl.add cubes (cw, ch, cd) dyna
end
| Some dyna -> begin
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
for i = -chunk_dist to chunk_dist do
for j = -chunk_dist to chunk_dist do
for k = -chunk_dist to chunk_dist do
add_to_table w h d ((w*sz)/chunk_size + i) ((h*sz)/chunk_size + j) ((d*sz)/chunk_size + k) 220 220 220
done
done
done
end(*;
Stdlib.print_endline " ";*)
done
done
done;
for w = -10 to 10 do
for d = -10 to 10 do
for i = -chunk_dist to chunk_dist do
for j = -chunk_dist to chunk_dist do
for k = -chunk_dist to chunk_dist do
add_to_table w (-1) d ((w*sz)/chunk_size + i) (((-1)*sz)/chunk_size + j) ((d*sz)/chunk_size + k) 256 256 32
done
done
done
done
done ;
cubes ;;
let cheesify (laby : tile array array array) =
let width = Array.length laby
and height = Array.length laby.(0)
@ -868,37 +862,10 @@ let cheesify (laby : tile array array array) =
done
done;;
let rec move_cam_hash (cubes : coloredCube dynamic) 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 camera_xyz cubes) then move_cam_hash cubes 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 camera_xyz cubes) then move_cam_hash cubes 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 camera_xyz cubes) then move_cam_hash cubes 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 camera_xyz cubes) then move_cam_hash cubes false 'q'
| 'p' ->
camera_xyz.y <- camera_xyz.y -. 1.;
if b && (is_collision_hash camera_xyz cubes) then move_cam_hash cubes false 'm'
| 'm' ->
camera_xyz.y <- camera_xyz.y +. 1.;
if b && (is_collision_hash camera_xyz cubes) then move_cam_hash cubes false 'p'
| 'a' -> 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.);
camera_xyz.z <- camera_xyz.z +. speed_multiplier *. Float.cos ((float_of_int !camera_angle_y) *. 3.1415926535 /. 180.);
camera_xyz.x <- camera_xyz.x +. speed_multiplier *. 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))) ||
@ -909,8 +876,8 @@ let rec move_cam_hash_2 hash cx cy cz b c =(* Printf.printf "[%b]" b; Stdlib.pri
(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.);
camera_xyz.z <- camera_xyz.z +. speed_multiplier *. Float.cos (((float_of_int !camera_angle_y) +. 90.) *. 3.1415926535 /. 180.);
camera_xyz.x <- camera_xyz.x +. speed_multiplier *. 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))) ||
@ -921,8 +888,8 @@ let rec move_cam_hash_2 hash cx cy cz b c =(* Printf.printf "[%b]" b; Stdlib.pri
(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.);
camera_xyz.z <- camera_xyz.z -. speed_multiplier *. Float.cos ((float_of_int !camera_angle_y) *. 3.1415926535 /. 180.);
camera_xyz.x <- camera_xyz.x -. speed_multiplier *. 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))) ||
@ -933,8 +900,8 @@ let rec move_cam_hash_2 hash cx cy cz b c =(* Printf.printf "[%b]" b; Stdlib.pri
(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.);
camera_xyz.z <- camera_xyz.z +. speed_multiplier *. Float.cos (((float_of_int !camera_angle_y) -. 90.) *. 3.1415926535 /. 180.);
camera_xyz.x <- camera_xyz.x +. speed_multiplier *. 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))) ||
@ -945,7 +912,7 @@ let rec move_cam_hash_2 hash cx cy cz b c =(* Printf.printf "[%b]" b; Stdlib.pri
(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.;
camera_xyz.y <- camera_xyz.y -. speed_multiplier ;
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))) ||
@ -956,7 +923,7 @@ let rec move_cam_hash_2 hash cx cy cz b c =(* Printf.printf "[%b]" b; Stdlib.pri
(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.;
camera_xyz.y <- camera_xyz.y +. speed_multiplier ;
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))) ||
@ -983,18 +950,26 @@ let initialize_flat hash ch_x ch_y ch_z =
let dyna = dyn_create {cube = create_cube 0 0 0 1; red = 33; green = 33; blue = 22} in
for i = 0 to 3 do
for j = 0 to 3 do
dyn_append dyna {cube = create_cube (4*ch_x +i) (4*ch_y) (4*ch_z+j) 1; red = 32; green = 222; blue = 32};
for k = 0 to 3 do
dyn_append dyna {cube = create_cube (4*ch_x +i) (4*ch_y +k) (4*ch_z +j) 1; red = 32; green = 222; blue = 32};
done
done
done;
Hashtbl.add hash (ch_x, ch_y, ch_z) dyna ;;
let manage_unexisting_chunk hash ch_x ch_y ch_z screen_wd screen_ht fov =
let init_random hash ch_x ch_y ch_z =
let dyna = dyn_create {cube = create_cube 0 0 0 1; red = 33; green = 33; blue = 22} in
if (Random.int 101) < density then
dyn_append dyna {cube = create_cube (4*ch_x) (4*ch_y) (4*ch_z) 4; red = 250 ; green = 250 ; blue = 250};
Hashtbl.add hash (ch_x, ch_y, ch_z) dyna ;;
let rec 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)) hash screen_wd screen_ht fov
with
| Not_found when ch_y = (-1)-> initialize_chunk hash ch_x ch_y ch_z
| Not_found when ch_y = (-2)-> initialize_flat hash ch_x ch_y ch_z
| Not_found -> () ;;
| Not_found when ch_y = (-64)-> initialize_flat hash ch_x ch_y ch_z ; manage_unexisting_chunk hash ch_x ch_y ch_z screen_wd screen_ht fov
| Not_found when ch_y = 64-> initialize_flat hash ch_x ch_y ch_z ; manage_unexisting_chunk hash ch_x ch_y ch_z screen_wd screen_ht fov
| Not_found -> init_random hash ch_x ch_y ch_z ; manage_unexisting_chunk hash ch_x ch_y ch_z screen_wd screen_ht fov ;;
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
@ -1028,14 +1003,14 @@ let get1char_plus () =
let play_dos laby =
try
Stdlib.print_endline "Building terrain...";
cheesify laby;
(*cheesify laby;*)
Stdlib.print_endline "Converting terrain...";
let hash = chunkify_2 laby 2 in
(*let hash = chunkify_2 laby 2 in*)
let hash = Hashtbl.create 100 in
camera_xyz.z <- -. (1.5) ;
camera_xyz.x <- -. (float_of_int width) /. 2. ;
camera_xyz.y <- -. (float_of_int height) /. 2. ;
let dyna = dyn_create {cube = create_cube 0 0 0 1 ; red = 30 ; green = 30 ; blue = 30} in
Hashtbl.add hash (0, 0, 0) dyna ;
(*print_cubes cs ;*)
@ -1045,6 +1020,10 @@ let play_dos laby =
and ch_y = ref 0
and ch_z = ref 0 in
camera_xyz.x <- (-. 2.) ;
camera_xyz.y <- (-. 2.) ;
camera_xyz.z <- 2. ;
while true do
if !redraw then begin
auto_synchronize false;
@ -1057,6 +1036,11 @@ let play_dos laby =
render_chunks hash !ch_x !ch_y !ch_z chunk_dist __width__ __height__ fov ;
set_color white;
draw_integer_alignedleft 10 (__height__ - 30) (-int_of_float camera_xyz.x) 25;
draw_integer_alignedleft 10 (__height__ - 90) (-int_of_float camera_xyz.y) 25;
draw_integer_alignedleft 10 (__height__ - 150) (int_of_float camera_xyz.z) 25;
auto_synchronize true;
end;
redraw := false;

BIN
display.o

Binary file not shown.