party.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998
  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/socket.h" // last_tick
  6. #include "../common/nullpo.h"
  7. #include "../common/malloc.h"
  8. #include "../common/showmsg.h"
  9. #include "../common/utils.h"
  10. #include "../common/strlib.h"
  11. #include "party.h"
  12. #include "atcommand.h" //msg_txt()
  13. #include "pc.h"
  14. #include "map.h"
  15. #include "battle.h"
  16. #include "intif.h"
  17. #include "clif.h"
  18. #include "log.h"
  19. #include "skill.h"
  20. #include "status.h"
  21. #include "itemdb.h"
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. static DBMap* party_db; // int party_id -> struct party_data*
  26. int party_send_xy_timer(int tid, unsigned int tick, int id, intptr data);
  27. /*==========================================
  28. * Fills the given party_member structure according to the sd provided.
  29. * Used when creating/adding people to a party. [Skotlex]
  30. *------------------------------------------*/
  31. static void party_fill_member(struct party_member *member, struct map_session_data *sd)
  32. {
  33. member->account_id = sd->status.account_id;
  34. member->char_id = sd->status.char_id;
  35. memcpy(member->name, sd->status.name, NAME_LENGTH);
  36. member->class_ = sd->status.class_;
  37. member->map = sd->mapindex;
  38. member->lv = sd->status.base_level;
  39. member->online = 1;
  40. member->leader = 0;
  41. }
  42. /*==========================================
  43. * Request an available sd of this party
  44. *------------------------------------------*/
  45. struct map_session_data* party_getavailablesd(struct party_data *p)
  46. {
  47. int i;
  48. nullpo_retr(NULL, p);
  49. ARR_FIND(0, MAX_PARTY, i, p->data[i].sd != NULL);
  50. return( i < MAX_PARTY ) ? p->data[i].sd : NULL;
  51. }
  52. /*==========================================
  53. * Retrieves and validates the sd pointer for this party member [Skotlex]
  54. *------------------------------------------*/
  55. static TBL_PC* party_sd_check(int party_id, int account_id, int char_id)
  56. {
  57. TBL_PC* sd = map_id2sd(account_id);
  58. if (!(sd && sd->status.char_id == char_id))
  59. return NULL;
  60. if (sd->status.party_id != party_id)
  61. { //If player belongs to a different party, kick him out.
  62. intif_party_leave(party_id,account_id,char_id);
  63. return NULL;
  64. }
  65. return sd;
  66. }
  67. /*==========================================
  68. * �I—¹
  69. *------------------------------------------*/
  70. void do_final_party(void)
  71. {
  72. party_db->destroy(party_db,NULL);
  73. }
  74. // �‰Šú‰»
  75. void do_init_party(void)
  76. {
  77. party_db = idb_alloc(DB_OPT_RELEASE_DATA);
  78. add_timer_func_list(party_send_xy_timer, "party_send_xy_timer");
  79. add_timer_interval(gettick()+battle_config.party_update_interval, party_send_xy_timer, 0, 0, battle_config.party_update_interval);
  80. }
  81. /// Party data lookup using party id.
  82. struct party_data* party_search(int party_id)
  83. {
  84. if(!party_id)
  85. return NULL;
  86. return (struct party_data*)idb_get(party_db,party_id);
  87. }
  88. /// Party data lookup using party name.
  89. struct party_data* party_searchname(const char* str)
  90. {
  91. struct party_data* p;
  92. DBIterator* iter = party_db->iterator(party_db);
  93. for( p = (struct party_data*)iter->first(iter,NULL); iter->exists(iter); p = (struct party_data*)iter->next(iter,NULL) )
  94. {
  95. if( strncmpi(p->party.name,str,NAME_LENGTH) == 0 )
  96. break;
  97. }
  98. iter->destroy(iter);
  99. return p;
  100. }
  101. int party_create(struct map_session_data *sd,char *name,int item,int item2)
  102. {
  103. struct party_member leader;
  104. char tname[NAME_LENGTH];
  105. safestrncpy(tname, name, NAME_LENGTH);
  106. if( strlen(trim(tname)) == 0 )
  107. {// empty name
  108. return 0;
  109. }
  110. if( sd->status.party_id > 0 || sd->party_joining || sd->party_creating )
  111. {// already associated with a party
  112. clif_party_created(sd,2);
  113. return 0;
  114. }
  115. sd->party_creating = true;
  116. party_fill_member(&leader, sd);
  117. leader.leader = 1;
  118. intif_create_party(&leader,name,item,item2);
  119. return 0;
  120. }
  121. void party_created(int account_id,int char_id,int fail,int party_id,char *name)
  122. {
  123. struct map_session_data *sd;
  124. sd=map_id2sd(account_id);
  125. if (!sd || sd->status.char_id != char_id || !sd->party_creating )
  126. { //Character logged off before creation ack?
  127. if (!fail) //break up party since player could not be added to it.
  128. intif_party_leave(party_id,account_id,char_id);
  129. return;
  130. }
  131. sd->party_creating = false;
  132. if( !fail ) {
  133. sd->status.party_id = party_id;
  134. clif_party_created(sd,0); //Success message
  135. //We don't do any further work here because the char-server sends a party info packet right after creating the party.
  136. } else {
  137. clif_party_created(sd,1); // "party name already exists"
  138. }
  139. }
  140. int party_request_info(int party_id)
  141. {
  142. return intif_request_partyinfo(party_id);
  143. }
  144. /// Checks if each char having a party actually belongs to that party.
  145. /// If check fails, the char gets marked as 'not in a party'.
  146. int party_check_member(struct party *p)
  147. {
  148. int i;
  149. struct map_session_data *sd;
  150. struct s_mapiterator* iter;
  151. nullpo_retr(0, p);
  152. iter = mapit_getallusers();
  153. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  154. {
  155. if( sd->status.party_id != p->party_id )
  156. continue;
  157. ARR_FIND( 0, MAX_PARTY, i, p->member[i].account_id == sd->status.account_id && p->member[i].char_id == sd->status.char_id );
  158. if( i == MAX_PARTY )
  159. {
  160. ShowWarning("party_check_member: '%s' (acc:%d) is not member of party '%s' (id:%d)\n",sd->status.name,sd->status.account_id,p->name,p->party_id);
  161. sd->status.party_id = 0;
  162. }
  163. }
  164. mapit_free(iter);
  165. return 0;
  166. }
  167. /// Marks all chars belonging to this party as 'not in a party'.
  168. int party_recv_noinfo(int party_id)
  169. {
  170. struct map_session_data *sd;
  171. struct s_mapiterator* iter;
  172. iter = mapit_getallusers();
  173. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  174. {
  175. if( sd->status.party_id == party_id )
  176. sd->status.party_id = 0; // erase party
  177. }
  178. mapit_free(iter);
  179. return 0;
  180. }
  181. static void* create_party(DBKey key, va_list args)
  182. {
  183. struct party_data *p;
  184. p=(struct party_data *)aCalloc(1,sizeof(struct party_data));
  185. return p;
  186. }
  187. static void party_check_state(struct party_data *p)
  188. {
  189. int i;
  190. memset(&p->state, 0, sizeof(p->state));
  191. for (i = 0; i < MAX_PARTY; i ++)
  192. {
  193. if (!p->party.member[i].online) continue; //Those not online shouldn't aport to skill usage and all that.
  194. switch (p->party.member[i].class_) {
  195. case JOB_MONK:
  196. case JOB_BABY_MONK:
  197. case JOB_CHAMPION:
  198. p->state.monk = 1;
  199. break;
  200. case JOB_STAR_GLADIATOR:
  201. p->state.sg = 1;
  202. break;
  203. case JOB_SUPER_NOVICE:
  204. case JOB_SUPER_BABY:
  205. p->state.snovice = 1;
  206. break;
  207. case JOB_TAEKWON:
  208. p->state.tk = 1;
  209. break;
  210. }
  211. }
  212. }
  213. int party_recv_info(struct party *sp)
  214. {
  215. struct party_data *p;
  216. int i;
  217. bool party_new = false;
  218. nullpo_retr(0, sp);
  219. p = (struct party_data*)idb_ensure(party_db, sp->party_id, create_party);
  220. if (!p->party.party_id) //party just received.
  221. {
  222. party_new = true;
  223. party_check_member(sp);
  224. }
  225. memcpy(&p->party,sp,sizeof(struct party));
  226. memset(&p->state, 0, sizeof(p->state));
  227. memset(&p->data, 0, sizeof(p->data));
  228. for(i=0;i<MAX_PARTY;i++){
  229. if (!p->party.member[i].account_id)
  230. continue;
  231. p->data[i].sd = party_sd_check(p->party.party_id, p->party.member[i].account_id, p->party.member[i].char_id);
  232. }
  233. party_check_state(p);
  234. if (party_new) {
  235. //Send party data to all players.
  236. struct map_session_data *sd;
  237. for(i=0;i<MAX_PARTY;i++){
  238. sd = p->data[i].sd;
  239. if(!sd) continue;
  240. clif_charnameupdate(sd); //Update other people's display. [Skotlex]
  241. clif_party_member_info(p,sd);
  242. clif_party_option(p,sd,0x100);
  243. clif_party_info(p,NULL);
  244. }
  245. }
  246. return 0;
  247. }
  248. int party_invite(struct map_session_data *sd,struct map_session_data *tsd)
  249. {
  250. struct party_data *p=party_search(sd->status.party_id);
  251. int i,flag=0;
  252. nullpo_retr(0, sd);
  253. if( p == NULL )
  254. return 0;
  255. if( tsd == NULL) { //TODO: Find the correct reply packet.
  256. clif_displaymessage(sd->fd, msg_txt(3));
  257. return 0;
  258. }
  259. if ( (pc_isGM(sd) > battle_config.lowest_gm_level && pc_isGM(tsd) < battle_config.lowest_gm_level && !battle_config.gm_can_party && pc_isGM(sd) < battle_config.gm_cant_party_min_lv)
  260. || ( pc_isGM(sd) < battle_config.lowest_gm_level && pc_isGM(tsd) > battle_config.lowest_gm_level && !battle_config.gm_can_party && pc_isGM(tsd) < battle_config.gm_cant_party_min_lv) )
  261. {
  262. //GMs can't invite non GMs to the party if not above the invite trust level
  263. //Likewise, as long as gm_can_party is off, players can't invite GMs.
  264. clif_displaymessage(sd->fd, msg_txt(81));
  265. return 0;
  266. }
  267. //Only leader can invite.
  268. ARR_FIND(0, MAX_PARTY, i, p->data[i].sd == sd);
  269. if (i == MAX_PARTY || !p->party.member[i].leader)
  270. { //TODO: Find the correct reply packet.
  271. clif_displaymessage(sd->fd, msg_txt(282));
  272. return 0;
  273. }
  274. if(!battle_config.invite_request_check) {
  275. if (tsd->guild_invite>0 || tsd->trade_partner || tsd->adopt_invite) {
  276. clif_party_inviteack(sd,tsd->status.name,0);
  277. return 0;
  278. }
  279. }
  280. if (!tsd->fd) { //You can't invite someone who has already disconnected.
  281. clif_party_inviteack(sd,tsd->status.name,1);
  282. return 0;
  283. }
  284. if( tsd->status.party_id > 0 || tsd->party_invite > 0 )
  285. {// already associated with a party
  286. clif_party_inviteack(sd,tsd->status.name,0);
  287. return 0;
  288. }
  289. for(i=0;i<MAX_PARTY;i++){
  290. if(p->party.member[i].account_id == 0) //Room for a new member.
  291. flag = 1;
  292. /* By default Aegis BLOCKS more than one char from the same account on a party.
  293. * But eA does support it... so this check is left commented.
  294. if(p->party.member[i].account_id==tsd->status.account_id)
  295. {
  296. clif_party_inviteack(sd,tsd->status.name,4);
  297. return 0;
  298. }
  299. */
  300. }
  301. if (!flag) { //Full party.
  302. clif_party_inviteack(sd,tsd->status.name,3);
  303. return 0;
  304. }
  305. tsd->party_invite=sd->status.party_id;
  306. tsd->party_invite_account=sd->status.account_id;
  307. clif_party_invite(sd,tsd);
  308. return 1;
  309. }
  310. void party_reply_invite(struct map_session_data *sd,int account_id,int flag)
  311. {
  312. struct map_session_data *tsd= map_id2sd(account_id);
  313. struct party_member member;
  314. if( flag == 1 && !sd->party_creating && !sd->party_joining )
  315. {// accepted and allowed
  316. sd->party_joining = true;
  317. party_fill_member(&member, sd);
  318. intif_party_addmember(sd->party_invite, &member);
  319. }
  320. else
  321. {// rejected or failure
  322. sd->party_invite = 0;
  323. sd->party_invite_account = 0;
  324. if( tsd != NULL )
  325. clif_party_inviteack(tsd,sd->status.name,1);
  326. }
  327. }
  328. //Invoked when a player joins:
  329. //- Loads up party data if not in server
  330. //- Sets up the pointer to him
  331. //- Player must be authed/active and belong to a party before calling this method
  332. void party_member_joined(struct map_session_data *sd)
  333. {
  334. struct party_data* p = party_search(sd->status.party_id);
  335. int i;
  336. if (!p)
  337. {
  338. party_request_info(sd->status.party_id);
  339. return;
  340. }
  341. ARR_FIND( 0, MAX_PARTY, i, p->party.member[i].account_id == sd->status.account_id && p->party.member[i].char_id == sd->status.char_id );
  342. if (i < MAX_PARTY)
  343. {
  344. p->data[i].sd = sd;
  345. if( p->instance_id )
  346. clif_instance_join(sd->fd,p->instance_id);
  347. }
  348. else
  349. sd->status.party_id = 0; //He does not belongs to the party really?
  350. }
  351. /// Invoked (from char-server) when a new member is added to the party.
  352. /// flag: 0-success, 1-failure
  353. int party_member_added(int party_id,int account_id,int char_id, int flag)
  354. {
  355. struct map_session_data *sd = map_id2sd(account_id),*sd2;
  356. struct party_data *p = party_search(party_id);
  357. int i;
  358. if(sd == NULL || sd->status.char_id != char_id || !sd->party_joining ) {
  359. if (!flag) //Char logged off before being accepted into party.
  360. intif_party_leave(party_id,account_id,char_id);
  361. return 0;
  362. }
  363. sd2 = map_id2sd(sd->party_invite_account);
  364. sd->party_joining = false;
  365. sd->party_invite = 0;
  366. sd->party_invite_account = 0;
  367. if (!p) {
  368. ShowError("party_member_added: party %d not found.\n",party_id);
  369. intif_party_leave(party_id,account_id,char_id);
  370. return 0;
  371. }
  372. if( flag )
  373. {// failed
  374. if( sd2 != NULL )
  375. clif_party_inviteack(sd2,sd->status.name,3);
  376. return 0;
  377. }
  378. sd->status.party_id = party_id;
  379. ARR_FIND( 0, MAX_PARTY, i, p->party.member[i].account_id == 0 );
  380. if (i < MAX_PARTY) {
  381. //TODO: This is a hack to allow the following clif calls to send the data to the new player.
  382. //The correct player data is set when party_recv_info arrives soon afterwards.
  383. party_fill_member(&p->party.member[i], sd);
  384. p->data[i].sd = sd;
  385. }
  386. clif_party_member_info(p,sd);
  387. clif_party_option(p,sd,0x100);
  388. clif_party_info(p,sd);
  389. if( sd2 != NULL )
  390. clif_party_inviteack(sd2,sd->status.name,2);
  391. for( i = 0; i < ARRAYLENGTH(p->data); ++i )
  392. {// hp of the other party members
  393. sd2 = p->data[i].sd;
  394. if( sd2 && sd2->status.account_id != account_id && sd2->status.char_id != char_id )
  395. clif_hpmeter_single(sd->fd, sd2->bl.id, sd2->battle_status.hp, sd2->battle_status.max_hp);
  396. }
  397. clif_party_hp(sd);
  398. clif_party_xy(sd);
  399. clif_charnameupdate(sd); //Update char name's display [Skotlex]
  400. if( p->instance_id )
  401. clif_instance_join(sd->fd, p->instance_id);
  402. return 0;
  403. }
  404. /// Party member 'sd' requesting kick of member with <account_id, name>.
  405. int party_removemember(struct map_session_data* sd, int account_id, char* name)
  406. {
  407. struct party_data *p;
  408. int i;
  409. p = party_search(sd->status.party_id);
  410. if( p == NULL )
  411. return 0;
  412. // check the requesting char's party membership
  413. ARR_FIND( 0, MAX_PARTY, i, p->party.member[i].account_id == sd->status.account_id && p->party.member[i].char_id == sd->status.char_id );
  414. if( i == MAX_PARTY )
  415. return 0; // request from someone not in party? o.O
  416. if( !p->party.member[i].leader )
  417. return 0; // only party leader may remove members
  418. ARR_FIND( 0, MAX_PARTY, i, p->party.member[i].account_id == account_id && strncmp(p->party.member[i].name,name,NAME_LENGTH) == 0 );
  419. if( i == MAX_PARTY )
  420. return 0; // no such char in party
  421. intif_party_leave(p->party.party_id,account_id,p->party.member[i].char_id);
  422. return 1;
  423. }
  424. /// Party member 'sd' requesting exit from party.
  425. int party_leave(struct map_session_data *sd)
  426. {
  427. struct party_data *p;
  428. int i;
  429. p = party_search(sd->status.party_id);
  430. if( p == NULL )
  431. return 0;
  432. ARR_FIND( 0, MAX_PARTY, i, p->party.member[i].account_id == sd->status.account_id && p->party.member[i].char_id == sd->status.char_id );
  433. if( i == MAX_PARTY )
  434. return 0;
  435. intif_party_leave(p->party.party_id,sd->status.account_id,sd->status.char_id);
  436. return 1;
  437. }
  438. /// Invoked (from char-server) when a party member leaves the party.
  439. int party_member_leaved(int party_id, int account_id, int char_id)
  440. {
  441. struct map_session_data* sd = map_id2sd(account_id);
  442. struct party_data* p = party_search(party_id);
  443. int i;
  444. if( p )
  445. {
  446. ARR_FIND( 0, MAX_PARTY, i, p->party.member[i].account_id == account_id && p->party.member[i].char_id == char_id );
  447. if( i < MAX_PARTY )
  448. {
  449. clif_party_leaved(p,sd,account_id,p->party.member[i].name,0x00);
  450. memset(&p->party.member[i], 0, sizeof(p->party.member[0]));
  451. memset(&p->data[i], 0, sizeof(p->data[0]));
  452. p->party.count--;
  453. party_check_state(p);
  454. }
  455. }
  456. if( sd && sd->status.party_id == party_id && sd->status.char_id == char_id )
  457. {
  458. sd->status.party_id = 0;
  459. clif_charnameupdate(sd); //Update name display [Skotlex]
  460. //TODO: hp bars should be cleared too
  461. if( p->instance_id )
  462. map_instance_check_kick(sd);
  463. }
  464. return 0;
  465. }
  466. /// Invoked (from char-server) when a party is disbanded.
  467. int party_broken(int party_id)
  468. {
  469. struct party_data* p;
  470. int i;
  471. p = party_search(party_id);
  472. if( p == NULL )
  473. return 0;
  474. if( p->instance_id )
  475. map_instance_destroy( p->instance_id );
  476. for(i=0;i<MAX_PARTY;i++){
  477. if(p->data[i].sd!=NULL){
  478. clif_party_leaved(p,p->data[i].sd,p->party.member[i].account_id,p->party.member[i].name,0x10);
  479. p->data[i].sd->status.party_id=0;
  480. }
  481. }
  482. idb_remove(party_db,party_id);
  483. return 0;
  484. }
  485. int party_changeoption(struct map_session_data *sd,int exp,int item)
  486. {
  487. nullpo_retr(0, sd);
  488. if( sd->status.party_id==0)
  489. return 0;
  490. intif_party_changeoption(sd->status.party_id,sd->status.account_id,exp,item);
  491. return 0;
  492. }
  493. int party_optionchanged(int party_id,int account_id,int exp,int item,int flag)
  494. {
  495. struct party_data *p;
  496. struct map_session_data *sd=map_id2sd(account_id);
  497. if( (p=party_search(party_id))==NULL)
  498. return 0;
  499. if(!(flag&0x01) && p->party.exp != exp) {
  500. p->party.exp=exp;
  501. clif_party_option(p,sd,flag); //This packet doesn't updates item info anymore...
  502. }
  503. if(!(flag&0x10) && p->party.item != item) {
  504. p->party.item=item;
  505. clif_party_member_info(p,sd);
  506. }
  507. if(flag&0x01) //Send denied message
  508. clif_party_option(p,sd,flag);
  509. return 0;
  510. }
  511. /// Invoked (from char-server) when a party member
  512. /// - changes maps
  513. /// - logs in or out
  514. /// - gains a level (disabled)
  515. int party_recv_movemap(int party_id,int account_id,int char_id, unsigned short map,int online,int lv)
  516. {
  517. struct party_member* m;
  518. struct party_data* p;
  519. int i;
  520. p = party_search(party_id);
  521. if( p == NULL )
  522. return 0;
  523. ARR_FIND( 0, MAX_PARTY, i, p->party.member[i].account_id == account_id && p->party.member[i].char_id == char_id );
  524. if( i == MAX_PARTY )
  525. {
  526. ShowError("party_recv_movemap: char %d/%d not found in party %s (id:%d)",account_id,char_id,p->party.name,party_id);
  527. return 0;
  528. }
  529. m = &p->party.member[i];
  530. m->map = map;
  531. m->online = online;
  532. m->lv = lv;
  533. //Check if they still exist on this map server
  534. p->data[i].sd = party_sd_check(party_id, account_id, char_id);
  535. clif_party_info(p,NULL);
  536. return 0;
  537. }
  538. void party_send_movemap(struct map_session_data *sd)
  539. {
  540. int i;
  541. struct party_data *p;
  542. if( sd->status.party_id==0 )
  543. return;
  544. intif_party_changemap(sd,1);
  545. p=party_search(sd->status.party_id);
  546. if (!p) return;
  547. if(sd->state.connect_new) {
  548. //Note that this works because this function is invoked before connect_new is cleared.
  549. clif_party_option(p,sd,0x100);
  550. clif_party_info(p,sd);
  551. clif_party_member_info(p,sd);
  552. }
  553. if (sd->fd) { // synchronize minimap positions with the rest of the party
  554. for(i=0; i < MAX_PARTY; i++) {
  555. if (p->data[i].sd &&
  556. p->data[i].sd != sd &&
  557. p->data[i].sd->bl.m == sd->bl.m)
  558. {
  559. clif_party_xy_single(sd->fd, p->data[i].sd);
  560. clif_party_xy_single(p->data[i].sd->fd, sd);
  561. }
  562. }
  563. }
  564. return;
  565. }
  566. void party_send_levelup(struct map_session_data *sd)
  567. {
  568. intif_party_changemap(sd,1);
  569. }
  570. int party_send_logout(struct map_session_data *sd)
  571. {
  572. struct party_data *p;
  573. int i;
  574. if(!sd->status.party_id)
  575. return 0;
  576. intif_party_changemap(sd,0);
  577. p=party_search(sd->status.party_id);
  578. if(!p) return 0;
  579. ARR_FIND( 0, MAX_PARTY, i, p->data[i].sd == sd );
  580. if( i < MAX_PARTY )
  581. memset(&p->data[i], 0, sizeof(p->data[0]));
  582. else
  583. ShowError("party_send_logout: Failed to locate member %d:%d in party %d!\n", sd->status.account_id, sd->status.char_id, p->party.party_id);
  584. return 1;
  585. }
  586. int party_send_message(struct map_session_data *sd,const char *mes,int len)
  587. {
  588. if(sd->status.party_id==0)
  589. return 0;
  590. intif_party_message(sd->status.party_id,sd->status.account_id,mes,len);
  591. party_recv_message(sd->status.party_id,sd->status.account_id,mes,len);
  592. // Chat logging type 'P' / Party Chat
  593. if( log_config.chat&1 || (log_config.chat&8 && !((agit_flag || agit2_flag) && log_config.chat&64)) )
  594. log_chat("P", sd->status.party_id, sd->status.char_id, sd->status.account_id, mapindex_id2name(sd->mapindex), sd->bl.x, sd->bl.y, NULL, mes);
  595. return 0;
  596. }
  597. int party_recv_message(int party_id,int account_id,const char *mes,int len)
  598. {
  599. struct party_data *p;
  600. if( (p=party_search(party_id))==NULL)
  601. return 0;
  602. clif_party_message(p,account_id,mes,len);
  603. return 0;
  604. }
  605. int party_skill_check(struct map_session_data *sd, int party_id, int skillid, int skilllv)
  606. {
  607. struct party_data *p;
  608. struct map_session_data *p_sd;
  609. int i;
  610. if(!party_id || (p=party_search(party_id))==NULL)
  611. return 0;
  612. switch(skillid) {
  613. case TK_COUNTER: //Increase Triple Attack rate of Monks.
  614. if (!p->state.monk) return 0;
  615. break;
  616. case MO_COMBOFINISH: //Increase Counter rate of Star Gladiators
  617. if (!p->state.sg) return 0;
  618. break;
  619. case AM_TWILIGHT2: //Twilight Pharmacy, requires Super Novice
  620. return p->state.snovice;
  621. case AM_TWILIGHT3: //Twilight Pharmacy, Requires Taekwon
  622. return p->state.tk;
  623. default:
  624. return 0; //Unknown case?
  625. }
  626. for(i=0;i<MAX_PARTY;i++){
  627. if ((p_sd = p->data[i].sd) == NULL)
  628. continue;
  629. if (sd->bl.m != p_sd->bl.m)
  630. continue;
  631. switch(skillid) {
  632. case TK_COUNTER: //Increase Triple Attack rate of Monks.
  633. if((p_sd->class_&MAPID_UPPERMASK) == MAPID_MONK
  634. && pc_checkskill(p_sd,MO_TRIPLEATTACK)) {
  635. sc_start4(&p_sd->bl,SC_SKILLRATE_UP,100,MO_TRIPLEATTACK,
  636. 50+50*skilllv, //+100/150/200% rate
  637. 0,0,skill_get_time(SG_FRIEND, 1));
  638. }
  639. break;
  640. case MO_COMBOFINISH: //Increase Counter rate of Star Gladiators
  641. if((p_sd->class_&MAPID_UPPERMASK) == MAPID_STAR_GLADIATOR
  642. && sd->sc.data[SC_READYCOUNTER]
  643. && pc_checkskill(p_sd,SG_FRIEND)) {
  644. sc_start4(&p_sd->bl,SC_SKILLRATE_UP,100,TK_COUNTER,
  645. 50+50*pc_checkskill(p_sd,SG_FRIEND), //+100/150/200% rate
  646. 0,0,skill_get_time(SG_FRIEND, 1));
  647. }
  648. break;
  649. }
  650. }
  651. return 0;
  652. }
  653. int party_send_xy_timer(int tid, unsigned int tick, int id, intptr data)
  654. {
  655. struct party_data* p;
  656. DBIterator* iter = party_db->iterator(party_db);
  657. // for each existing party,
  658. for( p = (struct party_data*)iter->first(iter,NULL); iter->exists(iter); p = (struct party_data*)iter->next(iter,NULL) )
  659. {
  660. int i;
  661. // for each member of this party,
  662. for( i = 0; i < MAX_PARTY; i++ )
  663. {
  664. //struct map_session_data* sd = p->data[i].sd;
  665. struct map_session_data* sd = map_charid2sd(p->party.member[i].char_id); //temporary crashfix
  666. if( !sd ) continue;
  667. if( p->data[i].x != sd->bl.x || p->data[i].y != sd->bl.y )
  668. {// perform position update
  669. clif_party_xy(sd);
  670. p->data[i].x = sd->bl.x;
  671. p->data[i].y = sd->bl.y;
  672. }
  673. if (battle_config.party_hp_mode && p->data[i].hp != sd->battle_status.hp)
  674. {// perform hp update
  675. clif_party_hp(sd);
  676. p->data[i].hp = sd->battle_status.hp;
  677. }
  678. }
  679. }
  680. iter->destroy(iter);
  681. return 0;
  682. }
  683. int party_send_xy_clear(struct party_data *p)
  684. {
  685. int i;
  686. nullpo_retr(0, p);
  687. for(i=0;i<MAX_PARTY;i++){
  688. if(!p->data[i].sd) continue;
  689. p->data[i].hp = 0;
  690. p->data[i].x = 0;
  691. p->data[i].y = 0;
  692. }
  693. return 0;
  694. }
  695. // exp share and added zeny share [Valaris]
  696. int party_exp_share(struct party_data* p, struct block_list* src, unsigned int base_exp, unsigned int job_exp, int zeny)
  697. {
  698. struct map_session_data* sd[MAX_PARTY];
  699. unsigned int i, c;
  700. nullpo_retr(0, p);
  701. // count the number of players eligible for exp sharing
  702. for (i = c = 0; i < MAX_PARTY; i++) {
  703. if( (sd[c] = p->data[i].sd) == NULL || sd[c]->bl.m != src->m || pc_isdead(sd[c]) || (battle_config.idle_no_share && pc_isidle(sd[c])) )
  704. continue;
  705. c++;
  706. }
  707. if (c < 1)
  708. return 0;
  709. base_exp/=c;
  710. job_exp/=c;
  711. zeny/=c;
  712. if (battle_config.party_even_share_bonus && c > 1)
  713. {
  714. double bonus = 100 + battle_config.party_even_share_bonus*(c-1);
  715. if (base_exp)
  716. base_exp = (unsigned int) cap_value(base_exp * bonus/100, 0, UINT_MAX);
  717. if (job_exp)
  718. job_exp = (unsigned int) cap_value(job_exp * bonus/100, 0, UINT_MAX);
  719. if (zeny)
  720. zeny = (unsigned int) cap_value(zeny * bonus/100, INT_MIN, INT_MAX);
  721. }
  722. for (i = 0; i < c; i++)
  723. {
  724. pc_gainexp(sd[i], src, base_exp, job_exp);
  725. if (zeny) // zeny from mobs [Valaris]
  726. pc_getzeny(sd[i],zeny);
  727. }
  728. return 0;
  729. }
  730. //Does party loot. first_charid holds the charid of the player who has time priority to take the item.
  731. int party_share_loot(struct party_data* p, struct map_session_data* sd, struct item* item_data, int first_charid)
  732. {
  733. TBL_PC* target = NULL;
  734. int i;
  735. if (p && p->party.item&2 && (first_charid || !(battle_config.party_share_type&1)))
  736. {
  737. //item distribution to party members.
  738. if (battle_config.party_share_type&2)
  739. { //Round Robin
  740. TBL_PC* psd;
  741. i = p->itemc;
  742. do {
  743. i++;
  744. if (i >= MAX_PARTY)
  745. i = 0; // reset counter to 1st person in party so it'll stop when it reaches "itemc"
  746. if( (psd = p->data[i].sd) == NULL || sd->bl.m != psd->bl.m || pc_isdead(psd) || (battle_config.idle_no_share && pc_isidle(psd)) )
  747. continue;
  748. if (pc_additem(psd,item_data,item_data->amount))
  749. continue; //Chosen char can't pick up loot.
  750. //Successful pick.
  751. p->itemc = i;
  752. target = psd;
  753. break;
  754. } while (i != p->itemc);
  755. }
  756. else
  757. { //Random pick
  758. TBL_PC* psd[MAX_PARTY];
  759. int count = 0;
  760. //Collect pick candidates
  761. for (i = 0; i < MAX_PARTY; i++) {
  762. if( (psd[count] = p->data[i].sd) == NULL || psd[count]->bl.m != sd->bl.m || pc_isdead(psd[count]) || (battle_config.idle_no_share && pc_isidle(psd[count])) )
  763. continue;
  764. count++;
  765. }
  766. while (count > 0) { //Pick a random member.
  767. i = rand()%count;
  768. if (pc_additem(psd[i],item_data,item_data->amount))
  769. { //Discard this receiver.
  770. psd[i] = psd[count-1];
  771. count--;
  772. } else { //Successful pick.
  773. target = psd[i];
  774. break;
  775. }
  776. }
  777. }
  778. }
  779. if (!target) {
  780. target = sd; //Give it to the char that picked it up
  781. if ((i=pc_additem(sd,item_data,item_data->amount)))
  782. return i;
  783. }
  784. if(log_config.enable_logs&0x8) //Logs items, taken by (P)layers [Lupus]
  785. log_pick_pc(target, "P", item_data->nameid, item_data->amount, item_data);
  786. if(battle_config.party_show_share_picker && target != sd) {
  787. char output[80];
  788. sprintf(output, "%s acquired %s.",target->status.name, itemdb_jname(item_data->nameid));
  789. clif_disp_onlyself(sd,output,strlen(output));
  790. }
  791. return 0;
  792. }
  793. int party_send_dot_remove(struct map_session_data *sd)
  794. {
  795. if (sd->status.party_id)
  796. clif_party_xy_remove(sd);
  797. return 0;
  798. }
  799. // To use for Taekwon's "Fighting Chant"
  800. // int c = 0;
  801. // party_foreachsamemap(party_sub_count, sd, 0, &c);
  802. int party_sub_count(struct block_list *bl, va_list ap)
  803. {
  804. struct map_session_data *sd = (TBL_PC *)bl;
  805. if (sd->state.autotrade)
  806. return 0;
  807. if (battle_config.idle_no_share && pc_isidle(sd))
  808. return 0;
  809. return 1;
  810. }
  811. /// Executes 'func' for each party member on the same map and in range (0:whole map)
  812. int party_foreachsamemap(int (*func)(struct block_list*,va_list),struct map_session_data *sd,int range,...)
  813. {
  814. struct party_data *p;
  815. int i;
  816. int x0,y0,x1,y1;
  817. struct block_list *list[MAX_PARTY];
  818. int blockcount=0;
  819. int total = 0; //Return value.
  820. nullpo_retr(0,sd);
  821. if((p=party_search(sd->status.party_id))==NULL)
  822. return 0;
  823. x0=sd->bl.x-range;
  824. y0=sd->bl.y-range;
  825. x1=sd->bl.x+range;
  826. y1=sd->bl.y+range;
  827. for(i=0;i<MAX_PARTY;i++)
  828. {
  829. struct map_session_data *psd = p->data[i].sd;
  830. if(!psd) continue;
  831. if(psd->bl.m!=sd->bl.m || !psd->bl.prev)
  832. continue;
  833. if(range &&
  834. (psd->bl.x<x0 || psd->bl.y<y0 ||
  835. psd->bl.x>x1 || psd->bl.y>y1 ) )
  836. continue;
  837. list[blockcount++]=&psd->bl;
  838. }
  839. map_freeblock_lock();
  840. for(i=0;i<blockcount;i++)
  841. {
  842. va_list ap;
  843. va_start(ap, range);
  844. total += func(list[i], ap);
  845. va_end(ap);
  846. }
  847. map_freeblock_unlock();
  848. return total;
  849. }