g_game.c 85 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000
  1. /* Emacs style mode select -*- C++ -*-
  2. *-----------------------------------------------------------------------------
  3. *
  4. *
  5. * PrBoom: a Doom port merged with LxDoom and LSDLDoom
  6. * based on BOOM, a modified and improved DOOM engine
  7. * Copyright (C) 1999 by
  8. * id Software, Chi Hoang, Lee Killough, Jim Flynn, Rand Phares, Ty Halderman
  9. * Copyright (C) 1999-2004 by
  10. * Jess Haas, Nicolas Kalkhof, Colin Phipps, Florian Schulze
  11. * Copyright 2005, 2006 by
  12. * Florian Schulze, Colin Phipps, Neil Stevens, Andrey Budko
  13. *
  14. * This program is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU General Public License
  16. * as published by the Free Software Foundation; either version 2
  17. * of the License, or (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software
  26. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  27. * 02111-1307, USA.
  28. *
  29. * DESCRIPTION: none
  30. * The original Doom description was none, basically because this file
  31. * has everything. This ties up the game logic, linking the menu and
  32. * input code to the underlying game by creating & respawning players,
  33. * building game tics, calling the underlying thing logic.
  34. *
  35. *-----------------------------------------------------------------------------
  36. */
  37. #include <stdio.h>
  38. #include <stdarg.h>
  39. #include <stdlib.h>
  40. #ifdef _MSC_VER
  41. #define F_OK 0 /* Check for file existence */
  42. #define W_OK 2 /* Check for write permission */
  43. #define R_OK 4 /* Check for read permission */
  44. #include <io.h>
  45. #else
  46. #include <unistd.h>
  47. #endif
  48. #include <fcntl.h>
  49. #ifdef HAVE_CONFIG_H
  50. #include "config.h"
  51. #endif
  52. #include "doomstat.h"
  53. #include "d_net.h"
  54. #include "f_finale.h"
  55. #include "m_argv.h"
  56. #include "m_misc.h"
  57. #include "m_menu.h"
  58. #include "m_random.h"
  59. #include "p_setup.h"
  60. #include "p_saveg.h"
  61. #include "p_tick.h"
  62. #include "p_map.h"
  63. #include "p_checksum.h"
  64. #include "d_main.h"
  65. #include "wi_stuff.h"
  66. #include "hu_stuff.h"
  67. #include "st_stuff.h"
  68. #include "am_map.h"
  69. #include "w_wad.h"
  70. #include "r_main.h"
  71. #include "r_draw.h"
  72. #include "p_map.h"
  73. #include "s_sound.h"
  74. #include "dstrings.h"
  75. #include "sounds.h"
  76. #include "r_data.h"
  77. #include "r_sky.h"
  78. #include "d_deh.h" // Ty 3/27/98 deh declarations
  79. #include "p_inter.h"
  80. #include "g_game.h"
  81. #include "lprintf.h"
  82. #include "i_main.h"
  83. #include "i_system.h"
  84. #include "r_demo.h"
  85. #include "r_fps.h"
  86. #define SAVEGAMESIZE 0x20000
  87. #define SAVESTRINGSIZE 24
  88. static size_t savegamesize = SAVEGAMESIZE; // killough
  89. static boolean netdemo;
  90. static const byte *demobuffer; /* cph - only used for playback */
  91. static int demolength; // check for overrun (missing DEMOMARKER)
  92. static FILE *demofp; /* cph - record straight to file */
  93. static const byte *demo_p;
  94. /* static JDC removed */ short consistancy[MAXPLAYERS][BACKUPTICS];
  95. gameaction_t gameaction;
  96. gamestate_t gamestate;
  97. skill_t gameskill;
  98. boolean respawnmonsters;
  99. int gameepisode;
  100. int gamemap;
  101. boolean paused;
  102. // CPhipps - moved *_loadgame vars here
  103. static boolean forced_loadgame = false;
  104. static boolean command_loadgame = false;
  105. boolean usergame; // ok to save / end game
  106. boolean timingdemo; // if true, exit with report on completion
  107. boolean fastdemo; // if true, run at full speed -- killough
  108. boolean nodrawers; // for comparative timing purposes
  109. boolean noblit; // for comparative timing purposes
  110. int starttime; // for comparative timing purposes
  111. boolean deathmatch; // only if started as net death
  112. boolean netgame; // only true if packets are broadcast
  113. boolean playeringame[MAXPLAYERS];
  114. player_t players[MAXPLAYERS];
  115. int consoleplayer; // player taking events and displaying
  116. int displayplayer; // view being displayed
  117. int gametic;
  118. int basetic; /* killough 9/29/98: for demo sync */
  119. int totalkills, totallive, totalitems, totalsecret; // for intermission
  120. boolean demorecording;
  121. boolean demoplayback;
  122. int demover;
  123. boolean singledemo; // quit after playing a demo from cmdline
  124. wbstartstruct_t wminfo; // parms for world map / intermission
  125. boolean haswolflevels = false;// jff 4/18/98 wolf levels present
  126. static byte *savebuffer; // CPhipps - static
  127. int autorun = false; // always running? // phares
  128. int totalleveltimes; // CPhipps - total time for all completed levels
  129. int longtics;
  130. //
  131. // controls (have defaults)
  132. //
  133. int key_right;
  134. int key_left;
  135. int key_up;
  136. int key_down;
  137. int key_menu_right; // phares 3/7/98
  138. int key_menu_left; // |
  139. int key_menu_up; // V
  140. int key_menu_down;
  141. int key_menu_backspace; // ^
  142. int key_menu_escape; // |
  143. int key_menu_enter; // phares 3/7/98
  144. int key_strafeleft;
  145. int key_straferight;
  146. int key_fire;
  147. int key_use;
  148. int key_strafe;
  149. int key_speed;
  150. int key_escape = KEYD_ESCAPE; // phares 4/13/98
  151. int key_savegame; // phares
  152. int key_loadgame; // |
  153. int key_autorun; // V
  154. int key_reverse;
  155. int key_zoomin;
  156. int key_zoomout;
  157. int key_chat;
  158. int key_backspace;
  159. int key_enter;
  160. int key_map_right;
  161. int key_map_left;
  162. int key_map_up;
  163. int key_map_down;
  164. int key_map_zoomin;
  165. int key_map_zoomout;
  166. int key_map;
  167. int key_map_gobig;
  168. int key_map_follow;
  169. int key_map_mark;
  170. int key_map_clear;
  171. int key_map_grid;
  172. int key_map_overlay; // cph - map overlay
  173. int key_map_rotate; // cph - map rotation
  174. int key_help = KEYD_F1; // phares 4/13/98
  175. int key_soundvolume;
  176. int key_hud;
  177. int key_quicksave;
  178. int key_endgame;
  179. int key_messages;
  180. int key_quickload;
  181. int key_quit;
  182. int key_gamma;
  183. int key_spy;
  184. int key_pause;
  185. int key_setup;
  186. int destination_keys[MAXPLAYERS];
  187. int key_weapontoggle;
  188. int key_weapon1;
  189. int key_weapon2;
  190. int key_weapon3;
  191. int key_weapon4;
  192. int key_weapon5;
  193. int key_weapon6;
  194. int key_weapon7; // ^
  195. int key_weapon8; // |
  196. int key_weapon9; // phares
  197. int key_screenshot; // killough 2/22/98: screenshot key
  198. int mousebfire;
  199. int mousebstrafe;
  200. int mousebforward;
  201. int joybfire;
  202. int joybstrafe;
  203. int joybuse;
  204. int joybspeed;
  205. #define MAXPLMOVE (forwardmove[1])
  206. #define TURBOTHRESHOLD 0x32
  207. #define SLOWTURNTICS 6
  208. #define QUICKREVERSE (short)32768 // 180 degree reverse // phares
  209. #define NUMKEYS 512
  210. fixed_t forwardmove[2] = {0x19, 0x32};
  211. fixed_t sidemove[2] = {0x18, 0x28};
  212. fixed_t angleturn[3] = {640, 1280, 320}; // + slow turn
  213. // CPhipps - made lots of key/button state vars static
  214. static boolean gamekeydown[NUMKEYS];
  215. static int turnheld; // for accelerative turning
  216. static boolean mousearray[4];
  217. static boolean *mousebuttons = &mousearray[1]; // allow [-1]
  218. // mouse values are used once
  219. static int mousex;
  220. static int mousey;
  221. static int dclicktime;
  222. static int dclickstate;
  223. static int dclicks;
  224. static int dclicktime2;
  225. static int dclickstate2;
  226. static int dclicks2;
  227. // joystick values are repeated
  228. static int joyxmove;
  229. static int joyymove;
  230. static boolean joyarray[5];
  231. static boolean *joybuttons = &joyarray[1]; // allow [-1]
  232. // Game events info
  233. static buttoncode_t special_event; // Event triggered by local player, to send
  234. static byte savegameslot; // Slot to load if gameaction == ga_loadgame
  235. char savedescription[SAVEDESCLEN]; // Description to save in savegame if gameaction == ga_savegame
  236. //jff 3/24/98 define defaultskill here
  237. int defaultskill; //note 1-based
  238. // killough 2/8/98: make corpse queue variable in size
  239. int bodyqueslot, bodyquesize; // killough 2/8/98
  240. mobj_t **bodyque = 0; // phares 8/10/98
  241. /* JDC: removed static */ void G_DoSaveGame (boolean menu);
  242. static const byte* G_ReadDemoHeader(const byte* demo_p, size_t size, boolean failonerror);
  243. //
  244. // G_BuildTiccmd
  245. // Builds a ticcmd from all of the available inputs
  246. // or reads it from the demo buffer.
  247. // If recording a demo, write it out
  248. //
  249. static inline signed char fudgef(signed char b)
  250. {
  251. static int c;
  252. if (!b || !demo_compatibility || longtics) return b;
  253. if (++c & 0x1f) return b;
  254. b |= 1; if (b>2) b-=2;
  255. return b;
  256. }
  257. static inline signed short fudgea(signed short b)
  258. {
  259. if (!b || !demo_compatibility || !longtics) return b;
  260. b |= 1; if (b>2) b-=2;
  261. return b;
  262. }
  263. void G_BuildTiccmd(ticcmd_t* cmd)
  264. {
  265. boolean strafe;
  266. boolean bstrafe;
  267. int speed;
  268. int tspeed;
  269. int forward;
  270. int side;
  271. int newweapon; // phares
  272. /* cphipps - remove needless I_BaseTiccmd call, just set the ticcmd to zero */
  273. memset(cmd,0,sizeof*cmd);
  274. cmd->consistancy = consistancy[consoleplayer][maketic%BACKUPTICS];
  275. strafe = gamekeydown[key_strafe] || mousebuttons[mousebstrafe]
  276. || joybuttons[joybstrafe];
  277. //e6y: the "RUN" key inverts the autorun state
  278. speed = (gamekeydown[key_speed] || joybuttons[joybspeed] ? !autorun : autorun); // phares
  279. forward = side = 0;
  280. // use two stage accelerative turning
  281. // on the keyboard and joystick
  282. if (joyxmove < 0 || joyxmove > 0 ||
  283. gamekeydown[key_right] || gamekeydown[key_left])
  284. turnheld += ticdup;
  285. else
  286. turnheld = 0;
  287. if (turnheld < SLOWTURNTICS)
  288. tspeed = 2; // slow turn
  289. else
  290. tspeed = speed;
  291. // turn 180 degrees in one keystroke? // phares
  292. // |
  293. if (gamekeydown[key_reverse]) // V
  294. {
  295. cmd->angleturn += QUICKREVERSE; // ^
  296. gamekeydown[key_reverse] = false; // |
  297. } // phares
  298. // let movement keys cancel each other out
  299. if (strafe)
  300. {
  301. if (gamekeydown[key_right])
  302. side += sidemove[speed];
  303. if (gamekeydown[key_left])
  304. side -= sidemove[speed];
  305. if (joyxmove > 0)
  306. side += sidemove[speed];
  307. if (joyxmove < 0)
  308. side -= sidemove[speed];
  309. }
  310. else
  311. {
  312. if (gamekeydown[key_right])
  313. cmd->angleturn -= angleturn[tspeed];
  314. if (gamekeydown[key_left])
  315. cmd->angleturn += angleturn[tspeed];
  316. if (joyxmove > 0)
  317. cmd->angleturn -= angleturn[tspeed];
  318. if (joyxmove < 0)
  319. cmd->angleturn += angleturn[tspeed];
  320. }
  321. if (gamekeydown[key_up])
  322. forward += forwardmove[speed];
  323. if (gamekeydown[key_down])
  324. forward -= forwardmove[speed];
  325. if (joyymove < 0)
  326. forward += forwardmove[speed];
  327. if (joyymove > 0)
  328. forward -= forwardmove[speed];
  329. if (gamekeydown[key_straferight])
  330. side += sidemove[speed];
  331. if (gamekeydown[key_strafeleft])
  332. side -= sidemove[speed];
  333. // buttons
  334. cmd->chatchar = HU_dequeueChatChar();
  335. if (gamekeydown[key_fire] || mousebuttons[mousebfire] ||
  336. joybuttons[joybfire])
  337. cmd->buttons |= BT_ATTACK;
  338. if (gamekeydown[key_use] || joybuttons[joybuse])
  339. {
  340. cmd->buttons |= BT_USE;
  341. // clear double clicks if hit use button
  342. dclicks = 0;
  343. }
  344. // Toggle between the top 2 favorite weapons. // phares
  345. // If not currently aiming one of these, switch to // phares
  346. // the favorite. Only switch if you possess the weapon. // phares
  347. // killough 3/22/98:
  348. //
  349. // Perform automatic weapons switch here rather than in p_pspr.c,
  350. // except in demo_compatibility mode.
  351. //
  352. // killough 3/26/98, 4/2/98: fix autoswitch when no weapons are left
  353. if ((!demo_compatibility && players[consoleplayer].attackdown && // killough
  354. !P_CheckAmmo(&players[consoleplayer])) || gamekeydown[key_weapontoggle])
  355. newweapon = P_SwitchWeapon(&players[consoleplayer]); // phares
  356. else
  357. { // phares 02/26/98: Added gamemode checks
  358. newweapon =
  359. gamekeydown[key_weapon1] ? wp_fist : // killough 5/2/98: reformatted
  360. gamekeydown[key_weapon2] ? wp_pistol :
  361. gamekeydown[key_weapon3] ? wp_shotgun :
  362. gamekeydown[key_weapon4] ? wp_chaingun :
  363. gamekeydown[key_weapon5] ? wp_missile :
  364. gamekeydown[key_weapon6] && gamemode != shareware ? wp_plasma :
  365. gamekeydown[key_weapon7] && gamemode != shareware ? wp_bfg :
  366. gamekeydown[key_weapon8] ? wp_chainsaw :
  367. (!demo_compatibility && gamekeydown[key_weapon9] && gamemode == commercial) ? wp_supershotgun :
  368. wp_nochange;
  369. // killough 3/22/98: For network and demo consistency with the
  370. // new weapons preferences, we must do the weapons switches here
  371. // instead of in p_user.c. But for old demos we must do it in
  372. // p_user.c according to the old rules. Therefore demo_compatibility
  373. // determines where the weapons switch is made.
  374. // killough 2/8/98:
  375. // Allow user to switch to fist even if they have chainsaw.
  376. // Switch to fist or chainsaw based on preferences.
  377. // Switch to shotgun or SSG based on preferences.
  378. if (!demo_compatibility)
  379. {
  380. const player_t *player = &players[consoleplayer];
  381. // only select chainsaw from '1' if it's owned, it's
  382. // not already in use, and the player prefers it or
  383. // the fist is already in use, or the player does not
  384. // have the berserker strength.
  385. if (newweapon==wp_fist && player->weaponowned[wp_chainsaw] &&
  386. player->readyweapon!=wp_chainsaw &&
  387. (player->readyweapon==wp_fist ||
  388. !player->powers[pw_strength] ||
  389. P_WeaponPreferred(wp_chainsaw, wp_fist)))
  390. newweapon = wp_chainsaw;
  391. // Select SSG from '3' only if it's owned and the player
  392. // does not have a shotgun, or if the shotgun is already
  393. // in use, or if the SSG is not already in use and the
  394. // player prefers it.
  395. if (newweapon == wp_shotgun && gamemode == commercial &&
  396. player->weaponowned[wp_supershotgun] &&
  397. (!player->weaponowned[wp_shotgun] ||
  398. player->readyweapon == wp_shotgun ||
  399. (player->readyweapon != wp_supershotgun &&
  400. P_WeaponPreferred(wp_supershotgun, wp_shotgun))))
  401. newweapon = wp_supershotgun;
  402. }
  403. // killough 2/8/98, 3/22/98 -- end of weapon selection changes
  404. }
  405. if (newweapon != wp_nochange)
  406. {
  407. cmd->buttons |= BT_CHANGE;
  408. cmd->buttons |= newweapon<<BT_WEAPONSHIFT;
  409. }
  410. // mouse
  411. if (mousebuttons[mousebforward])
  412. forward += forwardmove[speed];
  413. // forward double click
  414. if (mousebuttons[mousebforward] != dclickstate && dclicktime > 1 )
  415. {
  416. dclickstate = mousebuttons[mousebforward];
  417. if (dclickstate)
  418. dclicks++;
  419. if (dclicks == 2)
  420. {
  421. cmd->buttons |= BT_USE;
  422. dclicks = 0;
  423. }
  424. else
  425. dclicktime = 0;
  426. }
  427. else
  428. if ((dclicktime += ticdup) > 20)
  429. {
  430. dclicks = 0;
  431. dclickstate = 0;
  432. }
  433. // strafe double click
  434. bstrafe = mousebuttons[mousebstrafe] || joybuttons[joybstrafe];
  435. if (bstrafe != dclickstate2 && dclicktime2 > 1 )
  436. {
  437. dclickstate2 = bstrafe;
  438. if (dclickstate2)
  439. dclicks2++;
  440. if (dclicks2 == 2)
  441. {
  442. cmd->buttons |= BT_USE;
  443. dclicks2 = 0;
  444. }
  445. else
  446. dclicktime2 = 0;
  447. }
  448. else
  449. if ((dclicktime2 += ticdup) > 20)
  450. {
  451. dclicks2 = 0;
  452. dclickstate2 = 0;
  453. }
  454. forward += mousey;
  455. if (strafe)
  456. side += mousex / 4; /* mead Don't want to strafe as fast as turns.*/
  457. else
  458. cmd->angleturn -= mousex; /* mead now have enough dynamic range 2-10-00 */
  459. mousex = mousey = 0;
  460. if (forward > MAXPLMOVE)
  461. forward = MAXPLMOVE;
  462. else if (forward < -MAXPLMOVE)
  463. forward = -MAXPLMOVE;
  464. if (side > MAXPLMOVE)
  465. side = MAXPLMOVE;
  466. else if (side < -MAXPLMOVE)
  467. side = -MAXPLMOVE;
  468. cmd->forwardmove += fudgef((signed char)forward);
  469. cmd->sidemove += side;
  470. cmd->angleturn = fudgea(cmd->angleturn);
  471. // CPhipps - special events (game new/load/save/pause)
  472. if (special_event & BT_SPECIAL) {
  473. cmd->buttons = special_event;
  474. special_event = 0;
  475. }
  476. }
  477. //
  478. // G_RestartLevel
  479. //
  480. void G_RestartLevel(void)
  481. {
  482. special_event = BT_SPECIAL | (BTS_RESTARTLEVEL & BT_SPECIALMASK);
  483. }
  484. #include "z_bmalloc.h"
  485. //
  486. // G_DoLoadLevel
  487. //
  488. static void G_DoLoadLevel (void)
  489. {
  490. int i;
  491. // Set the sky map.
  492. // First thing, we have a dummy sky texture name,
  493. // a flat. The data is in the WAD only because
  494. // we look for an actual index, instead of simply
  495. // setting one.
  496. skyflatnum = R_FlatNumForName ( SKYFLATNAME );
  497. // DOOM determines the sky texture to be used
  498. // depending on the current episode, and the game version.
  499. if (gamemode == commercial)
  500. // || gamemode == pack_tnt //jff 3/27/98 sorry guys pack_tnt,pack_plut
  501. // || gamemode == pack_plut) //aren't gamemodes, this was matching retail
  502. {
  503. skytexture = R_TextureNumForName ("SKY3");
  504. if (gamemap < 12)
  505. skytexture = R_TextureNumForName ("SKY1");
  506. else
  507. if (gamemap < 21)
  508. skytexture = R_TextureNumForName ("SKY2");
  509. }
  510. else //jff 3/27/98 and lets not forget about DOOM and Ultimate DOOM huh?
  511. switch (gameepisode)
  512. {
  513. case 1:
  514. skytexture = R_TextureNumForName ("SKY1");
  515. break;
  516. case 2:
  517. skytexture = R_TextureNumForName ("SKY2");
  518. break;
  519. case 3:
  520. skytexture = R_TextureNumForName ("SKY3");
  521. break;
  522. case 4: // Special Edition sky
  523. skytexture = R_TextureNumForName ("SKY4");
  524. break;
  525. }//jff 3/27/98 end sky setting fix
  526. /* cph 2006/07/31 - took out unused levelstarttic variable */
  527. if (!demo_compatibility && !mbf_features) // killough 9/29/98
  528. basetic = gametic;
  529. if (wipegamestate == GS_LEVEL)
  530. wipegamestate = -1; // force a wipe
  531. gamestate = GS_LEVEL;
  532. for (i=0 ; i<MAXPLAYERS ; i++)
  533. {
  534. if (playeringame[i] && players[i].playerstate == PST_DEAD)
  535. players[i].playerstate = PST_REBORN;
  536. memset (players[i].frags,0,sizeof(players[i].frags));
  537. }
  538. // initialize the msecnode_t freelist. phares 3/25/98
  539. // any nodes in the freelist are gone by now, cleared
  540. // by Z_FreeTags() when the previous level ended or player
  541. // died.
  542. {
  543. DECLARE_BLOCK_MEMORY_ALLOC_ZONE(secnodezone);
  544. NULL_BLOCK_MEMORY_ALLOC_ZONE(secnodezone);
  545. //extern msecnode_t *headsecnode; // phares 3/25/98
  546. //headsecnode = NULL;
  547. }
  548. P_SetupLevel (gameepisode, gamemap, 0, gameskill);
  549. if (!demoplayback) // Don't switch views if playing a demo
  550. displayplayer = consoleplayer; // view the guy you are playing
  551. gameaction = ga_nothing;
  552. Z_CheckHeap ();
  553. // clear cmd building stuff
  554. memset (gamekeydown, 0, sizeof(gamekeydown));
  555. joyxmove = joyymove = 0;
  556. mousex = mousey = 0;
  557. special_event = 0; paused = false;
  558. memset (mousebuttons, 0, sizeof(mousebuttons));
  559. memset (joybuttons, 0, sizeof(joybuttons));
  560. // killough 5/13/98: in case netdemo has consoleplayer other than green
  561. ST_Start();
  562. HU_Start();
  563. }
  564. //
  565. // G_Responder
  566. // Get info needed to make ticcmd_ts for the players.
  567. //
  568. boolean G_Responder (event_t* ev)
  569. {
  570. // allow spy mode changes even during the demo
  571. // killough 2/22/98: even during DM demo
  572. //
  573. // killough 11/98: don't autorepeat spy mode switch
  574. if (ev->data1 == key_spy && netgame && (demoplayback || !deathmatch) &&
  575. gamestate == GS_LEVEL)
  576. {
  577. if (ev->type == ev_keyup)
  578. gamekeydown[key_spy] = false;
  579. if (ev->type == ev_keydown && !gamekeydown[key_spy])
  580. {
  581. gamekeydown[key_spy] = true;
  582. do // spy mode
  583. if (++displayplayer >= MAXPLAYERS)
  584. displayplayer = 0;
  585. while (!playeringame[displayplayer] && displayplayer!=consoleplayer);
  586. ST_Start(); // killough 3/7/98: switch status bar views too
  587. HU_Start();
  588. S_UpdateSounds(players[displayplayer].mo);
  589. R_ActivateSectorInterpolations();
  590. R_SmoothPlaying_Reset(NULL);
  591. }
  592. return true;
  593. }
  594. // any other key pops up menu if in demos
  595. //
  596. // killough 8/2/98: enable automap in -timedemo demos
  597. //
  598. // killough 9/29/98: make any key pop up menu regardless of
  599. // which kind of demo, and allow other events during playback
  600. if (gameaction == ga_nothing && (demoplayback || gamestate == GS_DEMOSCREEN))
  601. {
  602. // killough 9/29/98: allow user to pause demos during playback
  603. if (ev->type == ev_keydown && ev->data1 == key_pause)
  604. {
  605. if (paused ^= 2)
  606. S_PauseSound();
  607. else
  608. S_ResumeSound();
  609. return true;
  610. }
  611. // killough 10/98:
  612. // Don't pop up menu, if paused in middle
  613. // of demo playback, or if automap active.
  614. // Don't suck up keys, which may be cheats
  615. return gamestate == GS_DEMOSCREEN &&
  616. !(paused & 2) && !(automapmode & am_active) &&
  617. ((ev->type == ev_keydown) ||
  618. (ev->type == ev_mouse && ev->data1) ||
  619. (ev->type == ev_joystick && ev->data1)) ?
  620. M_StartControlPanel(), true : false;
  621. }
  622. if (gamestate == GS_FINALE && F_Responder(ev))
  623. return true; // finale ate the event
  624. switch (ev->type)
  625. {
  626. case ev_keydown:
  627. if (ev->data1 == key_pause) // phares
  628. {
  629. special_event = BT_SPECIAL | (BTS_PAUSE & BT_SPECIALMASK);
  630. return true;
  631. }
  632. if (ev->data1 <NUMKEYS)
  633. gamekeydown[ev->data1] = true;
  634. return true; // eat key down events
  635. case ev_keyup:
  636. if (ev->data1 <NUMKEYS)
  637. gamekeydown[ev->data1] = false;
  638. return false; // always let key up events filter down
  639. case ev_mouse:
  640. mousebuttons[0] = ev->data1 & 1;
  641. mousebuttons[1] = ev->data1 & 2;
  642. mousebuttons[2] = ev->data1 & 4;
  643. /*
  644. * bmead@surfree.com
  645. * Modified by Barry Mead after adding vastly more resolution
  646. * to the Mouse Sensitivity Slider in the options menu 1-9-2000
  647. * Removed the mouseSensitivity "*4" to allow more low end
  648. * sensitivity resolution especially for lsdoom users.
  649. */
  650. mousex += (ev->data2*(mouseSensitivity_horiz))/10; /* killough */
  651. mousey += (ev->data3*(mouseSensitivity_vert))/10; /*Mead rm *4 */
  652. return true; // eat events
  653. case ev_joystick:
  654. joybuttons[0] = ev->data1 & 1;
  655. joybuttons[1] = ev->data1 & 2;
  656. joybuttons[2] = ev->data1 & 4;
  657. joybuttons[3] = ev->data1 & 8;
  658. joyxmove = ev->data2;
  659. joyymove = ev->data3;
  660. return true; // eat events
  661. default:
  662. break;
  663. }
  664. return false;
  665. }
  666. //
  667. // G_Ticker
  668. // Make ticcmd_ts for the players.
  669. //
  670. void G_Ticker (void)
  671. {
  672. int i;
  673. static gamestate_t prevgamestate;
  674. // CPhipps - player colour changing
  675. if (!demoplayback && mapcolor_plyr[consoleplayer] != mapcolor_me) {
  676. // Changed my multiplayer colour - Inform the whole game
  677. int net_cl = LONG(mapcolor_me);
  678. #ifdef HAVE_NET
  679. D_NetSendMisc(nm_plcolour, sizeof(net_cl), &net_cl);
  680. #endif
  681. G_ChangedPlayerColour(consoleplayer, mapcolor_me);
  682. }
  683. P_MapStart();
  684. // do player reborns if needed
  685. for (i=0 ; i<MAXPLAYERS ; i++)
  686. if (playeringame[i] && players[i].playerstate == PST_REBORN)
  687. G_DoReborn (i);
  688. P_MapEnd();
  689. // do things to change the game state
  690. while (gameaction != ga_nothing)
  691. {
  692. switch (gameaction)
  693. {
  694. case ga_loadlevel:
  695. // force players to be initialized on level reload
  696. for (i=0 ; i<MAXPLAYERS ; i++)
  697. players[i].playerstate = PST_REBORN;
  698. G_DoLoadLevel ();
  699. break;
  700. case ga_newgame:
  701. G_DoNewGame ();
  702. break;
  703. case ga_loadgame:
  704. G_DoLoadGame ();
  705. break;
  706. case ga_savegame:
  707. G_DoSaveGame (false);
  708. break;
  709. case ga_playdemo:
  710. G_DoPlayDemo ();
  711. break;
  712. case ga_completed:
  713. G_DoCompleted ();
  714. break;
  715. case ga_victory:
  716. F_StartFinale ();
  717. break;
  718. case ga_worlddone:
  719. G_DoWorldDone ();
  720. break;
  721. case ga_nothing:
  722. break;
  723. }
  724. }
  725. if (paused & 2 || (!demoplayback && menuactive && !netgame))
  726. basetic++; // For revenant tracers and RNG -- we must maintain sync
  727. else {
  728. // get commands, check consistancy, and build new consistancy check
  729. int buf = (gametic/ticdup)%BACKUPTICS;
  730. for (i=0 ; i<MAXPLAYERS ; i++) {
  731. if (playeringame[i])
  732. {
  733. ticcmd_t *cmd = &players[i].cmd;
  734. memcpy(cmd, &netcmds[i][buf], sizeof *cmd);
  735. if (demoplayback)
  736. G_ReadDemoTiccmd (cmd);
  737. if (demorecording)
  738. G_WriteDemoTiccmd (cmd);
  739. // check for turbo cheats
  740. // killough 2/14/98, 2/20/98 -- only warn in netgames and demos
  741. if ((netgame || demoplayback) && cmd->forwardmove > TURBOTHRESHOLD &&
  742. !(gametic&31) && ((gametic>>5)&3) == i )
  743. {
  744. extern char *player_names[];
  745. /* cph - don't use sprintf, use doom_printf */
  746. doom_printf ("%s is turbo!", player_names[i]);
  747. }
  748. if (netgame && !netdemo && !(gametic%ticdup) )
  749. {
  750. #ifndef IPHONE // consistency checks are handled in AsyncTic() on packet receive
  751. if (gametic > BACKUPTICS
  752. && consistancy[i][buf] != cmd->consistancy)
  753. I_Error("G_Ticker: Consistency failure (%i should be %i)",
  754. cmd->consistancy, consistancy[i][buf]);
  755. #endif
  756. if (players[i].mo)
  757. consistancy[i][buf] = players[i].mo->x;
  758. else
  759. consistancy[i][buf] = 0; // killough 2/14/98
  760. }
  761. }
  762. }
  763. // check for special buttons
  764. for (i=0; i<MAXPLAYERS; i++) {
  765. if (playeringame[i])
  766. {
  767. if (players[i].cmd.buttons & BT_SPECIAL)
  768. {
  769. switch (players[i].cmd.buttons & BT_SPECIALMASK)
  770. {
  771. case BTS_PAUSE:
  772. paused ^= 1;
  773. if (paused)
  774. S_PauseSound ();
  775. else
  776. S_ResumeSound ();
  777. break;
  778. case BTS_SAVEGAME:
  779. if (!savedescription[0])
  780. strcpy(savedescription, "NET GAME");
  781. savegameslot =
  782. (players[i].cmd.buttons & BTS_SAVEMASK)>>BTS_SAVESHIFT;
  783. gameaction = ga_savegame;
  784. break;
  785. // CPhipps - remote loadgame request
  786. case BTS_LOADGAME:
  787. savegameslot =
  788. (players[i].cmd.buttons & BTS_SAVEMASK)>>BTS_SAVESHIFT;
  789. gameaction = ga_loadgame;
  790. forced_loadgame = netgame; // Force if a netgame
  791. command_loadgame = false;
  792. break;
  793. // CPhipps - Restart the level
  794. case BTS_RESTARTLEVEL:
  795. if (demoplayback || (compatibility_level < lxdoom_1_compatibility))
  796. break; // CPhipps - Ignore in demos or old games
  797. gameaction = ga_loadlevel;
  798. break;
  799. }
  800. players[i].cmd.buttons = 0;
  801. }
  802. }
  803. }
  804. }
  805. // cph - if the gamestate changed, we may need to clean up the old gamestate
  806. if (gamestate != prevgamestate) {
  807. switch (prevgamestate) {
  808. case GS_LEVEL:
  809. // This causes crashes at level end - Neil Stevens
  810. // The crash is because the sounds aren't stopped before freeing them
  811. // the following is a possible fix
  812. // This fix does avoid the crash wowever, with this fix in, the exit
  813. // switch sound is cut off
  814. // S_Stop();
  815. // Z_FreeTags(PU_LEVEL, PU_PURGELEVEL-1);
  816. break;
  817. case GS_INTERMISSION:
  818. WI_End();
  819. default:
  820. break;
  821. }
  822. prevgamestate = gamestate;
  823. }
  824. // e6y
  825. // do nothing if a pause has been pressed during playback
  826. // pausing during intermission can cause desynchs without that
  827. if (paused & 2 && gamestate != GS_LEVEL)
  828. return;
  829. // do main actions
  830. switch (gamestate)
  831. {
  832. case GS_LEVEL:
  833. P_Ticker ();
  834. ST_Ticker ();
  835. AM_Ticker ();
  836. HU_Ticker ();
  837. break;
  838. case GS_INTERMISSION:
  839. WI_Ticker ();
  840. break;
  841. case GS_FINALE:
  842. F_Ticker ();
  843. break;
  844. case GS_DEMOSCREEN:
  845. D_PageTicker ();
  846. break;
  847. }
  848. }
  849. //
  850. // PLAYER STRUCTURE FUNCTIONS
  851. // also see P_SpawnPlayer in P_Things
  852. //
  853. //
  854. // G_PlayerFinishLevel
  855. // Can when a player completes a level.
  856. //
  857. static void G_PlayerFinishLevel(int player)
  858. {
  859. player_t *p = &players[player];
  860. memset(p->powers, 0, sizeof p->powers);
  861. memset(p->cards, 0, sizeof p->cards);
  862. p->mo = NULL; // cph - this is allocated PU_LEVEL so it's gone
  863. p->extralight = 0; // cancel gun flashes
  864. p->fixedcolormap = 0; // cancel ir gogles
  865. p->damagecount = 0; // no palette changes
  866. p->bonuscount = 0;
  867. }
  868. // CPhipps - G_SetPlayerColour
  869. // Player colours stuff
  870. //
  871. // G_SetPlayerColour
  872. #include "r_draw.h"
  873. void G_ChangedPlayerColour(int pn, int cl)
  874. {
  875. int i;
  876. if (!netgame) return;
  877. mapcolor_plyr[pn] = cl;
  878. // Rebuild colour translation tables accordingly
  879. R_InitTranslationTables();
  880. // Change translations on existing player mobj's
  881. for (i=0; i<MAXPLAYERS; i++) {
  882. if ((gamestate == GS_LEVEL) && playeringame[i] && (players[i].mo != NULL)) {
  883. players[i].mo->flags &= ~MF_TRANSLATION;
  884. players[i].mo->flags |= playernumtotrans[i] << MF_TRANSSHIFT;
  885. }
  886. }
  887. }
  888. //
  889. // G_PlayerReborn
  890. // Called after a player dies
  891. // almost everything is cleared and initialized
  892. //
  893. void G_PlayerReborn (int player)
  894. {
  895. player_t *p;
  896. int i;
  897. int frags[MAXPLAYERS];
  898. int killcount;
  899. int itemcount;
  900. int secretcount;
  901. memcpy (frags, players[player].frags, sizeof frags);
  902. killcount = players[player].killcount;
  903. itemcount = players[player].itemcount;
  904. secretcount = players[player].secretcount;
  905. p = &players[player];
  906. // killough 3/10/98,3/21/98: preserve cheats across idclev
  907. {
  908. int cheats = p->cheats;
  909. memset (p, 0, sizeof(*p));
  910. p->cheats = cheats;
  911. }
  912. memcpy(players[player].frags, frags, sizeof(players[player].frags));
  913. players[player].killcount = killcount;
  914. players[player].itemcount = itemcount;
  915. players[player].secretcount = secretcount;
  916. p->usedown = p->attackdown = true; // don't do anything immediately
  917. p->playerstate = PST_LIVE;
  918. p->health = initial_health; // Ty 03/12/98 - use dehacked values
  919. p->readyweapon = p->pendingweapon = wp_pistol;
  920. p->weaponowned[wp_fist] = true;
  921. p->weaponowned[wp_pistol] = true;
  922. p->ammo[am_clip] = initial_bullets; // Ty 03/12/98 - use dehacked values
  923. for (i=0 ; i<NUMAMMO ; i++)
  924. p->maxammo[i] = maxammo[i];
  925. }
  926. //
  927. // G_CheckSpot
  928. // Returns false if the player cannot be respawned
  929. // at the given mapthing_t spot
  930. // because something is occupying it
  931. //
  932. static boolean G_CheckSpot(int playernum, mapthing_t *mthing)
  933. {
  934. fixed_t x,y;
  935. subsector_t *ss;
  936. int i;
  937. if (!players[playernum].mo)
  938. {
  939. // first spawn of level, before corpses
  940. for (i=0 ; i<playernum ; i++)
  941. if (players[i].mo->x == mthing->x << FRACBITS
  942. && players[i].mo->y == mthing->y << FRACBITS)
  943. return false;
  944. return true;
  945. }
  946. x = mthing->x << FRACBITS;
  947. y = mthing->y << FRACBITS;
  948. // killough 4/2/98: fix bug where P_CheckPosition() uses a non-solid
  949. // corpse to detect collisions with other players in DM starts
  950. //
  951. // Old code:
  952. // if (!P_CheckPosition (players[playernum].mo, x, y))
  953. // return false;
  954. players[playernum].mo->flags |= MF_SOLID;
  955. i = P_CheckPosition(players[playernum].mo, x, y);
  956. players[playernum].mo->flags &= ~MF_SOLID;
  957. if (!i)
  958. return false;
  959. // flush an old corpse if needed
  960. // killough 2/8/98: make corpse queue have an adjustable limit
  961. // killough 8/1/98: Fix bugs causing strange crashes
  962. if (bodyquesize > 0)
  963. {
  964. static int queuesize;
  965. if (queuesize < bodyquesize)
  966. {
  967. bodyque = realloc(bodyque, bodyquesize*sizeof*bodyque);
  968. memset(bodyque+queuesize, 0,
  969. (bodyquesize-queuesize)*sizeof*bodyque);
  970. queuesize = bodyquesize;
  971. }
  972. if (bodyqueslot >= bodyquesize)
  973. P_RemoveMobj(bodyque[bodyqueslot % bodyquesize]);
  974. bodyque[bodyqueslot++ % bodyquesize] = players[playernum].mo;
  975. }
  976. else
  977. if (!bodyquesize)
  978. P_RemoveMobj(players[playernum].mo);
  979. // spawn a teleport fog
  980. ss = R_PointInSubsector (x,y);
  981. { // Teleport fog at respawn point
  982. fixed_t xa,ya;
  983. int an;
  984. mobj_t *mo;
  985. /* BUG: an can end up negative, because mthing->angle is (signed) short.
  986. * We have to emulate original Doom's behaviour, deferencing past the start
  987. * of the array, into the previous array (finetangent) */
  988. an = ( ANG45 * ((signed)mthing->angle/45) ) >> ANGLETOFINESHIFT;
  989. xa = finecosine[an];
  990. ya = finesine[an];
  991. if (compatibility_level <= finaldoom_compatibility || compatibility_level == prboom_4_compatibility)
  992. switch (an) {
  993. case -4096: xa = finetangent[2048]; // finecosine[-4096]
  994. ya = finetangent[0]; // finesine[-4096]
  995. break;
  996. case -3072: xa = finetangent[3072]; // finecosine[-3072]
  997. ya = finetangent[1024]; // finesine[-3072]
  998. break;
  999. case -2048: xa = finesine[0]; // finecosine[-2048]
  1000. ya = finetangent[2048]; // finesine[-2048]
  1001. break;
  1002. case -1024: xa = finesine[1024]; // finecosine[-1024]
  1003. ya = finetangent[3072]; // finesine[-1024]
  1004. break;
  1005. case 1024:
  1006. case 2048:
  1007. case 3072:
  1008. case 4096:
  1009. case 0: break; /* correct angles set above */
  1010. default: I_Error("G_CheckSpot: unexpected angle %d\n",an);
  1011. }
  1012. mo = P_SpawnMobj(x+20*xa, y+20*ya, ss->sector->floorheight, MT_TFOG);
  1013. if (players[consoleplayer].viewz != 1)
  1014. S_StartSound(mo, sfx_telept); // don't start sound on first frame
  1015. }
  1016. return true;
  1017. }
  1018. // G_DeathMatchSpawnPlayer
  1019. // Spawns a player at one of the random death match spots
  1020. // called at level load and each death
  1021. //
  1022. void G_DeathMatchSpawnPlayer (int playernum)
  1023. {
  1024. int j, selections = deathmatch_p - deathmatchstarts;
  1025. if (selections < MAXPLAYERS)
  1026. I_Error("G_DeathMatchSpawnPlayer: Only %i deathmatch spots, %d required",
  1027. selections, MAXPLAYERS);
  1028. for (j=0 ; j<20 ; j++)
  1029. {
  1030. int i = P_Random(pr_dmspawn) % selections;
  1031. if (G_CheckSpot (playernum, &deathmatchstarts[i]) )
  1032. {
  1033. deathmatchstarts[i].type = playernum+1;
  1034. P_SpawnPlayer (playernum, &deathmatchstarts[i]);
  1035. return;
  1036. }
  1037. }
  1038. // no good spot, so the player will probably get stuck
  1039. P_SpawnPlayer (playernum, &playerstarts[playernum]);
  1040. }
  1041. //
  1042. // G_DoReborn
  1043. //
  1044. void G_DoReborn (int playernum)
  1045. {
  1046. if (!netgame)
  1047. gameaction = ga_loadlevel; // reload the level from scratch
  1048. else
  1049. { // respawn at the start
  1050. int i;
  1051. // first dissasociate the corpse
  1052. players[playernum].mo->player = NULL;
  1053. // spawn at random spot if in death match
  1054. if (deathmatch)
  1055. {
  1056. G_DeathMatchSpawnPlayer (playernum);
  1057. return;
  1058. }
  1059. if (G_CheckSpot (playernum, &playerstarts[playernum]) )
  1060. {
  1061. P_SpawnPlayer (playernum, &playerstarts[playernum]);
  1062. return;
  1063. }
  1064. // try to spawn at one of the other players spots
  1065. for (i=0 ; i<MAXPLAYERS ; i++)
  1066. {
  1067. if (G_CheckSpot (playernum, &playerstarts[i]) )
  1068. {
  1069. P_SpawnPlayer (playernum, &playerstarts[i]);
  1070. return;
  1071. }
  1072. // he's going to be inside something. Too bad.
  1073. }
  1074. P_SpawnPlayer (playernum, &playerstarts[playernum]);
  1075. }
  1076. }
  1077. // DOOM Par Times
  1078. int pars[4][10] = {
  1079. {0},
  1080. {0,30,75,120,90,165,180,180,30,165},
  1081. {0,90,90,90,120,90,360,240,30,170},
  1082. {0,90,45,90,150,90,90,165,30,135}
  1083. };
  1084. // DOOM II Par Times
  1085. int cpars[32] = {
  1086. 30,90,120,120,90,150,120,120,270,90, // 1-10
  1087. 210,150,150,150,210,150,420,150,210,150, // 11-20
  1088. 240,150,180,150,150,300,330,420,300,180, // 21-30
  1089. 120,30 // 31-32
  1090. };
  1091. static boolean secretexit;
  1092. void G_ExitLevel (void)
  1093. {
  1094. secretexit = false;
  1095. gameaction = ga_completed;
  1096. }
  1097. // Here's for the german edition.
  1098. // IF NO WOLF3D LEVELS, NO SECRET EXIT!
  1099. void G_SecretExitLevel (void)
  1100. {
  1101. if (gamemode!=commercial || haswolflevels)
  1102. secretexit = true;
  1103. else
  1104. secretexit = false;
  1105. gameaction = ga_completed;
  1106. }
  1107. //
  1108. // G_DoCompleted
  1109. //
  1110. void G_DoCompleted (void)
  1111. {
  1112. int i;
  1113. gameaction = ga_nothing;
  1114. for (i=0; i<MAXPLAYERS; i++)
  1115. if (playeringame[i])
  1116. G_PlayerFinishLevel(i); // take away cards and stuff
  1117. if (automapmode & am_active)
  1118. AM_Stop();
  1119. if (gamemode != commercial) // kilough 2/7/98
  1120. switch(gamemap)
  1121. {
  1122. // cph - Remove ExM8 special case, so it gets summary screen displayed
  1123. case 9:
  1124. for (i=0 ; i<MAXPLAYERS ; i++)
  1125. players[i].didsecret = true;
  1126. break;
  1127. }
  1128. wminfo.didsecret = players[consoleplayer].didsecret;
  1129. wminfo.epsd = gameepisode -1;
  1130. wminfo.last = gamemap -1;
  1131. // wminfo.next is 0 biased, unlike gamemap
  1132. if (gamemode == commercial)
  1133. {
  1134. if (secretexit)
  1135. switch(gamemap)
  1136. {
  1137. case 15:
  1138. wminfo.next = 30; break;
  1139. case 31:
  1140. wminfo.next = 31; break;
  1141. }
  1142. else
  1143. switch(gamemap)
  1144. {
  1145. case 31:
  1146. case 32:
  1147. wminfo.next = 15; break;
  1148. default:
  1149. wminfo.next = gamemap;
  1150. }
  1151. }
  1152. else
  1153. {
  1154. if (secretexit)
  1155. wminfo.next = 8; // go to secret level
  1156. else
  1157. if (gamemap == 9)
  1158. {
  1159. // returning from secret level
  1160. switch (gameepisode)
  1161. {
  1162. case 1:
  1163. wminfo.next = 3;
  1164. break;
  1165. case 2:
  1166. wminfo.next = 5;
  1167. break;
  1168. case 3:
  1169. wminfo.next = 6;
  1170. break;
  1171. case 4:
  1172. wminfo.next = 2;
  1173. break;
  1174. }
  1175. }
  1176. else
  1177. wminfo.next = gamemap; // go to next level
  1178. }
  1179. wminfo.maxkills = totalkills;
  1180. wminfo.maxitems = totalitems;
  1181. wminfo.maxsecret = totalsecret;
  1182. wminfo.maxfrags = 0;
  1183. if ( gamemode == commercial )
  1184. wminfo.partime = TICRATE*cpars[gamemap-1];
  1185. else
  1186. wminfo.partime = TICRATE*pars[gameepisode][gamemap];
  1187. wminfo.pnum = consoleplayer;
  1188. for (i=0 ; i<MAXPLAYERS ; i++)
  1189. {
  1190. wminfo.plyr[i].in = playeringame[i];
  1191. wminfo.plyr[i].skills = players[i].killcount;
  1192. wminfo.plyr[i].sitems = players[i].itemcount;
  1193. wminfo.plyr[i].ssecret = players[i].secretcount;
  1194. wminfo.plyr[i].stime = leveltime;
  1195. memcpy (wminfo.plyr[i].frags, players[i].frags,
  1196. sizeof(wminfo.plyr[i].frags));
  1197. }
  1198. /* cph - modified so that only whole seconds are added to the totalleveltimes
  1199. * value; so our total is compatible with the "naive" total of just adding
  1200. * the times in seconds shown for each level. Also means our total time
  1201. * will agree with Compet-n.
  1202. */
  1203. wminfo.totaltimes = (totalleveltimes += (leveltime - leveltime%35));
  1204. gamestate = GS_INTERMISSION;
  1205. automapmode &= ~am_active;
  1206. // lmpwatch.pl engine-side demo testing support
  1207. // print "FINISHED: <mapname>" when the player exits the current map
  1208. if (nodrawers && (demoplayback || timingdemo)) {
  1209. if (gamemode == commercial)
  1210. lprintf(LO_INFO, "FINISHED: MAP%02d\n", gamemap);
  1211. else
  1212. lprintf(LO_INFO, "FINISHED: E%dM%d\n", gameepisode, gamemap);
  1213. }
  1214. WI_Start (&wminfo);
  1215. #ifdef IPHONE
  1216. iphoneIntermission( &wminfo );
  1217. #endif
  1218. }
  1219. //
  1220. // G_WorldDone
  1221. //
  1222. void G_WorldDone (void)
  1223. {
  1224. gameaction = ga_worlddone;
  1225. if (secretexit)
  1226. players[consoleplayer].didsecret = true;
  1227. if (gamemode == commercial)
  1228. {
  1229. switch (gamemap)
  1230. {
  1231. case 15:
  1232. case 31:
  1233. if (!secretexit)
  1234. break;
  1235. case 6:
  1236. case 11:
  1237. case 20:
  1238. case 30:
  1239. F_StartFinale ();
  1240. break;
  1241. }
  1242. }
  1243. else if (gamemap == 8)
  1244. gameaction = ga_victory; // cph - after ExM8 summary screen, show victory stuff
  1245. }
  1246. void G_DoWorldDone (void)
  1247. {
  1248. idmusnum = -1; //jff 3/17/98 allow new level's music to be loaded
  1249. gamestate = GS_LEVEL;
  1250. gamemap = wminfo.next+1;
  1251. G_DoLoadLevel();
  1252. gameaction = ga_nothing;
  1253. AM_clearMarks(); //jff 4/12/98 clear any marks on the automap
  1254. iphoneStartLevel(); // JDC: update playState and set the "tried level" flag
  1255. }
  1256. // killough 2/28/98: A ridiculously large number
  1257. // of players, the most you'll ever need in a demo
  1258. // or savegame. This is used to prevent problems, in
  1259. // case more players in a game are supported later.
  1260. #define MIN_MAXPLAYERS 32
  1261. extern boolean setsizeneeded;
  1262. //CPhipps - savename variable redundant
  1263. /* killough 12/98:
  1264. * This function returns a signature for the current wad.
  1265. * It is used to distinguish between wads, for the purposes
  1266. * of savegame compatibility warnings, and options lookups.
  1267. */
  1268. static uint_64_t G_UpdateSignature(uint_64_t s, const char *name)
  1269. {
  1270. int i, lump = W_CheckNumForName(name);
  1271. if (lump != -1 && (i = lump+10) < numlumps)
  1272. do
  1273. {
  1274. int size = W_LumpLength(i);
  1275. const byte *p = W_CacheLumpNum(i);
  1276. while (size--)
  1277. s <<= 1, s += *p++;
  1278. W_UnlockLumpNum(i);
  1279. }
  1280. while (--i > lump);
  1281. return s;
  1282. }
  1283. static uint_64_t G_Signature(void)
  1284. {
  1285. static uint_64_t s = 0;
  1286. static boolean computed = false;
  1287. char name[9];
  1288. int episode, map;
  1289. if (!computed) {
  1290. computed = true;
  1291. if (gamemode == commercial)
  1292. for (map = haswolflevels ? 32 : 30; map; map--)
  1293. sprintf(name, "map%02d", map), s = G_UpdateSignature(s, name);
  1294. else
  1295. for (episode = gamemode==retail ? 4 :
  1296. gamemode==shareware ? 1 : 3; episode; episode--)
  1297. for (map = 9; map; map--)
  1298. sprintf(name, "E%dM%d", episode, map), s = G_UpdateSignature(s, name);
  1299. }
  1300. return s;
  1301. }
  1302. //
  1303. // killough 5/15/98: add forced loadgames, which allow user to override checks
  1304. //
  1305. void G_ForcedLoadGame(void)
  1306. {
  1307. // CPhipps - net loadgames are always forced, so we only reach here
  1308. // in single player
  1309. gameaction = ga_loadgame;
  1310. forced_loadgame = true;
  1311. }
  1312. // killough 3/16/98: add slot info
  1313. // killough 5/15/98: add command-line
  1314. void G_LoadGame(int slot, boolean command)
  1315. {
  1316. if (!demoplayback && !command) {
  1317. // CPhipps - handle savegame filename in G_DoLoadGame
  1318. // - Delay load so it can be communicated in net game
  1319. // - store info in special_event
  1320. special_event = BT_SPECIAL | (BTS_LOADGAME & BT_SPECIALMASK) |
  1321. ((slot << BTS_SAVESHIFT) & BTS_SAVEMASK);
  1322. forced_loadgame = netgame; // CPhipps - always force load netgames
  1323. } else {
  1324. // Do the old thing, immediate load
  1325. gameaction = ga_loadgame;
  1326. forced_loadgame = false;
  1327. savegameslot = slot;
  1328. demoplayback = false;
  1329. // Don't stay in netgame state if loading single player save
  1330. // while watching multiplayer demo
  1331. netgame = false;
  1332. }
  1333. command_loadgame = command;
  1334. R_SmoothPlaying_Reset(NULL); // e6y
  1335. }
  1336. // killough 5/15/98:
  1337. // Consistency Error when attempting to load savegame.
  1338. static void G_LoadGameErr(const char *msg)
  1339. {
  1340. Z_Free(savebuffer); // Free the savegame buffer
  1341. M_ForcedLoadGame(msg); // Print message asking for 'Y' to force
  1342. if (command_loadgame) // If this was a command-line -loadgame
  1343. {
  1344. D_StartTitle(); // Start the title screen
  1345. gamestate = GS_DEMOSCREEN; // And set the game state accordingly
  1346. }
  1347. }
  1348. // CPhipps - size of version header
  1349. #define VERSIONSIZE 16
  1350. const char * comp_lev_str[MAX_COMPATIBILITY_LEVEL] =
  1351. { "doom v1.2", "doom v1.666", "doom/doom2 v1.9", "ultimate doom", "final doom",
  1352. "dosdoom compatibility", "tasdoom compatibility", "\"boom compatibility\"", "boom v2.01", "boom v2.02", "lxdoom v1.3.2+",
  1353. "MBF", "PrBoom 2.03beta", "PrBoom v2.1.0-2.1.1", "PrBoom v2.1.2-v2.2.6",
  1354. "PrBoom v2.3.x", "PrBoom 2.4.0", "Current PrBoom" };
  1355. // comp_options_by_version removed - see G_Compatibility
  1356. static byte map_old_comp_levels[] =
  1357. { 0, 1, 2, 4, 5, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
  1358. static const struct {
  1359. int comp_level;
  1360. const char* ver_printf;
  1361. int version;
  1362. } version_headers[] = {
  1363. /* cph - we don't need a new version_header for prboom_3_comp/v2.1.1, since
  1364. * the file format is unchanged. */
  1365. { prboom_3_compatibility, "PrBoom %d", 210},
  1366. { prboom_5_compatibility, "PrBoom %d", 211},
  1367. { prboom_6_compatibility, "PrBoom %d", 212}
  1368. };
  1369. static const size_t num_version_headers = sizeof(version_headers) / sizeof(version_headers[0]);
  1370. boolean G_SaveGameValid() {
  1371. int length, i;
  1372. // CPhipps - do savegame filename stuff here
  1373. char name[PATH_MAX+1]; // killough 3/22/98
  1374. int savegame_compatibility = -1;
  1375. G_SaveGameName(name,sizeof(name),savegameslot, demoplayback);
  1376. gameaction = ga_nothing;
  1377. length = M_ReadFile(name, &savebuffer);
  1378. if (length<=0) {
  1379. return false;
  1380. }
  1381. return true;
  1382. }
  1383. void G_DoLoadGame(void)
  1384. {
  1385. int length, i;
  1386. // CPhipps - do savegame filename stuff here
  1387. char name[PATH_MAX+1]; // killough 3/22/98
  1388. int savegame_compatibility = -1;
  1389. G_SaveGameName(name,sizeof(name),savegameslot, demoplayback);
  1390. gameaction = ga_nothing;
  1391. length = M_ReadFile(name, &savebuffer);
  1392. if (length<=0)
  1393. I_Error("Couldn't read file %s: %s", name, "(Unknown Error)");
  1394. save_p = savebuffer + SAVESTRINGSIZE;
  1395. // CPhipps - read the description field, compare with supported ones
  1396. for (i=0; (size_t)i<num_version_headers; i++) {
  1397. char vcheck[VERSIONSIZE];
  1398. // killough 2/22/98: "proprietary" version string :-)
  1399. sprintf (vcheck, version_headers[i].ver_printf, version_headers[i].version);
  1400. if (!strncmp(save_p, vcheck, VERSIONSIZE)) {
  1401. savegame_compatibility = version_headers[i].comp_level;
  1402. i = num_version_headers;
  1403. }
  1404. }
  1405. if (savegame_compatibility == -1) {
  1406. if (forced_loadgame) {
  1407. savegame_compatibility = MAX_COMPATIBILITY_LEVEL-1;
  1408. } else {
  1409. G_LoadGameErr("Unrecognised savegame version!\nAre you sure? (y/n) ");
  1410. return;
  1411. }
  1412. }
  1413. save_p += VERSIONSIZE;
  1414. // CPhipps - always check savegames even when forced,
  1415. // only print a warning if forced
  1416. { // killough 3/16/98: check lump name checksum (independent of order)
  1417. uint_64_t checksum = 0;
  1418. checksum = G_Signature();
  1419. if (memcmp(&checksum, save_p, sizeof checksum)) {
  1420. if (!forced_loadgame) {
  1421. char *msg = malloc(strlen(save_p + sizeof checksum) + 128);
  1422. strcpy(msg,"Incompatible Savegame!!!\n");
  1423. if (save_p[sizeof checksum])
  1424. strcat(strcat(msg,"Wads expected:\n\n"), save_p + sizeof checksum);
  1425. strcat(msg, "\nAre you sure?");
  1426. G_LoadGameErr(msg);
  1427. free(msg);
  1428. return;
  1429. } else
  1430. lprintf(LO_WARN, "G_DoLoadGame: Incompatible savegame\n");
  1431. }
  1432. save_p += sizeof checksum;
  1433. }
  1434. save_p += strlen(save_p)+1;
  1435. compatibility_level = (savegame_compatibility >= prboom_4_compatibility) ? *save_p : savegame_compatibility;
  1436. if (savegame_compatibility < prboom_6_compatibility)
  1437. compatibility_level = map_old_comp_levels[compatibility_level];
  1438. save_p++;
  1439. gameskill = *save_p++;
  1440. gameepisode = *save_p++;
  1441. gamemap = *save_p++;
  1442. for (i=0 ; i<MAXPLAYERS ; i++)
  1443. playeringame[i] = *save_p++;
  1444. save_p += MIN_MAXPLAYERS-MAXPLAYERS; // killough 2/28/98
  1445. idmusnum = *save_p++; // jff 3/17/98 restore idmus music
  1446. if (idmusnum==255) idmusnum=-1; // jff 3/18/98 account for unsigned byte
  1447. /* killough 3/1/98: Read game options
  1448. * killough 11/98: move down to here
  1449. */
  1450. save_p = (char*)G_ReadOptions(save_p);
  1451. // load a base level
  1452. G_InitNew (gameskill, gameepisode, gamemap);
  1453. /* get the times - killough 11/98: save entire word */
  1454. memcpy(&leveltime, save_p, sizeof leveltime);
  1455. save_p += sizeof leveltime;
  1456. /* cph - total episode time */
  1457. if (compatibility_level >= prboom_2_compatibility) {
  1458. memcpy(&totalleveltimes, save_p, sizeof totalleveltimes);
  1459. save_p += sizeof totalleveltimes;
  1460. }
  1461. else totalleveltimes = 0;
  1462. // killough 11/98: load revenant tracer state
  1463. basetic = gametic - *save_p++;
  1464. // dearchive all the modifications
  1465. P_MapStart();
  1466. P_UnArchivePlayers ();
  1467. P_UnArchiveWorld ();
  1468. P_UnArchiveThinkers ();
  1469. P_UnArchiveSpecials ();
  1470. P_UnArchiveRNG (); // killough 1/18/98: load RNG information
  1471. P_UnArchiveMap (); // killough 1/22/98: load automap information
  1472. P_MapEnd();
  1473. R_SmoothPlaying_Reset(NULL); // e6y
  1474. if (*save_p != 0xe6)
  1475. I_Error ("G_DoLoadGame: Bad savegame");
  1476. // done
  1477. Z_Free (savebuffer);
  1478. if (setsizeneeded)
  1479. R_ExecuteSetViewSize ();
  1480. // draw the pattern into the back screen
  1481. R_FillBackScreen ();
  1482. /* killough 12/98: support -recordfrom and -loadgame -playdemo */
  1483. if (!command_loadgame)
  1484. singledemo = false; /* Clear singledemo flag if loading from menu */
  1485. else
  1486. if (singledemo) {
  1487. gameaction = ga_loadgame; /* Mark that we're loading a game before demo */
  1488. G_DoPlayDemo(); /* This will detect it and won't reinit level */
  1489. } else /* Command line + record means it's a recordfrom */
  1490. if (demorecording)
  1491. G_BeginRecording();
  1492. }
  1493. //
  1494. // G_SaveGame
  1495. // Called by the menu task.
  1496. // Description is a 24 byte text string
  1497. //
  1498. void G_SaveGame(int slot, char *description)
  1499. {
  1500. strcpy(savedescription, description);
  1501. if (demoplayback) {
  1502. /* cph - We're doing a user-initiated save game while a demo is
  1503. * running so, go outside normal mechanisms
  1504. */
  1505. savegameslot = slot;
  1506. G_DoSaveGame(true);
  1507. }
  1508. // CPhipps - store info in special_event
  1509. special_event = BT_SPECIAL | (BTS_SAVEGAME & BT_SPECIALMASK) |
  1510. ((slot << BTS_SAVESHIFT) & BTS_SAVEMASK);
  1511. #ifdef HAVE_NET
  1512. D_NetSendMisc(nm_savegamename, strlen(savedescription)+1, savedescription);
  1513. #endif
  1514. }
  1515. // Check for overrun and realloc if necessary -- Lee Killough 1/22/98
  1516. void (CheckSaveGame)(size_t size, const char* file, int line)
  1517. {
  1518. size_t pos = save_p - savebuffer;
  1519. #ifdef RANGECHECK
  1520. /* cph 2006/08/07 - after-the-fact sanity checking of CheckSaveGame calls */
  1521. static size_t prev_check;
  1522. static const char* prevf;
  1523. static int prevl;
  1524. if (pos > prev_check)
  1525. I_Error("CheckSaveGame at %s:%d called for insufficient buffer (%u < %u)", prevf, prevl, prev_check, pos);
  1526. prev_check = size + pos;
  1527. prevf = file;
  1528. prevl = line;
  1529. #endif
  1530. size += 1024; // breathing room
  1531. if (pos+size > savegamesize)
  1532. save_p = (savebuffer = realloc(savebuffer,
  1533. savegamesize += (size+1023) & ~1023)) + pos;
  1534. }
  1535. /* killough 3/22/98: form savegame name in one location
  1536. * (previously code was scattered around in multiple places)
  1537. * cph - Avoid possible buffer overflow problems by passing
  1538. * size to this function and using snprintf */
  1539. void G_SaveGameName(char *name, size_t size, int slot, boolean demoplayback)
  1540. {
  1541. const char* sgn = demoplayback ? "demosav" : savegamename;
  1542. #ifdef HAVE_SNPRINTF
  1543. snprintf (name, size, "%s/%s%d.dsg", basesavegame, sgn, slot);
  1544. #else
  1545. sprintf (name, "%s/%s%d.dsg", basesavegame, sgn, slot);
  1546. #endif
  1547. }
  1548. /* JDC: removed static */ void G_DoSaveGame (boolean menu)
  1549. {
  1550. char name[PATH_MAX+1];
  1551. char name2[VERSIONSIZE];
  1552. char *description;
  1553. int length, i;
  1554. gameaction = ga_nothing; // cph - cancel savegame at top of this function,
  1555. // in case later problems cause a premature exit
  1556. G_SaveGameName(name,sizeof(name),savegameslot, demoplayback && !menu);
  1557. description = savedescription;
  1558. save_p = savebuffer = malloc(savegamesize);
  1559. CheckSaveGame(SAVESTRINGSIZE+VERSIONSIZE+sizeof(uint_64_t));
  1560. memcpy (save_p, description, SAVESTRINGSIZE);
  1561. save_p += SAVESTRINGSIZE;
  1562. memset (name2,0,sizeof(name2));
  1563. // CPhipps - scan for the version header
  1564. for (i=0; (size_t)i<num_version_headers; i++)
  1565. if (version_headers[i].comp_level == best_compatibility) {
  1566. // killough 2/22/98: "proprietary" version string :-)
  1567. sprintf (name2,version_headers[i].ver_printf,version_headers[i].version);
  1568. memcpy (save_p, name2, VERSIONSIZE);
  1569. i = num_version_headers+1;
  1570. }
  1571. save_p += VERSIONSIZE;
  1572. { /* killough 3/16/98, 12/98: store lump name checksum */
  1573. uint_64_t checksum = G_Signature();
  1574. memcpy(save_p, &checksum, sizeof checksum);
  1575. save_p += sizeof checksum;
  1576. }
  1577. // killough 3/16/98: store pwad filenames in savegame
  1578. {
  1579. // CPhipps - changed for new wadfiles handling
  1580. size_t i;
  1581. for (i = 0; i<numwadfiles; i++)
  1582. {
  1583. const char *const w = wadfiles[i].name;
  1584. CheckSaveGame(strlen(w)+2);
  1585. strcpy(save_p, w);
  1586. save_p += strlen(save_p);
  1587. *save_p++ = '\n';
  1588. }
  1589. *save_p++ = 0;
  1590. }
  1591. CheckSaveGame(GAME_OPTION_SIZE+MIN_MAXPLAYERS+14);
  1592. *save_p++ = compatibility_level;
  1593. *save_p++ = gameskill;
  1594. *save_p++ = gameepisode;
  1595. *save_p++ = gamemap;
  1596. for (i=0 ; i<MAXPLAYERS ; i++)
  1597. *save_p++ = playeringame[i];
  1598. for (;i<MIN_MAXPLAYERS;i++) // killough 2/28/98
  1599. *save_p++ = 0;
  1600. *save_p++ = idmusnum; // jff 3/17/98 save idmus state
  1601. save_p = G_WriteOptions(save_p); // killough 3/1/98: save game options
  1602. /* cph - FIXME - endianness? */
  1603. /* killough 11/98: save entire word */
  1604. memcpy(save_p, &leveltime, sizeof leveltime);
  1605. save_p += sizeof leveltime;
  1606. /* cph - total episode time */
  1607. if (compatibility_level >= prboom_2_compatibility) {
  1608. memcpy(save_p, &totalleveltimes, sizeof totalleveltimes);
  1609. save_p += sizeof totalleveltimes;
  1610. }
  1611. else totalleveltimes = 0;
  1612. // killough 11/98: save revenant tracer state
  1613. *save_p++ = (gametic-basetic) & 255;
  1614. // killough 3/22/98: add Z_CheckHeap after each call to ensure consistency
  1615. Z_CheckHeap();
  1616. P_ArchivePlayers();
  1617. Z_CheckHeap();
  1618. // phares 9/13/98: Move mobj_t->index out of P_ArchiveThinkers so the
  1619. // indices can be used by P_ArchiveWorld when the sectors are saved.
  1620. // This is so we can save the index of the mobj_t of the thinker that
  1621. // caused a sound, referenced by sector_t->soundtarget.
  1622. P_ThinkerToIndex();
  1623. P_ArchiveWorld();
  1624. Z_CheckHeap();
  1625. P_ArchiveThinkers();
  1626. // phares 9/13/98: Move index->mobj_t out of P_ArchiveThinkers, simply
  1627. // for symmetry with the P_ThinkerToIndex call above.
  1628. P_IndexToThinker();
  1629. Z_CheckHeap();
  1630. P_ArchiveSpecials();
  1631. P_ArchiveRNG(); // killough 1/18/98: save RNG information
  1632. Z_CheckHeap();
  1633. P_ArchiveMap(); // killough 1/22/98: save automap information
  1634. *save_p++ = 0xe6; // consistancy marker
  1635. length = save_p - savebuffer;
  1636. Z_CheckHeap();
  1637. doom_printf( "%s", M_WriteFile(name, savebuffer, length)
  1638. ? s_GGSAVED /* Ty - externalised */
  1639. : "Game save failed!"); // CPhipps - not externalised
  1640. free(savebuffer); // killough
  1641. savebuffer = save_p = NULL;
  1642. savedescription[0] = 0;
  1643. }
  1644. static skill_t d_skill;
  1645. static int d_episode;
  1646. static int d_map;
  1647. void G_DeferedInitNew(skill_t skill, int episode, int map)
  1648. {
  1649. d_skill = skill;
  1650. d_episode = episode;
  1651. d_map = map;
  1652. gameaction = ga_newgame;
  1653. }
  1654. /* cph -
  1655. * G_Compatibility
  1656. *
  1657. * Initialises the comp[] array based on the compatibility_level
  1658. * For reference, MBF did:
  1659. * for (i=0; i < COMP_TOTAL; i++)
  1660. * comp[i] = compatibility;
  1661. *
  1662. * Instead, we have a lookup table showing at what version a fix was
  1663. * introduced, and made optional (replaces comp_options_by_version)
  1664. */
  1665. void G_Compatibility(void)
  1666. {
  1667. static const struct {
  1668. complevel_t fix; // level at which fix/change was introduced
  1669. complevel_t opt; // level at which fix/change was made optional
  1670. } levels[] = {
  1671. // comp_telefrag - monsters used to telefrag only on MAP30, now they do it for spawners only
  1672. { mbf_compatibility, mbf_compatibility },
  1673. // comp_dropoff - MBF encourages things to drop off of overhangs
  1674. { mbf_compatibility, mbf_compatibility },
  1675. // comp_vile - original Doom archville bugs like ghosts
  1676. { boom_compatibility, mbf_compatibility },
  1677. // comp_pain - original Doom limits Pain Elementals from spawning too many skulls
  1678. { boom_compatibility, mbf_compatibility },
  1679. // comp_skull - original Doom let skulls be spit through walls by Pain Elementals
  1680. { boom_compatibility, mbf_compatibility },
  1681. // comp_blazing - original Doom duplicated blazing door sound
  1682. { boom_compatibility, mbf_compatibility },
  1683. // e6y: "Tagged doors don't trigger special lighting" handled wrong
  1684. // http://sourceforge.net/tracker/index.php?func=detail&aid=1411400&group_id=148658&atid=772943
  1685. // comp_doorlight - MBF made door lighting changes more gradual
  1686. { boom_compatibility, mbf_compatibility },
  1687. // comp_model - improvements to the game physics
  1688. { boom_compatibility, mbf_compatibility },
  1689. // comp_god - fixes to God mode
  1690. { boom_compatibility, mbf_compatibility },
  1691. // comp_falloff - MBF encourages things to drop off of overhangs
  1692. { mbf_compatibility, mbf_compatibility },
  1693. // comp_floors - fixes for moving floors bugs
  1694. { boom_compatibility_compatibility, mbf_compatibility },
  1695. // comp_skymap
  1696. { boom_compatibility, mbf_compatibility },
  1697. // comp_pursuit - MBF AI change, limited pursuit?
  1698. { mbf_compatibility, mbf_compatibility },
  1699. // comp_doorstuck - monsters stuck in doors fix
  1700. { boom_202_compatibility, mbf_compatibility },
  1701. // comp_staylift - MBF AI change, monsters try to stay on lifts
  1702. { mbf_compatibility, mbf_compatibility },
  1703. // comp_zombie - prevent dead players triggering stuff
  1704. { lxdoom_1_compatibility, mbf_compatibility },
  1705. // comp_stairs - see p_floor.c
  1706. { boom_202_compatibility, mbf_compatibility },
  1707. // comp_infcheat - FIXME
  1708. { mbf_compatibility, mbf_compatibility },
  1709. // comp_zerotags - allow zero tags in wads */
  1710. { boom_compatibility, mbf_compatibility },
  1711. // comp_moveblock - enables keygrab and mancubi shots going thru walls
  1712. { lxdoom_1_compatibility, prboom_2_compatibility },
  1713. // comp_respawn - objects which aren't on the map at game start respawn at (0,0)
  1714. { prboom_2_compatibility, prboom_2_compatibility },
  1715. // comp_sound - see s_sound.c
  1716. { boom_compatibility_compatibility, prboom_3_compatibility },
  1717. // comp_666 - enables tag 666 in non-ExM8 levels
  1718. { ultdoom_compatibility, prboom_4_compatibility },
  1719. // comp_soul - enables lost souls bouncing (see P_ZMovement)
  1720. { prboom_4_compatibility, prboom_4_compatibility },
  1721. // comp_maskedanim - 2s mid textures don't animate
  1722. { doom_1666_compatibility, prboom_4_compatibility },
  1723. };
  1724. int i;
  1725. if (sizeof(levels)/sizeof(*levels) != COMP_NUM)
  1726. I_Error("G_Compatibility: consistency error");
  1727. for (i = 0; i < sizeof(levels)/sizeof(*levels); i++)
  1728. if (compatibility_level < levels[i].opt)
  1729. comp[i] = (compatibility_level < levels[i].fix);
  1730. if (!mbf_features) {
  1731. monster_infighting = 1;
  1732. monster_backing = 0;
  1733. monster_avoid_hazards = 0;
  1734. monster_friction = 0;
  1735. help_friends = 0;
  1736. #ifdef DOGS
  1737. dogs = 0;
  1738. dog_jumping = 0;
  1739. #endif
  1740. monkeys = 0;
  1741. }
  1742. }
  1743. #ifdef DOGS
  1744. /* killough 7/19/98: Marine's best friend :) */
  1745. static int G_GetHelpers(void)
  1746. {
  1747. int j = M_CheckParm ("-dog");
  1748. if (!j)
  1749. j = M_CheckParm ("-dogs");
  1750. return j ? j+1 < myargc ? atoi(myargv[j+1]) : 1 : default_dogs;
  1751. }
  1752. #endif
  1753. // killough 3/1/98: function to reload all the default parameter
  1754. // settings before a new game begins
  1755. void G_ReloadDefaults(void)
  1756. {
  1757. // killough 3/1/98: Initialize options based on config file
  1758. // (allows functions above to load different values for demos
  1759. // and savegames without messing up defaults).
  1760. weapon_recoil = default_weapon_recoil; // weapon recoil
  1761. player_bobbing = default_player_bobbing; // whether player bobs or not
  1762. /* cph 2007/06/31 - for some reason, the default_* of the next 2 vars was never implemented */
  1763. variable_friction = default_variable_friction;
  1764. allow_pushers = default_allow_pushers;
  1765. monsters_remember = default_monsters_remember; // remember former enemies
  1766. monster_infighting = default_monster_infighting; // killough 7/19/98
  1767. #ifdef DOGS
  1768. dogs = netgame ? 0 : G_GetHelpers(); // killough 7/19/98
  1769. dog_jumping = default_dog_jumping;
  1770. #endif
  1771. distfriend = default_distfriend; // killough 8/8/98
  1772. monster_backing = default_monster_backing; // killough 9/8/98
  1773. monster_avoid_hazards = default_monster_avoid_hazards; // killough 9/9/98
  1774. monster_friction = default_monster_friction; // killough 10/98
  1775. help_friends = default_help_friends; // killough 9/9/98
  1776. monkeys = default_monkeys;
  1777. // jff 1/24/98 reset play mode to command line spec'd version
  1778. // killough 3/1/98: moved to here
  1779. respawnparm = clrespawnparm;
  1780. fastparm = clfastparm;
  1781. nomonsters = clnomonsters;
  1782. //jff 3/24/98 set startskill from defaultskill in config file, unless
  1783. // it has already been set by a -skill parameter
  1784. if (startskill==sk_none)
  1785. startskill = (skill_t)(defaultskill-1);
  1786. demoplayback = false;
  1787. singledemo = false; // killough 9/29/98: don't stop after 1 demo
  1788. netdemo = false;
  1789. // killough 2/21/98:
  1790. memset(playeringame+1, 0, sizeof(*playeringame)*(MAXPLAYERS-1));
  1791. consoleplayer = 0;
  1792. compatibility_level = default_compatibility_level;
  1793. {
  1794. int i = M_CheckParm("-complevel");
  1795. if (i && (1+i) < myargc) {
  1796. int l = atoi(myargv[i+1]);;
  1797. if (l >= -1) compatibility_level = l;
  1798. }
  1799. }
  1800. if (compatibility_level == -1)
  1801. compatibility_level = best_compatibility;
  1802. if (mbf_features)
  1803. memcpy(comp, default_comp, sizeof comp);
  1804. G_Compatibility();
  1805. // killough 3/31/98, 4/5/98: demo sync insurance
  1806. demo_insurance = default_demo_insurance == 1;
  1807. rngseed += I_GetRandomTimeSeed() + gametic; // CPhipps
  1808. }
  1809. void G_DoNewGame (void)
  1810. {
  1811. G_ReloadDefaults(); // killough 3/1/98
  1812. netgame = false; // killough 3/29/98
  1813. deathmatch = false;
  1814. G_InitNew (d_skill, d_episode, d_map);
  1815. gameaction = ga_nothing;
  1816. //jff 4/26/98 wake up the status bar in case were coming out of a DM demo
  1817. ST_Start();
  1818. }
  1819. // killough 4/10/98: New function to fix bug which caused Doom
  1820. // lockups when idclev was used in conjunction with -fast.
  1821. void G_SetFastParms(int fast_pending)
  1822. {
  1823. static int fast = 0; // remembers fast state
  1824. int i;
  1825. if (fast != fast_pending) { /* only change if necessary */
  1826. if ((fast = fast_pending))
  1827. {
  1828. for (i=S_SARG_RUN1; i<=S_SARG_PAIN2; i++)
  1829. if (states[i].tics != 1 || demo_compatibility) // killough 4/10/98
  1830. states[i].tics >>= 1; // don't change 1->0 since it causes cycles
  1831. mobjinfo[MT_BRUISERSHOT].speed = 20*FRACUNIT;
  1832. mobjinfo[MT_HEADSHOT].speed = 20*FRACUNIT;
  1833. mobjinfo[MT_TROOPSHOT].speed = 20*FRACUNIT;
  1834. }
  1835. else
  1836. {
  1837. for (i=S_SARG_RUN1; i<=S_SARG_PAIN2; i++)
  1838. states[i].tics <<= 1;
  1839. mobjinfo[MT_BRUISERSHOT].speed = 15*FRACUNIT;
  1840. mobjinfo[MT_HEADSHOT].speed = 10*FRACUNIT;
  1841. mobjinfo[MT_TROOPSHOT].speed = 10*FRACUNIT;
  1842. }
  1843. }
  1844. }
  1845. //
  1846. // G_InitNew
  1847. // Can be called by the startup code or the menu task,
  1848. // consoleplayer, displayplayer, playeringame[] should be set.
  1849. //
  1850. void G_InitNew(skill_t skill, int episode, int map)
  1851. {
  1852. int i;
  1853. if (paused)
  1854. {
  1855. paused = false;
  1856. S_ResumeSound();
  1857. }
  1858. if (skill > sk_nightmare)
  1859. skill = sk_nightmare;
  1860. if (episode < 1)
  1861. episode = 1;
  1862. if (gamemode == retail)
  1863. {
  1864. if (episode > 4)
  1865. episode = 4;
  1866. }
  1867. else
  1868. if (gamemode == shareware)
  1869. {
  1870. if (episode > 1)
  1871. episode = 1; // only start episode 1 on shareware
  1872. }
  1873. else
  1874. if (episode > 3)
  1875. episode = 3;
  1876. if (map < 1)
  1877. map = 1;
  1878. if (map > 9 && gamemode != commercial)
  1879. map = 9;
  1880. G_SetFastParms(fastparm || skill == sk_nightmare); // killough 4/10/98
  1881. M_ClearRandom();
  1882. respawnmonsters = skill == sk_nightmare || respawnparm;
  1883. // force players to be initialized upon first level load
  1884. for (i=0 ; i<MAXPLAYERS ; i++)
  1885. players[i].playerstate = PST_REBORN;
  1886. usergame = true; // will be set false if a demo
  1887. paused = false;
  1888. automapmode &= ~am_active;
  1889. gameepisode = episode;
  1890. gamemap = map;
  1891. gameskill = skill;
  1892. totalleveltimes = 0; // cph
  1893. //jff 4/16/98 force marks on automap cleared every new level start
  1894. AM_clearMarks();
  1895. G_DoLoadLevel ();
  1896. }
  1897. //
  1898. // DEMO RECORDING
  1899. //
  1900. #define DEMOMARKER 0x80
  1901. void G_ReadDemoTiccmd (ticcmd_t* cmd)
  1902. {
  1903. unsigned char at; // e6y: tasdoom stuff
  1904. if (*demo_p == DEMOMARKER)
  1905. G_CheckDemoStatus(); // end of demo data stream
  1906. else if (demoplayback && demo_p + (longtics?5:4) > demobuffer + demolength)
  1907. {
  1908. lprintf(LO_WARN, "G_ReadDemoTiccmd: missing DEMOMARKER\n");
  1909. G_CheckDemoStatus();
  1910. }
  1911. else
  1912. {
  1913. cmd->forwardmove = ((signed char)*demo_p++);
  1914. cmd->sidemove = ((signed char)*demo_p++);
  1915. if (!longtics) {
  1916. cmd->angleturn = ((unsigned char)(at = *demo_p++))<<8;
  1917. } else {
  1918. unsigned int lowbyte = (unsigned char)*demo_p++;
  1919. cmd->angleturn = (((signed int)(*demo_p++))<<8) + lowbyte;
  1920. }
  1921. cmd->buttons = (unsigned char)*demo_p++;
  1922. // e6y: ability to play tasdoom demos directly
  1923. if (compatibility_level == tasdoom_compatibility)
  1924. {
  1925. signed char k = cmd->forwardmove;
  1926. cmd->forwardmove = cmd->sidemove;
  1927. cmd->sidemove = (signed char)at;
  1928. cmd->angleturn = ((unsigned char)cmd->buttons)<<8;
  1929. cmd->buttons = (byte)k;
  1930. }
  1931. }
  1932. }
  1933. /* Demo limits removed -- killough
  1934. * cph - record straight to file
  1935. */
  1936. void G_WriteDemoTiccmd (ticcmd_t* cmd)
  1937. {
  1938. char buf[5];
  1939. char *p = buf;
  1940. *p++ = cmd->forwardmove;
  1941. *p++ = cmd->sidemove;
  1942. if (!longtics) {
  1943. *p++ = (cmd->angleturn+128)>>8;
  1944. } else {
  1945. signed short a = cmd->angleturn;
  1946. *p++ = a & 0xff;
  1947. *p++ = (a >> 8) & 0xff;
  1948. }
  1949. *p++ = cmd->buttons;
  1950. if (fwrite(buf, p-buf, 1, demofp) != 1)
  1951. I_Error("G_WriteDemoTiccmd: error writing demo");
  1952. /* cph - alias demo_p to it so we can read it back */
  1953. demo_p = buf;
  1954. G_ReadDemoTiccmd (cmd); // make SURE it is exactly the same
  1955. }
  1956. //
  1957. // G_RecordDemo
  1958. //
  1959. void G_RecordDemo (const char* name)
  1960. {
  1961. char demoname[PATH_MAX];
  1962. usergame = false;
  1963. AddDefaultExtension(strcpy(demoname, name), ".lmp"); // 1/18/98 killough
  1964. demorecording = true;
  1965. /* cph - Record demos straight to file
  1966. * If file already exists, try to continue existing demo
  1967. */
  1968. if (access(demoname, F_OK)) {
  1969. demofp = fopen(demoname, "wb");
  1970. } else {
  1971. demofp = fopen(demoname, "r+");
  1972. if (demofp) {
  1973. int slot = -1;
  1974. int rc;
  1975. int bytes_per_tic;
  1976. const byte* pos;
  1977. { /* Read the demo header for options etc */
  1978. byte buf[200];
  1979. size_t len = fread(buf, 1, sizeof(buf), demofp);
  1980. pos = G_ReadDemoHeader(buf, len, false);
  1981. if (pos)
  1982. {
  1983. fseek(demofp, pos - buf, SEEK_SET);
  1984. }
  1985. }
  1986. bytes_per_tic = longtics ? 5 : 4;
  1987. if (pos)
  1988. /* Now read the demo to find the last save slot */
  1989. do {
  1990. byte buf[5];
  1991. rc = fread(buf, 1, bytes_per_tic, demofp);
  1992. if (buf[0] == DEMOMARKER) break;
  1993. if (buf[bytes_per_tic-1] & BT_SPECIAL)
  1994. if ((buf[bytes_per_tic-1] & BT_SPECIALMASK) == BTS_SAVEGAME)
  1995. slot = (buf[bytes_per_tic-1] & BTS_SAVEMASK)>>BTS_SAVESHIFT;
  1996. } while (rc == bytes_per_tic);
  1997. if (slot == -1) I_Error("G_RecordDemo: No save in demo, can't continue");
  1998. /* Return to the last save position, and load the relevant savegame */
  1999. fseek(demofp, -rc, SEEK_CUR);
  2000. G_LoadGame(slot, false);
  2001. autostart = false;
  2002. }
  2003. }
  2004. if (!demofp) I_Error("G_RecordDemo: failed to open %s", name);
  2005. }
  2006. // These functions are used to read and write game-specific options in demos
  2007. // and savegames so that demo sync is preserved and savegame restoration is
  2008. // complete. Not all options (for example "compatibility"), however, should
  2009. // be loaded and saved here. It is extremely important to use the same
  2010. // positions as before for the variables, so if one becomes obsolete, the
  2011. // byte(s) should still be skipped over or padded with 0's.
  2012. // Lee Killough 3/1/98
  2013. extern int forceOldBsp;
  2014. byte *G_WriteOptions(byte *demo_p)
  2015. {
  2016. byte *target = demo_p + GAME_OPTION_SIZE;
  2017. *demo_p++ = monsters_remember; // part of monster AI
  2018. *demo_p++ = variable_friction; // ice & mud
  2019. *demo_p++ = weapon_recoil; // weapon recoil
  2020. *demo_p++ = allow_pushers; // MT_PUSH Things
  2021. *demo_p++ = 0;
  2022. *demo_p++ = player_bobbing; // whether player bobs or not
  2023. // killough 3/6/98: add parameters to savegame, move around some in demos
  2024. *demo_p++ = respawnparm;
  2025. *demo_p++ = fastparm;
  2026. *demo_p++ = nomonsters;
  2027. *demo_p++ = demo_insurance; // killough 3/31/98
  2028. // killough 3/26/98: Added rngseed. 3/31/98: moved here
  2029. *demo_p++ = (byte)((rngseed >> 24) & 0xff);
  2030. *demo_p++ = (byte)((rngseed >> 16) & 0xff);
  2031. *demo_p++ = (byte)((rngseed >> 8) & 0xff);
  2032. *demo_p++ = (byte)( rngseed & 0xff);
  2033. // Options new to v2.03 begin here
  2034. *demo_p++ = monster_infighting; // killough 7/19/98
  2035. #ifdef DOGS
  2036. *demo_p++ = dogs; // killough 7/19/98
  2037. #else
  2038. *demo_p++ = 0;
  2039. #endif
  2040. *demo_p++ = 0;
  2041. *demo_p++ = 0;
  2042. *demo_p++ = (distfriend >> 8) & 0xff; // killough 8/8/98
  2043. *demo_p++ = distfriend & 0xff; // killough 8/8/98
  2044. *demo_p++ = monster_backing; // killough 9/8/98
  2045. *demo_p++ = monster_avoid_hazards; // killough 9/9/98
  2046. *demo_p++ = monster_friction; // killough 10/98
  2047. *demo_p++ = help_friends; // killough 9/9/98
  2048. #ifdef DOGS
  2049. *demo_p++ = dog_jumping;
  2050. #else
  2051. *demo_p++ = 0;
  2052. #endif
  2053. *demo_p++ = monkeys;
  2054. { // killough 10/98: a compatibility vector now
  2055. int i;
  2056. for (i=0; i < COMP_TOTAL; i++)
  2057. *demo_p++ = comp[i] != 0;
  2058. }
  2059. *demo_p++ = (compatibility_level >= prboom_2_compatibility) && forceOldBsp; // cph 2002/07/20
  2060. //----------------
  2061. // Padding at end
  2062. //----------------
  2063. while (demo_p < target)
  2064. *demo_p++ = 0;
  2065. if (demo_p != target)
  2066. I_Error("G_WriteOptions: GAME_OPTION_SIZE is too small");
  2067. return target;
  2068. }
  2069. /* Same, but read instead of write
  2070. * cph - const byte*'s
  2071. */
  2072. const byte *G_ReadOptions(const byte *demo_p)
  2073. {
  2074. const byte *target = demo_p + GAME_OPTION_SIZE;
  2075. monsters_remember = *demo_p++;
  2076. variable_friction = *demo_p; // ice & mud
  2077. demo_p++;
  2078. weapon_recoil = *demo_p; // weapon recoil
  2079. demo_p++;
  2080. allow_pushers = *demo_p; // MT_PUSH Things
  2081. demo_p++;
  2082. demo_p++;
  2083. player_bobbing = *demo_p; // whether player bobs or not
  2084. demo_p++;
  2085. // killough 3/6/98: add parameters to savegame, move from demo
  2086. respawnparm = *demo_p++;
  2087. fastparm = *demo_p++;
  2088. nomonsters = *demo_p++;
  2089. demo_insurance = *demo_p++; // killough 3/31/98
  2090. // killough 3/26/98: Added rngseed to demos; 3/31/98: moved here
  2091. rngseed = *demo_p++ & 0xff;
  2092. rngseed <<= 8;
  2093. rngseed += *demo_p++ & 0xff;
  2094. rngseed <<= 8;
  2095. rngseed += *demo_p++ & 0xff;
  2096. rngseed <<= 8;
  2097. rngseed += *demo_p++ & 0xff;
  2098. // Options new to v2.03
  2099. if (mbf_features)
  2100. {
  2101. monster_infighting = *demo_p++; // killough 7/19/98
  2102. #ifdef DOGS
  2103. dogs = *demo_p++; // killough 7/19/98
  2104. #else
  2105. demo_p++;
  2106. #endif
  2107. demo_p += 2;
  2108. distfriend = *demo_p++ << 8; // killough 8/8/98
  2109. distfriend+= *demo_p++;
  2110. monster_backing = *demo_p++; // killough 9/8/98
  2111. monster_avoid_hazards = *demo_p++; // killough 9/9/98
  2112. monster_friction = *demo_p++; // killough 10/98
  2113. help_friends = *demo_p++; // killough 9/9/98
  2114. #ifdef DOGS
  2115. dog_jumping = *demo_p++; // killough 10/98
  2116. #else
  2117. demo_p++;
  2118. #endif
  2119. monkeys = *demo_p++;
  2120. { // killough 10/98: a compatibility vector now
  2121. int i;
  2122. for (i=0; i < COMP_TOTAL; i++)
  2123. comp[i] = *demo_p++;
  2124. }
  2125. forceOldBsp = *demo_p++; // cph 2002/07/20
  2126. }
  2127. else /* defaults for versions <= 2.02 */
  2128. {
  2129. /* G_Compatibility will set these */
  2130. }
  2131. G_Compatibility();
  2132. return target;
  2133. }
  2134. void G_BeginRecording (void)
  2135. {
  2136. int i;
  2137. byte *demostart, *demo_p;
  2138. demostart = demo_p = malloc(1000);
  2139. longtics = 0;
  2140. /* cph - 3 demo record formats supported: MBF+, BOOM, and Doom v1.9 */
  2141. if (mbf_features) {
  2142. { /* Write version code into demo */
  2143. unsigned char v;
  2144. switch(compatibility_level) {
  2145. case mbf_compatibility: v = 203; break; // e6y: Bug in MBF compatibility mode fixed
  2146. case prboom_2_compatibility: v = 210; break;
  2147. case prboom_3_compatibility: v = 211; break;
  2148. case prboom_4_compatibility: v = 212; break;
  2149. case prboom_5_compatibility: v = 213; break;
  2150. case prboom_6_compatibility:
  2151. v = 214;
  2152. longtics = 1;
  2153. break;
  2154. }
  2155. *demo_p++ = v;
  2156. }
  2157. // signature
  2158. *demo_p++ = 0x1d;
  2159. *demo_p++ = 'M';
  2160. *demo_p++ = 'B';
  2161. *demo_p++ = 'F';
  2162. *demo_p++ = 0xe6;
  2163. *demo_p++ = '\0';
  2164. /* killough 2/22/98: save compatibility flag in new demos
  2165. * cph - FIXME? MBF demos will always be not in compat. mode */
  2166. *demo_p++ = 0;
  2167. *demo_p++ = gameskill;
  2168. *demo_p++ = gameepisode;
  2169. *demo_p++ = gamemap;
  2170. *demo_p++ = deathmatch;
  2171. *demo_p++ = consoleplayer;
  2172. demo_p = G_WriteOptions(demo_p); // killough 3/1/98: Save game options
  2173. for (i=0 ; i<MAXPLAYERS ; i++)
  2174. *demo_p++ = playeringame[i];
  2175. // killough 2/28/98:
  2176. // We always store at least MIN_MAXPLAYERS bytes in demo, to
  2177. // support enhancements later w/o losing demo compatibility
  2178. for (; i<MIN_MAXPLAYERS; i++)
  2179. *demo_p++ = 0;
  2180. } else if (compatibility_level > boom_compatibility_compatibility) {
  2181. byte v, c; /* Nominally, version and compatibility bits */
  2182. switch (compatibility_level) {
  2183. case boom_compatibility_compatibility: v = 202, c = 1; break;
  2184. case boom_201_compatibility: v = 201; c = 0; break;
  2185. case boom_202_compatibility: v = 202, c = 0; break;
  2186. default: I_Error("G_BeginRecording: Boom compatibility level unrecognised?");
  2187. }
  2188. *demo_p++ = v;
  2189. // signature
  2190. *demo_p++ = 0x1d;
  2191. *demo_p++ = 'B';
  2192. *demo_p++ = 'o';
  2193. *demo_p++ = 'o';
  2194. *demo_p++ = 'm';
  2195. *demo_p++ = 0xe6;
  2196. /* CPhipps - save compatibility level in demos */
  2197. *demo_p++ = c;
  2198. *demo_p++ = gameskill;
  2199. *demo_p++ = gameepisode;
  2200. *demo_p++ = gamemap;
  2201. *demo_p++ = deathmatch;
  2202. *demo_p++ = consoleplayer;
  2203. demo_p = G_WriteOptions(demo_p); // killough 3/1/98: Save game options
  2204. for (i=0 ; i<MAXPLAYERS ; i++)
  2205. *demo_p++ = playeringame[i];
  2206. // killough 2/28/98:
  2207. // We always store at least MIN_MAXPLAYERS bytes in demo, to
  2208. // support enhancements later w/o losing demo compatibility
  2209. for (; i<MIN_MAXPLAYERS; i++)
  2210. *demo_p++ = 0;
  2211. } else { // cph - write old v1.9 demos (might even sync)
  2212. longtics = M_CheckParm("-longtics");
  2213. *demo_p++ = longtics ? 111 : 109; // v1.9 has best chance of syncing these
  2214. *demo_p++ = gameskill;
  2215. *demo_p++ = gameepisode;
  2216. *demo_p++ = gamemap;
  2217. *demo_p++ = deathmatch;
  2218. *demo_p++ = respawnparm;
  2219. *demo_p++ = fastparm;
  2220. *demo_p++ = nomonsters;
  2221. *demo_p++ = consoleplayer;
  2222. for (i=0; i<4; i++) // intentionally hard-coded 4 -- killough
  2223. *demo_p++ = playeringame[i];
  2224. }
  2225. if (fwrite(demostart, 1, demo_p-demostart, demofp) != (size_t)(demo_p-demostart))
  2226. I_Error("G_BeginRecording: Error writing demo header");
  2227. free(demostart);
  2228. }
  2229. //
  2230. // G_PlayDemo
  2231. //
  2232. static const char *defdemoname;
  2233. void G_DeferedPlayDemo (const char* name)
  2234. {
  2235. defdemoname = name;
  2236. gameaction = ga_playdemo;
  2237. }
  2238. static int demolumpnum = -1;
  2239. static int G_GetOriginalDoomCompatLevel(int ver)
  2240. {
  2241. {
  2242. int lev;
  2243. int i = M_CheckParm("-complevel");
  2244. if (i && (i+1 < myargc))
  2245. {
  2246. lev = atoi(myargv[i+1]);
  2247. if (lev>=0)
  2248. return lev;
  2249. }
  2250. }
  2251. if (ver < 107) return doom_1666_compatibility;
  2252. if (gamemode == retail) return ultdoom_compatibility;
  2253. if (gamemission >= pack_tnt) return finaldoom_compatibility;
  2254. return doom2_19_compatibility;
  2255. }
  2256. //e6y: Check for overrun
  2257. static boolean CheckForOverrun(const byte *start_p, const byte *current_p, size_t maxsize, size_t size, boolean failonerror)
  2258. {
  2259. size_t pos = current_p - start_p;
  2260. if (pos + size > maxsize)
  2261. {
  2262. if (failonerror)
  2263. I_Error("G_ReadDemoHeader: wrong demo header\n");
  2264. else
  2265. return true;
  2266. }
  2267. return false;
  2268. }
  2269. static const byte* G_ReadDemoHeader(const byte *demo_p, size_t size, boolean failonerror)
  2270. {
  2271. skill_t skill;
  2272. int i, episode, map;
  2273. // e6y
  2274. // The local variable should be used instead of demobuffer,
  2275. // because demobuffer can be uninitialized
  2276. const byte *header_p = demo_p;
  2277. const byte *option_p = NULL; /* killough 11/98 */
  2278. basetic = gametic; // killough 9/29/98
  2279. // killough 2/22/98, 2/28/98: autodetect old demos and act accordingly.
  2280. // Old demos turn on demo_compatibility => compatibility; new demos load
  2281. // compatibility flag, and other flags as well, as a part of the demo.
  2282. //e6y: check for overrun
  2283. if (CheckForOverrun(header_p, demo_p, size, 1, failonerror))
  2284. return NULL;
  2285. demover = *demo_p++;
  2286. longtics = 0;
  2287. // e6y
  2288. // Handling of unrecognized demo formats
  2289. // Versions up to 1.2 use a 7-byte header - first byte is a skill level.
  2290. // Versions after 1.2 use a 13-byte header - first byte is a demoversion.
  2291. // BOOM's demoversion starts from 200
  2292. if (!((demover >= 0 && demover <= 4) ||
  2293. (demover >= 104 && demover <= 111) ||
  2294. (demover >= 200 && demover <= 214)))
  2295. {
  2296. I_Error("G_ReadDemoHeader: Unknown demo format %d.", demover);
  2297. }
  2298. if (demover < 200) // Autodetect old demos
  2299. {
  2300. if (demover >= 111) longtics = 1;
  2301. // killough 3/2/98: force these variables to be 0 in demo_compatibility
  2302. variable_friction = 0;
  2303. weapon_recoil = 0;
  2304. allow_pushers = 0;
  2305. monster_infighting = 1; // killough 7/19/98
  2306. #ifdef DOGS
  2307. dogs = 0; // killough 7/19/98
  2308. dog_jumping = 0; // killough 10/98
  2309. #endif
  2310. monster_backing = 0; // killough 9/8/98
  2311. monster_avoid_hazards = 0; // killough 9/9/98
  2312. monster_friction = 0; // killough 10/98
  2313. help_friends = 0; // killough 9/9/98
  2314. monkeys = 0;
  2315. // killough 3/6/98: rearrange to fix savegame bugs (moved fastparm,
  2316. // respawnparm, nomonsters flags to G_LoadOptions()/G_SaveOptions())
  2317. if ((skill=demover) >= 100) // For demos from versions >= 1.4
  2318. {
  2319. //e6y: check for overrun
  2320. if (CheckForOverrun(header_p, demo_p, size, 8, failonerror))
  2321. return NULL;
  2322. compatibility_level = G_GetOriginalDoomCompatLevel(demover);
  2323. skill = *demo_p++;
  2324. episode = *demo_p++;
  2325. map = *demo_p++;
  2326. deathmatch = *demo_p++;
  2327. respawnparm = *demo_p++;
  2328. fastparm = *demo_p++;
  2329. nomonsters = *demo_p++;
  2330. consoleplayer = *demo_p++;
  2331. }
  2332. else
  2333. {
  2334. //e6y: check for overrun
  2335. if (CheckForOverrun(header_p, demo_p, size, 2, failonerror))
  2336. return NULL;
  2337. compatibility_level = doom_12_compatibility;
  2338. episode = *demo_p++;
  2339. map = *demo_p++;
  2340. deathmatch = respawnparm = fastparm =
  2341. nomonsters = consoleplayer = 0;
  2342. }
  2343. G_Compatibility();
  2344. }
  2345. else // new versions of demos
  2346. {
  2347. demo_p += 6; // skip signature;
  2348. switch (demover) {
  2349. case 200: /* BOOM */
  2350. case 201:
  2351. //e6y: check for overrun
  2352. if (CheckForOverrun(header_p, demo_p, size, 1, failonerror))
  2353. return NULL;
  2354. if (!*demo_p++)
  2355. compatibility_level = boom_201_compatibility;
  2356. else
  2357. compatibility_level = boom_compatibility_compatibility;
  2358. break;
  2359. case 202:
  2360. //e6y: check for overrun
  2361. if (CheckForOverrun(header_p, demo_p, size, 1, failonerror))
  2362. return NULL;
  2363. if (!*demo_p++)
  2364. compatibility_level = boom_202_compatibility;
  2365. else
  2366. compatibility_level = boom_compatibility_compatibility;
  2367. break;
  2368. case 203:
  2369. /* LxDoom or MBF - determine from signature
  2370. * cph - load compatibility level */
  2371. switch (*(header_p + 2)) {
  2372. case 'B': /* LxDoom */
  2373. /* cph - DEMOSYNC - LxDoom demos recorded in compatibility modes support dropped */
  2374. compatibility_level = lxdoom_1_compatibility;
  2375. break;
  2376. case 'M':
  2377. compatibility_level = mbf_compatibility;
  2378. demo_p++;
  2379. break;
  2380. }
  2381. break;
  2382. case 210:
  2383. compatibility_level = prboom_2_compatibility;
  2384. demo_p++;
  2385. break;
  2386. case 211:
  2387. compatibility_level = prboom_3_compatibility;
  2388. demo_p++;
  2389. break;
  2390. case 212:
  2391. compatibility_level = prboom_4_compatibility;
  2392. demo_p++;
  2393. break;
  2394. case 213:
  2395. compatibility_level = prboom_5_compatibility;
  2396. demo_p++;
  2397. break;
  2398. case 214:
  2399. compatibility_level = prboom_6_compatibility;
  2400. longtics = 1;
  2401. demo_p++;
  2402. break;
  2403. }
  2404. //e6y: check for overrun
  2405. if (CheckForOverrun(header_p, demo_p, size, 5, failonerror))
  2406. return NULL;
  2407. skill = *demo_p++;
  2408. episode = *demo_p++;
  2409. map = *demo_p++;
  2410. deathmatch = *demo_p++;
  2411. consoleplayer = *demo_p++;
  2412. /* killough 11/98: save option pointer for below */
  2413. if (mbf_features)
  2414. option_p = demo_p;
  2415. //e6y: check for overrun
  2416. if (CheckForOverrun(header_p, demo_p, size, GAME_OPTION_SIZE, failonerror))
  2417. return NULL;
  2418. demo_p = G_ReadOptions(demo_p); // killough 3/1/98: Read game options
  2419. if (demover == 200) // killough 6/3/98: partially fix v2.00 demos
  2420. demo_p += 256-GAME_OPTION_SIZE;
  2421. }
  2422. if (sizeof(comp_lev_str)/sizeof(comp_lev_str[0]) != MAX_COMPATIBILITY_LEVEL)
  2423. I_Error("G_ReadDemoHeader: compatibility level strings incomplete");
  2424. lprintf(LO_INFO, "G_DoPlayDemo: playing demo with %s compatibility\n",
  2425. comp_lev_str[compatibility_level]);
  2426. if (demo_compatibility) // only 4 players can exist in old demos
  2427. {
  2428. //e6y: check for overrun
  2429. if (CheckForOverrun(header_p, demo_p, size, 4, failonerror))
  2430. return NULL;
  2431. for (i=0; i<4; i++) // intentionally hard-coded 4 -- killough
  2432. playeringame[i] = *demo_p++;
  2433. for (;i < MAXPLAYERS; i++)
  2434. playeringame[i] = 0;
  2435. }
  2436. else
  2437. {
  2438. //e6y: check for overrun
  2439. if (CheckForOverrun(header_p, demo_p, size, MAXPLAYERS, failonerror))
  2440. return NULL;
  2441. for (i=0 ; i < MAXPLAYERS; i++)
  2442. playeringame[i] = *demo_p++;
  2443. demo_p += MIN_MAXPLAYERS - MAXPLAYERS;
  2444. }
  2445. if (playeringame[1])
  2446. {
  2447. netgame = true;
  2448. netdemo = true;
  2449. }
  2450. if (gameaction != ga_loadgame) { /* killough 12/98: support -loadgame */
  2451. G_InitNew(skill, episode, map);
  2452. }
  2453. for (i=0; i<MAXPLAYERS;i++) // killough 4/24/98
  2454. players[i].cheats = 0;
  2455. return demo_p;
  2456. }
  2457. void G_DoPlayDemo(void)
  2458. {
  2459. char basename[9];
  2460. ExtractFileBase(defdemoname,basename); // killough
  2461. basename[8] = 0;
  2462. /* cph - store lump number for unlocking later */
  2463. demolumpnum = W_GetNumForName(basename);
  2464. demobuffer = W_CacheLumpNum(demolumpnum);
  2465. demolength = W_LumpLength(demolumpnum);
  2466. demo_p = G_ReadDemoHeader(demobuffer, demolength, true);
  2467. gameaction = ga_nothing;
  2468. usergame = false;
  2469. demoplayback = true;
  2470. R_SmoothPlaying_Reset(NULL); // e6y
  2471. starttime = I_GetTime_RealTime ();
  2472. }
  2473. /* G_CheckDemoStatus
  2474. *
  2475. * Called after a death or level completion to allow demos to be cleaned up
  2476. * Returns true if a new demo loop action will take place
  2477. */
  2478. boolean G_CheckDemoStatus (void)
  2479. {
  2480. P_ChecksumFinal();
  2481. if (demorecording)
  2482. {
  2483. demorecording = false;
  2484. fputc(DEMOMARKER, demofp);
  2485. I_Error("G_CheckDemoStatus: Demo recorded");
  2486. return false; // killough
  2487. }
  2488. if (timingdemo)
  2489. {
  2490. int endtime = I_GetTime_RealTime ();
  2491. // killough -- added fps information and made it work for longer demos:
  2492. unsigned realtics = endtime-starttime;
  2493. I_Error ("Timed %u gametics in %u realtics = %-.1f frames per second",
  2494. (unsigned) gametic,realtics,
  2495. (unsigned) gametic * (double) TICRATE / realtics);
  2496. }
  2497. if (demoplayback)
  2498. {
  2499. if (singledemo)
  2500. exit(0); // killough
  2501. if (demolumpnum != -1) {
  2502. // cph - unlock the demo lump
  2503. W_UnlockLumpNum(demolumpnum);
  2504. demolumpnum = -1;
  2505. }
  2506. G_ReloadDefaults(); // killough 3/1/98
  2507. netgame = false; // killough 3/29/98
  2508. deathmatch = false;
  2509. D_AdvanceDemo ();
  2510. return true;
  2511. }
  2512. return false;
  2513. }
  2514. // killough 1/22/98: this is a "Doom printf" for messages. I've gotten
  2515. // tired of using players->message=... and so I've added this dprintf.
  2516. //
  2517. // killough 3/6/98: Made limit static to allow z_zone functions to call
  2518. // this function, without calling realloc(), which seems to cause problems.
  2519. #define MAX_MESSAGE_SIZE 1024
  2520. // CPhipps - renamed to doom_printf to avoid name collision with glibc
  2521. void doom_printf(const char *s, ...)
  2522. {
  2523. static char msg[MAX_MESSAGE_SIZE];
  2524. va_list v;
  2525. va_start(v,s);
  2526. #ifdef HAVE_VSNPRINTF
  2527. vsnprintf(msg,sizeof(msg),s,v); /* print message in buffer */
  2528. #else
  2529. vsprintf(msg,s,v);
  2530. #endif
  2531. va_end(v);
  2532. players[consoleplayer].message = msg; // set new message
  2533. }