Open Source Tomb Raider Engine
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. /*!
  2. * \file src/Command.cpp
  3. * \brief OpenRaider command implementation
  4. *
  5. * \author xythobuz
  6. */
  7. #include <cstdio>
  8. #include <cstring>
  9. #include <assert.h>
  10. #include "WindowSDL.h"
  11. #include "config.h"
  12. #include "Console.h"
  13. #include "Game.h"
  14. #include "main.h"
  15. #include "math/math.h"
  16. #include "Menu.h"
  17. #include "Sound.h"
  18. #include "TombRaider.h"
  19. #include "utils/strings.h"
  20. #include "utils/time.h"
  21. #include "OpenRaider.h"
  22. int OpenRaider::command(const char *command, std::vector<char *> *args) {
  23. assert(command != NULL);
  24. assert(command[0] != '\0');
  25. assert(args != NULL);
  26. if (strcmp(command, "set") == 0) {
  27. if (args->size() != 2) {
  28. getConsole().print("Invalid use of set-command");
  29. return -1;
  30. } else {
  31. return set(args->at(0), args->at(1));
  32. }
  33. } else if (strcmp(command, "bind") == 0) {
  34. if (args->size() != 2) {
  35. getConsole().print("Invalid use of bind-command");
  36. return -2;
  37. } else {
  38. return bind(args->at(0), args->at(1));
  39. }
  40. } else if (strcmp(command, "quit") == 0) {
  41. exit(0);
  42. } else if (strcmp(command, "load") == 0) {
  43. if (!mRunning) {
  44. getConsole().print("Use load command interactively!");
  45. return -999;
  46. }
  47. char *tmp = bufferString("%s/%s", mPakDir, args->at(0));
  48. int error = getGame().loadLevel(tmp);
  49. delete [] tmp;
  50. return error;
  51. } else if (strcmp(command, "sshot") == 0) {
  52. if (!mRunning) {
  53. getConsole().print("Use sshot command interactively!");
  54. return -999;
  55. }
  56. char *filename = bufferString("%s/sshots/%s", mBaseDir, VERSION);
  57. bool console = (args->size() > 0) && (strcmp(args->at(0), "console") == 0);
  58. bool menu = (args->size() > 0) && (strcmp(args->at(0), "menu") == 0);
  59. if (!console) {
  60. getConsole().setVisible(false);
  61. if (menu)
  62. getMenu().setVisible(true);
  63. frame();
  64. frame(); // Double buffered
  65. }
  66. getRender().screenShot(filename);
  67. if (!console) {
  68. getConsole().setVisible(true);
  69. if (menu)
  70. getMenu().setVisible(false);
  71. }
  72. getConsole().print("Screenshot stored...");
  73. delete filename;
  74. } else if (strcmp(command, "mode") == 0) {
  75. if (args->size() > 0) {
  76. char *mode = args->at(0);
  77. if (strcmp(mode, "wireframe") == 0) {
  78. if (getGame().isLoaded()) {
  79. getRender().setMode(Render::modeWireframe);
  80. getConsole().print("Wireframe mode");
  81. } else {
  82. getConsole().print("Load a level to set this mode!");
  83. return -3;
  84. }
  85. } else if (strcmp(mode, "solid") == 0) {
  86. if (getGame().isLoaded()) {
  87. getRender().setMode(Render::modeSolid);
  88. getConsole().print("Solid mode");
  89. } else {
  90. getConsole().print("Load a level to set this mode!");
  91. return -4;
  92. }
  93. } else if (strcmp(mode, "texture") == 0) {
  94. if (getGame().isLoaded()) {
  95. getRender().setMode(Render::modeTexture);
  96. getConsole().print("Texture mode");
  97. } else {
  98. getConsole().print("Load a level to set this mode!");
  99. return -5;
  100. }
  101. } else if (strcmp(mode, "vertexlight") == 0) {
  102. if (getGame().isLoaded()) {
  103. getRender().setMode(Render::modeVertexLight);
  104. getConsole().print("Vertexlight mode");
  105. } else {
  106. getConsole().print("Load a level to set this mode!");
  107. return -6;
  108. }
  109. } else if (strcmp(mode, "titlescreen") == 0) {
  110. getRender().setMode(Render::modeLoadScreen);
  111. getConsole().print("Titlescreen mode");
  112. } else {
  113. getConsole().print("Invalid use of mode command (%s)!", mode);
  114. return -7;
  115. }
  116. } else {
  117. getConsole().print("Invalid use of mode command!");
  118. return -8;
  119. }
  120. } else if (strcmp(command, "move") == 0) {
  121. if (!mRunning) {
  122. getConsole().print("Use move command interactively!");
  123. return -999;
  124. }
  125. if (args->size() > 0) {
  126. if (getGame().isLoaded()) {
  127. char *move = args->at(0);
  128. if (strcmp(move, "walk") == 0) {
  129. getGame().mLara->moveType = worldMoveType_walk;
  130. getConsole().print("Lara is walking...");
  131. } else if (strcmp(move, "fly") == 0) {
  132. getGame().mLara->moveType = worldMoveType_fly;
  133. getConsole().print("Lara is flying...");
  134. } else if (strcmp(move, "noclip") == 0) {
  135. getGame().mLara->moveType = worldMoveType_noClipping;
  136. getConsole().print("Lara is noclipping...");
  137. } else {
  138. getConsole().print("Invalid use of move command (%s)!", move);
  139. return -9;
  140. }
  141. } else {
  142. getConsole().print("Load a level to change the movement type!");
  143. return -10;
  144. }
  145. } else {
  146. getConsole().print("Invalid use of move command!");
  147. return -11;
  148. }
  149. } else if (strcmp(command, "sound") == 0) {
  150. if ((!mRunning) || (!getGame().isLoaded())) {
  151. getConsole().print("Use sound command interactively!");
  152. return -999;
  153. }
  154. if (args->size() > 0) {
  155. getSound().play(atoi(args->at(0)));
  156. } else {
  157. getConsole().print("Invalid use of sound command!");
  158. return -12;
  159. }
  160. } else if (strcmp(command, "animate") == 0) {
  161. if ((!mRunning) || (!getGame().isLoaded())) {
  162. getConsole().print("Use animate command interactively!");
  163. return -999;
  164. }
  165. if (args->size() > 0) {
  166. char c = args->at(0)[0];
  167. if (c == 'n') {
  168. // Step all skeletal models to their next animation
  169. if (getRender().getFlags() & Render::fAnimateAllModels) {
  170. for (unsigned int i = 0; i < getRender().mModels.size(); i++) {
  171. SkeletalModel *m = getRender().mModels[i];
  172. if (m->getAnimation() < ((int)m->model->animation.size() - 1))
  173. m->setAnimation(m->getAnimation() + 1);
  174. else
  175. if (m->getAnimation() != 0)
  176. m->setAnimation(0);
  177. }
  178. } else {
  179. getConsole().print("Animations need to be enabled!");
  180. }
  181. } else if (c == 'p') {
  182. // Step all skeletal models to their previous animation
  183. if (getRender().getFlags() & Render::fAnimateAllModels) {
  184. for (unsigned int i = 0; i < getRender().mModels.size(); i++) {
  185. SkeletalModel *m = getRender().mModels[i];
  186. if (m->getAnimation() > 0)
  187. m->setAnimation(m->getAnimation() - 1);
  188. else
  189. if (m->model->animation.size() > 0)
  190. m->setAnimation(m->model->animation.size() - 1);
  191. }
  192. } else {
  193. getConsole().print("Animations need to be enabled!");
  194. }
  195. } else {
  196. // Enable or disable animating all skeletal models
  197. bool b;
  198. if (readBool(args->at(0), &b) < 0) {
  199. getConsole().print("Pass BOOL to animate command!");
  200. return -13;
  201. }
  202. if (b)
  203. getRender().setFlags(Render::fAnimateAllModels);
  204. else
  205. getRender().clearFlags(Render::fAnimateAllModels);
  206. getConsole().print(b ? "Animating all models" : "No longer animating all models");
  207. }
  208. } else {
  209. getConsole().print("Invalid use of animate command!");
  210. return -14;
  211. }
  212. } else if (strcmp(command, "light") == 0) {
  213. if (args->size() > 0) {
  214. bool b;
  215. if (readBool(args->at(0), &b) < 0) {
  216. getConsole().print("Pass BOOL to light command!");
  217. return -15;
  218. }
  219. if (b)
  220. getRender().setFlags(Render::fGL_Lights);
  221. else
  222. getRender().clearFlags(Render::fGL_Lights);
  223. getConsole().print("GL-Lights are now %s", b ? "on" : "off");
  224. } else {
  225. getConsole().print("Invalid use of light-command!");
  226. return -16;
  227. }
  228. } else if (strcmp(command, "fog") == 0) {
  229. if (args->size() > 0) {
  230. bool b;
  231. if (readBool(args->at(0), &b) < 0) {
  232. getConsole().print("Pass BOOL to fog command!");
  233. return -17;
  234. }
  235. if (b)
  236. getRender().setFlags(Render::fFog);
  237. else
  238. getRender().clearFlags(Render::fFog);
  239. getConsole().print("Fog is now %s", b ? "on" : "off");
  240. } else {
  241. getConsole().print("Invalid use of fog-command!");
  242. return -18;
  243. }
  244. } else if (strcmp(command, "viewmodel") == 0) {
  245. if ((!mRunning) || (!getGame().isLoaded())) {
  246. getConsole().print("Use viewmodel command interactively!");
  247. return -999;
  248. }
  249. if (getGame().mLara) {
  250. skeletal_model_t *mdl = getWorld().getModel(atoi(args->at(0)));
  251. if (getGame().mLara->tmpHook)
  252. getGame().mLara->tmpHook->setModel(mdl);
  253. }
  254. //m_render.ViewModel(LARA, atoi(cmd));
  255. } else if (strcmp(command, "pos") == 0) {
  256. if (getGame().mLara) {
  257. getConsole().print("Position:");
  258. getConsole().print(" Room %i (0x%X)", getGame().mLara->room, getWorld().getRoomInfo(getGame().mLara->room));
  259. getConsole().print(" %.1fx %.1fy %.1fz", getGame().mLara->pos[0], getGame().mLara->pos[1], getGame().mLara->pos[2]);
  260. getConsole().print(" %.1f Yaw %.1f Pitch", OR_RAD_TO_DEG(getGame().mLara->angles[1]), OR_RAD_TO_DEG(getGame().mLara->angles[2]));
  261. } else {
  262. getConsole().print("Load a level to get Laras position!");
  263. return -21;
  264. }
  265. } else if (strcmp(command, "vmodel") == 0) {
  266. if (args->size() > 0) {
  267. bool b;
  268. if (readBool(args->at(0), &b) < 0) {
  269. getConsole().print("Pass BOOL to vmodel command!");
  270. return -22;
  271. }
  272. if (b)
  273. getRender().setFlags(Render::fViewModel);
  274. else
  275. getRender().clearFlags(Render::fViewModel);
  276. getConsole().print("Viewmodel is now %s", b ? "on" : "off");
  277. } else {
  278. getConsole().print("Invalid use of vmodel-command!");
  279. return -23;
  280. }
  281. } else if (strcmp(command, "ralpha") == 0) {
  282. if (args->size() > 0) {
  283. bool b;
  284. if (readBool(args->at(0), &b) < 0) {
  285. getConsole().print("Pass BOOL to ralpha command!");
  286. return -24;
  287. }
  288. if (b)
  289. getRender().setFlags(Render::fRoomAlpha);
  290. else
  291. getRender().clearFlags(Render::fRoomAlpha);
  292. getConsole().print("Room Alpha is now %s", b ? "on" : "off");
  293. } else {
  294. getConsole().print("Invalid use of ralpha-command!");
  295. return -25;
  296. }
  297. } else if (strcmp(command, "vis") == 0) {
  298. if (args->size() > 0) {
  299. bool b;
  300. if (readBool(args->at(0), &b) < 0) {
  301. getConsole().print("Pass BOOL to vis command!");
  302. return -28;
  303. }
  304. if (b)
  305. getRender().setFlags(Render::fUsePortals);
  306. else
  307. getRender().clearFlags(Render::fUsePortals);
  308. getConsole().print("Portals are now %s", b ? "on" : "off");
  309. } else {
  310. getConsole().print("Invalid use of vis-command!");
  311. return -29;
  312. }
  313. } else if (strcmp(command, "upf") == 0) {
  314. if (args->size() > 0) {
  315. bool b;
  316. if (readBool(args->at(0), &b) < 0) {
  317. getConsole().print("Pass BOOL to upf command!");
  318. return -30;
  319. }
  320. if (b)
  321. getRender().setFlags(Render::fUpdateRoomListPerFrame);
  322. else
  323. getRender().clearFlags(Render::fUpdateRoomListPerFrame);
  324. getConsole().print("URLPF is now %s", b ? "on" : "off");
  325. } else {
  326. getConsole().print("Invalid use of upf-command!");
  327. return -31;
  328. }
  329. } else if (strcmp(command, "entmodel") == 0) {
  330. if (args->size() > 0) {
  331. bool b;
  332. if (readBool(args->at(0), &b) < 0) {
  333. getConsole().print("Pass BOOL to entmodel command!");
  334. return -38;
  335. }
  336. if (b)
  337. getRender().setFlags(Render::fEntityModels);
  338. else
  339. getRender().clearFlags(Render::fEntityModels);
  340. getConsole().print("Entmodels are now %s", b ? "on" : "off");
  341. } else {
  342. getConsole().print("Invalid use of entmodel-command!");
  343. return -39;
  344. }
  345. } else if (strcmp(command, "oneroom") == 0) {
  346. if (args->size() > 0) {
  347. bool b;
  348. if (readBool(args->at(0), &b) < 0) {
  349. getConsole().print("Pass BOOL to oneroom command!");
  350. return -40;
  351. }
  352. if (b)
  353. getRender().setFlags(Render::fOneRoom);
  354. else
  355. getRender().clearFlags(Render::fOneRoom);
  356. getConsole().print("Rendering one room is now %s", b ? "on" : "off");
  357. } else {
  358. getConsole().print("Invalid use of oneroom-command!");
  359. return -41;
  360. }
  361. } else if (strcmp(command, "allrooms") == 0) {
  362. if (args->size() > 0) {
  363. bool b;
  364. if (readBool(args->at(0), &b) < 0) {
  365. getConsole().print("Pass BOOL to allrooms command!");
  366. return -42;
  367. }
  368. if (b)
  369. getRender().setFlags(Render::fAllRooms);
  370. else
  371. getRender().clearFlags(Render::fAllRooms);
  372. getConsole().print("Rendering all rooms is now %s", b ? "on" : "off");
  373. } else {
  374. getConsole().print("Invalid use of allrooms-command!");
  375. return -43;
  376. }
  377. } else if (strcmp(command, "ponytail") == 0) {
  378. if (args->size() > 0) {
  379. bool b;
  380. if (readBool(args->at(0), &b) < 0) {
  381. getConsole().print("Pass BOOL to ponytail command!");
  382. return -44;
  383. }
  384. if (b)
  385. getRender().setFlags(Render::fRenderPonytail);
  386. else
  387. getRender().clearFlags(Render::fRenderPonytail);
  388. getConsole().print("Ponytail is now %s", b ? "on" : "off");
  389. } else {
  390. getConsole().print("Invalid use of ponytail-command!");
  391. return -45;
  392. }
  393. } else if (strcmp(command, "pigtail") == 0) {
  394. if ((!mRunning) || (!getGame().isLoaded())) {
  395. getConsole().print("Use pigtail command interactively!");
  396. return -999;
  397. }
  398. if (args->size() > 0) {
  399. bool b;
  400. if (readBool(args->at(0), &b) < 0) {
  401. getConsole().print("Pass BOOL to pigtail command!");
  402. return -46;
  403. }
  404. SkeletalModel *tmp = getGame().mLara->tmpHook;
  405. tmp->model->pigtails = b;
  406. if (b) {
  407. tmp->model->ponyOff -= 20;
  408. tmp->model->ponytail[1] -= 32;
  409. } else {
  410. tmp->model->ponyOff += 20;
  411. tmp->model->ponytail[1] += 32;
  412. }
  413. getConsole().print("Pigtail is now %s", b ? "on" : "off");
  414. } else {
  415. getConsole().print("Invalid use of pigtail-command!");
  416. return -47;
  417. }
  418. } else if (strcmp(command, "ponypos") == 0) {
  419. if ((!mRunning) || (!getGame().isLoaded())) {
  420. getConsole().print("Use ponypos command interactively!");
  421. return -999;
  422. }
  423. if (args->size() > 3) {
  424. SkeletalModel *tmp = getGame().mLara->tmpHook;
  425. tmp->model->ponytail[0] = (float)atof(args->at(0));
  426. tmp->model->ponytail[1] = (float)atof(args->at(1));
  427. tmp->model->ponytail[2] = (float)atof(args->at(2));
  428. tmp->model->ponytailAngle = (float)atof(args->at(3));
  429. } else {
  430. getConsole().print("Invalid use of ponypos-command!");
  431. return -48;
  432. }
  433. } else if (strcmp(command, "help") == 0) {
  434. if (args->size() == 0) {
  435. getConsole().print("Available commands:");
  436. getConsole().print(" load - load a level");
  437. getConsole().print(" set - set a parameter");
  438. getConsole().print(" bind - bind a keyboard/mouse action");
  439. getConsole().print(" sshot - make a screenshot");
  440. getConsole().print(" move - [walk|fly|noclip]");
  441. getConsole().print(" sound - INT - Test play sound");
  442. getConsole().print(" mode - MODE - Render mode");
  443. getConsole().print(" animate - [BOOL|n|p] - Animate models");
  444. getConsole().print(" light - BOOL - GL Lights");
  445. getConsole().print(" fog - BOOL - GL Fog");
  446. getConsole().print(" viewmodel - INT - Change Laras model");
  447. getConsole().print(" pos - Print position info");
  448. getConsole().print(" vmodel - BOOL - View Model");
  449. getConsole().print(" ralpha - BOOL - Room Alpha");
  450. getConsole().print(" vis - BOOL - Use Portals");
  451. getConsole().print(" upf - BOOL - Update Room List Per Frame");
  452. getConsole().print(" entmodel - BOOL");
  453. getConsole().print(" oneroom - BOOL");
  454. getConsole().print(" allrooms - BOOL");
  455. getConsole().print(" ponytail - BOOL");
  456. getConsole().print(" pigtail - BOOL");
  457. getConsole().print(" ponypos - FLOAT FLOAT FLOAT FLOAT - x y z angle");
  458. getConsole().print(" help - print command help");
  459. getConsole().print(" quit - exit OpenRaider");
  460. getConsole().print("Use help COMMAND to get additional info");
  461. } else if (args->size() == 1) {
  462. return help(args->at(0));
  463. } else {
  464. getConsole().print("Invalid use of help-command");
  465. return -49;
  466. }
  467. } else {
  468. getConsole().print("Unknown command: %s ", command);
  469. return -50;
  470. }
  471. return 0;
  472. }
  473. int OpenRaider::help(const char *cmd) {
  474. assert(cmd != NULL);
  475. assert(cmd[0] != '\0');
  476. if (strcmp(cmd, "set") == 0) {
  477. getConsole().print("set-Command Usage:");
  478. getConsole().print(" set VAR VAL");
  479. getConsole().print("Available Variables:");
  480. getConsole().print(" basedir STRING");
  481. getConsole().print(" pakdir STRING");
  482. getConsole().print(" audiodir STRING");
  483. getConsole().print(" datadir STRING");
  484. getConsole().print(" font STRING");
  485. getConsole().print(" gldriver STRING");
  486. getConsole().print(" size \"INTxINT\"");
  487. getConsole().print(" fullscreen BOOL");
  488. getConsole().print(" audio BOOL");
  489. getConsole().print(" volume BOOL");
  490. getConsole().print(" mouse_x FLOAT");
  491. getConsole().print(" mouse_y FLOAT");
  492. getConsole().print(" fps BOOL");
  493. getConsole().print("Enclose STRINGs with \"\"!");
  494. getConsole().print("size expects a STRING in the specified format");
  495. } else if (strcmp(cmd, "bind") == 0) {
  496. getConsole().print("bind-Command Usage:");
  497. getConsole().print(" bind ACTION KEY");
  498. getConsole().print("Available Actions:");
  499. getConsole().print(" menu");
  500. getConsole().print(" console");
  501. getConsole().print(" forward");
  502. getConsole().print(" backward");
  503. getConsole().print(" left");
  504. getConsole().print(" right");
  505. getConsole().print(" jump");
  506. getConsole().print(" crouch");
  507. getConsole().print(" use");
  508. getConsole().print(" holster");
  509. getConsole().print("Key-Format:");
  510. getConsole().print(" 'a' or '1' for character/number keys");
  511. getConsole().print(" \"leftctrl\" for symbols and special keys");
  512. } else if (strcmp(cmd, "load") == 0) {
  513. getConsole().print("load-Command Usage:");
  514. getConsole().print(" load levelfile.name");
  515. } else if (strcmp(cmd, "game") == 0) {
  516. getConsole().print("Use \"game help\" for more info");
  517. } else if (strcmp(cmd, "sshot") == 0) {
  518. getConsole().print("sshot-Command Usage:");
  519. getConsole().print(" sshot [console|menu]");
  520. getConsole().print("Add console/menu to capture them too");
  521. } else if (strcmp(cmd, "sound") == 0) {
  522. getConsole().print("sound-Command Usage:");
  523. getConsole().print(" sound INT");
  524. getConsole().print("Where INT is a valid sound ID integer");
  525. } else if (strcmp(cmd, "move") == 0) {
  526. getConsole().print("move-Command Usage:");
  527. getConsole().print(" move COMMAND");
  528. getConsole().print("Where COMMAND is one of the following:");
  529. getConsole().print(" walk");
  530. getConsole().print(" fly");
  531. getConsole().print(" noclip");
  532. } else if (strcmp(cmd, "mode") == 0) {
  533. getConsole().print("mode-Command Usage:");
  534. getConsole().print(" mode MODE");
  535. getConsole().print("Where MODE is one of the following:");
  536. getConsole().print(" wireframe");
  537. getConsole().print(" solid");
  538. getConsole().print(" texture");
  539. getConsole().print(" vertexlight");
  540. getConsole().print(" titlescreen");
  541. } else if (strcmp(cmd, "animate") == 0) {
  542. getConsole().print("animate-Command Usage:");
  543. getConsole().print(" animate [n|p|BOOL]");
  544. getConsole().print("Where the commands have the following meaning:");
  545. getConsole().print(" BOOL to (de)activate animating all models");
  546. getConsole().print(" n to step all models to their next animation");
  547. getConsole().print(" p to step all models to their previous animation");
  548. } else {
  549. getConsole().print("No help available for %s", cmd);
  550. return -1;
  551. }
  552. return 0;
  553. }