]> icculus.org git repositories - divverent/darkplaces.git/blob - world.c
remove unused DrawNotifyString, fix intermission screen so only the finale-style...
[divverent/darkplaces.git] / world.c
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 */
20 // world.c -- world query functions
21
22 #include "quakedef.h"
23
24 /*
25
26 entities never clip against themselves, or their owner
27
28 line of sight checks trace->crosscontent, but bullets don't
29
30 */
31
32 /*
33 typedef struct link_s
34 {
35         struct link_s   *prev, *next;
36 } link_t;
37 */
38
39
40 void ClearLink (link_t *l);
41 void RemoveLink (link_t *l);
42 void InsertLinkBefore (link_t *l, link_t *before);
43 void InsertLinkAfter (link_t *l, link_t *after);
44
45 // (type *)STRUCT_FROM_LINK(link_t *link, type, member)
46 // ent = STRUCT_FROM_LINK(link,entity_t,order)
47 // FIXME: remove this mess!
48 //#define       STRUCT_FROM_LINK(l,t,m) ((t *)((qbyte *)l - (int)&(((t *)0)->m)))
49
50 #define EDICT_FROM_AREA(l) ((edict_t *)((qbyte *)l - (int)&(((edict_t *)0)->area)))
51
52 //============================================================================
53
54 // ClearLink is used for new headnodes
55 void ClearLink (link_t *l)
56 {
57         l->prev = l->next = l;
58 }
59
60 void RemoveLink (link_t *l)
61 {
62         l->next->prev = l->prev;
63         l->prev->next = l->next;
64 }
65
66 void InsertLinkBefore (link_t *l, link_t *before)
67 {
68         l->next = before;
69         l->prev = before->prev;
70         l->prev->next = l;
71         l->next->prev = l;
72 }
73 void InsertLinkAfter (link_t *l, link_t *after)
74 {
75         l->next = after->next;
76         l->prev = after;
77         l->prev->next = l;
78         l->next->prev = l;
79 }
80
81
82 typedef struct
83 {
84         // bounding box of entire move area
85         vec3_t          boxmins, boxmaxs;
86
87         // size of the moving object
88         vec3_t          mins, maxs;
89
90         // size when clipping against monsters
91         vec3_t          mins2, maxs2;
92
93         // size when clipping against brush models
94         vec3_t          hullmins, hullmaxs;
95
96         // start and end origin of move
97         vec3_t          start, end;
98
99         // trace results
100         trace_t         trace;
101
102         // type of move (like ignoring monsters, or similar)
103         int                     type;
104
105         // the edict that is moving (if any)
106         edict_t         *passedict;
107 }
108 moveclip_t;
109
110
111 /*
112 ===============================================================================
113
114 ENTITY AREA CHECKING
115
116 ===============================================================================
117 */
118
119 typedef struct areanode_s
120 {
121         int             axis;           // -1 = leaf node
122         float   dist;
123         struct areanode_s       *children[2];
124         link_t  trigger_edicts;
125         link_t  solid_edicts;
126 } areanode_t;
127
128 #define AREA_DEPTH      4
129 #define AREA_NODES      32
130
131 static  areanode_t      sv_areanodes[AREA_NODES];
132 static  int                     sv_numareanodes;
133
134 /*
135 ===============
136 SV_CreateAreaNode
137
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
146         anode = &sv_areanodes[sv_numareanodes];
147         sv_numareanodes++;
148
149         ClearLink (&anode->trigger_edicts);
150         ClearLink (&anode->solid_edicts);
151
152         if (depth == AREA_DEPTH)
153         {
154                 anode->axis = -1;
155                 anode->children[0] = anode->children[1] = NULL;
156                 return anode;
157         }
158
159         VectorSubtract (maxs, mins, size);
160         if (size[0] > size[1])
161                 anode->axis = 0;
162         else
163                 anode->axis = 1;
164
165         anode->dist = 0.5 * (maxs[anode->axis] + mins[anode->axis]);
166         VectorCopy (mins, mins1);
167         VectorCopy (mins, mins2);
168         VectorCopy (maxs, maxs1);
169         VectorCopy (maxs, maxs2);
170
171         maxs1[anode->axis] = mins2[anode->axis] = anode->dist;
172
173         anode->children[0] = SV_CreateAreaNode (depth+1, mins2, maxs2);
174         anode->children[1] = SV_CreateAreaNode (depth+1, mins1, maxs1);
175
176         return anode;
177 }
178
179 /*
180 ===============
181 SV_ClearWorld
182
183 ===============
184 */
185 void SV_ClearWorld (void)
186 {
187         Collision_Init ();
188
189         memset (sv_areanodes, 0, sizeof(sv_areanodes));
190         sv_numareanodes = 0;
191         Mod_CheckLoaded(sv.worldmodel);
192         SV_CreateAreaNode (0, sv.worldmodel->normalmins, sv.worldmodel->normalmaxs);
193 }
194
195
196 /*
197 ===============
198 SV_UnlinkEdict
199
200 ===============
201 */
202 void SV_UnlinkEdict (edict_t *ent)
203 {
204         if (!ent->area.prev)
205                 return;         // not linked in anywhere
206         RemoveLink (&ent->area);
207         ent->area.prev = ent->area.next = NULL;
208 }
209
210
211 /*
212 ====================
213 SV_TouchLinks
214 ====================
215 */
216 void SV_TouchLinks ( edict_t *ent, areanode_t *node )
217 {
218         link_t          *l, *next;
219         edict_t         *touch;
220         int                     old_self, old_other;
221
222 loc0:
223 // touch linked edicts
224         for (l = node->trigger_edicts.next ; l != &node->trigger_edicts ; l = next)
225         {
226                 next = l->next;
227                 touch = EDICT_FROM_AREA(l);
228                 if (touch == ent)
229                         continue;
230                 if (!touch->v.touch || touch->v.solid != SOLID_TRIGGER)
231                         continue;
232                 if (ent->v.absmin[0] > touch->v.absmax[0]
233                  || ent->v.absmin[1] > touch->v.absmax[1]
234                  || ent->v.absmin[2] > touch->v.absmax[2]
235                  || ent->v.absmax[0] < touch->v.absmin[0]
236                  || ent->v.absmax[1] < touch->v.absmin[1]
237                  || ent->v.absmax[2] < touch->v.absmin[2])
238                         continue;
239                 old_self = pr_global_struct->self;
240                 old_other = pr_global_struct->other;
241
242                 pr_global_struct->self = EDICT_TO_PROG(touch);
243                 pr_global_struct->other = EDICT_TO_PROG(ent);
244                 pr_global_struct->time = sv.time;
245                 PR_ExecuteProgram (touch->v.touch, "");
246
247                 pr_global_struct->self = old_self;
248                 pr_global_struct->other = old_other;
249         }
250
251 // recurse down both sides
252         if (node->axis == -1)
253                 return;
254
255         // LordHavoc: optimized recursion
256 //      if (ent->v.absmax[node->axis] > node->dist) SV_TouchLinks (ent, node->children[0]);
257 //      if (ent->v.absmin[node->axis] < node->dist) SV_TouchLinks (ent, node->children[1]);
258         if (ent->v.absmax[node->axis] > node->dist)
259         {
260                 if (ent->v.absmin[node->axis] < node->dist)
261                         SV_TouchLinks(ent, node->children[1]); // order reversed to reduce code
262                 node = node->children[0];
263                 goto loc0;
264         }
265         else
266         {
267                 if (ent->v.absmin[node->axis] < node->dist)
268                 {
269                         node = node->children[1];
270                         goto loc0;
271                 }
272         }
273 }
274
275
276 /*
277 ===============
278 SV_LinkEdict
279
280 ===============
281 */
282 void SV_LinkEdict (edict_t *ent, qboolean touch_triggers)
283 {
284         model_t         *model;
285         areanode_t      *node;
286
287         if (ent->area.prev)
288                 SV_UnlinkEdict (ent);   // unlink from old position
289
290         if (ent == sv.edicts)
291                 return;         // don't add the world
292
293         if (ent->free)
294                 return;
295
296 // set the abs box
297
298         /*
299 // LordHavoc: enabling rotating bmodels
300         if (ent->v.solid == SOLID_BSP && (ent->v.angles[0] || ent->v.angles[1] || ent->v.angles[2]))
301         {
302                 // expand for rotation
303                 float           max, v;
304                 int                     i;
305
306                 max = DotProduct(ent->v.mins, ent->v.mins);
307                 v = DotProduct(ent->v.maxs, ent->v.maxs);
308                 if (max < v)
309                         max = v;
310                 max = sqrt(max);
311         */
312                 /*
313                 max = 0;
314                 for (i=0 ; i<3 ; i++)
315                 {
316                         v = fabs(ent->v.mins[i]);
317                         if (max < v)
318                                 max = v;
319                         v = fabs(ent->v.maxs[i]);
320                         if (max < v)
321                                 max = v;
322                 }
323                 */
324         /*
325                 for (i=0 ; i<3 ; i++)
326                 {
327                         ent->v.absmin[i] = ent->v.origin[i] - max;
328                         ent->v.absmax[i] = ent->v.origin[i] + max;
329                 }
330         }
331         else
332         {
333                 VectorAdd (ent->v.origin, ent->v.mins, ent->v.absmin);
334                 VectorAdd (ent->v.origin, ent->v.maxs, ent->v.absmax);
335         }
336         */
337
338         if (ent->v.solid == SOLID_BSP)
339         {
340                 if (ent->v.modelindex < 0 || ent->v.modelindex > MAX_MODELS)
341                         PR_RunError("SOLID_BSP with invalid modelindex!\n");
342                 model = sv.models[(int) ent->v.modelindex];
343                 if (model != NULL)
344                 {
345                         if (model->type != mod_brush)
346                                 PR_RunError("SOLID_BSP with non-BSP model\n");
347
348                         if (ent->v.angles[0] || ent->v.angles[2] || ent->v.avelocity[0] || ent->v.avelocity[2])
349                         {
350                                 VectorAdd(ent->v.origin, model->rotatedmins, ent->v.absmin);
351                                 VectorAdd(ent->v.origin, model->rotatedmaxs, ent->v.absmax);
352                         }
353                         else if (ent->v.angles[1] || ent->v.avelocity[1])
354                         {
355                                 VectorAdd(ent->v.origin, model->yawmins, ent->v.absmin);
356                                 VectorAdd(ent->v.origin, model->yawmaxs, ent->v.absmax);
357                         }
358                         else
359                         {
360                                 VectorAdd(ent->v.origin, model->normalmins, ent->v.absmin);
361                                 VectorAdd(ent->v.origin, model->normalmaxs, ent->v.absmax);
362                         }
363                 }
364                 else
365                 {
366                         // SOLID_BSP with no model is valid, mainly because some QC setup code does so temporarily
367                         VectorAdd (ent->v.origin, ent->v.mins, ent->v.absmin);
368                         VectorAdd (ent->v.origin, ent->v.maxs, ent->v.absmax);
369                 }
370         }
371         else
372         {
373                 VectorAdd (ent->v.origin, ent->v.mins, ent->v.absmin);
374                 VectorAdd (ent->v.origin, ent->v.maxs, ent->v.absmax);
375         }
376
377 //
378 // to make items easier to pick up and allow them to be grabbed off
379 // of shelves, the abs sizes are expanded
380 //
381         if ((int)ent->v.flags & FL_ITEM)
382         {
383                 ent->v.absmin[0] -= 15;
384                 ent->v.absmin[1] -= 15;
385                 ent->v.absmin[2] -= 1;
386                 ent->v.absmax[0] += 15;
387                 ent->v.absmax[1] += 15;
388                 ent->v.absmax[2] += 1;
389         }
390         else
391         {
392                 // because movement is clipped an epsilon away from an actual edge,
393                 // we must fully check even when bounding boxes don't quite touch
394                 ent->v.absmin[0] -= 1;
395                 ent->v.absmin[1] -= 1;
396                 ent->v.absmin[2] -= 1;
397                 ent->v.absmax[0] += 1;
398                 ent->v.absmax[1] += 1;
399                 ent->v.absmax[2] += 1;
400         }
401
402         if (ent->v.solid == SOLID_NOT)
403                 return;
404
405 // find the first node that the ent's box crosses
406         node = sv_areanodes;
407         while (1)
408         {
409                 if (node->axis == -1)
410                         break;
411                 if (ent->v.absmin[node->axis] > node->dist)
412                         node = node->children[0];
413                 else if (ent->v.absmax[node->axis] < node->dist)
414                         node = node->children[1];
415                 else
416                         break;          // crosses the node
417         }
418
419 // link it in
420
421         if (ent->v.solid == SOLID_TRIGGER)
422                 InsertLinkBefore (&ent->area, &node->trigger_edicts);
423         else
424                 InsertLinkBefore (&ent->area, &node->solid_edicts);
425
426 // if touch_triggers, touch all entities at this node and descend for more
427         if (touch_triggers)
428                 SV_TouchLinks ( ent, sv_areanodes );
429 }
430
431
432
433 /*
434 ===============================================================================
435
436 POINT TESTING IN HULLS
437
438 ===============================================================================
439 */
440
441 /*
442 ==================
443 SV_HullPointContents
444
445 ==================
446 */
447 int SV_HullPointContents (hull_t *hull, int num, vec3_t p)
448 {
449         while (num >= 0)
450                 num = hull->clipnodes[num].children[(hull->planes[hull->clipnodes[num].planenum].type < 3 ? p[hull->planes[hull->clipnodes[num].planenum].type] : DotProduct (hull->planes[hull->clipnodes[num].planenum].normal, p)) < hull->planes[hull->clipnodes[num].planenum].dist];
451
452         return num;
453 }
454
455 /*
456 ============
457 SV_TestEntityPosition
458
459 This could be a lot more efficient...
460 ============
461 */
462 edict_t *SV_TestEntityPosition (edict_t *ent)
463 {
464         trace_t trace;
465
466         trace = SV_Move (ent->v.origin, ent->v.mins, ent->v.maxs, ent->v.origin, MOVE_NORMAL, ent);
467
468         if (trace.startsolid)
469                 return sv.edicts;
470
471         return NULL;
472 }
473
474
475 /*
476 ===============================================================================
477
478 LINE TESTING IN HULLS
479
480 ===============================================================================
481 */
482
483 /*
484 ==================
485 SV_ClipMoveToEntity
486
487 Handles selection or creation of a clipping hull, and offseting (and
488 eventually rotation) of the end points
489 ==================
490 */
491 trace_t SV_ClipMoveToEntity (edict_t *ent, vec3_t start, vec3_t mins, vec3_t maxs, vec3_t end)
492 {
493         int i;
494         trace_t trace;
495         model_t *model;
496
497         i = ent->v.modelindex;
498         if ((unsigned int) i >= MAX_MODELS)
499                 PR_RunError("SV_ClipMoveToEntity: invalid modelindex\n");
500         model = sv.models[i];
501         if (i != 0 && model == NULL)
502                 PR_RunError("SV_ClipMoveToEntity: invalid modelindex\n");
503
504         if ((int) ent->v.solid == SOLID_BSP)
505         {
506                 Mod_CheckLoaded(model);
507                 if (model->type != mod_brush)
508                 {
509                         Con_Printf ("SV_ClipMoveToEntity: SOLID_BSP with a non bsp model, entity dump:\n");
510                         ED_Print (ent);
511                         Host_Error ("SV_ClipMoveToEntity: SOLID_BSP with a non bsp model\n");
512                 }
513                 if (ent->v.movetype != MOVETYPE_PUSH)
514                         Host_Error ("SV_ClipMoveToEntity: SOLID_BSP without MOVETYPE_PUSH");
515         }
516
517         Collision_ClipTrace(&trace, ent, model, ent->v.origin, ent->v.angles, ent->v.mins, ent->v.maxs, start, mins, maxs, end);
518
519         return trace;
520 }
521
522 //===========================================================================
523
524 /*
525 ====================
526 SV_ClipToLinks
527
528 Mins and maxs enclose the entire area swept by the move
529 ====================
530 */
531 void SV_ClipToLinks ( areanode_t *node, moveclip_t *clip )
532 {
533         link_t          *l, *next;
534         edict_t         *touch;
535         trace_t         trace;
536
537 loc0:
538         if (clip->trace.allsolid)
539                 return;
540 // touch linked edicts
541         for (l = node->solid_edicts.next ; l != &node->solid_edicts ; l = next)
542         {
543                 next = l->next;
544                 touch = EDICT_FROM_AREA(l);
545                 if (touch->v.solid == SOLID_NOT)
546                         continue;
547                 if (touch == clip->passedict)
548                         continue;
549                 if (touch->v.solid == SOLID_TRIGGER)
550                         Host_Error ("Trigger in clipping list");
551
552                 if (clip->type == MOVE_NOMONSTERS && touch->v.solid != SOLID_BSP)
553                         continue;
554
555                 if (clip->boxmins[0] > touch->v.absmax[0]
556                  || clip->boxmaxs[0] < touch->v.absmin[0]
557                  || clip->boxmins[1] > touch->v.absmax[1]
558                  || clip->boxmaxs[1] < touch->v.absmin[1]
559                  || clip->boxmins[2] > touch->v.absmax[2]
560                  || clip->boxmaxs[2] < touch->v.absmin[2])
561                         continue;
562
563                 if (clip->passedict)
564                 {
565                         if (clip->passedict->v.size[0] && !touch->v.size[0])
566                                 continue;       // points never interact
567                         if (PROG_TO_EDICT(touch->v.owner) == clip->passedict)
568                                 continue;       // don't clip against own missiles
569                         if (PROG_TO_EDICT(clip->passedict->v.owner) == touch)
570                                 continue;       // don't clip against owner
571                         // LordHavoc: corpse code
572                         if (clip->passedict->v.solid == SOLID_CORPSE && (touch->v.solid == SOLID_SLIDEBOX || touch->v.solid == SOLID_CORPSE))
573                                 continue;
574                         if (clip->passedict->v.solid == SOLID_SLIDEBOX && touch->v.solid == SOLID_CORPSE)
575                                 continue;
576                 }
577
578                 // might interact, so do an exact clip
579                 if ((int)touch->v.flags & FL_MONSTER)
580                         trace = SV_ClipMoveToEntity (touch, clip->start, clip->mins2, clip->maxs2, clip->end);
581                 else if (touch->v.solid == SOLID_BSP)
582                         trace = SV_ClipMoveToEntity (touch, clip->start, clip->hullmins, clip->hullmaxs, clip->end);
583                 else
584                         trace = SV_ClipMoveToEntity (touch, clip->start, clip->mins, clip->maxs, clip->end);
585                 // LordHavoc: take the 'best' answers from the new trace and combine with existing data
586                 if (trace.allsolid)
587                         clip->trace.allsolid = true;
588                 if (trace.startsolid)
589                 {
590                         clip->trace.startsolid = true;
591                         if (!clip->trace.ent)
592                                 clip->trace.ent = trace.ent;
593                 }
594                 if (trace.inopen)
595                         clip->trace.inopen = true;
596                 if (trace.inwater)
597                         clip->trace.inwater = true;
598                 if (trace.fraction < clip->trace.fraction)
599                 {
600                         clip->trace.fraction = trace.fraction;
601                         VectorCopy(trace.endpos, clip->trace.endpos);
602                         clip->trace.plane = trace.plane;
603                         clip->trace.endcontents = trace.endcontents;
604                         clip->trace.ent = trace.ent;
605                 }
606         }
607
608 // recurse down both sides
609         if (node->axis == -1)
610                 return;
611
612         // LordHavoc: optimized recursion
613 //      if (clip->boxmaxs[node->axis] > node->dist) SV_ClipToLinks(node->children[0], clip);
614 //      if (clip->boxmins[node->axis] < node->dist) SV_ClipToLinks(node->children[1], clip);
615         if (clip->boxmaxs[node->axis] > node->dist)
616         {
617                 if (clip->boxmins[node->axis] < node->dist)
618                         SV_ClipToLinks(node->children[1], clip);
619                 node = node->children[0];
620                 goto loc0;
621         }
622         else if (clip->boxmins[node->axis] < node->dist)
623         {
624                 node = node->children[1];
625                 goto loc0;
626         }
627 }
628
629
630 /*
631 ==================
632 SV_MoveBounds
633 ==================
634 */
635 void SV_MoveBounds (vec3_t start, vec3_t mins, vec3_t maxs, vec3_t end, vec3_t boxmins, vec3_t boxmaxs)
636 {
637 #if 0
638 // debug to test against everything
639 boxmins[0] = boxmins[1] = boxmins[2] = -999999999;
640 boxmaxs[0] = boxmaxs[1] = boxmaxs[2] =  999999999;
641 #else
642         int             i;
643
644         for (i=0 ; i<3 ; i++)
645         {
646                 if (end[i] > start[i])
647                 {
648                         boxmins[i] = start[i] + mins[i] - 1;
649                         boxmaxs[i] = end[i] + maxs[i] + 1;
650                 }
651                 else
652                 {
653                         boxmins[i] = end[i] + mins[i] - 1;
654                         boxmaxs[i] = start[i] + maxs[i] + 1;
655                 }
656         }
657 #endif
658 }
659
660 /*
661 ==================
662 SV_Move
663 ==================
664 */
665 trace_t SV_Move (vec3_t start, vec3_t mins, vec3_t maxs, vec3_t end, int type, edict_t *passedict)
666 {
667         moveclip_t      clip;
668         vec3_t          bigmins, bigmaxs;
669         int                     i;
670
671         memset ( &clip, 0, sizeof ( moveclip_t ) );
672
673         VectorCopy(start, clip.start);
674         VectorCopy(end, clip.end);
675         VectorCopy(mins, clip.mins);
676         VectorCopy(maxs, clip.maxs);
677         clip.type = type;
678         clip.passedict = passedict;
679
680         Collision_RoundUpToHullSize(sv.worldmodel, clip.mins, clip.maxs, clip.hullmins, clip.hullmaxs);
681
682         if (type == MOVE_MISSILE)
683         {
684                 // LordHavoc: modified this, was = -15, now = clip.mins[i] - 15
685                 for (i=0 ; i<3 ; i++)
686                 {
687                         clip.mins2[i] = clip.mins[i] - 15;
688                         clip.maxs2[i] = clip.maxs[i] + 15;
689                 }
690         }
691         else
692         {
693                 VectorCopy (clip.mins, clip.mins2);
694                 VectorCopy (clip.maxs, clip.maxs2);
695         }
696
697         bigmins[0] = min(clip.mins2[0], clip.hullmins[0]);
698         bigmaxs[0] = max(clip.maxs2[0], clip.hullmaxs[0]);
699         bigmins[1] = min(clip.mins2[1], clip.hullmins[1]);
700         bigmaxs[1] = max(clip.maxs2[1], clip.hullmaxs[1]);
701         bigmins[2] = min(clip.mins2[2], clip.hullmins[2]);
702         bigmaxs[2] = max(clip.maxs2[2], clip.hullmaxs[2]);
703
704         // clip to world
705         clip.trace = SV_ClipMoveToEntity (sv.edicts, start, mins, maxs, end);
706
707         // clip to entities
708         // create the bounding box of the entire move
709         SV_MoveBounds ( start, bigmins, bigmaxs, end, clip.boxmins, clip.boxmaxs );
710
711         SV_ClipToLinks ( sv_areanodes, &clip );
712
713         return clip.trace;
714 }