diff options
Diffstat (limited to 'src/game/base/template.cc')
-rw-r--r-- | src/game/base/template.cc | 181 |
1 files changed, 181 insertions, 0 deletions
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 |