game.cpp 148 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666
  1. ////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2016 RWS Inc, All Rights Reserved
  4. //
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of version 2 of the GNU General Public License as published by
  7. // the Free Software Foundation
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License along
  15. // with this program; if not, write to the Free Software Foundation, Inc.,
  16. // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. //
  18. // game.cpp
  19. // Project: Postal
  20. //
  21. // This module deals with the high-level aspects of setting up and running the
  22. // game.
  23. //
  24. // History:
  25. // 11/19/96 MJR Started.
  26. // 01/15/96 MJR Added GamePath()
  27. //
  28. // 01/28/97 JMI Added calls to MenuTrans API (a PalTran).
  29. //
  30. // 01/30/97 MJR Fixed bug in CorrectifyPath() that accidentally truncated
  31. // the passed-in string.
  32. //
  33. // 01/31/97 JMI Now uses FullPath() to get full path for filespecs.
  34. //
  35. // 01/31/97 JMI Added g_resmgrGame for specific to actual game data.
  36. //
  37. // 01/31/97 JMI Number of title loops done on start up is now gotten from
  38. // INI file.
  39. //
  40. // 02/02/97 JMI Now calls StartTitle() and EndTitle() after launching an
  41. // edit session, a user game, or a demo game.
  42. //
  43. // 02/02/97 JMI Now, before exiting, makes sure all samples are done
  44. // playing.
  45. //
  46. // 02/03/97 JMI Changed DoMenu() called to new DoMenuInput() and
  47. // DoMenuOutput() calls.
  48. //
  49. // 02/03/97 JMI Now sets the base path for g_resmgrGame, Shell, & Menus.
  50. //
  51. // 02/04/97 JMI Added functions to get and set the gamma level.
  52. //
  53. // 02/04/97 JMI Changed LoadDib() call to Load() (which now supports
  54. // loading of DIBs).
  55. //
  56. // 02/13/97 JMI Removed game level alpha XRay stuff (now setup in CHood).
  57. //
  58. // 02/18/97 JMI Now opens SampleMaster's SAK file.
  59. //
  60. // 03/06/97 JMI Made PalTranOn() and PalTranOff() extern instead of inline.
  61. //
  62. // 03/17/97 JMI Made PalTranOff() abortable via a keypress. Put in the
  63. // shape of the code for aborting via a key in PalTranOn() but
  64. // there is currently no interface to MenuTrans to allow this
  65. // type of abort (or jump).
  66. //
  67. // 03/28/97 JMI Un#if 0'd the SavePrefs() call.
  68. //
  69. // 03/28/97 JMI Now stores and resets the current dir.
  70. //
  71. // 04/11/97 JMI Added Game_Menu_Demo() externally callable function to
  72. // start the demo.
  73. //
  74. // 04/14/97 JMI Now specifies which image for StartTitle() to start with.
  75. //
  76. // 04/14/97 JMI Missed a spot in the previous change.
  77. //
  78. // 04/16/97 MJR Added simple test of file paths. Also changed it so that
  79. // preferences are always saved, even if the game encounters
  80. // an error beforehand.
  81. //
  82. // Fixed the simple test of file paths to actual use the
  83. // file path!!!
  84. //
  85. // 04/17/97 MJR Put the test for the CD path back in.
  86. //
  87. // 04/22/97 JMI No longer uses chdir().
  88. //
  89. // 04/22/97 JMI Now sets g_GameSettings.m_sServer and
  90. // g_GameSettings.m_sClient to 0 before running the demo.
  91. //
  92. // 04/24/97 JMI Currently, aborting the PalTranOff() is a bad idea, so
  93. // we don't allow it (until we support EndMenuTrans(TRUE) ).
  94. //
  95. // 05/06/97 JMI Now uses a Gamma multiplier to define a line instead of
  96. // the exponent defining the curve.
  97. //
  98. // 05/07/97 JMI Now uses a Gamma exponent and uses the 'Gamma Val' as a
  99. // constant multiplier:
  100. // MappedColorVal = (colorVal ^ GAMMA_EXPONENT) * GammaVal
  101. //
  102. // 05/21/97 JMI Added a resource manager for resources that are not SAKed.
  103. //
  104. // 05/23/97 JMI Added a CloseSaks() which closes and purges the SAKs.
  105. // This solves the problem with the ASSERTions about the logic
  106. // vars (the RResmgr's were declared at file scope as were the
  107. // CLogTabVars so it was just a matter of order of destruction
  108. // that dictated whether the ASSERTions occurred).
  109. //
  110. // 06/03/97 JMI Commented out error message for read-only prefs file.
  111. //
  112. // 06/03/97 JMI Now uses g_GameSettings members for demo timeouts instead
  113. // of the DEMO_TIMEOUT macro.
  114. //
  115. // 06/04/97 JMI Added conditionally compiled message instead of editor
  116. // when editor is requested from the menu.
  117. // Also, now StartTitle() calls within GameLoop() request the
  118. // last title page.
  119. //
  120. // 06/04/97 JMI Added AbortAllSamples() call when exitting.
  121. //
  122. // 06/04/97 JMI Added MUST_BE_ON_CD, EDITOR_DISABLED, and CHECK_FOR_COOKIE
  123. // conditional compilation macros and added a check for a
  124. // specific U32 in the COOKIE file.
  125. //
  126. // 06/12/97 MJR Reworked the callbacks so that the game-specific code now
  127. // resides in this module rather than the menu module.
  128. // Reworked the entire core loop so it passes all the
  129. // required parameters to Play() instead of using globals.
  130. // Moved all the demo and network setup stuff out of play.cpp
  131. // and into here.
  132. //
  133. // 06/15/97 MJR More changes in terms of how Play() is called and what
  134. // this module does to get ready for calling it.
  135. // Also changed calls to rspDoSystem() into calls to Update().
  136. //
  137. // Added required calls to DeleteInputDemoData().
  138. //
  139. // 06/15/97 MJR Made use of new-and-improved input demo interface.
  140. // (But demo mode is still not working right...had to check
  141. // it in though to go along with changes in other files).
  142. //
  143. // 06/16/97 MJR Fixed stupid error that caused demo record not to work.
  144. //
  145. // 06/16/97 JMI Now passes destination buffer in StartMenu() call.
  146. //
  147. // 06/16/97 JMI Added g_fontSmall.
  148. //
  149. // MJR Added make-a-demo-movie stuff for debugging demo mode
  150. // problems.
  151. //
  152. // JMI Now, based on INI setting g_GameSettings.m_sTrickySystemQuit,
  153. // we en/disable system quit status flags.
  154. //
  155. // JMI Changed "res/fonts/SmallSmash.fnt" to "res/fonts/SmSmash.fnt"
  156. //
  157. // 06/17/97 MJR Modified CorrectifyBasePath() so it will change any
  158. // relative paths into absolute paths. This, in turn, fixed
  159. // a problem whereby the loading of demo files failed because
  160. // it assumed the name of the demo file would begin with the
  161. // HD path. This assumption normally worked, but failed when
  162. // the HD path was a relative path.
  163. //
  164. // 06/18/97 MJR Added SeedRandom() and GetRandom().
  165. //
  166. // MJR Fixed bug in CorrectifyBasePath(). Now handles empty
  167. // paths (it ignores them).
  168. //
  169. // 06/24/97 JMI Added SynchLog() debug mechanism. See function comment for
  170. // functionality details. See macros in game.h for usage via
  171. // macro(s).
  172. //
  173. // 06/24/97 JMI Undefined CHECK_FOR_COOKIE.
  174. //
  175. // JMI Changed usage of strcmp to rspStricmp in GetRandom() and
  176. // SynchLog().
  177. //
  178. // 06/30/97 MJR Changed client and server objects to use new/delete instead
  179. // of having them live on the stack. They are very large,
  180. // and caused local (stack) data to exceed 32k on the mac.
  181. //
  182. // 07/03/97 JMI Converted calls to rspOpen/SaveBox() to new parm
  183. // conventions.
  184. //
  185. // 07/03/97 JMI Added Game_ControlsMenu() and actions for editting input
  186. // settings for keys and mouse.
  187. //
  188. // 07/05/97 MJR Changed to RSP_BLACK_INDEX instead of 0.
  189. //
  190. // 07/06/97 JMI Changed ACTION_EDIT_MOUSE_SETTINGS and
  191. // ACTION_EDIT_KEY_SETTINGS to ACTION_EDIT_INPUT_SETTINGS.
  192. // Also, now only one function is called no matter which type
  193. // of input settings are to be editted, EditInputSettings().
  194. //
  195. // 07/11/97 BRH Finished up the expiration date checking for the
  196. // Registry. Still need to add something for the Mac
  197. // version.
  198. //
  199. // 07/11/97 BRH Fixed typos causing the latest date not to be written to
  200. // the registry.
  201. //
  202. // 07/13/97 JMI Now sets the appropriate base path or opens the approriate
  203. // SAK dependent on the current audio mode.
  204. //
  205. // 07/13/97 JMI Added Game_StartChallengeGame() and an action for the
  206. // various challenge game types.
  207. //
  208. // 07/14/97 BRH Changed calls to Play() to pass the challenge mode flag.
  209. // Fixed value in Get Registry in case of an error on
  210. // decrypt.
  211. //
  212. // 07/16/97 BRH Made changes to the Mac expiration date code to normalize
  213. // the time to 1970 like the rest of the machines so that
  214. // the dates can be stored in 1 format.
  215. //
  216. // 07/16/97 JMI Now uses real time when pausing on the titel screens (in-
  217. // stead of 'Load Loops').
  218. //
  219. // 07/17/97 BRH Finished debugging the Mac Prefs file expiration date code.
  220. //
  221. // MJR Fixed sample rate problem on the mac.
  222. //
  223. // BRH Put in the real expiration date and a comment for the
  224. // real release date, and updated the current release date
  225. // to today so that we can still test it.
  226. //
  227. // 07/18/97 BRH Added game load and save functions so that the player's
  228. // game can be saved and loaded. Also added a global
  229. // stockpile object used to transfer loaded/saved info to/from
  230. // the CDude's stockpile.
  231. //
  232. // 07/19/97 MJR Fixed bug where demo mode would start if user cancelled out
  233. // of a dialog after spending a long time there.
  234. // Also changed so demo mode will not start if app is in BG.
  235. //
  236. // 07/23/97 BRH Changed calls to Title functions to use the new array of
  237. // title screen durations.
  238. //
  239. // 07/23/97 BRH Changed expiration date display for PC back to asctime(gmtime())
  240. // since ctime didn't work correctly.
  241. //
  242. // 07/26/97 JMI Added g_fontPostal to replace g_fontSmall. Got rid of
  243. // g_fontSmall.
  244. //
  245. // 07/28/97 JMI Now displays a different message for non-marketing
  246. // releases.
  247. //
  248. // 08/01/97 BRH Cycles through 3 demos rather than just 1. Also added
  249. // open dialog for playing demos so that the user can choose
  250. // which demo to play.
  251. //
  252. // 08/02/97 JMI Now initializes the default RGuiItem font to g_fontBig in-
  253. // stead of relying on everybody who uses it to invidually set
  254. // it.
  255. //
  256. // 08/05/97 JMI Added Game_AudioOptionsChoice().
  257. //
  258. // 08/06/97 MJR Added socket startup/shutdown.
  259. //
  260. // 08/08/97 MJR Background and foreground callbacks are now installed here
  261. // so they are in effect for the entire app.
  262. //
  263. // 08/08/97 JMI Now passes parameter to start musak in title to
  264. // StartTitle().
  265. //
  266. // 08/12/97 JMI m_szDemoFile was FullPath()'ed twice so the demo wouldn't
  267. // load. Fixed.
  268. //
  269. // 08/12/97 JMI Added SubPathOpenBox() (see proto for details) and
  270. // FullPathCustom().
  271. //
  272. // 08/13/97 MJR We now pass difficulty level to Play().
  273. //
  274. // 08/13/97 JMI No longer remembers the previous demo file you recorded as
  275. // a patch to either improper usage or problem in
  276. // SubPathOpenBox().
  277. //
  278. // 08/14/97 JMI Added prealm parameter to Game_Save/LoadPlayersGame() so
  279. // they can query/modify realm settings/flags.
  280. // Also, changed questionable access on m_action in
  281. // Game_SavePlayersGame() and Game_LoadPlayersGame() which
  282. // were casting &m_action to a (short*) even though ACTION is
  283. // a 32 bit value. May not have worked on the Mac although it
  284. // would for values less than 32767 and greater than 0 on the
  285. // little endian PC.
  286. // Also, added an ACTION_LOAD_GAME so that could be part of
  287. // the main loop with the intent of making the loaded
  288. // difficulty a local variable but this turned out to be error
  289. // prone with keeping this value up to date with the proper
  290. // value (either the INI difficulty or the loaded difficulty)
  291. // and it was annoying to determine which one to use on each
  292. // Play() call so I made a function that returns the
  293. // appropriate value (but only once...ick). If you feel like
  294. // it try it the other way, but be aware that on any iteration
  295. // g_GameSettings.m_sDifficulty may change via a menu.
  296. // Also, had to add a flag so one action can lead to another.
  297. // The main loop used to reset the action after every action
  298. // making it impossible for one action to lead to another.
  299. //
  300. // 08/15/97 JRD Added a function to control brightness/contrast in place
  301. // of gamma. This was because the gamma function could not
  302. // recreate a "normal palette."
  303. //
  304. // 08/15/97 JRD Attempted to hook both brightness and contrast changes to
  305. // the old gamma slider with a crude algorithm.
  306. //
  307. // 08/18/97 JMI Now allows ALT-F4 to drop you out of the title sequence.
  308. //
  309. // 08/18/97 JMI In both debug and release modes, we were setting the do
  310. // system mode to sleep. But we'd only restore it in release
  311. // mode which basically left us in sleep mode in debug mode.
  312. // Now restores it to TOLERATEOS in debug mode.
  313. //
  314. // 08/19/97 BRH Uses the game settings m_sNumAvailableDemos to control the
  315. // demo loop rather than the set macro NUM_DEMO_FILES
  316. //
  317. // MJR Added support for new MP flags.
  318. //
  319. // 08/20/97 JMI Modified Game_LoadPlayersGame() to receive the ptr to the
  320. // action to fill with the Player's saved action so the
  321. // function would not have to use the global m_action.
  322. // Also, Moved Game_LoadPlayersGame() proto into game.cpp b/c
  323. // it now takes an ACTION* but ACTION is defined in game.cpp
  324. // and since no one currently calls this function externally,
  325. // what the hell.
  326. //
  327. // 08/21/97 BRH Changed the game sak to be loaded from the GAME_PATH_GAME
  328. // directory, getting it ready for the installation process.
  329. // Also changed the demos to HD path to make it easier for
  330. // people to load their own demos. Changed the sound res
  331. // manager path to the GAME_PATH_SOUND for installation.
  332. //
  333. // 08/21/97 JMI Changed call to Update() to UpdateSystem() and occurrences
  334. // of rspUpdateDisplay() to UpdateDisplay().
  335. //
  336. // 08/22/97 JMI Changed occurrences of UpdateDisplay() back to
  337. // rspUpdateDisplay(). Now that we are using the lock
  338. // functions correctly, we don't need them.
  339. // Also, removed rspLockBuffer() and rspUnlockBuffer() that
  340. // were used to encapsulate the entire app in a lock.
  341. //
  342. // 08/22/97 BRH Changed the cookie position for use with a new smaller
  343. // res.sak file since we now have Direct X and more sound,
  344. // we needed more space on the CD.
  345. //
  346. // 08/23/97 JMI Now gets callbacks from the initialization and killing of
  347. // the main menu and stops any playing title/main-menu musak
  348. // when the main menu is killed.
  349. //
  350. // 08/24/97 JMI Added a timeout to the abortion of playing samples just in
  351. // case there's a bug or a sound driver problem (no need to
  352. // to taunt infinite loopage).
  353. //
  354. // 08/25/97 JMI Now sets the sound quality based on the current mode.
  355. // Also, now, if there's no sak dir and no sak for the current
  356. // audio mode, complains.
  357. //
  358. // 08/26/97 BRH Merged in changes from a branched game.cpp which has the
  359. // special cases for the ending demo level of the game.
  360. // Added a function GameEndingSequence which is called
  361. // when the global flag that indicates the player has won is
  362. // set. It plays the demo file and then calls the
  363. // Title_EndingSequence.
  364. //
  365. // 08/26/97 JMI Now any menu can be restore in the GameCore() loop instead
  366. // of just the main menu by setting pmenuStart to point at the
  367. // menu to be next started.
  368. //
  369. // 08/30/97 BRH Fixed the base path for the logic table resource manager
  370. // to load from the CD rather than the HD path.
  371. //
  372. // 09/02/97 JMI Added a new option to the demo actions which plays one of
  373. // the factory preset demos.
  374. // Also, in the case that there were no factory preset demos
  375. // and the auto play demo kicked in, it would ASSERT that
  376. // the filename was "".
  377. //
  378. // 09/02/97 JMI Now more deluxe control for regarding the starting of the
  379. // next menu after an action in GameCore().
  380. //
  381. // 09/03/97 BRH Changed the g_resmgrRes logic table manager to load
  382. // from the HD path rather than the CD path, so that it
  383. // is easier to modify, add or change logics, especially
  384. // if we have to patch them in the future.
  385. //
  386. // 09/04/97 JMI Now Game_ControlsMenu() only sets a new action if one is
  387. // not already set. This keeps the controls menu from setting
  388. // the action when it is launched from Play.cpp instead of
  389. // here.
  390. //
  391. // 09/05/97 PPL In CorrectifyBasePath, made sure we allocate the buffer for the
  392. // full path instead of letting getcwd allocate it for us. It's
  393. // not that we won't let it -- it's just that the Mac implementation
  394. // will not do it automatically as the PC implementation would.
  395. //
  396. // 09/05/97 PPL Per Bill's directions, we will use the path from the CD for
  397. // the challenge level.
  398. //
  399. // 09/05/97 MJR A few changes to use the new network stuff.
  400. //
  401. // 09/06/97 MJR Now inits and kills the network problem GUI.
  402. //
  403. // 09/10/97 BRH Had to change the res.sak in order to fit the MPlayer
  404. // and Heat setups on the CD, so I also had to change the
  405. // cookie.
  406. //
  407. // 09/10/97 MJR Now properly converts paths from system to rspix format
  408. // on the start single player and start challenge level
  409. // dialogs. This bug only showed up on the Mac.
  410. //
  411. // 09/11/97 BRH Added different cookie and file position for
  412. // COMP_USA_VERSION only.
  413. //
  414. // 09/11/97 JMI Removed old code for PLAY_SPECIFIC_LEVEL_ONLY macro which
  415. // no longer exists.
  416. //
  417. // 09/12/97 MJR Now more code doesn't compile when the editor is disabled,
  418. // in an attempt to make it harder to hack.
  419. //
  420. // 09/18/97 JMI Added expiration message for
  421. // EXPIRATION_MSG_POSTAL_LAUNCH_WEEKEND compile condition.
  422. //
  423. // JMI Changed 'will expire on' to 'expires' so it sounds better
  424. // both past/future/etc tense wise and the 'on' didn't work
  425. // well with the format "Mon Sep 22 23:59:00" which contains
  426. // no 'at'.
  427. //
  428. // 09/24/97 BRH For LOCALE != US version, took out the school yard demo
  429. // from the ending sequence.
  430. //
  431. // 09/29/97 JMI Now will try to load any samples SAK in the case that there
  432. // is no audio mode, the specified mode's samples SAK is not
  433. // present, and there's no 'NoSak' dir specification.
  434. // If absolutely no samples SAK is found, a message is
  435. // displayed that does not mention whether or not your
  436. // hardware supports the specified mode.
  437. //
  438. // 10/01/97 BRH Changed res.sak location so it would fit on the disk.
  439. //
  440. // 10/08/97 JMI Made the synchronization logs opened and closed via one
  441. // set of two functions to make it easier to use them in
  442. // different scenarios.
  443. // Also, the logs should now only use the filename (excluding
  444. // the path) to make it possible for Mac vs. PC logs to be
  445. // compared.
  446. //
  447. // 10/10/97 JMI Updated Game_ControlsMenu() to handle joystick option
  448. // when ALLOW_JOYSTICK is defined.
  449. //
  450. // 10/14/97 JMI Made SynchLog()'s expr parameter a double instead of an int
  451. // for more accuracy.
  452. //
  453. // 10/15/97 JMI Changed the %g used to fprintf() SynchLog() expr values
  454. // to %0.8f b/c the Mac and PC formats differed slightly. The
  455. // Mac one often contains as many as four zeroes after the
  456. // last significant decimal, sometimes less, and most of the
  457. // time none. The PC never seems to put trailing zeroes.
  458. //
  459. // 10/16/97 JMI Now mac demo basename is mdefault.dmo (instead of
  460. // default.dmo) since the game plays slightly differently on
  461. // the Mac than on the PC.
  462. //
  463. // 10/22/97 JMI In the call to Play() for recording demos, FullPathHD() was
  464. // being called on the passed in path but now that
  465. // CRealm::Open() tries all the deluxe path stuff we don't
  466. // need that (and cannot use it b/c Open() does a conversion
  467. // to system path format as does FullPathHD() ).
  468. //
  469. // 11/20/97 JMI Added cooperative flag to Play() calls.
  470. //
  471. // 12/01/97 BRH Added flags for Add On levels to play calls, and
  472. // added ACTION_PLAY_ADDON as one of the options.
  473. //
  474. // 12/04/97 BRH Added Single player menu option to play add on levels
  475. // or original levels.
  476. //
  477. // 01/05/98 JMI Now uses RMix::GetMode() to get sound quality. Also, now
  478. // uses the source bits per sample (instead of the device
  479. // setting) to determine which sample resources to use.
  480. // Note that the quality is still set based on the device bits
  481. // per sample though so that the volumes could be tuned
  482. // accordingly in SampleMaster's ms_asQualityCategoryAdjustors.
  483. //
  484. // 01/21/98 JMI Now the foreground and background callbacks use a value,
  485. // INVALID_CURSOR_SHOW_LEVEL, to flag whether or not they're
  486. // in the background so they know which callbacks to ignore
  487. // and which one's to use. I think that before (under DirectX)
  488. // we were getting more than one Backcall per Forecall and/or
  489. // vice-versa.
  490. //
  491. // 03/05/98 BRH Now attempts to identify the PostalSD disc when the game
  492. // starts to clear up any confusion about which CD must be
  493. // in the drive to play the Add on Pack. The Add on Pack
  494. // requires the original PostalCD in the drive, and it reads
  495. // the original res.sak file from that disc. The PostalSD
  496. // disc also has a res.sak file on it, but its not the one
  497. // we want, and if someone tried to play the game with the
  498. // PostalSD disc in the drive rather than the PostalCD, then
  499. // the copy protection would flag it as invalid. So an
  500. // additional check is now made and if the PostalSD disc
  501. // is in the drive, it will display a message to tell them
  502. // to put the PostalCD in the drive.
  503. //
  504. // 06/04/98 BRH Removed the demo timeout check from the SPAWN build since
  505. // the spawn version doesn't have any demo files available
  506. // it shouldn't try to run them when the user is idle. It
  507. // was attempting to do so, and when it can't find a demo
  508. // file, a message box pops up which would cause much
  509. // confusion to the user.
  510. //
  511. // 07/08/98 JMI Removed extra Close() in checking for add on CD. Also,
  512. // moved close that was in incorrect spot in check for add on
  513. // CD. These were only annoying in Debug mode when it is
  514. // flagged.
  515. //
  516. // 09/27/99 JMI Changed to allow ending sequence only in any locale
  517. // satisfying the CompilerOptions macro VIOLENT_LOCALE.
  518. //
  519. // 10/07/99 JMI Conditional remove Add On start menu option when in
  520. // SUPER_POSTAL target.
  521. //
  522. // 10/11/99 JMI Changed the audio sak name to be RATEjBITS instead of
  523. // RATE_BITS to differentiate it from Orig & SD Postal sound
  524. // saks.
  525. //
  526. // 02/04/00 MJR Added dialog box to prompt for original CD if
  527. // PROMPT_FOR_ORIGINAL_CD is defined.
  528. //
  529. // MJR Changed so that it now checks the HD first and the VD
  530. // second when trying to load the shell sak. This
  531. // was done to fix problems with the Japan Add On.
  532. //
  533. // 03/30/00 MJR Changed to new macro for controlling the test for the
  534. // original Postal CD to differentiate it from the test for
  535. // a CD-ROM drive.
  536. //
  537. // Added AUDIO_SAK_SEPARATOR_CHAR to make it easy to control
  538. // the separater character in audio sak filenames.
  539. //
  540. // 04/01/00 MJR Fixed bug with AUDIO_SAK_SEPARATOR_CHAR.
  541. // Changed so in debug mode, it continues even if not on CD.
  542. //
  543. // 04/03/00 MJR Modified MUST_BE_ON_CD code so that the test is bypassed if
  544. // the CD path is to our development server. This allows
  545. // for easier testing.
  546. //
  547. // 06/24/01 MJR Got rid of CompUSA cookie variation. Also cleaned up a
  548. // few other macros.
  549. //
  550. ////////////////////////////////////////////////////////////////////////////////
  551. #define DWORD U32
  552. #include "RSPiX.h"
  553. #include <time.h>
  554. #ifdef WIN32
  555. #include <direct.h>
  556. #else
  557. #include <unistd.h>
  558. #endif
  559. #include "WishPiX/Menu/menu.h"
  560. #include "WishPiX/Prefs/prefs.h"
  561. #include "WishPiX/ResourceManager/resmgr.h"
  562. #include "main.h"
  563. #include "menus.h"
  564. #include "game.h"
  565. #include "localize.h"
  566. #include "play.h"
  567. #include "title.h"
  568. #include "credits.h"
  569. #include "scene.h"
  570. #include "update.h"
  571. #include "gameedit.h"
  572. #include "MenuTrans.h"
  573. #include "SampleMaster.h"
  574. #include "net.h"
  575. #include "NetDlg.h"
  576. #include "input.h"
  577. #include "InputSettingsDlg.h"
  578. #include "encrypt.h"
  579. #include "credits.h"
  580. #include "CompileOptions.h"
  581. ////////////////////////////////////////////////////////////////////////////////
  582. // Macros/types/etc.
  583. ////////////////////////////////////////////////////////////////////////////////
  584. // Total units required for loading during title sequence (estimated)
  585. #define TITLE_LOAD_UNITS 5000
  586. // TEMPORARY! Determines how long the fake title sequence load will take.
  587. // Remove this when there's real stuff being loaded.
  588. #define TEMP_TITLE_LOOPS 500
  589. #define TEMP_TITLE_TIME_PER_LOOP 10 // 100 looks normal, 10 goes extremely quickly
  590. // Font stuff
  591. #define BIG_FONT_FILE "res/fonts/system1.fnt"
  592. #define POSTAL_FONT_FILE "res/fonts/smash.fnt"
  593. #define FONT_HEIGHT 12 // "Best" for ComicB.
  594. #define FONT_FORE_INDEX 255 // White or black.
  595. #define FONT_BACK_INDEX 0 // Transparent.
  596. #define NORMAL_PAL_TRAN_TIME 750
  597. #define TEMP_ALPHA_EFFECT "2dEffects/alpha00.bmp"
  598. #define SHELL_SAK_FILENAME "res/shell/shell.sak"
  599. #define GAME_SAK_FILENAME "res/game/game.sak"
  600. #define SAMPLES_SAK_SUBDIR "res/game/"
  601. #define CUTSCENE_SAK_FILENAME_FRMT "res/cutscene/cutscene%02d.sak"
  602. // Since the game plays slightly differently on the PC than on the Mac,
  603. // we'll have two different sets of demos.
  604. #define DEFAULT_DEMO_PREFIX "res/demos/default"
  605. #define DEFAULT_DEMO_SUFFIX ".dmo"
  606. #define DEMO_OPEN_TITLE "Choose a Demo File to Play Back"
  607. #define DEMO_LEVEL_DIR "res/demos/."
  608. #define ENDING_DEMO_NAME "res/levels/single/index.rdx" // Fake name for school.dmo
  609. #define DEMO_DIR "res/demos/."
  610. #define DEMO_EXT ".dmo"
  611. #define LEVEL_DIR "res/levels/."
  612. #if WITH_STEAMWORKS
  613. extern bool EnableSteamCloud;
  614. #define SAVEGAME_DIR (EnableSteamCloud ? "steamcloud" : "savegame")
  615. #else
  616. #define SAVEGAME_DIR ("savegame")
  617. #endif
  618. #define CHECK_FOR_ASSETS_FILENAME "res/res.sak"
  619. #define CHECK_FOR_POSTALSD_FILENAME "res/hoods/ezmart.sak"
  620. #define COOKIE_VALUE 0x9504cc39 //0xb5cf76dd
  621. #define COOKIE_FILE_POSITION 289546856 //323290320
  622. #define COOKIE_XOR_MASK 0x6afb39e5 //0x66666666
  623. // Exponent used to define gamma curve.
  624. #define GAMMA_EXPONENT 1.50
  625. // The directories for each type of challenge levels.
  626. #define TIMED_CHALLENGE_LEVEL_DIR "res/levels/gauntlet/timed/."
  627. #define CHECKPOINT_CHALLENGE_LEVEL_DIR "res/levels/gauntlet/checkpt/."
  628. #define GOAL_CHALLENGE_LEVEL_DIR "res/levels/gauntlet/goal/."
  629. #define FLAG_CHALLENGE_LEVEL_DIR "res/levels/gauntlet/capflag/."
  630. // The titles for the open dialog for each type of challenge levels.
  631. #ifdef MOBILE
  632. #define TIMED_CHALLENGE_OPEN_TITLE "Timed Challenge"
  633. #define CHECKPOINT_CHALLENGE_OPEN_TITLE "Checkpoint Challenge"
  634. #define GOAL_CHALLENGE_OPEN_TITLE "Goal Challenge"
  635. #define FLAG_CHALLENGE_OPEN_TITLE "Capture the Flag Challenge"
  636. #else
  637. #define TIMED_CHALLENGE_OPEN_TITLE "Choose Timed Challenge"
  638. #define CHECKPOINT_CHALLENGE_OPEN_TITLE "Choose Checkpoint Challenge"
  639. #define GOAL_CHALLENGE_OPEN_TITLE "Choose Goal Challenge"
  640. #define FLAG_CHALLENGE_OPEN_TITLE "Choose Capture the Flag Challenge"
  641. #endif
  642. #define INVALID_DIFFICULTY -7
  643. #define TIME_OUT_FOR_ABORT_SOUNDS 3000 // In ms.
  644. // Determines the number of elements in the passed array at compile time.
  645. #define NUM_ELEMENTS(a) (sizeof(a) / sizeof(a[0]) )
  646. // We use this value to flag the fact that we haven't hidden the cursor.
  647. #define INVALID_CURSOR_SHOW_LEVEL ( (short)0x8000)
  648. // Various action types
  649. typedef enum
  650. {
  651. ACTION_NOTHING,
  652. ACTION_PLAY_SINGLE,
  653. ACTION_PLAY_BROWSE,
  654. ACTION_PLAY_HOST,
  655. ACTION_PLAY_CONNECT,
  656. ACTION_PLAY_CHALLENGE,
  657. ACTION_EDITOR,
  658. ACTION_DEMO_PLAYBACK,
  659. ACTION_DEMO_RECORD,
  660. ACTION_EDIT_INPUT_SETTINGS,
  661. ACTION_POSTAL_ORGAN,
  662. ACTION_LOAD_GAME,
  663. ACTION_PLAY_ADDON
  664. #ifdef MOBILE
  665. ,ACTION_CONTINUE_GAME
  666. #endif
  667. } ACTION;
  668. ////////////////////////////////////////////////////////////////////////////////
  669. // Variables/data
  670. ////////////////////////////////////////////////////////////////////////////////
  671. // Global game settings
  672. CGameSettings g_GameSettings;
  673. // Cookie flag
  674. long g_lCookieMonster;
  675. // Global screen buffer
  676. RImage* g_pimScreenBuf;
  677. // Global big font
  678. RFont g_fontBig;
  679. // Global Postal font.
  680. RFont g_fontPostal;
  681. // Resource manager for game resources. These are resources used by the actual
  682. // game, like things that a CThing loads that is not level specific. For example,
  683. // CBall loads, of course, foot.bmp, which would be loaded through this ResMgr.
  684. // Note: This resmgr should not be used for things like info on ordering, menu
  685. // resources, g_fontBig, or anything not specific to the real game.
  686. // Note: Realm specifc data, such as alpha effects, etc., should be loaded
  687. // through prealm->m_resmgr.
  688. RResMgr g_resmgrGame;
  689. // Resource manager for shell resources. Do not use this to load things like
  690. // the main dudes' sprites.
  691. RResMgr g_resmgrShell;
  692. // Resource manager for non-SAK resources.
  693. RResMgr g_resmgrRes;
  694. // Time and Date values
  695. long g_lRegTime;
  696. long g_lRegValue;
  697. long g_lExpTime;
  698. long g_lExpValue;
  699. long g_lReleaseTime;
  700. // Stockpile used to transfer loaded/saved data to/from the CDude's stockpile
  701. CStockPile g_stockpile;
  702. bool g_bTransferStockpile;
  703. short g_sRealmNumToSave;
  704. // Flag for special end of game demo sequence which is set in Play() when
  705. // it has been determined that the player has won.
  706. bool g_bLastLevelDemo = false;
  707. // The secret cookie value used to determine if the humongous file exists
  708. static U32 ms_u32Cookie = COOKIE_VALUE;
  709. // These variables are generally controlled via the menu system
  710. static ACTION m_action;
  711. static long m_lDemoBaseTime;
  712. static long m_lDemoTimeOut;
  713. static char m_szRealmFile[RSP_MAX_PATH+1];
  714. static char m_szDemoFile[RSP_MAX_PATH+1];
  715. static short m_sRealmNum;
  716. static bool m_bJustOneRealm;
  717. // Cursor show level before we went to the background
  718. static short ms_sForegroundCursorShowLevel = INVALID_CURSOR_SHOW_LEVEL;
  719. // Used by random number stuff
  720. static long m_lRandom = 1;
  721. static RFile* m_pfileRandom = 0;
  722. // Used by if-logging schtuff.
  723. static long ms_lSynchLogSeq = 0;
  724. static RFile ms_fileSynchLog;
  725. // true to create synchronization logs, false to compare to them.
  726. static bool m_bWriteLogs = false;
  727. static short ms_sLoadedDifficulty = INVALID_DIFFICULTY;
  728. static SampleMaster::SoundInstance ms_siMusak = 0;
  729. ////////////////////////////////////////////////////////////////////////////////
  730. // Function prototypes
  731. ////////////////////////////////////////////////////////////////////////////////
  732. static short GameCore(void); // Returns 0 on success.
  733. static void ResetDemoTimer(void);
  734. static short OpenSaks(void); // Returns 0 on success.
  735. static void CloseSaks(void);
  736. static short LoadAssets(void);
  737. static short UnloadAssets(void);
  738. static void GameSetRegistry(void);
  739. static void GameGetRegistry(void);
  740. static void GameEndingSequence(void);
  741. static short GetRealmToRecord(
  742. char* pszRealmFile,
  743. short sMaxFileLen);
  744. static short GetDemoFile(
  745. char* pszDemoFile,
  746. short sMaxFileLen);
  747. // Callback gets called when OS is about to switch app into the background
  748. static void BackgroundCall(void);
  749. // Callback gets called when OS is about to switch app into the foreground
  750. static void ForegroundCall(void);
  751. // Returns difficulty for games.
  752. // Note that this function is only valid once after a difficulty adjustment
  753. // and then it goes back to the default (g_GameSettings value).
  754. static short GetGameDifficulty(void); // Returns cached game difficulty.
  755. // Opens the synchronization log with the specified access flags if in a
  756. // TRACENASSERT mode and synchronization logging is enabled.
  757. // Also, opens the random log, if it is enabled via
  758. // g_GameSettings.m_szDebugMovie or something.
  759. static void OpenSynchLogs( // Returns nothing.
  760. bool bWriteLogs); // In: true to create log, false to compare.
  761. // Closes the synchronization logs, if open.
  762. static void CloseSynchLogs(void); // Returns nothing.
  763. ////////////////////////////////////////////////////////////////////////////////
  764. //
  765. // Load a previously saved game
  766. //
  767. ////////////////////////////////////////////////////////////////////////////////
  768. extern short Game_LoadPlayersGame( // Returns SUCCESS if loaded saved game file
  769. char* pszSaveName, // In: Name of the saved game file to open
  770. short* psDifficulty, // Out: Saved game realm difficulty.
  771. ACTION* paction); // Out: Saved game action.
  772. bool StatsAreAllowed = false;
  773. int Stat_BulletsFired = 0;
  774. int Stat_BulletsHit = 0;
  775. int Stat_BulletsMissed = 0;
  776. int Stat_Deaths = 0;
  777. int Stat_Suicides = 0;
  778. int Stat_Executions = 0;
  779. int Stat_HitsTaken = 0;
  780. int Stat_DamageTaken = 0;
  781. int Stat_Burns = 0;
  782. int Stat_TimeRunning = 0;
  783. int Stat_KilledHostiles = 0;
  784. int Stat_KilledCivilians = 0;
  785. int Stat_TotalKilled = 0;
  786. int Stat_LevelsPlayed = 0;
  787. ULONG Flag_Achievements = 0;
  788. #if 1 //PLATFORM_UNIX
  789. #include <sys/stat.h>
  790. static void EnumExistingSaveGames(Menu *menu)
  791. {
  792. char gamename[RSP_MAX_PATH];
  793. int i = 0;
  794. int Max = (sizeof(menu->ami) / sizeof(menu->ami[0])) - 1;
  795. if (Max > MAX_SAVE_SLOTS)
  796. Max = MAX_SAVE_SLOTS;
  797. #if MOBILE
  798. snprintf(gamename, sizeof (gamename), "%s/auto.gme", SAVEGAME_DIR);
  799. const char *fname = FindCorrectFile(gamename, "w");
  800. struct stat statbuf;
  801. const char *str = "unused";
  802. char timebuf[32];
  803. menu->ami[0].sEnabled = (stat(fname, &statbuf) != -1);
  804. if (menu->ami[0].sEnabled)
  805. {
  806. struct tm *tm;
  807. if ((tm = localtime((const time_t*)&statbuf.st_mtime)) == NULL)
  808. str = "unknown";
  809. else
  810. {
  811. strftime(timebuf, sizeof (timebuf), "%m/%d/%y %H:%M", tm);
  812. str = timebuf;
  813. }
  814. }
  815. snprintf(gamename, sizeof (gamename), "Auto - [%s]", str);
  816. menu->ami[0].pszText = strdup(gamename);
  817. for (i = 0; i < Max; i++)
  818. {
  819. snprintf(gamename, sizeof (gamename), "%s/%d.gme", SAVEGAME_DIR, i);
  820. const char *fname = FindCorrectFile(gamename, "w");
  821. struct stat statbuf;
  822. const char *str = "unused";
  823. char timebuf[32];
  824. menu->ami[i+1].sEnabled = (stat(fname, &statbuf) != -1);
  825. if (menu->ami[i+1].sEnabled)
  826. {
  827. struct tm *tm;
  828. if ((tm = localtime((const time_t*)&statbuf.st_mtime)) == NULL)
  829. str = "unknown";
  830. else
  831. {
  832. strftime(timebuf, sizeof (timebuf), "%m/%d/%y %H:%M", tm);
  833. str = timebuf;
  834. }
  835. }
  836. snprintf(gamename, sizeof (gamename), "%d - [%s]", i, str);
  837. menu->ami[i+1].pszText = strdup(gamename);
  838. }
  839. #else
  840. for (i = 0; i < Max; i++)
  841. {
  842. snprintf(gamename, sizeof (gamename), "%s/%d.gme", SAVEGAME_DIR, i);
  843. const char *fname = FindCorrectFile(gamename, "w");
  844. struct stat statbuf;
  845. const char *str = "unused";
  846. char timebuf[32];
  847. menu->ami[i].sEnabled = (stat(fname, &statbuf) != -1);
  848. if (menu->ami[i].sEnabled)
  849. {
  850. struct tm *tm;
  851. if ((tm = localtime(&statbuf.st_mtime)) == NULL)
  852. str = "unknown";
  853. else
  854. {
  855. strftime(timebuf, sizeof (timebuf), "%m/%d/%y %H:%M", tm);
  856. str = timebuf;
  857. }
  858. }
  859. snprintf(gamename, sizeof (gamename), "%s/%d.gme [%s]", SAVEGAME_DIR, i, str);
  860. menu->ami[i].pszText = strdup(gamename);
  861. }
  862. #endif
  863. }
  864. #endif
  865. ////////////////////////////////////////////////////////////////////////////////
  866. //
  867. // Do the high-level startup stuff, run the game, and then cleanup afterwards.
  868. //
  869. // It is assumed that the system/RSPiX environment are setup properly before
  870. // this function is called.
  871. //
  872. ////////////////////////////////////////////////////////////////////////////////
  873. extern void TheGame(void)
  874. {
  875. short sResult = 0;
  876. // Set up callbacks for when OS sends us to foreground or background.
  877. rspSetBackgroundCallback(BackgroundCall);
  878. rspSetForegroundCallback(ForegroundCall);
  879. ms_u32Cookie = ~ms_u32Cookie;
  880. g_lRegValue = 0;
  881. g_lExpValue = 0;
  882. g_lRegTime = 0;
  883. #ifdef CHECK_EXPIRATION_DATE
  884. g_lExpTime = EXPIRATION_DATE;
  885. #else
  886. g_lExpTime = SAFE_DATE;
  887. #endif
  888. g_lReleaseTime = RELEASE_DATE;
  889. // To make it more confusing for someone debugging through the code,
  890. // the flag for the cookie check will be this large number SAFE_DATE.
  891. // Once the game determines elsewhere that the cookie is correct, it will
  892. // in some way modify the flag so that it is not SAFE_DATE, then later
  893. // the check will be for anything other than SAFE_DATE
  894. #ifdef CHECK_FOR_COOKIE
  895. g_lCookieMonster = SAFE_DATE;
  896. #else
  897. g_lCookieMonster = SAFE_DATE - RELEASE_DATE;
  898. #endif
  899. g_bTransferStockpile = false;
  900. // Get pointer to screen buffer and lock it. Our Update()
  901. // unlocks and re-locks these each time it is called.
  902. rspNameBuffers(&g_pimScreenBuf);
  903. // Lock the RSPiX composite buffer so we can rect it.
  904. rspLockBuffer();
  905. // Clear screen buffer
  906. rspRect(RSP_BLACK_INDEX, g_pimScreenBuf,
  907. 0, 0, g_pimScreenBuf->m_sWidth, g_pimScreenBuf->m_sHeight);
  908. // Unlock now that we're done with the composite buffer.
  909. rspUnlockBuffer();
  910. // Update display
  911. rspUpdateDisplay();
  912. // Read all settings from preference file
  913. sResult = CSettings::LoadPrefs(g_pszPrefFileName);
  914. RFile file;
  915. if (sResult == 0)
  916. {
  917. #ifdef PROMPT_FOR_ORIGINAL_CD
  918. rspMsgBox(RSP_MB_ICN_INFO | RSP_MB_BUT_OK, g_pszAppName, g_pszPromptForOriginalCD);
  919. #endif
  920. #ifdef REQUIRE_POSTAL_CD
  921. // Check to make sure the correct CD is in the drive. The Postal Add On Requires
  922. // that the original PostalCD is in the drive.
  923. short sCorrectCD = -2;
  924. while (sCorrectCD == -2)
  925. {
  926. // We are looking for a disc that has a res.sak, but doesn't have ezmart.sak
  927. // to insure that they have the original PostalCD in the drive
  928. if (file.Open(FullPathCD(CHECK_FOR_ASSETS_FILENAME), "r", RFile::LittleEndian) == 0)
  929. {
  930. file.Close();
  931. sCorrectCD++;
  932. if (file.Open(FullPathCD(CHECK_FOR_POSTALSD_FILENAME), "r", RFile::LittleEndian) != 0)
  933. {
  934. sCorrectCD++;
  935. }
  936. else
  937. {
  938. file.Close();
  939. }
  940. }
  941. if (sCorrectCD != 0)
  942. {
  943. TRACE("Game(): Wrong CD in the drive - this is not the original PostalCD\n");
  944. if (rspMsgBox(RSP_MB_ICN_INFO | RSP_MB_BUT_RETRYCANCEL, g_pszCriticalErrorTitle, g_pszWrongCD, "Wrong CD") == 3)
  945. sCorrectCD = -2;
  946. else
  947. sCorrectCD = -3;
  948. }
  949. }
  950. // If we have the correct CD in the drive, then we can proceed
  951. sResult = sCorrectCD;
  952. #endif // REQUIRE_POSTAL_CD
  953. // Try loading special file that exists solely for the purpose of checking for
  954. // valid paths in the prefs file. We do this for each of the paths.
  955. if (sResult == 0)
  956. {
  957. if (file.Open(FullPathHD(CHECK_FOR_ASSETS_FILENAME), "r", RFile::LittleEndian) == 0)
  958. file.Close();
  959. else
  960. {
  961. sResult = -1;
  962. TRACE("Game(): Can't find assets based on HD path specified in prefs!\n");
  963. rspMsgBox(RSP_MB_ICN_STOP | RSP_MB_BUT_OK, g_pszCriticalErrorTitle, g_pszCantFindAssets, "HD");
  964. }
  965. }
  966. if (sResult == 0)
  967. {
  968. if (file.Open(FullPathVD(CHECK_FOR_ASSETS_FILENAME), "r", RFile::LittleEndian) == 0)
  969. file.Close();
  970. else
  971. {
  972. sResult = -1;
  973. TRACE("Game(): Can't find assets based on VD path specified in prefs!\n");
  974. rspMsgBox(RSP_MB_ICN_STOP | RSP_MB_BUT_OK, g_pszCriticalErrorTitle, g_pszCantFindAssets, "VD");
  975. }
  976. }
  977. if (sResult == 0)
  978. {
  979. if (file.Open(FullPathSound(CHECK_FOR_ASSETS_FILENAME), "r", RFile::LittleEndian) == 0)
  980. file.Close();
  981. else
  982. {
  983. sResult = -1;
  984. TRACE("Game(): Can't find assets based on Sound path specified in prefs!\n");
  985. rspMsgBox(RSP_MB_ICN_STOP | RSP_MB_BUT_OK, g_pszCriticalErrorTitle, g_pszCantFindAssets, "Sound");
  986. }
  987. }
  988. if (sResult == 0)
  989. {
  990. if (file.Open(FullPathGame(CHECK_FOR_ASSETS_FILENAME), "r", RFile::LittleEndian) == 0)
  991. file.Close();
  992. else
  993. {
  994. sResult = -1;
  995. TRACE("Game(): Can't find assets based on Game path specified in prefs!\n");
  996. rspMsgBox(RSP_MB_ICN_STOP | RSP_MB_BUT_OK, g_pszCriticalErrorTitle, g_pszCantFindAssets, "Game");
  997. }
  998. }
  999. if (sResult == 0)
  1000. {
  1001. if (file.Open(FullPathHoods(CHECK_FOR_ASSETS_FILENAME), "r", RFile::LittleEndian) == 0)
  1002. file.Close();
  1003. else
  1004. {
  1005. sResult = -1;
  1006. TRACE("Game(): Can't find assets based on Hoods path specified in prefs!\n");
  1007. rspMsgBox(RSP_MB_ICN_STOP | RSP_MB_BUT_OK, g_pszCriticalErrorTitle, g_pszCantFindAssets, "Hoods");
  1008. }
  1009. }
  1010. // Check for special file, COOKIE, and size.
  1011. if (sResult == 0)
  1012. {
  1013. ms_u32Cookie ^= COOKIE_XOR_MASK;
  1014. if (file.Open(FullPathCD(CHECK_FOR_ASSETS_FILENAME), "rb", RFile::LittleEndian) == 0)
  1015. {
  1016. #if defined(CHECK_FOR_COOKIE)
  1017. if (file.Seek(COOKIE_FILE_POSITION, SEEK_SET) == 0)
  1018. {
  1019. U32 u32Cookie = 0;
  1020. if (file.Read(&u32Cookie) == 1)
  1021. {
  1022. if (u32Cookie == ms_u32Cookie)
  1023. {
  1024. // Okay, you can play.
  1025. // This can be any number, the check will verify that the
  1026. // g_lCookieMonster is not SAFE_DATE.
  1027. g_lCookieMonster += 15;
  1028. }
  1029. else
  1030. {
  1031. //sResult = -4;
  1032. TRACE("Game(): Cookie value is incorrect.\n");
  1033. }
  1034. }
  1035. else
  1036. {
  1037. //sResult = -3;
  1038. TRACE("Game(): Failed to read cookie.\n");
  1039. }
  1040. }
  1041. else
  1042. {
  1043. //sResult = -2;
  1044. TRACE("Game(): Cookie file is incorrect size!\n");
  1045. }
  1046. #endif // defined(CHECK_FOR_COOKIE)
  1047. file.Close();
  1048. }
  1049. else
  1050. {
  1051. sResult = -1;
  1052. TRACE("Game(): Can't find assets based on CD path specified in prefs!\n");
  1053. }
  1054. // If any problems . . .
  1055. if (sResult != 0)
  1056. {
  1057. rspMsgBox(RSP_MB_ICN_STOP | RSP_MB_BUT_OK, g_pszCriticalErrorTitle, g_pszCantFindAssets, "CD");
  1058. }
  1059. }
  1060. // Check for CDROM drive.
  1061. if (sResult == 0)
  1062. {
  1063. #if defined(MUST_BE_ON_CD)
  1064. // Check for the special case where the path is the one we use for
  1065. // development. If someone out there happens to use this as their
  1066. // own path, then they will defeat this test for CD-ROM. Oh well.
  1067. char* pszDevelopmentPath = "\\\\narnia\\projects\\";
  1068. if (strnicmp(FullPathCD("."), pszDevelopmentPath, strlen(pszDevelopmentPath)) != 0)
  1069. {
  1070. #if defined(WIN32)
  1071. if (GetDriveType(FullPathCD(".") ) != DRIVE_CDROM)
  1072. #else
  1073. #error MUST_BE_ON_CD feature is currently implemented only for Win32
  1074. #endif
  1075. {
  1076. // Only set the error flag if we're in release mode
  1077. #ifndef _DEBUG
  1078. sResult = -1;
  1079. #endif
  1080. TRACE("Game(): CD path is not a CDROM!\n");
  1081. rspMsgBox(RSP_MB_ICN_STOP | RSP_MB_BUT_OK, g_pszCriticalErrorTitle, g_pszNotOnCDROM);
  1082. }
  1083. }
  1084. #endif
  1085. }
  1086. if (sResult == 0)
  1087. {
  1088. // Set the gamma level to value indicated by settings.
  1089. SetGammaLevel(g_GameSettings.m_sGammaVal);
  1090. // If trickier quit specified . . .
  1091. if (g_GameSettings.m_sTrickySystemQuit != FALSE)
  1092. {
  1093. // Add shift key as a requirement for the quit status keys.
  1094. rspSetQuitStatusFlags(RSP_GKF_SHIFT);
  1095. }
  1096. // Open SAKs or setup equivalent paths.
  1097. sResult = OpenSaks();
  1098. if (sResult == 0)
  1099. {
  1100. // Start title, passing the "total units" for its progress meter
  1101. sResult = StartTitle(1, true, &ms_siMusak);
  1102. if (sResult == 0)
  1103. {
  1104. // Load assets that we want to keep around at all times
  1105. sResult = LoadAssets();
  1106. // End title (regardless of previous result)
  1107. EndTitle();
  1108. if (sResult == 0)
  1109. {
  1110. // Set the font most GUIs will use (the menu system uses its own RPrint).
  1111. // Note that the size does not matter, we just want to set the font ptr.
  1112. RGuiItem::ms_print.SetFont(15, &g_fontBig);
  1113. // Do the core game stuff
  1114. sResult = GameCore();
  1115. // If there weren't any errors, wrap things up
  1116. if (!sResult)
  1117. {
  1118. // Do ending credits
  1119. if (rspGetQuitStatus() > 1)
  1120. {
  1121. rspSetQuitStatus(0);
  1122. Credits();
  1123. }
  1124. }
  1125. }
  1126. else
  1127. {
  1128. rspMsgBox(RSP_MB_ICN_STOP | RSP_MB_BUT_OK, g_pszCriticalErrorTitle, g_pszGeneralError);
  1129. }
  1130. // Unload assets loaded earlier
  1131. UnloadAssets();
  1132. }
  1133. else
  1134. {
  1135. TRACE("Game(): Error returned by StartTitle()!\n");
  1136. rspMsgBox(RSP_MB_ICN_STOP | RSP_MB_BUT_OK, g_pszCriticalErrorTitle, g_pszTitleError);
  1137. }
  1138. }
  1139. else
  1140. {
  1141. TRACE("Game(): Error returned by OpenSaks().\n");
  1142. }
  1143. #if 0
  1144. // Make sure we're not paused . . .
  1145. if (RMix::IsPaused() )
  1146. {
  1147. RMix::Resume();
  1148. }
  1149. #endif
  1150. // Abort all samples.
  1151. AbortAllSamples();
  1152. // We should never need a timeout but I don't want to risk a Muppets
  1153. // scenario where a shitty sound driver causes us to think a sound is always
  1154. // playing.
  1155. // Wait for all samples to finish.
  1156. long lTimeOutTime = rspGetMilliseconds() + TIME_OUT_FOR_ABORT_SOUNDS;
  1157. // Wait for them to stop.
  1158. while (IsSamplePlaying() == true && rspGetMilliseconds() < lTimeOutTime)
  1159. {
  1160. // Always do periodic updates.
  1161. // Crucial to sound completing.
  1162. UpdateSystem();
  1163. }
  1164. // Close SAKs and/or create SAKs??
  1165. CloseSaks();
  1166. }
  1167. // Save all settings to preference file
  1168. sResult = CSettings::SavePrefs(g_pszPrefFileName);
  1169. if (sResult > 0)
  1170. {
  1171. TRACE("Game(): Read-only prefs file!\n");
  1172. // rspMsgBox(RSP_MB_ICN_INFO | RSP_MB_BUT_OK, g_pszCriticalErrorTitle, g_pszPrefReadOnly);
  1173. }
  1174. else if (sResult < 0)
  1175. {
  1176. TRACE("Game(): Error writing prefs file!\n");
  1177. rspMsgBox(RSP_MB_ICN_STOP | RSP_MB_BUT_OK, g_pszCriticalErrorTitle, g_pszPrefWriteError);
  1178. }
  1179. }
  1180. else
  1181. {
  1182. TRACE("Game(): Error returned by ReadGamePrefs()!\n");
  1183. rspMsgBox(RSP_MB_ICN_STOP | RSP_MB_BUT_OK, g_pszCriticalErrorTitle, g_pszPrefReadError);
  1184. }
  1185. // Remove the callbacks
  1186. rspSetBackgroundCallback(NULL);
  1187. rspSetForegroundCallback(NULL);
  1188. }
  1189. ////////////////////////////////////////////////////////////////////////////////
  1190. //
  1191. // Do the core game stuff (display menu, play a game, run the demo, etc.)
  1192. //
  1193. ////////////////////////////////////////////////////////////////////////////////
  1194. static short GameCore(void) // Returns 0 on success.
  1195. {
  1196. short sResult = 0;
  1197. USHORT usDemoCount = 0;
  1198. bool bMPath = false,
  1199. bMPathServer = false;
  1200. #ifdef CHECK_EXPIRATION_DATE
  1201. #ifdef WIN32
  1202. char acTime[100];
  1203. strcpy(acTime, asctime(gmtime(&g_lExpTime)));
  1204. #define NEXT_LINE "\n\n"
  1205. #else
  1206. char acTime[100];
  1207. unsigned long lTime = g_lExpTime + (((365 * 70UL) + 17) * 24 * 60 * 60); // time_fudge 1900->1970
  1208. strcpy(acTime, ctime(&lTime));
  1209. char* pCR = strchr(acTime, '\n');
  1210. if (pCR != NULL)
  1211. *pCR = 0;
  1212. #define NEXT_LINE "\r\r"
  1213. #endif // WIN32
  1214. rspMsgBox(
  1215. RSP_MB_ICN_INFO | RSP_MB_BUT_OK,
  1216. #if defined(EXPIRATION_MSG_POSTAL_LAUNCH_WEEKEND)
  1217. // Title
  1218. "Postal Launch Weekend Edition",
  1219. #elif defined(EXPIRATION_MSG_MARKETING_RELEASE)
  1220. // Title
  1221. "Postal Beta Contest",
  1222. // Message Body
  1223. "This is a large, single-player, single-level BETA test version -- that means we're "
  1224. "still working on it and it's not final. But we want your comments to help us make it "
  1225. "even cooler, so we're holding a BETA TEST CONTEST!"
  1226. NEXT_LINE
  1227. "We're giving away tons o' free stuff, including FREE GAMES, T-SHIRTS, and even a hot "
  1228. "NEW DELL 266MHZ COMPUTER!"
  1229. NEXT_LINE
  1230. "If you haven't already registered, visit http://www.gopostal.com to find out more about Postal "
  1231. "and how you can register for the BETA TEST CONTEST!"
  1232. NEXT_LINE
  1233. #else
  1234. // Title
  1235. "Limited-Time Version",
  1236. #endif
  1237. // Message Body
  1238. "This limited-time version will expire on %s"
  1239. NEXT_LINE
  1240. "After the expiration date, you will still be able to get into the game, "
  1241. "but you will notice that everyone bursts into flames and dies. This is "
  1242. "NOT a bug -- it simply indicates that your version has expired.",
  1243. // Time String
  1244. acTime);
  1245. #endif // CHECK_EXPIRATION_DATE
  1246. #ifndef _WIN32
  1247. UnlockAchievement(ACHIEVEMENT_PLAY_ON_NON_WINDOWS_PLATFORM);
  1248. #endif
  1249. // Clear end of game flag - play will set the flag if the player wins the game
  1250. g_bLastLevelDemo = false;
  1251. // Clear any events that might be in the queue
  1252. rspClearAllInputEvents();
  1253. ClearXInputState();
  1254. // Get the registry value
  1255. GameGetRegistry();
  1256. // Init demo mode times
  1257. m_lDemoBaseTime = rspGetMilliseconds();
  1258. m_lDemoTimeOut = g_GameSettings.m_lInitialDemoTimeOut;
  1259. // Clear flags that are updated by the menu system callbacks (in this module)
  1260. m_action = ACTION_NOTHING;
  1261. m_sRealmNum = 0;
  1262. m_szRealmFile[0] = 0;
  1263. m_bJustOneRealm = false;
  1264. // Flag indicates whether or not menu is active
  1265. bool bMenuActive = false;
  1266. ACTION actionNext = ACTION_NOTHING; // Initialized for safety.
  1267. Menu* pmenuStart = NULL; // Next menu to start if not NULL.
  1268. bool bPalTran = true; // true to PalTranOn() before next
  1269. // menu.
  1270. bool bTitleImage = true; // true to display title image before
  1271. // next menu.
  1272. bool bTitleMusak = true; // true to play title musak during
  1273. // next menu. Requires title image.
  1274. //////////////////////////////////////
  1275. //////////////////////////////////////
  1276. // Keep looping until user quits or error occurs. Maybe even play
  1277. // a game or two along the way. If there's no user input for a
  1278. // certain amount of time, automatically run the self-playing demo.
  1279. RInputEvent ie;
  1280. memset(&ie, '\0', sizeof (ie)); // fix valgrind complaining... --ryan.
  1281. while (sResult == 0)
  1282. {
  1283. // Clear the end of game flag each time just to be safe, it only needs
  1284. // to be set within the last iteration
  1285. g_bLastLevelDemo = false;
  1286. // As always...
  1287. UpdateSystem();
  1288. // Get next input event
  1289. ie.type = RInputEvent::None;
  1290. rspGetNextInputEvent(&ie);
  1291. // If there's any key or button input, or the app is in BG mode (per the OS),
  1292. // reset the demo timer
  1293. if ((ie.type != RInputEvent::None) || rspIsBackground())
  1294. ResetDemoTimer();
  1295. // Does user want to exit app? Do this BEFORE checking whether menu is active
  1296. // so we won't bother activating the menu only to find out we're going to exit.
  1297. if (rspGetQuitStatus())
  1298. break;
  1299. // If menu isn't active, get it going
  1300. if (!bMenuActive)
  1301. {
  1302. // If paltran requested . . .
  1303. if (bPalTran)
  1304. PalTranOn();
  1305. sResult = StartMenu(pmenuStart ? pmenuStart : &menuMain, &g_resmgrShell, g_pimScreenBuf);
  1306. if (sResult == 0)
  1307. {
  1308. bMenuActive = true;
  1309. // Restore defaults.
  1310. pmenuStart = NULL;
  1311. bPalTran = true;
  1312. bTitleImage = true;
  1313. bTitleMusak = true;
  1314. }
  1315. else
  1316. {
  1317. TRACE("GameLoop(): Error returned by StartMenu()!\n");
  1318. rspMsgBox(RSP_MB_ICN_STOP | RSP_MB_BUT_OK, g_pszAppName, "Cannot initialize menu system, most likely due to a memory or drive error.\n");
  1319. break;
  1320. }
  1321. }
  1322. // Let menu system do its thing
  1323. DoMenuInput(&ie, g_InputSettings.m_sUseJoy);
  1324. DoMenuOutput(g_pimScreenBuf);
  1325. // Update the screen
  1326. rspUpdateDisplay();
  1327. // If this is not the spawn version, then check to see if the user has been
  1328. // idle long enough to run a demo. The spawn version doesn't have any
  1329. // demos available, so we shouldn't try to run them.
  1330. #ifndef SPAWN
  1331. // If there's no user action and the demo timer expired, simulate the
  1332. // appropriate action to start the self-running demo.
  1333. #if 0 // Just disable demos entirely, we've changed things too much for the old demos to be compatible anymore. - Rick
  1334. if ((m_action == ACTION_NOTHING) &&
  1335. ((rspGetMilliseconds() - m_lDemoBaseTime) >= m_lDemoTimeOut) &&
  1336. g_GameSettings.m_sNumAvailableDemos > 0)
  1337. m_action = ACTION_DEMO_PLAYBACK;
  1338. #endif
  1339. #endif // SPAWN
  1340. // Reset next action.
  1341. actionNext = ACTION_NOTHING;
  1342. // Any action to handle?
  1343. if (m_action != ACTION_NOTHING)
  1344. {
  1345. // Handle the action
  1346. switch(m_action)
  1347. {
  1348. //------------------------------------------------------------------------------
  1349. // Play single player game
  1350. //------------------------------------------------------------------------------
  1351. case ACTION_PLAY_SINGLE:
  1352. // If this is the spawn version, then they are not allowed to play single
  1353. // player, so we will take out some of the single player code so its not
  1354. // easy to hack back in.
  1355. #ifndef SPAWN
  1356. // End menu
  1357. StopMenu();
  1358. PalTranOff();
  1359. bMenuActive = false;
  1360. // Clear the game winning flag - play will set it before it returns
  1361. // if the player has won the game
  1362. g_bLastLevelDemo = false;
  1363. Play(
  1364. NULL, // No client (not network game)
  1365. NULL, // No server (not network game)
  1366. INPUT_MODE_LIVE, // Input mode
  1367. m_sRealmNum, // Realm number OR -1 to use realm file
  1368. m_szRealmFile, // Realm file
  1369. m_bJustOneRealm, // Whether to play just one realm or not
  1370. false, // Not challenge mode
  1371. false, // Not new single player Add on levels
  1372. GetGameDifficulty(), // Difficulty level
  1373. false, // Rejunenate (MP only)
  1374. 0, // Time limit (MP only)
  1375. 0, // Kill limit (MP only)
  1376. 0, // Use cooperative levels (MP only)
  1377. 0, // Use cooperative mode (MP only)
  1378. 0, // Frame time (MP only)
  1379. NULL); // Demo mode file
  1380. #ifdef MOBILE
  1381. AndroidSetScreenMode(TOUCH_SCREEN_MENU);
  1382. #endif
  1383. // If the player won the game, show them the last level demo
  1384. // and then the ending cutscenes.
  1385. if (g_bLastLevelDemo)
  1386. GameEndingSequence();
  1387. // clear the end of game flag just to be safe
  1388. g_bLastLevelDemo = false;
  1389. #endif
  1390. break;
  1391. //------------------------------------------------------------------------------
  1392. // Join a multiplayer game
  1393. //------------------------------------------------------------------------------
  1394. case ACTION_PLAY_BROWSE:
  1395. case ACTION_PLAY_CONNECT:
  1396. // If multiplayer is disabled, leave out this code to make it harder to hack back in
  1397. #ifndef MULTIPLAYER_DISABLED
  1398. {
  1399. // Set flag as to whether we're browsing or connecting
  1400. bool bBrowse = (m_action == ACTION_PLAY_BROWSE) ? true : false;
  1401. // Startup sockets with selected protocol
  1402. if (RSocket::Startup((RSocket::ProtoType)g_GameSettings.m_usProtocol, false) == 0)
  1403. {
  1404. InitNetProbGUI();
  1405. // Set next menu to start on to the current menu.
  1406. pmenuStart = GetCurrentMenu();
  1407. // End the menu but don't PalTranOff unless we actually join a game.
  1408. StopMenu();
  1409. bMenuActive = false;
  1410. bPalTran = false;
  1411. bTitleImage = false;
  1412. bTitleMusak = false;
  1413. // Use the net game dialog to join a multiplayer game
  1414. CNetClient* pnetclient = new CNetClient;
  1415. NetMsg msg;
  1416. if (DoNetGameDialog(pnetclient, bBrowse, NULL, &msg) == 0)
  1417. {
  1418. // If the game was actually started...
  1419. if (msg.msg.nothing.ucType == NetMsg::START_GAME)
  1420. {
  1421. PalTranOff();
  1422. // Go back to the main menu when done and do
  1423. // all the deluxe stuff.
  1424. pmenuStart = NULL;
  1425. bPalTran = true;
  1426. bTitleImage = true;
  1427. bTitleMusak = true;
  1428. ASSERT(pnetclient->GetNumPlayers() >= 1);
  1429. // Create synchronization logs, if enabled.
  1430. OpenSynchLogs(true);
  1431. Play(
  1432. pnetclient, // Client
  1433. NULL, // No server (not hosting game)
  1434. INPUT_MODE_LIVE, // Input mode
  1435. msg.msg.startGame.sRealmNum, // Realm number OR -1 to use realm file
  1436. msg.msg.startGame.acRealmFile, // Realm file
  1437. msg.msg.startGame.sRealmNum >= 0 ? false : true, // Whether to play just one realm or not
  1438. false, // Not challenge mode
  1439. false, // Not new single player Add On leves
  1440. msg.msg.startGame.sDifficulty, // Difficulty
  1441. msg.msg.startGame.sRejuvenate, // Rejunenate (MP only)
  1442. msg.msg.startGame.sTimeLimit, // Time limit (MP only)
  1443. msg.msg.startGame.sKillLimit, // Kill limit (MP only)
  1444. msg.msg.startGame.sCoopLevels, // Cooperative or Deathmatch levels (MP only)
  1445. msg.msg.startGame.sCoopMode, // Cooperative or Deathmatch mode (MP only)
  1446. msg.msg.startGame.sFrameTime, // Frame time (MP only)
  1447. NULL); // Demo mode file
  1448. #ifdef MOBILE
  1449. AndroidSetScreenMode(TOUCH_SCREEN_MENU);
  1450. #endif
  1451. // Close synchronization logs, if opened.
  1452. CloseSynchLogs();
  1453. }
  1454. }
  1455. delete pnetclient;
  1456. KillNetProbGUI();
  1457. RSocket::Shutdown();
  1458. }
  1459. else
  1460. {
  1461. TRACE("GameCore(): Couldn't init protocol!\n");
  1462. rspMsgBox(RSP_MB_ICN_STOP | RSP_MB_BUT_OK, g_pszAppName, "The selected network protocol has failed to initialize. Please contact your system administrator or network vendor.\n");
  1463. }
  1464. }
  1465. #endif //MULTIPLAYER_DISABLED
  1466. break;
  1467. //------------------------------------------------------------------------------
  1468. // Host a multiplayer game
  1469. //------------------------------------------------------------------------------
  1470. case ACTION_PLAY_HOST:
  1471. // If multiplayer is disabled, leave out this code to make it harder to hack back in
  1472. #ifndef MULTIPLAYER_DISABLED
  1473. {
  1474. // Startup sockets with selected protocol
  1475. if (RSocket::Startup((RSocket::ProtoType)g_GameSettings.m_usProtocol, false) == 0)
  1476. {
  1477. InitNetProbGUI();
  1478. // Set next menu to start on to the current menu.
  1479. pmenuStart = GetCurrentMenu();
  1480. // End the menu but don't PalTranOff unless we actually join a game.
  1481. StopMenu();
  1482. bMenuActive = false;
  1483. bPalTran = false;
  1484. bTitleImage = false;
  1485. bTitleMusak = false;
  1486. // Use the net game dialog to host a multiplayer game
  1487. CNetClient* pnetclient = new CNetClient;
  1488. CNetServer* pnetserver = new CNetServer;
  1489. NetMsg msg;
  1490. if (DoNetGameDialog(pnetclient, false, pnetserver, &msg) == 0)
  1491. {
  1492. // If the game was actually started...
  1493. if (msg.msg.nothing.ucType == NetMsg::START_GAME)
  1494. {
  1495. PalTranOff();
  1496. // Go back to the main menu when done and do
  1497. // all the deluxe stuff.
  1498. pmenuStart = NULL;
  1499. bPalTran = true;
  1500. bTitleImage = true;
  1501. bTitleMusak = true;
  1502. ASSERT(pnetclient->GetNumPlayers() >= 1);
  1503. // Create synchronization logs, if enabled.
  1504. OpenSynchLogs(true);
  1505. Play(
  1506. pnetclient, // Client
  1507. pnetserver, // Server, too
  1508. INPUT_MODE_LIVE, // Input mode
  1509. msg.msg.startGame.sRealmNum, // Realm number OR -1 to use realm file
  1510. msg.msg.startGame.acRealmFile, // Realm file
  1511. msg.msg.startGame.sRealmNum >= 0 ? false : true, // Whether to play just one realm or not
  1512. false, // Not challenge mode
  1513. false, // Not new single player Add on levels
  1514. msg.msg.startGame.sDifficulty, // Difficulty
  1515. msg.msg.startGame.sRejuvenate, // Rejunenate (MP only)
  1516. msg.msg.startGame.sTimeLimit, // Time limit (MP only)
  1517. msg.msg.startGame.sKillLimit, // Kill limit (MP only)
  1518. msg.msg.startGame.sCoopLevels, // Cooperative or Deathmatch levels (MP only)
  1519. msg.msg.startGame.sCoopMode, // Cooperative or Deathmatch mode (MP only)
  1520. msg.msg.startGame.sFrameTime, // Frame time (MP only)
  1521. NULL); // Demo mode file
  1522. #ifdef MOBILE
  1523. AndroidSetScreenMode(TOUCH_SCREEN_MENU);
  1524. #endif
  1525. // Close synchronization logs, if opened.
  1526. CloseSynchLogs();
  1527. }
  1528. }
  1529. delete pnetserver;
  1530. delete pnetclient;
  1531. KillNetProbGUI();
  1532. RSocket::Shutdown();
  1533. }
  1534. else
  1535. {
  1536. TRACE("GameCore(): Couldn't init protocol!\n");
  1537. rspMsgBox(RSP_MB_ICN_STOP | RSP_MB_BUT_OK, g_pszAppName, "The selected network protocol has failed to initialize. Please contact your system administrator or network vendor.\n");
  1538. }
  1539. }
  1540. #endif //MULTIPLAYER_DISABLED
  1541. break;
  1542. //------------------------------------------------------------------------------
  1543. // Play challenge game
  1544. //------------------------------------------------------------------------------
  1545. case ACTION_PLAY_CHALLENGE:
  1546. // Remember menu to go back to.
  1547. pmenuStart = GetCurrentMenu();
  1548. // End the menu.
  1549. StopMenu();
  1550. bMenuActive = false;
  1551. // Turn off paltran but remember to restore.
  1552. PalTranOff();
  1553. bPalTran = true;
  1554. // Remember to show title, but no musak.
  1555. bTitleImage = true;
  1556. bTitleMusak = false;
  1557. // Note that m_sRealmNum, m_szRealmFile, and m_bJustOneRealm are
  1558. // set via the callback, Game_StartChallengeGame().
  1559. // ***ADD FLAG(S) TO THIS CALL INDICATING THIS IS A CHALLENGE GAME***
  1560. Play(
  1561. NULL, // No client (not network game)
  1562. NULL, // No server (not network game)
  1563. INPUT_MODE_LIVE, // Input mode
  1564. m_sRealmNum, // Realm number OR -1 to use realm file
  1565. m_szRealmFile, // Realm file
  1566. m_bJustOneRealm, // Whether to play just one realm or not
  1567. true, // Play challenge levels
  1568. false, // Not new single player Add On levels
  1569. GetGameDifficulty(), // Difficulty level
  1570. false, // Rejunenate (MP only)
  1571. 0, // Time limit (MP only)
  1572. 0, // Kill limit (MP only)
  1573. 0, // Cooperative (MP only)
  1574. 0, // Use cooperative mode (MP only)
  1575. 0, // Frame time (MP only)
  1576. NULL); // Demo mode file
  1577. #ifdef MOBILE
  1578. AndroidSetScreenMode(TOUCH_SCREEN_MENU);
  1579. #endif
  1580. break;
  1581. //------------------------------------------------------------------------------
  1582. // Play Add On levels
  1583. //------------------------------------------------------------------------------
  1584. case ACTION_PLAY_ADDON:
  1585. #ifndef SPAWN
  1586. // Remember menu to go back to.
  1587. pmenuStart = GetCurrentMenu();
  1588. // End the menu.
  1589. StopMenu();
  1590. bMenuActive = false;
  1591. // Turn off paltran but remember to restore.
  1592. PalTranOff();
  1593. bPalTran = true;
  1594. // Remember to show title, but no musak.
  1595. bTitleImage = true;
  1596. bTitleMusak = false;
  1597. // Note that m_sRealmNum, m_szRealmFile, and m_bJustOneRealm are
  1598. // set via the callback, Game_StartChallengeGame().
  1599. // ***ADD FLAG(S) TO THIS CALL INDICATING THIS IS A CHALLENGE GAME***
  1600. Play(
  1601. NULL, // No client (not network game)
  1602. NULL, // No server (not network game)
  1603. INPUT_MODE_LIVE, // Input mode
  1604. m_sRealmNum, // Realm number OR -1 to use realm file
  1605. m_szRealmFile, // Realm file
  1606. m_bJustOneRealm, // Whether to play just one realm or not
  1607. false, // Don't play challenge levels
  1608. true, // Play new single player Add on levels
  1609. GetGameDifficulty(), // Difficulty level
  1610. false, // Rejunenate (MP only)
  1611. 0, // Time limit (MP only)
  1612. 0, // Kill limit (MP only)
  1613. 0, // Cooperative (MP only)
  1614. 0, // Use cooperative mode (MP only)
  1615. 0, // Frame time (MP only)
  1616. NULL); // Demo mode file
  1617. #ifdef MOBILE
  1618. AndroidSetScreenMode(TOUCH_SCREEN_MENU);
  1619. #endif
  1620. #endif // SPAWN
  1621. break;
  1622. //------------------------------------------------------------------------------
  1623. // Playback demo
  1624. //------------------------------------------------------------------------------
  1625. case ACTION_DEMO_PLAYBACK:
  1626. {
  1627. // Prepare all settings for demo mode
  1628. CSettings::PreDemo();
  1629. // If demo debug movie file is specified in prefs, open it now. The RFile*
  1630. // is does double-duty as a flag, where non-zero means movie mode is enabled.
  1631. RFile* pfileDemoDebugMovie = 0;
  1632. #if 0
  1633. if (strlen(g_GameSettings.m_szDemoDebugMovie) > 0)
  1634. {
  1635. pfileDemoDebugMovie = new RFile;
  1636. if (pfileDemoDebugMovie->Open(g_GameSettings.m_szDemoDebugMovie, "rb", RFile::LittleEndian) != 0)
  1637. {
  1638. delete pfileDemoDebugMovie;
  1639. pfileDemoDebugMovie = 0;
  1640. }
  1641. }
  1642. #else
  1643. // Compare to synchronization logs, if enabled.
  1644. OpenSynchLogs(false);
  1645. #endif
  1646. if (InputDemoInit() == 0)
  1647. {
  1648. // If no specific filename has been set for the demo, then load one of
  1649. // the default demos.
  1650. if (m_szDemoFile[0] == '\0')
  1651. {
  1652. // If there are default demos . . .
  1653. if(g_GameSettings.m_sNumAvailableDemos > 0)
  1654. {
  1655. sprintf(m_szDemoFile, "%s%d%s", FullPathHD(DEFAULT_DEMO_PREFIX), usDemoCount % MAX((short) 1, g_GameSettings.m_sNumAvailableDemos), DEFAULT_DEMO_SUFFIX);
  1656. }
  1657. }
  1658. // If there is now a demo filename . . .
  1659. if (m_szDemoFile[0] != '\0')
  1660. {
  1661. RFile fileDemo;
  1662. usDemoCount++;
  1663. if (fileDemo.Open(m_szDemoFile, "rb", RFile::LittleEndian) == 0)
  1664. {
  1665. // Read name of realm file
  1666. char szRealmFile[RSP_MAX_PATH];
  1667. fileDemo.Read(szRealmFile);
  1668. // Read whether it's a full path.
  1669. short sRealmFileIsFullPath;
  1670. fileDemo.Read(&sRealmFileIsFullPath);
  1671. if (!fileDemo.Error())
  1672. {
  1673. // Load input demo data (must be BEFORE setting playback mode)
  1674. if (InputDemoLoad(&fileDemo) == 0)
  1675. {
  1676. // End menu (now that we know there were no errors)
  1677. StopMenu();
  1678. PalTranOff();
  1679. bMenuActive = false;
  1680. Play(
  1681. NULL, // No client (not network game)
  1682. NULL, // No server (not network game)
  1683. INPUT_MODE_PLAYBACK, // Input mode
  1684. -1, // Always use specific realm file
  1685. szRealmFile, // Realm file to be played
  1686. false, // Don't play just one realm
  1687. false, // Not challenge mode
  1688. false, // Not new single player Add On levels
  1689. GetGameDifficulty(), // Difficulty level
  1690. false, // Rejunenate (MP only)
  1691. 0, // Time limit (MP only)
  1692. 0, // Kill limit (MP only)
  1693. 0, // Cooperative (MP only)
  1694. 0, // Use cooperative mode (MP only)
  1695. 0, // Frame time (MP only)
  1696. pfileDemoDebugMovie); // Demo mode file
  1697. }
  1698. else
  1699. {
  1700. TRACE("GameCore(): Couldn't load demo data!\n");
  1701. rspMsgBox(RSP_MB_ICN_STOP | RSP_MB_BUT_OK, g_pszAppName, g_pszFileReadError_s, (char*) m_szDemoFile);
  1702. }
  1703. }
  1704. else
  1705. {
  1706. TRACE("GameCore(): Couldn't load realm name!\n");
  1707. rspMsgBox(RSP_MB_ICN_STOP | RSP_MB_BUT_OK, g_pszAppName, g_pszFileReadError_s, (char*) m_szDemoFile);
  1708. }
  1709. fileDemo.Close();
  1710. }
  1711. else
  1712. {
  1713. TRACE("GameCore(): Couldn't open demo file: '%s'\n", m_szDemoFile);
  1714. rspMsgBox(RSP_MB_ICN_STOP | RSP_MB_BUT_OK, g_pszAppName, g_pszFileOpenError_s, (char*) m_szDemoFile);
  1715. }
  1716. }
  1717. else
  1718. {
  1719. TRACE("GameCore(): No demo filename.\n");
  1720. }
  1721. // Reset demo file name for next time.
  1722. m_szDemoFile[0] = 0;
  1723. InputDemoKill();
  1724. }
  1725. #if 0
  1726. if (pfileDemoDebugMovie)
  1727. {
  1728. // File may have been closed by Play() if an error occurred
  1729. if (pfileDemoDebugMovie->IsOpen())
  1730. pfileDemoDebugMovie->Close();
  1731. delete pfileDemoDebugMovie;
  1732. pfileDemoDebugMovie = 0;
  1733. }
  1734. #else
  1735. CloseSynchLogs();
  1736. #endif
  1737. #ifdef MOBILE
  1738. AndroidSetScreenMode(TOUCH_SCREEN_MENU);
  1739. #endif
  1740. // Restore settings to what they were before demo mode
  1741. CSettings::PostDemo();
  1742. }
  1743. break;
  1744. //------------------------------------------------------------------------------
  1745. // Record demo
  1746. //------------------------------------------------------------------------------
  1747. case ACTION_DEMO_RECORD:
  1748. {
  1749. // Prepare all settings for demo mode
  1750. CSettings::PreDemo();
  1751. // If demo debug movie file is specified in prefs, open it now. The RFile*
  1752. // is does double-duty as a flag, where non-zero means movie mode is enabled.
  1753. RFile* pfileDemoDebugMovie = 0;
  1754. #if 0
  1755. if (strlen(g_GameSettings.m_szDemoDebugMovie) > 0)
  1756. {
  1757. pfileDemoDebugMovie = new RFile;
  1758. if (pfileDemoDebugMovie->Open(g_GameSettings.m_szDemoDebugMovie, "wb", RFile::LittleEndian) != 0)
  1759. {
  1760. delete pfileDemoDebugMovie;
  1761. pfileDemoDebugMovie = 0;
  1762. }
  1763. }
  1764. #else
  1765. // Create synchronization logs, if enabled.
  1766. OpenSynchLogs(true);
  1767. #endif
  1768. if (InputDemoInit() == 0)
  1769. {
  1770. // Get name of realm to play
  1771. char szRealmFile[RSP_MAX_PATH];
  1772. short sGetRealmResult = GetRealmToRecord(szRealmFile, sizeof(szRealmFile));
  1773. switch (sGetRealmResult)
  1774. {
  1775. case 0: // Success.
  1776. break;
  1777. case 1: // Not a relative path. Got a full path.
  1778. break;
  1779. default: // Error.
  1780. break;
  1781. }
  1782. if (sGetRealmResult >= 0)
  1783. {
  1784. // Get name of demo file to save to
  1785. char szDemoFile[RSP_MAX_PATH];
  1786. if (GetDemoFile(szDemoFile, sizeof(szDemoFile)) == 0)
  1787. {
  1788. // Open demo file
  1789. RFile fileDemo;
  1790. if (fileDemo.Open(szDemoFile, "wb", RFile::LittleEndian) == 0)
  1791. {
  1792. // Write name of realm file
  1793. fileDemo.Write(szRealmFile);
  1794. // Write whether it's a full path.
  1795. fileDemo.Write(sGetRealmResult);
  1796. // End menu (now that we know there were no errors)
  1797. StopMenu();
  1798. PalTranOff();
  1799. bMenuActive = false;
  1800. Play(
  1801. NULL, // No client (not network game)
  1802. NULL, // No server (not network game)
  1803. INPUT_MODE_RECORD, // Input mode
  1804. -1, // Always use specific realm file
  1805. szRealmFile, // Realm file to be played
  1806. false, // Don't play just one realm
  1807. false, // Not challenge mode
  1808. false, // Not new single player Add on levels
  1809. GetGameDifficulty(), // Difficulty level
  1810. false, // Rejunenate (MP only)
  1811. 0, // Time limit (MP only)
  1812. 0, // Kill limit (MP only)
  1813. 0, // Cooperative (MP only)
  1814. 0, // Use cooperative mode (MP only)
  1815. 0, // Frame time (MP only)
  1816. pfileDemoDebugMovie); // Demo mode file
  1817. #ifdef MOBILE
  1818. AndroidSetScreenMode(TOUCH_SCREEN_MENU);
  1819. #endif
  1820. // Save input data to file
  1821. if (InputDemoSave(&fileDemo) != 0)
  1822. {
  1823. TRACE("GameCore(): Couldn't save demo data!\n");
  1824. rspMsgBox(RSP_MB_ICN_STOP | RSP_MB_BUT_OK, g_pszAppName, g_pszFileWriteError_s, szDemoFile);
  1825. }
  1826. }
  1827. fileDemo.Close();
  1828. }
  1829. else
  1830. {
  1831. TRACE("GameCore(): Couldn't open demo file: '%s'\n", szDemoFile);
  1832. rspMsgBox(RSP_MB_ICN_STOP | RSP_MB_BUT_OK, g_pszAppName, g_pszFileOpenError_s, szDemoFile);
  1833. }
  1834. }
  1835. InputDemoKill();
  1836. }
  1837. #if 0
  1838. if (pfileDemoDebugMovie)
  1839. {
  1840. // File may have been closed by Play() if an error occurred
  1841. if (pfileDemoDebugMovie->IsOpen())
  1842. pfileDemoDebugMovie->Close();
  1843. delete pfileDemoDebugMovie;
  1844. pfileDemoDebugMovie = 0;
  1845. }
  1846. #else
  1847. CloseSynchLogs();
  1848. #endif
  1849. // Restore settings to what they were before demo mode
  1850. CSettings::PostDemo();
  1851. }
  1852. break;
  1853. //------------------------------------------------------------------------------
  1854. // Go to the editor
  1855. //------------------------------------------------------------------------------
  1856. #if !defined(EDITOR_DISABLED)
  1857. case ACTION_EDITOR:
  1858. // End menu
  1859. StopMenu();
  1860. PalTranOff();
  1861. bMenuActive = false;
  1862. // Run the game editor
  1863. GameEdit();
  1864. break;
  1865. #endif
  1866. //------------------------------------------------------------------------------
  1867. // Edit key, mouse, or joystick settings.
  1868. //------------------------------------------------------------------------------
  1869. case ACTION_EDIT_INPUT_SETTINGS:
  1870. // Leave the menu on.
  1871. // Run the input settings editor.
  1872. EditInputSettings();
  1873. break;
  1874. //------------------------------------------------------------------------------
  1875. // Postal organ.
  1876. //------------------------------------------------------------------------------
  1877. case ACTION_POSTAL_ORGAN:
  1878. // Launch Postal Organ.
  1879. PlayWithMyOrgan();
  1880. break;
  1881. //------------------------------------------------------------------------------
  1882. // Load previously saved user game.
  1883. //------------------------------------------------------------------------------
  1884. case ACTION_LOAD_GAME:
  1885. {
  1886. // Static so dialog will "remember" the previously-used name
  1887. static char szFileSaved[RSP_MAX_PATH] = "";
  1888. // If not yet used, start out in appropirate directory
  1889. if (szFileSaved[0] == '\0')
  1890. strcpy(szFileSaved, FullPathHD(SAVEGAME_DIR));
  1891. // Display option dialog to let user choose a realm file
  1892. #if 1 //PLATFORM_UNIX
  1893. char tmp[RSP_MAX_PATH];
  1894. if (PickFile("Choose Game Slot", EnumExistingSaveGames, szFileSaved, sizeof(szFileSaved)) == 0)
  1895. {
  1896. #ifdef MOBILE
  1897. //Android we have the format "1 - date"
  1898. //Auot save is "Auto - date"
  1899. //Need to create the filename
  1900. char number = szFileSaved[0];
  1901. if (number == 'A') //Check for auto save now
  1902. snprintf(szFileSaved, sizeof (szFileSaved), "%s/auto.gme", SAVEGAME_DIR);
  1903. else
  1904. snprintf(szFileSaved, sizeof (szFileSaved), "%s/%c.gme", SAVEGAME_DIR,number);
  1905. TRACE("Load file: %s",szFileSaved);
  1906. #else
  1907. char *ptr = strrchr(szFileSaved, '[');
  1908. if (ptr) *(ptr-1) = '\0';
  1909. #endif
  1910. // This function will open the saved game file and set the correct game mode
  1911. // and settings. Note that this modifies the m_action (that's how we get
  1912. // out this state...this confused me for a while but it seems like a good
  1913. // way to choose the appropriate original action).
  1914. Game_LoadPlayersGame(szFileSaved, &ms_sLoadedDifficulty, &actionNext);
  1915. m_bJustOneRealm = false;
  1916. }
  1917. #else
  1918. if (rspOpenBox(g_pszLoadGameTitle, szFileSaved, szFileSaved, sizeof(szFileSaved), ".gme") == 0)
  1919. {
  1920. // This function will open the saved game file and set the correct game mode
  1921. // and settings. Note that this modifies the m_action (that's how we get
  1922. // out this state...this confused me for a while but it seems like a good
  1923. // way to choose the appropriate original action).
  1924. Game_LoadPlayersGame(szFileSaved, &ms_sLoadedDifficulty, &actionNext);
  1925. m_bJustOneRealm = false;
  1926. }
  1927. #endif
  1928. break;
  1929. }
  1930. #ifdef MOBILE
  1931. case ACTION_CONTINUE_GAME:
  1932. {
  1933. static char szFileSaved[RSP_MAX_PATH] = "";
  1934. snprintf(szFileSaved, sizeof (szFileSaved), "%s/auto.gme", SAVEGAME_DIR);
  1935. Game_LoadPlayersGame(szFileSaved, &ms_sLoadedDifficulty, &actionNext);
  1936. m_bJustOneRealm = false;
  1937. }
  1938. break;
  1939. #endif
  1940. //------------------------------------------------------------------------------
  1941. // Oooops
  1942. //------------------------------------------------------------------------------
  1943. default:
  1944. TRACE("GameCore(): Unrecognized action: %ld!\n", (long)m_action);
  1945. break;
  1946. }
  1947. // If this action lead to another action . . .
  1948. if (actionNext != ACTION_NOTHING)
  1949. {
  1950. // Set current action to the next action
  1951. m_action = actionNext;
  1952. }
  1953. else
  1954. {
  1955. // Reset action
  1956. m_action = ACTION_NOTHING;
  1957. }
  1958. // If menu was taken away, restore the screen behind it
  1959. if (!bMenuActive)
  1960. {
  1961. // If title requested . . .
  1962. if (bTitleImage)
  1963. {
  1964. // Only use musak, if specified.
  1965. StartTitle(0, bTitleMusak, &ms_siMusak);
  1966. EndTitle();
  1967. }
  1968. }
  1969. // Reset demo timer
  1970. ResetDemoTimer();
  1971. }
  1972. }
  1973. // Set the registry value before exiting
  1974. GameSetRegistry();
  1975. // If the menu is active, end it
  1976. if (bMenuActive)
  1977. {
  1978. StopMenu();
  1979. PalTranOff();
  1980. bMenuActive = false;
  1981. }
  1982. return sResult;
  1983. }
  1984. ////////////////////////////////////////////////////////////////////////////////
  1985. //
  1986. // Reset temo timer
  1987. //
  1988. ////////////////////////////////////////////////////////////////////////////////
  1989. static void ResetDemoTimer(void)
  1990. {
  1991. // Reset base time
  1992. m_lDemoBaseTime = rspGetMilliseconds();
  1993. // Change timeout to use persistant timeout
  1994. m_lDemoTimeOut = g_GameSettings.m_lPersistentDemoTimeOut;
  1995. }
  1996. ////////////////////////////////////////////////////////////////////////////////
  1997. //
  1998. // Get realm to be recorded
  1999. //
  2000. ////////////////////////////////////////////////////////////////////////////////
  2001. static short GetRealmToRecord( // Returns 0 on success, negative on error, 1 if
  2002. // not subpathable (i.e., returned path is full path).
  2003. char* pszRealmFile,
  2004. short sMaxFileLen)
  2005. {
  2006. short sResult = 0;
  2007. // Static so dialog will "remember" the previously-used name
  2008. static char szFile[RSP_MAX_PATH] = "";
  2009. // If not yet used, start out in appropriate directory
  2010. // if (szFile[0] == '\0')
  2011. strcpy(szFile, FullPathHD(LEVEL_DIR));
  2012. // Display open dialog to let user choose a file
  2013. sResult = SubPathOpenBox(FullPathHD(""), "Choose Realm To Record", szFile, szFile, sizeof(szFile), "rlm");
  2014. if (sResult >= 0)
  2015. {
  2016. // Convert path to RSPiX path.
  2017. char* pszFullPath = rspPathFromSystem(szFile);
  2018. // Check if result will fit into specified buffer
  2019. if (strlen(pszFullPath) < sMaxFileLen)
  2020. {
  2021. strcpy(pszRealmFile, pszFullPath);
  2022. }
  2023. else
  2024. {
  2025. sResult = -1;
  2026. TRACE("GetRealmToRecord(): File name too long to return in specified buffer!\n");
  2027. }
  2028. }
  2029. return sResult;
  2030. }
  2031. ////////////////////////////////////////////////////////////////////////////////
  2032. //
  2033. // Get a subpath relative to the specified game path.
  2034. //
  2035. ////////////////////////////////////////////////////////////////////////////////
  2036. extern short SubPathOpenBox( // Returns 0 on success, negative on error, 1 if
  2037. // not subpathable (i.e., returned path is full path).
  2038. char* pszFullPath, // In: Full path to be relative to (system format).
  2039. char* pszBoxTitle, // In: Title of box.
  2040. char* pszDefFileName, // In: Default filename (system format).
  2041. char* pszChosenFileName, // Out: User's choice (system format).
  2042. short sStrSize, // In: Amount of memory pointed to by pszChosenFileName.
  2043. char* pszFilter /*= NULL*/) // In: If not NULL, '.' delimited extension based filename
  2044. // filter specification. Ex: ".cpp.h.exe.lib" or "cpp.h.exe.lib"
  2045. // Note: Cannot use '.' in filter. Preceding '.' ignored.
  2046. {
  2047. short sResult;
  2048. char szBasePath[RSP_MAX_PATH];
  2049. long lBasePathLen = strlen(pszFullPath);
  2050. if (lBasePathLen < sizeof(szBasePath) )
  2051. {
  2052. strcpy(szBasePath, pszFullPath);
  2053. // Get index to last character
  2054. short sLastIndex = lBasePathLen;
  2055. if (sLastIndex > 0)
  2056. sLastIndex--;
  2057. #ifdef WIN32
  2058. // If base path doesn't end with a slash, add one
  2059. if (szBasePath[sLastIndex] != RSP_SYSTEM_PATH_SEPARATOR)
  2060. {
  2061. if ((sLastIndex + 2) < RSP_MAX_PATH)
  2062. {
  2063. szBasePath[sLastIndex+1] = RSP_SYSTEM_PATH_SEPARATOR;
  2064. szBasePath[sLastIndex+2] = 0;
  2065. }
  2066. else
  2067. {
  2068. sResult = -1;
  2069. TRACE("SubPathOpenBox(): Path would've exceed max length with separator tacked on!\n");
  2070. }
  2071. }
  2072. #else
  2073. // If base path ends with a colon, get rid of it
  2074. if (szBasePath[sLastIndex] == RSP_SYSTEM_PATH_SEPARATOR)
  2075. szBasePath[sLastIndex] = 0;
  2076. #endif
  2077. char szChosenFileName[RSP_MAX_PATH];
  2078. // Display open dialog to let user choose a file
  2079. sResult = rspOpenBox(pszBoxTitle, pszDefFileName, szChosenFileName, sizeof(szChosenFileName), pszFilter);
  2080. if (sResult == 0)
  2081. {
  2082. // Attempt to remove path from the specified name
  2083. long lFullPathLen = strlen(szBasePath);
  2084. if (rspStrnicmp(szChosenFileName, szBasePath, lFullPathLen) == 0)
  2085. {
  2086. // Copy sub path to destination.
  2087. strcpy(pszChosenFileName, szChosenFileName + lFullPathLen);
  2088. }
  2089. else
  2090. {
  2091. // Not subpathable.
  2092. sResult = 1;
  2093. // Return fullpath.
  2094. // Copy full path to destination.
  2095. strcpy(pszChosenFileName, szChosenFileName);
  2096. }
  2097. }
  2098. }
  2099. else
  2100. {
  2101. sResult = -2;
  2102. TRACE("SubPathOpenBox(): pszFullPath string too long.\n");
  2103. }
  2104. return sResult;
  2105. }
  2106. ////////////////////////////////////////////////////////////////////////////////
  2107. //
  2108. // Get name of demo file
  2109. //
  2110. ////////////////////////////////////////////////////////////////////////////////
  2111. static short GetDemoFile(
  2112. char* pszDemoFile,
  2113. short sMaxFileLen)
  2114. {
  2115. short sResult = 0;
  2116. // Static so dialog will "remember" the previously-used name
  2117. static char szFile[RSP_MAX_PATH] = "";
  2118. // If not yet used, start out in appropriate directory
  2119. if (szFile[0] == '\0')
  2120. strcpy(szFile, FullPathVD(DEMO_DIR));
  2121. // Display save dialog to let user choose a file
  2122. sResult = rspSaveBox(g_pszSaveDemoTitle, szFile, szFile, sizeof(szFile), DEMO_EXT);
  2123. if (sResult == 0)
  2124. {
  2125. // Check if result will fit into specified buffer
  2126. if (strlen(szFile) < sMaxFileLen)
  2127. {
  2128. strcpy(pszDemoFile, szFile);
  2129. }
  2130. else
  2131. {
  2132. sResult = -1;
  2133. TRACE("GetDemoFile(): File name too long to return in specified buffer!\n");
  2134. }
  2135. }
  2136. return sResult;
  2137. }
  2138. ////////////////////////////////////////////////////////////////////////////////
  2139. // Macro to get the sound SAK and (for the event there is no SAK) dir path.
  2140. ////////////////////////////////////////////////////////////////////////////////
  2141. inline void GetSoundPaths( // Returns nothing.
  2142. long lSamplesPerSec, // In: The sample rate in samples per second.
  2143. long lBitsPerSample, // In: The number of bits per sample.
  2144. char* pszSakPath, // Out: The subpath and name of the sound SAK.
  2145. // Should be able to store at least RSP_MAX_PATH
  2146. // characters here.
  2147. char* pszNoSakDir) // Out: The full path of the sound dir to use when
  2148. // there is no sak.
  2149. // Should be able to store at least RSP_MAX_PATH
  2150. // characters here.
  2151. {
  2152. // Make the SAK and base path name.
  2153. char szAudioResDescriptor[256];
  2154. sprintf(
  2155. szAudioResDescriptor,
  2156. "%ld%c%ld",
  2157. lSamplesPerSec,
  2158. AUDIO_SAK_SEPARATOR_CHAR,
  2159. lBitsPerSample);
  2160. // Create the samples SAK sub path.
  2161. strcpy(pszSakPath, SAMPLES_SAK_SUBDIR);
  2162. strcat(pszSakPath, szAudioResDescriptor);
  2163. strcat(pszSakPath, ".sak");
  2164. // Create the samples NO SAK sub path.
  2165. char szSamplesNoSakSubPath[RSP_MAX_PATH];
  2166. strcpy(szSamplesNoSakSubPath, "sound/");
  2167. strcat(szSamplesNoSakSubPath, szAudioResDescriptor);
  2168. // Note that g_GameSettings.m_szNoSakDir is already system
  2169. // specific and already contains the appropriate ending
  2170. // path delimiter, if any, appropriate to the current
  2171. // platform.
  2172. strcpy(pszNoSakDir, g_GameSettings.m_szNoSakDir);
  2173. strcat(pszNoSakDir, rspPathToSystem(szSamplesNoSakSubPath) );
  2174. }
  2175. ////////////////////////////////////////////////////////////////////////////////
  2176. //
  2177. // Open SAKs or set equivalent base paths.
  2178. //
  2179. ////////////////////////////////////////////////////////////////////////////////
  2180. static short OpenSaks(void)
  2181. {
  2182. short sResult = 0; // Assume success.
  2183. // Set base paths.
  2184. g_resmgrShell.SetBasePath(g_GameSettings.m_szNoSakDir);
  2185. g_resmgrGame.SetBasePath(g_GameSettings.m_szNoSakDir);
  2186. g_resmgrRes.SetBasePath(FullPath(GAME_PATH_HD, "") );
  2187. // Attempt to load the Game SAK . . .
  2188. if (g_resmgrGame.OpenSak(FullPath(GAME_PATH_GAME, GAME_SAK_FILENAME) ) == 0)
  2189. {
  2190. }
  2191. // Attempt to load the Shell SAK . . .
  2192. if ((g_resmgrShell.OpenSak(FullPath(GAME_PATH_HD, SHELL_SAK_FILENAME) ) == 0) ||
  2193. (g_resmgrShell.OpenSak(FullPath(GAME_PATH_VD, SHELL_SAK_FILENAME) ) == 0))
  2194. {
  2195. }
  2196. ////////////////////////////////////////////////////////////////////////////
  2197. // The Samples res directory and SAK filename are based on the current
  2198. // audio mode.
  2199. ////////////////////////////////////////////////////////////////////////////
  2200. // Get the current audio mode, if any.
  2201. short sInSoundMode;
  2202. long lSamplesPerSec;
  2203. long lDevBitsPerSample;
  2204. long lSrcBitsPerSample;
  2205. long lMixBitsPerSample;
  2206. if (RMix::GetMode( // Returns 0 on success;
  2207. // nonzero if no mode.
  2208. &lSamplesPerSec, // Sample rate in samples per second
  2209. // returned here, if not NULL.
  2210. &lDevBitsPerSample, // Bits per sample of device,
  2211. // returned here, if not NULL.
  2212. NULL, // Number of channels (1 == mono,
  2213. // 2 == stereo) returned here,
  2214. // if not NULL.
  2215. NULL, // Amount of time in ms to lead the
  2216. // current play cursor returned here,
  2217. // if not NULL. This could also be
  2218. // described as the maximum amount of
  2219. // time in ms that can occur between
  2220. // calls to rspDoSound.
  2221. NULL, // Maximum buffer time. This is the amt
  2222. // that *plBufferTime can be increased to.
  2223. // This is indicative of how much space
  2224. // was/will-be allocated for the sound
  2225. // output device on rspLockSoundOut.
  2226. &lMixBitsPerSample, // Bits per sample at which samples are
  2227. // mixed, if not NULL.
  2228. &lSrcBitsPerSample) // Bits per sample at which samples must
  2229. // be to be mixed (0 if no requirement),
  2230. // if not NULL.
  2231. == 0)
  2232. {
  2233. // Sample quality values set by rspGetSoundOutMode().
  2234. // If no pref on src bits . . .
  2235. if (lSrcBitsPerSample == 0)
  2236. {
  2237. // Set it to the mix quality. This is a just in case as currently
  2238. // these are always the same as set in main.cpp.
  2239. lSrcBitsPerSample = lMixBitsPerSample;
  2240. }
  2241. // Note that there is a real sound mode.
  2242. sInSoundMode = TRUE;
  2243. }
  2244. else
  2245. {
  2246. // In the case that there is no audio mode we still need to access one set of
  2247. // samples so we assume Vanilla settings b/c these use less memory.
  2248. lSamplesPerSec = MAIN_VANILLA_AUDIO_RATE;
  2249. lDevBitsPerSample = lMixBitsPerSample = lSrcBitsPerSample = MAIN_VANILLA_AUDIO_BITS;
  2250. // Note that there is no real sound mode.
  2251. sInSoundMode = FALSE;
  2252. }
  2253. // Actual sample rates can (and do) vary from system to system. We need
  2254. // to "round" the actual rate so it matches one of the specific rates that
  2255. // we're looking for. The variance we allow for is +/- one percent.
  2256. if ((lSamplesPerSec >= (11025 - 110)) && (lSamplesPerSec <= (11025 + 110)))
  2257. lSamplesPerSec = 11025;
  2258. else if ((lSamplesPerSec >= (22050 - 220)) && (lSamplesPerSec <= (22050 + 220)))
  2259. lSamplesPerSec = 22050;
  2260. else if ((lSamplesPerSec >= (44100 - 441)) && (lSamplesPerSec <= (44100 + 441)))
  2261. lSamplesPerSec = 44100;
  2262. else
  2263. {
  2264. TRACE("OpenSaks(): Unsupported sample rate: %ld!\n", (long)lSamplesPerSec);
  2265. ASSERT(0);
  2266. }
  2267. char szSamplesSakSubPath[RSP_MAX_PATH];
  2268. char szSamplesNoSakFullPath[RSP_MAX_PATH];
  2269. GetSoundPaths(lSamplesPerSec, lSrcBitsPerSample, szSamplesSakSubPath, szSamplesNoSakFullPath);
  2270. // Attempt to load the Sample SAK . . .
  2271. if (g_resmgrSamples.OpenSak(FullPath(GAME_PATH_SOUND, szSamplesSakSubPath) ) == 0)
  2272. {
  2273. // Wahoo. No worries.
  2274. }
  2275. // Otherwise, if there's a dir for files when there's no SAK . . .
  2276. else if (g_GameSettings.m_szNoSakDir[0] != '\0')
  2277. {
  2278. g_resmgrSamples.SetBasePath(szSamplesNoSakFullPath);
  2279. }
  2280. // Otherwise, . . .
  2281. else
  2282. {
  2283. // If there's a sound mode that was successfully set up . . .
  2284. if (sInSoundMode)
  2285. {
  2286. char szSoundQuality[256];
  2287. sprintf(szSoundQuality, "%.3f kHz, %hd Bit",
  2288. (float)lSamplesPerSec/(float)1000,
  2289. (short)lSrcBitsPerSample,
  2290. (MAIN_AUDIO_CHANNELS == 1) ? "Mono" : "Stereo");
  2291. rspMsgBox(
  2292. RSP_MB_ICN_INFO | RSP_MB_BUT_OK,
  2293. g_pszAppName,
  2294. g_pszCannotOpenSoundFiles_s_s,
  2295. szSoundQuality,
  2296. szSoundQuality);
  2297. // Failure.
  2298. sResult = 1;
  2299. }
  2300. else
  2301. {
  2302. // We've one last chance. Try 'em all. Technically, we've already
  2303. // tried 22050, 8 (the 'Vanilla mode') if we got here, but let's try
  2304. // them all to make sure.
  2305. struct
  2306. {
  2307. long lSamplesPerSec;
  2308. long lBitsPerSample;
  2309. } amodes[] =
  2310. {
  2311. // Put the smaller ones first b/c they use less memory.
  2312. { 22050, 8 },
  2313. { 11025, 16 },
  2314. { 22050, 16 },
  2315. };
  2316. short sModeIndex;
  2317. bool bSakFound = false;
  2318. for (sModeIndex = 0; sModeIndex < NUM_ELEMENTS(amodes) && bSakFound == false; sModeIndex++)
  2319. {
  2320. // Get the appropriate sample SAK name.
  2321. GetSoundPaths(amodes[sModeIndex].lSamplesPerSec, amodes[sModeIndex].lBitsPerSample, szSamplesSakSubPath, szSamplesNoSakFullPath);
  2322. // Attempt to load the Sample SAK . . .
  2323. if (g_resmgrSamples.OpenSak(FullPath(GAME_PATH_SOUND, szSamplesSakSubPath) ) == 0)
  2324. {
  2325. // Set values to determine SampleMaster quality.
  2326. // This is probably not necessary when using no sound but let's be safe.
  2327. lSamplesPerSec = amodes[sModeIndex].lSamplesPerSec;
  2328. lSrcBitsPerSample = amodes[sModeIndex].lBitsPerSample;
  2329. // Got one.
  2330. bSakFound = true;
  2331. }
  2332. }
  2333. // If no SAK found . . .
  2334. if (bSakFound == false)
  2335. {
  2336. rspMsgBox(
  2337. RSP_MB_ICN_INFO | RSP_MB_BUT_OK,
  2338. g_pszAppName,
  2339. g_pszNoSoundFiles);
  2340. // Failure.
  2341. sResult = 1;
  2342. }
  2343. }
  2344. }
  2345. // Set the appropriate quality.
  2346. g_GameSettings.m_eCurSoundQuality = (SampleMaster::SoundQuality)( ( (lSamplesPerSec / 11025) - 1) * 2 + ( (lDevBitsPerSample / 8) - 1) );
  2347. // Set volumes based on quality's category adjustor.
  2348. short i;
  2349. for (i = 0; i < SampleMaster::MAX_NUM_SOUND_CATEGORIES; i++)
  2350. {
  2351. SetCategoryVolume((SampleMaster::SoundCategory)i, g_GameSettings.m_asCategoryVolumes[i] );
  2352. }
  2353. return sResult;
  2354. }
  2355. ////////////////////////////////////////////////////////////////////////////////
  2356. //
  2357. // Close SAKs and/or Purge() them.
  2358. //
  2359. ////////////////////////////////////////////////////////////////////////////////
  2360. static void CloseSaks(void)
  2361. {
  2362. g_resmgrShell.Purge();
  2363. g_resmgrShell.CloseSak();
  2364. g_resmgrGame.Purge();
  2365. g_resmgrGame.CloseSak();
  2366. g_resmgrSamples.Purge();
  2367. g_resmgrSamples.CloseSak();
  2368. g_resmgrRes.Purge();
  2369. g_resmgrRes.CloseSak();
  2370. }
  2371. ////////////////////////////////////////////////////////////////////////////////
  2372. //
  2373. // Load game data that we want around the entire game.
  2374. //
  2375. // I'm not entirely against just keeping all the game data global since that's
  2376. // the way it's always been done. On the other hand, I kind of like the idea
  2377. // of using a similar mechanism for loading game data as I did for doing the
  2378. // game settings. It allows various modules to automatically be included in
  2379. // the loading process, and keeps the data local to the module. Back on the
  2380. // first hand, we'll probably be grouping data into large, wad-type files, so
  2381. // somehow it seems less compelling to try to keep the data separated within
  2382. // the app.
  2383. //
  2384. ////////////////////////////////////////////////////////////////////////////////
  2385. static short LoadAssets(void)
  2386. {
  2387. // Load font.
  2388. if (g_fontBig.Load(FullPath(GAME_PATH_VD, BIG_FONT_FILE)) != 0)
  2389. {
  2390. TRACE("GameLoadAssets(): Error loading font: %s !\n", FullPath(GAME_PATH_VD, BIG_FONT_FILE));
  2391. return -1;
  2392. }
  2393. // Load font, the smaller.
  2394. if (g_fontPostal.Load(FullPath(GAME_PATH_VD, POSTAL_FONT_FILE) ) != 0)
  2395. {
  2396. TRACE("GameLoadAssets(): Error loading font: %s !\n", FullPath(GAME_PATH_VD, POSTAL_FONT_FILE));
  2397. return -1;
  2398. }
  2399. short i;
  2400. long lTotalTime = 0;
  2401. for (i = 0; i < TitleGetNumTitles(); i++)
  2402. lTotalTime += g_GameSettings.m_alTitleDurations[i];
  2403. // Fake lots of loading with a simple timing loop
  2404. long lTime;
  2405. long lLastTime = rspGetMilliseconds();
  2406. long lEndTime = lLastTime + lTotalTime;
  2407. do
  2408. {
  2409. lTime = rspGetMilliseconds();
  2410. UpdateSystem();
  2411. DoTitle(lTime - lLastTime);
  2412. lLastTime = lTime;
  2413. } while (lTime < lEndTime && rspGetQuitStatus() == FALSE);
  2414. // If we get this far, return success
  2415. return 0;
  2416. }
  2417. ////////////////////////////////////////////////////////////////////////////////
  2418. //
  2419. // Unload game data that was loaded by GameLoadAssets().
  2420. //
  2421. ////////////////////////////////////////////////////////////////////////////////
  2422. static short UnloadAssets(void)
  2423. {
  2424. return 0;
  2425. }
  2426. ////////////////////////////////////////////////////////////////////////////////
  2427. //
  2428. // Callback for the "Start Single Player Game" menu
  2429. //
  2430. ////////////////////////////////////////////////////////////////////////////////
  2431. extern void Game_StartSinglePlayerGame(
  2432. short sMenuItem)
  2433. {
  2434. // we reset these as we go along.
  2435. const bool usedCheats = ((Flag_Achievements & FLAG_USED_CHEATS) != 0);
  2436. Flag_Achievements = FLAG_USED_M16 | FLAG_KILLED_EVERYTHING | FLAG_KILLED_ONLY_HOSTILES | FLAG_HIGHEST_DIFFICULTY;
  2437. if (usedCheats)
  2438. Flag_Achievements |= FLAG_USED_CHEATS;
  2439. playthroughMS = 0;
  2440. // If its a spawn version, then don't allow them to play single player
  2441. // games, and to make it harder, we will take out some of the code for
  2442. // single player games so its harder to hack back in.
  2443. #ifndef SPAWN
  2444. switch (sMenuItem)
  2445. {
  2446. // "ORIGINAL LEVELS"
  2447. case 0:
  2448. m_action = ACTION_PLAY_SINGLE;
  2449. m_sRealmNum = 0;
  2450. m_szRealmFile[0] = 0;
  2451. m_bJustOneRealm = false;
  2452. break;
  2453. #if defined(START_MENU_ADDON_ITEM)
  2454. #define START_MENU_ID_OFFSET 0
  2455. // "ADD-ON LEVELS"
  2456. case 1:
  2457. m_action = ACTION_PLAY_ADDON;
  2458. m_sRealmNum = 0;
  2459. m_szRealmFile[0] = 0;
  2460. m_bJustOneRealm = false;
  2461. break;
  2462. #else
  2463. #define START_MENU_ID_OFFSET -1
  2464. #endif
  2465. #ifdef MOBILE
  2466. case 2 + START_MENU_ID_OFFSET:
  2467. m_action = ACTION_CONTINUE_GAME;
  2468. break;
  2469. case 3 + START_MENU_ID_OFFSET:
  2470. #else
  2471. // "LOAD" game
  2472. case 2 + START_MENU_ID_OFFSET:
  2473. #endif
  2474. #ifndef LOADLEVEL_REMOVED
  2475. {
  2476. // Static so dialog will "remember" the previously-used name
  2477. static char szFile[RSP_MAX_PATH] = "";
  2478. // If not yet used, start out in appropriate directory
  2479. if (szFile[0] == '\0')
  2480. strcpy(szFile, FullPathHD(LEVEL_DIR));
  2481. if (rspOpenBox("Load Realm", szFile, szFile, sizeof(szFile), ".rlm") == 0)
  2482. {
  2483. // Convert path from system format to rspix format so it matches the
  2484. // way we normally call Play(), which is with a rspix path.
  2485. rspPathFromSystem(szFile, m_szRealmFile);
  2486. m_action = ACTION_PLAY_SINGLE;
  2487. m_sRealmNum = -1;
  2488. m_bJustOneRealm = true;
  2489. }
  2490. break;
  2491. }
  2492. // For the final version, the LOAD above will actually be this, but
  2493. // it is still useful for testing the way it is now, so I'll add this
  2494. // as a separate option - Load Saved Game
  2495. case 3 + START_MENU_ID_OFFSET:
  2496. #endif
  2497. m_action = ACTION_LOAD_GAME;
  2498. break;
  2499. #if (TARGET == POSTAL_2015)
  2500. case 3 + START_MENU_ID_OFFSET:
  2501. Game_StartChallengeGame(0);
  2502. break;
  2503. #endif
  2504. }
  2505. // The main game loop resets the demo timer whenever it notices any user input.
  2506. // However, when the user is in a dialog or message box, the OS handles all the
  2507. // user input, and the main game loop won't know anything about what's going on
  2508. // in there. If the user spends a long time in there, the demo timer will
  2509. // expire. We don't want that to happen, so we manually reset the demo timer
  2510. // here in recognition of the fact that some kind of user input obviously occurred.
  2511. ResetDemoTimer();
  2512. #endif // SPAWN
  2513. }
  2514. ////////////////////////////////////////////////////////////////////////////////
  2515. //
  2516. // Callback for the "Start MultiPlayer Game" menu
  2517. //
  2518. ////////////////////////////////////////////////////////////////////////////////
  2519. extern bool Game_StartMultiPlayerGame(
  2520. short sMenuItem)
  2521. {
  2522. bool bAccept = true;
  2523. #if defined(MULTIPLAYER_DISABLED)
  2524. rspMsgBox(RSP_MB_ICN_INFO | RSP_MB_BUT_OK, APP_NAME, g_pszMultiplayerDisabled);
  2525. bAccept = false;
  2526. #else
  2527. // Do nothing if multiplayer is available
  2528. #endif
  2529. // The main game loop resets the demo timer whenever it notices any user input.
  2530. // However, when the user is in a dialog or message box, the OS handles all the
  2531. // user input, and the main game loop won't know anything about what's going on
  2532. // in there. If the user spends a long time in there, the demo timer will
  2533. // expire. We don't want that to happen, so we manually reset the demo timer
  2534. // here in recognition of the fact that some kind of user input obviously occurred.
  2535. ResetDemoTimer();
  2536. return bAccept;
  2537. }
  2538. ////////////////////////////////////////////////////////////////////////////////
  2539. //
  2540. // Callback for the "Join MultiPlayer Game" menu
  2541. //
  2542. ////////////////////////////////////////////////////////////////////////////////
  2543. extern void Game_JoinMultiPlayerGame(
  2544. short sMenuItem)
  2545. {
  2546. switch (sMenuItem)
  2547. {
  2548. // "BROWSE"
  2549. case 0:
  2550. m_action = ACTION_PLAY_BROWSE;
  2551. break;
  2552. // "CONNECT"
  2553. case 1:
  2554. m_action = ACTION_PLAY_CONNECT;
  2555. break;
  2556. }
  2557. // The main game loop resets the demo timer whenever it notices any user input.
  2558. // However, when the user is in a dialog or message box, the OS handles all the
  2559. // user input, and the main game loop won't know anything about what's going on
  2560. // in there. If the user spends a long time in there, the demo timer will
  2561. // expire. We don't want that to happen, so we manually reset the demo timer
  2562. // here in recognition of the fact that some kind of user input obviously occurred.
  2563. ResetDemoTimer();
  2564. }
  2565. ////////////////////////////////////////////////////////////////////////////////
  2566. //
  2567. // Callback for the "Host MultiPlayer Game" menu
  2568. //
  2569. ////////////////////////////////////////////////////////////////////////////////
  2570. extern void Game_HostMultiPlayerGame(
  2571. short sMenuItem)
  2572. {
  2573. switch (sMenuItem)
  2574. {
  2575. // "HOST"
  2576. case 0:
  2577. m_action = ACTION_PLAY_HOST;
  2578. break;
  2579. }
  2580. // The main game loop resets the demo timer whenever it notices any user input.
  2581. // However, when the user is in a dialog or message box, the OS handles all the
  2582. // user input, and the main game loop won't know anything about what's going on
  2583. // in there. If the user spends a long time in there, the demo timer will
  2584. // expire. We don't want that to happen, so we manually reset the demo timer
  2585. // here in recognition of the fact that some kind of user input obviously occurred.
  2586. ResetDemoTimer();
  2587. }
  2588. ////////////////////////////////////////////////////////////////////////////////
  2589. //
  2590. // Callback for the "Start Demo Game" menu
  2591. //
  2592. ////////////////////////////////////////////////////////////////////////////////
  2593. extern void Game_StartDemoGame(
  2594. short sMenuItem)
  2595. {
  2596. char* pszDemoFile = NULL;
  2597. char szLevelDir[RSP_MAX_PATH] = "";
  2598. char szTitle[256] = "";
  2599. switch (sMenuItem)
  2600. {
  2601. // Browse for and Playback demo
  2602. case 0:
  2603. {
  2604. // Get the filename of the demo to load.
  2605. static char szFile[RSP_MAX_PATH] = "";
  2606. sprintf(szLevelDir, "%s", DEMO_LEVEL_DIR);
  2607. pszDemoFile = szFile;
  2608. // If not yet used, start out in appropriate directory
  2609. if (pszDemoFile[0] == '\0')
  2610. strcpy(pszDemoFile, FullPathHD(szLevelDir) );
  2611. // Display open dialog to let user choose a realm file
  2612. sprintf(szTitle, "%s", DEMO_OPEN_TITLE);
  2613. if (rspOpenBox(szTitle, pszDemoFile, m_szDemoFile, sizeof(m_szDemoFile), ".dmo") == 0)
  2614. {
  2615. m_action = ACTION_DEMO_PLAYBACK;
  2616. }
  2617. }
  2618. break;
  2619. // Play auto demo.
  2620. case 1:
  2621. // Clear demo filename. This signifies that we should play one of
  2622. // the auto demos.
  2623. m_szDemoFile[0] = '\0';
  2624. m_action = ACTION_DEMO_PLAYBACK;
  2625. break;
  2626. // Record demo
  2627. case 2:
  2628. m_action = ACTION_DEMO_RECORD;
  2629. break;
  2630. }
  2631. // The main game loop resets the demo timer whenever it notices any user input.
  2632. // However, when the user is in a dialog or message box, the OS handles all the
  2633. // user input, and the main game loop won't know anything about what's going on
  2634. // in there. If the user spends a long time in there, the demo timer will
  2635. // expire. We don't want that to happen, so we manually reset the demo timer
  2636. // here in recognition of the fact that some kind of user input obviously occurred.
  2637. ResetDemoTimer();
  2638. }
  2639. ////////////////////////////////////////////////////////////////////////////////
  2640. //
  2641. // Callback for the "Buy" option on the Main Menu
  2642. //
  2643. ////////////////////////////////////////////////////////////////////////////////
  2644. extern void Game_Buy(void)
  2645. {
  2646. rspMsgBox(RSP_MB_ICN_INFO | RSP_MB_BUT_OK, APP_NAME, g_pszBuy);
  2647. // The main game loop resets the demo timer whenever it notices any user input.
  2648. // However, when the user is in a dialog or message box, the OS handles all the
  2649. // user input, and the main game loop won't know anything about what's going on
  2650. // in there. If the user spends a long time in there, the demo timer will
  2651. // expire. We don't want that to happen, so we manually reset the demo timer
  2652. // here in recognition of the fact that some kind of user input obviously occurred.
  2653. ResetDemoTimer();
  2654. }
  2655. ////////////////////////////////////////////////////////////////////////////////
  2656. //
  2657. // Callback for the "Editor" option on the Main Menu
  2658. //
  2659. ////////////////////////////////////////////////////////////////////////////////
  2660. extern void Game_StartEditor(void)
  2661. {
  2662. #if defined(EDITOR_DISABLED)
  2663. rspMsgBox(RSP_MB_ICN_INFO | RSP_MB_BUT_OK, APP_NAME, g_pszEditorDisabled);
  2664. #else
  2665. m_action = ACTION_EDITOR;
  2666. #endif
  2667. // The main game loop resets the demo timer whenever it notices any user input.
  2668. // However, when the user is in a dialog or message box, the OS handles all the
  2669. // user input, and the main game loop won't know anything about what's going on
  2670. // in there. If the user spends a long time in there, the demo timer will
  2671. // expire. We don't want that to happen, so we manually reset the demo timer
  2672. // here in recognition of the fact that some kind of user input obviously occurred.
  2673. ResetDemoTimer();
  2674. }
  2675. ////////////////////////////////////////////////////////////////////////////////
  2676. //
  2677. // Callback for the "Controls" menu.
  2678. //
  2679. ////////////////////////////////////////////////////////////////////////////////
  2680. extern void Game_ControlsMenu(
  2681. short sMenuItem)
  2682. {
  2683. // Only do this if we're not currently in an action . . .
  2684. if (m_action == ACTION_NOTHING)
  2685. {
  2686. switch (sMenuItem)
  2687. {
  2688. // Edit keyboard settings.
  2689. case 0:
  2690. m_action = ACTION_EDIT_INPUT_SETTINGS;
  2691. break;
  2692. // Edit mouse settings.
  2693. case 1:
  2694. m_action = ACTION_EDIT_INPUT_SETTINGS;
  2695. break;
  2696. // Edit joystick settings.
  2697. case 2:
  2698. #if defined(ALLOW_JOYSTICK)
  2699. m_action = ACTION_EDIT_INPUT_SETTINGS;
  2700. #endif // defined(ALLOW_JOYSTICK)
  2701. break;
  2702. }
  2703. // The main game loop resets the demo timer whenever it notices any user input.
  2704. // However, when the user is in a dialog or message box, the OS handles all the
  2705. // user input, and the main game loop won't know anything about what's going on
  2706. // in there. If the user spends a long time in there, the demo timer will
  2707. // expire. We don't want that to happen, so we manually reset the demo timer
  2708. // here in recognition of the fact that some kind of user input obviously occurred.
  2709. ResetDemoTimer();
  2710. }
  2711. }
  2712. ////////////////////////////////////////////////////////////////////////////////
  2713. //
  2714. // Callback for "Audio Options" menu.
  2715. //
  2716. ////////////////////////////////////////////////////////////////////////////////
  2717. extern void Game_AudioOptionsChoice( // Returns nothing.
  2718. short sMenuItem) // In: Chosen item.
  2719. {
  2720. switch (sMenuItem)
  2721. {
  2722. case 1:
  2723. m_action = ACTION_POSTAL_ORGAN;
  2724. break;
  2725. }
  2726. }
  2727. ////////////////////////////////////////////////////////////////////////////////
  2728. //
  2729. // Callback for the "Start Challenge" menu.
  2730. //
  2731. ////////////////////////////////////////////////////////////////////////////////
  2732. extern void Game_StartChallengeGame( // Returns nothing.
  2733. short sMenuItem) // In: Chosen menu item.
  2734. {
  2735. char* pszRealmFile = NULL;
  2736. char szLevelDir[RSP_MAX_PATH] = "";
  2737. char szTitle[256] = "";
  2738. switch (sMenuItem)
  2739. {
  2740. // Run the Gauntlet.
  2741. case 0:
  2742. m_action = ACTION_PLAY_CHALLENGE;
  2743. m_sRealmNum = 0;
  2744. m_szRealmFile[0] = 0;
  2745. m_bJustOneRealm = false;
  2746. break;
  2747. // Timed Challenge.
  2748. case 1:
  2749. {
  2750. // Static so dialog will "remember" the previously-used name
  2751. static char szFile[RSP_MAX_PATH] = "";
  2752. pszRealmFile = szFile;
  2753. strcpy(szLevelDir, TIMED_CHALLENGE_LEVEL_DIR);
  2754. strcpy(szTitle, TIMED_CHALLENGE_OPEN_TITLE);
  2755. break;
  2756. }
  2757. // Goal Challenge.
  2758. case 2:
  2759. {
  2760. // Static so dialog will "remember" the previously-used name
  2761. static char szFile[RSP_MAX_PATH] = "";
  2762. pszRealmFile = szFile;
  2763. strcpy(szLevelDir, GOAL_CHALLENGE_LEVEL_DIR);
  2764. strcpy(szTitle, GOAL_CHALLENGE_OPEN_TITLE);
  2765. break;
  2766. }
  2767. // Flag Challenge.
  2768. case 3:
  2769. {
  2770. // Static so dialog will "remember" the previously-used name
  2771. static char szFile[RSP_MAX_PATH] = "";
  2772. pszRealmFile = szFile;
  2773. strcpy(szLevelDir, FLAG_CHALLENGE_LEVEL_DIR);
  2774. strcpy(szTitle, FLAG_CHALLENGE_OPEN_TITLE);
  2775. break;
  2776. }
  2777. // Checkpoint Challenge.
  2778. case 4:
  2779. {
  2780. // Static so dialog will "remember" the previously-used name
  2781. static char szFile[RSP_MAX_PATH] = "";
  2782. pszRealmFile = szFile;
  2783. strcpy(szLevelDir, CHECKPOINT_CHALLENGE_LEVEL_DIR);
  2784. strcpy(szTitle, CHECKPOINT_CHALLENGE_OPEN_TITLE);
  2785. break;
  2786. }
  2787. }
  2788. if (pszRealmFile)
  2789. {
  2790. // If not yet used, start out in appropriate directory
  2791. if (pszRealmFile[0] == '\0')
  2792. //strcpy(pszRealmFile, FullPathHD(szLevelDir) );
  2793. strcpy(pszRealmFile, FullPathCD(szLevelDir) );
  2794. // Display open dialog to let user choose a realm file
  2795. if (rspOpenBox(szTitle, pszRealmFile, m_szRealmFile, sizeof(m_szRealmFile), ".rlm") == 0)
  2796. {
  2797. // Convert path from system format to rspix format so it matches the
  2798. // way we normally call Play(), which is with a rspix path.
  2799. // MASSIVE BUG IN rspPathFromSystem() on the Mac -- it doesn't allow
  2800. // the src and dst to be the same, even though the doc says it can!!!!
  2801. // Workaround is to use temporary buffer.
  2802. char szTmp[RSP_MAX_PATH];
  2803. strcpy(szTmp, m_szRealmFile);
  2804. rspPathFromSystem(szTmp, m_szRealmFile);
  2805. m_action = ACTION_PLAY_CHALLENGE;
  2806. m_sRealmNum = -1;
  2807. m_bJustOneRealm = false;
  2808. }
  2809. }
  2810. // The main game loop resets the demo timer whenever it notices any user input.
  2811. // However, when the user is in a dialog or message box, the OS handles all the
  2812. // user input, and the main game loop won't know anything about what's going on
  2813. // in there. If the user spends a long time in there, the demo timer will
  2814. // expire. We don't want that to happen, so we manually reset the demo timer
  2815. // here in recognition of the fact that some kind of user input obviously occurred.
  2816. ResetDemoTimer();
  2817. }
  2818. ////////////////////////////////////////////////////////////////////////////////
  2819. //
  2820. // Callback for the Main Menu init/kill.
  2821. //
  2822. ////////////////////////////////////////////////////////////////////////////////
  2823. extern void Game_InitMainMenu( // Returns nothing.
  2824. short sInit) // In: TRUE, if initializing; FALSE, if killing.
  2825. {
  2826. // If initializing the menu . . .
  2827. if (sInit)
  2828. {
  2829. }
  2830. // Otherwise, killing the menu.
  2831. else
  2832. {
  2833. // Abort the title/main-menu musak.
  2834. AbortSample(ms_siMusak);
  2835. // No need to reset ms_siMusak.
  2836. }
  2837. }
  2838. ////////////////////////////////////////////////////////////////////////////////
  2839. //
  2840. // Game_SavePlayersGame - Save the realm, game type, difficulty, and stockpile
  2841. //
  2842. ////////////////////////////////////////////////////////////////////////////////
  2843. extern short Game_SavePlayersGame(
  2844. char* pszSaveName, // In: Name of the save file
  2845. short sDifficulty) // In: Current realm difficulty.
  2846. {
  2847. RFile rf;
  2848. short sResult = rf.Open(pszSaveName, "wb", RFile::LittleEndian);
  2849. ULONG ulFileVersion = CRealm::FileVersion;
  2850. if (sResult == SUCCESS)
  2851. {
  2852. rf.Write(ulFileVersion);
  2853. rf.Write(sDifficulty);
  2854. rf.Write((short)m_action);
  2855. rf.Write(g_sRealmNumToSave);
  2856. g_stockpile.Save(&rf);
  2857. // new in version 48.
  2858. rf.Write(Flag_Achievements);
  2859. // new in version 49.
  2860. rf.Write(playthroughMS);
  2861. rf.Close();
  2862. }
  2863. return sResult;
  2864. }
  2865. ////////////////////////////////////////////////////////////////////////////////
  2866. //
  2867. // Game_LoadPlayersGame - Load a previously saved game, restore the settings
  2868. //
  2869. ////////////////////////////////////////////////////////////////////////////////
  2870. extern short Game_LoadPlayersGame(
  2871. char* pszSaveName, // In: Name of the saved game file to open
  2872. short* psDifficulty, // Out: Saved game realm difficulty.
  2873. ACTION* paction) // Out: Saved game action.
  2874. {
  2875. RFile rf;
  2876. short sResult = rf.Open(pszSaveName, "rb", RFile::LittleEndian);
  2877. ULONG ulFileVersion;
  2878. if (sResult == SUCCESS)
  2879. {
  2880. rf.Read(&ulFileVersion);
  2881. rf.Read(psDifficulty);
  2882. // Store as 16 bit value (in case Read() fails (we want to keep original
  2883. // functionality which read directly into m_action) ).
  2884. short sAction = (short)*paction;
  2885. // Read as 16 bit.
  2886. rf.Read(&sAction);
  2887. // Store as action.
  2888. *paction = (ACTION)sAction;
  2889. rf.Read(&m_sRealmNum);
  2890. g_stockpile.Load(&rf, ulFileVersion);
  2891. // new in version 48.
  2892. const bool usedCheats = ((Flag_Achievements & FLAG_USED_CHEATS) != 0);
  2893. Flag_Achievements = 0;
  2894. if (ulFileVersion >= 48)
  2895. rf.Read(&Flag_Achievements);
  2896. if (usedCheats)
  2897. Flag_Achievements |= FLAG_USED_CHEATS;
  2898. #if 0
  2899. printf("achievements:\n");
  2900. #define printflag(x) printf("%s: %s\n", #x, (Flag_Achievements & x) ? "true" : "false");
  2901. printflag(FLAG_USED_M16);
  2902. printflag(FLAG_USED_SHOTGUN);
  2903. printflag(FLAG_USED_DBL_SHOTGUN);
  2904. printflag(FLAG_USED_GRENADE);
  2905. printflag(FLAG_USED_ROCKET);
  2906. printflag(FLAG_USED_MOLOTOV);
  2907. printflag(FLAG_USED_NAPALM);
  2908. printflag(FLAG_USED_FLAMETHROWER);
  2909. printflag(FLAG_USED_PROXIMITY_MINE);
  2910. printflag(FLAG_USED_TIMED_MINE);
  2911. printflag(FLAG_USED_REMOTE_MINE);
  2912. printflag(FLAG_USED_BETTY_MINE);
  2913. printflag(FLAG_USED_HEATSEEKER);
  2914. printflag(FLAG_USED_SPRAY_CANNON);
  2915. printflag(FLAG_USED_DEATHWAD);
  2916. printflag(FLAG_USED_CHEATS);
  2917. printflag(FLAG_KILLED_EVERYTHING);
  2918. printflag(FLAG_KILLED_ONLY_HOSTILES);
  2919. #undef printflag
  2920. #endif
  2921. // new in version 49.
  2922. playthroughMS = -1; // disable the achievement for old save games.
  2923. if (ulFileVersion >= 49)
  2924. rf.Read(&playthroughMS);
  2925. rf.Close();
  2926. g_bTransferStockpile = true;
  2927. }
  2928. return sResult;
  2929. }
  2930. ////////////////////////////////////////////////////////////////////////////////
  2931. //
  2932. // GameEndingSequence - Start a demo of the last level
  2933. //
  2934. ////////////////////////////////////////////////////////////////////////////////
  2935. void GameEndingSequence(void)
  2936. {
  2937. // Prepare all settings for demo mode
  2938. CSettings::PreDemo();
  2939. #if VIOLENT_LOCALE
  2940. // If demo debug movie file is specified in prefs, open it now. The RFile*
  2941. // is does double-duty as a flag, where non-zero means movie mode is enabled.
  2942. RFile* pfileDemoDebugMovie = 0;
  2943. m_pfileRandom = 0;
  2944. if (strlen(g_GameSettings.m_szDemoDebugMovie) > 0)
  2945. {
  2946. m_pfileRandom = new RFile;
  2947. if (m_pfileRandom->Open(g_GameSettings.m_szDemoDebugMovie, "rb", RFile::LittleEndian) != 0)
  2948. {
  2949. delete m_pfileRandom;
  2950. m_pfileRandom = 0;
  2951. }
  2952. }
  2953. if (InputDemoInit() == 0)
  2954. {
  2955. // This is the special end of game demo, so set up the demo name
  2956. sprintf(m_szDemoFile, "%s", FullPathCD(ENDING_DEMO_NAME));
  2957. RFile fileDemo;
  2958. if (fileDemo.Open(m_szDemoFile, "rb", RFile::LittleEndian) == 0)
  2959. {
  2960. // Read name of realm file
  2961. char szRealmFile[RSP_MAX_PATH];
  2962. fileDemo.Read(szRealmFile);
  2963. // Read whether it's a full path.
  2964. short sRealmFileIsFullPath;
  2965. fileDemo.Read(&sRealmFileIsFullPath);
  2966. if (!fileDemo.Error())
  2967. {
  2968. // Load input demo data (must be BEFORE setting playback mode)
  2969. if (InputDemoLoad(&fileDemo) == 0)
  2970. {
  2971. // End menu (now that we know there were no errors)
  2972. // StopMenu();
  2973. // PalTranOff();
  2974. // bMenuActive = false;
  2975. Play(
  2976. NULL, // No client (not network game)
  2977. NULL, // No server (not network game)
  2978. INPUT_MODE_PLAYBACK, // Input mode
  2979. -1, // Always use specific realm file
  2980. szRealmFile, // Realm file to be played
  2981. false, // Don't play just one realm
  2982. false, // Not challenge mode
  2983. false, // Not new single player Add On levels
  2984. GetGameDifficulty(), // Difficulty level
  2985. false, // Rejunenate (MP only)
  2986. 0, // Time limit (MP only)
  2987. 0, // Kill limit (MP only)
  2988. 0, // Cooperative (MP only)
  2989. 0, // Use cooperative mode (MP only)
  2990. 0, // Frame time (MP only)
  2991. pfileDemoDebugMovie); // Demo mode file
  2992. }
  2993. else
  2994. {
  2995. TRACE("GameCore(): Couldn't load demo data!\n");
  2996. rspMsgBox(RSP_MB_ICN_STOP | RSP_MB_BUT_OK, g_pszAppName, g_pszFileReadError_s, (char*) m_szDemoFile);
  2997. }
  2998. }
  2999. else
  3000. {
  3001. TRACE("GameCore(): Couldn't load realm name!\n");
  3002. rspMsgBox(RSP_MB_ICN_STOP | RSP_MB_BUT_OK, g_pszAppName, g_pszFileReadError_s, (char*) m_szDemoFile);
  3003. }
  3004. fileDemo.Close();
  3005. }
  3006. else
  3007. {
  3008. TRACE("GameCore(): Couldn't open demo file: '%s'\n", m_szDemoFile);
  3009. rspMsgBox(RSP_MB_ICN_STOP | RSP_MB_BUT_OK, g_pszAppName, g_pszFileOpenError_s, (char*) m_szDemoFile);
  3010. }
  3011. // Reset demo file name for next time.
  3012. m_szDemoFile[0] = 0;
  3013. InputDemoKill();
  3014. }
  3015. if (m_pfileRandom)
  3016. {
  3017. // File may have been closed by Play() if an error occurred
  3018. if (m_pfileRandom->IsOpen())
  3019. m_pfileRandom->Close();
  3020. delete m_pfileRandom;
  3021. m_pfileRandom = 0;
  3022. }
  3023. #endif // VIOLENT_LOCALE
  3024. // Restore settings to what they were before demo mode
  3025. CSettings::PostDemo();
  3026. SetInputMode(INPUT_MODE_LIVE); // take it out of demo mode so achievements can unlock.
  3027. // Do the ending title sequence.
  3028. Title_GameEndSequence();
  3029. // Do special credits:
  3030. Credits(&g_smidFinalSceneCredits);
  3031. UnlockAchievement(ACHIEVEMENT_COMPLETE_GAME);
  3032. if (playthroughMS < MAX_PLAYTHROUGH_ACHIEVEMENT_MS)
  3033. UnlockAchievement(ACHIEVEMENT_COMPLETE_GAME_IN_X_MINUTES);
  3034. if ((Flag_Achievements & FLAG_MASK_WEAPONS) == FLAG_USED_M16)
  3035. UnlockAchievement(ACHIEVEMENT_USE_ONLY_M16);
  3036. if (Flag_Achievements & FLAG_KILLED_EVERYTHING)
  3037. UnlockAchievement(ACHIEVEMENT_KILL_EVERYTHING);
  3038. if (Flag_Achievements & FLAG_KILLED_ONLY_HOSTILES)
  3039. UnlockAchievement(ACHIEVEMENT_KILL_ONLY_HOSTILES);
  3040. if (Flag_Achievements & FLAG_HIGHEST_DIFFICULTY)
  3041. UnlockAchievement(ACHIEVEMENT_COMPLETE_GAME_ON_HARDEST);
  3042. }
  3043. ////////////////////////////////////////////////////////////////////////////////
  3044. // Returns a ptr to just the portion of the file path that specifies the file
  3045. // name (excluding the path).
  3046. ////////////////////////////////////////////////////////////////////////////////
  3047. static char* GetFileNameFromPath( // Returns file name.
  3048. char* pszFullPath) // In: File's full path.
  3049. {
  3050. // Scan back for the separator or the beginning.
  3051. char* pszIndex = pszFullPath + (strlen(pszFullPath) - 1);
  3052. while (pszIndex >= pszFullPath && *pszIndex != RSP_SYSTEM_PATH_SEPARATOR)
  3053. {
  3054. pszIndex--;
  3055. }
  3056. return (pszIndex + 1);
  3057. }
  3058. ////////////////////////////////////////////////////////////////////////////////
  3059. // Opens the synchronization log with the specified access flags if in a
  3060. // TRACENASSERT mode and synchronization logging is enabled.
  3061. // Also, opens the random log, if it is enabled via
  3062. // g_GameSettings.m_szDebugMovie or something.
  3063. ////////////////////////////////////////////////////////////////////////////////
  3064. static void OpenSynchLogs( // Returns nothing.
  3065. bool bWriteLogs) // In: true to create log, false to compare.
  3066. {
  3067. m_pfileRandom = 0;
  3068. if (strlen(g_GameSettings.m_szDemoDebugMovie) > 0)
  3069. {
  3070. m_pfileRandom = new RFile;
  3071. if (m_pfileRandom->Open(
  3072. g_GameSettings.m_szDemoDebugMovie,
  3073. bWriteLogs ? "wb" : "rb",
  3074. RFile::LittleEndian) != 0)
  3075. {
  3076. delete m_pfileRandom;
  3077. m_pfileRandom = 0;
  3078. }
  3079. }
  3080. #if defined(_DEBUG) || defined(TRACENASSERT)
  3081. ms_lSynchLogSeq = 0;
  3082. if (g_GameSettings.m_szSynchLogFile[0] != '\0')
  3083. {
  3084. if (ms_fileSynchLog.Open(
  3085. g_GameSettings.m_szSynchLogFile,
  3086. bWriteLogs ? "wb" : "rb",
  3087. RFile::LittleEndian) == 0)
  3088. {
  3089. // Success.
  3090. }
  3091. else
  3092. {
  3093. TRACE("OpenSynchLogs(): Failed to open 'if' log file \"%s\".\n",
  3094. g_GameSettings.m_szSynchLogFile);
  3095. }
  3096. }
  3097. #endif // defined(_DEBUG) || defined(TRACENASSERT)
  3098. m_bWriteLogs = bWriteLogs;
  3099. }
  3100. ////////////////////////////////////////////////////////////////////////////////
  3101. // Closes the synchronization logs, if open.
  3102. ////////////////////////////////////////////////////////////////////////////////
  3103. static void CloseSynchLogs(void) // Returns nothing.
  3104. {
  3105. if (m_pfileRandom)
  3106. {
  3107. // File may have been closed by Play() if an error occurred
  3108. if (m_pfileRandom->IsOpen())
  3109. m_pfileRandom->Close();
  3110. delete m_pfileRandom;
  3111. m_pfileRandom = 0;
  3112. }
  3113. #if defined(_DEBUG) || defined(TRACENASSERT)
  3114. if (ms_fileSynchLog.IsOpen() )
  3115. {
  3116. ms_fileSynchLog.Close();
  3117. }
  3118. #endif // defined(_DEBUG) || defined(TRACENASSERT)
  3119. }
  3120. ////////////////////////////////////////////////////////////////////////////////
  3121. // Synchronization logger -- Call this function to log an expression and a user
  3122. // value in the synch log. When active (if g_GameSettings.m_szSynchLog is
  3123. // a valid path and filename), if recording a demo, these calls are logged to
  3124. // a file including the calling file and line number. When active, if playing
  3125. // back a demo, these calls are compared to those stored in the log and, if
  3126. // a discrepancy occurs, a modal dialog box will pop up with the pertinent info
  3127. // followed by an ASSERT(0) for easy debugging.
  3128. ////////////////////////////////////////////////////////////////////////////////
  3129. extern int SynchLog( // Result of expr.
  3130. double expr, // In: Expression to evaluate.
  3131. char* pszFile, // In: Calling file.
  3132. long lLine, // In: Calling line.
  3133. char* pszExpr, // In: Original C++ source expression.
  3134. U32 u32User) // In: A user value that is intended to be consistent.
  3135. {
  3136. #if defined(_DEBUG) || defined(TRACENASSERT)
  3137. if (ms_fileSynchLog.IsOpen() )
  3138. {
  3139. if (m_bWriteLogs)
  3140. {
  3141. fprintf(
  3142. ms_fileSynchLog.m_fs,
  3143. "[Seq: %ld] %s : %ld <$%s$> == %0.8f; User == %lu\n",
  3144. ms_lSynchLogSeq++,
  3145. GetFileNameFromPath(pszFile),
  3146. lLine,
  3147. pszExpr,
  3148. expr,
  3149. u32User);
  3150. }
  3151. else
  3152. {
  3153. char szFileIn[RSP_MAX_PATH];
  3154. char szExprIn[1024];
  3155. long lLineIn;
  3156. long lSeqIn;
  3157. double exprIn;
  3158. U32 u32UserIn;
  3159. if (fscanf(
  3160. ms_fileSynchLog.m_fs,
  3161. "[Seq: %ld] %s : %ld <$%1024[^$]$> == %g; User == %lu\n",
  3162. &lSeqIn,
  3163. szFileIn,
  3164. &lLineIn,
  3165. szExprIn,
  3166. &exprIn,
  3167. &u32UserIn) == 6)
  3168. {
  3169. // Verify . . .
  3170. if ( (rspStricmp(szFileIn, GetFileNameFromPath(pszFile) ) != 0)
  3171. || (lLineIn != lLine)
  3172. || (exprIn != expr)
  3173. || (u32UserIn != u32User) )
  3174. {
  3175. char szOut[2048];
  3176. sprintf(
  3177. szOut,
  3178. "'If' sequence (%ld) mismatch!\n\n"
  3179. " Was <<%s>> at %s(%ld) which got %g; User == %lu\n\n"
  3180. " Now <<%s>> at %s(%ld) which got %g; User == %lu",
  3181. ms_lSynchLogSeq,
  3182. szExprIn,
  3183. szFileIn,
  3184. lLineIn,
  3185. exprIn,
  3186. u32UserIn,
  3187. pszExpr,
  3188. GetFileNameFromPath(pszFile),
  3189. lLine,
  3190. expr,
  3191. u32User);
  3192. TRACE("%s\n\n", szOut);
  3193. rspMsgBox(
  3194. RSP_MB_ICN_INFO | RSP_MB_BUT_OK,
  3195. "Postal",
  3196. szOut);
  3197. // Make this easy to debug
  3198. ASSERT(0);
  3199. }
  3200. ms_lSynchLogSeq++;
  3201. }
  3202. else
  3203. {
  3204. TRACE("Synch(): Error reading log file.\n");
  3205. }
  3206. }
  3207. }
  3208. #else
  3209. rspMsgBox(
  3210. RSP_MB_ICN_STOP | RSP_MB_BUT_OK,
  3211. "Postal",
  3212. "Synchronization logging is disabled for release mode for safety.\n");
  3213. #endif // defined(_DEBUG) || defined(TRACENASSERT)
  3214. return expr;
  3215. }
  3216. ////////////////////////////////////////////////////////////////////////////////
  3217. // GameGetRegistry
  3218. ////////////////////////////////////////////////////////////////////////////////
  3219. static void GameGetRegistry(void)
  3220. {
  3221. #ifdef CHECK_EXPIRATION_DATE
  3222. char szTime[40];
  3223. char szTimeEncrypt[40];
  3224. time_t lTime;
  3225. DWORD dwTimeLength;
  3226. DWORD dwSize = 255;
  3227. char szData[256];
  3228. char szName[256];
  3229. short sEncryptedKeyLength = 36;
  3230. unsigned char szKey[40];
  3231. // This is the encoded path name of the registry key where the value is stored
  3232. szKey[0] = 0x07;
  3233. szKey[1] = 0x29;
  3234. szKey[2] = 0xbd;
  3235. szKey[3] = 0x3a;
  3236. szKey[4] = 0xba;
  3237. szKey[5] = 0x22;
  3238. szKey[6] = 0xbe;
  3239. szKey[7] = 0x36;
  3240. szKey[8] = 0xf5;
  3241. szKey[9] = 0x22;
  3242. szKey[10] = 0xfb;
  3243. szKey[11] = 0x24;
  3244. szKey[12] = 0xd0;
  3245. szKey[13] = 0x3a;
  3246. szKey[14] = 0xd4;
  3247. szKey[15] = 0x71;
  3248. szKey[16] = 0xcb;
  3249. szKey[17] = 0x3e;
  3250. szKey[18] = 0xd5;
  3251. szKey[19] = 0x2a;
  3252. szKey[20] = 0xec;
  3253. szKey[21] = 0x29;
  3254. szKey[22] = 0x89;
  3255. szKey[23] = 0x30;
  3256. szKey[24] = 0x8f;
  3257. szKey[25] = 0x78;
  3258. szKey[26] = 0x83;
  3259. szKey[27] = 0x66;
  3260. szKey[28] = 0xb4;
  3261. szKey[29] = 0x40;
  3262. szKey[30] = 0x86;
  3263. szKey[31] = 0x66;
  3264. szKey[32] = 0x8f;
  3265. szKey[33] = 0x71;
  3266. szKey[34] = 0x9b;
  3267. szKey[35] = 0xfe;
  3268. szKey[36] = 0x00;
  3269. #ifdef WIN32
  3270. DWORD dwDisposition;
  3271. DWORD dwType;
  3272. DWORD dwNameSize = 255;
  3273. HKEY hkResult;
  3274. long lError;
  3275. short sEncryptedValueLength = 9;
  3276. unsigned char szIn[10];
  3277. // This is the encoded name of the registry value itendifier
  3278. szIn[0] = 0x07;
  3279. szIn[1] = 0x29;
  3280. szIn[2] = 0xa8;
  3281. szIn[3] = 0x0f;
  3282. szIn[4] = 0xa7;
  3283. szIn[5] = 0x0c;
  3284. szIn[6] = 0xa8;
  3285. szIn[7] = 0x0e;
  3286. szIn[8] = 0xfd;
  3287. szIn[9] = 0x00;
  3288. // Get the current time and convert it to a string so it can be encoded
  3289. time( &lTime );
  3290. sprintf(szTime, "%ld", lTime);
  3291. // Decrypte the registry key path so the key can be opened
  3292. Decrypt((char*) szKey, szName, sEncryptedKeyLength);
  3293. szName[sEncryptedKeyLength-2] = 0;
  3294. lError = RegCreateKeyEx(HKEY_LOCAL_MACHINE, szName, 0,
  3295. "", REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL,
  3296. &hkResult, &dwDisposition);
  3297. // Destroy the source and result.
  3298. memset(szName, 0xeb, sEncryptedKeyLength);
  3299. if (lError == ERROR_SUCCESS && lTime > g_lReleaseTime)
  3300. {
  3301. if (dwDisposition == REG_CREATED_NEW_KEY)
  3302. // Write the initial value
  3303. {
  3304. // Make sure current value is after release value
  3305. dwTimeLength = Encrypt(szTime, szTimeEncrypt, strlen(szTime));
  3306. Decrypt((char*) szIn, szName, sEncryptedValueLength);
  3307. szName[sEncryptedValueLength-2] = 0;
  3308. RegSetValueEx(hkResult, szName, 0, REG_BINARY, (unsigned char *) szTimeEncrypt, dwTimeLength);
  3309. memset(szName, 0xeb, sEncryptedValueLength);
  3310. g_lRegTime = lTime;
  3311. }
  3312. else
  3313. {
  3314. lError = RegEnumValue(hkResult, 0, szName, &dwNameSize, 0, &dwType, (unsigned char *) szData, &dwSize);
  3315. if (lError != ERROR_SUCCESS)
  3316. g_lRegTime = EXPIRATION_DATE;
  3317. else
  3318. {
  3319. if (Decrypt(szData, szTime, dwSize) == 0)
  3320. {
  3321. szTime[dwSize] = 0;
  3322. g_lRegTime = atol(szTime);
  3323. }
  3324. else
  3325. {
  3326. g_lRegTime = EXPIRATION_DATE;
  3327. }
  3328. }
  3329. }
  3330. RegCloseKey(hkResult);
  3331. }
  3332. else
  3333. // If there was an error opening the registry key, then it will trace the error
  3334. // message here.
  3335. {
  3336. LPVOID lpMsgBuf;
  3337. FormatMessage(
  3338. FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
  3339. NULL,
  3340. GetLastError(),
  3341. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
  3342. (LPTSTR) &lpMsgBuf,
  3343. 0,
  3344. NULL
  3345. );
  3346. // Display the string.
  3347. TRACE((char*) lpMsgBuf);
  3348. // Free the buffer.
  3349. LocalFree( lpMsgBuf );
  3350. // This failed either because it couldn't write, or because
  3351. // the date has been set back before the release date, so
  3352. // force this to fail
  3353. g_lRegTime = EXPIRATION_DATE;
  3354. }
  3355. #else // WIN32
  3356. // Do mac version here.
  3357. char szPath[512];
  3358. time( &lTime );
  3359. lTime -= ((365 * 70UL) + 17) * 24 * 60 * 60; // time_fudge 1900->1970
  3360. // Open a prefs file and read the value, or write the value
  3361. // if it is the first time.
  3362. RFile rfPref;
  3363. // If the file does not exist, then write the current date to the file
  3364. // as long as it is after the release date.
  3365. macGetSpecialPath(MacPreferencesFolderType, true, szPath, 254);
  3366. Decrypt((char*) szKey, szName, sEncryptedKeyLength);
  3367. sEncryptedKeyLength = MAX(0, sEncryptedKeyLength - 2);
  3368. szName[sEncryptedKeyLength] = 0;
  3369. strcat(szPath, szName+19);
  3370. if (rfPref.Open(szPath, "rb", RFile::LittleEndian) != SUCCESS)
  3371. {
  3372. if (rfPref.Open(szPath, "wb", RFile::LittleEndian) != SUCCESS)
  3373. {
  3374. // If the file cannot be created, then hose the program
  3375. g_lRegTime = EXPIRATION_DATE;
  3376. }
  3377. else
  3378. {
  3379. // If the date is set back before the release of the beta, then hose the
  3380. // program
  3381. g_lRegTime = lTime;
  3382. if (lTime < g_lReleaseTime)
  3383. g_lRegTime = lTime = EXPIRATION_DATE;
  3384. dwTimeLength = Encrypt((char*) &lTime, szTimeEncrypt, 4);
  3385. rfPref.Write(&dwTimeLength);
  3386. rfPref.Write(szTime, dwTimeLength);
  3387. rfPref.Close();
  3388. }
  3389. }
  3390. // If the prefs file is here, then open it and read the last date
  3391. else
  3392. {
  3393. rfPref.Read(&dwSize);
  3394. rfPref.Read(szData, dwSize);
  3395. rfPref.Close();
  3396. if (Decrypt(szData, (char*) &lTime, dwSize) == 0)
  3397. {
  3398. g_lRegTime = lTime;
  3399. }
  3400. else
  3401. {
  3402. g_lRegTime = EXPIRATION_DATE;
  3403. }
  3404. }
  3405. #endif // WIN32
  3406. if (g_lRegTime < RELEASE_DATE)
  3407. g_lRegTime = EXPIRATION_DATE;
  3408. #else // CHECK_EXPIRATION_DATE
  3409. g_lRegTime = 0;
  3410. #endif // CHECK_EXPIRATION_DATE
  3411. }
  3412. ////////////////////////////////////////////////////////////////////////////////
  3413. // GameSetRegistry
  3414. ////////////////////////////////////////////////////////////////////////////////
  3415. static void GameSetRegistry(void)
  3416. {
  3417. #ifdef CHECK_EXPIRATION_DATE
  3418. char szTimeEncrypt[40];
  3419. time_t lTime;
  3420. DWORD dwTimeLength;
  3421. char szName[256];
  3422. short sEncryptedKeyLength = 36;
  3423. unsigned char szKey[40];
  3424. szKey[0] = 0x00;
  3425. szKey[1] = 0x3e;
  3426. szKey[2] = 0xde;
  3427. szKey[3] = 0x28;
  3428. szKey[4] = 0xda;
  3429. szKey[5] = 0x46;
  3430. szKey[6] = 0xdd;
  3431. szKey[7] = 0x57;
  3432. szKey[8] = 0xc2;
  3433. szKey[9] = 0x34;
  3434. szKey[10] = 0xc4;
  3435. szKey[11] = 0x32;
  3436. szKey[12] = 0xe8;
  3437. szKey[13] = 0x3f;
  3438. szKey[14] = 0xf8;
  3439. szKey[15] = 0x2c;
  3440. szKey[16] = 0xe8;
  3441. szKey[17] = 0x3e;
  3442. szKey[18] = 0xe8;
  3443. szKey[19] = 0x2a;
  3444. szKey[20] = 0xdc;
  3445. szKey[21] = 0x03;
  3446. szKey[22] = 0xff;
  3447. szKey[23] = 0x14;
  3448. szKey[24] = 0xfb;
  3449. szKey[25] = 0x18;
  3450. szKey[26] = 0xfe;
  3451. szKey[27] = 0x1f;
  3452. szKey[28] = 0xc9;
  3453. szKey[29] = 0x35;
  3454. szKey[30] = 0xe0;
  3455. szKey[31] = 0x02;
  3456. szKey[32] = 0xea;
  3457. szKey[33] = 0x09;
  3458. szKey[34] = 0xf1;
  3459. szKey[35] = 0xf5;
  3460. szKey[36] = 0x00;
  3461. #ifdef WIN32
  3462. char szTime[40];
  3463. DWORD dwDisposition;
  3464. DWORD dwSize = 255;
  3465. DWORD dwNameSize = 255;
  3466. HKEY hkResult;
  3467. long lError;
  3468. short sEncryptedValueLength = 9;
  3469. unsigned char szIn[10];
  3470. szIn[0] = 0x07;
  3471. szIn[1] = 0x29;
  3472. szIn[2] = 0xa8;
  3473. szIn[3] = 0x0f;
  3474. szIn[4] = 0xa7;
  3475. szIn[5] = 0x0c;
  3476. szIn[6] = 0xa8;
  3477. szIn[7] = 0x0e;
  3478. szIn[8] = 0xfd;
  3479. szIn[9] = 0x00;
  3480. time( &lTime );
  3481. lTime = MAX(lTime, g_lRegTime);
  3482. sprintf(szTime, "%ld", lTime);
  3483. Decrypt((char*) szKey, szName, sEncryptedKeyLength);
  3484. szName[sEncryptedKeyLength-2] = 0;
  3485. lError = RegCreateKeyEx(HKEY_LOCAL_MACHINE, szName, 0,
  3486. "", REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL,
  3487. &hkResult, &dwDisposition);
  3488. memset(szName, 0xea, sEncryptedKeyLength);
  3489. if (lError == ERROR_SUCCESS)
  3490. {
  3491. // Make sure current value is after release value
  3492. dwTimeLength = Encrypt(szTime, szTimeEncrypt, strlen(szTime));
  3493. Decrypt((char*) szIn, szName, sEncryptedValueLength);
  3494. szName[sEncryptedValueLength-2] = 0;
  3495. RegSetValueEx(hkResult, szName, 0, REG_BINARY, (unsigned char *) szTimeEncrypt, dwTimeLength);
  3496. memset(szIn, 0xee, sEncryptedValueLength);
  3497. RegCloseKey(hkResult);
  3498. }
  3499. else
  3500. {
  3501. LPVOID lpMsgBuf;
  3502. FormatMessage(
  3503. FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
  3504. NULL,
  3505. GetLastError(),
  3506. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
  3507. (LPTSTR) &lpMsgBuf,
  3508. 0,
  3509. NULL
  3510. );
  3511. // Display the string.
  3512. TRACE((char*) lpMsgBuf);
  3513. // Free the buffer.
  3514. LocalFree( lpMsgBuf );
  3515. }
  3516. #else // WIN32
  3517. // Do the Mac version here
  3518. char szPath[512];
  3519. RFile rfPref;
  3520. time( &lTime );
  3521. lTime -= ((365 * 70UL) + 17) * 24 * 60 * 60; // time_fudge 1900->1970
  3522. // Make sure the time is after the last registered time.
  3523. lTime = MAX(lTime, (time_t) g_lRegTime);
  3524. dwTimeLength = Encrypt((char*) &lTime, szTimeEncrypt, 4);
  3525. macGetSpecialPath(MacPreferencesFolderType, true, szPath, 254);
  3526. Decrypt((char*) szKey, szName, sEncryptedKeyLength);
  3527. sEncryptedKeyLength = MAX(0, sEncryptedKeyLength - 2);
  3528. szName[sEncryptedKeyLength] = 0;
  3529. strcat(szPath, szName+19);
  3530. if (rfPref.Open(szPath, "wb", RFile::LittleEndian) == SUCCESS)
  3531. {
  3532. rfPref.Write(&dwTimeLength);
  3533. rfPref.Write(szTimeEncrypt, dwTimeLength);
  3534. rfPref.Close();
  3535. }
  3536. // Open the prefs file and write the value here.
  3537. #endif // WIN32
  3538. #endif // CHECK_EXPIRATION_DATE
  3539. }
  3540. ////////////////////////////////////////////////////////////////////////////////
  3541. //
  3542. // Seed the random number generator
  3543. //
  3544. ////////////////////////////////////////////////////////////////////////////////
  3545. extern void SeedRand(
  3546. long lSeed)
  3547. {
  3548. m_lRandom = lSeed;
  3549. }
  3550. ////////////////////////////////////////////////////////////////////////////////
  3551. //
  3552. // Get a random number
  3553. //
  3554. ////////////////////////////////////////////////////////////////////////////////
  3555. #if defined(_DEBUG) || defined(TRACENASSERT)
  3556. extern long GetRandomDebug(char* FILE_MACRO, long LINE_MACRO)
  3557. {
  3558. // Get next random number
  3559. long lNewVal = (((m_lRandom = m_lRandom * 214013L + 2531011L) >> 16) & 0x7fff);
  3560. if (m_pfileRandom)
  3561. {
  3562. if (m_bWriteLogs)
  3563. {
  3564. fprintf(
  3565. m_pfileRandom->m_fs,
  3566. "%s : %ld rand = %ld\n",
  3567. GetFileNameFromPath(FILE_MACRO),
  3568. LINE_MACRO,
  3569. lNewVal);
  3570. // m_pfileRandom->Write(lNewVal);
  3571. // m_pfileRandom->Write(LINE_MACRO);
  3572. // m_pfileRandom->Write(FILE_MACRO);
  3573. }
  3574. else
  3575. {
  3576. long lSavedVal;
  3577. long lSavedLine;
  3578. char szSavedFile[1024];
  3579. fscanf(
  3580. m_pfileRandom->m_fs,
  3581. "%s : %ld rand = %ld\n",
  3582. szSavedFile,
  3583. &lSavedLine,
  3584. &lSavedVal);
  3585. // m_pfileRandom->Read(&lSavedVal);
  3586. // m_pfileRandom->Read(&lSavedLine);
  3587. // m_pfileRandom->Read(szSavedFile);
  3588. if ((lSavedVal != lNewVal) || (lSavedLine != LINE_MACRO) || (rspStricmp(szSavedFile, GetFileNameFromPath(FILE_MACRO) ) != 0))
  3589. {
  3590. rspMsgBox(
  3591. RSP_MB_ICN_INFO | RSP_MB_BUT_OK,
  3592. "Postal",
  3593. "Random number sequence mismatch!\n\n"
  3594. " Was %s(%ld) which got %ld\n\n"
  3595. " Now %s(%ld) which got %ld",
  3596. szSavedFile,
  3597. (long)lSavedLine,
  3598. (long)lSavedVal,
  3599. GetFileNameFromPath(FILE_MACRO),
  3600. LINE_MACRO,
  3601. (long)lNewVal);
  3602. // Make this easy to debug
  3603. ASSERT(0);
  3604. }
  3605. }
  3606. }
  3607. return lNewVal;
  3608. }
  3609. #else
  3610. extern long GetRandom(void)
  3611. {
  3612. // Get next random number
  3613. return (((m_lRandom = m_lRandom * 214013L + 2531011L) >> 16) & 0x7fff);
  3614. }
  3615. #endif // defined(_DEBUG) || defined(TRACENASSERT).
  3616. ////////////////////////////////////////////////////////////////////////////////
  3617. //
  3618. // Define a rand() to take the place of the stdlib version. We don't want
  3619. // anyone calling that version!
  3620. //
  3621. ////////////////////////////////////////////////////////////////////////////////
  3622. // It seems that this only works in debug mode. In release, the linker
  3623. // generates a fatal "multiple definitions of rand()" error. For now, only
  3624. // do this in debug mode.
  3625. #ifdef _DEBUG
  3626. #ifndef PLATFORM_UNIX
  3627. extern int rand(void)
  3628. {
  3629. rspMsgBox(
  3630. RSP_MB_ICN_INFO | RSP_MB_BUT_OK,
  3631. "Postal",
  3632. "The stdlib version of rand() was called. It is forbidden within this application.");
  3633. return 0;
  3634. }
  3635. #endif
  3636. #endif
  3637. ////////////////////////////////////////////////////////////////////////////////
  3638. //
  3639. // Do palette transition so menu can be displayed on top of existing background.
  3640. // NOTE: There must be a matching PalTranOff() for each PalTranOn()!!!
  3641. //
  3642. ////////////////////////////////////////////////////////////////////////////////
  3643. extern void PalTranOn(
  3644. long lTime /* = -1 */) // In: How long transition should take (or -1 for default)
  3645. {
  3646. if (lTime == -1)
  3647. lTime = NORMAL_PAL_TRAN_TIME;
  3648. StartMenuTrans(lTime);
  3649. // Do until done or keypress.
  3650. while (DoPreMenuTrans() == false)
  3651. UpdateSystem();
  3652. }
  3653. ////////////////////////////////////////////////////////////////////////////////
  3654. //
  3655. // Undo the palette transition to restore the original background colors.
  3656. //
  3657. ////////////////////////////////////////////////////////////////////////////////
  3658. extern void PalTranOff(void)
  3659. {
  3660. // Do until done
  3661. while (DoPostMenuTrans() == false)
  3662. UpdateSystem();
  3663. // Let paltran finish/clean up.
  3664. EndMenuTrans(true);
  3665. }
  3666. ////////////////////////////////////////////////////////////////////////////////
  3667. //
  3668. // Set gamma/brighten-effect value. Note that there is no value that results
  3669. // in identity. See Brightness / contrast below:
  3670. // RIGHT NOW THIS FUNCTION IS BEING USED TO CALL BRIGHTNESS CONTRAST:
  3671. //
  3672. ////////////////////////////////////////////////////////////////////////////////
  3673. extern void SetGammaLevel( // Returns nothing.
  3674. short sBase) // New brighten value.
  3675. {
  3676. // For the time being, use this to control the other function:
  3677. // Pick a contrast based on the chosen brightness.
  3678. double dBrightness = double(sBase)/128.0 - 1.0;
  3679. // Allow a larger range:
  3680. dBrightness *= 2.0;
  3681. // This was no clip
  3682. //double dContrast = MIN(1.0 - dBrightness, dBrightness + 1.0);
  3683. // Let's link contrast with brightness and clip:
  3684. double dContrast = (dBrightness + 1.0); // needs to be a value of 1 to be neutral
  3685. // With this scheme, contrast can never be more than +1.0 or less than 0.0
  3686. SetBrightnessContrast(dBrightness,dContrast);
  3687. // Update settings.
  3688. g_GameSettings.m_sGammaVal = sBase;
  3689. return; // don't set gamma for now
  3690. U8 au8RedMap[256];
  3691. U8 au8GreenMap[256];
  3692. U8 au8BlueMap[256];
  3693. short i;
  3694. short sClipVal;
  3695. for ( i = 0;
  3696. i < 256;
  3697. i++)
  3698. {
  3699. sClipVal = MAX((short)0, MIN(short(pow((double)i / 100.0, GAMMA_EXPONENT) * sBase), (short)255));
  3700. au8RedMap[i] = (U8)sClipVal;
  3701. au8GreenMap[i] = (U8)sClipVal;
  3702. au8BlueMap[i] = (U8)sClipVal;
  3703. }
  3704. // Update map.
  3705. rspSetPaletteMaps(
  3706. 0,
  3707. 256,
  3708. au8RedMap,
  3709. au8GreenMap,
  3710. au8BlueMap,
  3711. sizeof(U8));
  3712. // Update hardware through new map.
  3713. rspUpdatePalette();
  3714. // Update settings.
  3715. g_GameSettings.m_sGammaVal = sBase;
  3716. }
  3717. ////////////////////////////////////////////////////////////////////////////////
  3718. //
  3719. // Set Brightness and Contrast. Zero (neutral) values yield and identity
  3720. // curve. Valid input is from -1 to 1.
  3721. //
  3722. ////////////////////////////////////////////////////////////////////////////////
  3723. extern void SetBrightnessContrast(
  3724. double dBrightness, // -1.0 = dim, 0.0 = normal, 1.0 = bright
  3725. double dContrast // -1.0 = low contrast, 0.0 = normal, 1.0 = high
  3726. )
  3727. {
  3728. U8 au8RedMap[256];
  3729. U8 au8GreenMap[256];
  3730. U8 au8BlueMap[256];
  3731. // I will scale the ranges to within reasonable limits:
  3732. ASSERT( (dBrightness >= -1.0) || (dBrightness <= 1.0));
  3733. //ASSERT( (dContrast >= -1.0) || (dContrast <= 1.0));
  3734. //dContrast = dContrast + 1.0; // this IS the tangent value (0-2)
  3735. short i;
  3736. double dScale = 1.0/128.0;
  3737. for (i=0;i < 256; i++)
  3738. {
  3739. double dX = dScale * i - 1.0;
  3740. short sLev = short(128.0 + 128.0 * (
  3741. ( ( ( ((1.0 - dContrast) * dX) - dBrightness) * dX) + dContrast) * dX
  3742. + dBrightness));
  3743. if (sLev < 0) sLev = 0;
  3744. if (sLev > 255) sLev = 255;
  3745. au8RedMap[i] = (U8)sLev;
  3746. au8GreenMap[i] = (U8)sLev;
  3747. au8BlueMap[i] = (U8)sLev;
  3748. }
  3749. // Update map.
  3750. rspSetPaletteMaps(
  3751. 0,
  3752. 256,
  3753. au8RedMap,
  3754. au8GreenMap,
  3755. au8BlueMap,
  3756. sizeof(U8));
  3757. // Update hardware through new map.
  3758. rspUpdatePalette();
  3759. //----------------------------------
  3760. // set postal levels?
  3761. }
  3762. ////////////////////////////////////////////////////////////////////////////////
  3763. //
  3764. // Get gamma/brighten-effect value from palette map (not from settings).
  3765. //
  3766. ////////////////////////////////////////////////////////////////////////////////
  3767. extern short GetGammaLevel(void) // Returns current brighten value.
  3768. {
  3769. return g_GameSettings.m_sGammaVal;
  3770. }
  3771. ////////////////////////////////////////////////////////////////////////////////
  3772. //
  3773. // Create a full path out of a partial path.
  3774. //
  3775. // The partial path must be in "RSPiX neutral" format, which is pretty much
  3776. // like a partial Windows path, except with slashes instead of backslashes.
  3777. //
  3778. // BEWARE: The return value is a pointer to a static string, which means its
  3779. // contents are changed every time this function is called! If you're just
  3780. // going to use the string and then be done with it, this should work fine, but
  3781. // if you need the string to stick around a while, you should probably do a
  3782. // strcpy() into your own string buffer.
  3783. //
  3784. // There are several variations - pick the one you like best.
  3785. //
  3786. ////////////////////////////////////////////////////////////////////////////////
  3787. // This string is much larger than it needs to be in case the string
  3788. // size changes as a result of the conversion to system format, in which
  3789. // case our tests would miss it until it's too late. A massive overrun
  3790. // will still cause a problem, but that would never happen :)
  3791. static char m_acFullPath[RSP_MAX_PATH + RSP_MAX_PATH];
  3792. extern char* FullPath( // Returns full path in system format
  3793. short sPathType, // In: PATH_CD, PATH_HD, or PATH_VD
  3794. char* pszPartialPath) // In: Partial path in RSPiX format
  3795. {
  3796. // Start with the specified base path (copy the string from the game settings)
  3797. if (sPathType == GAME_PATH_CD)
  3798. return FullPathCD(pszPartialPath);
  3799. else if (sPathType == GAME_PATH_HD)
  3800. return FullPathHD(pszPartialPath);
  3801. else if (sPathType == GAME_PATH_VD)
  3802. return FullPathVD(pszPartialPath);
  3803. else if (sPathType == GAME_PATH_SOUND)
  3804. return FullPathSound(pszPartialPath);
  3805. else if (sPathType == GAME_PATH_GAME)
  3806. return FullPathGame(pszPartialPath);
  3807. else if (sPathType == GAME_PATH_HOODS)
  3808. return FullPathHoods(pszPartialPath);
  3809. else
  3810. {
  3811. TRACE("FullPath(): Unknown path type: %d -- I predict an ASSERT() will occur soon...\n", (short)sPathType);
  3812. ASSERT(1);
  3813. // In case they want to ignore the assert, just return a pointer to an empty string
  3814. m_acFullPath[0] = 0;
  3815. return m_acFullPath;
  3816. }
  3817. }
  3818. extern char* FullPathCD( // Returns full path in system format
  3819. char* pszPartialPath) // In: Partial path in RSPiX format
  3820. {
  3821. // Start with proper base path
  3822. ASSERT(strlen(g_GameSettings.m_pszCDPath) < RSP_MAX_PATH);
  3823. strcpy(m_acFullPath, g_GameSettings.m_pszCDPath);
  3824. // Make sure partial path isn't too long. It is possible that the conversion
  3825. // to the system format will change its length slightly, but it shouldn't be
  3826. // enough to make a real difference to this test.
  3827. ASSERT(strlen(pszPartialPath) < RSP_MAX_PATH);
  3828. // Check if the combination of the partial and base path will be too long.
  3829. ASSERT((strlen(pszPartialPath) + strlen(m_acFullPath)) < RSP_MAX_PATH);
  3830. // Convert specified partial path from rspix to system format, putting the
  3831. // result immediately following the base path
  3832. rspPathToSystem(pszPartialPath, m_acFullPath + strlen(m_acFullPath));
  3833. // Make sure result isn't too long (our buffer can handle it, but it's still a problem)
  3834. ASSERT(strlen(m_acFullPath) < RSP_MAX_PATH);
  3835. // Return pointer to full path
  3836. return m_acFullPath;
  3837. }
  3838. extern char* FullPathHD( // Returns full path in system format
  3839. const char* pszPartialPath) // In: Partial path in RSPiX format
  3840. {
  3841. // Start with proper base path
  3842. ASSERT(strlen(g_GameSettings.m_pszHDPath) < RSP_MAX_PATH);
  3843. strcpy(m_acFullPath, g_GameSettings.m_pszHDPath);
  3844. // Make sure partial path isn't too long. It is possible that the conversion
  3845. // to the system format will change its length slightly, but it shouldn't be
  3846. // enough to make a real difference to this test.
  3847. ASSERT(strlen(pszPartialPath) < RSP_MAX_PATH);
  3848. // Check if the combination of the partial and base path will be too long.
  3849. ASSERT((strlen(pszPartialPath) + strlen(m_acFullPath)) < RSP_MAX_PATH);
  3850. // Convert specified partial path from rspix to system format, putting the
  3851. // result immediately following the base path
  3852. rspPathToSystem(pszPartialPath, m_acFullPath + strlen(m_acFullPath));
  3853. // Make sure result isn't too long (our buffer can handle it, but it's still a problem)
  3854. ASSERT(strlen(m_acFullPath) < RSP_MAX_PATH);
  3855. // Return pointer to full path
  3856. return m_acFullPath;
  3857. }
  3858. extern char* FullPathVD( // Returns full path in system format
  3859. char* pszPartialPath) // In: Partial path in RSPiX format
  3860. {
  3861. // Start with proper base path
  3862. ASSERT(strlen(g_GameSettings.m_pszVDPath) < RSP_MAX_PATH);
  3863. strcpy(m_acFullPath, g_GameSettings.m_pszVDPath);
  3864. // Make sure partial path isn't too long. It is possible that the conversion
  3865. // to the system format will change its length slightly, but it shouldn't be
  3866. // enough to make a real difference to this test.
  3867. ASSERT(strlen(pszPartialPath) < RSP_MAX_PATH);
  3868. // Check if the combination of the partial and base path will be too long.
  3869. ASSERT((strlen(pszPartialPath) + strlen(m_acFullPath)) < RSP_MAX_PATH);
  3870. // Convert specified partial path from rspix to system format, putting the
  3871. // result immediately following the base path
  3872. rspPathToSystem(pszPartialPath, m_acFullPath + strlen(m_acFullPath));
  3873. // Make sure result isn't too long (our buffer can handle it, but it's still a problem)
  3874. ASSERT(strlen(m_acFullPath) < RSP_MAX_PATH);
  3875. // Return pointer to full path
  3876. return m_acFullPath;
  3877. }
  3878. extern char* FullPathSound( // Returns full path in system format
  3879. char* pszPartialPath) // In: Partial path in RSPiX format
  3880. {
  3881. // Start with proper base path
  3882. ASSERT(strlen(g_GameSettings.m_pszSoundPath) < RSP_MAX_PATH);
  3883. strcpy(m_acFullPath, g_GameSettings.m_pszSoundPath);
  3884. // Make sure partial path isn't too long. It is possible that the conversion
  3885. // to the system format will change its length slightly, but it shouldn't be
  3886. // enough to make a real difference to this test.
  3887. ASSERT(strlen(pszPartialPath) < RSP_MAX_PATH);
  3888. // Check if the combination of the partial and base path will be too long.
  3889. ASSERT((strlen(pszPartialPath) + strlen(m_acFullPath)) < RSP_MAX_PATH);
  3890. // Convert specified partial path from rspix to system format, putting the
  3891. // result immediately following the base path
  3892. rspPathToSystem(pszPartialPath, m_acFullPath + strlen(m_acFullPath));
  3893. // Make sure result isn't too long (our buffer can handle it, but it's still a problem)
  3894. ASSERT(strlen(m_acFullPath) < RSP_MAX_PATH);
  3895. // Return pointer to full path
  3896. return m_acFullPath;
  3897. }
  3898. extern char* FullPathGame( // Returns full path in system format
  3899. char* pszPartialPath) // In: Partial path in RSPiX format
  3900. {
  3901. // Start with proper base path
  3902. ASSERT(strlen(g_GameSettings.m_pszGamePath) < RSP_MAX_PATH);
  3903. strcpy(m_acFullPath, g_GameSettings.m_pszGamePath);
  3904. // Make sure partial path isn't too long. It is possible that the conversion
  3905. // to the system format will change its length slightly, but it shouldn't be
  3906. // enough to make a real difference to this test.
  3907. ASSERT(strlen(pszPartialPath) < RSP_MAX_PATH);
  3908. // Check if the combination of the partial and base path will be too long.
  3909. ASSERT((strlen(pszPartialPath) + strlen(m_acFullPath)) < RSP_MAX_PATH);
  3910. // Convert specified partial path from rspix to system format, putting the
  3911. // result immediately following the base path
  3912. rspPathToSystem(pszPartialPath, m_acFullPath + strlen(m_acFullPath));
  3913. // Make sure result isn't too long (our buffer can handle it, but it's still a problem)
  3914. ASSERT(strlen(m_acFullPath) < RSP_MAX_PATH);
  3915. // Return pointer to full path
  3916. return m_acFullPath;
  3917. }
  3918. extern char* FullPathHoods( // Returns full path in system format
  3919. char* pszPartialPath) // In: Partial path in RSPiX format
  3920. {
  3921. // Start with proper base path
  3922. ASSERT(strlen(g_GameSettings.m_pszHoodsPath) < RSP_MAX_PATH);
  3923. strcpy(m_acFullPath, g_GameSettings.m_pszHoodsPath);
  3924. // Make sure partial path isn't too long. It is possible that the conversion
  3925. // to the system format will change its length slightly, but it shouldn't be
  3926. // enough to make a real difference to this test.
  3927. ASSERT(strlen(pszPartialPath) < RSP_MAX_PATH);
  3928. // Check if the combination of the partial and base path will be too long.
  3929. ASSERT((strlen(pszPartialPath) + strlen(m_acFullPath)) < RSP_MAX_PATH);
  3930. // Convert specified partial path from rspix to system format, putting the
  3931. // result immediately following the base path
  3932. rspPathToSystem(pszPartialPath, m_acFullPath + strlen(m_acFullPath));
  3933. // Make sure result isn't too long (our buffer can handle it, but it's still a problem)
  3934. ASSERT(strlen(m_acFullPath) < RSP_MAX_PATH);
  3935. // Return pointer to full path
  3936. return m_acFullPath;
  3937. }
  3938. extern char* FullPathCustom( // Returns full path in system format
  3939. char* pszFullPath, // In: Full path in in RSPiX format.
  3940. char* pszPartialPath) // In: Partial path in RSPiX format.
  3941. {
  3942. char* pszFullSystemPath = rspPathToSystem(pszFullPath);
  3943. // Start with proper base path
  3944. ASSERT(strlen(pszFullSystemPath) < RSP_MAX_PATH);
  3945. strcpy(m_acFullPath, pszFullSystemPath);
  3946. // Make sure partial path isn't too long. It is possible that the conversion
  3947. // to the system format will change its length slightly, but it shouldn't be
  3948. // enough to make a real difference to this test.
  3949. ASSERT(strlen(pszPartialPath) < RSP_MAX_PATH);
  3950. // Check if the combination of the partial and base path will be too long.
  3951. ASSERT((strlen(pszPartialPath) + strlen(m_acFullPath)) < RSP_MAX_PATH);
  3952. // Convert specified partial path from rspix to system format, putting the
  3953. // result immediately following the base path
  3954. rspPathToSystem(pszPartialPath, m_acFullPath + strlen(m_acFullPath));
  3955. // Make sure result isn't too long (our buffer can handle it, but it's still a problem)
  3956. ASSERT(strlen(m_acFullPath) < RSP_MAX_PATH);
  3957. // Return pointer to full path
  3958. return m_acFullPath;
  3959. }
  3960. ////////////////////////////////////////////////////////////////////////////////
  3961. //
  3962. // Correctify the specified base path.
  3963. //
  3964. // First, it ensures that the path is absolute (not relative). Then, it ensures
  3965. // the path ends properly, either with or without the system-specific separator
  3966. // character, depending on which system we're running on.
  3967. //
  3968. ////////////////////////////////////////////////////////////////////////////////
  3969. short CorrectifyBasePath( // Returns 0 if successfull, non-zero otherwise
  3970. char* pszBasePath, // I/O: Base path to be corrected
  3971. short sMaxPathLen) // In: Maximum length of base path
  3972. {
  3973. short sResult = 0;
  3974. // Make sure they aren't passing an empty string, which should be be left alone
  3975. if (strlen(pszBasePath) > 0)
  3976. {
  3977. // Make sure they aren't passing a string that's already too large!
  3978. if ((strlen(pszBasePath) + 1) <= sMaxPathLen)
  3979. {
  3980. //------------------------------------------------------------------------------
  3981. // Convert path from relative to absolute. We do this by setting the current
  3982. // directory to the specified path and then asking for the current directory,
  3983. // which is always returned as an absolute path. If the path is already
  3984. // absolute to begin with, it *should* be unchanged as a result.
  3985. //------------------------------------------------------------------------------
  3986. // Get original directory (let it allocate buffer of at LEAST specified size, more if needed)
  3987. // 09/05/97, PPL
  3988. // Apparently, the getcwd function works completely (well, maybe not completely) different
  3989. // on the MAC. We need to go ahead and allocate our own buffer and free it later because
  3990. // if a NULL is passed in, a NULL is returned. This should not have any adverse effects
  3991. // on the PC version.
  3992. //char* pszOrigDir = getcwd(NULL, RSP_MAX_PATH);
  3993. char* pszOrigDir = (char*)malloc(RSP_MAX_PATH);
  3994. if (pszOrigDir != NULL)
  3995. {
  3996. // Let's go ahead and get the current working directory here, once we're sure that
  3997. // the string to store it has been properly allocated.
  3998. pszOrigDir = getcwd(pszOrigDir, RSP_MAX_PATH);
  3999. // Change to specified directory, which may be relative or absolute
  4000. if (chdir(pszBasePath) == 0)
  4001. {
  4002. // Get directory, which is always returned as absolute
  4003. if (getcwd(pszBasePath, sMaxPathLen) == NULL)
  4004. {
  4005. sResult = -1;
  4006. TRACE("CorrectifyBasePath(): Couldn't get current directory (was set to '%s')!\n", pszBasePath);
  4007. }
  4008. }
  4009. else
  4010. {
  4011. sResult = -1;
  4012. TRACE("CorrectifyBasePath(): Couldn't change to specified directory: '%s'!\n", pszBasePath);
  4013. }
  4014. // Restore original directory
  4015. if (chdir(pszOrigDir) != 0)
  4016. {
  4017. sResult = -1;
  4018. TRACE("CorrectifyBasePath(): Couldn't restore original directory: '%s'!\n", pszOrigDir);
  4019. }
  4020. // Free buffer that was allocated by getcwd()
  4021. free(pszOrigDir);
  4022. pszOrigDir = 0;
  4023. }
  4024. //------------------------------------------------------------------------------
  4025. // Ensure that path ends properly, either with or without the system-specific
  4026. // separator character, depending on which system we're on.
  4027. //------------------------------------------------------------------------------
  4028. if (sResult == 0)
  4029. {
  4030. // Get index to last character
  4031. short sLastIndex = strlen(pszBasePath);
  4032. if (sLastIndex > 0)
  4033. sLastIndex--;
  4034. #if 1 //def WIN32
  4035. // If base path doesn't end with a slash, add one
  4036. if (pszBasePath[sLastIndex] != RSP_SYSTEM_PATH_SEPARATOR)
  4037. {
  4038. if ((sLastIndex + 2) < RSP_MAX_PATH)
  4039. {
  4040. pszBasePath[sLastIndex+1] = RSP_SYSTEM_PATH_SEPARATOR;
  4041. pszBasePath[sLastIndex+2] = 0;
  4042. }
  4043. else
  4044. {
  4045. sResult = -1;
  4046. TRACE("CorrectifyBasePath(): Path would've exceed max length with separator tacked on!\n");
  4047. }
  4048. }
  4049. #else
  4050. // If base path ends with a colon, get rid of it
  4051. if (pszBasePath[sLastIndex] == RSP_SYSTEM_PATH_SEPARATOR)
  4052. pszBasePath[sLastIndex] = 0;
  4053. #endif
  4054. }
  4055. }
  4056. else
  4057. {
  4058. sResult = -1;
  4059. TRACE("CorrectifyBasePath(): Specified path is already longer than the specified maximum length!\n");
  4060. }
  4061. }
  4062. return sResult;
  4063. }
  4064. ////////////////////////////////////////////////////////////////////////////////
  4065. //
  4066. // Notification that we are about to be in the background.
  4067. //
  4068. ////////////////////////////////////////////////////////////////////////////////
  4069. static void BackgroundCall(void)
  4070. {
  4071. // TRACE("Background\n");
  4072. // Return CPU to OS.
  4073. rspSetDoSystemMode(RSP_DOSYSTEM_SLEEP);
  4074. // If cursor show level not already stored . . .
  4075. if (ms_sForegroundCursorShowLevel == INVALID_CURSOR_SHOW_LEVEL)
  4076. {
  4077. // Store current cursor show level and then make cursor visible
  4078. ms_sForegroundCursorShowLevel = rspGetMouseCursorShowLevel();
  4079. rspSetMouseCursorShowLevel(1);
  4080. }
  4081. }
  4082. ////////////////////////////////////////////////////////////////////////////////
  4083. //
  4084. // Notification that we are about to be in the foreground.
  4085. //
  4086. ////////////////////////////////////////////////////////////////////////////////
  4087. static void ForegroundCall(void)
  4088. {
  4089. // TRACE("Foreground\n");
  4090. #if defined(_DEBUG)
  4091. // Wake CPU.
  4092. rspSetDoSystemMode(RSP_DOSYSTEM_TOLERATEOS);
  4093. #else
  4094. // Return CPU to us.
  4095. rspSetDoSystemMode(RSP_DOSYSTEM_HOGCPU);
  4096. #endif
  4097. // If cursor show level stored . . .
  4098. if (ms_sForegroundCursorShowLevel != INVALID_CURSOR_SHOW_LEVEL)
  4099. {
  4100. // Restore previous cursor show level
  4101. rspSetMouseCursorShowLevel(ms_sForegroundCursorShowLevel);
  4102. ms_sForegroundCursorShowLevel = 0;
  4103. // Reset the input.
  4104. ClearLocalInput();
  4105. // Flag that we're no longer storing the show level.
  4106. ms_sForegroundCursorShowLevel = INVALID_CURSOR_SHOW_LEVEL;
  4107. }
  4108. }
  4109. ////////////////////////////////////////////////////////////////////////////////
  4110. // Returns difficulty for games.
  4111. // Note that this function is only valid once after a difficulty adjustment
  4112. // and then it goes back to the default (g_GameSettings value).
  4113. ////////////////////////////////////////////////////////////////////////////////
  4114. static short GetGameDifficulty(void) // Returns cached game difficulty.
  4115. {
  4116. short sDifficulty = g_GameSettings.m_sDifficulty;
  4117. // If there is a cached difficulty . . .
  4118. if (ms_sLoadedDifficulty != INVALID_DIFFICULTY)
  4119. {
  4120. // Return cached difficulty.
  4121. sDifficulty = ms_sLoadedDifficulty;
  4122. // Clear cache.
  4123. ms_sLoadedDifficulty = INVALID_DIFFICULTY;
  4124. }
  4125. return sDifficulty;
  4126. }
  4127. ////////////////////////////////////////////////////////////////////////////////
  4128. // EOF
  4129. ////////////////////////////////////////////////////////////////////////////////