From 3b8ea0849fac5532d61a90608bda876cf518ba1b Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Sat, 13 Oct 2012 12:13:04 +0000 Subject: Read weapons.ini. --- src/game/base/Makefile.am | 6 +- src/game/base/game.cc | 10 +++ src/game/base/weapon.cc | 214 ++++++++++++++++++++++++++++++++++++++++++++++ src/game/base/weapon.h | 52 +++++++++++ 4 files changed, 280 insertions(+), 2 deletions(-) create mode 100644 src/game/base/weapon.cc create mode 100644 src/game/base/weapon.h (limited to 'src') diff --git a/src/game/base/Makefile.am b/src/game/base/Makefile.am index ddfa5ae..544cbb6 100644 --- a/src/game/base/Makefile.am +++ b/src/game/base/Makefile.am @@ -18,7 +18,8 @@ noinst_HEADERS = \ spacemine.h \ star.h \ station.h \ - template.h + template.h \ + weapon.h libbase_la_SOURCES = \ cargo.cc \ @@ -35,7 +36,8 @@ libbase_la_SOURCES = \ spacemine.cc \ star.cc \ station.cc \ - template.cc + template.cc \ + weapon.cc libbase_la_LDFLAGS = -avoid-version -no-undefined diff --git a/src/game/base/game.cc b/src/game/base/game.cc index dd720f0..a79f5cc 100644 --- a/src/game/base/game.cc +++ b/src/game/base/game.cc @@ -29,6 +29,7 @@ #include "base/ship.h" #include "base/star.h" #include "base/template.h" +#include "base/weapon.h" #include "math/mathlib.h" #include "sys/sys.h" @@ -939,6 +940,12 @@ Game::Game() : core::Module("Project::OSiRiON", true) return; } + // read weapons.ini + if (!Weapon::init()) { + abort(); + return; + } + // read ships.ini if (!ShipModel::init()) { abort(); @@ -1039,6 +1046,9 @@ Game::~Game() // clear Cargo Cargo::done(); + // clear Weapon + Weapon::clear(); + // clear ShipModel ShipModel::done(); diff --git a/src/game/base/weapon.cc b/src/game/base/weapon.cc new file mode 100644 index 0000000..8eca691 --- /dev/null +++ b/src/game/base/weapon.cc @@ -0,0 +1,214 @@ +/* + base/weapon.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 +#include + +#include "sys/sys.h" +#include "filesystem/inifile.h" +#include "auxiliary/functions.h" +#include "core/func.h" +#include "base/game.h" +#include "base/weapon.h" + +namespace game +{ + +core::InfoType *Weapon::weapon_infotype = 0; + +void func_list_weapons(const std::string &args) +{ + Weapon::list(); +} + +// load weapon types from ini file +bool Weapon::init() +{ + + // initialize weapon InfoType + weapon_infotype = new core::InfoType("weapon"); + + filesystem::IniFile weaponsini; + weaponsini.open("ini/weapons"); + if (!weaponsini.is_open()) { + con_error << "Could not open " << weaponsini.name() << "!" << std::endl; + return false; + } + + con_print << "^BLoading weapons..." << std::endl; + + size_t count = 0; + + Weapon *weapon = 0; + std::string str; + long l; + float f; + + while (weaponsini.getline()) { + if (weaponsini.got_key()) { + + if (weaponsini.section().compare("mine") == 0) { + if (weaponsini.got_key_label("label", str)) { + weapon->set_label(str); + continue; + + } else if (weaponsini.got_key_string("name", str)) { + weapon->set_name(str); + continue; + + } else if (weaponsini.got_key_string("info", str)) { + weapon->add_text(str); + continue; + + } else if (weaponsini.got_key_string("model", str)) { + weapon->set_modelname(str); + continue; + + } else if (weaponsini.got_key_long("price", l)) { + weapon->set_price(l); + continue; + + } else if (weaponsini.got_key_float("volume", f)) { + weapon->set_volume(f); + continue; + + } else if (weaponsini.got_key_long("level", l)) { + weapon->set_level(l); + continue; + + } else { + weaponsini.unknown_key(); + } + } else if (weaponsini.section().compare("cannon") == 0) { + if (weaponsini.got_key_label("label", str)) { + weapon->set_label(str); + continue; + + } else if (weaponsini.got_key_string("name", str)) { + weapon->set_name(str); + continue; + + } else if (weaponsini.got_key_string("info", str)) { + weapon->add_text(str); + continue; + + } else if (weaponsini.got_key_string("model", str)) { + weapon->set_modelname(str); + continue; + + } else if (weaponsini.got_key_long("price", l)) { + weapon->set_price(l); + continue; + + } else if (weaponsini.got_key_float("volume", f)) { + weapon->set_volume(f); + continue; + + } else if (weaponsini.got_key_long("level", l)) { + weapon->set_level(l); + continue; + + } else { + weaponsini.unknown_key(); + } + } else if (weaponsini.section().compare("turret") == 0) { + if (weaponsini.got_key_label("label", str)) { + weapon->set_label(str); + continue; + + } else if (weaponsini.got_key_string("name", str)) { + weapon->set_name(str); + continue; + + } else if (weaponsini.got_key_string("info", str)) { + weapon->add_text(str); + continue; + + } else if (weaponsini.got_key_string("model", str)) { + weapon->set_modelname(str); + continue; + + } else if (weaponsini.got_key_long("price", l)) { + weapon->set_price(l); + continue; + + } else if (weaponsini.got_key_float("volume", f)) { + weapon->set_volume(f); + continue; + + } else if (weaponsini.got_key_long("level", l)) { + weapon->set_level(l); + continue; + + } else { + weaponsini.unknown_key(); + } + } + + } else if (weaponsini.got_section()) { + + if (weaponsini.got_section("mine")) { + weapon = new Weapon(); + count++; + + } else if (weaponsini.got_section("cannon")) { + weapon = new Weapon(); + count++; + + } else if (weaponsini.got_section("turret")) { + weapon = new Weapon(); + count++; + + } else if (weaponsini.got_section()) { + weaponsini.unknown_section(); + } + } + } + + // add weapon infos + con_debug << " " << weaponsini.name() << " " << count << " weapon types" << std::endl; + + weaponsini.close(); + + core::Func *func = core::Func::add("list_weapons", func_list_weapons); + func->set_info("list available weapon types"); + + return true; +} + +void Weapon::done() +{ + core::Func::remove("list_weapons"); +} + +/* ---- class Weapon ------------------------------------------- */ + +Weapon::Weapon() : core::Info(weapon_infotype) +{ + set_volume(1); + set_level(1); +} + +Weapon::~Weapon() +{ +} + +Weapon *Weapon::find(const std::string & label) +{ + if (!label.size()) { + return 0; + } + + return (Weapon *) core::Info::find(weapon_infotype, label); +} + +void Weapon::list() +{ + core::Info::list(weapon_infotype); +} + +} // namespace game + diff --git a/src/game/base/weapon.h b/src/game/base/weapon.h new file mode 100644 index 0000000..4e99493 --- /dev/null +++ b/src/game/base/weapon.h @@ -0,0 +1,52 @@ +/* + base/weapon.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_WEAPON_H__ +#define __INCLUDED_BASE_WEAPON_H__ + +#include "core/info.h" + +namespace game +{ +class Weapon : public core::Info { + +public: + Weapon(); + ~Weapon(); + + inline int level() const { + return weapon_level; + } + + inline void set_level(const int level) { + weapon_level = level; + } + + + + /* --- static registry functions ---------------------------------- */ + + static Weapon *find(const std::string & label); + + static bool init(); + + static void done(); + + static void list(); + + static inline const core::InfoType *infotype() { + return weapon_infotype; + } + +private: + static core::InfoType *weapon_infotype; + + int weapon_level; +}; + +} // namespace game + +#endif // __INCLUDED_BASE_WEAPON_H__ -- cgit v1.2.3