-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClientManager.cpp
741 lines (593 loc) · 32.7 KB
/
ClientManager.cpp
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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
//
// Copyright © 2003-2010, by YaPB Development Team. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
// ClientManager.cpp
//
// Class: ClientManager
//
// Version: $ID:$
//
#include <Core.hpp>
void ClientManager::ClientTeamSwitchedToTerrorists (const unsigned char clientEdictIndex)
{
// This function called after client/bot switch team to terrorists.
// Reliability check.
InternalAssert (HalfLifeEngine::Utilities::IsPlayerEdictIndex (clientEdictIndex));
Client *const client (m_clients[clientEdictIndex - 1u]);
// Reliability check.
// InternalAssert (client->IsValid ()); // OCCURS!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
if (!client->IsValid ())
return;
// Reliability check. (Same team?)
if (client->m_currentTeamID == HalfLifeEngine::SDK::Constants::TeamID_Terrorist)
{
#if defined _DEBUG
Label_Check:
{
// AddLogEntry (true, LogLevel_Default, true, "CM::ClientTeamSwitchedToTerrorists(): Called for client \"%s\", m_currentTeamID=%i, BasePlayer::m_team=%i, Edict::IsAlive()=%i, Client::IsAlive()=%i.", client->GetName ().GetData (), client->m_currentTeamID, client->GetRealTeam (), client->GetEdict ()->IsAlive (), client->IsAlive ());
// Reliability checks.
InternalAssert (m_teamClients[TeamArrayID_Terrorist].IsMember (client));
InternalAssert (!m_teamClients[TeamArrayID_CounterTerrorist].IsMember (client));
InternalAssert (client->IsAlive () == m_teamAliveClients[TeamArrayID_Terrorist].IsMember (client));
InternalAssert (!m_teamAliveClients[TeamArrayID_CounterTerrorist].IsMember (client));
}
#endif // if defined _DEBUG
return;
}
if (m_teamClients[TeamArrayID_CounterTerrorist].Pop (client))
m_teamAliveClients[TeamArrayID_CounterTerrorist] -= client;
if (!m_teamClients[TeamArrayID_Terrorist].IsMember (client))
m_teamClients[TeamArrayID_Terrorist] += client;
if (client->IsAlive () && !m_teamAliveClients[TeamArrayID_Terrorist].IsMember (client))
m_teamAliveClients[TeamArrayID_Terrorist] += client;
if (client->IsFakeClient ())
{
g_server->GetFakeClientManager ()->ClientTeamSwitchedToTerrorists (client->GetFakeClientPointer ());
/*! @warning "New message started when msg '86' has not been sent yet" - @todo IMPROVE THIS
if (client->IsYaPB ())
for (HumanManager::HumansArray_t::IndexType_t index (0u); index < g_server->GetHumanManager ()->GetHumansCount (); ++index)
{
Human *const human (g_server->GetHumanManager ()->GetHumansArray ()[index]);
if (human->GetOpenedYaPBMenu ().id == MenuID_BotKickMenu¹1)
g_menusManager.Draw (human); // Redraw kick YaPB menu....
}*/
}
client->TeamChanged (HalfLifeEngine::SDK::Constants::TeamID_Terrorist);
#if defined _DEBUG
goto Label_Check;
#endif // if defined _DEBUG
}
void ClientManager::ClientTeamSwitchedToCounterTerrorists (const unsigned char clientEdictIndex)
{
// This function called after client/bot switch team to counter-terrorists.
// Reliability check.
InternalAssert (HalfLifeEngine::Utilities::IsPlayerEdictIndex (clientEdictIndex));
Client *const client (m_clients[clientEdictIndex - 1u]);
// Reliability check.
// InternalAssert (client->IsValid ()); // OCCURS!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
if (!client->IsValid ())
return;
// Reliability check. (Same team?)
if (client->m_currentTeamID == HalfLifeEngine::SDK::Constants::TeamID_CounterTerrorist)
{
#if defined _DEBUG
Label_Check:
{
// AddLogEntry (true, LogLevel_Default, true, "CM::ClientTeamSwitchedToCounterTerrorists(): Called for client \"%s\", m_currentTeamID=%i, BasePlayer::m_team=%i, Edict::IsAlive()=%i, IsAlive()=%i.", client->GetName ().GetData (), client->m_currentTeamID, client->GetRealTeam (), client->GetEdict ()->IsAlive (), client->IsAlive ());
// Reliability checks.
InternalAssert (!m_teamClients[TeamArrayID_Terrorist].IsMember (client));
InternalAssert (m_teamClients[TeamArrayID_CounterTerrorist].IsMember (client));
InternalAssert (!m_teamAliveClients[TeamArrayID_Terrorist].IsMember (client));
InternalAssert (client->IsAlive () == m_teamAliveClients[TeamArrayID_CounterTerrorist].IsMember (client));
}
#endif // if defined _DEBUG
return;
}
if (m_teamClients[TeamArrayID_Terrorist].Pop (client))
m_teamAliveClients[TeamArrayID_Terrorist] -= client;
if (!m_teamClients[TeamArrayID_CounterTerrorist].IsMember (client))
m_teamClients[TeamArrayID_CounterTerrorist] += client;
if (client->IsAlive () && !m_teamAliveClients[TeamArrayID_CounterTerrorist].IsMember (client))
m_teamAliveClients[TeamArrayID_CounterTerrorist] += client;
if (client->IsFakeClient ())
{
g_server->GetFakeClientManager ()->ClientTeamSwitchedToCounterTerrorists (client->GetFakeClientPointer ());
/*! @warning "New message started when msg '86' has not been sent yet" - @todo IMPROVE THIS
if (client->IsYaPB ())
for (HumanManager::HumansArray_t::IndexType_t index (0u); index < g_server->GetHumanManager ()->GetHumansCount (); ++index)
{
Human *const human (g_server->GetHumanManager ()->GetHumansArray ()[index]);
if (human->GetOpenedYaPBMenu ().id == MenuID_BotKickMenu¹1)
g_menusManager.Draw (human); // Redraw kick YaPB menu....
}*/
}
client->TeamChanged (HalfLifeEngine::SDK::Constants::TeamID_CounterTerrorist);
#if defined _DEBUG
goto Label_Check;
#endif // if defined _DEBUG
}
void ClientManager::ClientTeamSwitchedToSpectators (const unsigned char clientEdictIndex, const bool isNewTeamUnassigned)
{
// This function called after client/bot switch team to spectators.
// Reliability check.
InternalAssert (HalfLifeEngine::Utilities::IsPlayerEdictIndex (clientEdictIndex));
Client *const client (m_clients[clientEdictIndex - 1u]);
// Reliability check.
// InternalAssert (client->IsValid ()); // OCCURS!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
if (!client->IsValid ())
return;
const HalfLifeEngine::SDK::Constants::TeamID_t newTeamID (isNewTeamUnassigned ? HalfLifeEngine::SDK::Constants::TeamID_Unassigned : HalfLifeEngine::SDK::Constants::TeamID_Spectator);
// Reliability check. (Same team?)
if (client->m_currentTeamID == newTeamID)
{
#if defined _DEBUG
Label_Check:
{
// AddLogEntry (true, LogLevel_Default, true, "CM::ClientTeamSwitchedToSpectators(): Called for client \"%s\", m_currentTeamID=%i, BasePlayer::m_team=%i, Edict::IsAlive()=%i, IsAlive()=%i.", client->GetName ().GetData (), client->m_currentTeamID, client->GetRealTeam (), client->GetEdict ()->IsAlive (), client->IsAlive ());
// Reliability checks.
InternalAssert (!m_teamClients[TeamArrayID_Terrorist].IsMember (client));
InternalAssert (!m_teamAliveClients[TeamArrayID_Terrorist].IsMember (client));
InternalAssert (!m_teamClients[TeamArrayID_CounterTerrorist].IsMember (client));
InternalAssert (!m_teamAliveClients[TeamArrayID_CounterTerrorist].IsMember (client));
}
#endif // if defined _DEBUG
return;
}
// Just remove this client....
if (m_teamClients[TeamArrayID_Terrorist].Pop (client))
m_teamAliveClients[TeamArrayID_Terrorist] -= client;
else if (m_teamClients[TeamArrayID_CounterTerrorist].Pop (client))
m_teamAliveClients[TeamArrayID_CounterTerrorist] -= client;
if (client->IsFakeClient ())
{
g_server->GetFakeClientManager ()->ClientTeamSwitchedToSpectators (client->GetFakeClientPointer (), isNewTeamUnassigned);
/*! @warning "New message started when msg '86' has not been sent yet" - @todo IMPROVE THIS
if (client->IsYaPB ())
for (HumanManager::HumansArray_t::IndexType_t index (0u); index < g_server->GetHumanManager ()->GetHumansCount (); ++index)
{
Human *const human (g_server->GetHumanManager ()->GetHumansArray ()[index]);
if (human->GetOpenedYaPBMenu ().id == MenuID_BotKickMenu¹1)
g_menusManager.Draw (human); // Redraw kick YaPB menu....
}*/
}
client->TeamChanged (newTeamID);
#if defined _DEBUG
goto Label_Check;
#endif // if defined _DEBUG
}
void ClientManager::ClientSpawnPost (const HalfLifeEngine::SDK::Classes::Edict *const client)
{
// This function called after each client/bot spawn, also after bot creation.
/// @warning IN THIS FUNCTION WE SHOULD USE ONLY 'HalfLifeEngine::SDK::Classes::Edict::IsAlive()' FUNCTION!
// Reliability checks.
InternalAssert (client->IsValid () && client->IsNotWorldspawnPlayer ());
const ClientsArray_t::IndexType_t clientIndex (static_cast <const ClientsArray_t::IndexType_t> (client->GetIndex ()) - 1u);
// Reliability check.
InternalAssert (m_clients[clientIndex]->IsValid ());
m_clients[clientIndex]->SpawnPost ();
// Not alive?
if (!m_clients[clientIndex]->GetEdict ()->IsAlive ())
return;
char clientRealTeam (static_cast <char> (m_clients[clientIndex]->GetRealTeam ()));
// Didn't join a team yet?
if (clientRealTeam == HalfLifeEngine::SDK::Constants::TeamID_Unassigned)
return;
// Player spawned being alive.
--clientRealTeam;
// Client can spawn twice being alive!
if (!m_teamAliveClients[clientRealTeam].IsMember (m_clients[clientIndex]))
{
m_teamAliveClients[clientRealTeam] += m_clients[clientIndex];
// AddLogEntry (true, LogLevel_Default, true, "CM::ClientSpawnPost(): Called for client \"%s\", m_currentTeamID=%i, BasePlayer::m_team=%i.", m_clients[clientIndex]->GetName ().GetData (), m_clients[clientIndex]->m_currentTeamID, m_clients[clientIndex]->GetRealTeam ());
}
if (m_clients[clientIndex]->IsFakeClient ())
g_server->GetFakeClientManager ()->ClientAliveSpawnPost (m_clients[clientIndex]->GetFakeClientPointer ());
m_clients[clientIndex]->AliveSpawnPost ();
}
void ClientManager::ClientRoundRespawnPost (const HalfLifeEngine::SDK::Classes::Edict *const client)
{
// This function called after each client/bot spawn, also after bot creation.
// Reliability checks.
InternalAssert (client->IsValid () && client->IsNotWorldspawnPlayer ());
const ClientsArray_t::IndexType_t clientIndex (static_cast <const ClientsArray_t::IndexType_t> (client->GetIndex ()) - 1u);
// Reliability check.
InternalAssert (m_clients[clientIndex]->IsValid ());
m_clients[clientIndex]->RoundRespawnPost ();
}
void ClientManager::ClientUse (const HalfLifeEngine::SDK::Classes::Edict *const client, const HalfLifeEngine::SDK::Classes::Edict *const entity, const HalfLifeEngine::SDK::Classes::Edict *const caller, const HalfLifeEngine::SDK::Constants::UseType_t type, const float value) const
{
// Reliability check.
InternalAssert (client->IsValid () && client->IsNotWorldspawnPlayer ());
InternalAssert (caller->IsValid ());
const ClientsArray_t::IndexType_t clientIndex (static_cast <const ClientsArray_t::IndexType_t> (client->GetIndex ()) - 1u);
// Reliability check.
InternalAssert (m_clients[clientIndex]->IsValid ());
/*
if (!client->IsYaPB ())
{
// loop through all client/bot slots
for (ClientsArray_t::IndexType_t index (0u); index < m_clientsArray.GetElementNumber (); ++index)
{
// if this client slot not used, or client isn't this bot (YaPB), or bot has no target button
if (!m_clientsArray[index]->IsYaPB () || !m_clientsArray[index]->GetYaPBPointer ()->m_button->IsValid () || m_clientsArray[index]->GetYaPBPointer ()->m_button != entity)
continue;
// button was pressed by another client/bot, so reset button target
m_clientsArray[index]->GetYaPBPointer ()->m_button = NULL;
}
}
*/
m_clients[clientIndex]->Use (entity, caller, type, value);
}
void ClientManager::ClientDisconnect (const HalfLifeEngine::SDK::Classes::Edict *const client)
{
// This function frees one client selected by index (used on client disconnect).
// Reliability check.
InternalAssert (client->IsValid () && client->IsNotWorldspawnPlayer ());
/*
#if defined _DEBUG
AddLogEntry (true, LogLevel_Default, false, "ClientManager::ClientDisconnect(): called for client: \"%s\".", client->GetNetName ().GetData ());
#endif // if defined _DEBUG
*/
const ClientsArray_t::IndexType_t clientIndex (static_cast <const ClientsArray_t::IndexType_t> (client->GetIndex ()) - 1u);
// Reliability check.
InternalAssert (m_clients[clientIndex]->IsValid ());
// Record some Stats of all Players on the Server
{
m_clientsArray -= m_clients[clientIndex];
if (m_clients[clientIndex]->IsFakeClient ())
{
g_server->GetFakeClientManager ()->ClientDisconnect (m_clients[clientIndex]->GetFakeClientPointer ());
if (m_clients[clientIndex]->IsOtherBot ())
g_server->GetFakeClientNotYaPBManager ()->FakeClientNotYaPBDisconnect (m_clients[clientIndex]->GetFakeClientNotYaPBPointer ());
else if (m_clients[clientIndex]->IsYaPB ())
{
g_server->GetYaPBManager ()->YaPBDisconnect (m_clients[clientIndex]->GetYaPBPointer ());
for (HumanManager::HumansArray_t::IndexType_t index (0u); index < g_server->GetHumanManager ()->GetHumansCount (); ++index)
{
Human *const human (g_server->GetHumanManager ()->GetHumansArray ()[index]);
if (human->GetOpenedYaPBMenu ().id == MenuID_BotKickMenu¹1)
g_menusManager.Draw (human); // Redraw kick YaPB menu....
}
}
}
else
{
// Reliability check.
InternalAssert (m_clients[clientIndex]->IsHuman ());
g_server->GetHumanManager ()->HumanDisconnect (m_clients[clientIndex]->GetHumanPointer ());
}
{
char clientRealTeam (static_cast <char> (m_clients[clientIndex]->GetRealTeam ()));
if (!HalfLifeEngine::Utilities::IsSpectatorTeam (static_cast <HalfLifeEngine::SDK::Constants::TeamID_t> (clientRealTeam)))
{
--clientRealTeam;
m_teamClients[clientRealTeam] -= m_clients[clientIndex];
if (m_clients[clientIndex]->IsAlive ())
{
InternalAssert (m_teamAliveClients[clientRealTeam].IsMember (m_clients[clientIndex]));
//if (!m_teamAliveClients[clientRealTeam].IsMember (m_clients[clientIndex]))
// AddLogEntry (false, LogLevel_Critical, true, "client \"%s\" [IsAlive () && !m_teamAliveClients[clientRealTeam].IsMember (client)].", m_clients[clientIndex]->GetName ().GetData ());
m_teamAliveClients[clientRealTeam] -= m_clients[clientIndex];
}
#if defined _DEBUG
else
InternalAssert (!m_teamAliveClients[clientRealTeam].IsMember (m_clients[clientIndex]));
#endif // if defined _DEBUG
}
}
if (m_clients[clientIndex]->m_lastNavigationArea != NULL)
m_clients[clientIndex]->m_lastNavigationArea->RemovePlayer (m_clients[clientIndex]);
g_server->GetYaPBManager ()->OnClientDisconnect (m_clients[clientIndex]);
}
// Delete him/her/it from the list....
delete m_clients[clientIndex]; // Call destructor for this client,
m_clients[clientIndex] = NULL; // and reset pointer.
}
void ClientManager::ClientPutInServer (HalfLifeEngine::SDK::Classes::Edict *const client)
{
/// @note This function is not called for YaPB's!
// Reliability check.
InternalAssert (client->IsValid () && client->IsNotWorldspawnPlayer ());
/*
#if defined _DEBUG
AddLogEntry (true, LogLevel_Default, false, "ClientManager::ClientPutInServer(): called for client: \"%s\".", client->GetNetName ().GetData ());
#endif // if defined _DEBUG
*/
const ClientsArray_t::IndexType_t clientIndex (static_cast <const ClientsArray_t::IndexType_t> (client->GetIndex ()) - 1u);
// Reliability check.
InternalAssert (m_clients[clientIndex] == NULL);
if (client->IsPlayerHuman ())
{
g_server->GetYaPBManager ()->CheckAutoVacate ();
#if defined _DEBUG
if ((GetClientsCount () == 0u && !HalfLifeEngine::Globals::g_halfLifeEngine->IsDedicatedServer ()) || HalfLifeEngine::SDK::Network::NET_IsReservedAdr (g_server->GetStatic ().clients[clientIndex].netchan.remote_address))
#else
// If first human connect and listen server - create the host. (Host is always first :))
if (GetClientsCount () == 0u && !HalfLifeEngine::Globals::g_halfLifeEngine->IsDedicatedServer ())
#endif // if !defined _DEBUG
{
Host *const newHost (new Host (client));
#if defined _DEBUG
// Reliability check.
if (newHost == NULL)
{
AddLogEntry (true, LogLevel_Error, false, "Couldn't allocate %u bytes for creating host \"%s\"!", sizeof (*newHost), client->GetNetName ().GetData ());
return;
}
#endif // if defined _DEBUG
// add him/her to the lists
m_clients[clientIndex] = newHost;
g_server->GetHumanManager ()->HumanConnect (newHost);
/*
#if defined _DEBUG
AddLogEntry (true, LogLevel_Default, false, "ClientManager::ClientPutInServer(): host \"%s\" created succesfully! (0x%u)", client->GetNetName ().GetData (), sizeof (*newHost));
#endif // if defined _DEBUG
*/ }
else // Else - create player.
{
Player *const newPlayer (new Player (client));
#if defined _DEBUG
// Reliability check.
if (newPlayer == NULL)
{
AddLogEntry (true, LogLevel_Error, false, "Couldn't allocate %u bytes for creating player \"%s\"!", sizeof (*newPlayer), client->GetNetName ().GetData ());
return;
}
#endif // if defined _DEBUG
// add him/her to the lists
m_clients[clientIndex] = newPlayer;
g_server->GetHumanManager ()->HumanConnect (newPlayer);
/*
#if defined _DEBUG
AddLogEntry (true, LogLevel_Default, false, "ClientManager::ClientPutInServer(): player \"%s\" created succesfully! (0x%u)", client->GetNetName ().GetData (), sizeof (*newPlayer));
#endif // if defined _DEBUG
*/ }
}
else if (client->IsPlayerZBot ())
{
ZBot *const newZBot (new ZBot (client));
#if defined _DEBUG
// Reliability check.
if (newZBot == NULL)
{
AddLogEntry (true, LogLevel_Error, false, "Couldn't allocate %u bytes for creating Z bot \"%s\"!", sizeof (*newZBot), client->GetNetName ().GetData ());
return;
}
#endif // if defined _DEBUG
// add him/her to the lists
m_clients[clientIndex] = newZBot;
g_server->GetFakeClientManager ()->ClientConnect (newZBot);
// g_server->GetZBotManager ()->ZBotConnect (newZBot);
/*
#if defined _DEBUG
AddLogEntry (true, LogLevel_Default, false, "ClientManager::ClientPutInServer(): Z bot \"%s\" created succesfully! (0x%u)", client->GetNetName ().GetData (), sizeof (*newZBot));
#endif // if defined _DEBUG
*/ }
else
{
FakeClient_NotYaPB *const newFakeClientNotYaPB (new FakeClient_NotYaPB (client));
#if defined _DEBUG
// Reliability check.
if (newFakeClientNotYaPB == NULL)
{
AddLogEntry (true, LogLevel_Error, false, "Couldn't allocate %u bytes for creating other bot \"%s\"!", sizeof (*newFakeClientNotYaPB), client->GetNetName ().GetData ());
return;
}
#endif // if defined _DEBUG
// add him/her to the lists
m_clients[clientIndex] = newFakeClientNotYaPB;
g_server->GetFakeClientManager ()->ClientConnect (newFakeClientNotYaPB);
g_server->GetFakeClientNotYaPBManager ()->FakeClientNotYaPBConnect (newFakeClientNotYaPB);
/*
#if defined _DEBUG
AddLogEntry (true, LogLevel_Default, false, "ClientManager::ClientPutInServer(): other bot \"%s\" created succesfully! (0x%u)", client->GetNetName ().GetData (), sizeof (*newFakeClientNotYaPB));
#endif // if defined _DEBUG
*/ }
m_clientsArray += m_clients[clientIndex];
}
void ClientManager::ClientUserInfoChanged (HalfLifeEngine::SDK::Classes::Edict *const client, const DynamicString &infoBuffer)
{
// Reliability check.
InternalAssert (client->IsValid () && client->IsNotWorldspawnPlayer ());
/*
#if defined _DEBUG
AddLogEntry (true, LogLevel_Default, false, "ClientManager::ClientUserInfoChanged(): called for client: \"%s\", infoBuffer: \"%s\".", client->GetNetName ().GetData (), infoBuffer.GetData ());
#endif // if defined _DEBUG
*/
const ClientsArray_t::IndexType_t clientIndex (static_cast <const ClientsArray_t::IndexType_t> (client->GetIndex ()) - 1u);
// Client::IsHuman() check is added since if client entity is bot, flag 'HalfLifeEngine::SDK::Constants::FL_FAKECLIENT' is not added, yet.
if (m_clients[clientIndex]->IsValid () && m_clients[clientIndex]->IsHuman ())
m_clients[clientIndex]->GetHumanPointer ()->UserInfoChanged (infoBuffer);
}
void ClientManager::Think (void) const
{
// This function calls Think() function for all available at call moment clients, and try to catch internal error if such shit occurs.
// Loop through all client/bot slots....
for (ClientsArray_t::IndexType_t index (0u); index < m_clientsArray.GetElementNumber (); ++index)
{
// if think time > world time
if (m_clientsArray[index]->GetThinkIntervalTime () > HalfLifeEngine::Globals::g_halfLifeEngine->GetTime ())
continue;
// Use these try-catch blocks to prevent server crashes when error occurs
#if !defined NDEBUG && !defined _DEBUG
try
{
m_clientsArray[index]->Think ();
}
catch (...)
{
// Error occurred. Kick off all bots and then print a warning message.
KickAll ();
AddLogEntry (true, LogLevel_Error, true, "**** INTERNAL " PRODUCT_SHORT_NAME " ERROR! PLEASE SHUTDOWN AND RESTART YOUR SERVER! ****");
}
#else
m_clientsArray[index]->Think ();
#endif // if !defined NDEBUG && !defined _DEBUG
// randomness prevents all bots to run on same frame -> less laggy server
m_clientsArray[index]->m_thinkIntervalTime = HalfLifeEngine::Globals::g_halfLifeEngine->GetTime () + 1u / Console::Bot::variableManager.GetVariable (Console::Bot::VariableID_ThinkFPS)->GetValue <unsigned char> () * g_randomNumberGenerator.GetValueBetween (0.97f, 1.05f);
}
for (unsigned short index (0u); index < g_origins.GetElementNumber (); ++index)
{
HalfLifeEngine::Globals::g_halfLifeEngine->DrawCross3D (g_origins[index], 10.0f, HalfLifeEngine::Globals::g_halfLifeEngine->GetPrecachedSpriteIndex (HalfLifeEngine::Engine::PrecachedSpriteIndex_Laser), Color <> (0u, 255u, 0u), 1u);
// HalfLifeEngine::Globals::g_halfLifeEngine->DrawLine (g_server->GetHumanManager ()->GetHostClient ()->GetOrigin (), g_origins[index], HalfLifeEngine::Globals::g_halfLifeEngine->GetPrecachedSpriteIndex (HalfLifeEngine::Engine::PrecachedSpriteIndex_Arrow), 5u, 0u, Color <> (0u, 255u, 0u), 250u, 5u, 1u);
}
/*/
for (unsigned short index (1u); index < g_origins.GetElementNumber (); ++index)
HalfLifeEngine::Globals::g_halfLifeEngine->DrawLine (g_origins[index - 1u], g_origins[index], HalfLifeEngine::Globals::g_halfLifeEngine->GetPrecachedSpriteIndex (HalfLifeEngine::Engine::PrecachedSpriteIndex_Arrow), 5u, 0u, Color <> (0u, 255u, 0u), 250u, 5u, 1u);
*/
// Find the grenade entity....
for (HalfLifeEngine::SDK::Classes::Edict *grenade (HalfLifeEngine::Globals::g_halfLifeEngine->FIND_ENTITY_BY_CLASSNAME (NULL, "grenade")); grenade->IsValid (); grenade = HalfLifeEngine::Globals::g_halfLifeEngine->FIND_ENTITY_BY_CLASSNAME (grenade, "grenade"))
{
if (grenade->variables.gravity == 0.0f || grenade->variables.velocity == Math::Vector3D::ZeroValue || grenade->variables.owner == NULL || grenade->variables.damageTime <= HalfLifeEngine::Globals::g_halfLifeEngine->GetTime ())
continue;
InternalAssert (Math::AreEqual (Math::GetMidPoint (grenade->variables.absoluteBoundingBox.maxs.x, grenade->variables.absoluteBoundingBox.mins.x), grenade->variables.origin.x));
InternalAssert (Math::AreEqual (Math::GetMidPoint (grenade->variables.absoluteBoundingBox.maxs.y, grenade->variables.absoluteBoundingBox.mins.y), grenade->variables.origin.y));
InternalAssert (Math::AreEqual (grenade->variables.absoluteBoundingBox.mins.z + 1.0f, grenade->variables.origin.z));
InternalAssert (grenade->variables.viewOffset == Math::Vector3D::ZeroValue);
InternalAssert (grenade->variables.angleVelocity == Math::Angles3D::ZeroValue);
// HalfLifeEngine::Globals::g_halfLifeEngine->PrintFormat (HalfLifeEngine::SDK::Constants::HUDPrint_Talk, "Grenade: type=%i(& 3 = %i), owner team=%i.\n", grenade->GetPrivateDataReference <int> (HalfLifeEngine::SDK::Constants::OFFSET_Grenade_TypeID), grenade->GetPrivateDataReference <int> (HalfLifeEngine::SDK::Constants::OFFSET_Grenade_TypeID) & 3, grenade->GetPrivateDataReference <int> (HalfLifeEngine::SDK::Constants::OFFSET_Grenade_HE_OwnerTeam));
}
#if 0
const unsigned char MAX_POINTS (32u); // the total amount of points to store
static Math::Vector3D vOrigins[MAX_POINTS];
static unsigned char numO (0u);
static const unsigned char maxNumO (255u);
static HalfLifeEngine::SDK::Classes::Edict *lastGrenade (NULL);
// if (vOrigins[0u].x != 0.0f)
// for (unsigned char index (0u); index < MAX_POINTS; ++index)
// HalfLifeEngine::Globals::g_halfLifeEngine->DrawCross3D (vOrigins[index], 3.0f, HalfLifeEngine::Globals::g_halfLifeEngine->GetPrecachedSpriteIndex (HalfLifeEngine::Engine::PrecachedSpriteIndex_Laser), Color <> (0u, 255u, 0u), 1u);
if (numO > 0u)
for (unsigned char index (0u); index < numO; ++index)
HalfLifeEngine::Globals::g_halfLifeEngine->DrawCross3D (vOrigins[index], 15.0f, HalfLifeEngine::Globals::g_halfLifeEngine->GetPrecachedSpriteIndex (HalfLifeEngine::Engine::PrecachedSpriteIndex_Laser), Color <> (0u, 255u, 0u), 1u);
HalfLifeEngine::Globals::g_halfLifeEngine->PrintFormat (HalfLifeEngine::SDK::Constants::HUDPrint_Talk, "frameTime=%lf.", HalfLifeEngine::Globals::g_halfLifeEngine->GetGlobalVariables ()->frameTime);
if (numO >= maxNumO)
return;
HalfLifeEngine::Utilities::PseudoGrenadeEntityVariables_t pseudoGrenadeEntityVariables;
// Find the grenade entity....
for (HalfLifeEngine::SDK::Classes::Edict *grenade (HalfLifeEngine::Globals::g_halfLifeEngine->FIND_ENTITY_BY_CLASSNAME (NULL, "grenade")); grenade->IsValid (); grenade = HalfLifeEngine::Globals::g_halfLifeEngine->FIND_ENTITY_BY_CLASSNAME (grenade, "grenade"))
{
if (grenade->variables.gravity == 0.0f || grenade->variables.velocity == Math::Vector3D::ZeroValue || grenade->variables.owner == NULL || grenade->variables.damageTime <= HalfLifeEngine::Globals::g_halfLifeEngine->GetTime () || grenade == lastGrenade)
continue;
HalfLifeEngine::Utilities::TraceThrownGrenadeToss (grenade, pseudoGrenadeEntityVariables);
// HalfLifeEngine::Globals::g_halfLifeEngine->DrawLine (grenade->variables.origin, pseudoGrenadeEntityVariables.origin, HalfLifeEngine::Globals::g_halfLifeEngine->GetPrecachedSpriteIndex (HalfLifeEngine::Engine::PrecachedSpriteIndex_Arrow), 5u, 0u, Color <> (0u, 255u, 0u), 250u, 5u, 1u);
// HalfLifeEngine::Globals::g_halfLifeEngine->DrawCross3D (pseudoGrenadeEntityVariables.origin, 15.0f, HalfLifeEngine::Globals::g_halfLifeEngine->GetPrecachedSpriteIndex (HalfLifeEngine::Engine::PrecachedSpriteIndex_Laser), Color <> (0u, 255u, 0u), 1u);
vOrigins[numO] = pseudoGrenadeEntityVariables.origin;
++numO;
lastGrenade = grenade;
/*
// const Math::Vector3D s (grenade->variables.absoluteBoundingBox.maxs - grenade->variables.absoluteBoundingBox.mins);
const Math::Vector3D s (grenade->variables.absoluteBoundingBox.maxs.GetMidPoint (grenade->variables.absoluteBoundingBox.mins));
const Math::Vector3D s1 (grenade->variables.origin);
// AddLogEntry (false, LogLevel_Critical, true, "s=%s.", s.ToString ());
AddLogEntry (false, LogLevel_Critical, true, "s=[%lf, %lf, %lf], s1=[%lf, %lf, %lf].", s.x, s.y, s.z, s1.x, s1.y, s1.z);
/* AddLogEntry (false, LogLevel_Critical, true, "viewOffset=%s.", grenade->variables.viewOffset.ToString ());
*/
InternalAssert (Math::AreEqual (Math::GetMidPoint (grenade->variables.absoluteBoundingBox.maxs.x, grenade->variables.absoluteBoundingBox.mins.x), grenade->variables.origin.x));
InternalAssert (Math::AreEqual (Math::GetMidPoint (grenade->variables.absoluteBoundingBox.maxs.y, grenade->variables.absoluteBoundingBox.mins.y), grenade->variables.origin.y));
InternalAssert (Math::AreEqual (grenade->variables.absoluteBoundingBox.mins.z + 1.0f, grenade->variables.origin.z));
InternalAssert (grenade->variables.viewOffset == Math::Vector3D::ZeroValue);
InternalAssert (grenade->variables.angleVelocity == Math::Angles3D::ZeroValue);
/*
static unsigned int c (0u);
grenade->Print (FormatBuffer ("C:/Users/Ýäóàðä/Desktop/Grenade/%s_%u.log", grenade->GetModel ().GetData () + 9u, c));
HalfLifeEngine::Globals::g_halfLifeEngine->PrintFormat (HalfLifeEngine::SDK::Constants::HUDPrint_Talk, "waterLevel=%i, on grnd=%u, part grnd=%u, grnd ent=%s.", grenade->variables.waterLevel, (grenade->variables.flags & HalfLifeEngine::SDK::Constants::FL_ONGROUND), (grenade->variables.flags & HalfLifeEngine::SDK::Constants::FL_PARTIALGROUND), grenade->variables.groundEntity != NULL ? grenade->variables.groundEntity->GetClassName ().GetData () : "NULL");
/*
HalfLifeEngine::SDK::Structures::TraceResult_t traceResult;
for ()
{
}
*/#if 0
// ============= VERSION 2 ===============
// Uses maximum points and memory arrays
// =======================================
// ...
// grenade = entity to find trajectory
// Math::Vector3D vOrigins[MAX_POINTS];
// float fTimes[MAX_POINTS];
const Math::Vector3D vStartOrigin (grenade->variables.origin);
HalfLifeEngine::SDK::Structures::TraceResult_t traceResult;
HalfLifeEngine::Globals::g_halfLifeEngine->TraceToss (grenade, /*grenade*/grenade->variables.owner, traceResult);
const Math::Vector3D vStopOrigin (traceResult.endPosition);
const Math::Vector3D vVelocity (grenade->variables.velocity);
const float fGravity (grenade->variables.gravity * Console::Server::variableManager.GetVariable (Console::Server::VariableID_Gravity)->GetValue <float> ());
const float fTotalTime ((vVelocity.z + Math::sqrtf ((Math::powf (vVelocity.z, 2.0f) + (2.0f * fGravity * (vStartOrigin.z - vStopOrigin.z))))) / fGravity);
float fPercent;
float fTime;
for (unsigned char index (0u); index < MAX_POINTS; ++index)
{
fPercent = static_cast <float> (index) / MAX_POINTS;
fTime = fTotalTime * fPercent;
vOrigins[index].x = vStartOrigin.x + (fPercent * (vStopOrigin.x - vStartOrigin.x));
vOrigins[index].y = vStartOrigin.y + (fPercent * (vStopOrigin.y - vStartOrigin.y));
vOrigins[index].z = vStartOrigin.z - (Math::powf (fTime, 2.0f) * fGravity / 2.0f) + (fTime * vVelocity.z);
// fTimes[index] = fTime;
}
#endif // if 0
}
#endif // if 0
}
void ClientManager::EmitSound (const HalfLifeEngine::SDK::Classes::Edict *const entity, const DynamicString &sample, const float volume, const float attenuation, const HalfLifeEngine::SDK::Constants::SoundFlag_t flags, const HalfLifeEngine::SDK::Constants::SoundPitch_t pitch) const
{
// This function called by the sound hooking code (in pfnEmitSound()) enters the played sound into the array associated with the entity.
const HalfLifeEngine::SDK::Classes::Edict *clientEdict (entity);
if (clientEdict->IsPlayer ())
{
Label_CallEmitSoundCallbackAndReturn:
{
const ClientsArray_t::IndexType_t clientIndex (static_cast <const ClientsArray_t::IndexType_t> (clientEdict->GetIndex ()) - 1u);
// Reliability check.
InternalAssert (m_clients[clientIndex]->IsValid ());
if (m_clients[clientIndex]->IsAlive ())
m_clients[clientIndex]->EmitSound (sample, volume, attenuation, flags, pitch);
return;
}
}
// is owner of object? (For spraylogo sound....)
if (entity->variables.owner != NULL && entity->variables.owner->IsPlayer ())
{
clientEdict = entity->variables.owner;
goto Label_CallEmitSoundCallbackAndReturn;
}
/*
{
const DynamicString fullFilename ("/sound/" + sample);
const DynamicString CSfullFilename (HalfLifeEngine::Globals::g_halfLifeEngine->GetGameModName () + fullFilename);
const float maximumLastingTime (STDIOFile::IsExists (CSfullFilename, "rb") ? GetWaveSoundPlayDuration (CSfullFilename) : GetWaveSoundPlayDuration ("valve" + fullFilename));
HalfLifeEngine::Globals::g_halfLifeEngine->PrintFormat (HalfLifeEngine::SDK::Constants::HUDPrint_Talk, "\"%s\"::EmitSound(): sample: \"%s\", vol=%.1f, attn=%.2f, radius=%.2f, dur=%.2f.", entity->GetClassName ().GetData (), sample.GetData (), volume, attenuation, SOUND_ATTENUATION_TO_RADIUS (attenuation), maximumLastingTime);
}*/
#if 0
// Else....
const Math::Vector3D origin (entity->GetOrigin ());
float distanceSquared, nearestClientDistanceSquared (MATH_GET_SQUARED (512.0f));
char nearestClientIndex (-1);
// Loop through all players....
for (ClientsArray_t::IndexType_t index (0u); index < m_clientsArray.GetElementNumber (); ++index)
{
// now find nearest player
if (!m_clientsArray[index]->IsAlive () || (distanceSquared = m_clientsArray[index]->GetOrigin ().GetDistanceSquared (origin)) >= nearestClientDistanceSquared)
continue;
nearestClientIndex = index; // keep track of the closest found player
nearestClientDistanceSquared = distanceSquared; // update nearest distance
}
if (nearestClientIndex != -1)
m_clientsArray[static_cast <ClientsArray_t::IndexType_t> (nearestClientIndex)]->EmitSound (sample, volume, attenuation, flags, pitch);
#endif // if 0
}