]> git.mxchange.org Git - flightgear.git/blob - src/MultiPlayer/mpplayer.hxx
Vivian Meazza:
[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 <simgear/props/props.hxx>
48 #include <time.h>
49 #include <sys/time.h>
50
51 #include STL_STRING
52 SG_USING_STD(string);
53
54 // Number of seconds before a player is consider to be lost
55 #define TIME_TO_LIVE 10
56
57 class FGAIMultiplayer;
58
59 class MPPlayer 
60 {
61 public:
62     MPPlayer();
63     ~MPPlayer();
64     /** Enumeration of the states for the player's data */
65     enum PlayerDataState
66     {
67         PLAYER_DATA_NOT_AVAILABLE = 0, 
68         PLAYER_DATA_AVAILABLE, 
69         PLAYER_DATA_EXPIRED
70     };
71     /** Player data state */
72     typedef enum PlayerDataState TPlayerDataState;
73     /** Initialises the class.
74     * @param sIP IP address or host name for sending data to the player
75     * @param sPort Port number for sending data to the player
76     * @param sCallsign Callsign of the player (must be unique across all instances of MPPlayer).
77     * @param sModelName Path and name of the aircraft model file for the player
78     * @param bLocalPlayer True if this player is the local player, else false
79     * @return True if class opens successfully, else false
80     */
81     bool Open(const string &IP, const int &Port, const string &Callsign,
82               const string &ModelName, const bool LocalPlayer);
83     /** Closes the player connection */
84     void Close(void);
85     /** Checks if the time is valid for a position update and perhaps sets the time offset
86      */
87     bool CheckTime(int time, int timeusec);
88     /** Sets the positioning matrix held for this player
89     */
90     void SetPosition(const double lat, const double lon, const double alt,
91                      const double heading, const double roll, const double pitch,
92                      const double speedN, const double speedE, const double speedD,
93                      const double left_aileron, const double right_aileron, const double elevator, const double rudder,
94                      //const double rpms[6],
95                      const double rateH, const double rateR, const double rateP,
96                                          const double accN, const double accE, const double accD);
97     /** Sets a property for this player
98     */
99     void SetProperty(string property, SGPropertyNode::Type type, double val);
100     /** Transform and place model for player
101     */
102     TPlayerDataState Draw(void);
103     /** Returns the callsign for the player
104     * @return Aircraft's callsign
105     */
106     string Callsign(void) const;
107     /** Compares the player's callsign with the given callsign
108     * @param sCallsign Callsign to compare
109     * @return True if callsign matches
110     */
111     bool CompareCallsign(const char *Callsign) const;
112     /** Populates a position message for the player
113     * @param MsgHdr Header to be populated
114     * @param PosMsg Position message to be populated
115     */
116     void FillPosMsg(T_MsgHdr *MsgHdr, T_PositionMsg *PosMsg);
117     /** Populates a mesage header with information for the player
118     * @param MsgHdr Header to be populated
119     * @param iMsgId Message type identifier to insert into header
120     */
121     void FillMsgHdr(T_MsgHdr *MsgHdr, const int iMsgId);
122 private:
123     void    LoadAI(void);       // Loads the plane into the AI core
124     bool    m_Initialised;      // True if object is initialised 
125     
126     double  m_lat;              // location, orientation, etc...
127     double  m_lon;              // ...
128     double  m_alt;              // ...
129     double  m_hdg;              // ...
130     double  m_roll;             // ...
131     double  m_pitch;            // ...
132     double  m_speedN;           // ...
133     double  m_speedE;           // ...
134     double  m_speedD;           // ...
135     double  m_accN;             // ...
136     double  m_accE;             // ...
137     double  m_accD;             // ...
138     double  m_left_aileron;     // ...
139         double  m_right_aileron;     // ...
140     double  m_elevator;         // ...
141     double  m_rudder;           // ...
142     //double  m_rpms[6];          // ...
143     double  m_rateH;            // ...
144     double  m_rateR;            // ...
145     double  m_rateP;            // ...
146         
147     time_t  m_LastUpdate;       // last time update data received
148     int     m_LastTime;         // last seconds according to the packet
149     int     m_LastUTime;        // last microseconds according to the packet
150     double  m_Elapsed;          // Elapsed other-side time between responses
151     double  m_TimeOffset;       // the offset to aim for
152     double  m_LastOffset;       // the last offset we got
153     bool    m_Updated;          // Set when the player data is updated
154     string  m_Callsign;         // players callsign
155     bool    m_LocalPlayer;      // true if player is the local player
156     string  m_ModelName;        // Aircraft model name for player
157     netAddress m_PlayerAddress; // Address information for the player
158     FGAIMultiplayer *m_AIModel; // The AI model of this aircraft
159 };
160
161 #endif
162
163
164