p_lights.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. // Emacs style mode select -*- C++ -*-
  2. //-----------------------------------------------------------------------------
  3. //
  4. // $Id:$
  5. //
  6. // Copyright (C) 1993-1996 by id Software, Inc.
  7. //
  8. // This source is available for distribution and/or modification
  9. // only under the terms of the DOOM Source Code License as
  10. // published by id Software. All rights reserved.
  11. //
  12. // The source is distributed in the hope that it will be useful,
  13. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. // FITNESS FOR A PARTICULAR PURPOSE. See the DOOM Source Code License
  15. // for more details.
  16. //
  17. // $Log:$
  18. //
  19. // DESCRIPTION:
  20. // Handle Sector base lighting effects.
  21. // Muzzle flash?
  22. //
  23. //-----------------------------------------------------------------------------
  24. static const char
  25. rcsid[] = "$Id: p_lights.c,v 1.5 1997/02/03 22:45:11 b1 Exp $";
  26. #include "z_zone.h"
  27. #include "m_random.h"
  28. #include "doomdef.h"
  29. #include "p_local.h"
  30. // State.
  31. #include "r_state.h"
  32. //
  33. // FIRELIGHT FLICKER
  34. //
  35. //
  36. // T_FireFlicker
  37. //
  38. void T_FireFlicker (fireflicker_t* flick)
  39. {
  40. int amount;
  41. if (--flick->count)
  42. return;
  43. amount = (P_Random()&3)*16;
  44. if (flick->sector->lightlevel - amount < flick->minlight)
  45. flick->sector->lightlevel = flick->minlight;
  46. else
  47. flick->sector->lightlevel = flick->maxlight - amount;
  48. flick->count = 4;
  49. }
  50. //
  51. // P_SpawnFireFlicker
  52. //
  53. void P_SpawnFireFlicker (sector_t* sector)
  54. {
  55. fireflicker_t* flick;
  56. // Note that we are resetting sector attributes.
  57. // Nothing special about it during gameplay.
  58. sector->special = 0;
  59. flick = Z_Malloc ( sizeof(*flick), PU_LEVSPEC, 0);
  60. P_AddThinker (&flick->thinker);
  61. flick->thinker.function.acp1 = (actionf_p1) T_FireFlicker;
  62. flick->sector = sector;
  63. flick->maxlight = sector->lightlevel;
  64. flick->minlight = P_FindMinSurroundingLight(sector,sector->lightlevel)+16;
  65. flick->count = 4;
  66. }
  67. //
  68. // BROKEN LIGHT FLASHING
  69. //
  70. //
  71. // T_LightFlash
  72. // Do flashing lights.
  73. //
  74. void T_LightFlash (lightflash_t* flash)
  75. {
  76. if (--flash->count)
  77. return;
  78. if (flash->sector->lightlevel == flash->maxlight)
  79. {
  80. flash-> sector->lightlevel = flash->minlight;
  81. flash->count = (P_Random()&flash->mintime)+1;
  82. }
  83. else
  84. {
  85. flash-> sector->lightlevel = flash->maxlight;
  86. flash->count = (P_Random()&flash->maxtime)+1;
  87. }
  88. }
  89. //
  90. // P_SpawnLightFlash
  91. // After the map has been loaded, scan each sector
  92. // for specials that spawn thinkers
  93. //
  94. void P_SpawnLightFlash (sector_t* sector)
  95. {
  96. lightflash_t* flash;
  97. // nothing special about it during gameplay
  98. sector->special = 0;
  99. flash = Z_Malloc ( sizeof(*flash), PU_LEVSPEC, 0);
  100. P_AddThinker (&flash->thinker);
  101. flash->thinker.function.acp1 = (actionf_p1) T_LightFlash;
  102. flash->sector = sector;
  103. flash->maxlight = sector->lightlevel;
  104. flash->minlight = P_FindMinSurroundingLight(sector,sector->lightlevel);
  105. flash->maxtime = 64;
  106. flash->mintime = 7;
  107. flash->count = (P_Random()&flash->maxtime)+1;
  108. }
  109. //
  110. // STROBE LIGHT FLASHING
  111. //
  112. //
  113. // T_StrobeFlash
  114. //
  115. void T_StrobeFlash (strobe_t* flash)
  116. {
  117. if (--flash->count)
  118. return;
  119. if (flash->sector->lightlevel == flash->minlight)
  120. {
  121. flash-> sector->lightlevel = flash->maxlight;
  122. flash->count = flash->brighttime;
  123. }
  124. else
  125. {
  126. flash-> sector->lightlevel = flash->minlight;
  127. flash->count =flash->darktime;
  128. }
  129. }
  130. //
  131. // P_SpawnStrobeFlash
  132. // After the map has been loaded, scan each sector
  133. // for specials that spawn thinkers
  134. //
  135. void
  136. P_SpawnStrobeFlash
  137. ( sector_t* sector,
  138. int fastOrSlow,
  139. int inSync )
  140. {
  141. strobe_t* flash;
  142. flash = Z_Malloc ( sizeof(*flash), PU_LEVSPEC, 0);
  143. P_AddThinker (&flash->thinker);
  144. flash->sector = sector;
  145. flash->darktime = fastOrSlow;
  146. flash->brighttime = STROBEBRIGHT;
  147. flash->thinker.function.acp1 = (actionf_p1) T_StrobeFlash;
  148. flash->maxlight = sector->lightlevel;
  149. flash->minlight = P_FindMinSurroundingLight(sector, sector->lightlevel);
  150. if (flash->minlight == flash->maxlight)
  151. flash->minlight = 0;
  152. // nothing special about it during gameplay
  153. sector->special = 0;
  154. if (!inSync)
  155. flash->count = (P_Random()&7)+1;
  156. else
  157. flash->count = 1;
  158. }
  159. //
  160. // Start strobing lights (usually from a trigger)
  161. //
  162. void EV_StartLightStrobing(line_t* line)
  163. {
  164. int secnum;
  165. sector_t* sec;
  166. secnum = -1;
  167. while ((secnum = P_FindSectorFromLineTag(line,secnum)) >= 0)
  168. {
  169. sec = &sectors[secnum];
  170. if (sec->specialdata)
  171. continue;
  172. P_SpawnStrobeFlash (sec,SLOWDARK, 0);
  173. }
  174. }
  175. //
  176. // TURN LINE'S TAG LIGHTS OFF
  177. //
  178. void EV_TurnTagLightsOff(line_t* line)
  179. {
  180. int i;
  181. int j;
  182. int min;
  183. sector_t* sector;
  184. sector_t* tsec;
  185. line_t* templine;
  186. sector = sectors;
  187. for (j = 0;j < numsectors; j++, sector++)
  188. {
  189. if (sector->tag == line->tag)
  190. {
  191. min = sector->lightlevel;
  192. for (i = 0;i < sector->linecount; i++)
  193. {
  194. templine = sector->lines[i];
  195. tsec = getNextSector(templine,sector);
  196. if (!tsec)
  197. continue;
  198. if (tsec->lightlevel < min)
  199. min = tsec->lightlevel;
  200. }
  201. sector->lightlevel = min;
  202. }
  203. }
  204. }
  205. //
  206. // TURN LINE'S TAG LIGHTS ON
  207. //
  208. void
  209. EV_LightTurnOn
  210. ( line_t* line,
  211. int bright )
  212. {
  213. int i;
  214. int j;
  215. sector_t* sector;
  216. sector_t* temp;
  217. line_t* templine;
  218. sector = sectors;
  219. for (i=0;i<numsectors;i++, sector++)
  220. {
  221. if (sector->tag == line->tag)
  222. {
  223. // bright = 0 means to search
  224. // for highest light level
  225. // surrounding sector
  226. if (!bright)
  227. {
  228. for (j = 0;j < sector->linecount; j++)
  229. {
  230. templine = sector->lines[j];
  231. temp = getNextSector(templine,sector);
  232. if (!temp)
  233. continue;
  234. if (temp->lightlevel > bright)
  235. bright = temp->lightlevel;
  236. }
  237. }
  238. sector-> lightlevel = bright;
  239. }
  240. }
  241. }
  242. //
  243. // Spawn glowing light
  244. //
  245. void T_Glow(glow_t* g)
  246. {
  247. switch(g->direction)
  248. {
  249. case -1:
  250. // DOWN
  251. g->sector->lightlevel -= GLOWSPEED;
  252. if (g->sector->lightlevel <= g->minlight)
  253. {
  254. g->sector->lightlevel += GLOWSPEED;
  255. g->direction = 1;
  256. }
  257. break;
  258. case 1:
  259. // UP
  260. g->sector->lightlevel += GLOWSPEED;
  261. if (g->sector->lightlevel >= g->maxlight)
  262. {
  263. g->sector->lightlevel -= GLOWSPEED;
  264. g->direction = -1;
  265. }
  266. break;
  267. }
  268. }
  269. void P_SpawnGlowingLight(sector_t* sector)
  270. {
  271. glow_t* g;
  272. g = Z_Malloc( sizeof(*g), PU_LEVSPEC, 0);
  273. P_AddThinker(&g->thinker);
  274. g->sector = sector;
  275. g->minlight = P_FindMinSurroundingLight(sector,sector->lightlevel);
  276. g->maxlight = sector->lightlevel;
  277. g->thinker.function.acp1 = (actionf_p1) T_Glow;
  278. g->direction = -1;
  279. sector->special = 0;
  280. }