Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/game/base/Makefile.am6
-rw-r--r--src/game/base/cargo.h11
-rw-r--r--src/game/base/cargopod.cc14
-rw-r--r--src/game/base/cargopod.h8
-rw-r--r--src/game/base/faction.cc131
-rw-r--r--src/game/base/faction.h78
-rw-r--r--src/game/base/game.cc62
-rw-r--r--src/game/base/game.h5
-rw-r--r--src/game/base/navpoint.cc10
-rw-r--r--src/game/base/navpoint.h14
-rw-r--r--src/game/base/ship.cc16
-rw-r--r--src/game/base/shipmodel.cc69
-rw-r--r--src/game/base/shipmodel.h43
-rw-r--r--src/game/base/template.cc181
-rw-r--r--src/game/base/template.h92
15 files changed, 663 insertions, 77 deletions
diff --git a/src/game/base/Makefile.am b/src/game/base/Makefile.am
index 6391468..bb0d967 100644
--- a/src/game/base/Makefile.am
+++ b/src/game/base/Makefile.am
@@ -14,7 +14,8 @@ libbase_la_SOURCES = \
ship.cc \
shipmodel.cc \
star.cc \
- station.cc
+ station.cc \
+ template.cc
noinst_HEADERS = \
cargo.h \
cargopod.h \
@@ -27,4 +28,5 @@ noinst_HEADERS = \
ship.h \
shipmodel.h \
star.h \
- station.h
+ station.h \
+ template.h
diff --git a/src/game/base/cargo.h b/src/game/base/cargo.h
index eb616a5..bee0502 100644
--- a/src/game/base/cargo.h
+++ b/src/game/base/cargo.h
@@ -23,15 +23,20 @@ public:
void eject(core::EntityControlable *ejector, const int amount);
- /* -- static functions -- */
-
- static core::InfoType *cargo_infotype;
+ /* --- static registry functions ---------------------------------- */
static Cargo *find(const std::string & label);
static bool init();
static void list();
+
+ static inline const core::InfoType *infotype() {
+ return cargo_infotype;
+ }
+
+private:
+ static core::InfoType *cargo_infotype;
};
} // namespace game
diff --git a/src/game/base/cargopod.cc b/src/game/base/cargopod.cc
index 7406d3a..ed3ea69 100644
--- a/src/game/base/cargopod.cc
+++ b/src/game/base/cargopod.cc
@@ -9,7 +9,9 @@
namespace game
{
-
+
+const Template *CargoPod::cargopod_template = 0;
+
CargoPod::CargoPod() : EntityDynamic()
{
entity_moduletypeid = cargopod_enttype;
@@ -18,11 +20,13 @@ CargoPod::CargoPod() : EntityDynamic()
set_flag(core::Entity::KeepAlive);
- if (Default::podmodel)
- set_modelname(Default::podmodel->name());
- set_mass(radius());
-
+ // use template settings if available
+ if (cargopod_template) {
+ cargopod_template->apply(this);
+ }
+
// activate physics
+ set_mass(radius());
reset();
const float damp = Game::g_damping->value();
diff --git a/src/game/base/cargopod.h b/src/game/base/cargopod.h
index 7bce9a4..d23c125 100644
--- a/src/game/base/cargopod.h
+++ b/src/game/base/cargopod.h
@@ -8,6 +8,7 @@
#define __INCLUDED_BASE_CARGOPOD_H__
#include "core/entity.h"
+#include "base/template.h"
namespace game
{
@@ -19,6 +20,13 @@ public:
virtual ~CargoPod();
virtual void upkeep(const unsigned long timestamp);
+
+ static inline void set_template (const Template *entitytemplate) {
+ cargopod_template = entitytemplate;
+ }
+
+private:
+ static const Template *cargopod_template;
};
} // namespace game
diff --git a/src/game/base/faction.cc b/src/game/base/faction.cc
new file mode 100644
index 0000000..2194844
--- /dev/null
+++ b/src/game/base/faction.cc
@@ -0,0 +1,131 @@
+/*
+ base/faction.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 "core/func.h"
+#include "core/parser.h"
+#include "base/faction.h"
+
+namespace game {
+
+core::InfoType *Faction::faction_infotype = 0;
+
+void func_list_faction(const std::string &args)
+{
+ Faction::list();
+}
+
+Faction *Faction::find(const std::string & label)
+{
+ if (!label.size()) {
+ return 0;
+ }
+
+ return (Faction *) core::Info::find(faction_infotype, label);
+}
+
+void Faction::list()
+{
+ core::Info::list(faction_infotype);
+}
+
+bool Faction::init()
+{
+ // initialize faction InfoType
+ faction_infotype = new core::InfoType("faction");
+
+ std::string inifilename("ini/factions");
+
+ filesystem::IniFile inifile;
+ inifile.open(inifilename);
+
+ if (!inifile.is_open()) {
+ con_warn << "Could not open " << inifile.name() << std::endl;
+ return false;
+ }
+
+ con_print << "^BLoading factions..." << std::endl;
+
+ size_t count = 0;
+ Faction *faction = 0;
+ std::string strvalue;
+ math::Color colorvalue;
+
+ while (inifile.getline()) {
+
+ if (inifile.got_section()) {
+
+ faction = 0;
+
+ if (inifile.got_section("faction")) {
+ count++;
+ faction = new Faction();
+
+ } else {
+ inifile.unknown_section();
+ }
+
+ } else if (inifile.got_key()) {
+
+ if (inifile.in_section("faction")) {
+
+ if (inifile.got_key_label("label", strvalue)) {
+ faction->set_label(strvalue);
+
+ } else if (inifile.got_key_string("name", strvalue)) {
+ faction->set_name(strvalue);
+
+ } else if (inifile.got_key_string("info", strvalue)) {
+ faction->add_text(strvalue);
+
+ } else if (inifile.got_key_color("color", colorvalue)) {
+ faction->set_color(colorvalue);
+
+ } else if (inifile.got_key_color("colorsecond", colorvalue)) {
+ faction->set_color_second(colorvalue);
+
+ } else {
+ inifile.unkown_key();
+ }
+ }
+ }
+ }
+
+ inifile.close();
+
+ if (!count) {
+ con_warn << "No factions found!" << std::endl;
+ return true; // non-fatal
+ }
+
+ con_debug << " " << inifile.name() << " " << count << " factions" << std::endl;
+
+ core::Func *func = core::Func::add("list_faction", func_list_faction);
+ func->set_info("list available factions");
+
+ return true;
+}
+
+Faction::Faction() :
+ core::Info(faction_infotype),
+ faction_color(),
+ faction_color_second()
+{
+}
+
+Faction::~Faction()
+{
+}
+
+void Faction::apply(core::Entity *entity) const
+{
+ // set primary color
+ entity->set_color(color());
+
+ // set secondary color
+ entity->set_color(color());
+}
+
+} // namespace game
diff --git a/src/game/base/faction.h b/src/game/base/faction.h
new file mode 100644
index 0000000..608f7c7
--- /dev/null
+++ b/src/game/base/faction.h
@@ -0,0 +1,78 @@
+/*
+ base/faction.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_FACTION_H__
+#define __INCLUDED_BASE_FACTION_H__
+
+#include <list>
+
+#include "math/color.h"
+#include "core/info.h"
+
+namespace game
+{
+
+class Faction : public core::Info {
+public:
+ Faction();
+ virtual ~Faction();
+
+ /* --- inspectors ------------------------------------------------- */
+
+ inline const math::Color & color() const {
+ return faction_color;
+ }
+
+ inline const math::Color & color_second() const {
+ return faction_color_second;
+ }
+
+ /* --- actors ----------------------------------------------------- */
+
+ /**
+ * @brief apply faction colors to an entity
+ */
+ void apply(core::Entity *entity) const;
+
+ /* --- mutators --------------------------------------------------- */
+
+ inline void set_color(const math::Color &color) {
+ faction_color.assign(color);
+ }
+
+ inline void set_color_second(const math::Color &color_second) {
+ faction_color_second.assign(color_second);
+ }
+
+ /* --- static ----------------------------------------------------- */
+
+ /**
+ * @brief initialize Faction infotype and read factions.init
+ */
+ static bool init();
+
+ /**
+ * @brief list available factions
+ */
+ static void list();
+
+ static Faction *find(const std::string & label);
+
+private:
+
+ /* --- attributes ------------------------------------------------- */
+
+ math::Color faction_color;
+ math::Color faction_color_second;
+
+ /* --- static ----------------------------------------------------- */
+
+ static core::InfoType *faction_infotype;
+};
+
+} // namespace game
+
+#endif // __INCLUDED_BASE_FACTION_H__
diff --git a/src/game/base/game.cc b/src/game/base/game.cc
index 295b2f1..fa83a0a 100644
--- a/src/game/base/game.cc
+++ b/src/game/base/game.cc
@@ -25,6 +25,7 @@
#include "base/racetrack.h"
#include "base/ship.h"
#include "base/star.h"
+#include "base/template.h"
#include "math/mathlib.h"
#include "sys/sys.h"
@@ -36,7 +37,6 @@ namespace game
// default player settings
core::Zone *Default::zone = 0;
core::Entity *Default::view = 0;
-model::Model *Default::podmodel = 0;
ShipModel *Default::shipmodel = 0;
long Default::credits = 0;
@@ -46,7 +46,6 @@ void Default::clear()
view = 0;
shipmodel = 0;
credits = 0;
- podmodel = 0;
}
/* -- class Game static members ----------------------------------- */
@@ -854,8 +853,14 @@ Game::Game() : core::Module("Project::OSiRiON", true)
// read factions.ini
if (!Faction::init()) {
- // not fatal
-
+ abort();
+ return;
+ }
+
+ // read templates.ini
+ if (!Template::init()) {
+ abort();
+ return;
}
// read cargo.ini
@@ -870,13 +875,13 @@ Game::Game() : core::Module("Project::OSiRiON", true)
return;
}
- // load world.ini and the zones it refers to
+ // read world.ini and the zones it refers to
if (!load_world()) {
abort();
return;
}
- // load game default settings
+ // read game.ini
if (!load_settings()) {
abort();
return;
@@ -1218,9 +1223,15 @@ bool Game::load_zone(core::Zone *zone)
if (!faction) {
zoneini.unknown_error("unkown faction '" + strval + "'");
} else {
- jumppoint->set_color(faction->color());
- jumppoint->set_color_second(faction->color_second());
- }
+ faction->apply(jumppoint);
+ }
+ } else if (zoneini.got_key_label("template", strval)) {
+ Template *entitytemplate = Template::find(strval);
+ if (!entitytemplate) {
+ zoneini.unknown_error("unkown template '" + strval + "'");
+ } else {
+ entitytemplate->apply(jumppoint);
+ }
} else if (zoneini.got_key_string("target", strval)) {
jumppoint->set_targetlabel(strval);
continue;
@@ -1251,8 +1262,7 @@ bool Game::load_zone(core::Zone *zone)
if (!faction) {
zoneini.unknown_error("unkown faction '" + strval + "'");
} else {
- planet->set_color(faction->color());
- planet->set_color_second(faction->color_second());
+ faction->apply(planet);
}
} else {
zoneini.unkown_key();
@@ -1270,8 +1280,14 @@ bool Game::load_zone(core::Zone *zone)
if (!faction) {
zoneini.unknown_error("unkown faction '" + strval + "'");
} else {
- station->set_color(faction->color());
- station->set_color_second(faction->color_second());
+ faction->apply(station);
+ }
+ } else if (zoneini.got_key_label("template", strval)) {
+ Template *entitytemplate = Template::find(strval);
+ if (!entitytemplate) {
+ zoneini.unknown_error("unkown template '" + strval + "'");
+ } else {
+ entitytemplate->apply(station);
}
} else {
zoneini.unkown_key();
@@ -1306,6 +1322,13 @@ bool Game::load_zone(core::Zone *zone)
entity->set_color(faction->color());
entity->set_color_second(faction->color_second());
}
+ } else if (zoneini.got_key_label("template", strval)) {
+ Template *entitytemplate = Template::find(strval);
+ if (!entitytemplate) {
+ zoneini.unknown_error("unkown template '" + strval + "'");
+ } else {
+ entitytemplate->apply(entity);
+ }
} else {
zoneini.unkown_key();
}
@@ -1445,11 +1468,10 @@ bool Game::generate_entity_menus(core::Entity *entity)
for (core::Inventory::Items::const_iterator it = entity->inventory()->items().begin(); it != entity->inventory()->items().end(); it++) {
core::Item *item = (*it);
- if (item->info()->type() == Cargo::cargo_infotype) {
-
+ if (item->info()->type() == Cargo::infotype()) {
nbcargo++;
- } else if (item->info()->type() == ShipModel::shipmodel_infotype) {
+ } else if (item->info()->type() == ShipModel::infotype()) {
if (!menu_dealer) {
menu_dealer = new MenuDescription();
menu_dealer->set_label("ships");
@@ -1540,14 +1562,6 @@ bool Game::load_settings()
} else {
inifile.unkown_key();
}
-
- } else if (inifile.in_section("cargo")) {
-
- if (inifile.got_key_string("model", str)) {
- Default::podmodel = model::Model::load(str);
- } else {
- inifile.unkown_key();
- }
}
}
}
diff --git a/src/game/base/game.h b/src/game/base/game.h
index 7e2a384..4f583a8 100644
--- a/src/game/base/game.h
+++ b/src/game/base/game.h
@@ -38,12 +38,10 @@ const unsigned int cargopod_enttype = 263;
// planet docking distance
const float planet_safe_distance = 50.0f;
+// ship engine delay times
const float jump_timer_delay = 5.0f;
const float impulse_timer_delay = 3.0f;
-// info class type constants
-//const unsigned int shipmodel_class_id = 1;
-//const unsigned int commodity_class_id = 2;
/// default player settings
class Default
@@ -52,7 +50,6 @@ public:
static core::Zone *zone;
static core::Entity *view;
static ShipModel *shipmodel;
- static model::Model *podmodel;
static long credits;
static void clear();
diff --git a/src/game/base/navpoint.cc b/src/game/base/navpoint.cc
index 650473b..2c76807 100644
--- a/src/game/base/navpoint.cc
+++ b/src/game/base/navpoint.cc
@@ -10,16 +10,22 @@
namespace game
{
+const Template *NavPoint::navpoint_template = 0;
+
NavPoint::NavPoint() : core::Entity()
{
entity_shape = core::Entity::Diamond;
get_color().assign(1.0f, 1.0f);
get_color_second().assign(0.6f, 1.0f);
set_radius(0.25f);
- set_flag(core::Entity::Bright);
- //set_flag(core::Entity::ShowOnMap);
entity_moduletypeid = navpoint_enttype;
+
+ // use template settings if available
+ if (navpoint_template) {
+ navpoint_template->apply(this);
+ }
+
}
NavPoint::~NavPoint()
diff --git a/src/game/base/navpoint.h b/src/game/base/navpoint.h
index bb2420e..0d98dc8 100644
--- a/src/game/base/navpoint.h
+++ b/src/game/base/navpoint.h
@@ -7,11 +7,12 @@
#ifndef __INCLUDED_BASE_NAVPOINT_H__
#define __INCLUDED_BASE_NAVPOINT_H__
-#include "core/entity.h"
-#include "math/mathlib.h"
-
#include <string>
+#include "math/mathlib.h"
+#include "core/entity.h"
+#include "base/template.h"
+
namespace game
{
@@ -21,6 +22,13 @@ class NavPoint : public core::Entity
public:
NavPoint();
~NavPoint();
+
+ static inline void set_template (const Template *entitytemplate) {
+ navpoint_template = entitytemplate;
+ }
+
+private:
+ static const Template *navpoint_template;
};
}
diff --git a/src/game/base/ship.cc b/src/game/base/ship.cc
index 641894e..38ade38 100644
--- a/src/game/base/ship.cc
+++ b/src/game/base/ship.cc
@@ -26,10 +26,21 @@ const float MIN_DELTA = 0.000001f;
Ship::Ship(core::Player *owner, ShipModel *shipmodel) : core::EntityControlable()
{
+ assert(shipmodel);
+
entity_moduletypeid = ship_enttype;
- set_modelname(shipmodel->modelname());
- set_name(shipmodel->name());
+ // apply template settings
+ if (shipmodel->model_template()) {
+ shipmodel->model_template();
+ }
+
+ // apply ship model settings
+ // ship model overrides template model
+ if (shipmodel->modelname().size()) {
+ set_modelname(shipmodel->modelname());
+ }
+ set_name(shipmodel->name());
set_info(shipmodel);
ship_shipmodel = shipmodel;
@@ -40,6 +51,7 @@ Ship::Ship(core::Player *owner, ShipModel *shipmodel) : core::EntityControlable(
if (owner) {
// this ship is owned by a player
+ // player colors override template colors
set_owner(owner);
get_color().assign(owner->color());
get_color_second().assign(owner->color_second());
diff --git a/src/game/base/shipmodel.cc b/src/game/base/shipmodel.cc
index 77ddb30..4cf55bf 100644
--- a/src/game/base/shipmodel.cc
+++ b/src/game/base/shipmodel.cc
@@ -10,6 +10,7 @@
#include "auxiliary/functions.h"
#include "base/shipmodel.h"
#include "base/game.h"
+#include "base/template.h"
#include "sys/sys.h"
namespace game
@@ -30,10 +31,10 @@ bool ShipModel::init()
using math::Vector3f;
using math::Color;
- filesystem::IniFile shipsini;
- shipsini.open("ini/ships");
- if (!shipsini.is_open()) {
- con_error << "Could not open " << shipsini.name() << "!" << std::endl;
+ filesystem::IniFile inifile;
+ inifile.open("ini/ships");
+ if (!inifile.is_open()) {
+ con_error << "Could not open " << inifile.name() << "!" << std::endl;
return false;
}
@@ -46,60 +47,71 @@ bool ShipModel::init()
float f;
bool b;
- while (shipsini.getline()) {
- if (shipsini.got_key()) {
- if (shipsini.section().compare("ship") == 0) {
- if (shipsini.got_key_label("label", str)) {
+ while (inifile.getline()) {
+ if (inifile.got_key()) {
+ if (inifile.section().compare("ship") == 0) {
+ if (inifile.got_key_label("label", str)) {
shipmodel->set_label(str);
count++;
continue;
- } else if (shipsini.got_key_string("name", str)) {
+ } else if (inifile.got_key_string("name", str)) {
shipmodel->set_name(str);
continue;
- } else if (shipsini.got_key_string("info", str)) {
+ } else if (inifile.got_key_string("info", str)) {
shipmodel->add_text(str);
continue;
- } else if (shipsini.got_key_string("model", str)) {
+ } else if (inifile.got_key_string("model", str)) {
shipmodel->set_modelname(str);
continue;
- } else if (shipsini.got_key_long("price", l)) {
+ } else if (inifile.got_key_long("price", l)) {
shipmodel->set_price(l);
continue;
- } else if (shipsini.got_key_float("cargo", f)) {
+ } else if (inifile.got_key_float("cargo", f)) {
shipmodel->set_maxcargo(f);
continue;
- } else if (shipsini.got_key_bool("jumpdrive", b)) {
+ } else if (inifile.got_key_bool("jumpdrive", b)) {
shipmodel->set_jumpdrive(b);
continue;
- } else if (shipsini.got_key_bool("dock", b)) {
+ } else if (inifile.got_key_bool("dock", b)) {
shipmodel->set_dock(b);
continue;
- } else if (shipsini.got_key_float("maxspeed", f)) {
+ } else if (inifile.got_key_float("maxspeed", f)) {
shipmodel->set_maxspeed(f * 0.01f);
continue;
- } else if (shipsini.got_key_float("impulse", f)) {
+ } else if (inifile.got_key_float("impulse", f)) {
shipmodel->set_impulse_force(f);
continue;
- } else if (shipsini.got_key_float("thrust", f)) {
+ } else if (inifile.got_key_float("thrust", f)) {
shipmodel->set_thrust_force(f);
continue;
- } else if (shipsini.got_key_float("strafe", f)) {
+ } else if (inifile.got_key_float("strafe", f)) {
shipmodel->set_strafe_force(f);
continue;
- } else if (shipsini.got_key_float("turn", f)) {
+ } else if (inifile.got_key_float("turn", f)) {
shipmodel->set_turn_force(f);
continue;
- } else if (shipsini.got_key_float("roll", f)) {
+ } else if (inifile.got_key_float("roll", f)) {
shipmodel->set_roll_force(f);
continue;
- } else if (shipsini.got_key_float("mass", f)) {
+ } else if (inifile.got_key_float("mass", f)) {
shipmodel->set_mass(f);
continue;
+ } else if (inifile.got_key_float("radius", f)) {
+ shipmodel->set_radius(f);
+ continue;
+ } else if (inifile.got_key_label("template", str)) {
+ Template *entitytemplate = Template::find(str);
+ if (!entitytemplate) {
+ inifile.unknown_error("unkown template '" + str + "'");
+ } else {
+ shipmodel->set_template(entitytemplate);
+ }
} else {
- shipsini.unkown_key();
+ inifile.unkown_key();
}
}
- } else if (shipsini.got_section("ship")) {
+ } else if (inifile.got_section("ship")) {
+
// generate info for the last loaded ship model
if (shipmodel) {
shipmodel->generate_info();
@@ -108,12 +120,13 @@ bool ShipModel::init()
// add a new shipmodel
shipmodel = new ShipModel();
+ // the first ship model is set as default, game.ini can override this later
if (!Default::shipmodel) {
Default::shipmodel = shipmodel;
}
- } else if (shipsini.got_section()) {
- shipsini.unknown_section();
+ } else if (inifile.got_section()) {
+ inifile.unknown_section();
}
}
@@ -122,9 +135,9 @@ bool ShipModel::init()
shipmodel->generate_info();
}
- con_debug << " " << shipsini.name() << " " << count << " ship types" << std::endl;
+ con_debug << " " << inifile.name() << " " << count << " ship types" << std::endl;
- shipsini.close();
+ inifile.close();
core::Func *func = core::Func::add("list_ship", func_list_ship);
func->set_info("list available ship types");
diff --git a/src/game/base/shipmodel.h b/src/game/base/shipmodel.h
index c7aa2ba..9da50a6 100644
--- a/src/game/base/shipmodel.h
+++ b/src/game/base/shipmodel.h
@@ -11,6 +11,7 @@
#include "core/info.h"
#include "core/entity.h"
+#include "base/template.h"
namespace game
{
@@ -76,6 +77,18 @@ public:
inline const float maxcargo() const {
return shipmodel_maxcargo;
}
+
+ /// ship radius
+ inline const float radius() const {
+ return shipmodel_radius;
+ }
+
+ /// entity template
+ inline const Template *model_template() const {
+ return shipmodel_template;
+ }
+
+protected:
/* ---- mutators -------------------------------------------------- */
@@ -129,11 +142,22 @@ public:
inline void set_dock(const bool dock) {
shipmodel_dock = dock;
}
+
+ /// set radius
+ inline void set_radius(const float radius) {
+ shipmodel_radius = radius;
+ }
+
+ /// set entity template
+ inline void set_template(const Template *model_template) {
+ shipmodel_template = model_template;
+ }
+public:
void generate_info();
void buy(core::EntityControlable *buyer, core::Entity *seller);
-
+
/* --- static registry functions ---------------------------------- */
static ShipModel *find(const std::string & label);
@@ -143,10 +167,15 @@ public:
static bool init();
static void list();
+
+ static inline const core::InfoType *infotype() {
+ return shipmodel_infotype;
+ }
- static core::InfoType *shipmodel_infotype;
-
-private:
+private:
+ /* --- attributes ------------------------------------------------- */
+
+ float shipmodel_radius;
float shipmodel_mass;
float shipmodel_impulse_force;
float shipmodel_thrust_force;
@@ -159,6 +188,12 @@ private:
bool shipmodel_jumpdrive;
bool shipmodel_dock;
+
+ const Template *shipmodel_template;
+
+ /* --- static ----------------------------------------------------- */
+
+ static core::InfoType *shipmodel_infotype;
};
}
diff --git a/src/game/base/template.cc b/src/game/base/template.cc
new file mode 100644
index 0000000..1cdac1a
--- /dev/null
+++ b/src/game/base/template.cc
@@ -0,0 +1,181 @@
+/*
+ base/template.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 "core/func.h"
+#include "core/parser.h"
+#include "base/template.h"
+#include "base/cargopod.h"
+#include "base/navpoint.h"
+
+namespace game {
+
+core::InfoType *Template::template_infotype = 0;
+
+void func_list_template(const std::string &args)
+{
+ Template::list();
+}
+
+Template *Template::find(const std::string & label)
+{
+ if (!label.size()) {
+ return 0;
+ } else {
+ return (Template *) core::Info::find(template_infotype, label);
+ }
+}
+
+void Template::list()
+{
+ core::Info::list(template_infotype);
+}
+
+bool Template::init()
+{
+ // initialize template InfoType
+ template_infotype = new core::InfoType("template");
+
+ std::string inifilename("ini/templates");
+
+ filesystem::IniFile inifile;
+ inifile.open(inifilename);
+
+ if (!inifile.is_open()) {
+ con_warn << "Could not open " << inifile.name() << std::endl;
+ return false;
+ }
+
+ con_print << "^BLoading templates..." << std::endl;
+
+ size_t count = 0;
+ Template *entitytemplate = 0;
+ std::string strvalue;
+ math::Color colorvalue;
+ float floatvalue;
+
+ while (inifile.getline()) {
+
+ if (inifile.got_section()) {
+
+ entitytemplate = 0;
+
+ if (inifile.got_section("template")) {
+ count++;
+ entitytemplate = new Template();
+
+ } else {
+ inifile.unknown_section();
+ }
+
+ } else if (inifile.got_key()) {
+
+ if (inifile.in_section("template")) {
+
+ if (inifile.got_key_label("label", strvalue)) {
+ entitytemplate->set_label(strvalue);
+
+ } else if (inifile.got_key_string("name", strvalue)) {
+ entitytemplate->set_name(strvalue);
+
+ } else if (inifile.got_key_string("model", strvalue)) {
+ entitytemplate->set_modelname(strvalue);
+
+ } else if (inifile.got_key_string("info", strvalue)) {
+ entitytemplate->add_text(strvalue);
+
+ } else if (inifile.got_key_color("color", colorvalue)) {
+ entitytemplate->set_color(colorvalue);
+
+ } else if (inifile.got_key_color("colorsecond", colorvalue)) {
+ entitytemplate->set_color_second(colorvalue);
+
+ } else if (inifile.got_key_float("radius", floatvalue)) {
+
+ } else {
+ inifile.unkown_key();
+ }
+ }
+ }
+ }
+
+ inifile.close();
+
+ if (!count) {
+ con_error << "No entity templates found!" << std::endl;
+ return false;
+ }
+
+ entitytemplate = Template::find(std::string("cargopod"));
+ CargoPod::set_template(entitytemplate);
+ if (!entitytemplate) {
+ con_warn << "Template 'cargopod' not found!" << std::endl;
+ }
+
+ entitytemplate = Template::find(std::string("navpoint"));
+ NavPoint::set_template(entitytemplate);
+ if (entitytemplate) {
+ con_warn << "Template 'navpoint' not found!" << std::endl;
+ }
+
+ con_debug << " " << inifile.name() << " " << count << " entity templates" << std::endl;
+
+ core::Func *func = core::Func::add("list_template", func_list_template);
+ func->set_info("list available entity templates");
+
+ return true;
+}
+
+Template::Template() :
+ core::Info(template_infotype),
+ template_color(),
+ template_color_second()
+{
+ template_radius = 0;
+ template_has_color = false;
+ template_has_color_second = false;
+}
+
+Template::~Template()
+{
+}
+
+void Template::set_radius(const float radius)
+{
+ template_radius = radius;
+}
+
+void Template::set_color(const math::Color &color)
+{
+ template_color.assign(color);
+ template_has_color = true;
+}
+
+void Template::set_color_second(const math::Color &color_second)
+{
+ template_color_second.assign(color_second);
+ template_has_color_second = true;
+}
+
+void Template::apply(core::Entity *entity) const
+{
+ // set radius
+ if (radius())
+ entity->set_radius(radius());
+
+ // set modelname
+ if (modelname().size())
+ entity->set_modelname(modelname());
+
+ // set primary color
+ if (has_color())
+ entity->set_color(color());
+
+ // set secondary color
+ if (has_color())
+ entity->set_color(color());
+}
+
+} // namespace game
diff --git a/src/game/base/template.h b/src/game/base/template.h
new file mode 100644
index 0000000..47c9ff3
--- /dev/null
+++ b/src/game/base/template.h
@@ -0,0 +1,92 @@
+/*
+ base/template.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_TEMPLATE_H__
+#define __INCLUDED_BASE_TEMPLATE_H__
+
+#include <list>
+
+#include "math/color.h"
+#include "model/model.h"
+#include "core/info.h"
+
+namespace game
+{
+
+class Template : public core::Info {
+public:
+ Template();
+
+ virtual ~Template();
+
+ /* --- inspectors ------------------------------------------------- */
+
+ inline const float radius() const {
+ return template_radius;
+ }
+
+ inline const bool has_color() const {
+ return template_has_color;
+ }
+
+ inline const math::Color & color() const {
+ return template_color;
+ }
+
+ inline const bool has_color_second() const {
+ return template_has_color_second;
+ }
+
+ inline const math::Color & color_second() const {
+ return template_color_second;
+ }
+
+ /* --- actors ----------------------------------------------------- */
+
+ /**
+ * @brief apply a template to an entity
+ */
+ void apply(core::Entity *entity) const;
+
+protected:
+
+ /* --- mutators --------------------------------------------------- */
+
+ void set_radius(const float radius);
+
+ void set_color(const math::Color &color);
+
+ void set_color_second(const math::Color &color_second);
+
+public:
+ /* --- static ----------------------------------------------------- */
+
+ static bool init();
+
+ static void list();
+
+ static Template *find(const std::string & label);
+
+private:
+
+ /* --- attributes ------------------------------------------------- */
+
+ float template_radius;
+
+ math::Color template_color;
+ bool template_has_color;
+
+ math::Color template_color_second;
+ bool template_has_color_second;
+
+ /* --- static ----------------------------------------------------- */
+
+ static core::InfoType *template_infotype;
+};
+
+} // namespace game
+
+#endif // __INCLUDED_BASE_TEMPLATE_H__