diff options
author | Stijn Buys <ingar@osirion.org> | 2008-02-17 18:59:52 +0000 |
---|---|---|
committer | Stijn Buys <ingar@osirion.org> | 2008-02-17 18:59:52 +0000 |
commit | 982562fa19bb87a3dab352e562f386f61c171b7b (patch) | |
tree | aeade8d5b7d3c68f5c222af1d8ecc6a734e1b43f /src/core/application.cc | |
parent | d198b7b8d9ff713d891f35ab173d1f428f610e7d (diff) |
major rewrite of Cvar, Func and Entity
Diffstat (limited to 'src/core/application.cc')
-rw-r--r-- | src/core/application.cc | 277 |
1 files changed, 139 insertions, 138 deletions
diff --git a/src/core/application.cc b/src/core/application.cc index ce7460f..5d313ad 100644 --- a/src/core/application.cc +++ b/src/core/application.cc @@ -11,25 +11,18 @@ #include <vector> #include <sstream> -#include "math/mathlib.h" #include "sys/sys.h" +#include "math/mathlib.h" #include "filesystem/filesystem.h" #include "core/application.h" -#include "core/core.h" +#include "core/cvar.h" #include "core/entity.h" #include "core/func.h" -#include "core/cvar.h" #include "core/netserver.h" namespace core { -Cvar sv_dedicated; -Cvar sv_private; - -Cvar net_host; -Cvar net_port; - // return the global application object Application *application() { @@ -37,107 +30,88 @@ Application *application() } // --------------- engine functions ------------------------------ -void func_print(std::stringstream &args) +void func_print(std::string const &args) { - char text[MAXCMDSIZE]; - if (args.getline(text, MAXCMDSIZE)) { - // remove the leading space - if (text[0] && text[1]) { - con_print << text+1 << std::endl; - } - } + con_print << args << "\n"; } -void func_help(std::stringstream &args) +void func_help(std::string const &args) { - con_print << "This is the help function" << std::endl; + con_print << "This is the help function\n"; } -void func_quit(std::stringstream &args) +void func_quit(std::string const &args) { - if (Application::instance()) { - Application::instance()->shutdown(); - Application::instance()->quit(0); - } + application()->shutdown(); + application()->quit(0); } -void func_connect(std::stringstream &args) +void func_connect(std::string const &args) { + std::istringstream argstream(args); std::string host; - if (!(args >> host)) + if (!(argstream >> host)) host.clear(); - if (Application::instance()) { - Application::instance()->connect(host); - } -} - -void func_disconnect(std::stringstream &args) -{ - if (Application::instance()) - Application::instance()->disconnect(); -} - -void func_list_func(std::stringstream &args) -{ - func::list(); -} - -void func_list_var(std::stringstream &args) -{ - cvar::list(); + application()->connect(host); } -void func_list_ent(std::stringstream &args) +void func_disconnect(std::string const &args) { - entity::list(); + application()->disconnect(); } -void func_say(std::stringstream &args) +// FIXME this is a game function +void func_say(std::string const &args) { if (application()->netserver) { std::ostringstream osstream; - osstream << "msg public " << localplayer.name << " " << args.str() << "\n"; + osstream << "msg public " << Player::local.name() << " " << args << "\n"; application()->netserver->broadcast(osstream.str()); - con_print << localplayer.name << ": " << args.str() << std::endl; + con_print << Player::local.name() << ": " << args << "\n"; } else if (application()->netconnection.connected()) { std::ostringstream osstream; - osstream << args.str() << std::endl; - application()->netconnection.send(osstream.str()); + osstream << args << "\n"; + application()->netconnection.send(args); } else if (game() && game()->connected) { - con_print << args.str() << std::endl; + con_print << args << "\n"; } else con_print << "Not connected."; } -void func_name(std::stringstream &args) { +void func_name(std::string const &args) { + std::istringstream argstream(args); std::string name; - if (args >> name) { + if (argstream >> name) { if (name.size() > 16) name = name.substr(0,16); } else { - con_print << "name " << localplayer.name << std::endl; + con_print << "name " << Player::local.name() << "\n"; return; } - if (name == localplayer.name) { - con_print << "name " << name << std::endl; + if (name == Player::local.name()) { + con_print << "name " << name << "\n"; return; } if (application()->netserver) { std::ostringstream osstream; - osstream << "msg info " << localplayer.name << " renamed to " << name << "\n"; + osstream << "msg info " << Player::local.name() << " renamed to " << name << "\n"; application()->netserver->broadcast(osstream.str()); - con_print << "msg info " << localplayer.name << " renamed to " << name << std::endl; + + con_print << "msg info " << Player::local.name() << " renamed to " << name << "\n"; } else if (application()->netconnection.connected()) { std::ostringstream osstream; - osstream << args.str() << std::endl; + osstream << "name " << name << "\n"; application()->netconnection.send(osstream.str()); + + con_print << "name " << name << "\n"; } else { - con_print << "name " << name << std::endl; + con_print << "name " << name << "\n"; } - localplayer.name = name; + + Player::local.player_name = name; } // --------------- signal_handler ----------------------------------- @@ -150,17 +124,17 @@ extern "C" void signal_handler(int signum) case SIGQUIT: case SIGTERM: if (Application::instance()) { - con_warn << "Received signal " << signum << ", shutting down..." << std::endl; - Application::instance()->shutdown(); - Application::instance()->quit(0); + con_warn << "Received signal " << signum << ", shutting down...\n"; + application()->shutdown(); + application()->quit(0); } else { - std::cerr << "Received signal " << signum << ", terminated..." << std::endl; - sys::quit(1); + std::cerr << "Received signal " << signum << ", terminated...\n"; + application()->quit(1); } break; default: - std::cerr << "Received signal " << signum << ", terminated..." << std::endl; - sys::quit(1); + std::cerr << "Received signal " << signum << ", terminated...\n"; + application()->quit(1); break; } } @@ -172,7 +146,7 @@ Application *Application::application_instance = 0; Application::Application() { if (application_instance) { - std::cerr << "multiple core::Application instances!" << std::endl; + std::cerr << "multiple core::Application instances!\n"; sys::quit(2); } @@ -209,60 +183,66 @@ bool Application::connected() const void Application::init() { - con_debug << "Debug messages enabled" << std::endl; - con_print << "Initializing core..." << std::endl; + con_debug << "Debug messages enabled\n"; + con_print << "Initializing core...\n"; filesystem::init(); + CommandBuffer::init(); + gameinterface_preload = core::GameInterface::gameinterface_instance; core::GameInterface::gameinterface_instance = 0; if (gameinterface_preload) { - con_print << " preloaded game found: " << gameinterface_preload->name() << std::endl; + con_print << " preloaded game found: " << gameinterface_preload->name() << "\n"; } game_time = 0; - // dedicated server, client should have set this to 0 - core::sv_dedicated = core::cvar::get("sv_dedicated", "1", core::cvar::ReadOnly); + // dedicated server should set this to 1 + Cvar::sv_dedicated = Cvar::get("sv_dedicated", "0", Cvar::ReadOnly); - // private server for the client, server should have set this to 0 - core::sv_private = core::cvar::get("sv_private", "0"); + // client can set this to 1 + Cvar::sv_private = Cvar::get("sv_private", "0"); - if (sv_dedicated->value()) - localplayer.name = "Console"; + if (Cvar::sv_dedicated->value()) + Player::local.player_name = "Console"; else - localplayer.name = "Player0"; + Player::local.player_name = "Player0"; // network settings - core::net_host = core::cvar::get("net_host", "0.0.0.0"); - core::net_port = core::cvar::get("net_port", "8042"); + Cvar::net_host = Cvar::get("net_host", "0.0.0.0"); + Cvar::net_port = Cvar::get("net_port", "8042"); - // register our functions - func::add("print", func_print); - func::add("help", func_help); - func::add("quit", func_quit); + // register our engine functions + Func::add("print", func_print); + Func::add("help", func_help); + Func::add("quit", func_quit); - func::add("connect", func_connect); - func::add("disconnect", func_disconnect); + Func::add("connect", func_connect); + Func::add("disconnect", func_disconnect); - func::add("list_var", func_list_var); - func::add("list_func", func_list_func); - func::add("list_ent", func_list_ent); - - func::add("say", func_say); - func::add("name", func_name); + Func::add("say", func_say); + Func::add("name", func_name); } void Application::shutdown() { - con_print << "Shutting down core..." << std::endl; - - if (game() && game()->connected) + con_print << "Shutting down core...\n"; + + if (connected()) disconnect(); - if (netserver) { - delete netserver; - netserver = 0; - } + // remove our engine functions + Func::remove("print"); + Func::remove("help"); + Func::remove("quit"); + + Func::remove("connect"); + Func::remove("disconnect"); + + Func::remove("say"); + Func::remove("name"); + + CommandBuffer::shutdown(); filesystem::shutdown(); } @@ -272,10 +252,36 @@ void Application::quit(int status) sys::quit(status); } +// clear all game realted objects +void Application::clear() +{ + // remove all entities + for (std::map<unsigned int, Entity*>::iterator it = Entity::registry.begin(); it != Entity::registry.end(); it++) { + delete (*it).second; + } + Entity::registry.clear(); + + // remove all game functions + for (std::map<std::string, Func*>::iterator it = Func::registry.begin(); it != Func::registry.end(); it++) { + if ( ((*it).second->flags() & Func::Game) == Func::Game) { + delete (*it).second; + Func::registry.erase(it); + } + } + + // remove all game cvars + for (std::map<std::string, Cvar*>::iterator it = Cvar::registry.begin(); it != Cvar::registry.end(); it++) { + if ( ((*it).second->flags() & Cvar::Game) == Cvar::Game) { + delete (*it).second; + Cvar::registry.erase(it); + } + } +} + void Application::connect(std::string const &host) { if (game() && game()->connected || netconnection.connected()) { - con_warn << "Connected. Disconnect first." << std::endl; + con_warn << "Connected. Disconnect first.\n"; return; } @@ -285,6 +291,8 @@ void Application::connect(std::string const &host) delete core::GameInterface::gameinterface_instance; } + clear(); + if (host.size()) { // connect to remote core core::GameInterface::gameinterface_instance = 0; @@ -296,38 +304,37 @@ void Application::connect(std::string const &host) if (str >> port) { remotehost.erase(found, std::string::npos); } else { - con_print << "Invalid hostname '" << remotehost << "'" << std::endl; + con_print << "Invalid hostname '" << remotehost << "'\n"; return; } } netconnection.connect(remotehost, port); if (netconnection.connected()) { - con_print << "Connected." << std::endl; + con_print << "Connected.\n"; } else { netconnection.disconnect(); - con_warn << "Could not connect to '" << host << "'" << std::endl; + con_warn << "Could not connect to '" << host << "'\n"; } } else { // use preloaded game core::GameInterface::gameinterface_instance = gameinterface_preload; // reset everything - entity::clear(); game_time = 0; if (game()->connected = game()->init()) { - con_print << "Connected." << std::endl; + con_print << "Connected.\n"; } else { - con_warn << "Could not connect." << std::endl; + con_warn << "Could not connect.\n"; return; } - if (!netserver && (core::sv_dedicated->value() || core::sv_private->value())) { - netserver = new NetServer(net_host->text(), (unsigned int)net_port->value()); + if (!netserver && (Cvar::sv_dedicated->value() || Cvar::sv_private->value())) { + netserver = new NetServer(Cvar::net_host->str(), (unsigned int)Cvar::net_port->value()); if (!netserver->valid()) { delete netserver; - if (core::sv_dedicated->value()) + if (Cvar::sv_dedicated->value()) shutdown(); } } @@ -343,13 +350,13 @@ void Application::disconnect() if (netconnection.connected()) { netconnection.disconnect(); - con_print << "Disconnected." << std::endl; + con_print << "Disconnected.\n"; return; } if (game()) { if (!game()->connected) { - con_warn << "Not connected." << std::endl; + con_warn << "Not connected.\n"; return; } @@ -357,26 +364,9 @@ void Application::disconnect() game()->connected = false; game_time = 0; - // remove all entities - entity::clear(); - - // remove all game functions - for (std::map<std::string, Func>::iterator it = func::registry.begin(); it != func::registry.end(); it++) { - if ( ((*it).second->flags() & func::Game) == func::Game) { - delete (*it).second; - func::registry.erase(it); - } - } - - // remove all game cvars - for (std::map<std::string, Cvar>::iterator it = cvar::registry.begin(); it != cvar::registry.end(); it++) { - if ( ((*it).second->flags() & cvar::Game) == cvar::Game) { - delete (*it).second; - cvar::registry.erase(it); - } - } + clear(); - con_print << "Disconnected." << std::endl; + con_print << "Disconnected.\n"; } } @@ -384,7 +374,22 @@ void Application::frame(float seconds) { if (seconds == 0.0f) return; - + + // execute commands in the buffer + CommandBuffer::exec(); + + // update entities + if (connected()) { + std::map<unsigned int, Entity *>::iterator it; + for (it=Entity::registry.begin(); it != Entity::registry.end(); it++) { + Entity *entity = (*it).second; + if ((entity->type() == Entity::Controlable) || (entity->type() == Entity::Dynamic)) { + entity->frame(seconds); + } + } + } + + // netstuff if (netconnection.connected()) { netconnection.frame(seconds); // TODO this should come from server @@ -396,15 +401,11 @@ void Application::frame(float seconds) } if (game() && game()->connected) { - entity::frame(seconds); game_time += seconds; game()->frame(seconds); } } - - // execute commands in the buffer - commandbuffer::execute(); } } |