Facebook
From Hot Pheasant, 5 Years ago, written in Plain Text.
This paste is a reply to Re: Untitled from Diminutive Echidna - go back
Embed
Viewing differences between Re: Untitled and Re: Re: Untitled
#include 
#include 
#include 
#include "object.h"
#include 
void Object_destroy(void *self)
struct Monster {
 Object *obj = self;
 if(obj) {
 if(obj->description) free(obj->description);
 free(obj);
 }
}
void Object_describe(void *self)
{
 Object *obj = self;
 printf("%s.\n", obj->description);
}
int Object_init(void *self)
{
 // do nothing really
 return 1;
}
void *Object_move(void *self, Direction direction)
{
 printf("You can't go that direction.\n");
 return NULL;
}
int Object_attack(void *self, int damage)
{
 printf("You can't attack that.\n");
 return 0;
}
void *Object_new(size_t size, Object proto, char *description)
{
 // setup the default functions in case they aren't set
 if(!proto.init) proto.init = Object_init;
 if(!proto.describe) proto.describe = Object_describe;
 if(!proto.destroy) proto.destroy = Object_destroy;
 if(!proto.destroy) proto.destroy = Object_destroy;
 if(!proto.attack) proto.attack = Object_attack;
 if(!proto.move) proto.move = Object_move;
 // this seems weird, but we can make a struct of one size,
 // then point a different pointer at it to "cast" it
 Object *el = calloc(1, size);
 *el = 
proto;
 // copy the description over
 el->description = strdup(description);
 // initialize it with whatever init we were given
 if(!el->init(el)) {
 // looks like it didn't initialize properly
 el->destroy(el);
 return NULL;
 } else {
 // all done, we made an object of any type
 return el;
 }
}typedef enum {
 NORTH, SOUTH, EAST, WEST
} Direction;
int hit_points;
};
typedef struct {
 char *description;
 
Monster Monster;
int (*init)(void *self);
 void (*describe)(void *self);
 void (*destroy)(void *self);
 void *(*move)(void *self, Direction direction);
 int (*attack)(void 
Monster_attack(void *self, int damage);
} Object;
int Object_init(void Monster_init(void *self);
struct Room {
 Object proto;
 Monster *bad_guy;
 struct Room *north;
 struct Room *south;
 struct Room *east;
 struct Room *west;
};
typedef struct Room Room;
void Object_destroy(void *self);
void Object_describe(void *self);
void *Object_move(void 
*Room_move(void *self, Direction direction);
int Object_attack(void Room_attack(void *self, int damage);
void *Object_new(size_t size, int Room_init(void *self);
struct Map {
 
Object proto, char *description);
#define NEW(T, N) Object_new(sizeof(T), T##Proto, N)
#define _(N) proto.N
proto;
 Room *start;
 Room *location;
};
typedef struct Map Map;
void *Map_move(void *self, Direction direction);
int Map_attack(void *self, int damage);
int Map_init(void *self);