p_lights.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. /* Emacs style mode select -*- C++ -*-
  2. *-----------------------------------------------------------------------------
  3. *
  4. *
  5. * PrBoom: a Doom port merged with LxDoom and LSDLDoom
  6. * based on BOOM, a modified and improved DOOM engine
  7. * Copyright (C) 1999 by
  8. * id Software, Chi Hoang, Lee Killough, Jim Flynn, Rand Phares, Ty Halderman
  9. * Copyright (C) 1999-2000 by
  10. * Jess Haas, Nicolas Kalkhof, Colin Phipps, Florian Schulze
  11. * Copyright 2005, 2006 by
  12. * Florian Schulze, Colin Phipps, Neil Stevens, Andrey Budko
  13. *
  14. * This program is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU General Public License
  16. * as published by the Free Software Foundation; either version 2
  17. * of the License, or (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software
  26. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  27. * 02111-1307, USA.
  28. *
  29. * DESCRIPTION:
  30. * Action routines for lighting thinkers
  31. * Spawn sector based lighting effects.
  32. * Handle lighting linedef types
  33. *
  34. *-----------------------------------------------------------------------------*/
  35. #include "doomstat.h" //jff 5/18/98
  36. #include "doomdef.h"
  37. #include "m_random.h"
  38. #include "r_main.h"
  39. #include "p_spec.h"
  40. #include "p_tick.h"
  41. //////////////////////////////////////////////////////////
  42. //
  43. // Lighting action routines, called once per tick
  44. //
  45. //////////////////////////////////////////////////////////
  46. //
  47. // T_FireFlicker()
  48. //
  49. // Firelight flicker action routine, called once per tick
  50. //
  51. // Passed a fireflicker_t structure containing light levels and timing
  52. // Returns nothing
  53. //
  54. void T_FireFlicker (fireflicker_t* flick)
  55. {
  56. int amount;
  57. if (--flick->count)
  58. return;
  59. amount = (P_Random(pr_lights)&3)*16;
  60. if (flick->sector->lightlevel - amount < flick->minlight)
  61. flick->sector->lightlevel = flick->minlight;
  62. else
  63. flick->sector->lightlevel = flick->maxlight - amount;
  64. flick->count = 4;
  65. }
  66. //
  67. // T_LightFlash()
  68. //
  69. // Broken light flashing action routine, called once per tick
  70. //
  71. // Passed a lightflash_t structure containing light levels and timing
  72. // Returns nothing
  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(pr_lights)&flash->mintime)+1;
  82. }
  83. else
  84. {
  85. flash-> sector->lightlevel = flash->maxlight;
  86. flash->count = (P_Random(pr_lights)&flash->maxtime)+1;
  87. }
  88. }
  89. //
  90. // T_StrobeFlash()
  91. //
  92. // Strobe light flashing action routine, called once per tick
  93. //
  94. // Passed a strobe_t structure containing light levels and timing
  95. // Returns nothing
  96. //
  97. void T_StrobeFlash (strobe_t* flash)
  98. {
  99. if (--flash->count)
  100. return;
  101. if (flash->sector->lightlevel == flash->minlight)
  102. {
  103. flash-> sector->lightlevel = flash->maxlight;
  104. flash->count = flash->brighttime;
  105. }
  106. else
  107. {
  108. flash-> sector->lightlevel = flash->minlight;
  109. flash->count =flash->darktime;
  110. }
  111. }
  112. //
  113. // T_Glow()
  114. //
  115. // Glowing light action routine, called once per tick
  116. //
  117. // Passed a glow_t structure containing light levels and timing
  118. // Returns nothing
  119. //
  120. void T_Glow(glow_t* g)
  121. {
  122. switch(g->direction)
  123. {
  124. case -1:
  125. // light dims
  126. g->sector->lightlevel -= GLOWSPEED;
  127. if (g->sector->lightlevel <= g->minlight)
  128. {
  129. g->sector->lightlevel += GLOWSPEED;
  130. g->direction = 1;
  131. }
  132. break;
  133. case 1:
  134. // light brightens
  135. g->sector->lightlevel += GLOWSPEED;
  136. if (g->sector->lightlevel >= g->maxlight)
  137. {
  138. g->sector->lightlevel -= GLOWSPEED;
  139. g->direction = -1;
  140. }
  141. break;
  142. }
  143. }
  144. //////////////////////////////////////////////////////////
  145. //
  146. // Sector lighting type spawners
  147. //
  148. // After the map has been loaded, each sector is scanned
  149. // for specials that spawn thinkers
  150. //
  151. //////////////////////////////////////////////////////////
  152. //
  153. // P_SpawnFireFlicker()
  154. //
  155. // Spawns a fire flicker lighting thinker
  156. //
  157. // Passed the sector that spawned the thinker
  158. // Returns nothing
  159. //
  160. void P_SpawnFireFlicker (sector_t* sector)
  161. {
  162. fireflicker_t* flick;
  163. // Note that we are resetting sector attributes.
  164. // Nothing special about it during gameplay.
  165. sector->special &= ~31; //jff 3/14/98 clear non-generalized sector type
  166. flick = Z_Malloc ( sizeof(*flick), PU_LEVSPEC, 0);
  167. memset(flick, 0, sizeof(*flick));
  168. P_AddThinker (&flick->thinker);
  169. flick->thinker.function = T_FireFlicker;
  170. flick->sector = sector;
  171. flick->maxlight = sector->lightlevel;
  172. flick->minlight = P_FindMinSurroundingLight(sector,sector->lightlevel)+16;
  173. flick->count = 4;
  174. }
  175. //
  176. // P_SpawnLightFlash()
  177. //
  178. // Spawns a broken light flash lighting thinker
  179. //
  180. // Passed the sector that spawned the thinker
  181. // Returns nothing
  182. //
  183. void P_SpawnLightFlash (sector_t* sector)
  184. {
  185. lightflash_t* flash;
  186. // nothing special about it during gameplay
  187. sector->special &= ~31; //jff 3/14/98 clear non-generalized sector type
  188. flash = Z_Malloc ( sizeof(*flash), PU_LEVSPEC, 0);
  189. memset(flash, 0, sizeof(*flash));
  190. P_AddThinker (&flash->thinker);
  191. flash->thinker.function = T_LightFlash;
  192. flash->sector = sector;
  193. flash->maxlight = sector->lightlevel;
  194. flash->minlight = P_FindMinSurroundingLight(sector,sector->lightlevel);
  195. flash->maxtime = 64;
  196. flash->mintime = 7;
  197. flash->count = (P_Random(pr_lights)&flash->maxtime)+1;
  198. }
  199. //
  200. // P_SpawnStrobeFlash
  201. //
  202. // Spawns a blinking light thinker
  203. //
  204. // Passed the sector that spawned the thinker, speed of blinking
  205. // and whether blinking is to by syncrhonous with other sectors
  206. //
  207. // Returns nothing
  208. //
  209. void P_SpawnStrobeFlash
  210. ( sector_t* sector,
  211. int fastOrSlow,
  212. int inSync )
  213. {
  214. strobe_t* flash;
  215. flash = Z_Malloc ( sizeof(*flash), PU_LEVSPEC, 0);
  216. memset(flash, 0, sizeof(*flash));
  217. P_AddThinker (&flash->thinker);
  218. flash->sector = sector;
  219. flash->darktime = fastOrSlow;
  220. flash->brighttime = STROBEBRIGHT;
  221. flash->thinker.function = T_StrobeFlash;
  222. flash->maxlight = sector->lightlevel;
  223. flash->minlight = P_FindMinSurroundingLight(sector, sector->lightlevel);
  224. if (flash->minlight == flash->maxlight)
  225. flash->minlight = 0;
  226. // nothing special about it during gameplay
  227. sector->special &= ~31; //jff 3/14/98 clear non-generalized sector type
  228. if (!inSync)
  229. flash->count = (P_Random(pr_lights)&7)+1;
  230. else
  231. flash->count = 1;
  232. }
  233. //
  234. // P_SpawnGlowingLight()
  235. //
  236. // Spawns a glowing light (smooth oscillation from min to max) thinker
  237. //
  238. // Passed the sector that spawned the thinker
  239. // Returns nothing
  240. //
  241. void P_SpawnGlowingLight(sector_t* sector)
  242. {
  243. glow_t* g;
  244. g = Z_Malloc( sizeof(*g), PU_LEVSPEC, 0);
  245. memset(g, 0, sizeof(*g));
  246. P_AddThinker(&g->thinker);
  247. g->sector = sector;
  248. g->minlight = P_FindMinSurroundingLight(sector,sector->lightlevel);
  249. g->maxlight = sector->lightlevel;
  250. g->thinker.function = T_Glow;
  251. g->direction = -1;
  252. sector->special &= ~31; //jff 3/14/98 clear non-generalized sector type
  253. }
  254. //////////////////////////////////////////////////////////
  255. //
  256. // Linedef lighting function handlers
  257. //
  258. //////////////////////////////////////////////////////////
  259. //
  260. // EV_StartLightStrobing()
  261. //
  262. // Start strobing lights (usually from a trigger)
  263. //
  264. // Passed the line that activated the strobing
  265. // Returns true
  266. //
  267. // jff 2/12/98 added int return value, fixed return
  268. //
  269. int EV_StartLightStrobing(line_t* line)
  270. {
  271. int secnum;
  272. sector_t* sec;
  273. secnum = -1;
  274. // start lights strobing in all sectors tagged same as line
  275. while ((secnum = P_FindSectorFromLineTag(line,secnum)) >= 0)
  276. {
  277. sec = &sectors[secnum];
  278. // if already doing a lighting function, don't start a second
  279. if (P_SectorActive(lighting_special,sec)) //jff 2/22/98
  280. continue;
  281. P_SpawnStrobeFlash (sec,SLOWDARK, 0);
  282. }
  283. return 1;
  284. }
  285. //
  286. // EV_TurnTagLightsOff()
  287. //
  288. // Turn line's tagged sector's lights to min adjacent neighbor level
  289. //
  290. // Passed the line that activated the lights being turned off
  291. // Returns true
  292. //
  293. // jff 2/12/98 added int return value, fixed return
  294. //
  295. int EV_TurnTagLightsOff(line_t* line)
  296. {
  297. int j;
  298. // search sectors for those with same tag as activating line
  299. // killough 10/98: replaced inefficient search with fast search
  300. for (j = -1; (j = P_FindSectorFromLineTag(line,j)) >= 0;)
  301. {
  302. sector_t *sector = sectors + j, *tsec;
  303. int i, min = sector->lightlevel;
  304. // find min neighbor light level
  305. for (i = 0;i < sector->linecount; i++)
  306. if ((tsec = getNextSector(sector->lines[i], sector)) &&
  307. tsec->lightlevel < min)
  308. min = tsec->lightlevel;
  309. sector->lightlevel = min;
  310. }
  311. return 1;
  312. }
  313. //
  314. // EV_LightTurnOn()
  315. //
  316. // Turn sectors tagged to line lights on to specified or max neighbor level
  317. //
  318. // Passed the activating line, and a level to set the light to
  319. // If level passed is 0, the maximum neighbor lighting is used
  320. // Returns true
  321. //
  322. // jff 2/12/98 added int return value, fixed return
  323. //
  324. int EV_LightTurnOn(line_t *line, int bright)
  325. {
  326. int i;
  327. // search all sectors for ones with same tag as activating line
  328. // killough 10/98: replace inefficient search with fast search
  329. for (i = -1; (i = P_FindSectorFromLineTag(line,i)) >= 0;)
  330. {
  331. sector_t *temp, *sector = sectors+i;
  332. int j, tbright = bright; //jff 5/17/98 search for maximum PER sector
  333. // bright = 0 means to search for highest light level surrounding sector
  334. if (!bright)
  335. for (j = 0;j < sector->linecount; j++)
  336. if ((temp = getNextSector(sector->lines[j],sector)) &&
  337. temp->lightlevel > tbright)
  338. tbright = temp->lightlevel;
  339. sector->lightlevel = tbright;
  340. //jff 5/17/98 unless compatibility optioned
  341. //then maximum near ANY tagged sector
  342. if (comp[comp_model])
  343. bright = tbright;
  344. }
  345. return 1;
  346. }
  347. /* killough 10/98:
  348. *
  349. * EV_LightTurnOnPartway()
  350. *
  351. * Turn sectors tagged to line lights on to specified or max neighbor level
  352. *
  353. * Passed the activating line, and a light level fraction between 0 and 1.
  354. * Sets the light to min on 0, max on 1, and interpolates in-between.
  355. * Used for doors with gradual lighting effects.
  356. *
  357. * Returns true
  358. */
  359. int EV_LightTurnOnPartway(line_t *line, fixed_t level)
  360. {
  361. int i;
  362. if (level < 0) // clip at extremes
  363. level = 0;
  364. if (level > FRACUNIT)
  365. level = FRACUNIT;
  366. // search all sectors for ones with same tag as activating line
  367. for (i = -1; (i = P_FindSectorFromLineTag(line,i)) >= 0;)
  368. {
  369. sector_t *temp, *sector = sectors+i;
  370. int j, bright = 0, min = sector->lightlevel;
  371. for (j = 0; j < sector->linecount; j++)
  372. if ((temp = getNextSector(sector->lines[j],sector)))
  373. {
  374. if (temp->lightlevel > bright)
  375. bright = temp->lightlevel;
  376. if (temp->lightlevel < min)
  377. min = temp->lightlevel;
  378. }
  379. sector->lightlevel = // Set level in-between extremes
  380. (level * bright + (FRACUNIT-level) * min) >> FRACBITS;
  381. }
  382. return 1;
  383. }