Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: b514e57508ab53738bb9a73969623bea275fe7b7 (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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
/*
   client/client.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 <SDL/SDL.h>

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

#include "audio/audio.h"
#include "audio/sources.h"
#include "client/client.h"
#include "client/video.h"
#include "client/input.h"
#include "client/soundext.h"
#include "core/core.h"
#include "core/loader.h"
#include "core/zone.h"
#include "render/render.h"
#include "ui/ui.h"

namespace client
{

core::Cvar *cl_framerate = 0;
core::Cvar *cl_frametime = 0;

Client app;

//--- public ------------------------------------------------------


Client *client()
{
	return &app;
}

void run(int count, char **arguments)
{
	std::cout << core::name() << " " << core::version() << std::endl;

	for (int i = 0; i < count; i++)
		std::cout << arguments[i] << " ";
	std::cout << std::endl;

	app.init(count, arguments);
	app.run();
	app.shutdown();
}

//--- private -----------------------------------------------------

void Client::quit(int status)
{
	SDL_Quit();
	core::Application::quit(status);
}

void Client::init(int count, char **arguments)
{
	client_worldview = 0;
	con_print << "^BInitializing client..." << std::endl;

	// initialize core
	core::Cvar::sv_private = core::Cvar::set("sv_private", "0");
	core::Application::init(count, arguments);

	// engine variables
	cl_framerate = core::Cvar::get("cl_framerate", "125", core::Cvar::Archive);
	cl_framerate->set_info("[int] client framerate in frames/sec");

	// player info variables
	core::Cvar *cvar = 0;

	cvar = core::Cvar::get("cl_name", "Player", core::Cvar::Archive | core::Cvar::Info);
	cvar->set_info("[str] player name");

	cvar = core::Cvar::get("cl_color", "1.0 1.0 1.0", core::Cvar::Archive | core::Cvar::Info);
	cvar->set_info("[r g b] player primary color");

	cvar = core::Cvar::get("cl_colorsecond", "1.0 1.0 1.0", core::Cvar::Archive | core::Cvar::Info);
	cvar->set_info("[r g b] player secondary color");

	cvar = core::Cvar::get("rconpassword", "", core::Cvar::Archive | core::Cvar::Info);
	cvar->set_info("[string] password for remote console access");
	
	snd_volume = core::Cvar::get("snd_volume", "0.8", core::Cvar::Archive);
	snd_volume->set_info("[float] master volume from 0 (mute) to 1 (max volume)");
	
	snd_engines = core::Cvar::get("snd_engines", "1", core::Cvar::Archive);
	snd_engines->set_info("[bool] enable or disable engine sounds");

	snd_doppler = core::Cvar::get("snd_doppler", "0", core::Cvar::Archive);
	snd_doppler->set_info("[bool] enable or disable doppler effect");
	
	// initialize SDL, but do not initialize any subsystems
	SDL_Init(0);

	// initialize user interface
	ui::init();

	client_worldview = new WorldView(ui::root());
	
	client_testmodelview = new TestModelView(ui::root());
	client_testmodelview->hide();
	
	// Initialize the video subsystem
	if (!video::init()) {
		quit(1);
	}

	// initialize audio
	audio::init();

	// initialize input
	input::init();

	// add engine functions
	core::Func *func = 0;

	func = core::Func::add("r_restart", Client::func_r_restart);
	func->set_info("restart render subsystem");

	func = core::Func::add("snd_restart", Client::func_snd_restart);
	func->set_info("restart audio subsystem");

	func = core::Func::add("list_ui", func_list_ui);
	func->set_info("list user interface widgets");

	func = core::Func::add("list_menu", func_list_menu);
	func->set_info("list available menus");

	func = core::Func::add("ui", func_ui);
	func->set_info("[command] user interface functions");

	func = core::Func::add("ui_restart", func_ui_restart);
	func->set_info("reload user interface files");

	func = core::Func::add("ui_console", func_ui_console);
	func->set_info("toggle console");

	func = core::Func::add("ui_chat", func_ui_chat);
	func->set_info("toggle chat window");

	func = core::Func::add("ui_chatbar", func_ui_chatbar);
	func->set_info("toggle chat bar");
	
	func = core::Func::add("ui_inventory", func_ui_inventory);
	func->set_info("toggle inventory");

	func = core::Func::add("ui_map", func_ui_map);
	func->set_info("toggle map");

	func = core::Func::add("ui_menu", func_ui_menu);
	func->set_info("toggle main menu");
	
	func = core::Func::add("testmodel", func_testmodel);
	func->set_info("[str] load and view a single 3D model");

	func = core::Func::add("menu", func_menu);
	func->set_info("[command] menu functions");

	func = core::Func::add("view", func_view);
	func->set_info("[command] view menu functions");

	previous_timestamp = 0;
}

void Client::run()
{
	con_print << "^BRunning client..." << std::endl;

	// seed random generator
	unsigned int seed = (unsigned int) SDL_GetTicks();
	srand(seed);

	// default framerate 125fps, 8 milliseconds
	unsigned long client_frame_lenght = 8;
	unsigned long  client_current_timestamp = 0;

	while (true) {
		// current time in microseconds
		client_current_timestamp = timestamp();

		// calculate the desired frame length
		if (cl_framerate->value() < 0) {
			(*cl_framerate) = 0.0f;
		} else if (cl_framerate->value() > 1000.0f) {
			(*cl_framerate) = 1000.0f;
		}

		if (cl_framerate->value()) {
			client_frame_lenght = (unsigned long) floorf(1000.0f / cl_framerate->value());
			if (client_frame_lenght < 1)
				client_frame_lenght = 1;
		} else {
			client_frame_lenght = 1;
		}

		frame();
		
		// sleep for the remainder of the frame
		const unsigned long elapsed = timestamp() - client_current_timestamp;
		if (elapsed < client_frame_lenght - 1) {
			sys::sleep (client_frame_lenght - elapsed -1);
		}
		
		while (timestamp() < client_current_timestamp + client_frame_lenght) {
			// busy waiting for the last microsecond
		}
	}

}

void Client::frame()
{
	input::frame();

	core::Application::frame();

	if (!connected()) {
		const std::string module_label(core::Loader::label());

		// load the intro if nothing is running
		if (load("intro")) {
			connect("");
			if (module_label.size())
				load(module_label);
		}
		// show the console if everything fails
		if (!connected() && !ui::console()->visible()) {
			ui::console()->toggle();
		}
	} else if (!ui::root()->active()) {

		// show the main menu on non-interactive modules
		if (!core::game()->interactive()) {
			ui::root()->show_menu("main");

			// show the join menu when player does not control an entity
		} else if (core::game()->time() && !core::localcontrol()) {
			ui::root()->show_menu("join");
		}
		
		if (testmodelview()->visible()) {
			testmodelview()->raise();
		}		
		if (ui::console()->visible()) {
			ui::console()->raise();
			ui::console()->set_focus();
		}		
	} else {
		if (core::localcontrol()) {

			// hide join menu
			if (ui::root()->active()->label().compare("join") == 0) {
				ui::root()->hide_menu();
			}
		}
	}

	const float now = timestamp();
	video::frame((float)(now - previous_timestamp) / 1000.0f);
	previous_timestamp = now;
}

void Client::shutdown()
{
	con_print << "^BShutting down client..." << std::endl;

	if (connected()) disconnect();

	core::Func::remove("r_restart");
	core::Func::remove("snd_restart");
	core::Func::remove("list_menu");
	core::Func::remove("list_ui");
	core::Func::remove("ui");
	core::Func::remove("ui_restart");
	core::Func::remove("ui_console");
	core::Func::remove("ui_chat");
	core::Func::remove("ui_chatbar");
	core::Func::remove("ui_map");
	core::Func::remove("ui_menu");
	core::Func::remove("menu");
	core::Func::remove("view");

	audio::shutdown();

	input::shutdown();

	video::shutdown();

	ui::shutdown();

	core::Application::shutdown();

	quit(0);
}

/* -- notifications from core::Application ------------------------- */

void Client::notify_connect()
{
	video::set_loader_message();
	video::frame_loader();

	worldview()->clear();
	ui::root()->hide_menu();

	video::set_caption();
}

void Client::notify_disconnect()
{
	audio::reset();
	render::reset();
	input::reset();

	worldview()->clear();

	video::set_caption();
}

void Client::notify_zonechange()
{
	video::set_loader_message();
	video::frame_loader();

	// unload entity sounds
	for (core::Entity::Registry::iterator it = core::Entity::registry().begin(); it != core::Entity::registry().end(); it++) {
		core::Entity *entity = (*it).second;

		if (ext_sound(entity))
			delete ext_sound(entity);
	}

	render::unload();
}

void Client::notify_sound(const char *name)
{
	audio::play(name);
}

void Client::notify_message(const char *message)
{
	std::string text(message);
	notify_message(core::Message::Info, text);
}

void Client::notify_message(const std::string &message)
{
	notify_message(core::Message::Info, message);
}

void Client::notify_message(const core::Message::Channel channel, const std::string &message)
{

	switch (channel) {

		case core::Message::Info:	// Info message
			break;
		
		case core::Message::RCon:	// RCon message
			break;

		case core::Message::Public:	// Public chat message
			audio::play("com/chat");
			break;

		case core::Message::Local:	// Local chat message
			break;

		case core::Message::Private:	// Private chat message
			audio::play("com/priv");
			break;

		default:
			break;
	}

	if (worldview()) {
		worldview()->event_text(message);
	}

	con_print << message << std::endl;
}

void Client::notify_loader(const std::string &message)
{
	video::set_loader_message(message.c_str());
}

//--- engine functions --------------------------------------------

void Client::func_snd_restart(std::string const &args)
{
	// unload entity sounds
	for (core::Entity::Registry::iterator it = core::Entity::registry().begin(); it != core::Entity::registry().end(); it++) {
		core::Entity *entity = (*it).second;

		if (ext_sound(entity))
			delete ext_sound(entity);
	}

	audio::reset();
}

void Client::func_r_restart(std::string const &args)
{
	video::restart();
	video::set_loader_message();
	video::frame_loader();
}

/* ---- func_ui ---------------------------------------------------- */

void Client::func_list_ui(std::string const &args)
{
	if (ui::root()) {
		ui::root()->list();
	}
}

void Client::func_ui_restart(std::string const &args)
{
	if (ui::root()) {
		ui::root()->load_settings();
	}
}

void Client::func_ui_console(std::string const &args)
{
	ui::console()->toggle();
}

void Client::func_list_menu(std::string const &args)
{
	if (ui::root()) {
		ui::root()->list_menus();
	}
}

void Client::func_ui_help()
{
	con_print << "^BUser interface functions" << std::endl;
	con_print << "  ui help          show this help" << std::endl;
	con_print << "  ui debug         toggle debug mode" << std::endl;
	con_print << "  ui list          list widgets" << std::endl;
	con_print << "  ui visible       list visible widgets" << std::endl;
	con_print << "  ui restart       reload user interface files" << std::endl;
}

void Client::func_ui(std::string const &args)
{
	if (!ui::root()) {
		con_warn << "User Interface not available!" << std::endl;
		return;
	}

	if (!args.size()) {
		func_ui_help();
		return;
	}
	std::stringstream argstr(args);
	std::string command;
	argstr >> command;
	aux::to_label(command);

	if (command.compare("help") == 0) {
		func_ui_help();
	} else if (command.compare("debug") == 0) {
		ui::UI::ui_debug = !ui::UI::ui_debug;
	} else if (command.compare("list") == 0) {
		ui::root()->list();
	} else if (command.compare("visible") == 0) {
		ui::root()->list_visible();
	} else if (command.compare("restart") == 0) {
		ui::root()->load_settings();
	} else {
		func_ui_help();
	}
}

void Client::func_ui_chat(std::string const &args)
{
	if (client()->connected() && client()->worldview()->playerview()->visible()) {
		client()->worldview()->playerview()->toggle_chat();
	}
}

void Client::func_ui_chatbar(std::string const &args)
{
	if (client()->connected() && client()->worldview()->playerview()->visible()) {
		client()->worldview()->playerview()->toggle_chatbar();
	}
}

void Client::func_ui_inventory(std::string const &args)
{
	if (client()->connected() && client()->worldview()->playerview()->visible()) {
		client()->worldview()->playerview()->toggle_inventory();
	}
}

void Client::func_ui_map(std::string const &args)
{
	if (client()->connected() && client()->worldview()->playerview()->visible()) {
		client()->worldview()->playerview()->toggle_map();
	}
}

void Client::func_ui_menu(std::string const &args)
{
	if (client()->connected()) {
		if (ui::root()->active()) {
			ui::root()->hide_menu();
		} else {
			// show the main menu on non-interactive modules
			if (!core::game()->interactive()) {
				ui::root()->show_menu("main");
			} else {
				ui::root()->show_menu("game");
			}
		}
	} else {
		con_print << "Not connected." << std::endl;
	}
}

// entity menus
void Client::func_view(std::string const &args)
{
	if (client()->worldview()) {
		client()->worldview()->playerview()->show_menu(args);
	}
}

// global menus
void Client::func_menu(std::string const &args)
{
	if (!ui::root()) {
		con_warn << "User Interface not available!" << std::endl;
		return;
	}

	if (!args.size()) {
		con_print << "^Bmenu functions" << std::endl;
		con_print << "  menu help          show this help" << std::endl;
		con_print << "  menu list          list available menus" << std::endl;
		con_print << "  menu [name]        show a menu" << std::endl;
		con_print << "  menu back          return to the previous menu" << std::endl;
		con_print << "  menu previous      return to the previous menu" << std::endl;
		con_print << "  menu close         close the current menu" << std::endl;
		con_print << "  menu hide          hide the current menu" << std::endl;
		ui::root()->list_menus();
	}

	std::stringstream argstr(args);
	std::string command;
	argstr >> command;

	aux::to_label(command);

	if (command.compare("hide") == 0) {
		ui::root()->hide_menu();

	} else if (command.compare("close") == 0) {
		ui::root()->hide_menu();

	} else if (command.compare("back") == 0) {
		ui::root()->previous_menu();

	} else if (command.compare("previous") == 0) {
		ui::root()->previous_menu();

	} else if (command.compare("list") == 0) {
		ui::root()->list_menus();

	} else {
		ui::root()->show_menu(command.c_str());
	}
}

void Client::func_testmodel(std::string const &args)
{
	// if testmodel is called from the command line, no module has been loaded yet
	if (!client()->connected()) {
		const std::string module_label(core::Loader::label());
		
		// load the intro if nothing is running
		if (client()->load("intro")) {
			client()->connect("");
			if (module_label.size())
				client()->load(module_label);
		}
	}
	
	if (!client()->connected()) {
		client()->client_testmodelview->hide();
		ui::console()->show();
		return;
	}
	
	std::string modelname(args);
	aux::trim(modelname);
	
	if (!modelname.size()) {
		con_print << "usage: testmodel [model name]" << std::endl;
	}

	//video::set_loader_message();
	//video::frame_loader();
	
	client()->client_testmodelview->set_modelname(modelname);
	client()->client_testmodelview->raise();	// raise the window
	client()->client_testmodelview->show();

	if (ui::console()->visible()) {
		ui::console()->raise();
		ui::console()->set_focus();
	}
}

} // namespace client