]> icculus.org git repositories - theoddone33/hhexen.git/blob - base/p_lights.c
Remove obnoxious documentation files
[theoddone33/hhexen.git] / base / p_lights.c
1
2 //**************************************************************************
3 //**
4 //** p_lights.c : Heretic 2 : Raven Software, Corp.
5 //**
6 //** $RCSfile$
7 //** $Revision$
8 //** $Date$
9 //** $Author$
10 //**
11 //**************************************************************************
12
13 #include "h2def.h"
14 #include "p_local.h"
15
16 //============================================================================
17 //
18 //      T_Light
19 //
20 //============================================================================
21
22 void T_Light(light_t *light)
23 {
24         if(light->count)
25         {
26                 light->count--; 
27                 return;
28         }
29         switch(light->type)
30         {
31                 case LITE_FADE:
32                         light->sector->lightlevel = ((light->sector->lightlevel<<FRACBITS)
33                                 +light->value2)>>FRACBITS;
34                         if(light->tics2 == 1)
35                         {
36                                 if(light->sector->lightlevel >= light->value1)
37                                 {
38                                         light->sector->lightlevel = light->value1;
39                                         P_RemoveThinker(&light->thinker);
40                                 }
41                         }
42                         else if(light->sector->lightlevel <= light->value1)
43                         {
44                                 light->sector->lightlevel = light->value1;
45                                 P_RemoveThinker(&light->thinker);
46                         }
47                         break;
48                 case LITE_GLOW:
49                         light->sector->lightlevel = ((light->sector->lightlevel<<FRACBITS)
50                                 +light->tics1)>>FRACBITS;
51                         if(light->tics2 == 1)
52                         {
53                                 if(light->sector->lightlevel >= light->value1)
54                                 {
55                                         light->sector->lightlevel = light->value1;
56                                         light->tics1 = -light->tics1;
57                                         light->tics2 = -1; // reverse direction
58                                 }
59                         }
60                         else if(light->sector->lightlevel <= light->value2)
61                         {
62                                 light->sector->lightlevel = light->value2;
63                                 light->tics1 = -light->tics1;
64                                 light->tics2 = 1; // reverse direction
65                         }
66                         break;
67                 case LITE_FLICKER:
68                         if(light->sector->lightlevel == light->value1)
69                         {
70                                 light->sector->lightlevel = light->value2;
71                                 light->count = (P_Random()&7)+1;
72                         }
73                         else
74                         {
75                                 light->sector->lightlevel = light->value1;
76                                 light->count = (P_Random()&31)+1;
77                         }
78                         break;
79                 case LITE_STROBE:
80                         if(light->sector->lightlevel == light->value1)
81                         {
82                                 light->sector->lightlevel = light->value2;
83                                 light->count = light->tics2;
84                         }
85                         else
86                         {
87                                 light->sector->lightlevel = light->value1;
88                                 light->count = light->tics1;
89                         }
90                         break;
91                 default:
92                         break;
93         }
94 }
95
96 //============================================================================
97 //
98 //      EV_SpawnLight
99 //
100 //============================================================================
101
102 boolean EV_SpawnLight(line_t *line, byte *arg, lighttype_t type)
103 {
104         light_t *light;
105         sector_t *sec;
106         int secNum;     
107         int arg1, arg2, arg3, arg4;
108         boolean think;
109         boolean rtn;
110         
111         arg1 = arg[1];
112         arg2 = arg[2];
113         arg3 = arg[3];
114         arg4 = arg[4];
115
116         secNum = -1;
117         rtn = false;
118         think = false;
119         while((secNum = P_FindSectorFromTag(arg[0], secNum)) >= 0)
120         {
121                 think = false;
122                 sec = &sectors[secNum];
123
124                 light = (light_t *)Z_Malloc(sizeof(light_t), PU_LEVSPEC, 0);
125                 light->type = type;
126                 light->sector = sec;
127                 light->count = 0;
128                 rtn = true;
129                 switch(type)
130                 {
131                         case LITE_RAISEBYVALUE:
132                                 sec->lightlevel += arg1;
133                                 if(sec->lightlevel > 255)
134                                 {
135                                         sec->lightlevel = 255;
136                                 }
137                                 break;
138                         case LITE_LOWERBYVALUE:
139                                 sec->lightlevel -= arg1;
140                                 if(sec->lightlevel < 0)
141                                 {
142                                         sec->lightlevel = 0;
143                                 }
144                                 break;
145                         case LITE_CHANGETOVALUE:
146                                 sec->lightlevel = arg1;
147                                 if(sec->lightlevel < 0)
148                                 {
149                                         sec->lightlevel = 0;
150                                 }
151                                 else if(sec->lightlevel > 255)
152                                 {
153                                         sec->lightlevel = 255;
154                                 }
155                                 break;
156                         case LITE_FADE:
157                                 think = true;
158                                 light->value1 = arg1; // destination lightlevel
159                                 light->value2 = FixedDiv((arg1-sec->lightlevel)<<FRACBITS,
160                                         arg2<<FRACBITS);  // delta lightlevel
161                                 if(sec->lightlevel <= arg1)
162                                 {
163                                         light->tics2 = 1; // get brighter
164                                 }
165                                 else
166                                 {
167                                         light->tics2 = -1;
168                                 }
169                                 break;
170                         case LITE_GLOW:
171                                 think = true;
172                                 light->value1 = arg1; // upper lightlevel
173                                 light->value2 = arg2; // lower lightlevel
174                                 light->tics1 = FixedDiv((arg1-sec->lightlevel)<<FRACBITS,
175                                         arg3<<FRACBITS);  // lightlevel delta
176                                 if(sec->lightlevel <= arg1)
177                                 {
178                                         light->tics2 = 1; // get brighter
179                                 }
180                                 else
181                                 {
182                                         light->tics2 = -1;
183                                 }
184                                 break;
185                         case LITE_FLICKER:
186                                 think = true;
187                                 light->value1 = arg1; // upper lightlevel
188                                 light->value2 = arg2; // lower lightlevel
189                                 sec->lightlevel = light->value1;
190                                 light->count = (P_Random()&64)+1;
191                                 break;
192                         case LITE_STROBE:
193                                 think = true;
194                                 light->value1 = arg1; // upper lightlevel
195                                 light->value2 = arg2; // lower lightlevel
196                                 light->tics1 = arg3;  // upper tics
197                                 light->tics2 = arg4;  // lower tics
198                                 light->count = arg3;
199                                 sec->lightlevel = light->value1;
200                                 break;
201                         default:
202                                 rtn = false;
203                                 break;
204                 }
205                 if(think)
206                 {
207                         P_AddThinker(&light->thinker);
208                         light->thinker.function = T_Light;
209                 }
210                 else
211                 {
212                         Z_Free(light);
213                 }
214         }
215         return rtn;
216 }
217
218 //============================================================================
219 //
220 //      T_Phase
221 //
222 //============================================================================
223
224 int PhaseTable[64] =
225 {
226         128, 112, 96, 80, 64, 48, 32, 32,
227         16, 16, 16, 0, 0, 0, 0, 0,
228         0, 0, 0, 0, 0, 0, 0, 0,
229         0, 0, 0, 0, 0, 0, 0, 0,
230         0, 0, 0, 0, 0, 0, 0, 0,
231         0, 0, 0, 0, 0, 0, 0, 0,
232         0, 0, 0, 0, 0, 16, 16, 16,
233         32, 32, 48, 64, 80, 96, 112, 128
234 };
235
236 void T_Phase(phase_t *phase)
237 {
238         phase->index = (phase->index+1)&63;
239         phase->sector->lightlevel = phase->base+PhaseTable[phase->index];
240 }
241
242 //==========================================================================
243 //
244 // P_SpawnPhasedLight
245 //
246 //==========================================================================
247
248 void P_SpawnPhasedLight(sector_t *sector, int base, int index)
249 {
250         phase_t *phase;
251
252         phase = Z_Malloc(sizeof(*phase), PU_LEVSPEC, 0);
253         P_AddThinker(&phase->thinker);
254         phase->sector = sector;
255         if(index == -1)
256         { // sector->lightlevel as the index
257                 phase->index = sector->lightlevel&63;
258         }
259         else
260         {
261                 phase->index = index&63;
262         }
263         phase->base = base&255;
264         sector->lightlevel = phase->base+PhaseTable[phase->index];
265         phase->thinker.function = T_Phase;
266
267         sector->special = 0;
268 }
269
270 //==========================================================================
271 //
272 // P_SpawnLightSequence
273 //
274 //==========================================================================
275
276 void P_SpawnLightSequence(sector_t *sector, int indexStep)
277 {
278         sector_t *sec;
279         sector_t *nextSec;
280         sector_t *tempSec;
281         int seqSpecial;
282         int i;
283         int count;
284         fixed_t index;
285         fixed_t indexDelta;
286         int base;
287
288         seqSpecial = LIGHT_SEQUENCE; // look for Light_Sequence, first
289         sec = sector;
290         count = 1;
291         do
292         {
293                 nextSec = NULL;
294                 sec->special = LIGHT_SEQUENCE_START; // make sure that the search doesn't back up.
295                 for(i = 0; i < sec->linecount; i++)
296                 {
297                         tempSec = getNextSector(sec->lines[i], sec);
298                         if(!tempSec)
299                         {
300                                 continue;
301                         }
302                         if(tempSec->special == seqSpecial)
303                         {
304                                 if(seqSpecial == LIGHT_SEQUENCE)
305                                 {
306                                         seqSpecial = LIGHT_SEQUENCE_ALT;
307                                 }
308                                 else
309                                 {
310                                         seqSpecial = LIGHT_SEQUENCE;
311                                 }
312                                 nextSec = tempSec;
313                                 count++;
314                         }
315                 }
316                 sec = nextSec;
317         } while(sec);
318
319         sec = sector;
320         count *= indexStep;
321         index = 0;
322         indexDelta = FixedDiv(64*FRACUNIT, count*FRACUNIT);
323         base = sector->lightlevel;
324         do
325         {
326                 nextSec = NULL;
327                 if(sec->lightlevel)
328                 {
329                         base = sec->lightlevel;
330                 }
331                 P_SpawnPhasedLight(sec, base, index>>FRACBITS);
332                 index += indexDelta;
333                 for(i = 0; i < sec->linecount; i++)
334                 {
335                         tempSec = getNextSector(sec->lines[i], sec);
336                         if(!tempSec)
337                         {
338                                 continue;
339                         }
340                         if(tempSec->special == LIGHT_SEQUENCE_START)
341                         {
342                                 nextSec = tempSec;
343                         }
344                 }
345                 sec = nextSec;
346         } while(sec);
347 }