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