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