party.cpp 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520
  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 <stdlib.h>
  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. member->map = sd->mapindex;
  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 == NULL )
  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(NULL, p);
  69. ARR_FIND(0, MAX_PARTY, i, p->data[i].sd != NULL);
  70. return( i < MAX_PARTY ) ? p->data[i].sd : NULL;
  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 NULL;
  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 NULL;
  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,NULL);
  96. party_booking_db->destroy(party_booking_db,NULL); // 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 NULL;
  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 != NULL ) { // 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 == NULL )
  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 == NULL )
  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) ) == NULL )
  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 == NULL) {
  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 != NULL )
  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 == NULL || 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 != NULL )
  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 != NULL )
  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 == NULL )
  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 == NULL )
  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->instance_id ) {
  625. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  626. if( mapdata->instance_id ) { // User was on the instance map
  627. if( mapdata->save.map )
  628. pc_setpos(sd, mapdata->save.map, mapdata->save.x, mapdata->save.y, CLR_TELEPORT);
  629. else
  630. pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, CLR_TELEPORT);
  631. }
  632. }
  633. }
  634. return 0;
  635. }
  636. /// Invoked (from char-server) when a party is disbanded.
  637. int party_broken(int party_id)
  638. {
  639. struct party_data* p;
  640. int i;
  641. p = party_search(party_id);
  642. if( p == NULL )
  643. return 0;
  644. if( p->instance_id )
  645. instance_destroy( p->instance_id );
  646. for( i = 0; i < MAX_PARTY; i++ ) {
  647. if( p->data[i].sd != nullptr ) {
  648. clif_party_withdraw( *p->data[i].sd, p->party.member[i].account_id, p->party.member[i].name, PARTY_MEMBER_WITHDRAW_EXPEL, SELF );
  649. p->data[i].sd->status.party_id=0;
  650. }
  651. }
  652. idb_remove(party_db,party_id);
  653. return 1;
  654. }
  655. int party_changeoption(map_session_data *sd,int exp,int item)
  656. {
  657. nullpo_ret(sd);
  658. if( sd->status.party_id == 0 )
  659. return -3;
  660. intif_party_changeoption(sd->status.party_id,sd->status.account_id,exp,item);
  661. return 0;
  662. }
  663. //options: 0-exp, 1-item share, 2-pickup distribution
  664. int party_setoption(struct party_data *party, int option, int flag)
  665. {
  666. int i;
  667. ARR_FIND(0,MAX_PARTY,i,party->party.member[i].leader);
  668. if(i >= MAX_PARTY)
  669. return 0;
  670. switch(option) {
  671. case 0:
  672. intif_party_changeoption(party->party.party_id,party->party.member[i].account_id,flag,party->party.item);
  673. break;
  674. case 1:
  675. if(flag) flag = party->party.item|1;
  676. else flag = party->party.item&~1;
  677. intif_party_changeoption(party->party.party_id,party->party.member[i].account_id,party->party.exp,flag);
  678. break;
  679. case 2:
  680. if(flag) flag = party->party.item|2;
  681. else flag = party->party.item&~2;
  682. intif_party_changeoption(party->party.party_id,party->party.member[i].account_id,party->party.exp,flag);
  683. break;
  684. default:
  685. return 0;
  686. break;
  687. }
  688. return 1;
  689. }
  690. int party_optionchanged(int party_id,uint32 account_id,int exp,int item,int flag)
  691. {
  692. struct party_data *p;
  693. map_session_data *sd=map_id2sd(account_id);
  694. if( (p = party_search(party_id)) == NULL)
  695. return 0;
  696. //Flag&1: Exp change denied. Flag&2: Item change denied.
  697. if(!(flag&0x01) && p->party.exp != exp)
  698. p->party.exp = exp;
  699. if(!(flag&0x10) && p->party.item != item) {
  700. p->party.item = item;
  701. }
  702. clif_party_option(p,sd,flag);
  703. return 0;
  704. }
  705. int party_changeleader(map_session_data *sd, map_session_data *tsd, struct party_data *p)
  706. {
  707. int mi, tmi;
  708. if ( !p ) {
  709. if (!sd || !sd->status.party_id)
  710. return -1;
  711. if (!tsd || tsd->status.party_id != sd->status.party_id) {
  712. clif_displaymessage(sd->fd, msg_txt(sd,283));
  713. return -3;
  714. }
  715. if ( map_getmapflag(sd->bl.m, MF_PARTYLOCK) ) {
  716. clif_displaymessage(sd->fd, msg_txt(sd,287));
  717. return 0;
  718. }
  719. if ((p = party_search(sd->status.party_id)) == NULL )
  720. return -1;
  721. ARR_FIND( 0, MAX_PARTY, mi, p->data[mi].sd == sd );
  722. if (mi == MAX_PARTY)
  723. return 0; // Shouldn't happen
  724. if (!p->party.member[mi].leader) { // Need to be a party leader.
  725. clif_displaymessage(sd->fd, msg_txt(sd,282));
  726. return 0;
  727. }
  728. ARR_FIND( 0, MAX_PARTY, tmi, p->data[tmi].sd == tsd);
  729. if (tmi == MAX_PARTY)
  730. return 0; // Shouldn't happen
  731. if (battle_config.change_party_leader_samemap && p->party.member[mi].map != p->party.member[tmi].map) {
  732. clif_msg(sd, PARTY_MASTER_CHANGE_SAME_MAP);
  733. return 0;
  734. }
  735. } else {
  736. ARR_FIND(0,MAX_PARTY,mi,p->party.member[mi].leader);
  737. if (mi == MAX_PARTY)
  738. return 0; // Shouldn't happen
  739. ARR_FIND(0,MAX_PARTY,tmi,p->data[tmi].sd == tsd);
  740. if (tmi == MAX_PARTY)
  741. return 0; // Shouldn't happen
  742. }
  743. // Change leadership.
  744. p->party.member[mi].leader = 0;
  745. p->party.member[tmi].leader = 1;
  746. // Update members
  747. clif_party_leaderchanged(p->data[mi].sd, p->data[mi].sd->status.account_id, p->data[tmi].sd->status.account_id);
  748. // Update info.
  749. intif_party_leaderchange(p->party.party_id,p->party.member[tmi].account_id,p->party.member[tmi].char_id);
  750. clif_party_info( *p );
  751. return 1;
  752. }
  753. /// Invoked (from char-server) when a party member
  754. /// - changes maps
  755. /// - logs in or out
  756. /// - gains a level (disabled)
  757. int party_recv_movemap(int party_id,uint32 account_id,uint32 char_id, unsigned short map_idx,int online,int lv)
  758. {
  759. struct party_member* m;
  760. struct party_data* p;
  761. int i;
  762. p = party_search(party_id);
  763. if( p == NULL )
  764. return 0;
  765. ARR_FIND( 0, MAX_PARTY, i, p->party.member[i].account_id == account_id && p->party.member[i].char_id == char_id );
  766. if( i == MAX_PARTY ) {
  767. ShowError("party_recv_movemap: char %d/%d not found in party %s (id:%d)",account_id,char_id,p->party.name,party_id);
  768. return 0;
  769. }
  770. m = &p->party.member[i];
  771. m->map = map_idx;
  772. m->online = online;
  773. m->lv = lv;
  774. //Check if they still exist on this map server
  775. p->data[i].sd = party_sd_check(party_id, account_id, char_id);
  776. clif_party_info( *p );
  777. return 0;
  778. }
  779. void party_send_movemap(map_session_data *sd)
  780. {
  781. struct party_data *p;
  782. if( !sd->status.party_id )
  783. return;
  784. intif_party_changemap(sd,1);
  785. p=party_search(sd->status.party_id);
  786. if (!p) return;
  787. if(sd->state.connect_new) {
  788. //Note that this works because this function is invoked before connect_new is cleared.
  789. clif_party_option(p,sd,0x100);
  790. clif_party_info( *p, sd );
  791. clif_party_member_info( *p, *sd );
  792. }
  793. if (sd->fd) { // synchronize minimap positions with the rest of the party
  794. int i;
  795. for(i=0; i < MAX_PARTY; i++) {
  796. if (p->data[i].sd &&
  797. p->data[i].sd != sd &&
  798. p->data[i].sd->bl.m == sd->bl.m)
  799. {
  800. clif_party_xy_single( *sd, *p->data[i].sd );
  801. clif_party_xy_single( *p->data[i].sd, *sd );
  802. }
  803. }
  804. }
  805. return;
  806. }
  807. void party_send_levelup(map_session_data *sd)
  808. {
  809. intif_party_changemap(sd,1);
  810. }
  811. int party_send_logout(map_session_data *sd)
  812. {
  813. struct party_data *p;
  814. int i;
  815. if(!sd->status.party_id)
  816. return 0;
  817. intif_party_changemap(sd,0);
  818. p=party_search(sd->status.party_id);
  819. if(!p) return 0;
  820. ARR_FIND( 0, MAX_PARTY, i, p->data[i].sd == sd );
  821. if( i < MAX_PARTY )
  822. memset(&p->data[i], 0, sizeof(p->data[0]));
  823. else
  824. 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);
  825. return 1;
  826. }
  827. int party_send_message(map_session_data *sd,const char *mes,int len)
  828. {
  829. if(sd->status.party_id == 0)
  830. return 0;
  831. intif_party_message(sd->status.party_id,sd->status.account_id,mes,len);
  832. party_recv_message(sd->status.party_id,sd->status.account_id,mes,len);
  833. // Chat logging type 'P' / Party Chat
  834. 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, NULL, mes);
  835. return 0;
  836. }
  837. int party_recv_message(int party_id,uint32 account_id,const char *mes,int len)
  838. {
  839. struct party_data *p;
  840. if( (p=party_search(party_id))==NULL)
  841. return 0;
  842. clif_party_message( *p, account_id, mes, len );
  843. return 0;
  844. }
  845. int party_skill_check(map_session_data *sd, int party_id, uint16 skill_id, uint16 skill_lv)
  846. {
  847. struct party_data *p;
  848. map_session_data *p_sd;
  849. int i;
  850. if(!party_id || (p = party_search(party_id)) == NULL)
  851. return 0;
  852. party_check_state(p);
  853. switch(skill_id) {
  854. case TK_COUNTER: //Increase Triple Attack rate of Monks.
  855. if (!p->state.monk) return 0;
  856. break;
  857. case MO_COMBOFINISH: //Increase Counter rate of Star Gladiators
  858. if (!p->state.sg) return 0;
  859. break;
  860. case AM_TWILIGHT2: //Twilight Pharmacy, requires Super Novice
  861. return p->state.snovice;
  862. case AM_TWILIGHT3: //Twilight Pharmacy, Requires Taekwon
  863. return p->state.tk;
  864. default:
  865. return 0; //Unknown case?
  866. }
  867. for(i = 0; i < MAX_PARTY; i++) {
  868. if ((p_sd = p->data[i].sd) == NULL)
  869. continue;
  870. if (sd->bl.m != p_sd->bl.m)
  871. continue;
  872. switch(skill_id) {
  873. case TK_COUNTER: //Increase Triple Attack rate of Monks.
  874. if((p_sd->class_&MAPID_UPPERMASK) == MAPID_MONK
  875. && pc_checkskill(p_sd,MO_TRIPLEATTACK)) {
  876. sc_start4(&p_sd->bl,&p_sd->bl,SC_SKILLRATE_UP,100,MO_TRIPLEATTACK,
  877. 50+50*skill_lv, //+100/150/200% rate
  878. 0,0,skill_get_time(SG_FRIEND, 1));
  879. }
  880. break;
  881. case MO_COMBOFINISH: //Increase Counter rate of Star Gladiators
  882. if((p_sd->class_&MAPID_UPPERMASK) == MAPID_STAR_GLADIATOR
  883. && p_sd->sc.getSCE(SC_READYCOUNTER)
  884. && pc_checkskill(p_sd,SG_FRIEND)) {
  885. sc_start4(&p_sd->bl,&p_sd->bl,SC_SKILLRATE_UP,100,TK_COUNTER,
  886. 50+50*pc_checkskill(p_sd,SG_FRIEND), //+100/150/200% rate
  887. 0,0,skill_get_time(SG_FRIEND, 1));
  888. }
  889. break;
  890. }
  891. }
  892. return 0;
  893. }
  894. TIMER_FUNC(party_send_xy_timer){
  895. struct party_data* p;
  896. DBIterator *iter = db_iterator(party_db);
  897. // for each existing party
  898. for( p = (struct party_data*)dbi_first(iter); dbi_exists(iter); p = (struct party_data*)dbi_next(iter) ) {
  899. int i;
  900. if( !p->party.count ) // no online party members so do not iterate
  901. continue;
  902. // for each member of this party
  903. for( i = 0; i < MAX_PARTY; i++ ) {
  904. map_session_data* sd = p->data[i].sd;
  905. if( !sd )
  906. continue;
  907. if( p->data[i].x != sd->bl.x || p->data[i].y != sd->bl.y ) { // perform position update
  908. clif_party_xy( *sd );
  909. p->data[i].x = sd->bl.x;
  910. p->data[i].y = sd->bl.y;
  911. }
  912. if (battle_config.party_hp_mode && p->data[i].hp != sd->battle_status.hp) { // perform hp update
  913. clif_party_hp( *sd );
  914. p->data[i].hp = sd->battle_status.hp;
  915. }
  916. }
  917. }
  918. dbi_destroy(iter);
  919. return 0;
  920. }
  921. int party_send_xy_clear(struct party_data *p)
  922. {
  923. int i;
  924. nullpo_ret(p);
  925. for(i = 0; i < MAX_PARTY; i++) {
  926. if(!p->data[i].sd)
  927. continue;
  928. p->data[i].hp = 0;
  929. p->data[i].x = 0;
  930. p->data[i].y = 0;
  931. }
  932. return 0;
  933. }
  934. /** Party EXP and Zeny sharing
  935. * @param p Party data
  936. * @param src EXP source (for renewal level penalty)
  937. * @param base_exp Base EXP gained from killed mob
  938. * @param job_exp Job EXP gained from killed mob
  939. * @param zeny Zeny gained from killed mob
  940. * @author Valaris
  941. **/
  942. void party_exp_share(struct party_data* p, struct block_list* src, t_exp base_exp, t_exp job_exp, int zeny)
  943. {
  944. map_session_data* sd[MAX_PARTY];
  945. unsigned int i, c;
  946. #ifdef RENEWAL_EXP
  947. TBL_MOB *md = BL_CAST(BL_MOB, src);
  948. if (!md)
  949. return;
  950. #endif
  951. nullpo_retv(p);
  952. // count the number of players eligible for exp sharing
  953. for (i = c = 0; i < MAX_PARTY; i++) {
  954. if( (sd[c] = p->data[i].sd) == NULL || sd[c]->bl.m != src->m || pc_isdead(sd[c]) || (battle_config.idle_no_share && pc_isidle_party(sd[c])) )
  955. continue;
  956. c++;
  957. }
  958. if (c < 1)
  959. return;
  960. base_exp/=c;
  961. job_exp/=c;
  962. zeny/=c;
  963. if (battle_config.party_even_share_bonus && c > 1) {
  964. double bonus = 100 + battle_config.party_even_share_bonus*(c-1);
  965. if (base_exp)
  966. base_exp = (t_exp) cap_value(base_exp * bonus/100, 0, MAX_EXP);
  967. if (job_exp)
  968. job_exp = (t_exp) cap_value(job_exp * bonus/100, 0, MAX_EXP);
  969. if (zeny)
  970. zeny = (unsigned int) cap_value(zeny * bonus/100, INT_MIN, INT_MAX);
  971. }
  972. for (i = 0; i < c; i++) {
  973. #ifdef RENEWAL_EXP
  974. t_exp base_gained = base_exp, job_gained = job_exp;
  975. if (base_exp || job_exp) {
  976. int rate = pc_level_penalty_mod( sd[i], PENALTY_EXP, nullptr, md );
  977. if (rate != 100) {
  978. if (base_exp)
  979. base_gained = (t_exp)cap_value(apply_rate(base_exp, rate), 1, MAX_EXP);
  980. if (job_exp)
  981. job_gained = (t_exp)cap_value(apply_rate(job_exp, rate), 1, MAX_EXP);
  982. }
  983. }
  984. pc_gainexp(sd[i], src, base_gained, job_gained, 0);
  985. #else
  986. pc_gainexp(sd[i], src, base_exp, job_exp, 0);
  987. #endif
  988. if (zeny) // zeny from mobs [Valaris]
  989. pc_getzeny(sd[i],zeny,LOG_TYPE_PICKDROP_MONSTER,NULL);
  990. }
  991. }
  992. //Does party loot. first_charid holds the charid of the player who has time priority to take the item.
  993. int party_share_loot(struct party_data* p, map_session_data* sd, struct item* item, int first_charid)
  994. {
  995. TBL_PC* target = NULL;
  996. int i;
  997. if (p && p->party.item&2 && (first_charid || !(battle_config.party_share_type&1))) {
  998. //item distribution to party members.
  999. if (battle_config.party_share_type&2) { // Round Robin
  1000. TBL_PC* psd;
  1001. i = p->itemc;
  1002. do {
  1003. i++;
  1004. if (i >= MAX_PARTY)
  1005. i = 0; // reset counter to 1st person in party so it'll stop when it reaches "itemc"
  1006. if( (psd = p->data[i].sd) == NULL || sd->bl.m != psd->bl.m || pc_isdead(psd) || (battle_config.idle_no_share && pc_isidle_party(psd)) )
  1007. continue;
  1008. if (pc_additem(psd,item,item->amount,LOG_TYPE_PICKDROP_PLAYER))
  1009. continue; //Chosen char can't pick up loot.
  1010. //Successful pick.
  1011. p->itemc = i;
  1012. target = psd;
  1013. break;
  1014. } while (i != p->itemc);
  1015. } else { // Random pick
  1016. TBL_PC* psd[MAX_PARTY];
  1017. int count = 0;
  1018. //Collect pick candidates
  1019. for (i = 0; i < MAX_PARTY; i++) {
  1020. if( (psd[count] = p->data[i].sd) == NULL || psd[count]->bl.m != sd->bl.m || pc_isdead(psd[count]) || (battle_config.idle_no_share && pc_isidle_party(psd[count])) )
  1021. continue;
  1022. count++;
  1023. }
  1024. while (count > 0) { //Pick a random member.
  1025. i = rnd()%count;
  1026. if (pc_additem(psd[i],item,item->amount,LOG_TYPE_PICKDROP_PLAYER)) { // Discard this receiver.
  1027. psd[i] = psd[count-1];
  1028. count--;
  1029. } else { // Successful pick.
  1030. target = psd[i];
  1031. break;
  1032. }
  1033. }
  1034. }
  1035. }
  1036. if (!target) {
  1037. target = sd; //Give it to the char that picked it up
  1038. if ((i = pc_additem(sd,item,item->amount,LOG_TYPE_PICKDROP_PLAYER)))
  1039. return i;
  1040. }
  1041. if( p && battle_config.party_show_share_picker && battle_config.show_picker_item_type&(1<<itemdb_type(item->nameid)) )
  1042. clif_party_show_picker(target, item);
  1043. return 0;
  1044. }
  1045. int party_send_dot_remove(map_session_data *sd)
  1046. {
  1047. if (sd->status.party_id)
  1048. clif_party_xy_remove(sd);
  1049. return 0;
  1050. }
  1051. /**
  1052. * Check whether a party member is in autotrade or idle for count functions
  1053. * @param bl: Object invoking the counter
  1054. * @param ap: List of parameters
  1055. * @return 1 when neither autotrading and not idle or 0 otherwise
  1056. */
  1057. int party_sub_count(struct block_list *bl, va_list ap)
  1058. {
  1059. map_session_data *sd = (TBL_PC *)bl;
  1060. if (sd->state.autotrade)
  1061. return 0;
  1062. if (battle_config.idle_no_share && pc_isidle_party(sd))
  1063. return 0;
  1064. return 1;
  1065. }
  1066. /**
  1067. * To use for counting classes in a party.
  1068. * @param bl: Object invoking the counter
  1069. * @param ap: List of parameters: Class_Mask, Class_ID
  1070. * @return 1 when class exists in party or 0 otherwise
  1071. */
  1072. int party_sub_count_class(struct block_list *bl, va_list ap)
  1073. {
  1074. map_session_data *sd = (TBL_PC *)bl;
  1075. unsigned int mask = va_arg(ap, unsigned int);
  1076. unsigned int mapid_class = va_arg(ap, unsigned int);
  1077. if( !party_sub_count(bl, ap) )
  1078. return 0;
  1079. if( (sd->class_&mask) != mapid_class )
  1080. return 0;
  1081. return 1;
  1082. }
  1083. /// Executes 'func' for each party member on the same map and in range (0:whole map)
  1084. int party_foreachsamemap(int (*func)(struct block_list*,va_list),map_session_data *sd,int range,...)
  1085. {
  1086. struct party_data *p;
  1087. int i;
  1088. int x0,y0,x1,y1;
  1089. struct block_list *list[MAX_PARTY];
  1090. int blockcount=0;
  1091. int total = 0; //Return value.
  1092. nullpo_ret(sd);
  1093. if((p = party_search(sd->status.party_id)) == NULL)
  1094. return 0;
  1095. x0 = sd->bl.x-range;
  1096. y0 = sd->bl.y-range;
  1097. x1 = sd->bl.x+range;
  1098. y1 = sd->bl.y+range;
  1099. for(i = 0; i < MAX_PARTY; i++) {
  1100. map_session_data *psd = p->data[i].sd;
  1101. if(!psd)
  1102. continue;
  1103. if(psd->bl.m!=sd->bl.m || !psd->bl.prev)
  1104. continue;
  1105. if(range &&
  1106. (psd->bl.x<x0 || psd->bl.y<y0 ||
  1107. psd->bl.x>x1 || psd->bl.y>y1 ) )
  1108. continue;
  1109. list[blockcount++]=&psd->bl;
  1110. }
  1111. map_freeblock_lock();
  1112. for(i = 0; i < blockcount; i++) {
  1113. va_list ap;
  1114. va_start(ap, range);
  1115. total += func(list[i], ap);
  1116. va_end(ap);
  1117. }
  1118. map_freeblock_unlock();
  1119. return total;
  1120. }
  1121. /*==========================================
  1122. * Party Booking in KRO [Spiria]
  1123. *------------------------------------------*/
  1124. static struct party_booking_ad_info* create_party_booking_data(void)
  1125. {
  1126. struct party_booking_ad_info *pb_ad;
  1127. CREATE(pb_ad, struct party_booking_ad_info, 1);
  1128. pb_ad->index = party_booking_nextid++;
  1129. return pb_ad;
  1130. }
  1131. void party_booking_register(map_session_data *sd, short level, short mapid, short* job)
  1132. {
  1133. struct party_booking_ad_info *pb_ad;
  1134. int i;
  1135. pb_ad = (struct party_booking_ad_info*)idb_get(party_booking_db, sd->status.char_id);
  1136. if( pb_ad == NULL ) {
  1137. pb_ad = create_party_booking_data();
  1138. idb_put(party_booking_db, sd->status.char_id, pb_ad);
  1139. } else { // already registered
  1140. clif_PartyBookingRegisterAck(sd, 2);
  1141. return;
  1142. }
  1143. memcpy(pb_ad->charname,sd->status.name,NAME_LENGTH);
  1144. pb_ad->starttime = (int)time(NULL);
  1145. pb_ad->p_detail.level = level;
  1146. pb_ad->p_detail.mapid = mapid;
  1147. for(i = 0; i < MAX_PARTY_BOOKING_JOBS; i++)
  1148. if(job[i] != 0xFF)
  1149. pb_ad->p_detail.job[i] = job[i];
  1150. else pb_ad->p_detail.job[i] = -1;
  1151. clif_PartyBookingRegisterAck(sd, 0);
  1152. clif_PartyBookingInsertNotify(sd, pb_ad); // Notice
  1153. }
  1154. void party_booking_update(map_session_data *sd, short* job)
  1155. {
  1156. int i;
  1157. struct party_booking_ad_info *pb_ad;
  1158. pb_ad = (struct party_booking_ad_info*)idb_get(party_booking_db, sd->status.char_id);
  1159. if( pb_ad == NULL )
  1160. return;
  1161. pb_ad->starttime = (int)time(NULL);// Update time.
  1162. for(i = 0; i < MAX_PARTY_BOOKING_JOBS; i++)
  1163. if(job[i] != 0xFF)
  1164. pb_ad->p_detail.job[i] = job[i];
  1165. else
  1166. pb_ad->p_detail.job[i] = -1;
  1167. clif_PartyBookingUpdateNotify(sd, pb_ad);
  1168. }
  1169. void party_booking_search(map_session_data *sd, short level, short mapid, short job, unsigned long lastindex, short resultcount)
  1170. {
  1171. struct party_booking_ad_info *pb_ad;
  1172. int i, count=0;
  1173. struct party_booking_ad_info* result_list[MAX_PARTY_BOOKING_RESULTS];
  1174. bool more_result = false;
  1175. DBIterator* iter = db_iterator(party_booking_db);
  1176. memset(result_list, 0, sizeof(result_list));
  1177. for( pb_ad = (struct party_booking_ad_info*)dbi_first(iter); dbi_exists(iter); pb_ad = (struct party_booking_ad_info*)dbi_next(iter) ) {
  1178. if (pb_ad->index < lastindex || (level && (pb_ad->p_detail.level < level-15 || pb_ad->p_detail.level > level)))
  1179. continue;
  1180. if (count >= MAX_PARTY_BOOKING_RESULTS) {
  1181. more_result = true;
  1182. break;
  1183. }
  1184. if (mapid == 0 && job == -1)
  1185. result_list[count] = pb_ad;
  1186. else if (mapid == 0) {
  1187. for(i=0; i<MAX_PARTY_BOOKING_JOBS; i++)
  1188. if (pb_ad->p_detail.job[i] == job && job != -1)
  1189. result_list[count] = pb_ad;
  1190. } else if (job == -1) {
  1191. if (pb_ad->p_detail.mapid == mapid)
  1192. result_list[count] = pb_ad;
  1193. }
  1194. if( result_list[count] )
  1195. count++;
  1196. }
  1197. dbi_destroy(iter);
  1198. clif_PartyBookingSearchAck(sd->fd, result_list, count, more_result);
  1199. }
  1200. bool party_booking_delete(map_session_data *sd)
  1201. {
  1202. struct party_booking_ad_info* pb_ad;
  1203. if((pb_ad = (struct party_booking_ad_info*)idb_get(party_booking_db, sd->status.char_id)) != NULL) {
  1204. clif_PartyBookingDeleteNotify(sd, pb_ad->index);
  1205. idb_remove(party_booking_db,sd->status.char_id);
  1206. }
  1207. return true;
  1208. }