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
netserver_clients.cpp File Reference

Go to the source code of this file.

Functions

StarSystemGetLoadedStarSystem (const char *system)
 
void displayUnitInfo (Unit *un, const string callsign, const char *type)
 
Quaternion MinRotationFromDirections (Vector start, Vector finish, double &theta)
 
Vector ApplyQuaternion (const Quaternion &quat, Vector input)
 
ClientState aim_assist (ClientState cs, ClientState ocs, QVector realtargetpos, Vector realtargetvel, Vector targetpos, Vector targetvel)
 
ClientState aim_assist (ClientState cs, ClientState ocs, Unit *target, Vector targetpos, Vector targetvel)
 
ClientState aim_assist_debug (float x, float y, float z, float vx, float vy, float vz, float rx, float ry, float rz, float vrx, float vry, float vrz, float forex, float forey, float forez)
 
void AddWriteSave (std::string &netbuf, int cpnum)
 
void AcctLogout (VsnetHTTPSocket *acct_sock, ClientPtr clt)
 

Function Documentation

void AcctLogout ( VsnetHTTPSocket acct_sock,
ClientPtr  clt 
)

Definition at line 619 of file netserver_clients.cpp.

References _Universe, ACCT_LOGOUT, ACCT_SAVE_LOGOUT, addSimpleChar(), addSimpleString(), AddWriteSave(), COUT, Client::INGAME, VsnetHTTPSocket::sendstr(), and Universe::whichPlayerStarship().

620 {
621  if (acct_sock == NULL) return;
622  if (clt) {
623  std::string netbuf;
624 
625  Unit *un = clt->game_unit.GetUnit();
626  int cpnum = _Universe->whichPlayerStarship( un );
627  bool dosave = false;
628  if (clt->loginstate < Client::INGAME)
629  dosave = false;
630  addSimpleChar( netbuf, dosave ? ACCT_SAVE_LOGOUT : ACCT_LOGOUT );
631  addSimpleString( netbuf, clt->callsign );
632  addSimpleString( netbuf, clt->passwd );
633  if (dosave)
634  AddWriteSave( netbuf, cpnum );
635  if ( !acct_sock->sendstr( netbuf ) )
636  COUT<<"ERROR sending LOGOUT to account server"<<endl;
637  }
638 }
void AddWriteSave ( std::string &  netbuf,
int  cpnum 
)

Definition at line 611 of file netserver_clients.cpp.

References addSimpleString(), and SaveNetUtil::GetSaveStrings().

Referenced by AcctLogout().

612 {
613  string savestr, xmlstr;
614  SaveNetUtil::GetSaveStrings( cpnum, savestr, xmlstr, true );
615  addSimpleString( netbuf, savestr );
616  addSimpleString( netbuf, xmlstr );
617 }
ClientState aim_assist ( ClientState  cs,
ClientState  ocs,
QVector  realtargetpos,
Vector  realtargetvel,
Vector  targetpos,
Vector  targetvel 
)

Definition at line 343 of file netserver_clients.cpp.

References ApplyQuaternion(), UniverseUtil::cos(), FINITE, fprintf, Quaternion::from_vectors(), ClientState::getAngularVelocity(), ClientState::getOrientation(), ClientState::getPosition(), Matrix::getQ(), Matrix::getR(), VegaConfig::getVariable(), ClientState::getVelocity(), M_PI, MinRotationFromDirections(), XMLSupport::parse_bool(), XMLSupport::parse_float(), Quaternion::s, ClientState::setAngularVelocity(), ClientState::setOrientation(), Quaternion::to_matrix(), Quaternion::v, Vector, and vs_config.

Referenced by aim_assist(), aim_assist_debug(), and NetClient::sendPosition().

349 {
350  float t = 0;
351  static bool debug_predict = XMLSupport::parse_bool( vs_config->getVariable( "network", "debug_prediction", "false" ) );
352  double velaimtheta = 0;
353  double orientaimtheta = 0;
354  Matrix trans;
355  ClientState OrientationAim( cs );
356  ClientState VelocityAim( cs );
357  bool VelocityAimValid = false;
358  bool OrientationAimValid = false;
359  cs.getOrientation().to_matrix( trans );
360  Vector dir = trans.getR();
361  targetvel = targetvel-cs.getVelocity();
362  realtargetvel = realtargetvel-cs.getVelocity();
363  Vector targetperp = targetpos.Cross( targetvel );
364  realtargetpos = ( realtargetpos-cs.getPosition() );
365  Vector realtargetperp = realtargetpos.Cast().Cross( realtargetvel );
366  Vector targetdir = targetpos;
367  targetdir.Normalize();
368  float targetperpmag = targetperp.Magnitude();
369  float realtargetperpmag = realtargetperp.Magnitude();
370  static float cos_min_angle =
371  cos( M_PI*(1./180)*XMLSupport::parse_float( vs_config->getVariable( "network", "max_lead_prediction_angle", "60" ) ) ); //120 degree leeway for using this method
372  if (targetperpmag > .001 && realtargetperpmag > .001 && targetvel.MagnitudeSquared() > .25
373  && realtargetvel.MagnitudeSquared() > .25 && dir.Dot( targetdir ) >= cos_min_angle) {
374  //gotta make sure target is at least in viewscreen before giving benefit of the doubt lead
375  targetperp = targetperp*(1./targetperpmag);
376  realtargetperp = realtargetperp*(1./realtargetperpmag); //compute a unit direction perpendicular to the plane made by the target and its relative velocity (to playerstarship)... we'll use this to see how far off that plane the velocity actually is
377  float distoffplane = targetperp.Dot( dir );
378  Vector dirperp = targetperp.Scale( distoffplane );
379  Vector dirplane = dir-dirperp; //this is the projection of the player's orientation direction onto the plane if the target and its velocity... if he were aligned with the arc of travel of the target he would Dot(targetperp,dir) would be zero and this wouldn't change the dir
380  //now we have to figure out how far out the intersection point of dirplane and targetvel are...
381  //solve for t and k reltargetpos+targetvel*t=k*dirplane
382  float posx = targetpos.i;
383  float posy = targetpos.j;
384  float dirx = dirplane.i;
385  float diry = dirplane.j;
386  float velx = targetvel.i;
387  float vely = targetvel.j;
388  if ( fabs( dirplane.i ) <= fabs( dirplane.j ) && fabs( dirplane.i ) <= fabs( dirplane.k ) ) {
389  posx = targetpos.k;
390  posy = targetpos.j;
391  dirx = dirplane.k;
392  diry = dirplane.j;
393  velx = targetvel.k;
394  vely = targetvel.j;
395  }
396  if ( fabs( dirplane.j ) <= fabs( dirplane.i ) && fabs( dirplane.j ) <= fabs( dirplane.k ) ) {
397  posx = targetpos.i;
398  posy = targetpos.k;
399  dirx = dirplane.i;
400  diry = dirplane.k;
401  velx = targetvel.i;
402  vely = targetvel.k;
403  }
404  if ( fabs( diry ) > fabs( dirx ) ) {
405  float tmp = posx;
406  posx = posy;
407  posy = tmp;
408  tmp = dirx;
409  dirx = diry;
410  diry = tmp;
411  tmp = velx;
412  velx = vely;
413  vely = tmp;
414  }
415  {
416  float num = diry*posx/dirx-posy;
417  float denom = vely-diry*velx/dirx;
418  if (fabs( denom ) < .0001)
419  t = 0;
420  else
421  t = num/denom;
422  //if (t<=0) t=0;
423  }
424  static float velaimlim = XMLSupport::parse_float( vs_config->getVariable( "network", "max_lead_prediction", "10" ) );
425  if (fabs( t ) < velaimlim) {
426  Vector newdirplane = realtargetpos+realtargetvel*t;
427  Vector newdirperp = realtargetperp.Scale( distoffplane );
428  newdirplane.Normalize();
429  newdirplane *= dirplane.Magnitude();
430  Vector newdir = newdirplane+newdirperp;
431  newdir.Normalize();
432  Vector newright, newup;
433  CrossProduct( trans.getQ(), newdir, newright );
434  CrossProduct( newdir, newright, newup );
435  newright.Normalize();
436  newup.Normalize();
437  Quaternion forient = Quaternion::from_vectors( newright, newup, newdir );
438  Vector fvel = ApplyQuaternion( MinRotationFromDirections( dir, newdir, velaimtheta ), cs.getAngularVelocity() );
439  VelocityAimValid = FINITE( fvel.i ) && FINITE( fvel.j ) && FINITE( fvel.k ) && FINITE( forient.s ) && FINITE(
440  forient.v.i ) && FINITE( forient.v.j ) && FINITE( forient.v.k );
441  VelocityAim.setAngularVelocity( fvel );
442  VelocityAim.setOrientation( forient );
443  }
444  }
445  {
446  //some other scheme for when target is still...
447  targetpos.Normalize();
448  realtargetpos.Normalize();
449  Quaternion quat = MinRotationFromDirections( targetpos, realtargetpos, orientaimtheta );
450  Quaternion forient = cs.getOrientation()*quat;
451  Vector fvel = ApplyQuaternion( quat, cs.getAngularVelocity() );
452 
453  OrientationAimValid = FINITE( fvel.i ) && FINITE( fvel.j ) && FINITE( fvel.k ) && FINITE( forient.s ) && FINITE(
454  forient.v.i ) && FINITE( forient.v.j ) && FINITE( forient.v.k );
455  OrientationAim.setOrientation( forient );
456  OrientationAim.setAngularVelocity( fvel );
457  }
458  if (VelocityAimValid) {
459  if (debug_predict)
460  printf( "Velocity aim lead %f, rot %f deg\n", t, velaimtheta*180/M_PI );
461  return VelocityAim;
462  }
463  if (OrientationAimValid) {
464  if (debug_predict)
465  printf( "Using orientation aim rot %f deg\n", orientaimtheta*180/M_PI );
466  return OrientationAim;
467  }
468  fprintf( stderr, "Got non-finite result for aim_assist (client sent bad info?)\n" );
469  return cs;
470 }
ClientState aim_assist ( ClientState  cs,
ClientState  ocs,
Unit target,
Vector  targetpos,
Vector  targetvel 
)

Definition at line 472 of file netserver_clients.cpp.

References aim_assist(), Unit::Position(), and Unit::Velocity.

473 {
474  return aim_assist( cs, ocs, target->Position(), target->Velocity, targetpos, targetvel );
475 }
ClientState aim_assist_debug ( float  x,
float  y,
float  z,
float  vx,
float  vy,
float  vz,
float  rx,
float  ry,
float  rz,
float  vrx,
float  vry,
float  vrz,
float  forex,
float  forey,
float  forez 
)

Definition at line 477 of file netserver_clients.cpp.

References aim_assist(), Quaternion::from_vectors(), ClientState::getOrientation(), Matrix::getQ(), Matrix::getR(), QVector, Quaternion::to_matrix(), up, and Vector.

492 {
493  Vector fore( forex, forey, forez );
494  fore.Normalize();
495  Vector up( 0, 1, 0 );
496  if (fore.j > .9)
497  up = Vector( 0, 0, 1 );
498  Vector right;
499  CrossProduct( up, fore, right );
500  CrossProduct( fore, right, up );
501  right.Normalize();
502  up.Normalize();
503  fore.Normalize();
504  ClientState tmp( 1, QVector( 0, 0, 0 ), Quaternion::from_vectors( right, up, fore ), Vector( 0, 0, 0 ), Vector( 0,
505  0,
506  0 ), Vector(
507  0,
508  1,
509  0 ) );
510  tmp = aim_assist( tmp, tmp, QVector( rx, ry, rz ), Vector( vrx, vry, vrz ), Vector( x, y, z ), Vector( vx, vy, vz ) );
511  Matrix mat;
512  tmp.getOrientation().to_matrix( mat );
513  printf( "Final Direction %.3f %.3f %.3f (Up %.2f %.2f %.2f)\n", mat.getR().i, mat.getR().j, mat.getR().k,
514  mat.getQ().i, mat.getQ().j, mat.getQ().k );
515  return tmp;
516 }
Vector ApplyQuaternion ( const Quaternion quat,
Vector  input 
)

Definition at line 331 of file netserver_clients.cpp.

References Quaternion::to_matrix(), and TransformNormal().

Referenced by aim_assist().

332 {
333  Matrix tmp;
334  quat.to_matrix( tmp );
335  return TransformNormal( tmp, input );
336 }
void displayUnitInfo ( Unit un,
const string  callsign,
const char *  type 
)

Definition at line 46 of file zonemgr.cpp.

Referenced by ZoneMgr::addClient(), ZoneInfo::display(), ZoneMgr::displayNPCs(), ZoneMgr::removeClient(), NetServer::sendKill(), and NetServer::sendNewUnitQueue().

47 {
48  cout<<type;
49  if (!un) {
50  cout<<"\'"<<callsign<<"\' (dead)"<<endl;
51  } else {
52  {
53  char tmp[10];
54  sprintf( tmp, "% 5d ", un->GetSerial() );
55  cout<<tmp;
56  }
57  cout<<UnitUtil::getFactionName( un )<<" "<<un->getFullname()<<" ("<<un->name<<")";
58  Flightgroup *fg = un->getFlightgroup();
59  if (fg) cout<<", \'"<<fg->name<<"\' "<<un->getFgSubnumber();
60  if ( !callsign.empty() && (!fg || callsign != fg->name) )
61  cout<<", callsign \'"<<callsign<<"\'";
62  cout<<endl;
63  }
64 }
StarSystem* GetLoadedStarSystem ( const char *  system)

Definition at line 786 of file star_system_generic.cpp.

Referenced by ZoneMgr::addZone(), GameUniverse::GenerateStarSystem(), and Universe::GenerateStarSystem().

787 {
788  StarSystem *ss = star_system_table.Get( string( system ) );
789  std::string ssys( string( system )+string( ".system" ) );
790  if (!ss)
791  ss = star_system_table.Get( ssys );
792  return ss;
793 }
Quaternion MinRotationFromDirections ( Vector  start,
Vector  finish,
double &  theta 
)

Definition at line 314 of file netserver_clients.cpp.

References UniverseUtil::asin(), UniverseUtil::cos(), e, float, identity_quaternion(), M_PI, UniverseUtil::sin(), and Vector.

Referenced by aim_assist().

315 {
316  Vector rotation = finish.Cross( start );
317  double mag = rotation.Cast().Magnitude();
318  if (mag >= 1)
319  mag = 1;
320  if (mag < 1e-6) {
321  theta = 0;
322  return identity_quaternion;
323  }
324  theta = asin( mag );
325  if (start.Dot( finish ) < 0) theta += M_PI/2;
326  float s = (float) cos( theta*.5 );
327  rotation = rotation.Scale( 1./mag );
328  return Quaternion( s, rotation*sin( theta*.5 ) );
329 }