File: main.c

package info (click to toggle)
liquidwar 5.6.3-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 15,960 kB
  • ctags: 2,583
  • sloc: ansic: 25,365; xml: 4,001; sh: 3,053; makefile: 1,353; asm: 1,344; python: 537; php: 486; sql: 22
file content (242 lines) | stat: -rw-r--r-- 6,846 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
/*
* A Random map generator for Liquid War.
*
* Copyright (C) 2003, David Redick, Christian Mauduit
* Released under the GNU General Public License (v2)
*/

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

#include "map.h"
#include "func.h"
#include "map_generator.h"


static char *filename = NULL;
static int size = DEFAULT_MAP_SIZE;
static int grid_size = RANDOM_MAP_GRID_SIZE;
static int func_id = MIN_FUNC;


void do_args(int argc, char **argv);
void print_help();
void print_version();

#ifdef DOS
/*
 * These macros reduce the size of the DOS executable
 */
BEGIN_GFX_DRIVER_LIST
END_GFX_DRIVER_LIST

BEGIN_COLOR_DEPTH_LIST
COLOR_DEPTH_8
END_COLOR_DEPTH_LIST

BEGIN_DIGI_DRIVER_LIST
END_DIGI_DRIVER_LIST

BEGIN_MIDI_DRIVER_LIST
END_MIDI_DRIVER_LIST

BEGIN_JOYSTICK_DRIVER_LIST
END_JOYSTICK_DRIVER_LIST
#endif

#ifdef WIN32
/*
 * Under Win32 we want a console executable so we don't use
 * the "END_OF_MAIN" stuff, and this implies to undef "main".
 */
#undef main
#endif

int main( int argc, char **argv )
{
     PALETTE pal;
     int i;


     /* init allegro and init palette */
     install_allegro(SYSTEM_NONE, &errno, atexit);
     set_color_depth(8);
     set_color_conversion(COLORCONV_REDUCE_TO_256);
     for( i = 0; i < 256; i++ )
     {
          /* divided by 4 because the colour value ranges from 0-63 */
          pal[i].r = pal[i].g = pal[i].b = i/4;
     }


     srand(time(NULL));

     do_args(argc, argv);

printf("filename = %s\n", filename);

     map_generator(filename, size, grid_size, func_id);

printf("map-- %s\n", map.filename);
     save_bitmap(map.filename, map.map, pal);
printf("map-- %s\n", map.filename);

return EXIT_SUCCESS;
}

#ifndef WIN32
END_OF_MAIN();
#endif

/*****************************************************************************/

void do_args( int argc, char **argv )
{
     int i;

     for( i = 1; i < argc; i++ )
     {
          if( strcmp(argv[i], "-o") == 0 || strcmp(argv[i], "--out") == 0 )
          {
               int len = strlen(argv[++i]);
               filename = malloc( sizeof(char)*len );
               if( filename == NULL )
               {
                    fprintf(stderr, "fatal error: can't malloc space for map name.\n");
                    exit(EXIT_FAILURE);
               }
               strcpy(filename, argv[i]);
          }
          else if( strcmp(argv[i], "-s") == 0 || strcmp(argv[i], "--size") == 0 )
          {
               size = atoi(argv[++i]);
               if( size < MIN_MAP_SIZE )
               {
                    fprintf(stderr, "map size too small using: %d\n", MIN_MAP_SIZE);
                    size = MIN_MAP_SIZE;
               }
               else if( size >= MAX_MAP_SIZE )
               {
                    fprintf(stderr, "map size too large using: %d\n", MAX_MAP_SIZE-1);
                    size = MAX_MAP_SIZE-1;
               }
          }
          else if( strcmp(argv[i], "-g") == 0 || strcmp(argv[i], "--grid") == 0 )
          {
               grid_size = atoi(argv[++i]);
               if( grid_size == RANDOM_MAP_GRID_SIZE )
               {
                    /* ok.. don't do anything */
               }
               else if( grid_size < MIN_MAP_GRID_SIZE )
               {
                    fprintf(stderr, "map grid too small using: %d\n", MIN_MAP_GRID_SIZE);
                    grid_size = MIN_MAP_GRID_SIZE;
               }
               else if( grid_size >= MAX_MAP_GRID_SIZE )
               {
                    fprintf(stderr, "map grid too large using: %d\n", MAX_MAP_GRID_SIZE-1);
                    grid_size = MAX_MAP_GRID_SIZE-1;
               }
          }
          else if( strcmp(argv[i], "-f") == 0 || strcmp(argv[i], "--function") == 0 )
          {
               int f;
               i++;
               for( f = 0; f < MAX_FUNC; f++ )
               {
                    if( strcmp(argv[i], func[f].name) == 0 )
                         break;
               }
               if( f >= MAX_FUNC )
               {
                    fprintf(stderr, "fatal error: can't find function: %s\n", argv[i]);
                    exit(EXIT_FAILURE);
               }
               else
                    func_id = f;
          }
          else if( strcmp(argv[i], "-l") == 0 || strcmp(argv[i], "--list") == 0 )
          {
               int f;
               printf("Random map generating functions:\n");
               for( f = 0; f < MAX_FUNC; f++ )
                    printf("%2d)  %s\t%s\n", f, func[f].name, func[f].desc);

               exit(EXIT_SUCCESS);
          }
          else if( strcmp(argv[i], "--help") == 0 )
          {
               print_version();
               print_help();
               exit(EXIT_SUCCESS);
          }
          else if( strcmp(argv[i], "--version") == 0 )
          {
               print_version();
               exit(EXIT_SUCCESS);
          }
          else
          {
               fprintf(stderr, "error: unknown arg: %s\n", argv[i]);
               fprintf(stderr, "ignoring...\n");
          }
     }

return;
}

/*****************************************************************************/

void print_help()
{
     int i;

     printf("\n");
     printf("-o    --out       <filename>        Save bitmap to <filename>.    [%s]\n",
          default_filename);
     printf("-s    --size      <%d-%d>             Bitmap size.                  [%d]\n",
          0, MAX_MAP_SIZE-1, DEFAULT_MAP_SIZE);
     printf("-g    --grid      <%d-%d>             Map Grid Size                 [0]\n",
          0, MAX_MAP_GRID_SIZE-1);
     printf("\n");
     printf("-f    --function  <function_name>   Which function to use.        [rand_func]\n");
     printf("-l    --list                        List all functions.\n");
     printf("\n");
     printf("      --help                        Print this help.\n");
     printf("      --version                     Print Version.\n");

     printf("\nMap Sizes (WxH):\n");
     for( i = MIN_MAP_SIZE; i < MAX_MAP_SIZE; i++ )
          printf("(%d)%dx%d  ", i,  map_size[i][0], map_size[i][1]);
     printf("\n");

     printf("\nGrid Sizes (RxC):\n");
     printf("(0) Random  ");
     for( i = MIN_MAP_GRID_SIZE; i < MAX_MAP_GRID_SIZE; i++ )
     {
          if( i > 0 && i%8 == 0 )
               printf("\n");
          printf("(%d)%dx%d  ", i, map_grid_size[i][0], map_grid_size[i][1]);
     }
     printf("\n");

return;
}

/*****************************************************************************/

void print_version()
{
     printf(
     "Liquid War Random Map Generator Version 0.0.1\n"
     "Copyright (C) 2003, David Redick, Chris Guirl, Christian Mauduit.\n"
     "Released under the GNU General Public License (v2).\n"
     );

return;
}

/*****************************************************************************/