Skip to content
This repository has been archived by the owner on Feb 7, 2020. It is now read-only.

Commit

Permalink
#44: created and am now enforcing new attributes for object, takeable…
Browse files Browse the repository at this point in the history
… and droppable
  • Loading branch information
James Colannino committed Dec 5, 2012
1 parent 974d4fa commit bc2db80
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 17 deletions.
12 changes: 12 additions & 0 deletions game.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ It appears to be a firey cylinder of wax burning with the passion of a thousand
<description>
It's nothing special. It's gray, bumpy and a little lopsided.
</description>

<!-- this optional tag allows you to turn off the ability for the
player to drop an object. An object is droppable by default (you
can achieve the same effect manually by setting this to 1.) -->
<droppable>0</droppable>

<synonym>stone</synonym>
</object>

Expand All @@ -61,6 +67,12 @@ It's nothing special. It's gray, bumpy and a little lopsided.
It's a huge rock. You're not sure if you'll be able to lift it.
</description>
<weight>1</weight>

<!-- This optional tag allows you to turn off the player's ability to
take an object. An object is takeable by default (you can
achieve the same effect manually by setting this to 1.)-->
<takeable>0</takeable>

<synonym>rock</synonym>
<synonym>stone</synonym>
</object>
Expand Down
64 changes: 53 additions & 11 deletions src/core/event.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ static int afterDisplayObject(Player *player, void *data);
(type Object *) */
static int takeObjectTooHeavy(Player *player, void *data);

/* triggered when the user tries to take an untakeable object
- data = object we're trying to pick up (type Object *) */
static int takeObjectUntakeable(Player *player, void *data);

/* triggered when the user tries to drop an undroppable object
- data = object we're trying to pick up (type Object *) */
static int dropObjectUndroppable(Player *player, void *data);

/* called before setting a new location - data = room we're setting (type Room *) */
static int beforeSetLocation(Player *player, void *data);

Expand All @@ -80,17 +88,19 @@ void initEvent() {

events = g_hash_table_new(g_str_hash, g_str_equal);

registerEvent("beforeRoomDisplay", &beforeRoomDisplay);
registerEvent("afterRoomDisplay", &afterRoomDisplay);
registerEvent("beforeSetLocation", &beforeSetLocation);
registerEvent("afterSetLocation", &afterSetLocation);
registerEvent("beforeTakeObject", &beforeTakeObject);
registerEvent("afterTakeObject", &afterTakeObject);
registerEvent("beforeDropObject", &beforeDropObject);
registerEvent("afterDropObject", &afterDropObject);
registerEvent("beforeDisplayObject", &beforeDisplayObject);
registerEvent("afterDisplayObject", &afterDisplayObject);
registerEvent("takeObjectTooHeavy", &takeObjectTooHeavy);
registerEvent("beforeRoomDisplay", &beforeRoomDisplay);
registerEvent("afterRoomDisplay", &afterRoomDisplay);
registerEvent("beforeSetLocation", &beforeSetLocation);
registerEvent("afterSetLocation", &afterSetLocation);
registerEvent("beforeTakeObject", &beforeTakeObject);
registerEvent("afterTakeObject", &afterTakeObject);
registerEvent("beforeDropObject", &beforeDropObject);
registerEvent("afterDropObject", &afterDropObject);
registerEvent("beforeDisplayObject", &beforeDisplayObject);
registerEvent("afterDisplayObject", &afterDisplayObject);
registerEvent("takeObjectTooHeavy", &takeObjectTooHeavy);
registerEvent("takeObjectUntakeable", &takeObjectUntakeable);
registerEvent("dropObjectUndroppable", &dropObjectUndroppable);
}

/******************************************************************************/
Expand Down Expand Up @@ -244,6 +254,38 @@ static int takeObjectTooHeavy(Player *player, void *data) {

/******************************************************************************/

static int takeObjectUntakeable(Player *player, void *data) {

// TODO
// TODO: should this call a global script function or a script function
// unique to the object? Hmm... I'm leaning toward global.

Object *object = data;
lua_State *L = object->lua;

callLuaEventHandler(L, "takeObjectUntakeable", dstrview(player->name),
dstrview(object->name), 0);
return ALLOW_ACTION;
}

/******************************************************************************/

static int dropObjectUndroppable(Player *player, void *data) {

// TODO
// TODO: should this call a global script function or a script function
// unique to the object? Hmm... I'm leaning toward global.

Object *object = data;
lua_State *L = object->lua;

callLuaEventHandler(L, "dropObjectUndroppable", dstrview(player->name),
dstrview(object->name), 0);
return ALLOW_ACTION;
}

/******************************************************************************/

static int callLuaEventHandler(lua_State *L, char *function, const char *player,
const char *thing, int before) {

Expand Down
2 changes: 2 additions & 0 deletions src/core/include/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ typedef struct object {
dstring_t name; /* name of the object */
dstring_t description; /* the user reads this when seen the first time */
int weight; /* object's weight */
int takeable; /* whether or not object can be taken */
int droppable; /* whether or not object can be dropped */

ObjectState state;

Expand Down
2 changes: 2 additions & 0 deletions src/core/include/parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ typedef struct objectParsed {
dstring_t name;
dstring_t description;
dstring_t weight;
dstring_t takeable;
dstring_t droppable;

/* an array of synonyms (dstring_t's) */
GArray *synonyms;
Expand Down
14 changes: 13 additions & 1 deletion src/core/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,13 @@ void takeObject(Player *player, Object *object) {
return;
}

if (0 == player->inventory.maxWeight ||
if (!object->takeable) {
event(player, "takeObjectUntakeable", object);
g_outputString("You can't take the %s.\n", dstrview(object->name));
return;
}

else if (0 == player->inventory.maxWeight ||
player->inventory.weight + object->weight <= player->inventory.maxWeight) {

transferObject(player, object, TAKE_OBJECT);
Expand Down Expand Up @@ -109,6 +115,12 @@ void dropObject(Player *player, Object *object) {
return;
}

if (!object->droppable) {
event(player, "dropObjectUndroppable", object);
g_outputString("You can't drop the %s.\n", dstrview(object->name));
return;
}

transferObject(player, object, DROP_OBJECT);

player->inventory.weight -= object->weight;
Expand Down
2 changes: 2 additions & 0 deletions src/core/parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,8 @@ static Object *initObject(ObjectParsed *objectParsed) {
object->name = objectParsed->name;
object->description = objectParsed->description;
object->weight = atoi(dstrview(objectParsed->weight));
object->takeable = atoi(dstrview(objectParsed->takeable));
object->droppable = atoi(dstrview(objectParsed->droppable));

object->synonyms = objectParsed->synonyms;

Expand Down
38 changes: 33 additions & 5 deletions src/core/parsexml.c
Original file line number Diff line number Diff line change
Expand Up @@ -234,14 +234,19 @@ static void parseObject(xmlTextReaderPtr reader) {

object->name = createDstring();
object->description = NULL;
object->weight = NULL;

if (DSTR_SUCCESS != dstralloc(&object->weight)) {
PRINT_OUT_OF_MEMORY_ERROR;
}
object->weight = createDstring();
object->takeable = createDstring();
object->droppable = createDstring();

/* by default, an object has no weight */
cstrtodstr(object->weight, "0");

/* by default, an object is takeable */
cstrtodstr(object->takeable, "1");

/* by default, an object is droppable */
cstrtodstr(object->droppable, "1");

object->synonyms = g_array_sized_new(FALSE, FALSE, sizeof(dstring_t), 5);
object->scripts = g_array_sized_new(FALSE, FALSE, sizeof(dstring_t), 2);

Expand Down Expand Up @@ -287,13 +292,36 @@ static void parseObject(xmlTextReaderPtr reader) {
/* we're parsing the object's weight (optional) */
else if (XML_ELEMENT_NODE == tagtype && 0 == strcmp("weight", tagname)) {
GET_XML_TAG(weight, object)
dstrtrim(object->weight);
if (!isInt((char *)dstrview(object->weight))) {
g_outputError("error: %s is not a valid weight\n",
dstrview(object->weight));
exit(EXIT_FAILURE);
}
}

/* we're parsing the object's "takeability" (optional) */
else if (XML_ELEMENT_NODE == tagtype && 0 == strcmp("takeable", tagname)) {
GET_XML_TAG(takeable, object)
dstrtrim(object->takeable);
if (strcmp(dstrview(object->takeable), "0") &&
strcmp(dstrview(object->takeable), "1")) {
g_outputError("error: takeable should be set to 1 (true) or 0 (false)\n");
exit(EXIT_FAILURE);
}
}

/* we're parsing the object's "droppability" (optional) */
else if (XML_ELEMENT_NODE == tagtype && 0 == strcmp("droppable", tagname)) {
GET_XML_TAG(droppable, object)
dstrtrim(object->droppable);
if (0 != strcmp(dstrview(object->droppable), "0") &&
0 != strcmp(dstrview(object->droppable), "1")) {
g_outputError("error: droppable should be set to 1 (true) or 0 (false)\n");
exit(EXIT_FAILURE);
}
}

/* we're parsing a synonym */
else if (XML_ELEMENT_NODE == tagtype && 0 == strcmp("synonym", tagname)) {

Expand Down

0 comments on commit bc2db80

Please sign in to comment.