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