-
Notifications
You must be signed in to change notification settings - Fork 23
/
dg_handler.c
executable file
·316 lines (273 loc) · 6.92 KB
/
dg_handler.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
306
307
308
309
310
311
312
313
314
315
316
/**************************************************************************
* File: dg_handler.c Part of LuminariMUD *
* Usage: Contains functions to handle memory for scripts. *
* *
* All rights reserved. See license for complete information. *
* *
* Death's Gate MUD is based on CircleMUD, Copyright (C) 1993, 94. *
* CircleMUD is based on DikuMUD, Copyright (C) 1990, 1991. *
* *
* $Author: Mark A. Heilpern/egreen/Welcor $ *
* $Date: 2004/10/11 12:07:00$ *
* $Revision: 1.0.14 $ *
***************************************************************************/
#include "conf.h"
#include "sysdep.h"
#include "structs.h"
#include "utils.h"
#include "dg_scripts.h"
#include "comm.h"
#include "db.h"
#include "handler.h"
#include "spells.h"
#include "dg_event.h"
#include "constants.h"
/* frees memory associated with var */
void free_var_el(struct trig_var_data *var)
{
if (var->name)
free(var->name);
if (var->value)
free(var->value);
free(var);
}
/* release memory allocated for a variable list */
void free_varlist(struct trig_var_data *vd)
{
struct trig_var_data *i, *j;
for (i = vd; i;)
{
j = i;
i = i->next;
free_var_el(j);
}
}
/* Remove var name from var_list. Returns 1 if found, else 0. */
int remove_var(struct trig_var_data **var_list, char *name)
{
struct trig_var_data *i, *j;
for (j = NULL, i = *var_list; i && str_cmp(name, i->name);
j = i, i = i->next)
;
if (i)
{
if (j)
{
j->next = i->next;
free_var_el(i);
}
else
{
*var_list = i->next;
free_var_el(i);
}
return 1;
}
return 0;
}
/* Return memory used by a trigger. The command list is free'd when changed and
* when shutting down. */
void free_trigger(struct trig_data *trig)
{
free(trig->name);
trig->name = NULL;
if (trig->arglist)
{
free(trig->arglist);
trig->arglist = NULL;
}
if (trig->var_list)
{
free_varlist(trig->var_list);
trig->var_list = NULL;
}
if (GET_TRIG_WAIT(trig))
event_cancel(GET_TRIG_WAIT(trig));
free(trig);
}
/* remove a single trigger from a mob/obj/room */
void extract_trigger(struct trig_data *trig)
{
struct trig_data *temp;
if (GET_TRIG_WAIT(trig))
{
event_cancel(GET_TRIG_WAIT(trig));
GET_TRIG_WAIT(trig) = NULL;
}
trig_index[trig->nr]->number--;
/* walk the trigger list and remove this one */
REMOVE_FROM_LIST(trig, trigger_list, next_in_world);
free_trigger(trig);
}
/* remove all triggers from a mob/obj/room */
void extract_script(void *thing, int type)
{
struct script_data *sc = NULL;
struct trig_data *trig = NULL, *next_trig = NULL;
char_data *mob = NULL;
obj_data *obj = NULL;
room_data *room = NULL;
switch (type)
{
case MOB_TRIGGER:
mob = (struct char_data *)thing;
sc = SCRIPT(mob);
SCRIPT(mob) = NULL;
break;
case OBJ_TRIGGER:
obj = (struct obj_data *)thing;
sc = SCRIPT(obj);
SCRIPT(obj) = NULL;
break;
case WLD_TRIGGER:
room = (struct room_data *)thing;
sc = SCRIPT(room);
SCRIPT(room) = NULL;
break;
}
/* zusuk disabled this debug 10/15/2017 */
#if 0 /* debugging */
{
struct char_data *i = character_list;
struct obj_data *j = object_list;
room_rnum k = 0;
if (sc) {
for ( ; i ; i = i->next)
assert(sc != SCRIPT(i));
for ( ; j ; j = j->next)
assert(sc != SCRIPT(j));
for (k = 0; k < top_of_world; k++)
assert(sc != SCRIPT(&world[k]));
}
}
#endif
for (trig = TRIGGERS(sc); trig; trig = next_trig)
{
next_trig = trig->next;
extract_trigger(trig);
}
TRIGGERS(sc) = NULL;
/* Thanks to James Long for tracking down this memory leak */
free_varlist(sc->global_vars);
free(sc);
}
/* erase the script memory of a mob */
void extract_script_mem(struct script_memory *sc)
{
struct script_memory *next;
while (sc)
{
next = sc->next;
if (sc->cmd)
free(sc->cmd);
free(sc);
sc = next;
}
}
void free_proto_script(void *thing, int type)
{
struct trig_proto_list *proto = NULL, *fproto = NULL;
char_data *mob = NULL;
obj_data *obj = NULL;
room_data *room = NULL;
switch (type)
{
case MOB_TRIGGER:
mob = (struct char_data *)thing;
proto = mob->proto_script;
mob->proto_script = NULL;
break;
case OBJ_TRIGGER:
obj = (struct obj_data *)thing;
proto = obj->proto_script;
obj->proto_script = NULL;
break;
case WLD_TRIGGER:
room = (struct room_data *)thing;
proto = room->proto_script;
room->proto_script = NULL;
break;
}
/* zusuk disabled this debug 10/15/2017 */
#if 0 /* debugging */
{
struct char_data *i = character_list;
struct obj_data *j = object_list;
room_rnum k;
if (proto) {
for ( ; i ; i = i->next)
assert(proto != i->proto_script);
for ( ; j ; j = j->next)
assert(proto != j->proto_script);
for (k = 0; k < top_of_world; k++)
assert(proto != world[k].proto_script);
}
}
#endif
while (proto)
{
fproto = proto;
proto = proto->next;
free(fproto);
}
}
void copy_proto_script(void *source, void *dest, int type)
{
struct trig_proto_list *tp_src = NULL, *tp_dst = NULL;
switch (type)
{
case MOB_TRIGGER:
tp_src = ((char_data *)source)->proto_script;
break;
case OBJ_TRIGGER:
tp_src = ((obj_data *)source)->proto_script;
break;
case WLD_TRIGGER:
tp_src = ((room_data *)source)->proto_script;
break;
}
if (tp_src)
{
CREATE(tp_dst, struct trig_proto_list, 1);
switch (type)
{
case MOB_TRIGGER:
((char_data *)dest)->proto_script = tp_dst;
break;
case OBJ_TRIGGER:
((obj_data *)dest)->proto_script = tp_dst;
break;
case WLD_TRIGGER:
((room_data *)dest)->proto_script = tp_dst;
break;
}
while (tp_src)
{
tp_dst->vnum = tp_src->vnum;
tp_src = tp_src->next;
if (tp_src)
CREATE(tp_dst->next, struct trig_proto_list, 1);
tp_dst = tp_dst->next;
}
}
}
void delete_variables(const char *charname)
{
char filename[MAX_PATH];
if (!get_filename(filename, sizeof(filename), SCRIPT_VARS_FILE, charname))
return;
if (remove(filename) < 0 && errno != ENOENT)
log("SYSERR: deleting variable file %s: %s", filename, strerror(errno));
}
void update_wait_events(struct room_data *to, struct room_data *from)
{
struct trig_data *trig;
if (!SCRIPT(from))
return;
for (trig = TRIGGERS(SCRIPT(from)); trig; trig = trig->next)
{
if (!GET_TRIG_WAIT(trig))
continue;
((struct wait_event_data *)GET_TRIG_WAIT(trig)->event_obj)->go = to;
}
}