Debian Codegraph @

engine/include/network.h

raw
1 // ____ _ __
2 // / __ )____ _____ | | / /___ ___________
3 // / __ / __ \/ ___/ | | /| / / __ `/ ___/ ___/
4 // / /_/ / /_/ (__ ) | |/ |/ / /_/ / / (__ )
5 // /_____/\____/____/ |__/|__/\__,_/_/ /____/
6 //
7 // A futuristic real-time strategy game.
8 // This file is part of Bos Wars.
9 //
10 /**@name network.h - The network header file. */
11 //
12 // (c) Copyright 1998-2007 by Lutz Sammer, Russell Smith, and Jimmy Salmon
13 //
14 // This program is free software; you can redistribute it and/or modify
15 // it under the terms of the GNU General Public License as published by
16 // the Free Software Foundation; only version 2 of the License.
17 //
18 // This program is distributed in the hope that it will be useful,
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 // GNU General Public License for more details.
22 //
23 // You should have received a copy of the GNU General Public License
24 // along with this program; if not, write to the Free Software
25 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
26 // 02111-1307, USA.
27
28 #ifndef __NETWORK_H__
29 #define __NETWORK_H__
30
31 //@{
32
33 /*----------------------------------------------------------------------------
34 -- Includes
35 ----------------------------------------------------------------------------*/
36
37 #include "unit.h"
38 #include "net_lowlevel.h"
39
40 /*----------------------------------------------------------------------------
41 -- Defines
42 ----------------------------------------------------------------------------*/
43
44 #define MaxNetworkCommands 9 /// Max Commands In A Packet
45
46 #define IsNetworkGame() (NetworkFildes != (Socket)-1)
47
48 /*----------------------------------------------------------------------------
49 -- Declarations
50 ----------------------------------------------------------------------------*/
51
52 class CUnit;
53 class CUnitType;
54
55 /**
56 ** Network message types.
57 **
58 ** @todo cleanup the message types.
59 */
60 enum _message_type_ {
61 MessageNone, /// When Nothing Is Happening
62 MessageInitHello, /// Start connection
63 MessageInitReply, /// Connection reply
64 MessageInitConfig, /// Setup message configure clients
65
66 MessageSync, /// Heart beat
67 MessageSelection, /// Update a Selection from Team Player
68 MessageQuit, /// Quit game
69 MessageQuitAck, /// Quit reply - UNUSED YET - Protocol Version 2 - Reserved for menus
70 MessageResend, /// Resend message
71
72 MessageChat, /// Chat message
73 MessageChatTerm, /// Chat message termination - Protocol Version 2
74
75 MessageCommandStop, /// Unit command stop
76 MessageCommandStand, /// Unit command stand ground
77 MessageCommandFollow, /// Unit command follow
78 MessageCommandMove, /// Unit command move
79 MessageCommandRepair, /// Unit command repair
80 MessageCommandAutoRepair, /// Unit command autorepair
81 MessageCommandAttack, /// Unit command attack
82 MessageCommandGround, /// Unit command attack ground
83 MessageCommandPatrol, /// Unit command patrol
84 MessageCommandBoard, /// Unit command borad
85 MessageCommandUnload, /// Unit command unload
86 MessageCommandBuild, /// Unit command build building
87 MessageCommandDismiss, /// Unit command dismiss unit
88 MessageCommandResource, /// Unit command resource
89 MessageCommandReturn, /// Unit command return goods
90 MessageCommandTrain, /// Unit command train
91 MessageCommandCancelTrain, /// Unit command cancel training
92
93 MessageExtendedCommand, /// Command is the next byte
94
95 // ATTN: __MUST__ be last due to spellid encoding!!!
96 MessageCommandSpellCast /// Unit command spell cast
97 };
98
99 /**
100 ** Network extended message types.
101 */
102 enum _extended_message_type_ {
103 ExtendedMessageDiplomacy, /// Change diplomacy
104 ExtendedMessageSharedVision, /// Change shared vision
105 };
106
107 /**
108 ** Network command message.
109 */
110 class CNetworkCommand {
111 public:
112 CNetworkCommand() : Unit(0), X(0), Y(0), Dest(0) {}
113 void Clear() { this->Unit = this->X = this->Y = this->Dest = 0; }
114
115 void Serialize(unsigned char *p) const;
116 void Deserialize(unsigned char *p);
117 static size_t Size() { return 2+2+2+2; }
118
119 Uint16 Unit; /// Command for unit
120 Uint16 X; /// Map position X
121 Uint16 Y; /// Map position Y
122 Uint16 Dest; /// Destination unit
123 };
124
125 /**
126 ** Extended network command message.
127 */
128 class CNetworkExtendedCommand {
129 public:
130 CNetworkExtendedCommand() : ExtendedType(0), Arg1(0), Arg2(0), Arg3(0), Arg4(0) {}
131
132 void Serialize(unsigned char *p);
133 void Deserialize(unsigned char *p);
134 static size_t Size() { return 1+1+2+2+2; }
135
136 Uint8 ExtendedType; /// Extended network command type
137 Uint8 Arg1; /// Argument 1
138 Uint16 Arg2; /// Argument 2
139 Uint16 Arg3; /// Argument 3
140 Uint16 Arg4; /// Argument 4
141 };
142
143 /**
144 ** Network chat message.
145 */
146 class CNetworkChat {
147 public:
148 CNetworkChat() {
149 Player = 0;
150 memset(Text, 0, sizeof(Text));
151 }
152
153 void Serialize(unsigned char *p);
154 void Deserialize(unsigned char *p);
155 static size_t Size() { return 1+7; }
156
157 Uint8 Player; /// Sending player
158 char Text[7]; /// Message bytes
159 };
160
161 /**
162 ** Network Selection Info
163 */
164 typedef struct _network_selection_header_ {
165 unsigned NumberSent : 6; /// New Number Selected
166 unsigned Add : 1; /// Adding to Selection
167 unsigned Remove : 1; /// Removing from Selection
168 unsigned char Type[MaxNetworkCommands]; /// Command
169 } NetworkSelectionHeader;
170
171 /**
172 ** Network Selection Update
173 */
174 class CNetworkSelection {
175 public:
176 CNetworkSelection() {
177 memset(Unit, 0, sizeof(Unit));
178 }
179
180 void Serialize(unsigned char *p);
181 void Deserialize(unsigned char *p);
182 static size_t Size() { return 2*4; }
183
184 Uint16 Unit[4]; /// Selection Units
185 };
186
187 /**
188 ** Network packet header.
189 **
190 ** Header for the packet.
191 */
192 class CNetworkPacketHeader {
193 public:
194 CNetworkPacketHeader() {
195 Cycle = 0;
196 memset(Type, 0, sizeof(Type));
197 }
198
199 void Serialize(unsigned char *p) const;
200 void Deserialize(unsigned char *p);
201 static size_t Size() { return 1 + 1 * MaxNetworkCommands; }
202
203 Uint8 Cycle; /// Destination game cycle
204 Uint8 Type[MaxNetworkCommands]; /// Commands in packet
205 };
206
207 /**
208 ** Network packet.
209 **
210 ** This is sent over the network.
211 */
212 class CNetworkPacket {
213 public:
214 unsigned char *Serialize(int numcommands) const;
215 int Deserialize(unsigned char *p, unsigned int len);
216 static size_t Size(int numcommands) {
217 return CNetworkPacketHeader::Size() + numcommands * CNetworkCommand::Size();
218 }
219
220 CNetworkPacketHeader Header; /// Packet Header Info
221 CNetworkCommand Command[MaxNetworkCommands];
222 };
223
224 /*----------------------------------------------------------------------------
225 -- Variables
226 ----------------------------------------------------------------------------*/
227
228 extern int NetworkNumInterfaces; /// Network number of interfaces
229 extern Socket NetworkFildes; /// Network file descriptor
230 extern int NetworkInSync; /// Network is in sync
231 extern int NetworkUpdates; /// Network update each # game cycles
232 extern int NetworkLag; /// Network lag (# game cycles)
233 extern unsigned long NetworkStatus[PlayerMax]; /// Network status
234 extern int NoRandomPlacementMultiplayer; /// Removes randomization of player placements
235
236 /*----------------------------------------------------------------------------
237 -- Functions
238 ----------------------------------------------------------------------------*/
239
240 extern void InitNetwork1(void); /// Initialise network part 1 (ports)
241 extern void InitNetwork2(void); /// Initialise network part 2
242 extern void ExitNetwork1(void); /// Cleanup network part 1 (ports)
243 extern void NetworkEvent(void); /// Handle network events
244 extern void NetworkSync(void); /// Hold in sync
245 extern void NetworkQuit(void); /// Quit game
246 extern void NetworkRecover(void); /// Recover network
247 extern void NetworkCommands(void); /// Get all network commands
248 extern void NetworkChatMessage(const std::string &msg); /// Send chat message
249 /// Send network command.
250 extern void NetworkSendCommand(int command, const CUnit *unit, int x,
251 int y, const CUnit *dest, const CUnitType *type, int status);
252 /// Send extended network command.
253 extern void NetworkSendExtendedCommand(int command, int arg1, int arg2,
254 int arg3, int arg4, int status);
255 /// Send Selections to Team
256 extern void NetworkSendSelection(CUnit **units, int count);
257
258 //@}
259
260 #endif // !__NETWORK_H__