guild.c 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098
  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. //Constant related to the flash of the Guild EXP cache
  36. #define GUILD_SEND_XY_INVERVAL 5000 // Interval of sending coordinates and HP
  37. #define GUILD_PAYEXP_INVERVAL 10000 //Interval (maximum survival time of the cache, in milliseconds)
  38. #define GUILD_PAYEXP_LIST 8192 //The maximum number of cache
  39. //Guild EXP cache
  40. struct guild_expcache {
  41. int guild_id, account_id, char_id;
  42. uint64 exp;
  43. };
  44. static struct eri *expcache_ers; //For handling of guild exp payment.
  45. #define MAX_GUILD_SKILL_REQUIRE 5
  46. struct{
  47. int id;
  48. int max;
  49. struct{
  50. short id;
  51. short lv;
  52. }need[MAX_GUILD_SKILL_REQUIRE];
  53. } guild_skill_tree[MAX_GUILDSKILL];
  54. int guild_payexp_timer(int tid, unsigned int tick, int id, intptr_t data);
  55. static int guild_send_xy_timer(int tid, unsigned int tick, int id, intptr_t data);
  56. /*==========================================
  57. * Retrieves and validates the sd pointer for this guild member [Skotlex]
  58. *------------------------------------------*/
  59. static TBL_PC* guild_sd_check(int guild_id, int account_id, int char_id)
  60. {
  61. TBL_PC* sd = map_id2sd(account_id);
  62. if (!(sd && sd->status.char_id == char_id))
  63. return NULL;
  64. if (sd->status.guild_id != guild_id)
  65. { //If player belongs to a different guild, kick him out.
  66. intif_guild_leave(guild_id,account_id,char_id,0,"** Guild Mismatch **");
  67. return NULL;
  68. }
  69. return sd;
  70. }
  71. // Modified [Komurka]
  72. int guild_skill_get_max (int id)
  73. {
  74. if (id < GD_SKILLBASE || id >= GD_SKILLBASE+MAX_GUILDSKILL)
  75. return 0;
  76. return guild_skill_tree[id-GD_SKILLBASE].max;
  77. }
  78. // Retrive skilllv learned by guild
  79. int guild_checkskill(struct guild *g, int id) {
  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. //Creation of member information
  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. * Server cache to be flushed to inter the Guild EXP
  232. * @see DBApply
  233. */
  234. int guild_payexp_timer_sub(DBKey key, DBData *data, va_list ap) {
  235. int i;
  236. struct guild_expcache *c;
  237. struct guild *g;
  238. c = db_data2ptr(data);
  239. if (
  240. (g = guild_search(c->guild_id)) == NULL ||
  241. (i = guild_getindex(g, c->account_id, c->char_id)) < 0
  242. ) {
  243. ers_free(expcache_ers, c);
  244. return 0;
  245. }
  246. if (g->member[i].exp > UINT64_MAX - c->exp)
  247. g->member[i].exp = UINT64_MAX;
  248. else
  249. g->member[i].exp+= c->exp;
  250. intif_guild_change_memberinfo(g->guild_id,c->account_id,c->char_id,
  251. GMI_EXP,&g->member[i].exp,sizeof(g->member[i].exp));
  252. c->exp=0;
  253. ers_free(expcache_ers, c);
  254. return 0;
  255. }
  256. int guild_payexp_timer(int tid, unsigned int tick, int id, intptr_t data)
  257. {
  258. guild_expcache_db->clear(guild_expcache_db,guild_payexp_timer_sub);
  259. return 0;
  260. }
  261. /**
  262. * Taken from party_send_xy_timer_sub. [Skotlex]
  263. * @see DBApply
  264. */
  265. int guild_send_xy_timer_sub(DBKey key, DBData *data, va_list ap)
  266. {
  267. struct guild *g = db_data2ptr(data);
  268. int i;
  269. nullpo_ret(g);
  270. if( !g->connect_member )
  271. {// no members connected to this guild so do not iterate
  272. return 0;
  273. }
  274. for(i=0;i<g->max_member;i++){
  275. struct map_session_data* sd = g->member[i].sd;
  276. if( sd != NULL && sd->fd && (sd->guild_x != sd->bl.x || sd->guild_y != sd->bl.y) && !sd->bg_id )
  277. {
  278. clif_guild_xy(sd);
  279. sd->guild_x = sd->bl.x;
  280. sd->guild_y = sd->bl.y;
  281. }
  282. }
  283. return 0;
  284. }
  285. //Code from party_send_xy_timer [Skotlex]
  286. static int guild_send_xy_timer(int tid, unsigned int tick, int id, intptr_t data)
  287. {
  288. guild_db->foreach(guild_db,guild_send_xy_timer_sub,tick);
  289. return 0;
  290. }
  291. int guild_send_dot_remove(struct map_session_data *sd)
  292. {
  293. if (sd->status.guild_id)
  294. clif_guild_xy_remove(sd);
  295. return 0;
  296. }
  297. //------------------------------------------------------------------------
  298. int guild_create(struct map_session_data *sd, const char *name)
  299. {
  300. char tname[NAME_LENGTH];
  301. struct guild_member m;
  302. nullpo_ret(sd);
  303. safestrncpy(tname, name, NAME_LENGTH);
  304. trim(tname);
  305. if( !tname[0] )
  306. return 0; // empty name
  307. if( sd->status.guild_id )
  308. {// already in a guild
  309. clif_guild_created(sd,1);
  310. return 0;
  311. }
  312. if( battle_config.guild_emperium_check && pc_search_inventory(sd,714) == -1 )
  313. {// item required
  314. clif_guild_created(sd,3);
  315. return 0;
  316. }
  317. guild_makemember(&m,sd);
  318. m.position=0;
  319. intif_guild_create(name,&m);
  320. return 1;
  321. }
  322. //Whether or not to create guild
  323. int guild_created(int account_id,int guild_id)
  324. {
  325. struct map_session_data *sd=map_id2sd(account_id);
  326. if(sd==NULL)
  327. return 0;
  328. if(!guild_id) {
  329. clif_guild_created(sd, 2); // Creation failure (presence of the same name Guild)
  330. return 0;
  331. }
  332. //struct guild *g;
  333. sd->status.guild_id=guild_id;
  334. clif_guild_created(sd,0);
  335. if(battle_config.guild_emperium_check)
  336. pc_delitem(sd,pc_search_inventory(sd,714),1,0,0,LOG_TYPE_CONSUME); // �G���y���E������
  337. return 0;
  338. }
  339. //Information request
  340. int guild_request_info(int guild_id)
  341. {
  342. return intif_guild_request_info(guild_id);
  343. }
  344. //Information request with event
  345. int guild_npc_request_info(int guild_id,const char *event)
  346. {
  347. if( guild_search(guild_id) )
  348. {
  349. if( event && *event )
  350. npc_event_do(event);
  351. return 0;
  352. }
  353. if( event && *event )
  354. {
  355. struct eventlist *ev;
  356. DBData prev;
  357. ev=(struct eventlist *)aCalloc(sizeof(struct eventlist),1);
  358. memcpy(ev->name,event,strlen(event));
  359. //The one in the db (if present) becomes the next event from this.
  360. if (guild_infoevent_db->put(guild_infoevent_db, db_i2key(guild_id), db_ptr2data(ev), &prev))
  361. ev->next = db_data2ptr(&prev);
  362. }
  363. return guild_request_info(guild_id);
  364. }
  365. //Confirmation of the character belongs to guild
  366. int guild_check_member(struct guild *g)
  367. {
  368. int i;
  369. struct map_session_data *sd;
  370. struct s_mapiterator* iter;
  371. nullpo_ret(g);
  372. iter = mapit_getallusers();
  373. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  374. {
  375. if( sd->status.guild_id != g->guild_id )
  376. continue;
  377. i = guild_getindex(g,sd->status.account_id,sd->status.char_id);
  378. if (i < 0) {
  379. sd->status.guild_id=0;
  380. sd->guild_emblem_id=0;
  381. ShowWarning("guild: check_member %d[%s] is not member\n",sd->status.account_id,sd->status.name);
  382. }
  383. }
  384. mapit_free(iter);
  385. return 0;
  386. }
  387. //Delete association with guild_id for all characters
  388. int guild_recv_noinfo(int guild_id)
  389. {
  390. struct map_session_data *sd;
  391. struct s_mapiterator* iter;
  392. iter = mapit_getallusers();
  393. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  394. {
  395. if( sd->status.guild_id == guild_id )
  396. sd->status.guild_id = 0; // erase guild
  397. }
  398. mapit_free(iter);
  399. return 0;
  400. }
  401. //Get and display information for all member
  402. int guild_recv_info(struct guild *sg)
  403. {
  404. struct guild *g,before;
  405. int i,bm,m;
  406. DBData data;
  407. struct map_session_data *sd;
  408. bool guild_new = false;
  409. nullpo_ret(sg);
  410. if((g = (struct guild*)idb_get(guild_db,sg->guild_id))==NULL)
  411. {
  412. guild_new = true;
  413. g=(struct guild *)aCalloc(1,sizeof(struct guild));
  414. idb_put(guild_db,sg->guild_id,g);
  415. before=*sg;
  416. //Perform the check on the user because the first load
  417. guild_check_member(sg);
  418. if ((sd = map_nick2sd(sg->master)) != NULL)
  419. {
  420. //If the guild master is online the first time the guild_info is received,
  421. //that means he was the first to join, so apply guild skill blocking here.
  422. if( battle_config.guild_skill_relog_delay )
  423. guild_block_skill(sd, 300000);
  424. //Also set the guild master flag.
  425. sd->state.gmaster_flag = g;
  426. clif_charnameupdate(sd); // [LuzZza]
  427. clif_guild_masterormember(sd);
  428. }
  429. }else
  430. before=*g;
  431. memcpy(g,sg,sizeof(struct guild));
  432. if(g->max_member > MAX_GUILD)
  433. {
  434. 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);
  435. g->max_member = MAX_GUILD;
  436. }
  437. for(i=bm=m=0;i<g->max_member;i++){
  438. if(g->member[i].account_id>0){
  439. sd = g->member[i].sd = guild_sd_check(g->guild_id, g->member[i].account_id, g->member[i].char_id);
  440. if (sd) clif_charnameupdate(sd); // [LuzZza]
  441. m++;
  442. }else
  443. g->member[i].sd=NULL;
  444. if(before.member[i].account_id>0)
  445. bm++;
  446. }
  447. for (i = 0; i < g->max_member; i++) { //Transmission of information at all members
  448. sd = g->member[i].sd;
  449. if( sd==NULL )
  450. continue;
  451. if (before.guild_lv != g->guild_lv || bm != m ||
  452. before.max_member != g->max_member) {
  453. clif_guild_basicinfo(sd); //Submit basic information
  454. clif_guild_emblem(sd, g); //Submit emblem
  455. }
  456. if (bm != m) { //Send members information
  457. clif_guild_memberlist(g->member[i].sd);
  458. }
  459. if (before.skill_point != g->skill_point)
  460. clif_guild_skillinfo(sd); //Submit information skills
  461. if (guild_new) { // Send information and affiliation if unsent
  462. clif_guild_belonginfo(sd, g);
  463. clif_guild_notice(sd, g);
  464. sd->guild_emblem_id = g->emblem_id;
  465. }
  466. }
  467. //Occurrence of an event
  468. if (guild_infoevent_db->remove(guild_infoevent_db, db_i2key(sg->guild_id), &data))
  469. {
  470. struct eventlist *ev = db_data2ptr(&data), *ev2;
  471. while(ev){
  472. npc_event_do(ev->name);
  473. ev2=ev->next;
  474. aFree(ev);
  475. ev=ev2;
  476. }
  477. }
  478. return 0;
  479. }
  480. /*=============================================
  481. * Player sd send a guild invatation to player tsd to join his guild
  482. *--------------------------------------------*/
  483. int guild_invite(struct map_session_data *sd, struct map_session_data *tsd) {
  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) { //checking if there no other invitation pending
  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. //search an empty spot in guild
  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. * Add a player to a given guild_id
  596. *----------------------------------------*/
  597. int guild_member_added(int guild_id,int account_id,int char_id,int flag)
  598. {
  599. struct map_session_data *sd= map_id2sd(account_id),*sd2;
  600. struct guild *g;
  601. if( (g=guild_search(guild_id))==NULL )
  602. return 0;
  603. if(sd==NULL || sd->guild_invite==0){
  604. // cancel if player not present or invalide guild_id invitation
  605. if (flag == 0) {
  606. ShowError("guild: member added error %d is not online\n",account_id);
  607. intif_guild_leave(guild_id,account_id,char_id,0,"** Data Error **");
  608. }
  609. return 0;
  610. }
  611. sd2 = map_id2sd(sd->guild_invite_account);
  612. sd->guild_invite = 0;
  613. sd->guild_invite_account = 0;
  614. if (flag == 1) { //failure
  615. if( sd2!=NULL )
  616. clif_guild_inviteack(sd2,3);
  617. return 0;
  618. }
  619. //if all ok add player to guild
  620. sd->status.guild_id = g->guild_id;
  621. sd->guild_emblem_id = g->emblem_id;
  622. //Packets which were sent in the previous 'guild_sent' implementation.
  623. clif_guild_belonginfo(sd,g);
  624. clif_guild_notice(sd,g);
  625. //TODO: send new emblem info to others
  626. if( sd2!=NULL )
  627. clif_guild_inviteack(sd2,2);
  628. //Next line commented because it do nothing, look at guild_recv_info [LuzZza]
  629. //clif_charnameupdate(sd); //Update display name [Skotlex]
  630. return 0;
  631. }
  632. /*==========================================
  633. * Player request leaving a given guild_id
  634. *----------------------------------------*/
  635. int guild_leave(struct map_session_data* sd, int guild_id, int account_id, int char_id, const char* mes)
  636. {
  637. struct guild *g;
  638. nullpo_ret(sd);
  639. g = guild_search(sd->status.guild_id);
  640. if(g==NULL)
  641. return 0;
  642. if(sd->status.account_id!=account_id ||
  643. sd->status.char_id!=char_id || sd->status.guild_id!=guild_id ||
  644. ((agit_flag || agit2_flag) && map[sd->bl.m].flag.gvg_castle))
  645. return 0;
  646. intif_guild_leave(sd->status.guild_id, sd->status.account_id, sd->status.char_id,0,mes);
  647. return 0;
  648. }
  649. /*==========================================
  650. * Request remove a player to a given guild_id
  651. *----------------------------------------*/
  652. int guild_expulsion(struct map_session_data* sd, int guild_id, int account_id, int char_id, const char* mes)
  653. {
  654. struct map_session_data *tsd;
  655. struct guild *g;
  656. int i,ps;
  657. nullpo_ret(sd);
  658. g = guild_search(sd->status.guild_id);
  659. if(g==NULL)
  660. return 0;
  661. if(sd->status.guild_id!=guild_id)
  662. return 0;
  663. if( (ps=guild_getposition(g,sd))<0 || !(g->position[ps].mode&0x0010) )
  664. return 0; //Expulsion permission
  665. //Can't leave inside guild castles.
  666. if ((tsd = map_id2sd(account_id)) &&
  667. tsd->status.char_id == char_id &&
  668. ((agit_flag || agit2_flag) && map[tsd->bl.m].flag.gvg_castle))
  669. return 0;
  670. // find the member and perform expulsion
  671. i = guild_getindex(g, account_id, char_id);
  672. if( i != -1 && strcmp(g->member[i].name,g->master) != 0 ) //Can't expel the GL!
  673. intif_guild_leave(g->guild_id,account_id,char_id,1,mes);
  674. return 0;
  675. }
  676. int guild_member_withdraw(int guild_id, int account_id, int char_id, int flag, const char* name, const char* mes)
  677. {
  678. int i;
  679. struct guild* g = guild_search(guild_id);
  680. struct map_session_data* sd = map_charid2sd(char_id);
  681. struct map_session_data* online_member_sd;
  682. if(g == NULL)
  683. return 0; // no such guild (error!)
  684. i = guild_getindex(g, account_id, char_id);
  685. if( i == -1 )
  686. return 0; // not a member (inconsistency!)
  687. online_member_sd = guild_getavailablesd(g);
  688. if(online_member_sd == NULL)
  689. return 0; // noone online to inform
  690. if(!flag)
  691. clif_guild_leave(online_member_sd, name, mes);
  692. else
  693. clif_guild_expulsion(online_member_sd, name, mes, account_id);
  694. // remove member from guild
  695. memset(&g->member[i],0,sizeof(struct guild_member));
  696. clif_guild_memberlist(online_member_sd);
  697. // update char, if online
  698. if(sd != NULL && sd->status.guild_id == guild_id)
  699. {
  700. // do stuff that needs the guild_id first, BEFORE we wipe it
  701. if (sd->state.storage_flag == 2) //Close the guild storage.
  702. storage_guild_storageclose(sd);
  703. guild_send_dot_remove(sd);
  704. sd->status.guild_id = 0;
  705. sd->guild_emblem_id = 0;
  706. clif_charnameupdate(sd); //Update display name [Skotlex]
  707. //TODO: send emblem update to self and people around
  708. }
  709. return 0;
  710. }
  711. int guild_send_memberinfoshort(struct map_session_data *sd,int online)
  712. { // cleaned up [LuzZza]
  713. struct guild *g;
  714. nullpo_ret(sd);
  715. if(sd->status.guild_id <= 0)
  716. return 0;
  717. if(!(g = guild_search(sd->status.guild_id)))
  718. return 0;
  719. intif_guild_memberinfoshort(g->guild_id,
  720. sd->status.account_id,sd->status.char_id,online,sd->status.base_level,sd->status.class_);
  721. if(!online){
  722. int i=guild_getindex(g,sd->status.account_id,sd->status.char_id);
  723. if(i>=0)
  724. g->member[i].sd=NULL;
  725. else
  726. 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);
  727. return 0;
  728. }
  729. if(sd->state.connect_new)
  730. { //Note that this works because it is invoked in parse_LoadEndAck before connect_new is cleared.
  731. clif_guild_belonginfo(sd,g);
  732. clif_guild_notice(sd,g);
  733. sd->guild_emblem_id = g->emblem_id;
  734. }
  735. return 0;
  736. }
  737. int guild_recv_memberinfoshort(int guild_id,int account_id,int char_id,int online,int lv,int class_)
  738. { // cleaned up [LuzZza]
  739. int i,alv,c,idx=-1,om=0,oldonline=-1;
  740. struct guild *g = guild_search(guild_id);
  741. if(g == NULL)
  742. return 0;
  743. for(i=0,alv=0,c=0,om=0;i<g->max_member;i++){
  744. struct guild_member *m=&g->member[i];
  745. if(!m->account_id) continue;
  746. if(m->account_id==account_id && m->char_id==char_id ){
  747. oldonline=m->online;
  748. m->online=online;
  749. m->lv=lv;
  750. m->class_=class_;
  751. idx=i;
  752. }
  753. alv+=m->lv;
  754. c++;
  755. if(m->online)
  756. om++;
  757. }
  758. if(idx == -1 || c == 0) {
  759. //Treat char_id who doesn't match guild_id (not found as member)
  760. struct map_session_data *sd = map_id2sd(account_id);
  761. if(sd && sd->status.char_id == char_id) {
  762. sd->status.guild_id=0;
  763. sd->guild_emblem_id=0;
  764. }
  765. ShowWarning("guild: not found member %d,%d on %d[%s]\n", account_id,char_id,guild_id,g->name);
  766. return 0;
  767. }
  768. g->average_lv=alv/c;
  769. g->connect_member=om;
  770. //Ensure validity of pointer (ie: player logs in/out, changes map-server)
  771. g->member[idx].sd = guild_sd_check(guild_id, account_id, char_id);
  772. if(oldonline!=online)
  773. clif_guild_memberlogin_notice(g, idx, online);
  774. if(!g->member[idx].sd)
  775. return 0;
  776. //Send XY dot updates. [Skotlex]
  777. //Moved from guild_send_memberinfoshort [LuzZza]
  778. for(i=0; i < g->max_member; i++) {
  779. if(!g->member[i].sd || i == idx ||
  780. g->member[i].sd->bl.m != g->member[idx].sd->bl.m)
  781. continue;
  782. clif_guild_xy_single(g->member[idx].sd->fd, g->member[i].sd);
  783. clif_guild_xy_single(g->member[i].sd->fd, g->member[idx].sd);
  784. }
  785. return 0;
  786. }
  787. /*====================================================
  788. * Send a message to whole guild
  789. *---------------------------------------------------*/
  790. int guild_send_message(struct map_session_data *sd,const char *mes,int len)
  791. {
  792. nullpo_ret(sd);
  793. if(sd->status.guild_id==0)
  794. return 0;
  795. intif_guild_message(sd->status.guild_id,sd->status.account_id,mes,len);
  796. guild_recv_message(sd->status.guild_id,sd->status.account_id,mes,len);
  797. // Chat logging type 'G' / Guild Chat
  798. 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);
  799. return 0;
  800. }
  801. /*====================================================
  802. * Guild receive a message, will be displayed to whole member
  803. *---------------------------------------------------*/
  804. int guild_recv_message(int guild_id,int account_id,const char *mes,int len)
  805. {
  806. struct guild *g;
  807. if( (g=guild_search(guild_id))==NULL)
  808. return 0;
  809. clif_guild_message(g,account_id,mes,len);
  810. return 0;
  811. }
  812. /*====================================================
  813. * Member changing position in guild
  814. *---------------------------------------------------*/
  815. int guild_change_memberposition(int guild_id,int account_id,int char_id,short idx)
  816. {
  817. return intif_guild_change_memberinfo(guild_id,account_id,char_id,GMI_POSITION,&idx,sizeof(idx));
  818. }
  819. /*====================================================
  820. * Notification of new position for member
  821. *---------------------------------------------------*/
  822. int guild_memberposition_changed(struct guild *g,int idx,int pos)
  823. {
  824. nullpo_ret(g);
  825. g->member[idx].position=pos;
  826. clif_guild_memberpositionchanged(g,idx);
  827. // Update char position in client [LuzZza]
  828. if(g->member[idx].sd != NULL)
  829. clif_charnameupdate(g->member[idx].sd);
  830. return 0;
  831. }
  832. /*====================================================
  833. * Change guild title or member
  834. *---------------------------------------------------*/
  835. int guild_change_position(int guild_id,int idx,
  836. int mode,int exp_mode,const char *name)
  837. {
  838. struct guild_position p;
  839. exp_mode = cap_value(exp_mode, 0, battle_config.guild_exp_limit);
  840. //Mode 0x01 <- Invite
  841. //Mode 0x10 <- Expel.
  842. p.mode=mode&0x11;
  843. p.exp_mode=exp_mode;
  844. safestrncpy(p.name,name,NAME_LENGTH);
  845. return intif_guild_position(guild_id,idx,&p);
  846. }
  847. /*====================================================
  848. * Notification of member has changed his guild title
  849. *---------------------------------------------------*/
  850. int guild_position_changed(int guild_id,int idx,struct guild_position *p)
  851. {
  852. struct guild *g=guild_search(guild_id);
  853. int i;
  854. if(g==NULL)
  855. return 0;
  856. memcpy(&g->position[idx],p,sizeof(struct guild_position));
  857. clif_guild_positionchanged(g,idx);
  858. // Update char name in client [LuzZza]
  859. for(i=0;i<g->max_member;i++)
  860. if(g->member[i].position == idx && g->member[i].sd != NULL)
  861. clif_charnameupdate(g->member[i].sd);
  862. return 0;
  863. }
  864. /*====================================================
  865. * Change guild notice
  866. *---------------------------------------------------*/
  867. int guild_change_notice(struct map_session_data *sd,int guild_id,const char *mes1,const char *mes2)
  868. {
  869. nullpo_ret(sd);
  870. if(guild_id!=sd->status.guild_id)
  871. return 0;
  872. return intif_guild_notice(guild_id,mes1,mes2);
  873. }
  874. /*====================================================
  875. * Notification of guild has changed his notice
  876. *---------------------------------------------------*/
  877. int guild_notice_changed(int guild_id,const char *mes1,const char *mes2)
  878. {
  879. int i;
  880. struct map_session_data *sd;
  881. struct guild *g=guild_search(guild_id);
  882. if(g==NULL)
  883. return 0;
  884. memcpy(g->mes1,mes1,MAX_GUILDMES1);
  885. memcpy(g->mes2,mes2,MAX_GUILDMES2);
  886. for(i=0;i<g->max_member;i++){
  887. if((sd=g->member[i].sd)!=NULL)
  888. clif_guild_notice(sd,g);
  889. }
  890. return 0;
  891. }
  892. /*====================================================
  893. * Change guild emblem
  894. *---------------------------------------------------*/
  895. int guild_change_emblem(struct map_session_data *sd,int len,const char *data)
  896. {
  897. struct guild *g;
  898. nullpo_ret(sd);
  899. if (battle_config.require_glory_guild &&
  900. !((g = guild_search(sd->status.guild_id)) && guild_checkskill(g, GD_GLORYGUILD)>0)) {
  901. clif_skill_fail(sd,GD_GLORYGUILD,USESKILL_FAIL_LEVEL,0);
  902. return 0;
  903. }
  904. return intif_guild_emblem(sd->status.guild_id,len,data);
  905. }
  906. /*====================================================
  907. * Notification of guild emblem changed
  908. *---------------------------------------------------*/
  909. int guild_emblem_changed(int len,int guild_id,int emblem_id,const char *data)
  910. {
  911. int i;
  912. struct map_session_data *sd;
  913. struct guild *g=guild_search(guild_id);
  914. if(g==NULL)
  915. return 0;
  916. memcpy(g->emblem_data,data,len);
  917. g->emblem_len=len;
  918. g->emblem_id=emblem_id;
  919. for(i=0;i<g->max_member;i++){
  920. if((sd=g->member[i].sd)!=NULL){
  921. sd->guild_emblem_id=emblem_id;
  922. clif_guild_belonginfo(sd,g);
  923. clif_guild_emblem(sd,g);
  924. clif_guild_emblem_area(&sd->bl);
  925. }
  926. }
  927. {// update guardians (mobs)
  928. DBIterator* iter = db_iterator(castle_db);
  929. struct guild_castle* gc;
  930. for( gc = (struct guild_castle*)dbi_first(iter) ; dbi_exists(iter); gc = (struct guild_castle*)dbi_next(iter) )
  931. {
  932. if( gc->guild_id != guild_id )
  933. continue;
  934. // update permanent guardians
  935. for( i = 0; i < ARRAYLENGTH(gc->guardian); ++i )
  936. {
  937. TBL_MOB* md = (gc->guardian[i].id ? map_id2md(gc->guardian[i].id) : NULL);
  938. if( md == NULL || md->guardian_data == NULL )
  939. continue;
  940. md->guardian_data->emblem_id = emblem_id;
  941. clif_guild_emblem_area(&md->bl);
  942. }
  943. // update temporary guardians
  944. for( i = 0; i < gc->temp_guardians_max; ++i )
  945. {
  946. TBL_MOB* md = (gc->temp_guardians[i] ? map_id2md(gc->temp_guardians[i]) : NULL);
  947. if( md == NULL || md->guardian_data == NULL )
  948. continue;
  949. md->guardian_data->emblem_id = emblem_id;
  950. clif_guild_emblem_area(&md->bl);
  951. }
  952. }
  953. dbi_destroy(iter);
  954. }
  955. {// update npcs (flags or other npcs that used flagemblem to attach to this guild)
  956. // TODO this is not efficient [FlavioJS]
  957. struct s_mapiterator* iter = mapit_geteachnpc();
  958. TBL_NPC* nd;
  959. for( nd = (TBL_NPC*)mapit_first(iter) ; mapit_exists(iter); nd = (TBL_NPC*)mapit_next(iter) )
  960. {
  961. if( nd->subtype != SCRIPT || nd->u.scr.guild_id != guild_id )
  962. continue;
  963. clif_guild_emblem_area(&nd->bl);
  964. }
  965. mapit_free(iter);
  966. }
  967. return 0;
  968. }
  969. /**
  970. * @see DBCreateData
  971. */
  972. static DBData create_expcache(DBKey key, va_list args)
  973. {
  974. struct guild_expcache *c;
  975. struct map_session_data *sd = va_arg(args, struct map_session_data*);
  976. c = ers_alloc(expcache_ers, struct guild_expcache);
  977. c->guild_id = sd->status.guild_id;
  978. c->account_id = sd->status.account_id;
  979. c->char_id = sd->status.char_id;
  980. c->exp = 0;
  981. return db_ptr2data(c);
  982. }
  983. /*====================================================
  984. * Return taxed experience from player sd to guild
  985. *---------------------------------------------------*/
  986. unsigned int guild_payexp(struct map_session_data *sd,unsigned int exp)
  987. {
  988. struct guild *g;
  989. struct guild_expcache *c;
  990. int per;
  991. nullpo_ret(sd);
  992. if (!exp) return 0;
  993. if (sd->status.guild_id == 0 ||
  994. (g = guild_search(sd->status.guild_id)) == NULL ||
  995. (per = guild_getposition(g,sd)) < 0 ||
  996. (per = g->position[per].exp_mode) < 1)
  997. return 0;
  998. if (per < 100)
  999. exp = exp * per / 100;
  1000. //Otherwise tax everything.
  1001. c = db_data2ptr(guild_expcache_db->ensure(guild_expcache_db, db_i2key(sd->status.char_id), create_expcache, sd));
  1002. if (c->exp > UINT64_MAX - exp)
  1003. c->exp = UINT64_MAX;
  1004. else
  1005. c->exp += exp;
  1006. return exp;
  1007. }
  1008. /*====================================================
  1009. * Player sd pay a tribute experience to his guild
  1010. * Add this experience to guild exp
  1011. * [Celest]
  1012. *---------------------------------------------------*/
  1013. int guild_getexp(struct map_session_data *sd,int exp)
  1014. {
  1015. struct guild_expcache *c;
  1016. nullpo_ret(sd);
  1017. if (sd->status.guild_id == 0 || guild_search(sd->status.guild_id) == NULL)
  1018. return 0;
  1019. c = db_data2ptr(guild_expcache_db->ensure(guild_expcache_db, db_i2key(sd->status.char_id), create_expcache, sd));
  1020. if (c->exp > UINT64_MAX - exp)
  1021. c->exp = UINT64_MAX;
  1022. else
  1023. c->exp += exp;
  1024. return exp;
  1025. }
  1026. /*====================================================
  1027. * Ask to increase guildskill skill_num
  1028. *---------------------------------------------------*/
  1029. int guild_skillup(TBL_PC* sd, int skill_num)
  1030. {
  1031. struct guild* g;
  1032. int idx = skill_num - GD_SKILLBASE;
  1033. int max = guild_skill_get_max(skill_num);
  1034. nullpo_ret(sd);
  1035. if( idx < 0 || idx >= MAX_GUILDSKILL || // not a guild skill
  1036. sd->status.guild_id == 0 || (g=guild_search(sd->status.guild_id)) == NULL || // no guild
  1037. strcmp(sd->status.name, g->master) ) // not the guild master
  1038. return 0;
  1039. if( g->skill_point > 0 &&
  1040. g->skill[idx].id != 0 &&
  1041. g->skill[idx].lv < max )
  1042. intif_guild_skillup(g->guild_id, skill_num, sd->status.account_id, max);
  1043. return 0;
  1044. }
  1045. /*====================================================
  1046. * Notification of guildskill skill_num increase request
  1047. *---------------------------------------------------*/
  1048. int guild_skillupack(int guild_id,int skill_num,int account_id)
  1049. {
  1050. struct map_session_data *sd=map_id2sd(account_id);
  1051. struct guild *g=guild_search(guild_id);
  1052. int i;
  1053. if(g==NULL)
  1054. return 0;
  1055. if( sd != NULL ) {
  1056. clif_guild_skillup(sd,skill_num,g->skill[skill_num-GD_SKILLBASE].lv);
  1057. /* Guild Aura handling */
  1058. switch( skill_num ) {
  1059. case GD_LEADERSHIP:
  1060. case GD_GLORYWOUNDS:
  1061. case GD_SOULCOLD:
  1062. case GD_HAWKEYES:
  1063. guild_guildaura_refresh(sd,skill_num,g->skill[skill_num-GD_SKILLBASE].lv);
  1064. break;
  1065. }
  1066. }
  1067. // Inform all members
  1068. for(i=0;i<g->max_member;i++)
  1069. if((sd=g->member[i].sd)!=NULL)
  1070. clif_guild_skillinfo(sd);
  1071. return 0;
  1072. }
  1073. void guild_guildaura_refresh(struct map_session_data *sd, int skill_num, int skill_lv) {
  1074. struct skill_unit_group* group = NULL;
  1075. int type = status_skill2sc(skill_num);
  1076. if( !(battle_config.guild_aura&((agit_flag || agit2_flag)?2:1)) &&
  1077. !(battle_config.guild_aura&(map_flag_gvg2(sd->bl.m)?8:4)) )
  1078. return;
  1079. if( skill_lv <= 0 )
  1080. return;
  1081. if( sd->sc.data[type] && (group = skill_id2group(sd->sc.data[type]->val4)) ) {
  1082. skill_delunitgroup(group);
  1083. status_change_end(&sd->bl,type,INVALID_TIMER);
  1084. }
  1085. group = skill_unitsetting(&sd->bl,skill_num,skill_lv,sd->bl.x,sd->bl.y,0);
  1086. if( group ) {
  1087. sc_start4(&sd->bl,type,100,(battle_config.guild_aura&16)?0:skill_lv,0,0,group->group_id,600000);//duration doesn't matter these status never end with val4
  1088. }
  1089. return;
  1090. }
  1091. /*====================================================
  1092. * Count number of relations the guild has.
  1093. * Flag:
  1094. * 0 = allied
  1095. * 1 = enemy
  1096. *---------------------------------------------------*/
  1097. int guild_get_alliance_count(struct guild *g,int flag)
  1098. {
  1099. int i,c;
  1100. nullpo_ret(g);
  1101. for(i=c=0;i<MAX_GUILDALLIANCE;i++){
  1102. if( g->alliance[i].guild_id>0 &&
  1103. g->alliance[i].opposition==flag )
  1104. c++;
  1105. }
  1106. return c;
  1107. }
  1108. // Blocks all guild skills which have a common delay time.
  1109. void guild_block_skill(struct map_session_data *sd, int time)
  1110. {
  1111. int skill_num[] = { GD_BATTLEORDER, GD_REGENERATION, GD_RESTORE, GD_EMERGENCYCALL };
  1112. int i;
  1113. for (i = 0; i < 4; i++)
  1114. skill_blockpc_start_(sd, skill_num[i], time , true);
  1115. }
  1116. /*====================================================
  1117. * Check relation between guild_id1 and guild_id2.
  1118. * Flag:
  1119. * 0 = allied
  1120. * 1 = enemy
  1121. * Returns true if yes.
  1122. *---------------------------------------------------*/
  1123. int guild_check_alliance(int guild_id1, int guild_id2, int flag)
  1124. {
  1125. struct guild *g;
  1126. int i;
  1127. g = guild_search(guild_id1);
  1128. if (g == NULL)
  1129. return 0;
  1130. ARR_FIND( 0, MAX_GUILDALLIANCE, i, g->alliance[i].guild_id == guild_id2 && g->alliance[i].opposition == flag );
  1131. return( i < MAX_GUILDALLIANCE ) ? 1 : 0;
  1132. }
  1133. /*====================================================
  1134. * Player sd, asking player tsd an alliance between their 2 guilds
  1135. *---------------------------------------------------*/
  1136. int guild_reqalliance(struct map_session_data *sd,struct map_session_data *tsd)
  1137. {
  1138. struct guild *g[2];
  1139. int i;
  1140. if(agit_flag || agit2_flag) { // Disable alliance creation during woe [Valaris]
  1141. clif_displaymessage(sd->fd,msg_txt(676)); //"Alliances cannot be made during Guild Wars!"
  1142. return 0;
  1143. } // end addition [Valaris]
  1144. nullpo_ret(sd);
  1145. if(tsd==NULL || tsd->status.guild_id<=0)
  1146. return 0;
  1147. g[0]=guild_search(sd->status.guild_id);
  1148. g[1]=guild_search(tsd->status.guild_id);
  1149. if(g[0]==NULL || g[1]==NULL)
  1150. return 0;
  1151. // Prevent creation alliance with same guilds [LuzZza]
  1152. if(sd->status.guild_id == tsd->status.guild_id)
  1153. return 0;
  1154. if( guild_get_alliance_count(g[0],0) >= battle_config.max_guild_alliance ) {
  1155. clif_guild_allianceack(sd,4);
  1156. return 0;
  1157. }
  1158. if( guild_get_alliance_count(g[1],0) >= battle_config.max_guild_alliance ) {
  1159. clif_guild_allianceack(sd,3);
  1160. return 0;
  1161. }
  1162. if( tsd->guild_alliance>0 ){
  1163. clif_guild_allianceack(sd,1);
  1164. return 0;
  1165. }
  1166. for (i = 0; i < MAX_GUILDALLIANCE; i++) { // check if already allied
  1167. if( g[0]->alliance[i].guild_id==tsd->status.guild_id &&
  1168. g[0]->alliance[i].opposition==0){
  1169. clif_guild_allianceack(sd,0);
  1170. return 0;
  1171. }
  1172. }
  1173. tsd->guild_alliance=sd->status.guild_id;
  1174. tsd->guild_alliance_account=sd->status.account_id;
  1175. clif_guild_reqalliance(tsd,sd->status.account_id,g[0]->name);
  1176. return 0;
  1177. }
  1178. /*====================================================
  1179. * Player sd, answer to player tsd (account_id) for an alliance request
  1180. *---------------------------------------------------*/
  1181. int guild_reply_reqalliance(struct map_session_data *sd,int account_id,int flag)
  1182. {
  1183. struct map_session_data *tsd;
  1184. nullpo_ret(sd);
  1185. tsd= map_id2sd( account_id );
  1186. if (!tsd) { //Character left? Cancel alliance.
  1187. clif_guild_allianceack(sd,3);
  1188. return 0;
  1189. }
  1190. if (sd->guild_alliance != tsd->status.guild_id) // proposed guild_id alliance doesn't match tsd guildid
  1191. return 0;
  1192. if (flag == 1) { // consent
  1193. int i;
  1194. struct guild *g, *tg; // Reconfirm the number of alliance
  1195. g=guild_search(sd->status.guild_id);
  1196. tg=guild_search(tsd->status.guild_id);
  1197. if(g==NULL || guild_get_alliance_count(g,0) >= battle_config.max_guild_alliance){
  1198. clif_guild_allianceack(sd,4);
  1199. clif_guild_allianceack(tsd,3);
  1200. return 0;
  1201. }
  1202. if(tg==NULL || guild_get_alliance_count(tg,0) >= battle_config.max_guild_alliance){
  1203. clif_guild_allianceack(sd,3);
  1204. clif_guild_allianceack(tsd,4);
  1205. return 0;
  1206. }
  1207. for(i=0;i<MAX_GUILDALLIANCE;i++){
  1208. if(g->alliance[i].guild_id==tsd->status.guild_id &&
  1209. g->alliance[i].opposition==1)
  1210. intif_guild_alliance( sd->status.guild_id,tsd->status.guild_id,
  1211. sd->status.account_id,tsd->status.account_id,9 );
  1212. }
  1213. for(i=0;i<MAX_GUILDALLIANCE;i++){
  1214. if(tg->alliance[i].guild_id==sd->status.guild_id &&
  1215. tg->alliance[i].opposition==1)
  1216. intif_guild_alliance( tsd->status.guild_id,sd->status.guild_id,
  1217. tsd->status.account_id,sd->status.account_id,9 );
  1218. }
  1219. // inform other servers
  1220. intif_guild_alliance( sd->status.guild_id,tsd->status.guild_id,
  1221. sd->status.account_id,tsd->status.account_id,0 );
  1222. return 0;
  1223. } else { // deny
  1224. sd->guild_alliance=0;
  1225. sd->guild_alliance_account=0;
  1226. if(tsd!=NULL)
  1227. clif_guild_allianceack(tsd,3);
  1228. }
  1229. return 0;
  1230. }
  1231. /*====================================================
  1232. * Player sd asking to break alliance with guild guild_id
  1233. *---------------------------------------------------*/
  1234. int guild_delalliance(struct map_session_data *sd,int guild_id,int flag)
  1235. {
  1236. nullpo_ret(sd);
  1237. if(agit_flag || agit2_flag) { // Disable alliance breaking during woe [Valaris]
  1238. clif_displaymessage(sd->fd,msg_txt(677)); //"Alliances cannot be broken during Guild Wars!"
  1239. return 0;
  1240. } // end addition [Valaris]
  1241. intif_guild_alliance( sd->status.guild_id,guild_id,sd->status.account_id,0,flag|8 );
  1242. return 0;
  1243. }
  1244. /*====================================================
  1245. * Player sd, asking player tsd a formal enemy relation between their 2 guilds
  1246. *---------------------------------------------------*/
  1247. int guild_opposition(struct map_session_data *sd,struct map_session_data *tsd)
  1248. {
  1249. struct guild *g;
  1250. int i;
  1251. nullpo_ret(sd);
  1252. g=guild_search(sd->status.guild_id);
  1253. if(g==NULL || tsd==NULL)
  1254. return 0;
  1255. // Prevent creation opposition with same guilds [LuzZza]
  1256. if(sd->status.guild_id == tsd->status.guild_id)
  1257. return 0;
  1258. if( guild_get_alliance_count(g,1) >= battle_config.max_guild_alliance ) {
  1259. clif_guild_oppositionack(sd,1);
  1260. return 0;
  1261. }
  1262. for (i = 0; i < MAX_GUILDALLIANCE; i++) { // checking relations
  1263. if(g->alliance[i].guild_id==tsd->status.guild_id){
  1264. if (g->alliance[i].opposition == 1) { // check if not already hostile
  1265. clif_guild_oppositionack(sd,2);
  1266. return 0;
  1267. }
  1268. if(agit_flag || agit2_flag) // Prevent the changing of alliances to oppositions during WoE.
  1269. return 0;
  1270. //Change alliance to opposition.
  1271. intif_guild_alliance( sd->status.guild_id,tsd->status.guild_id,
  1272. sd->status.account_id,tsd->status.account_id,8 );
  1273. }
  1274. }
  1275. // inform other serv
  1276. intif_guild_alliance( sd->status.guild_id,tsd->status.guild_id,
  1277. sd->status.account_id,tsd->status.account_id,1 );
  1278. return 0;
  1279. }
  1280. /*====================================================
  1281. * Notification of a relationship between 2 guilds
  1282. *---------------------------------------------------*/
  1283. int guild_allianceack(int guild_id1,int guild_id2,int account_id1,int account_id2,int flag,const char *name1,const char *name2)
  1284. {
  1285. struct guild *g[2];
  1286. int guild_id[2];
  1287. const char *guild_name[2];
  1288. struct map_session_data *sd[2];
  1289. int j,i;
  1290. guild_id[0] = guild_id1;
  1291. guild_id[1] = guild_id2;
  1292. guild_name[0] = name1;
  1293. guild_name[1] = name2;
  1294. sd[0] = map_id2sd(account_id1);
  1295. sd[1] = map_id2sd(account_id2);
  1296. g[0]=guild_search(guild_id1);
  1297. g[1]=guild_search(guild_id2);
  1298. if(sd[0]!=NULL && (flag&0x0f)==0){
  1299. sd[0]->guild_alliance=0;
  1300. sd[0]->guild_alliance_account=0;
  1301. }
  1302. if (flag & 0x70) { // failure
  1303. for(i=0;i<2-(flag&1);i++)
  1304. if( sd[i]!=NULL )
  1305. clif_guild_allianceack(sd[i],((flag>>4)==i+1)?3:4);
  1306. return 0;
  1307. }
  1308. if (!(flag & 0x08)) { // new relationship
  1309. for(i=0;i<2-(flag&1);i++)
  1310. {
  1311. if(g[i]!=NULL)
  1312. {
  1313. ARR_FIND( 0, MAX_GUILDALLIANCE, j, g[i]->alliance[j].guild_id == 0 );
  1314. if( j < MAX_GUILDALLIANCE )
  1315. {
  1316. g[i]->alliance[j].guild_id=guild_id[1-i];
  1317. memcpy(g[i]->alliance[j].name,guild_name[1-i],NAME_LENGTH);
  1318. g[i]->alliance[j].opposition=flag&1;
  1319. }
  1320. }
  1321. }
  1322. } else { // remove relationship
  1323. for(i=0;i<2-(flag&1);i++)
  1324. {
  1325. if(g[i]!=NULL)
  1326. {
  1327. ARR_FIND( 0, MAX_GUILDALLIANCE, j, g[i]->alliance[j].guild_id == guild_id[1-i] && g[i]->alliance[j].opposition == (flag&1) );
  1328. if( j < MAX_GUILDALLIANCE )
  1329. g[i]->alliance[j].guild_id = 0;
  1330. }
  1331. if (sd[i] != NULL) // notify players
  1332. clif_guild_delalliance(sd[i],guild_id[1-i],(flag&1));
  1333. }
  1334. }
  1335. if ((flag & 0x0f) == 0) { // alliance notification
  1336. if( sd[1]!=NULL )
  1337. clif_guild_allianceack(sd[1],2);
  1338. } else if ((flag & 0x0f) == 1) { // enemy notification
  1339. if( sd[0]!=NULL )
  1340. clif_guild_oppositionack(sd[0],0);
  1341. }
  1342. for (i = 0; i < 2 - (flag & 1); i++) { // Retransmission of the relationship list to all members
  1343. struct map_session_data *sd;
  1344. if(g[i]!=NULL)
  1345. for(j=0;j<g[i]->max_member;j++)
  1346. if((sd=g[i]->member[j].sd)!=NULL)
  1347. clif_guild_allianceinfo(sd);
  1348. }
  1349. return 0;
  1350. }
  1351. /**
  1352. * Notification for the guild disbanded
  1353. * @see DBApply
  1354. */
  1355. int guild_broken_sub(DBKey key, DBData *data, va_list ap)
  1356. {
  1357. struct guild *g = db_data2ptr(data);
  1358. int guild_id=va_arg(ap,int);
  1359. int i,j;
  1360. struct map_session_data *sd=NULL;
  1361. nullpo_ret(g);
  1362. for(i=0;i<MAX_GUILDALLIANCE;i++){ // Destroy all relationships
  1363. if(g->alliance[i].guild_id==guild_id){
  1364. for(j=0;j<g->max_member;j++)
  1365. if( (sd=g->member[j].sd)!=NULL )
  1366. clif_guild_delalliance(sd,guild_id,g->alliance[i].opposition);
  1367. intif_guild_alliance(g->guild_id, guild_id,0,0,g->alliance[i].opposition|8);
  1368. g->alliance[i].guild_id=0;
  1369. }
  1370. }
  1371. return 0;
  1372. }
  1373. /**
  1374. * Invoked on Castles when a guild is broken. [Skotlex]
  1375. * @see DBApply
  1376. */
  1377. int castle_guild_broken_sub(DBKey key, DBData *data, va_list ap)
  1378. {
  1379. char name[EVENT_NAME_LENGTH];
  1380. struct guild_castle *gc = db_data2ptr(data);
  1381. int guild_id = va_arg(ap, int);
  1382. nullpo_ret(gc);
  1383. if (gc->guild_id == guild_id) {
  1384. // We call castle_event::OnGuildBreak of all castles of the guild
  1385. // You can set all castle_events in the 'db/castle_db.txt'
  1386. safestrncpy(name, gc->castle_event, sizeof(name));
  1387. npc_event_do(strcat(name, "::OnGuildBreak"));
  1388. //Save the new 'owner', this should invoke guardian clean up and other such things.
  1389. guild_castledatasave(gc->castle_id, 1, 0);
  1390. }
  1391. return 0;
  1392. }
  1393. //Invoked on /breakguild "Guild name"
  1394. int guild_broken(int guild_id,int flag)
  1395. {
  1396. struct guild *g = guild_search(guild_id);
  1397. struct map_session_data *sd = NULL;
  1398. int i;
  1399. if(flag!=0 || g==NULL)
  1400. return 0;
  1401. for(i=0;i<g->max_member;i++){ // Destroy all relationships
  1402. if((sd=g->member[i].sd)!=NULL){
  1403. if(sd->state.storage_flag == 2)
  1404. storage_guild_storage_quit(sd,1);
  1405. sd->status.guild_id=0;
  1406. clif_guild_broken(g->member[i].sd,0);
  1407. clif_charnameupdate(sd); // [LuzZza]
  1408. }
  1409. }
  1410. guild_db->foreach(guild_db,guild_broken_sub,guild_id);
  1411. castle_db->foreach(castle_db,castle_guild_broken_sub,guild_id);
  1412. guild_storage_delete(guild_id);
  1413. idb_remove(guild_db,guild_id);
  1414. return 0;
  1415. }
  1416. //Changes the Guild Master to the specified player. [Skotlex]
  1417. int guild_gm_change(int guild_id, struct map_session_data *sd)
  1418. {
  1419. struct guild *g;
  1420. nullpo_ret(sd);
  1421. if (sd->status.guild_id != guild_id)
  1422. return 0;
  1423. g=guild_search(guild_id);
  1424. nullpo_ret(g);
  1425. if (strcmp(g->master, sd->status.name) == 0) //Nothing to change.
  1426. return 0;
  1427. //Notify servers that master has changed.
  1428. intif_guild_change_gm(guild_id, sd->status.name, strlen(sd->status.name)+1);
  1429. return 1;
  1430. }
  1431. //Notification from Char server that a guild's master has changed. [Skotlex]
  1432. int guild_gm_changed(int guild_id, int account_id, int char_id)
  1433. {
  1434. struct guild *g;
  1435. struct guild_member gm;
  1436. int pos, i;
  1437. g=guild_search(guild_id);
  1438. if (!g)
  1439. return 0;
  1440. for(pos=0; pos<g->max_member && !(
  1441. g->member[pos].account_id==account_id &&
  1442. g->member[pos].char_id==char_id);
  1443. pos++);
  1444. if (pos == 0 || pos == g->max_member) return 0;
  1445. memcpy(&gm, &g->member[pos], sizeof (struct guild_member));
  1446. memcpy(&g->member[pos], &g->member[0], sizeof(struct guild_member));
  1447. memcpy(&g->member[0], &gm, sizeof(struct guild_member));
  1448. g->member[pos].position = g->member[0].position;
  1449. g->member[0].position = 0; //Position 0: guild Master.
  1450. strcpy(g->master, g->member[0].name);
  1451. if (g->member[pos].sd && g->member[pos].sd->fd)
  1452. {
  1453. clif_displaymessage(g->member[pos].sd->fd, msg_txt(678)); //"You no longer are the Guild Master."
  1454. g->member[pos].sd->state.gmaster_flag = 0;
  1455. }
  1456. if (g->member[0].sd && g->member[0].sd->fd)
  1457. {
  1458. clif_displaymessage(g->member[0].sd->fd, msg_txt(679)); //"You have become the Guild Master!"
  1459. g->member[0].sd->state.gmaster_flag = g;
  1460. //Block his skills for 5 minutes to prevent abuse.
  1461. guild_block_skill(g->member[0].sd, 300000);
  1462. }
  1463. // announce the change to all guild members
  1464. for( i = 0; i < g->max_member; i++ )
  1465. {
  1466. if( g->member[i].sd && g->member[i].sd->fd )
  1467. {
  1468. clif_guild_basicinfo(g->member[i].sd);
  1469. clif_guild_memberlist(g->member[i].sd);
  1470. }
  1471. }
  1472. return 1;
  1473. }
  1474. /*====================================================
  1475. * Guild disbanded
  1476. *---------------------------------------------------*/
  1477. int guild_break(struct map_session_data *sd,char *name)
  1478. {
  1479. struct guild *g;
  1480. int i;
  1481. nullpo_ret(sd);
  1482. if( (g=guild_search(sd->status.guild_id))==NULL )
  1483. return 0;
  1484. if(strcmp(g->name,name)!=0)
  1485. return 0;
  1486. if(!sd->state.gmaster_flag)
  1487. return 0;
  1488. for(i=0;i<g->max_member;i++){
  1489. if( g->member[i].account_id>0 && (
  1490. g->member[i].account_id!=sd->status.account_id ||
  1491. g->member[i].char_id!=sd->status.char_id ))
  1492. break;
  1493. }
  1494. if(i<g->max_member){
  1495. clif_guild_broken(sd,2);
  1496. return 0;
  1497. }
  1498. intif_guild_break(g->guild_id);
  1499. return 0;
  1500. }
  1501. /**
  1502. * Creates a list of guild castle IDs to be requested
  1503. * from char-server.
  1504. */
  1505. void guild_castle_map_init(void)
  1506. {
  1507. DBIterator* iter = NULL;
  1508. int num = db_size(castle_db);
  1509. if (num > 0) {
  1510. struct guild_castle* gc = NULL;
  1511. int *castle_ids, *cursor;
  1512. CREATE(castle_ids, int, num);
  1513. cursor = castle_ids;
  1514. iter = db_iterator(castle_db);
  1515. for (gc = dbi_first(iter); dbi_exists(iter); gc = dbi_next(iter)) {
  1516. *(cursor++) = gc->castle_id;
  1517. }
  1518. dbi_destroy(iter);
  1519. if (intif_guild_castle_dataload(num, castle_ids))
  1520. ShowStatus("Requested '"CL_WHITE"%d"CL_RESET"' guild castles from char-server...\n", num);
  1521. aFree(castle_ids);
  1522. }
  1523. }
  1524. /**
  1525. * Setter function for members of guild_castle struct.
  1526. * Handles all side-effects, like updating guardians.
  1527. * Sends updated info to char-server for saving.
  1528. * @param castle_id Castle ID
  1529. * @param index Type of data to change
  1530. * @param value New value
  1531. */
  1532. int guild_castledatasave(int castle_id, int index, int value)
  1533. {
  1534. struct guild_castle *gc = guild_castle_search(castle_id);
  1535. if (gc == NULL) {
  1536. ShowWarning("guild_castledatasave: guild castle '%d' not found\n", castle_id);
  1537. return 0;
  1538. }
  1539. switch (index) {
  1540. case 1: // The castle's owner has changed? Update or remove Guardians too. [Skotlex]
  1541. {
  1542. int i;
  1543. struct mob_data *gd;
  1544. gc->guild_id = value;
  1545. for (i = 0; i < MAX_GUARDIANS; i++)
  1546. if (gc->guardian[i].visible && (gd = map_id2md(gc->guardian[i].id)) != NULL)
  1547. mob_guardian_guildchange(gd);
  1548. break;
  1549. }
  1550. case 2:
  1551. gc->economy = value; break;
  1552. case 3: // defense invest change -> recalculate guardian hp
  1553. {
  1554. int i;
  1555. struct mob_data *gd;
  1556. gc->defense = value;
  1557. for (i = 0; i < MAX_GUARDIANS; i++)
  1558. if (gc->guardian[i].visible && (gd = map_id2md(gc->guardian[i].id)) != NULL)
  1559. status_calc_mob(gd, 0);
  1560. break;
  1561. }
  1562. case 4:
  1563. gc->triggerE = value; break;
  1564. case 5:
  1565. gc->triggerD = value; break;
  1566. case 6:
  1567. gc->nextTime = value; break;
  1568. case 7:
  1569. gc->payTime = value; break;
  1570. case 8:
  1571. gc->createTime = value; break;
  1572. case 9:
  1573. gc->visibleC = value; break;
  1574. default:
  1575. if (index > 9 && index <= 9+MAX_GUARDIANS) {
  1576. gc->guardian[index-10].visible = value;
  1577. break;
  1578. }
  1579. ShowWarning("guild_castledatasave: index = '%d' is out of allowed range\n", index);
  1580. return 0;
  1581. }
  1582. if (!intif_guild_castle_datasave(castle_id, index, value)) {
  1583. guild_castle_reconnect(castle_id, index, value);
  1584. }
  1585. return 0;
  1586. }
  1587. void guild_castle_reconnect_sub(void *key, void *data, va_list ap)
  1588. {
  1589. int castle_id = GetWord((int)__64BPRTSIZE(key), 0);
  1590. int index = GetWord((int)__64BPRTSIZE(key), 1);
  1591. intif_guild_castle_datasave(castle_id, index, *(int *)data);
  1592. aFree(data);
  1593. }
  1594. /**
  1595. * Saves pending guild castle data changes when char-server is
  1596. * disconnected.
  1597. * On reconnect pushes all changes to char-server for saving.
  1598. */
  1599. void guild_castle_reconnect(int castle_id, int index, int value)
  1600. {
  1601. static struct linkdb_node *gc_save_pending = NULL;
  1602. if (castle_id < 0) { // char-server reconnected
  1603. linkdb_foreach(&gc_save_pending, guild_castle_reconnect_sub);
  1604. linkdb_final(&gc_save_pending);
  1605. } else {
  1606. int *data;
  1607. CREATE(data, int, 1);
  1608. *data = value;
  1609. linkdb_replace(&gc_save_pending, (void*)__64BPRTSIZE((MakeDWord(castle_id, index))), data);
  1610. }
  1611. }
  1612. // Load castle data then invoke OnAgitInit* on last
  1613. int guild_castledataloadack(int len, struct guild_castle *gc)
  1614. {
  1615. int i;
  1616. int n = (len-4) / sizeof(struct guild_castle);
  1617. int ev;
  1618. nullpo_ret(gc);
  1619. //Last owned castle in the list invokes ::OnAgitInit
  1620. for( i = n-1; i >= 0 && !(gc[i].guild_id); --i );
  1621. ev = i; // offset of castle or -1
  1622. if( ev < 0 ) { //No castles owned, invoke OnAgitInit as it is.
  1623. npc_event_doall("OnAgitInit");
  1624. npc_event_doall("OnAgitInit2");
  1625. }
  1626. else // load received castles into memory, one by one
  1627. for( i = 0; i < n; i++, gc++ )
  1628. {
  1629. struct guild_castle *c = guild_castle_search(gc->castle_id);
  1630. if (!c) {
  1631. ShowError("guild_castledataloadack: castle id=%d not found.\n", gc->castle_id);
  1632. continue;
  1633. }
  1634. // update map-server castle data with new info
  1635. memcpy(&c->guild_id, &gc->guild_id, sizeof(struct guild_castle) - offsetof(struct guild_castle, guild_id));
  1636. if( c->guild_id )
  1637. {
  1638. if( i != ev )
  1639. guild_request_info(c->guild_id);
  1640. else { // last owned one
  1641. guild_npc_request_info(c->guild_id, "::OnAgitInit");
  1642. guild_npc_request_info(c->guild_id, "::OnAgitInit2");
  1643. }
  1644. }
  1645. }
  1646. ShowStatus("Received '"CL_WHITE"%d"CL_RESET"' guild castles from char-server.\n", n);
  1647. return 0;
  1648. }
  1649. /*====================================================
  1650. * Start normal woe and triggers all npc OnAgitStart
  1651. *---------------------------------------------------*/
  1652. int guild_agit_start(void)
  1653. { // Run All NPC_Event[OnAgitStart]
  1654. int c = npc_event_doall("OnAgitStart");
  1655. ShowStatus("NPC_Event:[OnAgitStart] Run (%d) Events by @AgitStart.\n",c);
  1656. return 0;
  1657. }
  1658. /*====================================================
  1659. * End normal woe and triggers all npc OnAgitEnd
  1660. *---------------------------------------------------*/
  1661. int guild_agit_end(void)
  1662. { // Run All NPC_Event[OnAgitEnd]
  1663. int c = npc_event_doall("OnAgitEnd");
  1664. ShowStatus("NPC_Event:[OnAgitEnd] Run (%d) Events by @AgitEnd.\n",c);
  1665. return 0;
  1666. }
  1667. /*====================================================
  1668. * Start woe2 and triggers all npc OnAgitStart2
  1669. *---------------------------------------------------*/
  1670. int guild_agit2_start(void)
  1671. { // Run All NPC_Event[OnAgitStart2]
  1672. int c = npc_event_doall("OnAgitStart2");
  1673. ShowStatus("NPC_Event:[OnAgitStart2] Run (%d) Events by @AgitStart2.\n",c);
  1674. return 0;
  1675. }
  1676. /*====================================================
  1677. * End woe2 and triggers all npc OnAgitEnd2
  1678. *---------------------------------------------------*/
  1679. int guild_agit2_end(void)
  1680. { // Run All NPC_Event[OnAgitEnd2]
  1681. int c = npc_event_doall("OnAgitEnd2");
  1682. ShowStatus("NPC_Event:[OnAgitEnd2] Run (%d) Events by @AgitEnd2.\n",c);
  1683. return 0;
  1684. }
  1685. // How many castles does this guild have?
  1686. int guild_checkcastles(struct guild *g)
  1687. {
  1688. int nb_cas = 0;
  1689. struct guild_castle* gc = NULL;
  1690. DBIterator *iter = db_iterator(castle_db);
  1691. for (gc = dbi_first(iter); dbi_exists(iter); gc = dbi_next(iter)) {
  1692. if (gc->guild_id == g->guild_id) {
  1693. nb_cas++;
  1694. }
  1695. }
  1696. dbi_destroy(iter);
  1697. return nb_cas;
  1698. }
  1699. // Are these two guilds allied?
  1700. bool guild_isallied(int guild_id, int guild_id2)
  1701. {
  1702. int i;
  1703. struct guild* g = guild_search(guild_id);
  1704. nullpo_ret(g);
  1705. ARR_FIND( 0, MAX_GUILDALLIANCE, i, g->alliance[i].guild_id == guild_id2 );
  1706. return( i < MAX_GUILDALLIANCE && g->alliance[i].opposition == 0 );
  1707. }
  1708. /**
  1709. * @see DBApply
  1710. */
  1711. static int eventlist_db_final(DBKey key, DBData *data, va_list ap)
  1712. {
  1713. struct eventlist *next = NULL;
  1714. struct eventlist *current = db_data2ptr(data);
  1715. while (current != NULL) {
  1716. next = current->next;
  1717. aFree(current);
  1718. current = next;
  1719. }
  1720. return 0;
  1721. }
  1722. /**
  1723. * @see DBApply
  1724. */
  1725. static int guild_expcache_db_final(DBKey key, DBData *data, va_list ap)
  1726. {
  1727. ers_free(expcache_ers, db_data2ptr(data));
  1728. return 0;
  1729. }
  1730. /**
  1731. * @see DBApply
  1732. */
  1733. static int guild_castle_db_final(DBKey key, DBData *data, va_list ap)
  1734. {
  1735. struct guild_castle* gc = db_data2ptr(data);
  1736. if( gc->temp_guardians )
  1737. aFree(gc->temp_guardians);
  1738. aFree(gc);
  1739. return 0;
  1740. }
  1741. void do_init_guild(void)
  1742. {
  1743. guild_db=idb_alloc(DB_OPT_RELEASE_DATA);
  1744. castle_db=idb_alloc(DB_OPT_BASE);
  1745. guild_expcache_db=idb_alloc(DB_OPT_BASE);
  1746. guild_infoevent_db=idb_alloc(DB_OPT_BASE);
  1747. expcache_ers = ers_new(sizeof(struct guild_expcache),"guild.c::expcache_ers",ERS_OPT_NONE);
  1748. sv_readdb(db_path, "castle_db.txt", ',', 4, 5, -1, &guild_read_castledb);
  1749. memset(guild_skill_tree,0,sizeof(guild_skill_tree));
  1750. 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]
  1751. add_timer_func_list(guild_payexp_timer,"guild_payexp_timer");
  1752. add_timer_func_list(guild_send_xy_timer, "guild_send_xy_timer");
  1753. add_timer_interval(gettick()+GUILD_PAYEXP_INVERVAL,guild_payexp_timer,0,0,GUILD_PAYEXP_INVERVAL);
  1754. add_timer_interval(gettick()+GUILD_SEND_XY_INVERVAL,guild_send_xy_timer,0,0,GUILD_SEND_XY_INVERVAL);
  1755. }
  1756. void do_final_guild(void)
  1757. {
  1758. db_destroy(guild_db);
  1759. castle_db->destroy(castle_db,guild_castle_db_final);
  1760. guild_expcache_db->destroy(guild_expcache_db,guild_expcache_db_final);
  1761. guild_infoevent_db->destroy(guild_infoevent_db,eventlist_db_final);
  1762. ers_destroy(expcache_ers);
  1763. }