]> icculus.org git repositories - btb/d2x.git/blob - main/physics.c
more header fixes
[btb/d2x.git] / main / physics.c
1 /*
2 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
3 SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
4 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
5 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
6 IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
7 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
8 FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
9 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
10 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
11 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
12 */
13
14 /*
15  *
16  * Code for flying through the mines
17  *
18  */
19
20
21 #ifdef HAVE_CONFIG_H
22 #include <conf.h>
23 #endif
24
25 #include <stdio.h>
26 #include <stdlib.h>
27
28 #include "joy.h"
29 #include "mono.h"
30 #include "error.h"
31 #include "inferno.h"
32 #include "key.h"
33 #include "timer.h"
34
35 #ifdef TACTILE
36 #include "tactile.h"
37 #endif
38
39
40 //Global variables for physics system
41
42 #define ROLL_RATE               0x2000
43 #define DAMP_ANG                        0x400                  //min angle to bank
44
45 #define TURNROLL_SCALE  (0x4ec4/2)
46
47 #define MAX_OBJECT_VEL i2f(100)
48
49 #define BUMP_HACK       1               //if defined, bump player when he gets stuck
50
51 //--unused-- int mike_mode=0;
52
53 //check point against each side of segment. return bitmask, where bit
54 //set means behind that side
55
56 int Physics_cheat_flag = 0;
57 extern char BounceCheat;
58
59 //##//returns the distance of a point (checkp) from a plane (defined by norm & planep)
60 //##fix dist_to_plane(vms_vector *checkp,vms_vector *norm,vms_vector *planep)
61 //##{
62 //##    vms_vector deltap;
63 //##
64 //##    vm_vec_sub(&deltap,checkp,planep);
65 //##
66 //##    return vm_vec_dot(&deltap,norm);
67 //##}
68
69 //--unused-- int dpjm_old_joy_x, dpjm_old_joy_y;
70
71 int floor_levelling=0;
72
73 //--unused-- level_with_floor()
74 //--unused-- {
75 //--unused--    floor_levelling=1;
76 //--unused-- }
77
78 //make sure matrix is orthogonal
79 void check_and_fix_matrix(vms_matrix *m)
80 {
81         vms_matrix tempm;
82
83         vm_vector_2_matrix(&tempm,&m->fvec,&m->uvec,NULL);
84         *m  = tempm;
85 }
86
87
88 void do_physics_align_object( object * obj )
89 {
90         vms_vector desired_upvec;
91         fixang delta_ang,roll_ang;
92         //vms_vector forvec = {0,0,f1_0};
93         vms_matrix temp_matrix;
94         fix d,largest_d=-f1_0;
95         int i,best_side;
96
97         best_side=0;
98         // bank player according to segment orientation
99
100         //find side of segment that player is most alligned with
101
102         for (i=0;i<6;i++) {
103                 #ifdef COMPACT_SEGS
104                         vms_vector _tv1;
105                         get_side_normal( &Segments[obj->segnum], i, 0, &_tv1 );
106                         d = vm_vec_dot(&_tv1,&obj->orient.uvec);
107                 #else                                   
108                         d = vm_vec_dot(&Segments[obj->segnum].sides[i].normals[0],&obj->orient.uvec);
109                 #endif
110
111                 if (d > largest_d) {largest_d = d; best_side=i;}
112         }
113
114         if (floor_levelling) {
115
116                 // old way: used floor's normal as upvec
117                 #ifdef COMPACT_SEGS
118                         get_side_normal(&Segments[obj->segnum], 3, 0, &desired_upvec );                 
119                 #else
120                         desired_upvec = Segments[obj->segnum].sides[3].normals[0];
121                 #endif
122
123         }
124         else  // new player leveling code: use normal of side closest to our up vec
125                 if (get_num_faces(&Segments[obj->segnum].sides[best_side])==2) {
126                         #ifdef COMPACT_SEGS
127                                 vms_vector normals[2];
128                                 get_side_normals(&Segments[obj->segnum], best_side, &normals[0], &normals[1] );                 
129
130                                 desired_upvec.x = (normals[0].x + normals[1].x) / 2;
131                                 desired_upvec.y = (normals[0].y + normals[1].y) / 2;
132                                 desired_upvec.z = (normals[0].z + normals[1].z) / 2;
133
134                                 vm_vec_normalize(&desired_upvec);
135                         #else
136                                 side *s = &Segments[obj->segnum].sides[best_side];
137                                 desired_upvec.x = (s->normals[0].x + s->normals[1].x) / 2;
138                                 desired_upvec.y = (s->normals[0].y + s->normals[1].y) / 2;
139                                 desired_upvec.z = (s->normals[0].z + s->normals[1].z) / 2;
140                 
141                                 vm_vec_normalize(&desired_upvec);
142                         #endif
143                 }
144                 else
145                         #ifdef COMPACT_SEGS
146                                 get_side_normal(&Segments[obj->segnum], best_side, 0, &desired_upvec );                 
147                         #else
148                                 desired_upvec = Segments[obj->segnum].sides[best_side].normals[0];
149                         #endif
150
151         if (labs(vm_vec_dot(&desired_upvec,&obj->orient.fvec)) < f1_0/2) {
152                 fixang save_delta_ang;
153                 vms_angvec tangles;
154                 
155                 vm_vector_2_matrix(&temp_matrix,&obj->orient.fvec,&desired_upvec,NULL);
156
157                 save_delta_ang = delta_ang = vm_vec_delta_ang(&obj->orient.uvec,&temp_matrix.uvec,&obj->orient.fvec);
158
159                 delta_ang += obj->mtype.phys_info.turnroll;
160
161                 if (abs(delta_ang) > DAMP_ANG) {
162                         vms_matrix rotmat, new_pm;
163
164                         roll_ang = fixmul(FrameTime,ROLL_RATE);
165
166                         if (abs(delta_ang) < roll_ang) roll_ang = delta_ang;
167                         else if (delta_ang<0) roll_ang = -roll_ang;
168
169                         tangles.p = tangles.h = 0;  tangles.b = roll_ang;
170                         vm_angles_2_matrix(&rotmat,&tangles);
171
172                         vm_matrix_x_matrix(&new_pm,&obj->orient,&rotmat);
173                         obj->orient = new_pm;
174                 }
175                 else floor_levelling=0;
176         }
177
178 }
179
180 void set_object_turnroll(object *obj)
181 {
182         fixang desired_bank;
183
184         desired_bank = -fixmul(obj->mtype.phys_info.rotvel.y,TURNROLL_SCALE);
185
186         if (obj->mtype.phys_info.turnroll != desired_bank) {
187                 fixang delta_ang,max_roll;
188
189                 max_roll = fixmul(ROLL_RATE,FrameTime);
190
191                 delta_ang = desired_bank - obj->mtype.phys_info.turnroll;
192
193                 if (labs(delta_ang) < max_roll)
194                         max_roll = delta_ang;
195                 else
196                         if (delta_ang < 0)
197                                 max_roll = -max_roll;
198
199                 obj->mtype.phys_info.turnroll += max_roll;
200         }
201
202 }
203
204 //list of segments went through
205 int phys_seglist[MAX_FVI_SEGS],n_phys_segs;
206
207
208 #define MAX_IGNORE_OBJS 100
209
210 #ifndef NDEBUG
211 #define EXTRA_DEBUG 1           //no extra debug when NDEBUG is on
212 #endif
213
214 #ifdef EXTRA_DEBUG
215 object *debug_obj=NULL;
216 #endif
217
218 #define XYZ(v) (v)->x,(v)->y,(v)->z
219
220
221 #ifndef NDEBUG
222 int     Total_retries=0, Total_sims=0;
223 int     Dont_move_ai_objects=0;
224 #endif
225
226 #define FT (f1_0/64)
227
228 extern int disable_new_fvi_stuff;
229 //      -----------------------------------------------------------------------------------------------------------
230 // add rotational velocity & acceleration
231 void do_physics_sim_rot(object *obj)
232 {
233         vms_angvec      tangles;
234         vms_matrix      rotmat,new_orient;
235         //fix                   rotdrag_scale;
236         physics_info *pi;
237
238         Assert(FrameTime > 0);  //Get MATT if hit this!
239
240         pi = &obj->mtype.phys_info;
241
242         if (!(pi->rotvel.x || pi->rotvel.y || pi->rotvel.z || pi->rotthrust.x || pi->rotthrust.y || pi->rotthrust.z))
243                 return;
244
245         if (obj->mtype.phys_info.drag) {
246                 int count;
247                 vms_vector accel;
248                 fix drag,r,k;
249
250                 count = FrameTime / FT;
251                 r = FrameTime % FT;
252                 k = fixdiv(r,FT);
253
254                 drag = (obj->mtype.phys_info.drag*5)/2;
255
256                 if (obj->mtype.phys_info.flags & PF_USES_THRUST) {
257
258                         vm_vec_copy_scale(&accel,&obj->mtype.phys_info.rotthrust,fixdiv(f1_0,obj->mtype.phys_info.mass));
259
260                         while (count--) {
261
262                                 vm_vec_add2(&obj->mtype.phys_info.rotvel,&accel);
263
264                                 vm_vec_scale(&obj->mtype.phys_info.rotvel,f1_0-drag);
265                         }
266
267                         //do linear scale on remaining bit of time
268
269                         vm_vec_scale_add2(&obj->mtype.phys_info.rotvel,&accel,k);
270                         vm_vec_scale(&obj->mtype.phys_info.rotvel,f1_0-fixmul(k,drag));
271                 }
272                 else if (! (obj->mtype.phys_info.flags & PF_FREE_SPINNING)) {
273                         fix total_drag=f1_0;
274
275                         while (count--)
276                                 total_drag = fixmul(total_drag,f1_0-drag);
277
278                         //do linear scale on remaining bit of time
279
280                         total_drag = fixmul(total_drag,f1_0-fixmul(k,drag));
281
282                         vm_vec_scale(&obj->mtype.phys_info.rotvel,total_drag);
283                 }
284
285         }
286
287         //mprintf( (0, "Rot vel = %.3f,%.3f,%.3f\n", f2fl(obj->mtype.phys_info.rotvel.x),f2fl(obj->mtype.phys_info.rotvel.y), f2fl(obj->mtype.phys_info.rotvel.z) ));
288
289         //now rotate object
290
291         //unrotate object for bank caused by turn
292         if (obj->mtype.phys_info.turnroll) {
293                 vms_matrix new_pm;
294
295                 tangles.p = tangles.h = 0;
296                 tangles.b = -obj->mtype.phys_info.turnroll;
297                 vm_angles_2_matrix(&rotmat,&tangles);
298                 vm_matrix_x_matrix(&new_pm,&obj->orient,&rotmat);
299                 obj->orient = new_pm;
300         }
301
302         tangles.p = fixmul(obj->mtype.phys_info.rotvel.x,FrameTime);
303         tangles.h = fixmul(obj->mtype.phys_info.rotvel.y,FrameTime);
304         tangles.b  = fixmul(obj->mtype.phys_info.rotvel.z,FrameTime);
305
306         vm_angles_2_matrix(&rotmat,&tangles);
307         vm_matrix_x_matrix(&new_orient,&obj->orient,&rotmat);
308         obj->orient = new_orient;
309
310         if (obj->mtype.phys_info.flags & PF_TURNROLL)
311                 set_object_turnroll(obj);
312
313         //re-rotate object for bank caused by turn
314         if (obj->mtype.phys_info.turnroll) {
315                 vms_matrix new_pm;
316
317                 tangles.p = tangles.h = 0;
318                 tangles.b = obj->mtype.phys_info.turnroll;
319                 vm_angles_2_matrix(&rotmat,&tangles);
320                 vm_matrix_x_matrix(&new_pm,&obj->orient,&rotmat);
321                 obj->orient = new_pm;
322         }
323
324         check_and_fix_matrix(&obj->orient);
325 }
326
327 //      -----------------------------------------------------------------------------------------------------------
328 //Simulate a physics object for this frame
329 void do_physics_sim(object *obj)
330 {
331         int ignore_obj_list[MAX_IGNORE_OBJS],n_ignore_objs;
332         int iseg;
333         int try_again;
334         int fate;
335         vms_vector frame_vec;                   //movement in this frame
336         vms_vector new_pos,ipos;                //position after this frame
337         int count=0;
338         int objnum;
339         int WallHitSeg, WallHitSide;
340         fvi_info hit_info;
341         fvi_query fq;
342         vms_vector save_pos;
343         int save_seg;
344         fix drag;
345         fix sim_time,old_sim_time;
346         vms_vector start_pos;
347         int obj_stopped=0;
348         fix moved_time;                 //how long objected moved before hit something
349         vms_vector save_p0,save_p1;
350         physics_info *pi;
351         int orig_segnum = obj->segnum;
352         int bounced=0;
353
354         Assert(obj->type != OBJ_NONE);
355         Assert(obj->movement_type == MT_PHYSICS);
356
357 #ifndef NDEBUG
358 if (Dont_move_ai_objects)
359         if (obj->control_type == CT_AI)
360                 return;
361 #endif
362
363         pi = &obj->mtype.phys_info;
364
365         do_physics_sim_rot(obj);
366
367         if (!(pi->velocity.x || pi->velocity.y || pi->velocity.z || pi->thrust.x || pi->thrust.y || pi->thrust.z))
368                 return;
369
370         objnum = OBJECT_NUMBER(obj);
371
372         n_phys_segs = 0;
373
374         disable_new_fvi_stuff = (obj->type != OBJ_PLAYER);
375
376         sim_time = FrameTime;
377
378 //debug_obj = obj;
379
380         #ifdef EXTRA_DEBUG
381         if (obj == debug_obj) {
382                 printf("object %d:\n  start pos = %x %x %x\n",objnum,XYZ(&obj->pos));
383                 printf("  thrust = %x %x %x\n",XYZ(&obj->mtype.phys_info.thrust));
384                 printf("  sim_time = %x\n",sim_time);
385         }
386
387         //check for correct object segment
388         if(!get_seg_masks(&obj->pos, obj->segnum, 0, __FILE__, __LINE__).centermask == 0)
389         {
390                 #ifndef NDEBUG
391                 mprintf((0,"Warning: object %d not in given seg!\n",objnum));
392                 #endif
393                 //Int3();  Removed by Rob 10/5/94
394                 if (!update_object_seg(obj)) {
395                         #ifndef NDEBUG
396                         mprintf((0,"Warning: can't find seg for object %d - moving\n",objnum));
397                         #endif
398                         if (!(Game_mode & GM_MULTI))
399                                 Int3();
400                         compute_segment_center(&obj->pos,&Segments[obj->segnum]);
401                         obj->pos.x += objnum;
402                 }
403         }
404         #endif
405
406         start_pos = obj->pos;
407
408         n_ignore_objs = 0;
409
410         Assert(obj->mtype.phys_info.brakes==0);         //brakes not used anymore?
411
412                 //if uses thrust, cannot have zero drag
413         Assert(!(obj->mtype.phys_info.flags&PF_USES_THRUST) || obj->mtype.phys_info.drag!=0);
414
415 //mprintf((0,"thrust=%x  speed=%x\n",vm_vec_mag(&obj->mtype.phys_info.thrust),vm_vec_mag(&obj->mtype.phys_info.velocity)));
416
417         //do thrust & drag
418         
419         if ((drag = obj->mtype.phys_info.drag) != 0) {
420
421                 int count;
422                 vms_vector accel;
423                 fix r,k;
424
425                 count = sim_time / FT;
426                 r = sim_time % FT;
427                 k = fixdiv(r,FT);
428
429                 if (obj->mtype.phys_info.flags & PF_USES_THRUST) {
430
431                         vm_vec_copy_scale(&accel,&obj->mtype.phys_info.thrust,fixdiv(f1_0,obj->mtype.phys_info.mass));
432
433                         while (count--) {
434
435                                 vm_vec_add2(&obj->mtype.phys_info.velocity,&accel);
436
437                                 vm_vec_scale(&obj->mtype.phys_info.velocity,f1_0-drag);
438                         }
439
440                         //do linear scale on remaining bit of time
441
442                         vm_vec_scale_add2(&obj->mtype.phys_info.velocity,&accel,k);
443
444                         vm_vec_scale(&obj->mtype.phys_info.velocity,f1_0-fixmul(k,drag));
445                 }
446                 else {
447                         fix total_drag=f1_0;
448
449                         while (count--)
450                                 total_drag = fixmul(total_drag,f1_0-drag);
451
452                         //do linear scale on remaining bit of time
453
454                         total_drag = fixmul(total_drag,f1_0-fixmul(k,drag));
455
456                         vm_vec_scale(&obj->mtype.phys_info.velocity,total_drag);
457                 }
458         }
459
460         #ifdef EXTRA_DEBUG
461         if (obj == debug_obj)
462                 printf("  velocity = %x %x %x\n",XYZ(&obj->mtype.phys_info.velocity));
463         #endif
464
465         do {
466                 try_again = 0;
467
468                 //Move the object
469                 vm_vec_copy_scale(&frame_vec, &obj->mtype.phys_info.velocity, sim_time);
470
471                 #ifdef EXTRA_DEBUG
472                 if (obj == debug_obj)
473                         printf("  pass %d, frame_vec = %x %x %x\n",count,XYZ(&frame_vec));
474                 #endif
475
476                 if ( (frame_vec.x==0) && (frame_vec.y==0) && (frame_vec.z==0) ) 
477                         break;
478
479                 count++;
480
481                 //      If retry count is getting large, then we are trying to do something stupid.
482                 if ( count > 3)         {
483                         if (obj->type == OBJ_PLAYER) {
484                                 if (count > 8)
485                                         break;
486                         } else
487                                 break;
488                 }
489
490                 vm_vec_add(&new_pos,&obj->pos,&frame_vec);
491
492                 #ifdef EXTRA_DEBUG
493                 if (obj == debug_obj)
494                         printf("   desired_pos  = %x %x %x\n",XYZ(&new_pos));
495                 #endif
496
497                 ignore_obj_list[n_ignore_objs] = -1;
498
499                 #ifdef EXTRA_DEBUG
500                 if (obj == debug_obj) {
501                         printf("   FVI parms: p0 = %8x %8x %8x, segnum=%x, size=%x\n",XYZ(&obj->pos),obj->segnum,obj->size);
502                         printf("              p1 = %8x %8x %8x\n",XYZ(&new_pos));
503                 }
504                 #endif
505
506                 fq.p0                                           = &obj->pos;
507                 fq.startseg                             = obj->segnum;
508                 fq.p1                                           = &new_pos;
509                 fq.rad                                  = obj->size;
510                 fq.thisobjnum                   = objnum;
511                 fq.ignore_obj_list      = ignore_obj_list;
512                 fq.flags                                        = FQ_CHECK_OBJS;
513
514                 if (obj->type == OBJ_WEAPON)
515                         fq.flags |= FQ_TRANSPOINT;
516
517                 if (obj->type == OBJ_PLAYER)
518                         fq.flags |= FQ_GET_SEGLIST;
519
520 //@@                    if (get_seg_masks(&obj->pos, obj->segnum, 0, __FILE__, __LINE__).centermask != 0)
521 //@@                            Int3();
522
523 save_p0 = *fq.p0;
524 save_p1 = *fq.p1;
525
526
527                 fate = find_vector_intersection(&fq,&hit_info);
528                 //      Matt: Mike's hack.
529                 if (fate == HIT_OBJECT) {
530                         object  *objp = &Objects[hit_info.hit_object];
531
532                         if ((objp->type == OBJ_WEAPON) && ((objp->id == PROXIMITY_ID) || (objp->id == SUPERPROX_ID)))
533                                 count--;
534                 }
535
536                 #ifndef NDEBUG
537                 if (fate == HIT_BAD_P0) {
538                         mprintf((0, "Warning: Bad p0 in physics!  Object = %i, type = %i [%s]\n", OBJECT_NUMBER(obj), obj->type, Object_type_names[obj->type]));
539                         Int3();
540                 }
541                 #endif
542
543                 if (obj->type == OBJ_PLAYER) {
544                         int i;
545
546                         if (n_phys_segs && phys_seglist[n_phys_segs-1]==hit_info.seglist[0])
547                                 n_phys_segs--;
548
549                         for (i=0;(i<hit_info.n_segs) && (n_phys_segs<MAX_FVI_SEGS-1);  )
550                                 phys_seglist[n_phys_segs++] = hit_info.seglist[i++];
551                 }
552
553                 #ifdef EXTRA_DEBUG
554                 if (obj == debug_obj)
555                         printf("   fate  = %d, hit_pnt = %8x %8x %8x\n",fate,XYZ(&hit_info.hit_pnt));;
556                 #endif
557
558                 ipos = hit_info.hit_pnt;
559                 iseg = hit_info.hit_seg;
560                 WallHitSide = hit_info.hit_side;
561                 WallHitSeg = hit_info.hit_side_seg;
562
563                 if (iseg==-1) {         //some sort of horrible error
564                         #ifndef NDEBUG
565                         mprintf((1, "iseg==-1 in physics!  Object = %i, type = %i (%s)\n", OBJECT_NUMBER(obj), obj->type, Object_type_names[obj->type]));
566                         #endif
567                         //Int3();
568                         //compute_segment_center(&ipos,&Segments[obj->segnum]);
569                         //ipos.x += objnum;
570                         //iseg = obj->segnum;
571                         //fate = HIT_NONE;
572                         if (obj->type == OBJ_WEAPON)
573                                 obj->flags |= OF_SHOULD_BE_DEAD;
574                         break;
575                 }
576
577                 Assert(!((fate==HIT_WALL) && ((WallHitSeg == -1) || (WallHitSeg > Highest_segment_index))));
578
579                 //if(!get_seg_masks(&hit_info.hit_pnt, hit_info.hit_seg, 0, __FILE__, __LINE__).centermask == 0)
580                 //      Int3();
581
582                 save_pos = obj->pos;                    //save the object's position
583                 save_seg = obj->segnum;
584
585                 // update object's position and segment number
586                 obj->pos = ipos;
587
588                 #ifdef EXTRA_DEBUG
589                 if (obj == debug_obj)
590                         printf("   new pos = %x %x %x\n",XYZ(&obj->pos));
591                 #endif
592
593                 if ( iseg != obj->segnum )
594                         obj_relink(objnum, iseg );
595
596                 //if start point not in segment, move object to center of segment
597                 if (get_seg_masks(&obj->pos, obj->segnum, 0, __FILE__, __LINE__).centermask !=0 )
598                 {
599                         int n;
600
601                         if ((n=find_object_seg(obj))==-1) {
602                                 //Int3();
603                                 if (obj->type==OBJ_PLAYER && (n=find_point_seg(&obj->last_pos,obj->segnum))!=-1) {
604                                         obj->pos = obj->last_pos;
605                                         obj_relink(objnum, n );
606                                 }
607                                 else {
608                                         compute_segment_center(&obj->pos,&Segments[obj->segnum]);
609                                         obj->pos.x += objnum;
610                                 }
611                                 if (obj->type == OBJ_WEAPON)
612                                         obj->flags |= OF_SHOULD_BE_DEAD;
613                         }
614                         return;
615                 }
616
617                 //calulate new sim time
618                 {
619                         //vms_vector moved_vec;
620                         vms_vector moved_vec_n;
621                         fix attempted_dist,actual_dist;
622
623                         old_sim_time = sim_time;
624
625                         actual_dist = vm_vec_normalized_dir(&moved_vec_n,&obj->pos,&save_pos);
626
627                         if (fate==HIT_WALL && vm_vec_dot(&moved_vec_n,&frame_vec) < 0) {                //moved backwards
628
629                                 //don't change position or sim_time
630
631 //*******                                       mprintf((0, "Obj %d moved backwards\n", OBJECT_NUMBER(obj)));
632
633                                 #ifdef EXTRA_DEBUG
634                                 if (obj == debug_obj)
635                                         printf("   Warning: moved backwards!\n");
636                                 #endif
637
638                                 obj->pos = save_pos;
639                 
640                                 //iseg = obj->segnum;           //don't change segment
641
642                                 obj_relink(objnum, save_seg );
643
644                                 moved_time = 0;
645                         }
646                         else {
647
648                                 //if (obj == debug_obj)
649                                 //      printf("   moved_vec = %x %x %x\n",XYZ(&moved_vec));
650                         
651                                 attempted_dist = vm_vec_mag(&frame_vec);
652
653                                 sim_time = fixmuldiv(sim_time,attempted_dist-actual_dist,attempted_dist);
654
655                                 moved_time = old_sim_time - sim_time;
656
657                                 if (sim_time < 0 || sim_time>old_sim_time) {
658                                         #ifndef NDEBUG
659                                         mprintf((0,"Bogus sim_time = %x, old = %x\n",sim_time,old_sim_time));
660                                         if (obj == debug_obj)
661                                                 printf("   Bogus sim_time = %x, old = %x, attempted_dist = %x, actual_dist = %x\n",sim_time,old_sim_time,attempted_dist,actual_dist);
662                                         //Int3(); Removed by Rob
663                                         #endif
664                                         sim_time = old_sim_time;
665                                         //WHY DOES THIS HAPPEN??
666
667                                         moved_time = 0;
668                                 }
669                         }
670
671                         #ifdef EXTRA_DEBUG
672                         if (obj == debug_obj)
673                                 printf("   new sim_time = %x\n",sim_time);
674                         #endif
675
676                 }
677
678
679                 switch( fate )          {
680
681                         case HIT_WALL:          {
682                                 vms_vector moved_v;
683                                 //@@fix total_d,moved_d;
684                                 fix hit_speed,wall_part;
685         
686                                 // Find hit speed       
687
688                                 vm_vec_sub(&moved_v,&obj->pos,&save_pos);
689
690                                 wall_part = vm_vec_dot(&moved_v,&hit_info.hit_wallnorm);
691
692                                 if (wall_part != 0 && moved_time>0 && (hit_speed=-fixdiv(wall_part,moved_time))>0)
693                                         collide_object_with_wall( obj, hit_speed, WallHitSeg, WallHitSide, &hit_info.hit_pnt );
694                                 else
695                                         scrape_object_on_wall(obj, WallHitSeg, WallHitSide, &hit_info.hit_pnt );
696
697                                 Assert( WallHitSeg > -1 );
698                                 Assert( WallHitSide > -1 );
699
700                                 if ( !(obj->flags&OF_SHOULD_BE_DEAD) )  {
701                                         int forcefield_bounce;          //bounce off a forcefield
702
703                                         Assert(BounceCheat || !(obj->mtype.phys_info.flags & PF_STICK && obj->mtype.phys_info.flags & PF_BOUNCE));      //can't be bounce and stick
704
705                                         forcefield_bounce = (TmapInfo[Segments[WallHitSeg].sides[WallHitSide].tmap_num].flags & TMI_FORCE_FIELD);
706
707                                         if (!forcefield_bounce && (obj->mtype.phys_info.flags & PF_STICK)) {            //stop moving
708
709                                                 // mprintf((0, "Object %i stuck at %i:%i\n", OBJECT_NUMBER(obj), WallHitSeg, WallHitSide));
710                                                 add_stuck_object(obj, WallHitSeg, WallHitSide);
711
712                                                 vm_vec_zero(&obj->mtype.phys_info.velocity);
713                                                 obj_stopped = 1;
714                                                 try_again = 0;
715                                         }
716                                         else {                                  // Slide object along wall
717                                                 int check_vel=0;
718
719                                                 //We're constrained by wall, so subtract wall part from
720                                                 //velocity vector
721
722                                                 wall_part = vm_vec_dot(&hit_info.hit_wallnorm,&obj->mtype.phys_info.velocity);
723
724 //                                              mprintf((0, "%d", f2i(vm_vec_mag(&hit_info.hit_wallnorm)) ));
725
726                                                 if (forcefield_bounce || (obj->mtype.phys_info.flags & PF_BOUNCE)) {            //bounce off wall
727                                                         wall_part *= 2; //Subtract out wall part twice to achieve bounce
728
729                                                         if (forcefield_bounce) {
730                                                                 check_vel = 1;                          //check for max velocity
731                                                                 if (obj->type == OBJ_PLAYER)
732                                                                         wall_part *= 2;         //player bounce twice as much
733                                                         }
734
735                                                         if ( obj->mtype.phys_info.flags & PF_BOUNCES_TWICE) {
736                                                                 Assert(obj->mtype.phys_info.flags & PF_BOUNCE);
737                                                                 if (obj->mtype.phys_info.flags & PF_BOUNCED_ONCE)
738                                                                         obj->mtype.phys_info.flags &= ~(PF_BOUNCE+PF_BOUNCED_ONCE+PF_BOUNCES_TWICE);
739                                                                 else
740                                                                         obj->mtype.phys_info.flags |= PF_BOUNCED_ONCE;
741                                                         }
742
743                                                         bounced = 1;            //this object bounced
744                                                 }
745
746                                                 vm_vec_scale_add2(&obj->mtype.phys_info.velocity,&hit_info.hit_wallnorm,-wall_part);
747
748 //                                              mprintf((0, "Velocity at bounce time = %d\n", f2i(vm_vec_mag(&obj->mtype.phys_info.velocity))));
749
750 //if (obj==ConsoleObject)
751 //      mprintf((0,"player vel = %x\n",vm_vec_mag_quick(&obj->mtype.phys_info.velocity)));
752
753                                                 if (check_vel) {
754                                                         fix vel = vm_vec_mag_quick(&obj->mtype.phys_info.velocity);
755
756                                                         if (vel > MAX_OBJECT_VEL)
757                                                                 vm_vec_scale(&obj->mtype.phys_info.velocity,fixdiv(MAX_OBJECT_VEL,vel));
758                                                 }
759
760                                                 if (bounced && obj->type == OBJ_WEAPON)
761                                                         vm_vector_2_matrix(&obj->orient,&obj->mtype.phys_info.velocity,&obj->orient.uvec,NULL);
762
763                                                 #ifdef EXTRA_DEBUG
764                                                 if (obj == debug_obj) {
765                                                         printf("   sliding - wall_norm %x %x %x %x\n",wall_part,XYZ(&hit_info.hit_wallnorm));
766                                                         printf("   wall_part %x, new velocity = %x %x %x\n",wall_part,XYZ(&obj->mtype.phys_info.velocity));
767                                                 }
768                                                 #endif
769
770                                                 try_again = 1;
771                                         }
772                                 }
773                                 break;
774                         }
775
776                         case HIT_OBJECT:                {
777                                 vms_vector old_vel;
778
779                                 // Mark the hit object so that on a retry the fvi code
780                                 // ignores this object.
781
782                                 Assert(hit_info.hit_object != -1);
783
784                                 //      Calculcate the hit point between the two objects.
785                                 {       vms_vector      *ppos0, *ppos1, pos_hit;
786                                         fix                     size0, size1;
787                                         ppos0 = &Objects[hit_info.hit_object].pos;
788                                         ppos1 = &obj->pos;
789                                         size0 = Objects[hit_info.hit_object].size;
790                                         size1 = obj->size;
791                                         Assert(size0+size1 != 0);       // Error, both sizes are 0, so how did they collide, anyway?!?
792                                         //vm_vec_scale(vm_vec_sub(&pos_hit, ppos1, ppos0), fixdiv(size0, size0 + size1));
793                                         //vm_vec_add2(&pos_hit, ppos0);
794                                         vm_vec_sub(&pos_hit, ppos1, ppos0);
795                                         vm_vec_scale_add(&pos_hit,ppos0,&pos_hit,fixdiv(size0, size0 + size1));
796
797                                         old_vel = obj->mtype.phys_info.velocity;
798
799                                         collide_two_objects( obj, &Objects[hit_info.hit_object], &pos_hit);
800
801                                 }
802
803                                 // Let object continue its movement
804                                 if ( !(obj->flags&OF_SHOULD_BE_DEAD)  ) {
805                                         //obj->pos = save_pos;
806
807                                         if (obj->mtype.phys_info.flags&PF_PERSISTENT || (old_vel.x == obj->mtype.phys_info.velocity.x && old_vel.y == obj->mtype.phys_info.velocity.y && old_vel.z == obj->mtype.phys_info.velocity.z)) {
808                                                 //if (Objects[hit_info.hit_object].type == OBJ_POWERUP)
809                                                         ignore_obj_list[n_ignore_objs++] = hit_info.hit_object;
810                                                 try_again = 1;
811                                         }
812                                 }
813
814                                 break;
815                         }       
816                         case HIT_NONE:          
817                         #ifdef TACTILE
818                                 if (TactileStick && obj==ConsoleObject && !(FrameCount & 15))
819                                  Tactile_Xvibrate_clear ();
820                         #endif
821                                 break;
822
823                         #ifndef NDEBUG
824                         case HIT_BAD_P0:
825                                 Int3();         // Unexpected collision type: start point not in specified segment.
826                                 mprintf((0,"Warning: Bad p0 in physics!!!\n"));
827                                 break;
828                         default:
829                                 // Unknown collision type returned from find_vector_intersection!!
830                                 Int3();
831                                 break;
832                         #endif
833                 }
834
835         } while ( try_again );
836
837         //      Pass retry count info to AI.
838         if (obj->control_type == CT_AI) {
839                 if (count > 0) {
840                         Ai_local_info[objnum].retry_count = count-1;
841                         #ifndef NDEBUG
842                         Total_retries += count-1;
843                         Total_sims++;
844                         #endif
845                 }
846         }
847
848         //I'm not sure why we do this.  I wish there were a comment that
849         //explained it.  I think maybe it only needs to be done if the object
850         //is sliding, but I don't know
851         if (!obj_stopped && !bounced)   {       //Set velocity from actual movement
852                 vms_vector moved_vec;
853
854                 vm_vec_sub(&moved_vec,&obj->pos,&start_pos);
855                 vm_vec_copy_scale(&obj->mtype.phys_info.velocity,&moved_vec,fixdiv(f1_0,FrameTime));
856
857                 #ifdef BUMP_HACK
858                 if (obj==ConsoleObject && (obj->mtype.phys_info.velocity.x==0 && obj->mtype.phys_info.velocity.y==0 && obj->mtype.phys_info.velocity.z==0) &&
859                           !(obj->mtype.phys_info.thrust.x==0 && obj->mtype.phys_info.thrust.y==0 && obj->mtype.phys_info.thrust.z==0)) {
860                         vms_vector center,bump_vec;
861
862                         //bump player a little towards center of segment to unstick
863
864                         compute_segment_center(&center,&Segments[obj->segnum]);
865                         vm_vec_normalized_dir_quick(&bump_vec,&center,&obj->pos);
866
867                         //don't bump player toward center of reactor segment
868                         if (Segment2s[obj->segnum].special == SEGMENT_IS_CONTROLCEN)
869                                 vm_vec_negate(&bump_vec);
870
871                         vm_vec_scale_add2(&obj->pos,&bump_vec,obj->size/5);
872
873                         //if moving away from seg, might move out of seg, so update
874                         if (Segment2s[obj->segnum].special == SEGMENT_IS_CONTROLCEN)
875                                 update_object_seg(obj);
876                 }
877                 #endif
878         }
879
880         //Assert(check_point_in_seg(&obj->pos,obj->segnum,0).centermask==0);
881
882         //if (obj->control_type == CT_FLYING)
883         if (obj->mtype.phys_info.flags & PF_LEVELLING)
884                 do_physics_align_object( obj );
885
886
887         //hack to keep player from going through closed doors
888         if (obj->type==OBJ_PLAYER && obj->segnum!=orig_segnum && (Physics_cheat_flag!=0xBADA55) ) {
889                 int sidenum;
890
891                 sidenum = find_connect_side(&Segments[obj->segnum],&Segments[orig_segnum]);
892
893                 if (sidenum != -1) {
894
895                         if (! (WALL_IS_DOORWAY(&Segments[orig_segnum],sidenum) & WID_FLY_FLAG)) {
896                                 side *s;
897                                 int vertnum,num_faces,i;
898                                 fix dist;
899                                 int vertex_list[6];
900
901                                 //bump object back
902
903                                 s = &Segments[orig_segnum].sides[sidenum];
904
905                                 if (orig_segnum==-1)
906                                         Error("orig_segnum == -1 in physics");
907
908                                 create_abs_vertex_lists(&num_faces, vertex_list, orig_segnum, sidenum, __FILE__, __LINE__);
909
910                                 //let's pretend this wall is not triangulated
911                                 vertnum = vertex_list[0];
912                                 for (i=1;i<4;i++)
913                                         if (vertex_list[i] < vertnum)
914                                                 vertnum = vertex_list[i];
915
916                                 #ifdef COMPACT_SEGS
917                                         {
918                                         vms_vector _vn;
919                                         get_side_normal(&Segments[orig_segnum], sidenum, 0, &_vn );
920                                         dist = vm_dist_to_plane(&start_pos, &_vn, &Vertices[vertnum]);
921                                         vm_vec_scale_add(&obj->pos,&start_pos,&_vn,obj->size-dist);
922                                         }
923                                 #else
924                                         dist = vm_dist_to_plane(&start_pos, &s->normals[0], &Vertices[vertnum]);
925                                         vm_vec_scale_add(&obj->pos,&start_pos,&s->normals[0],obj->size-dist);
926                                 #endif
927                                 update_object_seg(obj);
928
929                         }
930                 }
931         }
932
933 //--WE ALWYS WANT THIS IN, MATT AND MIKE DECISION ON 12/10/94, TWO MONTHS AFTER FINAL   #ifndef NDEBUG
934         //if end point not in segment, move object to last pos, or segment center
935         if (get_seg_masks(&obj->pos, obj->segnum, 0, __FILE__, __LINE__).centermask != 0)
936         {
937                 if (find_object_seg(obj)==-1) {
938                         int n;
939
940                         //Int3();
941                         if (obj->type==OBJ_PLAYER && (n=find_point_seg(&obj->last_pos,obj->segnum))!=-1) {
942                                 obj->pos = obj->last_pos;
943                                 obj_relink(objnum, n );
944                         }
945                         else {
946                                 compute_segment_center(&obj->pos,&Segments[obj->segnum]);
947                                 obj->pos.x += objnum;
948                         }
949                         if (obj->type == OBJ_WEAPON)
950                                 obj->flags |= OF_SHOULD_BE_DEAD;
951                 }
952         }
953 //--WE ALWYS WANT THIS IN, MATT AND MIKE DECISION ON 12/10/94, TWO MONTHS AFTER FINAL   #endif
954
955
956 }
957
958 //--unused-- //tell us what the given object will do (as far as hiting walls) in
959 //--unused-- //the given time (in seconds) t.  Igores acceleration (sorry)
960 //--unused-- //if check_objects is set, check with objects, else just with walls
961 //--unused-- //returns fate, fills in hit time.  If fate==HIT_NONE, hit_time undefined
962 //--unused-- int physics_lookahead(object *obj,fix t,int fvi_flags,fix *hit_time, fvi_info *hit_info)
963 //--unused-- {
964 //--unused--    vms_vector new_pos;
965 //--unused--    int objnum,fate;
966 //--unused--    fvi_query fq;
967 //--unused--
968 //--unused--    Assert(obj->movement_type == MT_PHYSICS);
969 //--unused--
970 //--unused--    objnum = OBJECT_NUMBER(obj);
971 //--unused--
972 //--unused--    vm_vec_scale_add(&new_pos, &obj->pos, &obj->mtype.phys_info.velocity, t);
973 //--unused--
974 //--unused--    fq.p0                                           = &obj->pos;
975 //--unused--    fq.startseg                             = obj->segnum;
976 //--unused--    fq.p1                                           = &new_pos;
977 //--unused--    fq.rad                                  = obj->size;
978 //--unused--    fq.thisobjnum                   = objnum;
979 //--unused--    fq.ignore_obj_list      = NULL;
980 //--unused--    fq.flags                                        = fvi_flags;
981 //--unused--
982 //--unused--    fate = find_vector_intersection(&fq,hit_info);
983 //--unused--
984 //--unused--    if (fate != HIT_NONE) {
985 //--unused--            fix dist,speed;
986 //--unused--
987 //--unused--            dist = vm_vec_dist(&obj->pos, &hit_info->hit_pnt);
988 //--unused--
989 //--unused--            speed = vm_vec_mag(&obj->mtype.phys_info.velocity);
990 //--unused--
991 //--unused--            *hit_time = fixdiv(dist,speed);
992 //--unused--
993 //--unused--    }
994 //--unused--
995 //--unused--    return fate;
996 //--unused--
997 //--unused-- }
998
999 //Applies an instantaneous force on an object, resulting in an instantaneous
1000 //change in velocity.
1001 void phys_apply_force(object *obj,vms_vector *force_vec)
1002 {
1003
1004         //      Put in by MK on 2/13/96 for force getting applied to Omega blobs, which have 0 mass,
1005         //      in collision with crazy reactor robot thing on d2levf-s.
1006         if (obj->mtype.phys_info.mass == 0)
1007                 return;
1008
1009         if (obj->movement_type != MT_PHYSICS)
1010                 return;
1011
1012 #ifdef TACTILE
1013    if (TactileStick && obj==&Objects[Players[Player_num].objnum])
1014                 Tactile_apply_force (force_vec,&obj->orient);
1015 #endif
1016
1017         //Add in acceleration due to force
1018         vm_vec_scale_add2(&obj->mtype.phys_info.velocity,force_vec,fixdiv(f1_0,obj->mtype.phys_info.mass));
1019
1020
1021 }
1022
1023 //      ----------------------------------------------------------------
1024 //      Do *dest = *delta unless:
1025 //                              *delta is pretty small
1026 //              and     they are of different signs.
1027 void physics_set_rotvel_and_saturate(fix *dest, fix delta)
1028 {
1029         if ((delta ^ *dest) < 0) {
1030                 if (abs(delta) < F1_0/8) {
1031                         // mprintf((0, "D"));
1032                         *dest = delta/4;
1033                 } else
1034                         // mprintf((0, "d"));
1035                         *dest = delta;
1036         } else {
1037                 // mprintf((0, "!"));
1038                 *dest = delta;
1039         }
1040 }
1041
1042 //      ------------------------------------------------------------------------------------------------------
1043 //      Note: This is the old ai_turn_towards_vector code.
1044 //      phys_apply_rot used to call ai_turn_towards_vector until I fixed it, which broke phys_apply_rot.
1045 void physics_turn_towards_vector(vms_vector *goal_vector, object *obj, fix rate)
1046 {
1047         vms_angvec      dest_angles, cur_angles;
1048         fix                     delta_p, delta_h;
1049         vms_vector      *rotvel_ptr = &obj->mtype.phys_info.rotvel;
1050
1051         // Make this object turn towards the goal_vector.  Changes orientation, doesn't change direction of movement.
1052         // If no one moves, will be facing goal_vector in 1 second.
1053
1054         //      Detect null vector.
1055         if ((goal_vector->x == 0) && (goal_vector->y == 0) && (goal_vector->z == 0))
1056                 return;
1057
1058         //      Make morph objects turn more slowly.
1059         if (obj->control_type == CT_MORPH)
1060                 rate *= 2;
1061
1062         vm_extract_angles_vector(&dest_angles, goal_vector);
1063         vm_extract_angles_vector(&cur_angles, &obj->orient.fvec);
1064
1065         delta_p = (dest_angles.p - cur_angles.p);
1066         delta_h = (dest_angles.h - cur_angles.h);
1067
1068         if (delta_p > F1_0/2) delta_p = dest_angles.p - cur_angles.p - F1_0;
1069         if (delta_p < -F1_0/2) delta_p = dest_angles.p - cur_angles.p + F1_0;
1070         if (delta_h > F1_0/2) delta_h = dest_angles.h - cur_angles.h - F1_0;
1071         if (delta_h < -F1_0/2) delta_h = dest_angles.h - cur_angles.h + F1_0;
1072
1073         delta_p = fixdiv(delta_p, rate);
1074         delta_h = fixdiv(delta_h, rate);
1075
1076         if (abs(delta_p) < F1_0/16) delta_p *= 4;
1077         if (abs(delta_h) < F1_0/16) delta_h *= 4;
1078
1079         physics_set_rotvel_and_saturate(&rotvel_ptr->x, delta_p);
1080         physics_set_rotvel_and_saturate(&rotvel_ptr->y, delta_h);
1081         rotvel_ptr->z = 0;
1082 }
1083
1084 //      -----------------------------------------------------------------------------
1085 //      Applies an instantaneous whack on an object, resulting in an instantaneous
1086 //      change in orientation.
1087 void phys_apply_rot(object *obj,vms_vector *force_vec)
1088 {
1089         fix     rate, vecmag;
1090
1091         if (obj->movement_type != MT_PHYSICS)
1092                 return;
1093
1094         vecmag = vm_vec_mag(force_vec)/8;
1095         if (vecmag < F1_0/256)
1096                 rate = 4*F1_0;
1097         else if (vecmag < obj->mtype.phys_info.mass >> 14)
1098                 rate = 4*F1_0;
1099         else {
1100                 rate = fixdiv(obj->mtype.phys_info.mass, vecmag);
1101                 if (obj->type == OBJ_ROBOT) {
1102                         if (rate < F1_0/4)
1103                                 rate = F1_0/4;
1104                         //      Changed by mk, 10/24/95, claw guys should not slow down when attacking!
1105                         if (!Robot_info[obj->id].thief && !Robot_info[obj->id].attack_type) {
1106                                 if (obj->ctype.ai_info.SKIP_AI_COUNT * FrameTime < 3*F1_0/4) {
1107                                         fix     tval = fixdiv(F1_0, 8*FrameTime);
1108                                         int     addval;
1109
1110                                         addval = f2i(tval);
1111
1112                                         if ( (d_rand() * 2) < (tval & 0xffff))
1113                                                 addval++;
1114                                         obj->ctype.ai_info.SKIP_AI_COUNT += addval;
1115                                         // -- mk: too much stuff making hard to see my debug messages...mprintf((0, "FrameTime = %7.3f, addval = %i\n", f2fl(FrameTime), addval));
1116                                 }
1117                         }
1118                 } else {
1119                         if (rate < F1_0/2)
1120                                 rate = F1_0/2;
1121                 }
1122         }
1123
1124         //      Turn amount inversely proportional to mass.  Third parameter is seconds to do 360 turn.
1125         physics_turn_towards_vector(force_vec, obj, rate);
1126
1127
1128 }
1129
1130
1131 //this routine will set the thrust for an object to a value that will
1132 //(hopefully) maintain the object's current velocity
1133 void set_thrust_from_velocity(object *obj)
1134 {
1135         fix k;
1136
1137         Assert(obj->movement_type == MT_PHYSICS);
1138
1139         k = fixmuldiv(obj->mtype.phys_info.mass,obj->mtype.phys_info.drag,(f1_0-obj->mtype.phys_info.drag));
1140
1141         vm_vec_copy_scale(&obj->mtype.phys_info.thrust,&obj->mtype.phys_info.velocity,k);
1142
1143 }
1144
1145