-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunc_doom_door.as
468 lines (405 loc) · 12.9 KB
/
func_doom_door.as
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
const int SF_DOOR_START_OPEN = 1;
const int SF_DOOR_USE_ONLY = 256;
const int SF_DOOR_NO_AUTO_RETURN = 32;
const int FL_DOOR_BUTTON_DONT_MOVE = 2;
void reset_but(EHandle h_ent)
{
if (!h_ent.IsValid())
return;
func_doom_door@ ent = cast<func_doom_door@>(CastToScriptClass(h_ent.GetEntity()));
ent.ButtonReset();
}
void delay_use(EHandle button, EHandle pActivator, int useType, float value, bool wasShot)
{
if (!button.IsValid() or !pActivator.IsValid())
return;
func_doom_door@ but = cast<func_doom_door@>(CastToScriptClass(button.GetEntity()));
but.Useit(pActivator.GetEntity(), pActivator.GetEntity(), USE_TYPE(useType), value, wasShot);
}
class func_doom_door : ScriptBaseEntity
{
Vector m_vecPosition1;
Vector m_vecPosition2;
Vector m_vecFinalDest;
int m_toggle_state;
int dir;
float m_flLip;
float m_flWait;
float lastCrush;
bool m_bIsReopening;
bool isButton;
bool always_use;
int lock = 0; // keys required to open (bitfield)
int sounds = 0;
bool touch_opens = false;
bool isCrusher = false;
bool shootable = false;
Vector useDir;
string sync;
float attn = 0.6f;
string switchSnd;
string openSnd;
string closeSnd;
array<EHandle> sync_buttons; // buttons to move with the door
EHandle parent;
bool KeyValue( const string& in szKey, const string& in szValue )
{
if (szKey == "dir") dir = atoi(szValue) == 1 ? 1 : -1;
else if (szKey == "lip") m_flLip = atof(szValue);
else if (szKey == "wait") m_flWait = atof(szValue);
else if (szKey == "speed") pev.speed = atof(szValue);
else if (szKey == "sounds") sounds = atoi(szValue);
else if (szKey == "sync") sync = szValue;
else if (szKey == "always_use") always_use = atoi(szValue) != 0;
else if (szKey == "touch_opens") touch_opens = atoi(szValue) != 0;
else if (szKey == "crusher") isCrusher = atoi(szValue) != 0;
else if (szKey == "lock") lock = atoi(szValue);
else if (szKey == "use_dir") useDir = parseVector(szValue);
else if (szKey == "shootable") shootable = atoi(szValue) != 0;
else return BaseClass.KeyValue( szKey, szValue );
return true;
}
void Precache()
{
PrecacheSound("doom/dsbdcls.wav"); // close quick
PrecacheSound("doom/dsdorcls.wav"); // close
PrecacheSound("doom/dsbdopn.wav"); // open quick
PrecacheSound("doom/dsdoropn.wav"); // open
PrecacheSound("doom/dsswtchn.wav"); // switch sound
PrecacheSound("doom/dsswtchx.wav"); // switch sound2
PrecacheSound("doom/dspstop.wav"); // floor stop
PrecacheSound("doom/dspstart.wav"); // floor start
PrecacheSound("doom/dsstnmov.wav"); // floor move
}
void Spawn()
{
Precache();
pev.movetype = MOVETYPE_PUSH;
pev.solid = SOLID_BSP;
pev.angles = g_vecZero;
pev.takedamage = shootable ? DAMAGE_YES : DAMAGE_NO;
g_EntityFuncs.SetOrigin(self, pev.origin);
g_EntityFuncs.SetModel(self, pev.model);
if (pev.speed == 0)
pev.speed = 100;
if (isCrusher)
m_flWait = 0.001f;
m_vecPosition1 = pev.origin;
// Subtract 2 from size because the engine expands bboxes by 1 in all directions making the size too big
m_vecPosition2 = m_vecPosition1 + Vector(0,0,(dir * (pev.size.z-2)) - dir*m_flLip);
isButton = string(pev.target).Length() > 0;
if (isButton)
{
if (sounds == 0)
switchSnd = "doom/dsswtchn.wav";
else
switchSnd = "doom/dsswtchx.wav";
}
else
{
if (sounds == 0)
{
openSnd = "doom/dsdoropn.wav";
closeSnd = "doom/dsdorcls.wav";
}
else if (sounds == 1)
{
openSnd = "doom/dsbdopn.wav";
closeSnd = "doom/dsbdcls.wav";
}
else if (sounds == 2)
{
openSnd = "doom/dspstart.wav";
closeSnd = "doom/dspstop.wav";
}
else if (sounds == 3)
{
openSnd = "doom/dsstnmov.wav";
closeSnd = "doom/dspstop.wav";
}
else if (sounds == 4)
{
openSnd = "doom/dspstop.wav";
closeSnd = "doom/dspstop.wav";
}
}
switchSnd = fixPath(switchSnd);
openSnd = fixPath(openSnd);
closeSnd = fixPath(closeSnd);
if ( pev.spawnflags & SF_DOOR_START_OPEN != 0 )
{ // swap pos1 and pos2, put door at pos2
pev.origin = m_vecPosition2;
m_vecPosition2 = m_vecPosition1;
m_vecPosition1 = pev.origin;
}
m_toggle_state = TS_AT_BOTTOM;
m_bIsReopening = false;
// if the door is flagged for USE button activation only, use NULL touch function
if ( pev.spawnflags & SF_DOOR_USE_ONLY == 0 )
SetTouch( TouchFunction(Touch) );
}
void Touch(CBaseEntity@ other)
{
if (isButton)
return;
if (m_toggle_state != TS_AT_BOTTOM and m_toggle_state != TS_AT_TOP)
return;
// Ignore touches by anything but players
if (!other.IsPlayer())
return;
// If door is somebody's target, then touching does nothing.
// You have to activate the owner (e.g. button).
if (string(pev.targetname).Length() > 0)
return;
if (!touch_opens)
return; // never touch open
DoorActivate();
}
int TakeDamage( entvars_t@ pevInflictor, entvars_t@ pevAttacker, float flDamage, int bitsDamageType )
{
if (!shootable)
return 0;
if (bitsDamageType & DMG_BLAST == 0)
{
CBaseEntity@ activator = g_EntityFuncs.Instance( pevAttacker );
//Useit(activator, activator, USE_TOGGLE, 0, true);
// for some reason button won't activate if triggered now so have to wait a frame
g_Scheduler.SetTimeout("delay_use", 0.0f, EHandle(self), EHandle(activator), int(USE_TOGGLE), 0, true);
}
return 0;
}
void ButtonReset()
{
g_SoundSystem.PlaySound(self.edict(), CHAN_STATIC, switchSnd, 1.0f, attn, 0, 100);
pev.frame = 0;
}
void Useit(CBaseEntity@ pActivator, CBaseEntity@ pCaller, USE_TYPE useType, float value, bool wasShot=false)
{
if (shootable and !wasShot)
return;
int haveKey = g_keys;
if (!g_strict_keys)
{
// allow skull keys to activate normal-key doors and vice versa
lock = (lock | (lock >> 3)) & (KEY_BLUE | KEY_YELLOW | KEY_RED);
haveKey = g_keys | (g_keys >> 3);
}
if (lock != 0 and lock & haveKey != lock)
{
if (pActivator.IsPlayer())
{
string keyname = "blue";
if (lock & (KEY_YELLOW | SKULL_YELLOW) != 0)
keyname = "yellow";
if (lock & (KEY_RED | SKULL_RED) != 0)
keyname = "red";
g_PlayerFuncs.PrintKeyBindingString(cast<CBasePlayer@>(pActivator), "You need a " + keyname + " key to activate this\n");
}
return;
}
if (pCaller.IsPlayer())
{
if (useDir != g_vecZero)
{
Vector doorOri = pev.absmin + (pev.absmax - pev.absmin)*0.5f;
Vector delta = (doorOri - pCaller.pev.origin).Normalize();
//println("USE DIR: " + useDir.ToString() + " == " + delta.ToString());
if (DotProduct(delta, useDir) > 0)
return;
}
//println("Z DELTA: " + (pev.absmax.z-pCaller.pev.absmin.z));
if (pCaller.pev.absmin.z + 4 > pev.absmax.z)
{
return; // don't allow using floors from above (MAP05 secret)
}
}
// if not ready to be used, ignore "use" command.
if (isButton)
{
if (pev.frame == 0 and m_toggle_state == TS_AT_BOTTOM)
{
pev.frame = 1;
g_SoundSystem.PlaySound(self.edict(), CHAN_STATIC, switchSnd, 1.0f, attn, 0, 100);
g_EntityFuncs.FireTargets(pev.target, pActivator, self, USE_TOGGLE);
if (m_flWait > 0)
g_Scheduler.SetTimeout("reset_but", m_flWait, EHandle(self));
}
}
else if ((m_toggle_state == TS_AT_BOTTOM and useType != USE_OFF) or
(pev.spawnflags & SF_DOOR_NO_AUTO_RETURN != 0) and m_toggle_state == TS_AT_TOP or useType == USE_OFF)
{
if ((string(pev.targetname).Length() == 0 or always_use) or !pCaller.IsPlayer())
{
if (string(pev.targetname).Length() != 0 and always_use)
{
// door synced with others
CBaseEntity@ ent = null;
do {
@ent = g_EntityFuncs.FindEntityByTargetname(ent, pev.targetname);
if (ent !is null)
{
func_doom_door@ door = cast<func_doom_door@>(CastToScriptClass(ent));
door.DoorActivate(useType);
}
} while (ent !is null);
}
else
DoorActivate(useType);
}
}
}
void Use( CBaseEntity@ pActivator, CBaseEntity@ pCaller, USE_TYPE useType, float value )
{
Useit(pActivator, pCaller, useType, value, false);
}
int DoorActivate(int useType=USE_TOGGLE)
{
if (m_flWait == -1)
{
if (m_toggle_state == TS_AT_TOP and (useType == USE_TOGGLE or useType == USE_ON))
return 1;
}
if (isCrusher and useType == USE_OFF)
{
if (pev.nextthink != -1 and (sounds == 2 or sounds == 3 or sounds == 4) and closeSnd.Length() > 0)
g_SoundSystem.PlaySound(self.edict(), CHAN_STATIC, closeSnd, 1.0f, attn, 0, 100);
pev.nextthink = -1;
return 1;
}
if ((pev.spawnflags & SF_DOOR_NO_AUTO_RETURN != 0) and m_toggle_state == TS_AT_TOP or useType == USE_OFF)
{
if (m_toggle_state != TS_AT_BOTTOM and m_toggle_state != TS_GOING_DOWN)
DoorGoDown();
}
else
DoorGoUp();
return 1;
}
void DoorGoUp()
{
if (!isButton and openSnd.Length() > 0)
g_SoundSystem.PlaySound(self.edict(), CHAN_STATIC, openSnd, 1.0f, attn, sounds == 3 ? int(SND_FORCE_LOOP) : 0, 100);
m_toggle_state = TS_GOING_UP;
LinearMove(m_vecPosition2, pev.speed);
for (uint i = 0; i < sync_buttons.length(); i++)
{
if (!sync_buttons[i].IsValid())
continue;
func_doom_door@ but = cast<func_doom_door@>(CastToScriptClass(sync_buttons[i].GetEntity()));
but.DoorGoUp();
}
}
void DoorHitTop()
{
if ((sounds == 2 or sounds == 3 or sounds == 4) and closeSnd.Length() > 0)
g_SoundSystem.PlaySound(self.edict(), CHAN_STATIC, closeSnd, 1.0f, attn, 0, 100);
m_toggle_state = TS_AT_TOP;
m_bIsReopening = false;
// toggle-doors don't come down automatically, they wait for refire.
if (!isButton and pev.spawnflags & SF_DOOR_NO_AUTO_RETURN == 0)
{
// In flWait seconds, DoorGoDown will fire, unless wait is -1, then door stays open
pev.nextthink = pev.ltime + m_flWait;
SetThink( ThinkFunction(DoorGoDown) );
if ( m_flWait == -1 )
pev.nextthink = -1;
}
}
void DoorGoDown()
{
if (!isButton)
{
if (sounds == 2 or sounds == 3)
{
if (openSnd.Length() > 0)
g_SoundSystem.PlaySound(self.edict(), CHAN_STATIC, openSnd, 1.0f, attn, sounds == 3 ? int(SND_FORCE_LOOP) : 0, 100);
}
else if (closeSnd.Length() > 0)
g_SoundSystem.PlaySound(self.edict(), CHAN_STATIC, closeSnd, 1.0f, attn, 0, 100);
}
m_toggle_state = TS_GOING_DOWN;
LinearMove( m_vecPosition1, pev.speed);
for (uint i = 0; i < sync_buttons.length(); i++)
{
if (!sync_buttons[i].IsValid())
continue;
func_doom_door@ but = cast<func_doom_door@>(CastToScriptClass(sync_buttons[i].GetEntity()));
but.DoorGoDown();
}
}
void DoorHitBottom()
{
if ((sounds == 2 or sounds == 3) and closeSnd.Length() > 0)
g_SoundSystem.PlaySound(self.edict(), CHAN_STATIC, closeSnd, 1.0f, attn, 0, 100);
m_toggle_state = TS_AT_BOTTOM;
if (isCrusher)
DoorActivate();
}
void Blocked( CBaseEntity@ pOther )
{
if (isButton and parent.IsValid())
{
parent.GetEntity().Blocked(pOther);
return;
}
// Hurt the blocker a little.
if ( pev.dmg != 0 and lastCrush + 0.0572f < g_Engine.time)
{
lastCrush = g_Engine.time;
doomTakeDamage( pOther, pev, pev, pev.dmg, DMG_CRUSH );
te_bloodsprite(pOther.pev.origin + pOther.pev.view_ofs, fixPath("sprites/doom/blud.spr"), "sprites/blood.spr", 70, 5);
}
// if a door has a negative wait, it would never come back if blocked,
// so let it just squash the object to death real fast
if (m_flWait >= 0 and !isCrusher)
{
if (m_toggle_state == TS_GOING_DOWN)
DoorGoUp();
else
DoorGoDown();
}
}
void LinearMove(Vector vecDest, float flSpeed)
{
m_vecFinalDest = vecDest;
// Already there?
if (vecDest == pev.origin)
{
LinearMoveDone();
return;
}
// set destdelta to the vector needed to move
Vector vecDestDelta = vecDest - pev.origin;
// divide vector length by speed to get time to reach dest
float flTravelTime = vecDestDelta.Length() / flSpeed;
// set nextthink to trigger a call to LinearMoveDone when dest is reached
pev.nextthink = pev.ltime + flTravelTime;
SetThink( ThinkFunction(LinearMoveDone) );
// scale the destdelta vector by the time spent traveling to get velocity
pev.velocity = vecDestDelta / flTravelTime;
}
void LinearMoveDone()
{
Vector delta = m_vecFinalDest - pev.origin;
float error = delta.Length();
if ( error > 0.03125 )
{
LinearMove( m_vecFinalDest, 100 );
return;
}
pev.origin = m_vecFinalDest;
pev.velocity = g_vecZero;
pev.nextthink = -1;
if (m_toggle_state == TS_GOING_UP)
DoorHitTop();
else
DoorHitBottom();
}
void SetToggleState( int state )
{
if ( state == TS_AT_TOP )
g_EntityFuncs.SetOrigin( self, m_vecPosition2 );
else
g_EntityFuncs.SetOrigin( self, m_vecPosition1 );
}
}