1 // mpplayer.cxx -- routines for a player within a multiplayer Flightgear
3 // Written by Duncan McCreanor, started February 2003.
4 // duncan.mccreanor@airservicesaustralia.com
6 // Copyright (C) 2003 Airservices Australia
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.
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.
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.
24 /******************************************************************
27 * Description: Provides a container for a player in a multiplayer
28 * game. The players network address, model, callsign and positoin
29 * are held. When the player is created and open called, the player's
30 * model is loaded onto the scene. The position transform matrix
31 * is updated by calling SetPosition. When Draw is called the
32 * elapsed time since the last update is checked. If the model
33 * position information has been updated in the last TIME_TO_LIVE
34 * seconds then the model position is updated on the scene.
36 ******************************************************************/
38 #include "mpplayer.hxx"
43 # include <sys/socket.h>
44 # include <netinet/in.h>
45 # include <arpa/inet.h>
47 #include <plib/netSocket.h>
49 #include <Main/globals.hxx>
50 #include <Model/loader.hxx>
51 #include <Scenery/scenery.hxx>
54 // These constants are provided so that the ident command can list file versions.
55 const char sMPPLAYER_BID[] = "$Id$";
56 const char sMPPLAYER_HID[] = MPPLAYER_HID;
59 /******************************************************************
61 * Description: Constructor.
62 ******************************************************************/
63 MPPlayer::MPPlayer() {
65 // Initialise private members
66 m_bInitialised = false;
68 m_PlayerAddress.set("localhost", 0);
74 /******************************************************************
76 * Description: Destructor.
77 ******************************************************************/
78 MPPlayer::~MPPlayer() {
85 /******************************************************************
87 * Description: Initialises class.
88 ******************************************************************/
89 bool MPPlayer::Open(const string &sAddress, const int &iPort, const string &sCallsign, const string &sModelName, bool bLocalPlayer) {
93 if (!m_bInitialised) {
95 m_PlayerAddress.set(sAddress.c_str(), iPort);
96 m_sCallsign = sCallsign;
97 m_sModelName = sModelName;
98 m_bLocalPlayer = bLocalPlayer;
100 // If the player is remote then load the model
107 m_bInitialised = bSuccess;
110 cerr << "MPPlayer::Open - Attempt to open an already open player connection." << endl;
115 /* Return true if open succeeds */
121 /******************************************************************
123 * Description: Resets the object.
124 ******************************************************************/
125 void MPPlayer::Close(void) {
128 // Remove the model from the game
129 if (!m_bLocalPlayer) {
130 globals->get_scenery()->get_scene_graph()->removeKid(m_ModelSel);
133 m_bInitialised = false;
140 /******************************************************************
142 * Description: Updates position data held for this player and resets
143 * the last update time.
144 ******************************************************************/
145 void MPPlayer::SetPosition(const sgMat4 PlayerPosMat4) {
148 // Save the position matrix and update time
149 if (m_bInitialised) {
150 memcpy(m_ModelPos, PlayerPosMat4, sizeof(sgMat4));
159 /******************************************************************
161 * Description: Updates the position for the player's model
162 * The state of the player (old, initialised etc)
164 ******************************************************************/
165 int MPPlayer::Draw(void) {
167 int iResult = PLAYER_DATA_NOT_AVAILABLE;
169 sgCoord sgPlayerCoord;
171 if (m_bInitialised) {
172 if ((time(NULL) - m_LastUpdate < TIME_TO_LIVE)) {
173 // Peform an update if it has changed since the last update
176 // Transform and update player model
177 m_ModelSel->select(1);
178 sgSetCoord( &sgPlayerCoord, m_ModelPos);
179 m_ModelTrans->setTransform( &sgPlayerCoord );
181 iResult = PLAYER_DATA_AVAILABLE;
183 // Clear the updated flag so that the position data
184 // is only available if it has changed
188 // Data has not been updated for some time.
190 iResult = PLAYER_DATA_EXPIRED;
200 /******************************************************************
202 * Description: Returns the player's callsign.
203 ******************************************************************/
204 string MPPlayer::Callsign(void) const {
211 /******************************************************************
212 * Name: CompareCallsign
213 * Description: Returns true if the player's callsign matches
214 * the given callsign.
215 ******************************************************************/
216 bool MPPlayer::CompareCallsign(const char *sCallsign) const {
218 return (m_sCallsign == sCallsign);
223 /******************************************************************
225 * Description: Loads the player's aircraft model.
226 ******************************************************************/
227 void MPPlayer::LoadModel(void) {
230 m_ModelSel = new ssgSelector;
231 m_ModelTrans = new ssgTransform;
233 ssgEntity *Model = globals->get_model_loader()->load_model(m_sModelName);
234 Model->clrTraversalMaskBits( SSGTRAV_HOT );
235 m_ModelTrans->addKid( Model );
236 m_ModelSel->addKid( m_ModelTrans );
238 ssgStripify( m_ModelSel );
240 globals->get_scenery()->get_scene_graph()->addKid( m_ModelSel );
241 globals->get_scenery()->get_scene_graph()->addKid( Model );
247 /******************************************************************
249 * Description: Populates the header and data for a position message.
250 ******************************************************************/
251 void MPPlayer::FillPosMsg(T_MsgHdr *MsgHdr, T_PositionMsg *PosMsg) {
253 FillMsgHdr(MsgHdr, POS_DATA_ID);
255 strncpy(PosMsg->sModel, m_sModelName.c_str(), MAX_MODEL_NAME_LEN);
256 PosMsg->sModel[MAX_MODEL_NAME_LEN - 1] = '\0';
258 memcpy(PosMsg->PlayerPos, m_ModelPos, sizeof(sgMat4));
264 /******************************************************************
266 * Description: Populates the header of a multiplayer message.
267 ******************************************************************/
268 void MPPlayer::FillMsgHdr(T_MsgHdr *MsgHdr, const int iMsgId) {
270 struct in_addr address;
272 MsgHdr->MsgId = iMsgId;
276 MsgHdr->iMsgLen = sizeof(T_MsgHdr) + sizeof(T_ChatMsg);
279 MsgHdr->iMsgLen = sizeof(T_MsgHdr) + sizeof(T_PositionMsg);
282 MsgHdr->iMsgLen = sizeof(T_MsgHdr);
286 address.s_addr = inet_addr( m_PlayerAddress.getHost() );
287 MsgHdr->lReplyAddress = address.s_addr;
289 MsgHdr->iReplyPort = m_PlayerAddress.getPort();
291 strncpy(MsgHdr->sCallsign, m_sCallsign.c_str(), MAX_CALLSIGN_LEN);
292 MsgHdr->sCallsign[MAX_CALLSIGN_LEN - 1] = '\0';