]> icculus.org git repositories - taylor/freespace2.git/blob - src/observer/observer.cpp
Initial revision
[taylor/freespace2.git] / src / observer / observer.cpp
1 /*
2  * $Logfile: /Freespace2/code/Observer/Observer.cpp $
3  * $Revision$
4  * $Date$
5  * $Author$
6  *
7  * $NoKeywords: $
8  */
9
10 #include "observer.h"
11 #include "object.h"
12 #include "vecmat.h"
13 #include "systemvars.h"
14 #include "player.h"
15
16 observer Observers[MAX_OBSERVER_OBS];
17
18 int Num_observer_obs;
19
20 void observer_init()
21 {
22         int idx;
23         for(idx=0;idx<MAX_OBSERVER_OBS;idx++){
24                 Observers[idx].flags = 0;
25                 Observers[idx].objnum = -1;
26                 Observers[idx].target_objnum = -1;
27         }
28
29         Num_observer_obs = 0;
30 }
31
32 int observer_create(matrix *orient, vector *pos)
33 {
34         int objnum,idx; 
35         physics_info *pi;
36
37         // try and find the first free observer slot
38         for(idx=0;idx<MAX_OBSERVER_OBS;idx++){
39                 if(!(Observers[idx].flags & OBS_FLAG_USED))
40                         break;
41         }
42         // if we couldn't find an open slot
43         if(idx == MAX_OBSERVER_OBS){
44                 mprintf(("Ran out of observer slots!\n"));
45                 return -1;
46         }
47         
48         // attempt to create the object
49         objnum = obj_create(OBJ_OBSERVER,0,idx,orient,pos,1.0f,0);
50
51         // give the observer Descent style physics
52         Objects[objnum].flags |= OF_PHYSICS;
53         physics_init(&Objects[objnum].phys_info);
54         pi = &Objects[objnum].phys_info;
55         pi->flags |= PF_ACCELERATES | PF_SLIDE_ENABLED;
56         
57         
58         // setup some physics parameters
59         pi->max_vel.x = OBS_MAX_VEL_X;
60         pi->max_vel.y = OBS_MAX_VEL_Y;
61         pi->max_vel.z = OBS_MAX_VEL_Z;  
62         vm_vec_zero(&pi->prev_ramp_vel);
63         vm_vec_zero(&pi->desired_vel);
64         vm_vec_zero(&pi->desired_rotvel);
65         vm_vec_zero(&pi->vel);
66         vm_vec_zero(&pi->rotvel);
67         vm_vec_zero(&pi->prev_fvec);
68         memset(&pi->last_rotmat,0,sizeof(matrix));
69         pi->forward_thrust = 0.0f;
70         pi->speed = 0.0f;
71         pi->fspeed = 0.0f;
72         pi->heading = 0.0f;
73                 
74         // fail situation
75         if(objnum == -1)
76                 return -1;
77
78         // set up the observer data
79         Observers[idx].flags |= OBS_FLAG_USED;
80         Observers[idx].objnum = objnum;
81         Observers[idx].target_objnum = -1;
82
83         return objnum;
84 }
85
86 void observer_delete(object *obj)
87 {
88         int num;
89         
90         num = obj->instance;
91         Assert( Observers[num].objnum == OBJ_INDEX(obj));
92
93         Observers[num].objnum = -1;
94         Observers[num].target_objnum = -1;
95         Observers[num].flags = 0;           // mark it as being free
96 }
97
98 // get the eye position and orientation for the passed observer object
99 void observer_get_eye(vector *eye_pos, matrix *eye_orient, object *obj)
100 {
101         // copy in the observer position and orientation
102         memcpy(eye_pos,&obj->pos,sizeof(vector));
103         memcpy(eye_orient,&obj->orient,sizeof(matrix));
104
105         // if we're in a weird padlock view
106         if ( Viewer_mode & VM_PADLOCK_ANY ){
107                 player_get_padlock_orient(eye_orient);
108         }
109 }