2 * Copyright (C) Volition, Inc. 1999. All rights reserved.
4 * All source code herein is the property of Volition, Inc. You may not sell
5 * or otherwise commercially exploit the source or things you created based on
10 * $Logfile: /Freespace2/code/Network/multi_observer.cpp $
16 * Revision 1.5 2002/06/17 06:33:10 relnev
17 * ryan's struct patch for gcc 2.95
19 * Revision 1.4 2002/06/09 04:41:23 relnev
20 * added copyright header
22 * Revision 1.3 2002/05/27 00:40:47 theoddone33
23 * Fix net_addr vs net_addr_t
25 * Revision 1.2 2002/05/07 03:16:47 theoddone33
26 * The Great Newline Fix
28 * Revision 1.1.1.1 2002/05/03 03:28:10 root
32 * 6 9/14/99 2:21p Dave
33 * Fixed observer mode joining and ingame stuff.
35 * 5 8/19/99 10:59a Dave
36 * Packet loss detection.
38 * 4 11/17/98 11:12a Dave
39 * Removed player identification by address. Now assign explicit id #'s.
41 * 3 11/05/98 5:55p Dave
42 * Big pass at reducing #includes
44 * 2 10/07/98 10:53a Dave
47 * 1 10/07/98 10:50a Dave
49 * 13 6/13/98 6:01p Hoffoss
50 * Externalized all new (or forgot to be added) strings to all the code.
52 * 12 6/04/98 11:04a Allender
53 * object update level stuff. Don't reset to high when becoming an
54 * observer of any type. default to low when guy is a dialup customer
56 * 11 5/22/98 9:49p Allender
57 * make observers Player_ship hidden from sensors so that he cannot target
60 * 10 5/19/98 11:36p Allender
61 * fixed very nasty mask problem with ingame joiner marking player objects
62 * incorrectly. Named ingame joiner ship and observer ship unique names
64 * 9 4/18/98 5:00p Dave
65 * Put in observer zoom key. Made mission sync screen more informative.
67 * 8 4/03/98 3:13p Dave
68 * More work on udp reliable transport. Fixed observer hud offset problem.
69 * Made observer join and ingame join work again. Put subnet broadcasting
72 * 7 3/30/98 6:27p Dave
73 * Put in a more official set of multiplayer options, including a system
74 * for distributing netplayer and netgame settings.
76 * 6 3/24/98 5:00p Dave
77 * Fixed several ui bugs. Put in pre and post voice stream playback sound
78 * fx. Put in error specific popups for clients getting dropped from games
79 * through actions other than their own.
81 * 5 3/23/98 5:42p Dave
82 * Put in automatic xfer of pilot pic files. Changed multi_xfer system so
83 * that it can support multiplayer sends/received between client and
84 * server simultaneously.
86 * 4 3/15/98 4:17p Dave
87 * Fixed oberver hud problems. Put in handy netplayer macros. Reduced size
88 * of network orientation matrices.
90 * 3 3/14/98 2:48p Dave
91 * Cleaned up observer joining code. Put in support for file xfers to
92 * ingame joiners (observers or not). Revamped and reinstalled pseudo
95 * 2 3/13/98 2:51p Dave
96 * Put in support for observers to join ingame.
98 * 1 3/12/98 5:44p Dave
103 #include "freespace.h"
105 #include "multiutil.h"
107 #include "observer.h"
108 #include "hudconfig.h"
109 #include "hudobserver.h"
110 #include "managepilot.h"
111 #include "multi_observer.h"
112 #include "missionparse.h"
115 // ---------------------------------------------------------------------------------------
116 // MULTI OBSERVER DEFINES/VARS
120 // ---------------------------------------------------------------------------------------
121 // MULTI OBSERVER FUNCTIONS
124 // create a _permanent_ observer player
125 int multi_obs_create_player(int player_num,char *name,net_addr_t *addr,player *pl)
127 // blast the player struct
128 memset(&Net_players[player_num],0,sizeof(net_player));
130 // Net_players[player_num].flags |= (NETINFO_FLAG_CONNECTED | NETINFO_FLAG_OBSERVER);
131 // DOH!!! The lack of this caused many bugs.
132 Net_players[player_num].flags = (NETINFO_FLAG_DO_NETWORKING | NETINFO_FLAG_OBSERVER);
133 // memcpy(&Net_players[player_num].p_info.addr, addr, sizeof(net_addr));
134 Net_players[player_num].player = pl;
136 // 6/3/98 -- don't set observer to update high...let it be whatever player set it at.
137 //Net_players[player_num].p_info.options.obj_update_level = OBJ_UPDATE_HIGH;
138 // set up the net_player structure
139 memset(pl, 0, sizeof(player));
140 stuff_netplayer_info( &Net_players[player_num], addr, 0, pl );
141 Net_players[player_num].last_heard_time = timer_get_fixed_seconds();
142 Net_players[player_num].reliable_socket = INVALID_SOCKET;
143 Net_players[player_num].s_info.kick_timestamp = -1;
144 Net_players[player_num].s_info.voice_token_timestamp = -1;
145 Net_players[player_num].s_info.tracker_security_last = -1;
146 Net_players[player_num].s_info.target_objnum = -1;
147 Net_players[player_num].s_info.accum_buttons = 0;
149 // reset the ping for this player
150 multi_ping_reset(&Net_players[player_num].s_info.ping);
152 // timestamp his last_full_update_time
153 Net_players[player_num].s_info.last_full_update_time = timestamp(0);
154 Net_players[player_num].player->objnum = -1;
156 // nil his file xfer handle
157 Net_players[player_num].s_info.xfer_handle = -1;
159 // zero out his object update and control info sequencing data
160 Net_players[player_num].client_cinfo_seq = 0;
161 Net_players[player_num].client_server_seq = 0;
163 // his kick timestamp
164 Net_players[player_num].s_info.kick_timestamp = -1;
166 // nil his data rate timestamp stuff
167 Net_players[player_num].s_info.rate_stamp = -1;
168 Net_players[player_num].s_info.rate_bytes = 0;
170 // nil packet buffer stuff
171 Net_players[player_num].s_info.unreliable_buffer_size = 0;
172 Net_players[player_num].s_info.reliable_buffer_size = 0;
174 // callsign and short callsign
175 SDL_strlcpy(pl->callsign, name, SDL_arraysize(pl->callsign));
176 pilot_set_short_callsign(pl,SHORT_CALLSIGN_PIXEL_W);
177 pl->flags |= PLAYER_FLAGS_STRUCTURE_IN_USE;
179 Net_players[player_num].sv_bytes_sent = 0;
180 Net_players[player_num].sv_last_pl = -1;
181 Net_players[player_num].cl_bytes_recvd = 0;
182 Net_players[player_num].cl_last_pl = -1;
187 // create an explicit observer object and assign it to the passed player
188 void multi_obs_create_observer(net_player *pl)
192 // create the basic observer object
193 objnum = observer_create( &vmd_identity_matrix, &vmd_zero_vector);
194 SDL_assert(objnum != -1);
195 Objects[objnum].flags |= OF_PLAYER_SHIP;
196 Objects[objnum].net_signature = 0;
199 Objects[objnum].pos.xyz.x = 1.0f;
200 Objects[objnum].pos.xyz.y = 1.0f;
201 Objects[objnum].pos.xyz.z = 1.0f;
203 // assign this object to the player
204 pl->player->objnum = objnum;
207 // create observer object locally, and additionally, setup some other information
208 // ( client-side equivalent of multi_obs_create_observer() )
209 void multi_obs_create_observer_client()
213 SDL_assert(!(Net_player->flags & NETINFO_FLAG_OBS_PLAYER));
215 // make me an observer object
216 multi_obs_create_observer(Net_player);
218 // set my object to be the observer object
219 Player_obj = &Objects[Net_player->player->objnum];
221 // create the default player ship object and use that as my default virtual "ship", and make it "invisible"
222 pobj_num = parse_create_object(&Player_start_pobject);
223 SDL_assert(pobj_num != -1);
224 obj_set_flags(&Objects[pobj_num],OF_PLAYER_SHIP);
225 Player_ship = &Ships[Objects[pobj_num].instance];
227 // make ship hidden from sensors so that this observer cannot target it. Observers really have two ships
228 // one observer, and one "Player_ship". Observer needs to ignore the Player_ship.
229 Player_ship->flags |= SF_HIDDEN_FROM_SENSORS;
230 SDL_strlcpy(Player_ship->ship_name, XSTR("Observer Ship",688), SDL_arraysize(Player_ship->ship_name));
231 Player_ai = &Ai_info[Ships[Objects[pobj_num].instance].ai_index];
233 // configure the hud to be in "observer" mode
234 hud_config_as_observer(Player_ship,Player_ai);
236 // set some flags for myself
237 Net_player->flags |= NETINFO_FLAG_OBSERVER;
239 // reset the control info structure
240 memset(&Player->ci,0,sizeof(control_info));
243 // create objects for all known observers in the game at level start
244 // call this before entering a mission
245 // this implies for the local player in the case of a client or for _all_ players in the case of a server
246 void multi_obs_level_init()
250 // unset the OBS_PLAYER flag here for all net players
251 for(idx=0;idx<MAX_PLAYERS;idx++){
252 Net_players[idx].flags &= ~(NETINFO_FLAG_OBS_PLAYER);
255 // if i'm a client and I'm an observer, create an object for myself
256 if(!(Net_player->flags & NETINFO_FLAG_AM_MASTER) && (Net_player->flags & NETINFO_FLAG_OBSERVER)){
257 // create my own observer object and setup other misc. data
258 multi_obs_create_observer_client();
260 // otherwise create stuff for all (permanent) observers in the game
262 for(idx=0;idx<MAX_PLAYERS;idx++){
263 if(MULTI_CONNECTED(Net_players[idx]) && MULTI_OBSERVER(Net_players[idx])){
264 // make an observer object for the guy
265 multi_obs_create_observer(&Net_players[idx]);
271 // if i'm an observer, zoom to near my targted object (if any)
272 void multi_obs_zoom_to_target()
277 // if i'm not an observer, do nothing
278 if(!(Game_mode & GM_MULTIPLAYER) || (Net_player == NULL) || !(Net_player->flags & NETINFO_FLAG_OBSERVER) || (Player_obj->type != OBJ_OBSERVER)){
282 // if I have no targeted object, do nothing
283 if(Player_ai->target_objnum == -1){
287 // get the normalized direction vector between the observer and the targeted object
288 vm_vec_sub(&direct,&Objects[Player_ai->target_objnum].pos,&Player_obj->pos);
289 dist = vm_vec_mag(&direct);
290 vm_vec_normalize(&direct);
292 // orient the guy correctly
293 vm_vec_ang_2_matrix(&Player_obj->orient,&direct,0.0f);
295 // keep about 3 object radii away when moving
296 dist -= (Objects[Player_ai->target_objnum].radius * 3.0f);
298 // get the movement vector
299 vm_vec_scale(&direct,dist);
302 vm_vec_add2(&Player_obj->pos,&direct);