guild.cpp 70 KB

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