summaryrefslogtreecommitdiff
path: root/jeu-test/lemmings_level_designer_source/lem2tap.c
blob: 68c858ed1fb8e272c810824ede45ca16c2846773 (plain)
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
/* LEM2TAP.C: for converting a .LEM file and a .BMP file to a .TAP
   file for use with the Spectrum version of Lemmings
   Copyright (c) 1997,9 Philip Kendall

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

    Author contact information:

    E-mail: pak21@cam.ac.uk (until at least June 2002)
    Postal address: 15 Crescent Road, Wokingham, Berks, RG40 2DB, England

*/

#include <stdio.h>		/* standard I/O routines */
#include <stdlib.h>		/* various standard routines */
#include <string.h>		/* string handling routines */

#define BufLen 1024
#define NameLen 32
#define SkillNum 8
#define HazardNum 10
#define FNameLen 256
#define BMPLen 8192
#define LevelLen 8405

int ReadBMP(char *FName,char *BMPData);
void ReadLine(char *Buffer,FILE *f);
unsigned int ReadInt(FILE *f);
unsigned long ReadLong(FILE *f);

int main(int argc, char** argv)
{FILE *f;
 char Buffer[BufLen],Name[NameLen+1],FName[FNameLen],*BMPData,*LevelData,*ptr;
 unsigned Level,Rating,Number,Save,Rate,Time,Skills[SkillNum],StartX,StartY,
	  HomeX,HomeY,Colour,Hazard[HazardNum],HazardX[HazardNum],
	  HazardY[HazardNum],i,j;

 if(argc<3)
   {fprintf(stderr,"Usage: lem2tap filename.lem filename.tap\n"); return 2;}

 f=fopen(argv[1],"rt");
 if(ferror(f))
     {fprintf(stderr,"Error opening %s for input\n",argv[1]); return 1;}

 ReadLine(Buffer,f); sscanf(Buffer,"%u,%u",&Level,&Rating);
 ReadLine(Buffer,f); sscanf(Buffer,"%u,%u",&Number,&Save);
 ReadLine(Buffer,f); sscanf(Buffer,"%u",&Rate);
 ReadLine(Buffer,f); sscanf(Buffer,"%u",&Time);
 ReadLine(Buffer,f);
 sscanf(Buffer,"%u,%u,%u,%u,%u,%u,%u,%u",&Skills[0],&Skills[1],&Skills[2],
	&Skills[3],&Skills[4],&Skills[5],&Skills[6],&Skills[7]);
 memset(Buffer,' ',NameLen); /* Fill Buffer with spaces */
 ReadLine(Buffer,f); strncpy(Name,Buffer,NameLen); Name[NameLen]='\0';
 ReadLine(Buffer,f);
 sscanf(Buffer,"%u,%u,%u,%u",&StartX,&StartY,&HomeX,&HomeY);
 ReadLine(Buffer,f); sscanf(Buffer,"%u",&Colour);
 for(i=0;i<HazardNum;i++)
 {ReadLine(Buffer,f);
  sscanf(Buffer,"%u,%u,%u",&Hazard[i],&HazardX[i],&HazardY[i]);
 }
 ReadLine(Buffer,f); sscanf(Buffer,"%s",FName);

 fclose(f);

 BMPData=(char*)malloc(BMPLen);
 if(!BMPData)
     {fprintf(stderr,"Error allocating memory for BMPData\n"); return 3;}

 if(ReadBMP(FName,BMPData)) {free(BMPData); return 6;}

 LevelData=(char*)malloc(LevelLen);
 if(!LevelData)
     {fprintf(stderr,"Error allocating memory for LevelData\n");
      free(BMPData); return 5;
     }

 f=fopen(argv[2],"ab");
 if(ferror(f))
   {fprintf(stderr,"Error opening %s for output\n",argv[2]);
    free(BMPData); free(LevelData); return 4;
   }

 fputc(3,f); fputc(0,f);	/* Length of the header */
 fputc(0,f);			/* Header ID byte */
 fputc(Level,f);		/* Level number */
 fputc(Level,f);		/* Checksum */

 ptr=LevelData;
 *ptr++=Level;			/* Level number */
 *ptr++=Number;			/* Number of lemmings */
 *ptr++=Rate;			/* Initial/min release rate */
 *ptr++=StartX%0x100; *ptr++=StartX/0x100;
 *ptr++=StartY;			/* Trapdoor position */
 *ptr++=(Time-1);		/* (Time available/min)-1 */
 for(i=0;i<SkillNum;i++) *ptr++=Skills[i]; /* Skills available */
 *ptr++=Colour;			/* Level colours */
 *ptr++=Save;			/* Number to be saved */
 *ptr++=HomeX%0x100; *ptr++=HomeX/0x100;
 *ptr++=HomeY;			/* Home position */
 *ptr++=Rating;			/* Level rating */
 for(i=0;i<NameLen;i++) *ptr++=Name[i]; /* Level name */
 for(i=0;i<HazardNum;i++)	/* For each hazard: */
 {*ptr++=Hazard[i];		/* Hazard type */
  *ptr++=HazardX[i]%0x100; *ptr++=HazardX[i]/0x100;
  *ptr++=HazardY[i];		/* and position */
 }
 for(i=0;i<120;i++) *ptr++='\0'; /* blank space */

 /* finally, write out the graphics -- remember that the .BMP format has the
    origin at the lower left corner, but we need the origin at the upper left
 */
 for(i=0;i<128;i++)
   for(j=0;j<(512/8);j++)
     *ptr++=BMPData[((127-i)*(512/8))+j];

 /* Write out the prelims */
 fputc((LevelLen+2)%0x100,f); fputc((LevelLen+2)/0x100,f); /* Length */
 fputc(0xff,f);			/* Datablock ID byte */

 /* Calculate the checksum and write the level data out */
 j=0xff; for(i=0;i<LevelLen;i++)
   {j^=LevelData[i]; fputc(LevelData[i],f);}

 /* Finally, write out the checksum */
 fputc(j,f);

 /* Close the file */
 fclose(f);

 /* Release memory */
 free(BMPData); free(LevelData);

 /* and go home :-) */
 return 0;

}

int ReadBMP(char *FName,char *BMPData)
{FILE *f; unsigned i; unsigned long l;
 f=fopen(FName,"rb"); if(ferror(f))
     {fprintf(stderr,"Unable to open %s for input\n",FName); return 1;}

 i=ReadInt(f);
 if( i != 'B' + 'M'*0x100 )
     {fprintf(stderr,"Error: ID bytes are not \"BM\"\n");
      fclose(f); return 2;
     }
 l=ReadLong(f);
 if(l!=0x203e)
     {fprintf(stderr,"Error: File size stored in %s is incorrect\n"
		     "Expected: 203eh; Found: %lxh\n",FName,l);
      fclose(f); return 3;
     }
 l=ReadLong(f);
 if(l) fprintf(stderr,"Warning: reserved bytes are non-zero\n");
 l=ReadLong(f);
 if(l!=0x3e)
     {fprintf(stderr,"Error: Bitmap offset is incorrect\n"
		     "Expected: 3eh; Found: %lxh\n",l); fclose(f); return 4;}
 l=ReadLong(f);
 if(l!=40)
     {fprintf(stderr,"Error: Size of the bitmap information is wrong\n"
		     "Expected: 40; Found: %lu\n",l); fclose(f); return 5;}
 l=ReadLong(f);
 if(l!=0x200)
     {fprintf(stderr,"Error: Bitmap width is wrong\n"
		     "Expected: 0x200; Found: %lxh\n",l);
      fclose(f); return 6;
     }
 l=ReadLong(f);
 if(l!=0x80)
     {fprintf(stderr,"Error: Bitmap height is wrong\n"
		     "Expected: 0x80; Found: %lxh\n",l);
      fclose(f); return 7;
     }
 i=ReadInt(f);
 if(i!=1)
     {fprintf(stderr,"Error: Planes per pixel is wrong\n"
		     "Expected: 1; Found: %u\n",i); fclose(f); return 8;}
 i=ReadInt(f);
 if(i!=1)
     {fprintf(stderr,"Error: Bits per pixel is wrong\n"
		     "Expected: 1; Found: %u\n",i); fclose(f); return 9;}
 l=ReadLong(f);
 if(l)
     {fprintf(stderr,"Sorry -- Can't handle compressed images!\n");
      fclose(f); return 10;
     }
 l=ReadLong(f);
 if(l!=0x10000)
     {fprintf(stderr,"Error: Number of pixels in image is wrong\n"
		     "Expected: 0x10000; Found: %lxh\n",l);
      fclose(f); return 11;
     }
 fseek(f,8,SEEK_CUR);		/* Skip pixels per metre */
 l=ReadLong(f);
 if(l!=2)
     {fprintf(stderr,"Error: Number of colours used is wrong\n"
		     "Expected: 2; Found: %u\n",l); fclose(f); return 12;}
 l=ReadLong(f);
 if(l!=2)
     {fprintf(stderr,"Error: Number of important colours is wrong\n"
		     "Expected: 2; Found: %u\n",l); fclose(f); return 13;}
 fseek(f,8,SEEK_CUR);		/* Skip colours */
 fread(BMPData,BMPLen,1,f);	/* Finally, actually read the data... */

 fclose(f);

 return 0;
}

void ReadLine(char *Buffer,FILE *f)
{char *ptr=Buffer-1;
 while( !ferror(f) && !feof(f) )
     {*(++ptr)=fgetc(f); if(*ptr=='\n') break;}
 *ptr='\0';
}

unsigned ReadInt(FILE *f) {return fgetc(f)+fgetc(f)*0x100;}

unsigned long ReadLong(FILE *f)
{return fgetc(f)+fgetc(f)*0x100+fgetc(f)*0x10000+fgetc(f)*0x1000000;}