Starshatter_Open
Open source Starshatter engine
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
NetUtil.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: NetUtil.cpp
7  AUTHOR: John DiCamillo
8 
9 
10  OVERVIEW
11  ========
12  Utility class to simplify sending NetData messages.
13 */
14 
15 
16 #include "MemDebug.h"
17 #include "NetUtil.h"
18 #include "NetData.h"
19 #include "NetGame.h"
20 #include "NetGameServer.h"
21 
22 #include "Element.h"
23 #include "Instruction.h"
24 #include "Random.h"
25 #include "Ship.h"
26 #include "Shot.h"
27 #include "System.h"
28 #include "Weapon.h"
29 
30 // +-------------------------------------------------------------------+
31 
32 void
33 NetUtil::SendObjDamage(SimObject* obj, double dmg, Shot* shot)
34 {
35  NetGame* net_game = NetGame::GetInstance();
36  if (!net_game || !obj) return;
37 
38  if (net_game->IsServer() && obj->GetObjID()) {
39  NetObjDamage damage;
40  damage.SetObjID(obj->GetObjID());
41  damage.SetDamage((float) dmg);
42 
43  if (shot)
44  damage.SetShotID(shot->GetObjID());
45 
46  net_game->SendData(&damage);
47  }
48 }
49 
50 // +-------------------------------------------------------------------+
51 
52 void
53 NetUtil::SendSysDamage(Ship* obj, System* sys, double dmg)
54 {
55  NetGame* net_game = NetGame::GetInstance();
56  if (!net_game || !obj || !sys) return;
57 
58  if (net_game->IsServer() && obj->GetObjID()) {
59  NetSysDamage damage;
60  damage.SetObjID(obj->GetObjID());
61  damage.SetDamage(dmg);
62  damage.SetSystem(sys->GetID());
63 
64  net_game->SendData(&damage);
65  }
66 }
67 
68 // +-------------------------------------------------------------------+
69 
70 void
72 {
73  NetGame* net_game = NetGame::GetInstance();
74  if (!net_game || !obj || !sys) return;
75 
76  if (obj->GetObjID()) {
77  NetSysStatus status;
78  status.SetObjID(obj->GetObjID());
79  status.SetSystem( (int) sys->GetID());
80  status.SetStatus( (int) sys->Status());
81  status.SetPower( (int) sys->GetPowerLevel());
82  status.SetReactor((int) sys->GetSourceIndex());
83  status.SetAvailablility(sys->Availability());
84 
85  net_game->SendData(&status);
86  }
87 }
88 
89 // +-------------------------------------------------------------------+
90 
91 void
92 NetUtil::SendObjKill(Ship* obj, const Ship* killer, int type, int deck)
93 {
94  NetGame* net_game = NetGame::GetInstance();
95  if (!net_game || !obj) return;
96 
97  if (type == NetObjKill::KILL_DOCK || (net_game->IsServer() && obj->GetObjID())) {
98  NetObjKill kill;
99  kill.SetObjID(obj->GetObjID());
100  kill.SetFlightDeck(deck);
101 
102  if (killer)
103  kill.SetKillerID(killer->GetObjID());
104 
105  kill.SetKillType(type);
106 
107  if (type != NetObjKill::KILL_DOCK && obj->RespawnCount() > 0) {
108  Print("NetObjKill preparing respawn for %s\n", obj->Name());
109 
110  Point respawn_loc = RandomPoint() * 1.75;
111  kill.SetRespawn(true);
112  kill.SetRespawnLoc(respawn_loc);
113  obj->SetRespawnLoc(respawn_loc);
114  }
115  else {
116  Print("NetObjKill no respawn for %s\n", obj->Name());
117  }
118 
119  net_game->SendData(&kill);
120  }
121 }
122 
123 // +-------------------------------------------------------------------+
124 
125 void
126 NetUtil::SendObjHyper(Ship* obj, const char* rgn, const Point& loc,
127 const Ship* fc1, const Ship* fc2, int transtype)
128 {
129  NetGame* net_game = NetGame::GetInstance();
130  if (!net_game || !obj) return;
131 
132  if (obj->GetObjID()) {
133  NetObjHyper obj_hyper;
134  obj_hyper.SetObjID(obj->GetObjID());
135  obj_hyper.SetRegion(rgn);
136  obj_hyper.SetLocation(loc);
137  obj_hyper.SetTransitionType(transtype);
138 
139  if (fc1)
140  obj_hyper.SetFarcaster1(fc1->GetObjID());
141 
142  if (fc2)
143  obj_hyper.SetFarcaster2(fc2->GetObjID());
144 
145  net_game->SendData(&obj_hyper);
146  }
147 }
148 
149 // +-------------------------------------------------------------------+
150 
151 void
153 {
154  NetGame* net_game = NetGame::GetInstance();
155  if (!net_game || !obj) return;
156 
157  if (obj->GetObjID()) {
158  NetObjTarget obj_target;
159  obj_target.SetObjID(obj->GetObjID());
160 
161  SimObject* target = obj->GetTarget();
162  if (target) {
163  obj_target.SetTgtID(target->GetObjID());
164 
165  System* subtarget = obj->GetSubTarget();
166 
167  if (subtarget) {
168  Ship* s = (Ship*) target;
169  obj_target.SetSubtarget(subtarget->GetID());
170  }
171  }
172 
173  net_game->SendData(&obj_target);
174  }
175 }
176 
177 // +-------------------------------------------------------------------+
178 
179 void
181 {
182  NetGame* net_game = NetGame::GetInstance();
183  if (!net_game || !obj) return;
184 
185  if (obj->GetObjID()) {
186  NetObjEmcon obj_emcon;
187  obj_emcon.SetObjID(obj->GetObjID());
188  obj_emcon.SetEMCON(obj->GetEMCON());
189  net_game->SendData(&obj_emcon);
190  }
191 }
192 
193 // +-------------------------------------------------------------------+
194 
195 void
197 {
198  NetGame* net_game = NetGame::GetInstance();
199  if (!net_game || !wep) return;
200 
201  if (wep->IsPrimary() || net_game->IsClient()) {
202  NetWepTrigger trigger;
203  trigger.SetObjID(wep->Owner()->GetObjID());
204 
205  SimObject* target = wep->GetTarget();
206  if (target) {
207  trigger.SetTgtID(target->GetObjID());
208 
209  System* subtarget = wep->GetSubTarget();
210 
211  if (subtarget) {
212  Ship* s = (Ship*) target;
213  trigger.SetSubtarget(subtarget->GetID());
214  }
215  }
216 
217  trigger.SetIndex(wep->GetIndex());
218  trigger.SetCount(count);
219  trigger.SetDecoy(wep->IsDecoy());
220  trigger.SetProbe(wep->IsProbe());
221 
222  net_game->SendData(&trigger);
223  }
224 }
225 
226 // +-------------------------------------------------------------------+
227 
228 void
230 {
231  NetGame* net_game = NetGame::GetInstance();
232  if (!net_game || !wep || !shot) return;
233 
234  if (net_game->IsServer() && wep->IsMissile()) {
235  DWORD wepid = NetGame::GetNextObjID(NetGame::SHOT);
236  shot->SetObjID(wepid);
237 
238  NetWepRelease release;
239  release.SetObjID(wep->Owner()->GetObjID());
240 
241  SimObject* target = wep->GetTarget();
242  if (target)
243  release.SetTgtID(target->GetObjID());
244 
245  System* subtarget = wep->GetSubTarget();
246  if (target && subtarget && target->Type() == SimObject::SIM_SHIP) {
247  Ship* tgt = (Ship*) target;
248  release.SetSubtarget(subtarget->GetID());
249  }
250 
251  release.SetWepID(wepid);
252  release.SetIndex(wep->GetIndex());
253  release.SetDecoy(shot->IsDecoy());
254  release.SetProbe(shot->IsProbe());
255 
256  net_game->SendData(&release);
257  }
258 }
259 
260 // +-------------------------------------------------------------------+
261 
262 void
264 {
265  NetGame* net_game = NetGame::GetInstance();
266  if (!net_game || !shot) return;
267 
268  if (net_game->IsServer() && shot->GetObjID()) {
269  NetWepDestroy destroy;
270 
271  destroy.SetObjID(shot->GetObjID());
272 
273  net_game->SendData(&destroy);
274  }
275 }
276 
277 // +-------------------------------------------------------------------+
278 
279 void
280 NetUtil::SendChat(DWORD dst, const char* name, const char* text)
281 {
282  NetGame* net_game = NetGame::GetInstance();
283  if (!net_game || !name || !*name || !text || !*text) return;
284 
285  NetChatMsg chat_msg;
286  chat_msg.SetDstID(dst);
287  chat_msg.SetName(name);
288  chat_msg.SetText(text);
289 
290  if (net_game->IsClient()) {
291  net_game->SendData(&chat_msg);
292  }
293 
294  else {
295  NetGameServer* net_game_server = (NetGameServer*) net_game;
296  net_game_server->RouteChatMsg(chat_msg);
297  }
298 }
299 
300 // +-------------------------------------------------------------------+
301 
302 void
303 NetUtil::SendElemRequest(const char* name)
304 {
305  NetGame* net_game = NetGame::GetInstance();
306  if (!net_game || !name) return;
307 
308  NetElemRequest elem_request;
309  elem_request.SetName(name);
310 
311  ::Print("NetUtil::SendElemRequest name: '%s'\n", name);
312  net_game->SendData(&elem_request);
313 }
314 
315 // +-------------------------------------------------------------------+
316 
317 void
318 NetUtil::SendElemCreate(Element* elem, int squadron, int* slots, bool alert, bool in_flight)
319 {
320  NetGame* net_game = NetGame::GetInstance();
321  if (!net_game || !elem) return;
322 
323  NetElemCreate elem_create;
324  elem_create.SetName(elem->Name());
325  elem_create.SetType(elem->Type());
326  elem_create.SetIFF(elem->GetIFF());
327  elem_create.SetIntel(elem->IntelLevel());
328  elem_create.SetLoadout(elem->Loadout());
329  elem_create.SetSquadron(squadron);
330  elem_create.SetSlots(slots);
331  elem_create.SetAlert(alert);
332  elem_create.SetInFlight(in_flight);
333 
334  if (elem->NumObjectives() > 0) {
335  Instruction* obj = elem->GetObjective(0);
336 
337  if (obj) {
338  elem_create.SetObjCode(obj->Action());
339  elem_create.SetObjective(obj->TargetName());
340  }
341  }
342 
343  if (elem->GetCarrier())
344  elem_create.SetCarrier(elem->GetCarrier()->Name());
345 
346  if (elem->GetCommander())
347  elem_create.SetCommander(elem->GetCommander()->Name());
348 
349  ::Print("NetUtil::SendElemCreate iff: %d name: '%s'\n", elem->GetIFF(), elem->Name().data());
350  net_game->SendData(&elem_create);
351 }
352 
353 // +-------------------------------------------------------------------+
354 
355 void
356 NetUtil::SendShipLaunch(Ship* carrier, int squadron, int slot)
357 {
358  NetGame* net_game = NetGame::GetInstance();
359  if (!net_game || !carrier) return;
360 
361  if (carrier->GetObjID()) {
362  NetShipLaunch ship_launch;
363 
364  ship_launch.SetObjID(carrier->GetObjID());
365  ship_launch.SetSquadron(squadron);
366  ship_launch.SetSlot(slot);
367 
368  net_game->SendData(&ship_launch);
369  }
370 }
371 
372 // +-------------------------------------------------------------------+
373 
374 void
375 NetUtil::SendNavData(bool add, Element* elem, int index, Instruction* navpt)
376 {
377  NetGame* net_game = NetGame::GetInstance();
378  if (!net_game || !elem || !navpt) return;
379 
380  // resolve rloc before copying the navpoint into the net nav data structure:
381  Point loc = navpt->Location();
382 
383  NetNavData nav_data;
384  nav_data.SetObjID(net_game->GetObjID());
385  nav_data.SetAdd(add);
386  nav_data.SetElem(elem->Name());
387  nav_data.SetIndex(index);
388  nav_data.SetNavPoint(navpt);
389 
390  net_game->SendData(&nav_data);
391 }
392 
393 // +-------------------------------------------------------------------+
394 
395 void
397 {
398  NetGame* net_game = NetGame::GetInstance();
399  if (!net_game || !elem) return;
400 
401  NetNavDelete nav_delete;
402  nav_delete.SetObjID(net_game->GetObjID());
403  nav_delete.SetElem(elem->Name());
404  nav_delete.SetIndex(index);
405 
406  net_game->SendData(&nav_delete);
407 }
408 
409 // +-------------------------------------------------------------------+
410 
411 void
413 {
414  NetGame* net_game = NetGame::GetInstance();
415  if (!net_game || !obj) return;
416 
417  if (obj->GetObjID()) {
418  NetSelfDestruct sd;
419  sd.SetObjID(obj->GetObjID());
420  sd.SetDamage((float) dmg);
421 
422  net_game->SendData(&sd);
423  }
424 }