| 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 |
#include "global.h" |
| 59 |
|
| 60 |
#include <assert.h> |
| 61 |
#include <ctype.h> |
| 62 |
#include <stdlib.h> |
| 63 |
#include <string.h> |
| 64 |
|
| 65 |
#include "sproto.h" |
| 66 |
#include "output_file.h" |
| 67 |
|
| 68 |
struct knowledge_player; |
| 69 |
struct knowledge_type; |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
typedef void (*knowledge_summary)(const char *code, StringBuffer *buf); |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
typedef void (*knowledge_detail)(const char *code, StringBuffer *buf); |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
typedef int (*knowledge_is_valid_item)(const char *code); |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
typedef int (*knowledge_add_item)(struct knowledge_player *current, const char *item, const struct knowledge_type *type, player *pl); |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
typedef StringBuffer* (*knowledge_can_use_alchemy)(sstring code, const char *item, StringBuffer *buf, int index); |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
typedef const Face *(*knowledge_face)(sstring code); |
| 118 |
|
| 119 |
struct knowledge_item; |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
typedef void (*knowledge_attempt)(player *pl, const struct knowledge_item *item); |
| 127 |
|
| 128 |
|
| 129 |
typedef struct knowledge_type { |
| 130 |
const char *type; |
| 131 |
knowledge_summary summary; |
| 132 |
knowledge_detail detail; |
| 133 |
knowledge_is_valid_item validate; |
| 134 |
knowledge_add_item add; |
| 135 |
const char *name; |
| 136 |
knowledge_can_use_alchemy use_alchemy; |
| 137 |
knowledge_attempt attempt_alchemy; |
| 138 |
const char *face; |
| 139 |
knowledge_face item_face; |
| 140 |
} knowledge_type; |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
typedef struct knowledge_item { |
| 145 |
sstring item; |
| 146 |
const knowledge_type* handler; |
| 147 |
} knowledge_item; |
| 148 |
|
| 149 |
|
| 150 |
typedef struct knowledge_player { |
| 151 |
sstring player_name; |
| 152 |
struct knowledge_item **items; |
| 153 |
int item_count; |
| 154 |
int item_allocated; |
| 155 |
int sent_up_to; |
| 156 |
|
| 157 |
struct knowledge_player *next; |
| 158 |
} knowledge_player; |
| 159 |
|
| 160 |
|
| 161 |
static knowledge_player *knowledge_global = NULL; |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
static const recipe* knowledge_alchemy_get_recipe(const char *value) { |
| 170 |
const recipe *rec; |
| 171 |
recipelist *rl; |
| 172 |
int count, index; |
| 173 |
const char *dot; |
| 174 |
|
| 175 |
dot = strchr(value, ':'); |
| 176 |
if (!dot) { |
| 177 |
|
| 178 |
return NULL; |
| 179 |
} |
| 180 |
|
| 181 |
count = atoi(value); |
| 182 |
index = atoi(dot + 1); |
| 183 |
dot = strchr(dot + 1, ':'); |
| 184 |
if (!dot) |
| 185 |
|
| 186 |
return NULL; |
| 187 |
dot++; |
| 188 |
|
| 189 |
rl = get_formulalist(count); |
| 190 |
rec = rl->items; |
| 191 |
while (rec) { |
| 192 |
if (rec->index == index && strcmp(rec->title, dot) == 0) |
| 193 |
return rec; |
| 194 |
|
| 195 |
rec = rec->next; |
| 196 |
} |
| 197 |
|
| 198 |
return NULL; |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
static void knowledge_alchemy_summary(const char *value, StringBuffer *buf) { |
| 208 |
const recipe *rec = knowledge_alchemy_get_recipe(value); |
| 209 |
const archetype *arch; |
| 210 |
|
| 211 |
if (!rec) |
| 212 |
|
| 213 |
return; |
| 214 |
|
| 215 |
arch = find_archetype(rec->arch_name[RANDOM()%rec->arch_names]); |
| 216 |
if (!arch) |
| 217 |
|
| 218 |
return; |
| 219 |
|
| 220 |
if (strcmp(rec->title, "NONE")) |
| 221 |
stringbuffer_append_printf(buf, "%s of %s", arch->clone.name, rec->title); |
| 222 |
else { |
| 223 |
if (arch->clone.title != NULL) { |
| 224 |
stringbuffer_append_printf(buf, "%s %s", arch->clone.name, arch->clone.title); |
| 225 |
} |
| 226 |
else |
| 227 |
stringbuffer_append_printf(buf, "%s", arch->clone.name); |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
static void knowledge_alchemy_detail(const char *value, StringBuffer *buf) { |
| 238 |
const recipe *rec = knowledge_alchemy_get_recipe(value); |
| 239 |
const linked_char *next; |
| 240 |
const archetype *arch; |
| 241 |
char name[MAX_BUF]; |
| 242 |
|
| 243 |
|
| 244 |
if (!rec) |
| 245 |
|
| 246 |
return; |
| 247 |
|
| 248 |
arch = find_archetype(rec->arch_name[RANDOM()%rec->arch_names]); |
| 249 |
if (!arch) |
| 250 |
|
| 251 |
return; |
| 252 |
|
| 253 |
if (strcmp(rec->title, "NONE")) |
| 254 |
stringbuffer_append_printf(buf, "The %s of %s", arch->clone.name, rec->title); |
| 255 |
else { |
| 256 |
if (arch->clone.title != NULL) { |
| 257 |
stringbuffer_append_printf(buf, "The %s %s", arch->clone.name, arch->clone.title); |
| 258 |
} |
| 259 |
else |
| 260 |
stringbuffer_append_printf(buf, "The %s", arch->clone.name); |
| 261 |
} |
| 262 |
|
| 263 |
arch = find_archetype(rec->cauldron); |
| 264 |
if (arch) |
| 265 |
query_name(&arch->clone, name, MAX_BUF); |
| 266 |
else |
| 267 |
snprintf(name, sizeof(name), "an unknown place"); |
| 268 |
|
| 269 |
stringbuffer_append_printf(buf, " is made at %s and uses the following ingredients:", name); |
| 270 |
|
| 271 |
for (next = rec->ingred; next != NULL; next = next->next) { |
| 272 |
stringbuffer_append_printf(buf, "\n - %s", next->name); |
| 273 |
} |
| 274 |
|
| 275 |
} |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
static int knowledge_alchemy_validate(const char *item) { |
| 283 |
return knowledge_alchemy_get_recipe(item) != NULL; |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
static StringBuffer* knowledge_alchemy_can_use_item(sstring code, const char *item, StringBuffer *buf, int index) { |
| 295 |
const recipe *rec = knowledge_alchemy_get_recipe(code); |
| 296 |
const linked_char *next; |
| 297 |
const archetype *arch; |
| 298 |
const char *name; |
| 299 |
|
| 300 |
if (!rec) |
| 301 |
|
| 302 |
return buf; |
| 303 |
|
| 304 |
arch = find_archetype(rec->arch_name[RANDOM()%rec->arch_names]); |
| 305 |
if (!arch) |
| 306 |
|
| 307 |
return buf; |
| 308 |
|
| 309 |
for (next = rec->ingred; next != NULL; next = next->next) { |
| 310 |
name = next->name; |
| 311 |
while ((*name) != '\0' && (isdigit(*name) || (*name) == ' ')) |
| 312 |
name++; |
| 313 |
|
| 314 |
if (strcmp(item, name) == 0) { |
| 315 |
if (buf == NULL) { |
| 316 |
buf = stringbuffer_new(); |
| 317 |
stringbuffer_append_string(buf, "It can be used in the following recipes: "); |
| 318 |
} else |
| 319 |
stringbuffer_append_string(buf, ", "); |
| 320 |
|
| 321 |
if (strcmp(rec->title, "NONE")) |
| 322 |
stringbuffer_append_printf(buf, "%s of %s", arch->clone.name, rec->title); |
| 323 |
else { |
| 324 |
if (arch->clone.title != NULL) { |
| 325 |
stringbuffer_append_printf(buf, "%s %s", arch->clone.name, arch->clone.title); |
| 326 |
} |
| 327 |
else |
| 328 |
stringbuffer_append_printf(buf, "%s", arch->clone.name); |
| 329 |
} |
| 330 |
stringbuffer_append_printf(buf, " (%d)", index); |
| 331 |
|
| 332 |
break; |
| 333 |
} |
| 334 |
} |
| 335 |
|
| 336 |
return buf; |
| 337 |
} |
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
static void knowledge_alchemy_attempt(player *pl, const knowledge_item *item) { |
| 345 |
const recipe *rp = knowledge_alchemy_get_recipe(item->item); |
| 346 |
object *cauldron = NULL, *inv; |
| 347 |
object *ingredients[50]; |
| 348 |
int index, x, y; |
| 349 |
uint32_t count, counts[50]; |
| 350 |
char name[MAX_BUF]; |
| 351 |
const char *ingname; |
| 352 |
linked_char *ing; |
| 353 |
tag_t cauldron_tag; |
| 354 |
mapstruct *map; |
| 355 |
|
| 356 |
if (!rp) { |
| 357 |
LOG(llevError, "knowledge: couldn't find recipe for %s", item->item); |
| 358 |
return; |
| 359 |
} |
| 360 |
|
| 361 |
if (rp->ingred_count > 50) { |
| 362 |
LOG(llevError, "knowledge: recipe %s has more than 50 ingredients!", item->item); |
| 363 |
draw_ext_info(NDI_UNIQUE, 0, pl->ob, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_INFO, "This recipe is too complicated."); |
| 364 |
return; |
| 365 |
} |
| 366 |
|
| 367 |
|
| 368 |
for (cauldron = pl->ob->below; cauldron; cauldron = cauldron->below) { |
| 369 |
if (strcmp(rp->cauldron, cauldron->arch->name) == 0) |
| 370 |
break; |
| 371 |
} |
| 372 |
|
| 373 |
if (cauldron == NULL) { |
| 374 |
draw_ext_info_format(NDI_UNIQUE, 0, pl->ob, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_INFO, "You are not on a %s", rp->cauldron); |
| 375 |
return; |
| 376 |
} |
| 377 |
|
| 378 |
cauldron_tag = cauldron->count; |
| 379 |
|
| 380 |
|
| 381 |
for (index = 0; index < 50; index++) { |
| 382 |
ingredients[index] = NULL; |
| 383 |
} |
| 384 |
for (inv = pl->ob->inv; inv != NULL; inv = inv->below) { |
| 385 |
|
| 386 |
if (QUERY_FLAG(inv, FLAG_INV_LOCKED) || QUERY_FLAG(inv, FLAG_STARTEQUIP)) |
| 387 |
continue; |
| 388 |
|
| 389 |
if (inv->title == NULL) |
| 390 |
safe_strncpy(name, inv->name, sizeof(name)); |
| 391 |
else |
| 392 |
snprintf(name, sizeof(name), "%s %s", inv->name, inv->title); |
| 393 |
|
| 394 |
index = 0; |
| 395 |
for (ing = rp->ingred; ing != NULL; ing = ing->next) { |
| 396 |
|
| 397 |
if (ingredients[index] != NULL) { |
| 398 |
index++; |
| 399 |
continue; |
| 400 |
} |
| 401 |
|
| 402 |
ingname = ing->name; |
| 403 |
count = 0; |
| 404 |
while (isdigit(*ingname)) { |
| 405 |
count = 10 * count + (*ingname - '0'); |
| 406 |
ingname++; |
| 407 |
} |
| 408 |
if (count == 0) |
| 409 |
count = 1; |
| 410 |
while (*ingname == ' ') |
| 411 |
ingname++; |
| 412 |
|
| 413 |
if (strcmp(name, ingname) == 0 && ((inv->nrof == 0 && count == 1) || (inv->nrof >= count))) { |
| 414 |
ingredients[index] = inv; |
| 415 |
counts[index] = count; |
| 416 |
break; |
| 417 |
} |
| 418 |
|
| 419 |
index++; |
| 420 |
} |
| 421 |
} |
| 422 |
|
| 423 |
index = 0; |
| 424 |
for (ing = rp->ingred; ing != NULL; ing = ing->next) { |
| 425 |
if (ingredients[index] == NULL) { |
| 426 |
draw_ext_info_format(NDI_UNIQUE, 0, pl->ob, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_INFO, "You don't have %s.", ing->name); |
| 427 |
return; |
| 428 |
} |
| 429 |
|
| 430 |
index++; |
| 431 |
} |
| 432 |
|
| 433 |
|
| 434 |
if (pl->ob->container != cauldron) { |
| 435 |
apply_by_living_below(pl->ob); |
| 436 |
if (pl->ob->container != cauldron) { |
| 437 |
draw_ext_info_format(NDI_UNIQUE, 0, pl->ob, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_INFO, "Couldn't activate the %s.", rp->cauldron); |
| 438 |
return; |
| 439 |
} |
| 440 |
} |
| 441 |
|
| 442 |
|
| 443 |
while (cauldron->inv != NULL) { |
| 444 |
inv = cauldron->inv; |
| 445 |
command_take(pl->ob, ""); |
| 446 |
if (cauldron->inv == inv) { |
| 447 |
draw_ext_info_format(NDI_UNIQUE, 0, pl->ob, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_INFO, "Couldn't empty the %s", rp->cauldron); |
| 448 |
return; |
| 449 |
} |
| 450 |
} |
| 451 |
|
| 452 |
|
| 453 |
for (index = 0; index < rp->ingred_count; index++) { |
| 454 |
tag_t tag = ingredients[index]->count; |
| 455 |
count = ingredients[index]->nrof; |
| 456 |
put_object_in_sack(pl->ob, cauldron, ingredients[index], counts[index]); |
| 457 |
if (object_was_destroyed(ingredients[index], tag)) { |
| 458 |
draw_ext_info(NDI_UNIQUE, 0, pl->ob, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_INFO, "Hum, some item disappeared, stopping the attempt."); |
| 459 |
return; |
| 460 |
|
| 461 |
} |
| 462 |
if (count == ingredients[index]->nrof && ingredients[index]->env == pl->ob) { |
| 463 |
draw_ext_info(NDI_UNIQUE, 0, pl->ob, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_INFO, "Hum, couldn't drop some items, stopping the attempt."); |
| 464 |
return; |
| 465 |
} |
| 466 |
} |
| 467 |
|
| 468 |
map = pl->ob->map; |
| 469 |
x = pl->ob->x; |
| 470 |
y = pl->ob->y; |
| 471 |
|
| 472 |
|
| 473 |
use_alchemy(pl->ob); |
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
pl->ob->speed_left -= 1.0 * (rp->ingred_count + 1); |
| 478 |
|
| 479 |
|
| 480 |
if (object_was_destroyed(cauldron, cauldron_tag) || map != pl->ob->map || x != pl->ob->x || y != pl->ob->y) { |
| 481 |
return; |
| 482 |
} |
| 483 |
|
| 484 |
|
| 485 |
while (cauldron->inv) { |
| 486 |
inv = cauldron->inv; |
| 487 |
examine(pl->ob, inv); |
| 488 |
command_take(pl->ob, ""); |
| 489 |
pl->ob->speed_left -= 1.0; |
| 490 |
if (inv == cauldron->inv) |
| 491 |
break; |
| 492 |
} |
| 493 |
} |
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
static const Face *knowledge_alchemy_face(sstring code) { |
| 501 |
const recipe *rp = knowledge_alchemy_get_recipe(code); |
| 502 |
const artifact *art; |
| 503 |
archetype *arch; |
| 504 |
object *item; |
| 505 |
const Face *face; |
| 506 |
|
| 507 |
if (!rp) { |
| 508 |
LOG(llevError, "knowledge: couldn't find recipe for %s", code); |
| 509 |
return NULL; |
| 510 |
} |
| 511 |
|
| 512 |
if (rp->arch_names == 0) |
| 513 |
return NULL; |
| 514 |
|
| 515 |
arch = find_archetype(rp->arch_name[0]); |
| 516 |
if (arch == NULL) { |
| 517 |
return NULL; |
| 518 |
} |
| 519 |
if (strcmp(rp->title, "NONE") == 0) { |
| 520 |
return arch->clone.face; |
| 521 |
} |
| 522 |
|
| 523 |
art = locate_recipe_artifact(rp, 0); |
| 524 |
if (art == NULL) |
| 525 |
return arch->clone.face; |
| 526 |
|
| 527 |
face = arch->clone.face; |
| 528 |
item = arch_to_object(arch); |
| 529 |
give_artifact_abilities(item, art->item); |
| 530 |
object_give_identified_properties(item); |
| 531 |
if (item->face != NULL && item->face != blank_face) |
| 532 |
face = item->face; |
| 533 |
object_free(item, FREE_OBJ_FREE_INVENTORY | FREE_OBJ_NO_DESTROY_CALLBACK); |
| 534 |
|
| 535 |
return face; |
| 536 |
} |
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
static int knowledge_known(const knowledge_player *current, const char *item, const knowledge_type *kt) { |
| 546 |
int i; |
| 547 |
for (i = 0; i < current->item_count; i++) { |
| 548 |
if (strcmp(kt->type, current->items[i]->handler->type) == 0 && strcmp(item, current->items[i]->item) == 0) { |
| 549 |
|
| 550 |
return 1; |
| 551 |
} |
| 552 |
} |
| 553 |
return 0; |
| 554 |
} |
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
static int knowledge_add(knowledge_player *current, const char *item, const knowledge_type *kt, player *pl) { |
| 565 |
knowledge_item *check; |
| 566 |
|
| 567 |
if (knowledge_known(current, item, kt)) { |
| 568 |
return 0; |
| 569 |
} |
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
check = malloc(sizeof(knowledge_item)); |
| 577 |
check->item = add_string(item); |
| 578 |
check->handler = kt; |
| 579 |
if (current->item_count >= current->item_allocated) { |
| 580 |
current->item_allocated += 10; |
| 581 |
current->items = realloc(current->items, current->item_allocated * sizeof(knowledge_item*)); |
| 582 |
} |
| 583 |
current->items[current->item_count] = check; |
| 584 |
current->item_count++; |
| 585 |
|
| 586 |
return 1; |
| 587 |
} |
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
static void knowledge_monster_summary(const char *item, StringBuffer *buf) { |
| 596 |
archetype *monster = find_archetype(item); |
| 597 |
if (!monster) |
| 598 |
return; |
| 599 |
|
| 600 |
stringbuffer_append_printf(buf, "%s", monster->clone.name); |
| 601 |
} |
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
static void knowledge_monster_detail(const char *item, StringBuffer *buf) { |
| 610 |
archetype *monster = find_archetype(item); |
| 611 |
|
| 612 |
if (!monster) |
| 613 |
return; |
| 614 |
|
| 615 |
stringbuffer_append_printf(buf, " *** %s ***\n", monster->clone.name); |
| 616 |
describe_item(&monster->clone, NULL, 1, buf); |
| 617 |
} |
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
static int knowledge_monster_validate(const char *item) { |
| 625 |
const archetype *monster = try_find_archetype(item); |
| 626 |
if (monster == NULL || monster->clone.type != 0) |
| 627 |
return 0; |
| 628 |
return 1; |
| 629 |
} |
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
static int knowledge_monster_add(struct knowledge_player *current, const char *item, const struct knowledge_type *type, player *pl) { |
| 640 |
char *dup = strdup_local(item); |
| 641 |
char *pos, *first = dup; |
| 642 |
int added = 0; |
| 643 |
|
| 644 |
while (first) { |
| 645 |
pos = strchr(first, ':'); |
| 646 |
if (pos) |
| 647 |
*pos = '\0'; |
| 648 |
added += knowledge_add(current, first, type, pl); |
| 649 |
first = pos ? pos + 1 : NULL; |
| 650 |
} |
| 651 |
|
| 652 |
free(dup); |
| 653 |
return added; |
| 654 |
} |
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
static const Face *knowledge_monster_face(sstring code) { |
| 662 |
const archetype *monster = find_archetype(code); |
| 663 |
|
| 664 |
if (!monster || monster->clone.face == blank_face || monster->clone.face == NULL) |
| 665 |
return NULL; |
| 666 |
|
| 667 |
return monster->clone.face; |
| 668 |
} |
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
static void knowledge_god_summary(const char *item, StringBuffer *buf) { |
| 677 |
char *dup = strdup_local(item), *pos = strchr(dup, ':'); |
| 678 |
|
| 679 |
if (pos) |
| 680 |
*pos = '\0'; |
| 681 |
|
| 682 |
stringbuffer_append_printf(buf, "%s [god]", dup); |
| 683 |
free(dup); |
| 684 |
} |
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
static void knowledge_god_detail(const char *item, StringBuffer *buf) { |
| 693 |
char *dup = strdup_local(item), *pos = strchr(dup, ':'); |
| 694 |
const archetype *god; |
| 695 |
int what; |
| 696 |
|
| 697 |
if (!pos) { |
| 698 |
LOG(llevError, "knowledge_god_detail: invalid god item %s\n", item); |
| 699 |
free(dup); |
| 700 |
return; |
| 701 |
} |
| 702 |
|
| 703 |
*pos = '\0'; |
| 704 |
what = atoi(pos + 1); |
| 705 |
god = find_archetype_by_object_name(dup); |
| 706 |
free(dup); |
| 707 |
|
| 708 |
if (!god) { |
| 709 |
LOG(llevError, "knowledge_god_detail: couldn't find god %s?\n", item); |
| 710 |
return; |
| 711 |
} |
| 712 |
|
| 713 |
describe_god(&god->clone, what, buf, 0); |
| 714 |
} |
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
static int knowledge_god_validate(const char *item) { |
| 722 |
char *dup = strdup_local(item), *pos = strchr(dup, ':'); |
| 723 |
int valid; |
| 724 |
|
| 725 |
if (!pos) { |
| 726 |
LOG(llevError, "knowledge_god_validate: invalid god item %s\n", item); |
| 727 |
free(dup); |
| 728 |
return 0; |
| 729 |
} |
| 730 |
*pos = '\0'; |
| 731 |
valid = find_archetype_by_object_name(dup) != NULL; |
| 732 |
free(dup); |
| 733 |
return valid; |
| 734 |
} |
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
static int knowledge_god_add(struct knowledge_player *current, const char *item, const struct knowledge_type *type, player *pl) { |
| 745 |
char *dup = strdup_local(item), *pos = strchr(dup, ':'); |
| 746 |
StringBuffer *buf; |
| 747 |
int what, i; |
| 748 |
knowledge_item* check; |
| 749 |
|
| 750 |
if (!pos) { |
| 751 |
LOG(llevError, "knowledge_god_add: invalid god item %s\n", item); |
| 752 |
free(dup); |
| 753 |
return 0; |
| 754 |
} |
| 755 |
|
| 756 |
*pos = '\0'; |
| 757 |
what = atoi(pos + 1); |
| 758 |
|
| 759 |
for (i = 0; i < current->item_count; i++) { |
| 760 |
check = current->items[i]; |
| 761 |
if (check->handler != type) |
| 762 |
|
| 763 |
continue; |
| 764 |
if (strncmp(check->item, dup, strlen(dup)) == 0) { |
| 765 |
|
| 766 |
int known, result; |
| 767 |
pos = strchr(check->item, ':'); |
| 768 |
known = atoi(pos + 1); |
| 769 |
result = known | what; |
| 770 |
buf = stringbuffer_new(); |
| 771 |
stringbuffer_append_printf(buf, "%s:%d", dup, result); |
| 772 |
free_string(check->item); |
| 773 |
check->item = stringbuffer_finish_shared(buf); |
| 774 |
free(dup); |
| 775 |
return (result != known); |
| 776 |
} |
| 777 |
} |
| 778 |
|
| 779 |
free(dup); |
| 780 |
|
| 781 |
|
| 782 |
return knowledge_add(current, item, type, pl); |
| 783 |
} |
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
static const Face *knowledge_god_face(sstring code) { |
| 791 |
char buf[MAX_BUF]; |
| 792 |
size_t letter; |
| 793 |
const archetype *altar_arch; |
| 794 |
|
| 795 |
snprintf(buf, MAX_BUF, "altar_"); |
| 796 |
letter = strlen(buf); |
| 797 |
strncpy(buf+letter, code, MAX_BUF-letter); |
| 798 |
for (; letter < strlen(buf); letter++) { |
| 799 |
if (buf[letter] == ':') { |
| 800 |
buf[letter] = '\0'; |
| 801 |
break; |
| 802 |
} |
| 803 |
buf[letter] = tolower(buf[letter]); |
| 804 |
} |
| 805 |
altar_arch = find_archetype(buf); |
| 806 |
if (altar_arch == NULL) |
| 807 |
return NULL; |
| 808 |
|
| 809 |
return altar_arch->clone.face; |
| 810 |
} |
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
static void knowledge_message_summary(const char *value, StringBuffer *buf) { |
| 819 |
const GeneralMessage *msg = get_message_from_identifier(value); |
| 820 |
|
| 821 |
if (!msg) |
| 822 |
|
| 823 |
return; |
| 824 |
|
| 825 |
stringbuffer_append_printf(buf, "%s", get_message_title(msg)); |
| 826 |
} |
| 827 |
|
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
static void knowledge_message_detail(const char *value, StringBuffer *buf) { |
| 835 |
const GeneralMessage *msg = get_message_from_identifier(value); |
| 836 |
|
| 837 |
if (!msg) |
| 838 |
|
| 839 |
return; |
| 840 |
|
| 841 |
stringbuffer_append_printf(buf, "%s", get_message_body(msg)); |
| 842 |
} |
| 843 |
|
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
static int knowledge_message_validate(const char *item) { |
| 850 |
return get_message_from_identifier(item) != NULL; |
| 851 |
} |
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
static const Face *knowledge_message_face(sstring code) { |
| 859 |
const GeneralMessage *msg = get_message_from_identifier(code); |
| 860 |
|
| 861 |
if (!msg) |
| 862 |
|
| 863 |
return NULL; |
| 864 |
|
| 865 |
return get_message_face(msg); |
| 866 |
} |
| 867 |
|
| 868 |
|
| 869 |
static const knowledge_type knowledges[] = { |
| 870 |
{ "alchemy", knowledge_alchemy_summary, knowledge_alchemy_detail, knowledge_alchemy_validate, knowledge_add, "recipes", knowledge_alchemy_can_use_item, knowledge_alchemy_attempt, "knowledge_recipes.111", knowledge_alchemy_face }, |
| 871 |
{ "monster", knowledge_monster_summary, knowledge_monster_detail, knowledge_monster_validate, knowledge_monster_add, "monsters", NULL, NULL, "knowledge_monsters.111", knowledge_monster_face }, |
| 872 |
{ "god", knowledge_god_summary, knowledge_god_detail, knowledge_god_validate, knowledge_god_add, "gods", NULL, NULL, "knowledge_gods.111", knowledge_god_face }, |
| 873 |
{ "message", knowledge_message_summary, knowledge_message_detail, knowledge_message_validate, knowledge_add, "messages", NULL, NULL, "knowledge_messages.111", knowledge_message_face }, |
| 874 |
{ NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL } |
| 875 |
}; |
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
|
| 881 |
|
| 882 |
|
| 883 |
static const knowledge_type *knowledge_find(const char *type) { |
| 884 |
int i = 0; |
| 885 |
while (knowledges[i].type != NULL) { |
| 886 |
if (strcmp(knowledges[i].type, type) == 0) |
| 887 |
return &knowledges[i]; |
| 888 |
i++; |
| 889 |
} |
| 890 |
|
| 891 |
return NULL; |
| 892 |
} |
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
static void knowledge_write_player_data(const knowledge_player *kp) { |
| 899 |
FILE *file; |
| 900 |
OutputFile of; |
| 901 |
char fname[MAX_BUF]; |
| 902 |
const knowledge_item *item; |
| 903 |
int i; |
| 904 |
|
| 905 |
snprintf(fname, sizeof(fname), "%s/%s/%s/%s.knowledge", settings.localdir, settings.playerdir, kp->player_name, kp->player_name); |
| 906 |
|
| 907 |
file = of_open(&of, fname); |
| 908 |
if (!file) { |
| 909 |
draw_ext_info(NDI_UNIQUE | NDI_ALL_DMS, 0, NULL, MSG_TYPE_ADMIN, MSG_TYPE_ADMIN_LOADSAVE, "File write error on server!"); |
| 910 |
return; |
| 911 |
} |
| 912 |
|
| 913 |
for (i = 0; i < kp->item_count; i++) { |
| 914 |
item = kp->items[i]; |
| 915 |
fprintf(file, "%s:%s\n", item->handler->type, item->item); |
| 916 |
} |
| 917 |
|
| 918 |
if (!of_close(&of)) { |
| 919 |
draw_ext_info(NDI_UNIQUE | NDI_ALL_DMS, 0, NULL, MSG_TYPE_ADMIN, MSG_TYPE_ADMIN_LOADSAVE, "File write error on server!"); |
| 920 |
return; |
| 921 |
} |
| 922 |
|
| 923 |
} |
| 924 |
|
| 925 |
|
| 926 |
|
| 927 |
|
| 928 |
|
| 929 |
static void knowledge_read_player_data(knowledge_player *kp) { |
| 930 |
FILE *file; |
| 931 |
char final[MAX_BUF], read[MAX_BUF], *dot; |
| 932 |
knowledge_item *item; |
| 933 |
const knowledge_type *type; |
| 934 |
|
| 935 |
snprintf(final, sizeof(final), "%s/%s/%s/%s.knowledge", settings.localdir, settings.playerdir, kp->player_name, kp->player_name); |
| 936 |
|
| 937 |
file = fopen(final, "r"); |
| 938 |
if (!file) { |
| 939 |
|
| 940 |
return; |
| 941 |
} |
| 942 |
|
| 943 |
while (fgets(read, sizeof(read), file) != NULL) { |
| 944 |
if (strlen(read) > 0) |
| 945 |
read[strlen(read) - 1] = '\0'; |
| 946 |
|
| 947 |
dot = strchr(read, ':'); |
| 948 |
if (!dot) { |
| 949 |
LOG(llevError, "knowledge: invalid line in file %s\n", final); |
| 950 |
continue; |
| 951 |
} |
| 952 |
|
| 953 |
*dot = '\0'; |
| 954 |
|
| 955 |
type = knowledge_find(read); |
| 956 |
if (!type) { |
| 957 |
LOG(llevError, "knowledge: invalid type %s in file %s\n", read, final); |
| 958 |
continue; |
| 959 |
} |
| 960 |
if (!type->validate(dot + 1)) { |
| 961 |
LOG(llevDebug, "knowledge: ignoring now invalid %s in file %s\n", read, final); |
| 962 |
continue; |
| 963 |
} |
| 964 |
|
| 965 |
item = malloc(sizeof(knowledge_item)); |
| 966 |
item->item = add_string(dot + 1); |
| 967 |
item->handler = type; |
| 968 |
if (kp->item_count == kp->item_allocated) { |
| 969 |
kp->item_allocated += 10; |
| 970 |
kp->items = realloc(kp->items, kp->item_allocated * sizeof(knowledge_item*)); |
| 971 |
} |
| 972 |
kp->items[kp->item_count] = item; |
| 973 |
kp->item_count++; |
| 974 |
} |
| 975 |
fclose(file); |
| 976 |
} |
| 977 |
|
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
|
| 983 |
|
| 984 |
static knowledge_player *knowledge_get_or_create(const player *pl) { |
| 985 |
knowledge_player *cur = knowledge_global; |
| 986 |
|
| 987 |
while (cur) { |
| 988 |
if (cur->player_name == pl->ob->name) |
| 989 |
return cur; |
| 990 |
cur = cur->next; |
| 991 |
} |
| 992 |
|
| 993 |
cur = calloc(1, sizeof(knowledge_player)); |
| 994 |
if (!cur) |
| 995 |
fatal(OUT_OF_MEMORY); |
| 996 |
cur->player_name = add_refcount(pl->ob->name); |
| 997 |
cur->next = knowledge_global; |
| 998 |
knowledge_global = cur; |
| 999 |
|
| 1000 |
knowledge_read_player_data(cur); |
| 1001 |
if (pl->socket.notifications < 2) |
| 1002 |
cur->sent_up_to = -1; |
| 1003 |
return cur; |
| 1004 |
} |
| 1005 |
|
| 1006 |
|
| 1007 |
|
| 1008 |
|
| 1009 |
|
| 1010 |
|
| 1011 |
|
| 1012 |
void knowledge_give(player *pl, const char *marker, const object *book) { |
| 1013 |
char *dot, *copy, *code; |
| 1014 |
const knowledge_type *type; |
| 1015 |
int none, added = 0; |
| 1016 |
knowledge_player *current = knowledge_get_or_create(pl); |
| 1017 |
|
| 1018 |
|
| 1019 |
dot = strchr(marker, ':'); |
| 1020 |
if (dot == NULL) |
| 1021 |
return; |
| 1022 |
|
| 1023 |
copy = strdup(marker); |
| 1024 |
if (!copy) |
| 1025 |
fatal(OUT_OF_MEMORY); |
| 1026 |
|
| 1027 |
dot = strchr(copy, ':'); |
| 1028 |
*dot = '\0'; |
| 1029 |
dot++; |
| 1030 |
|
| 1031 |
type = knowledge_find(copy); |
| 1032 |
if (!type) { |
| 1033 |
LOG(llevError, "knowledge: invalid marker type %s in %s\n", copy, book == NULL ? "(null)" : book->name); |
| 1034 |
free(copy); |
| 1035 |
return; |
| 1036 |
} |
| 1037 |
|
| 1038 |
none = (current->items == NULL); |
| 1039 |
|
| 1040 |
code = dot; |
| 1041 |
while (code && code[0] != '\0') { |
| 1042 |
dot = strchr(code, '/'); |
| 1043 |
if (dot) { |
| 1044 |
*dot = '\0'; |
| 1045 |
dot++; |
| 1046 |
} |
| 1047 |
if (type->validate(code)) { |
| 1048 |
added += type->add(current, code, type, pl); |
| 1049 |
} |
| 1050 |
code = dot; |
| 1051 |
} |
| 1052 |
|
| 1053 |
free(copy); |
| 1054 |
|
| 1055 |
if (added) { |
| 1056 |
draw_ext_info(NDI_UNIQUE, 0, pl->ob, MSG_TYPE_MISC, MSG_TYPE_CLIENT_NOTICE, "You write that down for future reference."); |
| 1057 |
if (none) { |
| 1058 |
|
| 1059 |
draw_ext_info(NDI_UNIQUE, 0, pl->ob, MSG_TYPE_MISC, MSG_TYPE_CLIENT_NOTICE, "Use the 'knowledge' command to browse what you write down (this message will not appear anymore)."); |
| 1060 |
} |
| 1061 |
} |
| 1062 |
|
| 1063 |
if (pl->has_directory) |
| 1064 |
knowledge_write_player_data(current); |
| 1065 |
} |
| 1066 |
|
| 1067 |
|
| 1068 |
|
| 1069 |
|
| 1070 |
|
| 1071 |
|
| 1072 |
void knowledge_read(player *pl, object *book) { |
| 1073 |
sstring marker = object_get_value(book, "knowledge_marker"); |
| 1074 |
if (marker != NULL) |
| 1075 |
knowledge_give(pl, marker, book); |
| 1076 |
} |
| 1077 |
|
| 1078 |
|
| 1079 |
|
| 1080 |
|
| 1081 |
|
| 1082 |
|
| 1083 |
|
| 1084 |
static void knowledge_do_display(object *pl, const knowledge_type *show_only, const char *search) { |
| 1085 |
knowledge_player *kp; |
| 1086 |
knowledge_item *item; |
| 1087 |
int header = 0, show, i; |
| 1088 |
StringBuffer *summary; |
| 1089 |
char *final; |
| 1090 |
|
| 1091 |
assert(search == NULL || search[0] != '\0'); |
| 1092 |
|
| 1093 |
kp = knowledge_get_or_create(pl->contr); |
| 1094 |
for (i = 0; i < kp->item_count; i++) { |
| 1095 |
item = kp->items[i]; |
| 1096 |
show = 1; |
| 1097 |
|
| 1098 |
summary = stringbuffer_new(); |
| 1099 |
item->handler->summary(item->item, summary); |
| 1100 |
final = stringbuffer_finish(summary); |
| 1101 |
|
| 1102 |
if (show_only != NULL && item->handler != show_only) { |
| 1103 |
show = 0; |
| 1104 |
} |
| 1105 |
if (search != NULL && search[0] != '\0') { |
| 1106 |
if (strstr(final, search) == NULL) { |
| 1107 |
char *fd; |
| 1108 |
StringBuffer *detail = stringbuffer_new(); |
| 1109 |
item->handler->detail(item->item, detail); |
| 1110 |
fd = stringbuffer_finish(detail); |
| 1111 |
if (strstr(fd, search) == NULL) |
| 1112 |
show = 0; |
| 1113 |
free(fd); |
| 1114 |
} |
| 1115 |
} |
| 1116 |
|
| 1117 |
if (show == 1) { |
| 1118 |
if (header == 0) { |
| 1119 |
if (search != NULL) |
| 1120 |
draw_ext_info_format(NDI_UNIQUE, 0, pl, MSG_TYPE_MISC, MSG_TYPE_CLIENT_NOTICE, "You have knowledge of the following %s concerning '%s':", show_only == NULL ? "things" : show_only->type, search); |
| 1121 |
else if (show_only != NULL) |
| 1122 |
draw_ext_info_format(NDI_UNIQUE, 0, pl, MSG_TYPE_MISC, MSG_TYPE_CLIENT_NOTICE, "You have knowledge of those %s:", show_only->name); |
| 1123 |
else |
| 1124 |
draw_ext_info(NDI_UNIQUE, 0, pl, MSG_TYPE_MISC, MSG_TYPE_CLIENT_NOTICE, "You have knowledge of:"); |
| 1125 |
header = 1; |
| 1126 |
} |
| 1127 |
|
| 1128 |
draw_ext_info_format(NDI_UNIQUE, 0, pl, MSG_TYPE_MISC, MSG_TYPE_CLIENT_NOTICE, "(%3d) %s", i + 1, final); |
| 1129 |
} |
| 1130 |
|
| 1131 |
free(final); |
| 1132 |
} |
| 1133 |
|
| 1134 |
if (header == 0) { |
| 1135 |
if (search != NULL) |
| 1136 |
draw_ext_info_format(NDI_UNIQUE, 0, pl, MSG_TYPE_MISC, MSG_TYPE_CLIENT_NOTICE, "You don't know yet any relevant information about '%s'.", search); |
| 1137 |
else if (show_only != NULL) |
| 1138 |
draw_ext_info_format(NDI_UNIQUE, 0, pl, MSG_TYPE_MISC, MSG_TYPE_CLIENT_NOTICE, "You don't know yet any relevant information about %s.", show_only->name); |
| 1139 |
else |
| 1140 |
draw_ext_info(NDI_UNIQUE, 0, pl, MSG_TYPE_MISC, MSG_TYPE_CLIENT_NOTICE, "You don't know yet any relevant information."); |
| 1141 |
} |
| 1142 |
} |
| 1143 |
|
| 1144 |
|
| 1145 |
|
| 1146 |
|
| 1147 |
|
| 1148 |
|
| 1149 |
static void knowledge_display(object *pl, const char *params) { |
| 1150 |
const knowledge_type *show_only = NULL; |
| 1151 |
|
| 1152 |
if (params && params[0] == ' ') { |
| 1153 |
const char *type = params + 1; |
| 1154 |
int idx = 0; |
| 1155 |
for (; knowledges[idx].type != NULL; idx++) { |
| 1156 |
if (strcmp(knowledges[idx].name, type) == 0) { |
| 1157 |
show_only = &knowledges[idx]; |
| 1158 |
break; |
| 1159 |
} |
| 1160 |
} |
| 1161 |
|
| 1162 |
if (show_only == NULL) { |
| 1163 |
draw_ext_info(NDI_UNIQUE, 0, pl, MSG_TYPE_MISC, MSG_TYPE_CLIENT_NOTICE, "Invalid type, valid types are:"); |
| 1164 |
for (idx = 0; knowledges[idx].type != NULL; idx++) { |
| 1165 |
draw_ext_info_format(NDI_UNIQUE, 0, pl, MSG_TYPE_MISC, MSG_TYPE_CLIENT_NOTICE, "- %s", knowledges[idx].name); |
| 1166 |
} |
| 1167 |
return; |
| 1168 |
} |
| 1169 |
} |
| 1170 |
|
| 1171 |
knowledge_do_display(pl, show_only, NULL); |
| 1172 |
} |
| 1173 |
|
| 1174 |
|
| 1175 |
|
| 1176 |
|
| 1177 |
|
| 1178 |
|
| 1179 |
static void knowledge_show(object *pl, const char *params) { |
| 1180 |
knowledge_player *kp; |
| 1181 |
knowledge_item *item; |
| 1182 |
int count = atoi(params) - 1; |
| 1183 |
StringBuffer *buf; |
| 1184 |
char *final; |
| 1185 |
|
| 1186 |
kp = knowledge_get_or_create(pl->contr); |
| 1187 |
if (count < 0 || count >= kp->item_count) { |
| 1188 |
draw_ext_info(NDI_UNIQUE, 0, pl, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_INFO, "Invalid knowledge number"); |
| 1189 |
return; |
| 1190 |
} |
| 1191 |
|
| 1192 |
item = kp->items[count]; |
| 1193 |
buf = stringbuffer_new(); |
| 1194 |
item->handler->detail(item->item, buf); |
| 1195 |
final = stringbuffer_finish(buf); |
| 1196 |
draw_ext_info(NDI_UNIQUE, 0, pl, MSG_TYPE_MISC, MSG_TYPE_CLIENT_NOTICE, final); |
| 1197 |
free(final); |
| 1198 |
} |
| 1199 |
|
| 1200 |
|
| 1201 |
|
| 1202 |
|
| 1203 |
|
| 1204 |
|
| 1205 |
static void knowledge_do_attempt(object *pl, const char *params) { |
| 1206 |
knowledge_player *kp; |
| 1207 |
knowledge_item *item; |
| 1208 |
int count = atoi(params) - 1; |
| 1209 |
|
| 1210 |
kp = knowledge_get_or_create(pl->contr); |
| 1211 |
if (count < 0 || count >= kp->item_count) { |
| 1212 |
draw_ext_info(NDI_UNIQUE, 0, pl, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_INFO, "Invalid knowledge number"); |
| 1213 |
return; |
| 1214 |
} |
| 1215 |
|
| 1216 |
item = kp->items[count]; |
| 1217 |
if (item->handler->attempt_alchemy == NULL) { |
| 1218 |
draw_ext_info(NDI_UNIQUE, 0, pl, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_INFO, "You can't do anything with that knowledge."); |
| 1219 |
} else { |
| 1220 |
item->handler->attempt_alchemy(pl->contr, item); |
| 1221 |
} |
| 1222 |
} |
| 1223 |
|
| 1224 |
|
| 1225 |
|
| 1226 |
|
| 1227 |
|
| 1228 |
|
| 1229 |
void command_knowledge(object *pl, const char *params) { |
| 1230 |
|
| 1231 |
if (!pl->contr) { |
| 1232 |
LOG(llevError, "command_knowledge: called for %s not a player!\n", pl->name); |
| 1233 |
return; |
| 1234 |
} |
| 1235 |
|
| 1236 |
if (!params || *params == '\0') { |
| 1237 |
command_help(pl, "knowledge"); |
| 1238 |
return; |
| 1239 |
} |
| 1240 |
|
| 1241 |
if (strncmp(params, "list", 4) == 0) { |
| 1242 |
knowledge_display(pl, params + 4); |
| 1243 |
return; |
| 1244 |
} |
| 1245 |
|
| 1246 |
if (strncmp(params, "search ", 7) == 0) { |
| 1247 |
knowledge_do_display(pl, NULL, params + 7); |
| 1248 |
return; |
| 1249 |
} |
| 1250 |
|
| 1251 |
if (strncmp(params, "show ", 5) == 0) { |
| 1252 |
knowledge_show(pl, params + 5); |
| 1253 |
return; |
| 1254 |
} |
| 1255 |
|
| 1256 |
if (strncmp(params, "attempt ", 8) == 0) { |
| 1257 |
knowledge_do_attempt(pl, params + 8); |
| 1258 |
return; |
| 1259 |
} |
| 1260 |
|
| 1261 |
command_help(pl, "knowledge"); |
| 1262 |
} |
| 1263 |
|
| 1264 |
|
| 1265 |
|
| 1266 |
|
| 1267 |
|
| 1268 |
static void free_knowledge_items(knowledge_player *kp) { |
| 1269 |
knowledge_item *item; |
| 1270 |
int i; |
| 1271 |
|
| 1272 |
for (i = 0; i < kp->item_count; i++) { |
| 1273 |
item = kp->items[i]; |
| 1274 |
free_string(item->item); |
| 1275 |
free(item); |
| 1276 |
} |
| 1277 |
free(kp->items); |
| 1278 |
kp->items = NULL; |
| 1279 |
kp->item_count = 0; |
| 1280 |
kp->item_allocated = 0; |
| 1281 |
} |
| 1282 |
|
| 1283 |
|
| 1284 |
|
| 1285 |
|
| 1286 |
|
| 1287 |
static void free_knowledge_player(knowledge_player *kp) { |
| 1288 |
free_knowledge_items(kp); |
| 1289 |
free_string(kp->player_name); |
| 1290 |
free(kp); |
| 1291 |
} |
| 1292 |
|
| 1293 |
|
| 1294 |
|
| 1295 |
|
| 1296 |
void free_knowledge(void) { |
| 1297 |
knowledge_player *kp, *next; |
| 1298 |
|
| 1299 |
kp = knowledge_global; |
| 1300 |
while (kp) { |
| 1301 |
next = kp->next; |
| 1302 |
free_knowledge_player(kp); |
| 1303 |
kp = next; |
| 1304 |
} |
| 1305 |
knowledge_global = NULL; |
| 1306 |
} |
| 1307 |
|
| 1308 |
|
| 1309 |
|
| 1310 |
|
| 1311 |
|
| 1312 |
|
| 1313 |
|
| 1314 |
int knowledge_player_knows(const player *pl, const char *knowledge) { |
| 1315 |
const knowledge_type *type; |
| 1316 |
char copy[MAX_BUF], *pos; |
| 1317 |
const knowledge_player *current; |
| 1318 |
|
| 1319 |
if (strlen(knowledge) >= MAX_BUF - 1) { |
| 1320 |
LOG(llevError, "knowledge_player_knows: too long knowledge %s\n", knowledge); |
| 1321 |
return 0; |
| 1322 |
} |
| 1323 |
|
| 1324 |
strlcpy(copy, knowledge, sizeof(copy)); |
| 1325 |
pos = strchr(copy, ':'); |
| 1326 |
if (pos == NULL) { |
| 1327 |
LOG(llevError, "knowledge_player_knows: invalid knowledge item %s\n", knowledge); |
| 1328 |
return 0; |
| 1329 |
} |
| 1330 |
|
| 1331 |
*pos = '\0'; |
| 1332 |
pos++; |
| 1333 |
|
| 1334 |
type = knowledge_find(copy); |
| 1335 |
if (type == NULL) { |
| 1336 |
LOG(llevError, "knowledge_player_knows: invalid knowledge type %s\n", knowledge); |
| 1337 |
return 0; |
| 1338 |
} |
| 1339 |
|
| 1340 |
current = knowledge_get_or_create(pl); |
| 1341 |
|
| 1342 |
return knowledge_known(current, pos, type); |
| 1343 |
} |
| 1344 |
|
| 1345 |
|
| 1346 |
|
| 1347 |
|
| 1348 |
|
| 1349 |
|
| 1350 |
void knowledge_item_can_be_used_alchemy(object *op, const object *item) { |
| 1351 |
knowledge_player *cur; |
| 1352 |
knowledge_item *ki; |
| 1353 |
char item_name[MAX_BUF], *result; |
| 1354 |
const char *name; |
| 1355 |
StringBuffer *buf = NULL; |
| 1356 |
int i; |
| 1357 |
|
| 1358 |
if (op->type != PLAYER || op->contr == NULL) |
| 1359 |
return; |
| 1360 |
|
| 1361 |
cur = knowledge_get_or_create(op->contr); |
| 1362 |
|
| 1363 |
if (item->title != NULL) { |
| 1364 |
snprintf(item_name, sizeof(item_name), "%s %s", item->name, item->title); |
| 1365 |
name = item_name; |
| 1366 |
} else |
| 1367 |
name = item->name; |
| 1368 |
|
| 1369 |
for (i = 0; i < cur->item_count; i++) { |
| 1370 |
ki = cur->items[i]; |
| 1371 |
if (ki->handler->use_alchemy != NULL) { |
| 1372 |
buf = ki->handler->use_alchemy(ki->item, name, buf, i + 1); |
| 1373 |
} |
| 1374 |
} |
| 1375 |
|
| 1376 |
if (buf == NULL) |
| 1377 |
return; |
| 1378 |
|
| 1379 |
stringbuffer_append_string(buf, "."); |
| 1380 |
result = stringbuffer_finish(buf); |
| 1381 |
draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_EXAMINE, |
| 1382 |
result); |
| 1383 |
free(result); |
| 1384 |
} |
| 1385 |
|
| 1386 |
|
| 1387 |
|
| 1388 |
|
| 1389 |
|
| 1390 |
void knowledge_send_info(socket_struct *ns) { |
| 1391 |
int i; |
| 1392 |
const Face *face; |
| 1393 |
SockList sl; |
| 1394 |
|
| 1395 |
face = find_face("knowledge_generic.111", NULL); |
| 1396 |
if (face != NULL && (!(ns->faces_sent[face->number] & NS_FACESENT_FACE))) |
| 1397 |
esrv_send_face(ns, face, 0); |
| 1398 |
|
| 1399 |
SockList_Init(&sl); |
| 1400 |
SockList_AddString(&sl, "replyinfo knowledge_info\n"); |
| 1401 |
SockList_AddPrintf(&sl, "::%u:0\n", face ? face->number : 0); |
| 1402 |
|
| 1403 |
for (i = 0; knowledges[i].type != NULL; i++) { |
| 1404 |
face = find_face(knowledges[i].face, NULL); |
| 1405 |
if (face != NULL && (!(ns->faces_sent[face->number] & NS_FACESENT_FACE))) |
| 1406 |
esrv_send_face(ns, face, 0); |
| 1407 |
|
| 1408 |
SockList_AddPrintf(&sl, "%s:%s:%u:%s\n", knowledges[i].type, knowledges[i].name, face ? face->number : 0, knowledges[i].attempt_alchemy != NULL ? "1" : "0"); |
| 1409 |
} |
| 1410 |
|
| 1411 |
Send_With_Handling(ns, &sl); |
| 1412 |
SockList_Term(&sl); |
| 1413 |
} |
| 1414 |
|
| 1415 |
|
| 1416 |
|
| 1417 |
|
| 1418 |
|
| 1419 |
void knowledge_send_known(player *pl) { |
| 1420 |
knowledge_player *kp; |
| 1421 |
|
| 1422 |
if (pl->socket.notifications < 2) |
| 1423 |
return; |
| 1424 |
|
| 1425 |
|
| 1426 |
|
| 1427 |
|
| 1428 |
kp = knowledge_get_or_create(pl); |
| 1429 |
kp->sent_up_to = 0; |
| 1430 |
} |
| 1431 |
|
| 1432 |
|
| 1433 |
|
| 1434 |
|
| 1435 |
|
| 1436 |
|
| 1437 |
|
| 1438 |
|
| 1439 |
void knowledge_first_player_save(player *pl) { |
| 1440 |
knowledge_player *cur = knowledge_global; |
| 1441 |
|
| 1442 |
while (cur) { |
| 1443 |
if (cur->player_name == pl->ob->name) { |
| 1444 |
knowledge_write_player_data(cur); |
| 1445 |
} |
| 1446 |
cur = cur->next; |
| 1447 |
} |
| 1448 |
|
| 1449 |
} |
| 1450 |
|
| 1451 |
|
| 1452 |
|
| 1453 |
|
| 1454 |
|
| 1455 |
void knowledge_process_incremental(void) { |
| 1456 |
int i, last; |
| 1457 |
SockList sl; |
| 1458 |
size_t size; |
| 1459 |
const knowledge_item *item; |
| 1460 |
StringBuffer *buf; |
| 1461 |
char *title; |
| 1462 |
const Face *face; |
| 1463 |
knowledge_player *cur = knowledge_global, *prev = NULL; |
| 1464 |
player *pl; |
| 1465 |
|
| 1466 |
while (cur) { |
| 1467 |
|
| 1468 |
for (pl = first_player; pl != NULL; pl = pl->next) { |
| 1469 |
if (pl->ob->name == cur->player_name) { |
| 1470 |
break; |
| 1471 |
} |
| 1472 |
} |
| 1473 |
|
| 1474 |
|
| 1475 |
if (pl == NULL) { |
| 1476 |
if (prev == NULL) { |
| 1477 |
knowledge_global = cur->next; |
| 1478 |
} else { |
| 1479 |
prev->next = cur->next; |
| 1480 |
} |
| 1481 |
|
| 1482 |
free_knowledge_player(cur); |
| 1483 |
|
| 1484 |
|
| 1485 |
return; |
| 1486 |
} |
| 1487 |
|
| 1488 |
if (cur->sent_up_to == -1 || cur->sent_up_to == cur->item_count) { |
| 1489 |
prev = cur; |
| 1490 |
cur = cur->next; |
| 1491 |
continue; |
| 1492 |
} |
| 1493 |
|
| 1494 |
last = MIN(cur->sent_up_to + 50, cur->item_count); |
| 1495 |
SockList_Init(&sl); |
| 1496 |
SockList_AddString(&sl, "addknowledge "); |
| 1497 |
for (i = cur->sent_up_to; i < last; i++) { |
| 1498 |
item = cur->items[i]; |
| 1499 |
|
| 1500 |
buf = stringbuffer_new(); |
| 1501 |
item->handler->summary(item->item, buf); |
| 1502 |
title = stringbuffer_finish(buf); |
| 1503 |
|
| 1504 |
face = NULL; |
| 1505 |
if (item->handler->item_face != NULL) |
| 1506 |
face = item->handler->item_face(item->item); |
| 1507 |
|
| 1508 |
if (face == NULL) |
| 1509 |
face = find_face(item->handler->face, NULL); |
| 1510 |
|
| 1511 |
size = 4 + (2 + strlen(item->handler->type)) + (2 + strlen(title)) + 4; |
| 1512 |
|
| 1513 |
if (SockList_Avail(&sl) < size) { |
| 1514 |
Send_With_Handling(&pl->socket, &sl); |
| 1515 |
SockList_Reset(&sl); |
| 1516 |
SockList_AddString(&sl, "addknowledge "); |
| 1517 |
} |
| 1518 |
|
| 1519 |
SockList_AddInt(&sl, i + 1); |
| 1520 |
SockList_AddLen16Data(&sl, item->handler->type, strlen(item->handler->type)); |
| 1521 |
if (face != NULL && !(pl->socket.faces_sent[face->number]&NS_FACESENT_FACE)) |
| 1522 |
esrv_send_face(&pl->socket, face, 0); |
| 1523 |
SockList_AddLen16Data(&sl, title, strlen(title)); |
| 1524 |
SockList_AddInt(&sl, face ? face->number : 0); |
| 1525 |
|
| 1526 |
free(title); |
| 1527 |
} |
| 1528 |
|
| 1529 |
cur->sent_up_to = last; |
| 1530 |
|
| 1531 |
Send_With_Handling(&pl->socket, &sl); |
| 1532 |
SockList_Term(&sl); |
| 1533 |
|
| 1534 |
|
| 1535 |
break; |
| 1536 |
} |
| 1537 |
} |
| 1538 |
|
| 1539 |
|
| 1540 |
|
| 1541 |
|
| 1542 |
|
| 1543 |
|
| 1544 |
void knowledge_show_monster_detail(object *op, const char *name) { |
| 1545 |
knowledge_player *kp; |
| 1546 |
StringBuffer *buf = NULL; |
| 1547 |
char *result; |
| 1548 |
|
| 1549 |
if (op->contr == NULL) |
| 1550 |
return; |
| 1551 |
|
| 1552 |
kp = knowledge_get_or_create(op->contr); |
| 1553 |
|
| 1554 |
if (!knowledge_known(kp, name, &knowledges[1])) |
| 1555 |
return; |
| 1556 |
|
| 1557 |
buf = stringbuffer_new(); |
| 1558 |
knowledge_monster_detail(name, buf); |
| 1559 |
result = stringbuffer_finish(buf); |
| 1560 |
draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_EXAMINE, result); |
| 1561 |
free(result); |
| 1562 |
} |