battleground.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. // Copyright (c) Athena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #include "../common/cbasetypes.h"
  4. #include "../common/timer.h"
  5. #include "../common/malloc.h"
  6. #include "../common/nullpo.h"
  7. #include "../common/showmsg.h"
  8. #include "../common/socket.h"
  9. #include "../common/strlib.h"
  10. #include "battleground.h"
  11. #include "battle.h"
  12. #include "clif.h"
  13. #include "map.h"
  14. #include "npc.h"
  15. #include "pc.h"
  16. #include "pet.h"
  17. #include "homunculus.h"
  18. #include "mercenary.h"
  19. #include <string.h>
  20. #include <stdio.h>
  21. static DBMap* bg_team_db; // int bg_id -> struct battleground_data*
  22. static unsigned int bg_team_counter = 0; // Next bg_id
  23. struct battleground_data* bg_team_search(int bg_id)
  24. { // Search a BG Team using bg_id
  25. if( !bg_id ) return NULL;
  26. return (struct battleground_data *)idb_get(bg_team_db, bg_id);
  27. }
  28. struct map_session_data* bg_getavailablesd(struct battleground_data *bg)
  29. {
  30. int i;
  31. nullpo_retr(NULL, bg);
  32. ARR_FIND(0, MAX_BG_MEMBERS, i, bg->members[i].sd != NULL);
  33. return( i < MAX_BG_MEMBERS ) ? bg->members[i].sd : NULL;
  34. }
  35. int bg_team_delete(int bg_id)
  36. { // Deletes BG Team from db
  37. int i;
  38. struct map_session_data *sd;
  39. struct battleground_data *bg = bg_team_search(bg_id);
  40. if( bg == NULL ) return 0;
  41. for( i = 0; i < MAX_BG_MEMBERS; i++ )
  42. {
  43. if( (sd = bg->members[i].sd) == NULL )
  44. continue;
  45. bg_send_dot_remove(sd);
  46. sd->bg_id = 0;
  47. }
  48. idb_remove(bg_team_db, bg_id);
  49. return 1;
  50. }
  51. int bg_team_warp(int bg_id, unsigned short mapindex, short x, short y)
  52. { // Warps a Team
  53. int i;
  54. struct battleground_data *bg = bg_team_search(bg_id);
  55. if( bg == NULL ) return 0;
  56. for( i = 0; i < MAX_BG_MEMBERS; i++ )
  57. if( bg->members[i].sd != NULL ) pc_setpos(bg->members[i].sd, mapindex, x, y, CLR_TELEPORT);
  58. return 1;
  59. }
  60. int bg_send_dot_remove(struct map_session_data *sd)
  61. {
  62. if( sd && sd->bg_id )
  63. clif_bg_xy_remove(sd);
  64. return 0;
  65. }
  66. int bg_team_join(int bg_id, struct map_session_data *sd)
  67. { // Player joins team
  68. int i;
  69. struct battleground_data *bg = bg_team_search(bg_id);
  70. struct map_session_data *pl_sd;
  71. if( bg == NULL || sd == NULL || sd->bg_id ) return 0;
  72. ARR_FIND(0, MAX_BG_MEMBERS, i, bg->members[i].sd == NULL);
  73. if( i == MAX_BG_MEMBERS ) return 0; // No free slots
  74. sd->bg_id = bg_id;
  75. bg->members[i].sd = sd;
  76. bg->members[i].x = sd->bl.x;
  77. bg->members[i].y = sd->bl.y;
  78. bg->count++;
  79. guild_send_dot_remove(sd);
  80. for( i = 0; i < MAX_BG_MEMBERS; i++ )
  81. {
  82. if( (pl_sd = bg->members[i].sd) != NULL && pl_sd != sd )
  83. clif_hpmeter_single(sd->fd, pl_sd->bl.id, pl_sd->battle_status.hp, pl_sd->battle_status.max_hp);
  84. }
  85. clif_bg_hp(sd);
  86. clif_bg_xy(sd);
  87. return 1;
  88. }
  89. int bg_team_leave(struct map_session_data *sd, int flag)
  90. { // Single Player leaves team
  91. int i, bg_id;
  92. struct battleground_data *bg;
  93. char output[128];
  94. if( sd == NULL || !sd->bg_id )
  95. return 0;
  96. bg_send_dot_remove(sd);
  97. bg_id = sd->bg_id;
  98. sd->bg_id = 0;
  99. if( (bg = bg_team_search(bg_id)) == NULL )
  100. return 0;
  101. ARR_FIND(0, MAX_BG_MEMBERS, i, bg->members[i].sd == sd);
  102. if( i < MAX_BG_MEMBERS ) // Removes member from BG
  103. memset(&bg->members[i], 0, sizeof(bg->members[0]));
  104. bg->count--;
  105. if( flag )
  106. sprintf(output, "Server : %s has quit the game...", sd->status.name);
  107. else
  108. sprintf(output, "Server : %s is leaving the battlefield...", sd->status.name);
  109. clif_bg_message(bg, 0, "Server", output, strlen(output) + 1);
  110. if( bg->logout_event[0] && flag )
  111. npc_event(sd, bg->logout_event, 0);
  112. return bg->count;
  113. }
  114. int bg_member_respawn(struct map_session_data *sd)
  115. { // Respawn after killed
  116. struct battleground_data *bg;
  117. if( sd == NULL || !pc_isdead(sd) || !sd->bg_id || (bg = bg_team_search(sd->bg_id)) == NULL )
  118. return 0;
  119. if( bg->mapindex == 0 )
  120. return 0; // Respawn not handled by Core
  121. pc_setpos(sd, bg->mapindex, bg->x, bg->y, CLR_OUTSIGHT);
  122. status_revive(&sd->bl, 1, 100);
  123. return 1; // Warped
  124. }
  125. int bg_create(unsigned short mapindex, short rx, short ry, const char *ev, const char *dev)
  126. {
  127. struct battleground_data *bg;
  128. bg_team_counter++;
  129. CREATE(bg, struct battleground_data, 1);
  130. bg->bg_id = bg_team_counter;
  131. bg->count = 0;
  132. bg->mapindex = mapindex;
  133. bg->x = rx;
  134. bg->y = ry;
  135. safestrncpy(bg->logout_event, ev, sizeof(bg->logout_event));
  136. safestrncpy(bg->die_event, dev, sizeof(bg->die_event));
  137. memset(&bg->members, 0, sizeof(bg->members));
  138. idb_put(bg_team_db, bg_team_counter, bg);
  139. return bg->bg_id;
  140. }
  141. int bg_team_get_id(struct block_list *bl)
  142. {
  143. nullpo_ret(bl);
  144. switch( bl->type )
  145. {
  146. case BL_PC:
  147. return ((TBL_PC*)bl)->bg_id;
  148. case BL_PET:
  149. if( ((TBL_PET*)bl)->msd )
  150. return ((TBL_PET*)bl)->msd->bg_id;
  151. break;
  152. case BL_MOB:
  153. {
  154. struct map_session_data *msd;
  155. struct mob_data *md = (TBL_MOB*)bl;
  156. if( md->special_state.ai && (msd = map_id2sd(md->master_id)) != NULL )
  157. return msd->bg_id;
  158. return md->bg_id;
  159. }
  160. case BL_HOM:
  161. if( ((TBL_HOM*)bl)->master )
  162. return ((TBL_HOM*)bl)->master->bg_id;
  163. break;
  164. case BL_MER:
  165. if( ((TBL_MER*)bl)->master )
  166. return ((TBL_MER*)bl)->master->bg_id;
  167. break;
  168. case BL_SKILL:
  169. return ((TBL_SKILL*)bl)->group->bg_id;
  170. }
  171. return 0;
  172. }
  173. int bg_send_message(struct map_session_data *sd, const char *mes, int len)
  174. {
  175. struct battleground_data *bg;
  176. nullpo_ret(sd);
  177. if( sd->bg_id == 0 || (bg = bg_team_search(sd->bg_id)) == NULL )
  178. return 0;
  179. clif_bg_message(bg, sd->bl.id, sd->status.name, mes, len);
  180. return 0;
  181. }
  182. /**
  183. * @see DBApply
  184. */
  185. int bg_send_xy_timer_sub(DBKey key, DBData *data, va_list ap)
  186. {
  187. struct battleground_data *bg = db_data2ptr(data);
  188. struct map_session_data *sd;
  189. int i;
  190. nullpo_ret(bg);
  191. for( i = 0; i < MAX_BG_MEMBERS; i++ )
  192. {
  193. if( (sd = bg->members[i].sd) == NULL )
  194. continue;
  195. if( sd->bl.x != bg->members[i].x || sd->bl.y != bg->members[i].y )
  196. { // xy update
  197. bg->members[i].x = sd->bl.x;
  198. bg->members[i].y = sd->bl.y;
  199. clif_bg_xy(sd);
  200. }
  201. }
  202. return 0;
  203. }
  204. int bg_send_xy_timer(int tid, unsigned int tick, int id, intptr_t data)
  205. {
  206. bg_team_db->foreach(bg_team_db, bg_send_xy_timer_sub, tick);
  207. return 0;
  208. }
  209. void do_init_battleground(void)
  210. {
  211. bg_team_db = idb_alloc(DB_OPT_RELEASE_DATA);
  212. add_timer_func_list(bg_send_xy_timer, "bg_send_xy_timer");
  213. add_timer_interval(gettick() + battle_config.bg_update_interval, bg_send_xy_timer, 0, 0, battle_config.bg_update_interval);
  214. }
  215. void do_final_battleground(void)
  216. {
  217. bg_team_db->destroy(bg_team_db, NULL);
  218. }