guild.cpp 64 KB

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