29 allClients.push_back( newclt );
31 COUT<<
" - Actual number of clients : "<<allClients.size()<<endl;
41 newUnits.push_back( un );
46 unsigned short vers[4] = {0, 4951, 4952, 65535};
47 for (ZoneMap::const_iterator zoneiter =
zonemgr->
zones.begin();
49 unsigned short zone = (*zoneiter).first;
50 for (
int verind = 0; verind < 4; verind += 2) {
51 unsigned short minver = vers[verind], maxver = vers[verind+1];
53 if (maxver != 65535) netbuf.
setVersion( maxver );
55 for (std::vector< UnitContainer >::iterator iter = newUnits.begin();
56 iter != newUnits.end();
58 Unit *un = (*iter).GetUnit();
62 unsigned short unzone = sys->
GetZone();
72 COUT<<
"New Unit already killed before being sent!!!"<<endl;
96 void NetServer::addClient(
ClientPtr clt )
98 Unit *un = clt->game_unit.GetUnit();
100 COUT<<
"Error: adding NULL (dead) client! "<<clt->callsign<<endl;
103 COUT<<
">>> SEND ENTERCLIENT =( serial #"<<un->
GetSerial()<<
" )= --------------------------------------"<<endl;
105 string savestr, xmlstr;
114 cerr<<
"ADDING Player number "<<player<<endl;
120 unsigned short zoneid;
125 string sysfile = starsys+
".system";
128 cerr<<
"!!! FATAL ERROR : star system '"<<starsys<<
"' not found"<<endl;
140 Unit *dockedUnit = NULL;
142 std::string srcsys =
getSaveString( player,
"jump_from", 0 );
145 (grav = *ui) != NULL;
148 for (
unsigned int i = 0;
i < siz; ++
i)
171 cerr<<
"PLAYER NOT DOCKED - STARTING AT POSITION : x="<<safevec.i<<
",y="<<safevec.j<<
",z="<<safevec.k<<endl;
174 cerr<<
"PLAYER DOCKED TO "<<dockedUnit->
getFullname()<<
" - STARTING DOCKED AT POSITION : x="<<safevec.i<<
",y="
175 <<safevec.j<<
",z="<<safevec.k<<endl;
212 <<
") TO OTHER CLIENT IN THE ZONE------------------------------------------"<<endl;
217 COUT<<
">>> SEND ADDED YOU =( serial #"<<un->
GetSerial()<<
" )= --------------------------------------"<<endl;
227 clt->prediction->InitInterpolation( un, un->
old_state, 0, clt->getNextDeltatime() );
277 unsigned short clt_port = netbuf.
getShort();
280 clt->setUDP( udpNetwork, adr );
297 void NetServer::removeClient(
ClientPtr clt )
300 Unit *un = clt->game_unit.GetUnit();
316 Vector rotation = finish.Cross( start );
317 double mag = rotation.Cast().Magnitude();
325 if (start.Dot( finish ) < 0) theta +=
M_PI/2;
327 rotation = rotation.Scale( 1./mag );
352 double velaimtheta = 0;
353 double orientaimtheta = 0;
357 bool VelocityAimValid =
false;
358 bool OrientationAimValid =
false;
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 =
372 if (targetperpmag > .001 && realtargetperpmag > .001 && targetvel.MagnitudeSquared() > .25
373 && realtargetvel.MagnitudeSquared() > .25 && dir.Dot( targetdir ) >= cos_min_angle) {
375 targetperp = targetperp*(1./targetperpmag);
376 realtargetperp = realtargetperp*(1./realtargetperpmag);
377 float distoffplane = targetperp.Dot( dir );
378 Vector dirperp = targetperp.Scale( distoffplane );
379 Vector dirplane = dir-dirperp;
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 ) ) {
396 if ( fabs( dirplane.j ) <= fabs( dirplane.i ) && fabs( dirplane.j ) <= fabs( dirplane.k ) ) {
404 if ( fabs( diry ) > fabs( dirx ) ) {
416 float num = diry*posx/dirx-posy;
417 float denom = vely-diry*velx/dirx;
418 if (fabs( denom ) < .0001)
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;
433 CrossProduct( trans.
getQ(), newdir, newright );
434 CrossProduct( newdir, newright, newup );
435 newright.Normalize();
447 targetpos.Normalize();
448 realtargetpos.Normalize();
458 if (VelocityAimValid) {
460 printf(
"Velocity aim lead %f, rot %f deg\n", t, velaimtheta*180/
M_PI );
463 if (OrientationAimValid) {
465 printf(
"Using orientation aim rot %f deg\n", orientaimtheta*180/
M_PI );
466 return OrientationAim;
468 fprintf( stderr,
"Got non-finite result for aim_assist (client sent bad info?)\n" );
493 Vector fore( forex, forey, forez );
499 CrossProduct( up, fore, right );
500 CrossProduct( fore, right, up );
504 ClientState tmp( 1,
QVector( 0, 0, 0 ),
Quaternion::from_vectors( right, up, fore ),
Vector( 0, 0, 0 ),
Vector( 0,
510 tmp =
aim_assist( tmp, tmp,
QVector( rx, ry, rz ),
Vector( vrx, vry, vrz ),
Vector( x, y, z ),
Vector( vx, vy, vz ) );
513 printf(
"Final Direction %.3f %.3f %.3f (Up %.2f %.2f %.2f)\n", mat.
getR().i, mat.
getR().j, mat.
getR().k,
518 void NetServer::posUpdate(
ClientPtr clt )
523 Unit *un = clt->game_unit.GetUnit();
530 clt->elapsed_since_packet = 0;
531 if ( clt_serial != un->GetSerial() )
532 cerr<<
"!!! ERROR : Received an update from a serial that differs with the client we found !!!"<<endl;
541 if (clt->netversion > 4960)
542 inSpec = netbuf.
getChar() ? 1 : 0;
543 if ( !
FINITE( cs.getPosition().i ) || !
FINITE( cs.getPosition().j ) || !
FINITE( cs.getPosition().k ) ) {
544 cerr<<
"Unit "<<clt_serial<<
" sent me an invalid position"<<endl;
551 if (target_serial != 0) {
552 unsigned int zone = un->getStarSystem()->GetZone();
555 QVector target_position = cs.getPosition()+rel_target_position;
557 if ( !
FINITE( target_position.i ) || !
FINITE( target_position.j ) || !
FINITE( target_position.k ) ) {
558 cerr<<
"Unit "<<clt_serial<<
" sent me an invalid target position for aim assist, targ="<<target_serial<<endl;
561 clt->versionBuf( netbuf );
562 netbuf.
addFloat( clt->getDeltatime() );
578 cs =
aim_assist( cs, clt->last_packet, target, rel_target_position, target_velocity );
583 if (debugPos)
COUT<<
"Received ZoneMgr::PosUpdate from client "<<clt_serial<<
" *** cs="<<cs<<endl;
584 un->curr_physical_state.position = cs.getPosition();
585 un->curr_physical_state.orientation = cs.getOrientation();
586 un->Velocity = cs.getVelocity();
587 un->AngularVelocity = cs.getAngularVelocity();
589 un->graphicOptions.WarpRamping = (inSpec != un->graphicOptions.InWarp);
590 un->graphicOptions.InWarp = inSpec;
592 assert( clt->prediction );
593 clt->prediction->InitInterpolation( un, clt->last_packet, clt->getDeltatime(), clt->getNextDeltatime() );
595 clt->last_packet = cs;
601 COUT<<
"ERROR: Client has NULL cockpit. Does he have two clients ingame? ser="<<un->GetSerial()<<
", callsign="
602 <<clt->callsign<<endl;
613 string savestr, xmlstr;
621 if (acct_sock == NULL)
return;
625 Unit *un = clt->game_unit.GetUnit();
635 if ( !acct_sock->
sendstr( netbuf ) )
636 COUT<<
"ERROR sending LOGOUT to account server"<<endl;
640 void NetServer::disconnect(
ClientPtr clt,
const char *debug_from_file,
int debug_from_line )
642 COUT<<
"enter "<<__PRETTY_FUNCTION__<<endl
643 <<
" *** from "<<debug_from_file<<
":"<<debug_from_line<<endl
644 <<
" *** disconnecting "<<clt->callsign<<
" because of "
645 <<clt->_disconnectReason<<endl;
655 Unit *un = clt->game_unit.GetUnit();
656 std::string callsign = clt->callsign;
658 std::map< std::string, WaitListEntry >::iterator iter = waitList.find( clt->callsign );
659 if (waitList.end() != iter)
660 waitList.erase( iter );
665 clt->tcp_sock.disconnect( __PRETTY_FUNCTION__ );
666 COUT<<
"Client "<<clt->callsign<<
" disconnected"<<endl;
667 COUT<<
"There were "<<allClients.size()<<
" clients - ";
668 allClients.remove( clt );
673 un->
Kill(
true,
true );
679 unused_players.push( i );
684 COUT<<allClients.size()<<
" clients left"<<endl;