forked from jcpellegrini/jump
-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.c
161 lines (126 loc) · 2.75 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
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
/* Copyright (C) 2015 Time do Projeto Proex 10/2015.
This file is part of jump. It is free software and it is covered
by the Affero GNU general public license, version 3.
See the file LICENSE for details.
*/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "error.h"
#include "map.h"
#include "player.h"
#include "item.h"
#include "command.h"
#define SPACE ' '
#define WALL '#'
void map_destroy(map m) {
}
/**
**/
void map_seralize (char *out, map m) {
}
/**
m will be created and allocated by this function.
**/
void map_deserialize(map m, char *in) {
}
void get_map_size(int *rows, int *cols, FILE *in) {
int j;
char c;
assert(NULL != in);
*rows = 0;
*cols = 0;
j = 0; /* current column */
c = (char) fgetc(in);
while (EOF!=c) {
j++;
if (c == '\n') {
(*rows)++;
/* set cols to maximum line size */
if (j > (*cols))
*cols = j-1;
j = 0;
}
c = (char) fgetc(in);
}
rewind(in);
}
map map_create(int rows, int cols) {
int i;
map m = malloc(sizeof(struct map_str));
if (NULL==m)
error(MAP_READ_ERROR, "map_read: weird -- not enough memory to allocate map dimension (2 unsigned ints!)");
m->matrix = (char**) malloc (sizeof(char*) * rows);
if (NULL==m->matrix)
error(MAP_READ_ERROR, "map_read: not enough memory to allocate map (%u x %u)",rows,cols);
for(i=0; i<rows; i++) {
m->matrix[i] = (char *)malloc(sizeof(char)*cols);
if (NULL==m->matrix[i])
error(MAP_READ_ERROR, "map_read: not enough memory to allocate map (%u x %u)",rows,cols);
}
m->rows = rows;
m->cols = cols;
return m;
}
map map_read(char *filename) {
int i;
int j;
int k;
int rows;
int cols;
char c;
FILE *in;
map m;
in = fopen(filename,"r");
if (NULL==in)
error(MAP_READ_ERROR, "could not read file \"%s\"", filename);
/* FIXME: this is unsafe (string may not be what we think it is) */
printf("reading %s...\n",filename);
get_map_size(&rows,&cols,in);
printf("map has %d rows and %d columns.\n",rows, cols);
m = map_create(rows, cols);
c = fgetc(in);
for (i=0; i<rows; i++) {
j = 0;
while ( c != '\n' ) {
m->matrix[i][j] = c;
c = fgetc(in);
j++;
}
c = fgetc(in);
while (j<cols) {
m->matrix[i][j] = ' ';
j++;
}
}
fclose(in);
return m;
}
void map_write(map m, char *filename) {
int i;
int j;
FILE *out;
out = fopen(filename, "w");
if (NULL==out)
error(MAP_WRITE_ERROR, "cannot open file \"%s\" for writing\n", filename);
assert(NULL != m);
assert(NULL != m->matrix);
for (i=0; i< m->rows; i++) {
for(j=0; j<m->cols; j++)
fprintf(out, "%c", m->matrix[i][j]);
fprintf(out,"\n");
}
fclose(out);
}
int map_move_player(map m, player p, command c) {
switch (c) {
case NORTH:
break;
case SOUTH:
break;
case EAST:
break;
case WEST:
break;
}
}