G_OLD.C 37 KB

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