guild.cpp 65 KB

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