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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: The "weapon" used to build objects
//
//
// $Workfile: $
// $Date: $
// $NoKeywords: $
//=============================================================================//
#include "cbase.h"
#include "tf_player.h"
#include "tf_basecombatweapon.h"
#include "EntityList.h"
#include "in_buttons.h"
#include "weapon_builder.h"
#include "tf_obj.h"
#include "sendproxy.h"
#include "weapon_objectselection.h"
#include "info_act.h"
#include "vguiscreen.h"
extern ConVar tf2_object_hard_limits;
extern ConVar tf_fastbuild;
EXTERN_SEND_TABLE(DT_BaseCombatWeapon)
IMPLEMENT_SERVERCLASS_ST(CWeaponBuilder, DT_WeaponBuilder)
SendPropInt( SENDINFO( m_iBuildState ), 4, SPROP_UNSIGNED ),
SendPropInt( SENDINFO( m_iCurrentObject ), BUILDER_OBJECT_BITS, SPROP_UNSIGNED ),
SendPropInt( SENDINFO( m_iCurrentObjectState ), 4, SPROP_UNSIGNED ),
SendPropEHandle( SENDINFO( m_hObjectBeingBuilt ) ),
SendPropTime( SENDINFO( m_flStartTime ) ),
SendPropTime( SENDINFO( m_flTotalTime ) ),
SendPropArray
(
SendPropInt( SENDINFO_ARRAY(m_bObjectValidity), 1, SPROP_UNSIGNED), m_bObjectValidity
),
SendPropArray
(
SendPropInt( SENDINFO_ARRAY(m_bObjectBuildability), 1, SPROP_UNSIGNED), m_bObjectBuildability
),
END_SEND_TABLE()
LINK_ENTITY_TO_CLASS( weapon_builder, CWeaponBuilder );
PRECACHE_WEAPON_REGISTER(weapon_builder);
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
CWeaponBuilder::CWeaponBuilder()
{
for ( int i=0; i < m_bObjectValidity.Count(); i++ )
m_bObjectValidity.Set( i, 0 );
m_iCurrentObject = BUILDER_INVALID_OBJECT;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CWeaponBuilder::Precache( void )
{
BaseClass::Precache();
PrecacheModel( "models/weapons/v_slam.mdl" );
PrecacheVGuiScreen( "screen_human_pda" );
}
//-----------------------------------------------------------------------------
// Purpose: Gets info about the control panels
//-----------------------------------------------------------------------------
void CWeaponBuilder::GetControlPanelInfo( int nPanelIndex, const char *&pPanelName )
{
pPanelName = "screen_human_pda";
}
//-----------------------------------------------------------------------------
// Purpose:
// Output : Returns true on success, false on failure.
//-----------------------------------------------------------------------------
bool CWeaponBuilder::ShouldShowControlPanels( void )
{
if ( GetActivity() == ACT_VM_IDLE )
return true;
return false;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CWeaponBuilder::UpdateOnRemove( void )
{
// Tell the player he's lost his build weapon
CBaseTFPlayer *pOwner = ToBaseTFPlayer( GetOwner() );
if ( pOwner && pOwner->GetWeaponBuilder() == this )
{
pOwner->SetWeaponBuilder( NULL );
}
// Chain at end to mimic destructor unwind order
BaseClass::UpdateOnRemove();
}
//-----------------------------------------------------------------------------
// Purpose: Builder weapon has just been given to a player
//-----------------------------------------------------------------------------
void CWeaponBuilder::Equip( CBaseCombatCharacter *pOwner )
{
BaseClass::Equip( pOwner );
((CBaseTFPlayer*)pOwner)->SetWeaponBuilder( this );
}
//-----------------------------------------------------------------------------
// Purpose: Add a new object type to this build weapon. This will allow
// the player carrying this builder to build the object.
//-----------------------------------------------------------------------------
void CWeaponBuilder::AddBuildableObject( int iObjectType )
{
m_bObjectValidity.Set( iObjectType, true );
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
bool CWeaponBuilder::CanDeploy( void )
{
if ( m_iCurrentObject != BUILDER_INVALID_OBJECT )
{
SetCurrentState( BS_PLACING );
StartPlacement();
m_flNextPrimaryAttack = gpGlobals->curtime + 0.35f;
}
else
{
m_hObjectBeingBuilt = NULL;
SetCurrentState( BS_IDLE );
SetCurrentObject( m_iCurrentObject );
}
return BaseClass::CanDeploy();
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
bool CWeaponBuilder::Deploy( )
{
#if !defined( CLIENT_DLL )
if ( m_hObjectBeingBuilt.Get() && m_hObjectBeingBuilt->IsAnUpgrade() )
return DefaultDeploy( (char*)GetViewModel(), (char*)GetWorldModel(), ACT_SLAM_STICKWALL_ND_DRAW, (char*)GetAnimPrefix() );
return DefaultDeploy( (char*)GetViewModel(), (char*)GetWorldModel(), ACT_VM_DRAW, (char*)GetAnimPrefix() );
#else
return true;
#endif
}
//-----------------------------------------------------------------------------
// Purpose: Prevent switching when working on something
//-----------------------------------------------------------------------------
bool CWeaponBuilder::CanHolster( void )
{
if ( IsBuilding() )
return false;
return BaseClass::CanHolster();
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
CBaseCombatWeapon *CWeaponBuilder::GetLastWeapon( void )
{
return BaseClass::GetLastWeapon();
}
//-----------------------------------------------------------------------------
// Purpose: Stop placement when holstering
//-----------------------------------------------------------------------------
bool CWeaponBuilder::Holster( CBaseCombatWeapon *pSwitchingTo )
{
if ( m_iBuildState == BS_PLACING || m_iBuildState == BS_PLACING_INVALID )
{
SetCurrentState( BS_IDLE );
}
StopPlacement();
return BaseClass::Holster(pSwitchingTo);
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CWeaponBuilder::ItemPostFrame( void )
{
CBaseTFPlayer *pOwner = ToBaseTFPlayer( GetOwner() );
if ( !pOwner )
return;
// Ignore input while the player's building anything
if ( pOwner->IsBuilding() )
return;
// Switch away if I'm not in placement mode
if ( m_iBuildState != BS_PLACING && m_iBuildState != BS_PLACING_INVALID )
{
pOwner->SwitchToNextBestWeapon( NULL );
return;
}
if (( pOwner->m_nButtons & IN_ATTACK ) && (m_flNextPrimaryAttack <= gpGlobals->curtime) )
{
PrimaryAttack();
}
// Allow shield post frame
AllowShieldPostFrame( true );
WeaponIdle();
}
//-----------------------------------------------------------------------------
// Purpose: Start placing or building the currently selected object
//-----------------------------------------------------------------------------
void CWeaponBuilder::PrimaryAttack( void )
{
CBaseTFPlayer *pOwner = ToBaseTFPlayer( GetOwner() );
if ( !pOwner )
return;
// What state should we move to?
switch( m_iBuildState )
{
case BS_IDLE:
{
// Idle state starts selection
SetCurrentState( BS_SELECTING );
}
break;
case BS_SELECTING:
{
// Do nothing, client handles selection
return;
}
break;
case BS_PLACING:
{
if ( m_hObjectBeingBuilt )
{
// Give the object a chance to veto the "start building" command. Objects like barbed wire
// may want to change their properties instead of actually building yet.
if ( m_hObjectBeingBuilt->PreStartBuilding() )
{
int iFlags = m_hObjectBeingBuilt->GetObjectFlags();
// Can't build if the game hasn't started
if ( !tf_fastbuild.GetInt() && CurrentActIsAWaitingAct() )
{
ClientPrint( pOwner, HUD_PRINTCENTER, "Can't build until the game's started.\n" );
return;
}
StartBuilding();
// Should we switch away?
if ( iFlags & OF_ALLOW_REPEAT_PLACEMENT )
{
// Start placing another
SetCurrentState( BS_PLACING );
StartPlacement();
}
else
{
pOwner->SwitchToNextBestWeapon( NULL );
}
}
}
}
break;
case BS_PLACING_INVALID:
{
WeaponSound( SINGLE_NPC );
// If there is any associated error text when placing the object, display it
if( m_hObjectBeingBuilt != NULL )
{
if (m_hObjectBeingBuilt->MustBeBuiltInResourceZone())
{
ClientPrint( pOwner, HUD_PRINTCENTER, "Only placeable in an empty resource zone.\n" );
}
else if (m_hObjectBeingBuilt->MustBeBuiltInConstructionYard())
{
ClientPrint( pOwner, HUD_PRINTCENTER, "Only placeable in a construction yard.\n" );
}
}
}
break;
}
m_flNextPrimaryAttack = gpGlobals->curtime + 0.2f;
}
//-----------------------------------------------------------------------------
// Purpose: Set the builder to the specified state
//-----------------------------------------------------------------------------
void CWeaponBuilder::SetCurrentState( int iState )
{
// Check the current build state... we may need to shut some stuff down...
switch(m_iBuildState)
{
case BS_PLACING:
case BS_PLACING_INVALID:
{
if ((iState != BS_PLACING) && (iState != BS_PLACING_INVALID) && (iState != BS_BUILDING))
{
StopPlacement();
WeaponSound( SPECIAL1 );
}
}
break;
}
m_iBuildState = iState;
}
//-----------------------------------------------------------------------------
// Purpose: Set the builder to the specified object
//-----------------------------------------------------------------------------
void CWeaponBuilder::SetCurrentObject( int iObject )
{
// Fixup for invalid objects
if (iObject < 0)
iObject = BUILDER_INVALID_OBJECT;
CBaseTFPlayer *pOwner = ToBaseTFPlayer( GetOwner() );
if ( !pOwner )
return;
int i;
// If -1 was passed in, set to our first available object
if ( iObject == BUILDER_INVALID_OBJECT )
{
for ( i = 0; i < OBJ_LAST; i++ )
{
if ( m_bObjectValidity[i] )
{
iObject = i;
break;
}
}
}
// Recalculate the buildability of each object (for propagation to the client)
for ( i=0; i < m_bObjectBuildability.Count(); i++ )
m_bObjectBuildability.Set( i, 0 );
for ( i = 0; i < OBJ_LAST; i++ )
{
if ( m_bObjectValidity[i] && pOwner->CanBuild(i) == CB_CAN_BUILD )
{
m_bObjectBuildability.Set( i, true );
}
}
m_iCurrentObject = iObject;
m_iCurrentObjectState = pOwner->CanBuild( m_iCurrentObject );
m_flStartTime = 0;
m_flTotalTime = 0;
}
//-----------------------------------------------------------------------------
// Purpose: Idle updates the position of the build placement model
//-----------------------------------------------------------------------------
void CWeaponBuilder::WeaponIdle( void )
{
CBaseTFPlayer *pOwner = ToBaseTFPlayer( GetOwner() );
if ( !pOwner )
return;
// If we're in placement mode, update the placement model
switch( m_iBuildState )
{
case BS_PLACING:
case BS_PLACING_INVALID:
{
if ( UpdatePlacement() )
{
SetCurrentState( BS_PLACING );
}
else
{
SetCurrentState( BS_PLACING_INVALID );
}
}
break;
default:
break;
}
if ( HasWeaponIdleTimeElapsed() )
{
SendWeaponAnim( ACT_VM_IDLE );
}
}
//-----------------------------------------------------------------------------
// Purpose: The player holding this weapon has just gained new technology.
// Check to see if it affects the medikit
//-----------------------------------------------------------------------------
void CWeaponBuilder::GainedNewTechnology( CBaseTechnology *pTechnology )
{
CBaseTFPlayer *pPlayer = ToBaseTFPlayer( GetOwner() );
if ( pPlayer )
{
// Force a recalculation of the state for this object
SetCurrentObject( m_iCurrentObject );
}
}
//-----------------------------------------------------------------------------
// Purpose: Start placing the object
//-----------------------------------------------------------------------------
void CWeaponBuilder::StartPlacement( void )
{
StopPlacement();
// Create the slab
m_hObjectBeingBuilt = (CBaseObject*)CreateEntityByName( GetObjectInfo( m_iCurrentObject )->m_pClassName );
if ( m_hObjectBeingBuilt )
{
m_hObjectBeingBuilt->Spawn();
m_hObjectBeingBuilt->StartPlacement( ToBaseTFPlayer( GetOwner() ) );
UpdatePlacement();
// Stomp this here in the same frame we make the object, so prevent clientside warnings that it's under attack
m_hObjectBeingBuilt->m_iHealth = OBJECT_CONSTRUCTION_STARTINGHEALTH;
}
}
//-----------------------------------------------------------------------------
// Purpose: Set the viewmodel according to the type of object we're placing
//-----------------------------------------------------------------------------
const char *CWeaponBuilder::GetViewModel( int viewmodelindex /*=0*/ ) const
{
if ( m_hObjectBeingBuilt.Get() && m_hObjectBeingBuilt->IsAnUpgrade() )
return "models/weapons/v_slam.mdl";
return BaseClass::GetViewModel( viewmodelindex );
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CWeaponBuilder::StopPlacement( void )
{
if ( m_hObjectBeingBuilt )
{
m_hObjectBeingBuilt->StopPlacement();
m_hObjectBeingBuilt = NULL;
}
}
//-----------------------------------------------------------------------------
// Purpose: Move the placement model to the current position. Return false if it's an invalid position
//-----------------------------------------------------------------------------
bool CWeaponBuilder::UpdatePlacement( void )
{
if ( !m_hObjectBeingBuilt )
return false;
return m_hObjectBeingBuilt->UpdatePlacement( ToBaseTFPlayer(GetOwner()) );
}
//-----------------------------------------------------------------------------
// Purpose: Player holding this weapon has started building something
//-----------------------------------------------------------------------------
void CWeaponBuilder::StartBuilding( void )
{
if ( m_hObjectBeingBuilt.Get() && UpdatePlacement() )
{
SetCurrentState( BS_BUILDING );
m_hObjectBeingBuilt->StartBuilding( GetOwner() );
}
}
//-----------------------------------------------------------------------------
// Purpose: Player holding this weapon has aborted the build of an object
//-----------------------------------------------------------------------------
void CWeaponBuilder::StoppedBuilding( int iObjectType )
{
CBaseTFPlayer *pPlayer = ToBaseTFPlayer( GetOwner() );
if ( pPlayer )
{
// Force a recalculation of the state for this object
SetCurrentObject( m_iCurrentObject );
SetCurrentState( BS_IDLE );
WeaponSound( SPECIAL2 );
}
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
bool CWeaponBuilder::IsBuilding( void )
{
return ( m_iBuildState == BS_BUILDING );
}
//-----------------------------------------------------------------------------
// Purpose: The player holding this weapon has just finished building an object
//-----------------------------------------------------------------------------
void CWeaponBuilder::FinishedObject( void )
{
CBaseTFPlayer *pPlayer = ToBaseTFPlayer( GetOwner() );
if ( pPlayer )
{
// We're no longer building anything...
m_hObjectBeingBuilt = NULL;
// Force a recalculation of the state for this object
SetCurrentObject( m_iCurrentObject );
SetCurrentState( BS_IDLE );
}
}
|