party.cpp 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575
  1. // Copyright (c) rAthena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #include "party.hpp"
  4. #include <cstdlib>
  5. #include <common/cbasetypes.hpp>
  6. #include <common/malloc.hpp>
  7. #include <common/nullpo.hpp>
  8. #include <common/random.hpp>
  9. #include <common/showmsg.hpp>
  10. #include <common/socket.hpp> // last_tick, session_isActive
  11. #include <common/strlib.hpp>
  12. #include <common/timer.hpp>
  13. #include <common/utils.hpp>
  14. #include "achievement.hpp"
  15. #include "atcommand.hpp" //msg_txt()
  16. #include "battle.hpp"
  17. #include "chrif.hpp" // charserver_name
  18. #include "clif.hpp"
  19. #include "instance.hpp"
  20. #include "intif.hpp"
  21. #include "log.hpp"
  22. #include "mapreg.hpp"
  23. #include "mob.hpp"
  24. #include "pc.hpp"
  25. #include "pc_groups.hpp"
  26. #include "trade.hpp"
  27. static DBMap* party_db; // int party_id -> struct party_data* (releases data)
  28. static DBMap* party_booking_db; // uint32 char_id -> struct party_booking_ad_info* (releases data) // Party Booking [Spiria]
  29. static unsigned long party_booking_nextid = 1;
  30. TIMER_FUNC(party_send_xy_timer);
  31. int party_create_byscript;
  32. /*==========================================
  33. * Fills the given party_member structure according to the sd provided.
  34. * Used when creating/adding people to a party. [Skotlex]
  35. *------------------------------------------*/
  36. static void party_fill_member( struct party_member& member, map_session_data& sd, uint32 leader ){
  37. member.account_id = sd.status.account_id;
  38. member.char_id = sd.status.char_id;
  39. safestrncpy(member.name, sd.status.name, NAME_LENGTH);
  40. member.class_ = sd.status.class_;
  41. safestrncpy( member.map, mapindex_id2name( sd.mapindex ), sizeof( member.map ) );
  42. member.lv = sd.status.base_level;
  43. member.online = 1;
  44. member.leader = leader;
  45. }
  46. /// Get the member_id of a party member.
  47. /// Return -1 if not in party.
  48. int party_getmemberid(struct party_data* p, map_session_data* sd)
  49. {
  50. int member_id;
  51. nullpo_retr(-1, p);
  52. if( sd == nullptr )
  53. return -1;// no player
  54. ARR_FIND(0, MAX_PARTY, member_id,
  55. p->party.member[member_id].account_id == sd->status.account_id &&
  56. p->party.member[member_id].char_id == sd->status.char_id);
  57. if( member_id == MAX_PARTY )
  58. return -1;// not found
  59. return member_id;
  60. }
  61. /*==========================================
  62. * Request an available sd of this party
  63. *------------------------------------------*/
  64. map_session_data* party_getavailablesd(struct party_data *p)
  65. {
  66. int i;
  67. nullpo_retr(nullptr, p);
  68. ARR_FIND(0, MAX_PARTY, i, p->data[i].sd != nullptr);
  69. return( i < MAX_PARTY ) ? p->data[i].sd : nullptr;
  70. }
  71. /*==========================================
  72. * Retrieves and validates the sd pointer for this party member [Skotlex]
  73. *------------------------------------------*/
  74. static TBL_PC* party_sd_check(int party_id, uint32 account_id, uint32 char_id)
  75. {
  76. TBL_PC* sd = map_id2sd(account_id);
  77. if (!(sd && sd->status.char_id == char_id))
  78. return nullptr;
  79. if( sd->status.party_id == 0 )
  80. sd->status.party_id = party_id;// auto-join if not in a party
  81. if (sd->status.party_id != party_id)
  82. { //If player belongs to a different party, kick him out.
  83. intif_party_leave(party_id,account_id,char_id,sd->status.name,PARTY_MEMBER_WITHDRAW_LEAVE);
  84. return nullptr;
  85. }
  86. return sd;
  87. }
  88. /*==========================================
  89. * Destructor
  90. * Called in map shutdown, cleanup var
  91. *------------------------------------------*/
  92. void do_final_party(void)
  93. {
  94. party_db->destroy(party_db,nullptr);
  95. party_booking_db->destroy(party_booking_db,nullptr); // Party Booking [Spiria]
  96. }
  97. // Constructor, init vars
  98. void do_init_party(void)
  99. {
  100. party_db = idb_alloc(DB_OPT_RELEASE_DATA);
  101. party_booking_db = idb_alloc(DB_OPT_RELEASE_DATA); // Party Booking [Spiria]
  102. add_timer_func_list(party_send_xy_timer, "party_send_xy_timer");
  103. add_timer_interval(gettick()+battle_config.party_update_interval, party_send_xy_timer, 0, 0, battle_config.party_update_interval);
  104. }
  105. /// Party data lookup using party id.
  106. struct party_data* party_search(int party_id)
  107. {
  108. if(!party_id)
  109. return nullptr;
  110. return (struct party_data*)idb_get(party_db,party_id);
  111. }
  112. /// Party data lookup using party name.
  113. struct party_data* party_searchname(const char* str)
  114. {
  115. struct party_data* p;
  116. DBIterator *iter = db_iterator(party_db);
  117. for( p = (struct party_data*)dbi_first(iter); dbi_exists(iter); p = (struct party_data*)dbi_next(iter) ) {
  118. if( strncmpi(p->party.name,str,NAME_LENGTH) == 0 )
  119. break;
  120. }
  121. dbi_destroy(iter);
  122. return p;
  123. }
  124. int party_create( map_session_data& sd, char *name, int item, int item2 ){
  125. struct party_member leader = {};
  126. char tname[NAME_LENGTH];
  127. safestrncpy(tname, name, NAME_LENGTH);
  128. trim(tname);
  129. if( !tname[0] ) // empty name
  130. return 0;
  131. // already associated with a party
  132. if( sd.status.party_id > 0 || sd.party_joining || sd.party_creating ){
  133. clif_party_created( sd, 2 );
  134. return -2;
  135. }
  136. sd.party_creating = true;
  137. party_fill_member( leader, sd, 1 );
  138. intif_create_party(&leader,name,item,item2);
  139. return 1;
  140. }
  141. void party_created(uint32 account_id,uint32 char_id,int fail,int party_id,char *name)
  142. {
  143. map_session_data *sd;
  144. sd = map_id2sd(account_id);
  145. if (!sd || sd->status.char_id != char_id || !sd->party_creating ) { // Character logged off before creation ack?
  146. if (!fail) // break up party since player could not be added to it.
  147. intif_party_leave(party_id,account_id,char_id,"",PARTY_MEMBER_WITHDRAW_LEAVE);
  148. return;
  149. }
  150. sd->party_creating = false;
  151. if( !fail ) {
  152. sd->status.party_id = party_id;
  153. clif_party_created( *sd, 0 ); // Success message
  154. achievement_update_objective(sd, AG_PARTY, 1, 1);
  155. // We don't do any further work here because the char-server sends a party info packet right after creating the party
  156. if(party_create_byscript) { // returns party id in $@party_create_id if party is created by script
  157. mapreg_setreg(add_str("$@party_create_id"),party_id);
  158. party_create_byscript = 0;
  159. }
  160. } else
  161. clif_party_created( *sd, 1 ); // "party name already exists"
  162. }
  163. int party_request_info(int party_id, uint32 char_id)
  164. {
  165. return intif_request_partyinfo(party_id, char_id);
  166. }
  167. /**
  168. * Close trade window if party member is kicked when trade a party bound item
  169. * @param sd
  170. **/
  171. static void party_trade_bound_cancel( map_session_data& sd ){
  172. #ifdef BOUND_ITEMS
  173. if (sd.state.isBoundTrading&(1<<BOUND_PARTY))
  174. trade_tradecancel( &sd );
  175. #endif
  176. }
  177. /// Invoked (from char-server) when the party info is not found.
  178. int party_recv_noinfo(int party_id, uint32 char_id)
  179. {
  180. party_broken(party_id);
  181. if( char_id != 0 ) { // requester
  182. map_session_data* sd;
  183. sd = map_charid2sd(char_id);
  184. if( sd && sd->status.party_id == party_id )
  185. sd->status.party_id = 0;
  186. }
  187. return 0;
  188. }
  189. static void party_check_state(struct party_data *p)
  190. {
  191. int i;
  192. memset(&p->state, 0, sizeof(p->state));
  193. for (i = 0; i < MAX_PARTY; i ++) {
  194. if (!p->party.member[i].online)
  195. continue; //Those not online shouldn't aport to skill usage and all that.
  196. switch (p->party.member[i].class_) {
  197. case JOB_MONK:
  198. case JOB_BABY_MONK:
  199. case JOB_CHAMPION:
  200. case JOB_SURA:
  201. case JOB_SURA_T:
  202. case JOB_BABY_SURA:
  203. case JOB_INQUISITOR:
  204. p->state.monk = 1;
  205. break;
  206. case JOB_STAR_GLADIATOR:
  207. case JOB_BABY_STAR_GLADIATOR:
  208. case JOB_STAR_EMPEROR:
  209. case JOB_BABY_STAR_EMPEROR:
  210. case JOB_SKY_EMPEROR:
  211. p->state.sg = 1;
  212. break;
  213. case JOB_SUPER_NOVICE:
  214. case JOB_SUPER_BABY:
  215. case JOB_SUPER_NOVICE_E:
  216. case JOB_SUPER_BABY_E:
  217. case JOB_HYPER_NOVICE:
  218. p->state.snovice = 1;
  219. break;
  220. case JOB_TAEKWON:
  221. case JOB_BABY_TAEKWON:
  222. p->state.tk = 1;
  223. break;
  224. }
  225. }
  226. }
  227. int party_recv_info(struct party* sp, uint32 char_id)
  228. {
  229. struct party_data* p;
  230. struct party_member* member;
  231. map_session_data* sd;
  232. int removed[MAX_PARTY];// member_id in old data
  233. int removed_count = 0;
  234. int added[MAX_PARTY];// member_id in new data
  235. int added_count = 0;
  236. int member_id;
  237. bool rename = false;
  238. nullpo_ret(sp);
  239. p = (struct party_data*)idb_get(party_db, sp->party_id);
  240. if( p != nullptr ) { // diff members
  241. int i;
  242. for( member_id = 0; member_id < MAX_PARTY; ++member_id ) {
  243. member = &p->party.member[member_id];
  244. if( member->char_id == 0 )
  245. continue; // empty
  246. ARR_FIND(0, MAX_PARTY, i,
  247. sp->member[i].account_id == member->account_id &&
  248. sp->member[i].char_id == member->char_id);
  249. if( i == MAX_PARTY )
  250. removed[removed_count++] = member_id;
  251. // If the member already existed, compare the old to the (possible) new name
  252. else if( !rename && strcmp(member->name,sp->member[i].name) )
  253. rename = true;
  254. }
  255. for( member_id = 0; member_id < MAX_PARTY; ++member_id ) {
  256. member = &sp->member[member_id];
  257. if( member->char_id == 0 )
  258. continue; // empty
  259. ARR_FIND(0, MAX_PARTY, i,
  260. p->party.member[i].account_id == member->account_id &&
  261. p->party.member[i].char_id == member->char_id);
  262. if( i == MAX_PARTY )
  263. added[added_count++] = member_id;
  264. }
  265. } else {
  266. for( member_id = 0; member_id < MAX_PARTY; ++member_id )
  267. if( sp->member[member_id].char_id != 0 )
  268. added[added_count++] = member_id;
  269. CREATE(p, struct party_data, 1);
  270. idb_put(party_db, sp->party_id, p);
  271. }
  272. while( removed_count > 0 ) { // no longer in party
  273. member_id = removed[--removed_count];
  274. sd = p->data[member_id].sd;
  275. if( sd == nullptr )
  276. continue; // not online
  277. party_member_withdraw(sp->party_id, sd->status.account_id, sd->status.char_id, sd->status.name, PARTY_MEMBER_WITHDRAW_LEAVE);
  278. }
  279. memcpy(&p->party, sp, sizeof(struct party));
  280. memset(&p->state, 0, sizeof(p->state));
  281. memset(&p->data, 0, sizeof(p->data));
  282. for( member_id = 0; member_id < MAX_PARTY; member_id++ ) {
  283. member = &p->party.member[member_id];
  284. if ( member->char_id == 0 )
  285. continue;// empty
  286. p->data[member_id].sd = party_sd_check(sp->party_id, member->account_id, member->char_id);
  287. }
  288. party_check_state(p);
  289. while( added_count > 0 ) { // new in party
  290. member_id = added[--added_count];
  291. sd = p->data[member_id].sd;
  292. if( sd == nullptr )
  293. continue;// not online
  294. clif_name_area(&sd->bl); //Update other people's display. [Skotlex]
  295. clif_party_member_info( *p, *sd );
  296. // Only send this on party creation, otherwise it will be sent by party_send_movemap [Lemongrass]
  297. if( sd->party_creating ){
  298. clif_party_option(p,sd,0x100);
  299. }
  300. clif_party_info( *p );
  301. if (p->instance_id > 0)
  302. instance_reqinfo(sd, p->instance_id);
  303. }
  304. // If a player was renamed, make sure to resend the party information
  305. if( rename ){
  306. clif_party_info( *p );
  307. }
  308. if( char_id != 0 ) { // requester
  309. sd = map_charid2sd(char_id);
  310. if( sd && sd->status.party_id == sp->party_id && party_getmemberid(p,sd) == -1 )
  311. sd->status.party_id = 0;// was not in the party
  312. }
  313. return 0;
  314. }
  315. ///! TODO: Party invitation cross map-server through inter-server, so does with the reply.
  316. bool party_invite( map_session_data& sd, map_session_data *tsd ){
  317. struct party_data* p = party_search( sd.status.party_id );
  318. if( p == nullptr ){
  319. return false;
  320. }
  321. int i;
  322. // confirm if this player is a party leader
  323. ARR_FIND( 0, MAX_PARTY, i, p->data[i].sd == &sd );
  324. if( i == MAX_PARTY || !p->party.member[i].leader ) {
  325. clif_displaymessage( sd.fd, msg_txt( &sd, 282 ) );
  326. return false;
  327. }
  328. // Party locked.
  329. if( map_getmapflag( sd.bl.m, MF_PARTYLOCK ) ){
  330. clif_displaymessage( sd.fd, msg_txt( &sd, 227 ) );
  331. return false;
  332. }
  333. if( p->instance_id > 0 && battle_config.instance_block_invite ){
  334. clif_party_invite_reply( sd, "", PARTY_REPLY_MEMORIALDUNGEON );
  335. return false;
  336. }
  337. if( tsd == NULL ){
  338. clif_party_invite_reply( sd, "", PARTY_REPLY_OFFLINE );
  339. return false;
  340. }
  341. if( tsd->status.disable_partyinvite ){
  342. clif_party_invite_reply( sd, tsd->status.name, PARTY_REPLY_JOINMSG_REFUSE );
  343. return false;
  344. }
  345. // @noask [LuzZza]
  346. if( tsd->state.noask ){
  347. clif_noask_sub( sd, *tsd, 394 ); // Autorejected party invite from %s.
  348. return false;
  349. }
  350. if( battle_config.block_account_in_same_party ){
  351. ARR_FIND(0, MAX_PARTY, i, p->party.member[i].account_id == tsd->status.account_id);
  352. if (i < MAX_PARTY) {
  353. clif_party_invite_reply( sd, tsd->status.name, PARTY_REPLY_DUAL );
  354. return false;
  355. }
  356. }
  357. // confirm if there is an open slot in the party
  358. ARR_FIND(0, MAX_PARTY, i, p->party.member[i].account_id == 0);
  359. if( i == MAX_PARTY ) {
  360. clif_party_invite_reply( sd, tsd->status.name, PARTY_REPLY_FULL );
  361. return false;
  362. }
  363. // confirm whether the account has the ability to invite before checking the player
  364. if( !pc_has_permission( &sd, PC_PERM_PARTY ) || !pc_has_permission( tsd, PC_PERM_PARTY ) ) {
  365. clif_displaymessage( sd.fd, msg_txt( &sd, 81 ) ); // Your GM level doesn't authorize you to perform this action on the specified player.
  366. return false;
  367. }
  368. if( !battle_config.invite_request_check && ( tsd->guild_invite > 0 || tsd->state.trading || tsd->adopt_invite ) ){
  369. clif_party_invite_reply( sd, tsd->status.name, PARTY_REPLY_JOIN_OTHER_PARTY );
  370. return false;
  371. }
  372. // You can't invite someone who has already disconnected.
  373. if( !session_isActive( tsd->fd ) ){
  374. clif_party_invite_reply( sd, tsd->status.name, PARTY_REPLY_REJECTED );
  375. return false;
  376. }
  377. // Already associated with a party
  378. if( tsd->status.party_id > 0 || tsd->party_invite > 0 ){
  379. clif_party_invite_reply( sd, tsd->status.name, PARTY_REPLY_JOIN_OTHER_PARTY );
  380. return false;
  381. }
  382. tsd->party_invite = sd.status.party_id;
  383. tsd->party_invite_account = sd.status.account_id;
  384. clif_party_invite( sd, *tsd );
  385. return true;
  386. }
  387. bool party_isleader( map_session_data* sd ){
  388. if( sd == nullptr ){
  389. return false;
  390. }
  391. if( sd->status.party_id == 0 ){
  392. return false;
  393. }
  394. struct party_data* party = party_search( sd->status.party_id );
  395. if( party == nullptr ){
  396. return false;
  397. }
  398. for( int i = 0; i < MAX_PARTY; i++ ){
  399. if( party->party.member[i].char_id == sd->status.char_id ){
  400. return party->party.member[i].leader != 0;
  401. }
  402. }
  403. return false;
  404. }
  405. void party_join( map_session_data& sd, int party_id ){
  406. // Player is in a party already now
  407. if( sd.status.party_id != 0 ){
  408. return;
  409. }
  410. // Player is already associated with a party
  411. if( sd.party_creating || sd.party_joining ){
  412. return;
  413. }
  414. struct party_data* party = party_search( party_id );
  415. if( party == nullptr ){
  416. return;
  417. }
  418. int i;
  419. if( battle_config.block_account_in_same_party ){
  420. ARR_FIND( 0, MAX_PARTY, i, party->party.member[i].account_id == sd.status.account_id );
  421. if( i < MAX_PARTY ){
  422. // Player is in the party with a different character already
  423. return;
  424. }
  425. }
  426. // Confirm if there is an open slot in the party
  427. ARR_FIND( 0, MAX_PARTY, i, party->party.member[i].account_id == 0 );
  428. if( i == MAX_PARTY ){
  429. // Party is already full
  430. return;
  431. }
  432. struct party_member member = {};
  433. sd.party_joining = true;
  434. party_fill_member( member, sd, 0 );
  435. intif_party_addmember( party_id, &member );
  436. }
  437. bool party_booking_load( uint32 account_id, uint32 char_id, struct s_party_booking_requirement* booking ){
  438. char world_name[NAME_LENGTH * 2 + 1];
  439. Sql_EscapeString( mmysql_handle, world_name, charserver_name );
  440. if( Sql_Query( mmysql_handle, "SELECT `minimum_level`, `maximum_level` FROM `%s` WHERE `world_name` = '%s' AND `account_id` = '%u' AND `char_id` = '%u'", partybookings_table, world_name, account_id, char_id ) != SQL_SUCCESS) {
  441. Sql_ShowDebug( mmysql_handle );
  442. return false;
  443. }
  444. if( Sql_NextRow( mmysql_handle ) != SQL_SUCCESS ){
  445. Sql_FreeResult( mmysql_handle );
  446. return false;
  447. }
  448. char* data;
  449. Sql_GetData( mmysql_handle, 0, &data, nullptr );
  450. booking->minimum_level = (uint16)strtoul( data, nullptr, 10 );
  451. Sql_GetData( mmysql_handle, 1, &data, nullptr );
  452. booking->maximum_level = (uint16)strtoul( data, nullptr, 10 );
  453. Sql_FreeResult( mmysql_handle );
  454. return true;
  455. }
  456. bool party_reply_invite( map_session_data& sd, int party_id, int flag ){
  457. // forged
  458. if( sd.party_invite != party_id ) {
  459. sd.party_invite = 0;
  460. sd.party_invite_account = 0;
  461. return false;
  462. }
  463. // The character is already in a party, possibly left a party invite open and created his own party
  464. if( sd.status.party_id != 0 ){
  465. // On Aegis no rejection packet is sent to the inviting player
  466. sd.party_invite = 0;
  467. sd.party_invite_account = 0;
  468. return false;
  469. }
  470. // accepted and allowed
  471. if( flag == 1 && !sd.party_creating && !sd.party_joining ) {
  472. struct party_data* party = party_search( party_id );
  473. if( party && party->instance_id > 0 && battle_config.instance_block_invite ){
  474. sd.party_invite = 0;
  475. sd.party_invite_account = 0;
  476. return false;
  477. }
  478. struct party_member member = {};
  479. sd.party_joining = true;
  480. party_fill_member( member, sd, 0 );
  481. intif_party_addmember( sd.party_invite, &member );
  482. return true;
  483. }
  484. // rejected or failure
  485. map_session_data* tsd = map_id2sd( sd.party_invite_account );
  486. sd.party_invite = 0;
  487. sd.party_invite_account = 0;
  488. if( tsd != nullptr ) {
  489. clif_party_invite_reply( *tsd, sd.status.name, PARTY_REPLY_REJECTED );
  490. }
  491. return false;
  492. }
  493. //Invoked when a player joins:
  494. //- Loads up party data if not in server
  495. //- Sets up the pointer to him
  496. //- Player must be authed/active and belong to a party before calling this method
  497. void party_member_joined( map_session_data& sd ){
  498. struct party_data* p = party_search( sd.status.party_id );
  499. int i;
  500. if (!p) {
  501. party_request_info( sd.status.party_id, sd.status.char_id );
  502. return;
  503. }
  504. 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 );
  505. if (i < MAX_PARTY) {
  506. p->data[i].sd = &sd;
  507. } else
  508. sd.status.party_id = 0; //He does not belongs to the party really?
  509. }
  510. /// Invoked (from char-server) when a new member is added to the party.
  511. /// flag: 0-success, 1-failure
  512. int party_member_added(int party_id,uint32 account_id,uint32 char_id, int flag)
  513. {
  514. map_session_data *sd = map_id2sd(account_id),*sd2;
  515. struct party_data *p = party_search(party_id);
  516. int i;
  517. if(sd == nullptr || sd->status.char_id != char_id || !sd->party_joining ) {
  518. if (!flag) //Char logged off before being accepted into party.
  519. intif_party_leave(party_id,account_id,char_id,"",PARTY_MEMBER_WITHDRAW_LEAVE);
  520. return 0;
  521. }
  522. sd2 = map_id2sd(sd->party_invite_account);
  523. sd->party_joining = false;
  524. sd->party_invite = 0;
  525. sd->party_invite_account = 0;
  526. if (!p) {
  527. ShowError("party_member_added: party %d not found.\n",party_id);
  528. intif_party_leave(party_id,account_id,char_id,"",PARTY_MEMBER_WITHDRAW_LEAVE);
  529. return 0;
  530. }
  531. if( flag ) { // failed
  532. if( sd2 != nullptr )
  533. clif_party_invite_reply( *sd2, sd->status.name, PARTY_REPLY_FULL );
  534. return 0;
  535. }
  536. sd->status.party_id = party_id;
  537. clif_party_member_info( *p, *sd );
  538. clif_party_option(p,sd,0x100);
  539. clif_party_info( *p, sd );
  540. if( sd2 != nullptr )
  541. clif_party_invite_reply( *sd2, sd->status.name, PARTY_REPLY_ACCEPTED );
  542. for( i = 0; i < ARRAYLENGTH(p->data); ++i ) { // hp of the other party members
  543. sd2 = p->data[i].sd;
  544. if( sd2 && sd2->status.account_id != account_id && sd2->status.char_id != char_id )
  545. clif_hpmeter_single( *sd, sd2->bl.id, sd2->battle_status.hp, sd2->battle_status.max_hp );
  546. }
  547. clif_party_hp( *sd );
  548. clif_party_xy( *sd );
  549. clif_name_area(&sd->bl); //Update char name's display [Skotlex]
  550. if (p->instance_id > 0)
  551. instance_reqinfo(sd, p->instance_id);
  552. return 0;
  553. }
  554. /// Party member 'sd' requesting kick of member with <account_id, name>.
  555. bool party_removemember( map_session_data& sd, uint32 account_id, const char* name ){
  556. // Party locked.
  557. if( map_getmapflag( sd.bl.m, MF_PARTYLOCK ) ){
  558. clif_displaymessage( sd.fd, msg_txt( &sd, 227 ) );
  559. return false;
  560. }
  561. struct party_data* p = party_search( sd.status.party_id );
  562. if( p == nullptr )
  563. return false;
  564. if( p->instance_id > 0 && battle_config.instance_block_expulsion ){
  565. return false;
  566. }
  567. int i;
  568. // check the requesting char's party membership
  569. 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 );
  570. if( i == MAX_PARTY )
  571. return false; // request from someone not in party? o.O
  572. if( !p->party.member[i].leader )
  573. return false; // only party leader may remove members
  574. ARR_FIND( 0, MAX_PARTY, i, p->party.member[i].account_id == account_id && strncmp(p->party.member[i].name,name,NAME_LENGTH) == 0 );
  575. if( i == MAX_PARTY )
  576. return false; // no such char in party
  577. party_trade_bound_cancel(sd);
  578. intif_party_leave(p->party.party_id,account_id,p->party.member[i].char_id,p->party.member[i].name,PARTY_MEMBER_WITHDRAW_EXPEL);
  579. return true;
  580. }
  581. int party_removemember2(map_session_data *sd,uint32 char_id,int party_id)
  582. {
  583. if( sd ) {
  584. if( !sd->status.party_id )
  585. return -3;
  586. party_trade_bound_cancel( *sd );
  587. intif_party_leave(sd->status.party_id,sd->status.account_id,sd->status.char_id,sd->status.name,PARTY_MEMBER_WITHDRAW_EXPEL);
  588. return 1;
  589. } else {
  590. int i;
  591. struct party_data *p;
  592. if( !(p = party_search(party_id)) )
  593. return -2;
  594. ARR_FIND(0,MAX_PARTY,i,p->party.member[i].char_id == char_id );
  595. if( i >= MAX_PARTY )
  596. return -1;
  597. intif_party_leave(party_id,p->party.member[i].account_id,char_id,p->party.member[i].name,PARTY_MEMBER_WITHDRAW_EXPEL);
  598. return 1;
  599. }
  600. }
  601. /// Party member 'sd' requesting exit from party.
  602. bool party_leave( map_session_data& sd, bool showMessage ){
  603. // Party locked.
  604. if( map_getmapflag( sd.bl.m, MF_PARTYLOCK ) ){
  605. // If it was not triggered by the user itself, but from a script for example
  606. if( showMessage ){
  607. clif_displaymessage( sd.fd, msg_txt( &sd,227 ) );
  608. }
  609. return false;
  610. }
  611. struct party_data* p = party_search( sd.status.party_id );
  612. if( p == nullptr ){
  613. return false;
  614. }
  615. if( p->instance_id > 0 && battle_config.instance_block_leave ){
  616. // If it was not triggered by the user itself, but from a script for example
  617. if( showMessage ){
  618. clif_party_withdraw( sd, sd.status.account_id, sd.status.name, PARTY_MEMBER_WITHDRAW_CANT_LEAVE, SELF );
  619. }
  620. return false;
  621. }
  622. int i;
  623. 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 );
  624. if( i == MAX_PARTY ){
  625. return false;
  626. }
  627. party_trade_bound_cancel( sd );
  628. intif_party_leave( p->party.party_id, sd.status.account_id, sd.status.char_id, sd.status.name, PARTY_MEMBER_WITHDRAW_LEAVE );
  629. return true;
  630. }
  631. /// Invoked (from char-server) when a party member leaves the party.
  632. int party_member_withdraw(int party_id, uint32 account_id, uint32 char_id, char *name, enum e_party_member_withdraw type)
  633. {
  634. map_session_data* sd = map_charid2sd(char_id);
  635. struct party_data* p = party_search(party_id);
  636. if( p ) {
  637. map_session_data* party_sd = party_getavailablesd( p );
  638. if( party_sd != nullptr ){
  639. clif_party_withdraw( *party_sd, account_id, name, type, PARTY );
  640. }
  641. int i;
  642. ARR_FIND( 0, MAX_PARTY, i, p->party.member[i].account_id == account_id && p->party.member[i].char_id == char_id );
  643. if( i < MAX_PARTY ) {
  644. memset(&p->party.member[i], 0, sizeof(p->party.member[0]));
  645. memset(&p->data[i], 0, sizeof(p->data[0]));
  646. p->party.count--;
  647. party_check_state(p);
  648. }
  649. }
  650. if( sd && sd->status.party_id == party_id ) {
  651. #ifdef BOUND_ITEMS
  652. int idxlist[MAX_INVENTORY]; //or malloc to reduce consumtion
  653. int j,i;
  654. party_trade_bound_cancel( *sd );
  655. j = pc_bound_chk(sd,BOUND_PARTY,idxlist);
  656. for(i = 0; i < j; i++)
  657. pc_delitem(sd,idxlist[i],sd->inventory.u.items_inventory[idxlist[i]].amount,0,1,LOG_TYPE_BOUND_REMOVAL);
  658. #endif
  659. sd->status.party_id = 0;
  660. clif_name_area(&sd->bl); //Update name display [Skotlex]
  661. //TODO: hp bars should be cleared too
  662. if( p != nullptr && p->instance_id ){
  663. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  664. // User was on the instance map of the party
  665. if( mapdata != nullptr && p->instance_id == mapdata->instance_id ){
  666. pc_setpos_savepoint( *sd );
  667. }
  668. }
  669. }
  670. return 0;
  671. }
  672. /// Invoked (from char-server) when a party is disbanded.
  673. int party_broken(int party_id)
  674. {
  675. struct party_data* p;
  676. int i;
  677. p = party_search(party_id);
  678. if( p == nullptr )
  679. return 0;
  680. if( p->instance_id )
  681. instance_destroy( p->instance_id );
  682. for( i = 0; i < MAX_PARTY; i++ ) {
  683. if( p->data[i].sd != nullptr ) {
  684. clif_party_withdraw( *p->data[i].sd, p->party.member[i].account_id, p->party.member[i].name, PARTY_MEMBER_WITHDRAW_EXPEL, SELF );
  685. p->data[i].sd->status.party_id=0;
  686. }
  687. }
  688. idb_remove(party_db,party_id);
  689. return 1;
  690. }
  691. int party_changeoption(map_session_data *sd,int exp,int item)
  692. {
  693. nullpo_ret(sd);
  694. if( sd->status.party_id == 0 )
  695. return -3;
  696. intif_party_changeoption(sd->status.party_id,sd->status.account_id,exp,item);
  697. return 0;
  698. }
  699. //options: 0-exp, 1-item share, 2-pickup distribution
  700. int party_setoption(struct party_data *party, int option, int flag)
  701. {
  702. int i;
  703. ARR_FIND(0,MAX_PARTY,i,party->party.member[i].leader);
  704. if(i >= MAX_PARTY)
  705. return 0;
  706. switch(option) {
  707. case 0:
  708. intif_party_changeoption(party->party.party_id,party->party.member[i].account_id,flag,party->party.item);
  709. break;
  710. case 1:
  711. if(flag) flag = party->party.item|1;
  712. else flag = party->party.item&~1;
  713. intif_party_changeoption(party->party.party_id,party->party.member[i].account_id,party->party.exp,flag);
  714. break;
  715. case 2:
  716. if(flag) flag = party->party.item|2;
  717. else flag = party->party.item&~2;
  718. intif_party_changeoption(party->party.party_id,party->party.member[i].account_id,party->party.exp,flag);
  719. break;
  720. default:
  721. return 0;
  722. break;
  723. }
  724. return 1;
  725. }
  726. int party_optionchanged(int party_id,uint32 account_id,int exp,int item,int flag)
  727. {
  728. struct party_data *p;
  729. map_session_data *sd=map_id2sd(account_id);
  730. if( (p = party_search(party_id)) == nullptr)
  731. return 0;
  732. //Flag&1: Exp change denied. Flag&2: Item change denied.
  733. if(!(flag&0x01) && p->party.exp != exp)
  734. p->party.exp = exp;
  735. if(!(flag&0x10) && p->party.item != item) {
  736. p->party.item = item;
  737. }
  738. clif_party_option(p,sd,flag);
  739. return 0;
  740. }
  741. int party_changeleader(map_session_data *sd, map_session_data *tsd, struct party_data *p)
  742. {
  743. int mi, tmi;
  744. if ( !p ) {
  745. if (!sd || !sd->status.party_id)
  746. return -1;
  747. if (!tsd || tsd->status.party_id != sd->status.party_id) {
  748. clif_displaymessage(sd->fd, msg_txt(sd,283));
  749. return -3;
  750. }
  751. if ( map_getmapflag(sd->bl.m, MF_PARTYLOCK) ) {
  752. clif_displaymessage(sd->fd, msg_txt(sd,287));
  753. return 0;
  754. }
  755. if ((p = party_search(sd->status.party_id)) == nullptr )
  756. return -1;
  757. if( p->instance_id > 0 && battle_config.instance_block_leaderchange ){
  758. return 0;
  759. }
  760. ARR_FIND( 0, MAX_PARTY, mi, p->data[mi].sd == sd );
  761. if (mi == MAX_PARTY)
  762. return 0; // Shouldn't happen
  763. if (!p->party.member[mi].leader) { // Need to be a party leader.
  764. clif_displaymessage(sd->fd, msg_txt(sd,282));
  765. return 0;
  766. }
  767. ARR_FIND( 0, MAX_PARTY, tmi, p->data[tmi].sd == tsd);
  768. if (tmi == MAX_PARTY)
  769. return 0; // Shouldn't happen
  770. if( battle_config.change_party_leader_samemap && strncmp( p->party.member[mi].map, p->party.member[tmi].map, sizeof( p->party.member[mi].map ) ) != 0 ){
  771. clif_msg(sd, MSI_PARTY_MASTER_CHANGE_SAME_MAP);
  772. return 0;
  773. }
  774. } else {
  775. ARR_FIND(0,MAX_PARTY,mi,p->party.member[mi].leader);
  776. if (mi == MAX_PARTY)
  777. return 0; // Shouldn't happen
  778. ARR_FIND(0,MAX_PARTY,tmi,p->data[tmi].sd == tsd);
  779. if (tmi == MAX_PARTY)
  780. return 0; // Shouldn't happen
  781. }
  782. // Change leadership.
  783. p->party.member[mi].leader = 0;
  784. p->party.member[tmi].leader = 1;
  785. // Update members
  786. clif_party_leaderchanged(p->data[mi].sd, p->data[mi].sd->status.account_id, p->data[tmi].sd->status.account_id);
  787. // Update info.
  788. intif_party_leaderchange(p->party.party_id,p->party.member[tmi].account_id,p->party.member[tmi].char_id);
  789. clif_party_info( *p );
  790. return 1;
  791. }
  792. /// Invoked (from char-server) when a party member
  793. /// - changes maps
  794. /// - logs in or out
  795. /// - gains a level (disabled)
  796. int party_recv_movemap( int party_id, uint32 account_id, uint32 char_id, int online, int lv, const char* map ){
  797. struct party_member* m;
  798. struct party_data* p;
  799. int i;
  800. p = party_search(party_id);
  801. if( p == nullptr )
  802. return 0;
  803. ARR_FIND( 0, MAX_PARTY, i, p->party.member[i].account_id == account_id && p->party.member[i].char_id == char_id );
  804. if( i == MAX_PARTY ) {
  805. ShowError("party_recv_movemap: char %d/%d not found in party %s (id:%d)",account_id,char_id,p->party.name,party_id);
  806. return 0;
  807. }
  808. m = &p->party.member[i];
  809. safestrncpy( m->map, map, sizeof( m->map ) );
  810. m->online = online;
  811. m->lv = lv;
  812. //Check if they still exist on this map server
  813. p->data[i].sd = party_sd_check(party_id, account_id, char_id);
  814. clif_party_info( *p );
  815. return 0;
  816. }
  817. void party_send_movemap(map_session_data *sd)
  818. {
  819. struct party_data *p;
  820. if( !sd->status.party_id )
  821. return;
  822. intif_party_changemap(sd,1);
  823. p=party_search(sd->status.party_id);
  824. if (!p) return;
  825. if(sd->state.connect_new) {
  826. //Note that this works because this function is invoked before connect_new is cleared.
  827. clif_party_option(p,sd,0x100);
  828. clif_party_info( *p, sd );
  829. clif_party_member_info( *p, *sd );
  830. }
  831. if (sd->fd) { // synchronize minimap positions with the rest of the party
  832. int i;
  833. for(i=0; i < MAX_PARTY; i++) {
  834. if (p->data[i].sd &&
  835. p->data[i].sd != sd &&
  836. p->data[i].sd->bl.m == sd->bl.m)
  837. {
  838. clif_party_xy_single( *sd, *p->data[i].sd );
  839. clif_party_xy_single( *p->data[i].sd, *sd );
  840. }
  841. }
  842. }
  843. return;
  844. }
  845. void party_send_levelup(map_session_data *sd)
  846. {
  847. intif_party_changemap(sd,1);
  848. }
  849. int party_send_logout(map_session_data *sd)
  850. {
  851. struct party_data *p;
  852. int i;
  853. if(!sd->status.party_id)
  854. return 0;
  855. intif_party_changemap(sd,0);
  856. p=party_search(sd->status.party_id);
  857. if(!p) return 0;
  858. ARR_FIND( 0, MAX_PARTY, i, p->data[i].sd == sd );
  859. if( i < MAX_PARTY )
  860. memset(&p->data[i], 0, sizeof(p->data[0]));
  861. else
  862. 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);
  863. return 1;
  864. }
  865. int party_send_message(map_session_data *sd,const char *mes, size_t len)
  866. {
  867. if(sd->status.party_id == 0)
  868. return 0;
  869. intif_party_message(sd->status.party_id,sd->status.account_id,mes,len);
  870. party_recv_message(sd->status.party_id,sd->status.account_id,mes,len);
  871. // Chat logging type 'P' / Party Chat
  872. log_chat(LOG_CHAT_PARTY, sd->status.party_id, sd->status.char_id, sd->status.account_id, mapindex_id2name(sd->mapindex), sd->bl.x, sd->bl.y, nullptr, mes);
  873. return 0;
  874. }
  875. int party_recv_message( int party_id, uint32 account_id, const char *mes, size_t len ){
  876. struct party_data *p;
  877. if( (p=party_search(party_id))==nullptr)
  878. return 0;
  879. clif_party_message( *p, account_id, mes, len );
  880. return 0;
  881. }
  882. int party_skill_check(map_session_data *sd, int party_id, uint16 skill_id, uint16 skill_lv)
  883. {
  884. struct party_data *p;
  885. map_session_data *p_sd;
  886. int i;
  887. if(!party_id || (p = party_search(party_id)) == nullptr)
  888. return 0;
  889. party_check_state(p);
  890. switch(skill_id) {
  891. case TK_COUNTER: //Increase Triple Attack rate of Monks.
  892. if (!p->state.monk) return 0;
  893. break;
  894. case MO_COMBOFINISH: //Increase Counter rate of Star Gladiators
  895. if (!p->state.sg) return 0;
  896. break;
  897. case AM_TWILIGHT2: //Twilight Pharmacy, requires Super Novice
  898. return p->state.snovice;
  899. case AM_TWILIGHT3: //Twilight Pharmacy, Requires Taekwon
  900. return p->state.tk;
  901. default:
  902. return 0; //Unknown case?
  903. }
  904. for(i = 0; i < MAX_PARTY; i++) {
  905. if ((p_sd = p->data[i].sd) == nullptr)
  906. continue;
  907. if (sd->bl.m != p_sd->bl.m)
  908. continue;
  909. switch(skill_id) {
  910. case TK_COUNTER: //Increase Triple Attack rate of Monks.
  911. if((p_sd->class_&MAPID_UPPERMASK) == MAPID_MONK
  912. && pc_checkskill(p_sd,MO_TRIPLEATTACK)) {
  913. sc_start4(&p_sd->bl,&p_sd->bl,SC_SKILLRATE_UP,100,MO_TRIPLEATTACK,
  914. 50+50*skill_lv, //+100/150/200% rate
  915. 0,0,skill_get_time(SG_FRIEND, 1));
  916. }
  917. break;
  918. case MO_COMBOFINISH: //Increase Counter rate of Star Gladiators
  919. if((p_sd->class_&MAPID_UPPERMASK) == MAPID_STAR_GLADIATOR
  920. && p_sd->sc.getSCE(SC_READYCOUNTER)
  921. && pc_checkskill(p_sd,SG_FRIEND)) {
  922. sc_start4(&p_sd->bl,&p_sd->bl,SC_SKILLRATE_UP,100,TK_COUNTER,
  923. 50+50*pc_checkskill(p_sd,SG_FRIEND), //+100/150/200% rate
  924. 0,0,skill_get_time(SG_FRIEND, 1));
  925. }
  926. break;
  927. }
  928. }
  929. return 0;
  930. }
  931. TIMER_FUNC(party_send_xy_timer){
  932. struct party_data* p;
  933. DBIterator *iter = db_iterator(party_db);
  934. // for each existing party
  935. for( p = (struct party_data*)dbi_first(iter); dbi_exists(iter); p = (struct party_data*)dbi_next(iter) ) {
  936. int i;
  937. if( !p->party.count ) // no online party members so do not iterate
  938. continue;
  939. // for each member of this party
  940. for( i = 0; i < MAX_PARTY; i++ ) {
  941. map_session_data* sd = p->data[i].sd;
  942. if( !sd )
  943. continue;
  944. if( p->data[i].x != sd->bl.x || p->data[i].y != sd->bl.y ) { // perform position update
  945. clif_party_xy( *sd );
  946. p->data[i].x = sd->bl.x;
  947. p->data[i].y = sd->bl.y;
  948. }
  949. if (battle_config.party_hp_mode && p->data[i].hp != sd->battle_status.hp) { // perform hp update
  950. clif_party_hp( *sd );
  951. p->data[i].hp = sd->battle_status.hp;
  952. }
  953. }
  954. }
  955. dbi_destroy(iter);
  956. return 0;
  957. }
  958. int party_send_xy_clear(struct party_data *p)
  959. {
  960. int i;
  961. nullpo_ret(p);
  962. for(i = 0; i < MAX_PARTY; i++) {
  963. if(!p->data[i].sd)
  964. continue;
  965. p->data[i].hp = 0;
  966. p->data[i].x = 0;
  967. p->data[i].y = 0;
  968. }
  969. return 0;
  970. }
  971. /** Party EXP and Zeny sharing
  972. * @param p Party data
  973. * @param src EXP source (for renewal level penalty)
  974. * @param base_exp Base EXP gained from killed mob
  975. * @param job_exp Job EXP gained from killed mob
  976. * @param zeny Zeny gained from killed mob
  977. * @author Valaris
  978. **/
  979. void party_exp_share(struct party_data* p, struct block_list* src, t_exp base_exp, t_exp job_exp, int zeny)
  980. {
  981. map_session_data* sd[MAX_PARTY];
  982. uint32 i, c;
  983. #ifdef RENEWAL_EXP
  984. TBL_MOB *md = BL_CAST(BL_MOB, src);
  985. if (!md)
  986. return;
  987. #endif
  988. nullpo_retv(p);
  989. // count the number of players eligible for exp sharing
  990. for (i = c = 0; i < MAX_PARTY; i++) {
  991. if( (sd[c] = p->data[i].sd) == nullptr || sd[c]->bl.m != src->m || pc_isdead(sd[c]) || (battle_config.idle_no_share && pc_isidle_party(sd[c])) )
  992. continue;
  993. c++;
  994. }
  995. if (c < 1)
  996. return;
  997. base_exp/=c;
  998. job_exp/=c;
  999. zeny/=c;
  1000. if (battle_config.party_even_share_bonus && c > 1) {
  1001. double bonus = 100 + battle_config.party_even_share_bonus*(c-1);
  1002. if (base_exp)
  1003. base_exp = (t_exp) cap_value(base_exp * bonus/100, 0, MAX_EXP);
  1004. if (job_exp)
  1005. job_exp = (t_exp) cap_value(job_exp * bonus/100, 0, MAX_EXP);
  1006. if (zeny)
  1007. zeny = (uint32)cap_value(zeny * bonus/100, INT_MIN, INT_MAX);
  1008. }
  1009. for (i = 0; i < c; i++) {
  1010. #ifdef RENEWAL_EXP
  1011. t_exp base_gained = base_exp, job_gained = job_exp;
  1012. if (base_exp || job_exp) {
  1013. int rate = pc_level_penalty_mod( sd[i], PENALTY_EXP, nullptr, md );
  1014. if (rate != 100) {
  1015. if (base_exp)
  1016. base_gained = (t_exp)cap_value(apply_rate(base_exp, rate), 1, MAX_EXP);
  1017. if (job_exp)
  1018. job_gained = (t_exp)cap_value(apply_rate(job_exp, rate), 1, MAX_EXP);
  1019. }
  1020. }
  1021. pc_gainexp(sd[i], src, base_gained, job_gained, 0);
  1022. #else
  1023. pc_gainexp(sd[i], src, base_exp, job_exp, 0);
  1024. #endif
  1025. if (zeny) // zeny from mobs [Valaris]
  1026. pc_getzeny(sd[i],zeny,LOG_TYPE_PICKDROP_MONSTER);
  1027. }
  1028. }
  1029. //Does party loot. first_charid holds the charid of the player who has time priority to take the item.
  1030. int party_share_loot(struct party_data* p, map_session_data* sd, struct item* item, int first_charid)
  1031. {
  1032. TBL_PC* target = nullptr;
  1033. int i;
  1034. if (p && p->party.item&2 && (first_charid || !(battle_config.party_share_type&1))) {
  1035. //item distribution to party members.
  1036. if (battle_config.party_share_type&2) { // Round Robin
  1037. TBL_PC* psd;
  1038. i = p->itemc;
  1039. do {
  1040. i++;
  1041. if (i >= MAX_PARTY)
  1042. i = 0; // reset counter to 1st person in party so it'll stop when it reaches "itemc"
  1043. if( (psd = p->data[i].sd) == nullptr || sd->bl.m != psd->bl.m || pc_isdead(psd) || (battle_config.idle_no_share && pc_isidle_party(psd)) )
  1044. continue;
  1045. if (pc_additem(psd,item,item->amount,LOG_TYPE_PICKDROP_PLAYER))
  1046. continue; //Chosen char can't pick up loot.
  1047. //Successful pick.
  1048. p->itemc = i;
  1049. target = psd;
  1050. break;
  1051. } while (i != p->itemc);
  1052. } else { // Random pick
  1053. TBL_PC* psd[MAX_PARTY];
  1054. int count = 0;
  1055. //Collect pick candidates
  1056. for (i = 0; i < MAX_PARTY; i++) {
  1057. if( (psd[count] = p->data[i].sd) == nullptr || psd[count]->bl.m != sd->bl.m || pc_isdead(psd[count]) || (battle_config.idle_no_share && pc_isidle_party(psd[count])) )
  1058. continue;
  1059. count++;
  1060. }
  1061. while (count > 0) { //Pick a random member.
  1062. i = rnd_value(0, count-1);
  1063. if (pc_additem(psd[i],item,item->amount,LOG_TYPE_PICKDROP_PLAYER)) { // Discard this receiver.
  1064. psd[i] = psd[count-1];
  1065. count--;
  1066. } else { // Successful pick.
  1067. target = psd[i];
  1068. break;
  1069. }
  1070. }
  1071. }
  1072. }
  1073. if (!target) {
  1074. target = sd; //Give it to the char that picked it up
  1075. if ((i = pc_additem(sd,item,item->amount,LOG_TYPE_PICKDROP_PLAYER)))
  1076. return i;
  1077. }
  1078. if( p && battle_config.party_show_share_picker && battle_config.show_picker_item_type&(1<<itemdb_type(item->nameid)) )
  1079. clif_party_show_picker(target, item);
  1080. return 0;
  1081. }
  1082. int party_send_dot_remove(map_session_data *sd)
  1083. {
  1084. if (sd->status.party_id)
  1085. clif_party_xy_remove(sd);
  1086. return 0;
  1087. }
  1088. /**
  1089. * Check whether a party member is in autotrade or idle for count functions
  1090. * @param bl: Object invoking the counter
  1091. * @param ap: List of parameters
  1092. * @return 1 when neither autotrading and not idle or 0 otherwise
  1093. */
  1094. int party_sub_count(struct block_list *bl, va_list ap)
  1095. {
  1096. map_session_data *sd = (TBL_PC *)bl;
  1097. if (sd->state.autotrade)
  1098. return 0;
  1099. if (battle_config.idle_no_share && pc_isidle_party(sd))
  1100. return 0;
  1101. return 1;
  1102. }
  1103. /**
  1104. * To use for counting classes in a party.
  1105. * @param bl: Object invoking the counter
  1106. * @param ap: List of parameters: Class_Mask, Class_ID
  1107. * @return 1 when class exists in party or 0 otherwise
  1108. */
  1109. int party_sub_count_class(struct block_list *bl, va_list ap)
  1110. {
  1111. map_session_data *sd = (TBL_PC *)bl;
  1112. uint32 mask = va_arg(ap, uint32);
  1113. uint32 mapid_class = va_arg(ap, uint32);
  1114. if( !party_sub_count(bl, ap) )
  1115. return 0;
  1116. if( (sd->class_&mask) != mapid_class )
  1117. return 0;
  1118. return 1;
  1119. }
  1120. /// Executes 'func' for each party member on the same map and in range (0:whole map)
  1121. int party_foreachsamemap(int (*func)(struct block_list*,va_list),map_session_data *sd,int range,...)
  1122. {
  1123. struct party_data *p;
  1124. int i;
  1125. int x0,y0,x1,y1;
  1126. struct block_list *list[MAX_PARTY];
  1127. int blockcount=0;
  1128. int total = 0; //Return value.
  1129. nullpo_ret(sd);
  1130. if((p = party_search(sd->status.party_id)) == nullptr)
  1131. return 0;
  1132. x0 = sd->bl.x-range;
  1133. y0 = sd->bl.y-range;
  1134. x1 = sd->bl.x+range;
  1135. y1 = sd->bl.y+range;
  1136. for(i = 0; i < MAX_PARTY; i++) {
  1137. map_session_data *psd = p->data[i].sd;
  1138. if(!psd)
  1139. continue;
  1140. if(psd->bl.m!=sd->bl.m || !psd->bl.prev)
  1141. continue;
  1142. if(range &&
  1143. (psd->bl.x<x0 || psd->bl.y<y0 ||
  1144. psd->bl.x>x1 || psd->bl.y>y1 ) )
  1145. continue;
  1146. list[blockcount++]=&psd->bl;
  1147. }
  1148. map_freeblock_lock();
  1149. for(i = 0; i < blockcount; i++) {
  1150. va_list ap;
  1151. va_start(ap, range);
  1152. total += func(list[i], ap);
  1153. va_end(ap);
  1154. }
  1155. map_freeblock_unlock();
  1156. return total;
  1157. }
  1158. /*==========================================
  1159. * Party Booking in KRO [Spiria]
  1160. *------------------------------------------*/
  1161. static struct party_booking_ad_info* create_party_booking_data(void)
  1162. {
  1163. struct party_booking_ad_info *pb_ad;
  1164. CREATE(pb_ad, struct party_booking_ad_info, 1);
  1165. pb_ad->index = party_booking_nextid++;
  1166. return pb_ad;
  1167. }
  1168. void party_booking_register(map_session_data *sd, short level, short mapid, short* job)
  1169. {
  1170. struct party_booking_ad_info *pb_ad;
  1171. int i;
  1172. pb_ad = (struct party_booking_ad_info*)idb_get(party_booking_db, sd->status.char_id);
  1173. if( pb_ad == nullptr ) {
  1174. pb_ad = create_party_booking_data();
  1175. idb_put(party_booking_db, sd->status.char_id, pb_ad);
  1176. } else { // already registered
  1177. clif_PartyBookingRegisterAck(sd, 2);
  1178. return;
  1179. }
  1180. memcpy(pb_ad->charname,sd->status.name,NAME_LENGTH);
  1181. pb_ad->starttime = (int)time(nullptr);
  1182. pb_ad->p_detail.level = level;
  1183. pb_ad->p_detail.mapid = mapid;
  1184. for(i = 0; i < MAX_PARTY_BOOKING_JOBS; i++)
  1185. if(job[i] != 0xFF)
  1186. pb_ad->p_detail.job[i] = job[i];
  1187. else pb_ad->p_detail.job[i] = -1;
  1188. clif_PartyBookingRegisterAck(sd, 0);
  1189. clif_PartyBookingInsertNotify(sd, pb_ad); // Notice
  1190. }
  1191. void party_booking_update(map_session_data *sd, short* job)
  1192. {
  1193. int i;
  1194. struct party_booking_ad_info *pb_ad;
  1195. pb_ad = (struct party_booking_ad_info*)idb_get(party_booking_db, sd->status.char_id);
  1196. if( pb_ad == nullptr )
  1197. return;
  1198. pb_ad->starttime = (int)time(nullptr);// Update time.
  1199. for(i = 0; i < MAX_PARTY_BOOKING_JOBS; i++)
  1200. if(job[i] != 0xFF)
  1201. pb_ad->p_detail.job[i] = job[i];
  1202. else
  1203. pb_ad->p_detail.job[i] = -1;
  1204. clif_PartyBookingUpdateNotify(sd, pb_ad);
  1205. }
  1206. void party_booking_search(map_session_data *sd, short level, short mapid, short job, unsigned long lastindex, short resultcount)
  1207. {
  1208. struct party_booking_ad_info *pb_ad;
  1209. int i, count=0;
  1210. struct party_booking_ad_info* result_list[MAX_PARTY_BOOKING_RESULTS];
  1211. bool more_result = false;
  1212. DBIterator* iter = db_iterator(party_booking_db);
  1213. memset(result_list, 0, sizeof(result_list));
  1214. for( pb_ad = (struct party_booking_ad_info*)dbi_first(iter); dbi_exists(iter); pb_ad = (struct party_booking_ad_info*)dbi_next(iter) ) {
  1215. if (pb_ad->index < lastindex || (level && (pb_ad->p_detail.level < level-15 || pb_ad->p_detail.level > level)))
  1216. continue;
  1217. if (count >= MAX_PARTY_BOOKING_RESULTS) {
  1218. more_result = true;
  1219. break;
  1220. }
  1221. if (mapid == 0 && job == -1)
  1222. result_list[count] = pb_ad;
  1223. else if (mapid == 0) {
  1224. for(i=0; i<MAX_PARTY_BOOKING_JOBS; i++)
  1225. if (pb_ad->p_detail.job[i] == job && job != -1)
  1226. result_list[count] = pb_ad;
  1227. } else if (job == -1) {
  1228. if (pb_ad->p_detail.mapid == mapid)
  1229. result_list[count] = pb_ad;
  1230. }
  1231. if( result_list[count] )
  1232. count++;
  1233. }
  1234. dbi_destroy(iter);
  1235. clif_PartyBookingSearchAck(sd->fd, result_list, count, more_result);
  1236. }
  1237. bool party_booking_delete(map_session_data *sd)
  1238. {
  1239. struct party_booking_ad_info* pb_ad;
  1240. if((pb_ad = (struct party_booking_ad_info*)idb_get(party_booking_db, sd->status.char_id)) != nullptr) {
  1241. clif_PartyBookingDeleteNotify(sd, pb_ad->index);
  1242. idb_remove(party_booking_db,sd->status.char_id);
  1243. }
  1244. return true;
  1245. }