added ore spawning through a structure

This commit is contained in:
Alexandre 2024-07-17 23:09:08 +02:00
parent 5e7a78be82
commit 0a874da94d
5 changed files with 125 additions and 12 deletions

BIN
a.out

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -59,7 +59,21 @@ ocamlfind ocamlopt -linkpkg -package unix -linkpkg -package graphics -thread -pa
*) *)
type chemical = Iron | Copper | Tungsten | Gold | Diamond | Carbon | Steel | Stackyte ;; (* used for building weapons with help of our fellow SI classmates *)
type chemical = Iron | Copper | Gold | Steel | Zinc | Carbon | Diamond | Tungsten | Stackyte ;;
(* used for ammunition crafting *)
type stoned = Magma | Sharp | Spherical | Normal ;;
let playerOreInventory = [|0; 0; 0; 0; 0; 0; 0; 0; 0|] ;;
let playerStoneInventory = [|0; 0; 0; 0|] ;;
(* 0 ~ 100 *)
let copperChance = 60 ;;
(* higher = more likely to have rarer ores, ranges between 0 and 1000 *)
let copperIncrChance = 150 ;;
let ironIncrChance = 88 ;;
(* ------------------------------------------------------------- *) (* ------------------------------------------------------------- *)
(* ------------------------------------------------------------- *) (* ------------------------------------------------------------- *)
@ -807,6 +821,75 @@ let is_collision_coin (cam_coords : pt_3d) (cube : pt_3d array) =
cube.(6).y +. coin_magnet_dist >= (-. cam_coords.y) && cube.(6).y +. coin_magnet_dist >= (-. cam_coords.y) &&
cube.(6).z +. coin_magnet_dist >= cam_coords.z ;; cube.(6).z +. coin_magnet_dist >= cam_coords.z ;;
let add_ore_to_inventory = function
| "F" -> playerOreInventory.(0) <- playerOreInventory.(0) +1
| "U" -> playerOreInventory.(1) <- playerOreInventory.(1) +1
| "A" -> playerOreInventory.(2) <- playerOreInventory.(2) +1
| "I" -> playerOreInventory.(3) <- playerOreInventory.(3) +1
| "Z" -> playerOreInventory.(4) <- playerOreInventory.(4) +1
| "C" -> playerOreInventory.(5) <- playerOreInventory.(5) +1
| "D" -> playerOreInventory.(6) <- playerOreInventory.(6) +1
| "T" -> playerOreInventory.(7) <- playerOreInventory.(7) +1
| "S" -> playerOreInventory.(8) <- playerOreInventory.(8) +1
| x -> Printf.printf "%s" x ; failwith "Error : illegal character" ;;
(* -------------------------------------------------------------------- *)
(* -------------------------------------------------------------------- *)
let oreColors = [|
[|192; 192; 192|];
[|230; 172; 23|];
[|243; 243; 28|];
[|101; 97; 120|];
[|240; 240; 240|];
[|6; 6; 6|];
[|41; 229; 255|];
[|30; 30; 150|];
[|0; 191; 47|];
|] ;;
let rec oreToString = function
| Iron -> ("F", oreColors.(0))
| Copper -> ("U", oreColors.(1))
| Gold -> ("A", oreColors.(2))
| Steel -> ("I", oreColors.(3))
| Zinc -> ("Z", oreColors.(4))
| Carbon -> ("C", oreColors.(5))
| Diamond -> ("D", oreColors.(6))
| Tungsten -> ("T", oreColors.(7))
| Stackyte -> ("S", oreColors.(8)) ;;
let copperUp = function
| Copper -> if (Random.int 2) = 0 then Zinc else Carbon
| Zinc -> Tungsten
| _ -> failwith "Not meant to happen 1" ;;
let ironUp = function
| Iron -> if (Random.int 2) = 0 then Gold else Steel
| Gold -> Diamond
| _ -> failwith "Not meant to happen 2" ;;
let rec auxCopper s =
if s = Tungsten || s = Carbon then
oreToString s
else match (Random.int 1000) with
| k when k < copperIncrChance -> auxCopper (copperUp s)
| _ -> oreToString s ;;
let rec auxIron s =
if s = Diamond || s = Steel then
oreToString s
else match (Random.int 1000) with
| k when k < ironIncrChance -> auxIron (ironUp s)
| _ -> oreToString s ;;
let generate_random_ore () = match (Random.int 100) with
| k when k < copperChance -> auxCopper Copper
| k -> auxIron Iron ;;
(* -------------------------------------------------------------------- *)
(* -------------------------------------------------------------------- *)
let is_collision_cube_bis (cam_coords : pt_3d) (cuube : coloredCube) = let is_collision_cube_bis (cam_coords : pt_3d) (cuube : coloredCube) =
if is_string_integer cuube.flag then if is_string_integer cuube.flag then
false false
@ -874,7 +957,7 @@ let is_collision_hash_2 (cam_coords : pt_3d) (rcubes : (coloredCube dynamic) opt
let rem_len = ref 0 in let rem_len = ref 0 in
for i = 0 to n-1 do for i = 0 to n-1 do
if is_string_integer cubes.tab.(i).flag then begin if is_string_integer cubes.tab.(i).flag then begin (* coin *)
if is_collision_coin cam_coords cubes.tab.(i).cube then begin if is_collision_coin cam_coords cubes.tab.(i).cube then begin
let valc = str_to_int cubes.tab.(i).flag in let valc = str_to_int cubes.tab.(i).flag in
coins := !coins + valc; coins := !coins + valc;
@ -884,6 +967,15 @@ let is_collision_hash_2 (cam_coords : pt_3d) (rcubes : (coloredCube dynamic) opt
Stdlib.print_endline " "*) Stdlib.print_endline " "*)
end end
end end
else if (String.length cubes.tab.(i).flag) = 1 then begin (* ore *)
if is_collision_coin cam_coords cubes.tab.(i).cube then begin
add_ore_to_inventory cubes.tab.(i).flag;
to_be_removed := (i - !rem_len)::(!to_be_removed);
incr rem_len;
(*Printf.printf "%d" valc;
Stdlib.print_endline " "*)
end
end
else if not !res && distances.(i) < chunk_size_f then else if not !res && distances.(i) < chunk_size_f then
(*res := is_collision_cube cam_coords cubes.tab.(i).cube*) (*res := is_collision_cube cam_coords cubes.tab.(i).cube*)
res := is_collision_cube_bis cam_coords cubes.tab.(i) res := is_collision_cube_bis cam_coords cubes.tab.(i)
@ -1251,17 +1343,18 @@ let generate_structure_2 hash mem ch_x ch_y ch_z =
for w = -2 to 2 do for w = -2 to 2 do
for h = -2 to 2 do for h = -2 to 2 do
for d = -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 if abs w = 2 || (abs d = 2 && w*h <> 0) || abs h = 2 then begin
let filled = dyn_create {flag = "struct_2" ; cube = create_cube 0 0 0 1; red = 33; green = 33; blue = 22} in let filled = dyn_create {flag = "struct_2" ; cube = create_cube 0 0 0 1; red = 33; green = 33; blue = 22} in
dyn_append filled {flag = "struct_2" ; cube = create_cube ((chx+w)*chunk_size) ((chy+h)*chunk_size) ((chz+d)*chunk_size) chunk_size; red = 128; green = 250; blue = 64}; dyn_append filled {flag = "struct_2" ; cube = create_cube ((chx+w)*chunk_size) ((chy+h)*chunk_size) ((chz+d)*chunk_size) chunk_size; red = 128; green = 250; blue = 64};
Hashtbl.add hash (chx+w, chy+h, chz+d) filled; Hashtbl.add hash (chx+w, chy+h, chz+d) filled;
end end
else if w = 0 && h = 0 && d = 0 then begin else if w = 0 && h = 0 && d = 0 then begin
let filled = dyn_create {flag = "10" ; cube = create_cube 0 0 0 1; red = 33; green = 33; blue = 22} in let filled = dyn_create {flag = "EEE" ; cube = create_cube 0 0 0 1; red = 33; green = 33; blue = 22} in
for i = 0 to chunk_size -1 do for i = 0 to chunk_size -1 do
for j = 0 to chunk_size -1 do for j = 0 to chunk_size -1 do
for k = 0 to chunk_size -1 do for k = 0 to chunk_size -1 do
dyn_append filled {flag = "10" ; cube = create_cube (chx*chunk_size+i) (chy*chunk_size+j) (chz*chunk_size+k) 1; red = 250; green = 250; blue = 64}; let (cOre, cColors) = generate_random_ore () in
dyn_append filled {flag = cOre ; cube = create_cube (chx*chunk_size+i) (chy*chunk_size+j) (chz*chunk_size+k) 1; red = cColors.(0); green = cColors.(1); blue = cColors.(2)};
done done
done done
done; done;
@ -1352,6 +1445,32 @@ let get1char_plus () =
else else
'@' ;; '@' ;;
let colorsInv = [|
rgb 192 192 192;
rgb 230 172 23;
rgb 243 243 28;
rgb 101 97 120;
rgb 240 240 240;
rgb 6 6 6;
rgb 41 229 255;
rgb 30 30 150;
rgb 0 191 47;
|] ;;
let draw_inventories () =
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;
set_color (rgb 250 250 32);
draw_integer_alignedleft 10 35 !coins 25 ;
for i = 0 to (Array.length playerOreInventory -1) do
set_color colorsInv.(i);
draw_integer_alignedleft 40 (__height__ - (240 + 50 * (Array.length playerOreInventory -1 -i))) playerOreInventory.(i) 20
done ;;
let play_dos laby = let play_dos laby =
try try
Stdlib.print_endline "Building terrain..."; Stdlib.print_endline "Building terrain...";
@ -1397,13 +1516,7 @@ let play_dos laby =
generate_structures hash memory !ch_x !ch_y !ch_z; generate_structures hash memory !ch_x !ch_y !ch_z;
render_chunks hash memory !ch_x !ch_y !ch_z chunk_dist __width__ __height__ fov ; render_chunks hash memory !ch_x !ch_y !ch_z chunk_dist __width__ __height__ fov ;
set_color white; draw_inventories ();
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;
set_color (rgb 250 250 32);
draw_integer_alignedleft 10 35 !coins 25;
auto_synchronize true; auto_synchronize true;
end; end;

BIN
display.o

Binary file not shown.