| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
#include "global.h" |
| 22 |
|
| 23 |
#include <ctype.h> |
| 24 |
#include <stdlib.h> |
| 25 |
#include <string.h> |
| 26 |
|
| 27 |
#ifndef WIN32 |
| 28 |
#include <unistd.h> |
| 29 |
#endif |
| 30 |
|
| 31 |
static void parse_regions(FILE *fp); |
| 32 |
static void assign_region_parents(void); |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
region *get_region_by_name(const char *region_name) { |
| 47 |
region *reg; |
| 48 |
|
| 49 |
for (reg = first_region; reg != NULL; reg = reg->next) |
| 50 |
if (!strcmp(reg->name, region_name)) |
| 51 |
return reg; |
| 52 |
|
| 53 |
for (reg = first_region; reg != NULL; reg = reg->next) { |
| 54 |
if (reg->fallback) { |
| 55 |
LOG(llevDebug, "region called %s requested, but not found, fallback used.\n", region_name); |
| 56 |
return reg; |
| 57 |
} |
| 58 |
} |
| 59 |
LOG(llevInfo, "Got no region or fallback for region %s.\n", region_name); |
| 60 |
return NULL; |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
region *get_region_by_map(mapstruct *m) { |
| 75 |
return get_region_by_name(get_name_of_region_for_map(m)); |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
const char *get_name_of_region_for_map(const mapstruct *m) { |
| 93 |
region *reg; |
| 94 |
|
| 95 |
if (m->region != NULL) |
| 96 |
return m->region->name; |
| 97 |
for (reg = first_region; reg != NULL; reg = reg->next) { |
| 98 |
if (reg->fallback) |
| 99 |
return reg->name; |
| 100 |
} |
| 101 |
LOG(llevInfo, "map %s had no region and I couldn't find a fallback to use.\n", m->name); |
| 102 |
return "unknown"; |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
region *get_region_from_string(const char *name) { |
| 122 |
region *reg; |
| 123 |
char *substr; |
| 124 |
char *p; |
| 125 |
|
| 126 |
if (first_region == NULL) { |
| 127 |
return NULL; |
| 128 |
} |
| 129 |
|
| 130 |
if (*name == '\0') { |
| 131 |
for (reg = first_region; reg->parent != NULL; reg = reg->parent) |
| 132 |
; |
| 133 |
return reg; |
| 134 |
} |
| 135 |
p = strchr(name, '\n'); |
| 136 |
if (p) |
| 137 |
*p = '\0'; |
| 138 |
for (reg = first_region; reg != NULL; reg = reg->next) |
| 139 |
if (!strcasecmp(reg->name, name)) |
| 140 |
return reg; |
| 141 |
|
| 142 |
for (reg = first_region; reg != NULL; reg = reg->next) |
| 143 |
if (reg->longname != NULL) { |
| 144 |
if (!strcasecmp(reg->longname, name)) |
| 145 |
return reg; |
| 146 |
} |
| 147 |
|
| 148 |
substr = NULL; |
| 149 |
for (reg = first_region; reg != NULL; reg = reg->next) |
| 150 |
if (reg->longname != NULL) { |
| 151 |
substr = strstr(reg->longname, name); |
| 152 |
if (substr != NULL) |
| 153 |
return reg; |
| 154 |
} |
| 155 |
for (reg = first_region; reg != NULL; reg = reg->next) |
| 156 |
if (reg->longname != NULL) { |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
substr = strstr(reg->name, name); |
| 164 |
if (substr != NULL) |
| 165 |
return reg; |
| 166 |
} |
| 167 |
for (reg = first_region; reg != NULL; reg = reg->next) { |
| 168 |
substr = strstr(reg->name, name); |
| 169 |
if (substr != NULL) |
| 170 |
return reg; |
| 171 |
} |
| 172 |
|
| 173 |
for (reg = first_region; reg->parent != NULL; reg = reg->parent) |
| 174 |
; |
| 175 |
return reg; |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
int region_is_child_of_region(const region *child, const region *r) { |
| 191 |
if (r == NULL) |
| 192 |
return -1; |
| 193 |
if (child == NULL) |
| 194 |
return 0; |
| 195 |
if (!strcmp(child->name, r->name)) |
| 196 |
return 1; |
| 197 |
else if (child->parent != NULL) |
| 198 |
return region_is_child_of_region(child->parent, r); |
| 199 |
else |
| 200 |
return 0; |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
const char *get_region_longname(const region *r) { |
| 218 |
if (r->longname != NULL) |
| 219 |
return r->longname; |
| 220 |
else if (r->parent != NULL) |
| 221 |
return get_region_longname(r->parent); |
| 222 |
else { |
| 223 |
LOG(llevDebug, "NOTICE region %s has no parent and no longname.\n", r->name); |
| 224 |
return "no name can be found for the current region"; |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
const char *get_region_msg(const region *r) { |
| 239 |
if (r->msg != NULL) |
| 240 |
return r->msg; |
| 241 |
else if (r->parent != NULL) |
| 242 |
return get_region_msg(r->parent); |
| 243 |
else { |
| 244 |
LOG(llevDebug, "NOTICE region %s has no parent and no msg.\n", r->name); |
| 245 |
return "no description can be found for the current region"; |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
object *get_jail_exit(object *op) { |
| 261 |
region *reg; |
| 262 |
object *exit; |
| 263 |
|
| 264 |
if (op->type != PLAYER) { |
| 265 |
LOG(llevError, "region.c: get_jail_exit called against non-player object.\n"); |
| 266 |
return NULL; |
| 267 |
} |
| 268 |
|
| 269 |
reg = get_region_by_map(op->map); |
| 270 |
while (reg != NULL) { |
| 271 |
if (reg->jailmap) { |
| 272 |
exit = object_new(); |
| 273 |
EXIT_PATH(exit) = add_string(reg->jailmap); |
| 274 |
|
| 275 |
SET_FLAG(exit, FLAG_DAMNED); |
| 276 |
EXIT_X(exit) = reg->jailx; |
| 277 |
EXIT_Y(exit) = reg->jaily; |
| 278 |
return exit; |
| 279 |
} else |
| 280 |
reg = reg->parent; |
| 281 |
} |
| 282 |
LOG(llevDebug, "No suitable jailmap for region %s was found.\n", reg->name); |
| 283 |
return NULL; |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
int init_regions(void) { |
| 293 |
FILE *fp; |
| 294 |
char filename[MAX_BUF]; |
| 295 |
|
| 296 |
if (first_region != NULL) |
| 297 |
return 0; |
| 298 |
|
| 299 |
snprintf(filename, sizeof(filename), "%s/%s/%s", settings.datadir, settings.mapdir, settings.regions); |
| 300 |
if ((fp = fopen(filename, "r")) == NULL) { |
| 301 |
LOG(llevError, "Couldn't read regions file from \"%s\".\n", filename); |
| 302 |
return 1; |
| 303 |
} |
| 304 |
parse_regions(fp); |
| 305 |
assign_region_parents(); |
| 306 |
|
| 307 |
fclose(fp); |
| 308 |
return 0; |
| 309 |
} |
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
region *get_region_struct(void) { |
| 326 |
region *new = (region *)calloc(1, sizeof(region)); |
| 327 |
if (new == NULL) |
| 328 |
fatal(OUT_OF_MEMORY); |
| 329 |
|
| 330 |
return new; |
| 331 |
} |
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
static void parse_regions(FILE *fp) { |
| 341 |
region *new; |
| 342 |
region *reg; |
| 343 |
|
| 344 |
char buf[HUGE_BUF], msgbuf[HUGE_BUF], *key = NULL, *value, *end; |
| 345 |
int msgpos = 0; |
| 346 |
|
| 347 |
new = NULL; |
| 348 |
while (fgets(buf, HUGE_BUF-1, fp) != NULL) { |
| 349 |
buf[HUGE_BUF-1] = 0; |
| 350 |
key = buf; |
| 351 |
while (isspace(*key)) |
| 352 |
key++; |
| 353 |
if (*key == 0) |
| 354 |
continue; |
| 355 |
value = strchr(key, ' '); |
| 356 |
if (!value) { |
| 357 |
end = strchr(key, '\n'); |
| 358 |
*end = 0; |
| 359 |
} else { |
| 360 |
*value = 0; |
| 361 |
value++; |
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
while (isspace(*value) && *value != '\n') |
| 366 |
value++; |
| 367 |
end = strchr(value, '\n'); |
| 368 |
} |
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
if (!strcmp(key, "region")) { |
| 384 |
*end = 0; |
| 385 |
new = get_region_struct(); |
| 386 |
new->name = strdup_local(value); |
| 387 |
} else if (!strcmp(key, "parent")) { |
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
*end = 0; |
| 394 |
|
| 395 |
if (!new) { |
| 396 |
LOG(llevError, "region.c: malformated regions file: \"parent\" before \"region\".\n"); |
| 397 |
fatal(SEE_LAST_ERROR); |
| 398 |
} |
| 399 |
if (!value) { |
| 400 |
LOG(llevError, "region.c: malformated regions file: No value given for \"parent\" key.\n"); |
| 401 |
fatal(SEE_LAST_ERROR); |
| 402 |
} |
| 403 |
new->parent_name = strdup_local(value); |
| 404 |
} else if (!strcmp(key, "longname")) { |
| 405 |
*end = 0; |
| 406 |
if (!new) { |
| 407 |
LOG(llevError, "region.c: malformated regions file: \"longname\" before \"region\".\n"); |
| 408 |
fatal(SEE_LAST_ERROR); |
| 409 |
} |
| 410 |
if (!value) { |
| 411 |
LOG(llevError, "region.c: malformated regions file: No value given for \"longname\" key.\n"); |
| 412 |
fatal(SEE_LAST_ERROR); |
| 413 |
} |
| 414 |
new->longname = strdup_local(value); |
| 415 |
} else if (!strcmp(key, "jail")) { |
| 416 |
|
| 417 |
char path[MAX_BUF]; |
| 418 |
int x, y; |
| 419 |
|
| 420 |
if (!new) { |
| 421 |
LOG(llevError, "region.c: malformated regions file: \"jail\" before \"region\".\n"); |
| 422 |
fatal(SEE_LAST_ERROR); |
| 423 |
} |
| 424 |
if (!value) { |
| 425 |
LOG(llevError, "region.c: malformated regions file: No value given for \"jail\" key.\n"); |
| 426 |
fatal(SEE_LAST_ERROR); |
| 427 |
} |
| 428 |
|
| 429 |
if (sscanf(value, "%[^ ] %d %d\n", path, &x, &y) != 3) { |
| 430 |
LOG(llevError, "region.c: malformated regions entry: jail %s\n", value); |
| 431 |
continue; |
| 432 |
} |
| 433 |
new->jailmap = strdup_local(path); |
| 434 |
new->jailx = x; |
| 435 |
new->jaily = y; |
| 436 |
} else if (!strcmp(key, "msg")) { |
| 437 |
if (!new) { |
| 438 |
LOG(llevError, "region.c: malformated regions file: \"msg\" before \"region\".\n"); |
| 439 |
fatal(SEE_LAST_ERROR); |
| 440 |
} |
| 441 |
while (fgets(buf, HUGE_BUF-1, fp) != NULL) { |
| 442 |
key = buf; |
| 443 |
while (isspace(*key)) |
| 444 |
key++; |
| 445 |
if (key && strcmp(key, "endmsg\n") == 0) |
| 446 |
break; |
| 447 |
else { |
| 448 |
strcpy(msgbuf+msgpos, key); |
| 449 |
msgpos += strlen(key); |
| 450 |
} |
| 451 |
} |
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
if (msgpos != 0) |
| 458 |
new->msg = strdup_local(msgbuf); |
| 459 |
|
| 460 |
|
| 461 |
msgpos = 0; |
| 462 |
} else if (!strcmp(key, "fallback")) { |
| 463 |
*end = 0; |
| 464 |
if (!new) { |
| 465 |
LOG(llevError, "region.c: malformated regions file: \"fallback\" before \"region\".\n"); |
| 466 |
fatal(SEE_LAST_ERROR); |
| 467 |
} |
| 468 |
if (!value) { |
| 469 |
LOG(llevError, "region.c: malformated regions file: No value given for \"fallback\" key.\n"); |
| 470 |
fatal(SEE_LAST_ERROR); |
| 471 |
} |
| 472 |
new->fallback = atoi(value); |
| 473 |
} else if (!strcmp(key, "end")) { |
| 474 |
if (!new) { |
| 475 |
LOG(llevError, "region.c: Ignoring spurious \"end\" between regions.\n"); |
| 476 |
continue; |
| 477 |
} |
| 478 |
|
| 479 |
for (reg = first_region; reg != NULL && reg->next != NULL; reg = reg->next) |
| 480 |
; |
| 481 |
|
| 482 |
if (reg == NULL) |
| 483 |
first_region = new; |
| 484 |
else |
| 485 |
reg->next = new; |
| 486 |
new = NULL; |
| 487 |
} else if (!strcmp(key, "nomore")) { |
| 488 |
if (new) { |
| 489 |
LOG(llevError, "region.c: Last region not properly closed.\n"); |
| 490 |
free(new); |
| 491 |
} |
| 492 |
|
| 493 |
break; |
| 494 |
} else { |
| 495 |
|
| 496 |
LOG(llevError, "Got unknown value in region file: %s %s\n", key, value); |
| 497 |
} |
| 498 |
} |
| 499 |
if (!key || strcmp(key, "nomore")) { |
| 500 |
LOG(llevError, "Got premature eof on regions file!\n"); |
| 501 |
free(new); |
| 502 |
} |
| 503 |
} |
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
static void assign_region_parents(void) { |
| 509 |
region *reg; |
| 510 |
uint32_t parent_count = 0; |
| 511 |
uint32_t region_count = 0; |
| 512 |
|
| 513 |
for (reg = first_region; reg != NULL; reg = reg->next) { |
| 514 |
if (reg->parent_name != NULL) { |
| 515 |
reg->parent = get_region_by_name(reg->parent_name); |
| 516 |
if (reg->parent == NULL) { |
| 517 |
LOG(llevError, "Couldn't find parent %s for region %s\n", reg->name, reg->parent_name); |
| 518 |
} |
| 519 |
parent_count++; |
| 520 |
} |
| 521 |
region_count++; |
| 522 |
} |
| 523 |
LOG(llevDebug, "regions: loaded %u with %u parents\n", |
| 524 |
region_count, parent_count); |
| 525 |
} |