/* 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" #include "base/jumppoint.h" #include "base/cargopod.h" #include "base/spacemine.h" namespace game { core::InfoType *Template::template_infotype = 0; void func_list_template(const std::string &args) { std::stringstream argstr(args); std::string label; if (argstr >> label) { aux::to_label(label); Template *entitytemplate = Template::find(label); if (entitytemplate) { entitytemplate->print(); } else { con_warn << "Unknown template '" << label << "'" << std::endl; return; } } else { 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::print() const { con_print << "Template '" << label() << "' info id " << id() << std::endl; // show radius if set if (radius()) con_print << " radius " << radius() << std::endl; // set modelname if (modelname().size()) con_print << " model " << modelname() << std::endl; // set primary color if (has_color()) con_print << " primary color " << color() << std::endl; // set secondary color if (has_color_second()) con_print << " primary color " << color_second() << std::endl; if (maxarmor()) con_print << " armor " << maxarmor() << std::endl; } void Template::list() { core::Info::list(template_infotype); } bool Template::got_template_key(filesystem::IniFile &inifile, Template *entitytemplate) { std::string strvalue; math::Color colorvalue; float floatvalue; if (inifile.got_key_label("label", strvalue)) { if (find(strvalue)) { inifile.unknown_error("duplicate template '" + strvalue + "'"); } entitytemplate->set_label(strvalue); return true; } else if (inifile.got_key_string("name", strvalue)) { entitytemplate->set_name(strvalue); return true; } else if (inifile.got_key_string("model", strvalue)) { entitytemplate->set_modelname(strvalue); return true; } else if (inifile.got_key_string("info", strvalue)) { entitytemplate->add_text(strvalue); return true; } else if (inifile.got_key_color("color", colorvalue)) { entitytemplate->set_color(colorvalue); return true; } else if (inifile.got_key_color("colorsecond", colorvalue)) { entitytemplate->set_color_second(colorvalue); return true; } else if (inifile.got_key_float("radius", floatvalue)) { entitytemplate->set_radius(floatvalue); return true; } else if (inifile.got_key_float("armor", floatvalue)) { entitytemplate->set_maxarmor(floatvalue); return true; } else { return false; } } 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; 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 (got_template_key(inifile, entitytemplate)) { continue; } else { inifile.unknown_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("spacemine")); SpaceMine::set_template(entitytemplate); if (!entitytemplate) { con_warn << "Template 'spacemine' not found!" << std::endl; } entitytemplate = Template::find(std::string("navpoint")); NavPoint::set_template(entitytemplate); if (!entitytemplate) { con_warn << "Template 'navpoint' not found!" << std::endl; } entitytemplate = Template::find(std::string("jumppoint")); JumpPoint::set_template(entitytemplate); if (!entitytemplate) { con_warn << "Template 'jumppoint' not found!" << std::endl; } entitytemplate = Template::find(std::string("jumpgate")); JumpGate::set_template(entitytemplate); if (!entitytemplate) { con_warn << "Template 'jumpgate' 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; } void Template::done() { core::Func::remove("list_template"); } Template::Template() : core::Info(template_infotype), template_color(), template_color_second() { template_radius = 0; template_has_color = false; template_has_color_second = false; template_maxarmor = 0; } 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::set_maxarmor(const float maxarmor) { template_maxarmor = maxarmor; } 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_second()) entity->set_color_second(color_second()); } } // namespace game