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