summaryrefslogtreecommitdiff
path: root/src/map/storage.c
blob: 3944f1ac1807c7fa94f3433b1d3c28137b891288 (plain) (blame)
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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
// $Id: storage.c,v 1.3 2004/09/25 02:05:22 MouseJstr Exp $
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "db.h"
#include "itemdb.h"
#include "clif.h"
#include "intif.h"
#include "pc.h"
#include "storage.h"
#include "guild.h"
#include "nullpo.h"
#include "malloc.h"

#ifdef MEMWATCH
#include "memwatch.h"
#endif

static struct dbt *storage_db;
static struct dbt *guild_storage_db;

/*==========================================
 * �q�ɓ��A�C�e���\�[�g
 *------------------------------------------
 */
int storage_comp_item(const void *_i1, const void *_i2){
struct item *i1=(struct item *)_i1;
struct item *i2=(struct item *)_i2;

	if (i1->nameid == i2->nameid) {
		return 0;
	} else if (!(i1->nameid) || !(i1->amount)){
		return 1;
	} else if (!(i2->nameid) || !(i2->amount)){
		return -1;
	} else {
		return i1->nameid - i2->nameid;
	}
}

 
void sortage_sortitem(struct storage* stor){
	nullpo_retv(stor);

	qsort(stor->storage, MAX_STORAGE, sizeof(struct item), storage_comp_item);
}

void sortage_gsortitem(struct guild_storage* gstor){
	nullpo_retv(gstor);

	qsort(gstor->storage, MAX_GUILD_STORAGE, sizeof(struct item), storage_comp_item);
}

/*==========================================
 * �������Ƃ�
 *------------------------------------------
 */
int do_init_storage(void) // map.c::do_init()����Ă΂��
{
	storage_db=numdb_init();
	guild_storage_db=numdb_init();
	return 1;
}

void do_final_storage(void) // map.c::do_final()����Ă΂��
{
}

struct storage *account2storage(int account_id)
{
	struct storage *stor;
	stor=numdb_search(storage_db,account_id);
	if(stor == NULL) {
		stor = aCalloc(sizeof(struct storage), 1);
		if(stor == NULL){
			printf("storage: out of memory!\n");
			exit(0);
		}
		memset(stor,0,sizeof(struct storage));
		stor->account_id=account_id;
		numdb_insert(storage_db,stor->account_id,stor);
	}
	return stor;
}

// Just to ask storage, without creation
struct storage *account2storage2(int account_id) {
	return numdb_search(storage_db, account_id);
}

int storage_delete(int account_id)
{
	struct storage *stor = numdb_search(storage_db,account_id);
	if(stor) {
		numdb_erase(storage_db,account_id);
		free(stor);
	}
	return 0;
}

/*==========================================
 * �J�v���q�ɂ��J��
 *------------------------------------------
 */
int storage_storageopen(struct map_session_data *sd)
{
	struct storage *stor;

	nullpo_retr(0, sd);

	if((stor = numdb_search(storage_db,sd->status.account_id)) != NULL) {
		if (stor->storage_status == 0) {
			stor->storage_status = 1;
			sd->state.storage_flag = 0;
			clif_storageitemlist(sd,stor);
			clif_storageequiplist(sd,stor);
			clif_updatestorageamount(sd,stor);
			return 0;
		}	
	} else
		intif_request_storage(sd->status.account_id);

	return 1;
}

/*==========================================
 * �J�v���q�ɂփA�C�e���lj�
 *------------------------------------------
 */
int storage_additem(struct map_session_data *sd,struct storage *stor,struct item *item_data,int amount)
{
	struct item_data *data;
	int i;

	nullpo_retr(1, sd);
	nullpo_retr(1, stor);
	nullpo_retr(1, item_data);

	stor->dirty = 1;

	if(item_data->nameid <= 0 || amount <= 0)
		return 1;
	nullpo_retr(1, data = itemdb_search(item_data->nameid));

	i=MAX_STORAGE;
	if(!itemdb_isequip2(data)){
		// �����i�ł͂Ȃ��̂ŁA�����L�i�Ȃ���̂ݕω�������
		for(i=0;i<MAX_STORAGE;i++){
			if( compare_item (&stor->storage[i], item_data)) {
				if(stor->storage[i].amount+amount > MAX_AMOUNT)
					return 1;
				stor->storage[i].amount+=amount;
				clif_storageitemadded(sd,stor,i,amount);
				break;
			}
		}
	}
	if(i>=MAX_STORAGE){
		// �����i�������L�i�������̂ŋ󂫗��֒lj�
		for(i=0;i<MAX_STORAGE;i++){
			if(stor->storage[i].nameid==0){
				memcpy(&stor->storage[i],item_data,sizeof(stor->storage[0]));
				stor->storage[i].amount=amount;
				stor->storage_amount++;
				clif_storageitemadded(sd,stor,i,amount);
				clif_updatestorageamount(sd,stor);
				break;
			}
		}
		if(i>=MAX_STORAGE)
			return 1;
	}

	return 0;
}
/*==========================================
 * �J�v���q�ɃA�C�e�������炷
 *------------------------------------------
 */
int storage_delitem(struct map_session_data *sd,struct storage *stor,int n,int amount)
{
	nullpo_retr(1, sd);
	nullpo_retr(1, stor);

	if(stor->storage[n].nameid==0 || stor->storage[n].amount<amount)
		return 1;

	stor->storage[n].amount-=amount;
	if(stor->storage[n].amount==0){
		memset(&stor->storage[n],0,sizeof(stor->storage[0]));
		stor->storage_amount--;
		clif_updatestorageamount(sd,stor);
	}
	clif_storageitemremoved(sd,n,amount);

	stor->dirty = 1;

	return 0;
}
/*==========================================
 * �J�v���q�ɂ֓����
 *------------------------------------------
 */
int storage_storageadd(struct map_session_data *sd,int index,int amount)
{
	struct storage *stor;

	nullpo_retr(0, sd);
	nullpo_retr(0, stor=account2storage2(sd->status.account_id));

	if( (stor->storage_amount <= MAX_STORAGE) && (stor->storage_status == 1) ) { // storage not full & storage open
		if(index>=0 && index<MAX_INVENTORY) { // valid index
			if( (amount <= sd->status.inventory[index].amount) && (amount > 0) ) { //valid amount
				if(storage_additem(sd,stor,&sd->status.inventory[index],amount)==0)
				// remove item from inventory
					pc_delitem(sd,index,amount,0);
			} // valid amount
		}// valid index
	}// storage not full & storage open

	return 0;
}

/*==========================================
 * �J�v���q�ɂ���o��
 *------------------------------------------
 */
int storage_storageget(struct map_session_data *sd,int index,int amount)
{
	struct storage *stor;
	int flag;

	nullpo_retr(0, sd);
	nullpo_retr(0, stor=account2storage2(sd->status.account_id));

	if(stor->storage_status == 1) { //  storage open
		if(index>=0 && index<MAX_STORAGE) { // valid index
			if( (amount <= stor->storage[index].amount) && (amount > 0) ) { //valid amount
				if((flag = pc_additem(sd,&stor->storage[index],amount)) == 0)
					storage_delitem(sd,stor,index,amount);
				else
					clif_additem(sd,0,0,flag);
			} // valid amount
		}// valid index
	}// storage open

	return 0;
}
/*==========================================
 * �J�v���q�ɂփJ�[�g��������
 *------------------------------------------
 */
int storage_storageaddfromcart(struct map_session_data *sd,int index,int amount)
{
	struct storage *stor;

	nullpo_retr(0, sd);
	nullpo_retr(0, stor=account2storage2(sd->status.account_id));

	if( (stor->storage_amount <= MAX_STORAGE) && (stor->storage_status == 1) ) { // storage not full & storage open
		if(index>=0 && index<MAX_INVENTORY) { // valid index
			if( (amount <= sd->status.cart[index].amount) && (amount > 0) ) { //valid amount
				if(storage_additem(sd,stor,&sd->status.cart[index],amount)==0)
					pc_cart_delitem(sd,index,amount,0);
			} // valid amount
		}// valid index
	}// storage not full & storage open

	return 0;
}

/*==========================================
 * �J�v���q�ɂ���J�[�g�֏o��
 *------------------------------------------
 */
int storage_storagegettocart(struct map_session_data *sd,int index,int amount)
{
	struct storage *stor;

	nullpo_retr(0, sd);
	nullpo_retr(0, stor=account2storage2(sd->status.account_id));

	if(stor->storage_status == 1) { //  storage open
		if(index>=0 && index<MAX_STORAGE) { // valid index
			if( (amount <= stor->storage[index].amount) && (amount > 0) ) { //valid amount
				if(pc_cart_additem(sd,&stor->storage[index],amount)==0){
					storage_delitem(sd,stor,index,amount);
				}
			} // valid amount
		}// valid index
	}// storage open

	return 0;
}


/*==========================================
 * �J�v���q�ɂ�‚���
 *------------------------------------------
 */
int storage_storageclose(struct map_session_data *sd)
{
	struct storage *stor;

	nullpo_retr(0, sd);
	nullpo_retr(0, stor=account2storage2(sd->status.account_id));

	stor->storage_status=0;
	sd->state.storage_flag = 0;
	clif_storageclose(sd);

	storage_storage_save(sd);

	sortage_sortitem(stor);
	return 0;
}

/*==========================================
 * ���O�A�E�g���J���Ă���J�v���q�ɂ̕ۑ�
 *------------------------------------------
 */
int storage_storage_quit(struct map_session_data *sd)
{
	struct storage *stor;

	nullpo_retr(0, sd);

	stor = numdb_search(storage_db,sd->status.account_id);
	if(stor)  {
		stor->storage_status = 0;
		storage_storage_save(sd);
	}

	return 0;
}

void storage_storage_dirty(struct map_session_data *sd)
{
	struct storage *stor;

	stor=numdb_search(storage_db,sd->status.account_id);

	if(stor)
		stor->dirty = 1;
}

int storage_storage_save(struct map_session_data *sd)
{
	struct storage *stor;

	nullpo_retr(0, sd);

	stor=numdb_search(storage_db,sd->status.account_id);
	if(stor && stor->dirty)  {
		intif_send_storage(stor);
		stor->dirty = 0;
	}

	return 0;
}

struct guild_storage *guild2storage(int guild_id)
{
	struct guild_storage *gs = NULL;
	if(guild_search(guild_id) != NULL) {
		gs=numdb_search(guild_storage_db,guild_id);
		if(gs == NULL) {
			gs = aCalloc(sizeof(struct guild_storage), 1);
			if(gs==NULL){
				printf("storage: out of memory!\n");
				exit(0);
			}
			gs->guild_id=guild_id;
			numdb_insert(guild_storage_db,gs->guild_id,gs);
		}
	}
	return gs;
}

int guild_storage_delete(int guild_id)
{
	struct guild_storage *gstor = numdb_search(guild_storage_db,guild_id);
	if(gstor) {
		numdb_erase(guild_storage_db,guild_id);
		free(gstor);
	}
	return 0;
}

int storage_guild_storageopen(struct map_session_data *sd)
{
	struct guild_storage *gstor;

	nullpo_retr(0, sd);

	if(sd->status.guild_id <= 0)
		return 2;
	if((gstor = numdb_search(guild_storage_db,sd->status.guild_id)) != NULL) {
		if(gstor->storage_status)
			return 1;
		gstor->storage_status = 1;
		sd->state.storage_flag = 1;
		clif_guildstorageitemlist(sd,gstor);
		clif_guildstorageequiplist(sd,gstor);
		clif_updateguildstorageamount(sd,gstor);
		return 0;
	}
	else {
		gstor = guild2storage(sd->status.guild_id);
		gstor->storage_status = 1;
		intif_request_guild_storage(sd->status.account_id,sd->status.guild_id);
	}

	return 0;
}

int guild_storage_additem(struct map_session_data *sd,struct guild_storage *stor,struct item *item_data,int amount)
{
	struct item_data *data;
	int i;

	nullpo_retr(1, sd);
	nullpo_retr(1, stor);
	nullpo_retr(1, item_data);
	nullpo_retr(1, data = itemdb_search(item_data->nameid));

	if(item_data->nameid <= 0 || amount <= 0)
		return 1;

	i=MAX_GUILD_STORAGE;
	if(!itemdb_isequip2(data)){
		// �����i�ł͂Ȃ��̂ŁA�����L�i�Ȃ���̂ݕω�������
		for(i=0;i<MAX_GUILD_STORAGE;i++){
			if(compare_item(&stor->storage[i], item_data)) {
				if(stor->storage[i].amount+amount > MAX_AMOUNT)
					return 1;
				stor->storage[i].amount+=amount;
				clif_guildstorageitemadded(sd,stor,i,amount);
				break;
			}
		}
	}
	if(i>=MAX_GUILD_STORAGE){
		// �����i�������L�i�������̂ŋ󂫗��֒lj�
		for(i=0;i<MAX_GUILD_STORAGE;i++){
			if(stor->storage[i].nameid==0){
				memcpy(&stor->storage[i],item_data,sizeof(stor->storage[0]));
				stor->storage[i].amount=amount;
				stor->storage_amount++;
				clif_guildstorageitemadded(sd,stor,i,amount);
				clif_updateguildstorageamount(sd,stor);
				break;
			}
		}
		if(i>=MAX_GUILD_STORAGE)
			return 1;
	}
	return 0;
}

int guild_storage_delitem(struct map_session_data *sd,struct guild_storage *stor,int n,int amount)
{
	nullpo_retr(1, sd);
	nullpo_retr(1, stor);

	if(stor->storage[n].nameid==0 || stor->storage[n].amount<amount)
		return 1;

	stor->storage[n].amount-=amount;
	if(stor->storage[n].amount==0){
		memset(&stor->storage[n],0,sizeof(stor->storage[0]));
		stor->storage_amount--;
		clif_updateguildstorageamount(sd,stor);
	}
	clif_storageitemremoved(sd,n,amount);

	return 0;
}

int storage_guild_storageadd(struct map_session_data *sd,int index,int amount)
{
	struct guild_storage *stor;

	nullpo_retr(0, sd);

	if((stor=guild2storage(sd->status.guild_id)) != NULL) {
		if( (stor->storage_amount <= MAX_GUILD_STORAGE) && (stor->storage_status == 1) ) { // storage not full & storage open
			if(index>=0 && index<MAX_INVENTORY) { // valid index
				if( (amount <= sd->status.inventory[index].amount) && (amount > 0) ) { //valid amount
					if(guild_storage_additem(sd,stor,&sd->status.inventory[index],amount)==0)
					// remove item from inventory
						pc_delitem(sd,index,amount,0);
				} // valid amount
			}// valid index
		}// storage not full & storage open
	}

	return 0;
}

int storage_guild_storageget(struct map_session_data *sd,int index,int amount)
{
	struct guild_storage *stor;
	int flag;

	nullpo_retr(0, sd);

	if((stor=guild2storage(sd->status.guild_id)) != NULL) {
		if(stor->storage_status == 1) { //  storage open
			if(index>=0 && index<MAX_GUILD_STORAGE) { // valid index
				if( (amount <= stor->storage[index].amount) && (amount > 0) ) { //valid amount
					if((flag = pc_additem(sd,&stor->storage[index],amount)) == 0)
						guild_storage_delitem(sd,stor,index,amount);
					else
						clif_additem(sd,0,0,flag);
				} // valid amount
			}// valid index
		}// storage open
	}

	return 0;
}

int storage_guild_storageaddfromcart(struct map_session_data *sd,int index,int amount)
{
	struct guild_storage *stor;

	nullpo_retr(0, sd);

	if((stor=guild2storage(sd->status.guild_id)) != NULL) {
		if( (stor->storage_amount <= MAX_GUILD_STORAGE) && (stor->storage_status == 1) ) { // storage not full & storage open
			if(index>=0 && index<MAX_INVENTORY) { // valid index
				if( (amount <= sd->status.cart[index].amount) && (amount > 0) ) { //valid amount
					if(guild_storage_additem(sd,stor,&sd->status.cart[index],amount)==0)
						pc_cart_delitem(sd,index,amount,0);
				} // valid amount
			}// valid index
		}// storage not full & storage open
	}

	return 0;
}

int storage_guild_storagegettocart(struct map_session_data *sd,int index,int amount)
{
	struct guild_storage *stor;

	nullpo_retr(0, sd);

	if((stor=guild2storage(sd->status.guild_id)) != NULL) {
		if(stor->storage_status == 1) { //  storage open
			if(index>=0 && index<MAX_GUILD_STORAGE) { // valid index
				if( (amount <= stor->storage[index].amount) && (amount > 0) ) { //valid amount
					if(pc_cart_additem(sd,&stor->storage[index],amount)==0){
						guild_storage_delitem(sd,stor,index,amount);
					}
				} // valid amount
			}// valid index
		}// storage open
	}

	return 0;
}

int storage_guild_storageclose(struct map_session_data *sd)
{
	struct guild_storage *stor;

	nullpo_retr(0, sd);

	if((stor=guild2storage(sd->status.guild_id)) != NULL) {
		intif_send_guild_storage(sd->status.account_id,stor);
		stor->storage_status = 0;
		sd->state.storage_flag = 0;
		sortage_gsortitem(stor);
	}
	clif_storageclose(sd);

	return 0;
}

int storage_guild_storage_quit(struct map_session_data *sd,int flag)
{
	struct guild_storage *stor;

	nullpo_retr(0, sd);

	stor = numdb_search(guild_storage_db,sd->status.guild_id);
	if(stor) {
		if(!flag)
			intif_send_guild_storage(sd->status.account_id,stor);
		stor->storage_status = 0;
		sd->state.storage_flag = 0;
	}

	return 0;
}