party.cpp 40 KB

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