summaryrefslogblamecommitdiff
path: root/src/map/guild.c
blob: 7d73692c92e9595b1299e930f181e3903eb5cd45 (plain) (tree)
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
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622



















                                                         
                    



































                                                                                 














                             


                           

                                            
                                  
                                             
                         
                                
 















































                                                                                                                                        
                                                                                                                                 
                               











































































































































































































































                                                                                                         
                                             




































































































































































































































































































































































































































































































































                                                                                                                                           
                               
 

                           










                                                                                                    






















































                                                                                    























                                                                                    



                                                                     
                                           


                           


                                            





                                                                                 
                                      


                                                                                      

                                        










                                                               
                                                                                     

















































































































































































                                                                                               


                                       
                






                                       

































































































































































































































































































































































































































                                                                                                                                 
// $Id: guild.c,v 1.5 2004/09/25 05:32:18 MouseJstr Exp $
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "guild.h"
#include "storage.h"
#include "db.h"
#include "timer.h"
#include "socket.h"
#include "nullpo.h"
#include "malloc.h"
#include "battle.h"
#include "npc.h"
#include "pc.h"
#include "map.h"
#include "mob.h"
#include "intif.h"
#include "clif.h"
#include "skill.h"
#include "showmsg.h"

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

static struct dbt *guild_db;
static struct dbt *castle_db;
static struct dbt *guild_expcache_db;
static struct dbt *guild_infoevent_db;
static struct dbt *guild_castleinfoevent_db;

struct eventlist {
	char name[50];
	struct eventlist *next;
};

// �M���h��EXP�L���b�V���̃t���b�V���Ɋ֘A����萔
#define GUILD_PAYEXP_INVERVAL 10000	// �Ԋu(�L���b�V���̍ő吶�����ԁA�~���b)
#define GUILD_PAYEXP_LIST 8192	// �L���b�V���̍ő吔

// �M���h��EXP�L���b�V��
struct guild_expcache {
	int guild_id, account_id, char_id, exp;
};

// �M���h�X�L��db�̃A�N�Z�T�i���͒��ł��ő�p�j
int guild_skill_get_inf(int id) { // Modified for new skills [Sara]
	if (id==GD_BATTLEORDER) return 4;
	else if (id==GD_REGENERATION) return 4;
	else if (id==GD_RESTORE) return 4;
	else if (id==GD_EMERGENCYCALL) return 4;
	else return 0;
}
int guild_skill_get_sp(int id,int lv){ return 0; }
int guild_skill_get_range(int id){ return 0; }
int guild_skill_get_max(int id) { // Modified for new skills [Sara]
	switch (id) {
	case GD_GUARDUP:
		return 3;
	case GD_EXTENSION:
		return 10;
	case GD_LEADERSHIP:
	case GD_GLORYWOUNDS:
	case GD_SOULCOLD:
	case GD_HAWKEYES:
		return 5;
	case GD_REGENERATION:
		return 3;
	default:
		return 1;
	}
}

// �M���h�X�L�������邩�m�F
int guild_checkskill(struct guild *g,int id)
{
	int idx = id-GD_SKILLBASE;
	if (idx < 0 || idx >= MAX_GUILDSKILL)
		return 0;
	return g->skill[idx].lv;
}


int guild_payexp_timer(int tid,unsigned int tick,int id,int data);
int guild_gvg_eliminate_timer(int tid,unsigned int tick,int id,int data);


static int guild_read_castledb(void)
{
	FILE *fp;
	char line[1024];
	int j,ln=0;
	char *str[32],*p;
	struct guild_castle *gc;

	if( (fp=fopen("db/castle_db.txt","r"))==NULL){
		printf("can't read db/castle_db.txt\n");
		return -1;
	}

	while(fgets(line,1020,fp)){
		if(line[0]=='/' && line[1]=='/')
			continue;
		memset(str,0,sizeof(str));
		gc=(struct guild_castle *)aCalloc(1,sizeof(struct guild_castle));
		for(j=0,p=line;j<6 && p;j++){
			str[j]=p;
			p=strchr(p,',');
			if(p) *p++=0;
		}

		gc->guild_id=0; // <Agit> Clear Data for Initialize
		gc->economy=0; gc->defense=0; gc->triggerE=0; gc->triggerD=0; gc->nextTime=0; gc->payTime=0;
		gc->createTime=0; gc->visibleC=0; gc->visibleG0=0; gc->visibleG1=0; gc->visibleG2=0;
		gc->visibleG3=0; gc->visibleG4=0; gc->visibleG5=0; gc->visibleG6=0; gc->visibleG7=0;
		gc->Ghp0=0; gc->Ghp1=0; gc->Ghp2=0; gc->Ghp3=0; gc->Ghp4=0; gc->Ghp5=0; gc->Ghp6=0; gc->Ghp7=0; // guardian HP [Valaris]

		gc->castle_id=atoi(str[0]);
		memcpy(gc->map_name,str[1],24);
		memcpy(gc->castle_name,str[2],24);
		memcpy(gc->castle_event,str[3],24);

		numdb_insert(castle_db,gc->castle_id,gc);

		//intif_guild_castle_info(gc->castle_id);

		ln++;
	}
	fclose(fp);
	sprintf(tmp_output,"Done reading '"CL_WHITE"%d"CL_RESET"' entries in '"CL_WHITE"%s"CL_RESET"'.\n",ln,"db/castle_db.txt");
	ShowStatus(tmp_output);
	return 0;
}

// ������
void do_init_guild(void)
{
	guild_db=numdb_init();
	castle_db=numdb_init();
	guild_expcache_db=numdb_init();
	guild_infoevent_db=numdb_init();
	guild_castleinfoevent_db=numdb_init();

	guild_read_castledb();

	add_timer_func_list(guild_gvg_eliminate_timer,"guild_gvg_eliminate_timer");
	add_timer_func_list(guild_payexp_timer,"guild_payexp_timer");
	add_timer_interval(gettick()+GUILD_PAYEXP_INVERVAL,guild_payexp_timer,0,0,GUILD_PAYEXP_INVERVAL);
}


// ����
struct guild *guild_search(int guild_id)
{
	return numdb_search(guild_db,guild_id);
}
int guild_searchname_sub(void *key,void *data,va_list ap)
{
	struct guild *g=(struct guild *)data,**dst;
	char *str;
	str=va_arg(ap,char *);
	dst=va_arg(ap,struct guild **);
	if(strcmpi(g->name,str)==0)
		*dst=g;
	return 0;
}
// �M���h������
struct guild* guild_searchname(char *str)
{
	struct guild *g=NULL;
	numdb_foreach(guild_db,guild_searchname_sub,str,&g);
	return g;
}
struct guild_castle *guild_castle_search(int gcid)
{
	return numdb_search(castle_db,gcid);
}

// mapname�ɑΉ������A�W�g��gc��Ԃ�
struct guild_castle *guild_mapname2gc(char *mapname)
{
	int i;
	struct guild_castle *gc=NULL;
	for(i=0;i<MAX_GUILDCASTLE;i++){
		gc=guild_castle_search(i);
		if(!gc) continue;
		if(strcmp(gc->map_name,mapname)==0) return gc;
	}
	return NULL;
}

// ���O�C�����̃M���h�����o�[�̂P�l��sd��Ԃ�
struct map_session_data *guild_getavailablesd(struct guild *g)
{
	int i;

	nullpo_retr(NULL, g);

	for(i=0;i<g->max_member;i++)
		if(g->member[i].sd!=NULL)
			return g->member[i].sd;
	return NULL;
}

// �M���h�����o�[�̃C���f�b�N�X��Ԃ�
int guild_getindex(struct guild *g,int account_id,int char_id)
{
	int i;
	if(g==NULL)
		return -1;
	for(i=0;i<g->max_member;i++)
		if( g->member[i].account_id==account_id &&
			g->member[i].char_id==char_id )
			return i;
	return -1;
}
// �M���h�����o�[�̖�E��Ԃ�
int guild_getposition(struct map_session_data *sd,struct guild *g)
{
	int i;

	nullpo_retr(-1, sd);

	if(g==NULL && (g=guild_search(sd->status.guild_id))==NULL)
		return -1;
	for(i=0;i<g->max_member;i++)
		if( g->member[i].account_id==sd->status.account_id &&
			g->member[i].char_id==sd->status.char_id )
			return g->member[i].position;
	return -1;
}

// �����o�[���̍쐬
void guild_makemember(struct guild_member *m,struct map_session_data *sd)
{
	nullpo_retv(sd);

	memset(m,0,sizeof(struct guild_member));
	m->account_id	=sd->status.account_id;
	m->char_id		=sd->status.char_id;
	m->hair			=sd->status.hair;
	m->hair_color	=sd->status.hair_color;
	m->gender		=sd->sex;
	m->class		=sd->status.class;
	m->lv			=sd->status.base_level;
	m->exp			=0;
	m->exp_payper	=0;
	m->online		=1;
	m->position		=MAX_GUILDPOSITION-1;
	memcpy(m->name,sd->status.name,24);
	return;
}
// �M���h�����m�F
int guild_check_conflict(struct map_session_data *sd)
{
	nullpo_retr(0, sd);

	intif_guild_checkconflict(sd->status.guild_id,
		sd->status.account_id,sd->status.char_id);
	return 0;
}

// �M���h��EXP�L���b�V����inter�I�Ƀt���b�V������
int guild_payexp_timer_sub(void *key,void *data,va_list ap)
{
	int i, *dellist,*delp, dataid=(int)key;
	struct guild_expcache *c;
	struct guild *g;

	nullpo_retr(0, ap);
	nullpo_retr(0, c=(struct guild_expcache *)data);
	nullpo_retr(0, dellist=va_arg(ap,int *));
	nullpo_retr(0, delp=va_arg(ap,int *));

	if( *delp>=GUILD_PAYEXP_LIST || (g=guild_search(c->guild_id))==NULL )
		return 0;
	if( ( i=guild_getindex(g,c->account_id,c->char_id) )<0 )
		return 0;

	g->member[i].exp+=c->exp;
	intif_guild_change_memberinfo(g->guild_id,c->account_id,c->char_id,
		GMI_EXP,&g->member[i].exp,sizeof(g->member[i].exp));
	c->exp=0;

	dellist[(*delp)++]=dataid;
	free(c);
	return 0;
}
int guild_payexp_timer(int tid,unsigned int tick,int id,int data)
{
	int dellist[GUILD_PAYEXP_LIST],delp=0,i;
	numdb_foreach(guild_expcache_db,guild_payexp_timer_sub,
		dellist,&delp);
	for(i=0;i<delp;i++)
		numdb_erase(guild_expcache_db,dellist[i]);
//	if(battle_config.etc_log)
//		printf("guild exp %d charactor's exp flushed !\n",delp);
	return 0;
}

//------------------------------------------------------------------------

// �쐬�v��
int guild_create(struct map_session_data *sd,char *name)
{
	nullpo_retr(0, sd);

	if(sd->status.guild_id==0){
		if(!battle_config.guild_emperium_check || pc_search_inventory(sd,714) >= 0) {
			struct guild_member m;
			guild_makemember(&m,sd);
			m.position=0;
			intif_guild_create(name,&m);
		} else
			clif_guild_created(sd,3);	// �G���y���E�������Ȃ�
	}else
		clif_guild_created(sd,1);	// ���łɏ������Ă���

	return 0;
}

// �쐬�”�
int guild_created(int account_id,int guild_id)
{
	struct map_session_data *sd=map_id2sd(account_id);

	if(sd==NULL)
		return 0;
	if(guild_id>0) {
			struct guild *g;
			sd->status.guild_id=guild_id;
			sd->guild_sended=0;
			if((g=numdb_search(guild_db,guild_id))!=NULL){
				printf("guild: id already exists!\n");
				exit(1);
			}
			clif_guild_created(sd,0);
			if(battle_config.guild_emperium_check)
				pc_delitem(sd,pc_search_inventory(sd,714),1,0);	// �G���y���E������
	} else {
		clif_guild_created(sd,2);	// �쐬���s�i�����M���h���݁j
	}
	return 0;
}

// ���v��
int guild_request_info(int guild_id)
{
//	if(battle_config.etc_log)
//		printf("guild_request_info\n");
	return intif_guild_request_info(guild_id);
}
// �C�x���g�t�����v��
int guild_npc_request_info(int guild_id,const char *event)
{
	struct eventlist *ev;

	if( guild_search(guild_id) ){
		if(event && *event)
			npc_event_do(event);
		return 0;
	}

	if(event==NULL || *event==0)
		return guild_request_info(guild_id);

	ev=(struct eventlist *)aCalloc(1,sizeof(struct eventlist));
	memcpy(ev->name,event,strlen(event));
	ev->next=(struct eventlist *)numdb_search(guild_infoevent_db,guild_id);
	numdb_insert(guild_infoevent_db,guild_id,ev);
	return guild_request_info(guild_id);
}

// �����L�����̊m�F
int guild_check_member(const struct guild *g)
{
	int i;
	struct map_session_data *sd;

	nullpo_retr(0, g);

	for(i=0;i<fd_max;i++){
		if(session[i] && (sd=session[i]->session_data) && sd->state.auth){
			if(sd->status.guild_id==g->guild_id){
				int j,f=1;
				for(j=0;j<MAX_GUILD;j++){	// �f�[�^�����邩
					if(	g->member[j].account_id==sd->status.account_id &&
						g->member[j].char_id==sd->status.char_id)
						f=0;
				}
				if(f){
					sd->status.guild_id=0;
					sd->guild_sended=0;
					sd->guild_emblem_id=0;
					if(battle_config.error_log)
						printf("guild: check_member %d[%s] is not member\n",sd->status.account_id,sd->status.name);
				}
			}
		}
	}
	return 0;
}
// ��񏊓����s�i����ID�̃L������S���������ɂ���j
int guild_recv_noinfo(int guild_id)
{
	int i;
	struct map_session_data *sd;
	for(i=0;i<fd_max;i++){
		if(session[i] && (sd=session[i]->session_data) && sd->state.auth){
			if(sd->status.guild_id==guild_id)
				sd->status.guild_id=0;
		}
	}
	return 0;
}
// ��񏊓�
int guild_recv_info(struct guild *sg)
{
	struct guild *g,before;
	int i,bm,m;
	struct eventlist *ev,*ev2;

	nullpo_retr(0, sg);

	if((g=numdb_search(guild_db,sg->guild_id))==NULL){
		g=(struct guild *)aCalloc(1,sizeof(struct guild));
		numdb_insert(guild_db,sg->guild_id,g);
		before=*sg;

		// �ŏ��̃��[�h�Ȃ̂Ń��[�U�[�̃`�F�b�N���s��
		guild_check_member(sg);
	}else
		before=*g;
	memcpy(g,sg,sizeof(struct guild));

	for(i=bm=m=0;i<g->max_member;i++){	// sd�̐ݒ�Ɛl���̊m�F
		if(g->member[i].account_id>0){
			struct map_session_data *sd = map_id2sd(g->member[i].account_id);
			g->member[i].sd=(sd!=NULL &&
				sd->status.char_id==g->member[i].char_id &&
				sd->status.guild_id==g->guild_id)?	sd:NULL;
			m++;
		}else
			g->member[i].sd=NULL;
		if(before.member[i].account_id>0)
			bm++;
	}

	for(i=0;i<g->max_member;i++){	// ���̑��M
		struct map_session_data *sd = g->member[i].sd;
		if( sd==NULL )
			continue;

		if(	before.guild_lv!=g->guild_lv || bm!=m ||
			before.max_member!=g->max_member ){
			clif_guild_basicinfo(sd);	// ��{��񑗐M
			clif_guild_emblem(sd,g);	// �G���u�������M
		}

		if(bm!=m){		// �����o�[��񑗐M
			clif_guild_memberlist(g->member[i].sd);
		}

		if( before.skill_point!=g->skill_point)
			clif_guild_skillinfo(sd);	// �X�L����񑗐M

		if( sd->guild_sended==0){	// �����M�Ȃ珊����������
			clif_guild_belonginfo(sd,g);
			clif_guild_notice(sd,g);
			sd->guild_emblem_id=g->emblem_id;
			sd->guild_sended=1;
		}
	}

	// �C�x���g�̔���
	if( (ev=numdb_search(guild_infoevent_db,sg->guild_id))!=NULL ){
		numdb_erase(guild_infoevent_db,sg->guild_id);
		for(;ev;ev2=ev->next,free(ev),ev=ev2){
			npc_event_do(ev->name);
		}
	}

	return 0;
}


// �M���h�ւ̊��U
int guild_invite(struct map_session_data *sd,int account_id)
{
	struct map_session_data *tsd;
	struct guild *g;
	int i;

	nullpo_retr(0, sd);

	tsd= map_id2sd(account_id);
	g=guild_search(sd->status.guild_id);

	if(tsd==NULL || g==NULL)
		return 0;
	if(!battle_config.invite_request_check) {
		if (tsd->party_invite>0 || tsd->trade_partner) {	// ���肪��������ǂ���
			clif_guild_inviteack(sd,0);
			return 0;
		}
	}
	if( tsd->status.guild_id>0 || tsd->guild_invite>0 ){	// ����̏����m�F
		clif_guild_inviteack(sd,0);
		return 0;
	}

	// ����m�F
	for(i=0;i<g->max_member;i++)
		if(g->member[i].account_id==0)
			break;
	if(i==g->max_member){
		clif_guild_inviteack(sd,3);
		return 0;
	}

	tsd->guild_invite=sd->status.guild_id;
	tsd->guild_invite_account=sd->status.account_id;

	clif_guild_invite(tsd,g);
	return 0;
}
// �M���h���U�ւ̕ԓ�
int guild_reply_invite(struct map_session_data *sd,int guild_id,int flag)
{
	struct map_session_data *tsd;

	nullpo_retr(0, sd);
	nullpo_retr(0, tsd= map_id2sd( sd->guild_invite_account ));

	if(sd->guild_invite!=guild_id)	// ���U�ƃM���hID���Ⴄ
		return 0;

	if(flag==1){	// ����
		struct guild_member m;
		struct guild *g;
		int i;

		// ����m�F
		if( (g=guild_search(tsd->status.guild_id))==NULL ){
			sd->guild_invite=0;
			sd->guild_invite_account=0;
			return 0;
		}
		for(i=0;i<g->max_member;i++)
			if(g->member[i].account_id==0)
				break;
		if(i==g->max_member){
			sd->guild_invite=0;
			sd->guild_invite_account=0;
			clif_guild_inviteack(tsd,3);
			return 0;
		}


		//inter�I�֒lj��v��
		guild_makemember(&m,sd);
		intif_guild_addmember( sd->guild_invite, &m );
		return 0;
	}else{		// ����
		sd->guild_invite=0;
		sd->guild_invite_account=0;
		if(tsd==NULL)
			return 0;
		clif_guild_inviteack(tsd,1);
	}
	return 0;
}
// �M���h�����o���lj����ꂽ
int guild_member_added(int guild_id,int account_id,int char_id,int flag)
{
	struct map_session_data *sd= map_id2sd(account_id),*sd2;
	struct guild *g;

	if( (g=guild_search(guild_id))==NULL )
		return 0;

	if((sd==NULL || sd->guild_invite==0) && flag==0){
		// �L�������ɓo�^�ł��Ȃ��������ߒE�ޗv�����o��
		if(battle_config.error_log)
			printf("guild: member added error %d is not online\n",account_id);
 		intif_guild_leave(guild_id,account_id,char_id,0,"**�o�^���s**");
		return 0;
	}
	sd->guild_invite=0;
	sd->guild_invite_account=0;

	sd2=map_id2sd(sd->guild_invite_account);

	if(flag==1){	// ���s
		if( sd2!=NULL )
			clif_guild_inviteack(sd2,3);
		return 0;
	}

		// ����
	sd->guild_sended=0;
	sd->status.guild_id=guild_id;

	if( sd2!=NULL )
		clif_guild_inviteack(sd2,2);

	// �������������m�F
	guild_check_conflict(sd);

	return 0;
}

// �M���h�E�ޗv��
int guild_leave(struct map_session_data *sd,int guild_id,
	int account_id,int char_id,const char *mes)
{
	struct guild *g;
	int i;

	nullpo_retr(0, sd);

	g = guild_search(sd->status.guild_id);

	if(g==NULL)
		return 0;

	if(	sd->status.account_id!=account_id ||
		sd->status.char_id!=char_id || sd->status.guild_id!=guild_id)
		return 0;

	for(i=0;i<g->max_member;i++){	// �������Ă��邩
		if(	g->member[i].account_id==sd->status.account_id &&
			g->member[i].char_id==sd->status.char_id ){
			intif_guild_leave(g->guild_id,sd->status.account_id,sd->status.char_id,0,mes);
			return 0;
		}
	}
	return 0;
}
// �M���h�Ǖ��v��
int guild_explusion(struct map_session_data *sd,int guild_id,
	int account_id,int char_id,const char *mes)
{
	struct guild *g;
	int i,ps;

	nullpo_retr(0, sd);

	g = guild_search(sd->status.guild_id);

	if(g==NULL)
		return 0;

	if(	sd->status.guild_id!=guild_id)
		return 0;

	if( (ps=guild_getposition(sd,g))<0 || !(g->position[ps].mode&0x0010) )
		return 0;	// ������������

	for(i=0;i<g->max_member;i++){	// �������Ă��邩
		if(	g->member[i].account_id==account_id &&
			g->member[i].char_id==char_id ){
			intif_guild_leave(g->guild_id,account_id,char_id,1,mes);
			return 0;
		}
	}
	return 0;
}
// �M���h�����o���E�ނ���
int guild_member_leaved(int guild_id,int account_id,int char_id,int flag,
	const char *name,const char *mes)
{
	struct map_session_data *sd=map_id2sd(account_id);
	struct guild *g=guild_search(guild_id);
	int i;

	if(g!=NULL){
		int i;
		for(i=0;i<g->max_member;i++)
			if(	g->member[i].account_id==account_id &&
				g->member[i].char_id==char_id ){
				struct map_session_data *sd2=sd;
				if(sd2==NULL)
					sd2=guild_getavailablesd(g);
				else
				{
					if(flag==0)
						clif_guild_leave(sd2,name,mes);
					else
						clif_guild_explusion(sd2,name,mes,account_id);
				}
				g->member[i].account_id=0;
				g->member[i].sd=NULL;
			}
	}
	if(sd!=NULL && sd->status.guild_id==guild_id){
		sd->status.guild_id=0;
		sd->guild_emblem_id=0;
		sd->guild_sended=0;
	}

	// �����o�[���X�g��S���ɍĒʒm
	for(i=0;i<g->max_member;i++){
		if( g->member[i].sd!=NULL )
			clif_guild_memberlist(g->member[i].sd);
	}

	return 0;
}
// �M���h�����o�̃I�����C�����/Lv�X�V���M
int guild_send_memberinfoshort(struct map_session_data *sd,int online)
{
	struct guild *g;

	nullpo_retr(0, sd);

	if(sd->status.guild_id<=0)
		return 0;
	g=guild_search(sd->status.guild_id);
	if(g==NULL)
		return 0;

	intif_guild_memberinfoshort(g->guild_id,
		sd->status.account_id,sd->status.char_id,online,sd->status.base_level,sd->status.class);

	if( !online ){	// ���O�A�E�g����Ȃ�sd���N���A���ďI��
		int i=guild_getindex(g,sd->status.account_id,sd->status.char_id);
		if(i>=0)
			g->member[i].sd=NULL;
		return 0;
	}

	if( sd->guild_sended!=0 )	// �M���h�������M�f�[�^�͑��M�ς�
		return 0;

	// �����m�F
	guild_check_conflict(sd);

	// ����Ȃ�M���h�������M�f�[�^���M
	if( (g=guild_search(sd->status.guild_id))!=NULL ){
		guild_check_member(g);	// �������m�F����
		if(sd->status.guild_id==g->guild_id){
			clif_guild_belonginfo(sd,g);
			clif_guild_notice(sd,g);
			sd->guild_sended=1;
			sd->guild_emblem_id=g->emblem_id;
		}
	}
	return 0;
}
// �M���h�����o�̃I�����C�����/Lv�X�V�ʒm
int guild_recv_memberinfoshort(int guild_id,int account_id,int char_id,int online,int lv,int class)
{
	int i,alv,c,idx=0,om=0,oldonline=-1;
	struct guild *g=guild_search(guild_id);
	if(g==NULL)
		return 0;
	for(i=0,alv=0,c=0,om=0;i<g->max_member;i++){
		struct guild_member *m=&g->member[i];
		if(m->account_id==account_id && m->char_id==char_id ){
			oldonline=m->online;
			m->online=online;
			m->lv=lv;
			m->class=class;
			idx=i;
		}
		if(m->account_id>0){
			alv+=m->lv;
			c++;
		}
		if(m->online)
			om++;
	}
	if(idx==g->max_member){
		if(battle_config.error_log)
			printf("guild: not found member %d,%d on %d[%s]\n",	account_id,char_id,guild_id,g->name);
		return 0;
	}
	g->average_lv=alv/c;
	g->connect_member=om;

	if(oldonline!=online)	// �I�����C����Ԃ��ς�����̂Œʒm
		clif_guild_memberlogin_notice(g,idx,online);

	for(i=0;i<g->max_member;i++){	// sd�Đݒ�
		struct map_session_data *sd= map_id2sd(g->member[i].account_id);
		g->member[i].sd=(sd!=NULL &&
			sd->status.char_id==g->member[i].char_id &&
			sd->status.guild_id==guild_id)?sd:NULL;
	}

	// �����ɃN���C�A���g�ɑ��M�������K�v

	return 0;
}
// �M���h��b���M
int guild_send_message(struct map_session_data *sd,char *mes,int len)
{
	nullpo_retr(0, sd);

	if(sd->status.guild_id==0)
		return 0;
	intif_guild_message(sd->status.guild_id,sd->status.account_id,mes,len);
	return 0;
}
// �M���h��b��M
int guild_recv_message(int guild_id,int account_id,char *mes,int len)
{
	struct guild *g;
	if( (g=guild_search(guild_id))==NULL)
		return 0;
	clif_guild_message(g,account_id,mes,len);
	return 0;
}
// �M���h�����o�̖�E�ύX
int guild_change_memberposition(int guild_id,int account_id,int char_id,int idx)
{
	return intif_guild_change_memberinfo(
		guild_id,account_id,char_id,GMI_POSITION,&idx,sizeof(idx));
}
// �M���h�����o�̖�E�ύX�ʒm
int guild_memberposition_changed(struct guild *g,int idx,int pos)
{
	nullpo_retr(0, g);

	g->member[idx].position=pos;
	clif_guild_memberpositionchanged(g,idx);
	return 0;
}
// �M���h��E�ύX
int guild_change_position(struct map_session_data *sd,int idx,
	int mode,int exp_mode,const char *name)
{
	struct guild_position p;

	nullpo_retr(0, sd);

	if(exp_mode>battle_config.guild_exp_limit)
		exp_mode=battle_config.guild_exp_limit;
	if(exp_mode<0)exp_mode=0;
	p.mode=mode;
	p.exp_mode=exp_mode;
	memcpy(p.name,name,24);
	return intif_guild_position(sd->status.guild_id,idx,&p);
}
// �M���h��E�ύX�ʒm
int guild_position_changed(int guild_id,int idx,struct guild_position *p)
{
	struct guild *g=guild_search(guild_id);
	if(g==NULL)
		return 0;
	memcpy(&g->position[idx],p,sizeof(struct guild_position));
	clif_guild_positionchanged(g,idx);
	return 0;
}
// �M���h���m�ύX
int guild_change_notice(struct map_session_data *sd,int guild_id,const char *mes1,const char *mes2)
{
	nullpo_retr(0, sd);

	if(guild_id!=sd->status.guild_id)
		return 0;
	return intif_guild_notice(guild_id,mes1,mes2);
}
// �M���h���m�ύX�ʒm
int guild_notice_changed(int guild_id,const char *mes1,const char *mes2)
{
	int i;
	struct map_session_data *sd;
	struct guild *g=guild_search(guild_id);
	if(g==NULL)
		return 0;

	memcpy(g->mes1,mes1,60);
	memcpy(g->mes2,mes2,120);

	for(i=0;i<g->max_member;i++){
		if((sd=g->member[i].sd)!=NULL)
			clif_guild_notice(sd,g);
	}
	return 0;
}
// �M���h�G���u�����ύX
int guild_change_emblem(struct map_session_data *sd,int len,const char *data)
{
	struct guild *g = NULL;

	nullpo_retr(0, sd);

	/* Temporarily only for TXT until there's proper char server support [Celest] */
	#ifdef TXT_ONLY
		if ((g = guild_search(sd->status.guild_id)) && guild_checkskill(g, GD_GLORYGUILD)>0)
			return intif_guild_emblem(sd->status.guild_id,len,data);

		clif_skill_fail(sd,GD_GLORYGUILD,0,0);
	#else
		if ((g = guild_search(sd->status.guild_id)))
			return intif_guild_emblem(sd->status.guild_id,len,data);
	#endif		
	return 0;
}
// �M���h�G���u�����ύX�ʒm
int guild_emblem_changed(int len,int guild_id,int emblem_id,const char *data)
{
	int i;
	struct map_session_data *sd;
	struct guild *g=guild_search(guild_id);
	if(g==NULL)
		return 0;

	memcpy(g->emblem_data,data,len);
	g->emblem_len=len;
	g->emblem_id=emblem_id;

	for(i=0;i<g->max_member;i++){
		if((sd=g->member[i].sd)!=NULL){
			sd->guild_emblem_id=emblem_id;
			clif_guild_belonginfo(sd,g);
			clif_guild_emblem(sd,g);
		}
	}
	return 0;
}

// �M���h��EXP��[
int guild_payexp(struct map_session_data *sd,int exp)
{
	struct guild *g;
	struct guild_expcache *c;
	int per,exp2;

	nullpo_retr(0, sd);

	if(sd->status.guild_id==0 || (g=guild_search(sd->status.guild_id))==NULL )
		return 0;
	if( (per=g->position[guild_getposition(sd,g)].exp_mode)<=0 )
		return 0;
	if( per>100 )per=100;

	if( (exp2=exp*per/100)<=0 )
		return 0;

	if( (c=numdb_search(guild_expcache_db,sd->status.char_id))==NULL ){
		c=(struct guild_expcache *)aCalloc(1,sizeof(struct guild_expcache));
		c->guild_id=sd->status.guild_id;
		c->account_id=sd->status.account_id;
		c->char_id=sd->status.char_id;
		c->exp=exp2;
		numdb_insert(guild_expcache_db,c->char_id,c);
	}else{
		c->exp+=exp2;
	}
	return exp2;
}

// Celest
int guild_getexp(struct map_session_data *sd,int exp)
{
	struct guild *g;
	struct guild_expcache *c;

	nullpo_retr(0, sd);

	if(sd->status.guild_id==0 || (g=guild_search(sd->status.guild_id))==NULL )
		return 0;
	
	if( (c=numdb_search(guild_expcache_db,sd->status.char_id))==NULL ){
		c=(struct guild_expcache *)aCalloc(1,sizeof(struct guild_expcache));
		c->guild_id=sd->status.guild_id;
		c->account_id=sd->status.account_id;
		c->char_id=sd->status.char_id;
		c->exp=exp;
		numdb_insert(guild_expcache_db,c->char_id,c);
	}else{
		c->exp+=exp;
	}
	return exp;
}

// �X�L���|�C���g����U��
int guild_skillup(struct map_session_data *sd,int skill_num,int flag)
{
	struct guild *g;
	int idx = skill_num - GD_SKILLBASE;

	nullpo_retr(0, sd);

	if(idx < 0 || idx >= MAX_GUILDSKILL)

		return 0;
	if(sd->status.guild_id==0 || (g=guild_search(sd->status.guild_id))==NULL)
		return 0;
	if(strcmp(sd->status.name,g->master))
		return 0;

	if( (g->skill_point>0 || flag&1) &&
		g->skill[idx].id!=0 &&
		g->skill[idx].lv < guild_skill_get_max(skill_num) ){
		intif_guild_skillup(g->guild_id,skill_num,sd->status.account_id,flag);
	}
	pc_calcstatus (sd, 0); // Celest

	return 0;
}
// �X�L���|�C���g����U��ʒm
int guild_skillupack(int guild_id,int skill_num,int account_id)
{
	struct map_session_data *sd=map_id2sd(account_id);
	struct guild *g=guild_search(guild_id);
	int i;
	if(g==NULL)
		return 0;
	if(sd!=NULL)
		clif_guild_skillup(sd,skill_num,g->skill[skill_num-GD_SKILLBASE].lv);
	// �S���ɒʒm
	for(i=0;i<g->max_member;i++)
		if((sd=g->member[i].sd)!=NULL)
			clif_guild_skillinfo(sd);
	return 0;
}

// �M���h����������
int guild_get_alliance_count(struct guild *g,int flag)
{
	int i,c;

	nullpo_retr(0, g);

	for(i=c=0;i<MAX_GUILDALLIANCE;i++){
		if(	g->alliance[i].guild_id>0 &&
			g->alliance[i].opposition==flag )
			c++;
	}
	return c;
}
// �M���h�����v��
int guild_reqalliance(struct map_session_data *sd,int account_id)
{
	struct map_session_data *tsd= map_id2sd(account_id);
	struct guild *g[2];
	int i;

	if(agit_flag)	{	// Disable alliance creation during woe [Valaris]
		clif_displaymessage(sd->fd,"Alliances cannot be made during Guild Wars!");
		return 0;
	}	// end addition [Valaris]


	nullpo_retr(0, sd);

	if(tsd==NULL || tsd->status.guild_id<=0)
		return 0;

	g[0]=guild_search(sd->status.guild_id);
	g[1]=guild_search(tsd->status.guild_id);

	if(g[0]==NULL || g[1]==NULL)
		return 0;

	if( guild_get_alliance_count(g[0],0)>3 )	// �������m�F
		clif_guild_allianceack(sd,4);
	if( guild_get_alliance_count(g[1],0)>3 )
		clif_guild_allianceack(sd,3);

	if( tsd->guild_alliance>0 ){	// ���肪�����v����Ԃ��ǂ����m�F
		clif_guild_allianceack(sd,1);
		return 0;
	}

	for(i=0;i<MAX_GUILDALLIANCE;i++){	// ���łɓ�����Ԃ��m�F
		if(	g[0]->alliance[i].guild_id==tsd->status.guild_id &&
			g[0]->alliance[i].opposition==0){
			clif_guild_allianceack(sd,0);
			return 0;
		}
	}

	tsd->guild_alliance=sd->status.guild_id;
	tsd->guild_alliance_account=sd->status.account_id;

	clif_guild_reqalliance(tsd,sd->status.account_id,g[0]->name);
	return 0;
}
// �M���h���U�ւ̕ԓ�
int guild_reply_reqalliance(struct map_session_data *sd,int account_id,int flag)
{
	struct map_session_data *tsd;

	nullpo_retr(0, sd);
	nullpo_retr(0, tsd= map_id2sd( account_id ));

	if(sd->guild_alliance!=tsd->status.guild_id)	// ���U�ƃM���hID���Ⴄ
		return 0;

	if(flag==1){	// ����
		int i;

		struct guild *g;	// �������Ċm�F
		if( (g=guild_search(sd->status.guild_id))==NULL ||
			guild_get_alliance_count(g,0)>3 ){
			clif_guild_allianceack(sd,4);
			clif_guild_allianceack(tsd,3);
			return 0;
		}
		if( (g=guild_search(tsd->status.guild_id))==NULL ||
			guild_get_alliance_count(g,0)>3 ){
			clif_guild_allianceack(sd,3);
			clif_guild_allianceack(tsd,4);
			return 0;
		}

		// �G�Ί֌W�Ȃ�G�΂��~�߂�
		if((g=guild_search(sd->status.guild_id)) == NULL)
			return 0;
		for(i=0;i<MAX_GUILDALLIANCE;i++){
			if(	g->alliance[i].guild_id==tsd->status.guild_id &&
				g->alliance[i].opposition==1)
				intif_guild_alliance( sd->status.guild_id,tsd->status.guild_id,
					sd->status.account_id,tsd->status.account_id,9 );
		}
		if((g=guild_search(tsd->status.guild_id)) == NULL)
			return 0;
		for(i=0;i<MAX_GUILDALLIANCE;i++){
			if(	g->alliance[i].guild_id==sd->status.guild_id &&
				g->alliance[i].opposition==1)
				intif_guild_alliance( tsd->status.guild_id,sd->status.guild_id,
					tsd->status.account_id,sd->status.account_id,9 );
		}

		// inter�I�֓����v��
		intif_guild_alliance( sd->status.guild_id,tsd->status.guild_id,
			sd->status.account_id,tsd->status.account_id,0 );
		return 0;
	}else{		// ����
		sd->guild_alliance=0;
		sd->guild_alliance_account=0;
		if(tsd!=NULL)
			clif_guild_allianceack(tsd,3);
	}
	return 0;
}
// �M���h�֌W����
int guild_delalliance(struct map_session_data *sd,int guild_id,int flag)
{
	if(agit_flag)	{	// Disable alliance breaking during woe [Valaris]
		clif_displaymessage(sd->fd,"Alliances cannot be broken during Guild Wars!");
		return 0;
	}	// end addition [Valaris]

	nullpo_retr(0, sd);

	intif_guild_alliance( sd->status.guild_id,guild_id,
		sd->status.account_id,0,flag|8 );
	return 0;
}
// �M���h�G��
int guild_opposition(struct map_session_data *sd,int char_id)
{
	struct map_session_data *tsd=map_id2sd(char_id);
	struct guild *g;
	int i;

	nullpo_retr(0, sd);

	g=guild_search(sd->status.guild_id);
	if(g==NULL || tsd==NULL)
		return 0;

	if( guild_get_alliance_count(g,1)>3 )	// �G�ΐ��m�F
		clif_guild_oppositionack(sd,1);

	for(i=0;i<MAX_GUILDALLIANCE;i++){	// ���łɊ֌W�������Ă��邩�m�F
		if(g->alliance[i].guild_id==tsd->status.guild_id){
			if(g->alliance[i].opposition==1){	// ���łɓG��
				clif_guild_oppositionack(sd,2);
				return 0;
			}else	// �����j��
				intif_guild_alliance( sd->status.guild_id,tsd->status.guild_id,
					sd->status.account_id,tsd->status.account_id,8 );
		}
	}

	// inter�I�ɓG�Ηv��
	intif_guild_alliance( sd->status.guild_id,tsd->status.guild_id,
			sd->status.account_id,tsd->status.account_id,1 );
	return 0;
}
// �M���h����/�G�Βʒm
int guild_allianceack(int guild_id1,int guild_id2,int account_id1,int account_id2,
	int flag,const char *name1,const char *name2)
{
	struct guild *g[2];
	int guild_id[2];
	const char *guild_name[2];
	struct map_session_data *sd[2];
	int j,i;
	
	guild_id[0] = guild_id1;
	guild_id[1] = guild_id2;
	guild_name[0] = name1;
	guild_name[1] = name2;
	sd[0] = map_id2sd(account_id1);
	sd[1] = map_id2sd(account_id2);

	g[0]=guild_search(guild_id1);
	g[1]=guild_search(guild_id2);

	if(sd[0]!=NULL && (flag&0x0f)==0){
		sd[0]->guild_alliance=0;
		sd[0]->guild_alliance_account=0;
	}

	if(flag&0x70){	// ���s
		for(i=0;i<2-(flag&1);i++)
			if( sd[i]!=NULL )
				clif_guild_allianceack(sd[i],((flag>>4)==i+1)?3:4);
		return 0;
	}
//	if(battle_config.etc_log)
//		printf("guild alliance_ack %d %d %d %d %d %s %s\n",guild_id1,guild_id2,account_id1,account_id2,flag,name1,name2);

	if(!(flag&0x08)){	// �֌W�lj�
		for(i=0;i<2-(flag&1);i++)
			if(g[i]!=NULL)
				for(j=0;j<MAX_GUILDALLIANCE;j++)
					if(g[i]->alliance[j].guild_id==0){
						g[i]->alliance[j].guild_id=guild_id[1-i];
						memcpy(g[i]->alliance[j].name,guild_name[1-i],24);
						g[i]->alliance[j].opposition=flag&1;
						break;
					}
	}else{				// �֌W����
		for(i=0;i<2-(flag&1);i++){
			if(g[i]!=NULL)
				for(j=0;j<MAX_GUILDALLIANCE;j++)
					if(	g[i]->alliance[j].guild_id==guild_id[1-i] &&
						g[i]->alliance[j].opposition==(flag&1)){
						g[i]->alliance[j].guild_id=0;
						break;
					}
			if( sd[i]!=NULL )	// �����ʒm
				clif_guild_delalliance(sd[i],guild_id[1-i],(flag&1));
		}
	}

	if((flag&0x0f)==0){			// �����ʒm
		if( sd[1]!=NULL )
			clif_guild_allianceack(sd[1],2);
	}else if((flag&0x0f)==1){	// �G�Βʒm
		if( sd[0]!=NULL )
			clif_guild_oppositionack(sd[0],0);
	}


	for(i=0;i<2-(flag&1);i++){	// ����/�G�΃��X�g�̍đ��M
		struct map_session_data *sd;
		if(g[i]!=NULL)
			for(j=0;j<g[i]->max_member;j++)
				if((sd=g[i]->member[j].sd)!=NULL)
					clif_guild_allianceinfo(sd);
	}
	return 0;
}
// �M���h���U�ʒm�p
int guild_broken_sub(void *key,void *data,va_list ap)
{
	struct guild *g=(struct guild *)data;
	int guild_id=va_arg(ap,int);
	int i,j;
	struct map_session_data *sd=NULL;

	nullpo_retr(0, g);

	for(i=0;i<MAX_GUILDALLIANCE;i++){	// �֌W��j��
		if(g->alliance[i].guild_id==guild_id){
			for(j=0;j<g->max_member;j++)
				if( (sd=g->member[j].sd)!=NULL )
					clif_guild_delalliance(sd,guild_id,g->alliance[i].opposition);
			g->alliance[i].guild_id=0;
		}
	}
	return 0;
}
// �M���h���U�ʒm
int guild_broken(int guild_id,int flag)
{
	struct guild *g=guild_search(guild_id);
	struct map_session_data *sd;
	int i;
	if(flag!=0 || g==NULL)
		return 0;

	for(i=0;i<g->max_member;i++){	// �M���h���U��ʒm
		if((sd=g->member[i].sd)!=NULL){
			if(sd->state.storage_flag)
				storage_guild_storage_quit(sd,1);
			sd->status.guild_id=0;
			sd->guild_sended=0;
			clif_guild_broken(g->member[i].sd,0);
		}
	}

	numdb_foreach(guild_db,guild_broken_sub,guild_id);
	numdb_erase(guild_db,guild_id);
	guild_storage_delete(guild_id);
	free(g);
	return 0;
}

// �M���h���U
int guild_break(struct map_session_data *sd,char *name)
{
	struct guild *g;
	int i;

	nullpo_retr(0, sd);

	if( (g=guild_search(sd->status.guild_id))==NULL )
		return 0;
	if(strcmp(g->name,name)!=0)
		return 0;
	if(strcmp(sd->status.name,g->master)!=0)
		return 0;
	for(i=0;i<g->max_member;i++){
		if(	g->member[i].account_id>0 && (
			g->member[i].account_id!=sd->status.account_id ||
			g->member[i].char_id!=sd->status.char_id ))
			break;
	}
	if(i<g->max_member){
		clif_guild_broken(sd,2);
		return 0;
	}

	intif_guild_break(g->guild_id);
	return 0;
}

// �M���h��f�[�^�v��
int guild_castledataload(int castle_id,int index)
{
	return intif_guild_castle_dataload(castle_id,index);
}
// �M���h���񏊓����C�x���g�lj�
int guild_addcastleinfoevent(int castle_id,int index,const char *name)
{
	struct eventlist *ev;
	int code=castle_id|(index<<16);

	if( name==NULL || *name==0 )
		return 0;

	ev=(struct eventlist *)aCalloc(1,sizeof(struct eventlist));
	memcpy(ev->name,name,sizeof(ev->name));
	ev->next=numdb_search(guild_castleinfoevent_db,code);
	numdb_insert(guild_castleinfoevent_db,code,ev);
	return 0;
}

// �M���h��f�[�^�v���ԐM
int guild_castledataloadack(int castle_id,int index,int value)
{
	struct guild_castle *gc=guild_castle_search(castle_id);
	int code=castle_id|(index<<16);
	struct eventlist *ev,*ev2;

	if(gc==NULL){
		return 0;
	}
	switch(index){
	case 1: gc->guild_id = value; break;
	case 2: gc->economy = value; break;
	case 3: gc->defense = value; break;
	case 4: gc->triggerE = value; break;
	case 5: gc->triggerD = value; break;
	case 6: gc->nextTime = value; break;
	case 7: gc->payTime = value; break;
	case 8: gc->createTime = value; break;
	case 9: gc->visibleC = value; break;
	case 10: gc->visibleG0 = value; break;
	case 11: gc->visibleG1 = value; break;
	case 12: gc->visibleG2 = value; break;
	case 13: gc->visibleG3 = value; break;
	case 14: gc->visibleG4 = value; break;
	case 15: gc->visibleG5 = value; break;
	case 16: gc->visibleG6 = value; break;
	case 17: gc->visibleG7 = value; break;
	case 18: gc->Ghp0 = value; break;	// guardian HP [Valaris]
	case 19: gc->Ghp1 = value; break;
	case 20: gc->Ghp2 = value; break;
	case 21: gc->Ghp3 = value; break;
	case 22: gc->Ghp4 = value; break;
	case 23: gc->Ghp5 = value; break;
	case 24: gc->Ghp6 = value; break;
	case 25: gc->Ghp7 = value; break;	// end additions [Valaris]
	default:
		printf("guild_castledataloadack ERROR!! (Not found index=%d)\n", index);
		return 0;
	}
	if( (ev=numdb_search(guild_castleinfoevent_db,code))!=NULL ){
		numdb_erase(guild_castleinfoevent_db,code);
		for(;ev;ev2=ev->next,free(ev),ev=ev2){
			npc_event_do(ev->name);
		}
	}
	return 1;
}
// �M���h��f�[�^�ύX�v��
int guild_castledatasave(int castle_id,int index,int value)
{
	return intif_guild_castle_datasave(castle_id,index,value);
}

// �M���h��f�[�^�ύX�ʒm
int guild_castledatasaveack(int castle_id,int index,int value)
{
	struct guild_castle *gc=guild_castle_search(castle_id);
	if(gc==NULL){
		return 0;
	}
	switch(index){
	case 1: gc->guild_id = value; break;
	case 2: gc->economy = value; break;
	case 3: gc->defense = value; break;
	case 4: gc->triggerE = value; break;
	case 5: gc->triggerD = value; break;
	case 6: gc->nextTime = value; break;
	case 7: gc->payTime = value; break;
	case 8: gc->createTime = value; break;
	case 9: gc->visibleC = value; break;
	case 10: gc->visibleG0 = value; break;
	case 11: gc->visibleG1 = value; break;
	case 12: gc->visibleG2 = value; break;
	case 13: gc->visibleG3 = value; break;
	case 14: gc->visibleG4 = value; break;
	case 15: gc->visibleG5 = value; break;
	case 16: gc->visibleG6 = value; break;
	case 17: gc->visibleG7 = value; break;
	case 18: gc->Ghp0 = value; break;	// guardian HP [Valaris]
	case 19: gc->Ghp1 = value; break;
	case 20: gc->Ghp2 = value; break;
	case 21: gc->Ghp3 = value; break;
	case 22: gc->Ghp4 = value; break;
	case 23: gc->Ghp5 = value; break;
	case 24: gc->Ghp6 = value; break;
	case 25: gc->Ghp7 = value; break;	// end additions [Valaris]
	default:
		printf("guild_castledatasaveack ERROR!! (Not found index=%d)\n", index);
		return 0;
	}
	return 1;
}

// �M���h�f�[�^�ꊇ��M�i���������j
int guild_castlealldataload(int len,struct guild_castle *gc)
{
	int i;
	int n = (len-4) / sizeof(struct guild_castle), ev = -1;

	nullpo_retr(0, gc);

	// �C�x���g�t���ŗv������f�[�^�ʒu��T��(�Ō�̐苒�f�[�^)
	for(i = 0; i < n; i++) {
		if ((gc + i)->guild_id)
			ev = i;
	}

	// ��f�[�^�i�[�ƃM���h���v��
	for(i = 0; i < n; i++, gc++) {
		struct guild_castle *c = guild_castle_search(gc->castle_id);
		if (!c) {
			printf("guild_castlealldataload ??\n");
			continue;
		}
		memcpy(&c->guild_id,&gc->guild_id,
			sizeof(struct guild_castle) - ((int)&c->guild_id - (int)c) );
		if( c->guild_id ){
			if(i!=ev)
				guild_request_info(c->guild_id);
			else
				guild_npc_request_info(c->guild_id, "::OnAgitInit");
		}
	}
	if (ev == -1)
		npc_event_doall("OnAgitInit");
	return 0;
}

int guild_agit_start(void)
{	// Run All NPC_Event[OnAgitStart]
	int c = npc_event_doall("OnAgitStart");
	printf("NPC_Event:[OnAgitStart] Run (%d) Events by @AgitStart.\n",c);
	return 0;
}

int guild_agit_end(void)
{	// Run All NPC_Event[OnAgitEnd]
	int c = npc_event_doall("OnAgitEnd");
	printf("NPC_Event:[OnAgitEnd] Run (%d) Events by @AgitEnd.\n",c);
	return 0;
}

int guild_gvg_eliminate_timer(int tid,unsigned int tick,int id,int data)
{	// Run One NPC_Event[OnAgitEliminate]
	size_t len = strlen((const char*)data);
	char *evname=(char*)aCalloc(len + 4,sizeof(char));
	int c=0;

	if(!agit_flag) return 0;	// Agit already End
	memcpy(evname,(const char *)data,len - 5);
	strcpy(evname + len - 5,"Eliminate");
	c = npc_event_do(evname);
	printf("NPC_Event:[%s] Run (%d) Events.\n",evname,c);
	return 0;
}

int guild_agit_break(struct mob_data *md)
{	// Run One NPC_Event[OnAgitBreak]
	char *evname;

	nullpo_retr(0, md);

	evname=(char *)aCalloc(strlen(md->npc_event) + 1, sizeof(char));

	strcpy(evname,md->npc_event);
// Now By User to Run [OnAgitBreak] NPC Event...
// It's a little impossible to null point with player disconnect in this!
// But Script will be stop, so nothing...
// Maybe will be changed in the futher..
//      int c = npc_event_do(evname);
	if(!agit_flag) return 0;	// Agit already End
	add_timer(gettick()+battle_config.gvg_eliminate_time,guild_gvg_eliminate_timer,md->bl.m,(int)evname);
	return 0;
}

// [MouseJstr]
//   How many castles does this guild have?
int guild_checkcastles(struct guild *g) {
	int i,nb_cas=0, id,cas_id=0;
	struct guild_castle *gc;
		id=g->guild_id;
	for(i=0;i<MAX_GUILDCASTLE;i++){
		gc=guild_castle_search(i);
		cas_id=gc->guild_id;
		if(g->guild_id==cas_id) 
			nb_cas=nb_cas+1;
		} //end for
	return nb_cas;
}

// [MouseJstr]
//    is this guild allied with this castle?
int guild_isallied(struct guild *g, struct guild_castle *gc)
{
	int i;

	nullpo_retr(0, g);

	if(g->guild_id == gc->guild_id)
		return 1;

	if (gc->guild_id == 0)
		return 0;


	for(i=0;i<MAX_GUILDALLIANCE;i++)
		if(g->alliance[i].guild_id == gc->guild_id) {
			if(g->alliance[i].opposition == 0)
				return 1;
			else
				return 0;
		}

	return 0;
}
  
static int guild_db_final(void *key,void *data,va_list ap)
{
	struct guild *g=data;

	free(g);

	return 0;
}
static int castle_db_final(void *key,void *data,va_list ap)
{
	struct guild_castle *gc=data;

	free(gc);

	return 0;
}
static int guild_expcache_db_final(void *key,void *data,va_list ap)
{
	struct guild_expcache *c=data;

	free(c);

	return 0;
}
static int guild_infoevent_db_final(void *key,void *data,va_list ap)
{
	struct eventlist *ev=data;

	free(ev);

	return 0;
}
void do_final_guild(void)
{
	if(guild_db)
		numdb_final(guild_db,guild_db_final);
	if(castle_db)
		numdb_final(castle_db,castle_db_final);
	if(guild_expcache_db)
		numdb_final(guild_expcache_db,guild_expcache_db_final);
	if(guild_infoevent_db)
		numdb_final(guild_infoevent_db,guild_infoevent_db_final);
	if(guild_castleinfoevent_db)
		numdb_final(guild_castleinfoevent_db,guild_infoevent_db_final);
}