1 #include <stdio.h>
2 #include <string.h>
3 #include <ctype.h>
4 #include "defs.h"
5 #include "externs.h"
6 #include "protos.h"
7 
8 /* locals */
9 static unsigned char buffer[512];
10 
11 
12 /* ----
13  * pce_load_map()
14  * ----
15  */
16 
17 int
pce_load_map(char * fname,int mode)18 pce_load_map(char *fname, int mode)
19 {
20 	FILE *fp;
21 	unsigned char header[16];
22 	int fsize;
23 	int size;
24 	int nb;
25 	int cnt;
26 	int i, j;
27 
28 	/* init */
29 	cnt = 0;
30 
31 	/* open the file */
32 	if ((fp = open_file(fname, "rb")) == NULL) {
33 		fatal_error("Can not open file!");
34 		return (1);
35 	}
36 
37 	/* check FMP header */
38 	fread(header, 1, 12, fp);
39 	fsize = (header[4] << 24) +
40 			(header[5] << 16) +
41 			(header[6] <<  8) +
42 			 header[7] - 4;
43 
44 	if (strncmp(header, "FORM", 4) || strncmp(&header[8], "FMAP", 4)) {
45 		/* incorrect header */
46 		if (mode)
47 			fatal_error("Invalid FMP format!");
48 		fclose(fp);
49 		return(mode);
50 	}
51 
52 	/* define label */
53 	labldef(loccnt, 1);
54 
55 	/* output */
56 	if (pass == LAST_PASS)
57 		loadlc(loccnt, 0);
58 
59 	/* browse chunks */
60 	while (fsize > 0) {
61 		fread(header, 1, 8, fp);
62 		size = (header[4] << 24) +
63 			   (header[5] << 16) +
64 			   (header[6] <<  8) +
65 			    header[7];
66 		fsize -= 8;
67 		fsize -= size;
68 		if (fsize < 0)
69 			break;
70 
71 		/* BODY chunk */
72 		if (strncmp(header, "BODY", 4) == 0) {
73 			/* add size */
74 			cnt += (size >> 1);
75 
76 			if (pass == LAST_PASS) {
77 				/* read chunk */
78 				while (size) {
79 					/* read a block */
80 					nb = (size > 512) ? 512 : size;
81 					fread(buffer, 1, nb, fp);
82 					size -= nb;
83 					nb >>= 1;
84 
85 					/* convert word -> byte */
86 					for (i = 0, j = 0; i < nb; i++, j += 2)
87 						buffer[i] = ((buffer[j] + (buffer[j+1] << 8)) >> 5) - 1;
88 
89 					/* output buffer */
90 					putbuffer(buffer, nb);
91 				}
92 			}
93 			else
94 				putbuffer(NULL, size >> 1);
95 
96 			/* ok */
97 			break;
98 		}
99 		else {
100 			/* unsupported chunk */
101 			fseek(fp, size, SEEK_CUR);
102 		}
103 	}
104 
105 	/* close file */
106 	fclose(fp);
107 
108 	/* size */
109 	if (lablptr) {
110 		lablptr->data_type = P_INCBIN;
111 		lablptr->data_size = cnt;
112 	}
113 	else {
114 		if (lastlabl) {
115 			if (lastlabl->data_type == P_INCBIN)
116 				lastlabl->data_size += cnt;
117 		}
118 	}
119 
120 	/* output line */
121 	if (pass == LAST_PASS)
122 		println();
123 
124 	/* ok */
125 	return (1);
126 }
127 
128 
129 
130 
131 //The FMP file format
132 //The first 12 bytes are as follows:
133 //4bytes ASCII = 'FORM'
134 //long int = size of file less header (which is filesize-8)
135 //4bytes ASCII = 'FMAP'
136 //NOTE:  The chunk size long ints like the one above are stored in
137 //'Motorola' format, NOT Intel.  You will have to byteswap to get the
138 //correct value, ie:  Bytes 1,2,3,4 need to become 4,3,2,1.
139 //
140 //The chunks in the file follow on one after the other, and consist of
141 //an 8byte header, and the information specific to that chunk.  See
142 //how the playback source reads in the information.  The chunks can be
143 //in any order, and some chunks may not be used in a particular file.
144 //
145 //Chunk header:
146 //4bytes ASCII = ChunkID (example: 'MPHD')
147 //long int = size of chunk data less header
148 //
149 //These are the chunks as of V1.1:
150 //ATHR - Up to 4 ASCII strings of author information, separated by 0 values,
151 //       always an even size.
152 //MPHD - Map header, see struct in mappy.c
153 //EDHD - Editor information, see struct in mappy.c
154 //CMAP - Colour palette for 8bit maps, red byte, green byte,
155 //       blue byte for however many colours are needed (so usually 256*3 bytes).
156 //BKDT - Block data. Contains BLKSTR structures for however many block
157 //       structures were made.
158 //ANDT - Animation data. Contains ANISTR structures for however many
159 //       animation structures were made, and also animation data.
160 //BGFX - The raw graphics in whatever format the map is in.
161 //       Examples: 8bit: mapwidth*mapheight bytes per block,
162 //       in forward format *numblocks 16bit: mapwidth*mapheight*2
163 //       bytes per block, each word contains 5 bits red, 6 bits green,
164 //       5 bits blue.
165 //BODY - An array of short ints containing positive offsets into BKDT,
166 //       and negative offsets into ANDT.
167 //LYR? - Where ? is an ASCII number form 1 to 7.
168 //       These are the same size and format as BODY, and allow object
169 //       layers to be used.
170 
171