-
Notifications
You must be signed in to change notification settings - Fork 0
/
genobj.c
executable file
·450 lines (390 loc) · 13.2 KB
/
genobj.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
/************************************************************************
* Generic OLC Library - Objects / genobj.c v1.0 *
* Original author: Levork *
* Copyright 1996 by Harvey Gilpin *
* Copyright 1997-2001 by George Greer (greerga@circlemud.org) *
************************************************************************/
#include "conf.h"
#include "sysdep.h"
#include "structs.h"
#include "utils.h"
#include "constants.h"
#include "db.h"
#include "boards.h"
#include "shop.h"
#include "genolc.h"
#include "genobj.h"
#include "genzon.h"
#include "dg_olc.h"
static int copy_object_main(struct obj_data *to, struct obj_data *from, int free_object);
extern struct obj_data *obj_proto;
extern struct obj_data *object_list;
extern struct index_data *obj_index;
extern struct zone_data *zone_table;
extern struct board_info_type board_info[];
extern struct shop_data *shop_index;
extern zone_rnum top_of_zone_table;
extern obj_rnum top_of_objt;
extern int top_shop;
obj_rnum add_object(struct obj_data *newobj, obj_vnum ovnum)
{
int found = NOTHING;
zone_rnum rznum = real_zone_by_thing(ovnum);
/*
* Write object to internal tables.
*/
newobj->item_number = real_object(ovnum);
if (newobj->item_number != NOTHING) {
copy_object(&obj_proto[newobj->item_number], newobj);
update_objects(&obj_proto[newobj->item_number]);
add_to_save_list(zone_table[rznum].number, SL_OBJ);
return newobj->item_number;
}
found = insert_object(newobj, ovnum);
adjust_objects(found);
add_to_save_list(zone_table[rznum].number, SL_OBJ);
return found;
}
/* ------------------------------------------------------------------------------------------------------------------------------ */
/*
* Fix all existing objects to have these values.
* We need to run through each and every object currently in the
* game to see which ones are pointing to this prototype.
* if object is pointing to this prototype, then we need to replace it
* with the new one.
*/
int update_objects(struct obj_data *refobj)
{
struct obj_data *obj, swap;
int count = 0;
for (obj = object_list; obj; obj = obj->next) {
if (obj->item_number != refobj->item_number)
continue;
count++;
/* Update the existing object but save a copy for private information. */
swap = *obj;
*obj = *refobj;
/* Copy game-time dependent variables over. */
obj->in_room = swap.in_room;
obj->carried_by = swap.carried_by;
obj->worn_by = swap.worn_by;
obj->worn_on = swap.worn_on;
obj->in_obj = swap.in_obj;
obj->contains = swap.contains;
obj->next_content = swap.next_content;
obj->next = swap.next;
}
return count;
}
/* ------------------------------------------------------------------------------------------------------------------------------ */
/*
* Adjust the internal values of other objects as if something was
* inserted at the given array index.
* Might also be useful to make 'holes' in the array for some reason.
*/
int adjust_objects(obj_rnum refpt)
{
int shop, i, zone, cmd_no;
struct obj_data *obj;
if (refpt == NOTHING || refpt >= top_of_objt)
return refpt;
/*
* Renumber live objects.
*/
for (obj = object_list; obj; obj = obj->next)
GET_OBJ_RNUM(obj) += (GET_OBJ_RNUM(obj) >= refpt);
/*
* Renumber zone table.
*/
for (zone = 0; zone <= top_of_zone_table; zone++) {
for (cmd_no = 0; ZCMD(zone, cmd_no).command != 'S'; cmd_no++) {
switch (ZCMD(zone, cmd_no).command) {
case 'P':
ZCMD(zone, cmd_no).arg3 += (ZCMD(zone, cmd_no).arg3 >= refpt);
/*
* No break here - drop into next case.
*/
case 'O':
case 'G':
case 'E':
ZCMD(zone, cmd_no).arg1 += (ZCMD(zone, cmd_no).arg1 >= refpt);
break;
case 'R':
ZCMD(zone, cmd_no).arg2 += (ZCMD(zone, cmd_no).arg2 >= refpt);
break;
}
}
}
/*
* Renumber notice boards.
*/
for (i = 0; i < NUM_OF_BOARDS; i++)
BOARD_RNUM(i) += (BOARD_RNUM(i) >= refpt);
/*
* Renumber shop produce.
*/
for (shop = 0; shop <= top_shop - top_shop_offset; shop++)
for (i = 0; SHOP_PRODUCT(shop, i) != NOTHING; i++)
SHOP_PRODUCT(shop, i) += (SHOP_PRODUCT(shop, i) >= refpt);
return refpt;
}
/* ------------------------------------------------------------------------------------------------------------------------------ */
/*
* Function handle the insertion of an object within the prototype framework.
* Note that this does not adjust internal values
* of other objects, use add_object() for that.
*/
int insert_object(struct obj_data *obj, obj_vnum ovnum)
{
int i;
top_of_objt++;
RECREATE(obj_index, struct index_data, top_of_objt + 1);
RECREATE(obj_proto, struct obj_data, top_of_objt + 1);
/*
* Start counting through both tables.
*/
for (i = top_of_objt; i > 0; i--) {
/*
* Check if current virtual is bigger than our virtual number.
*/
if (ovnum > obj_index[i - 1].vnum)
return index_object(obj, ovnum, i);
/* Copy over the object that should be here. */
obj_index[i] = obj_index[i - 1];
obj_proto[i] = obj_proto[i - 1];
obj_proto[i].item_number = i;
}
/* Not found, place at 0. */
return index_object(obj, ovnum, 0);
}
/* ------------------------------------------------------------------------------------------------------------------------------ */
int index_object(struct obj_data *obj, obj_vnum ovnum, obj_rnum ornum)
{
if (obj == NULL || ovnum < 0 || ornum == NOTHING || ornum > top_of_objt)
return NOWHERE;
obj->item_number = ornum;
obj_index[ornum].vnum = ovnum;
obj_index[ornum].number = 0;
obj_index[ornum].func = NULL;
copy_object_preserve(&obj_proto[ornum], obj);
obj_proto[ornum].in_room = NOWHERE;
return ornum;
}
/* ------------------------------------------------------------------------------------------------------------------------------ */
/*
* NOTE: This clobbers all of the global variable strings. Do not
* save anything in them around calls to this function.
*
* buf, buf1, buf2, arg
*/
int save_objects(zone_rnum zone_num)
{
char buf[MAX_STRING_LENGTH];
char buf1[MAX_STRING_LENGTH];
char buf2[MAX_STRING_LENGTH];
char buf3[MAX_STRING_LENGTH];
char arg[MAX_STRING_LENGTH];
int counter, counter2, realcounter, i;
FILE *fp;
struct obj_data *obj;
struct extra_descr_data *ex_desc;
if (zone_num == NOTHING || zone_num > top_of_zone_table) {
log("SYSERR: GenOLC: save_objects: Invalid real zone number %d. (0-%d)", zone_num, top_of_zone_table);
return FALSE;
}
sprintf(buf, "%s/%d.new", OBJ_PREFIX, zone_table[zone_num].number);
if (!(fp = fopen(buf, "w+"))) {
mudlog(BRF, LVL_SAINT, TRUE, "SYSERR: OLC: Cannot open objects file!");
return FALSE;
}
/*
* Start running through all objects in this zone.
*/
for (counter = genolc_zone_bottom(zone_num); counter <= zone_table[zone_num].top; counter++) {
if ((realcounter = real_object(counter)) != NOTHING) {
if ((obj = &obj_proto[realcounter])->action_description) {
buf1[MAX_STRING_LENGTH - 1] = '\0';
strncpy(buf1, obj->action_description, MAX_STRING_LENGTH - 1);
strip_cr(buf1);
} else
*buf1 = '\0';
fprintf(fp,
"#%d\n"
"%s~\n"
"%s~\n"
"%s~\n"
"%s~\n",
GET_OBJ_VNUM(obj),
(obj->name && *obj->name) ? obj->name : STRING_UNDEFINED,
(obj->short_description && *obj->short_description) ? obj->short_description : STRING_UNDEFINED,
(obj->description && *obj->description) ? obj->description : STRING_UNDEFINED,
buf1);
sprintasciilong(buf1, GET_OBJ_EXTRA(obj));
sprintasciilong(buf3, GET_OBJ_CLASS(obj));
sprintascii(buf2, GET_OBJ_WEAR(obj));
sprintasciilong(arg, GET_OBJ_PERM(obj));
fprintf(fp,
"%d %s %s %s %s\n"
"%d %d %d %d %d\n"
"%d %d %d %d %d\n",
GET_OBJ_TYPE(obj), buf1, buf2, arg, buf3,
GET_OBJ_VAL(obj, 0), GET_OBJ_VAL(obj, 1), GET_OBJ_VAL(obj, 2), GET_OBJ_VAL(obj, 3), GET_OBJ_SIZE(obj),
GET_OBJ_WEIGHT(obj), GET_OBJ_COST(obj), GET_OBJ_RENT(obj), GET_OBJ_LEVEL(obj), GET_OBJ_MAX_LEVEL(obj)
);
/*
* Do we have script(s) attached ?
*/
script_save_to_disk(fp, obj, OBJ_TRIGGER);
/* save resists */
for (i = 0; i < MAX_ATTACK_TYPES; i++) {
if(obj->resist[i] == 1)
fprintf(fp, "R\n%d %d\n",i, obj->resist[i]);
}
for (i = 0; i < MAX_ATTACK_TYPES; i++) {
if(obj->vulnerable[i] == 1)
fprintf(fp, "V\n%d %d\n",i, obj->vulnerable[i]);
}
for (i = 0; i < MAX_ATTACK_TYPES; i++) {
if(obj->immune[i] == 1)
fprintf(fp, "I\n%d %d\n",i, obj->immune[i]);
}
/*
* Do we have extra descriptions?
*/
if (obj->ex_description) { /* Yes, save them too. */
for (ex_desc = obj->ex_description; ex_desc; ex_desc = ex_desc->next) {
/*
* Sanity check to prevent nasty protection faults.
*/
if (!ex_desc->keyword || !ex_desc->description || !*ex_desc->keyword || !*ex_desc->description) {
mudlog(BRF, LVL_SAINT, TRUE, "SYSERR: OLC: oedit_save_to_disk: Corrupt ex_desc!");
continue;
}
buf1[MAX_STRING_LENGTH - 1] = '\0';
strncpy(buf1, ex_desc->description, MAX_STRING_LENGTH - 1);
strip_cr(buf1);
fprintf(fp, "E\n"
"%s~\n"
"%s~\n", ex_desc->keyword, buf1);
}
}
/*
* Do we have affects?
*/
for (counter2 = 0; counter2 < MAX_OBJ_AFFECT; counter2++)
if (obj->affected[counter2].modifier)
fprintf(fp, "A\n"
"%d %d\n", obj->affected[counter2].location,
obj->affected[counter2].modifier);
}
}
/*
* Write the final line, close the file.
*/
fprintf(fp, "$~\n");
fclose(fp);
sprintf(buf2, "%s/%d.obj", OBJ_PREFIX, zone_table[zone_num].number);
remove(buf2);
rename(buf, buf2);
remove_from_save_list(zone_table[zone_num].number, SL_OBJ);
return TRUE;
}
/*
* Free all, unconditionally.
*/
void free_object_strings(struct obj_data *obj)
{
#if 0 /* Debugging, do not enable. */
extern struct obj_data *object_list;
struct obj_data *t;
int i = 0;
for (t = object_list; t; t = t->next) {
if (t == obj) {
i++;
continue;
}
assert(!obj->name || obj->name != t->name);
assert(!obj->description || obj->description != t->description);
assert(!obj->short_description || obj->short_description != t->short_description);
assert(!obj->action_description || obj->action_description != t->action_description);
assert(!obj->ex_description || obj->ex_description != t->ex_description);
}
assert(i <= 1);
#endif
if (obj->name)
free(obj->name);
if (obj->description)
free(obj->description);
if (obj->short_description)
free(obj->short_description);
if (obj->action_description)
free(obj->action_description);
if (obj->ex_description)
free_ex_descriptions(obj->ex_description);
}
/*
* For object instances that are not the prototype.
*/
void free_object_strings_proto(struct obj_data *obj)
{
int robj_num = GET_OBJ_RNUM(obj);
if (obj->name && obj->name != obj_proto[robj_num].name)
free(obj->name);
if (obj->description && obj->description != obj_proto[robj_num].description)
free(obj->description);
if (obj->short_description && obj->short_description != obj_proto[robj_num].short_description)
free(obj->short_description);
if (obj->action_description && obj->action_description != obj_proto[robj_num].action_description)
free(obj->action_description);
if (obj->ex_description) {
struct extra_descr_data *thised, *plist, *next_one; /* O(horrible) */
int ok_key, ok_desc, ok_item;
for (thised = obj->ex_description; thised; thised = next_one) {
next_one = thised->next;
for (ok_item = ok_key = ok_desc = 1, plist = obj_proto[robj_num].ex_description; plist; plist = plist->next) {
if (plist->keyword == thised->keyword)
ok_key = 0;
if (plist->description == thised->description)
ok_desc = 0;
if (plist == thised)
ok_item = 0;
}
if (thised->keyword && ok_key)
free(thised->keyword);
if (thised->description && ok_desc)
free(thised->description);
if (ok_item)
free(thised);
}
if (obj->proto_script && obj->proto_script != obj_proto[robj_num].proto_script)
free_proto_script(obj, OBJ_TRIGGER);
}
}
void copy_object_strings(struct obj_data *to, struct obj_data *from)
{
to->name = from->name ? strdup(from->name) : NULL;
to->description = from->description ? strdup(from->description) : NULL;
to->short_description = from->short_description ? strdup(from->short_description) : NULL;
to->action_description = from->action_description ? strdup(from->action_description) : NULL;
if (from->ex_description)
copy_ex_descriptions(&to->ex_description, from->ex_description);
else
to->ex_description = NULL;
if (SCRIPT(from))
script_copy(to, from, OBJ_TRIGGER);
}
int copy_object(struct obj_data *to, struct obj_data *from)
{
free_object_strings(to);
return copy_object_main(to, from, TRUE);
}
int copy_object_preserve(struct obj_data *to, struct obj_data *from)
{
return copy_object_main(to, from, FALSE);
}
static int copy_object_main(struct obj_data *to, struct obj_data *from, int free_object)
{
*to = *from;
copy_object_strings(to, from);
return TRUE;
}