| 1 | /* |
| 2 | * PROPRIETARY INFORMATION. This software is proprietary to POWDER |
| 3 | * Development, and is not to be reproduced, transmitted, or disclosed |
| 4 | * in any way without written permission. |
| 5 | * |
| 6 | * Produced by: Jeff Lait |
| 7 | * |
| 8 | * POWDER Development |
| 9 | * |
| 10 | * NAME: compendium.cpp ( POWDER Library, C++ ) |
| 11 | * |
| 12 | * COMMENTS: |
| 13 | * Extracts relevant info from game data into something |
| 14 | * parseable by the website. |
| 15 | */ |
| 16 | |
| 17 | #include "mygba.h" |
| 18 | #include "../../glbdef.h" |
| 19 | #include "../../encyclopedia.h" |
| 20 | #include "../../buf.h" |
| 21 | #include "../../gfx/all_bitmaps.h" |
| 22 | |
| 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
| 26 | #include <ctype.h> |
| 27 | |
| 28 | const encyclopedia_entry * |
| 29 | encyc_getentry(const char *bookname, int key) |
| 30 | { |
| 31 | const encyclopedia_book *book; |
| 32 | int booknum; |
| 33 | int entrynum; |
| 34 | |
| 35 | for (booknum = 0; booknum < NUM_ENCYCLOPEDIA_BOOKS; booknum++) |
| 36 | { |
| 37 | book = glb_encyclopedia[booknum]; |
| 38 | |
| 39 | if (strcmp(book->bookname, bookname)) |
| 40 | continue; |
| 41 | |
| 42 | for (entrynum = 0; entrynum < book->numentries; entrynum++) |
| 43 | { |
| 44 | if (book->entries[entrynum]->key == key) |
| 45 | return book->entries[entrynum]; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | return 0; |
| 50 | } |
| 51 | |
| 52 | static void |
| 53 | writeportableshort(FILE *fp, u16 v) |
| 54 | { |
| 55 | u8 c[2]; |
| 56 | |
| 57 | c[0] = v & 0xff; |
| 58 | c[1] = v >> 8; |
| 59 | |
| 60 | fwrite(c, 2, 1, fp); |
| 61 | } |
| 62 | |
| 63 | static void |
| 64 | writeportableint(FILE *fp, u32 v) |
| 65 | { |
| 66 | writeportableshort(fp, v & 0xffff); |
| 67 | writeportableshort(fp, v >> 16); |
| 68 | } |
| 69 | |
| 70 | void |
| 71 | convertTo24bit(u16 clr, u8 &red, u8 &green, u8 &blue) |
| 72 | { |
| 73 | red = clr & 31; |
| 74 | red <<= 3; |
| 75 | red += 4; |
| 76 | clr >>= 5; |
| 77 | green = clr & 31; |
| 78 | green <<= 3; |
| 79 | green += 4; |
| 80 | clr >>= 5; |
| 81 | blue = clr & 31; |
| 82 | blue <<= 3; |
| 83 | blue += 4; |
| 84 | } |
| 85 | |
| 86 | // A 256 pixel image doesn't need a 256 colour palette :> |
| 87 | // Storing 24bit actually saves 512 byes, meaning png looks less tempting. |
| 88 | void |
| 89 | writeDataToBmp24bit(const char *fname, const u8 *data, const u16 *palette) |
| 90 | { |
| 91 | FILE *fp; |
| 92 | int i, x, y; |
| 93 | u8 red, green, blue, index; |
| 94 | |
| 95 | fp = fopen(fname, "wb"); |
| 96 | if (!fp) |
| 97 | { |
| 98 | printf("Failed to open %s for writing\n", fname); |
| 99 | return; |
| 100 | } |
| 101 | |
| 102 | // Write the header. |
| 103 | fwrite("BM", 2, 1, fp); |
| 104 | writeportableint(fp, 16*16*3+54); // size |
| 105 | writeportableint(fp, 0); // reserved |
| 106 | writeportableint(fp, 54); // Header size + palette |
| 107 | writeportableint(fp, 40); // Info size |
| 108 | writeportableint(fp, 16); // Width |
| 109 | writeportableint(fp, 16); // Height |
| 110 | writeportableshort(fp, 1); // Bit planes |
| 111 | writeportableshort(fp, 24); // Bits |
| 112 | writeportableint(fp, 0); // Compression |
| 113 | writeportableint(fp, 0); // image data size |
| 114 | writeportableint(fp, 0); // xpel/m |
| 115 | writeportableint(fp, 0); // ypel/m |
| 116 | writeportableint(fp, 0); // used colours |
| 117 | writeportableint(fp, 0); // important colours |
| 118 | |
| 119 | // Write out our data. Our source data is tilified so we |
| 120 | // need to invert the operation here |
| 121 | // 16*3 divides four evenly so we have no craziness with our |
| 122 | // scanline widths. |
| 123 | for (y = 16; y --> 0;) // the elusive --> operator. |
| 124 | { |
| 125 | for (x = 0; x < 16; x++) |
| 126 | { |
| 127 | // Simple sub-tile index |
| 128 | i = x & 7; |
| 129 | i += (y & 7) * 8; |
| 130 | |
| 131 | // Which tile |
| 132 | if (x & 8) |
| 133 | i += 8 * 8; |
| 134 | if (y & 8) |
| 135 | i += 2 * 8 * 8; |
| 136 | |
| 137 | index = data[i]; |
| 138 | convertTo24bit(palette[index], red, green, blue); |
| 139 | |
| 140 | if (!index) |
| 141 | { |
| 142 | // Colour zero gets hard coded to POWDER brown. |
| 143 | red = 0xAF; |
| 144 | green = 0xA2; |
| 145 | blue = 0x90; |
| 146 | } |
| 147 | |
| 148 | fwrite(&blue, 1, 1, fp); |
| 149 | fwrite(&green, 1, 1, fp); |
| 150 | fwrite(&red, 1, 1, fp); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | fclose(fp); |
| 155 | } |
| 156 | |
| 157 | void |
| 158 | writeDataToBmp8bit(const char *fname, const u8 *data, const u16 *palette) |
| 159 | { |
| 160 | FILE *fp; |
| 161 | int i, x, y; |
| 162 | u8 red, green, blue, zero = 0; |
| 163 | |
| 164 | fp = fopen(fname, "wb"); |
| 165 | if (!fp) |
| 166 | { |
| 167 | printf("Failed to open %s for writing\n", fname); |
| 168 | return; |
| 169 | } |
| 170 | |
| 171 | // Write the header. |
| 172 | fwrite("BM", 2, 1, fp); |
| 173 | writeportableint(fp, 16*16+1078); // size |
| 174 | writeportableint(fp, 0); // reserved |
| 175 | writeportableint(fp, 1078); // Header size + palette |
| 176 | writeportableint(fp, 40); // Info size |
| 177 | writeportableint(fp, 16); // Width |
| 178 | writeportableint(fp, 16); // Height |
| 179 | writeportableshort(fp, 1); // Bit planes |
| 180 | writeportableshort(fp, 8); // Bits |
| 181 | writeportableint(fp, 0); // Compression |
| 182 | writeportableint(fp, 0); // image data size |
| 183 | writeportableint(fp, 0); // xpel/m |
| 184 | writeportableint(fp, 0); // ypel/m |
| 185 | writeportableint(fp, 0); // used colours |
| 186 | writeportableint(fp, 0); // important colours |
| 187 | |
| 188 | // Write out our palette... |
| 189 | // Format is BGRA, where A is unused. |
| 190 | for (i = 0; i < 256; i++) |
| 191 | { |
| 192 | convertTo24bit(palette[i], red, green, blue); |
| 193 | |
| 194 | if (!i) |
| 195 | { |
| 196 | // Colour zero gets hard coded to POWDER brown. |
| 197 | red = 0xAF; |
| 198 | green = 0xA2; |
| 199 | blue = 0x90; |
| 200 | } |
| 201 | |
| 202 | fwrite(&blue, 1, 1, fp); |
| 203 | fwrite(&green, 1, 1, fp); |
| 204 | fwrite(&red, 1, 1, fp); |
| 205 | fwrite(&zero, 1, 1, fp); |
| 206 | } |
| 207 | |
| 208 | // Write out our data. Our source data is tilified so we |
| 209 | // need to invert the operation here |
| 210 | // 16 divides four evenly so we have no craziness with our |
| 211 | // scanline widths. |
| 212 | for (y = 16; y --> 0;) // the elusive --> operator. |
| 213 | { |
| 214 | for (x = 0; x < 16; x++) |
| 215 | { |
| 216 | // Simple sub-tile index |
| 217 | i = x & 7; |
| 218 | i += (y & 7) * 8; |
| 219 | |
| 220 | // Which tile |
| 221 | if (x & 8) |
| 222 | i += 8 * 8; |
| 223 | if (y & 8) |
| 224 | i += 2 * 8 * 8; |
| 225 | |
| 226 | fwrite(&data[i], 1, 1, fp); |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | fclose(fp); |
| 231 | } |
| 232 | |
| 233 | |
| 234 | void |
| 235 | writeTileToBmp(BUF fname, int tile) |
| 236 | { |
| 237 | // Find address of data... |
| 238 | const u8 *data; |
| 239 | |
| 240 | data = &glb_tilesets[4].dungeon[tile * 4 * 8 * 8]; |
| 241 | |
| 242 | writeDataToBmp24bit(fname.buffer(), data, glb_tilesets[4].palette); |
| 243 | } |
| 244 | |
| 245 | void |
| 246 | writeSpriteToBmp(BUF fname, int tile) |
| 247 | { |
| 248 | // Find address of data... |
| 249 | const u8 *data; |
| 250 | |
| 251 | data = &glb_tilesets[4].sprite[tile * 4 * 8 * 8]; |
| 252 | |
| 253 | writeDataToBmp24bit(fname.buffer(), data, glb_tilesets[4].spritepalette); |
| 254 | } |
| 255 | |
| 256 | // Does this char mark the end of a sentence? |
| 257 | bool |
| 258 | gram_isendsentence(char c) |
| 259 | { |
| 260 | switch (c) |
| 261 | { |
| 262 | case '.': |
| 263 | case '!': |
| 264 | case '?': |
| 265 | return true; |
| 266 | } |
| 267 | return false; |
| 268 | } |
| 269 | |
| 270 | BUF |
| 271 | gram_capitalize(BUF buf) |
| 272 | { |
| 273 | bool hard = false; |
| 274 | char *s; |
| 275 | bool docaps = true; // Start of sentence, caps. |
| 276 | BUF result; |
| 277 | const char *str = buf.buffer(); |
| 278 | |
| 279 | result = buf; |
| 280 | |
| 281 | // THis cast to char * is safe as we only assign to s after a harden. |
| 282 | for (s = (char *) str; *s; s++) |
| 283 | { |
| 284 | if (!isspace(*s)) |
| 285 | { |
| 286 | // Note we do not eat up caps if we get a non-alpha, ie: |
| 287 | // "foo bar" will become "Foo bar" (ignoring the quote) |
| 288 | // Note that numerical keys are ignored, so |
| 289 | // "+1 mace" becomes "+1 Mace" |
| 290 | if (docaps && isalpha(*s)) |
| 291 | { |
| 292 | if (islower(*s)) |
| 293 | { |
| 294 | if (!hard) |
| 295 | { |
| 296 | result.uniquify(); |
| 297 | s = result.evildata() + (s - str); |
| 298 | hard = true; |
| 299 | } |
| 300 | *s = toupper(*s); |
| 301 | } |
| 302 | // Eat the caps. |
| 303 | docaps = false; |
| 304 | } |
| 305 | |
| 306 | // Determine if this is end-sentence, if so, the next |
| 307 | // char should be capped. |
| 308 | if (gram_isendsentence(*s)) |
| 309 | docaps = true; |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | return result; |
| 314 | } |
| 315 | |
| 316 | void |
| 317 | writeFile(FILE *fplist, const char *rawname, const encyclopedia_entry *entry, |
| 318 | bool istile, int tile) |
| 319 | { |
| 320 | BUF fname, bmpname, imgname, name; |
| 321 | FILE *fp; |
| 322 | int i; |
| 323 | |
| 324 | fname.sprintf("%s.txt", rawname); |
| 325 | fname.makeFileSafe(); |
| 326 | bmpname.sprintf("%s.bmp", rawname); |
| 327 | bmpname.makeFileSafe(); |
| 328 | imgname.sprintf("%s", rawname); |
| 329 | imgname.makeFileSafe(); |
| 330 | |
| 331 | // We create bmps, but link to pngs as we expect |
| 332 | // later people to convert.... |
| 333 | // Rather than harcoding ourselves, our icon has no |
| 334 | // extesion so it is trivial to add it in the template. |
| 335 | fp = fopen(fname.buffer(), "wt"); |
| 336 | |
| 337 | // Header info |
| 338 | fprintf(fp, "Icon: %s\n", imgname.buffer()); |
| 339 | name.strcpy(rawname); |
| 340 | name = gram_capitalize(name); |
| 341 | fprintf(fp, "Name: %s\n", name.buffer()); |
| 342 | |
| 343 | fprintf(fp, "~~~~\n"); |
| 344 | |
| 345 | // Contents. Lines aren't wrapped, but we don't care. |
| 346 | for (i = 0; i < entry->numlines; i++) |
| 347 | { |
| 348 | fprintf(fp, "%s\n", entry->lines[i]); |
| 349 | } |
| 350 | // All done! |
| 351 | fclose(fp); |
| 352 | |
| 353 | // Output the relevant .bmp |
| 354 | if (istile) |
| 355 | writeTileToBmp(bmpname, tile); |
| 356 | else |
| 357 | writeSpriteToBmp(bmpname, tile); |
| 358 | |
| 359 | fprintf(fplist, "%s\n", fname.buffer()); |
| 360 | } |
| 361 | |
| 362 | int |
| 363 | main() |
| 364 | { |
| 365 | MOB_NAMES mob; |
| 366 | SPELL_NAMES spell; |
| 367 | const encyclopedia_entry *entry; |
| 368 | FILE *fplist; |
| 369 | encyclopedia_entry e; |
| 370 | |
| 371 | fplist = fopen("moblist.txt", "wt"); |
| 372 | |
| 373 | // Bestiary entry.... |
| 374 | e.numlines = 1; |
| 375 | e.lines = new const char*[1]; |
| 376 | e.lines[0] = "Piecing together the tales of those few that have escaped the dungeon depths, we have assembled this bestiary to help forewarn you of the dangers you face. Be warned it is incomplete: we've heard tale of many fearsome foes not on this paltry list!"; |
| 377 | writeFile(fplist, "bestiary", &e, true, TILE_VOID); |
| 378 | |
| 379 | FOREACH_MOB(mob) |
| 380 | { |
| 381 | if (!glb_mobdefs[mob].publish) |
| 382 | continue; |
| 383 | // Write out relevant data... |
| 384 | entry = encyc_getentry("MOB", mob); |
| 385 | if (entry) |
| 386 | { |
| 387 | writeFile(fplist, glb_mobdefs[mob].name, entry, |
| 388 | true, glb_mobdefs[mob].tile); |
| 389 | } |
| 390 | } |
| 391 | fclose(fplist); |
| 392 | |
| 393 | fplist = fopen("spelllist.txt", "wt"); |
| 394 | |
| 395 | e.lines[0] = "Wizards jealously guard their secrets, so any attempt to assemble all magic in one location is doomed to be incomplete. Consider this a mere primer for the wide range of spells that you may encounter on your journey."; |
| 396 | writeFile(fplist, "spelltome", &e, true, TILE_BLUESPELLBOOK); |
| 397 | FOREACH_SPELL(spell) |
| 398 | { |
| 399 | if (!glb_spelldefs[spell].publish) |
| 400 | continue; |
| 401 | |
| 402 | entry = encyc_getentry("SPELL", spell); |
| 403 | |
| 404 | if (entry) |
| 405 | { |
| 406 | writeFile(fplist, glb_spelldefs[spell].name, entry, |
| 407 | false, glb_spelldefs[spell].tile); |
| 408 | } |
| 409 | } |
| 410 | fclose(fplist); |
| 411 | |
| 412 | // We also want to write out the spellbook icon of choice. |
| 413 | BUF buf; |
| 414 | buf.reference("spellbook.bmp"); |
| 415 | writeTileToBmp(buf, TILE_BLUESPELLBOOK); |
| 416 | |
| 417 | return 0; |
| 418 | } |