guild.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972
  1. // Copyright (c) Athena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #include "../common/cbasetypes.h"
  4. #include "../common/timer.h"
  5. #include "../common/nullpo.h"
  6. #include "../common/malloc.h"
  7. #include "../common/mapindex.h"
  8. #include "../common/showmsg.h"
  9. #include "../common/ers.h"
  10. #include "../common/strlib.h"
  11. #include "../common/utils.h"
  12. #include "map.h"
  13. #include "guild.h"
  14. #include "storage.h"
  15. #include "battle.h"
  16. #include "npc.h"
  17. #include "pc.h"
  18. #include "status.h"
  19. #include "mob.h"
  20. #include "intif.h"
  21. #include "clif.h"
  22. #include "skill.h"
  23. #include "log.h"
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. static DBMap* guild_db; // int guild_id -> struct guild*
  28. static DBMap* castle_db; // int castle_id -> struct guild_castle*
  29. static DBMap* guild_expcache_db; // int char_id -> struct guild_expcache*
  30. static DBMap* guild_infoevent_db; // int guild_id -> struct eventlist*
  31. struct eventlist {
  32. char name[EVENT_NAME_LENGTH];
  33. struct eventlist *next;
  34. };
  35. // ギルドのEXPキャッシュのフラッシュに関連する定数
  36. #define GUILD_SEND_XY_INVERVAL 5000 // 座標やHP送信の間隔
  37. #define GUILD_PAYEXP_INVERVAL 10000 // 間隔(キャッシュの最大生存時間、ミリ秒)
  38. #define GUILD_PAYEXP_LIST 8192 // キャッシュの最大数
  39. // ギルドのEXPキャッシュ
  40. struct guild_expcache {
  41. int guild_id, account_id, char_id;
  42. uint64 exp;
  43. };
  44. #define MAX_GUILD_SKILL_REQUIRE 5
  45. struct{
  46. int id;
  47. int max;
  48. struct{
  49. short id;
  50. short lv;
  51. }need[MAX_GUILD_SKILL_REQUIRE];
  52. } guild_skill_tree[MAX_GUILDSKILL];
  53. int guild_payexp_timer(int tid, unsigned int tick, int id, intptr_t data);
  54. static int guild_send_xy_timer(int tid, unsigned int tick, int id, intptr_t data);
  55. /*==========================================
  56. * Retrieves and validates the sd pointer for this guild member [Skotlex]
  57. *------------------------------------------*/
  58. static TBL_PC* guild_sd_check(int guild_id, int account_id, int char_id)
  59. {
  60. TBL_PC* sd = map_id2sd(account_id);
  61. if (!(sd && sd->status.char_id == char_id))
  62. return NULL;
  63. if (sd->status.guild_id != guild_id)
  64. { //If player belongs to a different guild, kick him out.
  65. intif_guild_leave(guild_id,account_id,char_id,0,"** Guild Mismatch **");
  66. return NULL;
  67. }
  68. return sd;
  69. }
  70. // Modified [Komurka]
  71. int guild_skill_get_max (int id)
  72. {
  73. if (id < GD_SKILLBASE || id >= GD_SKILLBASE+MAX_GUILDSKILL)
  74. return 0;
  75. return guild_skill_tree[id-GD_SKILLBASE].max;
  76. }
  77. // ギルドスキルがあるか確認
  78. int guild_checkskill(struct guild *g,int id)
  79. {
  80. int idx = id-GD_SKILLBASE;
  81. if (idx < 0 || idx >= MAX_GUILDSKILL)
  82. return 0;
  83. return g->skill[idx].lv;
  84. }
  85. /*==========================================
  86. * guild_skill_tree.txt reading - from jA [Komurka]
  87. *------------------------------------------*/
  88. static bool guild_read_guildskill_tree_db(char* split[], int columns, int current)
  89. {// <skill id>,<max lv>,<req id1>,<req lv1>,<req id2>,<req lv2>,<req id3>,<req lv3>,<req id4>,<req lv4>,<req id5>,<req lv5>
  90. int k, id, skillid;
  91. skillid = atoi(split[0]);
  92. id = skillid - GD_SKILLBASE;
  93. if( id < 0 || id >= MAX_GUILDSKILL )
  94. {
  95. ShowWarning("guild_read_guildskill_tree_db: Invalid skill id %d.\n", skillid);
  96. return false;
  97. }
  98. guild_skill_tree[id].id = skillid;
  99. guild_skill_tree[id].max = atoi(split[1]);
  100. if( guild_skill_tree[id].id == GD_GLORYGUILD && battle_config.require_glory_guild && guild_skill_tree[id].max == 0 )
  101. {// enable guild's glory when required for emblems
  102. guild_skill_tree[id].max = 1;
  103. }
  104. for( k = 0; k < MAX_GUILD_SKILL_REQUIRE; k++ )
  105. {
  106. guild_skill_tree[id].need[k].id = atoi(split[k*2+2]);
  107. guild_skill_tree[id].need[k].lv = atoi(split[k*2+3]);
  108. }
  109. return true;
  110. }
  111. /*==========================================
  112. * Guild skill check - from jA [Komurka]
  113. *------------------------------------------*/
  114. int guild_check_skill_require(struct guild *g,int id)
  115. {
  116. int i;
  117. int idx = id-GD_SKILLBASE;
  118. if(g == NULL)
  119. return 0;
  120. if (idx < 0 || idx >= MAX_GUILDSKILL)
  121. return 0;
  122. for(i=0;i<MAX_GUILD_SKILL_REQUIRE;i++)
  123. {
  124. if(guild_skill_tree[idx].need[i].id == 0) break;
  125. if(guild_skill_tree[idx].need[i].lv > guild_checkskill(g,guild_skill_tree[idx].need[i].id))
  126. return 0;
  127. }
  128. return 1;
  129. }
  130. static bool guild_read_castledb(char* str[], int columns, int current)
  131. {// <castle id>,<map name>,<castle name>,<castle event>[,<reserved/unused switch flag>]
  132. struct guild_castle *gc;
  133. int mapindex = mapindex_name2id(str[1]);
  134. if (map_mapindex2mapid(mapindex) < 0) // Map not found or on another map-server
  135. return false;
  136. CREATE(gc, struct guild_castle, 1);
  137. gc->castle_id = atoi(str[0]);
  138. gc->mapindex = mapindex;
  139. safestrncpy(gc->castle_name, str[2], sizeof(gc->castle_name));
  140. safestrncpy(gc->castle_event, str[3], sizeof(gc->castle_event));
  141. idb_put(castle_db,gc->castle_id,gc);
  142. //intif_guild_castle_info(gc->castle_id);
  143. return true;
  144. }
  145. /// lookup: guild id -> guild*
  146. struct guild* guild_search(int guild_id)
  147. {
  148. return (struct guild*)idb_get(guild_db,guild_id);
  149. }
  150. /// lookup: guild name -> guild*
  151. struct guild* guild_searchname(char* str)
  152. {
  153. struct guild* g;
  154. DBIterator *iter = db_iterator(guild_db);
  155. for( g = dbi_first(iter); dbi_exists(iter); g = dbi_next(iter) )
  156. {
  157. if( strcmpi(g->name, str) == 0 )
  158. break;
  159. }
  160. dbi_destroy(iter);
  161. return g;
  162. }
  163. /// lookup: castle id -> castle*
  164. struct guild_castle* guild_castle_search(int gcid)
  165. {
  166. return (struct guild_castle*)idb_get(castle_db,gcid);
  167. }
  168. /// lookup: map index -> castle*
  169. struct guild_castle* guild_mapindex2gc(short mapindex)
  170. {
  171. struct guild_castle* gc;
  172. DBIterator *iter = db_iterator(castle_db);
  173. for( gc = dbi_first(iter); dbi_exists(iter); gc = dbi_next(iter) )
  174. {
  175. if( gc->mapindex == mapindex )
  176. break;
  177. }
  178. dbi_destroy(iter);
  179. return gc;
  180. }
  181. /// lookup: map name -> castle*
  182. struct guild_castle* guild_mapname2gc(const char* mapname)
  183. {
  184. return guild_mapindex2gc(mapindex_name2id(mapname));
  185. }
  186. struct map_session_data* guild_getavailablesd(struct guild* g)
  187. {
  188. int i;
  189. nullpo_retr(NULL, g);
  190. ARR_FIND( 0, g->max_member, i, g->member[i].sd != NULL );
  191. return( i < g->max_member ) ? g->member[i].sd : NULL;
  192. }
  193. /// lookup: player AID/CID -> member index
  194. int guild_getindex(struct guild *g,int account_id,int char_id)
  195. {
  196. int i;
  197. if( g == NULL )
  198. return -1;
  199. ARR_FIND( 0, g->max_member, i, g->member[i].account_id == account_id && g->member[i].char_id == char_id );
  200. return( i < g->max_member ) ? i : -1;
  201. }
  202. /// lookup: player sd -> member position
  203. int guild_getposition(struct guild* g, struct map_session_data* sd)
  204. {
  205. int i;
  206. if( g == NULL && (g=guild_search(sd->status.guild_id)) == NULL )
  207. return -1;
  208. ARR_FIND( 0, g->max_member, i, g->member[i].account_id == sd->status.account_id && g->member[i].char_id == sd->status.char_id );
  209. return( i < g->max_member ) ? g->member[i].position : -1;
  210. }
  211. // メンバー情報の作成
  212. void guild_makemember(struct guild_member *m,struct map_session_data *sd)
  213. {
  214. nullpo_retv(sd);
  215. memset(m,0,sizeof(struct guild_member));
  216. m->account_id =sd->status.account_id;
  217. m->char_id =sd->status.char_id;
  218. m->hair =sd->status.hair;
  219. m->hair_color =sd->status.hair_color;
  220. m->gender =sd->status.sex;
  221. m->class_ =sd->status.class_;
  222. m->lv =sd->status.base_level;
  223. // m->exp =0;
  224. // m->exp_payper =0;
  225. m->online =1;
  226. m->position =MAX_GUILDPOSITION-1;
  227. memcpy(m->name,sd->status.name,NAME_LENGTH);
  228. return;
  229. }
  230. /**
  231. * ギルドのEXPキャッシュをinter鯖にフラッシュする
  232. * @see DBApply
  233. */
  234. int guild_payexp_timer_sub(DBKey key, DBData *data, va_list ap)
  235. {
  236. int i;
  237. struct guild_expcache *c;
  238. struct guild *g;
  239. c = db_data2ptr(data);
  240. if (
  241. (g = guild_search(c->guild_id)) == NULL ||
  242. (i = guild_getindex(g, c->account_id, c->char_id)) < 0
  243. ) {
  244. db_remove(guild_expcache_db, db_i2key(c->char_id));
  245. return 0;
  246. }
  247. if (g->member[i].exp > UINT64_MAX - c->exp)
  248. g->member[i].exp = UINT64_MAX;
  249. else
  250. g->member[i].exp+= c->exp;
  251. intif_guild_change_memberinfo(g->guild_id,c->account_id,c->char_id,
  252. GMI_EXP,&g->member[i].exp,sizeof(g->member[i].exp));
  253. c->exp=0;
  254. db_remove(guild_expcache_db, db_i2key(c->char_id));
  255. return 0;
  256. }
  257. int guild_payexp_timer(int tid, unsigned int tick, int id, intptr_t data)
  258. {
  259. guild_expcache_db->clear(guild_expcache_db,guild_payexp_timer_sub);
  260. return 0;
  261. }
  262. /**
  263. * Taken from party_send_xy_timer_sub. [Skotlex]
  264. * @see DBApply
  265. */
  266. int guild_send_xy_timer_sub(DBKey key, DBData *data, va_list ap)
  267. {
  268. struct guild *g = db_data2ptr(data);
  269. int i;
  270. nullpo_ret(g);
  271. if( !g->connect_member )
  272. {// no members connected to this guild so do not iterate
  273. return 0;
  274. }
  275. for(i=0;i<g->max_member;i++){
  276. struct map_session_data* sd = g->member[i].sd;
  277. if( sd != NULL && sd->fd && (sd->guild_x != sd->bl.x || sd->guild_y != sd->bl.y) && !sd->bg_id )
  278. {
  279. clif_guild_xy(sd);
  280. sd->guild_x = sd->bl.x;
  281. sd->guild_y = sd->bl.y;
  282. }
  283. }
  284. return 0;
  285. }
  286. //Code from party_send_xy_timer [Skotlex]
  287. static int guild_send_xy_timer(int tid, unsigned int tick, int id, intptr_t data)
  288. {
  289. guild_db->foreach(guild_db,guild_send_xy_timer_sub,tick);
  290. return 0;
  291. }
  292. int guild_send_dot_remove(struct map_session_data *sd)
  293. {
  294. if (sd->status.guild_id)
  295. clif_guild_xy_remove(sd);
  296. return 0;
  297. }
  298. //------------------------------------------------------------------------
  299. int guild_create(struct map_session_data *sd, const char *name)
  300. {
  301. char tname[NAME_LENGTH];
  302. struct guild_member m;
  303. nullpo_ret(sd);
  304. safestrncpy(tname, name, NAME_LENGTH);
  305. trim(tname);
  306. if( !tname[0] )
  307. return 0; // empty name
  308. if( sd->status.guild_id )
  309. {// already in a guild
  310. clif_guild_created(sd,1);
  311. return 0;
  312. }
  313. if( battle_config.guild_emperium_check && pc_search_inventory(sd,714) == -1 )
  314. {// item required
  315. clif_guild_created(sd,3);
  316. return 0;
  317. }
  318. guild_makemember(&m,sd);
  319. m.position=0;
  320. intif_guild_create(name,&m);
  321. return 1;
  322. }
  323. // 作成可否
  324. int guild_created(int account_id,int guild_id)
  325. {
  326. struct map_session_data *sd=map_id2sd(account_id);
  327. if(sd==NULL)
  328. return 0;
  329. if(!guild_id) {
  330. clif_guild_created(sd,2); // 作成失敗(同名ギルド存在)
  331. return 0;
  332. }
  333. //struct guild *g;
  334. sd->status.guild_id=guild_id;
  335. clif_guild_created(sd,0);
  336. if(battle_config.guild_emperium_check)
  337. pc_delitem(sd,pc_search_inventory(sd,714),1,0,0,LOG_TYPE_CONSUME); // エンペリウム消耗
  338. return 0;
  339. }
  340. // 情報要求
  341. int guild_request_info(int guild_id)
  342. {
  343. return intif_guild_request_info(guild_id);
  344. }
  345. // イベント付き情報要求
  346. int guild_npc_request_info(int guild_id,const char *event)
  347. {
  348. if( guild_search(guild_id) )
  349. {
  350. if( event && *event )
  351. npc_event_do(event);
  352. return 0;
  353. }
  354. if( event && *event )
  355. {
  356. struct eventlist *ev;
  357. DBData prev;
  358. ev=(struct eventlist *)aCalloc(sizeof(struct eventlist),1);
  359. memcpy(ev->name,event,strlen(event));
  360. //The one in the db (if present) becomes the next event from this.
  361. if (guild_infoevent_db->put(guild_infoevent_db, db_i2key(guild_id), db_ptr2data(ev), &prev))
  362. ev->next = db_data2ptr(&prev);
  363. }
  364. return guild_request_info(guild_id);
  365. }
  366. // 所属キャラの確認
  367. int guild_check_member(struct guild *g)
  368. {
  369. int i;
  370. struct map_session_data *sd;
  371. struct s_mapiterator* iter;
  372. nullpo_ret(g);
  373. iter = mapit_getallusers();
  374. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  375. {
  376. if( sd->status.guild_id != g->guild_id )
  377. continue;
  378. i = guild_getindex(g,sd->status.account_id,sd->status.char_id);
  379. if (i < 0) {
  380. sd->status.guild_id=0;
  381. sd->guild_emblem_id=0;
  382. ShowWarning("guild: check_member %d[%s] is not member\n",sd->status.account_id,sd->status.name);
  383. }
  384. }
  385. mapit_free(iter);
  386. return 0;
  387. }
  388. // 情報所得失敗(そのIDのキャラを全部未所属にする)
  389. int guild_recv_noinfo(int guild_id)
  390. {
  391. struct map_session_data *sd;
  392. struct s_mapiterator* iter;
  393. iter = mapit_getallusers();
  394. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  395. {
  396. if( sd->status.guild_id == guild_id )
  397. sd->status.guild_id = 0; // erase guild
  398. }
  399. mapit_free(iter);
  400. return 0;
  401. }
  402. // 情報所得
  403. int guild_recv_info(struct guild *sg)
  404. {
  405. struct guild *g,before;
  406. int i,bm,m;
  407. DBData data;
  408. struct map_session_data *sd;
  409. bool guild_new = false;
  410. nullpo_ret(sg);
  411. if((g = (struct guild*)idb_get(guild_db,sg->guild_id))==NULL)
  412. {
  413. guild_new = true;
  414. g=(struct guild *)aCalloc(1,sizeof(struct guild));
  415. idb_put(guild_db,sg->guild_id,g);
  416. before=*sg;
  417. // 最初のロードなのでユーザーのチェックを行う
  418. guild_check_member(sg);
  419. if ((sd = map_nick2sd(sg->master)) != NULL)
  420. {
  421. //If the guild master is online the first time the guild_info is received,
  422. //that means he was the first to join, so apply guild skill blocking here.
  423. if( battle_config.guild_skill_relog_delay )
  424. guild_block_skill(sd, 300000);
  425. //Also set the guild master flag.
  426. sd->state.gmaster_flag = g;
  427. clif_charnameupdate(sd); // [LuzZza]
  428. clif_guild_masterormember(sd);
  429. }
  430. }else
  431. before=*g;
  432. memcpy(g,sg,sizeof(struct guild));
  433. if(g->max_member > MAX_GUILD)
  434. {
  435. ShowError("guild_recv_info: Received guild with %d members, but MAX_GUILD is only %d. Extra guild-members have been lost!\n", g->max_member, MAX_GUILD);
  436. g->max_member = MAX_GUILD;
  437. }
  438. for(i=bm=m=0;i<g->max_member;i++){
  439. if(g->member[i].account_id>0){
  440. sd = g->member[i].sd = guild_sd_check(g->guild_id, g->member[i].account_id, g->member[i].char_id);
  441. if (sd) clif_charnameupdate(sd); // [LuzZza]
  442. m++;
  443. }else
  444. g->member[i].sd=NULL;
  445. if(before.member[i].account_id>0)
  446. bm++;
  447. }
  448. for(i=0;i<g->max_member;i++){ // 情報の送信
  449. sd = g->member[i].sd;
  450. if( sd==NULL )
  451. continue;
  452. if( before.guild_lv!=g->guild_lv || bm!=m ||
  453. before.max_member!=g->max_member ){
  454. clif_guild_basicinfo(sd); // 基本情報送信
  455. clif_guild_emblem(sd,g); // エンブレム送信
  456. }
  457. if(bm!=m){ // メンバー情報送信
  458. clif_guild_memberlist(g->member[i].sd);
  459. }
  460. if( before.skill_point!=g->skill_point)
  461. clif_guild_skillinfo(sd); // スキル情報送信
  462. if( guild_new ){ // 未送信なら所属情報も送る
  463. clif_guild_belonginfo(sd,g);
  464. clif_guild_notice(sd,g);
  465. sd->guild_emblem_id=g->emblem_id;
  466. }
  467. }
  468. // イベントの発生
  469. if (guild_infoevent_db->remove(guild_infoevent_db, db_i2key(sg->guild_id), &data))
  470. {
  471. struct eventlist *ev = db_data2ptr(&data), *ev2;
  472. while(ev){
  473. npc_event_do(ev->name);
  474. ev2=ev->next;
  475. aFree(ev);
  476. ev=ev2;
  477. }
  478. }
  479. return 0;
  480. }
  481. // ギルドへの勧誘
  482. int guild_invite(struct map_session_data *sd,struct map_session_data *tsd)
  483. {
  484. struct guild *g;
  485. int i;
  486. nullpo_ret(sd);
  487. g=guild_search(sd->status.guild_id);
  488. if(tsd==NULL || g==NULL)
  489. return 0;
  490. if( (i=guild_getposition(g,sd))<0 || !(g->position[i].mode&0x0001) )
  491. return 0; //Invite permission.
  492. if(!battle_config.invite_request_check) {
  493. if (tsd->party_invite>0 || tsd->trade_partner || tsd->adopt_invite ) { // 相手が取引中かどうか
  494. clif_guild_inviteack(sd,0);
  495. return 0;
  496. }
  497. }
  498. if (!tsd->fd) { //You can't invite someone who has already disconnected.
  499. clif_guild_inviteack(sd,1);
  500. return 0;
  501. }
  502. if(tsd->status.guild_id>0 ||
  503. tsd->guild_invite>0 ||
  504. ((agit_flag || agit2_flag) && map[tsd->bl.m].flag.gvg_castle))
  505. { //Can't invite people inside castles. [Skotlex]
  506. clif_guild_inviteack(sd,0);
  507. return 0;
  508. }
  509. // 定員確認
  510. ARR_FIND( 0, g->max_member, i, g->member[i].account_id == 0 );
  511. if(i==g->max_member){
  512. clif_guild_inviteack(sd,3);
  513. return 0;
  514. }
  515. tsd->guild_invite=sd->status.guild_id;
  516. tsd->guild_invite_account=sd->status.account_id;
  517. clif_guild_invite(tsd,g);
  518. return 0;
  519. }
  520. /// Guild invitation reply.
  521. /// flag: 0:rejected, 1:accepted
  522. int guild_reply_invite(struct map_session_data* sd, int guild_id, int flag)
  523. {
  524. struct map_session_data* tsd;
  525. nullpo_ret(sd);
  526. // subsequent requests may override the value
  527. if( sd->guild_invite != guild_id )
  528. return 0; // mismatch
  529. // look up the person who sent the invite
  530. //NOTE: this can be NULL because the person might have logged off in the meantime
  531. tsd = map_id2sd(sd->guild_invite_account);
  532. if ( sd->status.guild_id > 0 ) // [Paradox924X]
  533. { // Already in another guild.
  534. if ( tsd ) clif_guild_inviteack(tsd,0);
  535. return 0;
  536. }
  537. else if( flag == 0 )
  538. {// rejected
  539. sd->guild_invite = 0;
  540. sd->guild_invite_account = 0;
  541. if( tsd ) clif_guild_inviteack(tsd,1);
  542. }
  543. else
  544. {// accepted
  545. struct guild_member m;
  546. struct guild* g;
  547. int i;
  548. if( (g=guild_search(guild_id)) == NULL )
  549. {
  550. sd->guild_invite = 0;
  551. sd->guild_invite_account = 0;
  552. return 0;
  553. }
  554. ARR_FIND( 0, g->max_member, i, g->member[i].account_id == 0 );
  555. if( i == g->max_member )
  556. {
  557. sd->guild_invite = 0;
  558. sd->guild_invite_account = 0;
  559. if( tsd ) clif_guild_inviteack(tsd,3);
  560. return 0;
  561. }
  562. guild_makemember(&m,sd);
  563. intif_guild_addmember(guild_id, &m);
  564. //TODO: send a minimap update to this player
  565. }
  566. return 0;
  567. }
  568. //Invoked when a player joins.
  569. //- If guild is not in memory, it is requested
  570. //- Otherwise sd pointer is set up.
  571. //- Player must be authed and must belong to a guild before invoking this method
  572. void guild_member_joined(struct map_session_data *sd)
  573. {
  574. struct guild* g;
  575. int i;
  576. g=guild_search(sd->status.guild_id);
  577. if (!g) {
  578. guild_request_info(sd->status.guild_id);
  579. return;
  580. }
  581. if (strcmp(sd->status.name,g->master) == 0)
  582. { // set the Guild Master flag
  583. sd->state.gmaster_flag = g;
  584. // prevent Guild Skills from being used directly after relog
  585. if( battle_config.guild_skill_relog_delay )
  586. guild_block_skill(sd, 300000);
  587. }
  588. i = guild_getindex(g, sd->status.account_id, sd->status.char_id);
  589. if (i == -1)
  590. sd->status.guild_id = 0;
  591. else
  592. g->member[i].sd = sd;
  593. }
  594. // ギルドメンバが追加された
  595. int guild_member_added(int guild_id,int account_id,int char_id,int flag)
  596. {
  597. struct map_session_data *sd= map_id2sd(account_id),*sd2;
  598. struct guild *g;
  599. if( (g=guild_search(guild_id))==NULL )
  600. return 0;
  601. if(sd==NULL || sd->guild_invite==0){
  602. // キャラ側に登録できなかったため脱退要求を出す
  603. if (flag == 0) {
  604. ShowError("guild: member added error %d is not online\n",account_id);
  605. intif_guild_leave(guild_id,account_id,char_id,0,"** Data Error **");
  606. }
  607. return 0;
  608. }
  609. sd2 = map_id2sd(sd->guild_invite_account);
  610. sd->guild_invite = 0;
  611. sd->guild_invite_account = 0;
  612. if(flag==1){ // 失敗
  613. if( sd2!=NULL )
  614. clif_guild_inviteack(sd2,3);
  615. return 0;
  616. }
  617. // 成功
  618. sd->status.guild_id = g->guild_id;
  619. sd->guild_emblem_id = g->emblem_id;
  620. //Packets which were sent in the previous 'guild_sent' implementation.
  621. clif_guild_belonginfo(sd,g);
  622. clif_guild_notice(sd,g);
  623. //TODO: send new emblem info to others
  624. if( sd2!=NULL )
  625. clif_guild_inviteack(sd2,2);
  626. //Next line commented because it do nothing, look at guild_recv_info [LuzZza]
  627. //clif_charnameupdate(sd); //Update display name [Skotlex]
  628. return 0;
  629. }
  630. // ギルド脱退要求
  631. int guild_leave(struct map_session_data* sd, int guild_id, int account_id, int char_id, const char* mes)
  632. {
  633. struct guild *g;
  634. nullpo_ret(sd);
  635. g = guild_search(sd->status.guild_id);
  636. if(g==NULL)
  637. return 0;
  638. if(sd->status.account_id!=account_id ||
  639. sd->status.char_id!=char_id || sd->status.guild_id!=guild_id ||
  640. ((agit_flag || agit2_flag) && map[sd->bl.m].flag.gvg_castle))
  641. return 0;
  642. intif_guild_leave(sd->status.guild_id, sd->status.account_id, sd->status.char_id,0,mes);
  643. return 0;
  644. }
  645. // ギルド追放要求
  646. int guild_expulsion(struct map_session_data* sd, int guild_id, int account_id, int char_id, const char* mes)
  647. {
  648. struct map_session_data *tsd;
  649. struct guild *g;
  650. int i,ps;
  651. nullpo_ret(sd);
  652. g = guild_search(sd->status.guild_id);
  653. if(g==NULL)
  654. return 0;
  655. if(sd->status.guild_id!=guild_id)
  656. return 0;
  657. if( (ps=guild_getposition(g,sd))<0 || !(g->position[ps].mode&0x0010) )
  658. return 0; //Expulsion permission
  659. //Can't leave inside guild castles.
  660. if ((tsd = map_id2sd(account_id)) &&
  661. tsd->status.char_id == char_id &&
  662. ((agit_flag || agit2_flag) && map[tsd->bl.m].flag.gvg_castle))
  663. return 0;
  664. // find the member and perform expulsion
  665. i = guild_getindex(g, account_id, char_id);
  666. if( i != -1 && strcmp(g->member[i].name,g->master) != 0 ) //Can't expel the GL!
  667. intif_guild_leave(g->guild_id,account_id,char_id,1,mes);
  668. return 0;
  669. }
  670. int guild_member_withdraw(int guild_id, int account_id, int char_id, int flag, const char* name, const char* mes)
  671. {
  672. int i;
  673. struct guild* g = guild_search(guild_id);
  674. struct map_session_data* sd = map_charid2sd(char_id);
  675. struct map_session_data* online_member_sd;
  676. if(g == NULL)
  677. return 0; // no such guild (error!)
  678. i = guild_getindex(g, account_id, char_id);
  679. if( i == -1 )
  680. return 0; // not a member (inconsistency!)
  681. online_member_sd = guild_getavailablesd(g);
  682. if(online_member_sd == NULL)
  683. return 0; // noone online to inform
  684. if(!flag)
  685. clif_guild_leave(online_member_sd, name, mes);
  686. else
  687. clif_guild_expulsion(online_member_sd, name, mes, account_id);
  688. // remove member from guild
  689. memset(&g->member[i],0,sizeof(struct guild_member));
  690. clif_guild_memberlist(online_member_sd);
  691. // update char, if online
  692. if(sd != NULL && sd->status.guild_id == guild_id)
  693. {
  694. // do stuff that needs the guild_id first, BEFORE we wipe it
  695. if (sd->state.storage_flag == 2) //Close the guild storage.
  696. storage_guild_storageclose(sd);
  697. guild_send_dot_remove(sd);
  698. sd->status.guild_id = 0;
  699. sd->guild_emblem_id = 0;
  700. clif_charnameupdate(sd); //Update display name [Skotlex]
  701. //TODO: send emblem update to self and people around
  702. }
  703. return 0;
  704. }
  705. int guild_send_memberinfoshort(struct map_session_data *sd,int online)
  706. { // cleaned up [LuzZza]
  707. struct guild *g;
  708. nullpo_ret(sd);
  709. if(sd->status.guild_id <= 0)
  710. return 0;
  711. if(!(g = guild_search(sd->status.guild_id)))
  712. return 0;
  713. intif_guild_memberinfoshort(g->guild_id,
  714. sd->status.account_id,sd->status.char_id,online,sd->status.base_level,sd->status.class_);
  715. if(!online){
  716. int i=guild_getindex(g,sd->status.account_id,sd->status.char_id);
  717. if(i>=0)
  718. g->member[i].sd=NULL;
  719. else
  720. ShowError("guild_send_memberinfoshort: Failed to locate member %d:%d in guild %d!\n", sd->status.account_id, sd->status.char_id, g->guild_id);
  721. return 0;
  722. }
  723. if(sd->state.connect_new)
  724. { //Note that this works because it is invoked in parse_LoadEndAck before connect_new is cleared.
  725. clif_guild_belonginfo(sd,g);
  726. clif_guild_notice(sd,g);
  727. sd->guild_emblem_id = g->emblem_id;
  728. }
  729. return 0;
  730. }
  731. int guild_recv_memberinfoshort(int guild_id,int account_id,int char_id,int online,int lv,int class_)
  732. { // cleaned up [LuzZza]
  733. int i,alv,c,idx=-1,om=0,oldonline=-1;
  734. struct guild *g = guild_search(guild_id);
  735. if(g == NULL)
  736. return 0;
  737. for(i=0,alv=0,c=0,om=0;i<g->max_member;i++){
  738. struct guild_member *m=&g->member[i];
  739. if(!m->account_id) continue;
  740. if(m->account_id==account_id && m->char_id==char_id ){
  741. oldonline=m->online;
  742. m->online=online;
  743. m->lv=lv;
  744. m->class_=class_;
  745. idx=i;
  746. }
  747. alv+=m->lv;
  748. c++;
  749. if(m->online)
  750. om++;
  751. }
  752. if(idx == -1 || c == 0) {
  753. // ギルドのメンバー外なので追放扱いする
  754. struct map_session_data *sd = map_id2sd(account_id);
  755. if(sd && sd->status.char_id == char_id) {
  756. sd->status.guild_id=0;
  757. sd->guild_emblem_id=0;
  758. }
  759. ShowWarning("guild: not found member %d,%d on %d[%s]\n", account_id,char_id,guild_id,g->name);
  760. return 0;
  761. }
  762. g->average_lv=alv/c;
  763. g->connect_member=om;
  764. //Ensure validity of pointer (ie: player logs in/out, changes map-server)
  765. g->member[idx].sd = guild_sd_check(guild_id, account_id, char_id);
  766. if(oldonline!=online)
  767. clif_guild_memberlogin_notice(g, idx, online);
  768. if(!g->member[idx].sd)
  769. return 0;
  770. //Send XY dot updates. [Skotlex]
  771. //Moved from guild_send_memberinfoshort [LuzZza]
  772. for(i=0; i < g->max_member; i++) {
  773. if(!g->member[i].sd || i == idx ||
  774. g->member[i].sd->bl.m != g->member[idx].sd->bl.m)
  775. continue;
  776. clif_guild_xy_single(g->member[idx].sd->fd, g->member[i].sd);
  777. clif_guild_xy_single(g->member[i].sd->fd, g->member[idx].sd);
  778. }
  779. return 0;
  780. }
  781. // ギルド会話送信
  782. int guild_send_message(struct map_session_data *sd,const char *mes,int len)
  783. {
  784. nullpo_ret(sd);
  785. if(sd->status.guild_id==0)
  786. return 0;
  787. intif_guild_message(sd->status.guild_id,sd->status.account_id,mes,len);
  788. guild_recv_message(sd->status.guild_id,sd->status.account_id,mes,len);
  789. // Chat logging type 'G' / Guild Chat
  790. log_chat(LOG_CHAT_GUILD, sd->status.guild_id, sd->status.char_id, sd->status.account_id, mapindex_id2name(sd->mapindex), sd->bl.x, sd->bl.y, NULL, mes);
  791. return 0;
  792. }
  793. // ギルド会話受信
  794. int guild_recv_message(int guild_id,int account_id,const char *mes,int len)
  795. {
  796. struct guild *g;
  797. if( (g=guild_search(guild_id))==NULL)
  798. return 0;
  799. clif_guild_message(g,account_id,mes,len);
  800. return 0;
  801. }
  802. // ギルドメンバの役職変更
  803. int guild_change_memberposition(int guild_id,int account_id,int char_id,short idx)
  804. {
  805. return intif_guild_change_memberinfo(guild_id,account_id,char_id,GMI_POSITION,&idx,sizeof(idx));
  806. }
  807. // ギルドメンバの役職変更通知
  808. int guild_memberposition_changed(struct guild *g,int idx,int pos)
  809. {
  810. nullpo_ret(g);
  811. g->member[idx].position=pos;
  812. clif_guild_memberpositionchanged(g,idx);
  813. // Update char position in client [LuzZza]
  814. if(g->member[idx].sd != NULL)
  815. clif_charnameupdate(g->member[idx].sd);
  816. return 0;
  817. }
  818. // ギルド役職変更
  819. int guild_change_position(int guild_id,int idx,
  820. int mode,int exp_mode,const char *name)
  821. {
  822. struct guild_position p;
  823. exp_mode = cap_value(exp_mode, 0, battle_config.guild_exp_limit);
  824. //Mode 0x01 <- Invite
  825. //Mode 0x10 <- Expel.
  826. p.mode=mode&0x11;
  827. p.exp_mode=exp_mode;
  828. safestrncpy(p.name,name,NAME_LENGTH);
  829. return intif_guild_position(guild_id,idx,&p);
  830. }
  831. // ギルド役職変更通知
  832. int guild_position_changed(int guild_id,int idx,struct guild_position *p)
  833. {
  834. struct guild *g=guild_search(guild_id);
  835. int i;
  836. if(g==NULL)
  837. return 0;
  838. memcpy(&g->position[idx],p,sizeof(struct guild_position));
  839. clif_guild_positionchanged(g,idx);
  840. // Update char name in client [LuzZza]
  841. for(i=0;i<g->max_member;i++)
  842. if(g->member[i].position == idx && g->member[i].sd != NULL)
  843. clif_charnameupdate(g->member[i].sd);
  844. return 0;
  845. }
  846. // ギルド告知変更
  847. int guild_change_notice(struct map_session_data *sd,int guild_id,const char *mes1,const char *mes2)
  848. {
  849. nullpo_ret(sd);
  850. if(guild_id!=sd->status.guild_id)
  851. return 0;
  852. return intif_guild_notice(guild_id,mes1,mes2);
  853. }
  854. // ギルド告知変更通知
  855. int guild_notice_changed(int guild_id,const char *mes1,const char *mes2)
  856. {
  857. int i;
  858. struct map_session_data *sd;
  859. struct guild *g=guild_search(guild_id);
  860. if(g==NULL)
  861. return 0;
  862. memcpy(g->mes1,mes1,MAX_GUILDMES1);
  863. memcpy(g->mes2,mes2,MAX_GUILDMES2);
  864. for(i=0;i<g->max_member;i++){
  865. if((sd=g->member[i].sd)!=NULL)
  866. clif_guild_notice(sd,g);
  867. }
  868. return 0;
  869. }
  870. // ギルドエンブレム変更
  871. int guild_change_emblem(struct map_session_data *sd,int len,const char *data)
  872. {
  873. struct guild *g;
  874. nullpo_ret(sd);
  875. if (battle_config.require_glory_guild &&
  876. !((g = guild_search(sd->status.guild_id)) && guild_checkskill(g, GD_GLORYGUILD)>0)) {
  877. clif_skill_fail(sd,GD_GLORYGUILD,USESKILL_FAIL_LEVEL,0);
  878. return 0;
  879. }
  880. return intif_guild_emblem(sd->status.guild_id,len,data);
  881. }
  882. // ギルドエンブレム変更通知
  883. int guild_emblem_changed(int len,int guild_id,int emblem_id,const char *data)
  884. {
  885. int i;
  886. struct map_session_data *sd;
  887. struct guild *g=guild_search(guild_id);
  888. if(g==NULL)
  889. return 0;
  890. memcpy(g->emblem_data,data,len);
  891. g->emblem_len=len;
  892. g->emblem_id=emblem_id;
  893. for(i=0;i<g->max_member;i++){
  894. if((sd=g->member[i].sd)!=NULL){
  895. sd->guild_emblem_id=emblem_id;
  896. clif_guild_belonginfo(sd,g);
  897. clif_guild_emblem(sd,g);
  898. clif_guild_emblem_area(&sd->bl);
  899. }
  900. }
  901. {// update guardians (mobs)
  902. DBIterator* iter = db_iterator(castle_db);
  903. struct guild_castle* gc;
  904. for( gc = (struct guild_castle*)dbi_first(iter) ; dbi_exists(iter); gc = (struct guild_castle*)dbi_next(iter) )
  905. {
  906. if( gc->guild_id != guild_id )
  907. continue;
  908. // update permanent guardians
  909. for( i = 0; i < ARRAYLENGTH(gc->guardian); ++i )
  910. {
  911. TBL_MOB* md = (gc->guardian[i].id ? map_id2md(gc->guardian[i].id) : NULL);
  912. if( md == NULL || md->guardian_data == NULL )
  913. continue;
  914. md->guardian_data->emblem_id = emblem_id;
  915. clif_guild_emblem_area(&md->bl);
  916. }
  917. // update temporary guardians
  918. for( i = 0; i < gc->temp_guardians_max; ++i )
  919. {
  920. TBL_MOB* md = (gc->temp_guardians[i] ? map_id2md(gc->temp_guardians[i]) : NULL);
  921. if( md == NULL || md->guardian_data == NULL )
  922. continue;
  923. md->guardian_data->emblem_id = emblem_id;
  924. clif_guild_emblem_area(&md->bl);
  925. }
  926. }
  927. dbi_destroy(iter);
  928. }
  929. {// update npcs (flags or other npcs that used flagemblem to attach to this guild)
  930. // TODO this is not efficient [FlavioJS]
  931. struct s_mapiterator* iter = mapit_geteachnpc();
  932. TBL_NPC* nd;
  933. for( nd = (TBL_NPC*)mapit_first(iter) ; mapit_exists(iter); nd = (TBL_NPC*)mapit_next(iter) )
  934. {
  935. if( nd->subtype != SCRIPT || nd->u.scr.guild_id != guild_id )
  936. continue;
  937. clif_guild_emblem_area(&nd->bl);
  938. }
  939. mapit_free(iter);
  940. }
  941. return 0;
  942. }
  943. /**
  944. * @see DBCreateData
  945. */
  946. static DBData create_expcache(DBKey key, va_list args) {
  947. struct guild_expcache *c;
  948. struct map_session_data *sd = va_arg(args, struct map_session_data*);
  949. CREATE(c, struct guild_expcache, 1);
  950. c->guild_id = sd->status.guild_id;
  951. c->account_id = sd->status.account_id;
  952. c->char_id = sd->status.char_id;
  953. c->exp = 0;
  954. return db_ptr2data(c);
  955. }
  956. // ギルドのEXP上納
  957. unsigned int guild_payexp(struct map_session_data *sd,unsigned int exp)
  958. {
  959. struct guild *g;
  960. struct guild_expcache *c;
  961. int per;
  962. nullpo_ret(sd);
  963. if (!exp) return 0;
  964. if (sd->status.guild_id == 0 ||
  965. (g = guild_search(sd->status.guild_id)) == NULL ||
  966. (per = guild_getposition(g,sd)) < 0 ||
  967. (per = g->position[per].exp_mode) < 1)
  968. return 0;
  969. if (per < 100)
  970. exp = exp * per / 100;
  971. //Otherwise tax everything.
  972. c = db_data2ptr(guild_expcache_db->ensure(guild_expcache_db, db_i2key(sd->status.char_id), create_expcache, sd));
  973. if (c->exp > UINT64_MAX - exp)
  974. c->exp = UINT64_MAX;
  975. else
  976. c->exp += exp;
  977. return exp;
  978. }
  979. // Celest
  980. int guild_getexp(struct map_session_data *sd,int exp)
  981. {
  982. struct guild_expcache *c;
  983. nullpo_ret(sd);
  984. if (sd->status.guild_id == 0 || guild_search(sd->status.guild_id) == NULL)
  985. return 0;
  986. c = db_data2ptr(guild_expcache_db->ensure(guild_expcache_db, db_i2key(sd->status.char_id), create_expcache, sd));
  987. if (c->exp > UINT64_MAX - exp)
  988. c->exp = UINT64_MAX;
  989. else
  990. c->exp += exp;
  991. return exp;
  992. }
  993. // スキルポイント割り振り
  994. int guild_skillup(TBL_PC* sd, int skill_num)
  995. {
  996. struct guild* g;
  997. int idx = skill_num - GD_SKILLBASE;
  998. int max = guild_skill_get_max(skill_num);
  999. nullpo_ret(sd);
  1000. if( idx < 0 || idx >= MAX_GUILDSKILL || // not a guild skill
  1001. sd->status.guild_id == 0 || (g=guild_search(sd->status.guild_id)) == NULL || // no guild
  1002. strcmp(sd->status.name, g->master) ) // not the guild master
  1003. return 0;
  1004. if( g->skill_point > 0 &&
  1005. g->skill[idx].id != 0 &&
  1006. g->skill[idx].lv < max )
  1007. intif_guild_skillup(g->guild_id, skill_num, sd->status.account_id, max);
  1008. return 0;
  1009. }
  1010. // スキルポイント割り振り通知
  1011. int guild_skillupack(int guild_id,int skill_num,int account_id)
  1012. {
  1013. struct map_session_data *sd=map_id2sd(account_id);
  1014. struct guild *g=guild_search(guild_id);
  1015. int i;
  1016. if(g==NULL)
  1017. return 0;
  1018. if(sd!=NULL)
  1019. clif_guild_skillup(sd,skill_num,g->skill[skill_num-GD_SKILLBASE].lv);
  1020. // 全員に通知
  1021. for(i=0;i<g->max_member;i++)
  1022. if((sd=g->member[i].sd)!=NULL)
  1023. clif_guild_skillinfo(sd);
  1024. return 0;
  1025. }
  1026. // ギルド同盟数所得
  1027. int guild_get_alliance_count(struct guild *g,int flag)
  1028. {
  1029. int i,c;
  1030. nullpo_ret(g);
  1031. for(i=c=0;i<MAX_GUILDALLIANCE;i++){
  1032. if( g->alliance[i].guild_id>0 &&
  1033. g->alliance[i].opposition==flag )
  1034. c++;
  1035. }
  1036. return c;
  1037. }
  1038. // Blocks all guild skills which have a common delay time.
  1039. void guild_block_skill(struct map_session_data *sd, int time)
  1040. {
  1041. int skill_num[] = { GD_BATTLEORDER, GD_REGENERATION, GD_RESTORE, GD_EMERGENCYCALL };
  1042. int i;
  1043. for (i = 0; i < 4; i++)
  1044. skill_blockpc_start(sd, skill_num[i], time);
  1045. }
  1046. // 同盟関係かどうかチェック
  1047. // 同盟なら1、それ以外は0
  1048. int guild_check_alliance(int guild_id1, int guild_id2, int flag)
  1049. {
  1050. struct guild *g;
  1051. int i;
  1052. g = guild_search(guild_id1);
  1053. if (g == NULL)
  1054. return 0;
  1055. ARR_FIND( 0, MAX_GUILDALLIANCE, i, g->alliance[i].guild_id == guild_id2 && g->alliance[i].opposition == flag );
  1056. return( i < MAX_GUILDALLIANCE ) ? 1 : 0;
  1057. }
  1058. // ギルド同盟要求
  1059. int guild_reqalliance(struct map_session_data *sd,struct map_session_data *tsd)
  1060. {
  1061. struct guild *g[2];
  1062. int i;
  1063. if(agit_flag || agit2_flag) { // Disable alliance creation during woe [Valaris]
  1064. clif_displaymessage(sd->fd,"Alliances cannot be made during Guild Wars!");
  1065. return 0;
  1066. } // end addition [Valaris]
  1067. nullpo_ret(sd);
  1068. if(tsd==NULL || tsd->status.guild_id<=0)
  1069. return 0;
  1070. g[0]=guild_search(sd->status.guild_id);
  1071. g[1]=guild_search(tsd->status.guild_id);
  1072. if(g[0]==NULL || g[1]==NULL)
  1073. return 0;
  1074. // Prevent creation alliance with same guilds [LuzZza]
  1075. if(sd->status.guild_id == tsd->status.guild_id)
  1076. return 0;
  1077. if( guild_get_alliance_count(g[0],0) >= battle_config.max_guild_alliance ) {
  1078. clif_guild_allianceack(sd,4);
  1079. return 0;
  1080. }
  1081. if( guild_get_alliance_count(g[1],0) >= battle_config.max_guild_alliance ) {
  1082. clif_guild_allianceack(sd,3);
  1083. return 0;
  1084. }
  1085. if( tsd->guild_alliance>0 ){
  1086. clif_guild_allianceack(sd,1);
  1087. return 0;
  1088. }
  1089. for(i=0;i<MAX_GUILDALLIANCE;i++){ // すでに同盟状態か確認
  1090. if( g[0]->alliance[i].guild_id==tsd->status.guild_id &&
  1091. g[0]->alliance[i].opposition==0){
  1092. clif_guild_allianceack(sd,0);
  1093. return 0;
  1094. }
  1095. }
  1096. tsd->guild_alliance=sd->status.guild_id;
  1097. tsd->guild_alliance_account=sd->status.account_id;
  1098. clif_guild_reqalliance(tsd,sd->status.account_id,g[0]->name);
  1099. return 0;
  1100. }
  1101. // ギルド勧誘への返答
  1102. int guild_reply_reqalliance(struct map_session_data *sd,int account_id,int flag)
  1103. {
  1104. struct map_session_data *tsd;
  1105. nullpo_ret(sd);
  1106. tsd= map_id2sd( account_id );
  1107. if (!tsd) { //Character left? Cancel alliance.
  1108. clif_guild_allianceack(sd,3);
  1109. return 0;
  1110. }
  1111. if(sd->guild_alliance!=tsd->status.guild_id) // 勧誘とギルドIDが違う
  1112. return 0;
  1113. if(flag==1){ // 承諾
  1114. int i;
  1115. struct guild *g,*tg; // 同盟数再確認
  1116. g=guild_search(sd->status.guild_id);
  1117. tg=guild_search(tsd->status.guild_id);
  1118. if(g==NULL || guild_get_alliance_count(g,0) >= battle_config.max_guild_alliance){
  1119. clif_guild_allianceack(sd,4);
  1120. clif_guild_allianceack(tsd,3);
  1121. return 0;
  1122. }
  1123. if(tg==NULL || guild_get_alliance_count(tg,0) >= battle_config.max_guild_alliance){
  1124. clif_guild_allianceack(sd,3);
  1125. clif_guild_allianceack(tsd,4);
  1126. return 0;
  1127. }
  1128. for(i=0;i<MAX_GUILDALLIANCE;i++){
  1129. if(g->alliance[i].guild_id==tsd->status.guild_id &&
  1130. g->alliance[i].opposition==1)
  1131. intif_guild_alliance( sd->status.guild_id,tsd->status.guild_id,
  1132. sd->status.account_id,tsd->status.account_id,9 );
  1133. }
  1134. for(i=0;i<MAX_GUILDALLIANCE;i++){
  1135. if(tg->alliance[i].guild_id==sd->status.guild_id &&
  1136. tg->alliance[i].opposition==1)
  1137. intif_guild_alliance( tsd->status.guild_id,sd->status.guild_id,
  1138. tsd->status.account_id,sd->status.account_id,9 );
  1139. }
  1140. // inter鯖へ同盟要請
  1141. intif_guild_alliance( sd->status.guild_id,tsd->status.guild_id,
  1142. sd->status.account_id,tsd->status.account_id,0 );
  1143. return 0;
  1144. }else{ // 拒否
  1145. sd->guild_alliance=0;
  1146. sd->guild_alliance_account=0;
  1147. if(tsd!=NULL)
  1148. clif_guild_allianceack(tsd,3);
  1149. }
  1150. return 0;
  1151. }
  1152. // ギルド関係解消
  1153. int guild_delalliance(struct map_session_data *sd,int guild_id,int flag)
  1154. {
  1155. nullpo_ret(sd);
  1156. if(agit_flag || agit2_flag) { // Disable alliance breaking during woe [Valaris]
  1157. clif_displaymessage(sd->fd,"Alliances cannot be broken during Guild Wars!");
  1158. return 0;
  1159. } // end addition [Valaris]
  1160. intif_guild_alliance( sd->status.guild_id,guild_id,sd->status.account_id,0,flag|8 );
  1161. return 0;
  1162. }
  1163. // ギルド敵対
  1164. int guild_opposition(struct map_session_data *sd,struct map_session_data *tsd)
  1165. {
  1166. struct guild *g;
  1167. int i;
  1168. nullpo_ret(sd);
  1169. g=guild_search(sd->status.guild_id);
  1170. if(g==NULL || tsd==NULL)
  1171. return 0;
  1172. // Prevent creation opposition with same guilds [LuzZza]
  1173. if(sd->status.guild_id == tsd->status.guild_id)
  1174. return 0;
  1175. if( guild_get_alliance_count(g,1) >= battle_config.max_guild_alliance ) {
  1176. clif_guild_oppositionack(sd,1);
  1177. return 0;
  1178. }
  1179. for(i=0;i<MAX_GUILDALLIANCE;i++){ // すでに関係を持っているか確認
  1180. if(g->alliance[i].guild_id==tsd->status.guild_id){
  1181. if(g->alliance[i].opposition==1){ // すでに敵対
  1182. clif_guild_oppositionack(sd,2);
  1183. return 0;
  1184. }
  1185. if(agit_flag || agit2_flag) // Prevent the changing of alliances to oppositions during WoE.
  1186. return 0;
  1187. //Change alliance to opposition.
  1188. intif_guild_alliance( sd->status.guild_id,tsd->status.guild_id,
  1189. sd->status.account_id,tsd->status.account_id,8 );
  1190. }
  1191. }
  1192. // inter鯖に敵対要請
  1193. intif_guild_alliance( sd->status.guild_id,tsd->status.guild_id,
  1194. sd->status.account_id,tsd->status.account_id,1 );
  1195. return 0;
  1196. }
  1197. // ギルド同盟/敵対通知
  1198. int guild_allianceack(int guild_id1,int guild_id2,int account_id1,int account_id2,int flag,const char *name1,const char *name2)
  1199. {
  1200. struct guild *g[2];
  1201. int guild_id[2];
  1202. const char *guild_name[2];
  1203. struct map_session_data *sd[2];
  1204. int j,i;
  1205. guild_id[0] = guild_id1;
  1206. guild_id[1] = guild_id2;
  1207. guild_name[0] = name1;
  1208. guild_name[1] = name2;
  1209. sd[0] = map_id2sd(account_id1);
  1210. sd[1] = map_id2sd(account_id2);
  1211. g[0]=guild_search(guild_id1);
  1212. g[1]=guild_search(guild_id2);
  1213. if(sd[0]!=NULL && (flag&0x0f)==0){
  1214. sd[0]->guild_alliance=0;
  1215. sd[0]->guild_alliance_account=0;
  1216. }
  1217. if(flag&0x70){ // 失敗
  1218. for(i=0;i<2-(flag&1);i++)
  1219. if( sd[i]!=NULL )
  1220. clif_guild_allianceack(sd[i],((flag>>4)==i+1)?3:4);
  1221. return 0;
  1222. }
  1223. if(!(flag&0x08)){ // 関係追加
  1224. for(i=0;i<2-(flag&1);i++)
  1225. {
  1226. if(g[i]!=NULL)
  1227. {
  1228. ARR_FIND( 0, MAX_GUILDALLIANCE, j, g[i]->alliance[j].guild_id == 0 );
  1229. if( j < MAX_GUILDALLIANCE )
  1230. {
  1231. g[i]->alliance[j].guild_id=guild_id[1-i];
  1232. memcpy(g[i]->alliance[j].name,guild_name[1-i],NAME_LENGTH);
  1233. g[i]->alliance[j].opposition=flag&1;
  1234. }
  1235. }
  1236. }
  1237. }else{ // 関係解消
  1238. for(i=0;i<2-(flag&1);i++)
  1239. {
  1240. if(g[i]!=NULL)
  1241. {
  1242. ARR_FIND( 0, MAX_GUILDALLIANCE, j, g[i]->alliance[j].guild_id == guild_id[1-i] && g[i]->alliance[j].opposition == (flag&1) );
  1243. if( j < MAX_GUILDALLIANCE )
  1244. g[i]->alliance[j].guild_id = 0;
  1245. }
  1246. if( sd[i]!=NULL ) // 解消通知
  1247. clif_guild_delalliance(sd[i],guild_id[1-i],(flag&1));
  1248. }
  1249. }
  1250. if((flag&0x0f)==0){ // 同盟通知
  1251. if( sd[1]!=NULL )
  1252. clif_guild_allianceack(sd[1],2);
  1253. }else if((flag&0x0f)==1){ // 敵対通知
  1254. if( sd[0]!=NULL )
  1255. clif_guild_oppositionack(sd[0],0);
  1256. }
  1257. for(i=0;i<2-(flag&1);i++){ // 同盟/敵対リストの再送信
  1258. struct map_session_data *sd;
  1259. if(g[i]!=NULL)
  1260. for(j=0;j<g[i]->max_member;j++)
  1261. if((sd=g[i]->member[j].sd)!=NULL)
  1262. clif_guild_allianceinfo(sd);
  1263. }
  1264. return 0;
  1265. }
  1266. /**
  1267. * ギルド解散通知用
  1268. * @see DBApply
  1269. */
  1270. int guild_broken_sub(DBKey key, DBData *data, va_list ap)
  1271. {
  1272. struct guild *g = db_data2ptr(data);
  1273. int guild_id=va_arg(ap,int);
  1274. int i,j;
  1275. struct map_session_data *sd=NULL;
  1276. nullpo_ret(g);
  1277. for(i=0;i<MAX_GUILDALLIANCE;i++){ // 関係を破棄
  1278. if(g->alliance[i].guild_id==guild_id){
  1279. for(j=0;j<g->max_member;j++)
  1280. if( (sd=g->member[j].sd)!=NULL )
  1281. clif_guild_delalliance(sd,guild_id,g->alliance[i].opposition);
  1282. intif_guild_alliance(g->guild_id, guild_id,0,0,g->alliance[i].opposition|8);
  1283. g->alliance[i].guild_id=0;
  1284. }
  1285. }
  1286. return 0;
  1287. }
  1288. /**
  1289. * Invoked on Castles when a guild is broken. [Skotlex]
  1290. * @see DBApply
  1291. */
  1292. int castle_guild_broken_sub(DBKey key, DBData *data, va_list ap)
  1293. {
  1294. char name[EVENT_NAME_LENGTH];
  1295. struct guild_castle *gc = db_data2ptr(data);
  1296. int guild_id = va_arg(ap, int);
  1297. nullpo_ret(gc);
  1298. if (gc->guild_id == guild_id) {
  1299. // We call castle_event::OnGuildBreak of all castles of the guild
  1300. // You can set all castle_events in the 'db/castle_db.txt'
  1301. safestrncpy(name, gc->castle_event, sizeof(name));
  1302. npc_event_do(strcat(name, "::OnGuildBreak"));
  1303. //Save the new 'owner', this should invoke guardian clean up and other such things.
  1304. guild_castledatasave(gc->castle_id, 1, 0);
  1305. }
  1306. return 0;
  1307. }
  1308. //Invoked on /breakguild "Guild name"
  1309. int guild_broken(int guild_id,int flag)
  1310. {
  1311. struct guild *g = guild_search(guild_id);
  1312. struct map_session_data *sd = NULL;
  1313. int i;
  1314. if(flag!=0 || g==NULL)
  1315. return 0;
  1316. for(i=0;i<g->max_member;i++){ // ギルド解散を通知
  1317. if((sd=g->member[i].sd)!=NULL){
  1318. if(sd->state.storage_flag == 2)
  1319. storage_guild_storage_quit(sd,1);
  1320. sd->status.guild_id=0;
  1321. clif_guild_broken(g->member[i].sd,0);
  1322. clif_charnameupdate(sd); // [LuzZza]
  1323. }
  1324. }
  1325. guild_db->foreach(guild_db,guild_broken_sub,guild_id);
  1326. castle_db->foreach(castle_db,castle_guild_broken_sub,guild_id);
  1327. guild_storage_delete(guild_id);
  1328. idb_remove(guild_db,guild_id);
  1329. return 0;
  1330. }
  1331. //Changes the Guild Master to the specified player. [Skotlex]
  1332. int guild_gm_change(int guild_id, struct map_session_data *sd)
  1333. {
  1334. struct guild *g;
  1335. nullpo_ret(sd);
  1336. if (sd->status.guild_id != guild_id)
  1337. return 0;
  1338. g=guild_search(guild_id);
  1339. nullpo_ret(g);
  1340. if (strcmp(g->master, sd->status.name) == 0) //Nothing to change.
  1341. return 0;
  1342. //Notify servers that master has changed.
  1343. intif_guild_change_gm(guild_id, sd->status.name, strlen(sd->status.name)+1);
  1344. return 1;
  1345. }
  1346. //Notification from Char server that a guild's master has changed. [Skotlex]
  1347. int guild_gm_changed(int guild_id, int account_id, int char_id)
  1348. {
  1349. struct guild *g;
  1350. struct guild_member gm;
  1351. int pos, i;
  1352. g=guild_search(guild_id);
  1353. if (!g)
  1354. return 0;
  1355. for(pos=0; pos<g->max_member && !(
  1356. g->member[pos].account_id==account_id &&
  1357. g->member[pos].char_id==char_id);
  1358. pos++);
  1359. if (pos == 0 || pos == g->max_member) return 0;
  1360. memcpy(&gm, &g->member[pos], sizeof (struct guild_member));
  1361. memcpy(&g->member[pos], &g->member[0], sizeof(struct guild_member));
  1362. memcpy(&g->member[0], &gm, sizeof(struct guild_member));
  1363. g->member[pos].position = g->member[0].position;
  1364. g->member[0].position = 0; //Position 0: guild Master.
  1365. strcpy(g->master, g->member[0].name);
  1366. if (g->member[pos].sd && g->member[pos].sd->fd)
  1367. {
  1368. clif_displaymessage(g->member[pos].sd->fd, "You no longer are the Guild Master.");
  1369. g->member[pos].sd->state.gmaster_flag = 0;
  1370. }
  1371. if (g->member[0].sd && g->member[0].sd->fd)
  1372. {
  1373. clif_displaymessage(g->member[0].sd->fd, "You have become the Guild Master!");
  1374. g->member[0].sd->state.gmaster_flag = g;
  1375. //Block his skills for 5 minutes to prevent abuse.
  1376. guild_block_skill(g->member[0].sd, 300000);
  1377. }
  1378. // announce the change to all guild members
  1379. for( i = 0; i < g->max_member; i++ )
  1380. {
  1381. if( g->member[i].sd && g->member[i].sd->fd )
  1382. {
  1383. clif_guild_basicinfo(g->member[i].sd);
  1384. clif_guild_memberlist(g->member[i].sd);
  1385. }
  1386. }
  1387. return 1;
  1388. }
  1389. // ギルド解散
  1390. int guild_break(struct map_session_data *sd,char *name)
  1391. {
  1392. struct guild *g;
  1393. int i;
  1394. nullpo_ret(sd);
  1395. if( (g=guild_search(sd->status.guild_id))==NULL )
  1396. return 0;
  1397. if(strcmp(g->name,name)!=0)
  1398. return 0;
  1399. if(!sd->state.gmaster_flag)
  1400. return 0;
  1401. for(i=0;i<g->max_member;i++){
  1402. if( g->member[i].account_id>0 && (
  1403. g->member[i].account_id!=sd->status.account_id ||
  1404. g->member[i].char_id!=sd->status.char_id ))
  1405. break;
  1406. }
  1407. if(i<g->max_member){
  1408. clif_guild_broken(sd,2);
  1409. return 0;
  1410. }
  1411. intif_guild_break(g->guild_id);
  1412. return 0;
  1413. }
  1414. /**
  1415. * Creates a list of guild castle IDs to be requested
  1416. * from char-server.
  1417. */
  1418. void guild_castle_map_init(void)
  1419. {
  1420. DBIterator* iter = NULL;
  1421. int num = db_size(castle_db);
  1422. if (num > 0) {
  1423. struct guild_castle* gc = NULL;
  1424. int *castle_ids, *cursor;
  1425. CREATE(castle_ids, int, num);
  1426. cursor = castle_ids;
  1427. iter = db_iterator(castle_db);
  1428. for (gc = dbi_first(iter); dbi_exists(iter); gc = dbi_next(iter)) {
  1429. *(cursor++) = gc->castle_id;
  1430. }
  1431. dbi_destroy(iter);
  1432. if (intif_guild_castle_dataload(num, castle_ids))
  1433. ShowStatus("Requested '"CL_WHITE"%d"CL_RESET"' guild castles from char-server...\n", num);
  1434. aFree(castle_ids);
  1435. }
  1436. }
  1437. /**
  1438. * Setter function for members of guild_castle struct.
  1439. * Handles all side-effects, like updating guardians.
  1440. * Sends updated info to char-server for saving.
  1441. * @param castle_id Castle ID
  1442. * @param index Type of data to change
  1443. * @param value New value
  1444. */
  1445. int guild_castledatasave(int castle_id, int index, int value)
  1446. {
  1447. struct guild_castle *gc = guild_castle_search(castle_id);
  1448. if (gc == NULL) {
  1449. ShowWarning("guild_castledatasave: guild castle '%d' not found\n", castle_id);
  1450. return 0;
  1451. }
  1452. switch (index) {
  1453. case 1: // The castle's owner has changed? Update or remove Guardians too. [Skotlex]
  1454. {
  1455. int i;
  1456. struct mob_data *gd;
  1457. gc->guild_id = value;
  1458. for (i = 0; i < MAX_GUARDIANS; i++)
  1459. if (gc->guardian[i].visible && (gd = map_id2md(gc->guardian[i].id)) != NULL)
  1460. mob_guardian_guildchange(gd);
  1461. break;
  1462. }
  1463. case 2:
  1464. gc->economy = value; break;
  1465. case 3: // defense invest change -> recalculate guardian hp
  1466. {
  1467. int i;
  1468. struct mob_data *gd;
  1469. gc->defense = value;
  1470. for (i = 0; i < MAX_GUARDIANS; i++)
  1471. if (gc->guardian[i].visible && (gd = map_id2md(gc->guardian[i].id)) != NULL)
  1472. status_calc_mob(gd, 0);
  1473. break;
  1474. }
  1475. case 4:
  1476. gc->triggerE = value; break;
  1477. case 5:
  1478. gc->triggerD = value; break;
  1479. case 6:
  1480. gc->nextTime = value; break;
  1481. case 7:
  1482. gc->payTime = value; break;
  1483. case 8:
  1484. gc->createTime = value; break;
  1485. case 9:
  1486. gc->visibleC = value; break;
  1487. default:
  1488. if (index > 9 && index <= 9+MAX_GUARDIANS) {
  1489. gc->guardian[index-10].visible = value;
  1490. break;
  1491. }
  1492. ShowWarning("guild_castledatasave: index = '%d' is out of allowed range\n", index);
  1493. return 0;
  1494. }
  1495. if (!intif_guild_castle_datasave(castle_id, index, value)) {
  1496. guild_castle_reconnect(castle_id, index, value);
  1497. }
  1498. return 0;
  1499. }
  1500. void guild_castle_reconnect_sub(void *key, void *data, va_list ap)
  1501. {
  1502. int castle_id = GetWord((int)key, 0);
  1503. int index = GetWord((int)key, 1);
  1504. intif_guild_castle_datasave(castle_id, index, *(int *)data);
  1505. aFree(data);
  1506. }
  1507. /**
  1508. * Saves pending guild castle data changes when char-server is
  1509. * disconnected.
  1510. * On reconnect pushes all changes to char-server for saving.
  1511. */
  1512. void guild_castle_reconnect(int castle_id, int index, int value)
  1513. {
  1514. static struct linkdb_node *gc_save_pending = NULL;
  1515. if (castle_id < 0) { // char-server reconnected
  1516. linkdb_foreach(&gc_save_pending, guild_castle_reconnect_sub);
  1517. linkdb_final(&gc_save_pending);
  1518. } else {
  1519. int *data;
  1520. CREATE(data, int, 1);
  1521. *data = value;
  1522. linkdb_replace(&gc_save_pending, (void*)(MakeDWord(castle_id, index)), data);
  1523. }
  1524. }
  1525. // ギルドデータ一括受信(初期化時)
  1526. int guild_castledataloadack(int len, struct guild_castle *gc)
  1527. {
  1528. int i;
  1529. int n = (len-4) / sizeof(struct guild_castle);
  1530. int ev;
  1531. nullpo_ret(gc);
  1532. //Last owned castle in the list invokes ::OnAgitInit
  1533. for( i = n-1; i >= 0 && !(gc[i].guild_id); --i );
  1534. ev = i; // offset of castle or -1
  1535. if( ev < 0 ) { //No castles owned, invoke OnAgitInit as it is.
  1536. npc_event_doall("OnAgitInit");
  1537. npc_event_doall("OnAgitInit2");
  1538. }
  1539. else // load received castles into memory, one by one
  1540. for( i = 0; i < n; i++, gc++ )
  1541. {
  1542. struct guild_castle *c = guild_castle_search(gc->castle_id);
  1543. if (!c) {
  1544. ShowError("guild_castledataloadack: castle id=%d not found.\n", gc->castle_id);
  1545. continue;
  1546. }
  1547. // update map-server castle data with new info
  1548. memcpy(&c->guild_id, &gc->guild_id, sizeof(struct guild_castle) - offsetof(struct guild_castle, guild_id));
  1549. if( c->guild_id )
  1550. {
  1551. if( i != ev )
  1552. guild_request_info(c->guild_id);
  1553. else { // last owned one
  1554. guild_npc_request_info(c->guild_id, "::OnAgitInit");
  1555. guild_npc_request_info(c->guild_id, "::OnAgitInit2");
  1556. }
  1557. }
  1558. }
  1559. ShowStatus("Received '"CL_WHITE"%d"CL_RESET"' guild castles from char-server.\n", n);
  1560. return 0;
  1561. }
  1562. int guild_agit_start(void)
  1563. { // Run All NPC_Event[OnAgitStart]
  1564. int c = npc_event_doall("OnAgitStart");
  1565. ShowStatus("NPC_Event:[OnAgitStart] Run (%d) Events by @AgitStart.\n",c);
  1566. return 0;
  1567. }
  1568. int guild_agit_end(void)
  1569. { // Run All NPC_Event[OnAgitEnd]
  1570. int c = npc_event_doall("OnAgitEnd");
  1571. ShowStatus("NPC_Event:[OnAgitEnd] Run (%d) Events by @AgitEnd.\n",c);
  1572. return 0;
  1573. }
  1574. int guild_agit2_start(void)
  1575. { // Run All NPC_Event[OnAgitStart2]
  1576. int c = npc_event_doall("OnAgitStart2");
  1577. ShowStatus("NPC_Event:[OnAgitStart2] Run (%d) Events by @AgitStart2.\n",c);
  1578. return 0;
  1579. }
  1580. int guild_agit2_end(void)
  1581. { // Run All NPC_Event[OnAgitEnd2]
  1582. int c = npc_event_doall("OnAgitEnd2");
  1583. ShowStatus("NPC_Event:[OnAgitEnd2] Run (%d) Events by @AgitEnd2.\n",c);
  1584. return 0;
  1585. }
  1586. // How many castles does this guild have?
  1587. int guild_checkcastles(struct guild *g)
  1588. {
  1589. int nb_cas = 0;
  1590. struct guild_castle* gc = NULL;
  1591. DBIterator *iter = db_iterator(castle_db);
  1592. for (gc = dbi_first(iter); dbi_exists(iter); gc = dbi_next(iter)) {
  1593. if (gc->guild_id == g->guild_id) {
  1594. nb_cas++;
  1595. }
  1596. }
  1597. dbi_destroy(iter);
  1598. return nb_cas;
  1599. }
  1600. // Are these two guilds allied?
  1601. bool guild_isallied(int guild_id, int guild_id2)
  1602. {
  1603. int i;
  1604. struct guild* g = guild_search(guild_id);
  1605. nullpo_ret(g);
  1606. ARR_FIND( 0, MAX_GUILDALLIANCE, i, g->alliance[i].guild_id == guild_id2 );
  1607. return( i < MAX_GUILDALLIANCE && g->alliance[i].opposition == 0 );
  1608. }
  1609. /**
  1610. * @see DBApply
  1611. */
  1612. static int eventlist_db_final(DBKey key, DBData *data, va_list ap)
  1613. {
  1614. struct eventlist *next = NULL;
  1615. struct eventlist *current = db_data2ptr(data);
  1616. while (current != NULL) {
  1617. next = current->next;
  1618. aFree(current);
  1619. current = next;
  1620. }
  1621. return 0;
  1622. }
  1623. /**
  1624. * @see DBApply
  1625. */
  1626. static int guild_castle_db_final(DBKey key, DBData *data, va_list ap)
  1627. {
  1628. struct guild_castle* gc = db_data2ptr(data);
  1629. if( gc->temp_guardians )
  1630. aFree(gc->temp_guardians);
  1631. aFree(gc);
  1632. return 0;
  1633. }
  1634. void do_init_guild(void)
  1635. {
  1636. guild_db=idb_alloc(DB_OPT_RELEASE_DATA);
  1637. castle_db=idb_alloc(DB_OPT_BASE);
  1638. guild_expcache_db=idb_alloc(DB_OPT_RELEASE_DATA);
  1639. guild_infoevent_db=idb_alloc(DB_OPT_BASE);
  1640. sv_readdb(db_path, "castle_db.txt", ',', 4, 5, -1, &guild_read_castledb);
  1641. memset(guild_skill_tree,0,sizeof(guild_skill_tree));
  1642. sv_readdb(db_path, "guild_skill_tree.txt", ',', 2+MAX_GUILD_SKILL_REQUIRE*2, 2+MAX_GUILD_SKILL_REQUIRE*2, -1, &guild_read_guildskill_tree_db); //guild skill tree [Komurka]
  1643. add_timer_func_list(guild_payexp_timer,"guild_payexp_timer");
  1644. add_timer_func_list(guild_send_xy_timer, "guild_send_xy_timer");
  1645. add_timer_interval(gettick()+GUILD_PAYEXP_INVERVAL,guild_payexp_timer,0,0,GUILD_PAYEXP_INVERVAL);
  1646. add_timer_interval(gettick()+GUILD_SEND_XY_INVERVAL,guild_send_xy_timer,0,0,GUILD_SEND_XY_INVERVAL);
  1647. }
  1648. void do_final_guild(void) {
  1649. db_destroy(guild_db);
  1650. castle_db->destroy(castle_db,guild_castle_db_final);
  1651. guild_infoevent_db->destroy(guild_infoevent_db,eventlist_db_final);
  1652. }