guild.cpp 70 KB

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