forked from librae8226/gmsv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
object.c
483 lines (432 loc) · 11.5 KB
/
object.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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
#include "version.h"
#include <sys/stat.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <dirent.h>
#include "common.h"
#include "object.h"
#include "char_base.h"
#include "char.h"
#include "item.h"
#include "buf.h"
#include "readmap.h"
#include "pet.h"
static Object *obj;
static int objnum;
#ifndef _SIMPLIFY_ITEMSTRING
static int Restored = FALSE;
#endif
BOOL initObjectArray( int num )
{
int i;
objnum = num;
obj = allocateMemory( sizeof( Object ) * objnum );
if( obj == NULL ) return FALSE;
for( i = 0 ; i < objnum ; i ++ ){
memset( &obj[i], 0 , sizeof( Object ));
obj[i].type = OBJTYPE_NOUSE;
}
return TRUE;
}
BOOL endObjectArray( void )
{
freeMemory( obj );
return TRUE;
}
INLINE int _initObjectOne( char *file, int line, Object* ob )
{
static int allocobjnum = 0;
int i;
BOOL first;
i = allocobjnum;
first = TRUE;
while( 1 ) {
if( !first && i >= allocobjnum ) {
fprint( "Can't allocate an object\n" );
return -1;
}
if( obj[i].type == OBJTYPE_NOUSE ) {
if( MAP_addNewObj( ob->floor ,ob->x, ob->y, i ) == 1 ){
memcpy( &obj[i] , ob , sizeof( Object ) );
allocobjnum = ( i+1 >= objnum) ? 0:i+1;
return i;
}else{
allocobjnum = ( i+1 >= objnum) ? 0:i+1;
fprint( "MAP_addNewObj error\n" );
return -1;
}
}else {
if( ++i >= objnum ) {
i = 0;
first = FALSE;
}
}
}
fprint( "Can't allocate an object\n" );
return -1;
}
void endObjectOne( int index )
{
if(objnum <= index || index < 0 )return;
if( MAP_removeObj(obj[index].floor,obj[index].x, obj[index].y,
index) == FALSE){
fprint( "REMOVE OBJ ERROR\n" );
}
obj[index].type = OBJTYPE_NOUSE;
}
BOOL CHECKOBJECT( int index )
{
if( 0<=index && index<objnum )return TRUE;
return FALSE;
}
BOOL CHECKOBJECTUSE( int index )
{
if( CHECKOBJECT(index) == FALSE )return FALSE;
if( obj[index].type == OBJTYPE_NOUSE )return FALSE;
return TRUE;
}
INLINE int OBJECT_getType( int index )
{
return obj[index].type;
}
#ifdef _NPCSERVER_NEW
INLINE int OBJECT_getNpcIndex( int index )
{
return obj[index].npcsindex;
}
INLINE char* OBJECT_getName( int index )
{
return obj[index].objname;
}//imagenum
INLINE int OBJECT_getNpcImage( int index )
{
return obj[index].imagenum;
}
INLINE int OBJECT_getNpcdir( int index )
{
return obj[index].dir;
}
#endif
INLINE int OBJECT_getchartype( int index )
{
return obj[index].chartype;
}
INLINE void OBJECT_setchartype( int index, int flg)
{
obj[index].chartype = flg;
}
INLINE int OBJECT_setType( int index, int newvalue )
{
int old;
old = obj[index].type;
obj[index].type = newvalue;
return old;
}
INLINE int OBJECT_getFloor( int index )
{
return obj[index].floor;
}
INLINE int OBJECT_setFloor( int index, int newvalue )
{
int old;
old = obj[index].floor;
obj[index].floor = newvalue;
return old;
}
INLINE int OBJECT_getX( int index )
{
return obj[index].x;
}
INLINE int OBJECT_setX( int index, int newvalue )
{
int old;
old = obj[index].x;
obj[index].x = newvalue;
return old;
}
INLINE int OBJECT_getY( int index )
{
return obj[index].y;
}
INLINE int OBJECT_setY( int index, int newvalue )
{
int old;
old = obj[index].y;
obj[index].y = newvalue;
return old;
}
#ifdef _DEL_DROP_GOLD
INLINE int OBJECT_getTime( int index )
{
return obj[index].time;
}
INLINE int OBJECT_setTime( int index, int newvalue )
{
int old;
old = obj[index].time;
obj[index].time = newvalue;
return old;
}
#endif
/*------------------------------------------------------------
* index 毛 月
* 娄醒
* index int 奶件犯永弁旦
* 忒曰袄
* int
------------------------------------------------------------*/
INLINE int OBJECT_getIndex( int index )
{
return obj[index].index;
}
/*------------------------------------------------------------
* y 毛涩烂允月
* 娄醒
* index int 奶件犯永弁旦
* newvalue int 蕙仄中袄
* 忒曰袄
* int 樯及袄
------------------------------------------------------------*/
INLINE int OBJECT_setIndex( int index, int newvalue )
{
int old;
old = obj[index].index;
obj[index].index = newvalue;
return old;
}
/*------------------------------------------------------------
* 左皮斥尼弁玄及 醒毛菲户月
* 娄醒
* 卅仄
* 忒曰袄
* int
------------------------------------------------------------*/
INLINE int OBJECT_getNum( void )
{
return objnum;
}
/*------------------------------------------------------------
* 左皮斥尼弁玄厌瞻 及丢件田毛蝈 娄醒卞 匀化
* 左皮斥尼弁玄厌瞻 毛综曰请仄化}瓒 允月[
*
* 娄醒
* type OBJTYPE 左皮斥尼弁玄及正奶皿
* index int 公木冗木及 匹及index
* x int x甄
* y int y甄
* floor int floorid
* 忒曰袄
* int 喃曰癫化木凶index, 撩 及凛反} -1
------------------------------------------------------------*/
int initObjectFromObjectMember(
OBJTYPE type, int index, int x, int y , int floor )
{
Object obj;
obj.type = type;
obj.index = index;
obj.x = x;
obj.y = y;
obj.floor = floor;
return initObjectOne( &obj );
}
int searchObjectFromCharaIndex( int index )
{
int i;
for( i = 0 ; i < objnum ; i ++ )
if( obj[i].type == OBJTYPE_CHARA && obj[i].index == index )
return i;
return -1;
}
#define ITEMGOLDSTOREFILENAME "itemgold"
#define STOREITEMID "ITEM"
#define STOREGOLDID "GOLD"
#define STORECHARID "CHAR"
#ifndef _SIMPLIFY_ITEMSTRING
static BOOL checkObjectStoreFile( char* line, Object* one, char** stringstart)
{
char token[16];
int ret;
int i;
ret = getStringFromIndexWithDelim(line,"|" ,1 , token, sizeof( token ) );
if( ret == FALSE )return FALSE;
if( strcmp( token , STOREITEMID ) == 0 )
one->type = OBJTYPE_ITEM;
else if( strcmp( token , STOREGOLDID ) == 0 )
one->type = OBJTYPE_GOLD;
else if( strcmp( token, STORECHARID) == 0 )
one->type = OBJTYPE_CHARA;
char first[64];
char second[64];
for( i = 2 ; i < 5 ; i ++ ){
ret = getStringFromIndexWithDelim(line,"|" ,i,
token,sizeof( token ));
if( ret == FALSE )return FALSE;
ret = getStringFromIndexWithDelim(token,"=" ,1, first,sizeof( first ));
if( ret == FALSE )return FALSE;
ret = getStringFromIndexWithDelim(token,"=" ,2, second,sizeof( second ));
if( ret == FALSE )return FALSE;
if( strcmp( first , "x" ) == 0 )
one->x = atoi( second );
else if( strcmp( first , "y" ) == 0 )
one->y = atoi( second );
else if( strcmp( first , "floor" ) == 0 )
one->floor = atoi( second );
}
{
char* findex = nindex( line , '|' , 4);
*stringstart = findex + 1;
}
return TRUE;
}
BOOL storeObjects( char* dirname )
{
int i;
FILE* igfile;
char igfilename[256];
if( Restored == TRUE ){
snprintf( igfilename ,sizeof( igfilename ) ,"%s/%s" ,dirname ,
ITEMGOLDSTOREFILENAME );
}else{
snprintf( igfilename ,sizeof( igfilename ) ,"%s/%s_extra" ,dirname ,
ITEMGOLDSTOREFILENAME );
print( "\n---- Saving didn't do with gmsv. ----- \n");
print( "---- It is because gmsv stopped before reading . ----- \n");
print( "---- It is kept with a name of (%s) just in case. ----- \n", igfilename );
}
igfile = fopen( igfilename , "w" );
if( igfile == NULL ){
print( "\n\n---- ERR:can not open (%s) for backup items. ----- \n\n", igfilename );
return FALSE;
}
print( "Store Objects ...");
int petindex;
for( i = 0 ; i < objnum ; i ++ ){
switch( obj[i].type ){
case OBJTYPE_ITEM:
{
fprintf( igfile , STOREITEMID "|x=%d|y=%d|floor=%d|%s\n" ,
obj[i].x , obj[i].y ,obj[i].floor,
ITEM_makeStringFromItemIndex( obj[i].index,0 ) );
break;
}
case OBJTYPE_GOLD:
{
fprintf( igfile , STOREGOLDID "|x=%d|y=%d|floor=%d|%d\n" ,
obj[i].x , obj[i].y ,obj[i].floor, obj[i].index );
break;
}
case OBJTYPE_CHARA:
{
petindex = obj[i].index;
if( CHAR_CHECKINDEX( petindex) ) {
if( CHAR_getInt( obj[i].index, CHAR_WHICHTYPE) == CHAR_TYPEPET){
fprintf( igfile , STORECHARID "|x=%d|y=%d|floor=%d|%s\n" ,
obj[i].x , obj[i].y ,obj[i].floor,
CHAR_makePetStringFromPetIndex( obj[i].index ) );
}
}
break;
}
default:
break;
}
}
fclose( igfile );
print( "done\n");
system( "./itemda.pl" );
print( "item backup end\n");
return TRUE;
}
#endif
int detectStoreFile( char* filename )
{
FILE* f;
char buffer[10];
f = fopen ( filename ,"r" );
if( f == NULL )return 3;
if( fgets( buffer, sizeof( buffer ), f ) == NULL ){
fclose(f);
return 3;
}
if( strncmp(buffer,STOREITEMID,sizeof(STOREITEMID) ) == 0 ||
strncmp(buffer,STOREGOLDID,sizeof(STOREGOLDID) ) == 0 ){
fclose(f);
return 2;
}
fclose(f);
return 3;
}
#ifndef _SIMPLIFY_ITEMSTRING
BOOL restoreObjects( char* dirname )
{
char igfilename[512];
FILE* file;
char line[2048];
snprintf( igfilename, sizeof(igfilename), "%s/%s", dirname, ITEMGOLDSTOREFILENAME );
file = fopen( igfilename , "r" );
if( file == NULL ){
Restored = TRUE;
return FALSE;
}
while( fgets( line, sizeof( line ), file ) != NULL ){
Object one;
char *string;
chomp( line );
if( checkObjectStoreFile( line, &one ,&string ) == FALSE )
continue;
switch( one.type ){
case OBJTYPE_ITEM:
{
ITEM_Item itmone;
BOOL ret;
int objindex;
ret = ITEM_makeExistItemsFromStringToArg( string, &itmone, 0 );
if( ret == TRUE ){
one.index = ITEM_initExistItemsOne( &itmone );
if( one.index == -1 ) break;
objindex = initObjectOne( &one );
ITEM_setWorkInt(one.index,ITEM_WORKCHARAINDEX,-1);
ITEM_setWorkInt(one.index,ITEM_WORKOBJINDEX,objindex);
}
break;
}
case OBJTYPE_GOLD:
one.index = atoi( string );
initObjectOne( &one );
break;
case OBJTYPE_CHARA:
{
Char ch;
int ret;
ret = CHAR_makePetFromStringToArg(string,&ch, -1);
if( ret ) {
Object object;
int objindex;
int petindex = PET_initCharOneArray( &ch );
if( petindex < 0 ) {
print( "宠物制作失败。\n");
}
object.type = OBJTYPE_CHARA;
object.index = petindex;
object.x = ch.data[CHAR_X];
object.y = ch.data[CHAR_Y];
object.floor = ch.data[CHAR_FLOOR];
objindex = initObjectOne( &object );
if( objindex == -1 ) {
CHAR_endCharOneArray( petindex );
}else {
CHAR_setWorkInt( petindex,CHAR_WORKOBJINDEX,objindex );
}
}
break;
}
default:
break;
}
}
fclose( file );
Restored = TRUE;
return TRUE;
}
#endif