pc.cpp 450 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937
  1. // Copyright (c) rAthena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #include "pc.hpp"
  4. #include <map>
  5. #include <math.h>
  6. #include <stdlib.h>
  7. #include "../common/cbasetypes.hpp"
  8. #include "../common/core.hpp" // get_svn_revision()
  9. #include "../common/database.hpp"
  10. #include "../common/ers.hpp" // ers_destroy
  11. #include "../common/malloc.hpp"
  12. #include "../common/mmo.hpp" //NAME_LENGTH
  13. #include "../common/nullpo.hpp"
  14. #include "../common/random.hpp"
  15. #include "../common/showmsg.hpp"
  16. #include "../common/socket.hpp" // session[]
  17. #include "../common/strlib.hpp" // safestrncpy()
  18. #include "../common/timer.hpp"
  19. #include "../common/utilities.hpp"
  20. #include "../common/utils.hpp"
  21. #include "achievement.hpp"
  22. #include "atcommand.hpp" // get_atcommand_level()
  23. #include "battle.hpp" // battle_config
  24. #include "battleground.hpp"
  25. #include "buyingstore.hpp" // struct s_buyingstore
  26. #include "channel.hpp"
  27. #include "chat.hpp"
  28. #include "chrif.hpp"
  29. #include "clan.hpp"
  30. #include "clif.hpp"
  31. #include "date.hpp" // is_day_of_*()
  32. #include "duel.hpp"
  33. #include "elemental.hpp"
  34. #include "guild.hpp"
  35. #include "homunculus.hpp"
  36. #include "instance.hpp"
  37. #include "intif.hpp"
  38. #include "itemdb.hpp" // MAX_ITEMGROUP
  39. #include "log.hpp"
  40. #include "map.hpp"
  41. #include "mercenary.hpp"
  42. #include "mob.hpp"
  43. #include "npc.hpp"
  44. #include "party.hpp" // party_search()
  45. #include "pc_groups.hpp"
  46. #include "pet.hpp" // pet_unlocktarget()
  47. #include "quest.hpp"
  48. #include "script.hpp" // struct script_reg, struct script_regstr
  49. #include "searchstore.hpp" // struct s_search_store_info
  50. #include "status.hpp" // OPTION_*, struct weapon_atk
  51. #include "storage.hpp"
  52. #include "unit.hpp" // unit_stop_attack(), unit_stop_walking()
  53. #include "vending.hpp" // struct s_vending
  54. using namespace rathena;
  55. JobDatabase job_db;
  56. int pc_split_atoui(char* str, unsigned int* val, char sep, int max);
  57. static inline bool pc_attendance_rewarded_today( struct map_session_data* sd );
  58. #define PVP_CALCRANK_INTERVAL 1000 // PVP calculation interval
  59. PlayerStatPointDatabase statpoint_db;
  60. SkillTreeDatabase skill_tree_db;
  61. // timer for night.day implementation
  62. int day_timer_tid = INVALID_TIMER;
  63. int night_timer_tid = INVALID_TIMER;
  64. struct eri *pc_sc_display_ers = NULL;
  65. struct eri *num_reg_ers;
  66. struct eri *str_reg_ers;
  67. int pc_expiration_tid = INVALID_TIMER;
  68. struct fame_list smith_fame_list[MAX_FAME_LIST];
  69. struct fame_list chemist_fame_list[MAX_FAME_LIST];
  70. struct fame_list taekwon_fame_list[MAX_FAME_LIST];
  71. const std::string AttendanceDatabase::getDefaultLocation(){
  72. return std::string(db_path) + "/attendance.yml";
  73. }
  74. /**
  75. * Reads and parses an entry from the attendance_db.
  76. * @param node: YAML node containing the entry.
  77. * @return count of successfully parsed rows
  78. */
  79. uint64 AttendanceDatabase::parseBodyNode(const ryml::NodeRef& node){
  80. uint32 start;
  81. if( !this->asUInt32( node, "Start", start ) ){
  82. return 0;
  83. }
  84. std::shared_ptr<s_attendance_period> attendance_period = this->find( start );
  85. bool exists = attendance_period != nullptr;
  86. if( !exists ){
  87. if( !this->nodeExists( node, "End" ) ){
  88. this->invalidWarning( node, "Node \"End\" is missing.\n" );
  89. return 0;
  90. }
  91. if( !this->nodeExists( node, "Rewards" ) ){
  92. this->invalidWarning( node, "Node \"Rewards\" is missing.\n" );
  93. return 0;
  94. }
  95. attendance_period = std::make_shared<s_attendance_period>();
  96. attendance_period->start = start;
  97. }
  98. // If it does not exist yet, we need to check it for sure
  99. bool requiresCollisionDetection = !exists;
  100. if( this->nodeExists( node, "End" ) ){
  101. uint32 end;
  102. if( !this->asUInt32( node, "End", end ) ){
  103. return 0;
  104. }
  105. // If the period is outdated already, we do not even bother parsing
  106. if( end < date_get( DT_YYYYMMDD ) ){
  107. this->invalidWarning( node, "Node \"End\" date %u has already passed, skipping.\n", end );
  108. return 0;
  109. }
  110. if( !exists || attendance_period->end != end ){
  111. requiresCollisionDetection = true;
  112. attendance_period->end = end;
  113. }
  114. }
  115. // Collision detection
  116. if( requiresCollisionDetection ){
  117. bool collision = false;
  118. for( std::pair<const uint32,std::shared_ptr<s_attendance_period>>& pair : *this ){
  119. std::shared_ptr<s_attendance_period> period = pair.second;
  120. if( exists && period->start == attendance_period->start ){
  121. // Dont compare to yourself
  122. continue;
  123. }
  124. // Check if start is inside another period
  125. if( period->start <= attendance_period->start && start <= period->end ){
  126. this->invalidWarning( node, "Node \"Start\" period %u intersects with period %u-%u, skipping.\n", attendance_period->start, period->start, period->end );
  127. collision = true;
  128. break;
  129. }
  130. // Check if end is inside another period
  131. if( period->start <= attendance_period->end && attendance_period->end <= period->end ){
  132. this->invalidWarning( node, "Node \"End\" period %u intersects with period %u-%u.\n", attendance_period->start, period->start, period->end );
  133. collision = true;
  134. break;
  135. }
  136. }
  137. if( collision ){
  138. return 0;
  139. }
  140. }
  141. if( this->nodeExists( node, "Rewards" ) ){
  142. const auto& rewardsNode = node["Rewards"];
  143. for( const auto& rewardNode : rewardsNode ){
  144. uint32 day;
  145. if( !this->asUInt32( rewardNode, "Day", day ) ){
  146. continue;
  147. }
  148. day -= 1;
  149. std::shared_ptr<s_attendance_reward> reward = util::map_find( attendance_period->rewards, day );
  150. bool reward_exists = reward != nullptr;
  151. if( !reward_exists ){
  152. if( !this->nodeExists( rewardNode, "ItemId" ) ){
  153. this->invalidWarning( rewardNode, "Node \"ItemId\" is missing.\n" );
  154. return 0;
  155. }
  156. reward = std::make_shared<s_attendance_reward>();
  157. }
  158. if( this->nodeExists( rewardNode, "ItemId" ) ){
  159. t_itemid item_id;
  160. if( !this->asUInt32( rewardNode, "ItemId", item_id ) ){
  161. continue;
  162. }
  163. if( item_id == 0 || !itemdb_exists( item_id ) ){
  164. ShowError( "pc_attendance_load: Unknown item ID %u for day %d.\n", item_id, day + 1 );
  165. continue;
  166. }
  167. reward->item_id = item_id;
  168. }
  169. if( this->nodeExists( rewardNode, "Amount" ) ){
  170. uint16 amount;
  171. if( !this->asUInt16( rewardNode, "Amount", amount ) ){
  172. continue;
  173. }
  174. if( amount == 0 ){
  175. ShowWarning( "pc_attendance_load: Invalid reward count %hu for day %d. Defaulting to 1...\n", amount, day + 1 );
  176. amount = 1;
  177. }else if( amount > MAX_AMOUNT ){
  178. ShowError( "pc_attendance_load: Reward count %hu above maximum %hu for day %d. Defaulting to %hu...\n", amount, MAX_AMOUNT, day + 1, MAX_AMOUNT );
  179. amount = MAX_AMOUNT;
  180. }
  181. reward->amount = amount;
  182. }else{
  183. if( !reward_exists ){
  184. reward->amount = 1;
  185. }
  186. }
  187. if( !reward_exists ){
  188. attendance_period->rewards[day] = reward;
  189. }
  190. }
  191. bool missing_day = false;
  192. for( int day = 0; day < attendance_period->rewards.size(); day++ ){
  193. if( attendance_period->rewards.find( day ) == attendance_period->rewards.end() ){
  194. ShowError( "pc_attendance_load: Reward for day %d is missing.\n", day + 1 );
  195. missing_day = true;
  196. break;
  197. }
  198. }
  199. if( missing_day ){
  200. return 0;
  201. }
  202. }
  203. if( !exists ){
  204. this->put( start, attendance_period );
  205. }
  206. return 1;
  207. }
  208. AttendanceDatabase attendance_db;
  209. const std::string PenaltyDatabase::getDefaultLocation(){
  210. return std::string( db_path ) + "/level_penalty.yml";
  211. }
  212. uint64 PenaltyDatabase::parseBodyNode(const ryml::NodeRef& node){
  213. std::string type_constant;
  214. if( !this->asString( node, "Type", type_constant ) ){
  215. return 0;
  216. }
  217. int64 constant_value;
  218. if( !script_get_constant( ( "PENALTY_" + type_constant ).c_str(), &constant_value ) ){
  219. this->invalidWarning( node["Type"], "Unknown penalty type \"%s\".\n", type_constant.c_str() );
  220. return 0;
  221. }
  222. if( constant_value < PENALTY_NONE || constant_value > PENALTY_MAX ){
  223. this->invalidWarning( node["Type"], "Invalid penalty type \"%s\".\n", type_constant.c_str() );
  224. return 0;
  225. }
  226. e_penalty_type type = static_cast<e_penalty_type>( constant_value );
  227. std::shared_ptr<s_penalty> penalty = this->find( type );
  228. bool exists = penalty != nullptr;
  229. if( !exists ){
  230. penalty = std::make_shared<s_penalty>();
  231. penalty->type = type;
  232. for( int i = 0, max = ARRAYLENGTH( penalty->rate ); i < max; i++ ){
  233. penalty->rate[i] = UINT16_MAX;
  234. }
  235. }
  236. if( this->nodeExists( node, "LevelDifferences" ) ){
  237. for( const auto& levelNode : node["LevelDifferences"] ){
  238. if( !this->nodesExist( levelNode, { "Difference", "Rate" } ) ){
  239. return 0;
  240. }
  241. int32 difference;
  242. if( !this->asInt32( levelNode, "Difference", difference ) ){
  243. return 0;
  244. }
  245. if( std::abs( difference ) > MAX_LEVEL ){
  246. this->invalidWarning( levelNode["Difference"], "Level difference %d is bigger than maximum level %d.\n", difference, MAX_LEVEL );
  247. return 0;
  248. }
  249. uint16 rate;
  250. if( !this->asUInt16Rate( levelNode, "Rate", rate ) ){
  251. return 0;
  252. }
  253. penalty->rate[difference + MAX_LEVEL - 1] = rate;
  254. }
  255. }
  256. if( !exists ){
  257. this->put( type, penalty );
  258. }
  259. return 1;
  260. }
  261. void PenaltyDatabase::loadingFinished(){
  262. for( const auto& pair : *this ){
  263. for( int i = MAX_LEVEL - 1, max = ARRAYLENGTH( pair.second->rate ), last_rate = 100; i < max; i++ ){
  264. uint16 rate = pair.second->rate[i];
  265. // Check if it has been defined
  266. if( rate == UINT16_MAX ){
  267. pair.second->rate[i] = last_rate;
  268. }else{
  269. last_rate = rate;
  270. }
  271. }
  272. for( int i = MAX_LEVEL - 1, last_rate = 100; i >= 0; i-- ){
  273. uint16 rate = pair.second->rate[i];
  274. // Check if it has been defined
  275. if( rate == UINT16_MAX ){
  276. pair.second->rate[i] = last_rate;
  277. }else{
  278. last_rate = rate;
  279. }
  280. }
  281. }
  282. }
  283. PenaltyDatabase penalty_db;
  284. #define MOTD_LINE_SIZE 128
  285. static char motd_text[MOTD_LINE_SIZE][CHAT_SIZE_MAX]; // Message of the day buffer [Valaris]
  286. bool reg_load;
  287. /**
  288. * Translation table from athena equip index to aegis bitmask
  289. */
  290. unsigned int equip_bitmask[EQI_MAX] = {
  291. EQP_ACC_L, // EQI_ACC_L
  292. EQP_ACC_R, // EQI_ACC_R
  293. EQP_SHOES, // EQI_SHOES
  294. EQP_GARMENT, // EQI_GARMENT
  295. EQP_HEAD_LOW, // EQI_HEAD_LOW
  296. EQP_HEAD_MID, // EQI_HEAD_MID
  297. EQP_HEAD_TOP, // EQI_HEAD_TOP
  298. EQP_ARMOR, // EQI_ARMOR
  299. EQP_HAND_L, // EQI_HAND_L
  300. EQP_HAND_R, // EQI_HAND_R
  301. EQP_COSTUME_HEAD_TOP, // EQI_COSTUME_HEAD_TOP
  302. EQP_COSTUME_HEAD_MID, // EQI_COSTUME_HEAD_MID
  303. EQP_COSTUME_HEAD_LOW, // EQI_COSTUME_HEAD_LOW
  304. EQP_COSTUME_GARMENT, // EQI_COSTUME_GARMENT
  305. EQP_AMMO, // EQI_AMMO
  306. EQP_SHADOW_ARMOR, // EQI_SHADOW_ARMOR
  307. EQP_SHADOW_WEAPON, // EQI_SHADOW_WEAPON
  308. EQP_SHADOW_SHIELD, // EQI_SHADOW_SHIELD
  309. EQP_SHADOW_SHOES, // EQI_SHADOW_SHOES
  310. EQP_SHADOW_ACC_R, // EQI_SHADOW_ACC_R
  311. EQP_SHADOW_ACC_L // EQI_SHADOW_ACC_L
  312. };
  313. //Links related info to the sd->hate_mob[]/sd->feel_map[] entries
  314. const struct sg_data sg_info[MAX_PC_FEELHATE] = {
  315. { SG_SUN_ANGER, SG_SUN_BLESS, SG_SUN_COMFORT, "PC_FEEL_SUN", "PC_HATE_MOB_SUN", is_day_of_sun },
  316. { SG_MOON_ANGER, SG_MOON_BLESS, SG_MOON_COMFORT, "PC_FEEL_MOON", "PC_HATE_MOB_MOON", is_day_of_moon },
  317. { SG_STAR_ANGER, SG_STAR_BLESS, SG_STAR_COMFORT, "PC_FEEL_STAR", "PC_HATE_MOB_STAR", is_day_of_star }
  318. };
  319. void pc_set_reg_load( bool val ){
  320. reg_load = val;
  321. }
  322. /**
  323. * Item Cool Down Delay Saving
  324. * Struct item_cd is not a member of struct map_session_data
  325. * to keep cooldowns in memory between player log-ins.
  326. * All cooldowns are reset when server is restarted.
  327. **/
  328. DBMap* itemcd_db = NULL; // char_id -> struct item_cd
  329. struct item_cd {
  330. t_tick tick[MAX_ITEMDELAYS]; //tick
  331. t_itemid nameid[MAX_ITEMDELAYS]; //item id
  332. };
  333. /**
  334. * Converts a class to its array index for CLASS_COUNT defined arrays.
  335. * Note that it does not do a validity check for speed purposes, where parsing
  336. * player input make sure to use a pcdb_checkid first!
  337. * @param class_ Job ID see enum e_job
  338. * @return Class Index
  339. */
  340. int pc_class2idx(int class_) {
  341. if (class_ >= JOB_NOVICE_HIGH)
  342. return class_- JOB_NOVICE_HIGH+JOB_MAX_BASIC;
  343. return class_;
  344. }
  345. /**
  346. * Get player's group ID
  347. * @param sd
  348. * @return Group ID
  349. */
  350. int pc_get_group_id(struct map_session_data *sd) {
  351. return sd->group_id;
  352. }
  353. /** Get player's group Level
  354. * @param sd
  355. * @return Group Level
  356. */
  357. int pc_get_group_level(struct map_session_data *sd) {
  358. return sd->group->level;
  359. }
  360. /**
  361. * Remove a player from queue after timeout
  362. * @param tid: Timer ID
  363. * @param tick: Timer
  364. * @param id: ID
  365. * @return 0 on success or 1 otherwise
  366. */
  367. static TIMER_FUNC(pc_on_expire_active)
  368. {
  369. map_session_data *sd = (map_session_data *)data;
  370. nullpo_retr(1, sd);
  371. sd->tid_queue_active = INVALID_TIMER;
  372. bg_queue_leave(sd);
  373. clif_bg_queue_entry_init(sd);
  374. return 0;
  375. }
  376. /**
  377. * Function used to set timer externally
  378. * @param sd: Player data
  379. */
  380. void pc_set_bg_queue_timer(map_session_data *sd) {
  381. nullpo_retv(sd);
  382. if (sd->tid_queue_active != INVALID_TIMER) {
  383. delete_timer(sd->tid_queue_active, pc_on_expire_active);
  384. sd->tid_queue_active = INVALID_TIMER;
  385. }
  386. sd->tid_queue_active = add_timer(gettick() + 20000, pc_on_expire_active, 0, (intptr_t)sd);
  387. }
  388. /**
  389. * Function used to delete timer externally
  390. * @param sd: Player data
  391. */
  392. void pc_delete_bg_queue_timer(map_session_data *sd) {
  393. nullpo_retv(sd);
  394. if (sd->tid_queue_active != INVALID_TIMER) {
  395. delete_timer(sd->tid_queue_active, pc_on_expire_active);
  396. sd->tid_queue_active = INVALID_TIMER;
  397. }
  398. }
  399. static TIMER_FUNC(pc_invincible_timer){
  400. struct map_session_data *sd;
  401. if( (sd=(struct map_session_data *)map_id2sd(id)) == NULL || sd->bl.type!=BL_PC )
  402. return 1;
  403. if(sd->invincible_timer != tid){
  404. ShowError("invincible_timer %d != %d\n",sd->invincible_timer,tid);
  405. return 0;
  406. }
  407. sd->invincible_timer = INVALID_TIMER;
  408. skill_unit_move(&sd->bl,tick,1);
  409. return 0;
  410. }
  411. void pc_setinvincibletimer(struct map_session_data* sd, int val) {
  412. nullpo_retv(sd);
  413. if( sd->invincible_timer != INVALID_TIMER )
  414. delete_timer(sd->invincible_timer,pc_invincible_timer);
  415. sd->invincible_timer = add_timer(gettick()+val,pc_invincible_timer,sd->bl.id,0);
  416. }
  417. void pc_delinvincibletimer(struct map_session_data* sd)
  418. {
  419. nullpo_retv(sd);
  420. if( sd->invincible_timer != INVALID_TIMER )
  421. {
  422. delete_timer(sd->invincible_timer,pc_invincible_timer);
  423. sd->invincible_timer = INVALID_TIMER;
  424. skill_unit_move(&sd->bl,gettick(),1);
  425. }
  426. }
  427. static TIMER_FUNC(pc_spiritball_timer){
  428. struct map_session_data *sd;
  429. int i;
  430. if( (sd=(struct map_session_data *)map_id2sd(id)) == NULL || sd->bl.type!=BL_PC )
  431. return 1;
  432. if( sd->spiritball <= 0 )
  433. {
  434. 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);
  435. sd->spiritball = 0;
  436. return 0;
  437. }
  438. ARR_FIND(0, sd->spiritball, i, sd->spirit_timer[i] == tid);
  439. if( i == sd->spiritball )
  440. {
  441. ShowError("pc_spiritball_timer: timer not found (aid=%d cid=%d tid=%d)\n", sd->status.account_id, sd->status.char_id, tid);
  442. return 0;
  443. }
  444. sd->spiritball--;
  445. if( i != sd->spiritball )
  446. memmove(sd->spirit_timer+i, sd->spirit_timer+i+1, (sd->spiritball-i)*sizeof(int));
  447. sd->spirit_timer[sd->spiritball] = INVALID_TIMER;
  448. clif_spiritball(&sd->bl);
  449. return 0;
  450. }
  451. /**
  452. * Adds a spiritball to player for 'interval' ms
  453. * @param sd
  454. * @param interval
  455. * @param max
  456. */
  457. void pc_addspiritball(struct map_session_data *sd,int interval,int max)
  458. {
  459. int tid;
  460. uint8 i;
  461. nullpo_retv(sd);
  462. if(max > MAX_SPIRITBALL)
  463. max = MAX_SPIRITBALL;
  464. if(sd->spiritball < 0)
  465. sd->spiritball = 0;
  466. if( sd->spiritball && sd->spiritball >= max )
  467. {
  468. if(sd->spirit_timer[0] != INVALID_TIMER)
  469. delete_timer(sd->spirit_timer[0],pc_spiritball_timer);
  470. sd->spiritball--;
  471. if( sd->spiritball != 0 )
  472. memmove(sd->spirit_timer+0, sd->spirit_timer+1, (sd->spiritball)*sizeof(int));
  473. sd->spirit_timer[sd->spiritball] = INVALID_TIMER;
  474. }
  475. tid = add_timer(gettick()+interval, pc_spiritball_timer, sd->bl.id, 0);
  476. 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);
  477. if( i != sd->spiritball )
  478. memmove(sd->spirit_timer+i+1, sd->spirit_timer+i, (sd->spiritball-i)*sizeof(int));
  479. sd->spirit_timer[i] = tid;
  480. sd->spiritball++;
  481. if( (sd->class_&MAPID_THIRDMASK) == MAPID_ROYAL_GUARD )
  482. clif_millenniumshield(&sd->bl,sd->spiritball);
  483. else
  484. clif_spiritball(&sd->bl);
  485. }
  486. /**
  487. * Removes number of spiritball from player
  488. * @param sd
  489. * @param count
  490. * @param type 1 = doesn't give client effect
  491. */
  492. void pc_delspiritball(struct map_session_data *sd,int count,int type)
  493. {
  494. uint8 i;
  495. nullpo_retv(sd);
  496. if(sd->spiritball <= 0) {
  497. sd->spiritball = 0;
  498. return;
  499. }
  500. if(count == 0)
  501. return;
  502. if(count > sd->spiritball)
  503. count = sd->spiritball;
  504. sd->spiritball -= count;
  505. if(count > MAX_SPIRITBALL)
  506. count = MAX_SPIRITBALL;
  507. for(i=0;i<count;i++) {
  508. if(sd->spirit_timer[i] != INVALID_TIMER) {
  509. delete_timer(sd->spirit_timer[i],pc_spiritball_timer);
  510. sd->spirit_timer[i] = INVALID_TIMER;
  511. }
  512. }
  513. for(i=count;i<MAX_SPIRITBALL;i++) {
  514. sd->spirit_timer[i-count] = sd->spirit_timer[i];
  515. sd->spirit_timer[i] = INVALID_TIMER;
  516. }
  517. if(!type) {
  518. if( (sd->class_&MAPID_THIRDMASK) == MAPID_ROYAL_GUARD )
  519. clif_millenniumshield(&sd->bl,sd->spiritball);
  520. else
  521. clif_spiritball(&sd->bl);
  522. }
  523. }
  524. /**
  525. * Adds a soulball to player
  526. * @param sd: Player data
  527. * @param max: Max amount of soulballs
  528. */
  529. int pc_addsoulball(map_session_data *sd, int max)
  530. {
  531. nullpo_ret(sd);
  532. status_change *sc = status_get_sc(&sd->bl);
  533. if (sc == nullptr || sc->data[SC_SOULENERGY] == nullptr) {
  534. sc_start(&sd->bl, &sd->bl, SC_SOULENERGY, 100, 0, skill_get_time2(SP_SOULCOLLECT, 1));
  535. sd->soulball = 0;
  536. }
  537. max = min(max, MAX_SOUL_BALL);
  538. sd->soulball = cap_value(sd->soulball, 0, MAX_SOUL_BALL);
  539. if (sd->soulball && sd->soulball >= max)
  540. sd->soulball--;
  541. sd->soulball++;
  542. sc_start(&sd->bl, &sd->bl, SC_SOULENERGY, 100, sd->soulball, skill_get_time2(SP_SOULCOLLECT, 1));
  543. clif_soulball(sd);
  544. return 0;
  545. }
  546. /**
  547. * Removes number of soulball from player
  548. * @param sd: Player data
  549. * @param count: Amount to remove
  550. * @param type: true = doesn't give client effect
  551. */
  552. int pc_delsoulball(map_session_data *sd, int count, bool type)
  553. {
  554. nullpo_ret(sd);
  555. if (count <= 0)
  556. return 0;
  557. status_change *sc = status_get_sc(&sd->bl);
  558. if (sd->soulball <= 0 || sc == nullptr || sc->data[SC_SOULENERGY] == nullptr) {
  559. sd->soulball = 0;
  560. }else{
  561. sd->soulball -= cap_value(count, 0, sd->soulball);
  562. if (sd->soulball == 0)
  563. status_change_end(&sd->bl, SC_SOULENERGY, INVALID_TIMER);
  564. else
  565. sc->data[SC_SOULENERGY]->val1 = sd->soulball;
  566. }
  567. if (!type)
  568. clif_soulball(sd);
  569. return 0;
  570. }
  571. /**
  572. * Adds servantballs to a player
  573. * @param sd: Player data
  574. * @param amount: Amount to add
  575. */
  576. void pc_addservantball( struct map_session_data& sd, int count ){
  577. sd.servantball = cap_value( sd.servantball + count, 0, MAX_SERVANTBALL );
  578. clif_servantball( sd );
  579. }
  580. /**
  581. * Removes number of servantballs from player
  582. * @param sd: Player data
  583. * @param count: Amount to remove
  584. */
  585. void pc_delservantball( struct map_session_data& sd, int count ){
  586. sd.servantball = cap_value( sd.servantball - count, 0, MAX_SERVANTBALL );
  587. clif_servantball( sd );
  588. }
  589. /**
  590. * Adds abyssballs to a player
  591. * @param sd: Player data
  592. * @param amount: Amount to add
  593. */
  594. void pc_addabyssball( struct map_session_data& sd, int count ){
  595. sd.abyssball = cap_value( sd.abyssball + count, 0, MAX_ABYSSBALL );
  596. clif_abyssball( sd );
  597. }
  598. /**
  599. * Removes number of abyssballs from player
  600. * @param sd: Player data
  601. * @param count: Amount to remove
  602. */
  603. void pc_delabyssball( struct map_session_data& sd, int count ){
  604. sd.abyssball = cap_value( sd.abyssball - count, 0, MAX_ABYSSBALL );
  605. clif_abyssball( sd );
  606. }
  607. /**
  608. * Increases a player's fame points and displays a notice to him
  609. * @param sd Player
  610. * @param count Fame point
  611. * @return true: on success, false: on error
  612. */
  613. bool pc_addfame(map_session_data &sd, int count)
  614. {
  615. enum e_rank ranktype;
  616. sd.status.fame += count;
  617. if (sd.status.fame > MAX_FAME)
  618. sd.status.fame = MAX_FAME;
  619. switch(sd.class_&MAPID_UPPERMASK){
  620. case MAPID_BLACKSMITH: ranktype = RANK_BLACKSMITH; break;
  621. case MAPID_ALCHEMIST: ranktype = RANK_ALCHEMIST; break;
  622. case MAPID_TAEKWON: ranktype = RANK_TAEKWON; break;
  623. default:
  624. ShowWarning( "pc_addfame: Trying to add fame to class '%s'(%d).\n", job_name(sd.status.class_), sd.status.class_ );
  625. return false;
  626. }
  627. clif_update_rankingpoint(sd, ranktype, count);
  628. chrif_updatefamelist(sd, ranktype);
  629. return true;
  630. }
  631. /**
  632. * Check whether a player ID is in the fame rankers list of its job, returns his/her position if so, 0 else
  633. * @param sd
  634. * @param job Job use enum e_mapid
  635. * @return Rank
  636. */
  637. unsigned char pc_famerank(uint32 char_id, int job)
  638. {
  639. uint8 i;
  640. switch(job){
  641. case MAPID_BLACKSMITH: // Blacksmith
  642. for(i = 0; i < MAX_FAME_LIST; i++){
  643. if(smith_fame_list[i].id == char_id)
  644. return i + 1;
  645. }
  646. break;
  647. case MAPID_ALCHEMIST: // Alchemist
  648. for(i = 0; i < MAX_FAME_LIST; i++){
  649. if(chemist_fame_list[i].id == char_id)
  650. return i + 1;
  651. }
  652. break;
  653. case MAPID_TAEKWON: // Taekwon
  654. for(i = 0; i < MAX_FAME_LIST; i++){
  655. if(taekwon_fame_list[i].id == char_id)
  656. return i + 1;
  657. }
  658. break;
  659. }
  660. return 0;
  661. }
  662. /**
  663. * Restart player's HP & SP value
  664. * @param sd
  665. * @param type Restart type: 1 - Normal Resurection
  666. */
  667. void pc_setrestartvalue(struct map_session_data *sd, char type) {
  668. struct status_data *status, *b_status;
  669. nullpo_retv(sd);
  670. b_status = &sd->base_status;
  671. status = &sd->battle_status;
  672. if (type&1) { //Normal resurrection
  673. status->hp = 1; //Otherwise status_heal may fail if dead.
  674. status_heal(&sd->bl, b_status->hp, 0, 1);
  675. if( status->sp < b_status->sp )
  676. status_set_sp(&sd->bl, b_status->sp, 1);
  677. } else { //Just for saving on the char-server (with values as if respawned)
  678. sd->status.hp = b_status->hp;
  679. sd->status.sp = (status->sp < b_status->sp)?b_status->sp:status->sp;
  680. sd->status.ap = (status->ap < b_status->ap)?b_status->ap:status->ap;
  681. }
  682. }
  683. /*==========================================
  684. Rental System
  685. *------------------------------------------*/
  686. /**
  687. * Ends a rental and removes the item/effect
  688. * @param tid: Tick ID
  689. * @param tick: Timer
  690. * @param id: Timer ID
  691. * @param data: Data
  692. * @return false - failure, true - success
  693. */
  694. TIMER_FUNC(pc_inventory_rental_end){
  695. struct map_session_data *sd = map_id2sd(id);
  696. if( sd == NULL )
  697. return 0;
  698. if( tid != sd->rental_timer ) {
  699. ShowError("pc_inventory_rental_end: invalid timer id.\n");
  700. return 0;
  701. }
  702. pc_inventory_rentals(sd);
  703. return 1;
  704. }
  705. /**
  706. * Removes the rental timer from the player
  707. * @param sd: Player data
  708. */
  709. void pc_inventory_rental_clear(struct map_session_data *sd)
  710. {
  711. if( sd->rental_timer != INVALID_TIMER ) {
  712. delete_timer(sd->rental_timer, pc_inventory_rental_end);
  713. sd->rental_timer = INVALID_TIMER;
  714. }
  715. }
  716. /**
  717. * Check for items in the player's inventory that are rental type
  718. * @param sd: Player data
  719. */
  720. void pc_inventory_rentals(struct map_session_data *sd)
  721. {
  722. int i, c = 0;
  723. unsigned int next_tick = UINT_MAX;
  724. for( i = 0; i < MAX_INVENTORY; i++ ) { // Check for Rentals on Inventory
  725. if( sd->inventory.u.items_inventory[i].nameid == 0 )
  726. continue; // Nothing here
  727. if( sd->inventory.u.items_inventory[i].expire_time == 0 )
  728. continue;
  729. if( sd->inventory.u.items_inventory[i].expire_time <= time(NULL) ) {
  730. if (sd->inventory_data[i]->unequip_script)
  731. run_script(sd->inventory_data[i]->unequip_script, 0, sd->bl.id, fake_nd->bl.id);
  732. clif_rental_expired(sd, i, sd->inventory.u.items_inventory[i].nameid);
  733. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 0, 0, LOG_TYPE_OTHER);
  734. } else {
  735. unsigned int expire_tick = (unsigned int)(sd->inventory.u.items_inventory[i].expire_time - time(NULL));
  736. clif_rental_time(sd, sd->inventory.u.items_inventory[i].nameid, (int)expire_tick);
  737. next_tick = umin(expire_tick * 1000U, next_tick);
  738. c++;
  739. }
  740. }
  741. 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
  742. sd->rental_timer = add_timer(gettick() + umin(next_tick,3600000), pc_inventory_rental_end, sd->bl.id, 0);
  743. else
  744. sd->rental_timer = INVALID_TIMER;
  745. }
  746. /**
  747. * Add a rental item to the player and adjusts the rental timer appropriately
  748. * @param sd: Player data
  749. * @param seconds: Rental time
  750. */
  751. void pc_inventory_rental_add(struct map_session_data *sd, unsigned int seconds)
  752. {
  753. t_tick tick = seconds * 1000;
  754. if( sd == NULL )
  755. return;
  756. if( sd->rental_timer != INVALID_TIMER ) {
  757. const struct TimerData * td;
  758. td = get_timer(sd->rental_timer);
  759. if( DIFF_TICK(td->tick, gettick()) > tick ) { // Update Timer as this one ends first than the current one
  760. pc_inventory_rental_clear(sd);
  761. sd->rental_timer = add_timer(gettick() + tick, pc_inventory_rental_end, sd->bl.id, 0);
  762. }
  763. } else
  764. sd->rental_timer = add_timer(gettick() + i64min(tick,3600000), pc_inventory_rental_end, sd->bl.id, 0);
  765. }
  766. /**
  767. * Check if the player can sell the current item
  768. * @param sd: map_session_data of the player
  769. * @param item: struct of the checking item
  770. * @param shoptype: NPC's sub type see enum npc_subtype
  771. * @return bool 'true' is sellable, 'false' otherwise
  772. */
  773. bool pc_can_sell_item(struct map_session_data *sd, struct item *item, enum npc_subtype shoptype) {
  774. if (sd == NULL || item == NULL)
  775. return false;
  776. if (!pc_can_give_items(sd))
  777. return false;
  778. if (item->equip > 0 || item->amount < 0)
  779. return false;
  780. if (battle_config.hide_fav_sell && item->favorite)
  781. return false; //Cannot sell favs (optional config)
  782. if (!battle_config.rental_transaction && item->expire_time)
  783. return false; // Cannot Sell Rental Items
  784. if( item->equipSwitch ){
  785. return false;
  786. }
  787. if (itemdb_ishatched_egg(item))
  788. return false;
  789. switch (shoptype) {
  790. case NPCTYPE_SHOP:
  791. if (item->bound && battle_config.allow_bound_sell&ISR_BOUND_SELLABLE && (
  792. item->bound != BOUND_GUILD ||
  793. (sd->guild && sd->status.char_id == sd->guild->member[0].char_id) ||
  794. (item->bound == BOUND_GUILD && !(battle_config.allow_bound_sell&ISR_BOUND_GUILDLEADER_ONLY))
  795. ))
  796. return true;
  797. break;
  798. case NPCTYPE_ITEMSHOP:
  799. if (item->bound && battle_config.allow_bound_sell&ISR_BOUND && (
  800. item->bound != BOUND_GUILD ||
  801. (sd->guild && sd->status.char_id == sd->guild->member[0].char_id) ||
  802. (item->bound == BOUND_GUILD && !(battle_config.allow_bound_sell&ISR_BOUND_GUILDLEADER_ONLY))
  803. ))
  804. return true;
  805. else if (!item->bound) {
  806. struct item_data *itd = itemdb_search(item->nameid);
  807. if (itd && itd->flag.trade_restriction.sell && battle_config.allow_bound_sell&ISR_SELLABLE)
  808. return true;
  809. }
  810. break;
  811. }
  812. if (!itemdb_cansell(item, pc_get_group_level(sd)))
  813. return false;
  814. if (item->bound && !pc_can_give_bounded_items(sd))
  815. return false; // Don't allow sale of bound items
  816. return true;
  817. }
  818. /**
  819. * Determines if player can give / drop / trade / vend items
  820. */
  821. bool pc_can_give_items(struct map_session_data *sd)
  822. {
  823. return pc_has_permission(sd, PC_PERM_TRADE);
  824. }
  825. /**
  826. * Determines if player can give / drop / trade / vend bounded items
  827. */
  828. bool pc_can_give_bounded_items(struct map_session_data *sd)
  829. {
  830. return pc_has_permission(sd, PC_PERM_TRADE_BOUNDED);
  831. }
  832. /**
  833. * Determine if an item in a player's inventory is tradeable based on several merits.
  834. * Checks for item_trade, bound, and rental restrictions.
  835. * @param sd: Player data
  836. * @param index: Item inventory index
  837. * @return True if the item can be traded or false otherwise
  838. */
  839. bool pc_can_trade_item(map_session_data *sd, int index) {
  840. if (sd && index >= 0) {
  841. return (sd->inventory.u.items_inventory[index].expire_time == 0 &&
  842. (sd->inventory.u.items_inventory[index].bound == 0 || pc_can_give_bounded_items(sd)) &&
  843. itemdb_cantrade(&sd->inventory.u.items_inventory[index], pc_get_group_level(sd), pc_get_group_level(sd))
  844. );
  845. }
  846. return false;
  847. }
  848. /*==========================================
  849. * Prepares character for saving.
  850. * @param sd
  851. *------------------------------------------*/
  852. void pc_makesavestatus(struct map_session_data *sd) {
  853. nullpo_retv(sd);
  854. if(!battle_config.save_clothcolor)
  855. sd->status.clothes_color = 0;
  856. if(!battle_config.save_body_style)
  857. sd->status.body = 0;
  858. //Only copy the Cart/Peco/Falcon options, the rest are handled via
  859. //status change load/saving. [Skotlex]
  860. #ifdef NEW_CARTS
  861. sd->status.option = sd->sc.option&(OPTION_INVISIBLE|OPTION_FALCON|OPTION_RIDING|OPTION_DRAGON|OPTION_WUG|OPTION_WUGRIDER|OPTION_MADOGEAR);
  862. #else
  863. sd->status.option = sd->sc.option&(OPTION_INVISIBLE|OPTION_CART|OPTION_FALCON|OPTION_RIDING|OPTION_DRAGON|OPTION_WUG|OPTION_WUGRIDER|OPTION_MADOGEAR);
  864. #endif
  865. if (sd->sc.data[SC_JAILED]) { //When Jailed, do not move last point.
  866. if(pc_isdead(sd)){
  867. pc_setrestartvalue(sd, 0);
  868. } else {
  869. sd->status.hp = sd->battle_status.hp;
  870. sd->status.sp = sd->battle_status.sp;
  871. sd->status.ap = sd->battle_status.ap;
  872. }
  873. sd->status.last_point.map = sd->mapindex;
  874. sd->status.last_point.x = sd->bl.x;
  875. sd->status.last_point.y = sd->bl.y;
  876. return;
  877. }
  878. if(pc_isdead(sd)) {
  879. pc_setrestartvalue(sd, 0);
  880. memcpy(&sd->status.last_point,&sd->status.save_point,sizeof(sd->status.last_point));
  881. } else {
  882. sd->status.hp = sd->battle_status.hp;
  883. sd->status.sp = sd->battle_status.sp;
  884. sd->status.ap = sd->battle_status.ap;
  885. sd->status.last_point.map = sd->mapindex;
  886. sd->status.last_point.x = sd->bl.x;
  887. sd->status.last_point.y = sd->bl.y;
  888. }
  889. if(map_getmapflag(sd->bl.m, MF_NOSAVE)) {
  890. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  891. if(mapdata->save.map)
  892. memcpy(&sd->status.last_point,&mapdata->save,sizeof(sd->status.last_point));
  893. else
  894. memcpy(&sd->status.last_point,&sd->status.save_point,sizeof(sd->status.last_point));
  895. }
  896. }
  897. /*==========================================
  898. * Off init ? Connection?
  899. *------------------------------------------*/
  900. void pc_setnewpc(struct map_session_data *sd, uint32 account_id, uint32 char_id, int login_id1, t_tick client_tick, int sex, int fd) {
  901. nullpo_retv(sd);
  902. sd->bl.id = account_id;
  903. sd->status.account_id = account_id;
  904. sd->status.char_id = char_id;
  905. sd->status.sex = sex;
  906. sd->login_id1 = login_id1;
  907. sd->login_id2 = 0; // at this point, we can not know the value :(
  908. sd->client_tick = client_tick;
  909. sd->state.active = 0; //to be set to 1 after player is fully authed and loaded.
  910. sd->bl.type = BL_PC;
  911. if(battle_config.prevent_logout_trigger&PLT_LOGIN)
  912. sd->canlog_tick = gettick();
  913. //Required to prevent homunculus copuing a base speed of 0.
  914. sd->battle_status.speed = sd->base_status.speed = DEFAULT_WALK_SPEED;
  915. }
  916. /**
  917. * Get equip point for an equip
  918. * @param sd
  919. * @param id
  920. */
  921. int pc_equippoint_sub(struct map_session_data *sd,struct item_data* id){
  922. int ep = 0;
  923. nullpo_ret(sd);
  924. nullpo_ret(id);
  925. if (!itemdb_isequip2(id))
  926. return 0; //Not equippable by players.
  927. ep = id->equip;
  928. if(id->subtype == W_DAGGER || id->subtype == W_1HSWORD || id->subtype == W_1HAXE) {
  929. 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]
  930. if (ep == EQP_WEAPON)
  931. return EQP_ARMS;
  932. if (ep == EQP_SHADOW_WEAPON)
  933. return EQP_SHADOW_ARMS;
  934. }
  935. }
  936. return ep;
  937. }
  938. /**
  939. * Get equip point for an equip
  940. * @param sd
  941. * @param n Equip index in inventory
  942. */
  943. int pc_equippoint(struct map_session_data *sd,int n){
  944. nullpo_ret(sd);
  945. return pc_equippoint_sub(sd,sd->inventory_data[n]);
  946. }
  947. /**
  948. * Fill inventory_data with struct *item_data through inventory (fill with struct *item)
  949. * @param sd : player session
  950. * @return 0 sucess, 1:invalid sd
  951. */
  952. void pc_setinventorydata(struct map_session_data *sd)
  953. {
  954. uint8 i;
  955. nullpo_retv(sd);
  956. for(i = 0; i < MAX_INVENTORY; i++) {
  957. t_itemid id = sd->inventory.u.items_inventory[i].nameid;
  958. sd->inventory_data[i] = id?itemdb_search(id):NULL;
  959. }
  960. }
  961. /**
  962. * 'Calculates' weapon type
  963. * @param sd : Player
  964. */
  965. void pc_calcweapontype(struct map_session_data *sd)
  966. {
  967. nullpo_retv(sd);
  968. // single-hand
  969. if(sd->weapontype2 == W_FIST) {
  970. sd->status.weapon = sd->weapontype1;
  971. return;
  972. }
  973. if(sd->weapontype1 == W_FIST) {
  974. sd->status.weapon = sd->weapontype2;
  975. return;
  976. }
  977. // dual-wield
  978. sd->status.weapon = W_FIST;
  979. switch (sd->weapontype1){
  980. case W_DAGGER:
  981. switch (sd->weapontype2) {
  982. case W_DAGGER: sd->status.weapon = W_DOUBLE_DD; break;
  983. case W_1HSWORD: sd->status.weapon = W_DOUBLE_DS; break;
  984. case W_1HAXE: sd->status.weapon = W_DOUBLE_DA; break;
  985. }
  986. break;
  987. case W_1HSWORD:
  988. switch (sd->weapontype2) {
  989. case W_DAGGER: sd->status.weapon = W_DOUBLE_DS; break;
  990. case W_1HSWORD: sd->status.weapon = W_DOUBLE_SS; break;
  991. case W_1HAXE: sd->status.weapon = W_DOUBLE_SA; break;
  992. }
  993. break;
  994. case W_1HAXE:
  995. switch (sd->weapontype2) {
  996. case W_DAGGER: sd->status.weapon = W_DOUBLE_DA; break;
  997. case W_1HSWORD: sd->status.weapon = W_DOUBLE_SA; break;
  998. case W_1HAXE: sd->status.weapon = W_DOUBLE_AA; break;
  999. }
  1000. }
  1001. // unknown, default to right hand type
  1002. if (!sd->status.weapon)
  1003. sd->status.weapon = sd->weapontype1;
  1004. }
  1005. /**
  1006. * Set equip index
  1007. * @param sd : Player
  1008. */
  1009. void pc_setequipindex(struct map_session_data *sd)
  1010. {
  1011. uint16 i;
  1012. nullpo_retv(sd);
  1013. for (i = 0; i < EQI_MAX; i++){
  1014. sd->equip_index[i] = -1;
  1015. sd->equip_switch_index[i] = -1;
  1016. }
  1017. for (i = 0; i < MAX_INVENTORY; i++) {
  1018. if (sd->inventory.u.items_inventory[i].nameid == 0)
  1019. continue;
  1020. if (sd->inventory.u.items_inventory[i].equip) {
  1021. uint8 j;
  1022. for (j = 0; j < EQI_MAX; j++)
  1023. if (sd->inventory.u.items_inventory[i].equip & equip_bitmask[j])
  1024. sd->equip_index[j] = i;
  1025. if (sd->inventory.u.items_inventory[i].equip & EQP_HAND_R) {
  1026. if (sd->inventory_data[i])
  1027. sd->weapontype1 = sd->inventory_data[i]->subtype;
  1028. else
  1029. sd->weapontype1 = W_FIST;
  1030. }
  1031. if( sd->inventory.u.items_inventory[i].equip & EQP_HAND_L ) {
  1032. if( sd->inventory_data[i] && sd->inventory_data[i]->type == IT_WEAPON )
  1033. sd->weapontype2 = sd->inventory_data[i]->subtype;
  1034. else
  1035. sd->weapontype2 = W_FIST;
  1036. }
  1037. }
  1038. if (sd->inventory.u.items_inventory[i].equipSwitch) {
  1039. for (uint8 j = 0; j < EQI_MAX; j++) {
  1040. if (sd->inventory.u.items_inventory[i].equipSwitch & equip_bitmask[j]) {
  1041. sd->equip_switch_index[j] = i;
  1042. }
  1043. }
  1044. }
  1045. }
  1046. pc_calcweapontype(sd);
  1047. }
  1048. //static int pc_isAllowedCardOn(struct map_session_data *sd,int s,int eqindex,int flag)
  1049. //{
  1050. // int i;
  1051. // struct item *item = &sd->inventory.u.items_inventory[eqindex];
  1052. // struct item_data *data;
  1053. //
  1054. // //Crafted/made/hatched items.
  1055. // if (itemdb_isspecial(item->card[0]))
  1056. // return 1;
  1057. //
  1058. // /* scan for enchant armor gems */
  1059. // if( item->card[MAX_SLOTS - 1] && s < MAX_SLOTS - 1 )
  1060. // s = MAX_SLOTS - 1;
  1061. //
  1062. // ARR_FIND( 0, s, i, item->card[i] && (data = itemdb_exists(item->card[i])) != NULL && data->flag.no_equip&flag );
  1063. // return( i < s ) ? 0 : 1;
  1064. //}
  1065. /**
  1066. * Check if an item is equiped by player
  1067. * (Check if the itemid is equiped then search if that match the index in inventory (should be))
  1068. * @param sd : player session
  1069. * @param nameid : itemid
  1070. * @return 1:yes, 0:no
  1071. */
  1072. bool pc_isequipped(struct map_session_data *sd, t_itemid nameid)
  1073. {
  1074. uint8 i;
  1075. for( i = 0; i < EQI_MAX; i++ )
  1076. {
  1077. short index = sd->equip_index[i], j;
  1078. if( index < 0 )
  1079. continue;
  1080. if( pc_is_same_equip_index((enum equip_index)i, sd->equip_index, index) )
  1081. continue;
  1082. if( !sd->inventory_data[index] )
  1083. continue;
  1084. if( sd->inventory_data[index]->nameid == nameid )
  1085. return true;
  1086. for( j = 0; j < MAX_SLOTS; j++ ){
  1087. if( sd->inventory.u.items_inventory[index].card[j] == nameid )
  1088. return true;
  1089. }
  1090. }
  1091. return false;
  1092. }
  1093. /**
  1094. * Check adoption rules
  1095. * @param p1_sd: Player 1
  1096. * @param p2_sd: Player 2
  1097. * @param b_sd: Player that will be adopted
  1098. * @return ADOPT_ALLOWED - Sent message to Baby to accept or deny
  1099. * ADOPT_ALREADY_ADOPTED - Already adopted
  1100. * ADOPT_MARRIED_AND_PARTY - Need to be married and in the same party
  1101. * ADOPT_EQUIP_RINGS - Need wedding rings equipped
  1102. * ADOPT_NOT_NOVICE - Adoptee is not a Novice
  1103. * ADOPT_CHARACTER_NOT_FOUND - Parent or Baby not found
  1104. * ADOPT_MORE_CHILDREN - Cannot adopt more than 1 Baby (client message)
  1105. * ADOPT_LEVEL_70 - Parents need to be level 70+ (client message)
  1106. * ADOPT_MARRIED - Cannot adopt a married person (client message)
  1107. */
  1108. enum adopt_responses pc_try_adopt(struct map_session_data *p1_sd, struct map_session_data *p2_sd, struct map_session_data *b_sd)
  1109. {
  1110. if( !p1_sd || !p2_sd || !b_sd )
  1111. return ADOPT_CHARACTER_NOT_FOUND;
  1112. if( b_sd->status.father || b_sd->status.mother || b_sd->adopt_invite )
  1113. return ADOPT_ALREADY_ADOPTED; // already adopted baby / in adopt request
  1114. if( !p1_sd->status.partner_id || !p1_sd->status.party_id || p1_sd->status.party_id != b_sd->status.party_id )
  1115. return ADOPT_MARRIED_AND_PARTY; // You need to be married and in party with baby to adopt
  1116. if( p1_sd->status.partner_id != p2_sd->status.char_id || p2_sd->status.partner_id != p1_sd->status.char_id )
  1117. return ADOPT_MARRIED_AND_PARTY; // Not married, wrong married
  1118. if( p2_sd->status.party_id != p1_sd->status.party_id )
  1119. return ADOPT_MARRIED_AND_PARTY; // Both parents need to be in the same party
  1120. // Parents need to have their ring equipped
  1121. if( !pc_isequipped(p1_sd, WEDDING_RING_M) && !pc_isequipped(p1_sd, WEDDING_RING_F) )
  1122. return ADOPT_EQUIP_RINGS;
  1123. if( !pc_isequipped(p2_sd, WEDDING_RING_M) && !pc_isequipped(p2_sd, WEDDING_RING_F) )
  1124. return ADOPT_EQUIP_RINGS;
  1125. // Already adopted a baby
  1126. if( p1_sd->status.child || p2_sd->status.child ) {
  1127. clif_Adopt_reply(p1_sd, ADOPT_REPLY_MORE_CHILDREN);
  1128. return ADOPT_MORE_CHILDREN;
  1129. }
  1130. // Parents need at least lvl 70 to adopt
  1131. if( p1_sd->status.base_level < 70 || p2_sd->status.base_level < 70 ) {
  1132. clif_Adopt_reply(p1_sd, ADOPT_REPLY_LEVEL_70);
  1133. return ADOPT_LEVEL_70;
  1134. }
  1135. if( b_sd->status.partner_id ) {
  1136. clif_Adopt_reply(p1_sd, ADOPT_REPLY_MARRIED);
  1137. return ADOPT_MARRIED;
  1138. }
  1139. 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 ) )
  1140. return ADOPT_NOT_NOVICE;
  1141. return ADOPT_ALLOWED;
  1142. }
  1143. /*==========================================
  1144. * Adoption Process
  1145. *------------------------------------------*/
  1146. bool pc_adoption(struct map_session_data *p1_sd, struct map_session_data *p2_sd, struct map_session_data *b_sd)
  1147. {
  1148. int job, joblevel;
  1149. t_exp jobexp;
  1150. if( pc_try_adopt(p1_sd, p2_sd, b_sd) != ADOPT_ALLOWED )
  1151. return false;
  1152. // Preserve current job levels and progress
  1153. joblevel = b_sd->status.job_level;
  1154. jobexp = b_sd->status.job_exp;
  1155. job = pc_mapid2jobid(b_sd->class_|JOBL_BABY, b_sd->status.sex);
  1156. if( job != -1 && pc_jobchange(b_sd, job, 0) )
  1157. { // Success, proceed to configure parents and baby skills
  1158. p1_sd->status.child = b_sd->status.char_id;
  1159. p2_sd->status.child = b_sd->status.char_id;
  1160. b_sd->status.father = p1_sd->status.char_id;
  1161. b_sd->status.mother = p2_sd->status.char_id;
  1162. // Restore progress
  1163. b_sd->status.job_level = joblevel;
  1164. clif_updatestatus(b_sd, SP_JOBLEVEL);
  1165. b_sd->status.job_exp = jobexp;
  1166. clif_updatestatus(b_sd, SP_JOBEXP);
  1167. // Baby Skills
  1168. pc_skill(b_sd, WE_BABY, 1, ADDSKILL_PERMANENT);
  1169. pc_skill(b_sd, WE_CALLPARENT, 1, ADDSKILL_PERMANENT);
  1170. pc_skill(b_sd, WE_CHEERUP, 1, ADDSKILL_PERMANENT);
  1171. // Parents Skills
  1172. pc_skill(p1_sd, WE_CALLBABY, 1, ADDSKILL_PERMANENT);
  1173. pc_skill(p2_sd, WE_CALLBABY, 1, ADDSKILL_PERMANENT);
  1174. chrif_save(p1_sd, CSAVE_NORMAL);
  1175. chrif_save(p2_sd, CSAVE_NORMAL);
  1176. chrif_save(b_sd, CSAVE_NORMAL);
  1177. achievement_update_objective(b_sd, AG_BABY, 1, 1);
  1178. achievement_update_objective(p1_sd, AG_BABY, 1, 2);
  1179. achievement_update_objective(p2_sd, AG_BABY, 1, 2);
  1180. return true;
  1181. }
  1182. return false; // Job Change Fail
  1183. }
  1184. static bool pc_job_can_use_item( struct map_session_data* sd, struct item_data* item ){
  1185. nullpo_retr( false, sd );
  1186. nullpo_retr( false, item );
  1187. // Calculate the required bit to check
  1188. uint64 job = 1ULL << ( sd->class_ & MAPID_BASEMASK );
  1189. size_t index;
  1190. // 2-1
  1191. if( ( sd->class_ & JOBL_2_1 ) != 0 ){
  1192. index = 1;
  1193. // 2-2
  1194. }else if( ( sd->class_ & JOBL_2_2 ) != 0 ){
  1195. index = 2;
  1196. // Basejob
  1197. }else{
  1198. index = 0;
  1199. }
  1200. return ( item->class_base[index] & job ) != 0;
  1201. }
  1202. /*==========================================
  1203. * Check if player can use/equip selected item. Used by pc_isUseitem and pc_isequip
  1204. Returns:
  1205. false : Cannot use/equip
  1206. true : Can use/equip
  1207. * Credits:
  1208. [Inkfish] for first idea
  1209. [Haru] for third-classes extension
  1210. [Cydh] finishing :D
  1211. *------------------------------------------*/
  1212. static bool pc_isItemClass (struct map_session_data *sd, struct item_data* item) {
  1213. while (1) {
  1214. if (item->class_upper&ITEMJ_NORMAL && !(sd->class_&(JOBL_UPPER|JOBL_BABY|JOBL_THIRD|JOBL_FOURTH))) //normal classes (no upper, no baby, no third, no fourth)
  1215. break;
  1216. #ifndef RENEWAL
  1217. //allow third classes to use trans. class items
  1218. if (item->class_upper&ITEMJ_UPPER && sd->class_&(JOBL_UPPER|JOBL_THIRD)) //trans. classes
  1219. break;
  1220. //third-baby classes can use same item too
  1221. if (item->class_upper&ITEMJ_BABY && sd->class_&JOBL_BABY) //baby classes
  1222. break;
  1223. //don't need to decide specific rules for third-classes?
  1224. //items for third classes can be used for all third classes
  1225. if (item->class_upper&(ITEMJ_THIRD|ITEMJ_THIRD_UPPER|ITEMJ_THIRD_BABY) && sd->class_&JOBL_THIRD)
  1226. break;
  1227. #else
  1228. //trans. classes (exl. third-trans.)
  1229. if (item->class_upper&ITEMJ_UPPER && sd->class_&JOBL_UPPER && !(sd->class_&JOBL_THIRD))
  1230. break;
  1231. //baby classes (exl. third-baby)
  1232. if (item->class_upper&ITEMJ_BABY && sd->class_&JOBL_BABY && !(sd->class_&JOBL_THIRD))
  1233. break;
  1234. //third classes (exl. third-trans. and baby-third and fourth)
  1235. if (item->class_upper&ITEMJ_THIRD && sd->class_&JOBL_THIRD && !(sd->class_&(JOBL_UPPER|JOBL_BABY)) && !(sd->class_&JOBL_FOURTH))
  1236. break;
  1237. //trans-third classes (exl. fourth)
  1238. if (item->class_upper&ITEMJ_THIRD_UPPER && sd->class_&JOBL_THIRD && sd->class_&JOBL_UPPER && !(sd->class_&JOBL_FOURTH))
  1239. break;
  1240. //third-baby classes (exl. fourth)
  1241. if (item->class_upper&ITEMJ_THIRD_BABY && sd->class_&JOBL_THIRD && sd->class_&JOBL_BABY && !(sd->class_&JOBL_FOURTH))
  1242. break;
  1243. //fourth classes
  1244. if (item->class_upper&ITEMJ_FOURTH && sd->class_&JOBL_FOURTH)
  1245. break;
  1246. #endif
  1247. return false;
  1248. }
  1249. return true;
  1250. }
  1251. /*=================================================
  1252. * Checks if the player can equip the item at index n in inventory.
  1253. * @param sd
  1254. * @param n Item index in inventory
  1255. * @return ITEM_EQUIP_ACK_OK(0) if can be equipped, or ITEM_EQUIP_ACK_FAIL(1)/ITEM_EQUIP_ACK_FAILLEVEL(2) if can't
  1256. *------------------------------------------------*/
  1257. uint8 pc_isequip(struct map_session_data *sd,int n)
  1258. {
  1259. struct item_data *item;
  1260. nullpo_retr(ITEM_EQUIP_ACK_FAIL, sd);
  1261. item = sd->inventory_data[n];
  1262. if(pc_has_permission(sd, PC_PERM_USE_ALL_EQUIPMENT))
  1263. return ITEM_EQUIP_ACK_OK;
  1264. if(item == NULL)
  1265. return ITEM_EQUIP_ACK_FAIL;
  1266. if(item->elv && sd->status.base_level < (unsigned int)item->elv)
  1267. return ITEM_EQUIP_ACK_FAILLEVEL;
  1268. if(item->elvmax && sd->status.base_level > (unsigned int)item->elvmax)
  1269. return ITEM_EQUIP_ACK_FAILLEVEL;
  1270. if(item->sex != SEX_BOTH && sd->status.sex != item->sex)
  1271. return ITEM_EQUIP_ACK_FAIL;
  1272. //fail to equip if item is restricted
  1273. if (!battle_config.allow_equip_restricted_item && itemdb_isNoEquip(item, sd->bl.m))
  1274. return ITEM_EQUIP_ACK_FAIL;
  1275. if (item->equip&EQP_AMMO) {
  1276. switch (item->subtype) {
  1277. case AMMO_ARROW:
  1278. if (battle_config.ammo_check_weapon && sd->status.weapon != W_BOW && sd->status.weapon != W_MUSICAL && sd->status.weapon != W_WHIP) {
  1279. clif_msg(sd, ITEM_NEED_BOW);
  1280. return ITEM_EQUIP_ACK_FAIL;
  1281. }
  1282. break;
  1283. case AMMO_DAGGER:
  1284. if (!pc_checkskill(sd, AS_VENOMKNIFE))
  1285. return ITEM_EQUIP_ACK_FAIL;
  1286. break;
  1287. case AMMO_BULLET:
  1288. case AMMO_SHELL:
  1289. 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
  1290. #ifdef RENEWAL
  1291. && sd->status.weapon != W_GRENADE
  1292. #endif
  1293. ) {
  1294. clif_msg(sd, ITEM_BULLET_EQUIP_FAIL);
  1295. return ITEM_EQUIP_ACK_FAIL;
  1296. }
  1297. break;
  1298. #ifndef RENEWAL
  1299. case AMMO_GRENADE:
  1300. if (battle_config.ammo_check_weapon && sd->status.weapon != W_GRENADE) {
  1301. clif_msg(sd, ITEM_BULLET_EQUIP_FAIL);
  1302. return ITEM_EQUIP_ACK_FAIL;
  1303. }
  1304. break;
  1305. #endif
  1306. case AMMO_CANNONBALL:
  1307. if (!pc_ismadogear(sd) && (sd->status.class_ == JOB_MECHANIC_T || sd->status.class_ == JOB_MECHANIC)) {
  1308. clif_msg(sd, ITEM_NEED_MADOGEAR); // Item can only be used when Mado Gear is mounted.
  1309. return ITEM_EQUIP_ACK_FAIL;
  1310. }
  1311. if (sd->state.active && !pc_iscarton(sd) && //Check if sc data is already loaded
  1312. (sd->status.class_ == JOB_GENETIC_T || sd->status.class_ == JOB_GENETIC)) {
  1313. clif_msg(sd, ITEM_NEED_CART); // Only available when cart is mounted.
  1314. return ITEM_EQUIP_ACK_FAIL;
  1315. }
  1316. break;
  1317. }
  1318. }
  1319. if (sd->sc.count) {
  1320. if(item->equip & EQP_ARMS && item->type == IT_WEAPON && sd->sc.data[SC_STRIPWEAPON]) // Also works with left-hand weapons [DracoRPG]
  1321. return ITEM_EQUIP_ACK_FAIL;
  1322. if(item->equip & EQP_SHIELD && item->type == IT_ARMOR && sd->sc.data[SC_STRIPSHIELD])
  1323. return ITEM_EQUIP_ACK_FAIL;
  1324. if(item->equip & EQP_ARMOR && sd->sc.data[SC_STRIPARMOR])
  1325. return ITEM_EQUIP_ACK_FAIL;
  1326. if(item->equip & EQP_HEAD_TOP && sd->sc.data[SC_STRIPHELM])
  1327. return ITEM_EQUIP_ACK_FAIL;
  1328. if(item->equip & EQP_ACC && sd->sc.data[SC__STRIPACCESSORY])
  1329. return ITEM_EQUIP_ACK_FAIL;
  1330. if (item->equip & EQP_ARMS && sd->sc.data[SC__WEAKNESS])
  1331. return ITEM_EQUIP_ACK_FAIL;
  1332. if (item->equip & EQP_SHADOW_GEAR && sd->sc.data[SC_SHADOW_STRIP])
  1333. return ITEM_EQUIP_ACK_FAIL;
  1334. if(item->equip && (sd->sc.data[SC_KYOUGAKU] || sd->sc.data[SC_SUHIDE]))
  1335. return ITEM_EQUIP_ACK_FAIL;
  1336. if (sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_SUPERNOVICE) {
  1337. //Spirit of Super Novice equip bonuses. [Skotlex]
  1338. if (sd->status.base_level > 90 && item->equip & EQP_HELM)
  1339. return ITEM_EQUIP_ACK_OK; //Can equip all helms
  1340. if (sd->status.base_level > 96 && item->equip & EQP_ARMS && item->type == IT_WEAPON && item->weapon_level == 4)
  1341. switch(item->subtype) { //In weapons, the look determines type of weapon.
  1342. case W_DAGGER: //All level 4 - Daggers
  1343. case W_1HSWORD: //All level 4 - 1H Swords
  1344. case W_1HAXE: //All level 4 - 1H Axes
  1345. case W_MACE: //All level 4 - 1H Maces
  1346. case W_STAFF: //All level 4 - 1H Staves
  1347. case W_2HSTAFF: //All level 4 - 2H Staves
  1348. return ITEM_EQUIP_ACK_OK;
  1349. }
  1350. }
  1351. }
  1352. //Not equipable by class. [Skotlex]
  1353. if (!pc_job_can_use_item(sd,item))
  1354. return ITEM_EQUIP_ACK_FAIL;
  1355. if (!pc_isItemClass(sd, item))
  1356. return ITEM_EQUIP_ACK_FAIL;
  1357. return ITEM_EQUIP_ACK_OK;
  1358. }
  1359. /*==========================================
  1360. * No problem with the session id
  1361. * set the status that has been sent from char server
  1362. *------------------------------------------*/
  1363. bool pc_authok(struct map_session_data *sd, uint32 login_id2, time_t expiration_time, int group_id, struct mmo_charstatus *st, bool changing_mapservers)
  1364. {
  1365. t_tick tick = gettick();
  1366. uint32 ip = session[sd->fd]->client_addr;
  1367. sd->login_id2 = login_id2;
  1368. sd->group_id = group_id;
  1369. /* load user permissions */
  1370. pc_group_pc_load(sd);
  1371. memcpy(&sd->status, st, sizeof(*st));
  1372. if (st->sex != sd->status.sex) {
  1373. clif_authfail_fd(sd->fd, 0);
  1374. return false;
  1375. }
  1376. //Set the map-server used job id. [Skotlex]
  1377. uint64 class_ = pc_jobid2mapid(sd->status.class_);
  1378. if (class_ == -1 || !job_db.exists(sd->status.class_)) { //Invalid class?
  1379. 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);
  1380. sd->status.class_ = JOB_NOVICE;
  1381. sd->class_ = MAPID_NOVICE;
  1382. } else
  1383. sd->class_ = class_;
  1384. // Checks and fixes to character status data, that are required
  1385. // in case of configuration change or stuff, which cannot be
  1386. // checked on char-server.
  1387. sd->status.hair = cap_value(sd->status.hair,MIN_HAIR_STYLE,MAX_HAIR_STYLE);
  1388. sd->status.hair_color = cap_value(sd->status.hair_color,MIN_HAIR_COLOR,MAX_HAIR_COLOR);
  1389. sd->status.clothes_color = cap_value(sd->status.clothes_color,MIN_CLOTH_COLOR,MAX_CLOTH_COLOR);
  1390. sd->status.body = cap_value(sd->status.body,MIN_BODY_STYLE,MAX_BODY_STYLE);
  1391. //Initializations to null/0 unneeded since map_session_data was filled with 0 upon allocation.
  1392. sd->state.connect_new = 1;
  1393. sd->followtimer = INVALID_TIMER; // [MouseJstr]
  1394. sd->invincible_timer = INVALID_TIMER;
  1395. sd->npc_timer_id = INVALID_TIMER;
  1396. sd->pvp_timer = INVALID_TIMER;
  1397. sd->expiration_tid = INVALID_TIMER;
  1398. sd->autotrade_tid = INVALID_TIMER;
  1399. sd->respawn_tid = INVALID_TIMER;
  1400. sd->tid_queue_active = INVALID_TIMER;
  1401. sd->skill_keep_using.tid = INVALID_TIMER;
  1402. sd->skill_keep_using.skill_id = 0;
  1403. sd->skill_keep_using.level = 0;
  1404. sd->skill_keep_using.target = 0;
  1405. #ifdef SECURE_NPCTIMEOUT
  1406. // Initialize to defaults/expected
  1407. sd->npc_idle_timer = INVALID_TIMER;
  1408. sd->npc_idle_tick = tick;
  1409. sd->npc_idle_type = NPCT_INPUT;
  1410. sd->state.ignoretimeout = false;
  1411. #endif
  1412. sd->canuseitem_tick = tick;
  1413. sd->canusecashfood_tick = tick;
  1414. sd->canequip_tick = tick;
  1415. sd->cantalk_tick = tick;
  1416. sd->canskill_tick = tick;
  1417. sd->cansendmail_tick = tick;
  1418. sd->idletime = last_tick;
  1419. sd->regen.tick.hp = tick;
  1420. sd->regen.tick.sp = tick;
  1421. for(int i = 0; i < MAX_SPIRITBALL; i++)
  1422. sd->spirit_timer[i] = INVALID_TIMER;
  1423. if (battle_config.item_auto_get)
  1424. sd->state.autoloot = 10000;
  1425. if (battle_config.disp_experience)
  1426. sd->state.showexp = 1;
  1427. if (battle_config.disp_zeny)
  1428. sd->state.showzeny = 1;
  1429. #ifdef VIP_ENABLE
  1430. if (!battle_config.vip_disp_rate)
  1431. sd->vip.disableshowrate = 1;
  1432. #endif
  1433. if (!(battle_config.display_skill_fail&2))
  1434. sd->state.showdelay = 1;
  1435. memset(&sd->inventory, 0, sizeof(struct s_storage));
  1436. memset(&sd->cart, 0, sizeof(struct s_storage));
  1437. memset(&sd->storage, 0, sizeof(struct s_storage));
  1438. memset(&sd->premiumStorage, 0, sizeof(struct s_storage));
  1439. memset(&sd->equip_index, -1, sizeof(sd->equip_index));
  1440. memset(&sd->equip_switch_index, -1, sizeof(sd->equip_switch_index));
  1441. if( pc_isinvisible(sd) && !pc_can_use_command( sd, "hide", COMMAND_ATCOMMAND ) ){
  1442. sd->status.option &= ~OPTION_INVISIBLE;
  1443. }
  1444. status_change_init(&sd->bl);
  1445. sd->sc.option = sd->status.option; //This is the actual option used in battle.
  1446. unit_dataset(&sd->bl);
  1447. sd->guild_x = -1;
  1448. sd->guild_y = -1;
  1449. sd->delayed_damage = 0;
  1450. // Event Timers
  1451. for( int i = 0; i < MAX_EVENTTIMER; i++ )
  1452. sd->eventtimer[i] = INVALID_TIMER;
  1453. // Rental Timer
  1454. sd->rental_timer = INVALID_TIMER;
  1455. for( int i = 0; i < 3; i++ )
  1456. sd->hate_mob[i] = -1;
  1457. sd->quest_log = NULL;
  1458. sd->num_quests = 0;
  1459. sd->avail_quests = 0;
  1460. sd->save_quest = false;
  1461. sd->count_rewarp = 0;
  1462. sd->mail.pending_weight = 0;
  1463. sd->mail.pending_zeny = 0;
  1464. sd->mail.pending_slots = 0;
  1465. sd->regs.vars = i64db_alloc(DB_OPT_BASE);
  1466. sd->regs.arrays = NULL;
  1467. sd->vars_dirty = false;
  1468. sd->vars_ok = false;
  1469. sd->vars_received = 0x0;
  1470. //warp player
  1471. enum e_setpos setpos_result = pc_setpos( sd, sd->status.last_point.map, sd->status.last_point.x, sd->status.last_point.y, CLR_OUTSIGHT );
  1472. if( setpos_result != SETPOS_OK ){
  1473. 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, setpos_result );
  1474. // try warping to a default map instead (church graveyard)
  1475. if (pc_setpos(sd, mapindex_name2id(MAP_PRONTERA), 273, 354, CLR_OUTSIGHT) != SETPOS_OK) {
  1476. // if we fail again
  1477. clif_authfail_fd(sd->fd, 0);
  1478. return false;
  1479. }
  1480. }
  1481. clif_inventory_expansion_info( sd );
  1482. clif_authok(sd);
  1483. //Prevent S. Novices from getting the no-death bonus just yet. [Skotlex]
  1484. sd->die_counter=-1;
  1485. //display login notice
  1486. ShowInfo("'" CL_WHITE "%s" CL_RESET "' logged in."
  1487. " (AID/CID: '" CL_WHITE "%d/%d" CL_RESET "',"
  1488. " IP: '" CL_WHITE "%d.%d.%d.%d" CL_RESET "',"
  1489. " Group '" CL_WHITE "%d" CL_RESET "').\n",
  1490. sd->status.name, sd->status.account_id, sd->status.char_id,
  1491. CONVIP(ip), sd->group_id);
  1492. // Send friends list
  1493. clif_friendslist_send(sd);
  1494. if( !changing_mapservers ) {
  1495. if (battle_config.display_version == 1)
  1496. pc_show_version(sd);
  1497. // Message of the Day [Valaris]
  1498. for(int i=0; i < MOTD_LINE_SIZE && motd_text[i][0]; i++) {
  1499. if (battle_config.motd_type)
  1500. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], motd_text[i], false, SELF);
  1501. else
  1502. clif_displaymessage(sd->fd, motd_text[i]);
  1503. }
  1504. if (expiration_time != 0)
  1505. sd->expiration_time = expiration_time;
  1506. /**
  1507. * Fixes login-without-aura glitch (the screen won't blink at this point, don't worry :P)
  1508. **/
  1509. clif_changemap(sd,sd->bl.m,sd->bl.x,sd->bl.y);
  1510. }
  1511. pc_validate_skill(sd);
  1512. /* [Ind] */
  1513. sd->sc_display = NULL;
  1514. sd->sc_display_count = 0;
  1515. // Player has not yet received the CashShop list
  1516. sd->status.cashshop_sent = false;
  1517. sd->last_addeditem_index = -1;
  1518. sd->bonus_script.head = NULL;
  1519. sd->bonus_script.count = 0;
  1520. // Initialize BG queue
  1521. sd->bg_queue_id = 0;
  1522. #if PACKETVER_MAIN_NUM >= 20150507 || PACKETVER_RE_NUM >= 20150429 || defined(PACKETVER_ZERO)
  1523. sd->hatEffects = {};
  1524. #endif
  1525. sd->catch_target_class = PET_CATCH_FAIL;
  1526. // Check EXP overflow, since in previous revision EXP on Max Level can be more than 'official' Max EXP
  1527. if (pc_is_maxbaselv(sd) && sd->status.base_exp > MAX_LEVEL_BASE_EXP) {
  1528. sd->status.base_exp = MAX_LEVEL_BASE_EXP;
  1529. clif_updatestatus(sd, SP_BASEEXP);
  1530. }
  1531. if (pc_is_maxjoblv(sd) && sd->status.job_exp > MAX_LEVEL_JOB_EXP) {
  1532. sd->status.job_exp = MAX_LEVEL_JOB_EXP;
  1533. clif_updatestatus(sd, SP_JOBEXP);
  1534. }
  1535. // Request all registries (auth is considered completed whence they arrive)
  1536. intif_request_registry(sd,7);
  1537. return true;
  1538. }
  1539. /*==========================================
  1540. * Closes a connection because it failed to be authenticated from the char server.
  1541. *------------------------------------------*/
  1542. void pc_authfail(struct map_session_data *sd)
  1543. {
  1544. clif_authfail_fd(sd->fd, 0);
  1545. return;
  1546. }
  1547. /**
  1548. * Player register a bl as hatred
  1549. * @param sd : player session
  1550. * @param pos : hate position [0;2]
  1551. * @param bl : target bl
  1552. * @return false:failed, true:success
  1553. */
  1554. bool pc_set_hate_mob(struct map_session_data *sd, int pos, struct block_list *bl)
  1555. {
  1556. int class_;
  1557. if (!sd || !bl || pos < 0 || pos > 2)
  1558. return false;
  1559. if (sd->hate_mob[pos] != -1)
  1560. { //Can't change hate targets.
  1561. clif_hate_info(sd, pos, sd->hate_mob[pos], 0); //Display current
  1562. return false;
  1563. }
  1564. class_ = status_get_class(bl);
  1565. if (!pcdb_checkid(class_)) {
  1566. unsigned int max_hp = status_get_max_hp(bl);
  1567. if ((pos == 1 && max_hp < 6000) || (pos == 2 && max_hp < 20000))
  1568. return false;
  1569. if (pos != status_get_size(bl))
  1570. return false; //Wrong size
  1571. }
  1572. sd->hate_mob[pos] = class_;
  1573. pc_setglobalreg(sd, add_str(sg_info[pos].hate_var), class_+1);
  1574. clif_hate_info(sd, pos, class_, 1);
  1575. return true;
  1576. }
  1577. /*==========================================
  1578. * Invoked once after the char/account/account2 registry variables are received. [Skotlex]
  1579. * We didn't receive item information at this point so DO NOT attempt to do item operations here.
  1580. * See intif_parse_StorageReceived() for item operations [lighta]
  1581. *------------------------------------------*/
  1582. void pc_reg_received(struct map_session_data *sd)
  1583. {
  1584. uint8 i;
  1585. sd->vars_ok = true;
  1586. sd->change_level_2nd = static_cast<unsigned char>(pc_readglobalreg(sd, add_str(JOBCHANGE2ND_VAR)));
  1587. sd->change_level_3rd = static_cast<unsigned char>(pc_readglobalreg(sd, add_str(JOBCHANGE3RD_VAR)));
  1588. sd->change_level_4th = static_cast<unsigned char>(pc_readglobalreg(sd, add_str(JOBCHANGE4TH_VAR)));
  1589. sd->die_counter = static_cast<int>(pc_readglobalreg(sd, add_str(PCDIECOUNTER_VAR)));
  1590. sd->langtype = static_cast<int>(pc_readaccountreg(sd, add_str(LANGTYPE_VAR)));
  1591. if (msg_checklangtype(sd->langtype,true) < 0)
  1592. sd->langtype = 0; //invalid langtype reset to default
  1593. // Cash shop
  1594. sd->cashPoints = static_cast<int>(pc_readaccountreg(sd, add_str(CASHPOINT_VAR)));
  1595. sd->kafraPoints = static_cast<int>(pc_readaccountreg(sd, add_str(KAFRAPOINT_VAR)));
  1596. // Cooking Exp
  1597. sd->cook_mastery = static_cast<short>(pc_readglobalreg(sd, add_str(COOKMASTERY_VAR)));
  1598. if( (sd->class_&MAPID_BASEMASK) == MAPID_TAEKWON )
  1599. { // Better check for class rather than skill to prevent "skill resets" from unsetting this
  1600. sd->mission_mobid = static_cast<short>(pc_readglobalreg(sd, add_str(TKMISSIONID_VAR)));
  1601. sd->mission_count = static_cast<unsigned char>(pc_readglobalreg(sd, add_str(TKMISSIONCOUNT_VAR)));
  1602. }
  1603. if (battle_config.feature_banking)
  1604. sd->bank_vault = static_cast<int>(pc_readreg2(sd, BANK_VAULT_VAR));
  1605. if (battle_config.feature_roulette) {
  1606. sd->roulette_point.bronze = static_cast<int>(pc_readreg2(sd, ROULETTE_BRONZE_VAR));
  1607. sd->roulette_point.silver = static_cast<int>(pc_readreg2(sd, ROULETTE_SILVER_VAR));
  1608. sd->roulette_point.gold = static_cast<int>(pc_readreg2(sd, ROULETTE_GOLD_VAR));
  1609. }
  1610. sd->roulette.prizeIdx = -1;
  1611. //SG map and mob read [Komurka]
  1612. for(i=0;i<MAX_PC_FEELHATE;i++) { //for now - someone need to make reading from txt/sql
  1613. uint16 j;
  1614. if ((j = static_cast<uint16>(pc_readglobalreg(sd, add_str(sg_info[i].feel_var)))) != 0) {
  1615. sd->feel_map[i].index = j;
  1616. sd->feel_map[i].m = map_mapindex2mapid(j);
  1617. } else {
  1618. sd->feel_map[i].index = 0;
  1619. sd->feel_map[i].m = -1;
  1620. }
  1621. sd->hate_mob[i] = static_cast<short>(pc_readglobalreg(sd, add_str(sg_info[i].hate_var)))-1;
  1622. }
  1623. if ((i = pc_checkskill(sd,RG_PLAGIARISM)) > 0) {
  1624. unsigned short skid = static_cast<unsigned short>(pc_readglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM)));
  1625. sd->cloneskill_idx = skill_get_index(skid);
  1626. if (sd->cloneskill_idx > 0) {
  1627. sd->status.skill[sd->cloneskill_idx].id = skid;
  1628. sd->status.skill[sd->cloneskill_idx].lv = static_cast<uint8>(pc_readglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM_LV)));
  1629. if (sd->status.skill[sd->cloneskill_idx].lv > i)
  1630. sd->status.skill[sd->cloneskill_idx].lv = i;
  1631. sd->status.skill[sd->cloneskill_idx].flag = SKILL_FLAG_PLAGIARIZED;
  1632. }
  1633. }
  1634. if ((i = pc_checkskill(sd,SC_REPRODUCE)) > 0) {
  1635. unsigned short skid = static_cast<unsigned short>(pc_readglobalreg(sd, add_str(SKILL_VAR_REPRODUCE)));
  1636. sd->reproduceskill_idx = skill_get_index(skid);
  1637. if (sd->reproduceskill_idx > 0) {
  1638. sd->status.skill[sd->reproduceskill_idx].id = skid;
  1639. sd->status.skill[sd->reproduceskill_idx].lv = static_cast<uint8>(pc_readglobalreg(sd, add_str(SKILL_VAR_REPRODUCE_LV)));
  1640. if (i < sd->status.skill[sd->reproduceskill_idx].lv)
  1641. sd->status.skill[sd->reproduceskill_idx].lv = i;
  1642. sd->status.skill[sd->reproduceskill_idx].flag = SKILL_FLAG_PLAGIARIZED;
  1643. }
  1644. }
  1645. //Weird... maybe registries were reloaded?
  1646. if (sd->state.active)
  1647. return;
  1648. sd->state.active = 1;
  1649. sd->state.pc_loaded = false; // Ensure inventory data and status data is loaded before we calculate player stats
  1650. intif_storage_request(sd,TABLE_STORAGE, 0, STOR_MODE_ALL); // Request storage data
  1651. intif_storage_request(sd,TABLE_CART, 0, STOR_MODE_ALL); // Request cart data
  1652. intif_storage_request(sd,TABLE_INVENTORY, 0, STOR_MODE_ALL); // Request inventory data
  1653. // Restore IM_CHAR instance to the player
  1654. for (const auto &instance : instances) {
  1655. if (instance.second->mode == IM_CHAR && instance.second->owner_id == sd->status.char_id) {
  1656. sd->instance_id = instance.first;
  1657. break;
  1658. }
  1659. }
  1660. #if PACKETVER_MAIN_NUM < 20190403 || PACKETVER_RE_NUM < 20190320 || PACKETVER_ZERO_NUM < 20190410
  1661. if (sd->instance_id > 0)
  1662. instance_reqinfo(sd, sd->instance_id);
  1663. if (sd->status.party_id > 0)
  1664. party_member_joined(sd);
  1665. if (sd->status.guild_id > 0)
  1666. guild_member_joined(sd);
  1667. if (sd->status.clan_id > 0)
  1668. clan_member_joined(sd);
  1669. #endif
  1670. // pet
  1671. if (sd->status.pet_id > 0)
  1672. intif_request_petdata(sd->status.account_id, sd->status.char_id, sd->status.pet_id);
  1673. // Homunculus [albator]
  1674. if( sd->status.hom_id > 0 )
  1675. intif_homunculus_requestload(sd->status.account_id, sd->status.hom_id);
  1676. if( sd->status.mer_id > 0 )
  1677. intif_mercenary_request(sd->status.mer_id, sd->status.char_id);
  1678. if( sd->status.ele_id > 0 )
  1679. intif_elemental_request(sd->status.ele_id, sd->status.char_id);
  1680. map_addiddb(&sd->bl);
  1681. map_delnickdb(sd->status.char_id, sd->status.name);
  1682. if (!chrif_auth_finished(sd))
  1683. ShowError("pc_reg_received: Failed to properly remove player %d:%d from logging db!\n", sd->status.account_id, sd->status.char_id);
  1684. chrif_skillcooldown_request(sd->status.account_id, sd->status.char_id);
  1685. chrif_bsdata_request(sd->status.char_id);
  1686. #ifdef VIP_ENABLE
  1687. sd->vip.time = 0;
  1688. sd->vip.enabled = 0;
  1689. chrif_req_login_operation(sd->status.account_id, sd->status.name, CHRIF_OP_LOGIN_VIP, 0, 1|8, 0); // request VIP information
  1690. #endif
  1691. intif_Mail_requestinbox(sd->status.char_id, 0, MAIL_INBOX_NORMAL); // MAIL SYSTEM - Request Mail Inbox
  1692. intif_request_questlog(sd);
  1693. if (battle_config.feature_achievement) {
  1694. sd->achievement_data.total_score = 0;
  1695. sd->achievement_data.level = 0;
  1696. sd->achievement_data.save = false;
  1697. sd->achievement_data.count = 0;
  1698. sd->achievement_data.incompleteCount = 0;
  1699. sd->achievement_data.achievements = NULL;
  1700. intif_request_achievements(sd->status.char_id);
  1701. }
  1702. if (sd->state.connect_new == 0 && sd->fd) { //Character already loaded map! Gotta trigger LoadEndAck manually.
  1703. sd->state.connect_new = 1;
  1704. clif_parse_LoadEndAck(sd->fd, sd);
  1705. if( pc_isinvisible( sd ) ){
  1706. sd->vd.class_ = JT_INVISIBLE;
  1707. clif_displaymessage( sd->fd, msg_txt( sd, 11 ) ); // Invisible: On
  1708. // decrement the number of pvp players on the map
  1709. map_getmapdata( sd->bl.m )->users_pvp--;
  1710. if( map_getmapflag( sd->bl.m, MF_PVP ) && !map_getmapflag( sd->bl.m, MF_PVP_NOCALCRANK ) && sd->pvp_timer != INVALID_TIMER ){
  1711. // unregister the player for ranking
  1712. delete_timer( sd->pvp_timer, pc_calc_pvprank_timer );
  1713. sd->pvp_timer = INVALID_TIMER;
  1714. }
  1715. clif_changeoption( &sd->bl );
  1716. }
  1717. }
  1718. channel_autojoin(sd);
  1719. }
  1720. static int pc_calc_skillpoint(struct map_session_data* sd)
  1721. {
  1722. uint16 i, skill_point = 0;
  1723. nullpo_ret(sd);
  1724. for(i = 1; i < MAX_SKILL; i++) {
  1725. if( sd->status.skill[i].id && sd->status.skill[i].lv > 0) {
  1726. std::shared_ptr<s_skill_db> skill = skill_db.find(sd->status.skill[i].id);
  1727. if ((!skill->inf2[INF2_ISQUEST] || battle_config.quest_skill_learn) &&
  1728. (!skill->inf2[INF2_ISWEDDING] || skill->inf2[INF2_ISSPIRIT]) //Do not count wedding/link skills. [Skotlex]
  1729. )
  1730. {
  1731. if(sd->status.skill[i].flag == SKILL_FLAG_PERMANENT)
  1732. skill_point += sd->status.skill[i].lv;
  1733. else if(sd->status.skill[i].flag >= SKILL_FLAG_REPLACED_LV_0)
  1734. skill_point += (sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0);
  1735. }
  1736. }
  1737. }
  1738. return skill_point;
  1739. }
  1740. static bool pc_grant_allskills(struct map_session_data *sd, bool addlv) {
  1741. if (!sd || !pc_has_permission(sd, PC_PERM_ALL_SKILL))
  1742. return false;
  1743. /**
  1744. * Dummy skills must NOT be added here otherwise they'll be displayed in the,
  1745. * skill tree and since they have no icons they'll give resource errors
  1746. * Get ALL skills except npc/guild ones. [Skotlex]
  1747. * Don't add SG_DEVIL [Komurka] and MO_TRIPLEATTACK and RG_SNATCHER [ultramage]
  1748. **/
  1749. for (const auto &skill : skill_db) {
  1750. uint16 skill_id = skill.second->nameid;
  1751. if (skill_id == 0 || skill.second->inf2[INF2_ISNPC]|| skill.second->inf2[INF2_ISGUILD])
  1752. continue;
  1753. switch (skill_id) {
  1754. case SM_SELFPROVOKE:
  1755. case AB_DUPLELIGHT_MELEE:
  1756. case AB_DUPLELIGHT_MAGIC:
  1757. case WL_CHAINLIGHTNING_ATK:
  1758. case WL_TETRAVORTEX_FIRE:
  1759. case WL_TETRAVORTEX_WATER:
  1760. case WL_TETRAVORTEX_WIND:
  1761. case WL_TETRAVORTEX_GROUND:
  1762. case WL_SUMMON_ATK_FIRE:
  1763. case WL_SUMMON_ATK_WIND:
  1764. case WL_SUMMON_ATK_WATER:
  1765. case WL_SUMMON_ATK_GROUND:
  1766. case LG_OVERBRAND_BRANDISH:
  1767. case LG_OVERBRAND_PLUSATK:
  1768. case WM_SEVERE_RAINSTORM_MELEE:
  1769. case RL_R_TRIP_PLUSATK:
  1770. case SG_DEVIL:
  1771. case MO_TRIPLEATTACK:
  1772. case RG_SNATCHER:
  1773. continue;
  1774. default:
  1775. {
  1776. uint8 lv = (uint8)skill_get_max(skill_id);
  1777. if (lv > 0) {
  1778. uint16 idx = skill_get_index(skill_id);
  1779. sd->status.skill[idx].id = skill_id;
  1780. if (addlv)
  1781. sd->status.skill[idx].lv = lv;
  1782. }
  1783. }
  1784. break;
  1785. }
  1786. }
  1787. return true;
  1788. }
  1789. /*==========================================
  1790. * Calculation of skill level.
  1791. * @param sd
  1792. *------------------------------------------*/
  1793. void pc_calc_skilltree(struct map_session_data *sd)
  1794. {
  1795. nullpo_retv(sd);
  1796. uint64 job = pc_calc_skilltree_normalize_job(sd);
  1797. int class_ = pc_mapid2jobid(job, sd->status.sex);
  1798. if( class_ == -1 )
  1799. { //Unable to normalize job??
  1800. ShowError( "pc_calc_skilltree: Unable to normalize job %s(%d) for character %s (%d:%d)\n", job_name( sd->status.class_ ), sd->status.class_, sd->status.name, sd->status.account_id, sd->status.char_id );
  1801. return;
  1802. }
  1803. uint16 job_id = class_;
  1804. class_ = pc_class2idx(class_);
  1805. for (const auto &skill : skill_db) {
  1806. uint16 skill_id = skill.second->nameid;
  1807. uint16 idx = skill_get_index(skill_id);
  1808. if( sd->status.skill[idx].flag != SKILL_FLAG_PLAGIARIZED && sd->status.skill[idx].flag != SKILL_FLAG_PERM_GRANTED ) //Don't touch these
  1809. sd->status.skill[idx].id = 0; //First clear skills.
  1810. /* permanent skills that must be re-checked */
  1811. if( sd->status.skill[idx].flag == SKILL_FLAG_PERM_GRANTED ) {
  1812. if (skill_id == 0) {
  1813. sd->status.skill[idx].id = 0;
  1814. sd->status.skill[idx].lv = 0;
  1815. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  1816. continue;
  1817. }
  1818. switch (skill_id) {
  1819. case NV_TRICKDEAD:
  1820. if( (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE ) {
  1821. sd->status.skill[idx].id = 0;
  1822. sd->status.skill[idx].lv = 0;
  1823. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  1824. }
  1825. break;
  1826. }
  1827. }
  1828. }
  1829. for (const auto &skill : skill_db) {
  1830. uint16 idx = skill_get_index(skill.second->nameid);
  1831. // Restore original level of skills after deleting earned skills.
  1832. if( sd->status.skill[idx].flag != SKILL_FLAG_PERMANENT && sd->status.skill[idx].flag != SKILL_FLAG_PERM_GRANTED && sd->status.skill[idx].flag != SKILL_FLAG_PLAGIARIZED ) {
  1833. sd->status.skill[idx].lv = (sd->status.skill[idx].flag == SKILL_FLAG_TEMPORARY) ? 0 : sd->status.skill[idx].flag - SKILL_FLAG_REPLACED_LV_0;
  1834. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  1835. }
  1836. }
  1837. // Removes Taekwon Ranker skill bonus
  1838. if ((sd->class_&MAPID_UPPERMASK) != MAPID_TAEKWON) {
  1839. std::shared_ptr<s_skill_tree> tree = skill_tree_db.find(JOB_TAEKWON);
  1840. if (tree != nullptr && !tree->skills.empty()) {
  1841. for (const auto &it : tree->skills) {
  1842. uint16 sk_idx = skill_get_index(it.first);
  1843. if (sk_idx == 0)
  1844. continue;
  1845. if (sd->status.skill[sk_idx].flag != SKILL_FLAG_PLAGIARIZED && sd->status.skill[sk_idx].flag != SKILL_FLAG_PERM_GRANTED) {
  1846. if (it.first == NV_BASIC || it.first == NV_FIRSTAID || it.first == WE_CALLBABY)
  1847. continue;
  1848. sd->status.skill[sk_idx].id = 0;
  1849. }
  1850. }
  1851. }
  1852. }
  1853. // Grant all skills
  1854. pc_grant_allskills(sd, false);
  1855. int flag;
  1856. std::shared_ptr<s_skill_tree> tree = skill_tree_db.find(job_id);
  1857. do {
  1858. flag = 0;
  1859. if (tree == nullptr || tree->skills.empty())
  1860. break;
  1861. for (const auto &skillsit : tree->skills) {
  1862. bool fail = false;
  1863. uint16 skid = skillsit.first;
  1864. uint16 sk_idx = skill_get_index(skid);
  1865. if (sd->status.skill[sk_idx].id)
  1866. continue; //Skill already known.
  1867. if (!battle_config.skillfree) {
  1868. // Checking required skills
  1869. std::shared_ptr<s_skill_tree_entry> entry = skillsit.second;
  1870. if (entry != nullptr && !entry->need.empty()) {
  1871. for (const auto &it : entry->need) {
  1872. uint16 sk_need_id = it.first;
  1873. uint16 sk_need_idx = skill_get_index(sk_need_id);
  1874. if (sk_need_idx > 0) {
  1875. uint16 sk_need = sk_need_id;
  1876. 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)
  1877. sk_need = 0; //Not learned.
  1878. else if (sd->status.skill[sk_need_idx].flag >= SKILL_FLAG_REPLACED_LV_0) //Real learned level
  1879. sk_need = sd->status.skill[sk_need_idx].flag - SKILL_FLAG_REPLACED_LV_0;
  1880. else
  1881. sk_need = pc_checkskill(sd,sk_need_id);
  1882. if (sk_need < it.second) {
  1883. fail = true;
  1884. break;
  1885. }
  1886. }
  1887. }
  1888. }
  1889. if (sd->status.base_level < entry->baselv) { //We need to get the actual class in this case
  1890. int c_ = pc_mapid2jobid(sd->class_, sd->status.sex);
  1891. c_ = pc_class2idx(c_);
  1892. if (class_ == c_ || (class_ != c_ && sd->status.base_level < entry->baselv))
  1893. fail = true; // base level requirement wasn't satisfied
  1894. }
  1895. if (sd->status.job_level < entry->joblv) { //We need to get the actual class in this case
  1896. int c_ = pc_mapid2jobid(sd->class_, sd->status.sex);
  1897. c_ = pc_class2idx(c_);
  1898. if (class_ == c_ || (class_ != c_ && sd->status.job_level < entry->joblv))
  1899. fail = true; // job level requirement wasn't satisfied
  1900. }
  1901. }
  1902. if (!fail) {
  1903. std::shared_ptr<s_skill_db> skill = skill_db.find(skid);
  1904. if (!sd->status.skill[sk_idx].lv && (
  1905. (skill->inf2[INF2_ISQUEST] && !battle_config.quest_skill_learn) ||
  1906. skill->inf2[INF2_ISWEDDING] ||
  1907. (skill->inf2[INF2_ISSPIRIT] && !sd->sc.data[SC_SPIRIT])
  1908. ))
  1909. continue; //Cannot be learned via normal means. Note this check DOES allows raising already known skills.
  1910. sd->status.skill[sk_idx].id = skid;
  1911. if(skill->inf2[INF2_ISSPIRIT]) { //Spirit skills cannot be learned, they will only show up on your tree when you get buffed.
  1912. sd->status.skill[sk_idx].lv = 1; // need to manually specify a skill level
  1913. sd->status.skill[sk_idx].flag = SKILL_FLAG_TEMPORARY; //So it is not saved, and tagged as a "bonus" skill.
  1914. }
  1915. flag = 1; // skill list has changed, perform another pass
  1916. }
  1917. }
  1918. } while(flag);
  1919. if( class_ > 0 && sd->status.skill_point == 0 && pc_is_taekwon_ranker(sd) ) {
  1920. uint16 skid = 0;
  1921. /* Taekwon Ranker Bonus Skill Tree
  1922. ============================================
  1923. - Grant All Taekwon Tree, but only as Bonus Skills in case they drop from ranking.
  1924. - (c > 0) to avoid grant Novice Skill Tree in case of Skill Reset (need more logic)
  1925. - (sd->status.skill_point == 0) to wait until all skill points are assigned to avoid problems with Job Change quest. */
  1926. std::shared_ptr<s_skill_tree> tree = skill_tree_db.find(job_id);
  1927. if (tree != nullptr && !tree->skills.empty()) {
  1928. for (const auto &it : tree->skills) {
  1929. skid = it.first;
  1930. uint16 sk_idx = skill_get_index(skid);
  1931. if (sk_idx == 0)
  1932. continue;
  1933. if( skill_get_inf2_(skid, { INF2_ISQUEST, INF2_ISWEDDING }) )
  1934. continue; //Do not include Quest/Wedding skills.
  1935. if( sd->status.skill[sk_idx].id == 0 ) {
  1936. sd->status.skill[sk_idx].id = skid;
  1937. sd->status.skill[sk_idx].flag = SKILL_FLAG_TEMPORARY; // So it is not saved, and tagged as a "bonus" skill.
  1938. } else if( skid != NV_BASIC )
  1939. sd->status.skill[sk_idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[sk_idx].lv; // Remember original level
  1940. sd->status.skill[sk_idx].lv = skill_tree_get_max(skid, sd->status.class_);
  1941. }
  1942. }
  1943. }
  1944. // Enable Bard/Dancer spirit linked skills.
  1945. if (sd->sc.count && sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_BARDDANCER) {
  1946. std::vector<std::vector<uint16>> linked_skills = { { BA_WHISTLE, DC_HUMMING },
  1947. { BA_ASSASSINCROSS, DC_DONTFORGETME },
  1948. { BA_POEMBRAGI, DC_FORTUNEKISS },
  1949. { BA_APPLEIDUN, DC_SERVICEFORYOU } };
  1950. for (const auto &skill : linked_skills) {
  1951. if (pc_checkskill(sd, skill[!sd->status.sex]) < 10)
  1952. continue;
  1953. // Tag it as a non-savable, non-uppable, bonus skill
  1954. pc_skill(sd, skill[sd->status.sex], 10, ADDSKILL_TEMP);
  1955. }
  1956. }
  1957. }
  1958. //Checks if you can learn a new skill after having leveled up a skill.
  1959. static void pc_check_skilltree(struct map_session_data *sd)
  1960. {
  1961. int flag = 0;
  1962. if (battle_config.skillfree)
  1963. return; //Function serves no purpose if this is set
  1964. int c = pc_mapid2jobid( pc_calc_skilltree_normalize_job( sd ), sd->status.sex );
  1965. if (c == -1) { //Unable to normalize job??
  1966. ShowError("pc_check_skilltree: Unable to normalize job %d for character %s (%d:%d)\n", sd->status.class_, sd->status.name, sd->status.account_id, sd->status.char_id);
  1967. return;
  1968. }
  1969. std::shared_ptr<s_skill_tree> tree = skill_tree_db.find(c);
  1970. if (tree == nullptr || tree->skills.empty())
  1971. return;
  1972. do {
  1973. flag = 0;
  1974. for (const auto &skillsit : tree->skills) {
  1975. uint16 skid = skillsit.first;
  1976. uint16 sk_idx = skill_get_index(skid);
  1977. bool fail = false;
  1978. if (sd->status.skill[sk_idx].id) //Already learned
  1979. continue;
  1980. // Checking required skills
  1981. std::shared_ptr<s_skill_tree_entry> entry = skillsit.second;
  1982. if (entry != nullptr && !entry->need.empty()) {
  1983. for (const auto &it : entry->need) {
  1984. uint16 sk_need_id = it.first;
  1985. uint16 sk_need_idx = skill_get_index(sk_need_id);
  1986. if (sk_need_id > 0) {
  1987. short sk_need = sk_need_id;
  1988. 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)
  1989. sk_need = 0; //Not learned.
  1990. else if (sd->status.skill[sk_need_idx].flag >= SKILL_FLAG_REPLACED_LV_0) //Real lerned level
  1991. sk_need = sd->status.skill[sk_need_idx].flag - SKILL_FLAG_REPLACED_LV_0;
  1992. else
  1993. sk_need = pc_checkskill(sd,sk_need_id);
  1994. if (sk_need < it.second) {
  1995. fail = true;
  1996. break;
  1997. }
  1998. }
  1999. }
  2000. }
  2001. if( fail )
  2002. continue;
  2003. if (sd->status.base_level < entry->baselv || sd->status.job_level < entry->joblv)
  2004. continue;
  2005. std::shared_ptr<s_skill_db> skill = skill_db.find(skid);
  2006. if( !sd->status.skill[sk_idx].lv && (
  2007. (skill->inf2[INF2_ISQUEST] && !battle_config.quest_skill_learn) ||
  2008. skill->inf2[INF2_ISWEDDING] ||
  2009. (skill->inf2[INF2_ISSPIRIT] && !sd->sc.data[SC_SPIRIT])
  2010. ) )
  2011. continue; //Cannot be learned via normal means.
  2012. sd->status.skill[sk_idx].id = skid;
  2013. flag = 1;
  2014. }
  2015. } while(flag);
  2016. }
  2017. // Make sure all the skills are in the correct condition
  2018. // before persisting to the backend.. [MouseJstr]
  2019. void pc_clean_skilltree(struct map_session_data *sd)
  2020. {
  2021. uint16 i;
  2022. for (i = 0; i < MAX_SKILL; i++){
  2023. if (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY || sd->status.skill[i].flag == SKILL_FLAG_PLAGIARIZED) {
  2024. sd->status.skill[i].id = 0;
  2025. sd->status.skill[i].lv = 0;
  2026. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  2027. }
  2028. else if (sd->status.skill[i].flag >= SKILL_FLAG_REPLACED_LV_0){
  2029. sd->status.skill[i].lv = sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
  2030. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  2031. }
  2032. }
  2033. }
  2034. uint64 pc_calc_skilltree_normalize_job_sub( struct map_session_data *sd ){
  2035. int skill_point = pc_calc_skillpoint( sd );
  2036. if( sd->class_ & MAPID_SUMMONER ){
  2037. // Novice's skill points for basic skill.
  2038. std::shared_ptr<s_job_info> summoner_job = job_db.find( JOB_SUMMONER );
  2039. int summoner_skills = summoner_job->max_job_level - 1;
  2040. if( skill_point < summoner_skills ){
  2041. return MAPID_SUMMONER;
  2042. }
  2043. skill_point -= summoner_skills;
  2044. }else{
  2045. // Novice's skill points for basic skill.
  2046. std::shared_ptr<s_job_info> novice_job = job_db.find( JOB_NOVICE );
  2047. int novice_skills = novice_job->max_job_level - 1;
  2048. if( skill_point < novice_skills ){
  2049. return MAPID_NOVICE;
  2050. }
  2051. skill_point -= novice_skills;
  2052. }
  2053. // 1st Class Job LV Check
  2054. if( sd->class_ & JOBL_2 && ( sd->class_ & MAPID_UPPERMASK ) != MAPID_SUPER_NOVICE ){
  2055. uint64 mapid_1st = sd->class_ & MAPID_BASEMASK;
  2056. int class_1st = pc_mapid2jobid( mapid_1st, sd->status.sex );
  2057. if( !sd->change_level_2nd ){
  2058. if( class_1st >= JOB_NOVICE ){
  2059. sd->change_level_2nd = job_db.find( class_1st )->max_job_level;
  2060. }else{
  2061. sd->change_level_2nd = 0;
  2062. }
  2063. pc_setglobalreg( sd, add_str( JOBCHANGE2ND_VAR ), sd->change_level_2nd );
  2064. }
  2065. if( class_1st > 0 && skill_point < ( sd->change_level_2nd - 1 ) ){
  2066. return mapid_1st;
  2067. }
  2068. skill_point -= ( sd->change_level_2nd - 1 );
  2069. }
  2070. // 2nd Class Job LV Check
  2071. if( sd->class_ & JOBL_THIRD && (sd->class_ & MAPID_THIRDMASK) != MAPID_SUPER_NOVICE_E ){
  2072. uint64 mapid_2nd = sd->class_ & MAPID_UPPERMASK;
  2073. int class_2nd = pc_mapid2jobid( mapid_2nd, sd->status.sex );
  2074. if( !sd->change_level_3rd ){
  2075. if( class_2nd >= JOB_NOVICE ){
  2076. sd->change_level_3rd = job_db.find( class_2nd )->max_job_level;
  2077. }else{
  2078. sd->change_level_3rd = 0;
  2079. }
  2080. pc_setglobalreg( sd, add_str( JOBCHANGE3RD_VAR ), sd->change_level_3rd );
  2081. }
  2082. if( class_2nd > 0 && skill_point < ( sd->change_level_3rd - 1 ) ){
  2083. return mapid_2nd;
  2084. }
  2085. skill_point -= ( sd->change_level_3rd - 1 );
  2086. }
  2087. // 3rd Class Job LV Check
  2088. if( sd->class_ & JOBL_FOURTH ){
  2089. uint64 mapid_3rd = sd->class_ & MAPID_THIRDMASK | JOBL_THIRD;
  2090. int class_3rd = pc_mapid2jobid( mapid_3rd, sd->status.sex );
  2091. if( !sd->change_level_4th ){
  2092. if( class_3rd >= JOB_NOVICE ){
  2093. sd->change_level_4th = job_db.find( class_3rd )->max_job_level;
  2094. }else{
  2095. sd->change_level_4th = 0;
  2096. }
  2097. pc_setglobalreg( sd, add_str( JOBCHANGE4TH_VAR ), sd->change_level_4th );
  2098. }
  2099. if( class_3rd > 0 && skill_point < ( sd->change_level_4th - 1 ) ){
  2100. return mapid_3rd;
  2101. }
  2102. skill_point -= ( sd->change_level_4th - 1 );
  2103. }
  2104. return sd->class_;
  2105. }
  2106. uint64 pc_calc_skilltree_normalize_job( struct map_session_data *sd ){
  2107. if( !battle_config.skillup_limit || pc_has_permission( sd, PC_PERM_ALL_SKILL ) ){
  2108. return sd->class_;
  2109. }
  2110. uint64 c = pc_calc_skilltree_normalize_job_sub( sd );
  2111. // Special Masks
  2112. if( sd->class_&JOBL_UPPER ){
  2113. if( pc_mapid2jobid( c | JOBL_UPPER, sd->status.sex ) >= JOB_NOVICE ){
  2114. c |= JOBL_UPPER;// Rebirth Job
  2115. }
  2116. }
  2117. if( sd->class_&JOBL_BABY ){
  2118. if( pc_mapid2jobid( c | JOBL_BABY, sd->status.sex ) >= JOB_NOVICE ){
  2119. c |= JOBL_BABY;// Baby Job
  2120. }
  2121. }
  2122. return c;
  2123. }
  2124. /*==========================================
  2125. * Updates the weight status
  2126. *------------------------------------------
  2127. * 1: overweight 50% for pre-renewal and 70% for renewal
  2128. * 2: overweight 90%
  2129. * It's assumed that SC_WEIGHT50 and SC_WEIGHT90 are only started/stopped here.
  2130. */
  2131. void pc_updateweightstatus(struct map_session_data *sd)
  2132. {
  2133. int old_overweight;
  2134. int new_overweight;
  2135. nullpo_retv(sd);
  2136. old_overweight = (sd->sc.data[SC_WEIGHT90]) ? 2 : (sd->sc.data[SC_WEIGHT50]) ? 1 : 0;
  2137. #ifdef RENEWAL
  2138. new_overweight = (pc_is90overweight(sd)) ? 2 : (pc_is70overweight(sd)) ? 1 : 0;
  2139. #else
  2140. new_overweight = (pc_is90overweight(sd)) ? 2 : (pc_is50overweight(sd)) ? 1 : 0;
  2141. #endif
  2142. if( old_overweight == new_overweight )
  2143. return; // no change
  2144. // stop old status change
  2145. if( old_overweight == 1 )
  2146. status_change_end(&sd->bl, SC_WEIGHT50, INVALID_TIMER);
  2147. else if( old_overweight == 2 )
  2148. status_change_end(&sd->bl, SC_WEIGHT90, INVALID_TIMER);
  2149. // start new status change
  2150. if( new_overweight == 1 )
  2151. sc_start(&sd->bl,&sd->bl, SC_WEIGHT50, 100, 0, 0);
  2152. else if( new_overweight == 2 )
  2153. sc_start(&sd->bl,&sd->bl, SC_WEIGHT90, 100, 0, 0);
  2154. // update overweight status
  2155. sd->regen.state.overweight = new_overweight;
  2156. }
  2157. int pc_disguise(struct map_session_data *sd, int class_)
  2158. {
  2159. if (!class_ && !sd->disguise)
  2160. return 0;
  2161. if (class_ && sd->disguise == class_)
  2162. return 0;
  2163. if(pc_isinvisible(sd))
  2164. { //Character is invisible. Stealth class-change. [Skotlex]
  2165. sd->disguise = class_; //viewdata is set on uncloaking.
  2166. return 2;
  2167. }
  2168. if (sd->bl.prev != NULL) {
  2169. pc_stop_walking(sd, 0);
  2170. clif_clearunit_area(&sd->bl, CLR_OUTSIGHT);
  2171. }
  2172. if (!class_) {
  2173. sd->disguise = 0;
  2174. class_ = sd->status.class_;
  2175. } else
  2176. sd->disguise=class_;
  2177. status_set_viewdata(&sd->bl, class_);
  2178. clif_changeoption(&sd->bl);
  2179. if (sd->bl.prev != NULL) {
  2180. clif_spawn(&sd->bl);
  2181. if (class_ == sd->status.class_ && pc_iscarton(sd))
  2182. { //It seems the cart info is lost on undisguise.
  2183. clif_cartlist(sd);
  2184. clif_updatestatus(sd,SP_CARTINFO);
  2185. }
  2186. if (sd->chatID) {
  2187. struct chat_data* cd;
  2188. if ((cd = (struct chat_data*)map_id2bl(sd->chatID)) != NULL)
  2189. clif_dispchat(cd,0);
  2190. }
  2191. }
  2192. return 1;
  2193. }
  2194. /// Show error message
  2195. #define PC_BONUS_SHOW_ERROR(type,type2,val) { ShowError("%s: %s: Invalid %s %d.\n",__FUNCTION__,#type,#type2,(val)); break; }
  2196. /// Check for valid Element, break & show error message if invalid Element
  2197. #define PC_BONUS_CHK_ELEMENT(ele,bonus) { if (!CHK_ELEMENT((ele))) { PC_BONUS_SHOW_ERROR((bonus),Element,(ele)); }}
  2198. /// Check for valid Race, break & show error message if invalid Race
  2199. #define PC_BONUS_CHK_RACE(rc,bonus) { if (!CHK_RACE((rc))) { PC_BONUS_SHOW_ERROR((bonus),Race,(rc)); }}
  2200. /// Check for valid Race2, break & show error message if invalid Race2
  2201. #define PC_BONUS_CHK_RACE2(rc2,bonus) { if (!CHK_RACE2((rc2))) { PC_BONUS_SHOW_ERROR((bonus),Race2,(rc2)); }}
  2202. /// Check for valid Class, break & show error message if invalid Class
  2203. #define PC_BONUS_CHK_CLASS(cl,bonus) { if (!CHK_CLASS((cl))) { PC_BONUS_SHOW_ERROR((bonus),Class,(cl)); }}
  2204. /// Check for valid Size, break & show error message if invalid Size
  2205. #define PC_BONUS_CHK_SIZE(sz,bonus) { if (!CHK_MOBSIZE((sz))) { PC_BONUS_SHOW_ERROR((bonus),Size,(sz)); }}
  2206. /// Check for valid SC, break & show error message if invalid SC
  2207. #define PC_BONUS_CHK_SC(sc,bonus) { if ((sc) <= SC_NONE || (sc) >= SC_MAX) { PC_BONUS_SHOW_ERROR((bonus),Effect,(sc)); }}
  2208. /**
  2209. * Add auto spell bonus for player while attacking/attacked
  2210. * @param spell: Spell array
  2211. * @param id: Skill to cast
  2212. * @param lv: Skill level
  2213. * @param rate: Success chance
  2214. * @param battle_flag: Battle flag
  2215. * @param card_id: Used to prevent card stacking
  2216. * @param flag: Flags used for extra arguments
  2217. * &0: forces the skill to be casted on self, rather than on the target
  2218. * &1: forces the skill to be casted on target, rather than self
  2219. * &2: random skill level in [1..lv] is chosen
  2220. */
  2221. static void pc_bonus_autospell(std::vector<s_autospell> &spell, uint16 id, uint16 lv, short rate, short battle_flag, t_itemid card_id, uint8 flag)
  2222. {
  2223. if (spell.size() == MAX_PC_BONUS) {
  2224. ShowWarning("pc_bonus_autospell: Reached max (%d) number of autospells per character!\n", MAX_PC_BONUS);
  2225. return;
  2226. }
  2227. if (!rate)
  2228. return;
  2229. if (!(battle_flag&BF_RANGEMASK))
  2230. battle_flag |= BF_SHORT | BF_LONG; //No range defined? Use both.
  2231. if (!(battle_flag&BF_WEAPONMASK))
  2232. battle_flag |= BF_WEAPON; //No attack type defined? Use weapon.
  2233. if (!(battle_flag&BF_SKILLMASK)) {
  2234. if (battle_flag&(BF_MAGIC | BF_MISC))
  2235. battle_flag |= BF_SKILL; //These two would never trigger without BF_SKILL
  2236. if (battle_flag&BF_WEAPON)
  2237. battle_flag |= BF_NORMAL; //By default autospells should only trigger on normal weapon attacks.
  2238. }
  2239. for (auto &it : spell) {
  2240. if ((it.card_id == card_id || it.rate < 0 || rate < 0) && it.id == id && it.lv == lv && it.battle_flag == battle_flag && it.flag == flag) {
  2241. if (!battle_config.autospell_stacking && it.rate > 0 && rate > 0) // Stacking disabled
  2242. return;
  2243. it.rate = cap_value(it.rate + rate, -10000, 10000);
  2244. return;
  2245. }
  2246. }
  2247. struct s_autospell entry = {};
  2248. if (rate < -10000 || rate > 10000)
  2249. ShowWarning("pc_bonus_autospell: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2250. entry.id = id;
  2251. entry.lv = lv;
  2252. entry.rate = cap_value(rate, -10000, 10000);
  2253. entry.battle_flag = battle_flag;
  2254. entry.card_id = card_id;
  2255. entry.flag = flag;
  2256. spell.push_back(entry);
  2257. }
  2258. /**
  2259. * Add auto spell bonus for player while using skills
  2260. * @param spell: Spell array
  2261. * @param src_skill: Trigger skill
  2262. * @param id: Support or target type
  2263. * @param lv: Skill level
  2264. * @param rate: Success chance
  2265. * @param card_id: Used to prevent card stacking
  2266. * @param flag: Flags used for extra arguments
  2267. * &1: forces the skill to be casted on self, rather than on the target of skill
  2268. * &2: random skill level in [1..lv] is chosen
  2269. */
  2270. static void pc_bonus_autospell_onskill(std::vector<s_autospell> &spell, uint16 src_skill, uint16 id, uint16 lv, short rate, t_itemid card_id, uint8 flag)
  2271. {
  2272. if (spell.size() == MAX_PC_BONUS) {
  2273. ShowWarning("pc_bonus_autospell_onskill: Reached max (%d) number of autospells per character!\n", MAX_PC_BONUS);
  2274. return;
  2275. }
  2276. if (!rate)
  2277. return;
  2278. struct s_autospell entry = {};
  2279. if (rate < -10000 || rate > 10000)
  2280. ShowWarning("pc_bonus_onskill: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2281. entry.trigger_skill = src_skill;
  2282. entry.id = id;
  2283. entry.lv = lv;
  2284. entry.rate = cap_value(rate, -10000, 10000);
  2285. entry.card_id = card_id;
  2286. entry.flag = flag;
  2287. spell.push_back(entry);
  2288. }
  2289. /**
  2290. * Add inflict effect bonus for player while attacking/attacked
  2291. * @param effect: Effect array
  2292. * @param sc: SC/Effect type
  2293. * @param rate: Success chance
  2294. * @param arrow_rate: success chance if bonus comes from arrow-type item
  2295. * @param flag: Target flag
  2296. * @param duration: Duration. If 0 use default duration lookup for associated skill with level 7
  2297. */
  2298. static void pc_bonus_addeff(std::vector<s_addeffect> &effect, enum sc_type sc, short rate, short arrow_rate, unsigned char flag, unsigned int duration)
  2299. {
  2300. if (effect.size() == MAX_PC_BONUS) {
  2301. ShowWarning("pc_bonus_addeff: Reached max (%d) number of add effects per character!\n", MAX_PC_BONUS);
  2302. return;
  2303. }
  2304. if (!(flag&(ATF_SHORT | ATF_LONG)))
  2305. flag |= ATF_SHORT | ATF_LONG; //Default range: both
  2306. if (!(flag&(ATF_TARGET | ATF_SELF)))
  2307. flag |= ATF_TARGET; //Default target: enemy.
  2308. if (!(flag&(ATF_WEAPON | ATF_MAGIC | ATF_MISC)))
  2309. flag |= ATF_WEAPON; //Default type: weapon.
  2310. if (!duration)
  2311. duration = (unsigned int)skill_get_time2(status_db.getSkill(sc), 7);
  2312. for (auto &it : effect) {
  2313. if (it.sc == sc && it.flag == flag) {
  2314. it.rate = cap_value(it.rate + rate, -10000, 10000);
  2315. it.arrow_rate += arrow_rate;
  2316. it.duration = umax(it.duration, duration);
  2317. return;
  2318. }
  2319. }
  2320. struct s_addeffect entry = {};
  2321. if (rate < -10000 || rate > 10000)
  2322. ShowWarning("pc_bonus_addeff: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2323. entry.sc = sc;
  2324. entry.rate = cap_value(rate, -10000, 10000);
  2325. entry.arrow_rate = arrow_rate;
  2326. entry.flag = flag;
  2327. entry.duration = duration;
  2328. effect.push_back(entry);
  2329. }
  2330. /**
  2331. * Add inflict effect bonus for player while attacking using skill
  2332. * @param effect: Effect array
  2333. * @param sc: SC/Effect type
  2334. * @param rate: Success chance
  2335. * @param skill_id: Skill to cast
  2336. * @param target: Target type
  2337. * @param duration: Duration. If 0 use default duration lookup for associated skill with level 7
  2338. */
  2339. static void pc_bonus_addeff_onskill(std::vector<s_addeffectonskill> &effect, enum sc_type sc, short rate, short skill_id, unsigned char target, unsigned int duration)
  2340. {
  2341. if (effect.size() == MAX_PC_BONUS) {
  2342. ShowWarning("pc_bonus_addeff_onskill: Reached max (%d) number of add effects per character!\n", MAX_PC_BONUS);
  2343. return;
  2344. }
  2345. if (!duration)
  2346. duration = (unsigned int)skill_get_time2(status_db.getSkill(sc), 7);
  2347. for (auto &it : effect) {
  2348. if (it.sc == sc && it.skill_id == skill_id && it.target == target) {
  2349. it.rate = cap_value(it.rate + rate, -10000, 10000);
  2350. it.duration = umax(it.duration, duration);
  2351. return;
  2352. }
  2353. }
  2354. struct s_addeffectonskill entry = {};
  2355. if (rate < -10000 || rate > 10000)
  2356. ShowWarning("pc_bonus_addeff_onskill: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2357. entry.sc = sc;
  2358. entry.rate = cap_value(rate, -10000, 10000);
  2359. entry.skill_id = skill_id;
  2360. entry.target = target;
  2361. entry.duration = duration;
  2362. effect.push_back(entry);
  2363. }
  2364. /**
  2365. * Adjust/add drop rate modifier for player
  2366. * @param drop: Player's sd->add_drop (struct s_add_drop)
  2367. * @param nameid: item id that will be dropped
  2368. * @param group: group id
  2369. * @param class_: target class
  2370. * @param race: target race. if < 0, means monster_id
  2371. * @param rate: rate value: 1 ~ 10000. If < 0, it will be multiplied with mob level/10
  2372. */
  2373. static void pc_bonus_item_drop(std::vector<s_add_drop> &drop, t_itemid nameid, uint16 group, int class_, short race, int rate)
  2374. {
  2375. if (!nameid && !group) {
  2376. ShowWarning("pc_bonus_item_drop: No Item ID nor Item Group ID specified.\n");
  2377. return;
  2378. }
  2379. if (nameid && !itemdb_exists(nameid)) {
  2380. ShowWarning("pc_bonus_item_drop: Invalid item id %u\n",nameid);
  2381. return;
  2382. }
  2383. if (group && !itemdb_group.exists(group)) {
  2384. ShowWarning("pc_bonus_item_drop: Invalid Item Group %hu\n",group);
  2385. return;
  2386. }
  2387. if (drop.size() == MAX_PC_BONUS) {
  2388. ShowWarning("pc_bonus_item_drop: Reached max (%d) number of added drops per character! (nameid: %u group: %d class_: %d race: %d rate: %d)\n", MAX_PC_BONUS, nameid, group, class_, race, rate);
  2389. return;
  2390. }
  2391. //Apply config rate adjustment settings.
  2392. if (rate >= 0) { //Absolute drop.
  2393. if (battle_config.item_rate_adddrop != 100)
  2394. rate = rate*battle_config.item_rate_adddrop/100;
  2395. if (rate < battle_config.item_drop_adddrop_min)
  2396. rate = battle_config.item_drop_adddrop_min;
  2397. else if (rate > battle_config.item_drop_adddrop_max)
  2398. rate = battle_config.item_drop_adddrop_max;
  2399. } else { //Relative drop, max/min limits are applied at drop time.
  2400. if (battle_config.item_rate_adddrop != 100)
  2401. rate = rate*battle_config.item_rate_adddrop/100;
  2402. if (rate > -1)
  2403. rate = -1;
  2404. }
  2405. for (auto &it : drop) {
  2406. if (it.nameid == nameid && it.group == group && it.race == race && it.class_ == class_) {
  2407. if ((rate < 0 && it.rate < 0) || (rate > 0 && it.rate > 0)) //Adjust the rate if it has same classification
  2408. it.rate = cap_value(it.rate + rate, -10000, 10000);
  2409. return;
  2410. }
  2411. }
  2412. struct s_add_drop entry = {};
  2413. if (rate < -10000 || rate > 10000)
  2414. ShowWarning("pc_bonus_item_drop: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2415. entry.nameid = nameid;
  2416. entry.group = group;
  2417. entry.race = race;
  2418. entry.class_ = class_;
  2419. entry.rate = cap_value(rate, -10000, 10000);
  2420. drop.push_back(entry);
  2421. }
  2422. s_autobonus::~s_autobonus(){
  2423. if( this->active != INVALID_TIMER ){
  2424. delete_timer( this->active, pc_endautobonus );
  2425. this->active = INVALID_TIMER;
  2426. }
  2427. if( this->bonus_script != nullptr ){
  2428. aFree( this->bonus_script );
  2429. this->bonus_script = nullptr;
  2430. }
  2431. if( this->other_script != nullptr ){
  2432. aFree( this->other_script );
  2433. this->other_script = nullptr;
  2434. }
  2435. }
  2436. /**
  2437. * Add autobonus to player when attacking/attacked
  2438. * @param bonus: Bonus array
  2439. * @param script: Script to execute
  2440. * @param rate: Success chance
  2441. * @param dur: Duration
  2442. * @param flag: Battle flag/skill
  2443. * @param other_script: Secondary script to execute
  2444. * @param pos: Item equip position
  2445. * @param onskill: Skill used to trigger autobonus
  2446. * @return True on success or false otherwise
  2447. */
  2448. bool pc_addautobonus(std::vector<std::shared_ptr<s_autobonus>> &bonus, const char *script, short rate, unsigned int dur, uint16 flag, const char *other_script, unsigned int pos, bool onskill){
  2449. // Check if the same bonus already exists
  2450. for( std::shared_ptr<s_autobonus> autobonus : bonus ){
  2451. // Compare based on position and bonus script
  2452. if( autobonus->pos == pos && strcmp( script, autobonus->bonus_script ) == 0 ){
  2453. return false;
  2454. }
  2455. }
  2456. if (bonus.size() == MAX_PC_BONUS) {
  2457. ShowWarning("pc_addautobonus: Reached max (%d) number of autobonus per character!\n", MAX_PC_BONUS);
  2458. return false;
  2459. }
  2460. if (!onskill) {
  2461. if (!(flag&BF_RANGEMASK))
  2462. flag |= BF_SHORT | BF_LONG; //No range defined? Use both.
  2463. if (!(flag&BF_WEAPONMASK))
  2464. flag |= BF_WEAPON; //No attack type defined? Use weapon.
  2465. if (!(flag&BF_SKILLMASK)) {
  2466. if (flag&(BF_MAGIC | BF_MISC))
  2467. flag |= BF_SKILL; //These two would never trigger without BF_SKILL
  2468. if (flag&BF_WEAPON)
  2469. flag |= BF_NORMAL | BF_SKILL;
  2470. }
  2471. }
  2472. std::shared_ptr<s_autobonus> entry = std::make_shared<s_autobonus>();
  2473. if (rate < -10000 || rate > 10000)
  2474. ShowWarning("pc_addautobonus: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2475. entry->rate = cap_value(rate, -10000, 10000);
  2476. entry->duration = dur;
  2477. entry->active = INVALID_TIMER;
  2478. entry->atk_type = flag;
  2479. entry->pos = pos;
  2480. entry->bonus_script = aStrdup(script);
  2481. entry->other_script = (other_script ? aStrdup(other_script) : NULL);
  2482. bonus.push_back(entry);
  2483. return true;
  2484. }
  2485. /**
  2486. * Remove an autobonus from player
  2487. * @param sd: Player data
  2488. * @param bonus: Autobonus array
  2489. * @param restore: Run script on clearing or not
  2490. */
  2491. void pc_delautobonus(struct map_session_data &sd, std::vector<std::shared_ptr<s_autobonus>> &bonus, bool restore){
  2492. std::vector<std::shared_ptr<s_autobonus>>::iterator it = bonus.begin();
  2493. while( it != bonus.end() ){
  2494. std::shared_ptr<s_autobonus> b = *it;
  2495. if( b->active != INVALID_TIMER && restore && b->bonus_script != nullptr ){
  2496. unsigned int equip_pos_idx = 0;
  2497. // Create a list of all equipped positions to see if all items needed for the autobonus are still present [Playtester]
  2498. for (uint8 j = 0; j < EQI_MAX; j++) {
  2499. if (sd.equip_index[j] >= 0)
  2500. equip_pos_idx |= sd.inventory.u.items_inventory[sd.equip_index[j]].equip;
  2501. }
  2502. if( ( equip_pos_idx&b->pos ) == b->pos ){
  2503. script_run_autobonus(b->bonus_script, &sd, b->pos);
  2504. }else{
  2505. // Not all required items equipped anymore
  2506. restore = false;
  2507. }
  2508. }
  2509. if( restore ){
  2510. it++;
  2511. continue;
  2512. }
  2513. it = bonus.erase(it);
  2514. }
  2515. }
  2516. /**
  2517. * Execute autobonus on player
  2518. * @param sd: Player data
  2519. * @param autobonus: Autobonus to run
  2520. */
  2521. void pc_exeautobonus(struct map_session_data &sd, std::vector<std::shared_ptr<s_autobonus>> *bonus, std::shared_ptr<s_autobonus> autobonus)
  2522. {
  2523. if (autobonus->active != INVALID_TIMER)
  2524. delete_timer(autobonus->active, pc_endautobonus);
  2525. if( autobonus->other_script )
  2526. {
  2527. int j;
  2528. unsigned int equip_pos_idx = 0;
  2529. //Create a list of all equipped positions to see if all items needed for the autobonus are still present [Playtester]
  2530. for(j = 0; j < EQI_MAX; j++) {
  2531. if(sd.equip_index[j] >= 0)
  2532. equip_pos_idx |= sd.inventory.u.items_inventory[sd.equip_index[j]].equip;
  2533. }
  2534. if((equip_pos_idx&autobonus->pos) == autobonus->pos)
  2535. script_run_autobonus(autobonus->other_script,&sd,autobonus->pos);
  2536. }
  2537. autobonus->active = add_timer(gettick()+autobonus->duration, pc_endautobonus, sd.bl.id, (intptr_t)bonus);
  2538. status_calc_pc(&sd,SCO_FORCE);
  2539. }
  2540. /**
  2541. * Remove autobonus timer from player
  2542. */
  2543. TIMER_FUNC(pc_endautobonus){
  2544. struct map_session_data *sd = map_id2sd(id);
  2545. std::vector<std::shared_ptr<s_autobonus>> *bonus = (std::vector<std::shared_ptr<s_autobonus>> *)data;
  2546. nullpo_ret(sd);
  2547. nullpo_ret(bonus);
  2548. for( std::shared_ptr<s_autobonus> autobonus : *bonus ){
  2549. if( autobonus->active == tid ){
  2550. autobonus->active = INVALID_TIMER;
  2551. break;
  2552. }
  2553. }
  2554. status_calc_pc(sd,SCO_FORCE);
  2555. return 0;
  2556. }
  2557. /**
  2558. * Add element bonus to player when attacking
  2559. * @param sd: Player data
  2560. * @param ele: Element to adjust
  2561. * @param rate: Success chance
  2562. * @param flag: Battle flag
  2563. */
  2564. static void pc_bonus_addele(struct map_session_data* sd, unsigned char ele, short rate, short flag)
  2565. {
  2566. nullpo_retv(sd);
  2567. struct weapon_data *wd = (sd->state.lr_flag ? &sd->left_weapon : &sd->right_weapon);
  2568. if (wd->addele2.size() == MAX_PC_BONUS) {
  2569. ShowWarning("pc_bonus_addele: Reached max (%d) number of add element damage bonuses per character!\n", MAX_PC_BONUS);
  2570. return;
  2571. }
  2572. if (!(flag&BF_RANGEMASK))
  2573. flag |= BF_SHORT | BF_LONG;
  2574. if (!(flag&BF_WEAPONMASK))
  2575. flag |= BF_WEAPON;
  2576. if (!(flag&BF_SKILLMASK)) {
  2577. if (flag&(BF_MAGIC | BF_MISC))
  2578. flag |= BF_SKILL;
  2579. if (flag&BF_WEAPON)
  2580. flag |= BF_NORMAL | BF_SKILL;
  2581. }
  2582. for (auto &it : wd->addele2) {
  2583. if (it.ele == ele && it.flag == flag) {
  2584. it.rate = cap_value(it.rate + rate, -10000, 10000);
  2585. return;
  2586. }
  2587. }
  2588. struct s_addele2 entry = {};
  2589. if (rate < -10000 || rate > 10000)
  2590. ShowWarning("pc_bonus_addele: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2591. entry.ele = ele;
  2592. entry.rate = cap_value(rate, -10000, 10000);
  2593. entry.flag = flag;
  2594. wd->addele2.push_back(entry);
  2595. }
  2596. /**
  2597. * Reduce element bonus to player when attacking
  2598. * @param sd: Player data
  2599. * @param ele: Element to adjust
  2600. * @param rate: Success chance
  2601. * @param flag: Battle flag
  2602. */
  2603. static void pc_bonus_subele(struct map_session_data* sd, unsigned char ele, short rate, short flag)
  2604. {
  2605. nullpo_retv(sd);
  2606. if (sd->subele2.size() == MAX_PC_BONUS) {
  2607. ShowWarning("pc_bonus_subele: Reached max (%d) number of resist element damage bonuses per character!\n", MAX_PC_BONUS);
  2608. return;
  2609. }
  2610. if (!(flag&BF_RANGEMASK))
  2611. flag |= BF_SHORT | BF_LONG;
  2612. if (!(flag&BF_WEAPONMASK))
  2613. flag |= BF_WEAPON;
  2614. if (!(flag&BF_SKILLMASK)) {
  2615. if (flag&(BF_MAGIC | BF_MISC))
  2616. flag |= BF_SKILL;
  2617. if (flag&BF_WEAPON)
  2618. flag |= BF_NORMAL | BF_SKILL;
  2619. }
  2620. for (auto &it : sd->subele2) {
  2621. if (it.ele == ele && it.flag == flag) {
  2622. it.rate = cap_value(it.rate + rate, -10000, 10000);
  2623. return;
  2624. }
  2625. }
  2626. struct s_addele2 entry = {};
  2627. if (rate < -10000 || rate > 10000)
  2628. ShowWarning("pc_bonus_subele: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2629. entry.ele = ele;
  2630. entry.rate = cap_value(rate, -10000, 10000);
  2631. entry.flag = flag;
  2632. sd->subele2.push_back(entry);
  2633. }
  2634. /**
  2635. * Adjust race damage to target when attacking
  2636. * @param sd: Player data
  2637. * @param race: Race to adjust
  2638. * @param rate: Success chance
  2639. * @param flag: Battle flag
  2640. */
  2641. static void pc_bonus_subrace(struct map_session_data* sd, unsigned char race, short rate, short flag)
  2642. {
  2643. if (sd->subrace3.size() == MAX_PC_BONUS) {
  2644. ShowWarning("pc_bonus_subrace: Reached max (%d) number of resist race damage bonuses per character!\n", MAX_PC_BONUS);
  2645. return;
  2646. }
  2647. if (!(flag&BF_RANGEMASK))
  2648. flag |= BF_SHORT | BF_LONG;
  2649. if (!(flag&BF_WEAPONMASK))
  2650. flag |= BF_WEAPON;
  2651. if (!(flag&BF_SKILLMASK)) {
  2652. if (flag&(BF_MAGIC | BF_MISC))
  2653. flag |= BF_SKILL;
  2654. if (flag&BF_WEAPON)
  2655. flag |= BF_NORMAL | BF_SKILL;
  2656. }
  2657. for (auto &it : sd->subrace3) {
  2658. if (it.race == race && it.flag == flag) {
  2659. it.rate = cap_value(it.rate + rate, -10000, 10000);
  2660. return;
  2661. }
  2662. }
  2663. struct s_addrace2 entry = {};
  2664. if (rate < -10000 || rate > 10000)
  2665. ShowWarning("pc_bonus_subrace: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2666. entry.race = race;
  2667. entry.rate = cap_value(rate, -10000, 10000);
  2668. entry.flag = flag;
  2669. sd->subrace3.push_back(entry);
  2670. }
  2671. /**
  2672. * General item bonus for player
  2673. * @param bonus: Bonus array
  2674. * @param id: Key
  2675. * @param val: Value
  2676. * @param cap_rate: If Value is a rate value that needs to be capped
  2677. */
  2678. static void pc_bonus_itembonus(std::vector<s_item_bonus> &bonus, uint16 id, int val, bool cap_rate)
  2679. {
  2680. for (auto &it : bonus) {
  2681. if (it.id == id) {
  2682. if (cap_rate)
  2683. it.val = cap_value(it.val + val, -10000, 10000);
  2684. else
  2685. it.val += val;
  2686. return;
  2687. }
  2688. }
  2689. struct s_item_bonus entry = {};
  2690. if (cap_rate && (val < -10000 || val > 10000)) {
  2691. ShowWarning("pc_bonus_itembonus: Item bonus val %d exceeds -10000~10000 range, capping.\n", val);
  2692. val = cap_value(val, -10000, 10000);
  2693. }
  2694. entry.id = id;
  2695. entry.val = val;
  2696. bonus.push_back(entry);
  2697. }
  2698. /**
  2699. * Remove HP/SP to player when attacking
  2700. * @param bonus: Bonus array
  2701. * @param rate: Success chance
  2702. * @param per: Percentage of HP/SP to vanish
  2703. * @param flag: Battle flag
  2704. */
  2705. static void pc_bonus_addvanish(std::vector<s_vanish_bonus> &bonus, int16 rate, int16 per, int flag) {
  2706. if (bonus.size() == MAX_PC_BONUS) {
  2707. ShowWarning("pc_bonus_addvanish: Reached max (%d) number of vanish damage bonuses per character!\n", MAX_PC_BONUS);
  2708. return;
  2709. }
  2710. if (!(flag&BF_RANGEMASK))
  2711. flag |= BF_SHORT | BF_LONG;
  2712. if (!(flag&BF_WEAPONMASK))
  2713. flag |= BF_WEAPON;
  2714. if (!(flag&BF_SKILLMASK)) {
  2715. if (flag&(BF_MAGIC | BF_MISC))
  2716. flag |= BF_SKILL;
  2717. if (flag&BF_WEAPON)
  2718. flag |= BF_NORMAL | BF_SKILL;
  2719. }
  2720. for (auto &it : bonus) {
  2721. if (it.flag == flag) {
  2722. it.rate = cap_value(it.rate + rate, -10000, 10000);
  2723. it.per += per;
  2724. return;
  2725. }
  2726. }
  2727. struct s_vanish_bonus entry = {};
  2728. if (rate < -10000 || rate > 10000)
  2729. ShowWarning("pc_bonus_addvanish: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2730. entry.rate = cap_value(rate, -10000, 10000);
  2731. entry.per = per;
  2732. entry.flag = flag;
  2733. bonus.push_back(entry);
  2734. }
  2735. /*==========================================
  2736. * Add a bonus(type) to player sd
  2737. * format: bonus bBonusName,val;
  2738. * @param sd
  2739. * @param type Bonus type used by bBonusName
  2740. * @param val Value that usually for rate or fixed value
  2741. *------------------------------------------*/
  2742. void pc_bonus(struct map_session_data *sd,int type,int val)
  2743. {
  2744. struct status_data *status;
  2745. int bonus;
  2746. nullpo_retv(sd);
  2747. status = &sd->base_status;
  2748. switch(type){
  2749. case SP_STR:
  2750. case SP_AGI:
  2751. case SP_VIT:
  2752. case SP_INT:
  2753. case SP_DEX:
  2754. case SP_LUK:
  2755. if(sd->state.lr_flag != 2)
  2756. sd->indexed_bonus.param_bonus[type-SP_STR]+=val;
  2757. break;
  2758. case SP_POW:
  2759. case SP_STA:
  2760. case SP_WIS:
  2761. case SP_SPL:
  2762. case SP_CON:
  2763. case SP_CRT:
  2764. if (sd->state.lr_flag != 2)
  2765. sd->indexed_bonus.param_bonus[type - SP_POW + PARAM_POW] += val;
  2766. break;
  2767. case SP_ATK1:
  2768. if(!sd->state.lr_flag) {
  2769. bonus = status->rhw.atk + val;
  2770. status->rhw.atk = cap_value(bonus, 0, USHRT_MAX);
  2771. }
  2772. else if(sd->state.lr_flag == 1) {
  2773. bonus = status->lhw.atk + val;
  2774. status->lhw.atk = cap_value(bonus, 0, USHRT_MAX);
  2775. }
  2776. break;
  2777. case SP_ATK2:
  2778. if(!sd->state.lr_flag) {
  2779. bonus = status->rhw.atk2 + val;
  2780. status->rhw.atk2 = cap_value(bonus, 0, USHRT_MAX);
  2781. }
  2782. else if(sd->state.lr_flag == 1) {
  2783. bonus = status->lhw.atk2 + val;
  2784. status->lhw.atk2 = cap_value(bonus, 0, USHRT_MAX);
  2785. }
  2786. break;
  2787. case SP_BASE_ATK:
  2788. if(sd->state.lr_flag != 2) {
  2789. #ifdef RENEWAL
  2790. bonus = sd->bonus.eatk + val;
  2791. sd->bonus.eatk = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2792. #else
  2793. bonus = status->batk + val;
  2794. status->batk = cap_value(bonus, 0, USHRT_MAX);
  2795. #endif
  2796. }
  2797. break;
  2798. case SP_DEF1:
  2799. if(sd->state.lr_flag != 2) {
  2800. bonus = status->def + val;
  2801. #ifdef RENEWAL
  2802. status->def = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2803. #else
  2804. status->def = cap_value(bonus, CHAR_MIN, CHAR_MAX);
  2805. #endif
  2806. }
  2807. break;
  2808. case SP_DEF2:
  2809. if(sd->state.lr_flag != 2) {
  2810. bonus = status->def2 + val;
  2811. status->def2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2812. }
  2813. break;
  2814. case SP_MDEF1:
  2815. if(sd->state.lr_flag != 2) {
  2816. bonus = status->mdef + val;
  2817. #ifdef RENEWAL
  2818. status->mdef = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2819. #else
  2820. status->mdef = cap_value(bonus, CHAR_MIN, CHAR_MAX);
  2821. #endif
  2822. if( sd->state.lr_flag == 3 ) {//Shield, used for royal guard
  2823. sd->bonus.shieldmdef += bonus;
  2824. }
  2825. }
  2826. break;
  2827. case SP_MDEF2:
  2828. if(sd->state.lr_flag != 2) {
  2829. bonus = status->mdef2 + val;
  2830. status->mdef2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2831. }
  2832. break;
  2833. case SP_HIT:
  2834. if(sd->state.lr_flag != 2) {
  2835. bonus = status->hit + val;
  2836. status->hit = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2837. } else
  2838. sd->bonus.arrow_hit+=val;
  2839. break;
  2840. case SP_FLEE1:
  2841. if(sd->state.lr_flag != 2) {
  2842. bonus = status->flee + val;
  2843. status->flee = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2844. }
  2845. break;
  2846. case SP_FLEE2:
  2847. if(sd->state.lr_flag != 2) {
  2848. bonus = status->flee2 + val*10;
  2849. status->flee2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2850. }
  2851. break;
  2852. case SP_CRITICAL:
  2853. if(sd->state.lr_flag != 2) {
  2854. bonus = status->cri + val*10;
  2855. status->cri = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2856. } else
  2857. sd->bonus.arrow_cri += val*10;
  2858. break;
  2859. case SP_PATK:
  2860. if (sd->state.lr_flag != 2) {
  2861. bonus = status->patk + val;
  2862. status->patk = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2863. }
  2864. break;
  2865. case SP_SMATK:
  2866. if (sd->state.lr_flag != 2) {
  2867. bonus = status->smatk + val;
  2868. status->smatk = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2869. }
  2870. break;
  2871. case SP_RES:
  2872. if (sd->state.lr_flag != 2) {
  2873. bonus = status->res + val;
  2874. status->res = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2875. }
  2876. break;
  2877. case SP_MRES:
  2878. if (sd->state.lr_flag != 2) {
  2879. bonus = status->mres + val;
  2880. status->mres = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2881. }
  2882. break;
  2883. case SP_HPLUS:
  2884. if (sd->state.lr_flag != 2) {
  2885. bonus = status->hplus + val;
  2886. status->hplus = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2887. }
  2888. break;
  2889. case SP_CRATE:
  2890. if (sd->state.lr_flag != 2) {
  2891. bonus = status->crate + val;
  2892. status->crate = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2893. }
  2894. break;
  2895. case SP_ATKELE:
  2896. PC_BONUS_CHK_ELEMENT(val,SP_ATKELE);
  2897. switch (sd->state.lr_flag)
  2898. {
  2899. case 2:
  2900. switch (sd->status.weapon) {
  2901. case W_BOW:
  2902. case W_REVOLVER:
  2903. case W_RIFLE:
  2904. case W_GATLING:
  2905. case W_SHOTGUN:
  2906. case W_GRENADE:
  2907. //Become weapon element.
  2908. status->rhw.ele=val;
  2909. break;
  2910. default: //Become arrow element.
  2911. sd->bonus.arrow_ele=val;
  2912. break;
  2913. }
  2914. break;
  2915. case 1:
  2916. status->lhw.ele=val;
  2917. break;
  2918. default:
  2919. status->rhw.ele=val;
  2920. break;
  2921. }
  2922. break;
  2923. case SP_DEFELE:
  2924. PC_BONUS_CHK_ELEMENT(val,SP_DEFELE);
  2925. if(sd->state.lr_flag != 2)
  2926. status->def_ele=val;
  2927. break;
  2928. case SP_MAXHP:
  2929. if(sd->state.lr_flag == 2)
  2930. break;
  2931. sd->bonus.hp += val;
  2932. break;
  2933. case SP_MAXSP:
  2934. if(sd->state.lr_flag == 2)
  2935. break;
  2936. sd->bonus.sp += val;
  2937. break;
  2938. case SP_MAXAP:
  2939. if (sd->state.lr_flag == 2)
  2940. break;
  2941. sd->bonus.ap += val;
  2942. break;
  2943. case SP_MAXHPRATE:
  2944. if(sd->state.lr_flag != 2)
  2945. sd->hprate+=val;
  2946. break;
  2947. case SP_MAXSPRATE:
  2948. if(sd->state.lr_flag != 2)
  2949. sd->sprate+=val;
  2950. break;
  2951. case SP_MAXAPRATE:
  2952. if (sd->state.lr_flag != 2)
  2953. sd->aprate += val;
  2954. break;
  2955. case SP_SPRATE:
  2956. if(sd->state.lr_flag != 2)
  2957. sd->dsprate+=val;
  2958. break;
  2959. case SP_ATTACKRANGE:
  2960. switch (sd->state.lr_flag) {
  2961. case 2:
  2962. switch (sd->status.weapon) {
  2963. case W_BOW:
  2964. case W_REVOLVER:
  2965. case W_RIFLE:
  2966. case W_GATLING:
  2967. case W_SHOTGUN:
  2968. case W_GRENADE:
  2969. status->rhw.range += val;
  2970. }
  2971. break;
  2972. case 1:
  2973. status->lhw.range += val;
  2974. break;
  2975. default:
  2976. status->rhw.range += val;
  2977. break;
  2978. }
  2979. break;
  2980. case SP_SPEED_RATE: //Non stackable increase
  2981. if(sd->state.lr_flag != 2)
  2982. sd->bonus.speed_rate = min(sd->bonus.speed_rate, -val);
  2983. break;
  2984. case SP_SPEED_ADDRATE: //Stackable increase
  2985. if(sd->state.lr_flag != 2)
  2986. sd->bonus.speed_add_rate -= val;
  2987. break;
  2988. case SP_ASPD: //Raw increase
  2989. if(sd->state.lr_flag != 2)
  2990. sd->bonus.aspd_add -= 10*val;
  2991. break;
  2992. case SP_ASPD_RATE: //Stackable increase - Made it linear as per rodatazone
  2993. if(sd->state.lr_flag != 2)
  2994. #ifndef RENEWAL_ASPD
  2995. status->aspd_rate -= 10*val;
  2996. #else
  2997. status->aspd_rate2 += val;
  2998. #endif
  2999. break;
  3000. case SP_HP_RECOV_RATE:
  3001. if(sd->state.lr_flag != 2)
  3002. sd->hprecov_rate += val;
  3003. break;
  3004. case SP_SP_RECOV_RATE:
  3005. if(sd->state.lr_flag != 2)
  3006. sd->sprecov_rate += val;
  3007. break;
  3008. case SP_CRITICAL_DEF:
  3009. if(sd->state.lr_flag != 2)
  3010. sd->bonus.critical_def += val;
  3011. break;
  3012. case SP_NEAR_ATK_DEF:
  3013. if(sd->state.lr_flag != 2)
  3014. sd->bonus.near_attack_def_rate += val;
  3015. break;
  3016. case SP_LONG_ATK_DEF:
  3017. if(sd->state.lr_flag != 2)
  3018. sd->bonus.long_attack_def_rate += val;
  3019. break;
  3020. case SP_DOUBLE_RATE:
  3021. if(sd->state.lr_flag == 0 && sd->bonus.double_rate < val)
  3022. sd->bonus.double_rate = val;
  3023. break;
  3024. case SP_DOUBLE_ADD_RATE:
  3025. if(sd->state.lr_flag == 0)
  3026. sd->bonus.double_add_rate += val;
  3027. break;
  3028. case SP_MATK_RATE:
  3029. if(sd->state.lr_flag != 2)
  3030. sd->matk_rate += val;
  3031. break;
  3032. case SP_IGNORE_DEF_ELE:
  3033. PC_BONUS_CHK_ELEMENT(val,SP_IGNORE_DEF_ELE);
  3034. if(!sd->state.lr_flag)
  3035. sd->right_weapon.ignore_def_ele |= 1<<val;
  3036. else if(sd->state.lr_flag == 1)
  3037. sd->left_weapon.ignore_def_ele |= 1<<val;
  3038. break;
  3039. case SP_IGNORE_DEF_RACE:
  3040. PC_BONUS_CHK_RACE(val,SP_IGNORE_DEF_RACE);
  3041. if(!sd->state.lr_flag)
  3042. sd->right_weapon.ignore_def_race |= 1<<val;
  3043. else if(sd->state.lr_flag == 1)
  3044. sd->left_weapon.ignore_def_race |= 1<<val;
  3045. break;
  3046. case SP_IGNORE_DEF_CLASS:
  3047. PC_BONUS_CHK_CLASS(val,SP_IGNORE_DEF_CLASS);
  3048. if(!sd->state.lr_flag)
  3049. sd->right_weapon.ignore_def_class |= 1<<val;
  3050. else if(sd->state.lr_flag == 1)
  3051. sd->left_weapon.ignore_def_class |= 1<<val;
  3052. break;
  3053. case SP_ATK_RATE:
  3054. if(sd->state.lr_flag != 2)
  3055. sd->bonus.atk_rate += val;
  3056. break;
  3057. case SP_MAGIC_ATK_DEF:
  3058. if(sd->state.lr_flag != 2)
  3059. sd->bonus.magic_def_rate += val;
  3060. break;
  3061. case SP_MISC_ATK_DEF:
  3062. if(sd->state.lr_flag != 2)
  3063. sd->bonus.misc_def_rate += val;
  3064. break;
  3065. case SP_IGNORE_MDEF_ELE:
  3066. PC_BONUS_CHK_ELEMENT(val,SP_IGNORE_MDEF_ELE);
  3067. if(sd->state.lr_flag != 2)
  3068. sd->bonus.ignore_mdef_ele |= 1<<val;
  3069. break;
  3070. case SP_IGNORE_MDEF_RACE:
  3071. PC_BONUS_CHK_RACE(val,SP_IGNORE_MDEF_RACE);
  3072. if(sd->state.lr_flag != 2)
  3073. sd->bonus.ignore_mdef_race |= 1<<val;
  3074. break;
  3075. case SP_PERFECT_HIT_RATE:
  3076. if(sd->state.lr_flag != 2 && sd->bonus.perfect_hit < val)
  3077. sd->bonus.perfect_hit = val;
  3078. break;
  3079. case SP_PERFECT_HIT_ADD_RATE:
  3080. if(sd->state.lr_flag != 2)
  3081. sd->bonus.perfect_hit_add += val;
  3082. break;
  3083. case SP_CRITICAL_RATE:
  3084. if(sd->state.lr_flag != 2)
  3085. sd->critical_rate+=val;
  3086. break;
  3087. case SP_DEF_RATIO_ATK_ELE:
  3088. PC_BONUS_CHK_ELEMENT(val,SP_DEF_RATIO_ATK_ELE);
  3089. if(!sd->state.lr_flag)
  3090. sd->right_weapon.def_ratio_atk_ele |= 1<<val;
  3091. else if(sd->state.lr_flag == 1)
  3092. sd->left_weapon.def_ratio_atk_ele |= 1<<val;
  3093. break;
  3094. case SP_DEF_RATIO_ATK_RACE:
  3095. PC_BONUS_CHK_RACE(val,SP_DEF_RATIO_ATK_RACE);
  3096. if(!sd->state.lr_flag)
  3097. sd->right_weapon.def_ratio_atk_race |= 1<<val;
  3098. else if(sd->state.lr_flag == 1)
  3099. sd->left_weapon.def_ratio_atk_race |= 1<<val;
  3100. break;
  3101. case SP_DEF_RATIO_ATK_CLASS:
  3102. PC_BONUS_CHK_CLASS(val,SP_DEF_RATIO_ATK_CLASS);
  3103. if(!sd->state.lr_flag)
  3104. sd->right_weapon.def_ratio_atk_class |= 1<<val;
  3105. else if(sd->state.lr_flag == 1)
  3106. sd->left_weapon.def_ratio_atk_class |= 1<<val;
  3107. break;
  3108. case SP_HIT_RATE:
  3109. if(sd->state.lr_flag != 2)
  3110. sd->hit_rate += val;
  3111. break;
  3112. case SP_FLEE_RATE:
  3113. if(sd->state.lr_flag != 2)
  3114. sd->flee_rate += val;
  3115. break;
  3116. case SP_FLEE2_RATE:
  3117. if(sd->state.lr_flag != 2)
  3118. sd->flee2_rate += val;
  3119. break;
  3120. case SP_DEF_RATE:
  3121. if(sd->state.lr_flag != 2)
  3122. sd->def_rate += val;
  3123. break;
  3124. case SP_DEF2_RATE:
  3125. if(sd->state.lr_flag != 2)
  3126. sd->def2_rate += val;
  3127. break;
  3128. case SP_MDEF_RATE:
  3129. if(sd->state.lr_flag != 2)
  3130. sd->mdef_rate += val;
  3131. break;
  3132. case SP_MDEF2_RATE:
  3133. if(sd->state.lr_flag != 2)
  3134. sd->mdef2_rate += val;
  3135. break;
  3136. case SP_PATK_RATE:
  3137. if (sd->state.lr_flag != 2)
  3138. sd->patk_rate += val;
  3139. break;
  3140. case SP_SMATK_RATE:
  3141. if (sd->state.lr_flag != 2)
  3142. sd->smatk_rate += val;
  3143. break;
  3144. case SP_RES_RATE:
  3145. if (sd->state.lr_flag != 2)
  3146. sd->res_rate += val;
  3147. break;
  3148. case SP_MRES_RATE:
  3149. if (sd->state.lr_flag != 2)
  3150. sd->mres_rate += val;
  3151. break;
  3152. case SP_HPLUS_RATE:
  3153. if (sd->state.lr_flag != 2)
  3154. sd->hplus_rate += val;
  3155. break;
  3156. case SP_CRATE_RATE:
  3157. if (sd->state.lr_flag != 2)
  3158. sd->crate_rate += val;
  3159. break;
  3160. case SP_RESTART_FULL_RECOVER:
  3161. if(sd->state.lr_flag != 2)
  3162. sd->special_state.restart_full_recover = 1;
  3163. break;
  3164. case SP_NO_CASTCANCEL:
  3165. if(sd->state.lr_flag != 2)
  3166. sd->special_state.no_castcancel = 1;
  3167. break;
  3168. case SP_NO_CASTCANCEL2:
  3169. if(sd->state.lr_flag != 2)
  3170. sd->special_state.no_castcancel2 = 1;
  3171. break;
  3172. case SP_NO_SIZEFIX:
  3173. if(sd->state.lr_flag != 2)
  3174. sd->special_state.no_sizefix = 1;
  3175. break;
  3176. case SP_NO_MAGIC_DAMAGE:
  3177. if(sd->state.lr_flag == 2)
  3178. break;
  3179. val+= sd->special_state.no_magic_damage;
  3180. sd->special_state.no_magic_damage = cap_value(val,0,100);
  3181. break;
  3182. case SP_NO_WEAPON_DAMAGE:
  3183. if(sd->state.lr_flag == 2)
  3184. break;
  3185. val+= sd->special_state.no_weapon_damage;
  3186. sd->special_state.no_weapon_damage = cap_value(val,0,100);
  3187. break;
  3188. case SP_NO_MISC_DAMAGE:
  3189. if(sd->state.lr_flag == 2)
  3190. break;
  3191. val+= sd->special_state.no_misc_damage;
  3192. sd->special_state.no_misc_damage = cap_value(val,0,100);
  3193. break;
  3194. case SP_NO_GEMSTONE:
  3195. if(sd->state.lr_flag != 2 && sd->special_state.no_gemstone != 2)
  3196. sd->special_state.no_gemstone = 1;
  3197. break;
  3198. case SP_INTRAVISION: // Maya Purple Card effect allowing to see Hiding/Cloaking people [DracoRPG]
  3199. if(sd->state.lr_flag != 2) {
  3200. sd->special_state.intravision = 1;
  3201. clif_status_load(&sd->bl, EFST_CLAIRVOYANCE, 1);
  3202. }
  3203. break;
  3204. case SP_NO_KNOCKBACK:
  3205. if(sd->state.lr_flag != 2)
  3206. sd->special_state.no_knockback = 1;
  3207. break;
  3208. case SP_SPLASH_RANGE:
  3209. if(sd->bonus.splash_range < val)
  3210. sd->bonus.splash_range = val;
  3211. break;
  3212. case SP_SPLASH_ADD_RANGE:
  3213. sd->bonus.splash_add_range += val;
  3214. break;
  3215. case SP_SHORT_WEAPON_DAMAGE_RETURN:
  3216. if(sd->state.lr_flag != 2)
  3217. sd->bonus.short_weapon_damage_return += val;
  3218. break;
  3219. case SP_LONG_WEAPON_DAMAGE_RETURN:
  3220. if(sd->state.lr_flag != 2)
  3221. sd->bonus.long_weapon_damage_return += val;
  3222. break;
  3223. case SP_MAGIC_DAMAGE_RETURN: //AppleGirl Was Here
  3224. if(sd->state.lr_flag != 2)
  3225. sd->bonus.magic_damage_return += val;
  3226. break;
  3227. case SP_REDUCE_DAMAGE_RETURN:
  3228. if (sd->state.lr_flag != 2)
  3229. sd->bonus.reduce_damage_return += val;
  3230. break;
  3231. case SP_ALL_STATS: // [Valaris]
  3232. if(sd->state.lr_flag!=2) {
  3233. sd->indexed_bonus.param_bonus[SP_STR-SP_STR]+=val;
  3234. sd->indexed_bonus.param_bonus[SP_AGI-SP_STR]+=val;
  3235. sd->indexed_bonus.param_bonus[SP_VIT-SP_STR]+=val;
  3236. sd->indexed_bonus.param_bonus[SP_INT-SP_STR]+=val;
  3237. sd->indexed_bonus.param_bonus[SP_DEX-SP_STR]+=val;
  3238. sd->indexed_bonus.param_bonus[SP_LUK-SP_STR]+=val;
  3239. }
  3240. break;
  3241. case SP_ALL_TRAIT_STATS:
  3242. if (sd->state.lr_flag != 2) {
  3243. sd->indexed_bonus.param_bonus[PARAM_POW] += val;
  3244. sd->indexed_bonus.param_bonus[PARAM_STA] += val;
  3245. sd->indexed_bonus.param_bonus[PARAM_WIS] += val;
  3246. sd->indexed_bonus.param_bonus[PARAM_SPL] += val;
  3247. sd->indexed_bonus.param_bonus[PARAM_CON] += val;
  3248. sd->indexed_bonus.param_bonus[PARAM_CRT] += val;
  3249. }
  3250. break;
  3251. case SP_AGI_VIT: // [Valaris]
  3252. if(sd->state.lr_flag!=2) {
  3253. sd->indexed_bonus.param_bonus[SP_AGI-SP_STR]+=val;
  3254. sd->indexed_bonus.param_bonus[SP_VIT-SP_STR]+=val;
  3255. }
  3256. break;
  3257. case SP_AGI_DEX_STR: // [Valaris]
  3258. if(sd->state.lr_flag!=2) {
  3259. sd->indexed_bonus.param_bonus[SP_AGI-SP_STR]+=val;
  3260. sd->indexed_bonus.param_bonus[SP_DEX-SP_STR]+=val;
  3261. sd->indexed_bonus.param_bonus[SP_STR-SP_STR]+=val;
  3262. }
  3263. break;
  3264. case SP_PERFECT_HIDE: // [Valaris]
  3265. if(sd->state.lr_flag!=2)
  3266. sd->special_state.perfect_hiding=1;
  3267. break;
  3268. case SP_UNBREAKABLE:
  3269. if(sd->state.lr_flag!=2)
  3270. sd->bonus.unbreakable += val;
  3271. break;
  3272. case SP_UNBREAKABLE_WEAPON:
  3273. if(sd->state.lr_flag != 2)
  3274. sd->bonus.unbreakable_equip |= EQP_WEAPON;
  3275. break;
  3276. case SP_UNBREAKABLE_ARMOR:
  3277. if(sd->state.lr_flag != 2)
  3278. sd->bonus.unbreakable_equip |= EQP_ARMOR;
  3279. break;
  3280. case SP_UNBREAKABLE_HELM:
  3281. if(sd->state.lr_flag != 2)
  3282. sd->bonus.unbreakable_equip |= EQP_HELM;
  3283. break;
  3284. case SP_UNBREAKABLE_SHIELD:
  3285. if(sd->state.lr_flag != 2)
  3286. sd->bonus.unbreakable_equip |= EQP_SHIELD;
  3287. break;
  3288. case SP_UNBREAKABLE_GARMENT:
  3289. if(sd->state.lr_flag != 2)
  3290. sd->bonus.unbreakable_equip |= EQP_GARMENT;
  3291. break;
  3292. case SP_UNBREAKABLE_SHOES:
  3293. if(sd->state.lr_flag != 2)
  3294. sd->bonus.unbreakable_equip |= EQP_SHOES;
  3295. break;
  3296. case SP_CLASSCHANGE: // [Valaris]
  3297. if(sd->state.lr_flag !=2)
  3298. sd->bonus.classchange=val;
  3299. break;
  3300. case SP_SHORT_ATK_RATE:
  3301. if(sd->state.lr_flag != 2) //[Lupus] it should stack, too. As any other cards rate bonuses
  3302. sd->bonus.short_attack_atk_rate+=val;
  3303. break;
  3304. case SP_LONG_ATK_RATE:
  3305. if(sd->state.lr_flag != 2) //[Lupus] it should stack, too. As any other cards rate bonuses
  3306. sd->bonus.long_attack_atk_rate+=val;
  3307. break;
  3308. case SP_BREAK_WEAPON_RATE:
  3309. if(sd->state.lr_flag != 2)
  3310. sd->bonus.break_weapon_rate+=val;
  3311. break;
  3312. case SP_BREAK_ARMOR_RATE:
  3313. if(sd->state.lr_flag != 2)
  3314. sd->bonus.break_armor_rate+=val;
  3315. break;
  3316. case SP_ADD_STEAL_RATE:
  3317. if(sd->state.lr_flag != 2)
  3318. sd->bonus.add_steal_rate+=val;
  3319. break;
  3320. case SP_DELAYRATE:
  3321. if(sd->state.lr_flag != 2)
  3322. sd->bonus.delayrate -= val;
  3323. break;
  3324. case SP_CRIT_ATK_RATE:
  3325. if(sd->state.lr_flag != 2)
  3326. sd->bonus.crit_atk_rate += val;
  3327. break;
  3328. case SP_CRIT_DEF_RATE:
  3329. if(sd->state.lr_flag != 2)
  3330. sd->bonus.crit_def_rate += val;
  3331. break;
  3332. case SP_NO_REGEN:
  3333. if(sd->state.lr_flag != 2)
  3334. sd->regen.state.block|=val;
  3335. break;
  3336. case SP_UNSTRIPABLE_WEAPON:
  3337. if(sd->state.lr_flag != 2)
  3338. sd->bonus.unstripable_equip |= EQP_WEAPON;
  3339. break;
  3340. case SP_UNSTRIPABLE:
  3341. case SP_UNSTRIPABLE_ARMOR:
  3342. if(sd->state.lr_flag != 2)
  3343. sd->bonus.unstripable_equip |= EQP_ARMOR;
  3344. break;
  3345. case SP_UNSTRIPABLE_HELM:
  3346. if(sd->state.lr_flag != 2)
  3347. sd->bonus.unstripable_equip |= EQP_HELM;
  3348. break;
  3349. case SP_UNSTRIPABLE_SHIELD:
  3350. if(sd->state.lr_flag != 2)
  3351. sd->bonus.unstripable_equip |= EQP_SHIELD;
  3352. break;
  3353. case SP_HP_DRAIN_VALUE: // bonus bHPDrainValue,n;
  3354. if(!sd->state.lr_flag) {
  3355. sd->right_weapon.hp_drain_class[CLASS_NORMAL] += val;
  3356. sd->right_weapon.hp_drain_class[CLASS_BOSS] += val;
  3357. } else if(sd->state.lr_flag == 1) {
  3358. sd->left_weapon.hp_drain_class[CLASS_NORMAL] += val;
  3359. sd->left_weapon.hp_drain_class[CLASS_BOSS] += val;
  3360. }
  3361. break;
  3362. case SP_SP_DRAIN_VALUE: // bonus bSPDrainValue,n;
  3363. if(!sd->state.lr_flag) {
  3364. sd->right_weapon.sp_drain_class[CLASS_NORMAL] += val;
  3365. sd->right_weapon.sp_drain_class[CLASS_BOSS] += val;
  3366. } else if(sd->state.lr_flag == 1) {
  3367. sd->left_weapon.sp_drain_class[CLASS_NORMAL] += val;
  3368. sd->left_weapon.sp_drain_class[CLASS_BOSS] += val;
  3369. }
  3370. break;
  3371. case SP_SP_GAIN_VALUE:
  3372. if(!sd->state.lr_flag)
  3373. sd->bonus.sp_gain_value += val;
  3374. break;
  3375. case SP_HP_GAIN_VALUE:
  3376. if(!sd->state.lr_flag)
  3377. sd->bonus.hp_gain_value += val;
  3378. break;
  3379. case SP_LONG_SP_GAIN_VALUE:
  3380. if(!sd->state.lr_flag)
  3381. sd->bonus.long_sp_gain_value += val;
  3382. case SP_LONG_HP_GAIN_VALUE:
  3383. if(!sd->state.lr_flag)
  3384. sd->bonus.long_hp_gain_value += val;
  3385. break;
  3386. case SP_MAGIC_SP_GAIN_VALUE:
  3387. if(!sd->state.lr_flag)
  3388. sd->bonus.magic_sp_gain_value += val;
  3389. break;
  3390. case SP_MAGIC_HP_GAIN_VALUE:
  3391. if(!sd->state.lr_flag)
  3392. sd->bonus.magic_hp_gain_value += val;
  3393. break;
  3394. case SP_ADD_HEAL_RATE:
  3395. if(sd->state.lr_flag != 2)
  3396. sd->bonus.add_heal_rate += val;
  3397. break;
  3398. case SP_ADD_HEAL2_RATE:
  3399. if(sd->state.lr_flag != 2)
  3400. sd->bonus.add_heal2_rate += val;
  3401. break;
  3402. case SP_ADD_ITEM_HEAL_RATE:
  3403. if(sd->state.lr_flag != 2)
  3404. sd->bonus.itemhealrate2 += val;
  3405. break;
  3406. case SP_EMATK:
  3407. if(sd->state.lr_flag != 2)
  3408. sd->bonus.ematk += val;
  3409. break;
  3410. case SP_ADD_VARIABLECAST:
  3411. if (sd->state.lr_flag != 2)
  3412. sd->bonus.add_varcast += val;
  3413. break;
  3414. #ifdef RENEWAL_CAST
  3415. case SP_FIXCASTRATE:
  3416. if(sd->state.lr_flag != 2)
  3417. sd->bonus.fixcastrate = min(sd->bonus.fixcastrate,val);
  3418. break;
  3419. case SP_ADD_FIXEDCAST:
  3420. if(sd->state.lr_flag != 2)
  3421. sd->bonus.add_fixcast += val;
  3422. break;
  3423. case SP_CASTRATE:
  3424. case SP_VARCASTRATE:
  3425. if(sd->state.lr_flag != 2)
  3426. sd->bonus.varcastrate -= val;
  3427. break;
  3428. #else
  3429. case SP_ADD_FIXEDCAST:
  3430. case SP_FIXCASTRATE:
  3431. //ShowWarning("pc_bonus: non-RENEWAL_CAST doesn't support this bonus %d.\n", type);
  3432. break;
  3433. case SP_VARCASTRATE:
  3434. case SP_CASTRATE:
  3435. if(sd->state.lr_flag != 2)
  3436. sd->castrate += val;
  3437. break;
  3438. #endif
  3439. case SP_ADDMAXWEIGHT:
  3440. if (sd->state.lr_flag != 2)
  3441. sd->add_max_weight += val;
  3442. break;
  3443. case SP_ABSORB_DMG_MAXHP: // bonus bAbsorbDmgMaxHP,n;
  3444. sd->bonus.absorb_dmg_maxhp = max(sd->bonus.absorb_dmg_maxhp, val);
  3445. break;
  3446. case SP_ABSORB_DMG_MAXHP2:
  3447. sd->bonus.absorb_dmg_maxhp2 = max(sd->bonus.absorb_dmg_maxhp2, val);
  3448. break;
  3449. case SP_CRITICAL_RANGEATK: // bonus bCriticalLong,n;
  3450. if (sd->state.lr_flag != 2)
  3451. sd->bonus.critical_rangeatk += val*10;
  3452. else
  3453. sd->bonus.arrow_cri += val*10;
  3454. break;
  3455. case SP_WEAPON_ATK_RATE:
  3456. if (sd->state.lr_flag != 2)
  3457. sd->bonus.weapon_atk_rate += val;
  3458. break;
  3459. case SP_WEAPON_MATK_RATE:
  3460. if (sd->state.lr_flag != 2)
  3461. sd->bonus.weapon_matk_rate += val;
  3462. break;
  3463. case SP_NO_MADO_FUEL:
  3464. if (sd->state.lr_flag != 2)
  3465. sd->special_state.no_mado_fuel = 1;
  3466. break;
  3467. case SP_NO_WALK_DELAY:
  3468. if (sd->state.lr_flag != 2)
  3469. sd->special_state.no_walk_delay = 1;
  3470. break;
  3471. case SP_ADD_ITEM_SPHEAL_RATE:
  3472. if(sd->state.lr_flag != 2)
  3473. sd->bonus.itemsphealrate2 += val;
  3474. break;
  3475. default:
  3476. if (current_equip_combo_pos > 0) {
  3477. ShowWarning("pc_bonus: unknown bonus type %d %d in a combo with item #%u\n", type, val, sd->inventory_data[pc_checkequip( sd, current_equip_combo_pos )]->nameid);
  3478. }
  3479. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  3480. ShowWarning("pc_bonus: unknown bonus type %d %d in item #%u\n", type, val, current_equip_card_id ? current_equip_card_id : sd->inventory_data[current_equip_item_index]->nameid);
  3481. }
  3482. else {
  3483. ShowWarning("pc_bonus: unknown bonus type %d %d in unknown usage. Report this!\n", type, val);
  3484. }
  3485. break;
  3486. }
  3487. }
  3488. /*==========================================
  3489. * Player bonus (type) with args type2 and val, called trough bonus2 (npc)
  3490. * format: bonus2 bBonusName,type2,val;
  3491. * @param sd
  3492. * @param type Bonus type used by bBonusName
  3493. * @param type2
  3494. * @param val Value that usually for rate or fixed value
  3495. *------------------------------------------*/
  3496. void pc_bonus2(struct map_session_data *sd,int type,int type2,int val)
  3497. {
  3498. nullpo_retv(sd);
  3499. switch(type){
  3500. case SP_ADDELE: // bonus2 bAddEle,e,x;
  3501. PC_BONUS_CHK_ELEMENT(type2,SP_ADDELE);
  3502. if(!sd->state.lr_flag || sd->state.lr_flag == 3)
  3503. sd->right_weapon.addele[type2]+=val;
  3504. else if(sd->state.lr_flag == 1)
  3505. sd->left_weapon.addele[type2]+=val;
  3506. else if(sd->state.lr_flag == 2)
  3507. sd->indexed_bonus.arrow_addele[type2]+=val;
  3508. break;
  3509. case SP_ADDRACE: // bonus2 bAddRace,r,x;
  3510. PC_BONUS_CHK_RACE(type2,SP_ADDRACE);
  3511. if(!sd->state.lr_flag || sd->state.lr_flag == 3)
  3512. sd->right_weapon.addrace[type2]+=val;
  3513. else if(sd->state.lr_flag == 1)
  3514. sd->left_weapon.addrace[type2]+=val;
  3515. else if(sd->state.lr_flag == 2)
  3516. sd->indexed_bonus.arrow_addrace[type2]+=val;
  3517. break;
  3518. case SP_ADDCLASS: // bonus2 bAddClass,c,x;
  3519. PC_BONUS_CHK_CLASS(type2,SP_ADDCLASS);
  3520. if(!sd->state.lr_flag || sd->state.lr_flag == 3)
  3521. sd->right_weapon.addclass[type2]+=val;
  3522. else if(sd->state.lr_flag == 1)
  3523. sd->left_weapon.addclass[type2]+=val;
  3524. else if(sd->state.lr_flag == 2)
  3525. sd->indexed_bonus.arrow_addclass[type2]+=val;
  3526. break;
  3527. case SP_ADDSIZE: // bonus2 bAddSize,s,x;
  3528. PC_BONUS_CHK_SIZE(type2,SP_ADDSIZE);
  3529. if(!sd->state.lr_flag || sd->state.lr_flag == 3)
  3530. sd->right_weapon.addsize[type2]+=val;
  3531. else if(sd->state.lr_flag == 1)
  3532. sd->left_weapon.addsize[type2]+=val;
  3533. else if(sd->state.lr_flag == 2)
  3534. sd->indexed_bonus.arrow_addsize[type2]+=val;
  3535. break;
  3536. case SP_SUBELE: // bonus2 bSubEle,e,x;
  3537. PC_BONUS_CHK_ELEMENT(type2,SP_SUBELE);
  3538. if(sd->state.lr_flag != 2)
  3539. sd->indexed_bonus.subele_script[type2] += val;
  3540. break;
  3541. case SP_SUBRACE: // bonus2 bSubRace,r,x;
  3542. PC_BONUS_CHK_RACE(type2,SP_SUBRACE);
  3543. if(sd->state.lr_flag != 2)
  3544. sd->indexed_bonus.subrace[type2]+=val;
  3545. break;
  3546. case SP_SUBCLASS: // bonus2 bSubClass,c,x;
  3547. PC_BONUS_CHK_CLASS(type2,SP_SUBCLASS);
  3548. if(sd->state.lr_flag != 2)
  3549. sd->indexed_bonus.subclass[type2]+=val;
  3550. break;
  3551. case SP_ADDEFF: // bonus2 bAddEff,eff,n;
  3552. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  3553. pc_bonus_addeff(sd->addeff, (sc_type)type2, sd->state.lr_flag != 2 ? val : 0, sd->state.lr_flag == 2 ? val : 0, 0, 0);
  3554. break;
  3555. case SP_ADDEFF2: // bonus2 bAddEff2,eff,n;
  3556. PC_BONUS_CHK_SC(type2,SP_ADDEFF2);
  3557. pc_bonus_addeff(sd->addeff, (sc_type)type2, sd->state.lr_flag != 2 ? val : 0, sd->state.lr_flag == 2 ? val : 0, ATF_SELF, 0);
  3558. break;
  3559. case SP_RESEFF: // bonus2 bResEff,eff,n;
  3560. if(sd->state.lr_flag == 2)
  3561. break;
  3562. if (sd->reseff.size() == MAX_PC_BONUS) {
  3563. ShowWarning("pc_bonus2: SP_RESEFF: Reached max (%d) number of resistance bonuses per character!\n", MAX_PC_BONUS);
  3564. break;
  3565. }
  3566. pc_bonus_itembonus(sd->reseff, type2, val, true);
  3567. break;
  3568. case SP_MAGIC_ADDELE: // bonus2 bMagicAddEle,e,x;
  3569. PC_BONUS_CHK_ELEMENT(type2,SP_MAGIC_ADDELE);
  3570. if(sd->state.lr_flag != 2)
  3571. sd->indexed_bonus.magic_addele_script[type2] += val;
  3572. break;
  3573. case SP_MAGIC_ADDRACE: // bonus2 bMagicAddRace,r,x;
  3574. PC_BONUS_CHK_RACE(type2,SP_MAGIC_ADDRACE);
  3575. if(sd->state.lr_flag != 2)
  3576. sd->indexed_bonus.magic_addrace[type2]+=val;
  3577. break;
  3578. case SP_MAGIC_ADDCLASS: // bonus2 bMagicAddClass,c,x;
  3579. PC_BONUS_CHK_CLASS(type2,SP_MAGIC_ADDCLASS);
  3580. if(sd->state.lr_flag != 2)
  3581. sd->indexed_bonus.magic_addclass[type2]+=val;
  3582. break;
  3583. case SP_MAGIC_ADDSIZE: // bonus2 bMagicAddSize,s,x;
  3584. PC_BONUS_CHK_SIZE(type2,SP_MAGIC_ADDSIZE);
  3585. if(sd->state.lr_flag != 2)
  3586. sd->indexed_bonus.magic_addsize[type2]+=val;
  3587. break;
  3588. case SP_MAGIC_ATK_ELE: // bonus2 bMagicAtkEle,e,x;
  3589. PC_BONUS_CHK_ELEMENT(type2,SP_MAGIC_ATK_ELE);
  3590. if(sd->state.lr_flag != 2)
  3591. sd->indexed_bonus.magic_atk_ele[type2]+=val;
  3592. break;
  3593. case SP_ADD_DAMAGE_CLASS: { // bonus2 bAddDamageClass,mid,x;
  3594. struct weapon_data *wd = (sd->state.lr_flag ? &sd->left_weapon : &sd->right_weapon);
  3595. if (wd->add_dmg.size() == MAX_PC_BONUS) {
  3596. ShowWarning("pc_bonus2: SP_ADD_DAMAGE_CLASS: Reached max (%d) number of add class damage bonuses per character!\n", MAX_PC_BONUS);
  3597. break;
  3598. }
  3599. pc_bonus_itembonus(wd->add_dmg, type2, val, false);
  3600. }
  3601. break;
  3602. case SP_ADD_MAGIC_DAMAGE_CLASS: // bonus2 bAddMagicDamageClass,mid,x;
  3603. if(sd->state.lr_flag == 2)
  3604. break;
  3605. if (sd->add_mdmg.size() == MAX_PC_BONUS) {
  3606. ShowWarning("pc_bonus2: SP_ADD_MAGIC_DAMAGE_CLASS: Reached max (%d) number of add class magic dmg bonuses per character!\n", MAX_PC_BONUS);
  3607. break;
  3608. }
  3609. pc_bonus_itembonus(sd->add_mdmg, type2, val, false);
  3610. break;
  3611. case SP_ADD_DEF_MONSTER: // bonus2 bAddDefMonster,mid,x;
  3612. if(sd->state.lr_flag == 2)
  3613. break;
  3614. if (sd->add_def.size() == MAX_PC_BONUS) {
  3615. ShowWarning("pc_bonus2: SP_ADD_DEF_MONSTER: Reached max (%d) number of add class def bonuses per character!\n", MAX_PC_BONUS);
  3616. break;
  3617. }
  3618. pc_bonus_itembonus(sd->add_def, type2, val, false);
  3619. break;
  3620. case SP_ADD_MDEF_MONSTER: // bonus2 bAddMDefMonster,mid,x;
  3621. if(sd->state.lr_flag == 2)
  3622. break;
  3623. if (sd->add_mdef.size() == MAX_PC_BONUS) {
  3624. ShowWarning("pc_bonus2: SP_ADD_MDEF_MONSTER: Reached max (%d) number of add class mdef bonuses per character!\n", MAX_PC_BONUS);
  3625. break;
  3626. }
  3627. pc_bonus_itembonus(sd->add_mdef, type2, val, false);
  3628. break;
  3629. case SP_HP_DRAIN_RATE: // bonus2 bHPDrainRate,x,n;
  3630. if(!sd->state.lr_flag) {
  3631. sd->right_weapon.hp_drain_rate.rate += type2;
  3632. sd->right_weapon.hp_drain_rate.per += val;
  3633. }
  3634. else if(sd->state.lr_flag == 1) {
  3635. sd->left_weapon.hp_drain_rate.rate += type2;
  3636. sd->left_weapon.hp_drain_rate.per += val;
  3637. }
  3638. break;
  3639. case SP_SP_DRAIN_RATE: // bonus2 bSPDrainRate,x,n;
  3640. if(!sd->state.lr_flag) {
  3641. sd->right_weapon.sp_drain_rate.rate += type2;
  3642. sd->right_weapon.sp_drain_rate.per += val;
  3643. }
  3644. else if(sd->state.lr_flag == 1) {
  3645. sd->left_weapon.sp_drain_rate.rate += type2;
  3646. sd->left_weapon.sp_drain_rate.per += val;
  3647. }
  3648. break;
  3649. case SP_SP_VANISH_RATE: // bonus2 bSPVanishRate,x,n;
  3650. if(sd->state.lr_flag != 2) {
  3651. pc_bonus_addvanish(sd->sp_vanish, type2, val, BF_NORMAL);
  3652. }
  3653. break;
  3654. case SP_HP_VANISH_RATE: // bonus2 bHPVanishRate,x,n;
  3655. if(sd->state.lr_flag != 2) {
  3656. pc_bonus_addvanish(sd->hp_vanish, type2, val, BF_NORMAL);
  3657. }
  3658. break;
  3659. case SP_GET_ZENY_NUM: // bonus2 bGetZenyNum,x,n;
  3660. if(sd->state.lr_flag != 2 && sd->bonus.get_zeny_rate < val) {
  3661. sd->bonus.get_zeny_rate = val;
  3662. sd->bonus.get_zeny_num = type2;
  3663. }
  3664. break;
  3665. case SP_ADD_GET_ZENY_NUM: // bonus2 bAddGetZenyNum,x,n;
  3666. if(sd->state.lr_flag != 2) {
  3667. sd->bonus.get_zeny_rate += val;
  3668. sd->bonus.get_zeny_num += type2;
  3669. }
  3670. break;
  3671. case SP_WEAPON_COMA_ELE: // bonus2 bWeaponComaEle,e,n;
  3672. PC_BONUS_CHK_ELEMENT(type2,SP_WEAPON_COMA_ELE);
  3673. if(sd->state.lr_flag == 2)
  3674. break;
  3675. sd->indexed_bonus.weapon_coma_ele[type2] += val;
  3676. sd->special_state.bonus_coma = 1;
  3677. break;
  3678. case SP_WEAPON_COMA_RACE: // bonus2 bWeaponComaRace,r,n;
  3679. PC_BONUS_CHK_RACE(type2,SP_WEAPON_COMA_RACE);
  3680. if(sd->state.lr_flag == 2)
  3681. break;
  3682. sd->indexed_bonus.weapon_coma_race[type2] += val;
  3683. sd->special_state.bonus_coma = 1;
  3684. break;
  3685. case SP_WEAPON_COMA_CLASS: // bonus2 bWeaponComaClass,c,n;
  3686. PC_BONUS_CHK_CLASS(type2,SP_WEAPON_COMA_CLASS);
  3687. if(sd->state.lr_flag == 2)
  3688. break;
  3689. sd->indexed_bonus.weapon_coma_class[type2] += val;
  3690. sd->special_state.bonus_coma = 1;
  3691. break;
  3692. case SP_WEAPON_ATK: // bonus2 bWeaponAtk,w,n;
  3693. if(sd->state.lr_flag != 2)
  3694. sd->indexed_bonus.weapon_atk[type2]+=val;
  3695. break;
  3696. case SP_WEAPON_DAMAGE_RATE: // bonus2 bWeaponDamageRate,w,n;
  3697. if(sd->state.lr_flag != 2)
  3698. sd->indexed_bonus.weapon_damage_rate[type2]+=val;
  3699. break;
  3700. case SP_CRITICAL_ADDRACE: // bonus2 bCriticalAddRace,r,n;
  3701. PC_BONUS_CHK_RACE(type2,SP_CRITICAL_ADDRACE);
  3702. if(sd->state.lr_flag != 2)
  3703. sd->indexed_bonus.critaddrace[type2] += val*10;
  3704. break;
  3705. case SP_ADDEFF_WHENHIT: // bonus2 bAddEffWhenHit,eff,n;
  3706. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  3707. if(sd->state.lr_flag != 2)
  3708. pc_bonus_addeff(sd->addeff_atked, (sc_type)type2, val, 0, 0, 0);
  3709. break;
  3710. case SP_SKILL_ATK: // bonus2 bSkillAtk,sk,n;
  3711. if(sd->state.lr_flag == 2)
  3712. break;
  3713. if (sd->skillatk.size() == MAX_PC_BONUS) {
  3714. ShowWarning("pc_bonus2: SP_SKILL_ATK: Reached max (%d) number of skills per character, bonus skill %d (+%d%%) lost.\n", MAX_PC_BONUS, type2, val);
  3715. break;
  3716. }
  3717. pc_bonus_itembonus(sd->skillatk, type2, val, false);
  3718. break;
  3719. case SP_SKILL_HEAL: // bonus2 bSkillHeal,sk,n;
  3720. if(sd->state.lr_flag == 2)
  3721. break;
  3722. if (sd->skillheal.size() == MAX_PC_BONUS) { // Better mention this so the array length can be updated. [Skotlex]
  3723. ShowWarning("pc_bonus2: SP_SKILL_HEAL: Reached max (%d) number of skills per character, bonus skill %d (+%d%%) lost.\n", MAX_PC_BONUS, type2, val);
  3724. break;
  3725. }
  3726. pc_bonus_itembonus(sd->skillheal, type2, val, false);
  3727. break;
  3728. case SP_SKILL_HEAL2: // bonus2 bSkillHeal2,sk,n;
  3729. if(sd->state.lr_flag == 2)
  3730. break;
  3731. if (sd->skillheal2.size() == MAX_PC_BONUS) { // Better mention this so the array length can be updated. [Skotlex]
  3732. ShowWarning("pc_bonus2: SP_SKILL_HEAL2: Reached max (%d) number of skills per character, bonus skill %d (+%d%%) lost.\n", MAX_PC_BONUS, type2, val);
  3733. break;
  3734. }
  3735. pc_bonus_itembonus(sd->skillheal2, type2, val, false);
  3736. break;
  3737. case SP_ADD_SKILL_BLOW: // bonus2 bAddSkillBlow,sk,n;
  3738. if(sd->state.lr_flag == 2)
  3739. break;
  3740. if (sd->skillblown.size() == MAX_PC_BONUS) { //Better mention this so the array length can be updated. [Skotlex]
  3741. ShowWarning("pc_bonus2: SP_ADD_SKILL_BLOW: Reached max (%d) number of skills per character, bonus skill %d (%d) lost.\n", MAX_PC_BONUS, type2, val);
  3742. break;
  3743. }
  3744. pc_bonus_itembonus(sd->skillblown, type2, val, false);
  3745. break;
  3746. case SP_HP_LOSS_RATE: // bonus2 bHPLossRate,n,t;
  3747. if(sd->state.lr_flag != 2) {
  3748. sd->hp_loss.value = type2;
  3749. sd->hp_loss.rate = val;
  3750. }
  3751. break;
  3752. case SP_HP_REGEN_RATE: // bonus2 bHPRegenRate,n,t;
  3753. if(sd->state.lr_flag != 2) {
  3754. sd->hp_regen.value = type2;
  3755. sd->hp_regen.rate = val;
  3756. }
  3757. break;
  3758. case SP_REGEN_PERCENT_HP: // bonus2 bRegenPercentHP,n,t;
  3759. if (sd->state.lr_flag != 2) {
  3760. sd->percent_hp_regen.value = type2;
  3761. sd->percent_hp_regen.rate = val;
  3762. }
  3763. break;
  3764. case SP_REGEN_PERCENT_SP: // bonus2 bRegenPercentSP,n,t;
  3765. if (sd->state.lr_flag != 2) {
  3766. sd->percent_sp_regen.value = type2;
  3767. sd->percent_sp_regen.rate = val;
  3768. }
  3769. break;
  3770. case SP_ADDRACE2: // bonus2 bAddRace2,mr,x;
  3771. PC_BONUS_CHK_RACE2(type2,SP_ADDRACE2);
  3772. if(sd->state.lr_flag != 2)
  3773. sd->right_weapon.addrace2[type2] += val;
  3774. else
  3775. sd->left_weapon.addrace2[type2] += val;
  3776. break;
  3777. case SP_SUBSIZE: // bonus2 bSubSize,s,x;
  3778. PC_BONUS_CHK_SIZE(type2,SP_SUBSIZE);
  3779. if(sd->state.lr_flag != 2)
  3780. sd->indexed_bonus.subsize[type2]+=val;
  3781. break;
  3782. case SP_WEAPON_SUBSIZE: // bonus2 bWeaponSubSize,s,x;
  3783. PC_BONUS_CHK_SIZE(type2, SP_WEAPON_SUBSIZE);
  3784. if (sd->state.lr_flag != 2)
  3785. sd->indexed_bonus.weapon_subsize[type2] += val;
  3786. break;
  3787. case SP_MAGIC_SUBSIZE: // bonus2 bMagicSubSize,s,x;
  3788. PC_BONUS_CHK_SIZE(type2,SP_MAGIC_SUBSIZE);
  3789. if(sd->state.lr_flag != 2)
  3790. sd->indexed_bonus.magic_subsize[type2]+=val;
  3791. break;
  3792. case SP_SUBRACE2: // bonus2 bSubRace2,mr,x;
  3793. PC_BONUS_CHK_RACE2(type2,SP_SUBRACE2);
  3794. if(sd->state.lr_flag != 2)
  3795. sd->indexed_bonus.subrace2[type2]+=val;
  3796. break;
  3797. case SP_ADD_ITEM_HEAL_RATE: // bonus2 bAddItemHealRate,iid,n;
  3798. if(sd->state.lr_flag == 2)
  3799. break;
  3800. if( !item_db.exists( type2 ) ){
  3801. ShowWarning("pc_bonus2: SP_ADD_ITEM_HEAL_RATE Invalid item with id %d\n", type2);
  3802. break;
  3803. }
  3804. if (sd->itemhealrate.size() == MAX_PC_BONUS) {
  3805. ShowWarning("pc_bonus2: SP_ADD_ITEM_HEAL_RATE: Reached max (%d) number of item heal bonuses per character!\n", MAX_PC_BONUS);
  3806. break;
  3807. }
  3808. pc_bonus_itembonus(sd->itemhealrate, type2, val, false);
  3809. break;
  3810. case SP_ADD_ITEMGROUP_HEAL_RATE: // bonus2 bAddItemGroupHealRate,ig,n;
  3811. if (sd->state.lr_flag == 2)
  3812. break;
  3813. if (!type2 || !itemdb_group.exists(type2)) {
  3814. ShowWarning("pc_bonus2: SP_ADD_ITEMGROUP_HEAL_RATE: Invalid item group with id %d\n", type2);
  3815. break;
  3816. }
  3817. if (sd->itemgrouphealrate.size() == MAX_PC_BONUS) {
  3818. ShowWarning("pc_bonus2: SP_ADD_ITEMGROUP_HEAL_RATE: Reached max (%d) number of item heal bonuses per character!\n", MAX_PC_BONUS);
  3819. break;
  3820. }
  3821. pc_bonus_itembonus(sd->itemgrouphealrate, type2, val, false);
  3822. break;
  3823. case SP_EXP_ADDRACE: // bonus2 bExpAddRace,r,x;
  3824. PC_BONUS_CHK_RACE(type2,SP_EXP_ADDRACE);
  3825. if(sd->state.lr_flag != 2)
  3826. sd->indexed_bonus.expaddrace[type2]+=val;
  3827. break;
  3828. case SP_EXP_ADDCLASS: // bonus2 bExpAddClass,c,x;
  3829. PC_BONUS_CHK_CLASS(type2,SP_EXP_ADDCLASS);
  3830. if(sd->state.lr_flag != 2)
  3831. sd->indexed_bonus.expaddclass[type2]+=val;
  3832. break;
  3833. case SP_SP_GAIN_RACE: // bonus2 bSPGainRace,r,n;
  3834. PC_BONUS_CHK_RACE(type2,SP_SP_GAIN_RACE);
  3835. if(sd->state.lr_flag != 2)
  3836. sd->indexed_bonus.sp_gain_race[type2]+=val;
  3837. break;
  3838. case SP_ADD_MONSTER_DROP_ITEM: // bonus2 bAddMonsterDropItem,iid,n;
  3839. if (sd->state.lr_flag != 2)
  3840. pc_bonus_item_drop(sd->add_drop, type2, 0, CLASS_ALL, RC_NONE_, val);
  3841. break;
  3842. case SP_ADD_MONSTER_DROP_ITEMGROUP: // bonus2 bAddMonsterDropItemGroup,ig,n;
  3843. if (sd->state.lr_flag != 2)
  3844. pc_bonus_item_drop(sd->add_drop, 0, type2, CLASS_ALL, RC_NONE_, val);
  3845. break;
  3846. case SP_SP_LOSS_RATE: // bonus2 bSPLossRate,n,t;
  3847. if(sd->state.lr_flag != 2) {
  3848. sd->sp_loss.value = type2;
  3849. sd->sp_loss.rate = val;
  3850. }
  3851. break;
  3852. case SP_SP_REGEN_RATE: // bonus2 bSPRegenRate,n,t;
  3853. if(sd->state.lr_flag != 2) {
  3854. sd->sp_regen.value = type2;
  3855. sd->sp_regen.rate = val;
  3856. }
  3857. break;
  3858. case SP_HP_DRAIN_VALUE_RACE: // bonus2 bHPDrainValueRace,r,n;
  3859. PC_BONUS_CHK_RACE(type2,SP_HP_DRAIN_VALUE_RACE);
  3860. if(!sd->state.lr_flag) {
  3861. sd->right_weapon.hp_drain_race[type2] += val;
  3862. }
  3863. else if(sd->state.lr_flag == 1) {
  3864. sd->left_weapon.hp_drain_race[type2] += val;
  3865. }
  3866. break;
  3867. case SP_SP_DRAIN_VALUE_RACE: // bonus2 bSPDrainValueRace,r,n;
  3868. PC_BONUS_CHK_RACE(type2,SP_SP_DRAIN_VALUE_RACE);
  3869. if(!sd->state.lr_flag) {
  3870. sd->right_weapon.sp_drain_race[type2] += val;
  3871. }
  3872. else if(sd->state.lr_flag == 1) {
  3873. sd->left_weapon.sp_drain_race[type2] += val;
  3874. }
  3875. break;
  3876. case SP_HP_DRAIN_VALUE_CLASS: // bonus2 bHPDrainValueClass,c,n;
  3877. PC_BONUS_CHK_CLASS(type2,SP_HP_DRAIN_VALUE_CLASS);
  3878. if(!sd->state.lr_flag) {
  3879. sd->right_weapon.hp_drain_class[type2] += val;
  3880. }
  3881. else if(sd->state.lr_flag == 1) {
  3882. sd->left_weapon.hp_drain_class[type2] += val;
  3883. }
  3884. break;
  3885. case SP_SP_DRAIN_VALUE_CLASS: // bonus2 bSPDrainValueClass,c,n;
  3886. PC_BONUS_CHK_CLASS(type2,SP_SP_DRAIN_VALUE_CLASS);
  3887. if(!sd->state.lr_flag) {
  3888. sd->right_weapon.sp_drain_class[type2] += val;
  3889. }
  3890. else if(sd->state.lr_flag == 1) {
  3891. sd->left_weapon.sp_drain_class[type2] += val;
  3892. }
  3893. break;
  3894. case SP_IGNORE_MDEF_RACE_RATE: // bonus2 bIgnoreMdefRaceRate,r,n;
  3895. PC_BONUS_CHK_RACE(type2,SP_IGNORE_MDEF_RACE_RATE);
  3896. if(sd->state.lr_flag != 2)
  3897. sd->indexed_bonus.ignore_mdef_by_race[type2] += val;
  3898. break;
  3899. case SP_IGNORE_MDEF_CLASS_RATE: // bonus2 bIgnoreMdefClassRate,c,n;
  3900. PC_BONUS_CHK_CLASS(type2,SP_IGNORE_MDEF_CLASS_RATE);
  3901. if(sd->state.lr_flag != 2)
  3902. sd->indexed_bonus.ignore_mdef_by_class[type2] += val;
  3903. break;
  3904. case SP_IGNORE_DEF_RACE_RATE: // bonus2 bIgnoreDefRaceRate,r,n;
  3905. PC_BONUS_CHK_RACE(type2,SP_IGNORE_DEF_RACE_RATE);
  3906. if(sd->state.lr_flag != 2)
  3907. sd->indexed_bonus.ignore_def_by_race[type2] += val;
  3908. break;
  3909. case SP_IGNORE_DEF_CLASS_RATE: // bonus2 bIgnoreDefClassRate,r,n;
  3910. PC_BONUS_CHK_CLASS(type2, SP_IGNORE_DEF_CLASS_RATE);
  3911. if (sd->state.lr_flag != 2)
  3912. sd->indexed_bonus.ignore_def_by_class[type2] += val;
  3913. break;
  3914. case SP_SKILL_USE_SP_RATE: // bonus2 bSkillUseSPrate,sk,n;
  3915. if(sd->state.lr_flag == 2)
  3916. break;
  3917. if (sd->skillusesprate.size() == MAX_PC_BONUS) {
  3918. ShowWarning("pc_bonus2: SP_SKILL_USE_SP_RATE: Reached max (%d) number of skills per character, bonus skill %d (+%d%%) lost.\n", MAX_PC_BONUS, type2, val);
  3919. break;
  3920. }
  3921. pc_bonus_itembonus(sd->skillusesprate, type2, val, true);
  3922. break;
  3923. case SP_SKILL_DELAY:
  3924. if(sd->state.lr_flag == 2)
  3925. break;
  3926. if (sd->skilldelay.size() == MAX_PC_BONUS) {
  3927. ShowWarning("pc_bonus2: SP_SKILL_DELAY: Reached max (%d) number of skills per character, bonus skill %d (%d) lost.\n", MAX_PC_BONUS, type2, val);
  3928. break;
  3929. }
  3930. pc_bonus_itembonus(sd->skilldelay, type2, val, false);
  3931. break;
  3932. case SP_SKILL_COOLDOWN: // bonus2 bSkillCooldown,sk,t;
  3933. if(sd->state.lr_flag == 2)
  3934. break;
  3935. if (sd->skillcooldown.size() == MAX_PC_BONUS) {
  3936. ShowWarning("pc_bonus2: SP_SKILL_COOLDOWN: Reached max (%d) number of skills per character, bonus skill %d (%d) lost.\n", MAX_PC_BONUS, type2, val);
  3937. break;
  3938. }
  3939. pc_bonus_itembonus(sd->skillcooldown, type2, val, false);
  3940. break;
  3941. case SP_SKILL_VARIABLECAST: // bonus2 bSkillVariableCast,sk,t;
  3942. if (sd->state.lr_flag == 2)
  3943. break;
  3944. if (sd->skillvarcast.size() == MAX_PC_BONUS) {
  3945. ShowWarning("pc_bonus2: SP_SKILL_VARIABLECAST: Reached max (%d) number of skills per character, bonus skill %d (%d) lost.\n", MAX_PC_BONUS, type2, val);
  3946. break;
  3947. }
  3948. pc_bonus_itembonus(sd->skillvarcast, type2, val, false);
  3949. break;
  3950. #ifdef RENEWAL_CAST
  3951. case SP_SKILL_FIXEDCAST: // bonus2 bSkillFixedCast,sk,t;
  3952. if(sd->state.lr_flag == 2)
  3953. break;
  3954. if (sd->skillfixcast.size() == MAX_PC_BONUS) {
  3955. ShowWarning("pc_bonus2: SP_SKILL_FIXEDCAST: Reached max (%d) number of skills per character, bonus skill %d (%d) lost.\n", MAX_PC_BONUS, type2, val);
  3956. break;
  3957. }
  3958. pc_bonus_itembonus(sd->skillfixcast, type2, val, false);
  3959. break;
  3960. case SP_CASTRATE: // bonus2 bCastrate,sk,n;
  3961. case SP_VARCASTRATE: // bonus2 bVariableCastrate,sk,n;
  3962. if(sd->state.lr_flag == 2)
  3963. break;
  3964. if (sd->skillcastrate.size() == MAX_PC_BONUS) {
  3965. ShowWarning("pc_bonus2: SP_VARCASTRATE: Reached max (%d) number of skills per character, bonus skill %d (%d%%) lost.\n", MAX_PC_BONUS, type2, val);
  3966. break;
  3967. }
  3968. pc_bonus_itembonus(sd->skillcastrate, type2, -val, true); // Send inversed value here
  3969. break;
  3970. case SP_FIXCASTRATE: // bonus2 bFixedCastrate,sk,n;
  3971. if(sd->state.lr_flag == 2)
  3972. break;
  3973. if (sd->skillfixcastrate.size() == MAX_PC_BONUS) {
  3974. ShowWarning("pc_bonus2: SP_FIXCASTRATE: Reached max (%d) number of skills per character, bonus skill %d (%d%%) lost.\n", MAX_PC_BONUS, type2, val);
  3975. break;
  3976. }
  3977. pc_bonus_itembonus(sd->skillfixcastrate, type2, -val, true); // Send inversed value here
  3978. break;
  3979. #else
  3980. case SP_SKILL_FIXEDCAST: // bonus2 bSkillFixedCast,sk,t;
  3981. case SP_FIXCASTRATE: // bonus2 bFixedCastrate,sk,n;
  3982. //ShowWarning("pc_bonus2: Non-RENEWAL_CAST doesn't support this bonus %d.\n", type);
  3983. break;
  3984. case SP_VARCASTRATE: // bonus2 bVariableCastrate,sk,n;
  3985. case SP_CASTRATE: // bonus2 bCastrate,sk,n;
  3986. if(sd->state.lr_flag == 2)
  3987. break;
  3988. if (sd->skillcastrate.size() == MAX_PC_BONUS) { //Better mention this so the array length can be updated. [Skotlex]
  3989. ShowWarning("pc_bonus2: %s: Reached max (%d) number of skills per character, bonus skill %d (%d%%) lost.\n", (type == SP_CASTRATE) ? "SP_CASTRATE" : "SP_VARCASTRATE", MAX_PC_BONUS, type2, val);
  3990. break;
  3991. }
  3992. pc_bonus_itembonus(sd->skillcastrate, type2, val, true);
  3993. break;
  3994. #endif
  3995. case SP_SKILL_USE_SP: // bonus2 bSkillUseSP,sk,n;
  3996. if(sd->state.lr_flag == 2)
  3997. break;
  3998. if (sd->skillusesp.size() == MAX_PC_BONUS) {
  3999. ShowWarning("pc_bonus2: SP_SKILL_USE_SP: Reached max (%d) number of skills per character, bonus skill %d (%d) lost.\n", MAX_PC_BONUS, type2, val);
  4000. break;
  4001. }
  4002. pc_bonus_itembonus(sd->skillusesp, type2, val, false);
  4003. break;
  4004. case SP_SUB_SKILL: // bonus2 bSubSkill,sk,n;
  4005. if (sd->subskill.size() == MAX_PC_BONUS) {
  4006. ShowWarning("pc_bonus2: SP_SUB_SKILL: Reached max (%d) number of skills per character, bonus skill %d (%d) lost.\n", MAX_PC_BONUS, type2, val);
  4007. break;
  4008. }
  4009. pc_bonus_itembonus(sd->subskill, type2, val, false);
  4010. break;
  4011. case SP_SUBDEF_ELE: // bonus2 bSubDefEle,e,x;
  4012. PC_BONUS_CHK_ELEMENT(type2,SP_SUBDEF_ELE);
  4013. sd->indexed_bonus.subdefele[type2] += val;
  4014. break;
  4015. case SP_COMA_CLASS: // bonus2 bComaClass,c,n;
  4016. PC_BONUS_CHK_CLASS(type2,SP_COMA_CLASS);
  4017. sd->indexed_bonus.coma_class[type2] += val;
  4018. sd->special_state.bonus_coma = 1;
  4019. break;
  4020. case SP_COMA_RACE: // bonus2 bComaRace,r,n;
  4021. PC_BONUS_CHK_RACE(type2,SP_COMA_RACE);
  4022. sd->indexed_bonus.coma_race[type2] += val;
  4023. sd->special_state.bonus_coma = 1;
  4024. break;
  4025. case SP_MAGIC_ADDRACE2: // bonus2 bMagicAddRace2,mr,n;
  4026. PC_BONUS_CHK_RACE2(type2, SP_MAGIC_ADDRACE2);
  4027. if(sd->state.lr_flag != 2)
  4028. sd->indexed_bonus.magic_addrace2[type2] += val;
  4029. break;
  4030. case SP_IGNORE_MDEF_RACE2_RATE: //bonus2 bIgnoreMdefRace2Rate,mr,n;
  4031. PC_BONUS_CHK_RACE2(type2, SP_IGNORE_MDEF_RACE2);
  4032. if (sd->state.lr_flag != 2)
  4033. sd->indexed_bonus.ignore_mdef_by_race2[type2] += val;
  4034. break;
  4035. case SP_DROP_ADDRACE: // bonus2 bDropAddRace,r,x;
  4036. PC_BONUS_CHK_RACE(type2, SP_DROP_ADDRACE);
  4037. if (sd->state.lr_flag != 2)
  4038. sd->indexed_bonus.dropaddrace[type2] += val;
  4039. break;
  4040. case SP_DROP_ADDCLASS: // bonus2 bDropAddClass,c,x;
  4041. PC_BONUS_CHK_CLASS(type2, SP_DROP_ADDCLASS);
  4042. if (sd->state.lr_flag != 2)
  4043. sd->indexed_bonus.dropaddclass[type2] += val;
  4044. break;
  4045. case SP_MAGIC_SUBDEF_ELE: // bonus2 bMagicSubDefEle,e,x;
  4046. PC_BONUS_CHK_ELEMENT(type2, SP_MAGIC_SUBDEF_ELE);
  4047. sd->indexed_bonus.magic_subdefele[type2] += val;
  4048. break;
  4049. case SP_ADD_ITEM_SPHEAL_RATE: // bonus2 bAddItemSPHealRate,iid,n;
  4050. if( sd->state.lr_flag == 2 ){
  4051. break;
  4052. }
  4053. if( !item_db.exists( type2 ) ){
  4054. ShowWarning( "pc_bonus2: SP_ADD_ITEM_SPHEAL_RATE Invalid item with id %d\n", type2 );
  4055. break;
  4056. }
  4057. if( sd->itemsphealrate.size() == MAX_PC_BONUS ){
  4058. ShowWarning( "pc_bonus2: SP_ADD_ITEM_SPHEAL_RATE: Reached max (%d) number of item SP heal bonuses per character!\n", MAX_PC_BONUS );
  4059. break;
  4060. }
  4061. pc_bonus_itembonus( sd->itemsphealrate, type2, val, false );
  4062. break;
  4063. case SP_ADD_ITEMGROUP_SPHEAL_RATE: // bonus2 bAddItemGroupSPHealRate,ig,n;
  4064. if( sd->state.lr_flag == 2 ){
  4065. break;
  4066. }
  4067. if( !type2 || !itemdb_group.exists( type2 ) ){
  4068. ShowWarning( "pc_bonus2: SP_ADD_ITEMGROUP_SPHEAL_RATE: Invalid item group with id %d\n", type2 );
  4069. break;
  4070. }
  4071. if( sd->itemgroupsphealrate.size() == MAX_PC_BONUS ){
  4072. ShowWarning( "pc_bonus2: SP_ADD_ITEMGROUP_SPHEAL_RATE: Reached max (%d) number of item SP heal bonuses per character!\n", MAX_PC_BONUS );
  4073. break;
  4074. }
  4075. pc_bonus_itembonus( sd->itemgroupsphealrate, type2, val, false );
  4076. break;
  4077. default:
  4078. if (current_equip_combo_pos > 0) {
  4079. ShowWarning("pc_bonus2: unknown bonus type %d %d %d in a combo with item #%u\n", type, type2, val, sd->inventory_data[pc_checkequip( sd, current_equip_combo_pos )]->nameid);
  4080. }
  4081. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  4082. ShowWarning("pc_bonus2: unknown bonus type %d %d %d in item #%u\n", type, type2, val, current_equip_card_id ? current_equip_card_id : sd->inventory_data[current_equip_item_index]->nameid);
  4083. }
  4084. else {
  4085. ShowWarning("pc_bonus2: unknown bonus type %d %d %d in unknown usage. Report this!\n", type, type2, val);
  4086. }
  4087. break;
  4088. }
  4089. }
  4090. /**
  4091. * Gives item bonus to player for format: bonus3 bBonusName,type2,type3,val;
  4092. * @param sd
  4093. * @param type Bonus type used by bBonusName
  4094. * @param type2
  4095. * @param type3
  4096. * @param val Value that usually for rate or fixed value
  4097. */
  4098. void pc_bonus3(struct map_session_data *sd,int type,int type2,int type3,int val)
  4099. {
  4100. nullpo_retv(sd);
  4101. switch(type){
  4102. case SP_ADD_MONSTER_DROP_ITEM: // bonus3 bAddMonsterDropItem,iid,r,n;
  4103. if(sd->state.lr_flag != 2)
  4104. pc_bonus_item_drop(sd->add_drop, type2, 0, CLASS_NONE, type3, val);
  4105. break;
  4106. case SP_ADD_MONSTER_ID_DROP_ITEM: // bonus3 bAddMonsterIdDropItem,iid,mid,n;
  4107. if(sd->state.lr_flag != 2)
  4108. pc_bonus_item_drop(sd->add_drop, type2, 0, CLASS_NONE, -type3, val);
  4109. break;
  4110. case SP_ADD_CLASS_DROP_ITEM: // bonus3 bAddClassDropItem,iid,c,n;
  4111. if(sd->state.lr_flag != 2)
  4112. pc_bonus_item_drop(sd->add_drop, type2, 0, type3, RC_NONE_, val);
  4113. break;
  4114. case SP_AUTOSPELL: // bonus3 bAutoSpell,sk,y,n;
  4115. if(sd->state.lr_flag != 2)
  4116. {
  4117. int target = skill_get_inf(type2); //Support or Self (non-auto-target) skills should pick self.
  4118. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !skill_get_inf2(type2, INF2_NOTARGETSELF));
  4119. pc_bonus_autospell(sd->autospell, type2, type3, val, 0, current_equip_card_id, target ? AUTOSPELL_FORCE_SELF : AUTOSPELL_FORCE_TARGET);
  4120. }
  4121. break;
  4122. case SP_AUTOSPELL_WHENHIT: // bonus3 bAutoSpellWhenHit,sk,y,n;
  4123. if(sd->state.lr_flag != 2)
  4124. {
  4125. int target = skill_get_inf(type2); //Support or Self (non-auto-target) skills should pick self.
  4126. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !skill_get_inf2(type2, INF2_NOTARGETSELF));
  4127. pc_bonus_autospell(sd->autospell2, type2, type3, val, BF_NORMAL|BF_SKILL, current_equip_card_id, target ? AUTOSPELL_FORCE_SELF : AUTOSPELL_FORCE_TARGET);
  4128. }
  4129. break;
  4130. case SP_ADD_MONSTER_DROP_ITEMGROUP: // bonus3 bAddMonsterDropItemGroup,ig,r,n;
  4131. if (sd->state.lr_flag != 2)
  4132. pc_bonus_item_drop(sd->add_drop, 0, type2, CLASS_NONE, type3, val);
  4133. break;
  4134. case SP_ADD_CLASS_DROP_ITEMGROUP: // bonus3 bAddClassDropItemGroup,ig,c,n;
  4135. if (sd->state.lr_flag != 2)
  4136. pc_bonus_item_drop(sd->add_drop, 0, type2, type3, RC_NONE_, val);
  4137. break;
  4138. case SP_ADDEFF: // bonus3 bAddEff,eff,n,y;
  4139. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  4140. pc_bonus_addeff(sd->addeff, (sc_type)type2, sd->state.lr_flag != 2 ? type3 : 0, sd->state.lr_flag == 2 ? type3 : 0, val, 0);
  4141. break;
  4142. case SP_ADDEFF_WHENHIT: // bonus3 bAddEffWhenHit,eff,n,y;
  4143. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  4144. if(sd->state.lr_flag != 2)
  4145. pc_bonus_addeff(sd->addeff_atked, (sc_type)type2, type3, 0, val, 0);
  4146. break;
  4147. case SP_ADDEFF_ONSKILL: // bonus3 bAddEffOnSkill,sk,eff,n;
  4148. PC_BONUS_CHK_SC(type2,SP_ADDEFF_ONSKILL);
  4149. if( sd->state.lr_flag != 2 )
  4150. pc_bonus_addeff_onskill(sd->addeff_onskill, (sc_type)type3, val, type2, ATF_TARGET, 0);
  4151. break;
  4152. case SP_ADDELE: // bonus3 bAddEle,e,x,bf;
  4153. PC_BONUS_CHK_ELEMENT(type2,SP_ADDELE);
  4154. if (sd->state.lr_flag != 2)
  4155. pc_bonus_addele(sd, (unsigned char)type2, type3, val);
  4156. break;
  4157. case SP_SUBELE: // bonus3 bSubEle,e,x,bf;
  4158. PC_BONUS_CHK_ELEMENT(type2,SP_SUBELE);
  4159. if (sd->state.lr_flag != 2)
  4160. pc_bonus_subele(sd, (unsigned char)type2, type3, val);
  4161. break;
  4162. case SP_SUBRACE: // bonus3 bSubRace,r,x,bf;
  4163. PC_BONUS_CHK_RACE(type2, SP_SUBRACE);
  4164. if (sd->state.lr_flag != 2)
  4165. pc_bonus_subrace(sd, (unsigned char)type2, type3, val);
  4166. break;
  4167. case SP_SP_VANISH_RACE_RATE: // bonus3 bSPVanishRaceRate,r,x,n;
  4168. PC_BONUS_CHK_RACE(type2,SP_SP_VANISH_RACE_RATE);
  4169. if(sd->state.lr_flag != 2) {
  4170. sd->sp_vanish_race[type2].rate += type3;
  4171. sd->sp_vanish_race[type2].per += val;
  4172. }
  4173. break;
  4174. case SP_HP_VANISH_RACE_RATE: // bonus3 bHPVanishRaceRate,r,x,n;
  4175. PC_BONUS_CHK_RACE(type2,SP_HP_VANISH_RACE_RATE);
  4176. if(sd->state.lr_flag != 2) {
  4177. sd->hp_vanish_race[type2].rate += type3;
  4178. sd->hp_vanish_race[type2].per += val;
  4179. }
  4180. break;
  4181. case SP_SP_VANISH_RATE: // bonus3 bSPVanishRate,x,n,bf;
  4182. if(sd->state.lr_flag != 2) {
  4183. pc_bonus_addvanish(sd->sp_vanish, type2, type3, val);
  4184. }
  4185. break;
  4186. case SP_HP_VANISH_RATE: // bonus3 bHPVanishRate,x,n,bf;
  4187. if(sd->state.lr_flag != 2) {
  4188. pc_bonus_addvanish(sd->hp_vanish, type2, type3, val);
  4189. }
  4190. break;
  4191. case SP_STATE_NORECOVER_RACE: // bonus3 bStateNoRecoverRace,r,x,t;
  4192. PC_BONUS_CHK_RACE(type2, SP_STATE_NORECOVER_RACE);
  4193. if (sd->state.lr_flag == 2)
  4194. break;
  4195. //! CONFIRM: Is it not stackable? Does not check max or min value?
  4196. //if (type3 > sd->norecover_state_race[type2].rate) {
  4197. // sd->norecover_state_race[type2].rate = type3;
  4198. // sd->norecover_state_race[type2].tick = val;
  4199. // break;
  4200. //}
  4201. sd->norecover_state_race[type2].rate = type3;
  4202. sd->norecover_state_race[type2].tick = val;
  4203. break;
  4204. default:
  4205. if (current_equip_combo_pos > 0) {
  4206. ShowWarning("pc_bonus3: unknown bonus type %d %d %d %d in a combo with item #%u\n", type, type2, type3, val, sd->inventory_data[pc_checkequip( sd, current_equip_combo_pos )]->nameid);
  4207. }
  4208. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  4209. ShowWarning("pc_bonus3: unknown bonus type %d %d %d %d in item #%u\n", type, type2, type3, val, current_equip_card_id ? current_equip_card_id : sd->inventory_data[current_equip_item_index]->nameid);
  4210. }
  4211. else {
  4212. ShowWarning("pc_bonus3: unknown bonus type %d %d %d %d in unknown usage. Report this!\n", type, type2, type3, val);
  4213. }
  4214. break;
  4215. }
  4216. }
  4217. /**
  4218. * Gives item bonus to player for format: bonus4 bBonusName,type2,type3,type4,val;
  4219. * @param sd
  4220. * @param type Bonus type used by bBonusName
  4221. * @param type2
  4222. * @param type3
  4223. * @param type4
  4224. * @param val Value that usually for rate or fixed value
  4225. */
  4226. void pc_bonus4(struct map_session_data *sd,int type,int type2,int type3,int type4,int val)
  4227. {
  4228. nullpo_retv(sd);
  4229. switch(type){
  4230. case SP_AUTOSPELL: // bonus4 bAutoSpell,sk,y,n,i;
  4231. if(sd->state.lr_flag != 2)
  4232. pc_bonus_autospell(sd->autospell, type2, type3, type4, 0, current_equip_card_id, val & AUTOSPELL_FORCE_ALL);
  4233. break;
  4234. case SP_AUTOSPELL_WHENHIT: // bonus4 bAutoSpellWhenHit,sk,y,n,i;
  4235. if(sd->state.lr_flag != 2)
  4236. pc_bonus_autospell(sd->autospell2, type2, type3, type4, BF_NORMAL|BF_SKILL, current_equip_card_id, val & AUTOSPELL_FORCE_ALL);
  4237. break;
  4238. case SP_AUTOSPELL_ONSKILL: // bonus4 bAutoSpellOnSkill,sk,x,y,n;
  4239. if(sd->state.lr_flag != 2)
  4240. {
  4241. int target = skill_get_inf(type3); //Support or Self (non-auto-target) skills should pick self.
  4242. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !skill_get_inf2(type3, INF2_NOTARGETSELF));
  4243. pc_bonus_autospell_onskill(sd->autospell3, type2, type3, type4, val, current_equip_card_id, target ? AUTOSPELL_FORCE_TARGET : AUTOSPELL_FORCE_SELF);
  4244. }
  4245. break;
  4246. case SP_ADDEFF: // bonus4 bAddEff,eff,n,y,t;
  4247. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  4248. pc_bonus_addeff(sd->addeff, (sc_type)type2, sd->state.lr_flag != 2 ? type3 : 0, sd->state.lr_flag == 2 ? type3 : 0, type4, val);
  4249. break;
  4250. case SP_ADDEFF_WHENHIT: // bonus4 bAddEffWhenHit,eff,n,y,t;
  4251. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  4252. if (sd->state.lr_flag != 2)
  4253. pc_bonus_addeff(sd->addeff_atked, (sc_type)type2, type3, 0, type4, val);
  4254. break;
  4255. case SP_ADDEFF_ONSKILL: // bonus4 bAddEffOnSkill,sk,eff,n,y;
  4256. PC_BONUS_CHK_SC(type3,SP_ADDEFF_ONSKILL);
  4257. if( sd->state.lr_flag != 2 )
  4258. pc_bonus_addeff_onskill(sd->addeff_onskill, (sc_type)type3, type4, type2, val, 0);
  4259. break;
  4260. case SP_SET_DEF_RACE: // bonus4 bSetDefRace,r,n,t,y;
  4261. PC_BONUS_CHK_RACE(type2,SP_SET_DEF_RACE);
  4262. if(sd->state.lr_flag == 2)
  4263. break;
  4264. sd->def_set_race[type2].rate = type3;
  4265. sd->def_set_race[type2].tick = type4;
  4266. sd->def_set_race[type2].value = val;
  4267. break;
  4268. case SP_SET_MDEF_RACE: // bonus4 bSetMDefRace,r,n,t,y;
  4269. PC_BONUS_CHK_RACE(type2,SP_SET_MDEF_RACE);
  4270. if(sd->state.lr_flag == 2)
  4271. break;
  4272. sd->mdef_set_race[type2].rate = type3;
  4273. sd->mdef_set_race[type2].tick = type4;
  4274. sd->mdef_set_race[type2].value = val;
  4275. break;
  4276. default:
  4277. if (current_equip_combo_pos > 0) {
  4278. ShowWarning("pc_bonus4: unknown bonus type %d %d %d %d %d in a combo with item #%u\n", type, type2, type3, type4, val, sd->inventory_data[pc_checkequip( sd, current_equip_combo_pos )]->nameid);
  4279. }
  4280. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  4281. ShowWarning("pc_bonus4: unknown bonus type %d %d %d %d %d in item #%u\n", type, type2, type3, type4, val, current_equip_card_id ? current_equip_card_id : sd->inventory_data[current_equip_item_index]->nameid);
  4282. }
  4283. else {
  4284. ShowWarning("pc_bonus4: unknown bonus type %d %d %d %d %d in unknown usage. Report this!\n", type, type2, type3, type4, val);
  4285. }
  4286. break;
  4287. }
  4288. }
  4289. /**
  4290. * Gives item bonus to player for format: bonus5 bBonusName,type2,type3,type4,val;
  4291. * @param sd
  4292. * @param type Bonus type used by bBonusName
  4293. * @param type2
  4294. * @param type3
  4295. * @param type4
  4296. * @param val Value that usually for rate or fixed value
  4297. */
  4298. void pc_bonus5(struct map_session_data *sd,int type,int type2,int type3,int type4,int type5,int val)
  4299. {
  4300. nullpo_retv(sd);
  4301. switch(type){
  4302. case SP_AUTOSPELL: // bonus5 bAutoSpell,sk,y,n,bf,i;
  4303. if(sd->state.lr_flag != 2)
  4304. pc_bonus_autospell(sd->autospell, type2, type3, type4, type5, current_equip_card_id, val & AUTOSPELL_FORCE_ALL);
  4305. break;
  4306. case SP_AUTOSPELL_WHENHIT: // bonus5 bAutoSpellWhenHit,sk,y,n,bf,i;
  4307. if(sd->state.lr_flag != 2)
  4308. pc_bonus_autospell(sd->autospell2, type2, type3, type4, type5, current_equip_card_id, val & AUTOSPELL_FORCE_ALL);
  4309. break;
  4310. case SP_AUTOSPELL_ONSKILL: // bonus5 bAutoSpellOnSkill,sk,x,y,n,i;
  4311. if(sd->state.lr_flag != 2)
  4312. pc_bonus_autospell_onskill(sd->autospell3, type2, type3, type4, type5, current_equip_card_id, val & AUTOSPELL_FORCE_ALL);
  4313. break;
  4314. case SP_ADDEFF_ONSKILL: // bonus5 bAddEffOnSkill,sk,eff,n,y,t;
  4315. PC_BONUS_CHK_SC(type3,SP_ADDEFF_ONSKILL);
  4316. if( sd->state.lr_flag != 2 )
  4317. pc_bonus_addeff_onskill(sd->addeff_onskill, (sc_type)type3, type4, type2, type5, val);
  4318. break;
  4319. default:
  4320. if (current_equip_combo_pos > 0) {
  4321. ShowWarning("pc_bonus5: unknown bonus type %d %d %d %d %d %d in a combo with item #%u\n", type, type2, type3, type4, type5, val, sd->inventory_data[pc_checkequip( sd, current_equip_combo_pos )]->nameid);
  4322. }
  4323. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  4324. ShowWarning("pc_bonus5: unknown bonus type %d %d %d %d %d %d in item #%u\n", type, type2, type3, type4, type5, val, current_equip_card_id ? current_equip_card_id : sd->inventory_data[current_equip_item_index]->nameid);
  4325. }
  4326. else {
  4327. ShowWarning("pc_bonus5: unknown bonus type %d %d %d %d %d %d in unknown usage. Report this!\n", type, type2, type3, type4, type5, val);
  4328. }
  4329. break;
  4330. }
  4331. }
  4332. /*==========================================
  4333. * Grants a player a given skill. Flag values are:
  4334. * 0 - Grant permanent skill to be bound to skill tree
  4335. * 1 - Grant an item skill (temporary)
  4336. * 2 - Like 1, except the level granted can stack with previously learned level.
  4337. * 4 - Like 0, except the skill will ignore skill tree (saves through job changes and resets).
  4338. *------------------------------------------*/
  4339. bool pc_skill(struct map_session_data* sd, uint16 skill_id, int level, enum e_addskill_type type) {
  4340. uint16 idx = 0;
  4341. nullpo_ret(sd);
  4342. if (!skill_id || !(idx = skill_get_index(skill_id))) {
  4343. ShowError("pc_skill: Skill with id %d does not exist in the skill database\n", skill_id);
  4344. return false;
  4345. }
  4346. if (level > MAX_SKILL_LEVEL) {
  4347. ShowError("pc_skill: Skill level %d too high. Max lv supported is %d\n", level, MAX_SKILL_LEVEL);
  4348. return false;
  4349. }
  4350. if (type == ADDSKILL_TEMP_ADDLEVEL && sd->status.skill[idx].lv + level > MAX_SKILL_LEVEL) {
  4351. 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);
  4352. level = MAX_SKILL_LEVEL - sd->status.skill[idx].lv;
  4353. }
  4354. switch (type) {
  4355. case ADDSKILL_PERMANENT: //Set skill data overwriting whatever was there before.
  4356. sd->status.skill[idx].id = skill_id;
  4357. sd->status.skill[idx].lv = level;
  4358. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  4359. if (level == 0) { //Remove skill.
  4360. sd->status.skill[idx].id = 0;
  4361. clif_deleteskill(sd,skill_id);
  4362. } else
  4363. clif_addskill(sd,skill_id);
  4364. if (!skill_get_inf(skill_id) || pc_checkskill_summoner(sd, SUMMONER_POWER_LAND) >= 20 || pc_checkskill_summoner(sd, SUMMONER_POWER_SEA) >= 20) //Only recalculate for passive skills.
  4365. status_calc_pc(sd, SCO_NONE);
  4366. break;
  4367. case ADDSKILL_TEMP: //Item bonus skill.
  4368. if (sd->status.skill[idx].id != 0) {
  4369. if (sd->status.skill[idx].lv >= level)
  4370. return true;
  4371. if (sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT) //Non-granted skill, store it's level.
  4372. sd->status.skill[idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[idx].lv;
  4373. } else {
  4374. sd->status.skill[idx].id = skill_id;
  4375. sd->status.skill[idx].flag = SKILL_FLAG_TEMPORARY;
  4376. }
  4377. sd->status.skill[idx].lv = level;
  4378. break;
  4379. case ADDSKILL_TEMP_ADDLEVEL: //Add skill bonus on top of what you had.
  4380. if (sd->status.skill[idx].id != 0) {
  4381. if (sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT)
  4382. sd->status.skill[idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[idx].lv; // Store previous level.
  4383. } else {
  4384. sd->status.skill[idx].id = skill_id;
  4385. sd->status.skill[idx].flag = SKILL_FLAG_TEMPORARY; //Set that this is a bonus skill.
  4386. }
  4387. sd->status.skill[idx].lv += level;
  4388. break;
  4389. case ADDSKILL_PERMANENT_GRANTED: //Permanent granted skills ignore the skill tree
  4390. sd->status.skill[idx].id = skill_id;
  4391. sd->status.skill[idx].lv = level;
  4392. sd->status.skill[idx].flag = SKILL_FLAG_PERM_GRANTED;
  4393. if (level == 0) { //Remove skill.
  4394. sd->status.skill[idx].id = 0;
  4395. clif_deleteskill(sd,skill_id);
  4396. } else
  4397. clif_addskill(sd,skill_id);
  4398. if (!skill_get_inf(skill_id) || pc_checkskill_summoner(sd, SUMMONER_POWER_LAND) >= 20 || pc_checkskill_summoner(sd, SUMMONER_POWER_SEA) >= 20) //Only recalculate for passive skills.
  4399. status_calc_pc(sd, SCO_NONE);
  4400. break;
  4401. default:
  4402. return false;
  4403. }
  4404. return true;
  4405. }
  4406. /*==========================================
  4407. * Append a card to an item ?
  4408. *------------------------------------------*/
  4409. int pc_insert_card(struct map_session_data* sd, int idx_card, int idx_equip)
  4410. {
  4411. nullpo_ret(sd);
  4412. if (idx_equip < 0 || idx_equip >= MAX_INVENTORY) {
  4413. return 0;
  4414. }
  4415. if (idx_card < 0 || idx_card >= MAX_INVENTORY) {
  4416. return 0;
  4417. }
  4418. int i;
  4419. t_itemid nameid;
  4420. struct item_data* item_eq = sd->inventory_data[idx_equip];
  4421. struct item_data* item_card = sd->inventory_data[idx_card];
  4422. if(item_eq == nullptr)
  4423. return 0; //Invalid item index.
  4424. if(item_card == nullptr)
  4425. return 0; //Invalid card index.
  4426. if( sd->inventory.u.items_inventory[idx_equip].nameid == 0 || sd->inventory.u.items_inventory[idx_equip].amount < 1 )
  4427. return 0; // target item missing
  4428. if( sd->inventory.u.items_inventory[idx_card].nameid == 0 || sd->inventory.u.items_inventory[idx_card].amount < 1 )
  4429. return 0; // target card missing
  4430. if( item_eq->type != IT_WEAPON && item_eq->type != IT_ARMOR )
  4431. return 0; // only weapons and armor are allowed
  4432. if( item_card->type != IT_CARD )
  4433. return 0; // must be a card
  4434. if( sd->inventory.u.items_inventory[idx_equip].identify == 0 )
  4435. return 0; // target must be identified
  4436. if( itemdb_isspecial(sd->inventory.u.items_inventory[idx_equip].card[0]) )
  4437. return 0; // card slots reserved for other purposes
  4438. if( (item_eq->equip & item_card->equip) == 0 )
  4439. return 0; // card cannot be compounded on this item type
  4440. if( item_eq->type == IT_WEAPON && item_card->equip == EQP_SHIELD )
  4441. return 0; // attempted to place shield card on left-hand weapon.
  4442. if( item_eq->type == IT_ARMOR && (item_card->equip & EQP_ACC) && ((item_card->equip & EQP_ACC) != EQP_ACC) && ((item_eq->equip & EQP_ACC) != (item_card->equip & EQP_ACC)) )
  4443. return 0; // specific accessory-card can only be inserted to specific accessory.
  4444. if( sd->inventory.u.items_inventory[idx_equip].equip != 0 )
  4445. return 0; // item must be unequipped
  4446. ARR_FIND( 0, item_eq->slots, i, sd->inventory.u.items_inventory[idx_equip].card[i] == 0 );
  4447. if( i == item_eq->slots )
  4448. return 0; // no free slots
  4449. // remember the card id to insert
  4450. nameid = sd->inventory.u.items_inventory[idx_card].nameid;
  4451. if( pc_delitem(sd,idx_card,1,1,0,LOG_TYPE_OTHER) == 1 )
  4452. {// failed
  4453. clif_insert_card(sd,idx_equip,idx_card,1);
  4454. }
  4455. else
  4456. {// success
  4457. log_pick_pc(sd, LOG_TYPE_OTHER, -1, &sd->inventory.u.items_inventory[idx_equip]);
  4458. sd->inventory.u.items_inventory[idx_equip].card[i] = nameid;
  4459. log_pick_pc(sd, LOG_TYPE_OTHER, 1, &sd->inventory.u.items_inventory[idx_equip]);
  4460. clif_insert_card(sd,idx_equip,idx_card,0);
  4461. }
  4462. return 0;
  4463. }
  4464. /**
  4465. * Returns the count of unidentified items with the option to identify too.
  4466. * @param sd: Player data
  4467. * @param identify_item: Whether or not to identify any unidentified items
  4468. * @return Unidentified items count
  4469. */
  4470. int pc_identifyall(struct map_session_data *sd, bool identify_item)
  4471. {
  4472. int unidentified_count = 0;
  4473. for (int i = 0; i < MAX_INVENTORY; i++) {
  4474. if (sd->inventory.u.items_inventory[i].nameid > 0 && sd->inventory.u.items_inventory[i].identify != 1) {
  4475. if (identify_item == true) {
  4476. sd->inventory.u.items_inventory[i].identify = 1;
  4477. clif_item_identified(sd,i,0);
  4478. }
  4479. unidentified_count++;
  4480. }
  4481. }
  4482. return unidentified_count;
  4483. }
  4484. //
  4485. // Items
  4486. //
  4487. /*==========================================
  4488. * Update buying value by skills
  4489. *------------------------------------------*/
  4490. int pc_modifybuyvalue(struct map_session_data *sd,int orig_value)
  4491. {
  4492. int skill,val = orig_value,rate1 = 0,rate2 = 0;
  4493. if((skill=pc_checkskill(sd,MC_DISCOUNT))>0) // merchant discount
  4494. rate1 = 5+skill*2-((skill==10)? 1:0);
  4495. if((skill=pc_checkskill(sd,RG_COMPULSION))>0) // rogue discount
  4496. rate2 = 5+skill*4;
  4497. if(rate1 < rate2) rate1 = rate2;
  4498. if(rate1)
  4499. val = (int)((double)orig_value*(double)(100-rate1)/100.);
  4500. if(val < battle_config.min_shop_buy)
  4501. val = battle_config.min_shop_buy;
  4502. return val;
  4503. }
  4504. /*==========================================
  4505. * Update selling value by skills
  4506. *------------------------------------------*/
  4507. int pc_modifysellvalue(struct map_session_data *sd,int orig_value)
  4508. {
  4509. int skill,val = orig_value,rate = 0;
  4510. if((skill=pc_checkskill(sd,MC_OVERCHARGE))>0) //OverCharge
  4511. rate = 5+skill*2-((skill==10)? 1:0);
  4512. if(rate)
  4513. val = (int)((double)orig_value*(double)(100+rate)/100.);
  4514. if (val < battle_config.min_shop_sell)
  4515. val = battle_config.min_shop_sell;
  4516. return val;
  4517. }
  4518. /*==========================================
  4519. * Checking if we have enough place on inventory for new item
  4520. * Make sure to take 30k as limit (for client I guess)
  4521. * @param sd
  4522. * @param nameid
  4523. * @param amount
  4524. * @return e_chkitem_result
  4525. *------------------------------------------*/
  4526. char pc_checkadditem(struct map_session_data *sd, t_itemid nameid, int amount)
  4527. {
  4528. int i;
  4529. struct item_data* data;
  4530. nullpo_ret(sd);
  4531. if(amount > MAX_AMOUNT)
  4532. return CHKADDITEM_OVERAMOUNT;
  4533. data = itemdb_search(nameid);
  4534. if(!itemdb_isstackable2(data))
  4535. return CHKADDITEM_NEW;
  4536. if( data->stack.inventory && amount > data->stack.amount )
  4537. return CHKADDITEM_OVERAMOUNT;
  4538. if (data->flag.guid)
  4539. return CHKADDITEM_NEW;
  4540. for(i=0;i<MAX_INVENTORY;i++){
  4541. // FIXME: This does not consider the checked item's cards, thus could check a wrong slot for stackability.
  4542. if(sd->inventory.u.items_inventory[i].nameid == nameid){
  4543. 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 ) )
  4544. return CHKADDITEM_OVERAMOUNT;
  4545. // If the item is in the inventory already, but the player is not allowed to use that many slots anymore
  4546. if( i >= sd->status.inventory_slots ){
  4547. return CHKADDITEM_OVERAMOUNT;
  4548. }
  4549. return CHKADDITEM_EXIST;
  4550. }
  4551. }
  4552. return CHKADDITEM_NEW;
  4553. }
  4554. /*==========================================
  4555. * Return number of available place in inventory
  4556. * Each non stackable item will reduce place by 1
  4557. * @param sd
  4558. * @return Number of empty slots
  4559. *------------------------------------------*/
  4560. uint8 pc_inventoryblank(struct map_session_data *sd)
  4561. {
  4562. uint16 i;
  4563. uint8 b;
  4564. nullpo_ret(sd);
  4565. for(i = 0, b = 0; i < sd->status.inventory_slots; i++){
  4566. if(sd->inventory.u.items_inventory[i].nameid == 0)
  4567. b++;
  4568. }
  4569. return b;
  4570. }
  4571. /**
  4572. * Attempts to remove zeny from player
  4573. * @param sd: Player
  4574. * @param zeny: Zeny removed
  4575. * @param type: Log type
  4576. * @param tsd: (optional) From who to log (if null take sd)
  4577. * @return 0: Success, 1: Failed (Removing negative Zeny or not enough Zeny), 2: Player not found
  4578. */
  4579. char pc_payzeny(struct map_session_data *sd, int zeny, enum e_log_pick_type type, struct map_session_data *tsd)
  4580. {
  4581. nullpo_retr(2,sd);
  4582. zeny = cap_value(zeny,-MAX_ZENY,MAX_ZENY); //prevent command UB
  4583. if( zeny < 0 )
  4584. {
  4585. ShowError("pc_payzeny: Paying negative Zeny (zeny=%d, account_id=%d, char_id=%d).\n", zeny, sd->status.account_id, sd->status.char_id);
  4586. return 1;
  4587. }
  4588. if( sd->status.zeny < zeny )
  4589. return 1; //Not enough.
  4590. sd->status.zeny -= zeny;
  4591. clif_updatestatus(sd,SP_ZENY);
  4592. if(!tsd) tsd = sd;
  4593. log_zeny(sd, type, tsd, -zeny);
  4594. if( zeny > 0 && sd->state.showzeny ) {
  4595. char output[255];
  4596. sprintf(output, "Removed %dz.", zeny);
  4597. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4598. }
  4599. return 0;
  4600. }
  4601. /**
  4602. * Attempts to give zeny to player
  4603. * @param sd: Player
  4604. * @param type: Log type
  4605. * @param tsd: (optional) From who to log (if null take sd)
  4606. * @return -1: Player not found, 0: Success, 1: Giving negative Zeny
  4607. */
  4608. char pc_getzeny(struct map_session_data *sd, int zeny, enum e_log_pick_type type, struct map_session_data *tsd)
  4609. {
  4610. nullpo_retr(-1,sd);
  4611. zeny = cap_value(zeny,-MAX_ZENY,MAX_ZENY); //prevent command UB
  4612. if( zeny < 0 )
  4613. {
  4614. ShowError("pc_getzeny: Obtaining negative Zeny (zeny=%d, account_id=%d, char_id=%d).\n", zeny, sd->status.account_id, sd->status.char_id);
  4615. return 1;
  4616. }
  4617. if( zeny > MAX_ZENY - sd->status.zeny )
  4618. zeny = MAX_ZENY - sd->status.zeny;
  4619. sd->status.zeny += zeny;
  4620. clif_updatestatus(sd,SP_ZENY);
  4621. if(!tsd) tsd = sd;
  4622. log_zeny(sd, type, tsd, zeny);
  4623. if( zeny > 0 && sd->state.showzeny ) {
  4624. char output[255];
  4625. sprintf(output, "Gained %dz.", zeny);
  4626. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4627. }
  4628. achievement_update_objective(sd, AG_GET_ZENY, 1, sd->status.zeny);
  4629. return 0;
  4630. }
  4631. /**
  4632. * Attempts to remove Cash Points from player
  4633. * @param sd: Player
  4634. * @param price: Total points (cash + kafra) the player has to pay
  4635. * @param points: Kafra points the player has to pay
  4636. * @param type: Log type
  4637. * @return -1: Not enough points, otherwise success (cash+points)
  4638. */
  4639. int pc_paycash(struct map_session_data *sd, int price, int points, e_log_pick_type type)
  4640. {
  4641. int cash;
  4642. nullpo_retr(-1,sd);
  4643. points = cap_value(points, 0, MAX_ZENY); //prevent command UB
  4644. cash = price-points;
  4645. if( sd->cashPoints < cash || sd->kafraPoints < points )
  4646. {
  4647. 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);
  4648. return -1;
  4649. }
  4650. if( cash ){
  4651. pc_setaccountreg(sd, add_str(CASHPOINT_VAR), sd->cashPoints - cash);
  4652. sd->cashPoints -= cash;
  4653. log_cash( sd, type, LOG_CASH_TYPE_CASH, -cash );
  4654. }
  4655. if( points ){
  4656. pc_setaccountreg(sd, add_str(KAFRAPOINT_VAR), sd->kafraPoints - points);
  4657. sd->kafraPoints -= points;
  4658. log_cash( sd, type, LOG_CASH_TYPE_KAFRA, -points );
  4659. }
  4660. if( battle_config.cashshop_show_points )
  4661. {
  4662. char output[CHAT_SIZE_MAX];
  4663. 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.
  4664. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4665. }
  4666. return cash+points;
  4667. }
  4668. /**
  4669. * Attempts to give Cash Points to player
  4670. * @param sd: Player
  4671. * @param cash: Cash points the player gets
  4672. * @param points: Kafra points the player gets
  4673. * @param type: Log type
  4674. * @return -1: Error, otherwise success (cash or points)
  4675. */
  4676. int pc_getcash(struct map_session_data *sd, int cash, int points, e_log_pick_type type)
  4677. {
  4678. char output[CHAT_SIZE_MAX];
  4679. nullpo_retr(-1,sd);
  4680. cash = cap_value(cash, 0, MAX_ZENY); //prevent command UB
  4681. points = cap_value(points, 0, MAX_ZENY); //prevent command UB
  4682. if( cash > 0 )
  4683. {
  4684. if( cash > MAX_ZENY-sd->cashPoints )
  4685. {
  4686. 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);
  4687. cash = MAX_ZENY-sd->cashPoints;
  4688. }
  4689. pc_setaccountreg(sd, add_str(CASHPOINT_VAR), sd->cashPoints+cash);
  4690. sd->cashPoints += cash;
  4691. log_cash( sd, type, LOG_CASH_TYPE_CASH, cash );
  4692. if( battle_config.cashshop_show_points )
  4693. {
  4694. sprintf(output, msg_txt(sd,505), cash, sd->cashPoints);
  4695. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4696. }
  4697. return cash;
  4698. }
  4699. if( points > 0 )
  4700. {
  4701. if( points > MAX_ZENY-sd->kafraPoints )
  4702. {
  4703. 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);
  4704. points = MAX_ZENY-sd->kafraPoints;
  4705. }
  4706. pc_setaccountreg(sd, add_str(KAFRAPOINT_VAR), sd->kafraPoints+points);
  4707. sd->kafraPoints += points;
  4708. log_cash( sd, type, LOG_CASH_TYPE_KAFRA, points );
  4709. if( battle_config.cashshop_show_points )
  4710. {
  4711. sprintf(output, msg_txt(sd,506), points, sd->kafraPoints);
  4712. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4713. }
  4714. return points;
  4715. }
  4716. return -1; //shouldn't happen but just in case
  4717. }
  4718. /**
  4719. * Searching a specified itemid in inventory and return his stored index
  4720. * @param sd Player
  4721. * @param nameid Find this Item!
  4722. * @return Stored index in inventory, or -1 if not found.
  4723. **/
  4724. short pc_search_inventory(struct map_session_data *sd, t_itemid nameid) {
  4725. short i;
  4726. nullpo_retr(-1, sd);
  4727. ARR_FIND( 0, MAX_INVENTORY, i, sd->inventory.u.items_inventory[i].nameid == nameid && (sd->inventory.u.items_inventory[i].amount > 0 || nameid == 0) );
  4728. return ( i < MAX_INVENTORY ) ? i : -1;
  4729. }
  4730. /** Attempt to add a new item to player inventory
  4731. * @param sd
  4732. * @param item_data
  4733. * @param amount
  4734. * @param log_type
  4735. * @return
  4736. * 0 = success
  4737. * 1 = invalid itemid not found or negative amount
  4738. * 2 = overweight
  4739. * 3 = ?
  4740. * 4 = no free place found
  4741. * 5 = max amount reached
  4742. * 6 = ?
  4743. * 7 = stack limitation
  4744. */
  4745. enum e_additem_result pc_additem(struct map_session_data *sd,struct item *item,int amount,e_log_pick_type log_type) {
  4746. struct item_data *id;
  4747. int16 i;
  4748. unsigned int w;
  4749. nullpo_retr(ADDITEM_INVALID, sd);
  4750. nullpo_retr(ADDITEM_INVALID, item);
  4751. if( item->nameid == 0 || amount <= 0 )
  4752. return ADDITEM_INVALID;
  4753. if( amount > MAX_AMOUNT )
  4754. return ADDITEM_OVERAMOUNT;
  4755. id = itemdb_search(item->nameid);
  4756. if( id->stack.inventory && amount > id->stack.amount )
  4757. {// item stack limitation
  4758. return ADDITEM_STACKLIMIT;
  4759. }
  4760. w = id->weight*amount;
  4761. if(sd->weight + w > sd->max_weight)
  4762. return ADDITEM_OVERWEIGHT;
  4763. if (id->flag.guid && !item->unique_id)
  4764. item->unique_id = pc_generate_unique_id(sd);
  4765. // Stackable | Non Rental
  4766. if( itemdb_isstackable2(id) && item->expire_time == 0 ) {
  4767. for( i = 0; i < MAX_INVENTORY; i++ ) {
  4768. if( sd->inventory.u.items_inventory[i].nameid == item->nameid &&
  4769. sd->inventory.u.items_inventory[i].bound == item->bound &&
  4770. sd->inventory.u.items_inventory[i].expire_time == 0 &&
  4771. sd->inventory.u.items_inventory[i].unique_id == item->unique_id &&
  4772. memcmp(&sd->inventory.u.items_inventory[i].card, &item->card, sizeof(item->card)) == 0 ) {
  4773. 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 ) )
  4774. return ADDITEM_OVERAMOUNT;
  4775. // If the item is in the inventory already, but the player is not allowed to use that many slots anymore
  4776. if( i >= sd->status.inventory_slots ){
  4777. return ADDITEM_OVERAMOUNT;
  4778. }
  4779. sd->inventory.u.items_inventory[i].amount += amount;
  4780. clif_additem(sd,i,amount,0);
  4781. break;
  4782. }
  4783. }
  4784. }else{
  4785. i = MAX_INVENTORY;
  4786. }
  4787. if (i >= MAX_INVENTORY) {
  4788. i = pc_search_inventory(sd,0);
  4789. if( i < 0 )
  4790. return ADDITEM_OVERITEM;
  4791. if( i >= sd->status.inventory_slots ){
  4792. return ADDITEM_OVERITEM;
  4793. }
  4794. memcpy(&sd->inventory.u.items_inventory[i], item, sizeof(sd->inventory.u.items_inventory[0]));
  4795. // clear equip and favorite fields first, just in case
  4796. if( item->equip )
  4797. sd->inventory.u.items_inventory[i].equip = 0;
  4798. if( item->favorite )
  4799. sd->inventory.u.items_inventory[i].favorite = 0;
  4800. if( item->equipSwitch )
  4801. sd->inventory.u.items_inventory[i].equipSwitch = 0;
  4802. sd->inventory.u.items_inventory[i].amount = amount;
  4803. sd->inventory_data[i] = id;
  4804. sd->last_addeditem_index = i;
  4805. if (!itemdb_isstackable2(id) || id->flag.guid)
  4806. sd->inventory.u.items_inventory[i].unique_id = item->unique_id ? item->unique_id : pc_generate_unique_id(sd);
  4807. clif_additem(sd,i,amount,0);
  4808. }
  4809. log_pick_pc(sd, log_type, amount, &sd->inventory.u.items_inventory[i]);
  4810. sd->weight += w;
  4811. clif_updatestatus(sd,SP_WEIGHT);
  4812. //Auto-equip
  4813. if(id->flag.autoequip)
  4814. pc_equipitem(sd, i, id->equip);
  4815. /* rental item check */
  4816. if( item->expire_time ) {
  4817. if( time(NULL) > item->expire_time ) {
  4818. clif_rental_expired(sd, i, sd->inventory.u.items_inventory[i].nameid);
  4819. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 1, 0, LOG_TYPE_OTHER);
  4820. } else {
  4821. unsigned int seconds = (unsigned int)( item->expire_time - time(NULL) );
  4822. clif_rental_time(sd, sd->inventory.u.items_inventory[i].nameid, seconds);
  4823. pc_inventory_rental_add(sd, seconds);
  4824. }
  4825. }
  4826. achievement_update_objective(sd, AG_GET_ITEM, 1, id->value_sell);
  4827. pc_show_questinfo(sd);
  4828. return ADDITEM_SUCCESS;
  4829. }
  4830. /*==========================================
  4831. * Remove an item at index n from inventory by amount.
  4832. * @param sd
  4833. * @param n Item index in inventory
  4834. * @param amount
  4835. * @param type &1: Don't notify deletion; &2 Don't notify weight change; &4 Don't calculate status
  4836. * @param reason Delete reason
  4837. * @param log_type e_log_pick_type
  4838. * @return 1 - invalid itemid or negative amount; 0 - Success
  4839. *------------------------------------------*/
  4840. char pc_delitem(struct map_session_data *sd,int n,int amount,int type, short reason, e_log_pick_type log_type)
  4841. {
  4842. nullpo_retr(1, sd);
  4843. 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)
  4844. return 1;
  4845. log_pick_pc(sd, log_type, -amount, &sd->inventory.u.items_inventory[n]);
  4846. sd->inventory.u.items_inventory[n].amount -= amount;
  4847. sd->weight -= sd->inventory_data[n]->weight*amount ;
  4848. if( sd->inventory.u.items_inventory[n].amount <= 0 ){
  4849. if(sd->inventory.u.items_inventory[n].equip)
  4850. pc_unequipitem(sd,n,2|(!(type&4) ? 1 : 0));
  4851. memset(&sd->inventory.u.items_inventory[n],0,sizeof(sd->inventory.u.items_inventory[0]));
  4852. sd->inventory_data[n] = NULL;
  4853. }
  4854. if(!(type&1))
  4855. clif_delitem(sd,n,amount,reason);
  4856. if(!(type&2))
  4857. clif_updatestatus(sd,SP_WEIGHT);
  4858. pc_show_questinfo(sd);
  4859. return 0;
  4860. }
  4861. /*==========================================
  4862. * Attempt to drop an item.
  4863. * @param sd
  4864. * @param n Item index in inventory
  4865. * @param amount Amount of item
  4866. * @return False = fail; True = success
  4867. *------------------------------------------*/
  4868. bool pc_dropitem(struct map_session_data *sd,int n,int amount)
  4869. {
  4870. nullpo_retr(1, sd);
  4871. if(n < 0 || n >= MAX_INVENTORY)
  4872. return false;
  4873. if(amount <= 0)
  4874. return false;
  4875. if(sd->inventory.u.items_inventory[n].nameid == 0 ||
  4876. sd->inventory.u.items_inventory[n].amount <= 0 ||
  4877. sd->inventory.u.items_inventory[n].amount < amount ||
  4878. sd->state.trading || sd->state.vending ||
  4879. !sd->inventory_data[n] //pc_delitem would fail on this case.
  4880. )
  4881. return false;
  4882. if( sd->inventory.u.items_inventory[n].equipSwitch )
  4883. return false;
  4884. if( map_getmapflag(sd->bl.m, MF_NODROP) )
  4885. {
  4886. clif_displaymessage (sd->fd, msg_txt(sd,271));
  4887. return false; //Can't drop items in nodrop mapflag maps.
  4888. }
  4889. if( !pc_candrop(sd,&sd->inventory.u.items_inventory[n]) )
  4890. {
  4891. clif_displaymessage (sd->fd, msg_txt(sd,263));
  4892. return false;
  4893. }
  4894. if (!map_addflooritem(&sd->inventory.u.items_inventory[n], amount, sd->bl.m, sd->bl.x, sd->bl.y, 0, 0, 0, 2, 0))
  4895. return false;
  4896. pc_delitem(sd, n, amount, 1, 0, LOG_TYPE_PICKDROP_PLAYER);
  4897. clif_dropitem(sd, n, amount);
  4898. return true;
  4899. }
  4900. /*==========================================
  4901. * Attempt to pick up an item.
  4902. * @param sd
  4903. * @param fitem Item that will be picked
  4904. * @return False = fail; True = success
  4905. *------------------------------------------*/
  4906. bool pc_takeitem(struct map_session_data *sd,struct flooritem_data *fitem)
  4907. {
  4908. int flag = 0;
  4909. t_tick tick = gettick();
  4910. struct party_data *p = NULL;
  4911. nullpo_ret(sd);
  4912. nullpo_ret(fitem);
  4913. if (!check_distance_bl(&fitem->bl, &sd->bl, 2) && sd->ud.skill_id!=BS_GREED)
  4914. return false; // Distance is too far
  4915. if (sd->sc.cant.pickup)
  4916. return false;
  4917. if (sd->status.party_id)
  4918. p = party_search(sd->status.party_id);
  4919. if (fitem->first_get_charid > 0 && fitem->first_get_charid != sd->status.char_id) {
  4920. struct map_session_data *first_sd = map_charid2sd(fitem->first_get_charid);
  4921. if (DIFF_TICK(tick,fitem->first_get_tick) < 0) {
  4922. if (!(p && p->party.item&1 &&
  4923. first_sd && first_sd->status.party_id == sd->status.party_id
  4924. ))
  4925. return false;
  4926. }
  4927. else if (fitem->second_get_charid > 0 && fitem->second_get_charid != sd->status.char_id) {
  4928. struct map_session_data *second_sd = map_charid2sd(fitem->second_get_charid);
  4929. if (DIFF_TICK(tick, fitem->second_get_tick) < 0) {
  4930. if (!(p && p->party.item&1 &&
  4931. ((first_sd && first_sd->status.party_id == sd->status.party_id) ||
  4932. (second_sd && second_sd->status.party_id == sd->status.party_id))
  4933. ))
  4934. return false;
  4935. }
  4936. else if (fitem->third_get_charid > 0 && fitem->third_get_charid != sd->status.char_id){
  4937. struct map_session_data *third_sd = map_charid2sd(fitem->third_get_charid);
  4938. if (DIFF_TICK(tick,fitem->third_get_tick) < 0) {
  4939. if(!(p && p->party.item&1 &&
  4940. ((first_sd && first_sd->status.party_id == sd->status.party_id) ||
  4941. (second_sd && second_sd->status.party_id == sd->status.party_id) ||
  4942. (third_sd && third_sd->status.party_id == sd->status.party_id))
  4943. ))
  4944. return false;
  4945. }
  4946. }
  4947. }
  4948. }
  4949. //This function takes care of giving the item to whoever should have it, considering party-share options.
  4950. if ((flag = party_share_loot(p,sd,&fitem->item, fitem->first_get_charid))) {
  4951. clif_additem(sd,0,0,flag);
  4952. return true;
  4953. }
  4954. //Display pickup animation.
  4955. pc_stop_attack(sd);
  4956. clif_takeitem(&sd->bl,&fitem->bl);
  4957. if (fitem->mob_id &&
  4958. (itemdb_search(fitem->item.nameid))->flag.broadcast &&
  4959. (!p || !(p->party.item&2)) // Somehow, if party's pickup distribution is 'Even Share', no announcemet
  4960. )
  4961. intif_broadcast_obtain_special_item(sd, fitem->item.nameid, fitem->mob_id, ITEMOBTAIN_TYPE_MONSTER_ITEM);
  4962. map_clearflooritem(&fitem->bl);
  4963. return true;
  4964. }
  4965. /*==========================================
  4966. * Check if item is usable.
  4967. * Return:
  4968. * false = no
  4969. * true = yes
  4970. *------------------------------------------*/
  4971. bool pc_isUseitem(struct map_session_data *sd,int n)
  4972. {
  4973. struct item_data *item;
  4974. t_itemid nameid;
  4975. nullpo_ret(sd);
  4976. item = sd->inventory_data[n];
  4977. nameid = sd->inventory.u.items_inventory[n].nameid;
  4978. if( item == NULL )
  4979. return false;
  4980. //Not consumable item
  4981. if( item->type != IT_HEALING && item->type != IT_USABLE && item->type != IT_CASH )
  4982. return false;
  4983. if (pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL))
  4984. return true;
  4985. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  4986. if(mapdata->flag[MF_NOITEMCONSUMPTION]) //consumable but mapflag prevent it
  4987. return false;
  4988. //Prevent mass item usage. [Skotlex]
  4989. if( DIFF_TICK(sd->canuseitem_tick,gettick()) > 0 ||
  4990. (itemdb_group.item_exists(IG_CASH_FOOD, nameid) && DIFF_TICK(sd->canusecashfood_tick,gettick()) > 0)
  4991. )
  4992. return false;
  4993. if( (item->item_usage.sitting) && (pc_issit(sd) == 1) && (pc_get_group_level(sd) < item->item_usage.override) ) {
  4994. clif_msg(sd,ITEM_NOUSE_SITTING);
  4995. return false; // You cannot use this item while sitting.
  4996. }
  4997. if (sd->state.storage_flag && item->type != IT_CASH) {
  4998. clif_messagecolor(&sd->bl, color_table[COLOR_RED], msg_txt(sd,388), false, SELF);
  4999. return false; // You cannot use this item while storage is open.
  5000. }
  5001. if (item->flag.dead_branch && (mapdata->flag[MF_NOBRANCH] || mapdata_flag_gvg2(mapdata)))
  5002. return false;
  5003. switch( nameid ) {
  5004. case ITEMID_WING_OF_FLY:
  5005. case ITEMID_GIANT_FLY_WING:
  5006. case ITEMID_N_FLY_WING:
  5007. if( mapdata->flag[MF_NOTELEPORT] || mapdata_flag_gvg2(mapdata) ) {
  5008. clif_skill_teleportmessage(sd,0);
  5009. return false;
  5010. }
  5011. if (nameid == ITEMID_GIANT_FLY_WING) {
  5012. struct party_data *pd = party_search(sd->status.party_id);
  5013. if (pd) {
  5014. int i;
  5015. ARR_FIND(0, MAX_PARTY, i, pd->data[i].sd == sd && pd->party.member[i].leader);
  5016. if (i == MAX_PARTY) { // User is not party leader
  5017. clif_msg(sd, ITEM_PARTY_MEMBER_NOT_SUMMONED);
  5018. break;
  5019. }
  5020. 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));
  5021. if (i == MAX_PARTY) { // No party members found on same map
  5022. clif_msg(sd, ITEM_PARTY_NO_MEMBER_IN_MAP);
  5023. break;
  5024. }
  5025. } else {
  5026. clif_msg(sd, ITEM_PARTY_MEMBER_NOT_SUMMONED);
  5027. break;
  5028. }
  5029. }
  5030. // Fall through
  5031. case ITEMID_WING_OF_BUTTERFLY:
  5032. case ITEMID_N_BUTTERFLY_WING:
  5033. case ITEMID_DUN_TELE_SCROLL1:
  5034. case ITEMID_DUN_TELE_SCROLL2:
  5035. case ITEMID_DUN_TELE_SCROLL3:
  5036. case ITEMID_WOB_RUNE:
  5037. case ITEMID_WOB_SCHWALTZ:
  5038. case ITEMID_WOB_RACHEL:
  5039. case ITEMID_WOB_LOCAL:
  5040. case ITEMID_SIEGE_TELEPORT_SCROLL:
  5041. if( sd->duel_group && !battle_config.duel_allow_teleport ) {
  5042. clif_displaymessage(sd->fd, msg_txt(sd,663));
  5043. return false;
  5044. }
  5045. if( mapdata->flag[MF_NORETURN] && nameid != ITEMID_WING_OF_FLY && nameid != ITEMID_GIANT_FLY_WING && nameid != ITEMID_N_FLY_WING )
  5046. return false;
  5047. break;
  5048. case ITEMID_MERCENARY_RED_POTION:
  5049. case ITEMID_MERCENARY_BLUE_POTION:
  5050. case ITEMID_M_CENTER_POTION:
  5051. case ITEMID_M_AWAKENING_POTION:
  5052. case ITEMID_M_BERSERK_POTION:
  5053. if( sd->md == NULL || sd->md->db == NULL )
  5054. return false;
  5055. if( sd->md->sc.cant.consume )
  5056. return false;
  5057. if( nameid == ITEMID_M_AWAKENING_POTION && sd->md->db->lv < 40 )
  5058. return false;
  5059. if( nameid == ITEMID_M_BERSERK_POTION && sd->md->db->lv < 80 )
  5060. return false;
  5061. break;
  5062. case ITEMID_NEURALIZER:
  5063. if( !mapdata->flag[MF_RESET] )
  5064. return false;
  5065. break;
  5066. }
  5067. if( itemdb_group.item_exists(IG_MERCENARY, nameid) && sd->md != NULL )
  5068. return false; // Mercenary Scrolls
  5069. if( item->flag.group || item->type == IT_CASH) { //safe check type cash disappear when overweight [Napster]
  5070. if( pc_is90overweight(sd) ) {
  5071. clif_msg(sd, ITEM_CANT_OBTAIN_WEIGHT);
  5072. return false;
  5073. }
  5074. if( !pc_inventoryblank(sd) ) {
  5075. clif_messagecolor(&sd->bl, color_table[COLOR_RED], msg_txt(sd, 732), false, SELF); //Item cannot be open when inventory is full
  5076. return false;
  5077. }
  5078. }
  5079. //Gender check
  5080. if(item->sex != SEX_BOTH && sd->status.sex != item->sex)
  5081. return false;
  5082. //Required level check
  5083. if(item->elv && sd->status.base_level < (unsigned int)item->elv)
  5084. return false;
  5085. if(item->elvmax && sd->status.base_level > (unsigned int)item->elvmax)
  5086. return false;
  5087. //Not equipable by class. [Skotlex]
  5088. if (!pc_job_can_use_item(sd,item))
  5089. return false;
  5090. if (sd->sc.cant.consume)
  5091. return false;
  5092. if (!pc_isItemClass(sd,item))
  5093. return false;
  5094. //Dead Branch items
  5095. if( item->flag.dead_branch )
  5096. log_branch(sd);
  5097. return true;
  5098. }
  5099. /*==========================================
  5100. * Last checks to use an item.
  5101. * Return:
  5102. * 0 = fail
  5103. * 1 = success
  5104. *------------------------------------------*/
  5105. int pc_useitem(struct map_session_data *sd,int n)
  5106. {
  5107. t_tick tick = gettick();
  5108. int amount;
  5109. t_itemid nameid;
  5110. struct script_code *script;
  5111. struct item item;
  5112. struct item_data *id;
  5113. nullpo_ret(sd);
  5114. if (sd->state.mail_writing)
  5115. return 0;
  5116. if (sd->npc_id) {
  5117. if (sd->progressbar.npc_id) {
  5118. clif_progressbar_abort(sd);
  5119. return 0; // First item use attempt cancels the progress bar
  5120. }
  5121. if( pc_hasprogress( sd, WIP_DISABLE_SKILLITEM ) || !sd->npc_item_flag ){
  5122. #ifdef RENEWAL
  5123. clif_msg( sd, WORK_IN_PROGRESS );
  5124. #endif
  5125. return 0;
  5126. }
  5127. }
  5128. item = sd->inventory.u.items_inventory[n];
  5129. id = sd->inventory_data[n];
  5130. if (item.nameid == 0 || item.amount <= 0)
  5131. return 0;
  5132. if( !pc_isUseitem(sd,n) )
  5133. return 0;
  5134. // Store information for later use before it is lost (via pc_delitem) [Paradox924X]
  5135. nameid = id->nameid;
  5136. if (nameid != ITEMID_NAUTHIZ && sd->sc.opt1 > 0 && sd->sc.opt1 != OPT1_STONEWAIT && sd->sc.opt1 != OPT1_BURNING)
  5137. return 0;
  5138. /* Items with delayed consume are not meant to work while in mounts except reins of mount(12622) */
  5139. if( id->flag.delay_consume > 0 ) {
  5140. if( nameid != ITEMID_REINS_OF_MOUNT && sd->sc.data[SC_ALL_RIDING] )
  5141. return 0;
  5142. else if( pc_issit(sd) )
  5143. return 0;
  5144. }
  5145. //Since most delay-consume items involve using a "skill-type" target cursor,
  5146. //perform a skill-use check before going through. [Skotlex]
  5147. //resurrection was picked as testing skill, as a non-offensive, generic skill, it will do.
  5148. //FIXME: Is this really needed here? It'll be checked in unit.cpp after all and this prevents skill items using when silenced [Inkfish]
  5149. if( id->flag.delay_consume > 0 && ( sd->ud.skilltimer != INVALID_TIMER /*|| !status_check_skilluse(&sd->bl, &sd->bl, ALL_RESURRECTION, 0)*/ ) )
  5150. return 0;
  5151. if( id->delay.duration > 0 && !pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL) && pc_itemcd_check(sd, id, tick, n))
  5152. return 0;
  5153. /* on restricted maps the item is consumed but the effect is not used */
  5154. if (!pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL) && itemdb_isNoEquip(id,sd->bl.m)) {
  5155. clif_msg(sd,ITEM_CANT_USE_AREA); // This item cannot be used within this area
  5156. if( battle_config.allow_consume_restricted_item && id->flag.delay_consume > 0 ) { //need confirmation for delayed consumption items
  5157. clif_useitemack(sd,n,item.amount-1,true);
  5158. pc_delitem(sd,n,1,1,0,LOG_TYPE_CONSUME);
  5159. }
  5160. return 0;/* regardless, effect is not run */
  5161. }
  5162. if (pet_db_search(id->nameid, PET_CATCH) != nullptr && map_getmapflag(sd->bl.m, MF_NOPETCAPTURE)) {
  5163. clif_displaymessage(sd->fd, msg_txt(sd, 669)); // You can't catch any pet on this map.
  5164. return 0;
  5165. }
  5166. sd->itemid = item.nameid;
  5167. sd->itemindex = n;
  5168. if(sd->catch_target_class != PET_CATCH_FAIL) //Abort pet catching.
  5169. sd->catch_target_class = PET_CATCH_FAIL;
  5170. amount = item.amount;
  5171. script = id->script;
  5172. //Check if the item is to be consumed immediately [Skotlex]
  5173. if (id->flag.delay_consume > 0)
  5174. clif_useitemack(sd, n, amount, true);
  5175. else
  5176. {
  5177. if( item.expire_time == 0 && nameid != ITEMID_REINS_OF_MOUNT )
  5178. {
  5179. clif_useitemack(sd, n, amount - 1, true);
  5180. pc_delitem(sd, n, 1, 1, 0, LOG_TYPE_CONSUME); // Rental Usable Items are not deleted until expiration
  5181. }
  5182. else
  5183. clif_useitemack(sd, n, 0, false);
  5184. }
  5185. if (item.card[0]==CARD0_CREATE && pc_famerank(MakeDWord(item.card[2],item.card[3]), MAPID_ALCHEMIST))
  5186. potion_flag = 2; // Famous player's potions have 50% more efficiency
  5187. //Update item use time.
  5188. sd->canuseitem_tick = tick + battle_config.item_use_interval;
  5189. if( itemdb_group.item_exists(IG_CASH_FOOD, nameid) )
  5190. sd->canusecashfood_tick = tick + battle_config.cashfood_use_interval;
  5191. run_script(script,0,sd->bl.id,fake_nd->bl.id);
  5192. potion_flag = 0;
  5193. return 1;
  5194. }
  5195. /**
  5196. * Add item on cart for given index.
  5197. * @param sd
  5198. * @param item
  5199. * @param amount
  5200. * @param log_type
  5201. * @return See pc.hpp::e_additem_result
  5202. */
  5203. enum e_additem_result pc_cart_additem(struct map_session_data *sd,struct item *item,int amount,e_log_pick_type log_type)
  5204. {
  5205. struct item_data *data;
  5206. int i,w;
  5207. nullpo_retr(ADDITEM_INVALID, sd);
  5208. nullpo_retr(ADDITEM_INVALID, item);
  5209. if(item->nameid == 0 || amount <= 0)
  5210. return ADDITEM_INVALID;
  5211. if (itemdb_ishatched_egg(item))
  5212. return ADDITEM_INVALID;
  5213. data = itemdb_search(item->nameid);
  5214. if( data->stack.cart && amount > data->stack.amount )
  5215. {// item stack limitation
  5216. return ADDITEM_STACKLIMIT;
  5217. }
  5218. if( !itemdb_cancartstore(item, pc_get_group_level(sd)) || (item->bound > BOUND_ACCOUNT && !pc_can_give_bounded_items(sd)))
  5219. { // Check item trade restrictions [Skotlex]
  5220. clif_displaymessage (sd->fd, msg_txt(sd,264));
  5221. return ADDITEM_INVALID;
  5222. }
  5223. if( (w = data->weight*amount) + sd->cart_weight > sd->cart_weight_max )
  5224. return ADDITEM_OVERWEIGHT;
  5225. i = MAX_CART;
  5226. if( itemdb_isstackable2(data) && !item->expire_time )
  5227. {
  5228. for (i = 0; i < MAX_CART; i++) {
  5229. if (sd->cart.u.items_cart[i].nameid == item->nameid
  5230. && sd->cart.u.items_cart[i].bound == item->bound
  5231. && sd->cart.u.items_cart[i].unique_id == item->unique_id
  5232. && memcmp(sd->cart.u.items_cart[i].card, item->card, sizeof(item->card)) == 0
  5233. )
  5234. break;
  5235. }
  5236. }
  5237. if( i < MAX_CART )
  5238. {// item already in cart, stack it
  5239. 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 ) )
  5240. return ADDITEM_OVERAMOUNT; // no slot
  5241. sd->cart.u.items_cart[i].amount += amount;
  5242. clif_cart_additem(sd,i,amount);
  5243. }
  5244. else
  5245. {// item not stackable or not present, add it
  5246. ARR_FIND( 0, MAX_CART, i, sd->cart.u.items_cart[i].nameid == 0 );
  5247. if( i == MAX_CART )
  5248. return ADDITEM_OVERAMOUNT; // no slot
  5249. memcpy(&sd->cart.u.items_cart[i],item,sizeof(sd->cart.u.items_cart[0]));
  5250. sd->cart.u.items_cart[i].id = 0;
  5251. sd->cart.u.items_cart[i].amount = amount;
  5252. sd->cart_num++;
  5253. clif_cart_additem(sd,i,amount);
  5254. }
  5255. sd->cart.u.items_cart[i].favorite = 0; // clear
  5256. sd->cart.u.items_cart[i].equipSwitch = 0;
  5257. log_pick_pc(sd, log_type, amount, &sd->cart.u.items_cart[i]);
  5258. sd->cart_weight += w;
  5259. clif_updatestatus(sd,SP_CARTINFO);
  5260. return ADDITEM_SUCCESS;
  5261. }
  5262. /*==========================================
  5263. * Delete item on cart for given index.
  5264. *------------------------------------------*/
  5265. void pc_cart_delitem(struct map_session_data *sd,int n,int amount,int type,e_log_pick_type log_type)
  5266. {
  5267. nullpo_retv(sd);
  5268. if(sd->cart.u.items_cart[n].nameid == 0 ||
  5269. sd->cart.u.items_cart[n].amount < amount)
  5270. return;
  5271. log_pick_pc(sd, log_type, -amount, &sd->cart.u.items_cart[n]);
  5272. sd->cart.u.items_cart[n].amount -= amount;
  5273. sd->cart_weight -= itemdb_weight(sd->cart.u.items_cart[n].nameid) * amount;
  5274. if(sd->cart.u.items_cart[n].amount <= 0) {
  5275. memset(&sd->cart.u.items_cart[n],0,sizeof(sd->cart.u.items_cart[0]));
  5276. sd->cart_num--;
  5277. }
  5278. if(!type) {
  5279. clif_cart_delitem(sd,n,amount);
  5280. clif_updatestatus(sd,SP_CARTINFO);
  5281. }
  5282. }
  5283. /*==========================================
  5284. * Transfer item from inventory to cart.
  5285. *------------------------------------------*/
  5286. void pc_putitemtocart(struct map_session_data *sd,int idx,int amount)
  5287. {
  5288. nullpo_retv(sd);
  5289. if (idx < 0 || idx >= MAX_INVENTORY) //Invalid index check [Skotlex]
  5290. return;
  5291. struct item *item_data = &sd->inventory.u.items_inventory[idx];
  5292. if( item_data->nameid == 0 || amount < 1 || item_data->amount < amount || sd->state.vending || sd->state.prevend )
  5293. return;
  5294. if( item_data->equipSwitch ){
  5295. clif_msg( sd, C_ITEM_EQUIP_SWITCH );
  5296. return;
  5297. }
  5298. enum e_additem_result flag = pc_cart_additem(sd,item_data,amount,LOG_TYPE_NONE);
  5299. if (flag == ADDITEM_SUCCESS)
  5300. pc_delitem(sd,idx,amount,0,5,LOG_TYPE_NONE);
  5301. else {
  5302. clif_cart_additem_ack(sd, (flag == ADDITEM_OVERAMOUNT) ? ADDITEM_TO_CART_FAIL_COUNT : ADDITEM_TO_CART_FAIL_WEIGHT);
  5303. clif_additem(sd, idx, amount, 0);
  5304. clif_delitem(sd, idx, amount, 0);
  5305. }
  5306. }
  5307. /*==========================================
  5308. * Get number of item in cart.
  5309. * Return:
  5310. -1 = itemid not found or no amount found
  5311. x = remaining itemid on cart after get
  5312. *------------------------------------------*/
  5313. int pc_cartitem_amount(struct map_session_data* sd, int idx, int amount)
  5314. {
  5315. struct item* item_data;
  5316. nullpo_retr(-1, sd);
  5317. item_data = &sd->cart.u.items_cart[idx];
  5318. if( item_data->nameid == 0 || item_data->amount == 0 )
  5319. return -1;
  5320. return item_data->amount - amount;
  5321. }
  5322. /*==========================================
  5323. * Retrieve an item at index idx from cart.
  5324. *------------------------------------------*/
  5325. void pc_getitemfromcart(struct map_session_data *sd,int idx,int amount)
  5326. {
  5327. nullpo_retv(sd);
  5328. if (idx < 0 || idx >= MAX_CART) //Invalid index check [Skotlex]
  5329. return;
  5330. struct item *item_data=&sd->cart.u.items_cart[idx];
  5331. if (item_data->nameid == 0 || amount < 1 || item_data->amount < amount || sd->state.vending || sd->state.prevend)
  5332. return;
  5333. enum e_additem_result flag = pc_additem(sd, item_data, amount, LOG_TYPE_NONE);
  5334. if (flag == ADDITEM_SUCCESS)
  5335. pc_cart_delitem(sd, idx, amount, 0, LOG_TYPE_NONE);
  5336. else {
  5337. clif_cart_delitem(sd, idx, amount);
  5338. clif_additem(sd, idx, amount, flag);
  5339. clif_cart_additem(sd, idx, amount);
  5340. }
  5341. }
  5342. /*==========================================
  5343. * Bound Item Check
  5344. * Type:
  5345. * 1 Account Bound
  5346. * 2 Guild Bound
  5347. * 3 Party Bound
  5348. * 4 Character Bound
  5349. *------------------------------------------*/
  5350. int pc_bound_chk(TBL_PC *sd,enum bound_type type,int *idxlist)
  5351. {
  5352. int i = 0, j = 0;
  5353. for(i = 0; i < MAX_INVENTORY; i++) {
  5354. 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) {
  5355. idxlist[j] = i;
  5356. j++;
  5357. }
  5358. }
  5359. return j;
  5360. }
  5361. /*==========================================
  5362. * Display item stolen msg to player sd
  5363. *------------------------------------------*/
  5364. int pc_show_steal(struct block_list *bl,va_list ap)
  5365. {
  5366. struct map_session_data *sd;
  5367. t_itemid itemid;
  5368. struct item_data *item=NULL;
  5369. char output[100];
  5370. sd=va_arg(ap,struct map_session_data *);
  5371. itemid=va_arg(ap,int);
  5372. if((item=itemdb_exists(itemid))==NULL)
  5373. sprintf(output,"%s stole an Unknown Item (id: %u).",sd->status.name, itemid);
  5374. else
  5375. sprintf(output,"%s stole %s.",sd->status.name,item->ename.c_str());
  5376. clif_displaymessage( ((struct map_session_data *)bl)->fd, output);
  5377. return 0;
  5378. }
  5379. /**
  5380. * Steal an item from bl (mob).
  5381. * @param sd: Player data
  5382. * @param bl: Object to steal from
  5383. * @param skill_lv: Level of skill used
  5384. * @return True on success or false otherwise
  5385. */
  5386. bool pc_steal_item(struct map_session_data *sd,struct block_list *bl, uint16 skill_lv)
  5387. {
  5388. int i;
  5389. t_itemid itemid;
  5390. double rate;
  5391. unsigned char flag = 0;
  5392. struct status_data *sd_status, *md_status;
  5393. struct mob_data *md;
  5394. if(!sd || !bl || bl->type!=BL_MOB)
  5395. return false;
  5396. md = (TBL_MOB *)bl;
  5397. if(md->state.steal_flag == UCHAR_MAX || ( md->sc.opt1 && md->sc.opt1 != OPT1_BURNING ) ) //already stolen from / status change check
  5398. return false;
  5399. sd_status= status_get_status_data(&sd->bl);
  5400. md_status= status_get_status_data(bl);
  5401. if (md->master_id || status_has_mode(md_status, MD_STATUSIMMUNE) || util::vector_exists(status_get_race2(&md->bl), RC2_TREASURE) ||
  5402. map_getmapflag(bl->m, MF_NOMOBLOOT) || // check noloot map flag [Lorky]
  5403. (battle_config.skill_steal_max_tries && //Reached limit of steal attempts. [Lupus]
  5404. md->state.steal_flag++ >= battle_config.skill_steal_max_tries)
  5405. ) { //Can't steal from
  5406. md->state.steal_flag = UCHAR_MAX;
  5407. return false;
  5408. }
  5409. // base skill success chance (percentual)
  5410. rate = (sd_status->dex - md_status->dex)/2 + skill_lv*6 + 4;
  5411. rate += sd->bonus.add_steal_rate;
  5412. if( rate < 1
  5413. #ifdef RENEWAL
  5414. || rnd()%100 >= rate
  5415. #endif
  5416. )
  5417. return false;
  5418. // Try dropping one item, in the order from first to last possible slot.
  5419. // Droprate is affected by the skill success rate.
  5420. for( i = 0; i < MAX_MOB_DROP; i++ )
  5421. 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].rate
  5422. #ifndef RENEWAL
  5423. * rate/100.
  5424. #endif
  5425. )
  5426. break;
  5427. if( i == MAX_MOB_DROP )
  5428. return false;
  5429. itemid = md->db->dropitem[i].nameid;
  5430. struct item tmp_item = {};
  5431. tmp_item.nameid = itemid;
  5432. tmp_item.amount = 1;
  5433. tmp_item.identify = itemdb_isidentified(itemid);
  5434. if( battle_config.skill_steal_random_options ){
  5435. mob_setdropitem_option( &tmp_item, &md->db->dropitem[i] );
  5436. }
  5437. flag = pc_additem(sd,&tmp_item,1,LOG_TYPE_PICKDROP_PLAYER);
  5438. //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?
  5439. md->state.steal_flag = UCHAR_MAX; //you can't steal from this mob any more
  5440. if(flag) { //Failed to steal due to overweight
  5441. clif_additem(sd,0,0,flag);
  5442. return false;
  5443. }
  5444. if(battle_config.show_steal_in_same_party)
  5445. party_foreachsamemap(pc_show_steal,sd,AREA_SIZE,sd,tmp_item.nameid);
  5446. //Logs items, Stolen from mobs [Lupus]
  5447. log_pick_mob(md, LOG_TYPE_STEAL, -1, &tmp_item);
  5448. //A Rare Steal Global Announce by Lupus
  5449. if(md->db->dropitem[i].rate <= battle_config.rare_drop_announce) {
  5450. struct item_data *i_data;
  5451. char message[128];
  5452. i_data = itemdb_search(itemid);
  5453. sprintf (message, msg_txt(sd,542), (sd->status.name[0])?sd->status.name :"GM", md->db->jname.c_str(), i_data->ename.c_str(), (float)md->db->dropitem[i].rate / 100);
  5454. //MSG: "'%s' stole %s's %s (chance: %0.02f%%)"
  5455. intif_broadcast(message, strlen(message) + 1, BC_DEFAULT);
  5456. }
  5457. return true;
  5458. }
  5459. /*==========================================
  5460. * Stole zeny from bl (mob)
  5461. * return
  5462. * 0 = fail
  5463. * 1 = success
  5464. *------------------------------------------*/
  5465. int pc_steal_coin(struct map_session_data *sd,struct block_list *target)
  5466. {
  5467. int rate, target_lv;
  5468. struct mob_data *md;
  5469. if(!sd || !target || target->type != BL_MOB)
  5470. return 0;
  5471. md = (TBL_MOB*)target;
  5472. target_lv = status_get_lv(target);
  5473. if (md->state.steal_coin_flag || md->sc.data[SC_STONE] || md->sc.data[SC_FREEZE] || md->sc.data[SC_HANDICAPSTATE_FROSTBITE] ||
  5474. md->sc.data[SC_HANDICAPSTATE_SWOONING] || md->sc.data[SC_HANDICAPSTATE_LIGHTNINGSTRIKE] || md->sc.data[SC_HANDICAPSTATE_CRYSTALLIZATION] ||
  5475. status_bl_has_mode(target,MD_STATUSIMMUNE) || util::vector_exists(status_get_race2(&md->bl), RC2_TREASURE))
  5476. return 0;
  5477. rate = sd->battle_status.dex / 2 + 2 * (sd->status.base_level - target_lv) + (10 * pc_checkskill(sd, RG_STEALCOIN)) + sd->battle_status.luk / 2;
  5478. if(rnd()%1000 < rate)
  5479. {
  5480. // Zeny Steal Amount: (rnd() % (10 * target_lv + 1 - 8 * target_lv)) + 8 * target_lv
  5481. int amount = (rnd() % (2 * target_lv + 1)) + 8 * target_lv; // Reduced formula
  5482. pc_getzeny(sd, amount, LOG_TYPE_STEAL, NULL);
  5483. md->state.steal_coin_flag = 1;
  5484. return 1;
  5485. }
  5486. return 0;
  5487. }
  5488. /*==========================================
  5489. * Set's a player position.
  5490. * @param sd
  5491. * @param mapindex
  5492. * @param x
  5493. * @param y
  5494. * @param clrtype
  5495. * @return SETPOS_OK Success
  5496. * SETPOS_MAPINDEX Invalid map index
  5497. * SETPOS_NO_MAPSERVER Map not in this map-server, and failed to locate alternate map-server.
  5498. * SETPOS_AUTOTRADE Player is in autotrade state
  5499. *------------------------------------------*/
  5500. enum e_setpos pc_setpos(struct map_session_data* sd, unsigned short mapindex, int x, int y, clr_type clrtype)
  5501. {
  5502. nullpo_retr(SETPOS_OK,sd);
  5503. if( !mapindex || !mapindex_id2name(mapindex) ) {
  5504. ShowDebug("pc_setpos: Passed mapindex(%d) is invalid!\n", mapindex);
  5505. return SETPOS_MAPINDEX;
  5506. }
  5507. if ( sd->state.autotrade && (sd->vender_id || sd->buyer_id) ) // Player with autotrade just causes clif glitch! @ FIXME
  5508. return SETPOS_AUTOTRADE;
  5509. if( battle_config.revive_onwarp && pc_isdead(sd) ) { //Revive dead people before warping them
  5510. pc_setstand(sd, true);
  5511. pc_setrestartvalue(sd,1);
  5512. }
  5513. int16 m = map_mapindex2mapid(mapindex);
  5514. struct map_data *mapdata = map_getmapdata(m);
  5515. status_change *sc = status_get_sc(&sd->bl);
  5516. sd->state.changemap = (sd->mapindex != mapindex);
  5517. sd->state.warping = 1;
  5518. sd->state.workinprogress = WIP_DISABLE_NONE;
  5519. sd->state.mail_writing = false;
  5520. sd->state.refineui_open = false;
  5521. if( sd->state.changemap ) { // Misc map-changing settings
  5522. int curr_map_instance_id = map_getmapdata(sd->bl.m)->instance_id, new_map_instance_id = (mapdata ? mapdata->instance_id : 0);
  5523. if (curr_map_instance_id != new_map_instance_id) {
  5524. if (curr_map_instance_id > 0) { // Update instance timer for the map on leave
  5525. instance_delusers(curr_map_instance_id);
  5526. sd->instance_mode = util::umap_find(instances, curr_map_instance_id)->mode; // Store mode for instance destruction button checks
  5527. }
  5528. if (new_map_instance_id > 0) // Update instance timer for the map on enter
  5529. instance_addusers(new_map_instance_id);
  5530. }
  5531. if (sd->bg_id && mapdata && !mapdata->flag[MF_BATTLEGROUND]) // Moving to a map that isn't a Battlegrounds
  5532. bg_team_leave(sd, false, true);
  5533. sd->state.pmap = sd->bl.m;
  5534. if (sc && sc->count) { // Cancel some map related stuff.
  5535. if (sc->cant.warp)
  5536. return SETPOS_MAPINDEX; // You may not get out!
  5537. for (const auto &it : status_db) {
  5538. if (sc->data[it.first]) {
  5539. if (it.second->flag[SCF_REMOVEONMAPWARP])
  5540. status_change_end(&sd->bl, static_cast<sc_type>(it.first), INVALID_TIMER);
  5541. if (it.second->flag[SCF_RESTARTONMAPWARP] && it.second->skill_id > 0) {
  5542. status_change_entry *sce = sd->sc.data[it.first];
  5543. if (sce->timer != INVALID_TIMER)
  5544. delete_timer(sce->timer, status_change_timer);
  5545. sce->timer = add_timer(gettick() + skill_get_time(it.second->skill_id, sce->val1), status_change_timer, sd->bl.id, it.first);
  5546. }
  5547. }
  5548. }
  5549. }
  5550. for(int i = 0; i < EQI_MAX; i++ ) {
  5551. if( sd->equip_index[i] >= 0 )
  5552. if( pc_isequip(sd,sd->equip_index[i]) )
  5553. pc_unequipitem(sd,sd->equip_index[i],2);
  5554. }
  5555. if (battle_config.clear_unit_onwarp&BL_PC)
  5556. skill_clear_unitgroup(&sd->bl);
  5557. if( battle_config.loose_ap_on_map && mapdata_flag_vs( mapdata ) ){
  5558. status_percent_damage( nullptr, &sd->bl, 0, 0, 100, 0 );
  5559. }
  5560. party_send_dot_remove(sd); //minimap dot fix [Kevin]
  5561. guild_send_dot_remove(sd);
  5562. bg_send_dot_remove(sd);
  5563. if (sd->regen.state.gc)
  5564. sd->regen.state.gc = 0;
  5565. if (mapdata) {
  5566. // make sure vending is allowed here
  5567. if (sd->state.vending && mapdata->flag[MF_NOVENDING]) {
  5568. clif_displaymessage(sd->fd, msg_txt(sd, 276)); // "You can't open a shop on this map"
  5569. vending_closevending(sd);
  5570. }
  5571. // make sure buyingstore is allowed here
  5572. if (sd->state.buyingstore && mapdata->flag[MF_NOBUYINGSTORE]) {
  5573. clif_displaymessage(sd->fd, msg_txt(sd, 276)); // "You can't open a shop on this map"
  5574. buyingstore_close(sd);
  5575. }
  5576. }
  5577. channel_pcquit(sd,4); //quit map chan
  5578. // Remove Cloaked NPC if changing to another map
  5579. for (auto it = sd->cloaked_npc.begin(); it != sd->cloaked_npc.end(); ++it) {
  5580. block_list *npc_bl = map_id2bl(*it);
  5581. if (npc_bl && npc_bl->m != m) {
  5582. sd->cloaked_npc.erase(it);
  5583. break;
  5584. }
  5585. }
  5586. }
  5587. if( m < 0 )
  5588. {
  5589. uint32 ip;
  5590. uint16 port;
  5591. struct script_state *st;
  5592. //if can't find any map-servers, just abort setting position.
  5593. if(!sd->mapindex || map_mapname2ipport(mapindex,&ip,&port))
  5594. return SETPOS_NO_MAPSERVER;
  5595. if (sd->npc_id){
  5596. npc_event_dequeue(sd,false);
  5597. st = sd->st;
  5598. }else{
  5599. st = nullptr;
  5600. }
  5601. if (sd->bg_id) // Switching map servers, remove from bg
  5602. bg_team_leave(sd, false, true);
  5603. if (sd->state.vending) // Stop vending
  5604. vending_closevending(sd);
  5605. if (sd->state.buyingstore) // Stop buyingstore
  5606. buyingstore_close(sd);
  5607. npc_script_event(sd, NPCE_LOGOUT);
  5608. //remove from map, THEN change x/y coordinates
  5609. unit_remove_map_pc(sd,clrtype);
  5610. sd->mapindex = mapindex;
  5611. sd->bl.x=x;
  5612. sd->bl.y=y;
  5613. pc_clean_skilltree(sd);
  5614. chrif_save(sd, CSAVE_CHANGE_MAPSERV|CSAVE_INVENTORY|CSAVE_CART);
  5615. chrif_changemapserver(sd, ip, (short)port);
  5616. //Free session data from this map server [Kevin]
  5617. unit_free_pc(sd);
  5618. if( st ){
  5619. // Has to be done here, because otherwise unit_free_pc will free the stack already
  5620. st->state = END;
  5621. }
  5622. return SETPOS_OK;
  5623. }
  5624. if( x < 0 || x >= mapdata->xs || y < 0 || y >= mapdata->ys )
  5625. {
  5626. 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);
  5627. x = y = 0; // make it random
  5628. }
  5629. if( x == 0 && y == 0 ) { // pick a random walkable cell
  5630. int c=0;
  5631. do {
  5632. x = rnd()%(mapdata->xs-2)+1;
  5633. y = rnd()%(mapdata->ys-2)+1;
  5634. c++;
  5635. if(c > (mapdata->xs * mapdata->ys)*3){ //force out
  5636. 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));
  5637. return SETPOS_OK; //preventing warp
  5638. //break; //allow warp anyway
  5639. }
  5640. } while(map_getcell(m,x,y,CELL_CHKNOPASS) || (!battle_config.teleport_on_portal && npc_check_areanpc(1,m,x,y,1)));
  5641. }
  5642. if (sd->state.vending && map_getcell(m,x,y,CELL_CHKNOVENDING)) {
  5643. clif_displaymessage (sd->fd, msg_txt(sd,204)); // "You can't open a shop on this cell."
  5644. vending_closevending(sd);
  5645. }
  5646. if (sd->state.buyingstore && map_getcell(m, x, y, CELL_CHKNOBUYINGSTORE)) {
  5647. clif_displaymessage(sd->fd, msg_txt(sd, 204)); // "You can't open a shop on this cell."
  5648. buyingstore_close(sd);
  5649. }
  5650. if(sd->bl.prev != NULL){
  5651. unit_remove_map_pc(sd,clrtype);
  5652. clif_changemap(sd,m,x,y); // [MouseJstr]
  5653. } else if(sd->state.active) //Tag player for rewarping after map-loading is done. [Skotlex]
  5654. sd->state.rewarp = 1;
  5655. sd->mapindex = mapindex;
  5656. sd->bl.m = m;
  5657. sd->bl.x = sd->ud.to_x = x;
  5658. sd->bl.y = sd->ud.to_y = y;
  5659. if( sd->status.guild_id > 0 && mapdata->flag[MF_GVG_CASTLE] )
  5660. { // Increased guild castle regen [Valaris]
  5661. std::shared_ptr<guild_castle> gc = castle_db.mapindex2gc(sd->mapindex);
  5662. if(gc && gc->guild_id == sd->status.guild_id)
  5663. sd->regen.state.gc = 1;
  5664. }
  5665. if( sd->status.pet_id > 0 && sd->pd && sd->pd->pet.intimate > 0 )
  5666. {
  5667. sd->pd->bl.m = m;
  5668. sd->pd->bl.x = sd->pd->ud.to_x = x;
  5669. sd->pd->bl.y = sd->pd->ud.to_y = y;
  5670. sd->pd->ud.dir = sd->ud.dir;
  5671. }
  5672. if( hom_is_active(sd->hd) )
  5673. {
  5674. sd->hd->bl.m = m;
  5675. sd->hd->bl.x = sd->hd->ud.to_x = x;
  5676. sd->hd->bl.y = sd->hd->ud.to_y = y;
  5677. sd->hd->ud.dir = sd->ud.dir;
  5678. }
  5679. if( sd->md )
  5680. {
  5681. sd->md->bl.m = m;
  5682. sd->md->bl.x = sd->md->ud.to_x = x;
  5683. sd->md->bl.y = sd->md->ud.to_y = y;
  5684. sd->md->ud.dir = sd->ud.dir;
  5685. }
  5686. if( sd->ed ) {
  5687. sd->ed->bl.m = m;
  5688. sd->ed->bl.x = sd->ed->ud.to_x = x;
  5689. sd->ed->bl.y = sd->ed->ud.to_y = y;
  5690. sd->ed->ud.dir = sd->ud.dir;
  5691. }
  5692. pc_cell_basilica(sd);
  5693. //check if we gonna be rewarped [lighta]
  5694. if(npc_check_areanpc(1,m,x,y,0)){
  5695. sd->count_rewarp++;
  5696. }
  5697. else
  5698. sd->count_rewarp = 0;
  5699. if (sd->state.vending)
  5700. vending_update(*sd);
  5701. if (sd->state.buyingstore)
  5702. buyingstore_update(*sd);
  5703. return SETPOS_OK;
  5704. }
  5705. /*==========================================
  5706. * Warp player sd to random location on current map.
  5707. * May fail if no walkable cell found (1000 attempts).
  5708. * Return:
  5709. * 0 = Success
  5710. * 1,2,3 = Fail
  5711. *------------------------------------------*/
  5712. char pc_randomwarp(struct map_session_data *sd, clr_type type, bool ignore_mapflag)
  5713. {
  5714. int x,y,i=0;
  5715. nullpo_ret(sd);
  5716. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  5717. if (mapdata->flag[MF_NOTELEPORT] && !ignore_mapflag) //Teleport forbidden
  5718. return 3;
  5719. do {
  5720. x = rnd()%(mapdata->xs-2)+1;
  5721. y = rnd()%(mapdata->ys-2)+1;
  5722. } 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);
  5723. if (i < 1000)
  5724. return pc_setpos(sd,mapdata->index,x,y,type);
  5725. return 3;
  5726. }
  5727. /*==========================================
  5728. * Records a memo point at sd's current position
  5729. * pos - entry to replace, (-1: shift oldest entry out)
  5730. *------------------------------------------*/
  5731. bool pc_memo(struct map_session_data* sd, int pos)
  5732. {
  5733. int skill;
  5734. nullpo_ret(sd);
  5735. // check mapflags
  5736. 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) ) {
  5737. clif_skill_teleportmessage(sd, 1); // "Saved point cannot be memorized."
  5738. return false;
  5739. }
  5740. // check inputs
  5741. if( pos < -1 || pos >= MAX_MEMOPOINTS )
  5742. return false; // invalid input
  5743. // check required skill level
  5744. skill = pc_checkskill(sd, AL_WARP);
  5745. if( skill < 1 ) {
  5746. clif_skill_memomessage(sd,2); // "You haven't learned Warp."
  5747. return false;
  5748. }
  5749. if( skill < 2 || skill - 2 < pos ) {
  5750. clif_skill_memomessage(sd,1); // "Skill Level is not high enough."
  5751. return false;
  5752. }
  5753. if( pos == -1 )
  5754. {
  5755. uint8 i;
  5756. // prevent memo-ing the same map multiple times
  5757. ARR_FIND( 0, MAX_MEMOPOINTS, i, sd->status.memo_point[i].map == map_id2index(sd->bl.m) );
  5758. memmove(&sd->status.memo_point[1], &sd->status.memo_point[0], (u8min(i,MAX_MEMOPOINTS-1))*sizeof(struct point));
  5759. pos = 0;
  5760. }
  5761. if( map_getmapdata(sd->bl.m)->instance_id ) {
  5762. clif_displaymessage( sd->fd, msg_txt(sd,384) ); // You cannot create a memo in an instance.
  5763. return false;
  5764. }
  5765. sd->status.memo_point[pos].map = map_id2index(sd->bl.m);
  5766. sd->status.memo_point[pos].x = sd->bl.x;
  5767. sd->status.memo_point[pos].y = sd->bl.y;
  5768. clif_skill_memomessage(sd, 0);
  5769. return true;
  5770. }
  5771. //
  5772. // Skills
  5773. //
  5774. /**
  5775. * Get the skill current cooldown for player.
  5776. * (get the db base cooldown for skill + player specific cooldown)
  5777. * @param sd : player pointer
  5778. * @param id : skill id
  5779. * @param lv : skill lv
  5780. * @return player skill cooldown
  5781. */
  5782. int pc_get_skillcooldown(struct map_session_data *sd, uint16 skill_id, uint16 skill_lv) {
  5783. if (skill_id == SJ_NOVAEXPLOSING) {
  5784. struct status_change *sc = status_get_sc(&sd->bl);
  5785. if (sc && sc->data[SC_DIMENSION])
  5786. return 0;
  5787. }
  5788. int cooldown = skill_get_cooldown(skill_id, skill_lv);
  5789. if (skill_id == SU_TUNABELLY && pc_checkskill(sd, SU_SPIRITOFSEA) > 0)
  5790. cooldown -= skill_get_time(SU_TUNABELLY, skill_lv);
  5791. for (auto &it : sd->skillcooldown) {
  5792. if (it.id == skill_id) {
  5793. cooldown += it.val;
  5794. break;
  5795. }
  5796. }
  5797. return max(0, cooldown);
  5798. }
  5799. /*==========================================
  5800. * Return player sd skill_lv learned for given skill
  5801. *------------------------------------------*/
  5802. uint8 pc_checkskill(struct map_session_data *sd, uint16 skill_id)
  5803. {
  5804. uint16 idx = 0;
  5805. if (sd == NULL)
  5806. return 0;
  5807. #ifdef RENEWAL
  5808. if ((idx = skill_get_index(skill_id)) == 0) {
  5809. #else
  5810. if( ( idx = skill_db.get_index( skill_id, skill_id >= RK_ENCHANTBLADE, __FUNCTION__, __FILE__, __LINE__ ) ) == 0 ){
  5811. if( skill_id >= RK_ENCHANTBLADE ){
  5812. // Silently fail for now -> future update planned
  5813. return 0;
  5814. }
  5815. #endif
  5816. ShowError("pc_checkskill: Invalid skill id %d (char_id=%d).\n", skill_id, sd->status.char_id);
  5817. return 0;
  5818. }
  5819. if (SKILL_CHK_GUILD(skill_id) ) {
  5820. struct guild *g;
  5821. if( sd->status.guild_id>0 && (g=sd->guild)!=NULL)
  5822. return guild_checkskill(g,skill_id);
  5823. return 0;
  5824. }
  5825. return (sd->status.skill[idx].id == skill_id) ? sd->status.skill[idx].lv : 0;
  5826. }
  5827. /**
  5828. * Returns the amount of skill points invested in a Summoner's Power of Sea/Land/Life
  5829. * @param sd: Player data
  5830. * @param type: Summoner Power Type
  5831. * @return Skill points invested
  5832. */
  5833. uint8 pc_checkskill_summoner(map_session_data *sd, e_summoner_power_type type) {
  5834. if (sd == nullptr)
  5835. return 0;
  5836. uint8 count = 0;
  5837. switch (type) {
  5838. case SUMMONER_POWER_SEA:
  5839. count = pc_checkskill(sd, SU_TUNABELLY) + pc_checkskill(sd, SU_TUNAPARTY) + pc_checkskill(sd, SU_BUNCHOFSHRIMP) + pc_checkskill(sd, SU_FRESHSHRIMP) +
  5840. pc_checkskill(sd, SU_GROOMING) + pc_checkskill(sd, SU_PURRING) + pc_checkskill(sd, SU_SHRIMPARTY);
  5841. break;
  5842. case SUMMONER_POWER_LAND:
  5843. count = pc_checkskill(sd, SU_SV_STEMSPEAR) + pc_checkskill(sd, SU_CN_POWDERING) + pc_checkskill(sd, SU_CN_METEOR) + pc_checkskill(sd, SU_SV_ROOTTWIST) +
  5844. pc_checkskill(sd, SU_CHATTERING) + pc_checkskill(sd, SU_MEOWMEOW) + pc_checkskill(sd, SU_NYANGGRASS);
  5845. break;
  5846. case SUMMONER_POWER_LIFE:
  5847. count = pc_checkskill(sd, SU_SCAROFTAROU) + pc_checkskill(sd, SU_PICKYPECK) + pc_checkskill(sd, SU_ARCLOUSEDASH) + pc_checkskill(sd, SU_LUNATICCARROTBEAT) +
  5848. pc_checkskill(sd, SU_HISS) + pc_checkskill(sd, SU_POWEROFFLOCK) + pc_checkskill(sd, SU_SVG_SPIRIT);
  5849. break;
  5850. }
  5851. return count;
  5852. }
  5853. /**
  5854. * Checks for Imperial Guard's passive skills.
  5855. * @param sd: Player data
  5856. * @param flag:
  5857. * Flag&1 = IG_SHIELD_MASTERY
  5858. * Flag&2 = IG_SPEAR_SWORD_M
  5859. */
  5860. uint8 pc_checkskill_imperial_guard(struct map_session_data *sd, short flag)
  5861. {
  5862. nullpo_retr(0, sd);
  5863. uint8 count = 0;
  5864. if (flag&1 && sd->status.shield > 0)
  5865. count += pc_checkskill(sd, IG_SHIELD_MASTERY);
  5866. if (flag&2 && (sd->status.weapon == W_1HSWORD || sd->status.weapon == W_1HSPEAR || sd->status.weapon == W_2HSPEAR))
  5867. count += pc_checkskill(sd, IG_SPEAR_SWORD_M);
  5868. return count;
  5869. }
  5870. /**
  5871. * Check if we still have the correct weapon to continue the skill (actually status)
  5872. * If not ending it
  5873. * @param sd
  5874. * @return 0:error, 1:check done
  5875. */
  5876. static void pc_checkallowskill(struct map_session_data *sd)
  5877. {
  5878. nullpo_retv(sd);
  5879. if(!sd->sc.count)
  5880. return;
  5881. for (const auto &it : status_db) {
  5882. sc_type status = it.second->type;
  5883. std::bitset<SCF_MAX> flag = it.second->flag;
  5884. if (flag[SCF_REQUIREWEAPON]) { // Skills requiring specific weapon types
  5885. if (status == SC_DANCING && !battle_config.dancing_weaponswitch_fix)
  5886. continue;
  5887. if (sd->sc.data[status] && !pc_check_weapontype(sd, skill_get_weapontype(it.second->skill_id)))
  5888. status_change_end(&sd->bl, status, INVALID_TIMER);
  5889. }
  5890. if (flag[SCF_REQUIRESHIELD]) { // Skills requiring a shield
  5891. if (sd->sc.data[status] && sd->status.shield <= 0)
  5892. status_change_end(&sd->bl, status, INVALID_TIMER);
  5893. }
  5894. }
  5895. }
  5896. /*==========================================
  5897. * Return equipped index of item on player sd at pos
  5898. * Return
  5899. * -1 : Nothing equipped
  5900. * idx : (this index could be used in inventory to found item_data)
  5901. *------------------------------------------*/
  5902. short pc_checkequip(struct map_session_data *sd,int pos, bool checkall)
  5903. {
  5904. uint8 i;
  5905. nullpo_retr(-1, sd);
  5906. for(i=0;i<EQI_MAX;i++){
  5907. if(pos & equip_bitmask[i]){
  5908. if( checkall && ( pos&~equip_bitmask[i] ) != 0 && sd->equip_index[i] == -1 ){
  5909. // Check all if any match is found
  5910. continue;
  5911. }
  5912. return sd->equip_index[i];
  5913. }
  5914. }
  5915. return -1;
  5916. }
  5917. /*==========================================
  5918. * Check if sd has nameid equipped somewhere
  5919. * @sd : the player session
  5920. * @nameid : id of the item to check
  5921. * @min : : see pc.hpp enum equip_index from ? to @max
  5922. * @max : see pc.hpp enum equip_index for @min to ?
  5923. * -return true,false
  5924. *------------------------------------------*/
  5925. bool pc_checkequip2(struct map_session_data *sd, t_itemid nameid, int min, int max)
  5926. {
  5927. int i;
  5928. for(i = min; i < max; i++) {
  5929. if(equip_bitmask[i]) {
  5930. int idx = sd->equip_index[i];
  5931. if (sd->inventory.u.items_inventory[idx].nameid == nameid)
  5932. return true;
  5933. }
  5934. }
  5935. return false;
  5936. }
  5937. /*==========================================
  5938. * Convert's from the client's lame Job ID system
  5939. * to the map server's 'makes sense' system. [Skotlex]
  5940. *------------------------------------------*/
  5941. uint64 pc_jobid2mapid(unsigned short b_class)
  5942. {
  5943. switch(b_class)
  5944. {
  5945. //Novice And 1-1 Jobs
  5946. case JOB_NOVICE: return MAPID_NOVICE;
  5947. case JOB_SWORDMAN: return MAPID_SWORDMAN;
  5948. case JOB_MAGE: return MAPID_MAGE;
  5949. case JOB_ARCHER: return MAPID_ARCHER;
  5950. case JOB_ACOLYTE: return MAPID_ACOLYTE;
  5951. case JOB_MERCHANT: return MAPID_MERCHANT;
  5952. case JOB_THIEF: return MAPID_THIEF;
  5953. case JOB_TAEKWON: return MAPID_TAEKWON;
  5954. case JOB_WEDDING: return MAPID_WEDDING;
  5955. case JOB_GUNSLINGER: return MAPID_GUNSLINGER;
  5956. case JOB_NINJA: return MAPID_NINJA;
  5957. case JOB_XMAS: return MAPID_XMAS;
  5958. case JOB_SUMMER: return MAPID_SUMMER;
  5959. case JOB_HANBOK: return MAPID_HANBOK;
  5960. case JOB_GANGSI: return MAPID_GANGSI;
  5961. case JOB_OKTOBERFEST: return MAPID_OKTOBERFEST;
  5962. case JOB_SUMMER2: return MAPID_SUMMER2;
  5963. //2-1 Jobs
  5964. case JOB_SUPER_NOVICE: return MAPID_SUPER_NOVICE;
  5965. case JOB_KNIGHT: return MAPID_KNIGHT;
  5966. case JOB_WIZARD: return MAPID_WIZARD;
  5967. case JOB_HUNTER: return MAPID_HUNTER;
  5968. case JOB_PRIEST: return MAPID_PRIEST;
  5969. case JOB_BLACKSMITH: return MAPID_BLACKSMITH;
  5970. case JOB_ASSASSIN: return MAPID_ASSASSIN;
  5971. case JOB_STAR_GLADIATOR: return MAPID_STAR_GLADIATOR;
  5972. case JOB_KAGEROU:
  5973. case JOB_OBORO: return MAPID_KAGEROUOBORO;
  5974. case JOB_REBELLION: return MAPID_REBELLION;
  5975. case JOB_DEATH_KNIGHT: return MAPID_DEATH_KNIGHT;
  5976. //2-2 Jobs
  5977. case JOB_CRUSADER: return MAPID_CRUSADER;
  5978. case JOB_SAGE: return MAPID_SAGE;
  5979. case JOB_BARD:
  5980. case JOB_DANCER: return MAPID_BARDDANCER;
  5981. case JOB_MONK: return MAPID_MONK;
  5982. case JOB_ALCHEMIST: return MAPID_ALCHEMIST;
  5983. case JOB_ROGUE: return MAPID_ROGUE;
  5984. case JOB_SOUL_LINKER: return MAPID_SOUL_LINKER;
  5985. case JOB_DARK_COLLECTOR: return MAPID_DARK_COLLECTOR;
  5986. //Trans Novice And Trans 1-1 Jobs
  5987. case JOB_NOVICE_HIGH: return MAPID_NOVICE_HIGH;
  5988. case JOB_SWORDMAN_HIGH: return MAPID_SWORDMAN_HIGH;
  5989. case JOB_MAGE_HIGH: return MAPID_MAGE_HIGH;
  5990. case JOB_ARCHER_HIGH: return MAPID_ARCHER_HIGH;
  5991. case JOB_ACOLYTE_HIGH: return MAPID_ACOLYTE_HIGH;
  5992. case JOB_MERCHANT_HIGH: return MAPID_MERCHANT_HIGH;
  5993. case JOB_THIEF_HIGH: return MAPID_THIEF_HIGH;
  5994. //Trans 2-1 Jobs
  5995. case JOB_LORD_KNIGHT: return MAPID_LORD_KNIGHT;
  5996. case JOB_HIGH_WIZARD: return MAPID_HIGH_WIZARD;
  5997. case JOB_SNIPER: return MAPID_SNIPER;
  5998. case JOB_HIGH_PRIEST: return MAPID_HIGH_PRIEST;
  5999. case JOB_WHITESMITH: return MAPID_WHITESMITH;
  6000. case JOB_ASSASSIN_CROSS: return MAPID_ASSASSIN_CROSS;
  6001. //Trans 2-2 Jobs
  6002. case JOB_PALADIN: return MAPID_PALADIN;
  6003. case JOB_PROFESSOR: return MAPID_PROFESSOR;
  6004. case JOB_CLOWN:
  6005. case JOB_GYPSY: return MAPID_CLOWNGYPSY;
  6006. case JOB_CHAMPION: return MAPID_CHAMPION;
  6007. case JOB_CREATOR: return MAPID_CREATOR;
  6008. case JOB_STALKER: return MAPID_STALKER;
  6009. //Baby Novice And Baby 1-1 Jobs
  6010. case JOB_BABY: return MAPID_BABY;
  6011. case JOB_BABY_SWORDMAN: return MAPID_BABY_SWORDMAN;
  6012. case JOB_BABY_MAGE: return MAPID_BABY_MAGE;
  6013. case JOB_BABY_ARCHER: return MAPID_BABY_ARCHER;
  6014. case JOB_BABY_ACOLYTE: return MAPID_BABY_ACOLYTE;
  6015. case JOB_BABY_MERCHANT: return MAPID_BABY_MERCHANT;
  6016. case JOB_BABY_THIEF: return MAPID_BABY_THIEF;
  6017. case JOB_BABY_TAEKWON: return MAPID_BABY_TAEKWON;
  6018. case JOB_BABY_GUNSLINGER: return MAPID_BABY_GUNSLINGER;
  6019. case JOB_BABY_NINJA: return MAPID_BABY_NINJA;
  6020. case JOB_BABY_SUMMONER: return MAPID_BABY_SUMMONER;
  6021. //Baby 2-1 Jobs
  6022. case JOB_SUPER_BABY: return MAPID_SUPER_BABY;
  6023. case JOB_BABY_KNIGHT: return MAPID_BABY_KNIGHT;
  6024. case JOB_BABY_WIZARD: return MAPID_BABY_WIZARD;
  6025. case JOB_BABY_HUNTER: return MAPID_BABY_HUNTER;
  6026. case JOB_BABY_PRIEST: return MAPID_BABY_PRIEST;
  6027. case JOB_BABY_BLACKSMITH: return MAPID_BABY_BLACKSMITH;
  6028. case JOB_BABY_ASSASSIN: return MAPID_BABY_ASSASSIN;
  6029. case JOB_BABY_STAR_GLADIATOR: return MAPID_BABY_STAR_GLADIATOR;
  6030. case JOB_BABY_REBELLION: return MAPID_BABY_REBELLION;
  6031. case JOB_BABY_KAGEROU:
  6032. case JOB_BABY_OBORO: return MAPID_BABY_KAGEROUOBORO;
  6033. //Baby 2-2 Jobs
  6034. case JOB_BABY_CRUSADER: return MAPID_BABY_CRUSADER;
  6035. case JOB_BABY_SAGE: return MAPID_BABY_SAGE;
  6036. case JOB_BABY_BARD:
  6037. case JOB_BABY_DANCER: return MAPID_BABY_BARDDANCER;
  6038. case JOB_BABY_MONK: return MAPID_BABY_MONK;
  6039. case JOB_BABY_ALCHEMIST: return MAPID_BABY_ALCHEMIST;
  6040. case JOB_BABY_ROGUE: return MAPID_BABY_ROGUE;
  6041. case JOB_BABY_SOUL_LINKER: return MAPID_BABY_SOUL_LINKER;
  6042. //3-1 Jobs
  6043. case JOB_SUPER_NOVICE_E: return MAPID_SUPER_NOVICE_E;
  6044. case JOB_RUNE_KNIGHT: return MAPID_RUNE_KNIGHT;
  6045. case JOB_WARLOCK: return MAPID_WARLOCK;
  6046. case JOB_RANGER: return MAPID_RANGER;
  6047. case JOB_ARCH_BISHOP: return MAPID_ARCH_BISHOP;
  6048. case JOB_MECHANIC: return MAPID_MECHANIC;
  6049. case JOB_GUILLOTINE_CROSS: return MAPID_GUILLOTINE_CROSS;
  6050. case JOB_STAR_EMPEROR: return MAPID_STAR_EMPEROR;
  6051. //3-2 Jobs
  6052. case JOB_ROYAL_GUARD: return MAPID_ROYAL_GUARD;
  6053. case JOB_SORCERER: return MAPID_SORCERER;
  6054. case JOB_MINSTREL:
  6055. case JOB_WANDERER: return MAPID_MINSTRELWANDERER;
  6056. case JOB_SURA: return MAPID_SURA;
  6057. case JOB_GENETIC: return MAPID_GENETIC;
  6058. case JOB_SHADOW_CHASER: return MAPID_SHADOW_CHASER;
  6059. case JOB_SOUL_REAPER: return MAPID_SOUL_REAPER;
  6060. //Trans 3-1 Jobs
  6061. case JOB_RUNE_KNIGHT_T: return MAPID_RUNE_KNIGHT_T;
  6062. case JOB_WARLOCK_T: return MAPID_WARLOCK_T;
  6063. case JOB_RANGER_T: return MAPID_RANGER_T;
  6064. case JOB_ARCH_BISHOP_T: return MAPID_ARCH_BISHOP_T;
  6065. case JOB_MECHANIC_T: return MAPID_MECHANIC_T;
  6066. case JOB_GUILLOTINE_CROSS_T: return MAPID_GUILLOTINE_CROSS_T;
  6067. //Trans 3-2 Jobs
  6068. case JOB_ROYAL_GUARD_T: return MAPID_ROYAL_GUARD_T;
  6069. case JOB_SORCERER_T: return MAPID_SORCERER_T;
  6070. case JOB_MINSTREL_T:
  6071. case JOB_WANDERER_T: return MAPID_MINSTRELWANDERER_T;
  6072. case JOB_SURA_T: return MAPID_SURA_T;
  6073. case JOB_GENETIC_T: return MAPID_GENETIC_T;
  6074. case JOB_SHADOW_CHASER_T: return MAPID_SHADOW_CHASER_T;
  6075. //Baby 3-1 Jobs
  6076. case JOB_SUPER_BABY_E: return MAPID_SUPER_BABY_E;
  6077. case JOB_BABY_RUNE_KNIGHT: return MAPID_BABY_RUNE_KNIGHT;
  6078. case JOB_BABY_WARLOCK: return MAPID_BABY_WARLOCK;
  6079. case JOB_BABY_RANGER: return MAPID_BABY_RANGER;
  6080. case JOB_BABY_ARCH_BISHOP: return MAPID_BABY_ARCH_BISHOP;
  6081. case JOB_BABY_MECHANIC: return MAPID_BABY_MECHANIC;
  6082. case JOB_BABY_GUILLOTINE_CROSS: return MAPID_BABY_GUILLOTINE_CROSS;
  6083. case JOB_BABY_STAR_EMPEROR: return MAPID_BABY_STAR_EMPEROR;
  6084. //Baby 3-2 Jobs
  6085. case JOB_BABY_ROYAL_GUARD: return MAPID_BABY_ROYAL_GUARD;
  6086. case JOB_BABY_SORCERER: return MAPID_BABY_SORCERER;
  6087. case JOB_BABY_MINSTREL:
  6088. case JOB_BABY_WANDERER: return MAPID_BABY_MINSTRELWANDERER;
  6089. case JOB_BABY_SURA: return MAPID_BABY_SURA;
  6090. case JOB_BABY_GENETIC: return MAPID_BABY_GENETIC;
  6091. case JOB_BABY_SHADOW_CHASER: return MAPID_BABY_SHADOW_CHASER;
  6092. case JOB_BABY_SOUL_REAPER: return MAPID_BABY_SOUL_REAPER;
  6093. //Doram Jobs
  6094. case JOB_SUMMONER: return MAPID_SUMMONER;
  6095. case JOB_SPIRIT_HANDLER: return MAPID_SPIRIT_HANDLER;
  6096. //4-1 Jobs
  6097. case JOB_HYPER_NOVICE: return MAPID_HYPER_NOVICE;
  6098. case JOB_DRAGON_KNIGHT: return MAPID_DRAGON_KNIGHT;
  6099. case JOB_ARCH_MAGE: return MAPID_ARCH_MAGE;
  6100. case JOB_WINDHAWK: return MAPID_WINDHAWK;
  6101. case JOB_CARDINAL: return MAPID_CARDINAL;
  6102. case JOB_MEISTER: return MAPID_MEISTER;
  6103. case JOB_SHADOW_CROSS: return MAPID_SHADOW_CROSS;
  6104. case JOB_SKY_EMPEROR: return MAPID_SKY_EMPEROR;
  6105. case JOB_NIGHT_WATCH: return MAPID_NIGHT_WATCH;
  6106. case JOB_SHINKIRO:
  6107. case JOB_SHIRANUI: return MAPID_SHINKIRO_SHIRANUI;
  6108. //4-2 Jobs
  6109. case JOB_IMPERIAL_GUARD: return MAPID_IMPERIAL_GUARD;
  6110. case JOB_ELEMENTAL_MASTER: return MAPID_ELEMENTAL_MASTER;
  6111. case JOB_INQUISITOR: return MAPID_INQUISITOR;
  6112. case JOB_TROUBADOUR:
  6113. case JOB_TROUVERE: return MAPID_TROUBADOURTROUVERE;
  6114. case JOB_BIOLO: return MAPID_BIOLO;
  6115. case JOB_ABYSS_CHASER: return MAPID_ABYSS_CHASER;
  6116. case JOB_SOUL_ASCETIC: return MAPID_SOUL_ASCETIC;
  6117. //Unknown
  6118. default:
  6119. return -1;
  6120. }
  6121. }
  6122. //Reverts the map-style class id to the client-style one.
  6123. int pc_mapid2jobid(uint64 class_, int sex)
  6124. {
  6125. switch(class_) {
  6126. //Novice And 1-1 Jobs
  6127. case MAPID_NOVICE: return JOB_NOVICE;
  6128. case MAPID_SWORDMAN: return JOB_SWORDMAN;
  6129. case MAPID_MAGE: return JOB_MAGE;
  6130. case MAPID_ARCHER: return JOB_ARCHER;
  6131. case MAPID_ACOLYTE: return JOB_ACOLYTE;
  6132. case MAPID_MERCHANT: return JOB_MERCHANT;
  6133. case MAPID_THIEF: return JOB_THIEF;
  6134. case MAPID_TAEKWON: return JOB_TAEKWON;
  6135. case MAPID_WEDDING: return JOB_WEDDING;
  6136. case MAPID_GUNSLINGER: return JOB_GUNSLINGER;
  6137. case MAPID_NINJA: return JOB_NINJA;
  6138. case MAPID_XMAS: return JOB_XMAS;
  6139. case MAPID_SUMMER: return JOB_SUMMER;
  6140. case MAPID_HANBOK: return JOB_HANBOK;
  6141. case MAPID_GANGSI: return JOB_GANGSI;
  6142. case MAPID_OKTOBERFEST: return JOB_OKTOBERFEST;
  6143. case MAPID_SUMMER2: return JOB_SUMMER2;
  6144. //2-1 Jobs
  6145. case MAPID_SUPER_NOVICE: return JOB_SUPER_NOVICE;
  6146. case MAPID_KNIGHT: return JOB_KNIGHT;
  6147. case MAPID_WIZARD: return JOB_WIZARD;
  6148. case MAPID_HUNTER: return JOB_HUNTER;
  6149. case MAPID_PRIEST: return JOB_PRIEST;
  6150. case MAPID_BLACKSMITH: return JOB_BLACKSMITH;
  6151. case MAPID_ASSASSIN: return JOB_ASSASSIN;
  6152. case MAPID_STAR_GLADIATOR: return JOB_STAR_GLADIATOR;
  6153. case MAPID_KAGEROUOBORO: return sex?JOB_KAGEROU:JOB_OBORO;
  6154. case MAPID_REBELLION: return JOB_REBELLION;
  6155. case MAPID_DEATH_KNIGHT: return JOB_DEATH_KNIGHT;
  6156. //2-2 Jobs
  6157. case MAPID_CRUSADER: return JOB_CRUSADER;
  6158. case MAPID_SAGE: return JOB_SAGE;
  6159. case MAPID_BARDDANCER: return sex?JOB_BARD:JOB_DANCER;
  6160. case MAPID_MONK: return JOB_MONK;
  6161. case MAPID_ALCHEMIST: return JOB_ALCHEMIST;
  6162. case MAPID_ROGUE: return JOB_ROGUE;
  6163. case MAPID_SOUL_LINKER: return JOB_SOUL_LINKER;
  6164. case MAPID_DARK_COLLECTOR: return JOB_DARK_COLLECTOR;
  6165. //Trans Novice And Trans 2-1 Jobs
  6166. case MAPID_NOVICE_HIGH: return JOB_NOVICE_HIGH;
  6167. case MAPID_SWORDMAN_HIGH: return JOB_SWORDMAN_HIGH;
  6168. case MAPID_MAGE_HIGH: return JOB_MAGE_HIGH;
  6169. case MAPID_ARCHER_HIGH: return JOB_ARCHER_HIGH;
  6170. case MAPID_ACOLYTE_HIGH: return JOB_ACOLYTE_HIGH;
  6171. case MAPID_MERCHANT_HIGH: return JOB_MERCHANT_HIGH;
  6172. case MAPID_THIEF_HIGH: return JOB_THIEF_HIGH;
  6173. //Trans 2-1 Jobs
  6174. case MAPID_LORD_KNIGHT: return JOB_LORD_KNIGHT;
  6175. case MAPID_HIGH_WIZARD: return JOB_HIGH_WIZARD;
  6176. case MAPID_SNIPER: return JOB_SNIPER;
  6177. case MAPID_HIGH_PRIEST: return JOB_HIGH_PRIEST;
  6178. case MAPID_WHITESMITH: return JOB_WHITESMITH;
  6179. case MAPID_ASSASSIN_CROSS: return JOB_ASSASSIN_CROSS;
  6180. //Trans 2-2 Jobs
  6181. case MAPID_PALADIN: return JOB_PALADIN;
  6182. case MAPID_PROFESSOR: return JOB_PROFESSOR;
  6183. case MAPID_CLOWNGYPSY: return sex?JOB_CLOWN:JOB_GYPSY;
  6184. case MAPID_CHAMPION: return JOB_CHAMPION;
  6185. case MAPID_CREATOR: return JOB_CREATOR;
  6186. case MAPID_STALKER: return JOB_STALKER;
  6187. //Baby Novice And Baby 1-1 Jobs
  6188. case MAPID_BABY: return JOB_BABY;
  6189. case MAPID_BABY_SWORDMAN: return JOB_BABY_SWORDMAN;
  6190. case MAPID_BABY_MAGE: return JOB_BABY_MAGE;
  6191. case MAPID_BABY_ARCHER: return JOB_BABY_ARCHER;
  6192. case MAPID_BABY_ACOLYTE: return JOB_BABY_ACOLYTE;
  6193. case MAPID_BABY_MERCHANT: return JOB_BABY_MERCHANT;
  6194. case MAPID_BABY_THIEF: return JOB_BABY_THIEF;
  6195. case MAPID_BABY_TAEKWON: return JOB_BABY_TAEKWON;
  6196. case MAPID_BABY_GUNSLINGER: return JOB_BABY_GUNSLINGER;
  6197. case MAPID_BABY_NINJA: return JOB_BABY_NINJA;
  6198. case MAPID_BABY_SUMMONER: return JOB_BABY_SUMMONER;
  6199. //Baby 2-1 Jobs
  6200. case MAPID_SUPER_BABY: return JOB_SUPER_BABY;
  6201. case MAPID_BABY_KNIGHT: return JOB_BABY_KNIGHT;
  6202. case MAPID_BABY_WIZARD: return JOB_BABY_WIZARD;
  6203. case MAPID_BABY_HUNTER: return JOB_BABY_HUNTER;
  6204. case MAPID_BABY_PRIEST: return JOB_BABY_PRIEST;
  6205. case MAPID_BABY_BLACKSMITH: return JOB_BABY_BLACKSMITH;
  6206. case MAPID_BABY_ASSASSIN: return JOB_BABY_ASSASSIN;
  6207. case MAPID_BABY_STAR_GLADIATOR: return JOB_BABY_STAR_GLADIATOR;
  6208. case MAPID_BABY_REBELLION: return JOB_BABY_REBELLION;
  6209. case MAPID_BABY_KAGEROUOBORO: return sex?JOB_BABY_KAGEROU:JOB_BABY_OBORO;
  6210. //Baby 2-2 Jobs
  6211. case MAPID_BABY_CRUSADER: return JOB_BABY_CRUSADER;
  6212. case MAPID_BABY_SAGE: return JOB_BABY_SAGE;
  6213. case MAPID_BABY_BARDDANCER: return sex?JOB_BABY_BARD:JOB_BABY_DANCER;
  6214. case MAPID_BABY_MONK: return JOB_BABY_MONK;
  6215. case MAPID_BABY_ALCHEMIST: return JOB_BABY_ALCHEMIST;
  6216. case MAPID_BABY_ROGUE: return JOB_BABY_ROGUE;
  6217. case MAPID_BABY_SOUL_LINKER: return JOB_BABY_SOUL_LINKER;
  6218. //3-1 Jobs
  6219. case MAPID_SUPER_NOVICE_E: return JOB_SUPER_NOVICE_E;
  6220. case MAPID_RUNE_KNIGHT: return JOB_RUNE_KNIGHT;
  6221. case MAPID_WARLOCK: return JOB_WARLOCK;
  6222. case MAPID_RANGER: return JOB_RANGER;
  6223. case MAPID_ARCH_BISHOP: return JOB_ARCH_BISHOP;
  6224. case MAPID_MECHANIC: return JOB_MECHANIC;
  6225. case MAPID_GUILLOTINE_CROSS: return JOB_GUILLOTINE_CROSS;
  6226. case MAPID_STAR_EMPEROR: return JOB_STAR_EMPEROR;
  6227. //3-2 Jobs
  6228. case MAPID_ROYAL_GUARD: return JOB_ROYAL_GUARD;
  6229. case MAPID_SORCERER: return JOB_SORCERER;
  6230. case MAPID_MINSTRELWANDERER: return sex?JOB_MINSTREL:JOB_WANDERER;
  6231. case MAPID_SURA: return JOB_SURA;
  6232. case MAPID_GENETIC: return JOB_GENETIC;
  6233. case MAPID_SHADOW_CHASER: return JOB_SHADOW_CHASER;
  6234. case MAPID_SOUL_REAPER: return JOB_SOUL_REAPER;
  6235. //Trans 3-1 Jobs
  6236. case MAPID_RUNE_KNIGHT_T: return JOB_RUNE_KNIGHT_T;
  6237. case MAPID_WARLOCK_T: return JOB_WARLOCK_T;
  6238. case MAPID_RANGER_T: return JOB_RANGER_T;
  6239. case MAPID_ARCH_BISHOP_T: return JOB_ARCH_BISHOP_T;
  6240. case MAPID_MECHANIC_T: return JOB_MECHANIC_T;
  6241. case MAPID_GUILLOTINE_CROSS_T: return JOB_GUILLOTINE_CROSS_T;
  6242. //Trans 3-2 Jobs
  6243. case MAPID_ROYAL_GUARD_T: return JOB_ROYAL_GUARD_T;
  6244. case MAPID_SORCERER_T: return JOB_SORCERER_T;
  6245. case MAPID_MINSTRELWANDERER_T: return sex?JOB_MINSTREL_T:JOB_WANDERER_T;
  6246. case MAPID_SURA_T: return JOB_SURA_T;
  6247. case MAPID_GENETIC_T: return JOB_GENETIC_T;
  6248. case MAPID_SHADOW_CHASER_T: return JOB_SHADOW_CHASER_T;
  6249. //Baby 3-1 Jobs
  6250. case MAPID_SUPER_BABY_E: return JOB_SUPER_BABY_E;
  6251. case MAPID_BABY_RUNE_KNIGHT: return JOB_BABY_RUNE_KNIGHT;
  6252. case MAPID_BABY_WARLOCK: return JOB_BABY_WARLOCK;
  6253. case MAPID_BABY_RANGER: return JOB_BABY_RANGER;
  6254. case MAPID_BABY_ARCH_BISHOP: return JOB_BABY_ARCH_BISHOP;
  6255. case MAPID_BABY_MECHANIC: return JOB_BABY_MECHANIC;
  6256. case MAPID_BABY_GUILLOTINE_CROSS: return JOB_BABY_GUILLOTINE_CROSS;
  6257. case MAPID_BABY_STAR_EMPEROR: return JOB_BABY_STAR_EMPEROR;
  6258. //Baby 3-2 Jobs
  6259. case MAPID_BABY_ROYAL_GUARD: return JOB_BABY_ROYAL_GUARD;
  6260. case MAPID_BABY_SORCERER: return JOB_BABY_SORCERER;
  6261. case MAPID_BABY_MINSTRELWANDERER: return sex?JOB_BABY_MINSTREL:JOB_BABY_WANDERER;
  6262. case MAPID_BABY_SURA: return JOB_BABY_SURA;
  6263. case MAPID_BABY_GENETIC: return JOB_BABY_GENETIC;
  6264. case MAPID_BABY_SHADOW_CHASER: return JOB_BABY_SHADOW_CHASER;
  6265. case MAPID_BABY_SOUL_REAPER: return JOB_BABY_SOUL_REAPER;
  6266. //Doram Jobs
  6267. case MAPID_SUMMONER: return JOB_SUMMONER;
  6268. case MAPID_SPIRIT_HANDLER: return JOB_SPIRIT_HANDLER;
  6269. //4-1 Jobs
  6270. case MAPID_HYPER_NOVICE: return JOB_HYPER_NOVICE;
  6271. case MAPID_DRAGON_KNIGHT: return JOB_DRAGON_KNIGHT;
  6272. case MAPID_ARCH_MAGE: return JOB_ARCH_MAGE;
  6273. case MAPID_WINDHAWK: return JOB_WINDHAWK;
  6274. case MAPID_CARDINAL: return JOB_CARDINAL;
  6275. case MAPID_MEISTER: return JOB_MEISTER;
  6276. case MAPID_SHADOW_CROSS: return JOB_SHADOW_CROSS;
  6277. case MAPID_SKY_EMPEROR: return JOB_SKY_EMPEROR;
  6278. case MAPID_NIGHT_WATCH: return JOB_NIGHT_WATCH;
  6279. case MAPID_SHINKIRO_SHIRANUI: return sex?JOB_SHINKIRO:JOB_SHIRANUI;
  6280. //4-2 Jobs
  6281. case MAPID_IMPERIAL_GUARD: return JOB_IMPERIAL_GUARD;
  6282. case MAPID_ELEMENTAL_MASTER: return JOB_ELEMENTAL_MASTER;
  6283. case MAPID_INQUISITOR: return JOB_INQUISITOR;
  6284. case MAPID_TROUBADOURTROUVERE: return sex?JOB_TROUBADOUR:JOB_TROUVERE;
  6285. case MAPID_BIOLO: return JOB_BIOLO;
  6286. case MAPID_ABYSS_CHASER: return JOB_ABYSS_CHASER;
  6287. case MAPID_SOUL_ASCETIC: return JOB_SOUL_ASCETIC;
  6288. //Unknown
  6289. default:
  6290. return -1;
  6291. }
  6292. }
  6293. /*====================================================
  6294. * This function return the name of the job (by [Yor])
  6295. *----------------------------------------------------*/
  6296. const char* job_name(int class_)
  6297. {
  6298. switch (class_) {
  6299. case JOB_NOVICE:
  6300. case JOB_SWORDMAN:
  6301. case JOB_MAGE:
  6302. case JOB_ARCHER:
  6303. case JOB_ACOLYTE:
  6304. case JOB_MERCHANT:
  6305. case JOB_THIEF:
  6306. return msg_txt(NULL,550 - JOB_NOVICE+class_);
  6307. case JOB_KNIGHT:
  6308. case JOB_PRIEST:
  6309. case JOB_WIZARD:
  6310. case JOB_BLACKSMITH:
  6311. case JOB_HUNTER:
  6312. case JOB_ASSASSIN:
  6313. return msg_txt(NULL,557 - JOB_KNIGHT+class_);
  6314. case JOB_KNIGHT2:
  6315. return msg_txt(NULL,557);
  6316. case JOB_CRUSADER:
  6317. case JOB_MONK:
  6318. case JOB_SAGE:
  6319. case JOB_ROGUE:
  6320. case JOB_ALCHEMIST:
  6321. case JOB_BARD:
  6322. case JOB_DANCER:
  6323. return msg_txt(NULL,563 - JOB_CRUSADER+class_);
  6324. case JOB_CRUSADER2:
  6325. return msg_txt(NULL,563);
  6326. case JOB_WEDDING:
  6327. case JOB_SUPER_NOVICE:
  6328. case JOB_GUNSLINGER:
  6329. case JOB_NINJA:
  6330. case JOB_XMAS:
  6331. return msg_txt(NULL,570 - JOB_WEDDING+class_);
  6332. case JOB_SUMMER:
  6333. case JOB_SUMMER2:
  6334. return msg_txt(NULL,621);
  6335. case JOB_HANBOK:
  6336. return msg_txt(NULL,694);
  6337. case JOB_OKTOBERFEST:
  6338. return msg_txt(NULL,696);
  6339. case JOB_NOVICE_HIGH:
  6340. case JOB_SWORDMAN_HIGH:
  6341. case JOB_MAGE_HIGH:
  6342. case JOB_ARCHER_HIGH:
  6343. case JOB_ACOLYTE_HIGH:
  6344. case JOB_MERCHANT_HIGH:
  6345. case JOB_THIEF_HIGH:
  6346. return msg_txt(NULL,575 - JOB_NOVICE_HIGH+class_);
  6347. case JOB_LORD_KNIGHT:
  6348. case JOB_HIGH_PRIEST:
  6349. case JOB_HIGH_WIZARD:
  6350. case JOB_WHITESMITH:
  6351. case JOB_SNIPER:
  6352. case JOB_ASSASSIN_CROSS:
  6353. return msg_txt(NULL,582 - JOB_LORD_KNIGHT+class_);
  6354. case JOB_LORD_KNIGHT2:
  6355. return msg_txt(NULL,582);
  6356. case JOB_PALADIN:
  6357. case JOB_CHAMPION:
  6358. case JOB_PROFESSOR:
  6359. case JOB_STALKER:
  6360. case JOB_CREATOR:
  6361. case JOB_CLOWN:
  6362. case JOB_GYPSY:
  6363. return msg_txt(NULL,588 - JOB_PALADIN + class_);
  6364. case JOB_PALADIN2:
  6365. return msg_txt(NULL,588);
  6366. case JOB_BABY:
  6367. case JOB_BABY_SWORDMAN:
  6368. case JOB_BABY_MAGE:
  6369. case JOB_BABY_ARCHER:
  6370. case JOB_BABY_ACOLYTE:
  6371. case JOB_BABY_MERCHANT:
  6372. case JOB_BABY_THIEF:
  6373. return msg_txt(NULL,595 - JOB_BABY + class_);
  6374. case JOB_BABY_KNIGHT:
  6375. case JOB_BABY_PRIEST:
  6376. case JOB_BABY_WIZARD:
  6377. case JOB_BABY_BLACKSMITH:
  6378. case JOB_BABY_HUNTER:
  6379. case JOB_BABY_ASSASSIN:
  6380. return msg_txt(NULL,602 - JOB_BABY_KNIGHT + class_);
  6381. case JOB_BABY_KNIGHT2:
  6382. return msg_txt(NULL,602);
  6383. case JOB_BABY_CRUSADER:
  6384. case JOB_BABY_MONK:
  6385. case JOB_BABY_SAGE:
  6386. case JOB_BABY_ROGUE:
  6387. case JOB_BABY_ALCHEMIST:
  6388. case JOB_BABY_BARD:
  6389. case JOB_BABY_DANCER:
  6390. return msg_txt(NULL,608 - JOB_BABY_CRUSADER + class_);
  6391. case JOB_BABY_CRUSADER2:
  6392. return msg_txt(NULL,608);
  6393. case JOB_SUPER_BABY:
  6394. return msg_txt(NULL,615);
  6395. case JOB_TAEKWON:
  6396. return msg_txt(NULL,616);
  6397. case JOB_STAR_GLADIATOR:
  6398. case JOB_STAR_GLADIATOR2:
  6399. return msg_txt(NULL,617);
  6400. case JOB_SOUL_LINKER:
  6401. return msg_txt(NULL,618);
  6402. case JOB_GANGSI:
  6403. case JOB_DEATH_KNIGHT:
  6404. case JOB_DARK_COLLECTOR:
  6405. return msg_txt(NULL,622 - JOB_GANGSI+class_);
  6406. case JOB_RUNE_KNIGHT:
  6407. case JOB_WARLOCK:
  6408. case JOB_RANGER:
  6409. case JOB_ARCH_BISHOP:
  6410. case JOB_MECHANIC:
  6411. case JOB_GUILLOTINE_CROSS:
  6412. return msg_txt(NULL,625 - JOB_RUNE_KNIGHT+class_);
  6413. case JOB_RUNE_KNIGHT_T:
  6414. case JOB_WARLOCK_T:
  6415. case JOB_RANGER_T:
  6416. case JOB_ARCH_BISHOP_T:
  6417. case JOB_MECHANIC_T:
  6418. case JOB_GUILLOTINE_CROSS_T:
  6419. return msg_txt(NULL,681 - JOB_RUNE_KNIGHT_T+class_);
  6420. case JOB_ROYAL_GUARD:
  6421. case JOB_SORCERER:
  6422. case JOB_MINSTREL:
  6423. case JOB_WANDERER:
  6424. case JOB_SURA:
  6425. case JOB_GENETIC:
  6426. case JOB_SHADOW_CHASER:
  6427. return msg_txt(NULL,631 - JOB_ROYAL_GUARD+class_);
  6428. case JOB_ROYAL_GUARD_T:
  6429. case JOB_SORCERER_T:
  6430. case JOB_MINSTREL_T:
  6431. case JOB_WANDERER_T:
  6432. case JOB_SURA_T:
  6433. case JOB_GENETIC_T:
  6434. case JOB_SHADOW_CHASER_T:
  6435. return msg_txt(NULL,687 - JOB_ROYAL_GUARD_T+class_);
  6436. case JOB_RUNE_KNIGHT2:
  6437. case JOB_RUNE_KNIGHT_T2:
  6438. return msg_txt(NULL,625);
  6439. case JOB_ROYAL_GUARD2:
  6440. case JOB_ROYAL_GUARD_T2:
  6441. return msg_txt(NULL,631);
  6442. case JOB_RANGER2:
  6443. case JOB_RANGER_T2:
  6444. return msg_txt(NULL,627);
  6445. case JOB_MECHANIC2:
  6446. case JOB_MECHANIC_T2:
  6447. return msg_txt(NULL,629);
  6448. case JOB_BABY_RUNE_KNIGHT:
  6449. case JOB_BABY_WARLOCK:
  6450. case JOB_BABY_RANGER:
  6451. case JOB_BABY_ARCH_BISHOP:
  6452. case JOB_BABY_MECHANIC:
  6453. case JOB_BABY_GUILLOTINE_CROSS:
  6454. case JOB_BABY_ROYAL_GUARD:
  6455. case JOB_BABY_SORCERER:
  6456. case JOB_BABY_MINSTREL:
  6457. case JOB_BABY_WANDERER:
  6458. case JOB_BABY_SURA:
  6459. case JOB_BABY_GENETIC:
  6460. case JOB_BABY_SHADOW_CHASER:
  6461. return msg_txt(NULL,638 - JOB_BABY_RUNE_KNIGHT+class_);
  6462. case JOB_BABY_RUNE_KNIGHT2:
  6463. return msg_txt(NULL,638);
  6464. case JOB_BABY_ROYAL_GUARD2:
  6465. return msg_txt(NULL,644);
  6466. case JOB_BABY_RANGER2:
  6467. return msg_txt(NULL,640);
  6468. case JOB_BABY_MECHANIC2:
  6469. return msg_txt(NULL,642);
  6470. case JOB_SUPER_NOVICE_E:
  6471. case JOB_SUPER_BABY_E:
  6472. return msg_txt(NULL,651 - JOB_SUPER_NOVICE_E+class_);
  6473. case JOB_KAGEROU:
  6474. case JOB_OBORO:
  6475. return msg_txt(NULL,653 - JOB_KAGEROU+class_);
  6476. case JOB_REBELLION:
  6477. return msg_txt(NULL,695);
  6478. case JOB_SUMMONER:
  6479. return msg_txt(NULL,697);
  6480. case JOB_BABY_SUMMONER:
  6481. return msg_txt(NULL,698);
  6482. case JOB_BABY_NINJA:
  6483. return msg_txt(NULL,699);
  6484. case JOB_BABY_KAGEROU:
  6485. case JOB_BABY_OBORO:
  6486. case JOB_BABY_TAEKWON:
  6487. case JOB_BABY_STAR_GLADIATOR:
  6488. case JOB_BABY_SOUL_LINKER:
  6489. case JOB_BABY_GUNSLINGER:
  6490. case JOB_BABY_REBELLION:
  6491. return msg_txt(NULL,753 - JOB_BABY_KAGEROU+class_);
  6492. case JOB_BABY_STAR_GLADIATOR2:
  6493. return msg_txt(NULL,756);
  6494. case JOB_STAR_EMPEROR:
  6495. case JOB_SOUL_REAPER:
  6496. case JOB_BABY_STAR_EMPEROR:
  6497. case JOB_BABY_SOUL_REAPER:
  6498. return msg_txt(NULL,782 - JOB_STAR_EMPEROR + class_);
  6499. case JOB_STAR_EMPEROR2:
  6500. return msg_txt(NULL,782);
  6501. case JOB_BABY_STAR_EMPEROR2:
  6502. return msg_txt(NULL,784);
  6503. case JOB_DRAGON_KNIGHT:
  6504. case JOB_MEISTER:
  6505. case JOB_SHADOW_CROSS:
  6506. case JOB_ARCH_MAGE:
  6507. case JOB_CARDINAL:
  6508. case JOB_WINDHAWK:
  6509. case JOB_IMPERIAL_GUARD:
  6510. case JOB_BIOLO:
  6511. case JOB_ABYSS_CHASER:
  6512. case JOB_ELEMENTAL_MASTER:
  6513. case JOB_INQUISITOR:
  6514. case JOB_TROUBADOUR:
  6515. case JOB_TROUVERE:
  6516. return msg_txt( nullptr, 800 - JOB_DRAGON_KNIGHT + class_ );
  6517. case JOB_WINDHAWK2:
  6518. return msg_txt( nullptr, 805);
  6519. case JOB_MEISTER2:
  6520. return msg_txt( nullptr, 801 );
  6521. case JOB_DRAGON_KNIGHT2:
  6522. return msg_txt( nullptr, 800 );
  6523. case JOB_IMPERIAL_GUARD2:
  6524. return msg_txt( nullptr, 806 );
  6525. case JOB_SKY_EMPEROR:
  6526. case JOB_SOUL_ASCETIC:
  6527. case JOB_SHINKIRO:
  6528. case JOB_SHIRANUI:
  6529. case JOB_NIGHT_WATCH:
  6530. case JOB_HYPER_NOVICE:
  6531. case JOB_SPIRIT_HANDLER:
  6532. return msg_txt( nullptr, 813 - JOB_SKY_EMPEROR + class_ );
  6533. case JOB_SKY_EMPEROR2:
  6534. return msg_txt( nullptr, 813 );
  6535. default:
  6536. return msg_txt(NULL,655);
  6537. }
  6538. }
  6539. /*====================================================
  6540. * Timered function to make id follow a target.
  6541. * @id = bl.id (player only atm)
  6542. * target is define in sd->followtarget (bl.id)
  6543. * used by pc_follow
  6544. *----------------------------------------------------*/
  6545. TIMER_FUNC(pc_follow_timer){
  6546. struct map_session_data *sd;
  6547. struct block_list *tbl;
  6548. sd = map_id2sd(id);
  6549. nullpo_ret(sd);
  6550. if (sd->followtimer != tid){
  6551. ShowError("pc_follow_timer %d != %d\n",sd->followtimer,tid);
  6552. sd->followtimer = INVALID_TIMER;
  6553. return 0;
  6554. }
  6555. sd->followtimer = INVALID_TIMER;
  6556. tbl = map_id2bl(sd->followtarget);
  6557. if (tbl == NULL || pc_isdead(sd))
  6558. {
  6559. pc_stop_following(sd);
  6560. return 0;
  6561. }
  6562. // either player or target is currently detached from map blocks (could be teleporting),
  6563. // but still connected to this map, so we'll just increment the timer and check back later
  6564. if (sd->bl.prev != NULL && tbl->prev != NULL &&
  6565. sd->ud.skilltimer == INVALID_TIMER && sd->ud.attacktimer == INVALID_TIMER && sd->ud.walktimer == INVALID_TIMER)
  6566. {
  6567. if((sd->bl.m == tbl->m) && unit_can_reach_bl(&sd->bl,tbl, AREA_SIZE, 0, NULL, NULL)) {
  6568. if (!check_distance_bl(&sd->bl, tbl, 5))
  6569. unit_walktobl(&sd->bl, tbl, 5, 0);
  6570. } else
  6571. pc_setpos(sd, map_id2index(tbl->m), tbl->x, tbl->y, CLR_TELEPORT);
  6572. }
  6573. sd->followtimer = add_timer(
  6574. tick + 1000, // increase time a bit to loosen up map's load
  6575. pc_follow_timer, sd->bl.id, 0);
  6576. return 0;
  6577. }
  6578. int pc_stop_following (struct map_session_data *sd)
  6579. {
  6580. nullpo_ret(sd);
  6581. if (sd->followtimer != INVALID_TIMER) {
  6582. delete_timer(sd->followtimer,pc_follow_timer);
  6583. sd->followtimer = INVALID_TIMER;
  6584. }
  6585. sd->followtarget = -1;
  6586. sd->ud.target_to = 0;
  6587. unit_stop_walking(&sd->bl, 1);
  6588. return 0;
  6589. }
  6590. int pc_follow(struct map_session_data *sd,int target_id)
  6591. {
  6592. struct block_list *bl = map_id2bl(target_id);
  6593. if (bl == NULL /*|| bl->type != BL_PC*/)
  6594. return 1;
  6595. if (sd->followtimer != INVALID_TIMER)
  6596. pc_stop_following(sd);
  6597. sd->followtarget = target_id;
  6598. pc_follow_timer(INVALID_TIMER, gettick(), sd->bl.id, 0);
  6599. return 0;
  6600. }
  6601. int pc_checkbaselevelup(struct map_session_data *sd) {
  6602. t_exp next = pc_nextbaseexp(sd);
  6603. if (!next || sd->status.base_exp < next || pc_is_maxbaselv(sd))
  6604. return 0;
  6605. uint32 base_level = sd->status.base_level;
  6606. do {
  6607. sd->status.base_exp -= next;
  6608. //Kyoki pointed out that the max overcarry exp is the exp needed for the previous level -1. [Skotlex]
  6609. if( ( !battle_config.multi_level_up || ( battle_config.multi_level_up_base > 0 && sd->status.base_level >= battle_config.multi_level_up_base ) ) && sd->status.base_exp > next-1 )
  6610. sd->status.base_exp = next-1;
  6611. sd->status.status_point += statpoint_db.pc_gets_status_point(sd->status.base_level);
  6612. sd->status.trait_point += statpoint_db.pc_gets_trait_point(sd->status.base_level);
  6613. sd->status.base_level++;
  6614. if( pc_is_maxbaselv(sd) ){
  6615. sd->status.base_exp = u64min(sd->status.base_exp,MAX_LEVEL_BASE_EXP);
  6616. break;
  6617. }
  6618. } while ((next=pc_nextbaseexp(sd)) > 0 && sd->status.base_exp >= next);
  6619. if (battle_config.pet_lv_rate && sd->pd) //<Skotlex> update pet's level
  6620. status_calc_pet(sd->pd,SCO_NONE);
  6621. clif_updatestatus(sd,SP_STATUSPOINT);
  6622. clif_updatestatus(sd,SP_TRAITPOINT);
  6623. clif_updatestatus(sd,SP_BASELEVEL);
  6624. clif_updatestatus(sd,SP_BASEEXP);
  6625. clif_updatestatus(sd,SP_NEXTBASEEXP);
  6626. status_calc_pc(sd,SCO_FORCE);
  6627. status_percent_heal(&sd->bl,100,100);
  6628. if ((sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE) {
  6629. for (const auto &status : status_db) {
  6630. if (status.second->flag[SCF_SUPERNOVICEANGEL])
  6631. sc_start(&sd->bl, &sd->bl, status.second->type, 100, 1, skill_get_time(status.second->skill_id, 1));
  6632. }
  6633. if (sd->state.snovice_dead_flag)
  6634. sd->state.snovice_dead_flag = 0; //Reenable steelbody resurrection on dead.
  6635. } else if( (sd->class_&MAPID_BASEMASK) == MAPID_TAEKWON ) {
  6636. for (const auto &status : status_db) {
  6637. if (status.second->flag[SCF_TAEKWONANGEL])
  6638. sc_start(&sd->bl, &sd->bl, status.second->type, 100, 10, 600000);
  6639. }
  6640. }
  6641. clif_misceffect(&sd->bl,0);
  6642. npc_script_event(sd, NPCE_BASELVUP); //LORDALFA - LVLUPEVENT
  6643. if(sd->status.party_id)
  6644. party_send_levelup(sd);
  6645. pc_baselevelchanged(sd);
  6646. for (; base_level <= sd->status.base_level; base_level++) {
  6647. achievement_update_objective(sd, AG_GOAL_LEVEL, 1, base_level);
  6648. achievement_update_objective(sd, AG_GOAL_STATUS, 2, base_level, sd->status.class_);
  6649. }
  6650. return 1;
  6651. }
  6652. void pc_baselevelchanged(struct map_session_data *sd) {
  6653. uint8 i;
  6654. for( i = 0; i < EQI_MAX; i++ ) {
  6655. if( sd->equip_index[i] >= 0 && sd->inventory_data[sd->equip_index[i]] ) {
  6656. if( sd->inventory_data[ sd->equip_index[i] ]->elvmax && sd->status.base_level > (unsigned int)sd->inventory_data[ sd->equip_index[i] ]->elvmax )
  6657. pc_unequipitem(sd, sd->equip_index[i], 3);
  6658. }
  6659. }
  6660. pc_show_questinfo(sd);
  6661. }
  6662. int pc_checkjoblevelup(struct map_session_data *sd)
  6663. {
  6664. t_exp next = pc_nextjobexp(sd);
  6665. nullpo_ret(sd);
  6666. if(!next || sd->status.job_exp < next || pc_is_maxjoblv(sd))
  6667. return 0;
  6668. uint32 job_level = sd->status.job_level;
  6669. do {
  6670. sd->status.job_exp -= next;
  6671. //Kyoki pointed out that the max overcarry exp is the exp needed for the previous level -1. [Skotlex]
  6672. if( ( !battle_config.multi_level_up || ( battle_config.multi_level_up_job > 0 && sd->status.job_level >= battle_config.multi_level_up_job ) ) && sd->status.job_exp > next-1 )
  6673. sd->status.job_exp = next-1;
  6674. sd->status.job_level ++;
  6675. sd->status.skill_point ++;
  6676. if( pc_is_maxjoblv(sd) ){
  6677. sd->status.job_exp = u64min(sd->status.job_exp,MAX_LEVEL_JOB_EXP);
  6678. break;
  6679. }
  6680. } while ((next=pc_nextjobexp(sd)) > 0 && sd->status.job_exp >= next);
  6681. clif_updatestatus(sd,SP_JOBLEVEL);
  6682. clif_updatestatus(sd,SP_JOBEXP);
  6683. clif_updatestatus(sd,SP_NEXTJOBEXP);
  6684. clif_updatestatus(sd,SP_SKILLPOINT);
  6685. status_calc_pc(sd,SCO_FORCE);
  6686. clif_misceffect(&sd->bl,1);
  6687. if (pc_checkskill(sd, SG_DEVIL) && ((sd->class_&MAPID_THIRDMASK) == MAPID_STAR_EMPEROR || pc_is_maxjoblv(sd)) )
  6688. clif_status_change(&sd->bl, EFST_DEVIL1, 1, 0, 0, 0, 1); //Permanent blind effect from SG_DEVIL.
  6689. npc_script_event(sd, NPCE_JOBLVUP);
  6690. for (; job_level <= sd->status.job_level; job_level++)
  6691. achievement_update_objective(sd, AG_GOAL_LEVEL, 1, job_level);
  6692. pc_show_questinfo(sd);
  6693. return 1;
  6694. }
  6695. /** Alters experiences calculation based on self bonuses that do not get even shared to the party.
  6696. * @param sd Player
  6697. * @param base_exp Base EXP before peronal bonuses
  6698. * @param job_exp Job EXP before peronal bonuses
  6699. * @param src Block list that affecting the exp calculation
  6700. */
  6701. static void pc_calcexp(struct map_session_data *sd, t_exp *base_exp, t_exp *job_exp, struct block_list *src)
  6702. {
  6703. int bonus = 0, vip_bonus_base = 0, vip_bonus_job = 0;
  6704. if (src) {
  6705. struct status_data *status = status_get_status_data(src);
  6706. if( sd->indexed_bonus.expaddrace[status->race] )
  6707. bonus += sd->indexed_bonus.expaddrace[status->race];
  6708. if( sd->indexed_bonus.expaddrace[RC_ALL] )
  6709. bonus += sd->indexed_bonus.expaddrace[RC_ALL];
  6710. if( sd->indexed_bonus.expaddclass[status->class_] )
  6711. bonus += sd->indexed_bonus.expaddclass[status->class_];
  6712. if( sd->indexed_bonus.expaddclass[CLASS_ALL] )
  6713. bonus += sd->indexed_bonus.expaddclass[CLASS_ALL];
  6714. if (battle_config.pk_mode &&
  6715. (int)(status_get_lv(src) - sd->status.base_level) >= 20)
  6716. bonus += 15; // pk_mode additional exp if monster >20 levels [Valaris]
  6717. if (src && src->type == BL_MOB && pc_isvip(sd)) { // EXP bonus for VIP player
  6718. vip_bonus_base = battle_config.vip_base_exp_increase;
  6719. vip_bonus_job = battle_config.vip_job_exp_increase;
  6720. }
  6721. }
  6722. // Give EXPBOOST for quests even if src is NULL.
  6723. if (sd->sc.data[SC_EXPBOOST]) {
  6724. bonus += sd->sc.data[SC_EXPBOOST]->val1;
  6725. if (battle_config.vip_bm_increase && pc_isvip(sd)) // Increase Battle Manual EXP rate for VIP
  6726. bonus += (sd->sc.data[SC_EXPBOOST]->val1 / battle_config.vip_bm_increase);
  6727. }
  6728. if (*base_exp) {
  6729. t_exp exp = (t_exp)(*base_exp + ((double)*base_exp * ((bonus + vip_bonus_base) / 100.)));
  6730. *base_exp = cap_value(exp, 1, MAX_EXP);
  6731. }
  6732. // Give JEXPBOOST for quests even if src is NULL.
  6733. if (sd->sc.data[SC_JEXPBOOST])
  6734. bonus += sd->sc.data[SC_JEXPBOOST]->val1;
  6735. if (*job_exp) {
  6736. t_exp exp = (t_exp)(*job_exp + ((double)*job_exp * ((bonus + vip_bonus_job) / 100.)));
  6737. *job_exp = cap_value(exp, 1, MAX_EXP);
  6738. }
  6739. return;
  6740. }
  6741. /**
  6742. * Show EXP gained by player in percentage by @showexp
  6743. * @param sd Player
  6744. * @param base_exp Base EXP gained/loss
  6745. * @param next_base_exp Base EXP needed for next base level
  6746. * @param job_exp Job EXP gained/loss
  6747. * @param next_job_exp Job EXP needed for next job level
  6748. * @param lost True:EXP penalty, lose EXP
  6749. **/
  6750. void pc_gainexp_disp(struct map_session_data *sd, t_exp base_exp, t_exp next_base_exp, t_exp job_exp, t_exp next_job_exp, bool lost) {
  6751. char output[CHAT_SIZE_MAX];
  6752. nullpo_retv(sd);
  6753. sprintf(output, msg_txt(sd,743), // Experience %s Base:%ld (%0.2f%%) Job:%ld (%0.2f%%)
  6754. (lost) ? msg_txt(sd,742) : msg_txt(sd,741),
  6755. (long)base_exp * (lost ? -1 : 1), (base_exp / (float)next_base_exp * 100 * (lost ? -1 : 1)),
  6756. (long)job_exp * (lost ? -1 : 1), (job_exp / (float)next_job_exp * 100 * (lost ? -1 : 1)));
  6757. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  6758. }
  6759. /**
  6760. * Give Base or Job EXP to player, then calculate remaining exp for next lvl
  6761. * @param sd Player
  6762. * @param src EXP source
  6763. * @param base_exp Base EXP gained
  6764. * @param base_exp Job EXP gained
  6765. * @param exp_flag 1: Quest EXP; 2: Param Exp (Ignore Guild EXP tax, EXP adjustments)
  6766. * @return
  6767. **/
  6768. void pc_gainexp(struct map_session_data *sd, struct block_list *src, t_exp base_exp, t_exp job_exp, uint8 exp_flag)
  6769. {
  6770. t_exp nextb = 0, nextj = 0;
  6771. uint8 flag = 0; ///< 1: Base EXP given, 2: Job EXP given, 4: Max Base level, 8: Max Job Level
  6772. nullpo_retv(sd);
  6773. if(sd->bl.prev == NULL || pc_isdead(sd))
  6774. return;
  6775. if (!(exp_flag&2)) {
  6776. if (!battle_config.pvp_exp && map_getmapflag(sd->bl.m, MF_PVP)) // [MouseJstr]
  6777. return; // no exp on pvp maps
  6778. if (sd->status.guild_id>0)
  6779. base_exp -= guild_payexp(sd,base_exp);
  6780. }
  6781. flag = ((base_exp) ? 1 : 0) |
  6782. ((job_exp) ? 2 : 0) |
  6783. ((pc_is_maxbaselv(sd)) ? 4 : 0) |
  6784. ((pc_is_maxjoblv(sd)) ? 8 : 0);
  6785. if (!(exp_flag&2))
  6786. pc_calcexp(sd, &base_exp, &job_exp, src);
  6787. nextb = pc_nextbaseexp(sd);
  6788. nextj = pc_nextjobexp(sd);
  6789. if (flag&4){
  6790. if( sd->status.base_exp >= MAX_LEVEL_BASE_EXP )
  6791. base_exp = 0;
  6792. else if( sd->status.base_exp + base_exp >= MAX_LEVEL_BASE_EXP )
  6793. base_exp = MAX_LEVEL_BASE_EXP - sd->status.base_exp;
  6794. }
  6795. if (flag&8){
  6796. if( sd->status.job_exp >= MAX_LEVEL_JOB_EXP )
  6797. job_exp = 0;
  6798. else if( sd->status.job_exp + job_exp >= MAX_LEVEL_JOB_EXP )
  6799. job_exp = MAX_LEVEL_JOB_EXP - sd->status.job_exp;
  6800. }
  6801. if (!(exp_flag&2) && battle_config.max_exp_gain_rate && (base_exp || job_exp)) {
  6802. //Note that this value should never be greater than the original
  6803. //therefore no overflow checks are needed. [Skotlex]
  6804. if (nextb > 0) {
  6805. float nextbp = (float) base_exp / (float) nextb;
  6806. if (nextbp > battle_config.max_exp_gain_rate/1000.)
  6807. base_exp = (unsigned int)(battle_config.max_exp_gain_rate/1000.*nextb);
  6808. }
  6809. if (nextj > 0) {
  6810. float nextjp = (float) job_exp / (float) nextj;
  6811. if (nextjp > battle_config.max_exp_gain_rate/1000.)
  6812. job_exp = (unsigned int)(battle_config.max_exp_gain_rate/1000.*nextj);
  6813. }
  6814. }
  6815. // Give EXP for Base Level
  6816. if (base_exp) {
  6817. sd->status.base_exp = util::safe_addition_cap(sd->status.base_exp, base_exp, MAX_EXP);
  6818. if (!pc_checkbaselevelup(sd))
  6819. clif_updatestatus(sd,SP_BASEEXP);
  6820. }
  6821. // Give EXP for Job Level
  6822. if (job_exp) {
  6823. sd->status.job_exp = util::safe_addition_cap(sd->status.job_exp, job_exp, MAX_EXP);
  6824. if (!pc_checkjoblevelup(sd))
  6825. clif_updatestatus(sd,SP_JOBEXP);
  6826. }
  6827. if (flag&1)
  6828. clif_displayexp(sd, (flag&4) ? 0 : base_exp, SP_BASEEXP, exp_flag&1, false);
  6829. if (flag&2)
  6830. clif_displayexp(sd, (flag&8) ? 0 : job_exp, SP_JOBEXP, exp_flag&1, false);
  6831. if (sd->state.showexp && (base_exp || job_exp))
  6832. pc_gainexp_disp(sd, base_exp, nextb, job_exp, nextj, false);
  6833. }
  6834. /**
  6835. * Lost Base/Job EXP from a player
  6836. * @param sd Player
  6837. * @param base_exp Base EXP lost
  6838. * @param job_exp Job EXP lost
  6839. **/
  6840. void pc_lostexp(struct map_session_data *sd, t_exp base_exp, t_exp job_exp) {
  6841. nullpo_retv(sd);
  6842. if (base_exp) {
  6843. base_exp = u64min(sd->status.base_exp, base_exp);
  6844. sd->status.base_exp -= base_exp;
  6845. clif_displayexp(sd, base_exp, SP_BASEEXP, false, true);
  6846. clif_updatestatus(sd, SP_BASEEXP);
  6847. }
  6848. if (job_exp) {
  6849. job_exp = u64min(sd->status.job_exp, job_exp);
  6850. sd->status.job_exp -= job_exp;
  6851. clif_displayexp(sd, job_exp, SP_JOBEXP, false, true);
  6852. clif_updatestatus(sd, SP_JOBEXP);
  6853. }
  6854. if (sd->state.showexp && (base_exp || job_exp))
  6855. pc_gainexp_disp(sd, base_exp, pc_nextbaseexp(sd), job_exp, pc_nextjobexp(sd), true);
  6856. }
  6857. /**
  6858. * Returns max base level for this character's class.
  6859. * @param job_id: Player's class
  6860. * @return Max Base Level
  6861. */
  6862. uint32 JobDatabase::get_maxBaseLv(uint16 job_id) {
  6863. std::shared_ptr<s_job_info> job = job_db.find(job_id);
  6864. return job ? job->max_base_level : 0;
  6865. }
  6866. /**
  6867. * Returns max base level for this character.
  6868. * @param sd Player
  6869. * @return Max Base Level
  6870. **/
  6871. unsigned int pc_maxbaselv(struct map_session_data *sd){
  6872. return job_db.get_maxBaseLv(sd->status.class_);
  6873. }
  6874. /**
  6875. * Returns max job level for this character's class.
  6876. * @param job_id: Player's class
  6877. * @return Max Job Level
  6878. */
  6879. uint32 JobDatabase::get_maxJobLv(uint16 job_id) {
  6880. std::shared_ptr<s_job_info> job = job_db.find(job_id);
  6881. return job ? job->max_job_level : 0;
  6882. }
  6883. /**
  6884. * Returns max job level for this character.
  6885. * @param sd Player
  6886. * @return Max Job Level
  6887. **/
  6888. unsigned int pc_maxjoblv(struct map_session_data *sd){
  6889. return job_db.get_maxJobLv(sd->status.class_);
  6890. }
  6891. /**
  6892. * Check if player is reached max base level
  6893. * @param sd
  6894. * @return True if reached max level
  6895. **/
  6896. bool pc_is_maxbaselv(struct map_session_data *sd) {
  6897. nullpo_retr(false, sd);
  6898. return (sd->status.base_level >= pc_maxbaselv(sd));
  6899. }
  6900. /**
  6901. * Check if player is reached max base level
  6902. * @param sd
  6903. * @return True if reached max level
  6904. **/
  6905. bool pc_is_maxjoblv(struct map_session_data *sd) {
  6906. nullpo_retr(false, sd);
  6907. return (sd->status.job_level >= pc_maxjoblv(sd));
  6908. }
  6909. /**
  6910. * Returns base experience for this character's class.
  6911. * @param job_id: Player's class
  6912. * @param level: Player's level
  6913. * @return Base EXP
  6914. */
  6915. t_exp JobDatabase::get_baseExp(uint16 job_id, uint32 level) {
  6916. std::shared_ptr<s_job_info> job = job_db.find(job_id);
  6917. return job ? job->base_exp[level - 1] : 0;
  6918. }
  6919. /**
  6920. * Base exp needed for player to level up.
  6921. * @param sd
  6922. * @return Base EXP needed for next base level
  6923. **/
  6924. t_exp pc_nextbaseexp(struct map_session_data *sd){
  6925. nullpo_ret(sd);
  6926. if (sd->status.base_level == 0) // Is this something that possible?
  6927. return 0;
  6928. if (pc_is_maxbaselv(sd))
  6929. return MAX_LEVEL_BASE_EXP; // On max level, player's base EXP limit is 99,999,999
  6930. return static_cast<t_exp>(job_db.get_baseExp(sd->status.class_, sd->status.base_level));
  6931. }
  6932. /**
  6933. * Returns job experience for this character's class.
  6934. * @param job_id: Player's class
  6935. * @param level: Player's level
  6936. * @return Job EXP
  6937. */
  6938. t_exp JobDatabase::get_jobExp(uint16 job_id, uint32 level) {
  6939. std::shared_ptr<s_job_info> job = job_db.find(job_id);
  6940. return job ? job->job_exp[level - 1] : 0;
  6941. }
  6942. /**
  6943. * Job exp needed for player to level up.
  6944. * @param sd
  6945. * @return Job EXP needed for next job level
  6946. **/
  6947. t_exp pc_nextjobexp(struct map_session_data *sd){
  6948. nullpo_ret(sd);
  6949. if (sd->status.job_level == 0) // Is this something that possible?
  6950. return 0;
  6951. if (pc_is_maxjoblv(sd))
  6952. return MAX_LEVEL_JOB_EXP; // On max level, player's job EXP limit is 999,999,999
  6953. return static_cast<t_exp>(job_db.get_jobExp(sd->status.class_, sd->status.job_level));
  6954. }
  6955. /**
  6956. * Returns max weight base for this character's class.
  6957. * @param job_id: Player's class
  6958. * @return Max weight base
  6959. */
  6960. int32 JobDatabase::get_maxWeight(uint16 job_id) {
  6961. std::shared_ptr<s_job_info> job = job_db.find(job_id);
  6962. return job ? job->max_weight_base : 0;
  6963. }
  6964. /// Returns the value of the specified stat.
  6965. int pc_getstat(map_session_data *sd, int type)
  6966. {
  6967. nullpo_retr(-1, sd);
  6968. switch( type ) {
  6969. case SP_STR: return sd->status.str;
  6970. case SP_AGI: return sd->status.agi;
  6971. case SP_VIT: return sd->status.vit;
  6972. case SP_INT: return sd->status.int_;
  6973. case SP_DEX: return sd->status.dex;
  6974. case SP_LUK: return sd->status.luk;
  6975. case SP_POW: return sd->status.pow;
  6976. case SP_STA: return sd->status.sta;
  6977. case SP_WIS: return sd->status.wis;
  6978. case SP_SPL: return sd->status.spl;
  6979. case SP_CON: return sd->status.con;
  6980. case SP_CRT: return sd->status.crt;
  6981. default:
  6982. return -1;
  6983. }
  6984. }
  6985. /// Sets the specified stat to the specified value.
  6986. /// Returns the new value.
  6987. int pc_setstat(struct map_session_data* sd, int type, int val)
  6988. {
  6989. nullpo_retr(-1, sd);
  6990. switch( type ) {
  6991. case SP_STR: sd->status.str = val; break;
  6992. case SP_AGI: sd->status.agi = val; break;
  6993. case SP_VIT: sd->status.vit = val; break;
  6994. case SP_INT: sd->status.int_ = val; break;
  6995. case SP_DEX: sd->status.dex = val; break;
  6996. case SP_LUK: sd->status.luk = val; break;
  6997. case SP_POW: sd->status.pow = val; break;
  6998. case SP_STA: sd->status.sta = val; break;
  6999. case SP_WIS: sd->status.wis = val; break;
  7000. case SP_SPL: sd->status.spl = val; break;
  7001. case SP_CON: sd->status.con = val; break;
  7002. case SP_CRT: sd->status.crt = val; break;
  7003. default:
  7004. return -1;
  7005. }
  7006. return val;
  7007. }
  7008. /**
  7009. * Gets the total number of status points at the provided level.
  7010. * @param level: Player base level.
  7011. * @return Total number of status points at specific base level.
  7012. */
  7013. uint32 PlayerStatPointDatabase::get_table_point(uint16 level) {
  7014. std::shared_ptr<s_statpoint_entry> entry = this->find( level );
  7015. if( entry != nullptr ){
  7016. return entry->statpoints;
  7017. }else{
  7018. return 0;
  7019. }
  7020. }
  7021. /**
  7022. * Calculates the number of status points PC gets when leveling up (from level to level+1)
  7023. * @param level: Player base level.
  7024. * @param table: Use table value or formula.
  7025. * @return Status points at specific base level.
  7026. */
  7027. uint32 PlayerStatPointDatabase::pc_gets_status_point(uint16 level) {
  7028. uint32 next_level = this->get_table_point( level + 1 );
  7029. uint32 current_level = this->get_table_point( level );
  7030. if( next_level > current_level ){
  7031. return next_level - current_level;
  7032. }else{
  7033. return 0;
  7034. }
  7035. }
  7036. /**
  7037. * Gets the total number of trait points at the provided level.
  7038. * @param level: Player base level.
  7039. * @return Total number of trait points at specific base level.
  7040. */
  7041. uint32 PlayerStatPointDatabase::get_trait_table_point(uint16 level) {
  7042. std::shared_ptr<s_statpoint_entry> entry = this->find( level );
  7043. if( entry != nullptr ){
  7044. return entry->traitpoints;
  7045. }else{
  7046. return 0;
  7047. }
  7048. }
  7049. /**
  7050. * Calculates the number of trait points PC gets when leveling up (from level to level+1)
  7051. * @param level: Player base level.
  7052. * @param table: Use table value or formula.
  7053. * @return Trait points at specific base level.
  7054. */
  7055. uint32 PlayerStatPointDatabase::pc_gets_trait_point(uint16 level) {
  7056. uint32 next_level = this->get_trait_table_point( level + 1 );
  7057. uint32 current_level = this->get_trait_table_point( level );
  7058. if( next_level > current_level ){
  7059. return next_level - current_level;
  7060. }else{
  7061. return 0;
  7062. }
  7063. }
  7064. #ifdef RENEWAL_STAT
  7065. /// Renewal status point cost formula
  7066. #define PC_STATUS_POINT_COST(low) (((low) < 100) ? (2 + ((low) - 1) / 10) : (16 + 4 * (((low) - 100) / 5)))
  7067. #else
  7068. /// Pre-Renewal status point cost formula
  7069. #define PC_STATUS_POINT_COST(low) (( 1 + ((low) + 9) / 10 ))
  7070. #endif
  7071. /// Returns the number of stat points needed to change the specified stat by val.
  7072. /// If val is negative, returns the number of stat points that would be needed to
  7073. /// raise the specified stat from (current value - val) to current value.
  7074. int pc_need_status_point(struct map_session_data* sd, int type, int val)
  7075. {
  7076. int low, high, sp = 0, max = 0;
  7077. if ( val == 0 )
  7078. return 0;
  7079. low = pc_getstat(sd,type);
  7080. max = pc_maxparameter(sd,(enum e_params)(type-SP_STR));
  7081. if ( low >= max && val > 0 )
  7082. return 0; // Official servers show '0' when max is reached
  7083. high = low + val;
  7084. if ( val < 0 )
  7085. SWAP(low, high);
  7086. for ( ; low < high; low++ )
  7087. sp += PC_STATUS_POINT_COST(low);
  7088. return sp;
  7089. }
  7090. /**
  7091. * Returns the value the specified stat can be increased by with the current
  7092. * amount of available status points for the current character's class.
  7093. *
  7094. * @param sd The target character.
  7095. * @param type Stat to verify.
  7096. * @return Maximum value the stat could grow by.
  7097. */
  7098. int pc_maxparameterincrease(struct map_session_data* sd, int type)
  7099. {
  7100. int base, final_val, status_points, max_param;
  7101. nullpo_ret(sd);
  7102. base = final_val = pc_getstat(sd, type);
  7103. status_points = sd->status.status_point;
  7104. max_param = pc_maxparameter(sd, (enum e_params)(type-SP_STR));
  7105. while (final_val <= max_param && status_points >= 0) {
  7106. status_points -= PC_STATUS_POINT_COST(final_val);
  7107. final_val++;
  7108. }
  7109. final_val--;
  7110. return (final_val > base ? final_val-base : 0);
  7111. }
  7112. /**
  7113. * Raises a stat by the specified amount.
  7114. *
  7115. * Obeys max_parameter limits.
  7116. * Subtracts status points according to the cost of the increased stat points.
  7117. *
  7118. * @param sd The target character.
  7119. * @param type The stat to change (see enum _sp)
  7120. * @param increase The stat increase (strictly positive) amount.
  7121. * @retval true if the stat was increased by any amount.
  7122. * @retval false if there were no changes.
  7123. */
  7124. bool pc_statusup(struct map_session_data* sd, int type, int increase)
  7125. {
  7126. int max_increase = 0, current = 0, needed_points = 0, final_value = 0;
  7127. nullpo_ret(sd);
  7128. // check conditions
  7129. if (type < SP_STR || type > SP_LUK || increase <= 0) {
  7130. clif_statusupack(sd, type, 0, 0);
  7131. return false;
  7132. }
  7133. // check limits
  7134. current = pc_getstat(sd, type);
  7135. max_increase = pc_maxparameterincrease(sd, type);
  7136. increase = cap_value(increase, 0, max_increase); // cap to the maximum status points available
  7137. if (increase <= 0 || current + increase > pc_maxparameter(sd, (enum e_params)(type-SP_STR))) {
  7138. clif_statusupack(sd, type, 0, 0);
  7139. return false;
  7140. }
  7141. // check status points
  7142. needed_points = pc_need_status_point(sd, type, increase);
  7143. if (needed_points < 0 || needed_points > sd->status.status_point) { // Sanity check
  7144. clif_statusupack(sd, type, 0, 0);
  7145. return false;
  7146. }
  7147. // set new values
  7148. final_value = pc_setstat(sd, type, current + increase);
  7149. sd->status.status_point -= needed_points;
  7150. status_calc_pc(sd,SCO_NONE);
  7151. // update increase cost indicator
  7152. clif_updatestatus(sd, SP_USTR + type-SP_STR);
  7153. // update statpoint count
  7154. clif_updatestatus(sd, SP_STATUSPOINT);
  7155. // update stat value
  7156. clif_statusupack(sd, type, 1, final_value); // required
  7157. if( final_value > 255 )
  7158. clif_updatestatus(sd, type); // send after the 'ack' to override the truncated value
  7159. achievement_update_objective(sd, AG_GOAL_STATUS, 1, final_value);
  7160. return true;
  7161. }
  7162. /**
  7163. * Raises a stat by the specified amount.
  7164. *
  7165. * Obeys max_parameter limits.
  7166. * Does not subtract status points for the cost of the modified stat points.
  7167. *
  7168. * @param sd The target character.
  7169. * @param type The stat to change (see enum _sp)
  7170. * @param val The stat increase (or decrease) amount.
  7171. * @return the stat increase amount.
  7172. * @retval 0 if no changes were made.
  7173. */
  7174. int pc_statusup2(struct map_session_data* sd, int type, int val)
  7175. {
  7176. int max, need;
  7177. nullpo_ret(sd);
  7178. if( type < SP_STR || type > SP_LUK )
  7179. {
  7180. clif_statusupack(sd,type,0,0);
  7181. return 0;
  7182. }
  7183. need = pc_need_status_point(sd,type,1);
  7184. max = pc_maxparameter(sd,(enum e_params)(type-SP_STR)); // set new value
  7185. val = pc_setstat(sd, type, cap_value(pc_getstat(sd,type) + val, 1, max));
  7186. status_calc_pc(sd,SCO_NONE);
  7187. // update increase cost indicator
  7188. if( need != pc_need_status_point(sd,type,1) )
  7189. clif_updatestatus(sd, SP_USTR + type-SP_STR);
  7190. // update stat value
  7191. clif_statusupack(sd,type,1,val); // required
  7192. if( val > 255 )
  7193. clif_updatestatus(sd,type); // send after the 'ack' to override the truncated value
  7194. return val;
  7195. }
  7196. /// Returns the number of trait stat points needed to change the specified trait stat by val.
  7197. /// If val is negative, returns the number of trait stat points that would be needed to
  7198. /// raise the specified trait stat from (current value - val) to current value.
  7199. int pc_need_trait_point(struct map_session_data* sd, int type, int val)
  7200. {
  7201. nullpo_retr(0, sd);
  7202. if (val == 0 || type < SP_POW || type > SP_CRT)
  7203. return 0;
  7204. int low = pc_getstat(sd, type);
  7205. int max = pc_maxparameter(sd, (e_params)(PARAM_POW + type - SP_POW));
  7206. if (low >= max && val > 0)
  7207. return 0; // Official servers show '0' when max is reached
  7208. int high = low + val, sp = 0;
  7209. if (val < 0)
  7210. SWAP(low, high);
  7211. for (; low < high; low++)
  7212. sp += 1;
  7213. return sp;
  7214. }
  7215. /**
  7216. * Returns the value the specified trait stat can be increased by with the current
  7217. * amount of available trait status points for the current character's class.
  7218. *
  7219. * @param sd The target character.
  7220. * @param type Trait stat to verify.
  7221. * @return Maximum value the stat could grow by.
  7222. */
  7223. int pc_maxtraitparameterincrease(struct map_session_data* sd, int type)
  7224. {
  7225. nullpo_ret(sd);
  7226. if( type < SP_POW || type > SP_CRT ){
  7227. return 0;
  7228. }
  7229. int base, final_val = pc_getstat(sd, type);
  7230. int trait_points = sd->status.trait_point;
  7231. int max_param = pc_maxparameter(sd, (enum e_params)(PARAM_POW + type - SP_POW));
  7232. base = final_val;
  7233. while (final_val <= max_param && trait_points >= 0) {
  7234. trait_points -= 1;
  7235. final_val++;
  7236. }
  7237. final_val--;
  7238. return (final_val > base ? final_val - base : 0);
  7239. }
  7240. /**
  7241. * Raises a trait stat by the specified amount.
  7242. *
  7243. * Obeys max_traitparameter limits.
  7244. * Subtracts trait status points according to the cost of the increased trait stat points.
  7245. *
  7246. * @param sd The target character.
  7247. * @param type The stat to change (see enum _sp)
  7248. * @param increase The stat increase (strictly positive) amount.
  7249. * @retval true if the trait stat was increased by any amount.
  7250. * @retval false if there were no changes.
  7251. */
  7252. bool pc_traitstatusup(struct map_session_data* sd, int type, int increase)
  7253. {
  7254. nullpo_ret(sd);
  7255. // check conditions
  7256. if (type < SP_POW || type > SP_CRT || increase <= 0) {
  7257. clif_statusupack(sd, type, 0, 0);
  7258. return false;
  7259. }
  7260. // check limits
  7261. int current = pc_getstat(sd, type);
  7262. int max_increase = pc_maxtraitparameterincrease(sd, type);
  7263. increase = cap_value(increase, 0, max_increase); // cap to the maximum status points available
  7264. if (increase <= 0 || current + increase > pc_maxparameter(sd, (enum e_params)(PARAM_POW + type - SP_POW))) {
  7265. clif_statusupack(sd, type, 0, 0);
  7266. return false;
  7267. }
  7268. // check status points
  7269. int needed_points = pc_need_trait_point(sd, type, increase);
  7270. if (needed_points < 0 || needed_points > sd->status.trait_point) { // Sanity check
  7271. clif_statusupack(sd, type, 0, 0);
  7272. return false;
  7273. }
  7274. // set new values
  7275. int final_value = pc_setstat(sd, type, current + increase);
  7276. sd->status.trait_point -= needed_points;
  7277. status_calc_pc(sd, SCO_NONE);
  7278. // update increase cost indicator
  7279. clif_updatestatus(sd, SP_UPOW + type - SP_POW);
  7280. // update statpoint count
  7281. clif_updatestatus(sd, SP_TRAITPOINT);
  7282. // update stat value
  7283. clif_statusupack(sd, type, 1, final_value); // required
  7284. if (final_value > 255)
  7285. clif_updatestatus(sd, type); // send after the 'ack' to override the truncated value
  7286. //achievement_update_objective(sd, AG_GOAL_STATUS, 1, final_value);
  7287. return true;
  7288. }
  7289. /**
  7290. * Raises a trait stat by the specified amount.
  7291. *
  7292. * Obeys max_trait_parameter limits.
  7293. * Does not subtract status points for the cost of the modified stat points.
  7294. *
  7295. * @param sd The target character.
  7296. * @param type The stat to change (see enum _sp)
  7297. * @param val The stat increase (or decrease) amount.
  7298. * @return the stat increase amount.
  7299. * @retval 0 if no changes were made.
  7300. */
  7301. int pc_traitstatusup2(struct map_session_data* sd, int type, int val)
  7302. {
  7303. nullpo_ret(sd);
  7304. if (type < SP_POW || type > SP_CRT) {
  7305. clif_statusupack(sd, type, 0, 0);
  7306. return 0;
  7307. }
  7308. int need = pc_need_trait_point(sd, type, 1);
  7309. int max = pc_maxparameter(sd, (enum e_params)(PARAM_POW + type - SP_POW)); // set new value
  7310. val = pc_setstat(sd, type, cap_value(pc_getstat(sd, type) + val, 0, max));
  7311. status_calc_pc(sd, SCO_NONE);
  7312. // update increase cost indicator
  7313. if (need != pc_need_trait_point(sd, type, 1))
  7314. clif_updatestatus(sd, SP_UPOW + type - SP_POW);
  7315. // update stat value
  7316. clif_statusupack(sd, type, 1, val); // required
  7317. if (val > 255)
  7318. clif_updatestatus(sd, type); // send after the 'ack' to override the truncated value
  7319. return val;
  7320. }
  7321. /*==========================================
  7322. * Update skill_lv for player sd
  7323. * Skill point allocation
  7324. *------------------------------------------*/
  7325. void pc_skillup(struct map_session_data *sd,uint16 skill_id)
  7326. {
  7327. uint16 idx = skill_get_index(skill_id);
  7328. nullpo_retv(sd);
  7329. if (!idx) {
  7330. if (skill_id)
  7331. ShowError("pc_skillup: Player attempts to level up invalid skill '%d'\n", skill_id);
  7332. return;
  7333. }
  7334. // Level up guild skill
  7335. if (SKILL_CHK_GUILD(skill_id)) {
  7336. guild_skillup(sd, skill_id);
  7337. return;
  7338. }
  7339. // Level up homunculus skill
  7340. else if (sd->hd && SKILL_CHK_HOMUN(skill_id)) {
  7341. hom_skillup(sd->hd, skill_id);
  7342. return;
  7343. }
  7344. else {
  7345. if( sd->status.skill_point > 0 &&
  7346. sd->status.skill[idx].id &&
  7347. sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT && //Don't allow raising while you have granted skills. [Skotlex]
  7348. sd->status.skill[idx].lv < skill_tree_get_max(skill_id, sd->status.class_) )
  7349. {
  7350. int lv, range, upgradable;
  7351. sd->status.skill[idx].lv++;
  7352. sd->status.skill_point--;
  7353. if( !skill_get_inf(skill_id) || pc_checkskill_summoner(sd, SUMMONER_POWER_LAND) >= 20 || pc_checkskill_summoner(sd, SUMMONER_POWER_SEA) >= 20 )
  7354. status_calc_pc(sd,SCO_NONE); // Only recalculate for passive skills.
  7355. else if( sd->status.skill_point == 0 && pc_is_taekwon_ranker(sd) )
  7356. pc_calc_skilltree(sd); // Required to grant all TK Ranker skills.
  7357. else
  7358. pc_check_skilltree(sd); // Check if a new skill can Lvlup
  7359. lv = sd->status.skill[idx].lv;
  7360. range = skill_get_range2(&sd->bl, skill_id, lv, false);
  7361. upgradable = (lv < skill_tree_get_max(sd->status.skill[idx].id, sd->status.class_)) ? 1 : 0;
  7362. clif_skillup(sd,skill_id,lv,range,upgradable);
  7363. clif_updatestatus(sd,SP_SKILLPOINT);
  7364. if( skill_id == GN_REMODELING_CART ) /* cart weight info was updated by status_calc_pc */
  7365. clif_updatestatus(sd,SP_CARTINFO);
  7366. if (pc_checkskill(sd, SG_DEVIL) && ((sd->class_&MAPID_THIRDMASK) == MAPID_STAR_EMPEROR || pc_is_maxjoblv(sd)))
  7367. clif_status_change(&sd->bl, EFST_DEVIL1, 1, 0, 0, 0, 1); //Permanent blind effect from SG_DEVIL.
  7368. if (!pc_has_permission(sd, PC_PERM_ALL_SKILL)) // may skill everything at any time anyways, and this would cause a huge slowdown
  7369. clif_skillinfoblock(sd);
  7370. }
  7371. //else
  7372. // 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);
  7373. }
  7374. }
  7375. /*==========================================
  7376. * /allskill
  7377. *------------------------------------------*/
  7378. int pc_allskillup(struct map_session_data *sd)
  7379. {
  7380. int i;
  7381. nullpo_ret(sd);
  7382. for (i = 0; i < MAX_SKILL; i++) {
  7383. 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) {
  7384. sd->status.skill[i].lv = (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY) ? 0 : sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
  7385. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  7386. if (sd->status.skill[i].lv == 0)
  7387. sd->status.skill[i].id = 0;
  7388. }
  7389. }
  7390. if (!pc_grant_allskills(sd, true)) {
  7391. uint16 sk_id;
  7392. std::shared_ptr<s_skill_tree> tree = skill_tree_db.find(sd->status.class_);
  7393. if (tree != nullptr && !tree->skills.empty()) {
  7394. for (const auto &skillsit : tree->skills) {
  7395. sk_id = skillsit.first;
  7396. uint16 sk_idx = skill_get_index(sk_id);
  7397. if (sk_idx == 0)
  7398. continue;
  7399. std::shared_ptr<s_skill_db> skill = skill_db.find(sk_id);
  7400. if (
  7401. (skill->inf2[INF2_ISQUEST] && !battle_config.quest_skill_learn) ||
  7402. ((skill->inf2[INF2_ISWEDDING] || skill->inf2[INF2_ISSPIRIT])) ||
  7403. sk_id == SG_DEVIL
  7404. )
  7405. continue; //Cannot be learned normally.
  7406. sd->status.skill[sk_idx].id = sk_id;
  7407. sd->status.skill[sk_idx].lv = skill_tree_get_max(sk_id, sd->status.class_); // celest
  7408. }
  7409. }
  7410. }
  7411. status_calc_pc(sd,SCO_NONE);
  7412. //Required because if you could level up all skills previously,
  7413. //the update will not be sent as only the lv variable changes.
  7414. clif_skillinfoblock(sd);
  7415. return 0;
  7416. }
  7417. /*==========================================
  7418. * /resetlvl
  7419. *------------------------------------------*/
  7420. int pc_resetlvl(struct map_session_data* sd,int type)
  7421. {
  7422. int i;
  7423. nullpo_ret(sd);
  7424. if (type != 3) //Also reset skills
  7425. pc_resetskill(sd, 0);
  7426. if(type == 1){
  7427. sd->status.skill_point=0;
  7428. sd->status.trait_point = 0;
  7429. sd->status.base_level=1;
  7430. sd->status.job_level=1;
  7431. sd->status.base_exp=0;
  7432. sd->status.job_exp=0;
  7433. if(sd->sc.option !=0)
  7434. sd->sc.option = 0;
  7435. sd->status.str=1;
  7436. sd->status.agi=1;
  7437. sd->status.vit=1;
  7438. sd->status.int_=1;
  7439. sd->status.dex=1;
  7440. sd->status.luk=1;
  7441. sd->status.pow=0;
  7442. sd->status.sta=0;
  7443. sd->status.wis=0;
  7444. sd->status.spl=0;
  7445. sd->status.con=0;
  7446. sd->status.crt=0;
  7447. if(sd->status.class_ == JOB_NOVICE_HIGH) {
  7448. sd->status.status_point=100; // not 88 [celest]
  7449. // give platinum skills upon changing
  7450. pc_skill(sd,NV_FIRSTAID,1,ADDSKILL_PERMANENT);
  7451. pc_skill(sd,NV_TRICKDEAD,1,ADDSKILL_PERMANENT);
  7452. }
  7453. }
  7454. if(type == 2){
  7455. sd->status.skill_point=0;
  7456. sd->status.base_level=1;
  7457. sd->status.job_level=1;
  7458. sd->status.base_exp=0;
  7459. sd->status.job_exp=0;
  7460. }
  7461. if(type == 3){
  7462. sd->status.base_level=1;
  7463. sd->status.base_exp=0;
  7464. }
  7465. if(type == 4){
  7466. sd->status.job_level=1;
  7467. sd->status.job_exp=0;
  7468. }
  7469. clif_updatestatus(sd,SP_STATUSPOINT);
  7470. clif_updatestatus(sd,SP_TRAITPOINT);
  7471. clif_updatestatus(sd,SP_STR);
  7472. clif_updatestatus(sd,SP_AGI);
  7473. clif_updatestatus(sd,SP_VIT);
  7474. clif_updatestatus(sd,SP_INT);
  7475. clif_updatestatus(sd,SP_DEX);
  7476. clif_updatestatus(sd,SP_LUK);
  7477. clif_updatestatus(sd,SP_POW);
  7478. clif_updatestatus(sd,SP_STA);
  7479. clif_updatestatus(sd,SP_WIS);
  7480. clif_updatestatus(sd,SP_SPL);
  7481. clif_updatestatus(sd,SP_CON);
  7482. clif_updatestatus(sd,SP_CRT);
  7483. clif_updatestatus(sd,SP_BASELEVEL);
  7484. clif_updatestatus(sd,SP_JOBLEVEL);
  7485. clif_updatestatus(sd,SP_STATUSPOINT);
  7486. clif_updatestatus(sd,SP_BASEEXP);
  7487. clif_updatestatus(sd,SP_JOBEXP);
  7488. clif_updatestatus(sd,SP_NEXTBASEEXP);
  7489. clif_updatestatus(sd,SP_NEXTJOBEXP);
  7490. clif_updatestatus(sd,SP_SKILLPOINT);
  7491. clif_updatestatus(sd,SP_USTR); // Updates needed stat points - Valaris
  7492. clif_updatestatus(sd,SP_UAGI);
  7493. clif_updatestatus(sd,SP_UVIT);
  7494. clif_updatestatus(sd,SP_UINT);
  7495. clif_updatestatus(sd,SP_UDEX);
  7496. clif_updatestatus(sd,SP_ULUK); // End Addition
  7497. clif_updatestatus(sd,SP_UPOW);
  7498. clif_updatestatus(sd,SP_USTA);
  7499. clif_updatestatus(sd,SP_UWIS);
  7500. clif_updatestatus(sd,SP_USPL);
  7501. clif_updatestatus(sd,SP_UCON);
  7502. clif_updatestatus(sd,SP_UCRT);
  7503. for(i=0;i<EQI_MAX;i++) { // unequip items that can't be equipped by base 1 [Valaris]
  7504. if(sd->equip_index[i] >= 0)
  7505. if(pc_isequip(sd,sd->equip_index[i]))
  7506. pc_unequipitem(sd,sd->equip_index[i],2);
  7507. }
  7508. if ((type == 1 || type == 2 || type == 3) && sd->status.party_id)
  7509. party_send_levelup(sd);
  7510. status_calc_pc(sd, SCO_FORCE);
  7511. clif_skillinfoblock(sd);
  7512. return 0;
  7513. }
  7514. /*==========================================
  7515. * /resetstate
  7516. *------------------------------------------*/
  7517. int pc_resetstate(struct map_session_data* sd)
  7518. {
  7519. nullpo_ret(sd);
  7520. if( sd->status.base_level > pc_maxbaselv( sd ) ){
  7521. ShowError( "pc_resetstate: Capping the Level to %d to reset the stats of %d:%d, the base level (%d) is greater than the max level supported.\n",
  7522. pc_maxbaselv( sd ), sd->status.account_id, sd->status.char_id, sd->status.base_level );
  7523. sd->status.base_level = pc_maxbaselv( sd );
  7524. clif_updatestatus( sd, SP_BASELEVEL );
  7525. }
  7526. sd->status.status_point = statpoint_db.get_table_point( sd->status.base_level );
  7527. sd->status.trait_point = statpoint_db.get_trait_table_point(sd->status.base_level);
  7528. if( ( sd->class_&JOBL_UPPER ) != 0 ){
  7529. sd->status.status_point += battle_config.transcendent_status_points;
  7530. }
  7531. if ((sd->class_&JOBL_FOURTH) != 0) {
  7532. sd->status.trait_point += battle_config.trait_points_job_change;
  7533. }
  7534. pc_setstat(sd, SP_STR, 1);
  7535. pc_setstat(sd, SP_AGI, 1);
  7536. pc_setstat(sd, SP_VIT, 1);
  7537. pc_setstat(sd, SP_INT, 1);
  7538. pc_setstat(sd, SP_DEX, 1);
  7539. pc_setstat(sd, SP_LUK, 1);
  7540. pc_setstat(sd, SP_POW, 0);
  7541. pc_setstat(sd, SP_STA, 0);
  7542. pc_setstat(sd, SP_WIS, 0);
  7543. pc_setstat(sd, SP_SPL, 0);
  7544. pc_setstat(sd, SP_CON, 0);
  7545. pc_setstat(sd, SP_CRT, 0);
  7546. clif_updatestatus(sd,SP_STR);
  7547. clif_updatestatus(sd,SP_AGI);
  7548. clif_updatestatus(sd,SP_VIT);
  7549. clif_updatestatus(sd,SP_INT);
  7550. clif_updatestatus(sd,SP_DEX);
  7551. clif_updatestatus(sd,SP_LUK);
  7552. clif_updatestatus(sd,SP_POW);
  7553. clif_updatestatus(sd,SP_STA);
  7554. clif_updatestatus(sd,SP_WIS);
  7555. clif_updatestatus(sd,SP_SPL);
  7556. clif_updatestatus(sd,SP_CON);
  7557. clif_updatestatus(sd,SP_CRT);
  7558. clif_updatestatus(sd,SP_USTR); // Updates needed stat points - Valaris
  7559. clif_updatestatus(sd,SP_UAGI);
  7560. clif_updatestatus(sd,SP_UVIT);
  7561. clif_updatestatus(sd,SP_UINT);
  7562. clif_updatestatus(sd,SP_UDEX);
  7563. clif_updatestatus(sd,SP_ULUK); // End Addition
  7564. clif_updatestatus(sd,SP_UPOW);
  7565. clif_updatestatus(sd,SP_USTA);
  7566. clif_updatestatus(sd,SP_UWIS);
  7567. clif_updatestatus(sd,SP_USPL);
  7568. clif_updatestatus(sd,SP_UCON);
  7569. clif_updatestatus(sd,SP_UCRT);
  7570. clif_updatestatus(sd,SP_STATUSPOINT);
  7571. clif_updatestatus(sd,SP_TRAITPOINT);
  7572. if( sd->mission_mobid ) { //bugreport:2200
  7573. sd->mission_mobid = 0;
  7574. sd->mission_count = 0;
  7575. pc_setglobalreg(sd, add_str(TKMISSIONID_VAR), 0);
  7576. }
  7577. status_calc_pc(sd, SCO_NONE);
  7578. return 1;
  7579. }
  7580. /*==========================================
  7581. * /resetskill
  7582. * if flag&1, perform block resync and status_calc call.
  7583. * if flag&2, just count total amount of skill points used by player, do not really reset.
  7584. * if flag&4, just reset the skills if the player class is a bard/dancer type (for changesex.)
  7585. *------------------------------------------*/
  7586. int pc_resetskill(struct map_session_data* sd, int flag)
  7587. {
  7588. int i, skill_point=0;
  7589. nullpo_ret(sd);
  7590. if( flag&4 && (sd->class_&MAPID_UPPERMASK) != MAPID_BARDDANCER )
  7591. return 0;
  7592. if( !(flag&2) ) { //Remove stuff lost when resetting skills.
  7593. /**
  7594. * 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)
  7595. **/
  7596. if( pc_is_taekwon_ranker(sd) )
  7597. return 0;
  7598. if( pc_checkskill(sd, SG_DEVIL) && ((sd->class_&MAPID_THIRDMASK) == MAPID_STAR_EMPEROR || pc_is_maxjoblv(sd)) )
  7599. clif_status_load(&sd->bl, EFST_DEVIL1, 0); //Remove perma blindness due to skill-reset. [Skotlex]
  7600. i = sd->sc.option;
  7601. if( i&OPTION_RIDING && pc_checkskill(sd, KN_RIDING) )
  7602. i &= ~OPTION_RIDING;
  7603. if( i&OPTION_FALCON && pc_checkskill(sd, HT_FALCON) )
  7604. i &= ~OPTION_FALCON;
  7605. if( i&OPTION_DRAGON && pc_checkskill(sd, RK_DRAGONTRAINING) )
  7606. i &= ~OPTION_DRAGON;
  7607. if( i&OPTION_WUG && pc_checkskill(sd, RA_WUGMASTERY) )
  7608. i &= ~OPTION_WUG;
  7609. if( i&OPTION_WUGRIDER && pc_checkskill(sd, RA_WUGRIDER) )
  7610. i &= ~OPTION_WUGRIDER;
  7611. if( i&OPTION_MADOGEAR && ( sd->class_&MAPID_THIRDMASK ) == MAPID_MECHANIC )
  7612. i &= ~OPTION_MADOGEAR;
  7613. #ifndef NEW_CARTS
  7614. if( i&OPTION_CART && pc_checkskill(sd, MC_PUSHCART) )
  7615. i &= ~OPTION_CART;
  7616. #else
  7617. if( sd->sc.data[SC_PUSH_CART] )
  7618. pc_setcart(sd, 0);
  7619. #endif
  7620. if( i != sd->sc.option )
  7621. pc_setoption(sd, i);
  7622. if( hom_is_active(sd->hd) && pc_checkskill(sd, AM_CALLHOMUN) )
  7623. hom_vaporize(sd, HOM_ST_ACTIVE);
  7624. if (sd->sc.data[SC_SPRITEMABLE] && pc_checkskill(sd, SU_SPRITEMABLE))
  7625. status_change_end(&sd->bl, SC_SPRITEMABLE, INVALID_TIMER);
  7626. if (sd->sc.data[SC_SOULATTACK] && pc_checkskill(sd, SU_SOULATTACK))
  7627. status_change_end(&sd->bl, SC_SOULATTACK, INVALID_TIMER);
  7628. }
  7629. for (const auto &skill : skill_db) {
  7630. uint16 skill_id = skill.second->nameid, idx = skill_get_index(skill_id);
  7631. uint8 lv = sd->status.skill[idx].lv;
  7632. if (lv == 0 || skill_id == 0)
  7633. continue;
  7634. if( skill.second->inf2[INF2_ISWEDDING] || skill.second->inf2[INF2_ISSPIRIT] ) //Avoid reseting wedding/linker skills.
  7635. continue;
  7636. // Don't reset trick dead if not a novice/baby
  7637. if( skill_id == NV_TRICKDEAD && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE )
  7638. {
  7639. sd->status.skill[idx].lv = 0;
  7640. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  7641. continue;
  7642. }
  7643. // do not reset basic skill
  7644. if (skill_id == NV_BASIC && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE )
  7645. continue;
  7646. if( sd->status.skill[idx].flag == SKILL_FLAG_PERM_GRANTED )
  7647. continue;
  7648. if( flag&4 && !skill_ischangesex(skill_id) )
  7649. continue;
  7650. if( skill.second->inf2[INF2_ISQUEST] && !battle_config.quest_skill_learn )
  7651. { //Only handle quest skills in a special way when you can't learn them manually
  7652. if( battle_config.quest_skill_reset && !(flag&2) )
  7653. { //Wipe them
  7654. sd->status.skill[idx].lv = 0;
  7655. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  7656. }
  7657. continue;
  7658. }
  7659. if( sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT )
  7660. skill_point += lv;
  7661. else
  7662. if( sd->status.skill[idx].flag >= SKILL_FLAG_REPLACED_LV_0 )
  7663. skill_point += (sd->status.skill[idx].flag - SKILL_FLAG_REPLACED_LV_0);
  7664. if( !(flag&2) )
  7665. {// reset
  7666. sd->status.skill[idx].lv = 0;
  7667. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  7668. }
  7669. }
  7670. if( flag&2 || !skill_point ) return skill_point;
  7671. sd->status.skill_point += skill_point;
  7672. if (flag&1) {
  7673. clif_updatestatus(sd,SP_SKILLPOINT);
  7674. clif_skillinfoblock(sd);
  7675. status_calc_pc(sd, SCO_FORCE);
  7676. }
  7677. return skill_point;
  7678. }
  7679. /*==========================================
  7680. * /resetfeel [Komurka]
  7681. *------------------------------------------*/
  7682. int pc_resetfeel(struct map_session_data* sd)
  7683. {
  7684. int i;
  7685. nullpo_ret(sd);
  7686. for (i=0; i<MAX_PC_FEELHATE; i++)
  7687. {
  7688. sd->feel_map[i].m = -1;
  7689. sd->feel_map[i].index = 0;
  7690. pc_setglobalreg(sd, add_str(sg_info[i].feel_var), 0);
  7691. }
  7692. return 0;
  7693. }
  7694. int pc_resethate(struct map_session_data* sd)
  7695. {
  7696. int i;
  7697. nullpo_ret(sd);
  7698. for (i=0; i<MAX_PC_FEELHATE; i++)
  7699. {
  7700. sd->hate_mob[i] = -1;
  7701. pc_setglobalreg(sd, add_str(sg_info[i].hate_var), 0);
  7702. }
  7703. return 0;
  7704. }
  7705. int pc_skillatk_bonus(struct map_session_data *sd, uint16 skill_id)
  7706. {
  7707. int bonus = 0;
  7708. nullpo_ret(sd);
  7709. skill_id = skill_dummy2skill_id(skill_id);
  7710. for (auto &it : sd->skillatk) {
  7711. if (it.id == skill_id) {
  7712. bonus += it.val;
  7713. break;
  7714. }
  7715. }
  7716. return bonus;
  7717. }
  7718. int pc_sub_skillatk_bonus(struct map_session_data *sd, uint16 skill_id)
  7719. {
  7720. int bonus = 0;
  7721. nullpo_ret(sd);
  7722. skill_id = skill_dummy2skill_id(skill_id);
  7723. for (auto &it : sd->subskill) {
  7724. if (it.id == skill_id) {
  7725. bonus += it.val;
  7726. break;
  7727. }
  7728. }
  7729. return bonus;
  7730. }
  7731. int pc_skillheal_bonus(struct map_session_data *sd, uint16 skill_id) {
  7732. int bonus = sd->bonus.add_heal_rate;
  7733. nullpo_ret(sd);
  7734. skill_id = skill_dummy2skill_id(skill_id);
  7735. if( bonus ) {
  7736. switch( skill_id ) {
  7737. case AL_HEAL: if( !(battle_config.skill_add_heal_rate&1) ) bonus = 0; break;
  7738. case PR_SANCTUARY: if( !(battle_config.skill_add_heal_rate&2) ) bonus = 0; break;
  7739. case AM_POTIONPITCHER: if( !(battle_config.skill_add_heal_rate&4) ) bonus = 0; break;
  7740. case CR_SLIMPITCHER: if( !(battle_config.skill_add_heal_rate&8) ) bonus = 0; break;
  7741. case BA_APPLEIDUN: if( !(battle_config.skill_add_heal_rate&16)) bonus = 0; break;
  7742. case AB_CHEAL: if (!(battle_config.skill_add_heal_rate & 32)) bonus = 0; break;
  7743. case AB_HIGHNESSHEAL: if (!(battle_config.skill_add_heal_rate & 64)) bonus = 0; break;
  7744. case CD_MEDIALE_VOTUM: if (!(battle_config.skill_add_heal_rate & 128)) bonus = 0; break;
  7745. case CD_DILECTIO_HEAL: if (!(battle_config.skill_add_heal_rate & 256)) bonus = 0; break;
  7746. }
  7747. }
  7748. for (auto &it : sd->skillheal) {
  7749. if (it.id == skill_id) {
  7750. bonus += it.val;
  7751. break;
  7752. }
  7753. }
  7754. return bonus;
  7755. }
  7756. int pc_skillheal2_bonus(struct map_session_data *sd, uint16 skill_id) {
  7757. int bonus = sd->bonus.add_heal2_rate;
  7758. skill_id = skill_dummy2skill_id(skill_id);
  7759. for (auto &it : sd->skillheal2) {
  7760. if (it.id == skill_id) {
  7761. bonus += it.val;
  7762. break;
  7763. }
  7764. }
  7765. return bonus;
  7766. }
  7767. void pc_respawn(struct map_session_data* sd, clr_type clrtype)
  7768. {
  7769. if( !pc_isdead(sd) )
  7770. return; // not applicable
  7771. if( sd->bg_id && bg_member_respawn(sd) )
  7772. return; // member revived by battleground
  7773. pc_setstand(sd, true);
  7774. pc_setrestartvalue(sd,3);
  7775. if( pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, clrtype) != SETPOS_OK )
  7776. clif_resurrection(&sd->bl, 1); //If warping fails, send a normal stand up packet.
  7777. }
  7778. static TIMER_FUNC(pc_respawn_timer){
  7779. struct map_session_data *sd = map_id2sd(id);
  7780. if( sd != NULL )
  7781. {
  7782. sd->pvp_point=0;
  7783. sd->respawn_tid = INVALID_TIMER;
  7784. pc_respawn(sd,CLR_OUTSIGHT);
  7785. }
  7786. return 0;
  7787. }
  7788. /*==========================================
  7789. * Invoked when a player has received damage
  7790. *------------------------------------------*/
  7791. void pc_damage(struct map_session_data *sd,struct block_list *src,unsigned int hp, unsigned int sp, unsigned int ap)
  7792. {
  7793. if (ap) clif_updatestatus(sd,SP_AP);
  7794. if (sp) clif_updatestatus(sd,SP_SP);
  7795. if (hp) clif_updatestatus(sd,SP_HP);
  7796. else return;
  7797. if (!src)
  7798. return;
  7799. if( pc_issit(sd) ) {
  7800. pc_setstand(sd, true);
  7801. skill_sit(sd,0);
  7802. }
  7803. if (sd->progressbar.npc_id)
  7804. clif_progressbar_abort(sd);
  7805. if( sd->status.pet_id > 0 && sd->pd && battle_config.pet_damage_support )
  7806. pet_target_check(sd->pd,src,1);
  7807. if( sd->status.ele_id > 0 )
  7808. elemental_set_target(sd,src);
  7809. if(battle_config.prevent_logout_trigger&PLT_DAMAGE)
  7810. sd->canlog_tick = gettick();
  7811. }
  7812. TIMER_FUNC(pc_close_npc_timer){
  7813. TBL_PC *sd = map_id2sd(id);
  7814. if(sd) pc_close_npc(sd,data);
  7815. return 0;
  7816. }
  7817. /**
  7818. * Method to properly close a NPC for player and clear anything related.
  7819. * @param sd: Player attached
  7820. * @param flag: Method of closure
  7821. * 1: Produce a close button and end the NPC
  7822. * 2: End the NPC (best for no dialog windows)
  7823. */
  7824. void pc_close_npc(struct map_session_data *sd,int flag)
  7825. {
  7826. nullpo_retv(sd);
  7827. if (sd->npc_id || sd->npc_shopid) {
  7828. if (sd->state.using_fake_npc) {
  7829. clif_clearunit_single(sd->npc_id, CLR_OUTSIGHT, sd->fd);
  7830. sd->state.using_fake_npc = 0;
  7831. }
  7832. if (sd->st) {
  7833. if(sd->st->state == RUN){ //wait ending code execution
  7834. add_timer(gettick()+500,pc_close_npc_timer,sd->bl.id,flag);
  7835. return;
  7836. }
  7837. sd->st->state = ((flag==1 && sd->st->mes_active)?CLOSE:END);
  7838. sd->st->mes_active = 0;
  7839. }
  7840. sd->state.menu_or_input = 0;
  7841. sd->npc_menu = 0;
  7842. sd->npc_shopid = 0;
  7843. #ifdef SECURE_NPCTIMEOUT
  7844. if( sd->npc_idle_timer != INVALID_TIMER ){
  7845. delete_timer( sd->npc_idle_timer, npc_secure_timeout_timer );
  7846. sd->npc_idle_timer = INVALID_TIMER;
  7847. }
  7848. #endif
  7849. if (sd->st) {
  7850. if (sd->st->state == CLOSE) {
  7851. clif_scriptclose(sd, sd->npc_id);
  7852. clif_scriptclear(sd, sd->npc_id); // [Ind/Hercules]
  7853. sd->st->state = END; // Force to end now
  7854. }
  7855. if (sd->st->state == END) { // free attached scripts that are waiting
  7856. script_free_state(sd->st);
  7857. sd->st = NULL;
  7858. sd->npc_id = 0;
  7859. }
  7860. }
  7861. }
  7862. }
  7863. /*==========================================
  7864. * Invoked when a player has negative current hp
  7865. *------------------------------------------*/
  7866. int pc_dead(struct map_session_data *sd,struct block_list *src)
  7867. {
  7868. int i=0,k=0;
  7869. t_tick tick = gettick();
  7870. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  7871. // Activate Steel body if a super novice dies at 99+% exp [celest]
  7872. // Super Novices have no kill or die functions attached when saved by their angel
  7873. if (!sd->state.snovice_dead_flag && (sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE) {
  7874. t_exp exp = pc_nextbaseexp(sd);
  7875. if( exp && get_percentage_exp(sd->status.base_exp, exp) >= 99 ) {
  7876. sd->state.snovice_dead_flag = 1;
  7877. pc_setrestartvalue(sd,1);
  7878. status_percent_heal(&sd->bl, 100, 100);
  7879. clif_resurrection(&sd->bl, 1);
  7880. if(battle_config.pc_invincible_time)
  7881. pc_setinvincibletimer(sd, battle_config.pc_invincible_time);
  7882. sc_start(&sd->bl,&sd->bl,SC_STEELBODY,100,5,skill_get_time(MO_STEELBODY,5));
  7883. if(mapdata_flag_gvg2(mapdata))
  7884. pc_respawn_timer(INVALID_TIMER, gettick(), sd->bl.id, 0);
  7885. return 0;
  7886. }
  7887. }
  7888. for(k = 0; k < MAX_DEVOTION; k++) {
  7889. if (sd->devotion[k]){
  7890. struct map_session_data *devsd = map_id2sd(sd->devotion[k]);
  7891. if (devsd)
  7892. status_change_end(&devsd->bl, SC_DEVOTION, INVALID_TIMER);
  7893. sd->devotion[k] = 0;
  7894. }
  7895. }
  7896. for (k = 0; k < MAX_STELLAR_MARKS; k++) {
  7897. if (sd->stellar_mark[k]) {
  7898. struct map_session_data *smarksd = map_id2sd(sd->stellar_mark[k]);
  7899. if (smarksd)
  7900. status_change_end(&smarksd->bl, SC_FLASHKICK, INVALID_TIMER);
  7901. sd->stellar_mark[k] = 0;
  7902. }
  7903. }
  7904. for (k = 0; k < MAX_UNITED_SOULS; k++) {
  7905. if (sd->united_soul[k]) {
  7906. struct map_session_data *usoulsd = map_id2sd(sd->united_soul[k]);
  7907. if (usoulsd)
  7908. status_change_end(&usoulsd->bl, SC_SOULUNITY, INVALID_TIMER);
  7909. sd->united_soul[k] = 0;
  7910. }
  7911. }
  7912. for (k = 0; k < MAX_SERVANT_SIGN; k++) {
  7913. if (sd->servant_sign[k]) {
  7914. struct map_session_data *ssignsd = map_id2sd(sd->servant_sign[k]);
  7915. if (ssignsd)
  7916. status_change_end(&ssignsd->bl, SC_SERVANT_SIGN, INVALID_TIMER);
  7917. sd->servant_sign[k] = 0;
  7918. }
  7919. }
  7920. if(sd->shadowform_id) { //if we were target of shadowform
  7921. status_change_end(map_id2bl(sd->shadowform_id), SC__SHADOWFORM, INVALID_TIMER);
  7922. sd->shadowform_id = 0; //should be remove on status end anyway
  7923. }
  7924. if(sd->status.pet_id > 0 && sd->pd) {
  7925. struct pet_data *pd = sd->pd;
  7926. if( !mapdata->flag[MF_NOEXPPENALTY] ) {
  7927. pet_set_intimate(pd, pd->pet.intimate + pd->get_pet_db()->die);
  7928. if( pd->pet.intimate <= PET_INTIMATE_NONE )
  7929. pet_set_intimate(pd, PET_INTIMATE_NONE);
  7930. clif_send_petdata(sd,sd->pd,1,pd->pet.intimate);
  7931. }
  7932. if( sd->pd->target_id ) // Unlock all targets...
  7933. pet_unlocktarget(sd->pd);
  7934. }
  7935. 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)
  7936. hom_vaporize(sd, HOM_ST_ACTIVE);
  7937. if( sd->md )
  7938. mercenary_delete(sd->md, 3); // Your mercenary soldier has ran away.
  7939. if( sd->ed )
  7940. elemental_delete(sd->ed);
  7941. // Leave duel if you die [LuzZza]
  7942. if(battle_config.duel_autoleave_when_die) {
  7943. if(sd->duel_group > 0)
  7944. duel_leave(sd->duel_group, sd);
  7945. if(sd->duel_invite > 0)
  7946. duel_reject(sd->duel_invite, sd);
  7947. }
  7948. if( sd->skill_keep_using.tid != INVALID_TIMER ){
  7949. delete_timer( sd->skill_keep_using.tid, skill_keep_using );
  7950. sd->skill_keep_using.tid = INVALID_TIMER;
  7951. }
  7952. pc_close_npc(sd,2); //close npc if we were using one
  7953. /* e.g. not killed thru pc_damage */
  7954. if( pc_issit(sd) ) {
  7955. clif_status_load(&sd->bl,EFST_SIT,0);
  7956. }
  7957. pc_setdead(sd);
  7958. clif_party_dead( sd );
  7959. pc_setparam(sd, SP_PCDIECOUNTER, sd->die_counter+1);
  7960. pc_setparam(sd, SP_KILLERRID, src?src->id:0);
  7961. if (battle_config.loose_ap_on_death == 1)
  7962. status_percent_damage( nullptr, &sd->bl, 0, 0, 100, 0 );
  7963. //Reset menu skills/item skills
  7964. if ((sd->skillitem) != 0)
  7965. sd->skillitem = sd->skillitemlv = 0;
  7966. if ((sd->menuskill_id) != 0)
  7967. sd->menuskill_id = sd->menuskill_val = 0;
  7968. //Reset ticks.
  7969. sd->hp_loss.tick = sd->sp_loss.tick = sd->hp_regen.tick = sd->sp_regen.tick = 0;
  7970. if ( sd->spiritball !=0 )
  7971. pc_delspiritball(sd,sd->spiritball,0);
  7972. if (sd->soulball != 0)
  7973. pc_delsoulball(sd, sd->soulball, false);
  7974. if (sd->servantball != 0)
  7975. pc_delservantball( *sd, sd->servantball );
  7976. if (sd->abyssball != 0)
  7977. pc_delabyssball( *sd, sd->abyssball );
  7978. if (sd->spiritcharm_type != CHARM_TYPE_NONE && sd->spiritcharm > 0)
  7979. pc_delspiritcharm(sd,sd->spiritcharm,sd->spiritcharm_type);
  7980. if (src)
  7981. switch (src->type) {
  7982. case BL_MOB:
  7983. {
  7984. struct mob_data *md=(struct mob_data *)src;
  7985. if(md->target_id==sd->bl.id)
  7986. mob_unlocktarget(md,tick);
  7987. if(battle_config.mobs_level_up && md->status.hp &&
  7988. (unsigned int)md->level < pc_maxbaselv(sd) &&
  7989. !md->guardian_data && !md->special_state.ai// Guardians/summons should not level. [Skotlex]
  7990. ) { // monster level up [Valaris]
  7991. clif_misceffect(&md->bl,0);
  7992. md->level++;
  7993. status_calc_mob(md, SCO_NONE);
  7994. status_percent_heal(src,10,0);
  7995. if( battle_config.show_mob_info&4 )
  7996. {// update name with new level
  7997. clif_name_area(&md->bl);
  7998. }
  7999. }
  8000. src = battle_get_master(src); // Maybe Player Summon
  8001. }
  8002. break;
  8003. case BL_PET: //Pass on to master...
  8004. case BL_HOM:
  8005. case BL_MER:
  8006. src = battle_get_master(src);
  8007. break;
  8008. }
  8009. if (src && src->type == BL_PC) {
  8010. struct map_session_data *ssd = (struct map_session_data *)src;
  8011. pc_setparam(ssd, SP_KILLEDRID, sd->bl.id);
  8012. npc_script_event(ssd, NPCE_KILLPC);
  8013. if (battle_config.pk_mode&2) {
  8014. ssd->status.manner -= 5;
  8015. if(ssd->status.manner < 0)
  8016. sc_start(&sd->bl,src,SC_NOCHAT,100,0,0);
  8017. #if 0
  8018. // PK/Karma system code (not enabled yet) [celest]
  8019. // originally from Kade Online, so i don't know if any of these is correct ^^;
  8020. // note: karma is measured REVERSE, so more karma = more 'evil' / less honourable,
  8021. // karma going down = more 'good' / more honourable.
  8022. // The Karma System way...
  8023. if (sd->status.karma > ssd->status.karma) { // If player killed was more evil
  8024. sd->status.karma--;
  8025. ssd->status.karma--;
  8026. }
  8027. else if (sd->status.karma < ssd->status.karma) // If player killed was more good
  8028. ssd->status.karma++;
  8029. // or the PK System way...
  8030. if (sd->status.karma > 0) // player killed is dishonourable?
  8031. ssd->status.karma--; // honour points earned
  8032. sd->status.karma++; // honour points lost
  8033. // To-do: Receive exp on certain occasions
  8034. #endif
  8035. }
  8036. }
  8037. if(battle_config.bone_drop==2
  8038. || (battle_config.bone_drop==1 && mapdata->flag[MF_PVP]))
  8039. {
  8040. struct item item_tmp;
  8041. memset(&item_tmp,0,sizeof(item_tmp));
  8042. item_tmp.nameid=ITEMID_SKULL_;
  8043. item_tmp.identify=1;
  8044. item_tmp.card[0]=CARD0_CREATE;
  8045. item_tmp.card[1]=0;
  8046. item_tmp.card[2]=GetWord(sd->status.char_id,0); // CharId
  8047. item_tmp.card[3]=GetWord(sd->status.char_id,1);
  8048. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  8049. }
  8050. //Remove bonus_script when dead
  8051. pc_bonus_script_clear(sd,BSF_REM_ON_DEAD);
  8052. // changed penalty options, added death by player if pk_mode [Valaris]
  8053. if(battle_config.death_penalty_type
  8054. && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE // only novices will receive no penalty
  8055. && !sd->sc.cant.deathpenalty
  8056. && !mapdata->flag[MF_NOEXPPENALTY] && !mapdata_flag_gvg2(mapdata))
  8057. {
  8058. t_exp base_penalty = 0;
  8059. t_exp job_penalty = 0;
  8060. uint32 zeny_penalty = 0;
  8061. if (pc_isvip(sd)) { // EXP penalty for VIP
  8062. base_penalty = battle_config.vip_exp_penalty_base;
  8063. job_penalty = battle_config.vip_exp_penalty_job;
  8064. zeny_penalty = battle_config.vip_zeny_penalty;
  8065. } else {
  8066. base_penalty = battle_config.death_penalty_base;
  8067. job_penalty = battle_config.death_penalty_job;
  8068. zeny_penalty = battle_config.zeny_penalty;
  8069. }
  8070. if ((battle_config.death_penalty_maxlv&1 || !pc_is_maxbaselv(sd)) && base_penalty > 0) {
  8071. switch (battle_config.death_penalty_type) {
  8072. case 1: base_penalty = (t_exp) ( pc_nextbaseexp(sd) * ( base_penalty / 10000. ) ); break;
  8073. case 2: base_penalty = (t_exp) ( sd->status.base_exp * ( base_penalty / 10000. ) ); break;
  8074. }
  8075. if (base_penalty){ //recheck after altering to speedup
  8076. if (battle_config.pk_mode && src && src->type==BL_PC)
  8077. base_penalty *= 2;
  8078. base_penalty = u64min(sd->status.base_exp, base_penalty);
  8079. }
  8080. }
  8081. else
  8082. base_penalty = 0;
  8083. if ((battle_config.death_penalty_maxlv&2 || !pc_is_maxjoblv(sd)) && job_penalty > 0) {
  8084. switch (battle_config.death_penalty_type) {
  8085. case 1: job_penalty = (uint32) ( pc_nextjobexp(sd) * ( job_penalty / 10000. ) ); break;
  8086. case 2: job_penalty = (uint32) ( sd->status.job_exp * ( job_penalty /10000. ) ); break;
  8087. }
  8088. if (job_penalty) {
  8089. if (battle_config.pk_mode && src && src->type==BL_PC)
  8090. job_penalty *= 2;
  8091. job_penalty = u64min(sd->status.job_exp, job_penalty);
  8092. }
  8093. }
  8094. else
  8095. job_penalty = 0;
  8096. if (base_penalty || job_penalty) {
  8097. short insurance_idx = pc_search_inventory(sd, ITEMID_NEW_INSURANCE);
  8098. if (insurance_idx < 0 || pc_delitem(sd, insurance_idx, 1, 0, 1, LOG_TYPE_CONSUME) != 0)
  8099. pc_lostexp(sd, base_penalty, job_penalty);
  8100. }
  8101. if( zeny_penalty > 0 && !mapdata->flag[MF_NOZENYPENALTY]) {
  8102. zeny_penalty = (uint32)( sd->status.zeny * ( zeny_penalty / 10000. ) );
  8103. if(zeny_penalty)
  8104. pc_payzeny(sd, zeny_penalty, LOG_TYPE_PICKDROP_PLAYER, NULL);
  8105. }
  8106. }
  8107. if( mapdata->flag[MF_PVP_NIGHTMAREDROP] ) { // Moved this outside so it works when PVP isn't enabled and during pk mode [Ancyker]
  8108. for (const auto &it : mapdata->drop_list) {
  8109. int id = it.drop_id, per = it.drop_per;
  8110. enum e_nightmare_drop_type type = it.drop_type;
  8111. if(id == 0)
  8112. continue;
  8113. if(id == -1){
  8114. int eq_num=0,eq_n[MAX_INVENTORY];
  8115. memset(eq_n,0,sizeof(eq_n));
  8116. for(i=0;i<MAX_INVENTORY;i++) {
  8117. if( (type&NMDT_INVENTORY && !sd->inventory.u.items_inventory[i].equip)
  8118. || (type&NMDT_EQUIP && sd->inventory.u.items_inventory[i].equip)
  8119. || type&NMDT_ALL)
  8120. {
  8121. int l;
  8122. ARR_FIND( 0, MAX_INVENTORY, l, eq_n[l] <= 0 );
  8123. if( l < MAX_INVENTORY )
  8124. eq_n[l] = i;
  8125. eq_num++;
  8126. }
  8127. }
  8128. if(eq_num > 0){
  8129. int n = eq_n[rnd()%eq_num];
  8130. if(rnd()%10000 < per) {
  8131. if(sd->inventory.u.items_inventory[n].equip)
  8132. pc_unequipitem(sd,n,3);
  8133. pc_dropitem(sd,n,1);
  8134. }
  8135. }
  8136. }
  8137. else if(id > 0) {
  8138. for(i=0;i<MAX_INVENTORY;i++){
  8139. if(sd->inventory.u.items_inventory[i].nameid == id
  8140. && rnd()%10000 < per
  8141. && ((type&NMDT_INVENTORY && !sd->inventory.u.items_inventory[i].equip)
  8142. || (type&NMDT_EQUIP && sd->inventory.u.items_inventory[i].equip)
  8143. || type&NMDT_ALL) ){
  8144. if(sd->inventory.u.items_inventory[i].equip)
  8145. pc_unequipitem(sd,i,3);
  8146. pc_dropitem(sd,i,1);
  8147. break;
  8148. }
  8149. }
  8150. }
  8151. }
  8152. }
  8153. // pvp
  8154. // disable certain pvp functions on pk_mode [Valaris]
  8155. if( !battle_config.pk_mode && mapdata->flag[MF_PVP] && !mapdata->flag[MF_PVP_NOCALCRANK] ) {
  8156. sd->pvp_point -= 5;
  8157. sd->pvp_lost++;
  8158. if( src && src->type == BL_PC ) {
  8159. struct map_session_data *ssd = (struct map_session_data *)src;
  8160. ssd->pvp_point++;
  8161. ssd->pvp_won++;
  8162. }
  8163. if( sd->pvp_point < 0 ) {
  8164. sd->respawn_tid = add_timer(tick+1000, pc_respawn_timer,sd->bl.id,0);
  8165. return 1|8;
  8166. }
  8167. }
  8168. //GvG
  8169. if( mapdata_flag_gvg2(mapdata) ) {
  8170. sd->respawn_tid = add_timer(tick+1000, pc_respawn_timer, sd->bl.id, 0);
  8171. return 1|8;
  8172. }
  8173. else if( sd->bg_id ) {
  8174. std::shared_ptr<s_battleground_data> bg = util::umap_find(bg_team_db, sd->bg_id);
  8175. if (bg) {
  8176. if (bg->cemetery.map > 0) { // Respawn by BG
  8177. sd->respawn_tid = add_timer(tick + 1000, pc_respawn_timer, sd->bl.id, 0);
  8178. return 1|8;
  8179. }
  8180. }
  8181. }
  8182. //Reset "can log out" tick.
  8183. if( battle_config.prevent_logout )
  8184. sd->canlog_tick = gettick() - battle_config.prevent_logout;
  8185. return 1;
  8186. }
  8187. void pc_revive(struct map_session_data *sd,unsigned int hp, unsigned int sp, unsigned int ap) {
  8188. if(hp) clif_updatestatus(sd,SP_HP);
  8189. if(sp) clif_updatestatus(sd,SP_SP);
  8190. if(ap) clif_updatestatus(sd,SP_AP);
  8191. pc_setstand(sd, true);
  8192. if(battle_config.pc_invincible_time > 0)
  8193. pc_setinvincibletimer(sd, battle_config.pc_invincible_time);
  8194. if( sd->state.gmaster_flag ) {
  8195. guild_guildaura_refresh(sd,GD_LEADERSHIP,guild_checkskill(sd->guild,GD_LEADERSHIP));
  8196. guild_guildaura_refresh(sd,GD_GLORYWOUNDS,guild_checkskill(sd->guild,GD_GLORYWOUNDS));
  8197. guild_guildaura_refresh(sd,GD_SOULCOLD,guild_checkskill(sd->guild,GD_SOULCOLD));
  8198. guild_guildaura_refresh(sd,GD_HAWKEYES,guild_checkskill(sd->guild,GD_HAWKEYES));
  8199. }
  8200. }
  8201. bool pc_revive_item(struct map_session_data *sd) {
  8202. nullpo_retr(false, sd);
  8203. if (!pc_isdead(sd) || sd->respawn_tid != INVALID_TIMER)
  8204. return false;
  8205. if (sd->sc.data[SC_HELLPOWER]) // Cannot resurrect while under the effect of SC_HELLPOWER.
  8206. return false;
  8207. int16 item_position = itemdb_group.item_exists_pc(sd, IG_TOKEN_OF_SIEGFRIED);
  8208. uint8 hp = 100, sp = 100;
  8209. if (item_position < 0) {
  8210. if (sd->sc.data[SC_LIGHT_OF_REGENE]) {
  8211. hp = sd->sc.data[SC_LIGHT_OF_REGENE]->val2;
  8212. sp = 0;
  8213. }
  8214. else
  8215. return false;
  8216. }
  8217. if (!status_revive(&sd->bl, hp, sp))
  8218. return false;
  8219. if (item_position < 0)
  8220. status_change_end(&sd->bl, SC_LIGHT_OF_REGENE, INVALID_TIMER);
  8221. else
  8222. pc_delitem(sd, item_position, 1, 0, 1, LOG_TYPE_CONSUME);
  8223. clif_skill_nodamage(&sd->bl, &sd->bl, ALL_RESURRECTION, 4, 1);
  8224. return true;
  8225. }
  8226. // script
  8227. //
  8228. /*==========================================
  8229. * script reading pc status registry
  8230. *------------------------------------------*/
  8231. int64 pc_readparam(struct map_session_data* sd,int64 type)
  8232. {
  8233. int64 val = 0;
  8234. nullpo_ret(sd);
  8235. switch(type) {
  8236. case SP_SKILLPOINT: val = sd->status.skill_point; break;
  8237. case SP_STATUSPOINT: val = sd->status.status_point; break;
  8238. case SP_TRAITPOINT: val = sd->status.trait_point; break;
  8239. case SP_ZENY: val = sd->status.zeny; break;
  8240. case SP_BASELEVEL: val = sd->status.base_level; break;
  8241. case SP_JOBLEVEL: val = sd->status.job_level; break;
  8242. case SP_CLASS: val = sd->status.class_; break;
  8243. case SP_BASEJOB: val = pc_mapid2jobid(sd->class_&MAPID_UPPERMASK, sd->status.sex); break; //Base job, extracting upper type.
  8244. case SP_UPPER: val = sd->class_&JOBL_UPPER?1:(sd->class_&JOBL_BABY?2:0); break;
  8245. case SP_BASECLASS: val = pc_mapid2jobid(sd->class_&MAPID_BASEMASK, sd->status.sex); break; //Extract base class tree. [Skotlex]
  8246. case SP_SEX: val = sd->status.sex; break;
  8247. case SP_WEIGHT: val = sd->weight; break;
  8248. case SP_MAXWEIGHT: val = sd->max_weight; break;
  8249. case SP_BASEEXP: val = u64min(sd->status.base_exp, MAX_EXP); break;
  8250. case SP_JOBEXP: val = u64min(sd->status.job_exp, MAX_EXP); break;
  8251. case SP_NEXTBASEEXP: val = u64min(pc_nextbaseexp(sd), MAX_EXP); break;
  8252. case SP_NEXTJOBEXP: val = u64min(pc_nextjobexp(sd), MAX_EXP); break;
  8253. case SP_HP: val = sd->battle_status.hp; break;
  8254. case SP_MAXHP: val = sd->battle_status.max_hp; break;
  8255. case SP_SP: val = sd->battle_status.sp; break;
  8256. case SP_MAXSP: val = sd->battle_status.max_sp; break;
  8257. case SP_AP: val = sd->battle_status.ap; break;
  8258. case SP_MAXAP: val = sd->battle_status.max_ap; break;
  8259. case SP_STR: val = sd->status.str; break;
  8260. case SP_AGI: val = sd->status.agi; break;
  8261. case SP_VIT: val = sd->status.vit; break;
  8262. case SP_INT: val = sd->status.int_; break;
  8263. case SP_DEX: val = sd->status.dex; break;
  8264. case SP_LUK: val = sd->status.luk; break;
  8265. case SP_POW: val = sd->status.pow; break;
  8266. case SP_STA: val = sd->status.sta; break;
  8267. case SP_WIS: val = sd->status.wis; break;
  8268. case SP_SPL: val = sd->status.spl; break;
  8269. case SP_CON: val = sd->status.con; break;
  8270. case SP_CRT: val = sd->status.crt; break;
  8271. case SP_KARMA: val = sd->status.karma; break;
  8272. case SP_MANNER: val = sd->status.manner; break;
  8273. case SP_FAME: val = sd->status.fame; break;
  8274. case SP_KILLERRID: val = sd->killerrid; break;
  8275. case SP_KILLEDRID: val = sd->killedrid; break;
  8276. case SP_KILLEDGID: val = sd->killedgid; break;
  8277. case SP_SITTING: val = pc_issit(sd)?1:0; break;
  8278. case SP_CHARMOVE: val = sd->status.character_moves; break;
  8279. case SP_CHARRENAME: val = sd->status.rename; break;
  8280. case SP_CHARFONT: val = sd->status.font; break;
  8281. case SP_BANK_VAULT: val = sd->bank_vault; break;
  8282. case SP_CASHPOINTS: val = sd->cashPoints; break;
  8283. case SP_KAFRAPOINTS: val = sd->kafraPoints; break;
  8284. case SP_ROULETTE_BRONZE: val = sd->roulette_point.bronze; break;
  8285. case SP_ROULETTE_SILVER: val = sd->roulette_point.silver; break;
  8286. case SP_ROULETTE_GOLD: val = sd->roulette_point.gold; break;
  8287. case SP_PCDIECOUNTER: val = sd->die_counter; break;
  8288. case SP_COOKMASTERY: val = sd->cook_mastery; break;
  8289. case SP_ACHIEVEMENT_LEVEL: val = sd->achievement_data.level; break;
  8290. case SP_CRITICAL: val = sd->battle_status.cri/10; break;
  8291. case SP_ASPD: val = (2000-sd->battle_status.amotion)/10; break;
  8292. case SP_BASE_ATK:
  8293. #ifdef RENEWAL
  8294. val = sd->bonus.eatk;
  8295. #else
  8296. val = sd->battle_status.batk;
  8297. #endif
  8298. break;
  8299. case SP_DEF1: val = sd->battle_status.def; break;
  8300. case SP_DEF2: val = sd->battle_status.def2; break;
  8301. case SP_MDEF1: val = sd->battle_status.mdef; break;
  8302. case SP_MDEF2: val = sd->battle_status.mdef2; break;
  8303. case SP_HIT: val = sd->battle_status.hit; break;
  8304. case SP_FLEE1: val = sd->battle_status.flee; break;
  8305. case SP_FLEE2: val = sd->battle_status.flee2; break;
  8306. case SP_PATK: val = sd->battle_status.patk; break;
  8307. case SP_SMATK: val = sd->battle_status.smatk; break;
  8308. case SP_RES: val = sd->battle_status.res; break;
  8309. case SP_MRES: val = sd->battle_status.mres; break;
  8310. case SP_HPLUS: val = sd->battle_status.hplus; break;
  8311. case SP_CRATE: val = sd->battle_status.crate; break;
  8312. case SP_DEFELE: val = sd->battle_status.def_ele; break;
  8313. case SP_MAXHPRATE: val = sd->hprate; break;
  8314. case SP_MAXSPRATE: val = sd->sprate; break;
  8315. case SP_MAXAPRATE: val = sd->aprate; break;
  8316. case SP_SPRATE: val = sd->dsprate; break;
  8317. case SP_SPEED_RATE: val = sd->bonus.speed_rate; break;
  8318. case SP_SPEED_ADDRATE: val = sd->bonus.speed_add_rate; break;
  8319. case SP_ASPD_RATE:
  8320. #ifndef RENEWAL_ASPD
  8321. val = sd->battle_status.aspd_rate;
  8322. #else
  8323. val = sd->battle_status.aspd_rate2;
  8324. #endif
  8325. break;
  8326. case SP_HP_RECOV_RATE: val = sd->hprecov_rate; break;
  8327. case SP_SP_RECOV_RATE: val = sd->sprecov_rate; break;
  8328. case SP_CRITICAL_DEF: val = sd->bonus.critical_def; break;
  8329. case SP_NEAR_ATK_DEF: val = sd->bonus.near_attack_def_rate; break;
  8330. case SP_LONG_ATK_DEF: val = sd->bonus.long_attack_def_rate; break;
  8331. case SP_DOUBLE_RATE: val = sd->bonus.double_rate; break;
  8332. case SP_DOUBLE_ADD_RATE: val = sd->bonus.double_add_rate; break;
  8333. case SP_MATK_RATE: val = sd->matk_rate; break;
  8334. case SP_ATK_RATE: val = sd->bonus.atk_rate; break;
  8335. case SP_MAGIC_ATK_DEF: val = sd->bonus.magic_def_rate; break;
  8336. case SP_MISC_ATK_DEF: val = sd->bonus.misc_def_rate; break;
  8337. case SP_PERFECT_HIT_RATE:val = sd->bonus.perfect_hit; break;
  8338. case SP_PERFECT_HIT_ADD_RATE: val = sd->bonus.perfect_hit_add; break;
  8339. case SP_CRITICAL_RATE: val = sd->critical_rate; break;
  8340. case SP_HIT_RATE: val = sd->hit_rate; break;
  8341. case SP_FLEE_RATE: val = sd->flee_rate; break;
  8342. case SP_FLEE2_RATE: val = sd->flee2_rate; break;
  8343. case SP_DEF_RATE: val = sd->def_rate; break;
  8344. case SP_DEF2_RATE: val = sd->def2_rate; break;
  8345. case SP_MDEF_RATE: val = sd->mdef_rate; break;
  8346. case SP_MDEF2_RATE: val = sd->mdef2_rate; break;
  8347. case SP_PATK_RATE: val = sd->patk_rate; break;
  8348. case SP_SMATK_RATE: val = sd->smatk_rate; break;
  8349. case SP_RES_RATE: val = sd->res_rate; break;
  8350. case SP_MRES_RATE: val = sd->mres_rate; break;
  8351. case SP_HPLUS_RATE: val = sd->hplus_rate; break;
  8352. case SP_CRATE_RATE: val = sd->crate_rate; break;
  8353. case SP_RESTART_FULL_RECOVER: val = sd->special_state.restart_full_recover?1:0; break;
  8354. case SP_NO_CASTCANCEL: val = sd->special_state.no_castcancel?1:0; break;
  8355. case SP_NO_CASTCANCEL2: val = sd->special_state.no_castcancel2?1:0; break;
  8356. case SP_NO_SIZEFIX: val = sd->special_state.no_sizefix?1:0; break;
  8357. case SP_NO_MAGIC_DAMAGE: val = sd->special_state.no_magic_damage; break;
  8358. case SP_NO_WEAPON_DAMAGE:val = sd->special_state.no_weapon_damage; break;
  8359. case SP_NO_MISC_DAMAGE: val = sd->special_state.no_misc_damage; break;
  8360. case SP_NO_GEMSTONE: val = sd->special_state.no_gemstone?1:0; break;
  8361. case SP_INTRAVISION: val = sd->special_state.intravision?1:0; break;
  8362. case SP_NO_KNOCKBACK: val = sd->special_state.no_knockback?1:0; break;
  8363. case SP_NO_MADO_FUEL: val = sd->special_state.no_mado_fuel?1:0; break;
  8364. case SP_NO_WALK_DELAY: val = sd->special_state.no_walk_delay?1:0; break;
  8365. case SP_SPLASH_RANGE: val = sd->bonus.splash_range; break;
  8366. case SP_SPLASH_ADD_RANGE:val = sd->bonus.splash_add_range; break;
  8367. case SP_SHORT_WEAPON_DAMAGE_RETURN: val = sd->bonus.short_weapon_damage_return; break;
  8368. case SP_LONG_WEAPON_DAMAGE_RETURN: val = sd->bonus.long_weapon_damage_return; break;
  8369. case SP_MAGIC_DAMAGE_RETURN: val = sd->bonus.magic_damage_return; break;
  8370. case SP_REDUCE_DAMAGE_RETURN: val = sd->bonus.reduce_damage_return; break;
  8371. case SP_PERFECT_HIDE: val = sd->special_state.perfect_hiding?1:0; break;
  8372. case SP_UNBREAKABLE: val = sd->bonus.unbreakable; break;
  8373. case SP_UNBREAKABLE_WEAPON: val = (sd->bonus.unbreakable_equip&EQP_WEAPON)?1:0; break;
  8374. case SP_UNBREAKABLE_ARMOR: val = (sd->bonus.unbreakable_equip&EQP_ARMOR)?1:0; break;
  8375. case SP_UNBREAKABLE_HELM: val = (sd->bonus.unbreakable_equip&EQP_HELM)?1:0; break;
  8376. case SP_UNBREAKABLE_SHIELD: val = (sd->bonus.unbreakable_equip&EQP_SHIELD)?1:0; break;
  8377. case SP_UNBREAKABLE_GARMENT: val = (sd->bonus.unbreakable_equip&EQP_GARMENT)?1:0; break;
  8378. case SP_UNBREAKABLE_SHOES: val = (sd->bonus.unbreakable_equip&EQP_SHOES)?1:0; break;
  8379. case SP_CLASSCHANGE: val = sd->bonus.classchange; break;
  8380. case SP_SHORT_ATK_RATE: val = sd->bonus.short_attack_atk_rate; break;
  8381. case SP_LONG_ATK_RATE: val = sd->bonus.long_attack_atk_rate; break;
  8382. case SP_BREAK_WEAPON_RATE: val = sd->bonus.break_weapon_rate; break;
  8383. case SP_BREAK_ARMOR_RATE: val = sd->bonus.break_armor_rate; break;
  8384. case SP_ADD_STEAL_RATE: val = sd->bonus.add_steal_rate; break;
  8385. case SP_DELAYRATE: val = sd->bonus.delayrate; break;
  8386. case SP_CRIT_ATK_RATE: val = sd->bonus.crit_atk_rate; break;
  8387. case SP_UNSTRIPABLE_WEAPON: val = (sd->bonus.unstripable_equip&EQP_WEAPON)?1:0; break;
  8388. case SP_UNSTRIPABLE:
  8389. case SP_UNSTRIPABLE_ARMOR:
  8390. val = (sd->bonus.unstripable_equip&EQP_ARMOR)?1:0;
  8391. break;
  8392. case SP_UNSTRIPABLE_HELM: val = (sd->bonus.unstripable_equip&EQP_HELM)?1:0; break;
  8393. case SP_UNSTRIPABLE_SHIELD: val = (sd->bonus.unstripable_equip&EQP_SHIELD)?1:0; break;
  8394. case SP_SP_GAIN_VALUE: val = sd->bonus.sp_gain_value; break;
  8395. case SP_HP_GAIN_VALUE: val = sd->bonus.hp_gain_value; break;
  8396. case SP_LONG_SP_GAIN_VALUE: val = sd->bonus.long_sp_gain_value; break;
  8397. case SP_LONG_HP_GAIN_VALUE: val = sd->bonus.long_hp_gain_value; break;
  8398. case SP_MAGIC_SP_GAIN_VALUE: val = sd->bonus.magic_sp_gain_value; break;
  8399. case SP_MAGIC_HP_GAIN_VALUE: val = sd->bonus.magic_hp_gain_value; break;
  8400. case SP_ADD_HEAL_RATE: val = sd->bonus.add_heal_rate; break;
  8401. case SP_ADD_HEAL2_RATE: val = sd->bonus.add_heal2_rate; break;
  8402. case SP_ADD_ITEM_HEAL_RATE: val = sd->bonus.itemhealrate2; break;
  8403. case SP_EMATK: val = sd->bonus.ematk; break;
  8404. case SP_FIXCASTRATE: val = sd->bonus.fixcastrate; break;
  8405. case SP_ADD_FIXEDCAST: val = sd->bonus.add_fixcast; break;
  8406. case SP_ADD_VARIABLECAST: val = sd->bonus.add_varcast; break;
  8407. case SP_CASTRATE:
  8408. case SP_VARCASTRATE:
  8409. #ifdef RENEWAL_CAST
  8410. val = sd->bonus.varcastrate; break;
  8411. #else
  8412. val = sd->castrate; break;
  8413. #endif
  8414. case SP_CRIT_DEF_RATE: val = sd->bonus.crit_def_rate; break;
  8415. case SP_ADD_ITEM_SPHEAL_RATE: val = sd->bonus.itemsphealrate2; break;
  8416. default:
  8417. ShowError("pc_readparam: Attempt to read unknown parameter '%lld'.\n", type);
  8418. return -1;
  8419. }
  8420. return val;
  8421. }
  8422. /*==========================================
  8423. * script set pc status registry
  8424. *------------------------------------------*/
  8425. bool pc_setparam(struct map_session_data *sd,int64 type,int64 val_tmp)
  8426. {
  8427. nullpo_retr(false,sd);
  8428. int val = static_cast<unsigned int>(val_tmp);
  8429. switch(type){
  8430. case SP_BASELEVEL:
  8431. if (val > pc_maxbaselv(sd)) //Capping to max
  8432. val = pc_maxbaselv(sd);
  8433. if (val > sd->status.base_level) {
  8434. for( int i = 0; i < (int)( val - sd->status.base_level ); i++ ){
  8435. sd->status.status_point += statpoint_db.pc_gets_status_point( sd->status.base_level + i );
  8436. sd->status.trait_point += statpoint_db.pc_gets_trait_point( sd->status.base_level + i );
  8437. }
  8438. }
  8439. sd->status.base_level = val;
  8440. sd->status.base_exp = 0;
  8441. // clif_updatestatus(sd, SP_BASELEVEL); // Gets updated at the bottom
  8442. clif_updatestatus(sd, SP_NEXTBASEEXP);
  8443. clif_updatestatus(sd, SP_STATUSPOINT);
  8444. clif_updatestatus(sd, SP_TRAITPOINT);
  8445. clif_updatestatus(sd, SP_BASEEXP);
  8446. status_calc_pc(sd, SCO_FORCE);
  8447. if(sd->status.party_id)
  8448. party_send_levelup(sd);
  8449. break;
  8450. case SP_JOBLEVEL:
  8451. if (val >= sd->status.job_level) {
  8452. if (val > pc_maxjoblv(sd)) val = pc_maxjoblv(sd);
  8453. sd->status.skill_point += val - sd->status.job_level;
  8454. clif_updatestatus(sd, SP_SKILLPOINT);
  8455. }
  8456. sd->status.job_level = val;
  8457. sd->status.job_exp = 0;
  8458. // clif_updatestatus(sd, SP_JOBLEVEL); // Gets updated at the bottom
  8459. clif_updatestatus(sd, SP_NEXTJOBEXP);
  8460. clif_updatestatus(sd, SP_JOBEXP);
  8461. status_calc_pc(sd, SCO_FORCE);
  8462. break;
  8463. case SP_SKILLPOINT:
  8464. sd->status.skill_point = val;
  8465. break;
  8466. case SP_STATUSPOINT:
  8467. sd->status.status_point = val;
  8468. break;
  8469. case SP_TRAITPOINT:
  8470. sd->status.trait_point = val;
  8471. break;
  8472. case SP_ZENY:
  8473. if( val < 0 )
  8474. return false;// can't set negative zeny
  8475. log_zeny(sd, LOG_TYPE_SCRIPT, sd, -(sd->status.zeny - cap_value(val, 0, MAX_ZENY)));
  8476. sd->status.zeny = cap_value(val, 0, MAX_ZENY);
  8477. break;
  8478. case SP_BASEEXP:
  8479. val_tmp = cap_value(val_tmp, 0, pc_is_maxbaselv(sd) ? MAX_LEVEL_BASE_EXP : MAX_EXP);
  8480. if (val_tmp < sd->status.base_exp) // Lost
  8481. pc_lostexp(sd, sd->status.base_exp - val_tmp, 0);
  8482. else // Gained
  8483. pc_gainexp(sd, NULL, val_tmp - sd->status.base_exp, 0, 2);
  8484. return true;
  8485. case SP_JOBEXP:
  8486. val_tmp = cap_value(val_tmp, 0, pc_is_maxjoblv(sd) ? MAX_LEVEL_JOB_EXP : MAX_EXP);
  8487. if (val_tmp < sd->status.job_exp) // Lost
  8488. pc_lostexp(sd, 0, sd->status.job_exp - val_tmp);
  8489. else // Gained
  8490. pc_gainexp(sd, NULL, 0, val_tmp - sd->status.job_exp, 2);
  8491. return true;
  8492. case SP_SEX:
  8493. sd->status.sex = val ? SEX_MALE : SEX_FEMALE;
  8494. break;
  8495. case SP_WEIGHT:
  8496. sd->weight = val;
  8497. break;
  8498. case SP_MAXWEIGHT:
  8499. sd->max_weight = val;
  8500. break;
  8501. case SP_HP:
  8502. sd->battle_status.hp = cap_value(val, 1, (int)sd->battle_status.max_hp);
  8503. break;
  8504. case SP_MAXHP:
  8505. if (sd->status.base_level < 100)
  8506. sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp_lv99);
  8507. else if (sd->status.base_level < 151)
  8508. sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp_lv150);
  8509. else
  8510. sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp);
  8511. if( sd->battle_status.max_hp < sd->battle_status.hp )
  8512. {
  8513. sd->battle_status.hp = sd->battle_status.max_hp;
  8514. clif_updatestatus(sd, SP_HP);
  8515. }
  8516. break;
  8517. case SP_SP:
  8518. sd->battle_status.sp = cap_value(val, 0, (int)sd->battle_status.max_sp);
  8519. break;
  8520. case SP_MAXSP:
  8521. sd->battle_status.max_sp = cap_value(val, 1, battle_config.max_sp);
  8522. if( sd->battle_status.max_sp < sd->battle_status.sp )
  8523. {
  8524. sd->battle_status.sp = sd->battle_status.max_sp;
  8525. clif_updatestatus(sd, SP_SP);
  8526. }
  8527. break;
  8528. case SP_AP:
  8529. sd->battle_status.ap = cap_value(val, 0, (int)sd->battle_status.max_ap);
  8530. break;
  8531. case SP_MAXAP:
  8532. sd->battle_status.max_ap = cap_value(val, 1, battle_config.max_ap);
  8533. if (sd->battle_status.max_ap < sd->battle_status.ap) {
  8534. sd->battle_status.ap = sd->battle_status.max_ap;
  8535. clif_updatestatus(sd, SP_AP);
  8536. }
  8537. break;
  8538. case SP_STR:
  8539. sd->status.str = cap_value(val, 1, pc_maxparameter(sd,PARAM_STR));
  8540. break;
  8541. case SP_AGI:
  8542. sd->status.agi = cap_value(val, 1, pc_maxparameter(sd,PARAM_AGI));
  8543. break;
  8544. case SP_VIT:
  8545. sd->status.vit = cap_value(val, 1, pc_maxparameter(sd,PARAM_VIT));
  8546. break;
  8547. case SP_INT:
  8548. sd->status.int_ = cap_value(val, 1, pc_maxparameter(sd,PARAM_INT));
  8549. break;
  8550. case SP_DEX:
  8551. sd->status.dex = cap_value(val, 1, pc_maxparameter(sd,PARAM_DEX));
  8552. break;
  8553. case SP_LUK:
  8554. sd->status.luk = cap_value(val, 1, pc_maxparameter(sd,PARAM_LUK));
  8555. break;
  8556. case SP_POW:
  8557. sd->status.pow = cap_value(val, 0, pc_maxparameter(sd,PARAM_POW));
  8558. break;
  8559. case SP_STA:
  8560. sd->status.sta = cap_value(val, 0, pc_maxparameter(sd,PARAM_STA));
  8561. break;
  8562. case SP_WIS:
  8563. sd->status.wis = cap_value(val, 0, pc_maxparameter(sd,PARAM_WIS));
  8564. break;
  8565. case SP_SPL:
  8566. sd->status.spl = cap_value(val, 0, pc_maxparameter(sd,PARAM_SPL));
  8567. break;
  8568. case SP_CON:
  8569. sd->status.con = cap_value(val, 0, pc_maxparameter(sd,PARAM_CON));
  8570. break;
  8571. case SP_CRT:
  8572. sd->status.crt = cap_value(val, 0, pc_maxparameter(sd,PARAM_CRT));
  8573. break;
  8574. case SP_KARMA:
  8575. sd->status.karma = val;
  8576. break;
  8577. case SP_MANNER:
  8578. sd->status.manner = val;
  8579. if( val < 0 )
  8580. sc_start(NULL, &sd->bl, SC_NOCHAT, 100, 0, 0);
  8581. else {
  8582. status_change_end(&sd->bl, SC_NOCHAT, INVALID_TIMER);
  8583. clif_manner_message(sd, 5);
  8584. }
  8585. return true; // status_change_start/status_change_end already sends packets warning the client
  8586. case SP_FAME:
  8587. sd->status.fame = val;
  8588. break;
  8589. case SP_KILLERRID:
  8590. sd->killerrid = val;
  8591. return true;
  8592. case SP_KILLEDRID:
  8593. sd->killedrid = val;
  8594. return true;
  8595. case SP_KILLEDGID:
  8596. sd->killedgid = val;
  8597. return true;
  8598. case SP_CHARMOVE:
  8599. sd->status.character_moves = val;
  8600. return true;
  8601. case SP_CHARRENAME:
  8602. sd->status.rename = val;
  8603. return true;
  8604. case SP_CHARFONT:
  8605. sd->status.font = val;
  8606. clif_font(sd);
  8607. return true;
  8608. case SP_BANK_VAULT:
  8609. if (val < 0)
  8610. return false;
  8611. log_zeny(sd, LOG_TYPE_BANK, sd, -(sd->bank_vault - cap_value(val, 0, MAX_BANK_ZENY)));
  8612. sd->bank_vault = cap_value(val, 0, MAX_BANK_ZENY);
  8613. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  8614. return true;
  8615. case SP_ROULETTE_BRONZE:
  8616. sd->roulette_point.bronze = val;
  8617. pc_setreg2(sd, ROULETTE_BRONZE_VAR, sd->roulette_point.bronze);
  8618. return true;
  8619. case SP_ROULETTE_SILVER:
  8620. sd->roulette_point.silver = val;
  8621. pc_setreg2(sd, ROULETTE_SILVER_VAR, sd->roulette_point.silver);
  8622. return true;
  8623. case SP_ROULETTE_GOLD:
  8624. sd->roulette_point.gold = val;
  8625. pc_setreg2(sd, ROULETTE_GOLD_VAR, sd->roulette_point.gold);
  8626. return true;
  8627. case SP_CASHPOINTS:
  8628. if (val < 0)
  8629. return false;
  8630. if (!sd->state.connect_new)
  8631. log_cash(sd, LOG_TYPE_SCRIPT, LOG_CASH_TYPE_CASH, -(sd->cashPoints - cap_value(val, 0, MAX_ZENY)));
  8632. sd->cashPoints = cap_value(val, 0, MAX_ZENY);
  8633. pc_setaccountreg(sd, add_str(CASHPOINT_VAR), sd->cashPoints);
  8634. return true;
  8635. case SP_KAFRAPOINTS:
  8636. if (val < 0)
  8637. return false;
  8638. if (!sd->state.connect_new)
  8639. log_cash(sd, LOG_TYPE_SCRIPT, LOG_CASH_TYPE_KAFRA, -(sd->kafraPoints - cap_value(val, 0, MAX_ZENY)));
  8640. sd->kafraPoints = cap_value(val, 0, MAX_ZENY);
  8641. pc_setaccountreg(sd, add_str(KAFRAPOINT_VAR), sd->kafraPoints);
  8642. return true;
  8643. case SP_PCDIECOUNTER:
  8644. if (val < 0)
  8645. return false;
  8646. if (sd->die_counter == val)
  8647. return true;
  8648. sd->die_counter = val;
  8649. if (!sd->state.connect_new && sd->die_counter == 1 && (sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE)
  8650. status_calc_pc(sd, SCO_NONE); // Lost the bonus.
  8651. pc_setglobalreg(sd, add_str(PCDIECOUNTER_VAR), sd->die_counter);
  8652. return true;
  8653. case SP_COOKMASTERY:
  8654. if (sd->cook_mastery == val)
  8655. return true;
  8656. val = cap_value(val, 0, 1999);
  8657. sd->cook_mastery = val;
  8658. pc_setglobalreg(sd, add_str(COOKMASTERY_VAR), sd->cook_mastery);
  8659. return true;
  8660. default:
  8661. ShowError("pc_setparam: Attempted to set unknown parameter '%lld'.\n", type);
  8662. return false;
  8663. }
  8664. clif_updatestatus(sd,static_cast<int>(type));
  8665. return true;
  8666. }
  8667. /*==========================================
  8668. * HP/SP/AP Healing. If flag is passed, the heal type is through clif_heal, otherwise update status.
  8669. *------------------------------------------*/
  8670. void pc_heal(struct map_session_data *sd,unsigned int hp,unsigned int sp, unsigned int ap, int type)
  8671. {// Is there going to be a effect for gaining AP soon??? [Rytech]
  8672. if (type&2) {
  8673. if (hp || type&4)
  8674. clif_heal(sd->fd,SP_HP,hp);
  8675. if (sp)
  8676. clif_heal(sd->fd,SP_SP,sp);
  8677. if (ap)
  8678. clif_heal(sd->fd,SP_AP,ap);
  8679. } else {
  8680. if(hp)
  8681. clif_updatestatus(sd,SP_HP);
  8682. if(sp)
  8683. clif_updatestatus(sd,SP_SP);
  8684. if (ap)
  8685. clif_updatestatus(sd,SP_AP);
  8686. }
  8687. return;
  8688. }
  8689. /**
  8690. * Heal player HP and/or SP linearly. Calculate any bonus based on active statuses.
  8691. * @param sd: Player data
  8692. * @param itemid: Item ID
  8693. * @param hp: HP to heal
  8694. * @param sp: SP to heal
  8695. * @return Amount healed to an object
  8696. */
  8697. int pc_itemheal(struct map_session_data *sd, t_itemid itemid, int hp, int sp)
  8698. {
  8699. int bonus, tmp, penalty = 0;
  8700. if (hp) {
  8701. bonus = 100 + (sd->battle_status.vit << 1) + pc_checkskill(sd, SM_RECOVERY) * 10 + pc_checkskill(sd, AM_LEARNINGPOTION) * 5;
  8702. // A potion produced by an Alchemist in the Fame Top 10 gets +50% effect [DracoRPG]
  8703. if (potion_flag == 2) {
  8704. bonus += bonus * 50 / 100;
  8705. if (sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_ROGUE)
  8706. bonus += bonus; // Receive an additional +100% effect from ranked potions to HP only
  8707. }
  8708. //All item bonuses.
  8709. bonus += sd->bonus.itemhealrate2;
  8710. //Item Group bonuses
  8711. bonus += bonus * pc_get_itemgroup_bonus(sd, itemid, sd->itemgrouphealrate) / 100;
  8712. //Individual item bonuses.
  8713. for(const auto &it : sd->itemhealrate) {
  8714. if (it.id == itemid) {
  8715. bonus += bonus * it.val / 100;
  8716. break;
  8717. }
  8718. }
  8719. // Recovery Potion
  8720. if (sd->sc.data[SC_INCHEALRATE])
  8721. bonus += bonus * sd->sc.data[SC_INCHEALRATE]->val1 / 100;
  8722. // 2014 Halloween Event : Pumpkin Bonus
  8723. if (sd->sc.data[SC_MTF_PUMPKIN]) {
  8724. if (itemid == ITEMID_PUMPKIN)
  8725. bonus += bonus * sd->sc.data[SC_MTF_PUMPKIN]->val1 / 100;
  8726. else if (itemid == ITEMID_COOKIE_BAT)
  8727. bonus += sd->sc.data[SC_MTF_PUMPKIN]->val2;
  8728. }
  8729. tmp = hp * bonus / 100; // Overflow check
  8730. if (bonus != 100 && tmp > hp)
  8731. hp = tmp;
  8732. }
  8733. if (sp) {
  8734. bonus = 100 + (sd->battle_status.int_ << 1) + pc_checkskill(sd, MG_SRECOVERY) * 10 + pc_checkskill(sd, AM_LEARNINGPOTION) * 5;
  8735. // A potion produced by an Alchemist in the Fame Top 10 gets +50% effect [DracoRPG]
  8736. if (potion_flag == 2)
  8737. bonus += bonus * 50 / 100;
  8738. // All item bonuses.
  8739. bonus += sd->bonus.itemsphealrate2;
  8740. // Item Group bonuses
  8741. bonus += bonus * pc_get_itemgroup_bonus( sd, itemid, sd->itemgroupsphealrate ) / 100;
  8742. // Individual item bonuses.
  8743. for( const auto &it : sd->itemsphealrate ){
  8744. if( it.id == itemid ){
  8745. bonus += bonus * it.val / 100;
  8746. break;
  8747. }
  8748. }
  8749. tmp = sp * bonus / 100; // Overflow check
  8750. if (bonus != 100 && tmp > sp)
  8751. sp = tmp;
  8752. }
  8753. if (sd->sc.count) {
  8754. // Critical Wound and Death Hurt stack
  8755. if (sd->sc.data[SC_CRITICALWOUND])
  8756. penalty += sd->sc.data[SC_CRITICALWOUND]->val2;
  8757. if (sd->sc.data[SC_DEATHHURT] && sd->sc.data[SC_DEATHHURT]->val3 == 1)
  8758. penalty += 20;
  8759. if (sd->sc.data[SC_NORECOVER_STATE])
  8760. penalty = 100;
  8761. if (sd->sc.data[SC_VITALITYACTIVATION])
  8762. hp += hp / 2; // 1.5 times
  8763. if (sd->sc.data[SC_WATER_INSIGNIA] && sd->sc.data[SC_WATER_INSIGNIA]->val1 == 2) {
  8764. hp += hp / 10;
  8765. sp += sp / 10;
  8766. }
  8767. #ifdef RENEWAL
  8768. if (sd->sc.data[SC_APPLEIDUN])
  8769. hp += sd->sc.data[SC_APPLEIDUN]->val3 / 100;
  8770. #endif
  8771. if (penalty > 0) {
  8772. hp -= hp * penalty / 100;
  8773. sp -= sp * penalty / 100;
  8774. }
  8775. #ifdef RENEWAL
  8776. if (sd->sc.data[SC_EXTREMITYFIST2])
  8777. sp = 0;
  8778. #endif
  8779. if (sd->sc.data[SC_BITESCAR])
  8780. hp = 0;
  8781. }
  8782. return status_heal(&sd->bl, hp, sp, 1);
  8783. }
  8784. /*==========================================
  8785. * HP/SP Recovery
  8786. * Heal player hp nad/or sp by rate
  8787. *------------------------------------------*/
  8788. int pc_percentheal(struct map_session_data *sd,int hp,int sp)
  8789. {
  8790. nullpo_ret(sd);
  8791. if (hp > 100) hp = 100;
  8792. else if (hp <-100) hp = -100;
  8793. if (sp > 100) sp = 100;
  8794. else if (sp <-100) sp = -100;
  8795. if(hp >= 0 && sp >= 0) //Heal
  8796. return status_percent_heal(&sd->bl, hp, sp);
  8797. if(hp <= 0 && sp <= 0) //Damage (negative rates indicate % of max rather than current), and only kill target IF the specified amount is 100%
  8798. return status_percent_damage(NULL, &sd->bl, hp, sp, hp==-100);
  8799. //Crossed signs
  8800. if(hp) {
  8801. if(hp > 0)
  8802. status_percent_heal(&sd->bl, hp, 0);
  8803. else
  8804. status_percent_damage(NULL, &sd->bl, hp, 0, hp==-100);
  8805. }
  8806. if(sp) {
  8807. if(sp > 0)
  8808. status_percent_heal(&sd->bl, 0, sp);
  8809. else
  8810. status_percent_damage(NULL, &sd->bl, 0, sp, false);
  8811. }
  8812. return 0;
  8813. }
  8814. static int jobchange_killclone(struct block_list *bl, va_list ap)
  8815. {
  8816. struct mob_data *md;
  8817. int flag;
  8818. md = (struct mob_data *)bl;
  8819. nullpo_ret(md);
  8820. flag = va_arg(ap, int);
  8821. if (md->master_id && md->special_state.clone && md->master_id == flag)
  8822. status_kill(&md->bl);
  8823. return 1;
  8824. }
  8825. /**
  8826. * Called when player changes job
  8827. * Rewrote to make it tidider [Celest]
  8828. * @param sd
  8829. * @param job JOB ID. See enum e_job
  8830. * @param upper 1 - JOBL_UPPER; 2 - JOBL_BABY
  8831. * @return True if success, false if failed
  8832. **/
  8833. bool pc_jobchange(struct map_session_data *sd,int job, char upper)
  8834. {
  8835. int i, fame_flag = 0;
  8836. nullpo_retr(false,sd);
  8837. if (job < 0)
  8838. return false;
  8839. //Normalize job.
  8840. uint64 b_class = pc_jobid2mapid(job);
  8841. if (b_class == -1)
  8842. return false;
  8843. switch (upper) {
  8844. case 1:
  8845. b_class|= JOBL_UPPER;
  8846. break;
  8847. case 2:
  8848. b_class|= JOBL_BABY;
  8849. break;
  8850. }
  8851. //This will automatically adjust bard/dancer classes to the correct gender
  8852. //That is, if you try to jobchange into dancer, it will turn you to bard.
  8853. job = pc_mapid2jobid(b_class, sd->status.sex);
  8854. if (job == -1)
  8855. return false;
  8856. if ((unsigned short)b_class == sd->class_)
  8857. return false; //Nothing to change.
  8858. // If the job does not exist in the job db, dont allow changing to it
  8859. if( !job_db.exists( job ) ){
  8860. return false;
  8861. }
  8862. if( ( b_class&JOBL_FOURTH ) && !( sd->class_&JOBL_FOURTH ) ){
  8863. // Changing to 4th job
  8864. sd->change_level_4th = sd->status.job_level;
  8865. pc_setglobalreg( sd, add_str( JOBCHANGE4TH_VAR ) , sd->change_level_4th );
  8866. }else if( ( b_class&JOBL_THIRD ) && !( sd->class_&JOBL_THIRD ) ){
  8867. // changing from 2nd to 3rd job
  8868. sd->change_level_3rd = sd->status.job_level;
  8869. pc_setglobalreg( sd, add_str( JOBCHANGE3RD_VAR ), sd->change_level_3rd );
  8870. }else if( ( b_class&JOBL_2 ) && !( sd->class_&JOBL_2 ) ){
  8871. // changing from 1st to 2nd job
  8872. sd->change_level_2nd = sd->status.job_level;
  8873. pc_setglobalreg( sd, add_str( JOBCHANGE2ND_VAR ), sd->change_level_2nd );
  8874. }
  8875. if(sd->cloneskill_idx > 0) {
  8876. if( sd->status.skill[sd->cloneskill_idx].flag == SKILL_FLAG_PLAGIARIZED ) {
  8877. sd->status.skill[sd->cloneskill_idx].id = 0;
  8878. sd->status.skill[sd->cloneskill_idx].lv = 0;
  8879. sd->status.skill[sd->cloneskill_idx].flag = SKILL_FLAG_PERMANENT;
  8880. clif_deleteskill(sd, static_cast<int>(pc_readglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM))));
  8881. }
  8882. sd->cloneskill_idx = 0;
  8883. pc_setglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM), 0);
  8884. pc_setglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM_LV), 0);
  8885. }
  8886. if(sd->reproduceskill_idx > 0) {
  8887. if( sd->status.skill[sd->reproduceskill_idx].flag == SKILL_FLAG_PLAGIARIZED ) {
  8888. sd->status.skill[sd->reproduceskill_idx].id = 0;
  8889. sd->status.skill[sd->reproduceskill_idx].lv = 0;
  8890. sd->status.skill[sd->reproduceskill_idx].flag = SKILL_FLAG_PERMANENT;
  8891. clif_deleteskill(sd, static_cast<int>(pc_readglobalreg(sd, add_str(SKILL_VAR_REPRODUCE))));
  8892. }
  8893. sd->reproduceskill_idx = 0;
  8894. pc_setglobalreg(sd, add_str(SKILL_VAR_REPRODUCE), 0);
  8895. pc_setglobalreg(sd, add_str(SKILL_VAR_REPRODUCE_LV), 0);
  8896. }
  8897. if ( (b_class&MAPID_UPPERMASK) != (sd->class_&MAPID_UPPERMASK) ) { //Things to remove when changing class tree.
  8898. status_db.changeSkillTree(sd);
  8899. }
  8900. if( (sd->class_&MAPID_UPPERMASK) == MAPID_STAR_GLADIATOR && (b_class&MAPID_UPPERMASK) != MAPID_STAR_GLADIATOR) {
  8901. /* going off star glad lineage, reset feel and hate to not store no-longer-used vars in the database */
  8902. pc_resetfeel(sd);
  8903. pc_resethate(sd);
  8904. }
  8905. // Reset body style to 0 before changing job to avoid
  8906. // errors since not every job has a alternate outfit.
  8907. sd->status.body = 0;
  8908. clif_changelook(&sd->bl,LOOK_BODY2,0);
  8909. sd->status.class_ = job;
  8910. fame_flag = pc_famerank(sd->status.char_id,sd->class_&MAPID_UPPERMASK);
  8911. uint64 previous_class = sd->class_;
  8912. sd->class_ = (unsigned short)b_class;
  8913. sd->status.job_level=1;
  8914. sd->status.job_exp=0;
  8915. if (sd->status.base_level > pc_maxbaselv(sd)) {
  8916. sd->status.base_level = pc_maxbaselv(sd);
  8917. sd->status.base_exp=0;
  8918. pc_resetstate(sd);
  8919. clif_updatestatus(sd,SP_STATUSPOINT);
  8920. clif_updatestatus(sd,SP_TRAITPOINT);
  8921. clif_updatestatus(sd,SP_BASELEVEL);
  8922. clif_updatestatus(sd,SP_BASEEXP);
  8923. clif_updatestatus(sd,SP_NEXTBASEEXP);
  8924. }
  8925. // Give or reduce transcendent status points
  8926. if( (b_class&JOBL_UPPER) && !(previous_class&JOBL_UPPER) ){ // Change from a non t class to a t class -> give points
  8927. sd->status.status_point += battle_config.transcendent_status_points;
  8928. clif_updatestatus(sd,SP_STATUSPOINT);
  8929. }else if( !(b_class&JOBL_UPPER) && (previous_class&JOBL_UPPER) ){ // Change from a t class to a non t class -> remove points
  8930. if( sd->status.status_point < battle_config.transcendent_status_points ){
  8931. // The player already used his bonus points, so we have to reset his status points
  8932. pc_resetstate(sd);
  8933. }else{
  8934. sd->status.status_point -= battle_config.transcendent_status_points;
  8935. clif_updatestatus(sd,SP_STATUSPOINT);
  8936. }
  8937. }
  8938. // Give or reduce trait status points
  8939. if ((b_class & JOBL_FOURTH) && !(previous_class & JOBL_FOURTH)) {// Change to a 4th job.
  8940. sd->status.trait_point += battle_config.trait_points_job_change;
  8941. clif_updatestatus(sd, SP_TRAITPOINT);
  8942. clif_updatestatus(sd, SP_UPOW);
  8943. clif_updatestatus(sd, SP_USTA);
  8944. clif_updatestatus(sd, SP_UWIS);
  8945. clif_updatestatus(sd, SP_USPL);
  8946. clif_updatestatus(sd, SP_UCON);
  8947. clif_updatestatus(sd, SP_UCRT);
  8948. } else if (!(b_class & JOBL_FOURTH) && (previous_class & JOBL_FOURTH)) {// Change to a non 4th job.
  8949. if (sd->status.trait_point < battle_config.trait_points_job_change) {
  8950. // Player may have already used the trait status points. Force a reset.
  8951. pc_resetstate(sd);
  8952. } else {
  8953. sd->status.trait_point = 0;
  8954. clif_updatestatus(sd, SP_TRAITPOINT);
  8955. clif_updatestatus(sd, SP_UPOW);
  8956. clif_updatestatus(sd, SP_USTA);
  8957. clif_updatestatus(sd, SP_UWIS);
  8958. clif_updatestatus(sd, SP_USPL);
  8959. clif_updatestatus(sd, SP_UCON);
  8960. clif_updatestatus(sd, SP_UCRT);
  8961. }
  8962. }
  8963. clif_updatestatus(sd,SP_JOBLEVEL);
  8964. clif_updatestatus(sd,SP_JOBEXP);
  8965. clif_updatestatus(sd,SP_NEXTJOBEXP);
  8966. for(i=0;i<EQI_MAX;i++) {
  8967. if(sd->equip_index[i] >= 0)
  8968. if(pc_isequip(sd,sd->equip_index[i]))
  8969. pc_unequipitem(sd,sd->equip_index[i],2); // unequip invalid item for class
  8970. }
  8971. //Change look, if disguised, you need to undisguise
  8972. //to correctly calculate new job sprite without
  8973. if (sd->disguise)
  8974. pc_disguise(sd, 0);
  8975. status_set_viewdata(&sd->bl, job);
  8976. clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_); // move sprite update to prevent client crashes with incompatible equipment [Valaris]
  8977. #if PACKETVER >= 20151001
  8978. clif_changelook(&sd->bl, LOOK_HAIR, sd->vd.hair_style); // Update player's head (only matters when switching to or from Doram)
  8979. #endif
  8980. if(sd->vd.cloth_color)
  8981. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  8982. /*
  8983. if(sd->vd.body_style)
  8984. clif_changelook(&sd->bl,LOOK_BODY2,sd->vd.body_style);
  8985. */
  8986. //Update skill tree.
  8987. pc_calc_skilltree(sd);
  8988. clif_skillinfoblock(sd);
  8989. if (sd->ed)
  8990. elemental_delete(sd->ed);
  8991. if (sd->state.vending)
  8992. vending_closevending(sd);
  8993. if (sd->state.buyingstore)
  8994. buyingstore_close(sd);
  8995. map_foreachinmap(jobchange_killclone, sd->bl.m, BL_MOB, sd->bl.id);
  8996. //Remove peco/cart/falcon
  8997. i = sd->sc.option;
  8998. if( i&OPTION_RIDING && !pc_checkskill(sd, KN_RIDING) )
  8999. i&=~OPTION_RIDING;
  9000. if( i&OPTION_FALCON && !pc_checkskill(sd, HT_FALCON) )
  9001. i&=~OPTION_FALCON;
  9002. if( i&OPTION_DRAGON && !pc_checkskill(sd,RK_DRAGONTRAINING) )
  9003. i&=~OPTION_DRAGON;
  9004. if( i&OPTION_WUGRIDER && !pc_checkskill(sd,RA_WUGMASTERY) )
  9005. i&=~OPTION_WUGRIDER;
  9006. if( i&OPTION_WUG && !pc_checkskill(sd,RA_WUGMASTERY) )
  9007. i&=~OPTION_WUG;
  9008. if( i&OPTION_MADOGEAR ) //You do not need a skill for this.
  9009. i&=~OPTION_MADOGEAR;
  9010. #ifndef NEW_CARTS
  9011. if( i&OPTION_CART && !pc_checkskill(sd, MC_PUSHCART) )
  9012. i&=~OPTION_CART;
  9013. #else
  9014. if( sd->sc.data[SC_PUSH_CART] && !pc_checkskill(sd, MC_PUSHCART) )
  9015. pc_setcart(sd, 0);
  9016. #endif
  9017. if(i != sd->sc.option)
  9018. pc_setoption(sd, i);
  9019. if(hom_is_active(sd->hd) && !pc_checkskill(sd, AM_CALLHOMUN))
  9020. hom_vaporize(sd, HOM_ST_ACTIVE);
  9021. if (sd->sc.data[SC_SPRITEMABLE] && !pc_checkskill(sd, SU_SPRITEMABLE))
  9022. status_change_end(&sd->bl, SC_SPRITEMABLE, INVALID_TIMER);
  9023. if (sd->sc.data[SC_SOULATTACK] && !pc_checkskill(sd, SU_SOULATTACK))
  9024. status_change_end(&sd->bl, SC_SOULATTACK, INVALID_TIMER);
  9025. if( sd->sc.data[SC_SPIRIT] ){
  9026. status_change_end( &sd->bl, SC_SPIRIT, INVALID_TIMER );
  9027. }
  9028. if(sd->status.manner < 0)
  9029. clif_changestatus(sd,SP_MANNER,sd->status.manner);
  9030. status_calc_pc(sd,SCO_FORCE);
  9031. pc_checkallowskill(sd);
  9032. pc_equiplookall(sd);
  9033. pc_show_questinfo(sd);
  9034. achievement_update_objective(sd, AG_JOB_CHANGE, 2, sd->status.base_level, job);
  9035. if( sd->status.party_id ){
  9036. struct party_data* p;
  9037. if( ( p = party_search( sd->status.party_id ) ) != NULL ){
  9038. ARR_FIND(0, MAX_PARTY, i, p->party.member[i].char_id == sd->status.char_id);
  9039. if( i < MAX_PARTY ){
  9040. p->party.member[i].class_ = sd->status.class_;
  9041. clif_party_job_and_level(sd);
  9042. }
  9043. }
  9044. }
  9045. chrif_save(sd, CSAVE_NORMAL);
  9046. //if you were previously famous, not anymore.
  9047. if (fame_flag)
  9048. chrif_buildfamelist();
  9049. else if (sd->status.fame > 0) {
  9050. //It may be that now they are famous?
  9051. switch (sd->class_&MAPID_UPPERMASK) {
  9052. case MAPID_BLACKSMITH:
  9053. case MAPID_ALCHEMIST:
  9054. case MAPID_TAEKWON:
  9055. chrif_buildfamelist();
  9056. break;
  9057. }
  9058. }
  9059. return true;
  9060. }
  9061. /*==========================================
  9062. * Tell client player sd has change equipement
  9063. *------------------------------------------*/
  9064. void pc_equiplookall(struct map_session_data *sd)
  9065. {
  9066. nullpo_retv(sd);
  9067. clif_changelook(&sd->bl,LOOK_WEAPON,0);
  9068. clif_changelook(&sd->bl,LOOK_SHOES,0);
  9069. clif_changelook(&sd->bl,LOOK_HEAD_BOTTOM,sd->status.head_bottom);
  9070. clif_changelook(&sd->bl,LOOK_HEAD_TOP,sd->status.head_top);
  9071. clif_changelook(&sd->bl,LOOK_HEAD_MID,sd->status.head_mid);
  9072. clif_changelook(&sd->bl,LOOK_ROBE, sd->status.robe);
  9073. }
  9074. /*==========================================
  9075. * Tell client player sd has change look (hair,equip...)
  9076. *------------------------------------------*/
  9077. void pc_changelook(struct map_session_data *sd,int type,int val) {
  9078. nullpo_retv(sd);
  9079. switch(type) {
  9080. case LOOK_HAIR: //Use the battle_config limits! [Skotlex]
  9081. val = cap_value(val, MIN_HAIR_STYLE, MAX_HAIR_STYLE);
  9082. if (sd->status.hair != val) {
  9083. sd->status.hair = val;
  9084. if (sd->status.guild_id) //Update Guild Window. [Skotlex]
  9085. intif_guild_change_memberinfo(sd->status.guild_id, sd->status.account_id, sd->status.char_id,
  9086. GMI_HAIR, &sd->status.hair, sizeof(sd->status.hair));
  9087. }
  9088. break;
  9089. case LOOK_WEAPON:
  9090. sd->status.weapon = val;
  9091. break;
  9092. case LOOK_HEAD_BOTTOM:
  9093. sd->status.head_bottom = val;
  9094. sd->setlook_head_bottom = val;
  9095. break;
  9096. case LOOK_HEAD_TOP:
  9097. sd->status.head_top = val;
  9098. sd->setlook_head_top = val;
  9099. break;
  9100. case LOOK_HEAD_MID:
  9101. sd->status.head_mid = val;
  9102. sd->setlook_head_mid = val;
  9103. break;
  9104. case LOOK_HAIR_COLOR: //Use the battle_config limits! [Skotlex]
  9105. val = cap_value(val, MIN_HAIR_COLOR, MAX_HAIR_COLOR);
  9106. if (sd->status.hair_color != val) {
  9107. sd->status.hair_color = val;
  9108. if (sd->status.guild_id) //Update Guild Window. [Skotlex]
  9109. intif_guild_change_memberinfo(sd->status.guild_id, sd->status.account_id, sd->status.char_id,
  9110. GMI_HAIR_COLOR, &sd->status.hair_color, sizeof(sd->status.hair_color));
  9111. }
  9112. break;
  9113. case LOOK_CLOTHES_COLOR: //Use the battle_config limits! [Skotlex]
  9114. val = cap_value(val, MIN_CLOTH_COLOR, MAX_CLOTH_COLOR);
  9115. sd->status.clothes_color = val;
  9116. break;
  9117. case LOOK_SHIELD:
  9118. sd->status.shield = val;
  9119. break;
  9120. case LOOK_SHOES:
  9121. break;
  9122. case LOOK_ROBE:
  9123. sd->status.robe = val;
  9124. sd->setlook_robe = val;
  9125. break;
  9126. case LOOK_BODY2:
  9127. val = cap_value(val, MIN_BODY_STYLE, MAX_BODY_STYLE);
  9128. sd->status.body = val;
  9129. break;
  9130. }
  9131. clif_changelook(&sd->bl, type, val);
  9132. }
  9133. /*==========================================
  9134. * Give an option (type) to player (sd) and display it to client
  9135. *------------------------------------------*/
  9136. void pc_setoption(struct map_session_data *sd,int type, int subtype)
  9137. {
  9138. int p_type, new_look=0;
  9139. nullpo_retv(sd);
  9140. p_type = sd->sc.option;
  9141. //Option has to be changed client-side before the class sprite or it won't always work (eg: Wedding sprite) [Skotlex]
  9142. sd->sc.option=type;
  9143. clif_changeoption(&sd->bl);
  9144. if( (type&OPTION_RIDING && !(p_type&OPTION_RIDING)) || (type&OPTION_DRAGON && !(p_type&OPTION_DRAGON) && pc_checkskill(sd,RK_DRAGONTRAINING) > 0) )
  9145. { // Mounting
  9146. clif_status_load(&sd->bl,EFST_RIDING,1);
  9147. status_calc_pc(sd,SCO_NONE);
  9148. }
  9149. else if( (!(type&OPTION_RIDING) && p_type&OPTION_RIDING) || (!(type&OPTION_DRAGON) && p_type&OPTION_DRAGON && pc_checkskill(sd,RK_DRAGONTRAINING) > 0) )
  9150. { // Dismount
  9151. clif_status_load(&sd->bl,EFST_RIDING,0);
  9152. status_calc_pc(sd,SCO_NONE);
  9153. }
  9154. #ifndef NEW_CARTS
  9155. if( type&OPTION_CART && !( p_type&OPTION_CART ) ) { //Cart On
  9156. clif_cartlist(sd);
  9157. clif_updatestatus(sd, SP_CARTINFO);
  9158. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  9159. status_calc_pc(sd,SCO_NONE); //Apply speed penalty.
  9160. } else if( !( type&OPTION_CART ) && p_type&OPTION_CART ){ //Cart Off
  9161. clif_clearcart(sd->fd);
  9162. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  9163. status_calc_pc(sd,SCO_NONE); //Remove speed penalty.
  9164. }
  9165. #endif
  9166. if (type&OPTION_FALCON && !(p_type&OPTION_FALCON)) //Falcon ON
  9167. clif_status_load(&sd->bl,EFST_FALCON,1);
  9168. else if (!(type&OPTION_FALCON) && p_type&OPTION_FALCON) //Falcon OFF
  9169. clif_status_load(&sd->bl,EFST_FALCON,0);
  9170. if( type&OPTION_WUGRIDER && !(p_type&OPTION_WUGRIDER) ) { // Mounting
  9171. clif_status_load(&sd->bl,EFST_WUGRIDER,1);
  9172. status_calc_pc(sd,SCO_NONE);
  9173. } else if( !(type&OPTION_WUGRIDER) && p_type&OPTION_WUGRIDER ) { // Dismount
  9174. clif_status_load(&sd->bl,EFST_WUGRIDER,0);
  9175. status_calc_pc(sd,SCO_NONE);
  9176. }
  9177. if( type&OPTION_MADOGEAR && !(p_type&OPTION_MADOGEAR) ) {
  9178. sc_start(&sd->bl, &sd->bl, SC_MADOGEAR, 100, subtype, INFINITE_TICK);
  9179. } else if( !(type&OPTION_MADOGEAR) && p_type&OPTION_MADOGEAR ) {
  9180. status_change_end(&sd->bl, SC_MADOGEAR, INVALID_TIMER);
  9181. }
  9182. if (type&OPTION_FLYING && !(p_type&OPTION_FLYING))
  9183. new_look = JOB_STAR_GLADIATOR2;
  9184. else if (!(type&OPTION_FLYING) && p_type&OPTION_FLYING)
  9185. new_look = -1;
  9186. if (sd->disguise || !new_look)
  9187. return; //Disguises break sprite changes
  9188. if (new_look < 0) { //Restore normal look.
  9189. status_set_viewdata(&sd->bl, sd->status.class_);
  9190. new_look = sd->vd.class_;
  9191. }
  9192. pc_stop_attack(sd); //Stop attacking on new view change (to prevent wedding/santa attacks.
  9193. clif_changelook(&sd->bl,LOOK_BASE,new_look);
  9194. if (sd->vd.cloth_color)
  9195. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  9196. if( sd->vd.body_style )
  9197. clif_changelook(&sd->bl,LOOK_BODY2,sd->vd.body_style);
  9198. clif_skillinfoblock(sd); // Skill list needs to be updated after base change.
  9199. }
  9200. /**
  9201. * Give player a cart
  9202. * @param sd Player
  9203. * @param type 0:Remove cart, 1 ~ MAX_CARTS: Cart type
  9204. **/
  9205. bool pc_setcart(struct map_session_data *sd,int type) {
  9206. #ifndef NEW_CARTS
  9207. int cart[6] = {0x0000,OPTION_CART1,OPTION_CART2,OPTION_CART3,OPTION_CART4,OPTION_CART5};
  9208. int option;
  9209. #endif
  9210. nullpo_retr(false,sd);
  9211. if( type < 0 || type > MAX_CARTS )
  9212. return false;// Never trust the values sent by the client! [Skotlex]
  9213. if( pc_checkskill(sd,MC_PUSHCART) <= 0 && type != 0 )
  9214. return false;// Push cart is required
  9215. #ifdef NEW_CARTS
  9216. switch( type ) {
  9217. case 0:
  9218. if( !sd->sc.data[SC_PUSH_CART] )
  9219. return 0;
  9220. status_change_end(&sd->bl,SC_PUSH_CART,INVALID_TIMER);
  9221. clif_clearcart(sd->fd);
  9222. break;
  9223. default:/* everything else is an allowed ID so we can move on */
  9224. if( !sd->sc.data[SC_PUSH_CART] ) { /* first time, so fill cart data */
  9225. clif_cartlist(sd);
  9226. status_calc_cart_weight(sd, (e_status_calc_weight_opt)(CALCWT_ITEM|CALCWT_MAXBONUS|CALCWT_CARTSTATE));
  9227. }
  9228. clif_updatestatus(sd, SP_CARTINFO);
  9229. sc_start(&sd->bl, &sd->bl, SC_PUSH_CART, 100, type, 0);
  9230. break;
  9231. }
  9232. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  9233. status_calc_pc(sd,SCO_NONE); //Recalc speed penalty.
  9234. #else
  9235. // Update option
  9236. option = sd->sc.option;
  9237. option &= ~OPTION_CART;// clear cart bits
  9238. option |= cart[type]; // set cart
  9239. pc_setoption(sd, option);
  9240. #endif
  9241. return true;
  9242. }
  9243. /*==========================================
  9244. * Give player a falcon
  9245. *------------------------------------------*/
  9246. void pc_setfalcon(struct map_session_data* sd, int flag)
  9247. {
  9248. if( flag ){
  9249. if( pc_checkskill(sd,HT_FALCON)>0 ) // add falcon if he have the skill
  9250. pc_setoption(sd,sd->sc.option|OPTION_FALCON);
  9251. } else if( pc_isfalcon(sd) ){
  9252. pc_setoption(sd,sd->sc.option&~OPTION_FALCON); // remove falcon
  9253. }
  9254. }
  9255. /*==========================================
  9256. * Set player riding
  9257. *------------------------------------------*/
  9258. void pc_setriding(struct map_session_data* sd, int flag)
  9259. {
  9260. if( sd->sc.data[SC_ALL_RIDING] )
  9261. return;
  9262. if( flag ){
  9263. if( pc_checkskill(sd,KN_RIDING) > 0 ) // add peco
  9264. pc_setoption(sd, sd->sc.option|OPTION_RIDING);
  9265. } else if( pc_isriding(sd) ){
  9266. pc_setoption(sd, sd->sc.option&~OPTION_RIDING);
  9267. }
  9268. }
  9269. /**
  9270. * Give player a mado
  9271. * @param sd: Player
  9272. * @param flag: Enable or disable mado
  9273. * @param type: See pc.hpp::e_mado_type (Default is MADO_ROBOT)
  9274. */
  9275. void pc_setmadogear(struct map_session_data *sd, bool flag, e_mado_type type)
  9276. {
  9277. if ((sd->class_ & MAPID_THIRDMASK) != MAPID_MECHANIC)
  9278. return;
  9279. if (flag) {
  9280. if (pc_checkskill(sd, NC_MADOLICENCE) > 0)
  9281. pc_setoption(sd, sd->sc.option | OPTION_MADOGEAR, type);
  9282. } else if (pc_ismadogear(sd)) {
  9283. pc_setoption(sd, sd->sc.option & ~OPTION_MADOGEAR);
  9284. }
  9285. }
  9286. /*==========================================
  9287. * Check if player can drop an item
  9288. *------------------------------------------*/
  9289. bool pc_candrop(struct map_session_data *sd, struct item *item)
  9290. {
  9291. if( item && ((item->expire_time || (item->bound && !pc_can_give_bounded_items(sd))) || (itemdb_ishatched_egg(item))) )
  9292. return false;
  9293. if( !pc_can_give_items(sd) || sd->sc.cant.drop) //check if this GM level can drop items
  9294. return false;
  9295. return (itemdb_isdropable(item, pc_get_group_level(sd)));
  9296. }
  9297. /*==========================================
  9298. * Read '@type' variables (temporary numeric char reg)
  9299. *------------------------------------------*/
  9300. int64 pc_readreg(struct map_session_data* sd, int64 reg)
  9301. {
  9302. return i64db_i64get(sd->regs.vars, reg);
  9303. }
  9304. /*==========================================
  9305. * Set '@type' variables (temporary numeric char reg)
  9306. *------------------------------------------*/
  9307. bool pc_setreg(struct map_session_data* sd, int64 reg, int64 val)
  9308. {
  9309. uint32 index = script_getvaridx(reg);
  9310. nullpo_retr(false, sd);
  9311. if( val ) {
  9312. i64db_i64put(sd->regs.vars, reg, val);
  9313. if( index )
  9314. script_array_update(&sd->regs, reg, false);
  9315. } else {
  9316. i64db_remove(sd->regs.vars, reg);
  9317. if( index )
  9318. script_array_update(&sd->regs, reg, true);
  9319. }
  9320. return true;
  9321. }
  9322. /*==========================================
  9323. * Read '@type$' variables (temporary string char reg)
  9324. *------------------------------------------*/
  9325. char* pc_readregstr(struct map_session_data* sd, int64 reg)
  9326. {
  9327. struct script_reg_str *p = NULL;
  9328. p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg);
  9329. return p ? p->value : NULL;
  9330. }
  9331. /*==========================================
  9332. * Set '@type$' variables (temporary string char reg)
  9333. *------------------------------------------*/
  9334. bool pc_setregstr(struct map_session_data* sd, int64 reg, const char* str)
  9335. {
  9336. struct script_reg_str *p = NULL;
  9337. unsigned int index = script_getvaridx(reg);
  9338. DBData prev;
  9339. nullpo_retr(false, sd);
  9340. if( str[0] ) {
  9341. p = ers_alloc(str_reg_ers, struct script_reg_str);
  9342. p->value = aStrdup(str);
  9343. p->flag.type = 1;
  9344. if (sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev)) {
  9345. p = (struct script_reg_str *)db_data2ptr(&prev);
  9346. if( p->value )
  9347. aFree(p->value);
  9348. ers_free(str_reg_ers, p);
  9349. } else {
  9350. if( index )
  9351. script_array_update(&sd->regs, reg, false);
  9352. }
  9353. } else {
  9354. if (sd->regs.vars->remove(sd->regs.vars, db_i642key(reg), &prev)) {
  9355. p = (struct script_reg_str *)db_data2ptr(&prev);
  9356. if( p->value )
  9357. aFree(p->value);
  9358. ers_free(str_reg_ers, p);
  9359. if( index )
  9360. script_array_update(&sd->regs, reg, true);
  9361. }
  9362. }
  9363. return true;
  9364. }
  9365. /**
  9366. * Serves the following variable types:
  9367. * - 'type' (permanent numeric char reg)
  9368. * - '#type' (permanent numeric account reg)
  9369. * - '##type' (permanent numeric account reg2)
  9370. **/
  9371. int64 pc_readregistry(struct map_session_data *sd, int64 reg)
  9372. {
  9373. struct script_reg_num *p = NULL;
  9374. if (!sd->vars_ok) {
  9375. ShowError("pc_readregistry: Trying to read reg %s before it's been loaded!\n", get_str(script_getvarid(reg)));
  9376. //This really shouldn't happen, so it's possible the data was lost somewhere, we should request it again.
  9377. //intif->request_registry(sd,type==3?4:type);
  9378. set_eof(sd->fd);
  9379. return 0;
  9380. }
  9381. p = (struct script_reg_num *)i64db_get(sd->regs.vars, reg);
  9382. return p ? p->value : 0;
  9383. }
  9384. /**
  9385. * Serves the following variable types:
  9386. * - 'type$' (permanent str char reg)
  9387. * - '#type$' (permanent str account reg)
  9388. * - '##type$' (permanent str account reg2)
  9389. **/
  9390. char* pc_readregistry_str(struct map_session_data *sd, int64 reg)
  9391. {
  9392. struct script_reg_str *p = NULL;
  9393. if (!sd->vars_ok) {
  9394. ShowError("pc_readregistry_str: Trying to read reg %s before it's been loaded!\n", get_str(script_getvarid(reg)));
  9395. //This really shouldn't happen, so it's possible the data was lost somewhere, we should request it again.
  9396. //intif->request_registry(sd,type==3?4:type);
  9397. set_eof(sd->fd);
  9398. return NULL;
  9399. }
  9400. p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg);
  9401. return p ? p->value : NULL;
  9402. }
  9403. /**
  9404. * Serves the following variable types:
  9405. * - 'type' (permanent numeric char reg)
  9406. * - '#type' (permanent numeric account reg)
  9407. * - '##type' (permanent numeric account reg2)
  9408. **/
  9409. bool pc_setregistry(struct map_session_data *sd, int64 reg, int64 val)
  9410. {
  9411. struct script_reg_num *p = NULL;
  9412. const char *regname = get_str(script_getvarid(reg));
  9413. uint32 index = script_getvaridx(reg);
  9414. if ( !reg_load && !sd->vars_ok ) {
  9415. ShowError("pc_setregistry : refusing to set %s until vars are received.\n", regname);
  9416. return false;
  9417. }
  9418. if ((p = (struct script_reg_num *)i64db_get(sd->regs.vars, reg))) {
  9419. if( val ) {
  9420. if( !p->value && index ) /* its a entry that was deleted, so we reset array */
  9421. script_array_update(&sd->regs, reg, false);
  9422. p->value = val;
  9423. } else {
  9424. p->value = 0;
  9425. if( index )
  9426. script_array_update(&sd->regs, reg, true);
  9427. }
  9428. if (!reg_load)
  9429. p->flag.update = 1;/* either way, it will require either delete or replace */
  9430. } else if( val ) {
  9431. DBData prev;
  9432. if( index )
  9433. script_array_update(&sd->regs, reg, false);
  9434. p = ers_alloc(num_reg_ers, struct script_reg_num);
  9435. p->value = val;
  9436. if (!reg_load)
  9437. p->flag.update = 1;
  9438. if (sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev)) {
  9439. p = (struct script_reg_num *)db_data2ptr(&prev);
  9440. ers_free(num_reg_ers, p);
  9441. }
  9442. }
  9443. if (!reg_load && p)
  9444. sd->vars_dirty = true;
  9445. return true;
  9446. }
  9447. /**
  9448. * Serves the following variable types:
  9449. * - 'type$' (permanent str char reg)
  9450. * - '#type$' (permanent str account reg)
  9451. * - '##type$' (permanent str account reg2)
  9452. **/
  9453. bool pc_setregistry_str(struct map_session_data *sd, int64 reg, const char *val)
  9454. {
  9455. struct script_reg_str *p = NULL;
  9456. const char *regname = get_str(script_getvarid(reg));
  9457. unsigned int index = script_getvaridx(reg);
  9458. size_t vlen = 0;
  9459. if (!reg_load && !sd->vars_ok) {
  9460. ShowError("pc_setregistry_str : refusing to set %s until vars are received.\n", regname);
  9461. return false;
  9462. }
  9463. if ( !script_check_RegistryVariableLength(1, val, &vlen ) )
  9464. {
  9465. 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);
  9466. return false;
  9467. }
  9468. if( (p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg) ) ) {
  9469. if( val[0] ) {
  9470. if( p->value )
  9471. aFree(p->value);
  9472. else if ( index ) // an entry that was deleted, so we reset
  9473. script_array_update(&sd->regs, reg, false);
  9474. p->value = aStrdup(val);
  9475. } else {
  9476. if (p->value)
  9477. aFree(p->value);
  9478. p->value = NULL;
  9479. if( index )
  9480. script_array_update(&sd->regs, reg, true);
  9481. }
  9482. if( !reg_load )
  9483. p->flag.update = 1; // either way, it will require either delete or replace
  9484. } else if( val[0] ) {
  9485. DBData prev;
  9486. if( index )
  9487. script_array_update(&sd->regs, reg, false);
  9488. p = ers_alloc(str_reg_ers, struct script_reg_str);
  9489. p->value = aStrdup(val);
  9490. if( !reg_load )
  9491. p->flag.update = 1;
  9492. p->flag.type = 1;
  9493. if( sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev) ) {
  9494. p = (struct script_reg_str *)db_data2ptr(&prev);
  9495. if( p->value )
  9496. aFree(p->value);
  9497. ers_free(str_reg_ers, p);
  9498. }
  9499. }
  9500. if( !reg_load && p )
  9501. sd->vars_dirty = true;
  9502. return true;
  9503. }
  9504. /**
  9505. * Set value of player variable
  9506. * @param sd Player
  9507. * @param reg Variable name
  9508. * @param value
  9509. * @return True if success, false if failed.
  9510. **/
  9511. bool pc_setreg2(struct map_session_data *sd, const char *reg, int64 val) {
  9512. char prefix = reg[0];
  9513. nullpo_retr(false, sd);
  9514. if (reg[strlen(reg)-1] == '$') {
  9515. ShowError("pc_setreg2: Invalid variable scope '%s'\n", reg);
  9516. return false;
  9517. }
  9518. val = cap_value(val, INT_MIN, INT_MAX);
  9519. switch (prefix) {
  9520. case '.':
  9521. case '\'':
  9522. case '$':
  9523. ShowError("pc_setreg2: Invalid variable scope '%s'\n", reg);
  9524. return false;
  9525. case '@':
  9526. return pc_setreg(sd, add_str(reg), val);
  9527. case '#':
  9528. return (reg[1] == '#') ? pc_setaccountreg2(sd, add_str(reg), val) : pc_setaccountreg(sd, add_str(reg), val);
  9529. default:
  9530. return pc_setglobalreg(sd, add_str(reg), val);
  9531. }
  9532. return false;
  9533. }
  9534. /**
  9535. * Get value of player variable
  9536. * @param sd Player
  9537. * @param reg Variable name
  9538. * @return Variable value or 0 if failed.
  9539. **/
  9540. int64 pc_readreg2(struct map_session_data *sd, const char *reg) {
  9541. char prefix = reg[0];
  9542. nullpo_ret(sd);
  9543. if (reg[strlen(reg)-1] == '$') {
  9544. ShowError("pc_readreg2: Invalid variable scope '%s'\n", reg);
  9545. return 0;
  9546. }
  9547. switch (prefix) {
  9548. case '.':
  9549. case '\'':
  9550. case '$':
  9551. ShowError("pc_readreg2: Invalid variable scope '%s'\n", reg);
  9552. return 0;
  9553. case '@':
  9554. return pc_readreg(sd, add_str(reg));
  9555. case '#':
  9556. return (reg[1] == '#') ? pc_readaccountreg2(sd, add_str(reg)) : pc_readaccountreg(sd, add_str(reg));
  9557. default:
  9558. return pc_readglobalreg(sd, add_str(reg));
  9559. }
  9560. return 0;
  9561. }
  9562. /*==========================================
  9563. * Exec eventtimer for player sd (retrieved from map_session (id))
  9564. *------------------------------------------*/
  9565. static TIMER_FUNC(pc_eventtimer){
  9566. struct map_session_data *sd=map_id2sd(id);
  9567. char *p = (char *)data;
  9568. int i;
  9569. if(sd==NULL)
  9570. return 0;
  9571. ARR_FIND( 0, MAX_EVENTTIMER, i, sd->eventtimer[i] == tid );
  9572. if( i < MAX_EVENTTIMER )
  9573. {
  9574. sd->eventtimer[i] = INVALID_TIMER;
  9575. sd->eventcount--;
  9576. npc_event(sd,p,0);
  9577. }
  9578. else
  9579. ShowError("pc_eventtimer: no such event timer\n");
  9580. if (p) aFree(p);
  9581. return 0;
  9582. }
  9583. /*==========================================
  9584. * Add eventtimer for player sd ?
  9585. *------------------------------------------*/
  9586. bool pc_addeventtimer(struct map_session_data *sd,int tick,const char *name)
  9587. {
  9588. int i;
  9589. nullpo_retr(false,sd);
  9590. ARR_FIND( 0, MAX_EVENTTIMER, i, sd->eventtimer[i] == INVALID_TIMER );
  9591. if( i == MAX_EVENTTIMER )
  9592. return false;
  9593. sd->eventtimer[i] = add_timer(gettick()+tick, pc_eventtimer, sd->bl.id, (intptr_t)aStrdup(name));
  9594. sd->eventcount++;
  9595. return true;
  9596. }
  9597. /*==========================================
  9598. * Del eventtimer for player sd ?
  9599. *------------------------------------------*/
  9600. bool pc_deleventtimer(struct map_session_data *sd,const char *name)
  9601. {
  9602. char* p = NULL;
  9603. int i;
  9604. nullpo_retr(false,sd);
  9605. if (sd->eventcount == 0)
  9606. return false;
  9607. // find the named event timer
  9608. ARR_FIND( 0, MAX_EVENTTIMER, i,
  9609. sd->eventtimer[i] != INVALID_TIMER &&
  9610. (p = (char *)(get_timer(sd->eventtimer[i])->data)) != NULL &&
  9611. strcmp(p, name) == 0
  9612. );
  9613. if( i == MAX_EVENTTIMER )
  9614. return false; // not found
  9615. delete_timer(sd->eventtimer[i],pc_eventtimer);
  9616. sd->eventtimer[i] = INVALID_TIMER;
  9617. if(sd->eventcount > 0)
  9618. sd->eventcount--;
  9619. aFree(p);
  9620. return true;
  9621. }
  9622. /*==========================================
  9623. * Update eventtimer count for player sd
  9624. *------------------------------------------*/
  9625. void pc_addeventtimercount(struct map_session_data *sd,const char *name,int tick)
  9626. {
  9627. int i;
  9628. nullpo_retv(sd);
  9629. for(i=0;i<MAX_EVENTTIMER;i++)
  9630. if( sd->eventtimer[i] != INVALID_TIMER && strcmp(
  9631. (char *)(get_timer(sd->eventtimer[i])->data), name)==0 ){
  9632. addt_tickimer(sd->eventtimer[i],tick);
  9633. break;
  9634. }
  9635. }
  9636. /*==========================================
  9637. * Remove all eventtimer for player sd
  9638. *------------------------------------------*/
  9639. void pc_cleareventtimer(struct map_session_data *sd)
  9640. {
  9641. int i;
  9642. nullpo_retv(sd);
  9643. if (sd->eventcount == 0)
  9644. return;
  9645. for(i=0;i<MAX_EVENTTIMER;i++){
  9646. if( sd->eventtimer[i] != INVALID_TIMER ){
  9647. char *p = (char *)(get_timer(sd->eventtimer[i])->data);
  9648. delete_timer(sd->eventtimer[i],pc_eventtimer);
  9649. sd->eventtimer[i] = INVALID_TIMER;
  9650. if(sd->eventcount > 0) //avoid looping to max val
  9651. sd->eventcount--;
  9652. if (p) aFree(p);
  9653. }
  9654. }
  9655. }
  9656. /**
  9657. * Called when an item with combo is worn
  9658. * @param sd: Player data
  9659. * @param data: Item data
  9660. * @return Number of succeeded combo(s)
  9661. */
  9662. static int pc_checkcombo(struct map_session_data *sd, item_data *data) {
  9663. int success = 0;
  9664. for (const auto &item_combo : data->combos) {
  9665. bool do_continue = false;
  9666. // Ensure this isn't a duplicate combo
  9667. for (const auto player_combo : sd->combos) {
  9668. if (player_combo->id == item_combo->id) {
  9669. do_continue = true;
  9670. break;
  9671. }
  9672. }
  9673. // Combo already equipped
  9674. if (do_continue)
  9675. continue;
  9676. size_t nb_itemCombo = item_combo->nameid.size();
  9677. if (nb_itemCombo < 2) // A combo with less then 2 item?
  9678. continue;
  9679. struct s_itemchk {
  9680. int idx;
  9681. t_itemid nameid, card[MAX_SLOTS];
  9682. s_itemchk() : idx(0), nameid(0), card() {};
  9683. };
  9684. std::vector<s_itemchk> combo_idx(nb_itemCombo);
  9685. size_t j;
  9686. unsigned int pos = 0;
  9687. for (j = 0; j < nb_itemCombo; j++) {
  9688. t_itemid id = item_combo->nameid[j];
  9689. bool found = false;
  9690. for (int16 k = 0; k < EQI_MAX; k++) {
  9691. short index = sd->equip_index[k];
  9692. if (index < 0)
  9693. continue;
  9694. if (pc_is_same_equip_index((equip_index)k, sd->equip_index, index))
  9695. continue;
  9696. if (!sd->inventory_data[index])
  9697. continue;
  9698. if (itemdb_type(id) != IT_CARD) {
  9699. if (sd->inventory_data[index]->nameid != id)
  9700. continue;
  9701. if (j > 0) { // Check if this item not already used
  9702. do_continue = false;
  9703. for (size_t z = 0; z < nb_itemCombo - 1; z++) {
  9704. if (combo_idx[z].idx == index && combo_idx[z].nameid == id) { // Index already recorded
  9705. do_continue = true;
  9706. break;
  9707. }
  9708. }
  9709. if (do_continue)
  9710. continue;
  9711. }
  9712. combo_idx[j].nameid = id;
  9713. combo_idx[j].idx = index;
  9714. pos |= sd->inventory.u.items_inventory[index].equip;
  9715. found = true;
  9716. break;
  9717. } else { // Cards and enchants
  9718. if (itemdb_isspecial(sd->inventory.u.items_inventory[index].card[0]))
  9719. continue;
  9720. for (uint8 z = 0; z < MAX_SLOTS; z++) {
  9721. do_continue = false;
  9722. if (sd->inventory.u.items_inventory[index].card[z] != id)
  9723. continue;
  9724. if (j > 0) {
  9725. for (size_t c1 = 0; c1 < nb_itemCombo - 1; c1++) {
  9726. if (combo_idx[c1].idx == index && combo_idx[c1].nameid == id) {
  9727. for (uint8 c2 = 0; c2 < MAX_SLOTS; c2++) {
  9728. if (combo_idx[c1].card[c2] == id) { // Card already recorded (at this same idx)
  9729. do_continue = true;
  9730. break;
  9731. }
  9732. }
  9733. }
  9734. }
  9735. }
  9736. if (do_continue)
  9737. continue;
  9738. combo_idx[j].nameid = id;
  9739. combo_idx[j].idx = index;
  9740. combo_idx[j].card[z] = id;
  9741. pos |= sd->inventory.u.items_inventory[index].equip;
  9742. found = true;
  9743. break;
  9744. }
  9745. }
  9746. }
  9747. if (!found)
  9748. break; // Unable to found all the IDs for this combo, return
  9749. }
  9750. // Broke out of the count loop without finding all IDs, move to the next combo
  9751. if (j < nb_itemCombo)
  9752. continue;
  9753. // All items in the combo are matching
  9754. auto entry = std::make_shared<s_combos>();
  9755. entry->bonus = item_combo->script;
  9756. entry->id = item_combo->id;
  9757. entry->pos = pos;
  9758. sd->combos.push_back(entry);
  9759. combo_idx.clear();
  9760. success++;
  9761. }
  9762. return success;
  9763. }
  9764. /**
  9765. * Called when an item with combo is removed
  9766. * @param sd: Player data
  9767. * @param data: Item data
  9768. * @return Number of removed combo(s)
  9769. */
  9770. static int pc_removecombo(struct map_session_data *sd, item_data *data ) {
  9771. if (sd->combos.empty())
  9772. return 0; // Nothing to do here, player has no combos
  9773. int retval = 0;
  9774. for (const auto &item_combo : data->combos) {
  9775. std::shared_ptr<s_combos> del_combo = nullptr;
  9776. // Check if this combo exists on this player
  9777. for (const auto &player_combo : sd->combos) {
  9778. if (player_combo->id == item_combo->id) {
  9779. del_combo = player_combo;
  9780. break;
  9781. }
  9782. }
  9783. // No match, skip this combo
  9784. if (del_combo == nullptr)
  9785. continue;
  9786. util::vector_erase_if_exists(sd->combos, del_combo);
  9787. retval++;
  9788. // Check if combo requirements still fit
  9789. if (pc_checkcombo(sd, data))
  9790. continue;
  9791. // It's empty, clear all the memory
  9792. if (sd->combos.empty()) {
  9793. sd->combos.clear();
  9794. return retval; // Return at this point as there are no more combos to check
  9795. }
  9796. }
  9797. return retval;
  9798. }
  9799. /**
  9800. * Load combo data(s) of player
  9801. * @param sd: Player data
  9802. * @return ret numbers of succeed combo
  9803. */
  9804. int pc_load_combo(struct map_session_data *sd) {
  9805. int ret = 0;
  9806. for (int16 i = 0; i < EQI_MAX; i++) {
  9807. item_data *id;
  9808. short idx = sd->equip_index[i];
  9809. if (idx < 0 || !(id = sd->inventory_data[idx]))
  9810. continue;
  9811. if (!id->combos.empty())
  9812. ret += pc_checkcombo(sd, id);
  9813. if (!itemdb_isspecial(sd->inventory.u.items_inventory[idx].card[0])) {
  9814. item_data *data;
  9815. for (uint8 j = 0; j < MAX_SLOTS; j++) {
  9816. if (!sd->inventory.u.items_inventory[idx].card[j])
  9817. continue;
  9818. if ((data = itemdb_exists(sd->inventory.u.items_inventory[idx].card[j])) != nullptr) {
  9819. if (!data->combos.empty())
  9820. ret += pc_checkcombo(sd, data);
  9821. }
  9822. }
  9823. }
  9824. }
  9825. return ret;
  9826. }
  9827. /*==========================================
  9828. * Equip item on player sd at req_pos from inventory index n
  9829. * return: false - fail; true - success
  9830. *------------------------------------------*/
  9831. bool pc_equipitem(struct map_session_data *sd,short n,int req_pos,bool equipswitch)
  9832. {
  9833. int i, pos, flag = 0, iflag;
  9834. struct item_data *id;
  9835. uint8 res = ITEM_EQUIP_ACK_OK;
  9836. short* equip_index;
  9837. nullpo_retr(false,sd);
  9838. if( n < 0 || n >= MAX_INVENTORY ) {
  9839. if( equipswitch ){
  9840. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  9841. }else{
  9842. clif_equipitemack(sd,0,0,ITEM_EQUIP_ACK_FAIL);
  9843. }
  9844. return false;
  9845. }
  9846. if( DIFF_TICK(sd->canequip_tick,gettick()) > 0 ) {
  9847. if( equipswitch ){
  9848. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  9849. }else{
  9850. clif_equipitemack(sd,n,0,ITEM_EQUIP_ACK_FAIL);
  9851. }
  9852. return false;
  9853. }
  9854. if (!(id = sd->inventory_data[n]))
  9855. return false;
  9856. pos = pc_equippoint(sd,n); //With a few exceptions, item should go in all specified slots.
  9857. if(battle_config.battle_log && !equipswitch)
  9858. ShowInfo("equip %u (%d) %x:%x\n",sd->inventory.u.items_inventory[n].nameid,n,id?id->equip:0,req_pos);
  9859. if((res = pc_isequip(sd,n))) {
  9860. if( equipswitch ){
  9861. clif_equipswitch_add( sd, n, req_pos, res );
  9862. }else{
  9863. clif_equipitemack(sd,n,0,res); // fail
  9864. }
  9865. return false;
  9866. }
  9867. if( equipswitch && id->type == IT_AMMO ){
  9868. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  9869. return false;
  9870. }
  9871. if (!(pos&req_pos) || sd->inventory.u.items_inventory[n].equip != 0 || sd->inventory.u.items_inventory[n].attribute==1 ) { // [Valaris]
  9872. if( equipswitch ){
  9873. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  9874. }else{
  9875. clif_equipitemack(sd,n,0,ITEM_EQUIP_ACK_FAIL); // fail
  9876. }
  9877. return false;
  9878. }
  9879. if( sd->sc.count && (sd->sc.cant.equip || (sd->sc.data[SC_PYROCLASTIC] && sd->inventory_data[n]->type == IT_WEAPON)) ) {
  9880. if( equipswitch ){
  9881. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  9882. }else{
  9883. clif_equipitemack(sd,n,0,ITEM_EQUIP_ACK_FAIL); //Fail
  9884. }
  9885. return false;
  9886. }
  9887. equip_index = equipswitch ? sd->equip_switch_index : sd->equip_index;
  9888. if ( !equipswitch && id->flag.bindOnEquip && !sd->inventory.u.items_inventory[n].bound) {
  9889. sd->inventory.u.items_inventory[n].bound = (char)battle_config.default_bind_on_equip;
  9890. clif_notify_bindOnEquip(sd,n);
  9891. }
  9892. if(pos == EQP_ACC) { //Accessories should only go in one of the two.
  9893. pos = req_pos&EQP_ACC;
  9894. if (pos == EQP_ACC) //User specified both slots.
  9895. pos = (equip_index[EQI_ACC_R] >= 0 && equip_index[EQI_ACC_L] < 0) ? EQP_ACC_L : EQP_ACC_R;
  9896. for (i = 0; i < sd->inventory_data[n]->slots; i++) { // Accessories that have cards that force equip location
  9897. if (!sd->inventory.u.items_inventory[n].card[i])
  9898. continue;
  9899. struct item_data *card_data = itemdb_exists(sd->inventory.u.items_inventory[n].card[i]);
  9900. if (card_data) {
  9901. int card_pos = card_data->equip;
  9902. if (card_pos == EQP_ACC_L || card_pos == EQP_ACC_R) {
  9903. pos = card_pos; // Use the card's equip position
  9904. break;
  9905. }
  9906. }
  9907. }
  9908. } else if(pos == EQP_ARMS && id->equip == EQP_HAND_R) { //Dual wield capable weapon.
  9909. pos = (req_pos&EQP_ARMS);
  9910. if (pos == EQP_ARMS) //User specified both slots, pick one for them.
  9911. #ifdef RENEWAL
  9912. pos = (equip_index[EQI_HAND_R] >= 0 && equip_index[EQI_HAND_L] < 0) ? EQP_HAND_L : EQP_HAND_R;
  9913. #else
  9914. pos = equip_index[EQI_HAND_R] >= 0 ? EQP_HAND_L : EQP_HAND_R;
  9915. #endif
  9916. } else if(pos == EQP_SHADOW_ACC) { // Shadow System
  9917. pos = req_pos&EQP_SHADOW_ACC;
  9918. if (pos == EQP_SHADOW_ACC)
  9919. pos = (equip_index[EQI_SHADOW_ACC_L] >= 0 && equip_index[EQI_SHADOW_ACC_R] < 0) ? EQP_SHADOW_ACC_R : EQP_SHADOW_ACC_L;
  9920. } else if(pos == EQP_SHADOW_ARMS && id->equip == EQP_SHADOW_WEAPON) {
  9921. pos = (req_pos&EQP_SHADOW_ARMS);
  9922. if( pos == EQP_SHADOW_ARMS )
  9923. pos = (equip_index[EQI_SHADOW_WEAPON] >= 0 ? EQP_SHADOW_SHIELD : EQP_SHADOW_WEAPON);
  9924. }
  9925. if (pos&EQP_HAND_R && battle_config.use_weapon_skill_range&BL_PC) {
  9926. //Update skill-block range database when weapon range changes. [Skotlex]
  9927. i = equip_index[EQI_HAND_R];
  9928. if (i < 0 || !sd->inventory_data[i]) //No data, or no weapon equipped
  9929. flag = 1;
  9930. else
  9931. flag = id->range != sd->inventory_data[i]->range;
  9932. }
  9933. if( equipswitch ){
  9934. for( i = 0; i < EQI_MAX; i++ ){
  9935. if( pos&equip_bitmask[i] ){
  9936. // If there was already an item assigned to this slot
  9937. if( sd->equip_switch_index[i] >= 0 ){
  9938. pc_equipswitch_remove( sd, sd->equip_switch_index[i] );
  9939. }
  9940. // Assign the new index to it
  9941. sd->equip_switch_index[i] = n;
  9942. }
  9943. }
  9944. sd->inventory.u.items_inventory[n].equipSwitch = pos;
  9945. clif_equipswitch_add( sd, n, pos, ITEM_EQUIP_ACK_OK );
  9946. return true;
  9947. }else{
  9948. for(i=0;i<EQI_MAX;i++) {
  9949. if(pos & equip_bitmask[i]) {
  9950. if(sd->equip_index[i] >= 0) //Slot taken, remove item from there.
  9951. pc_unequipitem(sd,sd->equip_index[i], 1 | 2 | 4);
  9952. sd->equip_index[i] = n;
  9953. }
  9954. }
  9955. pc_equipswitch_remove(sd, n);
  9956. if(pos==EQP_AMMO) {
  9957. clif_arrowequip(sd,n);
  9958. clif_arrow_fail(sd,3);
  9959. }
  9960. else
  9961. clif_equipitemack(sd,n,pos,ITEM_EQUIP_ACK_OK);
  9962. sd->inventory.u.items_inventory[n].equip = pos;
  9963. }
  9964. if(pos & EQP_HAND_R) {
  9965. if(id)
  9966. sd->weapontype1 = id->subtype;
  9967. else
  9968. sd->weapontype1 = W_FIST;
  9969. pc_calcweapontype(sd);
  9970. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  9971. }
  9972. if(pos & EQP_HAND_L) {
  9973. if(id) {
  9974. if(id->type == IT_WEAPON) {
  9975. sd->status.shield = W_FIST;
  9976. sd->weapontype2 = id->subtype;
  9977. }
  9978. else
  9979. if(id->type == IT_ARMOR) {
  9980. sd->status.shield = id->look;
  9981. sd->weapontype2 = W_FIST;
  9982. }
  9983. }
  9984. else
  9985. sd->status.shield = sd->weapontype2 = W_FIST;
  9986. pc_calcweapontype(sd);
  9987. clif_changelook(&sd->bl,LOOK_SHIELD,sd->status.shield);
  9988. }
  9989. if(pos & EQP_SHOES)
  9990. clif_changelook(&sd->bl,LOOK_SHOES,0);
  9991. if (battle_config.ammo_unequip && (pos&EQP_ARMS) && id->type == IT_WEAPON) {
  9992. short idx = sd->equip_index[EQI_AMMO];
  9993. if (idx >= 0) {
  9994. switch (sd->inventory_data[idx]->subtype) {
  9995. case AMMO_ARROW:
  9996. if (id->subtype != W_BOW && id->subtype != W_MUSICAL && id->subtype != W_WHIP)
  9997. pc_unequipitem(sd, idx, 2 | 4);
  9998. break;
  9999. case AMMO_BULLET:
  10000. case AMMO_SHELL:
  10001. if (id->subtype != W_REVOLVER && id->subtype != W_RIFLE && id->subtype != W_GATLING && id->subtype != W_SHOTGUN
  10002. #ifdef RENEWAL
  10003. && id->subtype != W_GRENADE
  10004. #endif
  10005. )
  10006. pc_unequipitem(sd, idx, 2 | 4);
  10007. break;
  10008. #ifndef RENEWAL
  10009. case AMMO_GRENADE:
  10010. if (id->subtype != W_GRENADE)
  10011. pc_unequipitem(sd, idx, 2 | 4);
  10012. break;
  10013. #endif
  10014. }
  10015. }
  10016. }
  10017. pc_set_costume_view(sd);
  10018. pc_checkallowskill(sd); //Check if status changes should be halted.
  10019. iflag = sd->npc_item_flag;
  10020. // Check for combos (MUST be before status_calc_pc)
  10021. if (!id->combos.empty())
  10022. pc_checkcombo(sd, id);
  10023. if (itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  10024. ; // No cards
  10025. else {
  10026. for (i = 0; i < MAX_SLOTS; i++) {
  10027. item_data *data;
  10028. if (!sd->inventory.u.items_inventory[n].card[i])
  10029. continue;
  10030. if ((data = itemdb_exists(sd->inventory.u.items_inventory[n].card[i])) != nullptr) {
  10031. if (!data->combos.empty())
  10032. pc_checkcombo(sd, data);
  10033. }
  10034. }
  10035. }
  10036. status_calc_pc(sd,SCO_NONE);
  10037. if (flag) //Update skill data
  10038. clif_skillinfoblock(sd);
  10039. //OnEquip script [Skotlex]
  10040. if (id) {
  10041. //only run the script if item isn't restricted
  10042. if (id->equip_script && (pc_has_permission(sd,PC_PERM_USE_ALL_EQUIPMENT) || !itemdb_isNoEquip(id,sd->bl.m)))
  10043. run_script(id->equip_script,0,sd->bl.id,fake_nd->bl.id);
  10044. if(itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  10045. ; //No cards
  10046. else {
  10047. for( i = 0; i < MAX_SLOTS; i++ ) {
  10048. struct item_data *data;
  10049. if (!sd->inventory.u.items_inventory[n].card[i])
  10050. continue;
  10051. if ( ( data = itemdb_exists(sd->inventory.u.items_inventory[n].card[i]) ) != NULL ) {
  10052. if (data->equip_script && (pc_has_permission(sd,PC_PERM_USE_ALL_EQUIPMENT) || !itemdb_isNoEquip(data,sd->bl.m)))
  10053. run_script(data->equip_script,0,sd->bl.id,fake_nd->bl.id);
  10054. }
  10055. }
  10056. }
  10057. }
  10058. sd->npc_item_flag = iflag;
  10059. return true;
  10060. }
  10061. static void pc_deleteautobonus( std::vector<std::shared_ptr<s_autobonus>>& bonus, int position ){
  10062. std::vector<std::shared_ptr<s_autobonus>>::iterator it = bonus.begin();
  10063. while( it != bonus.end() ){
  10064. std::shared_ptr<s_autobonus> b = *it;
  10065. if( ( b->pos & position ) != b->pos ){
  10066. it++;
  10067. continue;
  10068. }
  10069. it = bonus.erase( it );
  10070. }
  10071. }
  10072. /**
  10073. * Recalculate player status on unequip
  10074. * @param sd: Player data
  10075. * @param n: Item inventory index
  10076. * @param flag: Whether to recalculate a player's status or not
  10077. * @return True on success or false on failure
  10078. */
  10079. static void pc_unequipitem_sub(struct map_session_data *sd, int n, int flag) {
  10080. int i, iflag;
  10081. bool status_calc = false;
  10082. pc_deleteautobonus( sd->autobonus, sd->inventory.u.items_inventory[n].equip );
  10083. pc_deleteautobonus( sd->autobonus2, sd->inventory.u.items_inventory[n].equip );
  10084. pc_deleteautobonus( sd->autobonus3, sd->inventory.u.items_inventory[n].equip );
  10085. sd->inventory.u.items_inventory[n].equip = 0;
  10086. if (!(flag & 4))
  10087. pc_checkallowskill(sd);
  10088. iflag = sd->npc_item_flag;
  10089. // Check for combos (MUST be before status_calc_pc)
  10090. if (sd->inventory_data[n]) {
  10091. if (!sd->inventory_data[n]->combos.empty()) {
  10092. if (pc_removecombo(sd, sd->inventory_data[n]))
  10093. status_calc = true;
  10094. }
  10095. if (itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  10096. ; // No cards
  10097. else {
  10098. for (i = 0; i < MAX_SLOTS; i++) {
  10099. item_data *data;
  10100. if (!sd->inventory.u.items_inventory[n].card[i])
  10101. continue;
  10102. if ((data = itemdb_exists(sd->inventory.u.items_inventory[n].card[i])) != nullptr) {
  10103. if (!data->combos.empty()) {
  10104. if (pc_removecombo(sd, data))
  10105. status_calc = true;
  10106. }
  10107. }
  10108. }
  10109. }
  10110. }
  10111. if (flag & 1 || status_calc) {
  10112. pc_checkallowskill(sd);
  10113. status_calc_pc(sd, SCO_FORCE);
  10114. }
  10115. if (sd->sc.data[SC_SIGNUMCRUCIS] && !battle_check_undead(sd->battle_status.race, sd->battle_status.def_ele))
  10116. status_change_end(&sd->bl, SC_SIGNUMCRUCIS, INVALID_TIMER);
  10117. //OnUnEquip script [Skotlex]
  10118. if (sd->inventory_data[n]) {
  10119. if (sd->inventory_data[n]->unequip_script)
  10120. run_script(sd->inventory_data[n]->unequip_script, 0, sd->bl.id, fake_nd->bl.id);
  10121. if (itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  10122. ; //No cards
  10123. else {
  10124. for (i = 0; i < MAX_SLOTS; i++) {
  10125. struct item_data *data;
  10126. if (!sd->inventory.u.items_inventory[n].card[i])
  10127. continue;
  10128. if ((data = itemdb_exists(sd->inventory.u.items_inventory[n].card[i])) != NULL) {
  10129. if (data->unequip_script)
  10130. run_script(data->unequip_script, 0, sd->bl.id, fake_nd->bl.id);
  10131. }
  10132. }
  10133. }
  10134. }
  10135. sd->npc_item_flag = iflag;
  10136. }
  10137. /**
  10138. * Called when attempting to unequip an item from a player
  10139. * @param sd: Player data
  10140. * @param n: Item inventory index
  10141. * @param flag: Type of unequip
  10142. * 0 - only unequip
  10143. * 1 - calculate status after unequipping
  10144. * 2 - force unequip
  10145. * 4 - unequip by switching equipment
  10146. * @return True on success or false on failure
  10147. */
  10148. bool pc_unequipitem(struct map_session_data *sd, int n, int flag) {
  10149. int i, pos;
  10150. nullpo_retr(false,sd);
  10151. if (n < 0 || n >= MAX_INVENTORY) {
  10152. clif_unequipitemack(sd,0,0,0);
  10153. return false;
  10154. }
  10155. if (!(pos = sd->inventory.u.items_inventory[n].equip)) {
  10156. clif_unequipitemack(sd,n,0,0);
  10157. return false; //Nothing to unequip
  10158. }
  10159. // status change that makes player cannot unequip equipment
  10160. if (!(flag&2) && sd->sc.count &&( sd->sc.cant.unequip ||
  10161. (sd->sc.data[SC_PYROCLASTIC] && sd->inventory_data[n]->type == IT_WEAPON))) // can't switch weapon
  10162. {
  10163. clif_unequipitemack(sd,n,0,0);
  10164. return false;
  10165. }
  10166. if (battle_config.battle_log)
  10167. ShowInfo("unequip %d %x:%x\n",n,pc_equippoint(sd,n),pos);
  10168. for(i = 0; i < EQI_MAX; i++) {
  10169. if (pos & equip_bitmask[i])
  10170. sd->equip_index[i] = -1;
  10171. }
  10172. if(pos & EQP_HAND_R) {
  10173. sd->weapontype1 = 0;
  10174. sd->status.weapon = sd->weapontype2;
  10175. pc_calcweapontype(sd);
  10176. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  10177. if( !battle_config.dancing_weaponswitch_fix )
  10178. status_change_end(&sd->bl, SC_DANCING, INVALID_TIMER); // Unequipping => stop dancing.
  10179. #ifdef RENEWAL
  10180. if (battle_config.switch_remove_edp&2) {
  10181. #else
  10182. if (battle_config.switch_remove_edp&1) {
  10183. #endif
  10184. status_change_end(&sd->bl, SC_EDP, INVALID_TIMER);
  10185. }
  10186. }
  10187. if(pos & EQP_HAND_L) {
  10188. if (sd->status.shield && battle_getcurrentskill(&sd->bl) == LG_SHIELDSPELL)
  10189. unit_skillcastcancel(&sd->bl, 0); // Cancel Shield Spell if player swaps shields.
  10190. sd->status.shield = sd->weapontype2 = W_FIST;
  10191. pc_calcweapontype(sd);
  10192. clif_changelook(&sd->bl,LOOK_SHIELD,sd->status.shield);
  10193. }
  10194. if(pos & EQP_SHOES)
  10195. clif_changelook(&sd->bl,LOOK_SHOES,0);
  10196. clif_unequipitemack(sd,n,pos,1);
  10197. pc_set_costume_view(sd);
  10198. status_db.removeByStatusFlag(&sd->bl, { SCF_REMOVEONUNEQUIP });
  10199. // On weapon change (right and left hand)
  10200. if ((pos & EQP_ARMS) && sd->inventory_data[n]->type == IT_WEAPON) {
  10201. if (battle_config.ammo_unequip && !(flag & 4)) {
  10202. switch (sd->inventory_data[n]->subtype) {
  10203. case W_BOW:
  10204. case W_MUSICAL:
  10205. case W_WHIP:
  10206. case W_REVOLVER:
  10207. case W_RIFLE:
  10208. case W_GATLING:
  10209. case W_SHOTGUN:
  10210. case W_GRENADE: {
  10211. short idx = sd->equip_index[EQI_AMMO];
  10212. if (idx >= 0) {
  10213. sd->equip_index[EQI_AMMO] = -1;
  10214. clif_unequipitemack(sd, idx, sd->inventory.u.items_inventory[idx].equip, 1);
  10215. pc_unequipitem_sub(sd, idx, 0);
  10216. }
  10217. }
  10218. break;
  10219. }
  10220. }
  10221. status_db.removeByStatusFlag(&sd->bl, { SCF_REMOVEONUNEQUIPWEAPON });
  10222. }
  10223. // On armor change
  10224. if (pos & EQP_ARMOR) {
  10225. status_db.removeByStatusFlag(&sd->bl, { SCF_REMOVEONUNEQUIPARMOR });
  10226. }
  10227. // On equipment change
  10228. #ifndef RENEWAL
  10229. if (!(flag&4))
  10230. status_change_end(&sd->bl, SC_CONCENTRATION, INVALID_TIMER);
  10231. #endif
  10232. // On ammo change
  10233. 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_))
  10234. status_change_end(&sd->bl, SC_P_ALTER, INVALID_TIMER);
  10235. pc_unequipitem_sub(sd, n, flag);
  10236. return true;
  10237. }
  10238. int pc_equipswitch( struct map_session_data* sd, int index ){
  10239. // Get the target equip mask
  10240. int position = sd->inventory.u.items_inventory[index].equipSwitch;
  10241. // Get the currently equipped item
  10242. short equippedItem = pc_checkequip( sd, position, true );
  10243. // No item equipped at the target
  10244. if( equippedItem == -1 ){
  10245. // Remove it from the equip switch
  10246. pc_equipswitch_remove( sd, index );
  10247. pc_equipitem( sd, index, position );
  10248. return position;
  10249. }else{
  10250. std::map<int, int> unequipped;
  10251. int unequipped_position = 0;
  10252. // Unequip all items that interfere
  10253. for( int i = 0; i < EQI_MAX; i++ ){
  10254. int unequip_index = sd->equip_index[i];
  10255. if( unequip_index >= 0 && position & equip_bitmask[i] ){
  10256. struct item* unequip_item = &sd->inventory.u.items_inventory[unequip_index];
  10257. // Store the unequipped index and position mask for later
  10258. unequipped[unequip_index] = unequip_item->equip;
  10259. // Keep the position for later
  10260. unequipped_position |= unequip_item->equip;
  10261. // Unequip the item
  10262. pc_unequipitem( sd, unequip_index, 0 );
  10263. }
  10264. }
  10265. int all_position = position | unequipped_position;
  10266. // Equip everything that is hit by the mask
  10267. for( int i = 0; i < EQI_MAX; i++ ){
  10268. int exchange_index = sd->equip_switch_index[i];
  10269. if( exchange_index >= 0 && all_position & equip_bitmask[i] ){
  10270. struct item* exchange_item = &sd->inventory.u.items_inventory[exchange_index];
  10271. // Store the target position
  10272. int exchange_position = exchange_item->equipSwitch;
  10273. // Remove the item from equip switch
  10274. pc_equipswitch_remove( sd, exchange_index );
  10275. // Equip the item at the destinated position
  10276. pc_equipitem( sd, exchange_index, exchange_position );
  10277. }
  10278. }
  10279. // Place all unequipped items into the equip switch window
  10280. for( std::pair<int, int> pair : unequipped ){
  10281. int unequipped_index = pair.first;
  10282. int unequipped_position = pair.second;
  10283. // Rebuild the index cache
  10284. for( int i = 0; i < EQI_MAX; i++ ){
  10285. if( unequipped_position & equip_bitmask[i] ){
  10286. sd->equip_switch_index[i] = unequipped_index;
  10287. }
  10288. }
  10289. // Set the correct position mask
  10290. sd->inventory.u.items_inventory[unequipped_index].equipSwitch = unequipped_position;
  10291. // Notify the client
  10292. clif_equipswitch_add( sd, unequipped_index, unequipped_position, ITEM_EQUIP_ACK_OK );
  10293. }
  10294. return all_position;
  10295. }
  10296. }
  10297. void pc_equipswitch_remove( struct map_session_data* sd, int index ){
  10298. struct item* item = &sd->inventory.u.items_inventory[index];
  10299. if( !item->equipSwitch ){
  10300. return;
  10301. }
  10302. for( int i = 0; i < EQI_MAX; i++ ){
  10303. // If a match is found
  10304. if( sd->equip_switch_index[i] == index ){
  10305. // Remove it from the slot
  10306. sd->equip_switch_index[i] = -1;
  10307. }
  10308. }
  10309. // Send out one packet for all slots using the current item's mask
  10310. clif_equipswitch_remove( sd, index, item->equipSwitch, false );
  10311. item->equipSwitch = 0;
  10312. }
  10313. /*==========================================
  10314. * Checking if player (sd) has an invalid item
  10315. * and is unequiped on map load (item_noequip)
  10316. *------------------------------------------*/
  10317. void pc_checkitem(struct map_session_data *sd) {
  10318. int i, calc_flag = 0;
  10319. struct item* it;
  10320. nullpo_retv(sd);
  10321. if( sd->state.vending ) //Avoid reorganizing items when we are vending, as that leads to exploits (pointed out by End of Exam)
  10322. return;
  10323. pc_check_available_item(sd, ITMCHK_NONE); // Check for invalid(ated) items.
  10324. for( i = 0; i < MAX_INVENTORY; i++ ) {
  10325. it = &sd->inventory.u.items_inventory[i];
  10326. if( it->nameid == 0 )
  10327. continue;
  10328. if( !it->equip )
  10329. continue;
  10330. if( it->equip&~pc_equippoint(sd,i) ) {
  10331. pc_unequipitem(sd, i, 2);
  10332. calc_flag = 1;
  10333. continue;
  10334. }
  10335. 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) ) {
  10336. pc_unequipitem(sd, i, 2);
  10337. calc_flag = 1;
  10338. continue;
  10339. }
  10340. }
  10341. for( i = 0; i < MAX_INVENTORY; i++ ) {
  10342. it = &sd->inventory.u.items_inventory[i];
  10343. if( it->nameid == 0 )
  10344. continue;
  10345. if( !it->equipSwitch )
  10346. continue;
  10347. if( it->equipSwitch&~pc_equippoint(sd,i) ||
  10348. ( !pc_has_permission(sd, PC_PERM_USE_ALL_EQUIPMENT) && !battle_config.allow_equip_restricted_item && itemdb_isNoEquip(sd->inventory_data[i], sd->bl.m) ) ){
  10349. for( int j = 0; j < EQI_MAX; j++ ){
  10350. if( sd->equip_switch_index[j] == i ){
  10351. sd->equip_switch_index[j] = -1;
  10352. }
  10353. }
  10354. sd->inventory.u.items_inventory[i].equipSwitch = 0;
  10355. continue;
  10356. }
  10357. }
  10358. if( calc_flag && sd->state.active ) {
  10359. pc_checkallowskill(sd);
  10360. status_calc_pc(sd,SCO_NONE);
  10361. }
  10362. }
  10363. /*==========================================
  10364. * Checks for unavailable items and removes them.
  10365. * @param sd: Player data
  10366. * @param type Forced check:
  10367. * 1 - Inventory
  10368. * 2 - Cart
  10369. * 4 - Storage
  10370. *------------------------------------------*/
  10371. void pc_check_available_item(struct map_session_data *sd, uint8 type)
  10372. {
  10373. int i;
  10374. t_itemid nameid;
  10375. char output[256];
  10376. nullpo_retv(sd);
  10377. if (battle_config.item_check&ITMCHK_INVENTORY && type&ITMCHK_INVENTORY) { // Check for invalid(ated) items in inventory.
  10378. for(i = 0; i < MAX_INVENTORY; i++) {
  10379. nameid = sd->inventory.u.items_inventory[i].nameid;
  10380. if (!nameid)
  10381. continue;
  10382. if (!itemdb_available(nameid)) {
  10383. sprintf(output, msg_txt(sd, 709), nameid); // Item %u has been removed from your inventory.
  10384. clif_displaymessage(sd->fd, output);
  10385. ShowWarning("Removed invalid/disabled item (ID: %u, amount: %d) from inventory (char_id: %d).\n", nameid, sd->inventory.u.items_inventory[i].amount, sd->status.char_id);
  10386. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 4, 0, LOG_TYPE_OTHER);
  10387. continue;
  10388. }
  10389. if (!sd->inventory.u.items_inventory[i].unique_id && !itemdb_isstackable(nameid))
  10390. sd->inventory.u.items_inventory[i].unique_id = pc_generate_unique_id(sd);
  10391. }
  10392. }
  10393. if (battle_config.item_check&ITMCHK_CART && type&ITMCHK_CART) { // Check for invalid(ated) items in cart.
  10394. for(i = 0; i < MAX_CART; i++) {
  10395. nameid = sd->cart.u.items_cart[i].nameid;
  10396. if (!nameid)
  10397. continue;
  10398. if (!itemdb_available(nameid)) {
  10399. sprintf(output, msg_txt(sd, 710), nameid); // Item %u has been removed from your cart.
  10400. clif_displaymessage(sd->fd, output);
  10401. ShowWarning("Removed invalid/disabled item (ID: %u, amount: %d) from cart (char_id: %d).\n", nameid, sd->cart.u.items_cart[i].amount, sd->status.char_id);
  10402. pc_cart_delitem(sd, i, sd->cart.u.items_cart[i].amount, 0, LOG_TYPE_OTHER);
  10403. continue;
  10404. }
  10405. if (!sd->cart.u.items_cart[i].unique_id && !itemdb_isstackable(nameid))
  10406. sd->cart.u.items_cart[i].unique_id = pc_generate_unique_id(sd);
  10407. }
  10408. }
  10409. if (battle_config.item_check&ITMCHK_STORAGE && type&ITMCHK_STORAGE) { // Check for invalid(ated) items in storage.
  10410. for(i = 0; i < sd->storage.max_amount; i++) {
  10411. nameid = sd->storage.u.items_storage[i].nameid;
  10412. if (!nameid)
  10413. continue;
  10414. if (!itemdb_available(nameid)) {
  10415. sprintf(output, msg_txt(sd, 711), nameid); // Item %u has been removed from your storage.
  10416. clif_displaymessage(sd->fd, output);
  10417. ShowWarning("Removed invalid/disabled item (ID: %u, amount: %d) from storage (char_id: %d).\n", nameid, sd->storage.u.items_storage[i].amount, sd->status.char_id);
  10418. storage_delitem(sd, &sd->storage, i, sd->storage.u.items_storage[i].amount);
  10419. continue;
  10420. }
  10421. if (!sd->storage.u.items_storage[i].unique_id && !itemdb_isstackable(nameid))
  10422. sd->storage.u.items_storage[i].unique_id = pc_generate_unique_id(sd);
  10423. }
  10424. }
  10425. }
  10426. /*==========================================
  10427. * Update PVP rank for sd1 in cmp to sd2
  10428. *------------------------------------------*/
  10429. static int pc_calc_pvprank_sub(struct block_list *bl,va_list ap)
  10430. {
  10431. struct map_session_data *sd1,*sd2;
  10432. sd1=(struct map_session_data *)bl;
  10433. sd2=va_arg(ap,struct map_session_data *);
  10434. if( pc_isinvisible(sd1) || pc_isinvisible(sd2) )
  10435. {// cannot register pvp rank for hidden GMs
  10436. return 0;
  10437. }
  10438. if( sd1->pvp_point > sd2->pvp_point )
  10439. sd2->pvp_rank++;
  10440. return 0;
  10441. }
  10442. /*==========================================
  10443. * Calculate new rank beetween all present players (map_foreachinallarea)
  10444. * and display result
  10445. *------------------------------------------*/
  10446. int pc_calc_pvprank(struct map_session_data *sd)
  10447. {
  10448. int old = sd->pvp_rank;
  10449. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  10450. sd->pvp_rank=1;
  10451. map_foreachinmap(pc_calc_pvprank_sub,sd->bl.m,BL_PC,sd);
  10452. if(old!=sd->pvp_rank || sd->pvp_lastusers!=mapdata->users_pvp)
  10453. clif_pvpset(sd,sd->pvp_rank,sd->pvp_lastusers=mapdata->users_pvp,0);
  10454. return sd->pvp_rank;
  10455. }
  10456. /*==========================================
  10457. * Calculate next sd ranking calculation from config
  10458. *------------------------------------------*/
  10459. TIMER_FUNC(pc_calc_pvprank_timer){
  10460. struct map_session_data *sd;
  10461. sd=map_id2sd(id);
  10462. if(sd==NULL)
  10463. return 0;
  10464. sd->pvp_timer = INVALID_TIMER;
  10465. if( pc_isinvisible(sd) )
  10466. {// do not calculate the pvp rank for a hidden GM
  10467. return 0;
  10468. }
  10469. if( pc_calc_pvprank(sd) > 0 )
  10470. sd->pvp_timer = add_timer(gettick()+PVP_CALCRANK_INTERVAL,pc_calc_pvprank_timer,id,data);
  10471. return 0;
  10472. }
  10473. /*==========================================
  10474. * Checking if sd is married
  10475. * Return:
  10476. * partner_id = yes
  10477. * 0 = no
  10478. *------------------------------------------*/
  10479. int pc_ismarried(struct map_session_data *sd)
  10480. {
  10481. if(sd == NULL)
  10482. return -1;
  10483. if(sd->status.partner_id > 0)
  10484. return sd->status.partner_id;
  10485. else
  10486. return 0;
  10487. }
  10488. /*==========================================
  10489. * Marry player sd to player dstsd
  10490. * Return:
  10491. * false = fail
  10492. * true = success
  10493. *------------------------------------------*/
  10494. bool pc_marriage(struct map_session_data *sd,struct map_session_data *dstsd)
  10495. {
  10496. if(sd == NULL || dstsd == NULL ||
  10497. sd->status.partner_id > 0 || dstsd->status.partner_id > 0 ||
  10498. (sd->class_&JOBL_BABY) || (dstsd->class_&JOBL_BABY))
  10499. return false;
  10500. sd->status.partner_id = dstsd->status.char_id;
  10501. dstsd->status.partner_id = sd->status.char_id;
  10502. achievement_update_objective(sd, AG_MARRY, 1, 1);
  10503. achievement_update_objective(dstsd, AG_MARRY, 1, 1);
  10504. return true;
  10505. }
  10506. /*==========================================
  10507. * Divorce sd from its partner
  10508. * Return:
  10509. * false = fail
  10510. * true = success
  10511. *------------------------------------------*/
  10512. bool pc_divorce(struct map_session_data *sd)
  10513. {
  10514. struct map_session_data *p_sd;
  10515. int i;
  10516. if( sd == NULL || !pc_ismarried(sd) )
  10517. return false;
  10518. if( !sd->status.partner_id )
  10519. return false; // Char is not married
  10520. if( (p_sd = map_charid2sd(sd->status.partner_id)) == NULL )
  10521. { // Lets char server do the divorce
  10522. if( chrif_divorce(sd->status.char_id, sd->status.partner_id) )
  10523. return false; // No char server connected
  10524. return true;
  10525. }
  10526. // Both players online, lets do the divorce manually
  10527. sd->status.partner_id = 0;
  10528. p_sd->status.partner_id = 0;
  10529. for( i = 0; i < MAX_INVENTORY; i++ )
  10530. {
  10531. if( sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_M || sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_F )
  10532. pc_delitem(sd, i, 1, 0, 0, LOG_TYPE_OTHER);
  10533. if( p_sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_M || p_sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_F )
  10534. pc_delitem(p_sd, i, 1, 0, 0, LOG_TYPE_OTHER);
  10535. }
  10536. clif_divorced(sd, p_sd->status.name);
  10537. clif_divorced(p_sd, sd->status.name);
  10538. return true;
  10539. }
  10540. /**
  10541. * Get the partner map_session_data of a player
  10542. * @param sd : the husband|wife session
  10543. * @return partner session or NULL
  10544. */
  10545. struct map_session_data *pc_get_partner(struct map_session_data *sd){
  10546. if (!sd || !pc_ismarried(sd))
  10547. return NULL;
  10548. return map_charid2sd(sd->status.partner_id);
  10549. }
  10550. /**
  10551. * Get the father map_session_data of a player
  10552. * @param sd : the baby session
  10553. * @return father session or NULL
  10554. */
  10555. struct map_session_data *pc_get_father (struct map_session_data *sd){
  10556. if (!sd || !(sd->class_&JOBL_BABY) || !sd->status.father)
  10557. return NULL;
  10558. return map_charid2sd(sd->status.father);
  10559. }
  10560. /**
  10561. * Get the mother map_session_data of a player
  10562. * @param sd : the baby session
  10563. * @return mother session or NULL
  10564. */
  10565. struct map_session_data *pc_get_mother (struct map_session_data *sd){
  10566. if (!sd || !(sd->class_&JOBL_BABY) || !sd->status.mother)
  10567. return NULL;
  10568. return map_charid2sd(sd->status.mother);
  10569. }
  10570. /*==========================================
  10571. * Get sd children charid. (Need to be married)
  10572. *------------------------------------------*/
  10573. struct map_session_data *pc_get_child (struct map_session_data *sd)
  10574. {
  10575. if (!sd || !pc_ismarried(sd) || !sd->status.child)
  10576. // charid2sd returns NULL if not found
  10577. return NULL;
  10578. return map_charid2sd(sd->status.child);
  10579. }
  10580. /*==========================================
  10581. * Set player sd to bleed. (losing hp and/or sp each diff_tick)
  10582. *------------------------------------------*/
  10583. void pc_bleeding (struct map_session_data *sd, t_tick diff_tick)
  10584. {
  10585. int hp = 0, sp = 0;
  10586. if( pc_isdead(sd) )
  10587. return;
  10588. if (sd->hp_loss.value) {
  10589. sd->hp_loss.tick += diff_tick;
  10590. while (sd->hp_loss.tick >= sd->hp_loss.rate) {
  10591. hp += sd->hp_loss.value;
  10592. sd->hp_loss.tick -= sd->hp_loss.rate;
  10593. }
  10594. if(hp >= sd->battle_status.hp)
  10595. hp = sd->battle_status.hp-1; //Script drains cannot kill you.
  10596. }
  10597. if (sd->sp_loss.value) {
  10598. sd->sp_loss.tick += diff_tick;
  10599. while (sd->sp_loss.tick >= sd->sp_loss.rate) {
  10600. sp += sd->sp_loss.value;
  10601. sd->sp_loss.tick -= sd->sp_loss.rate;
  10602. }
  10603. }
  10604. if (hp > 0 || sp > 0)
  10605. status_zap(&sd->bl, hp, sp);
  10606. }
  10607. //Character regen. Flag is used to know which types of regen can take place.
  10608. //&1: HP regen
  10609. //&2: SP regen
  10610. void pc_regen (struct map_session_data *sd, t_tick diff_tick)
  10611. {
  10612. int hp = 0, sp = 0;
  10613. if (sd->hp_regen.value) {
  10614. sd->hp_regen.tick += diff_tick;
  10615. while (sd->hp_regen.tick >= sd->hp_regen.rate) {
  10616. hp += sd->hp_regen.value;
  10617. sd->hp_regen.tick -= sd->hp_regen.rate;
  10618. }
  10619. }
  10620. if (sd->sp_regen.value) {
  10621. sd->sp_regen.tick += diff_tick;
  10622. while (sd->sp_regen.tick >= sd->sp_regen.rate) {
  10623. sp += sd->sp_regen.value;
  10624. sd->sp_regen.tick -= sd->sp_regen.rate;
  10625. }
  10626. }
  10627. if (sd->percent_hp_regen.value) {
  10628. sd->percent_hp_regen.tick += diff_tick;
  10629. while (sd->percent_hp_regen.tick >= sd->percent_hp_regen.rate) {
  10630. hp += sd->status.max_hp * sd->percent_hp_regen.value / 100;
  10631. sd->percent_hp_regen.tick -= sd->percent_hp_regen.rate;
  10632. }
  10633. }
  10634. if (sd->percent_sp_regen.value) {
  10635. sd->percent_sp_regen.tick += diff_tick;
  10636. while (sd->percent_sp_regen.tick >= sd->percent_sp_regen.rate) {
  10637. sp += sd->status.max_sp * sd->percent_sp_regen.value / 100;
  10638. sd->percent_sp_regen.tick -= sd->percent_sp_regen.rate;
  10639. }
  10640. }
  10641. if (hp > 0 || sp > 0)
  10642. status_heal(&sd->bl, hp, sp, 0);
  10643. }
  10644. /*==========================================
  10645. * Memo player sd savepoint. (map,x,y)
  10646. *------------------------------------------*/
  10647. void pc_setsavepoint(struct map_session_data *sd, short mapindex,int x,int y)
  10648. {
  10649. nullpo_retv(sd);
  10650. sd->status.save_point.map = mapindex;
  10651. sd->status.save_point.x = x;
  10652. sd->status.save_point.y = y;
  10653. }
  10654. /*==========================================
  10655. * Save 1 player data at autosave interval
  10656. *------------------------------------------*/
  10657. static TIMER_FUNC(pc_autosave){
  10658. int interval;
  10659. struct s_mapiterator* iter;
  10660. struct map_session_data* sd;
  10661. static int last_save_id = 0, save_flag = 0;
  10662. if(save_flag == 2) //Someone was saved on last call, normal cycle
  10663. save_flag = 0;
  10664. else
  10665. save_flag = 1; //Noone was saved, so save first found char.
  10666. iter = mapit_getallusers();
  10667. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  10668. {
  10669. if (!sd->state.pc_loaded) // Player data hasn't fully loaded
  10670. continue;
  10671. if(sd->bl.id == last_save_id && save_flag != 1) {
  10672. save_flag = 1;
  10673. continue;
  10674. }
  10675. if(save_flag != 1) //Not our turn to save yet.
  10676. continue;
  10677. //Save char.
  10678. last_save_id = sd->bl.id;
  10679. save_flag = 2;
  10680. if (pc_isvip(sd)) // Check if we're still VIP
  10681. chrif_req_login_operation(1, sd->status.name, CHRIF_OP_LOGIN_VIP, 0, 1, 0);
  10682. chrif_save(sd, CSAVE_INVENTORY|CSAVE_CART);
  10683. break;
  10684. }
  10685. mapit_free(iter);
  10686. interval = autosave_interval/(map_usercount()+1);
  10687. if(interval < minsave_interval)
  10688. interval = minsave_interval;
  10689. add_timer(gettick()+interval,pc_autosave,0,0);
  10690. return 0;
  10691. }
  10692. static int pc_daynight_timer_sub(struct map_session_data *sd,va_list ap)
  10693. {
  10694. if (sd->state.night != night_flag && map_getmapflag(sd->bl.m, MF_NIGHTENABLED))
  10695. { //Night/day state does not match.
  10696. clif_status_load(&sd->bl, EFST_SKE, night_flag); //New night effect by dynamix [Skotlex]
  10697. sd->state.night = night_flag;
  10698. return 1;
  10699. }
  10700. return 0;
  10701. }
  10702. /*================================================
  10703. * timer to do the day [Yor]
  10704. * data: 0 = called by timer, 1 = gmcommand/script
  10705. *------------------------------------------------*/
  10706. TIMER_FUNC(map_day_timer){
  10707. char tmp_soutput[1024];
  10708. if (data == 0 && battle_config.day_duration <= 0) // if we want a day
  10709. return 0;
  10710. if (!night_flag)
  10711. return 0; //Already day.
  10712. night_flag = 0; // 0=day, 1=night [Yor]
  10713. map_foreachpc(pc_daynight_timer_sub);
  10714. strcpy(tmp_soutput, (data == 0) ? msg_txt(NULL,502) : msg_txt(NULL,60)); // The day has arrived!
  10715. intif_broadcast(tmp_soutput, strlen(tmp_soutput) + 1, BC_DEFAULT);
  10716. return 0;
  10717. }
  10718. /*================================================
  10719. * timer to do the night [Yor]
  10720. * data: 0 = called by timer, 1 = gmcommand/script
  10721. *------------------------------------------------*/
  10722. TIMER_FUNC(map_night_timer){
  10723. char tmp_soutput[1024];
  10724. if (data == 0 && battle_config.night_duration <= 0) // if we want a night
  10725. return 0;
  10726. if (night_flag)
  10727. return 0; //Already nigth.
  10728. night_flag = 1; // 0=day, 1=night [Yor]
  10729. map_foreachpc(pc_daynight_timer_sub);
  10730. strcpy(tmp_soutput, (data == 0) ? msg_txt(NULL,503) : msg_txt(NULL,59)); // The night has fallen...
  10731. intif_broadcast(tmp_soutput, strlen(tmp_soutput) + 1, BC_DEFAULT);
  10732. return 0;
  10733. }
  10734. /**
  10735. * Attempt to stand up a player
  10736. * @param sd
  10737. * @param force Ignore the check, ask player to stand up. Used in some cases like pc_damage(), pc_revive(), etc
  10738. * @return True if success, Fals if failed
  10739. */
  10740. bool pc_setstand(struct map_session_data *sd, bool force){
  10741. nullpo_ret(sd);
  10742. // Cannot stand yet
  10743. // TODO: Move to SCS_NOSTAND [Cydh]
  10744. if (!force && (sd->sc.data[SC_SITDOWN_FORCE] || sd->sc.data[SC_BANANA_BOMB_SITDOWN]))
  10745. return false;
  10746. status_change_end(&sd->bl, SC_TENSIONRELAX, INVALID_TIMER);
  10747. clif_status_load(&sd->bl,EFST_SIT,0);
  10748. clif_standing(&sd->bl); //Inform area PC is standing
  10749. //Reset sitting tick.
  10750. sd->ssregen.tick.hp = sd->ssregen.tick.sp = 0;
  10751. if( pc_isdead( sd ) ){
  10752. sd->state.dead_sit = sd->vd.dead_sit = 0;
  10753. clif_party_dead( sd );
  10754. }else{
  10755. sd->state.dead_sit = sd->vd.dead_sit = 0;
  10756. }
  10757. return true;
  10758. }
  10759. /**
  10760. * Calculate Overheat value
  10761. * @param sd: Player data
  10762. * @param heat: Amount of Heat to adjust
  10763. **/
  10764. void pc_overheat(struct map_session_data *sd, int16 heat) {
  10765. nullpo_retv(sd);
  10766. status_change_entry *sce = sd->sc.data[SC_OVERHEAT_LIMITPOINT];
  10767. if (sce) {
  10768. static std::vector<int16> limit = { 150, 200, 280, 360, 450 };
  10769. uint16 skill_lv = cap_value(pc_checkskill(sd, NC_MAINFRAME), 0, (uint16)(limit.size()-1));
  10770. sce->val1 += heat;
  10771. sce->val1 = cap_value(sce->val1, 0, 1000);
  10772. if (sd->sc.data[SC_OVERHEAT])
  10773. status_change_end(&sd->bl, SC_OVERHEAT, INVALID_TIMER);
  10774. if (sce->val1 > limit[skill_lv])
  10775. sc_start(&sd->bl, &sd->bl, SC_OVERHEAT, 100, sce->val1, 1000);
  10776. } else if (heat > 0)
  10777. sc_start(&sd->bl, &sd->bl, SC_OVERHEAT_LIMITPOINT, 100, heat, 1000);
  10778. }
  10779. /**
  10780. * Check if player is autolooting given itemID.
  10781. */
  10782. bool pc_isautolooting(struct map_session_data *sd, t_itemid nameid)
  10783. {
  10784. uint8 i = 0;
  10785. if (sd->state.autoloottype && sd->state.autoloottype&(1<<itemdb_type(nameid)))
  10786. return true;
  10787. if (!sd->state.autolooting)
  10788. return false;
  10789. if (sd->state.autolooting)
  10790. ARR_FIND(0, AUTOLOOTITEM_SIZE, i, sd->state.autolootid[i] == nameid);
  10791. return (i != AUTOLOOTITEM_SIZE);
  10792. }
  10793. /**
  10794. * Checks if player can use @/#command
  10795. * @param sd Player map session data
  10796. * @param command Command name without @/# and params
  10797. * @param type is it atcommand or charcommand
  10798. */
  10799. bool pc_can_use_command( struct map_session_data *sd, const char *command, AtCommandType type ){
  10800. return sd->group->can_use_command( command, type );
  10801. }
  10802. bool pc_has_permission( struct map_session_data* sd, e_pc_permission permission ){
  10803. return sd->permissions.test( permission );
  10804. }
  10805. /**
  10806. * Checks if commands used by a player should be logged
  10807. * according to their group setting.
  10808. * @param sd Player map session data
  10809. */
  10810. bool pc_should_log_commands( struct map_session_data *sd ){
  10811. return sd->group->log_commands;
  10812. }
  10813. /**
  10814. * Spirit Charm expiration timer.
  10815. * @see TimerFunc
  10816. */
  10817. static TIMER_FUNC(pc_spiritcharm_timer){
  10818. struct map_session_data *sd;
  10819. int i;
  10820. if ((sd = (struct map_session_data *)map_id2sd(id)) == NULL || sd->bl.type != BL_PC)
  10821. return 1;
  10822. if (sd->spiritcharm <= 0) {
  10823. 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);
  10824. sd->spiritcharm = 0;
  10825. sd->spiritcharm_type = CHARM_TYPE_NONE;
  10826. return 0;
  10827. }
  10828. ARR_FIND(0, sd->spiritcharm, i, sd->spiritcharm_timer[i] == tid);
  10829. if (i == sd->spiritcharm) {
  10830. ShowError("pc_spiritcharm_timer: timer not found (aid=%d cid=%d tid=%d)\n", sd->status.account_id, sd->status.char_id, tid);
  10831. return 0;
  10832. }
  10833. sd->spiritcharm--;
  10834. if (i != sd->spiritcharm)
  10835. memmove(sd->spiritcharm_timer + i, sd->spiritcharm_timer + i + 1, (sd->spiritcharm - i) * sizeof(int));
  10836. sd->spiritcharm_timer[sd->spiritcharm] = INVALID_TIMER;
  10837. if (sd->spiritcharm <= 0)
  10838. sd->spiritcharm_type = CHARM_TYPE_NONE;
  10839. clif_spiritcharm(sd);
  10840. return 0;
  10841. }
  10842. /**
  10843. * Adds a spirit charm.
  10844. * @param sd: Target character
  10845. * @param interval: Duration
  10846. * @param max: Maximum amount of charms to add
  10847. * @param type: Charm type (@see spirit_charm_types)
  10848. */
  10849. void pc_addspiritcharm(struct map_session_data *sd, int interval, int max, int type)
  10850. {
  10851. int tid, i;
  10852. nullpo_retv(sd);
  10853. if (sd->spiritcharm_type != CHARM_TYPE_NONE && type != sd->spiritcharm_type)
  10854. pc_delspiritcharm(sd, sd->spiritcharm, sd->spiritcharm_type);
  10855. if (max > MAX_SPIRITCHARM)
  10856. max = MAX_SPIRITCHARM;
  10857. if (sd->spiritcharm < 0)
  10858. sd->spiritcharm = 0;
  10859. if (sd->spiritcharm && sd->spiritcharm >= max) {
  10860. if (sd->spiritcharm_timer[0] != INVALID_TIMER)
  10861. delete_timer(sd->spiritcharm_timer[0], pc_spiritcharm_timer);
  10862. sd->spiritcharm--;
  10863. if (sd->spiritcharm != 0)
  10864. memmove(sd->spiritcharm_timer + 0, sd->spiritcharm_timer + 1, (sd->spiritcharm) * sizeof(int));
  10865. sd->spiritcharm_timer[sd->spiritcharm] = INVALID_TIMER;
  10866. }
  10867. tid = add_timer(gettick() + interval, pc_spiritcharm_timer, sd->bl.id, 0);
  10868. 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);
  10869. if (i != sd->spiritcharm)
  10870. memmove(sd->spiritcharm_timer + i + 1, sd->spiritcharm_timer + i, (sd->spiritcharm - i) * sizeof(int));
  10871. sd->spiritcharm_timer[i] = tid;
  10872. sd->spiritcharm++;
  10873. sd->spiritcharm_type = type;
  10874. clif_spiritcharm(sd);
  10875. }
  10876. /**
  10877. * Removes one or more spirit charms.
  10878. * @param sd: The target character
  10879. * @param count: Amount of charms to remove
  10880. * @param type: Type of charm to remove
  10881. */
  10882. void pc_delspiritcharm(struct map_session_data *sd, int count, int type)
  10883. {
  10884. int i;
  10885. nullpo_retv(sd);
  10886. if (sd->spiritcharm_type != type)
  10887. return;
  10888. if (sd->spiritcharm <= 0) {
  10889. sd->spiritcharm = 0;
  10890. return;
  10891. }
  10892. if (count <= 0)
  10893. return;
  10894. if (count > sd->spiritcharm)
  10895. count = sd->spiritcharm;
  10896. sd->spiritcharm -= count;
  10897. if (count > MAX_SPIRITCHARM)
  10898. count = MAX_SPIRITCHARM;
  10899. for (i = 0; i < count; i++) {
  10900. if (sd->spiritcharm_timer[i] != INVALID_TIMER) {
  10901. delete_timer(sd->spiritcharm_timer[i], pc_spiritcharm_timer);
  10902. sd->spiritcharm_timer[i] = INVALID_TIMER;
  10903. }
  10904. }
  10905. for (i = count; i < MAX_SPIRITCHARM; i++) {
  10906. sd->spiritcharm_timer[i - count] = sd->spiritcharm_timer[i];
  10907. sd->spiritcharm_timer[i] = INVALID_TIMER;
  10908. }
  10909. if (sd->spiritcharm <= 0)
  10910. sd->spiritcharm_type = CHARM_TYPE_NONE;
  10911. clif_spiritcharm(sd);
  10912. }
  10913. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  10914. /**
  10915. * Renewal EXP/Item Drop rate modifier based on level penalty
  10916. * @param level_diff: Monster and Player level difference
  10917. * @param mob_class: Monster class
  10918. * @param mode: Monster mode
  10919. * @param type: 1 - EXP, 2 - Item Drop
  10920. * @return Penalty rate
  10921. */
  10922. uint16 pc_level_penalty_mod( struct map_session_data* sd, e_penalty_type type, std::shared_ptr<s_mob_db> mob, mob_data* md ){
  10923. // No player was attached, we don't use any modifier (100 = rates are not touched)
  10924. if( sd == nullptr ){
  10925. return 100;
  10926. }
  10927. if ((type == PENALTY_DROP && map_getmapflag(sd->bl.m, MF_NORENEWALDROPPENALTY)) || (type == PENALTY_EXP && map_getmapflag(sd->bl.m, MF_NORENEWALEXPPENALTY))) {
  10928. return 100;
  10929. }
  10930. int monster_level;
  10931. if( md != nullptr ){
  10932. monster_level = md->level;
  10933. mob = md->db;
  10934. }else if( mob != nullptr ){
  10935. monster_level = mob->lv;
  10936. }else{
  10937. return 100;
  10938. }
  10939. if( ( type == PENALTY_DROP || type == PENALTY_MVP_DROP ) && status_has_mode( &mob->status, MD_FIXEDITEMDROP ) ){
  10940. return 100;
  10941. }
  10942. int level_difference = monster_level - sd->status.base_level;
  10943. std::shared_ptr<s_penalty> penalty = penalty_db.find( type );
  10944. if( penalty != nullptr ){
  10945. return penalty->rate[ level_difference + MAX_LEVEL - 1 ];
  10946. }else{
  10947. return 100;
  10948. }
  10949. }
  10950. #endif
  10951. int pc_split_str(char *str,char **val,int num)
  10952. {
  10953. int i;
  10954. for (i=0; i<num && str; i++){
  10955. val[i] = str;
  10956. str = strchr(str,',');
  10957. if (str && i<num-1) //Do not remove a trailing comma.
  10958. *str++=0;
  10959. }
  10960. return i;
  10961. }
  10962. int pc_split_atoui(char* str, unsigned int* val, char sep, int max)
  10963. {
  10964. static int warning=0;
  10965. int i,j;
  10966. for (i=0; i<max; i++) {
  10967. double f;
  10968. if (!str) break;
  10969. f = atof(str);
  10970. if (f < 0)
  10971. val[i] = 0;
  10972. else if (f > UINT_MAX) {
  10973. val[i] = UINT_MAX;
  10974. if (!warning) {
  10975. warning = 1;
  10976. ShowWarning("pc_readdb (exp.txt): Required exp per level is capped to %u\n", UINT_MAX);
  10977. }
  10978. } else
  10979. val[i] = (unsigned int)f;
  10980. str = strchr(str,sep);
  10981. if (str)
  10982. *str++=0;
  10983. }
  10984. //Zero up the remaining.
  10985. for(j=i; j < max; j++)
  10986. val[j] = 0;
  10987. return i;
  10988. }
  10989. std::shared_ptr<s_skill_tree_entry> SkillTreeDatabase::get_skill_data(int class_, uint16 skill_id) {
  10990. std::shared_ptr<s_skill_tree> tree = this->find(class_);
  10991. if (tree != nullptr)
  10992. return util::umap_find(tree->skills, skill_id);
  10993. return nullptr;
  10994. }
  10995. const std::string SkillTreeDatabase::getDefaultLocation() {
  10996. return std::string(db_path) + "/skill_tree.yml";
  10997. }
  10998. /**
  10999. * Reads and parses an entry from the skill_tree.
  11000. * @param node: YAML node containing the entry.
  11001. * @return count of successfully parsed rows
  11002. */
  11003. uint64 SkillTreeDatabase::parseBodyNode(const ryml::NodeRef& node) {
  11004. std::string job_name;
  11005. if (!this->asString(node, "Job", job_name))
  11006. return 0;
  11007. int64 constant;
  11008. std::string job_name_constant = "JOB_" + job_name;
  11009. if (!script_get_constant(job_name_constant.c_str(), &constant) || !pcdb_checkid(constant)) {
  11010. this->invalidWarning(node["Job"], "Invalid job %s.\n", job_name.c_str());
  11011. return 0;
  11012. }
  11013. uint16 job_id = static_cast<uint16>(constant);
  11014. std::shared_ptr<s_skill_tree> tree = this->find(job_id);
  11015. bool exists = tree != nullptr;
  11016. if (!exists)
  11017. tree = std::make_shared<s_skill_tree>();
  11018. if (this->nodeExists(node, "Inherit")) {
  11019. const ryml::NodeRef& InheritNode = node["Inherit"];
  11020. for (const auto &Inheritit : InheritNode) {
  11021. std::string inheritname;
  11022. c4::from_chars(Inheritit.key(), &inheritname);
  11023. std::string inheritname_constant = "JOB_" + inheritname;
  11024. if (!script_get_constant(inheritname_constant.c_str(), &constant) || !pcdb_checkid(constant)) {
  11025. this->invalidWarning(InheritNode[Inheritit.key()], "Invalid job %s.\n", inheritname.c_str());
  11026. return 0;
  11027. }
  11028. bool active;
  11029. if (!this->asBool(InheritNode, inheritname, active))
  11030. return 0;
  11031. uint16 inherit_job = static_cast<uint16>(constant);
  11032. if (!active) {
  11033. if (exists)
  11034. util::vector_erase_if_exists(tree->inherit_job, inherit_job);
  11035. }
  11036. else {
  11037. if (!util::vector_exists(tree->inherit_job, inherit_job))
  11038. tree->inherit_job.push_back(inherit_job);
  11039. }
  11040. }
  11041. }
  11042. if (this->nodeExists(node, "Tree")) {
  11043. for (const auto &it : node["Tree"]) {
  11044. std::string skill_name;
  11045. if (!this->asString(it, "Name", skill_name))
  11046. return 0;
  11047. uint16 skill_id = skill_name2id(skill_name.c_str());
  11048. if (skill_id == 0) {
  11049. this->invalidWarning(it["Name"], "Invalid skill name \"%s\".\n", skill_name.c_str());
  11050. return 0;
  11051. }
  11052. if (!skill_get_index(skill_id)) {
  11053. this->invalidWarning(it["Name"], "Unable to load skill %s into job %hu's tree.\n", skill_name.c_str(), job_id);
  11054. return 0;
  11055. }
  11056. std::shared_ptr<s_skill_tree_entry> entry;
  11057. bool skill_exists = tree->skills.count(skill_id) > 0;
  11058. if (skill_exists)
  11059. entry = tree->skills[skill_id];
  11060. else
  11061. entry = std::make_shared<s_skill_tree_entry>();
  11062. entry->skill_id = skill_id;
  11063. uint16 max_lv;
  11064. if (!this->asUInt16(it, "MaxLevel", max_lv))
  11065. return 0;
  11066. if (max_lv > MAX_SKILL_LEVEL) {
  11067. this->invalidWarning(it["MaxLevel"], "MaxLevel exceeds the maximum skill level of %d, skipping.\n", MAX_SKILL_LEVEL);
  11068. return 0;
  11069. }
  11070. uint16 skill_lv_max = skill_get_max(skill_id);
  11071. if (max_lv > skill_lv_max) {
  11072. this->invalidWarning(it["MaxLevel"], "Skill %s's level %hu exceeds the skill's max level %hu. Capping skill level.\n", skill_name.c_str(), max_lv, skill_lv_max);
  11073. max_lv = skill_lv_max;
  11074. }
  11075. // if (max_lv == 0) { // skill lvl 0 removed on loadingFinished (because of inherit)
  11076. // if (!skill_exists || entry->skill_id.erase(skill_id) == 0)
  11077. // this->invalidWarning(it["Name"], "Failed to erase %s, the skill doesn't exist in for job %s, skipping.\n", skill_name.c_str(), job_name.c_str());
  11078. // continue;
  11079. // }
  11080. entry->max_lv = max_lv;
  11081. if (this->nodeExists(it, "BaseLevel")) {
  11082. uint32 baselv;
  11083. if (!this->asUInt32(it, "BaseLevel", baselv))
  11084. return 0;
  11085. uint32 baselv_max = job_db.get_maxBaseLv(job_id);
  11086. if (baselv > baselv_max) {
  11087. this->invalidWarning(it["BaseLevel"], "Skill %hu's base level requirement %hu exceeds job %s's max base level %d. Capping skill base level.\n",
  11088. skill_id, baselv, job_name.c_str(), baselv_max);
  11089. baselv = baselv_max;
  11090. }
  11091. entry->baselv = baselv;
  11092. } else {
  11093. if (!skill_exists)
  11094. entry->baselv = 0;
  11095. }
  11096. if (this->nodeExists(it, "JobLevel")) {
  11097. uint32 joblv;
  11098. if (!this->asUInt32(it, "JobLevel", joblv))
  11099. return 0;
  11100. uint32 joblv_max = job_db.get_maxJobLv(job_id);
  11101. if (joblv > joblv_max) {
  11102. this->invalidWarning(it["JobLevel"], "Skill %hu's job level requirement %hu exceeds job %s's max job level %d. Capping skill job level.\n",
  11103. skill_id, joblv, job_name.c_str(), joblv_max);
  11104. joblv = joblv_max;
  11105. }
  11106. entry->joblv = joblv;
  11107. } else {
  11108. if (!skill_exists)
  11109. entry->joblv = 0;
  11110. }
  11111. if (this->nodeExists(it, "Requires")) {
  11112. for (const auto &Requiresit : it["Requires"]) {
  11113. if (!this->nodesExist(Requiresit, { "Name" }))
  11114. return 0;
  11115. std::string skill_name_req;
  11116. if (!this->asString(Requiresit, "Name", skill_name_req))
  11117. return 0;
  11118. uint16 skill_id_req = skill_name2id(skill_name_req.c_str());
  11119. if (skill_id_req == 0) {
  11120. this->invalidWarning(Requiresit["Name"], "Invalid skill name \"%s\".\n", skill_name_req.c_str());
  11121. return 0;
  11122. }
  11123. uint16 lv_req;
  11124. if (!this->asUInt16(Requiresit, "Level", lv_req))
  11125. return 0;
  11126. if (lv_req > MAX_SKILL_LEVEL) {
  11127. this->invalidWarning(Requiresit["Level"], "Level exceeds the maximum skill level of %d, skipping.\n", MAX_SKILL_LEVEL);
  11128. return 0;
  11129. }
  11130. uint16 lv_req_max = skill_get_max(skill_id_req);
  11131. if (lv_req > lv_req_max) {
  11132. this->invalidWarning(it["MaxLevel"], "Required skill %s's level %hu exceeds the skill's max level %hu. Capping skill level.\n", skill_name.c_str(), lv_req, lv_req_max);
  11133. lv_req = lv_req_max;
  11134. }
  11135. if (lv_req == 0) {
  11136. if (entry->need.erase(skill_id_req) == 0)
  11137. this->invalidWarning(Requiresit["Name"], "Failed to erase %s, the skill doesn't exist in for job %s, skipping.\n", skill_name_req.c_str(), job_name.c_str());
  11138. continue;
  11139. }
  11140. entry->need[skill_id_req] = lv_req;
  11141. }
  11142. }
  11143. if (this->nodeExists(it, "Exclude")) {
  11144. bool exclude;
  11145. if (!this->asBool(it, "Exclude", exclude))
  11146. return 0;
  11147. entry->exclude_inherit = exclude;
  11148. } else {
  11149. if (!skill_exists)
  11150. entry->exclude_inherit = false;
  11151. }
  11152. if (!skill_exists)
  11153. tree->skills.insert({ skill_id, entry });
  11154. }
  11155. }
  11156. if (!exists)
  11157. this->put(job_id, tree);
  11158. return true;
  11159. }
  11160. void SkillTreeDatabase::loadingFinished() {
  11161. std::unordered_map<uint16, std::shared_ptr<s_skill_tree>> job_tree; // get the data from skill_tree_db before populate it
  11162. for (auto &data : *this) {
  11163. if (data.second->inherit_job.empty())
  11164. continue;
  11165. std::shared_ptr<s_skill_tree> skill_tree = std::make_shared<s_skill_tree>();
  11166. uint32 baselv_max = job_db.get_maxBaseLv(data.first);
  11167. uint32 joblv_max = job_db.get_maxJobLv(data.first);
  11168. for (const auto &inherit_job : data.second->inherit_job) {
  11169. std::shared_ptr<s_skill_tree> tree = this->find(inherit_job);
  11170. if (tree == nullptr || tree->skills.empty())
  11171. continue;
  11172. for (const auto &it : tree->skills) {
  11173. if (it.second->exclude_inherit)
  11174. continue;
  11175. if (data.second->skills.count(it.first) > 0) // skill already in the skill tree
  11176. continue;
  11177. if (skill_tree->skills.count(it.first) > 0) // replaced by the last inheritance
  11178. skill_tree->skills[it.first] = it.second;
  11179. else
  11180. skill_tree->skills.insert({ it.first, it.second });
  11181. std::shared_ptr<s_skill_tree_entry> skill = skill_tree->skills[it.first];
  11182. if (skill->baselv > baselv_max) {
  11183. ShowWarning("SkillTreeDatabase: Skill %s (%hu)'s base level requirement %hu exceeds job %s's max base level %d. Capping skill base level.\n",
  11184. skill_get_name(skill->skill_id), skill->skill_id, skill->baselv, job_name(data.first), baselv_max);
  11185. skill->baselv = baselv_max;
  11186. }
  11187. if (skill->joblv > joblv_max) {
  11188. ShowWarning("SkillTreeDatabase: Skill %s (%hu)'s job level requirement %hu exceeds job %s's max job level %d. Capping skill job level.\n",
  11189. skill_get_name(skill->skill_id), skill->skill_id, skill->joblv, job_name(data.first), joblv_max);
  11190. skill->joblv = joblv_max;
  11191. }
  11192. }
  11193. }
  11194. if (skill_tree != nullptr && !skill_tree->skills.empty())
  11195. job_tree.insert({ data.first, skill_tree });
  11196. }
  11197. if (!job_tree.empty()) {
  11198. for (auto &data : *this) {
  11199. if (job_tree.count(data.first) == 0)
  11200. continue;
  11201. data.second->skills.insert(job_tree[data.first]->skills.begin(), job_tree[data.first]->skills.end());
  11202. }
  11203. }
  11204. // remove skills with max_lv = 0
  11205. for (const auto &job : *this) {
  11206. if (job.second->skills.empty())
  11207. continue;
  11208. auto it = job.second->skills.begin();
  11209. while( it != job.second->skills.end() ){
  11210. if( it->second->max_lv == 0 ){
  11211. it = job.second->skills.erase( it );
  11212. }else{
  11213. it++;
  11214. }
  11215. }
  11216. }
  11217. }
  11218. /**
  11219. * Calculates base hp of player. Reference: http://irowiki.org/wiki/Max_HP
  11220. * @param level: Base level of player
  11221. * @param job_id: Job ID @see enum e_job
  11222. * @return base_hp
  11223. * @author [Cydh]
  11224. */
  11225. static unsigned int pc_calc_basehp(uint16 level, uint16 job_id) {
  11226. std::shared_ptr<s_job_info> job = job_db.find(job_id);
  11227. double base_hp = 35 + level * (job->hp_increase / 100.);
  11228. #ifndef RENEWAL
  11229. if (level >= 10 && (job_id == JOB_NINJA || job_id == JOB_GUNSLINGER))
  11230. base_hp += 90;
  11231. #endif
  11232. for (uint16 i = 2; i <= level; i++)
  11233. base_hp += floor(((job->hp_factor / 100.) * i) + 0.5); //Don't have round()
  11234. if (job_id == JOB_SUMMONER || job_id == JOB_SPIRIT_HANDLER)
  11235. base_hp += floor((base_hp / 2) + 0.5);
  11236. return (unsigned int)base_hp;
  11237. }
  11238. /**
  11239. * Calculates base sp of player.
  11240. * @param level: Base level of player
  11241. * @param job_id: Job ID @see enum e_job
  11242. * @return base_sp
  11243. * @author [Playtester]
  11244. */
  11245. static unsigned int pc_calc_basesp(uint16 level, uint16 job_id) {
  11246. std::shared_ptr<s_job_info> job = job_db.find(job_id);
  11247. double base_sp = 10 + floor(level * (job->sp_increase / 100.));
  11248. switch (job_id) {
  11249. case JOB_NINJA:
  11250. if (level >= 10)
  11251. base_sp -= 22;
  11252. else
  11253. base_sp = 11 + 3*level;
  11254. break;
  11255. case JOB_GUNSLINGER:
  11256. if (level > 10)
  11257. base_sp -= 18;
  11258. else
  11259. base_sp = 9 + 3*level;
  11260. break;
  11261. case JOB_SUMMONER:
  11262. case JOB_SPIRIT_HANDLER:
  11263. base_sp -= floor(base_sp / 2);
  11264. break;
  11265. }
  11266. return (unsigned int)base_sp;
  11267. }
  11268. const std::string JobDatabase::getDefaultLocation() {
  11269. return std::string(db_path) + "/job_stats.yml";
  11270. }
  11271. /**
  11272. * Reads and parses an entry from the job_db.
  11273. * @param node: YAML node containing the entry.
  11274. * @return count of successfully parsed rows
  11275. */
  11276. uint64 JobDatabase::parseBodyNode(const ryml::NodeRef& node) {
  11277. if (this->nodeExists(node, "Jobs")) {
  11278. const ryml::NodeRef& jobsNode = node["Jobs"];
  11279. for (const auto &jobit : jobsNode) {
  11280. std::string job_name;
  11281. c4::from_chars(jobit.key(), &job_name);
  11282. std::string job_name_constant = "JOB_" + job_name;
  11283. int64 job_id;
  11284. if (!script_get_constant(job_name_constant.c_str(), &job_id)) {
  11285. this->invalidWarning(node["Job"], "Job %s does not exist.\n", job_name.c_str());
  11286. return 0;
  11287. }
  11288. std::shared_ptr<s_job_info> job = job_db.find(static_cast<uint16>(job_id));
  11289. bool exists = job != nullptr;
  11290. if (!exists) {
  11291. job = std::make_shared<s_job_info>();
  11292. job->job_bonus.resize(MAX_LEVEL);
  11293. std::fill(job->job_bonus.begin(), job->job_bonus.end(), std::array<uint16, PARAM_MAX> { 0 });
  11294. job->base_hp.resize(MAX_LEVEL);
  11295. std::fill(job->base_hp.begin(), job->base_hp.end(), 0);
  11296. job->base_sp.resize(MAX_LEVEL);
  11297. std::fill(job->base_sp.begin(), job->base_sp.end(), 0);
  11298. job->base_ap.resize(MAX_LEVEL);
  11299. std::fill(job->base_ap.begin(), job->base_ap.end(), 0);
  11300. }
  11301. if (this->nodeExists(node, "MaxWeight")) {
  11302. uint32 weight;
  11303. if (!this->asUInt32(node, "MaxWeight", weight))
  11304. return 0;
  11305. job->max_weight_base = weight;
  11306. } else {
  11307. if (!exists)
  11308. job->max_weight_base = 20000;
  11309. }
  11310. if (this->nodeExists(node, "HpFactor")) {
  11311. uint32 hp;
  11312. if (!this->asUInt32(node, "HpFactor", hp))
  11313. return 0;
  11314. job->hp_factor = hp;
  11315. } else {
  11316. if (!exists)
  11317. job->hp_factor = 0;
  11318. }
  11319. if (this->nodeExists(node, "HpIncrease")) {
  11320. uint32 hp;
  11321. if (!this->asUInt32(node, "HpIncrease", hp))
  11322. return 0;
  11323. job->hp_increase = hp;
  11324. } else {
  11325. if (!exists)
  11326. job->hp_increase = 500;
  11327. }
  11328. if (this->nodeExists(node, "SpIncrease")) {
  11329. uint32 sp;
  11330. if (!this->asUInt32(node, "SpIncrease", sp))
  11331. return 0;
  11332. job->sp_increase = sp;
  11333. } else {
  11334. if (!exists)
  11335. job->sp_increase = 100;
  11336. }
  11337. if (this->nodeExists(node, "BaseASPD")) {
  11338. const ryml::NodeRef& aspdNode = node["BaseASPD"];
  11339. uint8 max = MAX_WEAPON_TYPE;
  11340. #ifdef RENEWAL // Renewal adds an extra column for shields
  11341. max += 1;
  11342. #endif
  11343. if (!exists) {
  11344. job->aspd_base.resize(max);
  11345. std::fill(job->aspd_base.begin(), job->aspd_base.end(), 2000);
  11346. }
  11347. for (const auto &aspdit : aspdNode) {
  11348. std::string weapon;
  11349. c4::from_chars(aspdit.key(), &weapon);
  11350. std::string weapon_constant = "W_" + weapon;
  11351. int64 constant;
  11352. if (!script_get_constant(weapon_constant.c_str(), &constant)) {
  11353. this->invalidWarning(aspdNode["BaseASPD"], "Unknown weapon type %s specified for %s, skipping.\n", weapon.c_str(), job_name.c_str());
  11354. continue;
  11355. }
  11356. if (constant < W_FIST || constant > max) {
  11357. this->invalidWarning(aspdNode["BaseASPD"], "Invalid weapon type %s specified for %s, skipping.\n", weapon.c_str(), job_name.c_str());
  11358. continue;
  11359. }
  11360. int16 aspd;
  11361. if (!this->asInt16(aspdNode, weapon.c_str(), aspd))
  11362. return 0;
  11363. job->aspd_base[static_cast<int16>(constant)] = aspd;
  11364. }
  11365. } else {
  11366. if (!exists) {
  11367. uint8 max = MAX_WEAPON_TYPE;
  11368. #ifdef RENEWAL // Renewal adds an extra column for shields
  11369. max += 1;
  11370. #endif
  11371. job->aspd_base.resize(max);
  11372. std::fill(job->aspd_base.begin(), job->aspd_base.end(), 2000);
  11373. }
  11374. }
  11375. if (this->nodeExists(node, "MaxStats")) {
  11376. const ryml::NodeRef& statNode = node["MaxStats"];
  11377. for (const auto &statit : statNode) {
  11378. std::string stat;
  11379. c4::from_chars(statit.key(), &stat);
  11380. std::string stat_constant = "PARAM_" + stat;
  11381. int64 constant;
  11382. if (!script_get_constant(stat_constant.c_str(), &constant)) {
  11383. this->invalidWarning(statNode["Bonus"], "Unknown max stat %s specified for %s, skipping.\n", stat.c_str(), job_name.c_str());
  11384. continue;
  11385. }
  11386. if (constant < PARAM_STR || constant >= PARAM_MAX) {
  11387. this->invalidWarning(statNode["Bonus"], "Invalid max stat %s specified for %s, skipping.\n", stat.c_str(), job_name.c_str());
  11388. continue;
  11389. }
  11390. uint16 max;
  11391. if (!this->asUInt16(statNode, stat.c_str(), max))
  11392. return 0;
  11393. job->max_param[constant] = max;
  11394. }
  11395. }
  11396. if (this->nodeExists(node, "MaxBaseLevel")) {
  11397. uint16 level;
  11398. if (!this->asUInt16(node, "MaxBaseLevel", level))
  11399. return 0;
  11400. if (level == 0 || level > MAX_LEVEL) {
  11401. this->invalidWarning(node["MaxBaseLevel"], "MaxBaseLevel must be between 1~MAX_LEVEL for %s, capping to MAX_LEVEL.\n", job_name.c_str());
  11402. level = MAX_LEVEL;
  11403. }
  11404. job->max_base_level = level;
  11405. } else {
  11406. if (!exists)
  11407. job->max_base_level = MAX_LEVEL;
  11408. }
  11409. if (this->nodeExists(node, "BaseExp")) {
  11410. for (const ryml::NodeRef& bexpNode : node["BaseExp"]) {
  11411. uint16 level;
  11412. if (!this->asUInt16(bexpNode, "Level", level))
  11413. return 0;
  11414. if (level > job->max_base_level)
  11415. continue;
  11416. if (level == 0 || level > MAX_LEVEL) {
  11417. this->invalidWarning(bexpNode["Level"], "Level must be between 1~MAX_LEVEL for %s.\n", job_name.c_str());
  11418. return 0;
  11419. }
  11420. if (this->nodeExists(bexpNode, "Exp")) {
  11421. t_exp exp;
  11422. if (!this->asUInt64(bexpNode, "Exp", exp))
  11423. return 0;
  11424. job->base_exp[level - 1] = exp;
  11425. }
  11426. }
  11427. }
  11428. if (this->nodeExists(node, "MaxJobLevel")) {
  11429. uint16 level;
  11430. if (!this->asUInt16(node, "MaxJobLevel", level))
  11431. return 0;
  11432. if (level == 0 || level > MAX_LEVEL) {
  11433. this->invalidWarning(node["MaxJobLevel"], "MaxJobLevel must be between 1~MAX_LEVEL for %s, capping to MAX_LEVEL.\n", job_name.c_str());
  11434. level = MAX_LEVEL;
  11435. }
  11436. job->max_job_level = level;
  11437. } else {
  11438. if (!exists)
  11439. job->max_job_level = MAX_LEVEL;
  11440. }
  11441. if (this->nodeExists(node, "JobExp")) {
  11442. for (const ryml::NodeRef& jexpNode : node["JobExp"]) {
  11443. uint16 level;
  11444. if (!this->asUInt16(jexpNode, "Level", level))
  11445. return 0;
  11446. if (level > job->max_job_level)
  11447. continue;
  11448. if (level == 0 || level > MAX_LEVEL) {
  11449. this->invalidWarning(jexpNode["Level"], "Level must be between 1~MAX_LEVEL for %s.\n", job_name.c_str());
  11450. return 0;
  11451. }
  11452. if (this->nodeExists(jexpNode, "Exp")) {
  11453. t_exp exp;
  11454. if (!this->asUInt64(jexpNode, "Exp", exp))
  11455. return 0;
  11456. job->job_exp[level - 1] = exp;
  11457. }
  11458. }
  11459. }
  11460. if (this->nodeExists(node, "BonusStats")) {
  11461. const ryml::NodeRef& bonusNode = node["BonusStats"];
  11462. for (const ryml::NodeRef& levelNode : bonusNode) {
  11463. uint16 level;
  11464. if (!this->asUInt16(levelNode, "Level", level))
  11465. return 0;
  11466. if (level == 0 || level > MAX_LEVEL) {
  11467. this->invalidWarning(levelNode["Level"], "Level must be between 1~MAX_LEVEL for %s.\n", job_name.c_str());
  11468. return 0;
  11469. }
  11470. for (uint8 idx = PARAM_STR; idx < PARAM_MAX; idx++) {
  11471. if (this->nodeExists(levelNode, parameter_names[idx])) {
  11472. int16 change;
  11473. if (!this->asInt16(levelNode, parameter_names[idx], change))
  11474. return 0;
  11475. job->job_bonus[level - 1][idx] = change;
  11476. }
  11477. }
  11478. }
  11479. }
  11480. #ifdef HP_SP_TABLES
  11481. if (this->nodeExists(node, "BaseHp")) {
  11482. for (const ryml::NodeRef& bhpNode : node["BaseHp"]) {
  11483. uint16 level;
  11484. if (!this->asUInt16(bhpNode, "Level", level))
  11485. return 0;
  11486. if (level > job->max_base_level)
  11487. continue;
  11488. if (level == 0 || level > MAX_LEVEL) {
  11489. this->invalidWarning(bhpNode["Level"], "Level must be between 1~MAX_LEVEL for %s.\n", job_name.c_str());
  11490. return 0;
  11491. }
  11492. if (this->nodeExists(bhpNode, "Hp")) {
  11493. uint32 points;
  11494. if (!this->asUInt32(bhpNode, "Hp", points))
  11495. return 0;
  11496. job->base_hp[level - 1] = points;
  11497. }
  11498. }
  11499. }
  11500. if (this->nodeExists(node, "BaseSp")) {
  11501. for (const ryml::NodeRef& bspNode : node["BaseSp"]) {
  11502. uint16 level;
  11503. if (!this->asUInt16(bspNode, "Level", level))
  11504. return 0;
  11505. if (level > job->max_base_level)
  11506. continue;
  11507. if (level == 0 || level > MAX_LEVEL) {
  11508. this->invalidWarning(bspNode["Level"], "Level must be between 1~MAX_LEVEL for %s.\n", job_name.c_str());
  11509. return 0;
  11510. }
  11511. if (this->nodeExists(bspNode, "Sp")) {
  11512. uint32 points;
  11513. if (!this->asUInt32(bspNode, "Sp", points))
  11514. return 0;
  11515. job->base_sp[level - 1] = points;
  11516. }
  11517. }
  11518. }
  11519. if (this->nodeExists(node, "BaseAp")) {
  11520. for (const ryml::NodeRef& bapNode : node["BaseAp"]) {
  11521. uint16 level;
  11522. if (!this->asUInt16(bapNode, "Level", level))
  11523. return 0;
  11524. if (level > job->max_base_level)
  11525. continue;
  11526. if (level == 0 || level > MAX_LEVEL) {
  11527. this->invalidWarning(bapNode["Level"], "Level must be between 1~MAX_LEVEL for %s.\n", job_name.c_str());
  11528. return 0;
  11529. }
  11530. if (this->nodeExists(bapNode, "Ap")) {
  11531. uint32 points;
  11532. if (!this->asUInt32(bapNode, "Ap", points))
  11533. return 0;
  11534. job->base_ap[level - 1] = points;
  11535. }
  11536. }
  11537. }
  11538. #endif
  11539. if (!exists)
  11540. this->put(static_cast<uint16>(job_id), job);
  11541. }
  11542. }
  11543. return 1;
  11544. }
  11545. void JobDatabase::loadingFinished() {
  11546. // Checking if all class have their data
  11547. for (auto &jobIt : *this) {
  11548. uint16 job_id = jobIt.first;
  11549. if (!pcdb_checkid(job_id))
  11550. continue;
  11551. if (job_id == JOB_WEDDING || job_id == JOB_XMAS || job_id == JOB_SUMMER || job_id == JOB_HANBOK || job_id == JOB_OKTOBERFEST || job_id == JOB_SUMMER2)
  11552. continue; // Classes that do not need exp tables.
  11553. std::shared_ptr<s_job_info> job = jobIt.second;
  11554. uint16 maxBaseLv = job->max_base_level, maxJobLv = job->max_job_level;
  11555. if (!maxBaseLv)
  11556. ShowWarning("Class %s (%d) does not have a base exp table.\n", job_name(job_id), job_id);
  11557. if (!maxJobLv)
  11558. ShowWarning("Class %s (%d) does not have a job exp table.\n", job_name(job_id), job_id);
  11559. // Init and checking the empty value of Base HP/SP [Cydh]
  11560. if (job->base_hp.empty())
  11561. job->base_hp.resize(maxBaseLv);
  11562. for (uint16 j = 0; j < maxBaseLv; j++) {
  11563. if (job->base_hp[j] == 0)
  11564. job->base_hp[j] = pc_calc_basehp(j + 1, job_id);
  11565. }
  11566. if (job->base_sp.empty())
  11567. job->base_sp.resize(maxBaseLv);
  11568. for (uint16 j = 0; j < maxBaseLv; j++) {
  11569. if (job->base_sp[j] == 0)
  11570. job->base_sp[j] = pc_calc_basesp(j + 1, job_id);
  11571. }
  11572. // Resize to the maximum base level
  11573. if (job->base_hp.capacity() > maxBaseLv)
  11574. job->base_hp.erase(job->base_hp.begin() + maxBaseLv, job->base_hp.end());
  11575. if (job->base_sp.capacity() > maxBaseLv)
  11576. job->base_sp.erase(job->base_sp.begin() + maxBaseLv, job->base_sp.end());
  11577. if (job->base_ap.capacity() > maxBaseLv)
  11578. job->base_ap.erase(job->base_ap.begin() + maxBaseLv, job->base_ap.end());
  11579. // Resize to the maximum job level
  11580. if (job->job_bonus.capacity() > maxJobLv)
  11581. job->job_bonus.erase(job->job_bonus.begin() + maxJobLv, job->job_bonus.end());
  11582. for (uint16 parameter = PARAM_STR; parameter < PARAM_MAX; parameter++) {
  11583. // Store total
  11584. int16 current = 0;
  11585. for (uint16 job_level = 0; job_level < maxJobLv; job_level++) {
  11586. // Add the bonus from this job level
  11587. current += job->job_bonus[job_level][parameter];
  11588. // Set the new total on this job level
  11589. job->job_bonus[job_level][parameter] = current;
  11590. }
  11591. }
  11592. uint64 class_ = pc_jobid2mapid( job_id );
  11593. // Set normal status limits
  11594. uint16 max = battle_config.max_parameter;
  11595. do{
  11596. // Always check babies first
  11597. if( class_ & JOBL_BABY ){
  11598. if( class_ & JOBL_THIRD ){
  11599. max = battle_config.max_baby_third_parameter;
  11600. break;
  11601. }else{
  11602. max = battle_config.max_baby_parameter;
  11603. break;
  11604. }
  11605. }
  11606. // Summoner
  11607. if( ( class_ & MAPID_BASEMASK ) == MAPID_SUMMONER ){
  11608. max = battle_config.max_summoner_parameter;
  11609. break;
  11610. }
  11611. // Extended classes
  11612. if( ( class_ & MAPID_UPPERMASK ) == MAPID_KAGEROUOBORO || ( class_ & MAPID_UPPERMASK ) == MAPID_REBELLION ){
  11613. max = battle_config.max_extended_parameter;
  11614. break;
  11615. }
  11616. if( class_ & JOBL_FOURTH ){
  11617. max = battle_config.max_fourth_parameter;
  11618. break;
  11619. }
  11620. // 3rd class
  11621. if( class_ & JOBL_THIRD ){
  11622. // Transcendent
  11623. if( class_ & JOBL_UPPER ){
  11624. max = battle_config.max_third_trans_parameter;
  11625. break;
  11626. }else{
  11627. max = battle_config.max_third_parameter;
  11628. break;
  11629. }
  11630. }
  11631. // Transcendent
  11632. if( class_ & JOBL_UPPER ){
  11633. max = battle_config.max_trans_parameter;
  11634. break;
  11635. }
  11636. }while( false );
  11637. for( uint16 parameter = PARAM_STR; parameter < PARAM_POW; parameter++ ){
  11638. // If it is not explicitly set in the database file
  11639. if( job->max_param[parameter] == 0 ){
  11640. job->max_param[parameter] = max;
  11641. }
  11642. }
  11643. // Set trait status limit
  11644. if( class_ & JOBL_FOURTH ){
  11645. max = battle_config.max_trait_parameter;
  11646. }else{
  11647. max = 0;
  11648. }
  11649. for( uint16 parameter = PARAM_POW; parameter < PARAM_MAX; parameter++ ){
  11650. // If it is not explicitly set in the database file
  11651. if( job->max_param[parameter] == 0 ){
  11652. job->max_param[parameter] = max;
  11653. }
  11654. }
  11655. }
  11656. }
  11657. /**
  11658. * Read job_noenter_map.txt
  11659. **/
  11660. static bool pc_readdb_job_noenter_map(char *str[], int columns, int current) {
  11661. int class_ = -1;
  11662. int64 class_tmp;
  11663. if (ISDIGIT(str[0][0])) {
  11664. class_ = atoi(str[0]);
  11665. } else {
  11666. if (!script_get_constant(str[0], &class_tmp)) {
  11667. ShowError("pc_readdb_job_noenter_map: Invalid job %s specified.\n", str[0]);
  11668. return false;
  11669. }
  11670. class_ = static_cast<int>(class_tmp);
  11671. }
  11672. if (!pcdb_checkid(class_)) {
  11673. ShowError("pc_readdb_job_noenter_map: Invalid job %d specified.\n", class_);
  11674. return false;
  11675. }
  11676. std::shared_ptr<s_job_info> job = job_db.find(class_);
  11677. if (job == nullptr) {
  11678. ShowError("pc_readdb_job_noenter_map: Job %d data not initialized.\n", class_);
  11679. return false;
  11680. }
  11681. job->noenter_map.zone = atoi(str[1]);
  11682. job->noenter_map.group_lv = atoi(str[2]);
  11683. return true;
  11684. }
  11685. const std::string PlayerStatPointDatabase::getDefaultLocation() {
  11686. return std::string(db_path) + "/statpoint.yml";
  11687. }
  11688. uint64 PlayerStatPointDatabase::parseBodyNode(const ryml::NodeRef& node) {
  11689. if (!this->nodesExist(node, { "Level", "Points" })) {
  11690. return 0;
  11691. }
  11692. uint16 level;
  11693. if (!this->asUInt16(node, "Level", level))
  11694. return 0;
  11695. uint32 point;
  11696. if (!this->asUInt32(node, "Points", point))
  11697. return 0;
  11698. if (level == 0) {
  11699. this->invalidWarning(node["Level"], "The minimum level is 1.\n");
  11700. return 0;
  11701. }
  11702. if (level > MAX_LEVEL) {
  11703. this->invalidWarning(node["Level"], "Level %d exceeds maximum BaseLevel %d, skipping.\n", level, MAX_LEVEL);
  11704. return 0;
  11705. }
  11706. std::shared_ptr<s_statpoint_entry> entry = this->find( level );
  11707. bool exists = entry != nullptr;
  11708. if( !exists ){
  11709. entry = std::make_shared<s_statpoint_entry>();
  11710. entry->level = level;
  11711. entry->statpoints = point;
  11712. }
  11713. if( this->nodeExists( node, "TraitPoints" ) ){
  11714. uint32 traitpoints;
  11715. if( !this->asUInt32( node, "TraitPoints", traitpoints ) ){
  11716. return 0;
  11717. }
  11718. entry->traitpoints = traitpoints;
  11719. }else{
  11720. if( !exists ){
  11721. entry->traitpoints = 0;
  11722. }
  11723. }
  11724. if( !exists ){
  11725. this->put( level, entry );
  11726. }
  11727. return 1;
  11728. }
  11729. /**
  11730. * Generate the remaining parts of the db if necessary.
  11731. */
  11732. void PlayerStatPointDatabase::loadingFinished(){
  11733. const uint16 trait_start_level = 200;
  11734. std::shared_ptr<s_statpoint_entry> level_one = this->find( 1 );
  11735. if( level_one == nullptr ){
  11736. if( battle_config.use_statpoint_table ){
  11737. ShowError( "Missing status points for Level 1\n" );
  11738. }
  11739. level_one = std::make_shared<s_statpoint_entry>();
  11740. level_one->level = 1;
  11741. level_one->statpoints = start_status_points;
  11742. level_one->traitpoints = 0;
  11743. this->put( 1, level_one );
  11744. }else if( battle_config.use_statpoint_table ){
  11745. if( level_one->statpoints != start_status_points ){
  11746. ShowError( "Status points for Level 1 (=%u) do not match inter_athena.conf value (=%u).\n", level_one->statpoints, start_status_points );
  11747. level_one->statpoints = start_status_points;
  11748. }
  11749. }else{
  11750. level_one->statpoints = start_status_points;
  11751. level_one->traitpoints = 0;
  11752. }
  11753. std::shared_ptr<s_statpoint_entry> last_level = level_one;
  11754. for( uint16 level = 2; level <= MAX_LEVEL; level++ ){
  11755. std::shared_ptr<s_statpoint_entry> entry = this->find( level );
  11756. bool exists = entry != nullptr;
  11757. if( !exists ){
  11758. entry = std::make_shared<s_statpoint_entry>();
  11759. entry->level = level;
  11760. this->put( level, entry );
  11761. }
  11762. if( !battle_config.use_statpoint_table || !exists ){
  11763. if( battle_config.use_statpoint_table ){
  11764. ShowError("Missing status points for Level %hu\n", level);
  11765. }
  11766. if( level <= trait_start_level ){
  11767. entry->statpoints = last_level->statpoints + ( ( level - 1 + 15 ) / 5 );
  11768. }else{
  11769. entry->statpoints = last_level->statpoints;
  11770. }
  11771. }
  11772. if( !battle_config.use_traitpoint_table || !exists ){
  11773. if( battle_config.use_traitpoint_table && level > trait_start_level ){
  11774. ShowError( "Missing trait points for Level %hu\n", level );
  11775. }
  11776. if( level > trait_start_level ){
  11777. entry->traitpoints = ( level - trait_start_level ) * 3 + ( level - trait_start_level ) / 5 * 4;
  11778. }else{
  11779. entry->traitpoints = 0;
  11780. }
  11781. }
  11782. // Store it for next iteration
  11783. last_level = entry;
  11784. }
  11785. }
  11786. /*==========================================
  11787. * pc DB reading.
  11788. * job_stats.yml - Job values
  11789. * skill_tree.txt - skill tree for every class
  11790. * attr_fix.yml - elemental adjustment table
  11791. *------------------------------------------*/
  11792. void pc_readdb(void) {
  11793. int i, s = 1;
  11794. const char* dbsubpath[] = {
  11795. "",
  11796. "/" DBIMPORT,
  11797. //add other path here
  11798. };
  11799. //reset
  11800. job_db.clear(); // job_info table
  11801. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  11802. penalty_db.load();
  11803. #endif
  11804. statpoint_db.clear();
  11805. job_db.load();
  11806. for(i=0; i<ARRAYLENGTH(dbsubpath); i++){
  11807. uint8 n1 = (uint8)(strlen(db_path)+strlen(dbsubpath[i])+1);
  11808. uint8 n2 = (uint8)(strlen(db_path)+strlen(DBPATH)+strlen(dbsubpath[i])+1);
  11809. char* dbsubpath1 = (char*)aMalloc(n1+1);
  11810. char* dbsubpath2 = (char*)aMalloc(n2+1);
  11811. if(i==0) {
  11812. safesnprintf(dbsubpath1,n1,"%s%s",db_path,dbsubpath[i]);
  11813. safesnprintf(dbsubpath2,n2,"%s/%s%s",db_path,DBPATH,dbsubpath[i]);
  11814. }
  11815. else {
  11816. safesnprintf(dbsubpath1,n1,"%s%s",db_path,dbsubpath[i]);
  11817. safesnprintf(dbsubpath2,n1,"%s%s",db_path,dbsubpath[i]);
  11818. }
  11819. sv_readdb(dbsubpath2, "job_noenter_map.txt", ',', 3, 3, CLASS_COUNT, &pc_readdb_job_noenter_map, i > 0);
  11820. aFree(dbsubpath1);
  11821. aFree(dbsubpath2);
  11822. }
  11823. // Reset and read skilltree - needs to be read after pc_readdb_job_exp to get max base and job levels
  11824. skill_tree_db.reload();
  11825. statpoint_db.load();
  11826. }
  11827. // Read MOTD on startup. [Valaris]
  11828. int pc_read_motd(void)
  11829. {
  11830. FILE* fp;
  11831. // clear old MOTD
  11832. memset(motd_text, 0, sizeof(motd_text));
  11833. // read current MOTD
  11834. if( ( fp = fopen(motd_txt, "r") ) != NULL )
  11835. {
  11836. unsigned int entries = 0;
  11837. char buf[CHAT_SIZE_MAX];
  11838. while( entries < MOTD_LINE_SIZE && fgets(buf, CHAT_SIZE_MAX, fp) )
  11839. {
  11840. unsigned int lines = 0;
  11841. size_t len;
  11842. lines++;
  11843. if( buf[0] == '/' && buf[1] == '/' )
  11844. continue;
  11845. len = strlen(buf);
  11846. while( len && ( buf[len-1] == '\r' || buf[len-1] == '\n' ) ) // strip trailing EOL characters
  11847. len--;
  11848. if( len ) {
  11849. char * ptr;
  11850. buf[len] = 0;
  11851. if( ( ptr = strstr(buf, " :") ) != NULL && ptr-buf >= NAME_LENGTH ) // crashes newer clients
  11852. 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);
  11853. }
  11854. else {// empty line
  11855. buf[0] = ' ';
  11856. buf[1] = 0;
  11857. }
  11858. safestrncpy(motd_text[entries], buf, CHAT_SIZE_MAX);
  11859. entries++;
  11860. }
  11861. fclose(fp);
  11862. ShowStatus("Done reading '" CL_WHITE "%u" CL_RESET "' entries in '" CL_WHITE "%s" CL_RESET "'.\n", entries, motd_txt);
  11863. }
  11864. else
  11865. ShowWarning("File '" CL_WHITE "%s" CL_RESET "' not found.\n", motd_txt);
  11866. return 0;
  11867. }
  11868. void pc_itemcd_do(struct map_session_data *sd, bool load) {
  11869. int i,cursor = 0;
  11870. struct item_cd* cd = NULL;
  11871. if( load ) {
  11872. if( !(cd = (struct item_cd*)idb_get(itemcd_db, sd->status.char_id)) ) {
  11873. // no item cooldown is associated with this character
  11874. return;
  11875. }
  11876. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  11877. if( cd->nameid[i] && DIFF_TICK(gettick(),cd->tick[i]) < 0 ) {
  11878. sd->item_delay[cursor].tick = cd->tick[i];
  11879. sd->item_delay[cursor].nameid = cd->nameid[i];
  11880. cursor++;
  11881. }
  11882. }
  11883. idb_remove(itemcd_db,sd->status.char_id);
  11884. } else {
  11885. if( !(cd = (struct item_cd*)idb_get(itemcd_db,sd->status.char_id)) ) {
  11886. // create a new skill cooldown object for map storage
  11887. CREATE( cd, struct item_cd, 1 );
  11888. idb_put( itemcd_db, sd->status.char_id, cd );
  11889. }
  11890. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  11891. if( sd->item_delay[i].nameid && DIFF_TICK(gettick(),sd->item_delay[i].tick) < 0 ) {
  11892. cd->tick[cursor] = sd->item_delay[i].tick;
  11893. cd->nameid[cursor] = sd->item_delay[i].nameid;
  11894. cursor++;
  11895. }
  11896. }
  11897. }
  11898. return;
  11899. }
  11900. /**
  11901. * Add item delay to player's item delay data
  11902. * @param sd Player
  11903. * @param id Item data
  11904. * @param tick Current tick
  11905. * @param n Item index in inventory
  11906. * @return 0: No delay, can consume item.
  11907. * 1: Has delay, cancel consumption.
  11908. **/
  11909. uint8 pc_itemcd_add(struct map_session_data *sd, struct item_data *id, t_tick tick, unsigned short n) {
  11910. int i;
  11911. ARR_FIND(0, MAX_ITEMDELAYS, i, sd->item_delay[i].nameid == id->nameid );
  11912. if( i == MAX_ITEMDELAYS ) /* item not found. try first empty now */
  11913. ARR_FIND(0, MAX_ITEMDELAYS, i, !sd->item_delay[i].nameid );
  11914. if( i < MAX_ITEMDELAYS ) {
  11915. if( sd->item_delay[i].nameid ) {// found
  11916. if( DIFF_TICK(sd->item_delay[i].tick, tick) > 0 ) {
  11917. t_tick e_tick = DIFF_TICK(sd->item_delay[i].tick, tick)/1000;
  11918. char e_msg[CHAT_SIZE_MAX];
  11919. if( e_tick > 99 )
  11920. sprintf(e_msg,msg_txt(sd,379), // Item Failed. [%s] is cooling down. Wait %.1f minutes.
  11921. itemdb_ename(sd->item_delay[i].nameid), (double)e_tick / 60);
  11922. else
  11923. sprintf(e_msg,msg_txt(sd,380), // Item Failed. [%s] is cooling down. Wait %d seconds.
  11924. itemdb_ename(sd->item_delay[i].nameid), e_tick+1);
  11925. clif_messagecolor(&sd->bl,color_table[COLOR_YELLOW],e_msg,false,SELF);
  11926. return 1; // Delay has not expired yet
  11927. }
  11928. } else {// not yet used item (all slots are initially empty)
  11929. sd->item_delay[i].nameid = id->nameid;
  11930. }
  11931. if( !(id->nameid == ITEMID_REINS_OF_MOUNT && sd->sc.option&(OPTION_WUGRIDER|OPTION_RIDING|OPTION_DRAGON|OPTION_MADOGEAR)) )
  11932. sd->item_delay[i].tick = tick + sd->inventory_data[n]->delay.duration;
  11933. } else {// should not happen
  11934. ShowError("pc_itemcd_add: Exceeded item delay array capacity! (nameid=%u, char_id=%d)\n", id->nameid, sd->status.char_id);
  11935. }
  11936. //clean up used delays so we can give room for more
  11937. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  11938. if( DIFF_TICK(sd->item_delay[i].tick, tick) <= 0 ) {
  11939. sd->item_delay[i].tick = 0;
  11940. sd->item_delay[i].nameid = 0;
  11941. }
  11942. }
  11943. return 0;
  11944. }
  11945. /**
  11946. * Check if player has delay to reuse item
  11947. * @param sd Player
  11948. * @param id Item data
  11949. * @param tick Current tick
  11950. * @param n Item index in inventory
  11951. * @return 0: No delay, can consume item.
  11952. * 1: Has delay, cancel consumption.
  11953. **/
  11954. uint8 pc_itemcd_check(struct map_session_data *sd, struct item_data *id, t_tick tick, unsigned short n) {
  11955. struct status_change *sc = NULL;
  11956. nullpo_retr(0, sd);
  11957. nullpo_retr(0, id);
  11958. // Do normal delay assignment
  11959. if (id->delay.sc <= SC_NONE || id->delay.sc >= SC_MAX || !(sc = &sd->sc))
  11960. return pc_itemcd_add(sd, id, tick, n);
  11961. // Send reply of delay remains
  11962. if (sc->data[id->delay.sc]) {
  11963. const struct TimerData *timer = get_timer(sc->data[id->delay.sc]->timer);
  11964. clif_msg_value(sd, ITEM_REUSE_LIMIT, (int)(timer ? DIFF_TICK(timer->tick, tick) / 1000 : 99));
  11965. return 1;
  11966. }
  11967. sc_start(&sd->bl, &sd->bl, id->delay.sc, 100, id->nameid, id->delay.duration);
  11968. return 0;
  11969. }
  11970. /**
  11971. * Clear the dmglog data from player
  11972. * @param sd
  11973. * @param md
  11974. **/
  11975. static void pc_clear_log_damage_sub(uint32 char_id, struct mob_data *md)
  11976. {
  11977. uint8 i;
  11978. ARR_FIND(0,DAMAGELOG_SIZE,i,md->dmglog[i].id == char_id);
  11979. if (i < DAMAGELOG_SIZE) {
  11980. md->dmglog[i].id = 0;
  11981. md->dmglog[i].dmg = 0;
  11982. md->dmglog[i].flag = 0;
  11983. }
  11984. }
  11985. /**
  11986. * Add log to player's dmglog
  11987. * @param sd
  11988. * @param id Monster's GID
  11989. **/
  11990. void pc_damage_log_add(struct map_session_data *sd, int id)
  11991. {
  11992. uint8 i = 0;
  11993. if (!sd || !id)
  11994. return;
  11995. //Only store new data, don't need to renew the old one with same id
  11996. ARR_FIND(0, DAMAGELOG_SIZE_PC, i, sd->dmglog[i] == id);
  11997. if (i < DAMAGELOG_SIZE_PC)
  11998. return;
  11999. for (i = 0; i < DAMAGELOG_SIZE_PC; i++) {
  12000. if (sd->dmglog[i] == 0) {
  12001. sd->dmglog[i] = id;
  12002. return;
  12003. }
  12004. }
  12005. }
  12006. /**
  12007. * Clear dmglog data from player
  12008. * @param sd
  12009. * @param id Monster's id
  12010. **/
  12011. void pc_damage_log_clear(struct map_session_data *sd, int id)
  12012. {
  12013. uint8 i;
  12014. struct mob_data *md = NULL;
  12015. if (!sd)
  12016. return;
  12017. if (!id) {
  12018. for (i = 0; i < DAMAGELOG_SIZE_PC; i++) {
  12019. if( !sd->dmglog[i] ) //skip the empty value
  12020. continue;
  12021. if ((md = map_id2md(sd->dmglog[i])))
  12022. pc_clear_log_damage_sub(sd->status.char_id,md);
  12023. sd->dmglog[i] = 0;
  12024. }
  12025. }
  12026. else {
  12027. if ((md = map_id2md(id)))
  12028. pc_clear_log_damage_sub(sd->status.char_id,md);
  12029. ARR_FIND(0,DAMAGELOG_SIZE_PC,i,sd->dmglog[i] == id); // find the id position
  12030. if (i < DAMAGELOG_SIZE_PC)
  12031. sd->dmglog[i] = 0;
  12032. }
  12033. }
  12034. /**
  12035. * Status change data arrived from char-server
  12036. * @param sd: Player data
  12037. */
  12038. void pc_scdata_received(struct map_session_data *sd) {
  12039. pc_inventory_rentals(sd); // Needed here to remove rentals that have Status Changes after chrif_load_scdata has finished
  12040. clif_weight_limit( sd );
  12041. if( pc_has_permission( sd, PC_PERM_ATTENDANCE ) && pc_attendance_enabled() && !pc_attendance_rewarded_today( sd ) ){
  12042. clif_ui_open( sd, OUT_UI_ATTENDANCE, pc_attendance_counter( sd ) );
  12043. }
  12044. sd->state.pc_loaded = true;
  12045. if (sd->state.connect_new == 0 && sd->fd) { // Character already loaded map! Gotta trigger LoadEndAck manually.
  12046. sd->state.connect_new = 1;
  12047. clif_parse_LoadEndAck(sd->fd, sd);
  12048. }
  12049. if (pc_iscarton(sd)) {
  12050. sd->cart_weight_max = 0; // Force a client refesh
  12051. status_calc_cart_weight(sd, (e_status_calc_weight_opt)(CALCWT_ITEM|CALCWT_MAXBONUS|CALCWT_CARTSTATE));
  12052. }
  12053. if (sd->sc.data[SC_SOULENERGY])
  12054. sd->soulball = sd->sc.data[SC_SOULENERGY]->val1;
  12055. }
  12056. /**
  12057. * Check player account expiration time and rental item expirations
  12058. * @param sd: Player data
  12059. */
  12060. void pc_check_expiration(struct map_session_data *sd) {
  12061. #ifndef ENABLE_SC_SAVING
  12062. pc_inventory_rentals(sd); // Check here if Status Change saving is disabled
  12063. #endif
  12064. if (sd->expiration_time != 0) { //Don't display if it's unlimited or unknow value
  12065. time_t exp_time = sd->expiration_time;
  12066. char tmpstr[1024];
  12067. strftime(tmpstr,sizeof(tmpstr) - 1,msg_txt(sd,501),localtime(&exp_time)); // "Your account time limit is: %d-%m-%Y %H:%M:%S."
  12068. clif_wis_message(sd,wisp_server_name,tmpstr,strlen(tmpstr) + 1,0);
  12069. pc_expire_check(sd);
  12070. }
  12071. }
  12072. TIMER_FUNC(pc_expiration_timer){
  12073. struct map_session_data *sd = map_id2sd(id);
  12074. if( !sd ) return 0;
  12075. sd->expiration_tid = INVALID_TIMER;
  12076. if( sd->fd )
  12077. clif_authfail_fd(sd->fd,10);
  12078. map_quit(sd);
  12079. return 0;
  12080. }
  12081. TIMER_FUNC(pc_autotrade_timer){
  12082. struct map_session_data *sd = map_id2sd(id);
  12083. if (!sd)
  12084. return 0;
  12085. sd->autotrade_tid = INVALID_TIMER;
  12086. if (sd->state.autotrade&2)
  12087. vending_reopen(sd);
  12088. if (sd->state.autotrade&4)
  12089. buyingstore_reopen(sd);
  12090. if (!sd->vender_id && !sd->buyer_id) {
  12091. sd->state.autotrade = 0;
  12092. map_quit(sd);
  12093. }
  12094. return 0;
  12095. }
  12096. /* this timer exists only when a character with a expire timer > 24h is online */
  12097. /* it loops thru online players once an hour to check whether a new < 24h is available */
  12098. TIMER_FUNC(pc_global_expiration_timer){
  12099. struct s_mapiterator* iter;
  12100. struct map_session_data* sd;
  12101. iter = mapit_getallusers();
  12102. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  12103. if( sd->expiration_time )
  12104. pc_expire_check(sd);
  12105. mapit_free(iter);
  12106. return 0;
  12107. }
  12108. void pc_expire_check(struct map_session_data *sd) {
  12109. /* ongoing timer */
  12110. if( sd->expiration_tid != INVALID_TIMER )
  12111. return;
  12112. /* not within the next 24h, enable the global check */
  12113. if( sd->expiration_time > (time(NULL) + ((60 * 60) * 24)) ) {
  12114. /* global check not running, enable */
  12115. if( pc_expiration_tid == INVALID_TIMER ) /* Starts in 1h, repeats every hour */
  12116. pc_expiration_tid = add_timer_interval(gettick() + ((1000 * 60) * 60), pc_global_expiration_timer, 0, 0, ((1000 * 60) * 60));
  12117. return;
  12118. }
  12119. sd->expiration_tid = add_timer(gettick() + (unsigned int)(sd->expiration_time - time(NULL)) * 1000, pc_expiration_timer, sd->bl.id, 0);
  12120. }
  12121. /**
  12122. * Deposit some money to bank
  12123. * @param sd
  12124. * @param money Amount of money to deposit
  12125. **/
  12126. enum e_BANKING_DEPOSIT_ACK pc_bank_deposit(struct map_session_data *sd, int money) {
  12127. unsigned int limit_check = money + sd->bank_vault;
  12128. if( money <= 0 || limit_check > MAX_BANK_ZENY ) {
  12129. return BDA_OVERFLOW;
  12130. } else if ( money > sd->status.zeny ) {
  12131. return BDA_NO_MONEY;
  12132. }
  12133. if( pc_payzeny(sd,money, LOG_TYPE_BANK, NULL) )
  12134. return BDA_NO_MONEY;
  12135. sd->bank_vault += money;
  12136. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  12137. if( save_settings&CHARSAVE_BANK )
  12138. chrif_save(sd, CSAVE_NORMAL);
  12139. return BDA_SUCCESS;
  12140. }
  12141. /**
  12142. * Withdraw money from bank
  12143. * @param sd
  12144. * @param money Amount of money that will be withdrawn
  12145. **/
  12146. enum e_BANKING_WITHDRAW_ACK pc_bank_withdraw(struct map_session_data *sd, int money) {
  12147. unsigned int limit_check = money + sd->status.zeny;
  12148. if( money <= 0 ) {
  12149. return BWA_UNKNOWN_ERROR;
  12150. } else if ( money > sd->bank_vault ) {
  12151. return BWA_NO_MONEY;
  12152. } else if ( limit_check > MAX_ZENY ) {
  12153. /* no official response for this scenario exists. */
  12154. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,1495),false,SELF); //You can't withdraw that much money
  12155. return BWA_UNKNOWN_ERROR;
  12156. }
  12157. if( pc_getzeny(sd,money, LOG_TYPE_BANK, NULL) )
  12158. return BWA_NO_MONEY;
  12159. sd->bank_vault -= money;
  12160. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  12161. if( save_settings&CHARSAVE_BANK )
  12162. chrif_save(sd, CSAVE_NORMAL);
  12163. return BWA_SUCCESS;
  12164. }
  12165. /**
  12166. * Clear Crimson Marker data from caster
  12167. * @param sd: Player
  12168. **/
  12169. void pc_crimson_marker_clear(struct map_session_data *sd) {
  12170. uint8 i;
  12171. if (!sd)
  12172. return;
  12173. for (i = 0; i < MAX_SKILL_CRIMSON_MARKER; i++) {
  12174. struct block_list *bl = NULL;
  12175. if (sd->c_marker[i] && (bl = map_id2bl(sd->c_marker[i])))
  12176. status_change_end(bl,SC_C_MARKER,INVALID_TIMER);
  12177. sd->c_marker[i] = 0;
  12178. }
  12179. }
  12180. /**
  12181. * Show version to player
  12182. * @param sd: Player
  12183. **/
  12184. void pc_show_version(struct map_session_data *sd) {
  12185. const char* svn = get_svn_revision();
  12186. char buf[CHAT_SIZE_MAX];
  12187. if( svn[0] != UNKNOWN_VERSION )
  12188. sprintf(buf,msg_txt(sd,1295),"SVN: r",svn); //rAthena Version SVN: r%s
  12189. else {
  12190. const char* git = get_git_hash();
  12191. if( git[0] != UNKNOWN_VERSION )
  12192. sprintf(buf,msg_txt(sd,1295),"Git Hash: ",git); //rAthena Version Git Hash: %s
  12193. else
  12194. sprintf(buf,"%s",msg_txt(sd,1296)); //Cannot determine SVN/Git version.
  12195. }
  12196. clif_displaymessage(sd->fd,buf);
  12197. }
  12198. /**
  12199. * Run bonus_script on player
  12200. * @param sd
  12201. * @author [Cydh]
  12202. **/
  12203. void pc_bonus_script(struct map_session_data *sd) {
  12204. t_tick now = gettick();
  12205. struct linkdb_node *node = NULL, *next = NULL;
  12206. if (!sd || !(node = sd->bonus_script.head))
  12207. return;
  12208. while (node) {
  12209. struct s_bonus_script_entry *entry = NULL;
  12210. next = node->next;
  12211. if ((entry = (struct s_bonus_script_entry *)node->data)) {
  12212. // Only start timer for new bonus_script
  12213. if (entry->tid == INVALID_TIMER) {
  12214. if (entry->icon != EFST_BLANK) // Gives status icon if exist
  12215. clif_status_change(&sd->bl, entry->icon, 1, entry->tick, 1, 0, 0);
  12216. entry->tick += now;
  12217. entry->tid = add_timer(entry->tick, pc_bonus_script_timer, sd->bl.id, (intptr_t)entry);
  12218. }
  12219. if (entry->script)
  12220. run_script(entry->script, 0, sd->bl.id, 0);
  12221. else
  12222. ShowError("pc_bonus_script: The script has been removed somewhere. \"%s\"\n", StringBuf_Value(entry->script_buf));
  12223. }
  12224. node = next;
  12225. }
  12226. }
  12227. /**
  12228. * Add bonus_script to player
  12229. * @param sd Player
  12230. * @param script_str Script string
  12231. * @param dur Duration in ms
  12232. * @param icon EFST
  12233. * @param flag Flags @see enum e_bonus_script_flags
  12234. * @param type 0 - None, 1 - Buff, 2 - Debuff
  12235. * @return New created entry pointer or NULL if failed or NULL if duplicate fail
  12236. * @author [Cydh]
  12237. **/
  12238. struct s_bonus_script_entry *pc_bonus_script_add(struct map_session_data *sd, const char *script_str, t_tick dur, enum efst_type icon, uint16 flag, uint8 type) {
  12239. struct script_code *script = NULL;
  12240. struct linkdb_node *node = NULL;
  12241. struct s_bonus_script_entry *entry = NULL;
  12242. if (!sd)
  12243. return NULL;
  12244. if (!(script = parse_script(script_str, "bonus_script", 0, SCRIPT_IGNORE_EXTERNAL_BRACKETS))) {
  12245. ShowError("pc_bonus_script_add: Failed to parse script '%s' (CID:%d).\n", script_str, sd->status.char_id);
  12246. return NULL;
  12247. }
  12248. // Duplication checks
  12249. if ((node = sd->bonus_script.head)) {
  12250. while (node) {
  12251. entry = (struct s_bonus_script_entry *)node->data;
  12252. if (strcmpi(script_str, StringBuf_Value(entry->script_buf)) == 0) {
  12253. t_tick newdur = gettick() + dur;
  12254. if (flag&BSF_FORCE_REPLACE && entry->tick < newdur) { // Change duration
  12255. sett_tickimer(entry->tid, newdur);
  12256. script_free_code(script);
  12257. return NULL;
  12258. }
  12259. else if (flag&BSF_FORCE_DUPLICATE) // Allow duplicate
  12260. break;
  12261. else { // No duplicate bonus
  12262. script_free_code(script);
  12263. return NULL;
  12264. }
  12265. }
  12266. node = node->next;
  12267. }
  12268. }
  12269. CREATE(entry, struct s_bonus_script_entry, 1);
  12270. entry->script_buf = StringBuf_Malloc();
  12271. StringBuf_AppendStr(entry->script_buf, script_str);
  12272. entry->tid = INVALID_TIMER;
  12273. entry->flag = flag;
  12274. entry->icon = icon;
  12275. entry->tick = dur; // Use duration first, on run change to expire time
  12276. entry->type = type;
  12277. entry->script = script;
  12278. sd->bonus_script.count++;
  12279. return entry;
  12280. }
  12281. /**
  12282. * Remove bonus_script data from player
  12283. * @param sd: Target player
  12284. * @param list: Bonus script entry from player
  12285. * @author [Cydh]
  12286. **/
  12287. void pc_bonus_script_free_entry(struct map_session_data *sd, struct s_bonus_script_entry *entry) {
  12288. if (entry->tid != INVALID_TIMER)
  12289. delete_timer(entry->tid, pc_bonus_script_timer);
  12290. if (entry->script)
  12291. script_free_code(entry->script);
  12292. if (entry->script_buf)
  12293. StringBuf_Free(entry->script_buf);
  12294. if (sd) {
  12295. if (entry->icon != EFST_BLANK)
  12296. clif_status_load(&sd->bl, entry->icon, 0);
  12297. if (sd->bonus_script.count > 0)
  12298. sd->bonus_script.count--;
  12299. }
  12300. aFree(entry);
  12301. }
  12302. /**
  12303. * Do final process if no entry left
  12304. * @param sd
  12305. **/
  12306. static void inline pc_bonus_script_check_final(struct map_session_data *sd) {
  12307. if (sd->bonus_script.count == 0) {
  12308. if (sd->bonus_script.head && sd->bonus_script.head->data)
  12309. pc_bonus_script_free_entry(sd, (struct s_bonus_script_entry *)sd->bonus_script.head->data);
  12310. linkdb_final(&sd->bonus_script.head);
  12311. }
  12312. }
  12313. /**
  12314. * Timer for bonus_script
  12315. * @param tid
  12316. * @param tick
  12317. * @param id
  12318. * @param data
  12319. * @author [Cydh]
  12320. **/
  12321. TIMER_FUNC(pc_bonus_script_timer){
  12322. struct map_session_data *sd;
  12323. struct s_bonus_script_entry *entry = (struct s_bonus_script_entry *)data;
  12324. sd = map_id2sd(id);
  12325. if (!sd) {
  12326. ShowError("pc_bonus_script_timer: Null pointer id: %d tid: %d\n", id, tid);
  12327. return 0;
  12328. }
  12329. if (tid == INVALID_TIMER)
  12330. return 0;
  12331. if (!sd->bonus_script.head || entry == NULL) {
  12332. ShowError("pc_bonus_script_timer: Invalid entry pointer %p!\n", entry);
  12333. return 0;
  12334. }
  12335. linkdb_erase(&sd->bonus_script.head, (void *)((intptr_t)entry));
  12336. pc_bonus_script_free_entry(sd, entry);
  12337. pc_bonus_script_check_final(sd);
  12338. status_calc_pc(sd,SCO_NONE);
  12339. return 0;
  12340. }
  12341. /**
  12342. * Check then clear all active timer(s) of bonus_script data from player based on reason
  12343. * @param sd: Target player
  12344. * @param flag: Reason to remove the bonus_script. e_bonus_script_flags or e_bonus_script_types
  12345. * @author [Cydh]
  12346. **/
  12347. void pc_bonus_script_clear(struct map_session_data *sd, uint32 flag) {
  12348. struct linkdb_node *node = NULL;
  12349. uint16 count = 0;
  12350. if (!sd || !(node = sd->bonus_script.head))
  12351. return;
  12352. while (node) {
  12353. struct linkdb_node *next = node->next;
  12354. struct s_bonus_script_entry *entry = (struct s_bonus_script_entry *)node->data;
  12355. if (entry && (
  12356. (flag == BSF_PERMANENT) || // Remove all with permanent bonus
  12357. (!flag && !(entry->flag&BSF_PERMANENT)) || // Remove all WITHOUT permanent bonus
  12358. (flag&entry->flag) || // Matched flag
  12359. (flag&BSF_REM_BUFF && entry->type == 1) || // Remove buff
  12360. (flag&BSF_REM_DEBUFF && entry->type == 2) // Remove debuff
  12361. )
  12362. )
  12363. {
  12364. linkdb_erase(&sd->bonus_script.head, (void *)((intptr_t)entry));
  12365. pc_bonus_script_free_entry(sd, entry);
  12366. count++;
  12367. }
  12368. node = next;
  12369. }
  12370. pc_bonus_script_check_final(sd);
  12371. if (count && !(flag&BSF_REM_ON_LOGOUT)) //Don't need to do this if log out
  12372. status_calc_pc(sd,SCO_NONE);
  12373. }
  12374. /** [Cydh]
  12375. * Gives/removes SC_BASILICA when player steps in/out the cell with 'cell_basilica'
  12376. * @param sd: Target player
  12377. */
  12378. void pc_cell_basilica(struct map_session_data *sd) {
  12379. nullpo_retv(sd);
  12380. #ifdef RENEWAL
  12381. enum sc_type type = SC_BASILICA_CELL;
  12382. #else
  12383. enum sc_type type = SC_BASILICA;
  12384. #endif
  12385. if (!map_getcell(sd->bl.m,sd->bl.x,sd->bl.y,CELL_CHKBASILICA)) {
  12386. if (sd->sc.data[type])
  12387. status_change_end(&sd->bl, type,INVALID_TIMER);
  12388. }
  12389. else if (!sd->sc.data[type])
  12390. sc_start(&sd->bl,&sd->bl, type,100,0,INFINITE_TICK);
  12391. }
  12392. /** [Cydh]
  12393. * Get maximum specified parameter for specified class
  12394. * @param sd: Player data
  12395. * @param param: Max parameter to check
  12396. * @return max_param
  12397. */
  12398. uint16 pc_maxparameter(struct map_session_data *sd, e_params param) {
  12399. nullpo_retr(0, sd);
  12400. std::shared_ptr<s_job_info> job = job_db.find(pc_mapid2jobid(sd->class_,sd->status.sex));
  12401. if( job == nullptr || param == PARAM_MAX ){
  12402. return 0;
  12403. }
  12404. return job->max_param[param];
  12405. }
  12406. /**
  12407. * Get max ASPD for player based on Class
  12408. * @param sd Player
  12409. * @return ASPD
  12410. */
  12411. short pc_maxaspd(struct map_session_data *sd) {
  12412. nullpo_ret(sd);
  12413. return (( sd->class_&JOBL_THIRD) ? battle_config.max_third_aspd : (
  12414. ((sd->class_&MAPID_UPPERMASK) == MAPID_KAGEROUOBORO || (sd->class_&MAPID_UPPERMASK) == MAPID_REBELLION) ? battle_config.max_extended_aspd : (
  12415. (sd->class_&MAPID_BASEMASK) == MAPID_SUMMONER) ? battle_config.max_summoner_aspd :
  12416. battle_config.max_aspd ));
  12417. }
  12418. /**
  12419. * Calculates total item-group related bonuses for the given item
  12420. * @param sd Player
  12421. * @param nameid Item ID
  12422. * @return Heal rate
  12423. **/
  12424. short pc_get_itemgroup_bonus(struct map_session_data* sd, t_itemid nameid, std::vector<s_item_bonus>& bonuses) {
  12425. if (bonuses.empty())
  12426. return 0;
  12427. short bonus = 0;
  12428. for (const auto &it : bonuses) {
  12429. uint16 group_id = it.id;
  12430. if (group_id == 0)
  12431. continue;
  12432. if (itemdb_group.item_exists(group_id, nameid))
  12433. bonus += it.val;
  12434. }
  12435. return bonus;
  12436. }
  12437. /**
  12438. * Calculates total item-group related bonuses for the given item group
  12439. * @param sd Player
  12440. * @param group_id Item Group ID
  12441. * @return Heal rate
  12442. **/
  12443. short pc_get_itemgroup_bonus_group(struct map_session_data* sd, uint16 group_id, std::vector<s_item_bonus>& bonuses) {
  12444. if (bonuses.empty())
  12445. return 0;
  12446. for (const auto &it : bonuses) {
  12447. if (it.id == group_id)
  12448. return it.val;
  12449. }
  12450. return 0;
  12451. }
  12452. /**
  12453. * Check if player's equip index in same specified position, like for 2-Handed weapon & Heagdear (inc. costume)
  12454. * @param eqi Item EQI of enum equip_index
  12455. * @param *equip_index Player's equip_index[]
  12456. * @param index Known index item in inventory from sd->equip_index[] to compare with specified EQI in *equip_index
  12457. * @return True if item in same inventory index, False if doesn't
  12458. */
  12459. bool pc_is_same_equip_index(enum equip_index eqi, short *equip_index, short index) {
  12460. if (index < 0 || index >= MAX_INVENTORY)
  12461. return true;
  12462. // Dual weapon checks
  12463. if (eqi == EQI_HAND_R && equip_index[EQI_HAND_L] == index)
  12464. return true;
  12465. // Headgear with Mid & Low location
  12466. if (eqi == EQI_HEAD_MID && equip_index[EQI_HEAD_LOW] == index)
  12467. return true;
  12468. // Headgear with Top & Mid or Low location
  12469. if (eqi == EQI_HEAD_TOP && (equip_index[EQI_HEAD_MID] == index || equip_index[EQI_HEAD_LOW] == index))
  12470. return true;
  12471. // Headgear with Mid & Low location
  12472. if (eqi == EQI_COSTUME_HEAD_MID && equip_index[EQI_COSTUME_HEAD_LOW] == index)
  12473. return true;
  12474. // Headgear with Top & Mid or Low location
  12475. if (eqi == EQI_COSTUME_HEAD_TOP && (equip_index[EQI_COSTUME_HEAD_MID] == index || equip_index[EQI_COSTUME_HEAD_LOW] == index))
  12476. return true;
  12477. return false;
  12478. }
  12479. /**
  12480. * Generate Unique item ID for player
  12481. * @param sd : Player
  12482. * @return A generated Unique item ID
  12483. */
  12484. uint64 pc_generate_unique_id(struct map_session_data *sd) {
  12485. nullpo_ret(sd);
  12486. return ((uint64)sd->status.char_id << 32) | sd->status.uniqueitem_counter++;
  12487. }
  12488. /**
  12489. * Validating skill from player after logged on
  12490. * @param sd
  12491. **/
  12492. void pc_validate_skill(struct map_session_data *sd) {
  12493. if (sd) {
  12494. uint16 i = 0, count = 0;
  12495. struct s_skill tmp_skills[MAX_SKILL] = {{ 0 }};
  12496. memcpy(tmp_skills, sd->status.skill, sizeof(sd->status.skill));
  12497. memset(sd->status.skill, 0, sizeof(sd->status.skill));
  12498. for (i = 0; i < MAX_SKILL; i++) {
  12499. uint16 idx = 0;
  12500. if (tmp_skills[i].id == 0 || tmp_skills[i].lv == 0)
  12501. continue;
  12502. if ((idx = skill_get_index(tmp_skills[i].id))) {
  12503. memcpy(&sd->status.skill[idx], &tmp_skills[i], sizeof(tmp_skills[i]));
  12504. count++;
  12505. }
  12506. else
  12507. 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);
  12508. }
  12509. }
  12510. }
  12511. /**
  12512. * Show available NPC Quest / Event Icon Check [Kisuka]
  12513. * @param sd Player
  12514. **/
  12515. void pc_show_questinfo(struct map_session_data *sd) {
  12516. #if PACKETVER >= 20090218
  12517. nullpo_retv(sd);
  12518. if (sd->bl.m < 0 || sd->bl.m >= MAX_MAPINDEX)
  12519. return;
  12520. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  12521. nullpo_retv(mapdata);
  12522. if (mapdata->qi_npc.empty())
  12523. return;
  12524. if (mapdata->qi_npc.size() != sd->qi_display.size())
  12525. return; // init was not called yet
  12526. for (int i = 0; i < mapdata->qi_npc.size(); i++) {
  12527. struct npc_data *nd = map_id2nd(mapdata->qi_npc[i]);
  12528. if (!nd || nd->qi_data.empty())
  12529. continue;
  12530. bool show = false;
  12531. for (auto &qi : nd->qi_data) {
  12532. if (!qi->condition || achievement_check_condition(qi->condition, sd)) {
  12533. show = true;
  12534. // Check if need to be displayed
  12535. if (!sd->qi_display[i].is_active || qi->icon != sd->qi_display[i].icon || qi->color != sd->qi_display[i].color) {
  12536. sd->qi_display[i].is_active = true;
  12537. sd->qi_display[i].icon = qi->icon;
  12538. sd->qi_display[i].color = qi->color;
  12539. clif_quest_show_event(sd, &nd->bl, qi->icon, qi->color);
  12540. }
  12541. break;
  12542. }
  12543. }
  12544. if (show == false) {
  12545. // Check if need to be hide
  12546. if (sd->qi_display[i].is_active) {
  12547. sd->qi_display[i].is_active = false;
  12548. sd->qi_display[i].icon = QTYPE_NONE;
  12549. sd->qi_display[i].color = QMARK_NONE;
  12550. #if PACKETVER >= 20120410
  12551. clif_quest_show_event(sd, &nd->bl, QTYPE_NONE, QMARK_NONE);
  12552. #else
  12553. clif_quest_show_event(sd, &nd->bl, QTYPE_QUEST, QMARK_NONE);
  12554. #endif
  12555. }
  12556. }
  12557. }
  12558. #endif
  12559. }
  12560. /**
  12561. * Reinit the questinfo for player when changing map
  12562. * @param sd Player
  12563. **/
  12564. void pc_show_questinfo_reinit(struct map_session_data *sd) {
  12565. #if PACKETVER >= 20090218
  12566. nullpo_retv(sd);
  12567. sd->qi_display.clear();
  12568. if (sd->bl.m < 0 || sd->bl.m >= MAX_MAPINDEX)
  12569. return;
  12570. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  12571. nullpo_retv(mapdata);
  12572. if (mapdata->qi_npc.empty())
  12573. return;
  12574. sd->qi_display.reserve( mapdata->qi_npc.size() );
  12575. for( int i = 0; i < mapdata->qi_npc.size(); i++ ){
  12576. sd->qi_display.push_back( s_qi_display() );
  12577. }
  12578. #endif
  12579. }
  12580. /**
  12581. * Check if a job is allowed to enter the map
  12582. * @param jobid Job ID see enum e_job or sd->status.class_
  12583. * @param m ID -an index- for direct indexing map[] array
  12584. * @return 1 if job is allowed, 0 otherwise
  12585. **/
  12586. bool pc_job_can_entermap(enum e_job jobid, int m, int group_lv) {
  12587. // Map is other map server.
  12588. // !FIXME: Currently, a map-server doesn't recognized map's attributes on other server, so we assume it's fine to warp.
  12589. if (m < 0)
  12590. return true;
  12591. struct map_data *mapdata = map_getmapdata(m);
  12592. if (!mapdata->cell)
  12593. return false;
  12594. if (!pcdb_checkid(jobid))
  12595. return false;
  12596. std::shared_ptr<s_job_info> job = job_db.find(jobid);
  12597. if (!job->noenter_map.zone || group_lv > job->noenter_map.group_lv)
  12598. return true;
  12599. if ((job->noenter_map.zone&1 && !mapdata_flag_vs2(mapdata)) || // Normal
  12600. (job->noenter_map.zone&2 && mapdata->flag[MF_PVP]) || // PVP
  12601. (job->noenter_map.zone&4 && mapdata_flag_gvg2_no_te(mapdata)) || // GVG
  12602. (job->noenter_map.zone&8 && mapdata->flag[MF_BATTLEGROUND]) || // Battleground
  12603. (job->noenter_map.zone&16 && mapdata_flag_gvg2_te(mapdata)) || // WOE:TE
  12604. (job->noenter_map.zone&(mapdata->zone) && mapdata->flag[MF_RESTRICTED]) // Zone restriction
  12605. )
  12606. return false;
  12607. return true;
  12608. }
  12609. /**
  12610. * Tells client about player's costume view on mapchange for checking 'nocostume' mapflag.
  12611. * @param sd
  12612. **/
  12613. void pc_set_costume_view(struct map_session_data *sd) {
  12614. int i = -1, head_low = 0, head_mid = 0, head_top = 0, robe = 0;
  12615. struct item_data *id = NULL;
  12616. nullpo_retv(sd);
  12617. head_low = sd->status.head_bottom;
  12618. head_mid = sd->status.head_mid;
  12619. head_top = sd->status.head_top;
  12620. robe = sd->status.robe;
  12621. sd->status.head_bottom = sd->status.head_mid = sd->status.head_top = sd->status.robe = 0;
  12622. //Added check to prevent sending the same look on multiple slots ->
  12623. //causes client to redraw item on top of itself. (suggested by Lupus)
  12624. // Normal headgear checks
  12625. if ((i = sd->equip_index[EQI_HEAD_LOW]) != -1 && (id = sd->inventory_data[i])) {
  12626. if (!(id->equip&(EQP_HEAD_MID|EQP_HEAD_TOP)))
  12627. sd->status.head_bottom = id->look;
  12628. else
  12629. sd->status.head_bottom = 0;
  12630. }
  12631. if ((i = sd->equip_index[EQI_HEAD_MID]) != -1 && (id = sd->inventory_data[i])) {
  12632. if (!(id->equip&(EQP_HEAD_TOP)))
  12633. sd->status.head_mid = id->look;
  12634. else
  12635. sd->status.head_mid = 0;
  12636. }
  12637. if ((i = sd->equip_index[EQI_HEAD_TOP]) != -1 && (id = sd->inventory_data[i]))
  12638. sd->status.head_top = id->look;
  12639. if ((i = sd->equip_index[EQI_GARMENT]) != -1 && (id = sd->inventory_data[i]))
  12640. sd->status.robe = id->look;
  12641. // Costumes check
  12642. if (!map_getmapflag(sd->bl.m, MF_NOCOSTUME)) {
  12643. if ((i = sd->equip_index[EQI_COSTUME_HEAD_LOW]) != -1 && (id = sd->inventory_data[i])) {
  12644. if (!(id->equip&(EQP_COSTUME_HEAD_MID|EQP_COSTUME_HEAD_TOP)))
  12645. sd->status.head_bottom = id->look;
  12646. else
  12647. sd->status.head_bottom = 0;
  12648. }
  12649. if ((i = sd->equip_index[EQI_COSTUME_HEAD_MID]) != -1 && (id = sd->inventory_data[i])) {
  12650. if (!(id->equip&EQP_COSTUME_HEAD_TOP))
  12651. sd->status.head_mid = id->look;
  12652. else
  12653. sd->status.head_mid = 0;
  12654. }
  12655. if ((i = sd->equip_index[EQI_COSTUME_HEAD_TOP]) != -1 && (id = sd->inventory_data[i]))
  12656. sd->status.head_top = id->look;
  12657. if ((i = sd->equip_index[EQI_COSTUME_GARMENT]) != -1 && (id = sd->inventory_data[i]))
  12658. sd->status.robe = id->look;
  12659. }
  12660. if (sd->setlook_head_bottom)
  12661. sd->status.head_bottom = sd->setlook_head_bottom;
  12662. if (sd->setlook_head_mid)
  12663. sd->status.head_mid = sd->setlook_head_mid;
  12664. if (sd->setlook_head_top)
  12665. sd->status.head_top = sd->setlook_head_top;
  12666. if (sd->setlook_robe)
  12667. sd->status.robe = sd->setlook_robe;
  12668. if (head_low != sd->status.head_bottom)
  12669. clif_changelook(&sd->bl, LOOK_HEAD_BOTTOM, sd->status.head_bottom);
  12670. if (head_mid != sd->status.head_mid)
  12671. clif_changelook(&sd->bl, LOOK_HEAD_MID, sd->status.head_mid);
  12672. if (head_top != sd->status.head_top)
  12673. clif_changelook(&sd->bl, LOOK_HEAD_TOP, sd->status.head_top);
  12674. if (robe != sd->status.robe)
  12675. clif_changelook(&sd->bl, LOOK_ROBE, sd->status.robe);
  12676. }
  12677. std::shared_ptr<s_attendance_period> pc_attendance_period(){
  12678. uint32 date = date_get(DT_YYYYMMDD);
  12679. for( std::pair<const uint32,std::shared_ptr<s_attendance_period>>& pair : attendance_db ){
  12680. std::shared_ptr<s_attendance_period> period = pair.second;
  12681. if( period->start <= date && period->end >= date ){
  12682. return period;
  12683. }
  12684. }
  12685. return nullptr;
  12686. }
  12687. bool pc_attendance_enabled(){
  12688. // Check if the attendance feature is disabled
  12689. if( !battle_config.feature_attendance ){
  12690. return false;
  12691. }
  12692. // Check if there is a running attendance period
  12693. return pc_attendance_period() != nullptr;
  12694. }
  12695. static inline bool pc_attendance_rewarded_today( struct map_session_data* sd ){
  12696. return pc_readreg2( sd, ATTENDANCE_DATE_VAR ) >= date_get(DT_YYYYMMDD);
  12697. }
  12698. int32 pc_attendance_counter( struct map_session_data* sd ){
  12699. std::shared_ptr<s_attendance_period> period = pc_attendance_period();
  12700. // No running attendance period
  12701. if( period == nullptr ){
  12702. return 0;
  12703. }
  12704. // Get the counter for the current period
  12705. int counter = static_cast<int>(pc_readreg2( sd, ATTENDANCE_COUNT_VAR ));
  12706. // Check if we have a remaining counter from a previous period
  12707. if( counter > 0 && pc_readreg2( sd, ATTENDANCE_DATE_VAR ) < period->start ){
  12708. // Reset the counter to zero
  12709. pc_setreg2( sd, ATTENDANCE_COUNT_VAR, 0 );
  12710. return 0;
  12711. }
  12712. return 10 * counter + ( ( pc_attendance_rewarded_today(sd) ) ? 1 : 0 );
  12713. }
  12714. void pc_attendance_claim_reward( struct map_session_data* sd ){
  12715. // If the user's group does not have the permission
  12716. if( !pc_has_permission( sd, PC_PERM_ATTENDANCE ) ){
  12717. return;
  12718. }
  12719. // Check if the attendance feature is disabled
  12720. if( !pc_attendance_enabled() ){
  12721. return;
  12722. }
  12723. // Check if the user already got his reward today
  12724. if( pc_attendance_rewarded_today( sd ) ){
  12725. return;
  12726. }
  12727. int32 attendance_counter = static_cast<int32>(pc_readreg2( sd, ATTENDANCE_COUNT_VAR ));
  12728. attendance_counter += 1;
  12729. std::shared_ptr<s_attendance_period> period = pc_attendance_period();
  12730. if( period == nullptr ){
  12731. return;
  12732. }
  12733. if( period->rewards.size() < attendance_counter ){
  12734. return;
  12735. }
  12736. pc_setreg2( sd, ATTENDANCE_DATE_VAR, date_get(DT_YYYYMMDD) );
  12737. pc_setreg2( sd, ATTENDANCE_COUNT_VAR, attendance_counter );
  12738. if( save_settings&CHARSAVE_ATTENDANCE )
  12739. chrif_save(sd, CSAVE_NORMAL);
  12740. std::shared_ptr<s_attendance_reward> reward = period->rewards[attendance_counter - 1];
  12741. struct mail_message msg;
  12742. memset( &msg, 0, sizeof( struct mail_message ) );
  12743. msg.dest_id = sd->status.char_id;
  12744. safestrncpy( msg.send_name, msg_txt( sd, 788 ), NAME_LENGTH );
  12745. safesnprintf( msg.title, MAIL_TITLE_LENGTH, msg_txt( sd, 789 ), attendance_counter );
  12746. safesnprintf( msg.body, MAIL_BODY_LENGTH, msg_txt( sd, 790 ), attendance_counter );
  12747. msg.item[0].nameid = reward->item_id;
  12748. msg.item[0].amount = reward->amount;
  12749. msg.item[0].identify = 1;
  12750. msg.status = MAIL_NEW;
  12751. msg.type = MAIL_INBOX_NORMAL;
  12752. msg.timestamp = time(NULL);
  12753. intif_Mail_send(0, &msg);
  12754. clif_attendence_response( sd, attendance_counter );
  12755. }
  12756. /*==========================================
  12757. * pc Init/Terminate
  12758. *------------------------------------------*/
  12759. void do_final_pc(void) {
  12760. db_destroy(itemcd_db);
  12761. do_final_pc_groups();
  12762. ers_destroy(pc_sc_display_ers);
  12763. ers_destroy(num_reg_ers);
  12764. ers_destroy(str_reg_ers);
  12765. attendance_db.clear();
  12766. penalty_db.clear();
  12767. }
  12768. void do_init_pc(void) {
  12769. itemcd_db = idb_alloc(DB_OPT_RELEASE_DATA);
  12770. pc_readdb();
  12771. pc_read_motd(); // Read MOTD [Valaris]
  12772. attendance_db.load();
  12773. add_timer_func_list(pc_invincible_timer, "pc_invincible_timer");
  12774. add_timer_func_list(pc_eventtimer, "pc_eventtimer");
  12775. add_timer_func_list(pc_inventory_rental_end, "pc_inventory_rental_end");
  12776. add_timer_func_list(pc_calc_pvprank_timer, "pc_calc_pvprank_timer");
  12777. add_timer_func_list(pc_autosave, "pc_autosave");
  12778. add_timer_func_list(pc_spiritball_timer, "pc_spiritball_timer");
  12779. add_timer_func_list(pc_follow_timer, "pc_follow_timer");
  12780. add_timer_func_list(pc_endautobonus, "pc_endautobonus");
  12781. add_timer_func_list(pc_spiritcharm_timer, "pc_spiritcharm_timer");
  12782. add_timer_func_list(pc_global_expiration_timer, "pc_global_expiration_timer");
  12783. add_timer_func_list(pc_expiration_timer, "pc_expiration_timer");
  12784. add_timer_func_list(pc_autotrade_timer, "pc_autotrade_timer");
  12785. add_timer_func_list(pc_on_expire_active, "pc_on_expire_active");
  12786. add_timer(gettick() + autosave_interval, pc_autosave, 0, 0);
  12787. // 0=day, 1=night [Yor]
  12788. night_flag = battle_config.night_at_start ? 1 : 0;
  12789. if (battle_config.day_duration > 0 && battle_config.night_duration > 0) {
  12790. int day_duration = battle_config.day_duration;
  12791. int night_duration = battle_config.night_duration;
  12792. // add night/day timer [Yor]
  12793. add_timer_func_list(map_day_timer, "map_day_timer");
  12794. add_timer_func_list(map_night_timer, "map_night_timer");
  12795. day_timer_tid = add_timer_interval(gettick() + (night_flag ? 0 : day_duration) + night_duration, map_day_timer, 0, 0, day_duration + night_duration);
  12796. night_timer_tid = add_timer_interval(gettick() + day_duration + (night_flag ? night_duration : 0), map_night_timer, 0, 0, day_duration + night_duration);
  12797. }
  12798. do_init_pc_groups();
  12799. pc_sc_display_ers = ers_new(sizeof(struct sc_display_entry), "pc.cpp:pc_sc_display_ers", ERS_OPT_FLEX_CHUNK);
  12800. num_reg_ers = ers_new(sizeof(struct script_reg_num), "pc.cpp:num_reg_ers", (ERSOptions)(ERS_OPT_CLEAN|ERS_OPT_FLEX_CHUNK));
  12801. str_reg_ers = ers_new(sizeof(struct script_reg_str), "pc.cpp:str_reg_ers", (ERSOptions)(ERS_OPT_CLEAN|ERS_OPT_FLEX_CHUNK));
  12802. ers_chunk_size(pc_sc_display_ers, 150);
  12803. ers_chunk_size(num_reg_ers, 300);
  12804. ers_chunk_size(str_reg_ers, 50);
  12805. }