| 1 |
/* |
| 2 |
* Portions of this file are copyright Rebirth contributors and licensed as |
| 3 |
* described in COPYING.txt. |
| 4 |
* Portions of this file are copyright Parallax Software and licensed |
| 5 |
* according to the Parallax license below. |
| 6 |
* See COPYING.txt for license details. |
| 7 |
|
| 8 |
THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX |
| 9 |
SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO |
| 10 |
END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A |
| 11 |
ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS |
| 12 |
IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS |
| 13 |
SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE |
| 14 |
FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE |
| 15 |
CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS |
| 16 |
AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE. |
| 17 |
COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED. |
| 18 |
*/ |
| 19 |
|
| 20 |
/* |
| 21 |
* |
| 22 |
* New home for find_vector_intersection() |
| 23 |
* |
| 24 |
*/ |
| 25 |
|
| 26 |
#include <algorithm> |
| 27 |
#include <stdio.h> |
| 28 |
#include <stdlib.h> |
| 29 |
#include <string.h> |
| 30 |
#include "pstypes.h" |
| 31 |
#include "u_mem.h" |
| 32 |
#include "dxxerror.h" |
| 33 |
#include "inferno.h" |
| 34 |
#include "fvi.h" |
| 35 |
#include "segment.h" |
| 36 |
#include "object.h" |
| 37 |
#include "wall.h" |
| 38 |
#include "laser.h" |
| 39 |
#include "gameseg.h" |
| 40 |
#include "rle.h" |
| 41 |
#include "robot.h" |
| 42 |
#include "piggy.h" |
| 43 |
#include "player.h" |
| 44 |
#include "compiler-range_for.h" |
| 45 |
#include "d_levelstate.h" |
| 46 |
#include "segiter.h" |
| 47 |
|
| 48 |
using std::min; |
| 49 |
|
| 50 |
namespace { |
| 51 |
|
| 52 |
//intersection types |
| 53 |
enum class intersection_type : uint8_t |
| 54 |
{ |
| 55 |
None, //doesn't touch face at all |
| 56 |
Face, //touches face |
| 57 |
Edge, //touches edge of face |
| 58 |
}; |
| 59 |
|
| 60 |
/* Find the point on the specified plane where the line intersects. |
| 61 |
* Return a std::optional containing the point if found. |
| 62 |
* Return an empty std::optional if no point is found, due to the line being |
| 63 |
* parallel to the plane. |
| 64 |
*/ |
| 65 |
//plane_pnt & plane_norm describe the plane |
| 66 |
//p0 & p1 are the ends of the line |
| 67 |
[[nodiscard]] |
| 68 |
static std::optional<vms_vector> find_plane_line_intersection(const vms_vector &plane_pnt, const vms_vector &plane_norm, const vms_vector &p0, const vms_vector &p1, const fix rad) |
| 69 |
{ |
| 70 |
auto d{vm_vec_build_sub(p1, p0)}; |
| 71 |
const fix den{-vm_vec_build_dot(plane_norm, d)}; |
| 72 |
if (unlikely(!den)) // moving parallel to wall, so can't hit it |
| 73 |
return std::nullopt; |
| 74 |
|
| 75 |
const auto w{vm_vec_build_sub(p0, plane_pnt)}; |
| 76 |
fix num{vm_vec_build_dot(plane_norm,w) - rad}; //move point out by rad |
| 77 |
|
| 78 |
//check for various bad values |
| 79 |
if (den > 0 && (-num>>15) >= den) //will overflow (large negative) |
| 80 |
num = (f1_0-f0_5)*den; |
| 81 |
if (den > 0 && num > den) //frac greater than one |
| 82 |
return std::nullopt; |
| 83 |
if (den < 0 && num < den) //frac greater than one |
| 84 |
return std::nullopt; |
| 85 |
if (labs (num) / (f1_0 / 2) >= labs (den)) |
| 86 |
return std::nullopt; |
| 87 |
|
| 88 |
vm_vec_scale2(d,num,den); |
| 89 |
return vm_vec_build_add(p0, std::move(d)); |
| 90 |
} |
| 91 |
|
| 92 |
struct vec2d { |
| 93 |
fix i,j; |
| 94 |
}; |
| 95 |
|
| 96 |
struct ij_pair |
| 97 |
{ |
| 98 |
fix vms_vector::*largest_normal; |
| 99 |
fix vms_vector::*i; |
| 100 |
fix vms_vector::*j; |
| 101 |
}; |
| 102 |
|
| 103 |
[[nodiscard]] |
| 104 |
static ij_pair find_largest_normal(vms_vector t) |
| 105 |
{ |
| 106 |
t.x = labs(t.x); |
| 107 |
t.y = labs(t.y); |
| 108 |
t.z = labs(t.z); |
| 109 |
if (t.x > t.y) |
| 110 |
{ |
| 111 |
if (t.x > t.z) |
| 112 |
return {&vms_vector::x, &vms_vector::z, &vms_vector::y}; |
| 113 |
} |
| 114 |
else if (t.y > t.z) |
| 115 |
return {&vms_vector::y, &vms_vector::x, &vms_vector::z}; |
| 116 |
return {&vms_vector::z, &vms_vector::y, &vms_vector::x}; |
| 117 |
} |
| 118 |
|
| 119 |
//see if a point in inside a face by projecting into 2d |
| 120 |
[[nodiscard]] |
| 121 |
static unsigned check_point_to_face(const vms_vector &checkp, const vms_vector &norm, const unsigned facenum, const unsigned nv, const vertnum_array_list_t &vertex_list) |
| 122 |
{ |
| 123 |
auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state(); |
| 124 |
auto &Vertices = LevelSharedVertexState.get_vertices(); |
| 125 |
/// |
| 126 |
//now do 2d check to see if point is in side |
| 127 |
|
| 128 |
//project polygon onto plane by finding largest component of normal |
| 129 |
ij_pair ij = find_largest_normal(norm); |
| 130 |
if (norm.*ij.largest_normal <= 0) |
| 131 |
{ |
| 132 |
using std::swap; |
| 133 |
swap(ij.i, ij.j); |
| 134 |
} |
| 135 |
|
| 136 |
//now do the 2d problem in the i,j plane |
| 137 |
|
| 138 |
const auto check_i{checkp.*ij.i}; |
| 139 |
const auto check_j{checkp.*ij.j}; |
| 140 |
|
| 141 |
auto &vcvertptr = Vertices.vcptr; |
| 142 |
unsigned edgemask{}; |
| 143 |
for (const auto edge : xrange(nv)) |
| 144 |
{ |
| 145 |
auto &v0 = *vcvertptr(vertex_list[facenum * 3 + edge]); |
| 146 |
auto &v1 = *vcvertptr(vertex_list[facenum * 3 + ((edge + 1) % nv)]); |
| 147 |
const vec2d edgevec{ |
| 148 |
.i = v1.*ij.i - v0.*ij.i, |
| 149 |
.j = v1.*ij.j - v0.*ij.j |
| 150 |
}; |
| 151 |
const vec2d checkvec{ |
| 152 |
.i = check_i - v0.*ij.i, |
| 153 |
.j = check_j - v0.*ij.j |
| 154 |
}; |
| 155 |
const fix64 d{fixmul64(checkvec.i, edgevec.j) - fixmul64(checkvec.j, edgevec.i)}; |
| 156 |
if (d < 0) //we are outside of triangle |
| 157 |
edgemask |= (1<<edge); |
| 158 |
} |
| 159 |
return edgemask; |
| 160 |
} |
| 161 |
|
| 162 |
//check if a sphere intersects a face |
| 163 |
[[nodiscard]] |
| 164 |
static intersection_type check_sphere_to_face(const vms_vector &pnt, const vms_vector &normal, const unsigned facenum, const unsigned nv, const fix rad, const vertnum_array_list_t &vertex_list) |
| 165 |
{ |
| 166 |
auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state(); |
| 167 |
auto &Vertices = LevelSharedVertexState.get_vertices(); |
| 168 |
const auto checkp{pnt}; |
| 169 |
//now do 2d check to see if point is in side |
| 170 |
|
| 171 |
auto edgemask{check_point_to_face(pnt, normal, facenum, nv, vertex_list)}; |
| 172 |
|
| 173 |
//we've gone through all the sides, are we inside? |
| 174 |
|
| 175 |
if (edgemask == 0) |
| 176 |
return intersection_type::Face; |
| 177 |
else { |
| 178 |
vms_vector edgevec; //this time, real 3d vectors |
| 179 |
int edgenum; |
| 180 |
|
| 181 |
//get verts for edge we're behind |
| 182 |
|
| 183 |
for (edgenum=0;!(edgemask&1);(edgemask>>=1),edgenum++); |
| 184 |
|
| 185 |
auto &vcvertptr = Vertices.vcptr; |
| 186 |
auto &v0 = *vcvertptr(vertex_list[facenum * 3 + edgenum]); |
| 187 |
auto &v1 = *vcvertptr(vertex_list[facenum * 3 + ((edgenum + 1) % nv)]); |
| 188 |
|
| 189 |
//check if we are touching an edge or point |
| 190 |
|
| 191 |
const auto edgelen = vm_vec_normalized_dir(edgevec,v1,v0); |
| 192 |
|
| 193 |
//find point dist from planes of ends of edge |
| 194 |
|
| 195 |
const auto d = vm_vec_build_dot(edgevec, vm_vec_build_sub(checkp, v0)); |
| 196 |
if (d < 0) |
| 197 |
return intersection_type::None; |
| 198 |
else if (d > edgelen) |
| 199 |
return intersection_type::None; |
| 200 |
|
| 201 |
if (d+rad < 0) |
| 202 |
return intersection_type::None; //too far behind start point |
| 203 |
|
| 204 |
if (d-rad > edgelen) |
| 205 |
return intersection_type::None; //too far part end point |
| 206 |
|
| 207 |
//find closest point on edge to check point |
| 208 |
const auto dist = vm_vec_dist2(checkp, /* closest_point */ vm_vec_scale_add(v0, edgevec, d)); |
| 209 |
const fix64 rad64{rad}; |
| 210 |
if (dist > vm_distance_squared{rad64 * rad64}) |
| 211 |
return intersection_type::None; |
| 212 |
return intersection_type::Edge; |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
/* Return `std::optional<vms_vector>` indicating whether line intersects with |
| 217 |
* face. If the line intersects, then the optional contains a `vms_vector` |
| 218 |
* that tells where on the plane the intersection occurred. If the line does |
| 219 |
* not intersect, then the optional does not contain a value. |
| 220 |
*/ |
| 221 |
//facenum determines which of four possible faces we have |
| 222 |
//note: the seg parm is temporary, until the face itself has a point field |
| 223 |
[[nodiscard]] |
| 224 |
static std::optional<vms_vector> check_line_to_face(const vms_vector &p0, const vms_vector &p1, const shared_segment &seg, const sidenum_t side, const unsigned facenum, const unsigned nv, const fix rad) |
| 225 |
{ |
| 226 |
auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state(); |
| 227 |
auto &Vertices = LevelSharedVertexState.get_vertices(); |
| 228 |
auto &s = seg.sides[side]; |
| 229 |
const vms_vector &norm = s.normals[facenum]; |
| 230 |
|
| 231 |
const auto &&[num_faces, vertex_list] = create_abs_vertex_lists(seg, s, side); |
| 232 |
|
| 233 |
//use lowest point number |
| 234 |
const auto vertnum{ |
| 235 |
(num_faces != vertex_array_side_type::quad) |
| 236 |
? std::min(vertex_list[0], vertex_list[2]) |
| 237 |
: *std::ranges::min_element(std::span(vertex_list).first<4>()) |
| 238 |
}; |
| 239 |
|
| 240 |
const auto result{find_plane_line_intersection(Vertices.vcptr(vertnum), norm, p0, p1, rad)}; |
| 241 |
/* If `hit_type == None`, then `hit_point` is undefined, because no hit |
| 242 |
* occurred. |
| 243 |
* |
| 244 |
* Otherwise, `hit_point` is defined, and can be used in the next check. |
| 245 |
*/ |
| 246 |
if (!result) |
| 247 |
return result; |
| 248 |
|
| 249 |
vms_vector projected_point; |
| 250 |
auto &hit_point = *result; |
| 251 |
auto &checkp{ |
| 252 |
/* If `rad == 0`, then use the point as it was. |
| 253 |
* If `rad != 0`, then copy the point into a local, project the copy |
| 254 |
* down onto the plane of the polygon, and check that projected point. |
| 255 |
*/ |
| 256 |
rad == 0 |
| 257 |
? hit_point |
| 258 |
: (vm_vec_scale_add2(projected_point = hit_point, norm, -rad), projected_point) |
| 259 |
}; |
| 260 |
if (check_sphere_to_face(checkp, s.normals[facenum], facenum, nv, rad, vertex_list) == intersection_type::None) |
| 261 |
return std::nullopt; |
| 262 |
return result; |
| 263 |
} |
| 264 |
|
| 265 |
//returns the value of a determinant |
| 266 |
[[nodiscard]] |
| 267 |
static fix calc_det_value(const std::pair<vms_vector, vms_vector> &rfvec, const vms_vector &uvec) |
| 268 |
{ |
| 269 |
return fixmul(rfvec.first.x, fixmul(uvec.y, rfvec.second.z)) - |
| 270 |
fixmul(rfvec.first.x, fixmul(uvec.z, rfvec.second.y)) - |
| 271 |
fixmul(rfvec.first.y, fixmul(uvec.x, rfvec.second.z)) + |
| 272 |
fixmul(rfvec.first.y, fixmul(uvec.z, rfvec.second.x)) + |
| 273 |
fixmul(rfvec.first.z, fixmul(uvec.x, rfvec.second.y)) - |
| 274 |
fixmul(rfvec.first.z, fixmul(uvec.y, rfvec.second.x)); |
| 275 |
} |
| 276 |
|
| 277 |
//computes the parameters of closest approach of two lines |
| 278 |
//fill in two parameters, t0 & t1. returns 0 if lines are parallel, else 1 |
| 279 |
[[nodiscard]] |
| 280 |
static std::optional<std::pair<fix, fix>> check_line_to_line(const vms_vector &p1, const vms_vector &v1, const vms_vector &p2, const vms_vector &v2) |
| 281 |
{ |
| 282 |
std::pair<vms_vector, vms_vector> rfvec; |
| 283 |
auto &detf = rfvec.second; |
| 284 |
detf = vm_vec_cross(v1, v2); |
| 285 |
const auto cross_mag2{vm_vec_build_dot(detf, detf)}; |
| 286 |
|
| 287 |
if (cross_mag2 == 0) |
| 288 |
return std::nullopt; //lines are parallel |
| 289 |
|
| 290 |
auto &detr = rfvec.first; |
| 291 |
vm_vec_sub(detr, p2, p1); |
| 292 |
const auto dv2{calc_det_value(rfvec, v2)}; |
| 293 |
const auto dv1{calc_det_value(rfvec, v1)}; |
| 294 |
|
| 295 |
const auto t1{fixdiv(dv2, cross_mag2)}; |
| 296 |
const auto t2{fixdiv(dv1, cross_mag2)}; |
| 297 |
return std::pair(t1, t2); //found point |
| 298 |
} |
| 299 |
|
| 300 |
//this version is for when the start and end positions both poke through |
| 301 |
//the plane of a side. In this case, we must do checks against the edge |
| 302 |
//of faces |
| 303 |
[[nodiscard]] |
| 304 |
static std::optional<vms_vector> special_check_line_to_face(const vms_vector &p0, const vms_vector &p1, const shared_segment &seg, const sidenum_t side, const unsigned facenum, const unsigned nv, const fix rad) |
| 305 |
{ |
| 306 |
auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state(); |
| 307 |
auto &Vertices = LevelSharedVertexState.get_vertices(); |
| 308 |
fix edge_t2{0}; |
| 309 |
int edgenum; |
| 310 |
auto &s = seg.sides[side]; |
| 311 |
|
| 312 |
//calc some basic stuff |
| 313 |
|
| 314 |
const auto &&vertex_list = create_abs_vertex_lists(seg, s, side).vertex_list; |
| 315 |
|
| 316 |
//figure out which edge(s) to check against |
| 317 |
|
| 318 |
unsigned edgemask = check_point_to_face(p0, s.normals[facenum], facenum, nv, vertex_list); |
| 319 |
|
| 320 |
if (edgemask == 0) |
| 321 |
return check_line_to_face(p0, p1, seg, side, facenum, nv, rad); |
| 322 |
|
| 323 |
for (edgenum=0;!(edgemask&1);edgemask>>=1,edgenum++); |
| 324 |
|
| 325 |
auto &vcvertptr = Vertices.vcptr; |
| 326 |
auto &edge_v0 = *vcvertptr(vertex_list[facenum * 3 + edgenum]); |
| 327 |
auto &edge_v1 = *vcvertptr(vertex_list[facenum * 3 + ((edgenum + 1) % nv)]); |
| 328 |
|
| 329 |
auto edge_vec{vm_vec_build_sub(edge_v1, edge_v0)}; |
| 330 |
|
| 331 |
//is the start point already touching the edge? |
| 332 |
|
| 333 |
//?? |
| 334 |
|
| 335 |
//first, find point of closest approach of vec & edge |
| 336 |
|
| 337 |
const auto edge_len{vm_vec_normalize(edge_vec)}; |
| 338 |
auto move_vec{vm_vec_build_sub(p1, p0)}; |
| 339 |
const auto move_len{vm_vec_normalize(move_vec)}; |
| 340 |
|
| 341 |
const auto &cll = check_line_to_line(edge_v0,edge_vec,p0,move_vec); |
| 342 |
if (!cll) |
| 343 |
return std::nullopt; |
| 344 |
auto &&[edge_t, move_t] = *cll; |
| 345 |
|
| 346 |
//make sure t values are in valid range |
| 347 |
if (move_t<0 || move_t>move_len+rad) |
| 348 |
return std::nullopt; |
| 349 |
|
| 350 |
const fix move_t2{ |
| 351 |
(move_t > move_len) |
| 352 |
? move_len |
| 353 |
: move_t |
| 354 |
}; |
| 355 |
|
| 356 |
if (edge_t < 0) //saturate at points |
| 357 |
edge_t2 = 0; |
| 358 |
else |
| 359 |
edge_t2 = edge_t; |
| 360 |
|
| 361 |
if (edge_t2 > edge_len) //saturate at points |
| 362 |
edge_t2 = edge_len; |
| 363 |
|
| 364 |
//now, edge_t & move_t determine closest points. calculate the points. |
| 365 |
|
| 366 |
const auto closest_point_edge{vm_vec_scale_add(edge_v0, edge_vec, edge_t2)}; |
| 367 |
const auto closest_point_move{vm_vec_scale_add(p0, move_vec, move_t2)}; |
| 368 |
|
| 369 |
//find dist between closest points |
| 370 |
|
| 371 |
const auto closest_dist{vm_vec_dist2(closest_point_edge, closest_point_move)}; |
| 372 |
|
| 373 |
//could we hit with this dist? |
| 374 |
|
| 375 |
//note massive tolerance here |
| 376 |
const vm_distance fudge_rad{(rad * 15) / 20}; |
| 377 |
/* First test whether the square of the closest distance is less than the |
| 378 |
* radius. If yes, then the intersection is very close, and the |
| 379 |
* multiplication can be skipped. If no, test whether the square of the |
| 380 |
* closest distance is less than the square of the radius. |
| 381 |
*/ |
| 382 |
if (underlying_value(closest_dist) < fudge_rad || closest_dist < fudge_rad * fudge_rad) //we hit. figure out where |
| 383 |
{ |
| 384 |
//now figure out where we hit |
| 385 |
return vm_vec_scale_add(p0, move_vec, move_t - rad); |
| 386 |
} |
| 387 |
else |
| 388 |
return std::nullopt; //no hit |
| 389 |
} |
| 390 |
|
| 391 |
//maybe this routine should just return the distance and let the caller |
| 392 |
//decide it it's close enough to hit |
| 393 |
//determine if and where a vector intersects with a sphere |
| 394 |
//vector defined by p0,p1 |
| 395 |
//returns dist if intersects, and fills in intp |
| 396 |
//else returns 0 |
| 397 |
[[nodiscard]] |
| 398 |
static vm_distance_squared check_vector_to_sphere_1(vms_vector &intp,const vms_vector &p0,const vms_vector &p1,const vms_vector &sphere_pos,fix sphere_rad) |
| 399 |
{ |
| 400 |
vms_vector dn; |
| 401 |
|
| 402 |
//this routine could be optimized if it's taking too much time! |
| 403 |
|
| 404 |
const auto d{vm_vec_build_sub(p1, p0)}; |
| 405 |
const auto w{vm_vec_build_sub(sphere_pos, p0)}; |
| 406 |
|
| 407 |
const auto mag_d{vm_vec_copy_normalize(dn, d)}; |
| 408 |
|
| 409 |
if (mag_d == 0) { |
| 410 |
const auto int_dist{vm_vec_mag2(w)}; |
| 411 |
intp = p0; |
| 412 |
if (int_dist < sphere_rad) |
| 413 |
return build_vm_distance_squared(int_dist); |
| 414 |
const fix64 sphere_rad64{sphere_rad}; |
| 415 |
if (int_dist < vm_magnitude_squared{static_cast<uint64_t>(sphere_rad64 * sphere_rad64)}) |
| 416 |
return build_vm_distance_squared(int_dist); |
| 417 |
return vm_distance_squared::minimum_value; |
| 418 |
} |
| 419 |
|
| 420 |
const fix w_dist{vm_vec_build_dot(dn, w)}; |
| 421 |
|
| 422 |
if (w_dist < 0) //moving away from object |
| 423 |
return vm_distance_squared::minimum_value; |
| 424 |
|
| 425 |
if (w_dist > mag_d+sphere_rad) |
| 426 |
return vm_distance_squared::minimum_value; //cannot hit |
| 427 |
|
| 428 |
const auto closest_point{vm_vec_scale_add(p0, dn, w_dist)}; |
| 429 |
|
| 430 |
const auto dist2{vm_vec_dist2(closest_point, sphere_pos)}; |
| 431 |
const fix64 sphere_rad64{sphere_rad}; |
| 432 |
const vm_distance_squared sphere_rad_squared{sphere_rad64 * sphere_rad64}; |
| 433 |
if (dist2 < sphere_rad_squared) |
| 434 |
{ |
| 435 |
const fix64 delta_squared{static_cast<fix64>(sphere_rad_squared) - static_cast<fix64>(dist2)}; |
| 436 |
const fix delta{static_cast<fix>(delta_squared >> 16)}; |
| 437 |
const auto shorten{fix_sqrt(delta)}; |
| 438 |
const auto int_dist{w_dist - shorten}; |
| 439 |
|
| 440 |
if (int_dist > mag_d || int_dist < 0) //past one or the other end of vector, which means we're inside |
| 441 |
{ |
| 442 |
//past one or the other end of vector, which means we're inside? WRONG! Either you're inside OR you didn't quite make it! |
| 443 |
if (vm_vec_dist2(p0, sphere_pos) < sphere_rad_squared) |
| 444 |
{ |
| 445 |
intp = p0; //don't move at all |
| 446 |
return vm_distance_squared{static_cast<fix64>(1)}; // note that we do not calculate a valid collision point. This is up to collision handling. |
| 447 |
} else { |
| 448 |
return vm_distance_squared::minimum_value; |
| 449 |
} |
| 450 |
} |
| 451 |
|
| 452 |
intp = vm_vec_scale_add(p0, dn, int_dist); //calc intersection point |
| 453 |
return vm_distance_squared{static_cast<fix64>(int_dist) * int_dist}; |
| 454 |
} |
| 455 |
else |
| 456 |
return vm_distance_squared::minimum_value; |
| 457 |
} |
| 458 |
|
| 459 |
/* |
| 460 |
//$$fix get_sphere_int_dist(vms_vector *w,fix dist,fix rad); |
| 461 |
//$$ |
| 462 |
//$$#pragma aux get_sphere_int_dist parm [esi] [ebx] [ecx] value [eax] modify exact [eax ebx ecx edx] = \ |
| 463 |
//$$ "mov eax,ebx" \ |
| 464 |
//$$ "imul eax" \ |
| 465 |
//$$ \ |
| 466 |
//$$ "mov ebx,eax" \ |
| 467 |
//$$ "mov eax,ecx" \ |
| 468 |
//$$ "mov ecx,edx" \ |
| 469 |
//$$ \ |
| 470 |
//$$ "imul eax" \ |
| 471 |
//$$ \ |
| 472 |
//$$ "sub eax,ebx" \ |
| 473 |
//$$ "sbb edx,ecx" \ |
| 474 |
//$$ \ |
| 475 |
//$$ "call quad_sqrt" \ |
| 476 |
//$$ \ |
| 477 |
//$$ "push eax" \ |
| 478 |
//$$ \ |
| 479 |
//$$ "push ebx" \ |
| 480 |
//$$ "push ecx" \ |
| 481 |
//$$ \ |
| 482 |
//$$ "mov eax,[esi]" \ |
| 483 |
//$$ "imul eax" \ |
| 484 |
//$$ "mov ebx,eax" \ |
| 485 |
//$$ "mov ecx,edx" \ |
| 486 |
//$$ "mov eax,4[esi]" \ |
| 487 |
//$$ "imul eax" \ |
| 488 |
//$$ "add ebx,eax" \ |
| 489 |
//$$ "adc ecx,edx" \ |
| 490 |
//$$ "mov eax,8[esi]" \ |
| 491 |
//$$ "imul eax" \ |
| 492 |
//$$ "add eax,ebx" \ |
| 493 |
//$$ "adc edx,ecx" \ |
| 494 |
//$$ \ |
| 495 |
//$$ "pop ecx" \ |
| 496 |
//$$ "pop ebx" \ |
| 497 |
//$$ \ |
| 498 |
//$$ "sub eax,ebx" \ |
| 499 |
//$$ "sbb edx,ecx" \ |
| 500 |
//$$ \ |
| 501 |
//$$ "call quad_sqrt" \ |
| 502 |
//$$ \ |
| 503 |
//$$ "pop ebx" \ |
| 504 |
//$$ "sub eax,ebx"; |
| 505 |
//$$ |
| 506 |
//$$ |
| 507 |
//$$//determine if and where a vector intersects with a sphere |
| 508 |
//$$//vector defined by p0,p1 |
| 509 |
//$$//returns dist if intersects, and fills in intp. if no intersect, return 0 |
| 510 |
//$$fix check_vector_to_sphere_2(vms_vector *intp,vms_vector *p0,vms_vector *p1,vms_vector *sphere_pos,fix sphere_rad) |
| 511 |
//$${ |
| 512 |
//$$ vms_vector d,w,c; |
| 513 |
//$$ fix mag_d,dist,mag_c,mag_w; |
| 514 |
//$$ vms_vector wn,dn; |
| 515 |
//$$ |
| 516 |
//$$ vm_vec_sub(&d,p1,p0); |
| 517 |
//$$ vm_vec_sub(&w,sphere_pos,p0); |
| 518 |
//$$ |
| 519 |
//$$ //wn = w; mag_w = vm_vec_normalize(&wn); |
| 520 |
//$$ //dn = d; mag_d = vm_vec_normalize(&dn); |
| 521 |
//$$ |
| 522 |
//$$ mag_w = vm_vec_copy_normalize(&wn,&w); |
| 523 |
//$$ mag_d = vm_vec_copy_normalize(&dn,&d); |
| 524 |
//$$ |
| 525 |
//$$ //vm_vec_cross(&c,&w,&d); |
| 526 |
//$$ vm_vec_cross(&c,&wn,&dn); |
| 527 |
//$$ |
| 528 |
//$$ mag_c = vm_vec_mag(&c); |
| 529 |
//$$ //mag_d = vm_vec_mag(&d); |
| 530 |
//$$ |
| 531 |
//$$ //dist = fixdiv(mag_c,mag_d); |
| 532 |
//$$ |
| 533 |
//$$dist = fixmul(mag_c,mag_w); |
| 534 |
//$$ |
| 535 |
//$$ if (dist < sphere_rad) { //we intersect. find point of intersection |
| 536 |
//$$ fix int_dist; //length of vector to intersection point |
| 537 |
//$$ fix k; //portion of p0p1 we want |
| 538 |
//$$//@@ fix dist2,rad2,shorten,mag_w2; |
| 539 |
//$$ |
| 540 |
//$$//@@ mag_w2 = vm_vec_build_dot(&w,&w); //the square of the magnitude |
| 541 |
//$$//@@ //WHAT ABOUT OVERFLOW??? |
| 542 |
//$$//@@ dist2 = fixmul(dist,dist); |
| 543 |
//$$//@@ rad2 = fixmul(sphere_rad,sphere_rad); |
| 544 |
//$$//@@ shorten = fix_sqrt(rad2 - dist2); |
| 545 |
//$$//@@ int_dist = fix_sqrt(mag_w2 - dist2) - shorten; |
| 546 |
//$$ |
| 547 |
//$$ int_dist = get_sphere_int_dist(&w,dist,sphere_rad); |
| 548 |
//$$ |
| 549 |
//$$if (labs(int_dist) > mag_d) //I don't know why this would happen |
| 550 |
//$$ if (int_dist > 0) |
| 551 |
//$$ k = f1_0; |
| 552 |
//$$ else |
| 553 |
//$$ k = -f1_0; |
| 554 |
//$$else |
| 555 |
//$$ k = fixdiv(int_dist,mag_d); |
| 556 |
//$$ |
| 557 |
//$$// vm_vec_scale(&d,k); //vec from p0 to intersection point |
| 558 |
//$$// vm_vec_add(intp,p0,&d); //intersection point |
| 559 |
//$$ vm_vec_scale_add(intp,p0,&d,k); //calc new intersection point |
| 560 |
//$$ |
| 561 |
//$$ return int_dist; |
| 562 |
//$$ } |
| 563 |
//$$ else |
| 564 |
//$$ return 0; //no intersection |
| 565 |
//$$} |
| 566 |
*/ |
| 567 |
|
| 568 |
//determine if a vector intersects with an object |
| 569 |
//if no intersects, returns 0, else fills in intp and returns dist |
| 570 |
[[nodiscard]] |
| 571 |
static vm_distance_squared check_vector_to_object(const d_robot_info_array *const Robot_info, vms_vector &intp, const vms_vector &p0, const vms_vector &p1, const fix rad, const object_base &obj, const object &otherobj) |
| 572 |
{ |
| 573 |
fix size{obj.size}; |
| 574 |
|
| 575 |
if (obj.type == OBJ_ROBOT) |
| 576 |
{ |
| 577 |
if ((*Robot_info)[get_robot_id(obj)].attack_type) |
| 578 |
size = (size*3)/4; |
| 579 |
} |
| 580 |
//if obj is player, and bumping into other player or a weapon of another coop player, reduce radius |
| 581 |
else if (obj.type == OBJ_PLAYER && |
| 582 |
(otherobj.type == OBJ_PLAYER || |
| 583 |
((Game_mode & GM_MULTI_COOP) && otherobj.type == OBJ_WEAPON && otherobj.ctype.laser_info.parent_type == OBJ_PLAYER))) |
| 584 |
size = size/2; |
| 585 |
|
| 586 |
return check_vector_to_sphere_1(intp, p0, p1, obj.pos, size+rad); |
| 587 |
} |
| 588 |
|
| 589 |
#define MAX_SEGS_VISITED 100 |
| 590 |
struct fvi_segment_visit_count_t |
| 591 |
{ |
| 592 |
unsigned count{}; |
| 593 |
}; |
| 594 |
|
| 595 |
struct fvi_segments_visited_t : public fvi_segment_visit_count_t, public visited_segment_bitarray_t |
| 596 |
{ |
| 597 |
}; |
| 598 |
|
| 599 |
} |
| 600 |
|
| 601 |
namespace dsx { |
| 602 |
namespace { |
| 603 |
static fvi_hit_type fvi_sub(const fvi_query &, vms_vector &intp, segnum_t &ints, const vcsegptridx_t startseg, fix rad, fvi_info::segment_array_t &seglist, segnum_t entry_seg, fvi_segments_visited_t &visited, sidenum_t &fvi_hit_side, icsegidx_t &fvi_hit_side_seg, unsigned &fvi_nest_count, icsegidx_t &fvi_hit_pt_seg, const vms_vector *&wall_norm, icobjidx_t &fvi_hit_object); |
| 604 |
} |
| 605 |
|
| 606 |
//What the hell is fvi_hit_seg for??? |
| 607 |
|
| 608 |
//Find out if a vector intersects with anything. |
| 609 |
//Fills in hit_data, an fvi_info structure (see header file). |
| 610 |
//Parms: |
| 611 |
// p0 & startseg describe the start of the vector |
| 612 |
// p1 the end of the vector |
| 613 |
// rad the radius of the cylinder |
| 614 |
// thisobjnum used to prevent an object with colliding with itself |
| 615 |
// ingore_obj ignore collisions with this object |
| 616 |
// check_obj_flag determines whether collisions with objects are checked |
| 617 |
//Returns the hit_data->hit_type |
| 618 |
fvi_hit_type find_vector_intersection(const fvi_query fq, const segnum_t startseg, const fix rad, fvi_info &hit_data) |
| 619 |
{ |
| 620 |
auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state(); |
| 621 |
auto &Vertices = LevelSharedVertexState.get_vertices(); |
| 622 |
vms_vector hit_pnt; |
| 623 |
|
| 624 |
icobjidx_t fvi_hit_object = object_none; // object number of object hit in last find_vector_intersection call. |
| 625 |
|
| 626 |
//check to make sure start point is in seg its supposed to be in |
| 627 |
//Assert(check_point_in_seg(p0,startseg,0).centermask==0); //start point not in seg |
| 628 |
|
| 629 |
// invalid segnum, so say there is no hit. |
| 630 |
if (startseg > Highest_segment_index) |
| 631 |
{ |
| 632 |
assert(startseg <= Highest_segment_index); |
| 633 |
hit_data.hit_pnt = fq.p0; |
| 634 |
hit_data.hit_seg = segnum_t{}; |
| 635 |
hit_data.hit_object = 0; |
| 636 |
hit_data.hit_side = side_none; |
| 637 |
hit_data.hit_side_seg = segment_none; |
| 638 |
return fvi_hit_type::BadP0; |
| 639 |
} |
| 640 |
|
| 641 |
auto &vcvertptr = Vertices.vcptr; |
| 642 |
// Viewer is not in segment as claimed, so say there is no hit. |
| 643 |
if (get_seg_masks(vcvertptr, fq.p0, vcsegptr(startseg), 0).centermask != sidemask_t{}) |
| 644 |
{ |
| 645 |
hit_data.hit_pnt = fq.p0; |
| 646 |
hit_data.hit_seg = startseg; |
| 647 |
hit_data.hit_side = side_none; |
| 648 |
hit_data.hit_object = 0; |
| 649 |
hit_data.hit_side_seg = segment_none; |
| 650 |
return fvi_hit_type::BadP0; |
| 651 |
} |
| 652 |
|
| 653 |
fvi_segments_visited_t visited; |
| 654 |
visited[startseg] = true; |
| 655 |
|
| 656 |
sidenum_t fvi_hit_side; |
| 657 |
icsegidx_t fvi_hit_side_seg{segment_none}; // what seg the hitside is in |
| 658 |
unsigned fvi_nest_count{}; |
| 659 |
|
| 660 |
icsegidx_t fvi_hit_pt_seg{segment_none}; // what segment the hit point is in |
| 661 |
segnum_t hit_seg2{segment_none}; |
| 662 |
|
| 663 |
const vms_vector *wall_norm{nullptr}; //surface normal of hit wall |
| 664 |
const auto hit_type = fvi_sub(fq, hit_pnt, hit_seg2, vcsegptridx(startseg), rad, hit_data.seglist, segment_exit, visited, fvi_hit_side, fvi_hit_side_seg, fvi_nest_count, fvi_hit_pt_seg, wall_norm, fvi_hit_object); |
| 665 |
segnum_t hit_seg; |
| 666 |
if (hit_seg2 != segment_none && get_seg_masks(vcvertptr, hit_pnt, vcsegptr(hit_seg2), 0).centermask == sidemask_t{}) |
| 667 |
hit_seg = hit_seg2; |
| 668 |
else |
| 669 |
hit_seg = find_point_seg(LevelSharedSegmentState, LevelUniqueSegmentState, hit_pnt, imsegptridx(startseg)); |
| 670 |
|
| 671 |
//MATT: TAKE OUT THIS HACK AND FIX THE BUGS! |
| 672 |
if (hit_type == fvi_hit_type::Wall && hit_seg == segment_none) |
| 673 |
if (fvi_hit_pt_seg != segment_none && get_seg_masks(vcvertptr, hit_pnt, vcsegptr(fvi_hit_pt_seg), 0).centermask == sidemask_t{}) |
| 674 |
hit_seg = fvi_hit_pt_seg; |
| 675 |
|
| 676 |
if (hit_seg == segment_none) { |
| 677 |
segnum_t new_hit_seg2{segment_none}; |
| 678 |
vms_vector new_hit_pnt; |
| 679 |
|
| 680 |
//because of code that deal with object with non-zero radius has |
| 681 |
//problems, try using zero radius and see if we hit a wall |
| 682 |
|
| 683 |
const auto new_hit_type{fvi_sub(fq, new_hit_pnt, new_hit_seg2, vcsegptridx(startseg), 0, hit_data.seglist, segment_exit, visited, fvi_hit_side, fvi_hit_side_seg, fvi_nest_count, fvi_hit_pt_seg, wall_norm, fvi_hit_object)}; |
| 684 |
(void)new_hit_type; // FIXME! This should become hit_type, right? |
| 685 |
|
| 686 |
if (new_hit_seg2 != segment_none) { |
| 687 |
hit_seg = new_hit_seg2; |
| 688 |
hit_pnt = new_hit_pnt; |
| 689 |
} |
| 690 |
} |
| 691 |
|
| 692 |
|
| 693 |
if (hit_seg!=segment_none && (fq.flags & FQ_GET_SEGLIST)) |
| 694 |
{ |
| 695 |
fvi_info::segment_array_t::iterator i = hit_data.seglist.find(hit_seg), e = hit_data.seglist.end(); |
| 696 |
/* If `hit_seg` is present in `seglist`, truncate `seglist` such that |
| 697 |
* `seglist.back()` == `hit_seg`. |
| 698 |
* |
| 699 |
* Otherwise, `hit_seg` is not present in `seglist`. If there is space |
| 700 |
* to add it, then add it. |
| 701 |
*/ |
| 702 |
if (i != e) |
| 703 |
hit_data.seglist.erase(++i); |
| 704 |
else if (hit_data.seglist.size() < hit_data.seglist.max_size()) |
| 705 |
hit_data.seglist.emplace_back(hit_seg); |
| 706 |
} |
| 707 |
|
| 708 |
//I'm sorry to say that sometimes the seglist isn't correct. I did my |
| 709 |
//best. Really. |
| 710 |
|
| 711 |
|
| 712 |
//{ //verify hit list |
| 713 |
// |
| 714 |
// int i,ch; |
| 715 |
// |
| 716 |
// Assert(hit_data->seglist[0] == startseg); |
| 717 |
// |
| 718 |
// for (i=0;i<hit_data->n_segs-1;i++) { |
| 719 |
// for (ch=0;ch<6;ch++) |
| 720 |
// if (Segments[hit_data->seglist[i]].children[ch] == hit_data->seglist[i+1]) |
| 721 |
// break; |
| 722 |
// Assert(ch<6); |
| 723 |
// } |
| 724 |
// |
| 725 |
// Assert(hit_data->seglist[hit_data->n_segs-1] == hit_seg); |
| 726 |
//} |
| 727 |
|
| 728 |
|
| 729 |
//MATT: PUT THESE ASSERTS BACK IN AND FIX THE BUGS! |
| 730 |
//!! Assert(hit_seg!=-1); |
| 731 |
//!! Assert(!((hit_type==HIT_WALL) && (hit_seg == -1))); |
| 732 |
//When this assert happens, get Matt. Matt: Look at hit_seg2 & |
| 733 |
//fvi_hit_seg. At least one of these should be set. Why didn't |
| 734 |
//find_new_seg() find something? |
| 735 |
|
| 736 |
// Assert(fvi_hit_seg==-1 || fvi_hit_seg == hit_seg); |
| 737 |
|
| 738 |
assert(!(hit_type == fvi_hit_type::Object && fvi_hit_object == object_none)); |
| 739 |
|
| 740 |
hit_data.hit_pnt = hit_pnt; |
| 741 |
hit_data.hit_seg = hit_seg; |
| 742 |
hit_data.hit_side = fvi_hit_side; //looks at global |
| 743 |
hit_data.hit_side_seg = fvi_hit_side_seg; //looks at global |
| 744 |
hit_data.hit_object = fvi_hit_object; //looks at global |
| 745 |
if (wall_norm) |
| 746 |
hit_data.hit_wallnorm = *wall_norm; |
| 747 |
else |
| 748 |
{ |
| 749 |
hit_data.hit_wallnorm = {}; |
| 750 |
DXX_MAKE_VAR_UNDEFINED(hit_data.hit_wallnorm); |
| 751 |
} |
| 752 |
|
| 753 |
// if(hit_seg != -1 && get_seg_masks(&hit_data->hit_pnt, hit_data->hit_seg, 0, __FILE__, __LINE__).centermask != 0) |
| 754 |
// Int3(); |
| 755 |
|
| 756 |
return hit_type; |
| 757 |
} |
| 758 |
|
| 759 |
namespace { |
| 760 |
|
| 761 |
static int check_trans_wall(const vms_vector &pnt, vcsegptridx_t seg, sidenum_t sidenum, int facenum); |
| 762 |
} |
| 763 |
} |
| 764 |
|
| 765 |
namespace { |
| 766 |
static void append_segments(fvi_info::segment_array_t &dst, const fvi_info::segment_array_t &src) |
| 767 |
{ |
| 768 |
/* Avoid overflow. Original code had n_segs < MAX_SEGS_VISITED-1, |
| 769 |
* so leave an extra slot on min. |
| 770 |
*/ |
| 771 |
const size_t scount{src.size()}, dcount{dst.size()}, count{std::min(scount, dst.max_size() - dcount - 1)}; |
| 772 |
std::copy(src.begin(), src.begin() + count, std::back_inserter(dst)); |
| 773 |
} |
| 774 |
} |
| 775 |
|
| 776 |
namespace dsx { |
| 777 |
namespace { |
| 778 |
static fvi_hit_type fvi_sub(const fvi_query &fq, vms_vector &intp, segnum_t &ints, const vcsegptridx_t startseg, fix rad, fvi_info::segment_array_t &seglist, segnum_t entry_seg, fvi_segments_visited_t &visited, sidenum_t &fvi_hit_side, icsegidx_t &fvi_hit_side_seg, unsigned &fvi_nest_count, icsegidx_t &fvi_hit_pt_seg, const vms_vector *&wall_norm, icobjidx_t &fvi_hit_object) |
| 779 |
{ |
| 780 |
auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state(); |
| 781 |
auto &Vertices = LevelSharedVertexState.get_vertices(); |
| 782 |
vms_vector closest_hit_point{}; //where we hit |
| 783 |
auto closest_d{vm_distance_squared::maximum_value}; //distance to hit point |
| 784 |
auto hit_type{fvi_hit_type::None}; //what sort of hit |
| 785 |
segnum_t hit_seg{segment_none}; |
| 786 |
fvi_info::segment_array_t hit_none_seglist; |
| 787 |
|
| 788 |
seglist.clear(); |
| 789 |
if (fq.flags & FQ_GET_SEGLIST) |
| 790 |
seglist.emplace_back(startseg); |
| 791 |
|
| 792 |
const unsigned cur_nest_level{fvi_nest_count}; |
| 793 |
fvi_nest_count++; |
| 794 |
|
| 795 |
//first, see if vector hit any objects in this segment |
| 796 |
if (const auto LevelUniqueObjectState = fq.LevelUniqueObjectState) |
| 797 |
{ |
| 798 |
auto &Objects = LevelUniqueObjectState->Objects; |
| 799 |
auto &vcobjptridx = Objects.vcptridx; |
| 800 |
/* A caller which provides LevelUniqueObjectState must provide a valid object. |
| 801 |
* Obtain a require_valid instance once, before the loop begins. |
| 802 |
*/ |
| 803 |
const vcobjptridx_t thisobjnum = fq.thisobjnum; |
| 804 |
const auto this_is_robot{thisobjnum->type == OBJ_ROBOT}; |
| 805 |
const auto &collision = CollisionResult[thisobjnum->type]; |
| 806 |
const auto Robot_info{fq.Robot_info}; |
| 807 |
const robot_info *const robptrthis{this_is_robot |
| 808 |
? &(assert(Robot_info != nullptr), (*Robot_info)[get_robot_id(thisobjnum)]) |
| 809 |
: nullptr |
| 810 |
}; |
| 811 |
range_for (const auto objnum, objects_in(*startseg, vcobjptridx, vcsegptr)) |
| 812 |
{ |
| 813 |
if (thisobjnum == objnum) |
| 814 |
continue; |
| 815 |
if (objnum->flags & OF_SHOULD_BE_DEAD) |
| 816 |
continue; |
| 817 |
if (collision[objnum->type] == collision_result::ignore) |
| 818 |
continue; |
| 819 |
if (laser_are_related(objnum, thisobjnum)) |
| 820 |
continue; |
| 821 |
if (std::ranges::find(fq.ignore_obj_list, objnum) != fq.ignore_obj_list.end()) |
| 822 |
continue; |
| 823 |
int fudged_rad = rad; |
| 824 |
|
| 825 |
#if DXX_BUILD_DESCENT == 2 |
| 826 |
// If this is a powerup, don't do collision if flag FQ_IGNORE_POWERUPS is set |
| 827 |
if (objnum->type == OBJ_POWERUP) |
| 828 |
if (fq.flags & FQ_IGNORE_POWERUPS) |
| 829 |
continue; |
| 830 |
#endif |
| 831 |
|
| 832 |
/* |
| 833 |
* In Descent 1: |
| 834 |
// If this is a robot:robot collision, only do it if both of them have attack_type != 0 (eg, green guy) |
| 835 |
* |
| 836 |
* In Descent 2: |
| 837 |
* Robot-vs-robot collisions never happen. This appears to have |
| 838 |
* been done for the benefit of the Diamond Claw. However, it also |
| 839 |
* has the effect of allowing the Thief to pass through other |
| 840 |
* robots unharmed. |
| 841 |
*/ |
| 842 |
if (robptrthis) |
| 843 |
{ |
| 844 |
if (objnum->type == OBJ_ROBOT) |
| 845 |
{ |
| 846 |
#if DXX_BUILD_DESCENT == 1 |
| 847 |
if (!((*Robot_info)[get_robot_id(objnum)].attack_type && robptrthis->attack_type)) |
| 848 |
#endif |
| 849 |
// -- MK: 11/18/95, 4claws glomming together...this is easy. -- if (!(Robot_info[Objects[objnum].id].attack_type && Robot_info[Objects[thisobjnum].id].attack_type)) |
| 850 |
continue; |
| 851 |
} |
| 852 |
if (robptrthis->attack_type) |
| 853 |
fudged_rad = (rad * 3) / 4; |
| 854 |
} |
| 855 |
//if obj is player, and bumping into other player or a weapon of another coop player, reduce radius |
| 856 |
else if (fq.thisobjnum->type == OBJ_PLAYER && |
| 857 |
((objnum->type == OBJ_PLAYER) || |
| 858 |
((Game_mode&GM_MULTI_COOP) && objnum->type == OBJ_WEAPON && objnum->ctype.laser_info.parent_type == OBJ_PLAYER))) |
| 859 |
fudged_rad = rad/2; //(rad*3)/4; |
| 860 |
|
| 861 |
vms_vector hit_point; |
| 862 |
const auto &&d = check_vector_to_object(Robot_info, hit_point, fq.p0, fq.p1, fudged_rad, objnum, thisobjnum); |
| 863 |
if (d != vm_distance_squared{0}) //we have intersection |
| 864 |
if (d < closest_d) { |
| 865 |
fvi_hit_object = objnum; |
| 866 |
Assert(fvi_hit_object!=object_none); |
| 867 |
closest_d = d; |
| 868 |
closest_hit_point = hit_point; |
| 869 |
hit_type = fvi_hit_type::Object; |
| 870 |
} |
| 871 |
} |
| 872 |
} |
| 873 |
|
| 874 |
if (fq.thisobjnum != object_none && CollisionResult[fq.thisobjnum->type][OBJ_WALL] == collision_result::ignore) |
| 875 |
rad = 0; //HACK - ignore when edges hit walls |
| 876 |
|
| 877 |
//now, check segment walls |
| 878 |
|
| 879 |
auto &vcvertptr = Vertices.vcptr; |
| 880 |
int startmask{get_seg_masks(vcvertptr, fq.p0, startseg, rad).facemask}; |
| 881 |
|
| 882 |
const auto &&masks = get_seg_masks(vcvertptr, fq.p1, startseg, rad); //on back of which faces? |
| 883 |
const auto centermask{masks.centermask}; //where the center point is |
| 884 |
|
| 885 |
segnum_t hit_none_seg{segment_none}; |
| 886 |
if (centermask == sidemask_t{}) |
| 887 |
hit_none_seg = startseg; |
| 888 |
|
| 889 |
if (const auto endmask{masks.facemask}) |
| 890 |
{ //on the back of at least one face |
| 891 |
//for each face we are on the back of, check if intersected |
| 892 |
|
| 893 |
int bit{1}; |
| 894 |
for (const auto side : MAX_SIDES_PER_SEGMENT) |
| 895 |
{ |
| 896 |
if (endmask < bit) |
| 897 |
break; |
| 898 |
const unsigned nv{get_side_is_quad(startseg->shared_segment::sides[side]) ? 4u : 3u}; |
| 899 |
// commented out by mk on 02/13/94:: if ((num_faces=seg->sides[side].num_faces)==0) num_faces=1; |
| 900 |
|
| 901 |
for (int face=0; face < 2; ++face, bit <<= 1) |
| 902 |
{ |
| 903 |
if (endmask & bit) { //on the back of this face |
| 904 |
|
| 905 |
const auto child_segnum{startseg->shared_segment::children[side]}; |
| 906 |
if (child_segnum == entry_seg) |
| 907 |
continue; //don't go back through entry side |
| 908 |
|
| 909 |
//did we go through this wall/door? |
| 910 |
|
| 911 |
// face_hit_type: in what way did we hit the face? |
| 912 |
const auto &&opt_hit_point{ |
| 913 |
(startmask & bit) //start was also through. Do extra check |
| 914 |
? special_check_line_to_face(fq.p0, fq.p1, startseg, side, face, nv, rad) |
| 915 |
: check_line_to_face(fq.p0, fq.p1, startseg, side, face, nv, rad) |
| 916 |
}; |
| 917 |
|
| 918 |
if (opt_hit_point) |
| 919 |
{ //through this wall/door |
| 920 |
auto &hit_point = *opt_hit_point; |
| 921 |
auto &Walls = LevelUniqueWallSubsystemState.Walls; |
| 922 |
auto &vcwallptr = Walls.vcptr; |
| 923 |
auto wid_flag = WALL_IS_DOORWAY(GameBitmaps, Textures, vcwallptr, startseg, side); |
| 924 |
|
| 925 |
//if what we have hit is a door, check the adjoining seg |
| 926 |
|
| 927 |
if (fq.thisobjnum == get_local_player().objnum && cheats.ghostphysics) |
| 928 |
{ |
| 929 |
if (IS_CHILD(child_segnum)) |
| 930 |
wid_flag |= WALL_IS_DOORWAY_FLAG::fly; |
| 931 |
} |
| 932 |
|
| 933 |
if ((wid_flag & WALL_IS_DOORWAY_FLAG::fly) || |
| 934 |
( |
| 935 |
#if DXX_BUILD_DESCENT == 1 |
| 936 |
(wid_flag == wall_is_doorway_result::transparent_wall) && |
| 937 |
#elif DXX_BUILD_DESCENT == 2 |
| 938 |
((wid_flag & WALL_IS_DOORWAY_FLAG::render) && (wid_flag & WALL_IS_DOORWAY_FLAG::rendpast)) && |
| 939 |
#endif |
| 940 |
((fq.flags & FQ_TRANSWALL) || (fq.flags & FQ_TRANSPOINT && check_trans_wall(hit_point,startseg,side,face))))) { |
| 941 |
|
| 942 |
segnum_t newsegnum,sub_hit_seg; |
| 943 |
vms_vector sub_hit_point; |
| 944 |
const auto save_wall_norm{wall_norm}; |
| 945 |
auto save_hit_objnum{fvi_hit_object}; |
| 946 |
|
| 947 |
//do the check recursively on the next seg. |
| 948 |
|
| 949 |
newsegnum = child_segnum; |
| 950 |
|
| 951 |
if (auto &&v = visited[newsegnum]; !v) { //haven't visited here yet |
| 952 |
v = true; |
| 953 |
++ visited.count; |
| 954 |
|
| 955 |
if (visited.count >= MAX_SEGS_VISITED) |
| 956 |
goto quit_looking; //we've looked a long time, so give up |
| 957 |
|
| 958 |
fvi_info::segment_array_t temp_seglist; |
| 959 |
const auto sub_hit_type = fvi_sub(fq, sub_hit_point, sub_hit_seg, startseg.absolute_sibling(newsegnum), rad, temp_seglist, startseg, visited, fvi_hit_side, fvi_hit_side_seg, fvi_nest_count, fvi_hit_pt_seg, wall_norm, fvi_hit_object); |
| 960 |
|
| 961 |
if (sub_hit_type != fvi_hit_type::None) |
| 962 |
{ |
| 963 |
const auto d{vm_vec_dist2(sub_hit_point, fq.p0)}; |
| 964 |
if (d < closest_d) { |
| 965 |
closest_d = d; |
| 966 |
closest_hit_point = sub_hit_point; |
| 967 |
hit_type = sub_hit_type; |
| 968 |
if (sub_hit_seg!=segment_none) hit_seg = sub_hit_seg; |
| 969 |
|
| 970 |
//copy seglist |
| 971 |
if (fq.flags & FQ_GET_SEGLIST) { |
| 972 |
append_segments(seglist, temp_seglist); |
| 973 |
} |
| 974 |
} |
| 975 |
else { |
| 976 |
wall_norm = save_wall_norm; //global could be trashed |
| 977 |
fvi_hit_object = save_hit_objnum; |
| 978 |
} |
| 979 |
} |
| 980 |
else { |
| 981 |
wall_norm = save_wall_norm; //global could be trashed |
| 982 |
if (sub_hit_seg!=segment_none) hit_none_seg = sub_hit_seg; |
| 983 |
//copy seglist |
| 984 |
if (fq.flags & FQ_GET_SEGLIST) |
| 985 |
hit_none_seglist = temp_seglist; |
| 986 |
} |
| 987 |
} |
| 988 |
} |
| 989 |
else { //a wall |
| 990 |
//is this the closest hit? |
| 991 |
const auto d{vm_vec_dist2(hit_point, fq.p0)}; |
| 992 |
if (d < closest_d) { |
| 993 |
closest_d = d; |
| 994 |
closest_hit_point = hit_point; |
| 995 |
hit_type = fvi_hit_type::Wall; |
| 996 |
wall_norm = &startseg->shared_segment::sides[side].normals[face]; |
| 997 |
if (get_seg_masks(vcvertptr, hit_point, startseg, rad).centermask == sidemask_t{}) |
| 998 |
hit_seg = startseg; //hit in this segment |
| 999 |
else |
| 1000 |
fvi_hit_pt_seg = startseg; |
| 1001 |
|
| 1002 |
fvi_hit_side = side; |
| 1003 |
fvi_hit_side_seg = startseg; |
| 1004 |
|
| 1005 |
} |
| 1006 |
} |
| 1007 |
} |
| 1008 |
} |
| 1009 |
} |
| 1010 |
} |
| 1011 |
} |
| 1012 |
|
| 1013 |
// Assert(centermask==0 || hit_seg!=startseg); |
| 1014 |
|
| 1015 |
// Assert(sidemask==0); //Error("Didn't find side we went though"); |
| 1016 |
|
| 1017 |
quit_looking: |
| 1018 |
; |
| 1019 |
|
| 1020 |
if (hit_type == fvi_hit_type::None) |
| 1021 |
{ //didn't hit anything, return end point |
| 1022 |
intp = fq.p1; |
| 1023 |
ints = hit_none_seg; |
| 1024 |
//MATT: MUST FIX THIS!!!! |
| 1025 |
//Assert(!centermask); |
| 1026 |
|
| 1027 |
if (hit_none_seg!=segment_none) { ///(centermask == 0) |
| 1028 |
if (fq.flags & FQ_GET_SEGLIST) |
| 1029 |
//copy seglist |
| 1030 |
append_segments(seglist, hit_none_seglist); |
| 1031 |
} |
| 1032 |
else |
| 1033 |
if (cur_nest_level!=0) |
| 1034 |
seglist.clear(); |
| 1035 |
|
| 1036 |
} |
| 1037 |
else { |
| 1038 |
intp = closest_hit_point; |
| 1039 |
if (hit_seg==segment_none) |
| 1040 |
if (fvi_hit_pt_seg != segment_none) |
| 1041 |
ints = fvi_hit_pt_seg; |
| 1042 |
else |
| 1043 |
ints = hit_none_seg; |
| 1044 |
else |
| 1045 |
ints = hit_seg; |
| 1046 |
} |
| 1047 |
|
| 1048 |
assert(!(hit_type == fvi_hit_type::Object && fvi_hit_object == object_none)); |
| 1049 |
return hit_type; |
| 1050 |
} |
| 1051 |
} |
| 1052 |
} |
| 1053 |
|
| 1054 |
/* |
| 1055 |
//--unused-- //compute the magnitude of a 2d vector |
| 1056 |
//--unused-- fix mag2d(vec2d *v); |
| 1057 |
//--unused-- #pragma aux mag2d parm [esi] value [eax] modify exact [eax ebx ecx edx] = \ |
| 1058 |
//--unused-- "mov eax,[esi]" \ |
| 1059 |
//--unused-- "imul eax" \ |
| 1060 |
//--unused-- "mov ebx,eax" \ |
| 1061 |
//--unused-- "mov ecx,edx" \ |
| 1062 |
//--unused-- "mov eax,4[esi]" \ |
| 1063 |
//--unused-- "imul eax" \ |
| 1064 |
//--unused-- "add eax,ebx" \ |
| 1065 |
//--unused-- "adc edx,ecx" \ |
| 1066 |
//--unused-- "call quad_sqrt"; |
| 1067 |
*/ |
| 1068 |
|
| 1069 |
//--unused-- //returns mag |
| 1070 |
//--unused-- fix normalize_2d(vec2d *v) |
| 1071 |
//--unused-- { |
| 1072 |
//--unused-- fix mag; |
| 1073 |
//--unused-- |
| 1074 |
//--unused-- mag = mag2d(v); |
| 1075 |
//--unused-- |
| 1076 |
//--unused-- v->i = fixdiv(v->i,mag); |
| 1077 |
//--unused-- v->j = fixdiv(v->j,mag); |
| 1078 |
//--unused-- |
| 1079 |
//--unused-- return mag; |
| 1080 |
//--unused-- } |
| 1081 |
|
| 1082 |
#include "textures.h" |
| 1083 |
#include "texmerge.h" |
| 1084 |
|
| 1085 |
#define cross(v0,v1) (fixmul((v0)->i,(v1)->j) - fixmul((v0)->j,(v1)->i)) |
| 1086 |
|
| 1087 |
//finds the uv coords of the given point on the given seg & side |
| 1088 |
//fills in u & v. if l is non-NULL fills it in also |
| 1089 |
namespace dsx { |
| 1090 |
fvi_hitpoint find_hitpoint_uv(const vms_vector &pnt, const cscusegment seg, const sidenum_t sidenum, const uint_fast32_t facenum) |
| 1091 |
{ |
| 1092 |
auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state(); |
| 1093 |
auto &Vertices = LevelSharedVertexState.get_vertices(); |
| 1094 |
auto &side = seg.s.sides[sidenum]; |
| 1095 |
|
| 1096 |
//do lasers pass through illusory walls? |
| 1097 |
|
| 1098 |
//when do I return 0 & 1 for non-transparent walls? |
| 1099 |
|
| 1100 |
const auto &&vn = create_all_vertnum_lists(seg, side, sidenum); |
| 1101 |
|
| 1102 |
//now the hard work. |
| 1103 |
|
| 1104 |
//1. find what plane to project this wall onto to make it a 2d case |
| 1105 |
|
| 1106 |
const auto &normal_array = side.normals[facenum]; |
| 1107 |
auto fmax = [](const vms_vector &v, fix vms_vector::*a, fix vms_vector::*b) { |
| 1108 |
return abs(v.*a) > abs(v.*b) ? a : b; |
| 1109 |
}; |
| 1110 |
const auto biggest{fmax(normal_array, &vms_vector::z, fmax(normal_array, &vms_vector::y, &vms_vector::x))}; |
| 1111 |
const auto ii{(biggest == &vms_vector::x) ? &vms_vector::y : &vms_vector::x}; |
| 1112 |
const auto jj{(biggest == &vms_vector::z) ? &vms_vector::y : &vms_vector::z}; |
| 1113 |
|
| 1114 |
//2. compute u,v of intersection point |
| 1115 |
|
| 1116 |
//vec from 1 -> 0 |
| 1117 |
auto &vcvertptr = Vertices.vcptr; |
| 1118 |
auto &vf1 = *vcvertptr(vn[facenum * 3 + 1].vertex); |
| 1119 |
const vec2d p1{vf1.*ii, vf1.*jj}; |
| 1120 |
|
| 1121 |
auto &vf0 = *vcvertptr(vn[facenum * 3 + 0].vertex); |
| 1122 |
const vec2d vec0{vf0.*ii - p1.i, vf0.*jj - p1.j}; |
| 1123 |
//vec from 1 -> 2 |
| 1124 |
auto &vf2 = *vcvertptr(vn[facenum * 3 + 2].vertex); |
| 1125 |
const vec2d vec1{vf2.*ii - p1.i, vf2.*jj - p1.j}; |
| 1126 |
|
| 1127 |
//vec from 1 -> checkpoint |
| 1128 |
const vec2d checkp{pnt.*ii, pnt.*jj}; |
| 1129 |
|
| 1130 |
//@@checkv.i = checkp.i - p1.i; |
| 1131 |
//@@checkv.j = checkp.j - p1.j; |
| 1132 |
|
| 1133 |
const fix k1{-fixdiv(cross(&checkp, &vec0) + cross(&vec0, &p1), cross(&vec0, &vec1))}; |
| 1134 |
const fix k0{ |
| 1135 |
#if DXX_BUILD_DESCENT == 1 |
| 1136 |
(vec0.i) |
| 1137 |
#elif DXX_BUILD_DESCENT == 2 |
| 1138 |
(abs(vec0.i) > abs(vec0.j)) |
| 1139 |
#endif |
| 1140 |
? fixdiv(fixmul(-k1, vec1.i) + checkp.i - p1.i, vec0.i) |
| 1141 |
: fixdiv(fixmul(-k1, vec1.j) + checkp.j - p1.j, vec0.j) |
| 1142 |
}; |
| 1143 |
|
| 1144 |
std::array<uvl, 3> uvls; |
| 1145 |
auto &uside = seg.u.sides[sidenum]; |
| 1146 |
for (const auto i : xrange(3u)) |
| 1147 |
uvls[i] = uside.uvls[vn[facenum * 3 + i].vertnum]; |
| 1148 |
|
| 1149 |
auto p = [&uvls, k0, k1](fix uvl::*pmf) { |
| 1150 |
return uvls[1].*pmf + fixmul(k0,uvls[0].*pmf - uvls[1].*pmf) + fixmul(k1,uvls[2].*pmf - uvls[1].*pmf); |
| 1151 |
}; |
| 1152 |
return { |
| 1153 |
p(&uvl::u), |
| 1154 |
p(&uvl::v) |
| 1155 |
}; |
| 1156 |
} |
| 1157 |
|
| 1158 |
namespace { |
| 1159 |
//check if a particular point on a wall is a transparent pixel |
| 1160 |
//returns 1 if can pass though the wall, else 0 |
| 1161 |
int check_trans_wall(const vms_vector &pnt, const vcsegptridx_t seg, const sidenum_t sidenum, const int facenum) |
| 1162 |
{ |
| 1163 |
auto &side = seg->unique_segment::sides[sidenum]; |
| 1164 |
int bmx,bmy; |
| 1165 |
|
| 1166 |
#if DXX_BUILD_DESCENT == 1 |
| 1167 |
#ifndef NDEBUG |
| 1168 |
auto &Walls = LevelUniqueWallSubsystemState.Walls; |
| 1169 |
auto &vcwallptr = Walls.vcptr; |
| 1170 |
assert(WALL_IS_DOORWAY(GameBitmaps, Textures, vcwallptr, seg, sidenum) == wall_is_doorway_result::transparent_wall); |
| 1171 |
#endif |
| 1172 |
#endif |
| 1173 |
|
| 1174 |
const auto hitpoint{find_hitpoint_uv(pnt, seg, sidenum, facenum)}; // Don't compute light value. |
| 1175 |
auto &u = hitpoint.u; |
| 1176 |
auto &v = hitpoint.v; |
| 1177 |
|
| 1178 |
const auto tmap_num{side.tmap_num}; |
| 1179 |
const grs_bitmap &rbm = (side.tmap_num2 != texture2_value::None) |
| 1180 |
? texmerge_get_cached_bitmap(tmap_num, side.tmap_num2) |
| 1181 |
/* gcc-13 issues a -Wdangling-reference warning if this lambda returns |
| 1182 |
* a `const grs_bitmap &`, but no warning if the lambda returns a |
| 1183 |
* `const grs_bitmap *` to the same storage, and then converts it into |
| 1184 |
* a reference in the caller. Since the storage is in `GameBitmaps`, |
| 1185 |
* both uses are safe. |
| 1186 |
* |
| 1187 |
* gcc-14 improved its heuristics and no longer warns for the lambda |
| 1188 |
* returning a `const grs_bitmap &`. |
| 1189 |
*/ |
| 1190 |
: *( [](GameBitmaps_array &GameBitmaps, const bitmap_index texture1) -> const grs_bitmap * { |
| 1191 |
PIGGY_PAGE_IN(texture1); |
| 1192 |
return &GameBitmaps[texture1]; |
| 1193 |
} (GameBitmaps, Textures[get_texture_index(tmap_num)]) ); |
| 1194 |
const auto bm{rle_expand_texture(rbm)}; |
| 1195 |
|
| 1196 |
bmx = static_cast<unsigned>(f2i(u*bm->bm_w)) % bm->bm_w; |
| 1197 |
bmy = static_cast<unsigned>(f2i(v*bm->bm_h)) % bm->bm_h; |
| 1198 |
|
| 1199 |
//note: the line above had -v, but that was wrong, so I changed it. if |
| 1200 |
//something doesn't work, and you want to make it negative again, you |
| 1201 |
//should figure out what's going on. |
| 1202 |
|
| 1203 |
#if DXX_BUILD_DESCENT == 1 |
| 1204 |
return (gr_gpixel (*bm, bmx, bmy) == TRANSPARENCY_COLOR); |
| 1205 |
#elif DXX_BUILD_DESCENT == 2 |
| 1206 |
return (bm->bm_data[bmy*bm->bm_w+bmx] == TRANSPARENCY_COLOR); |
| 1207 |
#endif |
| 1208 |
} |
| 1209 |
} |
| 1210 |
} |
| 1211 |
|
| 1212 |
namespace { |
| 1213 |
//new function for Mike |
| 1214 |
//note: n_segs_visited must be set to zero before this is called |
| 1215 |
static sphere_intersects_wall_result sphere_intersects_wall(fvcsegptridx &vcsegptridx, fvcvertptr &vcvertptr, const vms_vector &pnt, const vcsegptridx_t seg, const fix rad, fvi_segments_visited_t &visited) |
| 1216 |
{ |
| 1217 |
visited[seg] = true; |
| 1218 |
++visited.count; |
| 1219 |
|
| 1220 |
const shared_segment &sseg = seg; |
| 1221 |
const int facemask{get_seg_masks(vcvertptr, pnt, sseg, rad).facemask}; |
| 1222 |
if (facemask != 0) { //on the back of at least one face |
| 1223 |
//for each face we are on the back of, check if intersected |
| 1224 |
|
| 1225 |
int bit{1}; |
| 1226 |
for (const auto side : MAX_SIDES_PER_SEGMENT) |
| 1227 |
{ |
| 1228 |
if (facemask < bit) |
| 1229 |
break; |
| 1230 |
for (int face = 0;face < 2; ++face, bit <<= 1) |
| 1231 |
{ |
| 1232 |
if (facemask & bit) { //on the back of this face |
| 1233 |
//did we go through this wall/door? |
| 1234 |
auto &sidep = sseg.sides[side]; |
| 1235 |
const auto &&[num_faces, vertex_list] = create_abs_vertex_lists(sseg, sidep, side); |
| 1236 |
|
| 1237 |
//in what way did we hit the face? |
| 1238 |
const auto face_hit_type = check_sphere_to_face(pnt, sidep.normals[face], |
| 1239 |
face, ((num_faces == vertex_array_side_type::quad) ? 4 : 3), rad, vertex_list); |
| 1240 |
|
| 1241 |
if (face_hit_type != intersection_type::None) |
| 1242 |
{ //through this wall/door |
| 1243 |
//if what we have hit is a door, check the adjoining seg |
| 1244 |
|
| 1245 |
const auto child = sseg.children[side]; |
| 1246 |
|
| 1247 |
if (!IS_CHILD(child)) |
| 1248 |
{ |
| 1249 |
return {&sseg, side}; |
| 1250 |
} |
| 1251 |
else if (!visited[child]) { //haven't visited here yet |
| 1252 |
const auto &&r = sphere_intersects_wall(vcsegptridx, vcvertptr, pnt, vcsegptridx(child), rad, visited); |
| 1253 |
if (r.seg) |
| 1254 |
return r; |
| 1255 |
} |
| 1256 |
} |
| 1257 |
} |
| 1258 |
} |
| 1259 |
} |
| 1260 |
} |
| 1261 |
return {}; |
| 1262 |
} |
| 1263 |
} |
| 1264 |
|
| 1265 |
sphere_intersects_wall_result sphere_intersects_wall(fvcsegptridx &vcsegptridx, fvcvertptr &vcvertptr, const vms_vector &pnt, const vcsegptridx_t seg, const fix rad) |
| 1266 |
{ |
| 1267 |
fvi_segments_visited_t visited; |
| 1268 |
return sphere_intersects_wall(vcsegptridx, vcvertptr, pnt, seg, rad, visited); |
| 1269 |
} |