/*-----------------------------------------------------------------------------
 * PROJECT:       Adventure Castle
 * --------
 *
 * MODULE:        castle_extract_dat.c
 * -------
 *
 * AUTHOR:        Dominique Rioual
 * -------
 * 
 * Extract data from castle.dat wich contains description of rooms 
 * Output description in plain text.
 *
 * BUILD:
 * ------
 * make extract
 *
 * LOGS:
 * -----
 *
 * 09/07/2026   Dominique Rioual      First release
 *
 ----------------------------------------------------------------------------*/
#include <stdio.h>

#define LOCS			181		/* number of locations */

unsigned places[LOCS];

int main(void) {
	
	FILE *fp;
	int chr;
    char *eptr1, *eptr2;
    unsigned locn = 0;

    static char code1[] = { 54, 126, 99, 72, 12, 213, 99, 0 },
		code2[] = { 77, 118, 41, 123, 18, 0 };


    fp = fopen("castle.dat","rb");
    while((chr =  getc(fp)) >= 0) {
		if(chr == 0) {
            places[locn] = ftell(fp);
            locn ++;
        }
    }

    for (locn = 0 ; locn < LOCS ; ++locn ) {
        fseek(fp, places[locn], SEEK_SET );
        printf("Room number: %d\n", locn);

        eptr1 = eptr2 = "";

		while((chr = getc(fp)) > 0) {
			if(!*eptr1)
				eptr1 = code1;
			if(!*eptr2)
				eptr2 = code2;
			chr ^= *eptr1++ ^ *eptr2++ ^ locn;
			putc(chr & 0x7f, stdout);
        }

    }

    fclose(fp);
}
