diff options
Diffstat (limited to 'map.c')
-rw-r--r-- | map.c | 97 |
1 files changed, 91 insertions, 6 deletions
@@ -1,10 +1,23 @@ #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 Object Object; +typedef struct Item Item; typedef struct Room Room; struct Entity @@ -13,18 +26,90 @@ struct Entity char *desc; }; -struct Object +struct Item { Entity; int type; double weight; - //Property props; }; struct Room { Entity; - Object *objects; - int nobject; - Room *neighbors[8]; + 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){ + + } + } +} |