guild.cpp 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755
  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->state.gmaster_flag = 1;
  670. clif_guild_masterormember(*sd);
  671. }
  672. } else {
  673. before = g->guild;
  674. }
  675. memcpy(&g->guild, &sg, sizeof(struct mmo_guild));
  676. if(g->guild.max_member > MAX_GUILD) {
  677. 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);
  678. g->guild.max_member = MAX_GUILD;
  679. }
  680. for(i=bm=m=0;i<g->guild.max_member;i++){
  681. if(g->guild.member[i].account_id>0){
  682. 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);
  683. m++;
  684. }else
  685. g->guild.member[i].sd=nullptr;
  686. if(before.member[i].account_id>0)
  687. bm++;
  688. }
  689. for (i = 0; i < g->guild.max_member; i++) { //Transmission of information at all members
  690. sd = g->guild.member[i].sd;
  691. if( sd==nullptr )
  692. continue;
  693. if( sd->guild == nullptr || sd->guild != g ){
  694. sd->guild = g;
  695. clif_name_area(&sd->bl);
  696. }
  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. #if PACKETVER >= 20200902
  712. // Clients after this version need this packet to show the guild name on alt+a
  713. clif_guild_basicinfo( *sd );
  714. #endif
  715. clif_guild_belonginfo( *sd );
  716. clif_guild_notice( *sd );
  717. sd->guild_emblem_id = g->guild.emblem_id;
  718. }
  719. if (g->instance_id > 0)
  720. instance_reqinfo(sd, g->instance_id);
  721. }
  722. //Occurrence of an event
  723. if (guild_infoevent_db->remove(guild_infoevent_db, db_i2key(sg.guild_id), &data)) {
  724. struct eventlist *ev = (struct eventlist *)db_data2ptr(&data), *ev2;
  725. while(ev) {
  726. npc_event_do(ev->name);
  727. ev2=ev->next;
  728. aFree(ev);
  729. ev=ev2;
  730. }
  731. }
  732. return 0;
  733. }
  734. /*=============================================
  735. * Player sd send a guild invatation to player tsd to join his guild
  736. *--------------------------------------------*/
  737. bool guild_invite( map_session_data& sd, map_session_data* tsd ){
  738. // No nullpo_retr, because its valid that target players might not exist or are not online
  739. if( tsd == nullptr ){
  740. return false;
  741. }
  742. auto& g = sd.guild;
  743. if( g == nullptr ){
  744. return false;
  745. }
  746. if( g->instance_id && battle_config.instance_block_invite ){
  747. return false;
  748. }
  749. // Guild locked.
  750. if( map_getmapflag( sd.bl.m, MF_GUILDLOCK ) ){
  751. clif_displaymessage( sd.fd, msg_txt( &sd, 228 ) ); // Guild modification is disabled on this map.
  752. return false;
  753. }
  754. // @noask [LuzZza]
  755. if( tsd->state.noask ){
  756. clif_noask_sub( sd, *tsd, 395 ); // Autorejected guild invite from %s.
  757. return false;
  758. }
  759. // Players in a clan can not join a guild
  760. if( tsd->clan ){
  761. // TODO: message?
  762. return false;
  763. }
  764. // Invite permission.
  765. if( !guild_has_permission( sd, GUILD_PERM_INVITE ) ){
  766. // TODO: message?
  767. return false;
  768. }
  769. // Checking if there no other invitation pending
  770. if( !battle_config.invite_request_check && ( tsd->party_invite > 0 || tsd->state.trading || tsd->adopt_invite ) ){
  771. clif_guild_inviteack( sd, 0 );
  772. return false;
  773. }
  774. // You can't invite someone who has already disconnected.
  775. if( !session_isActive( tsd->fd ) ){
  776. clif_guild_inviteack(sd,1);
  777. return false;
  778. }
  779. // Can't invite people inside castles. [Skotlex]
  780. if( tsd->status.guild_id > 0 || tsd->guild_invite > 0 || map_flag_gvg2( tsd->bl.m ) ){
  781. clif_guild_inviteack(sd,0);
  782. return false;
  783. }
  784. int i;
  785. //search an empty spot in guild
  786. ARR_FIND( 0, g->guild.max_member, i, g->guild.member[i].account_id == 0 );
  787. if( i == g->guild.max_member ){
  788. clif_guild_inviteack(sd,3);
  789. return false;
  790. }
  791. tsd->guild_invite = sd.status.guild_id;
  792. tsd->guild_invite_account = sd.status.account_id;
  793. clif_guild_invite(*tsd, g->guild);
  794. return true;
  795. }
  796. /// Guild invitation reply.
  797. /// flag: 0:rejected, 1:accepted
  798. bool guild_reply_invite( map_session_data& sd, int guild_id, int flag ){
  799. // subsequent requests may override the value
  800. if( sd.guild_invite != guild_id ){
  801. return false; // mismatch
  802. }
  803. // look up the person who sent the invite
  804. //NOTE: this can be nullptr because the person might have logged off in the meantime
  805. map_session_data* tsd = map_id2sd( sd.guild_invite_account );
  806. // Already in another guild.
  807. if( sd.status.guild_id > 0 ){
  808. // Set the flag to rejected, no matter what
  809. flag = 0;
  810. }
  811. // rejected
  812. if( flag == 0 ){
  813. sd.guild_invite = 0;
  814. sd.guild_invite_account = 0;
  815. if( tsd != nullptr ){
  816. clif_guild_inviteack( *tsd, 1 );
  817. }
  818. return true;
  819. }
  820. // accepted
  821. auto g = guild_search( guild_id );
  822. if( g == nullptr ){
  823. sd.guild_invite = 0;
  824. sd.guild_invite_account = 0;
  825. return false;
  826. }
  827. if( g->instance_id && battle_config.instance_block_invite ){
  828. sd.guild_invite = 0;
  829. sd.guild_invite_account = 0;
  830. return false;
  831. }
  832. int i;
  833. ARR_FIND( 0, g->guild.max_member, i, g->guild.member[i].account_id == 0 );
  834. if( i == g->guild.max_member ){
  835. sd.guild_invite = 0;
  836. sd.guild_invite_account = 0;
  837. if( tsd != nullptr ){
  838. clif_guild_inviteack( *tsd, 3 );
  839. }
  840. return true;
  841. }
  842. struct guild_member m = {};
  843. guild_makemember( m, sd );
  844. intif_guild_addmember( guild_id, m );
  845. //TODO: send a minimap update to this player
  846. return true;
  847. }
  848. //Invoked when a player joins.
  849. //- If guild is not in memory, it is requested
  850. //- Otherwise sd pointer is set up.
  851. //- Player must be authed and must belong to a guild before invoking this method
  852. void guild_member_joined(map_session_data *sd) {
  853. int i;
  854. auto g = guild_search(sd->status.guild_id);
  855. if (!g) {
  856. guild_request_info(sd->status.guild_id);
  857. return;
  858. }
  859. if (strcmp(sd->status.name,g->guild.master) == 0) { // set the Guild Master flag
  860. sd->state.gmaster_flag = 1;
  861. #ifndef RENEWAL
  862. // prevent Guild Skills from being used directly after relog
  863. if( sd->state.connect_new == 1 && battle_config.guild_skill_relog_delay )
  864. guild_block_skill(sd, battle_config.guild_skill_relog_delay);
  865. #endif
  866. }
  867. i = guild_getindex(g->guild, sd->status.account_id, sd->status.char_id);
  868. if (i == -1)
  869. sd->status.guild_id = 0;
  870. else {
  871. g->guild.member[i].sd = sd;
  872. sd->guild = g;
  873. clif_name_area(&sd->bl);
  874. if( channel_config.ally_tmpl.name[0] && (channel_config.ally_tmpl.opt&CHAN_OPT_AUTOJOIN) ) {
  875. channel_gjoin(sd,3);
  876. }
  877. }
  878. }
  879. /*==========================================
  880. * Add a player to a given guild_id
  881. *----------------------------------------*/
  882. int guild_member_added(int guild_id,uint32 account_id,uint32 char_id,int flag) {
  883. map_session_data *sd= map_id2sd(account_id),*sd2;
  884. auto g = guild_search(guild_id);
  885. if (!g)
  886. return 0;
  887. if(sd==nullptr || sd->guild_invite==0){
  888. // cancel if player not present or invalide guild_id invitation
  889. if (flag == 0) {
  890. ShowError("guild: member added error %d is not online\n",account_id);
  891. intif_guild_leave(guild_id,account_id,char_id,0,"** Data Error **");
  892. }
  893. return 0;
  894. }
  895. sd2 = map_id2sd(sd->guild_invite_account);
  896. sd->guild_invite = 0;
  897. sd->guild_invite_account = 0;
  898. if (flag == 1) { //failure
  899. if( sd2!=nullptr )
  900. clif_guild_inviteack( *sd2, 3 );
  901. return 0;
  902. }
  903. //if all ok add player to guild
  904. sd->status.guild_id = g->guild.guild_id;
  905. sd->guild_emblem_id = g->guild.emblem_id;
  906. sd->guild = g;
  907. //Packets which were sent in the previous 'guild_sent' implementation.
  908. clif_guild_belonginfo( *sd );
  909. clif_guild_notice( *sd );
  910. // Send emblem update to self and people around
  911. clif_guild_emblem_area(&sd->bl);
  912. if( sd2!=nullptr )
  913. clif_guild_inviteack( *sd2, 2 );
  914. // Update display name
  915. clif_name_area(&sd->bl);
  916. if (g->instance_id > 0)
  917. instance_reqinfo(sd, g->instance_id);
  918. return 0;
  919. }
  920. /*==========================================
  921. * Player request leaving a given guild_id
  922. *----------------------------------------*/
  923. bool guild_leave( map_session_data& sd, int guild_id, uint32 account_id, uint32 char_id, const char* mes ){
  924. auto& g = sd.guild;
  925. if( g == nullptr ){
  926. return false;
  927. }
  928. if( g->instance_id > 0 && battle_config.instance_block_leave ){
  929. return false;
  930. }
  931. if( map_getmapflag( sd.bl.m, MF_GUILDLOCK ) ){
  932. clif_displaymessage( sd.fd, msg_txt( &sd, 228 ) ); // Guild modification is disabled on this map.
  933. return false;
  934. }
  935. if( sd.bg_id ){
  936. clif_displaymessage( sd.fd, msg_txt( &sd, 670 ) ); // You can't leave battleground guilds.
  937. return false;
  938. }
  939. 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 ) ){
  940. return false;
  941. }
  942. guild_trade_bound_cancel(sd);
  943. return intif_guild_leave( sd.status.guild_id, sd.status.account_id, sd.status.char_id, 0, mes );
  944. }
  945. /*==========================================
  946. * Request remove a player to a given guild_id
  947. *----------------------------------------*/
  948. bool guild_expulsion( map_session_data& sd, int guild_id, uint32 account_id, uint32 char_id, const char* mes ){
  949. auto& g = sd.guild;
  950. if( g == nullptr ){
  951. return false;
  952. }
  953. if( sd.status.guild_id != guild_id ){
  954. return false;
  955. }
  956. if( !guild_has_permission( sd, GUILD_PERM_EXPEL ) ){
  957. return false;
  958. }
  959. if( g->instance_id > 0 && battle_config.instance_block_expulsion ){
  960. return false;
  961. }
  962. // TODO: for leave this is different messages
  963. if( sd.bg_id || map_getmapflag( sd.bl.m, MF_GUILDLOCK ) ){
  964. clif_displaymessage( sd.fd, msg_txt( &sd, 228 ) ); // Guild modification is disabled on this map.
  965. return false;
  966. }
  967. map_session_data *tsd = map_id2sd( account_id );
  968. //Can't leave inside guild castles.
  969. if( tsd != nullptr && tsd->status.char_id == char_id && map_flag_gvg2( tsd->bl.m ) ){
  970. return false;
  971. }
  972. // find the member and perform expulsion
  973. int i = guild_getindex( g->guild, account_id, char_id );
  974. if( i < 0 ){
  975. return false;
  976. }
  977. // Can't expel the guild leader
  978. if( strcmp( g->guild.member[i].name, g->guild.master ) == 0 ){
  979. return false;
  980. }
  981. if( tsd != nullptr ){
  982. guild_trade_bound_cancel( *tsd );
  983. }
  984. return intif_guild_leave( g->guild.guild_id, account_id, char_id, 1, mes );
  985. }
  986. /**
  987. * A confirmation from inter-serv that player is kicked successfully
  988. * @param guild_Id
  989. * @param account_id
  990. * @param char_id
  991. * @param flag
  992. * @param name
  993. * @param mes
  994. */
  995. int guild_member_withdraw(int guild_id, uint32 account_id, uint32 char_id, int flag, const char* name, const char* mes) {
  996. int i;
  997. auto g = guild_search(guild_id);
  998. map_session_data* sd = map_charid2sd(char_id);
  999. map_session_data* online_member_sd;
  1000. if (!g)
  1001. return 0; // no such guild (error!)
  1002. i = guild_getindex(g->guild, account_id, char_id);
  1003. if( i == -1 )
  1004. return 0; // not a member (inconsistency!)
  1005. #ifdef BOUND_ITEMS
  1006. //Guild bound item check
  1007. guild_retrieveitembound(char_id,account_id,guild_id);
  1008. #endif
  1009. online_member_sd = guild_getavailablesd(g->guild);
  1010. if(online_member_sd == nullptr)
  1011. return 0; // noone online to inform
  1012. if(!flag)
  1013. clif_guild_leave( *online_member_sd, name, char_id, mes );
  1014. else
  1015. clif_guild_expulsion( *online_member_sd, name, char_id, mes );
  1016. // remove member from guild
  1017. memset(&g->guild.member[i],0,sizeof(struct guild_member));
  1018. clif_guild_memberlist( *online_member_sd );
  1019. // update char, if online
  1020. if(sd != nullptr && sd->status.guild_id == guild_id) {
  1021. // do stuff that needs the guild_id first, BEFORE we wipe it
  1022. if (sd->state.storage_flag == 2) //Close the guild storage.
  1023. storage_guild_storageclose(sd);
  1024. guild_send_dot_remove(sd);
  1025. channel_pcquit(sd,3); //leave guild and ally chan
  1026. sd->status.guild_id = 0;
  1027. sd->guild = nullptr;
  1028. sd->guild_emblem_id = 0;
  1029. if (g->instance_id) {
  1030. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  1031. // User was on the instance map of the guild
  1032. if( g->instance_id == mapdata->instance_id ){
  1033. pc_setpos_savepoint( *sd );
  1034. }
  1035. }
  1036. clif_name_area(&sd->bl); //Update display name [Skotlex]
  1037. status_change_end(&sd->bl,SC_LEADERSHIP);
  1038. status_change_end(&sd->bl,SC_GLORYWOUNDS);
  1039. status_change_end(&sd->bl,SC_SOULCOLD);
  1040. status_change_end(&sd->bl,SC_HAWKEYES);
  1041. status_change_end(&sd->bl,SC_EMERGENCY_MOVE);
  1042. // Send emblem update to self and people around
  1043. clif_guild_emblem_area(&sd->bl);
  1044. }
  1045. return 0;
  1046. }
  1047. #ifdef BOUND_ITEMS
  1048. /**
  1049. * Retrieve guild bound items from kicked member
  1050. * @param char_id
  1051. * @param account_id
  1052. * @param guild_id
  1053. */
  1054. void guild_retrieveitembound(uint32 char_id, uint32 account_id, int guild_id) {
  1055. TBL_PC *sd = map_charid2sd(char_id);
  1056. if (sd) { //Character is online
  1057. int idxlist[MAX_INVENTORY];
  1058. int j;
  1059. j = pc_bound_chk(sd,BOUND_GUILD,idxlist);
  1060. if (j) {
  1061. struct s_storage* stor = guild2storage(sd->status.guild_id);
  1062. auto g = guild_search(guild_id);
  1063. int i;
  1064. if (stor && stor->status) { //Someone is in guild storage, close them
  1065. for (i = 0; i < g->guild.max_member; i++) {
  1066. TBL_PC *pl_sd = g->guild.member[i].sd;
  1067. if (pl_sd && pl_sd->state.storage_flag == 2)
  1068. storage_guild_storageclose(pl_sd);
  1069. }
  1070. }
  1071. for (i = 0; i < j; i++) { //Loop the matching items, gstorage_additem takes care of opening storage
  1072. if (stor)
  1073. storage_guild_additem(sd,stor,&sd->inventory.u.items_inventory[idxlist[i]],sd->inventory.u.items_inventory[idxlist[i]].amount);
  1074. pc_delitem(sd,idxlist[i],sd->inventory.u.items_inventory[idxlist[i]].amount,0,4,LOG_TYPE_GSTORAGE);
  1075. }
  1076. storage_guild_storageclose(sd); //Close and save the storage
  1077. }
  1078. } else { //Character is offline, ask char server to do the job
  1079. struct s_storage* stor = guild2storage2(guild_id);
  1080. auto g = guild_search(guild_id);
  1081. if (!g)
  1082. return;
  1083. if (stor && stor->status) { //Someone is in guild storage, close them
  1084. int i;
  1085. for (i = 0; i < g->guild.max_member; i++) {
  1086. TBL_PC *pl_sd = g->guild.member[i].sd;
  1087. if (pl_sd && pl_sd->state.storage_flag == 2)
  1088. storage_guild_storageclose(pl_sd);
  1089. }
  1090. }
  1091. intif_itembound_guild_retrieve(char_id,account_id,guild_id);
  1092. }
  1093. }
  1094. #endif
  1095. int guild_send_memberinfoshort(map_session_data *sd,int online) { // cleaned up [LuzZza]
  1096. nullpo_ret(sd);
  1097. if(sd->status.guild_id <= 0)
  1098. return 0;
  1099. auto &g = sd->guild;
  1100. if (!g)
  1101. return 0;
  1102. intif_guild_memberinfoshort(g->guild.guild_id,
  1103. sd->status.account_id,sd->status.char_id,online,sd->status.base_level,sd->status.class_);
  1104. if(!online){
  1105. int i=guild_getindex(g->guild, sd->status.account_id,sd->status.char_id);
  1106. if(i>=0)
  1107. g->guild.member[i].sd=nullptr;
  1108. else
  1109. 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);
  1110. return 0;
  1111. }
  1112. if(sd->state.connect_new) { //Note that this works because it is invoked in parse_LoadEndAck before connect_new is cleared.
  1113. #if PACKETVER >= 20200902
  1114. // Clients after this version need this packet to show the guild name on alt+a
  1115. clif_guild_basicinfo( *sd );
  1116. #endif
  1117. clif_guild_belonginfo( *sd );
  1118. sd->guild_emblem_id = g->guild.emblem_id;
  1119. }
  1120. return 0;
  1121. }
  1122. int guild_recv_memberinfoshort(int guild_id,uint32 account_id,uint32 char_id,int online,int lv,int class_) { // cleaned up [LuzZza]
  1123. int i,alv,c,idx=-1,om=0,oldonline=-1;
  1124. auto g = guild_search(guild_id);
  1125. if(g == nullptr)
  1126. return 0;
  1127. for(i=0,alv=0,c=0,om=0;i<g->guild.max_member;i++){
  1128. struct guild_member *m=&g->guild.member[i];
  1129. if(!m->account_id) continue;
  1130. if(m->account_id==account_id && m->char_id==char_id ){
  1131. oldonline=m->online;
  1132. m->online=online;
  1133. m->lv=lv;
  1134. m->class_=class_;
  1135. idx=i;
  1136. }
  1137. alv+=m->lv;
  1138. c++;
  1139. if(m->online)
  1140. om++;
  1141. }
  1142. if(idx == -1 || c == 0) {
  1143. //Treat char_id who doesn't match guild_id (not found as member)
  1144. map_session_data *sd = map_id2sd(account_id);
  1145. if(sd && sd->status.char_id == char_id) {
  1146. sd->status.guild_id=0;
  1147. sd->guild_emblem_id=0;
  1148. }
  1149. ShowWarning("guild: not found member %d,%d on %d[%s]\n", account_id,char_id,guild_id,g->guild.name);
  1150. return 0;
  1151. }
  1152. g->guild.average_lv=alv/c;
  1153. g->guild.connect_member=om;
  1154. //Ensure validity of pointer (ie: player logs in/out, changes map-server)
  1155. g->guild.member[idx].sd = guild_sd_check(guild_id, account_id, char_id);
  1156. if(oldonline!=online)
  1157. clif_guild_memberlogin_notice(g->guild, idx, online);
  1158. if(!g->guild.member[idx].sd)
  1159. return 0;
  1160. //Send XY dot updates. [Skotlex]
  1161. //Moved from guild_send_memberinfoshort [LuzZza]
  1162. for(i=0; i < g->guild.max_member; i++) {
  1163. if(!g->guild.member[i].sd || i == idx ||
  1164. g->guild.member[i].sd->bl.m != g->guild.member[idx].sd->bl.m)
  1165. continue;
  1166. clif_guild_xy_single( *g->guild.member[idx].sd, *g->guild.member[i].sd );
  1167. clif_guild_xy_single( *g->guild.member[i].sd, *g->guild.member[idx].sd );
  1168. }
  1169. return 0;
  1170. }
  1171. /*====================================================
  1172. * Send a message to whole guild
  1173. *---------------------------------------------------*/
  1174. int guild_send_message(map_session_data *sd, const char *mes, size_t len) {
  1175. nullpo_ret(sd);
  1176. if(sd->status.guild_id==0)
  1177. return 0;
  1178. intif_guild_message(sd->status.guild_id,sd->status.account_id,mes,len);
  1179. guild_recv_message(sd->status.guild_id,sd->status.account_id,mes,len);
  1180. // Chat logging type 'G' / Guild Chat
  1181. 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);
  1182. return 0;
  1183. }
  1184. /*====================================================
  1185. * Guild receive a message, will be displayed to whole member
  1186. *---------------------------------------------------*/
  1187. int guild_recv_message( int guild_id, uint32 account_id, const char *mes, size_t len ){
  1188. auto g = guild_search(guild_id);
  1189. if (!g)
  1190. return 0;
  1191. clif_guild_message(g->guild,mes,len);
  1192. return 0;
  1193. }
  1194. /*====================================================
  1195. * Member changing position in guild
  1196. *---------------------------------------------------*/
  1197. int guild_change_memberposition(int guild_id,uint32 account_id,uint32 char_id,short idx) {
  1198. return intif_guild_change_memberinfo(guild_id,account_id,char_id,GMI_POSITION,&idx,sizeof(idx));
  1199. }
  1200. /*====================================================
  1201. * Notification of new position for member
  1202. *---------------------------------------------------*/
  1203. int guild_memberposition_changed(struct mmo_guild &g,int idx,int pos) {
  1204. g.member[idx].position=pos;
  1205. clif_guild_memberpositionchanged(g,idx);
  1206. // Update char position in client [LuzZza]
  1207. if(g.member[idx].sd != nullptr)
  1208. clif_guild_position_selected(*g.member[idx].sd);
  1209. return 0;
  1210. }
  1211. /*====================================================
  1212. * Change guild title or member
  1213. *---------------------------------------------------*/
  1214. int guild_change_position(int guild_id,int idx, int mode, int exp_mode, const char *name) {
  1215. struct guild_position p;
  1216. exp_mode = cap_value(exp_mode, 0, battle_config.guild_exp_limit);
  1217. p.mode = mode&GUILD_PERM_ALL;
  1218. p.exp_mode=exp_mode;
  1219. safestrncpy(p.name,name,NAME_LENGTH);
  1220. return intif_guild_position(guild_id,idx,&p);
  1221. }
  1222. /*====================================================
  1223. * Notification of member has changed his guild title
  1224. *---------------------------------------------------*/
  1225. int guild_position_changed(int guild_id,int idx,struct guild_position *p) {
  1226. auto g = guild_search(guild_id);
  1227. int i;
  1228. if(g==nullptr)
  1229. return 0;
  1230. memcpy(&g->guild.position[idx],p,sizeof(struct guild_position));
  1231. clif_guild_positionchanged(g->guild,idx);
  1232. // Update char name in client [LuzZza]
  1233. for(i=0;i<g->guild.max_member;i++)
  1234. if(g->guild.member[i].position == idx && g->guild.member[i].sd != nullptr)
  1235. clif_guild_position_selected(*g->guild.member[i].sd);
  1236. return 0;
  1237. }
  1238. /*====================================================
  1239. * Change guild notice
  1240. *---------------------------------------------------*/
  1241. int guild_change_notice(map_session_data *sd,int guild_id,const char *mes1,const char *mes2) {
  1242. nullpo_ret(sd);
  1243. if(guild_id!=sd->status.guild_id)
  1244. return 0;
  1245. return intif_guild_notice(guild_id,mes1,mes2);
  1246. }
  1247. /*====================================================
  1248. * Notification of guild has changed his notice
  1249. *---------------------------------------------------*/
  1250. int guild_notice_changed(int guild_id,const char *mes1,const char *mes2) {
  1251. int i;
  1252. auto g = guild_search(guild_id);
  1253. if (!g)
  1254. return 0;
  1255. memcpy(g->guild.mes1,mes1,MAX_GUILDMES1);
  1256. memcpy(g->guild.mes2,mes2,MAX_GUILDMES2);
  1257. for(i=0;i<g->guild.max_member;i++){
  1258. map_session_data *sd = g->guild.member[i].sd;
  1259. if(sd != nullptr)
  1260. clif_guild_notice( *sd );
  1261. }
  1262. return 0;
  1263. }
  1264. /*====================================================
  1265. * Check condition for changing guild emblem
  1266. *---------------------------------------------------*/
  1267. bool guild_check_emblem_change_condition( map_session_data& sd ){
  1268. auto &g = sd.guild;
  1269. if (battle_config.require_glory_guild && g != nullptr && guild_checkskill(g->guild, GD_GLORYGUILD) > 0) {
  1270. clif_skill_fail( sd, GD_GLORYGUILD );
  1271. return false;
  1272. }
  1273. return true;
  1274. }
  1275. /*====================================================
  1276. * Change guild emblem
  1277. *---------------------------------------------------*/
  1278. int guild_change_emblem( map_session_data& sd, int len, const char* data ){
  1279. if (!guild_check_emblem_change_condition(sd)) {
  1280. return 0;
  1281. }
  1282. return intif_guild_emblem( sd.status.guild_id, len, data );
  1283. }
  1284. /*====================================================
  1285. * Change guild emblem version
  1286. *---------------------------------------------------*/
  1287. int guild_change_emblem_version( map_session_data& sd, int version ){
  1288. if (!guild_check_emblem_change_condition(sd)) {
  1289. return 0;
  1290. }
  1291. return intif_guild_emblem_version( sd.status.guild_id, version );
  1292. }
  1293. /*====================================================
  1294. * Notification of guild emblem changed
  1295. *---------------------------------------------------*/
  1296. int guild_emblem_changed(int len,int guild_id,int emblem_id,const char *data) {
  1297. int i;
  1298. map_session_data *sd;
  1299. auto g = guild_search(guild_id);
  1300. if (!g)
  1301. return 0;
  1302. if (data != nullptr)
  1303. memcpy(g->guild.emblem_data,data,len);
  1304. g->guild.emblem_len=len;
  1305. g->guild.emblem_id=emblem_id;
  1306. for(i=0;i<g->guild.max_member;i++){
  1307. if((sd=g->guild.member[i].sd)!=nullptr){
  1308. sd->guild_emblem_id=emblem_id;
  1309. clif_guild_belonginfo( *sd );
  1310. clif_guild_emblem(*sd, g->guild);
  1311. clif_guild_emblem_area(&sd->bl);
  1312. }
  1313. }
  1314. {// update guardians (mobs)
  1315. for (const auto &it : castle_db) {
  1316. if( it.second->guild_id != guild_id )
  1317. continue;
  1318. // update permanent guardians
  1319. for( i = 0; i < ARRAYLENGTH(it.second->guardian); ++i )
  1320. {
  1321. TBL_MOB* md = (it.second->guardian[i].id ? map_id2md(it.second->guardian[i].id) : nullptr);
  1322. if( md == nullptr || md->guardian_data == nullptr )
  1323. continue;
  1324. md->guardian_data->emblem_id = emblem_id;
  1325. clif_guild_emblem_area(&md->bl);
  1326. }
  1327. // update temporary guardians
  1328. for( i = 0; i < it.second->temp_guardians_max; ++i )
  1329. {
  1330. TBL_MOB* md = (it.second->temp_guardians[i] ? map_id2md(it.second->temp_guardians[i]) : nullptr);
  1331. if( md == nullptr || md->guardian_data == nullptr )
  1332. continue;
  1333. md->guardian_data->emblem_id = emblem_id;
  1334. clif_guild_emblem_area(&md->bl);
  1335. }
  1336. }
  1337. }
  1338. {// update npcs (flags or other npcs that used flagemblem to attach to this guild)
  1339. for( i = 0; i < guild_flags_count; i++ ) {
  1340. if( guild_flags[i] && guild_flags[i]->u.scr.guild_id == guild_id ) {
  1341. clif_guild_emblem_area(&guild_flags[i]->bl);
  1342. }
  1343. }
  1344. }
  1345. return 0;
  1346. }
  1347. /**
  1348. * @see DBCreateData
  1349. */
  1350. static DBData create_expcache(DBKey key, va_list args) {
  1351. struct guild_expcache *c;
  1352. map_session_data *sd = va_arg(args, map_session_data*);
  1353. c = ers_alloc(expcache_ers, struct guild_expcache);
  1354. c->guild_id = sd->status.guild_id;
  1355. c->account_id = sd->status.account_id;
  1356. c->char_id = sd->status.char_id;
  1357. c->exp = 0;
  1358. return db_ptr2data(c);
  1359. }
  1360. /*====================================================
  1361. * Return taxed experience from player sd to guild
  1362. *---------------------------------------------------*/
  1363. t_exp guild_payexp(map_session_data *sd,t_exp exp) {
  1364. struct guild_expcache *c;
  1365. int per;
  1366. nullpo_ret(sd);
  1367. if (!exp) return 0;
  1368. auto &g = sd->guild;
  1369. if (sd->status.guild_id == 0 || !g ||
  1370. (per = guild_getposition(*sd)) < 0 ||
  1371. (per = g->guild.position[per].exp_mode) < 1)
  1372. return 0;
  1373. if (per < 100)
  1374. exp = exp * per / 100;
  1375. //Otherwise tax everything.
  1376. c = (struct guild_expcache *)db_data2ptr(guild_expcache_db->ensure(guild_expcache_db, db_i2key(sd->status.char_id), create_expcache, sd));
  1377. c->exp = util::safe_addition_cap(c->exp, exp, MAX_GUILD_EXP);
  1378. return exp;
  1379. }
  1380. /*====================================================
  1381. * Player sd pay a tribute experience to his guild
  1382. * Add this experience to guild exp
  1383. * [Celest]
  1384. *---------------------------------------------------*/
  1385. t_exp guild_getexp(map_session_data *sd,t_exp exp) {
  1386. struct guild_expcache *c;
  1387. nullpo_ret(sd);
  1388. if (sd->status.guild_id == 0 || sd->guild == nullptr)
  1389. return 0;
  1390. c = (struct guild_expcache *)db_data2ptr(guild_expcache_db->ensure(guild_expcache_db, db_i2key(sd->status.char_id), create_expcache, sd));
  1391. c->exp = util::safe_addition_cap(c->exp, exp, MAX_GUILD_EXP);
  1392. return exp;
  1393. }
  1394. /*====================================================
  1395. * Ask to increase guildskill skill_id
  1396. *---------------------------------------------------*/
  1397. void guild_skillup(map_session_data* sd, uint16 skill_id) {
  1398. short idx = guild_skill_get_index(skill_id);
  1399. short max = 0;
  1400. nullpo_retv(sd);
  1401. if (idx == -1)
  1402. return;
  1403. auto &g = sd->guild;
  1404. if( sd->status.guild_id == 0 || !g || // no guild
  1405. strcmp(sd->status.name, g->guild.master) ) // not the guild master
  1406. return;
  1407. max = guild_skill_get_max(skill_id);
  1408. if( g->guild.skill_point > 0 &&
  1409. g->guild.skill[idx].id != 0 &&
  1410. g->guild.skill[idx].lv < max )
  1411. intif_guild_skillup(g->guild.guild_id, skill_id, sd->status.account_id, max);
  1412. }
  1413. /*====================================================
  1414. * Notification of guildskill skill_id increase request
  1415. *---------------------------------------------------*/
  1416. int guild_skillupack(int guild_id,uint16 skill_id,uint32 account_id) {
  1417. map_session_data *sd = map_id2sd(account_id);
  1418. auto g = guild_search(guild_id);
  1419. int i;
  1420. short idx = guild_skill_get_index(skill_id);
  1421. if (g == nullptr || idx == -1)
  1422. return 0;
  1423. if (sd != nullptr) {
  1424. int lv = g->guild.skill[idx].lv;
  1425. int range = skill_get_range(skill_id, lv);
  1426. clif_skillup(sd,skill_id,lv,range,1);
  1427. /* Guild Aura handling */
  1428. switch( skill_id ) {
  1429. case GD_LEADERSHIP:
  1430. case GD_GLORYWOUNDS:
  1431. case GD_SOULCOLD:
  1432. case GD_HAWKEYES:
  1433. guild_guildaura_refresh(sd,skill_id,g->guild.skill[idx].lv);
  1434. break;
  1435. }
  1436. }
  1437. // Inform all members
  1438. for (i = 0; i < g->guild.max_member; i++)
  1439. if ((sd = g->guild.member[i].sd) != nullptr)
  1440. clif_guild_skillinfo( *sd );
  1441. return 0;
  1442. }
  1443. void guild_guildaura_refresh(map_session_data *sd, uint16 skill_id, uint16 skill_lv) {
  1444. if( !(battle_config.guild_aura&(is_agit_start()?2:1)) &&
  1445. !(battle_config.guild_aura&(map_flag_gvg2(sd->bl.m)?8:4)) )
  1446. return;
  1447. if( !skill_lv )
  1448. return;
  1449. sc_type type = skill_get_sc(skill_id);
  1450. if (type == SC_NONE)
  1451. return;
  1452. status_change_end(&sd->bl, type);
  1453. std::shared_ptr<s_skill_unit_group> group = skill_unitsetting(&sd->bl,skill_id,skill_lv,sd->bl.x,sd->bl.y,0);
  1454. if( group )
  1455. 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
  1456. return;
  1457. }
  1458. /*====================================================
  1459. * Count number of relations the guild has.
  1460. * Flag:
  1461. * 0 = allied
  1462. * 1 = enemy
  1463. *---------------------------------------------------*/
  1464. int guild_get_alliance_count(const struct mmo_guild &g,int flag) {
  1465. int i,c;
  1466. for(i=c=0;i<MAX_GUILDALLIANCE;i++){
  1467. if( g.alliance[i].guild_id>0 &&
  1468. g.alliance[i].opposition==flag )
  1469. c++;
  1470. }
  1471. return c;
  1472. }
  1473. // Blocks all guild skills which have a common delay time.
  1474. void guild_block_skill(map_session_data *sd, int time) {
  1475. uint16 skill_id[] = { GD_BATTLEORDER, GD_REGENERATION, GD_RESTORE, GD_EMERGENCYCALL };
  1476. int i;
  1477. for (i = 0; i < 4; i++)
  1478. skill_blockpc_start(sd, skill_id[i], time);
  1479. }
  1480. /*====================================================
  1481. * Check relation between guild_id1 and guild_id2.
  1482. * Flag:
  1483. * 0 = allied
  1484. * 1 = enemy
  1485. * Returns true if yes.
  1486. *---------------------------------------------------*/
  1487. int guild_check_alliance(int guild_id1, int guild_id2, int flag) {
  1488. int i;
  1489. auto g = guild_search(guild_id1);
  1490. if (g == nullptr)
  1491. return 0;
  1492. ARR_FIND( 0, MAX_GUILDALLIANCE, i, g->guild.alliance[i].guild_id == guild_id2 && g->guild.alliance[i].opposition == flag );
  1493. return( i < MAX_GUILDALLIANCE ) ? 1 : 0;
  1494. }
  1495. /*====================================================
  1496. * Player sd, asking player tsd an alliance between their 2 guilds
  1497. *---------------------------------------------------*/
  1498. int guild_reqalliance(map_session_data *sd,map_session_data *tsd) {
  1499. int i;
  1500. if(is_agit_start()) { // Disable alliance creation during woe [Valaris]
  1501. clif_displaymessage(sd->fd,msg_txt(sd,676)); //"Alliances cannot be made during Guild Wars!"
  1502. return 0;
  1503. } // end addition [Valaris]
  1504. nullpo_ret(sd);
  1505. if(tsd==nullptr || tsd->status.guild_id<=0)
  1506. return 0;
  1507. // Check, is tsd guild master, if not - cancel alliance. [f0und3r]
  1508. if (battle_config.guild_alliance_onlygm && !tsd->state.gmaster_flag) {
  1509. clif_guild_allianceack(*sd, 5);
  1510. return 0;
  1511. }
  1512. auto &g = sd->guild;
  1513. auto &tg = tsd->guild;
  1514. if(g==nullptr || tg==nullptr)
  1515. return 0;
  1516. // Prevent creation alliance with same guilds [LuzZza]
  1517. if(sd->status.guild_id == tsd->status.guild_id)
  1518. return 0;
  1519. if( guild_get_alliance_count(g->guild,0) >= battle_config.max_guild_alliance ) {
  1520. clif_guild_allianceack(*sd,4);
  1521. return 0;
  1522. }
  1523. if( guild_get_alliance_count(tg->guild,0) >= battle_config.max_guild_alliance ) {
  1524. clif_guild_allianceack(*sd,3);
  1525. return 0;
  1526. }
  1527. if( tsd->guild_alliance>0 ){
  1528. clif_guild_allianceack(*sd,1);
  1529. return 0;
  1530. }
  1531. for (i = 0; i < MAX_GUILDALLIANCE; i++) { // check if already allied
  1532. if( g->guild.alliance[i].guild_id==tsd->status.guild_id &&
  1533. g->guild.alliance[i].opposition==0){
  1534. clif_guild_allianceack(*sd,0);
  1535. return 0;
  1536. }
  1537. }
  1538. tsd->guild_alliance=sd->status.guild_id;
  1539. tsd->guild_alliance_account=sd->status.account_id;
  1540. clif_guild_reqalliance(*tsd,sd->status.account_id,g->guild.name);
  1541. return 0;
  1542. }
  1543. /*====================================================
  1544. * Player sd, answer to player tsd (account_id) for an alliance request
  1545. *---------------------------------------------------*/
  1546. int guild_reply_reqalliance(map_session_data *sd,uint32 account_id,int flag) {
  1547. map_session_data *tsd;
  1548. nullpo_ret(sd);
  1549. tsd= map_id2sd( account_id );
  1550. if (!tsd) { //Character left? Cancel alliance.
  1551. clif_guild_allianceack(*sd,3);
  1552. return 0;
  1553. }
  1554. if (sd->guild_alliance != tsd->status.guild_id) // proposed guild_id alliance doesn't match tsd guildid
  1555. return 0;
  1556. if (flag == 1) { // consent
  1557. int i;
  1558. auto &g = sd->guild;
  1559. auto &tg = tsd->guild;
  1560. if (!g || guild_get_alliance_count(g->guild,0) >= battle_config.max_guild_alliance) {
  1561. clif_guild_allianceack(*sd,4);
  1562. clif_guild_allianceack(*tsd,3);
  1563. return 0;
  1564. }
  1565. if (!tg || guild_get_alliance_count(tg->guild,0) >= battle_config.max_guild_alliance) {
  1566. clif_guild_allianceack(*sd,3);
  1567. clif_guild_allianceack(*tsd,4);
  1568. return 0;
  1569. }
  1570. for(i=0;i<MAX_GUILDALLIANCE;i++){
  1571. if(g->guild.alliance[i].guild_id==tsd->status.guild_id &&
  1572. g->guild.alliance[i].opposition==1)
  1573. intif_guild_alliance( sd->status.guild_id,tsd->status.guild_id,
  1574. sd->status.account_id,tsd->status.account_id,9 );
  1575. }
  1576. for(i=0;i<MAX_GUILDALLIANCE;i++){
  1577. if(tg->guild.alliance[i].guild_id==sd->status.guild_id &&
  1578. tg->guild.alliance[i].opposition==1)
  1579. intif_guild_alliance( tsd->status.guild_id,sd->status.guild_id,
  1580. tsd->status.account_id,sd->status.account_id,9 );
  1581. }
  1582. // inform other servers
  1583. intif_guild_alliance( sd->status.guild_id,tsd->status.guild_id,
  1584. sd->status.account_id,tsd->status.account_id,0 );
  1585. return 0;
  1586. } else { // deny
  1587. sd->guild_alliance=0;
  1588. sd->guild_alliance_account=0;
  1589. if(tsd!=nullptr)
  1590. clif_guild_allianceack(*tsd,3);
  1591. }
  1592. return 0;
  1593. }
  1594. /*====================================================
  1595. * Player sd asking to break alliance with guild guild_id
  1596. *---------------------------------------------------*/
  1597. int guild_delalliance(map_session_data *sd,int guild_id,int flag) {
  1598. nullpo_ret(sd);
  1599. if(is_agit_start()) { // Disable alliance breaking during woe [Valaris]
  1600. clif_displaymessage(sd->fd,msg_txt(sd,677)); //"Alliances cannot be broken during Guild Wars!"
  1601. return 0;
  1602. } // end addition [Valaris]
  1603. intif_guild_alliance( sd->status.guild_id,guild_id,sd->status.account_id,0,flag|8 );
  1604. return 0;
  1605. }
  1606. /*====================================================
  1607. * Player sd, asking player tsd a formal enemy relation between their 2 guilds
  1608. *---------------------------------------------------*/
  1609. int guild_opposition(map_session_data *sd,map_session_data *tsd) {
  1610. int i;
  1611. nullpo_ret(sd);
  1612. auto &g = sd->guild;
  1613. if(g==nullptr || tsd==nullptr)
  1614. return 0;
  1615. // Prevent creation opposition with same guilds [LuzZza]
  1616. if(sd->status.guild_id == tsd->status.guild_id)
  1617. return 0;
  1618. if( guild_get_alliance_count(g->guild,1) >= battle_config.max_guild_alliance ) {
  1619. clif_guild_oppositionack(*sd,1);
  1620. return 0;
  1621. }
  1622. for (i = 0; i < MAX_GUILDALLIANCE; i++) { // checking relations
  1623. if(g->guild.alliance[i].guild_id==tsd->status.guild_id){
  1624. if (g->guild.alliance[i].opposition == 1) { // check if not already hostile
  1625. clif_guild_oppositionack(*sd,2);
  1626. return 0;
  1627. }
  1628. if(is_agit_start()) // Prevent the changing of alliances to oppositions during WoE.
  1629. return 0;
  1630. //Change alliance to opposition.
  1631. intif_guild_alliance( sd->status.guild_id,tsd->status.guild_id,
  1632. sd->status.account_id,tsd->status.account_id,8 );
  1633. }
  1634. }
  1635. // inform other serv
  1636. intif_guild_alliance( sd->status.guild_id,tsd->status.guild_id,
  1637. sd->status.account_id,tsd->status.account_id,1 );
  1638. return 0;
  1639. }
  1640. /*====================================================
  1641. * Notification of a relationship between 2 guilds
  1642. *---------------------------------------------------*/
  1643. int guild_allianceack(int guild_id1,int guild_id2,uint32 account_id1,uint32 account_id2,int flag,const char *name1,const char *name2)
  1644. {
  1645. struct mmo_guild *g[2];
  1646. int guild_id[2];
  1647. const char *guild_name[2];
  1648. map_session_data *sd[2];
  1649. int j,i;
  1650. guild_id[0] = guild_id1;
  1651. guild_id[1] = guild_id2;
  1652. guild_name[0] = name1;
  1653. guild_name[1] = name2;
  1654. sd[0] = map_id2sd(account_id1);
  1655. sd[1] = map_id2sd(account_id2);
  1656. g[0] = &guild_search(guild_id1)->guild;
  1657. g[1] = &guild_search(guild_id2)->guild;
  1658. if(sd[0]!=nullptr && (flag&0x0f)==0){
  1659. sd[0]->guild_alliance=0;
  1660. sd[0]->guild_alliance_account=0;
  1661. }
  1662. if (flag & 0x70) { // failure
  1663. for(i=0;i<2-(flag&1);i++)
  1664. if( sd[i]!=nullptr )
  1665. clif_guild_allianceack(*sd[i],((flag>>4)==i+1)?3:4);
  1666. return 0;
  1667. }
  1668. if (!(flag & 0x08)) { // new relationship
  1669. for(i=0;i<2-(flag&1);i++) {
  1670. if(g[i]!=nullptr) {
  1671. ARR_FIND( 0, MAX_GUILDALLIANCE, j, g[i]->alliance[j].guild_id == 0 );
  1672. if( j < MAX_GUILDALLIANCE ) {
  1673. g[i]->alliance[j].guild_id=guild_id[1-i];
  1674. memcpy(g[i]->alliance[j].name,guild_name[1-i],NAME_LENGTH);
  1675. g[i]->alliance[j].opposition=flag&1;
  1676. }
  1677. }
  1678. }
  1679. } else { // remove relationship
  1680. for(i=0;i<2-(flag&1);i++) {
  1681. if(g[i]!=nullptr) {
  1682. for(j=0;j<g[i]->max_member;j++) channel_pcquit(g[i]->member[j].sd,2); //leave all alliance chan
  1683. ARR_FIND( 0, MAX_GUILDALLIANCE, j, g[i]->alliance[j].guild_id == guild_id[1-i] && g[i]->alliance[j].opposition == (flag&1) );
  1684. if( j < MAX_GUILDALLIANCE )
  1685. g[i]->alliance[j].guild_id = 0;
  1686. }
  1687. if (sd[i] != nullptr) // notify players
  1688. clif_guild_delalliance(*sd[i],guild_id[1-i],(flag&1));
  1689. }
  1690. }
  1691. if ((flag & 0x0f) == 0) { // alliance notification
  1692. if( sd[1]!=nullptr )
  1693. clif_guild_allianceack(*sd[1],2);
  1694. } else if ((flag & 0x0f) == 1) { // enemy notification
  1695. if( sd[0]!=nullptr )
  1696. clif_guild_oppositionack(*sd[0],0);
  1697. }
  1698. for (i = 0; i < 2 - (flag & 1); i++) { // Retransmission of the relationship list to all members
  1699. if(g[i]!=nullptr)
  1700. for(j=0;j<g[i]->max_member;j++) {
  1701. map_session_data *sd_mem = g[i]->member[j].sd;
  1702. if( sd_mem!=nullptr){
  1703. clif_guild_allianceinfo(*sd_mem);
  1704. // join ally channel
  1705. if( channel_config.ally_tmpl.name[0] && (channel_config.ally_tmpl.opt&CHAN_OPT_AUTOJOIN) ) {
  1706. channel_gjoin(sd_mem,2);
  1707. }
  1708. }
  1709. }
  1710. }
  1711. return 0;
  1712. }
  1713. /**
  1714. * Notification for the guild disbanded
  1715. * @see DBApply
  1716. */
  1717. int guild_broken_sub(struct mmo_guild &g, int guild_id) {
  1718. for (int i = 0; i < MAX_GUILDALLIANCE; i++) { // Destroy all relationships
  1719. if (g.alliance[i].guild_id == guild_id) {
  1720. for (int j = 0; j < g.max_member; j++) {
  1721. if (g.member[j].sd)
  1722. clif_guild_delalliance(*g.member[j].sd, guild_id, g.alliance[i].opposition);
  1723. }
  1724. intif_guild_alliance(g.guild_id, guild_id, 0, 0, g.alliance[i].opposition | 8);
  1725. g.alliance[i].guild_id = 0;
  1726. }
  1727. }
  1728. return 0;
  1729. }
  1730. /**
  1731. * Invoked on Castles when a guild is broken. [Skotlex]
  1732. * @see DBApply
  1733. */
  1734. void castle_guild_broken_sub(int guild_id) {
  1735. for (const auto &it : castle_db) {
  1736. std::shared_ptr<guild_castle> gc = it.second;
  1737. if (gc->guild_id == guild_id) {
  1738. char name[EVENT_NAME_LENGTH];
  1739. // We call castle_event::OnGuildBreak of all castles of the guild
  1740. // You can set all castle_events in the 'db/castle_db.txt'
  1741. safesnprintf(name, EVENT_NAME_LENGTH, "%s::%s", gc->castle_event, script_config.guild_break_event_name);
  1742. npc_event_do(name);
  1743. //Save the new 'owner', this should invoke guardian clean up and other such things.
  1744. guild_castledatasave(gc->castle_id, CD_GUILD_ID, 0);
  1745. }
  1746. }
  1747. }
  1748. //Invoked on /breakguild "Guild name"
  1749. int guild_broken(int guild_id,int flag) {
  1750. auto g = guild_search(guild_id);
  1751. int i;
  1752. if (flag != 0 || g == nullptr)
  1753. return 0;
  1754. for (i = 0; i < g->guild.max_member; i++){ // Destroy all relationships
  1755. map_session_data *sd = g->guild.member[i].sd;
  1756. if(sd != nullptr){
  1757. if(sd->state.storage_flag == 2)
  1758. storage_guild_storage_quit(sd,1);
  1759. sd->status.guild_id=0;
  1760. sd->guild = nullptr;
  1761. sd->state.gmaster_flag = 0;
  1762. clif_guild_broken( *sd, 0 );
  1763. clif_name_area(&sd->bl); // [LuzZza]
  1764. status_change_end(&sd->bl,SC_LEADERSHIP);
  1765. status_change_end(&sd->bl,SC_GLORYWOUNDS);
  1766. status_change_end(&sd->bl,SC_SOULCOLD);
  1767. status_change_end(&sd->bl,SC_HAWKEYES);
  1768. status_change_end(&sd->bl,SC_EMERGENCY_MOVE);
  1769. }
  1770. }
  1771. for (auto &it : guild_db) {
  1772. if (!it.second)
  1773. continue;
  1774. guild_broken_sub(it.second->guild, guild_id);
  1775. }
  1776. castle_guild_broken_sub(guild_id);
  1777. storage_guild_delete(guild_id);
  1778. if( channel_config.ally_tmpl.name[0] ) {
  1779. channel_delete(g->channel,false);
  1780. }
  1781. guild_db.erase(guild_id);
  1782. return 0;
  1783. }
  1784. /** Changes the Guild Master to the specified player. [Skotlex]
  1785. * @param guild_id
  1786. * @param sd New guild master
  1787. */
  1788. bool guild_gm_change( int guild_id, uint32 char_id, bool showMessage ){
  1789. auto g = guild_search( guild_id );
  1790. if( g == nullptr ){
  1791. return false;
  1792. }
  1793. if( g->instance_id > 0 && battle_config.instance_block_leaderchange ){
  1794. return false;
  1795. }
  1796. int i;
  1797. ARR_FIND( 0, MAX_GUILD, i, g->guild.member[i].char_id == char_id );
  1798. if( i == MAX_GUILD ){
  1799. // Not part of the guild
  1800. return false;
  1801. }
  1802. char* name = g->guild.member[i].name;
  1803. // Nothing to change.
  1804. if( strcmp( g->guild.master, name ) == 0 ){
  1805. return false;
  1806. }
  1807. //Notify servers that master has changed.
  1808. return intif_guild_change_gm( guild_id, name, strlen( name ) + 1 );
  1809. }
  1810. /** Notification from Char server that a guild's master has changed. [Skotlex]
  1811. * @param guild_id
  1812. * @param account_id
  1813. * @param char_id
  1814. */
  1815. int guild_gm_changed(int guild_id, uint32 account_id, uint32 char_id, time_t time) {
  1816. struct guild_member gm;
  1817. int pos, i;
  1818. auto g = guild_search(guild_id);
  1819. if (!g)
  1820. return 0;
  1821. for(pos=0; pos<g->guild.max_member && !(
  1822. g->guild.member[pos].account_id==account_id &&
  1823. g->guild.member[pos].char_id==char_id);
  1824. pos++);
  1825. if (pos == 0 || pos == g->guild.max_member) return 0;
  1826. memcpy(&gm, &g->guild.member[pos], sizeof (struct guild_member));
  1827. memcpy(&g->guild.member[pos], &g->guild.member[0], sizeof(struct guild_member));
  1828. memcpy(&g->guild.member[0], &gm, sizeof(struct guild_member));
  1829. g->guild.member[pos].position = g->guild.member[0].position;
  1830. g->guild.member[0].position = 0; //Position 0: guild Master.
  1831. strcpy(g->guild.master, g->guild.member[0].name);
  1832. if (g->guild.member[pos].sd && g->guild.member[pos].sd->fd) {
  1833. clif_displaymessage(g->guild.member[pos].sd->fd, msg_txt(g->guild.member[pos].sd,678)); //"You no longer are the Guild Master."
  1834. g->guild.member[pos].sd->state.gmaster_flag = 0;
  1835. clif_name_area(&g->guild.member[pos].sd->bl);
  1836. }
  1837. if (g->guild.member[0].sd && g->guild.member[0].sd->fd) {
  1838. clif_displaymessage(g->guild.member[0].sd->fd, msg_txt(g->guild.member[pos].sd,679)); //"You have become the Guild Master!"
  1839. g->guild.member[0].sd->state.gmaster_flag = 1;
  1840. clif_name_area(&g->guild.member[0].sd->bl);
  1841. //Block his skills to prevent abuse.
  1842. #ifndef RENEWAL
  1843. if (battle_config.guild_skill_relog_delay)
  1844. guild_block_skill(g->guild.member[0].sd, battle_config.guild_skill_relog_delay);
  1845. #endif
  1846. }
  1847. // announce the change to all guild members
  1848. for( i = 0; i < g->guild.max_member; i++ ) {
  1849. if( g->guild.member[i].sd ){
  1850. clif_guild_basicinfo( *g->guild.member[i].sd );
  1851. clif_guild_memberlist( *g->guild.member[i].sd );
  1852. clif_guild_belonginfo( *g->guild.member[i].sd ); // Update clientside guildmaster flag
  1853. }
  1854. }
  1855. // Store changing time
  1856. g->guild.last_leader_change = time;
  1857. return 1;
  1858. }
  1859. /** Disband a guild
  1860. * @param sd Player who breaks the guild
  1861. * @param name Guild name
  1862. */
  1863. int guild_break( map_session_data& sd, const char* name ){
  1864. struct unit_data *ud;
  1865. int i;
  1866. #ifdef BOUND_ITEMS
  1867. int j;
  1868. int idxlist[MAX_INVENTORY];
  1869. #endif
  1870. auto& g = sd.guild;
  1871. if( g == nullptr ){
  1872. return 0;
  1873. }
  1874. if (strcmp(g->guild.name,name) != 0)
  1875. return 0;
  1876. if( !sd.state.gmaster_flag ){
  1877. return 0;
  1878. }
  1879. for (i = 0; i < g->guild.max_member; i++) {
  1880. if( g->guild.member[i].account_id>0 && (
  1881. g->guild.member[i].account_id != sd.status.account_id ||
  1882. g->guild.member[i].char_id != sd.status.char_id ) )
  1883. break;
  1884. }
  1885. if (i < g->guild.max_member) {
  1886. clif_guild_broken(sd,2);
  1887. return 0;
  1888. }
  1889. // Guild locked.
  1890. if( map_getmapflag( sd.bl.m, MF_GUILDLOCK ) ){
  1891. clif_displaymessage( sd.fd, msg_txt( &sd, 228 ) );
  1892. return 0;
  1893. }
  1894. if( g->instance_id ){
  1895. if( battle_config.instance_block_leave ){
  1896. return 0;
  1897. }
  1898. instance_destroy(g->instance_id);
  1899. }
  1900. /* Regardless of char server allowing it, we clear the guild master's auras */
  1901. if( ( ud = unit_bl2ud( &sd.bl ) ) ){
  1902. std::vector<std::shared_ptr<s_skill_unit_group>> group;
  1903. for (const auto su : ud->skillunits) {
  1904. switch (su->skill_id) {
  1905. case GD_LEADERSHIP:
  1906. case GD_GLORYWOUNDS:
  1907. case GD_SOULCOLD:
  1908. case GD_HAWKEYES:
  1909. group.push_back(su);
  1910. break;
  1911. }
  1912. }
  1913. for (auto it = group.begin(); it != group.end(); it++) {
  1914. skill_delunitgroup(*it);
  1915. }
  1916. }
  1917. #ifdef BOUND_ITEMS
  1918. //Guild bound item check - Removes the bound flag
  1919. j = pc_bound_chk( &sd, BOUND_GUILD, idxlist );
  1920. for(i = 0; i < j; i++)
  1921. pc_delitem( &sd,idxlist[i], sd.inventory.u.items_inventory[idxlist[i]].amount, 0, 1, LOG_TYPE_BOUND_REMOVAL );
  1922. #endif
  1923. intif_guild_break(g->guild.guild_id);
  1924. return 1;
  1925. }
  1926. /**
  1927. * Creates a list of guild castle IDs to be requested
  1928. * from char-server.
  1929. */
  1930. void guild_castle_map_init(void) {
  1931. std::vector<int32> castle_ids;
  1932. for( const auto &it : castle_db ){
  1933. castle_ids.push_back( it.first );
  1934. }
  1935. if( !castle_ids.empty() && intif_guild_castle_dataload( castle_ids ) ){
  1936. ShowStatus( "Requested '" CL_WHITE "%" PRIdPTR CL_RESET "' guild castles from char-server...\n", castle_ids.size() );
  1937. }
  1938. }
  1939. /**
  1940. * Setter function for members of guild_castle struct.
  1941. * Handles all side-effects, like updating guardians.
  1942. * Sends updated info to char-server for saving.
  1943. * @param castle_id Castle ID
  1944. * @param index Type of data to change
  1945. * @param value New value
  1946. */
  1947. int guild_castledatasave(int castle_id, int index, int value) {
  1948. std::shared_ptr<guild_castle> gc = castle_db.find(castle_id);
  1949. if (gc == nullptr) {
  1950. ShowWarning("guild_castledatasave: guild castle '%d' not found\n", castle_id);
  1951. return 0;
  1952. }
  1953. switch (index) {
  1954. case CD_GUILD_ID: // The castle's owner has changed? Update or remove Guardians too. [Skotlex]
  1955. {
  1956. int i;
  1957. gc->guild_id = value;
  1958. for (i = 0; i < MAX_GUARDIANS; i++){
  1959. struct mob_data *gd;
  1960. if (gc->guardian[i].visible && (gd = map_id2md(gc->guardian[i].id)) != nullptr)
  1961. mob_guardian_guildchange(gd);
  1962. }
  1963. break;
  1964. }
  1965. case CD_CURRENT_ECONOMY:
  1966. gc->economy = value; break;
  1967. case CD_CURRENT_DEFENSE: // defense invest change -> recalculate guardian hp
  1968. {
  1969. int i;
  1970. gc->defense = value;
  1971. for (i = 0; i < MAX_GUARDIANS; i++){
  1972. struct mob_data *gd;
  1973. if (gc->guardian[i].visible && (gd = map_id2md(gc->guardian[i].id)) != nullptr)
  1974. status_calc_mob(gd, SCO_NONE);
  1975. }
  1976. break;
  1977. }
  1978. case CD_INVESTED_ECONOMY:
  1979. gc->triggerE = value; break;
  1980. case CD_INVESTED_DEFENSE:
  1981. gc->triggerD = value; break;
  1982. case CD_NEXT_TIME:
  1983. gc->nextTime = value; break;
  1984. case CD_PAY_TIME:
  1985. gc->payTime = value; break;
  1986. case CD_CREATE_TIME:
  1987. gc->createTime = value; break;
  1988. case CD_ENABLED_KAFRA:
  1989. gc->visibleC = value; break;
  1990. default:
  1991. if (index >= CD_ENABLED_GUARDIAN00 && index < CD_MAX) {
  1992. gc->guardian[index - CD_ENABLED_GUARDIAN00].visible = value;
  1993. break;
  1994. }
  1995. ShowWarning("guild_castledatasave: index = '%d' is out of allowed range\n", index);
  1996. return 0;
  1997. }
  1998. if (!intif_guild_castle_datasave(castle_id, index, value)) {
  1999. guild_castle_reconnect(castle_id, index, value);
  2000. }
  2001. return 0;
  2002. }
  2003. void guild_castle_reconnect_sub(void *key, void *data, va_list ap) {
  2004. int castle_id = GetWord((int)__64BPRTSIZE(key), 0);
  2005. int index = GetWord((int)__64BPRTSIZE(key), 1);
  2006. intif_guild_castle_datasave(castle_id, index, *(int *)data);
  2007. aFree(data);
  2008. }
  2009. /**
  2010. * Saves pending guild castle data changes when char-server is
  2011. * disconnected.
  2012. * On reconnect pushes all changes to char-server for saving.
  2013. * @param castle_id
  2014. * @param index
  2015. * @param value
  2016. */
  2017. void guild_castle_reconnect(int castle_id, int index, int value) {
  2018. static struct linkdb_node *gc_save_pending = nullptr;
  2019. if (castle_id < 0) { // char-server reconnected
  2020. linkdb_foreach(&gc_save_pending, guild_castle_reconnect_sub);
  2021. linkdb_final(&gc_save_pending);
  2022. } else {
  2023. int *data;
  2024. CREATE(data, int, 1);
  2025. *data = value;
  2026. linkdb_replace(&gc_save_pending, (void*)__64BPRTSIZE((MakeDWord(castle_id, index))), data);
  2027. }
  2028. }
  2029. /** Load castle data then invoke OnAgitInit* on last
  2030. * @param len
  2031. * @param gc Guild Castle data
  2032. */
  2033. int guild_castledataloadack(int len, struct guild_castle *gc) {
  2034. int i;
  2035. int n = (len-4) / sizeof(struct guild_castle);
  2036. int ev;
  2037. nullpo_ret(gc);
  2038. //Last owned castle in the list invokes ::OnAgitInit
  2039. for( i = n-1; i >= 0 && !(gc[i].guild_id); --i );
  2040. ev = i; // offset of castle or -1
  2041. if( ev < 0 ) { //No castles owned, invoke OnAgitInit as it is.
  2042. npc_event_doall( script_config.agit_init_event_name );
  2043. npc_event_doall( script_config.agit_init2_event_name );
  2044. npc_event_doall( script_config.agit_init3_event_name );
  2045. } else // load received castles into memory, one by one
  2046. for( i = 0; i < n; i++, gc++ ) {
  2047. std::shared_ptr<guild_castle> c = castle_db.find(gc->castle_id);
  2048. if (c == nullptr) {
  2049. ShowError("guild_castledataloadack: castle id=%d not found.\n", gc->castle_id);
  2050. continue;
  2051. }
  2052. // update map-server castle data with new info
  2053. memcpy(&c->guild_id, &gc->guild_id, sizeof(struct guild_castle) - offsetof(struct guild_castle, guild_id));
  2054. if( c->guild_id ) {
  2055. if( i != ev )
  2056. guild_request_info(c->guild_id);
  2057. else { // last owned one
  2058. char event_name[EVENT_NAME_LENGTH];
  2059. snprintf( event_name, EVENT_NAME_LENGTH, "::%s", script_config.agit_init_event_name );
  2060. guild_npc_request_info(c->guild_id, event_name);
  2061. snprintf( event_name, EVENT_NAME_LENGTH, "::%s", script_config.agit_init2_event_name );
  2062. guild_npc_request_info(c->guild_id, event_name);
  2063. snprintf( event_name, EVENT_NAME_LENGTH, "::%s", script_config.agit_init3_event_name );
  2064. guild_npc_request_info(c->guild_id, event_name);
  2065. }
  2066. }
  2067. }
  2068. ShowStatus("Received '" CL_WHITE "%d" CL_RESET "' guild castles from char-server.\n", n);
  2069. return 0;
  2070. }
  2071. /**
  2072. * Start WoE:FE and triggers all npc OnAgitStart
  2073. */
  2074. bool guild_agit_start(void){
  2075. if( agit_flag ){
  2076. return false;
  2077. }
  2078. agit_flag = true;
  2079. npc_event_runall( script_config.agit_start_event_name );
  2080. return true;
  2081. }
  2082. /**
  2083. * End WoE:FE and triggers all npc OnAgitEnd
  2084. */
  2085. bool guild_agit_end(void){
  2086. if( !agit_flag ){
  2087. return false;
  2088. }
  2089. agit_flag = false;
  2090. npc_event_runall( script_config.agit_end_event_name );
  2091. return true;
  2092. }
  2093. /**
  2094. * Start WoE:SE and triggers all npc OnAgitStart2
  2095. */
  2096. bool guild_agit2_start(void){
  2097. if( agit2_flag ){
  2098. return false;
  2099. }
  2100. agit2_flag = true;
  2101. npc_event_runall( script_config.agit_start2_event_name );
  2102. return true;
  2103. }
  2104. /**
  2105. * End WoE:SE and triggers all npc OnAgitEnd2
  2106. */
  2107. bool guild_agit2_end(void){
  2108. if( !agit2_flag ){
  2109. return false;
  2110. }
  2111. agit2_flag = false;
  2112. npc_event_runall( script_config.agit_end2_event_name );
  2113. return true;
  2114. }
  2115. /**
  2116. * Start WoE:TE and triggers all npc OnAgitStart3
  2117. */
  2118. bool guild_agit3_start(void){
  2119. if( agit3_flag ){
  2120. return false;
  2121. }
  2122. agit3_flag = true;
  2123. npc_event_runall( script_config.agit_start3_event_name );
  2124. return true;
  2125. }
  2126. /**
  2127. * End WoE:TE and triggers all npc OnAgitEnd3
  2128. */
  2129. bool guild_agit3_end(void){
  2130. if( !agit3_flag ){
  2131. return false;
  2132. }
  2133. agit3_flag = false;
  2134. npc_event_runall( script_config.agit_end3_event_name );
  2135. return true;
  2136. }
  2137. // How many castles does this guild have?
  2138. int guild_checkcastles(const struct mmo_guild &g) {
  2139. int nb_cas = 0;
  2140. for (const auto &it : castle_db) {
  2141. if (it.second->guild_id == g.guild_id)
  2142. nb_cas++;
  2143. }
  2144. return nb_cas;
  2145. }
  2146. // Are these two guilds allied?
  2147. bool guild_isallied(int guild_id, int guild_id2) {
  2148. int i;
  2149. auto g = guild_search(guild_id);
  2150. if (!g)
  2151. return false;
  2152. ARR_FIND( 0, MAX_GUILDALLIANCE, i, g->guild.alliance[i].guild_id == guild_id2 );
  2153. return( i < MAX_GUILDALLIANCE && g->guild.alliance[i].opposition == 0 );
  2154. }
  2155. bool guild_has_permission( map_session_data& sd, enum e_guild_permission permission ){
  2156. int position = guild_getposition( sd );
  2157. if( position < 0 ){
  2158. return false;
  2159. }
  2160. return ( sd.guild->guild.position[position].mode & permission ) != 0;
  2161. }
  2162. void guild_flag_add(struct npc_data *nd) {
  2163. int i;
  2164. /* check */
  2165. for( i = 0; i < guild_flags_count; i++ ) {
  2166. if( guild_flags[i] && guild_flags[i]->bl.id == nd->bl.id ) {
  2167. return;/* exists, most likely updated the id. */
  2168. }
  2169. }
  2170. i = guild_flags_count;/* save the current slot */
  2171. /* add */
  2172. RECREATE(guild_flags,struct npc_data*,++guild_flags_count);
  2173. /* save */
  2174. guild_flags[i] = nd;
  2175. }
  2176. void guild_flag_remove(struct npc_data *nd) {
  2177. int i, cursor;
  2178. if( guild_flags_count == 0 )
  2179. return;
  2180. /* find it */
  2181. for( i = 0; i < guild_flags_count; i++ ) {
  2182. if( guild_flags[i] && guild_flags[i]->bl.id == nd->bl.id ) {/* found */
  2183. guild_flags[i] = nullptr;
  2184. break;
  2185. }
  2186. }
  2187. /* compact list */
  2188. for( i = 0, cursor = 0; i < guild_flags_count; i++ ) {
  2189. if( guild_flags[i] == nullptr )
  2190. continue;
  2191. if( cursor != i ) {
  2192. memmove(&guild_flags[cursor], &guild_flags[i], sizeof(struct npc_data*));
  2193. }
  2194. cursor++;
  2195. }
  2196. }
  2197. /**
  2198. * @see DBApply
  2199. */
  2200. static int eventlist_db_final(DBKey key, DBData *data, va_list ap) {
  2201. struct eventlist *next = nullptr;
  2202. struct eventlist *current = (struct eventlist *)db_data2ptr(data);
  2203. while (current != nullptr) {
  2204. next = current->next;
  2205. aFree(current);
  2206. current = next;
  2207. }
  2208. return 0;
  2209. }
  2210. /**
  2211. * @see DBApply
  2212. */
  2213. static int guild_expcache_db_final(DBKey key, DBData *data, va_list ap) {
  2214. ers_free(expcache_ers, db_data2ptr(data));
  2215. return 0;
  2216. }
  2217. /* called when scripts are reloaded/unloaded */
  2218. void guild_flags_clear(void) {
  2219. int i;
  2220. for( i = 0; i < guild_flags_count; i++ ) {
  2221. if( guild_flags[i] )
  2222. guild_flags[i] = nullptr;
  2223. }
  2224. guild_flags_count = 0;
  2225. }
  2226. void do_init_guild(void) {
  2227. guild_expcache_db = idb_alloc(DB_OPT_BASE);
  2228. guild_infoevent_db = idb_alloc(DB_OPT_BASE);
  2229. expcache_ers = ers_new(sizeof(struct guild_expcache),"guild.cpp::expcache_ers",ERS_OPT_NONE);
  2230. guild_flags_count = 0;
  2231. castle_db.load();
  2232. guild_skill_tree_db.load();
  2233. add_timer_func_list(guild_payexp_timer,"guild_payexp_timer");
  2234. add_timer_func_list(guild_send_xy_timer, "guild_send_xy_timer");
  2235. add_timer_interval(gettick()+GUILD_PAYEXP_INTERVAL,guild_payexp_timer,0,0,GUILD_PAYEXP_INTERVAL);
  2236. add_timer_interval(gettick()+GUILD_SEND_XY_INTERVAL,guild_send_xy_timer,0,0,GUILD_SEND_XY_INTERVAL);
  2237. }
  2238. void do_final_guild(void) {
  2239. for (auto &it : guild_db) {
  2240. if (!it.second)
  2241. continue;
  2242. channel_delete(it.second->channel, false);
  2243. }
  2244. guild_db.clear();
  2245. castle_db.clear();
  2246. guild_expcache_db->destroy(guild_expcache_db,guild_expcache_db_final);
  2247. guild_infoevent_db->destroy(guild_infoevent_db,eventlist_db_final);
  2248. ers_destroy(expcache_ers);
  2249. aFree(guild_flags);/* never empty; created on boot */
  2250. }