P_ANIM.C 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. //**************************************************************************
  2. //**
  3. //** p_anim.c : Heretic 2 : Raven Software, Corp.
  4. //**
  5. //** $RCSfile: p_anim.c,v $
  6. //** $Revision: 1.9 $
  7. //** $Date: 95/07/17 18:50:37 $
  8. //** $Author: bgokey $
  9. //**
  10. //**************************************************************************
  11. // HEADER FILES ------------------------------------------------------------
  12. #include "h2def.h"
  13. #include "p_local.h"
  14. // MACROS ------------------------------------------------------------------
  15. #define ANIM_SCRIPT_NAME "ANIMDEFS"
  16. #define MAX_ANIM_DEFS 20
  17. #define MAX_FRAME_DEFS 96
  18. #define ANIM_FLAT 0
  19. #define ANIM_TEXTURE 1
  20. #define SCI_FLAT "flat"
  21. #define SCI_TEXTURE "texture"
  22. #define SCI_PIC "pic"
  23. #define SCI_TICS "tics"
  24. #define SCI_RAND "rand"
  25. #define LIGHTNING_SPECIAL 198
  26. #define LIGHTNING_SPECIAL2 199
  27. #define SKYCHANGE_SPECIAL 200
  28. // TYPES -------------------------------------------------------------------
  29. typedef struct
  30. {
  31. int index;
  32. int tics;
  33. } frameDef_t;
  34. typedef struct
  35. {
  36. int type;
  37. int index;
  38. int tics;
  39. int currentFrameDef;
  40. int startFrameDef;
  41. int endFrameDef;
  42. } animDef_t;
  43. // EXTERNAL FUNCTION PROTOTYPES --------------------------------------------
  44. // PUBLIC FUNCTION PROTOTYPES ----------------------------------------------
  45. // PRIVATE FUNCTION PROTOTYPES ---------------------------------------------
  46. static void P_LightningFlash(void);
  47. // EXTERNAL DATA DECLARATIONS ----------------------------------------------
  48. extern fixed_t Sky1ColumnOffset;
  49. extern fixed_t Sky2ColumnOffset;
  50. extern int Sky1Texture;
  51. extern boolean DoubleSky;
  52. // PUBLIC DATA DEFINITIONS -------------------------------------------------
  53. fixed_t Sky1ScrollDelta;
  54. fixed_t Sky2ScrollDelta;
  55. // PRIVATE DATA DEFINITIONS ------------------------------------------------
  56. static animDef_t AnimDefs[MAX_ANIM_DEFS];
  57. static frameDef_t FrameDefs[MAX_FRAME_DEFS];
  58. static int AnimDefCount;
  59. static boolean LevelHasLightning;
  60. static int NextLightningFlash;
  61. static int LightningFlash;
  62. static int *LightningLightLevels;
  63. // CODE --------------------------------------------------------------------
  64. //==========================================================================
  65. //
  66. // P_AnimateSurfaces
  67. //
  68. //==========================================================================
  69. void P_AnimateSurfaces(void)
  70. {
  71. int i;
  72. animDef_t *ad;
  73. line_t *line;
  74. // Animate flats and textures
  75. for(i = 0; i < AnimDefCount; i++)
  76. {
  77. ad = &AnimDefs[i];
  78. ad->tics--;
  79. if(ad->tics == 0)
  80. {
  81. if(ad->currentFrameDef == ad->endFrameDef)
  82. {
  83. ad->currentFrameDef = ad->startFrameDef;
  84. }
  85. else
  86. {
  87. ad->currentFrameDef++;
  88. }
  89. ad->tics = FrameDefs[ad->currentFrameDef].tics;
  90. if(ad->tics > 255)
  91. { // Random tics
  92. ad->tics = (ad->tics>>16)
  93. +P_Random()%((ad->tics&0xff00)>>8);
  94. }
  95. if(ad->type == ANIM_FLAT)
  96. {
  97. flattranslation[ad->index] =
  98. FrameDefs[ad->currentFrameDef].index;
  99. }
  100. else
  101. { // Texture
  102. texturetranslation[ad->index] =
  103. FrameDefs[ad->currentFrameDef].index;
  104. }
  105. }
  106. }
  107. // Update scrolling textures
  108. for(i = 0; i < numlinespecials; i++)
  109. {
  110. line = linespeciallist[i];
  111. switch(line->special)
  112. {
  113. case 100: // Scroll_Texture_Left
  114. sides[line->sidenum[0]].textureoffset += line->arg1<<10;
  115. break;
  116. case 101: // Scroll_Texture_Right
  117. sides[line->sidenum[0]].textureoffset -= line->arg1<<10;
  118. break;
  119. case 102: // Scroll_Texture_Up
  120. sides[line->sidenum[0]].rowoffset += line->arg1<<10;
  121. break;
  122. case 103: // Scroll_Texture_Down
  123. sides[line->sidenum[0]].rowoffset -= line->arg1<<10;
  124. break;
  125. }
  126. }
  127. // Update sky column offsets
  128. Sky1ColumnOffset += Sky1ScrollDelta;
  129. Sky2ColumnOffset += Sky2ScrollDelta;
  130. if(LevelHasLightning)
  131. {
  132. if(!NextLightningFlash || LightningFlash)
  133. {
  134. P_LightningFlash();
  135. }
  136. else
  137. {
  138. NextLightningFlash--;
  139. }
  140. }
  141. }
  142. //==========================================================================
  143. //
  144. // P_LightningFlash
  145. //
  146. //==========================================================================
  147. static void P_LightningFlash(void)
  148. {
  149. int i;
  150. sector_t *tempSec;
  151. int *tempLight;
  152. boolean foundSec;
  153. int flashLight;
  154. if(LightningFlash)
  155. {
  156. LightningFlash--;
  157. if(LightningFlash)
  158. {
  159. tempLight = LightningLightLevels;
  160. tempSec = sectors;
  161. for(i = 0; i < numsectors; i++, tempSec++)
  162. {
  163. if(tempSec->ceilingpic == skyflatnum
  164. || tempSec->special == LIGHTNING_SPECIAL
  165. || tempSec->special == LIGHTNING_SPECIAL2)
  166. {
  167. if(*tempLight < tempSec->lightlevel-4)
  168. {
  169. tempSec->lightlevel -= 4;
  170. }
  171. tempLight++;
  172. }
  173. }
  174. }
  175. else
  176. { // remove the alternate lightning flash special
  177. tempLight = LightningLightLevels;
  178. tempSec = sectors;
  179. for(i = 0; i < numsectors; i++, tempSec++)
  180. {
  181. if(tempSec->ceilingpic == skyflatnum
  182. || tempSec->special == LIGHTNING_SPECIAL
  183. || tempSec->special == LIGHTNING_SPECIAL2)
  184. {
  185. tempSec->lightlevel = *tempLight;
  186. tempLight++;
  187. }
  188. }
  189. Sky1Texture = P_GetMapSky1Texture(gamemap);
  190. }
  191. return;
  192. }
  193. LightningFlash = (P_Random()&7)+8;
  194. flashLight = 200+(P_Random()&31);
  195. tempSec = sectors;
  196. tempLight = LightningLightLevels;
  197. foundSec = false;
  198. for(i = 0; i < numsectors; i++, tempSec++)
  199. {
  200. if(tempSec->ceilingpic == skyflatnum
  201. || tempSec->special == LIGHTNING_SPECIAL
  202. || tempSec->special == LIGHTNING_SPECIAL2)
  203. {
  204. *tempLight = tempSec->lightlevel;
  205. if(tempSec->special == LIGHTNING_SPECIAL)
  206. {
  207. tempSec->lightlevel += 64;
  208. if(tempSec->lightlevel > flashLight)
  209. {
  210. tempSec->lightlevel = flashLight;
  211. }
  212. }
  213. else if(tempSec->special == LIGHTNING_SPECIAL2)
  214. {
  215. tempSec->lightlevel += 32;
  216. if(tempSec->lightlevel > flashLight)
  217. {
  218. tempSec->lightlevel = flashLight;
  219. }
  220. }
  221. else
  222. {
  223. tempSec->lightlevel = flashLight;
  224. }
  225. if(tempSec->lightlevel < *tempLight)
  226. {
  227. tempSec->lightlevel = *tempLight;
  228. }
  229. tempLight++;
  230. foundSec = true;
  231. }
  232. }
  233. if(foundSec)
  234. {
  235. Sky1Texture = P_GetMapSky2Texture(gamemap); // set alternate sky
  236. S_StartSound(NULL, SFX_THUNDER_CRASH);
  237. }
  238. // Calculate the next lighting flash
  239. if(!NextLightningFlash)
  240. {
  241. if(P_Random() < 50)
  242. { // Immediate Quick flash
  243. NextLightningFlash = (P_Random()&15)+16;
  244. }
  245. else
  246. {
  247. if(P_Random() < 128 && !(leveltime&32))
  248. {
  249. NextLightningFlash = ((P_Random()&7)+2)*35;
  250. }
  251. else
  252. {
  253. NextLightningFlash = ((P_Random()&15)+5)*35;
  254. }
  255. }
  256. }
  257. }
  258. //==========================================================================
  259. //
  260. // P_ForceLightning
  261. //
  262. //==========================================================================
  263. void P_ForceLightning(void)
  264. {
  265. NextLightningFlash = 0;
  266. }
  267. //==========================================================================
  268. //
  269. // P_InitLightning
  270. //
  271. //==========================================================================
  272. void P_InitLightning(void)
  273. {
  274. int i;
  275. int secCount;
  276. if(!P_GetMapLightning(gamemap))
  277. {
  278. LevelHasLightning = false;
  279. LightningFlash = 0;
  280. return;
  281. }
  282. LightningFlash = 0;
  283. secCount = 0;
  284. for(i = 0; i < numsectors; i++)
  285. {
  286. if(sectors[i].ceilingpic == skyflatnum
  287. || sectors[i].special == LIGHTNING_SPECIAL
  288. || sectors[i].special == LIGHTNING_SPECIAL2)
  289. {
  290. secCount++;
  291. }
  292. }
  293. if(secCount)
  294. {
  295. LevelHasLightning = true;
  296. }
  297. else
  298. {
  299. LevelHasLightning = false;
  300. return;
  301. }
  302. LightningLightLevels = (int *)Z_Malloc(secCount*sizeof(int), PU_LEVEL,
  303. NULL);
  304. NextLightningFlash = ((P_Random()&15)+5)*35; // don't flash at level start
  305. }
  306. //==========================================================================
  307. //
  308. // P_InitFTAnims
  309. //
  310. // Initialize flat and texture animation lists.
  311. //
  312. //==========================================================================
  313. void P_InitFTAnims(void)
  314. {
  315. int base;
  316. int mod;
  317. int fd;
  318. animDef_t *ad;
  319. boolean ignore;
  320. boolean done;
  321. fd = 0;
  322. ad = AnimDefs;
  323. AnimDefCount = 0;
  324. SC_Open(ANIM_SCRIPT_NAME);
  325. while(SC_GetString())
  326. {
  327. if(AnimDefCount == MAX_ANIM_DEFS)
  328. {
  329. I_Error("P_InitFTAnims: too many AnimDefs.");
  330. }
  331. if(SC_Compare(SCI_FLAT))
  332. {
  333. ad->type = ANIM_FLAT;
  334. }
  335. else if(SC_Compare(SCI_TEXTURE))
  336. {
  337. ad->type = ANIM_TEXTURE;
  338. }
  339. else
  340. {
  341. SC_ScriptError(NULL);
  342. }
  343. SC_MustGetString(); // Name
  344. ignore = false;
  345. if(ad->type == ANIM_FLAT)
  346. {
  347. if(W_CheckNumForName(sc_String) == -1)
  348. {
  349. ignore = true;
  350. }
  351. else
  352. {
  353. ad->index = R_FlatNumForName(sc_String);
  354. }
  355. }
  356. else
  357. { // Texture
  358. if(R_CheckTextureNumForName(sc_String) == -1)
  359. {
  360. ignore = true;
  361. }
  362. else
  363. {
  364. ad->index = R_TextureNumForName(sc_String);
  365. }
  366. }
  367. ad->startFrameDef = fd;
  368. done = false;
  369. while(done == false)
  370. {
  371. if(SC_GetString())
  372. {
  373. if(SC_Compare(SCI_PIC))
  374. {
  375. if(fd == MAX_FRAME_DEFS)
  376. {
  377. I_Error("P_InitFTAnims: too many FrameDefs.");
  378. }
  379. SC_MustGetNumber();
  380. if(ignore == false)
  381. {
  382. FrameDefs[fd].index = ad->index+sc_Number-1;
  383. }
  384. SC_MustGetString();
  385. if(SC_Compare(SCI_TICS))
  386. {
  387. SC_MustGetNumber();
  388. if(ignore == false)
  389. {
  390. FrameDefs[fd].tics = sc_Number;
  391. fd++;
  392. }
  393. }
  394. else if(SC_Compare(SCI_RAND))
  395. {
  396. SC_MustGetNumber();
  397. base = sc_Number;
  398. SC_MustGetNumber();
  399. if(ignore == false)
  400. {
  401. mod = sc_Number-base+1;
  402. FrameDefs[fd].tics = (base<<16)+(mod<<8);
  403. fd++;
  404. }
  405. }
  406. else
  407. {
  408. SC_ScriptError(NULL);
  409. }
  410. }
  411. else
  412. {
  413. SC_UnGet();
  414. done = true;
  415. }
  416. }
  417. else
  418. {
  419. done = true;
  420. }
  421. }
  422. if((ignore == false) && (fd-ad->startFrameDef < 2))
  423. {
  424. I_Error("P_InitFTAnims: AnimDef has framecount < 2.");
  425. }
  426. if(ignore == false)
  427. {
  428. ad->endFrameDef = fd-1;
  429. ad->currentFrameDef = ad->endFrameDef;
  430. ad->tics = 1; // Force 1st game tic to animate
  431. AnimDefCount++;
  432. ad++;
  433. }
  434. }
  435. SC_Close();
  436. }