diff options
| author | Stijn Buys <ingar@osirion.org> | 2012-01-14 13:11:57 +0000 | 
|---|---|---|
| committer | Stijn Buys <ingar@osirion.org> | 2012-01-14 13:11:57 +0000 | 
| commit | 9a0b0432cbdb0bfc2578b74d305a091d56152839 (patch) | |
| tree | e81b00cf0b8b34f5644b989903398ab0a0dd8b06 /src/game | |
| parent | 8ecbe636dd7e4930265614152914be6cb7b42583 (diff) | |
Moved server-side savegame functions into a separate class.
Diffstat (limited to 'src/game')
| -rw-r--r-- | src/game/base/Makefile.am | 2 | ||||
| -rw-r--r-- | src/game/base/game.cc | 68 | ||||
| -rw-r--r-- | src/game/base/savegame.cc | 83 | ||||
| -rw-r--r-- | src/game/base/savegame.h | 46 | 
4 files changed, 146 insertions, 53 deletions
| diff --git a/src/game/base/Makefile.am b/src/game/base/Makefile.am index a64f73f..89c87d3 100644 --- a/src/game/base/Makefile.am +++ b/src/game/base/Makefile.am @@ -12,6 +12,7 @@ noinst_HEADERS = \  	navpoint.h \  	planet.h \  	racetrack.h \ +	savegame.h \  	ship.h \  	shipmodel.h \  	star.h \ @@ -27,6 +28,7 @@ libbase_la_SOURCES = \  	navpoint.cc \  	planet.cc \  	racetrack.cc \ +	savegame.cc \  	ship.cc \  	shipmodel.cc \  	star.cc \ diff --git a/src/game/base/game.cc b/src/game/base/game.cc index a91ba1d..1c68d11 100644 --- a/src/game/base/game.cc +++ b/src/game/base/game.cc @@ -22,6 +22,7 @@  #include "base/navpoint.h"  #include "base/jumppoint.h"  #include "base/planet.h" +#include "base/savegame.h"  #include "base/station.h"  #include "base/racetrack.h"  #include "base/ship.h" @@ -1964,19 +1965,21 @@ void Game::player_save(core::Player *player)  		std::string guid(player->guid().str());  		std::string directory(guid.substr(0,4)); -		std::string filename(filesystem::writedir());  		// create players/ directory +		std::string filename(filesystem::writedir());  		filename.append("players");  		if (!sys::directory_exists(filename)) {  			sys::mkdir(filename);  		}  		filename += '/'; +		  		// second level directory  		filename.append(directory);  		if (!sys::directory_exists(filename)) {  			sys::mkdir(filename);  		}  		filename += '/'; +		  		// guid.ini  		filename.append(guid);  		filename.append(".ini"); @@ -1987,65 +1990,24 @@ void Game::player_save(core::Player *player)  			return;  		} -		/* ---- HEADER ----------------------------------------------- */	 +		// save header  		ofs << "; " << guid << ".ini" << std::endl;  		ofs << "; Project::OSiRiON player data" << std::endl; -		 -		/* ---- PLAYER ----------------------------------------------- */  		ofs << std::endl; -		ofs << "[player]" << std::endl;		 -		// player name -		ofs << "name=" << player->name() << std::endl;		 -		// credit -		ofs << "credits=" << player->credits() << std::endl;		 -		/* ---- SHIP ------------------------------------------------- */		 -		Ship * ship = static_cast<Ship *>(player->control()); -		ofs << std::endl; -		ofs << "[ship]" << std::endl; -		ofs << "model=" << ship->shipmodel()->label() << std::endl; -		// ship zone -		ofs << "zone=" << ship->zone()->label()<< std::endl; -		// ship location -		ofs << "location=" << ship->location() << std::endl; -		// ship orientation -		ofs << "forward=" << ship->axis().forward() << std::endl; -		ofs << "left=" << ship->axis().left() << std::endl; -		ofs << "up=" << ship->axis().up() << std::endl; -		// ship is docked at spawn on load -		ofs << "docked="; -		if (ship->dock() && (ship->dock() == ship->spawn())) { -			ofs << "yes"; -		} else if (ship->state() == core::Entity::Destroyed) { -			ofs << "yes"; -		} else { -			ofs << "no"; -		} -		ofs << std::endl; -		// ship spawn -		ofs << "spawn="; -		if (ship->spawn()) { -			ofs << ship->spawn()->zone()->label() << ":" << ship->spawn()->label(); -		} -		ofs << std::endl; +		// save player data +		SaveGame::player_to_stream(player, ofs); -		/* ---- INVENTORY -------------------------------------------- */ -		assert (ship->inventory()); -		for (core::Inventory::Items::iterator it = ship->inventory()->items().begin();  -			it != ship->inventory()->items().end(); ++it)  -		{ -			core::Item *item = (*it); +		// save ship +		// TODO iterate assets and save all ships +		if (player->control()) { +			SaveGame::ship_to_stream(static_cast<Ship *>(player->control()), ofs); -			ofs << std::endl; -			ofs << "[item]" << std::endl; -			// item InfoType label -			ofs << "type=" << item->info()->type()->label() << std::endl; -			// item Info label -			ofs << "label=" << item->info()->label() << std::endl; -			ofs << "amount=" << item->amount() << std::endl; -			ofs << "price=" << item->price() << std::endl; -			ofs << "tradeable=" << (item->flag_is_set(core::Item::Tradeable) ? "yes" : "no") << std::endl; +			assert(player->control()->inventory()); +			SaveGame::inventory_to_stream(player->control()->inventory(), ofs);  		} + +		// close output stream  		ofs.close();  	}  } diff --git a/src/game/base/savegame.cc b/src/game/base/savegame.cc new file mode 100644 index 0000000..d9a7c4f --- /dev/null +++ b/src/game/base/savegame.cc @@ -0,0 +1,83 @@ +/* +   base/savegame.cc +   This file is part of the Osirion project and is distributed under +   the terms and conditions of the GNU General Public License version 2 +*/ + +#include "base/savegame.h" +#include "base/ship.h" + +namespace game { +	 +void SaveGame::player_to_stream(core::Player *player, std::ostream & os) +{ +	if (!os.good()) +		return; +	 +	os << "[player]" << std::endl; +	// player name +	os << "name=" << player->name() << std::endl; +	// credit +	os << "credits=" << player->credits() << std::endl; +	os << std::endl; +} + +void SaveGame::ship_to_stream(Ship *ship, std::ostream & os) +{ +	if (!os.good()) +		return; +	 +	os << "[ship]" << std::endl; +	os << "model=" << ship->shipmodel()->label() << std::endl; +	// ship zone +	os << "zone=" << ship->zone()->label()<< std::endl; +	// ship location +	os << "location=" << ship->location() << std::endl; +	// ship orientation +	os << "forward=" << ship->axis().forward() << std::endl; +	os << "left=" << ship->axis().left() << std::endl; +	os << "up=" << ship->axis().up() << std::endl; +	// ship is docked at spawn on load +	os << "docked="; +	if (ship->dock() && (ship->dock() == ship->spawn())) { +		os << "yes"; +	} else if (ship->state() == core::Entity::Destroyed) { +		os << "yes"; +	} else { +		os << "no"; +	} +	os << std::endl; +	// ship spawn +	os << "spawn="; +	if (ship->spawn()) { +		os << ship->spawn()->zone()->label() << ":" << ship->spawn()->label(); +	} +	os << std::endl; +} + +void SaveGame::inventory_to_stream(core::Inventory *inventory, std::ostream & os) +{ +	if (!os.good()) +		return; +	 +	if(!inventory) +		return; +	 +	for (core::Inventory::Items::iterator it = inventory->items().begin();  +			it != inventory->items().end(); ++it)  +	{ +		const core::Item *item = (*it); +		 +		os << std::endl; +		os << "[item]" << std::endl; +		// item InfoType label +		os << "type=" << item->info()->type()->label() << std::endl; +		// item Info label +		os << "label=" << item->info()->label() << std::endl; +		os << "amount=" << item->amount() << std::endl; +		os << "price=" << item->price() << std::endl; +		os << "tradeable=" << (item->flag_is_set(core::Item::Tradeable) ? "yes" : "no") << std::endl; +	} +} + +} // namespace game diff --git a/src/game/base/savegame.h b/src/game/base/savegame.h new file mode 100644 index 0000000..1f56ebc --- /dev/null +++ b/src/game/base/savegame.h @@ -0,0 +1,46 @@ +/* +   base/savegame.h +   This file is part of the Osirion project and is distributed under +   the terms and conditions of the GNU General Public License version 2 +*/ + +#ifndef __INCLUDED_BASE_SAVEGAME_H__ +#define __INCLUDED_BASE_SAVEGAME_H__ + +// system headers +#include <iostream> + +// project headers +#include "core/info.h" +#include "core/player.h" +#include "base/ship.h" + +namespace game +{ + +/** + * @brief a class containing helper functions for savegames + */  +class SaveGame { +public: +	/** +	 * @brief write player data to output stream, in .ini format +	 */ +	static void player_to_stream(core::Player *player, std::ostream & os); +	 +	/** +	 * @brief write ship data to output stream, in .ini format +	 */ +	static void ship_to_stream(Ship *ship, std::ostream & os); +	 +	/** +	 * @brief write inventory data to output stream, in .ini format +	 */ +	static void inventory_to_stream(core::Inventory *inventory, std::ostream & os); + +	 +}; // class SaveGame + +} // namespace game + +#endif // __INCLUDED_BASE_SAVEGAME_H__ | 
