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
basecomputer.h File Reference

Go to the source code of this file.

Classes

class  BaseComputer
 
struct  BaseComputer::TransactionList
 
class  BaseComputer::LoadSaveQuitConfirm
 

Functions

bool buyShip (Unit *base, Unit *player, std::string, bool myfleet, bool force_base_inventory, BaseComputer *)
 
bool sellShip (Unit *base, Unit *player, std::string, BaseComputer *)
 

Function Documentation

bool buyShip ( Unit base,
Unit player,
std::string  ,
bool  myfleet,
bool  force_base_inventory,
BaseComputer  
)

Definition at line 4491 of file basecomputer.cpp.

References _Universe, Universe::AccessCockpit(), Universe::activeStarSystem(), StarSystem::AddUnit(), Subcmd::BuyShip, c, Window::close(), CreateCargoForOwnerStarshipName(), UnitFactory::createUnit(), Cockpit::credits, Unit::curr_physical_state, CurrentBaseUnitSet(), CurrentSaveGameName, Unit::faction, Unit::GetCargo(), Cargo::GetCategory(), Cargo::GetContent(), Unit::getFlightgroup(), UniverseUtil::GetMasterPartList(), Cockpit::GetNumUnits(), Cockpit::GetUnitFileName(), Cockpit::GetUnitModifications(), VegaConfig::getVariable(), i, j, k, Unit::Kill(), LOAD_FAILED, SaveGame::LoadSavedMissions(), BaseComputer::m_player, Cargo::mission, Universe::netLocked(), Network, Flightgroup::nr_ships, Flightgroup::nr_ships_left, Unit::numCargo(), XMLSupport::parse_bool(), UniverseUtil::playSound(), Transformation::position, Unit::Position(), Unit::prev_physical_state, Cargo::price, Cargo::quantity, QVector, Unit::RemoveCargo(), UniverseUtil::SafeEntrancePoint(), Cockpit::savegame, Cockpit::SetParent(), UnitContainer::SetUnit(), NetClient::shipRequest(), UniverseUtil::StopAllSounds(), SwapInNewShipName(), Subcmd::SwitchShip, SwitchUnits(), Unit::UnDock(), Vector, vs_config, WindowController::window(), and WriteSaveGame().

Referenced by BaseUtil::BuyShip().

4497 {
4498  unsigned int tempInt; //Not used.
4499  Cargo *shipCargo = baseUnit->GetCargo( content, tempInt );
4500  if (shipCargo == NULL && force_base_inventory)
4501  shipCargo = UniverseUtil::GetMasterPartList()->GetCargo( content, tempInt );
4502  Cargo myFleetShipCargo;
4503  int swappingShipsIndex = -1;
4504  if (myfleet) {
4505  //Player owns this starship.
4506  shipCargo = &myFleetShipCargo;
4507  myFleetShipCargo = CreateCargoForOwnerStarshipName( _Universe->AccessCockpit(), baseUnit, content, swappingShipsIndex );
4508  if ( shipCargo->GetContent().empty() ) {
4509  //Something happened -- can't find ship by name.
4510  shipCargo = NULL;
4511  swappingShipsIndex = -1;
4512  }
4513  } else {
4514  Cockpit *cockpit = _Universe->AccessCockpit();
4515  for (size_t i = 1, n = cockpit->GetNumUnits(); i < n; ++i)
4516  if (cockpit->GetUnitFileName(i) == content)
4517  return false;
4518  //can't buy a ship you own
4519  }
4520  if (shipCargo) {
4521  if (shipCargo->price < _Universe->AccessCockpit()->credits) {
4522  if ( Network && !_Universe->netLocked() ) {
4523  Network[0].shipRequest( content, myfleet ? Subcmd::SwitchShip : Subcmd::BuyShip );
4524  return false;
4525  }
4526  Flightgroup *flightGroup = playerUnit->getFlightgroup();
4527  int fgsNumber = 0;
4528  if (flightGroup != NULL) {
4529  fgsNumber = flightGroup->nr_ships;
4530  flightGroup->nr_ships++;
4531  flightGroup->nr_ships_left++;
4532  }
4533  string newModifications;
4534  std::string tmpnam = CurrentSaveGameName;
4535  if (swappingShipsIndex != -1) {
4536  //if we're swapping not buying load the olde one
4537  newModifications = _Universe->AccessCockpit()->GetUnitModifications();
4539  }
4540  WriteSaveGame( _Universe->AccessCockpit(), true ); //oops saved game last time at wrong place
4542  UniverseUtil::playSound( "sales/salespitch"+content.substr( 0, content.find( "." ) )+"accept.wav", QVector( 0,
4543  0,
4544  0 ),
4545  Vector( 0, 0, 0 ) );
4546  Unit *newPart =
4547  UnitFactory::createUnit( content.c_str(),
4548  false,
4549  baseUnit->faction,
4550  newModifications,
4551  flightGroup,
4552  fgsNumber );
4553  CurrentSaveGameName = tmpnam;
4554  newPart->SetFaction( playerUnit->faction );
4555  if (newPart->name != LOAD_FAILED) {
4556  if (newPart->nummesh() > 0) {
4557  _Universe->AccessCockpit()->credits -= shipCargo->price;
4558  newPart->curr_physical_state = playerUnit->curr_physical_state;
4559  newPart->SetPosAndCumPos( UniverseUtil::SafeEntrancePoint( playerUnit->Position(), newPart->rSize() ) );
4560  newPart->prev_physical_state = playerUnit->prev_physical_state;
4561  _Universe->activeStarSystem()->AddUnit( newPart );
4562  SwapInNewShipName( _Universe->AccessCockpit(), baseUnit, content, swappingShipsIndex );
4563  for (int j = 0; j < 2; ++j) {
4564  for (int i = playerUnit->numCargo()-1; i >= 0; --i) {
4565  Cargo c = playerUnit->GetCargo( i );
4566  if ( (c.mission != 0 && j == 0)
4567  || (c.mission == 0 && j == 1 && (!myfleet || Network) && c.GetCategory().find( "upgrades" ) != 0) ) {
4568  for (int k = c.quantity; k > 0; --k) {
4569  c.quantity = k;
4570  if ( newPart->CanAddCargo( c ) ) {
4571  newPart->AddCargo( c );
4572  playerUnit->RemoveCargo( i, c.quantity, true );
4573  break;
4574  }
4575  }
4576  }
4577  }
4578  }
4579  WriteSaveGame( _Universe->AccessCockpit(), true ); //oops saved game last time at wrong place
4580 
4581  _Universe->AccessCockpit()->SetParent( newPart, content.c_str(),
4583  playerUnit->curr_physical_state.position ); //absolutely NO NO NO modifications...you got this baby clean off the slate
4584 
4585  //We now put the player in space.
4586  SwitchUnits( NULL, newPart );
4587  playerUnit->UnDock( baseUnit );
4588  if (bcomputer)
4589  bcomputer->m_player.SetUnit( newPart );
4591  if (baseUnit)
4592  newPart->ForceDock( baseUnit, 0 );
4593  CurrentBaseUnitSet( newPart );
4594  if (bcomputer)
4595  bcomputer->m_player.SetUnit( newPart );
4596  static bool persistent_missions_across_ship_switch =
4597  XMLSupport::parse_bool( vs_config->getVariable( "general", "persistent_mission_across_ship_switch",
4598  "true" ) );
4599  if (persistent_missions_across_ship_switch)
4601  newPart = NULL;
4602  playerUnit->Kill();
4603  if (bcomputer)
4604  bcomputer->window()->close();
4605  return true;
4606  }
4607  }
4608  newPart->Kill();
4609  newPart = NULL;
4610  }
4611  }
4612  return false;
4613 }
bool sellShip ( Unit base,
Unit player,
std::string  ,
BaseComputer  
)

Definition at line 4443 of file basecomputer.cpp.

References _Universe, Universe::activeStarSystem(), Cockpit::credits, Unit::GetCargo(), StarSystem::getFileName(), UniverseUtil::GetMasterPartList(), Cockpit::GetNumUnits(), Cockpit::GetUnitFileName(), Cockpit::GetUnitSystemName(), VegaConfig::getVariable(), i, Universe::isPlayerStarship(), BaseComputer::loadShipDealerControls(), Universe::netLocked(), Network, XMLSupport::parse_float(), Cargo::price, Cockpit::RemoveUnit(), Subcmd::SellShip, NetClient::shipRequest(), BaseComputer::updateTransactionControlsForSelection(), and vs_config.

Referenced by BaseComputer::sellShip(), and BaseUtil::SellShip().

4444 {
4445  Cockpit *cockpit = _Universe->isPlayerStarship( playerUnit );
4446  unsigned int tempInt = 1;
4447  Cargo *shipCargo = baseUnit->GetCargo( shipname, tempInt );
4448  if (shipCargo == NULL)
4449  shipCargo = UniverseUtil::GetMasterPartList()->GetCargo( shipname, tempInt );
4450  if (shipCargo) {
4451  //now we can actually do the selling
4452  for (size_t i = 1, n = cockpit->GetNumUnits(); i < n; ++i)
4453  if (cockpit->GetUnitFileName(i) == shipname) {
4454  if ( Network && !_Universe->netLocked() ) {
4455  Network[0].shipRequest( shipname, Subcmd::SellShip );
4456  return false;
4457  }
4458  float xtra = 0;
4459  if ( cockpit->GetUnitSystemName(i) == _Universe->activeStarSystem()->getFileName() ) {
4460  static const float shipping_price =
4461  XMLSupport::parse_float( vs_config->getVariable( "physics", "sellback_shipping_price", "6000" ) );
4462  xtra += shipping_price;
4463  }
4464  cockpit->RemoveUnit(i);
4465  static float shipSellback =
4466  XMLSupport::parse_float( vs_config->getVariable( "economics", "ship_sellback_price", ".5" ) );
4467  cockpit->credits += shipSellback*shipCargo->price; //sellback cost
4468  cockpit->credits -= xtra; //transportation cost
4469  break;
4470  }
4471  if (bcomputer) {
4472  bcomputer->loadShipDealerControls();
4473  bcomputer->updateTransactionControlsForSelection( NULL );
4474  }
4475  return true;
4476  }
4477  return false;
4478 }