-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot_interface.c
More file actions
3505 lines (2979 loc) · 99.1 KB
/
bot_interface.c
File metadata and controls
3505 lines (2979 loc) · 99.1 KB
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
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <assert.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
#include <stdarg.h>
#include <string.h>
#include <float.h>
#include <math.h>
#include <ctype.h>
#include "q2bridge/aas_translation.h"
#include "q2bridge/botlib.h"
#include "q2bridge/bridge.h"
#include "q2bridge/bridge_config.h"
#include "q2bridge/update_translator.h"
#include "botlib/common/l_libvar.h"
#include "botlib/common/l_log.h"
#include "botlib/aas/aas_map.h"
#include "botlib/aas/aas_local.h"
#include "botlib/aas/aas_sound.h"
#include "botlib/aas/aas_debug.h"
#include "botlib/ai_chat/ai_chat.h"
#include "botlib/ai_character/bot_character.h"
#include "botlib/ai/ai_dm.h"
#include "botlib/ai_weight/bot_weight.h"
#include "botlib/ai_goal/ai_goal.h"
#include "botlib/ai/goal_move_orchestrator.h"
#include "botlib/ai_move/mover_catalogue.h"
#include "bot_interface_assets.h"
#include "botlib/ea/ea_local.h"
#include "botlib/precomp/l_precomp.h"
#include "botlib_interface.h"
#include "bot_interface.h"
#include "bot_state.h"
static void BotInterface_Printf(int priority, const char *fmt, ...);
static bot_import_t *g_botImport = NULL;
static bot_chatstate_t *g_botInterfaceConsoleChat = NULL;
static botlib_import_table_t g_botInterfaceImportTable;
typedef struct botinterface_map_cache_s
{
char map_name[MAX_FILEPATH];
botinterface_asset_list_t models;
botinterface_asset_list_t sounds;
botinterface_asset_list_t images;
} botinterface_map_cache_t;
typedef struct botinterface_entity_snapshot_s
{
qboolean valid;
bot_updateentity_t state;
} botinterface_entity_snapshot_t;
#define BOT_INTERFACE_MAX_ENTITIES 1024
static botinterface_map_cache_t g_botInterfaceMapCache;
static botinterface_entity_snapshot_t g_botInterfaceEntityCache[BOT_INTERFACE_MAX_ENTITIES];
static float g_botInterfaceFrameTime = 0.0f;
static unsigned int g_botInterfaceFrameNumber = 0;
static bool g_botInterfaceDebugDrawEnabled = false;
#define CHARACTERISTIC_EASY_FRAGGER 42
#define CHARACTERISTIC_ALERTNESS 43
static void BotAI_InitEnemyInfo(ai_dm_enemy_info_t *info)
{
if (info == NULL)
{
return;
}
info->valid = false;
info->visible = false;
info->entity = -1;
VectorClear(info->origin);
VectorClear(info->velocity);
info->distance = 0.0f;
info->last_seen_time = -FLT_MAX;
info->field_of_view = 0.0f;
info->is_invisible = false;
info->is_chatting = false;
info->is_shooting = false;
info->triggered_by_damage = false;
info->in_field_of_view = false;
info->has_line_of_sight = false;
}
static int BotAI_MaxTrackedClients(void)
{
libvar_t *maxclients = Bridge_MaxClients();
if (maxclients == NULL)
{
return MAX_CLIENTS;
}
int value = (int)maxclients->value;
if (value < 0)
{
value = 0;
}
if (value > BOT_INTERFACE_MAX_ENTITIES)
{
value = BOT_INTERFACE_MAX_ENTITIES;
}
return value;
}
static float BotInterface_NormaliseAngle180(float angle)
{
while (angle > 180.0f)
{
angle -= 360.0f;
}
while (angle < -180.0f)
{
angle += 360.0f;
}
return angle;
}
static void BotInterface_VectorToAngles(const vec3_t vector, vec3_t angles)
{
if (angles == NULL || vector == NULL)
{
return;
}
float yaw;
float pitch;
if (vector[0] == 0.0f && vector[1] == 0.0f)
{
yaw = 0.0f;
pitch = (vector[2] > 0.0f) ? 90.0f : 270.0f;
}
else
{
yaw = atan2f(vector[1], vector[0]) * (180.0f / (float)M_PI);
if (yaw < 0.0f)
{
yaw += 360.0f;
}
float forward = sqrtf(vector[0] * vector[0] + vector[1] * vector[1]);
pitch = atan2f(vector[2], forward) * (180.0f / (float)M_PI);
}
angles[PITCH] = pitch;
angles[YAW] = yaw;
angles[ROLL] = 0.0f;
}
static bool BotInterface_InFieldOfVision(const vec3_t viewangles, float fov, const vec3_t target_angles)
{
if (viewangles == NULL || target_angles == NULL)
{
return false;
}
if (fov >= 360.0f)
{
return true;
}
float yaw_delta = BotInterface_NormaliseAngle180(viewangles[YAW] - target_angles[YAW]);
float pitch_delta = BotInterface_NormaliseAngle180(viewangles[PITCH] - target_angles[PITCH]);
float half_fov = fov * 0.5f;
return fabsf(yaw_delta) <= half_fov && fabsf(pitch_delta) <= half_fov;
}
static void BotInterface_ClientEyePosition(const bot_client_state_t *state, vec3_t out)
{
if (state == NULL || out == NULL)
{
return;
}
VectorCopy(state->last_client_update.origin, out);
out[0] += state->last_client_update.viewoffset[0];
out[1] += state->last_client_update.viewoffset[1];
out[2] += state->last_client_update.viewoffset[2];
}
static bool BotInterface_HasLineOfSight(const vec3_t from,
const vec3_t to,
int viewer,
int target)
{
if (from == NULL || to == NULL)
{
return false;
}
vec3_t start;
vec3_t end;
VectorCopy(from, start);
VectorCopy(to, end);
vec3_t mins = {0.0f, 0.0f, 0.0f};
vec3_t maxs = {0.0f, 0.0f, 0.0f};
bsp_trace_t trace = Q2_Trace(start, mins, maxs, end, viewer, MASK_SHOT);
return trace.fraction >= 1.0f || trace.ent == target;
}
static bool BotInterface_SameTeam(const bot_client_state_t *lhs, const bot_client_state_t *rhs)
{
if (lhs == NULL || rhs == NULL)
{
return false;
}
if (lhs->team < 0 || rhs->team < 0)
{
return false;
}
return lhs->team == rhs->team;
}
static bool BotInterface_IsChatting(const bot_client_state_t *state)
{
if (state == NULL)
{
return false;
}
return (state->last_client_update.stats[STAT_LAYOUTS] & 1) != 0;
}
static bool BotInterface_IsInvisible(const bot_updateentity_t *snapshot)
{
if (snapshot == NULL)
{
return false;
}
return (snapshot->renderfx & RF_TRANSLUCENT) != 0;
}
static bool BotInterface_IsShooting(const bot_updateentity_t *snapshot)
{
if (snapshot == NULL)
{
return false;
}
const int weapon_fx = EF_BLASTER | EF_ROCKET | EF_GRENADE | EF_HYPERBLASTER | EF_BFG | EF_IONRIPPER |
EF_BLUEHYPERBLASTER | EF_TRACKER | EF_PLASMA;
return (snapshot->effects & weapon_fx) != 0;
}
static float BotInterface_VectorLengthSquared(const vec3_t v)
{
if (v == NULL)
{
return 0.0f;
}
return v[0] * v[0] + v[1] * v[1] + v[2] * v[2];
}
static void BotAI_FindEnemy(bot_client_state_t *state, ai_dm_enemy_info_t *enemy)
{
if (enemy == NULL)
{
return;
}
BotAI_InitEnemyInfo(enemy);
if (state == NULL)
{
return;
}
if (!state->client_update_valid)
{
return;
}
bot_combat_state_t *combat = &state->combat;
int current_health = state->last_client_update.stats[STAT_HEALTH];
bool health_drop = false;
if (combat->last_health_valid && current_health < combat->last_known_health)
{
combat->took_damage = true;
combat->last_damage_amount = combat->last_known_health - current_health;
combat->last_damage_time = g_botInterfaceFrameTime;
health_drop = true;
}
else
{
combat->took_damage = false;
}
combat->last_known_health = current_health;
combat->last_health_valid = true;
float alertness = 0.5f;
float easyfragger = 1.0f;
if (state->character_handle > 0)
{
alertness = Characteristic_BFloat(state->character_handle, CHARACTERISTIC_ALERTNESS, 0.0f, 1.0f);
easyfragger = Characteristic_BFloat(state->character_handle, CHARACTERISTIC_EASY_FRAGGER, 0.0f, 1.0f);
}
vec3_t self_origin;
VectorCopy(state->last_client_update.origin, self_origin);
vec3_t eye_position;
BotInterface_ClientEyePosition(state, eye_position);
int curenemy = combat->current_enemy;
const bot_updateentity_t *current_snapshot = NULL;
float current_enemy_dist_sq = FLT_MAX;
if (curenemy >= 0 && curenemy < BOT_INTERFACE_MAX_ENTITIES &&
g_botInterfaceEntityCache[curenemy].valid)
{
current_snapshot = &g_botInterfaceEntityCache[curenemy].state;
vec3_t delta;
VectorSubtract(current_snapshot->origin, self_origin, delta);
current_enemy_dist_sq = BotInterface_VectorLengthSquared(delta);
}
else
{
curenemy = -1;
combat->current_enemy = -1;
}
if (curenemy >= 0 && current_snapshot != NULL)
{
bool invisible = BotInterface_IsInvisible(current_snapshot);
bool shooting = BotInterface_IsShooting(current_snapshot);
bot_client_state_t *current_state = BotState_Get(curenemy);
bool chatting = BotInterface_IsChatting(current_state);
if (!(invisible && !shooting))
{
vec3_t to_enemy;
VectorSubtract(current_snapshot->origin, eye_position, to_enemy);
vec3_t target_angles;
BotInterface_VectorToAngles(to_enemy, target_angles);
float limited = (current_enemy_dist_sq > 810.0f * 810.0f)
? 810.0f * 810.0f
: current_enemy_dist_sq;
float fov = 90.0f + (limited / (810.0f * 9.0f));
bool in_fov = BotInterface_InFieldOfVision(state->last_client_update.viewangles, fov, target_angles);
bool has_los = BotInterface_HasLineOfSight(eye_position, current_snapshot->origin, state->client_number, curenemy);
if (in_fov && has_los)
{
enemy->valid = true;
enemy->visible = true;
enemy->entity = curenemy;
VectorCopy(current_snapshot->origin, enemy->origin);
vec3_t displacement;
VectorSubtract(current_snapshot->origin, current_snapshot->old_origin, displacement);
VectorCopy(displacement, enemy->velocity);
enemy->distance = sqrtf(current_enemy_dist_sq);
enemy->last_seen_time = g_botInterfaceFrameTime;
enemy->field_of_view = fov;
enemy->is_invisible = invisible;
enemy->is_chatting = chatting;
enemy->is_shooting = shooting;
enemy->triggered_by_damage = health_drop;
enemy->in_field_of_view = in_fov;
enemy->has_line_of_sight = has_los;
combat->current_enemy = curenemy;
combat->enemy_visible = true;
combat->enemy_visible_time = g_botInterfaceFrameTime;
combat->enemy_last_seen_time = g_botInterfaceFrameTime;
VectorCopy(current_snapshot->origin, combat->last_enemy_origin);
VectorCopy(displacement, combat->last_enemy_velocity);
combat->enemy_death_time = -FLT_MAX;
return;
}
}
}
bool had_visible_enemy = combat->enemy_visible;
combat->enemy_visible = false;
if (had_visible_enemy)
{
combat->enemy_death_time = g_botInterfaceFrameTime;
}
int max_clients = BotAI_MaxTrackedClients();
float max_range = 900.0f + alertness * 4000.0f;
float max_range_sq = max_range * max_range;
for (int ent = 0; ent < max_clients; ++ent)
{
if (ent == state->client_number || ent == curenemy)
{
continue;
}
if (ent < 0 || ent >= BOT_INTERFACE_MAX_ENTITIES)
{
continue;
}
if (!g_botInterfaceEntityCache[ent].valid)
{
continue;
}
const bot_updateentity_t *snapshot = &g_botInterfaceEntityCache[ent].state;
vec3_t delta;
VectorSubtract(snapshot->origin, self_origin, delta);
float distance_sq = BotInterface_VectorLengthSquared(delta);
if (distance_sq > max_range_sq)
{
continue;
}
if (curenemy >= 0 && distance_sq > current_enemy_dist_sq)
{
continue;
}
bot_client_state_t *other = BotState_Get(ent);
if (BotInterface_SameTeam(state, other))
{
continue;
}
bool invisible = BotInterface_IsInvisible(snapshot);
bool shooting = BotInterface_IsShooting(snapshot);
if (invisible && !shooting)
{
continue;
}
bool chatting = BotInterface_IsChatting(other);
if (easyfragger < 0.5f && chatting)
{
continue;
}
float limited = (distance_sq > 810.0f * 810.0f) ? 810.0f * 810.0f : distance_sq;
float fov = (curenemy < 0 && (health_drop || shooting)) ? 360.0f : 90.0f + (limited / (810.0f * 9.0f));
vec3_t to_enemy;
VectorSubtract(snapshot->origin, eye_position, to_enemy);
vec3_t target_angles;
BotInterface_VectorToAngles(to_enemy, target_angles);
bool in_fov = BotInterface_InFieldOfVision(state->last_client_update.viewangles, fov, target_angles);
if (!in_fov)
{
continue;
}
bool has_los = BotInterface_HasLineOfSight(eye_position, snapshot->origin, state->client_number, ent);
if (!has_los)
{
continue;
}
if (curenemy < 0 && distance_sq > (100.0f * 100.0f) && !health_drop && !shooting)
{
bool bot_in_enemy_fov = true;
if (other != NULL)
{
vec3_t to_bot;
VectorSubtract(self_origin, snapshot->origin, to_bot);
vec3_t enemy_angles;
BotInterface_VectorToAngles(to_bot, enemy_angles);
bot_in_enemy_fov = BotInterface_InFieldOfVision(other->last_client_update.viewangles, 90.0f, enemy_angles);
}
if (!bot_in_enemy_fov)
{
continue;
}
}
enemy->valid = true;
enemy->visible = true;
enemy->entity = ent;
VectorCopy(snapshot->origin, enemy->origin);
vec3_t displacement;
VectorSubtract(snapshot->origin, snapshot->old_origin, displacement);
VectorCopy(displacement, enemy->velocity);
enemy->distance = sqrtf(distance_sq);
enemy->last_seen_time = g_botInterfaceFrameTime;
enemy->field_of_view = fov;
enemy->is_invisible = invisible;
enemy->is_chatting = chatting;
enemy->is_shooting = shooting;
enemy->triggered_by_damage = health_drop;
enemy->in_field_of_view = in_fov;
enemy->has_line_of_sight = has_los;
bool enemy_changed = (combat->current_enemy != ent);
combat->current_enemy = ent;
combat->enemy_visible = true;
combat->enemy_visible_time = g_botInterfaceFrameTime;
combat->enemy_last_seen_time = g_botInterfaceFrameTime;
combat->enemy_death_time = -FLT_MAX;
VectorCopy(snapshot->origin, combat->last_enemy_origin);
VectorCopy(displacement, combat->last_enemy_velocity);
if (enemy_changed && curenemy >= 0)
{
combat->enemy_sight_time = g_botInterfaceFrameTime - 2.0f;
}
else
{
combat->enemy_sight_time = g_botInterfaceFrameTime;
}
return;
}
}
static void BotInterface_SynchroniseCombatState(bot_client_state_t *state)
{
if (state == NULL || state->dm_state == NULL)
{
return;
}
ai_dm_metrics_t metrics = {0};
AI_DMState_GetMetrics(state->dm_state, &metrics);
bot_combat_state_t *combat = &state->combat;
combat->current_enemy = metrics.enemy_entity;
combat->revenge_enemy = metrics.revenge_enemy;
combat->revenge_kills = metrics.revenge_kills;
combat->enemy_visible = metrics.enemy_visible;
combat->enemy_visible_time = metrics.enemyvisible_time;
combat->enemy_sight_time = metrics.enemysight_time;
combat->enemy_death_time = metrics.enemydeath_time;
combat->enemy_last_seen_time = metrics.enemyposition_time;
VectorCopy(metrics.last_enemy_origin, combat->last_enemy_origin);
VectorCopy(metrics.last_enemy_velocity, combat->last_enemy_velocity);
}
static char *BotInterface_CopyString(const char *text);
static int BotInterface_StringCompareIgnoreCase(const char *lhs, const char *rhs)
{
if (lhs == NULL || rhs == NULL)
{
return (lhs == rhs) ? 0 : (lhs != NULL ? 1 : -1);
}
while (*lhs != '\0' && *rhs != '\0')
{
int diff = tolower((unsigned char)*lhs) - tolower((unsigned char)*rhs);
if (diff != 0)
{
return diff;
}
++lhs;
++rhs;
}
return tolower((unsigned char)*lhs) - tolower((unsigned char)*rhs);
}
static void BotInterface_FreeAssetList(botinterface_asset_list_t *list)
{
if (list == NULL)
{
return;
}
if (list->entries != NULL)
{
for (size_t index = 0; index < list->count; ++index)
{
free(list->entries[index]);
}
free(list->entries);
}
list->entries = NULL;
list->count = 0;
}
static bool BotInterface_CopyAssetList(botinterface_asset_list_t *target,
int count,
char *source[])
{
if (target == NULL)
{
return false;
}
BotInterface_FreeAssetList(target);
if (count <= 0 || source == NULL)
{
target->entries = NULL;
target->count = 0;
return true;
}
size_t allocation = (size_t)count;
char **table = (char **)calloc(allocation, sizeof(char *));
if (table == NULL)
{
return false;
}
for (size_t index = 0; index < allocation; ++index)
{
if (source[index] == NULL)
{
continue;
}
table[index] = BotInterface_CopyString(source[index]);
if (table[index] == NULL)
{
for (size_t rollback = 0; rollback < index; ++rollback)
{
free(table[rollback]);
}
free(table);
return false;
}
}
target->entries = table;
target->count = allocation;
return true;
}
static void BotInterface_ResetEntityCache(void)
{
for (size_t index = 0; index < BOT_INTERFACE_MAX_ENTITIES; ++index)
{
g_botInterfaceEntityCache[index].valid = qfalse;
}
}
static void BotInterface_ResetMapCache(void)
{
BotMove_MoverCatalogueReset();
BotInterface_FreeAssetList(&g_botInterfaceMapCache.models);
BotInterface_FreeAssetList(&g_botInterfaceMapCache.sounds);
BotInterface_FreeAssetList(&g_botInterfaceMapCache.images);
g_botInterfaceMapCache.map_name[0] = '\0';
}
static bool BotInterface_RecordMapAssets(const char *mapname,
int modelindexes,
char *modelindex[],
int soundindexes,
char *soundindex[],
int imageindexes,
char *imageindex[])
{
if (mapname != NULL)
{
strncpy(g_botInterfaceMapCache.map_name,
mapname,
sizeof(g_botInterfaceMapCache.map_name) - 1);
g_botInterfaceMapCache.map_name[sizeof(g_botInterfaceMapCache.map_name) - 1] = '\0';
}
else
{
g_botInterfaceMapCache.map_name[0] = '\0';
}
if (!BotInterface_CopyAssetList(&g_botInterfaceMapCache.models, modelindexes, modelindex))
{
return false;
}
if (!BotInterface_CopyAssetList(&g_botInterfaceMapCache.sounds, soundindexes, soundindex))
{
return false;
}
if (!BotInterface_CopyAssetList(&g_botInterfaceMapCache.images, imageindexes, imageindex))
{
return false;
}
return true;
}
static void BotInterface_ResetFrameQueues(void)
{
AAS_SoundSubsystem_ResetFrameEvents();
}
static void BotInterface_ResetGoalSnapshot(bot_client_state_t *state)
{
if (state == NULL)
{
return;
}
state->goal_snapshot_count = 0;
memset(state->goal_snapshot, 0, sizeof(state->goal_snapshot));
}
static void BotInterface_UpdateGoalSnapshot(bot_client_state_t *state)
{
if (state == NULL)
{
return;
}
BotInterface_ResetGoalSnapshot(state);
if (state->goal_handle <= 0)
{
return;
}
bot_goal_t goal = {0};
if (AI_GoalBotlib_GetTopGoal(state->goal_handle, &goal))
{
state->goal_snapshot[state->goal_snapshot_count++] = goal;
}
if (state->goal_snapshot_count < (int)(sizeof(state->goal_snapshot) / sizeof(state->goal_snapshot[0])) &&
AI_GoalBotlib_GetSecondGoal(state->goal_handle, &goal))
{
if (state->goal_snapshot_count == 0 || state->goal_snapshot[0].number != goal.number)
{
state->goal_snapshot[state->goal_snapshot_count++] = goal;
}
}
}
static const bot_goal_t *BotInterface_FindSnapshotGoal(const bot_client_state_t *state, int number)
{
if (state == NULL || state->goal_snapshot_count <= 0)
{
return NULL;
}
for (int i = 0; i < state->goal_snapshot_count; ++i)
{
if (state->goal_snapshot[i].number == number)
{
return &state->goal_snapshot[i];
}
}
return NULL;
}
static float BotInterface_NormaliseDirection(vec3_t out, const vec3_t in)
{
if (out == NULL || in == NULL)
{
return 0.0f;
}
float length = sqrtf(in[0] * in[0] + in[1] * in[1] + in[2] * in[2]);
if (length <= 0.0001f)
{
VectorClear(out);
return 0.0f;
}
float inv = 1.0f / length;
out[0] = in[0] * inv;
out[1] = in[1] * inv;
out[2] = in[2] * inv;
return length;
}
static void BotInterface_BuildMoveCommand(bot_input_t *out_input,
const vec3_t from,
const vec3_t to)
{
if (out_input == NULL || from == NULL || to == NULL)
{
return;
}
vec3_t delta;
VectorSubtract(to, from, delta);
float length = BotInterface_NormaliseDirection(out_input->dir, delta);
out_input->speed = length;
out_input->actionflags = 0;
}
static int BotInterface_RebuildGoalCandidates(bot_client_state_t *state)
{
if (state == NULL || state->goal_state == NULL)
{
return BLERR_INVALIDIMPORT;
}
AI_GoalState_ClearCandidates(state->goal_state);
for (int index = 0; index < state->goal_snapshot_count; ++index)
{
const bot_goal_t *goal = &state->goal_snapshot[index];
ai_goal_candidate_t candidate = {0};
candidate.item_index = goal->number;
candidate.area = goal->areanum;
candidate.travel_flags = TFL_DEFAULT;
VectorCopy(goal->origin, candidate.origin);
int start_area = AI_GoalState_GetCurrentArea(state->goal_state);
int travel_time = 0;
float weight = BotGoal_EvaluateStackGoal(state->goal_handle,
goal,
state->last_client_update.origin,
start_area,
state->last_client_update.inventory,
candidate.travel_flags,
&travel_time);
if (weight <= -FLT_MAX)
{
continue;
}
candidate.base_weight = weight;
AI_GoalState_AddCandidate(state->goal_state, &candidate);
}
return BLERR_NOERROR;
}
static float BotInterface_GoalWeight(void *ctx, const ai_goal_candidate_t *candidate)
{
bot_client_state_t *state = (bot_client_state_t *)ctx;
if (state == NULL || candidate == NULL)
{
return 0.0f;
}
const bot_goal_t *goal = BotInterface_FindSnapshotGoal(state, candidate->item_index);
if (goal == NULL)
{
return candidate->base_weight;
}
int start_area = AI_GoalState_GetCurrentArea(state->goal_state);
int travel_time = 0;
float weight = BotGoal_EvaluateStackGoal(state->goal_handle,
goal,
state->last_client_update.origin,
start_area,
state->last_client_update.inventory,
candidate->travel_flags,
&travel_time);
if (weight <= -FLT_MAX)
{
return candidate->base_weight;
}
return weight;
}
static float BotInterface_GoalTravelTime(void *ctx, int start_area, const ai_goal_candidate_t *candidate)
{
bot_client_state_t *state = (bot_client_state_t *)ctx;
if (state == NULL || candidate == NULL)
{
return 0.0f;
}
const bot_goal_t *goal = BotInterface_FindSnapshotGoal(state, candidate->item_index);
if (goal == NULL)
{
return -1.0f;
}
if (start_area <= 0 || goal->areanum <= 0)
{
int travel_time = 0;
BotGoal_EvaluateStackGoal(state->goal_handle,
goal,
state->last_client_update.origin,
start_area,
state->last_client_update.inventory,
candidate->travel_flags,
&travel_time);
return (float)travel_time;
}
vec3_t origin;
VectorCopy(state->last_client_update.origin, origin);
int travel = AAS_AreaTravelTimeToGoalArea(start_area, origin, goal->areanum, candidate->travel_flags);
return (float)travel;
}
static void BotInterface_GoalNotify(void *ctx, const ai_goal_selection_t *selection)
{
bot_client_state_t *state = (bot_client_state_t *)ctx;
if (state == NULL)
{
return;
}
if (selection == NULL || !selection->valid)
{
state->active_goal_number = 0;
return;
}
state->active_goal_number = selection->candidate.item_index;
}
static int BotInterface_PrepareMoveState(bot_client_state_t *state, float thinktime)
{
if (state == NULL || state->move_handle <= 0)
{
return BLERR_INVALIDIMPORT;
}
bot_initmove_t init = {0};
VectorCopy(state->last_client_update.origin, init.origin);
VectorCopy(state->last_client_update.velocity, init.velocity);
VectorCopy(state->last_client_update.viewoffset, init.viewoffset);
init.entitynum = state->client_number;
init.client = state->client_number;
init.thinktime = thinktime;
init.presencetype = (state->last_client_update.pm_flags & PMF_DUCKED) ? PRESENCE_CROUCH : PRESENCE_NORMAL;
if (state->last_client_update.pm_flags & PMF_ON_GROUND)
{
init.or_moveflags |= MFL_ONGROUND;
}
if ((state->last_client_update.pm_flags & PMF_TIME_TELEPORT) && state->last_client_update.pm_time > 0)
{
init.or_moveflags |= MFL_TELEPORTED;
}
if ((state->last_client_update.pm_flags & PMF_TIME_WATERJUMP) && state->last_client_update.pm_time > 0)
{
init.or_moveflags |= MFL_WATERJUMP;
}
VectorCopy(state->last_client_update.viewangles, init.viewangles);
BotInitMoveState(state->move_handle, &init);
bot_movestate_t *ms = BotMoveStateFromHandle(state->move_handle);
if (ms != NULL && state->goal_state != NULL)
{
AI_GoalState_SetCurrentArea(state->goal_state, ms->areanum);
}
return BLERR_NOERROR;
}
static void BotInterface_ApplyMoveResult(bot_client_state_t *state,
const bot_moveresult_t *result,
bot_input_t *out_input)
{
if (state == NULL || result == NULL || out_input == NULL)
{
return;
}
if (result->failure)
{
VectorClear(out_input->dir);
out_input->speed = 0.0f;
}
else
{
VectorCopy(result->movedir, out_input->dir);
out_input->speed = 400.0f;
}
out_input->actionflags = 0;
if (result->flags & MOVERESULT_MOVEMENTWEAPON)
{
out_input->actionflags |= ACTION_ATTACK;
state->current_weapon = result->weapon;
}
if (result->traveltype == TRAVEL_JUMP || result->traveltype == TRAVEL_ROCKETJUMP ||
result->traveltype == TRAVEL_BFGJUMP || result->traveltype == TRAVEL_WATERJUMP)
{
out_input->actionflags |= ACTION_JUMP;
}
if (result->traveltype == TRAVEL_CROUCH)
{