123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626 |
-
-
- #include <fstream>
- #include <sstream>
-
- #include "global.h"
- #include "Camera.h"
- #include "Console.h"
- #include "Entity.h"
- #include "Font.h"
- #include "Game.h"
- #include "math/math.h"
- #include "Menu.h"
- #include "Render.h"
- #include "Sound.h"
- #include "TombRaider.h"
- #include "Window.h"
- #include "World.h"
- #include "utils/strings.h"
- #include "utils/time.h"
- #include "OpenRaider.h"
-
- int OpenRaider::loadConfig(const char *config) {
- assert(config != NULL);
- assert(config[0] != '\0');
-
- char *configFile = fullPath(config, 0);
- getConsole() << "Loading config from \"" << configFile << "\"..." << Console::endl;
-
- std::ifstream file(configFile);
- if (!file) {
- getConsole() << "Could not open file!" << Console::endl;
- return -1;
- }
-
- for (std::string line; std::getline(file, line);) {
- if (line.length() == 0)
- continue;
-
- int error = command(line);
- if (error != 0)
- getConsole() << "Error Code: " << error << Console::endl;
- }
-
- file.close();
-
- return 0;
- }
-
- int OpenRaider::command(std::string c) {
-
- size_t comment = c.find_first_of('#');
- if (comment != std::string::npos)
- c.erase(comment);
-
-
- std::stringstream command(c);
- std::string cmd;
- command >> cmd;
- command >> std::boolalpha >> std::ws;
-
- if (cmd.length() == 0)
- return 0;
-
- if (cmd.compare("set") == 0) {
- return set(command);
- } else if (cmd.compare("bind") == 0) {
- std::string a, b;
- if (!(command >> a >> b)) {
- getConsole() << "Invalid use of bind-command" << Console::endl;
- return -1;
- } else {
- return bind(a.c_str(), b.c_str());
- }
- } else if (cmd.compare("quit") == 0) {
- exit(0);
- } else if (cmd.compare("load") == 0) {
- if (!mRunning) {
- getConsole() << "Use load command interactively!" << Console::endl;
- return -999;
- }
- std::string temp;
- command >> temp;
- int error = getGame().loadLevel(temp.c_str());
- return error;
- } else if (cmd.compare("help") == 0) {
- std::string tmp;
- if (!(command >> tmp)) {
- getConsole() << "Available commands:" << Console::endl;
- getConsole() << " load - load a level" << Console::endl;
- getConsole() << " set - set a parameter" << Console::endl;
- getConsole() << " bind - bind a keyboard/mouse action" << Console::endl;
- getConsole() << " animate - [BOOL|n|p] - Animate models" << Console::endl;
- getConsole() << " move - [walk|fly|noclip]" << Console::endl;
-
- getConsole() << " help - print command help" << Console::endl;
- getConsole() << " quit - exit OpenRaider" << Console::endl;
- getConsole() << "Use help COMMAND to get additional info" << Console::endl;
- getConsole() << "Pass BOOLs as true or false" << Console::endl;
- } else {
- return help(tmp);
- }
- } else if (cmd.compare("animate") == 0) {
- if ((!mRunning) || (!getGame().isLoaded())) {
- getConsole() << "Use animate command interactively!" << Console::endl;
- return -999;
- }
- if (command.peek() == 'n') {
-
- if (getRender().getFlags() & Render::fAnimateAllModels) {
- for (unsigned int i = 0; i < getWorld().sizeEntity(); i++) {
- Entity &e = getWorld().getEntity(i);
- SkeletalModel &m = e.getModel();
- if (e.getAnimation() < (m.size() - 1))
- e.setAnimation(e.getAnimation() + 1);
- else
- e.setAnimation(0);
- }
- } else {
- getConsole() << "Animations need to be enabled!" << Console::endl;
- }
- } else if (command.peek() == 'p') {
-
- if (getRender().getFlags() & Render::fAnimateAllModels) {
- for (unsigned int i = 0; i < getWorld().sizeEntity(); i++) {
- Entity &e = getWorld().getEntity(i);
- SkeletalModel &m = e.getModel();
- if (e.getAnimation() > 0)
- e.setAnimation(e.getAnimation() - 1);
- else
- if (m.size() > 0)
- e.setAnimation(m.size() - 1);
- }
- } else {
- getConsole() << "Animations need to be enabled!" << Console::endl;
- }
- } else {
-
- bool b = false;
- if (!(command >> b)) {
- getConsole() << "Pass BOOL to animate command!" << Console::endl;
- return -2;
- }
- if (b)
- getRender().setFlags(Render::fAnimateAllModels);
- else
- getRender().clearFlags(Render::fAnimateAllModels);
- getConsole() << (b ? "Animating all models" : "No longer animating all models") << Console::endl;
- }
- } else if (cmd.compare("move") == 0) {
- if ((!mRunning) || (!getGame().isLoaded())) {
- getConsole() << "Use move command interactively!" << Console::endl;
- return -999;
- }
- std::string temp;
- command >> temp;
- if (temp.compare("walk") == 0) {
- getGame().getLara().setMoveType(Entity::MoveTypeWalk);
- } else if (temp.compare("fly") == 0) {
- getGame().getLara().setMoveType(Entity::MoveTypeFly);
- } else if (temp.compare("noclip") == 0) {
- getGame().getLara().setMoveType(Entity::MoveTypeNoClipping);
- } else {
- getConsole() << "Invalid use of move command (" << temp.c_str() << ")!" << Console::endl;
- return -9;
- }
- getConsole() << temp.c_str() << "ing" << Console::endl;
-
-
- } else {
- getConsole() << "Unknown command: " << cmd.c_str() << Console::endl;
- return -50;
- }
-
- return 0;
- }
-
- int OpenRaider::help(std::string &cmd) {
- if (cmd.compare("set") == 0) {
- getConsole() << "set-Command Usage:" << Console::endl;
- getConsole() << " set VAR VAL" << Console::endl;
- getConsole() << "Available Variables:" << Console::endl;
- getConsole() << " basedir STRING" << Console::endl;
- getConsole() << " pakdir STRING" << Console::endl;
- getConsole() << " audiodir STRING" << Console::endl;
- getConsole() << " datadir STRING" << Console::endl;
- getConsole() << " font STRING" << Console::endl;
- getConsole() << " size INT INT" << Console::endl;
- getConsole() << " fullscreen BOOL" << Console::endl;
- getConsole() << " audio BOOL" << Console::endl;
- getConsole() << " volume BOOL" << Console::endl;
- getConsole() << " mouse_x FLOAT" << Console::endl;
- getConsole() << " mouse_y FLOAT" << Console::endl;
- getConsole() << " fps BOOL" << Console::endl;
- getConsole() << "Enclose STRINGs with \"\"!" << Console::endl;
- } else if (cmd.compare("bind") == 0) {
- getConsole() << "bind-Command Usage:" << Console::endl;
- getConsole() << " bind ACTION KEY" << Console::endl;
- getConsole() << "Available Actions:" << Console::endl;
- getConsole() << " menu" << Console::endl;
- getConsole() << " console" << Console::endl;
- getConsole() << " forward" << Console::endl;
- getConsole() << " backward" << Console::endl;
- getConsole() << " left" << Console::endl;
- getConsole() << " right" << Console::endl;
- getConsole() << " jump" << Console::endl;
- getConsole() << " crouch" << Console::endl;
- getConsole() << " use" << Console::endl;
- getConsole() << " holster" << Console::endl;
- getConsole() << " walk" << Console::endl;
- getConsole() << "Key-Format:" << Console::endl;
- getConsole() << " 'a' or '1' for character/number keys" << Console::endl;
- getConsole() << " \"leftctrl\" for symbols and special keys" << Console::endl;
- } else if (cmd.compare("load") == 0) {
- getConsole() << "load-Command Usage:" << Console::endl;
- getConsole() << " load /path/to/level" << Console::endl;
-
- } else if (cmd.compare("animate") == 0) {
- getConsole() << "animate-Command Usage:" << Console::endl;
- getConsole() << " animate [n|p|BOOL]" << Console::endl;
- getConsole() << "Where the commands have the following meaning:" << Console::endl;
- getConsole() << " BOOL to (de)activate animating all models" << Console::endl;
- getConsole() << " n to step all models to their next animation" << Console::endl;
- getConsole() << " p to step all models to their previous animation" << Console::endl;
- } else {
- getConsole() << "No help available for " << cmd.c_str() << Console::endl;
- return -1;
- }
-
- return 0;
- }
-
- char *OpenRaider::expandDirectoryNames(const char *s) {
- assert(s != NULL);
- assert(s[0] != '\0');
-
- char *result = bufferString("%s", s);
-
- if (mPakDir != NULL) {
- char *tmp = stringReplace(s, "$(pakdir)", mPakDir);
- delete [] result;
- result = tmp;
- }
-
- if (mAudioDir != NULL) {
- char *tmp = stringReplace(s, "$(audiodir)", mAudioDir);
- delete [] result;
- result = tmp;
- }
-
- if (mDataDir != NULL) {
- char *tmp = stringReplace(s, "$(datadir)", mDataDir);
- delete [] result;
- result = tmp;
- }
-
- if (mBaseDir != NULL) {
- char *tmp = stringReplace(result, "$(basedir)", mBaseDir);
- delete [] result;
- result = tmp;
- }
-
- return result;
- }
-
- #define CHANGE_DIR_WITH_EXPANSION(a) do { \
- std::string temp; \
- command >> temp; \
- const char *value = temp.c_str(); \
- char *quotes = stringRemoveQuotes(value); \
- char *tmp = expandDirectoryNames(quotes); \
- a = fullPath(tmp, 0); \
- delete [] tmp; \
- delete [] quotes; \
- } while(false)
-
- int OpenRaider::set(std::istream &command) {
- std::string var;
- command >> var;
-
- if (var.compare("size") == 0) {
- unsigned int w = DEFAULT_WIDTH, h = DEFAULT_HEIGHT;
- if (!(command >> w >> h)) {
- getConsole() << "set-size-Error: Invalid value(s)" << Console::endl;
- return -2;
- }
- getWindow().setSize(w, h);
- } else if (var.compare("fullscreen") == 0) {
- bool fullscreen = false;
- if (!(command >> fullscreen)) {
- getConsole() << "set-fullscreen-Error: Invalid value" << Console::endl;
- return -3;
- }
- getWindow().setFullscreen(fullscreen);
- } else if (var.compare("audio") == 0) {
- bool audio = false;
- if (!(command >> audio)) {
- getConsole() << "set-audio-Error: Invalid value" << Console::endl;
- return -4;
- }
- getSound().setEnabled(audio);
- } else if (var.compare("volume") == 0) {
- float vol = 1.0f;
- if (!(command >> vol)) {
- getConsole() << "set-volume-Error: Invalid value" << Console::endl;
- return -5;
- }
- getSound().setVolume(vol);
- } else if (var.compare("mouse_x") == 0) {
- float sense = 1.0f;
- if (!(command >> sense)) {
- getConsole() << "set-mouse_x-Error: Invalid value" << Console::endl;
- return -6;
- }
- getCamera().setSensitivityX(OR_DEG_TO_RAD(sense));
- } else if (var.compare("mouse_y") == 0) {
- float sense = 1.0f;
- if (!(command >> sense)) {
- getConsole() << "set-mouse_y-Error: Invalid value" << Console::endl;
- return -7;
- }
- getCamera().setSensitivityY(OR_DEG_TO_RAD(sense));
- } else if (var.compare("fps") == 0) {
- bool fps = false;
- if (!(command >> fps)) {
- getConsole() << "set-fps-Error: Invalid value" << Console::endl;
- return -8;
- }
- mFPS = fps;
- } else if (var.compare("basedir") == 0) {
- CHANGE_DIR_WITH_EXPANSION(mBaseDir);
- } else if (var.compare("pakdir") == 0) {
- CHANGE_DIR_WITH_EXPANSION(mPakDir);
- } else if (var.compare("audiodir") == 0) {
- CHANGE_DIR_WITH_EXPANSION(mAudioDir);
- } else if (var.compare("datadir") == 0) {
- CHANGE_DIR_WITH_EXPANSION(mDataDir);
- } else if (var.compare("font") == 0) {
- std::string temp;
- command >> temp;
- const char *value = temp.c_str();
- char *quotes = stringReplace(value, "\"", "");
- char *tmp = expandDirectoryNames(quotes);
- getFont().setFont(tmp);
- delete [] tmp;
- delete [] quotes;
- } else {
- getConsole() << "set-Error: Unknown variable (" << var.c_str() << ")" << Console::endl;
- return -1;
- }
-
- return 0;
- }
-
- int OpenRaider::bind(const char *action, const char *key) {
- assert(action != NULL);
- assert(action[0] != '\0');
- assert(key != NULL);
- assert(key[0] != '\0');
-
- ActionEvents e = stringToActionEvent(action);
- if (e == ActionEventCount) {
- getConsole() << "bind-Error: Unknown action (" << key << " --> " << action << ")" << Console::endl;
- return -1;
- }
-
- KeyboardButton c = stringToKeyboardButton(key);
- if (c == unknownKey) {
- getConsole() << "bind-Error: Unknown key (" << key << ")" << Console::endl;
- return -2;
- }
-
- keyBindings[e] = c;
- return 0;
- }
|