added random structure generation

This commit is contained in:
Alexandre 2024-07-10 12:11:25 +02:00
parent def4253da9
commit 0b08e44c4b
5 changed files with 136 additions and 10 deletions

BIN
a.out

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -25,6 +25,11 @@ let density = 50 ;;
let speed_multiplier = 1.5 ;;
(* -------------------------------------------------------------------------------------------------------- *)
let structure_1_frequency = 1000 ;;
(* avg number of chunk generation required before encountering a structure of type 1 *)
(* -------------------------------------------------------------------------------------------------------- *)
(* -------------------------------------------------------------------------------------------------------- *)
(* -------------------------------------------------------------------------------------------------------- *)
@ -669,6 +674,25 @@ let create_cube x0' y0' z0' sz' =
|]
in res ;;
let create_rect x0' y0' z0' sx' sy' sz' =
let x0 = float_of_int x0'
and y0 = float_of_int y0'
and z0 = float_of_int z0'
and sx = float_of_int sx'
and sy = float_of_int sy'
and sz = float_of_int sz' in
let res = [|
{x = x0 ; y = y0 ; z = z0};
{x = x0 +. sx ; y = y0 ; z = z0};
{x = x0 +. sx ; y = y0 +. sy ; z = z0};
{x = x0 ; y = y0 +. sy ; z = z0};
{x = x0 ; y = y0 ; z = z0 +. sz};
{x = x0 +. sx ; y = y0 ; z = z0 +. sz};
{x = x0 +. sx ; y = y0 +. sy ; z = z0 +. sz};
{x = x0 ; y = y0 +. sy ; z = z0 +. sz}
|]
in res ;;
let fov = 90 ;;
(*
@ -940,15 +964,6 @@ let rec move_cam_hash_2 hash cx cy cz b c =(* Printf.printf "[%b]" b; Stdlib.pri
| 'e' -> camera_angle_y := !camera_angle_y - 30
| _ -> () ;;
let initialize_chunk 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 + (i + j) mod 4) (4*ch_z+j) 1; red = 32; green = 32; blue = 200};
done
done;
Hashtbl.add hash (ch_x, ch_y, ch_z) dyna ;;
let init_chunk_all hash mem ch_x ch_y ch_z =
let n_cubes = chunk_size / 3 in
let dyna = dyn_create {cube = create_cube 0 0 0 1; red = 33; green = 33; blue = 22} in
@ -975,8 +990,14 @@ let redirect_generation hash mem ch_x ch_y ch_z =
else
init_chunk_all hash mem ch_x ch_y ch_z ;;
let needs_to_be_filled optres = match optres with
| None -> true
| Some k when k < 0 -> true
| _ -> false
let rec manage_unexisting_chunk hash mem ch_x ch_y ch_z screen_wd screen_ht fov =
if Hashtbl.find_opt mem (ch_x, ch_y, ch_z) = None then
let idk = Hashtbl.find_opt mem (ch_x, ch_y, ch_z) in
if needs_to_be_filled idk then
redirect_generation hash mem ch_x ch_y ch_z ;
draw_multiples_cubes_colored_hash (Hashtbl.find hash (ch_x, ch_y, ch_z)) hash screen_wd screen_ht fov ;;
@ -1003,6 +1024,104 @@ let render_chunks hash mem camx camy camz ch_distance screen_wd screen_ht fov =
manage_unexisting_chunk hash mem (fst (fst arr.(i))) (snd (fst arr.(i))) (fst (snd arr.(i))) screen_wd screen_ht fov ;
done ;;
let structure_1_spawnable opt = match opt with
| None -> true
| Some k when k <= 0 && k <> -1 -> true
| _ -> false ;;
let generate_structure_1 hash mem ch_x ch_y ch_z =
(* structure 1 is 5x5x5 chunks around the center *)
(* iterating on the edge of render distance field to prevent structure popping out of nowhere *)
let spawn_structure chx chy chz =
(*
par invariant global, Hashtbl.find_opt hash (chx, chy, chz) = None
*)
let empty = dyn_create {cube = create_cube 0 0 0 1; red = 33; green = 33; blue = 22} in
(*dyn_append dyna {cube = create_cube (chunk_size*chx) (chunk_size*chy) (chunk_size*chz) chunk_size; red = 250; green = 32; blue = 32};*)
for w = -2 to 2 do
for h = -2 to 2 do
for d = -2 to 2 do
if abs w = 2 || (abs h = 2 && w*d <> 0) || abs d = 2 then begin
let filled = dyn_create {cube = create_cube 0 0 0 1; red = 33; green = 33; blue = 22} in
dyn_append filled {cube = create_cube ((chx+w)*chunk_size) ((chy+h)*chunk_size) ((chz+d)*chunk_size) chunk_size; red = 250; green = 128; blue = 64};
Hashtbl.add hash (chx+w, chy+h, chz+d) filled;
end
else begin
Hashtbl.add hash (chx+w, chy+h, chz+d) empty;
end;
Hashtbl.add mem (chx+w, chy+h, chz+d) 2;
done
done
done;
(*Hashtbl.add mem (chx, chy, chz) 2;
Hashtbl.add hash (chx, chy, chz) dyna;*)
Printf.printf "Added S1 at (%d, %d, %d)\n" (chx*chunk_size) (chy*chunk_size) (chz*chunk_size);
Printf.printf "----------- (%d, %d, %d)" chx chy chz;
Stdlib.print_endline " "
in
for w = -(chunk_dist+3) to (chunk_dist+3) do
for h = -(chunk_dist+3) to (chunk_dist+3) do
if structure_1_spawnable (Hashtbl.find_opt mem (ch_x + w, ch_y + h, ch_z - (chunk_dist+3))) then begin (* if the chunk is valid *)
if (Random.int (structure_1_frequency-1)) = 0 then (* roll a dice *)
spawn_structure (ch_x + w) (ch_y + h) (ch_z - (chunk_dist+3))
else (* failed atempt *)
Hashtbl.add mem (ch_x + w, ch_y + h, ch_z - (chunk_dist+3)) (-1)
end;
done
done;
for w = -(chunk_dist+3) to (chunk_dist+3) do
for h = -(chunk_dist+3) to (chunk_dist+3) do
if structure_1_spawnable (Hashtbl.find_opt mem (ch_x + w, ch_y + h, ch_z + (chunk_dist+3))) then begin
if (Random.int (structure_1_frequency-1)) = 0 then
spawn_structure (ch_x + w) (ch_y + h) (ch_z + (chunk_dist+3))
else
Hashtbl.add mem (ch_x + w, ch_y + h, ch_z + (chunk_dist+3)) (-1)
end
done
done;
for w = -(chunk_dist+3) to (chunk_dist+3) do
for d = -(chunk_dist+3) to (chunk_dist+3) do
if structure_1_spawnable (Hashtbl.find_opt mem (ch_x + w, ch_y - (chunk_dist+3), ch_z + d)) then begin
if (Random.int (structure_1_frequency-1)) = 0 then
spawn_structure (ch_x + w) (ch_y - (chunk_dist+3)) (ch_z + d)
else
Hashtbl.add mem (ch_x + w, ch_y - (chunk_dist+3), ch_z + d) (-1)
end
done
done;
for w = -(chunk_dist+3) to (chunk_dist+3) do
for d = -(chunk_dist+3) to (chunk_dist+3) do
if structure_1_spawnable (Hashtbl.find_opt mem (ch_x + w, ch_y + (chunk_dist+3), ch_z + d)) then begin
if (Random.int (structure_1_frequency-1)) = 0 then
spawn_structure (ch_x + w) (ch_y + (chunk_dist+3)) (ch_z + d)
else
Hashtbl.add mem (ch_x + w, ch_y + (chunk_dist+3), ch_z + d) (-1)
end
done
done;
for h = -(chunk_dist+3) to (chunk_dist+3) do
for d = -(chunk_dist+3) to (chunk_dist+3) do
if structure_1_spawnable (Hashtbl.find_opt mem (ch_x - (chunk_dist+3), ch_y + h, ch_z + d)) then begin
if (Random.int (structure_1_frequency-1)) = 0 then
spawn_structure (ch_x - (chunk_dist+3)) (ch_y + h) (ch_z + d)
else
Hashtbl.add mem (ch_x - (chunk_dist+3), ch_y + h, ch_z + d) (-1)
end
done
done;
for h = -(chunk_dist+3) to (chunk_dist+3) do
for d = -(chunk_dist+3) to (chunk_dist+3) do
if structure_1_spawnable (Hashtbl.find_opt mem (ch_x + (chunk_dist+3), ch_y + h, ch_z + d)) then begin
if (Random.int (structure_1_frequency-1)) = 0 then
spawn_structure (ch_x + (chunk_dist+3)) (ch_y + h) (ch_z + d)
else
Hashtbl.add mem (ch_x + (chunk_dist+3), ch_y + h, ch_z + d) (-1)
end
done
done ;;
let get1char_plus () =
if key_pressed () then
read_key ()
@ -1022,6 +1141,12 @@ let play_dos laby =
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 ;
Hashtbl.add memory (0, 0, 0) 1;
(*
N/A = unloaded chunk
<0 = failed structure gen
1 = casual geeration
>1 = structure generation
*)
(*print_cubes cs ;*)
@ -1045,6 +1170,7 @@ let play_dos laby =
let (cx, cy, cz) = coords_to_chunk_f (-. camera_xyz.x) (-. camera_xyz.y) camera_xyz.z in
ch_x := cx ; ch_y := cy ; ch_z := cz;
generate_structure_1 hash memory !ch_x !ch_y !ch_z;
render_chunks hash memory !ch_x !ch_y !ch_z chunk_dist __width__ __height__ fov ;
set_color white;

BIN
display.o

Binary file not shown.