| 1 | // map2c.cpp : Defines the entry point for the console application. |
| 2 | // |
| 3 | |
| 4 | #include "stdafx.h" |
| 5 | #include <iostream> |
| 6 | #include <fstream> |
| 7 | #include <ctype.h> |
| 8 | |
| 9 | // Portability crap |
| 10 | #ifndef WIN32 |
| 11 | #define stricmp strcasecmp |
| 12 | #endif |
| 13 | |
| 14 | using namespace std; |
| 15 | |
| 16 | struct MAP_LUT |
| 17 | { |
| 18 | char letter; |
| 19 | char *entry; |
| 20 | MAP_LUT *next; |
| 21 | }; |
| 22 | |
| 23 | struct OUT_ENTRY |
| 24 | { |
| 25 | const char *name; |
| 26 | int x, y; |
| 27 | OUT_ENTRY *next; |
| 28 | }; |
| 29 | |
| 30 | MAP_LUT *glbLUTRoot = 0; |
| 31 | char **glbMap = 0; |
| 32 | int glbWidth = 0, glbHeight = 0; |
| 33 | |
| 34 | int glb_minlevel = -1, glb_maxlevel = -1; |
| 35 | int glb_rarity = 100; |
| 36 | int glb_mandatory = 0, glb_onlyonce = 0; |
| 37 | int glb_allowstairs = 1; |
| 38 | int glb_allowgeneration = 1; |
| 39 | int glb_allowdig = 1; |
| 40 | int glb_allowitems = 1; |
| 41 | int glb_allowtele = 1; |
| 42 | int glb_x = -1, glb_y = -1; |
| 43 | char *glb_quest = "-1"; |
| 44 | |
| 45 | void |
| 46 | writeList(ostream &os, const char *prefix, const char *listtype, OUT_ENTRY *list) |
| 47 | { |
| 48 | OUT_ENTRY *entry; |
| 49 | int i; |
| 50 | |
| 51 | os << "const PT2 glb_" << prefix << "_" << listtype << "list[] =" << endl; |
| 52 | os << "{" << endl; |
| 53 | i = 0; |
| 54 | for (entry = list; entry; entry = entry->next) |
| 55 | { |
| 56 | if (i) |
| 57 | os << "," << endl; |
| 58 | os << "\t{ " << entry->x << ", " << entry->y; |
| 59 | os << ", " << entry->name << " }"; |
| 60 | i++; |
| 61 | } |
| 62 | os << endl; |
| 63 | os << "};" << endl; |
| 64 | os << endl; |
| 65 | } |
| 66 | |
| 67 | void |
| 68 | readMap(istream &is, const char *firstline) |
| 69 | { |
| 70 | // Determine width from the line. |
| 71 | int i; |
| 72 | char line[1024]; |
| 73 | |
| 74 | for (i = 1; firstline[i] == '-'; i++); |
| 75 | |
| 76 | if (firstline[i] != '+') |
| 77 | { |
| 78 | cerr << "No terminating + on first map line!" << endl; |
| 79 | return; |
| 80 | } |
| 81 | |
| 82 | if (glbMap) |
| 83 | { |
| 84 | cerr << "Multiple maps defined!" << endl; |
| 85 | return; |
| 86 | } |
| 87 | |
| 88 | glbWidth = i - 1; |
| 89 | glbMap = new char *[100]; |
| 90 | |
| 91 | glbHeight = 0; |
| 92 | while (is.getline(line, 1000)) |
| 93 | { |
| 94 | if (line[0] == '+') |
| 95 | { |
| 96 | // Hit the end! |
| 97 | break; |
| 98 | } |
| 99 | |
| 100 | if (line[0] != '|') |
| 101 | { |
| 102 | cerr << "Missing opening | in map!" << endl; |
| 103 | return; |
| 104 | } |
| 105 | |
| 106 | if (strlen(line) != glbWidth + 2) |
| 107 | { |
| 108 | cerr << "Line: \"" << line << "\" is wrong length" << endl; |
| 109 | return; |
| 110 | } |
| 111 | glbMap[glbHeight] = new char [glbWidth+3]; |
| 112 | strcpy(glbMap[glbHeight], &line[1]); |
| 113 | glbMap[glbHeight][glbWidth] = '\0'; |
| 114 | glbHeight++; |
| 115 | if (glbHeight >= 100) |
| 116 | { |
| 117 | cerr << "Too many lines in the map!" << endl; |
| 118 | return; |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | void |
| 124 | readLUT(char *line) |
| 125 | { |
| 126 | char c; |
| 127 | MAP_LUT *lut; |
| 128 | char *word; |
| 129 | |
| 130 | c = line[0]; |
| 131 | |
| 132 | line += 2; |
| 133 | word = strtok(line, ", \t\n"); |
| 134 | while (word) |
| 135 | { |
| 136 | lut = new MAP_LUT; |
| 137 | lut->next = glbLUTRoot; |
| 138 | lut->letter = c; |
| 139 | lut->entry = strdup(word); |
| 140 | glbLUTRoot = lut; |
| 141 | word = strtok(0, ", \t\n"); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | int |
| 146 | check_boolean(char *word) |
| 147 | { |
| 148 | if (!stricmp(word, "true") || !stricmp(word, "on") || |
| 149 | !stricmp(word, "yes")) |
| 150 | return 1; |
| 151 | if (!stricmp(word, "false") || !stricmp(word, "off") || |
| 152 | !stricmp(word, "no")) |
| 153 | return 0; |
| 154 | |
| 155 | return atoi(word); |
| 156 | } |
| 157 | |
| 158 | void |
| 159 | readVarAssign(char *line) |
| 160 | { |
| 161 | char *var; |
| 162 | char *val; |
| 163 | |
| 164 | var = strtok(line, " =\t\n"); |
| 165 | val = strtok(0, " =\t\n"); |
| 166 | if (!var || !val) |
| 167 | { |
| 168 | cerr << "Illformed var assign ignored." << endl; |
| 169 | } |
| 170 | |
| 171 | // Test known variables: |
| 172 | if (!stricmp(var, "minlevel")) |
| 173 | glb_minlevel = atoi(val); |
| 174 | else if (!stricmp(var, "maxlevel")) |
| 175 | glb_maxlevel = atoi(val); |
| 176 | else if (!stricmp(var, "rarity")) |
| 177 | glb_rarity = atoi(val); |
| 178 | else if (!stricmp(var, "mandatory")) |
| 179 | glb_mandatory = check_boolean(val); |
| 180 | else if (!stricmp(var, "onlyonce")) |
| 181 | glb_onlyonce = check_boolean(val); |
| 182 | else if (!stricmp(var, "allowstairs")) |
| 183 | glb_allowstairs = check_boolean(val); |
| 184 | else if (!stricmp(var, "allowgeneration")) |
| 185 | glb_allowgeneration = check_boolean(val); |
| 186 | else if (!stricmp(var, "allowdig")) |
| 187 | glb_allowdig = check_boolean(val); |
| 188 | else if (!stricmp(var, "allowtele")) |
| 189 | glb_allowtele = check_boolean(val); |
| 190 | else if (!stricmp(var, "allowitems")) |
| 191 | glb_allowitems = check_boolean(val); |
| 192 | else if (!stricmp(var, "quest")) |
| 193 | glb_quest = strdup(val); |
| 194 | else if (!stricmp(var, "pos")) |
| 195 | { |
| 196 | glb_x = atoi(val); |
| 197 | val = strtok(0, " ,=\t\n"); |
| 198 | if (val) |
| 199 | glb_y = atoi(val); |
| 200 | else |
| 201 | cerr << "pos command has no y component!" << endl; |
| 202 | } |
| 203 | else |
| 204 | { |
| 205 | cerr << "Unknown variable " << var << endl; |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | int |
| 210 | main(int argc, char* argv[]) |
| 211 | { |
| 212 | if (argc < 3) |
| 213 | { |
| 214 | cerr << "map2c input.map output.cpp" << endl; |
| 215 | return -1; |
| 216 | } |
| 217 | |
| 218 | char *prefix; |
| 219 | int i; |
| 220 | |
| 221 | ifstream is(argv[1]); |
| 222 | |
| 223 | if (!is) |
| 224 | { |
| 225 | cerr << "Could not open to read " << argv[1] << endl; |
| 226 | return -1; |
| 227 | } |
| 228 | |
| 229 | char line[1024]; |
| 230 | |
| 231 | while (is.getline(line, 1000)) |
| 232 | { |
| 233 | // Skip blank lines. |
| 234 | if (!line[0] || !line[1]) |
| 235 | continue; |
| 236 | |
| 237 | // Skip comments. |
| 238 | if (line[0] == '#' && isspace(line[1])) |
| 239 | continue; |
| 240 | |
| 241 | // If we are +-, we are the start of a map. |
| 242 | if (line[0] == '+' && line[1] == '-') |
| 243 | { |
| 244 | readMap(is, line); |
| 245 | continue; |
| 246 | } |
| 247 | |
| 248 | // If : is second character, it is a definition. |
| 249 | if (line[1] == ':') |
| 250 | { |
| 251 | readLUT(line); |
| 252 | continue; |
| 253 | } |
| 254 | |
| 255 | if (strchr(line, '=')) |
| 256 | { |
| 257 | readVarAssign(line); |
| 258 | continue; |
| 259 | } |
| 260 | |
| 261 | cerr << "Ignoring unknown line:" << endl; |
| 262 | cerr << "\t\"" << line << "\"" << endl; |
| 263 | } |
| 264 | |
| 265 | if (!glbLUTRoot) |
| 266 | { |
| 267 | cerr << "No look ups found, aborting" << endl; |
| 268 | return -1; |
| 269 | } |
| 270 | |
| 271 | if (!glbMap) |
| 272 | { |
| 273 | cerr << "No map found, aborting" << endl; |
| 274 | return -1; |
| 275 | } |
| 276 | |
| 277 | ofstream os(argv[2]); |
| 278 | if (!os) |
| 279 | { |
| 280 | cerr << "Could not open for write " << argv[2] << endl; |
| 281 | return -1; |
| 282 | } |
| 283 | |
| 284 | prefix = strdup(argv[2]); |
| 285 | if (strchr(prefix, '.')) |
| 286 | *strchr(prefix, '.') = '\0'; |
| 287 | |
| 288 | // Output our lists.... |
| 289 | os << "// Auto-generated .map file" << endl; |
| 290 | os << "// DO NOT HAND EDIT" << endl; |
| 291 | os << "// Generated from " << argv[1] << endl; |
| 292 | os << endl; |
| 293 | os << "#include \"../map.h\"" << endl; |
| 294 | os << "#include \"../glbdef.h\"" << endl; |
| 295 | os << endl; |
| 296 | |
| 297 | // Output out our lookups.. |
| 298 | int x, y; |
| 299 | MAP_LUT *lut; |
| 300 | bool foundany; |
| 301 | |
| 302 | OUT_ENTRY *moblist = 0; |
| 303 | OUT_ENTRY *moblevellist = 0; |
| 304 | OUT_ENTRY *itemlist = 0; |
| 305 | OUT_ENTRY *itemtypelist = 0; |
| 306 | OUT_ENTRY *intrinsiclist = 0; |
| 307 | OUT_ENTRY *squareflaglist = 0; |
| 308 | OUT_ENTRY *signpostlist = 0; |
| 309 | OUT_ENTRY *entry; |
| 310 | |
| 311 | // We must null terminate our lists. |
| 312 | moblist = new OUT_ENTRY; |
| 313 | moblist->next = 0; |
| 314 | moblist->name = "MOB_NONE"; |
| 315 | moblist->x = -1; |
| 316 | moblist->y = -1; |
| 317 | |
| 318 | itemlist = new OUT_ENTRY; |
| 319 | itemlist->next = 0; |
| 320 | itemlist->name = "ITEM_NONE"; |
| 321 | itemlist->x = -1; |
| 322 | itemlist->y = -1; |
| 323 | |
| 324 | moblevellist = new OUT_ENTRY; |
| 325 | moblevellist->next = 0; |
| 326 | moblevellist->name = "MOBLEVEL_NONE"; |
| 327 | moblevellist->x = -1; |
| 328 | moblevellist->y = -1; |
| 329 | |
| 330 | itemtypelist = new OUT_ENTRY; |
| 331 | itemtypelist->next = 0; |
| 332 | itemtypelist->name = "ITEMTYPE_NONE"; |
| 333 | itemtypelist->x = -1; |
| 334 | itemtypelist->y = -1; |
| 335 | |
| 336 | intrinsiclist = new OUT_ENTRY; |
| 337 | intrinsiclist->next = 0; |
| 338 | intrinsiclist->name = "INTRINSIC_NONE"; |
| 339 | intrinsiclist->x = -1; |
| 340 | intrinsiclist->y = -1; |
| 341 | |
| 342 | squareflaglist = new OUT_ENTRY; |
| 343 | squareflaglist->next = 0; |
| 344 | squareflaglist->name = "SQUAREFLAG_NONE"; |
| 345 | squareflaglist->x = -1; |
| 346 | squareflaglist->y = -1; |
| 347 | |
| 348 | signpostlist = new OUT_ENTRY; |
| 349 | signpostlist->next = 0; |
| 350 | signpostlist->name = "SIGNPOST_NONE"; |
| 351 | signpostlist->x = -1; |
| 352 | signpostlist->y = -1; |
| 353 | |
| 354 | os << "const SQUARE_NAMES glb_" << prefix << "_squarelist[] =" << endl; |
| 355 | os << "{" << endl; |
| 356 | |
| 357 | i = 0; |
| 358 | for (y = 0; y < glbHeight; y++) |
| 359 | { |
| 360 | for (x = 0; x < glbWidth; x++) |
| 361 | { |
| 362 | if (i) |
| 363 | os << ",\t"; |
| 364 | if (i && !(i & 3)) |
| 365 | os << endl; |
| 366 | |
| 367 | foundany = false; |
| 368 | for (lut = glbLUTRoot; lut; lut = lut->next) |
| 369 | { |
| 370 | if (lut->letter == glbMap[y][x]) |
| 371 | { |
| 372 | // Found a valid legend. |
| 373 | foundany = true; |
| 374 | |
| 375 | if (!strncmp(lut->entry, "MOBLEVEL", 8)) |
| 376 | { |
| 377 | entry = new OUT_ENTRY; |
| 378 | entry->next = moblevellist; |
| 379 | moblevellist = entry; |
| 380 | entry->name = lut->entry; |
| 381 | entry->x = x; |
| 382 | entry->y = y; |
| 383 | } |
| 384 | else if (!strncmp(lut->entry, "MOB", 3)) |
| 385 | { |
| 386 | entry = new OUT_ENTRY; |
| 387 | entry->next = moblist; |
| 388 | moblist = entry; |
| 389 | entry->name = lut->entry; |
| 390 | entry->x = x; |
| 391 | entry->y = y; |
| 392 | } |
| 393 | else if (!strncmp(lut->entry, "SQUAREFLAG", 10)) |
| 394 | { |
| 395 | entry = new OUT_ENTRY; |
| 396 | entry->next = squareflaglist; |
| 397 | squareflaglist = entry; |
| 398 | entry->name = lut->entry; |
| 399 | entry->x = x; |
| 400 | entry->y = y; |
| 401 | } |
| 402 | else if (!strncmp(lut->entry, "INTRINSIC", 9)) |
| 403 | { |
| 404 | entry = new OUT_ENTRY; |
| 405 | entry->next = intrinsiclist; |
| 406 | intrinsiclist = entry; |
| 407 | entry->name = lut->entry; |
| 408 | entry->x = x; |
| 409 | entry->y = y; |
| 410 | } |
| 411 | else if (!strncmp(lut->entry, "SQUARE", 6)) |
| 412 | { |
| 413 | os << lut->entry; |
| 414 | i++; |
| 415 | } |
| 416 | else if (!strncmp(lut->entry, "ITEMTYPE", 8)) |
| 417 | { |
| 418 | entry = new OUT_ENTRY; |
| 419 | entry->next = itemtypelist; |
| 420 | itemtypelist = entry; |
| 421 | entry->name = lut->entry; |
| 422 | entry->x = x; |
| 423 | entry->y = y; |
| 424 | } |
| 425 | else if (!strncmp(lut->entry, "ITEM", 4)) |
| 426 | { |
| 427 | entry = new OUT_ENTRY; |
| 428 | entry->next = itemlist; |
| 429 | itemlist = entry; |
| 430 | entry->name = lut->entry; |
| 431 | entry->x = x; |
| 432 | entry->y = y; |
| 433 | } |
| 434 | else if (!strncmp(lut->entry, "SIGNPOST", 8)) |
| 435 | { |
| 436 | entry = new OUT_ENTRY; |
| 437 | entry->next = signpostlist; |
| 438 | signpostlist = entry; |
| 439 | entry->name = lut->entry; |
| 440 | entry->x = x; |
| 441 | entry->y = y; |
| 442 | } |
| 443 | else |
| 444 | { |
| 445 | cerr << "Unknown square type " << lut->entry << ", ignoring" << endl; |
| 446 | } |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | if (!foundany) |
| 451 | { |
| 452 | fprintf(stderr, "Could not find symbol %c in Legend!\r\n", |
| 453 | glbMap[y][x]); |
| 454 | } |
| 455 | } |
| 456 | } |
| 457 | os << endl; |
| 458 | os << "};" << endl; |
| 459 | |
| 460 | os << endl; |
| 461 | |
| 462 | // Output our moblist & itemlist. |
| 463 | writeList(os, prefix, "mob", moblist); |
| 464 | writeList(os, prefix, "item", itemlist); |
| 465 | writeList(os, prefix, "itemtype", itemtypelist); |
| 466 | writeList(os, prefix, "moblevel", moblevellist); |
| 467 | writeList(os, prefix, "intrinsic", intrinsiclist); |
| 468 | writeList(os, prefix, "squareflag", squareflaglist); |
| 469 | writeList(os, prefix, "signpost", signpostlist); |
| 470 | |
| 471 | // Finally, the stuff to link it together. |
| 472 | os << "const ROOM_DEF glb_" << prefix << "_roomdef =" << endl; |
| 473 | os << "{" << endl; |
| 474 | os << "\t{ " << glbWidth << ", " << glbHeight << ", 0 }," << endl; |
| 475 | os << "\tglb_" << prefix << "_squarelist," << endl; |
| 476 | os << "\tglb_" << prefix << "_squareflaglist," << endl; |
| 477 | os << "\tglb_" << prefix << "_itemlist," << endl; |
| 478 | os << "\tglb_" << prefix << "_moblist," << endl; |
| 479 | os << "\tglb_" << prefix << "_itemtypelist," << endl; |
| 480 | os << "\tglb_" << prefix << "_moblevellist," << endl; |
| 481 | os << "\tglb_" << prefix << "_intrinsiclist," << endl; |
| 482 | os << "\tglb_" << prefix << "_signpostlist," << endl; |
| 483 | |
| 484 | // Global values: |
| 485 | os << "\t" << glb_minlevel << ", " << glb_maxlevel << "," << endl; |
| 486 | os << "\t" << glb_rarity << "," << endl; |
| 487 | os << "\t" << glb_mandatory << ", " << glb_onlyonce << "," << endl; |
| 488 | os << "\t" << glb_allowstairs << "," << endl; |
| 489 | os << "\t" << glb_allowgeneration << "," << endl; |
| 490 | os << "\t" << glb_allowdig << "," << endl; |
| 491 | os << "\t" << glb_allowitems << "," << endl; |
| 492 | os << "\t" << glb_allowtele << "," << endl; |
| 493 | os << "\t" << glb_x << ", " << glb_y << ", " << endl; |
| 494 | os << "\t" << glb_quest << ", " << endl; |
| 495 | // Insert new globals here so you get the comma proper! |
| 496 | |
| 497 | os << "\t" << "\"" << prefix << "\"" << endl; |
| 498 | |
| 499 | os << "};" << endl; |
| 500 | |
| 501 | return 0; |
| 502 | } |
| 503 |