Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: a2bf66c86a7a8a990aaefc24c7bd640a3f7c06e0 (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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*
   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;
}

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 {
		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::~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_second())
			entity->set_color_second(color_second());
}

} // namespace game