File: load.c

package info (click to toggle)
xbattle 5.4.1-9
  • links: PTS
  • area: main
  • in suites: potato
  • size: 736 kB
  • ctags: 1,086
  • sloc: ansic: 9,102; sh: 845; makefile: 493
file content (336 lines) | stat: -rw-r--r-- 9,782 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
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
#include <stdio.h>

#include "constant.h"
  
/**** x include files ****/
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>
#include <X11/keysym.h>
#include <X11/keysymdef.h>

#if USE_LONGJMP
#include <setjmp.h>
#endif

#include "extern.h"


/******************************************************************************
  dump_board (filename, use_brief)

  Dump all relevant information about the current state of the game to
  the file <filename>.  If <use_brief>, just dump minimal terrain and
  side info (as generated by edit, for example). 
******************************************************************************/

dump_board (filename, use_brief)
  char filename[];
  int use_brief;
{
  int i, j;

  FILE *fp,
       *fopen();

  cell_type cell;

  if ((fp = fopen (filename, "w")) == NULL)
    throw_error ("Unable to open map file: %s", filename);

  /** Save the crucial parameters which determine compatibility **/

  fwrite (&Config->board_x_size,	sizeof(short), 1, fp);
  fwrite (&Config->board_y_size,	sizeof(short), 1, fp);
  fwrite (&Config->level_min,		sizeof(int), 1, fp);
  fwrite (&Config->level_max,		sizeof(int), 1, fp);
  fwrite (&Config->tile_type,		sizeof(int), 1, fp);
  fwrite (&Config->value_int[OPTION_CELL][0], sizeof(int), 1, fp);
  fwrite (&Config->side_count,		sizeof(short), 1, fp);
  if (use_brief)
    fprintf (fp, "B");
  else
    fprintf (fp, "F");

  /** Save each cell **/

  for (i=0; i<Board->cell_count; i++)
    dump_cell (CELL(i), Config->side_count,
			Config->direction_count, fp, use_brief);

  fclose (fp);
}



/******************************************************************************
  dump_cell (cell, side_count, direction_count, fp, use_brief)

  Dump all relevant information about <cell> to the dump file pointed to by
  <fp>, making sure to save only relevant information.  If <use_brief>,
  just dump essential terrain and side information.
******************************************************************************/

dump_cell (cell, side_count, direction_count, fp, use_brief)
  cell_type *cell;
  int side_count,
      direction_count;
  FILE *fp;
{
  int i,
      fixed_size;

  s_char dummy;

  if (use_brief)
  {
    fwrite (&cell->level,			sizeof(s_char), 1, fp);

    if (cell->side == SIDE_NONE && cell->angle == 0)
    {
      dummy = SIDE_VOID;
      fwrite (&dummy,				sizeof(s_char), 1, fp);
    }
    else
    {
      fwrite (&cell->side,			sizeof(s_char), 1, fp);
      if (cell->side != SIDE_NONE)
        fwrite (&(cell->value[cell->side]),	sizeof(s_char), 1, fp);

      fwrite (&cell->angle,			sizeof(short), 1, fp);
      if (cell->angle == ANGLE_FULL)
        fwrite (&cell->growth,			sizeof(s_char), 1, fp);
      else if (cell->angle > 0)
        fwrite (&cell->old_growth,		sizeof(s_char), 1, fp);
    }
  }
  else
  {
    /** Save the first part of the cell_type structure, which consists	**/
    /** of statically defined variables.				**/

    fixed_size = &(cell->old_value) - &(cell->level) + sizeof(char);
    fwrite (cell, fixed_size, 1, fp);

    /** Save the second part of the cell_type structure, which contains	**/
    /** dynamically allocated arrays.					**/

    fwrite (cell->dir,		sizeof(s_char), direction_count, fp);
    fwrite (cell->march,	sizeof(s_char), side_count, fp);
    fwrite (cell->march_dir,	sizeof(s_char), side_count, fp);
    fwrite (cell->march_type,	sizeof(s_char), side_count, fp);
    fwrite (cell->value,	sizeof(s_char), side_count, fp);
    fwrite (cell->seen,		sizeof(s_char), side_count, fp);
    fwrite (cell->draw_level,	sizeof(s_char), side_count, fp);
  }
}



/******************************************************************************
  load_board_header ()

  Load all relevant information about the current state of the game from
  the pointer <Config->fp>.
******************************************************************************/

load_board_header ()
{
  int i, j,
      side, player,
      cell_size;
  short side_count;
  char dummy;

  /** Load the set of header information **/

  fread (&Config->board_x_size,	sizeof(short), 1, Config->fp);
  fread (&Config->board_y_size,	sizeof(short), 1, Config->fp);
  fread (&Config->level_min,	sizeof(int), 1, Config->fp);
  fread (&Config->level_max,	sizeof(int), 1, Config->fp);
  fread (&Config->tile_type,	sizeof(int), 1, Config->fp);
  fread (&cell_size,		sizeof(int), 1, Config->fp);

  fread (&Config->load_side_count, sizeof(short), 1, Config->fp);

  fread (&dummy, sizeof(char), 1, Config->fp);
  if (dummy == 'B')
    Config->use_brief_load = TRUE;
  else
    Config->use_brief_load = FALSE;

  /** Correct any conflicting options **/

  if (Config->level_min < 0)
  {
    /** Set sea values if not set in command line **/

    if (!Config->enable_all[OPTION_SEA])
    {
      for (side=0; side<Config->side_count; side++)
        Config->value_double[OPTION_SEA][side] = 5;
    }

    Config->enable_all[OPTION_SEA] = TRUE;
    Config->enable_terrain = TRUE;
  }

  Config->value_int_all[OPTION_HILL_TONES] = Config->level_max+1;
  Config->value_int_all[OPTION_FOREST_TONES] = Config->level_max+1;
  Config->value_int_all[OPTION_SEA_TONES] = -Config->level_min;

  if (Config->level_max > 0)
  {
    /** Set default hill values if not set in command line **/

    if (!Config->enable_all[OPTION_HILLS])
    {
      for (side=0; side<Config->side_count; side++)
        Config->value_double[OPTION_HILLS][side] = 5.0*HILL_FACTOR;
    }

    if (!Config->enable_all[OPTION_HILLS] &&
                        !Config->enable_all[OPTION_FOREST])
      Config->enable_all[OPTION_HILLS] = TRUE;

    Config->enable_terrain = TRUE;
  }

  Config->fill_number = -Config->level_min;

  /** Set cell sizes for any side which didn't explicitly set it **/

  for (player=0; player<Config->player_count; player++)
  {
    side = Config->player_to_side[player];
    if (!Config->enable[OPTION_CELL][side])
    {
      Config->value_int[OPTION_CELL][side] = cell_size;
      Config->cell_size[side] = cell_size;
    }
  }  

  /** If side counts in file and game don't match, set OVERWRITE so	**/
  /** that troops are not loaded.					**/

  if (Config->side_count != Config->load_side_count)
    Config->enable_all[OPTION_OVERWRITE] = TRUE;
}



/******************************************************************************
  load_board (use_brief)

  Load each cell of the board from the already open file (from which the
  header has already been loaded).  If <use_brief>, load a succinct version
  of the board.
******************************************************************************/

load_board (use_brief)
  int use_brief;
{
  int i, j;

  cell_type *cell;

  /** Load each cell **/

  for (i=0; i<Board->cell_count; i++)
  {
    cell = CELL(i);
    load_cell (cell, Config->load_side_count,
		Config->direction_count, Config->fp, use_brief);

    j = 0;
  }

  fclose (Config->fp);
}



/******************************************************************************
  load_cell (cell, side_count, direction_count, fp, use_brief)

  Load all relevant information about <cell> from the dump file pointed to by
  <fp>, making sure to load only relevant information.  If <use_brief>, load
  a succinct version of the board.
******************************************************************************/

load_cell (cell, load_side_count, direction_count, fp, use_brief)
  cell_type *cell;
  int load_side_count,
      direction_count,
      use_brief;
  FILE *fp;
{
  int i,
      fixed_size,
      full_fixed_size;
  char buffer[1000];

  if (use_brief)
  {
    fread (&cell->level,			sizeof(s_char), 1, fp);
    fread (&cell->side,				sizeof(s_char), 1, fp);

    if (cell->side == SIDE_VOID)
      cell->side = SIDE_NONE;
    else
    {
      if (cell->side != SIDE_NONE)
        fread (&(cell->value[cell->side]),	sizeof(s_char), 1, fp);

      fread (&cell->angle,			sizeof(short), 1, fp);
      if (cell->angle == ANGLE_FULL)
      {
        fread (&cell->growth,			sizeof(s_char), 1, fp);
        cell->old_growth = cell->growth;
      }
      else if (cell->angle > 0)
      {
        fread (&cell->old_growth,		sizeof(s_char), 1, fp);
        cell->growth = 0;
      }
    }
  }
  else
  {
    /** Load the first part of the cell_type structure, which consists	**/
    /** of statically defined variables.				**/

    fixed_size = &(cell->side) - &(cell->level);
    fread (&(cell->level), fixed_size, 1, fp);

    fixed_size = &(cell->old_value) - &(cell->side) + sizeof(s_char);
    if (Config->enable_all[OPTION_OVERWRITE])
      fread (buffer, fixed_size, 1, fp);
    else
      fread (&(cell->side), fixed_size, 1, fp);

    /** Load the second part of the cell_type structure, which contains	**/
    /** dynamically allocated arrays.  Assume arrays already allocated.	**/

    if (Config->enable_all[OPTION_OVERWRITE])
    {
      fread (buffer, sizeof(s_char), direction_count, fp);
      fread (buffer, sizeof(s_char), load_side_count, fp);
      fread (buffer, sizeof(s_char), load_side_count, fp);
      fread (buffer, sizeof(s_char), load_side_count, fp);
      fread (buffer, sizeof(s_char), load_side_count, fp);
      fread (buffer, sizeof(s_char), load_side_count, fp);
      fread (buffer, sizeof(s_char), load_side_count, fp);
    }
    else
    {
      fread (cell->dir,		sizeof(s_char), direction_count, fp);
      fread (cell->march,	sizeof(s_char), load_side_count, fp);
      fread (cell->march_dir,	sizeof(s_char), load_side_count, fp);
      fread (cell->march_type,	sizeof(s_char), load_side_count, fp);
      fread (cell->value,	sizeof(s_char), load_side_count, fp);
      fread (cell->seen,	sizeof(s_char), load_side_count, fp);
      fread (cell->draw_level,	sizeof(s_char), load_side_count, fp);
    }
  }
}