| 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 |
#include "../qcommon/q_shared.h" |
| 33 |
#include "l_libvar.h" |
| 34 |
#include "l_log.h" |
| 35 |
#include "l_memory.h" |
| 36 |
#include "l_utils.h" |
| 37 |
#include "l_script.h" |
| 38 |
#include "l_precomp.h" |
| 39 |
#include "l_struct.h" |
| 40 |
#include "aasfile.h" |
| 41 |
#include "botlib.h" |
| 42 |
#include "be_aas.h" |
| 43 |
#include "be_aas_funcs.h" |
| 44 |
#include "be_interface.h" |
| 45 |
#include "be_ai_weight.h" |
| 46 |
#include "be_ai_weap.h" |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
#define WEAPON_OFS(x) (size_t)&(((weaponinfo_t *)0)->x) |
| 52 |
#define PROJECTILE_OFS(x) (size_t)&(((projectileinfo_t *)0)->x) |
| 53 |
|
| 54 |
|
| 55 |
static fielddef_t weaponinfo_fields[] = |
| 56 |
{ |
| 57 |
{"number", WEAPON_OFS(number), FT_INT}, |
| 58 |
{"name", WEAPON_OFS(name), FT_STRING}, |
| 59 |
{"level", WEAPON_OFS(level), FT_INT}, |
| 60 |
{"model", WEAPON_OFS(model), FT_STRING}, |
| 61 |
{"weaponindex", WEAPON_OFS(weaponindex), FT_INT}, |
| 62 |
{"flags", WEAPON_OFS(flags), FT_INT}, |
| 63 |
{"projectile", WEAPON_OFS(projectile), FT_STRING}, |
| 64 |
{"numprojectiles", WEAPON_OFS(numprojectiles), FT_INT}, |
| 65 |
{"hspread", WEAPON_OFS(hspread), FT_FLOAT}, |
| 66 |
{"vspread", WEAPON_OFS(vspread), FT_FLOAT}, |
| 67 |
{"speed", WEAPON_OFS(speed), FT_FLOAT}, |
| 68 |
{"acceleration", WEAPON_OFS(acceleration), FT_FLOAT}, |
| 69 |
{"recoil", WEAPON_OFS(recoil), FT_FLOAT|FT_ARRAY, 3}, |
| 70 |
{"offset", WEAPON_OFS(offset), FT_FLOAT|FT_ARRAY, 3}, |
| 71 |
{"angleoffset", WEAPON_OFS(angleoffset), FT_FLOAT|FT_ARRAY, 3}, |
| 72 |
{"extrazvelocity", WEAPON_OFS(extrazvelocity), FT_FLOAT}, |
| 73 |
{"ammoamount", WEAPON_OFS(ammoamount), FT_INT}, |
| 74 |
{"ammoindex", WEAPON_OFS(ammoindex), FT_INT}, |
| 75 |
{"activate", WEAPON_OFS(activate), FT_FLOAT}, |
| 76 |
{"reload", WEAPON_OFS(reload), FT_FLOAT}, |
| 77 |
{"spinup", WEAPON_OFS(spinup), FT_FLOAT}, |
| 78 |
{"spindown", WEAPON_OFS(spindown), FT_FLOAT}, |
| 79 |
{NULL, 0, 0, 0} |
| 80 |
}; |
| 81 |
|
| 82 |
|
| 83 |
static fielddef_t projectileinfo_fields[] = |
| 84 |
{ |
| 85 |
{"name", PROJECTILE_OFS(name), FT_STRING}, |
| 86 |
{"model", PROJECTILE_OFS(model), FT_STRING}, |
| 87 |
{"flags", PROJECTILE_OFS(flags), FT_INT}, |
| 88 |
{"gravity", PROJECTILE_OFS(gravity), FT_FLOAT}, |
| 89 |
{"damage", PROJECTILE_OFS(damage), FT_INT}, |
| 90 |
{"radius", PROJECTILE_OFS(radius), FT_FLOAT}, |
| 91 |
{"visdamage", PROJECTILE_OFS(visdamage), FT_INT}, |
| 92 |
{"damagetype", PROJECTILE_OFS(damagetype), FT_INT}, |
| 93 |
{"healthinc", PROJECTILE_OFS(healthinc), FT_INT}, |
| 94 |
{"push", PROJECTILE_OFS(push), FT_FLOAT}, |
| 95 |
{"detonation", PROJECTILE_OFS(detonation), FT_FLOAT}, |
| 96 |
{"bounce", PROJECTILE_OFS(bounce), FT_FLOAT}, |
| 97 |
{"bouncefric", PROJECTILE_OFS(bouncefric), FT_FLOAT}, |
| 98 |
{"bouncestop", PROJECTILE_OFS(bouncestop), FT_FLOAT}, |
| 99 |
|
| 100 |
{NULL, 0, 0, 0} |
| 101 |
}; |
| 102 |
|
| 103 |
static structdef_t weaponinfo_struct = |
| 104 |
{ |
| 105 |
sizeof(weaponinfo_t), weaponinfo_fields |
| 106 |
}; |
| 107 |
static structdef_t projectileinfo_struct = |
| 108 |
{ |
| 109 |
sizeof(projectileinfo_t), projectileinfo_fields |
| 110 |
}; |
| 111 |
|
| 112 |
|
| 113 |
typedef struct weaponconfig_s |
| 114 |
{ |
| 115 |
int numweapons; |
| 116 |
int numprojectiles; |
| 117 |
projectileinfo_t *projectileinfo; |
| 118 |
weaponinfo_t *weaponinfo; |
| 119 |
} weaponconfig_t; |
| 120 |
|
| 121 |
|
| 122 |
typedef struct bot_weaponstate_s |
| 123 |
{ |
| 124 |
struct weightconfig_s *weaponweightconfig; |
| 125 |
int *weaponweightindex; |
| 126 |
} bot_weaponstate_t; |
| 127 |
|
| 128 |
static bot_weaponstate_t *botweaponstates[MAX_CLIENTS+1]; |
| 129 |
static weaponconfig_t *weaponconfig; |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
int BotValidWeaponNumber(int weaponnum) |
| 138 |
{ |
| 139 |
if (weaponnum <= 0 || weaponnum > weaponconfig->numweapons) |
| 140 |
{ |
| 141 |
botimport.Print(PRT_ERROR, "weapon number out of range\n"); |
| 142 |
return qfalse; |
| 143 |
} |
| 144 |
return qtrue; |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
bot_weaponstate_t *BotWeaponStateFromHandle(int handle) |
| 153 |
{ |
| 154 |
if (handle <= 0 || handle > MAX_CLIENTS) |
| 155 |
{ |
| 156 |
botimport.Print(PRT_FATAL, "weapon state handle %d out of range\n", handle); |
| 157 |
return NULL; |
| 158 |
} |
| 159 |
if (!botweaponstates[handle]) |
| 160 |
{ |
| 161 |
botimport.Print(PRT_FATAL, "invalid weapon state %d\n", handle); |
| 162 |
return NULL; |
| 163 |
} |
| 164 |
return botweaponstates[handle]; |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
#ifdef DEBUG_AI_WEAP |
| 173 |
void DumpWeaponConfig(weaponconfig_t *wc) |
| 174 |
{ |
| 175 |
FILE *fp; |
| 176 |
int i; |
| 177 |
|
| 178 |
fp = Log_FileStruct(); |
| 179 |
if (!fp) return; |
| 180 |
for (i = 0; i < wc->numprojectiles; i++) |
| 181 |
{ |
| 182 |
WriteStructure(fp, &projectileinfo_struct, (char *) &wc->projectileinfo[i]); |
| 183 |
Log_Flush(); |
| 184 |
} |
| 185 |
for (i = 0; i < wc->numweapons; i++) |
| 186 |
{ |
| 187 |
WriteStructure(fp, &weaponinfo_struct, (char *) &wc->weaponinfo[i]); |
| 188 |
Log_Flush(); |
| 189 |
} |
| 190 |
} |
| 191 |
#endif |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
weaponconfig_t *LoadWeaponConfig(char *filename) |
| 199 |
{ |
| 200 |
int max_weaponinfo, max_projectileinfo; |
| 201 |
token_t token; |
| 202 |
char path[MAX_QPATH]; |
| 203 |
int i, j; |
| 204 |
source_t *source; |
| 205 |
weaponconfig_t *wc; |
| 206 |
weaponinfo_t weaponinfo; |
| 207 |
|
| 208 |
max_weaponinfo = (int) LibVarValue("max_weaponinfo", "32"); |
| 209 |
if (max_weaponinfo < 0) |
| 210 |
{ |
| 211 |
botimport.Print(PRT_ERROR, "max_weaponinfo = %d\n", max_weaponinfo); |
| 212 |
max_weaponinfo = 32; |
| 213 |
LibVarSet("max_weaponinfo", "32"); |
| 214 |
} |
| 215 |
max_projectileinfo = (int) LibVarValue("max_projectileinfo", "32"); |
| 216 |
if (max_projectileinfo < 0) |
| 217 |
{ |
| 218 |
botimport.Print(PRT_ERROR, "max_projectileinfo = %d\n", max_projectileinfo); |
| 219 |
max_projectileinfo = 32; |
| 220 |
LibVarSet("max_projectileinfo", "32"); |
| 221 |
} |
| 222 |
Q_strncpyz(path, filename, sizeof(path)); |
| 223 |
PC_SetBaseFolder(BOTFILESBASEFOLDER); |
| 224 |
source = LoadSourceFile(path); |
| 225 |
if (!source) |
| 226 |
{ |
| 227 |
botimport.Print(PRT_ERROR, "couldn't load %s\n", path); |
| 228 |
return NULL; |
| 229 |
} |
| 230 |
|
| 231 |
wc = (weaponconfig_t *) GetClearedHunkMemory(sizeof(weaponconfig_t) + |
| 232 |
max_weaponinfo * sizeof(weaponinfo_t) + |
| 233 |
max_projectileinfo * sizeof(projectileinfo_t)); |
| 234 |
wc->weaponinfo = (weaponinfo_t *) ((char *) wc + sizeof(weaponconfig_t)); |
| 235 |
wc->projectileinfo = (projectileinfo_t *) ((char *) wc->weaponinfo + |
| 236 |
max_weaponinfo * sizeof(weaponinfo_t)); |
| 237 |
wc->numweapons = max_weaponinfo; |
| 238 |
wc->numprojectiles = 0; |
| 239 |
|
| 240 |
while(PC_ReadToken(source, &token)) |
| 241 |
{ |
| 242 |
if (!strcmp(token.string, "weaponinfo")) |
| 243 |
{ |
| 244 |
Com_Memset(&weaponinfo, 0, sizeof(weaponinfo_t)); |
| 245 |
if (!ReadStructure(source, &weaponinfo_struct, (char *) &weaponinfo)) |
| 246 |
{ |
| 247 |
FreeMemory(wc); |
| 248 |
FreeSource(source); |
| 249 |
return NULL; |
| 250 |
} |
| 251 |
if (weaponinfo.number < 0 || weaponinfo.number >= max_weaponinfo) |
| 252 |
{ |
| 253 |
botimport.Print(PRT_ERROR, "weapon info number %d out of range in %s\n", weaponinfo.number, path); |
| 254 |
FreeMemory(wc); |
| 255 |
FreeSource(source); |
| 256 |
return NULL; |
| 257 |
} |
| 258 |
Com_Memcpy(&wc->weaponinfo[weaponinfo.number], &weaponinfo, sizeof(weaponinfo_t)); |
| 259 |
wc->weaponinfo[weaponinfo.number].valid = qtrue; |
| 260 |
} |
| 261 |
else if (!strcmp(token.string, "projectileinfo")) |
| 262 |
{ |
| 263 |
if (wc->numprojectiles >= max_projectileinfo) |
| 264 |
{ |
| 265 |
botimport.Print(PRT_ERROR, "more than %d projectiles defined in %s\n", max_projectileinfo, path); |
| 266 |
FreeMemory(wc); |
| 267 |
FreeSource(source); |
| 268 |
return NULL; |
| 269 |
} |
| 270 |
Com_Memset(&wc->projectileinfo[wc->numprojectiles], 0, sizeof(projectileinfo_t)); |
| 271 |
if (!ReadStructure(source, &projectileinfo_struct, (char *) &wc->projectileinfo[wc->numprojectiles])) |
| 272 |
{ |
| 273 |
FreeMemory(wc); |
| 274 |
FreeSource(source); |
| 275 |
return NULL; |
| 276 |
} |
| 277 |
wc->numprojectiles++; |
| 278 |
} |
| 279 |
else |
| 280 |
{ |
| 281 |
botimport.Print(PRT_ERROR, "unknown definition %s in %s\n", token.string, path); |
| 282 |
FreeMemory(wc); |
| 283 |
FreeSource(source); |
| 284 |
return NULL; |
| 285 |
} |
| 286 |
} |
| 287 |
FreeSource(source); |
| 288 |
|
| 289 |
for (i = 0; i < wc->numweapons; i++) |
| 290 |
{ |
| 291 |
if (!wc->weaponinfo[i].valid) continue; |
| 292 |
if (!wc->weaponinfo[i].name[0]) |
| 293 |
{ |
| 294 |
botimport.Print(PRT_ERROR, "weapon %d has no name in %s\n", i, path); |
| 295 |
FreeMemory(wc); |
| 296 |
return NULL; |
| 297 |
} |
| 298 |
if (!wc->weaponinfo[i].projectile[0]) |
| 299 |
{ |
| 300 |
botimport.Print(PRT_ERROR, "weapon %s has no projectile in %s\n", wc->weaponinfo[i].name, path); |
| 301 |
FreeMemory(wc); |
| 302 |
return NULL; |
| 303 |
} |
| 304 |
|
| 305 |
for (j = 0; j < wc->numprojectiles; j++) |
| 306 |
{ |
| 307 |
if (!strcmp(wc->projectileinfo[j].name, wc->weaponinfo[i].projectile)) |
| 308 |
{ |
| 309 |
Com_Memcpy(&wc->weaponinfo[i].proj, &wc->projectileinfo[j], sizeof(projectileinfo_t)); |
| 310 |
break; |
| 311 |
} |
| 312 |
} |
| 313 |
if (j == wc->numprojectiles) |
| 314 |
{ |
| 315 |
botimport.Print(PRT_ERROR, "weapon %s uses undefined projectile in %s\n", wc->weaponinfo[i].name, path); |
| 316 |
FreeMemory(wc); |
| 317 |
return NULL; |
| 318 |
} |
| 319 |
} |
| 320 |
if (!wc->numweapons) botimport.Print(PRT_WARNING, "no weapon info loaded\n"); |
| 321 |
botimport.Print(PRT_MESSAGE, "loaded %s\n", path); |
| 322 |
return wc; |
| 323 |
} |
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
int *WeaponWeightIndex(weightconfig_t *wwc, weaponconfig_t *wc) |
| 331 |
{ |
| 332 |
int *index, i; |
| 333 |
|
| 334 |
|
| 335 |
index = (int *) GetClearedMemory(sizeof(int) * wc->numweapons); |
| 336 |
|
| 337 |
for (i = 0; i < wc->numweapons; i++) |
| 338 |
{ |
| 339 |
index[i] = FindFuzzyWeight(wwc, wc->weaponinfo[i].name); |
| 340 |
} |
| 341 |
return index; |
| 342 |
} |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
void BotFreeWeaponWeights(int weaponstate) |
| 350 |
{ |
| 351 |
bot_weaponstate_t *ws; |
| 352 |
|
| 353 |
ws = BotWeaponStateFromHandle(weaponstate); |
| 354 |
if (!ws) return; |
| 355 |
if (ws->weaponweightconfig) FreeWeightConfig(ws->weaponweightconfig); |
| 356 |
if (ws->weaponweightindex) FreeMemory(ws->weaponweightindex); |
| 357 |
} |
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
int BotLoadWeaponWeights(int weaponstate, char *filename) |
| 365 |
{ |
| 366 |
bot_weaponstate_t *ws; |
| 367 |
|
| 368 |
ws = BotWeaponStateFromHandle(weaponstate); |
| 369 |
if (!ws) return BLERR_CANNOTLOADWEAPONWEIGHTS; |
| 370 |
BotFreeWeaponWeights(weaponstate); |
| 371 |
|
| 372 |
ws->weaponweightconfig = ReadWeightConfig(filename); |
| 373 |
if (!ws->weaponweightconfig) |
| 374 |
{ |
| 375 |
botimport.Print(PRT_FATAL, "couldn't load weapon config %s\n", filename); |
| 376 |
return BLERR_CANNOTLOADWEAPONWEIGHTS; |
| 377 |
} |
| 378 |
if (!weaponconfig) return BLERR_CANNOTLOADWEAPONCONFIG; |
| 379 |
ws->weaponweightindex = WeaponWeightIndex(ws->weaponweightconfig, weaponconfig); |
| 380 |
return BLERR_NOERROR; |
| 381 |
} |
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
void BotGetWeaponInfo(int weaponstate, int weapon, weaponinfo_t *weaponinfo) |
| 389 |
{ |
| 390 |
bot_weaponstate_t *ws; |
| 391 |
|
| 392 |
if (!BotValidWeaponNumber(weapon)) return; |
| 393 |
ws = BotWeaponStateFromHandle(weaponstate); |
| 394 |
if (!ws) return; |
| 395 |
if (!weaponconfig) return; |
| 396 |
Com_Memcpy(weaponinfo, &weaponconfig->weaponinfo[weapon], sizeof(weaponinfo_t)); |
| 397 |
} |
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
int BotChooseBestFightWeapon(int weaponstate, int *inventory) |
| 405 |
{ |
| 406 |
int i, index, bestweapon; |
| 407 |
float weight, bestweight; |
| 408 |
weaponconfig_t *wc; |
| 409 |
bot_weaponstate_t *ws; |
| 410 |
|
| 411 |
ws = BotWeaponStateFromHandle(weaponstate); |
| 412 |
if (!ws) return 0; |
| 413 |
wc = weaponconfig; |
| 414 |
if (!weaponconfig) return 0; |
| 415 |
|
| 416 |
|
| 417 |
if (!ws->weaponweightconfig) return 0; |
| 418 |
|
| 419 |
bestweight = 0; |
| 420 |
bestweapon = 0; |
| 421 |
for (i = 0; i < wc->numweapons; i++) |
| 422 |
{ |
| 423 |
if (!wc->weaponinfo[i].valid) continue; |
| 424 |
index = ws->weaponweightindex[i]; |
| 425 |
if (index < 0) continue; |
| 426 |
weight = FuzzyWeight(inventory, ws->weaponweightconfig, index); |
| 427 |
if (weight > bestweight) |
| 428 |
{ |
| 429 |
bestweight = weight; |
| 430 |
bestweapon = i; |
| 431 |
} |
| 432 |
} |
| 433 |
return bestweapon; |
| 434 |
} |
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
void BotResetWeaponState(int weaponstate) |
| 442 |
{ |
| 443 |
} |
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
int BotAllocWeaponState(void) |
| 451 |
{ |
| 452 |
int i; |
| 453 |
|
| 454 |
for (i = 1; i <= MAX_CLIENTS; i++) |
| 455 |
{ |
| 456 |
if (!botweaponstates[i]) |
| 457 |
{ |
| 458 |
botweaponstates[i] = GetClearedMemory(sizeof(bot_weaponstate_t)); |
| 459 |
return i; |
| 460 |
} |
| 461 |
} |
| 462 |
return 0; |
| 463 |
} |
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
void BotFreeWeaponState(int handle) |
| 471 |
{ |
| 472 |
if (handle <= 0 || handle > MAX_CLIENTS) |
| 473 |
{ |
| 474 |
botimport.Print(PRT_FATAL, "weapon state handle %d out of range\n", handle); |
| 475 |
return; |
| 476 |
} |
| 477 |
if (!botweaponstates[handle]) |
| 478 |
{ |
| 479 |
botimport.Print(PRT_FATAL, "invalid weapon state %d\n", handle); |
| 480 |
return; |
| 481 |
} |
| 482 |
BotFreeWeaponWeights(handle); |
| 483 |
FreeMemory(botweaponstates[handle]); |
| 484 |
botweaponstates[handle] = NULL; |
| 485 |
} |
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
int BotSetupWeaponAI(void) |
| 493 |
{ |
| 494 |
char *file; |
| 495 |
|
| 496 |
file = LibVarString("weaponconfig", "weapons.c"); |
| 497 |
weaponconfig = LoadWeaponConfig(file); |
| 498 |
if (!weaponconfig) |
| 499 |
{ |
| 500 |
botimport.Print(PRT_FATAL, "couldn't load the weapon config\n"); |
| 501 |
return BLERR_CANNOTLOADWEAPONCONFIG; |
| 502 |
} |
| 503 |
|
| 504 |
#ifdef DEBUG_AI_WEAP |
| 505 |
DumpWeaponConfig(weaponconfig); |
| 506 |
#endif |
| 507 |
|
| 508 |
return BLERR_NOERROR; |
| 509 |
} |
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
void BotShutdownWeaponAI(void) |
| 517 |
{ |
| 518 |
int i; |
| 519 |
|
| 520 |
if (weaponconfig) FreeMemory(weaponconfig); |
| 521 |
weaponconfig = NULL; |
| 522 |
|
| 523 |
for (i = 1; i <= MAX_CLIENTS; i++) |
| 524 |
{ |
| 525 |
if (botweaponstates[i]) |
| 526 |
{ |
| 527 |
BotFreeWeaponState(i); |
| 528 |
} |
| 529 |
} |
| 530 |
} |
| 531 |
|