battleground.cpp 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435
  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 <yaml-cpp/yaml.h>
  6. #include "../common/cbasetypes.hpp"
  7. #include "../common/malloc.hpp"
  8. #include "../common/nullpo.hpp"
  9. #include "../common/random.hpp"
  10. #include "../common/showmsg.hpp"
  11. #include "../common/strlib.hpp"
  12. #include "../common/timer.hpp"
  13. #include "../common/utilities.hpp"
  14. #include "battle.hpp"
  15. #include "clif.hpp"
  16. #include "guild.hpp"
  17. #include "homunculus.hpp"
  18. #include "mapreg.hpp"
  19. #include "mercenary.hpp"
  20. #include "mob.hpp"
  21. #include "npc.hpp"
  22. #include "party.hpp"
  23. #include "pc.hpp"
  24. #include "pet.hpp"
  25. using namespace rathena;
  26. BattlegroundDatabase battleground_db;
  27. std::unordered_map<int, std::shared_ptr<s_battleground_data>> bg_team_db;
  28. std::vector<std::shared_ptr<s_battleground_queue>> bg_queues;
  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 YAML::Node &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 * 2 > MAX_BG_MEMBERS) {
  61. this->invalidWarning(node["MinPlayers"], "Minimum players %d exceeds MAX_BG_MEMBERS, capping to %d.\n", min, MAX_BG_MEMBERS / 2);
  62. min = MAX_BG_MEMBERS / 2;
  63. }
  64. bg->required_players = min;
  65. } else {
  66. if (!exists)
  67. bg->required_players = 1;
  68. }
  69. if (this->nodeExists(node, "MaxPlayers")) {
  70. int max;
  71. if (!this->asInt32(node, "MaxPlayers", max))
  72. return 0;
  73. if (max * 2 > MAX_BG_MEMBERS) {
  74. this->invalidWarning(node["MaxPlayers"], "Maximum players %d exceeds MAX_BG_MEMBERS, capping to %d.\n", max, MAX_BG_MEMBERS / 2);
  75. max = MAX_BG_MEMBERS / 2;
  76. }
  77. bg->max_players = max;
  78. } else {
  79. if (!exists)
  80. bg->max_players = MAX_BG_MEMBERS / 2;
  81. }
  82. if (this->nodeExists(node, "MinLevel")) {
  83. int min;
  84. if (!this->asInt32(node, "MinLevel", min))
  85. return 0;
  86. if (min > MAX_LEVEL) {
  87. this->invalidWarning(node["MinLevel"], "Minimum level %d exceeds MAX_LEVEL, capping to %d.\n", min, MAX_LEVEL);
  88. min = MAX_LEVEL;
  89. }
  90. bg->min_lvl = min;
  91. } else {
  92. if (!exists)
  93. bg->min_lvl = 1;
  94. }
  95. if (this->nodeExists(node, "MaxLevel")) {
  96. int max;
  97. if (!this->asInt32(node, "MaxLevel", max))
  98. return 0;
  99. if (max > MAX_LEVEL) {
  100. this->invalidWarning(node["MaxLevel"], "Maximum level %d exceeds MAX_LEVEL, capping to %d.\n", max, MAX_LEVEL);
  101. max = MAX_LEVEL;
  102. }
  103. bg->max_lvl = max;
  104. } else {
  105. if (!exists)
  106. bg->max_lvl = MAX_LEVEL;
  107. }
  108. if (this->nodeExists(node, "Deserter")) {
  109. uint32 deserter;
  110. if (!this->asUInt32(node, "Deserter", deserter))
  111. return 0;
  112. bg->deserter_time = deserter;
  113. } else {
  114. if (!exists)
  115. bg->deserter_time = 600;
  116. }
  117. if (this->nodeExists(node, "StartDelay")) {
  118. uint32 delay;
  119. if (!this->asUInt32(node, "StartDelay", delay))
  120. return 0;
  121. bg->start_delay = delay;
  122. } else {
  123. if (!exists)
  124. bg->start_delay = 30;
  125. }
  126. if (this->nodeExists(node, "Locations")) {
  127. int count = 0;
  128. for (const auto &locationit : node["Locations"]) {
  129. const YAML::Node &location = locationit;
  130. s_battleground_map map_entry;
  131. if (this->nodeExists(location, "Map")) {
  132. std::string map_name;
  133. if (!this->asString(location, "Map", map_name))
  134. return 0;
  135. map_entry.mapid = map_mapname2mapid(map_name.c_str());
  136. if (map_entry.mapid == -1) {
  137. this->invalidWarning(location["Map"], "Invalid battleground map name %s, skipping.\n", map_name.c_str());
  138. return 0;
  139. }
  140. }
  141. if (this->nodeExists(location, "StartEvent")) {
  142. if (!this->asString(location, "StartEvent", map_entry.bgcallscript))
  143. return 0;
  144. map_entry.bgcallscript.resize(EVENT_NAME_LENGTH);
  145. if (map_entry.bgcallscript.find("::On") == std::string::npos) {
  146. this->invalidWarning(location["StartEvent"], "Battleground StartEvent label %s should begin with '::On', skipping.\n", map_entry.bgcallscript.c_str());
  147. return 0;
  148. }
  149. }
  150. std::vector<std::string> team_list = { "TeamA", "TeamB" };
  151. for (const auto &it : team_list) {
  152. const YAML::Node &team = location;
  153. if (this->nodeExists(team, it)) {
  154. s_battleground_team *team_ptr;
  155. if (it.find("TeamA") != std::string::npos)
  156. team_ptr = &map_entry.team1;
  157. else if (it.find("TeamB") != std::string::npos)
  158. team_ptr = &map_entry.team2;
  159. else {
  160. this->invalidWarning(team[it], "An invalid Team is defined.\n");
  161. return 0;
  162. }
  163. if (this->nodeExists(team[it], "RespawnX")) {
  164. if (!this->asInt16(team[it], "RespawnX", team_ptr->warp_x))
  165. return 0;
  166. }
  167. if (this->nodeExists(team[it], "RespawnY")) {
  168. if (!this->asInt16(team[it], "RespawnY", team_ptr->warp_y))
  169. return 0;
  170. }
  171. if (this->nodeExists(team[it], "DeathEvent")) {
  172. if (!this->asString(team[it], "DeathEvent", team_ptr->death_event))
  173. return 0;
  174. team_ptr->death_event.resize(EVENT_NAME_LENGTH);
  175. if (team_ptr->death_event.find("::On") == std::string::npos) {
  176. this->invalidWarning(team["DeathEvent"], "Battleground DeathEvent label %s should begin with '::On', skipping.\n", team_ptr->death_event.c_str());
  177. return 0;
  178. }
  179. }
  180. if (this->nodeExists(team[it], "QuitEvent")) {
  181. if (!this->asString(team[it], "QuitEvent", team_ptr->quit_event))
  182. return 0;
  183. team_ptr->quit_event.resize(EVENT_NAME_LENGTH);
  184. if (team_ptr->quit_event.find("::On") == std::string::npos) {
  185. this->invalidWarning(team["QuitEvent"], "Battleground QuitEvent label %s should begin with '::On', skipping.\n", team_ptr->quit_event.c_str());
  186. return 0;
  187. }
  188. }
  189. if (this->nodeExists(team[it], "Variable")) {
  190. if (!this->asString(team[it], "Variable", team_ptr->bg_id_var))
  191. return 0;
  192. team_ptr->bg_id_var.resize(NAME_LENGTH);
  193. }
  194. map_entry.id = count++;
  195. map_entry.isReserved = false;
  196. }
  197. }
  198. bg->maps.push_back(map_entry);
  199. }
  200. }
  201. if (!exists)
  202. this->put(id, bg);
  203. return 1;
  204. }
  205. /**
  206. * Search for a battleground based on the given name
  207. * @param name: Battleground name
  208. * @return s_battleground_type on success or nullptr on failure
  209. */
  210. std::shared_ptr<s_battleground_type> bg_search_name(const char *name)
  211. {
  212. for (const auto &entry : battleground_db) {
  213. auto bg = entry.second;
  214. if (!stricmp(bg->name.c_str(), name))
  215. return bg;
  216. }
  217. return nullptr;
  218. }
  219. /**
  220. * Search for an available player in Battleground
  221. * @param bg: Battleground data
  222. * @return map_session_data
  223. */
  224. struct map_session_data* bg_getavailablesd(s_battleground_data *bg)
  225. {
  226. nullpo_retr(nullptr, bg);
  227. return (bg->members.size() != 0) ? bg->members[0].sd : nullptr;
  228. }
  229. /**
  230. * Delete a Battleground team from the db
  231. * @param bg_id: Battleground ID
  232. * @return True on success or false otherwise
  233. */
  234. bool bg_team_delete(int bg_id)
  235. {
  236. std::shared_ptr<s_battleground_data> bgteam = util::umap_find(bg_team_db, bg_id);
  237. if (bgteam) {
  238. for (const auto &pl_sd : bgteam->members) {
  239. bg_send_dot_remove(pl_sd.sd);
  240. pl_sd.sd->bg_id = 0;
  241. }
  242. bg_team_db.erase(bg_id);
  243. return true;
  244. }
  245. return false;
  246. }
  247. /**
  248. * Warps a Battleground team
  249. * @param bg_id: Battleground ID
  250. * @param mapindex: Map Index
  251. * @param x: X coordinate
  252. * @param y: Y coordinate
  253. * @return True on success or false otherwise
  254. */
  255. bool bg_team_warp(int bg_id, unsigned short mapindex, short x, short y)
  256. {
  257. std::shared_ptr<s_battleground_data> bgteam = util::umap_find(bg_team_db, bg_id);
  258. if (bgteam) {
  259. for (const auto &pl_sd : bgteam->members)
  260. pc_setpos(pl_sd.sd, mapindex, x, y, CLR_TELEPORT);
  261. return true;
  262. }
  263. return false;
  264. }
  265. /**
  266. * Remove a player's Battleground map marker
  267. * @param sd: Player data
  268. */
  269. void bg_send_dot_remove(struct map_session_data *sd)
  270. {
  271. nullpo_retv(sd);
  272. if( sd && sd->bg_id )
  273. clif_bg_xy_remove(sd);
  274. return;
  275. }
  276. /**
  277. * Join a player to a Battleground team
  278. * @param bg_id: Battleground ID
  279. * @param sd: Player data
  280. * @param is_queue: Joined from queue
  281. * @return True on success or false otherwise
  282. */
  283. bool bg_team_join(int bg_id, struct map_session_data *sd, bool is_queue)
  284. {
  285. if (!sd || sd->bg_id)
  286. return false;
  287. std::shared_ptr<s_battleground_data> bgteam = util::umap_find(bg_team_db, bg_id);
  288. if (bgteam) {
  289. if (bgteam->members.size() == MAX_BG_MEMBERS)
  290. return false; // No free slots
  291. s_battleground_member_data member = {};
  292. sd->bg_id = bg_id;
  293. member.sd = sd;
  294. member.x = sd->bl.x;
  295. member.y = sd->bl.y;
  296. if (is_queue) { // Save the location from where the person entered the battleground
  297. member.entry_point.map = sd->mapindex;
  298. member.entry_point.x = sd->bl.x;
  299. member.entry_point.y = sd->bl.y;
  300. }
  301. bgteam->members.push_back(member);
  302. guild_send_dot_remove(sd);
  303. for (const auto &pl_sd : bgteam->members) {
  304. if (pl_sd.sd != sd)
  305. clif_hpmeter_single(sd->fd, pl_sd.sd->bl.id, pl_sd.sd->battle_status.hp, pl_sd.sd->battle_status.max_hp);
  306. }
  307. clif_bg_hp(sd);
  308. clif_bg_xy(sd);
  309. return true;
  310. }
  311. return false;
  312. }
  313. /**
  314. * Remove a player from Battleground team
  315. * @param sd: Player data
  316. * @param quit: True if closed client or false otherwise
  317. * @param deserter: Whether to apply the deserter status or not
  318. * @return Remaining count in Battleground team or -1 on failure
  319. */
  320. int bg_team_leave(struct map_session_data *sd, bool quit, bool deserter)
  321. {
  322. if (!sd || !sd->bg_id)
  323. return -1;
  324. bg_send_dot_remove(sd);
  325. int bg_id = sd->bg_id;
  326. std::shared_ptr<s_battleground_data> bgteam = util::umap_find(bg_team_db, bg_id);
  327. sd->bg_id = 0;
  328. if (bgteam) {
  329. char output[CHAT_SIZE_MAX];
  330. int i;
  331. ARR_FIND(0, bgteam->members.size(), i, bgteam->members[i].sd == sd);
  332. if (i < bgteam->members.size()) { // Removes member from BG
  333. if (bgteam->members[i].entry_point.map != 0) {
  334. int16 map_id = map_mapindex2mapid(bgteam->members[i].entry_point.map);
  335. if (!map_getmapflag(map_id, MF_NOSAVE))
  336. pc_setpos(sd, bgteam->members[i].entry_point.map, bgteam->members[i].entry_point.x, bgteam->members[i].entry_point.y, CLR_TELEPORT);
  337. else
  338. pc_setpos(sd, 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.
  339. }
  340. util::erase_at(bgteam->members, i);
  341. }
  342. if (quit)
  343. sprintf(output, "Server: %s has quit the game...", sd->status.name);
  344. else
  345. sprintf(output, "Server: %s is leaving the battlefield...", sd->status.name);
  346. clif_bg_message(bgteam.get(), 0, "Server", output, strlen(output) + 1);
  347. if (!bgteam->logout_event.empty() && quit)
  348. npc_event(sd, bgteam->logout_event.c_str(), 0);
  349. if (deserter) {
  350. std::shared_ptr<s_battleground_type> bg = battleground_db.find(bg_id);
  351. if (bg)
  352. sc_start(nullptr, &sd->bl, SC_ENTRY_QUEUE_NOTIFY_ADMISSION_TIME_OUT, 100, 1, static_cast<t_tick>(bg->deserter_time) * 1000); // Deserter timer
  353. }
  354. return bgteam->members.size();
  355. }
  356. return -1;
  357. }
  358. /**
  359. * Respawn a Battleground player
  360. * @param sd: Player data
  361. * @return True on success or false otherwise
  362. */
  363. bool bg_member_respawn(struct map_session_data *sd)
  364. {
  365. if (!sd || !sd->bg_id || !pc_isdead(sd))
  366. return false;
  367. std::shared_ptr<s_battleground_data> bgteam = util::umap_find(bg_team_db, sd->bg_id);
  368. if (bgteam) {
  369. if (bgteam->cemetery.map == 0)
  370. return false; // Respawn not handled by Core
  371. pc_setpos(sd, bgteam->cemetery.map, bgteam->cemetery.x, bgteam->cemetery.y, CLR_OUTSIGHT);
  372. status_revive(&sd->bl, 1, 100);
  373. return true; // Warped
  374. }
  375. return false;
  376. }
  377. /**
  378. * Initialize Battleground data
  379. * @param mapindex: Map Index
  380. * @param rx: Return X coordinate (on death)
  381. * @param ry: Return Y coordinate (on death)
  382. * @param ev: Logout NPC Event
  383. * @param dev: Death NPC Event
  384. * @return Battleground ID
  385. */
  386. int bg_create(uint16 mapindex, s_battleground_team* team)
  387. {
  388. int bg_team_counter = 1;
  389. while (bg_team_db.find(bg_team_counter) != bg_team_db.end())
  390. bg_team_counter++;
  391. bg_team_db[bg_team_counter] = std::make_shared<s_battleground_data>();
  392. std::shared_ptr<s_battleground_data> bg = util::umap_find(bg_team_db, bg_team_counter);
  393. bg->id = bg_team_counter;
  394. bg->cemetery.map = mapindex;
  395. bg->cemetery.x = team->warp_x;
  396. bg->cemetery.y = team->warp_y;
  397. bg->logout_event = team->quit_event.c_str();
  398. bg->die_event = team->death_event.c_str();
  399. bg->members.clear();
  400. return bg->id;
  401. }
  402. /**
  403. * Get an object's Battleground ID
  404. * @param bl: Object
  405. * @return Battleground ID
  406. */
  407. int bg_team_get_id(struct block_list *bl)
  408. {
  409. nullpo_ret(bl);
  410. switch( bl->type ) {
  411. case BL_PC:
  412. return ((TBL_PC*)bl)->bg_id;
  413. case BL_PET:
  414. if( ((TBL_PET*)bl)->master )
  415. return ((TBL_PET*)bl)->master->bg_id;
  416. break;
  417. case BL_MOB: {
  418. struct map_session_data *msd;
  419. struct mob_data *md = (TBL_MOB*)bl;
  420. if( md->special_state.ai && !(msd = map_id2sd(md->master_id)) )
  421. return msd->bg_id;
  422. return md->bg_id;
  423. }
  424. case BL_HOM:
  425. if( ((TBL_HOM*)bl)->master )
  426. return ((TBL_HOM*)bl)->master->bg_id;
  427. break;
  428. case BL_MER:
  429. if( ((TBL_MER*)bl)->master )
  430. return ((TBL_MER*)bl)->master->bg_id;
  431. break;
  432. case BL_SKILL:
  433. return ((TBL_SKILL*)bl)->group->bg_id;
  434. }
  435. return 0;
  436. }
  437. /**
  438. * Send a Battleground chat message
  439. * @param sd: Player data
  440. * @param mes: Message
  441. * @param len: Message length
  442. */
  443. void bg_send_message(struct map_session_data *sd, const char *mes, int len)
  444. {
  445. nullpo_retv(sd);
  446. if (!sd->bg_id)
  447. return;
  448. std::shared_ptr<s_battleground_data> bgteam = util::umap_find(bg_team_db, sd->bg_id);
  449. if (bgteam)
  450. clif_bg_message(bgteam.get(), sd->bl.id, sd->status.name, mes, len);
  451. return;
  452. }
  453. /**
  454. * Update a player's Battleground minimap icon
  455. * @see DBApply
  456. */
  457. int bg_send_xy_timer_sub(std::shared_ptr<s_battleground_data> bg)
  458. {
  459. struct map_session_data *sd;
  460. for (auto &pl_sd : bg->members) {
  461. sd = pl_sd.sd;
  462. if (sd->bl.x != pl_sd.x || sd->bl.y != pl_sd.y) { // xy update
  463. pl_sd.x = sd->bl.x;
  464. pl_sd.y = sd->bl.y;
  465. clif_bg_xy(sd);
  466. }
  467. }
  468. return 0;
  469. }
  470. /**
  471. * Update a player's Battleground minimap icon
  472. * @param tid: Timer ID
  473. * @param tick: Timer
  474. * @param id: ID
  475. * @return 0 on success or 1 otherwise
  476. */
  477. TIMER_FUNC(bg_send_xy_timer)
  478. {
  479. for (const auto &entry : bg_team_db)
  480. bg_send_xy_timer_sub(entry.second);
  481. return 0;
  482. }
  483. /**
  484. * Mark a Battleground as ready to begin queuing
  485. * @param tid: Timer ID
  486. * @param tick: Timer
  487. * @param id: ID
  488. * @return 0 on success or 1 otherwise
  489. */
  490. static TIMER_FUNC(bg_on_ready_loopback)
  491. {
  492. s_battleground_queue *queue = (s_battleground_queue*)data;
  493. nullpo_retr(1, queue);
  494. std::shared_ptr<s_battleground_type> bg = battleground_db.find(queue->id);
  495. if (bg) {
  496. bg_queue_on_ready(bg->name.c_str(), std::shared_ptr<s_battleground_queue>(queue));
  497. return 0;
  498. } else {
  499. ShowError("bg_on_ready_loopback: Can't find battleground %d in the battlegrounds database.\n", queue->id);
  500. return 1;
  501. }
  502. }
  503. /**
  504. * Reset Battleground queue data
  505. * @param tid: Timer ID
  506. * @param tick: Timer
  507. * @param id: ID
  508. * @return 0 on success or 1 otherwise
  509. */
  510. static TIMER_FUNC(bg_on_ready_expire)
  511. {
  512. s_battleground_queue *queue = (s_battleground_queue*)data;
  513. nullpo_retr(1, queue);
  514. queue->in_ready_state = false;
  515. queue->map->isReserved = false; // Remove reservation to free up for future queue
  516. queue->map = nullptr;
  517. queue->accepted_players = 0; // Reset the queue count
  518. std::string bg_name = battleground_db.find(queue->id)->name;
  519. for (const auto &sd : queue->teama_members) {
  520. sd->bg_queue_accept_state = false;
  521. clif_bg_queue_apply_result(BG_APPLY_QUEUE_FINISHED, bg_name.c_str(), sd);
  522. }
  523. for (const auto &sd : queue->teamb_members) {
  524. sd->bg_queue_accept_state = false;
  525. clif_bg_queue_apply_result(BG_APPLY_QUEUE_FINISHED, bg_name.c_str(), sd);
  526. }
  527. return 0;
  528. }
  529. /**
  530. * Start a Battleground
  531. * @param tid: Timer ID
  532. * @param tick: Timer
  533. * @param id: ID
  534. * @return 0 on success or 1 otherwise
  535. */
  536. static TIMER_FUNC(bg_on_ready_start)
  537. {
  538. s_battleground_queue *queue = (s_battleground_queue*)data;
  539. nullpo_retr(1, queue);
  540. bg_queue_start_battleground(std::shared_ptr<s_battleground_queue>(queue));
  541. return 0;
  542. }
  543. /**
  544. * Check if the given player is in a battleground
  545. * @param sd: Player data
  546. * @return True if in a battleground or false otherwise
  547. */
  548. bool bg_player_is_in_bg_map(struct map_session_data *sd)
  549. {
  550. nullpo_retr(false, sd);
  551. for (const auto &pair : battleground_db) {
  552. for (const auto &it : pair.second->maps) {
  553. if (it.mapid == sd->bl.m)
  554. return true;
  555. }
  556. }
  557. return false;
  558. }
  559. /**
  560. * Battleground status change check
  561. * @param sd: Player data
  562. * @param name: Battleground name
  563. * @return True if the player is good to join a queue or false otherwise
  564. */
  565. static bool bg_queue_check_status(struct map_session_data* sd, const char *name)
  566. {
  567. nullpo_retr(false, sd);
  568. if (sd->sc.count) {
  569. if (sd->sc.data[SC_ENTRY_QUEUE_APPLY_DELAY]) { // Exclude any player who's recently left a battleground queue
  570. char buf[CHAT_SIZE_MAX];
  571. sprintf(buf, msg_txt(sd, 339), (get_timer(sd->sc.data[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.
  572. clif_bg_queue_apply_result(BG_APPLY_NONE, name, sd);
  573. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], buf, false, SELF);
  574. return false;
  575. }
  576. if (sd->sc.data[SC_ENTRY_QUEUE_NOTIFY_ADMISSION_TIME_OUT]) { // Exclude any player who's recently deserted a battleground
  577. char buf[CHAT_SIZE_MAX];
  578. t_tick status_tick = get_timer(sd->sc.data[SC_ENTRY_QUEUE_NOTIFY_ADMISSION_TIME_OUT]->timer)->tick, tick = gettick();
  579. sprintf(buf, msg_txt(sd, 338), ((status_tick - tick) / 1000) / 60, ((status_tick - tick) / 1000) % 60); // You can't apply to a battleground queue due to recently deserting a battleground. Time remaining: %d minutes and %d seconds.
  580. clif_bg_queue_apply_result(BG_APPLY_NONE, name, sd);
  581. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], buf, false, SELF);
  582. return false;
  583. }
  584. }
  585. return true;
  586. }
  587. /**
  588. * Check to see if a Battleground is joinable
  589. * @param bg: Battleground data
  590. * @param sd: Player data
  591. * @param name: Battleground name
  592. * @return True on success or false otherwise
  593. */
  594. bool bg_queue_check_joinable(std::shared_ptr<s_battleground_type> bg, struct map_session_data *sd, const char *name)
  595. {
  596. nullpo_retr(false, sd);
  597. if (bg->min_lvl && sd->status.base_level < bg->min_lvl) { // Check min level if min_lvl isn't 0
  598. clif_bg_queue_apply_result(BG_APPLY_PLAYER_LEVEL, name, sd);
  599. return false;
  600. }
  601. if (bg->max_lvl && sd->status.base_level > bg->max_lvl) { // Check max level if max_lvl isn't 0
  602. clif_bg_queue_apply_result(BG_APPLY_PLAYER_LEVEL, name, sd);
  603. return false;
  604. }
  605. if (!bg_queue_check_status(sd, name))
  606. return false;
  607. if (bg_player_is_in_bg_map(sd)) { // Is the player currently in a battleground map? Reject them.
  608. clif_bg_queue_apply_result(BG_APPLY_NONE, name, sd);
  609. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], msg_txt(sd, 337), false, SELF); // You may not join a battleground queue when you're in a battleground map.
  610. return false;
  611. }
  612. return true; // Return true if all conditions are met.
  613. }
  614. /**
  615. * Sub function for reserving a slot in the Battleground if it's joinable
  616. * @param name: Battleground map name
  617. * @param state: Whether to mark reserved or not
  618. * @return True on success or false otherwise
  619. */
  620. bool bg_queue_reservation(const char *name, bool state)
  621. {
  622. int16 mapid = map_mapname2mapid(name);
  623. for (const auto &pair : battleground_db) {
  624. // Bound checking isn't needed since we iterate within battleground_db's bound.
  625. for (auto &it : pair.second->maps) {
  626. if (it.mapid == mapid) {
  627. it.isReserved = state;
  628. return true;
  629. }
  630. }
  631. }
  632. return false;
  633. }
  634. /**
  635. * Initialize a Battleground queue
  636. * @param bg_id: Battleground ID
  637. * @param req_players: Required amount of players
  638. * @return s_battleground_queue*
  639. */
  640. std::shared_ptr<s_battleground_queue> bg_queue_create(int bg_id, int req_players)
  641. {
  642. auto queue = std::make_shared<s_battleground_queue>();
  643. queue->id = bg_id;
  644. queue->required_players = req_players;
  645. queue->accepted_players = 0;
  646. queue->tid_expire = INVALID_TIMER;
  647. queue->tid_start = INVALID_TIMER;
  648. queue->tid_requeue = INVALID_TIMER;
  649. queue->in_ready_state = false;
  650. return queue;
  651. }
  652. /**
  653. * Allow a player to join a Battleground queue
  654. * @param name: Battleground name
  655. * @param sd: Player data
  656. * @return @see e_bg_queue_apply_ack
  657. */
  658. e_bg_queue_apply_ack bg_queue_join(const char *name, struct map_session_data *sd)
  659. {
  660. if (!sd) {
  661. ShowError("bg_queue_join: Tried to join non-existent player.\n.");
  662. return BG_APPLY_NONE;
  663. }
  664. if (!bg_queue_check_status(sd, name))
  665. return BG_APPLY_NONE;
  666. if (bg_player_is_in_bg_map(sd)) {
  667. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], msg_txt(sd, 337), false, SELF); // You may not join a battleground queue when you're in a battleground map.
  668. return BG_APPLY_NONE;
  669. }
  670. std::shared_ptr<s_battleground_type> bg = bg_search_name(name);
  671. if (!bg) {
  672. ShowWarning("bq_queue_join: 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);
  673. return BG_APPLY_INVALID_NAME;
  674. }
  675. if (bg->min_lvl && sd->status.base_level < bg->min_lvl)
  676. return BG_APPLY_PLAYER_LEVEL; // Level too low
  677. if (bg->max_lvl && sd->status.base_level > bg->max_lvl)
  678. return BG_APPLY_PLAYER_LEVEL; // Level too high
  679. std::shared_ptr<s_battleground_queue> queue;
  680. bool r;
  681. if (bg_queues.empty()) {
  682. r = rnd() % 2 != 0;
  683. queue = bg_queue_create(bg->id, bg->required_players);
  684. if (!r)
  685. queue->teama_members.push_back(sd);
  686. else
  687. queue->teamb_members.push_back(sd);
  688. sd->bg_queue = queue;
  689. bg_queues.insert(bg_queues.begin(), queue);
  690. return BG_APPLY_ACCEPT;
  691. } else {
  692. r = rnd() % 2 != 0;
  693. for (const auto &it : bg_queues) {
  694. try {
  695. queue = it;
  696. } catch (std::out_of_range &) {
  697. continue;
  698. }
  699. if (queue->in_ready_state)
  700. continue;
  701. if (!r) {
  702. if (queue->teama_members.size() != queue->required_players) {
  703. sd->bg_queue = queue;
  704. queue->teama_members.push_back(sd);
  705. if (queue->teama_members.size() == bg->required_players && queue->teamb_members.size() == bg->required_players) // Enough players have joined
  706. bg_queue_on_ready(name, queue);
  707. return BG_APPLY_ACCEPT;
  708. } else if (queue->teamb_members.size() != queue->required_players) {
  709. sd->bg_queue = queue;
  710. queue->teamb_members.push_back(sd);
  711. if (queue->teama_members.size() == bg->required_players && queue->teamb_members.size() == bg->required_players) // Enough players have joined
  712. bg_queue_on_ready(name, queue);
  713. return BG_APPLY_ACCEPT;
  714. }
  715. } else {
  716. if (queue->teamb_members.size() != queue->required_players) {
  717. sd->bg_queue = queue;
  718. queue->teamb_members.push_back(sd);
  719. if (queue->teama_members.size() == bg->required_players && queue->teamb_members.size() == bg->required_players) // Enough players have joined
  720. bg_queue_on_ready(name, queue);
  721. return BG_APPLY_ACCEPT;
  722. } else if (queue->teama_members.size() != queue->required_players) {
  723. sd->bg_queue = queue;
  724. queue->teama_members.push_back(sd);
  725. if (queue->teama_members.size() == bg->required_players && queue->teamb_members.size() == bg->required_players) // Enough players have joined
  726. bg_queue_on_ready(name, queue);
  727. return BG_APPLY_ACCEPT;
  728. }
  729. }
  730. }
  731. }
  732. queue = bg_queue_create(bg->id, bg->required_players);
  733. r = rnd() % 2 != 0;
  734. if (!r)
  735. queue->teama_members.push_back(sd);
  736. else
  737. queue->teamb_members.push_back(sd);
  738. sd->bg_queue = queue;
  739. bg_queues.insert(bg_queues.begin(), queue);
  740. return BG_APPLY_ACCEPT;
  741. }
  742. /**
  743. * Join a party onto the same side of a Battleground
  744. * @param name: Battleground name
  745. * @param sd: Player who requested to join the battlegrounds
  746. * @return @see e_bg_queue_apply_ack
  747. */
  748. e_bg_queue_apply_ack bg_queue_join_party(const char *name, struct map_session_data *sd)
  749. {
  750. struct party_data *p = party_search(sd->status.party_id);
  751. if (!p)
  752. return BG_APPLY_INVALID_APP; // Someone has bypassed the client check for being in a party
  753. for (const auto &it : p->party.member) {
  754. if (it.leader && sd->status.char_id != it.char_id)
  755. return BG_APPLY_PARTYGUILD_LEADER; // Not the party leader
  756. }
  757. std::shared_ptr<s_battleground_type> bg = bg_search_name(name);
  758. if (bg) {
  759. int p_online = 0;
  760. for (const auto &it : p->party.member) {
  761. if (it.online)
  762. p_online++;
  763. }
  764. if (p_online > bg->max_players)
  765. return BG_APPLY_PLAYER_COUNT; // Too many party members online
  766. std::vector<struct map_session_data *> list;
  767. for (const auto &it : p->party.member) {
  768. if (list.size() == bg->max_players)
  769. break;
  770. if (it.online) {
  771. struct map_session_data *pl_sd = map_charid2sd(it.char_id);
  772. if (pl_sd)
  773. list.push_back(pl_sd);
  774. }
  775. }
  776. return bg_queue_join_multi(name, sd, list); // Join as party, all on the same side of the BG
  777. } else {
  778. 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);
  779. return BG_APPLY_INVALID_NAME; // Invalid BG name
  780. }
  781. }
  782. /**
  783. * Join a guild onto the same side of a Battleground
  784. * @param name: Battleground name
  785. * @param sd: Player who requested to join the battlegrounds
  786. * @return @see e_bg_queue_apply_ack
  787. */
  788. e_bg_queue_apply_ack bg_queue_join_guild(const char *name, struct map_session_data *sd)
  789. {
  790. if (!sd->guild)
  791. return BG_APPLY_INVALID_APP; // Someone has bypassed the client check for being in a guild
  792. if (strcmp(sd->status.name, sd->guild->master) != 0)
  793. return BG_APPLY_PARTYGUILD_LEADER; // Not the guild leader
  794. std::shared_ptr<s_battleground_type> bg = bg_search_name(name);
  795. if (bg) {
  796. struct guild *g = guild_search(sd->status.guild_id);
  797. int g_online = 0;
  798. for (const auto &it : g->member) {
  799. if (it.online)
  800. g_online++;
  801. }
  802. if (g_online > bg->max_players)
  803. return BG_APPLY_PLAYER_COUNT; // Too many guild members online
  804. std::vector<struct map_session_data *> list;
  805. for (const auto &it : g->member) {
  806. if (list.size() == bg->max_players)
  807. break;
  808. if (it.online) {
  809. struct map_session_data *pl_sd = map_charid2sd(it.char_id);
  810. if (pl_sd)
  811. list.push_back(pl_sd);
  812. }
  813. }
  814. return bg_queue_join_multi(name, sd, list); // Join as guild, all on the same side of the BG
  815. } else {
  816. 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);
  817. return BG_APPLY_INVALID_NAME; // Invalid BG name
  818. }
  819. }
  820. /**
  821. * Join multiple players onto the same side of a Battleground
  822. * @param name: Battleground name
  823. * @param sd: Player who requested to join the battlegrounds
  824. * @param list: Contains all players including the player who requested to join
  825. * @return @see e_bg_queue_apply_ack
  826. */
  827. e_bg_queue_apply_ack bg_queue_join_multi(const char *name, struct map_session_data *sd, std::vector <map_session_data *> list)
  828. {
  829. if (!sd) {
  830. ShowError("bg_queue_join_multi: Tried to join non-existent player\n.");
  831. return BG_APPLY_NONE;
  832. }
  833. if (!bg_queue_check_status(sd, name))
  834. return BG_APPLY_NONE;
  835. if (bg_player_is_in_bg_map(sd)) {
  836. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], msg_txt(sd, 337), false, SELF); // You may not join a battleground queue when you're in a battleground map.
  837. return BG_APPLY_NONE;
  838. }
  839. std::shared_ptr<s_battleground_type> bg = bg_search_name(name);
  840. if (!bg) {
  841. 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);
  842. return BG_APPLY_INVALID_NAME;
  843. }
  844. if (bg->min_lvl && sd->status.base_level < bg->min_lvl)
  845. return BG_APPLY_PLAYER_LEVEL; // Level too low
  846. if (bg->max_lvl && sd->status.base_level > bg->max_lvl)
  847. return BG_APPLY_PLAYER_LEVEL; // Level too high
  848. if (bg_queues.empty()) {
  849. std::shared_ptr<s_battleground_queue> queue = bg_queue_create(bg->id, bg->required_players);
  850. bool r = rnd() % 2 != 0;
  851. if (!r) {
  852. while (!list.empty() && queue->teama_members.size() < bg->required_players) {
  853. struct map_session_data *sd2 = list.back();
  854. list.pop_back();
  855. if (!sd2 || sd2->bg_queue)
  856. continue;
  857. if (!bg_queue_check_joinable(bg, sd2, name))
  858. continue;
  859. sd2->bg_queue = queue;
  860. clif_bg_queue_apply_result(BG_APPLY_ACCEPT, name, sd2);
  861. clif_bg_queue_apply_notify(name, sd2);
  862. queue->teama_members.insert(queue->teama_members.begin(), sd2);
  863. }
  864. } else {
  865. while (!list.empty() && queue->teamb_members.size() < bg->required_players) {
  866. struct map_session_data *sd2 = list.back();
  867. list.pop_back();
  868. if (!sd2 || sd2->bg_queue)
  869. continue;
  870. if (!bg_queue_check_joinable(bg, sd2, name))
  871. continue;
  872. sd2->bg_queue = queue;
  873. clif_bg_queue_apply_result(BG_APPLY_ACCEPT, name, sd2);
  874. clif_bg_queue_apply_notify(name, sd2);
  875. queue->teamb_members.insert(queue->teamb_members.begin(), sd2);
  876. }
  877. }
  878. bg_queues.insert(bg_queues.begin(), queue);
  879. return BG_APPLY_ACCEPT;
  880. } else {
  881. std::shared_ptr<s_battleground_queue> queue;
  882. bool r = rnd() % 2 != 0;
  883. for (const auto &it : bg_queues) {
  884. try {
  885. queue = it;
  886. } catch (std::out_of_range &) {
  887. continue;
  888. }
  889. if (queue->in_ready_state)
  890. continue;
  891. if (queue->teama_members.size() + list.size() <= bg->required_players || queue->teamb_members.size() + list.size() <= bg->required_players) { // Make sure there's enough space on one side to join as a party/guild in this queue
  892. if (!r && queue->teama_members.size() + list.size() <= bg->required_players) {
  893. while (!list.empty() && queue->teama_members.size() < bg->required_players) {
  894. struct map_session_data *sd2 = list.back();
  895. list.pop_back();
  896. if (!sd2 || sd2->bg_queue)
  897. continue;
  898. if (!bg_queue_check_joinable(bg, sd2, name))
  899. continue;
  900. sd2->bg_queue = queue;
  901. clif_bg_queue_apply_result(BG_APPLY_ACCEPT, name, sd2);
  902. clif_bg_queue_apply_notify(name, sd2);
  903. queue->teama_members.insert(queue->teama_members.begin(), sd2);
  904. }
  905. if (queue->teama_members.size() == bg->required_players && queue->teamb_members.size() == bg->required_players) // Enough players have joined
  906. bg_queue_on_ready(name, queue);
  907. return BG_APPLY_ACCEPT;
  908. } else {
  909. while (!list.empty() && queue->teamb_members.size() < bg->required_players) {
  910. struct map_session_data *sd2 = list.back();
  911. list.pop_back();
  912. if (!sd2)
  913. continue;
  914. if (!bg_queue_check_joinable(bg, sd2, name))
  915. continue;
  916. sd2->bg_queue = queue;
  917. clif_bg_queue_apply_result(BG_APPLY_ACCEPT, name, sd2);
  918. clif_bg_queue_apply_notify(name, sd2);
  919. queue->teamb_members.insert(queue->teamb_members.begin(), sd2);
  920. }
  921. }
  922. return BG_APPLY_ACCEPT;
  923. }
  924. }
  925. // Create a new queue if none of the existing ones are joinable for this party/guild
  926. queue = nullptr;
  927. queue = bg_queue_create(bg->id, bg->required_players);
  928. r = rnd() % 2 != 0;
  929. if (!r) {
  930. while (!list.empty() && queue->teama_members.size() < bg->required_players) {
  931. struct map_session_data *sd2 = list.back();
  932. list.pop_back();
  933. if (!sd2 || sd2->bg_queue)
  934. continue;
  935. if (!bg_queue_check_joinable(bg, sd2, name))
  936. continue;
  937. sd2->bg_queue = queue;
  938. clif_bg_queue_apply_result(BG_APPLY_ACCEPT, name, sd2);
  939. clif_bg_queue_apply_notify(name, sd2);
  940. queue->teama_members.insert(queue->teama_members.begin(), sd2);
  941. }
  942. } else {
  943. while (!list.empty() && queue->teamb_members.size() < bg->required_players) {
  944. struct map_session_data *sd2 = list.back();
  945. list.pop_back();
  946. if (!sd2 || sd2->bg_queue)
  947. continue;
  948. if (!bg_queue_check_joinable(bg, sd2, name))
  949. continue;
  950. sd2->bg_queue = queue;
  951. clif_bg_queue_apply_result(BG_APPLY_ACCEPT, name, sd2);
  952. clif_bg_queue_apply_notify(name, sd2);
  953. queue->teamb_members.insert(queue->teamb_members.begin(), sd2);
  954. }
  955. }
  956. bg_queues.insert(bg_queues.begin(), queue);
  957. return BG_APPLY_ACCEPT;
  958. }
  959. return BG_APPLY_RECONNECT; // Something went wrong, sends reconnect and then reapply message to client.
  960. }
  961. /**
  962. * Sub function for leaving a Battleground queue
  963. * @param sd: Player leaving
  964. * @param lista: List of players in queue data
  965. * @param listb: List of players in second queue data
  966. * @return True on success or false otherwise
  967. */
  968. static bool bg_queue_leave_sub(struct map_session_data *sd, std::vector<map_session_data *> lista, std::vector<map_session_data *> listb)
  969. {
  970. if (!sd)
  971. return false;
  972. auto list_it = lista.begin();
  973. while (list_it != lista.end()) {
  974. struct map_session_data *player = *list_it;
  975. if (player == sd) {
  976. if (sd->bg_queue->in_ready_state) {
  977. sd->bg_queue->accepted_players = 0;
  978. sd->bg_queue->in_ready_state = false;
  979. sd->bg_queue_accept_state = false;
  980. }
  981. list_it = lista.erase(list_it);
  982. if (lista.empty() && listb.empty()) { // If there are no players left in the queue, discard it
  983. auto queue_it = bg_queues.begin();
  984. while (queue_it != bg_queues.end()) {
  985. std::shared_ptr<s_battleground_queue> q = *queue_it;
  986. if (sd->bg_queue == q) {
  987. if (q->tid_requeue != INVALID_TIMER && get_timer(q->tid_requeue)) {
  988. delete_timer(q->tid_requeue, bg_on_ready_loopback);
  989. q->tid_requeue = INVALID_TIMER;
  990. }
  991. queue_it = bg_queues.erase(queue_it);
  992. }
  993. }
  994. }
  995. sc_start(nullptr, &sd->bl, SC_ENTRY_QUEUE_APPLY_DELAY, 100, 1, 60000);
  996. sd->bg_queue = nullptr;
  997. return true;
  998. } else {
  999. list_it++;
  1000. }
  1001. }
  1002. return false;
  1003. }
  1004. /**
  1005. * Leave a Battleground queue
  1006. * @param sd: Player data
  1007. * @return True on success or false otherwise
  1008. */
  1009. bool bg_queue_leave(struct map_session_data *sd)
  1010. {
  1011. if (!sd || !sd->bg_queue)
  1012. return false;
  1013. if (!bg_queue_leave_sub(sd, sd->bg_queue->teama_members, sd->bg_queue->teamb_members) && !bg_queue_leave_sub(sd, sd->bg_queue->teamb_members, sd->bg_queue->teama_members)) {
  1014. ShowError("bg_queue_leave: Couldn't find player %s in battlegrounds queue.\n", sd->status.name);
  1015. return false;
  1016. } else
  1017. return true;
  1018. }
  1019. /**
  1020. * Send packets to all clients in queue to notify them that the battleground is ready to be joined
  1021. * @param name: Battleground name
  1022. * @param queue: Battleground queue
  1023. * @return True on success or false otherwise
  1024. */
  1025. bool bg_queue_on_ready(const char *name, std::shared_ptr<s_battleground_queue> queue)
  1026. {
  1027. std::shared_ptr<s_battleground_type> bg = battleground_db.find(queue->id);
  1028. if (!bg) {
  1029. ShowError("bg_queue_on_ready: Couldn't find battleground ID %d in battlegrounds database.\n", queue->id);
  1030. return false;
  1031. }
  1032. queue->accepted_players = 0; // Reset the counter just in case.
  1033. if (queue->teama_members.size() != queue->required_players || queue->teamb_members.size() != queue->required_players)
  1034. return false; // Return players to the queue and stop reapplying the timer
  1035. s_battleground_map *bgmap = nullptr;
  1036. for (auto &it : bg->maps) {
  1037. if (!it.isReserved) {
  1038. it.isReserved = true;
  1039. bgmap = &it;
  1040. queue->map = &it;
  1041. break;
  1042. }
  1043. }
  1044. if (!bgmap) { // All the battleground maps are reserved. Set a timer to check for an open battleground every 10 seconds.
  1045. queue->tid_requeue = add_timer(gettick() + 10000, bg_on_ready_loopback, 0, (intptr_t)queue.get());
  1046. return false;
  1047. }
  1048. queue->in_ready_state = true;
  1049. queue->tid_expire = add_timer(gettick() + 20000, bg_on_ready_expire, 0, (intptr_t)queue.get());
  1050. for (const auto &sd : queue->teama_members)
  1051. clif_bg_queue_lobby_notify(name, sd);
  1052. for (const auto &sd : queue->teamb_members)
  1053. clif_bg_queue_lobby_notify(name, sd);
  1054. return true;
  1055. }
  1056. /**
  1057. * Update the Battleground queue when the player accepts the invite
  1058. * @param queue: Battleground queue
  1059. * @param sd: Player data
  1060. */
  1061. void bg_queue_on_accept_invite(std::shared_ptr<s_battleground_queue> queue, struct map_session_data *sd)
  1062. {
  1063. nullpo_retv(sd);
  1064. sd->bg_queue_accept_state = true;
  1065. queue->accepted_players++;
  1066. clig_bg_queue_ack_lobby(true, map_mapid2mapname(queue->map->mapid), map_mapid2mapname(queue->map->mapid), sd);
  1067. if (queue->accepted_players == queue->required_players * 2) {
  1068. queue->tid_start = add_timer(gettick() + battleground_db.find(queue->id)->start_delay * 1000, bg_on_ready_start, 0, (intptr_t)queue.get());
  1069. if (queue->tid_expire != INVALID_TIMER && get_timer(queue->tid_expire)) {
  1070. delete_timer(queue->tid_expire, bg_on_ready_expire);
  1071. queue->tid_expire = INVALID_TIMER;
  1072. }
  1073. }
  1074. }
  1075. /**
  1076. * Begin the Battleground from the given queue
  1077. * @param queue: Battleground queue
  1078. */
  1079. void bg_queue_start_battleground(std::shared_ptr<s_battleground_queue> queue)
  1080. {
  1081. if (queue->tid_start != INVALID_TIMER && get_timer(queue->tid_start)) {
  1082. delete_timer(queue->tid_start, bg_on_ready_start);
  1083. queue->tid_start = INVALID_TIMER;
  1084. }
  1085. std::shared_ptr<s_battleground_type> bg = battleground_db.find(queue->id);
  1086. if (!bg) {
  1087. queue->map->isReserved = false; // Remove reservation to free up for future queue
  1088. queue->map = nullptr;
  1089. ShowError("bg_queue_start_battleground: Could not find battleground ID %d in battlegrounds database.\n", queue->id);
  1090. return;
  1091. }
  1092. uint16 map_idx = map_id2index(queue->map->mapid);
  1093. int bg_team_1 = bg_create(map_idx, &queue->map->team1);
  1094. int bg_team_2 = bg_create(map_idx, &queue->map->team2);
  1095. for (const auto &sd : queue->teama_members) {
  1096. sd->bg_queue = nullptr;
  1097. sd->bg_queue_accept_state = false;
  1098. clif_bg_queue_entry_init(sd);
  1099. bg_team_join(bg_team_1, sd, true);
  1100. }
  1101. for (const auto &sd : queue->teamb_members) {
  1102. sd->bg_queue = nullptr;
  1103. sd->bg_queue_accept_state = false;
  1104. clif_bg_queue_entry_init(sd);
  1105. bg_team_join(bg_team_2, sd, true);
  1106. }
  1107. mapreg_setreg(add_str(queue->map->team1.bg_id_var.c_str()), bg_team_1);
  1108. mapreg_setreg(add_str(queue->map->team2.bg_id_var.c_str()), bg_team_2);
  1109. npc_event_do(queue->map->bgcallscript.c_str());
  1110. queue->teama_members.clear();
  1111. queue->teamb_members.clear();
  1112. queue->teama_members.shrink_to_fit();
  1113. queue->teamb_members.shrink_to_fit();
  1114. auto queue_it = bg_queues.begin();
  1115. while (queue_it != bg_queues.end()) {
  1116. if (*queue_it == queue)
  1117. queue_it = bg_queues.erase(queue_it);
  1118. }
  1119. return;
  1120. }
  1121. /**
  1122. * Initialize the Battleground data
  1123. */
  1124. void do_init_battleground(void)
  1125. {
  1126. if (battle_config.feature_bgqueue)
  1127. battleground_db.load();
  1128. add_timer_func_list(bg_send_xy_timer, "bg_send_xy_timer");
  1129. add_timer_func_list(bg_on_ready_loopback, "bg_on_ready_loopback");
  1130. add_timer_func_list(bg_on_ready_expire, "bg_on_ready_expire");
  1131. add_timer_func_list(bg_on_ready_start, "bg_on_ready_start");
  1132. add_timer_interval(gettick() + battle_config.bg_update_interval, bg_send_xy_timer, 0, 0, battle_config.bg_update_interval);
  1133. }
  1134. /**
  1135. * Clear the Battleground data from memory
  1136. */
  1137. void do_final_battleground(void)
  1138. {
  1139. }