#include #include #include #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){ } } }