Starshatter_Open
Open source Starshatter engine
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
NetPacket.cpp
Go to the documentation of this file.
1 /* Project Starshatter 4.5
2  Destroyer Studios LLC
3  Copyright © 1997-2004. All Rights Reserved.
4 
5  SUBSYSTEM: Stars.exe
6  FILE: NetGame.cpp
7  AUTHOR: John DiCamillo
8 
9 
10  OVERVIEW
11  ========
12  Network Game Manager and Player classes
13 */
14 
15 #include "MemDebug.h"
16 #include "NetPacket.h"
17 #include "NetLink.h"
18 #include "NetMsg.h"
19 #include "Ship.h"
20 
21 #include "Game.h"
22 
23 // +--------------------------------------------------------------------+
24 
25 const int PING_SIZE = 8;
26 const int SHIP_LOC_SIZE = 36;
27 const int JOIN_REQ_SIZE = 116;
28 const int JOIN_ANN_SIZE = 116;
29 const int QUIT_ANN_SIZE = 8;
30 
31 // +--------------------------------------------------------------------+
32 
34 : msg(g)
35 { }
36 
37 NetPacket::NetPacket(DWORD netid, BYTE type)
38 {
39  int len = 0;
40  char buf[256];
41  ZeroMemory(buf, 256);
42 
43  switch (type) {
44  case NET_PING: len = PING_SIZE; break;
45  case NET_PONG: len = PING_SIZE; break;
46  case NET_OBJ_LOC: len = SHIP_LOC_SIZE; break;
47 
48  case NET_JOIN_REQUEST: len = JOIN_REQ_SIZE; break;
49  case NET_JOIN_ANNOUNCE: len = JOIN_ANN_SIZE; break;
50  case NET_QUIT_ANNOUNCE: len = QUIT_ANN_SIZE; break;
51 
52  default: len = JOIN_REQ_SIZE; break;
53  }
54 
55  msg = new(__FILE__,__LINE__) NetMsg(netid, type, buf, len);
56 }
57 
59 {
60  delete msg;
61 }
62 
63 bool
65 {
66  bool sent = false;
67 
68  if (msg)
69  sent = link.SendMessage(msg);
70 
71  msg = 0;
72  return sent;
73 }
74 
75 // +--------------------------------------------------------------------+
76 
77 DWORD
79 {
80  if (msg)
81  return msg->NetID();
82 
83  return 0;
84 }
85 
86 BYTE
88 {
89  if (msg)
90  return msg->Type();
91 
92  return 0;
93 }
94 
95 // +--------------------------------------------------------------------+
96 
97 DWORD
99 {
100  if (msg && msg->Length() >= PING_SIZE) {
101  DWORD* data = (DWORD*) (msg->Data()+4);
102  return *data;
103  }
104 
105  return 0;
106 }
107 
108 void
110 {
111  if (msg && msg->Length() >= PING_SIZE) {
112  DWORD* data = (DWORD*) (msg->Data()+4);
113  *data = seq;
114  }
115 }
116 
117 // +--------------------------------------------------------------------+
118 
119 DWORD
121 {
122  if (msg && msg->Length() >= PING_SIZE) {
123  DWORD* data = (DWORD*) (msg->Data()+4);
124  return *data;
125  }
126 
127  return 0;
128 }
129 
130 void
132 {
133  if (msg && msg->Length() >= PING_SIZE) {
134  DWORD* data = (DWORD*) (msg->Data()+4);
135  *data = id;
136  }
137 }
138 
139 // +--------------------------------------------------------------------+
140 
141 Point
143 {
144  if (msg && msg->Length() >= SHIP_LOC_SIZE) {
145  long* data = (long*) (msg->Data()+8);
146  long x = *(data + 0);
147  long y = *(data + 1);
148  long z = *(data + 2);
149 
150  return Point(x/100.0, y/100.0, z/100.0);
151  }
152 
153  return Point();
154 }
155 
156 void
158 {
159  if (msg && msg->Length() >= SHIP_LOC_SIZE) {
160  long x = (long) (loc.x * 100);
161  long y = (long) (loc.y * 100);
162  long z = (long) (loc.z * 100);
163 
164  long* data = (long*) (msg->Data()+8);
165 
166  *(data + 0) = x;
167  *(data + 1) = y;
168  *(data + 2) = z;
169  }
170 }
171 
172 // +--------------------------------------------------------------------+
173 
174 Point
176 {
177  if (msg && msg->Length() >= SHIP_LOC_SIZE) {
178  short* data = (short*) (msg->Data()+20);
179 
180  short dx = *(data + 0);
181  short dy = *(data + 1);
182  short dz = *(data + 2);
183 
184  return Point(dx, dy, dz);
185  }
186 
187  return Point();
188 }
189 
190 void
192 {
193  if (msg && msg->Length() >= SHIP_LOC_SIZE) {
194  short* data = (short*) (msg->Data()+20);
195 
196  *(data + 0) = (short) vel.x;
197  *(data + 1) = (short) vel.y;
198  *(data + 2) = (short) vel.z;
199  }
200 }
201 
202 // +--------------------------------------------------------------------+
203 
204 Point
206 {
207  if (msg && msg->Length() >= SHIP_LOC_SIZE) {
208  short* data = (short*) (msg->Data()+26);
209 
210  short r = *(data + 0);
211  short p = *(data + 1);
212  short y = *(data + 2);
213 
214  return Point(2*PI*r/32767, 2*PI*p/32767, 2*PI*y/32767);
215  }
216 
217  return Point();
218 }
219 
220 void
222 {
223  if (msg && msg->Length() >= SHIP_LOC_SIZE) {
224  short* data = (short*) (msg->Data()+26);
225 
226  *(data + 0) = (short) (32767*rpy.x/(2*PI));
227  *(data + 1) = (short) (32767*rpy.y/(2*PI));
228  *(data + 2) = (short) (32767*rpy.z/(2*PI));
229  }
230 }
231 
232 // +--------------------------------------------------------------------+
233 
234 double
236 {
237  if (msg && msg->Length() >= SHIP_LOC_SIZE) {
238  BYTE* data = (BYTE*) msg->Data()+32;
239 
240  return (double) *data;
241  }
242 
243  return 0;
244 }
245 
246 void
248 {
249  if (msg && msg->Length() >= SHIP_LOC_SIZE) {
250  BYTE* data = (BYTE*) msg->Data()+32;
251 
252  *data = (BYTE) t;
253  }
254 }
255 
256 // +--------------------------------------------------------------------+
257 
258 bool
260 {
261  if (i >= 0 && i < 8 && msg && msg->Length() >= SHIP_LOC_SIZE) {
262  BYTE* data = (BYTE*) msg->Data()+33;
263 
264  BYTE select = 1 << i;
265  return (*data & select)?true:false;
266  }
267 
268  return false;
269 }
270 
271 void
272 NetPacket::SetTrigger(int i, bool trigger)
273 {
274  if (i >= 0 && i < 8 && msg && msg->Length() >= SHIP_LOC_SIZE) {
275  BYTE* data = (BYTE*) msg->Data()+33;
276 
277  BYTE select = 1 << i;
278 
279  if (trigger)
280  *data = *data | select;
281  else
282  *data = *data & ~select;
283  }
284 }
285 
286 // +--------------------------------------------------------------------+
287 
288 const char*
290 {
291  if (msg && msg->Length() >= JOIN_REQ_SIZE) {
292  BYTE* data = (BYTE*) msg->Data()+20;
293 
294  return (const char*) data;
295  }
296 
297  return 0;
298 }
299 
300 void
301 NetPacket::SetName(const char* name)
302 {
303  if (msg && msg->Length() >= JOIN_REQ_SIZE) {
304  BYTE* data = (BYTE*) msg->Data()+20;
305  strncpy((char*) data, name, 32);
306  }
307 }
308 
309 // +--------------------------------------------------------------------+
310 
311 const char*
313 {
314  if (msg && msg->Length() >= JOIN_REQ_SIZE) {
315  BYTE* data = (BYTE*) msg->Data()+52;
316 
317  return (const char*) data;
318  }
319 
320  return 0;
321 }
322 
323 void
324 NetPacket::SetDesign(const char* design)
325 {
326  if (msg && msg->Length() >= JOIN_REQ_SIZE) {
327  BYTE* data = (BYTE*) msg->Data()+52;
328  strncpy((char*) data, design, 32);
329  }
330 }
331 
332 // +--------------------------------------------------------------------+
333 
334 const char*
336 {
337  if (msg && msg->Length() >= JOIN_REQ_SIZE) {
338  BYTE* data = (BYTE*) msg->Data()+84;
339 
340  return (const char*) data;
341  }
342 
343  return 0;
344 }
345 
346 void
347 NetPacket::SetRegion(const char* rgn_name)
348 {
349  if (msg && msg->Length() >= JOIN_REQ_SIZE) {
350  BYTE* data = (BYTE*) msg->Data()+84;
351  strncpy((char*) data, rgn_name, 32);
352  }
353 }
354 
355 // +--------------------------------------------------------------------+
356