party.c 25 KB

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