-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.c
98 lines (72 loc) · 2.2 KB
/
map.c
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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "main.h"
#include "map.h"
int read_mapfile(char filename[], MAP *map) {
FILE *mapFile = fopen(filename, "rb+");
if(mapFile == NULL) {
return PMT_ERROR_OPEN_MAP;
}
fread(&map->magic, sizeof(char), 2, mapFile);
fread(&map->version, sizeof(int), 1, mapFile);
fread(&map->name, sizeof(char), 255, mapFile);
fread(&map->sizeW, sizeof(int), 1, mapFile);
fread(&map->sizeH, sizeof(int), 1, mapFile);
fread(&map->hero, sizeof(int), 1, mapFile);
if(map->magic[0]!='m' && map->magic[1]!='f') {
return PMT_WRONG_MAP_FORMAT;
} else if(map->version!=PMT_VERSION) {
return PMT_WRONG_MAP_VERSION;
}
fread(&map->data, sizeof(char), map->sizeW*map->sizeH, mapFile);
fclose(mapFile);
return PMT_SUCCESS;
}
int write_mapfile(MAP *map, char filename[]) {
FILE *mapFile = fopen(filename, "wb");
if(mapFile == NULL) {
return PMT_ERROR_OPEN_MAP;
}
fwrite(&map->magic, sizeof(char), 2, mapFile);
fwrite(&map->version, sizeof(int), 1, mapFile);
fwrite(&map->name, sizeof(char), 255, mapFile);
fwrite(&map->sizeW, sizeof(int), 1, mapFile);
fwrite(&map->sizeH, sizeof(int), 1, mapFile);
fwrite(&map->hero, sizeof(int), 1, mapFile);
fwrite(map->data, sizeof(char), map->sizeW*map->sizeH, mapFile);
fclose(mapFile);
return PMT_SUCCESS;
}
int make_mapfile(char filename[], char name[], int sizeW, int sizeH) {
int totalSize = sizeW*sizeH;
if(totalSize<4) {
return PMT_WRONG_MAP_FORMAT;
}
FILE *mapFile = fopen(filename, "wb");
if(mapFile == NULL) {
return PMT_ERROR_OPEN_MAP;
}
MAP m;
m.magic[0] = 'm';
m.magic[1] = 'f';
m.version = 1;
strncpy(m.name, name, 255);
m.name[255] = '\0';
m.sizeW = sizeW;
m.sizeH = sizeH;
m.hero = sizeH * sizeW / 2 + sizeW / 2 - 1;
// Fill with empty chars
for(int i=0; i<totalSize; i++) {
m.data[i] = PMT_BLOCK_EMPTY;
}
fwrite(&m.magic, sizeof(char), 2, mapFile);
fwrite(&m.version, sizeof(int), 1, mapFile);
fwrite(&m.name, sizeof(char), 255, mapFile);
fwrite(&m.sizeW, sizeof(int), 1, mapFile);
fwrite(&m.sizeH, sizeof(int), 1, mapFile);
fwrite(&m.hero, sizeof(int), 1, mapFile);
fwrite(m.data, sizeof(char), totalSize, mapFile);
fclose(mapFile);
return PMT_SUCCESS;
}