diff --git a/a.out b/a.out index 9b8fa7b..2e05ad6 100755 Binary files a/a.out and b/a.out differ diff --git a/display.cmi b/display.cmi index 7be64e8..316ab49 100644 Binary files a/display.cmi and b/display.cmi differ diff --git a/display.cmx b/display.cmx index 144e4ec..9780e96 100644 Binary files a/display.cmx and b/display.cmx differ diff --git a/display.ml b/display.ml index cea4223..423e845 100644 --- a/display.ml +++ b/display.ml @@ -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; diff --git a/display.o b/display.o index 0637d65..95a4d31 100644 Binary files a/display.o and b/display.o differ