Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
unit_factory_generic.cpp
Go to the documentation of this file.
1 #include "unit_factory.h"
2 #include "unit_generic.h"
3 #include "universe_util.h"
4 #include "planet_generic.h"
5 #include "asteroid_generic.h"
6 #include "missile_generic.h"
7 #include "nebula_generic.h"
8 #include "networking/zonemgr.h"
10 
13 {
14  if (_masterPartList == NULL) {
15  static bool making = true;
16  if (making) {
17  making = false;
19  making = true;
20  }
21  }
22  return _masterPartList;
23 }
24 
26  const string &filename,
27  const string &name,
28  const string &fullname,
29  bool SubUnit,
30  int faction,
31  std::string customizedUnit,
32  const ClientState &curr_state,
33  Flightgroup *flightgroup,
34  int fg_subnumber,
35  string *netxml,
36  ObjSerial netcreate )
37 {
38  netbuf.addChar( ZoneMgr::AddUnit );
39  assert( netcreate != 0 ); //crash if we are doing something stupid :)
40  netbuf.addSerial( netcreate );
41  netbuf.addString( filename );
42  netbuf.addString( name );
43  netbuf.addString( fullname );
44  netbuf.addChar( SubUnit );
45  netbuf.addInt32( faction );
46  netbuf.addString( flightgroup != NULL ? flightgroup->name : std::string( "Object" ) );
47  netbuf.addString( customizedUnit );
48  netbuf.addInt32( fg_subnumber );
49  if (netbuf.version() <= 4951)
50  netbuf.addTransformation( curr_state.getTransformation() );
51  else
52  netbuf.addClientState( curr_state );
53 }
54 
55 void UnitFactory::addUnitBuffer( NetBuffer &netbuf, const Unit *un, string *netxml )
56 {
57  addUnitBuffer( netbuf, un->getFilename(), un->name.get(), un->fullname, un->isSubUnit(), un->faction,
58  "" /* Not sure... maybe netxml will take care of this? */, ClientState( un ),
59  un->getFlightgroup(),
60  un->getFgSubnumber(), netxml /*For ENTERCLIENT, will generate a saved game netxml*/, un->GetSerial() );
61 }
62 
64 {
65  ObjSerial serial = netbuf.getSerial();
66  string file( netbuf.getString() );
67  string name( netbuf.getString() );
68  string fullname( netbuf.getString() );
69  bool sub = netbuf.getChar();
70  int faction = netbuf.getInt32();
71  string fname( netbuf.getString() );
72  string custom( netbuf.getString() );
73  int fg_num = netbuf.getInt32();
74 
75  cerr<<"NETCREATE UNIT : "<<file<<" ("<<serial<<")"<<endl;
76 
77  string facname = FactionUtil::GetFactionName( faction );
78  if ( facname.empty() ) {
79  //Got an invalid faction number...
80  cerr<<" Unit has an invalid faction "<<faction<<endl;
81  faction = 0;
82  }
83  Flightgroup *fg = NULL;
84  if ( !fname.empty() ) {
85  fg = mission[0].findFlightgroup( fname, facname );
86  if (!fg)
87  fg = Flightgroup::newFlightgroup( fname, file, facname,
88  "default", 1, 1, "", "", mission );
89  }
90  Unit *un = createUnit( file.c_str(), sub, faction, custom, fg, fg_num, NULL, serial );
91  if (netbuf.version() <= 4951)
93  else
94  netbuf.getClientState().setUnitState( un );
95  un->name = name;
96  un->fullname = fullname;
97  return un;
98 }
99 
101  QVector x,
102  QVector y,
103  float vely,
104  const Vector &rotvel,
105  float pos,
106  float gravity,
107  float radius,
108  const char *filename,
109  BLENDFUNC sr,
110  BLENDFUNC ds,
111  const vector< string > &dest,
112  const QVector &orbitcent,
113  Unit *parent,
114  const GFXMaterial &ourmat,
115  const std::vector< GFXLightLocal > &ligh,
116  int faction,
117  string fullname,
118  bool inside_out,
119  ObjSerial netcreate )
120 {
121  netbuf.addChar( ZoneMgr::AddPlanet );
122  netbuf.addSerial( netcreate );
123  netbuf.addQVector( x );
124  netbuf.addQVector( y );
125  netbuf.addFloat( vely );
126  netbuf.addVector( Vector( rotvel ) );
127  netbuf.addFloat( pos );
128  netbuf.addFloat( gravity );
129  netbuf.addFloat( radius );
130 
131  netbuf.addString( string( filename ) );
132  netbuf.addChar( sr );
133  netbuf.addChar( ds );
134 
135  netbuf.addShort( dest.size() );
136  for (unsigned int i = 0; i < dest.size(); i++)
137  netbuf.addString( dest[i] );
138  netbuf.addQVector( QVector( orbitcent ) );
139  netbuf.addSerial( parent->GetSerial() );
140  netbuf.addGFXMaterial( ourmat );
141  netbuf.addShort( ligh.size() );
142  for (unsigned int j = 0; j < ligh.size(); j++)
143  netbuf.addGFXLightLocal( ligh[j] );
144  netbuf.addInt32( faction );
145  netbuf.addString( fullname );
146  netbuf.addChar( inside_out );
147 }
148 
150 {
151  ObjSerial serial = netbuf.getSerial();
152  QVector x = netbuf.getQVector();
153  QVector y = netbuf.getQVector();
154  float vely = netbuf.getFloat();
155  const Vector rotvel( netbuf.getVector() );
156  float pos = netbuf.getFloat();
157  float gravity = netbuf.getFloat();
158  float radius = netbuf.getFloat();
159 
160  string file( netbuf.getString() );
161  char sr = netbuf.getChar();
162  char ds = netbuf.getChar();
163 
164  vector< string >dest;
165  unsigned short nbdest = netbuf.getShort();
166  int i = 0;
167  for (i = 0; i < nbdest; i++) {
168  string tmp( netbuf.getString() );
169  char *ctmp = new char[tmp.length()+1];
170  ctmp[tmp.length()] = 0;
171  memcpy( ctmp, tmp.c_str(), tmp.length() );
172  dest.push_back( ctmp );
173  }
174  const QVector orbitcent( netbuf.getQVector() );
176  GFXMaterial mat = netbuf.getGFXMaterial();
177 
178  vector< GFXLightLocal >lights;
179  unsigned short nblight = netbuf.getShort();
180  for (i = 0; i < nblight; i++)
181  lights.push_back( netbuf.getGFXLightLocal() );
182  int faction = netbuf.getInt32();
183 
184  string facname = FactionUtil::GetFactionName( faction );
185  if ( facname.empty() ) {
186  //Got an invalid faction number...
187  cerr<<" Planet "<<file<<" has an invalid faction "<<faction<<endl;
188  faction = 0;
189  }
190  string fullname( netbuf.getString() );
191  char insideout = netbuf.getChar();
192 
193  cerr<<"NETCREATE PLANET : "<<file<<endl;
194 
195  return UnitFactory::createPlanet( x, y, vely, rotvel, pos, gravity, radius,
196  file, string(), string(),
197  (BLENDFUNC) sr, (BLENDFUNC) ds, dest, orbitcent, un, mat, lights, faction, fullname,
198  insideout, serial );
199 }
200 
202  const char *unitfile,
203  bool SubU,
204  int faction,
205  Flightgroup *fg,
206  int fg_snumber,
207  ObjSerial netcreate )
208 {
209  netbuf.addChar( ZoneMgr::AddNebula );
210  netbuf.addSerial( netcreate );
211  netbuf.addString( string( unitfile ) );
212  netbuf.addChar( SubU );
213  netbuf.addInt32( faction );
214  netbuf.addString( fg->name );
215  netbuf.addInt32( fg_snumber );
216 }
217 
218 void UnitFactory::addNebulaBuffer( NetBuffer &netbuf, const Nebula *neb )
219 {
220  addNebulaBuffer( netbuf, neb->getFilename().c_str(), neb->isSubUnit(), neb->faction,
221  neb->getFlightgroup(), neb->getFgSubnumber(), neb->GetSerial() );
222 }
223 
225 {
226  ObjSerial serial = netbuf.getSerial();
227  string file( netbuf.getString() );
228  bool sub = netbuf.getChar();
229  int faction = netbuf.getInt32();
230  string fname( netbuf.getString() );
231  int fg_num = netbuf.getInt32();
232 
233  cerr<<"NETCREATE NEBULA : "<<file<<endl;
234 
235  string facname = FactionUtil::GetFactionName( faction );
236  if ( facname.empty() ) {
237  //Got an invalid faction number...
238  cerr<<" Nebula has an invalid faction "<<faction<<endl;
239  faction = 0;
240  }
241  Flightgroup *fg = mission[0].findFlightgroup( fname, facname );
242  return UnitFactory::createNebula( file.c_str(), sub, faction, fg, fg_num, serial );
243 }
244 
246  const string &filename,
247  const string &name,
248  const string &fullname,
249  int faction,
250  const string &modifications,
251  const ClientState &curr_state,
252  const float damage,
253  float phasedamage,
254  float time,
255  float radialeffect,
256  float radmult,
257  float detonation_radius,
258  ObjSerial netcreate )
259 {
260  netbuf.addChar( ZoneMgr::AddMissile );
261  netbuf.addSerial( netcreate );
262  netbuf.addString( filename );
263  netbuf.addString( name );
264  netbuf.addString( fullname );
265 
266  netbuf.addInt32( faction );
267  netbuf.addString( modifications );
268  netbuf.addFloat( damage );
269  netbuf.addFloat( phasedamage );
270  netbuf.addFloat( time );
271  netbuf.addFloat( radialeffect );
272  netbuf.addFloat( radmult );
273  netbuf.addFloat( detonation_radius );
274  if (netbuf.version() <= 4951)
275  netbuf.addTransformation( curr_state.getTransformation() );
276  else
277  netbuf.addClientState( curr_state );
278 }
279 
281 {
282  addMissileBuffer( netbuf, mis->getFilename().c_str(), mis->name, mis->getFullname(), mis->faction,
283  "" /* modifications */, ClientState(
284  static_cast< const Unit* > (mis) ), mis->damage, mis->phasedamage, mis->time,
285  mis->radial_effect, mis->radial_multiplier, mis->detonation_radius, mis->GetSerial() );
286 }
287 
289 {
290  ObjSerial serial = netbuf.getSerial();
291  string file( netbuf.getString() );
292  string name( netbuf.getString() );
293  string fullname( netbuf.getString() );
294  int faction = netbuf.getInt32();
295  string mods( netbuf.getString() );
296  const float damage( netbuf.getFloat() );
297  float phasedamage = netbuf.getFloat();
298  float time = netbuf.getFloat();
299  float radialeffect = netbuf.getFloat();
300  float radmult = netbuf.getFloat();
301  float detonation_radius = netbuf.getFloat();
302  const string modifs( mods );
303 
304  cerr<<"NETCREATE MISSILE : "<<file<<" ("<<serial<<")"<<endl;
305 
306  string facname = FactionUtil::GetFactionName( faction );
307  if ( facname.empty() ) {
308  //Got an invalid faction number...
309  cerr<<" Missile has an invalid faction "<<faction<<endl;
310  faction = 0;
311  }
312  Missile *mis = createMissile(
313  file.c_str(), faction, modifs, damage, phasedamage, time, radialeffect, radmult, detonation_radius, serial );
314  if (netbuf.version() <= 4951)
315  mis->curr_physical_state = netbuf.getTransformation();
316  else
317  netbuf.getClientState().setUnitState( mis );
318  mis->name = name;
319  mis->fullname = fullname;
320  return mis;
321 }
322 
324  const char *filename,
325  int faction,
326  Flightgroup *fg,
327  int fg_snumber,
328  float difficulty,
329  ObjSerial netcreate )
330 {
331  netbuf.addChar( ZoneMgr::AddAsteroid );
332  netbuf.addSerial( netcreate );
333  netbuf.addString( string( filename ) );
334  netbuf.addInt32( faction );
335  netbuf.addString( fg->name );
336  netbuf.addInt32( fg_snumber );
337  netbuf.addFloat( difficulty );
338 }
339 
340 void UnitFactory::addAsteroidBuffer( NetBuffer &netbuf, const Asteroid *aster )
341 {
342  addAsteroidBuffer( netbuf, aster->getFilename().c_str(), aster->faction, aster->getFlightgroup(), aster->getFgSubnumber(),
343  0 /*difficulty--only determines angular velocity*/, aster->GetSerial() );
344 }
345 
347 {
348  ObjSerial serial = netbuf.getSerial();
349  string file( netbuf.getString() );
350  int faction = netbuf.getInt32();
351  string fname( netbuf.getString() );
352  int fg_snumber = netbuf.getInt32();
353  float diff = netbuf.getFloat();
354 
355  cerr<<"NETCREATE ASTEROID : "<<file<<endl;
356 
357  string facname = FactionUtil::GetFactionName( faction );
358  if ( facname.empty() ) {
359  //Got an invalid faction number...
360  cerr<<" Asteroid has an invalid faction "<<faction<<endl;
361  faction = 0;
362  }
363  Flightgroup *fg = mission[0].findFlightgroup( fname, facname );
364 
365  return UnitFactory::createAsteroid( file.c_str(), faction, fg, fg_snumber, diff, serial );
366 }
367 
368 void UnitFactory::addBuffer( NetBuffer &netbuf, const Unit *un, bool allowSystemTypes, string *netxml )
369 {
370  switch ( un->isUnit() )
371  {
372  case UNITPTR:
373  addUnitBuffer( netbuf, un, netxml );
374  break;
375  case MISSILEPTR:
376  addMissileBuffer( netbuf, static_cast< const Missile* > (un) );
377  break;
378  case NEBULAPTR:
379  if (allowSystemTypes)
380  addNebulaBuffer( netbuf, static_cast< const Nebula* > (un) );
381  break;
382  case ASTEROIDPTR:
383  if (allowSystemTypes)
384  addAsteroidBuffer( netbuf, static_cast< const Asteroid* > (un) );
385  break;
386  default:
387  break;
388  }
389 }
390 
392 {
393  netbuf.addChar( ZoneMgr::End );
394 }
395