BLOOD.CPP 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451
  1. #include <string.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <assert.h>
  5. #include <io.h>
  6. #include <fcntl.h>
  7. #include <malloc.h>
  8. #include "engine.h"
  9. #include "multi.h"
  10. #include "typedefs.h"
  11. #include "misc.h"
  12. #include "gameutil.h"
  13. #include "globals.h"
  14. #include "names.h"
  15. #include "key.h"
  16. #include "timer.h"
  17. #include "textio.h"
  18. #include "inifile.h"
  19. #include "screen.h"
  20. #include "view.h"
  21. #include "db.h"
  22. #include "debug4g.h"
  23. #include "error.h"
  24. #include "fire.h"
  25. #include "sectorfx.h"
  26. #include "helix.h"
  27. #include "getopt.h"
  28. #include "levels.h"
  29. #include "sound.h"
  30. #include "controls.h"
  31. #include "actor.h"
  32. #include "player.h"
  33. #include "resource.h"
  34. #include "protect.h"
  35. #include "triggers.h"
  36. #include "weapon.h"
  37. #include "options.h"
  38. #include "tile.h"
  39. #include "gui.h"
  40. #include "seq.h"
  41. #include "trig.h"
  42. #include "mirrors.h"
  43. #include "warp.h"
  44. #include "credits.h"
  45. #include "sfx.h"
  46. #include <memcheck.h>
  47. #define kAttrTitle (kColorRed * 16 + kColorYellow)
  48. EHF prevErrorHandler;
  49. static BOOL gForceMap = FALSE;
  50. short gPacketIndex = 0;
  51. BOOL ready2send = FALSE;
  52. static char playerreadyflag[kMaxPlayers];
  53. #define kNetFifoSize 256
  54. INPUT gNetInput[kMaxPlayers]; // input received from net players
  55. INPUT gFifoInput[kNetFifoSize][kMaxPlayers];
  56. int gNetFifoClock;
  57. int gNetFifoHead, gNetFifoTail;
  58. ulong gFifoSendCRC[kNetFifoSize], gFifoReceiveCRC[kNetFifoSize];
  59. int gSendCRCHead, gReceiveCRCHead, gCRCTail;
  60. BOOL bOutOfSync = FALSE;
  61. /***********************************************************************
  62. * Function prototypes
  63. **********************************************************************/
  64. enum {
  65. // kTweakDecel = 0,
  66. // kTweakSideDecel,
  67. kTweakFrontAccel,
  68. kTweakSideAccel,
  69. kTweakBackAccel,
  70. kTweakFrontSpeed1,
  71. kTweakFrontSpeed2,
  72. kTweakSideSpeed1,
  73. kTweakSideSpeed2,
  74. kTweakBackSpeed1,
  75. kTweakBackSpeed2,
  76. kTweakPace1,
  77. kTweakPace2,
  78. kMaxTweaks
  79. };
  80. struct TWEAKS
  81. {
  82. int x;
  83. int y;
  84. int *pData;
  85. int nDefault;
  86. } tweakSet[ kMaxTweaks ] =
  87. {
  88. // {2, 2, &gPosture[0].decel, 0},
  89. // {2, 20, &gPosture[0].sideDecel, 0},
  90. {2, 38, &gPosture[0].frontAccel, 0},
  91. {2, 56, &gPosture[0].sideAccel, 0},
  92. {2, 74, &gPosture[0].backAccel, 0},
  93. {2, 92, &gPosture[0].frontSpeed[0], 0},
  94. {2, 110, &gPosture[0].frontSpeed[1], 0},
  95. {2, 128, &gPosture[0].sideSpeed[0], 0},
  96. {2, 146, &gPosture[0].sideSpeed[1], 0},
  97. {2, 164, &gPosture[0].backSpeed[0], 0},
  98. {90, 2, &gPosture[0].backSpeed[1], 0},
  99. {90, 20, &gPosture[0].pace[0], 0},
  100. {90, 38, &gPosture[0].pace[1], 0},
  101. };
  102. void TweakProfile( char *title )
  103. {
  104. int i;
  105. EditNumber *enTweaks[kMaxTweaks];
  106. // create the dialog
  107. Window dialog(0, 0, 319, 199, title);
  108. // these will automatically be destroyed when the dialog is destroyed
  109. for (i = 0; i < kMaxTweaks; i++)
  110. {
  111. tweakSet[i].nDefault = *tweakSet[i].pData; // initialize the defaults
  112. enTweaks[i] = new EditNumber(tweakSet[i].x, tweakSet[i].y, 80, 16, tweakSet[i].nDefault);
  113. dialog.Insert(enTweaks[i]);
  114. }
  115. ShowModal(&dialog);
  116. for (i = 0; i < kMaxTweaks; i++)
  117. {
  118. if ( dialog.endState != mrOk)
  119. *tweakSet[i].pData = tweakSet[i].nDefault; // restore the defaults
  120. else
  121. *tweakSet[i].pData = enTweaks[i]->value;
  122. }
  123. }
  124. void PreloadCache( void )
  125. {
  126. long i;
  127. dprintf("Preload floor and ceiling tiles\n");
  128. for (i = 0; i < numsectors; i++)
  129. {
  130. tilePreloadTile(sector[i].floorpicnum);
  131. tilePreloadTile(sector[i].ceilingpicnum);
  132. if ( sector[i].ceilingstat & kSectorParallax )
  133. {
  134. for (int j = 1; j < gSkyCount; j++)
  135. tilePreloadTile(sector[i].ceilingpicnum + j);
  136. }
  137. }
  138. dprintf("Preload wall tiles\n");
  139. for (i = 0; i < numwalls; i++)
  140. {
  141. tilePreloadTile(wall[i].picnum);
  142. if (wall[i].overpicnum >= 0)
  143. tilePreloadTile(wall[i].overpicnum);
  144. }
  145. dprintf("Preload sprite tiles\n");
  146. for (i = 0; i < kMaxSprites; i++)
  147. {
  148. if (sprite[i].statnum < kMaxStatus)
  149. tilePreloadTile(sprite[i].picnum);
  150. }
  151. }
  152. void prepareboard( void )
  153. {
  154. if ( !gForceMap )
  155. levelGetName(gEpisodeId, gMapId);
  156. dbLoadMap(gLevelName, &startposx, &startposy, &startposz, &startang, &startsectnum);
  157. srand(gMapCRC);
  158. levelLoadDef();
  159. scrLoadPLUs();
  160. for ( int i = 0; i < kMaxPlayers; i++ )
  161. {
  162. gStartZone[i].x = startposx;
  163. gStartZone[i].y = startposy;
  164. gStartZone[i].z = sector[startsectnum].floorz;
  165. gStartZone[i].sector = startsectnum;
  166. gStartZone[i].angle = startang;
  167. }
  168. // make sure the player starts out on the floor
  169. startposz = sector[startsectnum].floorz;
  170. seqKillAll(); // clear the current sequence list
  171. InitSectorFX();
  172. InitPlayerStartZones();
  173. actInit();
  174. // initialize all the tag buckets
  175. dprintf("evInit()\n");
  176. evInit();
  177. gFrameClock = gGameClock = 0;
  178. // initialize external trigger masks for sprites, sectors, and walls
  179. dprintf("trInit()\n");
  180. trInit();
  181. PreloadCache();
  182. //Map starts out blank; you map out what you see in 3D mode
  183. automapping = 1;
  184. for (i = 0; i < numplayers; i++)
  185. playerInit( i );
  186. InitMirrors();
  187. // start the music for the level
  188. sndPlaySong(gLevelSong, TRUE);
  189. gNetFifoClock = gFrameClock = gGameClock = 0;
  190. gNetFifoHead = gNetFifoTail = 0;
  191. gSendCRCHead = gReceiveCRCHead = gCRCTail = 0;
  192. }
  193. BYTE cheatAmmo[] = { KEY_Q, KEY_S, KEY_A, KEY_M, KEY_M, KEY_O };
  194. BYTE cheatRate[] = { KEY_Q, KEY_S, KEY_R, KEY_A, KEY_T, KEY_E };
  195. BYTE cheatGoto[] = { KEY_Q, KEY_S, KEY_G, KEY_O, KEY_T, KEY_O };
  196. BYTE cheatJump[] = { KEY_Q, KEY_S, KEY_J, KEY_U, KEY_M, KEY_P };
  197. BYTE cheatClip[] = { KEY_Q, KEY_S, KEY_C, KEY_L, KEY_I, KEY_P };
  198. BYTE cheatKeys[] = { KEY_Q, KEY_S, KEY_K, KEY_E, KEY_Y, KEY_S };
  199. BYTE cheatMaps[] = { KEY_Q, KEY_S, KEY_M, KEY_A, KEY_P, KEY_S };
  200. BYTE cheatHurt[] = { KEY_Q, KEY_S, KEY_O, KEY_U, KEY_C, KEY_H };
  201. BYTE cheatExplode[] = { KEY_Q, KEY_S, KEY_B, KEY_O, KEY_O, KEY_M };
  202. BYTE cheatBurn[] = { KEY_Q, KEY_S, KEY_B, KEY_U, KEY_R, KEY_N };
  203. BYTE cheatHigh[] = { KEY_Q, KEY_S, KEY_H, KEY_I, KEY_G, KEY_H };
  204. BYTE cheatInviso[] = { KEY_Q, KEY_S, KEY_O, KEY_N, KEY_E, KEY_R, KEY_I, KEY_N, KEY_G };
  205. BYTE cheatBeast[] = { KEY_Q, KEY_S, KEY_L, KEY_U, KEY_S, KEY_T };
  206. BYTE cheatHealth[] = { KEY_Q, KEY_S, KEY_L, KEY_A, KEY_M, KEY_E };
  207. BYTE cheatSeer[] = { KEY_Q, KEY_S, KEY_I, KEY_C, KEY_U };
  208. BYTE cheatFrag[] = { KEY_Q, KEY_S, KEY_F, KEY_R, KEY_A, KEY_G };
  209. BYTE cheatGod[] = { KEY_Q, KEY_S, KEY_G, KEY_O, KEY_D };
  210. BYTE cheatSight[] = { KEY_Q, KEY_S, KEY_O, KEY_S, KEY_C , KEY_A , KEY_R };
  211. BYTE cheatAim[] = { KEY_Q, KEY_S, KEY_A, KEY_I, KEY_M };
  212. void LocalKeys( void )
  213. {
  214. char buffer[80];
  215. BYTE key;
  216. BYTE alt = keystatus[KEY_LALT] | keystatus[KEY_RALT];
  217. while ( (key = keyGet()) != 0 )
  218. {
  219. switch( key )
  220. {
  221. case KEY_F1: // help/info screens
  222. TweakProfile( "Player Profile" );
  223. break;
  224. case KEY_F2: // save game
  225. break;
  226. case KEY_F3: // load game
  227. break;
  228. case KEY_F4: // volume
  229. break;
  230. case KEY_F5: // detail
  231. break;
  232. case KEY_F6: // quicksave
  233. break;
  234. case KEY_F7: // end game
  235. break;
  236. case KEY_F8: // messages
  237. break;
  238. case KEY_F10: // quit game
  239. break;
  240. case KEY_F11: // gamma
  241. if (gGamma == gGammaLevels - 1)
  242. gGamma = 0;
  243. else
  244. gGamma = ClipHigh(gGamma + 1, gGammaLevels - 1);
  245. BloodINI.PutKeyInt("View", "Gamma", gGamma);
  246. scrSetGamma(gGamma);
  247. sprintf(buffer, "Gamma correction level %i", gGamma);
  248. viewSetMessage(buffer);
  249. break;
  250. case KEY_F12:
  251. // setup gViewIndex for different play and power-up modes
  252. if ( gNetGame && gNetPlayers > 1 )
  253. {
  254. if ( powerupCheck(gMe, kItemCrystalBall - kItemBase) )
  255. {
  256. gViewIndex = connectpoint2[gViewIndex];
  257. if (gViewIndex == -1)
  258. gViewIndex = connecthead;
  259. }
  260. else
  261. {
  262. if (gNetMode == kNetModeTeams)
  263. {
  264. gViewIndex = connectpoint2[gViewIndex];
  265. if (gViewIndex == -1)
  266. gViewIndex = connecthead;
  267. while ( gPlayer[gViewIndex].teamID != gMe->teamID )
  268. {
  269. gViewIndex = connectpoint2[gViewIndex];
  270. if (gViewIndex == -1)
  271. gViewIndex = connecthead;
  272. }
  273. gView = &gPlayer[gViewIndex];
  274. }
  275. }
  276. }
  277. else
  278. {
  279. gViewIndex = connectpoint2[gViewIndex];
  280. if (gViewIndex == -1)
  281. gViewIndex = connecthead;
  282. gView = &gPlayer[gViewIndex];
  283. }
  284. break;
  285. case KEY_MINUS:
  286. if ( keystatus[KEY_G] )
  287. {
  288. gGamma = ClipLow(gGamma - 1, 0);
  289. BloodINI.PutKeyInt("View", "Gamma", gGamma);
  290. scrSetGamma(gGamma);
  291. sprintf(buffer, "Gamma correction level %i", gGamma);
  292. viewSetMessage(buffer);
  293. }
  294. else if ( keystatus[KEY_D] )
  295. {
  296. visibility = ClipHigh(visibility + 16, 4096);
  297. sprintf(buffer, "Depth cueing level %i", visibility);
  298. viewSetMessage(buffer);
  299. }
  300. else if ( keystatus[KEY_S] )
  301. {
  302. gSpring = ClipLow(gSpring - 1, 1);
  303. BloodINI.PutKeyInt("Player", "SpringCoefficient", gSpring);
  304. gSpringPhaseInc = (int)(kAngle360 / (2 * 3.141592654 * (double)gSpring));
  305. sprintf(buffer, "Spring coefficient = %i", gSpring);
  306. viewSetMessage(buffer);
  307. }
  308. else if (gViewMode == kView3D)
  309. {
  310. viewResizeView(gViewSize + 1);
  311. BloodINI.PutKeyInt("View", "Size", gViewSize);
  312. }
  313. break;
  314. case KEY_PLUS:
  315. if ( keystatus[KEY_G] )
  316. {
  317. gGamma = ClipHigh(gGamma + 1, gGammaLevels - 1);
  318. BloodINI.PutKeyInt("View", "Gamma", gGamma);
  319. scrSetGamma(gGamma);
  320. sprintf(buffer, "Gamma correction level %i", gGamma);
  321. viewSetMessage(buffer);
  322. }
  323. else if ( keystatus[KEY_D] )
  324. {
  325. visibility = ClipLow(visibility - 16, 128);
  326. sprintf(buffer, "Depth cueing level %i", visibility);
  327. viewSetMessage(buffer);
  328. }
  329. else if ( keystatus[KEY_S])
  330. {
  331. gSpring++;
  332. BloodINI.PutKeyInt("Player", "SpringCoefficient", gSpring);
  333. gSpringPhaseInc = (int)(kAngle360 / (2 * 3.141592654 * (double)gSpring));
  334. sprintf(buffer, "Spring coefficient = %i", gSpring);
  335. viewSetMessage(buffer);
  336. }
  337. else if (gViewMode == kView3D)
  338. {
  339. viewResizeView(gViewSize - 1);
  340. BloodINI.PutKeyInt("View", "Size", gViewSize);
  341. }
  342. break;
  343. case KEY_P:
  344. parallaxtype = (char)IncRotate(parallaxtype, 3);
  345. break;
  346. case KEY_V:
  347. if ( alt )
  348. {
  349. if ( gViewPos > 0 )
  350. gViewPos = (VIEWPOS)((gViewPos & 7) + 1);
  351. sprintf(buffer, "gViewPos = %i", gViewPos);
  352. viewSetMessage(buffer);
  353. }
  354. else
  355. {
  356. if ( gViewPos > 0 )
  357. gViewPos = kViewPosCenter;
  358. else
  359. gViewPos = kViewPosBack;
  360. }
  361. break;
  362. case KEY_PRINTSCREEN:
  363. screencapture("captxxxx.pcx");
  364. break;
  365. default:
  366. break;
  367. }
  368. }
  369. if (keystatus[KEY_MINUS] && (gViewMode == kView2D || gViewMode == kView2DIcon))
  370. {
  371. gZoom = ClipLow(gZoom - (gZoom >> 6), 64);
  372. }
  373. if (keystatus[KEY_PLUS] && (gViewMode == kView2D || gViewMode == kView2DIcon))
  374. {
  375. gZoom = ClipHigh(gZoom + (gZoom >> 6), 4096);
  376. }
  377. int nDelirium = powerupCheck(gView, kItemShroomDelirium - kItemBase);
  378. if ( nDelirium )
  379. {
  380. static int timer = 0;
  381. timer += kFrameTicks;
  382. int maxTilt = kAngle30;
  383. int maxTurn = kAngle30;
  384. int maxPitch = 20;
  385. // this constant determine when the effect starts to wear off
  386. #define kWaneTime 512
  387. if (nDelirium < kWaneTime)
  388. {
  389. int scale = (nDelirium << 16) / kWaneTime;
  390. maxTilt = mulscale16(maxTilt, scale);
  391. maxTurn = mulscale16(maxTurn, scale);
  392. maxPitch = mulscale16(maxPitch, scale);
  393. }
  394. gScreenTilt = mulscale30(Sin(timer * 2) / 2 + Sin(timer * 3) / 2, maxTilt);
  395. deliriumTurn = mulscale30(Sin(timer * 3) / 2 + Sin(timer * 4) / 2, maxTurn);
  396. deliriumPitch = mulscale30(Sin(timer * 4) / 2 + Sin(timer * 5) / 2, maxPitch);
  397. }
  398. else
  399. {
  400. // this code needs a different calculation,
  401. // and should probably be partially placed in ctrlGetInput
  402. if ( keystatus[KEY_LBRACE] )
  403. gScreenTilt += kFrameTicks * 4;
  404. if ( keystatus[KEY_RBRACE] )
  405. gScreenTilt -= kFrameTicks * 4;
  406. // put gScreenTilt in the range -kAngle180 to kAngle180
  407. gScreenTilt = ((gScreenTilt + kAngle180) & kAngleMask) - kAngle180;
  408. // pull screen to vertical
  409. if ( gScreenTilt > 0 )
  410. gScreenTilt = ClipLow(gScreenTilt - kFrameTicks * 2, 0);
  411. else if ( gScreenTilt < 0 )
  412. gScreenTilt = ClipHigh(gScreenTilt + kFrameTicks * 2, 0);
  413. }
  414. if ( keyCompareStream( cheatRate, LENGTH(cheatRate)) )
  415. {
  416. keyPokeStream(0);
  417. gShowFrameRate = !gShowFrameRate;
  418. }
  419. if ( keyCompareStream( cheatFrag, LENGTH(cheatFrag)) )
  420. {
  421. keyPokeStream(0);
  422. gShowFrags = !gShowFrags;
  423. }
  424. if ( keyCompareStream( cheatAim, LENGTH(cheatAim)) )
  425. {
  426. keyPokeStream(0);
  427. gAimReticle = !gAimReticle;
  428. }
  429. if ( gNetGame == FALSE )
  430. {
  431. if ( keyCompareStream( cheatAmmo, LENGTH(cheatAmmo)) )
  432. {
  433. keyPokeStream(0);
  434. for (int i = 0; i < kWeaponMax; i++)
  435. {
  436. gMe->hasWeapon[i] = TRUE;
  437. //gMe->weaponState[i] = 0;
  438. }
  439. for (i = 0; i < kAmmoMax; i++)
  440. {
  441. gMe->ammoCount[i] = gAmmoInfo[i].max;
  442. }
  443. viewSetMessage("Full armory granted!");
  444. }
  445. if ( keyCompareStream( cheatKeys, LENGTH(cheatKeys)) )
  446. {
  447. keyPokeStream(0);
  448. for (int i = 1; i <= 6; i++)
  449. gMe->hasKey[i] = (short)(kPicKey1 + i - 1);
  450. viewSetMessage("Full keychain granted!");
  451. }
  452. if ( keyCompareStream( cheatMaps, LENGTH(cheatMaps)) )
  453. {
  454. keyPokeStream(0);
  455. gFullMap = !gFullMap;
  456. if ( gFullMap )
  457. viewSetMessage("Full atlas granted");
  458. else
  459. viewSetMessage("Full atlas revoked");
  460. }
  461. if ( keyCompareStream( cheatClip, LENGTH(cheatClip)) )
  462. {
  463. keyPokeStream(0);
  464. gNoClip = !gNoClip;
  465. if ( gNoClip )
  466. viewSetMessage("Non-clipped mode enabled");
  467. else
  468. viewSetMessage("Non-clipped mode disabled");
  469. }
  470. if ( keyCompareStream( cheatGoto, LENGTH(cheatGoto)) )
  471. {
  472. keyPokeStream(0);
  473. long x = GetNumberBox("X=", gMe->sprite->x, gMe->sprite->x);
  474. long y = GetNumberBox("Y=", gMe->sprite->y, gMe->sprite->y);
  475. short nSector;
  476. updatesector(x, y, &nSector);
  477. if (nSector != -1) {
  478. changespritesect((short)gMe->nSprite, nSector);
  479. gMe->sprite->x = x;
  480. gMe->sprite->y = y;
  481. gMe->sprite->z = sector[nSector].floorz;
  482. gMe->sprite->zvel = 0;
  483. }
  484. }
  485. if ( keyCompareStream( cheatJump, LENGTH(cheatJump)) )
  486. {
  487. keyPokeStream(0);
  488. long newEpisodeId = GetNumberBox("Episode (1..3)=", gEpisodeId, 1);
  489. long newMapId = GetNumberBox("Map (1..15)=", gMapId, 1);
  490. if ((newEpisodeId != 0) && (newMapId != 0)) {
  491. gEpisodeId = newEpisodeId;
  492. gMapId = newMapId;
  493. gForceMap = FALSE;
  494. ready2send = FALSE;
  495. prepareboard();
  496. ready2send = TRUE;
  497. }
  498. viewResizeView(gViewSize);
  499. }
  500. if ( keyCompareStream( cheatHurt, LENGTH(cheatHurt)) )
  501. {
  502. keyPokeStream(0);
  503. actDamageSprite(gMe->nSprite, gMe->nSprite, kDamagePummel, 100 << 4);
  504. viewSetMessage("Ouch!");
  505. }
  506. if ( keyCompareStream( cheatExplode, LENGTH(cheatExplode)) )
  507. {
  508. keyPokeStream(0);
  509. actDamageSprite(gMe->nSprite, gMe->nSprite, kDamageExplode, 100 << 4);
  510. viewSetMessage("Boom.");
  511. }
  512. if ( keyCompareStream( cheatBurn, LENGTH(cheatBurn)) )
  513. {
  514. keyPokeStream(0);
  515. actAddBurnTime( gMe->nSprite, gMe->xsprite, kMaxBurnTime );
  516. viewSetMessage("Crispy!");
  517. }
  518. if ( keyCompareStream( cheatHigh, LENGTH(cheatHigh)) )
  519. {
  520. keyPokeStream(0);
  521. if ( powerupCheck( gMe, kItemShroomDelirium - kItemBase ) )
  522. powerupDeactivate( gMe, kItemShroomDelirium - kItemBase );
  523. else
  524. powerupActivate( gMe, kItemShroomDelirium - kItemBase );
  525. viewSetMessage("Cosmic, man!");
  526. }
  527. if ( keyCompareStream( cheatGod, LENGTH(cheatGod)) )
  528. {
  529. keyPokeStream(0);
  530. playerSetGodMode( gMe, !gMe->godMode );
  531. if (gMe->godMode)
  532. viewSetMessage("Entering Broussard mode!");
  533. else
  534. viewSetMessage("Back to normal mode!");
  535. }
  536. if ( keyCompareStream( cheatInviso, LENGTH(cheatInviso)) )
  537. {
  538. keyPokeStream(0);
  539. if ( powerupCheck( gMe, kItemLtdInvisibility - kItemBase ) )
  540. {
  541. viewSetMessage("Deactivating the One Ring");
  542. powerupDeactivate( gMe, kItemLtdInvisibility - kItemBase );
  543. }
  544. else
  545. {
  546. viewSetMessage("Activating the One Ring");
  547. powerupActivate( gMe, kItemLtdInvisibility - kItemBase );
  548. }
  549. }
  550. if ( keyCompareStream( cheatSeer, LENGTH(cheatSeer)) )
  551. {
  552. keyPokeStream(0);
  553. if ( powerupCheck( gMe, kItemCrystalBall - kItemBase ) )
  554. {
  555. viewSetMessage("Deactivating the Crystal Ball");
  556. powerupDeactivate( gMe, kItemCrystalBall - kItemBase );
  557. }
  558. else
  559. {
  560. viewSetMessage("Activating the Crystal Ball");
  561. powerupActivate( gMe, kItemCrystalBall - kItemBase );
  562. }
  563. }
  564. if ( keyCompareStream( cheatBeast, LENGTH(cheatBeast)) )
  565. {
  566. keyPokeStream(0);
  567. if (gMe->lifemode == kModeBeast)
  568. playerSetRace( gMe, kModeHuman, gMe->sprite->type );
  569. else
  570. playerSetRace( gMe, kModeBeast, gMe->sprite->type );
  571. }
  572. if ( keyCompareStream( cheatHealth, LENGTH(cheatHealth)) )
  573. {
  574. keyPokeStream(0);
  575. gMe->xsprite->health = 200 << 4;
  576. viewSetMessage("Full health granted, you maggot!");
  577. }
  578. if ( keyCompareStream( cheatSight, LENGTH(cheatSight)) )
  579. {
  580. keyPokeStream(0);
  581. if ( powerupCheck( gMe, kItemRoseGlasses - kItemBase ) )
  582. {
  583. viewSetMessage("Deactivating Oscar Sight");
  584. powerupDeactivate( gMe, kItemRoseGlasses - kItemBase );
  585. }
  586. else
  587. {
  588. viewSetMessage("Activating Oscar Sight");
  589. powerupActivate( gMe, kItemRoseGlasses - kItemBase );
  590. }
  591. }
  592. }
  593. if (*control[kAutomapToggle])
  594. {
  595. *control[kAutomapToggle] = 0;
  596. if (gViewMode == kView3D)
  597. {
  598. gViewMode = kView2D;
  599. }
  600. else if (gViewMode == kView2D)
  601. {
  602. gViewMode = kView2DIcon;
  603. }
  604. else
  605. {
  606. gViewMode = kView3D;
  607. viewResizeView(gViewSize);
  608. }
  609. }
  610. if (gNetPlayers == 1) //Single player only keys
  611. {
  612. if (keystatus[KEY_INSERT]) //Insert - Insert player
  613. {
  614. keystatus[KEY_INSERT] = 0;
  615. if (numplayers < kMaxPlayers)
  616. {
  617. connectpoint2[numplayers-1] = numplayers;
  618. connectpoint2[numplayers] = -1;
  619. gViewIndex = myconnectindex = numplayers++;
  620. gView = gMe = &gPlayer[myconnectindex];
  621. playerInit( myconnectindex );
  622. }
  623. }
  624. if (keystatus[KEY_DELETE]) //Delete - Delete player
  625. {
  626. keystatus[KEY_DELETE] = 0;
  627. if (numplayers > 1)
  628. {
  629. // actDeletePlayer( myconnectindex ); // should probably add something like this!
  630. numplayers--;
  631. connectpoint2[numplayers-1] = -1;
  632. seqKill( SS_SPRITE, gPlayer[numplayers].sprite->extra );
  633. actPostSprite( gPlayer[numplayers].nSprite, kStatFree );
  634. gPlayer[numplayers].sprite = NULL;
  635. if (myconnectindex >= numplayers) myconnectindex = 0;
  636. gMe = &gPlayer[myconnectindex];
  637. if (gViewIndex >= numplayers) gViewIndex = 0;
  638. gView = &gPlayer[gViewIndex];
  639. }
  640. }
  641. if (keystatus[KEY_SCROLLLOCK]) //Scroll Lock
  642. {
  643. keystatus[KEY_SCROLLLOCK] = 0;
  644. myconnectindex = connectpoint2[myconnectindex];
  645. if (myconnectindex < 0) myconnectindex = connecthead;
  646. gViewIndex = myconnectindex;
  647. gView = gMe = &gPlayer[myconnectindex];
  648. }
  649. }
  650. }
  651. ulong CalcGameChecksum( void )
  652. {
  653. ulong value = 0;
  654. value = Random(65536);
  655. // dprintf("Frame #%d: randCount1 = %d, randCount2 = %d, Random() = %4x\n",
  656. // gFrame, randCount1, randCount2, value);
  657. //value ^= CRC32(gPlayer, sizeof(gPlayer));
  658. for ( int i = connecthead; i >= 0; i = connectpoint2[i] )
  659. value ^= CRC32(gPlayer[i].sprite, sizeof(SPRITE));
  660. return value;
  661. }
  662. /*
  663. void CheckMasterSwitch( void )
  664. {
  665. int i, j;
  666. i = connecthead; j = connectpoint2[i];
  667. while (j >= 0)
  668. {
  669. if (gPlayer[j].keyFlags.master)
  670. {
  671. connectpoint2[i] = connectpoint2[j];
  672. connectpoint2[j] = connecthead;
  673. connecthead = (short)j;
  674. gGameClock = gFrameClock;
  675. if (connecthead == myconnectindex)
  676. viewSetMessage("Master");
  677. else
  678. viewSetMessage("Slave");
  679. gPlayer[j].keyFlags.master = 0;
  680. return;
  681. }
  682. i = j; j = connectpoint2[i];
  683. }
  684. }
  685. */
  686. /*******************************************************************************
  687. FUNCTION: ProcessFrame()
  688. DESCRIPTION: Main game processing function
  689. *******************************************************************************/
  690. void ProcessFrame()
  691. {
  692. for ( int i = connecthead; i >= 0; i = connectpoint2[i] )
  693. {
  694. gPlayerInput[i].syncFlags = gFifoInput[gNetFifoHead][i].syncFlags;
  695. gPlayerInput[i].buttonFlags = gFifoInput[gNetFifoHead][i].buttonFlags;
  696. // keys and weapon selection are non-destructive
  697. gPlayerInput[i].keyFlags.byte |= gFifoInput[gNetFifoHead][i].keyFlags.byte;
  698. if (gFifoInput[gNetFifoHead][i].newWeapon != 0)
  699. gPlayerInput[i].newWeapon = gFifoInput[gNetFifoHead][i].newWeapon;
  700. gPlayerInput[i].forward = gFifoInput[gNetFifoHead][i].forward;
  701. gPlayerInput[i].strafe = gFifoInput[gNetFifoHead][i].strafe;
  702. gPlayerInput[i].turn = gFifoInput[gNetFifoHead][i].turn;
  703. gPlayerInput[i].ticks = gFifoInput[gNetFifoHead][i].ticks;
  704. }
  705. gNetFifoHead = IncRotate(gNetFifoHead, kNetFifoSize);
  706. gFifoSendCRC[gSendCRCHead] = CalcGameChecksum();
  707. gSendCRCHead = IncRotate(gSendCRCHead, kNetFifoSize);
  708. for (i = connecthead; i >= 0; i = connectpoint2[i])
  709. {
  710. if ( gPlayerInput[i].keyFlags.restart )
  711. {
  712. gPlayerInput[i].keyFlags.restart = 0;
  713. gForceMap = TRUE;
  714. gPaused = FALSE;
  715. ready2send = FALSE;
  716. prepareboard();
  717. ready2send = TRUE;
  718. }
  719. if ( gPlayerInput[i].keyFlags.pause )
  720. {
  721. gPlayerInput[i].keyFlags.pause = 0;
  722. gPaused = !gPaused;
  723. }
  724. }
  725. if (gPaused)
  726. return;
  727. for (i = connecthead; i >= 0; i = connectpoint2[i])
  728. viewBackupPlayerLoc(i);
  729. trProcessBusy(); // process busy triggers
  730. evProcess(gFrameClock); // process event queue
  731. seqProcess(kFrameTicks);
  732. for (i = connecthead; i >= 0; i = connectpoint2[i])
  733. {
  734. powerupProcess(&gPlayer[i]); // process the power-ups
  735. playerMove(&gPlayer[i], &gPlayerInput[i]); // Move player
  736. }
  737. actProcessSprites(); // Actor code: projectiles, etc.
  738. actPostProcess(); // clean up deleted sprites or sprites changing status
  739. sndProcess();
  740. sfxUpdate3DSounds();
  741. gFrame++;
  742. gFrameClock += kFrameTicks;
  743. }
  744. void UpdateNetFifo( void )
  745. {
  746. for ( int i = connecthead; i >= 0; i = connectpoint2[i] )
  747. {
  748. gFifoInput[gNetFifoTail][i] = gNetInput[i];
  749. }
  750. gNetFifoTail = IncRotate(gNetFifoTail, kNetFifoSize);
  751. // CheckMasterSwitch();
  752. }
  753. char packet[576];
  754. inline char GetPacketByte(char * &p)
  755. { return *p++; }
  756. inline PutPacketByte(char * &p, int b)
  757. { *p++ = (char)b; }
  758. inline short GetPacketShort(char * &p)
  759. {
  760. short w = *(short *)p;
  761. p += 2;
  762. return w;
  763. }
  764. inline PutPacketShort(char * &p, int w)
  765. {
  766. *(short *)p = (short)w;
  767. p += 2;
  768. }
  769. inline long GetPacketLong(char * &p)
  770. {
  771. long l = *(long *)p;
  772. p += 4;
  773. return l;
  774. }
  775. inline PutPacketLong(char * &p, int l)
  776. {
  777. *(long *)p = (long)l;
  778. p += 4;
  779. }
  780. int GetPackets( void )
  781. {
  782. short nPlayer, length;
  783. long i, j;
  784. char *p;
  785. int n = 0;
  786. int nMoveCount = 0;
  787. while ( (length = getpacket(&nPlayer, packet)) > 0)
  788. {
  789. n++;
  790. p = packet;
  791. switch( GetPacketByte(p) )
  792. {
  793. case 0: // receive master sync buffer
  794. j = GetPacketShort(p);
  795. if (j != gPacketIndex++)
  796. ThrowError("Missing master packet", ES_ERROR);
  797. for (i = connecthead; i >= 0; i = connectpoint2[i])
  798. {
  799. SYNCFLAGS syncFlags;
  800. syncFlags.byte = GetPacketByte(p);
  801. gNetInput[i].syncFlags.byte = syncFlags.byte;
  802. if (syncFlags.buttonChange)
  803. gNetInput[i].buttonFlags.byte = GetPacketByte(p);
  804. if (syncFlags.keyChange)
  805. gNetInput[i].keyFlags.byte = GetPacketByte(p);
  806. if (syncFlags.weaponChange)
  807. gNetInput[i].newWeapon = GetPacketByte(p);
  808. if (syncFlags.forwardChange)
  809. gNetInput[i].forward = GetPacketByte(p);
  810. if (syncFlags.turnChange)
  811. gNetInput[i].turn = GetPacketShort(p);
  812. if (syncFlags.strafeChange)
  813. gNetInput[i].strafe = GetPacketByte(p);
  814. gNetInput[i].ticks = GetPacketByte(p);
  815. }
  816. while ( p < &packet[length] )
  817. {
  818. gFifoReceiveCRC[gReceiveCRCHead] = GetPacketLong(p);
  819. gReceiveCRCHead = IncRotate(gReceiveCRCHead, kNetFifoSize);
  820. }
  821. if ( gReceiveCRCHead != gCRCTail && gSendCRCHead != gCRCTail )
  822. {
  823. bOutOfSync = FALSE;
  824. do
  825. {
  826. if ( gFifoReceiveCRC[gCRCTail] != gFifoSendCRC[gCRCTail] )
  827. {
  828. dprintf("SYNC: gFifoReceiveCRC[%d] = %8x, gFifoSendCRC[%d] = %8x\n",
  829. gCRCTail, gFifoReceiveCRC[gCRCTail], gCRCTail, gFifoSendCRC[gCRCTail]);
  830. bOutOfSync = TRUE;
  831. }
  832. gCRCTail = IncRotate(gCRCTail, kNetFifoSize);
  833. }
  834. while ( gReceiveCRCHead != gCRCTail && gSendCRCHead != gCRCTail );
  835. }
  836. UpdateNetFifo();
  837. nMoveCount++;
  838. continue;
  839. case 1: // receive slave sync buffer
  840. SYNCFLAGS syncFlags;
  841. syncFlags.byte = GetPacketByte(p);
  842. gNetInput[nPlayer].syncFlags.byte = syncFlags.byte;
  843. if (syncFlags.buttonChange)
  844. gNetInput[nPlayer].buttonFlags.byte = GetPacketByte(p);
  845. if (syncFlags.keyChange)
  846. gNetInput[nPlayer].keyFlags.byte = GetPacketByte(p);
  847. if (syncFlags.weaponChange)
  848. gNetInput[nPlayer].newWeapon = GetPacketByte(p);
  849. if (syncFlags.forwardChange)
  850. gNetInput[nPlayer].forward = GetPacketByte(p);
  851. if (syncFlags.turnChange)
  852. gNetInput[nPlayer].turn = GetPacketShort(p);
  853. if (syncFlags.strafeChange)
  854. gNetInput[nPlayer].strafe = GetPacketByte(p);
  855. gNetInput[nPlayer].ticks = GetPacketByte(p);
  856. continue;
  857. case 250:
  858. playerreadyflag[nPlayer] = GetPacketByte(p);
  859. // if the message came from the master, acknowledge it
  860. if ( nPlayer == connecthead && playerreadyflag[connecthead] == 2 )
  861. sendpacket(connecthead, packet, 2);
  862. break;
  863. case 255: //[255] (logout)
  864. keystatus[1] = 1;
  865. break;
  866. }
  867. break;
  868. }
  869. // try to prevent slave and master from getting into phase lock-step
  870. if ( myconnectindex != connecthead && nMoveCount != 1 )
  871. {
  872. if (nMoveCount == 2)
  873. dprintf("%4d: Received 2 packets from master\n", gPacketIndex);
  874. // if (nMoveCount & 2)
  875. // gNetFifoClock += kFrameTicks / 2;
  876. // else
  877. // gNetFifoClock -= kFrameTicks / 2;
  878. }
  879. return n;
  880. }
  881. void GetInput( void )
  882. {
  883. if ( gNetPlayers > 1 )
  884. GetPackets();
  885. // this doesn't currently do anything but Ken might add to this later
  886. if (getoutputcirclesize() >= 16)
  887. return;
  888. ctrlGetInput();
  889. char *p = packet;
  890. // single player or master
  891. if (gNetPlayers == 1 || myconnectindex == connecthead)
  892. {
  893. // copy in local input
  894. gNetInput[myconnectindex].syncFlags.byte = gInput.syncFlags.byte;
  895. if (gInput.syncFlags.buttonChange)
  896. gNetInput[myconnectindex].buttonFlags.byte = gInput.buttonFlags.byte;
  897. if (gInput.syncFlags.keyChange)
  898. gNetInput[myconnectindex].keyFlags.byte = gInput.keyFlags.byte;
  899. if (gInput.syncFlags.weaponChange)
  900. gNetInput[myconnectindex].newWeapon = gInput.newWeapon;
  901. if (gInput.syncFlags.forwardChange)
  902. gNetInput[myconnectindex].forward = gInput.forward;
  903. if (gInput.syncFlags.turnChange)
  904. gNetInput[myconnectindex].turn = gInput.turn;
  905. if (gInput.syncFlags.strafeChange)
  906. gNetInput[myconnectindex].strafe = gInput.strafe;
  907. gNetInput[myconnectindex].ticks = gInput.ticks;
  908. if ( gNetPlayers > 1 )
  909. {
  910. // build master packet
  911. PutPacketByte(p, 0);
  912. PutPacketShort(p, gPacketIndex++);
  913. for (int i = connecthead; i >= 0; i = connectpoint2[i])
  914. {
  915. PutPacketByte(p, gNetInput[i].syncFlags.byte);
  916. if (gNetInput[i].syncFlags.buttonChange)
  917. PutPacketByte(p, gNetInput[i].buttonFlags.byte);
  918. if (gNetInput[i].syncFlags.keyChange)
  919. PutPacketByte(p, gNetInput[i].keyFlags.byte);
  920. if (gNetInput[i].syncFlags.weaponChange)
  921. PutPacketByte(p, gNetInput[i].newWeapon);
  922. if (gNetInput[i].syncFlags.forwardChange)
  923. PutPacketByte(p, gNetInput[i].forward);
  924. if (gNetInput[i].syncFlags.turnChange)
  925. PutPacketShort(p, gNetInput[i].turn);
  926. if (gNetInput[i].syncFlags.strafeChange)
  927. PutPacketByte(p, gNetInput[i].strafe);
  928. PutPacketByte(p, gNetInput[i].ticks);
  929. gNetInput[i].syncFlags.byte = 0;
  930. }
  931. while ( gCRCTail != gSendCRCHead )
  932. {
  933. PutPacketLong(p, gFifoSendCRC[gCRCTail]);
  934. gCRCTail = IncRotate(gCRCTail, kNetFifoSize); // master increment SEND tail
  935. }
  936. // send packets to slaves
  937. for (i = connectpoint2[connecthead]; i >= 0; i = connectpoint2[i])
  938. sendpacket((short)i, packet, (short)(p - packet));
  939. }
  940. UpdateNetFifo();
  941. }
  942. else //I am a SLAVE
  943. {
  944. // build slave packet
  945. PutPacketByte(p, 1);
  946. PutPacketByte(p, gInput.syncFlags.byte);
  947. if (gInput.syncFlags.buttonChange)
  948. PutPacketByte(p, gInput.buttonFlags.byte);
  949. if (gInput.syncFlags.keyChange)
  950. PutPacketByte(p, gInput.keyFlags.byte);
  951. if (gInput.syncFlags.weaponChange)
  952. PutPacketByte(p, gInput.newWeapon);
  953. if (gInput.syncFlags.forwardChange)
  954. PutPacketByte(p, gInput.forward);
  955. if (gInput.syncFlags.turnChange)
  956. PutPacketShort(p, gInput.turn);
  957. if (gInput.syncFlags.strafeChange)
  958. PutPacketByte(p, gInput.strafe);
  959. PutPacketByte(p, gInput.ticks);
  960. // send packet to master
  961. sendpacket((short)connecthead, packet, (short)(p - packet));
  962. }
  963. }
  964. void faketimerhandler( void )
  965. {
  966. if ( gGameClock >= gNetFifoClock && ready2send )
  967. {
  968. gNetFifoClock += kFrameTicks;
  969. GetInput();
  970. }
  971. }
  972. BOOL WaitForAllPlayers( void )
  973. {
  974. long i, j, oldtotalclock;
  975. if (numplayers < 2) return TRUE;
  976. // if I'm the Master
  977. if (myconnectindex == connecthead)
  978. {
  979. for (j = 1; j <= 2; j++)
  980. {
  981. for (i = connectpoint2[connecthead]; i >= 0; i = connectpoint2[i])
  982. playerreadyflag[i] = 0;
  983. oldtotalclock = gGameClock - 8;
  984. do
  985. {
  986. if (keystatus[1]) return FALSE;
  987. if ( gNetPlayers > 1 )
  988. GetPackets();
  989. if (gGameClock >= oldtotalclock + 8)
  990. {
  991. oldtotalclock = gGameClock;
  992. char *p = packet;
  993. PutPacketByte(p, 250);
  994. PutPacketByte(p, j);
  995. for(i = connectpoint2[connecthead]; i >= 0; i = connectpoint2[i])
  996. if (playerreadyflag[i] != j)
  997. sendpacket((short)i, packet, 2);
  998. }
  999. for(i = connectpoint2[connecthead]; i >= 0; i = connectpoint2[i])
  1000. if (playerreadyflag[i] != j) break;
  1001. } while (i >= 0);
  1002. }
  1003. }
  1004. else
  1005. {
  1006. playerreadyflag[connecthead] = 0;
  1007. while (playerreadyflag[connecthead] != 2)
  1008. {
  1009. if (keystatus[1]) return FALSE;
  1010. if ( gNetPlayers > 1 )
  1011. GetPackets();
  1012. if (playerreadyflag[connecthead] == 1)
  1013. {
  1014. playerreadyflag[connecthead] = 0;
  1015. sendpacket(connecthead, packet, 2);
  1016. }
  1017. }
  1018. }
  1019. return TRUE;
  1020. }
  1021. ErrorResult GameErrorHandler( const Error& error )
  1022. {
  1023. // shut down the game engine
  1024. if ( gNetGame )
  1025. {
  1026. sendlogoff();
  1027. uninitmultiplayers();
  1028. }
  1029. sndTerm();
  1030. timerRemove();
  1031. ctrlTerm();
  1032. UnlockClockStrobe();
  1033. uninitengine();
  1034. setvmode(gOldDisplayMode);
  1035. // chain to the default error handler
  1036. return prevErrorHandler(error);
  1037. };
  1038. enum
  1039. {
  1040. kSwitchNet,
  1041. kSwitchBloodBath,
  1042. kSwitchTeams,
  1043. };
  1044. SWITCH switches[] =
  1045. {
  1046. { "net", kSwitchNet, TRUE },
  1047. { "bloodbath", kSwitchBloodBath, FALSE },
  1048. { "teams", kSwitchBloodBath, FALSE },
  1049. { NULL, 0, FALSE },
  1050. };
  1051. /***********************************************************************
  1052. * Process command line arguments
  1053. **********************************************************************/
  1054. void ParseOptions( void )
  1055. {
  1056. char buffer[256];
  1057. int r;
  1058. while ( (r = GetOptions(switches)) != GO_EOF )
  1059. {
  1060. switch (r)
  1061. {
  1062. case GO_INVALID:
  1063. sprintf(buffer, "Invalid argument: %s", OptArgument);
  1064. ThrowError(buffer, ES_ERROR);
  1065. case kSwitchNet:
  1066. gNetGame = TRUE;
  1067. dprintf("Net parameter is %s\n", OptArgument);
  1068. break;
  1069. case kSwitchBloodBath:
  1070. // NET PARAMETER MUST COME FIRST: Can you add a CheckParm?
  1071. gNetMode = kNetModeBloodBath;
  1072. dprintf("Bloodbath network mode\n");
  1073. break;
  1074. case kSwitchTeams:
  1075. // NET PARAMETER MUST COME FIRST: Can you add a CheckParm?
  1076. gNetMode = kNetModeTeams;
  1077. dprintf("Team network mode\n");
  1078. break;
  1079. case GO_FULL:
  1080. strcpy(gLevelName, OptArgument);
  1081. gForceMap = TRUE;
  1082. break;
  1083. }
  1084. }
  1085. }
  1086. /***********************************************************************
  1087. * main()
  1088. *
  1089. * It all starts here. Have a Bloody day.
  1090. *
  1091. **********************************************************************/
  1092. void main( /* int argc, char *argv[] */ )
  1093. {
  1094. char title[256];
  1095. gOldDisplayMode = getvmode();
  1096. sprintf(title, "BLOOD ALPHA BUILD [%s] -- DO NOT DISTRIBUTE", gBuildDate);
  1097. tioInit();
  1098. tioCenterString(0, 0, tioScreenCols - 1, title, kAttrTitle);
  1099. tioCenterString(tioScreenRows - 1, 0, tioScreenCols - 1,
  1100. "Copyright (c) 1994, 1995 Q Studios Corporation", kAttrTitle);
  1101. tioWindow(1, 0, tioScreenRows - 2, tioScreenCols);
  1102. if ( _grow_handles(kRequiredFiles) < kRequiredFiles )
  1103. {
  1104. tioPrint("Increase FILES=## value in CONFIG.SYS.");
  1105. ThrowError("Not enough file handles available", ES_ERROR);
  1106. }
  1107. ParseOptions();
  1108. tioPrint("Loading INI file");
  1109. optLoadINI();
  1110. tioPrint("Initializing heap and resource system");
  1111. Resource::heap = new QHeap(dpmiDetermineMaxRealAlloc());
  1112. tioPrint("Initializing resource archive");
  1113. gSysRes.Init("BLOOD.RFF", "*.*");
  1114. gGuiRes.Init("GUI.RFF", NULL);
  1115. CheckDemoDate(gSysRes);
  1116. // install our error handler
  1117. prevErrorHandler = errSetHandler(GameErrorHandler);
  1118. tioPrint("Initializing 3D engine");
  1119. InitEngine();
  1120. tioPrint("Creating standard color lookups");
  1121. scrCreateStdColors();
  1122. tioPrint("Loading tiles");
  1123. if (tileInit() == 0)
  1124. ThrowError("ART files not found", ES_ERROR);
  1125. powerupInit(); // calc zoom table for power-up animations
  1126. tioPrint("Loading cosine table");
  1127. trigInit(gSysRes);
  1128. tioPrint("Initializing screen");
  1129. scrInit();
  1130. tioPrint("Initializing view subsystem");
  1131. viewInit();
  1132. tioPrint("Initializing dynamic fire");
  1133. FireInit();
  1134. tioPrint("Initializing weapon animations");
  1135. WeaponInit();
  1136. tioPrint("Installing keyboard handler");
  1137. keyInstall();
  1138. tioPrint("Initializing network users");
  1139. initmultiplayers(0, 0, 0);
  1140. gNetPlayers = numplayers;
  1141. gMe = gView = &gPlayer[myconnectindex];
  1142. gViewIndex = myconnectindex;
  1143. // this must be done before initializing timer, because mouse interrupt hooks timer vector
  1144. tioPrint("Loading control setup");
  1145. ctrlInit();
  1146. tioPrint("Installing timer");
  1147. LockClockStrobe();
  1148. timerRegisterClient(ClockStrobe, kTimerRate);
  1149. timerInstall();
  1150. tioPrint("Initializing sound system");
  1151. sndInit();
  1152. sfxInit();
  1153. tioPrint("Waiting for network players");
  1154. WaitForAllPlayers();
  1155. scrSetGameMode();
  1156. scrSetGamma(gGamma);
  1157. // if ( !gForceMap )
  1158. // credCredits();
  1159. viewResizeView(gViewSize);
  1160. prepareboard();
  1161. gNetFifoClock = gFrameClock = gGameClock = 0;
  1162. gNetFifoHead = gNetFifoTail = 0;
  1163. gSendCRCHead = gReceiveCRCHead = gCRCTail = 0;
  1164. ready2send = TRUE;
  1165. //This is the whole game loop!
  1166. while ( keystatus[KEY_ESC] == 0 && !syncstate )
  1167. {
  1168. LocalKeys();
  1169. if ( gNetPlayers > 1 )
  1170. GetPackets();
  1171. faketimerhandler();
  1172. while ( gNetFifoHead != gNetFifoTail )
  1173. ProcessFrame();
  1174. if ( !gPaused )
  1175. gInterpolate = (gGameClock + kFrameTicks - gFrameClock) * (0x10000 / kFrameTicks);
  1176. viewDrawScreen();
  1177. if (bOutOfSync)
  1178. viewSetMessage("Out of Sync!");
  1179. if ( gEndLevelFlag )
  1180. {
  1181. // fade out the current music
  1182. sndFadeSong(4000);
  1183. if ( gEndLevelFlag < 0 )
  1184. {
  1185. // end episode or something special like that
  1186. }
  1187. else
  1188. gMapId = gEndLevelFlag;
  1189. gEndLevelFlag = 0;
  1190. gForceMap = FALSE;
  1191. ready2send = FALSE;
  1192. prepareboard();
  1193. ready2send = TRUE;
  1194. viewResizeView(gViewSize);
  1195. }
  1196. }
  1197. ready2send = FALSE;
  1198. setvmode(gOldDisplayMode);
  1199. sndTerm();
  1200. dprintf("Removing timer\n");
  1201. timerRemove();
  1202. ctrlTerm();
  1203. UnlockClockStrobe();
  1204. if ( gNetGame )
  1205. {
  1206. sendlogoff();
  1207. uninitmultiplayers();
  1208. }
  1209. dprintf("uninitengine()\n");
  1210. uninitengine();
  1211. BloodINI.Save();
  1212. if (syncstate)
  1213. printf("A packet was lost! (syncstate)\n");
  1214. dprintf("All subsystems shut down. Processing exit functions\n");
  1215. errSetHandler(prevErrorHandler);
  1216. }