Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: b3802f78934f81c933fb3a9366302c75dca9788a (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
/*
   base/shipmodel.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 <iomanip>

#include "auxiliary/functions.h"
#include "base/shipmodel.h"
#include "sys/sys.h"

namespace game {

// the ship model registry
std::map<std::string, ShipModel *>	ShipModel::registry;

ShipModel::ShipModel()
{
	//default specifications
	shipmodel_acceleration = 1.0f;	// thruster acceleration in game untits/second^2
	shipmodel_maxspeed = 3.0f;	// maximum thruster speed in game units/second
	shipmodel_turnspeed = 45.0f;	// 45 degrees per second
	shipmodel_price = 0;		// the ship is for free by default
	shipmodel_maxcargo = 0;

	shipmodel_jumpdrive = false;
}

ShipModel::~ShipModel()
{}


// clear the ship model registry
void ShipModel::clear()
{
	for (iterator smit = registry.begin(); smit != registry.end(); smit++) {
		delete (*smit).second;
	}
	registry.clear();
}

void ShipModel::print()
{
	con_print << "label: ^B" << label() << " ^Nname: ^B" << name() << std::endl;
 	con_print << "  price:         ^B" << price() << std::endl;
	con_print << "  acceleration:  ^B" << acceleration() << std::endl;
	con_print << "  turnspeed:     ^B" << turnspeed() << std::endl;
	con_print << "  max speed:     ^B" << maxspeed() << std::endl;
	con_print << "  cargo:         ^B" << maxcargo() << std::endl;
}

void ShipModel::generate_info(core::Info *info)
{
	info->clear_text();

	info->set_name(name());
	info->set_modelname(modelname());

	// info text form ships.ini
	for (core::Info::Text::iterator it = shipmodel_infotext.begin(); it != shipmodel_infotext.end(); it++) {
		info->add_text((*it));
	}

	info->add_text("");
	info->add_text("^BSpecifications:^N");
	std::stringstream str;	
	str << "price:        ^B" << price() << " ^Ncredits";
	info->add_text(str.str()); str.str("");

	str << "cargo hold:   ^B" << 0.1f * maxcargo() << " ^Nmetric tonnes";
	info->add_text(str.str()); str.str("");

	str << "top speed:    ^B" << 100.0f * maxspeed() << " ^Nmps";
	info->add_text(str.str()); str.str("");

	str << "response:     ^B" << turnspeed() << " ^Ndps";
	info->add_text(str.str()); str.str("");

	str << "acceleration: ^B" << acceleration() << " ^Nstandard";
	info->add_text(str.str()); str.str("");

	if(shipmodel_jumpdrive) {
		str << "hyperspace jump drive capable";
		info->add_text(str.str()); str.str("");
	}
}

void ShipModel::list()
{
	for (iterator smit = registry.begin(); smit != registry.end(); smit++) {
		con_print << std::setw(24) <<  (*smit).second->label() 
			<< " ^B" << (*smit).second->name() << "\n";
	}
	con_print << registry.size() << " registered ship models\n";
}

ShipModel *ShipModel::find(ShipModel *shipmodel)
{
	for (iterator smit = registry.begin(); smit != registry.end(); smit++) {
		if ((*smit).second == shipmodel)
			return shipmodel;
	}

	return 0;
}

ShipModel *ShipModel::find(const std::string label)
{
	std::map<std::string, ShipModel *>::iterator it = registry.find(label);
	if (it == registry.end())
		return 0;
	else
		return (*it).second;
}

ShipModel *ShipModel::search(const std::string searchname)
{
	std::string strsearchkey(aux::lowercase(searchname));
	if (strsearchkey.size() < 3) {
		return 0;
	}

	std::string label;
	std::string name;

	for (iterator smit = registry.begin(); smit != registry.end(); smit++) {
		ShipModel *shipmodel = (*smit).second;
	
		label.assign(shipmodel->label());
		if (label.size() && (label.find(strsearchkey) != std::string::npos)) {
			return shipmodel;
		}
		
		name.assign(aux::lowercase(shipmodel->name()));
		if (name.size() && (name.find(strsearchkey) != std::string::npos)) {
			return shipmodel;
		}
	}

	return 0;
}

// add a new ship model
void ShipModel::add(ShipModel *shipmodel)
{
	ShipModel *m = find(shipmodel->label());
	if (m) {
		con_warn << "Duplicate ship model " << shipmodel->label() << "!\n";
		delete m;
	}
	
	registry[shipmodel->label()] = shipmodel;
}

}