guild.c 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227
  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 "intif.h"
  19. #include "channel.h"
  20. #include "log.h"
  21. #include "trade.h"
  22. #include <stdlib.h>
  23. static DBMap* guild_db; // int guild_id -> struct guild*
  24. static DBMap* castle_db; // int castle_id -> struct guild_castle*
  25. static DBMap* guild_expcache_db; // uint32 char_id -> struct guild_expcache*
  26. static DBMap* guild_infoevent_db; // int guild_id -> struct eventlist*
  27. struct eventlist {
  28. char name[EVENT_NAME_LENGTH];
  29. struct eventlist *next;
  30. };
  31. //Constant related to the flash of the Guild EXP cache
  32. #define GUILD_SEND_XY_INTERVAL 5000 // Interval of sending coordinates and HP
  33. #define GUILD_PAYEXP_INTERVAL 10000 //Interval (maximum survival time of the cache, in milliseconds)
  34. #define GUILD_PAYEXP_LIST 8192 //The maximum number of cache
  35. //Guild EXP cache
  36. struct guild_expcache {
  37. int guild_id, account_id, char_id;
  38. uint64 exp;
  39. };
  40. static struct eri *expcache_ers; //For handling of guild exp payment.
  41. #define MAX_GUILD_SKILL_REQUIRE 5
  42. struct{
  43. int id;
  44. int max;
  45. struct{
  46. short id;
  47. short lv;
  48. }need[MAX_GUILD_SKILL_REQUIRE];
  49. } guild_skill_tree[MAX_GUILDSKILL];
  50. int guild_payexp_timer(int tid, unsigned int tick, int id, intptr_t data);
  51. static int guild_send_xy_timer(int tid, unsigned int tick, int id, intptr_t data);
  52. /* guild flags cache */
  53. struct npc_data **guild_flags;
  54. unsigned short guild_flags_count;
  55. /**
  56. * Get guild skill index in guild_skill_tree
  57. * @param skill_id
  58. * @return Index in skill_tree or -1
  59. **/
  60. static short guild_skill_get_index(uint16 skill_id) {
  61. if (!SKILL_CHK_GUILD(skill_id))
  62. return -1;
  63. skill_id -= GD_SKILLBASE;
  64. if (skill_id >= MAX_GUILDSKILL)
  65. return -1;
  66. return skill_id;
  67. }
  68. /*==========================================
  69. * Retrieves and validates the sd pointer for this guild member [Skotlex]
  70. *------------------------------------------*/
  71. static TBL_PC* guild_sd_check(int guild_id, uint32 account_id, uint32 char_id) {
  72. TBL_PC* sd = map_id2sd(account_id);
  73. if (!(sd && sd->status.char_id == char_id))
  74. return NULL;
  75. if (sd->status.guild_id != guild_id)
  76. { //If player belongs to a different guild, kick him out.
  77. intif_guild_leave(guild_id,account_id,char_id,0,"** Guild Mismatch **");
  78. return NULL;
  79. }
  80. return sd;
  81. }
  82. // Modified [Komurka]
  83. int guild_skill_get_max (int id) {
  84. if ((id = guild_skill_get_index(id)) < 0)
  85. return 0;
  86. return guild_skill_tree[id].max;
  87. }
  88. // Retrive skill_lv learned by guild
  89. int guild_checkskill(struct guild *g, int id) {
  90. if ((id = guild_skill_get_index(id)) < 0)
  91. return 0;
  92. return g->skill[id].lv;
  93. }
  94. /*==========================================
  95. * guild_skill_tree.txt reading - from jA [Komurka]
  96. *------------------------------------------*/
  97. static bool guild_read_guildskill_tree_db(char* split[], int columns, int current) {// <skill id>,<max lv>,<req id1>,<req lv1>,<req id2>,<req lv2>,<req id3>,<req lv3>,<req id4>,<req lv4>,<req id5>,<req lv5>
  98. int k, skill_id = atoi(split[0]);
  99. short idx = -1;
  100. if ((idx = guild_skill_get_index(skill_id)) < 0) {
  101. ShowError("guild_read_guildskill_tree_db: Invalid Guild skill '%s'.\n", split[1]);
  102. return false;
  103. }
  104. guild_skill_tree[idx].id = skill_id;
  105. guild_skill_tree[idx].max = atoi(split[1]);
  106. if( guild_skill_tree[idx].id == GD_GLORYGUILD && battle_config.require_glory_guild && guild_skill_tree[idx].max == 0 ) {// enable guild's glory when required for emblems
  107. guild_skill_tree[idx].max = 1;
  108. }
  109. for( k = 0; k < MAX_GUILD_SKILL_REQUIRE; k++ ) {
  110. guild_skill_tree[idx].need[k].id = atoi(split[k*2+2]);
  111. guild_skill_tree[idx].need[k].lv = atoi(split[k*2+3]);
  112. }
  113. return true;
  114. }
  115. /*==========================================
  116. * Guild skill check - from jA [Komurka]
  117. *------------------------------------------*/
  118. int guild_check_skill_require(struct guild *g,int id) {
  119. uint8 i;
  120. short idx = -1;
  121. if(g == NULL)
  122. return 0;
  123. if ((idx = guild_skill_get_index(id)) < 0)
  124. return 0;
  125. for(i=0;i<MAX_GUILD_SKILL_REQUIRE;i++)
  126. {
  127. if(guild_skill_tree[idx].need[i].id == 0) break;
  128. if(guild_skill_tree[idx].need[i].lv > guild_checkskill(g,guild_skill_tree[idx].need[i].id))
  129. return 0;
  130. }
  131. return 1;
  132. }
  133. static bool guild_read_castledb(char* str[], int columns, int current) {// <castle id>,<map name>,<castle name>,<castle event>[,<reserved/unused switch flag>]
  134. struct guild_castle *gc;
  135. int mapindex = mapindex_name2id(str[1]);
  136. if (map_mapindex2mapid(mapindex) < 0) // Map not found or on another map-server
  137. return false;
  138. CREATE(gc, struct guild_castle, 1);
  139. gc->castle_id = atoi(str[0]);
  140. gc->mapindex = mapindex;
  141. safestrncpy(gc->castle_name, str[2], sizeof(gc->castle_name));
  142. safestrncpy(gc->castle_event, str[3], sizeof(gc->castle_event));
  143. idb_put(castle_db,gc->castle_id,gc);
  144. return true;
  145. }
  146. /// lookup: guild id -> guild*
  147. struct guild* guild_search(int guild_id) {
  148. return (struct guild*)idb_get(guild_db,guild_id);
  149. }
  150. /// lookup: guild name -> guild*
  151. struct guild* guild_searchname(char* str) {
  152. struct guild* g;
  153. DBIterator *iter = db_iterator(guild_db);
  154. for( g = dbi_first(iter); dbi_exists(iter); g = dbi_next(iter) ) {
  155. if( strcmpi(g->name, str) == 0 )
  156. break;
  157. }
  158. dbi_destroy(iter);
  159. return g;
  160. }
  161. /// lookup: castle id -> castle*
  162. struct guild_castle* guild_castle_search(int gcid) {
  163. return (struct guild_castle*)idb_get(castle_db,gcid);
  164. }
  165. /// lookup: map index -> castle*
  166. struct guild_castle* guild_mapindex2gc(short mapindex) {
  167. struct guild_castle* gc;
  168. DBIterator *iter = db_iterator(castle_db);
  169. for( gc = dbi_first(iter); dbi_exists(iter); gc = dbi_next(iter) ) {
  170. if( gc->mapindex == mapindex )
  171. break;
  172. }
  173. dbi_destroy(iter);
  174. return gc;
  175. }
  176. /// lookup: map name -> castle*
  177. struct guild_castle* guild_mapname2gc(const char* mapname) {
  178. return guild_mapindex2gc(mapindex_name2id(mapname));
  179. }
  180. struct map_session_data* guild_getavailablesd(struct guild* g) {
  181. int i;
  182. nullpo_retr(NULL, g);
  183. ARR_FIND( 0, g->max_member, i, g->member[i].sd != NULL );
  184. return( i < g->max_member ) ? g->member[i].sd : NULL;
  185. }
  186. /// lookup: player AID/CID -> member index
  187. int guild_getindex(struct guild *g,uint32 account_id,uint32 char_id) {
  188. int i;
  189. if( g == NULL )
  190. return -1;
  191. ARR_FIND( 0, g->max_member, i, g->member[i].account_id == account_id && g->member[i].char_id == char_id );
  192. return( i < g->max_member ) ? i : -1;
  193. }
  194. /// lookup: player sd -> member position
  195. int guild_getposition(struct guild* g, struct map_session_data* sd) {
  196. int i;
  197. if( g == NULL && (g=sd->guild) == NULL )
  198. return -1;
  199. 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 );
  200. return( i < g->max_member ) ? g->member[i].position : -1;
  201. }
  202. //Creation of member information
  203. void guild_makemember(struct guild_member *m,struct map_session_data *sd) {
  204. nullpo_retv(sd);
  205. memset(m,0,sizeof(struct guild_member));
  206. m->account_id =sd->status.account_id;
  207. m->char_id =sd->status.char_id;
  208. m->hair =sd->status.hair;
  209. m->hair_color =sd->status.hair_color;
  210. m->gender =sd->status.sex;
  211. m->class_ =sd->status.class_;
  212. m->lv =sd->status.base_level;
  213. // m->exp =0;
  214. // m->exp_payper =0;
  215. m->online =1;
  216. m->position =MAX_GUILDPOSITION-1;
  217. memcpy(m->name,sd->status.name,NAME_LENGTH);
  218. return;
  219. }
  220. /**
  221. * Server cache to be flushed to inter the Guild EXP
  222. * @see DBApply
  223. */
  224. int guild_payexp_timer_sub(DBKey key, DBData *data, va_list ap) {
  225. int i;
  226. struct guild_expcache *c;
  227. struct guild *g;
  228. c = db_data2ptr(data);
  229. if (
  230. (g = guild_search(c->guild_id)) == NULL ||
  231. (i = guild_getindex(g, c->account_id, c->char_id)) < 0
  232. ) {
  233. ers_free(expcache_ers, c);
  234. return 0;
  235. }
  236. if (g->member[i].exp > UINT64_MAX - c->exp)
  237. g->member[i].exp = UINT64_MAX;
  238. else
  239. g->member[i].exp+= c->exp;
  240. intif_guild_change_memberinfo(g->guild_id,c->account_id,c->char_id,
  241. GMI_EXP,&g->member[i].exp,sizeof(g->member[i].exp));
  242. c->exp=0;
  243. ers_free(expcache_ers, c);
  244. return 0;
  245. }
  246. int guild_payexp_timer(int tid, unsigned int tick, int id, intptr_t data) {
  247. guild_expcache_db->clear(guild_expcache_db,guild_payexp_timer_sub);
  248. return 0;
  249. }
  250. /**
  251. * Taken from party_send_xy_timer_sub. [Skotlex]
  252. * @see DBApply
  253. */
  254. int guild_send_xy_timer_sub(DBKey key, DBData *data, va_list ap) {
  255. struct guild *g = db_data2ptr(data);
  256. int i;
  257. nullpo_ret(g);
  258. if( !g->connect_member ) {
  259. // no members connected to this guild so do not iterate
  260. return 0;
  261. }
  262. for(i=0;i<g->max_member;i++){
  263. struct map_session_data* sd = g->member[i].sd;
  264. if( sd != NULL && sd->fd && (sd->guild_x != sd->bl.x || sd->guild_y != sd->bl.y) && !sd->bg_id ) {
  265. clif_guild_xy(sd);
  266. sd->guild_x = sd->bl.x;
  267. sd->guild_y = sd->bl.y;
  268. }
  269. }
  270. return 0;
  271. }
  272. //Code from party_send_xy_timer [Skotlex]
  273. static int guild_send_xy_timer(int tid, unsigned int tick, int id, intptr_t data) {
  274. guild_db->foreach(guild_db,guild_send_xy_timer_sub,tick);
  275. return 0;
  276. }
  277. int guild_send_dot_remove(struct map_session_data *sd) {
  278. if (sd->status.guild_id)
  279. clif_guild_xy_remove(sd);
  280. return 0;
  281. }
  282. //------------------------------------------------------------------------
  283. int guild_create(struct map_session_data *sd, const char *name) {
  284. char tname[NAME_LENGTH];
  285. struct guild_member m;
  286. nullpo_ret(sd);
  287. safestrncpy(tname, name, NAME_LENGTH);
  288. trim(tname);
  289. if( !tname[0] )
  290. return 0; // empty name
  291. if( sd->status.guild_id ) {
  292. // already in a guild
  293. clif_guild_created(sd,1);
  294. return 0;
  295. }
  296. if( battle_config.guild_emperium_check && pc_search_inventory(sd,ITEMID_EMPERIUM) == -1 ) {
  297. // item required
  298. clif_guild_created(sd,3);
  299. return 0;
  300. }
  301. guild_makemember(&m,sd);
  302. m.position=0;
  303. intif_guild_create(name,&m);
  304. return 1;
  305. }
  306. //Whether or not to create guild
  307. int guild_created(uint32 account_id,int guild_id) {
  308. struct map_session_data *sd=map_id2sd(account_id);
  309. if(sd==NULL)
  310. return 0;
  311. if(!guild_id) {
  312. clif_guild_created(sd, 2); // Creation failure (presence of the same name Guild)
  313. return 0;
  314. }
  315. sd->status.guild_id = guild_id;
  316. clif_guild_created(sd,0);
  317. if(battle_config.guild_emperium_check)
  318. pc_delitem(sd,pc_search_inventory(sd,ITEMID_EMPERIUM),1,0,0,LOG_TYPE_CONSUME); //emperium consumption
  319. return 0;
  320. }
  321. //Information request
  322. int guild_request_info(int guild_id) {
  323. return intif_guild_request_info(guild_id);
  324. }
  325. //Information request with event
  326. int guild_npc_request_info(int guild_id,const char *event) {
  327. if( guild_search(guild_id) ) {
  328. if( event && *event )
  329. npc_event_do(event);
  330. return 0;
  331. }
  332. if( event && *event ) {
  333. struct eventlist *ev;
  334. DBData prev;
  335. ev=(struct eventlist *)aCalloc(sizeof(struct eventlist),1);
  336. memcpy(ev->name,event,strlen(event));
  337. //The one in the db (if present) becomes the next event from this.
  338. if (guild_infoevent_db->put(guild_infoevent_db, db_i2key(guild_id), db_ptr2data(ev), &prev))
  339. ev->next = db_data2ptr(&prev);
  340. }
  341. return guild_request_info(guild_id);
  342. }
  343. /**
  344. * Close trade window if party member is kicked when trade a party bound item
  345. * @param sd
  346. **/
  347. static void guild_trade_bound_cancel(struct map_session_data *sd) {
  348. #ifdef BOUND_ITEMS
  349. nullpo_retv(sd);
  350. if (sd->state.isBoundTrading&(1<<BOUND_GUILD))
  351. trade_tradecancel(sd);
  352. #else
  353. ;
  354. #endif
  355. }
  356. //Confirmation of the character belongs to guild
  357. int guild_check_member(struct guild *g) {
  358. int i;
  359. struct map_session_data *sd;
  360. struct s_mapiterator* iter;
  361. nullpo_ret(g);
  362. iter = mapit_getallusers();
  363. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) ) {
  364. if( sd->status.guild_id != g->guild_id )
  365. continue;
  366. i = guild_getindex(g,sd->status.account_id,sd->status.char_id);
  367. if (i < 0) {
  368. sd->guild = NULL;
  369. sd->status.guild_id=0;
  370. sd->guild_emblem_id=0;
  371. ShowWarning("guild: check_member %d[%s] is not member\n",sd->status.account_id,sd->status.name);
  372. }
  373. }
  374. mapit_free(iter);
  375. return 0;
  376. }
  377. //Delete association with guild_id for all characters
  378. int guild_recv_noinfo(int guild_id) {
  379. struct map_session_data *sd;
  380. struct s_mapiterator* iter;
  381. iter = mapit_getallusers();
  382. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) ) {
  383. if( sd->status.guild_id == guild_id )
  384. sd->status.guild_id = 0; // erase guild
  385. }
  386. mapit_free(iter);
  387. return 0;
  388. }
  389. //Get and display information for all member
  390. int guild_recv_info(struct guild *sg) {
  391. struct guild *g,before;
  392. int i,bm,m;
  393. DBData data;
  394. struct map_session_data *sd;
  395. bool guild_new = false;
  396. nullpo_ret(sg);
  397. if((g = guild_search(sg->guild_id))==NULL) {
  398. guild_new = true;
  399. g=(struct guild *)aCalloc(1,sizeof(struct guild));
  400. idb_put(guild_db,sg->guild_id,g);
  401. before=*sg;
  402. //Perform the check on the user because the first load
  403. guild_check_member(sg);
  404. if ((sd = map_nick2sd(sg->master)) != NULL) {
  405. //If the guild master is online the first time the guild_info is received,
  406. //that means he was the first to join, so apply guild skill blocking here.
  407. if( battle_config.guild_skill_relog_delay )
  408. guild_block_skill(sd, battle_config.guild_skill_relog_delay);
  409. //Also set the guild master flag.
  410. sd->guild = g;
  411. sd->state.gmaster_flag = 1;
  412. clif_charnameupdate(sd); // [LuzZza]
  413. clif_guild_masterormember(sd);
  414. }
  415. } else {
  416. before=*g;
  417. }
  418. memcpy(g,sg,sizeof(struct guild));
  419. if(g->max_member > MAX_GUILD) {
  420. 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);
  421. g->max_member = MAX_GUILD;
  422. }
  423. for(i=bm=m=0;i<g->max_member;i++){
  424. if(g->member[i].account_id>0){
  425. sd = g->member[i].sd = guild_sd_check(g->guild_id, g->member[i].account_id, g->member[i].char_id);
  426. if (sd) clif_charnameupdate(sd); // [LuzZza]
  427. m++;
  428. }else
  429. g->member[i].sd=NULL;
  430. if(before.member[i].account_id>0)
  431. bm++;
  432. }
  433. for (i = 0; i < g->max_member; i++) { //Transmission of information at all members
  434. sd = g->member[i].sd;
  435. if( sd==NULL )
  436. continue;
  437. sd->guild = g;
  438. if(channel_config.ally_autojoin ) {
  439. channel_gjoin(sd,3); //make all member join guildchan+allieschan
  440. }
  441. if (before.guild_lv != g->guild_lv || bm != m ||
  442. before.max_member != g->max_member) {
  443. clif_guild_basicinfo(sd); //Submit basic information
  444. clif_guild_emblem(sd, g); //Submit emblem
  445. }
  446. if (bm != m) { //Send members information
  447. clif_guild_memberlist(g->member[i].sd);
  448. }
  449. if (before.skill_point != g->skill_point)
  450. clif_guild_skillinfo(sd); //Submit information skills
  451. if (guild_new) { // Send information and affiliation if unsent
  452. clif_guild_belonginfo(sd, g);
  453. clif_guild_notice(sd, g);
  454. sd->guild_emblem_id = g->emblem_id;
  455. }
  456. }
  457. //Occurrence of an event
  458. if (guild_infoevent_db->remove(guild_infoevent_db, db_i2key(sg->guild_id), &data)) {
  459. struct eventlist *ev = db_data2ptr(&data), *ev2;
  460. while(ev) {
  461. npc_event_do(ev->name);
  462. ev2=ev->next;
  463. aFree(ev);
  464. ev=ev2;
  465. }
  466. }
  467. return 0;
  468. }
  469. /*=============================================
  470. * Player sd send a guild invatation to player tsd to join his guild
  471. *--------------------------------------------*/
  472. int guild_invite(struct map_session_data *sd, struct map_session_data *tsd) {
  473. struct guild *g;
  474. int i;
  475. nullpo_ret(sd);
  476. g=sd->guild;
  477. if(tsd==NULL || g==NULL)
  478. return 0;
  479. if( (i=guild_getposition(g,sd))<0 || !(g->position[i].mode&0x0001) )
  480. return 0; //Invite permission.
  481. if(!battle_config.invite_request_check) {
  482. if (tsd->party_invite > 0 || tsd->trade_partner || tsd->adopt_invite) { //checking if there no other invitation pending
  483. clif_guild_inviteack(sd,0);
  484. return 0;
  485. }
  486. }
  487. if (!tsd->fd) { //You can't invite someone who has already disconnected.
  488. clif_guild_inviteack(sd,1);
  489. return 0;
  490. }
  491. if(tsd->status.guild_id>0 ||
  492. tsd->guild_invite>0 ||
  493. ((agit_flag || agit2_flag) && map[tsd->bl.m].flag.gvg_castle))
  494. { //Can't invite people inside castles. [Skotlex]
  495. clif_guild_inviteack(sd,0);
  496. return 0;
  497. }
  498. //search an empty spot in guild
  499. ARR_FIND( 0, g->max_member, i, g->member[i].account_id == 0 );
  500. if(i==g->max_member){
  501. clif_guild_inviteack(sd,3);
  502. return 0;
  503. }
  504. tsd->guild_invite=sd->status.guild_id;
  505. tsd->guild_invite_account=sd->status.account_id;
  506. clif_guild_invite(tsd,g);
  507. return 0;
  508. }
  509. /// Guild invitation reply.
  510. /// flag: 0:rejected, 1:accepted
  511. int guild_reply_invite(struct map_session_data* sd, int guild_id, int flag) {
  512. struct map_session_data* tsd;
  513. nullpo_ret(sd);
  514. // subsequent requests may override the value
  515. if( sd->guild_invite != guild_id )
  516. return 0; // mismatch
  517. // look up the person who sent the invite
  518. //NOTE: this can be NULL because the person might have logged off in the meantime
  519. tsd = map_id2sd(sd->guild_invite_account);
  520. if ( sd->status.guild_id > 0 ) {
  521. // [Paradox924X]
  522. // Already in another guild.
  523. if ( tsd ) clif_guild_inviteack(tsd,0);
  524. return 0;
  525. } else if( flag == 0 ) {// rejected
  526. sd->guild_invite = 0;
  527. sd->guild_invite_account = 0;
  528. if( tsd ) clif_guild_inviteack(tsd,1);
  529. } else {// accepted
  530. struct guild_member m;
  531. struct guild* g;
  532. int i;
  533. if( (g=guild_search(guild_id)) == NULL ) {
  534. sd->guild_invite = 0;
  535. sd->guild_invite_account = 0;
  536. return 0;
  537. }
  538. ARR_FIND( 0, g->max_member, i, g->member[i].account_id == 0 );
  539. if( i == g->max_member ) {
  540. sd->guild_invite = 0;
  541. sd->guild_invite_account = 0;
  542. if( tsd ) clif_guild_inviteack(tsd,3);
  543. return 0;
  544. }
  545. guild_makemember(&m,sd);
  546. intif_guild_addmember(guild_id, &m);
  547. //TODO: send a minimap update to this player
  548. }
  549. return 0;
  550. }
  551. //Invoked when a player joins.
  552. //- If guild is not in memory, it is requested
  553. //- Otherwise sd pointer is set up.
  554. //- Player must be authed and must belong to a guild before invoking this method
  555. void guild_member_joined(struct map_session_data *sd) {
  556. struct guild* g;
  557. int i;
  558. g=guild_search(sd->status.guild_id);
  559. if (!g) {
  560. guild_request_info(sd->status.guild_id);
  561. return;
  562. }
  563. if (strcmp(sd->status.name,g->master) == 0) { // set the Guild Master flag
  564. sd->state.gmaster_flag = 1;
  565. // prevent Guild Skills from being used directly after relog
  566. if( battle_config.guild_skill_relog_delay )
  567. guild_block_skill(sd, battle_config.guild_skill_relog_delay);
  568. }
  569. i = guild_getindex(g, sd->status.account_id, sd->status.char_id);
  570. if (i == -1)
  571. sd->status.guild_id = 0;
  572. else {
  573. g->member[i].sd = sd;
  574. sd->guild = g;
  575. if( channel_config.ally_enable && channel_config.ally_autojoin ) {
  576. channel_gjoin(sd,3);
  577. }
  578. }
  579. }
  580. /*==========================================
  581. * Add a player to a given guild_id
  582. *----------------------------------------*/
  583. int guild_member_added(int guild_id,uint32 account_id,uint32 char_id,int flag) {
  584. struct map_session_data *sd= map_id2sd(account_id),*sd2;
  585. struct guild *g;
  586. if( (g=guild_search(guild_id))==NULL )
  587. return 0;
  588. if(sd==NULL || sd->guild_invite==0){
  589. // cancel if player not present or invalide guild_id invitation
  590. if (flag == 0) {
  591. ShowError("guild: member added error %d is not online\n",account_id);
  592. intif_guild_leave(guild_id,account_id,char_id,0,"** Data Error **");
  593. }
  594. return 0;
  595. }
  596. sd2 = map_id2sd(sd->guild_invite_account);
  597. sd->guild_invite = 0;
  598. sd->guild_invite_account = 0;
  599. if (flag == 1) { //failure
  600. if( sd2!=NULL )
  601. clif_guild_inviteack(sd2,3);
  602. return 0;
  603. }
  604. //if all ok add player to guild
  605. sd->status.guild_id = g->guild_id;
  606. sd->guild_emblem_id = g->emblem_id;
  607. sd->guild = g;
  608. //Packets which were sent in the previous 'guild_sent' implementation.
  609. clif_guild_belonginfo(sd,g);
  610. clif_guild_notice(sd,g);
  611. //TODO: send new emblem info to others
  612. if( sd2!=NULL )
  613. clif_guild_inviteack(sd2,2);
  614. //Next line commented because it do nothing, look at guild_recv_info [LuzZza]
  615. //clif_charnameupdate(sd); //Update display name [Skotlex]
  616. return 0;
  617. }
  618. /*==========================================
  619. * Player request leaving a given guild_id
  620. *----------------------------------------*/
  621. int guild_leave(struct map_session_data* sd, int guild_id, uint32 account_id, uint32 char_id, const char* mes) {
  622. struct guild *g;
  623. nullpo_ret(sd);
  624. g = sd->guild;
  625. if(g==NULL)
  626. return 0;
  627. if(sd->status.account_id!=account_id ||
  628. sd->status.char_id!=char_id || sd->status.guild_id!=guild_id ||
  629. ((agit_flag || agit2_flag) && map[sd->bl.m].flag.gvg_castle))
  630. return 0;
  631. guild_trade_bound_cancel(sd);
  632. intif_guild_leave(sd->status.guild_id, sd->status.account_id, sd->status.char_id,0,mes);
  633. return 0;
  634. }
  635. /*==========================================
  636. * Request remove a player to a given guild_id
  637. *----------------------------------------*/
  638. int guild_expulsion(struct map_session_data* sd, int guild_id, uint32 account_id, uint32 char_id, const char* mes) {
  639. struct map_session_data *tsd;
  640. struct guild *g;
  641. int i,ps;
  642. nullpo_ret(sd);
  643. g = sd->guild;
  644. if(g==NULL)
  645. return 0;
  646. if(sd->status.guild_id!=guild_id)
  647. return 0;
  648. if( (ps=guild_getposition(g,sd))<0 || !(g->position[ps].mode&0x0010) )
  649. return 0; //Expulsion permission
  650. //Can't leave inside guild castles.
  651. if ((tsd = map_id2sd(account_id)) &&
  652. tsd->status.char_id == char_id &&
  653. ((agit_flag || agit2_flag) && map[tsd->bl.m].flag.gvg_castle))
  654. return 0;
  655. // find the member and perform expulsion
  656. i = guild_getindex(g, account_id, char_id);
  657. if( i != -1 && strcmp(g->member[i].name,g->master) != 0 ) { //Can't expel the GL!
  658. if (tsd)
  659. guild_trade_bound_cancel(tsd);
  660. intif_guild_leave(g->guild_id,account_id,char_id,1,mes);
  661. }
  662. return 0;
  663. }
  664. /**
  665. * A confirmation from inter-serv that player is kicked successfully
  666. * @param guild_Id
  667. * @param account_id
  668. * @param char_id
  669. * @param flag
  670. * @param name
  671. * @param mes
  672. */
  673. int guild_member_withdraw(int guild_id, uint32 account_id, uint32 char_id, int flag, const char* name, const char* mes) {
  674. int i;
  675. struct guild* g = guild_search(guild_id);
  676. struct map_session_data* sd = map_charid2sd(char_id);
  677. struct map_session_data* online_member_sd;
  678. if(g == NULL)
  679. return 0; // no such guild (error!)
  680. i = guild_getindex(g, account_id, char_id);
  681. if( i == -1 )
  682. return 0; // not a member (inconsistency!)
  683. #ifdef BOUND_ITEMS
  684. //Guild bound item check
  685. guild_retrieveitembound(char_id,account_id,guild_id);
  686. #endif
  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. // do stuff that needs the guild_id first, BEFORE we wipe it
  700. if (sd->state.storage_flag == 2) //Close the guild storage.
  701. gstorage_storageclose(sd);
  702. guild_send_dot_remove(sd);
  703. channel_pcquit(sd,3); //leave guild and ally chan
  704. sd->status.guild_id = 0;
  705. sd->guild = NULL;
  706. sd->guild_emblem_id = 0;
  707. clif_charnameupdate(sd); //Update display name [Skotlex]
  708. status_change_end(&sd->bl,SC_LEADERSHIP,INVALID_TIMER);
  709. status_change_end(&sd->bl,SC_GLORYWOUNDS,INVALID_TIMER);
  710. status_change_end(&sd->bl,SC_SOULCOLD,INVALID_TIMER);
  711. status_change_end(&sd->bl,SC_HAWKEYES,INVALID_TIMER);
  712. //@TODO: Send emblem update to self and people around
  713. }
  714. return 0;
  715. }
  716. #ifdef BOUND_ITEMS
  717. /**
  718. * Retrieve guild bound items from kicked member
  719. * @param char_id
  720. * @param account_id
  721. * @param guild_id
  722. */
  723. void guild_retrieveitembound(uint32 char_id, uint32 account_id, int guild_id) {
  724. TBL_PC *sd = map_charid2sd(char_id);
  725. if (sd) { //Character is online
  726. int idxlist[MAX_INVENTORY];
  727. int j;
  728. j = pc_bound_chk(sd,BOUND_GUILD,idxlist);
  729. if (j) {
  730. struct guild_storage* stor = gstorage_guild2storage(sd->status.guild_id);
  731. int i;
  732. // Close the storage first if someone open it
  733. if (stor && stor->opened) {
  734. struct map_session_data *tsd = map_charid2sd(stor->opened);
  735. if (tsd)
  736. gstorage_storageclose(tsd);
  737. }
  738. for (i = 0; i < j; i++) { //Loop the matching items, gstorage_additem takes care of opening storage
  739. if (stor)
  740. gstorage_additem(sd,stor,&sd->status.inventory[idxlist[i]],sd->status.inventory[idxlist[i]].amount);
  741. pc_delitem(sd,idxlist[i],sd->status.inventory[idxlist[i]].amount,0,4,LOG_TYPE_GSTORAGE);
  742. }
  743. gstorage_storageclose(sd); //Close and save the storage
  744. }
  745. } else { //Character is offline, ask char server to do the job
  746. struct guild_storage* stor = gstorage_get_storage(guild_id);
  747. struct guild *g = guild_search(guild_id);
  748. nullpo_retv(g);
  749. if(stor && stor->opened) { //Someone is in guild storage, close them
  750. struct map_session_data *tsd = map_charid2sd(stor->opened);
  751. if (tsd)
  752. gstorage_storageclose(tsd);
  753. }
  754. intif_itembound_guild_retrieve(char_id,account_id,guild_id);
  755. }
  756. }
  757. #endif
  758. int guild_send_memberinfoshort(struct map_session_data *sd,int online) { // cleaned up [LuzZza]
  759. struct guild *g;
  760. nullpo_ret(sd);
  761. if(sd->status.guild_id <= 0)
  762. return 0;
  763. if(!(g = sd->guild))
  764. return 0;
  765. intif_guild_memberinfoshort(g->guild_id,
  766. sd->status.account_id,sd->status.char_id,online,sd->status.base_level,sd->status.class_);
  767. if(!online){
  768. int i=guild_getindex(g,sd->status.account_id,sd->status.char_id);
  769. if(i>=0)
  770. g->member[i].sd=NULL;
  771. else
  772. 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);
  773. return 0;
  774. }
  775. if(sd->state.connect_new) { //Note that this works because it is invoked in parse_LoadEndAck before connect_new is cleared.
  776. clif_guild_belonginfo(sd,g);
  777. sd->guild_emblem_id = g->emblem_id;
  778. }
  779. return 0;
  780. }
  781. int guild_recv_memberinfoshort(int guild_id,uint32 account_id,uint32 char_id,int online,int lv,int class_) { // cleaned up [LuzZza]
  782. int i,alv,c,idx=-1,om=0,oldonline=-1;
  783. struct guild *g = guild_search(guild_id);
  784. if(g == NULL)
  785. return 0;
  786. for(i=0,alv=0,c=0,om=0;i<g->max_member;i++){
  787. struct guild_member *m=&g->member[i];
  788. if(!m->account_id) continue;
  789. if(m->account_id==account_id && m->char_id==char_id ){
  790. oldonline=m->online;
  791. m->online=online;
  792. m->lv=lv;
  793. m->class_=class_;
  794. idx=i;
  795. }
  796. alv+=m->lv;
  797. c++;
  798. if(m->online)
  799. om++;
  800. }
  801. if(idx == -1 || c == 0) {
  802. //Treat char_id who doesn't match guild_id (not found as member)
  803. struct map_session_data *sd = map_id2sd(account_id);
  804. if(sd && sd->status.char_id == char_id) {
  805. sd->status.guild_id=0;
  806. sd->guild_emblem_id=0;
  807. }
  808. ShowWarning("guild: not found member %d,%d on %d[%s]\n", account_id,char_id,guild_id,g->name);
  809. return 0;
  810. }
  811. g->average_lv=alv/c;
  812. g->connect_member=om;
  813. //Ensure validity of pointer (ie: player logs in/out, changes map-server)
  814. g->member[idx].sd = guild_sd_check(guild_id, account_id, char_id);
  815. if(oldonline!=online)
  816. clif_guild_memberlogin_notice(g, idx, online);
  817. if(!g->member[idx].sd)
  818. return 0;
  819. //Send XY dot updates. [Skotlex]
  820. //Moved from guild_send_memberinfoshort [LuzZza]
  821. for(i=0; i < g->max_member; i++) {
  822. if(!g->member[i].sd || i == idx ||
  823. g->member[i].sd->bl.m != g->member[idx].sd->bl.m)
  824. continue;
  825. clif_guild_xy_single(g->member[idx].sd->fd, g->member[i].sd);
  826. clif_guild_xy_single(g->member[i].sd->fd, g->member[idx].sd);
  827. }
  828. return 0;
  829. }
  830. /*====================================================
  831. * Send a message to whole guild
  832. *---------------------------------------------------*/
  833. int guild_send_message(struct map_session_data *sd,const char *mes,int len) {
  834. nullpo_ret(sd);
  835. if(sd->status.guild_id==0)
  836. return 0;
  837. intif_guild_message(sd->status.guild_id,sd->status.account_id,mes,len);
  838. guild_recv_message(sd->status.guild_id,sd->status.account_id,mes,len);
  839. // Chat logging type 'G' / Guild Chat
  840. 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);
  841. return 0;
  842. }
  843. /*====================================================
  844. * Guild receive a message, will be displayed to whole member
  845. *---------------------------------------------------*/
  846. int guild_recv_message(int guild_id,uint32 account_id,const char *mes,int len) {
  847. struct guild *g;
  848. if( (g=guild_search(guild_id))==NULL)
  849. return 0;
  850. clif_guild_message(g,account_id,mes,len);
  851. return 0;
  852. }
  853. /*====================================================
  854. * Member changing position in guild
  855. *---------------------------------------------------*/
  856. int guild_change_memberposition(int guild_id,uint32 account_id,uint32 char_id,short idx) {
  857. return intif_guild_change_memberinfo(guild_id,account_id,char_id,GMI_POSITION,&idx,sizeof(idx));
  858. }
  859. /*====================================================
  860. * Notification of new position for member
  861. *---------------------------------------------------*/
  862. int guild_memberposition_changed(struct guild *g,int idx,int pos) {
  863. nullpo_ret(g);
  864. g->member[idx].position=pos;
  865. clif_guild_memberpositionchanged(g,idx);
  866. // Update char position in client [LuzZza]
  867. if(g->member[idx].sd != NULL)
  868. clif_charnameupdate(g->member[idx].sd);
  869. return 0;
  870. }
  871. /*====================================================
  872. * Change guild title or member
  873. *---------------------------------------------------*/
  874. int guild_change_position(int guild_id,int idx,
  875. int mode,int exp_mode,const char *name) {
  876. struct guild_position p;
  877. exp_mode = cap_value(exp_mode, 0, battle_config.guild_exp_limit);
  878. //Mode 0x01 <- Invite
  879. //Mode 0x10 <- Expel.
  880. p.mode=mode&0x11;
  881. p.exp_mode=exp_mode;
  882. safestrncpy(p.name,name,NAME_LENGTH);
  883. return intif_guild_position(guild_id,idx,&p);
  884. }
  885. /*====================================================
  886. * Notification of member has changed his guild title
  887. *---------------------------------------------------*/
  888. int guild_position_changed(int guild_id,int idx,struct guild_position *p) {
  889. struct guild *g=guild_search(guild_id);
  890. int i;
  891. if(g==NULL)
  892. return 0;
  893. memcpy(&g->position[idx],p,sizeof(struct guild_position));
  894. clif_guild_positionchanged(g,idx);
  895. // Update char name in client [LuzZza]
  896. for(i=0;i<g->max_member;i++)
  897. if(g->member[i].position == idx && g->member[i].sd != NULL)
  898. clif_charnameupdate(g->member[i].sd);
  899. return 0;
  900. }
  901. /*====================================================
  902. * Change guild notice
  903. *---------------------------------------------------*/
  904. int guild_change_notice(struct map_session_data *sd,int guild_id,const char *mes1,const char *mes2) {
  905. nullpo_ret(sd);
  906. if(guild_id!=sd->status.guild_id)
  907. return 0;
  908. return intif_guild_notice(guild_id,mes1,mes2);
  909. }
  910. /*====================================================
  911. * Notification of guild has changed his notice
  912. *---------------------------------------------------*/
  913. int guild_notice_changed(int guild_id,const char *mes1,const char *mes2) {
  914. int i;
  915. struct guild *g=guild_search(guild_id);
  916. if(g==NULL)
  917. return 0;
  918. memcpy(g->mes1,mes1,MAX_GUILDMES1);
  919. memcpy(g->mes2,mes2,MAX_GUILDMES2);
  920. for(i=0;i<g->max_member;i++){
  921. struct map_session_data *sd = g->member[i].sd;
  922. if(sd != NULL)
  923. clif_guild_notice(sd,g);
  924. }
  925. return 0;
  926. }
  927. /*====================================================
  928. * Change guild emblem
  929. *---------------------------------------------------*/
  930. int guild_change_emblem(struct map_session_data *sd,int len,const char *data) {
  931. struct guild *g;
  932. nullpo_ret(sd);
  933. if (battle_config.require_glory_guild &&
  934. !((g = sd->guild) && guild_checkskill(g, GD_GLORYGUILD)>0)) {
  935. clif_skill_fail(sd,GD_GLORYGUILD,USESKILL_FAIL_LEVEL,0);
  936. return 0;
  937. }
  938. return intif_guild_emblem(sd->status.guild_id,len,data);
  939. }
  940. /*====================================================
  941. * Notification of guild emblem changed
  942. *---------------------------------------------------*/
  943. int guild_emblem_changed(int len,int guild_id,int emblem_id,const char *data) {
  944. int i;
  945. struct map_session_data *sd;
  946. struct guild *g=guild_search(guild_id);
  947. if(g==NULL)
  948. return 0;
  949. memcpy(g->emblem_data,data,len);
  950. g->emblem_len=len;
  951. g->emblem_id=emblem_id;
  952. for(i=0;i<g->max_member;i++){
  953. if((sd=g->member[i].sd)!=NULL){
  954. sd->guild_emblem_id=emblem_id;
  955. clif_guild_belonginfo(sd,g);
  956. clif_guild_emblem(sd,g);
  957. clif_guild_emblem_area(&sd->bl);
  958. }
  959. }
  960. {// update guardians (mobs)
  961. DBIterator* iter = db_iterator(castle_db);
  962. struct guild_castle* gc;
  963. for( gc = (struct guild_castle*)dbi_first(iter) ; dbi_exists(iter); gc = (struct guild_castle*)dbi_next(iter) )
  964. {
  965. if( gc->guild_id != guild_id )
  966. continue;
  967. // update permanent guardians
  968. for( i = 0; i < ARRAYLENGTH(gc->guardian); ++i )
  969. {
  970. TBL_MOB* md = (gc->guardian[i].id ? map_id2md(gc->guardian[i].id) : NULL);
  971. if( md == NULL || md->guardian_data == NULL )
  972. continue;
  973. md->guardian_data->emblem_id = emblem_id;
  974. clif_guild_emblem_area(&md->bl);
  975. }
  976. // update temporary guardians
  977. for( i = 0; i < gc->temp_guardians_max; ++i )
  978. {
  979. TBL_MOB* md = (gc->temp_guardians[i] ? map_id2md(gc->temp_guardians[i]) : NULL);
  980. if( md == NULL || md->guardian_data == NULL )
  981. continue;
  982. md->guardian_data->emblem_id = emblem_id;
  983. clif_guild_emblem_area(&md->bl);
  984. }
  985. }
  986. dbi_destroy(iter);
  987. }
  988. {// update npcs (flags or other npcs that used flagemblem to attach to this guild)
  989. for( i = 0; i < guild_flags_count; i++ ) {
  990. if( guild_flags[i] && guild_flags[i]->u.scr.guild_id == guild_id ) {
  991. clif_guild_emblem_area(&guild_flags[i]->bl);
  992. }
  993. }
  994. }
  995. return 0;
  996. }
  997. /**
  998. * @see DBCreateData
  999. */
  1000. static DBData create_expcache(DBKey key, va_list args) {
  1001. struct guild_expcache *c;
  1002. struct map_session_data *sd = va_arg(args, struct map_session_data*);
  1003. c = ers_alloc(expcache_ers, struct guild_expcache);
  1004. c->guild_id = sd->status.guild_id;
  1005. c->account_id = sd->status.account_id;
  1006. c->char_id = sd->status.char_id;
  1007. c->exp = 0;
  1008. return db_ptr2data(c);
  1009. }
  1010. /*====================================================
  1011. * Return taxed experience from player sd to guild
  1012. *---------------------------------------------------*/
  1013. unsigned int guild_payexp(struct map_session_data *sd,unsigned int exp) {
  1014. struct guild *g;
  1015. struct guild_expcache *c;
  1016. int per;
  1017. nullpo_ret(sd);
  1018. if (!exp) return 0;
  1019. if (sd->status.guild_id == 0 ||
  1020. (g = sd->guild) == NULL ||
  1021. (per = guild_getposition(g,sd)) < 0 ||
  1022. (per = g->position[per].exp_mode) < 1)
  1023. return 0;
  1024. if (per < 100)
  1025. exp = exp * per / 100;
  1026. //Otherwise tax everything.
  1027. c = db_data2ptr(guild_expcache_db->ensure(guild_expcache_db, db_i2key(sd->status.char_id), create_expcache, sd));
  1028. if (c->exp > UINT64_MAX - exp)
  1029. c->exp = UINT64_MAX;
  1030. else
  1031. c->exp += exp;
  1032. return exp;
  1033. }
  1034. /*====================================================
  1035. * Player sd pay a tribute experience to his guild
  1036. * Add this experience to guild exp
  1037. * [Celest]
  1038. *---------------------------------------------------*/
  1039. int guild_getexp(struct map_session_data *sd,int exp) {
  1040. struct guild_expcache *c;
  1041. nullpo_ret(sd);
  1042. if (sd->status.guild_id == 0 || sd->guild == NULL)
  1043. return 0;
  1044. c = db_data2ptr(guild_expcache_db->ensure(guild_expcache_db, db_i2key(sd->status.char_id), create_expcache, sd));
  1045. if (c->exp > UINT64_MAX - exp)
  1046. c->exp = UINT64_MAX;
  1047. else
  1048. c->exp += exp;
  1049. return exp;
  1050. }
  1051. /*====================================================
  1052. * Ask to increase guildskill skill_id
  1053. *---------------------------------------------------*/
  1054. void guild_skillup(TBL_PC* sd, uint16 skill_id) {
  1055. struct guild* g;
  1056. short idx = guild_skill_get_index(skill_id);
  1057. short max = 0;
  1058. nullpo_retv(sd);
  1059. if (idx == -1)
  1060. return;
  1061. if( sd->status.guild_id == 0 || (g=sd->guild) == NULL || // no guild
  1062. strcmp(sd->status.name, g->master) ) // not the guild master
  1063. return;
  1064. max = guild_skill_get_max(skill_id);
  1065. if( g->skill_point > 0 &&
  1066. g->skill[idx].id != 0 &&
  1067. g->skill[idx].lv < max )
  1068. intif_guild_skillup(g->guild_id, skill_id, sd->status.account_id, max);
  1069. }
  1070. /*====================================================
  1071. * Notification of guildskill skill_id increase request
  1072. *---------------------------------------------------*/
  1073. int guild_skillupack(int guild_id,uint16 skill_id,uint32 account_id) {
  1074. struct map_session_data *sd = map_id2sd(account_id);
  1075. struct guild *g = guild_search(guild_id);
  1076. int i;
  1077. short idx = guild_skill_get_index(skill_id);
  1078. if (g == NULL || idx == -1)
  1079. return 0;
  1080. if (sd != NULL) {
  1081. int lv = g->skill[idx].lv;
  1082. int range = skill_get_range(skill_id, lv);
  1083. clif_skillup(sd,skill_id,lv,range,1);
  1084. /* Guild Aura handling */
  1085. switch( skill_id ) {
  1086. case GD_LEADERSHIP:
  1087. case GD_GLORYWOUNDS:
  1088. case GD_SOULCOLD:
  1089. case GD_HAWKEYES:
  1090. guild_guildaura_refresh(sd,skill_id,g->skill[idx].lv);
  1091. break;
  1092. }
  1093. }
  1094. // Inform all members
  1095. for (i = 0; i < g->max_member; i++)
  1096. if ((sd = g->member[i].sd) != NULL)
  1097. clif_guild_skillinfo(sd);
  1098. return 0;
  1099. }
  1100. void guild_guildaura_refresh(struct map_session_data *sd, uint16 skill_id, uint16 skill_lv) {
  1101. struct skill_unit_group* group = NULL;
  1102. int type = status_skill2sc(skill_id);
  1103. if( !(battle_config.guild_aura&((agit_flag || agit2_flag)?2:1)) &&
  1104. !(battle_config.guild_aura&(map_flag_gvg2(sd->bl.m)?8:4)) )
  1105. return;
  1106. if( !skill_lv )
  1107. return;
  1108. if( sd->sc.data[type] && (group = skill_id2group(sd->sc.data[type]->val4)) ) {
  1109. skill_delunitgroup(group);
  1110. status_change_end(&sd->bl,type,INVALID_TIMER);
  1111. }
  1112. group = skill_unitsetting(&sd->bl,skill_id,skill_lv,sd->bl.x,sd->bl.y,0);
  1113. if( group )
  1114. sc_start4(NULL,&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
  1115. return;
  1116. }
  1117. /*====================================================
  1118. * Count number of relations the guild has.
  1119. * Flag:
  1120. * 0 = allied
  1121. * 1 = enemy
  1122. *---------------------------------------------------*/
  1123. int guild_get_alliance_count(struct guild *g,int flag) {
  1124. int i,c;
  1125. nullpo_ret(g);
  1126. for(i=c=0;i<MAX_GUILDALLIANCE;i++){
  1127. if( g->alliance[i].guild_id>0 &&
  1128. g->alliance[i].opposition==flag )
  1129. c++;
  1130. }
  1131. return c;
  1132. }
  1133. // Blocks all guild skills which have a common delay time.
  1134. void guild_block_skill(struct map_session_data *sd, int time) {
  1135. uint16 skill_id[] = { GD_BATTLEORDER, GD_REGENERATION, GD_RESTORE, GD_EMERGENCYCALL };
  1136. int i;
  1137. for (i = 0; i < 4; i++)
  1138. skill_blockpc_start(sd, skill_id[i], time);
  1139. }
  1140. /*====================================================
  1141. * Check relation between guild_id1 and guild_id2.
  1142. * Flag:
  1143. * 0 = allied
  1144. * 1 = enemy
  1145. * Returns true if yes.
  1146. *---------------------------------------------------*/
  1147. int guild_check_alliance(int guild_id1, int guild_id2, int flag) {
  1148. struct guild *g;
  1149. int i;
  1150. g = guild_search(guild_id1);
  1151. if (g == NULL)
  1152. return 0;
  1153. ARR_FIND( 0, MAX_GUILDALLIANCE, i, g->alliance[i].guild_id == guild_id2 && g->alliance[i].opposition == flag );
  1154. return( i < MAX_GUILDALLIANCE ) ? 1 : 0;
  1155. }
  1156. /*====================================================
  1157. * Player sd, asking player tsd an alliance between their 2 guilds
  1158. *---------------------------------------------------*/
  1159. int guild_reqalliance(struct map_session_data *sd,struct map_session_data *tsd) {
  1160. struct guild *g[2];
  1161. int i;
  1162. if(agit_flag || agit2_flag) { // Disable alliance creation during woe [Valaris]
  1163. clif_displaymessage(sd->fd,msg_txt(sd,676)); //"Alliances cannot be made during Guild Wars!"
  1164. return 0;
  1165. } // end addition [Valaris]
  1166. nullpo_ret(sd);
  1167. if(tsd==NULL || tsd->status.guild_id<=0)
  1168. return 0;
  1169. g[0]=sd->guild;
  1170. g[1]=tsd->guild;
  1171. if(g[0]==NULL || g[1]==NULL)
  1172. return 0;
  1173. // Prevent creation alliance with same guilds [LuzZza]
  1174. if(sd->status.guild_id == tsd->status.guild_id)
  1175. return 0;
  1176. if( guild_get_alliance_count(g[0],0) >= battle_config.max_guild_alliance ) {
  1177. clif_guild_allianceack(sd,4);
  1178. return 0;
  1179. }
  1180. if( guild_get_alliance_count(g[1],0) >= battle_config.max_guild_alliance ) {
  1181. clif_guild_allianceack(sd,3);
  1182. return 0;
  1183. }
  1184. if( tsd->guild_alliance>0 ){
  1185. clif_guild_allianceack(sd,1);
  1186. return 0;
  1187. }
  1188. for (i = 0; i < MAX_GUILDALLIANCE; i++) { // check if already allied
  1189. if( g[0]->alliance[i].guild_id==tsd->status.guild_id &&
  1190. g[0]->alliance[i].opposition==0){
  1191. clif_guild_allianceack(sd,0);
  1192. return 0;
  1193. }
  1194. }
  1195. tsd->guild_alliance=sd->status.guild_id;
  1196. tsd->guild_alliance_account=sd->status.account_id;
  1197. clif_guild_reqalliance(tsd,sd->status.account_id,g[0]->name);
  1198. return 0;
  1199. }
  1200. /*====================================================
  1201. * Player sd, answer to player tsd (account_id) for an alliance request
  1202. *---------------------------------------------------*/
  1203. int guild_reply_reqalliance(struct map_session_data *sd,uint32 account_id,int flag) {
  1204. struct map_session_data *tsd;
  1205. nullpo_ret(sd);
  1206. tsd= map_id2sd( account_id );
  1207. if (!tsd) { //Character left? Cancel alliance.
  1208. clif_guild_allianceack(sd,3);
  1209. return 0;
  1210. }
  1211. if (sd->guild_alliance != tsd->status.guild_id) // proposed guild_id alliance doesn't match tsd guildid
  1212. return 0;
  1213. if (flag == 1) { // consent
  1214. int i;
  1215. struct guild *g, *tg; // Reconfirm the number of alliance
  1216. g=sd->guild;
  1217. tg=tsd->guild;
  1218. if(g==NULL || guild_get_alliance_count(g,0) >= battle_config.max_guild_alliance){
  1219. clif_guild_allianceack(sd,4);
  1220. clif_guild_allianceack(tsd,3);
  1221. return 0;
  1222. }
  1223. if(tg==NULL || guild_get_alliance_count(tg,0) >= battle_config.max_guild_alliance){
  1224. clif_guild_allianceack(sd,3);
  1225. clif_guild_allianceack(tsd,4);
  1226. return 0;
  1227. }
  1228. for(i=0;i<MAX_GUILDALLIANCE;i++){
  1229. if(g->alliance[i].guild_id==tsd->status.guild_id &&
  1230. g->alliance[i].opposition==1)
  1231. intif_guild_alliance( sd->status.guild_id,tsd->status.guild_id,
  1232. sd->status.account_id,tsd->status.account_id,9 );
  1233. }
  1234. for(i=0;i<MAX_GUILDALLIANCE;i++){
  1235. if(tg->alliance[i].guild_id==sd->status.guild_id &&
  1236. tg->alliance[i].opposition==1)
  1237. intif_guild_alliance( tsd->status.guild_id,sd->status.guild_id,
  1238. tsd->status.account_id,sd->status.account_id,9 );
  1239. }
  1240. // inform other servers
  1241. intif_guild_alliance( sd->status.guild_id,tsd->status.guild_id,
  1242. sd->status.account_id,tsd->status.account_id,0 );
  1243. return 0;
  1244. } else { // deny
  1245. sd->guild_alliance=0;
  1246. sd->guild_alliance_account=0;
  1247. if(tsd!=NULL)
  1248. clif_guild_allianceack(tsd,3);
  1249. }
  1250. return 0;
  1251. }
  1252. /*====================================================
  1253. * Player sd asking to break alliance with guild guild_id
  1254. *---------------------------------------------------*/
  1255. int guild_delalliance(struct map_session_data *sd,int guild_id,int flag) {
  1256. nullpo_ret(sd);
  1257. if(agit_flag || agit2_flag) { // Disable alliance breaking during woe [Valaris]
  1258. clif_displaymessage(sd->fd,msg_txt(sd,677)); //"Alliances cannot be broken during Guild Wars!"
  1259. return 0;
  1260. } // end addition [Valaris]
  1261. intif_guild_alliance( sd->status.guild_id,guild_id,sd->status.account_id,0,flag|8 );
  1262. return 0;
  1263. }
  1264. /*====================================================
  1265. * Player sd, asking player tsd a formal enemy relation between their 2 guilds
  1266. *---------------------------------------------------*/
  1267. int guild_opposition(struct map_session_data *sd,struct map_session_data *tsd) {
  1268. struct guild *g;
  1269. int i;
  1270. nullpo_ret(sd);
  1271. g=sd->guild;
  1272. if(g==NULL || tsd==NULL)
  1273. return 0;
  1274. // Prevent creation opposition with same guilds [LuzZza]
  1275. if(sd->status.guild_id == tsd->status.guild_id)
  1276. return 0;
  1277. if( guild_get_alliance_count(g,1) >= battle_config.max_guild_alliance ) {
  1278. clif_guild_oppositionack(sd,1);
  1279. return 0;
  1280. }
  1281. for (i = 0; i < MAX_GUILDALLIANCE; i++) { // checking relations
  1282. if(g->alliance[i].guild_id==tsd->status.guild_id){
  1283. if (g->alliance[i].opposition == 1) { // check if not already hostile
  1284. clif_guild_oppositionack(sd,2);
  1285. return 0;
  1286. }
  1287. if(agit_flag || agit2_flag) // Prevent the changing of alliances to oppositions during WoE.
  1288. return 0;
  1289. //Change alliance to opposition.
  1290. intif_guild_alliance( sd->status.guild_id,tsd->status.guild_id,
  1291. sd->status.account_id,tsd->status.account_id,8 );
  1292. }
  1293. }
  1294. // inform other serv
  1295. intif_guild_alliance( sd->status.guild_id,tsd->status.guild_id,
  1296. sd->status.account_id,tsd->status.account_id,1 );
  1297. return 0;
  1298. }
  1299. /*====================================================
  1300. * Notification of a relationship between 2 guilds
  1301. *---------------------------------------------------*/
  1302. int guild_allianceack(int guild_id1,int guild_id2,uint32 account_id1,uint32 account_id2,int flag,const char *name1,const char *name2)
  1303. {
  1304. struct guild *g[2];
  1305. int guild_id[2];
  1306. const char *guild_name[2];
  1307. struct map_session_data *sd[2];
  1308. int j,i;
  1309. guild_id[0] = guild_id1;
  1310. guild_id[1] = guild_id2;
  1311. guild_name[0] = name1;
  1312. guild_name[1] = name2;
  1313. sd[0] = map_id2sd(account_id1);
  1314. sd[1] = map_id2sd(account_id2);
  1315. g[0]=guild_search(guild_id1);
  1316. g[1]=guild_search(guild_id2);
  1317. if(sd[0]!=NULL && (flag&0x0f)==0){
  1318. sd[0]->guild_alliance=0;
  1319. sd[0]->guild_alliance_account=0;
  1320. }
  1321. if (flag & 0x70) { // failure
  1322. for(i=0;i<2-(flag&1);i++)
  1323. if( sd[i]!=NULL )
  1324. clif_guild_allianceack(sd[i],((flag>>4)==i+1)?3:4);
  1325. return 0;
  1326. }
  1327. if (!(flag & 0x08)) { // new relationship
  1328. for(i=0;i<2-(flag&1);i++) {
  1329. if(g[i]!=NULL) {
  1330. ARR_FIND( 0, MAX_GUILDALLIANCE, j, g[i]->alliance[j].guild_id == 0 );
  1331. if( j < MAX_GUILDALLIANCE ) {
  1332. g[i]->alliance[j].guild_id=guild_id[1-i];
  1333. memcpy(g[i]->alliance[j].name,guild_name[1-i],NAME_LENGTH);
  1334. g[i]->alliance[j].opposition=flag&1;
  1335. }
  1336. }
  1337. }
  1338. } else { // remove relationship
  1339. for(i=0;i<2-(flag&1);i++) {
  1340. if(g[i]!=NULL) {
  1341. for(j=0;j<g[i]->max_member;j++) channel_pcquit(g[i]->member[j].sd,2); //leave all alliance chan
  1342. ARR_FIND( 0, MAX_GUILDALLIANCE, j, g[i]->alliance[j].guild_id == guild_id[1-i] && g[i]->alliance[j].opposition == (flag&1) );
  1343. if( j < MAX_GUILDALLIANCE )
  1344. g[i]->alliance[j].guild_id = 0;
  1345. }
  1346. if (sd[i] != NULL) // notify players
  1347. clif_guild_delalliance(sd[i],guild_id[1-i],(flag&1));
  1348. }
  1349. }
  1350. if ((flag & 0x0f) == 0) { // alliance notification
  1351. if( sd[1]!=NULL )
  1352. clif_guild_allianceack(sd[1],2);
  1353. } else if ((flag & 0x0f) == 1) { // enemy notification
  1354. if( sd[0]!=NULL )
  1355. clif_guild_oppositionack(sd[0],0);
  1356. }
  1357. for (i = 0; i < 2 - (flag & 1); i++) { // Retransmission of the relationship list to all members
  1358. if(g[i]!=NULL)
  1359. for(j=0;j<g[i]->max_member;j++) {
  1360. struct map_session_data *sd_mem = g[i]->member[j].sd;
  1361. if( sd_mem!=NULL){
  1362. clif_guild_allianceinfo(sd_mem);
  1363. channel_gjoin(sd_mem,2); //join ally join
  1364. }
  1365. }
  1366. }
  1367. return 0;
  1368. }
  1369. /**
  1370. * Notification for the guild disbanded
  1371. * @see DBApply
  1372. */
  1373. int guild_broken_sub(DBKey key, DBData *data, va_list ap) {
  1374. struct guild *g = db_data2ptr(data);
  1375. int guild_id=va_arg(ap,int);
  1376. int i,j;
  1377. struct map_session_data *sd=NULL;
  1378. nullpo_ret(g);
  1379. for(i=0;i<MAX_GUILDALLIANCE;i++){ // Destroy all relationships
  1380. if(g->alliance[i].guild_id==guild_id){
  1381. for(j=0;j<g->max_member;j++)
  1382. if( (sd=g->member[j].sd)!=NULL )
  1383. clif_guild_delalliance(sd,guild_id,g->alliance[i].opposition);
  1384. intif_guild_alliance(g->guild_id, guild_id,0,0,g->alliance[i].opposition|8);
  1385. g->alliance[i].guild_id=0;
  1386. }
  1387. }
  1388. return 0;
  1389. }
  1390. /**
  1391. * Invoked on Castles when a guild is broken. [Skotlex]
  1392. * @see DBApply
  1393. */
  1394. int castle_guild_broken_sub(DBKey key, DBData *data, va_list ap)
  1395. {
  1396. struct guild_castle *gc = db_data2ptr(data);
  1397. int guild_id = va_arg(ap, int);
  1398. nullpo_ret(gc);
  1399. if (gc->guild_id == guild_id) {
  1400. char name[EVENT_NAME_LENGTH];
  1401. // We call castle_event::OnGuildBreak of all castles of the guild
  1402. // You can set all castle_events in the 'db/castle_db.txt'
  1403. safestrncpy(name, gc->castle_event, sizeof(name));
  1404. npc_event_do(strcat(name, "::OnGuildBreak"));
  1405. //Save the new 'owner', this should invoke guardian clean up and other such things.
  1406. guild_castledatasave(gc->castle_id, 1, 0);
  1407. }
  1408. return 0;
  1409. }
  1410. //Invoked on /breakguild "Guild name"
  1411. int guild_broken(int guild_id,int flag) {
  1412. struct guild *g = guild_search(guild_id);
  1413. int i;
  1414. if (flag != 0 || g == NULL)
  1415. return 0;
  1416. for (i = 0; i < g->max_member; i++){ // Destroy all relationships
  1417. struct map_session_data *sd = g->member[i].sd;
  1418. if(sd != NULL){
  1419. if(sd->state.storage_flag == 2)
  1420. gstorage_storage_quit(sd,1);
  1421. sd->status.guild_id=0;
  1422. sd->guild = NULL;
  1423. sd->state.gmaster_flag = 0;
  1424. clif_guild_broken(g->member[i].sd,0);
  1425. clif_charnameupdate(sd); // [LuzZza]
  1426. status_change_end(&sd->bl,SC_LEADERSHIP,INVALID_TIMER);
  1427. status_change_end(&sd->bl,SC_GLORYWOUNDS,INVALID_TIMER);
  1428. status_change_end(&sd->bl,SC_SOULCOLD,INVALID_TIMER);
  1429. status_change_end(&sd->bl,SC_HAWKEYES,INVALID_TIMER);
  1430. }
  1431. }
  1432. guild_db->foreach(guild_db,guild_broken_sub,guild_id);
  1433. castle_db->foreach(castle_db,castle_guild_broken_sub,guild_id);
  1434. gstorage_delete(guild_id);
  1435. if( channel_config.ally_enable ) {
  1436. channel_delete(g->channel);
  1437. }
  1438. idb_remove(guild_db,guild_id);
  1439. return 0;
  1440. }
  1441. /** Changes the Guild Master to the specified player. [Skotlex]
  1442. * @param guild_id
  1443. * @param sd New guild master
  1444. */
  1445. int guild_gm_change(int guild_id, struct map_session_data *sd) {
  1446. struct guild *g;
  1447. nullpo_ret(sd);
  1448. if (sd->status.guild_id != guild_id)
  1449. return 0;
  1450. g = guild_search(guild_id);
  1451. nullpo_ret(g);
  1452. if (strcmp(g->master, sd->status.name) == 0) //Nothing to change.
  1453. return 0;
  1454. //Notify servers that master has changed.
  1455. intif_guild_change_gm(guild_id, sd->status.name, strlen(sd->status.name)+1);
  1456. return 1;
  1457. }
  1458. /** Notification from Char server that a guild's master has changed. [Skotlex]
  1459. * @param guild_id
  1460. * @param account_id
  1461. * @param char_id
  1462. */
  1463. int guild_gm_changed(int guild_id, uint32 account_id, uint32 char_id) {
  1464. struct guild *g;
  1465. struct guild_member gm;
  1466. int pos, i;
  1467. g=guild_search(guild_id);
  1468. if (!g)
  1469. return 0;
  1470. for(pos=0; pos<g->max_member && !(
  1471. g->member[pos].account_id==account_id &&
  1472. g->member[pos].char_id==char_id);
  1473. pos++);
  1474. if (pos == 0 || pos == g->max_member) return 0;
  1475. memcpy(&gm, &g->member[pos], sizeof (struct guild_member));
  1476. memcpy(&g->member[pos], &g->member[0], sizeof(struct guild_member));
  1477. memcpy(&g->member[0], &gm, sizeof(struct guild_member));
  1478. g->member[pos].position = g->member[0].position;
  1479. g->member[0].position = 0; //Position 0: guild Master.
  1480. strcpy(g->master, g->member[0].name);
  1481. if (g->member[pos].sd && g->member[pos].sd->fd) {
  1482. clif_displaymessage(g->member[pos].sd->fd, msg_txt(g->member[pos].sd,678)); //"You no longer are the Guild Master."
  1483. g->member[pos].sd->state.gmaster_flag = 0;
  1484. }
  1485. if (g->member[0].sd && g->member[0].sd->fd) {
  1486. clif_displaymessage(g->member[0].sd->fd, msg_txt(g->member[pos].sd,679)); //"You have become the Guild Master!"
  1487. g->member[0].sd->state.gmaster_flag = 1;
  1488. //Block his skills to prevent abuse.
  1489. if (battle_config.guild_skill_relog_delay)
  1490. guild_block_skill(g->member[0].sd, battle_config.guild_skill_relog_delay);
  1491. }
  1492. // announce the change to all guild members
  1493. for( i = 0; i < g->max_member; i++ ) {
  1494. if( g->member[i].sd && g->member[i].sd->fd ) {
  1495. clif_guild_basicinfo(g->member[i].sd);
  1496. clif_guild_memberlist(g->member[i].sd);
  1497. }
  1498. }
  1499. return 1;
  1500. }
  1501. /** Disband a guild
  1502. * @param sd Player who breaks the guild
  1503. * @param name Guild name
  1504. */
  1505. int guild_break(struct map_session_data *sd,char *name) {
  1506. struct guild *g;
  1507. struct unit_data *ud;
  1508. int i;
  1509. #ifdef BOUND_ITEMS
  1510. int j;
  1511. int idxlist[MAX_INVENTORY];
  1512. #endif
  1513. nullpo_ret(sd);
  1514. if ((g=sd->guild)==NULL)
  1515. return 0;
  1516. if (strcmp(g->name,name) != 0)
  1517. return 0;
  1518. if (!sd->state.gmaster_flag)
  1519. return 0;
  1520. for (i = 0; i < g->max_member; i++) {
  1521. if( g->member[i].account_id>0 && (
  1522. g->member[i].account_id!=sd->status.account_id ||
  1523. g->member[i].char_id!=sd->status.char_id ))
  1524. break;
  1525. }
  1526. if (i < g->max_member) {
  1527. clif_guild_broken(sd,2);
  1528. return 0;
  1529. }
  1530. /* Regardless of char server allowing it, we clear the guild master's auras */
  1531. if ((ud = unit_bl2ud(&sd->bl))) {
  1532. int count = 0;
  1533. struct skill_unit_group *group[4];
  1534. for(i = 0; i < MAX_SKILLUNITGROUP && ud->skillunit[i]; i++) {
  1535. switch(ud->skillunit[i]->skill_id) {
  1536. case GD_LEADERSHIP:
  1537. case GD_GLORYWOUNDS:
  1538. case GD_SOULCOLD:
  1539. case GD_HAWKEYES:
  1540. if(count == 4)
  1541. ShowWarning("guild_break: '%s' got more than 4 guild aura instances! (%d)\n",sd->status.name,ud->skillunit[i]->skill_id);
  1542. else
  1543. group[count++] = ud->skillunit[i];
  1544. break;
  1545. }
  1546. }
  1547. for (i = 0; i < count; i++)
  1548. skill_delunitgroup(group[i]);
  1549. }
  1550. #ifdef BOUND_ITEMS
  1551. //Guild bound item check - Removes the bound flag
  1552. j = pc_bound_chk(sd,BOUND_GUILD,idxlist);
  1553. for(i = 0; i < j; i++)
  1554. pc_delitem(sd,idxlist[i],sd->status.inventory[idxlist[i]].amount,0,1,LOG_TYPE_BOUND_REMOVAL);
  1555. #endif
  1556. intif_guild_break(g->guild_id);
  1557. return 1;
  1558. }
  1559. /**
  1560. * Creates a list of guild castle IDs to be requested
  1561. * from char-server.
  1562. */
  1563. void guild_castle_map_init(void) {
  1564. int num = db_size(castle_db);
  1565. if (num > 0) {
  1566. struct guild_castle* gc = NULL;
  1567. int *castle_ids, *cursor;
  1568. DBIterator* iter = NULL;
  1569. CREATE(castle_ids, int, num);
  1570. cursor = castle_ids;
  1571. iter = db_iterator(castle_db);
  1572. for (gc = dbi_first(iter); dbi_exists(iter); gc = dbi_next(iter)) {
  1573. *(cursor++) = gc->castle_id;
  1574. }
  1575. dbi_destroy(iter);
  1576. if (intif_guild_castle_dataload(num, castle_ids))
  1577. ShowStatus("Requested '"CL_WHITE"%d"CL_RESET"' guild castles from char-server...\n", num);
  1578. aFree(castle_ids);
  1579. }
  1580. }
  1581. /**
  1582. * Setter function for members of guild_castle struct.
  1583. * Handles all side-effects, like updating guardians.
  1584. * Sends updated info to char-server for saving.
  1585. * @param castle_id Castle ID
  1586. * @param index Type of data to change
  1587. * @param value New value
  1588. */
  1589. int guild_castledatasave(int castle_id, int index, int value) {
  1590. struct guild_castle *gc = guild_castle_search(castle_id);
  1591. if (gc == NULL) {
  1592. ShowWarning("guild_castledatasave: guild castle '%d' not found\n", castle_id);
  1593. return 0;
  1594. }
  1595. switch (index) {
  1596. case 1: // The castle's owner has changed? Update or remove Guardians too. [Skotlex]
  1597. {
  1598. int i;
  1599. gc->guild_id = value;
  1600. for (i = 0; i < MAX_GUARDIANS; i++){
  1601. struct mob_data *gd;
  1602. if (gc->guardian[i].visible && (gd = map_id2md(gc->guardian[i].id)) != NULL)
  1603. mob_guardian_guildchange(gd);
  1604. }
  1605. break;
  1606. }
  1607. case 2:
  1608. gc->economy = value; break;
  1609. case 3: // defense invest change -> recalculate guardian hp
  1610. {
  1611. int i;
  1612. gc->defense = value;
  1613. for (i = 0; i < MAX_GUARDIANS; i++){
  1614. struct mob_data *gd;
  1615. if (gc->guardian[i].visible && (gd = map_id2md(gc->guardian[i].id)) != NULL)
  1616. status_calc_mob(gd, SCO_NONE);
  1617. }
  1618. break;
  1619. }
  1620. case 4:
  1621. gc->triggerE = value; break;
  1622. case 5:
  1623. gc->triggerD = value; break;
  1624. case 6:
  1625. gc->nextTime = value; break;
  1626. case 7:
  1627. gc->payTime = value; break;
  1628. case 8:
  1629. gc->createTime = value; break;
  1630. case 9:
  1631. gc->visibleC = value; break;
  1632. default:
  1633. if (index > 9 && index <= 9+MAX_GUARDIANS) {
  1634. gc->guardian[index-10].visible = value;
  1635. break;
  1636. }
  1637. ShowWarning("guild_castledatasave: index = '%d' is out of allowed range\n", index);
  1638. return 0;
  1639. }
  1640. if (!intif_guild_castle_datasave(castle_id, index, value)) {
  1641. guild_castle_reconnect(castle_id, index, value);
  1642. }
  1643. return 0;
  1644. }
  1645. void guild_castle_reconnect_sub(void *key, void *data, va_list ap) {
  1646. int castle_id = GetWord((int)__64BPRTSIZE(key), 0);
  1647. int index = GetWord((int)__64BPRTSIZE(key), 1);
  1648. intif_guild_castle_datasave(castle_id, index, *(int *)data);
  1649. aFree(data);
  1650. }
  1651. /**
  1652. * Saves pending guild castle data changes when char-server is
  1653. * disconnected.
  1654. * On reconnect pushes all changes to char-server for saving.
  1655. * @param castle_id
  1656. * @param index
  1657. * @param value
  1658. */
  1659. void guild_castle_reconnect(int castle_id, int index, int value) {
  1660. static struct linkdb_node *gc_save_pending = NULL;
  1661. if (castle_id < 0) { // char-server reconnected
  1662. linkdb_foreach(&gc_save_pending, guild_castle_reconnect_sub);
  1663. linkdb_final(&gc_save_pending);
  1664. } else {
  1665. int *data;
  1666. CREATE(data, int, 1);
  1667. *data = value;
  1668. linkdb_replace(&gc_save_pending, (void*)__64BPRTSIZE((MakeDWord(castle_id, index))), data);
  1669. }
  1670. }
  1671. /** Load castle data then invoke OnAgitInit* on last
  1672. * @param len
  1673. * @param gc Guild Castle data
  1674. */
  1675. int guild_castledataloadack(int len, struct guild_castle *gc) {
  1676. int i;
  1677. int n = (len-4) / sizeof(struct guild_castle);
  1678. int ev;
  1679. nullpo_ret(gc);
  1680. //Last owned castle in the list invokes ::OnAgitInit
  1681. for( i = n-1; i >= 0 && !(gc[i].guild_id); --i );
  1682. ev = i; // offset of castle or -1
  1683. if( ev < 0 ) { //No castles owned, invoke OnAgitInit as it is.
  1684. npc_event_doall("OnAgitInit");
  1685. npc_event_doall("OnAgitInit2");
  1686. } else // load received castles into memory, one by one
  1687. for( i = 0; i < n; i++, gc++ ) {
  1688. struct guild_castle *c = guild_castle_search(gc->castle_id);
  1689. if (!c) {
  1690. ShowError("guild_castledataloadack: castle id=%d not found.\n", gc->castle_id);
  1691. continue;
  1692. }
  1693. // update map-server castle data with new info
  1694. memcpy(&c->guild_id, &gc->guild_id, sizeof(struct guild_castle) - offsetof(struct guild_castle, guild_id));
  1695. if( c->guild_id ) {
  1696. if( i != ev )
  1697. guild_request_info(c->guild_id);
  1698. else { // last owned one
  1699. guild_npc_request_info(c->guild_id, "::OnAgitInit");
  1700. guild_npc_request_info(c->guild_id, "::OnAgitInit2");
  1701. }
  1702. }
  1703. }
  1704. ShowStatus("Received '"CL_WHITE"%d"CL_RESET"' guild castles from char-server.\n", n);
  1705. return 0;
  1706. }
  1707. /*====================================================
  1708. * Start normal woe and triggers all npc OnAgitStart
  1709. *---------------------------------------------------*/
  1710. void guild_agit_start(void) { // Run All NPC_Event[OnAgitStart]
  1711. int c = npc_event_doall("OnAgitStart");
  1712. ShowStatus("NPC_Event:[OnAgitStart] Run (%d) Events by @AgitStart.\n",c);
  1713. }
  1714. /*====================================================
  1715. * End normal woe and triggers all npc OnAgitEnd
  1716. *---------------------------------------------------*/
  1717. void guild_agit_end(void) { // Run All NPC_Event[OnAgitEnd]
  1718. int c = npc_event_doall("OnAgitEnd");
  1719. ShowStatus("NPC_Event:[OnAgitEnd] Run (%d) Events by @AgitEnd.\n",c);
  1720. }
  1721. /*====================================================
  1722. * Start woe2 and triggers all npc OnAgitStart2
  1723. *---------------------------------------------------*/
  1724. void guild_agit2_start(void) { // Run All NPC_Event[OnAgitStart2]
  1725. int c = npc_event_doall("OnAgitStart2");
  1726. ShowStatus("NPC_Event:[OnAgitStart2] Run (%d) Events by @AgitStart2.\n",c);
  1727. }
  1728. /*====================================================
  1729. * End woe2 and triggers all npc OnAgitEnd2
  1730. *---------------------------------------------------*/
  1731. void guild_agit2_end(void) { // Run All NPC_Event[OnAgitEnd2]
  1732. int c = npc_event_doall("OnAgitEnd2");
  1733. ShowStatus("NPC_Event:[OnAgitEnd2] Run (%d) Events by @AgitEnd2.\n",c);
  1734. }
  1735. // How many castles does this guild have?
  1736. int guild_checkcastles(struct guild *g) {
  1737. int nb_cas = 0;
  1738. struct guild_castle* gc = NULL;
  1739. DBIterator *iter = db_iterator(castle_db);
  1740. for (gc = dbi_first(iter); dbi_exists(iter); gc = dbi_next(iter)) {
  1741. if (gc->guild_id == g->guild_id) {
  1742. nb_cas++;
  1743. }
  1744. }
  1745. dbi_destroy(iter);
  1746. return nb_cas;
  1747. }
  1748. // Are these two guilds allied?
  1749. bool guild_isallied(int guild_id, int guild_id2) {
  1750. int i;
  1751. struct guild* g = guild_search(guild_id);
  1752. nullpo_ret(g);
  1753. ARR_FIND( 0, MAX_GUILDALLIANCE, i, g->alliance[i].guild_id == guild_id2 );
  1754. return( i < MAX_GUILDALLIANCE && g->alliance[i].opposition == 0 );
  1755. }
  1756. void guild_flag_add(struct npc_data *nd) {
  1757. int i;
  1758. /* check */
  1759. for( i = 0; i < guild_flags_count; i++ ) {
  1760. if( guild_flags[i] && guild_flags[i]->bl.id == nd->bl.id ) {
  1761. return;/* exists, most likely updated the id. */
  1762. }
  1763. }
  1764. i = guild_flags_count;/* save the current slot */
  1765. /* add */
  1766. RECREATE(guild_flags,struct npc_data*,++guild_flags_count);
  1767. /* save */
  1768. guild_flags[i] = nd;
  1769. }
  1770. void guild_flag_remove(struct npc_data *nd) {
  1771. int i, cursor;
  1772. if( guild_flags_count == 0 )
  1773. return;
  1774. /* find it */
  1775. for( i = 0; i < guild_flags_count; i++ ) {
  1776. if( guild_flags[i] && guild_flags[i]->bl.id == nd->bl.id ) {/* found */
  1777. guild_flags[i] = NULL;
  1778. break;
  1779. }
  1780. }
  1781. /* compact list */
  1782. for( i = 0, cursor = 0; i < guild_flags_count; i++ ) {
  1783. if( guild_flags[i] == NULL )
  1784. continue;
  1785. if( cursor != i ) {
  1786. memmove(&guild_flags[cursor], &guild_flags[i], sizeof(struct npc_data*));
  1787. }
  1788. cursor++;
  1789. }
  1790. }
  1791. /**
  1792. * @see DBApply
  1793. */
  1794. static int eventlist_db_final(DBKey key, DBData *data, va_list ap) {
  1795. struct eventlist *next = NULL;
  1796. struct eventlist *current = db_data2ptr(data);
  1797. while (current != NULL) {
  1798. next = current->next;
  1799. aFree(current);
  1800. current = next;
  1801. }
  1802. return 0;
  1803. }
  1804. /**
  1805. * @see DBApply
  1806. */
  1807. static int guild_expcache_db_final(DBKey key, DBData *data, va_list ap) {
  1808. ers_free(expcache_ers, db_data2ptr(data));
  1809. return 0;
  1810. }
  1811. /**
  1812. * @see DBApply
  1813. */
  1814. static int guild_castle_db_final(DBKey key, DBData *data, va_list ap) {
  1815. struct guild_castle* gc = db_data2ptr(data);
  1816. if( gc->temp_guardians )
  1817. aFree(gc->temp_guardians);
  1818. aFree(gc);
  1819. return 0;
  1820. }
  1821. /* called when scripts are reloaded/unloaded */
  1822. void guild_flags_clear(void) {
  1823. int i;
  1824. for( i = 0; i < guild_flags_count; i++ ) {
  1825. if( guild_flags[i] )
  1826. guild_flags[i] = NULL;
  1827. }
  1828. guild_flags_count = 0;
  1829. }
  1830. void do_init_guild(void) {
  1831. const char* dbsubpath[] = {
  1832. "",
  1833. "/"DBIMPORT,
  1834. };
  1835. int i;
  1836. guild_db = idb_alloc(DB_OPT_RELEASE_DATA);
  1837. castle_db = idb_alloc(DB_OPT_BASE);
  1838. guild_expcache_db = idb_alloc(DB_OPT_BASE);
  1839. guild_infoevent_db = idb_alloc(DB_OPT_BASE);
  1840. expcache_ers = ers_new(sizeof(struct guild_expcache),"guild.c::expcache_ers",ERS_OPT_NONE);
  1841. guild_flags_count = 0;
  1842. memset(guild_skill_tree,0,sizeof(guild_skill_tree));
  1843. for(i=0; i<ARRAYLENGTH(dbsubpath); i++){
  1844. int n1 = strlen(db_path)+strlen(dbsubpath[i])+1;
  1845. char* dbsubpath1 = (char*)aMalloc(n1+1);
  1846. safesnprintf(dbsubpath1,n1+1,"%s%s",db_path,dbsubpath[i]);
  1847. sv_readdb(dbsubpath1, "castle_db.txt", ',', 4, 4, -1, &guild_read_castledb, i);
  1848. sv_readdb(dbsubpath1, "guild_skill_tree.txt", ',', 2+MAX_GUILD_SKILL_REQUIRE*2, 2+MAX_GUILD_SKILL_REQUIRE*2, -1, &guild_read_guildskill_tree_db, i); //guild skill tree [Komurka]
  1849. aFree(dbsubpath1);
  1850. }
  1851. add_timer_func_list(guild_payexp_timer,"guild_payexp_timer");
  1852. add_timer_func_list(guild_send_xy_timer, "guild_send_xy_timer");
  1853. add_timer_interval(gettick()+GUILD_PAYEXP_INTERVAL,guild_payexp_timer,0,0,GUILD_PAYEXP_INTERVAL);
  1854. add_timer_interval(gettick()+GUILD_SEND_XY_INTERVAL,guild_send_xy_timer,0,0,GUILD_SEND_XY_INTERVAL);
  1855. }
  1856. void do_final_guild(void) {
  1857. DBIterator *iter = db_iterator(guild_db);
  1858. struct guild *g;
  1859. for( g = dbi_first(iter); dbi_exists(iter); g = dbi_next(iter) ) {
  1860. channel_delete(g->channel);
  1861. }
  1862. dbi_destroy(iter);
  1863. db_destroy(guild_db);
  1864. castle_db->destroy(castle_db,guild_castle_db_final);
  1865. guild_expcache_db->destroy(guild_expcache_db,guild_expcache_db_final);
  1866. guild_infoevent_db->destroy(guild_infoevent_db,eventlist_db_final);
  1867. ers_destroy(expcache_ers);
  1868. aFree(guild_flags);/* never empty; created on boot */
  1869. }