battleground.cpp 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617
  1. // Copyright (c) rAthena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #include "battleground.hpp"
  4. #include <unordered_map>
  5. #include <common/cbasetypes.hpp>
  6. #include <common/malloc.hpp>
  7. #include <common/nullpo.hpp>
  8. #include <common/random.hpp>
  9. #include <common/showmsg.hpp>
  10. #include <common/strlib.hpp>
  11. #include <common/timer.hpp>
  12. #include <common/utilities.hpp>
  13. #include "battle.hpp"
  14. #include "clif.hpp"
  15. #include "guild.hpp"
  16. #include "homunculus.hpp"
  17. #include "mapreg.hpp"
  18. #include "mercenary.hpp"
  19. #include "mob.hpp"
  20. #include "npc.hpp"
  21. #include "party.hpp"
  22. #include "pc.hpp"
  23. #include "pet.hpp"
  24. using namespace rathena;
  25. BattlegroundDatabase battleground_db;
  26. std::unordered_map<int, std::shared_ptr<s_battleground_data>> bg_team_db;
  27. std::vector<std::shared_ptr<s_battleground_queue>> bg_queues;
  28. int bg_queue_count = 1;
  29. const std::string BattlegroundDatabase::getDefaultLocation() {
  30. return std::string(db_path) + "/battleground_db.yml";
  31. }
  32. /**
  33. * Reads and parses an entry from the battleground_db
  34. * @param node: The YAML node containing the entry
  35. * @return count of successfully parsed rows
  36. */
  37. uint64 BattlegroundDatabase::parseBodyNode(const ryml::NodeRef& node) {
  38. uint32 id;
  39. if (!this->asUInt32(node, "Id", id))
  40. return 0;
  41. std::shared_ptr<s_battleground_type> bg = this->find(id);
  42. bool exists = bg != nullptr;
  43. if (!exists) {
  44. if (!this->nodesExist(node, { "Name", "Locations" }))
  45. return 0;
  46. bg = std::make_shared<s_battleground_type>();
  47. bg->id = id;
  48. }
  49. if (this->nodeExists(node, "Name")) {
  50. std::string name;
  51. if (!this->asString(node, "Name", name))
  52. return 0;
  53. name.resize(NAME_LENGTH);
  54. bg->name = name;
  55. }
  56. if (this->nodeExists(node, "MinPlayers")) {
  57. int min;
  58. if (!this->asInt32(node, "MinPlayers", min))
  59. return 0;
  60. if (min < 1) {
  61. this->invalidWarning(node["MinPlayers"], "Minimum players %d cannot be less than 1, capping to 1.\n", min);
  62. min = 1;
  63. }
  64. if (min * 2 > MAX_BG_MEMBERS) {
  65. this->invalidWarning(node["MinPlayers"], "Minimum players %d exceeds MAX_BG_MEMBERS, capping to %d.\n", min, MAX_BG_MEMBERS / 2);
  66. min = MAX_BG_MEMBERS / 2;
  67. }
  68. bg->required_players = min;
  69. } else {
  70. if (!exists)
  71. bg->required_players = 1;
  72. }
  73. if (this->nodeExists(node, "MaxPlayers")) {
  74. int max;
  75. if (!this->asInt32(node, "MaxPlayers", max))
  76. return 0;
  77. if (max < 1) {
  78. this->invalidWarning(node["MaxPlayers"], "Maximum players %d cannot be less than 1, capping to 1.\n", max);
  79. max = 1;
  80. }
  81. if (max * 2 > MAX_BG_MEMBERS) {
  82. this->invalidWarning(node["MaxPlayers"], "Maximum players %d exceeds MAX_BG_MEMBERS, capping to %d.\n", max, MAX_BG_MEMBERS / 2);
  83. max = MAX_BG_MEMBERS / 2;
  84. }
  85. bg->max_players = max;
  86. } else {
  87. if (!exists)
  88. bg->max_players = MAX_BG_MEMBERS / 2;
  89. }
  90. if (this->nodeExists(node, "MinLevel")) {
  91. int min;
  92. if (!this->asInt32(node, "MinLevel", min))
  93. return 0;
  94. if (min > MAX_LEVEL) {
  95. this->invalidWarning(node["MinLevel"], "Minimum level %d exceeds MAX_LEVEL, capping to %d.\n", min, MAX_LEVEL);
  96. min = MAX_LEVEL;
  97. }
  98. bg->min_lvl = min;
  99. } else {
  100. if (!exists)
  101. bg->min_lvl = 1;
  102. }
  103. if (this->nodeExists(node, "MaxLevel")) {
  104. int max;
  105. if (!this->asInt32(node, "MaxLevel", max))
  106. return 0;
  107. if (max > MAX_LEVEL) {
  108. this->invalidWarning(node["MaxLevel"], "Maximum level %d exceeds MAX_LEVEL, capping to %d.\n", max, MAX_LEVEL);
  109. max = MAX_LEVEL;
  110. }
  111. bg->max_lvl = max;
  112. } else {
  113. if (!exists)
  114. bg->max_lvl = MAX_LEVEL;
  115. }
  116. if (this->nodeExists(node, "Deserter")) {
  117. uint32 deserter;
  118. if (!this->asUInt32(node, "Deserter", deserter))
  119. return 0;
  120. bg->deserter_time = deserter;
  121. } else {
  122. if (!exists)
  123. bg->deserter_time = 600;
  124. }
  125. if (this->nodeExists(node, "StartDelay")) {
  126. uint32 delay;
  127. if (!this->asUInt32(node, "StartDelay", delay))
  128. return 0;
  129. bg->start_delay = delay;
  130. } else {
  131. if (!exists)
  132. bg->start_delay = 0;
  133. }
  134. if (this->nodeExists(node, "Join")) {
  135. const auto& joinNode = node["Join"];
  136. if (this->nodeExists(joinNode, "Solo")) {
  137. bool active;
  138. if (!this->asBool(joinNode, "Solo", active))
  139. return 0;
  140. bg->solo = active;
  141. } else {
  142. if (!exists)
  143. bg->solo = true;
  144. }
  145. if (this->nodeExists(joinNode, "Party")) {
  146. bool active;
  147. if (!this->asBool(joinNode, "Party", active))
  148. return 0;
  149. bg->party = active;
  150. } else {
  151. if (!exists)
  152. bg->party = true;
  153. }
  154. if (this->nodeExists(joinNode, "Guild")) {
  155. bool active;
  156. if (!this->asBool(joinNode, "Guild", active))
  157. return 0;
  158. bg->guild = active;
  159. } else {
  160. if (!exists)
  161. bg->guild = true;
  162. }
  163. } else {
  164. if (!exists) {
  165. bg->solo = true;
  166. bg->party = true;
  167. bg->guild = true;
  168. }
  169. }
  170. if (this->nodeExists(node, "JobRestrictions")) {
  171. const auto& jobsNode = node["JobRestrictions"];
  172. for (const auto& jobit : jobsNode) {
  173. std::string job_name;
  174. c4::from_chars(jobit.key(), &job_name);
  175. std::string job_name_constant = "JOB_" + job_name;
  176. int64 constant;
  177. if (!script_get_constant(job_name_constant.c_str(), &constant)) {
  178. this->invalidWarning(node["JobRestrictions"], "Job %s does not exist.\n", job_name.c_str());
  179. continue;
  180. }
  181. bool active;
  182. if (!this->asBool(jobsNode, job_name, active))
  183. return 0;
  184. if (active)
  185. bg->job_restrictions.push_back(static_cast<int32>(constant));
  186. else
  187. util::vector_erase_if_exists(bg->job_restrictions, static_cast<int32>(constant));
  188. }
  189. }
  190. if (this->nodeExists(node, "Locations")) {
  191. int count = 0;
  192. for (const auto& location : node["Locations"]) {
  193. s_battleground_map map_entry;
  194. if (this->nodeExists(location, "Map")) {
  195. std::string map_name;
  196. if (!this->asString(location, "Map", map_name))
  197. return 0;
  198. map_entry.mapindex = mapindex_name2id(map_name.c_str());
  199. if (map_entry.mapindex == 0) {
  200. this->invalidWarning(location["Map"], "Invalid battleground map name %s, skipping.\n", map_name.c_str());
  201. return 0;
  202. }
  203. if( map_mapindex2mapid( map_entry.mapindex ) < 0 ){
  204. // Ignore silently, the map is on another mapserver
  205. return 0;
  206. }
  207. }
  208. if (this->nodeExists(location, "StartEvent")) {
  209. if (!this->asString(location, "StartEvent", map_entry.bgcallscript))
  210. return 0;
  211. if (map_entry.bgcallscript.length() > EVENT_NAME_LENGTH) {
  212. this->invalidWarning(location["StartEvent"], "StartEvent \"%s\" exceeds maximum of %d characters, capping...\n", map_entry.bgcallscript.c_str(), EVENT_NAME_LENGTH - 1);
  213. }
  214. map_entry.bgcallscript.resize(EVENT_NAME_LENGTH);
  215. if (map_entry.bgcallscript.find("::On") == std::string::npos) {
  216. this->invalidWarning(location["StartEvent"], "Battleground StartEvent label %s should begin with '::On', skipping.\n", map_entry.bgcallscript.c_str());
  217. return 0;
  218. }
  219. }
  220. std::vector<std::string> team_list = { "TeamA", "TeamB" };
  221. for (const auto &it : team_list) {
  222. c4::csubstr team_name = c4::to_csubstr(it);
  223. const ryml::NodeRef& team = location;
  224. if (this->nodeExists(team, it)) {
  225. s_battleground_team *team_ptr;
  226. if (it.find("TeamA") != std::string::npos)
  227. team_ptr = &map_entry.team1;
  228. else if (it.find("TeamB") != std::string::npos)
  229. team_ptr = &map_entry.team2;
  230. else {
  231. this->invalidWarning(team[team_name], "An invalid Team is defined.\n");
  232. return 0;
  233. }
  234. if (this->nodeExists(team[team_name], "RespawnX")) {
  235. uint16 warp_x;
  236. if (!this->asUInt16(team[team_name], "RespawnX", warp_x))
  237. return 0;
  238. if (warp_x == 0) {
  239. this->invalidWarning(node["RespawnX"], "RespawnX has to be greater than zero.\n");
  240. return 0;
  241. }
  242. map_data *md = map_getmapdata(map_mapindex2mapid(map_entry.mapindex));
  243. if (warp_x >= md->xs) {
  244. this->invalidWarning(node["RespawnX"], "RespawnX has to be smaller than %hu.\n", md->xs);
  245. return 0;
  246. }
  247. team_ptr->warp_x = warp_x;
  248. }
  249. if (this->nodeExists(team[team_name], "RespawnY")) {
  250. uint16 warp_y;
  251. if (!this->asUInt16(team[team_name], "RespawnY", warp_y))
  252. return 0;
  253. if (warp_y == 0) {
  254. this->invalidWarning(node["RespawnY"], "RespawnY has to be greater than zero.\n");
  255. return 0;
  256. }
  257. map_data *md = map_getmapdata(map_mapindex2mapid(map_entry.mapindex));
  258. if (warp_y >= md->ys) {
  259. this->invalidWarning(node["RespawnY"], "RespawnY has to be smaller than %hu.\n", md->ys);
  260. return 0;
  261. }
  262. team_ptr->warp_y = warp_y;
  263. }
  264. if (this->nodeExists(team[team_name], "DeathEvent")) {
  265. if (!this->asString(team[team_name], "DeathEvent", team_ptr->death_event))
  266. return 0;
  267. team_ptr->death_event.resize(EVENT_NAME_LENGTH);
  268. if (team_ptr->death_event.find("::On") == std::string::npos) {
  269. this->invalidWarning(team["DeathEvent"], "Battleground DeathEvent label %s should begin with '::On', skipping.\n", team_ptr->death_event.c_str());
  270. return 0;
  271. }
  272. }
  273. if (this->nodeExists(team[team_name], "QuitEvent")) {
  274. if (!this->asString(team[team_name], "QuitEvent", team_ptr->quit_event))
  275. return 0;
  276. team_ptr->quit_event.resize(EVENT_NAME_LENGTH);
  277. if (team_ptr->quit_event.find("::On") == std::string::npos) {
  278. this->invalidWarning(team["QuitEvent"], "Battleground QuitEvent label %s should begin with '::On', skipping.\n", team_ptr->quit_event.c_str());
  279. return 0;
  280. }
  281. }
  282. if (this->nodeExists(team[team_name], "ActiveEvent")) {
  283. if (!this->asString(team[team_name], "ActiveEvent", team_ptr->active_event))
  284. return 0;
  285. team_ptr->active_event.resize(EVENT_NAME_LENGTH);
  286. if (team_ptr->active_event.find("::On") == std::string::npos) {
  287. this->invalidWarning(team["ActiveEvent"], "Battleground ActiveEvent label %s should begin with '::On', skipping.\n", team_ptr->active_event.c_str());
  288. return 0;
  289. }
  290. }
  291. if (this->nodeExists(team[team_name], "Variable")) {
  292. if (!this->asString(team[team_name], "Variable", team_ptr->bg_id_var))
  293. return 0;
  294. team_ptr->bg_id_var.resize(NAME_LENGTH);
  295. }
  296. map_entry.id = count++;
  297. map_entry.isReserved = false;
  298. }
  299. }
  300. bg->maps.push_back(map_entry);
  301. }
  302. }
  303. if (!exists)
  304. this->put(id, bg);
  305. return 1;
  306. }
  307. /**
  308. * Search for a battleground based on the given name
  309. * @param name: Battleground name
  310. * @return s_battleground_type on success or nullptr on failure
  311. */
  312. std::shared_ptr<s_battleground_type> bg_search_name(const char *name)
  313. {
  314. for (const auto &entry : battleground_db) {
  315. auto bg = entry.second;
  316. if (!stricmp(bg->name.c_str(), name))
  317. return bg;
  318. }
  319. return nullptr;
  320. }
  321. /**
  322. * Search for a Battleground queue based on the given queue ID
  323. * @param queue_id: Queue ID
  324. * @return s_battleground_queue on success or nullptr on failure
  325. */
  326. std::shared_ptr<s_battleground_queue> bg_search_queue(int queue_id)
  327. {
  328. for (const auto &queue : bg_queues) {
  329. if (queue_id == queue->queue_id)
  330. return queue;
  331. }
  332. return nullptr;
  333. }
  334. /**
  335. * Search for an available player in Battleground
  336. * @param bg: Battleground data
  337. * @return map_session_data
  338. */
  339. map_session_data* bg_getavailablesd(s_battleground_data *bg)
  340. {
  341. nullpo_retr(nullptr, bg);
  342. for (const auto &member : bg->members) {
  343. if (member.sd != nullptr)
  344. return member.sd;
  345. }
  346. return nullptr;
  347. }
  348. /**
  349. * Delete a Battleground team from the db
  350. * @param bg_id: Battleground ID
  351. * @return True on success or false otherwise
  352. */
  353. bool bg_team_delete(int bg_id)
  354. {
  355. std::shared_ptr<s_battleground_data> bgteam = util::umap_find(bg_team_db, bg_id);
  356. if (bgteam) {
  357. for (const auto &pl_sd : bgteam->members) {
  358. bg_send_dot_remove(pl_sd.sd);
  359. pl_sd.sd->bg_id = 0;
  360. }
  361. bg_team_db.erase(bg_id);
  362. return true;
  363. }
  364. return false;
  365. }
  366. /**
  367. * Warps a Battleground team
  368. * @param bg_id: Battleground ID
  369. * @param mapindex: Map Index
  370. * @param x: X coordinate
  371. * @param y: Y coordinate
  372. * @return True on success or false otherwise
  373. */
  374. bool bg_team_warp(int bg_id, unsigned short mapindex, short x, short y)
  375. {
  376. std::shared_ptr<s_battleground_data> bgteam = util::umap_find(bg_team_db, bg_id);
  377. if (bgteam) {
  378. for (const auto &pl_sd : bgteam->members)
  379. pc_setpos(pl_sd.sd, mapindex, x, y, CLR_TELEPORT);
  380. return true;
  381. }
  382. return false;
  383. }
  384. /**
  385. * Remove a player's Battleground map marker
  386. * @param sd: Player data
  387. */
  388. void bg_send_dot_remove(map_session_data *sd)
  389. {
  390. nullpo_retv(sd);
  391. if( sd && sd->bg_id )
  392. clif_bg_xy_remove(sd);
  393. return;
  394. }
  395. /**
  396. * Join a player to a Battleground team
  397. * @param bg_id: Battleground ID
  398. * @param sd: Player data
  399. * @param is_queue: Joined from queue
  400. * @return True on success or false otherwise
  401. */
  402. bool bg_team_join(int bg_id, map_session_data *sd, bool is_queue)
  403. {
  404. if (!sd || sd->bg_id)
  405. return false;
  406. std::shared_ptr<s_battleground_data> bgteam = util::umap_find(bg_team_db, bg_id);
  407. if (bgteam) {
  408. if (bgteam->members.size() == MAX_BG_MEMBERS)
  409. return false; // No free slots
  410. s_battleground_member_data member = {};
  411. sd->bg_id = bg_id;
  412. member.sd = sd;
  413. member.x = sd->bl.x;
  414. member.y = sd->bl.y;
  415. if (is_queue) { // Save the location from where the person entered the battleground
  416. member.entry_point.map = sd->mapindex;
  417. member.entry_point.x = sd->bl.x;
  418. member.entry_point.y = sd->bl.y;
  419. }
  420. bgteam->members.push_back(member);
  421. guild_send_dot_remove(sd);
  422. for (const auto &pl_sd : bgteam->members) {
  423. if (pl_sd.sd != sd)
  424. clif_hpmeter_single( *sd, pl_sd.sd->bl.id, pl_sd.sd->battle_status.hp, pl_sd.sd->battle_status.max_hp );
  425. }
  426. clif_bg_hp(sd);
  427. clif_bg_xy(sd);
  428. return true;
  429. }
  430. return false;
  431. }
  432. /**
  433. * Remove a player from Battleground team
  434. * @param sd: Player data
  435. * @param quit: True if closed client or false otherwise
  436. * @param deserter: Whether to apply the deserter status or not
  437. * @return Remaining count in Battleground team or -1 on failure
  438. */
  439. int bg_team_leave(map_session_data *sd, bool quit, bool deserter)
  440. {
  441. if (!sd || !sd->bg_id)
  442. return -1;
  443. bg_send_dot_remove(sd);
  444. int bg_id = sd->bg_id;
  445. std::shared_ptr<s_battleground_data> bgteam = util::umap_find(bg_team_db, bg_id);
  446. sd->bg_id = 0;
  447. if (bgteam) {
  448. // Warping members out only applies to the Battleground Queue System
  449. if (battle_config.feature_bgqueue) {
  450. auto member = bgteam->members.begin();
  451. while (member != bgteam->members.end()) {
  452. if (member->sd == sd) {
  453. if (member->entry_point.map != 0 && !map_getmapflag(map_mapindex2mapid(member->entry_point.map), MF_NOSAVE))
  454. pc_setpos(sd, member->entry_point.map, member->entry_point.x, member->entry_point.y, CLR_TELEPORT);
  455. else
  456. pc_setpos( sd, mapindex_name2id( sd->status.save_point.map ), sd->status.save_point.x, sd->status.save_point.y, CLR_TELEPORT ); // Warp to save point if the entry map has no save flag.
  457. bgteam->members.erase(member);
  458. break;
  459. } else
  460. member++;
  461. }
  462. }
  463. char output[CHAT_SIZE_MAX];
  464. if (quit)
  465. sprintf(output, "Server: %s has quit the game...", sd->status.name);
  466. else
  467. sprintf(output, "Server: %s is leaving the battlefield...", sd->status.name);
  468. clif_bg_message(bgteam.get(), 0, "Server", output, strlen(output) + 1);
  469. if (!bgteam->logout_event.empty() && quit)
  470. npc_event(sd, bgteam->logout_event.c_str(), 0);
  471. if (deserter) {
  472. std::shared_ptr<s_battleground_type> bg = battleground_db.find(bg_id);
  473. if (bg)
  474. sc_start(nullptr, &sd->bl, SC_ENTRY_QUEUE_NOTIFY_ADMISSION_TIME_OUT, 100, 1, static_cast<t_tick>(bg->deserter_time) * 1000); // Deserter timer
  475. }
  476. return bgteam->members.size();
  477. }
  478. return -1;
  479. }
  480. /**
  481. * Respawn a Battleground player
  482. * @param sd: Player data
  483. * @return True on success or false otherwise
  484. */
  485. bool bg_member_respawn(map_session_data *sd)
  486. {
  487. if (!sd || !sd->bg_id || !pc_isdead(sd))
  488. return false;
  489. std::shared_ptr<s_battleground_data> bgteam = util::umap_find(bg_team_db, sd->bg_id);
  490. if (bgteam) {
  491. if (bgteam->cemetery.map == 0)
  492. return false; // Respawn not handled by Core
  493. pc_setpos(sd, bgteam->cemetery.map, bgteam->cemetery.x, bgteam->cemetery.y, CLR_OUTSIGHT);
  494. status_revive(&sd->bl, 1, 100);
  495. return true; // Warped
  496. }
  497. return false;
  498. }
  499. /**
  500. * Initialize Battleground data
  501. * @param mapindex: Map Index
  502. * @param rx: Return X coordinate (on death)
  503. * @param ry: Return Y coordinate (on death)
  504. * @param ev: Logout NPC Event
  505. * @param dev: Death NPC Event
  506. * @return Battleground ID
  507. */
  508. int bg_create(uint16 mapindex, s_battleground_team* team)
  509. {
  510. int bg_team_counter = 1;
  511. while (bg_team_db.find(bg_team_counter) != bg_team_db.end())
  512. bg_team_counter++;
  513. bg_team_db[bg_team_counter] = std::make_shared<s_battleground_data>();
  514. std::shared_ptr<s_battleground_data> bg = util::umap_find(bg_team_db, bg_team_counter);
  515. bg->id = bg_team_counter;
  516. bg->cemetery.map = mapindex;
  517. bg->cemetery.x = team->warp_x;
  518. bg->cemetery.y = team->warp_y;
  519. bg->logout_event = team->quit_event.c_str();
  520. bg->die_event = team->death_event.c_str();
  521. bg->active_event = team->active_event.c_str();
  522. return bg->id;
  523. }
  524. /**
  525. * Get an object's Battleground ID
  526. * @param bl: Object
  527. * @return Battleground ID
  528. */
  529. int bg_team_get_id(struct block_list *bl)
  530. {
  531. nullpo_ret(bl);
  532. switch( bl->type ) {
  533. case BL_PC:
  534. return ((TBL_PC*)bl)->bg_id;
  535. case BL_PET:
  536. if( ((TBL_PET*)bl)->master )
  537. return ((TBL_PET*)bl)->master->bg_id;
  538. break;
  539. case BL_MOB: {
  540. map_session_data *msd;
  541. struct mob_data *md = (TBL_MOB*)bl;
  542. if( md->special_state.ai && (msd = map_id2sd(md->master_id)) != nullptr )
  543. return msd->bg_id;
  544. return md->bg_id;
  545. }
  546. case BL_HOM:
  547. if( ((TBL_HOM*)bl)->master )
  548. return ((TBL_HOM*)bl)->master->bg_id;
  549. break;
  550. case BL_MER:
  551. if( ((TBL_MER*)bl)->master )
  552. return ((TBL_MER*)bl)->master->bg_id;
  553. break;
  554. case BL_SKILL:
  555. return ((TBL_SKILL*)bl)->group->bg_id;
  556. }
  557. return 0;
  558. }
  559. /**
  560. * Send a Battleground chat message
  561. * @param sd: Player data
  562. * @param mes: Message
  563. * @param len: Message length
  564. */
  565. void bg_send_message(map_session_data *sd, const char *mes, size_t len)
  566. {
  567. nullpo_retv(sd);
  568. if (sd->bg_id == 0)
  569. return;
  570. std::shared_ptr<s_battleground_data> bgteam = util::umap_find(bg_team_db, sd->bg_id);
  571. if (bgteam)
  572. clif_bg_message(bgteam.get(), sd->bl.id, sd->status.name, mes, len);
  573. return;
  574. }
  575. /**
  576. * Update a player's Battleground minimap icon
  577. * @see DBApply
  578. */
  579. int bg_send_xy_timer_sub(std::shared_ptr<s_battleground_data> bg)
  580. {
  581. map_session_data *sd;
  582. for (auto &pl_sd : bg->members) {
  583. sd = pl_sd.sd;
  584. if (sd->bl.x != pl_sd.x || sd->bl.y != pl_sd.y) { // xy update
  585. pl_sd.x = sd->bl.x;
  586. pl_sd.y = sd->bl.y;
  587. clif_bg_xy(sd);
  588. }
  589. }
  590. return 0;
  591. }
  592. /**
  593. * Update a player's Battleground minimap icon
  594. * @param tid: Timer ID
  595. * @param tick: Timer
  596. * @param id: ID
  597. * @return 0 on success or 1 otherwise
  598. */
  599. TIMER_FUNC(bg_send_xy_timer)
  600. {
  601. for (const auto &entry : bg_team_db)
  602. bg_send_xy_timer_sub(entry.second);
  603. return 0;
  604. }
  605. /**
  606. * Mark a Battleground as ready to begin queuing for a free map
  607. * @param tid: Timer ID
  608. * @param tick: Timer
  609. * @param id: ID
  610. * @return 0 on success or 1 otherwise
  611. */
  612. static TIMER_FUNC(bg_on_ready_loopback)
  613. {
  614. int queue_id = (int)data;
  615. std::shared_ptr<s_battleground_queue> queue = bg_search_queue(queue_id);
  616. if (queue == nullptr) {
  617. ShowError("bg_on_ready_loopback: Invalid battleground queue %d.\n", queue_id);
  618. return 1;
  619. }
  620. std::shared_ptr<s_battleground_type> bg = battleground_db.find(queue->id);
  621. if (bg) {
  622. bg_queue_on_ready(bg->name.c_str(), queue);
  623. return 0;
  624. } else {
  625. ShowError("bg_on_ready_loopback: Can't find battleground %d in the battlegrounds database.\n", queue->id);
  626. return 1;
  627. }
  628. }
  629. /**
  630. * Reset Battleground queue data if players don't accept in time
  631. * @param tid: Timer ID
  632. * @param tick: Timer
  633. * @param id: ID
  634. * @return 0 on success or 1 otherwise
  635. */
  636. static TIMER_FUNC(bg_on_ready_expire)
  637. {
  638. int queue_id = (int)data;
  639. std::shared_ptr<s_battleground_queue> queue = bg_search_queue(queue_id);
  640. if (queue == nullptr) {
  641. ShowError("bg_on_ready_expire: Invalid battleground queue %d.\n", queue_id);
  642. return 1;
  643. }
  644. std::string bg_name = battleground_db.find(queue->id)->name;
  645. for (const auto &sd : queue->teama_members) {
  646. clif_bg_queue_apply_result(BG_APPLY_QUEUE_FINISHED, bg_name.c_str(), sd);
  647. clif_bg_queue_entry_init(sd);
  648. }
  649. for (const auto &sd : queue->teamb_members) {
  650. clif_bg_queue_apply_result(BG_APPLY_QUEUE_FINISHED, bg_name.c_str(), sd);
  651. clif_bg_queue_entry_init(sd);
  652. }
  653. bg_queue_clear(queue, true);
  654. return 0;
  655. }
  656. /**
  657. * Start a Battleground when all players have accepted
  658. * @param tid: Timer ID
  659. * @param tick: Timer
  660. * @param id: ID
  661. * @return 0 on success or 1 otherwise
  662. */
  663. static TIMER_FUNC(bg_on_ready_start)
  664. {
  665. int queue_id = (int)data;
  666. std::shared_ptr<s_battleground_queue> queue = bg_search_queue(queue_id);
  667. if (queue == nullptr) {
  668. ShowError("bg_on_ready_start: Invalid battleground queue %d.\n", queue_id);
  669. return 1;
  670. }
  671. queue->tid_start = INVALID_TIMER;
  672. bg_queue_start_battleground(queue);
  673. return 0;
  674. }
  675. /**
  676. * Check if the given player is in a battleground
  677. * @param sd: Player data
  678. * @return True if in a battleground or false otherwise
  679. */
  680. bool bg_player_is_in_bg_map(map_session_data *sd)
  681. {
  682. nullpo_retr(false, sd);
  683. for (const auto &pair : battleground_db) {
  684. for (const auto &it : pair.second->maps) {
  685. if (it.mapindex == sd->mapindex)
  686. return true;
  687. }
  688. }
  689. return false;
  690. }
  691. /**
  692. * Battleground status change check
  693. * @param sd: Player data
  694. * @param name: Battleground name
  695. * @return True if the player is good to join a queue or false otherwise
  696. */
  697. static bool bg_queue_check_status(map_session_data* sd, const char *name)
  698. {
  699. nullpo_retr(false, sd);
  700. if (sd->sc.count) {
  701. if (sd->sc.getSCE(SC_ENTRY_QUEUE_APPLY_DELAY)) { // Exclude any player who's recently left a battleground queue
  702. char buf[CHAT_SIZE_MAX];
  703. sprintf(buf, msg_txt(sd, 339), static_cast<int32>((get_timer(sd->sc.getSCE(SC_ENTRY_QUEUE_APPLY_DELAY)->timer)->tick - gettick()) / 1000)); // You can't apply to a battleground queue for %d seconds due to recently leaving one.
  704. clif_bg_queue_apply_result(BG_APPLY_NONE, name, sd);
  705. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], buf, false, SELF);
  706. return false;
  707. }
  708. if (sd->sc.getSCE(SC_ENTRY_QUEUE_NOTIFY_ADMISSION_TIME_OUT)) { // Exclude any player who's recently deserted a battleground
  709. char buf[CHAT_SIZE_MAX];
  710. int32 status_tick = static_cast<int32>(DIFF_TICK(get_timer(sd->sc.getSCE(SC_ENTRY_QUEUE_NOTIFY_ADMISSION_TIME_OUT)->timer)->tick, gettick()) / 1000);
  711. sprintf(buf, msg_txt(sd, 338), status_tick / 60, status_tick % 60); // You can't apply to a battleground queue due to recently deserting a battleground. Time remaining: %d minutes and %d seconds.
  712. clif_bg_queue_apply_result(BG_APPLY_NONE, name, sd);
  713. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], buf, false, SELF);
  714. return false;
  715. }
  716. }
  717. return true;
  718. }
  719. /**
  720. * Check to see if a Battleground is joinable
  721. * @param bg: Battleground data
  722. * @param sd: Player data
  723. * @param name: Battleground name
  724. * @return True on success or false otherwise
  725. */
  726. bool bg_queue_check_joinable(std::shared_ptr<s_battleground_type> bg, map_session_data *sd, const char *name)
  727. {
  728. nullpo_retr(false, sd);
  729. for (const auto &job : bg->job_restrictions) { // Check class requirement
  730. if (sd->status.class_ == job) {
  731. clif_bg_queue_apply_result(BG_APPLY_PLAYER_CLASS, name, sd);
  732. return false;
  733. }
  734. }
  735. if (bg->min_lvl > 0 && sd->status.base_level < bg->min_lvl) { // Check minimum level requirement
  736. clif_bg_queue_apply_result(BG_APPLY_PLAYER_LEVEL, name, sd);
  737. return false;
  738. }
  739. if (bg->max_lvl > 0 && sd->status.base_level > bg->max_lvl) { // Check maximum level requirement
  740. clif_bg_queue_apply_result(BG_APPLY_PLAYER_LEVEL, name, sd);
  741. return false;
  742. }
  743. if (!bg_queue_check_status(sd, name)) // Check status blocks
  744. return false;
  745. if (bg_player_is_in_bg_map(sd)) { // Is the player currently in a battleground map? Reject them.
  746. clif_bg_queue_apply_result(BG_APPLY_NONE, name, sd);
  747. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], msg_txt(sd, 337), false, SELF); // You can't apply to a battleground queue from this map.
  748. return false;
  749. }
  750. if (battle_config.bgqueue_nowarp_mapflag > 0 && map_getmapflag(sd->bl.m, MF_NOWARP)) { // Check player's current position for mapflag check
  751. clif_bg_queue_apply_result(BG_APPLY_NONE, name, sd);
  752. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], msg_txt(sd, 337), false, SELF); // You can't apply to a battleground queue from this map.
  753. return false;
  754. }
  755. return true;
  756. }
  757. /**
  758. * Mark a map as reserved for a Battleground
  759. * @param name: Battleground map name
  760. * @param state: Whether to mark reserved or not
  761. * @param ended: Whether the Battleground event is complete; players getting prize
  762. * @return True on success or false otherwise
  763. */
  764. bool bg_queue_reservation(const char *name, bool state, bool ended)
  765. {
  766. uint16 mapindex = mapindex_name2id(name);
  767. for (auto &pair : battleground_db) {
  768. for (auto &map : pair.second->maps) {
  769. if (map.mapindex == mapindex) {
  770. map.isReserved = state;
  771. for (auto &queue : bg_queues) {
  772. if (queue->map == &map) {
  773. if (ended) // The ended flag is applied from bg_reserve (bg_unbook clears it for the next queue)
  774. queue->state = QUEUE_STATE_ENDED;
  775. if (!state)
  776. bg_queue_clear(queue, true);
  777. }
  778. }
  779. return true;
  780. }
  781. }
  782. }
  783. return false;
  784. }
  785. /**
  786. * Join as an individual into a Battleground
  787. * @param name: Battleground name
  788. * @param sd: Player who requested to join the battlegrounds
  789. */
  790. void bg_queue_join_solo(const char *name, map_session_data *sd)
  791. {
  792. if (!sd) {
  793. ShowError("bg_queue_join_solo: Tried to join non-existent player\n.");
  794. return;
  795. }
  796. std::shared_ptr<s_battleground_type> bg = bg_search_name(name);
  797. if (!bg) {
  798. ShowWarning("bq_queue_join_solo: Could not find battleground \"%s\" requested by %s (AID: %d / CID: %d)\n", name, sd->status.name, sd->status.account_id, sd->status.char_id);
  799. return;
  800. }
  801. if (!bg->solo) {
  802. clif_bg_queue_apply_result(BG_APPLY_INVALID_APP, name, sd);
  803. return;
  804. }
  805. bg_queue_join_multi(name, sd, { sd }); // Join as solo
  806. }
  807. /**
  808. * Join a party onto the same side of a Battleground
  809. * @param name: Battleground name
  810. * @param sd: Player who requested to join the battlegrounds
  811. */
  812. void bg_queue_join_party(const char *name, map_session_data *sd)
  813. {
  814. if (!sd) {
  815. ShowError("bg_queue_join_party: Tried to join non-existent player\n.");
  816. return;
  817. }
  818. struct party_data *p = party_search(sd->status.party_id);
  819. if (!p) {
  820. clif_bg_queue_apply_result(BG_APPLY_INVALID_APP, name, sd);
  821. return; // Someone has bypassed the client check for being in a party
  822. }
  823. for (const auto &it : p->party.member) {
  824. if (it.leader && sd->status.char_id != it.char_id) {
  825. clif_bg_queue_apply_result(BG_APPLY_PARTYGUILD_LEADER, name, sd);
  826. return; // Not the party leader
  827. }
  828. }
  829. std::shared_ptr<s_battleground_type> bg = bg_search_name(name);
  830. if (bg) {
  831. if (!bg->party) {
  832. clif_bg_queue_apply_result(BG_APPLY_INVALID_APP, name, sd);
  833. return;
  834. }
  835. int p_online = 0;
  836. for (const auto &it : p->party.member) {
  837. if (it.online)
  838. p_online++;
  839. }
  840. if (p_online > bg->max_players) {
  841. clif_bg_queue_apply_result(BG_APPLY_PLAYER_COUNT, name, sd);
  842. return; // Too many party members online
  843. }
  844. std::vector<map_session_data *> list;
  845. for (const auto &it : p->party.member) {
  846. if (list.size() == bg->max_players)
  847. break;
  848. if (it.online) {
  849. map_session_data *pl_sd = map_charid2sd(it.char_id);
  850. if (pl_sd)
  851. list.push_back(pl_sd);
  852. }
  853. }
  854. bg_queue_join_multi(name, sd, list); // Join as party, all on the same side of the BG
  855. } else {
  856. ShowWarning("clif_parse_bg_queue_apply_request: Could not find Battleground: \"%s\" requested by player: %s (AID:%d CID:%d)\n", name, sd->status.name, sd->status.account_id, sd->status.char_id);
  857. clif_bg_queue_apply_result(BG_APPLY_INVALID_NAME, name, sd);
  858. return; // Invalid BG name
  859. }
  860. }
  861. /**
  862. * Join a guild onto the same side of a Battleground
  863. * @param name: Battleground name
  864. * @param sd: Player who requested to join the battlegrounds
  865. */
  866. void bg_queue_join_guild(const char *name, map_session_data *sd)
  867. {
  868. if (!sd) {
  869. ShowError("bg_queue_join_guild: Tried to join non-existent player\n.");
  870. return;
  871. }
  872. if (!sd->guild) {
  873. clif_bg_queue_apply_result(BG_APPLY_INVALID_APP, name, sd);
  874. return; // Someone has bypassed the client check for being in a guild
  875. }
  876. if (strcmp(sd->status.name, sd->guild->guild.master) != 0) {
  877. clif_bg_queue_apply_result(BG_APPLY_PARTYGUILD_LEADER, name, sd);
  878. return; // Not the guild leader
  879. }
  880. std::shared_ptr<s_battleground_type> bg = bg_search_name(name);
  881. if (bg) {
  882. if (!bg->guild) {
  883. clif_bg_queue_apply_result(BG_APPLY_INVALID_APP, name, sd);
  884. return;
  885. }
  886. auto &g = sd->guild;
  887. if (g->guild.connect_member > bg->max_players) {
  888. clif_bg_queue_apply_result(BG_APPLY_PLAYER_COUNT, name, sd);
  889. return; // Too many guild members online
  890. }
  891. std::vector<map_session_data *> list;
  892. for (const auto &it : g->guild.member) {
  893. if (list.size() == bg->max_players)
  894. break;
  895. if (it.online) {
  896. map_session_data *pl_sd = map_charid2sd(it.char_id);
  897. if (pl_sd)
  898. list.push_back(pl_sd);
  899. }
  900. }
  901. bg_queue_join_multi(name, sd, list); // Join as guild, all on the same side of the BG
  902. } else {
  903. ShowWarning("clif_parse_bg_queue_apply_request: Could not find Battleground: \"%s\" requested by player: %s (AID:%d CID:%d)\n", name, sd->status.name, sd->status.account_id, sd->status.char_id);
  904. clif_bg_queue_apply_result(BG_APPLY_INVALID_NAME, name, sd);
  905. return; // Invalid BG name
  906. }
  907. }
  908. /**
  909. * Join multiple players onto the same side of a Battleground
  910. * @param name: Battleground name
  911. * @param sd: Player who requested to join the battlegrounds
  912. * @param list: Contains all players including the player who requested to join
  913. */
  914. void bg_queue_join_multi(const char *name, map_session_data *sd, std::vector <map_session_data *> list)
  915. {
  916. if (!sd) {
  917. ShowError("bg_queue_join_multi: Tried to join non-existent player\n.");
  918. return;
  919. }
  920. std::shared_ptr<s_battleground_type> bg = bg_search_name(name);
  921. if (!bg) {
  922. ShowWarning("bq_queue_join_multi: Could not find battleground \"%s\" requested by %s (AID: %d / CID: %d)\n", name, sd->status.name, sd->status.account_id, sd->status.char_id);
  923. return;
  924. }
  925. if (!bg_queue_check_joinable(bg, sd, name)){
  926. return;
  927. }
  928. for (const auto &queue : bg_queues) {
  929. if (queue->id != bg->id || queue->state == QUEUE_STATE_SETUP_DELAY || queue->state == QUEUE_STATE_ENDED)
  930. continue;
  931. // Make sure there's enough space on one side to join as a party/guild in this queue
  932. if (queue->teama_members.size() + list.size() > bg->max_players && queue->teamb_members.size() + list.size() > bg->max_players) {
  933. break;
  934. }
  935. bool r = rnd_chance(50, 100);
  936. std::vector<map_session_data *> *team = r ? &queue->teamb_members : &queue->teama_members;
  937. if (queue->state == QUEUE_STATE_ACTIVE) {
  938. // If one team has lesser members try to balance (on an active BG)
  939. if (r && queue->teama_members.size() < queue->teamb_members.size())
  940. team = &queue->teama_members;
  941. else if (!r && queue->teamb_members.size() < queue->teama_members.size())
  942. team = &queue->teamb_members;
  943. } else {
  944. // If the designated team is full, put the player into the other team
  945. if (team->size() + list.size() > bg->required_players)
  946. team = r ? &queue->teama_members : &queue->teamb_members;
  947. }
  948. while (!list.empty() && team->size() < bg->max_players) {
  949. map_session_data *sd2 = list.back();
  950. list.pop_back();
  951. if (!sd2 || sd2->bg_queue_id > 0)
  952. continue;
  953. if (!bg_queue_check_joinable(bg, sd2, name))
  954. continue;
  955. sd2->bg_queue_id = queue->queue_id;
  956. team->push_back(sd2);
  957. clif_bg_queue_apply_result(BG_APPLY_ACCEPT, name, sd2);
  958. clif_bg_queue_apply_notify(name, sd2);
  959. }
  960. if (queue->state == QUEUE_STATE_ACTIVE) { // Battleground is already active
  961. for (auto &pl_sd : *team) {
  962. if (queue->map->mapindex == pl_sd->mapindex)
  963. continue;
  964. pc_set_bg_queue_timer(pl_sd);
  965. clif_bg_queue_lobby_notify(name, pl_sd);
  966. }
  967. } else if (queue->state == QUEUE_STATE_SETUP && queue->teamb_members.size() >= bg->required_players && queue->teama_members.size() >= bg->required_players) // Enough players have joined
  968. bg_queue_on_ready(name, queue);
  969. return;
  970. }
  971. // Something went wrong, sends reconnect and then reapply message to client.
  972. clif_bg_queue_apply_result(BG_APPLY_RECONNECT, name, sd);
  973. }
  974. /**
  975. * Clear Battleground queue for next one
  976. * @param queue: Queue to clean up
  977. * @param ended: If a Battleground has ended through normal means (by script command bg_unbook)
  978. */
  979. void bg_queue_clear(std::shared_ptr<s_battleground_queue> queue, bool ended)
  980. {
  981. if (queue == nullptr)
  982. return;
  983. if (queue->tid_requeue != INVALID_TIMER) {
  984. delete_timer(queue->tid_requeue, bg_on_ready_loopback);
  985. queue->tid_requeue = INVALID_TIMER;
  986. }
  987. if (queue->tid_expire != INVALID_TIMER) {
  988. delete_timer(queue->tid_expire, bg_on_ready_expire);
  989. queue->tid_expire = INVALID_TIMER;
  990. }
  991. if (queue->tid_start != INVALID_TIMER) {
  992. delete_timer(queue->tid_start, bg_on_ready_start);
  993. queue->tid_start = INVALID_TIMER;
  994. }
  995. if (ended) {
  996. if (queue->map != nullptr) {
  997. queue->map->isReserved = false; // Remove reservation to free up for future queue
  998. queue->map = nullptr;
  999. }
  1000. for (const auto &sd : queue->teama_members)
  1001. sd->bg_queue_id = 0;
  1002. for (const auto &sd : queue->teamb_members)
  1003. sd->bg_queue_id = 0;
  1004. queue->teama_members.clear();
  1005. queue->teamb_members.clear();
  1006. queue->teama_members.shrink_to_fit();
  1007. queue->teamb_members.shrink_to_fit();
  1008. queue->accepted_players = 0;
  1009. queue->state = QUEUE_STATE_SETUP;
  1010. }
  1011. }
  1012. /**
  1013. * Sub function for leaving a Battleground queue
  1014. * @param sd: Player leaving
  1015. * @param members: List of players in queue data
  1016. * @param apply_sc: Apply the SC_ENTRY_QUEUE_APPLY_DELAY status on queue leave (default: true)
  1017. * @return True on success or false otherwise
  1018. */
  1019. static bool bg_queue_leave_sub(map_session_data *sd, std::vector<map_session_data *> &members, bool apply_sc)
  1020. {
  1021. if (!sd)
  1022. return false;
  1023. auto list_it = members.begin();
  1024. while (list_it != members.end()) {
  1025. if (*list_it == sd) {
  1026. members.erase(list_it);
  1027. if (apply_sc)
  1028. sc_start(nullptr, &sd->bl, SC_ENTRY_QUEUE_APPLY_DELAY, 100, 1, 60000);
  1029. sd->bg_queue_id = 0;
  1030. pc_delete_bg_queue_timer(sd);
  1031. return true;
  1032. } else {
  1033. list_it++;
  1034. }
  1035. }
  1036. return false;
  1037. }
  1038. /**
  1039. * Leave a Battleground queue
  1040. * @param sd: Player data
  1041. * @param apply_sc: Apply the SC_ENTRY_QUEUE_APPLY_DELAY status on queue leave (default: true)
  1042. * @return True on success or false otherwise
  1043. */
  1044. bool bg_queue_leave(map_session_data *sd, bool apply_sc)
  1045. {
  1046. if (!sd || sd->bg_queue_id == 0)
  1047. return false;
  1048. pc_delete_bg_queue_timer(sd);
  1049. for (auto &queue : bg_queues) {
  1050. if (sd->bg_queue_id == queue->queue_id) {
  1051. if (!bg_queue_leave_sub(sd, queue->teama_members, apply_sc) && !bg_queue_leave_sub(sd, queue->teamb_members, apply_sc)) {
  1052. ShowError("bg_queue_leave: Couldn't find player %s in battlegrounds queue.\n", sd->status.name);
  1053. return false;
  1054. } else {
  1055. if ((queue->state == QUEUE_STATE_SETUP || queue->state == QUEUE_STATE_SETUP_DELAY) && queue->teama_members.empty() && queue->teamb_members.empty()) // If there are no players left in the queue (that hasn't started), discard it
  1056. bg_queue_clear(queue, true);
  1057. return true;
  1058. }
  1059. }
  1060. }
  1061. return false;
  1062. }
  1063. /**
  1064. * Send packets to all clients in queue to notify them that the battleground is ready to be joined
  1065. * @param name: Battleground name
  1066. * @param queue: Battleground queue
  1067. * @return True on success or false otherwise
  1068. */
  1069. bool bg_queue_on_ready(const char *name, std::shared_ptr<s_battleground_queue> queue)
  1070. {
  1071. std::shared_ptr<s_battleground_type> bg = battleground_db.find(queue->id);
  1072. if (!bg) {
  1073. ShowError("bg_queue_on_ready: Couldn't find battleground ID %d in battlegrounds database.\n", queue->id);
  1074. return false;
  1075. }
  1076. if (queue->teama_members.size() < queue->required_players || queue->teamb_members.size() < queue->required_players)
  1077. return false; // Return players to the queue and stop reapplying the timer
  1078. bool map_reserved = false;
  1079. for (auto &map : bg->maps) {
  1080. if (!map.isReserved) {
  1081. map.isReserved = true;
  1082. map_reserved = true;
  1083. queue->map = &map;
  1084. break;
  1085. }
  1086. }
  1087. if (!map_reserved) { // All the battleground maps are reserved. Set a timer to check for an open battleground every 10 seconds.
  1088. queue->tid_requeue = add_timer(gettick() + 10000, bg_on_ready_loopback, 0, (intptr_t)queue->queue_id);
  1089. return false;
  1090. }
  1091. queue->state = QUEUE_STATE_SETUP_DELAY;
  1092. queue->tid_expire = add_timer(gettick() + 20000, bg_on_ready_expire, 0, (intptr_t)queue->queue_id);
  1093. for (const auto &sd : queue->teama_members)
  1094. clif_bg_queue_lobby_notify(name, sd);
  1095. for (const auto &sd : queue->teamb_members)
  1096. clif_bg_queue_lobby_notify(name, sd);
  1097. return true;
  1098. }
  1099. /**
  1100. * Send a player into an active Battleground
  1101. * @param sd: Player to send in
  1102. * @param queue: Queue data
  1103. */
  1104. void bg_join_active(map_session_data *sd, std::shared_ptr<s_battleground_queue> queue)
  1105. {
  1106. if (sd == nullptr || queue == nullptr)
  1107. return;
  1108. // Check player's current position for mapflag check
  1109. if (battle_config.bgqueue_nowarp_mapflag > 0 && map_getmapflag(sd->bl.m, MF_NOWARP)) {
  1110. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], msg_txt(sd, 337), false, SELF); // You can't apply to a battleground queue from this map.
  1111. bg_queue_leave(sd);
  1112. clif_bg_queue_entry_init(sd);
  1113. return;
  1114. }
  1115. pc_delete_bg_queue_timer(sd); // Cancel timer so player doesn't leave the queue.
  1116. int bg_id_team_1 = static_cast<int>(mapreg_readreg(add_str(queue->map->team1.bg_id_var.c_str())));
  1117. std::shared_ptr<s_battleground_data> bgteam_1 = util::umap_find(bg_team_db, bg_id_team_1);
  1118. for (auto &pl_sd : queue->teama_members) {
  1119. if (sd != pl_sd)
  1120. continue;
  1121. if (bgteam_1 == nullptr) {
  1122. bg_queue_leave(sd);
  1123. clif_bg_queue_apply_result(BG_APPLY_RECONNECT, battleground_db.find(queue->id)->name.c_str(), sd);
  1124. clif_bg_queue_entry_init(sd);
  1125. return;
  1126. }
  1127. clif_bg_queue_entry_init(pl_sd);
  1128. bg_team_join(bg_id_team_1, pl_sd, true);
  1129. npc_event(pl_sd, bgteam_1->active_event.c_str(), 0);
  1130. return;
  1131. }
  1132. int bg_id_team_2 = static_cast<int>(mapreg_readreg(add_str(queue->map->team2.bg_id_var.c_str())));
  1133. std::shared_ptr<s_battleground_data> bgteam_2 = util::umap_find(bg_team_db, bg_id_team_2);
  1134. for (auto &pl_sd : queue->teamb_members) {
  1135. if (sd != pl_sd)
  1136. continue;
  1137. if (bgteam_2 == nullptr) {
  1138. bg_queue_leave(sd);
  1139. clif_bg_queue_apply_result(BG_APPLY_RECONNECT, battleground_db.find(queue->id)->name.c_str(), sd);
  1140. clif_bg_queue_entry_init(sd);
  1141. return;
  1142. }
  1143. clif_bg_queue_entry_init(pl_sd);
  1144. bg_team_join(bg_id_team_2, pl_sd, true);
  1145. npc_event(pl_sd, bgteam_2->active_event.c_str(), 0);
  1146. return;
  1147. }
  1148. return;
  1149. }
  1150. /**
  1151. * Check to see if any players in the queue are on a map with MF_NOWARP and remove them from the queue
  1152. * @param queue: Queue data
  1153. * @return True if the player is on a map with MF_NOWARP or false otherwise
  1154. */
  1155. bool bg_mapflag_check(std::shared_ptr<s_battleground_queue> queue) {
  1156. if (queue == nullptr || battle_config.bgqueue_nowarp_mapflag == 0)
  1157. return false;
  1158. bool found = false;
  1159. for (const auto &sd : queue->teama_members) {
  1160. if (map_getmapflag(sd->bl.m, MF_NOWARP)) {
  1161. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], msg_txt(sd, 337), false, SELF); // You can't apply to a battleground queue from this map.
  1162. bg_queue_leave(sd);
  1163. clif_bg_queue_entry_init(sd);
  1164. found = true;
  1165. }
  1166. }
  1167. for (const auto &sd : queue->teamb_members) {
  1168. if (map_getmapflag(sd->bl.m, MF_NOWARP)) {
  1169. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], msg_txt(sd, 337), false, SELF); // You can't apply to a battleground queue from this map.
  1170. bg_queue_leave(sd);
  1171. clif_bg_queue_entry_init(sd);
  1172. found = true;
  1173. }
  1174. }
  1175. if (found) {
  1176. queue->state = QUEUE_STATE_SETUP; // Set back to queueing state
  1177. queue->accepted_players = 0; // Reset acceptance count
  1178. // Free map to avoid creating a reservation delay
  1179. if (queue->map != nullptr) {
  1180. queue->map->isReserved = false;
  1181. queue->map = nullptr;
  1182. }
  1183. // Announce failure to remaining players
  1184. for (const auto &sd : queue->teama_members)
  1185. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], msg_txt(sd, 340), false, SELF); // Participants were unable to join. Delaying entry for more participants.
  1186. for (const auto &sd : queue->teamb_members)
  1187. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], msg_txt(sd, 340), false, SELF); // Participants were unable to join. Delaying entry for more participants.
  1188. }
  1189. return found;
  1190. }
  1191. /**
  1192. * Update the Battleground queue when the player accepts the invite
  1193. * @param queue: Battleground queue
  1194. * @param sd: Player data
  1195. */
  1196. void bg_queue_on_accept_invite(map_session_data *sd)
  1197. {
  1198. nullpo_retv(sd);
  1199. std::shared_ptr<s_battleground_queue> queue = bg_search_queue(sd->bg_queue_id);
  1200. if (queue == nullptr) {
  1201. ShowError("bg_queue_on_accept_invite: Couldn't find player %s in battlegrounds queue.\n", sd->status.name);
  1202. return;
  1203. }
  1204. queue->accepted_players++;
  1205. clif_bg_queue_ack_lobby(true, mapindex_id2name(queue->map->mapindex), mapindex_id2name(queue->map->mapindex), sd);
  1206. if (queue->state == QUEUE_STATE_ACTIVE) // Battleground is already active
  1207. bg_join_active(sd, queue);
  1208. else if (queue->state == QUEUE_STATE_SETUP_DELAY) {
  1209. if (queue->accepted_players == queue->required_players * 2) {
  1210. if (queue->tid_expire != INVALID_TIMER) {
  1211. delete_timer(queue->tid_expire, bg_on_ready_expire);
  1212. queue->tid_expire = INVALID_TIMER;
  1213. }
  1214. // Check player's current position for mapflag check
  1215. if (battle_config.bgqueue_nowarp_mapflag > 0 && bg_mapflag_check(queue))
  1216. return;
  1217. queue->tid_start = add_timer(gettick() + battleground_db.find(queue->id)->start_delay * 1000, bg_on_ready_start, 0, (intptr_t)queue->queue_id);
  1218. }
  1219. }
  1220. }
  1221. /**
  1222. * Begin the Battleground from the given queue
  1223. * @param queue: Battleground queue
  1224. */
  1225. void bg_queue_start_battleground(std::shared_ptr<s_battleground_queue> queue)
  1226. {
  1227. if (queue == nullptr)
  1228. return;
  1229. std::shared_ptr<s_battleground_type> bg = battleground_db.find(queue->id);
  1230. if (!bg) {
  1231. bg_queue_clear(queue, true);
  1232. ShowError("bg_queue_start_battleground: Could not find battleground ID %d in battlegrounds database.\n", queue->id);
  1233. return;
  1234. }
  1235. // Check player's current position for mapflag check
  1236. if (battle_config.bgqueue_nowarp_mapflag > 0 && bg_mapflag_check(queue))
  1237. return;
  1238. uint16 map_idx = queue->map->mapindex;
  1239. int bg_team_1 = bg_create(map_idx, &queue->map->team1);
  1240. int bg_team_2 = bg_create(map_idx, &queue->map->team2);
  1241. for (const auto &sd : queue->teama_members) {
  1242. clif_bg_queue_entry_init(sd);
  1243. bg_team_join(bg_team_1, sd, true);
  1244. }
  1245. for (const auto &sd : queue->teamb_members) {
  1246. clif_bg_queue_entry_init(sd);
  1247. bg_team_join(bg_team_2, sd, true);
  1248. }
  1249. mapreg_setreg(add_str(queue->map->team1.bg_id_var.c_str()), bg_team_1);
  1250. mapreg_setreg(add_str(queue->map->team2.bg_id_var.c_str()), bg_team_2);
  1251. npc_event_do(queue->map->bgcallscript.c_str());
  1252. queue->state = QUEUE_STATE_ACTIVE;
  1253. bg_queue_clear(queue, false);
  1254. }
  1255. /**
  1256. * Initialize a Battleground queue
  1257. * @param bg_id: Battleground ID
  1258. * @param req_players: Required amount of players
  1259. * @return s_battleground_queue*
  1260. */
  1261. static void bg_queue_create(int bg_id, int req_players)
  1262. {
  1263. auto queue = std::make_shared<s_battleground_queue>();
  1264. queue->queue_id = bg_queue_count++;
  1265. queue->id = bg_id;
  1266. queue->required_players = req_players;
  1267. queue->accepted_players = 0;
  1268. queue->tid_expire = INVALID_TIMER;
  1269. queue->tid_start = INVALID_TIMER;
  1270. queue->tid_requeue = INVALID_TIMER;
  1271. queue->state = QUEUE_STATE_SETUP;
  1272. bg_queues.push_back(queue);
  1273. }
  1274. /**
  1275. * Initialize the Battleground data
  1276. */
  1277. void do_init_battleground(void)
  1278. {
  1279. if (battle_config.feature_bgqueue) {
  1280. battleground_db.load();
  1281. for (const auto &bg : battleground_db)
  1282. bg_queue_create(bg.first, bg.second->required_players);
  1283. }
  1284. add_timer_func_list(bg_send_xy_timer, "bg_send_xy_timer");
  1285. add_timer_func_list(bg_on_ready_loopback, "bg_on_ready_loopback");
  1286. add_timer_func_list(bg_on_ready_expire, "bg_on_ready_expire");
  1287. add_timer_func_list(bg_on_ready_start, "bg_on_ready_start");
  1288. add_timer_interval(gettick() + battle_config.bg_update_interval, bg_send_xy_timer, 0, 0, battle_config.bg_update_interval);
  1289. }
  1290. /**
  1291. * Clear the Battleground data from memory
  1292. */
  1293. void do_final_battleground(void)
  1294. {
  1295. }