G_GAME.C 41 KB

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