p_lights.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /*
  2. ===========================================================================
  3. Doom 3 BFG Edition GPL Source Code
  4. Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
  6. Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #include "Precompiled.h"
  21. #include "globaldata.h"
  22. #include "z_zone.h"
  23. #include "m_random.h"
  24. #include "doomdef.h"
  25. #include "p_local.h"
  26. // State.
  27. #include "r_state.h"
  28. //
  29. // FIRELIGHT FLICKER
  30. //
  31. //
  32. // T_FireFlicker
  33. //
  34. void T_FireFlicker (fireflicker_t* flick)
  35. {
  36. int amount;
  37. if (--flick->count)
  38. return;
  39. amount = (P_Random()&3)*16;
  40. if (flick->sector->lightlevel - amount < flick->minlight)
  41. flick->sector->lightlevel = flick->minlight;
  42. else
  43. flick->sector->lightlevel = flick->maxlight - amount;
  44. flick->count = 4;
  45. }
  46. //
  47. // P_SpawnFireFlicker
  48. //
  49. void P_SpawnFireFlicker (sector_t* sector)
  50. {
  51. fireflicker_t* flick;
  52. // Note that we are resetting sector attributes.
  53. // Nothing special about it during gameplay.
  54. sector->special = 0;
  55. flick = (fireflicker_t*)DoomLib::Z_Malloc( sizeof(*flick), PU_LEVEL, 0);
  56. P_AddThinker (&flick->thinker);
  57. flick->thinker.function.acp1 = (actionf_p1) T_FireFlicker;
  58. flick->sector = sector;
  59. flick->maxlight = sector->lightlevel;
  60. flick->minlight = P_FindMinSurroundingLight(sector,sector->lightlevel)+16;
  61. flick->count = 4;
  62. }
  63. //
  64. // BROKEN LIGHT FLASHING
  65. //
  66. //
  67. // T_LightFlash
  68. // Do flashing lights.
  69. //
  70. void T_LightFlash (lightflash_t* flash)
  71. {
  72. if (--flash->count)
  73. return;
  74. if (flash->sector->lightlevel == flash->maxlight)
  75. {
  76. flash-> sector->lightlevel = flash->minlight;
  77. flash->count = (P_Random()&flash->mintime)+1;
  78. }
  79. else
  80. {
  81. flash-> sector->lightlevel = flash->maxlight;
  82. flash->count = (P_Random()&flash->maxtime)+1;
  83. }
  84. }
  85. //
  86. // P_SpawnLightFlash
  87. // After the map has been loaded, scan each sector
  88. // for specials that spawn thinkers
  89. //
  90. void P_SpawnLightFlash (sector_t* sector)
  91. {
  92. lightflash_t* flash;
  93. // nothing special about it during gameplay
  94. sector->special = 0;
  95. flash = (lightflash_t*)DoomLib::Z_Malloc( sizeof(*flash), PU_LEVEL, 0);
  96. P_AddThinker (&flash->thinker);
  97. flash->thinker.function.acp1 = (actionf_p1) T_LightFlash;
  98. flash->sector = sector;
  99. flash->maxlight = sector->lightlevel;
  100. flash->minlight = P_FindMinSurroundingLight(sector,sector->lightlevel);
  101. flash->maxtime = 64;
  102. flash->mintime = 7;
  103. flash->count = (P_Random()&flash->maxtime)+1;
  104. }
  105. //
  106. // STROBE LIGHT FLASHING
  107. //
  108. //
  109. // T_StrobeFlash
  110. //
  111. void T_StrobeFlash (strobe_t* flash)
  112. {
  113. if (--flash->count)
  114. return;
  115. if (flash->sector->lightlevel == flash->minlight)
  116. {
  117. flash-> sector->lightlevel = flash->maxlight;
  118. flash->count = flash->brighttime;
  119. }
  120. else
  121. {
  122. flash-> sector->lightlevel = flash->minlight;
  123. flash->count =flash->darktime;
  124. }
  125. }
  126. //
  127. // P_SpawnStrobeFlash
  128. // After the map has been loaded, scan each sector
  129. // for specials that spawn thinkers
  130. //
  131. void
  132. P_SpawnStrobeFlash
  133. ( sector_t* sector,
  134. int fastOrSlow,
  135. int inSync )
  136. {
  137. strobe_t* flash;
  138. flash = (strobe_t*)DoomLib::Z_Malloc( sizeof(*flash), PU_LEVEL, 0);
  139. P_AddThinker (&flash->thinker);
  140. flash->sector = sector;
  141. flash->darktime = fastOrSlow;
  142. flash->brighttime = STROBEBRIGHT;
  143. flash->thinker.function.acp1 = (actionf_p1) T_StrobeFlash;
  144. flash->maxlight = sector->lightlevel;
  145. flash->minlight = P_FindMinSurroundingLight(sector, sector->lightlevel);
  146. if (flash->minlight == flash->maxlight)
  147. flash->minlight = 0;
  148. // nothing special about it during gameplay
  149. sector->special = 0;
  150. if (!inSync)
  151. flash->count = (P_Random()&7)+1;
  152. else
  153. flash->count = 1;
  154. }
  155. //
  156. // Start strobing lights (usually from a trigger)
  157. //
  158. void EV_StartLightStrobing(line_t* line)
  159. {
  160. int secnum;
  161. sector_t* sec;
  162. secnum = -1;
  163. while ((secnum = P_FindSectorFromLineTag(line,secnum)) >= 0)
  164. {
  165. sec = &::g->sectors[secnum];
  166. if (sec->specialdata)
  167. continue;
  168. P_SpawnStrobeFlash (sec,SLOWDARK, 0);
  169. }
  170. }
  171. //
  172. // TURN LINE'S TAG LIGHTS OFF
  173. //
  174. void EV_TurnTagLightsOff(line_t* line)
  175. {
  176. int i;
  177. int j;
  178. int min;
  179. sector_t* sector;
  180. sector_t* tsec;
  181. line_t* templine;
  182. sector = ::g->sectors;
  183. for (j = 0;j < ::g->numsectors; j++, sector++)
  184. {
  185. if (sector->tag == line->tag)
  186. {
  187. min = sector->lightlevel;
  188. for (i = 0;i < sector->linecount; i++)
  189. {
  190. templine = sector->lines[i];
  191. tsec = getNextSector(templine,sector);
  192. if (!tsec)
  193. continue;
  194. if (tsec->lightlevel < min)
  195. min = tsec->lightlevel;
  196. }
  197. sector->lightlevel = min;
  198. }
  199. }
  200. }
  201. //
  202. // TURN LINE'S TAG LIGHTS ON
  203. //
  204. void
  205. EV_LightTurnOn
  206. ( line_t* line,
  207. int bright )
  208. {
  209. int i;
  210. int j;
  211. sector_t* sector;
  212. sector_t* temp;
  213. line_t* templine;
  214. sector = ::g->sectors;
  215. for (i = 0; i < ::g->numsectors; i++, sector++)
  216. {
  217. if (sector->tag == line->tag)
  218. {
  219. // bright = 0 means to search
  220. // for highest light level
  221. // surrounding sector
  222. if (!bright)
  223. {
  224. for (j = 0;j < sector->linecount; j++)
  225. {
  226. templine = sector->lines[j];
  227. temp = getNextSector(templine,sector);
  228. if (!temp)
  229. continue;
  230. if (temp->lightlevel > bright)
  231. bright = temp->lightlevel;
  232. }
  233. }
  234. sector-> lightlevel = bright;
  235. }
  236. }
  237. }
  238. //
  239. // Spawn glowing light
  240. //
  241. void T_Glow(glow_t* g)
  242. {
  243. switch(g->direction)
  244. {
  245. case -1:
  246. // DOWN
  247. g->sector->lightlevel -= GLOWSPEED;
  248. if (g->sector->lightlevel <= g->minlight)
  249. {
  250. g->sector->lightlevel += GLOWSPEED;
  251. g->direction = 1;
  252. }
  253. break;
  254. case 1:
  255. // UP
  256. g->sector->lightlevel += GLOWSPEED;
  257. if (g->sector->lightlevel >= g->maxlight)
  258. {
  259. g->sector->lightlevel -= GLOWSPEED;
  260. g->direction = -1;
  261. }
  262. break;
  263. }
  264. }
  265. void P_SpawnGlowingLight(sector_t* sector)
  266. {
  267. glow_t* g;
  268. g = (glow_t*)DoomLib::Z_Malloc( sizeof(*g), PU_LEVEL, 0);
  269. P_AddThinker(&g->thinker);
  270. g->sector = sector;
  271. g->minlight = P_FindMinSurroundingLight(sector,sector->lightlevel);
  272. g->maxlight = sector->lightlevel;
  273. g->thinker.function.acp1 = (actionf_p1) T_Glow;
  274. g->direction = -1;
  275. sector->special = 0;
  276. }