· 7 years ago · Dec 01, 2018, 05:16 AM
1// Copyright (c) rAthena Dev Teams - Licensed under GNU GPL
2// For more information, see LICENCE in the main folder
3
4#include "pc.hpp"
5
6#include <map>
7
8#include <math.h>
9#include <stdlib.h>
10
11#include <yaml-cpp/yaml.h>
12
13#include "../common/cbasetypes.hpp"
14#include "../common/core.hpp" // get_svn_revision()
15#include "../common/ers.hpp" // ers_destroy
16#include "../common/malloc.hpp"
17#include "../common/mmo.hpp" //NAME_LENGTH
18#include "../common/nullpo.hpp"
19#include "../common/random.hpp"
20#include "../common/showmsg.hpp"
21#include "../common/socket.hpp" // session[]
22#include "../common/strlib.hpp" // safestrncpy()
23#include "../common/timer.hpp"
24#include "../common/utilities.hpp"
25#include "../common/utils.hpp"
26
27#include "achievement.hpp"
28#include "atcommand.hpp" // get_atcommand_level()
29#include "battle.hpp" // battle_config
30#include "battleground.hpp"
31#include "buyingstore.hpp" // struct s_buyingstore
32#include "channel.hpp"
33#include "chat.hpp"
34#include "chrif.hpp"
35#include "clan.hpp"
36#include "clif.hpp"
37#include "date.hpp" // is_day_of_*()
38#include "duel.hpp"
39#include "elemental.hpp"
40#include "guild.hpp"
41#include "homunculus.hpp"
42#include "instance.hpp"
43#include "intif.hpp"
44#include "itemdb.hpp" // MAX_ITEMGROUP
45#include "log.hpp"
46#include "map.hpp"
47#include "mercenary.hpp"
48#include "mob.hpp"
49#include "npc.hpp"
50#include "party.hpp" // party_search()
51#include "pc_groups.hpp"
52#include "pet.hpp" // pet_unlocktarget()
53#include "quest.hpp"
54#include "script.hpp" // struct script_reg, struct script_regstr
55#include "searchstore.hpp" // struct s_search_store_info
56#include "status.hpp" // OPTION_*, struct weapon_atk
57#include "storage.hpp"
58#include "unit.hpp" // unit_stop_attack(), unit_stop_walking()
59#include "vending.hpp" // struct s_vending
60
61using namespace rathena;
62
63int pc_split_atoui(char* str, unsigned int* val, char sep, int max);
64static inline bool pc_attendance_rewarded_today( struct map_session_data* sd );
65
66#define PVP_CALCRANK_INTERVAL 1000 // PVP calculation interval
67#define MAX_LEVEL_BASE_EXP 99999999 ///< Max Base EXP for player on Max Base Level
68#define MAX_LEVEL_JOB_EXP 999999999 ///< Max Job EXP for player on Max Job Level
69
70static unsigned int statp[MAX_LEVEL+1];
71#if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
72static unsigned int level_penalty[3][CLASS_MAX][MAX_LEVEL*2+1];
73#endif
74
75// h-files are for declarations, not for implementations... [Shinomori]
76struct skill_tree_entry skill_tree[CLASS_COUNT][MAX_SKILL_TREE];
77// timer for night.day implementation
78int day_timer_tid = INVALID_TIMER;
79int night_timer_tid = INVALID_TIMER;
80
81struct eri *pc_sc_display_ers = NULL;
82struct eri *pc_itemgrouphealrate_ers = NULL;
83struct eri *num_reg_ers;
84struct eri *str_reg_ers;
85int pc_expiration_tid = INVALID_TIMER;
86
87struct fame_list smith_fame_list[MAX_FAME_LIST];
88struct fame_list chemist_fame_list[MAX_FAME_LIST];
89struct fame_list taekwon_fame_list[MAX_FAME_LIST];
90
91struct s_job_info job_info[CLASS_COUNT];
92
93struct s_attendance_reward{
94 uint16 item_id;
95 uint16 amount;
96};
97
98struct s_attendance_period{
99 uint32 start;
100 uint32 end;
101 std::map<int,struct s_attendance_reward> rewards;
102};
103
104std::vector<struct s_attendance_period> attendance_periods;
105
106#define MOTD_LINE_SIZE 128
107static char motd_text[MOTD_LINE_SIZE][CHAT_SIZE_MAX]; // Message of the day buffer [Valaris]
108
109bool reg_load;
110
111/**
112 * Translation table from athena equip index to aegis bitmask
113*/
114unsigned int equip_bitmask[EQI_MAX] = {
115 EQP_ACC_L, // EQI_ACC_L
116 EQP_ACC_R, // EQI_ACC_R
117 EQP_SHOES, // EQI_SHOES
118 EQP_GARMENT, // EQI_GARMENT
119 EQP_HEAD_LOW, // EQI_HEAD_LOW
120 EQP_HEAD_MID, // EQI_HEAD_MID
121 EQP_HEAD_TOP, // EQI_HEAD_TOP
122 EQP_ARMOR, // EQI_ARMOR
123 EQP_HAND_L, // EQI_HAND_L
124 EQP_HAND_R, // EQI_HAND_R
125 EQP_COSTUME_HEAD_TOP, // EQI_COSTUME_HEAD_TOP
126 EQP_COSTUME_HEAD_MID, // EQI_COSTUME_HEAD_MID
127 EQP_COSTUME_HEAD_LOW, // EQI_COSTUME_HEAD_LOW
128 EQP_COSTUME_GARMENT, // EQI_COSTUME_GARMENT
129 EQP_AMMO, // EQI_AMMO
130 EQP_SHADOW_ARMOR, // EQI_SHADOW_ARMOR
131 EQP_SHADOW_WEAPON, // EQI_SHADOW_WEAPON
132 EQP_SHADOW_SHIELD, // EQI_SHADOW_SHIELD
133 EQP_SHADOW_SHOES, // EQI_SHADOW_SHOES
134 EQP_SHADOW_ACC_R, // EQI_SHADOW_ACC_R
135 EQP_SHADOW_ACC_L // EQI_SHADOW_ACC_L
136};
137
138//Links related info to the sd->hate_mob[]/sd->feel_map[] entries
139const struct sg_data sg_info[MAX_PC_FEELHATE] = {
140 { SG_SUN_ANGER, SG_SUN_BLESS, SG_SUN_COMFORT, "PC_FEEL_SUN", "PC_HATE_MOB_SUN", is_day_of_sun },
141 { SG_MOON_ANGER, SG_MOON_BLESS, SG_MOON_COMFORT, "PC_FEEL_MOON", "PC_HATE_MOB_MOON", is_day_of_moon },
142 { SG_STAR_ANGER, SG_STAR_BLESS, SG_STAR_COMFORT, "PC_FEEL_STAR", "PC_HATE_MOB_STAR", is_day_of_star }
143 };
144
145void pc_set_reg_load( bool val ){
146 reg_load = val;
147}
148
149/**
150 * Item Cool Down Delay Saving
151 * Struct item_cd is not a member of struct map_session_data
152 * to keep cooldowns in memory between player log-ins.
153 * All cooldowns are reset when server is restarted.
154 **/
155DBMap* itemcd_db = NULL; // char_id -> struct item_cd
156struct item_cd {
157 unsigned int tick[MAX_ITEMDELAYS]; //tick
158 unsigned short nameid[MAX_ITEMDELAYS]; //item id
159};
160
161/**
162* Converts a class to its array index for CLASS_COUNT defined arrays.
163* Note that it does not do a validity check for speed purposes, where parsing
164* player input make sure to use a pcdb_checkid first!
165* @param class_ Job ID see enum e_job
166* @return Class Index
167*/
168int pc_class2idx(int class_) {
169 if (class_ >= JOB_NOVICE_HIGH)
170 return class_- JOB_NOVICE_HIGH+JOB_MAX_BASIC;
171 return class_;
172}
173
174/**
175* Get player's group ID
176* @param sd
177* @return Group ID
178*/
179int pc_get_group_id(struct map_session_data *sd) {
180 return sd->group_id;
181}
182
183/** Get player's group Level
184* @param sd
185* @return Group Level
186*/
187int pc_get_group_level(struct map_session_data *sd) {
188 return sd->group_level;
189}
190
191static TIMER_FUNC(pc_invincible_timer){
192 struct map_session_data *sd;
193
194 if( (sd=(struct map_session_data *)map_id2sd(id)) == NULL || sd->bl.type!=BL_PC )
195 return 1;
196
197 if(sd->invincible_timer != tid){
198 ShowError("invincible_timer %d != %d\n",sd->invincible_timer,tid);
199 return 0;
200 }
201 sd->invincible_timer = INVALID_TIMER;
202 skill_unit_move(&sd->bl,tick,1);
203
204 return 0;
205}
206
207void pc_setinvincibletimer(struct map_session_data* sd, int val) {
208 nullpo_retv(sd);
209
210 if( sd->invincible_timer != INVALID_TIMER )
211 delete_timer(sd->invincible_timer,pc_invincible_timer);
212 sd->invincible_timer = add_timer(gettick()+val,pc_invincible_timer,sd->bl.id,0);
213}
214
215void pc_delinvincibletimer(struct map_session_data* sd)
216{
217 nullpo_retv(sd);
218
219 if( sd->invincible_timer != INVALID_TIMER )
220 {
221 delete_timer(sd->invincible_timer,pc_invincible_timer);
222 sd->invincible_timer = INVALID_TIMER;
223 skill_unit_move(&sd->bl,gettick(),1);
224 }
225}
226
227static TIMER_FUNC(pc_spiritball_timer){
228 struct map_session_data *sd;
229 int i;
230
231 if( (sd=(struct map_session_data *)map_id2sd(id)) == NULL || sd->bl.type!=BL_PC )
232 return 1;
233
234 if( sd->spiritball <= 0 )
235 {
236 ShowError("pc_spiritball_timer: %d spiritball's available. (aid=%d cid=%d tid=%d)\n", sd->spiritball, sd->status.account_id, sd->status.char_id, tid);
237 sd->spiritball = 0;
238 return 0;
239 }
240
241 ARR_FIND(0, sd->spiritball, i, sd->spirit_timer[i] == tid);
242 if( i == sd->spiritball )
243 {
244 ShowError("pc_spiritball_timer: timer not found (aid=%d cid=%d tid=%d)\n", sd->status.account_id, sd->status.char_id, tid);
245 return 0;
246 }
247
248 sd->spiritball--;
249 if( i != sd->spiritball )
250 memmove(sd->spirit_timer+i, sd->spirit_timer+i+1, (sd->spiritball-i)*sizeof(int));
251 sd->spirit_timer[sd->spiritball] = INVALID_TIMER;
252
253 clif_spiritball(&sd->bl);
254
255 return 0;
256}
257
258/**
259* Adds a spiritball to player for 'interval' ms
260* @param sd
261* @param interval
262* @param max
263*/
264void pc_addspiritball(struct map_session_data *sd,int interval,int max)
265{
266 int tid;
267 uint8 i;
268
269 nullpo_retv(sd);
270
271 if(max > MAX_SPIRITBALL)
272 max = MAX_SPIRITBALL;
273 if(sd->spiritball < 0)
274 sd->spiritball = 0;
275
276 if( sd->spiritball && sd->spiritball >= max )
277 {
278 if(sd->spirit_timer[0] != INVALID_TIMER)
279 delete_timer(sd->spirit_timer[0],pc_spiritball_timer);
280 sd->spiritball--;
281 if( sd->spiritball != 0 )
282 memmove(sd->spirit_timer+0, sd->spirit_timer+1, (sd->spiritball)*sizeof(int));
283 sd->spirit_timer[sd->spiritball] = INVALID_TIMER;
284 }
285
286 tid = add_timer(gettick()+interval, pc_spiritball_timer, sd->bl.id, 0);
287 ARR_FIND(0, sd->spiritball, i, sd->spirit_timer[i] == INVALID_TIMER || DIFF_TICK(get_timer(tid)->tick, get_timer(sd->spirit_timer[i])->tick) < 0);
288 if( i != sd->spiritball )
289 memmove(sd->spirit_timer+i+1, sd->spirit_timer+i, (sd->spiritball-i)*sizeof(int));
290 sd->spirit_timer[i] = tid;
291 sd->spiritball++;
292 if( (sd->class_&MAPID_THIRDMASK) == MAPID_ROYAL_GUARD )
293 clif_millenniumshield(&sd->bl,sd->spiritball);
294 else
295 clif_spiritball(&sd->bl);
296}
297
298/**
299* Removes number of spiritball from player
300* @param sd
301* @param count
302* @param type 1 = doesn't give client effect
303*/
304void pc_delspiritball(struct map_session_data *sd,int count,int type)
305{
306 uint8 i;
307
308 nullpo_retv(sd);
309
310 if(sd->spiritball <= 0) {
311 sd->spiritball = 0;
312 return;
313 }
314
315 if(count == 0)
316 return;
317 if(count > sd->spiritball)
318 count = sd->spiritball;
319 sd->spiritball -= count;
320 if(count > MAX_SPIRITBALL)
321 count = MAX_SPIRITBALL;
322
323 for(i=0;i<count;i++) {
324 if(sd->spirit_timer[i] != INVALID_TIMER) {
325 delete_timer(sd->spirit_timer[i],pc_spiritball_timer);
326 sd->spirit_timer[i] = INVALID_TIMER;
327 }
328 }
329 for(i=count;i<MAX_SPIRITBALL;i++) {
330 sd->spirit_timer[i-count] = sd->spirit_timer[i];
331 sd->spirit_timer[i] = INVALID_TIMER;
332 }
333
334 if(!type) {
335 if( (sd->class_&MAPID_THIRDMASK) == MAPID_ROYAL_GUARD )
336 clif_millenniumshield(&sd->bl,sd->spiritball);
337 else
338 clif_spiritball(&sd->bl);
339 }
340}
341
342/**
343* Increases a player's fame points and displays a notice to him
344* @param sd Player
345* @param count Fame point
346*/
347void pc_addfame(struct map_session_data *sd,int count)
348{
349 enum e_rank ranktype;
350 nullpo_retv(sd);
351 sd->status.fame += count;
352 if(sd->status.fame > MAX_FAME)
353 sd->status.fame = MAX_FAME;
354
355 switch(sd->class_&MAPID_UPPERMASK){
356 case MAPID_BLACKSMITH: ranktype = RANK_BLACKSMITH; break;
357 case MAPID_ALCHEMIST: ranktype = RANK_ALCHEMIST; break;
358 case MAPID_TAEKWON: ranktype = RANK_TAEKWON; break;
359 default:
360 ShowWarning( "pc_addfame: Trying to add fame to class '%s'(%d).\n", job_name(sd->class_), sd->class_ );
361 return;
362 }
363
364 clif_update_rankingpoint(sd,ranktype,count);
365 chrif_updatefamelist(sd);
366}
367
368/**
369 * Check whether a player ID is in the fame rankers list of its job, returns his/her position if so, 0 else
370 * @param sd
371 * @param job Job use enum e_mapid
372 * @return Rank
373 */
374unsigned char pc_famerank(uint32 char_id, int job)
375{
376 uint8 i;
377
378 switch(job){
379 case MAPID_BLACKSMITH: // Blacksmith
380 for(i = 0; i < MAX_FAME_LIST; i++){
381 if(smith_fame_list[i].id == char_id)
382 return i + 1;
383 }
384 break;
385 case MAPID_ALCHEMIST: // Alchemist
386 for(i = 0; i < MAX_FAME_LIST; i++){
387 if(chemist_fame_list[i].id == char_id)
388 return i + 1;
389 }
390 break;
391 case MAPID_TAEKWON: // Taekwon
392 for(i = 0; i < MAX_FAME_LIST; i++){
393 if(taekwon_fame_list[i].id == char_id)
394 return i + 1;
395 }
396 break;
397 }
398
399 return 0;
400}
401
402/**
403* Restart player's HP & SP value
404* @param sd
405* @param type Restart type: 1 - Normal Resurection
406*/
407void pc_setrestartvalue(struct map_session_data *sd, char type) {
408 struct status_data *status, *b_status;
409 nullpo_retv(sd);
410
411 b_status = &sd->base_status;
412 status = &sd->battle_status;
413
414 if (type&1) { //Normal resurrection
415 status->hp = 1; //Otherwise status_heal may fail if dead.
416 status_heal(&sd->bl, b_status->hp, 0, 1);
417 if( status->sp < b_status->sp )
418 status_set_sp(&sd->bl, b_status->sp, 1);
419 } else { //Just for saving on the char-server (with values as if respawned)
420 sd->status.hp = b_status->hp;
421 sd->status.sp = (status->sp < b_status->sp)?b_status->sp:status->sp;
422 }
423}
424
425/*==========================================
426 Rental System
427 *------------------------------------------*/
428
429/**
430 * Ends a rental and removes the item/effect
431 * @param tid: Tick ID
432 * @param tick: Timer
433 * @param id: Timer ID
434 * @param data: Data
435 * @return false - failure, true - success
436 */
437TIMER_FUNC(pc_inventory_rental_end){
438 struct map_session_data *sd = map_id2sd(id);
439
440 if( sd == NULL )
441 return 0;
442 if( tid != sd->rental_timer ) {
443 ShowError("pc_inventory_rental_end: invalid timer id.\n");
444 return 0;
445 }
446
447 pc_inventory_rentals(sd);
448 return 1;
449}
450
451/**
452 * Removes the rental timer from the player
453 * @param sd: Player data
454 */
455void pc_inventory_rental_clear(struct map_session_data *sd)
456{
457 if( sd->rental_timer != INVALID_TIMER ) {
458 delete_timer(sd->rental_timer, pc_inventory_rental_end);
459 sd->rental_timer = INVALID_TIMER;
460 }
461}
462
463/**
464 * Check for items in the player's inventory that are rental type
465 * @param sd: Player data
466 */
467void pc_inventory_rentals(struct map_session_data *sd)
468{
469 int i, c = 0;
470 unsigned int next_tick = UINT_MAX;
471
472 for( i = 0; i < MAX_INVENTORY; i++ ) { // Check for Rentals on Inventory
473 if( sd->inventory.u.items_inventory[i].nameid == 0 )
474 continue; // Nothing here
475 if( sd->inventory.u.items_inventory[i].expire_time == 0 )
476 continue;
477 if( sd->inventory.u.items_inventory[i].expire_time <= time(NULL) ) {
478 if (sd->inventory_data[i]->unequip_script)
479 run_script(sd->inventory_data[i]->unequip_script, 0, sd->bl.id, fake_nd->bl.id);
480 clif_rental_expired(sd->fd, i, sd->inventory.u.items_inventory[i].nameid);
481 pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 0, 0, LOG_TYPE_OTHER);
482 } else {
483 unsigned int expire_tick = (unsigned int)(sd->inventory.u.items_inventory[i].expire_time - time(NULL));
484
485 clif_rental_time(sd->fd, sd->inventory.u.items_inventory[i].nameid, (int)expire_tick);
486 next_tick = umin(expire_tick * 1000U, next_tick);
487 c++;
488 }
489 }
490
491 if( c > 0 ) // min(next_tick,3600000) 1 hour each timer to keep announcing to the owner, and to avoid a but with rental time > 15 days
492 sd->rental_timer = add_timer(gettick() + umin(next_tick,3600000), pc_inventory_rental_end, sd->bl.id, 0);
493 else
494 sd->rental_timer = INVALID_TIMER;
495}
496
497/**
498 * Add a rental item to the player and adjusts the rental timer appropriately
499 * @param sd: Player data
500 * @param seconds: Rental time
501 */
502void pc_inventory_rental_add(struct map_session_data *sd, unsigned int seconds)
503{
504 unsigned int tick = seconds * 1000;
505
506 if( sd == NULL )
507 return;
508
509 if( sd->rental_timer != INVALID_TIMER ) {
510 const struct TimerData * td;
511
512 td = get_timer(sd->rental_timer);
513 if( DIFF_TICK(td->tick, gettick()) > tick ) { // Update Timer as this one ends first than the current one
514 pc_inventory_rental_clear(sd);
515 sd->rental_timer = add_timer(gettick() + tick, pc_inventory_rental_end, sd->bl.id, 0);
516 }
517 } else
518 sd->rental_timer = add_timer(gettick() + umin(tick,3600000), pc_inventory_rental_end, sd->bl.id, 0);
519}
520
521/**
522 * Check if the player can sell the current item
523 * @param sd: map_session_data of the player
524 * @param item: struct of the checking item
525 * @return bool 'true' is sellable, 'false' otherwise
526 */
527bool pc_can_sell_item(struct map_session_data *sd, struct item *item) {
528 struct npc_data *nd;
529
530 if (sd == NULL || item == NULL)
531 return false;
532
533 nd = map_id2nd(sd->npc_shopid);
534
535 if (battle_config.hide_fav_sell && item->favorite)
536 return false; //Cannot sell favs (optional config)
537
538 if (item->expire_time)
539 return false; // Cannot Sell Rental Items
540
541 if (nd && nd->subtype == NPCTYPE_ITEMSHOP) {
542 struct item_data *itd;
543
544 if (item->bound && battle_config.allow_bound_sell&ISR_BOUND)
545 return true; // NPCTYPE_ITEMSHOP and bound item config is sellable
546 if ((itd = itemdb_search(item->nameid)) && itd->flag.trade_restriction&8 && battle_config.allow_bound_sell&ISR_SELLABLE)
547 return true; // NPCTYPE_ITEMSHOP and sell restricted item config is sellable
548 }
549
550 if (!itemdb_cansell(item, pc_get_group_level(sd)))
551 return false;
552
553 if (item->bound && !pc_can_give_bounded_items(sd))
554 return false; // Don't allow sale of bound items
555 return true;
556}
557
558/**
559 * Determines if player can give / drop / trade / vend items
560 */
561bool pc_can_give_items(struct map_session_data *sd)
562{
563 return pc_has_permission(sd, PC_PERM_TRADE);
564}
565
566/**
567 * Determines if player can give / drop / trade / vend bounded items
568 */
569bool pc_can_give_bounded_items(struct map_session_data *sd)
570{
571 return pc_has_permission(sd, PC_PERM_TRADE_BOUNDED);
572}
573
574/*==========================================
575 * Prepares character for saving.
576 * @param sd
577 *------------------------------------------*/
578void pc_makesavestatus(struct map_session_data *sd) {
579 nullpo_retv(sd);
580
581 if(!battle_config.save_clothcolor)
582 sd->status.clothes_color = 0;
583
584 // Since this is currently not officially released,
585 // its best to have a forced option to not save body styles.
586 if(!battle_config.save_body_style)
587 sd->status.body = 0;
588
589 //Only copy the Cart/Peco/Falcon options, the rest are handled via
590 //status change load/saving. [Skotlex]
591#ifdef NEW_CARTS
592 sd->status.option = sd->sc.option&(OPTION_INVISIBLE|OPTION_FALCON|OPTION_RIDING|OPTION_DRAGON|OPTION_WUG|OPTION_WUGRIDER|OPTION_MADOGEAR);
593#else
594 sd->status.option = sd->sc.option&(OPTION_INVISIBLE|OPTION_CART|OPTION_FALCON|OPTION_RIDING|OPTION_DRAGON|OPTION_WUG|OPTION_WUGRIDER|OPTION_MADOGEAR);
595#endif
596 if (sd->sc.data[SC_JAILED]) { //When Jailed, do not move last point.
597 if(pc_isdead(sd)){
598 pc_setrestartvalue(sd, 0);
599 } else {
600 sd->status.hp = sd->battle_status.hp;
601 sd->status.sp = sd->battle_status.sp;
602 }
603 sd->status.last_point.map = sd->mapindex;
604 sd->status.last_point.x = sd->bl.x;
605 sd->status.last_point.y = sd->bl.y;
606 return;
607 }
608
609 if(pc_isdead(sd)) {
610 pc_setrestartvalue(sd, 0);
611 memcpy(&sd->status.last_point,&sd->status.save_point,sizeof(sd->status.last_point));
612 } else {
613 sd->status.hp = sd->battle_status.hp;
614 sd->status.sp = sd->battle_status.sp;
615 sd->status.last_point.map = sd->mapindex;
616 sd->status.last_point.x = sd->bl.x;
617 sd->status.last_point.y = sd->bl.y;
618 }
619
620 if(map_getmapflag(sd->bl.m, MF_NOSAVE)) {
621 struct map_data *mapdata = map_getmapdata(sd->bl.m);
622
623 if(mapdata->save.map)
624 memcpy(&sd->status.last_point,&mapdata->save,sizeof(sd->status.last_point));
625 else
626 memcpy(&sd->status.last_point,&sd->status.save_point,sizeof(sd->status.last_point));
627 }
628}
629
630/*==========================================
631 * Off init ? Connection?
632 *------------------------------------------*/
633void pc_setnewpc(struct map_session_data *sd, uint32 account_id, uint32 char_id, int login_id1, unsigned int client_tick, int sex, int fd) {
634 nullpo_retv(sd);
635
636 sd->bl.id = account_id;
637 sd->status.account_id = account_id;
638 sd->status.char_id = char_id;
639 sd->status.sex = sex;
640 sd->login_id1 = login_id1;
641 sd->login_id2 = 0; // at this point, we can not know the value :(
642 sd->client_tick = client_tick;
643 sd->state.active = 0; //to be set to 1 after player is fully authed and loaded.
644 sd->bl.type = BL_PC;
645 if(battle_config.prevent_logout_trigger&PLT_LOGIN)
646 sd->canlog_tick = gettick();
647 //Required to prevent homunculus copuing a base speed of 0.
648 sd->battle_status.speed = sd->base_status.speed = DEFAULT_WALK_SPEED;
649}
650
651/**
652* Get equip point for an equip
653* @param sd
654* @param id
655*/
656int pc_equippoint_sub(struct map_session_data *sd,struct item_data* id){
657 int ep = 0;
658
659 nullpo_ret(sd);
660 nullpo_ret(id);
661
662 if (!itemdb_isequip2(id))
663 return 0; //Not equippable by players.
664
665 ep = id->equip;
666 if(id->look == W_DAGGER || id->look == W_1HSWORD || id->look == W_1HAXE) {
667 if(pc_checkskill(sd,AS_LEFT) > 0 || (sd->class_&MAPID_UPPERMASK) == MAPID_ASSASSIN || (sd->class_&MAPID_UPPERMASK) == MAPID_KAGEROUOBORO) { //Kagerou and Oboro can dual wield daggers. [Rytech]
668 if (ep == EQP_WEAPON)
669 return EQP_ARMS;
670 if (ep == EQP_SHADOW_WEAPON)
671 return EQP_SHADOW_ARMS;
672 }
673 }
674 return ep;
675}
676
677/**
678* Get equip point for an equip
679* @param sd
680* @param n Equip index in inventory
681*/
682int pc_equippoint(struct map_session_data *sd,int n){
683 nullpo_ret(sd);
684
685 return pc_equippoint_sub(sd,sd->inventory_data[n]);
686}
687
688/**
689 * Fill inventory_data with struct *item_data through inventory (fill with struct *item)
690 * @param sd : player session
691 * @return 0 sucess, 1:invalid sd
692 */
693void pc_setinventorydata(struct map_session_data *sd)
694{
695 uint8 i;
696 nullpo_retv(sd);
697
698 for(i = 0; i < MAX_INVENTORY; i++) {
699 unsigned short id = sd->inventory.u.items_inventory[i].nameid;
700 sd->inventory_data[i] = id?itemdb_search(id):NULL;
701 }
702}
703
704/**
705* 'Calculates' weapon type
706* @param sd : Player
707*/
708void pc_calcweapontype(struct map_session_data *sd)
709{
710 nullpo_retv(sd);
711
712 // single-hand
713 if(sd->weapontype2 == W_FIST) {
714 sd->status.weapon = sd->weapontype1;
715 return;
716 }
717 if(sd->weapontype1 == W_FIST) {
718 sd->status.weapon = sd->weapontype2;
719 return;
720 }
721 // dual-wield
722 sd->status.weapon = 0;
723 switch (sd->weapontype1){
724 case W_DAGGER:
725 switch (sd->weapontype2) {
726 case W_DAGGER: sd->status.weapon = W_DOUBLE_DD; break;
727 case W_1HSWORD: sd->status.weapon = W_DOUBLE_DS; break;
728 case W_1HAXE: sd->status.weapon = W_DOUBLE_DA; break;
729 }
730 break;
731 case W_1HSWORD:
732 switch (sd->weapontype2) {
733 case W_DAGGER: sd->status.weapon = W_DOUBLE_DS; break;
734 case W_1HSWORD: sd->status.weapon = W_DOUBLE_SS; break;
735 case W_1HAXE: sd->status.weapon = W_DOUBLE_SA; break;
736 }
737 break;
738 case W_1HAXE:
739 switch (sd->weapontype2) {
740 case W_DAGGER: sd->status.weapon = W_DOUBLE_DA; break;
741 case W_1HSWORD: sd->status.weapon = W_DOUBLE_SA; break;
742 case W_1HAXE: sd->status.weapon = W_DOUBLE_AA; break;
743 }
744 }
745 // unknown, default to right hand type
746 if (!sd->status.weapon)
747 sd->status.weapon = sd->weapontype1;
748}
749
750/**
751* Set equip index
752* @param sd : Player
753*/
754void pc_setequipindex(struct map_session_data *sd)
755{
756 uint16 i;
757
758 nullpo_retv(sd);
759
760 for (i = 0; i < EQI_MAX; i++)
761 sd->equip_index[i] = -1;
762
763 for (i = 0; i < MAX_INVENTORY; i++) {
764 if (sd->inventory.u.items_inventory[i].nameid <= 0)
765 continue;
766 if (sd->inventory.u.items_inventory[i].equip) {
767 uint8 j;
768 for (j = 0; j < EQI_MAX; j++)
769 if (sd->inventory.u.items_inventory[i].equip & equip_bitmask[j])
770 sd->equip_index[j] = i;
771
772 if (sd->inventory.u.items_inventory[i].equip & EQP_HAND_R) {
773 if (sd->inventory_data[i])
774 sd->weapontype1 = sd->inventory_data[i]->look;
775 else
776 sd->weapontype1 = 0;
777 }
778
779 if( sd->inventory.u.items_inventory[i].equip & EQP_HAND_L ) {
780 if( sd->inventory_data[i] && sd->inventory_data[i]->type == IT_WEAPON )
781 sd->weapontype2 = sd->inventory_data[i]->look;
782 else
783 sd->weapontype2 = 0;
784 }
785 }
786 }
787 pc_calcweapontype(sd);
788}
789
790//static int pc_isAllowedCardOn(struct map_session_data *sd,int s,int eqindex,int flag)
791//{
792// int i;
793// struct item *item = &sd->inventory.u.items_inventory[eqindex];
794// struct item_data *data;
795//
796// //Crafted/made/hatched items.
797// if (itemdb_isspecial(item->card[0]))
798// return 1;
799//
800// /* scan for enchant armor gems */
801// if( item->card[MAX_SLOTS - 1] && s < MAX_SLOTS - 1 )
802// s = MAX_SLOTS - 1;
803//
804// ARR_FIND( 0, s, i, item->card[i] && (data = itemdb_exists(item->card[i])) != NULL && data->flag.no_equip&flag );
805// return( i < s ) ? 0 : 1;
806//}
807
808
809/**
810 * Check if an item is equiped by player
811 * (Check if the itemid is equiped then search if that match the index in inventory (should be))
812 * @param sd : player session
813 * @param nameid : itemid
814 * @return 1:yes, 0:no
815 */
816bool pc_isequipped(struct map_session_data *sd, unsigned short nameid)
817{
818 uint8 i;
819
820 for( i = 0; i < EQI_MAX; i++ )
821 {
822 short index = sd->equip_index[i], j;
823 if( index < 0 )
824 continue;
825 if( pc_is_same_equip_index((enum equip_index)i, sd->equip_index, index) )
826 continue;
827 if( !sd->inventory_data[index] )
828 continue;
829 if( sd->inventory_data[index]->nameid == nameid )
830 return true;
831 for( j = 0; j < sd->inventory_data[index]->slot; j++ ){
832 if( sd->inventory.u.items_inventory[index].card[j] == nameid )
833 return true;
834 }
835 }
836
837 return false;
838}
839
840/**
841 * Check adoption rules
842 * @param p1_sd: Player 1
843 * @param p2_sd: Player 2
844 * @param b_sd: Player that will be adopted
845 * @return ADOPT_ALLOWED - Sent message to Baby to accept or deny
846 * ADOPT_ALREADY_ADOPTED - Already adopted
847 * ADOPT_MARRIED_AND_PARTY - Need to be married and in the same party
848 * ADOPT_EQUIP_RINGS - Need wedding rings equipped
849 * ADOPT_NOT_NOVICE - Adoptee is not a Novice
850 * ADOPT_CHARACTER_NOT_FOUND - Parent or Baby not found
851 * ADOPT_MORE_CHILDREN - Cannot adopt more than 1 Baby (client message)
852 * ADOPT_LEVEL_70 - Parents need to be level 70+ (client message)
853 * ADOPT_MARRIED - Cannot adopt a married person (client message)
854 */
855enum adopt_responses pc_try_adopt(struct map_session_data *p1_sd, struct map_session_data *p2_sd, struct map_session_data *b_sd)
856{
857 if( !p1_sd || !p2_sd || !b_sd )
858 return ADOPT_CHARACTER_NOT_FOUND;
859
860 if( b_sd->status.father || b_sd->status.mother || b_sd->adopt_invite )
861 return ADOPT_ALREADY_ADOPTED; // already adopted baby / in adopt request
862
863 if( !p1_sd->status.partner_id || !p1_sd->status.party_id || p1_sd->status.party_id != b_sd->status.party_id )
864 return ADOPT_MARRIED_AND_PARTY; // You need to be married and in party with baby to adopt
865
866 if( p1_sd->status.partner_id != p2_sd->status.char_id || p2_sd->status.partner_id != p1_sd->status.char_id )
867 return ADOPT_MARRIED_AND_PARTY; // Not married, wrong married
868
869 if( p2_sd->status.party_id != p1_sd->status.party_id )
870 return ADOPT_MARRIED_AND_PARTY; // Both parents need to be in the same party
871
872 // Parents need to have their ring equipped
873 if( !pc_isequipped(p1_sd, WEDDING_RING_M) && !pc_isequipped(p1_sd, WEDDING_RING_F) )
874 return ADOPT_EQUIP_RINGS;
875
876 if( !pc_isequipped(p2_sd, WEDDING_RING_M) && !pc_isequipped(p2_sd, WEDDING_RING_F) )
877 return ADOPT_EQUIP_RINGS;
878
879 // Already adopted a baby
880 if( p1_sd->status.child || p2_sd->status.child ) {
881 clif_Adopt_reply(p1_sd, ADOPT_REPLY_MORE_CHILDREN);
882 return ADOPT_MORE_CHILDREN;
883 }
884
885 // Parents need at least lvl 70 to adopt
886 if( p1_sd->status.base_level < 70 || p2_sd->status.base_level < 70 ) {
887 clif_Adopt_reply(p1_sd, ADOPT_REPLY_LEVEL_70);
888 return ADOPT_LEVEL_70;
889 }
890
891 if( b_sd->status.partner_id ) {
892 clif_Adopt_reply(p1_sd, ADOPT_REPLY_MARRIED);
893 return ADOPT_MARRIED;
894 }
895
896 if( !( ( b_sd->status.class_ >= JOB_NOVICE && b_sd->status.class_ <= JOB_THIEF ) || b_sd->status.class_ == JOB_SUPER_NOVICE || b_sd->status.class_ == JOB_SUPER_NOVICE_E ) )
897 return ADOPT_NOT_NOVICE;
898
899 return ADOPT_ALLOWED;
900}
901
902/*==========================================
903 * Adoption Process
904 *------------------------------------------*/
905bool pc_adoption(struct map_session_data *p1_sd, struct map_session_data *p2_sd, struct map_session_data *b_sd)
906{
907 int job, joblevel;
908 unsigned int jobexp;
909
910 if( pc_try_adopt(p1_sd, p2_sd, b_sd) != ADOPT_ALLOWED )
911 return false;
912
913 // Preserve current job levels and progress
914 joblevel = b_sd->status.job_level;
915 jobexp = b_sd->status.job_exp;
916
917 job = pc_mapid2jobid(b_sd->class_|JOBL_BABY, b_sd->status.sex);
918 if( job != -1 && pc_jobchange(b_sd, job, 0) )
919 { // Success, proceed to configure parents and baby skills
920 p1_sd->status.child = b_sd->status.char_id;
921 p2_sd->status.child = b_sd->status.char_id;
922 b_sd->status.father = p1_sd->status.char_id;
923 b_sd->status.mother = p2_sd->status.char_id;
924
925 // Restore progress
926 b_sd->status.job_level = joblevel;
927 clif_updatestatus(b_sd, SP_JOBLEVEL);
928 b_sd->status.job_exp = jobexp;
929 clif_updatestatus(b_sd, SP_JOBEXP);
930
931 // Baby Skills
932 pc_skill(b_sd, WE_BABY, 1, ADDSKILL_PERMANENT);
933 pc_skill(b_sd, WE_CALLPARENT, 1, ADDSKILL_PERMANENT);
934 pc_skill(b_sd, WE_CHEERUP, 1, ADDSKILL_PERMANENT);
935
936 // Parents Skills
937 pc_skill(p1_sd, WE_CALLBABY, 1, ADDSKILL_PERMANENT);
938 pc_skill(p2_sd, WE_CALLBABY, 1, ADDSKILL_PERMANENT);
939
940 chrif_save(p1_sd, CSAVE_NORMAL);
941 chrif_save(p2_sd, CSAVE_NORMAL);
942 chrif_save(b_sd, CSAVE_NORMAL);
943
944 achievement_update_objective(b_sd, AG_BABY, 1, 1);
945 achievement_update_objective(p1_sd, AG_BABY, 1, 2);
946 achievement_update_objective(p2_sd, AG_BABY, 1, 2);
947
948 return true;
949 }
950
951 return false; // Job Change Fail
952}
953
954/*==========================================
955 * Check if player can use/equip selected item. Used by pc_isUseitem and pc_isequip
956 Returns:
957 false : Cannot use/equip
958 true : Can use/equip
959 * Credits:
960 [Inkfish] for first idea
961 [Haru] for third-classes extension
962 [Cydh] finishing :D
963 *------------------------------------------*/
964static bool pc_isItemClass (struct map_session_data *sd, struct item_data* item) {
965 while (1) {
966 if (item->class_upper&ITEMJ_NORMAL && !(sd->class_&(JOBL_UPPER|JOBL_THIRD|JOBL_BABY))) //normal classes (no upper, no baby, no third)
967 break;
968#ifndef RENEWAL
969 //allow third classes to use trans. class items
970 if (item->class_upper&ITEMJ_UPPER && sd->class_&(JOBL_UPPER|JOBL_THIRD)) //trans. classes
971 break;
972 //third-baby classes can use same item too
973 if (item->class_upper&ITEMJ_BABY && sd->class_&JOBL_BABY) //baby classes
974 break;
975 //don't need to decide specific rules for third-classes?
976 //items for third classes can be used for all third classes
977 if (item->class_upper&(ITEMJ_THIRD|ITEMJ_THIRD_TRANS|ITEMJ_THIRD_BABY) && sd->class_&JOBL_THIRD)
978 break;
979#else
980 //trans. classes (exl. third-trans.)
981 if (item->class_upper&ITEMJ_UPPER && sd->class_&JOBL_UPPER && !(sd->class_&JOBL_THIRD))
982 break;
983 //baby classes (exl. third-baby)
984 if (item->class_upper&ITEMJ_BABY && sd->class_&JOBL_BABY && !(sd->class_&JOBL_THIRD))
985 break;
986 //third classes (exl. third-trans. and baby-third)
987 if (item->class_upper&ITEMJ_THIRD && sd->class_&JOBL_THIRD && !(sd->class_&(JOBL_UPPER|JOBL_BABY)))
988 break;
989 //trans-third classes
990 if (item->class_upper&ITEMJ_THIRD_TRANS && sd->class_&JOBL_THIRD && sd->class_&JOBL_UPPER)
991 break;
992 //third-baby classes
993 if (item->class_upper&ITEMJ_THIRD_BABY && sd->class_&JOBL_THIRD && sd->class_&JOBL_BABY)
994 break;
995#endif
996 return false;
997 }
998 return true;
999}
1000
1001/*=================================================
1002 * Checks if the player can equip the item at index n in inventory.
1003 * @param sd
1004 * @param n Item index in inventory
1005 * @return ITEM_EQUIP_ACK_OK(0) if can be equipped, or ITEM_EQUIP_ACK_FAIL(1)/ITEM_EQUIP_ACK_FAILLEVEL(2) if can't
1006 *------------------------------------------------*/
1007uint8 pc_isequip(struct map_session_data *sd,int n)
1008{
1009 struct item_data *item;
1010
1011 nullpo_retr(ITEM_EQUIP_ACK_FAIL, sd);
1012
1013 item = sd->inventory_data[n];
1014
1015 if(pc_has_permission(sd, PC_PERM_USE_ALL_EQUIPMENT))
1016 return ITEM_EQUIP_ACK_OK;
1017
1018 if(item == NULL)
1019 return ITEM_EQUIP_ACK_FAIL;
1020 if(item->elv && sd->status.base_level < (unsigned int)item->elv)
1021 return ITEM_EQUIP_ACK_FAILLEVEL;
1022 if(item->elvmax && sd->status.base_level > (unsigned int)item->elvmax)
1023 return ITEM_EQUIP_ACK_FAILLEVEL;
1024 if(item->sex != 2 && sd->status.sex != item->sex)
1025 return ITEM_EQUIP_ACK_FAIL;
1026
1027 //fail to equip if item is restricted
1028 if (!battle_config.allow_equip_restricted_item && itemdb_isNoEquip(item, sd->bl.m))
1029 return ITEM_EQUIP_ACK_FAIL;
1030
1031 if (item->equip&EQP_AMMO) {
1032 switch (item->look) {
1033 case AMMO_ARROW:
1034 if (battle_config.ammo_check_weapon && sd->status.weapon != W_BOW && sd->status.weapon != W_MUSICAL && sd->status.weapon != W_WHIP) {
1035 clif_msg(sd, ITEM_NEED_BOW);
1036 return ITEM_EQUIP_ACK_FAIL;
1037 }
1038 break;
1039 case AMMO_THROWABLE_DAGGER:
1040 if (!pc_checkskill(sd, AS_VENOMKNIFE))
1041 return ITEM_EQUIP_ACK_FAIL;
1042 break;
1043 case AMMO_BULLET:
1044 case AMMO_SHELL:
1045 if (battle_config.ammo_check_weapon && sd->status.weapon != W_REVOLVER && sd->status.weapon != W_RIFLE && sd->status.weapon != W_GATLING && sd->status.weapon != W_SHOTGUN
1046#ifdef RENEWAL
1047 && sd->status.weapon != W_GRENADE
1048#endif
1049 ) {
1050 clif_msg(sd, ITEM_BULLET_EQUIP_FAIL);
1051 return ITEM_EQUIP_ACK_FAIL;
1052 }
1053 break;
1054#ifndef RENEWAL
1055 case AMMO_GRENADE:
1056 if (battle_config.ammo_check_weapon && sd->status.weapon != W_GRENADE) {
1057 clif_msg(sd, ITEM_BULLET_EQUIP_FAIL);
1058 return ITEM_EQUIP_ACK_FAIL;
1059 }
1060 break;
1061#endif
1062 case AMMO_CANNONBALL:
1063 if (!pc_ismadogear(sd) && (sd->status.class_ == JOB_MECHANIC_T || sd->status.class_ == JOB_MECHANIC)) {
1064 clif_msg(sd, ITEM_NEED_MADOGEAR); // Item can only be used when Mado Gear is mounted.
1065 return ITEM_EQUIP_ACK_FAIL;
1066 }
1067 if (sd->state.active && !pc_iscarton(sd) && //Check if sc data is already loaded
1068 (sd->status.class_ == JOB_GENETIC_T || sd->status.class_ == JOB_GENETIC)) {
1069 clif_msg(sd, ITEM_NEED_CART); // Only available when cart is mounted.
1070 return ITEM_EQUIP_ACK_FAIL;
1071 }
1072 break;
1073 }
1074 }
1075
1076 if (sd->sc.count) {
1077 if(item->equip & EQP_ARMS && item->type == IT_WEAPON && sd->sc.data[SC_STRIPWEAPON]) // Also works with left-hand weapons [DracoRPG]
1078 return ITEM_EQUIP_ACK_FAIL;
1079 if(item->equip & EQP_SHIELD && item->type == IT_ARMOR && sd->sc.data[SC_STRIPSHIELD])
1080 return ITEM_EQUIP_ACK_FAIL;
1081 if(item->equip & EQP_ARMOR && sd->sc.data[SC_STRIPARMOR])
1082 return ITEM_EQUIP_ACK_FAIL;
1083 if(item->equip & EQP_HEAD_TOP && sd->sc.data[SC_STRIPHELM])
1084 return ITEM_EQUIP_ACK_FAIL;
1085 if(item->equip & EQP_ACC && sd->sc.data[SC__STRIPACCESSORY])
1086 return ITEM_EQUIP_ACK_FAIL;
1087 if (item->equip & EQP_ARMS && sd->sc.data[SC__WEAKNESS])
1088 return ITEM_EQUIP_ACK_FAIL;
1089 if(item->equip && (sd->sc.data[SC_KYOUGAKU] || sd->sc.data[SC_SUHIDE]))
1090 return ITEM_EQUIP_ACK_FAIL;
1091
1092 if (sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_SUPERNOVICE) {
1093 //Spirit of Super Novice equip bonuses. [Skotlex]
1094 if (sd->status.base_level > 90 && item->equip & EQP_HELM)
1095 return ITEM_EQUIP_ACK_OK; //Can equip all helms
1096
1097 if (sd->status.base_level > 96 && item->equip & EQP_ARMS && item->type == IT_WEAPON && item->wlv == 4)
1098 switch(item->look) { //In weapons, the look determines type of weapon.
1099 case W_DAGGER: //All level 4 - Daggers
1100 case W_1HSWORD: //All level 4 - 1H Swords
1101 case W_1HAXE: //All level 4 - 1H Axes
1102 case W_MACE: //All level 4 - 1H Maces
1103 case W_STAFF: //All level 4 - 1H Staves
1104 case W_2HSTAFF: //All level 4 - 2H Staves
1105 return ITEM_EQUIP_ACK_OK;
1106 }
1107 }
1108 }
1109
1110 //Not equipable by class. [Skotlex]
1111 if (!(1ULL << (sd->class_&MAPID_BASEMASK)&item->class_base[(sd->class_&JOBL_2_1) ? 1 : ((sd->class_&JOBL_2_2) ? 2 : 0)]))
1112 return ITEM_EQUIP_ACK_FAIL;
1113
1114 if (!pc_isItemClass(sd, item))
1115 return ITEM_EQUIP_ACK_FAIL;
1116
1117 return ITEM_EQUIP_ACK_OK;
1118}
1119
1120/*==========================================
1121 * No problem with the session id
1122 * set the status that has been sent from char server
1123 *------------------------------------------*/
1124bool pc_authok(struct map_session_data *sd, uint32 login_id2, time_t expiration_time, int group_id, struct mmo_charstatus *st, bool changing_mapservers)
1125{
1126 int i;
1127 unsigned long tick = gettick();
1128 uint32 ip = session[sd->fd]->client_addr;
1129
1130 sd->login_id2 = login_id2;
1131 sd->group_id = group_id;
1132
1133 /* load user permissions */
1134 pc_group_pc_load(sd);
1135
1136 memcpy(&sd->status, st, sizeof(*st));
1137
1138 if (st->sex != sd->status.sex) {
1139 clif_authfail_fd(sd->fd, 0);
1140 return false;
1141 }
1142
1143 //Set the map-server used job id. [Skotlex]
1144 i = pc_jobid2mapid(sd->status.class_);
1145 if (i == -1) { //Invalid class?
1146 ShowError("pc_authok: Invalid class %d for player %s (%d:%d). Class was changed to novice.\n", sd->status.class_, sd->status.name, sd->status.account_id, sd->status.char_id);
1147 sd->status.class_ = JOB_NOVICE;
1148 sd->class_ = MAPID_NOVICE;
1149 } else
1150 sd->class_ = i;
1151
1152 // Checks and fixes to character status data, that are required
1153 // in case of configuration change or stuff, which cannot be
1154 // checked on char-server.
1155 sd->status.hair = cap_value(sd->status.hair,MIN_HAIR_STYLE,MAX_HAIR_STYLE);
1156 sd->status.hair_color = cap_value(sd->status.hair_color,MIN_HAIR_COLOR,MAX_HAIR_COLOR);
1157 sd->status.clothes_color = cap_value(sd->status.clothes_color,MIN_CLOTH_COLOR,MAX_CLOTH_COLOR);
1158 sd->status.body = cap_value(sd->status.body,MIN_BODY_STYLE,MAX_BODY_STYLE);
1159
1160 //Initializations to null/0 unneeded since map_session_data was filled with 0 upon allocation.
1161 sd->state.connect_new = 1;
1162
1163 sd->followtimer = INVALID_TIMER; // [MouseJstr]
1164 sd->invincible_timer = INVALID_TIMER;
1165 sd->npc_timer_id = INVALID_TIMER;
1166 sd->pvp_timer = INVALID_TIMER;
1167 sd->expiration_tid = INVALID_TIMER;
1168 sd->autotrade_tid = INVALID_TIMER;
1169
1170#ifdef SECURE_NPCTIMEOUT
1171 // Initialize to defaults/expected
1172 sd->npc_idle_timer = INVALID_TIMER;
1173 sd->npc_idle_tick = tick;
1174 sd->npc_idle_type = NPCT_INPUT;
1175 sd->state.ignoretimeout = false;
1176#endif
1177
1178 sd->canuseitem_tick = tick;
1179 sd->canusecashfood_tick = tick;
1180 sd->canequip_tick = tick;
1181 sd->cantalk_tick = tick;
1182 sd->canskill_tick = tick;
1183 sd->cansendmail_tick = tick;
1184 sd->idletime = last_tick;
1185
1186 for(i = 0; i < MAX_SPIRITBALL; i++)
1187 sd->spirit_timer[i] = INVALID_TIMER;
1188 for(i = 0; i < ARRAYLENGTH(sd->autobonus); i++)
1189 sd->autobonus[i].active = INVALID_TIMER;
1190 for(i = 0; i < ARRAYLENGTH(sd->autobonus2); i++)
1191 sd->autobonus2[i].active = INVALID_TIMER;
1192 for(i = 0; i < ARRAYLENGTH(sd->autobonus3); i++)
1193 sd->autobonus3[i].active = INVALID_TIMER;
1194
1195 if (battle_config.item_auto_get)
1196 sd->state.autoloot = 10000;
1197
1198 if (battle_config.disp_experience)
1199 sd->state.showexp = 1;
1200 if (battle_config.disp_zeny)
1201 sd->state.showzeny = 1;
1202#ifdef VIP_ENABLE
1203 if (!battle_config.vip_disp_rate)
1204 sd->vip.disableshowrate = 1;
1205#endif
1206
1207 if (!(battle_config.display_skill_fail&2))
1208 sd->state.showdelay = 1;
1209
1210 memset(&sd->inventory, 0, sizeof(struct s_storage));
1211 memset(&sd->cart, 0, sizeof(struct s_storage));
1212 memset(&sd->storage, 0, sizeof(struct s_storage));
1213 memset(&sd->premiumStorage, 0, sizeof(struct s_storage));
1214 memset(&sd->equip_index, -1, sizeof(sd->equip_index));
1215
1216 if( pc_isinvisible(sd) && !pc_can_use_command( sd, "hide", COMMAND_ATCOMMAND ) ){
1217 sd->status.option &= ~OPTION_INVISIBLE;
1218 }
1219
1220 status_change_init(&sd->bl);
1221
1222 sd->sc.option = sd->status.option; //This is the actual option used in battle.
1223
1224 unit_dataset(&sd->bl);
1225
1226 sd->guild_x = -1;
1227 sd->guild_y = -1;
1228
1229 sd->delayed_damage = 0;
1230
1231 // Event Timers
1232 for( i = 0; i < MAX_EVENTTIMER; i++ )
1233 sd->eventtimer[i] = INVALID_TIMER;
1234 // Rental Timer
1235 sd->rental_timer = INVALID_TIMER;
1236
1237 for( i = 0; i < 3; i++ )
1238 sd->hate_mob[i] = -1;
1239
1240 sd->quest_log = NULL;
1241 sd->num_quests = 0;
1242 sd->avail_quests = 0;
1243 sd->save_quest = false;
1244 sd->count_rewarp = 0;
1245
1246 sd->regs.vars = i64db_alloc(DB_OPT_BASE);
1247 sd->regs.arrays = NULL;
1248 sd->vars_dirty = false;
1249 sd->vars_ok = false;
1250 sd->vars_received = 0x0;
1251
1252 sd->qi_display = NULL;
1253 sd->qi_count = 0;
1254
1255 //warp player
1256 if ((i=pc_setpos(sd,sd->status.last_point.map, sd->status.last_point.x, sd->status.last_point.y, CLR_OUTSIGHT)) != SETPOS_OK) {
1257 ShowError ("Last_point_map %s - id %d not found (error code %d)\n", mapindex_id2name(sd->status.last_point.map), sd->status.last_point.map, i);
1258
1259 // try warping to a default map instead (church graveyard)
1260 if (pc_setpos(sd, mapindex_name2id(MAP_PRONTERA), 273, 354, CLR_OUTSIGHT) != SETPOS_OK) {
1261 // if we fail again
1262 clif_authfail_fd(sd->fd, 0);
1263 return false;
1264 }
1265 }
1266
1267 clif_authok(sd);
1268
1269 //Prevent S. Novices from getting the no-death bonus just yet. [Skotlex]
1270 sd->die_counter=-1;
1271
1272 //display login notice
1273 ShowInfo("'" CL_WHITE "%s" CL_RESET "' logged in."
1274 " (AID/CID: '" CL_WHITE "%d/%d" CL_RESET "',"
1275 " IP: '" CL_WHITE "%d.%d.%d.%d" CL_RESET "',"
1276 " Group '" CL_WHITE "%d" CL_RESET "').\n",
1277 sd->status.name, sd->status.account_id, sd->status.char_id,
1278 CONVIP(ip), sd->group_id);
1279 // Send friends list
1280 clif_friendslist_send(sd);
1281
1282 if( !changing_mapservers ) {
1283
1284 if (battle_config.display_version == 1)
1285 pc_show_version(sd);
1286
1287 // Message of the Day [Valaris]
1288 for(i=0; i < MOTD_LINE_SIZE && motd_text[i][0]; i++) {
1289 if (battle_config.motd_type)
1290 clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], motd_text[i], false, SELF);
1291 else
1292 clif_displaymessage(sd->fd, motd_text[i]);
1293 }
1294
1295 if (expiration_time != 0)
1296 sd->expiration_time = expiration_time;
1297
1298 /**
1299 * Fixes login-without-aura glitch (the screen won't blink at this point, don't worry :P)
1300 **/
1301 clif_changemap(sd,sd->bl.m,sd->bl.x,sd->bl.y);
1302 }
1303
1304 pc_validate_skill(sd);
1305
1306 /* [Ind] */
1307 sd->sc_display = NULL;
1308 sd->sc_display_count = 0;
1309
1310 // Player has not yet received the CashShop list
1311 sd->status.cashshop_sent = false;
1312
1313 sd->last_addeditem_index = -1;
1314
1315 sd->bonus_script.head = NULL;
1316 sd->bonus_script.count = 0;
1317
1318#if PACKETVER >= 20150513
1319 sd->hatEffectIDs = NULL;
1320 sd->hatEffectCount = 0;
1321#endif
1322
1323 sd->catch_target_class = PET_CATCH_FAIL;
1324
1325 // Check EXP overflow, since in previous revision EXP on Max Level can be more than 'official' Max EXP
1326 if (pc_is_maxbaselv(sd) && sd->status.base_exp > MAX_LEVEL_BASE_EXP) {
1327 sd->status.base_exp = MAX_LEVEL_BASE_EXP;
1328 clif_updatestatus(sd, SP_BASEEXP);
1329 }
1330 if (pc_is_maxjoblv(sd) && sd->status.job_exp > MAX_LEVEL_JOB_EXP) {
1331 sd->status.job_exp = MAX_LEVEL_JOB_EXP;
1332 clif_updatestatus(sd, SP_JOBEXP);
1333 }
1334
1335 // Request all registries (auth is considered completed whence they arrive)
1336 intif_request_registry(sd,7);
1337 return true;
1338}
1339
1340/*==========================================
1341 * Closes a connection because it failed to be authenticated from the char server.
1342 *------------------------------------------*/
1343void pc_authfail(struct map_session_data *sd)
1344{
1345 clif_authfail_fd(sd->fd, 0);
1346 return;
1347}
1348
1349/**
1350 * Player register a bl as hatred
1351 * @param sd : player session
1352 * @param pos : hate position [0;2]
1353 * @param bl : target bl
1354 * @return false:failed, true:success
1355 */
1356bool pc_set_hate_mob(struct map_session_data *sd, int pos, struct block_list *bl)
1357{
1358 int class_;
1359 if (!sd || !bl || pos < 0 || pos > 2)
1360 return false;
1361 if (sd->hate_mob[pos] != -1)
1362 { //Can't change hate targets.
1363 clif_hate_info(sd, pos, sd->hate_mob[pos], 0); //Display current
1364 return false;
1365 }
1366
1367 class_ = status_get_class(bl);
1368 if (!pcdb_checkid(class_)) {
1369 unsigned int max_hp = status_get_max_hp(bl);
1370 if ((pos == 1 && max_hp < 6000) || (pos == 2 && max_hp < 20000))
1371 return false;
1372 if (pos != status_get_size(bl))
1373 return false; //Wrong size
1374 }
1375 sd->hate_mob[pos] = class_;
1376 pc_setglobalreg(sd, add_str(sg_info[pos].hate_var), class_+1);
1377 clif_hate_info(sd, pos, class_, 1);
1378 return true;
1379}
1380
1381/*==========================================
1382 * Invoked once after the char/account/account2 registry variables are received. [Skotlex]
1383 * We didn't receive item information at this point so DO NOT attempt to do item operations here.
1384 * See intif_parse_StorageReceived() for item operations [lighta]
1385 *------------------------------------------*/
1386void pc_reg_received(struct map_session_data *sd)
1387{
1388 uint8 i;
1389
1390 sd->vars_ok = true;
1391
1392 sd->change_level_2nd = pc_readglobalreg(sd, add_str(JOBCHANGE2ND_VAR));
1393 sd->change_level_3rd = pc_readglobalreg(sd, add_str(JOBCHANGE3RD_VAR));
1394 sd->die_counter = pc_readglobalreg(sd, add_str(PCDIECOUNTER_VAR));
1395
1396 sd->langtype = pc_readaccountreg(sd, add_str(LANGTYPE_VAR));
1397 if (msg_checklangtype(sd->langtype,true) < 0)
1398 sd->langtype = 0; //invalid langtype reset to default
1399
1400 // Cash shop
1401 sd->cashPoints = pc_readaccountreg(sd, add_str(CASHPOINT_VAR));
1402 sd->kafraPoints = pc_readaccountreg(sd, add_str(KAFRAPOINT_VAR));
1403
1404 // Cooking Exp
1405 sd->cook_mastery = pc_readglobalreg(sd, add_str(COOKMASTERY_VAR));
1406
1407 if( (sd->class_&MAPID_BASEMASK) == MAPID_TAEKWON )
1408 { // Better check for class rather than skill to prevent "skill resets" from unsetting this
1409 sd->mission_mobid = pc_readglobalreg(sd, add_str(TKMISSIONID_VAR));
1410 sd->mission_count = pc_readglobalreg(sd, add_str(TKMISSIONCOUNT_VAR));
1411 }
1412
1413 if (battle_config.feature_banking)
1414 sd->bank_vault = pc_readreg2(sd, BANK_VAULT_VAR);
1415
1416 if (battle_config.feature_roulette) {
1417 sd->roulette_point.bronze = pc_readreg2(sd, ROULETTE_BRONZE_VAR);
1418 sd->roulette_point.silver = pc_readreg2(sd, ROULETTE_SILVER_VAR);
1419 sd->roulette_point.gold = pc_readreg2(sd, ROULETTE_GOLD_VAR);
1420 }
1421 sd->roulette.prizeIdx = -1;
1422
1423 //SG map and mob read [Komurka]
1424 for(i=0;i<MAX_PC_FEELHATE;i++) { //for now - someone need to make reading from txt/sql
1425 uint16 j;
1426
1427 if ((j = pc_readglobalreg(sd, add_str(sg_info[i].feel_var))) != 0) {
1428 sd->feel_map[i].index = j;
1429 sd->feel_map[i].m = map_mapindex2mapid(j);
1430 } else {
1431 sd->feel_map[i].index = 0;
1432 sd->feel_map[i].m = -1;
1433 }
1434 sd->hate_mob[i] = pc_readglobalreg(sd, add_str(sg_info[i].hate_var))-1;
1435 }
1436
1437 if ((i = pc_checkskill(sd,RG_PLAGIARISM)) > 0) {
1438 unsigned short skid = pc_readglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM));
1439 sd->cloneskill_idx = skill_get_index(skid);
1440 if (sd->cloneskill_idx > 0) {
1441 sd->status.skill[sd->cloneskill_idx].id = skid;
1442 sd->status.skill[sd->cloneskill_idx].lv = pc_readglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM_LV));
1443 if (sd->status.skill[sd->cloneskill_idx].lv > i)
1444 sd->status.skill[sd->cloneskill_idx].lv = i;
1445 sd->status.skill[sd->cloneskill_idx].flag = SKILL_FLAG_PLAGIARIZED;
1446 }
1447 }
1448 if ((i = pc_checkskill(sd,SC_REPRODUCE)) > 0) {
1449 unsigned short skid = pc_readglobalreg(sd, add_str(SKILL_VAR_REPRODUCE));
1450 sd->reproduceskill_idx = skill_get_index(skid);
1451 if (sd->reproduceskill_idx > 0) {
1452 sd->status.skill[sd->reproduceskill_idx].id = skid;
1453 sd->status.skill[sd->reproduceskill_idx].lv = pc_readglobalreg(sd, add_str(SKILL_VAR_REPRODUCE_LV));
1454 if (i < sd->status.skill[sd->reproduceskill_idx].lv)
1455 sd->status.skill[sd->reproduceskill_idx].lv = i;
1456 sd->status.skill[sd->reproduceskill_idx].flag = SKILL_FLAG_PLAGIARIZED;
1457 }
1458 }
1459 //Weird... maybe registries were reloaded?
1460 if (sd->state.active)
1461 return;
1462 sd->state.active = 1;
1463 sd->state.pc_loaded = false; // Ensure inventory data and status data is loaded before we calculate player stats
1464
1465 intif_storage_request(sd,TABLE_STORAGE, 0, STOR_MODE_ALL); // Request storage data
1466 intif_storage_request(sd,TABLE_CART, 0, STOR_MODE_ALL); // Request cart data
1467 intif_storage_request(sd,TABLE_INVENTORY, 0, STOR_MODE_ALL); // Request inventory data
1468
1469 if (sd->status.party_id)
1470 party_member_joined(sd);
1471 if (sd->status.guild_id)
1472 guild_member_joined(sd);
1473 if( sd->status.clan_id )
1474 clan_member_joined(sd);
1475
1476 // pet
1477 if (sd->status.pet_id > 0)
1478 intif_request_petdata(sd->status.account_id, sd->status.char_id, sd->status.pet_id);
1479
1480 // Homunculus [albator]
1481 if( sd->status.hom_id > 0 )
1482 intif_homunculus_requestload(sd->status.account_id, sd->status.hom_id);
1483 if( sd->status.mer_id > 0 )
1484 intif_mercenary_request(sd->status.mer_id, sd->status.char_id);
1485 if( sd->status.ele_id > 0 )
1486 intif_elemental_request(sd->status.ele_id, sd->status.char_id);
1487
1488 map_addiddb(&sd->bl);
1489 map_delnickdb(sd->status.char_id, sd->status.name);
1490 if (!chrif_auth_finished(sd))
1491 ShowError("pc_reg_received: Failed to properly remove player %d:%d from logging db!\n", sd->status.account_id, sd->status.char_id);
1492
1493 chrif_skillcooldown_request(sd->status.account_id, sd->status.char_id);
1494 chrif_bsdata_request(sd->status.char_id);
1495#ifdef VIP_ENABLE
1496 sd->vip.time = 0;
1497 sd->vip.enabled = 0;
1498 chrif_req_login_operation(sd->status.account_id, sd->status.name, CHRIF_OP_LOGIN_VIP, 0, 1|8, 0); // request VIP information
1499#endif
1500 intif_Mail_requestinbox(sd->status.char_id, 0, MAIL_INBOX_NORMAL); // MAIL SYSTEM - Request Mail Inbox
1501 intif_request_questlog(sd);
1502
1503 if (battle_config.feature_achievement) {
1504 sd->achievement_data.total_score = 0;
1505 sd->achievement_data.level = 0;
1506 sd->achievement_data.save = false;
1507 sd->achievement_data.count = 0;
1508 sd->achievement_data.incompleteCount = 0;
1509 sd->achievement_data.achievements = NULL;
1510 intif_request_achievements(sd->status.char_id);
1511 }
1512
1513 if (sd->state.connect_new == 0 && sd->fd) { //Character already loaded map! Gotta trigger LoadEndAck manually.
1514 sd->state.connect_new = 1;
1515 clif_parse_LoadEndAck(sd->fd, sd);
1516 }
1517
1518 if( pc_isinvisible(sd) ) {
1519 sd->vd.class_ = JT_INVISIBLE;
1520 clif_displaymessage( sd->fd, msg_txt( sd, 11 ) ); // Invisible: On
1521 // decrement the number of pvp players on the map
1522 map_getmapdata(sd->bl.m)->users_pvp--;
1523
1524 if( map_getmapflag(sd->bl.m, MF_PVP) && !map_getmapflag(sd->bl.m, MF_PVP_NOCALCRANK) && sd->pvp_timer != INVALID_TIMER ){
1525 // unregister the player for ranking
1526 delete_timer( sd->pvp_timer, pc_calc_pvprank_timer );
1527 sd->pvp_timer = INVALID_TIMER;
1528 }
1529
1530 clif_changeoption( &sd->bl );
1531 }
1532
1533 channel_autojoin(sd);
1534}
1535
1536static int pc_calc_skillpoint(struct map_session_data* sd)
1537{
1538 uint16 i, skill_point = 0;
1539
1540 nullpo_ret(sd);
1541
1542 for(i = 1; i < MAX_SKILL; i++) {
1543 if( sd->status.skill[i].id && sd->status.skill[i].lv > 0) {
1544 uint16 inf2 = skill_get_inf2(sd->status.skill[i].id);
1545 if ((!(inf2&INF2_QUEST_SKILL) || battle_config.quest_skill_learn) &&
1546 !(inf2&(INF2_WEDDING_SKILL|INF2_SPIRIT_SKILL)) //Do not count wedding/link skills. [Skotlex]
1547 )
1548 {
1549 if(sd->status.skill[i].flag == SKILL_FLAG_PERMANENT)
1550 skill_point += sd->status.skill[i].lv;
1551 else if(sd->status.skill[i].flag >= SKILL_FLAG_REPLACED_LV_0)
1552 skill_point += (sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0);
1553 }
1554 }
1555 }
1556
1557 return skill_point;
1558}
1559
1560static bool pc_grant_allskills(struct map_session_data *sd, bool addlv) {
1561 uint16 i = 0;
1562
1563 if (!sd || !pc_has_permission(sd, PC_PERM_ALL_SKILL) || !SKILL_MAX_DB())
1564 return false;
1565
1566 /**
1567 * Dummy skills must NOT be added here otherwise they'll be displayed in the,
1568 * skill tree and since they have no icons they'll give resource errors
1569 * Get ALL skills except npc/guild ones. [Skotlex]
1570 * Don't add SG_DEVIL [Komurka] and MO_TRIPLEATTACK and RG_SNATCHER [ultramage]
1571 **/
1572 for( i = 0; i < MAX_SKILL; i++ ) {
1573 uint16 skill_id = skill_idx2id(i);
1574 if (!skill_id || (skill_get_inf2(skill_id)&(INF2_NPC_SKILL|INF2_GUILD_SKILL)))
1575 continue;
1576 switch (skill_id) {
1577 case SM_SELFPROVOKE:
1578 case AB_DUPLELIGHT_MELEE:
1579 case AB_DUPLELIGHT_MAGIC:
1580 case WL_CHAINLIGHTNING_ATK:
1581 case WL_TETRAVORTEX_FIRE:
1582 case WL_TETRAVORTEX_WATER:
1583 case WL_TETRAVORTEX_WIND:
1584 case WL_TETRAVORTEX_GROUND:
1585 case WL_SUMMON_ATK_FIRE:
1586 case WL_SUMMON_ATK_WIND:
1587 case WL_SUMMON_ATK_WATER:
1588 case WL_SUMMON_ATK_GROUND:
1589 case LG_OVERBRAND_BRANDISH:
1590 case LG_OVERBRAND_PLUSATK:
1591 case WM_SEVERE_RAINSTORM_MELEE:
1592 case RL_R_TRIP_PLUSATK:
1593 case SG_DEVIL:
1594 case MO_TRIPLEATTACK:
1595 case RG_SNATCHER:
1596 continue;
1597 default:
1598 {
1599 uint8 lv = (uint8)skill_get_max(skill_id);
1600 if (lv > 0) {
1601 sd->status.skill[i].id = skill_id;
1602 if (addlv)
1603 sd->status.skill[i].lv = lv;
1604 }
1605 }
1606 break;
1607 }
1608 }
1609 return true;
1610}
1611
1612/*==========================================
1613 * Calculation of skill level.
1614 * @param sd
1615 *------------------------------------------*/
1616void pc_calc_skilltree(struct map_session_data *sd)
1617{
1618 int i, flag;
1619 int c = 0;
1620
1621 nullpo_retv(sd);
1622 i = pc_calc_skilltree_normalize_job(sd);
1623 c = pc_mapid2jobid(i, sd->status.sex);
1624 if( c == -1 )
1625 { //Unable to normalize job??
1626 ShowError("pc_calc_skilltree: Unable to normalize job %d for character %s (%d:%d)\n", i, sd->status.name, sd->status.account_id, sd->status.char_id);
1627 return;
1628 }
1629 c = pc_class2idx(c);
1630
1631 for( i = 0; i < MAX_SKILL; i++ ) {
1632 if( sd->status.skill[i].flag != SKILL_FLAG_PLAGIARIZED && sd->status.skill[i].flag != SKILL_FLAG_PERM_GRANTED ) //Don't touch these
1633 sd->status.skill[i].id = 0; //First clear skills.
1634 /* permanent skills that must be re-checked */
1635 if( sd->status.skill[i].flag == SKILL_FLAG_PERM_GRANTED ) {
1636 uint16 sk_id = skill_idx2id(i);
1637 if (!sk_id) {
1638 sd->status.skill[i].id = 0;
1639 sd->status.skill[i].lv = 0;
1640 sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
1641 continue;
1642 }
1643 switch (sk_id) {
1644 case NV_TRICKDEAD:
1645 if( (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE ) {
1646 sd->status.skill[i].id = 0;
1647 sd->status.skill[i].lv = 0;
1648 sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
1649 }
1650 break;
1651 }
1652 }
1653 }
1654
1655 for( i = 0; i < MAX_SKILL; i++ ) {
1656 uint16 skill_id = 0;
1657
1658 // Restore original level of skills after deleting earned skills.
1659 if( sd->status.skill[i].flag != SKILL_FLAG_PERMANENT && sd->status.skill[i].flag != SKILL_FLAG_PERM_GRANTED && sd->status.skill[i].flag != SKILL_FLAG_PLAGIARIZED ) {
1660 sd->status.skill[i].lv = (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY) ? 0 : sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
1661 sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
1662 }
1663
1664 //Enable Bard/Dancer spirit linked skills.
1665 if (!(skill_id = skill_idx2id(i)) || skill_id < DC_HUMMING || skill_id > DC_SERVICEFORYOU)
1666 continue;
1667
1668 if( sd->sc.count && sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_BARDDANCER ) {
1669 //Link Dancer skills to bard.
1670 if( sd->status.sex ) {
1671 if( sd->status.skill[i-8].lv < 10 )
1672 continue;
1673 sd->status.skill[i].id = skill_id;
1674 sd->status.skill[i].lv = sd->status.skill[i-8].lv; // Set the level to the same as the linking skill
1675 sd->status.skill[i].flag = SKILL_FLAG_TEMPORARY; // Tag it as a non-savable, non-uppable, bonus skill
1676 }
1677 //Link Bard skills to dancer.
1678 else {
1679 if( sd->status.skill[i].lv < 10 )
1680 continue;
1681 sd->status.skill[i-8].id = skill_id - 8;
1682 sd->status.skill[i-8].lv = sd->status.skill[i].lv; // Set the level to the same as the linking skill
1683 sd->status.skill[i-8].flag = SKILL_FLAG_TEMPORARY; // Tag it as a non-savable, non-uppable, bonus skill
1684 }
1685 }
1686 }
1687
1688 // Removes Taekwon Ranker skill bonus
1689 if ((sd->class_&MAPID_UPPERMASK) != MAPID_TAEKWON) {
1690 uint16 c_ = pc_class2idx(JOB_TAEKWON);
1691
1692 for (i = 0; i < MAX_SKILL_TREE; i++) {
1693 uint16 sk_id = skill_tree[c_][i].skill_id;
1694 uint16 sk_idx = 0;
1695
1696 if (!sk_id || !(sk_idx = skill_get_index(skill_tree[c_][i].skill_id)))
1697 continue;
1698
1699 if (sd->status.skill[sk_idx].flag != SKILL_FLAG_PLAGIARIZED && sd->status.skill[sk_idx].flag != SKILL_FLAG_PERM_GRANTED) {
1700 if (sk_id == NV_BASIC || sk_id == NV_FIRSTAID || sk_id == WE_CALLBABY)
1701 continue;
1702 sd->status.skill[sk_idx].id = 0;
1703 }
1704 }
1705 }
1706
1707 // Grant all skills
1708 pc_grant_allskills(sd, false);
1709
1710 do {
1711 uint16 skid = 0;
1712
1713 flag = 0;
1714 for (i = 0; i < MAX_SKILL_TREE && (skid = skill_tree[c][i].skill_id) > 0; i++) {
1715 bool fail = false;
1716 uint16 sk_idx = skill_get_index(skid);
1717
1718 if (sd->status.skill[sk_idx].id)
1719 continue; //Skill already known.
1720
1721 if (!battle_config.skillfree) {
1722 uint8 j;
1723
1724 // Checking required skills
1725 for(j = 0; j < MAX_PC_SKILL_REQUIRE; j++) {
1726 uint16 sk_need_id = skill_tree[c][i].need[j].skill_id;
1727 uint16 sk_need_idx = 0;
1728
1729 if (sk_need_id && (sk_need_idx = skill_get_index(sk_need_id))) {
1730 short sk_need = sk_need_id;
1731
1732 if (sd->status.skill[sk_need_idx].id == 0 || sd->status.skill[sk_need_idx].flag == SKILL_FLAG_TEMPORARY || sd->status.skill[sk_need_idx].flag == SKILL_FLAG_PLAGIARIZED)
1733 sk_need = 0; //Not learned.
1734 else if (sd->status.skill[sk_need_idx].flag >= SKILL_FLAG_REPLACED_LV_0) //Real learned level
1735 sk_need = sd->status.skill[sk_need_idx].flag - SKILL_FLAG_REPLACED_LV_0;
1736 else
1737 sk_need = pc_checkskill(sd,sk_need_id);
1738
1739 if (sk_need < skill_tree[c][i].need[j].skill_lv) {
1740 fail = true;
1741 break;
1742 }
1743 }
1744 }
1745
1746 if (sd->status.base_level < skill_tree[c][i].baselv) { //We need to get the actual class in this case
1747 int class_ = pc_mapid2jobid(sd->class_, sd->status.sex);
1748 class_ = pc_class2idx(class_);
1749 if (class_ == c || (class_ != c && sd->status.base_level < skill_tree[class_][i].baselv))
1750 fail = true; // base level requirement wasn't satisfied
1751 }
1752 if (sd->status.job_level < skill_tree[c][i].joblv) { //We need to get the actual class in this case
1753 int class_ = pc_mapid2jobid(sd->class_, sd->status.sex);
1754 class_ = pc_class2idx(class_);
1755 if (class_ == c || (class_ != c && sd->status.job_level < skill_tree[class_][i].joblv))
1756 fail = true; // job level requirement wasn't satisfied
1757 }
1758 }
1759
1760 if (!fail) {
1761 int inf2 = skill_get_inf2(skid);
1762
1763 if (!sd->status.skill[sk_idx].lv && (
1764 (inf2&INF2_QUEST_SKILL && !battle_config.quest_skill_learn) ||
1765 inf2&INF2_WEDDING_SKILL ||
1766 (inf2&INF2_SPIRIT_SKILL && !sd->sc.data[SC_SPIRIT])
1767 ))
1768 continue; //Cannot be learned via normal means. Note this check DOES allows raising already known skills.
1769
1770 sd->status.skill[sk_idx].id = skid;
1771
1772 if(inf2&INF2_SPIRIT_SKILL) { //Spirit skills cannot be learned, they will only show up on your tree when you get buffed.
1773 sd->status.skill[sk_idx].lv = 1; // need to manually specify a skill level
1774 sd->status.skill[sk_idx].flag = SKILL_FLAG_TEMPORARY; //So it is not saved, and tagged as a "bonus" skill.
1775 }
1776 flag = 1; // skill list has changed, perform another pass
1777 }
1778 }
1779 } while(flag);
1780
1781 if( c > 0 && sd->status.skill_point == 0 && pc_is_taekwon_ranker(sd) ) {
1782 unsigned short skid = 0;
1783 /* Taekwon Ranker Bonus Skill Tree
1784 ============================================
1785 - Grant All Taekwon Tree, but only as Bonus Skills in case they drop from ranking.
1786 - (c > 0) to avoid grant Novice Skill Tree in case of Skill Reset (need more logic)
1787 - (sd->status.skill_point == 0) to wait until all skill points are assigned to avoid problems with Job Change quest. */
1788
1789 for( i = 0; i < MAX_SKILL_TREE && (skid = skill_tree[c][i].skill_id) > 0; i++ ) {
1790 uint16 sk_idx = 0;
1791 if (!(sk_idx = skill_get_index(skid)))
1792 continue;
1793 if( (skill_get_inf2(skid)&(INF2_QUEST_SKILL|INF2_WEDDING_SKILL)) )
1794 continue; //Do not include Quest/Wedding skills.
1795 if( sd->status.skill[sk_idx].id == 0 ) {
1796 sd->status.skill[sk_idx].id = skid;
1797 sd->status.skill[sk_idx].flag = SKILL_FLAG_TEMPORARY; // So it is not saved, and tagged as a "bonus" skill.
1798 } else if( skid != NV_BASIC )
1799 sd->status.skill[sk_idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[sk_idx].lv; // Remember original level
1800 sd->status.skill[sk_idx].lv = skill_tree_get_max(skid, sd->status.class_);
1801 }
1802 }
1803}
1804
1805//Checks if you can learn a new skill after having leveled up a skill.
1806static void pc_check_skilltree(struct map_session_data *sd)
1807{
1808 int i, flag = 0;
1809 int c = 0;
1810
1811 if (battle_config.skillfree)
1812 return; //Function serves no purpose if this is set
1813
1814 i = pc_calc_skilltree_normalize_job(sd);
1815 c = pc_mapid2jobid(i, sd->status.sex);
1816 if (c == -1) { //Unable to normalize job??
1817 ShowError("pc_check_skilltree: Unable to normalize job %d for character %s (%d:%d)\n", i, sd->status.name, sd->status.account_id, sd->status.char_id);
1818 return;
1819 }
1820 c = pc_class2idx(c);
1821
1822 do {
1823 uint16 skid = 0;
1824
1825 flag = 0;
1826 for (i = 0; i < MAX_SKILL_TREE && (skid = skill_tree[c][i].skill_id) > 0; i++ ) {
1827 uint16 sk_idx = skill_get_index(skid);
1828 bool fail = false;
1829 uint8 j = 0;
1830
1831 if (sd->status.skill[sk_idx].id) //Already learned
1832 continue;
1833
1834 // Checking required skills
1835 for (j = 0; j < MAX_PC_SKILL_REQUIRE; j++) {
1836 uint16 sk_need_id = skill_tree[c][i].need[j].skill_id;
1837 uint16 sk_need_idx = 0;
1838
1839 if (sk_need_id && (sk_need_idx = skill_get_index(sk_need_id))) {
1840 short sk_need = sk_need_id;
1841
1842 if (sd->status.skill[sk_need_idx].id == 0 || sd->status.skill[sk_need_idx].flag == SKILL_FLAG_TEMPORARY || sd->status.skill[sk_need_idx].flag == SKILL_FLAG_PLAGIARIZED)
1843 sk_need = 0; //Not learned.
1844 else if (sd->status.skill[sk_need_idx].flag >= SKILL_FLAG_REPLACED_LV_0) //Real lerned level
1845 sk_need = sd->status.skill[sk_need_idx].flag - SKILL_FLAG_REPLACED_LV_0;
1846 else
1847 sk_need = pc_checkskill(sd,sk_need_id);
1848
1849 if (sk_need < skill_tree[c][i].need[j].skill_lv) {
1850 fail = true;
1851 break;
1852 }
1853 }
1854 }
1855
1856 if( fail )
1857 continue;
1858 if (sd->status.base_level < skill_tree[c][i].baselv || sd->status.job_level < skill_tree[c][i].joblv)
1859 continue;
1860
1861 j = skill_get_inf2(skid);
1862 if( !sd->status.skill[sk_idx].lv && (
1863 (j&INF2_QUEST_SKILL && !battle_config.quest_skill_learn) ||
1864 j&INF2_WEDDING_SKILL ||
1865 (j&INF2_SPIRIT_SKILL && !sd->sc.data[SC_SPIRIT])
1866 ) )
1867 continue; //Cannot be learned via normal means.
1868
1869 sd->status.skill[sk_idx].id = skid;
1870 flag = 1;
1871 }
1872 } while(flag);
1873}
1874
1875// Make sure all the skills are in the correct condition
1876// before persisting to the backend.. [MouseJstr]
1877void pc_clean_skilltree(struct map_session_data *sd)
1878{
1879 uint16 i;
1880 for (i = 0; i < MAX_SKILL; i++){
1881 if (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY || sd->status.skill[i].flag == SKILL_FLAG_PLAGIARIZED) {
1882 sd->status.skill[i].id = 0;
1883 sd->status.skill[i].lv = 0;
1884 sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
1885 }
1886 else if (sd->status.skill[i].flag >= SKILL_FLAG_REPLACED_LV_0){
1887 sd->status.skill[i].lv = sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
1888 sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
1889 }
1890 }
1891}
1892
1893int pc_calc_skilltree_normalize_job(struct map_session_data *sd)
1894{
1895 int skill_point, novice_skills;
1896 int c = sd->class_;
1897
1898 if (!battle_config.skillup_limit || pc_has_permission(sd, PC_PERM_ALL_SKILL))
1899 return c;
1900
1901 skill_point = pc_calc_skillpoint(sd);
1902
1903 novice_skills = job_info[pc_class2idx(JOB_NOVICE)].max_level[1] - 1;
1904
1905 // limit 1st class and above to novice job levels
1906 if(skill_point < novice_skills && (sd->class_&MAPID_BASEMASK) != MAPID_SUMMONER)
1907 {
1908 c = MAPID_NOVICE;
1909 }
1910 // limit 2nd class and above to first class job levels (super novices are exempt)
1911 else if (sd->class_&JOBL_2 && (sd->class_&MAPID_UPPERMASK) != MAPID_SUPER_NOVICE)
1912 {
1913 // regenerate change_level_2nd
1914 if (!sd->change_level_2nd)
1915 {
1916 if (sd->class_&JOBL_THIRD)
1917 {
1918 // if neither 2nd nor 3rd jobchange levels are known, we have to assume a default for 2nd
1919 if (!sd->change_level_3rd)
1920 sd->change_level_2nd = job_info[pc_class2idx(pc_mapid2jobid(sd->class_&MAPID_UPPERMASK, sd->status.sex))].max_level[1];
1921 else
1922 sd->change_level_2nd = 1 + skill_point + sd->status.skill_point
1923 - (sd->status.job_level - 1)
1924 - (sd->change_level_3rd - 1)
1925 - novice_skills;
1926 }
1927 else
1928 {
1929 sd->change_level_2nd = 1 + skill_point + sd->status.skill_point
1930 - (sd->status.job_level - 1)
1931 - novice_skills;
1932
1933 }
1934
1935 pc_setglobalreg(sd, add_str(JOBCHANGE2ND_VAR), sd->change_level_2nd);
1936 }
1937
1938 if (skill_point < novice_skills + (sd->change_level_2nd - 1))
1939 {
1940 c &= MAPID_BASEMASK;
1941 }
1942 // limit 3rd class to 2nd class/trans job levels
1943 else if(sd->class_&JOBL_THIRD)
1944 {
1945 // regenerate change_level_3rd
1946 if (!sd->change_level_3rd)
1947 {
1948 sd->change_level_3rd = 1 + skill_point + sd->status.skill_point
1949 - (sd->status.job_level - 1)
1950 - (sd->change_level_2nd - 1)
1951 - novice_skills;
1952 pc_setglobalreg(sd, add_str(JOBCHANGE3RD_VAR), sd->change_level_3rd);
1953 }
1954
1955 if (skill_point < novice_skills + (sd->change_level_2nd - 1) + (sd->change_level_3rd - 1))
1956 c &= MAPID_UPPERMASK;
1957 }
1958 }
1959
1960 // restore non-limiting flags
1961 c |= sd->class_&(JOBL_UPPER|JOBL_BABY);
1962
1963 return c;
1964}
1965
1966/*==========================================
1967 * Updates the weight status
1968 *------------------------------------------
1969 * 1: overweight 50% for pre-renewal and 70% for renewal
1970 * 2: overweight 90%
1971 * It's assumed that SC_WEIGHT50 and SC_WEIGHT90 are only started/stopped here.
1972 */
1973void pc_updateweightstatus(struct map_session_data *sd)
1974{
1975 int old_overweight;
1976 int new_overweight;
1977
1978 nullpo_retv(sd);
1979
1980 old_overweight = (sd->sc.data[SC_WEIGHT90]) ? 2 : (sd->sc.data[SC_WEIGHT50]) ? 1 : 0;
1981#ifdef RENEWAL
1982 new_overweight = (pc_is90overweight(sd)) ? 2 : (pc_is70overweight(sd)) ? 1 : 0;
1983#else
1984 new_overweight = (pc_is90overweight(sd)) ? 2 : (pc_is50overweight(sd)) ? 1 : 0;
1985#endif
1986
1987 if( old_overweight == new_overweight )
1988 return; // no change
1989
1990 // stop old status change
1991 if( old_overweight == 1 )
1992 status_change_end(&sd->bl, SC_WEIGHT50, INVALID_TIMER);
1993 else if( old_overweight == 2 )
1994 status_change_end(&sd->bl, SC_WEIGHT90, INVALID_TIMER);
1995
1996 // start new status change
1997 if( new_overweight == 1 )
1998 sc_start(&sd->bl,&sd->bl, SC_WEIGHT50, 100, 0, 0);
1999 else if( new_overweight == 2 )
2000 sc_start(&sd->bl,&sd->bl, SC_WEIGHT90, 100, 0, 0);
2001
2002 // update overweight status
2003 sd->regen.state.overweight = new_overweight;
2004}
2005
2006int pc_disguise(struct map_session_data *sd, int class_)
2007{
2008 if (!class_ && !sd->disguise)
2009 return 0;
2010 if (class_ && sd->disguise == class_)
2011 return 0;
2012
2013 if(pc_isinvisible(sd))
2014 { //Character is invisible. Stealth class-change. [Skotlex]
2015 sd->disguise = class_; //viewdata is set on uncloaking.
2016 return 2;
2017 }
2018
2019 if (sd->bl.prev != NULL) {
2020 pc_stop_walking(sd, 0);
2021 clif_clearunit_area(&sd->bl, CLR_OUTSIGHT);
2022 }
2023
2024 if (!class_) {
2025 sd->disguise = 0;
2026 class_ = sd->status.class_;
2027 } else
2028 sd->disguise=class_;
2029
2030 status_set_viewdata(&sd->bl, class_);
2031 clif_changeoption(&sd->bl);
2032
2033 if (sd->bl.prev != NULL) {
2034 clif_spawn(&sd->bl);
2035 if (class_ == sd->status.class_ && pc_iscarton(sd))
2036 { //It seems the cart info is lost on undisguise.
2037 clif_cartlist(sd);
2038 clif_updatestatus(sd,SP_CARTINFO);
2039 }
2040 if (sd->chatID) {
2041 struct chat_data* cd;
2042 if ((cd = (struct chat_data*)map_id2bl(sd->chatID)) != NULL)
2043 clif_dispchat(cd,0);
2044 }
2045 }
2046 return 1;
2047}
2048
2049/// Show error message
2050#define PC_BONUS_SHOW_ERROR(type,type2,val) { ShowError("%s: %s: Invalid %s %d.\n",__FUNCTION__,#type,#type2,(val)); break; }
2051/// Check for valid Element, break & show error message if invalid Element
2052#define PC_BONUS_CHK_ELEMENT(ele,bonus) { if (!CHK_ELEMENT((ele))) { PC_BONUS_SHOW_ERROR((bonus),Element,(ele)); }}
2053/// Check for valid Race, break & show error message if invalid Race
2054#define PC_BONUS_CHK_RACE(rc,bonus) { if (!CHK_RACE((rc))) { PC_BONUS_SHOW_ERROR((bonus),Race,(rc)); }}
2055/// Check for valid Race2, break & show error message if invalid Race2
2056#define PC_BONUS_CHK_RACE2(rc2,bonus) { if (!CHK_RACE2((rc2))) { PC_BONUS_SHOW_ERROR((bonus),Race2,(rc2)); }}
2057/// Check for valid Class, break & show error message if invalid Class
2058#define PC_BONUS_CHK_CLASS(cl,bonus) { if (!CHK_CLASS((cl))) { PC_BONUS_SHOW_ERROR((bonus),Class,(cl)); }}
2059/// Check for valid Size, break & show error message if invalid Size
2060#define PC_BONUS_CHK_SIZE(sz,bonus) { if (!CHK_MOBSIZE((sz))) { PC_BONUS_SHOW_ERROR((bonus),Size,(sz)); }}
2061/// Check for valid SC, break & show error message if invalid SC
2062#define PC_BONUS_CHK_SC(sc,bonus) { if ((sc) <= SC_NONE || (sc) >= SC_MAX) { PC_BONUS_SHOW_ERROR((bonus),Effect,(sc)); }}
2063
2064static void pc_bonus_autospell(struct s_autospell *spell, int max, short id, short lv, short rate, short flag, unsigned short card_id)
2065{
2066 uint8 i;
2067
2068 if( !rate )
2069 return;
2070
2071 for( i = 0; i < max && spell[i].id; i++ )
2072 {
2073 if( (spell[i].card_id == card_id || spell[i].rate < 0 || rate < 0) && spell[i].id == id && spell[i].lv == lv )
2074 {
2075 if( !battle_config.autospell_stacking && spell[i].rate > 0 && rate > 0 )
2076 return;
2077 rate += spell[i].rate;
2078 break;
2079 }
2080 }
2081 if (i == max) {
2082 ShowWarning("pc_bonus_autospell: Reached max (%d) number of autospells per character!\n", max);
2083 return;
2084 }
2085 spell[i].id = id;
2086 spell[i].lv = lv;
2087 spell[i].rate = rate;
2088 //Auto-update flag value.
2089 if (!(flag&BF_RANGEMASK)) flag|=BF_SHORT|BF_LONG; //No range defined? Use both.
2090 if (!(flag&BF_WEAPONMASK)) flag|=BF_WEAPON; //No attack type defined? Use weapon.
2091 if (!(flag&BF_SKILLMASK)) {
2092 if (flag&(BF_MAGIC|BF_MISC)) flag|=BF_SKILL; //These two would never trigger without BF_SKILL
2093 if (flag&BF_WEAPON) flag|=BF_NORMAL; //By default autospells should only trigger on normal weapon attacks.
2094 }
2095 spell[i].flag|= flag;
2096 spell[i].card_id = card_id;
2097}
2098
2099static void pc_bonus_autospell_onskill(struct s_autospell *spell, int max, short src_skill, short id, short lv, short rate, unsigned short card_id)
2100{
2101 uint8 i;
2102
2103 if( !rate )
2104 return;
2105
2106 for( i = 0; i < max && spell[i].id; i++ )
2107 {
2108 ; // each autospell works independently
2109 }
2110
2111 if( i == max )
2112 {
2113 ShowWarning("pc_bonus_autospell_onskill: Reached max (%d) number of autospells per character!\n", max);
2114 return;
2115 }
2116
2117 spell[i].flag = src_skill;
2118 spell[i].id = id;
2119 spell[i].lv = lv;
2120 spell[i].rate = rate;
2121 spell[i].card_id = card_id;
2122 return;
2123}
2124
2125/**
2126 * Add inflict effect bonus for player while attacking/atatcked
2127 * @param effect Effect array
2128 * @param pmax Max array
2129 * @param sc SC/Effect type
2130 * @param rate Success chance
2131 * @param arrow_rate success chance if bonus comes from arrow-type item
2132 * @param flag Target flag
2133 * @param duration Duration. If 0 use default duration lookup for associated skill with level 7
2134 **/
2135static void pc_bonus_addeff(struct s_addeffect* effect, int pmax, enum sc_type sc, short rate, short arrow_rate, unsigned char flag, unsigned int duration)
2136{
2137 uint16 i;
2138
2139 if (!(flag&(ATF_SHORT|ATF_LONG)))
2140 flag |= ATF_SHORT|ATF_LONG; //Default range: both
2141 if (!(flag&(ATF_TARGET|ATF_SELF)))
2142 flag |= ATF_TARGET; //Default target: enemy.
2143 if (!(flag&(ATF_WEAPON|ATF_MAGIC|ATF_MISC)))
2144 flag |= ATF_WEAPON; //Default type: weapon.
2145
2146 if (!duration)
2147 duration =(unsigned int) skill_get_time2(status_sc2skill(sc),7);
2148
2149 for (i = 0; i < pmax && effect[i].flag; i++) {
2150 if (effect[i].sc == sc && effect[i].flag == flag) {
2151 effect[i].rate += rate;
2152 effect[i].arrow_rate += arrow_rate;
2153 effect[i].duration = umax(effect[i].duration, duration);
2154 return;
2155 }
2156 }
2157 if (i == pmax) {
2158 ShowWarning("pc_bonus_addeff: Reached max (%d) number of add effects per character!\n", pmax);
2159 return;
2160 }
2161 effect[i].sc = sc;
2162 effect[i].rate = rate;
2163 effect[i].arrow_rate = arrow_rate;
2164 effect[i].flag = flag;
2165 effect[i].duration = duration;
2166}
2167
2168/**
2169 * Add inflict effect bonus for player while attacking using skill
2170 * @param effect Effect array
2171 * @param pmax Max array
2172 * @param sc SC/Effect type
2173 * @param rate Success chance
2174 * @param flag Target flag
2175 * @param duration Duration. If 0 use default duration lookup for associated skill with level 7
2176 **/
2177static void pc_bonus_addeff_onskill(struct s_addeffectonskill* effect, int pmax, enum sc_type sc, short rate, short skill_id, unsigned char target, unsigned int duration)
2178{
2179 uint8 i;
2180
2181 if (!duration)
2182 duration =(unsigned int) skill_get_time2(status_sc2skill(sc),7);
2183
2184 for( i = 0; i < pmax && effect[i].skill_id; i++ ) {
2185 if( effect[i].sc == sc && effect[i].skill_id == skill_id && effect[i].target == target ) {
2186 effect[i].rate += rate;
2187 effect[i].duration = umax(effect[i].duration, duration);
2188 return;
2189 }
2190 }
2191 if( i == pmax ) {
2192 ShowWarning("pc_bonus_addeff_onskill: Reached max (%d) number of add effects on skill per character!\n", pmax);
2193 return;
2194 }
2195 effect[i].sc = sc;
2196 effect[i].rate = rate;
2197 effect[i].skill_id = skill_id;
2198 effect[i].target = target;
2199 effect[i].duration = duration;
2200}
2201
2202/** Adjust/add drop rate modifier for player
2203* @param drop: Player's sd->add_drop (struct s_add_drop)
2204* @param max: Max bonus can be received
2205* @param nameid: item id that will be dropped
2206* @param group: group id
2207* @param class_: target class
2208* @param race: target race. if < 0, means monster_id
2209* @param rate: rate value: 1 ~ 10000. If < 0, it will be multiplied with mob level/10
2210*/
2211static void pc_bonus_item_drop(struct s_add_drop *drop, const short max, unsigned short nameid, uint16 group, int class_, short race, int rate)
2212{
2213 uint8 i;
2214
2215 if (!nameid && !group) {
2216 ShowWarning("pc_bonus_item_drop: No Item ID nor Item Group ID specified.\n");
2217 return;
2218 }
2219 if (nameid && !itemdb_exists(nameid)) {
2220 ShowWarning("pc_bonus_item_drop: Invalid item id %hu\n",nameid);
2221 return;
2222 }
2223 if (group && !itemdb_group_exists(group)) {
2224 ShowWarning("pc_bonus_item_drop: Invalid Item Group %hu\n",group);
2225 return;
2226 }
2227
2228 //Apply config rate adjustment settings.
2229 if (rate >= 0) { //Absolute drop.
2230 if (battle_config.item_rate_adddrop != 100)
2231 rate = rate*battle_config.item_rate_adddrop/100;
2232 if (rate < battle_config.item_drop_adddrop_min)
2233 rate = battle_config.item_drop_adddrop_min;
2234 else if (rate > battle_config.item_drop_adddrop_max)
2235 rate = battle_config.item_drop_adddrop_max;
2236 } else { //Relative drop, max/min limits are applied at drop time.
2237 if (battle_config.item_rate_adddrop != 100)
2238 rate = rate*battle_config.item_rate_adddrop/100;
2239 if (rate > -1)
2240 rate = -1;
2241 }
2242
2243 //Find match entry, and adjust the rate only
2244 for (i = 0; i < max; i++) {
2245 if (!&drop[i] || (!drop[i].nameid && !drop[i].group))
2246 continue;
2247 if (drop[i].nameid == nameid &&
2248 drop[i].group == group &&
2249 drop[i].race == race &&
2250 drop[i].class_ == class_
2251 )
2252 {
2253 //Adjust the rate if it has same classification
2254 if ((rate < 0 && drop[i].rate < 0) ||
2255 (rate > 0 && drop[i].rate > 0))
2256 {
2257 drop[i].rate += rate;
2258 return;
2259 }
2260 }
2261 }
2262 ARR_FIND(0,max,i,!&drop[i] || (drop[i].nameid == 0 && drop[i].group == 0));
2263 if (i >= max) {
2264 ShowWarning("pc_bonus_item_drop: Reached max (%d) number of added drops per character! (nameid:%hu group:%d class_:%d race:%d rate:%d)\n",max,nameid,group,class_,race,rate);
2265 return;
2266 }
2267 drop[i].nameid = nameid;
2268 drop[i].group = group;
2269 drop[i].race = race;
2270 drop[i].class_ = class_;
2271 drop[i].rate = rate;
2272}
2273
2274bool pc_addautobonus(struct s_autobonus *bonus,char max,const char *script,short rate,unsigned int dur,short flag,const char *other_script,unsigned int pos,bool onskill)
2275{
2276 int i;
2277
2278 ARR_FIND(0, max, i, bonus[i].rate == 0);
2279 if( i == max )
2280 {
2281 ShowWarning("pc_addautobonus: Reached max (%d) number of autobonus per character!\n", max);
2282 return false;
2283 }
2284
2285 if( !onskill )
2286 {
2287 if( !(flag&BF_RANGEMASK) )
2288 flag|=BF_SHORT|BF_LONG; //No range defined? Use both.
2289 if( !(flag&BF_WEAPONMASK) )
2290 flag|=BF_WEAPON; //No attack type defined? Use weapon.
2291 if( !(flag&BF_SKILLMASK) )
2292 {
2293 if( flag&(BF_MAGIC|BF_MISC) )
2294 flag|=BF_SKILL; //These two would never trigger without BF_SKILL
2295 if( flag&BF_WEAPON )
2296 flag|=BF_NORMAL|BF_SKILL;
2297 }
2298 }
2299
2300 bonus[i].rate = rate;
2301 bonus[i].duration = dur;
2302 bonus[i].active = INVALID_TIMER;
2303 bonus[i].atk_type = flag;
2304 bonus[i].pos = pos;
2305 bonus[i].bonus_script = aStrdup(script);
2306 bonus[i].other_script = other_script?aStrdup(other_script):NULL;
2307 return true;
2308}
2309
2310void pc_delautobonus(struct map_session_data* sd, struct s_autobonus *autobonus,char max,bool restore)
2311{
2312 int i;
2313 if (!sd)
2314 return;
2315
2316 for( i = 0; i < max; i++ )
2317 {
2318 if( autobonus[i].active != INVALID_TIMER )
2319 {
2320 if( restore && (sd->state.autobonus&autobonus[i].pos) == autobonus[i].pos)
2321 {
2322 if( autobonus[i].bonus_script )
2323 {
2324 int j;
2325 unsigned int equip_pos_idx = 0;
2326 //Create a list of all equipped positions to see if all items needed for the autobonus are still present [Playtester]
2327 for(j = 0; j < EQI_MAX; j++) {
2328 if(sd->equip_index[j] >= 0)
2329 equip_pos_idx |= sd->inventory.u.items_inventory[sd->equip_index[j]].equip;
2330 }
2331 if((equip_pos_idx&autobonus[i].pos) == autobonus[i].pos)
2332 script_run_autobonus(autobonus[i].bonus_script,sd,autobonus[i].pos);
2333 }
2334 continue;
2335 }
2336 else
2337 { // Logout / Unequipped an item with an activated bonus
2338 delete_timer(autobonus[i].active,pc_endautobonus);
2339 autobonus[i].active = INVALID_TIMER;
2340 }
2341 }
2342
2343 if( autobonus[i].bonus_script ) aFree(autobonus[i].bonus_script);
2344 if( autobonus[i].other_script ) aFree(autobonus[i].other_script);
2345 autobonus[i].bonus_script = autobonus[i].other_script = NULL;
2346 autobonus[i].rate = autobonus[i].atk_type = autobonus[i].duration = autobonus[i].pos = 0;
2347 autobonus[i].active = INVALID_TIMER;
2348 }
2349}
2350
2351void pc_exeautobonus(struct map_session_data *sd,struct s_autobonus *autobonus)
2352{
2353 if (!sd || !autobonus)
2354 return;
2355
2356 if (autobonus->active != INVALID_TIMER)
2357 delete_timer(autobonus->active, pc_endautobonus);
2358
2359 if( autobonus->other_script )
2360 {
2361 int j;
2362 unsigned int equip_pos_idx = 0;
2363 //Create a list of all equipped positions to see if all items needed for the autobonus are still present [Playtester]
2364 for(j = 0; j < EQI_MAX; j++) {
2365 if(sd->equip_index[j] >= 0)
2366 equip_pos_idx |= sd->inventory.u.items_inventory[sd->equip_index[j]].equip;
2367 }
2368 if((equip_pos_idx&autobonus->pos) == autobonus->pos)
2369 script_run_autobonus(autobonus->other_script,sd,autobonus->pos);
2370 }
2371
2372 autobonus->active = add_timer(gettick()+autobonus->duration, pc_endautobonus, sd->bl.id, (intptr_t)autobonus);
2373 sd->state.autobonus |= autobonus->pos;
2374 status_calc_pc(sd,SCO_FORCE);
2375}
2376
2377TIMER_FUNC(pc_endautobonus){
2378 struct map_session_data *sd = map_id2sd(id);
2379 struct s_autobonus *autobonus = (struct s_autobonus *)data;
2380
2381 nullpo_ret(sd);
2382 nullpo_ret(autobonus);
2383
2384 autobonus->active = INVALID_TIMER;
2385 sd->state.autobonus &= ~autobonus->pos;
2386 status_calc_pc(sd,SCO_FORCE);
2387 return 0;
2388}
2389
2390static void pc_bonus_addele(struct map_session_data* sd, unsigned char ele, short rate, short flag)
2391{
2392 uint8 i;
2393 struct weapon_data* wd;
2394
2395 wd = (sd->state.lr_flag ? &sd->left_weapon : &sd->right_weapon);
2396
2397 ARR_FIND(0, MAX_PC_BONUS, i, wd->addele2[i].rate == 0);
2398
2399 if (i == MAX_PC_BONUS)
2400 {
2401 ShowWarning("pc_bonus_addele: Reached max (%d) possible bonuses for this player.\n", MAX_PC_BONUS);
2402 return;
2403 }
2404
2405 if (!(flag&BF_RANGEMASK))
2406 flag |= BF_SHORT|BF_LONG;
2407 if (!(flag&BF_WEAPONMASK))
2408 flag |= BF_WEAPON;
2409 if (!(flag&BF_SKILLMASK))
2410 {
2411 if (flag&(BF_MAGIC|BF_MISC))
2412 flag |= BF_SKILL;
2413 if (flag&BF_WEAPON)
2414 flag |= BF_NORMAL|BF_SKILL;
2415 }
2416
2417 wd->addele2[i].ele = ele;
2418 wd->addele2[i].rate = rate;
2419 wd->addele2[i].flag = flag;
2420}
2421
2422static void pc_bonus_subele(struct map_session_data* sd, unsigned char ele, short rate, short flag)
2423{
2424 uint8 i;
2425
2426 ARR_FIND(0, MAX_PC_BONUS, i, sd->subele2[i].rate == 0);
2427
2428 if (i == MAX_PC_BONUS)
2429 {
2430 ShowWarning("pc_bonus_subele: Reached max (%d) possible bonuses for this player.\n", MAX_PC_BONUS);
2431 return;
2432 }
2433
2434 if (!(flag&BF_RANGEMASK))
2435 flag |= BF_SHORT|BF_LONG;
2436 if (!(flag&BF_WEAPONMASK))
2437 flag |= BF_WEAPON;
2438 if (!(flag&BF_SKILLMASK))
2439 {
2440 if (flag&(BF_MAGIC|BF_MISC))
2441 flag |= BF_SKILL;
2442 if (flag&BF_WEAPON)
2443 flag |= BF_NORMAL|BF_SKILL;
2444 }
2445
2446 sd->subele2[i].ele = ele;
2447 sd->subele2[i].rate = rate;
2448 sd->subele2[i].flag = flag;
2449}
2450
2451/** Add item group heal rate bonus to player
2452* @param sd Player
2453* @param group_id Item Group ID
2454* @param rate
2455* @author Cydh
2456*/
2457void pc_itemgrouphealrate(struct map_session_data *sd, uint16 group_id, short rate) {
2458 struct s_pc_itemgrouphealrate *entry;
2459 uint8 i;
2460
2461 for (i = 0; i < sd->itemgrouphealrate_count; i++) {
2462 if (sd->itemgrouphealrate[i]->group_id == group_id)
2463 break;
2464 }
2465
2466 if (i != sd->itemgrouphealrate_count) {
2467 sd->itemgrouphealrate[i]->rate += rate;
2468 return;
2469 }
2470
2471 if (i >= UINT8_MAX) {
2472 ShowError("pc_itemgrouphealrate_add: Reached max (%d) possible bonuses for this player %d\n", UINT8_MAX);
2473 return;
2474 }
2475
2476 entry = ers_alloc(pc_itemgrouphealrate_ers, struct s_pc_itemgrouphealrate);
2477 entry->group_id = group_id;
2478 entry->rate = rate;
2479
2480 RECREATE(sd->itemgrouphealrate, struct s_pc_itemgrouphealrate *, sd->itemgrouphealrate_count+1);
2481 sd->itemgrouphealrate[sd->itemgrouphealrate_count++] = entry;
2482}
2483
2484/** Clear item group heal rate from player
2485* @param sd Player
2486* @author Cydh
2487*/
2488void pc_itemgrouphealrate_clear(struct map_session_data *sd) {
2489 if (!sd || !sd->itemgrouphealrate_count)
2490 return;
2491 else {
2492 uint8 i;
2493 for( i = 0; i < sd->itemgrouphealrate_count; i++ )
2494 ers_free(pc_itemgrouphealrate_ers, sd->itemgrouphealrate[i]);
2495 sd->itemgrouphealrate_count = 0;
2496 aFree(sd->itemgrouphealrate);
2497 sd->itemgrouphealrate = NULL;
2498 }
2499}
2500
2501/*==========================================
2502 * Add a bonus(type) to player sd
2503 * format: bonus bBonusName,val;
2504 * @param sd
2505 * @param type Bonus type used by bBonusName
2506 * @param val Value that usually for rate or fixed value
2507 *------------------------------------------*/
2508void pc_bonus(struct map_session_data *sd,int type,int val)
2509{
2510 struct status_data *status;
2511 int bonus;
2512 nullpo_retv(sd);
2513
2514 status = &sd->base_status;
2515
2516 switch(type){
2517 case SP_STR:
2518 case SP_AGI:
2519 case SP_VIT:
2520 case SP_INT:
2521 case SP_DEX:
2522 case SP_LUK:
2523 if(sd->state.lr_flag != 2)
2524 sd->param_bonus[type-SP_STR]+=val;
2525 break;
2526 case SP_ATK1:
2527 if(!sd->state.lr_flag) {
2528 bonus = status->rhw.atk + val;
2529 status->rhw.atk = cap_value(bonus, 0, USHRT_MAX);
2530 }
2531 else if(sd->state.lr_flag == 1) {
2532 bonus = status->lhw.atk + val;
2533 status->lhw.atk = cap_value(bonus, 0, USHRT_MAX);
2534 }
2535 break;
2536 case SP_ATK2:
2537 if(!sd->state.lr_flag) {
2538 bonus = status->rhw.atk2 + val;
2539 status->rhw.atk2 = cap_value(bonus, 0, USHRT_MAX);
2540 }
2541 else if(sd->state.lr_flag == 1) {
2542 bonus = status->lhw.atk2 + val;
2543 status->lhw.atk2 = cap_value(bonus, 0, USHRT_MAX);
2544 }
2545 break;
2546 case SP_BASE_ATK:
2547 if(sd->state.lr_flag != 2) {
2548#ifdef RENEWAL
2549 bonus = sd->bonus.eatk + val;
2550 sd->bonus.eatk = cap_value(bonus, SHRT_MIN, SHRT_MAX);
2551#else
2552 bonus = status->batk + val;
2553 status->batk = cap_value(bonus, 0, USHRT_MAX);
2554#endif
2555 }
2556 break;
2557 case SP_DEF1:
2558 if(sd->state.lr_flag != 2) {
2559 bonus = status->def + val;
2560#ifdef RENEWAL
2561 status->def = cap_value(bonus, SHRT_MIN, SHRT_MAX);
2562#else
2563 status->def = cap_value(bonus, CHAR_MIN, CHAR_MAX);
2564#endif
2565 }
2566 break;
2567 case SP_DEF2:
2568 if(sd->state.lr_flag != 2) {
2569 bonus = status->def2 + val;
2570 status->def2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
2571 }
2572 break;
2573 case SP_MDEF1:
2574 if(sd->state.lr_flag != 2) {
2575 bonus = status->mdef + val;
2576#ifdef RENEWAL
2577 status->mdef = cap_value(bonus, SHRT_MIN, SHRT_MAX);
2578#else
2579 status->mdef = cap_value(bonus, CHAR_MIN, CHAR_MAX);
2580#endif
2581 if( sd->state.lr_flag == 3 ) {//Shield, used for royal guard
2582 sd->bonus.shieldmdef += bonus;
2583 }
2584 }
2585 break;
2586 case SP_MDEF2:
2587 if(sd->state.lr_flag != 2) {
2588 bonus = status->mdef2 + val;
2589 status->mdef2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
2590 }
2591 break;
2592 case SP_HIT:
2593 if(sd->state.lr_flag != 2) {
2594 bonus = status->hit + val;
2595 status->hit = cap_value(bonus, SHRT_MIN, SHRT_MAX);
2596 } else
2597 sd->bonus.arrow_hit+=val;
2598 break;
2599 case SP_FLEE1:
2600 if(sd->state.lr_flag != 2) {
2601 bonus = status->flee + val;
2602 status->flee = cap_value(bonus, SHRT_MIN, SHRT_MAX);
2603 }
2604 break;
2605 case SP_FLEE2:
2606 if(sd->state.lr_flag != 2) {
2607 bonus = status->flee2 + val*10;
2608 status->flee2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
2609 }
2610 break;
2611 case SP_CRITICAL:
2612 if(sd->state.lr_flag != 2) {
2613 bonus = status->cri + val*10;
2614 status->cri = cap_value(bonus, SHRT_MIN, SHRT_MAX);
2615 } else
2616 sd->bonus.arrow_cri += val*10;
2617 break;
2618 case SP_ATKELE:
2619 PC_BONUS_CHK_ELEMENT(val,SP_ATKELE);
2620 switch (sd->state.lr_flag)
2621 {
2622 case 2:
2623 switch (sd->status.weapon) {
2624 case W_BOW:
2625 case W_REVOLVER:
2626 case W_RIFLE:
2627 case W_GATLING:
2628 case W_SHOTGUN:
2629 case W_GRENADE:
2630 //Become weapon element.
2631 status->rhw.ele=val;
2632 break;
2633 default: //Become arrow element.
2634 sd->bonus.arrow_ele=val;
2635 break;
2636 }
2637 break;
2638 case 1:
2639 status->lhw.ele=val;
2640 break;
2641 default:
2642 status->rhw.ele=val;
2643 break;
2644 }
2645 break;
2646 case SP_DEFELE:
2647 PC_BONUS_CHK_ELEMENT(val,SP_DEFELE);
2648 if(sd->state.lr_flag != 2)
2649 status->def_ele=val;
2650 break;
2651 case SP_MAXHP:
2652 if(sd->state.lr_flag == 2)
2653 break;
2654 sd->bonus.hp += val;
2655 break;
2656 case SP_MAXSP:
2657 if(sd->state.lr_flag == 2)
2658 break;
2659 sd->bonus.sp += val;
2660 break;
2661 case SP_MAXHPRATE:
2662 if(sd->state.lr_flag != 2)
2663 sd->hprate+=val;
2664 break;
2665 case SP_MAXSPRATE:
2666 if(sd->state.lr_flag != 2)
2667 sd->sprate+=val;
2668 break;
2669 case SP_SPRATE:
2670 if(sd->state.lr_flag != 2)
2671 sd->dsprate+=val;
2672 break;
2673 case SP_ATTACKRANGE:
2674 switch (sd->state.lr_flag) {
2675 case 2:
2676 switch (sd->status.weapon) {
2677 case W_BOW:
2678 case W_REVOLVER:
2679 case W_RIFLE:
2680 case W_GATLING:
2681 case W_SHOTGUN:
2682 case W_GRENADE:
2683 status->rhw.range += val;
2684 }
2685 break;
2686 case 1:
2687 status->lhw.range += val;
2688 break;
2689 default:
2690 status->rhw.range += val;
2691 break;
2692 }
2693 break;
2694 case SP_SPEED_RATE: //Non stackable increase
2695 if(sd->state.lr_flag != 2)
2696 sd->bonus.speed_rate = min(sd->bonus.speed_rate, -val);
2697 break;
2698 case SP_SPEED_ADDRATE: //Stackable increase
2699 if(sd->state.lr_flag != 2)
2700 sd->bonus.speed_add_rate -= val;
2701 break;
2702 case SP_ASPD: //Raw increase
2703 if(sd->state.lr_flag != 2)
2704 sd->bonus.aspd_add -= 10*val;
2705 break;
2706 case SP_ASPD_RATE: //Stackable increase - Made it linear as per rodatazone
2707 if(sd->state.lr_flag != 2)
2708#ifndef RENEWAL_ASPD
2709 status->aspd_rate -= 10*val;
2710#else
2711 status->aspd_rate2 += val;
2712#endif
2713 break;
2714 case SP_HP_RECOV_RATE:
2715 if(sd->state.lr_flag != 2)
2716 sd->hprecov_rate += val;
2717 break;
2718 case SP_SP_RECOV_RATE:
2719 if(sd->state.lr_flag != 2)
2720 sd->sprecov_rate += val;
2721 break;
2722 case SP_CRITICAL_DEF:
2723 if(sd->state.lr_flag != 2)
2724 sd->bonus.critical_def += val;
2725 break;
2726 case SP_NEAR_ATK_DEF:
2727 if(sd->state.lr_flag != 2)
2728 sd->bonus.near_attack_def_rate += val;
2729 break;
2730 case SP_LONG_ATK_DEF:
2731 if(sd->state.lr_flag != 2)
2732 sd->bonus.long_attack_def_rate += val;
2733 break;
2734 case SP_DOUBLE_RATE:
2735 if(sd->state.lr_flag == 0 && sd->bonus.double_rate < val)
2736 sd->bonus.double_rate = val;
2737 break;
2738 case SP_DOUBLE_ADD_RATE:
2739 if(sd->state.lr_flag == 0)
2740 sd->bonus.double_add_rate += val;
2741 break;
2742 case SP_MATK_RATE:
2743 if(sd->state.lr_flag != 2)
2744 sd->matk_rate += val;
2745 break;
2746 case SP_IGNORE_DEF_ELE:
2747 PC_BONUS_CHK_ELEMENT(val,SP_IGNORE_DEF_ELE);
2748 if(!sd->state.lr_flag)
2749 sd->right_weapon.ignore_def_ele |= 1<<val;
2750 else if(sd->state.lr_flag == 1)
2751 sd->left_weapon.ignore_def_ele |= 1<<val;
2752 break;
2753 case SP_IGNORE_DEF_RACE:
2754 PC_BONUS_CHK_RACE(val,SP_IGNORE_DEF_RACE);
2755 if(!sd->state.lr_flag)
2756 sd->right_weapon.ignore_def_race |= 1<<val;
2757 else if(sd->state.lr_flag == 1)
2758 sd->left_weapon.ignore_def_race |= 1<<val;
2759 break;
2760 case SP_IGNORE_DEF_CLASS:
2761 PC_BONUS_CHK_CLASS(val,SP_IGNORE_DEF_CLASS);
2762 if(!sd->state.lr_flag)
2763 sd->right_weapon.ignore_def_class |= 1<<val;
2764 else if(sd->state.lr_flag == 1)
2765 sd->left_weapon.ignore_def_class |= 1<<val;
2766 break;
2767 case SP_ATK_RATE:
2768 if(sd->state.lr_flag != 2)
2769 sd->bonus.atk_rate += val;
2770 break;
2771 case SP_MAGIC_ATK_DEF:
2772 if(sd->state.lr_flag != 2)
2773 sd->bonus.magic_def_rate += val;
2774 break;
2775 case SP_MISC_ATK_DEF:
2776 if(sd->state.lr_flag != 2)
2777 sd->bonus.misc_def_rate += val;
2778 break;
2779 case SP_IGNORE_MDEF_ELE:
2780 PC_BONUS_CHK_ELEMENT(val,SP_IGNORE_MDEF_ELE);
2781 if(sd->state.lr_flag != 2)
2782 sd->bonus.ignore_mdef_ele |= 1<<val;
2783 break;
2784 case SP_IGNORE_MDEF_RACE:
2785 PC_BONUS_CHK_RACE(val,SP_IGNORE_MDEF_RACE);
2786 if(sd->state.lr_flag != 2)
2787 sd->bonus.ignore_mdef_race |= 1<<val;
2788 break;
2789 case SP_PERFECT_HIT_RATE:
2790 if(sd->state.lr_flag != 2 && sd->bonus.perfect_hit < val)
2791 sd->bonus.perfect_hit = val;
2792 break;
2793 case SP_PERFECT_HIT_ADD_RATE:
2794 if(sd->state.lr_flag != 2)
2795 sd->bonus.perfect_hit_add += val;
2796 break;
2797 case SP_CRITICAL_RATE:
2798 if(sd->state.lr_flag != 2)
2799 sd->critical_rate+=val;
2800 break;
2801 case SP_DEF_RATIO_ATK_ELE:
2802 PC_BONUS_CHK_ELEMENT(val,SP_DEF_RATIO_ATK_ELE);
2803 if(!sd->state.lr_flag)
2804 sd->right_weapon.def_ratio_atk_ele |= 1<<val;
2805 else if(sd->state.lr_flag == 1)
2806 sd->left_weapon.def_ratio_atk_ele |= 1<<val;
2807 break;
2808 case SP_DEF_RATIO_ATK_RACE:
2809 PC_BONUS_CHK_RACE(val,SP_DEF_RATIO_ATK_RACE);
2810 if(!sd->state.lr_flag)
2811 sd->right_weapon.def_ratio_atk_race |= 1<<val;
2812 else if(sd->state.lr_flag == 1)
2813 sd->left_weapon.def_ratio_atk_race |= 1<<val;
2814 break;
2815 case SP_DEF_RATIO_ATK_CLASS:
2816 PC_BONUS_CHK_CLASS(val,SP_DEF_RATIO_ATK_CLASS);
2817 if(!sd->state.lr_flag)
2818 sd->right_weapon.def_ratio_atk_class |= 1<<val;
2819 else if(sd->state.lr_flag == 1)
2820 sd->left_weapon.def_ratio_atk_class |= 1<<val;
2821 break;
2822 case SP_HIT_RATE:
2823 if(sd->state.lr_flag != 2)
2824 sd->hit_rate += val;
2825 break;
2826 case SP_FLEE_RATE:
2827 if(sd->state.lr_flag != 2)
2828 sd->flee_rate += val;
2829 break;
2830 case SP_FLEE2_RATE:
2831 if(sd->state.lr_flag != 2)
2832 sd->flee2_rate += val;
2833 break;
2834 case SP_DEF_RATE:
2835 if(sd->state.lr_flag != 2)
2836 sd->def_rate += val;
2837 break;
2838 case SP_DEF2_RATE:
2839 if(sd->state.lr_flag != 2)
2840 sd->def2_rate += val;
2841 break;
2842 case SP_MDEF_RATE:
2843 if(sd->state.lr_flag != 2)
2844 sd->mdef_rate += val;
2845 break;
2846 case SP_MDEF2_RATE:
2847 if(sd->state.lr_flag != 2)
2848 sd->mdef2_rate += val;
2849 break;
2850 case SP_RESTART_FULL_RECOVER:
2851 if(sd->state.lr_flag != 2)
2852 sd->special_state.restart_full_recover = 1;
2853 break;
2854 case SP_NO_CASTCANCEL:
2855 if(sd->state.lr_flag != 2)
2856 sd->special_state.no_castcancel = 1;
2857 break;
2858 case SP_NO_CASTCANCEL2:
2859 if(sd->state.lr_flag != 2)
2860 sd->special_state.no_castcancel2 = 1;
2861 break;
2862 case SP_NO_SIZEFIX:
2863 if(sd->state.lr_flag != 2)
2864 sd->special_state.no_sizefix = 1;
2865 break;
2866 case SP_NO_MAGIC_DAMAGE:
2867 if(sd->state.lr_flag == 2)
2868 break;
2869 val+= sd->special_state.no_magic_damage;
2870 sd->special_state.no_magic_damage = cap_value(val,0,100);
2871 break;
2872 case SP_NO_WEAPON_DAMAGE:
2873 if(sd->state.lr_flag == 2)
2874 break;
2875 val+= sd->special_state.no_weapon_damage;
2876 sd->special_state.no_weapon_damage = cap_value(val,0,100);
2877 break;
2878 case SP_NO_MISC_DAMAGE:
2879 if(sd->state.lr_flag == 2)
2880 break;
2881 val+= sd->special_state.no_misc_damage;
2882 sd->special_state.no_misc_damage = cap_value(val,0,100);
2883 break;
2884 case SP_NO_GEMSTONE:
2885 if(sd->state.lr_flag != 2 && sd->special_state.no_gemstone != 2)
2886 sd->special_state.no_gemstone = 1;
2887 break;
2888 case SP_INTRAVISION: // Maya Purple Card effect allowing to see Hiding/Cloaking people [DracoRPG]
2889 if(sd->state.lr_flag != 2) {
2890 sd->special_state.intravision = 1;
2891 clif_status_load(&sd->bl, EFST_CLAIRVOYANCE, 1);
2892 }
2893 break;
2894 case SP_NO_KNOCKBACK:
2895 if(sd->state.lr_flag != 2)
2896 sd->special_state.no_knockback = 1;
2897 break;
2898 case SP_SPLASH_RANGE:
2899 if(sd->bonus.splash_range < val)
2900 sd->bonus.splash_range = val;
2901 break;
2902 case SP_SPLASH_ADD_RANGE:
2903 sd->bonus.splash_add_range += val;
2904 break;
2905 case SP_SHORT_WEAPON_DAMAGE_RETURN:
2906 if(sd->state.lr_flag != 2)
2907 sd->bonus.short_weapon_damage_return += val;
2908 break;
2909 case SP_LONG_WEAPON_DAMAGE_RETURN:
2910 if(sd->state.lr_flag != 2)
2911 sd->bonus.long_weapon_damage_return += val;
2912 break;
2913 case SP_MAGIC_DAMAGE_RETURN: //AppleGirl Was Here
2914 if(sd->state.lr_flag != 2)
2915 sd->bonus.magic_damage_return += val;
2916 break;
2917 case SP_ALL_STATS: // [Valaris]
2918 if(sd->state.lr_flag!=2) {
2919 sd->param_bonus[SP_STR-SP_STR]+=val;
2920 sd->param_bonus[SP_AGI-SP_STR]+=val;
2921 sd->param_bonus[SP_VIT-SP_STR]+=val;
2922 sd->param_bonus[SP_INT-SP_STR]+=val;
2923 sd->param_bonus[SP_DEX-SP_STR]+=val;
2924 sd->param_bonus[SP_LUK-SP_STR]+=val;
2925 }
2926 break;
2927 case SP_AGI_VIT: // [Valaris]
2928 if(sd->state.lr_flag!=2) {
2929 sd->param_bonus[SP_AGI-SP_STR]+=val;
2930 sd->param_bonus[SP_VIT-SP_STR]+=val;
2931 }
2932 break;
2933 case SP_AGI_DEX_STR: // [Valaris]
2934 if(sd->state.lr_flag!=2) {
2935 sd->param_bonus[SP_AGI-SP_STR]+=val;
2936 sd->param_bonus[SP_DEX-SP_STR]+=val;
2937 sd->param_bonus[SP_STR-SP_STR]+=val;
2938 }
2939 break;
2940 case SP_PERFECT_HIDE: // [Valaris]
2941 if(sd->state.lr_flag!=2)
2942 sd->special_state.perfect_hiding=1;
2943 break;
2944 case SP_UNBREAKABLE:
2945 if(sd->state.lr_flag!=2)
2946 sd->bonus.unbreakable += val;
2947 break;
2948 case SP_UNBREAKABLE_WEAPON:
2949 if(sd->state.lr_flag != 2)
2950 sd->bonus.unbreakable_equip |= EQP_WEAPON;
2951 break;
2952 case SP_UNBREAKABLE_ARMOR:
2953 if(sd->state.lr_flag != 2)
2954 sd->bonus.unbreakable_equip |= EQP_ARMOR;
2955 break;
2956 case SP_UNBREAKABLE_HELM:
2957 if(sd->state.lr_flag != 2)
2958 sd->bonus.unbreakable_equip |= EQP_HELM;
2959 break;
2960 case SP_UNBREAKABLE_SHIELD:
2961 if(sd->state.lr_flag != 2)
2962 sd->bonus.unbreakable_equip |= EQP_SHIELD;
2963 break;
2964 case SP_UNBREAKABLE_GARMENT:
2965 if(sd->state.lr_flag != 2)
2966 sd->bonus.unbreakable_equip |= EQP_GARMENT;
2967 break;
2968 case SP_UNBREAKABLE_SHOES:
2969 if(sd->state.lr_flag != 2)
2970 sd->bonus.unbreakable_equip |= EQP_SHOES;
2971 break;
2972 case SP_CLASSCHANGE: // [Valaris]
2973 if(sd->state.lr_flag !=2)
2974 sd->bonus.classchange=val;
2975 break;
2976 case SP_LONG_ATK_RATE:
2977 if(sd->state.lr_flag != 2) //[Lupus] it should stack, too. As any other cards rate bonuses
2978 sd->bonus.long_attack_atk_rate+=val;
2979 break;
2980 case SP_BREAK_WEAPON_RATE:
2981 if(sd->state.lr_flag != 2)
2982 sd->bonus.break_weapon_rate+=val;
2983 break;
2984 case SP_BREAK_ARMOR_RATE:
2985 if(sd->state.lr_flag != 2)
2986 sd->bonus.break_armor_rate+=val;
2987 break;
2988 case SP_ADD_STEAL_RATE:
2989 if(sd->state.lr_flag != 2)
2990 sd->bonus.add_steal_rate+=val;
2991 break;
2992 case SP_DELAYRATE:
2993 if(sd->state.lr_flag != 2)
2994 sd->delayrate+=val;
2995 break;
2996 case SP_CRIT_ATK_RATE:
2997 if(sd->state.lr_flag != 2)
2998 sd->bonus.crit_atk_rate += val;
2999 break;
3000 case SP_NO_REGEN:
3001 if(sd->state.lr_flag != 2)
3002 sd->regen.state.block|=val;
3003 break;
3004 case SP_UNSTRIPABLE_WEAPON:
3005 if(sd->state.lr_flag != 2)
3006 sd->bonus.unstripable_equip |= EQP_WEAPON;
3007 break;
3008 case SP_UNSTRIPABLE:
3009 case SP_UNSTRIPABLE_ARMOR:
3010 if(sd->state.lr_flag != 2)
3011 sd->bonus.unstripable_equip |= EQP_ARMOR;
3012 break;
3013 case SP_UNSTRIPABLE_HELM:
3014 if(sd->state.lr_flag != 2)
3015 sd->bonus.unstripable_equip |= EQP_HELM;
3016 break;
3017 case SP_UNSTRIPABLE_SHIELD:
3018 if(sd->state.lr_flag != 2)
3019 sd->bonus.unstripable_equip |= EQP_SHIELD;
3020 break;
3021 case SP_HP_DRAIN_VALUE: // bonus bHPDrainValue,n;
3022 if(!sd->state.lr_flag) {
3023 sd->right_weapon.hp_drain_class[CLASS_NORMAL] += val;
3024 sd->right_weapon.hp_drain_class[CLASS_BOSS] += val;
3025 } else if(sd->state.lr_flag == 1) {
3026 sd->left_weapon.hp_drain_class[CLASS_NORMAL] += val;
3027 sd->left_weapon.hp_drain_class[CLASS_BOSS] += val;
3028 }
3029 break;
3030 case SP_SP_DRAIN_VALUE: // bonus bSPDrainValue,n;
3031 if(!sd->state.lr_flag) {
3032 sd->right_weapon.sp_drain_class[CLASS_NORMAL] += val;
3033 sd->right_weapon.sp_drain_class[CLASS_BOSS] += val;
3034 } else if(sd->state.lr_flag == 1) {
3035 sd->left_weapon.sp_drain_class[CLASS_NORMAL] += val;
3036 sd->left_weapon.sp_drain_class[CLASS_BOSS] += val;
3037 }
3038 break;
3039 case SP_SP_GAIN_VALUE:
3040 if(!sd->state.lr_flag)
3041 sd->bonus.sp_gain_value += val;
3042 break;
3043 case SP_HP_GAIN_VALUE:
3044 if(!sd->state.lr_flag)
3045 sd->bonus.hp_gain_value += val;
3046 break;
3047 case SP_MAGIC_SP_GAIN_VALUE:
3048 if(!sd->state.lr_flag)
3049 sd->bonus.magic_sp_gain_value += val;
3050 break;
3051 case SP_MAGIC_HP_GAIN_VALUE:
3052 if(!sd->state.lr_flag)
3053 sd->bonus.magic_hp_gain_value += val;
3054 break;
3055 case SP_ADD_HEAL_RATE:
3056 if(sd->state.lr_flag != 2)
3057 sd->bonus.add_heal_rate += val;
3058 break;
3059 case SP_ADD_HEAL2_RATE:
3060 if(sd->state.lr_flag != 2)
3061 sd->bonus.add_heal2_rate += val;
3062 break;
3063 case SP_ADD_ITEM_HEAL_RATE:
3064 if(sd->state.lr_flag != 2)
3065 sd->bonus.itemhealrate2 += val;
3066 break;
3067 case SP_EMATK:
3068 if(sd->state.lr_flag != 2)
3069 sd->bonus.ematk += val;
3070 break;
3071#ifdef RENEWAL_CAST
3072 case SP_FIXCASTRATE:
3073 if(sd->state.lr_flag != 2)
3074 sd->bonus.fixcastrate = min(sd->bonus.fixcastrate,val);
3075 break;
3076 case SP_ADD_FIXEDCAST:
3077 if(sd->state.lr_flag != 2)
3078 sd->bonus.add_fixcast += val;
3079 break;
3080 case SP_CASTRATE:
3081 case SP_VARCASTRATE:
3082 if(sd->state.lr_flag != 2)
3083 sd->bonus.varcastrate -= val;
3084 break;
3085 case SP_ADD_VARIABLECAST:
3086 if(sd->state.lr_flag != 2)
3087 sd->bonus.add_varcast += val;
3088 break;
3089#else
3090 case SP_ADD_FIXEDCAST:
3091 case SP_FIXCASTRATE:
3092 case SP_ADD_VARIABLECAST:
3093 //ShowWarning("pc_bonus: non-RENEWAL_CAST doesn't support this bonus %d.\n", type);
3094 break;
3095 case SP_VARCASTRATE:
3096 case SP_CASTRATE:
3097 if(sd->state.lr_flag != 2)
3098 sd->castrate += val;
3099 break;
3100#endif
3101 case SP_ADDMAXWEIGHT:
3102 if (sd->state.lr_flag != 2)
3103 sd->add_max_weight += val;
3104 break;
3105 case SP_ABSORB_DMG_MAXHP: // bonus bAbsorbDmgMaxHP,n;
3106 sd->bonus.absorb_dmg_maxhp = max(sd->bonus.absorb_dmg_maxhp, val);
3107 break;
3108 case SP_CRITICAL_RANGEATK: // bonus bCriticalLong,n;
3109 if (sd->state.lr_flag != 2)
3110 sd->bonus.critical_rangeatk += val*10;
3111 else
3112 sd->bonus.arrow_cri += val*10;
3113 break;
3114 case SP_WEAPON_ATK_RATE:
3115 if (sd->state.lr_flag != 2)
3116 sd->bonus.weapon_atk_rate += val;
3117 break;
3118 case SP_WEAPON_MATK_RATE:
3119 if (sd->state.lr_flag != 2)
3120 sd->bonus.weapon_matk_rate += val;
3121 break;
3122 case SP_NO_MADO_FUEL:
3123 if (sd->state.lr_flag != 2)
3124 sd->special_state.no_mado_fuel = 1;
3125 break;
3126 default:
3127 if (running_npc_stat_calc_event) {
3128 ShowWarning("pc_bonus: unknown bonus type %d %d in OnPCStatCalcEvent!\n", type, val);
3129 }
3130 else if (current_equip_combo_pos > 0) {
3131 ShowWarning("pc_bonus: unknown bonus type %d %d in a combo with item #%d\n", type, val, sd->inventory_data[pc_checkequip( sd, current_equip_combo_pos )]->nameid);
3132 }
3133 else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
3134 ShowWarning("pc_bonus: unknown bonus type %d %d in item #%d\n", type, val, current_equip_card_id ? current_equip_card_id : sd->inventory_data[current_equip_item_index]->nameid);
3135 }
3136 else {
3137 ShowWarning("pc_bonus: unknown bonus type %d %d in unknown usage. Report this!\n", type, val);
3138 }
3139 break;
3140 }
3141}
3142
3143/*==========================================
3144 * Player bonus (type) with args type2 and val, called trough bonus2 (npc)
3145 * format: bonus2 bBonusName,type2,val;
3146 * @param sd
3147 * @param type Bonus type used by bBonusName
3148 * @param type2
3149 * @param val Value that usually for rate or fixed value
3150 *------------------------------------------*/
3151void pc_bonus2(struct map_session_data *sd,int type,int type2,int val)
3152{
3153 int i;
3154
3155 nullpo_retv(sd);
3156
3157 switch(type){
3158 case SP_ADDELE: // bonus2 bAddEle,e,x;
3159 PC_BONUS_CHK_ELEMENT(type2,SP_ADDELE);
3160 if(!sd->state.lr_flag || sd->state.lr_flag == 3)
3161 sd->right_weapon.addele[type2]+=val;
3162 else if(sd->state.lr_flag == 1)
3163 sd->left_weapon.addele[type2]+=val;
3164 else if(sd->state.lr_flag == 2)
3165 sd->arrow_addele[type2]+=val;
3166 break;
3167 case SP_ADDRACE: // bonus2 bAddRace,r,x;
3168 PC_BONUS_CHK_RACE(type2,SP_ADDRACE);
3169 if(!sd->state.lr_flag || sd->state.lr_flag == 3)
3170 sd->right_weapon.addrace[type2]+=val;
3171 else if(sd->state.lr_flag == 1)
3172 sd->left_weapon.addrace[type2]+=val;
3173 else if(sd->state.lr_flag == 2)
3174 sd->arrow_addrace[type2]+=val;
3175 break;
3176 case SP_ADDCLASS: // bonus2 bAddClass,c,x;
3177 PC_BONUS_CHK_CLASS(type2,SP_ADDCLASS);
3178 if(!sd->state.lr_flag || sd->state.lr_flag == 3)
3179 sd->right_weapon.addclass[type2]+=val;
3180 else if(sd->state.lr_flag == 1)
3181 sd->left_weapon.addclass[type2]+=val;
3182 else if(sd->state.lr_flag == 2)
3183 sd->arrow_addclass[type2]+=val;
3184 break;
3185 case SP_ADDSIZE: // bonus2 bAddSize,s,x;
3186 PC_BONUS_CHK_SIZE(type2,SP_ADDSIZE);
3187 if(!sd->state.lr_flag || sd->state.lr_flag == 3)
3188 sd->right_weapon.addsize[type2]+=val;
3189 else if(sd->state.lr_flag == 1)
3190 sd->left_weapon.addsize[type2]+=val;
3191 else if(sd->state.lr_flag == 2)
3192 sd->arrow_addsize[type2]+=val;
3193 break;
3194 case SP_SUBELE: // bonus2 bSubEle,e,x;
3195 PC_BONUS_CHK_ELEMENT(type2,SP_SUBELE);
3196 if(sd->state.lr_flag != 2)
3197 sd->subele_script[type2] += val;
3198 break;
3199 case SP_SUBRACE: // bonus2 bSubRace,r,x;
3200 PC_BONUS_CHK_RACE(type2,SP_SUBRACE);
3201 if(sd->state.lr_flag != 2)
3202 sd->subrace[type2]+=val;
3203 break;
3204 case SP_SUBCLASS: // bonus2 bSubClass,c,x;
3205 PC_BONUS_CHK_CLASS(type2,SP_SUBCLASS);
3206 if(sd->state.lr_flag != 2)
3207 sd->subclass[type2]+=val;
3208 break;
3209 case SP_ADDEFF: // bonus2 bAddEff,eff,n;
3210 PC_BONUS_CHK_SC(type2,SP_ADDEFF);
3211 pc_bonus_addeff(sd->addeff, ARRAYLENGTH(sd->addeff), (sc_type)type2,
3212 sd->state.lr_flag != 2 ? val : 0, sd->state.lr_flag == 2 ? val : 0, 0, 0);
3213 break;
3214 case SP_ADDEFF2: // bonus2 bAddEff2,eff,n;
3215 PC_BONUS_CHK_SC(type2,SP_ADDEFF2);
3216 pc_bonus_addeff(sd->addeff, ARRAYLENGTH(sd->addeff), (sc_type)type2,
3217 sd->state.lr_flag != 2 ? val : 0, sd->state.lr_flag == 2 ? val : 0, ATF_SELF, 0);
3218 break;
3219 case SP_RESEFF: // bonus2 bResEff,eff,n;
3220 if (type2 < SC_COMMON_MIN || type2 > SC_COMMON_MAX) {
3221 ShowError("pc_bonus2: SP_RESEFF: %d is invalid effect.\n", type2);
3222 break;
3223 }
3224 if(sd->state.lr_flag == 2)
3225 break;
3226 i = sd->reseff[type2]+val;
3227 sd->reseff[type2]= cap_value(i, -10000, 10000);
3228 break;
3229 case SP_MAGIC_ADDELE: // bonus2 bMagicAddEle,e,x;
3230 PC_BONUS_CHK_ELEMENT(type2,SP_MAGIC_ADDELE);
3231 if(sd->state.lr_flag != 2)
3232 sd->magic_addele_script[type2] += val;
3233 break;
3234 case SP_MAGIC_ADDRACE: // bonus2 bMagicAddRace,r,x;
3235 PC_BONUS_CHK_RACE(type2,SP_MAGIC_ADDRACE);
3236 if(sd->state.lr_flag != 2)
3237 sd->magic_addrace[type2]+=val;
3238 break;
3239 case SP_MAGIC_ADDCLASS: // bonus2 bMagicAddClass,c,x;
3240 PC_BONUS_CHK_CLASS(type2,SP_MAGIC_ADDCLASS);
3241 if(sd->state.lr_flag != 2)
3242 sd->magic_addclass[type2]+=val;
3243 break;
3244 case SP_MAGIC_ADDSIZE: // bonus2 bMagicAddSize,s,x;
3245 PC_BONUS_CHK_SIZE(type2,SP_MAGIC_ADDSIZE);
3246 if(sd->state.lr_flag != 2)
3247 sd->magic_addsize[type2]+=val;
3248 break;
3249 case SP_MAGIC_ATK_ELE: // bonus2 bMagicAtkEle,e,x;
3250 PC_BONUS_CHK_ELEMENT(type2,SP_MAGIC_ATK_ELE);
3251 if(sd->state.lr_flag != 2)
3252 sd->magic_atk_ele[type2]+=val;
3253 break;
3254 case SP_ADD_DAMAGE_CLASS: // bonus2 bAddDamageClass,mid,x;
3255 switch (sd->state.lr_flag) {
3256 case 0: //Right hand
3257 ARR_FIND(0, ARRAYLENGTH(sd->right_weapon.add_dmg), i, sd->right_weapon.add_dmg[i].rate == 0 || sd->right_weapon.add_dmg[i].class_ == type2);
3258 if (i == ARRAYLENGTH(sd->right_weapon.add_dmg))
3259 {
3260 ShowError("pc_bonus2: SP_ADD_DAMAGE_CLASS: Reached max (%d) number of add Class dmg bonuses per character!\n", ARRAYLENGTH(sd->right_weapon.add_dmg));
3261 break;
3262 }
3263 sd->right_weapon.add_dmg[i].class_ = type2;
3264 sd->right_weapon.add_dmg[i].rate += val;
3265 if (!sd->right_weapon.add_dmg[i].rate) //Shift the rest of elements up.
3266 memmove(&sd->right_weapon.add_dmg[i], &sd->right_weapon.add_dmg[i+1], sizeof(sd->right_weapon.add_dmg) - (i+1)*sizeof(sd->right_weapon.add_dmg[0]));
3267 break;
3268 case 1: //Left hand
3269 ARR_FIND(0, ARRAYLENGTH(sd->left_weapon.add_dmg), i, sd->left_weapon.add_dmg[i].rate == 0 || sd->left_weapon.add_dmg[i].class_ == type2);
3270 if (i == ARRAYLENGTH(sd->left_weapon.add_dmg))
3271 {
3272 ShowError("pc_bonus2: SP_ADD_DAMAGE_CLASS: Reached max (%d) number of add Class dmg bonuses per character!\n", ARRAYLENGTH(sd->left_weapon.add_dmg));
3273 break;
3274 }
3275 sd->left_weapon.add_dmg[i].class_ = type2;
3276 sd->left_weapon.add_dmg[i].rate += val;
3277 if (!sd->left_weapon.add_dmg[i].rate) //Shift the rest of elements up.
3278 memmove(&sd->left_weapon.add_dmg[i], &sd->left_weapon.add_dmg[i+1], sizeof(sd->left_weapon.add_dmg) - (i+1)*sizeof(sd->left_weapon.add_dmg[0]));
3279 break;
3280 }
3281 break;
3282 case SP_ADD_MAGIC_DAMAGE_CLASS: // bonus2 bAddMagicDamageClass,mid,x;
3283 if(sd->state.lr_flag == 2)
3284 break;
3285 ARR_FIND(0, ARRAYLENGTH(sd->add_mdmg), i, sd->add_mdmg[i].rate == 0 || sd->add_mdmg[i].class_ == type2);
3286 if (i == ARRAYLENGTH(sd->add_mdmg))
3287 {
3288 ShowError("pc_bonus2: SP_ADD_MAGIC_DAMAGE_CLASS: Reached max (%d) number of add Class magic dmg bonuses per character!\n", ARRAYLENGTH(sd->add_mdmg));
3289 break;
3290 }
3291 sd->add_mdmg[i].class_ = type2;
3292 sd->add_mdmg[i].rate += val;
3293 if (!sd->add_mdmg[i].rate) //Shift the rest of elements up.
3294 memmove(&sd->add_mdmg[i], &sd->add_mdmg[i+1], sizeof(sd->add_mdmg) - (i+1)*sizeof(sd->add_mdmg[0]));
3295 break;
3296 case SP_ADD_DEF_MONSTER: // bonus2 bAddDefMonster,mid,x;
3297 if(sd->state.lr_flag == 2)
3298 break;
3299 ARR_FIND(0, ARRAYLENGTH(sd->add_def), i, sd->add_def[i].rate == 0 || sd->add_def[i].class_ == type2);
3300 if (i == ARRAYLENGTH(sd->add_def))
3301 {
3302 ShowError("pc_bonus2: SP_ADD_DEF_MONSTER: Reached max (%d) number of add Class def bonuses per character!\n", ARRAYLENGTH(sd->add_def));
3303 break;
3304 }
3305 sd->add_def[i].class_ = type2;
3306 sd->add_def[i].rate += val;
3307 if (!sd->add_def[i].rate) //Shift the rest of elements up.
3308 memmove(&sd->add_def[i], &sd->add_def[i+1], sizeof(sd->add_def) - (i+1)*sizeof(sd->add_def[0]));
3309 break;
3310 case SP_ADD_MDEF_MONSTER: // bonus2 bAddMDefMonster,mid,x;
3311 if(sd->state.lr_flag == 2)
3312 break;
3313 ARR_FIND(0, ARRAYLENGTH(sd->add_mdef), i, sd->add_mdef[i].rate == 0 || sd->add_mdef[i].class_ == type2);
3314 if (i == ARRAYLENGTH(sd->add_mdef))
3315 {
3316 ShowError("pc_bonus2: SP_ADD_MDEF_MONSTER: Reached max (%d) number of add Class mdef bonuses per character!\n", ARRAYLENGTH(sd->add_mdef));
3317 break;
3318 }
3319 sd->add_mdef[i].class_ = type2;
3320 sd->add_mdef[i].rate += val;
3321 if (!sd->add_mdef[i].rate) //Shift the rest of elements up.
3322 memmove(&sd->add_mdef[i], &sd->add_mdef[i+1], sizeof(sd->add_mdef) - (i+1)*sizeof(sd->add_mdef[0]));
3323 break;
3324 case SP_HP_DRAIN_RATE: // bonus2 bHPDrainRate,x,n;
3325 if(!sd->state.lr_flag) {
3326 sd->right_weapon.hp_drain_rate.rate += type2;
3327 sd->right_weapon.hp_drain_rate.per += val;
3328 }
3329 else if(sd->state.lr_flag == 1) {
3330 sd->left_weapon.hp_drain_rate.rate += type2;
3331 sd->left_weapon.hp_drain_rate.per += val;
3332 }
3333 break;
3334 case SP_SP_DRAIN_RATE: // bonus2 bSPDrainRate,x,n;
3335 if(!sd->state.lr_flag) {
3336 sd->right_weapon.sp_drain_rate.rate += type2;
3337 sd->right_weapon.sp_drain_rate.per += val;
3338 }
3339 else if(sd->state.lr_flag == 1) {
3340 sd->left_weapon.sp_drain_rate.rate += type2;
3341 sd->left_weapon.sp_drain_rate.per += val;
3342 }
3343 break;
3344 case SP_SP_VANISH_RATE: // bonus2 bSPVanishRate,x,n;
3345 if(sd->state.lr_flag != 2) {
3346 sd->bonus.sp_vanish_rate += type2;
3347 sd->bonus.sp_vanish_per += val;
3348 }
3349 break;
3350 case SP_HP_VANISH_RATE: // bonus2 bHPVanishRate,x,n;
3351 if(sd->state.lr_flag != 2) {
3352 sd->bonus.hp_vanish_rate += type2;
3353 sd->bonus.hp_vanish_per += val;
3354 }
3355 break;
3356 case SP_GET_ZENY_NUM: // bonus2 bGetZenyNum,x,n;
3357 if(sd->state.lr_flag != 2 && sd->bonus.get_zeny_rate < val) {
3358 sd->bonus.get_zeny_rate = val;
3359 sd->bonus.get_zeny_num = type2;
3360 }
3361 break;
3362 case SP_ADD_GET_ZENY_NUM: // bonus2 bAddGetZenyNum,x,n;
3363 if(sd->state.lr_flag != 2) {
3364 sd->bonus.get_zeny_rate += val;
3365 sd->bonus.get_zeny_num += type2;
3366 }
3367 break;
3368 case SP_WEAPON_COMA_ELE: // bonus2 bWeaponComaEle,e,n;
3369 PC_BONUS_CHK_ELEMENT(type2,SP_WEAPON_COMA_ELE);
3370 if(sd->state.lr_flag == 2)
3371 break;
3372 sd->weapon_coma_ele[type2] += val;
3373 sd->special_state.bonus_coma = 1;
3374 break;
3375 case SP_WEAPON_COMA_RACE: // bonus2 bWeaponComaRace,r,n;
3376 PC_BONUS_CHK_RACE(type2,SP_WEAPON_COMA_RACE);
3377 if(sd->state.lr_flag == 2)
3378 break;
3379 sd->weapon_coma_race[type2] += val;
3380 sd->special_state.bonus_coma = 1;
3381 break;
3382 case SP_WEAPON_COMA_CLASS: // bonus2 bWeaponComaClass,c,n;
3383 PC_BONUS_CHK_CLASS(type2,SP_WEAPON_COMA_CLASS);
3384 if(sd->state.lr_flag == 2)
3385 break;
3386 sd->weapon_coma_class[type2] += val;
3387 sd->special_state.bonus_coma = 1;
3388 break;
3389 case SP_WEAPON_ATK: // bonus2 bWeaponAtk,w,n;
3390 if(sd->state.lr_flag != 2)
3391 sd->weapon_atk[type2]+=val;
3392 break;
3393 case SP_WEAPON_DAMAGE_RATE: // bonus2 bWeaponDamageRate,w,n;
3394 if(sd->state.lr_flag != 2)
3395 sd->weapon_damage_rate[type2]+=val;
3396 break;
3397 case SP_CRITICAL_ADDRACE: // bonus2 bCriticalAddRace,r,n;
3398 PC_BONUS_CHK_RACE(type2,SP_CRITICAL_ADDRACE);
3399 if(sd->state.lr_flag != 2)
3400 sd->critaddrace[type2] += val*10;
3401 break;
3402 case SP_ADDEFF_WHENHIT: // bonus2 bAddEffWhenHit,eff,n;
3403 PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
3404 if(sd->state.lr_flag != 2)
3405 pc_bonus_addeff(sd->addeff_atked, ARRAYLENGTH(sd->addeff_atked), (sc_type)type2, val, 0, 0, 0);
3406 break;
3407 case SP_SKILL_ATK: // bonus2 bSkillAtk,sk,n;
3408 if(sd->state.lr_flag == 2)
3409 break;
3410 ARR_FIND(0, ARRAYLENGTH(sd->skillatk), i, sd->skillatk[i].id == 0 || sd->skillatk[i].id == type2);
3411 if (i == ARRAYLENGTH(sd->skillatk))
3412 { //Better mention this so the array length can be updated. [Skotlex]
3413 ShowError("pc_bonus2: SP_SKILL_ATK: Reached max (%d) number of skills per character, bonus skill %d (+%d%%) lost.\n", ARRAYLENGTH(sd->skillatk), type2, val);
3414 break;
3415 }
3416 if (sd->skillatk[i].id == type2)
3417 sd->skillatk[i].val += val;
3418 else {
3419 sd->skillatk[i].id = type2;
3420 sd->skillatk[i].val = val;
3421 }
3422 break;
3423 case SP_SKILL_HEAL: // bonus2 bSkillHeal,sk,n;
3424 if(sd->state.lr_flag == 2)
3425 break;
3426 ARR_FIND(0, ARRAYLENGTH(sd->skillheal), i, sd->skillheal[i].id == 0 || sd->skillheal[i].id == type2);
3427 if (i == ARRAYLENGTH(sd->skillheal))
3428 { // Better mention this so the array length can be updated. [Skotlex]
3429 ShowError("pc_bonus2: SP_SKILL_HEAL: Reached max (%d) number of skills per character, bonus skill %d (+%d%%) lost.\n", ARRAYLENGTH(sd->skillheal), type2, val);
3430 break;
3431 }
3432 if (sd->skillheal[i].id == type2)
3433 sd->skillheal[i].val += val;
3434 else {
3435 sd->skillheal[i].id = type2;
3436 sd->skillheal[i].val = val;
3437 }
3438 break;
3439 case SP_SKILL_HEAL2: // bonus2 bSkillHeal2,sk,n;
3440 if(sd->state.lr_flag == 2)
3441 break;
3442 ARR_FIND(0, ARRAYLENGTH(sd->skillheal2), i, sd->skillheal2[i].id == 0 || sd->skillheal2[i].id == type2);
3443 if (i == ARRAYLENGTH(sd->skillheal2))
3444 { // Better mention this so the array length can be updated. [Skotlex]
3445 ShowError("pc_bonus2: SP_SKILL_HEAL2: Reached max (%d) number of skills per character, bonus skill %d (+%d%%) lost.\n", ARRAYLENGTH(sd->skillheal2), type2, val);
3446 break;
3447 }
3448 if (sd->skillheal2[i].id == type2)
3449 sd->skillheal2[i].val += val;
3450 else {
3451 sd->skillheal2[i].id = type2;
3452 sd->skillheal2[i].val = val;
3453 }
3454 break;
3455 case SP_ADD_SKILL_BLOW: // bonus2 bAddSkillBlow,sk,n;
3456 if(sd->state.lr_flag == 2)
3457 break;
3458 ARR_FIND(0, ARRAYLENGTH(sd->skillblown), i, sd->skillblown[i].id == 0 || sd->skillblown[i].id == type2);
3459 if (i == ARRAYLENGTH(sd->skillblown))
3460 { //Better mention this so the array length can be updated. [Skotlex]
3461 ShowError("pc_bonus2: SP_ADD_SKILL_BLOW: Reached max (%d) number of skills per character, bonus skill %d (%d) lost.\n", ARRAYLENGTH(sd->skillblown), type2, val);
3462 break;
3463 }
3464 if(sd->skillblown[i].id == type2)
3465 sd->skillblown[i].val += val;
3466 else {
3467 sd->skillblown[i].id = type2;
3468 sd->skillblown[i].val = val;
3469 }
3470 break;
3471 case SP_HP_LOSS_RATE: // bonus2 bHPLossRate,n,t;
3472 if(sd->state.lr_flag != 2) {
3473 sd->hp_loss.value = type2;
3474 sd->hp_loss.rate = val;
3475 }
3476 break;
3477 case SP_HP_REGEN_RATE: // bonus2 bHPRegenRate,n,t;
3478 if(sd->state.lr_flag != 2) {
3479 sd->hp_regen.value = type2;
3480 sd->hp_regen.rate = val;
3481 }
3482 break;
3483 case SP_REGEN_PERCENT_HP: // bonus2 bRegenPercentHP,n,t;
3484 if (sd->state.lr_flag != 2) {
3485 sd->percent_hp_regen.value = type2;
3486 sd->percent_hp_regen.rate = val;
3487 }
3488 break;
3489 case SP_REGEN_PERCENT_SP: // bonus2 bRegenPercentSP,n,t;
3490 if (sd->state.lr_flag != 2) {
3491 sd->percent_sp_regen.value = type2;
3492 sd->percent_sp_regen.rate = val;
3493 }
3494 break;
3495 case SP_ADDRACE2: // bonus2 bAddRace2,mr,x;
3496 PC_BONUS_CHK_RACE2(type2,SP_ADDRACE2);
3497 if(sd->state.lr_flag != 2)
3498 sd->right_weapon.addrace2[type2] += val;
3499 else
3500 sd->left_weapon.addrace2[type2] += val;
3501 break;
3502 case SP_SUBSIZE: // bonus2 bSubSize,s,x;
3503 PC_BONUS_CHK_SIZE(type2,SP_SUBSIZE);
3504 if(sd->state.lr_flag != 2)
3505 sd->subsize[type2]+=val;
3506 break;
3507 case SP_SUBRACE2: // bonus2 bSubRace2,mr,x;
3508 PC_BONUS_CHK_RACE2(type2,SP_SUBRACE2);
3509 if(sd->state.lr_flag != 2)
3510 sd->subrace2[type2]+=val;
3511 break;
3512 case SP_ADD_ITEM_HEAL_RATE: // bonus2 bAddItemHealRate,iid,n;
3513 if(sd->state.lr_flag == 2)
3514 break;
3515 if (!itemdb_exists(type2)) {
3516 ShowError("pc_bonus2: SP_ADD_ITEM_HEAL_RATE Invalid item with id %d\n", type2);
3517 break;
3518 }
3519 for(i=0; i < ARRAYLENGTH(sd->itemhealrate) && sd->itemhealrate[i].nameid && sd->itemhealrate[i].nameid != type2; i++);
3520 if(i == ARRAYLENGTH(sd->itemhealrate)) {
3521 ShowError("pc_bonus2: SP_ADD_ITEM_HEAL_RATE: Reached max (%d) number of item heal bonuses per character!\n", ARRAYLENGTH(sd->itemhealrate));
3522 break;
3523 }
3524 sd->itemhealrate[i].nameid = type2;
3525 sd->itemhealrate[i].rate += val;
3526 break;
3527 case SP_ADD_ITEMGROUP_HEAL_RATE: // bonus2 bAddItemGroupHealRate,ig,n;
3528 if (sd->state.lr_flag == 2)
3529 break;
3530 if (!type2 || !itemdb_group_exists(type2)) {
3531 ShowError("pc_bonus2: SP_ADD_ITEMGROUP_HEAL_RATE: Invalid item group with id %d\n", type2);
3532 break;
3533 }
3534 pc_itemgrouphealrate(sd, type2, val);
3535 break;
3536 case SP_EXP_ADDRACE: // bonus2 bExpAddRace,r,x;
3537 PC_BONUS_CHK_RACE(type2,SP_EXP_ADDRACE);
3538 if(sd->state.lr_flag != 2)
3539 sd->expaddrace[type2]+=val;
3540 break;
3541 case SP_EXP_ADDCLASS: // bonus2 bExpAddClass,c,x;
3542 PC_BONUS_CHK_CLASS(type2,SP_EXP_ADDCLASS);
3543 if(sd->state.lr_flag != 2)
3544 sd->expaddclass[type2]+=val;
3545 break;
3546 case SP_SP_GAIN_RACE: // bonus2 bSPGainRace,r,n;
3547 PC_BONUS_CHK_RACE(type2,SP_SP_GAIN_RACE);
3548 if(sd->state.lr_flag != 2)
3549 sd->sp_gain_race[type2]+=val;
3550 break;
3551 case SP_ADD_MONSTER_DROP_ITEM: // bonus2 bAddMonsterDropItem,iid,n;
3552 if (sd->state.lr_flag != 2)
3553 pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), type2, 0, CLASS_ALL, RC_NONE_, val);
3554 break;
3555 case SP_ADD_MONSTER_DROP_ITEMGROUP: // bonus2 bAddMonsterDropItemGroup,ig,n;
3556 if (sd->state.lr_flag != 2)
3557 pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), 0, type2, CLASS_ALL, RC_NONE_, val);
3558 break;
3559 case SP_SP_LOSS_RATE: // bonus2 bSPLossRate,n,t;
3560 if(sd->state.lr_flag != 2) {
3561 sd->sp_loss.value = type2;
3562 sd->sp_loss.rate = val;
3563 }
3564 break;
3565 case SP_SP_REGEN_RATE: // bonus2 bSPRegenRate,n,t;
3566 if(sd->state.lr_flag != 2) {
3567 sd->sp_regen.value = type2;
3568 sd->sp_regen.rate = val;
3569 }
3570 break;
3571 case SP_HP_DRAIN_VALUE_RACE: // bonus2 bHPDrainValueRace,r,n;
3572 PC_BONUS_CHK_RACE(type2,SP_HP_DRAIN_VALUE_RACE);
3573 if(!sd->state.lr_flag) {
3574 sd->right_weapon.hp_drain_race[type2] += val;
3575 }
3576 else if(sd->state.lr_flag == 1) {
3577 sd->left_weapon.hp_drain_race[type2] += val;
3578 }
3579 break;
3580 case SP_SP_DRAIN_VALUE_RACE: // bonus2 bSPDrainValueRace,r,n;
3581 PC_BONUS_CHK_RACE(type2,SP_SP_DRAIN_VALUE_RACE);
3582 if(!sd->state.lr_flag) {
3583 sd->right_weapon.sp_drain_race[type2] += val;
3584 }
3585 else if(sd->state.lr_flag == 1) {
3586 sd->left_weapon.sp_drain_race[type2] += val;
3587 }
3588 break;
3589 case SP_HP_DRAIN_VALUE_CLASS: // bonus2 bHPDrainValueClass,c,n;
3590 PC_BONUS_CHK_CLASS(type2,SP_HP_DRAIN_VALUE_CLASS);
3591 if(!sd->state.lr_flag) {
3592 sd->right_weapon.hp_drain_class[type2] += val;
3593 }
3594 else if(sd->state.lr_flag == 1) {
3595 sd->left_weapon.hp_drain_class[type2] += val;
3596 }
3597 break;
3598 case SP_SP_DRAIN_VALUE_CLASS: // bonus2 bSPDrainValueClass,c,n;
3599 PC_BONUS_CHK_CLASS(type2,SP_SP_DRAIN_VALUE_CLASS);
3600 if(!sd->state.lr_flag) {
3601 sd->right_weapon.sp_drain_class[type2] += val;
3602 }
3603 else if(sd->state.lr_flag == 1) {
3604 sd->left_weapon.sp_drain_class[type2] += val;
3605 }
3606 break;
3607 case SP_IGNORE_MDEF_RACE_RATE: // bonus2 bIgnoreMdefRaceRate,r,n;
3608 PC_BONUS_CHK_RACE(type2,SP_IGNORE_MDEF_RACE_RATE);
3609 if(sd->state.lr_flag != 2)
3610 sd->ignore_mdef_by_race[type2] += val;
3611 break;
3612 case SP_IGNORE_MDEF_CLASS_RATE: // bonus2 bIgnoreMdefClassRate,c,n;
3613 PC_BONUS_CHK_CLASS(type2,SP_IGNORE_MDEF_CLASS_RATE);
3614 if(sd->state.lr_flag != 2)
3615 sd->ignore_mdef_by_class[type2] += val;
3616 break;
3617 case SP_IGNORE_DEF_RACE_RATE: // bonus2 bIgnoreDefRaceRate,r,n;
3618 PC_BONUS_CHK_RACE(type2,SP_IGNORE_DEF_RACE_RATE);
3619 if(sd->state.lr_flag != 2)
3620 sd->ignore_def_by_race[type2] += val;
3621 break;
3622 case SP_IGNORE_DEF_CLASS_RATE: // bonus2 bIgnoreDefClassRate,r,n;
3623 PC_BONUS_CHK_CLASS(type2, SP_IGNORE_DEF_CLASS_RATE);
3624 if (sd->state.lr_flag != 2)
3625 sd->ignore_def_by_class[type2] += val;
3626 break;
3627 case SP_SKILL_USE_SP_RATE: // bonus2 bSkillUseSPrate,sk,n;
3628 if(sd->state.lr_flag == 2)
3629 break;
3630 ARR_FIND(0, ARRAYLENGTH(sd->skillusesprate), i, sd->skillusesprate[i].id == 0 || sd->skillusesprate[i].id == type2);
3631 if (i == ARRAYLENGTH(sd->skillusesprate)) {
3632 ShowError("pc_bonus2: SP_SKILL_USE_SP_RATE: Reached max (%d) number of skills per character, bonus skill %d (+%d%%) lost.\n", ARRAYLENGTH(sd->skillusesprate), type2, val);
3633 break;
3634 }
3635 if (sd->skillusesprate[i].id == type2)
3636 sd->skillusesprate[i].val += val;
3637 else {
3638 sd->skillusesprate[i].id = type2;
3639 sd->skillusesprate[i].val = val;
3640 }
3641 break;
3642 case SP_SKILL_DELAY:
3643 if(sd->state.lr_flag == 2)
3644 break;
3645 ARR_FIND(0, ARRAYLENGTH(sd->skilldelay), i, sd->skilldelay[i].id == 0 || sd->skilldelay[i].id == type2);
3646 if (i == ARRAYLENGTH(sd->skilldelay)) {
3647 ShowError("pc_bonus2: SP_SKILL_DELAY: Reached max (%d) number of skills per character, bonus skill %d (%d) lost.\n", ARRAYLENGTH(sd->skilldelay), type2, val);
3648 break;
3649 }
3650 if (sd->skilldelay[i].id == type2)
3651 sd->skilldelay[i].val += val;
3652 else {
3653 sd->skilldelay[i].id = type2;
3654 sd->skilldelay[i].val = val;
3655 }
3656 break;
3657 case SP_SKILL_COOLDOWN: // bonus2 bSkillCooldown,sk,t;
3658 if(sd->state.lr_flag == 2)
3659 break;
3660 ARR_FIND(0, ARRAYLENGTH(sd->skillcooldown), i, sd->skillcooldown[i].id == 0 || sd->skillcooldown[i].id == type2);
3661 if (i == ARRAYLENGTH(sd->skillcooldown))
3662 {
3663 ShowError("pc_bonus2: SP_SKILL_COOLDOWN: Reached max (%d) number of skills per character, bonus skill %d (%d) lost.\n", ARRAYLENGTH(sd->skillcooldown), type2, val);
3664 break;
3665 }
3666 if (sd->skillcooldown[i].id == type2)
3667 sd->skillcooldown[i].val += val;
3668 else {
3669 sd->skillcooldown[i].id = type2;
3670 sd->skillcooldown[i].val = val;
3671 }
3672 break;
3673#ifdef RENEWAL_CAST
3674 case SP_SKILL_FIXEDCAST: // bonus2 bSkillFixedCast,sk,t;
3675 if(sd->state.lr_flag == 2)
3676 break;
3677 ARR_FIND(0, ARRAYLENGTH(sd->skillfixcast), i, sd->skillfixcast[i].id == 0 || sd->skillfixcast[i].id == type2);
3678 if (i == ARRAYLENGTH(sd->skillfixcast))
3679 {
3680 ShowError("pc_bonus2: SP_SKILL_FIXEDCAST: Reached max (%d) number of skills per character, bonus skill %d (%d) lost.\n", ARRAYLENGTH(sd->skillfixcast), type2, val);
3681 break;
3682 }
3683 if (sd->skillfixcast[i].id == type2)
3684 sd->skillfixcast[i].val += val;
3685 else {
3686 sd->skillfixcast[i].id = type2;
3687 sd->skillfixcast[i].val = val;
3688 }
3689 break;
3690 case SP_SKILL_VARIABLECAST: // bonus2 bSkillVariableCast,sk,t;
3691 if(sd->state.lr_flag == 2)
3692 break;
3693 ARR_FIND(0, ARRAYLENGTH(sd->skillvarcast), i, sd->skillvarcast[i].id == 0 || sd->skillvarcast[i].id == type2);
3694 if (i == ARRAYLENGTH(sd->skillvarcast))
3695 {
3696 ShowError("pc_bonus2: SP_SKILL_VARIABLECAST: Reached max (%d) number of skills per character, bonus skill %d (%d) lost.\n", ARRAYLENGTH(sd->skillvarcast), type2, val);
3697 break;
3698 }
3699 if (sd->skillvarcast[i].id == type2)
3700 sd->skillvarcast[i].val += val;
3701 else {
3702 sd->skillvarcast[i].id = type2;
3703 sd->skillvarcast[i].val = val;
3704 }
3705 break;
3706 case SP_CASTRATE: // bonus2 bCastrate,sk,n;
3707 case SP_VARCASTRATE: // bonus2 bVariableCastrate,sk,n;
3708 if(sd->state.lr_flag == 2)
3709 break;
3710 ARR_FIND(0, ARRAYLENGTH(sd->skillcastrate), i, sd->skillcastrate[i].id == 0 || sd->skillcastrate[i].id == type2);
3711 if (i == ARRAYLENGTH(sd->skillcastrate))
3712 {
3713 ShowError("pc_bonus2: SP_VARCASTRATE: Reached max (%d) number of skills per character, bonus skill %d (%d%%) lost.\n",ARRAYLENGTH(sd->skillcastrate), type2, val);
3714 break;
3715 }
3716 if(sd->skillcastrate[i].id == type2)
3717 sd->skillcastrate[i].val -= val;
3718 else {
3719 sd->skillcastrate[i].id = type2;
3720 sd->skillcastrate[i].val -= val;
3721 }
3722 break;
3723 case SP_FIXCASTRATE: // bonus2 bFixedCastrate,sk,n;
3724 if(sd->state.lr_flag == 2)
3725 break;
3726 ARR_FIND(0, ARRAYLENGTH(sd->skillfixcastrate), i, sd->skillfixcastrate[i].id == 0 || sd->skillfixcastrate[i].id == type2);
3727 if (i == ARRAYLENGTH(sd->skillfixcastrate))
3728 {
3729 ShowError("pc_bonus2: SP_FIXCASTRATE: Reached max (%d) number of skills per character, bonus skill %d (%d%%) lost.\n", ARRAYLENGTH(sd->skillfixcastrate), type2, val);
3730 break;
3731 }
3732 if(sd->skillfixcastrate[i].id == type2)
3733 sd->skillfixcastrate[i].val -= val;
3734 else {
3735 sd->skillfixcastrate[i].id = type2;
3736 sd->skillfixcastrate[i].val -= val;
3737 }
3738 break;
3739#else
3740 case SP_SKILL_FIXEDCAST: // bonus2 bSkillFixedCast,sk,t;
3741 case SP_SKILL_VARIABLECAST: // bonus2 bSkillVariableCast,sk,t;
3742 case SP_FIXCASTRATE: // bonus2 bFixedCastrate,sk,n;
3743 //ShowWarning("pc_bonus2: Non-RENEWAL_CAST doesn't support this bonus %d.\n", type);
3744 break;
3745 case SP_VARCASTRATE: // bonus2 bVariableCastrate,sk,n;
3746 case SP_CASTRATE: // bonus2 bCastrate,sk,n;
3747 if(sd->state.lr_flag == 2)
3748 break;
3749 ARR_FIND(0, ARRAYLENGTH(sd->skillcastrate), i, sd->skillcastrate[i].id == 0 || sd->skillcastrate[i].id == type2);
3750 if (i == ARRAYLENGTH(sd->skillcastrate))
3751 { //Better mention this so the array length can be updated. [Skotlex]
3752 ShowError("pc_bonus2: %s: Reached max (%d) number of skills per character, bonus skill %d (%d%%) lost.\n",
3753 (type == SP_CASTRATE) ? "SP_CASTRATE" : "SP_VARCASTRATE", ARRAYLENGTH(sd->skillcastrate), type2, val);
3754 break;
3755 }
3756 if(sd->skillcastrate[i].id == type2)
3757 sd->skillcastrate[i].val += val;
3758 else {
3759 sd->skillcastrate[i].id = type2;
3760 sd->skillcastrate[i].val = val;
3761 }
3762 break;
3763#endif
3764 case SP_SKILL_USE_SP: // bonus2 bSkillUseSP,sk,n;
3765 if(sd->state.lr_flag == 2)
3766 break;
3767 ARR_FIND(0, ARRAYLENGTH(sd->skillusesp), i, sd->skillusesp[i].id == 0 || sd->skillusesp[i].id == type2);
3768 if (i == ARRAYLENGTH(sd->skillusesp)) {
3769 ShowError("pc_bonus2: SP_SKILL_USE_SP: Reached max (%d) number of skills per character, bonus skill %d (%d) lost.\n", ARRAYLENGTH(sd->skillusesp), type2, val);
3770 break;
3771 }
3772 if (sd->skillusesp[i].id == type2)
3773 sd->skillusesp[i].val += val;
3774 else {
3775 sd->skillusesp[i].id = type2;
3776 sd->skillusesp[i].val = val;
3777 }
3778 break;
3779 case SP_SUB_SKILL: // bonus2 bSubSkill,sk,n;
3780 ARR_FIND(0, ARRAYLENGTH(sd->subskill), i, sd->subskill[i].id == type2 || sd->subskill[i].id == 0);
3781 if (i == ARRAYLENGTH(sd->subskill)) {
3782 ShowError("pc_bonus2: SP_SUB_SKILL: Reached max (%d) number of skills per character, bonus skill %d (%d) lost.\n", ARRAYLENGTH(sd->subskill), type2, val);
3783 break;
3784 }
3785 if (sd->subskill[i].id == type2)
3786 sd->subskill[i].val += val;
3787 else {
3788 sd->subskill[i].id = type2;
3789 sd->subskill[i].val = val;
3790 }
3791 break;
3792 case SP_SUBDEF_ELE: // bonus2 bSubDefEle,e,x;
3793 PC_BONUS_CHK_ELEMENT(type2,SP_SUBDEF_ELE);
3794 sd->subdefele[type2] += val;
3795 break;
3796 case SP_COMA_CLASS: // bonus2 bComaClass,c,n;
3797 PC_BONUS_CHK_CLASS(type2,SP_COMA_CLASS);
3798 sd->coma_class[type2] += val;
3799 sd->special_state.bonus_coma = 1;
3800 break;
3801 case SP_COMA_RACE: // bonus2 bComaRace,r,n;
3802 PC_BONUS_CHK_RACE(type2,SP_COMA_RACE);
3803 sd->coma_race[type2] += val;
3804 sd->special_state.bonus_coma = 1;
3805 break;
3806 case SP_MAGIC_ADDRACE2: // bonus2 bMagicAddRace2,mr,n;
3807 PC_BONUS_CHK_RACE2(type2, SP_MAGIC_ADDRACE2);
3808 if(sd->state.lr_flag != 2)
3809 sd->magic_addrace2[type2] += val;
3810 break;
3811 case SP_IGNORE_MDEF_RACE2_RATE: //bonus2 bIgnoreMdefRace2Rate,mr,n;
3812 PC_BONUS_CHK_RACE2(type2, SP_IGNORE_MDEF_RACE2);
3813 if (sd->state.lr_flag != 2)
3814 sd->ignore_mdef_by_race2[type2] += val;
3815 break;
3816 case SP_DROP_ADDRACE: // bonus2 bDropAddRace,r,x;
3817 PC_BONUS_CHK_RACE(type2, SP_DROP_ADDRACE);
3818 if (sd->state.lr_flag != 2)
3819 sd->dropaddrace[type2] += val;
3820 break;
3821 case SP_DROP_ADDCLASS: // bonus2 bDropAddClass,c,x;
3822 PC_BONUS_CHK_CLASS(type2, SP_DROP_ADDCLASS);
3823 if (sd->state.lr_flag != 2)
3824 sd->dropaddclass[type2] += val;
3825 break;
3826 default:
3827 if (running_npc_stat_calc_event) {
3828 ShowWarning("pc_bonus2: unknown bonus type %d %d %d in OnPCStatCalcEvent!\n", type, type2, val);
3829 }
3830 else if (current_equip_combo_pos > 0) {
3831 ShowWarning("pc_bonus2: unknown bonus type %d %d %d in a combo with item #%d\n", type, type2, val, sd->inventory_data[pc_checkequip( sd, current_equip_combo_pos )]->nameid);
3832 }
3833 else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
3834 ShowWarning("pc_bonus2: unknown bonus type %d %d %d in item #%d\n", type, type2, val, current_equip_card_id ? current_equip_card_id : sd->inventory_data[current_equip_item_index]->nameid);
3835 }
3836 else {
3837 ShowWarning("pc_bonus2: unknown bonus type %d %d %d in unknown usage. Report this!\n", type, type2, val);
3838 }
3839 break;
3840 }
3841}
3842
3843/**
3844* Gives item bonus to player for format: bonus3 bBonusName,type2,val;
3845* @param sd
3846* @param type Bonus type used by bBonusName
3847* @param type2
3848* @param val Value that usually for rate or fixed value
3849*/
3850void pc_bonus3(struct map_session_data *sd,int type,int type2,int type3,int val)
3851{
3852 nullpo_retv(sd);
3853
3854 switch(type){
3855 case SP_ADD_MONSTER_DROP_ITEM: // bonus3 bAddMonsterDropItem,iid,r,n;
3856 if(sd->state.lr_flag != 2)
3857 pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), type2, 0, CLASS_NONE, type3, val);
3858 break;
3859 case SP_ADD_MONSTER_ID_DROP_ITEM: // bonus3 bAddMonsterIdDropItem,iid,mid,n;
3860 if(sd->state.lr_flag != 2)
3861 pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), type2, 0, CLASS_NONE, -type3, val);
3862 break;
3863 case SP_ADD_CLASS_DROP_ITEM: // bonus3 bAddClassDropItem,iid,c,n;
3864 if(sd->state.lr_flag != 2)
3865 pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), type2, 0, type3, RC_NONE_, val);
3866 break;
3867 case SP_AUTOSPELL: // bonus3 bAutoSpell,sk,y,n;
3868 if(sd->state.lr_flag != 2)
3869 {
3870 int target = skill_get_inf(type2); //Support or Self (non-auto-target) skills should pick self.
3871 target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !(skill_get_inf2(type2)&INF2_NO_TARGET_SELF));
3872 pc_bonus_autospell(sd->autospell, ARRAYLENGTH(sd->autospell),
3873 target?-type2:type2, type3, val, 0, current_equip_card_id);
3874 }
3875 break;
3876 case SP_AUTOSPELL_WHENHIT: // bonus3 bAutoSpellWhenHit,sk,y,n;
3877 if(sd->state.lr_flag != 2)
3878 {
3879 int target = skill_get_inf(type2); //Support or Self (non-auto-target) skills should pick self.
3880 target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !(skill_get_inf2(type2)&INF2_NO_TARGET_SELF));
3881 pc_bonus_autospell(sd->autospell2, ARRAYLENGTH(sd->autospell2),
3882 target?-type2:type2, type3, val, BF_NORMAL|BF_SKILL, current_equip_card_id);
3883 }
3884 break;
3885 case SP_ADD_MONSTER_DROP_ITEMGROUP: // bonus3 bAddMonsterDropItemGroup,ig,r,n;
3886 if (sd->state.lr_flag != 2)
3887 pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), 0, type2, CLASS_NONE, type3, val);
3888 break;
3889 case SP_ADD_CLASS_DROP_ITEMGROUP: // bonus3 bAddClassDropItemGroup,ig,c,n;
3890 if (sd->state.lr_flag != 2)
3891 pc_bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), 0, type2, type3, RC_NONE_, val);
3892 break;
3893
3894 case SP_ADDEFF: // bonus3 bAddEff,eff,n,y;
3895 PC_BONUS_CHK_SC(type2,SP_ADDEFF);
3896 pc_bonus_addeff(sd->addeff, ARRAYLENGTH(sd->addeff), (sc_type)type2,
3897 sd->state.lr_flag != 2 ? type3 : 0, sd->state.lr_flag == 2 ? type3 : 0, val, 0);
3898 break;
3899
3900 case SP_ADDEFF_WHENHIT: // bonus3 bAddEffWhenHit,eff,n,y;
3901 PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
3902 if(sd->state.lr_flag != 2)
3903 pc_bonus_addeff(sd->addeff_atked, ARRAYLENGTH(sd->addeff_atked), (sc_type)type2, type3, 0, val, 0);
3904 break;
3905
3906 case SP_ADDEFF_ONSKILL: // bonus3 bAddEffOnSkill,sk,eff,n;
3907 PC_BONUS_CHK_SC(type2,SP_ADDEFF_ONSKILL);
3908 if( sd->state.lr_flag != 2 )
3909 pc_bonus_addeff_onskill(sd->addeff_onskill, ARRAYLENGTH(sd->addeff_onskill), (sc_type)type3, val, type2, ATF_TARGET, 0);
3910 break;
3911
3912 case SP_ADDELE: // bonus3 bAddEle,e,x,bf;
3913 PC_BONUS_CHK_ELEMENT(type2,SP_ADDELE);
3914 if (sd->state.lr_flag != 2)
3915 pc_bonus_addele(sd, (unsigned char)type2, type3, val);
3916 break;
3917
3918 case SP_SUBELE: // bonus3 bSubEle,e,x,bf;
3919 PC_BONUS_CHK_ELEMENT(type2,SP_SUBELE);
3920 if (sd->state.lr_flag != 2)
3921 pc_bonus_subele(sd, (unsigned char)type2, type3, val);
3922 break;
3923
3924 case SP_SP_VANISH_RACE_RATE: // bonus3 bSPVanishRaceRate,r,x,n;
3925 PC_BONUS_CHK_RACE(type2,SP_SP_VANISH_RACE_RATE);
3926 if(sd->state.lr_flag != 2) {
3927 sd->sp_vanish_race[type2].rate += type3;
3928 sd->sp_vanish_race[type2].per += val;
3929 }
3930 break;
3931
3932 case SP_HP_VANISH_RACE_RATE: // bonus3 bHPVanishRaceRate,r,x,n;
3933 PC_BONUS_CHK_RACE(type2,SP_HP_VANISH_RACE_RATE);
3934 if(sd->state.lr_flag != 2) {
3935 sd->hp_vanish_race[type2].rate += type3;
3936 sd->hp_vanish_race[type2].per += val;
3937 }
3938 break;
3939 case SP_STATE_NORECOVER_RACE: // bonus3 bStateNoRecoverRace,r,x,t;
3940 PC_BONUS_CHK_RACE(type2, SP_STATE_NORECOVER_RACE);
3941 if (sd->state.lr_flag == 2)
3942 break;
3943 //! CONFIRM: Is it not stackable? Does not check max or min value?
3944 //if (type3 > sd->norecover_state_race[type2].rate) {
3945 // sd->norecover_state_race[type2].rate = type3;
3946 // sd->norecover_state_race[type2].tick = val;
3947 // break;
3948 //}
3949 sd->norecover_state_race[type2].rate = type3;
3950 sd->norecover_state_race[type2].tick = val;
3951 break;
3952 default:
3953 if (running_npc_stat_calc_event) {
3954 ShowWarning("pc_bonus3: unknown bonus type %d %d %d %d in OnPCStatCalcEvent!\n", type, type2, type3, val);
3955 }
3956 else if (current_equip_combo_pos > 0) {
3957 ShowWarning("pc_bonus3: unknown bonus type %d %d %d %d in a combo with item #%d\n", type, type2, type3, val, sd->inventory_data[pc_checkequip( sd, current_equip_combo_pos )]->nameid);
3958 }
3959 else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
3960 ShowWarning("pc_bonus3: unknown bonus type %d %d %d %d in item #%d\n", type, type2, type3, val, current_equip_card_id ? current_equip_card_id : sd->inventory_data[current_equip_item_index]->nameid);
3961 }
3962 else {
3963 ShowWarning("pc_bonus3: unknown bonus type %d %d %d %d in unknown usage. Report this!\n", type, type2, type3, val);
3964 }
3965 break;
3966 }
3967}
3968
3969/**
3970* Gives item bonus to player for format: bonus4 bBonusName,type2,type3,val;
3971* @param sd
3972* @param type Bonus type used by bBonusName
3973* @param type2
3974* @param type3
3975* @param val Value that usually for rate or fixed value
3976*/
3977void pc_bonus4(struct map_session_data *sd,int type,int type2,int type3,int type4,int val)
3978{
3979 nullpo_retv(sd);
3980
3981 switch(type){
3982 case SP_AUTOSPELL: // bonus4 bAutoSpell,sk,y,n,i;
3983 if(sd->state.lr_flag != 2)
3984 pc_bonus_autospell(sd->autospell, ARRAYLENGTH(sd->autospell), (val&1?type2:-type2), (val&2?-type3:type3), type4, 0, current_equip_card_id);
3985 break;
3986
3987 case SP_AUTOSPELL_WHENHIT: // bonus4 bAutoSpellWhenHit,sk,y,n,i;
3988 if(sd->state.lr_flag != 2)
3989 pc_bonus_autospell(sd->autospell2, ARRAYLENGTH(sd->autospell2), (val&1?type2:-type2), (val&2?-type3:type3), type4, BF_NORMAL|BF_SKILL, current_equip_card_id);
3990 break;
3991
3992 case SP_AUTOSPELL_ONSKILL: // bonus4 bAutoSpellOnSkill,sk,x,y,n;
3993 if(sd->state.lr_flag != 2)
3994 {
3995 int target = skill_get_inf(type3); //Support or Self (non-auto-target) skills should pick self.
3996 target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !(skill_get_inf2(type3)&INF2_NO_TARGET_SELF));
3997
3998 pc_bonus_autospell_onskill(sd->autospell3, ARRAYLENGTH(sd->autospell3), type2, target?-type3:type3, type4, val, current_equip_card_id);
3999 }
4000 break;
4001
4002 case SP_ADDEFF: // bonus4 bAddEff,eff,n,y,t;
4003 PC_BONUS_CHK_SC(type2,SP_ADDEFF);
4004 pc_bonus_addeff(sd->addeff, ARRAYLENGTH(sd->addeff), (sc_type)type2,
4005 sd->state.lr_flag != 2 ? type3 : 0, sd->state.lr_flag == 2 ? type3 : 0, type4, val);
4006 break;
4007
4008 case SP_ADDEFF_WHENHIT: // bonus4 bAddEffWhenHit,eff,n,y,t;
4009 PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
4010 if (sd->state.lr_flag != 2)
4011 pc_bonus_addeff(sd->addeff_atked, ARRAYLENGTH(sd->addeff_atked), (sc_type)type2, type3, 0, type4, val);
4012 break;
4013
4014 case SP_ADDEFF_ONSKILL: // bonus4 bAddEffOnSkill,sk,eff,n,y;
4015 PC_BONUS_CHK_SC(type3,SP_ADDEFF_ONSKILL);
4016 if( sd->state.lr_flag != 2 )
4017 pc_bonus_addeff_onskill(sd->addeff_onskill, ARRAYLENGTH(sd->addeff_onskill), (sc_type)type3, type4, type2, val, 0);
4018 break;
4019
4020 case SP_SET_DEF_RACE: // bonus4 bSetDefRace,r,n,t,y;
4021 PC_BONUS_CHK_RACE(type2,SP_SET_DEF_RACE);
4022 if(sd->state.lr_flag == 2)
4023 break;
4024 sd->def_set_race[type2].rate = type3;
4025 sd->def_set_race[type2].tick = type4;
4026 sd->def_set_race[type2].value = val;
4027 break;
4028
4029 case SP_SET_MDEF_RACE: // bonus4 bSetMDefRace,r,n,t,y;
4030 PC_BONUS_CHK_RACE(type2,SP_SET_MDEF_RACE);
4031 if(sd->state.lr_flag == 2)
4032 break;
4033 sd->mdef_set_race[type2].rate = type3;
4034 sd->mdef_set_race[type2].tick = type4;
4035 sd->mdef_set_race[type2].value = val;
4036 break;
4037
4038 default:
4039 if (running_npc_stat_calc_event) {
4040 ShowWarning("pc_bonus4: unknown bonus type %d %d %d %d %d in OnPCStatCalcEvent!\n", type, type2, type3, type4, val);
4041 }
4042 else if (current_equip_combo_pos > 0) {
4043 ShowWarning("pc_bonus4: unknown bonus type %d %d %d %d %d in a combo with item #%d\n", type, type2, type3, type4, val, sd->inventory_data[pc_checkequip( sd, current_equip_combo_pos )]->nameid);
4044 }
4045 else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
4046 ShowWarning("pc_bonus4: unknown bonus type %d %d %d %d %d in item #%d\n", type, type2, type3, type4, val, current_equip_card_id ? current_equip_card_id : sd->inventory_data[current_equip_item_index]->nameid);
4047 }
4048 else {
4049 ShowWarning("pc_bonus4: unknown bonus type %d %d %d %d %d in unknown usage. Report this!\n", type, type2, type3, type4, val);
4050 }
4051 break;
4052 }
4053}
4054
4055/**
4056* Gives item bonus to player for format: bonus5 bBonusName,type2,type3,type4,val;
4057* @param sd
4058* @param type Bonus type used by bBonusName
4059* @param type2
4060* @param type3
4061* @param type4
4062* @param val Value that usually for rate or fixed value
4063*/
4064void pc_bonus5(struct map_session_data *sd,int type,int type2,int type3,int type4,int type5,int val)
4065{
4066 nullpo_retv(sd);
4067
4068 switch(type){
4069 case SP_AUTOSPELL: // bonus5 bAutoSpell,sk,y,n,bf,i;
4070 if(sd->state.lr_flag != 2)
4071 pc_bonus_autospell(sd->autospell, ARRAYLENGTH(sd->autospell), (val&1?type2:-type2), (val&2?-type3:type3), type4, type5, current_equip_card_id);
4072 break;
4073
4074 case SP_AUTOSPELL_WHENHIT: // bonus5 bAutoSpellWhenHit,sk,y,n,bf,i;
4075 if(sd->state.lr_flag != 2)
4076 pc_bonus_autospell(sd->autospell2, ARRAYLENGTH(sd->autospell2), (val&1?type2:-type2), (val&2?-type3:type3), type4, type5, current_equip_card_id);
4077 break;
4078
4079 case SP_AUTOSPELL_ONSKILL: // bonus5 bAutoSpellOnSkill,sk,x,y,n,i;
4080 if(sd->state.lr_flag != 2)
4081 pc_bonus_autospell_onskill(sd->autospell3, ARRAYLENGTH(sd->autospell3), type2, (val&1?-type3:type3), (val&2?-type4:type4), type5, current_equip_card_id);
4082 break;
4083
4084 case SP_ADDEFF_ONSKILL: // bonus5 bAddEffOnSkill,sk,eff,n,y,t;
4085 PC_BONUS_CHK_SC(type3,SP_ADDEFF_ONSKILL);
4086 if( sd->state.lr_flag != 2 )
4087 pc_bonus_addeff_onskill(sd->addeff_onskill, ARRAYLENGTH(sd->addeff_onskill), (sc_type)type3, type4, type2, type5, val);
4088 break;
4089
4090 default:
4091 if (running_npc_stat_calc_event) {
4092 ShowWarning("pc_bonus5: unknown bonus type %d %d %d %d %d %d in OnPCStatCalcEvent!\n", type, type2, type3, type4, type5, val);
4093 }
4094 else if (current_equip_combo_pos > 0) {
4095 ShowWarning("pc_bonus5: unknown bonus type %d %d %d %d %d %d in a combo with item #%d\n", type, type2, type3, type4, type5, val, sd->inventory_data[pc_checkequip( sd, current_equip_combo_pos )]->nameid);
4096 }
4097 else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
4098 ShowWarning("pc_bonus5: unknown bonus type %d %d %d %d %d %d in item #%d\n", type, type2, type3, type4, type5, val, current_equip_card_id ? current_equip_card_id : sd->inventory_data[current_equip_item_index]->nameid);
4099 }
4100 else {
4101 ShowWarning("pc_bonus5: unknown bonus type %d %d %d %d %d %d in unknown usage. Report this!\n", type, type2, type3, type4, type5, val);
4102 }
4103 break;
4104 }
4105}
4106
4107/*==========================================
4108 * Grants a player a given skill. Flag values are:
4109 * 0 - Grant permanent skill to be bound to skill tree
4110 * 1 - Grant an item skill (temporary)
4111 * 2 - Like 1, except the level granted can stack with previously learned level.
4112 * 4 - Like 0, except the skill will ignore skill tree (saves through job changes and resets).
4113 *------------------------------------------*/
4114bool pc_skill(struct map_session_data* sd, uint16 skill_id, int level, enum e_addskill_type type) {
4115 uint16 idx = 0;
4116 nullpo_ret(sd);
4117
4118 if (!skill_id || !(idx = skill_get_index(skill_id))) {
4119 ShowError("pc_skill: Skill with id %d does not exist in the skill database\n", skill_id);
4120 return false;
4121 }
4122 if (level > MAX_SKILL_LEVEL) {
4123 ShowError("pc_skill: Skill level %d too high. Max lv supported is %d\n", level, MAX_SKILL_LEVEL);
4124 return false;
4125 }
4126 if (type == ADDSKILL_TEMP_ADDLEVEL && sd->status.skill[idx].lv + level > MAX_SKILL_LEVEL) {
4127 ShowWarning("pc_skill: Skill level bonus %d too high. Max lv supported is %d. Curr lv is %d. Set to max level.\n", level, MAX_SKILL_LEVEL, sd->status.skill[idx].lv);
4128 level = MAX_SKILL_LEVEL - sd->status.skill[idx].lv;
4129 }
4130
4131 switch (type) {
4132 case ADDSKILL_PERMANENT: //Set skill data overwriting whatever was there before.
4133 sd->status.skill[idx].id = skill_id;
4134 sd->status.skill[idx].lv = level;
4135 sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
4136 if (level == 0) { //Remove skill.
4137 sd->status.skill[idx].id = 0;
4138 clif_deleteskill(sd,skill_id);
4139 } else
4140 clif_addskill(sd,skill_id);
4141 if (!skill_get_inf(skill_id)) //Only recalculate for passive skills.
4142 status_calc_pc(sd, SCO_NONE);
4143 break;
4144
4145 case ADDSKILL_TEMP: //Item bonus skill.
4146 if (sd->status.skill[idx].id != 0) {
4147 if (sd->status.skill[idx].lv >= level)
4148 return true;
4149 if (sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT) //Non-granted skill, store it's level.
4150 sd->status.skill[idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[idx].lv;
4151 } else {
4152 sd->status.skill[idx].id = skill_id;
4153 sd->status.skill[idx].flag = SKILL_FLAG_TEMPORARY;
4154 }
4155 sd->status.skill[idx].lv = level;
4156 break;
4157
4158 case ADDSKILL_TEMP_ADDLEVEL: //Add skill bonus on top of what you had.
4159 if (sd->status.skill[idx].id != 0) {
4160 if (sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT)
4161 sd->status.skill[idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[idx].lv; // Store previous level.
4162 } else {
4163 sd->status.skill[idx].id = skill_id;
4164 sd->status.skill[idx].flag = SKILL_FLAG_TEMPORARY; //Set that this is a bonus skill.
4165 }
4166 sd->status.skill[idx].lv += level;
4167 break;
4168
4169 case ADDSKILL_PERMANENT_GRANTED: //Permanent granted skills ignore the skill tree
4170 sd->status.skill[idx].id = skill_id;
4171 sd->status.skill[idx].lv = level;
4172 sd->status.skill[idx].flag = SKILL_FLAG_PERM_GRANTED;
4173 if (level == 0) { //Remove skill.
4174 sd->status.skill[idx].id = 0;
4175 clif_deleteskill(sd,skill_id);
4176 } else
4177 clif_addskill(sd,skill_id);
4178 if (!skill_get_inf(skill_id)) //Only recalculate for passive skills.
4179 status_calc_pc(sd, SCO_NONE);
4180 break;
4181
4182 default:
4183 return false;
4184 }
4185 return true;
4186}
4187/*==========================================
4188 * Append a card to an item ?
4189 *------------------------------------------*/
4190int pc_insert_card(struct map_session_data* sd, int idx_card, int idx_equip)
4191{
4192 int i;
4193 unsigned short nameid;
4194
4195 nullpo_ret(sd);
4196
4197 if( idx_equip < 0 || idx_equip >= MAX_INVENTORY || sd->inventory_data[idx_equip] == NULL )
4198 return 0; //Invalid item index.
4199 if( idx_card < 0 || idx_card >= MAX_INVENTORY || sd->inventory_data[idx_card] == NULL )
4200 return 0; //Invalid card index.
4201 if( sd->inventory.u.items_inventory[idx_equip].nameid <= 0 || sd->inventory.u.items_inventory[idx_equip].amount < 1 )
4202 return 0; // target item missing
4203 if( sd->inventory.u.items_inventory[idx_card].nameid <= 0 || sd->inventory.u.items_inventory[idx_card].amount < 1 )
4204 return 0; // target card missing
4205 if( sd->inventory_data[idx_equip]->type != IT_WEAPON && sd->inventory_data[idx_equip]->type != IT_ARMOR )
4206 return 0; // only weapons and armor are allowed
4207 if( sd->inventory_data[idx_card]->type != IT_CARD )
4208 return 0; // must be a card
4209 if( sd->inventory.u.items_inventory[idx_equip].identify == 0 )
4210 return 0; // target must be identified
4211 if( itemdb_isspecial(sd->inventory.u.items_inventory[idx_equip].card[0]) )
4212 return 0; // card slots reserved for other purposes
4213 if( (sd->inventory_data[idx_equip]->equip & sd->inventory_data[idx_card]->equip) == 0 )
4214 return 0; // card cannot be compounded on this item type
4215 if( sd->inventory_data[idx_equip]->type == IT_WEAPON && sd->inventory_data[idx_card]->equip == EQP_SHIELD )
4216 return 0; // attempted to place shield card on left-hand weapon.
4217 if( sd->inventory.u.items_inventory[idx_equip].equip != 0 )
4218 return 0; // item must be unequipped
4219
4220 ARR_FIND( 0, sd->inventory_data[idx_equip]->slot, i, sd->inventory.u.items_inventory[idx_equip].card[i] == 0 );
4221 if( i == sd->inventory_data[idx_equip]->slot )
4222 return 0; // no free slots
4223
4224 // remember the card id to insert
4225 nameid = sd->inventory.u.items_inventory[idx_card].nameid;
4226
4227 if( pc_delitem(sd,idx_card,1,1,0,LOG_TYPE_OTHER) == 1 )
4228 {// failed
4229 clif_insert_card(sd,idx_equip,idx_card,1);
4230 }
4231 else
4232 {// success
4233 log_pick_pc(sd, LOG_TYPE_OTHER, -1, &sd->inventory.u.items_inventory[idx_equip]);
4234 sd->inventory.u.items_inventory[idx_equip].card[i] = nameid;
4235 log_pick_pc(sd, LOG_TYPE_OTHER, 1, &sd->inventory.u.items_inventory[idx_equip]);
4236 clif_insert_card(sd,idx_equip,idx_card,0);
4237 }
4238
4239 return 0;
4240}
4241
4242/**
4243 * Returns the count of unidentified items with the option to identify too.
4244 * @param sd: Player data
4245 * @param identify_item: Whether or not to identify any unidentified items
4246 * @return Unidentified items count
4247 */
4248int pc_identifyall(struct map_session_data *sd, bool identify_item)
4249{
4250 int unidentified_count = 0;
4251
4252 for (int i = 0; i < MAX_INVENTORY; i++) {
4253 if (sd->inventory.u.items_inventory[i].nameid > 0 && sd->inventory.u.items_inventory[i].identify != 1) {
4254 if (identify_item == true) {
4255 sd->inventory.u.items_inventory[i].identify = 1;
4256 clif_item_identified(sd,i,0);
4257 }
4258 unidentified_count++;
4259 }
4260 }
4261
4262 return unidentified_count;
4263}
4264
4265//
4266// Items
4267//
4268
4269/*==========================================
4270 * Update buying value by skills
4271 *------------------------------------------*/
4272int pc_modifybuyvalue(struct map_session_data *sd,int orig_value)
4273{
4274 int skill,val = orig_value,rate1 = 0,rate2 = 0;
4275 if((skill=pc_checkskill(sd,MC_DISCOUNT))>0) // merchant discount
4276 rate1 = 5+skill*2-((skill==10)? 1:0);
4277 if((skill=pc_checkskill(sd,RG_COMPULSION))>0) // rogue discount
4278 rate2 = 5+skill*4;
4279 if(rate1 < rate2) rate1 = rate2;
4280 if(rate1)
4281 val = (int)((double)orig_value*(double)(100-rate1)/100.);
4282 if(val < 0) val = 0;
4283 if(orig_value > 0 && val < 1) val = 1;
4284
4285 return val;
4286}
4287
4288/*==========================================
4289 * Update selling value by skills
4290 *------------------------------------------*/
4291int pc_modifysellvalue(struct map_session_data *sd,int orig_value)
4292{
4293 int skill,val = orig_value,rate = 0;
4294 if((skill=pc_checkskill(sd,MC_OVERCHARGE))>0) //OverCharge
4295 rate = 5+skill*2-((skill==10)? 1:0);
4296 if(rate)
4297 val = (int)((double)orig_value*(double)(100+rate)/100.);
4298 if(val < 0) val = 0;
4299 if(orig_value > 0 && val < 1) val = 1;
4300
4301 return val;
4302}
4303
4304/*==========================================
4305 * Checking if we have enough place on inventory for new item
4306 * Make sure to take 30k as limit (for client I guess)
4307 * @param sd
4308 * @param nameid
4309 * @param amount
4310 * @return e_chkitem_result
4311 *------------------------------------------*/
4312char pc_checkadditem(struct map_session_data *sd, unsigned short nameid, int amount)
4313{
4314 int i;
4315 struct item_data* data;
4316
4317 nullpo_ret(sd);
4318
4319 if(amount > MAX_AMOUNT)
4320 return CHKADDITEM_OVERAMOUNT;
4321
4322 data = itemdb_search(nameid);
4323
4324 if(!itemdb_isstackable2(data))
4325 return CHKADDITEM_NEW;
4326
4327 if( data->stack.inventory && amount > data->stack.amount )
4328 return CHKADDITEM_OVERAMOUNT;
4329
4330 for(i=0;i<MAX_INVENTORY;i++){
4331 // FIXME: This does not consider the checked item's cards, thus could check a wrong slot for stackability.
4332 if(sd->inventory.u.items_inventory[i].nameid == nameid){
4333 if( amount > MAX_AMOUNT - sd->inventory.u.items_inventory[i].amount || ( data->stack.inventory && amount > data->stack.amount - sd->inventory.u.items_inventory[i].amount ) )
4334 return CHKADDITEM_OVERAMOUNT;
4335 return CHKADDITEM_EXIST;
4336 }
4337 }
4338
4339 return CHKADDITEM_NEW;
4340}
4341
4342/*==========================================
4343 * Return number of available place in inventory
4344 * Each non stackable item will reduce place by 1
4345 * @param sd
4346 * @return Number of empty slots
4347 *------------------------------------------*/
4348uint8 pc_inventoryblank(struct map_session_data *sd)
4349{
4350 uint8 i, b;
4351
4352 nullpo_ret(sd);
4353
4354 for(i = 0, b = 0; i < MAX_INVENTORY; i++){
4355 if(sd->inventory.u.items_inventory[i].nameid == 0)
4356 b++;
4357 }
4358
4359 return b;
4360}
4361
4362/**
4363 * Attempts to remove zeny from player
4364 * @param sd: Player
4365 * @param zeny: Zeny removed
4366 * @param type: Log type
4367 * @param tsd: (optional) From who to log (if null take sd)
4368 * @return 0: Success, 1: Failed (Removing negative Zeny or not enough Zeny), 2: Player not found
4369 */
4370char pc_payzeny(struct map_session_data *sd, int zeny, enum e_log_pick_type type, struct map_session_data *tsd)
4371{
4372 nullpo_retr(2,sd);
4373
4374 zeny = cap_value(zeny,-MAX_ZENY,MAX_ZENY); //prevent command UB
4375 if( zeny < 0 )
4376 {
4377 ShowError("pc_payzeny: Paying negative Zeny (zeny=%d, account_id=%d, char_id=%d).\n", zeny, sd->status.account_id, sd->status.char_id);
4378 return 1;
4379 }
4380
4381 if( sd->status.zeny < zeny )
4382 return 1; //Not enough.
4383
4384 sd->status.zeny -= zeny;
4385 clif_updatestatus(sd,SP_ZENY);
4386
4387 if(!tsd) tsd = sd;
4388 log_zeny(sd, type, tsd, -zeny);
4389 if( zeny > 0 && sd->state.showzeny ) {
4390 char output[255];
4391 sprintf(output, "Removed %dz.", zeny);
4392 clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
4393 }
4394
4395 return 0;
4396}
4397
4398/**
4399 * Attempts to give zeny to player
4400 * @param sd: Player
4401 * @param type: Log type
4402 * @param tsd: (optional) From who to log (if null take sd)
4403 * @return -1: Player not found, 0: Success, 1: Giving negative Zeny
4404 */
4405char pc_getzeny(struct map_session_data *sd, int zeny, enum e_log_pick_type type, struct map_session_data *tsd)
4406{
4407 nullpo_retr(-1,sd);
4408
4409 zeny = cap_value(zeny,-MAX_ZENY,MAX_ZENY); //prevent command UB
4410 if( zeny < 0 )
4411 {
4412 ShowError("pc_getzeny: Obtaining negative Zeny (zeny=%d, account_id=%d, char_id=%d).\n", zeny, sd->status.account_id, sd->status.char_id);
4413 return 1;
4414 }
4415
4416 if( zeny > MAX_ZENY - sd->status.zeny )
4417 zeny = MAX_ZENY - sd->status.zeny;
4418
4419 sd->status.zeny += zeny;
4420 clif_updatestatus(sd,SP_ZENY);
4421
4422 if(!tsd) tsd = sd;
4423 log_zeny(sd, type, tsd, zeny);
4424 if( zeny > 0 && sd->state.showzeny ) {
4425 char output[255];
4426 sprintf(output, "Gained %dz.", zeny);
4427 clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
4428 }
4429
4430 achievement_update_objective(sd, AG_GET_ZENY, 1, sd->status.zeny);
4431
4432 return 0;
4433}
4434
4435/**
4436 * Attempts to remove Cash Points from player
4437 * @param sd: Player
4438 * @param price: Points player has to pay
4439 * @param points: Points player has
4440 * @param type: Log type
4441 * @return -2: Paying negative points, -1: Not enough points, otherwise success (cash+points)
4442 */
4443int pc_paycash(struct map_session_data *sd, int price, int points, e_log_pick_type type)
4444{
4445 int cash;
4446 nullpo_retr(-1,sd);
4447
4448 points = cap_value(points,-MAX_ZENY,MAX_ZENY); //prevent command UB
4449 if( price < 0 || points < 0 )
4450 {
4451 ShowError("pc_paycash: Paying negative points (price=%d, points=%d, account_id=%d, char_id=%d).\n", price, points, sd->status.account_id, sd->status.char_id);
4452 return -2;
4453 }
4454
4455 if( points > price )
4456 {
4457 ShowWarning("pc_paycash: More kafra points provided than needed (price=%d, points=%d, account_id=%d, char_id=%d).\n", price, points, sd->status.account_id, sd->status.char_id);
4458 points = price;
4459 }
4460
4461 cash = price-points;
4462
4463 if( sd->cashPoints < cash || sd->kafraPoints < points )
4464 {
4465 ShowError("pc_paycash: Not enough points (cash=%d, kafra=%d) to cover the price (cash=%d, kafra=%d) (account_id=%d, char_id=%d).\n", sd->cashPoints, sd->kafraPoints, cash, points, sd->status.account_id, sd->status.char_id);
4466 return -1;
4467 }
4468
4469 if( cash ){
4470 pc_setaccountreg(sd, add_str(CASHPOINT_VAR), sd->cashPoints - cash);
4471 sd->cashPoints -= cash;
4472 log_cash( sd, type, LOG_CASH_TYPE_CASH, -cash );
4473 }
4474
4475 if( points ){
4476 pc_setaccountreg(sd, add_str(KAFRAPOINT_VAR), sd->kafraPoints - points);
4477 sd->kafraPoints -= points;
4478 log_cash( sd, type, LOG_CASH_TYPE_KAFRA, -points );
4479 }
4480
4481 if( battle_config.cashshop_show_points )
4482 {
4483 char output[CHAT_SIZE_MAX];
4484
4485 sprintf(output, msg_txt(sd,504), points, cash, sd->kafraPoints, sd->cashPoints); // Used %d kafra points and %d cash points. %d kafra and %d cash points remaining.
4486 clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
4487 }
4488 return cash+points;
4489}
4490
4491/**
4492 * Attempts to give Cash Points to player
4493 * @param sd: Player
4494 * @param cash: Cash player gets
4495 * @param points: Points player has
4496 * @param type: Log type
4497 * @return -2: Error, -1: Giving negative cash/points, otherwise success (cash or points)
4498 */
4499int pc_getcash(struct map_session_data *sd, int cash, int points, e_log_pick_type type)
4500{
4501 char output[CHAT_SIZE_MAX];
4502
4503 nullpo_retr(-1,sd);
4504
4505 cash = cap_value(cash,-MAX_ZENY,MAX_ZENY); //prevent command UB
4506 points = cap_value(points,-MAX_ZENY,MAX_ZENY); //prevent command UB
4507 if( cash > 0 )
4508 {
4509 if( cash > MAX_ZENY-sd->cashPoints )
4510 {
4511 ShowWarning("pc_getcash: Cash point overflow (cash=%d, have cash=%d, account_id=%d, char_id=%d).\n", cash, sd->cashPoints, sd->status.account_id, sd->status.char_id);
4512 cash = MAX_ZENY-sd->cashPoints;
4513 }
4514
4515 pc_setaccountreg(sd, add_str(CASHPOINT_VAR), sd->cashPoints+cash);
4516 sd->cashPoints += cash;
4517 log_cash( sd, type, LOG_CASH_TYPE_CASH, cash );
4518
4519 if( battle_config.cashshop_show_points )
4520 {
4521 sprintf(output, msg_txt(sd,505), cash, sd->cashPoints);
4522 clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
4523 }
4524 return cash;
4525 }
4526 else if( cash < 0 )
4527 {
4528 ShowError("pc_getcash: Obtaining negative cash points (cash=%d, account_id=%d, char_id=%d).\n", cash, sd->status.account_id, sd->status.char_id);
4529 return -1;
4530 }
4531
4532 if( points > 0 )
4533 {
4534 if( points > MAX_ZENY-sd->kafraPoints )
4535 {
4536 ShowWarning("pc_getcash: Kafra point overflow (points=%d, have points=%d, account_id=%d, char_id=%d).\n", points, sd->kafraPoints, sd->status.account_id, sd->status.char_id);
4537 points = MAX_ZENY-sd->kafraPoints;
4538 }
4539
4540 pc_setaccountreg(sd, add_str(KAFRAPOINT_VAR), sd->kafraPoints+points);
4541 sd->kafraPoints += points;
4542 log_cash( sd, type, LOG_CASH_TYPE_KAFRA, points );
4543
4544 if( battle_config.cashshop_show_points )
4545 {
4546 sprintf(output, msg_txt(sd,506), points, sd->kafraPoints);
4547 clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
4548 }
4549 return points;
4550 }
4551 else if( points < 0 )
4552 {
4553 ShowError("pc_getcash: Obtaining negative kafra points (points=%d, account_id=%d, char_id=%d).\n", points, sd->status.account_id, sd->status.char_id);
4554 return -1;
4555 }
4556 return -2; //shouldn't happen but just in case
4557}
4558
4559/**
4560 * Searching a specified itemid in inventory and return his stored index
4561 * @param sd Player
4562 * @param nameid Find this Item!
4563 * @return Stored index in inventory, or -1 if not found.
4564 **/
4565short pc_search_inventory(struct map_session_data *sd, unsigned short nameid) {
4566 short i;
4567 nullpo_retr(-1, sd);
4568
4569 ARR_FIND( 0, MAX_INVENTORY, i, sd->inventory.u.items_inventory[i].nameid == nameid && (sd->inventory.u.items_inventory[i].amount > 0 || nameid == 0) );
4570 return ( i < MAX_INVENTORY ) ? i : -1;
4571}
4572
4573/** Attempt to add a new item to player inventory
4574 * @param sd
4575 * @param item_data
4576 * @param amount
4577 * @param log_type
4578 * @return
4579 * 0 = success
4580 * 1 = invalid itemid not found or negative amount
4581 * 2 = overweight
4582 * 3 = ?
4583 * 4 = no free place found
4584 * 5 = max amount reached
4585 * 6 = ?
4586 * 7 = stack limitation
4587 */
4588char pc_additem(struct map_session_data *sd,struct item *item,int amount,e_log_pick_type log_type) {
4589 struct item_data *id;
4590 int16 i;
4591 unsigned int w;
4592
4593 nullpo_retr(1, sd);
4594 nullpo_retr(1, item);
4595
4596 if( item->nameid == 0 || amount <= 0 )
4597 return ADDITEM_INVALID;
4598 if( amount > MAX_AMOUNT )
4599 return ADDITEM_OVERAMOUNT;
4600
4601 id = itemdb_search(item->nameid);
4602
4603 if( id->stack.inventory && amount > id->stack.amount )
4604 {// item stack limitation
4605 return ADDITEM_STACKLIMIT;
4606 }
4607
4608 w = id->weight*amount;
4609 if(sd->weight + w > sd->max_weight)
4610 return ADDITEM_OVERWEIGHT;
4611
4612 i = MAX_INVENTORY;
4613
4614 if (id->flag.guid && !item->unique_id)
4615 item->unique_id = pc_generate_unique_id(sd);
4616
4617 // Stackable | Non Rental
4618 if( itemdb_isstackable2(id) && item->expire_time == 0 ) {
4619 for( i = 0; i < MAX_INVENTORY; i++ ) {
4620 if( sd->inventory.u.items_inventory[i].nameid == item->nameid &&
4621 sd->inventory.u.items_inventory[i].bound == item->bound &&
4622 sd->inventory.u.items_inventory[i].expire_time == 0 &&
4623 sd->inventory.u.items_inventory[i].unique_id == item->unique_id &&
4624 memcmp(&sd->inventory.u.items_inventory[i].card, &item->card, sizeof(item->card)) == 0 ) {
4625 if( amount > MAX_AMOUNT - sd->inventory.u.items_inventory[i].amount || ( id->stack.inventory && amount > id->stack.amount - sd->inventory.u.items_inventory[i].amount ) )
4626 return ADDITEM_OVERAMOUNT;
4627 sd->inventory.u.items_inventory[i].amount += amount;
4628 clif_additem(sd,i,amount,0);
4629 break;
4630 }
4631 }
4632 }
4633
4634 if (i >= MAX_INVENTORY) {
4635 i = pc_search_inventory(sd,0);
4636 if( i < 0 )
4637 return ADDITEM_OVERITEM;
4638
4639 memcpy(&sd->inventory.u.items_inventory[i], item, sizeof(sd->inventory.u.items_inventory[0]));
4640 // clear equip and favorite fields first, just in case
4641 if( item->equip )
4642 sd->inventory.u.items_inventory[i].equip = 0;
4643 if( item->favorite )
4644 sd->inventory.u.items_inventory[i].favorite = 0;
4645
4646 sd->inventory.u.items_inventory[i].amount = amount;
4647 sd->inventory_data[i] = id;
4648 sd->last_addeditem_index = i;
4649
4650 if (!itemdb_isstackable2(id) || id->flag.guid)
4651 sd->inventory.u.items_inventory[i].unique_id = item->unique_id ? item->unique_id : pc_generate_unique_id(sd);
4652
4653 clif_additem(sd,i,amount,0);
4654 }
4655
4656 log_pick_pc(sd, log_type, amount, &sd->inventory.u.items_inventory[i]);
4657
4658 sd->weight += w;
4659 clif_updatestatus(sd,SP_WEIGHT);
4660 //Auto-equip
4661 if(id->flag.autoequip)
4662 pc_equipitem(sd, i, id->equip);
4663
4664 /* rental item check */
4665 if( item->expire_time ) {
4666 if( time(NULL) > item->expire_time ) {
4667 clif_rental_expired(sd->fd, i, sd->inventory.u.items_inventory[i].nameid);
4668 pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 1, 0, LOG_TYPE_OTHER);
4669 } else {
4670 unsigned int seconds = (unsigned int)( item->expire_time - time(NULL) );
4671 clif_rental_time(sd->fd, sd->inventory.u.items_inventory[i].nameid, seconds);
4672 pc_inventory_rental_add(sd, seconds);
4673 }
4674 }
4675
4676 achievement_update_objective(sd, AG_GET_ITEM, 1, id->value_sell);
4677
4678 return ADDITEM_SUCCESS;
4679}
4680
4681/*==========================================
4682 * Remove an item at index n from inventory by amount.
4683 * @param sd
4684 * @param n Item index in inventory
4685 * @param amount
4686 * @param type &1: Don't notify deletion; &2 Don't notify weight change; &4 Don't calculate status
4687 * @param reason Delete reason
4688 * @param log_type e_log_pick_type
4689 * @return 1 - invalid itemid or negative amount; 0 - Success
4690 *------------------------------------------*/
4691char pc_delitem(struct map_session_data *sd,int n,int amount,int type, short reason, e_log_pick_type log_type)
4692{
4693 nullpo_retr(1, sd);
4694
4695 if(n < 0 || sd->inventory.u.items_inventory[n].nameid == 0 || amount <= 0 || sd->inventory.u.items_inventory[n].amount<amount || sd->inventory_data[n] == NULL)
4696 return 1;
4697
4698 log_pick_pc(sd, log_type, -amount, &sd->inventory.u.items_inventory[n]);
4699
4700 sd->inventory.u.items_inventory[n].amount -= amount;
4701 sd->weight -= sd->inventory_data[n]->weight*amount ;
4702 if( sd->inventory.u.items_inventory[n].amount <= 0 ){
4703 if(sd->inventory.u.items_inventory[n].equip)
4704 pc_unequipitem(sd,n,2|(!(type&4) ? 1 : 0));
4705 memset(&sd->inventory.u.items_inventory[n],0,sizeof(sd->inventory.u.items_inventory[0]));
4706 sd->inventory_data[n] = NULL;
4707 }
4708 if(!(type&1))
4709 clif_delitem(sd,n,amount,reason);
4710 if(!(type&2))
4711 clif_updatestatus(sd,SP_WEIGHT);
4712
4713 return 0;
4714}
4715
4716/*==========================================
4717 * Attempt to drop an item.
4718 * @param sd
4719 * @param n Item index in inventory
4720 * @param amount Amount of item
4721 * @return False = fail; True = success
4722 *------------------------------------------*/
4723bool pc_dropitem(struct map_session_data *sd,int n,int amount)
4724{
4725 nullpo_retr(1, sd);
4726
4727 if(n < 0 || n >= MAX_INVENTORY)
4728 return false;
4729
4730 if(amount <= 0)
4731 return false;
4732
4733 if(sd->inventory.u.items_inventory[n].nameid <= 0 ||
4734 sd->inventory.u.items_inventory[n].amount <= 0 ||
4735 sd->inventory.u.items_inventory[n].amount < amount ||
4736 sd->state.trading || sd->state.vending ||
4737 !sd->inventory_data[n] //pc_delitem would fail on this case.
4738 )
4739 return false;
4740
4741 if( map_getmapflag(sd->bl.m, MF_NODROP) )
4742 {
4743 clif_displaymessage (sd->fd, msg_txt(sd,271));
4744 return false; //Can't drop items in nodrop mapflag maps.
4745 }
4746
4747 if( !pc_candrop(sd,&sd->inventory.u.items_inventory[n]) )
4748 {
4749 clif_displaymessage (sd->fd, msg_txt(sd,263));
4750 return false;
4751 }
4752
4753 if (!map_addflooritem(&sd->inventory.u.items_inventory[n], amount, sd->bl.m, sd->bl.x, sd->bl.y, 0, 0, 0, 2, 0))
4754 return false;
4755
4756 pc_delitem(sd, n, amount, 1, 0, LOG_TYPE_PICKDROP_PLAYER);
4757 clif_dropitem(sd, n, amount);
4758 return true;
4759}
4760
4761/*==========================================
4762 * Attempt to pick up an item.
4763 * @param sd
4764 * @param fitem Item that will be picked
4765 * @return False = fail; True = success
4766 *------------------------------------------*/
4767bool pc_takeitem(struct map_session_data *sd,struct flooritem_data *fitem)
4768{
4769 int flag = 0;
4770 unsigned int tick = gettick();
4771 struct party_data *p = NULL;
4772
4773 nullpo_ret(sd);
4774 nullpo_ret(fitem);
4775
4776 if (!check_distance_bl(&fitem->bl, &sd->bl, 2) && sd->ud.skill_id!=BS_GREED)
4777 return false; // Distance is too far
4778
4779 if (sd->sc.cant.pickup)
4780 return false;
4781
4782 if (sd->status.party_id)
4783 p = party_search(sd->status.party_id);
4784
4785 if (fitem->first_get_charid > 0 && fitem->first_get_charid != sd->status.char_id) {
4786 struct map_session_data *first_sd = map_charid2sd(fitem->first_get_charid);
4787 if (DIFF_TICK(tick,fitem->first_get_tick) < 0) {
4788 if (!(p && p->party.item&1 &&
4789 first_sd && first_sd->status.party_id == sd->status.party_id
4790 ))
4791 return false;
4792 }
4793 else if (fitem->second_get_charid > 0 && fitem->second_get_charid != sd->status.char_id) {
4794 struct map_session_data *second_sd = map_charid2sd(fitem->second_get_charid);
4795 if (DIFF_TICK(tick, fitem->second_get_tick) < 0) {
4796 if (!(p && p->party.item&1 &&
4797 ((first_sd && first_sd->status.party_id == sd->status.party_id) ||
4798 (second_sd && second_sd->status.party_id == sd->status.party_id))
4799 ))
4800 return false;
4801 }
4802 else if (fitem->third_get_charid > 0 && fitem->third_get_charid != sd->status.char_id){
4803 struct map_session_data *third_sd = map_charid2sd(fitem->third_get_charid);
4804 if (DIFF_TICK(tick,fitem->third_get_tick) < 0) {
4805 if(!(p && p->party.item&1 &&
4806 ((first_sd && first_sd->status.party_id == sd->status.party_id) ||
4807 (second_sd && second_sd->status.party_id == sd->status.party_id) ||
4808 (third_sd && third_sd->status.party_id == sd->status.party_id))
4809 ))
4810 return false;
4811 }
4812 }
4813 }
4814 }
4815
4816 //This function takes care of giving the item to whoever should have it, considering party-share options.
4817 if ((flag = party_share_loot(p,sd,&fitem->item, fitem->first_get_charid))) {
4818 clif_additem(sd,0,0,flag);
4819 return true;
4820 }
4821
4822 //Display pickup animation.
4823 pc_stop_attack(sd);
4824 clif_takeitem(&sd->bl,&fitem->bl);
4825
4826 if (fitem->mob_id &&
4827 (itemdb_search(fitem->item.nameid))->flag.broadcast &&
4828 (!p || !(p->party.item&2)) // Somehow, if party's pickup distribution is 'Even Share', no announcemet
4829 )
4830 intif_broadcast_obtain_special_item(sd, fitem->item.nameid, fitem->mob_id, ITEMOBTAIN_TYPE_MONSTER_ITEM);
4831
4832 map_clearflooritem(&fitem->bl);
4833 return true;
4834}
4835
4836/*==========================================
4837 * Check if item is usable.
4838 * Return:
4839 * false = no
4840 * true = yes
4841 *------------------------------------------*/
4842bool pc_isUseitem(struct map_session_data *sd,int n)
4843{
4844 struct item_data *item;
4845 unsigned short nameid;
4846
4847 nullpo_ret(sd);
4848
4849 item = sd->inventory_data[n];
4850 nameid = sd->inventory.u.items_inventory[n].nameid;
4851
4852 if( item == NULL )
4853 return false;
4854 //Not consumable item
4855 if( item->type != IT_HEALING && item->type != IT_USABLE && item->type != IT_CASH )
4856 return false;
4857 if (pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL))
4858 return true;
4859
4860 struct map_data *mapdata = map_getmapdata(sd->bl.m);
4861
4862 if(mapdata->flag[MF_NOITEMCONSUMPTION]) //consumable but mapflag prevent it
4863 return false;
4864 //Prevent mass item usage. [Skotlex]
4865 if( DIFF_TICK(sd->canuseitem_tick,gettick()) > 0 ||
4866 (itemdb_group_item_exists(IG_CASH_FOOD, nameid) && DIFF_TICK(sd->canusecashfood_tick,gettick()) > 0)
4867 )
4868 return false;
4869
4870 if( (item->item_usage.flag&NOUSE_SITTING) && (pc_issit(sd) == 1) && (pc_get_group_level(sd) < item->item_usage.override) ) {
4871 clif_msg(sd,ITEM_NOUSE_SITTING);
4872 return false; // You cannot use this item while sitting.
4873 }
4874
4875 /*if (sd->state.storage_flag && item->type != IT_CASH) {
4876 clif_messagecolor(&sd->bl, color_table[COLOR_RED], msg_txt(sd,388), false, SELF);
4877 return false; // You cannot use this item while storage is open.
4878 }*/
4879
4880 if (item->flag.dead_branch && (mapdata->flag[MF_NOBRANCH] || mapdata_flag_gvg2(mapdata)))
4881 return false;
4882
4883 switch( nameid ) {
4884 case ITEMID_WING_OF_FLY:
4885 case ITEMID_GIANT_FLY_WING:
4886 case ITEMID_N_FLY_WING:
4887 if( mapdata->flag[MF_NOTELEPORT] || mapdata_flag_gvg2(mapdata) ) {
4888 clif_skill_teleportmessage(sd,0);
4889 return false;
4890 }
4891 if (nameid == ITEMID_GIANT_FLY_WING) {
4892 struct party_data *pd = party_search(sd->status.party_id);
4893
4894 if (pd) {
4895 int i;
4896
4897 ARR_FIND(0, MAX_PARTY, i, pd->data[i].sd == sd && pd->party.member[i].leader);
4898 if (i == MAX_PARTY) { // User is not party leader
4899 clif_msg(sd, ITEM_PARTY_MEMBER_NOT_SUMMONED);
4900 break;
4901 }
4902
4903 ARR_FIND(0, MAX_PARTY, i, pd->data[i].sd && pd->data[i].sd != sd && pd->data[i].sd->bl.m == sd->bl.m && !pc_isdead(pd->data[i].sd));
4904 if (i == MAX_PARTY) { // No party members found on same map
4905 clif_msg(sd, ITEM_PARTY_NO_MEMBER_IN_MAP);
4906 break;
4907 }
4908 } else {
4909 clif_msg(sd, ITEM_PARTY_MEMBER_NOT_SUMMONED);
4910 break;
4911 }
4912 }
4913 // Fall through
4914 case ITEMID_WING_OF_BUTTERFLY:
4915 case ITEMID_N_BUTTERFLY_WING:
4916 case ITEMID_DUN_TELE_SCROLL1:
4917 case ITEMID_DUN_TELE_SCROLL2:
4918 case ITEMID_DUN_TELE_SCROLL3:
4919 case ITEMID_WOB_RUNE:
4920 case ITEMID_WOB_SCHWALTZ:
4921 case ITEMID_WOB_RACHEL:
4922 case ITEMID_WOB_LOCAL:
4923 case ITEMID_SIEGE_TELEPORT_SCROLL:
4924 if( sd->duel_group && !battle_config.duel_allow_teleport ) {
4925 clif_displaymessage(sd->fd, msg_txt(sd,663));
4926 return false;
4927 }
4928 if( mapdata->flag[MF_NORETURN] && nameid != ITEMID_WING_OF_FLY && nameid != ITEMID_GIANT_FLY_WING && nameid != ITEMID_N_FLY_WING )
4929 return false;
4930 break;
4931 case ITEMID_MERCENARY_RED_POTION:
4932 case ITEMID_MERCENARY_BLUE_POTION:
4933 case ITEMID_M_CENTER_POTION:
4934 case ITEMID_M_AWAKENING_POTION:
4935 case ITEMID_M_BERSERK_POTION:
4936 if( sd->md == NULL || sd->md->db == NULL )
4937 return false;
4938 if( sd->md->sc.data[SC_BERSERK] )
4939 return false;
4940 if( nameid == ITEMID_M_AWAKENING_POTION && sd->md->db->lv < 40 )
4941 return false;
4942 if( nameid == ITEMID_M_BERSERK_POTION && sd->md->db->lv < 80 )
4943 return false;
4944 break;
4945
4946 case ITEMID_NEURALIZER:
4947 if( !mapdata->flag[MF_RESET] )
4948 return false;
4949 break;
4950 }
4951
4952 if( itemdb_group_item_exists(IG_MERCENARY, nameid) && sd->md != NULL )
4953 return false; // Mercenary Scrolls
4954
4955 if( item->flag.group || item->type == IT_CASH) { //safe check type cash disappear when overweight [Napster]
4956 if( pc_is90overweight(sd) ) {
4957 clif_msg(sd, ITEM_CANT_OBTAIN_WEIGHT);
4958 return false;
4959 }
4960 if( !pc_inventoryblank(sd) ) {
4961 clif_messagecolor(&sd->bl, color_table[COLOR_RED], msg_txt(sd, 732), false, SELF); //Item cannot be open when inventory is full
4962 return false;
4963 }
4964 }
4965
4966 //Gender check
4967 if(item->sex != 2 && sd->status.sex != item->sex)
4968 return false;
4969 //Required level check
4970 if(item->elv && sd->status.base_level < (unsigned int)item->elv)
4971 return false;
4972 if(item->elvmax && sd->status.base_level > (unsigned int)item->elvmax)
4973 return false;
4974
4975 //Not equipable by class. [Skotlex]
4976 if (!(
4977 (1ULL<<(sd->class_&MAPID_BASEMASK)) &
4978 (item->class_base[sd->class_&JOBL_2_1?1:(sd->class_&JOBL_2_2?2:0)])
4979 ))
4980 return false;
4981
4982 if (sd->sc.count && (
4983 sd->sc.data[SC_BERSERK] || sd->sc.data[SC_SATURDAYNIGHTFEVER] ||
4984 (sd->sc.data[SC_GRAVITATION] && sd->sc.data[SC_GRAVITATION]->val3 == BCT_SELF) ||
4985 sd->sc.data[SC_TRICKDEAD] ||
4986 sd->sc.data[SC_HIDING] ||
4987 sd->sc.data[SC__SHADOWFORM] ||
4988 sd->sc.data[SC__INVISIBILITY] ||
4989 sd->sc.data[SC__MANHOLE] ||
4990 sd->sc.data[SC_DEEPSLEEP] ||
4991 sd->sc.data[SC_CRYSTALIZE] ||
4992 sd->sc.data[SC_KAGEHUMI] ||
4993 (sd->sc.data[SC_NOCHAT] && sd->sc.data[SC_NOCHAT]->val1&MANNER_NOITEM) ||
4994 sd->sc.data[SC_KINGS_GRACE] ||
4995 sd->sc.data[SC_SUHIDE]))
4996 return false;
4997
4998 if (!pc_isItemClass(sd,item))
4999 return false;
5000
5001 //Dead Branch items
5002 if( item->flag.dead_branch )
5003 log_branch(sd);
5004
5005 return true;
5006}
5007
5008/*==========================================
5009 * Last checks to use an item.
5010 * Return:
5011 * 0 = fail
5012 * 1 = success
5013 *------------------------------------------*/
5014int pc_useitem(struct map_session_data *sd,int n)
5015{
5016 unsigned int tick = gettick();
5017 int amount;
5018 unsigned short nameid;
5019 struct script_code *script;
5020 struct item item;
5021 struct item_data *id;
5022
5023 nullpo_ret(sd);
5024
5025 if (sd->npc_id) {
5026 if (sd->progressbar.npc_id) {
5027 clif_progressbar_abort(sd);
5028 return 0; // First item use attempt cancels the progress bar
5029 }
5030
5031 if( pc_hasprogress( sd, WIP_DISABLE_SKILLITEM ) || !sd->npc_item_flag ){
5032#ifdef RENEWAL
5033 clif_msg( sd, WORK_IN_PROGRESS );
5034#endif
5035 return 0;
5036 }
5037 }
5038 item = sd->inventory.u.items_inventory[n];
5039 id = sd->inventory_data[n];
5040
5041 if (item.nameid == 0 || item.amount <= 0)
5042 return 0;
5043
5044 if( !pc_isUseitem(sd,n) )
5045 return 0;
5046
5047 // Store information for later use before it is lost (via pc_delitem) [Paradox924X]
5048 nameid = id->nameid;
5049
5050 if (nameid != ITEMID_NAUTHIZ && sd->sc.opt1 > 0 && sd->sc.opt1 != OPT1_STONEWAIT && sd->sc.opt1 != OPT1_BURNING)
5051 return 0;
5052
5053 /* Items with delayed consume are not meant to work while in mounts except reins of mount(12622) */
5054 if( id->flag.delay_consume ) {
5055 if( nameid != ITEMID_REINS_OF_MOUNT && sd->sc.data[SC_ALL_RIDING] )
5056 return 0;
5057 else if( pc_issit(sd) )
5058 return 0;
5059 }
5060 //Since most delay-consume items involve using a "skill-type" target cursor,
5061 //perform a skill-use check before going through. [Skotlex]
5062 //resurrection was picked as testing skill, as a non-offensive, generic skill, it will do.
5063 //FIXME: Is this really needed here? It'll be checked in unit.c after all and this prevents skill items using when silenced [Inkfish]
5064 if( id->flag.delay_consume && ( sd->ud.skilltimer != INVALID_TIMER /*|| !status_check_skilluse(&sd->bl, &sd->bl, ALL_RESURRECTION, 0)*/ ) )
5065 return 0;
5066
5067 if( id->delay > 0 && !pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL) && pc_itemcd_check(sd, id, tick, n))
5068 return 0;
5069
5070 /* on restricted maps the item is consumed but the effect is not used */
5071 if (!pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL) && itemdb_isNoEquip(id,sd->bl.m)) {
5072 clif_msg(sd,ITEM_CANT_USE_AREA); // This item cannot be used within this area
5073 if( battle_config.allow_consume_restricted_item && !id->flag.delay_consume ) { //need confirmation for delayed consumption items
5074 clif_useitemack(sd,n,item.amount-1,true);
5075 pc_delitem(sd,n,1,1,0,LOG_TYPE_CONSUME);
5076 }
5077 return 0;/* regardless, effect is not run */
5078 }
5079
5080 sd->itemid = item.nameid;
5081 sd->itemindex = n;
5082 if(sd->catch_target_class != PET_CATCH_FAIL) //Abort pet catching.
5083 sd->catch_target_class = PET_CATCH_FAIL;
5084
5085 amount = item.amount;
5086 script = id->script;
5087 //Check if the item is to be consumed immediately [Skotlex]
5088 if (id->flag.delay_consume)
5089 clif_useitemack(sd, n, amount, true);
5090 else
5091 {
5092 if( item.expire_time == 0 && nameid != ITEMID_REINS_OF_MOUNT )
5093 {
5094 clif_useitemack(sd, n, amount - 1, true);
5095 pc_delitem(sd, n, 1, 1, 0, LOG_TYPE_CONSUME); // Rental Usable Items are not deleted until expiration
5096 }
5097 else
5098 clif_useitemack(sd, n, 0, false);
5099 }
5100 if (item.card[0]==CARD0_CREATE && pc_famerank(MakeDWord(item.card[2],item.card[3]), MAPID_ALCHEMIST))
5101 potion_flag = 2; // Famous player's potions have 50% more efficiency
5102
5103 //Update item use time.
5104 sd->canuseitem_tick = tick + battle_config.item_use_interval;
5105 if( itemdb_group_item_exists(IG_CASH_FOOD, nameid) )
5106 sd->canusecashfood_tick = tick + battle_config.cashfood_use_interval;
5107
5108 run_script(script,0,sd->bl.id,fake_nd->bl.id);
5109 potion_flag = 0;
5110 return 1;
5111}
5112
5113/**
5114 * Add item on cart for given index.
5115 * @param sd
5116 * @param item
5117 * @param amount
5118 * @param log_type
5119 * @return 0 = success; 1 = fail; 2 = no slot
5120 */
5121unsigned char pc_cart_additem(struct map_session_data *sd,struct item *item,int amount,e_log_pick_type log_type)
5122{
5123 struct item_data *data;
5124 int i,w;
5125
5126 nullpo_retr(1, sd);
5127 nullpo_retr(1, item);
5128
5129 if(item->nameid == 0 || amount <= 0)
5130 return 1;
5131 data = itemdb_search(item->nameid);
5132
5133 if( data->stack.cart && amount > data->stack.amount )
5134 {// item stack limitation
5135 return 1;
5136 }
5137
5138 if( !itemdb_cancartstore(item, pc_get_group_level(sd)) || (item->bound > BOUND_ACCOUNT && !pc_can_give_bounded_items(sd)))
5139 { // Check item trade restrictions [Skotlex]
5140 clif_displaymessage (sd->fd, msg_txt(sd,264));
5141 return 1;
5142 }
5143
5144 if( (w = data->weight*amount) + sd->cart_weight > sd->cart_weight_max )
5145 return 1;
5146
5147 i = MAX_CART;
5148 if( itemdb_isstackable2(data) && !item->expire_time )
5149 {
5150 for (i = 0; i < MAX_CART; i++) {
5151 if (sd->cart.u.items_cart[i].nameid == item->nameid
5152 && sd->cart.u.items_cart[i].bound == item->bound
5153 && sd->cart.u.items_cart[i].unique_id == item->unique_id
5154 && memcmp(sd->cart.u.items_cart[i].card, item->card, sizeof(item->card)) == 0
5155 )
5156 break;
5157 }
5158 }
5159
5160 if( i < MAX_CART )
5161 {// item already in cart, stack it
5162 if( amount > MAX_AMOUNT - sd->cart.u.items_cart[i].amount || ( data->stack.cart && amount > data->stack.amount - sd->cart.u.items_cart[i].amount ) )
5163 return 2; // no slot
5164
5165 sd->cart.u.items_cart[i].amount += amount;
5166 clif_cart_additem(sd,i,amount,0);
5167 }
5168 else
5169 {// item not stackable or not present, add it
5170 ARR_FIND( 0, MAX_CART, i, sd->cart.u.items_cart[i].nameid == 0 );
5171 if( i == MAX_CART )
5172 return 2; // no slot
5173
5174 memcpy(&sd->cart.u.items_cart[i],item,sizeof(sd->cart.u.items_cart[0]));
5175 sd->cart.u.items_cart[i].id = 0;
5176 sd->cart.u.items_cart[i].amount = amount;
5177 sd->cart_num++;
5178 clif_cart_additem(sd,i,amount,0);
5179 }
5180 sd->cart.u.items_cart[i].favorite = 0; // clear
5181 log_pick_pc(sd, log_type, amount, &sd->cart.u.items_cart[i]);
5182
5183 sd->cart_weight += w;
5184 clif_updatestatus(sd,SP_CARTINFO);
5185
5186 return 0;
5187}
5188
5189/*==========================================
5190 * Delete item on cart for given index.
5191 *------------------------------------------*/
5192void pc_cart_delitem(struct map_session_data *sd,int n,int amount,int type,e_log_pick_type log_type)
5193{
5194 nullpo_retv(sd);
5195
5196 if(sd->cart.u.items_cart[n].nameid == 0 ||
5197 sd->cart.u.items_cart[n].amount < amount)
5198 return;
5199
5200 log_pick_pc(sd, log_type, -amount, &sd->cart.u.items_cart[n]);
5201
5202 sd->cart.u.items_cart[n].amount -= amount;
5203 sd->cart_weight -= itemdb_weight(sd->cart.u.items_cart[n].nameid) * amount;
5204 if(sd->cart.u.items_cart[n].amount <= 0) {
5205 memset(&sd->cart.u.items_cart[n],0,sizeof(sd->cart.u.items_cart[0]));
5206 sd->cart_num--;
5207 }
5208 if(!type) {
5209 clif_cart_delitem(sd,n,amount);
5210 clif_updatestatus(sd,SP_CARTINFO);
5211 }
5212}
5213
5214/*==========================================
5215 * Transfer item from inventory to cart.
5216 *------------------------------------------*/
5217void pc_putitemtocart(struct map_session_data *sd,int idx,int amount)
5218{
5219 struct item *item_data;
5220 char flag;
5221
5222 nullpo_retv(sd);
5223
5224 if (idx < 0 || idx >= MAX_INVENTORY) //Invalid index check [Skotlex]
5225 return;
5226
5227 item_data = &sd->inventory.u.items_inventory[idx];
5228
5229 if( item_data->nameid == 0 || amount < 1 || item_data->amount < amount || sd->state.vending )
5230 return;
5231
5232 if( (flag = pc_cart_additem(sd,item_data,amount,LOG_TYPE_NONE)) == 0 )
5233 pc_delitem(sd,idx,amount,0,5,LOG_TYPE_NONE);
5234 else {
5235 clif_dropitem(sd,idx,0);
5236 clif_cart_additem_ack(sd,(flag==1)?ADDITEM_TO_CART_FAIL_WEIGHT:ADDITEM_TO_CART_FAIL_COUNT);
5237 }
5238}
5239
5240/*==========================================
5241 * Get number of item in cart.
5242 * Return:
5243 -1 = itemid not found or no amount found
5244 x = remaining itemid on cart after get
5245 *------------------------------------------*/
5246int pc_cartitem_amount(struct map_session_data* sd, int idx, int amount)
5247{
5248 struct item* item_data;
5249
5250 nullpo_retr(-1, sd);
5251
5252 item_data = &sd->cart.u.items_cart[idx];
5253 if( item_data->nameid == 0 || item_data->amount == 0 )
5254 return -1;
5255
5256 return item_data->amount - amount;
5257}
5258
5259/*==========================================
5260 * Retrieve an item at index idx from cart.
5261 *------------------------------------------*/
5262void pc_getitemfromcart(struct map_session_data *sd,int idx,int amount)
5263{
5264 struct item *item_data;
5265 unsigned char flag = 0;
5266
5267 nullpo_retv(sd);
5268
5269 if (idx < 0 || idx >= MAX_CART) //Invalid index check [Skotlex]
5270 return;
5271
5272 item_data=&sd->cart.u.items_cart[idx];
5273
5274 if(item_data->nameid == 0 || amount < 1 || item_data->amount < amount || sd->state.vending )
5275 return;
5276 if((flag = pc_additem(sd,item_data,amount,LOG_TYPE_NONE)) == 0)
5277 pc_cart_delitem(sd,idx,amount,0,LOG_TYPE_NONE);
5278 else {
5279 clif_dropitem(sd,idx,0);
5280 clif_additem(sd,0,0,flag);
5281 }
5282}
5283
5284/*==========================================
5285 * Bound Item Check
5286 * Type:
5287 * 1 Account Bound
5288 * 2 Guild Bound
5289 * 3 Party Bound
5290 * 4 Character Bound
5291 *------------------------------------------*/
5292int pc_bound_chk(TBL_PC *sd,enum bound_type type,int *idxlist)
5293{
5294 int i = 0, j = 0;
5295 for(i = 0; i < MAX_INVENTORY; i++) {
5296 if(sd->inventory.u.items_inventory[i].nameid > 0 && sd->inventory.u.items_inventory[i].amount > 0 && sd->inventory.u.items_inventory[i].bound == type) {
5297 idxlist[j] = i;
5298 j++;
5299 }
5300 }
5301 return j;
5302}
5303
5304/*==========================================
5305 * Display item stolen msg to player sd
5306 *------------------------------------------*/
5307int pc_show_steal(struct block_list *bl,va_list ap)
5308{
5309 struct map_session_data *sd;
5310 int itemid;
5311
5312 struct item_data *item=NULL;
5313 char output[100];
5314
5315 sd=va_arg(ap,struct map_session_data *);
5316 itemid=va_arg(ap,int);
5317
5318 if((item=itemdb_exists(itemid))==NULL)
5319 sprintf(output,"%s stole an Unknown Item (id: %i).",sd->status.name, itemid);
5320 else
5321 sprintf(output,"%s stole %s.",sd->status.name,item->jname);
5322 clif_displaymessage( ((struct map_session_data *)bl)->fd, output);
5323
5324 return 0;
5325}
5326/*==========================================
5327 * Steal an item from bl (mob).
5328 * Return:
5329 * 0 = fail
5330 * 1 = succes
5331 *------------------------------------------*/
5332int pc_steal_item(struct map_session_data *sd,struct block_list *bl, uint16 skill_lv)
5333{
5334 int i,itemid;
5335 double rate;
5336 unsigned char flag = 0;
5337 struct status_data *sd_status, *md_status;
5338 struct mob_data *md;
5339 struct item tmp_item;
5340
5341 if(!sd || !bl || bl->type!=BL_MOB)
5342 return 0;
5343
5344 md = (TBL_MOB *)bl;
5345
5346 if(md->state.steal_flag == UCHAR_MAX || ( md->sc.opt1 && md->sc.opt1 != OPT1_BURNING ) ) //already stolen from / status change check
5347 return 0;
5348
5349 sd_status= status_get_status_data(&sd->bl);
5350 md_status= status_get_status_data(bl);
5351
5352 if (md->master_id || status_has_mode(md_status, MD_STATUS_IMMUNE) || status_get_race2(&md->bl) == RC2_TREASURE ||
5353 map_getmapflag(bl->m, MF_NOMOBLOOT) || // check noloot map flag [Lorky]
5354 (battle_config.skill_steal_max_tries && //Reached limit of steal attempts. [Lupus]
5355 md->state.steal_flag++ >= battle_config.skill_steal_max_tries)
5356 ) { //Can't steal from
5357 md->state.steal_flag = UCHAR_MAX;
5358 return 0;
5359 }
5360
5361 // base skill success chance (percentual)
5362 rate = (sd_status->dex - md_status->dex)/2 + skill_lv*6 + 4;
5363 rate += sd->bonus.add_steal_rate;
5364
5365 if( rate < 1 )
5366 return 0;
5367
5368 // Try dropping one item, in the order from first to last possible slot.
5369 // Droprate is affected by the skill success rate.
5370 for( i = 0; i < MAX_STEAL_DROP; i++ )
5371 if( md->db->dropitem[i].nameid > 0 && !md->db->dropitem[i].steal_protected && itemdb_exists(md->db->dropitem[i].nameid) && rnd() % 10000 < md->db->dropitem[i].p * rate/100. )
5372 break;
5373 if( i == MAX_STEAL_DROP )
5374 return 0;
5375
5376 itemid = md->db->dropitem[i].nameid;
5377 memset(&tmp_item,0,sizeof(tmp_item));
5378 tmp_item.nameid = itemid;
5379 tmp_item.amount = 1;
5380 tmp_item.identify = itemdb_isidentified(itemid);
5381 mob_setdropitem_option(&tmp_item, &md->db->dropitem[i]);
5382 flag = pc_additem(sd,&tmp_item,1,LOG_TYPE_PICKDROP_PLAYER);
5383
5384 //TODO: Should we disable stealing when the item you stole couldn't be added to your inventory? Perhaps players will figure out a way to exploit this behaviour otherwise?
5385 md->state.steal_flag = UCHAR_MAX; //you can't steal from this mob any more
5386
5387 if(flag) { //Failed to steal due to overweight
5388 clif_additem(sd,0,0,flag);
5389 return 0;
5390 }
5391
5392 if(battle_config.show_steal_in_same_party)
5393 party_foreachsamemap(pc_show_steal,sd,AREA_SIZE,sd,tmp_item.nameid);
5394
5395 //Logs items, Stolen from mobs [Lupus]
5396 log_pick_mob(md, LOG_TYPE_STEAL, -1, &tmp_item);
5397
5398 //A Rare Steal Global Announce by Lupus
5399 if(md->db->dropitem[i].p<=battle_config.rare_drop_announce) {
5400 struct item_data *i_data;
5401 char message[128];
5402 i_data = itemdb_search(itemid);
5403 sprintf (message, msg_txt(sd,542), (sd->status.name[0])?sd->status.name :"GM", md->db->jname, i_data->jname, (float)md->db->dropitem[i].p/100);
5404 //MSG: "'%s' stole %s's %s (chance: %0.02f%%)"
5405 intif_broadcast(message, strlen(message) + 1, BC_DEFAULT);
5406 }
5407 return 1;
5408}
5409
5410/*==========================================
5411 * Stole zeny from bl (mob)
5412 * return
5413 * 0 = fail
5414 * 1 = success
5415 *------------------------------------------*/
5416int pc_steal_coin(struct map_session_data *sd,struct block_list *target)
5417{
5418 int rate, target_lv;
5419 struct mob_data *md;
5420
5421 if(!sd || !target || target->type != BL_MOB)
5422 return 0;
5423
5424 md = (TBL_MOB*)target;
5425 target_lv = status_get_lv(target);
5426
5427 if (md->state.steal_coin_flag || md->sc.data[SC_STONE] || md->sc.data[SC_FREEZE] || status_bl_has_mode(target,MD_STATUS_IMMUNE) || status_get_race2(&md->bl) == RC2_TREASURE)
5428 return 0;
5429
5430 rate = sd->battle_status.dex / 2 + 2 * (sd->status.base_level - target_lv) + (10 * pc_checkskill(sd, RG_STEALCOIN)) + sd->battle_status.luk / 2;
5431 if(rnd()%1000 < rate)
5432 {
5433 // Zeny Steal Amount: (rnd() % (10 * target_lv + 1 - 8 * target_lv)) + 8 * target_lv
5434 int amount = (rnd() % (2 * target_lv + 1)) + 8 * target_lv; // Reduced formula
5435
5436 pc_getzeny(sd, amount, LOG_TYPE_STEAL, NULL);
5437 md->state.steal_coin_flag = 1;
5438 return 1;
5439 }
5440 return 0;
5441}
5442
5443/*==========================================
5444 * Set's a player position.
5445 * @param sd
5446 * @param mapindex
5447 * @param x
5448 * @param y
5449 * @param clrtype
5450 * @return SETPOS_OK Success
5451 * SETPOS_MAPINDEX Invalid map index
5452 * SETPOS_NO_MAPSERVER Map not in this map-server, and failed to locate alternate map-server.
5453 * SETPOS_AUTOTRADE Player is in autotrade state
5454 *------------------------------------------*/
5455enum e_setpos pc_setpos(struct map_session_data* sd, unsigned short mapindex, int x, int y, clr_type clrtype)
5456{
5457 nullpo_retr(SETPOS_OK,sd);
5458
5459 if( !mapindex || !mapindex_id2name(mapindex) ) {
5460 ShowDebug("pc_setpos: Passed mapindex(%d) is invalid!\n", mapindex);
5461 return SETPOS_MAPINDEX;
5462 }
5463
5464 if ( sd->state.autotrade && (sd->vender_id || sd->buyer_id) ) // Player with autotrade just causes clif glitch! @ FIXME
5465 return SETPOS_AUTOTRADE;
5466
5467 if( battle_config.revive_onwarp && pc_isdead(sd) ) { //Revive dead people before warping them
5468 pc_setstand(sd, true);
5469 pc_setrestartvalue(sd,1);
5470 }
5471
5472 int16 m = map_mapindex2mapid(mapindex);
5473 struct map_data *mapdata = map_getmapdata(m);
5474
5475 sd->state.changemap = (sd->mapindex != mapindex);
5476 sd->state.warping = 1;
5477 sd->state.workinprogress = WIP_DISABLE_NONE;
5478
5479 if( sd->state.changemap ) { // Misc map-changing settings
5480 int i;
5481
5482 if(map_getmapdata(sd->bl.m)->instance_id && !mapdata->instance_id) {
5483 bool instance_found = false;
5484 struct party_data *p = NULL;
5485 struct guild *g = NULL;
5486
5487 if (sd->instance_id) {
5488 instance_delusers(sd->instance_id);
5489 instance_found = true;
5490 }
5491 if (!instance_found && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id) {
5492 instance_delusers(p->instance_id);
5493 instance_found = true;
5494 }
5495 if (!instance_found && sd->status.guild_id && (g = guild_search(sd->status.guild_id)) != NULL && g->instance_id)
5496 instance_delusers(g->instance_id);
5497 }
5498
5499 sd->state.pmap = sd->bl.m;
5500 if (sd->sc.count) { // Cancel some map related stuff.
5501 if (sd->sc.data[SC_JAILED])
5502 return SETPOS_MAPINDEX; //You may not get out!
5503 status_change_end(&sd->bl, SC_BOSSMAPINFO, INVALID_TIMER);
5504 status_change_end(&sd->bl, SC_WARM, INVALID_TIMER);
5505 status_change_end(&sd->bl, SC_SUN_COMFORT, INVALID_TIMER);
5506 status_change_end(&sd->bl, SC_MOON_COMFORT, INVALID_TIMER);
5507 status_change_end(&sd->bl, SC_STAR_COMFORT, INVALID_TIMER);
5508 status_change_end(&sd->bl, SC_MIRACLE, INVALID_TIMER);
5509 if (sd->sc.data[SC_KNOWLEDGE]) {
5510 struct status_change_entry *sce = sd->sc.data[SC_KNOWLEDGE];
5511 if (sce->timer != INVALID_TIMER)
5512 delete_timer(sce->timer, status_change_timer);
5513 sce->timer = add_timer(gettick() + skill_get_time(SG_KNOWLEDGE, sce->val1), status_change_timer, sd->bl.id, SC_KNOWLEDGE);
5514 }
5515 status_change_end(&sd->bl, SC_PROPERTYWALK, INVALID_TIMER);
5516 status_change_end(&sd->bl, SC_CLOAKING, INVALID_TIMER);
5517 status_change_end(&sd->bl, SC_CLOAKINGEXCEED, INVALID_TIMER);
5518 }
5519 for( i = 0; i < EQI_MAX; i++ ) {
5520 if( sd->equip_index[i] >= 0 )
5521 if( pc_isequip(sd,sd->equip_index[i]) )
5522 pc_unequipitem(sd,sd->equip_index[i],2);
5523 }
5524 if (battle_config.clear_unit_onwarp&BL_PC)
5525 skill_clear_unitgroup(&sd->bl);
5526 party_send_dot_remove(sd); //minimap dot fix [Kevin]
5527 guild_send_dot_remove(sd);
5528 bg_send_dot_remove(sd);
5529 if (sd->regen.state.gc)
5530 sd->regen.state.gc = 0;
5531 // make sure vending is allowed here
5532 if (sd->state.vending && mapdata->flag[MF_NOVENDING]) {
5533 clif_displaymessage (sd->fd, msg_txt(sd,276)); // "You can't open a shop on this map"
5534 vending_closevending(sd);
5535 }
5536
5537 channel_pcquit(sd,4); //quit map chan
5538 }
5539
5540 if( m < 0 )
5541 {
5542 uint32 ip;
5543 uint16 port;
5544 struct script_state *st;
5545
5546 //if can't find any map-servers, just abort setting position.
5547 if(!sd->mapindex || map_mapname2ipport(mapindex,&ip,&port))
5548 return SETPOS_NO_MAPSERVER;
5549
5550 if (sd->npc_id){
5551 npc_event_dequeue(sd,false);
5552 st = sd->st;
5553 }else{
5554 st = nullptr;
5555 }
5556
5557 npc_script_event(sd, NPCE_LOGOUT);
5558 //remove from map, THEN change x/y coordinates
5559 unit_remove_map_pc(sd,clrtype);
5560 sd->mapindex = mapindex;
5561 sd->bl.x=x;
5562 sd->bl.y=y;
5563 pc_clean_skilltree(sd);
5564 chrif_save(sd, CSAVE_CHANGE_MAPSERV|CSAVE_INVENTORY|CSAVE_CART);
5565 chrif_changemapserver(sd, ip, (short)port);
5566
5567 //Free session data from this map server [Kevin]
5568 unit_free_pc(sd);
5569
5570 if( st ){
5571 // Has to be done here, because otherwise unit_free_pc will free the stack already
5572 st->state = END;
5573 }
5574
5575 return SETPOS_OK;
5576 }
5577
5578 if( x < 0 || x >= mapdata->xs || y < 0 || y >= mapdata->ys )
5579 {
5580 ShowError("pc_setpos: attempt to place player '%s' (%d:%d) on invalid coordinates (%s-%d,%d)\n", sd->status.name, sd->status.account_id, sd->status.char_id, mapindex_id2name(mapindex),x,y);
5581 x = y = 0; // make it random
5582 }
5583
5584 if( x == 0 && y == 0 ) { // pick a random walkable cell
5585 int c=0;
5586 do {
5587 x = rnd()%(mapdata->xs-2)+1;
5588 y = rnd()%(mapdata->ys-2)+1;
5589 c++;
5590
5591 if(c > (mapdata->xs * mapdata->ys)*3){ //force out
5592 ShowError("pc_setpos: couldn't found a valid coordinates for player '%s' (%d:%d) on (%s), preventing warp\n", sd->status.name, sd->status.account_id, sd->status.char_id, mapindex_id2name(mapindex));
5593 return SETPOS_OK; //preventing warp
5594 //break; //allow warp anyway
5595 }
5596 } while(map_getcell(m,x,y,CELL_CHKNOPASS) || (!battle_config.teleport_on_portal && npc_check_areanpc(1,m,x,y,1)));
5597 }
5598
5599 if (sd->state.vending && map_getcell(m,x,y,CELL_CHKNOVENDING)) {
5600 clif_displaymessage (sd->fd, msg_txt(sd,204)); // "You can't open a shop on this cell."
5601 vending_closevending(sd);
5602 }
5603
5604 if(sd->bl.prev != NULL){
5605 unit_remove_map_pc(sd,clrtype);
5606 clif_changemap(sd,m,x,y); // [MouseJstr]
5607 } else if(sd->state.active) //Tag player for rewarping after map-loading is done. [Skotlex]
5608 sd->state.rewarp = 1;
5609
5610 sd->mapindex = mapindex;
5611 sd->bl.m = m;
5612 sd->bl.x = sd->ud.to_x = x;
5613 sd->bl.y = sd->ud.to_y = y;
5614
5615 if( sd->status.guild_id > 0 && mapdata->flag[MF_GVG_CASTLE] )
5616 { // Increased guild castle regen [Valaris]
5617 struct guild_castle *gc = guild_mapindex2gc(sd->mapindex);
5618 if(gc && gc->guild_id == sd->status.guild_id)
5619 sd->regen.state.gc = 1;
5620 }
5621
5622 if( sd->status.pet_id > 0 && sd->pd && sd->pd->pet.intimate > 0 )
5623 {
5624 sd->pd->bl.m = m;
5625 sd->pd->bl.x = sd->pd->ud.to_x = x;
5626 sd->pd->bl.y = sd->pd->ud.to_y = y;
5627 sd->pd->ud.dir = sd->ud.dir;
5628 }
5629
5630 if( hom_is_active(sd->hd) )
5631 {
5632 sd->hd->bl.m = m;
5633 sd->hd->bl.x = sd->hd->ud.to_x = x;
5634 sd->hd->bl.y = sd->hd->ud.to_y = y;
5635 sd->hd->ud.dir = sd->ud.dir;
5636 }
5637
5638 if( sd->md )
5639 {
5640 sd->md->bl.m = m;
5641 sd->md->bl.x = sd->md->ud.to_x = x;
5642 sd->md->bl.y = sd->md->ud.to_y = y;
5643 sd->md->ud.dir = sd->ud.dir;
5644 }
5645
5646 if( sd->ed ) {
5647 sd->ed->bl.m = m;
5648 sd->ed->bl.x = sd->ed->ud.to_x = x;
5649 sd->ed->bl.y = sd->ed->ud.to_y = y;
5650 sd->ed->ud.dir = sd->ud.dir;
5651 }
5652
5653 pc_cell_basilica(sd);
5654
5655 //check if we gonna be rewarped [lighta]
5656 if(npc_check_areanpc(1,m,x,y,0)){
5657 sd->count_rewarp++;
5658 }
5659 else
5660 sd->count_rewarp = 0;
5661
5662 return SETPOS_OK;
5663}
5664
5665/*==========================================
5666 * Warp player sd to random location on current map.
5667 * May fail if no walkable cell found (1000 attempts).
5668 * Return:
5669 * 0 = Success
5670 * 1,2,3 = Fail
5671 *------------------------------------------*/
5672char pc_randomwarp(struct map_session_data *sd, clr_type type)
5673{
5674 int x,y,i=0;
5675
5676 nullpo_ret(sd);
5677
5678 struct map_data *mapdata = map_getmapdata(sd->bl.m);
5679
5680 if (mapdata->flag[MF_NOTELEPORT]) //Teleport forbidden
5681 return 3;
5682
5683 do {
5684 x = rnd()%(mapdata->xs-2)+1;
5685 y = rnd()%(mapdata->ys-2)+1;
5686 } while((map_getcell(sd->bl.m,x,y,CELL_CHKNOPASS) || (!battle_config.teleport_on_portal && npc_check_areanpc(1,sd->bl.m,x,y,1))) && (i++) < 1000);
5687
5688 if (i < 1000)
5689 return pc_setpos(sd,mapdata->index,x,y,type);
5690
5691 return 3;
5692}
5693
5694/*==========================================
5695 * Records a memo point at sd's current position
5696 * pos - entry to replace, (-1: shift oldest entry out)
5697 *------------------------------------------*/
5698bool pc_memo(struct map_session_data* sd, int pos)
5699{
5700 int skill;
5701
5702 nullpo_ret(sd);
5703
5704 // check mapflags
5705 if( sd->bl.m >= 0 && (map_getmapflag(sd->bl.m, MF_NOMEMO) || map_getmapflag(sd->bl.m, MF_NOWARPTO)) && !pc_has_permission(sd, PC_PERM_WARP_ANYWHERE) ) {
5706 clif_skill_teleportmessage(sd, 1); // "Saved point cannot be memorized."
5707 return false;
5708 }
5709
5710 // check inputs
5711 if( pos < -1 || pos >= MAX_MEMOPOINTS )
5712 return false; // invalid input
5713
5714 // check required skill level
5715 skill = pc_checkskill(sd, AL_WARP);
5716 if( skill < 1 ) {
5717 clif_skill_memomessage(sd,2); // "You haven't learned Warp."
5718 return false;
5719 }
5720 if( skill < 2 || skill - 2 < pos ) {
5721 clif_skill_memomessage(sd,1); // "Skill Level is not high enough."
5722 return false;
5723 }
5724
5725 if( pos == -1 )
5726 {
5727 uint8 i;
5728 // prevent memo-ing the same map multiple times
5729 ARR_FIND( 0, MAX_MEMOPOINTS, i, sd->status.memo_point[i].map == map_id2index(sd->bl.m) );
5730 memmove(&sd->status.memo_point[1], &sd->status.memo_point[0], (u8min(i,MAX_MEMOPOINTS-1))*sizeof(struct point));
5731 pos = 0;
5732 }
5733
5734 if( map_getmapdata(sd->bl.m)->instance_id ) {
5735 clif_displaymessage( sd->fd, msg_txt(sd,384) ); // You cannot create a memo in an instance.
5736 return false;
5737 }
5738
5739 sd->status.memo_point[pos].map = map_id2index(sd->bl.m);
5740 sd->status.memo_point[pos].x = sd->bl.x;
5741 sd->status.memo_point[pos].y = sd->bl.y;
5742
5743 clif_skill_memomessage(sd, 0);
5744
5745 return true;
5746}
5747
5748//
5749// Skills
5750//
5751
5752/**
5753 * Get the skill current cooldown for player.
5754 * (get the db base cooldown for skill + player specific cooldown)
5755 * @param sd : player pointer
5756 * @param id : skill id
5757 * @param lv : skill lv
5758 * @return player skill cooldown
5759 */
5760int pc_get_skillcooldown(struct map_session_data *sd, uint16 skill_id, uint16 skill_lv) {
5761 uint8 i;
5762 uint16 idx = skill_get_index(skill_id);
5763 int cooldown = 0, cooldownlen = ARRAYLENGTH(sd->skillcooldown);
5764
5765 if (!idx) return 0;
5766 if (skill_db[idx]->cooldown[skill_lv - 1])
5767 cooldown = skill_db[idx]->cooldown[skill_lv - 1];
5768 if (skill_id == SU_TUNABELLY && pc_checkskill(sd, SU_SPIRITOFSEA))
5769 cooldown -= skill_get_time(SU_TUNABELLY, skill_lv);
5770
5771 ARR_FIND(0, cooldownlen, i, sd->skillcooldown[i].id == skill_id);
5772 if (i < cooldownlen) {
5773 cooldown += sd->skillcooldown[i].val;
5774 cooldown = max(0,cooldown);
5775 }
5776 return cooldown;
5777}
5778
5779/*==========================================
5780 * Return player sd skill_lv learned for given skill
5781 *------------------------------------------*/
5782uint8 pc_checkskill(struct map_session_data *sd, uint16 skill_id)
5783{
5784 uint16 idx = 0;
5785 if (sd == NULL)
5786 return 0;
5787 if ((idx = skill_get_index(skill_id)) == 0) {
5788 ShowError("pc_checkskill: Invalid skill id %d (char_id=%d).\n", skill_id, sd->status.char_id);
5789 return 0;
5790 }
5791 if (SKILL_CHK_GUILD(skill_id) ) {
5792 struct guild *g;
5793
5794 if( sd->status.guild_id>0 && (g=sd->guild)!=NULL)
5795 return guild_checkskill(g,skill_id);
5796 return 0;
5797 }
5798 return (sd->status.skill[idx].id == skill_id) ? sd->status.skill[idx].lv : 0;
5799}
5800
5801/**
5802 * Check if we still have the correct weapon to continue the skill (actually status)
5803 * If not ending it
5804 * @param sd
5805 * @return 0:error, 1:check done
5806 */
5807static void pc_checkallowskill(struct map_session_data *sd)
5808{
5809 const enum sc_type scw_list[] = {
5810 SC_TWOHANDQUICKEN,
5811 SC_ONEHAND,
5812 SC_AURABLADE,
5813 SC_PARRYING,
5814 SC_SPEARQUICKEN,
5815 SC_ADRENALINE,
5816 SC_ADRENALINE2,
5817 SC_DANCING,
5818 SC_GATLINGFEVER,
5819 };
5820 uint8 i;
5821 nullpo_retv(sd);
5822
5823 if(!sd->sc.count)
5824 return;
5825
5826 for (i = 0; i < ARRAYLENGTH(scw_list); i++)
5827 { // Skills requiring specific weapon types
5828 if( scw_list[i] == SC_DANCING && !battle_config.dancing_weaponswitch_fix )
5829 continue;
5830 if(sd->sc.data[scw_list[i]] &&
5831 !pc_check_weapontype(sd,skill_get_weapontype(status_sc2skill(scw_list[i]))))
5832 status_change_end(&sd->bl, scw_list[i], INVALID_TIMER);
5833 }
5834
5835 if(sd->sc.data[SC_SPURT] && sd->status.weapon)
5836 // Spurt requires bare hands (feet, in fact xD)
5837 status_change_end(&sd->bl, SC_SPURT, INVALID_TIMER);
5838
5839 if(sd->status.shield <= 0) { // Skills requiring a shield
5840 const enum sc_type scs_list[] = {
5841 SC_AUTOGUARD,
5842 SC_DEFENDER,
5843 SC_REFLECTSHIELD,
5844 SC_REFLECTDAMAGE
5845 };
5846 for (i = 0; i < ARRAYLENGTH(scs_list); i++)
5847 if(sd->sc.data[scs_list[i]])
5848 status_change_end(&sd->bl, scs_list[i], INVALID_TIMER);
5849 }
5850}
5851
5852/*==========================================
5853 * Return equipped index of item on player sd at pos
5854 * Return
5855 * -1 : Nothing equipped
5856 * idx : (this index could be used in inventory to found item_data)
5857 *------------------------------------------*/
5858short pc_checkequip(struct map_session_data *sd,int pos)
5859{
5860 uint8 i;
5861
5862 nullpo_retr(-1, sd);
5863
5864 for(i=0;i<EQI_MAX;i++){
5865 if(pos & equip_bitmask[i])
5866 return sd->equip_index[i];
5867 }
5868
5869 return -1;
5870}
5871
5872/*==========================================
5873 * Check if sd has nameid equipped somewhere
5874 * @sd : the player session
5875 * @nameid : id of the item to check
5876 * @min : : see pc.hpp enum equip_index from ? to @max
5877 * @max : see pc.hpp enum equip_index for @min to ?
5878 * -return true,false
5879 *------------------------------------------*/
5880bool pc_checkequip2(struct map_session_data *sd, unsigned short nameid, int min, int max)
5881{
5882 int i;
5883
5884 for(i = min; i < max; i++) {
5885 if(equip_bitmask[i]) {
5886 int idx = sd->equip_index[i];
5887
5888 if (sd->inventory.u.items_inventory[idx].nameid == nameid)
5889 return true;
5890 }
5891 }
5892 return false;
5893}
5894
5895/*==========================================
5896 * Convert's from the client's lame Job ID system
5897 * to the map server's 'makes sense' system. [Skotlex]
5898 *------------------------------------------*/
5899int pc_jobid2mapid(unsigned short b_class)
5900{
5901 switch(b_class)
5902 {
5903 //Novice And 1-1 Jobs
5904 case JOB_NOVICE: return MAPID_NOVICE;
5905 case JOB_SWORDMAN: return MAPID_SWORDMAN;
5906 case JOB_MAGE: return MAPID_MAGE;
5907 case JOB_ARCHER: return MAPID_ARCHER;
5908 case JOB_ACOLYTE: return MAPID_ACOLYTE;
5909 case JOB_MERCHANT: return MAPID_MERCHANT;
5910 case JOB_THIEF: return MAPID_THIEF;
5911 case JOB_TAEKWON: return MAPID_TAEKWON;
5912 case JOB_WEDDING: return MAPID_WEDDING;
5913 case JOB_GUNSLINGER: return MAPID_GUNSLINGER;
5914 case JOB_NINJA: return MAPID_NINJA;
5915 case JOB_XMAS: return MAPID_XMAS;
5916 case JOB_SUMMER: return MAPID_SUMMER;
5917 case JOB_HANBOK: return MAPID_HANBOK;
5918 case JOB_GANGSI: return MAPID_GANGSI;
5919 case JOB_OKTOBERFEST: return MAPID_OKTOBERFEST;
5920 case JOB_SUMMER2: return MAPID_SUMMER2;
5921 //2-1 Jobs
5922 case JOB_SUPER_NOVICE: return MAPID_SUPER_NOVICE;
5923 case JOB_KNIGHT: return MAPID_KNIGHT;
5924 case JOB_WIZARD: return MAPID_WIZARD;
5925 case JOB_HUNTER: return MAPID_HUNTER;
5926 case JOB_PRIEST: return MAPID_PRIEST;
5927 case JOB_BLACKSMITH: return MAPID_BLACKSMITH;
5928 case JOB_ASSASSIN: return MAPID_ASSASSIN;
5929 case JOB_STAR_GLADIATOR: return MAPID_STAR_GLADIATOR;
5930 case JOB_KAGEROU:
5931 case JOB_OBORO: return MAPID_KAGEROUOBORO;
5932 case JOB_REBELLION: return MAPID_REBELLION;
5933 case JOB_DEATH_KNIGHT: return MAPID_DEATH_KNIGHT;
5934 //2-2 Jobs
5935 case JOB_CRUSADER: return MAPID_CRUSADER;
5936 case JOB_SAGE: return MAPID_SAGE;
5937 case JOB_BARD:
5938 case JOB_DANCER: return MAPID_BARDDANCER;
5939 case JOB_MONK: return MAPID_MONK;
5940 case JOB_ALCHEMIST: return MAPID_ALCHEMIST;
5941 case JOB_ROGUE: return MAPID_ROGUE;
5942 case JOB_SOUL_LINKER: return MAPID_SOUL_LINKER;
5943 case JOB_DARK_COLLECTOR: return MAPID_DARK_COLLECTOR;
5944 //Trans Novice And Trans 1-1 Jobs
5945 case JOB_NOVICE_HIGH: return MAPID_NOVICE_HIGH;
5946 case JOB_SWORDMAN_HIGH: return MAPID_SWORDMAN_HIGH;
5947 case JOB_MAGE_HIGH: return MAPID_MAGE_HIGH;
5948 case JOB_ARCHER_HIGH: return MAPID_ARCHER_HIGH;
5949 case JOB_ACOLYTE_HIGH: return MAPID_ACOLYTE_HIGH;
5950 case JOB_MERCHANT_HIGH: return MAPID_MERCHANT_HIGH;
5951 case JOB_THIEF_HIGH: return MAPID_THIEF_HIGH;
5952 //Trans 2-1 Jobs
5953 case JOB_LORD_KNIGHT: return MAPID_LORD_KNIGHT;
5954 case JOB_HIGH_WIZARD: return MAPID_HIGH_WIZARD;
5955 case JOB_SNIPER: return MAPID_SNIPER;
5956 case JOB_HIGH_PRIEST: return MAPID_HIGH_PRIEST;
5957 case JOB_WHITESMITH: return MAPID_WHITESMITH;
5958 case JOB_ASSASSIN_CROSS: return MAPID_ASSASSIN_CROSS;
5959 //Trans 2-2 Jobs
5960 case JOB_PALADIN: return MAPID_PALADIN;
5961 case JOB_PROFESSOR: return MAPID_PROFESSOR;
5962 case JOB_CLOWN:
5963 case JOB_GYPSY: return MAPID_CLOWNGYPSY;
5964 case JOB_CHAMPION: return MAPID_CHAMPION;
5965 case JOB_CREATOR: return MAPID_CREATOR;
5966 case JOB_STALKER: return MAPID_STALKER;
5967 //Baby Novice And Baby 1-1 Jobs
5968 case JOB_BABY: return MAPID_BABY;
5969 case JOB_BABY_SWORDMAN: return MAPID_BABY_SWORDMAN;
5970 case JOB_BABY_MAGE: return MAPID_BABY_MAGE;
5971 case JOB_BABY_ARCHER: return MAPID_BABY_ARCHER;
5972 case JOB_BABY_ACOLYTE: return MAPID_BABY_ACOLYTE;
5973 case JOB_BABY_MERCHANT: return MAPID_BABY_MERCHANT;
5974 case JOB_BABY_THIEF: return MAPID_BABY_THIEF;
5975 case JOB_BABY_TAEKWON: return MAPID_BABY_TAEKWON;
5976 case JOB_BABY_GUNSLINGER: return MAPID_BABY_GUNSLINGER;
5977 case JOB_BABY_NINJA: return MAPID_BABY_NINJA;
5978 case JOB_BABY_SUMMONER: return MAPID_BABY_SUMMONER;
5979 //Baby 2-1 Jobs
5980 case JOB_SUPER_BABY: return MAPID_SUPER_BABY;
5981 case JOB_BABY_KNIGHT: return MAPID_BABY_KNIGHT;
5982 case JOB_BABY_WIZARD: return MAPID_BABY_WIZARD;
5983 case JOB_BABY_HUNTER: return MAPID_BABY_HUNTER;
5984 case JOB_BABY_PRIEST: return MAPID_BABY_PRIEST;
5985 case JOB_BABY_BLACKSMITH: return MAPID_BABY_BLACKSMITH;
5986 case JOB_BABY_ASSASSIN: return MAPID_BABY_ASSASSIN;
5987 case JOB_BABY_STAR_GLADIATOR: return MAPID_BABY_STAR_GLADIATOR;
5988 case JOB_BABY_REBELLION: return MAPID_BABY_REBELLION;
5989 case JOB_BABY_KAGEROU:
5990 case JOB_BABY_OBORO: return MAPID_BABY_KAGEROUOBORO;
5991 //Baby 2-2 Jobs
5992 case JOB_BABY_CRUSADER: return MAPID_BABY_CRUSADER;
5993 case JOB_BABY_SAGE: return MAPID_BABY_SAGE;
5994 case JOB_BABY_BARD:
5995 case JOB_BABY_DANCER: return MAPID_BABY_BARDDANCER;
5996 case JOB_BABY_MONK: return MAPID_BABY_MONK;
5997 case JOB_BABY_ALCHEMIST: return MAPID_BABY_ALCHEMIST;
5998 case JOB_BABY_ROGUE: return MAPID_BABY_ROGUE;
5999 case JOB_BABY_SOUL_LINKER: return MAPID_BABY_SOUL_LINKER;
6000 //3-1 Jobs
6001 case JOB_SUPER_NOVICE_E: return MAPID_SUPER_NOVICE_E;
6002 case JOB_RUNE_KNIGHT: return MAPID_RUNE_KNIGHT;
6003 case JOB_WARLOCK: return MAPID_WARLOCK;
6004 case JOB_RANGER: return MAPID_RANGER;
6005 case JOB_ARCH_BISHOP: return MAPID_ARCH_BISHOP;
6006 case JOB_MECHANIC: return MAPID_MECHANIC;
6007 case JOB_GUILLOTINE_CROSS: return MAPID_GUILLOTINE_CROSS;
6008 case JOB_STAR_EMPEROR: return MAPID_STAR_EMPEROR;
6009 //3-2 Jobs
6010 case JOB_ROYAL_GUARD: return MAPID_ROYAL_GUARD;
6011 case JOB_SORCERER: return MAPID_SORCERER;
6012 case JOB_MINSTREL:
6013 case JOB_WANDERER: return MAPID_MINSTRELWANDERER;
6014 case JOB_SURA: return MAPID_SURA;
6015 case JOB_GENETIC: return MAPID_GENETIC;
6016 case JOB_SHADOW_CHASER: return MAPID_SHADOW_CHASER;
6017 case JOB_SOUL_REAPER: return MAPID_SOUL_REAPER;
6018 //Trans 3-1 Jobs
6019 case JOB_RUNE_KNIGHT_T: return MAPID_RUNE_KNIGHT_T;
6020 case JOB_WARLOCK_T: return MAPID_WARLOCK_T;
6021 case JOB_RANGER_T: return MAPID_RANGER_T;
6022 case JOB_ARCH_BISHOP_T: return MAPID_ARCH_BISHOP_T;
6023 case JOB_MECHANIC_T: return MAPID_MECHANIC_T;
6024 case JOB_GUILLOTINE_CROSS_T: return MAPID_GUILLOTINE_CROSS_T;
6025 //Trans 3-2 Jobs
6026 case JOB_ROYAL_GUARD_T: return MAPID_ROYAL_GUARD_T;
6027 case JOB_SORCERER_T: return MAPID_SORCERER_T;
6028 case JOB_MINSTREL_T:
6029 case JOB_WANDERER_T: return MAPID_MINSTRELWANDERER_T;
6030 case JOB_SURA_T: return MAPID_SURA_T;
6031 case JOB_GENETIC_T: return MAPID_GENETIC_T;
6032 case JOB_SHADOW_CHASER_T: return MAPID_SHADOW_CHASER_T;
6033 //Baby 3-1 Jobs
6034 case JOB_SUPER_BABY_E: return MAPID_SUPER_BABY_E;
6035 case JOB_BABY_RUNE: return MAPID_BABY_RUNE;
6036 case JOB_BABY_WARLOCK: return MAPID_BABY_WARLOCK;
6037 case JOB_BABY_RANGER: return MAPID_BABY_RANGER;
6038 case JOB_BABY_BISHOP: return MAPID_BABY_BISHOP;
6039 case JOB_BABY_MECHANIC: return MAPID_BABY_MECHANIC;
6040 case JOB_BABY_CROSS: return MAPID_BABY_CROSS;
6041 case JOB_BABY_STAR_EMPEROR: return MAPID_BABY_STAR_EMPEROR;
6042 //Baby 3-2 Jobs
6043 case JOB_BABY_GUARD: return MAPID_BABY_GUARD;
6044 case JOB_BABY_SORCERER: return MAPID_BABY_SORCERER;
6045 case JOB_BABY_MINSTREL:
6046 case JOB_BABY_WANDERER: return MAPID_BABY_MINSTRELWANDERER;
6047 case JOB_BABY_SURA: return MAPID_BABY_SURA;
6048 case JOB_BABY_GENETIC: return MAPID_BABY_GENETIC;
6049 case JOB_BABY_CHASER: return MAPID_BABY_CHASER;
6050 case JOB_BABY_SOUL_REAPER: return MAPID_BABY_SOUL_REAPER;
6051 //Doram Jobs
6052 case JOB_SUMMONER: return MAPID_SUMMONER;
6053 default:
6054 return -1;
6055 }
6056}
6057
6058//Reverts the map-style class id to the client-style one.
6059int pc_mapid2jobid(unsigned short class_, int sex)
6060{
6061 switch(class_) {
6062 //Novice And 1-1 Jobs
6063 case MAPID_NOVICE: return JOB_NOVICE;
6064 case MAPID_SWORDMAN: return JOB_SWORDMAN;
6065 case MAPID_MAGE: return JOB_MAGE;
6066 case MAPID_ARCHER: return JOB_ARCHER;
6067 case MAPID_ACOLYTE: return JOB_ACOLYTE;
6068 case MAPID_MERCHANT: return JOB_MERCHANT;
6069 case MAPID_THIEF: return JOB_THIEF;
6070 case MAPID_TAEKWON: return JOB_TAEKWON;
6071 case MAPID_WEDDING: return JOB_WEDDING;
6072 case MAPID_GUNSLINGER: return JOB_GUNSLINGER;
6073 case MAPID_NINJA: return JOB_NINJA;
6074 case MAPID_XMAS: return JOB_XMAS;
6075 case MAPID_SUMMER: return JOB_SUMMER;
6076 case MAPID_HANBOK: return JOB_HANBOK;
6077 case MAPID_GANGSI: return JOB_GANGSI;
6078 case MAPID_OKTOBERFEST: return JOB_OKTOBERFEST;
6079 case MAPID_SUMMER2: return JOB_SUMMER2;
6080 //2-1 Jobs
6081 case MAPID_SUPER_NOVICE: return JOB_SUPER_NOVICE;
6082 case MAPID_KNIGHT: return JOB_KNIGHT;
6083 case MAPID_WIZARD: return JOB_WIZARD;
6084 case MAPID_HUNTER: return JOB_HUNTER;
6085 case MAPID_PRIEST: return JOB_PRIEST;
6086 case MAPID_BLACKSMITH: return JOB_BLACKSMITH;
6087 case MAPID_ASSASSIN: return JOB_ASSASSIN;
6088 case MAPID_STAR_GLADIATOR: return JOB_STAR_GLADIATOR;
6089 case MAPID_KAGEROUOBORO: return sex?JOB_KAGEROU:JOB_OBORO;
6090 case MAPID_REBELLION: return JOB_REBELLION;
6091 case MAPID_DEATH_KNIGHT: return JOB_DEATH_KNIGHT;
6092 //2-2 Jobs
6093 case MAPID_CRUSADER: return JOB_CRUSADER;
6094 case MAPID_SAGE: return JOB_SAGE;
6095 case MAPID_BARDDANCER: return sex?JOB_BARD:JOB_DANCER;
6096 case MAPID_MONK: return JOB_MONK;
6097 case MAPID_ALCHEMIST: return JOB_ALCHEMIST;
6098 case MAPID_ROGUE: return JOB_ROGUE;
6099 case MAPID_SOUL_LINKER: return JOB_SOUL_LINKER;
6100 case MAPID_DARK_COLLECTOR: return JOB_DARK_COLLECTOR;
6101 //Trans Novice And Trans 2-1 Jobs
6102 case MAPID_NOVICE_HIGH: return JOB_NOVICE_HIGH;
6103 case MAPID_SWORDMAN_HIGH: return JOB_SWORDMAN_HIGH;
6104 case MAPID_MAGE_HIGH: return JOB_MAGE_HIGH;
6105 case MAPID_ARCHER_HIGH: return JOB_ARCHER_HIGH;
6106 case MAPID_ACOLYTE_HIGH: return JOB_ACOLYTE_HIGH;
6107 case MAPID_MERCHANT_HIGH: return JOB_MERCHANT_HIGH;
6108 case MAPID_THIEF_HIGH: return JOB_THIEF_HIGH;
6109 //Trans 2-1 Jobs
6110 case MAPID_LORD_KNIGHT: return JOB_LORD_KNIGHT;
6111 case MAPID_HIGH_WIZARD: return JOB_HIGH_WIZARD;
6112 case MAPID_SNIPER: return JOB_SNIPER;
6113 case MAPID_HIGH_PRIEST: return JOB_HIGH_PRIEST;
6114 case MAPID_WHITESMITH: return JOB_WHITESMITH;
6115 case MAPID_ASSASSIN_CROSS: return JOB_ASSASSIN_CROSS;
6116 //Trans 2-2 Jobs
6117 case MAPID_PALADIN: return JOB_PALADIN;
6118 case MAPID_PROFESSOR: return JOB_PROFESSOR;
6119 case MAPID_CLOWNGYPSY: return sex?JOB_CLOWN:JOB_GYPSY;
6120 case MAPID_CHAMPION: return JOB_CHAMPION;
6121 case MAPID_CREATOR: return JOB_CREATOR;
6122 case MAPID_STALKER: return JOB_STALKER;
6123 //Baby Novice And Baby 1-1 Jobs
6124 case MAPID_BABY: return JOB_BABY;
6125 case MAPID_BABY_SWORDMAN: return JOB_BABY_SWORDMAN;
6126 case MAPID_BABY_MAGE: return JOB_BABY_MAGE;
6127 case MAPID_BABY_ARCHER: return JOB_BABY_ARCHER;
6128 case MAPID_BABY_ACOLYTE: return JOB_BABY_ACOLYTE;
6129 case MAPID_BABY_MERCHANT: return JOB_BABY_MERCHANT;
6130 case MAPID_BABY_THIEF: return JOB_BABY_THIEF;
6131 case MAPID_BABY_TAEKWON: return JOB_BABY_TAEKWON;
6132 case MAPID_BABY_GUNSLINGER: return JOB_BABY_GUNSLINGER;
6133 case MAPID_BABY_NINJA: return JOB_BABY_NINJA;
6134 case MAPID_BABY_SUMMONER: return JOB_BABY_SUMMONER;
6135 //Baby 2-1 Jobs
6136 case MAPID_SUPER_BABY: return JOB_SUPER_BABY;
6137 case MAPID_BABY_KNIGHT: return JOB_BABY_KNIGHT;
6138 case MAPID_BABY_WIZARD: return JOB_BABY_WIZARD;
6139 case MAPID_BABY_HUNTER: return JOB_BABY_HUNTER;
6140 case MAPID_BABY_PRIEST: return JOB_BABY_PRIEST;
6141 case MAPID_BABY_BLACKSMITH: return JOB_BABY_BLACKSMITH;
6142 case MAPID_BABY_ASSASSIN: return JOB_BABY_ASSASSIN;
6143 case MAPID_BABY_STAR_GLADIATOR: return JOB_BABY_STAR_GLADIATOR;
6144 case MAPID_BABY_REBELLION: return JOB_BABY_REBELLION;
6145 case MAPID_BABY_KAGEROUOBORO: return sex?JOB_BABY_KAGEROU:JOB_BABY_OBORO;
6146 //Baby 2-2 Jobs
6147 case MAPID_BABY_CRUSADER: return JOB_BABY_CRUSADER;
6148 case MAPID_BABY_SAGE: return JOB_BABY_SAGE;
6149 case MAPID_BABY_BARDDANCER: return sex?JOB_BABY_BARD:JOB_BABY_DANCER;
6150 case MAPID_BABY_MONK: return JOB_BABY_MONK;
6151 case MAPID_BABY_ALCHEMIST: return JOB_BABY_ALCHEMIST;
6152 case MAPID_BABY_ROGUE: return JOB_BABY_ROGUE;
6153 case MAPID_BABY_SOUL_LINKER: return JOB_BABY_SOUL_LINKER;
6154 //3-1 Jobs
6155 case MAPID_SUPER_NOVICE_E: return JOB_SUPER_NOVICE_E;
6156 case MAPID_RUNE_KNIGHT: return JOB_RUNE_KNIGHT;
6157 case MAPID_WARLOCK: return JOB_WARLOCK;
6158 case MAPID_RANGER: return JOB_RANGER;
6159 case MAPID_ARCH_BISHOP: return JOB_ARCH_BISHOP;
6160 case MAPID_MECHANIC: return JOB_MECHANIC;
6161 case MAPID_GUILLOTINE_CROSS: return JOB_GUILLOTINE_CROSS;
6162 case MAPID_STAR_EMPEROR: return JOB_STAR_EMPEROR;
6163 //3-2 Jobs
6164 case MAPID_ROYAL_GUARD: return JOB_ROYAL_GUARD;
6165 case MAPID_SORCERER: return JOB_SORCERER;
6166 case MAPID_MINSTRELWANDERER: return sex?JOB_MINSTREL:JOB_WANDERER;
6167 case MAPID_SURA: return JOB_SURA;
6168 case MAPID_GENETIC: return JOB_GENETIC;
6169 case MAPID_SHADOW_CHASER: return JOB_SHADOW_CHASER;
6170 case MAPID_SOUL_REAPER: return JOB_SOUL_REAPER;
6171 //Trans 3-1 Jobs
6172 case MAPID_RUNE_KNIGHT_T: return JOB_RUNE_KNIGHT_T;
6173 case MAPID_WARLOCK_T: return JOB_WARLOCK_T;
6174 case MAPID_RANGER_T: return JOB_RANGER_T;
6175 case MAPID_ARCH_BISHOP_T: return JOB_ARCH_BISHOP_T;
6176 case MAPID_MECHANIC_T: return JOB_MECHANIC_T;
6177 case MAPID_GUILLOTINE_CROSS_T: return JOB_GUILLOTINE_CROSS_T;
6178 //Trans 3-2 Jobs
6179 case MAPID_ROYAL_GUARD_T: return JOB_ROYAL_GUARD_T;
6180 case MAPID_SORCERER_T: return JOB_SORCERER_T;
6181 case MAPID_MINSTRELWANDERER_T: return sex?JOB_MINSTREL_T:JOB_WANDERER_T;
6182 case MAPID_SURA_T: return JOB_SURA_T;
6183 case MAPID_GENETIC_T: return JOB_GENETIC_T;
6184 case MAPID_SHADOW_CHASER_T: return JOB_SHADOW_CHASER_T;
6185 //Baby 3-1 Jobs
6186 case MAPID_SUPER_BABY_E: return JOB_SUPER_BABY_E;
6187 case MAPID_BABY_RUNE: return JOB_BABY_RUNE;
6188 case MAPID_BABY_WARLOCK: return JOB_BABY_WARLOCK;
6189 case MAPID_BABY_RANGER: return JOB_BABY_RANGER;
6190 case MAPID_BABY_BISHOP: return JOB_BABY_BISHOP;
6191 case MAPID_BABY_MECHANIC: return JOB_BABY_MECHANIC;
6192 case MAPID_BABY_CROSS: return JOB_BABY_CROSS;
6193 case MAPID_BABY_STAR_EMPEROR: return JOB_BABY_STAR_EMPEROR;
6194 //Baby 3-2 Jobs
6195 case MAPID_BABY_GUARD: return JOB_BABY_GUARD;
6196 case MAPID_BABY_SORCERER: return JOB_BABY_SORCERER;
6197 case MAPID_BABY_MINSTRELWANDERER: return sex?JOB_BABY_MINSTREL:JOB_BABY_WANDERER;
6198 case MAPID_BABY_SURA: return JOB_BABY_SURA;
6199 case MAPID_BABY_GENETIC: return JOB_BABY_GENETIC;
6200 case MAPID_BABY_CHASER: return JOB_BABY_CHASER;
6201 case MAPID_BABY_SOUL_REAPER: return JOB_BABY_SOUL_REAPER;
6202 //Doram Jobs
6203 case MAPID_SUMMONER: return JOB_SUMMONER;
6204 default:
6205 return -1;
6206 }
6207}
6208
6209/*====================================================
6210 * This function return the name of the job (by [Yor])
6211 *----------------------------------------------------*/
6212const char* job_name(int class_)
6213{
6214 switch (class_) {
6215 case JOB_NOVICE:
6216 case JOB_SWORDMAN:
6217 case JOB_MAGE:
6218 case JOB_ARCHER:
6219 case JOB_ACOLYTE:
6220 case JOB_MERCHANT:
6221 case JOB_THIEF:
6222 return msg_txt(NULL,550 - JOB_NOVICE+class_);
6223
6224 case JOB_KNIGHT:
6225 case JOB_PRIEST:
6226 case JOB_WIZARD:
6227 case JOB_BLACKSMITH:
6228 case JOB_HUNTER:
6229 case JOB_ASSASSIN:
6230 return msg_txt(NULL,557 - JOB_KNIGHT+class_);
6231
6232 case JOB_KNIGHT2:
6233 return msg_txt(NULL,557);
6234
6235 case JOB_CRUSADER:
6236 case JOB_MONK:
6237 case JOB_SAGE:
6238 case JOB_ROGUE:
6239 case JOB_ALCHEMIST:
6240 case JOB_BARD:
6241 case JOB_DANCER:
6242 return msg_txt(NULL,563 - JOB_CRUSADER+class_);
6243
6244 case JOB_CRUSADER2:
6245 return msg_txt(NULL,563);
6246
6247 case JOB_WEDDING:
6248 case JOB_SUPER_NOVICE:
6249 case JOB_GUNSLINGER:
6250 case JOB_NINJA:
6251 case JOB_XMAS:
6252 return msg_txt(NULL,570 - JOB_WEDDING+class_);
6253
6254 case JOB_SUMMER:
6255 case JOB_SUMMER2:
6256 return msg_txt(NULL,621);
6257
6258 case JOB_HANBOK:
6259 return msg_txt(NULL,694);
6260
6261 case JOB_OKTOBERFEST:
6262 return msg_txt(NULL,696);
6263
6264 case JOB_NOVICE_HIGH:
6265 case JOB_SWORDMAN_HIGH:
6266 case JOB_MAGE_HIGH:
6267 case JOB_ARCHER_HIGH:
6268 case JOB_ACOLYTE_HIGH:
6269 case JOB_MERCHANT_HIGH:
6270 case JOB_THIEF_HIGH:
6271 return msg_txt(NULL,575 - JOB_NOVICE_HIGH+class_);
6272
6273 case JOB_LORD_KNIGHT:
6274 case JOB_HIGH_PRIEST:
6275 case JOB_HIGH_WIZARD:
6276 case JOB_WHITESMITH:
6277 case JOB_SNIPER:
6278 case JOB_ASSASSIN_CROSS:
6279 return msg_txt(NULL,582 - JOB_LORD_KNIGHT+class_);
6280
6281 case JOB_LORD_KNIGHT2:
6282 return msg_txt(NULL,582);
6283
6284 case JOB_PALADIN:
6285 case JOB_CHAMPION:
6286 case JOB_PROFESSOR:
6287 case JOB_STALKER:
6288 case JOB_CREATOR:
6289 case JOB_CLOWN:
6290 case JOB_GYPSY:
6291 return msg_txt(NULL,588 - JOB_PALADIN + class_);
6292
6293 case JOB_PALADIN2:
6294 return msg_txt(NULL,588);
6295
6296 case JOB_BABY:
6297 case JOB_BABY_SWORDMAN:
6298 case JOB_BABY_MAGE:
6299 case JOB_BABY_ARCHER:
6300 case JOB_BABY_ACOLYTE:
6301 case JOB_BABY_MERCHANT:
6302 case JOB_BABY_THIEF:
6303 return msg_txt(NULL,595 - JOB_BABY + class_);
6304
6305 case JOB_BABY_KNIGHT:
6306 case JOB_BABY_PRIEST:
6307 case JOB_BABY_WIZARD:
6308 case JOB_BABY_BLACKSMITH:
6309 case JOB_BABY_HUNTER:
6310 case JOB_BABY_ASSASSIN:
6311 return msg_txt(NULL,602 - JOB_BABY_KNIGHT + class_);
6312
6313 case JOB_BABY_KNIGHT2:
6314 return msg_txt(NULL,602);
6315
6316 case JOB_BABY_CRUSADER:
6317 case JOB_BABY_MONK:
6318 case JOB_BABY_SAGE:
6319 case JOB_BABY_ROGUE:
6320 case JOB_BABY_ALCHEMIST:
6321 case JOB_BABY_BARD:
6322 case JOB_BABY_DANCER:
6323 return msg_txt(NULL,608 - JOB_BABY_CRUSADER + class_);
6324
6325 case JOB_BABY_CRUSADER2:
6326 return msg_txt(NULL,608);
6327
6328 case JOB_SUPER_BABY:
6329 return msg_txt(NULL,615);
6330
6331 case JOB_TAEKWON:
6332 return msg_txt(NULL,616);
6333 case JOB_STAR_GLADIATOR:
6334 case JOB_STAR_GLADIATOR2:
6335 return msg_txt(NULL,617);
6336 case JOB_SOUL_LINKER:
6337 return msg_txt(NULL,618);
6338
6339 case JOB_GANGSI:
6340 case JOB_DEATH_KNIGHT:
6341 case JOB_DARK_COLLECTOR:
6342 return msg_txt(NULL,622 - JOB_GANGSI+class_);
6343
6344 case JOB_RUNE_KNIGHT:
6345 case JOB_WARLOCK:
6346 case JOB_RANGER:
6347 case JOB_ARCH_BISHOP:
6348 case JOB_MECHANIC:
6349 case JOB_GUILLOTINE_CROSS:
6350 return msg_txt(NULL,625 - JOB_RUNE_KNIGHT+class_);
6351
6352 case JOB_RUNE_KNIGHT_T:
6353 case JOB_WARLOCK_T:
6354 case JOB_RANGER_T:
6355 case JOB_ARCH_BISHOP_T:
6356 case JOB_MECHANIC_T:
6357 case JOB_GUILLOTINE_CROSS_T:
6358 return msg_txt(NULL,681 - JOB_RUNE_KNIGHT_T+class_);
6359
6360 case JOB_ROYAL_GUARD:
6361 case JOB_SORCERER:
6362 case JOB_MINSTREL:
6363 case JOB_WANDERER:
6364 case JOB_SURA:
6365 case JOB_GENETIC:
6366 case JOB_SHADOW_CHASER:
6367 return msg_txt(NULL,631 - JOB_ROYAL_GUARD+class_);
6368
6369 case JOB_ROYAL_GUARD_T:
6370 case JOB_SORCERER_T:
6371 case JOB_MINSTREL_T:
6372 case JOB_WANDERER_T:
6373 case JOB_SURA_T:
6374 case JOB_GENETIC_T:
6375 case JOB_SHADOW_CHASER_T:
6376 return msg_txt(NULL,687 - JOB_ROYAL_GUARD_T+class_);
6377
6378 case JOB_RUNE_KNIGHT2:
6379 case JOB_RUNE_KNIGHT_T2:
6380 return msg_txt(NULL,625);
6381
6382 case JOB_ROYAL_GUARD2:
6383 case JOB_ROYAL_GUARD_T2:
6384 return msg_txt(NULL,631);
6385
6386 case JOB_RANGER2:
6387 case JOB_RANGER_T2:
6388 return msg_txt(NULL,627);
6389
6390 case JOB_MECHANIC2:
6391 case JOB_MECHANIC_T2:
6392 return msg_txt(NULL,629);
6393
6394 case JOB_BABY_RUNE:
6395 case JOB_BABY_WARLOCK:
6396 case JOB_BABY_RANGER:
6397 case JOB_BABY_BISHOP:
6398 case JOB_BABY_MECHANIC:
6399 case JOB_BABY_CROSS:
6400 case JOB_BABY_GUARD:
6401 case JOB_BABY_SORCERER:
6402 case JOB_BABY_MINSTREL:
6403 case JOB_BABY_WANDERER:
6404 case JOB_BABY_SURA:
6405 case JOB_BABY_GENETIC:
6406 case JOB_BABY_CHASER:
6407 return msg_txt(NULL,638 - JOB_BABY_RUNE+class_);
6408
6409 case JOB_BABY_RUNE2:
6410 return msg_txt(NULL,638);
6411
6412 case JOB_BABY_GUARD2:
6413 return msg_txt(NULL,644);
6414
6415 case JOB_BABY_RANGER2:
6416 return msg_txt(NULL,640);
6417
6418 case JOB_BABY_MECHANIC2:
6419 return msg_txt(NULL,642);
6420
6421 case JOB_SUPER_NOVICE_E:
6422 case JOB_SUPER_BABY_E:
6423 return msg_txt(NULL,651 - JOB_SUPER_NOVICE_E+class_);
6424
6425 case JOB_KAGEROU:
6426 case JOB_OBORO:
6427 return msg_txt(NULL,653 - JOB_KAGEROU+class_);
6428
6429 case JOB_REBELLION:
6430 return msg_txt(NULL,695);
6431
6432 case JOB_SUMMONER:
6433 return msg_txt(NULL,697);
6434 case JOB_BABY_SUMMONER:
6435 return msg_txt(NULL,698);
6436 case JOB_BABY_NINJA:
6437 return msg_txt(NULL,699);
6438
6439 case JOB_BABY_KAGEROU:
6440 case JOB_BABY_OBORO:
6441 case JOB_BABY_TAEKWON:
6442 case JOB_BABY_STAR_GLADIATOR:
6443 case JOB_BABY_SOUL_LINKER:
6444 case JOB_BABY_GUNSLINGER:
6445 case JOB_BABY_REBELLION:
6446 return msg_txt(NULL,753 - JOB_BABY_KAGEROU+class_);
6447
6448 case JOB_BABY_STAR_GLADIATOR2:
6449 return msg_txt(NULL,756);
6450
6451 case JOB_STAR_EMPEROR:
6452 case JOB_SOUL_REAPER:
6453 case JOB_BABY_STAR_EMPEROR:
6454 case JOB_BABY_SOUL_REAPER:
6455 return msg_txt(NULL,782 - JOB_STAR_EMPEROR + class_);
6456
6457 case JOB_STAR_EMPEROR2:
6458 return msg_txt(NULL,782);
6459
6460 case JOB_BABY_STAR_EMPEROR2:
6461 return msg_txt(NULL,784);
6462
6463 default:
6464 return msg_txt(NULL,655);
6465 }
6466}
6467
6468/*====================================================
6469 * Timered function to make id follow a target.
6470 * @id = bl.id (player only atm)
6471 * target is define in sd->followtarget (bl.id)
6472 * used by pc_follow
6473 *----------------------------------------------------*/
6474TIMER_FUNC(pc_follow_timer){
6475 struct map_session_data *sd;
6476 struct block_list *tbl;
6477
6478 sd = map_id2sd(id);
6479 nullpo_ret(sd);
6480
6481 if (sd->followtimer != tid){
6482 ShowError("pc_follow_timer %d != %d\n",sd->followtimer,tid);
6483 sd->followtimer = INVALID_TIMER;
6484 return 0;
6485 }
6486
6487 sd->followtimer = INVALID_TIMER;
6488 tbl = map_id2bl(sd->followtarget);
6489
6490 if (tbl == NULL || pc_isdead(sd))
6491 {
6492 pc_stop_following(sd);
6493 return 0;
6494 }
6495
6496 // either player or target is currently detached from map blocks (could be teleporting),
6497 // but still connected to this map, so we'll just increment the timer and check back later
6498 if (sd->bl.prev != NULL && tbl->prev != NULL &&
6499 sd->ud.skilltimer == INVALID_TIMER && sd->ud.attacktimer == INVALID_TIMER && sd->ud.walktimer == INVALID_TIMER)
6500 {
6501 if((sd->bl.m == tbl->m) && unit_can_reach_bl(&sd->bl,tbl, AREA_SIZE, 0, NULL, NULL)) {
6502 if (!check_distance_bl(&sd->bl, tbl, 5))
6503 unit_walktobl(&sd->bl, tbl, 5, 0);
6504 } else
6505 pc_setpos(sd, map_id2index(tbl->m), tbl->x, tbl->y, CLR_TELEPORT);
6506 }
6507 sd->followtimer = add_timer(
6508 tick + 1000, // increase time a bit to loosen up map's load
6509 pc_follow_timer, sd->bl.id, 0);
6510 return 0;
6511}
6512
6513int pc_stop_following (struct map_session_data *sd)
6514{
6515 nullpo_ret(sd);
6516
6517 if (sd->followtimer != INVALID_TIMER) {
6518 delete_timer(sd->followtimer,pc_follow_timer);
6519 sd->followtimer = INVALID_TIMER;
6520 }
6521 sd->followtarget = -1;
6522 sd->ud.target_to = 0;
6523
6524 unit_stop_walking(&sd->bl, 1);
6525
6526 return 0;
6527}
6528
6529int pc_follow(struct map_session_data *sd,int target_id)
6530{
6531 struct block_list *bl = map_id2bl(target_id);
6532 if (bl == NULL /*|| bl->type != BL_PC*/)
6533 return 1;
6534 if (sd->followtimer != INVALID_TIMER)
6535 pc_stop_following(sd);
6536
6537 sd->followtarget = target_id;
6538 pc_follow_timer(INVALID_TIMER, gettick(), sd->bl.id, 0);
6539
6540 return 0;
6541}
6542
6543int pc_checkbaselevelup(struct map_session_data *sd) {
6544 unsigned int next = pc_nextbaseexp(sd);
6545
6546 if (!next || sd->status.base_exp < next || pc_is_maxbaselv(sd))
6547 return 0;
6548
6549 do {
6550 sd->status.base_exp -= next;
6551 //Kyoki pointed out that the max overcarry exp is the exp needed for the previous level -1. [Skotlex]
6552 if(!battle_config.multi_level_up && sd->status.base_exp > next-1)
6553 sd->status.base_exp = next-1;
6554
6555 next = pc_gets_status_point(sd->status.base_level);
6556 sd->status.base_level++;
6557 sd->status.status_point += next;
6558
6559 if( pc_is_maxbaselv(sd) ){
6560 sd->status.base_exp = u32min(sd->status.base_exp,MAX_LEVEL_BASE_EXP);
6561 break;
6562 }
6563 } while ((next=pc_nextbaseexp(sd)) > 0 && sd->status.base_exp >= next);
6564
6565 if (battle_config.pet_lv_rate && sd->pd) //<Skotlex> update pet's level
6566 status_calc_pet(sd->pd,SCO_NONE);
6567
6568 clif_updatestatus(sd,SP_STATUSPOINT);
6569 clif_updatestatus(sd,SP_BASELEVEL);
6570 clif_updatestatus(sd,SP_BASEEXP);
6571 clif_updatestatus(sd,SP_NEXTBASEEXP);
6572 status_calc_pc(sd,SCO_FORCE);
6573 status_percent_heal(&sd->bl,100,100);
6574
6575 if ((sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE) {
6576 sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_KYRIE),100,1,skill_get_time(PR_KYRIE,1));
6577 sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_IMPOSITIO),100,1,skill_get_time(PR_IMPOSITIO,1));
6578 sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_MAGNIFICAT),100,1,skill_get_time(PR_MAGNIFICAT,1));
6579 sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_GLORIA),100,1,skill_get_time(PR_GLORIA,1));
6580 sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_SUFFRAGIUM),100,1,skill_get_time(PR_SUFFRAGIUM,1));
6581 if (sd->state.snovice_dead_flag)
6582 sd->state.snovice_dead_flag = 0; //Reenable steelbody resurrection on dead.
6583 } else if( (sd->class_&MAPID_BASEMASK) == MAPID_TAEKWON ) {
6584 sc_start(&sd->bl,&sd->bl,status_skill2sc(AL_INCAGI),100,10,600000);
6585 sc_start(&sd->bl,&sd->bl,status_skill2sc(AL_BLESSING),100,10,600000);
6586 }
6587 clif_misceffect(&sd->bl,0);
6588 npc_script_event(sd, NPCE_BASELVUP); //LORDALFA - LVLUPEVENT
6589
6590 if(sd->status.party_id)
6591 party_send_levelup(sd);
6592
6593 pc_baselevelchanged(sd);
6594 achievement_update_objective(sd, AG_GOAL_LEVEL, 1, sd->status.base_level);
6595 achievement_update_objective(sd, AG_GOAL_STATUS, 2, sd->status.base_level, sd->status.class_);
6596 return 1;
6597}
6598
6599void pc_baselevelchanged(struct map_session_data *sd) {
6600 uint8 i;
6601 for( i = 0; i < EQI_MAX; i++ ) {
6602 if( sd->equip_index[i] >= 0 ) {
6603 if( sd->inventory_data[ sd->equip_index[i] ]->elvmax && sd->status.base_level > (unsigned int)sd->inventory_data[ sd->equip_index[i] ]->elvmax )
6604 pc_unequipitem(sd, sd->equip_index[i], 3);
6605 }
6606 }
6607 pc_show_questinfo(sd);
6608}
6609
6610int pc_checkjoblevelup(struct map_session_data *sd)
6611{
6612 unsigned int next = pc_nextjobexp(sd);
6613
6614 nullpo_ret(sd);
6615 if(!next || sd->status.job_exp < next || pc_is_maxjoblv(sd))
6616 return 0;
6617
6618 do {
6619 sd->status.job_exp -= next;
6620 //Kyoki pointed out that the max overcarry exp is the exp needed for the previous level -1. [Skotlex]
6621 if(!battle_config.multi_level_up && sd->status.job_exp > next-1)
6622 sd->status.job_exp = next-1;
6623
6624 sd->status.job_level ++;
6625 sd->status.skill_point ++;
6626
6627 if( pc_is_maxjoblv(sd) ){
6628 sd->status.job_exp = u32min(sd->status.job_exp,MAX_LEVEL_JOB_EXP);
6629 break;
6630 }
6631 } while ((next=pc_nextjobexp(sd)) > 0 && sd->status.job_exp >= next);
6632
6633 clif_updatestatus(sd,SP_JOBLEVEL);
6634 clif_updatestatus(sd,SP_JOBEXP);
6635 clif_updatestatus(sd,SP_NEXTJOBEXP);
6636 clif_updatestatus(sd,SP_SKILLPOINT);
6637 status_calc_pc(sd,SCO_FORCE);
6638 clif_misceffect(&sd->bl,1);
6639 if (pc_checkskill(sd, SG_DEVIL) && pc_is_maxbaselv(sd))
6640 clif_status_change(&sd->bl, EFST_DEVIL1, 1, 0, 0, 0, 1); //Permanent blind effect from SG_DEVIL.
6641
6642 npc_script_event(sd, NPCE_JOBLVUP);
6643 achievement_update_objective(sd, AG_GOAL_LEVEL, 1, sd->status.job_level);
6644 return 1;
6645}
6646
6647/** Alters experiences calculation based on self bonuses that do not get even shared to the party.
6648* @param sd Player
6649* @param base_exp Base EXP before peronal bonuses
6650* @param job_exp Job EXP before peronal bonuses
6651* @param src Block list that affecting the exp calculation
6652*/
6653static void pc_calcexp(struct map_session_data *sd, unsigned int *base_exp, unsigned int *job_exp, struct block_list *src)
6654{
6655 int bonus = 0, vip_bonus_base = 0, vip_bonus_job = 0;
6656
6657 if (src) {
6658 struct status_data *status = status_get_status_data(src);
6659
6660 if( sd->expaddrace[status->race] )
6661 bonus += sd->expaddrace[status->race];
6662 if( sd->expaddrace[RC_ALL] )
6663 bonus += sd->expaddrace[RC_ALL];
6664 if( sd->expaddclass[status->class_] )
6665 bonus += sd->expaddclass[status->class_];
6666 if( sd->expaddclass[CLASS_ALL] )
6667 bonus += sd->expaddclass[CLASS_ALL];
6668
6669 if (battle_config.pk_mode &&
6670 (int)(status_get_lv(src) - sd->status.base_level) >= 20)
6671 bonus += 15; // pk_mode additional exp if monster >20 levels [Valaris]
6672
6673 if (src && src->type == BL_MOB && pc_isvip(sd)) { // EXP bonus for VIP player
6674 vip_bonus_base = battle_config.vip_base_exp_increase;
6675 vip_bonus_job = battle_config.vip_job_exp_increase;
6676 }
6677 }
6678
6679 // Give EXPBOOST for quests even if src is NULL.
6680 if (sd->sc.data[SC_EXPBOOST]) {
6681 bonus += sd->sc.data[SC_EXPBOOST]->val1;
6682 if (battle_config.vip_bm_increase && pc_isvip(sd)) // Increase Battle Manual EXP rate for VIP
6683 bonus += (sd->sc.data[SC_EXPBOOST]->val1 / battle_config.vip_bm_increase);
6684 }
6685
6686 if (*base_exp) {
6687 unsigned int exp = (unsigned int)(*base_exp + (double)*base_exp * (bonus + vip_bonus_base)/100.);
6688 *base_exp = cap_value(exp, 1, UINT_MAX);
6689 }
6690
6691 // Give JEXPBOOST for quests even if src is NULL.
6692 if (sd->sc.data[SC_JEXPBOOST])
6693 bonus += sd->sc.data[SC_JEXPBOOST]->val1;
6694
6695 if (*job_exp) {
6696 unsigned int exp = (unsigned int)(*job_exp + (double)*job_exp * (bonus + vip_bonus_job)/100.);
6697 *job_exp = cap_value(exp, 1, UINT_MAX);
6698 }
6699
6700 return;
6701}
6702
6703/**
6704 * Show EXP gained by player in percentage by @showexp
6705 * @param sd Player
6706 * @param base_exp Base EXP gained/loss
6707 * @param next_base_exp Base EXP needed for next base level
6708 * @param job_exp Job EXP gained/loss
6709 * @param next_job_exp Job EXP needed for next job level
6710 * @param lost True:EXP penalty, lose EXP
6711 **/
6712void pc_gainexp_disp(struct map_session_data *sd, unsigned int base_exp, unsigned int next_base_exp, unsigned int job_exp, unsigned int next_job_exp, bool lost) {
6713 char output[CHAT_SIZE_MAX];
6714
6715 nullpo_retv(sd);
6716
6717 sprintf(output, msg_txt(sd,743), // Experience %s Base:%ld (%0.2f%%) Job:%ld (%0.2f%%)
6718 (lost) ? msg_txt(sd,742) : msg_txt(sd,741),
6719 (long)base_exp * (lost ? -1 : 1), (base_exp / (float)next_base_exp * 100 * (lost ? -1 : 1)),
6720 (long)job_exp * (lost ? -1 : 1), (job_exp / (float)next_job_exp * 100 * (lost ? -1 : 1)));
6721 clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
6722}
6723
6724/**
6725 * Give Base or Job EXP to player, then calculate remaining exp for next lvl
6726 * @param sd Player
6727 * @param src EXP source
6728 * @param base_exp Base EXP gained
6729 * @param base_exp Job EXP gained
6730 * @param exp_flag 1: Quest EXP; 2: Param Exp (Ignore Guild EXP tax, EXP adjustments)
6731 * @return
6732 **/
6733void pc_gainexp(struct map_session_data *sd, struct block_list *src, unsigned int base_exp, unsigned int job_exp, uint8 exp_flag)
6734{
6735 unsigned int nextb = 0, nextj = 0;
6736 uint8 flag = 0; ///< 1: Base EXP given, 2: Job EXP given, 4: Max Base level, 8: Max Job Level
6737
6738 nullpo_retv(sd);
6739
6740 if(sd->bl.prev == NULL || pc_isdead(sd))
6741 return;
6742
6743 if (!(exp_flag&2)) {
6744
6745 if (!battle_config.pvp_exp && map_getmapflag(sd->bl.m, MF_PVP)) // [MouseJstr]
6746 return; // no exp on pvp maps
6747
6748 if (sd->status.guild_id>0)
6749 base_exp -= guild_payexp(sd,base_exp);
6750 }
6751
6752 flag = ((base_exp) ? 1 : 0) |
6753 ((job_exp) ? 2 : 0) |
6754 ((pc_is_maxbaselv(sd)) ? 4 : 0) |
6755 ((pc_is_maxjoblv(sd)) ? 8 : 0);
6756
6757 if (!(exp_flag&2))
6758 pc_calcexp(sd, &base_exp, &job_exp, src);
6759
6760 nextb = pc_nextbaseexp(sd);
6761 nextj = pc_nextjobexp(sd);
6762
6763 if (flag&4){
6764 if( sd->status.base_exp >= MAX_LEVEL_BASE_EXP )
6765 base_exp = 0;
6766 else if( sd->status.base_exp + base_exp >= MAX_LEVEL_BASE_EXP )
6767 base_exp = MAX_LEVEL_BASE_EXP - sd->status.base_exp;
6768 }
6769 if (flag&8){
6770 if( sd->status.job_exp >= MAX_LEVEL_JOB_EXP )
6771 job_exp = 0;
6772 else if( sd->status.job_exp + job_exp >= MAX_LEVEL_JOB_EXP )
6773 job_exp = MAX_LEVEL_JOB_EXP - sd->status.job_exp;
6774 }
6775
6776 if (!(exp_flag&2) && battle_config.max_exp_gain_rate && (base_exp || job_exp)) {
6777 //Note that this value should never be greater than the original
6778 //therefore no overflow checks are needed. [Skotlex]
6779 if (nextb > 0) {
6780 float nextbp = (float) base_exp / (float) nextb;
6781 if (nextbp > battle_config.max_exp_gain_rate/1000.)
6782 base_exp = (unsigned int)(battle_config.max_exp_gain_rate/1000.*nextb);
6783 }
6784 if (nextj > 0) {
6785 float nextjp = (float) job_exp / (float) nextj;
6786 if (nextjp > battle_config.max_exp_gain_rate/1000.)
6787 job_exp = (unsigned int)(battle_config.max_exp_gain_rate/1000.*nextj);
6788 }
6789 }
6790
6791 // Give EXP for Base Level
6792 if (base_exp) {
6793 if ((uint64)sd->status.base_exp + base_exp > UINT32_MAX)
6794 sd->status.base_exp = UINT32_MAX;
6795 else
6796 sd->status.base_exp += base_exp;
6797 if (!pc_checkbaselevelup(sd))
6798 clif_updatestatus(sd,SP_BASEEXP);
6799 }
6800
6801 // Give EXP for Job Level
6802 if (job_exp) {
6803 if ((uint64)sd->status.job_exp + job_exp > UINT32_MAX)
6804 sd->status.job_exp = UINT32_MAX;
6805 else
6806 sd->status.job_exp += job_exp;
6807 if (!pc_checkjoblevelup(sd))
6808 clif_updatestatus(sd,SP_JOBEXP);
6809 }
6810
6811 if (flag&1)
6812 clif_displayexp(sd, (flag&4) ? 0 : base_exp, SP_BASEEXP, exp_flag&1, false);
6813 if (flag&2)
6814 clif_displayexp(sd, (flag&8) ? 0 : job_exp, SP_JOBEXP, exp_flag&1, false);
6815
6816 if (sd->state.showexp && (base_exp || job_exp))
6817 pc_gainexp_disp(sd, base_exp, nextb, job_exp, nextj, false);
6818}
6819
6820/**
6821 * Lost Base/Job EXP from a player
6822 * @param sd Player
6823 * @param base_exp Base EXP lost
6824 * @param job_exp Job EXP lost
6825 **/
6826void pc_lostexp(struct map_session_data *sd, unsigned int base_exp, unsigned int job_exp) {
6827
6828 nullpo_retv(sd);
6829
6830 if (base_exp) {
6831 base_exp = u32min(sd->status.base_exp, base_exp);
6832 sd->status.base_exp -= base_exp;
6833 clif_displayexp(sd, base_exp, SP_BASEEXP, false, true);
6834 clif_updatestatus(sd, SP_BASEEXP);
6835 }
6836
6837 if (job_exp) {
6838 job_exp = u32min(sd->status.job_exp, job_exp);
6839 sd->status.job_exp -= job_exp;
6840 clif_displayexp(sd, job_exp, SP_JOBEXP, false, true);
6841 clif_updatestatus(sd, SP_JOBEXP);
6842 }
6843
6844 if (sd->state.showexp && (base_exp || job_exp))
6845 pc_gainexp_disp(sd, base_exp, pc_nextbaseexp(sd), job_exp, pc_nextjobexp(sd), true);
6846}
6847
6848/**
6849 * Returns max base level for this character's class.
6850 * @param class_: Player's class
6851 * @return Max Base Level
6852 */
6853static unsigned int pc_class_maxbaselv(unsigned short class_) {
6854 return job_info[pc_class2idx(class_)].max_level[0];
6855}
6856
6857/**
6858 * Returns max base level for this character.
6859 * @param sd Player
6860 * @return Max Base Level
6861 **/
6862unsigned int pc_maxbaselv(struct map_session_data *sd){
6863 return pc_class_maxbaselv(sd->status.class_);
6864}
6865
6866/**
6867 * Returns max job level for this character's class.
6868 * @param class_: Player's class
6869 * @return Max Job Level
6870 */
6871static unsigned int pc_class_maxjoblv(unsigned short class_) {
6872 return job_info[pc_class2idx(class_)].max_level[1];
6873}
6874
6875/**
6876 * Returns max job level for this character.
6877 * @param sd Player
6878 * @return Max Job Level
6879 **/
6880unsigned int pc_maxjoblv(struct map_session_data *sd){
6881 return pc_class_maxjoblv(sd->status.class_);
6882}
6883
6884/**
6885 * Check if player is reached max base level
6886 * @param sd
6887 * @return True if reached max level
6888 **/
6889bool pc_is_maxbaselv(struct map_session_data *sd) {
6890 nullpo_retr(false, sd);
6891 return (sd->status.base_level >= pc_maxbaselv(sd));
6892}
6893
6894/**
6895 * Check if player is reached max base level
6896 * @param sd
6897 * @return True if reached max level
6898 **/
6899bool pc_is_maxjoblv(struct map_session_data *sd) {
6900 nullpo_retr(false, sd);
6901 return (sd->status.job_level >= pc_maxjoblv(sd));
6902}
6903
6904/**
6905 * Base exp needed for player to level up.
6906 * @param sd
6907 * @return Base EXP needed for next base level
6908 **/
6909unsigned int pc_nextbaseexp(struct map_session_data *sd){
6910 nullpo_ret(sd);
6911 if (sd->status.base_level == 0) // Is this something that possible?
6912 return 0;
6913 if (pc_is_maxbaselv(sd))
6914 return MAX_LEVEL_BASE_EXP; // On max level, player's base EXP limit is 99,999,999
6915 return job_info[pc_class2idx(sd->status.class_)].exp_table[0][sd->status.base_level-1];
6916}
6917
6918/**
6919 * Job exp needed for player to level up.
6920 * @param sd
6921 * @return Job EXP needed for next job level
6922 **/
6923unsigned int pc_nextjobexp(struct map_session_data *sd){
6924 nullpo_ret(sd);
6925 if (sd->status.job_level == 0) // Is this something that possible?
6926 return 0;
6927 if (pc_is_maxjoblv(sd))
6928 return MAX_LEVEL_JOB_EXP; // On max level, player's job EXP limit is 999,999,999
6929 return job_info[pc_class2idx(sd->status.class_)].exp_table[1][sd->status.job_level-1];
6930}
6931
6932/// Returns the value of the specified stat.
6933static int pc_getstat(struct map_session_data* sd, int type)
6934{
6935 nullpo_retr(-1, sd);
6936
6937 switch( type ) {
6938 case SP_STR: return sd->status.str;
6939 case SP_AGI: return sd->status.agi;
6940 case SP_VIT: return sd->status.vit;
6941 case SP_INT: return sd->status.int_;
6942 case SP_DEX: return sd->status.dex;
6943 case SP_LUK: return sd->status.luk;
6944 default:
6945 return -1;
6946 }
6947}
6948
6949/// Sets the specified stat to the specified value.
6950/// Returns the new value.
6951static int pc_setstat(struct map_session_data* sd, int type, int val)
6952{
6953 nullpo_retr(-1, sd);
6954
6955 switch( type ) {
6956 case SP_STR: sd->status.str = val; break;
6957 case SP_AGI: sd->status.agi = val; break;
6958 case SP_VIT: sd->status.vit = val; break;
6959 case SP_INT: sd->status.int_ = val; break;
6960 case SP_DEX: sd->status.dex = val; break;
6961 case SP_LUK: sd->status.luk = val; break;
6962 default:
6963 return -1;
6964 }
6965
6966 return val;
6967}
6968
6969// Calculates the number of status points PC gets when leveling up (from level to level+1)
6970int pc_gets_status_point(int level)
6971{
6972 if (battle_config.use_statpoint_table) //Use values from "db/statpoint.txt"
6973 return (statp[level+1] - statp[level]);
6974 else //Default increase
6975 return ((level+15) / 5);
6976}
6977
6978#ifdef RENEWAL_STAT
6979/// Renewal status point cost formula
6980#define PC_STATUS_POINT_COST(low) (((low) < 100) ? (2 + ((low) - 1) / 10) : (16 + 4 * (((low) - 100) / 5)))
6981#else
6982/// Pre-Renewal status point cost formula
6983#define PC_STATUS_POINT_COST(low) (( 1 + ((low) + 9) / 10 ))
6984#endif
6985
6986/// Returns the number of stat points needed to change the specified stat by val.
6987/// If val is negative, returns the number of stat points that would be needed to
6988/// raise the specified stat from (current value - val) to current value.
6989int pc_need_status_point(struct map_session_data* sd, int type, int val)
6990{
6991 int low, high, sp = 0, max = 0;
6992
6993 if ( val == 0 )
6994 return 0;
6995
6996 low = pc_getstat(sd,type);
6997 max = pc_maxparameter(sd,(enum e_params)(type-SP_STR));
6998
6999 if ( low >= max && val > 0 )
7000 return 0; // Official servers show '0' when max is reached
7001
7002 high = low + val;
7003
7004 if ( val < 0 )
7005 SWAP(low, high);
7006
7007 for ( ; low < high; low++ )
7008 sp += PC_STATUS_POINT_COST(low);
7009
7010 return sp;
7011}
7012
7013/**
7014 * Returns the value the specified stat can be increased by with the current
7015 * amount of available status points for the current character's class.
7016 *
7017 * @param sd The target character.
7018 * @param type Stat to verify.
7019 * @return Maximum value the stat could grow by.
7020 */
7021int pc_maxparameterincrease(struct map_session_data* sd, int type)
7022{
7023 int base, final_val, status_points, max_param;
7024
7025 nullpo_ret(sd);
7026
7027 base = final_val = pc_getstat(sd, type);
7028 status_points = sd->status.status_point;
7029 max_param = pc_maxparameter(sd, (enum e_params)(type-SP_STR));
7030
7031 while (final_val <= max_param && status_points >= 0) {
7032 status_points -= PC_STATUS_POINT_COST(final_val);
7033 final_val++;
7034 }
7035 final_val--;
7036
7037 return (final_val > base ? final_val-base : 0);
7038}
7039
7040/**
7041 * Raises a stat by the specified amount.
7042 *
7043 * Obeys max_parameter limits.
7044 * Subtracts status points according to the cost of the increased stat points.
7045 *
7046 * @param sd The target character.
7047 * @param type The stat to change (see enum _sp)
7048 * @param increase The stat increase (strictly positive) amount.
7049 * @retval true if the stat was increased by any amount.
7050 * @retval false if there were no changes.
7051 */
7052bool pc_statusup(struct map_session_data* sd, int type, int increase)
7053{
7054 int max_increase = 0, current = 0, needed_points = 0, final_value = 0;
7055
7056 nullpo_ret(sd);
7057
7058 // check conditions
7059 if (type < SP_STR || type > SP_LUK || increase <= 0) {
7060 clif_statusupack(sd, type, 0, 0);
7061 return false;
7062 }
7063
7064 // check limits
7065 current = pc_getstat(sd, type);
7066 max_increase = pc_maxparameterincrease(sd, type);
7067 increase = cap_value(increase, 0, max_increase); // cap to the maximum status points available
7068 if (increase <= 0 || current + increase > pc_maxparameter(sd, (enum e_params)(type-SP_STR))) {
7069 clif_statusupack(sd, type, 0, 0);
7070 return false;
7071 }
7072
7073 // check status points
7074 needed_points = pc_need_status_point(sd, type, increase);
7075 if (needed_points < 0 || needed_points > sd->status.status_point) { // Sanity check
7076 clif_statusupack(sd, type, 0, 0);
7077 return false;
7078 }
7079
7080 // set new values
7081 final_value = pc_setstat(sd, type, current + increase);
7082 sd->status.status_point -= needed_points;
7083
7084 status_calc_pc(sd,SCO_NONE);
7085
7086 // update increase cost indicator
7087 clif_updatestatus(sd, SP_USTR + type-SP_STR);
7088
7089 // update statpoint count
7090 clif_updatestatus(sd, SP_STATUSPOINT);
7091
7092 // update stat value
7093 clif_statusupack(sd, type, 1, final_value); // required
7094 if( final_value > 255 )
7095 clif_updatestatus(sd, type); // send after the 'ack' to override the truncated value
7096
7097 achievement_update_objective(sd, AG_GOAL_STATUS, 1, final_value);
7098
7099 return true;
7100}
7101
7102/**
7103 * Raises a stat by the specified amount.
7104 *
7105 * Obeys max_parameter limits.
7106 * Does not subtract status points for the cost of the modified stat points.
7107 *
7108 * @param sd The target character.
7109 * @param type The stat to change (see enum _sp)
7110 * @param val The stat increase (or decrease) amount.
7111 * @return the stat increase amount.
7112 * @retval 0 if no changes were made.
7113 */
7114int pc_statusup2(struct map_session_data* sd, int type, int val)
7115{
7116 int max, need;
7117 nullpo_ret(sd);
7118
7119 if( type < SP_STR || type > SP_LUK )
7120 {
7121 clif_statusupack(sd,type,0,0);
7122 return 0;
7123 }
7124
7125 need = pc_need_status_point(sd,type,1);
7126 max = pc_maxparameter(sd,(enum e_params)(type-SP_STR)); // set new value
7127
7128 val = pc_setstat(sd, type, cap_value(pc_getstat(sd,type) + val, 1, max));
7129
7130 status_calc_pc(sd,SCO_NONE);
7131
7132 // update increase cost indicator
7133 if( need != pc_need_status_point(sd,type,1) )
7134 clif_updatestatus(sd, SP_USTR + type-SP_STR);
7135
7136 // update stat value
7137 clif_statusupack(sd,type,1,val); // required
7138 if( val > 255 )
7139 clif_updatestatus(sd,type); // send after the 'ack' to override the truncated value
7140
7141 return val;
7142}
7143
7144/*==========================================
7145 * Update skill_lv for player sd
7146 * Skill point allocation
7147 *------------------------------------------*/
7148void pc_skillup(struct map_session_data *sd,uint16 skill_id)
7149{
7150 uint16 idx = skill_get_index(skill_id);
7151
7152 nullpo_retv(sd);
7153
7154 if (!idx) {
7155 if (skill_id)
7156 ShowError("pc_skillup: Player attempts to level up invalid skill '%d'\n", skill_id);
7157 return;
7158 }
7159
7160 // Level up guild skill
7161 if (SKILL_CHK_GUILD(skill_id)) {
7162 guild_skillup(sd, skill_id);
7163 return;
7164 }
7165 // Level up homunculus skill
7166 else if (sd->hd && SKILL_CHK_HOMUN(skill_id)) {
7167 hom_skillup(sd->hd, skill_id);
7168 return;
7169 }
7170 else {
7171 if( sd->status.skill_point > 0 &&
7172 sd->status.skill[idx].id &&
7173 sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT && //Don't allow raising while you have granted skills. [Skotlex]
7174 sd->status.skill[idx].lv < skill_tree_get_max(skill_id, sd->status.class_) )
7175 {
7176 int lv, range, upgradable;
7177 sd->status.skill[idx].lv++;
7178 sd->status.skill_point--;
7179 if( !skill_get_inf(skill_id) )
7180 status_calc_pc(sd,SCO_NONE); // Only recalculate for passive skills.
7181 else if( sd->status.skill_point == 0 && pc_is_taekwon_ranker(sd) )
7182 pc_calc_skilltree(sd); // Required to grant all TK Ranker skills.
7183 else
7184 pc_check_skilltree(sd); // Check if a new skill can Lvlup
7185
7186 lv = sd->status.skill[idx].lv;
7187 range = skill_get_range2(&sd->bl, skill_id, lv, false);
7188 upgradable = (lv < skill_tree_get_max(sd->status.skill[idx].id, sd->status.class_)) ? 1 : 0;
7189 clif_skillup(sd,skill_id,lv,range,upgradable);
7190 clif_updatestatus(sd,SP_SKILLPOINT);
7191 if( skill_id == GN_REMODELING_CART ) /* cart weight info was updated by status_calc_pc */
7192 clif_updatestatus(sd,SP_CARTINFO);
7193 if (!pc_has_permission(sd, PC_PERM_ALL_SKILL)) // may skill everything at any time anyways, and this would cause a huge slowdown
7194 clif_skillinfoblock(sd);
7195 }
7196 //else
7197 // ShowDebug("Skill Level up failed. ID:%d idx:%d (CID=%d. AID=%d)\n", skill_id, idx, sd->status.char_id, sd->status.account_id);
7198 }
7199}
7200
7201/*==========================================
7202 * /allskill
7203 *------------------------------------------*/
7204int pc_allskillup(struct map_session_data *sd)
7205{
7206 int i;
7207
7208 nullpo_ret(sd);
7209
7210 for (i = 0; i < MAX_SKILL; i++) {
7211 if (sd->status.skill[i].flag != SKILL_FLAG_PERMANENT && sd->status.skill[i].flag != SKILL_FLAG_PERM_GRANTED && sd->status.skill[i].flag != SKILL_FLAG_PLAGIARIZED) {
7212 sd->status.skill[i].lv = (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY) ? 0 : sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
7213 sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
7214 if (sd->status.skill[i].lv == 0)
7215 sd->status.skill[i].id = 0;
7216 }
7217 }
7218
7219 if (!pc_grant_allskills(sd, true)) {
7220 uint16 sk_id;
7221 for (i = 0; i < MAX_SKILL_TREE && (sk_id = skill_tree[pc_class2idx(sd->status.class_)][i].skill_id) > 0;i++){
7222 int inf2 = 0;
7223 uint16 sk_idx = 0;
7224 if (!sk_id || !(sk_idx = skill_get_index(sk_id)))
7225 continue;
7226 inf2 = skill_get_inf2(sk_id);
7227 if (
7228 (inf2&INF2_QUEST_SKILL && !battle_config.quest_skill_learn) ||
7229 (inf2&(INF2_WEDDING_SKILL|INF2_SPIRIT_SKILL)) ||
7230 sk_id == SG_DEVIL
7231 )
7232 continue; //Cannot be learned normally.
7233
7234 sd->status.skill[sk_idx].id = sk_id;
7235 sd->status.skill[sk_idx].lv = skill_tree_get_max(sk_id, sd->status.class_); // celest
7236 }
7237 }
7238 status_calc_pc(sd,SCO_NONE);
7239 //Required because if you could level up all skills previously,
7240 //the update will not be sent as only the lv variable changes.
7241 clif_skillinfoblock(sd);
7242 return 0;
7243}
7244
7245/*==========================================
7246 * /resetlvl
7247 *------------------------------------------*/
7248int pc_resetlvl(struct map_session_data* sd,int type)
7249{
7250 int i;
7251
7252 nullpo_ret(sd);
7253
7254 if (type != 3) //Also reset skills
7255 pc_resetskill(sd, 0);
7256
7257 if(type == 1){
7258 sd->status.skill_point=0;
7259 sd->status.base_level=1;
7260 sd->status.job_level=1;
7261 sd->status.base_exp=0;
7262 sd->status.job_exp=0;
7263 if(sd->sc.option !=0)
7264 sd->sc.option = 0;
7265
7266 sd->status.str=1;
7267 sd->status.agi=1;
7268 sd->status.vit=1;
7269 sd->status.int_=1;
7270 sd->status.dex=1;
7271 sd->status.luk=1;
7272 if(sd->status.class_ == JOB_NOVICE_HIGH) {
7273 sd->status.status_point=100; // not 88 [celest]
7274 // give platinum skills upon changing
7275 pc_skill(sd,NV_FIRSTAID,1,ADDSKILL_PERMANENT);
7276 pc_skill(sd,NV_TRICKDEAD,1,ADDSKILL_PERMANENT);
7277 }
7278 }
7279
7280 if(type == 2){
7281 sd->status.skill_point=0;
7282 sd->status.base_level=1;
7283 sd->status.job_level=1;
7284 sd->status.base_exp=0;
7285 sd->status.job_exp=0;
7286 }
7287 if(type == 3){
7288 sd->status.base_level=1;
7289 sd->status.base_exp=0;
7290 }
7291 if(type == 4){
7292 sd->status.job_level=1;
7293 sd->status.job_exp=0;
7294 }
7295
7296 clif_updatestatus(sd,SP_STATUSPOINT);
7297 clif_updatestatus(sd,SP_STR);
7298 clif_updatestatus(sd,SP_AGI);
7299 clif_updatestatus(sd,SP_VIT);
7300 clif_updatestatus(sd,SP_INT);
7301 clif_updatestatus(sd,SP_DEX);
7302 clif_updatestatus(sd,SP_LUK);
7303 clif_updatestatus(sd,SP_BASELEVEL);
7304 clif_updatestatus(sd,SP_JOBLEVEL);
7305 clif_updatestatus(sd,SP_STATUSPOINT);
7306 clif_updatestatus(sd,SP_BASEEXP);
7307 clif_updatestatus(sd,SP_JOBEXP);
7308 clif_updatestatus(sd,SP_NEXTBASEEXP);
7309 clif_updatestatus(sd,SP_NEXTJOBEXP);
7310 clif_updatestatus(sd,SP_SKILLPOINT);
7311
7312 clif_updatestatus(sd,SP_USTR); // Updates needed stat points - Valaris
7313 clif_updatestatus(sd,SP_UAGI);
7314 clif_updatestatus(sd,SP_UVIT);
7315 clif_updatestatus(sd,SP_UINT);
7316 clif_updatestatus(sd,SP_UDEX);
7317 clif_updatestatus(sd,SP_ULUK); // End Addition
7318
7319 for(i=0;i<EQI_MAX;i++) { // unequip items that can't be equipped by base 1 [Valaris]
7320 if(sd->equip_index[i] >= 0)
7321 if(pc_isequip(sd,sd->equip_index[i]))
7322 pc_unequipitem(sd,sd->equip_index[i],2);
7323 }
7324
7325 if ((type == 1 || type == 2 || type == 3) && sd->status.party_id)
7326 party_send_levelup(sd);
7327
7328 status_calc_pc(sd, SCO_FORCE);
7329 clif_skillinfoblock(sd);
7330
7331 return 0;
7332}
7333/*==========================================
7334 * /resetstate
7335 *------------------------------------------*/
7336int pc_resetstate(struct map_session_data* sd)
7337{
7338 nullpo_ret(sd);
7339
7340 if (battle_config.use_statpoint_table)
7341 { // New statpoint table used here - Dexity
7342 if (sd->status.base_level > MAX_LEVEL)
7343 { //statp[] goes out of bounds, can't reset!
7344 ShowError("pc_resetstate: Can't reset stats of %d:%d, the base level (%d) is greater than the max level supported (%d)\n",
7345 sd->status.account_id, sd->status.char_id, sd->status.base_level, MAX_LEVEL);
7346 return 0;
7347 }
7348
7349 sd->status.status_point = statp[sd->status.base_level] + ( sd->class_&JOBL_UPPER ? 52 : 0 ); // extra 52+48=100 stat points
7350 }
7351 else
7352 {
7353 int add=0;
7354 add += pc_need_status_point(sd, SP_STR, 1-pc_getstat(sd, SP_STR));
7355 add += pc_need_status_point(sd, SP_AGI, 1-pc_getstat(sd, SP_AGI));
7356 add += pc_need_status_point(sd, SP_VIT, 1-pc_getstat(sd, SP_VIT));
7357 add += pc_need_status_point(sd, SP_INT, 1-pc_getstat(sd, SP_INT));
7358 add += pc_need_status_point(sd, SP_DEX, 1-pc_getstat(sd, SP_DEX));
7359 add += pc_need_status_point(sd, SP_LUK, 1-pc_getstat(sd, SP_LUK));
7360
7361 sd->status.status_point+=add;
7362 }
7363
7364 pc_setstat(sd, SP_STR, 1);
7365 pc_setstat(sd, SP_AGI, 1);
7366 pc_setstat(sd, SP_VIT, 1);
7367 pc_setstat(sd, SP_INT, 1);
7368 pc_setstat(sd, SP_DEX, 1);
7369 pc_setstat(sd, SP_LUK, 1);
7370
7371 clif_updatestatus(sd,SP_STR);
7372 clif_updatestatus(sd,SP_AGI);
7373 clif_updatestatus(sd,SP_VIT);
7374 clif_updatestatus(sd,SP_INT);
7375 clif_updatestatus(sd,SP_DEX);
7376 clif_updatestatus(sd,SP_LUK);
7377
7378 clif_updatestatus(sd,SP_USTR); // Updates needed stat points - Valaris
7379 clif_updatestatus(sd,SP_UAGI);
7380 clif_updatestatus(sd,SP_UVIT);
7381 clif_updatestatus(sd,SP_UINT);
7382 clif_updatestatus(sd,SP_UDEX);
7383 clif_updatestatus(sd,SP_ULUK); // End Addition
7384
7385 clif_updatestatus(sd,SP_STATUSPOINT);
7386
7387 if( sd->mission_mobid ) { //bugreport:2200
7388 sd->mission_mobid = 0;
7389 sd->mission_count = 0;
7390 pc_setglobalreg(sd, add_str(TKMISSIONID_VAR), 0);
7391 }
7392
7393 status_calc_pc(sd, SCO_NONE);
7394
7395 return 1;
7396}
7397
7398/*==========================================
7399 * /resetskill
7400 * if flag&1, perform block resync and status_calc call.
7401 * if flag&2, just count total amount of skill points used by player, do not really reset.
7402 * if flag&4, just reset the skills if the player class is a bard/dancer type (for changesex.)
7403 *------------------------------------------*/
7404int pc_resetskill(struct map_session_data* sd, int flag)
7405{
7406 int i, skill_point=0;
7407 nullpo_ret(sd);
7408
7409 if( flag&4 && (sd->class_&MAPID_UPPERMASK) != MAPID_BARDDANCER )
7410 return 0;
7411
7412 if( !(flag&2) ) { //Remove stuff lost when resetting skills.
7413 /**
7414 * It has been confirmed on official servers that when you reset skills with a ranked Taekwon your skills are not reset (because you have all of them anyway)
7415 **/
7416 if( pc_is_taekwon_ranker(sd) )
7417 return 0;
7418
7419 if( pc_checkskill(sd, SG_DEVIL) && pc_is_maxjoblv(sd) )
7420 clif_status_load(&sd->bl, EFST_DEVIL1, 0); //Remove perma blindness due to skill-reset. [Skotlex]
7421 i = sd->sc.option;
7422 if( i&OPTION_RIDING && pc_checkskill(sd, KN_RIDING) )
7423 i &= ~OPTION_RIDING;
7424 if( i&OPTION_FALCON && pc_checkskill(sd, HT_FALCON) )
7425 i &= ~OPTION_FALCON;
7426 if( i&OPTION_DRAGON && pc_checkskill(sd, RK_DRAGONTRAINING) )
7427 i &= ~OPTION_DRAGON;
7428 if( i&OPTION_WUG && pc_checkskill(sd, RA_WUGMASTERY) )
7429 i &= ~OPTION_WUG;
7430 if( i&OPTION_WUGRIDER && pc_checkskill(sd, RA_WUGRIDER) )
7431 i &= ~OPTION_WUGRIDER;
7432 if( i&OPTION_MADOGEAR && ( sd->class_&MAPID_THIRDMASK ) == MAPID_MECHANIC )
7433 i &= ~OPTION_MADOGEAR;
7434#ifndef NEW_CARTS
7435 if( i&OPTION_CART && pc_checkskill(sd, MC_PUSHCART) )
7436 i &= ~OPTION_CART;
7437#else
7438 if( sd->sc.data[SC_PUSH_CART] )
7439 pc_setcart(sd, 0);
7440#endif
7441 if( i != sd->sc.option )
7442 pc_setoption(sd, i);
7443
7444 if( hom_is_active(sd->hd) && pc_checkskill(sd, AM_CALLHOMUN) )
7445 hom_vaporize(sd, HOM_ST_ACTIVE);
7446
7447 if (sd->sc.data[SC_SPRITEMABLE] && pc_checkskill(sd, SU_SPRITEMABLE))
7448 status_change_end(&sd->bl, SC_SPRITEMABLE, INVALID_TIMER);
7449 }
7450
7451 for( i = 1; i < MAX_SKILL; i++ )
7452 {
7453 uint8 lv = sd->status.skill[i].lv;
7454 int inf2;
7455 uint16 skill_id = skill_idx2id(i);
7456 if (lv == 0 || skill_id == 0)
7457 continue;
7458
7459 inf2 = skill_get_inf2(skill_id);
7460
7461 if( inf2&(INF2_WEDDING_SKILL|INF2_SPIRIT_SKILL) ) //Avoid reseting wedding/linker skills.
7462 continue;
7463
7464 // Don't reset trick dead if not a novice/baby
7465 if( skill_id == NV_TRICKDEAD && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE )
7466 {
7467 sd->status.skill[i].lv = 0;
7468 sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
7469 continue;
7470 }
7471
7472 // do not reset basic skill
7473 if (skill_id == NV_BASIC && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE )
7474 continue;
7475
7476 if( sd->status.skill[i].flag == SKILL_FLAG_PERM_GRANTED )
7477 continue;
7478
7479 if( flag&4 && !skill_ischangesex(skill_id) )
7480 continue;
7481
7482 if( inf2&INF2_QUEST_SKILL && !battle_config.quest_skill_learn )
7483 { //Only handle quest skills in a special way when you can't learn them manually
7484 if( battle_config.quest_skill_reset && !(flag&2) )
7485 { //Wipe them
7486 sd->status.skill[i].lv = 0;
7487 sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
7488 }
7489 continue;
7490 }
7491 if( sd->status.skill[i].flag == SKILL_FLAG_PERMANENT )
7492 skill_point += lv;
7493 else
7494 if( sd->status.skill[i].flag >= SKILL_FLAG_REPLACED_LV_0 )
7495 skill_point += (sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0);
7496
7497 if( !(flag&2) )
7498 {// reset
7499 sd->status.skill[i].lv = 0;
7500 sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
7501 }
7502 }
7503
7504 if( flag&2 || !skill_point ) return skill_point;
7505
7506 sd->status.skill_point += skill_point;
7507
7508 if (flag&1) {
7509 clif_updatestatus(sd,SP_SKILLPOINT);
7510 clif_skillinfoblock(sd);
7511 status_calc_pc(sd, SCO_FORCE);
7512 }
7513
7514 return skill_point;
7515}
7516
7517/*==========================================
7518 * /resetfeel [Komurka]
7519 *------------------------------------------*/
7520int pc_resetfeel(struct map_session_data* sd)
7521{
7522 int i;
7523 nullpo_ret(sd);
7524
7525 for (i=0; i<MAX_PC_FEELHATE; i++)
7526 {
7527 sd->feel_map[i].m = -1;
7528 sd->feel_map[i].index = 0;
7529 pc_setglobalreg(sd, add_str(sg_info[i].feel_var), 0);
7530 }
7531
7532 return 0;
7533}
7534
7535int pc_resethate(struct map_session_data* sd)
7536{
7537 int i;
7538 nullpo_ret(sd);
7539
7540 for (i=0; i<3; i++)
7541 {
7542 sd->hate_mob[i] = -1;
7543 pc_setglobalreg(sd, add_str(sg_info[i].hate_var), 0);
7544 }
7545 return 0;
7546}
7547
7548int pc_skillatk_bonus(struct map_session_data *sd, uint16 skill_id)
7549{
7550 int i, bonus = 0;
7551 nullpo_ret(sd);
7552
7553 skill_id = skill_dummy2skill_id(skill_id);
7554
7555 ARR_FIND(0, ARRAYLENGTH(sd->skillatk), i, sd->skillatk[i].id == skill_id);
7556 if( i < ARRAYLENGTH(sd->skillatk) )
7557 bonus = sd->skillatk[i].val;
7558
7559 return bonus;
7560}
7561
7562int pc_sub_skillatk_bonus(struct map_session_data *sd, uint16 skill_id)
7563{
7564 int i, bonus = 0;
7565 nullpo_ret(sd);
7566
7567 skill_id = skill_dummy2skill_id(skill_id);
7568
7569 ARR_FIND(0, ARRAYLENGTH(sd->subskill), i, sd->subskill[i].id == skill_id);
7570 if( i < ARRAYLENGTH(sd->subskill) )
7571 bonus = sd->subskill[i].val;
7572
7573 return bonus;
7574}
7575
7576int pc_skillheal_bonus(struct map_session_data *sd, uint16 skill_id) {
7577 int i, bonus = sd->bonus.add_heal_rate;
7578
7579 skill_id = skill_dummy2skill_id(skill_id);
7580
7581 if( bonus ) {
7582 switch( skill_id ) {
7583 case AL_HEAL: if( !(battle_config.skill_add_heal_rate&1) ) bonus = 0; break;
7584 case PR_SANCTUARY: if( !(battle_config.skill_add_heal_rate&2) ) bonus = 0; break;
7585 case AM_POTIONPITCHER: if( !(battle_config.skill_add_heal_rate&4) ) bonus = 0; break;
7586 case CR_SLIMPITCHER: if( !(battle_config.skill_add_heal_rate&8) ) bonus = 0; break;
7587 case BA_APPLEIDUN: if( !(battle_config.skill_add_heal_rate&16)) bonus = 0; break;
7588 }
7589 }
7590
7591 ARR_FIND(0, ARRAYLENGTH(sd->skillheal), i, sd->skillheal[i].id == skill_id);
7592
7593 if( i < ARRAYLENGTH(sd->skillheal) )
7594 bonus += sd->skillheal[i].val;
7595
7596 return bonus;
7597}
7598
7599int pc_skillheal2_bonus(struct map_session_data *sd, uint16 skill_id) {
7600 int i, bonus = sd->bonus.add_heal2_rate;
7601
7602 skill_id = skill_dummy2skill_id(skill_id);
7603
7604 ARR_FIND(0, ARRAYLENGTH(sd->skillheal2), i, sd->skillheal2[i].id == skill_id);
7605
7606 if( i < ARRAYLENGTH(sd->skillheal2) )
7607 bonus += sd->skillheal2[i].val;
7608
7609 return bonus;
7610}
7611
7612void pc_respawn(struct map_session_data* sd, clr_type clrtype)
7613{
7614 if( !pc_isdead(sd) )
7615 return; // not applicable
7616 if( sd->bg_id && bg_member_respawn(sd) )
7617 return; // member revived by battleground
7618
7619 pc_setstand(sd, true);
7620 pc_setrestartvalue(sd,3);
7621 if( pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, clrtype) != SETPOS_OK )
7622 clif_resurrection(&sd->bl, 1); //If warping fails, send a normal stand up packet.
7623}
7624
7625static TIMER_FUNC(pc_respawn_timer){
7626 struct map_session_data *sd = map_id2sd(id);
7627 if( sd != NULL )
7628 {
7629 sd->pvp_point=0;
7630 pc_respawn(sd,CLR_OUTSIGHT);
7631 }
7632
7633 return 0;
7634}
7635
7636/*==========================================
7637 * Invoked when a player has received damage
7638 *------------------------------------------*/
7639void pc_damage(struct map_session_data *sd,struct block_list *src,unsigned int hp, unsigned int sp)
7640{
7641 if (sp) clif_updatestatus(sd,SP_SP);
7642 if (hp) clif_updatestatus(sd,SP_HP);
7643 else return;
7644
7645 if (!src)
7646 return;
7647
7648 if( pc_issit(sd) ) {
7649 pc_setstand(sd, true);
7650 skill_sit(sd,0);
7651 }
7652
7653 if (sd->progressbar.npc_id)
7654 clif_progressbar_abort(sd);
7655
7656 if( sd->status.pet_id > 0 && sd->pd && battle_config.pet_damage_support )
7657 pet_target_check(sd->pd,src,1);
7658
7659 if( sd->status.ele_id > 0 )
7660 elemental_set_target(sd,src);
7661
7662 if(battle_config.prevent_logout_trigger&PLT_DAMAGE)
7663 sd->canlog_tick = gettick();
7664}
7665
7666TIMER_FUNC(pc_close_npc_timer){
7667 TBL_PC *sd = map_id2sd(id);
7668 if(sd) pc_close_npc(sd,data);
7669 return 0;
7670}
7671/*
7672 * Method to properly close npc for player and clear anything related
7673 * @flag == 1 : produce close button
7674 * @flag == 2 : directly close it
7675 */
7676void pc_close_npc(struct map_session_data *sd,int flag)
7677{
7678 nullpo_retv(sd);
7679
7680 if (sd->npc_id || sd->npc_shopid) {
7681 if (sd->state.using_fake_npc) {
7682 clif_clearunit_single(sd->npc_id, CLR_OUTSIGHT, sd->fd);
7683 sd->state.using_fake_npc = 0;
7684 }
7685
7686 if (sd->st) {
7687 if(sd->st->state == RUN){ //wait ending code execution
7688 add_timer(gettick()+500,pc_close_npc_timer,sd->bl.id,flag);
7689 return;
7690 }
7691 sd->st->state = ((flag==1 && sd->st->mes_active)?CLOSE:END);
7692 sd->st->mes_active = 0;
7693 }
7694 sd->state.menu_or_input = 0;
7695 sd->npc_menu = 0;
7696 sd->npc_shopid = 0;
7697#ifdef SECURE_NPCTIMEOUT
7698 sd->npc_idle_timer = INVALID_TIMER;
7699#endif
7700 if (sd->st && sd->st->state == CLOSE) {
7701 clif_scriptclose(sd, sd->npc_id);
7702 clif_scriptclear(sd, sd->npc_id); // [Ind/Hercules]
7703 sd->st->state = END; // Force to end now
7704 }
7705 if(sd->st && sd->st->state == END ) {// free attached scripts that are waiting
7706 script_free_state(sd->st);
7707 sd->st = NULL;
7708 sd->npc_id = 0;
7709 }
7710 }
7711}
7712
7713/*==========================================
7714 * Invoked when a player has negative current hp
7715 *------------------------------------------*/
7716int pc_dead(struct map_session_data *sd,struct block_list *src)
7717{
7718 int i=0,k=0;
7719 unsigned int tick = gettick();
7720 struct map_data *mapdata = map_getmapdata(sd->bl.m);
7721
7722 // Activate Steel body if a super novice dies at 99+% exp [celest]
7723 // Super Novices have no kill or die functions attached when saved by their angel
7724 if (!sd->state.snovice_dead_flag && (sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE) {
7725 unsigned int exp = pc_nextbaseexp(sd);
7726
7727 if( exp && get_percentage(sd->status.base_exp,exp) >= 99 ) {
7728 sd->state.snovice_dead_flag = 1;
7729 pc_setrestartvalue(sd,1);
7730 status_percent_heal(&sd->bl, 100, 100);
7731 clif_resurrection(&sd->bl, 1);
7732 if(battle_config.pc_invincible_time)
7733 pc_setinvincibletimer(sd, battle_config.pc_invincible_time);
7734 sc_start(&sd->bl,&sd->bl,status_skill2sc(MO_STEELBODY),100,5,skill_get_time(MO_STEELBODY,5));
7735 if(mapdata_flag_gvg2(mapdata))
7736 pc_respawn_timer(INVALID_TIMER, gettick(), sd->bl.id, 0);
7737 return 0;
7738 }
7739 }
7740
7741 for(k = 0; k < MAX_DEVOTION; k++) {
7742 if (sd->devotion[k]){
7743 struct map_session_data *devsd = map_id2sd(sd->devotion[k]);
7744 if (devsd)
7745 status_change_end(&devsd->bl, SC_DEVOTION, INVALID_TIMER);
7746 sd->devotion[k] = 0;
7747 }
7748 }
7749 if(sd->shadowform_id) { //if we were target of shadowform
7750 status_change_end(map_id2bl(sd->shadowform_id), SC__SHADOWFORM, INVALID_TIMER);
7751 sd->shadowform_id = 0; //should be remove on status end anyway
7752 }
7753
7754 if(sd->status.pet_id > 0 && sd->pd) {
7755 struct pet_data *pd = sd->pd;
7756 if( !mapdata->flag[MF_NOEXPPENALTY] ) {
7757 pet_set_intimate(pd, pd->pet.intimate - pd->get_pet_db()->die);
7758 if( pd->pet.intimate < 0 )
7759 pd->pet.intimate = 0;
7760 clif_send_petdata(sd,sd->pd,1,pd->pet.intimate);
7761 }
7762 if( sd->pd->target_id ) // Unlock all targets...
7763 pet_unlocktarget(sd->pd);
7764 }
7765
7766 if (hom_is_active(sd->hd) && battle_config.homunculus_auto_vapor && get_percentage(sd->hd->battle_status.hp, sd->hd->battle_status.max_hp) >= battle_config.homunculus_auto_vapor)
7767 hom_vaporize(sd, HOM_ST_ACTIVE);
7768
7769 if( sd->md )
7770 mercenary_delete(sd->md, 3); // Your mercenary soldier has ran away.
7771
7772 if( sd->ed )
7773 elemental_delete(sd->ed);
7774
7775 // Leave duel if you die [LuzZza]
7776 if(battle_config.duel_autoleave_when_die) {
7777 if(sd->duel_group > 0)
7778 duel_leave(sd->duel_group, sd);
7779 if(sd->duel_invite > 0)
7780 duel_reject(sd->duel_invite, sd);
7781 }
7782
7783 pc_close_npc(sd,2); //close npc if we were using one
7784
7785 /* e.g. not killed thru pc_damage */
7786 if( pc_issit(sd) ) {
7787 clif_status_load(&sd->bl,EFST_SIT,0);
7788 }
7789
7790 pc_setdead(sd);
7791
7792 clif_party_dead( sd );
7793
7794 pc_setglobalreg(sd, add_str(PCDIECOUNTER_VAR), sd->die_counter+1);
7795 pc_setparam(sd, SP_KILLERRID, src?src->id:0);
7796
7797 //Reset menu skills/item skills
7798 if ((sd->skillitem) != 0)
7799 sd->skillitem = sd->skillitemlv = 0;
7800 if ((sd->menuskill_id) != 0)
7801 sd->menuskill_id = sd->menuskill_val = 0;
7802 //Reset ticks.
7803 sd->hp_loss.tick = sd->sp_loss.tick = sd->hp_regen.tick = sd->sp_regen.tick = 0;
7804
7805 if ( sd->spiritball !=0 )
7806 pc_delspiritball(sd,sd->spiritball,0);
7807
7808 if (sd->spiritcharm_type != CHARM_TYPE_NONE && sd->spiritcharm > 0)
7809 pc_delspiritcharm(sd,sd->spiritcharm,sd->spiritcharm_type);
7810
7811 if (src)
7812 switch (src->type) {
7813 case BL_MOB:
7814 {
7815 struct mob_data *md=(struct mob_data *)src;
7816 if(md->target_id==sd->bl.id)
7817 mob_unlocktarget(md,tick);
7818 if(battle_config.mobs_level_up && md->status.hp &&
7819 (unsigned int)md->level < pc_maxbaselv(sd) &&
7820 !md->guardian_data && !md->special_state.ai// Guardians/summons should not level. [Skotlex]
7821 ) { // monster level up [Valaris]
7822 clif_misceffect(&md->bl,0);
7823 md->level++;
7824 status_calc_mob(md, SCO_NONE);
7825 status_percent_heal(src,10,0);
7826
7827 if( battle_config.show_mob_info&4 )
7828 {// update name with new level
7829 clif_name_area(&md->bl);
7830 }
7831 }
7832 src = battle_get_master(src); // Maybe Player Summon
7833 }
7834 break;
7835 case BL_PET: //Pass on to master...
7836 case BL_HOM:
7837 case BL_MER:
7838 src = battle_get_master(src);
7839 break;
7840 }
7841
7842 if (src && src->type == BL_PC) {
7843 struct map_session_data *ssd = (struct map_session_data *)src;
7844 pc_setparam(ssd, SP_KILLEDRID, sd->bl.id);
7845 npc_script_event(ssd, NPCE_KILLPC);
7846
7847 if (battle_config.pk_mode&2) {
7848 ssd->status.manner -= 5;
7849 if(ssd->status.manner < 0)
7850 sc_start(&sd->bl,src,SC_NOCHAT,100,0,0);
7851#if 0
7852 // PK/Karma system code (not enabled yet) [celest]
7853 // originally from Kade Online, so i don't know if any of these is correct ^^;
7854 // note: karma is measured REVERSE, so more karma = more 'evil' / less honourable,
7855 // karma going down = more 'good' / more honourable.
7856 // The Karma System way...
7857
7858 if (sd->status.karma > ssd->status.karma) { // If player killed was more evil
7859 sd->status.karma--;
7860 ssd->status.karma--;
7861 }
7862 else if (sd->status.karma < ssd->status.karma) // If player killed was more good
7863 ssd->status.karma++;
7864
7865
7866 // or the PK System way...
7867
7868 if (sd->status.karma > 0) // player killed is dishonourable?
7869 ssd->status.karma--; // honour points earned
7870 sd->status.karma++; // honour points lost
7871
7872 // To-do: Receive exp on certain occasions
7873#endif
7874 }
7875 }
7876
7877 if(battle_config.bone_drop==2
7878 || (battle_config.bone_drop==1 && mapdata->flag[MF_PVP]))
7879 {
7880 struct item item_tmp;
7881 memset(&item_tmp,0,sizeof(item_tmp));
7882 item_tmp.nameid=ITEMID_SKULL_;
7883 item_tmp.identify=1;
7884 item_tmp.card[0]=CARD0_CREATE;
7885 item_tmp.card[1]=0;
7886 item_tmp.card[2]=GetWord(sd->status.char_id,0); // CharId
7887 item_tmp.card[3]=GetWord(sd->status.char_id,1);
7888 map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
7889 }
7890
7891 //Remove bonus_script when dead
7892 pc_bonus_script_clear(sd,BSF_REM_ON_DEAD);
7893
7894 // changed penalty options, added death by player if pk_mode [Valaris]
7895 if(battle_config.death_penalty_type
7896 && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE // only novices will receive no penalty
7897 && !sd->sc.data[SC_BABY] && !sd->sc.data[SC_LIFEINSURANCE]
7898 && !mapdata->flag[MF_NOEXPPENALTY] && !mapdata_flag_gvg2(mapdata))
7899 {
7900 uint32 base_penalty = 0;
7901 uint32 job_penalty = 0;
7902 uint32 zeny_penalty = 0;
7903
7904 if (pc_isvip(sd)) { // EXP penalty for VIP
7905 base_penalty = battle_config.vip_exp_penalty_base;
7906 job_penalty = battle_config.vip_exp_penalty_job;
7907 zeny_penalty = battle_config.vip_zeny_penalty;
7908 } else {
7909 base_penalty = battle_config.death_penalty_base;
7910 job_penalty = battle_config.death_penalty_job;
7911 zeny_penalty = battle_config.zeny_penalty;
7912 }
7913
7914 if ((battle_config.death_penalty_maxlv&1 || !pc_is_maxbaselv(sd)) && base_penalty > 0) {
7915 switch (battle_config.death_penalty_type) {
7916 case 1: base_penalty = (uint32) ( pc_nextbaseexp(sd) * ( base_penalty / 10000. ) ); break;
7917 case 2: base_penalty = (uint32) ( sd->status.base_exp * ( base_penalty / 10000. ) ); break;
7918 }
7919 if (base_penalty){ //recheck after altering to speedup
7920 if (battle_config.pk_mode && src && src->type==BL_PC)
7921 base_penalty *= 2;
7922 base_penalty = u32min(sd->status.base_exp, base_penalty);
7923 }
7924 }
7925 else
7926 base_penalty = 0;
7927
7928 if ((battle_config.death_penalty_maxlv&2 || !pc_is_maxjoblv(sd)) && job_penalty > 0) {
7929 switch (battle_config.death_penalty_type) {
7930 case 1: job_penalty = (uint32) ( pc_nextjobexp(sd) * ( job_penalty / 10000. ) ); break;
7931 case 2: job_penalty = (uint32) ( sd->status.job_exp * ( job_penalty /10000. ) ); break;
7932 }
7933 if (job_penalty) {
7934 if (battle_config.pk_mode && src && src->type==BL_PC)
7935 job_penalty *= 2;
7936 job_penalty = u32min(sd->status.job_exp, job_penalty);
7937 }
7938 }
7939 else
7940 job_penalty = 0;
7941
7942 if (base_penalty || job_penalty)
7943 pc_lostexp(sd, base_penalty, job_penalty);
7944
7945 if( zeny_penalty > 0 && !mapdata->flag[MF_NOZENYPENALTY]) {
7946 zeny_penalty = (uint32)( sd->status.zeny * ( zeny_penalty / 10000. ) );
7947 if(zeny_penalty)
7948 pc_payzeny(sd, zeny_penalty, LOG_TYPE_PICKDROP_PLAYER, NULL);
7949 }
7950 }
7951
7952 if( mapdata->flag[MF_PVP_NIGHTMAREDROP] ) { // Moved this outside so it works when PVP isn't enabled and during pk mode [Ancyker]
7953 for (const auto &it : mapdata->drop_list) {
7954 int id = it.drop_id, per = it.drop_per;
7955 enum e_nightmare_drop_type type = it.drop_type;
7956
7957 if(id == 0)
7958 continue;
7959 if(id == -1){
7960 int eq_num=0,eq_n[MAX_INVENTORY];
7961 memset(eq_n,0,sizeof(eq_n));
7962 for(i=0;i<MAX_INVENTORY;i++) {
7963 if( (type&NMDT_INVENTORY && !sd->inventory.u.items_inventory[i].equip)
7964 || (type&NMDT_EQUIP && sd->inventory.u.items_inventory[i].equip)
7965 || type&NMDT_ALL)
7966 {
7967 int l;
7968 ARR_FIND( 0, MAX_INVENTORY, l, eq_n[l] <= 0 );
7969 if( l < MAX_INVENTORY )
7970 eq_n[l] = i;
7971
7972 eq_num++;
7973 }
7974 }
7975 if(eq_num > 0){
7976 int n = eq_n[rnd()%eq_num];
7977 if(rnd()%10000 < per) {
7978 if(sd->inventory.u.items_inventory[n].equip)
7979 pc_unequipitem(sd,n,3);
7980 pc_dropitem(sd,n,1);
7981 }
7982 }
7983 }
7984 else if(id > 0) {
7985 for(i=0;i<MAX_INVENTORY;i++){
7986 if(sd->inventory.u.items_inventory[i].nameid == id
7987 && rnd()%10000 < per
7988 && ((type&NMDT_INVENTORY && !sd->inventory.u.items_inventory[i].equip)
7989 || (type&NMDT_EQUIP && sd->inventory.u.items_inventory[i].equip)
7990 || type&NMDT_ALL) ){
7991 if(sd->inventory.u.items_inventory[i].equip)
7992 pc_unequipitem(sd,i,3);
7993 pc_dropitem(sd,i,1);
7994 break;
7995 }
7996 }
7997 }
7998 }
7999 }
8000 // pvp
8001 // disable certain pvp functions on pk_mode [Valaris]
8002 if( !battle_config.pk_mode && mapdata->flag[MF_PVP] && !mapdata->flag[MF_PVP_NOCALCRANK] ) {
8003 sd->pvp_point -= 5;
8004 sd->pvp_lost++;
8005 if( src && src->type == BL_PC ) {
8006 struct map_session_data *ssd = (struct map_session_data *)src;
8007 ssd->pvp_point++;
8008 ssd->pvp_won++;
8009 }
8010 if( sd->pvp_point < 0 ) {
8011 add_timer(tick+1000, pc_respawn_timer,sd->bl.id,0);
8012 return 1|8;
8013 }
8014 }
8015 //GvG
8016 if( mapdata_flag_gvg2(mapdata) ) {
8017 add_timer(tick+1000, pc_respawn_timer, sd->bl.id, 0);
8018 return 1|8;
8019 }
8020 else if( sd->bg_id ) {
8021 struct battleground_data *bg = bg_team_search(sd->bg_id);
8022 if( bg && bg->mapindex > 0 ) { // Respawn by BG
8023 add_timer(tick+1000, pc_respawn_timer, sd->bl.id, 0);
8024 return 1|8;
8025 }
8026 }
8027
8028 //Reset "can log out" tick.
8029 if( battle_config.prevent_logout )
8030 sd->canlog_tick = gettick() - battle_config.prevent_logout;
8031 return 1;
8032}
8033
8034void pc_revive(struct map_session_data *sd,unsigned int hp, unsigned int sp) {
8035 if(hp) clif_updatestatus(sd,SP_HP);
8036 if(sp) clif_updatestatus(sd,SP_SP);
8037
8038 pc_setstand(sd, true);
8039 if(battle_config.pc_invincible_time > 0)
8040 pc_setinvincibletimer(sd, battle_config.pc_invincible_time);
8041
8042 if( sd->state.gmaster_flag ) {
8043 guild_guildaura_refresh(sd,GD_LEADERSHIP,guild_checkskill(sd->guild,GD_LEADERSHIP));
8044 guild_guildaura_refresh(sd,GD_GLORYWOUNDS,guild_checkskill(sd->guild,GD_GLORYWOUNDS));
8045 guild_guildaura_refresh(sd,GD_SOULCOLD,guild_checkskill(sd->guild,GD_SOULCOLD));
8046 guild_guildaura_refresh(sd,GD_HAWKEYES,guild_checkskill(sd->guild,GD_HAWKEYES));
8047 }
8048}
8049// script
8050//
8051/*==========================================
8052 * script reading pc status registry
8053 *------------------------------------------*/
8054int pc_readparam(struct map_session_data* sd,int type)
8055{
8056 int val = 0;
8057
8058 nullpo_ret(sd);
8059
8060 switch(type) {
8061 case SP_SKILLPOINT: val = sd->status.skill_point; break;
8062 case SP_STATUSPOINT: val = sd->status.status_point; break;
8063 case SP_ZENY: val = sd->status.zeny; break;
8064 case SP_BASELEVEL: val = sd->status.base_level; break;
8065 case SP_JOBLEVEL: val = sd->status.job_level; break;
8066 case SP_CLASS: val = sd->status.class_; break;
8067 case SP_BASEJOB: val = pc_mapid2jobid(sd->class_&MAPID_UPPERMASK, sd->status.sex); break; //Base job, extracting upper type.
8068 case SP_UPPER: val = sd->class_&JOBL_UPPER?1:(sd->class_&JOBL_BABY?2:0); break;
8069 case SP_BASECLASS: val = pc_mapid2jobid(sd->class_&MAPID_BASEMASK, sd->status.sex); break; //Extract base class tree. [Skotlex]
8070 case SP_SEX: val = sd->status.sex; break;
8071 case SP_WEIGHT: val = sd->weight; break;
8072 case SP_MAXWEIGHT: val = sd->max_weight; break;
8073 case SP_BASEEXP: val = sd->status.base_exp; break;
8074 case SP_JOBEXP: val = sd->status.job_exp; break;
8075 case SP_NEXTBASEEXP: val = pc_nextbaseexp(sd); break;
8076 case SP_NEXTJOBEXP: val = pc_nextjobexp(sd); break;
8077 case SP_HP: val = sd->battle_status.hp; break;
8078 case SP_MAXHP: val = sd->battle_status.max_hp; break;
8079 case SP_SP: val = sd->battle_status.sp; break;
8080 case SP_MAXSP: val = sd->battle_status.max_sp; break;
8081 case SP_STR: val = sd->status.str; break;
8082 case SP_AGI: val = sd->status.agi; break;
8083 case SP_VIT: val = sd->status.vit; break;
8084 case SP_INT: val = sd->status.int_; break;
8085 case SP_DEX: val = sd->status.dex; break;
8086 case SP_LUK: val = sd->status.luk; break;
8087 case SP_KARMA: val = sd->status.karma; break;
8088 case SP_MANNER: val = sd->status.manner; break;
8089 case SP_FAME: val = sd->status.fame; break;
8090 case SP_KILLERRID: val = sd->killerrid; break;
8091 case SP_KILLEDRID: val = sd->killedrid; break;
8092 case SP_KILLEDGID: val = sd->killedgid; break;
8093 case SP_SITTING: val = pc_issit(sd)?1:0; break;
8094 case SP_CHARMOVE: val = sd->status.character_moves; break;
8095 case SP_CHARRENAME: val = sd->status.rename; break;
8096 case SP_CHARFONT: val = sd->status.font; break;
8097 case SP_BANK_VAULT: val = sd->bank_vault; break;
8098 case SP_CASHPOINTS: val = sd->cashPoints; break;
8099 case SP_KAFRAPOINTS: val = sd->kafraPoints; break;
8100 case SP_ROULETTE_BRONZE: val = sd->roulette_point.bronze; break;
8101 case SP_ROULETTE_SILVER: val = sd->roulette_point.silver; break;
8102 case SP_ROULETTE_GOLD: val = sd->roulette_point.gold; break;
8103 case SP_PCDIECOUNTER: val = sd->die_counter; break;
8104 case SP_COOKMASTERY: val = sd->cook_mastery; break;
8105 case SP_CRITICAL: val = sd->battle_status.cri/10; break;
8106 case SP_ASPD: val = (2000-sd->battle_status.amotion)/10; break;
8107 case SP_BASE_ATK: val = sd->battle_status.batk; break;
8108 case SP_DEF1: val = sd->battle_status.def; break;
8109 case SP_DEF2: val = sd->battle_status.def2; break;
8110 case SP_MDEF1: val = sd->battle_status.mdef; break;
8111 case SP_MDEF2: val = sd->battle_status.mdef2; break;
8112 case SP_HIT: val = sd->battle_status.hit; break;
8113 case SP_FLEE1: val = sd->battle_status.flee; break;
8114 case SP_FLEE2: val = sd->battle_status.flee2; break;
8115 case SP_DEFELE: val = sd->battle_status.def_ele; break;
8116 case SP_MAXHPRATE: val = sd->hprate; break;
8117 case SP_MAXSPRATE: val = sd->sprate; break;
8118 case SP_SPRATE: val = sd->dsprate; break;
8119 case SP_SPEED_RATE: val = sd->bonus.speed_rate; break;
8120 case SP_SPEED_ADDRATE: val = sd->bonus.speed_add_rate; break;
8121 case SP_ASPD_RATE:
8122#ifndef RENEWAL_ASPD
8123 val = sd->battle_status.aspd_rate;
8124#else
8125 val = sd->battle_status.aspd_rate2;
8126#endif
8127 break;
8128 case SP_HP_RECOV_RATE: val = sd->hprecov_rate; break;
8129 case SP_SP_RECOV_RATE: val = sd->sprecov_rate; break;
8130 case SP_CRITICAL_DEF: val = sd->bonus.critical_def; break;
8131 case SP_NEAR_ATK_DEF: val = sd->bonus.near_attack_def_rate; break;
8132 case SP_LONG_ATK_DEF: val = sd->bonus.long_attack_def_rate; break;
8133 case SP_DOUBLE_RATE: val = sd->bonus.double_rate; break;
8134 case SP_DOUBLE_ADD_RATE: val = sd->bonus.double_add_rate; break;
8135 case SP_MATK_RATE: val = sd->matk_rate; break;
8136 case SP_ATK_RATE: val = sd->bonus.atk_rate; break;
8137 case SP_MAGIC_ATK_DEF: val = sd->bonus.magic_def_rate; break;
8138 case SP_MISC_ATK_DEF: val = sd->bonus.misc_def_rate; break;
8139 case SP_PERFECT_HIT_RATE:val = sd->bonus.perfect_hit; break;
8140 case SP_PERFECT_HIT_ADD_RATE: val = sd->bonus.perfect_hit_add; break;
8141 case SP_CRITICAL_RATE: val = sd->critical_rate; break;
8142 case SP_HIT_RATE: val = sd->hit_rate; break;
8143 case SP_FLEE_RATE: val = sd->flee_rate; break;
8144 case SP_FLEE2_RATE: val = sd->flee2_rate; break;
8145 case SP_DEF_RATE: val = sd->def_rate; break;
8146 case SP_DEF2_RATE: val = sd->def2_rate; break;
8147 case SP_MDEF_RATE: val = sd->mdef_rate; break;
8148 case SP_MDEF2_RATE: val = sd->mdef2_rate; break;
8149 case SP_RESTART_FULL_RECOVER: val = sd->special_state.restart_full_recover?1:0; break;
8150 case SP_NO_CASTCANCEL: val = sd->special_state.no_castcancel?1:0; break;
8151 case SP_NO_CASTCANCEL2: val = sd->special_state.no_castcancel2?1:0; break;
8152 case SP_NO_SIZEFIX: val = sd->special_state.no_sizefix?1:0; break;
8153 case SP_NO_MAGIC_DAMAGE: val = sd->special_state.no_magic_damage; break;
8154 case SP_NO_WEAPON_DAMAGE:val = sd->special_state.no_weapon_damage; break;
8155 case SP_NO_MISC_DAMAGE: val = sd->special_state.no_misc_damage; break;
8156 case SP_NO_GEMSTONE: val = sd->special_state.no_gemstone?1:0; break;
8157 case SP_INTRAVISION: val = sd->special_state.intravision?1:0; break;
8158 case SP_NO_KNOCKBACK: val = sd->special_state.no_knockback?1:0; break;
8159 case SP_SPLASH_RANGE: val = sd->bonus.splash_range; break;
8160 case SP_SPLASH_ADD_RANGE:val = sd->bonus.splash_add_range; break;
8161 case SP_SHORT_WEAPON_DAMAGE_RETURN: val = sd->bonus.short_weapon_damage_return; break;
8162 case SP_LONG_WEAPON_DAMAGE_RETURN: val = sd->bonus.long_weapon_damage_return; break;
8163 case SP_MAGIC_DAMAGE_RETURN: val = sd->bonus.magic_damage_return; break;
8164 case SP_PERFECT_HIDE: val = sd->special_state.perfect_hiding?1:0; break;
8165 case SP_UNBREAKABLE: val = sd->bonus.unbreakable; break;
8166 case SP_UNBREAKABLE_WEAPON: val = (sd->bonus.unbreakable_equip&EQP_WEAPON)?1:0; break;
8167 case SP_UNBREAKABLE_ARMOR: val = (sd->bonus.unbreakable_equip&EQP_ARMOR)?1:0; break;
8168 case SP_UNBREAKABLE_HELM: val = (sd->bonus.unbreakable_equip&EQP_HELM)?1:0; break;
8169 case SP_UNBREAKABLE_SHIELD: val = (sd->bonus.unbreakable_equip&EQP_SHIELD)?1:0; break;
8170 case SP_UNBREAKABLE_GARMENT: val = (sd->bonus.unbreakable_equip&EQP_GARMENT)?1:0; break;
8171 case SP_UNBREAKABLE_SHOES: val = (sd->bonus.unbreakable_equip&EQP_SHOES)?1:0; break;
8172 case SP_CLASSCHANGE: val = sd->bonus.classchange; break;
8173 case SP_LONG_ATK_RATE: val = sd->bonus.long_attack_atk_rate; break;
8174 case SP_BREAK_WEAPON_RATE: val = sd->bonus.break_weapon_rate; break;
8175 case SP_BREAK_ARMOR_RATE: val = sd->bonus.break_armor_rate; break;
8176 case SP_ADD_STEAL_RATE: val = sd->bonus.add_steal_rate; break;
8177 case SP_DELAYRATE: val = sd->delayrate; break;
8178 case SP_CRIT_ATK_RATE: val = sd->bonus.crit_atk_rate; break;
8179 case SP_UNSTRIPABLE_WEAPON: val = (sd->bonus.unstripable_equip&EQP_WEAPON)?1:0; break;
8180 case SP_UNSTRIPABLE:
8181 case SP_UNSTRIPABLE_ARMOR:
8182 val = (sd->bonus.unstripable_equip&EQP_ARMOR)?1:0;
8183 break;
8184 case SP_UNSTRIPABLE_HELM: val = (sd->bonus.unstripable_equip&EQP_HELM)?1:0; break;
8185 case SP_UNSTRIPABLE_SHIELD: val = (sd->bonus.unstripable_equip&EQP_SHIELD)?1:0; break;
8186 case SP_SP_GAIN_VALUE: val = sd->bonus.sp_gain_value; break;
8187 case SP_HP_GAIN_VALUE: val = sd->bonus.hp_gain_value; break;
8188 case SP_MAGIC_SP_GAIN_VALUE: val = sd->bonus.magic_sp_gain_value; break;
8189 case SP_MAGIC_HP_GAIN_VALUE: val = sd->bonus.magic_hp_gain_value; break;
8190 case SP_ADD_HEAL_RATE: val = sd->bonus.add_heal_rate; break;
8191 case SP_ADD_HEAL2_RATE: val = sd->bonus.add_heal2_rate; break;
8192 case SP_ADD_ITEM_HEAL_RATE: val = sd->bonus.itemhealrate2; break;
8193 case SP_EMATK: val = sd->bonus.ematk; break;
8194 case SP_FIXCASTRATE: val = sd->bonus.fixcastrate; break;
8195 case SP_ADD_FIXEDCAST: val = sd->bonus.add_fixcast; break;
8196 case SP_ADD_VARIABLECAST: val = sd->bonus.add_varcast; break;
8197 case SP_CASTRATE:
8198 case SP_VARCASTRATE:
8199#ifdef RENEWAL_CAST
8200 val = sd->bonus.varcastrate; break;
8201#else
8202 val = sd->castrate; break;
8203#endif
8204 default:
8205 ShowError("pc_readparam: Attempt to read unknown parameter '%d'.\n", type);
8206 return -1;
8207 }
8208
8209 return val;
8210}
8211
8212/*==========================================
8213 * script set pc status registry
8214 *------------------------------------------*/
8215bool pc_setparam(struct map_session_data *sd,int type,int val)
8216{
8217 nullpo_retr(false,sd);
8218
8219 switch(type){
8220 case SP_BASELEVEL:
8221 if ((unsigned int)val > pc_maxbaselv(sd)) //Capping to max
8222 val = pc_maxbaselv(sd);
8223 if ((unsigned int)val > sd->status.base_level) {
8224 int i = 0;
8225 int stat=0;
8226 for (i = 0; i < (int)((unsigned int)val - sd->status.base_level); i++)
8227 stat += pc_gets_status_point(sd->status.base_level + i);
8228 sd->status.status_point += stat;
8229 }
8230 sd->status.base_level = (unsigned int)val;
8231 sd->status.base_exp = 0;
8232 // clif_updatestatus(sd, SP_BASELEVEL); // Gets updated at the bottom
8233 clif_updatestatus(sd, SP_NEXTBASEEXP);
8234 clif_updatestatus(sd, SP_STATUSPOINT);
8235 clif_updatestatus(sd, SP_BASEEXP);
8236 status_calc_pc(sd, SCO_FORCE);
8237 if(sd->status.party_id)
8238 party_send_levelup(sd);
8239 break;
8240 case SP_JOBLEVEL:
8241 if ((unsigned int)val >= sd->status.job_level) {
8242 if ((unsigned int)val > pc_maxjoblv(sd)) val = pc_maxjoblv(sd);
8243 sd->status.skill_point += val - sd->status.job_level;
8244 clif_updatestatus(sd, SP_SKILLPOINT);
8245 }
8246 sd->status.job_level = (unsigned int)val;
8247 sd->status.job_exp = 0;
8248 // clif_updatestatus(sd, SP_JOBLEVEL); // Gets updated at the bottom
8249 clif_updatestatus(sd, SP_NEXTJOBEXP);
8250 clif_updatestatus(sd, SP_JOBEXP);
8251 status_calc_pc(sd, SCO_FORCE);
8252 break;
8253 case SP_SKILLPOINT:
8254 sd->status.skill_point = val;
8255 break;
8256 case SP_STATUSPOINT:
8257 sd->status.status_point = val;
8258 break;
8259 case SP_ZENY:
8260 if( val < 0 )
8261 return false;// can't set negative zeny
8262 log_zeny(sd, LOG_TYPE_SCRIPT, sd, -(sd->status.zeny - cap_value(val, 0, MAX_ZENY)));
8263 sd->status.zeny = cap_value(val, 0, MAX_ZENY);
8264 break;
8265 case SP_BASEEXP:
8266 {
8267 val = cap_value(val, 0, INT_MAX);
8268 if ((unsigned int)val < sd->status.base_exp) // Lost
8269 pc_lostexp(sd, sd->status.base_exp - val, 0);
8270 else // Gained
8271 pc_gainexp(sd, NULL, val - sd->status.base_exp, 0, 2);
8272 }
8273 return true;
8274 case SP_JOBEXP:
8275 {
8276 val = cap_value(val, 0, INT_MAX);
8277 if ((unsigned int)val < sd->status.job_exp) // Lost
8278 pc_lostexp(sd, 0, sd->status.job_exp - val);
8279 else // Gained
8280 pc_gainexp(sd, NULL, 0, val - sd->status.job_exp, 2);
8281 }
8282 return true;
8283 case SP_SEX:
8284 sd->status.sex = val ? SEX_MALE : SEX_FEMALE;
8285 break;
8286 case SP_WEIGHT:
8287 sd->weight = val;
8288 break;
8289 case SP_MAXWEIGHT:
8290 sd->max_weight = val;
8291 break;
8292 case SP_HP:
8293 sd->battle_status.hp = cap_value(val, 1, (int)sd->battle_status.max_hp);
8294 break;
8295 case SP_MAXHP:
8296 if (sd->status.base_level < 100)
8297 sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp_lv99);
8298 else if (sd->status.base_level < 151)
8299 sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp_lv150);
8300 else
8301 sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp);
8302
8303 if( sd->battle_status.max_hp < sd->battle_status.hp )
8304 {
8305 sd->battle_status.hp = sd->battle_status.max_hp;
8306 clif_updatestatus(sd, SP_HP);
8307 }
8308 break;
8309 case SP_SP:
8310 sd->battle_status.sp = cap_value(val, 0, (int)sd->battle_status.max_sp);
8311 break;
8312 case SP_MAXSP:
8313 sd->battle_status.max_sp = cap_value(val, 1, battle_config.max_sp);
8314
8315 if( sd->battle_status.max_sp < sd->battle_status.sp )
8316 {
8317 sd->battle_status.sp = sd->battle_status.max_sp;
8318 clif_updatestatus(sd, SP_SP);
8319 }
8320 break;
8321 case SP_STR:
8322 sd->status.str = cap_value(val, 1, pc_maxparameter(sd,PARAM_STR));
8323 break;
8324 case SP_AGI:
8325 sd->status.agi = cap_value(val, 1, pc_maxparameter(sd,PARAM_AGI));
8326 break;
8327 case SP_VIT:
8328 sd->status.vit = cap_value(val, 1, pc_maxparameter(sd,PARAM_VIT));
8329 break;
8330 case SP_INT:
8331 sd->status.int_ = cap_value(val, 1, pc_maxparameter(sd,PARAM_INT));
8332 break;
8333 case SP_DEX:
8334 sd->status.dex = cap_value(val, 1, pc_maxparameter(sd,PARAM_DEX));
8335 break;
8336 case SP_LUK:
8337 sd->status.luk = cap_value(val, 1, pc_maxparameter(sd,PARAM_LUK));
8338 break;
8339 case SP_KARMA:
8340 sd->status.karma = val;
8341 break;
8342 case SP_MANNER:
8343 sd->status.manner = val;
8344 if( val < 0 )
8345 sc_start(NULL, &sd->bl, SC_NOCHAT, 100, 0, 0);
8346 else {
8347 status_change_end(&sd->bl, SC_NOCHAT, INVALID_TIMER);
8348 clif_manner_message(sd, 5);
8349 }
8350 return true; // status_change_start/status_change_end already sends packets warning the client
8351 case SP_FAME:
8352 sd->status.fame = val;
8353 break;
8354 case SP_KILLERRID:
8355 sd->killerrid = val;
8356 return true;
8357 case SP_KILLEDRID:
8358 sd->killedrid = val;
8359 return true;
8360 case SP_KILLEDGID:
8361 sd->killedgid = val;
8362 return true;
8363 case SP_CHARMOVE:
8364 sd->status.character_moves = val;
8365 return true;
8366 case SP_CHARRENAME:
8367 sd->status.rename = val;
8368 return true;
8369 case SP_CHARFONT:
8370 sd->status.font = val;
8371 clif_font(sd);
8372 return true;
8373 case SP_BANK_VAULT:
8374 if (val < 0)
8375 return false;
8376 log_zeny(sd, LOG_TYPE_BANK, sd, -(sd->bank_vault - cap_value(val, 0, MAX_BANK_ZENY)));
8377 sd->bank_vault = cap_value(val, 0, MAX_BANK_ZENY);
8378 pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
8379 return true;
8380 case SP_ROULETTE_BRONZE:
8381 sd->roulette_point.bronze = val;
8382 pc_setreg2(sd, ROULETTE_BRONZE_VAR, sd->roulette_point.bronze);
8383 return true;
8384 case SP_ROULETTE_SILVER:
8385 sd->roulette_point.silver = val;
8386 pc_setreg2(sd, ROULETTE_SILVER_VAR, sd->roulette_point.silver);
8387 return true;
8388 case SP_ROULETTE_GOLD:
8389 sd->roulette_point.gold = val;
8390 pc_setreg2(sd, ROULETTE_GOLD_VAR, sd->roulette_point.gold);
8391 return true;
8392 case SP_CASHPOINTS:
8393 if (val < 0)
8394 return false;
8395 if (!sd->state.connect_new)
8396 log_cash(sd, LOG_TYPE_SCRIPT, LOG_CASH_TYPE_CASH, -(sd->cashPoints - cap_value(val, 0, MAX_ZENY)));
8397 sd->cashPoints = cap_value(val, 0, MAX_ZENY);
8398 pc_setaccountreg(sd, add_str(CASHPOINT_VAR), sd->cashPoints);
8399 return true;
8400 case SP_KAFRAPOINTS:
8401 if (val < 0)
8402 return false;
8403 if (!sd->state.connect_new)
8404 log_cash(sd, LOG_TYPE_SCRIPT, LOG_CASH_TYPE_KAFRA, -(sd->kafraPoints - cap_value(val, 0, MAX_ZENY)));
8405 sd->kafraPoints = cap_value(val, 0, MAX_ZENY);
8406 pc_setaccountreg(sd, add_str(KAFRAPOINT_VAR), sd->kafraPoints);
8407 return true;
8408 case SP_PCDIECOUNTER:
8409 if (val < 0)
8410 return false;
8411 if (sd->die_counter == val)
8412 return true;
8413 sd->die_counter = val;
8414 if (!sd->die_counter && (sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE)
8415 status_calc_pc(sd, SCO_NONE); // Lost the bonus.
8416 pc_setglobalreg(sd, add_str(PCDIECOUNTER_VAR), sd->die_counter);
8417 return true;
8418 case SP_COOKMASTERY:
8419 if (val < 0)
8420 return false;
8421 if (sd->cook_mastery == val)
8422 return true;
8423 val = cap_value(val, 0, 1999);
8424 sd->cook_mastery = val;
8425 pc_setglobalreg(sd, add_str(COOKMASTERY_VAR), sd->cook_mastery);
8426 return true;
8427 default:
8428 ShowError("pc_setparam: Attempted to set unknown parameter '%d'.\n", type);
8429 return false;
8430 }
8431 clif_updatestatus(sd,type);
8432
8433 return true;
8434}
8435
8436/*==========================================
8437 * HP/SP Healing. If flag is passed, the heal type is through clif_heal, otherwise update status.
8438 *------------------------------------------*/
8439void pc_heal(struct map_session_data *sd,unsigned int hp,unsigned int sp, int type)
8440{
8441 if (type&2) {
8442 if (hp || type&4)
8443 clif_heal(sd->fd,SP_HP,hp);
8444 if (sp)
8445 clif_heal(sd->fd,SP_SP,sp);
8446 } else {
8447 if(hp)
8448 clif_updatestatus(sd,SP_HP);
8449 if(sp)
8450 clif_updatestatus(sd,SP_SP);
8451 }
8452 return;
8453}
8454
8455/**
8456 * Heal player HP and/or SP linearly. Calculate any bonus based on active statuses.
8457 * @param sd: Player data
8458 * @param itemid: Item ID
8459 * @param hp: HP to heal
8460 * @param sp: SP to heal
8461 * @return Amount healed to an object
8462 */
8463int pc_itemheal(struct map_session_data *sd, int itemid, int hp, int sp)
8464{
8465 int bonus, tmp, penalty = 0;
8466
8467 if (hp) {
8468 int i;
8469
8470 bonus = 100 + (sd->battle_status.vit << 1) + pc_checkskill(sd, SM_RECOVERY) * 10 + pc_checkskill(sd, AM_LEARNINGPOTION) * 5;
8471 // A potion produced by an Alchemist in the Fame Top 10 gets +50% effect [DracoRPG]
8472 if (potion_flag == 2) {
8473 bonus += 50;
8474 if (sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_ROGUE)
8475 bonus += 100; // Receive an additional +100% effect from ranked potions to HP only
8476 }
8477 //All item bonuses.
8478 bonus += sd->bonus.itemhealrate2;
8479 //Item Group bonuses
8480 bonus += pc_get_itemgroup_bonus(sd, itemid);
8481 //Individual item bonuses.
8482 for(i = 0; i < ARRAYLENGTH(sd->itemhealrate) && sd->itemhealrate[i].nameid; i++) {
8483 if (sd->itemhealrate[i].nameid == itemid) {
8484 bonus += sd->itemhealrate[i].rate;
8485 break;
8486 }
8487 }
8488
8489 // Recovery Potion
8490 if (sd->sc.data[SC_INCHEALRATE])
8491 bonus += sd->sc.data[SC_INCHEALRATE]->val1;
8492 // 2014 Halloween Event : Pumpkin Bonus
8493 if (sd->sc.data[SC_MTF_PUMPKIN] && itemid == ITEMID_PUMPKIN)
8494 bonus += sd->sc.data[SC_MTF_PUMPKIN]->val1;
8495
8496 tmp = hp * bonus / 100; // Overflow check
8497 if (bonus != 100 && tmp > hp)
8498 hp = tmp;
8499 }
8500 if (sp) {
8501 bonus = 100 + (sd->battle_status.int_ << 1) + pc_checkskill(sd, MG_SRECOVERY) * 10 + pc_checkskill(sd, AM_LEARNINGPOTION) * 5;
8502 // A potion produced by an Alchemist in the Fame Top 10 gets +50% effect [DracoRPG]
8503 if (potion_flag == 2)
8504 bonus += 50;
8505
8506 tmp = sp * bonus / 100; // Overflow check
8507 if (bonus != 100 && tmp > sp)
8508 sp = tmp;
8509 }
8510 if (sd->sc.count) {
8511 // Critical Wound and Death Hurt stack
8512 if (sd->sc.data[SC_CRITICALWOUND])
8513 penalty += sd->sc.data[SC_CRITICALWOUND]->val2;
8514
8515 if (sd->sc.data[SC_DEATHHURT])
8516 penalty += 20;
8517
8518 if (sd->sc.data[SC_NORECOVER_STATE])
8519 penalty = 100;
8520
8521 if (sd->sc.data[SC_VITALITYACTIVATION]) {
8522 hp += hp / 2; // 1.5 times
8523 sp -= sp / 2;
8524 }
8525
8526 if (sd->sc.data[SC_WATER_INSIGNIA] && sd->sc.data[SC_WATER_INSIGNIA]->val1 == 2) {
8527 hp += hp / 10;
8528 sp += sp / 10;
8529 }
8530
8531 if (penalty > 0) {
8532 hp -= hp * penalty / 100;
8533 sp -= sp * penalty / 100;
8534 }
8535
8536#ifdef RENEWAL
8537 if (sd->sc.data[SC_EXTREMITYFIST2])
8538 sp = 0;
8539#endif
8540 if (sd->sc.data[SC_BITESCAR])
8541 hp = 0;
8542 }
8543
8544 return status_heal(&sd->bl, hp, sp, 1);
8545}
8546
8547/*==========================================
8548 * HP/SP Recovery
8549 * Heal player hp nad/or sp by rate
8550 *------------------------------------------*/
8551int pc_percentheal(struct map_session_data *sd,int hp,int sp)
8552{
8553 nullpo_ret(sd);
8554
8555 if (hp > 100) hp = 100;
8556 else if (hp <-100) hp = -100;
8557
8558 if (sp > 100) sp = 100;
8559 else if (sp <-100) sp = -100;
8560
8561 if(hp >= 0 && sp >= 0) //Heal
8562 return status_percent_heal(&sd->bl, hp, sp);
8563
8564 if(hp <= 0 && sp <= 0) //Damage (negative rates indicate % of max rather than current), and only kill target IF the specified amount is 100%
8565 return status_percent_damage(NULL, &sd->bl, hp, sp, hp==-100);
8566
8567 //Crossed signs
8568 if(hp) {
8569 if(hp > 0)
8570 status_percent_heal(&sd->bl, hp, 0);
8571 else
8572 status_percent_damage(NULL, &sd->bl, hp, 0, hp==-100);
8573 }
8574
8575 if(sp) {
8576 if(sp > 0)
8577 status_percent_heal(&sd->bl, 0, sp);
8578 else
8579 status_percent_damage(NULL, &sd->bl, 0, sp, false);
8580 }
8581 return 0;
8582}
8583
8584static int jobchange_killclone(struct block_list *bl, va_list ap)
8585{
8586 struct mob_data *md;
8587 int flag;
8588 md = (struct mob_data *)bl;
8589 nullpo_ret(md);
8590 flag = va_arg(ap, int);
8591
8592 if (md->master_id && md->special_state.clone && md->master_id == flag)
8593 status_kill(&md->bl);
8594 return 1;
8595}
8596
8597/**
8598 * Called when player changes job
8599 * Rewrote to make it tidider [Celest]
8600 * @param sd
8601 * @param job JOB ID. See enum e_job
8602 * @param upper 1 - JOBL_UPPER; 2 - JOBL_BABY
8603 * @return True if success, false if failed
8604 **/
8605bool pc_jobchange(struct map_session_data *sd,int job, char upper)
8606{
8607 int i, fame_flag = 0;
8608 int b_class;
8609
8610 nullpo_retr(false,sd);
8611
8612 if (job < 0)
8613 return false;
8614
8615 //Normalize job.
8616 b_class = pc_jobid2mapid(job);
8617 if (b_class == -1)
8618 return false;
8619 switch (upper) {
8620 case 1:
8621 b_class|= JOBL_UPPER;
8622 break;
8623 case 2:
8624 b_class|= JOBL_BABY;
8625 break;
8626 }
8627 //This will automatically adjust bard/dancer classes to the correct gender
8628 //That is, if you try to jobchange into dancer, it will turn you to bard.
8629 job = pc_mapid2jobid(b_class, sd->status.sex);
8630 if (job == -1)
8631 return false;
8632
8633 if ((unsigned short)b_class == sd->class_)
8634 return false; //Nothing to change.
8635
8636 // changing from 1st to 2nd job
8637 if ((b_class&JOBL_2) && !(sd->class_&JOBL_2) && (sd->class_&MAPID_UPPERMASK) != MAPID_SUPER_NOVICE) {
8638 sd->change_level_2nd = sd->status.job_level;
8639 pc_setglobalreg(sd, add_str(JOBCHANGE2ND_VAR), sd->change_level_2nd);
8640 }
8641 // changing from 2nd to 3rd job
8642 else if((b_class&JOBL_THIRD) && !(sd->class_&JOBL_THIRD)) {
8643 sd->change_level_3rd = sd->status.job_level;
8644 pc_setglobalreg(sd, add_str(JOBCHANGE3RD_VAR), sd->change_level_3rd);
8645 }
8646
8647 if(sd->cloneskill_idx > 0) {
8648 if( sd->status.skill[sd->cloneskill_idx].flag == SKILL_FLAG_PLAGIARIZED ) {
8649 sd->status.skill[sd->cloneskill_idx].id = 0;
8650 sd->status.skill[sd->cloneskill_idx].lv = 0;
8651 sd->status.skill[sd->cloneskill_idx].flag = SKILL_FLAG_PERMANENT;
8652 clif_deleteskill(sd,pc_readglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM)));
8653 }
8654 sd->cloneskill_idx = 0;
8655 pc_setglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM), 0);
8656 pc_setglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM_LV), 0);
8657 }
8658
8659 if(sd->reproduceskill_idx > 0) {
8660 if( sd->status.skill[sd->reproduceskill_idx].flag == SKILL_FLAG_PLAGIARIZED ) {
8661 sd->status.skill[sd->reproduceskill_idx].id = 0;
8662 sd->status.skill[sd->reproduceskill_idx].lv = 0;
8663 sd->status.skill[sd->reproduceskill_idx].flag = SKILL_FLAG_PERMANENT;
8664 clif_deleteskill(sd,pc_readglobalreg(sd, add_str(SKILL_VAR_REPRODUCE)));
8665 }
8666 sd->reproduceskill_idx = 0;
8667 pc_setglobalreg(sd, add_str(SKILL_VAR_REPRODUCE), 0);
8668 pc_setglobalreg(sd, add_str(SKILL_VAR_REPRODUCE_LV), 0);
8669 }
8670
8671 // Give or reduce transcendent status points
8672 if( (b_class&JOBL_UPPER) && !(sd->class_&JOBL_UPPER) ){ // Change from a non t class to a t class -> give points
8673 sd->status.status_point += battle_config.transcendent_status_points;
8674 clif_updatestatus(sd,SP_STATUSPOINT);
8675 }else if( !(b_class&JOBL_UPPER) && (sd->class_&JOBL_UPPER) ){ // Change from a t class to a non t class -> remove points
8676 if( sd->status.status_point < battle_config.transcendent_status_points ){
8677 // The player already used his bonus points, so we have to reset his status points
8678 pc_resetstate(sd);
8679 }
8680 sd->status.status_point -= battle_config.transcendent_status_points;
8681 clif_updatestatus(sd,SP_STATUSPOINT);
8682 }
8683
8684 if ( (b_class&MAPID_UPPERMASK) != (sd->class_&MAPID_UPPERMASK) ) { //Things to remove when changing class tree.
8685 const int class_ = pc_class2idx(sd->status.class_);
8686 uint16 skill_id;
8687 for(i = 0; i < MAX_SKILL_TREE && (skill_id = skill_tree[class_][i].skill_id) > 0; i++) {
8688 //Remove status specific to your current tree skills.
8689 enum sc_type sc = status_skill2sc(skill_id);
8690 if (sc > SC_COMMON_MAX && sd->sc.data[sc])
8691 status_change_end(&sd->bl, sc, INVALID_TIMER);
8692 }
8693 }
8694
8695 if( (sd->class_&MAPID_UPPERMASK) == MAPID_STAR_GLADIATOR && (b_class&MAPID_UPPERMASK) != MAPID_STAR_GLADIATOR) {
8696 /* going off star glad lineage, reset feel to not store no-longer-used vars in the database */
8697 pc_resetfeel(sd);
8698 }
8699
8700 // Reset body style to 0 before changing job to avoid
8701 // errors since not every job has a alternate outfit.
8702 sd->status.body = 0;
8703 clif_changelook(&sd->bl,LOOK_BODY2,0);
8704
8705 sd->status.class_ = job;
8706 fame_flag = pc_famerank(sd->status.char_id,sd->class_&MAPID_UPPERMASK);
8707 sd->class_ = (unsigned short)b_class;
8708 sd->status.job_level=1;
8709 sd->status.job_exp=0;
8710
8711 if (sd->status.base_level > pc_maxbaselv(sd)) {
8712 sd->status.base_level = pc_maxbaselv(sd);
8713 sd->status.base_exp=0;
8714 pc_resetstate(sd);
8715 clif_updatestatus(sd,SP_STATUSPOINT);
8716 clif_updatestatus(sd,SP_BASELEVEL);
8717 clif_updatestatus(sd,SP_BASEEXP);
8718 clif_updatestatus(sd,SP_NEXTBASEEXP);
8719 }
8720
8721 clif_updatestatus(sd,SP_JOBLEVEL);
8722 clif_updatestatus(sd,SP_JOBEXP);
8723 clif_updatestatus(sd,SP_NEXTJOBEXP);
8724
8725 for(i=0;i<EQI_MAX;i++) {
8726 if(sd->equip_index[i] >= 0)
8727 if(pc_isequip(sd,sd->equip_index[i]))
8728 pc_unequipitem(sd,sd->equip_index[i],2); // unequip invalid item for class
8729 }
8730
8731 //Change look, if disguised, you need to undisguise
8732 //to correctly calculate new job sprite without
8733 if (sd->disguise)
8734 pc_disguise(sd, 0);
8735
8736 status_set_viewdata(&sd->bl, job);
8737 clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_); // move sprite update to prevent client crashes with incompatible equipment [Valaris]
8738#if PACKETVER >= 20151001
8739 clif_changelook(&sd->bl, LOOK_HAIR, sd->vd.hair_style); // Update player's head (only matters when switching to or from Doram)
8740#endif
8741 if(sd->vd.cloth_color)
8742 clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
8743 /*
8744 if(sd->vd.body_style)
8745 clif_changelook(&sd->bl,LOOK_BODY2,sd->vd.body_style);
8746 */
8747 //Update skill tree.
8748 pc_calc_skilltree(sd);
8749 clif_skillinfoblock(sd);
8750
8751 if (sd->ed)
8752 elemental_delete(sd->ed);
8753 if (sd->state.vending)
8754 vending_closevending(sd);
8755 if (sd->state.buyingstore)
8756 buyingstore_close(sd);
8757
8758 map_foreachinmap(jobchange_killclone, sd->bl.m, BL_MOB, sd->bl.id);
8759
8760 //Remove peco/cart/falcon
8761 i = sd->sc.option;
8762 if( i&OPTION_RIDING && !pc_checkskill(sd, KN_RIDING) )
8763 i&=~OPTION_RIDING;
8764 if( i&OPTION_FALCON && !pc_checkskill(sd, HT_FALCON) )
8765 i&=~OPTION_FALCON;
8766 if( i&OPTION_DRAGON && !pc_checkskill(sd,RK_DRAGONTRAINING) )
8767 i&=~OPTION_DRAGON;
8768 if( i&OPTION_WUGRIDER && !pc_checkskill(sd,RA_WUGMASTERY) )
8769 i&=~OPTION_WUGRIDER;
8770 if( i&OPTION_WUG && !pc_checkskill(sd,RA_WUGMASTERY) )
8771 i&=~OPTION_WUG;
8772 if( i&OPTION_MADOGEAR ) //You do not need a skill for this.
8773 i&=~OPTION_MADOGEAR;
8774#ifndef NEW_CARTS
8775 if( i&OPTION_CART && !pc_checkskill(sd, MC_PUSHCART) )
8776 i&=~OPTION_CART;
8777#else
8778 if( sd->sc.data[SC_PUSH_CART] && !pc_checkskill(sd, MC_PUSHCART) )
8779 pc_setcart(sd, 0);
8780#endif
8781 if(i != sd->sc.option)
8782 pc_setoption(sd, i);
8783
8784 if(hom_is_active(sd->hd) && !pc_checkskill(sd, AM_CALLHOMUN))
8785 hom_vaporize(sd, HOM_ST_ACTIVE);
8786
8787 if (sd->sc.data[SC_SPRITEMABLE] && !pc_checkskill(sd, SU_SPRITEMABLE))
8788 status_change_end(&sd->bl, SC_SPRITEMABLE, INVALID_TIMER);
8789
8790 if(sd->status.manner < 0)
8791 clif_changestatus(sd,SP_MANNER,sd->status.manner);
8792
8793 status_calc_pc(sd,SCO_FORCE);
8794 pc_checkallowskill(sd);
8795 pc_equiplookall(sd);
8796 pc_show_questinfo(sd);
8797 achievement_update_objective(sd, AG_JOB_CHANGE, 2, sd->status.base_level, job);
8798 if( sd->status.party_id ){
8799 struct party_data* p;
8800
8801 if( ( p = party_search( sd->status.party_id ) ) != NULL ){
8802 ARR_FIND(0, MAX_PARTY, i, p->party.member[i].char_id == sd->status.char_id);
8803
8804 if( i < MAX_PARTY ){
8805 p->party.member[i].class_ = sd->status.class_;
8806 clif_party_job_and_level(sd);
8807 }
8808 }
8809 }
8810
8811 chrif_save(sd, CSAVE_NORMAL);
8812 //if you were previously famous, not anymore.
8813 if (fame_flag)
8814 chrif_buildfamelist();
8815 else if (sd->status.fame > 0) {
8816 //It may be that now they are famous?
8817 switch (sd->class_&MAPID_UPPERMASK) {
8818 case MAPID_BLACKSMITH:
8819 case MAPID_ALCHEMIST:
8820 case MAPID_TAEKWON:
8821 chrif_buildfamelist();
8822 break;
8823 }
8824 }
8825
8826 return true;
8827}
8828
8829/*==========================================
8830 * Tell client player sd has change equipement
8831 *------------------------------------------*/
8832void pc_equiplookall(struct map_session_data *sd)
8833{
8834 nullpo_retv(sd);
8835
8836 clif_changelook(&sd->bl,LOOK_WEAPON,0);
8837 clif_changelook(&sd->bl,LOOK_SHOES,0);
8838 clif_changelook(&sd->bl,LOOK_HEAD_BOTTOM,sd->status.head_bottom);
8839 clif_changelook(&sd->bl,LOOK_HEAD_TOP,sd->status.head_top);
8840 clif_changelook(&sd->bl,LOOK_HEAD_MID,sd->status.head_mid);
8841 clif_changelook(&sd->bl,LOOK_ROBE, sd->status.robe);
8842}
8843
8844/*==========================================
8845 * Tell client player sd has change look (hair,equip...)
8846 *------------------------------------------*/
8847void pc_changelook(struct map_session_data *sd,int type,int val) {
8848 nullpo_retv(sd);
8849
8850 switch(type) {
8851 case LOOK_HAIR: //Use the battle_config limits! [Skotlex]
8852 val = cap_value(val, MIN_HAIR_STYLE, MAX_HAIR_STYLE);
8853
8854 if (sd->status.hair != val) {
8855 sd->status.hair = val;
8856 if (sd->status.guild_id) //Update Guild Window. [Skotlex]
8857 intif_guild_change_memberinfo(sd->status.guild_id, sd->status.account_id, sd->status.char_id,
8858 GMI_HAIR, &sd->status.hair, sizeof(sd->status.hair));
8859 }
8860 break;
8861 case LOOK_WEAPON:
8862 sd->status.weapon = val;
8863 break;
8864 case LOOK_HEAD_BOTTOM:
8865 sd->status.head_bottom = val;
8866 sd->setlook_head_bottom = val;
8867 break;
8868 case LOOK_HEAD_TOP:
8869 sd->status.head_top = val;
8870 sd->setlook_head_top = val;
8871 break;
8872 case LOOK_HEAD_MID:
8873 sd->status.head_mid = val;
8874 sd->setlook_head_mid = val;
8875 break;
8876 case LOOK_HAIR_COLOR: //Use the battle_config limits! [Skotlex]
8877 val = cap_value(val, MIN_HAIR_COLOR, MAX_HAIR_COLOR);
8878
8879 if (sd->status.hair_color != val) {
8880 sd->status.hair_color = val;
8881 if (sd->status.guild_id) //Update Guild Window. [Skotlex]
8882 intif_guild_change_memberinfo(sd->status.guild_id, sd->status.account_id, sd->status.char_id,
8883 GMI_HAIR_COLOR, &sd->status.hair_color, sizeof(sd->status.hair_color));
8884 }
8885 break;
8886 case LOOK_CLOTHES_COLOR: //Use the battle_config limits! [Skotlex]
8887 val = cap_value(val, MIN_CLOTH_COLOR, MAX_CLOTH_COLOR);
8888
8889 sd->status.clothes_color = val;
8890 break;
8891 case LOOK_SHIELD:
8892 sd->status.shield = val;
8893 break;
8894 case LOOK_SHOES:
8895 break;
8896 case LOOK_ROBE:
8897 sd->status.robe = val;
8898 sd->setlook_robe = val;
8899 break;
8900 case LOOK_BODY2:
8901 val = cap_value(val, MIN_BODY_STYLE, MAX_BODY_STYLE);
8902
8903 sd->status.body = val;
8904 break;
8905 }
8906 clif_changelook(&sd->bl, type, val);
8907}
8908
8909/*==========================================
8910 * Give an option (type) to player (sd) and display it to client
8911 *------------------------------------------*/
8912void pc_setoption(struct map_session_data *sd,int type)
8913{
8914 int p_type, new_look=0;
8915 nullpo_retv(sd);
8916 p_type = sd->sc.option;
8917
8918 //Option has to be changed client-side before the class sprite or it won't always work (eg: Wedding sprite) [Skotlex]
8919 sd->sc.option=type;
8920 clif_changeoption(&sd->bl);
8921
8922 if( (type&OPTION_RIDING && !(p_type&OPTION_RIDING)) || (type&OPTION_DRAGON && !(p_type&OPTION_DRAGON) && pc_checkskill(sd,RK_DRAGONTRAINING) > 0) )
8923 { // Mounting
8924 clif_status_load(&sd->bl,EFST_RIDING,1);
8925 status_calc_pc(sd,SCO_NONE);
8926 }
8927 else if( (!(type&OPTION_RIDING) && p_type&OPTION_RIDING) || (!(type&OPTION_DRAGON) && p_type&OPTION_DRAGON && pc_checkskill(sd,RK_DRAGONTRAINING) > 0) )
8928 { // Dismount
8929 clif_status_load(&sd->bl,EFST_RIDING,0);
8930 status_calc_pc(sd,SCO_NONE);
8931 }
8932
8933#ifndef NEW_CARTS
8934 if( type&OPTION_CART && !( p_type&OPTION_CART ) ) { //Cart On
8935 clif_cartlist(sd);
8936 clif_updatestatus(sd, SP_CARTINFO);
8937 if(pc_checkskill(sd, MC_PUSHCART) < 10)
8938 status_calc_pc(sd,SCO_NONE); //Apply speed penalty.
8939 } else if( !( type&OPTION_CART ) && p_type&OPTION_CART ){ //Cart Off
8940 clif_clearcart(sd->fd);
8941 if(pc_checkskill(sd, MC_PUSHCART) < 10)
8942 status_calc_pc(sd,SCO_NONE); //Remove speed penalty.
8943 }
8944#endif
8945
8946 if (type&OPTION_FALCON && !(p_type&OPTION_FALCON)) //Falcon ON
8947 clif_status_load(&sd->bl,EFST_FALCON,1);
8948 else if (!(type&OPTION_FALCON) && p_type&OPTION_FALCON) //Falcon OFF
8949 clif_status_load(&sd->bl,EFST_FALCON,0);
8950
8951 if( (sd->class_&MAPID_THIRDMASK) == MAPID_RANGER ) {
8952 if( type&OPTION_WUGRIDER && !(p_type&OPTION_WUGRIDER) ) { // Mounting
8953 clif_status_load(&sd->bl,EFST_WUGRIDER,1);
8954 status_calc_pc(sd,SCO_NONE);
8955 } else if( !(type&OPTION_WUGRIDER) && p_type&OPTION_WUGRIDER ) { // Dismount
8956 clif_status_load(&sd->bl,EFST_WUGRIDER,0);
8957 status_calc_pc(sd,SCO_NONE);
8958 }
8959 }
8960 if( (sd->class_&MAPID_THIRDMASK) == MAPID_MECHANIC ) {
8961 if( type&OPTION_MADOGEAR && !(p_type&OPTION_MADOGEAR) ) {
8962 status_calc_pc(sd,SCO_NONE);
8963 status_change_end(&sd->bl,SC_MAXIMIZEPOWER,INVALID_TIMER);
8964 status_change_end(&sd->bl,SC_OVERTHRUST,INVALID_TIMER);
8965 status_change_end(&sd->bl,SC_WEAPONPERFECTION,INVALID_TIMER);
8966 status_change_end(&sd->bl,SC_ADRENALINE,INVALID_TIMER);
8967 status_change_end(&sd->bl,SC_CARTBOOST,INVALID_TIMER);
8968 status_change_end(&sd->bl,SC_MELTDOWN,INVALID_TIMER);
8969 status_change_end(&sd->bl,SC_MAXOVERTHRUST,INVALID_TIMER);
8970 pc_bonus_script_clear(sd,BSF_REM_ON_MADOGEAR);
8971 } else if( !(type&OPTION_MADOGEAR) && p_type&OPTION_MADOGEAR ) {
8972 status_calc_pc(sd,SCO_NONE);
8973 status_change_end(&sd->bl,SC_SHAPESHIFT,INVALID_TIMER);
8974 status_change_end(&sd->bl,SC_HOVERING,INVALID_TIMER);
8975 status_change_end(&sd->bl,SC_ACCELERATION,INVALID_TIMER);
8976 status_change_end(&sd->bl,SC_OVERHEAT_LIMITPOINT,INVALID_TIMER);
8977 status_change_end(&sd->bl,SC_OVERHEAT,INVALID_TIMER);
8978 status_change_end(&sd->bl,SC_MAGNETICFIELD,INVALID_TIMER);
8979 status_change_end(&sd->bl,SC_NEUTRALBARRIER_MASTER,INVALID_TIMER);
8980 status_change_end(&sd->bl,SC_STEALTHFIELD_MASTER,INVALID_TIMER);
8981 pc_bonus_script_clear(sd,BSF_REM_ON_MADOGEAR);
8982 }
8983 }
8984
8985 if (type&OPTION_FLYING && !(p_type&OPTION_FLYING))
8986 new_look = JOB_STAR_GLADIATOR2;
8987 else if (!(type&OPTION_FLYING) && p_type&OPTION_FLYING)
8988 new_look = -1;
8989
8990 if (sd->disguise || !new_look)
8991 return; //Disguises break sprite changes
8992
8993 if (new_look < 0) { //Restore normal look.
8994 status_set_viewdata(&sd->bl, sd->status.class_);
8995 new_look = sd->vd.class_;
8996 }
8997
8998 pc_stop_attack(sd); //Stop attacking on new view change (to prevent wedding/santa attacks.
8999 clif_changelook(&sd->bl,LOOK_BASE,new_look);
9000 if (sd->vd.cloth_color)
9001 clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
9002 if( sd->vd.body_style )
9003 clif_changelook(&sd->bl,LOOK_BODY2,sd->vd.body_style);
9004 clif_skillinfoblock(sd); // Skill list needs to be updated after base change.
9005}
9006
9007/**
9008 * Give player a cart
9009 * @param sd Player
9010 * @param type 0:Remove cart, 1 ~ MAX_CARTS: Cart type
9011 **/
9012bool pc_setcart(struct map_session_data *sd,int type) {
9013#ifndef NEW_CARTS
9014 int cart[6] = {0x0000,OPTION_CART1,OPTION_CART2,OPTION_CART3,OPTION_CART4,OPTION_CART5};
9015 int option;
9016#endif
9017 nullpo_retr(false,sd);
9018
9019 if( type < 0 || type > MAX_CARTS )
9020 return false;// Never trust the values sent by the client! [Skotlex]
9021
9022 if( pc_checkskill(sd,MC_PUSHCART) <= 0 && type != 0 )
9023 return false;// Push cart is required
9024
9025#ifdef NEW_CARTS
9026
9027 switch( type ) {
9028 case 0:
9029 if( !sd->sc.data[SC_PUSH_CART] )
9030 return 0;
9031 status_change_end(&sd->bl,SC_PUSH_CART,INVALID_TIMER);
9032 clif_clearcart(sd->fd);
9033 break;
9034 default:/* everything else is an allowed ID so we can move on */
9035 if( !sd->sc.data[SC_PUSH_CART] ) { /* first time, so fill cart data */
9036 clif_cartlist(sd);
9037 status_calc_cart_weight(sd, (e_status_calc_weight_opt)(CALCWT_ITEM|CALCWT_MAXBONUS|CALCWT_CARTSTATE));
9038 }
9039 clif_updatestatus(sd, SP_CARTINFO);
9040 sc_start(&sd->bl, &sd->bl, SC_PUSH_CART, 100, type, 0);
9041 break;
9042 }
9043
9044 if(pc_checkskill(sd, MC_PUSHCART) < 10)
9045 status_calc_pc(sd,SCO_NONE); //Recalc speed penalty.
9046#else
9047 // Update option
9048 option = sd->sc.option;
9049 option &= ~OPTION_CART;// clear cart bits
9050 option |= cart[type]; // set cart
9051 pc_setoption(sd, option);
9052#endif
9053
9054 return true;
9055}
9056
9057/*==========================================
9058 * Give player a falcon
9059 *------------------------------------------*/
9060void pc_setfalcon(struct map_session_data* sd, int flag)
9061{
9062 if( flag ){
9063 if( pc_checkskill(sd,HT_FALCON)>0 ) // add falcon if he have the skill
9064 pc_setoption(sd,sd->sc.option|OPTION_FALCON);
9065 } else if( pc_isfalcon(sd) ){
9066 pc_setoption(sd,sd->sc.option&~OPTION_FALCON); // remove falcon
9067 }
9068}
9069
9070/*==========================================
9071 * Set player riding
9072 *------------------------------------------*/
9073void pc_setriding(struct map_session_data* sd, int flag)
9074{
9075 if( sd->sc.data[SC_ALL_RIDING] )
9076 return;
9077
9078 if( flag ){
9079 if( pc_checkskill(sd,KN_RIDING) > 0 ) // add peco
9080 pc_setoption(sd, sd->sc.option|OPTION_RIDING);
9081 } else if( pc_isriding(sd) ){
9082 pc_setoption(sd, sd->sc.option&~OPTION_RIDING);
9083 }
9084}
9085
9086/*==========================================
9087 * Give player a mado
9088 *------------------------------------------*/
9089void pc_setmadogear(struct map_session_data* sd, int flag)
9090{
9091 if( flag ){
9092 if( pc_checkskill(sd,NC_MADOLICENCE) > 0 )
9093 pc_setoption(sd, sd->sc.option|OPTION_MADOGEAR);
9094 } else if( pc_ismadogear(sd) ){
9095 pc_setoption(sd, sd->sc.option&~OPTION_MADOGEAR);
9096 }
9097}
9098
9099/*==========================================
9100 * Check if player can drop an item
9101 *------------------------------------------*/
9102bool pc_candrop(struct map_session_data *sd, struct item *item)
9103{
9104 if( item && (item->expire_time || (item->bound && !pc_can_give_bounded_items(sd))) )
9105 return false;
9106 if( !pc_can_give_items(sd) || sd->sc.cant.drop) //check if this GM level can drop items
9107 return false;
9108 return (itemdb_isdropable(item, pc_get_group_level(sd)));
9109}
9110
9111/**
9112 * Determines whether a player can attack based on status changes
9113 * Why not use status_check_skilluse?
9114 * "src MAY be null to indicate we shouldn't check it, this is a ground-based skill attack."
9115 * Even ground-based attacks should be blocked by these statuses
9116 * Called from unit_attack and unit_attack_timer_sub
9117 * @retval true Can attack
9118 **/
9119bool pc_can_attack( struct map_session_data *sd, int target_id ) {
9120 nullpo_retr(false, sd);
9121
9122 if( pc_is90overweight(sd) || pc_isridingwug(sd) )
9123 return false;
9124
9125 if( sd->sc.data[SC_BASILICA] ||
9126 sd->sc.data[SC__SHADOWFORM] ||
9127 sd->sc.data[SC_CURSEDCIRCLE_ATKER] ||
9128 sd->sc.data[SC_CURSEDCIRCLE_TARGET] ||
9129 sd->sc.data[SC_CRYSTALIZE] ||
9130 sd->sc.data[SC_ALL_RIDING] || // The client doesn't let you, this is to make cheat-safe
9131 sd->sc.data[SC_TRICKDEAD] ||
9132 (sd->sc.data[SC_VOICEOFSIREN] && sd->sc.data[SC_VOICEOFSIREN]->val2 == target_id) ||
9133 sd->sc.data[SC_BLADESTOP] ||
9134 sd->sc.data[SC_DEEPSLEEP] ||
9135 (sd->sc.data[SC_GRAVITATION] && sd->sc.data[SC_GRAVITATION]->val3 == BCT_SELF) ||
9136 sd->sc.data[SC_KINGS_GRACE] )
9137 return false;
9138
9139 return true;
9140}
9141
9142/*==========================================
9143 * Read '@type' variables (temporary numeric char reg)
9144 *------------------------------------------*/
9145int pc_readreg(struct map_session_data* sd, int64 reg)
9146{
9147 return i64db_iget(sd->regs.vars, reg);
9148}
9149
9150/*==========================================
9151 * Set '@type' variables (temporary numeric char reg)
9152 *------------------------------------------*/
9153bool pc_setreg(struct map_session_data* sd, int64 reg, int val)
9154{
9155 unsigned int index = script_getvaridx(reg);
9156
9157 nullpo_retr(false, sd);
9158
9159 if( val ) {
9160 i64db_iput(sd->regs.vars, reg, val);
9161 if( index )
9162 script_array_update(&sd->regs, reg, false);
9163 } else {
9164 i64db_remove(sd->regs.vars, reg);
9165 if( index )
9166 script_array_update(&sd->regs, reg, true);
9167 }
9168
9169 return true;
9170}
9171
9172/*==========================================
9173 * Read '@type$' variables (temporary string char reg)
9174 *------------------------------------------*/
9175char* pc_readregstr(struct map_session_data* sd, int64 reg)
9176{
9177 struct script_reg_str *p = NULL;
9178
9179 p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg);
9180
9181 return p ? p->value : NULL;
9182}
9183
9184/*==========================================
9185 * Set '@type$' variables (temporary string char reg)
9186 *------------------------------------------*/
9187bool pc_setregstr(struct map_session_data* sd, int64 reg, const char* str)
9188{
9189 struct script_reg_str *p = NULL;
9190 unsigned int index = script_getvaridx(reg);
9191 DBData prev;
9192
9193 nullpo_retr(false, sd);
9194
9195 if( str[0] ) {
9196 p = ers_alloc(str_reg_ers, struct script_reg_str);
9197
9198 p->value = aStrdup(str);
9199 p->flag.type = 1;
9200
9201 if (sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev)) {
9202 p = (struct script_reg_str *)db_data2ptr(&prev);
9203 if( p->value )
9204 aFree(p->value);
9205 ers_free(str_reg_ers, p);
9206 } else {
9207 if( index )
9208 script_array_update(&sd->regs, reg, false);
9209 }
9210 } else {
9211 if (sd->regs.vars->remove(sd->regs.vars, db_i642key(reg), &prev)) {
9212 p = (struct script_reg_str *)db_data2ptr(&prev);
9213 if( p->value )
9214 aFree(p->value);
9215 ers_free(str_reg_ers, p);
9216 if( index )
9217 script_array_update(&sd->regs, reg, true);
9218 }
9219 }
9220
9221 return true;
9222}
9223
9224/**
9225 * Serves the following variable types:
9226 * - 'type' (permanent numeric char reg)
9227 * - '#type' (permanent numeric account reg)
9228 * - '##type' (permanent numeric account reg2)
9229 **/
9230int pc_readregistry(struct map_session_data *sd, int64 reg)
9231{
9232 struct script_reg_num *p = NULL;
9233
9234 if (!sd->vars_ok) {
9235 ShowError("pc_readregistry: Trying to read reg %s before it's been loaded!\n", get_str(script_getvarid(reg)));
9236 //This really shouldn't happen, so it's possible the data was lost somewhere, we should request it again.
9237 //intif->request_registry(sd,type==3?4:type);
9238 set_eof(sd->fd);
9239 return 0;
9240 }
9241
9242 p = (struct script_reg_num *)i64db_get(sd->regs.vars, reg);
9243
9244 return p ? p->value : 0;
9245}
9246
9247/**
9248 * Serves the following variable types:
9249 * - 'type$' (permanent str char reg)
9250 * - '#type$' (permanent str account reg)
9251 * - '##type$' (permanent str account reg2)
9252 **/
9253char* pc_readregistry_str(struct map_session_data *sd, int64 reg)
9254{
9255 struct script_reg_str *p = NULL;
9256
9257 if (!sd->vars_ok) {
9258 ShowError("pc_readregistry_str: Trying to read reg %s before it's been loaded!\n", get_str(script_getvarid(reg)));
9259 //This really shouldn't happen, so it's possible the data was lost somewhere, we should request it again.
9260 //intif->request_registry(sd,type==3?4:type);
9261 set_eof(sd->fd);
9262 return NULL;
9263 }
9264
9265 p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg);
9266
9267 return p ? p->value : NULL;
9268}
9269
9270/**
9271 * Serves the following variable types:
9272 * - 'type' (permanent numeric char reg)
9273 * - '#type' (permanent numeric account reg)
9274 * - '##type' (permanent numeric account reg2)
9275 **/
9276int pc_setregistry(struct map_session_data *sd, int64 reg, int val)
9277{
9278 struct script_reg_num *p = NULL;
9279 const char *regname = get_str(script_getvarid(reg));
9280 unsigned int index = script_getvaridx(reg);
9281
9282 if ( !reg_load && !sd->vars_ok ) {
9283 ShowError("pc_setregistry : refusing to set %s until vars are received.\n", regname);
9284 return 0;
9285 }
9286
9287 if ((p = (struct script_reg_num *)i64db_get(sd->regs.vars, reg))) {
9288 if( val ) {
9289 if( !p->value && index ) /* its a entry that was deleted, so we reset array */
9290 script_array_update(&sd->regs, reg, false);
9291 p->value = val;
9292 } else {
9293 p->value = 0;
9294 if( index )
9295 script_array_update(&sd->regs, reg, true);
9296 }
9297 if (!reg_load)
9298 p->flag.update = 1;/* either way, it will require either delete or replace */
9299 } else if( val ) {
9300 DBData prev;
9301
9302 if( index )
9303 script_array_update(&sd->regs, reg, false);
9304
9305 p = ers_alloc(num_reg_ers, struct script_reg_num);
9306
9307 p->value = val;
9308 if (!reg_load)
9309 p->flag.update = 1;
9310
9311 if (sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev)) {
9312 p = (struct script_reg_num *)db_data2ptr(&prev);
9313 ers_free(num_reg_ers, p);
9314 }
9315 }
9316
9317 if (!reg_load && p)
9318 sd->vars_dirty = true;
9319
9320 return 1;
9321}
9322
9323/**
9324 * Serves the following variable types:
9325 * - 'type$' (permanent str char reg)
9326 * - '#type$' (permanent str account reg)
9327 * - '##type$' (permanent str account reg2)
9328 **/
9329int pc_setregistry_str(struct map_session_data *sd, int64 reg, const char *val)
9330{
9331 struct script_reg_str *p = NULL;
9332 const char *regname = get_str(script_getvarid(reg));
9333 unsigned int index = script_getvaridx(reg);
9334 size_t vlen = 0;
9335
9336 if (!reg_load && !sd->vars_ok) {
9337 ShowError("pc_setregistry_str : refusing to set %s until vars are received.\n", regname);
9338 return 0;
9339 }
9340
9341 if ( !script_check_RegistryVariableLength(1, val, &vlen ) )
9342 {
9343 ShowError("pc_check_RegistryVariableLength: Variable value length is too long (aid: %d, cid: %d): '%s' sz=%zu\n", sd->status.account_id, sd->status.char_id, val, vlen);
9344 return 0;
9345 }
9346
9347 if( (p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg) ) ) {
9348 if( val[0] ) {
9349 if( p->value )
9350 aFree(p->value);
9351 else if ( index ) // an entry that was deleted, so we reset
9352 script_array_update(&sd->regs, reg, false);
9353 p->value = aStrdup(val);
9354 } else {
9355 if (p->value)
9356 aFree(p->value);
9357 p->value = NULL;
9358 if( index )
9359 script_array_update(&sd->regs, reg, true);
9360 }
9361 if( !reg_load )
9362 p->flag.update = 1; // either way, it will require either delete or replace
9363 } else if( val[0] ) {
9364 DBData prev;
9365
9366 if( index )
9367 script_array_update(&sd->regs, reg, false);
9368
9369 p = ers_alloc(str_reg_ers, struct script_reg_str);
9370
9371 p->value = aStrdup(val);
9372 if( !reg_load )
9373 p->flag.update = 1;
9374 p->flag.type = 1;
9375
9376 if( sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev) ) {
9377 p = (struct script_reg_str *)db_data2ptr(&prev);
9378 if( p->value )
9379 aFree(p->value);
9380 ers_free(str_reg_ers, p);
9381 }
9382 }
9383
9384 if( !reg_load && p )
9385 sd->vars_dirty = true;
9386
9387 return 1;
9388}
9389
9390/**
9391 * Set value of player variable
9392 * @param sd Player
9393 * @param reg Variable name
9394 * @param value
9395 * @return True if success, false if failed.
9396 **/
9397bool pc_setreg2(struct map_session_data *sd, const char *reg, int val) {
9398 char prefix = reg[0];
9399
9400 nullpo_retr(false, sd);
9401
9402 if (reg[strlen(reg)-1] == '$') {
9403 ShowError("pc_setreg2: Invalid variable scope '%s'\n", reg);
9404 return false;
9405 }
9406
9407 val = cap_value(val, INT_MIN, INT_MAX);
9408
9409 switch (prefix) {
9410 case '.':
9411 case '\'':
9412 case '$':
9413 ShowError("pc_setreg2: Invalid variable scope '%s'\n", reg);
9414 return false;
9415 case '@':
9416 return pc_setreg(sd, add_str(reg), val);
9417 case '#':
9418 return (reg[1] == '#') ? pc_setaccountreg2(sd, add_str(reg), val) > 0 : pc_setaccountreg(sd, add_str(reg), val) > 0;
9419 default:
9420 return pc_setglobalreg(sd, add_str(reg), val) > 0;
9421 }
9422
9423 return false;
9424}
9425
9426/**
9427 * Get value of player variable
9428 * @param sd Player
9429 * @param reg Variable name
9430 * @return Variable value or 0 if failed.
9431 **/
9432int pc_readreg2(struct map_session_data *sd, const char *reg) {
9433 char prefix = reg[0];
9434
9435 nullpo_ret(sd);
9436
9437 if (reg[strlen(reg)-1] == '$') {
9438 ShowError("pc_readreg2: Invalid variable scope '%s'\n", reg);
9439 return 0;
9440 }
9441
9442 switch (prefix) {
9443 case '.':
9444 case '\'':
9445 case '$':
9446 ShowError("pc_readreg2: Invalid variable scope '%s'\n", reg);
9447 return 0;
9448 case '@':
9449 return pc_readreg(sd, add_str(reg));
9450 case '#':
9451 return (reg[1] == '#') ? pc_readaccountreg2(sd, add_str(reg)) : pc_readaccountreg(sd, add_str(reg));
9452 default:
9453 return pc_readglobalreg(sd, add_str(reg));
9454 }
9455
9456 return 0;
9457}
9458
9459/*==========================================
9460 * Exec eventtimer for player sd (retrieved from map_session (id))
9461 *------------------------------------------*/
9462static TIMER_FUNC(pc_eventtimer){
9463 struct map_session_data *sd=map_id2sd(id);
9464 char *p = (char *)data;
9465 int i;
9466 if(sd==NULL)
9467 return 0;
9468
9469 ARR_FIND( 0, MAX_EVENTTIMER, i, sd->eventtimer[i] == tid );
9470 if( i < MAX_EVENTTIMER )
9471 {
9472 sd->eventtimer[i] = INVALID_TIMER;
9473 sd->eventcount--;
9474 npc_event(sd,p,0);
9475 }
9476 else
9477 ShowError("pc_eventtimer: no such event timer\n");
9478
9479 if (p) aFree(p);
9480 return 0;
9481}
9482
9483/*==========================================
9484 * Add eventtimer for player sd ?
9485 *------------------------------------------*/
9486bool pc_addeventtimer(struct map_session_data *sd,int tick,const char *name)
9487{
9488 int i;
9489 nullpo_retr(false,sd);
9490
9491 ARR_FIND( 0, MAX_EVENTTIMER, i, sd->eventtimer[i] == INVALID_TIMER );
9492 if( i == MAX_EVENTTIMER )
9493 return false;
9494
9495 sd->eventtimer[i] = add_timer(gettick()+tick, pc_eventtimer, sd->bl.id, (intptr_t)aStrdup(name));
9496 sd->eventcount++;
9497
9498 return true;
9499}
9500
9501/*==========================================
9502 * Del eventtimer for player sd ?
9503 *------------------------------------------*/
9504bool pc_deleventtimer(struct map_session_data *sd,const char *name)
9505{
9506 char* p = NULL;
9507 int i;
9508
9509 nullpo_retr(false,sd);
9510 if (sd->eventcount == 0)
9511 return false;
9512
9513 // find the named event timer
9514 ARR_FIND( 0, MAX_EVENTTIMER, i,
9515 sd->eventtimer[i] != INVALID_TIMER &&
9516 (p = (char *)(get_timer(sd->eventtimer[i])->data)) != NULL &&
9517 strcmp(p, name) == 0
9518 );
9519 if( i == MAX_EVENTTIMER )
9520 return false; // not found
9521
9522 delete_timer(sd->eventtimer[i],pc_eventtimer);
9523 sd->eventtimer[i] = INVALID_TIMER;
9524 if(sd->eventcount > 0)
9525 sd->eventcount--;
9526 aFree(p);
9527
9528 return true;
9529}
9530
9531/*==========================================
9532 * Update eventtimer count for player sd
9533 *------------------------------------------*/
9534void pc_addeventtimercount(struct map_session_data *sd,const char *name,int tick)
9535{
9536 int i;
9537
9538 nullpo_retv(sd);
9539
9540 for(i=0;i<MAX_EVENTTIMER;i++)
9541 if( sd->eventtimer[i] != INVALID_TIMER && strcmp(
9542 (char *)(get_timer(sd->eventtimer[i])->data), name)==0 ){
9543 addtick_timer(sd->eventtimer[i],tick);
9544 break;
9545 }
9546}
9547
9548/*==========================================
9549 * Remove all eventtimer for player sd
9550 *------------------------------------------*/
9551void pc_cleareventtimer(struct map_session_data *sd)
9552{
9553 int i;
9554
9555 nullpo_retv(sd);
9556
9557 if (sd->eventcount == 0)
9558 return;
9559
9560 for(i=0;i<MAX_EVENTTIMER;i++){
9561 if( sd->eventtimer[i] != INVALID_TIMER ){
9562 char *p = (char *)(get_timer(sd->eventtimer[i])->data);
9563 delete_timer(sd->eventtimer[i],pc_eventtimer);
9564 sd->eventtimer[i] = INVALID_TIMER;
9565 if(sd->eventcount > 0) //avoid looping to max val
9566 sd->eventcount--;
9567 if (p) aFree(p);
9568 }
9569 }
9570}
9571
9572/**
9573* Called when an item with combo is worn
9574* @param *sd
9575* @param *data struct item_data
9576* @return success numbers of succeed combo
9577*/
9578static int pc_checkcombo(struct map_session_data *sd, struct item_data *data) {
9579 uint16 i;
9580 int success = 0;
9581 for( i = 0; i < data->combos_count; i++ ) {
9582 struct itemchk {
9583 int idx;
9584 unsigned short nameid;
9585 short card[MAX_SLOTS];
9586 } *combo_idx;
9587 int idx, j;
9588 int nb_itemCombo;
9589 unsigned int pos = 0;
9590 /* ensure this isn't a duplicate combo */
9591 if( sd->combos.bonus != NULL ) {
9592 int x;
9593 ARR_FIND( 0, sd->combos.count, x, sd->combos.id[x] == data->combos[i]->id );
9594 /* found a match, skip this combo */
9595 if( x < sd->combos.count )
9596 continue;
9597 }
9598
9599 nb_itemCombo = data->combos[i]->count;
9600 if(nb_itemCombo<2) //a combo with less then 2 item ?? how that possible
9601 continue;
9602 CREATE(combo_idx,struct itemchk,nb_itemCombo);
9603 for(j=0; j < nb_itemCombo; j++){
9604 combo_idx[j].idx=-1;
9605 combo_idx[j].nameid=-1;
9606 memset(combo_idx[j].card,-1,MAX_SLOTS);
9607 }
9608
9609 for( j = 0; j < nb_itemCombo; j++ ) {
9610 uint16 id = data->combos[i]->nameid[j], k;
9611 bool found = false;
9612
9613 for( k = 0; k < EQI_MAX; k++ ) {
9614 short index = sd->equip_index[k];
9615 if( index < 0 )
9616 continue;
9617 if( pc_is_same_equip_index((enum equip_index)k, sd->equip_index, index) )
9618 continue;
9619 if (!sd->inventory_data[index] )
9620 continue;
9621
9622 if ( itemdb_type(id) != IT_CARD ) {
9623 if ( sd->inventory_data[index]->nameid != id )
9624 continue;
9625 if(j>0){ //check if this item not already used
9626 bool do_continue = false; //used to continue that specific loop with some check that also use some loop
9627 uint8 z;
9628 for (z = 0; z < nb_itemCombo-1; z++)
9629 if(combo_idx[z].idx == index && combo_idx[z].nameid == id) //we already have that index recorded
9630 do_continue=true;
9631 if(do_continue)
9632 continue;
9633 }
9634 combo_idx[j].nameid = id;
9635 combo_idx[j].idx = index;
9636 pos |= sd->inventory.u.items_inventory[index].equip;
9637 found = true;
9638 break;
9639 } else { //Cards and enchants
9640 uint16 z;
9641 if ( itemdb_isspecial(sd->inventory.u.items_inventory[index].card[0]) )
9642 continue;
9643 for (z = 0; z < MAX_SLOTS; z++) {
9644 bool do_continue=false;
9645 if (sd->inventory.u.items_inventory[index].card[z] != id)
9646 continue;
9647 if(j>0){
9648 int c1, c2;
9649 for (c1 = 0; c1 < nb_itemCombo-1; c1++){
9650 if(combo_idx[c1].idx == index && combo_idx[c1].nameid == id){
9651 for (c2 = 0; c2 < MAX_SLOTS; c2++){
9652 if(combo_idx[c1].card[c2] == id){ //we already have that card recorded (at this same idx)
9653 do_continue = true;
9654 break;
9655 }
9656 }
9657 }
9658 }
9659 }
9660 if(do_continue)
9661 continue;
9662 combo_idx[j].nameid = id;
9663 combo_idx[j].idx = index;
9664 combo_idx[j].card[z] = id;
9665 pos |= sd->inventory.u.items_inventory[index].equip;
9666 found = true;
9667 break;
9668 }
9669 }
9670 }
9671 if( !found )
9672 break;/* we haven't found all the ids for this combo, so we can return */
9673 }
9674 aFree(combo_idx);
9675 /* means we broke out of the count loop w/o finding all ids, we can move to the next combo */
9676 if( j < nb_itemCombo )
9677 continue;
9678 /* we got here, means all items in the combo are matching */
9679 idx = sd->combos.count;
9680 if( sd->combos.bonus == NULL ) {
9681 CREATE(sd->combos.bonus, struct script_code *, 1);
9682 CREATE(sd->combos.id, unsigned short, 1);
9683 CREATE(sd->combos.pos, unsigned int, 1);
9684 sd->combos.count = 1;
9685 } else {
9686 RECREATE(sd->combos.bonus, struct script_code *, ++sd->combos.count);
9687 RECREATE(sd->combos.id, unsigned short, sd->combos.count);
9688 RECREATE(sd->combos.pos, unsigned int, sd->combos.count);
9689 }
9690 /* we simply copy the pointer */
9691 sd->combos.bonus[idx] = data->combos[i]->script;
9692 /* save this combo's id */
9693 sd->combos.id[idx] = data->combos[i]->id;
9694 /* save pos of combo*/
9695 sd->combos.pos[idx] = pos;
9696 success++;
9697 }
9698 return success;
9699}
9700
9701/**
9702* Called when an item with combo is removed
9703* @param *sd
9704* @param *data struct item_data
9705* @return retval numbers of removed combo
9706*/
9707static int pc_removecombo(struct map_session_data *sd, struct item_data *data ) {
9708 int i, retval = 0;
9709
9710 if( sd->combos.bonus == NULL )
9711 return 0;/* nothing to do here, player has no combos */
9712 for( i = 0; i < data->combos_count; i++ ) {
9713 /* check if this combo exists in this user */
9714 int x = 0, cursor = 0, j;
9715 ARR_FIND( 0, sd->combos.count, x, sd->combos.id[x] == data->combos[i]->id );
9716 /* no match, skip this combo */
9717 if(x >= sd->combos.count)
9718 continue;
9719
9720 sd->combos.bonus[x] = NULL;
9721 sd->combos.id[x] = 0;
9722 sd->combos.pos[x] = 0;
9723 retval++;
9724
9725 /* check if combo requirements still fit */
9726 if( pc_checkcombo( sd, data ) )
9727 continue;
9728
9729 /* move next value to empty slot */
9730 for( j = 0, cursor = 0; j < sd->combos.count; j++ ) {
9731 if( sd->combos.bonus[j] == NULL )
9732 continue;
9733
9734 if( cursor != j ) {
9735 sd->combos.bonus[cursor] = sd->combos.bonus[j];
9736 sd->combos.id[cursor] = sd->combos.id[j];
9737 sd->combos.pos[cursor] = sd->combos.pos[j];
9738 }
9739 cursor++;
9740 }
9741
9742 /* it's empty, we can clear all the memory */
9743 if( (sd->combos.count = cursor) == 0 ) {
9744 aFree(sd->combos.bonus);
9745 aFree(sd->combos.id);
9746 aFree(sd->combos.pos);
9747 sd->combos.bonus = NULL;
9748 sd->combos.id = NULL;
9749 sd->combos.pos = NULL;
9750 return retval; /* we also can return at this point for we have no more combos to check */
9751 }
9752 }
9753
9754 return retval;
9755}
9756
9757/**
9758* Load combo data(s) of player
9759* @param *sd
9760* @return ret numbers of succeed combo
9761*/
9762int pc_load_combo(struct map_session_data *sd) {
9763 int i, ret = 0;
9764 for( i = 0; i < EQI_MAX; i++ ) {
9765 struct item_data *id = NULL;
9766 short idx = sd->equip_index[i];
9767 if( idx < 0 || !(id = sd->inventory_data[idx] ) )
9768 continue;
9769 if( id->combos_count )
9770 ret += pc_checkcombo(sd,id);
9771 if(!itemdb_isspecial(sd->inventory.u.items_inventory[idx].card[0])) {
9772 struct item_data *data;
9773 int j;
9774 for( j = 0; j < id->slot; j++ ) {
9775 if (!sd->inventory.u.items_inventory[idx].card[j])
9776 continue;
9777 if ( ( data = itemdb_exists(sd->inventory.u.items_inventory[idx].card[j]) ) != NULL ) {
9778 if( data->combos_count )
9779 ret += pc_checkcombo(sd,data);
9780 }
9781 }
9782 }
9783 }
9784 return ret;
9785}
9786/*==========================================
9787 * Equip item on player sd at req_pos from inventory index n
9788 * return: false - fail; true - success
9789 *------------------------------------------*/
9790bool pc_equipitem(struct map_session_data *sd,short n,int req_pos)
9791{
9792 int i, pos, flag = 0, iflag;
9793 struct item_data *id;
9794 uint8 res = ITEM_EQUIP_ACK_OK;
9795
9796 nullpo_retr(false,sd);
9797
9798 if( n < 0 || n >= MAX_INVENTORY ) {
9799 clif_equipitemack(sd,0,0,ITEM_EQUIP_ACK_FAIL);
9800 return false;
9801 }
9802 if( DIFF_TICK(sd->canequip_tick,gettick()) > 0 ) {
9803 clif_equipitemack(sd,n,0,ITEM_EQUIP_ACK_FAIL);
9804 return false;
9805 }
9806
9807 if (!(id = sd->inventory_data[n]))
9808 return false;
9809 pos = pc_equippoint(sd,n); //With a few exceptions, item should go in all specified slots.
9810
9811 if(battle_config.battle_log)
9812 ShowInfo("equip %hu (%d) %x:%x\n",sd->inventory.u.items_inventory[n].nameid,n,id?id->equip:0,req_pos);
9813
9814 if((res = pc_isequip(sd,n))) {
9815 clif_equipitemack(sd,n,0,res); // fail
9816 return false;
9817 }
9818
9819 if (!(pos&req_pos) || sd->inventory.u.items_inventory[n].equip != 0 || sd->inventory.u.items_inventory[n].attribute==1 ) { // [Valaris]
9820 clif_equipitemack(sd,n,0,ITEM_EQUIP_ACK_FAIL); // fail
9821 return false;
9822 }
9823 if( sd->sc.count && (sd->sc.data[SC_BERSERK] || sd->sc.data[SC_SATURDAYNIGHTFEVER] ||
9824 sd->sc.data[SC_KYOUGAKU] || (sd->sc.data[SC_PYROCLASTIC] && sd->inventory_data[n]->type == IT_WEAPON)) ) {
9825 clif_equipitemack(sd,n,0,ITEM_EQUIP_ACK_FAIL); //Fail
9826 return false;
9827 }
9828
9829 if (id->flag.bindOnEquip && !sd->inventory.u.items_inventory[n].bound) {
9830 sd->inventory.u.items_inventory[n].bound = (char)battle_config.default_bind_on_equip;
9831 clif_notify_bindOnEquip(sd,n);
9832 }
9833
9834 if(pos == EQP_ACC) { //Accesories should only go in one of the two,
9835 pos = req_pos&EQP_ACC;
9836 if (pos == EQP_ACC) //User specified both slots..
9837 pos = sd->equip_index[EQI_ACC_R] >= 0 ? EQP_ACC_L : EQP_ACC_R;
9838 }
9839
9840 if(pos == EQP_ARMS && id->equip == EQP_HAND_R) { //Dual wield capable weapon.
9841 pos = (req_pos&EQP_ARMS);
9842 if (pos == EQP_ARMS) //User specified both slots, pick one for them.
9843 pos = sd->equip_index[EQI_HAND_R] >= 0 ? EQP_HAND_L : EQP_HAND_R;
9844 }
9845
9846 if(pos == EQP_SHADOW_ACC) { // Shadow System
9847 pos = req_pos&EQP_SHADOW_ACC;
9848 if (pos == EQP_SHADOW_ACC)
9849 pos = sd->equip_index[EQI_SHADOW_ACC_L] >= 0 ? EQP_SHADOW_ACC_R : EQP_SHADOW_ACC_L;
9850 }
9851 if(pos == EQP_SHADOW_ARMS && id->equip == EQP_SHADOW_WEAPON) {
9852 pos = (req_pos&EQP_SHADOW_ARMS);
9853 if( pos == EQP_SHADOW_ARMS )
9854 pos = (sd->equip_index[EQI_SHADOW_WEAPON] >= 0 ? EQP_SHADOW_SHIELD : EQP_SHADOW_WEAPON);
9855 }
9856
9857 if (pos&EQP_HAND_R && battle_config.use_weapon_skill_range&BL_PC) {
9858 //Update skill-block range database when weapon range changes. [Skotlex]
9859 i = sd->equip_index[EQI_HAND_R];
9860 if (i < 0 || !sd->inventory_data[i]) //No data, or no weapon equipped
9861 flag = 1;
9862 else
9863 flag = id->range != sd->inventory_data[i]->range;
9864 }
9865
9866 for(i=0;i<EQI_MAX;i++) {
9867 if(pos & equip_bitmask[i]) {
9868 if(sd->equip_index[i] >= 0) //Slot taken, remove item from there.
9869 pc_unequipitem(sd,sd->equip_index[i],2 | 4);
9870
9871 sd->equip_index[i] = n;
9872 }
9873 }
9874
9875 if(pos==EQP_AMMO) {
9876 clif_arrowequip(sd,n);
9877 clif_arrow_fail(sd,3);
9878 }
9879 else
9880 clif_equipitemack(sd,n,pos,ITEM_EQUIP_ACK_OK);
9881
9882 sd->inventory.u.items_inventory[n].equip = pos;
9883
9884 if(pos & EQP_HAND_R) {
9885 if(id)
9886 sd->weapontype1 = id->look;
9887 else
9888 sd->weapontype1 = 0;
9889 pc_calcweapontype(sd);
9890 clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
9891 }
9892 if(pos & EQP_HAND_L) {
9893 if(id) {
9894 if(id->type == IT_WEAPON) {
9895 sd->status.shield = 0;
9896 sd->weapontype2 = id->look;
9897 }
9898 else
9899 if(id->type == IT_ARMOR) {
9900 sd->status.shield = id->look;
9901 sd->weapontype2 = 0;
9902 }
9903 }
9904 else
9905 sd->status.shield = sd->weapontype2 = 0;
9906 pc_calcweapontype(sd);
9907 clif_changelook(&sd->bl,LOOK_SHIELD,sd->status.shield);
9908 }
9909 if(pos & EQP_SHOES)
9910 clif_changelook(&sd->bl,LOOK_SHOES,0);
9911
9912 if (battle_config.ammo_unequip && (pos&EQP_ARMS) && id->type == IT_WEAPON) {
9913 short idx = sd->equip_index[EQI_AMMO];
9914
9915 if (idx >= 0) {
9916 switch (sd->inventory_data[idx]->look) {
9917 case AMMO_ARROW:
9918 if (id->look != W_BOW && id->look != W_MUSICAL && id->look != W_WHIP)
9919 pc_unequipitem(sd, idx, 2 | 4);
9920 break;
9921 case AMMO_BULLET:
9922 case AMMO_SHELL:
9923 if (id->look != W_REVOLVER && id->look != W_RIFLE && id->look != W_GATLING && id->look != W_SHOTGUN
9924#ifdef RENEWAL
9925 && id->look != W_GRENADE
9926#endif
9927 )
9928 pc_unequipitem(sd, idx, 2 | 4);
9929 break;
9930#ifndef RENEWAL
9931 case AMMO_GRENADE:
9932 if (id->look != W_GRENADE)
9933 pc_unequipitem(sd, idx, 2 | 4);
9934 break;
9935#endif
9936 }
9937 }
9938 }
9939
9940 pc_set_costume_view(sd);
9941
9942 pc_checkallowskill(sd); //Check if status changes should be halted.
9943 iflag = sd->npc_item_flag;
9944
9945 /* check for combos (MUST be before status_calc_pc) */
9946 if( id->combos_count )
9947 pc_checkcombo(sd,id);
9948 if(itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
9949 ; //No cards
9950 else {
9951 for( i = 0; i < MAX_SLOTS; i++ ) {
9952 struct item_data *data;
9953 if (!sd->inventory.u.items_inventory[n].card[i])
9954 continue;
9955 if ( ( data = itemdb_exists(sd->inventory.u.items_inventory[n].card[i]) ) != NULL ) {
9956 if( data->combos_count )
9957 pc_checkcombo(sd,data);
9958 }
9959 }
9960 }
9961
9962 status_calc_pc(sd,SCO_NONE);
9963 if (flag) //Update skill data
9964 clif_skillinfoblock(sd);
9965
9966 //OnEquip script [Skotlex]
9967 if (id) {
9968 //only run the script if item isn't restricted
9969 if (id->equip_script && (pc_has_permission(sd,PC_PERM_USE_ALL_EQUIPMENT) || !itemdb_isNoEquip(id,sd->bl.m)))
9970 run_script(id->equip_script,0,sd->bl.id,fake_nd->bl.id);
9971 if(itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
9972 ; //No cards
9973 else {
9974 for( i = 0; i < MAX_SLOTS; i++ ) {
9975 struct item_data *data;
9976 if (!sd->inventory.u.items_inventory[n].card[i])
9977 continue;
9978 if ( ( data = itemdb_exists(sd->inventory.u.items_inventory[n].card[i]) ) != NULL ) {
9979 if (data->equip_script && (pc_has_permission(sd,PC_PERM_USE_ALL_EQUIPMENT) || !itemdb_isNoEquip(data,sd->bl.m)))
9980 run_script(data->equip_script,0,sd->bl.id,fake_nd->bl.id);
9981 }
9982 }
9983 }
9984 }
9985 sd->npc_item_flag = iflag;
9986
9987 return true;
9988}
9989
9990/**
9991 * Recalculate player status on unequip
9992 * @param sd: Player data
9993 * @param n: Item inventory index
9994 * @param flag: Whether to recalculate a player's status or not
9995 * @return True on success or false on failure
9996 */
9997static void pc_unequipitem_sub(struct map_session_data *sd, int n, int flag) {
9998 int i, iflag;
9999 bool status_calc = false;
10000
10001 if (sd->state.autobonus&sd->inventory.u.items_inventory[n].equip)
10002 sd->state.autobonus &= ~sd->inventory.u.items_inventory[n].equip; //Check for activated autobonus [Inkfish]
10003
10004 sd->inventory.u.items_inventory[n].equip = 0;
10005 pc_checkallowskill(sd);
10006 iflag = sd->npc_item_flag;
10007
10008 /* check for combos (MUST be before status_calc_pc) */
10009 if (sd->inventory_data[n]) {
10010 if (sd->inventory_data[n]->combos_count) {
10011 if (pc_removecombo(sd, sd->inventory_data[n]))
10012 status_calc = true;
10013 }
10014 if (itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
10015 ; //No cards
10016 else {
10017 for (i = 0; i < MAX_SLOTS; i++) {
10018 struct item_data *data;
10019
10020 if (!sd->inventory.u.items_inventory[n].card[i])
10021 continue;
10022 if ((data = itemdb_exists(sd->inventory.u.items_inventory[n].card[i])) != NULL) {
10023 if (data->combos_count) {
10024 if (pc_removecombo(sd, data))
10025 status_calc = true;
10026 }
10027 }
10028 }
10029 }
10030 }
10031
10032 if (status_calc)
10033 status_calc_pc(sd, SCO_NONE);
10034
10035 if (sd->sc.data[SC_SIGNUMCRUCIS] && !battle_check_undead(sd->battle_status.race, sd->battle_status.def_ele))
10036 status_change_end(&sd->bl, SC_SIGNUMCRUCIS, INVALID_TIMER);
10037
10038 //OnUnEquip script [Skotlex]
10039 if (sd->inventory_data[n]) {
10040 if (sd->inventory_data[n]->unequip_script)
10041 run_script(sd->inventory_data[n]->unequip_script, 0, sd->bl.id, fake_nd->bl.id);
10042 if (itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
10043 ; //No cards
10044 else {
10045 for (i = 0; i < MAX_SLOTS; i++) {
10046 struct item_data *data;
10047 if (!sd->inventory.u.items_inventory[n].card[i])
10048 continue;
10049
10050 if ((data = itemdb_exists(sd->inventory.u.items_inventory[n].card[i])) != NULL) {
10051 if (data->unequip_script)
10052 run_script(data->unequip_script, 0, sd->bl.id, fake_nd->bl.id);
10053 }
10054
10055 }
10056 }
10057 }
10058
10059 if (flag & 1)
10060 status_calc_pc(sd, SCO_FORCE);
10061 sd->npc_item_flag = iflag;
10062}
10063
10064/**
10065 * Called when attempting to unequip an item from a player
10066 * @param sd: Player data
10067 * @param n: Item inventory index
10068 * @param flag: Type of unequip
10069 * 0 - only unequip
10070 * 1 - calculate status after unequipping
10071 * 2 - force unequip
10072 * 4 - unequip by switching equipment
10073 * @return True on success or false on failure
10074 */
10075bool pc_unequipitem(struct map_session_data *sd, int n, int flag) {
10076 int i, pos;
10077
10078 nullpo_retr(false,sd);
10079
10080 if (n < 0 || n >= MAX_INVENTORY) {
10081 clif_unequipitemack(sd,0,0,0);
10082 return false;
10083 }
10084 if (!(pos = sd->inventory.u.items_inventory[n].equip)) {
10085 clif_unequipitemack(sd,n,0,0);
10086 return false; //Nothing to unequip
10087 }
10088 // status change that makes player cannot unequip equipment
10089 if (!(flag&2) && sd->sc.count &&
10090 (sd->sc.data[SC_BERSERK] ||
10091 sd->sc.data[SC_SATURDAYNIGHTFEVER] ||
10092 sd->sc.data[SC__BLOODYLUST] ||
10093 sd->sc.data[SC_KYOUGAKU] ||
10094 (sd->sc.data[SC_PYROCLASTIC] &&
10095 sd->inventory_data[n]->type == IT_WEAPON))) // can't switch weapon
10096 {
10097 clif_unequipitemack(sd,n,0,0);
10098 return false;
10099 }
10100
10101 if (battle_config.battle_log)
10102 ShowInfo("unequip %d %x:%x\n",n,pc_equippoint(sd,n),pos);
10103
10104 for(i = 0; i < EQI_MAX; i++) {
10105 if (pos & equip_bitmask[i])
10106 sd->equip_index[i] = -1;
10107 }
10108
10109 if(pos & EQP_HAND_R) {
10110 sd->weapontype1 = 0;
10111 sd->status.weapon = sd->weapontype2;
10112 pc_calcweapontype(sd);
10113 clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
10114 if( !battle_config.dancing_weaponswitch_fix )
10115 status_change_end(&sd->bl, SC_DANCING, INVALID_TIMER); // Unequipping => stop dancing.
10116#ifdef RENEWAL
10117 if (battle_config.switch_remove_edp&2) {
10118#else
10119 if (battle_config.switch_remove_edp&1) {
10120#endif
10121 status_change_end(&sd->bl, SC_EDP, INVALID_TIMER);
10122 }
10123 }
10124 if(pos & EQP_HAND_L) {
10125 if (sd->status.shield && battle_getcurrentskill(&sd->bl) == LG_SHIELDSPELL)
10126 unit_skillcastcancel(&sd->bl, 0); // Cancel Shield Spell if player swaps shields.
10127
10128 sd->status.shield = sd->weapontype2 = 0;
10129 pc_calcweapontype(sd);
10130 clif_changelook(&sd->bl,LOOK_SHIELD,sd->status.shield);
10131 }
10132
10133 if(pos & EQP_SHOES)
10134 clif_changelook(&sd->bl,LOOK_SHOES,0);
10135
10136 clif_unequipitemack(sd,n,pos,1);
10137 pc_set_costume_view(sd);
10138
10139 status_change_end(&sd->bl,SC_HEAT_BARREL,INVALID_TIMER);
10140 // On weapon change (right and left hand)
10141 if ((pos & EQP_ARMS) && sd->inventory_data[n]->type == IT_WEAPON) {
10142 if (battle_config.ammo_unequip && !(flag & 4)) {
10143 switch (sd->inventory_data[n]->look) {
10144 case W_BOW:
10145 case W_MUSICAL:
10146 case W_WHIP:
10147 case W_REVOLVER:
10148 case W_RIFLE:
10149 case W_GATLING:
10150 case W_SHOTGUN:
10151 case W_GRENADE: {
10152 short idx = sd->equip_index[EQI_AMMO];
10153
10154 if (idx >= 0) {
10155 sd->equip_index[EQI_AMMO] = -1;
10156 clif_unequipitemack(sd, idx, sd->inventory.u.items_inventory[idx].equip, 1);
10157 pc_unequipitem_sub(sd, idx, 0);
10158 }
10159 }
10160 break;
10161 }
10162 }
10163 if (!sd->sc.data[SC_SEVENWIND] || sd->sc.data[SC_ASPERSIO]) //Check for seven wind (but not level seven!)
10164 skill_enchant_elemental_end(&sd->bl, SC_NONE);
10165 status_change_end(&sd->bl, SC_FEARBREEZE, INVALID_TIMER);
10166 status_change_end(&sd->bl, SC_EXEEDBREAK, INVALID_TIMER);
10167 }
10168
10169 // On armor change
10170 if (pos & EQP_ARMOR) {
10171 if (sd->sc.data[SC_HOVERING] && sd->inventory_data[n]->nameid == ITEMID_HOVERING_BOOSTER)
10172 status_change_end(&sd->bl, SC_HOVERING, INVALID_TIMER);
10173 //status_change_end(&sd->bl, SC_BENEDICTIO, INVALID_TIMER); // No longer is removed? Need confirmation
10174 status_change_end(&sd->bl, SC_ARMOR_RESIST, INVALID_TIMER);
10175 }
10176
10177 // On ammo change
10178 if (sd->inventory_data[n]->type == IT_AMMO && (sd->inventory_data[n]->nameid != ITEMID_SILVER_BULLET || sd->inventory_data[n]->nameid != ITEMID_PURIFICATION_BULLET || sd->inventory_data[n]->nameid != ITEMID_SILVER_BULLET_))
10179 status_change_end(&sd->bl, SC_P_ALTER, INVALID_TIMER);
10180
10181 pc_unequipitem_sub(sd, n, flag);
10182
10183 return true;
10184}
10185
10186/*==========================================
10187 * Checking if player (sd) has an invalid item
10188 * and is unequiped on map load (item_noequip)
10189 *------------------------------------------*/
10190void pc_checkitem(struct map_session_data *sd) {
10191 int i, calc_flag = 0;
10192 struct item it;
10193
10194 nullpo_retv(sd);
10195
10196 if( sd->state.vending ) //Avoid reorganizing items when we are vending, as that leads to exploits (pointed out by End of Exam)
10197 return;
10198
10199 pc_check_available_item(sd, ITMCHK_NONE); // Check for invalid(ated) items.
10200
10201 for( i = 0; i < MAX_INVENTORY; i++ ) {
10202 it = sd->inventory.u.items_inventory[i];
10203
10204 if( it.nameid == 0 )
10205 continue;
10206 if( !it.equip )
10207 continue;
10208 if( it.equip&~pc_equippoint(sd,i) ) {
10209 pc_unequipitem(sd, i, 2);
10210 calc_flag = 1;
10211 continue;
10212 }
10213
10214 if( !pc_has_permission(sd, PC_PERM_USE_ALL_EQUIPMENT) && !battle_config.allow_equip_restricted_item && itemdb_isNoEquip(sd->inventory_data[i], sd->bl.m) ) {
10215 pc_unequipitem(sd, i, 2);
10216 calc_flag = 1;
10217 continue;
10218 }
10219 }
10220
10221 if( calc_flag && sd->state.active ) {
10222 pc_checkallowskill(sd);
10223 status_calc_pc(sd,SCO_NONE);
10224 }
10225}
10226
10227/*==========================================
10228 * Checks for unavailable items and removes them.
10229 * @param sd: Player data
10230 * @param type Forced check:
10231 * 1 - Inventory
10232 * 2 - Cart
10233 * 4 - Storage
10234 *------------------------------------------*/
10235void pc_check_available_item(struct map_session_data *sd, uint8 type)
10236{
10237 int i;
10238 unsigned short nameid;
10239 char output[256];
10240
10241 nullpo_retv(sd);
10242
10243 if (battle_config.item_check&ITMCHK_INVENTORY && type&ITMCHK_INVENTORY) { // Check for invalid(ated) items in inventory.
10244 for(i = 0; i < MAX_INVENTORY; i++) {
10245 nameid = sd->inventory.u.items_inventory[i].nameid;
10246
10247 if (!nameid)
10248 continue;
10249 if (!itemdb_available(nameid)) {
10250 sprintf(output, msg_txt(sd, 709), nameid); // Item %hu has been removed from your inventory.
10251 clif_displaymessage(sd->fd, output);
10252 ShowWarning("Removed invalid/disabled item (ID: %hu, amount: %d) from inventory (char_id: %d).\n", nameid, sd->inventory.u.items_inventory[i].amount, sd->status.char_id);
10253 pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 4, 0, LOG_TYPE_OTHER);
10254 continue;
10255 }
10256 if (!sd->inventory.u.items_inventory[i].unique_id && !itemdb_isstackable(nameid))
10257 sd->inventory.u.items_inventory[i].unique_id = pc_generate_unique_id(sd);
10258 }
10259 }
10260
10261 if (battle_config.item_check&ITMCHK_CART && type&ITMCHK_CART) { // Check for invalid(ated) items in cart.
10262 for(i = 0; i < MAX_CART; i++) {
10263 nameid = sd->cart.u.items_cart[i].nameid;
10264
10265 if (!nameid)
10266 continue;
10267 if (!itemdb_available(nameid)) {
10268 sprintf(output, msg_txt(sd, 710), nameid); // Item %hu has been removed from your cart.
10269 clif_displaymessage(sd->fd, output);
10270 ShowWarning("Removed invalid/disabled item (ID: %hu, amount: %d) from cart (char_id: %d).\n", nameid, sd->cart.u.items_cart[i].amount, sd->status.char_id);
10271 pc_cart_delitem(sd, i, sd->cart.u.items_cart[i].amount, 0, LOG_TYPE_OTHER);
10272 continue;
10273 }
10274 if (!sd->cart.u.items_cart[i].unique_id && !itemdb_isstackable(nameid))
10275 sd->cart.u.items_cart[i].unique_id = pc_generate_unique_id(sd);
10276 }
10277 }
10278
10279 if (battle_config.item_check&ITMCHK_STORAGE && type&ITMCHK_STORAGE) { // Check for invalid(ated) items in storage.
10280 for(i = 0; i < sd->storage.max_amount; i++) {
10281 nameid = sd->storage.u.items_storage[i].nameid;
10282
10283 if (!nameid)
10284 continue;
10285 if (!itemdb_available(nameid)) {
10286 sprintf(output, msg_txt(sd, 711), nameid); // Item %hu has been removed from your storage.
10287 clif_displaymessage(sd->fd, output);
10288 ShowWarning("Removed invalid/disabled item (ID: %hu, amount: %d) from storage (char_id: %d).\n", nameid, sd->storage.u.items_storage[i].amount, sd->status.char_id);
10289 storage_delitem(sd, &sd->storage, i, sd->storage.u.items_storage[i].amount);
10290 continue;
10291 }
10292 if (!sd->storage.u.items_storage[i].unique_id && !itemdb_isstackable(nameid))
10293 sd->storage.u.items_storage[i].unique_id = pc_generate_unique_id(sd);
10294 }
10295 }
10296}
10297
10298/*==========================================
10299 * Update PVP rank for sd1 in cmp to sd2
10300 *------------------------------------------*/
10301static int pc_calc_pvprank_sub(struct block_list *bl,va_list ap)
10302{
10303 struct map_session_data *sd1,*sd2;
10304
10305 sd1=(struct map_session_data *)bl;
10306 sd2=va_arg(ap,struct map_session_data *);
10307
10308 if( pc_isinvisible(sd1) || pc_isinvisible(sd2) )
10309 {// cannot register pvp rank for hidden GMs
10310 return 0;
10311 }
10312
10313 if( sd1->pvp_point > sd2->pvp_point )
10314 sd2->pvp_rank++;
10315 return 0;
10316}
10317/*==========================================
10318 * Calculate new rank beetween all present players (map_foreachinallarea)
10319 * and display result
10320 *------------------------------------------*/
10321int pc_calc_pvprank(struct map_session_data *sd)
10322{
10323 int old = sd->pvp_rank;
10324 struct map_data *mapdata = map_getmapdata(sd->bl.m);
10325
10326 sd->pvp_rank=1;
10327 map_foreachinmap(pc_calc_pvprank_sub,sd->bl.m,BL_PC,sd);
10328 if(old!=sd->pvp_rank || sd->pvp_lastusers!=mapdata->users_pvp)
10329 clif_pvpset(sd,sd->pvp_rank,sd->pvp_lastusers=mapdata->users_pvp,0);
10330 return sd->pvp_rank;
10331}
10332/*==========================================
10333 * Calculate next sd ranking calculation from config
10334 *------------------------------------------*/
10335TIMER_FUNC(pc_calc_pvprank_timer){
10336 struct map_session_data *sd;
10337
10338 sd=map_id2sd(id);
10339 if(sd==NULL)
10340 return 0;
10341 sd->pvp_timer = INVALID_TIMER;
10342
10343 if( pc_isinvisible(sd) )
10344 {// do not calculate the pvp rank for a hidden GM
10345 return 0;
10346 }
10347
10348 if( pc_calc_pvprank(sd) > 0 )
10349 sd->pvp_timer = add_timer(gettick()+PVP_CALCRANK_INTERVAL,pc_calc_pvprank_timer,id,data);
10350 return 0;
10351}
10352
10353/*==========================================
10354 * Checking if sd is married
10355 * Return:
10356 * partner_id = yes
10357 * 0 = no
10358 *------------------------------------------*/
10359int pc_ismarried(struct map_session_data *sd)
10360{
10361 if(sd == NULL)
10362 return -1;
10363 if(sd->status.partner_id > 0)
10364 return sd->status.partner_id;
10365 else
10366 return 0;
10367}
10368/*==========================================
10369 * Marry player sd to player dstsd
10370 * Return:
10371 * false = fail
10372 * true = success
10373 *------------------------------------------*/
10374bool pc_marriage(struct map_session_data *sd,struct map_session_data *dstsd)
10375{
10376 if(sd == NULL || dstsd == NULL ||
10377 sd->status.partner_id > 0 || dstsd->status.partner_id > 0 ||
10378 (sd->class_&JOBL_BABY) || (dstsd->class_&JOBL_BABY))
10379 return false;
10380 sd->status.partner_id = dstsd->status.char_id;
10381 dstsd->status.partner_id = sd->status.char_id;
10382
10383 achievement_update_objective(sd, AG_MARRY, 1, 1);
10384 achievement_update_objective(dstsd, AG_MARRY, 1, 1);
10385
10386 return true;
10387}
10388
10389/*==========================================
10390 * Divorce sd from its partner
10391 * Return:
10392 * false = fail
10393 * true = success
10394 *------------------------------------------*/
10395bool pc_divorce(struct map_session_data *sd)
10396{
10397 struct map_session_data *p_sd;
10398 int i;
10399
10400 if( sd == NULL || !pc_ismarried(sd) )
10401 return false;
10402
10403 if( !sd->status.partner_id )
10404 return false; // Char is not married
10405
10406 if( (p_sd = map_charid2sd(sd->status.partner_id)) == NULL )
10407 { // Lets char server do the divorce
10408 if( chrif_divorce(sd->status.char_id, sd->status.partner_id) )
10409 return false; // No char server connected
10410
10411 return true;
10412 }
10413
10414 // Both players online, lets do the divorce manually
10415 sd->status.partner_id = 0;
10416 p_sd->status.partner_id = 0;
10417 for( i = 0; i < MAX_INVENTORY; i++ )
10418 {
10419 if( sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_M || sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_F )
10420 pc_delitem(sd, i, 1, 0, 0, LOG_TYPE_OTHER);
10421 if( p_sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_M || p_sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_F )
10422 pc_delitem(p_sd, i, 1, 0, 0, LOG_TYPE_OTHER);
10423 }
10424
10425 clif_divorced(sd, p_sd->status.name);
10426 clif_divorced(p_sd, sd->status.name);
10427
10428 return true;
10429}
10430
10431/**
10432 * Get the partner map_session_data of a player
10433 * @param sd : the husband|wife session
10434 * @return partner session or NULL
10435 */
10436struct map_session_data *pc_get_partner(struct map_session_data *sd){
10437 if (!sd || !pc_ismarried(sd))
10438 return NULL;
10439 return map_charid2sd(sd->status.partner_id);
10440}
10441
10442/**
10443 * Get the father map_session_data of a player
10444 * @param sd : the baby session
10445 * @return father session or NULL
10446 */
10447struct map_session_data *pc_get_father (struct map_session_data *sd){
10448 if (!sd || !(sd->class_&JOBL_BABY) || !sd->status.father)
10449 return NULL;
10450 return map_charid2sd(sd->status.father);
10451}
10452
10453/**
10454 * Get the mother map_session_data of a player
10455 * @param sd : the baby session
10456 * @return mother session or NULL
10457 */
10458struct map_session_data *pc_get_mother (struct map_session_data *sd){
10459 if (!sd || !(sd->class_&JOBL_BABY) || !sd->status.mother)
10460 return NULL;
10461 return map_charid2sd(sd->status.mother);
10462}
10463
10464/*==========================================
10465 * Get sd children charid. (Need to be married)
10466 *------------------------------------------*/
10467struct map_session_data *pc_get_child (struct map_session_data *sd)
10468{
10469 if (!sd || !pc_ismarried(sd) || !sd->status.child)
10470 // charid2sd returns NULL if not found
10471 return NULL;
10472 return map_charid2sd(sd->status.child);
10473}
10474
10475/*==========================================
10476 * Set player sd to bleed. (losing hp and/or sp each diff_tick)
10477 *------------------------------------------*/
10478void pc_bleeding (struct map_session_data *sd, unsigned int diff_tick)
10479{
10480 int hp = 0, sp = 0;
10481
10482 if( pc_isdead(sd) )
10483 return;
10484
10485 if (sd->hp_loss.value) {
10486 sd->hp_loss.tick += diff_tick;
10487 while (sd->hp_loss.tick >= sd->hp_loss.rate) {
10488 hp += sd->hp_loss.value;
10489 sd->hp_loss.tick -= sd->hp_loss.rate;
10490 }
10491 if(hp >= sd->battle_status.hp)
10492 hp = sd->battle_status.hp-1; //Script drains cannot kill you.
10493 }
10494
10495 if (sd->sp_loss.value) {
10496 sd->sp_loss.tick += diff_tick;
10497 while (sd->sp_loss.tick >= sd->sp_loss.rate) {
10498 sp += sd->sp_loss.value;
10499 sd->sp_loss.tick -= sd->sp_loss.rate;
10500 }
10501 }
10502
10503 if (hp > 0 || sp > 0)
10504 status_zap(&sd->bl, hp, sp);
10505}
10506
10507//Character regen. Flag is used to know which types of regen can take place.
10508//&1: HP regen
10509//&2: SP regen
10510void pc_regen (struct map_session_data *sd, unsigned int diff_tick)
10511{
10512 int hp = 0, sp = 0;
10513
10514 if (sd->hp_regen.value) {
10515 sd->hp_regen.tick += diff_tick;
10516 while (sd->hp_regen.tick >= sd->hp_regen.rate) {
10517 hp += sd->hp_regen.value;
10518 sd->hp_regen.tick -= sd->hp_regen.rate;
10519 }
10520 }
10521
10522 if (sd->sp_regen.value) {
10523 sd->sp_regen.tick += diff_tick;
10524 while (sd->sp_regen.tick >= sd->sp_regen.rate) {
10525 sp += sd->sp_regen.value;
10526 sd->sp_regen.tick -= sd->sp_regen.rate;
10527 }
10528 }
10529
10530 if (sd->percent_hp_regen.value) {
10531 sd->percent_hp_regen.tick += diff_tick;
10532 while (sd->percent_hp_regen.tick >= sd->percent_hp_regen.rate) {
10533 hp += (sd->percent_hp_regen.value * sd->status.max_hp);
10534 sd->percent_hp_regen.tick -= sd->percent_hp_regen.rate;
10535 }
10536 }
10537
10538 if (sd->percent_sp_regen.value) {
10539 sd->percent_sp_regen.tick += diff_tick;
10540 while (sd->percent_sp_regen.tick >= sd->percent_sp_regen.rate) {
10541 sp += (sd->percent_sp_regen.value * sd->status.max_sp);
10542 sd->percent_sp_regen.tick -= sd->percent_sp_regen.rate;
10543 }
10544 }
10545
10546 if (hp > 0 || sp > 0)
10547 status_heal(&sd->bl, hp, sp, 0);
10548}
10549
10550/*==========================================
10551 * Memo player sd savepoint. (map,x,y)
10552 *------------------------------------------*/
10553void pc_setsavepoint(struct map_session_data *sd, short mapindex,int x,int y)
10554{
10555 nullpo_retv(sd);
10556
10557 sd->status.save_point.map = mapindex;
10558 sd->status.save_point.x = x;
10559 sd->status.save_point.y = y;
10560}
10561
10562/*==========================================
10563 * Save 1 player data at autosave interval
10564 *------------------------------------------*/
10565static TIMER_FUNC(pc_autosave){
10566 int interval;
10567 struct s_mapiterator* iter;
10568 struct map_session_data* sd;
10569 static int last_save_id = 0, save_flag = 0;
10570
10571 if(save_flag == 2) //Someone was saved on last call, normal cycle
10572 save_flag = 0;
10573 else
10574 save_flag = 1; //Noone was saved, so save first found char.
10575
10576 iter = mapit_getallusers();
10577 for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
10578 {
10579 if(sd->bl.id == last_save_id && save_flag != 1) {
10580 save_flag = 1;
10581 continue;
10582 }
10583
10584 if(save_flag != 1) //Not our turn to save yet.
10585 continue;
10586
10587 //Save char.
10588 last_save_id = sd->bl.id;
10589 save_flag = 2;
10590 if (pc_isvip(sd)) // Check if we're still VIP
10591 chrif_req_login_operation(1, sd->status.name, CHRIF_OP_LOGIN_VIP, 0, 1, 0);
10592 chrif_save(sd, CSAVE_INVENTORY|CSAVE_CART);
10593 break;
10594 }
10595 mapit_free(iter);
10596
10597 interval = autosave_interval/(map_usercount()+1);
10598 if(interval < minsave_interval)
10599 interval = minsave_interval;
10600 add_timer(gettick()+interval,pc_autosave,0,0);
10601
10602 return 0;
10603}
10604
10605static int pc_daynight_timer_sub(struct map_session_data *sd,va_list ap)
10606{
10607 if (sd->state.night != night_flag && map_getmapflag(sd->bl.m, MF_NIGHTENABLED))
10608 { //Night/day state does not match.
10609 clif_status_load(&sd->bl, EFST_SKE, night_flag); //New night effect by dynamix [Skotlex]
10610 sd->state.night = night_flag;
10611 return 1;
10612 }
10613 return 0;
10614}
10615/*================================================
10616 * timer to do the day [Yor]
10617 * data: 0 = called by timer, 1 = gmcommand/script
10618 *------------------------------------------------*/
10619TIMER_FUNC(map_day_timer){
10620 char tmp_soutput[1024];
10621
10622 if (data == 0 && battle_config.day_duration <= 0) // if we want a day
10623 return 0;
10624
10625 if (!night_flag)
10626 return 0; //Already day.
10627
10628 night_flag = 0; // 0=day, 1=night [Yor]
10629 map_foreachpc(pc_daynight_timer_sub);
10630 strcpy(tmp_soutput, (data == 0) ? msg_txt(NULL,502) : msg_txt(NULL,60)); // The day has arrived!
10631 intif_broadcast(tmp_soutput, strlen(tmp_soutput) + 1, BC_DEFAULT);
10632 return 0;
10633}
10634
10635/*================================================
10636 * timer to do the night [Yor]
10637 * data: 0 = called by timer, 1 = gmcommand/script
10638 *------------------------------------------------*/
10639TIMER_FUNC(map_night_timer){
10640 char tmp_soutput[1024];
10641
10642 if (data == 0 && battle_config.night_duration <= 0) // if we want a night
10643 return 0;
10644
10645 if (night_flag)
10646 return 0; //Already nigth.
10647
10648 night_flag = 1; // 0=day, 1=night [Yor]
10649 map_foreachpc(pc_daynight_timer_sub);
10650 strcpy(tmp_soutput, (data == 0) ? msg_txt(NULL,503) : msg_txt(NULL,59)); // The night has fallen...
10651 intif_broadcast(tmp_soutput, strlen(tmp_soutput) + 1, BC_DEFAULT);
10652 return 0;
10653}
10654
10655/**
10656* Attempt to stand up a player
10657* @param sd
10658* @param force Ignore the check, ask player to stand up. Used in some cases like pc_damage(), pc_revive(), etc
10659* @return True if success, Fals if failed
10660*/
10661bool pc_setstand(struct map_session_data *sd, bool force){
10662 nullpo_ret(sd);
10663
10664 // Cannot stand yet
10665 // TODO: Move to SCS_NOSTAND [Cydh]
10666 if (!force && (sd->sc.data[SC_SITDOWN_FORCE] || sd->sc.data[SC_BANANA_BOMB_SITDOWN]))
10667 return false;
10668
10669 status_change_end(&sd->bl, SC_TENSIONRELAX, INVALID_TIMER);
10670 clif_status_load(&sd->bl,EFST_SIT,0);
10671 clif_standing(&sd->bl); //Inform area PC is standing
10672 //Reset sitting tick.
10673 sd->ssregen.tick.hp = sd->ssregen.tick.sp = 0;
10674 if( pc_isdead( sd ) ){
10675 sd->state.dead_sit = sd->vd.dead_sit = 0;
10676 clif_party_dead( sd );
10677 }else{
10678 sd->state.dead_sit = sd->vd.dead_sit = 0;
10679 }
10680 return true;
10681}
10682
10683/**
10684 * Calculate Overheat value
10685 * @param sd: Player data
10686 * @param heat: Amount of Heat to adjust
10687 **/
10688void pc_overheat(struct map_session_data *sd, int16 heat) {
10689 struct status_change_entry *sce = NULL;
10690 int16 limit[] = { 150, 200, 280, 360, 450 };
10691 uint16 skill_lv;
10692
10693 nullpo_retv(sd);
10694
10695 skill_lv = cap_value(pc_checkskill(sd, NC_MAINFRAME), 0, 4);
10696 if ((sce = sd->sc.data[SC_OVERHEAT_LIMITPOINT])) {
10697 sce->val1 += heat;
10698 sce->val1 = cap_value(sce->val1, 0, 1000);
10699 if (sd->sc.data[SC_OVERHEAT])
10700 status_change_end(&sd->bl, SC_OVERHEAT, INVALID_TIMER);
10701 if (sce->val1 > limit[skill_lv])
10702 sc_start(&sd->bl, &sd->bl, SC_OVERHEAT, 100, sce->val1, 1000);
10703 } else if (heat > 0)
10704 sc_start(&sd->bl, &sd->bl, SC_OVERHEAT_LIMITPOINT, 100, heat, 1000);
10705}
10706
10707/**
10708 * Check if player is autolooting given itemID.
10709 */
10710bool pc_isautolooting(struct map_session_data *sd, unsigned short nameid)
10711{
10712 uint8 i = 0;
10713
10714 if (sd->state.autoloottype && sd->state.autoloottype&(1<<itemdb_type(nameid)))
10715 return true;
10716
10717 if (!sd->state.autolooting)
10718 return false;
10719
10720 if (sd->state.autolooting)
10721 ARR_FIND(0, AUTOLOOTITEM_SIZE, i, sd->state.autolootid[i] == nameid);
10722
10723 return (i != AUTOLOOTITEM_SIZE);
10724}
10725
10726/**
10727 * Checks if player can use @/#command
10728 * @param sd Player map session data
10729 * @param command Command name without @/# and params
10730 * @param type is it atcommand or charcommand
10731 */
10732bool pc_can_use_command(struct map_session_data *sd, const char *command, AtCommandType type)
10733{
10734 return pc_group_can_use_command(pc_get_group_id(sd), command, type);
10735}
10736
10737/**
10738 * Checks if commands used by a player should be logged
10739 * according to their group setting.
10740 * @param sd Player map session data
10741 */
10742bool pc_should_log_commands(struct map_session_data *sd)
10743{
10744 return pc_group_should_log_commands(pc_get_group_id(sd));
10745}
10746
10747/**
10748 * Spirit Charm expiration timer.
10749 * @see TimerFunc
10750 */
10751static TIMER_FUNC(pc_spiritcharm_timer){
10752 struct map_session_data *sd;
10753 int i;
10754
10755 if ((sd = (struct map_session_data *)map_id2sd(id)) == NULL || sd->bl.type != BL_PC)
10756 return 1;
10757
10758 if (sd->spiritcharm <= 0) {
10759 ShowError("pc_spiritcharm_timer: %d spiritcharm's available. (aid=%d cid=%d tid=%d)\n", sd->spiritcharm, sd->status.account_id, sd->status.char_id, tid);
10760 sd->spiritcharm = 0;
10761 sd->spiritcharm_type = CHARM_TYPE_NONE;
10762 return 0;
10763 }
10764
10765 ARR_FIND(0, sd->spiritcharm, i, sd->spiritcharm_timer[i] == tid);
10766
10767 if (i == sd->spiritcharm) {
10768 ShowError("pc_spiritcharm_timer: timer not found (aid=%d cid=%d tid=%d)\n", sd->status.account_id, sd->status.char_id, tid);
10769 return 0;
10770 }
10771
10772 sd->spiritcharm--;
10773
10774 if (i != sd->spiritcharm)
10775 memmove(sd->spiritcharm_timer + i, sd->spiritcharm_timer + i + 1, (sd->spiritcharm - i) * sizeof(int));
10776
10777 sd->spiritcharm_timer[sd->spiritcharm] = INVALID_TIMER;
10778
10779 if (sd->spiritcharm <= 0)
10780 sd->spiritcharm_type = CHARM_TYPE_NONE;
10781
10782 clif_spiritcharm(sd);
10783
10784 return 0;
10785}
10786
10787/**
10788 * Adds a spirit charm.
10789 * @param sd: Target character
10790 * @param interval: Duration
10791 * @param max: Maximum amount of charms to add
10792 * @param type: Charm type (@see spirit_charm_types)
10793 */
10794void pc_addspiritcharm(struct map_session_data *sd, int interval, int max, int type)
10795{
10796 int tid, i;
10797
10798 nullpo_retv(sd);
10799
10800 if (sd->spiritcharm_type != CHARM_TYPE_NONE && type != sd->spiritcharm_type)
10801 pc_delspiritcharm(sd, sd->spiritcharm, sd->spiritcharm_type);
10802
10803 if (max > MAX_SPIRITCHARM)
10804 max = MAX_SPIRITCHARM;
10805
10806 if (sd->spiritcharm < 0)
10807 sd->spiritcharm = 0;
10808
10809 if (sd->spiritcharm && sd->spiritcharm >= max) {
10810 if (sd->spiritcharm_timer[0] != INVALID_TIMER)
10811 delete_timer(sd->spiritcharm_timer[0], pc_spiritcharm_timer);
10812 sd->spiritcharm--;
10813 if (sd->spiritcharm != 0)
10814 memmove(sd->spiritcharm_timer + 0, sd->spiritcharm_timer + 1, (sd->spiritcharm) * sizeof(int));
10815 sd->spiritcharm_timer[sd->spiritcharm] = INVALID_TIMER;
10816 }
10817
10818 tid = add_timer(gettick() + interval, pc_spiritcharm_timer, sd->bl.id, 0);
10819 ARR_FIND(0, sd->spiritcharm, i, sd->spiritcharm_timer[i] == INVALID_TIMER || DIFF_TICK(get_timer(tid)->tick, get_timer(sd->spiritcharm_timer[i])->tick) < 0);
10820
10821 if (i != sd->spiritcharm)
10822 memmove(sd->spiritcharm_timer + i + 1, sd->spiritcharm_timer + i, (sd->spiritcharm - i) * sizeof(int));
10823
10824 sd->spiritcharm_timer[i] = tid;
10825 sd->spiritcharm++;
10826 sd->spiritcharm_type = type;
10827
10828 clif_spiritcharm(sd);
10829}
10830
10831/**
10832 * Removes one or more spirit charms.
10833 * @param sd: The target character
10834 * @param count: Amount of charms to remove
10835 * @param type: Type of charm to remove
10836 */
10837void pc_delspiritcharm(struct map_session_data *sd, int count, int type)
10838{
10839 int i;
10840
10841 nullpo_retv(sd);
10842
10843 if (sd->spiritcharm_type != type)
10844 return;
10845
10846 if (sd->spiritcharm <= 0) {
10847 sd->spiritcharm = 0;
10848 return;
10849 }
10850
10851 if (count <= 0)
10852 return;
10853
10854 if (count > sd->spiritcharm)
10855 count = sd->spiritcharm;
10856
10857 sd->spiritcharm -= count;
10858
10859 if (count > MAX_SPIRITCHARM)
10860 count = MAX_SPIRITCHARM;
10861
10862 for (i = 0; i < count; i++) {
10863 if (sd->spiritcharm_timer[i] != INVALID_TIMER) {
10864 delete_timer(sd->spiritcharm_timer[i], pc_spiritcharm_timer);
10865 sd->spiritcharm_timer[i] = INVALID_TIMER;
10866 }
10867 }
10868
10869 for (i = count; i < MAX_SPIRITCHARM; i++) {
10870 sd->spiritcharm_timer[i - count] = sd->spiritcharm_timer[i];
10871 sd->spiritcharm_timer[i] = INVALID_TIMER;
10872 }
10873
10874 if (sd->spiritcharm <= 0)
10875 sd->spiritcharm_type = CHARM_TYPE_NONE;
10876
10877 clif_spiritcharm(sd);
10878}
10879
10880#if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
10881/**
10882 * Renewal EXP/Item Drop rate modifier based on level penalty
10883 * @param level_diff: Monster and Player level difference
10884 * @param mob_class: Monster class
10885 * @param mode: Monster mode
10886 * @param type: 1 - EXP, 2 - Item Drop
10887 * @return Penalty rate
10888 */
10889int pc_level_penalty_mod(int level_diff, uint32 mob_class, enum e_mode mode, int type)
10890{
10891 int rate = 100;
10892
10893 if (type == 2 && (mode&MD_FIXED_ITEMDROP))
10894 return rate;
10895
10896 if (level_diff < 0)
10897 level_diff = MAX_LEVEL + (~level_diff + 1);
10898
10899 if ((rate = level_penalty[type][mob_class][level_diff]) > 0) // Monster class found, return rate
10900 return rate;
10901
10902 return 100; // Penalty not found, return default
10903}
10904#endif
10905
10906int pc_split_str(char *str,char **val,int num)
10907{
10908 int i;
10909
10910 for (i=0; i<num && str; i++){
10911 val[i] = str;
10912 str = strchr(str,',');
10913 if (str && i<num-1) //Do not remove a trailing comma.
10914 *str++=0;
10915 }
10916 return i;
10917}
10918
10919int pc_split_atoi(char* str, int* val, char sep, int max)
10920{
10921 int i,j;
10922 for (i=0; i<max; i++) {
10923 if (!str) break;
10924 val[i] = atoi(str);
10925 str = strchr(str,sep);
10926 if (str)
10927 *str++=0;
10928 }
10929 //Zero up the remaining.
10930 for(j=i; j < max; j++)
10931 val[j] = 0;
10932 return i;
10933}
10934
10935int pc_split_atoui(char* str, unsigned int* val, char sep, int max)
10936{
10937 static int warning=0;
10938 int i,j;
10939 for (i=0; i<max; i++) {
10940 double f;
10941 if (!str) break;
10942 f = atof(str);
10943 if (f < 0)
10944 val[i] = 0;
10945 else if (f > UINT_MAX) {
10946 val[i] = UINT_MAX;
10947 if (!warning) {
10948 warning = 1;
10949 ShowWarning("pc_readdb (exp.txt): Required exp per level is capped to %u\n", UINT_MAX);
10950 }
10951 } else
10952 val[i] = (unsigned int)f;
10953 str = strchr(str,sep);
10954 if (str)
10955 *str++=0;
10956 }
10957 //Zero up the remaining.
10958 for(j=i; j < max; j++)
10959 val[j] = 0;
10960 return i;
10961}
10962
10963/*==========================================
10964 * sub DB reading.
10965 * Function used to read skill_tree.txt
10966 *------------------------------------------*/
10967static bool pc_readdb_skilltree(char* fields[], int columns, int current)
10968{
10969 uint32 baselv, joblv, baselv_max, joblv_max;
10970 uint16 skill_id, skill_lv, skill_lv_max;
10971 int idx, class_;
10972 unsigned int i, offset, skill_idx;
10973
10974 class_ = atoi(fields[0]);
10975 skill_id = (uint16)atoi(fields[1]);
10976 skill_lv = (uint16)atoi(fields[2]);
10977
10978 if (columns == 5 + MAX_PC_SKILL_REQUIRE * 2) { // Base/Job level requirement extra columns
10979 baselv = (uint32)atoi(fields[3]);
10980 joblv = (uint32)atoi(fields[4]);
10981 offset = 5;
10982 }
10983 else if (columns == 3 + MAX_PC_SKILL_REQUIRE * 2) {
10984 baselv = joblv = 0;
10985 offset = 3;
10986 }
10987 else {
10988 ShowWarning("pc_readdb_skilltree: Invalid number of colums in skill %hu of job %d's tree.\n", skill_id, class_);
10989 return false;
10990 }
10991
10992 if(!pcdb_checkid(class_))
10993 {
10994 ShowWarning("pc_readdb_skilltree: Invalid job class %d specified.\n", class_);
10995 return false;
10996 }
10997 idx = pc_class2idx(class_);
10998
10999 if (!skill_get_index(skill_id)) {
11000 ShowWarning("pc_readdb_skilltree: Unable to load skill %hu into job %d's tree.", skill_id, class_);
11001 return false;
11002 }
11003 if (skill_lv > (skill_lv_max = skill_get_max(skill_id))) {
11004 ShowWarning("pc_readdb_skilltree: Skill %hu's level %hu exceeds job %d's max level %hu. Capping skill level.\n", skill_id, skill_lv, class_, skill_lv_max);
11005 skill_lv = skill_lv_max;
11006 }
11007 if (baselv > (baselv_max = pc_class_maxbaselv(class_))) {
11008 ShowWarning("pc_readdb_skilltree: Skill %hu's base level requirement %d exceeds job %d's max base level %d. Capping skill base level.\n", skill_id, baselv, class_, baselv_max);
11009 baselv = baselv_max;
11010 }
11011 if (joblv > (joblv_max = pc_class_maxjoblv(class_))) {
11012 ShowWarning("pc_readdb_skilltree: Skill %hu's job level requirement %d exceeds job %d's max job level %d. Capping skill job level.\n", skill_id, joblv, class_, joblv_max);
11013 joblv = joblv_max;
11014 }
11015
11016 //This is to avoid adding two lines for the same skill. [Skotlex]
11017 ARR_FIND( 0, MAX_SKILL_TREE, skill_idx, skill_tree[idx][skill_idx].skill_id == 0 || skill_tree[idx][skill_idx].skill_id == skill_id );
11018 if( skill_idx == MAX_SKILL_TREE )
11019 {
11020 ShowWarning("pc_readdb_skilltree: Unable to load skill %hu into job %d's tree. Maximum number of skills per job has been reached.\n", skill_id, class_);
11021 return false;
11022 }
11023 else if(skill_tree[idx][skill_idx].skill_id)
11024 {
11025 ShowNotice("pc_readdb_skilltree: Overwriting skill %hu for job %d.\n", skill_id, class_);
11026 }
11027
11028 skill_tree[idx][skill_idx].skill_id = skill_id;
11029 skill_tree[idx][skill_idx].skill_lv = skill_lv;
11030 skill_tree[idx][skill_idx].baselv = baselv;
11031 skill_tree[idx][skill_idx].joblv = joblv;
11032
11033 for(i = 0; i < MAX_PC_SKILL_REQUIRE; i++)
11034 {
11035 skill_id = (uint16)atoi(fields[i * 2 + offset]);
11036 skill_lv = (uint16)atoi(fields[i * 2 + offset + 1]);
11037
11038 if (skill_id == 0) {
11039 if (skill_tree[idx][skill_idx].need[i].skill_id > 0) { // Remove pre-requisite
11040 skill_tree[idx][skill_idx].need[i].skill_id = 0;
11041 skill_tree[idx][skill_idx].need[i].skill_lv = 0;
11042 }
11043 continue;
11044 }
11045 if (skill_id > MAX_SKILL_ID || !skill_get_index(skill_id)) {
11046 ShowWarning("pc_readdb_skilltree: Unable to load requirement skill %hu into job %d's tree.", skill_id, class_);
11047 return false;
11048 }
11049 if (skill_lv > (skill_lv_max = skill_get_max(skill_id))) {
11050 ShowWarning("pc_readdb_skilltree: Skill %hu's level %hu exceeds job %d's max level %hu. Capping skill level.\n", skill_id, skill_lv, class_, skill_lv_max);
11051 skill_lv = skill_lv_max;
11052 }
11053
11054 skill_tree[idx][skill_idx].need[i].skill_id = skill_id;
11055 skill_tree[idx][skill_idx].need[i].skill_lv = skill_lv;
11056 }
11057 return true;
11058}
11059#if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
11060static bool pc_readdb_levelpenalty(char* fields[], int columns, int current)
11061{
11062 int type, class_, diff;
11063
11064 type = atoi(fields[0]); //1=experience, 2=item drop
11065 class_ = atoi(fields[1]);
11066 diff = atoi(fields[2]);
11067
11068 if( type != 1 && type != 2 ){
11069 ShowWarning("pc_readdb_levelpenalty: Invalid type %d specified.\n", type);
11070 return false;
11071 }
11072
11073 if( !CHK_CLASS(class_) ){
11074 ShowWarning("pc_readdb_levelpenalty: Invalid class %d specified.\n", class_);
11075 return false;
11076 }
11077
11078 diff = min(diff, MAX_LEVEL);
11079
11080 if( diff < 0 )
11081 diff = min(MAX_LEVEL + ( ~(diff) + 1 ), MAX_LEVEL*2);
11082
11083 level_penalty[type][class_][diff] = atoi(fields[3]);
11084
11085 return true;
11086}
11087#endif
11088
11089/** [Cydh]
11090* Calculates base hp of player. Reference: http://irowiki.org/wiki/Max_HP
11091* @param level Base level of player
11092* @param class_ Job ID @see enum e_job
11093* @return base_hp
11094*/
11095static unsigned int pc_calc_basehp(uint16 level, uint16 class_) {
11096 double base_hp;
11097 uint16 i, idx = pc_class2idx(class_);
11098
11099 base_hp = 35 + level * (job_info[idx].hp_multiplicator/100.);
11100#ifndef RENEWAL
11101 if(level >= 10 && (class_ == JOB_NINJA || class_ == JOB_GUNSLINGER)) base_hp += 90;
11102#endif
11103 for (i = 2; i <= level; i++)
11104 base_hp += floor(((job_info[idx].hp_factor/100.) * i) + 0.5); //Don't have round()
11105 if (class_ == JOB_SUMMONER)
11106 base_hp += floor((base_hp / 2) + 0.5);
11107 return (unsigned int)base_hp;
11108}
11109
11110/** [Playtester]
11111* Calculates base sp of player.
11112* @param level Base level of player
11113* @param class_ Job ID @see enum e_job
11114* @return base_sp
11115*/
11116static unsigned int pc_calc_basesp(uint16 level, uint16 class_) {
11117 double base_sp;
11118 uint16 idx = pc_class2idx(class_);
11119
11120 base_sp = 10 + floor(level * (job_info[idx].sp_factor / 100.));
11121
11122 switch (class_) {
11123 case JOB_NINJA:
11124 if (level >= 10)
11125 base_sp -= 22;
11126 else
11127 base_sp = 11 + 3*level;
11128 break;
11129 case JOB_GUNSLINGER:
11130 if (level > 10)
11131 base_sp -= 18;
11132 else
11133 base_sp = 9 + 3*level;
11134 break;
11135 case JOB_SUMMONER:
11136 base_sp -= floor(base_sp / 2);
11137 break;
11138 }
11139
11140 return (unsigned int)base_sp;
11141}
11142
11143//Reading job_db1.txt line, (class,weight,HPFactor,HPMultiplicator,SPFactor,aspd/lvl...)
11144static bool pc_readdb_job1(char* fields[], int columns, int current){
11145 int idx, class_;
11146 unsigned int i;
11147
11148 class_ = atoi(fields[0]);
11149
11150 if (!pcdb_checkid(class_)) {
11151 ShowWarning("status_readdb_job1: Invalid job class %d specified.\n", class_);
11152 return false;
11153 }
11154 idx = pc_class2idx(class_);
11155
11156 job_info[idx].max_weight_base = atoi(fields[1]);
11157 job_info[idx].hp_factor = atoi(fields[2]);
11158 job_info[idx].hp_multiplicator = atoi(fields[3]);
11159 job_info[idx].sp_factor = atoi(fields[4]);
11160
11161#ifdef RENEWAL_ASPD
11162 for(i = 0; i <= MAX_WEAPON_TYPE; i++)
11163#else
11164 for(i = 0; i < MAX_WEAPON_TYPE; i++)
11165#endif
11166 {
11167 job_info[idx].aspd_base[i] = atoi(fields[i+5]);
11168 }
11169
11170 return true;
11171}
11172
11173//Reading job_db2.txt line (class,JobLv1,JobLv2,JobLv3,...)
11174static bool pc_readdb_job2(char* fields[], int columns, int current)
11175{
11176 int idx, class_, i;
11177
11178 class_ = atoi(fields[0]);
11179
11180 if(!pcdb_checkid(class_))
11181 {
11182 ShowWarning("status_readdb_job2: Invalid job class %d specified.\n", class_);
11183 return false;
11184 }
11185 idx = pc_class2idx(class_);
11186
11187 for(i = 1; i < columns; i++)
11188 {
11189 job_info[idx].job_bonus[i-1] = atoi(fields[i]);
11190 }
11191 return true;
11192}
11193
11194//Reading job_exp.txt line
11195//Max Level,Class list,Type (0 - Base Exp; 1 - Job Exp),Exp/lvl...
11196static bool pc_readdb_job_exp(char* fields[], int columns, int current)
11197{
11198 int idx, i, type;
11199 int job_id,job_count,jobs[CLASS_COUNT];
11200 unsigned int ui, maxlvl;
11201
11202 maxlvl = atoi(fields[0]);
11203 if(maxlvl > MAX_LEVEL || maxlvl<1){
11204 ShowError("pc_readdb_job_exp: Invalid maxlevel %d specified.\n", maxlvl);
11205 return false;
11206 }
11207 if((maxlvl+3) > columns){ //nb values = (maxlvl-startlvl)+1-index1stvalue
11208 ShowError("pc_readdb_job_exp: Number of columns %d defined is too low for max level %d.\n",columns,maxlvl);
11209 return false;
11210 }
11211 type = atoi(fields[2]);
11212 if(type < 0 || type > 1){
11213 ShowError("pc_readdb_job_exp: Invalid type %d specified.\n", type);
11214 return false;
11215 }
11216 job_count = pc_split_atoi(fields[1],jobs,':',CLASS_COUNT);
11217 if (job_count < 1)
11218 return false;
11219 job_id = jobs[0];
11220 if(!pcdb_checkid(job_id)){
11221 ShowError("pc_readdb_job_exp: Invalid job class %d specified.\n", job_id);
11222 return false;
11223 }
11224 idx = pc_class2idx(job_id);
11225
11226 job_info[idx].max_level[type] = maxlvl;
11227 for(i=0; i<maxlvl; i++)
11228 job_info[idx].exp_table[type][i] = ((uint32) atoi(fields[3+i]));
11229 //Reverse check in case the array has a bunch of trailing zeros... [Skotlex]
11230 //The reasoning behind the -2 is this... if the max level is 5, then the array
11231 //should look like this:
11232 //0: x, 1: x, 2: x: 3: x 4: 0 <- last valid value is at 3.
11233 while ((ui = job_info[idx].max_level[type]) >= 2 && job_info[idx].exp_table[type][ui-2] <= 0)
11234 job_info[idx].max_level[type]--;
11235 if (job_info[idx].max_level[type] < maxlvl) {
11236 ShowWarning("pc_readdb_job_exp: Specified max %u for job %d, but that job's exp table only goes up to level %u.\n", maxlvl, job_id, job_info[idx].max_level[type]);
11237 ShowInfo("Filling the missing values with the last exp entry.\n");
11238 //Fill the requested values with the last entry.
11239 ui = (job_info[idx].max_level[type] <= 2? 0: job_info[idx].max_level[type]-2);
11240 for (; ui+2 < maxlvl; ui++)
11241 job_info[idx].exp_table[type][ui] = job_info[idx].exp_table[type][ui-1];
11242 job_info[idx].max_level[type] = maxlvl;
11243 }
11244// ShowInfo("%s - Class %d: %d\n", type?"Job":"Base", job_id, job_info[idx].max_level[type]);
11245 for (i = 1; i < job_count; i++) {
11246 job_id = jobs[i];
11247 if (!pcdb_checkid(job_id)) {
11248 ShowError("pc_readdb_job_exp: Invalid job ID %d.\n", job_id);
11249 continue;
11250 }
11251 idx = pc_class2idx(job_id);
11252 memcpy(job_info[idx].exp_table[type], job_info[pc_class2idx(jobs[0])].exp_table[type], sizeof(job_info[pc_class2idx(jobs[0])].exp_table[type]));
11253 job_info[idx].max_level[type] = maxlvl;
11254// ShowInfo("%s - Class %d: %u\n", type?"Job":"Base", job_id, job_info[idx].max_level[type]);
11255 }
11256 return true;
11257}
11258
11259/**
11260* #ifdef HP_SP_TABLES, reads 'job_basehpsp_db.txt to replace hp/sp results from formula
11261* startlvl,endlvl,class,type,values...
11262*/
11263#ifdef HP_SP_TABLES
11264static bool pc_readdb_job_basehpsp(char* fields[], int columns, int current)
11265{
11266 int i, startlvl, endlvl;
11267 int job_count,jobs[CLASS_COUNT];
11268 short type;
11269
11270 startlvl = atoi(fields[0]);
11271 if(startlvl<1){
11272 ShowError("pc_readdb_job_basehpsp: Invalid start level %d specified.\n", startlvl);
11273 return false;
11274 }
11275 endlvl = atoi(fields[1]);
11276 if(endlvl<1 || endlvl<startlvl){
11277 ShowError("pc_readdb_job_basehpsp: Invalid end level %d specified.\n", endlvl);
11278 return false;
11279 }
11280 if((endlvl-startlvl+1+4) > columns){ //nb values = (maxlvl-startlvl)+1-index1stvalue
11281 ShowError("pc_readdb_job_basehpsp: Number of columns %d (needs %d) defined is too low for start level %d, max level %d.\n",columns,(endlvl-startlvl+1+4),startlvl,endlvl);
11282 return false;
11283 }
11284 type = atoi(fields[3]);
11285 if(type < 0 || type > 1){
11286 ShowError("pc_readdb_job_basehpsp: Invalid type %d specified.\n", type);
11287 return false;
11288 }
11289
11290 job_count = pc_split_atoi(fields[2],jobs,':',CLASS_COUNT);
11291 if (job_count < 1)
11292 return false;
11293
11294 for (i = 0; i < job_count; i++) {
11295 int idx, job_id = jobs[i], use_endlvl;
11296 if (!pcdb_checkid(job_id)) {
11297 ShowError("pc_readdb_job_basehpsp: Invalid job class %d specified.\n", job_id);
11298 return false;
11299 }
11300 idx = pc_class2idx(job_id);
11301 if (startlvl > job_info[idx].max_level[0]) {
11302 ShowError("pc_readdb_job_basehpsp: Invalid start level %d specified.\n", startlvl);
11303 return false;
11304 }
11305 //Just read until available max level for this job, don't use MAX_LEVEL!
11306 use_endlvl = endlvl;
11307 if (use_endlvl > job_info[idx].max_level[0])
11308 use_endlvl = job_info[idx].max_level[0];
11309
11310 if(type == 0) { //hp type
11311 uint16 j;
11312 for(j = 0; j < use_endlvl; j++) {
11313 if (atoi(fields[j+4])) {
11314 uint16 lvl_idx = startlvl-1+j;
11315 job_info[idx].base_hp[lvl_idx] = atoi(fields[j+4]);
11316 //Tells if this HP is lower than previous level (but not for 99->100)
11317 if (lvl_idx-1 >= 0 && lvl_idx != 99 && job_info[idx].base_hp[lvl_idx] < job_info[idx].base_hp[lvl_idx-1])
11318 ShowInfo("pc_readdb_job_basehpsp: HP value at entry %d col %d is lower than previous level (job=%d,lvl=%d,oldval=%d,val=%d).\n",
11319 current,j+4,job_id,lvl_idx+1,job_info[idx].base_hp[lvl_idx-1],job_info[idx].base_hp[lvl_idx]);
11320 }
11321 }
11322 }
11323 else { //sp type
11324 uint16 j;
11325 for(j = 0; j < use_endlvl; j++) {
11326 if (atoi(fields[j+4])) {
11327 uint16 lvl_idx = startlvl-1+j;
11328 job_info[idx].base_sp[lvl_idx] = atoi(fields[j+4]);
11329 //Tells if this SP is lower than previous level (but not for 99->100)
11330 if (lvl_idx-1 >= 0 && lvl_idx != 99 && job_info[idx].base_sp[lvl_idx] < job_info[idx].base_sp[lvl_idx-1])
11331 ShowInfo("pc_readdb_job_basehpsp: SP value at entry %d col %d is lower than previous level (job=%d,lvl=%d,oldval=%d,val=%d).\n",
11332 current,j+4,job_id,lvl_idx+1,job_info[idx].base_sp[lvl_idx-1],job_info[idx].base_sp[lvl_idx]);
11333 }
11334 }
11335 }
11336 }
11337 return true;
11338}
11339#endif
11340
11341/** [Cydh]
11342* Reads 'job_param_db.txt' to check max. param each job and store them to job_info[].max_param.*
11343*/
11344static bool pc_readdb_job_param(char* fields[], int columns, int current)
11345{
11346 int idx, class_;
11347 uint16 str, agi, vit, int_, dex, luk;
11348
11349 script_get_constant(trim(fields[0]),&class_);
11350
11351 if ((idx = pc_class2idx(class_)) < 0) {
11352 ShowError("pc_readdb_job_param: Invalid job '%s'. Skipping!",fields[0]);
11353 return false;
11354 }
11355 str = cap_value(atoi(fields[1]),10,SHRT_MAX);
11356 agi = atoi(fields[2]) ? cap_value(atoi(fields[2]),10,SHRT_MAX) : str;
11357 vit = atoi(fields[3]) ? cap_value(atoi(fields[3]),10,SHRT_MAX) : str;
11358 int_ = atoi(fields[4]) ? cap_value(atoi(fields[4]),10,SHRT_MAX) : str;
11359 dex = atoi(fields[5]) ? cap_value(atoi(fields[5]),10,SHRT_MAX) : str;
11360 luk = atoi(fields[6]) ? cap_value(atoi(fields[6]),10,SHRT_MAX) : str;
11361
11362 job_info[idx].max_param.str = str;
11363 job_info[idx].max_param.agi = agi;
11364 job_info[idx].max_param.vit = vit;
11365 job_info[idx].max_param.int_ = int_;
11366 job_info[idx].max_param.dex = dex;
11367 job_info[idx].max_param.luk = luk;
11368
11369 return true;
11370}
11371
11372/**
11373 * Read job_noenter_map.txt
11374 **/
11375static bool pc_readdb_job_noenter_map(char *str[], int columns, int current) {
11376 int idx, class_ = -1;
11377
11378 if (ISDIGIT(str[0][0])) {
11379 class_ = atoi(str[0]);
11380 } else {
11381 if (!script_get_constant(str[0], &class_)) {
11382 ShowError("pc_readdb_job_noenter_map: Invalid job %s specified.\n", str[0]);
11383 return false;
11384 }
11385 }
11386
11387 if (!pcdb_checkid(class_) || (idx = pc_class2idx(class_)) < 0) {
11388 ShowError("pc_readdb_job_noenter_map: Invalid job %d specified.\n", str[0]);
11389 return false;
11390 }
11391
11392 job_info[idx].noenter_map.zone = atoi(str[1]);
11393 job_info[idx].noenter_map.group_lv = atoi(str[2]);
11394 return true;
11395}
11396
11397static int pc_read_statsdb(const char *basedir, int last_s, bool silent){
11398 int i=1;
11399 char line[24000]; //FIXME this seem too big
11400 FILE *fp;
11401
11402 sprintf(line, "%s/statpoint.txt", basedir);
11403 fp=fopen(line,"r");
11404 if(fp == NULL){
11405 if(silent==0) ShowWarning("Can't read '" CL_WHITE "%s" CL_RESET "'... Generating DB.\n",line);
11406 return max(last_s,i);
11407 } else {
11408 int entries=0;
11409 while(fgets(line, sizeof(line), fp))
11410 {
11411 int stat;
11412 trim(line);
11413 if(line[0] == '\0' || (line[0]=='/' && line[1]=='/'))
11414 continue;
11415 if ((stat=strtoul(line,NULL,10))<0)
11416 stat=0;
11417 if (i > MAX_LEVEL)
11418 break;
11419 statp[i]=stat;
11420 i++;
11421 entries++;
11422 }
11423 fclose(fp);
11424 ShowStatus("Done reading '" CL_WHITE "%d" CL_RESET "' entries in '" CL_WHITE "%s/%s" CL_RESET "'.\n", entries, basedir,"statpoint.txt");
11425 }
11426 return max(last_s,i);
11427}
11428
11429/*==========================================
11430 * pc DB reading.
11431 * job_exp.txt - required experience values
11432 * skill_tree.txt - skill tree for every class
11433 * attr_fix.txt - elemental adjustment table
11434 * job_db1.txt - job,weight,hp_factor,hp_multiplicator,sp_factor,aspds/lvl
11435 * job_db2.txt - job,stats bonuses/lvl
11436 * job_maxhpsp_db.txt - strtlvl,maxlvl,job,type,values/lvl (values=hp|sp)
11437 *------------------------------------------*/
11438void pc_readdb(void) {
11439 int i, k, s = 1;
11440 const char* dbsubpath[] = {
11441 "",
11442 "/" DBIMPORT,
11443 //add other path here
11444 };
11445
11446 //reset
11447 memset(job_info,0,sizeof(job_info)); // job_info table
11448
11449#if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
11450 sv_readdb(db_path, "re/level_penalty.txt", ',', 4, 4, -1, &pc_readdb_levelpenalty, 0);
11451 sv_readdb(db_path, DBIMPORT"/level_penalty.txt", ',', 4, 4, -1, &pc_readdb_levelpenalty, 1);
11452 for( k=1; k < 3; k++ ){ // fill in the blanks
11453 int j;
11454 for( j = 0; j < CLASS_ALL; j++ ){
11455 int tmp = 0;
11456 for( i = 0; i < MAX_LEVEL*2; i++ ){
11457 if( i == MAX_LEVEL+1 )
11458 tmp = level_penalty[k][j][0];// reset
11459 if( level_penalty[k][j][i] > 0 )
11460 tmp = level_penalty[k][j][i];
11461 else
11462 level_penalty[k][j][i] = tmp;
11463 }
11464 }
11465 }
11466#endif
11467
11468 // reset then read statspoint
11469 memset(statp,0,sizeof(statp));
11470 for(i=0; i<ARRAYLENGTH(dbsubpath); i++){
11471 uint8 n1 = (uint8)(strlen(db_path)+strlen(dbsubpath[i])+1);
11472 uint8 n2 = (uint8)(strlen(db_path)+strlen(DBPATH)+strlen(dbsubpath[i])+1);
11473 char* dbsubpath1 = (char*)aMalloc(n1+1);
11474 char* dbsubpath2 = (char*)aMalloc(n2+1);
11475
11476 if(i==0) {
11477 safesnprintf(dbsubpath1,n1,"%s%s",db_path,dbsubpath[i]);
11478 safesnprintf(dbsubpath2,n2,"%s/%s%s",db_path,DBPATH,dbsubpath[i]);
11479 }
11480 else {
11481 safesnprintf(dbsubpath1,n1,"%s%s",db_path,dbsubpath[i]);
11482 safesnprintf(dbsubpath2,n1,"%s%s",db_path,dbsubpath[i]);
11483 }
11484
11485 s = pc_read_statsdb(dbsubpath2,s,i > 0);
11486 if (i == 0)
11487#ifdef RENEWAL_ASPD
11488 sv_readdb(dbsubpath1, "re/job_db1.txt",',',6+MAX_WEAPON_TYPE,6+MAX_WEAPON_TYPE,CLASS_COUNT,&pc_readdb_job1, i > 0);
11489#else
11490 sv_readdb(dbsubpath1, "pre-re/job_db1.txt",',',5+MAX_WEAPON_TYPE,5+MAX_WEAPON_TYPE,CLASS_COUNT,&pc_readdb_job1, i > 0);
11491#endif
11492 else
11493 sv_readdb(dbsubpath1, "job_db1.txt",',',5+MAX_WEAPON_TYPE,6+MAX_WEAPON_TYPE,CLASS_COUNT,&pc_readdb_job1, i > 0);
11494 sv_readdb(dbsubpath1, "job_db2.txt",',',1,1+MAX_LEVEL,CLASS_COUNT,&pc_readdb_job2, i > 0);
11495 sv_readdb(dbsubpath2, "job_exp.txt",',',4,1000+3,CLASS_COUNT*2,&pc_readdb_job_exp, i > 0); //support till 1000lvl
11496#ifdef HP_SP_TABLES
11497 sv_readdb(dbsubpath2, "job_basehpsp_db.txt", ',', 4, 4+500, CLASS_COUNT*2, &pc_readdb_job_basehpsp, i > 0); //Make it support until lvl 500!
11498#endif
11499 sv_readdb(dbsubpath2, "job_param_db.txt", ',', 2, PARAM_MAX+1, CLASS_COUNT, &pc_readdb_job_param, i > 0);
11500 sv_readdb(dbsubpath2, "job_noenter_map.txt", ',', 3, 3, CLASS_COUNT, &pc_readdb_job_noenter_map, i > 0);
11501 aFree(dbsubpath1);
11502 aFree(dbsubpath2);
11503 }
11504
11505 // Reset and read skilltree - needs to be read after pc_readdb_job_exp to get max base and job levels
11506 memset(skill_tree, 0, sizeof(skill_tree));
11507 sv_readdb(db_path, DBPATH"skill_tree.txt", ',', 3 + MAX_PC_SKILL_REQUIRE * 2, 5 + MAX_PC_SKILL_REQUIRE * 2, -1, &pc_readdb_skilltree, 0);
11508 sv_readdb(db_path, DBIMPORT"/skill_tree.txt", ',', 3 + MAX_PC_SKILL_REQUIRE * 2, 5 + MAX_PC_SKILL_REQUIRE * 2, -1, &pc_readdb_skilltree, 1);
11509
11510 // generate the remaining parts of the db if necessary
11511 k = battle_config.use_statpoint_table; //save setting
11512 battle_config.use_statpoint_table = 0; //temporarily disable to force pc_gets_status_point use default values
11513 statp[0] = 45; // seed value
11514 for (; s <= MAX_LEVEL; s++)
11515 statp[s] = statp[s-1] + pc_gets_status_point(s-1);
11516 battle_config.use_statpoint_table = k; //restore setting
11517
11518 //Checking if all class have their data
11519 for (i = 0; i < JOB_MAX; i++) {
11520 int idx;
11521 uint16 j;
11522 if (!pcdb_checkid(i))
11523 continue;
11524 if (i == JOB_WEDDING || i == JOB_XMAS || i == JOB_SUMMER || i == JOB_HANBOK || i == JOB_OKTOBERFEST || i == JOB_SUMMER2)
11525 continue; //Classes that do not need exp tables.
11526 idx = pc_class2idx(i);
11527 if (!job_info[idx].max_level[0])
11528 ShowWarning("Class %s (%d) does not have a base exp table.\n", job_name(i), i);
11529 if (!job_info[idx].max_level[1])
11530 ShowWarning("Class %s (%d) does not have a job exp table.\n", job_name(i), i);
11531
11532 //Init and checking the empty value of Base HP/SP [Cydh]
11533 for (j = 0; j < (job_info[idx].max_level[0] ? job_info[idx].max_level[0] : MAX_LEVEL); j++) {
11534 if (job_info[idx].base_hp[j] == 0)
11535 job_info[idx].base_hp[j] = pc_calc_basehp(j+1,i);
11536 if (job_info[idx].base_sp[j] == 0)
11537 job_info[idx].base_sp[j] = pc_calc_basesp(j+1,i);
11538 }
11539 }
11540}
11541
11542// Read MOTD on startup. [Valaris]
11543int pc_read_motd(void)
11544{
11545 FILE* fp;
11546 // clear old MOTD
11547 memset(motd_text, 0, sizeof(motd_text));
11548
11549 // read current MOTD
11550 if( ( fp = fopen(motd_txt, "r") ) != NULL )
11551 {
11552 unsigned int entries = 0;
11553 char buf[CHAT_SIZE_MAX];
11554
11555 while( entries < MOTD_LINE_SIZE && fgets(buf, CHAT_SIZE_MAX, fp) )
11556 {
11557 unsigned int lines = 0;
11558 size_t len;
11559 lines++;
11560 if( buf[0] == '/' && buf[1] == '/' )
11561 continue;
11562 len = strlen(buf);
11563 while( len && ( buf[len-1] == '\r' || buf[len-1] == '\n' ) ) // strip trailing EOL characters
11564 len--;
11565 if( len ) {
11566 char * ptr;
11567 buf[len] = 0;
11568 if( ( ptr = strstr(buf, " :") ) != NULL && ptr-buf >= NAME_LENGTH ) // crashes newer clients
11569 ShowWarning("Found sequence '" CL_WHITE " :" CL_RESET "' on line '" CL_WHITE "%u" CL_RESET "' in '" CL_WHITE "%s" CL_RESET "'. This can cause newer clients to crash.\n", lines, motd_txt);
11570 }
11571 else {// empty line
11572 buf[0] = ' ';
11573 buf[1] = 0;
11574 }
11575 safestrncpy(motd_text[entries], buf, CHAT_SIZE_MAX);
11576 entries++;
11577 }
11578 fclose(fp);
11579 ShowStatus("Done reading '" CL_WHITE "%u" CL_RESET "' entries in '" CL_WHITE "%s" CL_RESET "'.\n", entries, motd_txt);
11580 }
11581 else
11582 ShowWarning("File '" CL_WHITE "%s" CL_RESET "' not found.\n", motd_txt);
11583
11584 return 0;
11585}
11586
11587void pc_itemcd_do(struct map_session_data *sd, bool load) {
11588 int i,cursor = 0;
11589 struct item_cd* cd = NULL;
11590
11591 if( load ) {
11592 if( !(cd = (struct item_cd*)idb_get(itemcd_db, sd->status.char_id)) ) {
11593 // no item cooldown is associated with this character
11594 return;
11595 }
11596 for(i = 0; i < MAX_ITEMDELAYS; i++) {
11597 if( cd->nameid[i] && DIFF_TICK(gettick(),cd->tick[i]) < 0 ) {
11598 sd->item_delay[cursor].tick = cd->tick[i];
11599 sd->item_delay[cursor].nameid = cd->nameid[i];
11600 cursor++;
11601 }
11602 }
11603 idb_remove(itemcd_db,sd->status.char_id);
11604 } else {
11605 if( !(cd = (struct item_cd*)idb_get(itemcd_db,sd->status.char_id)) ) {
11606 // create a new skill cooldown object for map storage
11607 CREATE( cd, struct item_cd, 1 );
11608 idb_put( itemcd_db, sd->status.char_id, cd );
11609 }
11610 for(i = 0; i < MAX_ITEMDELAYS; i++) {
11611 if( sd->item_delay[i].nameid && DIFF_TICK(gettick(),sd->item_delay[i].tick) < 0 ) {
11612 cd->tick[cursor] = sd->item_delay[i].tick;
11613 cd->nameid[cursor] = sd->item_delay[i].nameid;
11614 cursor++;
11615 }
11616 }
11617 }
11618 return;
11619}
11620
11621/**
11622 * Add item delay to player's item delay data
11623 * @param sd Player
11624 * @param id Item data
11625 * @param tick Current tick
11626 * @param n Item index in inventory
11627 * @return 0: No delay, can consume item.
11628 * 1: Has delay, cancel consumption.
11629 **/
11630uint8 pc_itemcd_add(struct map_session_data *sd, struct item_data *id, unsigned int tick, unsigned short n) {
11631 int i;
11632 ARR_FIND(0, MAX_ITEMDELAYS, i, sd->item_delay[i].nameid == id->nameid );
11633 if( i == MAX_ITEMDELAYS ) /* item not found. try first empty now */
11634 ARR_FIND(0, MAX_ITEMDELAYS, i, !sd->item_delay[i].nameid );
11635 if( i < MAX_ITEMDELAYS ) {
11636 if( sd->item_delay[i].nameid ) {// found
11637 if( DIFF_TICK(sd->item_delay[i].tick, tick) > 0 ) {
11638 int e_tick = DIFF_TICK(sd->item_delay[i].tick, tick)/1000;
11639 char e_msg[CHAT_SIZE_MAX];
11640 if( e_tick > 99 )
11641 sprintf(e_msg,msg_txt(sd,379), // Item Failed. [%s] is cooling down. Wait %.1f minutes.
11642 itemdb_jname(sd->item_delay[i].nameid), (double)e_tick / 60);
11643 else
11644 sprintf(e_msg,msg_txt(sd,380), // Item Failed. [%s] is cooling down. Wait %d seconds.
11645 itemdb_jname(sd->item_delay[i].nameid), e_tick+1);
11646 clif_messagecolor(&sd->bl,color_table[COLOR_YELLOW],e_msg,false,SELF);
11647 return 1; // Delay has not expired yet
11648 }
11649 } else {// not yet used item (all slots are initially empty)
11650 sd->item_delay[i].nameid = id->nameid;
11651 }
11652 if( !(id->nameid == ITEMID_REINS_OF_MOUNT && sd->sc.option&(OPTION_WUGRIDER|OPTION_RIDING|OPTION_DRAGON|OPTION_MADOGEAR)) )
11653 sd->item_delay[i].tick = tick + sd->inventory_data[n]->delay;
11654 } else {// should not happen
11655 ShowError("pc_itemcd_add: Exceeded item delay array capacity! (nameid=%hu, char_id=%d)\n", id->nameid, sd->status.char_id);
11656 }
11657 //clean up used delays so we can give room for more
11658 for(i = 0; i < MAX_ITEMDELAYS; i++) {
11659 if( DIFF_TICK(sd->item_delay[i].tick, tick) <= 0 ) {
11660 sd->item_delay[i].tick = 0;
11661 sd->item_delay[i].nameid = 0;
11662 }
11663 }
11664 return 0;
11665}
11666
11667/**
11668 * Check if player has delay to reuse item
11669 * @param sd Player
11670 * @param id Item data
11671 * @param tick Current tick
11672 * @param n Item index in inventory
11673 * @return 0: No delay, can consume item.
11674 * 1: Has delay, cancel consumption.
11675 **/
11676uint8 pc_itemcd_check(struct map_session_data *sd, struct item_data *id, unsigned int tick, unsigned short n) {
11677 struct status_change *sc = NULL;
11678
11679 nullpo_retr(0, sd);
11680 nullpo_retr(0, id);
11681
11682 // Do normal delay assignment
11683 if (id->delay_sc <= SC_NONE || id->delay_sc >= SC_MAX || !(sc = &sd->sc))
11684 return pc_itemcd_add(sd, id, tick, n);
11685
11686 // Send reply of delay remains
11687 if (sc->data[id->delay_sc]) {
11688 const struct TimerData *timer = get_timer(sc->data[id->delay_sc]->timer);
11689 clif_msg_value(sd, ITEM_REUSE_LIMIT, timer ? DIFF_TICK(timer->tick, tick) / 1000 : 99);
11690 return 1;
11691 }
11692
11693 sc_start(&sd->bl, &sd->bl, (sc_type)id->delay_sc, 100, id->nameid, id->delay);
11694 return 0;
11695}
11696
11697/**
11698* Clear the dmglog data from player
11699* @param sd
11700* @param md
11701**/
11702static void pc_clear_log_damage_sub(uint32 char_id, struct mob_data *md)
11703{
11704 uint8 i;
11705 ARR_FIND(0,DAMAGELOG_SIZE,i,md->dmglog[i].id == char_id);
11706 if (i < DAMAGELOG_SIZE) {
11707 md->dmglog[i].id = 0;
11708 md->dmglog[i].dmg = 0;
11709 md->dmglog[i].flag = 0;
11710 }
11711}
11712
11713/**
11714* Add log to player's dmglog
11715* @param sd
11716* @param id Monster's GID
11717**/
11718void pc_damage_log_add(struct map_session_data *sd, int id)
11719{
11720 uint8 i = 0;
11721
11722 if (!sd || !id)
11723 return;
11724
11725 //Only store new data, don't need to renew the old one with same id
11726 ARR_FIND(0, DAMAGELOG_SIZE_PC, i, sd->dmglog[i] == id);
11727 if (i < DAMAGELOG_SIZE_PC)
11728 return;
11729
11730 for (i = 0; i < DAMAGELOG_SIZE_PC; i++) {
11731 if (sd->dmglog[i] == 0) {
11732 sd->dmglog[i] = id;
11733 return;
11734 }
11735 }
11736}
11737
11738/**
11739* Clear dmglog data from player
11740* @param sd
11741* @param id Monster's id
11742**/
11743void pc_damage_log_clear(struct map_session_data *sd, int id)
11744{
11745 uint8 i;
11746 struct mob_data *md = NULL;
11747
11748 if (!sd)
11749 return;
11750
11751 if (!id) {
11752 for (i = 0; i < DAMAGELOG_SIZE_PC; i++) {
11753 if( !sd->dmglog[i] ) //skip the empty value
11754 continue;
11755
11756 if ((md = map_id2md(sd->dmglog[i])))
11757 pc_clear_log_damage_sub(sd->status.char_id,md);
11758 sd->dmglog[i] = 0;
11759 }
11760 }
11761 else {
11762 if ((md = map_id2md(id)))
11763 pc_clear_log_damage_sub(sd->status.char_id,md);
11764
11765 ARR_FIND(0,DAMAGELOG_SIZE_PC,i,sd->dmglog[i] == id); // find the id position
11766 if (i < DAMAGELOG_SIZE_PC)
11767 sd->dmglog[i] = 0;
11768 }
11769}
11770
11771/**
11772 * Status change data arrived from char-server
11773 * @param sd: Player data
11774 */
11775void pc_scdata_received(struct map_session_data *sd) {
11776 pc_inventory_rentals(sd); // Needed here to remove rentals that have Status Changes after chrif_load_scdata has finished
11777
11778 clif_weight_limit( sd );
11779
11780 if( pc_has_permission( sd, PC_PERM_ATTENDANCE ) && pc_attendance_enabled() && !pc_attendance_rewarded_today( sd ) ){
11781 clif_ui_open( sd, OUT_UI_ATTENDANCE, pc_attendance_counter( sd ) );
11782 }
11783
11784 sd->state.pc_loaded = true;
11785
11786 if (sd->state.connect_new == 0 && sd->fd) { // Character already loaded map! Gotta trigger LoadEndAck manually.
11787 sd->state.connect_new = 1;
11788 clif_parse_LoadEndAck(sd->fd, sd);
11789 }
11790
11791 if (pc_iscarton(sd)) {
11792 sd->cart_weight_max = 0; // Force a client refesh
11793 status_calc_cart_weight(sd, (e_status_calc_weight_opt)(CALCWT_ITEM|CALCWT_MAXBONUS|CALCWT_CARTSTATE));
11794 }
11795}
11796
11797/**
11798 * Check player account expiration time and rental item expirations
11799 * @param sd: Player data
11800 */
11801void pc_check_expiration(struct map_session_data *sd) {
11802#ifndef ENABLE_SC_SAVING
11803 pc_inventory_rentals(sd); // Check here if Status Change saving is disabled
11804#endif
11805
11806 if (sd->expiration_time != 0) { //Don't display if it's unlimited or unknow value
11807 time_t exp_time = sd->expiration_time;
11808 char tmpstr[1024];
11809
11810 strftime(tmpstr,sizeof(tmpstr) - 1,msg_txt(sd,501),localtime(&exp_time)); // "Your account time limit is: %d-%m-%Y %H:%M:%S."
11811 clif_wis_message(sd,wisp_server_name,tmpstr,strlen(tmpstr) + 1,0);
11812
11813 pc_expire_check(sd);
11814 }
11815}
11816
11817TIMER_FUNC(pc_expiration_timer){
11818 struct map_session_data *sd = map_id2sd(id);
11819
11820 if( !sd ) return 0;
11821
11822 sd->expiration_tid = INVALID_TIMER;
11823
11824 if( sd->fd )
11825 clif_authfail_fd(sd->fd,10);
11826
11827 map_quit(sd);
11828
11829 return 0;
11830}
11831
11832TIMER_FUNC(pc_autotrade_timer){
11833 struct map_session_data *sd = map_id2sd(id);
11834
11835 if (!sd)
11836 return 0;
11837
11838 sd->autotrade_tid = INVALID_TIMER;
11839
11840 if (sd->state.autotrade&2)
11841 vending_reopen(sd);
11842 if (sd->state.autotrade&4)
11843 buyingstore_reopen(sd);
11844
11845 if (!sd->vender_id && !sd->buyer_id) {
11846 sd->state.autotrade = 0;
11847 map_quit(sd);
11848 }
11849
11850 return 0;
11851}
11852
11853/* this timer exists only when a character with a expire timer > 24h is online */
11854/* it loops thru online players once an hour to check whether a new < 24h is available */
11855TIMER_FUNC(pc_global_expiration_timer){
11856 struct s_mapiterator* iter;
11857 struct map_session_data* sd;
11858
11859 iter = mapit_getallusers();
11860
11861 for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
11862 if( sd->expiration_time )
11863 pc_expire_check(sd);
11864
11865 mapit_free(iter);
11866
11867 return 0;
11868}
11869
11870void pc_expire_check(struct map_session_data *sd) {
11871 /* ongoing timer */
11872 if( sd->expiration_tid != INVALID_TIMER )
11873 return;
11874
11875 /* not within the next 24h, enable the global check */
11876 if( sd->expiration_time > (time(NULL) + ((60 * 60) * 24)) ) {
11877
11878 /* global check not running, enable */
11879 if( pc_expiration_tid == INVALID_TIMER ) /* Starts in 1h, repeats every hour */
11880 pc_expiration_tid = add_timer_interval(gettick() + ((1000 * 60) * 60), pc_global_expiration_timer, 0, 0, ((1000 * 60) * 60));
11881
11882 return;
11883 }
11884
11885 sd->expiration_tid = add_timer(gettick() + (unsigned int)(sd->expiration_time - time(NULL)) * 1000, pc_expiration_timer, sd->bl.id, 0);
11886}
11887
11888/**
11889* Deposit some money to bank
11890* @param sd
11891* @param money Amount of money to deposit
11892**/
11893enum e_BANKING_DEPOSIT_ACK pc_bank_deposit(struct map_session_data *sd, int money) {
11894 unsigned int limit_check = money + sd->bank_vault;
11895
11896 if( money <= 0 || limit_check > MAX_BANK_ZENY ) {
11897 return BDA_OVERFLOW;
11898 } else if ( money > sd->status.zeny ) {
11899 return BDA_NO_MONEY;
11900 }
11901
11902 if( pc_payzeny(sd,money, LOG_TYPE_BANK, NULL) )
11903 return BDA_NO_MONEY;
11904
11905 sd->bank_vault += money;
11906 pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
11907 if( save_settings&CHARSAVE_BANK )
11908 chrif_save(sd, CSAVE_NORMAL);
11909 return BDA_SUCCESS;
11910}
11911
11912/**
11913* Withdraw money from bank
11914* @param sd
11915* @param money Amount of money that will be withdrawn
11916**/
11917enum e_BANKING_WITHDRAW_ACK pc_bank_withdraw(struct map_session_data *sd, int money) {
11918 unsigned int limit_check = money + sd->status.zeny;
11919
11920 if( money <= 0 ) {
11921 return BWA_UNKNOWN_ERROR;
11922 } else if ( money > sd->bank_vault ) {
11923 return BWA_NO_MONEY;
11924 } else if ( limit_check > MAX_ZENY ) {
11925 /* no official response for this scenario exists. */
11926 clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,1495),false,SELF); //You can't withdraw that much money
11927 return BWA_UNKNOWN_ERROR;
11928 }
11929
11930 if( pc_getzeny(sd,money, LOG_TYPE_BANK, NULL) )
11931 return BWA_NO_MONEY;
11932
11933 sd->bank_vault -= money;
11934 pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
11935 if( save_settings&CHARSAVE_BANK )
11936 chrif_save(sd, CSAVE_NORMAL);
11937 return BWA_SUCCESS;
11938}
11939
11940/**
11941* Clear Crimson Marker data from caster
11942* @param sd: Player
11943**/
11944void pc_crimson_marker_clear(struct map_session_data *sd) {
11945 uint8 i;
11946
11947 if (!sd)
11948 return;
11949
11950 for (i = 0; i < MAX_SKILL_CRIMSON_MARKER; i++) {
11951 struct block_list *bl = NULL;
11952 if (sd->c_marker[i] && (bl = map_id2bl(sd->c_marker[i])))
11953 status_change_end(bl,SC_C_MARKER,INVALID_TIMER);
11954 sd->c_marker[i] = 0;
11955 }
11956}
11957
11958/**
11959* Show version to player
11960* @param sd: Player
11961**/
11962void pc_show_version(struct map_session_data *sd) {
11963 const char* svn = get_svn_revision();
11964 char buf[CHAT_SIZE_MAX];
11965
11966 if( svn[0] != UNKNOWN_VERSION )
11967 sprintf(buf,msg_txt(sd,1295),"SVN: r",svn); //rAthena Version SVN: r%s
11968 else {
11969 const char* git = get_git_hash();
11970 if( git[0] != UNKNOWN_VERSION )
11971 sprintf(buf,msg_txt(sd,1295),"Git Hash: ",git); //rAthena Version Git Hash: %s
11972 else
11973 sprintf(buf,"%s",msg_txt(sd,1296)); //Cannot determine SVN/Git version.
11974 }
11975 clif_displaymessage(sd->fd,buf);
11976}
11977
11978/**
11979 * Run bonus_script on player
11980 * @param sd
11981 * @author [Cydh]
11982 **/
11983void pc_bonus_script(struct map_session_data *sd) {
11984 int now = gettick();
11985 struct linkdb_node *node = NULL, *next = NULL;
11986
11987 if (!sd || !(node = sd->bonus_script.head))
11988 return;
11989
11990 while (node) {
11991 struct s_bonus_script_entry *entry = NULL;
11992 next = node->next;
11993
11994 if ((entry = (struct s_bonus_script_entry *)node->data)) {
11995 // Only start timer for new bonus_script
11996 if (entry->tid == INVALID_TIMER) {
11997 if (entry->icon != EFST_BLANK) // Gives status icon if exist
11998 clif_status_change(&sd->bl, entry->icon, 1, entry->tick, 1, 0, 0);
11999
12000 entry->tick += now;
12001 entry->tid = add_timer(entry->tick, pc_bonus_script_timer, sd->bl.id, (intptr_t)entry);
12002 }
12003
12004 if (entry->script)
12005 run_script(entry->script, 0, sd->bl.id, 0);
12006 else
12007 ShowError("pc_bonus_script: The script has been removed somewhere. \"%s\"\n", StringBuf_Value(entry->script_buf));
12008 }
12009
12010 node = next;
12011 }
12012}
12013
12014/**
12015 * Add bonus_script to player
12016 * @param sd Player
12017 * @param script_str Script string
12018 * @param dur Duration in ms
12019 * @param icon EFST
12020 * @param flag Flags @see enum e_bonus_script_flags
12021 * @param type 0 - None, 1 - Buff, 2 - Debuff
12022 * @return New created entry pointer or NULL if failed or NULL if duplicate fail
12023 * @author [Cydh]
12024 **/
12025struct s_bonus_script_entry *pc_bonus_script_add(struct map_session_data *sd, const char *script_str, uint32 dur, enum efst_types icon, uint16 flag, uint8 type) {
12026 struct script_code *script = NULL;
12027 struct linkdb_node *node = NULL;
12028 struct s_bonus_script_entry *entry = NULL;
12029
12030 if (!sd)
12031 return NULL;
12032
12033 if (!(script = parse_script(script_str, "bonus_script", 0, SCRIPT_IGNORE_EXTERNAL_BRACKETS))) {
12034 ShowError("pc_bonus_script_add: Failed to parse script '%s' (CID:%d).\n", script_str, sd->status.char_id);
12035 return NULL;
12036 }
12037
12038 // Duplication checks
12039 if ((node = sd->bonus_script.head)) {
12040 while (node) {
12041 entry = (struct s_bonus_script_entry *)node->data;
12042 if (strcmpi(script_str, StringBuf_Value(entry->script_buf)) == 0) {
12043 int newdur = gettick() + dur;
12044 if (flag&BSF_FORCE_REPLACE && entry->tick < newdur) { // Change duration
12045 settick_timer(entry->tid, newdur);
12046 script_free_code(script);
12047 return NULL;
12048 }
12049 else if (flag&BSF_FORCE_DUPLICATE) // Allow duplicate
12050 break;
12051 else { // No duplicate bonus
12052 script_free_code(script);
12053 return NULL;
12054 }
12055 }
12056 node = node->next;
12057 }
12058 }
12059
12060 CREATE(entry, struct s_bonus_script_entry, 1);
12061
12062 entry->script_buf = StringBuf_Malloc();
12063 StringBuf_AppendStr(entry->script_buf, script_str);
12064 entry->tid = INVALID_TIMER;
12065 entry->flag = flag;
12066 entry->icon = icon;
12067 entry->tick = dur; // Use duration first, on run change to expire time
12068 entry->type = type;
12069 entry->script = script;
12070 sd->bonus_script.count++;
12071 return entry;
12072}
12073
12074/**
12075* Remove bonus_script data from player
12076* @param sd: Target player
12077* @param list: Bonus script entry from player
12078* @author [Cydh]
12079**/
12080void pc_bonus_script_free_entry(struct map_session_data *sd, struct s_bonus_script_entry *entry) {
12081 if (entry->tid != INVALID_TIMER)
12082 delete_timer(entry->tid, pc_bonus_script_timer);
12083
12084 if (entry->script)
12085 script_free_code(entry->script);
12086
12087 if (entry->script_buf)
12088 StringBuf_Free(entry->script_buf);
12089
12090 if (sd) {
12091 if (entry->icon != EFST_BLANK)
12092 clif_status_load(&sd->bl, entry->icon, 0);
12093 if (sd->bonus_script.count > 0)
12094 sd->bonus_script.count--;
12095 }
12096 aFree(entry);
12097}
12098
12099/**
12100 * Do final process if no entry left
12101 * @param sd
12102 **/
12103static void inline pc_bonus_script_check_final(struct map_session_data *sd) {
12104 if (sd->bonus_script.count == 0) {
12105 if (sd->bonus_script.head && sd->bonus_script.head->data)
12106 pc_bonus_script_free_entry(sd, (struct s_bonus_script_entry *)sd->bonus_script.head->data);
12107 linkdb_final(&sd->bonus_script.head);
12108 }
12109}
12110
12111/**
12112* Timer for bonus_script
12113* @param tid
12114* @param tick
12115* @param id
12116* @param data
12117* @author [Cydh]
12118**/
12119TIMER_FUNC(pc_bonus_script_timer){
12120 struct map_session_data *sd;
12121 struct s_bonus_script_entry *entry = (struct s_bonus_script_entry *)data;
12122
12123 sd = map_id2sd(id);
12124 if (!sd) {
12125 ShowError("pc_bonus_script_timer: Null pointer id: %d tid: %d\n", id, tid);
12126 return 0;
12127 }
12128
12129 if (tid == INVALID_TIMER)
12130 return 0;
12131
12132 if (!sd->bonus_script.head || entry == NULL) {
12133 ShowError("pc_bonus_script_timer: Invalid entry pointer 0x%08X!\n", entry);
12134 return 0;
12135 }
12136
12137 linkdb_erase(&sd->bonus_script.head, (void *)((intptr_t)entry));
12138 pc_bonus_script_free_entry(sd, entry);
12139 pc_bonus_script_check_final(sd);
12140 status_calc_pc(sd,SCO_NONE);
12141 return 0;
12142}
12143
12144/**
12145* Check then clear all active timer(s) of bonus_script data from player based on reason
12146* @param sd: Target player
12147* @param flag: Reason to remove the bonus_script. e_bonus_script_flags or e_bonus_script_types
12148* @author [Cydh]
12149**/
12150void pc_bonus_script_clear(struct map_session_data *sd, uint16 flag) {
12151 struct linkdb_node *node = NULL;
12152 uint16 count = 0;
12153
12154 if (!sd || !(node = sd->bonus_script.head))
12155 return;
12156
12157 while (node) {
12158 struct linkdb_node *next = node->next;
12159 struct s_bonus_script_entry *entry = (struct s_bonus_script_entry *)node->data;
12160
12161 if (entry && (
12162 (flag == BSF_PERMANENT) || // Remove all with permanent bonus
12163 (!flag && !(entry->flag&BSF_PERMANENT)) || // Remove all WITHOUT permanent bonus
12164 (flag&entry->flag) || // Matched flag
12165 (flag&BSF_REM_BUFF && entry->type == 1) || // Remove buff
12166 (flag&BSF_REM_DEBUFF && entry->type == 2) // Remove debuff
12167 )
12168 )
12169 {
12170 linkdb_erase(&sd->bonus_script.head, (void *)((intptr_t)entry));
12171 pc_bonus_script_free_entry(sd, entry);
12172 count++;
12173 }
12174
12175 node = next;
12176 }
12177
12178 pc_bonus_script_check_final(sd);
12179
12180 if (count && !(flag&BSF_REM_ON_LOGOUT)) //Don't need to do this if log out
12181 status_calc_pc(sd,SCO_NONE);
12182}
12183
12184/** [Cydh]
12185 * Gives/removes SC_BASILICA when player steps in/out the cell with 'cell_basilica'
12186 * @param sd: Target player
12187 */
12188void pc_cell_basilica(struct map_session_data *sd) {
12189 nullpo_retv(sd);
12190
12191 if (!map_getcell(sd->bl.m,sd->bl.x,sd->bl.y,CELL_CHKBASILICA)) {
12192 if (sd->sc.data[SC_BASILICA])
12193 status_change_end(&sd->bl,SC_BASILICA,INVALID_TIMER);
12194 }
12195 else if (!sd->sc.data[SC_BASILICA])
12196 sc_start(&sd->bl,&sd->bl,SC_BASILICA,100,0,-1);
12197}
12198
12199/** [Cydh]
12200 * Get maximum specified parameter for specified class
12201 * @param class_: sd->class
12202 * @param sex: sd->status.sex
12203 * @param flag: parameter will be checked
12204 * @return max_param
12205 */
12206short pc_maxparameter(struct map_session_data *sd, enum e_params param) {
12207 int idx = -1, class_ = sd->class_;
12208
12209 if ((idx = pc_class2idx(pc_mapid2jobid(class_,sd->status.sex))) >= 0) {
12210 short max_param = 0;
12211 switch (param) {
12212 case PARAM_STR: max_param = job_info[idx].max_param.str; break;
12213 case PARAM_AGI: max_param = job_info[idx].max_param.agi; break;
12214 case PARAM_VIT: max_param = job_info[idx].max_param.vit; break;
12215 case PARAM_INT: max_param = job_info[idx].max_param.int_; break;
12216 case PARAM_DEX: max_param = job_info[idx].max_param.dex; break;
12217 case PARAM_LUK: max_param = job_info[idx].max_param.luk; break;
12218 }
12219 if (max_param > 0)
12220 return max_param;
12221 }
12222
12223 return (class_&MAPID_BASEMASK) == MAPID_SUMMONER ? battle_config.max_summoner_parameter :
12224 ((class_&MAPID_UPPERMASK) == MAPID_KAGEROUOBORO || (class_&MAPID_UPPERMASK) == MAPID_REBELLION) ? battle_config.max_extended_parameter :
12225 ((class_&JOBL_THIRD) ? ((class_&JOBL_UPPER) ? battle_config.max_third_trans_parameter : ((class_&JOBL_BABY) ? battle_config.max_baby_third_parameter : battle_config.max_third_parameter)) :
12226 ((class_&JOBL_BABY) ? battle_config.max_baby_parameter :
12227 ((class_&JOBL_UPPER) ? battle_config.max_trans_parameter : battle_config.max_parameter)));
12228}
12229
12230/**
12231* Get max ASPD for player based on Class
12232* @param sd Player
12233* @return ASPD
12234*/
12235short pc_maxaspd(struct map_session_data *sd) {
12236 nullpo_ret(sd);
12237
12238 return (( sd->class_&JOBL_THIRD) ? battle_config.max_third_aspd : (
12239 ((sd->class_&MAPID_UPPERMASK) == MAPID_KAGEROUOBORO || (sd->class_&MAPID_UPPERMASK) == MAPID_REBELLION) ? battle_config.max_extended_aspd :
12240 battle_config.max_aspd ));
12241}
12242
12243/**
12244* Calculates total item-group related bonuses for the given item
12245* @param sd Player
12246* @param nameid Item ID
12247* @return Heal rate
12248**/
12249short pc_get_itemgroup_bonus(struct map_session_data* sd, unsigned short nameid) {
12250 short bonus = 0;
12251 uint8 i;
12252
12253 if (!sd->itemgrouphealrate_count)
12254 return bonus;
12255 for (i = 0; i < sd->itemgrouphealrate_count; i++) {
12256 uint16 group_id = sd->itemgrouphealrate[i]->group_id, j;
12257 struct s_item_group_db *group = NULL;
12258 if (!group_id || !(group = itemdb_group_exists(group_id)))
12259 continue;
12260
12261 for (j = 0; j < group->random[0].data_qty; j++) {
12262 if (group->random[0].data[j].nameid == nameid) {
12263 bonus += sd->itemgrouphealrate[i]->rate;
12264 break;
12265 }
12266 }
12267 }
12268 return bonus;
12269}
12270
12271/**
12272* Calculates total item-group related bonuses for the given item group
12273* @param sd Player
12274* @param group_id Item Group ID
12275* @return Heal rate
12276**/
12277short pc_get_itemgroup_bonus_group(struct map_session_data* sd, uint16 group_id) {
12278 short bonus = 0;
12279 uint8 i;
12280
12281 if (!sd->itemgrouphealrate_count)
12282 return bonus;
12283 for (i = 0; i < sd->itemgrouphealrate_count; i++) {
12284 if (sd->itemgrouphealrate[i]->group_id == group_id)
12285 return sd->itemgrouphealrate[i]->rate;
12286 }
12287 return bonus;
12288}
12289
12290/**
12291* Check if player's equip index in same specified position, like for 2-Handed weapon & Heagdear (inc. costume)
12292* @param eqi Item EQI of enum equip_index
12293* @param *equip_index Player's equip_index[]
12294* @param index Known index item in inventory from sd->equip_index[] to compare with specified EQI in *equip_index
12295* @return True if item in same inventory index, False if doesn't
12296*/
12297bool pc_is_same_equip_index(enum equip_index eqi, short *equip_index, short index) {
12298 if (index < 0 || index >= MAX_INVENTORY)
12299 return true;
12300 // Dual weapon checks
12301 if (eqi == EQI_HAND_R && equip_index[EQI_HAND_L] == index)
12302 return true;
12303 // Headgear with Mid & Low location
12304 if (eqi == EQI_HEAD_MID && equip_index[EQI_HEAD_LOW] == index)
12305 return true;
12306 // Headgear with Top & Mid or Low location
12307 if (eqi == EQI_HEAD_TOP && (equip_index[EQI_HEAD_MID] == index || equip_index[EQI_HEAD_LOW] == index))
12308 return true;
12309 // Headgear with Mid & Low location
12310 if (eqi == EQI_COSTUME_HEAD_MID && equip_index[EQI_COSTUME_HEAD_LOW] == index)
12311 return true;
12312 // Headgear with Top & Mid or Low location
12313 if (eqi == EQI_COSTUME_HEAD_TOP && (equip_index[EQI_COSTUME_HEAD_MID] == index || equip_index[EQI_COSTUME_HEAD_LOW] == index))
12314 return true;
12315 return false;
12316}
12317
12318/**
12319 * Generate Unique item ID for player
12320 * @param sd : Player
12321 * @return A generated Unique item ID
12322 */
12323uint64 pc_generate_unique_id(struct map_session_data *sd) {
12324 nullpo_ret(sd);
12325 return ((uint64)sd->status.char_id << 32) | sd->status.uniqueitem_counter++;
12326}
12327
12328/**
12329 * Validating skill from player after logged on
12330 * @param sd
12331 **/
12332void pc_validate_skill(struct map_session_data *sd) {
12333 if (sd) {
12334 uint16 i = 0, count = 0;
12335 struct s_skill tmp_skills[MAX_SKILL] = {{ 0 }};
12336
12337 memcpy(tmp_skills, sd->status.skill, sizeof(sd->status.skill));
12338 memset(sd->status.skill, 0, sizeof(sd->status.skill));
12339
12340 for (i = 0; i < MAX_SKILL; i++) {
12341 uint16 idx = 0;
12342 if (tmp_skills[i].id == 0 || tmp_skills[i].lv == 0)
12343 continue;
12344 if ((idx = skill_get_index(tmp_skills[i].id))) {
12345 memcpy(&sd->status.skill[idx], &tmp_skills[i], sizeof(tmp_skills[i]));
12346 count++;
12347 }
12348 else
12349 ShowWarning("pc_validate_skill: Removing invalid skill '%d' from player (AID=%d CID=%d).\n", tmp_skills[i].id, sd->status.account_id, sd->status.char_id);
12350 }
12351 }
12352}
12353
12354/**
12355 * Toggle to remember if the questinfo is displayed yet or not.
12356 * @param qi_display Display flag
12357 * @param show If show is true and qi_display is 0, set qi_display to 1 and show the event bubble.
12358 * If show is false and qi_display is 1, set qi_display to 0 and hide the event bubble.
12359 **/
12360static void pc_show_questinfo_sub(struct map_session_data *sd, bool *qi_display, struct questinfo *qi, bool show) {
12361 if (show) {
12362 // Check if need to be displayed
12363 if ((*qi_display) != 1) {
12364 (*qi_display) = 1;
12365 clif_quest_show_event(sd, &qi->nd->bl, qi->icon, qi->color);
12366 }
12367 }
12368 else {
12369 // Check if need to be hide
12370 if ((*qi_display) != 0) {
12371 (*qi_display) = 0;
12372#if PACKETVER >= 20120410
12373 clif_quest_show_event(sd, &qi->nd->bl, 9999, 0);
12374#else
12375 clif_quest_show_event(sd, &qi->nd->bl, 0, 0);
12376#endif
12377 }
12378 }
12379}
12380
12381/**
12382 * Show available NPC Quest / Event Icon Check [Kisuka]
12383 * @param sd Player
12384 **/
12385void pc_show_questinfo(struct map_session_data *sd) {
12386#if PACKETVER >= 20090218
12387 struct questinfo *qi = NULL;
12388 unsigned short i;
12389 uint8 j;
12390 int8 mystate = 0;
12391 bool failed = false;
12392
12393 nullpo_retv(sd);
12394
12395 if (sd->bl.m < 0 || sd->bl.m >= MAX_MAPINDEX)
12396 return;
12397
12398 struct map_data *mapdata = map_getmapdata(sd->bl.m);
12399
12400 if (!mapdata->qi_count || !mapdata->qi_data)
12401 return;
12402 if (mapdata->qi_count != sd->qi_count)
12403 return; // init was not called yet
12404
12405 for(i = 0; i < mapdata->qi_count; i++) {
12406 qi = &mapdata->qi_data[i];
12407
12408 if (!qi)
12409 continue;
12410
12411 if (quest_check(sd, qi->quest_id, HAVEQUEST) != -1) { // Check if quest is not started
12412 pc_show_questinfo_sub(sd, &sd->qi_display[i], qi, false);
12413 continue;
12414 }
12415
12416 // Level range checks
12417 if (sd->status.base_level < qi->min_level || sd->status.base_level > qi->max_level) {
12418 pc_show_questinfo_sub(sd, &sd->qi_display[i], qi, false);
12419 continue;
12420 }
12421
12422 // Quest requirements
12423 if (qi->req_count) {
12424 failed = false;
12425 for (j = 0; j < qi->req_count; j++) {
12426 mystate = quest_check(sd, qi->req[j].quest_id, HAVEQUEST);
12427 mystate = mystate + (mystate < 1);
12428 if (mystate != qi->req[j].state) {
12429 failed = true;
12430 break;
12431 }
12432 }
12433 if (failed) {
12434 pc_show_questinfo_sub(sd, &sd->qi_display[i], qi, false);
12435 continue;
12436 }
12437 }
12438
12439 // Job requirements
12440 if (qi->jobid_count) {
12441 failed = true;
12442 for (j = 0; j < qi->jobid_count; j++) {
12443 if (pc_mapid2jobid(sd->class_,sd->status.sex) == qi->jobid[j]) {
12444 pc_show_questinfo_sub(sd, &sd->qi_display[i], qi, true);
12445 failed = false;
12446 break;
12447 }
12448 }
12449 if (!failed)
12450 continue;
12451 pc_show_questinfo_sub(sd, &sd->qi_display[i], qi, false);
12452 }
12453 else {
12454 pc_show_questinfo_sub(sd, &sd->qi_display[i], qi, true);
12455 }
12456 }
12457#endif
12458}
12459
12460/**
12461 * Reinit the questinfo for player when changing map
12462 * @param sd Player
12463 **/
12464void pc_show_questinfo_reinit(struct map_session_data *sd) {
12465#if PACKETVER >= 20090218
12466 nullpo_retv(sd);
12467
12468 if (sd->qi_display) {
12469 aFree(sd->qi_display);
12470 sd->qi_display = NULL;
12471 }
12472 sd->qi_count = 0;
12473
12474 if (sd->bl.m < 0 || sd->bl.m >= MAX_MAPINDEX)
12475 return;
12476
12477 struct map_data *mapdata = map_getmapdata(sd->bl.m);
12478
12479 if (!mapdata->qi_count || !mapdata->qi_data)
12480 return;
12481 CREATE(sd->qi_display, bool, (sd->qi_count = mapdata->qi_count));
12482#endif
12483}
12484
12485/**
12486 * Check if a job is allowed to enter the map
12487 * @param jobid Job ID see enum e_job or sd->status.class_
12488 * @param m ID -an index- for direct indexing map[] array
12489 * @return 1 if job is allowed, 0 otherwise
12490 **/
12491bool pc_job_can_entermap(enum e_job jobid, int m, int group_lv) {
12492 uint16 idx = 0;
12493
12494 // Map is other map server.
12495 // !FIXME: Currently, a map-server doesn't recognized map's attributes on other server, so we assume it's fine to warp.
12496 if (m < 0)
12497 return true;
12498
12499 struct map_data *mapdata = map_getmapdata(m);
12500
12501 if (!mapdata->cell)
12502 return false;
12503
12504 if (!pcdb_checkid(jobid))
12505 return false;
12506
12507 idx = pc_class2idx(jobid);
12508 if (!job_info[idx].noenter_map.zone || group_lv > job_info[idx].noenter_map.group_lv)
12509 return true;
12510
12511 if ((job_info[idx].noenter_map.zone&1 && !mapdata_flag_vs2(mapdata)) || // Normal
12512 (job_info[idx].noenter_map.zone&2 && mapdata->flag[MF_PVP]) || // PVP
12513 (job_info[idx].noenter_map.zone&4 && mapdata_flag_gvg2_no_te(mapdata)) || // GVG
12514 (job_info[idx].noenter_map.zone&8 && mapdata->flag[MF_BATTLEGROUND]) || // Battleground
12515 (job_info[idx].noenter_map.zone&16 && mapdata_flag_gvg2_te(mapdata)) || // WOE:TE
12516 (job_info[idx].noenter_map.zone&(8*mapdata->zone) && mapdata->flag[MF_RESTRICTED]) // Zone restriction
12517 )
12518 return false;
12519
12520 return true;
12521}
12522
12523/**
12524 * Tells client about player's costume view on mapchange for checking 'nocostume' mapflag.
12525 * @param sd
12526 **/
12527void pc_set_costume_view(struct map_session_data *sd) {
12528 int i = -1, head_low = 0, head_mid = 0, head_top = 0, robe = 0;
12529 struct item_data *id = NULL;
12530
12531 nullpo_retv(sd);
12532
12533 head_low = sd->status.head_bottom;
12534 head_mid = sd->status.head_mid;
12535 head_top = sd->status.head_top;
12536 robe = sd->status.robe;
12537
12538 sd->status.head_bottom = sd->status.head_mid = sd->status.head_top = sd->status.robe = 0;
12539
12540 //Added check to prevent sending the same look on multiple slots ->
12541 //causes client to redraw item on top of itself. (suggested by Lupus)
12542 // Normal headgear checks
12543 if ((i = sd->equip_index[EQI_HEAD_LOW]) != -1 && (id = sd->inventory_data[i])) {
12544 if (!(id->equip&(EQP_HEAD_MID|EQP_HEAD_TOP)))
12545 sd->status.head_bottom = id->look;
12546 else
12547 sd->status.head_bottom = 0;
12548 }
12549 if ((i = sd->equip_index[EQI_HEAD_MID]) != -1 && (id = sd->inventory_data[i])) {
12550 if (!(id->equip&(EQP_HEAD_TOP)))
12551 sd->status.head_mid = id->look;
12552 else
12553 sd->status.head_mid = 0;
12554 }
12555 if ((i = sd->equip_index[EQI_HEAD_TOP]) != -1 && (id = sd->inventory_data[i]))
12556 sd->status.head_top = id->look;
12557 if ((i = sd->equip_index[EQI_GARMENT]) != -1 && (id = sd->inventory_data[i]))
12558 sd->status.robe = id->look;
12559
12560 // Costumes check
12561 if (!map_getmapflag(sd->bl.m, MF_NOCOSTUME)) {
12562 if ((i = sd->equip_index[EQI_COSTUME_HEAD_LOW]) != -1 && (id = sd->inventory_data[i])) {
12563 if (!(id->equip&(EQP_COSTUME_HEAD_MID|EQP_COSTUME_HEAD_TOP)))
12564 sd->status.head_bottom = id->look;
12565 else
12566 sd->status.head_bottom = 0;
12567 }
12568 if ((i = sd->equip_index[EQI_COSTUME_HEAD_MID]) != -1 && (id = sd->inventory_data[i])) {
12569 if (!(id->equip&EQP_COSTUME_HEAD_TOP))
12570 sd->status.head_mid = id->look;
12571 else
12572 sd->status.head_mid = 0;
12573 }
12574 if ((i = sd->equip_index[EQI_COSTUME_HEAD_TOP]) != -1 && (id = sd->inventory_data[i]))
12575 sd->status.head_top = id->look;
12576 if ((i = sd->equip_index[EQI_COSTUME_GARMENT]) != -1 && (id = sd->inventory_data[i]))
12577 sd->status.robe = id->look;
12578 }
12579
12580 if (sd->setlook_head_bottom)
12581 sd->status.head_bottom = sd->setlook_head_bottom;
12582 if (sd->setlook_head_mid)
12583 sd->status.head_mid = sd->setlook_head_mid;
12584 if (sd->setlook_head_top)
12585 sd->status.head_top = sd->setlook_head_top;
12586 if (sd->setlook_robe)
12587 sd->status.robe = sd->setlook_robe;
12588
12589 if (head_low != sd->status.head_bottom)
12590 clif_changelook(&sd->bl, LOOK_HEAD_BOTTOM, sd->status.head_bottom);
12591 if (head_mid != sd->status.head_mid)
12592 clif_changelook(&sd->bl, LOOK_HEAD_MID, sd->status.head_mid);
12593 if (head_top != sd->status.head_top)
12594 clif_changelook(&sd->bl, LOOK_HEAD_TOP, sd->status.head_top);
12595 if (robe != sd->status.robe)
12596 clif_changelook(&sd->bl, LOOK_ROBE, sd->status.robe);
12597}
12598
12599struct s_attendance_period* pc_attendance_period(){
12600 uint32 date = date_get(DT_YYYYMMDD);
12601
12602 for( struct s_attendance_period& period : attendance_periods ){
12603 if( period.start <= date && period.end >= date ){
12604 return .
12605 }
12606 }
12607
12608 return nullptr;
12609}
12610
12611bool pc_attendance_enabled(){
12612 // Check if the attendance feature is disabled
12613 if( !battle_config.feature_attendance ){
12614 return false;
12615 }
12616
12617 // Check if there is a running attendance period
12618 return pc_attendance_period() != nullptr;
12619}
12620
12621static inline bool pc_attendance_rewarded_today( struct map_session_data* sd ){
12622 return pc_readreg2( sd, ATTENDANCE_DATE_VAR ) >= date_get(DT_YYYYMMDD);
12623}
12624
12625int32 pc_attendance_counter( struct map_session_data* sd ){
12626 struct s_attendance_period* period = pc_attendance_period();
12627
12628 // No running attendance period
12629 if( period == nullptr ){
12630 return 0;
12631 }
12632
12633 // Get the counter for the current period
12634 int counter = pc_readreg2( sd, ATTENDANCE_COUNT_VAR );
12635
12636 // Check if we have a remaining counter from a previous period
12637 if( counter > 0 && pc_readreg2( sd, ATTENDANCE_DATE_VAR ) < period->start ){
12638 // Reset the counter to zero
12639 pc_setreg2( sd, ATTENDANCE_COUNT_VAR, 0 );
12640
12641 return 0;
12642 }
12643
12644 return 10 * counter + ( ( pc_attendance_rewarded_today(sd) ) ? 1 : 0 );
12645}
12646
12647void pc_attendance_claim_reward( struct map_session_data* sd ){
12648 // If the user's group does not have the permission
12649 if( !pc_has_permission( sd, PC_PERM_ATTENDANCE ) ){
12650 return;
12651 }
12652
12653 // Check if the attendance feature is disabled
12654 if( !pc_attendance_enabled() ){
12655 return;
12656 }
12657
12658 // Check if the user already got his reward today
12659 if( pc_attendance_rewarded_today( sd ) ){
12660 return;
12661 }
12662
12663 int32 attendance_counter = pc_readreg2( sd, ATTENDANCE_COUNT_VAR );
12664
12665 attendance_counter += 1;
12666
12667 struct s_attendance_period* period = pc_attendance_period();
12668
12669 if( period == nullptr ){
12670 return;
12671 }
12672
12673 if( period->rewards.size() < attendance_counter ){
12674 return;
12675 }
12676
12677 pc_setreg2( sd, ATTENDANCE_DATE_VAR, date_get(DT_YYYYMMDD) );
12678 pc_setreg2( sd, ATTENDANCE_COUNT_VAR, attendance_counter );
12679
12680 if( save_settings&CHARSAVE_ATTENDANCE )
12681 chrif_save(sd, CSAVE_NORMAL);
12682
12683 struct s_attendance_reward& reward = period->rewards.at( attendance_counter - 1 );
12684
12685 struct mail_message msg;
12686
12687 memset( &msg, 0, sizeof( struct mail_message ) );
12688
12689 msg.dest_id = sd->status.char_id;
12690 safestrncpy( msg.send_name, msg_txt( sd, 788 ), NAME_LENGTH );
12691 safesnprintf( msg.title, MAIL_TITLE_LENGTH, msg_txt( sd, 789 ), attendance_counter );
12692 safesnprintf( msg.body, MAIL_BODY_LENGTH, msg_txt( sd, 790 ), attendance_counter );
12693
12694 msg.item[0].nameid = reward.item_id;
12695 msg.item[0].amount = reward.amount;
12696 msg.item[0].identify = 1;
12697
12698 msg.status = MAIL_NEW;
12699 msg.type = MAIL_INBOX_NORMAL;
12700 msg.timestamp = time(NULL);
12701
12702 intif_Mail_send(0, &msg);
12703
12704 clif_attendence_response( sd, attendance_counter );
12705}
12706
12707void pc_attendance_load( std::string path ){
12708 YAML::Node root;
12709
12710 try{
12711 root = YAML::LoadFile( path );
12712 }catch( ... ){
12713 ShowError( "pc_attendance_load: Failed to read attendance configuration file \"%s\".\n", path.c_str() );
12714 return;
12715 }
12716
12717 if( root["Attendance"] ){
12718 YAML::Node attendance = root["Attendance"];
12719
12720 for( const auto &periodNode : attendance ){
12721 if( !periodNode["Start"].IsDefined() ){
12722 ShowError( "pc_attendance_load: Missing \"Start\" for period in line %d.\n", periodNode.Mark().line );
12723 continue;
12724 }
12725
12726 YAML::Node startNode = periodNode["Start"];
12727
12728 if( !periodNode["End"].IsDefined() ){
12729 ShowError( "pc_attendance_load: Missing \"End\" for period in line %d.\n", periodNode.Mark().line );
12730 continue;
12731 }
12732
12733 YAML::Node endNode = periodNode["End"];
12734
12735 if( !periodNode["Rewards"].IsDefined() ){
12736 ShowError( "pc_attendance_load: Missing \"Rewards\" for period in line %d.\n", periodNode.Mark().line );
12737 continue;
12738 }
12739
12740 YAML::Node rewardsNode = periodNode["Rewards"];
12741
12742 uint32 start = startNode.as<uint32>();
12743 uint32 end = endNode.as<uint32>();
12744
12745 // If the period is outdated already, we do not even bother parsing
12746 if( end < date_get( DT_YYYYMMDD ) ){
12747 continue;
12748 }
12749
12750 // Collision detection
12751 bool collision = false;
12752
12753 for( struct s_attendance_period& period : attendance_periods ){
12754 // Check if start is inside another period
12755 if( period.start <= start && start <= period.end ){
12756 ShowError( "pc_attendance_load: period start %u intersects with period %u-%u.\n", start, period.start, period.end );
12757 collision = true;
12758 break;
12759 }
12760
12761 // Check if end is inside another period
12762 if( period.start <= end && end <= period.end ){
12763 ShowError( "pc_attendance_load: period end %u intersects with period %u-%u.\n", start, period.start, period.end );
12764 collision = true;
12765 break;
12766 }
12767 }
12768
12769 if( collision ){
12770 continue;
12771 }
12772
12773 struct s_attendance_period period;
12774
12775 period.start = start;
12776 period.end = end;
12777
12778 for( const auto& rewardNode : rewardsNode ){
12779 if( !rewardNode["Day"].IsDefined() ){
12780 ShowError( "pc_attendance_load: No day defined for node in line %d.\n", rewardNode.Mark().line );
12781 continue;
12782 }
12783
12784 uint32 day = rewardNode["Day"].as<uint32>();
12785
12786 if( !rewardNode["ItemId"].IsDefined() ){
12787 ShowError( "pc_attendance_load: No reward defined for day %d.\n", day );
12788 continue;
12789 }
12790
12791 YAML::Node itemNode = rewardNode["ItemId"];
12792
12793 uint16 item_id = itemNode.as<uint16>();
12794
12795 if( item_id == 0 || !itemdb_exists( item_id ) ){
12796 ShowError( "pc_attendance_load: Unknown item ID %hu for day %d.\n", item_id, day );
12797 continue;
12798 }
12799
12800 uint16 amount;
12801
12802 if( rewardNode["Amount"] ){
12803 amount = rewardNode["Amount"].as<uint16>();
12804
12805 if( amount == 0 ){
12806 ShowError( "pc_attendance_load: Invalid reward count %hu for day %d. Defaulting to 1...\n", amount, day );
12807 amount = 1;
12808 }else if( amount > MAX_AMOUNT ){
12809 ShowError( "pc_attendance_load: Reward count %hu above maximum %hu for day %d. Defaulting to %hu...\n", amount, MAX_AMOUNT, day, MAX_AMOUNT );
12810 amount = MAX_AMOUNT;
12811 }
12812 }else{
12813 amount = 1;
12814 }
12815
12816 struct s_attendance_reward* reward = &period.rewards[day - 1];
12817
12818 reward->item_id = item_id;
12819 reward->amount = amount;
12820 }
12821
12822 bool missing_day = false;
12823
12824 for( int day = 0; day < period.rewards.size(); day++ ){
12825 if( !util::map_exists( period.rewards, day ) ){
12826 ShowError( "pc_attendance_load: Reward for day %d is missing.\n", day + 1 );
12827 missing_day = true;
12828 break;
12829 }
12830 }
12831
12832 if( missing_day ){
12833 continue;
12834 }
12835
12836 attendance_periods.push_back( period );
12837 }
12838 }
12839}
12840
12841void pc_read_attendance(){
12842 char path[1024];
12843
12844 sprintf( path, "%s/%sattendance.yml", db_path, DBPATH );
12845
12846 pc_attendance_load( path );
12847
12848 sprintf( path, "%s/%s/attendance.yml", db_path, DBIMPORT );
12849
12850 pc_attendance_load( path );
12851}
12852
12853/*==========================================
12854 * pc Init/Terminate
12855 *------------------------------------------*/
12856void do_final_pc(void) {
12857 db_destroy(itemcd_db);
12858 do_final_pc_groups();
12859
12860 ers_destroy(pc_sc_display_ers);
12861 ers_destroy(pc_itemgrouphealrate_ers);
12862 ers_destroy(num_reg_ers);
12863 ers_destroy(str_reg_ers);
12864
12865 attendance_periods.clear();
12866}
12867
12868void do_init_pc(void) {
12869
12870 itemcd_db = idb_alloc(DB_OPT_RELEASE_DATA);
12871
12872 pc_readdb();
12873 pc_read_motd(); // Read MOTD [Valaris]
12874 pc_read_attendance();
12875
12876 add_timer_func_list(pc_invincible_timer, "pc_invincible_timer");
12877 add_timer_func_list(pc_eventtimer, "pc_eventtimer");
12878 add_timer_func_list(pc_inventory_rental_end, "pc_inventory_rental_end");
12879 add_timer_func_list(pc_calc_pvprank_timer, "pc_calc_pvprank_timer");
12880 add_timer_func_list(pc_autosave, "pc_autosave");
12881 add_timer_func_list(pc_spiritball_timer, "pc_spiritball_timer");
12882 add_timer_func_list(pc_follow_timer, "pc_follow_timer");
12883 add_timer_func_list(pc_endautobonus, "pc_endautobonus");
12884 add_timer_func_list(pc_spiritcharm_timer, "pc_spiritcharm_timer");
12885 add_timer_func_list(pc_global_expiration_timer, "pc_global_expiration_timer");
12886 add_timer_func_list(pc_expiration_timer, "pc_expiration_timer");
12887 add_timer_func_list(pc_autotrade_timer, "pc_autotrade_timer");
12888
12889 add_timer(gettick() + autosave_interval, pc_autosave, 0, 0);
12890
12891 // 0=day, 1=night [Yor]
12892 night_flag = battle_config.night_at_start ? 1 : 0;
12893
12894 if (battle_config.day_duration > 0 && battle_config.night_duration > 0) {
12895 int day_duration = battle_config.day_duration;
12896 int night_duration = battle_config.night_duration;
12897 // add night/day timer [Yor]
12898 add_timer_func_list(map_day_timer, "map_day_timer");
12899 add_timer_func_list(map_night_timer, "map_night_timer");
12900
12901 day_timer_tid = add_timer_interval(gettick() + (night_flag ? 0 : day_duration) + night_duration, map_day_timer, 0, 0, day_duration + night_duration);
12902 night_timer_tid = add_timer_interval(gettick() + day_duration + (night_flag ? night_duration : 0), map_night_timer, 0, 0, day_duration + night_duration);
12903 }
12904
12905 do_init_pc_groups();
12906
12907 pc_sc_display_ers = ers_new(sizeof(struct sc_display_entry), "pc.cpp:pc_sc_display_ers", ERS_OPT_FLEX_CHUNK);
12908 pc_itemgrouphealrate_ers = ers_new(sizeof(struct s_pc_itemgrouphealrate), "pc.cpp:pc_itemgrouphealrate_ers", ERS_OPT_NONE);
12909 num_reg_ers = ers_new(sizeof(struct script_reg_num), "pc.cpp:num_reg_ers", (ERSOptions)(ERS_OPT_CLEAN|ERS_OPT_FLEX_CHUNK));
12910 str_reg_ers = ers_new(sizeof(struct script_reg_str), "pc.cpp:str_reg_ers", (ERSOptions)(ERS_OPT_CLEAN|ERS_OPT_FLEX_CHUNK));
12911
12912 ers_chunk_size(pc_sc_display_ers, 150);
12913 ers_chunk_size(num_reg_ers, 300);
12914 ers_chunk_size(str_reg_ers, 50);
12915}