]> git.mxchange.org Git - flightgear.git/blob - src/MultiPlayer/mpplayer.cxx
This is step "1" of probably "many" in the process of separating out the
[flightgear.git] / src / MultiPlayer / mpplayer.cxx
1 // mpplayer.cxx -- routines for a player within a multiplayer Flightgear
2 //
3 // Written by Duncan McCreanor, started February 2003.
4 // duncan.mccreanor@airservicesaustralia.com
5 //
6 // Copyright (C) 2003  Airservices Australia
7 //
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License as
10 // published by the Free Software Foundation; either version 2 of the
11 // License, or (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful, but
14 // WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 // General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 //
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #ifdef FG_MPLAYER_AS
28
29 /******************************************************************
30 * $Id$
31 *
32 * Description: Provides a container for a player in a multiplayer
33 * game. The players network address, model, callsign and positoin
34 * are held. When the player is created and open called, the player's
35 * model is loaded onto the scene. The position transform matrix
36 * is updated by calling SetPosition. When Draw is called the
37 * elapsed time since the last update is checked. If the model
38 * position information has been updated in the last TIME_TO_LIVE
39 * seconds then the model position is updated on the scene.
40 *
41 ******************************************************************/
42
43 #include "mpplayer.hxx"
44
45 #include <stdlib.h>
46 #if !(defined(_MSC_VER) || defined(__MINGW32__))
47 # include <netdb.h>
48 # include <sys/socket.h>
49 # include <netinet/in.h>
50 # include <arpa/inet.h>
51 #endif
52 #include <plib/netSocket.h>
53 #include <plib/sg.h>
54
55 #include <Main/globals.hxx>
56 #include <Model/loader.hxx>
57 #include <Scenery/scenery.hxx>
58
59
60 // These constants are provided so that the ident command can list file versions.
61 const char sMPPLAYER_BID[] = "$Id$";
62 const char sMPPLAYER_HID[] = MPPLAYER_HID;
63
64
65 /******************************************************************
66 * Name: MPPlayer
67 * Description: Constructor.
68 ******************************************************************/
69 MPPlayer::MPPlayer() {
70
71     // Initialise private members
72     m_bInitialised = false;
73     m_LastUpdate = 0;
74     m_PlayerAddress.set("localhost", 0);
75     m_sCallsign = "none";
76
77
78 }
79
80
81 /******************************************************************
82 * Name: ~MPPlayer
83 * Description: Destructor.
84 ******************************************************************/
85 MPPlayer::~MPPlayer() {
86
87     Close();
88
89 }
90
91
92 /******************************************************************
93 * Name: Open
94 * Description: Initialises class.
95 ******************************************************************/
96 bool MPPlayer::Open(const string &sAddress, const int &iPort, const string &sCallsign, const string &sModelName, bool bLocalPlayer) {
97
98     bool bSuccess = true;
99
100     if (!m_bInitialised) {
101
102         m_PlayerAddress.set(sAddress.c_str(), iPort);
103         m_sCallsign = sCallsign;
104         m_sModelName = sModelName;
105         m_bLocalPlayer = bLocalPlayer;
106
107         // If the player is remote then load the model
108         if (!bLocalPlayer) {
109
110              LoadModel();
111
112         }
113
114         m_bInitialised = bSuccess;
115
116     } else {
117         SG_LOG( SG_NETWORK, SG_ALERT, "MPPlayer::Open - Attempt to open an already open player connection." );
118         bSuccess = false;
119     }
120
121
122     /* Return true if open succeeds */
123     return bSuccess;
124
125 }
126
127
128 /******************************************************************
129 * Name: Close
130 * Description: Resets the object.
131 ******************************************************************/
132 void MPPlayer::Close(void) {
133
134     // Remove the model from the game
135     if (m_bInitialised && !m_bLocalPlayer) {
136
137         // Disconnect the model from the transform, then the transform from the scene.
138         m_ModelTrans->removeKid(m_Model);
139         globals->get_scenery()->get_aircraft_branch()->removeKid( m_ModelTrans);
140
141         // Flush the model loader so that it erases the model from its list of
142         // models.
143         globals->get_model_loader()->flush();
144
145         // Assume that plib/ssg deletes the model and transform as their
146         // refcounts should be zero.
147
148     }
149
150     m_bInitialised = false;
151     m_bUpdated = false;
152     m_LastUpdate = 0;
153     m_sCallsign = "none";
154
155 }
156
157
158 /******************************************************************
159 * Name: SetPosition
160 * Description: Updates position data held for this player and resets
161 * the last update time.
162 ******************************************************************/
163 void MPPlayer::SetPosition(const sgMat4 PlayerPosMat4) {
164
165
166     // Save the position matrix and update time
167     if (m_bInitialised) {
168         sgCopyMat4(m_ModelPos, PlayerPosMat4);
169         time(&m_LastUpdate);
170         m_bUpdated = true;
171     }
172
173
174 }
175
176
177 /******************************************************************
178 * Name: Draw
179 * Description: Updates the position for the player's model
180 * The state of the player's data is returned.
181 ******************************************************************/
182 MPPlayer::TPlayerDataState MPPlayer::Draw(void) {
183
184     MPPlayer::TPlayerDataState eResult = PLAYER_DATA_NOT_AVAILABLE;
185
186     sgCoord sgPlayerCoord;
187
188     if (m_bInitialised && !m_bLocalPlayer) {
189         if ((time(NULL) - m_LastUpdate < TIME_TO_LIVE)) {
190             // Peform an update if it has changed since the last update
191             if (m_bUpdated) {
192
193                 // Transform and update player model
194                 sgSetCoord( &sgPlayerCoord, m_ModelPos);
195                 m_ModelTrans->setTransform( &sgPlayerCoord );
196
197                 eResult = PLAYER_DATA_AVAILABLE;
198
199                 // Clear the updated flag so that the position data
200                 // is only available if it has changed
201                 m_bUpdated = false;
202             }
203
204         // Data has not been updated for some time.
205         } else {
206             eResult = PLAYER_DATA_EXPIRED;
207         }
208
209     }
210
211     return eResult;
212
213 }
214
215
216 /******************************************************************
217 * Name: Callsign
218 * Description: Returns the player's callsign.
219 ******************************************************************/
220 string MPPlayer::Callsign(void) const {
221
222     return m_sCallsign;
223
224 }
225
226
227 /******************************************************************
228 * Name: CompareCallsign
229 * Description: Returns true if the player's callsign matches
230 * the given callsign.
231 ******************************************************************/
232 bool MPPlayer::CompareCallsign(const char *sCallsign) const {
233
234     return (m_sCallsign == sCallsign);
235
236 }
237
238
239 /******************************************************************
240 * Name: LoadModel
241 * Description: Loads the player's aircraft model.
242 ******************************************************************/
243 void MPPlayer::LoadModel(void) {
244
245
246     m_ModelTrans = new ssgTransform;
247
248     // Load the model
249     m_Model = globals->get_model_loader()->load_model( globals->get_fg_root(),
250                                                        m_sModelName,
251                                                        globals->get_props(),
252                                                        globals->get_sim_time_sec() );
253     m_Model->clrTraversalMaskBits( SSGTRAV_HOT );
254
255     // Add model to transform
256     m_ModelTrans->addKid( m_Model );
257
258     // Optimise model and transform
259     ssgFlatten( m_Model );
260     ssgStripify( m_ModelTrans );
261
262     // Place on scene under aircraft branch
263     globals->get_scenery()->get_aircraft_branch()->addKid( m_ModelTrans );
264
265
266 }
267
268
269 /******************************************************************
270 * Name: FillPosMsg
271 * Description: Populates the header and data for a position message.
272 ******************************************************************/
273 void MPPlayer::FillPosMsg(T_MsgHdr *MsgHdr, T_PositionMsg *PosMsg) {
274
275     FillMsgHdr(MsgHdr, POS_DATA_ID);
276
277     strncpy(PosMsg->sModel, m_sModelName.c_str(), MAX_MODEL_NAME_LEN);
278     PosMsg->sModel[MAX_MODEL_NAME_LEN - 1] = '\0';
279     sgCopyMat4(PosMsg->PlayerPos, m_ModelPos);
280
281
282 }
283
284
285 /******************************************************************
286 * Name: FillMsgHdr
287 * Description: Populates the header of a multiplayer message.
288 ******************************************************************/
289 void MPPlayer::FillMsgHdr(T_MsgHdr *MsgHdr, const int iMsgId) {
290
291     struct in_addr address;
292
293     MsgHdr->MsgId = iMsgId;
294
295     switch (iMsgId) {
296         case CHAT_MSG_ID:
297             MsgHdr->iMsgLen = sizeof(T_MsgHdr) + sizeof(T_ChatMsg);
298             break;
299         case POS_DATA_ID:
300             MsgHdr->iMsgLen = sizeof(T_MsgHdr) + sizeof(T_PositionMsg);
301             break;
302         default:
303             MsgHdr->iMsgLen = sizeof(T_MsgHdr);
304             break;
305     }
306
307     address.s_addr = inet_addr( m_PlayerAddress.getHost() );
308     MsgHdr->lReplyAddress = address.s_addr;
309
310     MsgHdr->iReplyPort = m_PlayerAddress.getPort();
311
312     strncpy(MsgHdr->sCallsign, m_sCallsign.c_str(), MAX_CALLSIGN_LEN);
313     MsgHdr->sCallsign[MAX_CALLSIGN_LEN - 1] = '\0';
314
315
316 }
317
318 #endif // FG_MPLAYER_AS
319