Open Source Tomb Raider Engine
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Command.cpp 26KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  1. /*!
  2. * \file src/Command.cpp
  3. * \brief OpenRaider command implementation
  4. *
  5. * \author xythobuz
  6. */
  7. #include <fstream>
  8. #include "WindowSDL.h"
  9. #include "global.h"
  10. #include "Console.h"
  11. #include "Entity.h"
  12. #include "Font.h"
  13. #include "Game.h"
  14. #include "math/math.h"
  15. #include "Menu.h"
  16. #include "Sound.h"
  17. #include "TombRaider.h"
  18. #include "utils/strings.h"
  19. #include "utils/time.h"
  20. #include "OpenRaider.h"
  21. int OpenRaider::loadConfig(const char *config) {
  22. assert(config != NULL);
  23. assert(config[0] != '\0');
  24. char *configFile = fullPath(config, 0);
  25. getConsole().print("Loading config from \"%s\"...", configFile);
  26. std::ifstream file(configFile);
  27. if (!file) {
  28. getConsole().print("Could not open file!");
  29. return -1;
  30. }
  31. for (std::string line; std::getline(file, line);) {
  32. if (line.length() == 0)
  33. continue;
  34. int error = command(line);
  35. if (error != 0)
  36. getConsole().print("Error Code: %d", error);
  37. }
  38. file.close();
  39. return 0;
  40. }
  41. int OpenRaider::command(const char *command) {
  42. std::string tmp(command);
  43. return this->command(tmp);
  44. }
  45. int OpenRaider::command(std::string &c) {
  46. // Remove comment, if any
  47. size_t comment = c.find_first_of('#');
  48. if (comment != std::string::npos)
  49. c.erase(comment);
  50. // Execute command
  51. std::stringstream command(c);
  52. std::string cmd;
  53. command >> cmd;
  54. command >> std::boolalpha >> std::ws;
  55. if (cmd.length() == 0)
  56. return 0;
  57. if (cmd.compare("set") == 0) {
  58. return set(command);
  59. } else if (cmd.compare("bind") == 0) {
  60. std::string a, b;
  61. if (!(command >> a >> b)) {
  62. getConsole().print("Invalid use of bind-command");
  63. return -1;
  64. } else {
  65. return bind(a.c_str(), b.c_str());
  66. }
  67. } else if (cmd.compare("quit") == 0) {
  68. exit(0);
  69. } else if (cmd.compare("load") == 0) {
  70. if (!mRunning) {
  71. getConsole().print("Use load command interactively!");
  72. return -999;
  73. }
  74. std::string temp;
  75. command >> temp;
  76. char *tmp = bufferString("%s/%s", mPakDir, temp.c_str());
  77. int error = getGame().loadLevel(tmp);
  78. delete [] tmp;
  79. return error;
  80. } else if (cmd.compare("help") == 0) {
  81. std::string tmp;
  82. if (!(command >> tmp)) {
  83. getConsole().print("Available commands:");
  84. getConsole().print(" load - load a level");
  85. getConsole().print(" set - set a parameter");
  86. getConsole().print(" bind - bind a keyboard/mouse action");
  87. getConsole().print(" animate - [BOOL|n|p] - Animate models");
  88. /*
  89. getConsole().print(" sshot - make a screenshot");
  90. getConsole().print(" move - [walk|fly|noclip]");
  91. getConsole().print(" sound - INT - Test play sound");
  92. getConsole().print(" mode - MODE - Render mode");
  93. getConsole().print(" light - BOOL - GL Lights");
  94. getConsole().print(" fog - BOOL - GL Fog");
  95. getConsole().print(" viewmodel - INT - Change Laras model");
  96. getConsole().print(" pos - Print position info");
  97. getConsole().print(" vmodel - BOOL - View Model");
  98. getConsole().print(" ralpha - BOOL - Room Alpha");
  99. getConsole().print(" vis - BOOL - Use Portals");
  100. getConsole().print(" upf - BOOL - Update Room List Per Frame");
  101. getConsole().print(" entmodel - BOOL");
  102. getConsole().print(" oneroom - BOOL");
  103. getConsole().print(" allrooms - BOOL");
  104. getConsole().print(" ponytail - BOOL");
  105. getConsole().print(" pigtail - BOOL");
  106. getConsole().print(" ponypos - FLOAT FLOAT FLOAT FLOAT - x y z angle");
  107. */
  108. getConsole().print(" help - print command help");
  109. getConsole().print(" quit - exit OpenRaider");
  110. getConsole().print("Use help COMMAND to get additional info");
  111. getConsole().print("Pass BOOLs as true or false");
  112. } else {
  113. return help(tmp);
  114. }
  115. } else if (cmd.compare("animate") == 0) {
  116. if ((!mRunning) || (!getGame().isLoaded())) {
  117. getConsole().print("Use animate command interactively!");
  118. return -999;
  119. }
  120. if (command.peek() == 'n') {
  121. // Step all skeletal models to their next animation
  122. if (getRender().getFlags() & Render::fAnimateAllModels) {
  123. for (unsigned int i = 0; i < getWorld().sizeEntity(); i++) {
  124. Entity &e = getWorld().getEntity(i);
  125. SkeletalModel &m = e.getModel();
  126. if (e.getAnimation() < (m.size() - 1))
  127. e.setAnimation(e.getAnimation() + 1);
  128. else
  129. e.setAnimation(0);
  130. }
  131. } else {
  132. getConsole().print("Animations need to be enabled!");
  133. }
  134. } else if (command.peek() == 'p') {
  135. // Step all skeletal models to their previous animation
  136. if (getRender().getFlags() & Render::fAnimateAllModels) {
  137. for (unsigned int i = 0; i < getWorld().sizeEntity(); i++) {
  138. Entity &e = getWorld().getEntity(i);
  139. SkeletalModel &m = e.getModel();
  140. if (e.getAnimation() > 0)
  141. e.setAnimation(e.getAnimation() - 1);
  142. else
  143. if (m.size() > 0)
  144. e.setAnimation(m.size() - 1);
  145. }
  146. } else {
  147. getConsole().print("Animations need to be enabled!");
  148. }
  149. } else {
  150. // Enable or disable animating all skeletal models
  151. bool b = false;
  152. if (!(command >> b)) {
  153. getConsole().print("Pass BOOL to animate command!");
  154. return -2;
  155. }
  156. if (b)
  157. getRender().setFlags(Render::fAnimateAllModels);
  158. else
  159. getRender().clearFlags(Render::fAnimateAllModels);
  160. getConsole().print(b ? "Animating all models" : "No longer animating all models");
  161. }
  162. /*
  163. } else if (cmd.compare("mode") == 0) {
  164. std::string mode;
  165. command >> mode;
  166. if (!getGame().isLoaded()) {
  167. getConsole().print("Load a level to set the mode!");
  168. return -1;
  169. }
  170. if (mode.compare("wireframe") == 0) {
  171. getRender().setMode(Render::modeWireframe);
  172. getConsole().print("Wireframe mode");
  173. } else if (mode.compare("solid") == 0) {
  174. getRender().setMode(Render::modeSolid);
  175. getConsole().print("Solid mode");
  176. } else if (mode.compare("texture") == 0) {
  177. getRender().setMode(Render::modeTexture);
  178. getConsole().print("Texture mode");
  179. } else if (mode.compare("vertexlight") == 0) {
  180. getRender().setMode(Render::modeVertexLight);
  181. getConsole().print("Vertexlight mode");
  182. } else if (mode.compare("titlescreen") == 0) {
  183. getRender().setMode(Render::modeLoadScreen);
  184. getConsole().print("Titlescreen mode");
  185. } else {
  186. getConsole().print("Invalid use of mode command (%s)!", mode.c_str());
  187. return -1;
  188. }
  189. } else if (cmd.compare("light") == 0) {
  190. if (args->size() > 0) {
  191. bool b;
  192. if (readBool(args->at(0), &b) < 0) {
  193. getConsole().print("Pass BOOL to light command!");
  194. return -15;
  195. }
  196. if (b)
  197. getRender().setFlags(Render::fGL_Lights);
  198. else
  199. getRender().clearFlags(Render::fGL_Lights);
  200. getConsole().print("GL-Lights are now %s", b ? "on" : "off");
  201. } else {
  202. getConsole().print("Invalid use of light-command!");
  203. return -16;
  204. }
  205. } else if (cmd.compare("fog") == 0) {
  206. if (args->size() > 0) {
  207. bool b;
  208. if (readBool(args->at(0), &b) < 0) {
  209. getConsole().print("Pass BOOL to fog command!");
  210. return -17;
  211. }
  212. if (b)
  213. getRender().setFlags(Render::fFog);
  214. else
  215. getRender().clearFlags(Render::fFog);
  216. getConsole().print("Fog is now %s", b ? "on" : "off");
  217. } else {
  218. getConsole().print("Invalid use of fog-command!");
  219. return -18;
  220. }
  221. } else if (cmd.compare("vmodel") == 0) {
  222. if (args->size() > 0) {
  223. bool b;
  224. if (readBool(args->at(0), &b) < 0) {
  225. getConsole().print("Pass BOOL to vmodel command!");
  226. return -22;
  227. }
  228. if (b)
  229. getRender().setFlags(Render::fViewModel);
  230. else
  231. getRender().clearFlags(Render::fViewModel);
  232. getConsole().print("Viewmodel is now %s", b ? "on" : "off");
  233. } else {
  234. getConsole().print("Invalid use of vmodel-command!");
  235. return -23;
  236. }
  237. } else if (cmd.compare("ralpha") == 0) {
  238. if (args->size() > 0) {
  239. bool b;
  240. if (readBool(args->at(0), &b) < 0) {
  241. getConsole().print("Pass BOOL to ralpha command!");
  242. return -24;
  243. }
  244. if (b)
  245. getRender().setFlags(Render::fRoomAlpha);
  246. else
  247. getRender().clearFlags(Render::fRoomAlpha);
  248. getConsole().print("Room Alpha is now %s", b ? "on" : "off");
  249. } else {
  250. getConsole().print("Invalid use of ralpha-command!");
  251. return -25;
  252. }
  253. } else if (cmd.compare("vis") == 0) {
  254. if (args->size() > 0) {
  255. bool b;
  256. if (readBool(args->at(0), &b) < 0) {
  257. getConsole().print("Pass BOOL to vis command!");
  258. return -28;
  259. }
  260. if (b)
  261. getRender().setFlags(Render::fUsePortals);
  262. else
  263. getRender().clearFlags(Render::fUsePortals);
  264. getConsole().print("Portals are now %s", b ? "on" : "off");
  265. } else {
  266. getConsole().print("Invalid use of vis-command!");
  267. return -29;
  268. }
  269. } else if (cmd.compare("upf") == 0) {
  270. if (args->size() > 0) {
  271. bool b;
  272. if (readBool(args->at(0), &b) < 0) {
  273. getConsole().print("Pass BOOL to upf command!");
  274. return -30;
  275. }
  276. if (b)
  277. getRender().setFlags(Render::fUpdateRoomListPerFrame);
  278. else
  279. getRender().clearFlags(Render::fUpdateRoomListPerFrame);
  280. getConsole().print("URLPF is now %s", b ? "on" : "off");
  281. } else {
  282. getConsole().print("Invalid use of upf-command!");
  283. return -31;
  284. }
  285. } else if (cmd.compare("entmodel") == 0) {
  286. if (args->size() > 0) {
  287. bool b;
  288. if (readBool(args->at(0), &b) < 0) {
  289. getConsole().print("Pass BOOL to entmodel command!");
  290. return -38;
  291. }
  292. if (b)
  293. getRender().setFlags(Render::fEntityModels);
  294. else
  295. getRender().clearFlags(Render::fEntityModels);
  296. getConsole().print("Entmodels are now %s", b ? "on" : "off");
  297. } else {
  298. getConsole().print("Invalid use of entmodel-command!");
  299. return -39;
  300. }
  301. } else if (cmd.compare("oneroom") == 0) {
  302. if (args->size() > 0) {
  303. bool b;
  304. if (readBool(args->at(0), &b) < 0) {
  305. getConsole().print("Pass BOOL to oneroom command!");
  306. return -40;
  307. }
  308. if (b)
  309. getRender().setFlags(Render::fOneRoom);
  310. else
  311. getRender().clearFlags(Render::fOneRoom);
  312. getConsole().print("Rendering one room is now %s", b ? "on" : "off");
  313. } else {
  314. getConsole().print("Invalid use of oneroom-command!");
  315. return -41;
  316. }
  317. } else if (cmd.compare("allrooms") == 0) {
  318. if (args->size() > 0) {
  319. bool b;
  320. if (readBool(args->at(0), &b) < 0) {
  321. getConsole().print("Pass BOOL to allrooms command!");
  322. return -42;
  323. }
  324. if (b)
  325. getRender().setFlags(Render::fAllRooms);
  326. else
  327. getRender().clearFlags(Render::fAllRooms);
  328. getConsole().print("Rendering all rooms is now %s", b ? "on" : "off");
  329. } else {
  330. getConsole().print("Invalid use of allrooms-command!");
  331. return -43;
  332. }
  333. } else if (cmd.compare("ponytail") == 0) {
  334. if (args->size() > 0) {
  335. bool b;
  336. if (readBool(args->at(0), &b) < 0) {
  337. getConsole().print("Pass BOOL to ponytail command!");
  338. return -44;
  339. }
  340. if (b)
  341. getRender().setFlags(Render::fRenderPonytail);
  342. else
  343. getRender().clearFlags(Render::fRenderPonytail);
  344. getConsole().print("Ponytail is now %s", b ? "on" : "off");
  345. } else {
  346. getConsole().print("Invalid use of ponytail-command!");
  347. return -45;
  348. }
  349. } else if (cmd.compare("sshot") == 0) {
  350. if (!mRunning) {
  351. getConsole().print("Use sshot command interactively!");
  352. return -999;
  353. }
  354. char *filename = bufferString("%s/sshots/%s", mBaseDir, VERSION);
  355. bool console = (args->size() > 0) && (strcmp(args->at(0), "console") == 0);
  356. bool menu = (args->size() > 0) && (strcmp(args->at(0), "menu") == 0);
  357. if (!console) {
  358. getConsole().setVisible(false);
  359. if (menu)
  360. getMenu().setVisible(true);
  361. frame();
  362. frame(); // Double buffered
  363. }
  364. getRender().screenShot(filename);
  365. if (!console) {
  366. getConsole().setVisible(true);
  367. if (menu)
  368. getMenu().setVisible(false);
  369. }
  370. getConsole().print("Screenshot stored...");
  371. delete filename;
  372. } else if (cmd.compare("move") == 0) {
  373. if (!mRunning) {
  374. getConsole().print("Use move command interactively!");
  375. return -999;
  376. }
  377. if (args->size() > 0) {
  378. if (getGame().isLoaded()) {
  379. char *move = args->at(0);
  380. if (strcmp(move, "walk") == 0) {
  381. getGame().getLara().setMoveType(Entity::MoveTypeWalk);
  382. getConsole().print("Lara is walking...");
  383. } else if (strcmp(move, "fly") == 0) {
  384. getGame().getLara().setMoveType(Entity::MoveTypeFly);
  385. getConsole().print("Lara is flying...");
  386. } else if (strcmp(move, "noclip") == 0) {
  387. getGame().getLara().setMoveType(Entity::MoveTypeNoClipping);
  388. getConsole().print("Lara is noclipping...");
  389. } else {
  390. getConsole().print("Invalid use of move command (%s)!", move);
  391. return -9;
  392. }
  393. } else {
  394. getConsole().print("Load a level to change the movement type!");
  395. return -10;
  396. }
  397. } else {
  398. getConsole().print("Invalid use of move command!");
  399. return -11;
  400. }
  401. } else if (cmd.compare("sound") == 0) {
  402. if ((!mRunning) || (!getGame().isLoaded())) {
  403. getConsole().print("Use sound command interactively!");
  404. return -999;
  405. }
  406. if (args->size() > 0) {
  407. getSound().play(atoi(args->at(0)));
  408. } else {
  409. getConsole().print("Invalid use of sound command!");
  410. return -12;
  411. }
  412. } else if (cmd.compare("viewmodel") == 0) {
  413. if ((!mRunning) || (!getGame().isLoaded())) {
  414. getConsole().print("Use viewmodel command interactively!");
  415. return -999;
  416. }
  417. unsigned int n = atoi(args->at(0));
  418. if (n < getWorld().sizeSkeletalModel())
  419. getGame().getLara().setSkeletalModel(n);
  420. else {
  421. getConsole().print("Invalid SkeletalModel index!");
  422. return -66;
  423. }
  424. } else if (cmd.compare("pos") == 0) {
  425. if ((!mRunning) || (!getGame().isLoaded())) {
  426. getConsole().print("Use pos command interactively!");
  427. return -21;
  428. }
  429. getGame().getLara().print();
  430. } else if (cmd.compare("pigtail") == 0) {
  431. if ((!mRunning) || (!getGame().isLoaded())) {
  432. getConsole().print("Use pigtail command interactively!");
  433. return -999;
  434. }
  435. if (args->size() > 0) {
  436. bool b;
  437. if (readBool(args->at(0), &b) < 0) {
  438. getConsole().print("Pass BOOL to pigtail command!");
  439. return -46;
  440. }
  441. SkeletalModel &tmp = getGame().getLara().getModel();
  442. tmp.setPigTail(b);
  443. getConsole().print("Pigtail is now %s", b ? "on" : "off");
  444. } else {
  445. getConsole().print("Invalid use of pigtail-command!");
  446. return -47;
  447. }
  448. } else if (cmd.compare("ponypos") == 0) {
  449. if ((!mRunning) || (!getGame().isLoaded())) {
  450. getConsole().print("Use ponypos command interactively!");
  451. return -999;
  452. }
  453. if (args->size() > 3) {
  454. SkeletalModel &tmp = getGame().getLara().getModel();
  455. tmp.setPonyPos((float)atof(args->at(0)), (float)atof(args->at(1)),
  456. (float)atof(args->at(2)), (float)atof(args->at(3)));
  457. } else {
  458. getConsole().print("Invalid use of ponypos-command!");
  459. return -48;
  460. }
  461. */
  462. } else {
  463. getConsole().print("Unknown command: %s", cmd.c_str());
  464. return -50;
  465. }
  466. return 0;
  467. }
  468. int OpenRaider::help(std::string &cmd) {
  469. if (cmd.compare("set") == 0) {
  470. getConsole().print("set-Command Usage:");
  471. getConsole().print(" set VAR VAL");
  472. getConsole().print("Available Variables:");
  473. getConsole().print(" basedir STRING");
  474. getConsole().print(" pakdir STRING");
  475. getConsole().print(" audiodir STRING");
  476. getConsole().print(" datadir STRING");
  477. getConsole().print(" font STRING");
  478. getConsole().print(" gldriver STRING");
  479. getConsole().print(" size INT INT");
  480. getConsole().print(" fullscreen BOOL");
  481. getConsole().print(" audio BOOL");
  482. getConsole().print(" volume BOOL");
  483. getConsole().print(" mouse_x FLOAT");
  484. getConsole().print(" mouse_y FLOAT");
  485. getConsole().print(" fps BOOL");
  486. getConsole().print("Enclose STRINGs with \"\"!");
  487. } else if (cmd.compare("bind") == 0) {
  488. getConsole().print("bind-Command Usage:");
  489. getConsole().print(" bind ACTION KEY");
  490. getConsole().print("Available Actions:");
  491. getConsole().print(" menu");
  492. getConsole().print(" console");
  493. getConsole().print(" forward");
  494. getConsole().print(" backward");
  495. getConsole().print(" left");
  496. getConsole().print(" right");
  497. getConsole().print(" jump");
  498. getConsole().print(" crouch");
  499. getConsole().print(" use");
  500. getConsole().print(" holster");
  501. getConsole().print("Key-Format:");
  502. getConsole().print(" 'a' or '1' for character/number keys");
  503. getConsole().print(" \"leftctrl\" for symbols and special keys");
  504. } else if (cmd.compare("load") == 0) {
  505. getConsole().print("load-Command Usage:");
  506. getConsole().print(" load levelfile.name");
  507. /*
  508. } else if (cmd.compare("sshot") == 0) {
  509. getConsole().print("sshot-Command Usage:");
  510. getConsole().print(" sshot [console|menu]");
  511. getConsole().print("Add console/menu to capture them too");
  512. } else if (cmd.compare("sound") == 0) {
  513. getConsole().print("sound-Command Usage:");
  514. getConsole().print(" sound INT");
  515. getConsole().print("Where INT is a valid sound ID integer");
  516. } else if (cmd.compare("move") == 0) {
  517. getConsole().print("move-Command Usage:");
  518. getConsole().print(" move COMMAND");
  519. getConsole().print("Where COMMAND is one of the following:");
  520. getConsole().print(" walk");
  521. getConsole().print(" fly");
  522. getConsole().print(" noclip");
  523. } else if (cmd.compare("mode") == 0) {
  524. getConsole().print("mode-Command Usage:");
  525. getConsole().print(" mode MODE");
  526. getConsole().print("Where MODE is one of the following:");
  527. getConsole().print(" wireframe");
  528. getConsole().print(" solid");
  529. getConsole().print(" texture");
  530. getConsole().print(" vertexlight");
  531. getConsole().print(" titlescreen");
  532. */
  533. } else if (cmd.compare("animate") == 0) {
  534. getConsole().print("animate-Command Usage:");
  535. getConsole().print(" animate [n|p|BOOL]");
  536. getConsole().print("Where the commands have the following meaning:");
  537. getConsole().print(" BOOL to (de)activate animating all models");
  538. getConsole().print(" n to step all models to their next animation");
  539. getConsole().print(" p to step all models to their previous animation");
  540. } else {
  541. getConsole().print("No help available for %s", cmd.c_str());
  542. return -1;
  543. }
  544. return 0;
  545. }
  546. char *OpenRaider::expandDirectoryNames(const char *s) {
  547. assert(s != NULL);
  548. assert(s[0] != '\0');
  549. char *result = bufferString("%s", s);
  550. if (mPakDir != NULL) {
  551. char *tmp = stringReplace(s, "$(pakdir)", mPakDir);
  552. delete [] result;
  553. result = tmp;
  554. }
  555. if (mAudioDir != NULL) {
  556. char *tmp = stringReplace(s, "$(audiodir)", mAudioDir);
  557. delete [] result;
  558. result = tmp;
  559. }
  560. if (mDataDir != NULL) {
  561. char *tmp = stringReplace(s, "$(datadir)", mDataDir);
  562. delete [] result;
  563. result = tmp;
  564. }
  565. if (mBaseDir != NULL) {
  566. char *tmp = stringReplace(result, "$(basedir)", mBaseDir);
  567. delete [] result;
  568. result = tmp;
  569. }
  570. return result;
  571. }
  572. #define CHANGE_DIR_WITH_EXPANSION(a) do { \
  573. std::string temp; \
  574. command >> temp; \
  575. const char *value = temp.c_str(); \
  576. char *quotes = stringRemoveQuotes(value); \
  577. char *tmp = expandDirectoryNames(quotes); \
  578. a = fullPath(tmp, 0); \
  579. delete [] tmp; \
  580. delete [] quotes; \
  581. } while(false)
  582. int OpenRaider::set(std::istream &command) {
  583. std::string var;
  584. command >> var;
  585. if (var.compare("size") == 0) {
  586. unsigned int w = DEFAULT_WIDTH, h = DEFAULT_HEIGHT;
  587. if (!(command >> w >> h)) {
  588. getConsole().print("set-size-Error: Invalid value(s)");
  589. return -2;
  590. }
  591. getWindow().setSize(w, h);
  592. } else if (var.compare("fullscreen") == 0) {
  593. bool fullscreen = false;
  594. if (!(command >> fullscreen)) {
  595. getConsole().print("set-fullscreen-Error: Invalid value");
  596. return -3;
  597. }
  598. getWindow().setFullscreen(fullscreen);
  599. } else if (var.compare("gldriver") == 0) {
  600. std::string driver;
  601. command >> driver;
  602. getWindow().setDriver(driver.c_str());
  603. } else if (var.compare("audio") == 0) {
  604. bool audio = false;
  605. if (!(command >> audio)) {
  606. getConsole().print("set-audio-Error: Invalid value");
  607. return -4;
  608. }
  609. getSound().setEnabled(audio);
  610. } else if (var.compare("volume") == 0) {
  611. float vol = 1.0f;
  612. if (!(command >> vol)) {
  613. getConsole().print("set-volume-Error: Invalid value");
  614. return -5;
  615. }
  616. getSound().setVolume(vol);
  617. } else if (var.compare("mouse_x") == 0) {
  618. float sense = 1.0f;
  619. if (!(command >> sense)) {
  620. getConsole().print("set-mouse_x-Error: Invalid value");
  621. return -6;
  622. }
  623. getCamera().setSensitivityX(OR_DEG_TO_RAD(sense));
  624. } else if (var.compare("mouse_y") == 0) {
  625. float sense = 1.0f;
  626. if (!(command >> sense)) {
  627. getConsole().print("set-mouse_y-Error: Invalid value");
  628. return -7;
  629. }
  630. getCamera().setSensitivityY(OR_DEG_TO_RAD(sense));
  631. } else if (var.compare("fps") == 0) {
  632. bool fps = false;
  633. if (!(command >> fps)) {
  634. getConsole().print("set-fps-Error: Invalid value");
  635. return -8;
  636. }
  637. mFPS = fps;
  638. } else if (var.compare("basedir") == 0) {
  639. CHANGE_DIR_WITH_EXPANSION(mBaseDir);
  640. } else if (var.compare("pakdir") == 0) {
  641. CHANGE_DIR_WITH_EXPANSION(mPakDir);
  642. } else if (var.compare("audiodir") == 0) {
  643. CHANGE_DIR_WITH_EXPANSION(mAudioDir);
  644. } else if (var.compare("datadir") == 0) {
  645. CHANGE_DIR_WITH_EXPANSION(mDataDir);
  646. } else if (var.compare("font") == 0) {
  647. std::string temp;
  648. command >> temp;
  649. const char *value = temp.c_str();
  650. char *quotes = stringReplace(value, "\"", "");
  651. char *tmp = expandDirectoryNames(quotes);
  652. getFont().setFont(tmp);
  653. delete [] tmp;
  654. delete [] quotes;
  655. } else {
  656. getConsole().print("set-Error: Unknown variable (%s)", var.c_str());
  657. return -1;
  658. }
  659. return 0;
  660. }
  661. int OpenRaider::bind(const char *action, const char *key) {
  662. assert(action != NULL);
  663. assert(action[0] != '\0');
  664. assert(key != NULL);
  665. assert(key[0] != '\0');
  666. ActionEvents e = stringToActionEvent(action);
  667. if (e == ActionEventCount) {
  668. getConsole().print("bind-Error: Unknown action (%s --> %s)", key, action);
  669. return -1;
  670. }
  671. KeyboardButton c = stringToKeyboardButton(key);
  672. if (c == unknownKey) {
  673. getConsole().print("bind-Error: Unknown key (%s)", key);
  674. return -2;
  675. }
  676. keyBindings[e] = c;
  677. return 0;
  678. }