]> icculus.org git repositories - taylor/freespace2.git/blob - src/network/multi_observer.cpp
Initial revision
[taylor/freespace2.git] / src / network / multi_observer.cpp
1 /*
2  * $Logfile: /Freespace2/code/Network/multi_observer.cpp $
3  * $Revision$
4  * $Date$
5  * $Author$
6  *
7  * $Log$
8  * Revision 1.1  2002/05/03 03:28:10  root
9  * Initial revision
10  *  
11  * 
12  * 6     9/14/99 2:21p Dave
13  * Fixed observer mode joining and ingame stuff.
14  * 
15  * 5     8/19/99 10:59a Dave
16  * Packet loss detection.
17  * 
18  * 4     11/17/98 11:12a Dave
19  * Removed player identification by address. Now assign explicit id #'s.
20  * 
21  * 3     11/05/98 5:55p Dave
22  * Big pass at reducing #includes
23  * 
24  * 2     10/07/98 10:53a Dave
25  * Initial checkin.
26  * 
27  * 1     10/07/98 10:50a Dave
28  * 
29  * 13    6/13/98 6:01p Hoffoss
30  * Externalized all new (or forgot to be added) strings to all the code.
31  * 
32  * 12    6/04/98 11:04a Allender
33  * object update level stuff.  Don't reset to high when becoming an
34  * observer of any type.  default to low when guy is a dialup customer
35  * 
36  * 11    5/22/98 9:49p Allender
37  * make observers Player_ship hidden from sensors so that he cannot target
38  * his "ship".
39  * 
40  * 10    5/19/98 11:36p Allender
41  * fixed very nasty mask problem with ingame joiner marking player objects
42  * incorrectly.  Named ingame joiner ship and observer ship unique names
43  * 
44  * 9     4/18/98 5:00p Dave
45  * Put in observer zoom key. Made mission sync screen more informative.
46  * 
47  * 8     4/03/98 3:13p Dave
48  * More work on udp reliable transport. Fixed observer hud offset problem.
49  * Made observer join and ingame join work again. Put subnet broadcasting
50  * in for TCP.
51  * 
52  * 7     3/30/98 6:27p Dave
53  * Put in a more official set of multiplayer options, including a system
54  * for distributing netplayer and netgame settings.
55  * 
56  * 6     3/24/98 5:00p Dave
57  * Fixed several ui bugs. Put in pre and post voice stream playback sound
58  * fx. Put in error specific popups for clients getting dropped from games
59  * through actions other than their own.
60  * 
61  * 5     3/23/98 5:42p Dave
62  * Put in automatic xfer of pilot pic files. Changed multi_xfer system so
63  * that it can support multiplayer sends/received between client and
64  * server simultaneously.
65  * 
66  * 4     3/15/98 4:17p Dave
67  * Fixed oberver hud problems. Put in handy netplayer macros. Reduced size
68  * of network orientation matrices.
69  * 
70  * 3     3/14/98 2:48p Dave
71  * Cleaned up observer joining code. Put in support for file xfers to
72  * ingame joiners (observers or not). Revamped and reinstalled pseudo
73  * lag/loss system.
74  * 
75  * 2     3/13/98 2:51p Dave
76  * Put in support for observers to join ingame.
77  * 
78  * 1     3/12/98 5:44p Dave
79  *  
80  * $NoKeywords: $
81  */
82
83 #include "freespace.h"
84 #include "multi.h"
85 #include "multiutil.h"
86 #include "object.h"
87 #include "observer.h"
88 #include "hudconfig.h"
89 #include "hudobserver.h"
90 #include "managepilot.h"
91 #include "multi_observer.h"
92 #include "missionparse.h"
93 #include "timer.h"
94
95 // ---------------------------------------------------------------------------------------
96 // MULTI OBSERVER DEFINES/VARS
97 //
98
99
100 // ---------------------------------------------------------------------------------------
101 // MULTI OBSERVER FUNCTIONS
102 //
103
104 // create a _permanent_ observer player 
105 int multi_obs_create_player(int player_num,char *name,net_addr *addr,player *pl)
106 {       
107         // blast the player struct
108         memset(&Net_players[player_num],0,sizeof(net_player));
109         
110         // Net_players[player_num].flags |= (NETINFO_FLAG_CONNECTED | NETINFO_FLAG_OBSERVER);   
111         // DOH!!! The lack of this caused many bugs. 
112         Net_players[player_num].flags = (NETINFO_FLAG_DO_NETWORKING | NETINFO_FLAG_OBSERVER);
113         // memcpy(&Net_players[player_num].p_info.addr, addr, sizeof(net_addr));        
114         Net_players[player_num].player = pl;
115         
116         // 6/3/98 -- don't set observer to update high...let it be whatever player set it at.
117         //Net_players[player_num].p_info.options.obj_update_level = OBJ_UPDATE_HIGH;
118         // set up the net_player structure
119         memset(pl, 0, sizeof(player));
120         stuff_netplayer_info( &Net_players[player_num], addr, 0, pl );
121         Net_players[player_num].last_heard_time = timer_get_fixed_seconds();
122         Net_players[player_num].reliable_socket = INVALID_SOCKET;
123         Net_players[player_num].s_info.kick_timestamp = -1;
124         Net_players[player_num].s_info.voice_token_timestamp = -1;
125         Net_players[player_num].s_info.tracker_security_last = -1;
126         Net_players[player_num].s_info.target_objnum = -1;
127         Net_players[player_num].s_info.accum_buttons = 0;
128
129         // reset the ping for this player
130         multi_ping_reset(&Net_players[player_num].s_info.ping);
131         
132         // timestamp his last_full_update_time
133         Net_players[player_num].s_info.last_full_update_time = timestamp(0);                    
134         Net_players[player_num].player->objnum = -1;    
135
136         // nil his file xfer handle
137         Net_players[player_num].s_info.xfer_handle = -1;
138
139         // zero out his object update and control info sequencing data
140         Net_players[player_num].client_cinfo_seq = 0;
141         Net_players[player_num].client_server_seq = 0;          
142
143         // his kick timestamp
144         Net_players[player_num].s_info.kick_timestamp = -1;
145
146         // nil his data rate timestamp stuff
147         Net_players[player_num].s_info.rate_stamp = -1;
148         Net_players[player_num].s_info.rate_bytes = 0;
149
150         // nil packet buffer stuff
151         Net_players[player_num].s_info.unreliable_buffer_size = 0;
152         Net_players[player_num].s_info.reliable_buffer_size = 0;
153
154         // callsign and short callsign
155         strcpy(pl->callsign,name);
156         pilot_set_short_callsign(pl,SHORT_CALLSIGN_PIXEL_W);
157         pl->flags |= PLAYER_FLAGS_STRUCTURE_IN_USE;     
158
159         Net_players[player_num].sv_bytes_sent = 0;      
160         Net_players[player_num].sv_last_pl = -1;        
161         Net_players[player_num].cl_bytes_recvd = 0;     
162         Net_players[player_num].cl_last_pl = -1;
163         
164         return 1;
165 }
166
167 // create an explicit observer object and assign it to the passed player
168 void multi_obs_create_observer(net_player *pl)
169 {
170         int objnum;
171         
172         // create the basic observer object
173         objnum = observer_create( &vmd_identity_matrix, &vmd_zero_vector);      
174         Assert(objnum != -1);
175         Objects[objnum].flags |= OF_PLAYER_SHIP;        
176         Objects[objnum].net_signature = 0;
177
178         // put it a 1,1,1
179         Objects[objnum].pos.x = 1.0f;
180         Objects[objnum].pos.y = 1.0f;
181         Objects[objnum].pos.z = 1.0f;
182
183         // assign this object to the player
184         pl->player->objnum = objnum;                            
185 }
186
187 // create observer object locally, and additionally, setup some other information
188 // ( client-side equivalent of multi_obs_create_observer() )
189 void multi_obs_create_observer_client()
190 {
191         int pobj_num;
192         
193         Assert(!(Net_player->flags & NETINFO_FLAG_OBS_PLAYER));                                 
194
195         // make me an observer object
196         multi_obs_create_observer(Net_player);  
197                                         
198         // set my object to be the observer object      
199         Player_obj = &Objects[Net_player->player->objnum];
200         
201         // create the default player ship object and use that as my default virtual "ship", and make it "invisible"
202         pobj_num = parse_create_object(&Player_start_pobject);
203         Assert(pobj_num != -1);
204         obj_set_flags(&Objects[pobj_num],OF_PLAYER_SHIP);
205         Player_ship = &Ships[Objects[pobj_num].instance];
206
207         // make ship hidden from sensors so that this observer cannot target it.  Observers really have two ships
208         // one observer, and one "Player_ship".  Observer needs to ignore the Player_ship.
209         Player_ship->flags |= SF_HIDDEN_FROM_SENSORS;
210         strcpy(Player_ship->ship_name, XSTR("Observer Ship",688));
211         Player_ai = &Ai_info[Ships[Objects[pobj_num].instance].ai_index];               
212
213         // configure the hud to be in "observer" mode
214         hud_config_as_observer(Player_ship,Player_ai);  
215                 
216         // set some flags for myself
217         Net_player->flags |= NETINFO_FLAG_OBSERVER;     
218         
219         // reset the control info structure
220         memset(&Player->ci,0,sizeof(control_info));     
221 }
222
223 // create objects for all known observers in the game at level start
224 // call this before entering a mission
225 // this implies for the local player in the case of a client or for _all_ players in the case of a server
226 void multi_obs_level_init()
227 {
228         int idx;        
229
230         // unset the OBS_PLAYER flag here for all net players
231         for(idx=0;idx<MAX_PLAYERS;idx++){
232                 Net_players[idx].flags &= ~(NETINFO_FLAG_OBS_PLAYER);
233         }
234
235         // if i'm a client and I'm an observer, create an object for myself
236         if(!(Net_player->flags & NETINFO_FLAG_AM_MASTER) && (Net_player->flags & NETINFO_FLAG_OBSERVER)){               
237                 // create my own observer object and setup other misc. data
238                 multi_obs_create_observer_client();
239         }
240         // otherwise create stuff for all (permanent) observers in the game
241         else {
242                 for(idx=0;idx<MAX_PLAYERS;idx++){
243                         if(MULTI_CONNECTED(Net_players[idx]) && MULTI_OBSERVER(Net_players[idx])){
244                                 // make an observer object for the guy
245                                 multi_obs_create_observer(&Net_players[idx]);
246                         }
247                 }
248         }
249 }
250
251 // if i'm an observer, zoom to near my targted object (if any)
252 void multi_obs_zoom_to_target()
253 {
254         vector direct;          
255         float dist;     
256         
257         // if i'm not an observer, do nothing
258         if(!(Game_mode & GM_MULTIPLAYER) || (Net_player == NULL) || !(Net_player->flags & NETINFO_FLAG_OBSERVER) || (Player_obj->type != OBJ_OBSERVER)){                
259                 return;
260         }
261
262         // if I have no targeted object, do nothing
263         if(Player_ai->target_objnum == -1){             
264                 return;
265         }
266
267         // get the normalized direction vector between the observer and the targeted object
268         vm_vec_sub(&direct,&Objects[Player_ai->target_objnum].pos,&Player_obj->pos);
269         dist = vm_vec_mag(&direct);
270         vm_vec_normalize(&direct);      
271
272         // orient the guy correctly
273         vm_vec_ang_2_matrix(&Player_obj->orient,&direct,0.0f);
274
275         // keep about 3 object radii away when moving
276         dist -= (Objects[Player_ai->target_objnum].radius * 3.0f);
277
278         // get the movement vector
279         vm_vec_scale(&direct,dist);
280
281         // move
282         vm_vec_add2(&Player_obj->pos,&direct);
283 }
284         
285
286