G_GAME.C 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953
  1. // G_game.c
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include "DoomDef.h"
  5. #include "P_local.h"
  6. #include "soundst.h"
  7. // Macros
  8. #define SVG_RAM 0
  9. #define SVG_FILE 1
  10. #define SAVE_GAME_TERMINATOR 0x1d
  11. #define AM_STARTKEY 9
  12. // Functions
  13. boolean G_CheckDemoStatus (void);
  14. void G_ReadDemoTiccmd (ticcmd_t *cmd);
  15. void G_WriteDemoTiccmd (ticcmd_t *cmd);
  16. void G_PlayerReborn (int player);
  17. void G_InitNew (skill_t skill, int episode, int map);
  18. void G_DoReborn (int playernum);
  19. void G_DoLoadLevel (void);
  20. void G_DoNewGame (void);
  21. void G_DoLoadGame (void);
  22. void G_DoPlayDemo (void);
  23. void G_DoCompleted (void);
  24. void G_DoVictory (void);
  25. void G_DoWorldDone (void);
  26. void G_DoSaveGame (void);
  27. void D_PageTicker(void);
  28. void D_AdvanceDemo(void);
  29. struct
  30. {
  31. mobjtype_t type;
  32. int speed[2];
  33. } MonsterMissileInfo[] =
  34. {
  35. { MT_IMPBALL, 10, 20 },
  36. { MT_MUMMYFX1, 9, 18 },
  37. { MT_KNIGHTAXE, 9, 18 },
  38. { MT_REDAXE, 9, 18 },
  39. { MT_BEASTBALL, 12, 20 },
  40. { MT_WIZFX1, 18, 24 },
  41. { MT_SNAKEPRO_A, 14, 20 },
  42. { MT_SNAKEPRO_B, 14, 20 },
  43. { MT_HEADFX1, 13, 20 },
  44. { MT_HEADFX3, 10, 18 },
  45. { MT_MNTRFX1, 20, 26 },
  46. { MT_MNTRFX2, 14, 20 },
  47. { MT_SRCRFX1, 20, 28 },
  48. { MT_SOR2FX1, 20, 28 },
  49. { -1, -1, -1 } // Terminator
  50. };
  51. FILE *SaveGameFP;
  52. int SaveGameType;
  53. gameaction_t gameaction;
  54. gamestate_t gamestate;
  55. skill_t gameskill;
  56. boolean respawnmonsters;
  57. int gameepisode;
  58. int gamemap;
  59. int prevmap;
  60. boolean paused;
  61. boolean sendpause; // send a pause event next tic
  62. boolean sendsave; // send a save event next tic
  63. boolean usergame; // ok to save / end game
  64. boolean timingdemo; // if true, exit with report on completion
  65. int starttime; // for comparative timing purposes
  66. boolean viewactive;
  67. boolean deathmatch; // only if started as net death
  68. boolean netgame; // only true if packets are broadcast
  69. boolean playeringame[MAXPLAYERS];
  70. player_t players[MAXPLAYERS];
  71. int consoleplayer; // player taking events and displaying
  72. int displayplayer; // view being displayed
  73. int gametic;
  74. int levelstarttic; // gametic at level start
  75. int totalkills, totalitems, totalsecret; // for intermission
  76. char demoname[32];
  77. boolean demorecording;
  78. boolean demoplayback;
  79. byte *demobuffer, *demo_p;
  80. boolean singledemo; // quit after playing a demo from cmdline
  81. boolean precache = true; // if true, load all graphics at start
  82. short consistancy[MAXPLAYERS][BACKUPTICS];
  83. byte *savebuffer, *save_p;
  84. //
  85. // controls (have defaults)
  86. //
  87. int key_right, key_left, key_up, key_down;
  88. int key_strafeleft, key_straferight;
  89. int key_fire, key_use, key_strafe, key_speed;
  90. int key_flyup, key_flydown, key_flycenter;
  91. int key_lookup, key_lookdown, key_lookcenter;
  92. int key_invleft, key_invright, key_useartifact;
  93. int mousebfire;
  94. int mousebstrafe;
  95. int mousebforward;
  96. int joybfire;
  97. int joybstrafe;
  98. int joybuse;
  99. int joybspeed;
  100. #define MAXPLMOVE 0x32
  101. fixed_t forwardmove[2] = {0x19, 0x32};
  102. fixed_t sidemove[2] = {0x18, 0x28};
  103. fixed_t angleturn[3] = {640, 1280, 320}; // + slow turn
  104. #define SLOWTURNTICS 6
  105. #define NUMKEYS 256
  106. boolean gamekeydown[NUMKEYS];
  107. int turnheld; // for accelerative turning
  108. int lookheld;
  109. boolean mousearray[4];
  110. boolean *mousebuttons = &mousearray[1];
  111. // allow [-1]
  112. int mousex, mousey; // mouse values are used once
  113. int dclicktime, dclickstate, dclicks;
  114. int dclicktime2, dclickstate2, dclicks2;
  115. int joyxmove, joyymove; // joystick values are repeated
  116. boolean joyarray[5];
  117. boolean *joybuttons = &joyarray[1]; // allow [-1]
  118. int savegameslot;
  119. char savedescription[32];
  120. int inventoryTics;
  121. #ifdef __WATCOMC__
  122. extern externdata_t *i_ExternData;
  123. #endif
  124. //=============================================================================
  125. // Not used - ripped out for Heretic
  126. /*
  127. int G_CmdChecksum(ticcmd_t *cmd)
  128. {
  129. int i;
  130. int sum;
  131. sum = 0;
  132. for(i = 0; i < sizeof(*cmd)/4-1; i++)
  133. {
  134. sum += ((int *)cmd)[i];
  135. }
  136. return(sum);
  137. }
  138. */
  139. /*
  140. ====================
  141. =
  142. = G_BuildTiccmd
  143. =
  144. = Builds a ticcmd from all of the available inputs or reads it from the
  145. = demo buffer.
  146. = If recording a demo, write it out
  147. ====================
  148. */
  149. extern boolean inventory;
  150. extern int curpos;
  151. extern int inv_ptr;
  152. extern int isCyberPresent; // is CyberMan present?
  153. boolean usearti = true;
  154. void I_ReadCyberCmd (ticcmd_t *cmd);
  155. void G_BuildTiccmd (ticcmd_t *cmd)
  156. {
  157. int i;
  158. boolean strafe, bstrafe;
  159. int speed, tspeed, lspeed;
  160. int forward, side;
  161. int look, arti;
  162. int flyheight;
  163. extern boolean noartiskip;
  164. #ifdef __WATCOMC__
  165. int angleDelta;
  166. static int oldAngle;
  167. extern int newViewAngleOff;
  168. static int externInvKey;
  169. extern boolean automapactive;
  170. event_t ev;
  171. #endif
  172. memset (cmd,0,sizeof(*cmd));
  173. //cmd->consistancy =
  174. // consistancy[consoleplayer][(maketic*ticdup)%BACKUPTICS];
  175. cmd->consistancy =
  176. consistancy[consoleplayer][maketic%BACKUPTICS];
  177. if (isCyberPresent)
  178. I_ReadCyberCmd (cmd);
  179. //printf ("cons: %i\n",cmd->consistancy);
  180. strafe = gamekeydown[key_strafe] || mousebuttons[mousebstrafe]
  181. || joybuttons[joybstrafe];
  182. speed = gamekeydown[key_speed] || joybuttons[joybspeed]
  183. || joybuttons[joybspeed];
  184. #ifdef __WATCOMC__
  185. if(useexterndriver)
  186. {
  187. speed |= (i_ExternData->buttons&EBT_SPEED);
  188. strafe |= (i_ExternData->buttons&EBT_STRAFE);
  189. }
  190. #endif
  191. forward = side = look = arti = flyheight = 0;
  192. //
  193. // use two stage accelerative turning on the keyboard and joystick
  194. //
  195. if (joyxmove < 0 || joyxmove > 0
  196. || gamekeydown[key_right] || gamekeydown[key_left])
  197. turnheld += ticdup;
  198. else
  199. turnheld = 0;
  200. if (turnheld < SLOWTURNTICS)
  201. tspeed = 2; // slow turn
  202. else
  203. tspeed = speed;
  204. if(gamekeydown[key_lookdown] || gamekeydown[key_lookup])
  205. {
  206. lookheld += ticdup;
  207. }
  208. else
  209. {
  210. lookheld = 0;
  211. }
  212. if(lookheld < SLOWTURNTICS)
  213. {
  214. lspeed = 1;
  215. }
  216. else
  217. {
  218. lspeed = 2;
  219. }
  220. //
  221. // let movement keys cancel each other out
  222. //
  223. if(strafe)
  224. {
  225. if (gamekeydown[key_right])
  226. side += sidemove[speed];
  227. if (gamekeydown[key_left])
  228. side -= sidemove[speed];
  229. if (joyxmove > 0)
  230. side += sidemove[speed];
  231. if (joyxmove < 0)
  232. side -= sidemove[speed];
  233. }
  234. else
  235. {
  236. if (gamekeydown[key_right])
  237. cmd->angleturn -= angleturn[tspeed];
  238. if (gamekeydown[key_left])
  239. cmd->angleturn += angleturn[tspeed];
  240. if (joyxmove > 0)
  241. cmd->angleturn -= angleturn[tspeed];
  242. if (joyxmove < 0)
  243. cmd->angleturn += angleturn[tspeed];
  244. }
  245. if (gamekeydown[key_up])
  246. forward += forwardmove[speed];
  247. if (gamekeydown[key_down])
  248. forward -= forwardmove[speed];
  249. if (joyymove < 0)
  250. forward += forwardmove[speed];
  251. if (joyymove > 0)
  252. forward -= forwardmove[speed];
  253. if (gamekeydown[key_straferight])
  254. side += sidemove[speed];
  255. if (gamekeydown[key_strafeleft])
  256. side -= sidemove[speed];
  257. // Look up/down/center keys
  258. if(gamekeydown[key_lookup])
  259. {
  260. look = lspeed;
  261. }
  262. if(gamekeydown[key_lookdown])
  263. {
  264. look = -lspeed;
  265. }
  266. #ifdef __WATCOMC__
  267. if(gamekeydown[key_lookcenter] && !useexterndriver)
  268. {
  269. look = TOCENTER;
  270. }
  271. #else
  272. if(gamekeydown[key_lookcenter])
  273. {
  274. look = TOCENTER;
  275. }
  276. #endif
  277. #ifdef __WATCOMC__
  278. if(useexterndriver && look != TOCENTER && (gamestate == GS_LEVEL ||
  279. gamestate == GS_INTERMISSION))
  280. {
  281. if(i_ExternData->moveForward)
  282. {
  283. forward += i_ExternData->moveForward;
  284. if(speed)
  285. {
  286. forward <<= 1;
  287. }
  288. }
  289. if(i_ExternData->angleTurn)
  290. {
  291. if(strafe)
  292. {
  293. side += i_ExternData->angleTurn;
  294. }
  295. else
  296. {
  297. cmd->angleturn += i_ExternData->angleTurn;
  298. }
  299. }
  300. if(i_ExternData->moveSideways)
  301. {
  302. side += i_ExternData->moveSideways;
  303. if(speed)
  304. {
  305. side <<= 1;
  306. }
  307. }
  308. if(i_ExternData->buttons&EBT_CENTERVIEW)
  309. {
  310. look = TOCENTER;
  311. oldAngle = 0;
  312. }
  313. else if(i_ExternData->pitch)
  314. {
  315. angleDelta = i_ExternData->pitch-oldAngle;
  316. if(abs(angleDelta) < 35)
  317. {
  318. look = angleDelta/5;
  319. }
  320. else
  321. {
  322. look = 7*(angleDelta > 0 ? 1 : -1);
  323. }
  324. if(look == TOCENTER)
  325. {
  326. look++;
  327. }
  328. oldAngle += look*5;
  329. }
  330. if(i_ExternData->flyDirection)
  331. {
  332. if(i_ExternData->flyDirection > 0)
  333. {
  334. flyheight = 5;
  335. }
  336. else
  337. {
  338. flyheight = -5;
  339. }
  340. }
  341. if(abs(newViewAngleOff-i_ExternData->angleHead) < 3000)
  342. {
  343. newViewAngleOff = i_ExternData->angleHead;
  344. }
  345. if(i_ExternData->buttons&EBT_FIRE)
  346. {
  347. cmd->buttons |= BT_ATTACK;
  348. }
  349. if(i_ExternData->buttons&EBT_OPENDOOR)
  350. {
  351. cmd->buttons |= BT_USE;
  352. }
  353. if(i_ExternData->buttons&EBT_PAUSE)
  354. {
  355. cmd->buttons = BT_SPECIAL | BTS_PAUSE;
  356. i_ExternData->buttons &= ~EBT_PAUSE;
  357. }
  358. if(externInvKey&EBT_USEARTIFACT)
  359. {
  360. ev.type = ev_keyup;
  361. ev.data1 = key_useartifact;
  362. D_PostEvent(&ev);
  363. externInvKey &= ~EBT_USEARTIFACT;
  364. }
  365. else if(i_ExternData->buttons&EBT_USEARTIFACT)
  366. {
  367. externInvKey |= EBT_USEARTIFACT;
  368. ev.type = ev_keydown;
  369. ev.data1 = key_useartifact;
  370. D_PostEvent(&ev);
  371. }
  372. if(externInvKey&EBT_INVENTORYRIGHT)
  373. {
  374. ev.type = ev_keyup;
  375. ev.data1 = key_invright;
  376. D_PostEvent(&ev);
  377. externInvKey &= ~EBT_INVENTORYRIGHT;
  378. }
  379. else if(i_ExternData->buttons&EBT_INVENTORYRIGHT)
  380. {
  381. externInvKey |= EBT_INVENTORYRIGHT;
  382. ev.type = ev_keydown;
  383. ev.data1 = key_invright;
  384. D_PostEvent(&ev);
  385. }
  386. if(externInvKey&EBT_INVENTORYLEFT)
  387. {
  388. ev.type = ev_keyup;
  389. ev.data1 = key_invleft;
  390. D_PostEvent(&ev);
  391. externInvKey &= ~EBT_INVENTORYLEFT;
  392. }
  393. else if(i_ExternData->buttons&EBT_INVENTORYLEFT)
  394. {
  395. externInvKey |= EBT_INVENTORYLEFT;
  396. ev.type = ev_keydown;
  397. ev.data1 = key_invleft;
  398. D_PostEvent(&ev);
  399. }
  400. if(i_ExternData->buttons&EBT_FLYDROP)
  401. {
  402. flyheight = TOCENTER;
  403. }
  404. if(gamestate == GS_LEVEL)
  405. {
  406. if(externInvKey&EBT_MAP)
  407. { // AutoMap
  408. ev.type = ev_keyup;
  409. ev.data1 = AM_STARTKEY;
  410. D_PostEvent(&ev);
  411. externInvKey &= ~EBT_MAP;
  412. }
  413. else if(i_ExternData->buttons&EBT_MAP)
  414. {
  415. externInvKey |= EBT_MAP;
  416. ev.type = ev_keydown;
  417. ev.data1 = AM_STARTKEY;
  418. D_PostEvent(&ev);
  419. }
  420. }
  421. #if 0
  422. if((i = (i_ExternData->buttons>>EBT_WEAPONSHIFT)&EBT_WEAPONMASK) != 0)
  423. {
  424. cmd->buttons |= BT_CHANGE;
  425. cmd->buttons |= (i-1)<<BT_WEAPONSHIFT;
  426. }
  427. #endif
  428. if(i_ExternData->buttons&EBT_WEAPONCYCLE)
  429. {
  430. int curWeapon;
  431. player_t *pl;
  432. pl = &players[consoleplayer];
  433. curWeapon = pl->readyweapon;
  434. for(curWeapon = (curWeapon+1)&7; curWeapon != pl->readyweapon;
  435. curWeapon = (curWeapon+1)&7)
  436. {
  437. if(pl->weaponowned[curWeapon])
  438. {
  439. if(curWeapon >= wp_goldwand && curWeapon <= wp_mace &&
  440. !pl->ammo[wpnlev1info[curWeapon].ammo])
  441. { // weapon that requires ammo is empty
  442. continue;
  443. }
  444. break;
  445. }
  446. }
  447. cmd->buttons |= BT_CHANGE;
  448. cmd->buttons |= curWeapon<<BT_WEAPONSHIFT;
  449. }
  450. }
  451. #endif
  452. // Fly up/down/drop keys
  453. if(gamekeydown[key_flyup])
  454. {
  455. flyheight = 5; // note that the actual flyheight will be twice this
  456. }
  457. if(gamekeydown[key_flydown])
  458. {
  459. flyheight = -5;
  460. }
  461. if(gamekeydown[key_flycenter])
  462. {
  463. flyheight = TOCENTER;
  464. #ifdef __WATCOMC__
  465. if(!useexterndriver)
  466. {
  467. look = TOCENTER;
  468. }
  469. #else
  470. look = TOCENTER;
  471. #endif
  472. }
  473. // Use artifact key
  474. if(gamekeydown[key_useartifact])
  475. {
  476. if(gamekeydown[key_speed] && !noartiskip)
  477. {
  478. if(players[consoleplayer].inventory[inv_ptr].type != arti_none)
  479. {
  480. gamekeydown[key_useartifact] = false;
  481. cmd->arti = 0xff; // skip artifact code
  482. }
  483. }
  484. else
  485. {
  486. if(inventory)
  487. {
  488. players[consoleplayer].readyArtifact =
  489. players[consoleplayer].inventory[inv_ptr].type;
  490. inventory = false;
  491. cmd->arti = 0;
  492. usearti = false;
  493. }
  494. else if(usearti)
  495. {
  496. cmd->arti = players[consoleplayer].inventory[inv_ptr].type;
  497. usearti = false;
  498. }
  499. }
  500. }
  501. if(gamekeydown[127] && !cmd->arti
  502. && !players[consoleplayer].powers[pw_weaponlevel2])
  503. {
  504. gamekeydown[127] = false;
  505. cmd->arti = arti_tomeofpower;
  506. }
  507. //
  508. // buttons
  509. //
  510. cmd->chatchar = CT_dequeueChatChar();
  511. if (gamekeydown[key_fire] || mousebuttons[mousebfire]
  512. || joybuttons[joybfire])
  513. cmd->buttons |= BT_ATTACK;
  514. if (gamekeydown[key_use] || joybuttons[joybuse] )
  515. {
  516. cmd->buttons |= BT_USE;
  517. dclicks = 0; // clear double clicks if hit use button
  518. }
  519. for(i = 0; i < NUMWEAPONS-2; i++)
  520. {
  521. if(gamekeydown['1'+i])
  522. {
  523. cmd->buttons |= BT_CHANGE;
  524. cmd->buttons |= i<<BT_WEAPONSHIFT;
  525. break;
  526. }
  527. }
  528. //
  529. // mouse
  530. //
  531. if (mousebuttons[mousebforward])
  532. {
  533. forward += forwardmove[speed];
  534. }
  535. //
  536. // forward double click
  537. //
  538. if (mousebuttons[mousebforward] != dclickstate && dclicktime > 1 )
  539. {
  540. dclickstate = mousebuttons[mousebforward];
  541. if (dclickstate)
  542. dclicks++;
  543. if (dclicks == 2)
  544. {
  545. cmd->buttons |= BT_USE;
  546. dclicks = 0;
  547. }
  548. else
  549. dclicktime = 0;
  550. }
  551. else
  552. {
  553. dclicktime += ticdup;
  554. if (dclicktime > 20)
  555. {
  556. dclicks = 0;
  557. dclickstate = 0;
  558. }
  559. }
  560. //
  561. // strafe double click
  562. //
  563. bstrafe = mousebuttons[mousebstrafe]
  564. || joybuttons[joybstrafe];
  565. if (bstrafe != dclickstate2 && dclicktime2 > 1 )
  566. {
  567. dclickstate2 = bstrafe;
  568. if (dclickstate2)
  569. dclicks2++;
  570. if (dclicks2 == 2)
  571. {
  572. cmd->buttons |= BT_USE;
  573. dclicks2 = 0;
  574. }
  575. else
  576. dclicktime2 = 0;
  577. }
  578. else
  579. {
  580. dclicktime2 += ticdup;
  581. if (dclicktime2 > 20)
  582. {
  583. dclicks2 = 0;
  584. dclickstate2 = 0;
  585. }
  586. }
  587. if (strafe)
  588. {
  589. side += mousex*2;
  590. }
  591. else
  592. {
  593. cmd->angleturn -= mousex*0x8;
  594. }
  595. forward += mousey;
  596. mousex = mousey = 0;
  597. if (forward > MAXPLMOVE)
  598. forward = MAXPLMOVE;
  599. else if (forward < -MAXPLMOVE)
  600. forward = -MAXPLMOVE;
  601. if (side > MAXPLMOVE)
  602. side = MAXPLMOVE;
  603. else if (side < -MAXPLMOVE)
  604. side = -MAXPLMOVE;
  605. cmd->forwardmove += forward;
  606. cmd->sidemove += side;
  607. if(players[consoleplayer].playerstate == PST_LIVE)
  608. {
  609. if(look < 0)
  610. {
  611. look += 16;
  612. }
  613. cmd->lookfly = look;
  614. }
  615. if(flyheight < 0)
  616. {
  617. flyheight += 16;
  618. }
  619. cmd->lookfly |= flyheight<<4;
  620. //
  621. // special buttons
  622. //
  623. if (sendpause)
  624. {
  625. sendpause = false;
  626. cmd->buttons = BT_SPECIAL | BTS_PAUSE;
  627. }
  628. if (sendsave)
  629. {
  630. sendsave = false;
  631. cmd->buttons = BT_SPECIAL | BTS_SAVEGAME | (savegameslot<<BTS_SAVESHIFT);
  632. }
  633. }
  634. /*
  635. ==============
  636. =
  637. = G_DoLoadLevel
  638. =
  639. ==============
  640. */
  641. void G_DoLoadLevel (void)
  642. {
  643. int i;
  644. levelstarttic = gametic; // for time calculation
  645. gamestate = GS_LEVEL;
  646. for (i=0 ; i<MAXPLAYERS ; i++)
  647. {
  648. if (playeringame[i] && players[i].playerstate == PST_DEAD)
  649. players[i].playerstate = PST_REBORN;
  650. memset (players[i].frags,0,sizeof(players[i].frags));
  651. }
  652. P_SetupLevel (gameepisode, gamemap, 0, gameskill);
  653. displayplayer = consoleplayer; // view the guy you are playing
  654. starttime = I_GetTime ();
  655. gameaction = ga_nothing;
  656. Z_CheckHeap ();
  657. //
  658. // clear cmd building stuff
  659. //
  660. memset (gamekeydown, 0, sizeof(gamekeydown));
  661. joyxmove = joyymove = 0;
  662. mousex = mousey = 0;
  663. sendpause = sendsave = paused = false;
  664. memset (mousebuttons, 0, sizeof(mousebuttons));
  665. memset (joybuttons, 0, sizeof(joybuttons));
  666. }
  667. /*
  668. ===============================================================================
  669. =
  670. = G_Responder
  671. =
  672. = get info needed to make ticcmd_ts for the players
  673. =
  674. ===============================================================================
  675. */
  676. boolean G_Responder(event_t *ev)
  677. {
  678. player_t *plr;
  679. extern boolean MenuActive;
  680. plr = &players[consoleplayer];
  681. if(ev->type == ev_keyup && ev->data1 == key_useartifact)
  682. { // flag to denote that it's okay to use an artifact
  683. if(!inventory)
  684. {
  685. plr->readyArtifact = plr->inventory[inv_ptr].type;
  686. }
  687. usearti = true;
  688. }
  689. // Check for spy mode player cycle
  690. if(gamestate == GS_LEVEL && ev->type == ev_keydown
  691. && ev->data1 == KEY_F12 && !deathmatch)
  692. { // Cycle the display player
  693. do
  694. {
  695. displayplayer++;
  696. if(displayplayer == MAXPLAYERS)
  697. {
  698. displayplayer = 0;
  699. }
  700. } while(!playeringame[displayplayer]
  701. && displayplayer != consoleplayer);
  702. return(true);
  703. }
  704. if(gamestate == GS_LEVEL)
  705. {
  706. if(CT_Responder(ev))
  707. { // Chat ate the event
  708. return(true);
  709. }
  710. if(SB_Responder(ev))
  711. { // Status bar ate the event
  712. return(true);
  713. }
  714. if(AM_Responder(ev))
  715. { // Automap ate the event
  716. return(true);
  717. }
  718. }
  719. switch(ev->type)
  720. {
  721. case ev_keydown:
  722. if(ev->data1 == key_invleft)
  723. {
  724. inventoryTics = 5*35;
  725. if(!inventory)
  726. {
  727. inventory = true;
  728. break;
  729. }
  730. inv_ptr--;
  731. if(inv_ptr < 0)
  732. {
  733. inv_ptr = 0;
  734. }
  735. else
  736. {
  737. curpos--;
  738. if(curpos < 0)
  739. {
  740. curpos = 0;
  741. }
  742. }
  743. return(true);
  744. }
  745. if(ev->data1 == key_invright)
  746. {
  747. inventoryTics = 5*35;
  748. if(!inventory)
  749. {
  750. inventory = true;
  751. break;
  752. }
  753. inv_ptr++;
  754. if(inv_ptr >= plr->inventorySlotNum)
  755. {
  756. inv_ptr--;
  757. if(inv_ptr < 0)
  758. inv_ptr = 0;
  759. }
  760. else
  761. {
  762. curpos++;
  763. if(curpos > 6)
  764. {
  765. curpos = 6;
  766. }
  767. }
  768. return(true);
  769. }
  770. if(ev->data1 == KEY_PAUSE && !MenuActive)
  771. {
  772. sendpause = true;
  773. return(true);
  774. }
  775. if(ev->data1 < NUMKEYS)
  776. {
  777. gamekeydown[ev->data1] = true;
  778. }
  779. return(true); // eat key down events
  780. case ev_keyup:
  781. if(ev->data1 < NUMKEYS)
  782. {
  783. gamekeydown[ev->data1] = false;
  784. }
  785. return(false); // always let key up events filter down
  786. case ev_mouse:
  787. mousebuttons[0] = ev->data1&1;
  788. mousebuttons[1] = ev->data1&2;
  789. mousebuttons[2] = ev->data1&4;
  790. mousex = ev->data2*(mouseSensitivity+5)/10;
  791. mousey = ev->data3*(mouseSensitivity+5)/10;
  792. return(true); // eat events
  793. case ev_joystick:
  794. joybuttons[0] = ev->data1&1;
  795. joybuttons[1] = ev->data1&2;
  796. joybuttons[2] = ev->data1&4;
  797. joybuttons[3] = ev->data1&8;
  798. joyxmove = ev->data2;
  799. joyymove = ev->data3;
  800. return(true); // eat events
  801. default:
  802. break;
  803. }
  804. return(false);
  805. }
  806. /*
  807. ===============================================================================
  808. =
  809. = G_Ticker
  810. =
  811. ===============================================================================
  812. */
  813. void G_Ticker (void)
  814. {
  815. int i, buf;
  816. ticcmd_t *cmd;
  817. //
  818. // do player reborns if needed
  819. //
  820. for (i=0 ; i<MAXPLAYERS ; i++)
  821. if (playeringame[i] && players[i].playerstate == PST_REBORN)
  822. G_DoReborn (i);
  823. //
  824. // do things to change the game state
  825. //
  826. while (gameaction != ga_nothing)
  827. {
  828. switch (gameaction)
  829. {
  830. case ga_loadlevel:
  831. G_DoLoadLevel ();
  832. break;
  833. case ga_newgame:
  834. G_DoNewGame ();
  835. break;
  836. case ga_loadgame:
  837. G_DoLoadGame ();
  838. break;
  839. case ga_savegame:
  840. G_DoSaveGame ();
  841. break;
  842. case ga_playdemo:
  843. G_DoPlayDemo ();
  844. break;
  845. case ga_screenshot:
  846. M_ScreenShot ();
  847. gameaction = ga_nothing;
  848. break;
  849. case ga_completed:
  850. G_DoCompleted ();
  851. break;
  852. case ga_worlddone:
  853. G_DoWorldDone();
  854. break;
  855. case ga_victory:
  856. F_StartFinale();
  857. break;
  858. default:
  859. break;
  860. }
  861. }
  862. //
  863. // get commands, check consistancy, and build new consistancy check
  864. //
  865. //buf = gametic%BACKUPTICS;
  866. buf = (gametic/ticdup)%BACKUPTICS;
  867. for (i=0 ; i<MAXPLAYERS ; i++)
  868. if (playeringame[i])
  869. {
  870. cmd = &players[i].cmd;
  871. memcpy (cmd, &netcmds[i][buf], sizeof(ticcmd_t));
  872. if (demoplayback)
  873. G_ReadDemoTiccmd (cmd);
  874. if (demorecording)
  875. G_WriteDemoTiccmd (cmd);
  876. if (netgame && !(gametic%ticdup) )
  877. {
  878. if (gametic > BACKUPTICS
  879. && consistancy[i][buf] != cmd->consistancy)
  880. {
  881. I_Error ("consistency failure (%i should be %i)",cmd->consistancy, consistancy[i][buf]);
  882. }
  883. if (players[i].mo)
  884. consistancy[i][buf] = players[i].mo->x;
  885. else
  886. consistancy[i][buf] = rndindex;
  887. }
  888. }
  889. //
  890. // check for special buttons
  891. //
  892. for (i=0 ; i<MAXPLAYERS ; i++)
  893. if (playeringame[i])
  894. {
  895. if (players[i].cmd.buttons & BT_SPECIAL)
  896. {
  897. switch (players[i].cmd.buttons & BT_SPECIALMASK)
  898. {
  899. case BTS_PAUSE:
  900. paused ^= 1;
  901. if(paused)
  902. {
  903. S_PauseSound();
  904. }
  905. else
  906. {
  907. S_ResumeSound();
  908. }
  909. break;
  910. case BTS_SAVEGAME:
  911. if (!savedescription[0])
  912. {
  913. if(netgame)
  914. {
  915. strcpy (savedescription, "NET GAME");
  916. }
  917. else
  918. {
  919. strcpy(savedescription, "SAVE GAME");
  920. }
  921. }
  922. savegameslot =
  923. (players[i].cmd.buttons & BTS_SAVEMASK)>>BTS_SAVESHIFT;
  924. gameaction = ga_savegame;
  925. break;
  926. }
  927. }
  928. }
  929. // turn inventory off after a certain amount of time
  930. if(inventory && !(--inventoryTics))
  931. {
  932. players[consoleplayer].readyArtifact =
  933. players[consoleplayer].inventory[inv_ptr].type;
  934. inventory = false;
  935. cmd->arti = 0;
  936. }
  937. //
  938. // do main actions
  939. //
  940. //
  941. // do main actions
  942. //
  943. switch (gamestate)
  944. {
  945. case GS_LEVEL:
  946. P_Ticker ();
  947. SB_Ticker ();
  948. AM_Ticker ();
  949. CT_Ticker();
  950. break;
  951. case GS_INTERMISSION:
  952. IN_Ticker ();
  953. break;
  954. case GS_FINALE:
  955. F_Ticker();
  956. break;
  957. case GS_DEMOSCREEN:
  958. D_PageTicker ();
  959. break;
  960. }
  961. }
  962. /*
  963. ==============================================================================
  964. PLAYER STRUCTURE FUNCTIONS
  965. also see P_SpawnPlayer in P_Things
  966. ==============================================================================
  967. */
  968. /*
  969. ====================
  970. =
  971. = G_InitPlayer
  972. =
  973. = Called at the start
  974. = Called by the game initialization functions
  975. ====================
  976. */
  977. void G_InitPlayer (int player)
  978. {
  979. player_t *p;
  980. // set up the saved info
  981. p = &players[player];
  982. // clear everything else to defaults
  983. G_PlayerReborn (player);
  984. }
  985. /*
  986. ====================
  987. =
  988. = G_PlayerFinishLevel
  989. =
  990. = Can when a player completes a level
  991. ====================
  992. */
  993. extern int curpos;
  994. extern int inv_ptr;
  995. extern int playerkeys;
  996. void G_PlayerFinishLevel(int player)
  997. {
  998. player_t *p;
  999. int i;
  1000. /* // BIG HACK
  1001. inv_ptr = 0;
  1002. curpos = 0;
  1003. */
  1004. // END HACK
  1005. p = &players[player];
  1006. for(i=0; i<p->inventorySlotNum; i++)
  1007. {
  1008. p->inventory[i].count = 1;
  1009. }
  1010. p->artifactCount = p->inventorySlotNum;
  1011. if(!deathmatch)
  1012. {
  1013. for(i = 0; i < 16; i++)
  1014. {
  1015. P_PlayerUseArtifact(p, arti_fly);
  1016. }
  1017. }
  1018. memset(p->powers, 0, sizeof(p->powers));
  1019. memset(p->keys, 0, sizeof(p->keys));
  1020. playerkeys = 0;
  1021. // memset(p->inventory, 0, sizeof(p->inventory));
  1022. if(p->chickenTics)
  1023. {
  1024. p->readyweapon = p->mo->special1; // Restore weapon
  1025. p->chickenTics = 0;
  1026. }
  1027. p->messageTics = 0;
  1028. p->lookdir = 0;
  1029. p->mo->flags &= ~MF_SHADOW; // Remove invisibility
  1030. p->extralight = 0; // Remove weapon flashes
  1031. p->fixedcolormap = 0; // Remove torch
  1032. p->damagecount = 0; // No palette changes
  1033. p->bonuscount = 0;
  1034. p->rain1 = NULL;
  1035. p->rain2 = NULL;
  1036. if(p == &players[consoleplayer])
  1037. {
  1038. SB_state = -1; // refresh the status bar
  1039. }
  1040. }
  1041. /*
  1042. ====================
  1043. =
  1044. = G_PlayerReborn
  1045. =
  1046. = Called after a player dies
  1047. = almost everything is cleared and initialized
  1048. ====================
  1049. */
  1050. void G_PlayerReborn(int player)
  1051. {
  1052. player_t *p;
  1053. int i;
  1054. int frags[MAXPLAYERS];
  1055. int killcount, itemcount, secretcount;
  1056. boolean secret;
  1057. secret = false;
  1058. memcpy(frags, players[player].frags, sizeof(frags));
  1059. killcount = players[player].killcount;
  1060. itemcount = players[player].itemcount;
  1061. secretcount = players[player].secretcount;
  1062. p = &players[player];
  1063. if(p->didsecret)
  1064. {
  1065. secret = true;
  1066. }
  1067. memset(p, 0, sizeof(*p));
  1068. memcpy(players[player].frags, frags, sizeof(players[player].frags));
  1069. players[player].killcount = killcount;
  1070. players[player].itemcount = itemcount;
  1071. players[player].secretcount = secretcount;
  1072. p->usedown = p->attackdown = true; // don't do anything immediately
  1073. p->playerstate = PST_LIVE;
  1074. p->health = MAXHEALTH;
  1075. p->readyweapon = p->pendingweapon = wp_goldwand;
  1076. p->weaponowned[wp_staff] = true;
  1077. p->weaponowned[wp_goldwand] = true;
  1078. p->messageTics = 0;
  1079. p->lookdir = 0;
  1080. p->ammo[am_goldwand] = 50;
  1081. for(i = 0; i < NUMAMMO; i++)
  1082. {
  1083. p->maxammo[i] = maxammo[i];
  1084. }
  1085. if(gamemap == 9 || secret)
  1086. {
  1087. p->didsecret = true;
  1088. }
  1089. if(p == &players[consoleplayer])
  1090. {
  1091. SB_state = -1; // refresh the status bar
  1092. inv_ptr = 0; // reset the inventory pointer
  1093. curpos = 0;
  1094. }
  1095. }
  1096. /*
  1097. ====================
  1098. =
  1099. = G_CheckSpot
  1100. =
  1101. = Returns false if the player cannot be respawned at the given mapthing_t spot
  1102. = because something is occupying it
  1103. ====================
  1104. */
  1105. void P_SpawnPlayer (mapthing_t *mthing);
  1106. boolean G_CheckSpot (int playernum, mapthing_t *mthing)
  1107. {
  1108. fixed_t x,y;
  1109. subsector_t *ss;
  1110. unsigned an;
  1111. mobj_t *mo;
  1112. x = mthing->x << FRACBITS;
  1113. y = mthing->y << FRACBITS;
  1114. players[playernum].mo->flags2 &= ~MF2_PASSMOBJ;
  1115. if (!P_CheckPosition (players[playernum].mo, x, y) )
  1116. {
  1117. players[playernum].mo->flags2 |= MF2_PASSMOBJ;
  1118. return false;
  1119. }
  1120. players[playernum].mo->flags2 |= MF2_PASSMOBJ;
  1121. // spawn a teleport fog
  1122. ss = R_PointInSubsector (x,y);
  1123. an = ( ANG45 * (mthing->angle/45) ) >> ANGLETOFINESHIFT;
  1124. mo = P_SpawnMobj (x+20*finecosine[an], y+20*finesine[an]
  1125. , ss->sector->floorheight+TELEFOGHEIGHT
  1126. , MT_TFOG);
  1127. if (players[consoleplayer].viewz != 1)
  1128. S_StartSound (mo, sfx_telept); // don't start sound on first frame
  1129. return true;
  1130. }
  1131. /*
  1132. ====================
  1133. =
  1134. = G_DeathMatchSpawnPlayer
  1135. =
  1136. = Spawns a player at one of the random death match spots
  1137. = called at level load and each death
  1138. ====================
  1139. */
  1140. void G_DeathMatchSpawnPlayer (int playernum)
  1141. {
  1142. int i,j;
  1143. int selections;
  1144. selections = deathmatch_p - deathmatchstarts;
  1145. if (selections < 4)
  1146. I_Error ("Only %i deathmatch spots, 4 required", selections);
  1147. for (j=0 ; j<20 ; j++)
  1148. {
  1149. i = P_Random() % selections;
  1150. if (G_CheckSpot (playernum, &deathmatchstarts[i]) )
  1151. {
  1152. deathmatchstarts[i].type = playernum+1;
  1153. P_SpawnPlayer (&deathmatchstarts[i]);
  1154. return;
  1155. }
  1156. }
  1157. // no good spot, so the player will probably get stuck
  1158. P_SpawnPlayer (&playerstarts[playernum]);
  1159. }
  1160. /*
  1161. ====================
  1162. =
  1163. = G_DoReborn
  1164. =
  1165. ====================
  1166. */
  1167. void G_DoReborn (int playernum)
  1168. {
  1169. int i;
  1170. if (G_CheckDemoStatus ())
  1171. return;
  1172. if (!netgame)
  1173. gameaction = ga_loadlevel; // reload the level from scratch
  1174. else
  1175. { // respawn at the start
  1176. players[playernum].mo->player = NULL; // dissasociate the corpse
  1177. // spawn at random spot if in death match
  1178. if (deathmatch)
  1179. {
  1180. G_DeathMatchSpawnPlayer (playernum);
  1181. return;
  1182. }
  1183. if (G_CheckSpot (playernum, &playerstarts[playernum]) )
  1184. {
  1185. P_SpawnPlayer (&playerstarts[playernum]);
  1186. return;
  1187. }
  1188. // try to spawn at one of the other players spots
  1189. for (i=0 ; i<MAXPLAYERS ; i++)
  1190. if (G_CheckSpot (playernum, &playerstarts[i]) )
  1191. {
  1192. playerstarts[i].type = playernum+1; // fake as other player
  1193. P_SpawnPlayer (&playerstarts[i]);
  1194. playerstarts[i].type = i+1; // restore
  1195. return;
  1196. }
  1197. // he's going to be inside something. Too bad.
  1198. P_SpawnPlayer (&playerstarts[playernum]);
  1199. }
  1200. }
  1201. void G_ScreenShot (void)
  1202. {
  1203. gameaction = ga_screenshot;
  1204. }
  1205. /*
  1206. ====================
  1207. =
  1208. = G_DoCompleted
  1209. =
  1210. ====================
  1211. */
  1212. boolean secretexit;
  1213. void G_ExitLevel (void)
  1214. {
  1215. secretexit = false;
  1216. gameaction = ga_completed;
  1217. }
  1218. void G_SecretExitLevel (void)
  1219. {
  1220. secretexit = true;
  1221. gameaction = ga_completed;
  1222. }
  1223. void G_DoCompleted(void)
  1224. {
  1225. int i;
  1226. static int afterSecret[5] = { 7, 5, 5, 5, 4 };
  1227. gameaction = ga_nothing;
  1228. if(G_CheckDemoStatus())
  1229. {
  1230. return;
  1231. }
  1232. for(i = 0; i < MAXPLAYERS; i++)
  1233. {
  1234. if(playeringame[i])
  1235. {
  1236. G_PlayerFinishLevel(i);
  1237. }
  1238. }
  1239. prevmap = gamemap;
  1240. if(secretexit == true)
  1241. {
  1242. gamemap = 9;
  1243. }
  1244. else if(gamemap == 9)
  1245. { // Finished secret level
  1246. gamemap = afterSecret[gameepisode-1];
  1247. }
  1248. else if(gamemap == 8)
  1249. {
  1250. gameaction = ga_victory;
  1251. return;
  1252. }
  1253. else
  1254. {
  1255. gamemap++;
  1256. }
  1257. gamestate = GS_INTERMISSION;
  1258. IN_Start();
  1259. }
  1260. //============================================================================
  1261. //
  1262. // G_WorldDone
  1263. //
  1264. //============================================================================
  1265. void G_WorldDone(void)
  1266. {
  1267. gameaction = ga_worlddone;
  1268. }
  1269. //============================================================================
  1270. //
  1271. // G_DoWorldDone
  1272. //
  1273. //============================================================================
  1274. void G_DoWorldDone(void)
  1275. {
  1276. gamestate = GS_LEVEL;
  1277. G_DoLoadLevel();
  1278. gameaction = ga_nothing;
  1279. viewactive = true;
  1280. }
  1281. //---------------------------------------------------------------------------
  1282. //
  1283. // PROC G_LoadGame
  1284. //
  1285. // Can be called by the startup code or the menu task.
  1286. //
  1287. //---------------------------------------------------------------------------
  1288. char savename[256];
  1289. void G_LoadGame(char *name)
  1290. {
  1291. strcpy(savename, name);
  1292. gameaction = ga_loadgame;
  1293. }
  1294. //---------------------------------------------------------------------------
  1295. //
  1296. // PROC G_DoLoadGame
  1297. //
  1298. // Called by G_Ticker based on gameaction.
  1299. //
  1300. //---------------------------------------------------------------------------
  1301. #define VERSIONSIZE 16
  1302. void G_DoLoadGame(void)
  1303. {
  1304. int length;
  1305. int i;
  1306. int a, b, c;
  1307. char vcheck[VERSIONSIZE];
  1308. gameaction = ga_nothing;
  1309. length = M_ReadFile(savename, &savebuffer);
  1310. save_p = savebuffer+SAVESTRINGSIZE;
  1311. // Skip the description field
  1312. memset(vcheck, 0, sizeof(vcheck));
  1313. sprintf(vcheck, "version %i", VERSION);
  1314. if (strcmp (save_p, vcheck))
  1315. { // Bad version
  1316. return;
  1317. }
  1318. save_p += VERSIONSIZE;
  1319. gameskill = *save_p++;
  1320. gameepisode = *save_p++;
  1321. gamemap = *save_p++;
  1322. for(i = 0; i < MAXPLAYERS; i++)
  1323. {
  1324. playeringame[i] = *save_p++;
  1325. }
  1326. // Load a base level
  1327. G_InitNew(gameskill, gameepisode, gamemap);
  1328. // Create leveltime
  1329. a = *save_p++;
  1330. b = *save_p++;
  1331. c = *save_p++;
  1332. leveltime = (a<<16)+(b<<8)+c;
  1333. // De-archive all the modifications
  1334. P_UnArchivePlayers();
  1335. P_UnArchiveWorld();
  1336. P_UnArchiveThinkers();
  1337. P_UnArchiveSpecials();
  1338. if(*save_p != SAVE_GAME_TERMINATOR)
  1339. { // Missing savegame termination marker
  1340. I_Error("Bad savegame");
  1341. }
  1342. Z_Free(savebuffer);
  1343. }
  1344. /*
  1345. ====================
  1346. =
  1347. = G_InitNew
  1348. =
  1349. = Can be called by the startup code or the menu task
  1350. = consoleplayer, displayplayer, playeringame[] should be set
  1351. ====================
  1352. */
  1353. skill_t d_skill;
  1354. int d_episode;
  1355. int d_map;
  1356. void G_DeferedInitNew (skill_t skill, int episode, int map)
  1357. {
  1358. d_skill = skill;
  1359. d_episode = episode;
  1360. d_map = map;
  1361. gameaction = ga_newgame;
  1362. }
  1363. void G_DoNewGame (void)
  1364. {
  1365. G_InitNew (d_skill, d_episode, d_map);
  1366. gameaction = ga_nothing;
  1367. }
  1368. extern int skytexture;
  1369. void G_InitNew(skill_t skill, int episode, int map)
  1370. {
  1371. int i;
  1372. int speed;
  1373. static char *skyLumpNames[5] =
  1374. {
  1375. "SKY1", "SKY2", "SKY3", "SKY1", "SKY3"
  1376. };
  1377. if(paused)
  1378. {
  1379. paused = false;
  1380. S_ResumeSound();
  1381. }
  1382. if(skill < sk_baby)
  1383. skill = sk_baby;
  1384. if(skill > sk_nightmare)
  1385. skill = sk_nightmare;
  1386. if(episode < 1)
  1387. episode = 1;
  1388. // Up to 9 episodes for testing
  1389. if(episode > 9)
  1390. episode = 9;
  1391. if(map < 1)
  1392. map = 1;
  1393. if(map > 9)
  1394. map = 9;
  1395. M_ClearRandom();
  1396. if(respawnparm)
  1397. {
  1398. respawnmonsters = true;
  1399. }
  1400. else
  1401. {
  1402. respawnmonsters = false;
  1403. }
  1404. // Set monster missile speeds
  1405. speed = skill == sk_nightmare;
  1406. for(i = 0; MonsterMissileInfo[i].type != -1; i++)
  1407. {
  1408. mobjinfo[MonsterMissileInfo[i].type].speed
  1409. = MonsterMissileInfo[i].speed[speed]<<FRACBITS;
  1410. }
  1411. // Force players to be initialized upon first level load
  1412. for(i = 0; i < MAXPLAYERS; i++)
  1413. {
  1414. players[i].playerstate = PST_REBORN;
  1415. players[i].didsecret = false;
  1416. }
  1417. // Set up a bunch of globals
  1418. usergame = true; // will be set false if a demo
  1419. paused = false;
  1420. demorecording = false;
  1421. demoplayback = false;
  1422. viewactive = true;
  1423. gameepisode = episode;
  1424. gamemap = map;
  1425. gameskill = skill;
  1426. viewactive = true;
  1427. BorderNeedRefresh = true;
  1428. // Set the sky map
  1429. if(episode > 5)
  1430. {
  1431. skytexture = R_TextureNumForName("SKY1");
  1432. }
  1433. else
  1434. {
  1435. skytexture = R_TextureNumForName(skyLumpNames[episode-1]);
  1436. }
  1437. //
  1438. // give one null ticcmd_t
  1439. //
  1440. #if 0
  1441. gametic = 0;
  1442. maketic = 1;
  1443. for (i=0 ; i<MAXPLAYERS ; i++)
  1444. nettics[i] = 1; // one null event for this gametic
  1445. memset (localcmds,0,sizeof(localcmds));
  1446. memset (netcmds,0,sizeof(netcmds));
  1447. #endif
  1448. G_DoLoadLevel();
  1449. }
  1450. /*
  1451. ===============================================================================
  1452. DEMO RECORDING
  1453. ===============================================================================
  1454. */
  1455. #define DEMOMARKER 0x80
  1456. void G_ReadDemoTiccmd (ticcmd_t *cmd)
  1457. {
  1458. if (*demo_p == DEMOMARKER)
  1459. { // end of demo data stream
  1460. G_CheckDemoStatus ();
  1461. return;
  1462. }
  1463. cmd->forwardmove = ((signed char)*demo_p++);
  1464. cmd->sidemove = ((signed char)*demo_p++);
  1465. cmd->angleturn = ((unsigned char)*demo_p++)<<8;
  1466. cmd->buttons = (unsigned char)*demo_p++;
  1467. cmd->lookfly = (unsigned char)*demo_p++;
  1468. cmd->arti = (unsigned char)*demo_p++;
  1469. }
  1470. void G_WriteDemoTiccmd (ticcmd_t *cmd)
  1471. {
  1472. if (gamekeydown['q']) // press q to end demo recording
  1473. G_CheckDemoStatus ();
  1474. *demo_p++ = cmd->forwardmove;
  1475. *demo_p++ = cmd->sidemove;
  1476. *demo_p++ = cmd->angleturn>>8;
  1477. *demo_p++ = cmd->buttons;
  1478. *demo_p++ = cmd->lookfly;
  1479. *demo_p++ = cmd->arti;
  1480. demo_p -= 6;
  1481. G_ReadDemoTiccmd (cmd); // make SURE it is exactly the same
  1482. }
  1483. /*
  1484. ===================
  1485. =
  1486. = G_RecordDemo
  1487. =
  1488. ===================
  1489. */
  1490. void G_RecordDemo (skill_t skill, int numplayers, int episode, int map, char *name)
  1491. {
  1492. int i;
  1493. G_InitNew (skill, episode, map);
  1494. usergame = false;
  1495. strcpy (demoname, name);
  1496. strcat (demoname, ".lmp");
  1497. demobuffer = demo_p = Z_Malloc (0x20000,PU_STATIC,NULL);
  1498. *demo_p++ = skill;
  1499. *demo_p++ = episode;
  1500. *demo_p++ = map;
  1501. for (i=0 ; i<MAXPLAYERS ; i++)
  1502. *demo_p++ = playeringame[i];
  1503. demorecording = true;
  1504. }
  1505. /*
  1506. ===================
  1507. =
  1508. = G_PlayDemo
  1509. =
  1510. ===================
  1511. */
  1512. char *defdemoname;
  1513. void G_DeferedPlayDemo (char *name)
  1514. {
  1515. defdemoname = name;
  1516. gameaction = ga_playdemo;
  1517. }
  1518. void G_DoPlayDemo (void)
  1519. {
  1520. skill_t skill;
  1521. int i, episode, map;
  1522. gameaction = ga_nothing;
  1523. demobuffer = demo_p = W_CacheLumpName (defdemoname, PU_STATIC);
  1524. skill = *demo_p++;
  1525. episode = *demo_p++;
  1526. map = *demo_p++;
  1527. for (i=0 ; i<MAXPLAYERS ; i++)
  1528. playeringame[i] = *demo_p++;
  1529. precache = false; // don't spend a lot of time in loadlevel
  1530. G_InitNew (skill, episode, map);
  1531. precache = true;
  1532. usergame = false;
  1533. demoplayback = true;
  1534. }
  1535. /*
  1536. ===================
  1537. =
  1538. = G_TimeDemo
  1539. =
  1540. ===================
  1541. */
  1542. void G_TimeDemo (char *name)
  1543. {
  1544. skill_t skill;
  1545. int episode, map;
  1546. demobuffer = demo_p = W_CacheLumpName (name, PU_STATIC);
  1547. skill = *demo_p++;
  1548. episode = *demo_p++;
  1549. map = *demo_p++;
  1550. G_InitNew (skill, episode, map);
  1551. usergame = false;
  1552. demoplayback = true;
  1553. timingdemo = true;
  1554. singletics = true;
  1555. }
  1556. /*
  1557. ===================
  1558. =
  1559. = G_CheckDemoStatus
  1560. =
  1561. = Called after a death or level completion to allow demos to be cleaned up
  1562. = Returns true if a new demo loop action will take place
  1563. ===================
  1564. */
  1565. boolean G_CheckDemoStatus (void)
  1566. {
  1567. int endtime;
  1568. if (timingdemo)
  1569. {
  1570. endtime = I_GetTime ();
  1571. I_Error ("timed %i gametics in %i realtics",gametic
  1572. , endtime-starttime);
  1573. }
  1574. if (demoplayback)
  1575. {
  1576. if (singledemo)
  1577. I_Quit ();
  1578. Z_ChangeTag (demobuffer, PU_CACHE);
  1579. demoplayback = false;
  1580. D_AdvanceDemo ();
  1581. return true;
  1582. }
  1583. if (demorecording)
  1584. {
  1585. *demo_p++ = DEMOMARKER;
  1586. M_WriteFile (demoname, demobuffer, demo_p - demobuffer);
  1587. Z_Free (demobuffer);
  1588. demorecording = false;
  1589. I_Error ("Demo %s recorded",demoname);
  1590. }
  1591. return false;
  1592. }
  1593. /**************************************************************************/
  1594. /**************************************************************************/
  1595. //==========================================================================
  1596. //
  1597. // G_SaveGame
  1598. //
  1599. // Called by the menu task. <description> is a 24 byte text string.
  1600. //
  1601. //==========================================================================
  1602. void G_SaveGame(int slot, char *description)
  1603. {
  1604. savegameslot = slot;
  1605. strcpy(savedescription, description);
  1606. sendsave = true;
  1607. }
  1608. //==========================================================================
  1609. //
  1610. // G_DoSaveGame
  1611. //
  1612. // Called by G_Ticker based on gameaction.
  1613. //
  1614. //==========================================================================
  1615. void G_DoSaveGame(void)
  1616. {
  1617. int i;
  1618. char name[100];
  1619. char verString[VERSIONSIZE];
  1620. char *description;
  1621. if(cdrom)
  1622. {
  1623. sprintf(name, SAVEGAMENAMECD"%d.hsg", savegameslot);
  1624. }
  1625. else
  1626. {
  1627. sprintf(name, SAVEGAMENAME"%d.hsg", savegameslot);
  1628. }
  1629. description = savedescription;
  1630. SV_Open(name);
  1631. SV_Write(description, SAVESTRINGSIZE);
  1632. memset(verString, 0, sizeof(verString));
  1633. sprintf(verString, "version %i", VERSION);
  1634. SV_Write(verString, VERSIONSIZE);
  1635. SV_WriteByte(gameskill);
  1636. SV_WriteByte(gameepisode);
  1637. SV_WriteByte(gamemap);
  1638. for(i = 0; i < MAXPLAYERS; i++)
  1639. {
  1640. SV_WriteByte(playeringame[i]);
  1641. }
  1642. SV_WriteByte(leveltime>>16);
  1643. SV_WriteByte(leveltime>>8);
  1644. SV_WriteByte(leveltime);
  1645. P_ArchivePlayers();
  1646. P_ArchiveWorld();
  1647. P_ArchiveThinkers();
  1648. P_ArchiveSpecials();
  1649. SV_Close(name);
  1650. gameaction = ga_nothing;
  1651. savedescription[0] = 0;
  1652. P_SetMessage(&players[consoleplayer], TXT_GAMESAVED, true);
  1653. }
  1654. //==========================================================================
  1655. //
  1656. // SV_Open
  1657. //
  1658. //==========================================================================
  1659. void SV_Open(char *fileName)
  1660. {
  1661. MallocFailureOk = true;
  1662. save_p = savebuffer = Z_Malloc(SAVEGAMESIZE, PU_STATIC, NULL);
  1663. MallocFailureOk = false;
  1664. if(savebuffer == NULL)
  1665. { // Not enough memory - use file save method
  1666. SaveGameType = SVG_FILE;
  1667. SaveGameFP = fopen(fileName, "wb");
  1668. }
  1669. else
  1670. {
  1671. SaveGameType = SVG_RAM;
  1672. }
  1673. }
  1674. //==========================================================================
  1675. //
  1676. // SV_Close
  1677. //
  1678. //==========================================================================
  1679. void SV_Close(char *fileName)
  1680. {
  1681. int length;
  1682. SV_WriteByte(SAVE_GAME_TERMINATOR);
  1683. if(SaveGameType == SVG_RAM)
  1684. {
  1685. length = save_p-savebuffer;
  1686. if(length > SAVEGAMESIZE)
  1687. {
  1688. I_Error("Savegame buffer overrun");
  1689. }
  1690. M_WriteFile(fileName, savebuffer, length);
  1691. Z_Free(savebuffer);
  1692. }
  1693. else
  1694. { // SVG_FILE
  1695. fclose(SaveGameFP);
  1696. }
  1697. }
  1698. //==========================================================================
  1699. //
  1700. // SV_Write
  1701. //
  1702. //==========================================================================
  1703. void SV_Write(void *buffer, int size)
  1704. {
  1705. if(SaveGameType == SVG_RAM)
  1706. {
  1707. memcpy(save_p, buffer, size);
  1708. save_p += size;
  1709. }
  1710. else
  1711. { // SVG_FILE
  1712. fwrite(buffer, size, 1, SaveGameFP);
  1713. }
  1714. }
  1715. void SV_WriteByte(byte val)
  1716. {
  1717. SV_Write(&val, sizeof(byte));
  1718. }
  1719. void SV_WriteWord(unsigned short val)
  1720. {
  1721. SV_Write(&val, sizeof(unsigned short));
  1722. }
  1723. void SV_WriteLong(unsigned int val)
  1724. {
  1725. SV_Write(&val, sizeof(int));
  1726. }