aboutsummaryrefslogtreecommitdiff
path: root/src/room.c
blob: 58563a8abc8cdd25ef945aaac9a35b41046236a4 (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
120
121
122
123
124
125
126
127
128
/*
 *   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/>.
 */

#include "globals.h"

#include "hash.h"
#include "server.h"
#include "room.h"
#include "userdb.h"
#include "world.h"

bool room_user_add(room_id id, struct child_data *child)
{
    struct room_t *room = room_get(id);
    /* make sure there are no duplicates */

    if(!room)
        error("unknown room %d", id);

    if(child->user)
    {
        /* hash_insert returns NULL on success */
        return !hash_insert(room->users, child->user, child);
    }
    else
        return false;
}

bool room_user_del(room_id id, struct child_data *child)
{
    struct room_t *room = room_get(id);

    if(child->user)
        return hash_remove(room->users, child->user);
    else
        return false;
}

void room_free(struct room_t *room)
{
    hash_free(room->users);
    room->users = NULL;

    hash_free(room->objects);
    room->objects = NULL;

    hash_free(room->verbs);
    room->verbs = NULL;

    free(room->data.name);
    free(room->data.desc);
}

bool room_obj_add(room_id room, struct object_t *obj)
{
    return !hash_insert(room_get(room)->objects, obj->name, obj);
}

struct object_t *room_obj_iterate(room_id room, void **save)
{
    if(room != ROOM_NONE)
        return hash_iterate(room_get(room)->objects, save, NULL);
    else
        return hash_iterate(NULL, save, NULL);
}

struct object_t *room_obj_get(room_id room, const char *name)
{
    return hash_lookup(room_get(room)->objects, name);
}

size_t room_obj_count(room_id room)
{
    return hash_size(room_get(room)->objects);
}

bool room_obj_del(room_id room, const char *name)
{
    return hash_remove(room_get(room)->objects, name);
}

#define OBJMAP_SIZE 8
#define VERBMAP_SZ 8

/* initialize the room's hash tables */
void room_init_maps(struct room_t *room)
{
    room->users = hash_init((userdb_size() / 2) + 1, hash_djb, compare_strings);

    room->objects = hash_init(OBJMAP_SIZE, hash_djb, compare_strings);
    hash_setfreedata_cb(room->objects, obj_free);

    room->verbs = hash_init(VERBMAP_SZ,
                            hash_djb,
                            compare_strings);

    hash_setfreedata_cb(room->verbs, verb_free);
}

void *room_verb_map(room_id id)
{
    return room_get(id)->verbs;
}

bool room_verb_add(room_id id, struct verb_t *verb)
{
    return !hash_insert(room_get(id)->verbs, verb->name, verb);
}

bool room_verb_del(room_id id, const char *name)
{
    return hash_remove(room_get(id), name);
}