Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: ee2ecc755bdbd4ce514cde7cfdb2e75ecd8a283f (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
/*
   base/racetrack.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 <string>
#include <sstream>

#include "base/racetrack.h"
#include "core/gameserver.h"

namespace base {

/* ---- class CheckPoint ------------------------------------------- */

CheckPoint::CheckPoint(RaceTrack *parent)
{
	entity_eventstate = core::Entity::NoPower;
	parent_track = parent;
	if (parent) {
		entity_color = parent->color();
		entity_color_second = parent->color_second();
		set_zone(parent->zone());
		parent->add_checkpoint(this);
	} else {
		die();
	}
}

CheckPoint::~CheckPoint()
{
	
}

/* ---- class RaceTrack -------------------------------------------- */

RaceTrack::RaceTrack()
{
	track_player = 0;
	track_racestart = 0;
	track_checkpointtime = 0;

	entity_eventstate = core::Entity::NoPower;
}

RaceTrack::~RaceTrack()
{
	track_checkpoints.clear();
}

void RaceTrack::add_checkpoint(CheckPoint *checkpoint)
{
	track_checkpoints.push_back(checkpoint);
}

void RaceTrack::reset()
{
	if (track_player) {
		track_player->set_mission_target(0);
	}
	track_player = 0;
	track_racestart = 0;
	track_checkpointtime = 0;
	
	for (CheckPoints::iterator cpit = track_checkpoints.begin(); cpit != track_checkpoints.end(); ++cpit) {
		(*cpit)->set_eventstate(core::Entity::NoPower);
	}

	set_eventstate(core::Entity::NoPower);
}

void RaceTrack::frame(float seconds)
{
	if (!track_checkpoints.size())
		return;

	if (!track_player) {

		// FIXME this should go through proper collision detection
		for (core::GameServer::Players::iterator it = core::server()->players().begin(); it != core::server()->players().end(); ++it) {
			if ((*it)->control() && (*it)->control()->zone() == zone()) {
				if (math::distance((*it)->control()->location(), location()) <= 1) {
					track_player = (*it);

					track_racestart = core::server()->time();
					set_eventstate(core::Entity::Normal);

					for (CheckPoints::iterator cpit = track_checkpoints.begin(); cpit != track_checkpoints.end(); ++cpit) {
						set_eventstate(core::Entity::Normal);
					}
					
					entity_timer = 5.0f;
					std::string message("^B");
					message.append(track_player->name());
					message.append(" ^Bactivated the race! Race starts in 5...");
					core::server()->broadcast(message);
					track_player->set_mission_target(this);
					return;
				}
			}
		}

		return;
	}

	// FIXME this should go into a proper general function
	// validate current player
	core::Player *player = 0;
	for (core::GameServer::Players::iterator pit = core::server()->players().begin(); (!player) && (pit != core::server()->players().end()); ++pit) {
		if ((*pit) == track_player) {
			player = (*pit);
		}
	}

	if (!player) {
		reset();
		return;
	}

	if (!player->control() || (player->control()->zone() != zone())) {
		reset();
		return;
	}

	if (entity_timer) {

		if (math::distance(player->control()->location(), location()) > 1) {
			std::string message("^BNo cheating!");
			core::server()->broadcast(message);
			reset();
			return;
		}

		if (track_racestart + 1.0f <= core::server()->time()) {
			entity_timer -= 1.0f;
			entity_dirty = true;

			if (entity_timer > 0) {
				std::stringstream msgstr;
				msgstr << "^B" << entity_timer << "...";
				core::server()->broadcast(msgstr.str());
				track_racestart = core::server()->time();
			} else {
				for (CheckPoints::iterator cpit = track_checkpoints.begin(); cpit != track_checkpoints.end(); ++cpit) {
					(*cpit)->set_eventstate(core::Entity::NoPower);
				}
				std::string message("^BGo!");
				core::server()->broadcast(message);

				track_racestart = core::server()->time();
				track_checkpointtime = core::server()->time() + 15.0f;
				track_checkpoint = track_checkpoints.begin();
				(*track_checkpoint)->set_eventstate(core::Entity::Normal);
				track_player->set_mission_target((*track_checkpoint));
			}
		}
		

	} else {

		if (core::server()->time() > track_checkpointtime) {
			std::string message("^BToo slow, race lost!");
			core::server()->broadcast(message);

			reset();
			return;
		}

		if (math::distance(track_player->control()->location(), (*track_checkpoint)->location()) <= 1) {
			CheckPoints::iterator next_checkpoint = track_checkpoint;
			next_checkpoint++;

			if (next_checkpoint != track_checkpoints.end()) {

				std::string message("^BCheckpoint!");
				core::server()->broadcast(message);
				track_checkpointtime = core::server()->time() + 15.0f;
				(*track_checkpoint)->set_eventstate(core::Entity::NoPower);
				track_checkpoint++;
				(*track_checkpoint)->set_eventstate(core::Entity::Normal);
				track_player->set_mission_target((*track_checkpoint));

			} else {

				std::stringstream msgstr;
				msgstr << "^BRace completed in " << core::server()->time() - track_racestart << " seconds!";
				core::server()->broadcast(msgstr.str());
				reset();
			}
		}
	}
}

}