aboutsummaryrefslogtreecommitdiff
path: root/src/room.h
blob: c0304c7f60f7a1251782ddac0242abc472928a49 (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
119
/*
 *   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"

/* Our world is an array of rooms, each having a list of objects in
 * them, as well as actions that can be performed in them
 * (verbs). Objects are added by hooks in rooms, which are provided as
 * function pointers by the world module. */

struct child_data;
struct object_t;
struct verb_t;

typedef enum room_id { ROOM_NONE = -1 } room_id;

enum direction_t { DIR_N = 0, DIR_NE, DIR_E, DIR_SE, DIR_S, DIR_SW, DIR_W, DIR_NW, DIR_UP, DIR_DN, DIR_IN, DIR_OT, NUM_DIRECTIONS };

/* the data we get from a world module */
struct roomdata_t {
    /* the non-const pointers can be modified by the world module */
    const char * const uniq_id;

    /* mutable properties */
    char *name;
    char *desc;

    /* unmutable properties, changes will have no effect */
    const char * const adjacent[NUM_DIRECTIONS];

    void (* const hook_init)(room_id id);

    /* return values indicate permission to enter/leave,
     * setting to NULL defaults to true.
     */

    /* NOTE: struct child_data is aliased as "user_t"!!! */

    /* return false to deny entering/leaving a room */
    bool (* const hook_enter)(room_id room, struct child_data *user);
    bool (* const hook_leave)(room_id room, struct child_data *user);
    void (* const hook_serialize)(room_id room, int fd);
    void (* const hook_deserialize)(room_id room, int fd);
    void (* const hook_destroy)(room_id room);
};

struct room_t {
    room_id id;
    struct roomdata_t data;

    room_id adjacent[NUM_DIRECTIONS];

    /* hash maps */
    void *objects; /* multimap of object name -> object */
    void *verbs;
    void *users; /* username -> child_data */

    void *userdata;
};

/* room/world */
bool room_user_add(room_id id, struct child_data *child);
bool room_user_del(room_id id, struct child_data *child);

/* On the first call, room should be a valid room id, and *save should
 * point to a void pointer. On subsequent calls, room should be
 * ROOM_NONE, and *save should remain unchanged from the previous
 * call. This call returns a LINKED LIST of objects with the same
 * name every time it is called, not individual objects. */
const struct multimap_list *room_obj_iterate(room_id room, void **save, size_t *n_pairs);

/* new should point to a new object allocated with obj_new(), with
 * 'name' properly set
 * returns true if the name is room-unique
 */
bool room_obj_add(room_id room, struct object_t *obj);

bool room_obj_add_alias(room_id room, struct object_t *obj, char *alias);

bool room_obj_del(room_id room, const char *name);
bool room_obj_del_alias(room_id room, struct object_t *obj, const char *alias);
bool room_obj_del_by_ptr(room_id room, struct object_t *obj);

const struct multimap_list *room_obj_get(room_id room, const char *obj);
const struct multimap_list *room_obj_get_size(room_id room, const char *name, size_t *n_objs);

size_t room_obj_count(room_id room);

/* local verbs override global verbs */
bool room_verb_add(room_id room, struct verb_t*);
bool room_verb_del(room_id room, const char *verbname);

/* get the local map of verbs */
void *room_verb_map(room_id room);

/* free a room and its resources */
void room_free(struct room_t *room);

/* semi-protected, should only be called from world_ */
void room_init_maps(struct room_t *room);

size_t room_obj_count_noalias(room_id id);