bool NetServer::loadFromSavegame( ClientPtr clt, Cockpit *cp ) { ObjSerial cltserial = getUniqueSerial(); QVector tmpvec( 0, 0, 0 ); bool update = true; float credits; vector< string >savedships; string str( "" ); COUT<<"-> LOADING SAVE FROM NETWORK"<<endl; cp->savegame->SetStarSystem( string() ); cp->savegame->ParseSaveGame( "", str, "", tmpvec, update, credits, savedships, cltserial, clt->savegame[0], false ); //Generate the system we enter in if needed and add the client in it if ( savedships.empty() ) { COUT<<"There are no saved ships... corrupted save file for "<<clt->callsign<<endl; std::string logoutnetbuf; addSimpleChar( logoutnetbuf, ACCT_LOGOUT ); addSimpleString( logoutnetbuf, clt->callsign ); addSimpleString( logoutnetbuf, clt->passwd ); //We can't find the unit saved for player -> send a login error this->logout( clt ); return false; } COUT<<"\tcredits = "<<credits<<endl; COUT<<"\tfaction = "<<cp->savegame->GetPlayerFaction()<<endl; COUT<<"-> SAVE LOADED"<<endl; //WARNING : WE DON'T SAVE FACTION NOR FLIGHTGROUP YET COUT<<"-> UNIT FACTORY WITH XML"<<endl; //We may have to determine which is the current ship of the player if we handle several ships for one player string PLAYER_SHIPNAME = savedships[0]; string PLAYER_FACTION_STRING = cp->savegame->GetPlayerFaction(); int saved_faction = FactionUtil::GetFactionIndex( PLAYER_FACTION_STRING ); //vector<vector <string> > path = lookforUnit( savedships[0].c_str(), saved_faction, false); bool exist = true; //(VSFileSystem::LookForFile( savedships[0], VSFileSystem::UnitFile)<=VSFileSystem::Ok); static std::string loadfailed( "LOAD_FAILED" ); Unit *un = NULL; if ( !PLAYER_SHIPNAME.empty() ) { un = UnitFactory::createUnit( PLAYER_SHIPNAME.c_str(), false, saved_faction, string( "" ), Flightgroup::newFlightgroup( clt->callsign, PLAYER_SHIPNAME, PLAYER_FACTION_STRING, "default", 1, 1, "", "", mission ), 0, &clt->savegame[1] ); } if (!un) { exist = false; } else if (un->name == loadfailed) { exist = false; un->Kill(); } if (!exist) { unsigned short serial = cltserial; std::string logoutnetbuf; addSimpleChar( logoutnetbuf, ACCT_LOGOUT ); addSimpleString( logoutnetbuf, clt->callsign ); addSimpleString( logoutnetbuf, clt->passwd ); //We can't find the unit saved for player -> send a login error this->logout( clt ); Packet p2; //Send the account server a logout info if ( !acct_sock->sendstr( logoutnetbuf ) ) COUT<<"ERROR sending LOGOUT to account server"<<endl; cerr<<"WARNING : Unit file ("<<savedships[0]<<") not found for "<<clt->callsign<<endl; return cp; } COUT<<"\tAFTER UNIT FACTORY WITH XML"<<endl; clt->game_unit.SetUnit( un ); //Assign its serial to client* un->SetSerial( cltserial ); un->SetPosAndCumPos( cp->savegame->GetPlayerLocation() ); //Affect the created unit to the cockpit COUT<<"-> UNIT LOADED"<<endl; cp->SetParent( un, PLAYER_SHIPNAME.c_str(), "", tmpvec ); COUT<<"-> COCKPIT AFFECTED TO UNIT"<<endl; COUT<<"SHIP -- "<<savedships[0]<<" -- LOCATION: x="<<tmpvec.i<<",y="<<tmpvec.j<<",z="<<tmpvec.k<<endl; return true; }
Unit* Mission::call_unit_launch( CreateFlightgroup *fg, int type, const string &destinations ) { int faction_nr = FactionUtil::GetFactionIndex( fg->fg->faction ); Unit **units = new Unit*[fg->nr_ships]; int u; Unit *par = _Universe->AccessCockpit()->GetParent(); CollideMap::iterator metahint[2] = { _Universe->scriptStarSystem()->collidemap[Unit::UNIT_ONLY]->begin(), _Universe->scriptStarSystem()->collidemap[Unit::UNIT_BOLT]->begin() }; CollideMap::iterator *hint = metahint; if ( par && !is_null( par->location[Unit::UNIT_ONLY] ) && !is_null( par->location[Unit::UNIT_BOLT] ) && par->activeStarSystem == _Universe->scriptStarSystem() ) hint = par->location; for (u = 0; u < fg->nr_ships; u++) { Unit *my_unit; if (type == PLANETPTR) { float radius = 1; char *tex = strdup( fg->fg->type.c_str() ); char *nam = strdup( fg->fg->type.c_str() ); char *bsrc = strdup( fg->fg->type.c_str() ); char *bdst = strdup( fg->fg->type.c_str() ); char *citylights = strdup( fg->fg->type.c_str() ); tex[0] = '\0'; bsrc[0] = '\0'; //have at least 1 char bdst[0] = '\0'; citylights[0] = '\0'; GFXMaterial mat; GFXGetMaterial( 0, mat ); BLENDFUNC s = ONE; BLENDFUNC d = ZERO; if (bdst[0] != '\0') d = parse_alpha( bdst ); if (bsrc[0] != '\0') s = parse_alpha( bsrc ); my_unit = UnitFactory::createPlanet( QVector( 0, 0, 0 ), QVector( 0, 0, 0 ), 0, Vector( 0, 0, 0 ), 0, 0, radius, tex, "", "", s, d, ParseDestinations( destinations ), QVector( 0, 0, 0 ), NULL, mat, vector< GFXLightLocal > (), faction_nr, nam, getUniqueSerial() ); free( bsrc ); free( bdst ); free( tex ); free( nam ); free( citylights ); } else if (type == NEBULAPTR) { my_unit = UnitFactory::createNebula( fg->fg->type.c_str(), false, faction_nr, fg->fg, u+fg->fg->nr_ships-fg->nr_ships, getUniqueSerial() ); } else if (type == ASTEROIDPTR) { my_unit = UnitFactory::createAsteroid( fg->fg->type.c_str(), faction_nr, fg->fg, u+fg->fg->nr_ships-fg->nr_ships, .01, getUniqueSerial() ); } else { my_unit = UnitFactory::createUnit( fg->fg->type.c_str(), false, faction_nr, string( "" ), fg->fg, u+fg->fg->nr_ships-fg->nr_ships, NULL, getUniqueSerial() ); } units[u] = my_unit; } float fg_radius = units[0]->rSize(); Unit *my_unit; for (u = 0; u < fg->nr_ships; u++) { my_unit = units[u]; QVector pox; pox.i = fg->fg->pos.i+u*fg_radius*3; pox.j = fg->fg->pos.j+u*fg_radius*3; pox.k = fg->fg->pos.k+u*fg_radius*3; my_unit->SetPosAndCumPos( pox ); if (type == ASTEROIDPTR || type == NEBULAPTR) { my_unit->PrimeOrders(); } else { my_unit->LoadAIScript( fg->fg->ainame ); my_unit->SetTurretAI(); } _Universe->scriptStarSystem()->AddUnit( my_unit ); my_unit->UpdateCollideQueue( _Universe->scriptStarSystem(), hint ); if ( !is_null( my_unit->location[Unit::UNIT_ONLY] ) && !is_null( my_unit->location[Unit::UNIT_BOLT] ) ) hint = my_unit->location; my_unit->Target( NULL ); } my_unit = units[0]; if ( !_Universe->isPlayerStarship( fg->fg->leader.GetUnit() ) ) fg->fg->leader.SetUnit( my_unit ); delete[] units; return my_unit; }
bool NetServer::loadFromNewGame( ClientPtr clt, Cockpit *cp, string fighter ) { ObjSerial cltserial = getUniqueSerial(); string PLAYER_SHIPNAME = fighter; Mission *mission = NULL; if (active_missions.size() > 0) mission = active_missions[0]; if (!mission) { COUT<<"Cannot login player without acctserver: No missions available"; sendLoginError( clt ); return false; } int saved_faction = 0; //NETFIXME: Send faction over network too! cp->savegame->SetSavedCredits( XMLSupport::parse_float( mission->getVariable( "credits", "0" ) ) ); cp->savegame->SetStarSystem( mission->getVariable( "system", "Sol/Sol" ) ); if ( !mission->flightgroups.empty() ) cp->savegame->SetPlayerFaction( mission->flightgroups[0]->faction ); COUT<<"\tcredits = "<<cp->savegame->GetSavedCredits()<<endl; COUT<<"\tfaction = "<<cp->savegame->GetPlayerFaction()<<endl; COUT<<"-> SAVE LOADED"<<endl; cp->credits = cp->savegame->GetSavedCredits(); //WARNING : WE DON'T SAVE FACTION NOR FLIGHTGROUP YET COUT<<"-> UNIT FACTORY WITH XML"<<endl; //We may have to determine which is the current ship of the player if we handle several ships for one player string PLAYER_FACTION_STRING = cp->savegame->GetPlayerFaction(); saved_faction = FactionUtil::GetFactionIndex( PLAYER_FACTION_STRING ); bool exist = true; //(VSFileSystem::LookForFile( savedships[0], VSFileSystem::UnitFile)<=VSFileSystem::Ok); static std::string loadfailed( "LOAD_FAILED" ); Unit *un = NULL; if ( !PLAYER_SHIPNAME.empty() ) { un = UnitFactory::createUnit( PLAYER_SHIPNAME.c_str(), false, saved_faction, string( "" ), Flightgroup::newFlightgroup( clt->callsign, PLAYER_SHIPNAME, PLAYER_FACTION_STRING, "default", 1, 1, "", "", mission ), 0 ); } if (!un) { exist = false; } else if (un->name == loadfailed) { exist = false; un->Kill(); } if (!exist) { //We can't find the unit saved for player -> send a login error this->sendLoginError( clt ); cerr<<"WARNING : Unit file ("<<PLAYER_SHIPNAME<<") not found for "<<clt->callsign<<endl; return false; } COUT<<"\tAFTER UNIT FACTORY WITH XML"<<endl; clt->game_unit.SetUnit( un ); //Assign its serial to client* un->SetSerial( cltserial ); un->PrimeOrders(); //Accept Comm messages //Affect the created unit to the cockpit COUT<<"-> UNIT LOADED"<<endl; QVector tmpvec( 0, 0, 0 ); cp->SetParent( un, PLAYER_SHIPNAME.c_str(), "", tmpvec ); COUT<<"-> COCKPIT AFFECTED TO UNIT"<<endl; { string savestr, xmlstr; clt->savegame.resize( 0 ); //Get the save parts in a string array cp->activeStarSystem = zonemgr->addZone( cp->savegame->GetStarSystem() ); //Needed for GetSaveStrings. SaveNetUtil::GetSaveStrings( clt, savestr, xmlstr, true ); clt->savegame.push_back( savestr ); clt->savegame.push_back( xmlstr ); } return true; }