/
consistency.c
359 lines (311 loc) · 10.6 KB
/
consistency.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
/*
$Id: consistency.c,v 1.1 2005/09/24 09:48:53 ssim Exp $
$Log: consistency.c,v $
Revision 1.1 2005/09/24 09:48:53 ssim
Initial revision
Revision 1.2 2003/10/13 14:12:01 sam
Added RCS tags
*/
#include <stdio.h>
#include <string.h>
#include "server.h"
#include "log.h"
#include "mem.h"
#include "container.h"
#include "create.h"
#include "talk.h"
#include "consistency.h"
static unsigned char *item_used=NULL;
int consistency_check_items(void)
{
int in,n,cn,m,ct,err=0;
for (in=1; in<MAXITEM; in++) {
if (!it[in].flags) continue;
if (it[in].flags&IF_VOID) continue;
// check if the item is valid (just one check so far:)
// check containers:
if ((ct=it[in].content)) {
if (ct<1 || ct>=MAXCONTAINER) {
elog("item %s (%d) thinks its content is container %d but that is out of bounds. fixing.",it[in].name,in,it[in].content);
elog_item(in);
it[in].content=0;
continue;
}
if (con[ct].in!=in) {
elog("item %s (%d) thinks its content is container %d but that container has no link back. fixing.",it[in].name,in,it[in].content);
elog_item(in);
it[in].content=0;
continue;
}
}
// check if the item is linked (ie. carried by a char, on the map, in a container)
if ((cn=it[in].carried)) {
if (cn<1 || cn>=MAXCHARS) {
elog("item %s (%d) thinks it is carried by character %d, but that number is out of bounds. fixing.",it[in].name,in,cn);
elog_item(in);
it[in].carried=0;
err++;
continue;
}
if (!ch[cn].flags) {
elog("item %s (%d) thinks it is carried by character %s (%d), but that character is unused. fixing.",it[in].name,in,ch[cn].name,cn);
elog_item(in);
it[in].carried=0;
err++;
continue;
}
for (n=0; n<INVENTORYSIZE; n++) {
if (ch[cn].item[n]==in) break;
}
if (n==INVENTORYSIZE && ch[cn].citem!=in) {
elog("item %s (%d) thinks it is carried by character %s (%d), but there is no link back. fixing.",it[in].name,in,ch[cn].name,cn);
elog_item(in);
it[in].carried=0;
err++;
continue;
}
} else if (it[in].x) {
if (it[in].x<1 || it[in].x>=MAXMAP || it[in].y<1 || it[in].y>=MAXMAP) {
elog("item %s (%d) thinks it is on position %d,%d, but that position is out of bounds. fixing.",it[in].name,in,it[in].x,it[in].y);
elog_item(in);
it[in].x=it[in].y=0;
err++;
continue;
}
m=it[in].x+it[in].y*MAXMAP;
if (map[m].it!=in) {
elog("item %s (%d) thinks it is on position %d,%d, but there is no link back. fixing.",it[in].name,in,it[in].x,it[in].y);
elog_item(in);
it[in].x=it[in].y=0;
err++;
continue;
}
} else if ((ct=it[in].contained)) {
if (ct<1 || ct>=MAXCONTAINER) {
elog("item %s (%d) thinks it is in container %d, but that number is out of bounds. fixing.",it[in].name,in,ct);
elog_item(in);
it[in].contained=0;
err++;
continue;
}
if (!con[ct].cn && !con[ct].in) {
elog("item %s (%d) thinks it is in container (%d), but that container is unused. fixing.",it[in].name,in,ct);
elog_item(in);
it[in].contained=0;
err++;
continue;
}
for (n=0; n<CONTAINERSIZE; n++) {
if (con[ct].item[n]==in) break;
}
if (n==CONTAINERSIZE) {
elog("item %s (%d) thinks it is in container (%d), but there is no link back. fixing.",it[in].name,in,ct);
elog_item(in);
it[in].contained=0;
err++;
continue;
}
} else {
elog("item %s (%d): not linked. fixing.",it[in].name,in);
elog_item(in);
free_item(in);
err++;
}
}
return err;
}
// check if all item references on the map are valid
int consistency_check_map(void)
{
int m,in,err=0;
if (!item_used) item_used=xcalloc(MAXITEM,IM_TEMP);
else bzero(item_used,MAXITEM);
for (m=0; m<MAXMAP*MAXMAP; m++) {
if ((in=map[m].it)) {
if (in<1 || in>=MAXITEM) {
elog("map pos %d,%d contains link to illegal item number %d. fixing.",m%MAXMAP,m/MAXMAP,in);
map[m].it=0;
err++;
continue;
}
if (!it[in].flags) {
elog("map pos %d,%d contains link to item %s (%d) but that item is unused. fixing.",m%MAXMAP,m/MAXMAP,it[in].name,in);
elog_item(in);
map[m].it=0;
err++;
continue;
}
if (it[in].flags&IF_VOID) {
elog("map pos %d,%d contains link to item %s (%d) but that item is in the void. fixing.",m%MAXMAP,m/MAXMAP,it[in].name,in);
elog_item(in);
map[m].it=0;
err++;
continue;
}
if (it[in].carried) {
elog("map pos %d,%d contains link to item %s (%d) but that item has carried set to %d. fixing.",m%MAXMAP,m/MAXMAP,it[in].name,in,it[in].carried);
elog_item(in);
map[m].it=0;
err++;
continue;
}
if (it[in].contained) {
elog("map pos %d,%d contains link to item %s (%d) but that item has contained set to %d. fixing.",m%MAXMAP,m/MAXMAP,it[in].name,in,it[in].contained);
elog_item(in);
map[m].it=0;
err++;
continue;
}
if (it[in].x+it[in].y*MAXMAP!=m) {
elog("map pos %d,%d contains link to item %s (%d) but that item has no link back (%d,%d). fixing.",m%MAXMAP,m/MAXMAP,it[in].name,in,it[in].x,it[in].y);
elog_item(in);
map[m].it=0;
err++;
continue;
}
item_used[in]++;
if (item_used[in]>1) {
elog("item %s (%d) has %d links, one from map at %d,%d. fixing.",it[in].name,in,item_used[in],m%MAXMAP,m/MAXMAP);
elog_item(in);
map[m].it=0;
err++;
continue;
}
}
}
return err;
}
// check if all item references on characters are valid
int consistency_check_chars(void)
{
int cn,in,n,err=0;
if (!item_used) return -1;
for (cn=1; cn<MAXCHARS; cn++) {
if (!ch[cn].flags) continue;
for (n=0; n<=INVENTORYSIZE; n++) {
if ((n!=INVENTORYSIZE && (in=ch[cn].item[n])) || (n==INVENTORYSIZE && (in=ch[cn].citem))) {
if (in<1 || in>=MAXITEM) {
elog("character %s (%d) contains link to illegal item number %d. fixing.",ch[cn].name,cn,in);
if (n==INVENTORYSIZE) ch[cn].citem=0; else ch[cn].item[n]=0;
ch[cn].flags|=CF_ITEMS;
if (ch[cn].flags&CF_PLAYER) log_char(cn,LOG_SYSTEM,0,"°c3You encountered a bug (consist1). Your item %s has been removed. Please email game@astonia.com. We apologize for the bug.",it[in].name);
err++;
continue;
}
if (!it[in].flags) {
elog("character %s (%d) contains link to item %s (%d) but that item is unused. fixing.",ch[cn].name,cn,it[in].name,in);
elog_item(in);
if (n==INVENTORYSIZE) ch[cn].citem=0; else ch[cn].item[n]=0;
ch[cn].flags|=CF_ITEMS;
if (ch[cn].flags&CF_PLAYER) log_char(cn,LOG_SYSTEM,0,"°c3You encountered a bug (consist2). Your item %s has been removed. Please email game@astonia.com. We apologize for the bug.",it[in].name);
err++;
continue;
}
if (it[in].flags&IF_VOID) {
elog("character %s (%d) contains link to item %s (%d) but that item is in the void. fixing.",ch[cn].name,cn,it[in].name,in);
elog_item(in);
if (n==INVENTORYSIZE) ch[cn].citem=0; else ch[cn].item[n]=0;
ch[cn].flags|=CF_ITEMS;
if (ch[cn].flags&CF_PLAYER) log_char(cn,LOG_SYSTEM,0,"°c3You encountered a bug (consist3). Your item %s has been removed. Please email game@astonia.com. We apologize for the bug.",it[in].name);
err++;
continue;
}
if (it[in].carried!=cn) {
elog("character %s (%d) contains link to item %s (%d) but that item has no link back (%d). fixing.",ch[cn].name,cn,it[in].name,in,it[in].carried);
elog_item(in);
if (n==INVENTORYSIZE) ch[cn].citem=0; else ch[cn].item[n]=0;
ch[cn].flags|=CF_ITEMS;
if (ch[cn].flags&CF_PLAYER) log_char(cn,LOG_SYSTEM,0,"°c3You encountered a bug (consist4). Your item %s has been removed. Please email game@astonia.com. We apologize for the bug.",it[in].name);
err++;
continue;
}
if (it[in].x) {
elog("character %s (%d) contains link to item %s (%d) but that item has pos set to %d,%d. fixing.",ch[cn].name,cn,it[in].name,in,it[in].x,it[in].y);
elog_item(in);
it[in].x=it[in].y=0;
err++;
continue;
}
if (it[in].contained) {
elog("character %s (%d) contains link to item %s (%d) but that item has contained set to %d. fixing.",ch[cn].name,cn,it[in].name,in,it[in].contained);
elog_item(in);
it[in].contained=0;
err++;
continue;
}
item_used[in]++;
if (item_used[in]>1) {
elog("item %s (%d) has %d links, one from character %s (%d). fixing.",it[in].name,in,item_used[in],ch[cn].name,cn);
elog_item(in);
if (n==INVENTORYSIZE) ch[cn].citem=0; else ch[cn].item[n]=0;
ch[cn].flags|=CF_ITEMS;
if (ch[cn].flags&CF_PLAYER) log_char(cn,LOG_SYSTEM,0,"°c3You encountered a bug (consist4). Your item %s has been removed. Please email game@astonia.com. We apologize for the bug.",it[in].name);
err++;
continue;
}
}
}
}
return err;
}
int consistency_check_containers(void)
{
int ct,n,in,err=0;
for (ct=1; ct<MAXCONTAINER; ct++) {
if (!con[ct].cn && !con[ct].in) continue;
for (n=0; n<CONTAINERSIZE; n++) {
if ((in=con[ct].item[n])) {
if (in<1 || in>=MAXITEM) {
elog("container (%d) contains link to illegal item number %d. fixing.",ct,in);
con[ct].item[n]=0;
err++;
continue;
}
if (!it[in].flags) {
elog("container (%d) contains link to item %s (%d) but that item is unused. fixing.",ct,it[in].name,in);
elog_item(in);
con[ct].item[n]=0;
err++;
continue;
}
if (it[in].flags&IF_VOID) {
elog("container (%d) contains link to item %s (%d) but that item is in the void. fixing.",ct,it[in].name,in);
elog_item(in);
con[ct].item[n]=0;
err++;
continue;
}
if (it[in].contained!=ct) {
elog("container (%d) contains link to item %s (%d) but that item has no link back (%d). fixing.",ct,it[in].name,in,it[in].contained);
elog_item(in);
con[ct].item[n]=0;
err++;
continue;
}
if (it[in].x) {
elog("container (%d) contains link to item %s (%d) but that item has pos set to %d,%d. fixing.",ct,it[in].name,in,it[in].x,it[in].y);
elog_item(in);
it[in].x=it[in].y=0;
err++;
continue;
}
if (it[in].carried) {
elog("container (%d) contains link to item %s (%d) but that item has carried set to %d. fixing.",ct,it[in].name,in,it[in].carried);
elog_item(in);
it[in].carried=0;
err++;
continue;
}
item_used[in]++;
if (item_used[in]>1) {
elog("item %s (%d) has %d links, one from container (%d). fixing.",it[in].name,in,item_used[in],ct);
elog_item(in);
con[ct].item[n]=0;
err++;
continue;
}
}
}
}
return err;
}