-
Notifications
You must be signed in to change notification settings - Fork 0
/
room.c
305 lines (254 loc) · 6.25 KB
/
room.c
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/*
* ISC License
*
* Copyright (c) 2021, Tommi Leino <namhas@gmail.com>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "room.h"
#include "player.h"
#include "object.h"
#include "message.h"
#include "tell.h"
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
struct desc
{
char *name;
char *text;
int tags;
size_t text_alloc;
};
#define DESC_CHUNK 4
#define LOC_CHUNK 32
#if 0
#define LOC_HASH_SIZE (65536 * 2)
#else
#define LOC_HASH_SIZE 8192
#endif
int nitems;
int nconflicts;
#include <math.h>
static size_t room_find_exit(struct room *room, const char *key);
/*
* For internal usage.
*/
static char *
simple_wrap(const char *p)
{
static char out[8192];
size_t i, j;
size_t len;
len = i = j = 0;
while (*p != '\0') {
len++;
out[i] = *p;
if (isspace(*p))
j = i;
if (len >= 65) {
p -= (i - j);
i = j;
out[i++] = '\n';
out[i++] = '\t';
len = 0;
p++;
}
out[i] = *p;
p++;
i++;
}
out[i] = '\0';
return out;
}
void
room_save(struct room *room, FILE *fp)
{
size_t i;
fprintf(fp, "goto %s\n", OBJ(room)->key);
for (i = 0; i < room->nexits; i++) {
fprintf(fp, "dig to:%s %s -\n", room->exit_targets[i],
room->exit_keys[i]);
}
fprintf(fp, "describe title <\n\t%s\n\t.\n",
simple_wrap(title(OBJ(room))));
for (i = 0; i < room->nexits; i++) {
fprintf(fp, "describe travel %s <\n\t%s\n\t.\n",
room->exit_keys[i], simple_wrap(travel_desc(room,
room->exit_keys[i])));
fprintf(fp, "describe exit %s <\n\t%s\n\t.\n",
room->exit_keys[i], simple_wrap(exit_desc(room,
room->exit_keys[i])));
}
fprintf(fp, "\n");
}
const char *
room_exit_target(struct room *room, const char *key)
{
size_t i;
i = room_find_exit(room, key);
if (i == MAX_EXITS)
return 0;
return room->exit_targets[i];
}
size_t
room_find_exit(struct room *room, const char *key)
{
size_t i;
for (i = 0; i < room->nexits; i++)
if (room->exit_keys[i] != NULL &&
strcmp(room->exit_keys[i], key) == 0)
break;
return i;
}
int
room_add_exit(struct room *room, const char *key, const char *target)
{
size_t i;
if (room->nexits == MAX_EXITS)
return -1;
if (room_find_exit(room, key) != room->nexits)
return -1;
i = room->nexits++;
if ((room->exit_keys[i] = strdup(key)) == NULL)
return -1;
if ((room->exit_targets[i] = strdup(target)) == NULL)
return -1;
tellrf(OBJ(room), NULL, "A way to %s appears leads to %s.", key,
target);
return 0;
}
int
room_remove_exit(struct room *room, const char *key)
{
size_t i;
if ((i = room_find_exit(room, key)) == room->nexits)
return -1;
room->nexits--;
if (room->nexits > i) {
if (room->exit_keys[i] != NULL)
free(room->exit_keys[i]);
if (room->exit_targets[i] != NULL)
free(room->exit_targets[i]);
memmove(&room->exit_keys[i], &room->exit_keys[i+1],
sizeof(char *) * (room->nexits - i));
memmove(&room->exit_targets[i], &room->exit_targets[i+1],
sizeof(char *) * (room->nexits - i));
memmove(&room->exit_travel_desc[i],
&room->exit_travel_desc[i+1],
sizeof(char *) * (room->nexits - i));
memmove(&room->exit_desc[i], &room->exit_desc[i+1],
sizeof(char *) * (room->nexits - i));
}
tellrf(OBJ(room), NULL, "A way to %s disappears.", key);
return 0;
}
struct room *
room_create(struct object *obj, const char *id)
{
obj->v.room = calloc(1, sizeof(struct room));
obj->v.room->object = obj;
return obj->v.room;
}
int
room_bytes(struct room *room)
{
int i, j, bytes;
bytes = sizeof(struct room);
for (i = 0; i < MAX_DESC_TYPES; i++) {
bytes += room->alloc_desc[i] * sizeof(struct desc);
for (j = 0; j < room->n_desc[i]; j++) {
bytes += room->desc[i][j].text_alloc;
}
}
return bytes;
}
void
room_free(struct room *room)
{
free(room);
}
const char *
room_desc(struct room *room, DescType type)
{
if (room->desc[type] == NULL)
return "nothing";
return room->desc[type][0].text;
}
void
add_desc(struct room *room, DescType type, const char *str)
{
struct desc *d;
char *p;
if (room->n_desc[type] == room->alloc_desc[type]) {
if (room->alloc_desc[type] == 0)
room->alloc_desc[type] = DESC_CHUNK;
else
room->alloc_desc[type] *= 2;
d = realloc(room->desc[type],
sizeof(struct desc) * room->alloc_desc[type]);
if (d == NULL) {
err(1, "realloc, tried +%lu bytes",
sizeof(struct desc) * room->alloc_desc[type]);
return;
}
room->desc[type] = d;
}
p = strdup(str);
if (p == NULL) {
err(1, "strdup");
return;
}
room->desc[type][room->n_desc[type]].text = p;
room->desc[type][room->n_desc[type]].text_alloc = strlen(str) + 1;
room->n_desc[type] = 0;
tellrf(OBJ(room), NULL, "Something here seems different.");
}
const char *
travel_desc(struct room *room, const char *dkey)
{
size_t i;
if ((i = room_find_exit(room, dkey)) == room->nexits)
return NULL;
return room->exit_travel_desc[i];
}
const char *
exit_desc(struct room *room, const char *dkey)
{
size_t i;
if ((i = room_find_exit(room, dkey)) == room->nexits)
return NULL;
return room->exit_desc[i];
}
void
set_travel_desc(struct room *room, const char *dkey, const char *str)
{
size_t i;
tellrf(OBJ(room), NULL, "Something changes in %s.", dkey);
if ((i = room_find_exit(room, dkey)) == room->nexits)
return;
if ((room->exit_travel_desc[i] = strdup(str)) == NULL)
return;
}
void
set_exit_desc(struct room *room, const char *dkey, const char *str)
{
size_t i;
tellrf(OBJ(room), NULL, "Something changes in %s.", dkey);
if ((i = room_find_exit(room, dkey)) == room->nexits)
return;
if ((room->exit_desc[i] = strdup(str)) == NULL)
return;
}