guild.cpp 69 KB

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