Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: 3505e2ceb563f89a8861be05e89b6cfb1faa05e5 (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
222
223
224
225
226
227
228
229
/*
   core/gameinterface.cc
   This file is part of the Osirion project and is distributed under
   the terms of the GNU General Public License version 2
*/

#include <cstdlib>
#include <iostream>
#include <iomanip>

#include "auxiliary/functions.h"
#include "core/application.h"
#include "core/cvar.h"
#include "core/func.h"
#include "core/info.h"
#include "core/gameinterface.h"
#include "core/player.h"
#include "core/zone.h"
#include "model/material.h"
#include "model/model.h"
#include "sys/sys.h"

namespace core
{

void func_list_players(std::string const &args)
{

	if (args.size()) {
		Player *player = game()->find_player(args);
		if (!player) {
			con_print << "^BPlayer '" + args + "^B' not found";
			return;
		} else {
			player->print();
		}
	} else {
		game()->list_players();
	}
}

const float MIN_DELTA = 10e-10;

Player GameInterface::game_localplayer;

EntityControlable *localcontrol()
{
	if (game()->localplayer())
		return game()->localplayer()->control();
	else
		return 0;

}
Player *localplayer()
{
	return game()->localplayer();
}

GameInterface::GameInterface()
{
	clear();

	game_localplayer.clear();
	if (Cvar::sv_dedicated->value()) {
		game_localplayer.player_name.assign("Console");
	} else {
		game_localplayer.player_name.assign("Player");
		game_localplayer.update_info();
	}

	Func *func = Func::add("list_players", func_list_players);
	func->set_info("get the local list of connected players");

	// TODO this should be moved back into render
	size_t mb = (size_t) Cvar::mem_vertex->value();
	if (mb < 4 * sizeof(float))
		mb = 4 * sizeof(float);
	if (mb > 512)
		mb = 512;
	(*Cvar::mem_vertex) = (float) mb;
	game_vertexarray = new model::VertexArray(mb);
	
	model::Material::init();
	
	game_timestamp = 0;
	game_running = false;
	game_interactive = false;
}

GameInterface::~GameInterface()
{
	Func::remove("list_players");

	game_localplayer.clear();

	clear();

	// delete vertex array
	if (game_vertexarray) {
		delete game_vertexarray;
		game_vertexarray = 0;
	}
}

// clear all game related objects
void GameInterface::clear()
{
	// remove all entities
	Entity::clear();

	// remove all zones
	Zone::clear();

	// remove info records
	Info::clear();

	// remove models
	model::Model::clear();
	
	// remove materials
	model::Material::clear();

	// clear player list
	for (Players::iterator it = game_players.begin(); it != game_players.end(); it++) {
		Player *player = (*it);
		if (player != localplayer()) {
			delete player;
		}
	}
	game_players.clear();
	game_playerlist_timestamp = 0;

	// remove all game functions
	for (Func::Registry::iterator it = Func::registry().begin(); it != Func::registry().end();) {
		if (((*it).second->flags() & Func::Game) == Func::Game) {
			delete(*it).second;
			Func::registry().erase(it++);
		} else {
			++it;
		}
	}

	// remove all game cvars
	for (Cvar::Registry::iterator it = Cvar::registry().begin(); it != Cvar::registry().end();) {
		if (((*it).second->flags() & Cvar::Game) == Cvar::Game) {
			delete(*it).second;
			Cvar::registry().erase(it++);
		} else {
			++it;
		}
	}
	
	game_running = false;
	game_interactive = true;
}

void GameInterface::set_running(const bool running)
{
	game_running = running;
}

void GameInterface::set_interactive(const bool interactive)
{
	game_interactive = interactive;
}

void GameInterface::set_timestamp(const unsigned long timestamp)
{
	game_timestamp = timestamp;
}

void GameInterface::set_playerlist_timestamp(const unsigned long timestamp)
{
	game_playerlist_timestamp = ( timestamp > 0 ? timestamp : 1);
}

void GameInterface::abort() {
	game_running = false;
}

Player *GameInterface::find_player(const std::string &search)
{
	using aux::lowercase;

	std::istringstream searchstr(search);
	int id = 0;
	if (searchstr >> id) {
		for (std::list<Player *>:: iterator it = game_players.begin(); it != game_players.end(); it++) {
			if ((*it)->id() == id) {
				return (*it);
			}
		}
	}

	if (search.size() < 3)
		return 0;

	for (std::list<Player *>:: iterator it = game_players.begin(); it != game_players.end(); it++) {
		if (aux::text_strip_lowercase((*it)->name()).find(lowercase(search)) != std::string::npos)
			return (*it);
	}

	return 0;
}

void GameInterface::list_players()
{
	using namespace std;
	int count = 0;

	for (Players::iterator it = game_players.begin(); it != game_players.end(); it++) {
		const Player *player = (*it);
		con_print << "  "
		<< aux::pad_left(player->name(), 24) << "^N "
		<< "id^B" << setw(4) << player->id() << " "
		<< "ping^B" << setw(4) << player->ping() << "^N "
		<< "level^B" << setw(4) << player->level() << "^N";

		if (player->zone())
			con_print << aux::pad_left(player->zone()->name(), 24) << "^N";

		con_print  << std::endl;
		count++;
	}

	con_print << count << " connected " << aux::plural("player", count) << std::endl;
}

}