]> git.mxchange.org Git - flightgear.git/blob - src/Network/multiplay.cxx
ebf511f88554db75b784ee74f36c64724d42efa8
[flightgear.git] / src / Network / multiplay.cxx
1 // multiplay.cxx -- protocol object for multiplay in Flightgear
2 //
3 // Written by Diarmuid Tyson, started February 2003.
4 // diarmuid.tyson@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 #include <simgear/compiler.h>
28
29 #include STL_STRING
30
31 #include <iostream>
32
33 #include <simgear/debug/logstream.hxx>
34 #include <simgear/scene/model/placement.hxx>
35 #include <simgear/scene/model/placementtrans.hxx>
36
37 #include <Scenery/scenery.hxx>
38
39 #include "multiplay.hxx"
40
41 SG_USING_STD(string);
42
43
44 // These constants are provided so that the ident command can list file versions.
45 const char sFG_MULTIPLAY_BID[] = "$Id$";
46 const char sFG_MULTIPLAY_HID[] = FG_MULTIPLAY_HID;
47
48
49 /******************************************************************
50 * Name: FGMultiplay
51 * Description: Constructor.  Initialises the protocol and stores
52 * host and port information.
53 ******************************************************************/
54 FGMultiplay::FGMultiplay (const string &dir, const int rate, const string &host, const int port) {
55
56   set_hz(rate);
57
58   set_direction(dir);
59
60   if (get_direction() == SG_IO_IN) {
61
62     fgSetInt("/sim/multiplay/rxport", port);
63     fgSetString("/sim/multiplay/rxhost", host.c_str());
64
65   } else if (get_direction() == SG_IO_OUT) {
66
67     fgSetInt("/sim/multiplay/txport", port);
68     fgSetString("/sim/multiplay/txhost", host.c_str());
69
70   }
71
72 }
73
74
75 /******************************************************************
76 * Name: ~FGMultiplay
77 * Description: Destructor.
78 ******************************************************************/
79 FGMultiplay::~FGMultiplay () {
80 }
81
82
83 /******************************************************************
84 * Name: open
85 * Description: Enables the protocol.
86 ******************************************************************/
87 bool FGMultiplay::open() {
88
89     if ( is_enabled() ) {
90         SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
91                 << "is already in use, ignoring" );
92         return false;
93     }
94
95     set_enabled(true);
96
97     return is_enabled();
98 }
99
100
101 /******************************************************************
102 * Name: process
103 * Description: Prompts the multiplayer mgr to either send
104 * or receive data over the network
105 ******************************************************************/
106 bool FGMultiplay::process() {
107
108   if (get_direction() == SG_IO_IN) {
109
110     globals->get_multiplayer_mgr()->ProcessData();
111
112   } else if (get_direction() == SG_IO_OUT) {
113
114     sgMat4 posTrans;
115     globals->get_aircraft_model()->get3DModel()->getTransform()->getTransform(posTrans);
116     Point3D center = globals->get_scenery()->get_center();
117     sgdVec3 PlayerPosition;
118     sgdSetVec3(PlayerPosition, posTrans[3][0] + center[0],
119                posTrans[3][1] + center[1], posTrans[3][2] + center[2]);
120     sgQuat PlayerOrientation;
121     sgMatrixToQuat(PlayerOrientation, posTrans);
122
123     globals->get_multiplayer_mgr()->SendMyPosition(PlayerOrientation, PlayerPosition);
124
125   }
126
127     return true;
128 }
129
130
131 /******************************************************************
132 * Name: close
133 * Description:  Closes the multiplayer mgrs to stop any further
134 * network processing
135 ******************************************************************/
136 bool FGMultiplay::close() {
137
138   if (get_direction() == SG_IO_IN) {
139
140     globals->get_multiplayer_mgr()->Close();
141
142   } else if (get_direction() == SG_IO_OUT) {
143
144 //    globals->get_multiplayer_mgr()->Close();
145
146   }
147
148     return true;
149 }
150