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