Debian Codegraph @

src/hardness_tiles.cpp

raw
1 /**
2 * Hardness tileset for game map
3
4 * Copyright (C) 1997, 1998, 1999, 2002, 2003 Seth A. Robinson
5 * Copyright (C) 2005, 2007, 2008, 2009, 2010, 2012, 2014, 2015 Sylvain Beucler
6
7 * This file is part of GNU FreeDink
8
9 * GNU FreeDink is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 3 of the
12 * License, or (at your option) any later version.
13
14 * GNU FreeDink is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see
21 * <http://www.gnu.org/licenses/>.
22 */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include "hardness_tiles.h"
29 #include "EditorMap.h"
30 #include "paths.h"
31 #include "log.h"
32
33 /* HWND hWndMain = NULL; */
34 struct hardness hmap;
35
36 char current_hard[50] = "hard.dat";
37
38 /***
39 * Saves hard.dat (only used from the editor)
40 */
41 void save_hard(void)
42 {
43 char skipbuf[10000]; // more than any fseek we do
44 memset(skipbuf, 0, 10000);
45
46 FILE *f = paths_dmodfile_fopen(current_hard, "wb");
47 if (!f)
48 {
49 perror("Couldn't save hard.dat");
50 return;
51 }
52
53 /* Portably dump struct hardness hmap to disk */
54 int i = 0;
55 for (i = 0; i < HARDNESS_NB_TILES; i++)
56 {
57 for (int x = 0; x < 50+1; x++)
58 for (int y = 0; y < 50+1; y++)
59 fwrite(&hmap.htile[i].hm[x][y], 1, 1, f);
60 fputc(hmap.htile[i].used, f);
61 fwrite(skipbuf, 2, 1, f); // reproduce memory alignment
62 fwrite(skipbuf, 4, 1, f); // unused 'hold' field
63 }
64 for (i = 0; i < GFX_TILES_NB_SQUARES; i++)
65 write_lsb_int(hmap.btile_default[i], f);
66 fseek(f, (8000-GFX_TILES_NB_SQUARES)*4, SEEK_CUR); // reproduce unused data
67
68 fclose(f);
69 }
70
71
72 /**
73 * Load hard.dat which contains tile hardness information.
74 *
75 * Unlike 1.08, don't reset and save hard.dat during game in case
76 * e.g. it was just being written by an external editor.
77 */
78 void load_hard(void)
79 {
80 memset(&hmap, 0, sizeof(struct hardness));
81
82 char skipbuf[10000]; // more than any fseek we do
83
84 FILE *f = NULL;
85
86 /* Try loading the D-Mod hard.dat */
87 f = paths_dmodfile_fopen(current_hard, "rb");
88
89 /* Fallback to the default hard.dat */
90 if (f == NULL)
91 f = paths_fallbackfile_fopen(current_hard, "rb");
92
93 if (f == NULL)
94 {
95 log_error("Did not find existing hard.dat, using empty data.");
96 return;
97 }
98
99 /* Portably load struct hardness hmap from disk */
100 int i = 0;
101 for (i = 0; i < HARDNESS_NB_TILES; i++)
102 {
103 for (int x = 0; x < 50+1; x++)
104 for (int y = 0; y < 50+1; y++)
105 fread(&hmap.htile[i].hm[x][y], 1, 1, f);
106 hmap.htile[i].used = fgetc(f);
107 fread(skipbuf, 2, 1, f); // reproduce memory alignment
108 fread(skipbuf, 4, 1, f); // unused 'hold' field
109 }
110 for (i = 0; i < GFX_TILES_NB_SQUARES; i++)
111 hmap.btile_default[i] = read_lsb_int(f);
112 fseek(f, (8000-GFX_TILES_NB_SQUARES)*4, SEEK_CUR); // reproduce unused data
113
114 fclose(f);
115 }