File: liquidwarmap.c

package info (click to toggle)
liquidwar 5.4.5-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 5,020 kB
  • ctags: 1,508
  • sloc: ansic: 13,836; sh: 2,488; xml: 2,270; asm: 1,349; makefile: 749; python: 443
file content (428 lines) | stat: -rw-r--r-- 11,732 bytes parent folder | download
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
419
420
421
422
423
424
425
426
427
428
/********************************************************************/
/*                                                                  */
/*            L   I  QQ  U U I DD    W   W  A  RR    555            */
/*            L   I Q  Q U U I D D   W   W A A R R   5              */
/*            L   I Q  Q U U I D D   W W W AAA RR    55             */
/*            L   I Q Q  U U I D D   WW WW A A R R     5            */
/*            LLL I  Q Q  U  I DD    W   W A A R R   55             */
/*                                                                  */
/*                             b                                    */
/*                             bb  y y                              */
/*                             b b yyy                              */
/*                             bb    y                              */
/*                                 yy                               */
/*                                                                  */
/*                     U U       FFF  O   O  TTT                    */
/*                     U U       F   O O O O  T                     */
/*                     U U TIRET FF  O O O O  T                     */
/*                     U U       F   O O O O  T                     */
/*                      U        F    O   O   T                     */
/*                                                                  */
/********************************************************************/

/********************************************************************/
/* this software is protected by the GPL, see copying.txt           */
/********************************************************************/

/********************************************************************/
/* nom           : liquidwarmap.c                                   */
/* contenu       : tools to generate tabs                           */
/* date de modif : 3 mai 98                                         */
/********************************************************************/

/*=================================================================*/
/* includes                                                        */
/*=================================================================*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <time.h>
#include <allegro.h>
#include <sys/stat.h>

/*=================================================================*/
/* variables globales                                              */
/*=================================================================*/

int   ARGC;
char  **ARGV;
int   FLAG_HELP=0;
int   FLAG_SILENT=0;
char  *FILENAMES[65536];
int   NUMBER_OF_FILES=0;
PALETTE PALETTE_SRC;
BITMAP *BITMAP_SRC=NULL;
BITMAP *BITMAP_DST=NULL;
signed char  BUFFER[1048576];
int   DST_X,DST_Y,DST_W,DST_H,DST_SIZE,BG_SIZE;
char READABLE_NAME[32];

/*=================================================================*/
/* fonctions                                                       */
/*=================================================================*/

/*-----------------------------------------------------------------*/
/* affichages d'aide                                               */
/*-----------------------------------------------------------------*/

/*-----------------------------------------------------------------*/
void display_common_help(void)
{
 printf ("liquidwarmap 1.0.0 by U-Foot. (ufoot@bigfoot.com)\n");
 printf ("This program creates maps for Liquid War.\n");
 printf ("It is free software, protected by the GPL.\n");
 printf ("It uses Allegro by Shawn Hargreaves.\n");
 printf ("\n");
}

/*-----------------------------------------------------------------*/
void display_short_help (void)
{
 display_common_help();
 printf ("Type \"liquidwarmap -?\" for more help.\n");
}

/*-----------------------------------------------------------------*/
void display_long_help (void)
{
 display_common_help();
 printf ("The source must be a 256 color bitmap.\n");
 printf ("Color 0=walls, the map must be contained in a closed rectangle.\n");
 printf ("Other colors ignored.\n");
 printf ("\n");
 printf ("Syntax:\n");
 printf ("liquidwarmap [options] filenames\n");
 printf ("\n");
 printf ("Options:\n");
 printf ("-? -h -H : displays this help.\n");
 printf ("-s -S :    silent mode, nothing written to the console.\n");
 printf ("\n");
 printf ("Remark: the created map will have a .map extension.\n");
 printf ("Warning: the source file will be replaced by a smaller file.\n");
}

/*-----------------------------------------------------------------*/
/* lecture des parametres de la ligne de commande                  */
/*-----------------------------------------------------------------*/

/*-----------------------------------------------------------------*/
int acknowledge_flag (char *str)
{
 int found=0;

 if (str[0]=='-' || str[0]=='/')
    {
     found=1;
     switch (str[1])
     {
      case '?':
      case 'h':
      case 'H': FLAG_HELP=1;
                break;
      case 's':
      case 'S': FLAG_SILENT=1;
                break;
      default : found=0;
     }
    }
 return found;
}

/*-----------------------------------------------------------------*/
int read_command_line (void)
{
 int      i,j=0,success=1;

 for (i=1; i<ARGC; ++i)
     {
      if ((!acknowledge_flag (ARGV[i])))
         {
             FILENAMES[j]=ARGV[i];
          j++;
         }
     }
 NUMBER_OF_FILES=j;
 if (NUMBER_OF_FILES<=0)
     {
      success=0;
      if (!FLAG_HELP)
         printf ("ERROR! Two few arguments.\n");
     }
 return success;
}

/*-----------------------------------------------------------------*/
/* acces disque                                                    */
/*-----------------------------------------------------------------*/

/*-----------------------------------------------------------------*/
int load_file (char *filename)
{
 int success;

 if (!FLAG_SILENT)
    printf ("Loading '%s'.\n", filename);

 if (BITMAP_SRC != NULL)
    destroy_bitmap (BITMAP_SRC);
 BITMAP_SRC=load_bitmap (filename, PALETTE_SRC);
 success=BITMAP_SRC!=NULL;
 if (!success)
    printf ("ERROR! Unable to read '%s'.\n", filename);
 return success;
}

/*-----------------------------------------------------------------*/
/* lecture des parametres du tableau                               */
/*-----------------------------------------------------------------*/

/*-----------------------------------------------------------------*/
void get_range (void)
{
 int min_x=BITMAP_SRC->w;
 int min_y=BITMAP_SRC->h;
 int max_x=-1;
 int max_y=-1;
 int x,y;

 for (y=0;y<BITMAP_SRC->h;++y)
     for (x=0;x<BITMAP_SRC->w;++x)
         if (!getpixel (BITMAP_SRC,x,y))
            {
             if (min_x>x)
                min_x=x;
             if (min_y>y)
                min_y=y;
             if (max_x<x)
                max_x=x;
             if (max_y<y)
                max_y=y;
            }
 DST_X=min_x;
 DST_Y=min_y;
 DST_W=max_x-min_x+1;
 DST_H=max_y-min_y+1;

 if (DST_W<1 || DST_H<1)
    {
     printf ("ERROR! Map is too small.\n");
     DST_W=DST_H=1;
    }
 if (BITMAP_DST!=NULL)
    destroy_bitmap (BITMAP_DST);
 BITMAP_DST=create_bitmap (DST_W,DST_H);
 blit (BITMAP_SRC, BITMAP_DST, DST_X, DST_Y, 0, 0, DST_W, DST_H);
}

/*-----------------------------------------------------------------*/
/* conversion dans le buffer                                       */
/*-----------------------------------------------------------------*/

/*-----------------------------------------------------------------*/
void convert_to_buffer (void)
{
 int pos_src,pos_dst,l,wh;
 char *data;

 wh=BITMAP_DST->h*BITMAP_DST->w;
 data=BITMAP_DST->dat;
 pos_src=pos_dst=0;
 BG_SIZE=0;

 while (pos_src<wh)
   {
   l=0;
   if (!data[pos_src])
      {
       while (pos_src<wh && (!data[pos_src]) && l<127)
         {
          l++;
          pos_src++;
         }
       BUFFER[pos_dst++]=l;
      }
   else
      {
       while (pos_src<wh && data[pos_src] && l<127)
         {
          l++;
          pos_src++;
         }
       BUFFER[pos_dst++]=-l;
       BG_SIZE+=l;
      }
   }
 BUFFER[pos_dst++]=0;

 DST_SIZE=pos_dst;
}

/*-----------------------------------------------------------------*/
void change_ext_to_txt (char *str)
{
  char *ext;

  ext=strchr (str, '.');
  ext[1]='t';
  ext[2]='x';
  ext[3]='t';
}

/*-----------------------------------------------------------------*/
void update_readable_name(char *str)
{
  FILE *f;
  char *search;

  memset(READABLE_NAME,0,sizeof(READABLE_NAME));

  f=fopen(str,"r");
  if (f!=NULL)
    {
      /*
       * If there's a corresponding .txt file, we open it and interpret
       * it as the name of the map
       */
      fread(READABLE_NAME,sizeof(READABLE_NAME),1,f);
      fclose(f);
      /*
       * Now we get rid of all the lines following the first one
       */
      search=strchr(READABLE_NAME,10);
      if (search)
	{
	  (*search)=0;
	}
      search=strchr(READABLE_NAME,13);
      if (search)
	{
	  (*search)=0;
	}
    }
  else
    {
      /*
       * No .txt file we just use the file name
       */
      strncpy(READABLE_NAME,str,sizeof(READABLE_NAME));
      search=strchr(READABLE_NAME,'.');
      if (search)
	{
	  (*search)='\0';
	}
    }
}

/*-----------------------------------------------------------------*/
/* on ecrit sur le disque                                          */
/*-----------------------------------------------------------------*/

/*-----------------------------------------------------------------*/
void change_ext_to_map (char *str)
{
  char *ext;

  ext=strchr (str, '.');
  ext[1]='m';
  ext[2]='a';
  ext[3]='p';
}

/*-----------------------------------------------------------------*/
void write_to_disk (char *str)
{
 FILE *f;
 short w,h;
 int size;

 if (!FLAG_SILENT)
    printf ("Writing %s to disk.\n",str);
 delete_file (str);
 f=fopen (str,"w");
 if (f!=NULL)
 {
    w=DST_W;
    h=DST_H;
    size=DST_SIZE;
    fwrite (&BG_SIZE,4,1,f);
    fwrite (&w,2,1,f);
    fwrite (&h,2,1,f);
    fwrite (READABLE_NAME,sizeof(READABLE_NAME),1,f);
    fwrite (BUFFER,DST_SIZE,1,f);
    fclose (f);
 }
 else
 {
  printf("Unable to write %s.\n",str);
 }
}

/*-----------------------------------------------------------------*/
void write_with_new_size (char *str)
{
 if (!FLAG_SILENT)
     printf ("Writing %s to disk.\n",str);
 delete_file (str);

 save_bitmap (str,BITMAP_DST,PALETTE_SRC);
}

/*=================================================================*/
/* fonctions main                                                  */
/*=================================================================*/

/*-----------------------------------------------------------------*/
int main (int argc, char **argv)
{
 int  i;

 ARGC=argc;
 ARGV=argv;

#ifdef ALLEGRO_UNIX
 install_allegro(SYSTEM_NONE, &errno, atexit);
#else
 allegro_init ();
#endif

 if (read_command_line ())
    {
     for (i=0; i<NUMBER_OF_FILES; ++i)
         {
          if (load_file (FILENAMES [i]))
             {
              get_range ();
	      if (BITMAP_SRC->w!=BITMAP_DST->w || BITMAP_SRC->h!=BITMAP_DST->h)
              	  write_with_new_size(FILENAMES [i]);
              convert_to_buffer ();
              change_ext_to_txt (FILENAMES [i]);
	      update_readable_name(FILENAMES [i]);
              change_ext_to_map (FILENAMES [i]);
              write_to_disk (FILENAMES [i]);
              }
        }
    }
 else
     {
      if (FLAG_HELP)
         display_long_help ();
      else
          display_short_help ();
     }
 allegro_exit ();

 return 0;

}

#ifdef ALLEGRO_WINDOWS
END_OF_MAIN()
#endif

#ifdef ALLEGRO_UNIX
END_OF_MAIN();
#endif