uchicago-cs / chiventure

A text adventure game engine developed in UChicago's CMSC 22000 - Introduction to Software Development
40 stars 13 forks source link

Design and implement new object store #813

Open borjasotomayor opened 3 years ago

borjasotomayor commented 3 years ago

Based on the experiences from the last two years, it has become clear that chiventure needs a unified object store where all the various components can store their data. This will largely involve the following:

dwahme commented 3 years ago

Below is a summary of the existing WDL++/WDZ documentation and some proposed updates/clarifications to how the directory structure is handled such that it can be cleanly coupled with a revamped libobj object store. The following is subject to updates and review

Existing WDL++/WDZ Format Documentation Summary

The following is a compilation of all documentation for WDL++

WDZ Sandbox

Source code from- https://github.com/uchicago-cs/chiventure/tree/dev/src/wdl/sandbox

Characteristics:

WDL++ Design Doc

Reference- https://github.com/uchicago-cs/chiventure/wiki/What-WDL---will-look-like

Characteristics:

WDL++/WDZ JSON Directory Structure Documentation

Reference- https://github.com/uchicago-cs/chiventure/wiki/WDL:-Looking-into-options-to-store-JSON-objects

Characteristics:

Proposed WDL++/WDZ Requirements (and Rules for Parsing into Object Store)

Characteristics

For determing how files in WDZ are translated into the object store, the following convention is proposed:

As an example, consider the following combination of files/folders

sample-game.wdz zip file structure

- DEFAULT.json
- ROOMS.json
- ITEMS/
    - DEFAULT.json
    - staff.json
- MEDIA/
    - creak.mp4

default.json

{
    "GAME":
        {
            "name": "sample-game"
        }
}

ROOMS.json

{
    "hall":
        {
            "name": "hall",
            "items": "wood-staff"
        }
}

ITEMS/default.json

{
    "orb":
        {
            "name": "big-orb"
        }
}

ITEMS/staff.json

{
    "name": "wood-staff"
    "sound": "creak.mp4"
}

The above WDZ file would then get parse into libobj 3.0 as the following:

GAME.name = sample-game
ROOMS.hall.name = hall
ROOMS.hall.items = wood-staff
ITEMS.orb.name = big-orb
ITEMS.staff.name = wood-staff
ITEMS.staff.sound = creak.mp4
dwahme commented 3 years ago

Libobj 3.0 API sketch

The following is a sample proposed API for libobj 3.0 that is capable of tightly coupling with a folder/file JSON storage format. Key components are:

For more examples of dot notation for object retrieval, this is based on the original libobj style. See https://github.com/uchicago-cs/chiventure/blob/libobj/src/libobj/examples/obj.c

// All datatypes that can be encoded in an obj
typedef enum type
{
    TYPE_ERROR = -1,
    TYPE_NONE  = 0,
    TYPE_BOOL  = 1,
    TYPE_CHAR  = 2,
    TYPE_INT   = 3,
    TYPE_STR   = 4,
    TYPE_LUA   = 5,
    TYPE_LIST  = 6,
} type_t;

/* A simple object model using strings as attribute identifiers */
typedef struct obj
{
    // The identifier of the object- must be unique for the parent object
    char id[MAXLEN_ID + 1];

    // The type of the data
    type_t type;

    // The data associated with the object
    union
    {
        bool b;
        char c;
        int  i;
        char *s;
        char *lua;
    } data;

    // The object's attributes (a hash table)
    struct obj *attr;

    // For if the obj is part of a list
    struct obj *next;
    struct obj *prev;

    // Required uthash identifier for making the hash table
    UT_hash_handle hh;

} obj_t;

// Lists are just objects using utlist
typedef obj_t obj_list_t;

// Object creation/deletion
obj_t *obj_new(char *id);
int obj_init(obj_t *obj, char *id);
int obj_free(obj_t *obj);
int obj_free_all(obj_t *obj);

// Getters
obj_t *obj_get_attr(obj_t *obj, char *id, bool create);
type_t obj_get_type(obj_t *obj, char *id);
bool   obj_get_bool(obj_t *obj, char *id);
char   obj_get_char(obj_t *obj, char *id);
int    obj_get_int (obj_t *obj, char *id);
char  *obj_get_str (obj_t *obj, char *id);
obj_list_t *obj_get_list(obj_t *obj, char *id);

// Setters
int obj_add_attr(obj_t *obj, char *id, obj_t *attr);
int obj_set_bool(obj_t *obj, char *id, bool value);
int obj_set_char(obj_t *obj, char *id, char value);
int obj_set_int (obj_t *obj, char *id, int value);
int obj_set_str (obj_t *obj, char *id, char *value);

It might be worthwhile to see if some fancy macro stuff could be done to clean up the function calls (like OBJ_GET(objstore, "ROOMS.hall.name", str))

dwahme commented 3 years ago

Notes from the first review of the above items:

WDL++/WDZ

Better way to designate object references:

"location":{
        "__ref":"object",
        "id":"ROOMS.purple room"
}

Libobj 3.0

dwahme commented 3 years ago

A sample wdz file has been produced incorporating the proposed changes. It can be found at: https://github.com/uchicago-cs/chiventure/tree/wdz_revamp/games/test_game

This is a translation of an older test game located at: https://github.com/uchicago-cs/chiventure/blob/dev/games/test_game.wdl