diff --git a/a.out b/a.out index 84cebc7..9a81321 100755 Binary files a/a.out and b/a.out differ diff --git a/display.cmi b/display.cmi index 950a000..dbb9dd6 100644 Binary files a/display.cmi and b/display.cmi differ diff --git a/display.cmx b/display.cmx index 1c6d686..c687c0a 100644 Binary files a/display.cmx and b/display.cmx differ diff --git a/display.ml b/display.ml index 1f209b6..9988be7 100644 --- a/display.ml +++ b/display.ml @@ -2,11 +2,21 @@ open Graphics ;; Random.self_init () ;; +(* -------------------------------------------------------------------------------------------------------- *) + let __width__ = 1500 and __height__ = 1000 ;; let openstring = " 1500x1000" ;; -(* --------------------------------------------- *) +(* ------------------------------------------------------------------------------------------------------------------------------------ *) +(* ------------------------------------------------------------------------------------------------------------------------------------ *) +(* -----------------------------X=======================================================================X------------------------------ *) +(* -----------------------------| Global/General configs |------------------------------ *) +(* -----------------------------X=======================================================================X------------------------------ *) +(* ------------------------------------------------------------------------------------------------------------------------------------ *) +(* ------------------------------------------------------------------------------------------------------------------------------------ *) + +let max_fps = 120. ;; type tile = Free | Wall | Crate | Exit | Craxit | Camera ;; @@ -15,7 +25,7 @@ and height = 4 and depth = 4 ;; (* dimensions *) -let render_distance = 7 ;; +(* render distance *) let chunk_dist = 2 ;; let cube_size = 3 ;; @@ -25,9 +35,7 @@ let chunk_size = 6 ;; let chunk_size_f = float_of_int chunk_size ;; (* between 0 (empty) and 100 (full) *) -let density = 35 ;; - -let speed_multiplier = 0.1 ;; +let density = 25 ;; (* money money money *) let coins = ref 0 ;; @@ -57,18 +65,34 @@ and az = ref 0.0 ;; (* -------------------------------------------------------------------------------------------------------- *) (* avg number of chunk generation required before encountering a structure of type 1 *) -let structure_1_frequency = 2500 ;; - -(* 0 ~ 1000 *) -let oreMissChance = 50 ;; +let structure_1_frequency = 5000 ;; (* avg number of chunk generation required before encountering a structure of type 2 *) (* NOTE : structure 2 attempts to spawn after structure 1, so the odds will be ever so slightly less than this number *) -let structure_2_frequency = 2500 ;; +let structure_2_frequency = 5000 ;; -(* -------------------------------------------------------------------------------------------------------- *) -(* -------------------------------------------------------------------------------------------------------- *) -(* -------------------------------------------------------------------------------------------------------- *) +(* related to structures ; chance out of 1000 of a single ore generation failing *) +(* 0 ~ 1000 *) +let oreMissChance = 50 ;; + +(* X and Z speed of player*) +let speed_multiplier = 0.6 ;; + +(* Y speed of player*) +let speed_multiplier_Y = 0.4 ;; + +(* accuracy for movement *) +let precision = 10 ;; +let precisionf = 10. ;; + +(* friction multiplier *) +let f = 1.7 ;; + +(* below that value, velocities will be set to 0. *) +let vel_epsilon = 0.005 ;; + +(* ------------------------------------------------------------------------------------------------------------------------------------ *) +(* ------------------------------------------------------------------------------------------------------------------------------------ *) type 'a dynamic = {mutable tab : 'a array ; mutable len : int ; mutable memlen : int} ;; @@ -1310,43 +1334,27 @@ let is_collision_global_G hash cx cy cz = done; !boo ;; -let rec move_cam_hash_2 hash cx cy cz b c =(* Printf.printf "[%b]" b; Stdlib.print_endline " " ; *)match c with +let add_velocity = function | 'z' -> - 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_global_2 hash cx cy cz - ) then move_cam_hash_2 hash cx cy cz false 's' - | 'q' -> - 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_global_2 hash cx cy cz - ) then move_cam_hash_2 hash cx cy cz false 'd' - | 's' -> - 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_global_2 hash cx cy cz - ) then move_cam_hash_2 hash cx cy cz false 'z' + vz := -. speed_multiplier *. Float.cos ((float_of_int !camera_angle_y) *. 3.1415926535 /. 180.); + vy := -. speed_multiplier_Y *. 1.; + vx := speed_multiplier *. Float.sin ((float_of_int !camera_angle_y) *. 3.1415926535 /. 180.); | 'd' -> - 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_global_2 hash cx cy cz - ) then move_cam_hash_2 hash cx cy cz false 'q' - | 'p' -> - camera_xyz.y <- camera_xyz.y -. speed_multiplier ; - if b && ( - is_collision_global_2 hash cx cy cz - ) then begin Stdlib.print_endline "Nope (p)" ; move_cam_hash_2 hash cx cy cz false 'm' end - | 'm' -> - camera_xyz.y <- camera_xyz.y +. speed_multiplier ; - if b && ( - is_collision_global_2 hash cx cy cz - ) then begin Stdlib.print_endline "Nope (m)" ; move_cam_hash_2 hash cx cy cz false 'p' end - | 'a' -> camera_angle_y := !camera_angle_y + 1 - | 'e' -> camera_angle_y := !camera_angle_y - 1 + vz := speed_multiplier *. Float.cos (((float_of_int !camera_angle_y) +. 90.) *. 3.1415926535 /. 180.); + vy := -. speed_multiplier_Y *. 1.; + vx := -. speed_multiplier *. Float.sin (((float_of_int !camera_angle_y) +. 90.) *. 3.1415926535 /. 180.); + | 's' -> + vz := speed_multiplier *. Float.cos ((float_of_int !camera_angle_y) *. 3.1415926535 /. 180.); + vy := -. speed_multiplier_Y *. 1.; + vx := -. speed_multiplier *. Float.sin ((float_of_int !camera_angle_y) *. 3.1415926535 /. 180.); + | 'q' -> + vz := speed_multiplier *. Float.cos (((float_of_int !camera_angle_y) -. 90.) *. 3.1415926535 /. 180.); + vy := -. speed_multiplier_Y *. 1.; + vx := -. speed_multiplier *. Float.sin (((float_of_int !camera_angle_y) -. 90.) *. 3.1415926535 /. 180.); + | 'p' -> vy := -. speed_multiplier_Y *. 2.5 + | 'm' -> vx := 0. ; vz := 0. + | 'a' -> camera_angle_y := !camera_angle_y + 15 + | 'e' -> camera_angle_y := !camera_angle_y - 15 | _ -> () ;; let init_chunk_all hash mem ch_x ch_y ch_z = @@ -1676,34 +1684,76 @@ let draw_inventories () = draw_integer_alignedleft 40 (__height__ - (240 + 50 * (Array.length playerOreInventory -1 -i))) playerOreInventory.(i) 20 done ;; -exception CannotPass ;; -let move_auto_y hash redraw = - try - let (cx, cy, cz) = coords_to_chunk_f (-. camera_xyz.x) (-. camera_xyz.y) camera_xyz.z in - for i = 1 to 15 do - camera_xyz.y <- camera_xyz.y +. !vy /. 15.; +(* ----------------------------------------| Motion-related functions |---------------------------------------- *) + +let move_all_at_once hash redraw = + let (cx, cy, cz) = coords_to_chunk_f (-. camera_xyz.x) (-. camera_xyz.y) camera_xyz.z in + let do_x = ref (!vx <> 0.) + and do_y = ref (!vy <> 0.) + and do_z = ref (!vz <> 0.) in + (*Printf.printf "[%b %b %b]\n" !do_x !do_y !do_z ;*) + for i = 1 to precision do + (**) + if !do_x then begin + camera_xyz.x <- camera_xyz.x +. !vx /. precisionf ; + let new_cx = ctcf_one (-. camera_xyz.x) in + if is_collision_global_G hash new_cx cy cz then begin + if i <> 1 then + redraw := true; + camera_xyz.x <- camera_xyz.x -. !vx /. precisionf ; + do_x := false; + vx := 0. *. sign !vx + end + end; + (**) + if !do_y then begin + camera_xyz.y <- camera_xyz.y +. !vy /. precisionf ; let new_cy = ctcf_one (-. camera_xyz.y) in if is_collision_global_G hash cx new_cy cz then begin if i <> 1 then redraw := true; - camera_xyz.y <- camera_xyz.y -. !vy /. 15. ; - raise CannotPass + camera_xyz.y <- camera_xyz.y -. !vy /. precisionf ; + do_y := false; + vy := 0. *. sign !vy end - else - () - done ; - redraw := true; - () - with - | CannotPass -> vy := 0.; () ;; + end; + (**) + if !do_z then begin + camera_xyz.z <- camera_xyz.z -. !vz /. precisionf ; + let new_cz = ctcf_one (camera_xyz.z) in + if is_collision_global_G hash cx cy new_cz then begin + if i <> 1 then + redraw := true; + camera_xyz.z <- camera_xyz.z +. !vz /. precisionf ; + do_z := false; + vz := 0. *. sign !vz + end + end + done; + if !do_x || !do_y || !do_z then + redraw := true ;; +let friction dt = + vx := !vx -. !vx *. f *. dt ; + if absf !vx < vel_epsilon then vx := 0. ; -let update_gravity dt hash redraw = + vy := !vy -. !vy *. f *. dt ; + if absf !vy < vel_epsilon then vy := 0. ; + + vz := !vz -. !vz *. f *. dt ; + if absf !vz < vel_epsilon then vz := 0. ;; + +let update_velocities dt hash redraw = + (* move the player from T = t to T = t + dt *) vy := !vy +. gravity *. dt ; - Printf.printf "{%f, %f, %f}" !vx !vy !vz; - Stdlib.print_endline " "; - move_auto_y hash redraw ;; + (*Printf.printf "{%f, %f, %f}" !vx !vy !vz; + Stdlib.print_endline " " ;*) + move_all_at_once hash redraw ; + friction dt ;; +(* ----------------------------------------| Main function |---------------------------------------- *) + +let gray = rgb 128 128 128 ;; let play_dos laby = try Stdlib.print_endline "Building terrain..."; @@ -1715,8 +1765,10 @@ let play_dos laby = let memory = Hashtbl.create 100 in let dyna = dyn_create {flag = "spawn" ; cube = create_cube 0 0 0 1 ; red = 30 ; green = 30 ; blue = 30} in - Hashtbl.add hash (0, 0, 0) dyna ; + dyn_append dyna {flag = "spawn" ; cube = create_rect 0 0 0 chunk_size 1 chunk_size ; red = 234 ; green = 100 ; blue = 234} ; + Hashtbl.add hash (0, 0, 0) dyna ; (* set the spawning chunk to be empty with a bottom platform *) Hashtbl.add memory (0, 0, 0) 1; + (* N/A = unloaded chunk <0 = failed structure gen @@ -1732,9 +1784,9 @@ 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. ; + camera_xyz.x <- (-. 0.5 *. (float_of_int chunk_size)) ; + camera_xyz.y <- (-. 0.5 *. (float_of_int chunk_size)) ; + camera_xyz.z <- 0.5 *. (float_of_int chunk_size) ; while true do let time_s = Unix.gettimeofday() in @@ -1759,18 +1811,35 @@ let play_dos laby = let usr_input = get1char_plus () in if usr_input <> '@' then begin - Stdlib.print_endline "EEEEE"; - for i = 0 to 15 do + (*Stdlib.print_endline "EEEEE";*) + (*for i = 0 to 15 do move_cam_hash_2 hash !ch_x !ch_y !ch_z true usr_input - done; + done;*) + add_velocity usr_input ; redraw := true end; - Unix.sleepf 0.001; let time_e = Unix.gettimeofday() in + + let delta_t = time_e -. time_s in + + update_velocities delta_t hash redraw ; + + if (1. /. max_fps) -. delta_t > 0. then begin + Unix.sleepf (1. /. max_fps -. delta_t); + set_color black ; + fill_ellipse (__width__ - 100) (__height__ - 30) 100 40 ; + set_color gray ; + draw_integer_alignedleft (__width__ - 150) (__height__ - 30) (int_of_float max_fps) 25 + end + else begin + set_color black ; + fill_ellipse (__width__ - 100) (__height__ - 30) 100 40 ; + set_color gray ; + draw_integer_alignedleft (__width__ - 150) (__height__ - 30) (int_of_float (1. /. delta_t)) 25 + end (*Printf.printf "[%f]" (time_e -. time_s); Stdlib.print_endline " ";*) - update_gravity (time_e -. time_s) hash redraw done ; () with diff --git a/display.o b/display.o index b47a3cb..462cfbd 100644 Binary files a/display.o and b/display.o differ diff --git a/stone.bmp b/stone.bmp deleted file mode 100644 index a852ac3..0000000 Binary files a/stone.bmp and /dev/null differ diff --git a/stoned.bmp b/stoned.bmp deleted file mode 100644 index a852ac3..0000000 Binary files a/stoned.bmp and /dev/null differ diff --git a/textures/carbon_ingot.bmp b/textures/carbon_ingot.bmp deleted file mode 100644 index a052dfd..0000000 Binary files a/textures/carbon_ingot.bmp and /dev/null differ diff --git a/textures/carbon_ingot.png b/textures/carbon_ingot.png deleted file mode 100644 index 56a6f7e..0000000 Binary files a/textures/carbon_ingot.png and /dev/null differ diff --git a/textures/copper_ingot.bmp b/textures/copper_ingot.bmp deleted file mode 100644 index 3fd652d..0000000 Binary files a/textures/copper_ingot.bmp and /dev/null differ diff --git a/textures/diamond.bmp b/textures/diamond.bmp deleted file mode 100644 index 4b65865..0000000 Binary files a/textures/diamond.bmp and /dev/null differ diff --git a/textures/gold_ingot.bmp b/textures/gold_ingot.bmp deleted file mode 100644 index 7c5c188..0000000 Binary files a/textures/gold_ingot.bmp and /dev/null differ diff --git a/textures/iron_ingot.bmp b/textures/iron_ingot.bmp deleted file mode 100644 index ece8099..0000000 Binary files a/textures/iron_ingot.bmp and /dev/null differ diff --git a/textures/iron_ingot.png b/textures/iron_ingot.png deleted file mode 100644 index c332bd2..0000000 Binary files a/textures/iron_ingot.png and /dev/null differ diff --git a/textures/nether_star.bmp b/textures/nether_star.bmp deleted file mode 100644 index 45bd16a..0000000 Binary files a/textures/nether_star.bmp and /dev/null differ diff --git a/textures/tungsten.bmp b/textures/tungsten.bmp deleted file mode 100644 index 1e3ef33..0000000 Binary files a/textures/tungsten.bmp and /dev/null differ diff --git a/textures/zinc_ingot.bmp b/textures/zinc_ingot.bmp deleted file mode 100644 index 3040d63..0000000 Binary files a/textures/zinc_ingot.bmp and /dev/null differ