summaryrefslogtreecommitdiff
path: root/src/hexen/p_lights.c
blob: c7e85f9b3a082bfbff9a85f5e5c3fb1ea5a39eb8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
// Emacs style mode select   -*- C++ -*- 
//-----------------------------------------------------------------------------
//
// Copyright(C) 1993-1996 Id Software, Inc.
// Copyright(C) 1993-2008 Raven Software
// Copyright(C) 2008 Simon Howard
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
// 02111-1307, USA.
//
//-----------------------------------------------------------------------------


#include "h2def.h"
#include "m_random.h"
#include "p_local.h"

//============================================================================
//
//      T_Light
//
//============================================================================

void T_Light(light_t * light)
{
    if (light->count)
    {
        light->count--;
        return;
    }
    switch (light->type)
    {
        case LITE_FADE:
            light->sector->lightlevel =
                ((light->sector->lightlevel << FRACBITS) +
                 light->value2) >> FRACBITS;
            if (light->tics2 == 1)
            {
                if (light->sector->lightlevel >= light->value1)
                {
                    light->sector->lightlevel = light->value1;
                    P_RemoveThinker(&light->thinker);
                }
            }
            else if (light->sector->lightlevel <= light->value1)
            {
                light->sector->lightlevel = light->value1;
                P_RemoveThinker(&light->thinker);
            }
            break;
        case LITE_GLOW:
            light->sector->lightlevel =
                ((light->sector->lightlevel << FRACBITS) +
                 light->tics1) >> FRACBITS;
            if (light->tics2 == 1)
            {
                if (light->sector->lightlevel >= light->value1)
                {
                    light->sector->lightlevel = light->value1;
                    light->tics1 = -light->tics1;
                    light->tics2 = -1;  // reverse direction
                }
            }
            else if (light->sector->lightlevel <= light->value2)
            {
                light->sector->lightlevel = light->value2;
                light->tics1 = -light->tics1;
                light->tics2 = 1;       // reverse direction
            }
            break;
        case LITE_FLICKER:
            if (light->sector->lightlevel == light->value1)
            {
                light->sector->lightlevel = light->value2;
                light->count = (P_Random() & 7) + 1;
            }
            else
            {
                light->sector->lightlevel = light->value1;
                light->count = (P_Random() & 31) + 1;
            }
            break;
        case LITE_STROBE:
            if (light->sector->lightlevel == light->value1)
            {
                light->sector->lightlevel = light->value2;
                light->count = light->tics2;
            }
            else
            {
                light->sector->lightlevel = light->value1;
                light->count = light->tics1;
            }
            break;
        default:
            break;
    }
}

//============================================================================
//
//      EV_SpawnLight
//
//============================================================================

boolean EV_SpawnLight(line_t * line, byte * arg, lighttype_t type)
{
    light_t *light;
    sector_t *sec;
    int secNum;
    int arg1, arg2, arg3, arg4;
    boolean think;
    boolean rtn;

    /*
    Original code; redundant considering that a byte value is always
    in the range 0-255:

    arg1 = arg[1] > 255 ? 255 : arg[1];
    arg1 = arg1 < 0 ? 0 : arg1;
    arg2 = arg[2] > 255 ? 255 : arg[2];
    arg2 = arg2 < 0 ? 0 : arg2;
    arg3 = arg[3] > 255 ? 255 : arg[3];
    arg3 = arg3 < 0 ? 0 : arg3;
    arg4 = arg[4] > 255 ? 255 : arg[4];
    arg4 = arg4 < 0 ? 0 : arg4;
    */

    arg1 = arg[1];
    arg2 = arg[2];
    arg3 = arg[3];
    arg4 = arg[4];

    secNum = -1;
    rtn = false;
    think = false;
    while ((secNum = P_FindSectorFromTag(arg[0], secNum)) >= 0)
    {
        think = false;
        sec = &sectors[secNum];

        light = (light_t *) Z_Malloc(sizeof(light_t), PU_LEVSPEC, 0);
        light->type = type;
        light->sector = sec;
        light->count = 0;
        rtn = true;
        switch (type)
        {
            case LITE_RAISEBYVALUE:
                sec->lightlevel += arg1;
                if (sec->lightlevel > 255)
                {
                    sec->lightlevel = 255;
                }
                break;
            case LITE_LOWERBYVALUE:
                sec->lightlevel -= arg1;
                if (sec->lightlevel < 0)
                {
                    sec->lightlevel = 0;
                }
                break;
            case LITE_CHANGETOVALUE:
                sec->lightlevel = arg1;
                if (sec->lightlevel < 0)
                {
                    sec->lightlevel = 0;
                }
                else if (sec->lightlevel > 255)
                {
                    sec->lightlevel = 255;
                }
                break;
            case LITE_FADE:
                think = true;
                light->value1 = arg1;   // destination lightlevel
                light->value2 = FixedDiv((arg1 - sec->lightlevel) << FRACBITS, arg2 << FRACBITS);       // delta lightlevel
                if (sec->lightlevel <= arg1)
                {
                    light->tics2 = 1;   // get brighter
                }
                else
                {
                    light->tics2 = -1;
                }
                break;
            case LITE_GLOW:
                think = true;
                light->value1 = arg1;   // upper lightlevel
                light->value2 = arg2;   // lower lightlevel
                light->tics1 = FixedDiv((arg1 - sec->lightlevel) << FRACBITS, arg3 << FRACBITS);        // lightlevel delta
                if (sec->lightlevel <= arg1)
                {
                    light->tics2 = 1;   // get brighter
                }
                else
                {
                    light->tics2 = -1;
                }
                break;
            case LITE_FLICKER:
                think = true;
                light->value1 = arg1;   // upper lightlevel
                light->value2 = arg2;   // lower lightlevel
                sec->lightlevel = light->value1;
                light->count = (P_Random() & 64) + 1;
                break;
            case LITE_STROBE:
                think = true;
                light->value1 = arg1;   // upper lightlevel
                light->value2 = arg2;   // lower lightlevel
                light->tics1 = arg3;    // upper tics
                light->tics2 = arg4;    // lower tics
                light->count = arg3;
                sec->lightlevel = light->value1;
                break;
            default:
                rtn = false;
                break;
        }
        if (think)
        {
            P_AddThinker(&light->thinker);
            light->thinker.function = T_Light;
        }
        else
        {
            Z_Free(light);
        }
    }
    return rtn;
}

//============================================================================
//
//      T_Phase
//
//============================================================================

int PhaseTable[64] = {
    128, 112, 96, 80, 64, 48, 32, 32,
    16, 16, 16, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 16, 16, 16,
    32, 32, 48, 64, 80, 96, 112, 128
};

void T_Phase(phase_t * phase)
{
    phase->index = (phase->index + 1) & 63;
    phase->sector->lightlevel = phase->base + PhaseTable[phase->index];
}

//==========================================================================
//
// P_SpawnPhasedLight
//
//==========================================================================

void P_SpawnPhasedLight(sector_t * sector, int base, int index)
{
    phase_t *phase;

    phase = Z_Malloc(sizeof(*phase), PU_LEVSPEC, 0);
    P_AddThinker(&phase->thinker);
    phase->sector = sector;
    if (index == -1)
    {                           // sector->lightlevel as the index
        phase->index = sector->lightlevel & 63;
    }
    else
    {
        phase->index = index & 63;
    }
    phase->base = base & 255;
    sector->lightlevel = phase->base + PhaseTable[phase->index];
    phase->thinker.function = T_Phase;

    sector->special = 0;
}

//==========================================================================
//
// P_SpawnLightSequence
//
//==========================================================================

void P_SpawnLightSequence(sector_t * sector, int indexStep)
{
    sector_t *sec;
    sector_t *nextSec;
    sector_t *tempSec;
    int seqSpecial;
    int i;
    int count;
    fixed_t index;
    fixed_t indexDelta;
    int base;

    seqSpecial = LIGHT_SEQUENCE;        // look for Light_Sequence, first
    sec = sector;
    count = 1;
    do
    {
        nextSec = NULL;
        sec->special = LIGHT_SEQUENCE_START;    // make sure that the search doesn't back up.
        for (i = 0; i < sec->linecount; i++)
        {
            tempSec = getNextSector(sec->lines[i], sec);
            if (!tempSec)
            {
                continue;
            }
            if (tempSec->special == seqSpecial)
            {
                if (seqSpecial == LIGHT_SEQUENCE)
                {
                    seqSpecial = LIGHT_SEQUENCE_ALT;
                }
                else
                {
                    seqSpecial = LIGHT_SEQUENCE;
                }
                nextSec = tempSec;
                count++;
            }
        }
        sec = nextSec;
    }
    while (sec);

    sec = sector;
    count *= indexStep;
    index = 0;
    indexDelta = FixedDiv(64 * FRACUNIT, count * FRACUNIT);
    base = sector->lightlevel;
    do
    {
        nextSec = NULL;
        if (sec->lightlevel)
        {
            base = sec->lightlevel;
        }
        P_SpawnPhasedLight(sec, base, index >> FRACBITS);
        index += indexDelta;
        for (i = 0; i < sec->linecount; i++)
        {
            tempSec = getNextSector(sec->lines[i], sec);
            if (!tempSec)
            {
                continue;
            }
            if (tempSec->special == LIGHT_SEQUENCE_START)
            {
                nextSec = tempSec;
            }
        }
        sec = nextSec;
    }
    while (sec);
}