Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: 7f23fd903238c3ba880302f0e15a33eadc897be8 (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
/*
   base/jumppoint.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 "base/game.h"
#include "base/jumppoint.h"

namespace game
{

/* ---- class JumpPoint -------------------------------------------- */

const Template *JumpPoint::jumppoint_template = 0;

JumpPoint::JumpPoint() : core::EntityDynamic()
{
	set_shape(core::Entity::Diamond);

	// FIXME jumppoints should be harder to find
	set_flag(core::Entity::ShowOnMap);
	set_flag(core::Entity::NonSolid);
	
	get_color().assign(0.0f, 0.8f, 0.8f, 1.0f);
	get_color_second().assign(0.6f, 1.0f);
	set_radius(0.25f);
	
	set_flag(core::Entity::Bright);
	
	if (jumppoint_template)
		jumppoint_template->apply(this);
	
	entity_moduletypeid = jumppoint_enttype;
	jumppoint_target = 0;
	set_serverside(false);
}

JumpPoint::~JumpPoint()
{
}

void JumpPoint::set_target_label(const std::string &label)
{
	jumppoint_target_label.assign(label);
}

void JumpPoint::validate()
{
	jumppoint_target = 0;

	if (target_label().size() == 0)
		return;
	
	if (target_label().size() < 3) {
		con_warn << "  Jumppoint '" << label() << "' has invalid target '" << target_label() << "'\n";
		return;
	}
	size_t pos = target_label().find(':');
	if ((pos == std::string::npos) || (pos < 1) || (pos >= (target_label().size() - 1))) {
		con_warn << "  Jumppoint '" << label() << "' has invalid target '" << target_label() << "'\n";
		return;
	}

	std::string zonelabel(target_label().substr(0, pos));
	std::string entitylabel(target_label().substr(pos + 1, target_label().size() - pos));

	core::Zone *targetzone = core::Zone::find(zonelabel);
	if (!targetzone) {
		con_warn << "  Jumppoint '" << label() << "' has invalid target zone '" << zonelabel << "'\n";
		return;
	}

	core::Entity *targetentity = targetzone->find_entity(entitylabel);
	if (!targetentity) {
		con_warn << "  Jumppoint '" << label() << "' has unknown target '" << entitylabel << "' in zone '" << zonelabel << "'\n";
		return;
	}

	if ((targetentity->moduletype() != jumppoint_enttype) && (targetentity->moduletype() != jumpgate_enttype)) {
		con_warn << "  Jumppoint '" << label() << "' has invalid target '" << entitylabel << "' in zone '" << zonelabel << "'\n";
		return;
	}

	jumppoint_target = static_cast<JumpPoint *>(targetentity);
	
	// overwrite name and info, remove the "system" part from the name
	std::string name("Jumppoint " + zone()->name() + " -> " + target()->zone()->name());	
	for (size_t pos = name.find(" system"); pos != std::string::npos; pos = name.find(" system")) {
		name.erase(pos, 7);
	}
	set_name(name);
	
	// info is const, core::Info::find returns a non-const pointer
	core::Info *entity_info = core::Info::find(info());
	if (!entity_info) {
		std::string labelstr(zone()->label());
		labelstr += ':';
		labelstr.append(label());
		entity_info = new core::Info(core::Entity::infotype(), labelstr.c_str());
		set_info(entity_info);
	}
	entity_info->set_name("Jumppoint");
	
	entity_info->clear_text();
	entity_info->add_text("Jumppoint to the " + target()->zone()->name());
	
	con_debug << "  " << label() << " jumppoint to " << target()->zone()->label() << ":" << target()->label() << std::endl;
}

/* ---- class JumpGate --------------------------------------------- */

const Template *JumpGate::jumpgate_template = 0;

JumpGate::JumpGate() : JumpPoint()
{
	entity_moduletypeid = jumpgate_enttype;
	unset_flag(core::Entity::Bright);
	unset_flag(core::Entity::NonSolid);
	set_flag(core::Entity::ShowOnMap);
	set_radius(0.0f);
	set_state(core::Entity::NoPower);
	
	if (jumpgate_template)
		jumpgate_template->apply(this);
	
	jumpgate_timer = 0;
}

JumpGate::~JumpGate()
{
}

void JumpGate::validate()
{
	JumpPoint::validate();

	if (target()) {
		set_flag(core::Entity::Dockable);
		
		// overwrite name and info
		// overwrite name and info, remove the "system" part from the name
		std::string name("Jumpgate " + zone()->name() + " -> " + target()->zone()->name());	
		for (size_t pos = name.find(" system"); pos != std::string::npos; pos = name.find(" system")) {
			name.erase(pos, 7);
		}
		set_name(name);

		core::Info *entity_info = core::Info::find(info());
		entity_info->set_name("Jumpgate");
		entity_info->clear_text();
		// FIXME if 'system' was erased from the name, 'the' should not appear in the info text
		// e.g. Jumpgate to Kor Telos
		entity_info->add_text("Jumpgate to the " + target()->zone()->name());

	} else {
		unset_flag(core::Entity::Dockable);
		
		std::string name("Inactive Jumpgate");
		set_name(name);
	}
}

void JumpGate::func_dock(Ship *ship)
{
	if (target()) {
		// check if this jumpgate is in use
		if (activated()) {
			if (ship->owner()) {
				ship->owner()->send("Jumpgate in use");
			}
			return;
		}

		if (target()->moduletype() == jumpgate_enttype) {
			// check if the target jumpgate is in use
			if (static_cast<JumpGate *>(target())->activated()) {
				if (ship->owner()) {
					ship->owner()->send("Jumpgate in use");
				}
				return;
			}
			// activate target jumpgate
			static_cast<JumpGate *>(target())->activate();
		}
				
		// activate jumpgate
		activate();
		ship->initiate_jump(this);
		
		if (ship->owner() && ship->owner()->control() == ship) {
			ship->owner()->set_view(this);
		}
		
	} else {
		if (ship->owner())
			ship->owner()->send("Jumpgate inactive");
		return;
	}
}

void JumpGate::activate()
{
	jumpgate_timer = (jump_timer_delay + jump_cooldown_delay) * 1000;
	set_state(core::Entity::Normal);
}

void JumpGate::frame(const unsigned long elapsed)
{
	if (jumpgate_timer > 0) {
		
		if (jumpgate_timer > elapsed) {
			jumpgate_timer -= elapsed;
		} else if (state() != core::Entity::NoPower) {
			set_state(core::Entity::NoPower);
			jumpgate_timer = 0;
		}
	}
}

}