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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
|
/*
base/weapon.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 <assert.h>
#include <cmath>
#include "sys/sys.h"
#include "filesystem/inifile.h"
#include "auxiliary/functions.h"
#include "core/func.h"
#include "base/cargopod.h"
#include "base/game.h"
#include "base/weapon.h"
namespace game
{
core::InfoType *Weapon::weapon_infotype = 0;
void func_list_weapons(const std::string &args)
{
Weapon::list();
}
// load weapon types from ini file
bool Weapon::init()
{
// initialize weapon InfoType
weapon_infotype = new core::InfoType("weapon");
filesystem::IniFile weaponsini;
weaponsini.open("ini/weapons");
if (!weaponsini.is_open()) {
con_error << "Could not open " << weaponsini.name() << "!" << std::endl;
return false;
}
con_print << "^BLoading weapons..." << std::endl;
size_t count = 0;
Weapon *weapon = 0;
std::string str;
long l;
float f;
while (weaponsini.getline()) {
if (weaponsini.got_key()) {
if (weaponsini.section().compare("mine") == 0) {
if (weaponsini.got_key_label("label", str)) {
weapon->set_label(str);
continue;
} else if (weaponsini.got_key_string("name", str)) {
weapon->set_name(str);
continue;
} else if (weaponsini.got_key_string("info", str)) {
weapon->add_text(str);
continue;
} else if (weaponsini.got_key_string("model", str)) {
weapon->set_modelname(str);
continue;
} else if (weaponsini.got_key_long("price", l)) {
weapon->set_price(l);
continue;
} else if (weaponsini.got_key_float("volume", f)) {
weapon->set_volume(f);
continue;
} else if (weaponsini.got_key_long("level", l)) {
weapon->set_level(l);
continue;
} else if (weaponsini.got_key_float("damage", f)) {
weapon->set_damage(f);
continue;
} else {
weaponsini.unknown_key();
}
} else if (weaponsini.section().compare("cannon") == 0) {
if (weaponsini.got_key_label("label", str)) {
weapon->set_label(str);
continue;
} else if (weaponsini.got_key_string("name", str)) {
weapon->set_name(str);
continue;
} else if (weaponsini.got_key_string("info", str)) {
weapon->add_text(str);
continue;
} else if (weaponsini.got_key_string("model", str)) {
weapon->set_modelname(str);
continue;
} else if (weaponsini.got_key_long("price", l)) {
weapon->set_price(l);
continue;
} else if (weaponsini.got_key_float("volume", f)) {
weapon->set_volume(f);
continue;
} else if (weaponsini.got_key_long("level", l)) {
weapon->set_level(l);
continue;
} else {
weaponsini.unknown_key();
}
} else if (weaponsini.section().compare("turret") == 0) {
if (weaponsini.got_key_label("label", str)) {
weapon->set_label(str);
continue;
} else if (weaponsini.got_key_string("name", str)) {
weapon->set_name(str);
continue;
} else if (weaponsini.got_key_string("info", str)) {
weapon->add_text(str);
continue;
} else if (weaponsini.got_key_string("model", str)) {
weapon->set_modelname(str);
continue;
} else if (weaponsini.got_key_long("price", l)) {
weapon->set_price(l);
continue;
} else if (weaponsini.got_key_float("volume", f)) {
weapon->set_volume(f);
continue;
} else if (weaponsini.got_key_long("level", l)) {
weapon->set_level(l);
continue;
} else {
weaponsini.unknown_key();
}
} else if (weaponsini.section().compare("projectile") == 0) {
if (weapon) {
if (weaponsini.got_key_float("speed", f)) {
// convert speed from meters/second to game units/second
weapon->set_projectile_speed(f * 0.01f);
continue;
} else if (weaponsini.got_key_long("interval", l)) {
weapon->set_projectile_interval((unsigned long) l);
continue;
} else if (weaponsini.got_key_long("lifespan", l)) {
// lifespan in milliseconds
weapon->set_projectile_lifespan((unsigned long) l);
continue;
} else if (weaponsini.got_key_float("range", f)) {
// range in meters, one game unit is 100 meters
if (weapon->projectile_speed() == 0) {
weaponsini.unknown_error("cannot set range if projectile speed is 0!");
} else {
weapon->set_projectile_lifespan((unsigned long) (10.0f * f / weapon->projectile_speed()));
}
continue;
} else if (weaponsini.got_key_float("damage", f)) {
weapon->set_damage(f);
continue;
} else if (weaponsini.got_key_string("model", str)) {
weapon->set_projectile_modelname(str);
continue;
} else if (weaponsini.got_key_string("sound", str)) {
weapon->set_projectile_soundname(str);
continue;
} else {
weaponsini.unknown_key();
}
}
}
} else if (weaponsini.got_section()) {
Weapon *previous = 0;
if (weaponsini.got_section("mine")) {
previous = weapon;
weapon = new Weapon();
weapon->set_subtype(Mine);
count++;
} else if (weaponsini.got_section("cannon")) {
previous = weapon;
weapon = new Weapon();
weapon->set_subtype(Cannon);
count++;
} else if (weaponsini.got_section("turret")) {
previous = weapon;
weapon = new Weapon();
weapon->set_subtype(Turret);
count++;
} else if (weaponsini.got_section("projectile")) {
if (!weapon) {
weaponsini.unknown_error("projectile section without weapon section");
}
} else if (weaponsini.got_section()) {
weaponsini.unknown_section();
}
if (previous) {
previous->generate_info();
}
}
}
if (weapon) {
weapon->generate_info();
}
// add weapon infos
con_debug << " " << weaponsini.name() << " " << count << " weapon types" << std::endl;
weaponsini.close();
core::Func *func = core::Func::add("list_weapons", func_list_weapons);
func->set_info("list available weapon types");
return true;
}
void Weapon::done()
{
core::Func::remove("list_weapons");
}
Weapon *Weapon::find(const std::string & label)
{
if (!label.size()) {
return 0;
}
return (Weapon *) core::Info::find(weapon_infotype, label);
}
void Weapon::list()
{
core::Info::list(weapon_infotype);
}
/* ---- class Weapon ------------------------------------------- */
Weapon::Weapon() : core::Info(weapon_infotype)
{
set_volume(1);
set_level(1);
set_subtype(Ammo);
set_damage(0);
set_projectile_interval(0);
set_projectile_lifespan(0);
set_projectile_speed(0);
}
Weapon::~Weapon()
{
}
void Weapon::set_level(const int level)
{
weapon_level = level;
}
void Weapon::set_subtype(const SubType subtype)
{
weapon_subtype = subtype;
}
void Weapon::generate_info()
{
if (text().size())
add_line("");
add_line("^BSpecifications:^N");
std::stringstream str;
str << "price: ^B" << price() << " ^Ncredits";
add_line(str.str());
str.str("");
str << "volume: ^B" << volume() << " ^Ncubic meter";
add_line(str.str());
str.str("");
add_line("");
if ((subtype() == Cannon) || (subtype() == Turret)) {
str << "damage: ^B" << damage() << "^N";
add_line(str.str());
str.str("");
str << "range: ^B" << floor(((float) projectile_lifespan() * projectile_speed()) / 10.0f) << " ^Nmeter";
add_line(str.str());
str.str("");
str << "speed: ^B" << floor(projectile_speed()) * 100.0f << " ^Nmeter/sec";
add_line(str.str());
str.str("");
str << "fire rate: ^B" << "1/" << projectile_interval() << " ^Nmilliseconds";
add_line(str.str());
str.str("");
} else {
str << "damage: ^B" << damage() << "^N";
add_line(str.str());
str.str("");
}
}
} // namespace game
|