-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathp_switch.c
488 lines (417 loc) · 12.7 KB
/
p_switch.c
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
/* Emacs style mode select -*- C++ -*-
*-----------------------------------------------------------------------------
*
*
* PrBoom: a Doom port merged with LxDoom and LSDLDoom
* based on BOOM, a modified and improved DOOM engine
* Copyright (C) 1999 by
* id Software, Chi Hoang, Lee Killough, Jim Flynn, Rand Phares, Ty Halderman
* Copyright (C) 1999-2000 by
* Jess Haas, Nicolas Kalkhof, Colin Phipps, Florian Schulze
* Copyright 2005, 2006 by
* Florian Schulze, Colin Phipps, Neil Stevens, Andrey Budko
* Copyright 2023, 2024 by
* Frenkel Smeijers
*
* 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.
*
* DESCRIPTION:
* Switches, buttons. Two-state animation. Exits.
*
*-----------------------------------------------------------------------------*/
#include "d_englsh.h"
#include "d_player.h"
#include "w_wad.h"
#include "r_main.h"
#include "p_spec.h"
#include "g_game.h"
#include "sounds.h"
#include "i_system.h"
#include "globdata.h"
#define MAXSWITCHES 19
static int16_t switchlist[(MAXSWITCHES + 1) * 2];
static int16_t numswitches;
button_t _g_buttonlist[MAXBUTTONS];
// switch animation structure type
typedef struct
{
char name1[9];
char name2[9];
} switchlist_t;
typedef char assertSwitchlistSize[sizeof(switchlist_t) == 18 ? 1 : -1];
static const switchlist_t alphSwitchList[MAXSWITCHES] =
{
// Doom episode 1 switches
{"SW1BRCOM", "SW2BRCOM"},
{"SW1BRN1", "SW2BRN1" },
{"SW1BRN2", "SW2BRN2" },
{"SW1BRNGN", "SW2BRNGN"},
{"SW1BROWN", "SW2BROWN"},
{"SW1COMM", "SW2COMM" },
{"SW1COMP", "SW2COMP" },
{"SW1DIRT", "SW2DIRT" },
{"SW1EXIT", "SW2EXIT" },
{"SW1GRAY", "SW2GRAY" },
{"SW1GRAY1", "SW2GRAY1"},
{"SW1METAL", "SW2METAL"},
{"SW1PIPE", "SW2PIPE" },
{"SW1SLAD", "SW2SLAD" },
{"SW1STARG", "SW2STARG"},
{"SW1STON1", "SW2STON1"},
{"SW1STON2", "SW2STON2"},
{"SW1STONE", "SW2STONE"},
{"SW1STRTN", "SW2STRTN"}
};
//
// P_InitSwitchList
// Only called at game initialization.
//
void P_InitSwitchList(void)
{
int16_t index = 0;
for (int16_t i = 0; i < MAXSWITCHES; i++)
{
switchlist[index++] = R_CheckTextureNumForName(alphSwitchList[i].name1);
switchlist[index++] = R_CheckTextureNumForName(alphSwitchList[i].name2);
}
numswitches = index/2;
switchlist[index] = -1;
}
void P_LoadTexture(int16_t texture)
{
R_GetTexture(texture);
for (int16_t i = 0; i < numswitches * 2; i++)
{
if (switchlist[i] == texture)
{
R_GetTexture(switchlist[i ^ 1]);
return;
}
}
}
//
// P_StartButton()
//
// Start a button (retriggerable switch) counting down till it turns off.
//
// Passed the linedef the button is on, which texture on the sidedef contains
// the button, the texture number of the button, and the time the button is
// to remain active in gametics.
// No return.
//
static void P_StartButton(const line_t __far* line, bwhere_e w, int16_t texture, int16_t time)
{
int16_t i;
// See if button is already pressed
for (i = 0;i < MAXBUTTONS;i++)
if (_g_buttonlist[i].btimer && _g_buttonlist[i].line == line)
return;
for (i = 0;i < MAXBUTTONS;i++)
if (!_g_buttonlist[i].btimer) // use first unused element of list
{
_g_buttonlist[i].line = line;
_g_buttonlist[i].where = w;
_g_buttonlist[i].btexture = texture;
_g_buttonlist[i].btimer = time;
/* use sound origin of line itself - no need to compatibility-wrap
* as the popout code gets it wrong whatever its value */
_g_buttonlist[i].soundorg = &LN_FRONTSECTOR(line)->soundorg;
return;
}
I_Error("P_StartButton: no button slots left!");
}
//
// P_ChangeSwitchTexture()
//
// Function that changes switch wall texture on activation.
//
// Passed the line which the switch is on, and whether its retriggerable.
// If not retriggerable, this function clears the line special to insure that
//
// No return
//
// 1 second, in ticks.
#define BUTTONTIME TICRATE
static void P_ChangeSwitchTexture(line_t __far* line, boolean useAgain)
{
/* Rearranged a bit to avoid too much code duplication */
int16_t i;
int16_t *texture, ttop, tmid, tbot;
bwhere_e position;
ttop = _g_sides[line->sidenum[0]].toptexture;
tmid = _g_sides[line->sidenum[0]].midtexture;
tbot = _g_sides[line->sidenum[0]].bottomtexture;
/* don't zero line->special until after exit switch test */
if (!useAgain)
LN_SPECIAL(line) = 0;
/* search for a texture to change */
texture = NULL;
position = 0;
for (i = 0; i < numswitches*2; i++)
{
if (switchlist[i] == ttop)
{
texture = &ttop;
position = top;
break;
}
else if (switchlist[i] == tmid)
{
texture = &tmid;
position = middle;
break;
}
else if (switchlist[i] == tbot)
{
texture = &tbot;
position = bottom;
break;
}
}
if (texture == NULL)
return; /* no switch texture was found to change */
*texture = switchlist[i^1];
switch(position)
{
case top:
_g_sides[line->sidenum[0]].toptexture = *texture;
break;
case middle:
_g_sides[line->sidenum[0]].midtexture = *texture;
break;
case bottom:
_g_sides[line->sidenum[0]].bottomtexture = *texture;
break;
}
if (useAgain)
P_StartButton(line, position, switchlist[i], BUTTONTIME);
}
//
// EV_VerticalDoor
//
// Handle opening a door manually, no tag value
//
// Passed the line activating the door and the thing activating it
//
static void EV_VerticalDoor(line_t __far* line, mobj_t __far* thing)
{
player_t* player;
sector_t __far* sec;
vldoor_t __far* door;
// Check for locks
player = P_MobjIsPlayer(thing);
switch(LN_SPECIAL(line))
{
case 26: // Blue Lock
if ( !player )
return;
if (!player->cards[it_bluecard])
{
player->message = PD_BLUEK; // Ty 03/27/98 - externalized
return;
}
break;
case 27: // Yellow Lock
case 34:
if ( !player )
return;
if (!player->cards[it_yellowcard])
{
player->message = PD_YELLOWK; // Ty 03/27/98 - externalized
return;
}
break;
case 28: // Red Lock
if ( !player )
return;
if (!player->cards[it_redcard])
{
player->message = PD_REDK; // Ty 03/27/98 - externalized
return;
}
break;
default:
break;
}
// if the wrong side of door is pushed, give oof sound
if (line->sidenum[1]==NO_INDEX) // killough
{
return;
}
// get the sector on the second side of activating linedef
sec = _g_sides[line->sidenum[1]].sector;
door = sec->ceilingdata;
/* If this is a repeatable line, and the door is already moving, then we can just reverse the current action. Note that in prboom 2.3.0 I erroneously removed the if-this-is-repeatable check, hence the prboom_4_compatibility clause below (foolishly assumed that already moving implies repeatable - but it could be moving due to another switch, e.g. lv19-509) */
if (door &&
(
(LN_SPECIAL(line) == 1) || (LN_SPECIAL(line) == 26) || (LN_SPECIAL(line) == 27) || (LN_SPECIAL(line) == 28)
)
) {
/* For old demos we have to emulate the old buggy behavior and
* mess up non-T_VerticalDoor actions.
*/
if (door->thinker.function == T_VerticalDoor)
{
/* cph - we are writing outval to door->direction iff it is non-zero */
int16_t outval = 0;
/* An already moving repeatable door which is being re-pressed, or a
* monster is trying to open a closing door - so change direction
* DEMOSYNC: we only read door->direction now if it really is a door.
*/
if (door->thinker.function == T_VerticalDoor && door->direction == -1) {
outval = 1; /* go back up */
} else if (player) {
outval = -1; /* go back down */
}
/* Write this to the thinker. In demo compatibility mode, we might be
* overwriting a field of a non-vldoor_t thinker - we need to add any
* other thinker types here if any demos depend on specific fields
* being corrupted by this.
*/
if (outval) {
if (door->thinker.function == T_VerticalDoor) {
door->direction = outval;
} else if (door->thinker.function == T_PlatRaise) {
plat_t __far* p = (plat_t __far*)door;
p->wait = outval;
} else {
printf("EV_VerticalDoor: unknown thinker.function in thinker corruption emulation\n");
}
return;
}
}
/* Either we're in prboom >=v2.3 and it's not a door, or it's a door but
* we're a monster and don't want to shut it; exit with no action.
*/
return;
}
// new door thinker
door = Z_CallocLevSpec(sizeof(*door));
P_AddThinker (&door->thinker);
sec->ceilingdata = door; //jff 2/22/98
door->thinker.function = T_VerticalDoor;
door->sector = sec;
door->direction = 1;
door->speed = VDOORSPEED;
door->topwait = VDOORWAIT;
door->line = line; // jff 1/31/98 remember line that triggered us
/* killough 10/98: use gradual lighting changes if nonzero tag given */
door->lighttag = line->tag;
// set the type of door from the activating linedef type
switch(LN_SPECIAL(line))
{
case 1:
case 26:
case 27:
case 28:
door->type = dnormal;
break;
case 31:
case 34:
door->type = dopen;
LN_SPECIAL(line) = 0;
break;
default:
door->lighttag = 0; // killough 10/98
break;
}
// find the top and bottom of the movement range
door->topheight = P_FindLowestCeilingSurrounding(sec);
door->topheight -= 4*FRACUNIT;
}
//
// P_UseSpecialLine
//
//
// Called when a thing uses (pushes) a special line.
// Only the front sides of lines are usable.
// Dispatches to the appropriate linedef function handler.
//
// Passed the thing using the line and the line being used
// Returns true if a thinker was created
//
boolean P_UseSpecialLine(mobj_t __far* thing, line_t __far* line)
{
// Switches that other things can activate.
if (!P_MobjIsPlayer(thing))
{
// never open secret doors
if (line->flags & ML_SECRET)
return false;
switch(LN_SPECIAL(line))
{
case 1: // MANUAL DOOR RAISE
case 34: // MANUAL YELLOW
break;
default:
return false;
}
}
if (!P_CheckTag(line)) //jff 2/27/98 disallow zero tag on some types
return false;
// Dispatch to handler according to linedef type
switch (LN_SPECIAL(line))
{
// Manual doors, push type with no tag
case 1: // Vertical Door
case 26: // Blue Door/Locked
case 27: // Yellow Door /Locked
case 28: // Red Door /Locked
case 31: // Manual door open
case 34: // Yellow locked door open
EV_VerticalDoor (line, thing);
break;
// Switches (non-retriggerable)
case 11:
/* Exit level
* killough 10/98: prevent zombies from exiting levels
*/
if (P_MobjIsPlayer(thing) && P_MobjIsPlayer(thing)->health <= 0)
{
return false;
}
P_ChangeSwitchTexture(line,false);
G_ExitLevel ();
break;
case 23:
// Lower Floor to Lowest
if (EV_DoFloor(line,lowerFloorToLowest))
P_ChangeSwitchTexture(line,false);
break;
case 103:
// Open Door
if (EV_DoDoor(line,dopen))
P_ChangeSwitchTexture(line,false);
break;
// Buttons (retriggerable switches)
case 62:
// PlatDownWaitUpStay
if (EV_DoPlat(line,downWaitUpStay))
P_ChangeSwitchTexture(line,true);
break;
case 63:
// Raise Door
if (EV_DoDoor(line,dnormal))
P_ChangeSwitchTexture(line,true);
break;
case 70:
// Turbo Lower Floor
if (EV_DoFloor(line,turboLower))
P_ChangeSwitchTexture(line,true);
break;
}
return true;
}