aboutsummaryrefslogtreecommitdiff
path: root/src/obj.h
blob: 121e3ab2fd21d32081e87d84fadcf3b5905fd2f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
 *   NetCosm - a MUD server
 *   Copyright (C) 2016 Franklin Wei
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#pragma once

#include "globals.h"

#include "room.h"

/* Objects belong to an object class. Objects define their object
 * class through the class name, which is converted to a class ID
 * internally.
 */

typedef struct child_data user_t;

struct object_t;

struct obj_class_t {
    const char *class_name;

    /* write an object's user data to disk */
    void (*hook_serialize)(int fd, struct object_t*);

     /* read an object's user data */
    void (*hook_deserialize)(int fd, struct object_t*);

    /* called when an object is taken;
     * true = can take
     * false = can't take
     * no function (NULL) = can take */
    bool (*hook_take)(struct object_t*, user_t *user);

    bool (*hook_drop)(struct object_t*, user_t *user);
    void (*hook_destroy)(struct object_t*); // free resources
    const char* (*hook_desc)(struct object_t*, user_t*); // get object description

    void *(*hook_dupdata)(struct object_t *obj); // duplicate the userdata pointer
};

typedef uint64_t obj_id;

struct obj_alias_t {
    char *alias;
    struct obj_alias_t *next;
};

/* world modules should not instantiate this directly, use obj_new()
 * instead also, fields marked with 'protected' should not be modified
 * by the world module */
struct object_t {
    obj_id id; // protected

    /* the object name needs to be freeable with free(), and should
     * not be modified by the world module after being added to a
     * room */
    char *name;

    struct obj_class_t *class; // protected

    size_t n_alias;
    struct obj_alias_t *alias_list; // protected

    bool hidden; /* whether to list in room description */
    bool default_article; /* whether or not to use 'a' or 'an' when describing this */

    void *userdata;

    unsigned refcount; // protected
};

/* returns a new object of class 'c' */
struct object_t *obj_new(const char *c);

/* serialize an object */
void obj_write(int fd, struct object_t *obj);

/* deserialize an object */
struct object_t *obj_read(int fd);

/* this adds a reference to an object, DOES NOT COPY */
struct object_t *obj_dup(struct object_t *obj);

/* makes a new object with a new ID, but with the same data fields as
 * the original */
struct object_t *obj_copy(struct object_t *obj);

/* decrements an object's reference count; frees the object if there
 * are no more references to it */
void obj_free(void*);

/* shut down the obj_* module */
void obj_shutdown(void);

/* internal use */
obj_id obj_get_idcounter(void);
void obj_set_idcounter(obj_id);

/* compare two objects */
int obj_compare(const void *a, const void *b);

/* count the number of non-alias objects in the given multimap */
size_t obj_count_noalias(const void *multimap);