Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: 1f2c0a9d5862af6e3c6078099d189c9851c8b8b5 (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
/*
   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);
	
	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_targetlabel(const std::string &label)
{
	jumppoint_targetlabel.assign(label);
}

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

	if (targetlabel().size() < 3) {
		con_warn << "  Jumppoint with invalid target label '" << targetlabel() << "'\n";
		return;
	}
	size_t pos = targetlabel().find(':');
	if ((pos < 1) || (pos >= (targetlabel().size() - 1))) {
		con_warn << "  Jumppoint with invalid target label '" << targetlabel() << "'\n";
		return;
	}

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

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

	core::Entity *targetentity = targetzone->find_entity(entitylabel);
	if (!targetentity) {
		con_warn << "  Could not find target jumppoint '" << entitylabel << "' in zone '" << zonelabel << "'\n";
		return;
	}

	if ((targetentity->moduletype() != jumppoint_enttype) && (targetentity->moduletype() != jumpgate_enttype)) {
		con_warn << "  Jumppoint with invalid target jumppoint '" << 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) {
		entity_info = new core::Info(core::Entity::infotype(), label().c_str());
		set_info(entity_info);
	}
	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);
	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();
	
	// 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->clear_text();
	entity_info->add_text("Jumpgate to the " + target()->zone()->name());

	if (target()) {
		set_flag(core::Entity::Dockable);
	} else {
		unset_flag(core::Entity::Dockable);
	}
}

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

		ship->initiate_jump(this);
		activate();
		if (target()->moduletype() == jumpgate_enttype) {
			static_cast<JumpGate *>(target())->activate();
		}

		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;
	set_state(core::Entity::Normal);
}

void JumpGate::frame(float elapsed)
{
	if (jumpgate_timer > 0) {
		jumpgate_timer -= elapsed;

		if (jumpgate_timer < 0) {
			set_state(core::Entity::NoPower);
			jumpgate_timer = 0;
		}
	}
}

}