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.

OpenRaider.cpp 97KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594
  1. /*!
  2. * \file include/OpenRaider.h
  3. * \brief Main Game Singleton
  4. *
  5. * \author Mongoose
  6. */
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <stdio.h>
  10. #include <stdarg.h>
  11. #include <unistd.h>
  12. #include <math.h>
  13. #include <sys/types.h>
  14. #ifdef DEBUG_MEMORY
  15. #include <memory_test.h>
  16. #endif
  17. #include <World.h>
  18. #include <SkeletalModel.h>
  19. #include <TombRaider1.h> // tmp stop-gap
  20. #include <OpenRaider.h>
  21. enum OpenRaiderText { textConsole = 2, textMenu = 3, textOutput = 4 };
  22. const unsigned int TextureLimit = 24;
  23. const float TexelScale = 256.0f;
  24. World gWorld;
  25. Map<int, int> gMapTex2Bump;
  26. Vector <unsigned int> gColorTextureHACK;
  27. int gTextureOffset;
  28. entity_t *LARA = 0x0;
  29. entity_t *gClientPlayer[32];
  30. unsigned int gNetTicks = 0;
  31. unsigned int gNetLastTicks = 0;
  32. bool gStartServer = false;
  33. skeletal_model_t *gLaraModel = 0x0;
  34. char *gFontFilename = 0x0;
  35. unsigned int getTicks()
  36. {
  37. OpenRaider *game = OpenRaider::Instance();
  38. return game->getTicks();
  39. }
  40. ////////////////////////////////////////////////////////////
  41. // Constructors
  42. ////////////////////////////////////////////////////////////
  43. OpenRaider *OpenRaider::mInstance = 0x0;
  44. OpenRaider *OpenRaider::Instance()
  45. {
  46. if (mInstance == 0x0)
  47. {
  48. mInstance = new OpenRaider();
  49. }
  50. return mInstance;
  51. }
  52. void killOpenRaiderSingleton()
  53. {
  54. killNetworkSingleton();
  55. printf("Shutting down Game...\n");
  56. // Requires public deconstructor
  57. // Causes pointer-being-freed-not-allocated error!
  58. //delete OpenRaider::Instance();
  59. #warning "Can't delete OpenRaider::Instance() without a not-allocated-free error. Something is fishy here..."
  60. #ifdef DEBUG_MEMORY
  61. printf("\n[Mongoose MEMORY_DEBUG]\nMemory leak table:\n");
  62. dump_memory_report();
  63. #endif
  64. printf("\nThanks for testing %s\n", VERSION);
  65. printf("Please file bug reports\n\n");
  66. printf("Build date: %s @ %s\n", __DATE__, __TIME__);
  67. printf("Build host: %s\n", BUILD_HOST);
  68. printf("Web site : http://github.com/xythobuz/OpenRaider\n");
  69. }
  70. OpenRaider::OpenRaider() : SDLSystem()
  71. {
  72. Network::Instance();
  73. for (unsigned int i = 0; i < 32; ++i)
  74. {
  75. gClientPlayer[i] = 0x0;
  76. }
  77. m_pakDir = 0x0;
  78. m_audioDir = 0x0;
  79. m_homeDir = 0x0;
  80. m_mouseX = 2.5;
  81. m_mouseY = 0.5;
  82. m_texOffset = 0;
  83. mLevelTextureOffset = 0;
  84. m_testSFX = -1;
  85. mText = NULL;
  86. m_flags = 0;
  87. m_mapName[0] = '\0';
  88. /*! \todo Replace numbers with enum modes.
  89. * Only do this when you know the amount of commands + 1 (0 reserved)
  90. */
  91. mMode[addCommandMode("[OpenRaider.Engine]")] = 2;
  92. mMode[addCommandMode("[Network.Server]")] = 5;
  93. mMode[addCommandMode("[Video.OpenGL]")] = 0;
  94. mMode[addCommandMode("[Audio.OpenAL]")] = 1;
  95. mMode[addCommandMode("[OpenRaider.Console]")] = 4;
  96. mMode[addCommandMode("[Input.Mouse]")] = 3;
  97. }
  98. OpenRaider::~OpenRaider()
  99. {
  100. /*! \fixme GL call to critical section,
  101. * needs mutex really -- Mongoose 2002.01.02
  102. */
  103. m_render.setMode(Render::modeDisabled);
  104. sleep(1);
  105. printf("Removing World...\n");
  106. gWorld.destroy();
  107. printf("Cleaning up...\n");
  108. for (mMapList.start(); mMapList.forward(); mMapList.next())
  109. {
  110. if (mMapList.current())
  111. {
  112. delete [] mMapList.current();
  113. }
  114. }
  115. for (mMusicList.start(); mMusicList.forward(); mMusicList.next())
  116. {
  117. if (mMusicList.current())
  118. {
  119. delete [] mMusicList.current();
  120. }
  121. }
  122. if (m_pakDir)
  123. delete [] m_pakDir;
  124. if (m_audioDir)
  125. delete [] m_audioDir;
  126. if (m_homeDir)
  127. delete [] m_homeDir;
  128. if (gFontFilename)
  129. delete [] gFontFilename;
  130. }
  131. ////////////////////////////////////////////////////////////
  132. // Public Accessors
  133. ////////////////////////////////////////////////////////////
  134. ////////////////////////////////////////////////////////////
  135. // Public Mutators
  136. ////////////////////////////////////////////////////////////
  137. void eventAnimTest(int anim)
  138. {
  139. if (LARA)
  140. {
  141. SkeletalModel *mdl = static_cast<SkeletalModel *>(LARA->tmpHook);
  142. if (mdl)
  143. {
  144. mdl->setAnimation(anim);
  145. }
  146. }
  147. }
  148. void percent_callback(int p)
  149. {
  150. OpenRaider &game = *OpenRaider::Instance();
  151. switch (p)
  152. {
  153. case 10:
  154. game.print(true, "Level textures loaded");
  155. break;
  156. default:
  157. // %% has problems with OpenRaider::print it seems
  158. game.print(false, "Level pak %i/100 loaded", p);
  159. }
  160. }
  161. void openraider_warning(const char *func_name, const char *warning,
  162. const char *filename, int line)
  163. {
  164. printf("%s> WARNING %s %s:%i\n", func_name, warning, filename, line);
  165. }
  166. void openraider_error(const char *func_name, const char *error,
  167. const char *filename, int line)
  168. {
  169. printf("%s> ERROR %s %s:%i\n", func_name, error, filename, line);
  170. }
  171. void from_network_layer(network_packet_t *p, unsigned int *last_id)
  172. {
  173. static unsigned int i = 0;
  174. *last_id = i++;
  175. if (gNetTicks > gNetLastTicks + 200) // 200ms
  176. {
  177. gNetLastTicks = gNetTicks;
  178. p->send = 1;
  179. }
  180. if (LARA)
  181. {
  182. p->pos[0] = LARA->pos[0];
  183. p->pos[1] = LARA->pos[1];
  184. p->pos[2] = LARA->pos[2];
  185. p->yaw = LARA->angles[1];
  186. p->pitch = LARA->angles[1];
  187. p->view_model = LARA->modelId;
  188. SkeletalModel *mdl = static_cast<SkeletalModel *>(LARA->tmpHook);
  189. if (mdl)
  190. {
  191. p->aframe = mdl->getAnimation();
  192. p->bframe = mdl->getFrame();
  193. }
  194. }
  195. #ifdef DEBUG_NETWORK
  196. printf("<> Sending data\n");
  197. #endif
  198. }
  199. void to_network_layer(network_packet_t p)
  200. {
  201. OpenRaider &game = *OpenRaider::Instance();
  202. #ifdef DEBUG_NETWORK
  203. printf("<> Recieved data\n");
  204. #endif
  205. if (gClientPlayer[p.cid])
  206. {
  207. gClientPlayer[p.cid]->pos[0] = p.pos[0];
  208. gClientPlayer[p.cid]->pos[1] = p.pos[1];
  209. gClientPlayer[p.cid]->pos[2] = p.pos[2];
  210. gClientPlayer[p.cid]->angles[1] = p.yaw;
  211. gClientPlayer[p.cid]->angles[2] = p.pitch;
  212. }
  213. else if (LARA)
  214. {
  215. gClientPlayer[p.cid] = new entity_t;
  216. gClientPlayer[p.cid]->pos[0] = p.pos[0];
  217. gClientPlayer[p.cid]->pos[1] = p.pos[1];
  218. gClientPlayer[p.cid]->pos[2] = p.pos[2];
  219. gClientPlayer[p.cid]->angles[1] = p.yaw;
  220. gClientPlayer[p.cid]->angles[2] = p.pitch;
  221. gClientPlayer[p.cid]->moving = true;
  222. gClientPlayer[p.cid]->objectId = 0;
  223. gClientPlayer[p.cid]->id = LARA->id+1000+p.cid;
  224. gClientPlayer[p.cid]->type = 0x02;
  225. SkeletalModel *mdl = new SkeletalModel();
  226. gClientPlayer[p.cid]->tmpHook = mdl;
  227. mdl->setAnimation(p.aframe);
  228. mdl->setFrame(p.bframe);
  229. mdl->setIdleAnimation(TR_ANIAMTION_STAND);
  230. gClientPlayer[p.cid]->modelId = LARA->modelId; //p.view_model;
  231. gClientPlayer[p.cid]->animate = true;
  232. gWorld.addEntity(gClientPlayer[p.cid]);
  233. game.print(true, "A new player (%u) connected", p.cid);
  234. }
  235. if (LARA)
  236. {
  237. Network &network = *Network::Instance();
  238. network_frame_t &gPiggyBack = network.getPiggyBack();
  239. if (gNetTicks > gNetLastTicks + 200)
  240. {
  241. gNetLastTicks = gNetTicks;
  242. gPiggyBack.data.send = 1;
  243. }
  244. gPiggyBack.data.pos[0] = LARA->pos[0];
  245. gPiggyBack.data.pos[1] = LARA->pos[1];
  246. gPiggyBack.data.pos[2] = LARA->pos[2];
  247. gPiggyBack.data.yaw = LARA->angles[1];
  248. gPiggyBack.data.pitch = LARA->angles[2];
  249. gPiggyBack.data.view_model = LARA->modelId;
  250. SkeletalModel *mdl = static_cast<SkeletalModel *>(LARA->tmpHook);
  251. if (mdl)
  252. {
  253. gPiggyBack.data.aframe = mdl->getAnimation();
  254. gPiggyBack.data.bframe = mdl->getFrame();
  255. }
  256. }
  257. }
  258. void OpenRaider::handleMouseMotionEvent(float x, float y)
  259. {
  260. if (x > 0)
  261. {
  262. while (x-- > 0) // Tried to fix very slow camera movements
  263. m_camera.command(CAMERA_ROTATE_RIGHT);
  264. if (LARA)
  265. {
  266. switch (LARA->moveType)
  267. {
  268. case worldMoveType_swim:
  269. case worldMoveType_fly:
  270. case worldMoveType_noClipping:
  271. eventAnimTest(TR_ANIAMTION_SWIM_R);
  272. break;
  273. default:
  274. case worldMoveType_walk:
  275. case worldMoveType_walkNoSwim:
  276. eventAnimTest(TR_ANIAMTION_TURN_L); // TURN left always for now
  277. }
  278. LARA->moving = true;
  279. }
  280. }
  281. else if (x < 0)
  282. {
  283. while (x++ < 0) // Tried to fix very slow camera movements
  284. m_camera.command(CAMERA_ROTATE_LEFT);
  285. if (LARA)
  286. {
  287. switch (LARA->moveType)
  288. {
  289. case worldMoveType_swim:
  290. case worldMoveType_fly:
  291. case worldMoveType_noClipping:
  292. eventAnimTest(TR_ANIAMTION_SWIM_L);
  293. break;
  294. default:
  295. case worldMoveType_walk:
  296. case worldMoveType_walkNoSwim:
  297. eventAnimTest(TR_ANIAMTION_TURN_L);
  298. }
  299. LARA->moving = true;
  300. }
  301. }
  302. if (y > 0)
  303. while (y-- > 0) // Tried to fix very slow camera movements
  304. m_camera.command(CAMERA_ROTATE_UP);
  305. else if (y < 0)
  306. while (y++ < 0) // Tried to fix very slow camera movements
  307. m_camera.command(CAMERA_ROTATE_DOWN);
  308. if (LARA)
  309. {
  310. LARA->angles[1] = m_camera.getRadianYaw();
  311. LARA->angles[2] = m_camera.getRadianPitch();
  312. }
  313. }
  314. void OpenRaider::handleKeyReleaseEvent(unsigned int key, unsigned int mod)
  315. {
  316. }
  317. void OpenRaider::handleBoundKeyPressEvent(unsigned int key)
  318. {
  319. switch (key)
  320. {
  321. case OpenRaiderKey_jump:
  322. m_camera.command(CAMERA_MOVE_UP);
  323. if (LARA)
  324. {
  325. LARA->moving = true;
  326. }
  327. break;
  328. case OpenRaiderKey_crouch:
  329. m_camera.command(CAMERA_MOVE_DOWN);
  330. if (LARA)
  331. {
  332. LARA->moving = true;
  333. }
  334. break;
  335. case OpenRaiderKey_forward:
  336. gWorld.moveEntity(LARA, 'f');
  337. if (LARA && LARA->moving)
  338. {
  339. switch (LARA->moveType)
  340. {
  341. case worldMoveType_swim:
  342. case worldMoveType_fly:
  343. case worldMoveType_noClipping:
  344. eventAnimTest(TR_ANIAMTION_SWIM);
  345. break;
  346. default:
  347. case worldMoveType_walk:
  348. case worldMoveType_walkNoSwim:
  349. eventAnimTest(TR_ANIAMTION_RUN);
  350. }
  351. }
  352. else if (LARA)
  353. {
  354. switch (LARA->moveType)
  355. {
  356. case worldMoveType_swim:
  357. case worldMoveType_fly:
  358. case worldMoveType_noClipping:
  359. eventAnimTest(TR_ANIAMTION_SWIM_IDLE);
  360. break;
  361. default:
  362. case worldMoveType_walk:
  363. case worldMoveType_walkNoSwim:
  364. eventAnimTest(TR_ANIAMTION_HIT_WALL_FRONT);
  365. }
  366. }
  367. else
  368. {
  369. m_camera.command(CAMERA_MOVE_FORWARD);
  370. }
  371. break;
  372. case OpenRaiderKey_backward:
  373. gWorld.moveEntity(LARA, 'b');
  374. if (LARA && LARA->moving)
  375. {
  376. switch (LARA->moveType)
  377. {
  378. case worldMoveType_swim:
  379. case worldMoveType_fly:
  380. case worldMoveType_noClipping:
  381. eventAnimTest(TR_ANIAMTION_SWIM);
  382. break;
  383. default:
  384. case worldMoveType_walk:
  385. case worldMoveType_walkNoSwim:
  386. eventAnimTest(TR_ANIAMTION_RUN);
  387. }
  388. }
  389. break;
  390. case OpenRaiderKey_left:
  391. gWorld.moveEntity(LARA, 'l');
  392. if (LARA && LARA->moving)
  393. {
  394. switch (LARA->moveType)
  395. {
  396. case worldMoveType_swim:
  397. case worldMoveType_fly:
  398. case worldMoveType_noClipping:
  399. eventAnimTest(TR_ANIAMTION_SWIM);
  400. break;
  401. default:
  402. case worldMoveType_walk:
  403. case worldMoveType_walkNoSwim:
  404. eventAnimTest(TR_ANIAMTION_RUN);
  405. }
  406. }
  407. else if (!LARA)
  408. {
  409. m_camera.command(CAMERA_MOVE_LEFT);
  410. }
  411. break;
  412. case OpenRaiderKey_right:
  413. gWorld.moveEntity(LARA, 'r');
  414. if (LARA && LARA->moving)
  415. {
  416. switch (LARA->moveType)
  417. {
  418. case worldMoveType_swim:
  419. case worldMoveType_fly:
  420. case worldMoveType_noClipping:
  421. eventAnimTest(TR_ANIAMTION_SWIM);
  422. break;
  423. default:
  424. case worldMoveType_walk:
  425. case worldMoveType_walkNoSwim:
  426. eventAnimTest(TR_ANIAMTION_RUN);
  427. }
  428. }
  429. else if (!LARA)
  430. {
  431. m_camera.command(CAMERA_MOVE_RIGHT);
  432. }
  433. break;
  434. case OpenRaiderKey_panRight:
  435. m_camera.command(CAMERA_ROTATE_RIGHT);
  436. break;
  437. case OpenRaiderKey_panLeft:
  438. m_camera.command(CAMERA_ROTATE_LEFT);
  439. break;
  440. case OpenRaiderKey_tiltDown:
  441. m_camera.command(CAMERA_ROTATE_DOWN);
  442. break;
  443. case OpenRaiderKey_tiltUp:
  444. m_camera.command(CAMERA_ROTATE_UP);
  445. break;
  446. }
  447. }
  448. void OpenRaider::handleBoundKeyReleaseEvent(unsigned int key)
  449. {
  450. switch (key)
  451. {
  452. case '`': // Temp hack also until console is rolled back
  453. //i = 2;
  454. if (mConsoleMode)
  455. {
  456. //buffer[0] = '>';
  457. //buffer[1] = ' ';
  458. //buffer[2] = 0;
  459. mText->setActive(textConsole, true);
  460. }
  461. else
  462. {
  463. mText->setActive(textConsole, false);
  464. //buffer[0] = 0;
  465. }
  466. break;
  467. case OpenRaiderKey_forward:
  468. case OpenRaiderKey_backward:
  469. case OpenRaiderKey_right:
  470. case OpenRaiderKey_left:
  471. if (LARA)
  472. {
  473. LARA->moving = false;
  474. }
  475. }
  476. }
  477. void OpenRaider::handleConsoleKeyPressEvent(unsigned int key,unsigned int mod)
  478. {
  479. static char lastCmd[64] = "2 ";
  480. static unsigned int i = 0;
  481. char *buffer;
  482. unsigned int len;
  483. // Cheap console test
  484. len = mText->GetStringLen(textConsole);
  485. buffer = mText->GetBuffer(textConsole);
  486. //printf("- 0x%x\n", key);
  487. if (key == mConsoleKey)
  488. {
  489. i = 2;
  490. if (mConsoleMode)
  491. {
  492. buffer[0] = '>';
  493. buffer[1] = ' ';
  494. buffer[2] = 0;
  495. mText->setActive(textConsole, true);
  496. }
  497. else
  498. {
  499. mText->setActive(textConsole, false);
  500. buffer[0] = 0;
  501. }
  502. }
  503. else if (mConsoleMode && len > i+1)
  504. {
  505. switch (key)
  506. {
  507. case SYS_KEY_UP:
  508. i = lastCmd[0];
  509. lastCmd[0] = '>';
  510. mText->SetString(textConsole, lastCmd);
  511. break;
  512. case 0:
  513. break;
  514. case '\b':
  515. if (i > 2)
  516. {
  517. buffer[--i] = 0;
  518. }
  519. break;
  520. case '\r':
  521. case '\n':
  522. strncpy(lastCmd, buffer, 63);
  523. lastCmd[0] = (char)i;
  524. command("[OpenRaider.Console]");
  525. command(buffer);
  526. i = 2;
  527. buffer[0] = '>';
  528. buffer[1] = ' ';
  529. buffer[2] = 0;
  530. break;
  531. default:
  532. if (mod & (SYS_MOD_KEY_RSHIFT | SYS_MOD_KEY_LSHIFT) &&
  533. key > 96 && key < 122)
  534. {
  535. buffer[i++] = (char)(key - 32);
  536. }
  537. else
  538. {
  539. if (mod & (SYS_MOD_KEY_RSHIFT | SYS_MOD_KEY_LSHIFT))
  540. {
  541. if (key == '-')
  542. key = '_';
  543. }
  544. buffer[i++] = (char)key;
  545. }
  546. buffer[i] = 0;
  547. }
  548. }
  549. }
  550. void OpenRaider::handleKeyPressEvent(unsigned int key, unsigned int mod)
  551. {
  552. static bool menu = false;
  553. // Cheap menu test
  554. if (menu)
  555. {
  556. switch (key)
  557. {
  558. case SYS_KEY_ESC:
  559. menu = false;
  560. mText->SetString(textMenu, " ");
  561. mText->setActive(textMenu, false);
  562. break;
  563. case SYS_KEY_ENTER:
  564. loadLevel(mMapList.current());
  565. mText->SetString(textMenu, " ");
  566. mText->setActive(textMenu, false);
  567. menu = false;
  568. break;
  569. case SYS_KEY_DOWN:
  570. mMapList.prev();
  571. if (!mMapList.backward())
  572. {
  573. mMapList.finish();
  574. }
  575. if (mMapList.current())
  576. {
  577. mText->SetString(textMenu, "Load %s?", mMapList.current());
  578. }
  579. else
  580. {
  581. mText->SetString(textMenu, "See README for map install");
  582. }
  583. break;
  584. case SYS_KEY_UP:
  585. mMapList.next();
  586. if (!mMapList.forward())
  587. {
  588. mMapList.start();
  589. }
  590. if (mMapList.current())
  591. {
  592. mText->SetString(textMenu, "Load %s?", mMapList.current());
  593. }
  594. else
  595. {
  596. mText->SetString(textMenu, "See README for map install");
  597. }
  598. }
  599. return;
  600. }
  601. switch (key)
  602. {
  603. case SYS_KEY_ESC:
  604. menu = true;
  605. mText->SetString(textMenu, "Select a map UP/DOWN");
  606. mText->setActive(textMenu, true);
  607. break;
  608. case SYS_KEY_ENTER:
  609. if (mod & SYS_MOD_KEY_LALT)
  610. {
  611. toggleFullscreen();
  612. }
  613. break;
  614. }
  615. ////////////////////////////////////////////////////////////
  616. switch (key)
  617. {
  618. case SYS_MOUSE_LEFT:
  619. mSound.play(m_testSFX);
  620. if (LARA)
  621. {
  622. eventAnimTest(15);
  623. }
  624. break;
  625. case '/':
  626. if (LARA)
  627. {
  628. entity_t *e = new entity_t;
  629. e->pos[0] = LARA->pos[0];
  630. e->pos[1] = LARA->pos[1] - 256.0f; // crawling H
  631. e->pos[2] = LARA->pos[2];
  632. e->type = 61;
  633. e->master = LARA->master;
  634. LARA->master = e;
  635. }
  636. break;
  637. case '[':
  638. if (LARA)
  639. {
  640. SkeletalModel *mdl = static_cast<SkeletalModel *>(LARA->tmpHook);
  641. if (mdl)
  642. {
  643. mdl->setIdleAnimation(mdl->getIdleAnimation() - 1);
  644. mdl->setAnimation(mdl->getIdleAnimation());
  645. print(false, "AFRAME %i\n", mdl->getIdleAnimation());
  646. }
  647. }
  648. break;
  649. case ']':
  650. if (LARA)
  651. {
  652. SkeletalModel *mdl = static_cast<SkeletalModel *>(LARA->tmpHook);
  653. if (mdl)
  654. {
  655. mdl->setIdleAnimation(mdl->getIdleAnimation() + 1);
  656. mdl->setAnimation(mdl->getIdleAnimation());
  657. print(false, "AFRAME %i\n", mdl->getIdleAnimation());
  658. }
  659. }
  660. break;
  661. case 'r':
  662. mSound.play(m_testSFX);
  663. break;
  664. }
  665. }
  666. void OpenRaider::print(bool dump_stdout, const char *format, ...)
  667. {
  668. static char buffer[128];
  669. va_list args;
  670. unsigned int l;
  671. va_start(args, format);
  672. vsnprintf(buffer, 128, format, args);
  673. l = strlen(buffer);
  674. if (!l || !buffer[0])
  675. {
  676. return;
  677. }
  678. // Strip message of an trailing carrage return
  679. if (buffer[l-1] == '\n')
  680. {
  681. buffer[l-1] = 0;
  682. }
  683. // Print to game 'console'
  684. if (mText)
  685. {
  686. mText->SetString(textOutput, buffer);
  687. }
  688. // Print to stdout
  689. if (dump_stdout)
  690. {
  691. fprintf(stdout, "> %s\n", buffer);
  692. }
  693. va_end(args);
  694. // Mongoose 2002.08.14, Currently, you must request
  695. // frame redraws in this mode
  696. if (m_flags & OpenRaider_Loading)
  697. {
  698. gameFrame();
  699. }
  700. }
  701. void OpenRaider::initGL()
  702. {
  703. // Mongoose 2002.08.13, Handled in start() actually
  704. }
  705. void OpenRaider::start()
  706. {
  707. vec3_t up;
  708. char *filename;
  709. printf("\n[Starting %s]\n\n", VERSION);
  710. filename = fullPath("~/.OpenRaider/OpenRaider.init", 0);
  711. if (loadResourceFile(filename) < 0)
  712. {
  713. printf("Could not find ~/.OpenRaider/OpenRaider.init\n");
  714. if (loadResourceFile("OpenRaider.init") < 0)
  715. {
  716. shutdown(-1);
  717. }
  718. printf("Loaded defaults...\n");
  719. }
  720. delete [] filename;
  721. m_camera.reset();
  722. m_camera.setSensitivityX(m_mouseX);
  723. m_camera.setSensitivityY(m_mouseY);
  724. m_camera.setSpeed(512);
  725. up[0] = 0.0;
  726. up[1] = -1.0;
  727. up[2] = 0.0;
  728. m_camera.setUp(up);
  729. if (mMapList.empty())
  730. {
  731. printf("No maps in map list, please edit your OpenRaider.init\n");
  732. shutdown(0);
  733. }
  734. // Mongoose 2002.08.13, System protected members abuse
  735. m_fovY = 40.0f;
  736. m_clipFar = 600000.0f;
  737. m_clipNear = 10.0f;
  738. // Mongoose 2002.08.13, This starts GL context
  739. printf("\n[Starting video subsystem...]\n");
  740. setFastCardPerformance(true);
  741. initVideo(m_width, m_height, false);
  742. // Mongoose 2002.01.02, Disable rendering to avoid GL call conflicts
  743. m_render.setMode(Render::modeDisabled);
  744. m_render.Init(m_width, m_height, m_fastCard);
  745. m_render.initTextures(m_homeDir, &m_texOffset, &mLevelTextureOffset);
  746. m_render.RegisterCamera(&m_camera);
  747. mText = m_render.GetString();
  748. // Mongoose 2002.01.03, Good idea?
  749. m_render.setMode(Render::modeLoadScreen);
  750. // Mongoose 2002.01.02, Load external audio tracks and fx
  751. // General audio init
  752. initSound();
  753. // Old room movement
  754. gWorld.setFlag(World::fEnableHopping);
  755. // reenabled, what should be the new room movement? --xythobuz
  756. resetTicks();
  757. runGame();
  758. }
  759. void OpenRaider::initSound()
  760. {
  761. //char filename[128];
  762. //unsigned int len;
  763. //int id;
  764. if (!(m_flags & OpenRaider_EnableSound) || mSound.init() < 0)
  765. {
  766. print(true, "Disabling sound...\n");
  767. m_flags |= OpenRaider_EnableSound;
  768. m_flags ^= OpenRaider_EnableSound;
  769. return;
  770. }
  771. #ifdef OBSOLETE
  772. printf("Processing external sound files: ");
  773. // Mongoose 2001.12.31, Use music list from init script
  774. if (mMusicList.Empty())
  775. {
  776. print(true, "No music in map list, please edit your OpenRaider.init\n");
  777. }
  778. else
  779. {
  780. snprintf(filename, 126, "%s%s", m_audioDir, mMusicList[0]);
  781. filename[127] = 0;
  782. if (!mSound.addFile(filename, &id, mSound.SoundFlagsLoop))
  783. {
  784. mSound.play(id);
  785. printf("m");
  786. fflush(stdout);
  787. }
  788. }
  789. //snprintf(filename, 126, "%s%s", m_homeDir, "sample.wav");
  790. //filename[127] = 0;
  791. //mSound.addFile(filename, &m_testSFX, mSound.SoundFlagsNone);
  792. printf(".");
  793. fflush(stdout);
  794. printf("\n");
  795. #endif
  796. }
  797. void OpenRaider::processPakSounds()
  798. {
  799. unsigned char *riff;
  800. unsigned int riffSz;
  801. //tr2_sound_source_t *sound;
  802. //tr2_sound_details_t *detail;
  803. //float pos[3];
  804. unsigned int i;
  805. int id;
  806. /* detail
  807. short sample;
  808. short volume;
  809. short sound_range;
  810. short flags; // bits 8-15: priority?, 2-7: number of sound samples
  811. // in this group, bits 0-1: channel number
  812. */
  813. if (m_flags & OpenRaider_EnableSound)
  814. {
  815. printf("Processing pak sound files: ");
  816. }
  817. else
  818. {
  819. return;
  820. }
  821. for (i = 0; i < m_tombraider.getSoundSamplesCount(); ++i)
  822. {
  823. print(false, "Processing pak sounds: %i / %i",
  824. i, m_tombraider.getSoundSamplesCount());
  825. m_tombraider.getSoundSample(i, &riffSz, &riff);
  826. mSound.addWave(riff, riffSz, &id, mSound.SoundFlagsNone);
  827. if (((i + 1) == TR_SOUND_F_PISTOL) && (id > 0))
  828. {
  829. m_testSFX = id;
  830. }
  831. delete [] riff;
  832. // sound[i].sound_id; // internal sound index
  833. // sound[i].flags; // 0x40, 0x80, or 0xc0
  834. //pos[0] = sound[i].x;
  835. //pos[1] = sound[i].y;
  836. //pos[2] = sound[i].z;
  837. //mSound.SourceAt(id, pos);
  838. printf(".");
  839. fflush(stdout);
  840. }
  841. printf("\n");
  842. }
  843. void OpenRaider::loadLevel(char *mapname)
  844. {
  845. int i, j, len;
  846. char filename[256];
  847. // Mongoose 2002.03.23, Clear last loaded 'world'
  848. m_render.setMode(Render::modeDisabled);
  849. m_render.ClearWorld();
  850. gWorld.destroy();
  851. // Mongoose 2002.01.02, Now draw load screen, since external
  852. // GL textures are loaded and renderer is initialized
  853. m_render.setMode(Render::modeLoadScreen);
  854. gameFrame();
  855. /* 2003.07.05, Mongoose - No longer seeding, and looking for
  856. map to load -- load this map or fail and return */
  857. snprintf(filename, 254, "%s%s", m_pakDir, mapname);
  858. filename[255] = 0;
  859. len = strlen(filename);
  860. for (i = len, j = 0; i > 0; --i, ++j)
  861. {
  862. if (filename[i] == '/' || filename[i] == '\\')
  863. break;
  864. }
  865. j--;
  866. memset(m_mapName, 0, 32);
  867. for (i = 0; i < len - j && i < 30; ++i)
  868. {
  869. m_mapName[i] = filename[i + len - j];
  870. }
  871. m_tombraider.setDebug(m_flags & OpenRaider_DebugMap);
  872. m_flags |= OpenRaider_Loading;
  873. print(true, "Level %s loading", m_mapName);
  874. if (m_tombraider.Load(filename, percent_callback))
  875. {
  876. print(true, "ERROR: Failed to load level %s\n", m_mapName);
  877. return;
  878. }
  879. else
  880. {
  881. char altSfx[256];
  882. switch (m_tombraider.getEngine())
  883. {
  884. case TR_VERSION_2:
  885. case TR_VERSION_3:
  886. // Now loads eg. "tr3/MAIN.SFX" instead of "tr3/SHORE.TR2.sfx"
  887. filename[len - j] = '\0';
  888. snprintf(altSfx, 255, "%sMAIN.SFX", filename);
  889. print(true, "Loading alternate SFX for TR2 or TR3 pak at %sMAIN.SFX\n", filename);
  890. m_tombraider.loadSFX(altSfx);
  891. break;
  892. default:
  893. case TR_VERSION_1:
  894. case TR_VERSION_4:
  895. case TR_VERSION_5:
  896. case TR_VERSION_UNKNOWN:
  897. ;
  898. }
  899. }
  900. print(true, "Optimizing and translating pak data...");
  901. m_flags ^= OpenRaider_Loading;
  902. // Mongoose 2002.08.16, Used to cache TR4 textures at 10% of file load
  903. // in a callback - then with other engines process them here
  904. m_render.setMode(Render::modeDisabled);
  905. //sleep(1); // Why should we sleep here? -- xythobuz
  906. processTextures();
  907. m_render.setMode(Render::modeLoadScreen);
  908. print(true, "Level textures cached\n");
  909. // Cache/process rooms
  910. printf("Processing rooms: ");
  911. for (i = 0; i < m_tombraider.NumRooms(); ++i)
  912. {
  913. processRoom(i);
  914. print(false, "Processing rooms: %i/%i", i, m_tombraider.NumRooms());
  915. }
  916. printf("\n");
  917. // Cache/process meshes
  918. printf("Processing meshes: ");
  919. for (i = 0; i < m_tombraider.getMeshCount(); ++i)
  920. {
  921. m_render.setMode(Render::modeDisabled);
  922. processModel(i);
  923. m_render.setMode(Render::modeLoadScreen);
  924. print(false, "Processing meshes: %i/%i", i, m_tombraider.getMeshCount());
  925. }
  926. printf("\n");
  927. // Cache/process sprites
  928. processSprites();
  929. // Cache/process skeletal models
  930. processMoveables();
  931. // Cache/process sound fx
  932. processPakSounds();
  933. #ifdef DEBUG_MEMORY
  934. // Right before pak is cleared will be highest use or memory
  935. display_memory_usage();
  936. #endif
  937. // Mongoose 2002.01.02, Go ahead and free the TR pak
  938. m_tombraider.reset();
  939. print(true, "Level pak freed from memory, Starting game...");
  940. #ifdef DEBUG_MEMORY
  941. display_memory_usage();
  942. #endif
  943. /*! \fixme GL call to critical section,
  944. * needs mutex really -- Mongoose 2002.01.02
  945. */
  946. m_render.setMode(Render::modeDisabled);
  947. sleep(1);
  948. // Draw game, level is loaded
  949. m_render.setMode(Render::modeVertexLight);
  950. m_flags ^= OpenRaider_Loading;
  951. }
  952. void OpenRaider::gameFrame()
  953. {
  954. SkeletalModel *mdl = 0x0;
  955. unsigned int ticks;
  956. float time;
  957. // Remember: ticks in milliseconds, time in hundredths
  958. gNetTicks = ticks = getTicks();
  959. time = gNetTicks * 0.1f;
  960. switch (m_render.getMode())
  961. {
  962. case Render::modeDisabled:
  963. break;
  964. case Render::modeLoadScreen:
  965. if ((ticks % 10000) > 1)
  966. {
  967. m_render.drawLoadScreen();
  968. swapBuffersGL();
  969. }
  970. break;
  971. default:
  972. if (LARA)
  973. {
  974. mdl = static_cast<SkeletalModel *>(LARA->tmpHook);
  975. if (mdl)
  976. {
  977. float elapsed = time - mdl->lastTime;
  978. if (elapsed > mdl->rate)
  979. {
  980. if (mdl->getAnimation() == 0 &&
  981. (mdl->getFrame() == 2 || mdl->getFrame() == 10))
  982. {
  983. if (m_flags & OpenRaider_EnableSound)
  984. {
  985. mSound.play(TR_SOUND_FOOTSTEP0);
  986. }
  987. }
  988. else if (mdl->getAnimation() == 15)
  989. {
  990. eventAnimTest(TR_ANIAMTION_RUN);
  991. }
  992. mdl->setFrame(mdl->getFrame()+1);
  993. mdl->lastTime = elapsed / mdl->rate;
  994. }
  995. else
  996. {
  997. mdl->lastTime = time;
  998. }
  999. }
  1000. }
  1001. m_render.Display();
  1002. swapBuffersGL();
  1003. if (mdl && mdl->getAnimation() == 12)
  1004. {
  1005. switch (LARA->moveType)
  1006. {
  1007. case worldMoveType_swim:
  1008. case worldMoveType_fly:
  1009. case worldMoveType_noClipping:
  1010. eventAnimTest(TR_ANIAMTION_SWIM_IDLE);
  1011. break;
  1012. default:
  1013. case worldMoveType_walk:
  1014. case worldMoveType_walkNoSwim:
  1015. eventAnimTest(TR_ANIAMTION_STAND);
  1016. }
  1017. LARA->moving = false;
  1018. }
  1019. }
  1020. if (m_flags & OpenRaider_ShowFPS)
  1021. {
  1022. static unsigned int frames = 0;
  1023. static float lastTime = 0.0f;
  1024. ++frames;
  1025. // Only update every second, so it can't be affected
  1026. // by fast/slow cards it only counts frames instead
  1027. // of estimating
  1028. if (time - lastTime > 100.0f)
  1029. {
  1030. if (mText)
  1031. mText->SetString(1, "%dFPS", frames);
  1032. lastTime = time;
  1033. frames = 0;
  1034. }
  1035. }
  1036. }
  1037. void OpenRaider::processTextures()
  1038. {
  1039. unsigned char *image;
  1040. unsigned char *bumpmap;
  1041. int i;
  1042. printf("Processing TR textures: ");
  1043. //if ( m_tombraider.getNumBumpMaps())
  1044. // gBumpMapStart = m_tombraider.NumTextures();
  1045. for (i = 0; i < m_tombraider.NumTextures(); ++i)
  1046. {
  1047. print(false, "Processing textures: %i/%i", i, m_tombraider.NumTextures());
  1048. m_tombraider.Texture(i, &image, &bumpmap);
  1049. // Overwrite any previous level textures on load
  1050. m_render.loadTexture(image, 256, 256, mLevelTextureOffset + i);
  1051. gMapTex2Bump.Add(mLevelTextureOffset + i, -1);
  1052. if (bumpmap)
  1053. {
  1054. gMapTex2Bump.Add(mLevelTextureOffset + i, mLevelTextureOffset + i +
  1055. m_tombraider.NumTextures());
  1056. m_render.loadTexture(bumpmap, 256, 256, mLevelTextureOffset + i +
  1057. m_tombraider.NumTextures());
  1058. }
  1059. if (image)
  1060. delete [] image;
  1061. if (bumpmap)
  1062. delete [] bumpmap;
  1063. printf(".");
  1064. fflush(stdout);
  1065. }
  1066. gTextureOffset = mLevelTextureOffset + m_tombraider.NumTextures();
  1067. printf("\n");
  1068. }
  1069. ////////////////////////////////////////////////////////////
  1070. // Private Accessors
  1071. ////////////////////////////////////////////////////////////
  1072. ////////////////////////////////////////////////////////////
  1073. // Private Mutators
  1074. ////////////////////////////////////////////////////////////
  1075. void OpenRaider::soundEvent(int type, int id, vec3_t pos, vec3_t angles)
  1076. {
  1077. switch (type)
  1078. {
  1079. case 0: // Reset listener position
  1080. mSound.listenAt(pos, angles);
  1081. break;
  1082. default:
  1083. mSound.sourceAt(id, pos);
  1084. mSound.play(id);
  1085. }
  1086. }
  1087. void OpenRaider::processSprites()
  1088. {
  1089. int i, j, k, l, x, y, s_index, width, height;
  1090. float scale, width2, height2;
  1091. tr2_sprite_texture_t *sprite;
  1092. tr2_sprite_texture_t *sprite_textures;
  1093. tr2_sprite_sequence_t *sprite_sequence;
  1094. sprite_seq_t *r_mesh;
  1095. tr2_item_t *item;
  1096. item = m_tombraider.Item();
  1097. sprite_textures = m_tombraider.Sprite();
  1098. sprite_sequence = m_tombraider.SpriteSequence();
  1099. scale = 4.0;
  1100. printf("Processing sprites: ");
  1101. for (i = 0; i < m_tombraider.NumItems() - 1; ++i)
  1102. {
  1103. print(false, "Processing sprites in Items: %i/%i",
  1104. i, m_tombraider.NumItems());
  1105. // It's a mesh, skip it
  1106. if (m_tombraider.Engine() == TR_VERSION_1 && item[i].intensity1 == -1)
  1107. continue;
  1108. k = item[i].object_id;
  1109. // Search the SpriteSequence list
  1110. // (if we didn't already decide that it's a mesh)
  1111. for (j = 0; j < (int)m_tombraider.NumSpriteSequences(); ++j)
  1112. {
  1113. if (sprite_sequence[j].object_id == k)
  1114. {
  1115. k = item[i].object_id;
  1116. s_index = sprite_sequence[j].offset;
  1117. r_mesh = new sprite_seq_t;
  1118. gWorld.addSprite(r_mesh);
  1119. r_mesh->num_sprites = -sprite_sequence[j].negative_length;
  1120. r_mesh->sprite = new sprite_t[r_mesh->num_sprites];
  1121. for (l = 0; l < r_mesh->num_sprites; ++l)
  1122. {
  1123. sprite = &sprite_textures[s_index];
  1124. width = sprite->width >> 8;
  1125. height = sprite->height >> 8;
  1126. x = sprite->x;
  1127. y = sprite->y;
  1128. width2 = width * scale;
  1129. height2 = height * scale;
  1130. // For vising use
  1131. r_mesh->sprite[l].pos[0] = item[i].x;
  1132. r_mesh->sprite[l].pos[1] = item[i].y;
  1133. r_mesh->sprite[l].pos[2] = item[i].z;
  1134. r_mesh->sprite[l].texture = sprite->tile + m_texOffset;
  1135. r_mesh->sprite[l].radius = width2 / 2.0f;
  1136. r_mesh->sprite[l].vertex[0].pos[0] = -width2 / 2.0f;
  1137. r_mesh->sprite[l].vertex[1].pos[0] = -width2 / 2.0f;
  1138. r_mesh->sprite[l].vertex[2].pos[0] = width2 / 2.0f;
  1139. r_mesh->sprite[l].vertex[3].pos[0] = width2 / 2.0f;
  1140. r_mesh->sprite[l].vertex[0].pos[1] = 0;
  1141. r_mesh->sprite[l].vertex[1].pos[1] = -height2;
  1142. r_mesh->sprite[l].vertex[2].pos[1] = -height2;
  1143. r_mesh->sprite[l].vertex[3].pos[1] = 0;
  1144. r_mesh->sprite[l].vertex[0].pos[2] = 0;
  1145. r_mesh->sprite[l].vertex[1].pos[2] = 0;
  1146. r_mesh->sprite[l].vertex[2].pos[2] = 0;
  1147. r_mesh->sprite[l].vertex[3].pos[2] = 0;
  1148. r_mesh->sprite[l].texel[3].st[0] = (vec_t)(x+width)/TexelScale;
  1149. r_mesh->sprite[l].texel[3].st[1] = (vec_t)(y+height)/TexelScale;
  1150. r_mesh->sprite[l].texel[2].st[0] = (vec_t)(x+width)/TexelScale;
  1151. r_mesh->sprite[l].texel[2].st[1] = (vec_t)(y)/TexelScale;
  1152. r_mesh->sprite[l].texel[1].st[0] = (vec_t)(x) /TexelScale;
  1153. r_mesh->sprite[l].texel[1].st[1] = (vec_t)(y) /TexelScale;
  1154. r_mesh->sprite[l].texel[0].st[0] = (vec_t)(x) / TexelScale;
  1155. r_mesh->sprite[l].texel[0].st[1] = (vec_t)(y+height)/TexelScale;
  1156. printf(".");
  1157. fflush(stdout);
  1158. }
  1159. }
  1160. }
  1161. }
  1162. printf("\n");
  1163. }
  1164. void OpenRaider::processMoveables()
  1165. {
  1166. List <unsigned int> cache;
  1167. List <skeletal_model_t *> cache2;
  1168. tr2_mesh_t *mesh = NULL;
  1169. tr2_moveable_t *moveable = NULL;
  1170. tr2_meshtree_t *meshtree = NULL;
  1171. tr2_item_t *item = NULL;
  1172. tr2_animation_t *animation = NULL;
  1173. unsigned short *frame = NULL;
  1174. tr2_sprite_sequence_t *sprite_sequence = NULL;
  1175. tr2_object_texture_t *object_texture = NULL;
  1176. int i, j, object_id;
  1177. int ent = 0;
  1178. frame = m_tombraider.Frame();
  1179. moveable = m_tombraider.Moveable();
  1180. meshtree = m_tombraider.MeshTree();
  1181. mesh = m_tombraider.Mesh();
  1182. object_texture = m_tombraider.ObjectTextures();
  1183. item = m_tombraider.Item();
  1184. animation = m_tombraider.Animation();
  1185. sprite_sequence = m_tombraider.SpriteSequence();
  1186. printf("Processing skeletal models: ");
  1187. for (i = 0; i < m_tombraider.NumItems(); ++i)
  1188. {
  1189. object_id = item[i].object_id;
  1190. // It may not be a moveable, test for sprite
  1191. if (!(m_tombraider.Engine() == TR_VERSION_1 && item[i].intensity1 == -1))
  1192. {
  1193. for (j = 0; j < (int)m_tombraider.NumSpriteSequences(); ++j)
  1194. {
  1195. if (sprite_sequence[j].object_id == object_id)
  1196. break;
  1197. }
  1198. // It's not a moveable, skip sprite
  1199. if (j != (int)m_tombraider.NumSpriteSequences())
  1200. {
  1201. printf("s");
  1202. fflush(stdout);
  1203. continue;
  1204. }
  1205. }
  1206. for (j = 0; j < (int)m_tombraider.NumMoveables(); ++j)
  1207. {
  1208. if ((int)moveable[j].object_id == object_id)
  1209. break;
  1210. }
  1211. // It's not a moveable or even a sprite?, skip unknown
  1212. if (j == (int)m_tombraider.NumMoveables())
  1213. {
  1214. printf("?"); // what the wolf?
  1215. fflush(stdout);
  1216. continue;
  1217. }
  1218. processMoveable(j, i, &ent, cache2, cache, object_id);
  1219. }
  1220. // Get models that aren't items
  1221. for (i = 0; i < m_tombraider.NumMoveables(); ++i)
  1222. {
  1223. print(false, "Processing skeletal models: %i/%i",
  1224. i, m_tombraider.NumMoveables());
  1225. switch ((int)moveable[i].object_id)
  1226. {
  1227. case 30:
  1228. case 2: // Which tr needs this as model agian?
  1229. processMoveable(i, i, &ent, cache2, cache,
  1230. (int)moveable[i].object_id);
  1231. break;
  1232. default:
  1233. switch (m_tombraider.Engine())
  1234. {
  1235. case TR_VERSION_1:
  1236. switch ((int)moveable[i].object_id)
  1237. {
  1238. case TombRaider1::LaraMutant:
  1239. processMoveable(i, i, &ent, cache2, cache,
  1240. (int)moveable[i].object_id);
  1241. break;
  1242. }
  1243. break;
  1244. case TR_VERSION_4:
  1245. switch ((int)moveable[i].object_id)
  1246. {
  1247. case TR4_PISTOLS_ANIM:
  1248. case TR4_UZI_ANIM:
  1249. case TR4_SHOTGUN_ANIM:
  1250. case TR4_CROSSBOW_ANIM:
  1251. case TR4_GRENADE_GUN_ANIM:
  1252. case TR4_SIXSHOOTER_ANIM:
  1253. processMoveable(i, i, &ent, cache2, cache,
  1254. (int)moveable[i].object_id);
  1255. break;
  1256. }
  1257. break;
  1258. default:
  1259. case TR_VERSION_2:
  1260. case TR_VERSION_3:
  1261. case TR_VERSION_5:
  1262. case TR_VERSION_UNKNOWN:
  1263. ;
  1264. }
  1265. }
  1266. }
  1267. printf("\n");
  1268. }
  1269. void OpenRaider::processMoveable(int index, int i, int *ent,
  1270. List <skeletal_model_t *> &cache2,
  1271. List <unsigned int> &cache, int object_id)
  1272. {
  1273. skeletal_model_t *r_model = NULL;
  1274. skeletal_model_t *c_model = NULL;
  1275. animation_frame_t *animation_frame = NULL;
  1276. tr2_mesh_t *mesh = NULL;
  1277. tr2_moveable_t *moveable = NULL;
  1278. tr2_meshtree_t *meshtree = NULL;
  1279. tr2_item_t *item = NULL;
  1280. tr2_animation_t *animation = NULL;
  1281. tr2_meshtree_t *mesh_tree = NULL;
  1282. bone_frame_t *bone = NULL;
  1283. bone_tag_t *tag = NULL;
  1284. entity_t *thing = NULL;
  1285. SkeletalModel *sModel = 0x0;
  1286. unsigned short *frame;
  1287. int j, k, a, frame_step;
  1288. unsigned int l, frame_offset, frame_count, f;
  1289. float pos[3];
  1290. float yaw;
  1291. bool lara = false;
  1292. int skyMesh;
  1293. skyMesh = m_tombraider.getSkyModelId();
  1294. frame = m_tombraider.Frame();
  1295. moveable = m_tombraider.Moveable();
  1296. meshtree = m_tombraider.MeshTree();
  1297. mesh = m_tombraider.Mesh();
  1298. item = m_tombraider.Item();
  1299. animation = m_tombraider.Animation();
  1300. pos[0] = item[i].x;
  1301. pos[1] = item[i].y;
  1302. pos[2] = item[i].z;
  1303. yaw = ((item[i].angle >> 14) & 0x03);
  1304. yaw *= 90;
  1305. thing = new entity_t;
  1306. thing->id = (*ent)++;
  1307. thing->type = 0x00;
  1308. thing->pos[0] = item[i].x;
  1309. thing->pos[1] = item[i].y;
  1310. thing->pos[2] = item[i].z;
  1311. thing->angles[1] = yaw;
  1312. thing->objectId = moveable[index].object_id;
  1313. thing->moving = false;
  1314. thing->animate = false;
  1315. sModel = new SkeletalModel();
  1316. m_render.addSkeletalModel(sModel);
  1317. thing->tmpHook = sModel; // temp hack to keep a running version during refactoring
  1318. if (m_tombraider.Engine() == TR_VERSION_1)
  1319. {
  1320. switch (thing->objectId)
  1321. {
  1322. case TombRaider1::Wolf:
  1323. thing->state = TombRaider1::WolfState_Lying;
  1324. //thing->animate = true;
  1325. sModel->setAnimation(3);
  1326. sModel->setFrame(0);
  1327. break;
  1328. }
  1329. }
  1330. //! \fixme Check here and see if we already have one for object_id later
  1331. // if (gWorld.isCachedSkeletalModel(moveable[index].object_id))
  1332. // {
  1333. // thing->modelId = m_render.add(sModel);
  1334. // return;
  1335. // }
  1336. r_model = new skeletal_model_t;
  1337. r_model->id = moveable[index].object_id;
  1338. // Gather more info if this is lara
  1339. if (moveable[index].object_id == 0)
  1340. {
  1341. gLaraModel = r_model; // hack to avoid broken system until new event sys
  1342. lara = true;
  1343. m_camera.translate(pos[0], pos[1] - 470, pos[2]);
  1344. thing->type = 0x02;
  1345. LARA = thing; // Mongoose 2002.03.22, Cheap hack for now
  1346. LARA->master = 0x0;
  1347. switch (m_tombraider.Engine())
  1348. {
  1349. case TR_VERSION_3:
  1350. LARA->modelId = i;
  1351. sModel->setAnimation(TR_ANIAMTION_RUN);
  1352. sModel->setIdleAnimation(TR_ANIAMTION_STAND);
  1353. r_model->tr4Overlay = false;
  1354. break;
  1355. case TR_VERSION_4:
  1356. LARA->modelId = i;
  1357. sModel->setAnimation(TR_ANIAMTION_RUN);
  1358. sModel->setIdleAnimation(TR_ANIAMTION_STAND);
  1359. // Only TR4 lara has 2 layer bone tags/meshes per bone frame
  1360. r_model->tr4Overlay = true;
  1361. break;
  1362. default:
  1363. case TR_VERSION_1:
  1364. case TR_VERSION_2:
  1365. case TR_VERSION_5:
  1366. case TR_VERSION_UNKNOWN:
  1367. LARA->modelId = index;
  1368. sModel->setAnimation(TR_ANIAMTION_RUN);
  1369. sModel->setIdleAnimation(TR_ANIAMTION_STAND);
  1370. r_model->tr4Overlay = false;
  1371. }
  1372. r_model->ponytailId = 0;
  1373. }
  1374. else
  1375. {
  1376. lara = false;
  1377. r_model->ponytailId = -1;
  1378. }
  1379. // Animation
  1380. a = moveable[index].animation;
  1381. frame_offset = animation[a].frame_offset / 2;
  1382. frame_step = animation[a].frame_size;
  1383. int frame_cycle = 0;
  1384. if (a >= (int)m_tombraider.NumAnimations())
  1385. {
  1386. a = m_tombraider.NumFrames() - frame_offset;
  1387. }
  1388. else
  1389. {
  1390. a = (animation[a].frame_offset / 2) - frame_offset;
  1391. }
  1392. if (frame_step != 0) // prevent divide-by-zero errors
  1393. a /= frame_step;
  1394. if (a != 0) // prevent divide-by-zero errors
  1395. frame_offset += frame_step * (frame_cycle % a);
  1396. if (a < 0)
  1397. {
  1398. // continue;
  1399. return;
  1400. }
  1401. //! \fixme Might be better UID for each model, but this seems to work well
  1402. j = object_id;
  1403. // We only want one copy of the skeletal model in memory
  1404. if (cache.Empty() || cache.SearchKey(j) == UINT_MAX)
  1405. {
  1406. sModel->model = r_model;
  1407. gWorld.addEntity(thing);
  1408. k = gWorld.addModel(r_model);
  1409. cache.Add(j);
  1410. cache2.Add(r_model);
  1411. switch (m_tombraider.Engine())
  1412. {
  1413. case TR_VERSION_4:
  1414. if (LARA && moveable[index].object_id == 30)
  1415. {
  1416. r_model->ponytailId = k;
  1417. r_model->ponytailMeshId = moveable[index].starting_mesh;
  1418. r_model->ponytailNumMeshes = ((moveable[index].num_meshes > 0) ?
  1419. moveable[index].num_meshes : 0);
  1420. r_model->ponytailAngle = -90.0f;
  1421. r_model->ponytail[0] = -3;
  1422. r_model->ponytail[1] = -22;
  1423. r_model->ponytail[2] = -20;
  1424. r_model->ponyOff = 40;
  1425. r_model->ponyOff2 = 32;
  1426. r_model->pigtails = false;
  1427. // Try to guess pigtails by looking for certian num verts in head
  1428. if (mesh[moveable[0].starting_mesh].num_vertices > 80)
  1429. {
  1430. r_model->pigtails = true;
  1431. r_model->ponyOff -= 20;
  1432. r_model->ponytail[1] -= 32;
  1433. }
  1434. m_render.setFlags(Render::fRenderPonytail);
  1435. print(true, "Found known ponytail\n");
  1436. }
  1437. break; // ?
  1438. default:
  1439. case TR_VERSION_1:
  1440. case TR_VERSION_2:
  1441. case TR_VERSION_3:
  1442. case TR_VERSION_5:
  1443. case TR_VERSION_UNKNOWN:
  1444. if (LARA && moveable[index].object_id == 2)
  1445. {
  1446. r_model->ponytailId = k;
  1447. r_model->ponytailMeshId = moveable[index].starting_mesh;
  1448. r_model->ponytailNumMeshes = ((moveable[index].num_meshes > 0) ?
  1449. moveable[index].num_meshes : 0);
  1450. r_model->ponytailAngle = -90.0f;
  1451. r_model->ponytail[0] = 0;
  1452. r_model->ponytail[1] = -20;
  1453. r_model->ponytail[2] = -20;
  1454. r_model->ponyOff = 40;
  1455. r_model->ponyOff2 = 0;
  1456. m_render.setFlags(Render::fRenderPonytail);
  1457. print(true, "Found ponytail?\n");
  1458. }
  1459. }
  1460. }
  1461. else
  1462. {
  1463. delete r_model;
  1464. c_model = cache2[cache.SearchKey(j)];
  1465. sModel->model = c_model;
  1466. gWorld.addEntity(thing);
  1467. gWorld.addModel(c_model);
  1468. printf("c"); // it's already cached
  1469. fflush(stdout);
  1470. // continue;
  1471. return;
  1472. }
  1473. int aloop = m_tombraider.getNumAnimsForMoveable(index);
  1474. #ifdef DEBUG
  1475. if (m_flags & OpenRaider_DebugModel)
  1476. {
  1477. printf("\nanimation = %i, num_animations = %i\n",
  1478. moveable[index].animation, aloop);
  1479. printf("\nitem[%i].flags = %i\nentity[%i]\n",
  1480. i, item[i].flags, thing->id);
  1481. }
  1482. #endif
  1483. //a = moveable[index].animation;
  1484. //frame_offset = animation[a].frame_offset / 2;
  1485. //frame_step = animation[a].frame_size;
  1486. for (; a < aloop; ++a,
  1487. frame_offset = animation[a].frame_offset / 2,
  1488. frame_step = animation[a].frame_size)
  1489. {
  1490. animation_frame = new animation_frame_t;
  1491. r_model->animation.pushBack(animation_frame);
  1492. frame_count = (animation[a].frame_end - animation[a].frame_start) + 1;
  1493. animation_frame->rate = animation[a].frame_rate;
  1494. #ifdef DEBUG
  1495. if (m_flags & OpenRaider_DebugModel)
  1496. {
  1497. printf("animation[%i] state and unknowns = %i, %i, %i, %i, %i\n",
  1498. a, animation[a].state_id, animation[a].unknown1,
  1499. animation[a].unknown2, animation[a].unknown3,
  1500. animation[a].unknown4);
  1501. printf("animation[%i].frame_rate = %i\n",
  1502. a, animation[a].frame_rate);
  1503. printf("animation[%i].next_animation = %i\n",
  1504. a, animation[a].next_animation);
  1505. printf("animation[%i].frame_offset = %i\n",
  1506. a, animation[a].frame_offset);
  1507. printf("animation[%i].anim_command = %i\n",
  1508. a, animation[a].anim_command);
  1509. printf("animation[%i].num_anim_commands = %i\n",
  1510. a, animation[a].num_anim_commands);
  1511. printf("animation[%i].state_change_offset = %i\n",
  1512. a, animation[a].state_change_offset);
  1513. printf(" frame_offset = %i\n",
  1514. frame_offset);
  1515. }
  1516. #endif
  1517. // Get all the frames for aniamtion
  1518. for (f = 0; f < frame_count; ++f, frame_offset += frame_step)
  1519. {
  1520. // HACK: Lara's ObjectID is 315, but her meshes start at 0, so make a
  1521. // quick substitution (so she doesn't appear as a bunch of thighs)
  1522. if (index == 0 && m_tombraider.Engine() == TR_VERSION_3)
  1523. {
  1524. for (j = 0; j < (int)m_tombraider.NumMoveables() && !index; ++j)
  1525. {
  1526. if (moveable[j].object_id == 315)
  1527. index = j;
  1528. }
  1529. }
  1530. // Fix Lara in TR4
  1531. if (index == 0 && m_tombraider.Engine() == TR_VERSION_4)
  1532. {
  1533. for (j = 0; j < (int)m_tombraider.NumMoveables() && !index; ++j)
  1534. {
  1535. // Body is ItemID 8, joints are ItemID 9
  1536. // (TR4 demo: body is ItemID 10, joints are ItemID 11)
  1537. if (moveable[j].object_id == 8)
  1538. index = j;
  1539. }
  1540. }
  1541. else if (moveable[index].object_id == 8 &&
  1542. m_tombraider.Engine() == TR_VERSION_4)
  1543. {
  1544. // KLUDGE to do "skinning"
  1545. index = 0;
  1546. for (j = 0; j < (int)m_tombraider.NumMoveables() && !index; ++j)
  1547. {
  1548. // Body is ItemID 8, joints are ItemID 9
  1549. // (TR4 demo: body is ItemID 10, joints are ItemID 11)
  1550. if (moveable[j].object_id == 9)
  1551. index = j;
  1552. }
  1553. }
  1554. #ifdef DEBUG
  1555. if (m_flags & OpenRaider_DebugModel)
  1556. {
  1557. printf("animation[%i].boneframe[%i] = offset %i, step %i\n",
  1558. a, f, frame_offset, frame_step);
  1559. }
  1560. #endif
  1561. // Mongoose 2002.08.15, Was
  1562. // if (frame_offset + 8 > _tombraider.NumFrames())
  1563. if (frame_offset > m_tombraider.NumFrames())
  1564. {
  1565. print(true, "WARNING: Bad animation frame %i > %i\n",
  1566. frame_offset, m_tombraider.NumFrames());
  1567. // Mongoose 2002.08.15, Atempt to skip more likely bad animation data
  1568. print(true, "WARNING: Handling bad animation data...");
  1569. return; //continue;
  1570. }
  1571. // Generate bone frames and tags per frame ////////////
  1572. bone = new bone_frame_t;
  1573. animation_frame->frame.pushBack(bone);
  1574. // Init translate for bone frame
  1575. bone->pos[0] = (short)frame[frame_offset + 6];
  1576. bone->pos[1] = (short)frame[frame_offset + 7];
  1577. bone->pos[2] = (short)frame[frame_offset + 8];
  1578. bone->yaw = yaw;
  1579. //printf("%f %f %f\n", bone->pos[0], bone->pos[1], bone->pos[2]);
  1580. l = 9; // First angle offset in this Frame
  1581. // Run through the tag and calculate the rotation and offset
  1582. for (j = 0; j < (int)moveable[index].num_meshes; ++j)
  1583. {
  1584. tag = new bone_tag_t;
  1585. bone->tag.pushBack(tag);
  1586. tag->off[0] = 0.0;
  1587. tag->off[1] = 0.0;
  1588. tag->off[2] = 0.0;
  1589. tag->flag = 0x00;
  1590. tag->rot[0] = 0.0;
  1591. tag->rot[1] = 0.0;
  1592. tag->rot[2] = 0.0;
  1593. tag->mesh = moveable[index].starting_mesh + j;
  1594. // Setup offsets to produce skeletion
  1595. if (j == 0)
  1596. {
  1597. // Since we use bone's offset, these aren't used
  1598. tag->off[0] = 0.0;
  1599. tag->off[1] = 0.0;
  1600. tag->off[2] = 0.0;
  1601. // Always push tag[0], this isn't really used either
  1602. tag->flag = 0x02;
  1603. }
  1604. else // Nonprimary tag - position relative to first tag
  1605. {
  1606. int *tree;
  1607. // Hack: moveable[index].mesh_tree is a byte offset
  1608. // into mesh_tree[], so we have to convert to index
  1609. tree = (int *)(void *)meshtree;
  1610. mesh_tree = (tr2_meshtree_t *)&tree[moveable[index].mesh_tree
  1611. + ((j - 1) * 4)];
  1612. tag->off[0] = mesh_tree->x;
  1613. tag->off[1] = mesh_tree->y;
  1614. tag->off[2] = mesh_tree->z;
  1615. tag->flag = (char)mesh_tree->flags;
  1616. }
  1617. // Setup tag rotations
  1618. m_tombraider.computeRotationAngles(&frame, &frame_offset, &l,
  1619. tag->rot, tag->rot+1, tag->rot+2);
  1620. }
  1621. }
  1622. }
  1623. if (i == skyMesh)
  1624. {
  1625. m_render.setSkyMesh(i, //moveable[i].starting_mesh,
  1626. (m_tombraider.Engine() == TR_VERSION_2));
  1627. }
  1628. printf(".");
  1629. fflush(stdout);
  1630. }
  1631. int compareFaceTextureId(const void *voidA, const void *voidB)
  1632. {
  1633. texture_tri_t *a = (texture_tri_t *)voidA, *b = (texture_tri_t *)voidB;
  1634. if (!a || !b)
  1635. return -1; // error really
  1636. // less than
  1637. if (a->texture < b->texture)
  1638. return -1;
  1639. // greater than ( no need for equal )
  1640. return 1;
  1641. }
  1642. void setupTextureColor(texture_tri_t *r_tri, Render *render, float *colorf)
  1643. {
  1644. unsigned char color[4];
  1645. unsigned int colorI;
  1646. color[0] = (unsigned char)(colorf[0]*255.0f);
  1647. color[1] = (unsigned char)(colorf[1]*255.0f);
  1648. color[2] = (unsigned char)(colorf[2]*255.0f);
  1649. color[3] = (unsigned char)(colorf[3]*255.0f);
  1650. ((unsigned char *)(&colorI))[3] = color[0];
  1651. ((unsigned char *)(&colorI))[2] = color[1];
  1652. ((unsigned char *)(&colorI))[1] = color[2];
  1653. ((unsigned char *)(&colorI))[0] = color[3];
  1654. if (!gColorTextureHACK.find(colorI))
  1655. {
  1656. gColorTextureHACK.pushBack(colorI);
  1657. r_tri->texture = gTextureOffset + gColorTextureHACK.size();
  1658. render->loadTexture(Texture::generateColorTexture(color, 32, 32),
  1659. 32, 32,
  1660. r_tri->texture);
  1661. #ifdef DEBUG_COLOR_TEXTURE_GEN
  1662. printf("Color 0x%02x%02x%02x%02x | 0x%08xto texture[%i]?\n",
  1663. color[0], color[1], color[2], color[3], colorI,
  1664. gColorTextureHACK.size());
  1665. #endif
  1666. }
  1667. else
  1668. {
  1669. //printf("Color already loaded %i -> 0x%08x\n",
  1670. // gColorTextureHACK.getCurrentIndex(),
  1671. // gColorTextureHACK.current());
  1672. r_tri->texture = gTextureOffset + gColorTextureHACK.getCurrentIndex();
  1673. }
  1674. //r_tri->texture = white; // White texture
  1675. }
  1676. void OpenRaider::processModel(int index)
  1677. {
  1678. int i, j, count, texture, white;
  1679. int vertexIndices[6];
  1680. float st[12];
  1681. float color[4];
  1682. unsigned short transparency;
  1683. texture_tri_t *r_tri;
  1684. // Assert common sense
  1685. if (index < 0 || !m_tombraider.isMeshValid(index))
  1686. {
  1687. //! \fixme allow sparse lists with matching ids instead?
  1688. gWorld.addMesh(NULL); // Filler, to make meshes array ids align
  1689. printf("x");
  1690. fflush(stdout);
  1691. return;
  1692. }
  1693. // WHITE texture id
  1694. white = 0;
  1695. model_mesh_t *mesh = new model_mesh_t;
  1696. // Mongoose 2002.08.30, Testing support for 'shootable' models ( traceable )
  1697. m_tombraider.getMeshCollisionInfo(index, mesh->center, &mesh->radius);
  1698. //! \fixme Arrays don't work either =)
  1699. // Mesh geometery, colors, etc
  1700. m_tombraider.getMeshVertexArrays(index,
  1701. &mesh->vertexCount, &mesh->vertices,
  1702. &mesh->normalCount, &mesh->normals,
  1703. &mesh->colorCount, &mesh->colors);
  1704. // Textured Triangles
  1705. count = m_tombraider.getMeshTexturedTriangleCount(index);
  1706. mesh->texturedTriangles.reserve(count); // little faster
  1707. for (i = 0; i < count; ++i)
  1708. {
  1709. r_tri = new texture_tri_t;
  1710. m_tombraider.getMeshTexturedTriangle(index, i,
  1711. r_tri->index,
  1712. r_tri->st,
  1713. &r_tri->texture,
  1714. &r_tri->transparency);
  1715. r_tri->texture += m_texOffset;
  1716. // Add to face vector
  1717. mesh->texturedTriangles.pushBack(r_tri);
  1718. }
  1719. // Coloured Triangles
  1720. count = m_tombraider.getMeshColoredTriangleCount(index);
  1721. mesh->coloredTriangles.reserve(count); // little faster
  1722. for (i = 0; i < count; i++)
  1723. {
  1724. r_tri = new texture_tri_t;
  1725. m_tombraider.getMeshColoredTriangle(index, i,
  1726. r_tri->index,
  1727. color);
  1728. r_tri->st[0] = color[0];
  1729. r_tri->st[1] = color[1];
  1730. r_tri->st[2] = color[2];
  1731. r_tri->st[3] = color[3];
  1732. r_tri->st[4] = 1.0;
  1733. r_tri->st[5] = 1.0;
  1734. #ifdef EXPERIMENTAL
  1735. setupTextureColor(r_tri, &m_render, color);
  1736. #else
  1737. r_tri->texture = white; // White texture
  1738. #endif
  1739. r_tri->transparency = 0;
  1740. // Add to face vector
  1741. mesh->coloredTriangles.pushBack(r_tri);
  1742. }
  1743. // Textured Rectangles
  1744. count = m_tombraider.getMeshTexturedRectangleCount(index);
  1745. mesh->texturedRectangles.reserve(count*2); // little faster
  1746. for (i = 0; i < count; ++i)
  1747. {
  1748. m_tombraider.getMeshTexturedRectangle(index, i,
  1749. vertexIndices,
  1750. st,
  1751. &texture,
  1752. &transparency);
  1753. r_tri = new texture_tri_t;
  1754. for (j = 0; j < 3; ++j)
  1755. r_tri->index[j] = vertexIndices[j];
  1756. for (j = 0; j < 6; ++j)
  1757. r_tri->st[j] = st[j];
  1758. r_tri->texture = texture + m_texOffset;
  1759. r_tri->transparency = transparency;
  1760. // Add to face vector
  1761. mesh->texturedRectangles.pushBack(r_tri);
  1762. r_tri = new texture_tri_t;
  1763. for (j = 3; j < 6; ++j)
  1764. r_tri->index[j-3] = vertexIndices[j];
  1765. for (j = 6; j < 12; ++j)
  1766. r_tri->st[j-6] = st[j];
  1767. r_tri->texture = texture + m_texOffset;
  1768. r_tri->transparency = transparency;
  1769. // Add to face vector
  1770. mesh->texturedRectangles.pushBack(r_tri);
  1771. }
  1772. // Coloured Rectangles
  1773. count = m_tombraider.getMeshColoredRectangleCount(index);
  1774. mesh->coloredRectangles.reserve(count*2); // little faster
  1775. for (i = 0; i < count; ++i)
  1776. {
  1777. m_tombraider.getMeshColoredRectangle(index, i,
  1778. vertexIndices,
  1779. color);
  1780. r_tri = new texture_tri_t;
  1781. for (j = 0; j < 3; ++j)
  1782. r_tri->index[j] = vertexIndices[j];
  1783. //for (j = 0; j < 6; ++j)
  1784. // r_tri->st[j] = st[j];
  1785. r_tri->st[0] = color[0];
  1786. r_tri->st[1] = color[1];
  1787. r_tri->st[2] = color[2];
  1788. r_tri->st[3] = color[3];
  1789. r_tri->st[4] = 1.0;
  1790. r_tri->st[5] = 1.0;
  1791. #ifdef EXPERIMENTAL
  1792. //for (j = 6; j < 12; ++j)
  1793. // r_tri->st[j-6] = st[j];
  1794. setupTextureColor(r_tri, &m_render, color);
  1795. #else
  1796. r_tri->texture = white; // White texture
  1797. #endif
  1798. r_tri->transparency = 0;
  1799. // Add to face vector
  1800. mesh->coloredRectangles.pushBack(r_tri);
  1801. r_tri = new texture_tri_t;
  1802. for (j = 3; j < 6; ++j)
  1803. r_tri->index[j-3] = vertexIndices[j];
  1804. //for (j = 6; j < 12; ++j)
  1805. // r_tri->st[j-6] = st[j];
  1806. r_tri->st[0] = color[0];
  1807. r_tri->st[1] = color[1];
  1808. r_tri->st[2] = color[2];
  1809. r_tri->st[3] = color[3];
  1810. r_tri->st[4] = 1.0;
  1811. r_tri->st[5] = 1.0;
  1812. #ifdef EXPERIMENTAL
  1813. setupTextureColor(r_tri, &m_render, color);
  1814. #else
  1815. r_tri->texture = white; // White texture
  1816. #endif
  1817. r_tri->transparency = 0;
  1818. // Add to face vector
  1819. mesh->coloredRectangles.pushBack(r_tri);
  1820. }
  1821. // Sort faces by texture
  1822. mesh->texturedTriangles.qSort(compareFaceTextureId);
  1823. mesh->coloredTriangles.qSort(compareFaceTextureId);
  1824. mesh->texturedRectangles.qSort(compareFaceTextureId);
  1825. mesh->coloredRectangles.qSort(compareFaceTextureId);
  1826. gWorld.addMesh(mesh);
  1827. printf(".");
  1828. fflush(stdout);
  1829. }
  1830. #define MATRIX_TRANSFORMS
  1831. void OpenRaider::processRoom(int index)
  1832. {
  1833. unsigned int i, j, count;
  1834. room_mesh_t *r_mesh = NULL;
  1835. RenderRoom *rRoom = NULL;
  1836. #ifdef MATRIX_TRANSFORMS
  1837. //matrix_t transform;
  1838. Matrix transform;
  1839. #endif
  1840. if (!m_tombraider.isRoomValid(index))
  1841. {
  1842. openraider_error("OpenRaider::RoomSetup", "Invalid room index",
  1843. __FILE__, __LINE__);
  1844. print(false, "WARNING: Handling invalid vertex array in room");
  1845. gWorld.addRoom(0x0);
  1846. m_render.addRoom(0x0);
  1847. printf("x");
  1848. fflush(stdout);
  1849. return;
  1850. }
  1851. rRoom = new RenderRoom();
  1852. r_mesh = new room_mesh_t;
  1853. r_mesh->id = index;
  1854. m_tombraider.getRoomInfo(index, &r_mesh->flags, r_mesh->pos,
  1855. r_mesh->bbox_min, r_mesh->bbox_max);
  1856. // Adjust positioning for OR world coord translation
  1857. r_mesh->bbox_min[0] += r_mesh->pos[0];
  1858. r_mesh->bbox_max[0] += r_mesh->pos[0];
  1859. r_mesh->bbox_min[2] += r_mesh->pos[2];
  1860. r_mesh->bbox_max[2] += r_mesh->pos[2];
  1861. // Mongoose 2002.04.03, Setup 3d transform
  1862. #ifdef MATRIX_TRANSFORMS
  1863. //mtkMatrixIdentity(transform);
  1864. //mtkMatrixTranslate(transform,
  1865. // r_mesh->pos[0], r_mesh->pos[1], r_mesh->pos[2]);
  1866. transform.setIdentity();
  1867. transform.translate(r_mesh->pos);
  1868. #else
  1869. #endif
  1870. // Setup portals
  1871. float portalVertices[12];
  1872. count = m_tombraider.getRoomPortalCount(index);
  1873. //! \fixme OR wrongly uses a cached adj room list for rendering vis
  1874. r_mesh->adjacentRooms.reserve(count + 1);
  1875. r_mesh->adjacentRooms.setError(-1);
  1876. // Current room is always first
  1877. r_mesh->adjacentRooms.pushBack(index);
  1878. for (i = 0; i < count; ++i)
  1879. {
  1880. portal_t *portal = new portal_t;
  1881. m_tombraider.getRoomPortal(index, i,
  1882. &portal->adjoining_room, portal->normal,
  1883. portalVertices);
  1884. for (j = 0; j < 4; ++j)
  1885. {
  1886. portal->vertices[j][0] = portalVertices[j*3];
  1887. portal->vertices[j][1] = portalVertices[j*3+1];
  1888. portal->vertices[j][2] = portalVertices[j*3+2];
  1889. // Relative coors in vis portals
  1890. #ifdef MATRIX_TRANSFORMS
  1891. //mtkMatrixTransform(transform, portal->vertices[j]);
  1892. transform.multiply3v(portal->vertices[j], portal->vertices[j]);
  1893. #else
  1894. portal->vertices[j][0] += r_mesh->pos[0];
  1895. portal->vertices[j][1] += r_mesh->pos[1];
  1896. portal->vertices[j][2] += r_mesh->pos[2];
  1897. #endif
  1898. }
  1899. r_mesh->adjacentRooms.pushBack(portal->adjoining_room);
  1900. r_mesh->portals.pushBack(portal);
  1901. }
  1902. // Physics/gameplay use /////////////////////////////
  1903. //! \fixme Use more of sector structure, boxes, and floordata
  1904. // List of sectors in this room
  1905. unsigned int sectorFlags;
  1906. int floorDataIndex, boxIndex, roomBelow, roomAbove;
  1907. count = m_tombraider.getRoomSectorCount(index, &r_mesh->numZSectors,
  1908. &r_mesh->numXSectors);
  1909. r_mesh->sectors.reserve(count);
  1910. r_mesh->sectors.setError(0x0);
  1911. for (i = 0; i < count; ++i)
  1912. {
  1913. sector_t *sector = new sector_t;
  1914. m_tombraider.getRoomSector(index, i, &sectorFlags,
  1915. &sector->ceiling, &sector->floor,
  1916. &floorDataIndex, &boxIndex, &roomBelow,
  1917. &roomAbove);
  1918. if (sectorFlags & tombraiderSector_wall)
  1919. {
  1920. sector->wall = true;
  1921. }
  1922. else
  1923. {
  1924. sector->wall = false;
  1925. }
  1926. r_mesh->sectors.pushBack(sector);
  1927. }
  1928. // Setup collision boxes ( Should use sectors, but this is a test )
  1929. count = m_tombraider.getRoomBoxCount(index);
  1930. r_mesh->boxes.reserve(count);
  1931. r_mesh->boxes.setError(0x0);
  1932. //! fixme Only to be done only on room[0]? I don't think so...
  1933. for (i = 0; !index && i < count; ++i)
  1934. {
  1935. box_t *box = new box_t;
  1936. m_tombraider.getRoomBox(index, i,
  1937. box->a.pos, box->b.pos, box->c.pos, box->d.pos);
  1938. r_mesh->boxes.pushBack(box);
  1939. }
  1940. // Setup room lights /////////////////////////////////////
  1941. unsigned int lightFlags, lightType;
  1942. count = m_tombraider.getRoomLightCount(index);
  1943. rRoom->lights.reserve(count);
  1944. rRoom->lights.setError(0x0);
  1945. for (i = 0; i < count; ++i)
  1946. {
  1947. Light *light = new Light();
  1948. m_tombraider.getRoomLight(index, i,
  1949. light->mPos, light->mColor, light->mDir,
  1950. &light->mAtt, &light->mCutoff,
  1951. &lightType, &lightFlags);
  1952. switch (lightType)
  1953. {
  1954. case tombraiderLight_typeDirectional:
  1955. light->mType = Light::typeDirectional;
  1956. break;
  1957. case tombraiderLight_typeSpot:
  1958. light->mType = Light::typeSpot;
  1959. break;
  1960. case tombraiderLight_typePoint:
  1961. default:
  1962. light->mType = Light::typePoint;
  1963. }
  1964. rRoom->lights.pushBack(light);
  1965. }
  1966. // Room geometery //////////////////////////////////
  1967. //#define EXPERIMENTAL_UNFIFIED_ROOM_GEOMETERY
  1968. #ifdef EXPERIMENTAL_UNFIFIED_ROOM_GEOMETERY
  1969. unsigned int vertexCount, normalCount, colorCount, triCount;
  1970. vec_t *vertexArray;
  1971. vec_t *normalArray;
  1972. vec_t *colorArray;
  1973. unsigned int *indices, *flags;
  1974. float *texCoords;
  1975. int *textures;
  1976. m_tombraider.getRoomVertexArrays(index,
  1977. &vertexCount, &vertexArray,
  1978. &normalCount, &normalArray,
  1979. &colorCount, &colorArray);
  1980. rRoom->mesh.bufferVertexArray(vertexCount, (vec_t *)vertexArray);
  1981. rRoom->mesh.bufferNormalArray(normalCount, (vec_t *)normalArray);
  1982. rRoom->mesh.bufferColorArray(vertexCount, (vec_t *)colorArray);
  1983. m_tombraider.getRoomTriangles(index, m_texOffset,
  1984. &triCount, &indices, &texCoords, &textures,
  1985. &flags);
  1986. rRoom->mesh.bufferTriangles(triCount, indices, texCoords, textures, flags);
  1987. #else
  1988. float rgba[4];
  1989. float xyz[3];
  1990. count = m_tombraider.getRoomVertexCount(index);
  1991. rRoom->mesh.allocateVertices(count);
  1992. rRoom->mesh.allocateNormals(0); // count
  1993. rRoom->mesh.allocateColors(count);
  1994. for (i = 0; i < count; ++i)
  1995. {
  1996. m_tombraider.getRoomVertex(index, i, xyz, rgba);
  1997. rRoom->mesh.setVertex(i, xyz[0], xyz[1], xyz[2]);
  1998. rRoom->mesh.setColor(i, rgba);
  1999. }
  2000. // Mongoose 2002.06.09, Setup allocation of meshes and polygons
  2001. // Counters ( Textured polygon lists are allocated per texture)
  2002. // ( Textures are mapped to these meshes )
  2003. int triangle_counter[TextureLimit];
  2004. int triangle_counter_alpha[TextureLimit];
  2005. int rectangle_counter[TextureLimit];
  2006. int rectangle_counter_alpha[TextureLimit];
  2007. int tris_mesh_map[TextureLimit];
  2008. int rect_mesh_map[TextureLimit];
  2009. for (i = 0; i < TextureLimit; ++i)
  2010. {
  2011. triangle_counter[i] = 0;
  2012. triangle_counter_alpha[i] = 0;
  2013. rectangle_counter[i] = 0;
  2014. rectangle_counter_alpha[i] = 0;
  2015. tris_mesh_map[i] = -1;
  2016. rect_mesh_map[i] = -1;
  2017. }
  2018. unsigned int numTris = 0;
  2019. unsigned int numQuads = 0;
  2020. int texture;
  2021. unsigned int r, t, q, v, flags;
  2022. unsigned int indices[4];
  2023. float texCoords[8];
  2024. count = m_tombraider.getRoomTriangleCount(index);
  2025. // Mongoose 2002.08.15, Presort by alpha and texture and setup mapping
  2026. for (t = 0; t < count; ++t)
  2027. {
  2028. m_tombraider.getRoomTriangle(index, t,
  2029. indices, texCoords, &texture, &flags);
  2030. texture += m_texOffset;
  2031. if (texture > (int)TextureLimit)
  2032. {
  2033. print(true, "Handling bad room[%i].tris[%i].texture = %i",
  2034. index, t, texture);
  2035. texture = TextureLimit - 1;
  2036. }
  2037. // Counters set up polygon allocation
  2038. if (flags & tombraiderFace_Alpha ||
  2039. flags & tombraiderFace_PartialAlpha)
  2040. {
  2041. triangle_counter_alpha[texture] += 1;
  2042. }
  2043. else
  2044. {
  2045. triangle_counter[texture] += 1;
  2046. }
  2047. // Counter sets up texture id to mesh id mapping
  2048. if (tris_mesh_map[texture] == -1)
  2049. {
  2050. tris_mesh_map[texture] = ++numTris;
  2051. }
  2052. }
  2053. count = m_tombraider.getRoomRectangleCount(index);
  2054. for (r = 0; r < count; ++r)
  2055. {
  2056. m_tombraider.getRoomRectangle(index, r,
  2057. indices, texCoords, &texture, &flags);
  2058. texture += m_texOffset;
  2059. if (texture > (int)TextureLimit)
  2060. {
  2061. print(true, "Handling bad room[%i].quad[%i].texture = %i",
  2062. index, r, texture);
  2063. texture = TextureLimit - 1;
  2064. }
  2065. if (flags & tombraiderFace_Alpha ||
  2066. flags & tombraiderFace_PartialAlpha)
  2067. {
  2068. rectangle_counter_alpha[texture] += 1;
  2069. }
  2070. else
  2071. {
  2072. rectangle_counter[texture] += 1;
  2073. }
  2074. if (rect_mesh_map[texture] == -1)
  2075. {
  2076. rect_mesh_map[texture] = ++numQuads;
  2077. }
  2078. }
  2079. // Allocate indexed polygon meshes
  2080. rRoom->mesh.allocateTriangles(numTris);
  2081. rRoom->mesh.allocateRectangles(numQuads);
  2082. for (i = 0, j = 0; i < TextureLimit; ++i)
  2083. {
  2084. if (tris_mesh_map[i] > 0)
  2085. {
  2086. j = tris_mesh_map[i] - 1;
  2087. t = triangle_counter[i];
  2088. rRoom->mesh.mTris[j].texture = i;
  2089. #ifdef MULTITEXTURE
  2090. rRoom->mesh.mTris[j].bumpmap = gMapTex2Bump[i];
  2091. #endif
  2092. rRoom->mesh.mTris[j].cnum_triangles = 0;
  2093. rRoom->mesh.mTris[j].num_triangles = 0;
  2094. rRoom->mesh.mTris[j].cnum_alpha_triangles = 0;
  2095. rRoom->mesh.mTris[j].num_alpha_triangles = 0;
  2096. rRoom->mesh.mTris[j].triangles = 0x0;
  2097. rRoom->mesh.mTris[j].alpha_triangles = 0x0;
  2098. rRoom->mesh.mTris[j].texcoors = 0x0;
  2099. rRoom->mesh.mTris[j].texcoors2 = 0x0;
  2100. if (t > 0)
  2101. {
  2102. rRoom->mesh.mTris[j].num_triangles = t;
  2103. rRoom->mesh.mTris[j].triangles = new unsigned int[t*3];
  2104. rRoom->mesh.mTris[j].num_texcoors = t * 3;
  2105. rRoom->mesh.mTris[j].texcoors = new vec2_t[t * 3];
  2106. }
  2107. t = triangle_counter_alpha[i];
  2108. if (t > 0)
  2109. {
  2110. rRoom->mesh.mTris[j].num_alpha_triangles = t;
  2111. rRoom->mesh.mTris[j].alpha_triangles = new unsigned int[t*3];
  2112. rRoom->mesh.mTris[j].num_texcoors2 = t * 3;
  2113. rRoom->mesh.mTris[j].texcoors2 = new vec2_t[t * 3];
  2114. }
  2115. }
  2116. ///////////////////////////////////////////
  2117. if (rect_mesh_map[i] > 0)
  2118. {
  2119. j = rect_mesh_map[i] - 1;
  2120. r = rectangle_counter[i];
  2121. rRoom->mesh.mQuads[j].texture = i;
  2122. #ifdef MULTITEXTURE
  2123. rRoom->mesh.mQuads[j].bumpmap = gMapTex2Bump[i];
  2124. #endif
  2125. rRoom->mesh.mQuads[j].cnum_quads = 0;
  2126. rRoom->mesh.mQuads[j].num_quads = 0;
  2127. rRoom->mesh.mQuads[j].cnum_alpha_quads = 0;
  2128. rRoom->mesh.mQuads[j].num_alpha_quads = 0;
  2129. rRoom->mesh.mQuads[j].quads = 0x0;
  2130. rRoom->mesh.mQuads[j].alpha_quads = 0x0;
  2131. rRoom->mesh.mQuads[j].texcoors = 0x0;
  2132. rRoom->mesh.mQuads[j].texcoors2 = 0x0;
  2133. if (r > 0)
  2134. {
  2135. rRoom->mesh.mQuads[j].num_quads = r;
  2136. rRoom->mesh.mQuads[j].quads = new unsigned int[r*4];
  2137. rRoom->mesh.mQuads[j].num_texcoors = r * 4;
  2138. rRoom->mesh.mQuads[j].texcoors = new vec2_t[r * 4];
  2139. }
  2140. r = rectangle_counter_alpha[i];
  2141. if (r > 0)
  2142. {
  2143. rRoom->mesh.mQuads[j].num_alpha_quads = r;
  2144. rRoom->mesh.mQuads[j].alpha_quads = new unsigned int[r*4];
  2145. rRoom->mesh.mQuads[j].num_texcoors2 = r * 4;
  2146. rRoom->mesh.mQuads[j].texcoors2 = new vec2_t[r * 4];
  2147. }
  2148. }
  2149. }
  2150. // Generate textured triangles
  2151. count = m_tombraider.getRoomTriangleCount(index);
  2152. for (t = 0; t < count; ++t)
  2153. {
  2154. m_tombraider.getRoomTriangle(index, t,
  2155. indices, texCoords, &texture, &flags);
  2156. // Adjust texture id using m_texOffset to map into
  2157. // correct textures
  2158. texture += m_texOffset;
  2159. j = tris_mesh_map[texture] - 1;
  2160. // Setup per vertex
  2161. for (i = 0; i < 3; ++i)
  2162. {
  2163. // Get vertex index {(0, a), (1, b), (2, c)}
  2164. v = indices[i];
  2165. if ((flags & tombraiderFace_Alpha ||
  2166. flags & tombraiderFace_PartialAlpha) &&
  2167. rRoom->mesh.mTris[j].num_alpha_triangles > 0)
  2168. {
  2169. q = rRoom->mesh.mTris[j].cnum_alpha_triangles*3+i;
  2170. rRoom->mesh.mTris[j].alpha_triangles[q] = v;
  2171. rRoom->mesh.mTris[j].texcoors2[q][0] = texCoords[i*2];
  2172. rRoom->mesh.mTris[j].texcoors2[q][1] = texCoords[i*2+1];
  2173. }
  2174. else if (rRoom->mesh.mTris[j].num_triangles > 0)
  2175. {
  2176. q = rRoom->mesh.mTris[j].cnum_triangles*3+i;
  2177. rRoom->mesh.mTris[j].triangles[q] = v;
  2178. rRoom->mesh.mTris[j].texcoors[q][0] = texCoords[i*2];
  2179. rRoom->mesh.mTris[j].texcoors[q][1] = texCoords[i*2+1];
  2180. }
  2181. // Partial alpha hack
  2182. if (flags & tombraiderFace_PartialAlpha)
  2183. {
  2184. //rRoom->mesh.colors[v].rgba[3] = 0.45;
  2185. }
  2186. }
  2187. if (flags & tombraiderFace_Alpha ||
  2188. flags & tombraiderFace_PartialAlpha)
  2189. {
  2190. rRoom->mesh.mTris[j].cnum_alpha_triangles++;
  2191. }
  2192. else
  2193. {
  2194. rRoom->mesh.mTris[j].cnum_triangles++;
  2195. }
  2196. }
  2197. // Generate textured quads
  2198. count = m_tombraider.getRoomRectangleCount(index);
  2199. for (r = 0; r < count; ++r)
  2200. {
  2201. m_tombraider.getRoomRectangle(index, r,
  2202. indices, texCoords, &texture, &flags);
  2203. // Adjust texture id using m_texOffset to map into
  2204. // correct textures
  2205. texture += m_texOffset;
  2206. if (texture > (int)TextureLimit)
  2207. {
  2208. texture = TextureLimit - 1;
  2209. }
  2210. j = rect_mesh_map[texture] - 1;
  2211. if (rRoom->mesh.mQuads[j].num_quads <= 0 &&
  2212. rRoom->mesh.mQuads[j].num_alpha_quads <= 0)
  2213. continue;
  2214. // Setup per vertex
  2215. for (i = 0; i < 4; ++i)
  2216. {
  2217. // Get vertex index {(0, a), (1, b), (2, c), (3, d)}
  2218. v = indices[i];
  2219. if ((flags & tombraiderFace_Alpha ||
  2220. flags & tombraiderFace_PartialAlpha) &&
  2221. rRoom->mesh.mQuads[j].num_alpha_quads > 0)
  2222. {
  2223. q = rRoom->mesh.mQuads[j].cnum_alpha_quads*4+i;
  2224. rRoom->mesh.mQuads[j].alpha_quads[q] = v;
  2225. rRoom->mesh.mQuads[j].texcoors2[q][0] = texCoords[i*2];
  2226. rRoom->mesh.mQuads[j].texcoors2[q][1] = texCoords[i*2+1];
  2227. }
  2228. else if (rRoom->mesh.mQuads[j].num_quads > 0)
  2229. {
  2230. q = rRoom->mesh.mQuads[j].cnum_quads*4+i;
  2231. rRoom->mesh.mQuads[j].quads[q] = v;
  2232. rRoom->mesh.mQuads[j].texcoors[q][0] = texCoords[i*2];
  2233. rRoom->mesh.mQuads[j].texcoors[q][1] = texCoords[i*2+1];
  2234. }
  2235. // Partial alpha hack
  2236. if (flags & tombraiderFace_PartialAlpha)
  2237. {
  2238. //rRoom->mesh.colors[v].rgba[3] = 0.45;
  2239. }
  2240. }
  2241. if (flags & tombraiderFace_Alpha ||
  2242. flags & tombraiderFace_PartialAlpha)
  2243. {
  2244. rRoom->mesh.mQuads[j].cnum_alpha_quads++;
  2245. }
  2246. else
  2247. {
  2248. rRoom->mesh.mQuads[j].cnum_quads++;
  2249. }
  2250. }
  2251. #endif
  2252. // Room models
  2253. count = m_tombraider.getRoomModelCount(index);
  2254. r_mesh->models.reserve(count);
  2255. r_mesh->models.setError(0x0);
  2256. for (i = 0; i < count; ++i)
  2257. {
  2258. static_model_t *model = new static_model_t;
  2259. m_tombraider.getRoomModel(index, i,
  2260. &model->index, model->pos, &model->yaw);
  2261. r_mesh->models.pushBack(model);
  2262. }
  2263. // Room sprites
  2264. float spriteVertices[12];
  2265. float spriteTexCoords[8];
  2266. count = m_tombraider.getRoomSpriteCount(index);
  2267. r_mesh->sprites.reserve(count);
  2268. r_mesh->sprites.setError(0x0);
  2269. for (i = 0; i < count; ++i)
  2270. {
  2271. sprite_t *sprite = new sprite_t;
  2272. m_tombraider.getRoomSprite(index, i,
  2273. 10.0f, &sprite->texture, sprite->pos,
  2274. spriteVertices, spriteTexCoords);
  2275. sprite->texture += m_texOffset; // OpenRaider preloads some textures
  2276. sprite->vertex[0].pos[0] = spriteVertices[0];
  2277. sprite->vertex[0].pos[1] = spriteVertices[1];
  2278. sprite->vertex[0].pos[2] = spriteVertices[2];
  2279. sprite->vertex[1].pos[0] = spriteVertices[3];
  2280. sprite->vertex[1].pos[1] = spriteVertices[4];
  2281. sprite->vertex[1].pos[2] = spriteVertices[5];
  2282. sprite->vertex[2].pos[0] = spriteVertices[6];
  2283. sprite->vertex[2].pos[1] = spriteVertices[7];
  2284. sprite->vertex[2].pos[2] = spriteVertices[8];
  2285. sprite->vertex[3].pos[0] = spriteVertices[9];
  2286. sprite->vertex[3].pos[1] = spriteVertices[10];
  2287. sprite->vertex[3].pos[2] = spriteVertices[11];
  2288. sprite->texel[0].st[0] = spriteTexCoords[0];
  2289. sprite->texel[0].st[1] = spriteTexCoords[1];
  2290. sprite->texel[1].st[0] = spriteTexCoords[2];
  2291. sprite->texel[1].st[1] = spriteTexCoords[3];
  2292. sprite->texel[2].st[0] = spriteTexCoords[4];
  2293. sprite->texel[2].st[1] = spriteTexCoords[5];
  2294. sprite->texel[3].st[0] = spriteTexCoords[6];
  2295. sprite->texel[3].st[1] = spriteTexCoords[7];
  2296. r_mesh->sprites.pushBack(sprite);
  2297. }
  2298. gWorld.addRoom(r_mesh);
  2299. rRoom->room = r_mesh;
  2300. m_render.addRoom(rRoom);
  2301. printf(".");
  2302. fflush(stdout);
  2303. }
  2304. void OpenRaider::consoleCommand(char *cmd)
  2305. {
  2306. if (!cmd || !cmd[0])
  2307. return;
  2308. // Mongoose 2003.05.31, Strip off decp console prefix
  2309. if (cmd[0] == '>')
  2310. {
  2311. rc_command(">", cmd);
  2312. }
  2313. if (rc_command("quit", cmd))
  2314. {
  2315. shutdown(0);
  2316. }
  2317. else if (rc_command("port", cmd))
  2318. {
  2319. Network &net = *Network::Instance();
  2320. net.setPort(atoi(cmd));
  2321. }
  2322. else if (rc_command("killserver", cmd))
  2323. {
  2324. Network &net = *Network::Instance();
  2325. print(true, "Stopping network server...");
  2326. net.killServerThread();
  2327. }
  2328. else if (rc_command("server", cmd))
  2329. {
  2330. Network &net = *Network::Instance();
  2331. print(true, "Starting network server...");
  2332. net.spawnServerThread();
  2333. }
  2334. else if (rc_command("disconnect", cmd))
  2335. {
  2336. Network &net = *Network::Instance();
  2337. print(true, "Diconnecting...");
  2338. net.killClientThread();
  2339. }
  2340. else if (rc_command("connect", cmd))
  2341. {
  2342. Network &net = *Network::Instance();
  2343. print(true, "Connecting to %s...", cmd);
  2344. net.setRemoteHost(cmd);
  2345. net.spawnClientThread();
  2346. }
  2347. else if (rc_command("fly", cmd))
  2348. {
  2349. if (LARA)
  2350. {
  2351. LARA->moveType = worldMoveType_fly;
  2352. }
  2353. print(false, "World clipping is [SPACEY]");
  2354. }
  2355. else if (rc_command("walk", cmd))
  2356. {
  2357. if (LARA)
  2358. {
  2359. LARA->moveType = worldMoveType_walk;
  2360. }
  2361. print(false, "World clipping is [ON]");
  2362. }
  2363. else if (rc_command("ghost", cmd))
  2364. {
  2365. if (LARA)
  2366. {
  2367. LARA->moveType = worldMoveType_noClipping;
  2368. }
  2369. print(false, "World clipping is [OFF]");
  2370. }
  2371. else if (rc_command("mem", cmd))
  2372. {
  2373. #ifdef DEBUG_MEMORY
  2374. if (rc_command("usage", cmd))
  2375. {
  2376. display_memory_usage();
  2377. }
  2378. else if (rc_command("report", cmd))
  2379. {
  2380. dump_memory_report();
  2381. }
  2382. #else
  2383. printf("This build isn't DEBUG_MEMORY enabled\n");
  2384. #endif
  2385. }
  2386. else if (rc_command("loadlevel", cmd))
  2387. {
  2388. loadLevel(cmd);
  2389. }
  2390. else if (rc_command("play", cmd))
  2391. {
  2392. if (m_flags & OpenRaider_EnableSound)
  2393. {
  2394. mSound.play(atoi(cmd));
  2395. }
  2396. }
  2397. else if (rc_command("sensitivity.x", cmd))
  2398. {
  2399. m_mouseX = static_cast<float>(atof(cmd));
  2400. }
  2401. else if (rc_command("sensitivity.y", cmd))
  2402. {
  2403. m_mouseY = static_cast<float>(atof(cmd));
  2404. }
  2405. else if (rc_command("r_pigtails", cmd))
  2406. {
  2407. if (gLaraModel)
  2408. {
  2409. gLaraModel->pigtails = atoi(cmd);
  2410. if (gLaraModel->pigtails)
  2411. {
  2412. gLaraModel->ponyOff -= 20;
  2413. gLaraModel->ponytail[1] -= 32;
  2414. }
  2415. else
  2416. {
  2417. gLaraModel->ponyOff += 20;
  2418. gLaraModel->ponytail[1] += 32;
  2419. }
  2420. }
  2421. }
  2422. else if (rc_command("r_ponyangle", cmd))
  2423. {
  2424. if (gLaraModel)
  2425. {
  2426. gLaraModel->ponytailAngle = atoi(cmd);
  2427. }
  2428. }
  2429. else if (rc_command("r_ponyx", cmd))
  2430. {
  2431. if (gLaraModel)
  2432. {
  2433. gLaraModel->ponytail[0] = atoi(cmd);
  2434. }
  2435. }
  2436. else if (rc_command("r_ponyy", cmd))
  2437. {
  2438. if (gLaraModel)
  2439. {
  2440. gLaraModel->ponytail[1] = atoi(cmd);
  2441. }
  2442. }
  2443. else if (rc_command("r_ponyz", cmd))
  2444. {
  2445. if (gLaraModel)
  2446. {
  2447. gLaraModel->ponytail[2] = atoi(cmd);
  2448. }
  2449. }
  2450. else if (rc_command("r_animate", cmd))
  2451. {
  2452. if (atoi(cmd))
  2453. {
  2454. m_render.setFlags(Render::fAnimateAllModels);
  2455. print(false, "Animating all models");
  2456. }
  2457. else
  2458. {
  2459. m_render.clearFlags(Render::fAnimateAllModels);
  2460. print(false, "No longer animating all models");
  2461. }
  2462. }
  2463. else if (rc_command("r_ponytail", cmd))
  2464. {
  2465. if (atoi(cmd))
  2466. {
  2467. m_render.setFlags(Render::fRenderPonytail);
  2468. print(false, "Rendering ponytail");
  2469. }
  2470. else
  2471. {
  2472. m_render.clearFlags(Render::fRenderPonytail);
  2473. print(false, "No longer rendering ponytail");
  2474. }
  2475. }
  2476. else if (rc_command("r_light", cmd))
  2477. {
  2478. if (atoi(cmd))
  2479. {
  2480. m_render.setFlags(Render::fGL_Lights);
  2481. }
  2482. else
  2483. {
  2484. m_render.clearFlags(Render::fGL_Lights);
  2485. }
  2486. }
  2487. else if (rc_command("hop", cmd))
  2488. {
  2489. if (atoi(cmd))
  2490. {
  2491. gWorld.setFlag(World::fEnableHopping);
  2492. print(true, "Room hopping is on");
  2493. }
  2494. else
  2495. {
  2496. gWorld.clearFlag(World::fEnableHopping);
  2497. print(true, "Room hopping is off");
  2498. }
  2499. }
  2500. else if (rc_command("r_fog", cmd))
  2501. {
  2502. if (atoi(cmd))
  2503. {
  2504. m_render.setFlags(Render::fFog);
  2505. }
  2506. else
  2507. {
  2508. m_render.clearFlags(Render::fFog);
  2509. }
  2510. }
  2511. else if (rc_command("wireframe", cmd))
  2512. {
  2513. m_render.setMode(Render::modeWireframe);
  2514. print(false, "wireframe mode");
  2515. }
  2516. else if (rc_command("solid", cmd))
  2517. {
  2518. m_render.setMode(Render::modeSolid);
  2519. print(false, "solid mode");
  2520. }
  2521. else if (rc_command("texture", cmd))
  2522. {
  2523. m_render.setMode(Render::modeTexture);
  2524. print(false, "texture mode");
  2525. }
  2526. else if (rc_command("vertexlight", cmd))
  2527. {
  2528. m_render.setMode(Render::modeVertexLight);
  2529. print(false, "vertexlight mode");
  2530. }
  2531. else if (rc_command("titlescreen", cmd))
  2532. {
  2533. m_render.setMode(Render::modeLoadScreen);
  2534. print(false, "titlescreen mode");
  2535. }
  2536. else if (rc_command("r_viewmodel", cmd))
  2537. {
  2538. if (LARA)
  2539. {
  2540. SkeletalModel *smdl = static_cast<SkeletalModel *>(LARA->tmpHook);
  2541. skeletal_model_t *mdl = gWorld.getModel(atoi(cmd));
  2542. if (smdl)
  2543. {
  2544. smdl->setModel(mdl);
  2545. }
  2546. }
  2547. //m_render.ViewModel(LARA, atoi(cmd));
  2548. }
  2549. else if (rc_command("r_oneroom", cmd))
  2550. {
  2551. if (atoi(cmd))
  2552. {
  2553. m_render.setFlags(Render::fOneRoom);
  2554. }
  2555. else
  2556. {
  2557. m_render.clearFlags(Render::fOneRoom);
  2558. }
  2559. }
  2560. else if (rc_command("r_allrooms", cmd))
  2561. {
  2562. if (atoi(cmd))
  2563. {
  2564. m_render.setFlags(Render::fAllRooms);
  2565. }
  2566. else
  2567. {
  2568. m_render.clearFlags(Render::fAllRooms);
  2569. }
  2570. print(true, "Rendering all rooms [%s]", (atoi(cmd) == 0) ? "off" : "on");
  2571. }
  2572. else if (rc_command("r_sprite", cmd))
  2573. {
  2574. if (atoi(cmd))
  2575. {
  2576. m_render.setFlags(Render::fSprites);
  2577. }
  2578. else
  2579. {
  2580. m_render.clearFlags(Render::fSprites);
  2581. }
  2582. }
  2583. else if (rc_command("r_roommodel", cmd))
  2584. {
  2585. if (atoi(cmd))
  2586. {
  2587. m_render.setFlags(Render::fRoomModels);
  2588. }
  2589. else
  2590. {
  2591. m_render.clearFlags(Render::fRoomModels);
  2592. }
  2593. }
  2594. else if (rc_command("r_entmodel", cmd))
  2595. {
  2596. if (atoi(cmd))
  2597. {
  2598. m_render.setFlags(Render::fEntityModels);
  2599. }
  2600. else
  2601. {
  2602. m_render.clearFlags(Render::fEntityModels);
  2603. }
  2604. }
  2605. else if (rc_command("r_particle", cmd))
  2606. {
  2607. if (atoi(cmd))
  2608. {
  2609. m_render.setFlags(Render::fParticles);
  2610. }
  2611. else
  2612. {
  2613. m_render.clearFlags(Render::fParticles);
  2614. }
  2615. }
  2616. else if (rc_command("r_vis", cmd))
  2617. {
  2618. if (atoi(cmd))
  2619. {
  2620. m_render.setFlags(Render::fUsePortals);
  2621. }
  2622. else
  2623. {
  2624. m_render.clearFlags(Render::fUsePortals);
  2625. }
  2626. }
  2627. else if (rc_command("r_upf", cmd))
  2628. {
  2629. if (atoi(cmd))
  2630. {
  2631. m_render.setFlags(Render::fUpdateRoomListPerFrame);
  2632. }
  2633. else
  2634. {
  2635. m_render.clearFlags(Render::fUpdateRoomListPerFrame);
  2636. }
  2637. }
  2638. else if (rc_command("r_portal", cmd))
  2639. {
  2640. if (atoi(cmd))
  2641. {
  2642. m_render.setFlags(Render::fPortals);
  2643. }
  2644. else
  2645. {
  2646. m_render.clearFlags(Render::fPortals);
  2647. }
  2648. }
  2649. else if (rc_command("r_vmodel", cmd))
  2650. {
  2651. if (atoi(cmd))
  2652. {
  2653. m_render.setFlags(Render::fViewModel);
  2654. }
  2655. else
  2656. {
  2657. m_render.clearFlags(Render::fViewModel);
  2658. }
  2659. }
  2660. else if (rc_command("r_ralpha", cmd))
  2661. {
  2662. if (atoi(cmd))
  2663. {
  2664. m_render.setFlags(Render::fRoomAlpha);
  2665. }
  2666. else
  2667. {
  2668. m_render.clearFlags(Render::fRoomAlpha);
  2669. }
  2670. }
  2671. else if (rc_command("resize", cmd))
  2672. {
  2673. if (rc_command("xga", cmd))
  2674. {
  2675. resize(1024, 768);
  2676. m_render.Update(1024, 768);
  2677. }
  2678. else if (rc_command("svga", cmd))
  2679. {
  2680. resize(800, 600);
  2681. m_render.Update(800, 600);
  2682. }
  2683. else if (rc_command("vga", cmd))
  2684. {
  2685. resize(640, 460);
  2686. m_render.Update(640, 460);
  2687. }
  2688. }
  2689. else if (rc_command("sshot", cmd))
  2690. {
  2691. char sfilename[1024];
  2692. char *tmp = fullPath("~/.OpenRaider/sshots/", '/');
  2693. /* Not very pretty, but gets it done */
  2694. snprintf(sfilename, 1024, "%s%s", tmp, VERSION);
  2695. delete [] tmp;
  2696. m_render.screenShot(sfilename);
  2697. print(false, "Took screenshot");
  2698. }
  2699. else if (rc_command("fullscreen", cmd))
  2700. {
  2701. toggleFullscreen();
  2702. }
  2703. else if (rc_command("showfps", cmd))
  2704. {
  2705. m_flags |= OpenRaider_ShowFPS;
  2706. if (!atoi(cmd))
  2707. {
  2708. m_flags ^= OpenRaider_ShowFPS;
  2709. }
  2710. }
  2711. else if (rc_command("bind", cmd))
  2712. {
  2713. if (rc_command("+forward", cmd))
  2714. {
  2715. bindKeyCommand("+forward", atoi(cmd), OpenRaiderKey_forward);
  2716. }
  2717. else if (rc_command("+console", cmd))
  2718. {
  2719. bindKeyCommand("+console", atoi(cmd), OpenRaiderKey_console);
  2720. }
  2721. else if (rc_command("+backward", cmd))
  2722. {
  2723. bindKeyCommand("+backward", atoi(cmd), OpenRaiderKey_backward);
  2724. }
  2725. else if (rc_command("+jump", cmd))
  2726. {
  2727. bindKeyCommand("+jump", atoi(cmd), OpenRaiderKey_jump);
  2728. }
  2729. else if (rc_command("+crouch", cmd))
  2730. {
  2731. bindKeyCommand("+crouch", atoi(cmd), OpenRaiderKey_crouch);
  2732. }
  2733. else if (rc_command("+right", cmd))
  2734. {
  2735. bindKeyCommand("+right", atoi(cmd), OpenRaiderKey_right);
  2736. }
  2737. else if (rc_command("+left", cmd))
  2738. {
  2739. bindKeyCommand("+left", atoi(cmd), OpenRaiderKey_left);
  2740. }
  2741. else if (rc_command("+attack", cmd))
  2742. {
  2743. bindKeyCommand("+attack", atoi(cmd), OpenRaiderKey_attack);
  2744. }
  2745. }
  2746. else if (rc_command("set", cmd))
  2747. {
  2748. if (rc_command("mousegrab", cmd))
  2749. {
  2750. setGrabMouse(atoi(cmd));
  2751. print(true, "Mouse grabbing [%s]", atoi(cmd) ? "on" : "off");
  2752. }
  2753. }
  2754. else if (rc_command("stat", cmd))
  2755. {
  2756. if (rc_command("fps", cmd))
  2757. {
  2758. m_flags ^= OpenRaider_ShowFPS;
  2759. }
  2760. else if (rc_command("pos", cmd))
  2761. {
  2762. if (LARA)
  2763. {
  2764. print(true, "Room %2i Pos %.0f %.0f %.0f Yaw %.0f Pitch %.0f",
  2765. LARA->room,
  2766. LARA->pos[0], LARA->pos[1], LARA->pos[2],
  2767. HEL_RAD_TO_DEG(LARA->angles[1]),
  2768. HEL_RAD_TO_DEG(LARA->angles[2]));
  2769. }
  2770. }
  2771. else if (rc_command("room", cmd))
  2772. {
  2773. if (rc_command("flags", cmd))
  2774. {
  2775. if (LARA)
  2776. {
  2777. print(true, "Room[%i] flags: 0x%x",
  2778. LARA->room,
  2779. gWorld.getRoomInfo(LARA->room));
  2780. }
  2781. }
  2782. }
  2783. }
  2784. }
  2785. void OpenRaider::handleCommand(char *cmd, unsigned int mode)
  2786. {
  2787. bool b;
  2788. int i;
  2789. // So we can use switch stmt, translate the Ids
  2790. mode = mMode[mode];
  2791. switch (mode)
  2792. {
  2793. case 0: // [Video.OpenGL]
  2794. if (rc_command("Width", cmd))
  2795. {
  2796. m_width = atoi(cmd);
  2797. }
  2798. else if (rc_command("Height", cmd))
  2799. {
  2800. m_height = atoi(cmd);
  2801. }
  2802. else if (rc_command("FastCard", cmd))
  2803. {
  2804. rc_get_bool(cmd, &m_fastCard);
  2805. }
  2806. else if (rc_command("FullScreen", cmd))
  2807. {
  2808. rc_get_bool(cmd, &b);
  2809. m_flags |= OpenRaider_FullScreen;
  2810. if (!b)
  2811. {
  2812. m_flags ^= OpenRaider_FullScreen;
  2813. }
  2814. }
  2815. else if (rc_command("Font", cmd))
  2816. {
  2817. gFontFilename = fullPath(cmd, '/');
  2818. }
  2819. else if (rc_command("Driver", cmd))
  2820. {
  2821. if (cmd[0])
  2822. {
  2823. setDriverGL(cmd);
  2824. }
  2825. }
  2826. else
  2827. {
  2828. printf("Command> [Video.OpenGL] Unknown command '%s'\n", cmd);
  2829. }
  2830. break;
  2831. case 1: // [Audio.OpenAL]
  2832. if (rc_command("Enable", cmd))
  2833. {
  2834. rc_get_bool(cmd, &b);
  2835. m_flags |= OpenRaider_EnableSound;
  2836. if (!b)
  2837. {
  2838. m_flags ^= OpenRaider_EnableSound;
  2839. }
  2840. }
  2841. else
  2842. {
  2843. printf("Command> [Audio.OpenAL] Unknown command '%s'\n", cmd);
  2844. }
  2845. break;
  2846. case 2: // [OpenRaider.Engine]
  2847. if (rc_command("PakDir", cmd))
  2848. {
  2849. if (m_pakDir)
  2850. {
  2851. delete [] m_pakDir;
  2852. }
  2853. m_pakDir = fullPath(cmd, '/');
  2854. }
  2855. else if (rc_command("HomeDir", cmd))
  2856. {
  2857. if (m_homeDir)
  2858. {
  2859. delete [] m_homeDir;
  2860. }
  2861. i = strlen(cmd);
  2862. m_homeDir = fullPath(cmd, '/');
  2863. }
  2864. else if (rc_command("AudioDir", cmd))
  2865. {
  2866. if (m_audioDir)
  2867. {
  2868. delete [] m_audioDir;
  2869. }
  2870. m_audioDir = fullPath(cmd, '/');
  2871. }
  2872. else if (rc_command("MapDebug", cmd))
  2873. {
  2874. rc_get_bool(cmd, &b);
  2875. m_flags |= OpenRaider_DebugMap;
  2876. if (!b)
  2877. {
  2878. m_flags ^= OpenRaider_DebugMap;
  2879. }
  2880. }
  2881. else if (rc_command("Map", cmd))
  2882. {
  2883. if (cmd[0])
  2884. {
  2885. char *fullPathMap;
  2886. fullPathMap = bufferString("%s%s", m_pakDir, cmd);
  2887. if (m_tombraider.checkMime(fullPathMap) == 0)
  2888. {
  2889. printf("Validated pak: '%s'\n",
  2890. fullPathMap);
  2891. delete [] fullPathMap;
  2892. /* Just load relative filename */
  2893. mMapList.pushBack(bufferString("%s", cmd));
  2894. }
  2895. else
  2896. {
  2897. printf("ERROR: pak file '%s' not found or invalid\n",
  2898. fullPathMap);
  2899. delete [] fullPathMap;
  2900. }
  2901. }
  2902. }
  2903. // Mongoose 2001.12.31, Added music list back
  2904. else if (rc_command("Music", cmd))
  2905. {
  2906. if (cmd[0])
  2907. {
  2908. char *music;
  2909. i = strlen(cmd);
  2910. music = new char[i+1];
  2911. strncpy(music, cmd, i);
  2912. music[i] = 0;
  2913. mMusicList.pushBack(music);
  2914. }
  2915. }
  2916. else if (rc_command("DisplayFPS", cmd))
  2917. {
  2918. rc_get_bool(cmd, &b);
  2919. m_flags |= OpenRaider_ShowFPS;
  2920. if (!b)
  2921. {
  2922. m_flags ^= OpenRaider_ShowFPS;
  2923. }
  2924. }
  2925. else if (rc_command("ModelDebug", cmd))
  2926. {
  2927. rc_get_bool(cmd, &b);
  2928. m_flags |= OpenRaider_DebugModel;
  2929. if (!b)
  2930. {
  2931. m_flags ^= OpenRaider_DebugModel;
  2932. }
  2933. }
  2934. else if (rc_command("DumpTexture", cmd))
  2935. {
  2936. rc_get_bool(cmd, &b);
  2937. m_flags |= OpenRaider_DumpTexture;
  2938. if (!b)
  2939. {
  2940. m_flags ^= OpenRaider_DumpTexture;
  2941. }
  2942. }
  2943. else
  2944. {
  2945. printf("Command> [OpenRaider.Engine] Unknown command '%s'\n", cmd);
  2946. }
  2947. break;
  2948. case 3: // [Input.Mouse]
  2949. if (rc_command("SensitivityX", cmd))
  2950. {
  2951. m_mouseX = static_cast<float>(atof(cmd));
  2952. }
  2953. else if (rc_command("SensitivityY", cmd))
  2954. {
  2955. m_mouseY = static_cast<float>(atof(cmd));
  2956. }
  2957. else
  2958. {
  2959. printf("Command> [Input.Mouse] Unknown command '%s'\n", cmd);
  2960. }
  2961. break;
  2962. case 4:
  2963. if (cmd[1] == '.')
  2964. {
  2965. cmd[0] = '>';
  2966. cmd[1] = ' ';
  2967. for (i = strlen(cmd) - 1; i >= 0; --i)
  2968. {
  2969. if (cmd[i] == '.' || cmd[i] == '=')
  2970. {
  2971. cmd[i] = ' ';
  2972. }
  2973. }
  2974. }
  2975. consoleCommand(cmd);
  2976. break;
  2977. case 5:
  2978. if (rc_command("Enable", cmd))
  2979. {
  2980. rc_get_bool(cmd, &gStartServer);
  2981. }
  2982. else if (rc_command("Port", cmd))
  2983. {
  2984. if (gStartServer)
  2985. {
  2986. Network &net = *Network::Instance();
  2987. int port = atoi(cmd);
  2988. net.setPort(port);
  2989. print(true, "Starting network server on port %i...", port);
  2990. net.spawnServerThread();
  2991. }
  2992. }
  2993. else
  2994. {
  2995. printf("Command> [Network.Server] '%s' not implemented\n", cmd);
  2996. }
  2997. break;
  2998. }
  2999. }
  3000. int main(int argc, char *argv[]) {
  3001. if (argc != 1) {
  3002. printf("Usage:\n\t%s\n", argv[0]);
  3003. return 1;
  3004. }
  3005. OpenRaider *game = OpenRaider::Instance();
  3006. atexit(killOpenRaiderSingleton);
  3007. game->start();
  3008. return 0;
  3009. }