Newer
Older
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
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include "dungeon.h"
#include "save_load.h"
FILE* open_save_file(char* mode) {
char *home = getenv("HOME");
char *path;
path = malloc(strlen(home) + strlen(FILENAME) + 1);
strcpy(path, home);
strcat(path, FILENAME);
FILE* f = fopen(path, mode);
if (!f) {
fprintf(stderr, "Error: Cannot open file: %s\n", path);
free(path);
exit(-1);
}
free(path);
return f;
}
void read_short(FILE* f, uint16_t* val) {
uint16_t tmp;
fread(&tmp, 2, 1, f);
*val = be16toh(tmp);
}
void read_int(FILE* f, uint32_t* val) {
uint32_t tmp;
fread(&tmp, 4, 1, f);
*val = be32toh(tmp);
}
void write_short(FILE* f, uint16_t val) {
uint16_t tmp = val;
tmp = htobe16(tmp);
fwrite(&tmp, 2, 1, f);
}
void write_int(FILE* f, uint32_t val) {
uint32_t tmp = val;
tmp = htobe32(tmp);
fwrite(&tmp, 4, 1, f);
}
void load_dungeon(dungeon_t* d) {
int i;
FILE* f = open_save_file("r");
// Skip the file type marker, version, and filesize
fseek(f, 20, SEEK_CUR);
// Read the player position
fread(&(d->player_pos.x), 1, 1, f);
fread(&(d->player_pos.y), 1, 1, f);
// Read the hardness map
fread(d->hardness, 1, MAP_WIDTH * MAP_HEIGHT, f);
// Read the rooms
read_short(f, &(d->room_count));
d->room_list = malloc(d->room_count * sizeof(room_t));
for (i = 0; i < d->room_count; i++) {
fread(&(d->room_list[i].x), 1, 1, f);
fread(&(d->room_list[i].y), 1, 1, f);
fread(&(d->room_list[i].w), 1, 1, f);
fread(&(d->room_list[i].h), 1, 1, f);
}
// Read the up staircases
read_short(f, &(d->upstair_count));
d->upstair_list = malloc(d->upstair_count * sizeof(position_t));
for (i = 0; i < d->upstair_count; i++) {
fread(&(d->upstair_list[i].x), 1, 1, f);
fread(&(d->upstair_list[i].y), 1, 1, f);
}
// Read the down staircases
read_short(f, &(d->downstair_count));
d->downstair_list = malloc(d->downstair_count * sizeof(position_t));
for (i = 0; i < d->downstair_count; i++) {
fread(&(d->downstair_list[i].x), 1, 1, f);
fread(&(d->downstair_list[i].y), 1, 1, f);
}
fclose(f);
}
void save_dungeon(dungeon_t* d) {
int i;
FILE* f = open_save_file("w");
// Write the file-type maker
fprintf(f, "%s", FILETYPE_MARKER);
// Write the version number
write_int(f, VERSION);
// Write the filesize
write_int(f, 1708 + (d->room_count*4) + (d->downstair_count*2) + (d->upstair_count*2));
// Write the player position
fwrite(&(d->player_pos.x), 1, 1, f);
fwrite(&(d->player_pos.y), 1, 1, f);
// Write the hardness map
fwrite(d->hardness, 1, MAP_WIDTH * MAP_HEIGHT, f);
// Write the rooms
write_short(f, d->room_count);
for (i = 0; i < d->room_count; i++) {
fwrite(&(d->room_list[i].x), 1, 1, f);
fwrite(&(d->room_list[i].y), 1, 1, f);
fwrite(&(d->room_list[i].w), 1, 1, f);
fwrite(&(d->room_list[i].h), 1, 1, f);
}
// Write the up staircases
write_short(f, d->upstair_count);
for (i = 0; i < d->upstair_count; i++) {
fwrite(&(d->upstair_list[i].x), 1, 1, f);
fwrite(&(d->upstair_list[i].y), 1, 1, f);
}
// Read the down staircases
write_short(f, d->downstair_count);
for (i = 0; i < d->downstair_count; i++) {
fwrite(&(d->downstair_list[i].x), 1, 1, f);
fwrite(&(d->downstair_list[i].y), 1, 1, f);
}
fclose(f);
}