guild.cpp 72 KB

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