File: compendium.cpp

package info (click to toggle)
powder 117-2
  • links: PTS
  • area: non-free
  • in suites: stretch
  • size: 10,576 kB
  • ctags: 3,545
  • sloc: cpp: 55,002; makefile: 541; sh: 258; objc: 245; ansic: 107; csh: 54
file content (418 lines) | stat: -rw-r--r-- 9,742 bytes parent folder | download | duplicates (7)
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/*
 * PROPRIETARY INFORMATION.  This software is proprietary to POWDER
 * Development, and is not to be reproduced, transmitted, or disclosed
 * in any way without written permission.
 *
 * Produced by:	Jeff Lait
 *
 *      	POWDER Development
 *
 * NAME:        compendium.cpp ( POWDER Library, C++ )
 *
 * COMMENTS:
 *	Extracts relevant info from game data into something
 *	parseable by the website.
 */

#include "mygba.h"
#include "../../glbdef.h"
#include "../../encyclopedia.h"
#include "../../buf.h"
#include "../../gfx/all_bitmaps.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

const encyclopedia_entry *
encyc_getentry(const char *bookname, int key)
{
    const encyclopedia_book	*book;
    int				 booknum;
    int				 entrynum;
    
    for (booknum = 0; booknum < NUM_ENCYCLOPEDIA_BOOKS; booknum++)
    {
	book = glb_encyclopedia[booknum];

	if (strcmp(book->bookname, bookname))
	    continue;
	
	for (entrynum = 0; entrynum < book->numentries; entrynum++)
	{
	    if (book->entries[entrynum]->key == key)
		return book->entries[entrynum];
	}
    }

    return 0;
}

static void
writeportableshort(FILE *fp, u16 v)
{
    u8		c[2];

    c[0] = v & 0xff;
    c[1] = v >> 8;

    fwrite(c, 2, 1, fp);
}

static void
writeportableint(FILE *fp, u32 v)
{
    writeportableshort(fp, v & 0xffff);
    writeportableshort(fp, v >> 16);
}

void
convertTo24bit(u16 clr, u8 &red, u8 &green, u8 &blue)
{
    red = clr & 31;
    red <<= 3;
    red += 4;
    clr >>= 5;
    green = clr & 31;
    green <<= 3;
    green += 4;
    clr >>= 5;
    blue = clr & 31;
    blue <<= 3;
    blue += 4;
}

// A 256 pixel image doesn't need a 256 colour palette :>
// Storing 24bit actually saves 512 byes, meaning png looks less tempting.
void
writeDataToBmp24bit(const char *fname, const u8 *data, const u16 *palette)
{
    FILE	*fp;
    int		 i, x, y;
    u8		 red, green, blue, index;

    fp = fopen(fname, "wb");
    if (!fp)
    {
	printf("Failed to open %s for writing\n", fname);
	return;
    }

    // Write the header.
    fwrite("BM", 2, 1, fp);
    writeportableint(fp, 16*16*3+54);	// size
    writeportableint(fp, 0);	// reserved
    writeportableint(fp, 54);	// Header size + palette
    writeportableint(fp, 40);	// Info size
    writeportableint(fp, 16);	// Width
    writeportableint(fp, 16);	// Height
    writeportableshort(fp, 1);	// Bit planes
    writeportableshort(fp, 24);	// Bits
    writeportableint(fp, 0);	// Compression
    writeportableint(fp, 0);	// image data size
    writeportableint(fp, 0);	// xpel/m
    writeportableint(fp, 0);	// ypel/m
    writeportableint(fp, 0);	// used colours
    writeportableint(fp, 0);	// important colours

    // Write out our data.  Our source data is tilified so we
    // need to invert the operation here
    // 16*3 divides four evenly so we have no craziness with our
    // scanline widths.
    for (y = 16; y --> 0;)  // the elusive --> operator.
    {
	for (x = 0; x < 16; x++)
	{
	    // Simple sub-tile index
	    i = x & 7;
	    i += (y & 7) * 8;

	    // Which tile
	    if (x & 8)
		i += 8 * 8;
	    if (y & 8)
		i += 2 * 8 * 8;

	    index = data[i];
	    convertTo24bit(palette[index], red, green, blue);

	    if (!index)
	    {
		// Colour zero gets hard coded to POWDER brown.
		red = 0xAF;
		green = 0xA2;
		blue = 0x90;
	    }

	    fwrite(&blue, 1, 1, fp);
	    fwrite(&green, 1, 1, fp);
	    fwrite(&red, 1, 1, fp);
	}
    }

    fclose(fp);
}

void
writeDataToBmp8bit(const char *fname, const u8 *data, const u16 *palette)
{
    FILE		*fp;
    int		 i, x, y;
    u8		 red, green, blue, zero = 0;

    fp = fopen(fname, "wb");
    if (!fp)
    {
	printf("Failed to open %s for writing\n", fname);
	return;
    }

    // Write the header.
    fwrite("BM", 2, 1, fp);
    writeportableint(fp, 16*16+1078);	// size
    writeportableint(fp, 0);	// reserved
    writeportableint(fp, 1078);	// Header size + palette
    writeportableint(fp, 40);	// Info size
    writeportableint(fp, 16);	// Width
    writeportableint(fp, 16);	// Height
    writeportableshort(fp, 1);	// Bit planes
    writeportableshort(fp, 8);	// Bits
    writeportableint(fp, 0);	// Compression
    writeportableint(fp, 0);	// image data size
    writeportableint(fp, 0);	// xpel/m
    writeportableint(fp, 0);	// ypel/m
    writeportableint(fp, 0);	// used colours
    writeportableint(fp, 0);	// important colours

    // Write out our palette...
    // Format is BGRA, where A is unused.
    for (i = 0; i < 256; i++)
    {
	convertTo24bit(palette[i], red, green, blue);

	if (!i)
	{
	    // Colour zero gets hard coded to POWDER brown.
	    red = 0xAF;
	    green = 0xA2;
	    blue = 0x90;
	}

	fwrite(&blue, 1, 1, fp);
	fwrite(&green, 1, 1, fp);
	fwrite(&red, 1, 1, fp);
	fwrite(&zero, 1, 1, fp);
    }

    // Write out our data.  Our source data is tilified so we
    // need to invert the operation here
    // 16 divides four evenly so we have no craziness with our
    // scanline widths.
    for (y = 16; y --> 0;)  // the elusive --> operator.
    {
	for (x = 0; x < 16; x++)
	{
	    // Simple sub-tile index
	    i = x & 7;
	    i += (y & 7) * 8;

	    // Which tile
	    if (x & 8)
		i += 8 * 8;
	    if (y & 8)
		i += 2 * 8 * 8;

	    fwrite(&data[i], 1, 1, fp);
	}
    }

    fclose(fp);
}


void
writeTileToBmp(BUF fname, int tile)
{
    // Find address of data...
    const u8		*data;

    data = &glb_tilesets[4].dungeon[tile * 4 * 8 * 8];
    
    writeDataToBmp24bit(fname.buffer(), data, glb_tilesets[4].palette);
}

void
writeSpriteToBmp(BUF fname, int tile)
{
    // Find address of data...
    const u8		*data;

    data = &glb_tilesets[4].sprite[tile * 4 * 8 * 8];
    
    writeDataToBmp24bit(fname.buffer(), data, glb_tilesets[4].spritepalette);
}

// Does this char mark the end of a sentence?
bool
gram_isendsentence(char c)
{
    switch (c)
    {
	case '.':
	case '!':
	case '?':
	    return true;
    }
    return false;
}

BUF
gram_capitalize(BUF buf)
{
    bool		hard = false;
    char		*s;
    bool		docaps = true;	// Start of sentence, caps.
    BUF			result;
    const char		*str = buf.buffer();

    result = buf;
    
    // THis cast to char * is safe as we only assign to s after a harden.
    for (s = (char *) str; *s; s++)
    {
	if (!isspace(*s))
	{
	    // Note we do not eat up caps if we get a non-alpha, ie:
	    // "foo bar" will become "Foo bar" (ignoring the quote)
	    // Note that numerical keys are ignored, so
	    // "+1 mace" becomes "+1 Mace"
	    if (docaps && isalpha(*s))
	    {
		if (islower(*s))
		{
		    if (!hard)
		    {
			result.uniquify();
			s = result.evildata() + (s - str);
			hard = true;
		    }
		    *s = toupper(*s);
		}
		// Eat the caps.
		docaps = false;
	    }

	    // Determine if this is end-sentence, if so, the next
	    // char should be capped.
	    if (gram_isendsentence(*s))
		docaps = true;
	}
    }
    
    return result;
}

void
writeFile(FILE *fplist, const char *rawname, const encyclopedia_entry *entry,
	bool istile, int tile)
{
    BUF		 fname, bmpname, imgname, name;
    FILE	*fp;
    int		 i;

    fname.sprintf("%s.txt", rawname);
    fname.makeFileSafe();
    bmpname.sprintf("%s.bmp", rawname);
    bmpname.makeFileSafe();
    imgname.sprintf("%s", rawname);
    imgname.makeFileSafe();

    // We create bmps, but link to pngs as we expect
    // later people to convert....
    // Rather than harcoding ourselves, our icon has no
    // extesion so it is trivial to add it in the template.
    fp = fopen(fname.buffer(), "wt");

    // Header info
    fprintf(fp, "Icon: %s\n", imgname.buffer());
    name.strcpy(rawname);
    name = gram_capitalize(name);
    fprintf(fp, "Name: %s\n", name.buffer());

    fprintf(fp, "~~~~\n");

    // Contents.  Lines aren't wrapped, but we don't care.
    for (i = 0; i < entry->numlines; i++)
    {
	fprintf(fp, "%s\n", entry->lines[i]);
    }
    // All done!
    fclose(fp);

    // Output the relevant .bmp
    if (istile)
	writeTileToBmp(bmpname, tile);
    else
	writeSpriteToBmp(bmpname, tile);

    fprintf(fplist, "%s\n", fname.buffer());
}

int
main()
{
    MOB_NAMES			 mob;
    SPELL_NAMES			 spell;
    const encyclopedia_entry	*entry;
    FILE			*fplist;
    encyclopedia_entry		 e;

    fplist = fopen("moblist.txt", "wt");
    
    // Bestiary entry....
    e.numlines = 1;
    e.lines = new const char*[1];
    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!";
    writeFile(fplist, "bestiary", &e, true, TILE_VOID);
    
    FOREACH_MOB(mob)
    {
	if (!glb_mobdefs[mob].publish)
	    continue;
	// Write out relevant data...
	entry = encyc_getentry("MOB", mob);
	if (entry)
	{
	    writeFile(fplist, glb_mobdefs[mob].name, entry,
			true, glb_mobdefs[mob].tile);
	}
    }
    fclose(fplist);

    fplist = fopen("spelllist.txt", "wt");

    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.";
    writeFile(fplist, "spelltome", &e, true, TILE_BLUESPELLBOOK);
    FOREACH_SPELL(spell)
    {
	if (!glb_spelldefs[spell].publish)
	    continue;

	entry = encyc_getentry("SPELL", spell);

	if (entry)
	{
	    writeFile(fplist, glb_spelldefs[spell].name, entry,
			false, glb_spelldefs[spell].tile);
	}
    }
    fclose(fplist);

    // We also want to write out the spellbook icon of choice.
    BUF buf;
    buf.reference("spellbook.bmp");
    writeTileToBmp(buf, TILE_BLUESPELLBOOK);

    return 0;
}