From ada8263817ed45e29d4bd63ab0ac635a83eec4f8 Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Sat, 8 Mar 2008 13:13:37 +0000 Subject: game reads world.ini --- src/core/model.cc | 4 ++ src/core/model.h | 1 + src/filesystem/Makefile.am | 2 +- src/filesystem/inifile.cc | 59 +++++++++++++------- src/filesystem/inifile.h | 39 +++++++++----- src/game/game.cc | 132 +++++++++++++++++++++++++++++++++++++-------- 6 files changed, 180 insertions(+), 57 deletions(-) (limited to 'src') diff --git a/src/core/model.cc b/src/core/model.cc index 3b1cabd..1e571c3 100644 --- a/src/core/model.cc +++ b/src/core/model.cc @@ -69,6 +69,7 @@ std::map Model::registry; Model::Model(std::string const & name) : model_name(name) { + model_valid = false; model_scale = 1.0f / 1024.0f; std::string fn("maps/"); @@ -214,6 +215,9 @@ Model::Model(std::string const & name) : ifs.close(); + if (model_face.size()) { + model_valid = true; + } con_debug << " maps/" << name << ".map " << model_face.size() << " polygons\n"; } diff --git a/src/core/model.h b/src/core/model.h index e22d87a..b273c46 100644 --- a/src/core/model.h +++ b/src/core/model.h @@ -103,6 +103,7 @@ private: std::string model_name; float model_scale; + bool model_valid; }; diff --git a/src/filesystem/Makefile.am b/src/filesystem/Makefile.am index 20cd25a..0793f61 100644 --- a/src/filesystem/Makefile.am +++ b/src/filesystem/Makefile.am @@ -1,6 +1,6 @@ METASOURCES = AUTO -libfilesystem_la_SOURCES = diskfile.cc file.cc filesystem.cc +libfilesystem_la_SOURCES = diskfile.cc file.cc filesystem.cc inifile.cc libfilesystem_la_LDFLAGS = -avoid-version -no-undefined libfilesystem_la_LIBADD = $(top_builddir)/src/sys/libsys.la noinst_LTLIBRARIES = libfilesystem.la diff --git a/src/filesystem/inifile.cc b/src/filesystem/inifile.cc index f04d116..c76e06a 100644 --- a/src/filesystem/inifile.cc +++ b/src/filesystem/inifile.cc @@ -5,17 +5,17 @@ */ // project headers +#include "filesystem/filesystem.h" #include "filesystem/inifile.h" -namespace filesystem -{ +namespace filesystem { IniFile::IniFile() {} IniFile::~IniFile() {} -void IniFile::open(const char * filename, std::ios_base::openmode mode) -{ +void IniFile::open(std::string const & name) { + last_read_was_section = false; last_read_was_key = false; key_current = ""; @@ -23,22 +23,36 @@ void IniFile::open(const char * filename, std::ios_base::openmode mode) section_current = ""; line_number = 0; - File::open(filename, mode); + std::string inifile_name("ini/"); + inifile_name.append(name); + inifile_name.append(".ini"); + + filesystem::File *f = filesystem::open(inifile_name.c_str()); + if (!f) { + return; + } + + std::string fn = f->path(); + fn.append(f->name()); + filesystem::close(f); + + inifile_ifs.open(fn.c_str()); + if (!inifile_ifs.is_open()) { + con_warn << "Could not stream " << fn << "!\n"; + return; + } } -bool IniFile::got_section() const -{ +bool IniFile::got_section() const { return last_read_was_section; } -bool IniFile::got_section(const char * sectionlabel) const -{ +bool IniFile::got_section(const char * sectionlabel) const { return (last_read_was_section && section_current == sectionlabel); } -IniFile & IniFile::getline() -{ +bool IniFile::getline() { char line[1024]; last_read_was_section = false; @@ -46,8 +60,10 @@ IniFile & IniFile::getline() key_current = ""; value_current = ""; - while ((*this)) { - File::getline(line, 1024); + if (!inifile_ifs.is_open()) + return false; + + if (inifile_ifs.getline(line, 1024)) { std::string s(line); line_number++; @@ -63,7 +79,7 @@ IniFile & IniFile::getline() // condebug << "Inifile got section header " << s << std::endl; section_current = s.substr(1, s.size()-2); last_read_was_section = true; - break; + return true; } else { // key=value pair size_t found = s.find('='); @@ -74,18 +90,17 @@ IniFile & IniFile::getline() if (key_current.size() > 0) { value_current = s.substr(found+1, s.size() - found - 1); last_read_was_key = true; - break; + return true; } } } - } - - return (*this); + return true; + } else + return false; } -bool IniFile::got_key_string(char * const keylabel, std::string & valuestring) -{ +bool IniFile::got_key_string(char * const keylabel, std::string & valuestring) { //condebug << "IniFile got_value_string " << keylabel << " " << last_read_was_key << std::endl; if (last_read_was_key && key_current == keylabel) { valuestring.assign(value_current); @@ -95,6 +110,10 @@ bool IniFile::got_key_string(char * const keylabel, std::string & valuestring) } } +void IniFile::close() +{ + inifile_ifs.close(); +} } // namespace filesystem diff --git a/src/filesystem/inifile.h b/src/filesystem/inifile.h index d3984c5..ba7eb6a 100644 --- a/src/filesystem/inifile.h +++ b/src/filesystem/inifile.h @@ -7,13 +7,11 @@ #ifndef __INCLUDED_FILESYSTEM_INIFILE_H__ #define __INCLUDED_FILESYSTEM_INIFILE_H__ -// project headers -#include "filesystem/file.h" - -// C++ headers #include #include +#include "filesystem/file.h" + namespace filesystem { /// a class to read .ini files @@ -21,15 +19,17 @@ namespace filesystem { * consists of one or more [section] headers followed by one or more key=value * pairs. Lines starting with # or ; are considered comments */ -class IniFile : public File { +class IniFile { public: IniFile(); virtual ~IniFile(); /// open the file for reading - virtual void open(const char * filename, std::ios_base::openmode mode = std::ios_base::in); + /** the filename will get the "ini/" prefix and ".ini" suffix + */ + virtual void open(std::string const & name); - IniFile & getline(); + bool getline(); /// current section label inline std::string section() const { @@ -48,6 +48,7 @@ public: /// true if the last read statement was a section header bool got_section() const; + /// true if the last read statement was a certain section header bool got_section(const char * sectionlabel) const; @@ -55,6 +56,7 @@ public: inline bool got_key() const { return last_read_was_key; } + /// check if the last read key=value pair matches keylabel and store the value in valuestring bool got_key_string(char * const keylabel, std::string & valuestring); @@ -62,15 +64,26 @@ public: return line_number; } + /// return true of the ini file is open for reading + inline bool is_open() const { return inifile_ifs.is_open(); } + + /// current filename + inline std::string const & name() const {return inifile_name; } + + /// close the file + void close(); + private: - std::string section_current; - std::string key_current; - std::string value_current; + std::string section_current; + std::string key_current; + std::string value_current; - bool last_read_was_key; - bool last_read_was_section; + bool last_read_was_key; + bool last_read_was_section; - unsigned int line_number; + unsigned int line_number; + std::ifstream inifile_ifs; + std::string inifile_name; }; } diff --git a/src/game/game.cc b/src/game/game.cc index 77e534f..f0fd190 100644 --- a/src/game/game.cc +++ b/src/game/game.cc @@ -13,7 +13,7 @@ #include "sys/sys.h" #include "math/mathlib.h" #include "filesystem/filesystem.h" - +#include "filesystem/inifile.h" namespace game { @@ -25,7 +25,7 @@ void func_join(core::Player *player, std::string const &args) if (player->control()) return; - player->player_control = new Ship(player, "micron_vector"); + player->player_control = new Ship(player, "vector"); player->control()->entity_color = player->color(); std::string message(player->name()); @@ -64,7 +64,7 @@ void func_buy(core::Player *player, std::string const &args) std::string shipname; std::istringstream is(args); is >> shipname; - if ((shipname == "micron_vector") || (shipname == "canasta")) { + if ((shipname == "vector") || (shipname == "canasta")) { // player has only ship for now player->player_control->die(); player->player_control = 0; @@ -75,7 +75,7 @@ void func_buy(core::Player *player, std::string const &args) core::server()->broadcast(player->name() + " purchased a " + shipname); player->player_dirty = true; } else { - core::server()->send(player, "Usage: buy "); + core::server()->send(player, "Usage: buy "); } } /*----- Game ------------------------------------------------------ */ @@ -96,11 +96,89 @@ void Game::init() module_running = false; // setup the game world - - // the star - Star *star = new Star(); - star->entity_location = Vector3f(256.0f, -256.0f, 0.0f); - star->entity_name = "star: Sabishi Hoshi"; + filesystem::IniFile f; + f.open("world"); + + if (!f.is_open()) { + return; + } + + Star *star = 0; + core::Entity *entity = 0; + std::string tmp; + + while (f.getline()) { + if (f.got_key()) { + if (f.section() == "star") { + + if (f.got_key_string("name", tmp)) { + star->entity_name = tmp; + + } else if (f.got_key_string("model", tmp)) { + star->entity_modelname = tmp; + + } else if (f.got_key_string("location", tmp)) { + std::istringstream is(tmp); + float x, y, z; + if ((is >> x) && (is >> y) && (is >> z)) { + star->entity_location = math::Vector3f(x,y,z); + } + + } else if (f.got_key_string("color", tmp)) { + std::istringstream is(tmp); + float r, g, b; + if ((is >> r) && (is >> g) && (is >> b)) { + star->entity_color = math::Color(r, g, b); + } + } else + con_warn << f.name() << " unknown key '" << f.key() << "' at line " << f.line() << std::endl; + + } else if (f.section() == "entity") { + + if (f.got_key_string("name", tmp)) { + entity->entity_name = tmp; + + } else if (f.got_key_string("model", tmp)) { + entity->entity_modelname = tmp; + + } else if (f.got_key_string("direction", tmp)) { + std::istringstream is(tmp); + float a; + if (is >> a) { + entity->entity_direction = math::degrees360f(a); + } + + } else if (f.got_key_string("location", tmp)) { + std::istringstream is(tmp); + float x, y, z; + if ((is >> x) && (is >> y) && (is >> z)) { + entity->entity_location = math::Vector3f(x,y,z); + } + + } else if (f.got_key_string("color", tmp)) { + std::istringstream is(tmp); + float r, g, b; + if ((is >> r) && (is >> g) && (is >> b)) { + entity->entity_color = math::Color(r, g, b); + } + + } else { + con_warn << f.name() << " unknown key '" << f.key() << "' at line " << f.line() << std::endl; + } + } + } else if (f.got_section("star")) { + //con_debug << "[star] section" << std::endl; + star = new Star(); + + } else if (f.got_section("entity")) { + //con_debug << "[entity] section" << std::endl; + entity = new core::Entity(); + + } else if (f.got_section()) { + con_warn << f.name() << " unknown section '" << f.section() << "' at line " << f.line() << std::endl; + } + } + f.close(); // the green cube core::Entity *cube = new core::Entity(core::Entity::Solid & core::Entity::Static); @@ -120,6 +198,27 @@ void Game::init() cube->entity_modelname = "cube"; cube->entity_moduletypeid = cube_enttype; + // the yellow sphere + core::Entity *sphere = new core::Entity(core::Entity::Solid & core::Entity::Static); + sphere->entity_shape = core::Entity::Sphere; + sphere->entity_color = Color(0.8f, 0.8f, 0.0f); + sphere->entity_location = Vector3f(0.0f, 32.0f, 0.0f); + sphere->entity_name ="sphere: The Sphere"; + + // the galactic origin + core::Entity *axis = new core::Entity(core::Entity::Static); + axis->entity_shape = core::Entity::Diamond; + axis->entity_color = Color(1.0f, 1.0f, 0.0f); + axis->entity_location = Vector3f(0, 0, 0); + axis->entity_name = "axis: Origin"; + + /* + + // the star + Star *star = new Star(); + star->entity_location = Vector3f(256.0f, -256.0f, 0.0f); + star->entity_name = "star: Sabishi Hoshi"; + // Canasta cube = new core::Entity(core::Entity::Solid & core::Entity::Static); cube->entity_shape = core::Entity::Diamond; @@ -141,20 +240,7 @@ void Game::init() alexandria->entity_location = Vector3f(0.0f, -64.0f, 0.0f); alexandria->entity_name = "station: Alexandria"; alexandria->entity_modelname = "stations/alexandria"; - - // the yellow sphere - core::Entity *sphere = new core::Entity(core::Entity::Solid & core::Entity::Static); - sphere->entity_shape = core::Entity::Sphere; - sphere->entity_color = Color(0.8f, 0.8f, 0.0f); - sphere->entity_location = Vector3f(0.0f, 32.0f, 0.0f); - sphere->entity_name ="sphere: The Sphere"; - - // the galactic origin - core::Entity *axis = new core::Entity(core::Entity::Static); - axis->entity_shape = core::Entity::Diamond; - axis->entity_color = Color(1.0f, 1.0f, 0.0f); - axis->entity_location = Vector3f(0, 0, 0); - axis->entity_name = "axis: Origin"; + */ // add engine game functions core::Func::add("buy", (core::GameFuncPtr) func_buy); -- cgit v1.2.3