Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: f937e2288d52a4b282a9202aca3653114837216a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
   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)) {
					entitytemplate->set_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;
}

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::~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