| 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_log.h" |
| 34 |
#include "l_memory.h" |
| 35 |
#include "l_utils.h" |
| 36 |
#include "l_script.h" |
| 37 |
#include "l_precomp.h" |
| 38 |
#include "l_struct.h" |
| 39 |
#include "l_libvar.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_char.h" |
| 46 |
|
| 47 |
#define MAX_CHARACTERISTICS 80 |
| 48 |
|
| 49 |
#define CT_INTEGER 1 |
| 50 |
#define CT_FLOAT 2 |
| 51 |
#define CT_STRING 3 |
| 52 |
|
| 53 |
#define DEFAULT_CHARACTER "bots/default_c.c" |
| 54 |
|
| 55 |
|
| 56 |
union cvalue |
| 57 |
{ |
| 58 |
int integer; |
| 59 |
float _float; |
| 60 |
char *string; |
| 61 |
}; |
| 62 |
|
| 63 |
typedef struct bot_characteristic_s |
| 64 |
{ |
| 65 |
char type; |
| 66 |
union cvalue value; |
| 67 |
} bot_characteristic_t; |
| 68 |
|
| 69 |
|
| 70 |
typedef struct bot_character_s |
| 71 |
{ |
| 72 |
char filename[MAX_QPATH]; |
| 73 |
float skill; |
| 74 |
bot_characteristic_t c[1]; |
| 75 |
} bot_character_t; |
| 76 |
|
| 77 |
bot_character_t *botcharacters[MAX_CLIENTS + 1]; |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
bot_character_t *BotCharacterFromHandle(int handle) |
| 86 |
{ |
| 87 |
if (handle <= 0 || handle > MAX_CLIENTS) |
| 88 |
{ |
| 89 |
botimport.Print(PRT_FATAL, "character handle %d out of range\n", handle); |
| 90 |
return NULL; |
| 91 |
} |
| 92 |
if (!botcharacters[handle]) |
| 93 |
{ |
| 94 |
botimport.Print(PRT_FATAL, "invalid character %d\n", handle); |
| 95 |
return NULL; |
| 96 |
} |
| 97 |
return botcharacters[handle]; |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
void BotDumpCharacter(bot_character_t *ch) |
| 106 |
{ |
| 107 |
int i; |
| 108 |
|
| 109 |
Log_Write("%s\n", ch->filename); |
| 110 |
Log_Write("skill %.1f\n", ch->skill); |
| 111 |
Log_Write("{\n"); |
| 112 |
for (i = 0; i < MAX_CHARACTERISTICS; i++) |
| 113 |
{ |
| 114 |
switch(ch->c[i].type) |
| 115 |
{ |
| 116 |
case CT_INTEGER: Log_Write(" %4d %d\n", i, ch->c[i].value.integer); break; |
| 117 |
case CT_FLOAT: Log_Write(" %4d %f\n", i, ch->c[i].value._float); break; |
| 118 |
case CT_STRING: Log_Write(" %4d %s\n", i, ch->c[i].value.string); break; |
| 119 |
} |
| 120 |
} |
| 121 |
Log_Write("}\n"); |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
void BotFreeCharacterStrings(bot_character_t *ch) |
| 130 |
{ |
| 131 |
int i; |
| 132 |
|
| 133 |
for (i = 0; i < MAX_CHARACTERISTICS; i++) |
| 134 |
{ |
| 135 |
if (ch->c[i].type == CT_STRING) |
| 136 |
{ |
| 137 |
FreeMemory(ch->c[i].value.string); |
| 138 |
} |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
void BotFreeCharacter2(int handle) |
| 148 |
{ |
| 149 |
if (handle <= 0 || handle > MAX_CLIENTS) |
| 150 |
{ |
| 151 |
botimport.Print(PRT_FATAL, "character handle %d out of range\n", handle); |
| 152 |
return; |
| 153 |
} |
| 154 |
if (!botcharacters[handle]) |
| 155 |
{ |
| 156 |
botimport.Print(PRT_FATAL, "invalid character %d\n", handle); |
| 157 |
return; |
| 158 |
} |
| 159 |
BotFreeCharacterStrings(botcharacters[handle]); |
| 160 |
FreeMemory(botcharacters[handle]); |
| 161 |
botcharacters[handle] = NULL; |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
void BotFreeCharacter(int handle) |
| 170 |
{ |
| 171 |
if (!LibVarGetValue("bot_reloadcharacters")) return; |
| 172 |
BotFreeCharacter2(handle); |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
void BotDefaultCharacteristics(bot_character_t *ch, bot_character_t *defaultch) |
| 181 |
{ |
| 182 |
int i; |
| 183 |
|
| 184 |
for (i = 0; i < MAX_CHARACTERISTICS; i++) |
| 185 |
{ |
| 186 |
if (ch->c[i].type) continue; |
| 187 |
|
| 188 |
if (defaultch->c[i].type == CT_FLOAT) |
| 189 |
{ |
| 190 |
ch->c[i].type = CT_FLOAT; |
| 191 |
ch->c[i].value._float = defaultch->c[i].value._float; |
| 192 |
} |
| 193 |
else if (defaultch->c[i].type == CT_INTEGER) |
| 194 |
{ |
| 195 |
ch->c[i].type = CT_INTEGER; |
| 196 |
ch->c[i].value.integer = defaultch->c[i].value.integer; |
| 197 |
} |
| 198 |
else if (defaultch->c[i].type == CT_STRING) |
| 199 |
{ |
| 200 |
ch->c[i].type = CT_STRING; |
| 201 |
ch->c[i].value.string = (char *) GetMemory(strlen(defaultch->c[i].value.string)+1); |
| 202 |
strcpy(ch->c[i].value.string, defaultch->c[i].value.string); |
| 203 |
} |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
bot_character_t *BotLoadCharacterFromFile(char *charfile, int skill) |
| 213 |
{ |
| 214 |
int indent, index, foundcharacter; |
| 215 |
bot_character_t *ch; |
| 216 |
source_t *source; |
| 217 |
token_t token; |
| 218 |
|
| 219 |
foundcharacter = qfalse; |
| 220 |
|
| 221 |
PC_SetBaseFolder(BOTFILESBASEFOLDER); |
| 222 |
source = LoadSourceFile(charfile); |
| 223 |
if (!source) |
| 224 |
{ |
| 225 |
botimport.Print(PRT_ERROR, "couldn't load %s\n", charfile); |
| 226 |
return NULL; |
| 227 |
} |
| 228 |
ch = (bot_character_t *) GetClearedMemory(sizeof(bot_character_t) + |
| 229 |
MAX_CHARACTERISTICS * sizeof(bot_characteristic_t)); |
| 230 |
strcpy(ch->filename, charfile); |
| 231 |
while(PC_ReadToken(source, &token)) |
| 232 |
{ |
| 233 |
if (!strcmp(token.string, "skill")) |
| 234 |
{ |
| 235 |
if (!PC_ExpectTokenType(source, TT_NUMBER, 0, &token)) |
| 236 |
{ |
| 237 |
FreeSource(source); |
| 238 |
BotFreeCharacterStrings(ch); |
| 239 |
FreeMemory(ch); |
| 240 |
return NULL; |
| 241 |
} |
| 242 |
if (!PC_ExpectTokenString(source, "{")) |
| 243 |
{ |
| 244 |
FreeSource(source); |
| 245 |
BotFreeCharacterStrings(ch); |
| 246 |
FreeMemory(ch); |
| 247 |
return NULL; |
| 248 |
} |
| 249 |
|
| 250 |
if (skill < 0 || (int)token.intvalue == skill) |
| 251 |
{ |
| 252 |
foundcharacter = qtrue; |
| 253 |
ch->skill = token.intvalue; |
| 254 |
while(PC_ExpectAnyToken(source, &token)) |
| 255 |
{ |
| 256 |
if (!strcmp(token.string, "}")) break; |
| 257 |
if (token.type != TT_NUMBER || !(token.subtype & TT_INTEGER)) |
| 258 |
{ |
| 259 |
SourceError(source, "expected integer index, found %s", token.string); |
| 260 |
FreeSource(source); |
| 261 |
BotFreeCharacterStrings(ch); |
| 262 |
FreeMemory(ch); |
| 263 |
return NULL; |
| 264 |
} |
| 265 |
index = token.intvalue; |
| 266 |
if (index < 0 || index > MAX_CHARACTERISTICS) |
| 267 |
{ |
| 268 |
SourceError(source, "characteristic index out of range [0, %d]", MAX_CHARACTERISTICS); |
| 269 |
FreeSource(source); |
| 270 |
BotFreeCharacterStrings(ch); |
| 271 |
FreeMemory(ch); |
| 272 |
return NULL; |
| 273 |
} |
| 274 |
if (ch->c[index].type) |
| 275 |
{ |
| 276 |
SourceError(source, "characteristic %d already initialized", index); |
| 277 |
FreeSource(source); |
| 278 |
BotFreeCharacterStrings(ch); |
| 279 |
FreeMemory(ch); |
| 280 |
return NULL; |
| 281 |
} |
| 282 |
if (!PC_ExpectAnyToken(source, &token)) |
| 283 |
{ |
| 284 |
FreeSource(source); |
| 285 |
BotFreeCharacterStrings(ch); |
| 286 |
FreeMemory(ch); |
| 287 |
return NULL; |
| 288 |
} |
| 289 |
if (token.type == TT_NUMBER) |
| 290 |
{ |
| 291 |
if (token.subtype & TT_FLOAT) |
| 292 |
{ |
| 293 |
ch->c[index].value._float = token.floatvalue; |
| 294 |
ch->c[index].type = CT_FLOAT; |
| 295 |
} |
| 296 |
else |
| 297 |
{ |
| 298 |
ch->c[index].value.integer = token.intvalue; |
| 299 |
ch->c[index].type = CT_INTEGER; |
| 300 |
} |
| 301 |
} |
| 302 |
else if (token.type == TT_STRING) |
| 303 |
{ |
| 304 |
StripDoubleQuotes(token.string); |
| 305 |
ch->c[index].value.string = GetMemory(strlen(token.string)+1); |
| 306 |
strcpy(ch->c[index].value.string, token.string); |
| 307 |
ch->c[index].type = CT_STRING; |
| 308 |
} |
| 309 |
else |
| 310 |
{ |
| 311 |
SourceError(source, "expected integer, float or string, found %s", token.string); |
| 312 |
FreeSource(source); |
| 313 |
BotFreeCharacterStrings(ch); |
| 314 |
FreeMemory(ch); |
| 315 |
return NULL; |
| 316 |
} |
| 317 |
} |
| 318 |
break; |
| 319 |
} |
| 320 |
else |
| 321 |
{ |
| 322 |
indent = 1; |
| 323 |
while(indent) |
| 324 |
{ |
| 325 |
if (!PC_ExpectAnyToken(source, &token)) |
| 326 |
{ |
| 327 |
FreeSource(source); |
| 328 |
BotFreeCharacterStrings(ch); |
| 329 |
FreeMemory(ch); |
| 330 |
return NULL; |
| 331 |
} |
| 332 |
if (!strcmp(token.string, "{")) indent++; |
| 333 |
else if (!strcmp(token.string, "}")) indent--; |
| 334 |
} |
| 335 |
} |
| 336 |
} |
| 337 |
else |
| 338 |
{ |
| 339 |
SourceError(source, "unknown definition %s", token.string); |
| 340 |
FreeSource(source); |
| 341 |
BotFreeCharacterStrings(ch); |
| 342 |
FreeMemory(ch); |
| 343 |
return NULL; |
| 344 |
} |
| 345 |
} |
| 346 |
FreeSource(source); |
| 347 |
|
| 348 |
if (!foundcharacter) |
| 349 |
{ |
| 350 |
BotFreeCharacterStrings(ch); |
| 351 |
FreeMemory(ch); |
| 352 |
return NULL; |
| 353 |
} |
| 354 |
return ch; |
| 355 |
} |
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
int BotFindCachedCharacter(char *charfile, float skill) |
| 363 |
{ |
| 364 |
int handle; |
| 365 |
|
| 366 |
for (handle = 1; handle <= MAX_CLIENTS; handle++) |
| 367 |
{ |
| 368 |
if ( !botcharacters[handle] ) continue; |
| 369 |
if ( strcmp( botcharacters[handle]->filename, charfile ) == 0 && |
| 370 |
(skill < 0 || fabs(botcharacters[handle]->skill - skill) < 0.01) ) |
| 371 |
{ |
| 372 |
return handle; |
| 373 |
} |
| 374 |
} |
| 375 |
return 0; |
| 376 |
} |
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
int BotLoadCachedCharacter(char *charfile, float skill, int reload) |
| 384 |
{ |
| 385 |
int handle, cachedhandle, intskill; |
| 386 |
bot_character_t *ch = NULL; |
| 387 |
#ifdef DEBUG |
| 388 |
int starttime; |
| 389 |
|
| 390 |
starttime = Sys_MilliSeconds(); |
| 391 |
#endif |
| 392 |
|
| 393 |
|
| 394 |
for (handle = 1; handle <= MAX_CLIENTS; handle++) |
| 395 |
{ |
| 396 |
if (!botcharacters[handle]) break; |
| 397 |
} |
| 398 |
if (handle > MAX_CLIENTS) return 0; |
| 399 |
|
| 400 |
if (!reload) |
| 401 |
{ |
| 402 |
cachedhandle = BotFindCachedCharacter(charfile, skill); |
| 403 |
if (cachedhandle) |
| 404 |
{ |
| 405 |
botimport.Print(PRT_MESSAGE, "loaded cached skill %f from %s\n", skill, charfile); |
| 406 |
return cachedhandle; |
| 407 |
} |
| 408 |
} |
| 409 |
|
| 410 |
intskill = (int) (skill + 0.5); |
| 411 |
|
| 412 |
ch = BotLoadCharacterFromFile(charfile, intskill); |
| 413 |
if (ch) |
| 414 |
{ |
| 415 |
botcharacters[handle] = ch; |
| 416 |
|
| 417 |
botimport.Print(PRT_MESSAGE, "loaded skill %d from %s\n", intskill, charfile); |
| 418 |
#ifdef DEBUG |
| 419 |
if (botDeveloper) |
| 420 |
{ |
| 421 |
botimport.Print(PRT_MESSAGE, "skill %d loaded in %d msec from %s\n", intskill, Sys_MilliSeconds() - starttime, charfile); |
| 422 |
} |
| 423 |
#endif |
| 424 |
return handle; |
| 425 |
} |
| 426 |
|
| 427 |
botimport.Print(PRT_WARNING, "couldn't find skill %d in %s\n", intskill, charfile); |
| 428 |
|
| 429 |
if (!reload) |
| 430 |
{ |
| 431 |
|
| 432 |
cachedhandle = BotFindCachedCharacter(DEFAULT_CHARACTER, skill); |
| 433 |
if (cachedhandle) |
| 434 |
{ |
| 435 |
botimport.Print(PRT_MESSAGE, "loaded cached default skill %d from %s\n", intskill, charfile); |
| 436 |
return cachedhandle; |
| 437 |
} |
| 438 |
} |
| 439 |
|
| 440 |
ch = BotLoadCharacterFromFile(DEFAULT_CHARACTER, intskill); |
| 441 |
if (ch) |
| 442 |
{ |
| 443 |
botcharacters[handle] = ch; |
| 444 |
botimport.Print(PRT_MESSAGE, "loaded default skill %d from %s\n", intskill, charfile); |
| 445 |
return handle; |
| 446 |
} |
| 447 |
|
| 448 |
if (!reload) |
| 449 |
{ |
| 450 |
|
| 451 |
cachedhandle = BotFindCachedCharacter(charfile, -1); |
| 452 |
if (cachedhandle) |
| 453 |
{ |
| 454 |
botimport.Print(PRT_MESSAGE, "loaded cached skill %f from %s\n", botcharacters[cachedhandle]->skill, charfile); |
| 455 |
return cachedhandle; |
| 456 |
} |
| 457 |
} |
| 458 |
|
| 459 |
ch = BotLoadCharacterFromFile(charfile, -1); |
| 460 |
if (ch) |
| 461 |
{ |
| 462 |
botcharacters[handle] = ch; |
| 463 |
botimport.Print(PRT_MESSAGE, "loaded skill %f from %s\n", ch->skill, charfile); |
| 464 |
return handle; |
| 465 |
} |
| 466 |
|
| 467 |
if (!reload) |
| 468 |
{ |
| 469 |
|
| 470 |
cachedhandle = BotFindCachedCharacter(DEFAULT_CHARACTER, -1); |
| 471 |
if (cachedhandle) |
| 472 |
{ |
| 473 |
botimport.Print(PRT_MESSAGE, "loaded cached default skill %f from %s\n", botcharacters[cachedhandle]->skill, charfile); |
| 474 |
return cachedhandle; |
| 475 |
} |
| 476 |
} |
| 477 |
|
| 478 |
ch = BotLoadCharacterFromFile(DEFAULT_CHARACTER, -1); |
| 479 |
if (ch) |
| 480 |
{ |
| 481 |
botcharacters[handle] = ch; |
| 482 |
botimport.Print(PRT_MESSAGE, "loaded default skill %f from %s\n", ch->skill, charfile); |
| 483 |
return handle; |
| 484 |
} |
| 485 |
|
| 486 |
botimport.Print(PRT_WARNING, "couldn't load any skill from %s\n", charfile); |
| 487 |
|
| 488 |
return 0; |
| 489 |
} |
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
int BotLoadCharacterSkill(char *charfile, float skill) |
| 497 |
{ |
| 498 |
int ch, defaultch; |
| 499 |
|
| 500 |
defaultch = BotLoadCachedCharacter(DEFAULT_CHARACTER, skill, qfalse); |
| 501 |
ch = BotLoadCachedCharacter(charfile, skill, LibVarGetValue("bot_reloadcharacters")); |
| 502 |
|
| 503 |
if (defaultch && ch) |
| 504 |
{ |
| 505 |
BotDefaultCharacteristics(botcharacters[ch], botcharacters[defaultch]); |
| 506 |
} |
| 507 |
|
| 508 |
return ch; |
| 509 |
} |
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
int BotInterpolateCharacters(int handle1, int handle2, float desiredskill) |
| 517 |
{ |
| 518 |
bot_character_t *ch1, *ch2, *out; |
| 519 |
int i, handle; |
| 520 |
float scale; |
| 521 |
|
| 522 |
ch1 = BotCharacterFromHandle(handle1); |
| 523 |
ch2 = BotCharacterFromHandle(handle2); |
| 524 |
if (!ch1 || !ch2) |
| 525 |
return 0; |
| 526 |
|
| 527 |
for (handle = 1; handle <= MAX_CLIENTS; handle++) |
| 528 |
{ |
| 529 |
if (!botcharacters[handle]) break; |
| 530 |
} |
| 531 |
if (handle > MAX_CLIENTS) return 0; |
| 532 |
out = (bot_character_t *) GetClearedMemory(sizeof(bot_character_t) + |
| 533 |
MAX_CHARACTERISTICS * sizeof(bot_characteristic_t)); |
| 534 |
out->skill = desiredskill; |
| 535 |
strcpy(out->filename, ch1->filename); |
| 536 |
botcharacters[handle] = out; |
| 537 |
|
| 538 |
scale = (float) (desiredskill - ch1->skill) / (ch2->skill - ch1->skill); |
| 539 |
for (i = 0; i < MAX_CHARACTERISTICS; i++) |
| 540 |
{ |
| 541 |
|
| 542 |
if (ch1->c[i].type == CT_FLOAT && ch2->c[i].type == CT_FLOAT) |
| 543 |
{ |
| 544 |
out->c[i].type = CT_FLOAT; |
| 545 |
out->c[i].value._float = ch1->c[i].value._float + |
| 546 |
(ch2->c[i].value._float - ch1->c[i].value._float) * scale; |
| 547 |
} |
| 548 |
else if (ch1->c[i].type == CT_INTEGER) |
| 549 |
{ |
| 550 |
out->c[i].type = CT_INTEGER; |
| 551 |
out->c[i].value.integer = ch1->c[i].value.integer; |
| 552 |
} |
| 553 |
else if (ch1->c[i].type == CT_STRING) |
| 554 |
{ |
| 555 |
out->c[i].type = CT_STRING; |
| 556 |
out->c[i].value.string = (char *) GetMemory(strlen(ch1->c[i].value.string)+1); |
| 557 |
strcpy(out->c[i].value.string, ch1->c[i].value.string); |
| 558 |
} |
| 559 |
} |
| 560 |
return handle; |
| 561 |
} |
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
int BotLoadCharacter(char *charfile, float skill) |
| 569 |
{ |
| 570 |
int firstskill, secondskill, handle; |
| 571 |
|
| 572 |
|
| 573 |
if (skill < 1.0) skill = 1.0; |
| 574 |
else if (skill > 5.0) skill = 5.0; |
| 575 |
|
| 576 |
if (skill == 1.0 || skill == 4.0 || skill == 5.0) |
| 577 |
{ |
| 578 |
return BotLoadCharacterSkill(charfile, skill); |
| 579 |
} |
| 580 |
|
| 581 |
handle = BotFindCachedCharacter(charfile, skill); |
| 582 |
if (handle) |
| 583 |
{ |
| 584 |
botimport.Print(PRT_MESSAGE, "loaded cached skill %f from %s\n", skill, charfile); |
| 585 |
return handle; |
| 586 |
} |
| 587 |
if (skill < 4.0) |
| 588 |
{ |
| 589 |
|
| 590 |
firstskill = BotLoadCharacterSkill(charfile, 1); |
| 591 |
if (!firstskill) return 0; |
| 592 |
secondskill = BotLoadCharacterSkill(charfile, 4); |
| 593 |
if (!secondskill) return firstskill; |
| 594 |
} |
| 595 |
else |
| 596 |
{ |
| 597 |
|
| 598 |
firstskill = BotLoadCharacterSkill(charfile, 4); |
| 599 |
if (!firstskill) return 0; |
| 600 |
secondskill = BotLoadCharacterSkill(charfile, 5); |
| 601 |
if (!secondskill) return firstskill; |
| 602 |
} |
| 603 |
|
| 604 |
handle = BotInterpolateCharacters(firstskill, secondskill, skill); |
| 605 |
if (!handle) return 0; |
| 606 |
|
| 607 |
BotDumpCharacter(botcharacters[handle]); |
| 608 |
|
| 609 |
return handle; |
| 610 |
} |
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
int CheckCharacteristicIndex(int character, int index) |
| 618 |
{ |
| 619 |
bot_character_t *ch; |
| 620 |
|
| 621 |
ch = BotCharacterFromHandle(character); |
| 622 |
if (!ch) return qfalse; |
| 623 |
if (index < 0 || index >= MAX_CHARACTERISTICS) |
| 624 |
{ |
| 625 |
botimport.Print(PRT_ERROR, "characteristic %d does not exist\n", index); |
| 626 |
return qfalse; |
| 627 |
} |
| 628 |
if (!ch->c[index].type) |
| 629 |
{ |
| 630 |
botimport.Print(PRT_ERROR, "characteristic %d is not initialized\n", index); |
| 631 |
return qfalse; |
| 632 |
} |
| 633 |
return qtrue; |
| 634 |
} |
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
float Characteristic_Float(int character, int index) |
| 642 |
{ |
| 643 |
bot_character_t *ch; |
| 644 |
|
| 645 |
ch = BotCharacterFromHandle(character); |
| 646 |
if (!ch) return 0; |
| 647 |
|
| 648 |
if (!CheckCharacteristicIndex(character, index)) return 0; |
| 649 |
|
| 650 |
if (ch->c[index].type == CT_INTEGER) |
| 651 |
{ |
| 652 |
return (float) ch->c[index].value.integer; |
| 653 |
} |
| 654 |
|
| 655 |
else if (ch->c[index].type == CT_FLOAT) |
| 656 |
{ |
| 657 |
return ch->c[index].value._float; |
| 658 |
} |
| 659 |
|
| 660 |
else |
| 661 |
{ |
| 662 |
botimport.Print(PRT_ERROR, "characteristic %d is not a float\n", index); |
| 663 |
return 0; |
| 664 |
} |
| 665 |
|
| 666 |
} |
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
float Characteristic_BFloat(int character, int index, float min, float max) |
| 674 |
{ |
| 675 |
float value; |
| 676 |
bot_character_t *ch; |
| 677 |
|
| 678 |
ch = BotCharacterFromHandle(character); |
| 679 |
if (!ch) return 0; |
| 680 |
if (min > max) |
| 681 |
{ |
| 682 |
botimport.Print(PRT_ERROR, "cannot bound characteristic %d between %f and %f\n", index, min, max); |
| 683 |
return 0; |
| 684 |
} |
| 685 |
value = Characteristic_Float(character, index); |
| 686 |
if (value < min) return min; |
| 687 |
if (value > max) return max; |
| 688 |
return value; |
| 689 |
} |
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
int Characteristic_Integer(int character, int index) |
| 697 |
{ |
| 698 |
bot_character_t *ch; |
| 699 |
|
| 700 |
ch = BotCharacterFromHandle(character); |
| 701 |
if (!ch) return 0; |
| 702 |
|
| 703 |
if (!CheckCharacteristicIndex(character, index)) return 0; |
| 704 |
|
| 705 |
if (ch->c[index].type == CT_INTEGER) |
| 706 |
{ |
| 707 |
return ch->c[index].value.integer; |
| 708 |
} |
| 709 |
|
| 710 |
else if (ch->c[index].type == CT_FLOAT) |
| 711 |
{ |
| 712 |
return (int) ch->c[index].value._float; |
| 713 |
} |
| 714 |
else |
| 715 |
{ |
| 716 |
botimport.Print(PRT_ERROR, "characteristic %d is not an integer\n", index); |
| 717 |
return 0; |
| 718 |
} |
| 719 |
|
| 720 |
} |
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
int Characteristic_BInteger(int character, int index, int min, int max) |
| 728 |
{ |
| 729 |
int value; |
| 730 |
bot_character_t *ch; |
| 731 |
|
| 732 |
ch = BotCharacterFromHandle(character); |
| 733 |
if (!ch) return 0; |
| 734 |
if (min > max) |
| 735 |
{ |
| 736 |
botimport.Print(PRT_ERROR, "cannot bound characteristic %d between %d and %d\n", index, min, max); |
| 737 |
return 0; |
| 738 |
} |
| 739 |
value = Characteristic_Integer(character, index); |
| 740 |
if (value < min) return min; |
| 741 |
if (value > max) return max; |
| 742 |
return value; |
| 743 |
} |
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
void Characteristic_String(int character, int index, char *buf, int size) |
| 751 |
{ |
| 752 |
bot_character_t *ch; |
| 753 |
|
| 754 |
ch = BotCharacterFromHandle(character); |
| 755 |
if (!ch) return; |
| 756 |
|
| 757 |
if (!CheckCharacteristicIndex(character, index)) return; |
| 758 |
|
| 759 |
if (ch->c[index].type == CT_STRING) |
| 760 |
{ |
| 761 |
Q_strncpyz(buf, ch->c[index].value.string, size); |
| 762 |
} |
| 763 |
else |
| 764 |
{ |
| 765 |
botimport.Print(PRT_ERROR, "characteristic %d is not a string\n", index); |
| 766 |
} |
| 767 |
} |
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
void BotShutdownCharacters(void) |
| 775 |
{ |
| 776 |
int handle; |
| 777 |
|
| 778 |
for (handle = 1; handle <= MAX_CLIENTS; handle++) |
| 779 |
{ |
| 780 |
if (botcharacters[handle]) |
| 781 |
{ |
| 782 |
BotFreeCharacter2(handle); |
| 783 |
} |
| 784 |
} |
| 785 |
} |
| 786 |
|