]> git.mxchange.org Git - flightgear.git/blob - src/MultiPlayer/mpplayer.hxx
Better cross platform compatibility.
[flightgear.git] / src / MultiPlayer / mpplayer.hxx
1 // mpplayer.hxx -- 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
24 #ifndef MPPLAYER_H
25 #define MPPLAYER_H
26
27 #define MPPLAYER_HID "$Id$"
28
29 /****************************************************************
30 * @version $Id$
31 *
32 * Description: This class holds information about a player in a
33 * multiplayer game. The model for a remote player is loaded and
34 * added onto the aircraft branch of the scene on calling Open.
35 * The model is unloaded from the scene when Close is called
36 * or the object is deleted. The model's positioning transform is
37 * updated by calling SetPosition. The updated position transform
38 * is applied to the model on the scene by calling Draw.
39 *
40 ******************************************************************/
41
42 #include "mpmessages.hxx"
43
44 #include <plib/sg.h>
45 #include <plib/netSocket.h>
46 #include <simgear/io/sg_socket_udp.hxx>
47 #include <time.h>
48
49 #include STL_STRING
50 SG_USING_STD(string);
51
52 // Number of seconds before a player is consider to be lost
53 #define TIME_TO_LIVE 10
54
55
56 class ssgEntity;
57 class ssgPlacementTransform;
58
59
60 class MPPlayer {
61 public:
62
63     /** Constructor */
64     MPPlayer();
65
66     /** Destructor. */
67     ~MPPlayer();
68
69     /** Enumeration of the states for the player's data */
70     enum PlayerDataState {PLAYER_DATA_NOT_AVAILABLE = 0, PLAYER_DATA_AVAILABLE, PLAYER_DATA_EXPIRED};
71
72     /** Player data state */
73     typedef enum PlayerDataState TPlayerDataState;
74
75     /** Initialises the class.
76     * @param sIP IP address or host name for sending data to the player
77     * @param sPort Port number for sending data to the player
78     * @param sCallsign Callsign of the player (must be unique across all instances of MPPlayer).
79     * @param sModelName Path and name of the aircraft model file for the player
80     * @param bLocalPlayer True if this player is the local player, else false
81     * @return True if class opens successfully, else false
82     */
83     bool Open(const string &sIP, const int &iPort, const string &sCallsign,
84               const string &sModelName, const bool bLocalPlayer);
85
86     /** Closes the player connection */
87     void Close(void);
88
89     /** Sets the positioning matrix held for this player
90     * @param PlayerPosMat4 Matrix for positioning player's aircraft
91     */
92     void SetPosition(const sgQuat PlayerOrientation,
93                      const sgdVec3 PlayerPosition);
94
95     /** Transform and place model for player
96     */
97     TPlayerDataState Draw(void);
98
99     /** Returns the callsign for the player
100     * @return Aircraft's callsign
101     */
102     string Callsign(void) const;
103
104     /** Compares the player's callsign with the given callsign
105     * @param sCallsign Callsign to compare
106     * @return True if callsign matches
107     */
108     bool CompareCallsign(const char *sCallsign) const;
109
110     /** Populates a position message for the player
111     * @param MsgHdr Header to be populated
112     * @param PosMsg Position message to be populated
113     */
114     void FillPosMsg(T_MsgHdr *MsgHdr, T_PositionMsg *PosMsg);
115
116     /** Populates a mesage header with information for the player
117     * @param MsgHdr Header to be populated
118     * @param iMsgId Message type identifier to insert into header
119     */
120     void FillMsgHdr(T_MsgHdr *MsgHdr, const int iMsgId);
121
122
123 private:
124
125     /** Loads the model of the aircraft */
126     void LoadModel(void);
127
128     /** True if object is initialised */
129     bool m_bInitialised;
130
131     /** Position of the player's aircraft wrt the earth fixed global system */
132     sgdVec3 m_ModelPosition;
133
134     /** Orientation the player's aircraft wrt the earth fixed global system */
135     sgQuat m_ModelOrientation;
136
137     /** Used to remove player if no activity */
138     time_t m_LastUpdate;
139
140     /** Set when the player data is updated and cleared when read */
141     bool m_bUpdated;
142
143     /** Player's callsign */
144     string m_sCallsign;
145
146     /** Aircraft model name for player */
147     string m_sModelName;
148
149     /** The player's loaded model */
150     ssgEntity *m_Model;
151
152     /** Model transform */
153     ssgPlacementTransform *m_ModelTrans;
154
155     /** True if this player is the local player */
156     bool m_bLocalPlayer;
157
158     /** Address information for the player */
159     netAddress m_PlayerAddress;
160
161 };
162
163 #endif
164
165
166