forked from APGRoboCop/jk_botti
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathengine.cpp
535 lines (429 loc) · 15.6 KB
/
engine.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
//
// JK_Botti - be more human!
//
// engine.cpp
//
#ifndef _WIN32
#include <cstring>
#endif
#include <extdll.h>
#include <dllapi.h>
#include <h_export.h>
#include <meta_api.h>
#include "bot.h"
#include "bot_client.h"
#include "bot_func.h"
#include "bot_sound.h"
#include "bot_weapons.h"
extern enginefuncs_t g_engfuncs;
extern bot_t bots[32];
extern qboolean isFakeClientCommand;
extern int fake_arg_count;
char g_argv[1024*3];
char g_arg1[1024];
char g_arg2[1024];
char g_arg3[1024];
extern int get_cvars;
extern qboolean aim_fix;
extern float turn_skill;
void (*botMsgFunction)(void *, int) = nullptr;
void (*botMsgEndFunction)(void *, int) = nullptr;
int botMsgIndex;
qboolean g_in_intermission = FALSE;
typedef struct event_info_s {
int eventindex;
const char * eventname;
float volume;
float recoil[2];
} event_info_t;
event_info_t g_event_info[] = {
//from valve
{-1, "events/glock1.sc", 0.96, {-2.0f, -2.0f}},
{-1, "events/glock2.sc", 0.96, {-2.0f, -2.0f}},
{-1, "events/shotgun1.sc", 0.975, {-5.0f, -5.0f}},
{-1, "events/shotgun2.sc", 0.99, {-10.0f, -10.0f}},
{-1, "events/mp5.sc", 1.0f, {-2.0f, 2.0f}},
{-1, "events/mp52.sc", 1.0f, {-10.0f, -10.0f}},
{-1, "events/python.sc", 0.85, {-10.0f, -10.0f}},
{-1, "events/gauss.sc", 1.0f, {-2.0f, -2.0f}},
{-1, "events/gaussspin.sc", 1.0f, {0, 0}},
{-1, "events/train.sc", 0.6, {0, 0}},
{-1, "events/crowbar.sc", 1.0f, {0, 0}},
{-1, "events/crossbow1.sc", 1.0f, {-2.0f, -2.0f}},
{-1, "events/crossbow2.sc", 1.0f, {0, 0}},
{-1, "events/rpg.sc", 0.9, {-5.0f, -5.0f}},
{-1, "events/egon_fire.sc", 0.94, {0, 0}},
{-1, "events/egon_stop.sc", 0.98, {0, 0}},
{-1, "events/firehornet.sc", 1.0f, {0.0f, 2.0f}},
{-1, "events/tripfire.sc", 0.0f, {0, 0}},
{-1, "events/snarkfire.sc", 0.0f, {0, 0}},
//from gearbox
{-1, "events/egon_effect.sc", 0.0f, {0, 0}},
{-1, "events/eagle.sc", 0.90, {-20.0f, -20.0f}},
{-1, "events/pipewrench.sc", 0.8, {0, 0}},
{-1, "events/m249.sc", 0.96, {-7.0f, 7.0f}},
{-1, "events/shock.sc", 0.96, {0.0f, 2.0f}},
{-1, "events/sniper.sc", 1.0f, {-10.0f, -10.0f}},
{-1, "events/knife.sc", 0.6, {0, 0}},
{-1, "events/penguinfire.sc", 0.85, {0, 0}},
{-1, "events/spore.sc", 0.96, {-10.0f, -10.0f}},
{-1, "", 0, {0, 0}} //NULL
};
//
static unsigned short pfnPrecacheEvent_Post(int type, const char* psz)
{
if (!gpGlobals->deathmatch)
RETURN_META_VALUE (MRES_IGNORED, 0);
const unsigned short eventindex = (META_RESULT_STATUS == MRES_OVERRIDE || META_RESULT_STATUS == MRES_SUPERCEDE) ? META_RESULT_OVERRIDE_RET(unsigned short) : META_RESULT_ORIG_RET(unsigned short);
event_info_t *pei;
for(pei = g_event_info; pei->eventname && pei->eventname[0]; pei++)
{
if(strcmp(psz, pei->eventname) == 0)
{
// found event, update eventindex
pei->eventindex = eventindex;
break;
}
}
if(!pei->eventname || !pei->eventname[0])
{
// unknown event
//UTIL_ConsolePrintf("PrecacheEvent, new event type: \"%s\" : %d", psz, eventindex);
}
RETURN_META_VALUE (MRES_IGNORED, 0);
}
//
static void pfnPlaybackEvent( int flags, const edict_t *pInvoker, unsigned short eventindex, float delay, float *origin, float *angles, float fparam1, float fparam2, int iparam1, int iparam2, int bparam1, int bparam2 )
{
if (!gpGlobals->deathmatch)
RETURN_META (MRES_IGNORED);
// get event info
event_info_t *pei;
for(pei = g_event_info; pei->eventname; pei++)
{
if(eventindex == pei->eventindex)
break;
}
if(!pei->eventname)
RETURN_META (MRES_IGNORED);
// event creates sound?
if(pei->volume > 0.0f)
{
const int ivolume = int(1000 * pei->volume);
SaveSound((edict_t*)pInvoker, pInvoker->v.origin, ivolume, CHAN_WEAPON, 5.0f);
}
// event causes client recoil?
if(pei->recoil[0] != 0.0f && pei->recoil[1] != 0.0f)
{
// gauss uses bug fix that sends two events at same time,
// ignore the duplicated one...
if(strcmp("events/gauss.sc", pei->eventname) == 0 && delay > 0.0f && (flags & FEV_RELIABLE))
RETURN_META (MRES_IGNORED);
const int index = UTIL_GetBotIndex(pInvoker);
if(index != -1)
{
bots[index].f_recoil += RANDOM_FLOAT2(pei->recoil[0], pei->recoil[1]);
}
}
RETURN_META (MRES_IGNORED);
}
static void pfnEmitSound(edict_t *entity, int channel, const char *sample, float volume, float attenuation, int fFlags, int pitch)
{
if (gpGlobals->deathmatch)
{
const int ivolume = int(1000 * volume);
const char *classname = STRING(entity->v.classname);
float duration = 5.0f;
if (strncmp("item_health", classname, 11) == 0)
duration = 8.0f;
else if (strncmp("item_battery", classname, 12) == 0)
duration = 8.0f;
else if (GetAmmoItemFlag(classname) != 0)
duration = 8.0f;
else if (GetWeaponItemFlag(classname) != 0 && (entity->v.owner == nullptr))
duration = 8.0f;
else if (strcmp("item_longjump", classname) == 0)
duration = 8.0f;
SaveSound(entity, entity->v.origin, ivolume, channel, duration);
}
RETURN_META (MRES_IGNORED);
}
static void pfnChangeLevel(char* s1, char* s2)
{
if (!gpGlobals->deathmatch)
RETURN_META (MRES_IGNORED);
// kick any bot off of the server after time/frag limit...
for (int index = 0; index < 32; index++)
if (bots[index].is_used) // is this slot used?
BotKick(bots[index]);
RETURN_META (MRES_IGNORED);
}
static void pfnClientCommand(edict_t* pEdict, char* szFmt, ...)
{
if ((FBitSet(pEdict->v.flags, FL_FAKECLIENT) || FBitSet(pEdict->v.flags, FL_THIRDPARTYBOT)))
RETURN_META (MRES_SUPERCEDE);
RETURN_META (MRES_IGNORED);
}
static int FAST_GET_USER_MSG_ID(plid_t plindex, int & value, const char * name, int * size)
{
return(value ? value : (value = GET_USER_MSG_ID(plindex, name, size)));
}
static void pfnMessageBegin(int msg_dest, int msg_type, const float *pOrigin, edict_t *ed)
{
if (gpGlobals->deathmatch)
{
static int WeaponList = 0;
if (ed)
{
static int CurWeapon = 0;
int index = UTIL_GetBotIndex(ed);
// is this message for a bot?
if (index != -1)
{
static int ScreenFade = 0;
static int Damage = 0;
static int Battery = 0;
static int Health = 0;
static int WeapPickup = 0;
static int ItemPickup = 0;
static int AmmoPickup = 0;
static int AmmoX = 0;
botMsgEndFunction = nullptr; // no msg end function until known otherwise
botMsgIndex = index; // index of bot receiving message
if (msg_type == FAST_GET_USER_MSG_ID (PLID, WeaponList, "WeaponList", nullptr))
botMsgFunction = BotClient_Valve_WeaponList;
else if (msg_type == FAST_GET_USER_MSG_ID (PLID, CurWeapon, "CurWeapon", nullptr))
botMsgFunction = BotClient_Valve_CurrentWeapon;
else if (msg_type == FAST_GET_USER_MSG_ID (PLID, AmmoX, "AmmoX", nullptr))
botMsgFunction = BotClient_Valve_AmmoX;
else if (msg_type == FAST_GET_USER_MSG_ID (PLID, AmmoPickup, "AmmoPickup", nullptr))
botMsgFunction = BotClient_Valve_AmmoPickup;
else if (msg_type == FAST_GET_USER_MSG_ID (PLID, WeapPickup, "WeapPickup", nullptr))
botMsgFunction = BotClient_Valve_WeaponPickup;
else if (msg_type == FAST_GET_USER_MSG_ID (PLID, ItemPickup, "ItemPickup", nullptr))
botMsgFunction = BotClient_Valve_ItemPickup;
else if (msg_type == FAST_GET_USER_MSG_ID (PLID, Health, "Health", nullptr))
botMsgFunction = BotClient_Valve_Health;
else if (msg_type == FAST_GET_USER_MSG_ID (PLID, Battery, "Battery", nullptr))
botMsgFunction = BotClient_Valve_Battery;
else if (msg_type == FAST_GET_USER_MSG_ID (PLID, Damage, "Damage", nullptr))
botMsgFunction = BotClient_Valve_Damage;
else if (msg_type == FAST_GET_USER_MSG_ID (PLID, ScreenFade, "ScreenFade", nullptr))
botMsgFunction = BotClient_Valve_ScreenFade;
}
// is this message for a player?
else
{
index = ENTINDEX(ed) - 1;
if(index > -1 && index < gpGlobals->maxClients)
{
botMsgFunction = nullptr; // no msg function until known otherwise
botMsgEndFunction = nullptr; // no msg end function until known otherwise
botMsgIndex = index; // index of bot receiving message
}
if (msg_type == FAST_GET_USER_MSG_ID (PLID, CurWeapon, "CurWeapon", nullptr))
botMsgFunction = PlayerClient_Valve_CurrentWeapon;
}
}
else if (msg_dest == MSG_ALL)
{
static int DeathMsg = 0;
botMsgFunction = nullptr; // no msg function until known otherwise
botMsgEndFunction = nullptr; // no msg end function until known otherwise
botMsgIndex = -1; // index of bot receiving message (none)
if (msg_type == SVC_INTERMISSION)
g_in_intermission = TRUE;
else if (msg_type == FAST_GET_USER_MSG_ID (PLID, DeathMsg, "DeathMsg", nullptr))
botMsgFunction = BotClient_Valve_DeathMsg;
}
else
{
// Steam makes the WeaponList message be sent differently
botMsgFunction = nullptr; // no msg function until known otherwise
botMsgEndFunction = nullptr; // no msg end function until known otherwise
botMsgIndex = -1; // index of bot receiving message (none)
if (msg_type == FAST_GET_USER_MSG_ID (PLID, WeaponList, "WeaponList", nullptr))
botMsgFunction = BotClient_Valve_WeaponList;
}
}
RETURN_META (MRES_IGNORED);
}
static void pfnMessageEnd()
{
if (gpGlobals->deathmatch)
{
if (botMsgEndFunction)
(*botMsgEndFunction)(nullptr, botMsgIndex); // NULL indicated msg end
// clear out the bot message function pointers...
botMsgFunction = nullptr;
botMsgEndFunction = nullptr;
}
RETURN_META (MRES_IGNORED);
}
static void pfnWriteByte(int iValue)
{
if (gpGlobals->deathmatch)
{
// if this message is for a bot, call the client message function...
if (botMsgFunction)
(*botMsgFunction)((void *)&iValue, botMsgIndex);
}
RETURN_META (MRES_IGNORED);
}
static void pfnWriteChar(int iValue)
{
if (gpGlobals->deathmatch)
{
// if this message is for a bot, call the client message function...
if (botMsgFunction)
(*botMsgFunction)((void *)&iValue, botMsgIndex);
}
RETURN_META (MRES_IGNORED);
}
static void pfnWriteShort(int iValue)
{
if (gpGlobals->deathmatch)
{
// if this message is for a bot, call the client message function...
if (botMsgFunction)
(*botMsgFunction)((void *)&iValue, botMsgIndex);
}
RETURN_META (MRES_IGNORED);
}
static void pfnWriteLong(int iValue)
{
if (gpGlobals->deathmatch)
{
// if this message is for a bot, call the client message function...
if (botMsgFunction)
(*botMsgFunction)((void *)&iValue, botMsgIndex);
}
RETURN_META (MRES_IGNORED);
}
static void pfnWriteAngle(float flValue)
{
if (gpGlobals->deathmatch)
{
// if this message is for a bot, call the client message function...
if (botMsgFunction)
(*botMsgFunction)((void *)&flValue, botMsgIndex);
}
RETURN_META (MRES_IGNORED);
}
static void pfnWriteCoord(float flValue)
{
if (gpGlobals->deathmatch)
{
// if this message is for a bot, call the client message function...
if (botMsgFunction)
(*botMsgFunction)((void *)&flValue, botMsgIndex);
}
RETURN_META (MRES_IGNORED);
}
static void pfnWriteString(const char *sz)
{
if (gpGlobals->deathmatch)
{
// if this message is for a bot, call the client message function...
if (botMsgFunction)
(*botMsgFunction)((void *)sz, botMsgIndex);
}
RETURN_META (MRES_IGNORED);
}
static void pfnWriteEntity(int iValue)
{
if (gpGlobals->deathmatch)
{
// if this message is for a bot, call the client message function...
if (botMsgFunction)
(*botMsgFunction)((void *)&iValue, botMsgIndex);
}
RETURN_META (MRES_IGNORED);
}
static void pfnClientPrintf( edict_t* pEdict, PRINT_TYPE ptype, const char *szMsg )
{
if ((FBitSet(pEdict->v.flags, FL_FAKECLIENT) || FBitSet(pEdict->v.flags, FL_THIRDPARTYBOT)))
RETURN_META (MRES_SUPERCEDE);
RETURN_META (MRES_IGNORED);
}
static const char *pfnCmd_Args()
{
if (isFakeClientCommand)
RETURN_META_VALUE (MRES_SUPERCEDE, &g_argv[0]);
RETURN_META_VALUE (MRES_IGNORED, NULL);
}
static const char *pfnCmd_Argv( int argc )
{
if (isFakeClientCommand)
{
if (argc == 0)
RETURN_META_VALUE (MRES_SUPERCEDE, g_arg1);
else if (argc == 1)
RETURN_META_VALUE (MRES_SUPERCEDE, g_arg2);
else if (argc == 2)
RETURN_META_VALUE (MRES_SUPERCEDE, g_arg3);
else
RETURN_META_VALUE (MRES_SUPERCEDE, NULL);
}
RETURN_META_VALUE (MRES_IGNORED, NULL);
}
static int pfnCmd_Argc()
{
if (isFakeClientCommand)
RETURN_META_VALUE (MRES_SUPERCEDE, fake_arg_count);
RETURN_META_VALUE (MRES_IGNORED, 0);
}
static void pfnSetClientMaxspeed(const edict_t *pEdict, float fNewMaxspeed)
{
if (!gpGlobals->deathmatch)
RETURN_META (MRES_IGNORED);
const int index = UTIL_GetBotIndex((edict_t*)pEdict);
// is this message for a bot?
if (index != -1)
bots[index].f_max_speed = fNewMaxspeed;
RETURN_META (MRES_IGNORED);
}
static int pfnGetPlayerUserId(edict_t *e )
{
if (gpGlobals->deathmatch)
{
if (submod_id == SUBMOD_OP4)
{
// is this edict a bot?
if (UTIL_GetBotPointer( e ))
RETURN_META_VALUE (MRES_SUPERCEDE, 0); // don't return a valid index (so bot won't get kicked)
}
}
RETURN_META_VALUE (MRES_IGNORED, 0);
}
C_DLLEXPORT int GetEngineFunctions (enginefuncs_t *pengfuncsFromEngine, int *interfaceVersion)
{
memset(pengfuncsFromEngine, 0, sizeof(enginefuncs_t));
pengfuncsFromEngine->pfnPlaybackEvent = pfnPlaybackEvent;
pengfuncsFromEngine->pfnChangeLevel = pfnChangeLevel;
pengfuncsFromEngine->pfnEmitSound = pfnEmitSound;
pengfuncsFromEngine->pfnClientCommand = pfnClientCommand;
pengfuncsFromEngine->pfnMessageBegin = pfnMessageBegin;
pengfuncsFromEngine->pfnMessageEnd = pfnMessageEnd;
pengfuncsFromEngine->pfnWriteByte = pfnWriteByte;
pengfuncsFromEngine->pfnWriteChar = pfnWriteChar;
pengfuncsFromEngine->pfnWriteShort = pfnWriteShort;
pengfuncsFromEngine->pfnWriteLong = pfnWriteLong;
pengfuncsFromEngine->pfnWriteAngle = pfnWriteAngle;
pengfuncsFromEngine->pfnWriteCoord = pfnWriteCoord;
pengfuncsFromEngine->pfnWriteString = pfnWriteString;
pengfuncsFromEngine->pfnWriteEntity = pfnWriteEntity;
pengfuncsFromEngine->pfnClientPrintf = pfnClientPrintf;
pengfuncsFromEngine->pfnCmd_Args = pfnCmd_Args;
pengfuncsFromEngine->pfnCmd_Argv = pfnCmd_Argv;
pengfuncsFromEngine->pfnCmd_Argc = pfnCmd_Argc;
pengfuncsFromEngine->pfnSetClientMaxspeed = pfnSetClientMaxspeed;
pengfuncsFromEngine->pfnGetPlayerUserId = pfnGetPlayerUserId;
return TRUE;
}
C_DLLEXPORT int GetEngineFunctions_POST (enginefuncs_t *pengfuncsFromEngine, int *interfaceVersion)
{
memset(pengfuncsFromEngine, 0, sizeof(enginefuncs_t));
pengfuncsFromEngine->pfnPrecacheEvent = pfnPrecacheEvent_Post;
return TRUE;
}