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
|
#include <u.h>
#include <libc.h>
#include <bio.h>
#include "dat.h"
#include "fns.h"
enum {
RN, /* tramontana */
RNE, /* greco */
RE, /* levante */
RSE, /* scirocco */
RS, /* ostro */
RSW, /* libeccio */
RW, /* ponente */
RNW, /* maestro */
NROOMS
};
typedef struct Entity Entity;
typedef struct Item Item;
typedef struct Room Room;
struct Entity
{
int id;
char *desc;
};
struct Item
{
Entity;
int type;
double weight;
};
struct Room
{
Entity;
Item *items;
int nitems;
Room *neighbors[NROOMS];
};
Room *rooms;
int nrooms;
void*
emalloc(ulong n)
{
void *p;
p = malloc(n);
if(p == nil)
sysfatal("malloc: %r");
memset(p, 0, n);
setmalloctag(p, getcallerpc(&n));
return p;
}
void*
erealloc(void *p, ulong n)
{
p = realloc(p, n);
if(p == nil)
sysfatal("realloc: %r");
setrealloctag(p, getcallerpc(&n));
return p;
}
int
readrooms(char *file)
{
Biobuf *bin;
Room *r;
char *line, k[128], v[128], *p;
bin = Bopen(file, OREAD);
if(bin == nil)
sysfatal("Bopen: %r");
rooms = erealloc(rooms, ++nrooms*sizeof(Room));
r = &rooms[nrooms-1];
while((line = Brdline(bin, '\n')) != nil){
if(Blinelen(bin) == 1 && *line == '\n'){
rooms = erealloc(rooms, ++nrooms+sizeof(Room));
r = &rooms[nrooms-1];
}
while(isspace(*line))
line++;
p = k;
while(isalnum(*line) && p < k + sizeof k - 1)
*p++ = *line++;
*p = 0;
while(isspace(*line))
line++;
if(*line != '='){
werrstr("syntax error: expected '=', got '%c'", *line);
return -1;
}
while(isspace(*line))
line++;
p = v;
while(*line != '\n' && *line != '\0' && p < v + sizeof v - 1)
*p++ = *line++;
*p = 0;
if(strcmp(k, "id") == 0)
r->id = strtol(v, nil, 10);
else if(strcmp(k, "description") == 0)
r->desc = strdup(v);
else if(strcmp(k, "rooms") == 0){
}
}
}
|