]> git.lizzy.rs Git - plan9front.git/blob - sys/src/games/doom/p_lights.c
merge
[plan9front.git] / sys / src / games / doom / p_lights.c
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
25 static const char
26 rcsid[] = "$Id: p_lights.c,v 1.5 1997/02/03 22:45:11 b1 Exp $";
27
28
29 #include "z_zone.h"
30 #include "m_random.h"
31
32 #include "doomdef.h"
33 #include "p_local.h"
34
35
36 // State.
37 #include "r_state.h"
38
39 //
40 // FIRELIGHT FLICKER
41 //
42
43 //
44 // T_FireFlicker
45 //
46 void T_FireFlicker (void *_flick, void*)
47 {
48     fireflicker_t *flick = (fireflicker_t*)_flick;
49     int amount;
50         
51     if (--flick->count)
52         return;
53         
54     amount = (P_Random()&3)*16;
55     
56     if (flick->sector->lightlevel - amount < flick->minlight)
57         flick->sector->lightlevel = flick->minlight;
58     else
59         flick->sector->lightlevel = flick->maxlight - amount;
60
61     flick->count = 4;
62 }
63
64
65
66 //
67 // P_SpawnFireFlicker
68 //
69 void P_SpawnFireFlicker (sector_t*      sector)
70 {
71     fireflicker_t*      flick;
72         
73     // Note that we are resetting sector attributes.
74     // Nothing special about it during gameplay.
75     sector->special = 0; 
76         
77     flick = Z_Malloc ( sizeof(*flick), PU_LEVSPEC, 0);
78
79     P_AddThinker (&flick->thinker);
80
81     flick->thinker.function = T_FireFlicker;
82     flick->sector = sector;
83     flick->maxlight = sector->lightlevel;
84     flick->minlight = P_FindMinSurroundingLight(sector,sector->lightlevel)+16;
85     flick->count = 4;
86 }
87
88
89
90 //
91 // BROKEN LIGHT FLASHING
92 //
93
94
95 //
96 // T_LightFlash
97 // Do flashing lights.
98 //
99 void T_LightFlash (void* _flash, void*)
100 {
101     lightflash_t *flash = (lightflash_t*)_flash;
102     if (--flash->count)
103         return;
104         
105     if (flash->sector->lightlevel == flash->maxlight)
106     {
107         flash-> sector->lightlevel = flash->minlight;
108         flash->count = (P_Random()&flash->mintime)+1;
109     }
110     else
111     {
112         flash-> sector->lightlevel = flash->maxlight;
113         flash->count = (P_Random()&flash->maxtime)+1;
114     }
115
116 }
117
118
119
120
121 //
122 // P_SpawnLightFlash
123 // After the map has been loaded, scan each sector
124 // for specials that spawn thinkers
125 //
126 void P_SpawnLightFlash (sector_t*       sector)
127 {
128     lightflash_t*       flash;
129
130     // nothing special about it during gameplay
131     sector->special = 0;        
132         
133     flash = Z_Malloc ( sizeof(*flash), PU_LEVSPEC, 0);
134
135     P_AddThinker (&flash->thinker);
136
137     flash->thinker.function = T_LightFlash;
138     flash->sector = sector;
139     flash->maxlight = sector->lightlevel;
140
141     flash->minlight = P_FindMinSurroundingLight(sector,sector->lightlevel);
142     flash->maxtime = 64;
143     flash->mintime = 7;
144     flash->count = (P_Random()&flash->maxtime)+1;
145 }
146
147
148
149 //
150 // STROBE LIGHT FLASHING
151 //
152
153
154 //
155 // T_StrobeFlash
156 //
157 void T_StrobeFlash (void *_flash, void*)
158 {
159     strobe_t *flash = (strobe_t*)_flash;
160     if (--flash->count)
161         return;
162         
163     if (flash->sector->lightlevel == flash->minlight)
164     {
165         flash-> sector->lightlevel = flash->maxlight;
166         flash->count = flash->brighttime;
167     }
168     else
169     {
170         flash-> sector->lightlevel = flash->minlight;
171         flash->count =flash->darktime;
172     }
173
174 }
175
176
177
178 //
179 // P_SpawnStrobeFlash
180 // After the map has been loaded, scan each sector
181 // for specials that spawn thinkers
182 //
183 void
184 P_SpawnStrobeFlash
185 ( sector_t*     sector,
186   int           fastOrSlow,
187   int           inSync )
188 {
189     strobe_t*   flash;
190         
191     flash = Z_Malloc ( sizeof(*flash), PU_LEVSPEC, 0);
192
193     P_AddThinker (&flash->thinker);
194
195     flash->sector = sector;
196     flash->darktime = fastOrSlow;
197     flash->brighttime = STROBEBRIGHT;
198     flash->thinker.function = T_StrobeFlash;
199     flash->maxlight = sector->lightlevel;
200     flash->minlight = P_FindMinSurroundingLight(sector, sector->lightlevel);
201                 
202     if (flash->minlight == flash->maxlight)
203         flash->minlight = 0;
204
205     // nothing special about it during gameplay
206     sector->special = 0;        
207
208     if (!inSync)
209         flash->count = (P_Random()&7)+1;
210     else
211         flash->count = 1;
212 }
213
214
215 //
216 // Start strobing lights (usually from a trigger)
217 //
218 void EV_StartLightStrobing(line_t*      line)
219 {
220     int         secnum;
221     sector_t*   sec;
222         
223     secnum = -1;
224     while ((secnum = P_FindSectorFromLineTag(line,secnum)) >= 0)
225     {
226         sec = &sectors[secnum];
227         if (sec->specialdata)
228             continue;
229         
230         P_SpawnStrobeFlash (sec,SLOWDARK, 0);
231     }
232 }
233
234
235
236 //
237 // TURN LINE'S TAG LIGHTS OFF
238 //
239 void EV_TurnTagLightsOff(line_t* line)
240 {
241     int                 i;
242     int                 j;
243     int                 min;
244     sector_t*           sector;
245     sector_t*           tsec;
246     line_t*             templine;
247         
248     sector = sectors;
249     
250     for (j = 0;j < numsectors; j++, sector++)
251     {
252         if (sector->tag == line->tag)
253         {
254             min = sector->lightlevel;
255             for (i = 0;i < sector->linecount; i++)
256             {
257                 templine = sector->lines[i];
258                 tsec = getNextSector(templine,sector);
259                 if (!tsec)
260                     continue;
261                 if (tsec->lightlevel < min)
262                     min = tsec->lightlevel;
263             }
264             sector->lightlevel = min;
265         }
266     }
267 }
268
269
270 //
271 // TURN LINE'S TAG LIGHTS ON
272 //
273 void
274 EV_LightTurnOn
275 ( line_t*       line,
276   int           bright )
277 {
278     int         i;
279     int         j;
280     sector_t*   sector;
281     sector_t*   temp;
282     line_t*     templine;
283         
284     sector = sectors;
285         
286     for (i=0;i<numsectors;i++, sector++)
287     {
288         if (sector->tag == line->tag)
289         {
290             // bright = 0 means to search
291             // for highest light level
292             // surrounding sector
293             if (!bright)
294             {
295                 for (j = 0;j < sector->linecount; j++)
296                 {
297                     templine = sector->lines[j];
298                     temp = getNextSector(templine,sector);
299
300                     if (!temp)
301                         continue;
302
303                     if (temp->lightlevel > bright)
304                         bright = temp->lightlevel;
305                 }
306             }
307             sector-> lightlevel = bright;
308         }
309     }
310 }
311
312     
313 //
314 // Spawn glowing light
315 //
316
317 void T_Glow(void* _g, void*)
318 {
319     glow_t *g = (glow_t*)_g;
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 = Z_Malloc( sizeof(*g), PU_LEVSPEC, 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 = T_Glow;
357     g->direction = -1;
358
359     sector->special = 0;
360 }
361