build.rs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // primarily used for writing the file
  2. use std::f64::consts::PI;
  3. use std::{env, fs, path::Path};
  4. const PROJECTION_PLANE_WIDTH: usize = 320;
  5. const PROJECTION_PLANE_HEIGHT: usize = 200;
  6. const DISTANCE_TO_PROJECTION_PLANE: usize = 277;
  7. const TILE_SIZE: f64 = 64.0;
  8. const ANGLE_0: usize = 0;
  9. const ANGLE_60: usize = PROJECTION_PLANE_WIDTH;
  10. const ANGLE_30: usize = ANGLE_60 / 2;
  11. const ANGLE_90: usize = ANGLE_30 * 3;
  12. const ANGLE_180: usize = ANGLE_60 * 3;
  13. const ANGLE_270: usize = ANGLE_90 * 3;
  14. const ANGLE_360: usize = ANGLE_60 * 6;
  15. const MAX_RAY_LENGTH: usize = 2048;
  16. const WALL_HEIGHT_SCALE_FACTOR: usize = 18000;
  17. const WALL_HEIGHT_MAX: i32 = 640;
  18. const WALL_HEIGHT_MIN: i32 = 8;
  19. const PLAYER_HEIGHT: usize = 32;
  20. const PROJECTION_PLANE_CENTRE_Y: usize = PROJECTION_PLANE_HEIGHT >> 1;
  21. fn clamp(x: i32, min: i32, max: i32) -> i32 {
  22. if x < min {
  23. min
  24. } else if x > max {
  25. max
  26. } else {
  27. x
  28. }
  29. }
  30. fn radian(angle: usize) -> f64 {
  31. angle as f64 * PI / ANGLE_180 as f64
  32. }
  33. fn iradian(angle: i32) -> f64 {
  34. angle as f64 * PI / ANGLE_180 as f64
  35. }
  36. fn float_to_fix(x: f64) -> i32 {
  37. (x * 65536.0) as i32
  38. }
  39. fn stringify(name: &str, arr: &[i32], size: usize) -> String {
  40. let mut array_string = String::from("static ");
  41. array_string.push_str(name);
  42. array_string.push_str(":[i32; ");
  43. array_string.push_str(size.to_string().as_str());
  44. array_string.push_str("] = [\r\n");
  45. for a in arr {
  46. // a little bit of formatting is happening as well
  47. array_string.push_str("\u{20}\u{20}\u{20}\u{20}");
  48. array_string.push_str(a.to_string().as_str());
  49. array_string.push_str(",\r\n");
  50. }
  51. array_string.push_str("];\r\n");
  52. array_string
  53. }
  54. fn main() {
  55. const SIZE: usize = ANGLE_360 + 1;
  56. let mut sin: [i32; SIZE] = [0; SIZE];
  57. let mut cos: [i32; SIZE] = [0; SIZE];
  58. let mut tan: [i32; SIZE] = [0; SIZE];
  59. let mut isin: [i32; SIZE] = [0; SIZE];
  60. let mut icos: [i32; SIZE] = [0; SIZE];
  61. let mut itan: [i32; SIZE] = [0; SIZE];
  62. for i in 0..=1920 {
  63. sin[i] = float_to_fix(radian(i).sin());
  64. cos[i] = float_to_fix(radian(i).cos());
  65. tan[i] = float_to_fix(radian(i).tan());
  66. isin[i] = float_to_fix(1.0 / radian(i).sin());
  67. icos[i] = float_to_fix(1.0 / radian(i).cos());
  68. itan[i] = float_to_fix(1.0 / radian(i).tan());
  69. }
  70. let mut output = stringify("SIN", &sin, SIZE);
  71. output.push_str(stringify("COS", &cos, SIZE).as_str());
  72. output.push_str(stringify("TAN", &tan, SIZE).as_str());
  73. output.push_str(stringify("ISIN", &isin, SIZE).as_str());
  74. output.push_str(stringify("ICOS", &icos, SIZE).as_str());
  75. output.push_str(stringify("ITAN", &itan, SIZE).as_str());
  76. let mut x_step: [i32; SIZE] = [0; SIZE];
  77. let mut y_step: [i32; SIZE] = [0; SIZE];
  78. for i in 0..=1920 {
  79. let mut step: f64;
  80. if radian(i).tan() == 0.0 {
  81. step = f64::MAX
  82. } else {
  83. step = TILE_SIZE / radian(i).tan();
  84. if i >= ANGLE_90 && i < ANGLE_270 {
  85. if step > 0.0 {
  86. step = -step;
  87. }
  88. } else {
  89. if step < 0.0 {
  90. step = -step;
  91. }
  92. }
  93. }
  94. x_step[i] = float_to_fix(step);
  95. }
  96. for i in 0..=1920 {
  97. let mut step = TILE_SIZE * radian(i).tan();
  98. if i >= ANGLE_0 && i < ANGLE_180 {
  99. if step < 0.0 {
  100. step = -step;
  101. }
  102. } else {
  103. if step > 0.0 {
  104. step = -step;
  105. }
  106. }
  107. y_step[i] = (step * 65536.0) as i32; //float_to_fix(step);
  108. }
  109. output.push_str(stringify("X_STEP", &x_step, SIZE).as_str());
  110. output.push_str(stringify("Y_STEP", &y_step, SIZE).as_str());
  111. let mut fisheye: [i32; PROJECTION_PLANE_WIDTH] = [0; PROJECTION_PLANE_WIDTH];
  112. for i in 0..PROJECTION_PLANE_WIDTH {
  113. fisheye[i] = float_to_fix(1.0 / iradian(i as i32 - ANGLE_30 as i32).cos());
  114. }
  115. output.push_str(stringify("FISHEYE", &fisheye, PROJECTION_PLANE_WIDTH).as_str());
  116. let mut wall_height: [i32; MAX_RAY_LENGTH + 1] = [0; MAX_RAY_LENGTH + 1];
  117. for i in 0..=MAX_RAY_LENGTH {
  118. wall_height[i] = clamp((WALL_HEIGHT_SCALE_FACTOR / i.max(1)) as i32, WALL_HEIGHT_MIN, WALL_HEIGHT_MAX);
  119. }
  120. output.push_str(stringify("WALL_HEIGHT", &wall_height, MAX_RAY_LENGTH + 1).as_str());
  121. // let mut FLOOR_TEXTURE_Y_RAYS: [i32; PROJECTION_PLANE_WIDTH * PROJECTION_PLANE_CENTRE_Y] = [0; PROJECTION_PLANE_WIDTH * PROJECTION_PLANE_CENTRE_Y];
  122. // let mut FLOOR_TEXTURE_X_RAYS: [i32; PROJECTION_PLANE_WIDTH * PROJECTION_PLANE_CENTRE_Y] = [0; PROJECTION_PLANE_WIDTH * PROJECTION_PLANE_CENTRE_Y];
  123. // for y in (PROJECTION_PLANE_CENTRE_Y + 1)..PROJECTION_PLANE_HEIGHT {
  124. // let ratio: f64 = PLAYER_HEIGHT as f64 / (y - PROJECTION_PLANE_CENTRE_Y) as f64;
  125. // for sweep in 0..PROJECTION_PLANE_WIDTH {
  126. // let distance = DISTANCE_TO_PROJECTION_PLANE as f64 * ratio * fisheye[sweep];
  127. // }
  128. // }
  129. // var diagonalDistance=Math.floor((this.fPlayerDistanceToTheProjectionPlane * ratio) * (this.fFishTable[castColumn]));
  130. // var yEnd = Math.floor(diagonalDistance * this.fSinTable[castArc]);
  131. // var xEnd = Math.floor(diagonalDistance * this.fCosTable[castArc]);
  132. // write the string to a file. OUT_DIR environment variable is defined by cargo
  133. let out_dir = env::var("OUT_DIR").unwrap();
  134. let dest_path = Path::new(&out_dir).join("lookup.rs");
  135. fs::write(&dest_path, output).unwrap();
  136. }