world.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925
  1. /*
  2. Copyright (C) 1996-1997 Id Software, Inc.
  3. This program is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU General Public License
  5. as published by the Free Software Foundation; either version 2
  6. of the License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. See the GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. */
  15. // world.c -- world query functions
  16. #include "qwsvdef.h"
  17. /*
  18. entities never clip against themselves, or their owner
  19. line of sight checks trace->crosscontent, but bullets don't
  20. */
  21. typedef struct
  22. {
  23. vec3_t boxmins, boxmaxs;// enclose the test object along entire move
  24. float *mins, *maxs; // size of the moving object
  25. vec3_t mins2, maxs2; // size when clipping against mosnters
  26. float *start, *end;
  27. trace_t trace;
  28. int type;
  29. edict_t *passedict;
  30. } moveclip_t;
  31. int SV_HullPointContents (hull_t *hull, int num, vec3_t p);
  32. /*
  33. ===============================================================================
  34. HULL BOXES
  35. ===============================================================================
  36. */
  37. static hull_t box_hull;
  38. static dclipnode_t box_clipnodes[6];
  39. static mplane_t box_planes[6];
  40. /*
  41. ===================
  42. SV_InitBoxHull
  43. Set up the planes and clipnodes so that the six floats of a bounding box
  44. can just be stored out and get a proper hull_t structure.
  45. ===================
  46. */
  47. void SV_InitBoxHull (void)
  48. {
  49. int i;
  50. int side;
  51. box_hull.clipnodes = box_clipnodes;
  52. box_hull.planes = box_planes;
  53. box_hull.firstclipnode = 0;
  54. box_hull.lastclipnode = 5;
  55. for (i=0 ; i<6 ; i++)
  56. {
  57. box_clipnodes[i].planenum = i;
  58. side = i&1;
  59. box_clipnodes[i].children[side] = CONTENTS_EMPTY;
  60. if (i != 5)
  61. box_clipnodes[i].children[side^1] = i + 1;
  62. else
  63. box_clipnodes[i].children[side^1] = CONTENTS_SOLID;
  64. box_planes[i].type = i>>1;
  65. box_planes[i].normal[i>>1] = 1;
  66. }
  67. }
  68. /*
  69. ===================
  70. SV_HullForBox
  71. To keep everything totally uniform, bounding boxes are turned into small
  72. BSP trees instead of being compared directly.
  73. ===================
  74. */
  75. hull_t *SV_HullForBox (vec3_t mins, vec3_t maxs)
  76. {
  77. box_planes[0].dist = maxs[0];
  78. box_planes[1].dist = mins[0];
  79. box_planes[2].dist = maxs[1];
  80. box_planes[3].dist = mins[1];
  81. box_planes[4].dist = maxs[2];
  82. box_planes[5].dist = mins[2];
  83. return &box_hull;
  84. }
  85. /*
  86. ================
  87. SV_HullForEntity
  88. Returns a hull that can be used for testing or clipping an object of mins/maxs
  89. size.
  90. Offset is filled in to contain the adjustment that must be added to the
  91. testing object's origin to get a point to use with the returned hull.
  92. ================
  93. */
  94. hull_t *SV_HullForEntity (edict_t *ent, vec3_t mins, vec3_t maxs, vec3_t offset)
  95. {
  96. model_t *model;
  97. vec3_t size;
  98. vec3_t hullmins, hullmaxs;
  99. hull_t *hull;
  100. // decide which clipping hull to use, based on the size
  101. if (ent->v.solid == SOLID_BSP)
  102. { // explicit hulls in the BSP model
  103. if (ent->v.movetype != MOVETYPE_PUSH)
  104. SV_Error ("SOLID_BSP without MOVETYPE_PUSH");
  105. model = sv.models[ (int)ent->v.modelindex ];
  106. if (!model || model->type != mod_brush)
  107. SV_Error ("MOVETYPE_PUSH with a non bsp model");
  108. VectorSubtract (maxs, mins, size);
  109. if (size[0] < 3)
  110. hull = &model->hulls[0];
  111. else if (size[0] <= 32)
  112. hull = &model->hulls[1];
  113. else
  114. hull = &model->hulls[2];
  115. // calculate an offset value to center the origin
  116. VectorSubtract (hull->clip_mins, mins, offset);
  117. VectorAdd (offset, ent->v.origin, offset);
  118. }
  119. else
  120. { // create a temp hull from bounding box sizes
  121. VectorSubtract (ent->v.mins, maxs, hullmins);
  122. VectorSubtract (ent->v.maxs, mins, hullmaxs);
  123. hull = SV_HullForBox (hullmins, hullmaxs);
  124. VectorCopy (ent->v.origin, offset);
  125. }
  126. return hull;
  127. }
  128. /*
  129. ===============================================================================
  130. ENTITY AREA CHECKING
  131. ===============================================================================
  132. */
  133. areanode_t sv_areanodes[AREA_NODES];
  134. int sv_numareanodes;
  135. /*
  136. ===============
  137. SV_CreateAreaNode
  138. ===============
  139. */
  140. areanode_t *SV_CreateAreaNode (int depth, vec3_t mins, vec3_t maxs)
  141. {
  142. areanode_t *anode;
  143. vec3_t size;
  144. vec3_t mins1, maxs1, mins2, maxs2;
  145. anode = &sv_areanodes[sv_numareanodes];
  146. sv_numareanodes++;
  147. ClearLink (&anode->trigger_edicts);
  148. ClearLink (&anode->solid_edicts);
  149. if (depth == AREA_DEPTH)
  150. {
  151. anode->axis = -1;
  152. anode->children[0] = anode->children[1] = NULL;
  153. return anode;
  154. }
  155. VectorSubtract (maxs, mins, size);
  156. if (size[0] > size[1])
  157. anode->axis = 0;
  158. else
  159. anode->axis = 1;
  160. anode->dist = 0.5 * (maxs[anode->axis] + mins[anode->axis]);
  161. VectorCopy (mins, mins1);
  162. VectorCopy (mins, mins2);
  163. VectorCopy (maxs, maxs1);
  164. VectorCopy (maxs, maxs2);
  165. maxs1[anode->axis] = mins2[anode->axis] = anode->dist;
  166. anode->children[0] = SV_CreateAreaNode (depth+1, mins2, maxs2);
  167. anode->children[1] = SV_CreateAreaNode (depth+1, mins1, maxs1);
  168. return anode;
  169. }
  170. /*
  171. ===============
  172. SV_ClearWorld
  173. ===============
  174. */
  175. void SV_ClearWorld (void)
  176. {
  177. SV_InitBoxHull ();
  178. memset (sv_areanodes, 0, sizeof(sv_areanodes));
  179. sv_numareanodes = 0;
  180. SV_CreateAreaNode (0, sv.worldmodel->mins, sv.worldmodel->maxs);
  181. }
  182. /*
  183. ===============
  184. SV_UnlinkEdict
  185. ===============
  186. */
  187. void SV_UnlinkEdict (edict_t *ent)
  188. {
  189. if (!ent->area.prev)
  190. return; // not linked in anywhere
  191. RemoveLink (&ent->area);
  192. ent->area.prev = ent->area.next = NULL;
  193. }
  194. /*
  195. ====================
  196. SV_TouchLinks
  197. ====================
  198. */
  199. void SV_TouchLinks ( edict_t *ent, areanode_t *node )
  200. {
  201. link_t *l, *next;
  202. edict_t *touch;
  203. int old_self, old_other;
  204. // touch linked edicts
  205. for (l = node->trigger_edicts.next ; l != &node->trigger_edicts ; l = next)
  206. {
  207. next = l->next;
  208. touch = EDICT_FROM_AREA(l);
  209. if (touch == ent)
  210. continue;
  211. if (!touch->v.touch || touch->v.solid != SOLID_TRIGGER)
  212. continue;
  213. if (ent->v.absmin[0] > touch->v.absmax[0]
  214. || ent->v.absmin[1] > touch->v.absmax[1]
  215. || ent->v.absmin[2] > touch->v.absmax[2]
  216. || ent->v.absmax[0] < touch->v.absmin[0]
  217. || ent->v.absmax[1] < touch->v.absmin[1]
  218. || ent->v.absmax[2] < touch->v.absmin[2] )
  219. continue;
  220. old_self = pr_global_struct->self;
  221. old_other = pr_global_struct->other;
  222. pr_global_struct->self = EDICT_TO_PROG(touch);
  223. pr_global_struct->other = EDICT_TO_PROG(ent);
  224. pr_global_struct->time = sv.time;
  225. PR_ExecuteProgram (touch->v.touch);
  226. pr_global_struct->self = old_self;
  227. pr_global_struct->other = old_other;
  228. }
  229. // recurse down both sides
  230. if (node->axis == -1)
  231. return;
  232. if ( ent->v.absmax[node->axis] > node->dist )
  233. SV_TouchLinks ( ent, node->children[0] );
  234. if ( ent->v.absmin[node->axis] < node->dist )
  235. SV_TouchLinks ( ent, node->children[1] );
  236. }
  237. /*
  238. ===============
  239. SV_FindTouchedLeafs
  240. ===============
  241. */
  242. void SV_FindTouchedLeafs (edict_t *ent, mnode_t *node)
  243. {
  244. mplane_t *splitplane;
  245. mleaf_t *leaf;
  246. int sides;
  247. int leafnum;
  248. if (node->contents == CONTENTS_SOLID)
  249. return;
  250. // add an efrag if the node is a leaf
  251. if ( node->contents < 0)
  252. {
  253. if (ent->num_leafs == MAX_ENT_LEAFS)
  254. return;
  255. leaf = (mleaf_t *)node;
  256. leafnum = leaf - sv.worldmodel->leafs - 1;
  257. ent->leafnums[ent->num_leafs] = leafnum;
  258. ent->num_leafs++;
  259. return;
  260. }
  261. // NODE_MIXED
  262. splitplane = node->plane;
  263. sides = BOX_ON_PLANE_SIDE(ent->v.absmin, ent->v.absmax, splitplane);
  264. // recurse down the contacted sides
  265. if (sides & 1)
  266. SV_FindTouchedLeafs (ent, node->children[0]);
  267. if (sides & 2)
  268. SV_FindTouchedLeafs (ent, node->children[1]);
  269. }
  270. /*
  271. ===============
  272. SV_LinkEdict
  273. ===============
  274. */
  275. void SV_LinkEdict (edict_t *ent, qboolean touch_triggers)
  276. {
  277. areanode_t *node;
  278. if (ent->area.prev)
  279. SV_UnlinkEdict (ent); // unlink from old position
  280. if (ent == sv.edicts)
  281. return; // don't add the world
  282. if (ent->free)
  283. return;
  284. // set the abs box
  285. VectorAdd (ent->v.origin, ent->v.mins, ent->v.absmin);
  286. VectorAdd (ent->v.origin, ent->v.maxs, ent->v.absmax);
  287. //
  288. // to make items easier to pick up and allow them to be grabbed off
  289. // of shelves, the abs sizes are expanded
  290. //
  291. if ((int)ent->v.flags & FL_ITEM)
  292. {
  293. ent->v.absmin[0] -= 15;
  294. ent->v.absmin[1] -= 15;
  295. ent->v.absmax[0] += 15;
  296. ent->v.absmax[1] += 15;
  297. }
  298. else
  299. { // because movement is clipped an epsilon away from an actual edge,
  300. // we must fully check even when bounding boxes don't quite touch
  301. ent->v.absmin[0] -= 1;
  302. ent->v.absmin[1] -= 1;
  303. ent->v.absmin[2] -= 1;
  304. ent->v.absmax[0] += 1;
  305. ent->v.absmax[1] += 1;
  306. ent->v.absmax[2] += 1;
  307. }
  308. // link to PVS leafs
  309. ent->num_leafs = 0;
  310. if (ent->v.modelindex)
  311. SV_FindTouchedLeafs (ent, sv.worldmodel->nodes);
  312. if (ent->v.solid == SOLID_NOT)
  313. return;
  314. // find the first node that the ent's box crosses
  315. node = sv_areanodes;
  316. while (1)
  317. {
  318. if (node->axis == -1)
  319. break;
  320. if (ent->v.absmin[node->axis] > node->dist)
  321. node = node->children[0];
  322. else if (ent->v.absmax[node->axis] < node->dist)
  323. node = node->children[1];
  324. else
  325. break; // crosses the node
  326. }
  327. // link it in
  328. if (ent->v.solid == SOLID_TRIGGER)
  329. InsertLinkBefore (&ent->area, &node->trigger_edicts);
  330. else
  331. InsertLinkBefore (&ent->area, &node->solid_edicts);
  332. // if touch_triggers, touch all entities at this node and decend for more
  333. if (touch_triggers)
  334. SV_TouchLinks ( ent, sv_areanodes );
  335. }
  336. /*
  337. ===============================================================================
  338. POINT TESTING IN HULLS
  339. ===============================================================================
  340. */
  341. #if !id386
  342. /*
  343. ==================
  344. SV_HullPointContents
  345. ==================
  346. */
  347. int SV_HullPointContents (hull_t *hull, int num, vec3_t p)
  348. {
  349. float d;
  350. dclipnode_t *node;
  351. mplane_t *plane;
  352. while (num >= 0)
  353. {
  354. if (num < hull->firstclipnode || num > hull->lastclipnode)
  355. SV_Error ("SV_HullPointContents: bad node number");
  356. node = hull->clipnodes + num;
  357. plane = hull->planes + node->planenum;
  358. if (plane->type < 3)
  359. d = p[plane->type] - plane->dist;
  360. else
  361. d = DotProduct (plane->normal, p) - plane->dist;
  362. if (d < 0)
  363. num = node->children[1];
  364. else
  365. num = node->children[0];
  366. }
  367. return num;
  368. }
  369. #endif // !id386
  370. /*
  371. ==================
  372. SV_PointContents
  373. ==================
  374. */
  375. int SV_PointContents (vec3_t p)
  376. {
  377. return SV_HullPointContents (&sv.worldmodel->hulls[0], 0, p);
  378. }
  379. //===========================================================================
  380. /*
  381. ============
  382. SV_TestEntityPosition
  383. A small wrapper around SV_BoxInSolidEntity that never clips against the
  384. supplied entity.
  385. ============
  386. */
  387. edict_t *SV_TestEntityPosition (edict_t *ent)
  388. {
  389. trace_t trace;
  390. trace = SV_Move (ent->v.origin, ent->v.mins, ent->v.maxs, ent->v.origin, 0, ent);
  391. if (trace.startsolid)
  392. return sv.edicts;
  393. return NULL;
  394. }
  395. /*
  396. ===============================================================================
  397. LINE TESTING IN HULLS
  398. ===============================================================================
  399. */
  400. // 1/32 epsilon to keep floating point happy
  401. #define DIST_EPSILON (0.03125)
  402. /*
  403. ==================
  404. SV_RecursiveHullCheck
  405. ==================
  406. */
  407. qboolean SV_RecursiveHullCheck (hull_t *hull, int num, float p1f, float p2f, vec3_t p1, vec3_t p2, trace_t *trace)
  408. {
  409. dclipnode_t *node;
  410. mplane_t *plane;
  411. float t1, t2;
  412. float frac;
  413. int i;
  414. vec3_t mid;
  415. int side;
  416. float midf;
  417. // check for empty
  418. if (num < 0)
  419. {
  420. if (num != CONTENTS_SOLID)
  421. {
  422. trace->allsolid = false;
  423. if (num == CONTENTS_EMPTY)
  424. trace->inopen = true;
  425. else
  426. trace->inwater = true;
  427. }
  428. else
  429. trace->startsolid = true;
  430. return true; // empty
  431. }
  432. if (num < hull->firstclipnode || num > hull->lastclipnode)
  433. SV_Error ("SV_RecursiveHullCheck: bad node number");
  434. //
  435. // find the point distances
  436. //
  437. node = hull->clipnodes + num;
  438. plane = hull->planes + node->planenum;
  439. if (plane->type < 3)
  440. {
  441. t1 = p1[plane->type] - plane->dist;
  442. t2 = p2[plane->type] - plane->dist;
  443. }
  444. else
  445. {
  446. t1 = DotProduct (plane->normal, p1) - plane->dist;
  447. t2 = DotProduct (plane->normal, p2) - plane->dist;
  448. }
  449. #if 1
  450. if (t1 >= 0 && t2 >= 0)
  451. return SV_RecursiveHullCheck (hull, node->children[0], p1f, p2f, p1, p2, trace);
  452. if (t1 < 0 && t2 < 0)
  453. return SV_RecursiveHullCheck (hull, node->children[1], p1f, p2f, p1, p2, trace);
  454. #else
  455. if ( (t1 >= DIST_EPSILON && t2 >= DIST_EPSILON) || (t2 > t1 && t1 >= 0) )
  456. return SV_RecursiveHullCheck (hull, node->children[0], p1f, p2f, p1, p2, trace);
  457. if ( (t1 <= -DIST_EPSILON && t2 <= -DIST_EPSILON) || (t2 < t1 && t1 <= 0) )
  458. return SV_RecursiveHullCheck (hull, node->children[1], p1f, p2f, p1, p2, trace);
  459. #endif
  460. // put the crosspoint DIST_EPSILON pixels on the near side
  461. if (t1 < 0)
  462. frac = (t1 + DIST_EPSILON)/(t1-t2);
  463. else
  464. frac = (t1 - DIST_EPSILON)/(t1-t2);
  465. if (frac < 0)
  466. frac = 0;
  467. if (frac > 1)
  468. frac = 1;
  469. midf = p1f + (p2f - p1f)*frac;
  470. for (i=0 ; i<3 ; i++)
  471. mid[i] = p1[i] + frac*(p2[i] - p1[i]);
  472. side = (t1 < 0);
  473. // move up to the node
  474. if (!SV_RecursiveHullCheck (hull, node->children[side], p1f, midf, p1, mid, trace) )
  475. return false;
  476. #ifdef PARANOID
  477. if (SV_HullPointContents (sv_hullmodel, mid, node->children[side])
  478. == CONTENTS_SOLID)
  479. {
  480. Con_Printf ("mid PointInHullSolid\n");
  481. return false;
  482. }
  483. #endif
  484. if (SV_HullPointContents (hull, node->children[side^1], mid)
  485. != CONTENTS_SOLID)
  486. // go past the node
  487. return SV_RecursiveHullCheck (hull, node->children[side^1], midf, p2f, mid, p2, trace);
  488. if (trace->allsolid)
  489. return false; // never got out of the solid area
  490. //==================
  491. // the other side of the node is solid, this is the impact point
  492. //==================
  493. if (!side)
  494. {
  495. VectorCopy (plane->normal, trace->plane.normal);
  496. trace->plane.dist = plane->dist;
  497. }
  498. else
  499. {
  500. VectorSubtract (vec3_origin, plane->normal, trace->plane.normal);
  501. trace->plane.dist = -plane->dist;
  502. }
  503. while (SV_HullPointContents (hull, hull->firstclipnode, mid)
  504. == CONTENTS_SOLID)
  505. { // shouldn't really happen, but does occasionally
  506. frac -= 0.1;
  507. if (frac < 0)
  508. {
  509. trace->fraction = midf;
  510. VectorCopy (mid, trace->endpos);
  511. Con_Printf ("backup past 0\n");
  512. return false;
  513. }
  514. midf = p1f + (p2f - p1f)*frac;
  515. for (i=0 ; i<3 ; i++)
  516. mid[i] = p1[i] + frac*(p2[i] - p1[i]);
  517. }
  518. trace->fraction = midf;
  519. VectorCopy (mid, trace->endpos);
  520. return false;
  521. }
  522. /*
  523. ==================
  524. SV_ClipMoveToEntity
  525. Handles selection or creation of a clipping hull, and offseting (and
  526. eventually rotation) of the end points
  527. ==================
  528. */
  529. trace_t SV_ClipMoveToEntity (edict_t *ent, vec3_t start, vec3_t mins, vec3_t maxs, vec3_t end)
  530. {
  531. trace_t trace;
  532. vec3_t offset;
  533. vec3_t start_l, end_l;
  534. hull_t *hull;
  535. // fill in a default trace
  536. memset (&trace, 0, sizeof(trace_t));
  537. trace.fraction = 1;
  538. trace.allsolid = true;
  539. VectorCopy (end, trace.endpos);
  540. // get the clipping hull
  541. hull = SV_HullForEntity (ent, mins, maxs, offset);
  542. VectorSubtract (start, offset, start_l);
  543. VectorSubtract (end, offset, end_l);
  544. // trace a line through the apropriate clipping hull
  545. SV_RecursiveHullCheck (hull, hull->firstclipnode, 0, 1, start_l, end_l, &trace);
  546. // fix trace up by the offset
  547. if (trace.fraction != 1)
  548. VectorAdd (trace.endpos, offset, trace.endpos);
  549. // did we clip the move?
  550. if (trace.fraction < 1 || trace.startsolid )
  551. trace.ent = ent;
  552. return trace;
  553. }
  554. //===========================================================================
  555. /*
  556. ====================
  557. SV_ClipToLinks
  558. Mins and maxs enclose the entire area swept by the move
  559. ====================
  560. */
  561. void SV_ClipToLinks ( areanode_t *node, moveclip_t *clip )
  562. {
  563. link_t *l, *next;
  564. edict_t *touch;
  565. trace_t trace;
  566. // touch linked edicts
  567. for (l = node->solid_edicts.next ; l != &node->solid_edicts ; l = next)
  568. {
  569. next = l->next;
  570. touch = EDICT_FROM_AREA(l);
  571. if (touch->v.solid == SOLID_NOT)
  572. continue;
  573. if (touch == clip->passedict)
  574. continue;
  575. if (touch->v.solid == SOLID_TRIGGER)
  576. SV_Error ("Trigger in clipping list");
  577. if (clip->type == MOVE_NOMONSTERS && touch->v.solid != SOLID_BSP)
  578. continue;
  579. if (clip->boxmins[0] > touch->v.absmax[0]
  580. || clip->boxmins[1] > touch->v.absmax[1]
  581. || clip->boxmins[2] > touch->v.absmax[2]
  582. || clip->boxmaxs[0] < touch->v.absmin[0]
  583. || clip->boxmaxs[1] < touch->v.absmin[1]
  584. || clip->boxmaxs[2] < touch->v.absmin[2] )
  585. continue;
  586. if (clip->passedict && clip->passedict->v.size[0] && !touch->v.size[0])
  587. continue; // points never interact
  588. // might intersect, so do an exact clip
  589. if (clip->trace.allsolid)
  590. return;
  591. if (clip->passedict)
  592. {
  593. if (PROG_TO_EDICT(touch->v.owner) == clip->passedict)
  594. continue; // don't clip against own missiles
  595. if (PROG_TO_EDICT(clip->passedict->v.owner) == touch)
  596. continue; // don't clip against owner
  597. }
  598. if ((int)touch->v.flags & FL_MONSTER)
  599. trace = SV_ClipMoveToEntity (touch, clip->start, clip->mins2, clip->maxs2, clip->end);
  600. else
  601. trace = SV_ClipMoveToEntity (touch, clip->start, clip->mins, clip->maxs, clip->end);
  602. if (trace.allsolid || trace.startsolid ||
  603. trace.fraction < clip->trace.fraction)
  604. {
  605. trace.ent = touch;
  606. if (clip->trace.startsolid)
  607. {
  608. clip->trace = trace;
  609. clip->trace.startsolid = true;
  610. }
  611. else
  612. clip->trace = trace;
  613. }
  614. else if (trace.startsolid)
  615. clip->trace.startsolid = true;
  616. }
  617. // recurse down both sides
  618. if (node->axis == -1)
  619. return;
  620. if ( clip->boxmaxs[node->axis] > node->dist )
  621. SV_ClipToLinks ( node->children[0], clip );
  622. if ( clip->boxmins[node->axis] < node->dist )
  623. SV_ClipToLinks ( node->children[1], clip );
  624. }
  625. /*
  626. ==================
  627. SV_MoveBounds
  628. ==================
  629. */
  630. void SV_MoveBounds (vec3_t start, vec3_t mins, vec3_t maxs, vec3_t end, vec3_t boxmins, vec3_t boxmaxs)
  631. {
  632. #if 0
  633. // debug to test against everything
  634. boxmins[0] = boxmins[1] = boxmins[2] = -9999;
  635. boxmaxs[0] = boxmaxs[1] = boxmaxs[2] = 9999;
  636. #else
  637. int i;
  638. for (i=0 ; i<3 ; i++)
  639. {
  640. if (end[i] > start[i])
  641. {
  642. boxmins[i] = start[i] + mins[i] - 1;
  643. boxmaxs[i] = end[i] + maxs[i] + 1;
  644. }
  645. else
  646. {
  647. boxmins[i] = end[i] + mins[i] - 1;
  648. boxmaxs[i] = start[i] + maxs[i] + 1;
  649. }
  650. }
  651. #endif
  652. }
  653. /*
  654. ==================
  655. SV_Move
  656. ==================
  657. */
  658. trace_t SV_Move (vec3_t start, vec3_t mins, vec3_t maxs, vec3_t end, int type, edict_t *passedict)
  659. {
  660. moveclip_t clip;
  661. int i;
  662. memset ( &clip, 0, sizeof ( moveclip_t ) );
  663. // clip to world
  664. clip.trace = SV_ClipMoveToEntity ( sv.edicts, start, mins, maxs, end );
  665. clip.start = start;
  666. clip.end = end;
  667. clip.mins = mins;
  668. clip.maxs = maxs;
  669. clip.type = type;
  670. clip.passedict = passedict;
  671. if (type == MOVE_MISSILE)
  672. {
  673. for (i=0 ; i<3 ; i++)
  674. {
  675. clip.mins2[i] = -15;
  676. clip.maxs2[i] = 15;
  677. }
  678. }
  679. else
  680. {
  681. VectorCopy (mins, clip.mins2);
  682. VectorCopy (maxs, clip.maxs2);
  683. }
  684. // create the bounding box of the entire move
  685. SV_MoveBounds ( start, clip.mins2, clip.maxs2, end, clip.boxmins, clip.boxmaxs );
  686. // clip to entities
  687. SV_ClipToLinks ( sv_areanodes, &clip );
  688. return clip.trace;
  689. }
  690. //=============================================================================
  691. /*
  692. ============
  693. SV_TestPlayerPosition
  694. ============
  695. */
  696. edict_t *SV_TestPlayerPosition (edict_t *ent, vec3_t origin)
  697. {
  698. hull_t *hull;
  699. edict_t *check;
  700. vec3_t boxmins, boxmaxs;
  701. vec3_t offset;
  702. int e;
  703. // check world first
  704. hull = &sv.worldmodel->hulls[1];
  705. if ( SV_HullPointContents (hull, hull->firstclipnode, origin) != CONTENTS_EMPTY )
  706. return sv.edicts;
  707. // check all entities
  708. VectorAdd (origin, ent->v.mins, boxmins);
  709. VectorAdd (origin, ent->v.maxs, boxmaxs);
  710. check = NEXT_EDICT(sv.edicts);
  711. for (e=1 ; e<sv.num_edicts ; e++, check = NEXT_EDICT(check))
  712. {
  713. if (check->free)
  714. continue;
  715. if (check->v.solid != SOLID_BSP &&
  716. check->v.solid != SOLID_BBOX &&
  717. check->v.solid != SOLID_SLIDEBOX)
  718. continue;
  719. if (boxmins[0] > check->v.absmax[0]
  720. || boxmins[1] > check->v.absmax[1]
  721. || boxmins[2] > check->v.absmax[2]
  722. || boxmaxs[0] < check->v.absmin[0]
  723. || boxmaxs[1] < check->v.absmin[1]
  724. || boxmaxs[2] < check->v.absmin[2] )
  725. continue;
  726. if (check == ent)
  727. continue;
  728. // get the clipping hull
  729. hull = SV_HullForEntity (check, ent->v.mins, ent->v.maxs, offset);
  730. VectorSubtract (origin, offset, offset);
  731. // test the point
  732. if ( SV_HullPointContents (hull, hull->firstclipnode, offset) != CONTENTS_EMPTY )
  733. return check;
  734. }
  735. return NULL;
  736. }