pc.cpp 410 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353
  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 <yaml-cpp/yaml.h>
  8. #include "../common/cbasetypes.hpp"
  9. #include "../common/core.hpp" // get_svn_revision()
  10. #include "../common/database.hpp"
  11. #include "../common/ers.hpp" // ers_destroy
  12. #include "../common/malloc.hpp"
  13. #include "../common/mmo.hpp" //NAME_LENGTH
  14. #include "../common/nullpo.hpp"
  15. #include "../common/random.hpp"
  16. #include "../common/showmsg.hpp"
  17. #include "../common/socket.hpp" // session[]
  18. #include "../common/strlib.hpp" // safestrncpy()
  19. #include "../common/timer.hpp"
  20. #include "../common/utilities.hpp"
  21. #include "../common/utils.hpp"
  22. #include "achievement.hpp"
  23. #include "atcommand.hpp" // get_atcommand_level()
  24. #include "battle.hpp" // battle_config
  25. #include "battleground.hpp"
  26. #include "buyingstore.hpp" // struct s_buyingstore
  27. #include "channel.hpp"
  28. #include "chat.hpp"
  29. #include "chrif.hpp"
  30. #include "clan.hpp"
  31. #include "clif.hpp"
  32. #include "date.hpp" // is_day_of_*()
  33. #include "duel.hpp"
  34. #include "elemental.hpp"
  35. #include "guild.hpp"
  36. #include "homunculus.hpp"
  37. #include "instance.hpp"
  38. #include "intif.hpp"
  39. #include "itemdb.hpp" // MAX_ITEMGROUP
  40. #include "log.hpp"
  41. #include "map.hpp"
  42. #include "mercenary.hpp"
  43. #include "mob.hpp"
  44. #include "npc.hpp"
  45. #include "party.hpp" // party_search()
  46. #include "pc_groups.hpp"
  47. #include "pet.hpp" // pet_unlocktarget()
  48. #include "quest.hpp"
  49. #include "script.hpp" // struct script_reg, struct script_regstr
  50. #include "searchstore.hpp" // struct s_search_store_info
  51. #include "status.hpp" // OPTION_*, struct weapon_atk
  52. #include "storage.hpp"
  53. #include "unit.hpp" // unit_stop_attack(), unit_stop_walking()
  54. #include "vending.hpp" // struct s_vending
  55. using namespace rathena;
  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. #define MAX_LEVEL_BASE_EXP 99999999 ///< Max Base EXP for player on Max Base Level
  60. #define MAX_LEVEL_JOB_EXP 999999999 ///< Max Job EXP for player on Max Job Level
  61. static unsigned int statp[MAX_LEVEL+1];
  62. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  63. static unsigned int level_penalty[3][CLASS_MAX][MAX_LEVEL*2+1];
  64. #endif
  65. // h-files are for declarations, not for implementations... [Shinomori]
  66. struct skill_tree_entry skill_tree[CLASS_COUNT][MAX_SKILL_TREE];
  67. // timer for night.day implementation
  68. int day_timer_tid = INVALID_TIMER;
  69. int night_timer_tid = INVALID_TIMER;
  70. struct eri *pc_sc_display_ers = NULL;
  71. struct eri *num_reg_ers;
  72. struct eri *str_reg_ers;
  73. int pc_expiration_tid = INVALID_TIMER;
  74. struct fame_list smith_fame_list[MAX_FAME_LIST];
  75. struct fame_list chemist_fame_list[MAX_FAME_LIST];
  76. struct fame_list taekwon_fame_list[MAX_FAME_LIST];
  77. struct s_job_info job_info[CLASS_COUNT];
  78. const std::string AttendanceDatabase::getDefaultLocation(){
  79. return std::string(db_path) + "/attendance.yml";
  80. }
  81. /**
  82. * Reads and parses an entry from the attendance_db.
  83. * @param node: YAML node containing the entry.
  84. * @return count of successfully parsed rows
  85. */
  86. uint64 AttendanceDatabase::parseBodyNode(const YAML::Node &node){
  87. uint32 start;
  88. if( !this->asUInt32( node, "Start", start ) ){
  89. return 0;
  90. }
  91. std::shared_ptr<s_attendance_period> attendance_period = this->find( start );
  92. bool exists = attendance_period != nullptr;
  93. if( !exists ){
  94. if( !this->nodeExists( node, "End" ) ){
  95. this->invalidWarning( node, "Node \"End\" is missing.\n" );
  96. return 0;
  97. }
  98. if( !this->nodeExists( node, "Rewards" ) ){
  99. this->invalidWarning( node, "Node \"Rewards\" is missing.\n" );
  100. return 0;
  101. }
  102. attendance_period = std::make_shared<s_attendance_period>();
  103. attendance_period->start = start;
  104. }
  105. // If it does not exist yet, we need to check it for sure
  106. bool requiresCollisionDetection = !exists;
  107. if( this->nodeExists( node, "End" ) ){
  108. uint32 end;
  109. if( !this->asUInt32( node, "End", end ) ){
  110. return 0;
  111. }
  112. // If the period is outdated already, we do not even bother parsing
  113. if( end < date_get( DT_YYYYMMDD ) ){
  114. this->invalidWarning( node, "Node \"End\" date %u has already passed, skipping.\n", end );
  115. return 0;
  116. }
  117. if( !exists || attendance_period->end != end ){
  118. requiresCollisionDetection = true;
  119. attendance_period->end = end;
  120. }
  121. }
  122. // Collision detection
  123. if( requiresCollisionDetection ){
  124. bool collision = false;
  125. for( std::pair<const uint32,std::shared_ptr<s_attendance_period>>& pair : *this ){
  126. std::shared_ptr<s_attendance_period> period = pair.second;
  127. if( exists && period->start == attendance_period->start ){
  128. // Dont compare to yourself
  129. continue;
  130. }
  131. // Check if start is inside another period
  132. if( period->start <= attendance_period->start && start <= period->end ){
  133. this->invalidWarning( node, "Node \"Start\" period %u intersects with period %u-%u, skipping.\n", attendance_period->start, period->start, period->end );
  134. collision = true;
  135. break;
  136. }
  137. // Check if end is inside another period
  138. if( period->start <= attendance_period->end && attendance_period->end <= period->end ){
  139. this->invalidWarning( node, "Node \"End\" period %u intersects with period %u-%u.\n", attendance_period->start, period->start, period->end );
  140. collision = true;
  141. break;
  142. }
  143. }
  144. if( collision ){
  145. return 0;
  146. }
  147. }
  148. if( this->nodeExists( node, "Rewards" ) ){
  149. const YAML::Node& rewardsNode = node["Rewards"];
  150. for( const YAML::Node& rewardNode : rewardsNode ){
  151. uint32 day;
  152. if( !this->asUInt32( rewardNode, "Day", day ) ){
  153. continue;
  154. }
  155. day -= 1;
  156. std::shared_ptr<s_attendance_reward> reward = util::map_find( attendance_period->rewards, day );
  157. bool reward_exists = reward != nullptr;
  158. if( !reward_exists ){
  159. if( !this->nodeExists( rewardNode, "ItemId" ) ){
  160. this->invalidWarning( rewardNode, "Node \"ItemId\" is missing.\n" );
  161. return 0;
  162. }
  163. reward = std::make_shared<s_attendance_reward>();
  164. }
  165. if( this->nodeExists( rewardNode, "ItemId" ) ){
  166. uint16 item_id;
  167. if( !this->asUInt16( rewardNode, "ItemId", item_id ) ){
  168. continue;
  169. }
  170. if( item_id == 0 || !itemdb_exists( item_id ) ){
  171. ShowError( "pc_attendance_load: Unknown item ID %hu for day %d.\n", item_id, day + 1 );
  172. continue;
  173. }
  174. reward->item_id = item_id;
  175. }
  176. if( this->nodeExists( rewardNode, "Amount" ) ){
  177. uint16 amount;
  178. if( !this->asUInt16( rewardNode, "Amount", amount ) ){
  179. continue;
  180. }
  181. if( amount == 0 ){
  182. ShowWarning( "pc_attendance_load: Invalid reward count %hu for day %d. Defaulting to 1...\n", amount, day + 1 );
  183. amount = 1;
  184. }else if( amount > MAX_AMOUNT ){
  185. ShowError( "pc_attendance_load: Reward count %hu above maximum %hu for day %d. Defaulting to %hu...\n", amount, MAX_AMOUNT, day + 1, MAX_AMOUNT );
  186. amount = MAX_AMOUNT;
  187. }
  188. reward->amount = amount;
  189. }else{
  190. if( !reward_exists ){
  191. reward->amount = 1;
  192. }
  193. }
  194. if( !reward_exists ){
  195. attendance_period->rewards[day] = reward;
  196. }
  197. }
  198. bool missing_day = false;
  199. for( int day = 0; day < attendance_period->rewards.size(); day++ ){
  200. if( attendance_period->rewards.find( day ) == attendance_period->rewards.end() ){
  201. ShowError( "pc_attendance_load: Reward for day %d is missing.\n", day + 1 );
  202. missing_day = true;
  203. break;
  204. }
  205. }
  206. if( missing_day ){
  207. return 0;
  208. }
  209. }
  210. if( !exists ){
  211. this->put( start, attendance_period );
  212. }
  213. return 1;
  214. }
  215. AttendanceDatabase attendance_db;
  216. #define MOTD_LINE_SIZE 128
  217. static char motd_text[MOTD_LINE_SIZE][CHAT_SIZE_MAX]; // Message of the day buffer [Valaris]
  218. bool reg_load;
  219. /**
  220. * Translation table from athena equip index to aegis bitmask
  221. */
  222. unsigned int equip_bitmask[EQI_MAX] = {
  223. EQP_ACC_L, // EQI_ACC_L
  224. EQP_ACC_R, // EQI_ACC_R
  225. EQP_SHOES, // EQI_SHOES
  226. EQP_GARMENT, // EQI_GARMENT
  227. EQP_HEAD_LOW, // EQI_HEAD_LOW
  228. EQP_HEAD_MID, // EQI_HEAD_MID
  229. EQP_HEAD_TOP, // EQI_HEAD_TOP
  230. EQP_ARMOR, // EQI_ARMOR
  231. EQP_HAND_L, // EQI_HAND_L
  232. EQP_HAND_R, // EQI_HAND_R
  233. EQP_COSTUME_HEAD_TOP, // EQI_COSTUME_HEAD_TOP
  234. EQP_COSTUME_HEAD_MID, // EQI_COSTUME_HEAD_MID
  235. EQP_COSTUME_HEAD_LOW, // EQI_COSTUME_HEAD_LOW
  236. EQP_COSTUME_GARMENT, // EQI_COSTUME_GARMENT
  237. EQP_AMMO, // EQI_AMMO
  238. EQP_SHADOW_ARMOR, // EQI_SHADOW_ARMOR
  239. EQP_SHADOW_WEAPON, // EQI_SHADOW_WEAPON
  240. EQP_SHADOW_SHIELD, // EQI_SHADOW_SHIELD
  241. EQP_SHADOW_SHOES, // EQI_SHADOW_SHOES
  242. EQP_SHADOW_ACC_R, // EQI_SHADOW_ACC_R
  243. EQP_SHADOW_ACC_L // EQI_SHADOW_ACC_L
  244. };
  245. //Links related info to the sd->hate_mob[]/sd->feel_map[] entries
  246. const struct sg_data sg_info[MAX_PC_FEELHATE] = {
  247. { SG_SUN_ANGER, SG_SUN_BLESS, SG_SUN_COMFORT, "PC_FEEL_SUN", "PC_HATE_MOB_SUN", is_day_of_sun },
  248. { SG_MOON_ANGER, SG_MOON_BLESS, SG_MOON_COMFORT, "PC_FEEL_MOON", "PC_HATE_MOB_MOON", is_day_of_moon },
  249. { SG_STAR_ANGER, SG_STAR_BLESS, SG_STAR_COMFORT, "PC_FEEL_STAR", "PC_HATE_MOB_STAR", is_day_of_star }
  250. };
  251. void pc_set_reg_load( bool val ){
  252. reg_load = val;
  253. }
  254. /**
  255. * Item Cool Down Delay Saving
  256. * Struct item_cd is not a member of struct map_session_data
  257. * to keep cooldowns in memory between player log-ins.
  258. * All cooldowns are reset when server is restarted.
  259. **/
  260. DBMap* itemcd_db = NULL; // char_id -> struct item_cd
  261. struct item_cd {
  262. t_tick tick[MAX_ITEMDELAYS]; //tick
  263. unsigned short nameid[MAX_ITEMDELAYS]; //item id
  264. };
  265. /**
  266. * Converts a class to its array index for CLASS_COUNT defined arrays.
  267. * Note that it does not do a validity check for speed purposes, where parsing
  268. * player input make sure to use a pcdb_checkid first!
  269. * @param class_ Job ID see enum e_job
  270. * @return Class Index
  271. */
  272. int pc_class2idx(int class_) {
  273. if (class_ >= JOB_NOVICE_HIGH)
  274. return class_- JOB_NOVICE_HIGH+JOB_MAX_BASIC;
  275. return class_;
  276. }
  277. /**
  278. * Get player's group ID
  279. * @param sd
  280. * @return Group ID
  281. */
  282. int pc_get_group_id(struct map_session_data *sd) {
  283. return sd->group_id;
  284. }
  285. /** Get player's group Level
  286. * @param sd
  287. * @return Group Level
  288. */
  289. int pc_get_group_level(struct map_session_data *sd) {
  290. return sd->group_level;
  291. }
  292. static TIMER_FUNC(pc_invincible_timer){
  293. struct map_session_data *sd;
  294. if( (sd=(struct map_session_data *)map_id2sd(id)) == NULL || sd->bl.type!=BL_PC )
  295. return 1;
  296. if(sd->invincible_timer != tid){
  297. ShowError("invincible_timer %d != %d\n",sd->invincible_timer,tid);
  298. return 0;
  299. }
  300. sd->invincible_timer = INVALID_TIMER;
  301. skill_unit_move(&sd->bl,tick,1);
  302. return 0;
  303. }
  304. void pc_setinvincibletimer(struct map_session_data* sd, int val) {
  305. nullpo_retv(sd);
  306. if( sd->invincible_timer != INVALID_TIMER )
  307. delete_timer(sd->invincible_timer,pc_invincible_timer);
  308. sd->invincible_timer = add_timer(gettick()+val,pc_invincible_timer,sd->bl.id,0);
  309. }
  310. void pc_delinvincibletimer(struct map_session_data* sd)
  311. {
  312. nullpo_retv(sd);
  313. if( sd->invincible_timer != INVALID_TIMER )
  314. {
  315. delete_timer(sd->invincible_timer,pc_invincible_timer);
  316. sd->invincible_timer = INVALID_TIMER;
  317. skill_unit_move(&sd->bl,gettick(),1);
  318. }
  319. }
  320. static TIMER_FUNC(pc_spiritball_timer){
  321. struct map_session_data *sd;
  322. int i;
  323. if( (sd=(struct map_session_data *)map_id2sd(id)) == NULL || sd->bl.type!=BL_PC )
  324. return 1;
  325. if( sd->spiritball <= 0 )
  326. {
  327. 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);
  328. sd->spiritball = 0;
  329. return 0;
  330. }
  331. ARR_FIND(0, sd->spiritball, i, sd->spirit_timer[i] == tid);
  332. if( i == sd->spiritball )
  333. {
  334. ShowError("pc_spiritball_timer: timer not found (aid=%d cid=%d tid=%d)\n", sd->status.account_id, sd->status.char_id, tid);
  335. return 0;
  336. }
  337. sd->spiritball--;
  338. if( i != sd->spiritball )
  339. memmove(sd->spirit_timer+i, sd->spirit_timer+i+1, (sd->spiritball-i)*sizeof(int));
  340. sd->spirit_timer[sd->spiritball] = INVALID_TIMER;
  341. clif_spiritball(&sd->bl);
  342. return 0;
  343. }
  344. /**
  345. * Adds a spiritball to player for 'interval' ms
  346. * @param sd
  347. * @param interval
  348. * @param max
  349. */
  350. void pc_addspiritball(struct map_session_data *sd,int interval,int max)
  351. {
  352. int tid;
  353. uint8 i;
  354. nullpo_retv(sd);
  355. if(max > MAX_SPIRITBALL)
  356. max = MAX_SPIRITBALL;
  357. if(sd->spiritball < 0)
  358. sd->spiritball = 0;
  359. if( sd->spiritball && sd->spiritball >= max )
  360. {
  361. if(sd->spirit_timer[0] != INVALID_TIMER)
  362. delete_timer(sd->spirit_timer[0],pc_spiritball_timer);
  363. sd->spiritball--;
  364. if( sd->spiritball != 0 )
  365. memmove(sd->spirit_timer+0, sd->spirit_timer+1, (sd->spiritball)*sizeof(int));
  366. sd->spirit_timer[sd->spiritball] = INVALID_TIMER;
  367. }
  368. tid = add_timer(gettick()+interval, pc_spiritball_timer, sd->bl.id, 0);
  369. 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);
  370. if( i != sd->spiritball )
  371. memmove(sd->spirit_timer+i+1, sd->spirit_timer+i, (sd->spiritball-i)*sizeof(int));
  372. sd->spirit_timer[i] = tid;
  373. sd->spiritball++;
  374. if( (sd->class_&MAPID_THIRDMASK) == MAPID_ROYAL_GUARD )
  375. clif_millenniumshield(&sd->bl,sd->spiritball);
  376. else
  377. clif_spiritball(&sd->bl);
  378. }
  379. /**
  380. * Removes number of spiritball from player
  381. * @param sd
  382. * @param count
  383. * @param type 1 = doesn't give client effect
  384. */
  385. void pc_delspiritball(struct map_session_data *sd,int count,int type)
  386. {
  387. uint8 i;
  388. nullpo_retv(sd);
  389. if(sd->spiritball <= 0) {
  390. sd->spiritball = 0;
  391. return;
  392. }
  393. if(count == 0)
  394. return;
  395. if(count > sd->spiritball)
  396. count = sd->spiritball;
  397. sd->spiritball -= count;
  398. if(count > MAX_SPIRITBALL)
  399. count = MAX_SPIRITBALL;
  400. for(i=0;i<count;i++) {
  401. if(sd->spirit_timer[i] != INVALID_TIMER) {
  402. delete_timer(sd->spirit_timer[i],pc_spiritball_timer);
  403. sd->spirit_timer[i] = INVALID_TIMER;
  404. }
  405. }
  406. for(i=count;i<MAX_SPIRITBALL;i++) {
  407. sd->spirit_timer[i-count] = sd->spirit_timer[i];
  408. sd->spirit_timer[i] = INVALID_TIMER;
  409. }
  410. if(!type) {
  411. if( (sd->class_&MAPID_THIRDMASK) == MAPID_ROYAL_GUARD )
  412. clif_millenniumshield(&sd->bl,sd->spiritball);
  413. else
  414. clif_spiritball(&sd->bl);
  415. }
  416. }
  417. /**
  418. * Increases a player's fame points and displays a notice to him
  419. * @param sd Player
  420. * @param count Fame point
  421. */
  422. void pc_addfame(struct map_session_data *sd,int count)
  423. {
  424. enum e_rank ranktype;
  425. nullpo_retv(sd);
  426. sd->status.fame += count;
  427. if(sd->status.fame > MAX_FAME)
  428. sd->status.fame = MAX_FAME;
  429. switch(sd->class_&MAPID_UPPERMASK){
  430. case MAPID_BLACKSMITH: ranktype = RANK_BLACKSMITH; break;
  431. case MAPID_ALCHEMIST: ranktype = RANK_ALCHEMIST; break;
  432. case MAPID_TAEKWON: ranktype = RANK_TAEKWON; break;
  433. default:
  434. ShowWarning( "pc_addfame: Trying to add fame to class '%s'(%d).\n", job_name(sd->class_), sd->class_ );
  435. return;
  436. }
  437. clif_update_rankingpoint(sd,ranktype,count);
  438. chrif_updatefamelist(sd);
  439. }
  440. /**
  441. * Check whether a player ID is in the fame rankers list of its job, returns his/her position if so, 0 else
  442. * @param sd
  443. * @param job Job use enum e_mapid
  444. * @return Rank
  445. */
  446. unsigned char pc_famerank(uint32 char_id, int job)
  447. {
  448. uint8 i;
  449. switch(job){
  450. case MAPID_BLACKSMITH: // Blacksmith
  451. for(i = 0; i < MAX_FAME_LIST; i++){
  452. if(smith_fame_list[i].id == char_id)
  453. return i + 1;
  454. }
  455. break;
  456. case MAPID_ALCHEMIST: // Alchemist
  457. for(i = 0; i < MAX_FAME_LIST; i++){
  458. if(chemist_fame_list[i].id == char_id)
  459. return i + 1;
  460. }
  461. break;
  462. case MAPID_TAEKWON: // Taekwon
  463. for(i = 0; i < MAX_FAME_LIST; i++){
  464. if(taekwon_fame_list[i].id == char_id)
  465. return i + 1;
  466. }
  467. break;
  468. }
  469. return 0;
  470. }
  471. /**
  472. * Restart player's HP & SP value
  473. * @param sd
  474. * @param type Restart type: 1 - Normal Resurection
  475. */
  476. void pc_setrestartvalue(struct map_session_data *sd, char type) {
  477. struct status_data *status, *b_status;
  478. nullpo_retv(sd);
  479. b_status = &sd->base_status;
  480. status = &sd->battle_status;
  481. if (type&1) { //Normal resurrection
  482. status->hp = 1; //Otherwise status_heal may fail if dead.
  483. status_heal(&sd->bl, b_status->hp, 0, 1);
  484. if( status->sp < b_status->sp )
  485. status_set_sp(&sd->bl, b_status->sp, 1);
  486. } else { //Just for saving on the char-server (with values as if respawned)
  487. sd->status.hp = b_status->hp;
  488. sd->status.sp = (status->sp < b_status->sp)?b_status->sp:status->sp;
  489. }
  490. }
  491. /*==========================================
  492. Rental System
  493. *------------------------------------------*/
  494. /**
  495. * Ends a rental and removes the item/effect
  496. * @param tid: Tick ID
  497. * @param tick: Timer
  498. * @param id: Timer ID
  499. * @param data: Data
  500. * @return false - failure, true - success
  501. */
  502. TIMER_FUNC(pc_inventory_rental_end){
  503. struct map_session_data *sd = map_id2sd(id);
  504. if( sd == NULL )
  505. return 0;
  506. if( tid != sd->rental_timer ) {
  507. ShowError("pc_inventory_rental_end: invalid timer id.\n");
  508. return 0;
  509. }
  510. pc_inventory_rentals(sd);
  511. return 1;
  512. }
  513. /**
  514. * Removes the rental timer from the player
  515. * @param sd: Player data
  516. */
  517. void pc_inventory_rental_clear(struct map_session_data *sd)
  518. {
  519. if( sd->rental_timer != INVALID_TIMER ) {
  520. delete_timer(sd->rental_timer, pc_inventory_rental_end);
  521. sd->rental_timer = INVALID_TIMER;
  522. }
  523. }
  524. /**
  525. * Check for items in the player's inventory that are rental type
  526. * @param sd: Player data
  527. */
  528. void pc_inventory_rentals(struct map_session_data *sd)
  529. {
  530. int i, c = 0;
  531. unsigned int next_tick = UINT_MAX;
  532. for( i = 0; i < MAX_INVENTORY; i++ ) { // Check for Rentals on Inventory
  533. if( sd->inventory.u.items_inventory[i].nameid == 0 )
  534. continue; // Nothing here
  535. if( sd->inventory.u.items_inventory[i].expire_time == 0 )
  536. continue;
  537. if( sd->inventory.u.items_inventory[i].expire_time <= time(NULL) ) {
  538. if (sd->inventory_data[i]->unequip_script)
  539. run_script(sd->inventory_data[i]->unequip_script, 0, sd->bl.id, fake_nd->bl.id);
  540. clif_rental_expired(sd->fd, i, sd->inventory.u.items_inventory[i].nameid);
  541. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 0, 0, LOG_TYPE_OTHER);
  542. } else {
  543. unsigned int expire_tick = (unsigned int)(sd->inventory.u.items_inventory[i].expire_time - time(NULL));
  544. clif_rental_time(sd->fd, sd->inventory.u.items_inventory[i].nameid, (int)expire_tick);
  545. next_tick = umin(expire_tick * 1000U, next_tick);
  546. c++;
  547. }
  548. }
  549. 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
  550. sd->rental_timer = add_timer(gettick() + umin(next_tick,3600000), pc_inventory_rental_end, sd->bl.id, 0);
  551. else
  552. sd->rental_timer = INVALID_TIMER;
  553. }
  554. /**
  555. * Add a rental item to the player and adjusts the rental timer appropriately
  556. * @param sd: Player data
  557. * @param seconds: Rental time
  558. */
  559. void pc_inventory_rental_add(struct map_session_data *sd, unsigned int seconds)
  560. {
  561. t_tick tick = seconds * 1000;
  562. if( sd == NULL )
  563. return;
  564. if( sd->rental_timer != INVALID_TIMER ) {
  565. const struct TimerData * td;
  566. td = get_timer(sd->rental_timer);
  567. if( DIFF_TICK(td->tick, gettick()) > tick ) { // Update Timer as this one ends first than the current one
  568. pc_inventory_rental_clear(sd);
  569. sd->rental_timer = add_timer(gettick() + tick, pc_inventory_rental_end, sd->bl.id, 0);
  570. }
  571. } else
  572. sd->rental_timer = add_timer(gettick() + i64min(tick,3600000), pc_inventory_rental_end, sd->bl.id, 0);
  573. }
  574. /**
  575. * Check if the player can sell the current item
  576. * @param sd: map_session_data of the player
  577. * @param item: struct of the checking item
  578. * @param shoptype: NPC's sub type see enum npc_subtype
  579. * @return bool 'true' is sellable, 'false' otherwise
  580. */
  581. bool pc_can_sell_item(struct map_session_data *sd, struct item *item, enum npc_subtype shoptype) {
  582. if (sd == NULL || item == NULL)
  583. return false;
  584. if (item->equip > 0 || item->amount < 0)
  585. return false;
  586. if (battle_config.hide_fav_sell && item->favorite)
  587. return false; //Cannot sell favs (optional config)
  588. if (!battle_config.rental_transaction && item->expire_time)
  589. return false; // Cannot Sell Rental Items
  590. if( item->equipSwitch ){
  591. return false;
  592. }
  593. if (itemdb_ishatched_egg(item))
  594. return false;
  595. switch (shoptype) {
  596. case NPCTYPE_SHOP:
  597. if (item->bound && battle_config.allow_bound_sell&ISR_BOUND_SELLABLE && (
  598. item->bound != BOUND_GUILD ||
  599. (sd->guild && sd->status.char_id == sd->guild->member[0].char_id) ||
  600. (item->bound == BOUND_GUILD && !(battle_config.allow_bound_sell&ISR_BOUND_GUILDLEADER_ONLY))
  601. ))
  602. return true;
  603. break;
  604. case NPCTYPE_ITEMSHOP:
  605. if (item->bound && battle_config.allow_bound_sell&ISR_BOUND && (
  606. item->bound != BOUND_GUILD ||
  607. (sd->guild && sd->status.char_id == sd->guild->member[0].char_id) ||
  608. (item->bound == BOUND_GUILD && !(battle_config.allow_bound_sell&ISR_BOUND_GUILDLEADER_ONLY))
  609. ))
  610. return true;
  611. else if (!item->bound) {
  612. struct item_data *itd = itemdb_search(item->nameid);
  613. if (itd && itd->flag.trade_restriction&8 && battle_config.allow_bound_sell&ISR_SELLABLE)
  614. return true;
  615. }
  616. break;
  617. }
  618. if (!itemdb_cansell(item, pc_get_group_level(sd)))
  619. return false;
  620. if (item->bound && !pc_can_give_bounded_items(sd))
  621. return false; // Don't allow sale of bound items
  622. return true;
  623. }
  624. /**
  625. * Determines if player can give / drop / trade / vend items
  626. */
  627. bool pc_can_give_items(struct map_session_data *sd)
  628. {
  629. return pc_has_permission(sd, PC_PERM_TRADE);
  630. }
  631. /**
  632. * Determines if player can give / drop / trade / vend bounded items
  633. */
  634. bool pc_can_give_bounded_items(struct map_session_data *sd)
  635. {
  636. return pc_has_permission(sd, PC_PERM_TRADE_BOUNDED);
  637. }
  638. /*==========================================
  639. * Prepares character for saving.
  640. * @param sd
  641. *------------------------------------------*/
  642. void pc_makesavestatus(struct map_session_data *sd) {
  643. nullpo_retv(sd);
  644. if(!battle_config.save_clothcolor)
  645. sd->status.clothes_color = 0;
  646. // Since this is currently not officially released,
  647. // its best to have a forced option to not save body styles.
  648. if(!battle_config.save_body_style)
  649. sd->status.body = 0;
  650. //Only copy the Cart/Peco/Falcon options, the rest are handled via
  651. //status change load/saving. [Skotlex]
  652. #ifdef NEW_CARTS
  653. sd->status.option = sd->sc.option&(OPTION_INVISIBLE|OPTION_FALCON|OPTION_RIDING|OPTION_DRAGON|OPTION_WUG|OPTION_WUGRIDER|OPTION_MADOGEAR);
  654. #else
  655. sd->status.option = sd->sc.option&(OPTION_INVISIBLE|OPTION_CART|OPTION_FALCON|OPTION_RIDING|OPTION_DRAGON|OPTION_WUG|OPTION_WUGRIDER|OPTION_MADOGEAR);
  656. #endif
  657. if (sd->sc.data[SC_JAILED]) { //When Jailed, do not move last point.
  658. if(pc_isdead(sd)){
  659. pc_setrestartvalue(sd, 0);
  660. } else {
  661. sd->status.hp = sd->battle_status.hp;
  662. sd->status.sp = sd->battle_status.sp;
  663. }
  664. sd->status.last_point.map = sd->mapindex;
  665. sd->status.last_point.x = sd->bl.x;
  666. sd->status.last_point.y = sd->bl.y;
  667. return;
  668. }
  669. if(pc_isdead(sd)) {
  670. pc_setrestartvalue(sd, 0);
  671. memcpy(&sd->status.last_point,&sd->status.save_point,sizeof(sd->status.last_point));
  672. } else {
  673. sd->status.hp = sd->battle_status.hp;
  674. sd->status.sp = sd->battle_status.sp;
  675. sd->status.last_point.map = sd->mapindex;
  676. sd->status.last_point.x = sd->bl.x;
  677. sd->status.last_point.y = sd->bl.y;
  678. }
  679. if(map_getmapflag(sd->bl.m, MF_NOSAVE)) {
  680. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  681. if(mapdata->save.map)
  682. memcpy(&sd->status.last_point,&mapdata->save,sizeof(sd->status.last_point));
  683. else
  684. memcpy(&sd->status.last_point,&sd->status.save_point,sizeof(sd->status.last_point));
  685. }
  686. }
  687. /*==========================================
  688. * Off init ? Connection?
  689. *------------------------------------------*/
  690. 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) {
  691. nullpo_retv(sd);
  692. sd->bl.id = account_id;
  693. sd->status.account_id = account_id;
  694. sd->status.char_id = char_id;
  695. sd->status.sex = sex;
  696. sd->login_id1 = login_id1;
  697. sd->login_id2 = 0; // at this point, we can not know the value :(
  698. sd->client_tick = client_tick;
  699. sd->state.active = 0; //to be set to 1 after player is fully authed and loaded.
  700. sd->bl.type = BL_PC;
  701. if(battle_config.prevent_logout_trigger&PLT_LOGIN)
  702. sd->canlog_tick = gettick();
  703. //Required to prevent homunculus copuing a base speed of 0.
  704. sd->battle_status.speed = sd->base_status.speed = DEFAULT_WALK_SPEED;
  705. }
  706. /**
  707. * Get equip point for an equip
  708. * @param sd
  709. * @param id
  710. */
  711. int pc_equippoint_sub(struct map_session_data *sd,struct item_data* id){
  712. int ep = 0;
  713. nullpo_ret(sd);
  714. nullpo_ret(id);
  715. if (!itemdb_isequip2(id))
  716. return 0; //Not equippable by players.
  717. ep = id->equip;
  718. if(id->look == W_DAGGER || id->look == W_1HSWORD || id->look == W_1HAXE) {
  719. 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]
  720. if (ep == EQP_WEAPON)
  721. return EQP_ARMS;
  722. if (ep == EQP_SHADOW_WEAPON)
  723. return EQP_SHADOW_ARMS;
  724. }
  725. }
  726. return ep;
  727. }
  728. /**
  729. * Get equip point for an equip
  730. * @param sd
  731. * @param n Equip index in inventory
  732. */
  733. int pc_equippoint(struct map_session_data *sd,int n){
  734. nullpo_ret(sd);
  735. return pc_equippoint_sub(sd,sd->inventory_data[n]);
  736. }
  737. /**
  738. * Fill inventory_data with struct *item_data through inventory (fill with struct *item)
  739. * @param sd : player session
  740. * @return 0 sucess, 1:invalid sd
  741. */
  742. void pc_setinventorydata(struct map_session_data *sd)
  743. {
  744. uint8 i;
  745. nullpo_retv(sd);
  746. for(i = 0; i < MAX_INVENTORY; i++) {
  747. unsigned short id = sd->inventory.u.items_inventory[i].nameid;
  748. sd->inventory_data[i] = id?itemdb_search(id):NULL;
  749. }
  750. }
  751. /**
  752. * 'Calculates' weapon type
  753. * @param sd : Player
  754. */
  755. void pc_calcweapontype(struct map_session_data *sd)
  756. {
  757. nullpo_retv(sd);
  758. // single-hand
  759. if(sd->weapontype2 == W_FIST) {
  760. sd->status.weapon = sd->weapontype1;
  761. return;
  762. }
  763. if(sd->weapontype1 == W_FIST) {
  764. sd->status.weapon = sd->weapontype2;
  765. return;
  766. }
  767. // dual-wield
  768. sd->status.weapon = 0;
  769. switch (sd->weapontype1){
  770. case W_DAGGER:
  771. switch (sd->weapontype2) {
  772. case W_DAGGER: sd->status.weapon = W_DOUBLE_DD; break;
  773. case W_1HSWORD: sd->status.weapon = W_DOUBLE_DS; break;
  774. case W_1HAXE: sd->status.weapon = W_DOUBLE_DA; break;
  775. }
  776. break;
  777. case W_1HSWORD:
  778. switch (sd->weapontype2) {
  779. case W_DAGGER: sd->status.weapon = W_DOUBLE_DS; break;
  780. case W_1HSWORD: sd->status.weapon = W_DOUBLE_SS; break;
  781. case W_1HAXE: sd->status.weapon = W_DOUBLE_SA; break;
  782. }
  783. break;
  784. case W_1HAXE:
  785. switch (sd->weapontype2) {
  786. case W_DAGGER: sd->status.weapon = W_DOUBLE_DA; break;
  787. case W_1HSWORD: sd->status.weapon = W_DOUBLE_SA; break;
  788. case W_1HAXE: sd->status.weapon = W_DOUBLE_AA; break;
  789. }
  790. }
  791. // unknown, default to right hand type
  792. if (!sd->status.weapon)
  793. sd->status.weapon = sd->weapontype1;
  794. }
  795. /**
  796. * Set equip index
  797. * @param sd : Player
  798. */
  799. void pc_setequipindex(struct map_session_data *sd)
  800. {
  801. uint16 i;
  802. nullpo_retv(sd);
  803. for (i = 0; i < EQI_MAX; i++){
  804. sd->equip_index[i] = -1;
  805. sd->equip_switch_index[i] = -1;
  806. }
  807. for (i = 0; i < MAX_INVENTORY; i++) {
  808. if (sd->inventory.u.items_inventory[i].nameid <= 0)
  809. continue;
  810. if (sd->inventory.u.items_inventory[i].equip) {
  811. uint8 j;
  812. for (j = 0; j < EQI_MAX; j++)
  813. if (sd->inventory.u.items_inventory[i].equip & equip_bitmask[j])
  814. sd->equip_index[j] = i;
  815. if (sd->inventory.u.items_inventory[i].equip & EQP_HAND_R) {
  816. if (sd->inventory_data[i])
  817. sd->weapontype1 = sd->inventory_data[i]->look;
  818. else
  819. sd->weapontype1 = 0;
  820. }
  821. if( sd->inventory.u.items_inventory[i].equip & EQP_HAND_L ) {
  822. if( sd->inventory_data[i] && sd->inventory_data[i]->type == IT_WEAPON )
  823. sd->weapontype2 = sd->inventory_data[i]->look;
  824. else
  825. sd->weapontype2 = 0;
  826. }
  827. }
  828. if (sd->inventory.u.items_inventory[i].equipSwitch) {
  829. for (uint8 j = 0; j < EQI_MAX; j++) {
  830. if (sd->inventory.u.items_inventory[i].equipSwitch & equip_bitmask[j]) {
  831. sd->equip_switch_index[j] = i;
  832. }
  833. }
  834. }
  835. }
  836. pc_calcweapontype(sd);
  837. }
  838. //static int pc_isAllowedCardOn(struct map_session_data *sd,int s,int eqindex,int flag)
  839. //{
  840. // int i;
  841. // struct item *item = &sd->inventory.u.items_inventory[eqindex];
  842. // struct item_data *data;
  843. //
  844. // //Crafted/made/hatched items.
  845. // if (itemdb_isspecial(item->card[0]))
  846. // return 1;
  847. //
  848. // /* scan for enchant armor gems */
  849. // if( item->card[MAX_SLOTS - 1] && s < MAX_SLOTS - 1 )
  850. // s = MAX_SLOTS - 1;
  851. //
  852. // ARR_FIND( 0, s, i, item->card[i] && (data = itemdb_exists(item->card[i])) != NULL && data->flag.no_equip&flag );
  853. // return( i < s ) ? 0 : 1;
  854. //}
  855. /**
  856. * Check if an item is equiped by player
  857. * (Check if the itemid is equiped then search if that match the index in inventory (should be))
  858. * @param sd : player session
  859. * @param nameid : itemid
  860. * @return 1:yes, 0:no
  861. */
  862. bool pc_isequipped(struct map_session_data *sd, unsigned short nameid)
  863. {
  864. uint8 i;
  865. for( i = 0; i < EQI_MAX; i++ )
  866. {
  867. short index = sd->equip_index[i], j;
  868. if( index < 0 )
  869. continue;
  870. if( pc_is_same_equip_index((enum equip_index)i, sd->equip_index, index) )
  871. continue;
  872. if( !sd->inventory_data[index] )
  873. continue;
  874. if( sd->inventory_data[index]->nameid == nameid )
  875. return true;
  876. for( j = 0; j < sd->inventory_data[index]->slot; j++ ){
  877. if( sd->inventory.u.items_inventory[index].card[j] == nameid )
  878. return true;
  879. }
  880. }
  881. return false;
  882. }
  883. /**
  884. * Check adoption rules
  885. * @param p1_sd: Player 1
  886. * @param p2_sd: Player 2
  887. * @param b_sd: Player that will be adopted
  888. * @return ADOPT_ALLOWED - Sent message to Baby to accept or deny
  889. * ADOPT_ALREADY_ADOPTED - Already adopted
  890. * ADOPT_MARRIED_AND_PARTY - Need to be married and in the same party
  891. * ADOPT_EQUIP_RINGS - Need wedding rings equipped
  892. * ADOPT_NOT_NOVICE - Adoptee is not a Novice
  893. * ADOPT_CHARACTER_NOT_FOUND - Parent or Baby not found
  894. * ADOPT_MORE_CHILDREN - Cannot adopt more than 1 Baby (client message)
  895. * ADOPT_LEVEL_70 - Parents need to be level 70+ (client message)
  896. * ADOPT_MARRIED - Cannot adopt a married person (client message)
  897. */
  898. e_adopt_responses pc_try_adopt(struct map_session_data *p1_sd, struct map_session_data *p2_sd, struct map_session_data *b_sd)
  899. {
  900. if( !p1_sd || !p2_sd || !b_sd )
  901. return ADOPT_CHARACTER_NOT_FOUND;
  902. if( b_sd->status.father || b_sd->status.mother || b_sd->adopt_invite )
  903. return ADOPT_ALREADY_ADOPTED; // already adopted baby / in adopt request
  904. if( !p1_sd->status.partner_id || !p1_sd->status.party_id || p1_sd->status.party_id != b_sd->status.party_id )
  905. return ADOPT_MARRIED_AND_PARTY; // You need to be married and in party with baby to adopt
  906. if( p1_sd->status.partner_id != p2_sd->status.char_id || p2_sd->status.partner_id != p1_sd->status.char_id )
  907. return ADOPT_MARRIED_AND_PARTY; // Not married, wrong married
  908. if( p2_sd->status.party_id != p1_sd->status.party_id )
  909. return ADOPT_MARRIED_AND_PARTY; // Both parents need to be in the same party
  910. // Parents need to have their ring equipped
  911. if( !pc_isequipped(p1_sd, WEDDING_RING_M) && !pc_isequipped(p1_sd, WEDDING_RING_F) )
  912. return ADOPT_EQUIP_RINGS;
  913. if( !pc_isequipped(p2_sd, WEDDING_RING_M) && !pc_isequipped(p2_sd, WEDDING_RING_F) )
  914. return ADOPT_EQUIP_RINGS;
  915. // Already adopted a baby
  916. if( p1_sd->status.child || p2_sd->status.child ) {
  917. clif_Adopt_reply(p1_sd, ADOPT_REPLY_MORE_CHILDREN);
  918. return ADOPT_MORE_CHILDREN;
  919. }
  920. // Parents need at least lvl 70 to adopt
  921. if( p1_sd->status.base_level < 70 || p2_sd->status.base_level < 70 ) {
  922. clif_Adopt_reply(p1_sd, ADOPT_REPLY_LEVEL_70);
  923. return ADOPT_LEVEL_70;
  924. }
  925. if( b_sd->status.partner_id ) {
  926. clif_Adopt_reply(p1_sd, ADOPT_REPLY_MARRIED);
  927. return ADOPT_MARRIED;
  928. }
  929. #ifdef RENEWAL
  930. if (b_sd->status.base_level < 51)
  931. #else
  932. 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))
  933. #endif
  934. return ADOPT_NOT_NOVICE;
  935. return ADOPT_ALLOWED;
  936. }
  937. /*==========================================
  938. * Adoption Process
  939. *------------------------------------------*/
  940. bool pc_adoption(struct map_session_data *p1_sd, struct map_session_data *p2_sd, struct map_session_data *b_sd)
  941. {
  942. int job, joblevel;
  943. unsigned int jobexp;
  944. if( pc_try_adopt(p1_sd, p2_sd, b_sd) != ADOPT_ALLOWED )
  945. return false;
  946. // Preserve current job levels and progress
  947. joblevel = b_sd->status.job_level;
  948. jobexp = b_sd->status.job_exp;
  949. job = pc_mapid2jobid(b_sd->class_|JOBL_BABY, b_sd->status.sex);
  950. if( job != -1 && pc_jobchange(b_sd, job, 0) )
  951. { // Success, proceed to configure parents and baby skills
  952. p1_sd->status.child = b_sd->status.char_id;
  953. p2_sd->status.child = b_sd->status.char_id;
  954. b_sd->status.father = p1_sd->status.char_id;
  955. b_sd->status.mother = p2_sd->status.char_id;
  956. // Restore progress
  957. b_sd->status.job_level = joblevel;
  958. clif_updatestatus(b_sd, SP_JOBLEVEL);
  959. b_sd->status.job_exp = jobexp;
  960. clif_updatestatus(b_sd, SP_JOBEXP);
  961. // Baby Skills
  962. pc_skill(b_sd, WE_BABY, 1, ADDSKILL_PERMANENT);
  963. pc_skill(b_sd, WE_CALLPARENT, 1, ADDSKILL_PERMANENT);
  964. pc_skill(b_sd, WE_CHEERUP, 1, ADDSKILL_PERMANENT);
  965. // Parents Skills
  966. pc_skill(p1_sd, WE_CALLBABY, 1, ADDSKILL_PERMANENT);
  967. pc_skill(p2_sd, WE_CALLBABY, 1, ADDSKILL_PERMANENT);
  968. chrif_save(p1_sd, CSAVE_NORMAL);
  969. chrif_save(p2_sd, CSAVE_NORMAL);
  970. chrif_save(b_sd, CSAVE_NORMAL);
  971. achievement_update_objective(b_sd, AG_BABY, 1, 1);
  972. achievement_update_objective(p1_sd, AG_BABY, 1, 2);
  973. achievement_update_objective(p2_sd, AG_BABY, 1, 2);
  974. return true;
  975. }
  976. return false; // Job Change Fail
  977. }
  978. /**
  979. * Check unadoption rules and remove adoption
  980. * @param p1_sd: Player 1
  981. * @param p2_sd: Player 2
  982. * @param b_sd: Player that will be unadopted
  983. * @param independent_baby: If the baby becomes a normal class
  984. * @return see pc.hpp::e_unadopt_responses
  985. */
  986. e_unadopt_responses pc_unadopt(struct map_session_data *p1_sd, struct map_session_data *p2_sd, struct map_session_data *b_sd, bool independent_baby) {
  987. if (!p1_sd || !p2_sd || !b_sd)
  988. return UNADOPT_CHARACTER_NOT_FOUND;
  989. if (!b_sd->status.father || !b_sd->status.mother)
  990. return UNADOPT_NOT_ADOPTED;
  991. if (p1_sd->status.child != b_sd->status.char_id && p2_sd->status.child != b_sd->status.char_id)
  992. return UNADOPT_NOT_CHILD;
  993. // Baby is separated from parents by independence option (Becomes a normal class)
  994. if (independent_baby) {
  995. int32 joblevel = b_sd->status.job_level, job = pc_mapid2jobid(b_sd->class_ | ~JOBL_BABY, b_sd->status.sex);
  996. uint32 jobexp = b_sd->status.job_exp;
  997. if (job == -1 || !pc_jobchange(b_sd, job, 0))
  998. return UNADOPT_CHARACTER_NOT_FOUND;
  999. }
  1000. // Remove Baby/Parent status
  1001. p1_sd->status.child = 0;
  1002. p2_sd->status.child = 0;
  1003. b_sd->status.father = 0;
  1004. b_sd->status.mother = 0;
  1005. uint16 sk_idx;
  1006. // Remove Baby Skills
  1007. if ((sk_idx = skill_get_index(WE_BABY)) > 0 && pc_checkskill(b_sd, WE_BABY) > 0) {
  1008. b_sd->status.skill[sk_idx].lv = 0;
  1009. b_sd->status.skill[sk_idx].flag = SKILL_FLAG_TEMPORARY;
  1010. clif_deleteskill(b_sd, WE_BABY);
  1011. }
  1012. if ((sk_idx = skill_get_index(WE_CALLPARENT)) > 0 && pc_checkskill(b_sd, WE_CALLPARENT) > 0) {
  1013. b_sd->status.skill[sk_idx].lv = 0;
  1014. b_sd->status.skill[sk_idx].flag = SKILL_FLAG_TEMPORARY;
  1015. clif_deleteskill(b_sd, WE_CALLPARENT);
  1016. }
  1017. if ((sk_idx = skill_get_index(WE_CHEERUP)) > 0 && pc_checkskill(b_sd, WE_CHEERUP) > 0) {
  1018. b_sd->status.skill[sk_idx].lv = 0;
  1019. b_sd->status.skill[sk_idx].flag = SKILL_FLAG_TEMPORARY;
  1020. clif_deleteskill(b_sd, WE_CHEERUP);
  1021. }
  1022. // Remove Parents Skills
  1023. if ((sk_idx = skill_get_index(WE_CALLBABY)) > 0) {
  1024. if (pc_checkskill(p1_sd, WE_CALLBABY) > 0) {
  1025. p1_sd->status.skill[sk_idx].lv = 0;
  1026. p1_sd->status.skill[sk_idx].flag = SKILL_FLAG_TEMPORARY;
  1027. clif_deleteskill(b_sd, WE_CALLBABY);
  1028. }
  1029. if (pc_checkskill(p2_sd, WE_CALLBABY) > 0) {
  1030. p1_sd->status.skill[sk_idx].lv = 0;
  1031. p1_sd->status.skill[sk_idx].flag = SKILL_FLAG_TEMPORARY;
  1032. clif_deleteskill(b_sd, WE_CALLBABY);
  1033. }
  1034. }
  1035. chrif_save(p1_sd, CSAVE_NORMAL);
  1036. chrif_save(p2_sd, CSAVE_NORMAL);
  1037. chrif_save(b_sd, CSAVE_NORMAL);
  1038. return UNADOPT_ALLOWED;
  1039. }
  1040. /*==========================================
  1041. * Check if player can use/equip selected item. Used by pc_isUseitem and pc_isequip
  1042. Returns:
  1043. false : Cannot use/equip
  1044. true : Can use/equip
  1045. * Credits:
  1046. [Inkfish] for first idea
  1047. [Haru] for third-classes extension
  1048. [Cydh] finishing :D
  1049. *------------------------------------------*/
  1050. static bool pc_isItemClass (struct map_session_data *sd, struct item_data* item) {
  1051. while (1) {
  1052. if (item->class_upper&ITEMJ_NORMAL && !(sd->class_&(JOBL_UPPER|JOBL_THIRD|JOBL_BABY))) //normal classes (no upper, no baby, no third)
  1053. break;
  1054. #ifndef RENEWAL
  1055. //allow third classes to use trans. class items
  1056. if (item->class_upper&ITEMJ_UPPER && sd->class_&(JOBL_UPPER|JOBL_THIRD)) //trans. classes
  1057. break;
  1058. //third-baby classes can use same item too
  1059. if (item->class_upper&ITEMJ_BABY && sd->class_&JOBL_BABY) //baby classes
  1060. break;
  1061. //don't need to decide specific rules for third-classes?
  1062. //items for third classes can be used for all third classes
  1063. if (item->class_upper&(ITEMJ_THIRD|ITEMJ_THIRD_TRANS|ITEMJ_THIRD_BABY) && sd->class_&JOBL_THIRD)
  1064. break;
  1065. #else
  1066. //trans. classes (exl. third-trans.)
  1067. if (item->class_upper&ITEMJ_UPPER && sd->class_&JOBL_UPPER && !(sd->class_&JOBL_THIRD))
  1068. break;
  1069. //baby classes (exl. third-baby)
  1070. if (item->class_upper&ITEMJ_BABY && sd->class_&JOBL_BABY && !(sd->class_&JOBL_THIRD))
  1071. break;
  1072. //third classes (exl. third-trans. and baby-third)
  1073. if (item->class_upper&ITEMJ_THIRD && sd->class_&JOBL_THIRD && !(sd->class_&(JOBL_UPPER|JOBL_BABY)))
  1074. break;
  1075. //trans-third classes
  1076. if (item->class_upper&ITEMJ_THIRD_TRANS && sd->class_&JOBL_THIRD && sd->class_&JOBL_UPPER)
  1077. break;
  1078. //third-baby classes
  1079. if (item->class_upper&ITEMJ_THIRD_BABY && sd->class_&JOBL_THIRD && sd->class_&JOBL_BABY)
  1080. break;
  1081. #endif
  1082. return false;
  1083. }
  1084. return true;
  1085. }
  1086. /*=================================================
  1087. * Checks if the player can equip the item at index n in inventory.
  1088. * @param sd
  1089. * @param n Item index in inventory
  1090. * @return ITEM_EQUIP_ACK_OK(0) if can be equipped, or ITEM_EQUIP_ACK_FAIL(1)/ITEM_EQUIP_ACK_FAILLEVEL(2) if can't
  1091. *------------------------------------------------*/
  1092. uint8 pc_isequip(struct map_session_data *sd,int n)
  1093. {
  1094. struct item_data *item;
  1095. nullpo_retr(ITEM_EQUIP_ACK_FAIL, sd);
  1096. item = sd->inventory_data[n];
  1097. if(pc_has_permission(sd, PC_PERM_USE_ALL_EQUIPMENT))
  1098. return ITEM_EQUIP_ACK_OK;
  1099. if(item == NULL)
  1100. return ITEM_EQUIP_ACK_FAIL;
  1101. if(item->elv && sd->status.base_level < (unsigned int)item->elv)
  1102. return ITEM_EQUIP_ACK_FAILLEVEL;
  1103. if(item->elvmax && sd->status.base_level > (unsigned int)item->elvmax)
  1104. return ITEM_EQUIP_ACK_FAILLEVEL;
  1105. if(item->sex != 2 && sd->status.sex != item->sex)
  1106. return ITEM_EQUIP_ACK_FAIL;
  1107. //fail to equip if item is restricted
  1108. if (!battle_config.allow_equip_restricted_item && itemdb_isNoEquip(item, sd->bl.m))
  1109. return ITEM_EQUIP_ACK_FAIL;
  1110. if (item->equip&EQP_AMMO) {
  1111. switch (item->look) {
  1112. case AMMO_ARROW:
  1113. if (battle_config.ammo_check_weapon && sd->status.weapon != W_BOW && sd->status.weapon != W_MUSICAL && sd->status.weapon != W_WHIP) {
  1114. clif_msg(sd, ITEM_NEED_BOW);
  1115. return ITEM_EQUIP_ACK_FAIL;
  1116. }
  1117. break;
  1118. case AMMO_THROWABLE_DAGGER:
  1119. if (!pc_checkskill(sd, AS_VENOMKNIFE))
  1120. return ITEM_EQUIP_ACK_FAIL;
  1121. break;
  1122. case AMMO_BULLET:
  1123. case AMMO_SHELL:
  1124. 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
  1125. #ifdef RENEWAL
  1126. && sd->status.weapon != W_GRENADE
  1127. #endif
  1128. ) {
  1129. clif_msg(sd, ITEM_BULLET_EQUIP_FAIL);
  1130. return ITEM_EQUIP_ACK_FAIL;
  1131. }
  1132. break;
  1133. #ifndef RENEWAL
  1134. case AMMO_GRENADE:
  1135. if (battle_config.ammo_check_weapon && sd->status.weapon != W_GRENADE) {
  1136. clif_msg(sd, ITEM_BULLET_EQUIP_FAIL);
  1137. return ITEM_EQUIP_ACK_FAIL;
  1138. }
  1139. break;
  1140. #endif
  1141. case AMMO_CANNONBALL:
  1142. if (!pc_ismadogear(sd) && (sd->status.class_ == JOB_MECHANIC_T || sd->status.class_ == JOB_MECHANIC)) {
  1143. clif_msg(sd, ITEM_NEED_MADOGEAR); // Item can only be used when Mado Gear is mounted.
  1144. return ITEM_EQUIP_ACK_FAIL;
  1145. }
  1146. if (sd->state.active && !pc_iscarton(sd) && //Check if sc data is already loaded
  1147. (sd->status.class_ == JOB_GENETIC_T || sd->status.class_ == JOB_GENETIC)) {
  1148. clif_msg(sd, ITEM_NEED_CART); // Only available when cart is mounted.
  1149. return ITEM_EQUIP_ACK_FAIL;
  1150. }
  1151. break;
  1152. }
  1153. }
  1154. if (sd->sc.count) {
  1155. if(item->equip & EQP_ARMS && item->type == IT_WEAPON && sd->sc.data[SC_STRIPWEAPON]) // Also works with left-hand weapons [DracoRPG]
  1156. return ITEM_EQUIP_ACK_FAIL;
  1157. if(item->equip & EQP_SHIELD && item->type == IT_ARMOR && sd->sc.data[SC_STRIPSHIELD])
  1158. return ITEM_EQUIP_ACK_FAIL;
  1159. if(item->equip & EQP_ARMOR && sd->sc.data[SC_STRIPARMOR])
  1160. return ITEM_EQUIP_ACK_FAIL;
  1161. if(item->equip & EQP_HEAD_TOP && sd->sc.data[SC_STRIPHELM])
  1162. return ITEM_EQUIP_ACK_FAIL;
  1163. if(item->equip & EQP_ACC && sd->sc.data[SC__STRIPACCESSORY])
  1164. return ITEM_EQUIP_ACK_FAIL;
  1165. if (item->equip & EQP_ARMS && sd->sc.data[SC__WEAKNESS])
  1166. return ITEM_EQUIP_ACK_FAIL;
  1167. if(item->equip && (sd->sc.data[SC_KYOUGAKU] || sd->sc.data[SC_SUHIDE]))
  1168. return ITEM_EQUIP_ACK_FAIL;
  1169. if (sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_SUPERNOVICE) {
  1170. //Spirit of Super Novice equip bonuses. [Skotlex]
  1171. if (sd->status.base_level > 90 && item->equip & EQP_HELM)
  1172. return ITEM_EQUIP_ACK_OK; //Can equip all helms
  1173. if (sd->status.base_level > 96 && item->equip & EQP_ARMS && item->type == IT_WEAPON && item->wlv == 4)
  1174. switch(item->look) { //In weapons, the look determines type of weapon.
  1175. case W_DAGGER: //All level 4 - Daggers
  1176. case W_1HSWORD: //All level 4 - 1H Swords
  1177. case W_1HAXE: //All level 4 - 1H Axes
  1178. case W_MACE: //All level 4 - 1H Maces
  1179. case W_STAFF: //All level 4 - 1H Staves
  1180. case W_2HSTAFF: //All level 4 - 2H Staves
  1181. return ITEM_EQUIP_ACK_OK;
  1182. }
  1183. }
  1184. }
  1185. //Not equipable by class. [Skotlex]
  1186. if (!(1ULL << (sd->class_&MAPID_BASEMASK)&item->class_base[(sd->class_&JOBL_2_1) ? 1 : ((sd->class_&JOBL_2_2) ? 2 : 0)]))
  1187. return ITEM_EQUIP_ACK_FAIL;
  1188. if (!pc_isItemClass(sd, item))
  1189. return ITEM_EQUIP_ACK_FAIL;
  1190. return ITEM_EQUIP_ACK_OK;
  1191. }
  1192. /*==========================================
  1193. * No problem with the session id
  1194. * set the status that has been sent from char server
  1195. *------------------------------------------*/
  1196. 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)
  1197. {
  1198. int i;
  1199. t_tick tick = gettick();
  1200. uint32 ip = session[sd->fd]->client_addr;
  1201. sd->login_id2 = login_id2;
  1202. sd->group_id = group_id;
  1203. /* load user permissions */
  1204. pc_group_pc_load(sd);
  1205. memcpy(&sd->status, st, sizeof(*st));
  1206. if (st->sex != sd->status.sex) {
  1207. clif_authfail_fd(sd->fd, 0);
  1208. return false;
  1209. }
  1210. //Set the map-server used job id. [Skotlex]
  1211. i = pc_jobid2mapid(sd->status.class_);
  1212. if (i == -1) { //Invalid class?
  1213. 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);
  1214. sd->status.class_ = JOB_NOVICE;
  1215. sd->class_ = MAPID_NOVICE;
  1216. } else
  1217. sd->class_ = i;
  1218. // Checks and fixes to character status data, that are required
  1219. // in case of configuration change or stuff, which cannot be
  1220. // checked on char-server.
  1221. sd->status.hair = cap_value(sd->status.hair,MIN_HAIR_STYLE,MAX_HAIR_STYLE);
  1222. sd->status.hair_color = cap_value(sd->status.hair_color,MIN_HAIR_COLOR,MAX_HAIR_COLOR);
  1223. sd->status.clothes_color = cap_value(sd->status.clothes_color,MIN_CLOTH_COLOR,MAX_CLOTH_COLOR);
  1224. sd->status.body = cap_value(sd->status.body,MIN_BODY_STYLE,MAX_BODY_STYLE);
  1225. //Initializations to null/0 unneeded since map_session_data was filled with 0 upon allocation.
  1226. sd->state.connect_new = 1;
  1227. sd->followtimer = INVALID_TIMER; // [MouseJstr]
  1228. sd->invincible_timer = INVALID_TIMER;
  1229. sd->npc_timer_id = INVALID_TIMER;
  1230. sd->pvp_timer = INVALID_TIMER;
  1231. sd->expiration_tid = INVALID_TIMER;
  1232. sd->autotrade_tid = INVALID_TIMER;
  1233. sd->respawn_tid = INVALID_TIMER;
  1234. #ifdef SECURE_NPCTIMEOUT
  1235. // Initialize to defaults/expected
  1236. sd->npc_idle_timer = INVALID_TIMER;
  1237. sd->npc_idle_tick = tick;
  1238. sd->npc_idle_type = NPCT_INPUT;
  1239. sd->state.ignoretimeout = false;
  1240. #endif
  1241. sd->canuseitem_tick = tick;
  1242. sd->canusecashfood_tick = tick;
  1243. sd->canequip_tick = tick;
  1244. sd->cantalk_tick = tick;
  1245. sd->canskill_tick = tick;
  1246. sd->cansendmail_tick = tick;
  1247. sd->idletime = last_tick;
  1248. for(i = 0; i < MAX_SPIRITBALL; i++)
  1249. sd->spirit_timer[i] = INVALID_TIMER;
  1250. if (battle_config.item_auto_get)
  1251. sd->state.autoloot = 10000;
  1252. if (battle_config.disp_experience)
  1253. sd->state.showexp = 1;
  1254. if (battle_config.disp_zeny)
  1255. sd->state.showzeny = 1;
  1256. #ifdef VIP_ENABLE
  1257. if (!battle_config.vip_disp_rate)
  1258. sd->vip.disableshowrate = 1;
  1259. #endif
  1260. if (!(battle_config.display_skill_fail&2))
  1261. sd->state.showdelay = 1;
  1262. memset(&sd->inventory, 0, sizeof(struct s_storage));
  1263. memset(&sd->cart, 0, sizeof(struct s_storage));
  1264. memset(&sd->storage, 0, sizeof(struct s_storage));
  1265. memset(&sd->premiumStorage, 0, sizeof(struct s_storage));
  1266. memset(&sd->equip_index, -1, sizeof(sd->equip_index));
  1267. memset(&sd->equip_switch_index, -1, sizeof(sd->equip_switch_index));
  1268. if( pc_isinvisible(sd) && !pc_can_use_command( sd, "hide", COMMAND_ATCOMMAND ) ){
  1269. sd->status.option &= ~OPTION_INVISIBLE;
  1270. }
  1271. status_change_init(&sd->bl);
  1272. sd->sc.option = sd->status.option; //This is the actual option used in battle.
  1273. unit_dataset(&sd->bl);
  1274. sd->guild_x = -1;
  1275. sd->guild_y = -1;
  1276. sd->delayed_damage = 0;
  1277. // Event Timers
  1278. for( i = 0; i < MAX_EVENTTIMER; i++ )
  1279. sd->eventtimer[i] = INVALID_TIMER;
  1280. // Rental Timer
  1281. sd->rental_timer = INVALID_TIMER;
  1282. for( i = 0; i < 3; i++ )
  1283. sd->hate_mob[i] = -1;
  1284. sd->quest_log = NULL;
  1285. sd->num_quests = 0;
  1286. sd->avail_quests = 0;
  1287. sd->save_quest = false;
  1288. sd->count_rewarp = 0;
  1289. sd->regs.vars = i64db_alloc(DB_OPT_BASE);
  1290. sd->regs.arrays = NULL;
  1291. sd->vars_dirty = false;
  1292. sd->vars_ok = false;
  1293. sd->vars_received = 0x0;
  1294. sd->qi_display = nullptr;
  1295. sd->qi_count = 0;
  1296. //warp player
  1297. if ((i=pc_setpos(sd,sd->status.last_point.map, sd->status.last_point.x, sd->status.last_point.y, CLR_OUTSIGHT)) != SETPOS_OK) {
  1298. ShowError ("Last_point_map %s - id %d not found (error code %d)\n", mapindex_id2name(sd->status.last_point.map), sd->status.last_point.map, i);
  1299. // try warping to a default map instead (church graveyard)
  1300. if (pc_setpos(sd, mapindex_name2id(MAP_PRONTERA), 273, 354, CLR_OUTSIGHT) != SETPOS_OK) {
  1301. // if we fail again
  1302. clif_authfail_fd(sd->fd, 0);
  1303. return false;
  1304. }
  1305. }
  1306. clif_authok(sd);
  1307. //Prevent S. Novices from getting the no-death bonus just yet. [Skotlex]
  1308. sd->die_counter=-1;
  1309. //display login notice
  1310. ShowInfo("'" CL_WHITE "%s" CL_RESET "' logged in."
  1311. " (AID/CID: '" CL_WHITE "%d/%d" CL_RESET "',"
  1312. " IP: '" CL_WHITE "%d.%d.%d.%d" CL_RESET "',"
  1313. " Group '" CL_WHITE "%d" CL_RESET "').\n",
  1314. sd->status.name, sd->status.account_id, sd->status.char_id,
  1315. CONVIP(ip), sd->group_id);
  1316. // Send friends list
  1317. clif_friendslist_send(sd);
  1318. if( !changing_mapservers ) {
  1319. if (battle_config.display_version == 1)
  1320. pc_show_version(sd);
  1321. // Message of the Day [Valaris]
  1322. for(i=0; i < MOTD_LINE_SIZE && motd_text[i][0]; i++) {
  1323. if (battle_config.motd_type)
  1324. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], motd_text[i], false, SELF);
  1325. else
  1326. clif_displaymessage(sd->fd, motd_text[i]);
  1327. }
  1328. if (expiration_time != 0)
  1329. sd->expiration_time = expiration_time;
  1330. /**
  1331. * Fixes login-without-aura glitch (the screen won't blink at this point, don't worry :P)
  1332. **/
  1333. clif_changemap(sd,sd->bl.m,sd->bl.x,sd->bl.y);
  1334. }
  1335. pc_validate_skill(sd);
  1336. /* [Ind] */
  1337. sd->sc_display = NULL;
  1338. sd->sc_display_count = 0;
  1339. // Player has not yet received the CashShop list
  1340. sd->status.cashshop_sent = false;
  1341. sd->last_addeditem_index = -1;
  1342. sd->bonus_script.head = NULL;
  1343. sd->bonus_script.count = 0;
  1344. // Initialize BG queue pointer
  1345. sd->bg_queue = nullptr;
  1346. sd->bg_queue_accept_state = false;
  1347. #if PACKETVER >= 20150513
  1348. sd->hatEffectIDs = NULL;
  1349. sd->hatEffectCount = 0;
  1350. #endif
  1351. sd->catch_target_class = PET_CATCH_FAIL;
  1352. // Check EXP overflow, since in previous revision EXP on Max Level can be more than 'official' Max EXP
  1353. if (pc_is_maxbaselv(sd) && sd->status.base_exp > MAX_LEVEL_BASE_EXP) {
  1354. sd->status.base_exp = MAX_LEVEL_BASE_EXP;
  1355. clif_updatestatus(sd, SP_BASEEXP);
  1356. }
  1357. if (pc_is_maxjoblv(sd) && sd->status.job_exp > MAX_LEVEL_JOB_EXP) {
  1358. sd->status.job_exp = MAX_LEVEL_JOB_EXP;
  1359. clif_updatestatus(sd, SP_JOBEXP);
  1360. }
  1361. // Request all registries (auth is considered completed whence they arrive)
  1362. intif_request_registry(sd,7);
  1363. return true;
  1364. }
  1365. /*==========================================
  1366. * Closes a connection because it failed to be authenticated from the char server.
  1367. *------------------------------------------*/
  1368. void pc_authfail(struct map_session_data *sd)
  1369. {
  1370. clif_authfail_fd(sd->fd, 0);
  1371. return;
  1372. }
  1373. /**
  1374. * Player register a bl as hatred
  1375. * @param sd : player session
  1376. * @param pos : hate position [0;2]
  1377. * @param bl : target bl
  1378. * @return false:failed, true:success
  1379. */
  1380. bool pc_set_hate_mob(struct map_session_data *sd, int pos, struct block_list *bl)
  1381. {
  1382. int class_;
  1383. if (!sd || !bl || pos < 0 || pos > 2)
  1384. return false;
  1385. if (sd->hate_mob[pos] != -1)
  1386. { //Can't change hate targets.
  1387. clif_hate_info(sd, pos, sd->hate_mob[pos], 0); //Display current
  1388. return false;
  1389. }
  1390. class_ = status_get_class(bl);
  1391. if (!pcdb_checkid(class_)) {
  1392. unsigned int max_hp = status_get_max_hp(bl);
  1393. if ((pos == 1 && max_hp < 6000) || (pos == 2 && max_hp < 20000))
  1394. return false;
  1395. if (pos != status_get_size(bl))
  1396. return false; //Wrong size
  1397. }
  1398. sd->hate_mob[pos] = class_;
  1399. pc_setglobalreg(sd, add_str(sg_info[pos].hate_var), class_+1);
  1400. clif_hate_info(sd, pos, class_, 1);
  1401. return true;
  1402. }
  1403. /*==========================================
  1404. * Invoked once after the char/account/account2 registry variables are received. [Skotlex]
  1405. * We didn't receive item information at this point so DO NOT attempt to do item operations here.
  1406. * See intif_parse_StorageReceived() for item operations [lighta]
  1407. *------------------------------------------*/
  1408. void pc_reg_received(struct map_session_data *sd)
  1409. {
  1410. uint8 i;
  1411. sd->vars_ok = true;
  1412. sd->change_level_2nd = static_cast<unsigned char>(pc_readglobalreg(sd, add_str(JOBCHANGE2ND_VAR)));
  1413. sd->change_level_3rd = static_cast<unsigned char>(pc_readglobalreg(sd, add_str(JOBCHANGE3RD_VAR)));
  1414. sd->die_counter = static_cast<int>(pc_readglobalreg(sd, add_str(PCDIECOUNTER_VAR)));
  1415. sd->langtype = static_cast<int>(pc_readaccountreg(sd, add_str(LANGTYPE_VAR)));
  1416. if (msg_checklangtype(sd->langtype,true) < 0)
  1417. sd->langtype = 0; //invalid langtype reset to default
  1418. // Cash shop
  1419. sd->cashPoints = static_cast<int>(pc_readaccountreg(sd, add_str(CASHPOINT_VAR)));
  1420. sd->kafraPoints = static_cast<int>(pc_readaccountreg(sd, add_str(KAFRAPOINT_VAR)));
  1421. // Cooking Exp
  1422. sd->cook_mastery = static_cast<short>(pc_readglobalreg(sd, add_str(COOKMASTERY_VAR)));
  1423. if( (sd->class_&MAPID_BASEMASK) == MAPID_TAEKWON )
  1424. { // Better check for class rather than skill to prevent "skill resets" from unsetting this
  1425. sd->mission_mobid = static_cast<short>(pc_readglobalreg(sd, add_str(TKMISSIONID_VAR)));
  1426. sd->mission_count = static_cast<unsigned char>(pc_readglobalreg(sd, add_str(TKMISSIONCOUNT_VAR)));
  1427. }
  1428. if (battle_config.feature_banking)
  1429. sd->bank_vault = static_cast<int>(pc_readreg2(sd, BANK_VAULT_VAR));
  1430. if (battle_config.feature_roulette) {
  1431. sd->roulette_point.bronze = static_cast<int>(pc_readreg2(sd, ROULETTE_BRONZE_VAR));
  1432. sd->roulette_point.silver = static_cast<int>(pc_readreg2(sd, ROULETTE_SILVER_VAR));
  1433. sd->roulette_point.gold = static_cast<int>(pc_readreg2(sd, ROULETTE_GOLD_VAR));
  1434. }
  1435. sd->roulette.prizeIdx = -1;
  1436. //SG map and mob read [Komurka]
  1437. for(i=0;i<MAX_PC_FEELHATE;i++) { //for now - someone need to make reading from txt/sql
  1438. uint16 j;
  1439. if ((j = static_cast<uint16>(pc_readglobalreg(sd, add_str(sg_info[i].feel_var)))) != 0) {
  1440. sd->feel_map[i].index = j;
  1441. sd->feel_map[i].m = map_mapindex2mapid(j);
  1442. } else {
  1443. sd->feel_map[i].index = 0;
  1444. sd->feel_map[i].m = -1;
  1445. }
  1446. sd->hate_mob[i] = static_cast<short>(pc_readglobalreg(sd, add_str(sg_info[i].hate_var)))-1;
  1447. }
  1448. if ((i = pc_checkskill(sd,RG_PLAGIARISM)) > 0) {
  1449. unsigned short skid = static_cast<unsigned short>(pc_readglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM)));
  1450. sd->cloneskill_idx = skill_get_index(skid);
  1451. if (sd->cloneskill_idx > 0) {
  1452. sd->status.skill[sd->cloneskill_idx].id = skid;
  1453. sd->status.skill[sd->cloneskill_idx].lv = static_cast<uint8>(pc_readglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM_LV)));
  1454. if (sd->status.skill[sd->cloneskill_idx].lv > i)
  1455. sd->status.skill[sd->cloneskill_idx].lv = i;
  1456. sd->status.skill[sd->cloneskill_idx].flag = SKILL_FLAG_PLAGIARIZED;
  1457. }
  1458. }
  1459. if ((i = pc_checkskill(sd,SC_REPRODUCE)) > 0) {
  1460. unsigned short skid = static_cast<unsigned short>(pc_readglobalreg(sd, add_str(SKILL_VAR_REPRODUCE)));
  1461. sd->reproduceskill_idx = skill_get_index(skid);
  1462. if (sd->reproduceskill_idx > 0) {
  1463. sd->status.skill[sd->reproduceskill_idx].id = skid;
  1464. sd->status.skill[sd->reproduceskill_idx].lv = static_cast<uint8>(pc_readglobalreg(sd, add_str(SKILL_VAR_REPRODUCE_LV)));
  1465. if (i < sd->status.skill[sd->reproduceskill_idx].lv)
  1466. sd->status.skill[sd->reproduceskill_idx].lv = i;
  1467. sd->status.skill[sd->reproduceskill_idx].flag = SKILL_FLAG_PLAGIARIZED;
  1468. }
  1469. }
  1470. //Weird... maybe registries were reloaded?
  1471. if (sd->state.active)
  1472. return;
  1473. sd->state.active = 1;
  1474. sd->state.pc_loaded = false; // Ensure inventory data and status data is loaded before we calculate player stats
  1475. intif_storage_request(sd,TABLE_STORAGE, 0, STOR_MODE_ALL); // Request storage data
  1476. intif_storage_request(sd,TABLE_CART, 0, STOR_MODE_ALL); // Request cart data
  1477. intif_storage_request(sd,TABLE_INVENTORY, 0, STOR_MODE_ALL); // Request inventory data
  1478. if (sd->status.party_id)
  1479. party_member_joined(sd);
  1480. if (sd->status.guild_id)
  1481. guild_member_joined(sd);
  1482. if( sd->status.clan_id )
  1483. clan_member_joined(sd);
  1484. // pet
  1485. if (sd->status.pet_id > 0)
  1486. intif_request_petdata(sd->status.account_id, sd->status.char_id, sd->status.pet_id);
  1487. // Homunculus [albator]
  1488. if( sd->status.hom_id > 0 )
  1489. intif_homunculus_requestload(sd->status.account_id, sd->status.hom_id);
  1490. if( sd->status.mer_id > 0 )
  1491. intif_mercenary_request(sd->status.mer_id, sd->status.char_id);
  1492. if( sd->status.ele_id > 0 )
  1493. intif_elemental_request(sd->status.ele_id, sd->status.char_id);
  1494. map_addiddb(&sd->bl);
  1495. map_delnickdb(sd->status.char_id, sd->status.name);
  1496. if (!chrif_auth_finished(sd))
  1497. ShowError("pc_reg_received: Failed to properly remove player %d:%d from logging db!\n", sd->status.account_id, sd->status.char_id);
  1498. chrif_skillcooldown_request(sd->status.account_id, sd->status.char_id);
  1499. chrif_bsdata_request(sd->status.char_id);
  1500. #ifdef VIP_ENABLE
  1501. sd->vip.time = 0;
  1502. sd->vip.enabled = 0;
  1503. chrif_req_login_operation(sd->status.account_id, sd->status.name, CHRIF_OP_LOGIN_VIP, 0, 1|8, 0); // request VIP information
  1504. #endif
  1505. intif_Mail_requestinbox(sd->status.char_id, 0, MAIL_INBOX_NORMAL); // MAIL SYSTEM - Request Mail Inbox
  1506. intif_request_questlog(sd);
  1507. if (battle_config.feature_achievement) {
  1508. sd->achievement_data.total_score = 0;
  1509. sd->achievement_data.level = 0;
  1510. sd->achievement_data.save = false;
  1511. sd->achievement_data.count = 0;
  1512. sd->achievement_data.incompleteCount = 0;
  1513. sd->achievement_data.achievements = NULL;
  1514. intif_request_achievements(sd->status.char_id);
  1515. }
  1516. if (sd->state.connect_new == 0 && sd->fd) { //Character already loaded map! Gotta trigger LoadEndAck manually.
  1517. sd->state.connect_new = 1;
  1518. clif_parse_LoadEndAck(sd->fd, sd);
  1519. if( pc_isinvisible( sd ) ){
  1520. sd->vd.class_ = JT_INVISIBLE;
  1521. clif_displaymessage( sd->fd, msg_txt( sd, 11 ) ); // Invisible: On
  1522. // decrement the number of pvp players on the map
  1523. map_getmapdata( sd->bl.m )->users_pvp--;
  1524. if( map_getmapflag( sd->bl.m, MF_PVP ) && !map_getmapflag( sd->bl.m, MF_PVP_NOCALCRANK ) && sd->pvp_timer != INVALID_TIMER ){
  1525. // unregister the player for ranking
  1526. delete_timer( sd->pvp_timer, pc_calc_pvprank_timer );
  1527. sd->pvp_timer = INVALID_TIMER;
  1528. }
  1529. clif_changeoption( &sd->bl );
  1530. }
  1531. }
  1532. channel_autojoin(sd);
  1533. }
  1534. static int pc_calc_skillpoint(struct map_session_data* sd)
  1535. {
  1536. uint16 i, skill_point = 0;
  1537. nullpo_ret(sd);
  1538. for(i = 1; i < MAX_SKILL; i++) {
  1539. if( sd->status.skill[i].id && sd->status.skill[i].lv > 0) {
  1540. std::shared_ptr<s_skill_db> skill = skill_db.find(sd->status.skill[i].id);
  1541. if ((!skill->inf2[INF2_ISQUEST] || battle_config.quest_skill_learn) &&
  1542. (!skill->inf2[INF2_ISWEDDING] || skill->inf2[INF2_ISSPIRIT]) //Do not count wedding/link skills. [Skotlex]
  1543. )
  1544. {
  1545. if(sd->status.skill[i].flag == SKILL_FLAG_PERMANENT)
  1546. skill_point += sd->status.skill[i].lv;
  1547. else if(sd->status.skill[i].flag >= SKILL_FLAG_REPLACED_LV_0)
  1548. skill_point += (sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0);
  1549. }
  1550. }
  1551. }
  1552. return skill_point;
  1553. }
  1554. static bool pc_grant_allskills(struct map_session_data *sd, bool addlv) {
  1555. if (!sd || !pc_has_permission(sd, PC_PERM_ALL_SKILL))
  1556. return false;
  1557. /**
  1558. * Dummy skills must NOT be added here otherwise they'll be displayed in the,
  1559. * skill tree and since they have no icons they'll give resource errors
  1560. * Get ALL skills except npc/guild ones. [Skotlex]
  1561. * Don't add SG_DEVIL [Komurka] and MO_TRIPLEATTACK and RG_SNATCHER [ultramage]
  1562. **/
  1563. for (const auto &skill : skill_db) {
  1564. uint16 skill_id = skill.second->nameid;
  1565. if (skill_id == 0 || skill.second->inf2[INF2_ISNPC]|| skill.second->inf2[INF2_ISGUILD])
  1566. continue;
  1567. switch (skill_id) {
  1568. case SM_SELFPROVOKE:
  1569. case AB_DUPLELIGHT_MELEE:
  1570. case AB_DUPLELIGHT_MAGIC:
  1571. case WL_CHAINLIGHTNING_ATK:
  1572. case WL_TETRAVORTEX_FIRE:
  1573. case WL_TETRAVORTEX_WATER:
  1574. case WL_TETRAVORTEX_WIND:
  1575. case WL_TETRAVORTEX_GROUND:
  1576. case WL_SUMMON_ATK_FIRE:
  1577. case WL_SUMMON_ATK_WIND:
  1578. case WL_SUMMON_ATK_WATER:
  1579. case WL_SUMMON_ATK_GROUND:
  1580. case LG_OVERBRAND_BRANDISH:
  1581. case LG_OVERBRAND_PLUSATK:
  1582. case WM_SEVERE_RAINSTORM_MELEE:
  1583. case RL_R_TRIP_PLUSATK:
  1584. case SG_DEVIL:
  1585. case MO_TRIPLEATTACK:
  1586. case RG_SNATCHER:
  1587. continue;
  1588. default:
  1589. {
  1590. uint8 lv = (uint8)skill_get_max(skill_id);
  1591. if (lv > 0) {
  1592. uint16 idx = skill_get_index(skill_id);
  1593. sd->status.skill[idx].id = skill_id;
  1594. if (addlv)
  1595. sd->status.skill[idx].lv = lv;
  1596. }
  1597. }
  1598. break;
  1599. }
  1600. }
  1601. return true;
  1602. }
  1603. /*==========================================
  1604. * Calculation of skill level.
  1605. * @param sd
  1606. *------------------------------------------*/
  1607. void pc_calc_skilltree(struct map_session_data *sd)
  1608. {
  1609. nullpo_retv(sd);
  1610. int job = pc_calc_skilltree_normalize_job(sd);
  1611. int class_ = pc_mapid2jobid(job, sd->status.sex);
  1612. if( class_ == -1 )
  1613. { //Unable to normalize job??
  1614. ShowError("pc_calc_skilltree: Unable to normalize job %d for character %s (%d:%d)\n", job, sd->status.name, sd->status.account_id, sd->status.char_id);
  1615. return;
  1616. }
  1617. class_ = pc_class2idx(class_);
  1618. for (const auto &skill : skill_db) {
  1619. uint16 skill_id = skill.second->nameid;
  1620. uint16 idx = skill_get_index(skill_id);
  1621. if( sd->status.skill[idx].flag != SKILL_FLAG_PLAGIARIZED && sd->status.skill[idx].flag != SKILL_FLAG_PERM_GRANTED ) //Don't touch these
  1622. sd->status.skill[idx].id = 0; //First clear skills.
  1623. /* permanent skills that must be re-checked */
  1624. if( sd->status.skill[idx].flag == SKILL_FLAG_PERM_GRANTED ) {
  1625. if (skill_id == 0) {
  1626. sd->status.skill[idx].id = 0;
  1627. sd->status.skill[idx].lv = 0;
  1628. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  1629. continue;
  1630. }
  1631. switch (skill_id) {
  1632. case NV_TRICKDEAD:
  1633. if( (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE ) {
  1634. sd->status.skill[idx].id = 0;
  1635. sd->status.skill[idx].lv = 0;
  1636. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  1637. }
  1638. break;
  1639. }
  1640. }
  1641. }
  1642. for (const auto &skill : skill_db) {
  1643. uint16 skill_id = skill.second->nameid;
  1644. uint16 idx = skill_get_index(skill_id);
  1645. // Restore original level of skills after deleting earned skills.
  1646. 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 ) {
  1647. sd->status.skill[idx].lv = (sd->status.skill[idx].flag == SKILL_FLAG_TEMPORARY) ? 0 : sd->status.skill[idx].flag - SKILL_FLAG_REPLACED_LV_0;
  1648. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  1649. }
  1650. //Enable Bard/Dancer spirit linked skills.
  1651. if (skill_id < DC_HUMMING || skill_id > DC_SERVICEFORYOU)
  1652. continue;
  1653. if( sd->sc.count && sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_BARDDANCER ) {
  1654. //Link Dancer skills to bard.
  1655. if( sd->status.sex ) {
  1656. if( sd->status.skill[idx - 8].lv < 10 )
  1657. continue;
  1658. sd->status.skill[idx].id = skill_id;
  1659. sd->status.skill[idx].lv = sd->status.skill[idx - 8].lv; // Set the level to the same as the linking skill
  1660. sd->status.skill[idx].flag = SKILL_FLAG_TEMPORARY; // Tag it as a non-savable, non-uppable, bonus skill
  1661. }
  1662. //Link Bard skills to dancer.
  1663. else {
  1664. if( sd->status.skill[idx].lv < 10 )
  1665. continue;
  1666. sd->status.skill[idx - 8].id = skill_id - 8;
  1667. sd->status.skill[idx - 8].lv = sd->status.skill[idx].lv; // Set the level to the same as the linking skill
  1668. sd->status.skill[idx - 8].flag = SKILL_FLAG_TEMPORARY; // Tag it as a non-savable, non-uppable, bonus skill
  1669. }
  1670. }
  1671. }
  1672. // Removes Taekwon Ranker skill bonus
  1673. if ((sd->class_&MAPID_UPPERMASK) != MAPID_TAEKWON) {
  1674. uint16 c_ = pc_class2idx(JOB_TAEKWON);
  1675. for (uint16 i = 0; i < MAX_SKILL_TREE; i++) {
  1676. uint16 sk_id = skill_tree[c_][i].skill_id;
  1677. uint16 sk_idx = 0;
  1678. if (!sk_id || !(sk_idx = skill_get_index(skill_tree[c_][i].skill_id)))
  1679. continue;
  1680. if (sd->status.skill[sk_idx].flag != SKILL_FLAG_PLAGIARIZED && sd->status.skill[sk_idx].flag != SKILL_FLAG_PERM_GRANTED) {
  1681. if (sk_id == NV_BASIC || sk_id == NV_FIRSTAID || sk_id == WE_CALLBABY)
  1682. continue;
  1683. sd->status.skill[sk_idx].id = 0;
  1684. }
  1685. }
  1686. }
  1687. // Grant all skills
  1688. pc_grant_allskills(sd, false);
  1689. int flag;
  1690. do {
  1691. uint16 skid = 0;
  1692. flag = 0;
  1693. for (uint16 i = 0; i < MAX_SKILL_TREE && (skid = skill_tree[class_][i].skill_id) > 0; i++) {
  1694. bool fail = false;
  1695. uint16 sk_idx = skill_get_index(skid);
  1696. if (sd->status.skill[sk_idx].id)
  1697. continue; //Skill already known.
  1698. if (!battle_config.skillfree) {
  1699. // Checking required skills
  1700. for(uint8 j = 0; j < MAX_PC_SKILL_REQUIRE; j++) {
  1701. uint16 sk_need_id = skill_tree[class_][i].need[j].skill_id;
  1702. uint16 sk_need_idx = 0;
  1703. if (sk_need_id && (sk_need_idx = skill_get_index(sk_need_id))) {
  1704. short sk_need = sk_need_id;
  1705. 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)
  1706. sk_need = 0; //Not learned.
  1707. else if (sd->status.skill[sk_need_idx].flag >= SKILL_FLAG_REPLACED_LV_0) //Real learned level
  1708. sk_need = sd->status.skill[sk_need_idx].flag - SKILL_FLAG_REPLACED_LV_0;
  1709. else
  1710. sk_need = pc_checkskill(sd,sk_need_id);
  1711. if (sk_need < skill_tree[class_][i].need[j].skill_lv) {
  1712. fail = true;
  1713. break;
  1714. }
  1715. }
  1716. }
  1717. if (sd->status.base_level < skill_tree[class_][i].baselv) { //We need to get the actual class in this case
  1718. int c_ = pc_mapid2jobid(sd->class_, sd->status.sex);
  1719. c_ = pc_class2idx(c_);
  1720. if (class_ == c_ || (class_ != c_ && sd->status.base_level < skill_tree[class_][i].baselv))
  1721. fail = true; // base level requirement wasn't satisfied
  1722. }
  1723. if (sd->status.job_level < skill_tree[class_][i].joblv) { //We need to get the actual class in this case
  1724. int c_ = pc_mapid2jobid(sd->class_, sd->status.sex);
  1725. c_ = pc_class2idx(c_);
  1726. if (class_ == c_ || (class_ != c_ && sd->status.job_level < skill_tree[class_][i].joblv))
  1727. fail = true; // job level requirement wasn't satisfied
  1728. }
  1729. }
  1730. if (!fail) {
  1731. std::shared_ptr<s_skill_db> skill = skill_db.find(skid);
  1732. if (!sd->status.skill[sk_idx].lv && (
  1733. (skill->inf2[INF2_ISQUEST] && !battle_config.quest_skill_learn) ||
  1734. skill->inf2[INF2_ISWEDDING] ||
  1735. (skill->inf2[INF2_ISSPIRIT] && !sd->sc.data[SC_SPIRIT])
  1736. ))
  1737. continue; //Cannot be learned via normal means. Note this check DOES allows raising already known skills.
  1738. sd->status.skill[sk_idx].id = skid;
  1739. if(skill->inf2[INF2_ISSPIRIT]) { //Spirit skills cannot be learned, they will only show up on your tree when you get buffed.
  1740. sd->status.skill[sk_idx].lv = 1; // need to manually specify a skill level
  1741. sd->status.skill[sk_idx].flag = SKILL_FLAG_TEMPORARY; //So it is not saved, and tagged as a "bonus" skill.
  1742. }
  1743. flag = 1; // skill list has changed, perform another pass
  1744. }
  1745. }
  1746. } while(flag);
  1747. if( class_ > 0 && sd->status.skill_point == 0 && pc_is_taekwon_ranker(sd) ) {
  1748. uint16 skid = 0;
  1749. /* Taekwon Ranker Bonus Skill Tree
  1750. ============================================
  1751. - Grant All Taekwon Tree, but only as Bonus Skills in case they drop from ranking.
  1752. - (c > 0) to avoid grant Novice Skill Tree in case of Skill Reset (need more logic)
  1753. - (sd->status.skill_point == 0) to wait until all skill points are assigned to avoid problems with Job Change quest. */
  1754. for( uint16 i = 0; i < MAX_SKILL_TREE && (skid = skill_tree[class_][i].skill_id) > 0; i++ ) {
  1755. uint16 sk_idx = skill_get_index(skid);
  1756. if (sk_idx == 0)
  1757. continue;
  1758. if( skill_get_inf2_(skid, { INF2_ISQUEST, INF2_ISWEDDING }) )
  1759. continue; //Do not include Quest/Wedding skills.
  1760. if( sd->status.skill[sk_idx].id == 0 ) {
  1761. sd->status.skill[sk_idx].id = skid;
  1762. sd->status.skill[sk_idx].flag = SKILL_FLAG_TEMPORARY; // So it is not saved, and tagged as a "bonus" skill.
  1763. } else if( skid != NV_BASIC )
  1764. sd->status.skill[sk_idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[sk_idx].lv; // Remember original level
  1765. sd->status.skill[sk_idx].lv = skill_tree_get_max(skid, sd->status.class_);
  1766. }
  1767. }
  1768. }
  1769. //Checks if you can learn a new skill after having leveled up a skill.
  1770. static void pc_check_skilltree(struct map_session_data *sd)
  1771. {
  1772. int i, flag = 0;
  1773. int c = 0;
  1774. if (battle_config.skillfree)
  1775. return; //Function serves no purpose if this is set
  1776. i = pc_calc_skilltree_normalize_job(sd);
  1777. c = pc_mapid2jobid(i, sd->status.sex);
  1778. if (c == -1) { //Unable to normalize job??
  1779. ShowError("pc_check_skilltree: Unable to normalize job %d for character %s (%d:%d)\n", i, sd->status.name, sd->status.account_id, sd->status.char_id);
  1780. return;
  1781. }
  1782. c = pc_class2idx(c);
  1783. do {
  1784. uint16 skid = 0;
  1785. flag = 0;
  1786. for (i = 0; i < MAX_SKILL_TREE && (skid = skill_tree[c][i].skill_id) > 0; i++ ) {
  1787. uint16 sk_idx = skill_get_index(skid);
  1788. bool fail = false;
  1789. uint8 j = 0;
  1790. if (sd->status.skill[sk_idx].id) //Already learned
  1791. continue;
  1792. // Checking required skills
  1793. for (j = 0; j < MAX_PC_SKILL_REQUIRE; j++) {
  1794. uint16 sk_need_id = skill_tree[c][i].need[j].skill_id;
  1795. uint16 sk_need_idx = 0;
  1796. if (sk_need_id && (sk_need_idx = skill_get_index(sk_need_id))) {
  1797. short sk_need = sk_need_id;
  1798. 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)
  1799. sk_need = 0; //Not learned.
  1800. else if (sd->status.skill[sk_need_idx].flag >= SKILL_FLAG_REPLACED_LV_0) //Real lerned level
  1801. sk_need = sd->status.skill[sk_need_idx].flag - SKILL_FLAG_REPLACED_LV_0;
  1802. else
  1803. sk_need = pc_checkskill(sd,sk_need_id);
  1804. if (sk_need < skill_tree[c][i].need[j].skill_lv) {
  1805. fail = true;
  1806. break;
  1807. }
  1808. }
  1809. }
  1810. if( fail )
  1811. continue;
  1812. if (sd->status.base_level < skill_tree[c][i].baselv || sd->status.job_level < skill_tree[c][i].joblv)
  1813. continue;
  1814. std::shared_ptr<s_skill_db> skill = skill_db.find(skid);
  1815. if( !sd->status.skill[sk_idx].lv && (
  1816. (skill->inf2[INF2_ISQUEST] && !battle_config.quest_skill_learn) ||
  1817. skill->inf2[INF2_ISWEDDING] ||
  1818. (skill->inf2[INF2_ISSPIRIT] && !sd->sc.data[SC_SPIRIT])
  1819. ) )
  1820. continue; //Cannot be learned via normal means.
  1821. sd->status.skill[sk_idx].id = skid;
  1822. flag = 1;
  1823. }
  1824. } while(flag);
  1825. }
  1826. // Make sure all the skills are in the correct condition
  1827. // before persisting to the backend.. [MouseJstr]
  1828. void pc_clean_skilltree(struct map_session_data *sd)
  1829. {
  1830. uint16 i;
  1831. for (i = 0; i < MAX_SKILL; i++){
  1832. if (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY || sd->status.skill[i].flag == SKILL_FLAG_PLAGIARIZED) {
  1833. sd->status.skill[i].id = 0;
  1834. sd->status.skill[i].lv = 0;
  1835. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1836. }
  1837. else if (sd->status.skill[i].flag >= SKILL_FLAG_REPLACED_LV_0){
  1838. sd->status.skill[i].lv = sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
  1839. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1840. }
  1841. }
  1842. }
  1843. int pc_calc_skilltree_normalize_job(struct map_session_data *sd)
  1844. {
  1845. int skill_point, novice_skills;
  1846. int c = sd->class_;
  1847. if (!battle_config.skillup_limit || pc_has_permission(sd, PC_PERM_ALL_SKILL))
  1848. return c;
  1849. skill_point = pc_calc_skillpoint(sd);
  1850. novice_skills = job_info[pc_class2idx(JOB_NOVICE)].max_level[1] - 1;
  1851. // limit 1st class and above to novice job levels
  1852. if(skill_point < novice_skills && (sd->class_&MAPID_BASEMASK) != MAPID_SUMMONER)
  1853. {
  1854. c = MAPID_NOVICE;
  1855. }
  1856. // limit 2nd class and above to first class job levels (super novices are exempt)
  1857. else if (sd->class_&JOBL_2 && (sd->class_&MAPID_UPPERMASK) != MAPID_SUPER_NOVICE)
  1858. {
  1859. // regenerate change_level_2nd
  1860. if (!sd->change_level_2nd)
  1861. {
  1862. if (sd->class_&JOBL_THIRD)
  1863. {
  1864. // if neither 2nd nor 3rd jobchange levels are known, we have to assume a default for 2nd
  1865. if (!sd->change_level_3rd)
  1866. sd->change_level_2nd = job_info[pc_class2idx(pc_mapid2jobid(sd->class_&MAPID_UPPERMASK, sd->status.sex))].max_level[1];
  1867. else
  1868. sd->change_level_2nd = 1 + skill_point + sd->status.skill_point
  1869. - (sd->status.job_level - 1)
  1870. - (sd->change_level_3rd - 1)
  1871. - novice_skills;
  1872. }
  1873. else
  1874. {
  1875. sd->change_level_2nd = 1 + skill_point + sd->status.skill_point
  1876. - (sd->status.job_level - 1)
  1877. - novice_skills;
  1878. }
  1879. pc_setglobalreg(sd, add_str(JOBCHANGE2ND_VAR), sd->change_level_2nd);
  1880. }
  1881. if (skill_point < novice_skills + (sd->change_level_2nd - 1))
  1882. {
  1883. c &= MAPID_BASEMASK;
  1884. }
  1885. // limit 3rd class to 2nd class/trans job levels
  1886. else if(sd->class_&JOBL_THIRD)
  1887. {
  1888. // regenerate change_level_3rd
  1889. if (!sd->change_level_3rd)
  1890. {
  1891. sd->change_level_3rd = 1 + skill_point + sd->status.skill_point
  1892. - (sd->status.job_level - 1)
  1893. - (sd->change_level_2nd - 1)
  1894. - novice_skills;
  1895. pc_setglobalreg(sd, add_str(JOBCHANGE3RD_VAR), sd->change_level_3rd);
  1896. }
  1897. if (skill_point < novice_skills + (sd->change_level_2nd - 1) + (sd->change_level_3rd - 1))
  1898. c &= MAPID_UPPERMASK;
  1899. }
  1900. }
  1901. // restore non-limiting flags
  1902. c |= sd->class_&(JOBL_UPPER|JOBL_BABY);
  1903. return c;
  1904. }
  1905. /*==========================================
  1906. * Updates the weight status
  1907. *------------------------------------------
  1908. * 1: overweight 50% for pre-renewal and 70% for renewal
  1909. * 2: overweight 90%
  1910. * It's assumed that SC_WEIGHT50 and SC_WEIGHT90 are only started/stopped here.
  1911. */
  1912. void pc_updateweightstatus(struct map_session_data *sd)
  1913. {
  1914. int old_overweight;
  1915. int new_overweight;
  1916. nullpo_retv(sd);
  1917. old_overweight = (sd->sc.data[SC_WEIGHT90]) ? 2 : (sd->sc.data[SC_WEIGHT50]) ? 1 : 0;
  1918. #ifdef RENEWAL
  1919. new_overweight = (pc_is90overweight(sd)) ? 2 : (pc_is70overweight(sd)) ? 1 : 0;
  1920. #else
  1921. new_overweight = (pc_is90overweight(sd)) ? 2 : (pc_is50overweight(sd)) ? 1 : 0;
  1922. #endif
  1923. if( old_overweight == new_overweight )
  1924. return; // no change
  1925. // stop old status change
  1926. if( old_overweight == 1 )
  1927. status_change_end(&sd->bl, SC_WEIGHT50, INVALID_TIMER);
  1928. else if( old_overweight == 2 )
  1929. status_change_end(&sd->bl, SC_WEIGHT90, INVALID_TIMER);
  1930. // start new status change
  1931. if( new_overweight == 1 )
  1932. sc_start(&sd->bl,&sd->bl, SC_WEIGHT50, 100, 0, 0);
  1933. else if( new_overweight == 2 )
  1934. sc_start(&sd->bl,&sd->bl, SC_WEIGHT90, 100, 0, 0);
  1935. // update overweight status
  1936. sd->regen.state.overweight = new_overweight;
  1937. }
  1938. int pc_disguise(struct map_session_data *sd, int class_)
  1939. {
  1940. if (!class_ && !sd->disguise)
  1941. return 0;
  1942. if (class_ && sd->disguise == class_)
  1943. return 0;
  1944. if(pc_isinvisible(sd))
  1945. { //Character is invisible. Stealth class-change. [Skotlex]
  1946. sd->disguise = class_; //viewdata is set on uncloaking.
  1947. return 2;
  1948. }
  1949. if (sd->bl.prev != NULL) {
  1950. pc_stop_walking(sd, 0);
  1951. clif_clearunit_area(&sd->bl, CLR_OUTSIGHT);
  1952. }
  1953. if (!class_) {
  1954. sd->disguise = 0;
  1955. class_ = sd->status.class_;
  1956. } else
  1957. sd->disguise=class_;
  1958. status_set_viewdata(&sd->bl, class_);
  1959. clif_changeoption(&sd->bl);
  1960. if (sd->bl.prev != NULL) {
  1961. clif_spawn(&sd->bl);
  1962. if (class_ == sd->status.class_ && pc_iscarton(sd))
  1963. { //It seems the cart info is lost on undisguise.
  1964. clif_cartlist(sd);
  1965. clif_updatestatus(sd,SP_CARTINFO);
  1966. }
  1967. if (sd->chatID) {
  1968. struct chat_data* cd;
  1969. if ((cd = (struct chat_data*)map_id2bl(sd->chatID)) != NULL)
  1970. clif_dispchat(cd,0);
  1971. }
  1972. }
  1973. return 1;
  1974. }
  1975. /// Show error message
  1976. #define PC_BONUS_SHOW_ERROR(type,type2,val) { ShowError("%s: %s: Invalid %s %d.\n",__FUNCTION__,#type,#type2,(val)); break; }
  1977. /// Check for valid Element, break & show error message if invalid Element
  1978. #define PC_BONUS_CHK_ELEMENT(ele,bonus) { if (!CHK_ELEMENT((ele))) { PC_BONUS_SHOW_ERROR((bonus),Element,(ele)); }}
  1979. /// Check for valid Race, break & show error message if invalid Race
  1980. #define PC_BONUS_CHK_RACE(rc,bonus) { if (!CHK_RACE((rc))) { PC_BONUS_SHOW_ERROR((bonus),Race,(rc)); }}
  1981. /// Check for valid Race2, break & show error message if invalid Race2
  1982. #define PC_BONUS_CHK_RACE2(rc2,bonus) { if (!CHK_RACE2((rc2))) { PC_BONUS_SHOW_ERROR((bonus),Race2,(rc2)); }}
  1983. /// Check for valid Class, break & show error message if invalid Class
  1984. #define PC_BONUS_CHK_CLASS(cl,bonus) { if (!CHK_CLASS((cl))) { PC_BONUS_SHOW_ERROR((bonus),Class,(cl)); }}
  1985. /// Check for valid Size, break & show error message if invalid Size
  1986. #define PC_BONUS_CHK_SIZE(sz,bonus) { if (!CHK_MOBSIZE((sz))) { PC_BONUS_SHOW_ERROR((bonus),Size,(sz)); }}
  1987. /// Check for valid SC, break & show error message if invalid SC
  1988. #define PC_BONUS_CHK_SC(sc,bonus) { if ((sc) <= SC_NONE || (sc) >= SC_MAX) { PC_BONUS_SHOW_ERROR((bonus),Effect,(sc)); }}
  1989. /**
  1990. * Add auto spell bonus for player while attacking/attacked
  1991. * @param spell: Spell array
  1992. * @param id: Skill to cast
  1993. * @param lv: Skill level
  1994. * @param rate: Success chance
  1995. * @param flag: Battle flag
  1996. * @param card_id: Used to prevent card stacking
  1997. */
  1998. static void pc_bonus_autospell(std::vector<s_autospell> &spell, short id, short lv, short rate, short flag, unsigned short card_id)
  1999. {
  2000. if (spell.size() == MAX_PC_BONUS) {
  2001. ShowWarning("pc_bonus_autospell: Reached max (%d) number of autospells per character!\n", MAX_PC_BONUS);
  2002. return;
  2003. }
  2004. if (!rate)
  2005. return;
  2006. if (!(flag&BF_RANGEMASK))
  2007. flag |= BF_SHORT | BF_LONG; //No range defined? Use both.
  2008. if (!(flag&BF_WEAPONMASK))
  2009. flag |= BF_WEAPON; //No attack type defined? Use weapon.
  2010. if (!(flag&BF_SKILLMASK)) {
  2011. if (flag&(BF_MAGIC | BF_MISC))
  2012. flag |= BF_SKILL; //These two would never trigger without BF_SKILL
  2013. if (flag&BF_WEAPON)
  2014. flag |= BF_NORMAL; //By default autospells should only trigger on normal weapon attacks.
  2015. }
  2016. for (auto &it : spell) {
  2017. if ((it.card_id == card_id || it.rate < 0 || rate < 0) && it.id == id && it.lv == lv && it.flag == flag) {
  2018. if (!battle_config.autospell_stacking && it.rate > 0 && rate > 0) // Stacking disabled
  2019. return;
  2020. it.rate = cap_value(it.rate + rate, -10000, 10000);
  2021. return;
  2022. }
  2023. }
  2024. struct s_autospell entry = {};
  2025. if (rate < -10000 || rate > 10000)
  2026. ShowWarning("pc_bonus_autospell: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2027. entry.id = id;
  2028. entry.lv = lv;
  2029. entry.rate = cap_value(rate, -10000, 10000);
  2030. entry.flag = flag;
  2031. entry.card_id = card_id;
  2032. spell.push_back(entry);
  2033. }
  2034. /**
  2035. * Add auto spell bonus for player while using skills
  2036. * @param spell: Spell array
  2037. * @param src_skill: Trigger skill
  2038. * @param id: Support or target type
  2039. * @param lv: Skill level
  2040. * @param rate: Success chance
  2041. * @param card_id: Used to prevent card stacking
  2042. */
  2043. static void pc_bonus_autospell_onskill(std::vector<s_autospell> &spell, short src_skill, short id, short lv, short rate, unsigned short card_id)
  2044. {
  2045. if (spell.size() == MAX_PC_BONUS) {
  2046. ShowWarning("pc_bonus_autospell_onskill: Reached max (%d) number of autospells per character!\n", MAX_PC_BONUS);
  2047. return;
  2048. }
  2049. if (!rate)
  2050. return;
  2051. struct s_autospell entry = {};
  2052. if (rate < -10000 || rate > 10000)
  2053. ShowWarning("pc_bonus_onskill: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2054. entry.flag = src_skill;
  2055. entry.id = id;
  2056. entry.lv = lv;
  2057. entry.rate = cap_value(rate, -10000, 10000);
  2058. entry.card_id = card_id;
  2059. spell.push_back(entry);
  2060. }
  2061. /**
  2062. * Add inflict effect bonus for player while attacking/attacked
  2063. * @param effect: Effect array
  2064. * @param sc: SC/Effect type
  2065. * @param rate: Success chance
  2066. * @param arrow_rate: success chance if bonus comes from arrow-type item
  2067. * @param flag: Target flag
  2068. * @param duration: Duration. If 0 use default duration lookup for associated skill with level 7
  2069. */
  2070. 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)
  2071. {
  2072. if (effect.size() == MAX_PC_BONUS) {
  2073. ShowWarning("pc_bonus_addeff: Reached max (%d) number of add effects per character!\n", MAX_PC_BONUS);
  2074. return;
  2075. }
  2076. if (!(flag&(ATF_SHORT | ATF_LONG)))
  2077. flag |= ATF_SHORT | ATF_LONG; //Default range: both
  2078. if (!(flag&(ATF_TARGET | ATF_SELF)))
  2079. flag |= ATF_TARGET; //Default target: enemy.
  2080. if (!(flag&(ATF_WEAPON | ATF_MAGIC | ATF_MISC)))
  2081. flag |= ATF_WEAPON; //Default type: weapon.
  2082. if (!duration)
  2083. duration = (unsigned int)skill_get_time2(status_sc2skill(sc), 7);
  2084. for (auto &it : effect) {
  2085. if (it.sc == sc && it.flag == flag) {
  2086. it.rate = cap_value(it.rate + rate, -10000, 10000);
  2087. it.arrow_rate += arrow_rate;
  2088. it.duration = umax(it.duration, duration);
  2089. return;
  2090. }
  2091. }
  2092. struct s_addeffect entry = {};
  2093. if (rate < -10000 || rate > 10000)
  2094. ShowWarning("pc_bonus_addeff: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2095. entry.sc = sc;
  2096. entry.rate = cap_value(rate, -10000, 10000);
  2097. entry.arrow_rate = arrow_rate;
  2098. entry.flag = flag;
  2099. entry.duration = duration;
  2100. effect.push_back(entry);
  2101. }
  2102. /**
  2103. * Add inflict effect bonus for player while attacking using skill
  2104. * @param effect: Effect array
  2105. * @param sc: SC/Effect type
  2106. * @param rate: Success chance
  2107. * @param skill_id: Skill to cast
  2108. * @param target: Target type
  2109. * @param duration: Duration. If 0 use default duration lookup for associated skill with level 7
  2110. */
  2111. 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)
  2112. {
  2113. if (effect.size() == MAX_PC_BONUS) {
  2114. ShowWarning("pc_bonus_addeff_onskill: Reached max (%d) number of add effects per character!\n", MAX_PC_BONUS);
  2115. return;
  2116. }
  2117. if (!duration)
  2118. duration = (unsigned int)skill_get_time2(status_sc2skill(sc), 7);
  2119. for (auto &it : effect) {
  2120. if (it.sc == sc && it.skill_id == skill_id && it.target == target) {
  2121. it.rate = cap_value(it.rate + rate, -10000, 10000);
  2122. it.duration = umax(it.duration, duration);
  2123. return;
  2124. }
  2125. }
  2126. struct s_addeffectonskill entry = {};
  2127. if (rate < -10000 || rate > 10000)
  2128. ShowWarning("pc_bonus_addeff_onskill: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2129. entry.sc = sc;
  2130. entry.rate = cap_value(rate, -10000, 10000);
  2131. entry.skill_id = skill_id;
  2132. entry.target = target;
  2133. entry.duration = duration;
  2134. effect.push_back(entry);
  2135. }
  2136. /**
  2137. * Adjust/add drop rate modifier for player
  2138. * @param drop: Player's sd->add_drop (struct s_add_drop)
  2139. * @param nameid: item id that will be dropped
  2140. * @param group: group id
  2141. * @param class_: target class
  2142. * @param race: target race. if < 0, means monster_id
  2143. * @param rate: rate value: 1 ~ 10000. If < 0, it will be multiplied with mob level/10
  2144. */
  2145. static void pc_bonus_item_drop(std::vector<s_add_drop> &drop, unsigned short nameid, uint16 group, int class_, short race, int rate)
  2146. {
  2147. if (!nameid && !group) {
  2148. ShowWarning("pc_bonus_item_drop: No Item ID nor Item Group ID specified.\n");
  2149. return;
  2150. }
  2151. if (nameid && !itemdb_exists(nameid)) {
  2152. ShowWarning("pc_bonus_item_drop: Invalid item id %hu\n",nameid);
  2153. return;
  2154. }
  2155. if (group && !itemdb_group_exists(group)) {
  2156. ShowWarning("pc_bonus_item_drop: Invalid Item Group %hu\n",group);
  2157. return;
  2158. }
  2159. if (drop.size() == MAX_PC_BONUS) {
  2160. ShowWarning("pc_bonus_item_drop: Reached max (%d) number of added drops per character! (nameid: %hu group: %d class_: %d race: %d rate: %d)\n", MAX_PC_BONUS, nameid, group, class_, race, rate);
  2161. return;
  2162. }
  2163. //Apply config rate adjustment settings.
  2164. if (rate >= 0) { //Absolute drop.
  2165. if (battle_config.item_rate_adddrop != 100)
  2166. rate = rate*battle_config.item_rate_adddrop/100;
  2167. if (rate < battle_config.item_drop_adddrop_min)
  2168. rate = battle_config.item_drop_adddrop_min;
  2169. else if (rate > battle_config.item_drop_adddrop_max)
  2170. rate = battle_config.item_drop_adddrop_max;
  2171. } else { //Relative drop, max/min limits are applied at drop time.
  2172. if (battle_config.item_rate_adddrop != 100)
  2173. rate = rate*battle_config.item_rate_adddrop/100;
  2174. if (rate > -1)
  2175. rate = -1;
  2176. }
  2177. for (auto &it : drop) {
  2178. if (it.nameid == nameid && it.group == group && it.race == race && it.class_ == class_) {
  2179. if ((rate < 0 && it.rate < 0) || (rate > 0 && it.rate > 0)) //Adjust the rate if it has same classification
  2180. it.rate = cap_value(it.rate + rate, -10000, 10000);
  2181. return;
  2182. }
  2183. }
  2184. struct s_add_drop entry = {};
  2185. if (rate < -10000 || rate > 10000)
  2186. ShowWarning("pc_bonus_item_drop: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2187. entry.nameid = nameid;
  2188. entry.group = group;
  2189. entry.race = race;
  2190. entry.class_ = class_;
  2191. entry.rate = cap_value(rate, -10000, 10000);
  2192. drop.push_back(entry);
  2193. }
  2194. /**
  2195. * Add autobonus to player when attacking/attacked
  2196. * @param bonus: Bonus array
  2197. * @param script: Script to execute
  2198. * @param rate: Success chance
  2199. * @param dur: Duration
  2200. * @param flag: Battle flag/skill
  2201. * @param other_script: Secondary script to execute
  2202. * @param pos: Item equip position
  2203. * @param onskill: Skill used to trigger autobonus
  2204. * @return True on success or false otherwise
  2205. */
  2206. bool pc_addautobonus(std::vector<s_autobonus> &bonus, const char *script, short rate, unsigned int dur, uint16 flag, const char *other_script, unsigned int pos, bool onskill)
  2207. {
  2208. if (bonus.size() == MAX_PC_BONUS) {
  2209. ShowWarning("pc_addautobonus: Reached max (%d) number of autobonus per character!\n", MAX_PC_BONUS);
  2210. return false;
  2211. }
  2212. if (!onskill) {
  2213. if (!(flag&BF_RANGEMASK))
  2214. flag |= BF_SHORT | BF_LONG; //No range defined? Use both.
  2215. if (!(flag&BF_WEAPONMASK))
  2216. flag |= BF_WEAPON; //No attack type defined? Use weapon.
  2217. if (!(flag&BF_SKILLMASK)) {
  2218. if (flag&(BF_MAGIC | BF_MISC))
  2219. flag |= BF_SKILL; //These two would never trigger without BF_SKILL
  2220. if (flag&BF_WEAPON)
  2221. flag |= BF_NORMAL | BF_SKILL;
  2222. }
  2223. }
  2224. struct s_autobonus entry = {};
  2225. if (rate < -10000 || rate > 10000)
  2226. ShowWarning("pc_addautobonus: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2227. entry.rate = cap_value(rate, -10000, 10000);
  2228. entry.duration = dur;
  2229. entry.active = INVALID_TIMER;
  2230. entry.atk_type = flag;
  2231. entry.pos = pos;
  2232. entry.bonus_script = aStrdup(script);
  2233. entry.other_script = (other_script ? aStrdup(other_script) : NULL);
  2234. bonus.push_back(entry);
  2235. return true;
  2236. }
  2237. /**
  2238. * Remove an autobonus from player
  2239. * @param sd: Player data
  2240. * @param bonus: Autobonus array
  2241. * @param restore: Run script on clearing or not
  2242. */
  2243. void pc_delautobonus(struct map_session_data* sd, std::vector<s_autobonus> &bonus, bool restore)
  2244. {
  2245. nullpo_retv(sd);
  2246. std::vector<s_autobonus>::iterator it = bonus.begin();
  2247. while( it != bonus.end() ){
  2248. s_autobonus b = *it;
  2249. if (b.active != INVALID_TIMER) {
  2250. if (restore && (sd->state.autobonus&b.pos) == b.pos) {
  2251. if (b.bonus_script) {
  2252. unsigned int equip_pos_idx = 0;
  2253. // Create a list of all equipped positions to see if all items needed for the autobonus are still present [Playtester]
  2254. for (uint8 j = 0; j < EQI_MAX; j++) {
  2255. if (sd->equip_index[j] >= 0)
  2256. equip_pos_idx |= sd->inventory.u.items_inventory[sd->equip_index[j]].equip;
  2257. }
  2258. if ((equip_pos_idx&b.pos) == b.pos)
  2259. script_run_autobonus(b.bonus_script, sd, b.pos);
  2260. }
  2261. it++;
  2262. continue;
  2263. } else { // Logout / Unequipped an item with an activated bonus
  2264. delete_timer(b.active, pc_endautobonus);
  2265. b.active = INVALID_TIMER;
  2266. }
  2267. }
  2268. if (b.bonus_script)
  2269. aFree(b.bonus_script);
  2270. if (b.other_script)
  2271. aFree(b.other_script);
  2272. it = bonus.erase(it);
  2273. }
  2274. }
  2275. /**
  2276. * Execute autobonus on player
  2277. * @param sd: Player data
  2278. * @param autobonus: Autobonus to run
  2279. */
  2280. void pc_exeautobonus(struct map_session_data *sd, std::vector<s_autobonus> *bonus, struct s_autobonus *autobonus)
  2281. {
  2282. nullpo_retv(sd);
  2283. nullpo_retv(autobonus);
  2284. if (autobonus->active != INVALID_TIMER)
  2285. delete_timer(autobonus->active, pc_endautobonus);
  2286. if( autobonus->other_script )
  2287. {
  2288. int j;
  2289. unsigned int equip_pos_idx = 0;
  2290. //Create a list of all equipped positions to see if all items needed for the autobonus are still present [Playtester]
  2291. for(j = 0; j < EQI_MAX; j++) {
  2292. if(sd->equip_index[j] >= 0)
  2293. equip_pos_idx |= sd->inventory.u.items_inventory[sd->equip_index[j]].equip;
  2294. }
  2295. if((equip_pos_idx&autobonus->pos) == autobonus->pos)
  2296. script_run_autobonus(autobonus->other_script,sd,autobonus->pos);
  2297. }
  2298. autobonus->active = add_timer(gettick()+autobonus->duration, pc_endautobonus, sd->bl.id, (intptr_t)bonus);
  2299. sd->state.autobonus |= autobonus->pos;
  2300. status_calc_pc(sd,SCO_NONE);
  2301. }
  2302. /**
  2303. * Remove autobonus timer from player
  2304. */
  2305. TIMER_FUNC(pc_endautobonus){
  2306. struct map_session_data *sd = map_id2sd(id);
  2307. std::vector<s_autobonus> *bonus = (std::vector<s_autobonus> *)data;
  2308. nullpo_ret(sd);
  2309. nullpo_ret(bonus);
  2310. for( struct s_autobonus& autobonus : *bonus ){
  2311. if( autobonus.active == tid ){
  2312. autobonus.active = INVALID_TIMER;
  2313. sd->state.autobonus &= ~autobonus.pos;
  2314. break;
  2315. }
  2316. }
  2317. status_calc_pc(sd,SCO_NONE);
  2318. return 0;
  2319. }
  2320. /**
  2321. * Add element bonus to player when attacking
  2322. * @param sd: Player data
  2323. * @param ele: Element to adjust
  2324. * @param rate: Success chance
  2325. * @param flag: Battle flag
  2326. */
  2327. static void pc_bonus_addele(struct map_session_data* sd, unsigned char ele, short rate, short flag)
  2328. {
  2329. nullpo_retv(sd);
  2330. struct weapon_data *wd = (sd->state.lr_flag ? &sd->left_weapon : &sd->right_weapon);
  2331. if (wd->addele2.size() == MAX_PC_BONUS) {
  2332. ShowWarning("pc_bonus_addele: Reached max (%d) number of add element damage bonuses per character!\n", MAX_PC_BONUS);
  2333. return;
  2334. }
  2335. if (!(flag&BF_RANGEMASK))
  2336. flag |= BF_SHORT | BF_LONG;
  2337. if (!(flag&BF_WEAPONMASK))
  2338. flag |= BF_WEAPON;
  2339. if (!(flag&BF_SKILLMASK)) {
  2340. if (flag&(BF_MAGIC | BF_MISC))
  2341. flag |= BF_SKILL;
  2342. if (flag&BF_WEAPON)
  2343. flag |= BF_NORMAL | BF_SKILL;
  2344. }
  2345. for (auto &it : wd->addele2) {
  2346. if (it.ele == ele && it.flag == flag) {
  2347. it.rate = cap_value(it.rate + rate, -10000, 10000);
  2348. return;
  2349. }
  2350. }
  2351. struct s_addele2 entry = {};
  2352. if (rate < -10000 || rate > 10000)
  2353. ShowWarning("pc_bonus_addele: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2354. entry.ele = ele;
  2355. entry.rate = cap_value(rate, -10000, 10000);
  2356. entry.flag = flag;
  2357. wd->addele2.push_back(entry);
  2358. }
  2359. /**
  2360. * Reduce element bonus to player when attacking
  2361. * @param sd: Player data
  2362. * @param ele: Element to adjust
  2363. * @param rate: Success chance
  2364. * @param flag: Battle flag
  2365. */
  2366. static void pc_bonus_subele(struct map_session_data* sd, unsigned char ele, short rate, short flag)
  2367. {
  2368. nullpo_retv(sd);
  2369. if (sd->subele2.size() == MAX_PC_BONUS) {
  2370. ShowWarning("pc_bonus_subele: Reached max (%d) number of resist element damage bonuses per character!\n", MAX_PC_BONUS);
  2371. return;
  2372. }
  2373. if (!(flag&BF_RANGEMASK))
  2374. flag |= BF_SHORT | BF_LONG;
  2375. if (!(flag&BF_WEAPONMASK))
  2376. flag |= BF_WEAPON;
  2377. if (!(flag&BF_SKILLMASK)) {
  2378. if (flag&(BF_MAGIC | BF_MISC))
  2379. flag |= BF_SKILL;
  2380. if (flag&BF_WEAPON)
  2381. flag |= BF_NORMAL | BF_SKILL;
  2382. }
  2383. for (auto &it : sd->subele2) {
  2384. if (it.ele == ele && it.flag == flag) {
  2385. it.rate = cap_value(it.rate + rate, -10000, 10000);
  2386. return;
  2387. }
  2388. }
  2389. struct s_addele2 entry = {};
  2390. if (rate < -10000 || rate > 10000)
  2391. ShowWarning("pc_bonus_subele: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2392. entry.ele = ele;
  2393. entry.rate = cap_value(rate, -10000, 10000);
  2394. entry.flag = flag;
  2395. sd->subele2.push_back(entry);
  2396. }
  2397. /**
  2398. * General item bonus for player
  2399. * @param bonus: Bonus array
  2400. * @param id: Key
  2401. * @param val: Value
  2402. * @param cap_rate: If Value is a rate value that needs to be capped
  2403. */
  2404. static void pc_bonus_itembonus(std::vector<s_item_bonus> &bonus, uint16 id, int val, bool cap_rate)
  2405. {
  2406. for (auto &it : bonus) {
  2407. if (it.id == id) {
  2408. if (cap_rate)
  2409. it.val = cap_value(it.val + val, -10000, 10000);
  2410. else
  2411. it.val += val;
  2412. return;
  2413. }
  2414. }
  2415. struct s_item_bonus entry = {};
  2416. if (cap_rate && (val < -10000 || val > 10000)) {
  2417. ShowWarning("pc_bonus_itembonus: Item bonus val %d exceeds -10000~10000 range, capping.\n", val);
  2418. val = cap_value(val, -10000, 10000);
  2419. }
  2420. entry.id = id;
  2421. entry.val = val;
  2422. bonus.push_back(entry);
  2423. }
  2424. /**
  2425. * Remove HP/SP to player when attacking
  2426. * @param bonus: Bonus array
  2427. * @param rate: Success chance
  2428. * @param per: Percentage of HP/SP to vanish
  2429. * @param flag: Battle flag
  2430. */
  2431. static void pc_bonus_addvanish(std::vector<s_vanish_bonus> &bonus, int16 rate, int16 per, int flag) {
  2432. if (bonus.size() == MAX_PC_BONUS) {
  2433. ShowWarning("pc_bonus_addvanish: Reached max (%d) number of vanish damage bonuses per character!\n", MAX_PC_BONUS);
  2434. return;
  2435. }
  2436. if (!(flag&BF_RANGEMASK))
  2437. flag |= BF_SHORT | BF_LONG;
  2438. if (!(flag&BF_WEAPONMASK))
  2439. flag |= BF_WEAPON;
  2440. if (!(flag&BF_SKILLMASK)) {
  2441. if (flag&(BF_MAGIC | BF_MISC))
  2442. flag |= BF_SKILL;
  2443. if (flag&BF_WEAPON)
  2444. flag |= BF_NORMAL | BF_SKILL;
  2445. }
  2446. for (auto &it : bonus) {
  2447. if (it.flag == flag) {
  2448. it.rate = cap_value(it.rate + rate, -10000, 10000);
  2449. it.per += per;
  2450. return;
  2451. }
  2452. }
  2453. struct s_vanish_bonus entry = {};
  2454. if (rate < -10000 || rate > 10000)
  2455. ShowWarning("pc_bonus_addvanish: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2456. entry.rate = cap_value(rate, -10000, 10000);
  2457. entry.per = per;
  2458. entry.flag = flag;
  2459. bonus.push_back(entry);
  2460. }
  2461. /*==========================================
  2462. * Add a bonus(type) to player sd
  2463. * format: bonus bBonusName,val;
  2464. * @param sd
  2465. * @param type Bonus type used by bBonusName
  2466. * @param val Value that usually for rate or fixed value
  2467. *------------------------------------------*/
  2468. void pc_bonus(struct map_session_data *sd,int type,int val)
  2469. {
  2470. struct status_data *status;
  2471. int bonus;
  2472. nullpo_retv(sd);
  2473. status = &sd->base_status;
  2474. switch(type){
  2475. case SP_STR:
  2476. case SP_AGI:
  2477. case SP_VIT:
  2478. case SP_INT:
  2479. case SP_DEX:
  2480. case SP_LUK:
  2481. if(sd->state.lr_flag != 2)
  2482. sd->param_bonus[type-SP_STR]+=val;
  2483. break;
  2484. case SP_ATK1:
  2485. if(!sd->state.lr_flag) {
  2486. bonus = status->rhw.atk + val;
  2487. status->rhw.atk = cap_value(bonus, 0, USHRT_MAX);
  2488. }
  2489. else if(sd->state.lr_flag == 1) {
  2490. bonus = status->lhw.atk + val;
  2491. status->lhw.atk = cap_value(bonus, 0, USHRT_MAX);
  2492. }
  2493. break;
  2494. case SP_ATK2:
  2495. if(!sd->state.lr_flag) {
  2496. bonus = status->rhw.atk2 + val;
  2497. status->rhw.atk2 = cap_value(bonus, 0, USHRT_MAX);
  2498. }
  2499. else if(sd->state.lr_flag == 1) {
  2500. bonus = status->lhw.atk2 + val;
  2501. status->lhw.atk2 = cap_value(bonus, 0, USHRT_MAX);
  2502. }
  2503. break;
  2504. case SP_BASE_ATK:
  2505. if(sd->state.lr_flag != 2) {
  2506. #ifdef RENEWAL
  2507. bonus = sd->bonus.eatk + val;
  2508. sd->bonus.eatk = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2509. #else
  2510. bonus = status->batk + val;
  2511. status->batk = cap_value(bonus, 0, USHRT_MAX);
  2512. #endif
  2513. }
  2514. break;
  2515. case SP_DEF1:
  2516. if(sd->state.lr_flag != 2) {
  2517. bonus = status->def + val;
  2518. #ifdef RENEWAL
  2519. status->def = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2520. #else
  2521. status->def = cap_value(bonus, CHAR_MIN, CHAR_MAX);
  2522. #endif
  2523. }
  2524. break;
  2525. case SP_DEF2:
  2526. if(sd->state.lr_flag != 2) {
  2527. bonus = status->def2 + val;
  2528. status->def2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2529. }
  2530. break;
  2531. case SP_MDEF1:
  2532. if(sd->state.lr_flag != 2) {
  2533. bonus = status->mdef + val;
  2534. #ifdef RENEWAL
  2535. status->mdef = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2536. #else
  2537. status->mdef = cap_value(bonus, CHAR_MIN, CHAR_MAX);
  2538. #endif
  2539. if( sd->state.lr_flag == 3 ) {//Shield, used for royal guard
  2540. sd->bonus.shieldmdef += bonus;
  2541. }
  2542. }
  2543. break;
  2544. case SP_MDEF2:
  2545. if(sd->state.lr_flag != 2) {
  2546. bonus = status->mdef2 + val;
  2547. status->mdef2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2548. }
  2549. break;
  2550. case SP_HIT:
  2551. if(sd->state.lr_flag != 2) {
  2552. bonus = status->hit + val;
  2553. status->hit = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2554. } else
  2555. sd->bonus.arrow_hit+=val;
  2556. break;
  2557. case SP_FLEE1:
  2558. if(sd->state.lr_flag != 2) {
  2559. bonus = status->flee + val;
  2560. status->flee = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2561. }
  2562. break;
  2563. case SP_FLEE2:
  2564. if(sd->state.lr_flag != 2) {
  2565. bonus = status->flee2 + val*10;
  2566. status->flee2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2567. }
  2568. break;
  2569. case SP_CRITICAL:
  2570. if(sd->state.lr_flag != 2) {
  2571. bonus = status->cri + val*10;
  2572. status->cri = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2573. } else
  2574. sd->bonus.arrow_cri += val*10;
  2575. break;
  2576. case SP_ATKELE:
  2577. PC_BONUS_CHK_ELEMENT(val,SP_ATKELE);
  2578. switch (sd->state.lr_flag)
  2579. {
  2580. case 2:
  2581. switch (sd->status.weapon) {
  2582. case W_BOW:
  2583. case W_REVOLVER:
  2584. case W_RIFLE:
  2585. case W_GATLING:
  2586. case W_SHOTGUN:
  2587. case W_GRENADE:
  2588. //Become weapon element.
  2589. status->rhw.ele=val;
  2590. break;
  2591. default: //Become arrow element.
  2592. sd->bonus.arrow_ele=val;
  2593. break;
  2594. }
  2595. break;
  2596. case 1:
  2597. status->lhw.ele=val;
  2598. break;
  2599. default:
  2600. status->rhw.ele=val;
  2601. break;
  2602. }
  2603. break;
  2604. case SP_DEFELE:
  2605. PC_BONUS_CHK_ELEMENT(val,SP_DEFELE);
  2606. if(sd->state.lr_flag != 2)
  2607. status->def_ele=val;
  2608. break;
  2609. case SP_MAXHP:
  2610. if(sd->state.lr_flag == 2)
  2611. break;
  2612. sd->bonus.hp += val;
  2613. break;
  2614. case SP_MAXSP:
  2615. if(sd->state.lr_flag == 2)
  2616. break;
  2617. sd->bonus.sp += val;
  2618. break;
  2619. case SP_MAXHPRATE:
  2620. if(sd->state.lr_flag != 2)
  2621. sd->hprate+=val;
  2622. break;
  2623. case SP_MAXSPRATE:
  2624. if(sd->state.lr_flag != 2)
  2625. sd->sprate+=val;
  2626. break;
  2627. case SP_SPRATE:
  2628. if(sd->state.lr_flag != 2)
  2629. sd->dsprate+=val;
  2630. break;
  2631. case SP_ATTACKRANGE:
  2632. switch (sd->state.lr_flag) {
  2633. case 2:
  2634. switch (sd->status.weapon) {
  2635. case W_BOW:
  2636. case W_REVOLVER:
  2637. case W_RIFLE:
  2638. case W_GATLING:
  2639. case W_SHOTGUN:
  2640. case W_GRENADE:
  2641. status->rhw.range += val;
  2642. }
  2643. break;
  2644. case 1:
  2645. status->lhw.range += val;
  2646. break;
  2647. default:
  2648. status->rhw.range += val;
  2649. break;
  2650. }
  2651. break;
  2652. case SP_SPEED_RATE: //Non stackable increase
  2653. if(sd->state.lr_flag != 2)
  2654. sd->bonus.speed_rate = min(sd->bonus.speed_rate, -val);
  2655. break;
  2656. case SP_SPEED_ADDRATE: //Stackable increase
  2657. if(sd->state.lr_flag != 2)
  2658. sd->bonus.speed_add_rate -= val;
  2659. break;
  2660. case SP_ASPD: //Raw increase
  2661. if(sd->state.lr_flag != 2)
  2662. sd->bonus.aspd_add -= 10*val;
  2663. break;
  2664. case SP_ASPD_RATE: //Stackable increase - Made it linear as per rodatazone
  2665. if(sd->state.lr_flag != 2)
  2666. #ifndef RENEWAL_ASPD
  2667. status->aspd_rate -= 10*val;
  2668. #else
  2669. status->aspd_rate2 += val;
  2670. #endif
  2671. break;
  2672. case SP_HP_RECOV_RATE:
  2673. if(sd->state.lr_flag != 2)
  2674. sd->hprecov_rate += val;
  2675. break;
  2676. case SP_SP_RECOV_RATE:
  2677. if(sd->state.lr_flag != 2)
  2678. sd->sprecov_rate += val;
  2679. break;
  2680. case SP_CRITICAL_DEF:
  2681. if(sd->state.lr_flag != 2)
  2682. sd->bonus.critical_def += val;
  2683. break;
  2684. case SP_NEAR_ATK_DEF:
  2685. if(sd->state.lr_flag != 2)
  2686. sd->bonus.near_attack_def_rate += val;
  2687. break;
  2688. case SP_LONG_ATK_DEF:
  2689. if(sd->state.lr_flag != 2)
  2690. sd->bonus.long_attack_def_rate += val;
  2691. break;
  2692. case SP_DOUBLE_RATE:
  2693. if(sd->state.lr_flag == 0 && sd->bonus.double_rate < val)
  2694. sd->bonus.double_rate = val;
  2695. break;
  2696. case SP_DOUBLE_ADD_RATE:
  2697. if(sd->state.lr_flag == 0)
  2698. sd->bonus.double_add_rate += val;
  2699. break;
  2700. case SP_MATK_RATE:
  2701. if(sd->state.lr_flag != 2)
  2702. sd->matk_rate += val;
  2703. break;
  2704. case SP_IGNORE_DEF_ELE:
  2705. PC_BONUS_CHK_ELEMENT(val,SP_IGNORE_DEF_ELE);
  2706. if(!sd->state.lr_flag)
  2707. sd->right_weapon.ignore_def_ele |= 1<<val;
  2708. else if(sd->state.lr_flag == 1)
  2709. sd->left_weapon.ignore_def_ele |= 1<<val;
  2710. break;
  2711. case SP_IGNORE_DEF_RACE:
  2712. PC_BONUS_CHK_RACE(val,SP_IGNORE_DEF_RACE);
  2713. if(!sd->state.lr_flag)
  2714. sd->right_weapon.ignore_def_race |= 1<<val;
  2715. else if(sd->state.lr_flag == 1)
  2716. sd->left_weapon.ignore_def_race |= 1<<val;
  2717. break;
  2718. case SP_IGNORE_DEF_CLASS:
  2719. PC_BONUS_CHK_CLASS(val,SP_IGNORE_DEF_CLASS);
  2720. if(!sd->state.lr_flag)
  2721. sd->right_weapon.ignore_def_class |= 1<<val;
  2722. else if(sd->state.lr_flag == 1)
  2723. sd->left_weapon.ignore_def_class |= 1<<val;
  2724. break;
  2725. case SP_ATK_RATE:
  2726. if(sd->state.lr_flag != 2)
  2727. sd->bonus.atk_rate += val;
  2728. break;
  2729. case SP_MAGIC_ATK_DEF:
  2730. if(sd->state.lr_flag != 2)
  2731. sd->bonus.magic_def_rate += val;
  2732. break;
  2733. case SP_MISC_ATK_DEF:
  2734. if(sd->state.lr_flag != 2)
  2735. sd->bonus.misc_def_rate += val;
  2736. break;
  2737. case SP_IGNORE_MDEF_ELE:
  2738. PC_BONUS_CHK_ELEMENT(val,SP_IGNORE_MDEF_ELE);
  2739. if(sd->state.lr_flag != 2)
  2740. sd->bonus.ignore_mdef_ele |= 1<<val;
  2741. break;
  2742. case SP_IGNORE_MDEF_RACE:
  2743. PC_BONUS_CHK_RACE(val,SP_IGNORE_MDEF_RACE);
  2744. if(sd->state.lr_flag != 2)
  2745. sd->bonus.ignore_mdef_race |= 1<<val;
  2746. break;
  2747. case SP_PERFECT_HIT_RATE:
  2748. if(sd->state.lr_flag != 2 && sd->bonus.perfect_hit < val)
  2749. sd->bonus.perfect_hit = val;
  2750. break;
  2751. case SP_PERFECT_HIT_ADD_RATE:
  2752. if(sd->state.lr_flag != 2)
  2753. sd->bonus.perfect_hit_add += val;
  2754. break;
  2755. case SP_CRITICAL_RATE:
  2756. if(sd->state.lr_flag != 2)
  2757. sd->critical_rate+=val;
  2758. break;
  2759. case SP_DEF_RATIO_ATK_ELE:
  2760. PC_BONUS_CHK_ELEMENT(val,SP_DEF_RATIO_ATK_ELE);
  2761. if(!sd->state.lr_flag)
  2762. sd->right_weapon.def_ratio_atk_ele |= 1<<val;
  2763. else if(sd->state.lr_flag == 1)
  2764. sd->left_weapon.def_ratio_atk_ele |= 1<<val;
  2765. break;
  2766. case SP_DEF_RATIO_ATK_RACE:
  2767. PC_BONUS_CHK_RACE(val,SP_DEF_RATIO_ATK_RACE);
  2768. if(!sd->state.lr_flag)
  2769. sd->right_weapon.def_ratio_atk_race |= 1<<val;
  2770. else if(sd->state.lr_flag == 1)
  2771. sd->left_weapon.def_ratio_atk_race |= 1<<val;
  2772. break;
  2773. case SP_DEF_RATIO_ATK_CLASS:
  2774. PC_BONUS_CHK_CLASS(val,SP_DEF_RATIO_ATK_CLASS);
  2775. if(!sd->state.lr_flag)
  2776. sd->right_weapon.def_ratio_atk_class |= 1<<val;
  2777. else if(sd->state.lr_flag == 1)
  2778. sd->left_weapon.def_ratio_atk_class |= 1<<val;
  2779. break;
  2780. case SP_HIT_RATE:
  2781. if(sd->state.lr_flag != 2)
  2782. sd->hit_rate += val;
  2783. break;
  2784. case SP_FLEE_RATE:
  2785. if(sd->state.lr_flag != 2)
  2786. sd->flee_rate += val;
  2787. break;
  2788. case SP_FLEE2_RATE:
  2789. if(sd->state.lr_flag != 2)
  2790. sd->flee2_rate += val;
  2791. break;
  2792. case SP_DEF_RATE:
  2793. if(sd->state.lr_flag != 2)
  2794. sd->def_rate += val;
  2795. break;
  2796. case SP_DEF2_RATE:
  2797. if(sd->state.lr_flag != 2)
  2798. sd->def2_rate += val;
  2799. break;
  2800. case SP_MDEF_RATE:
  2801. if(sd->state.lr_flag != 2)
  2802. sd->mdef_rate += val;
  2803. break;
  2804. case SP_MDEF2_RATE:
  2805. if(sd->state.lr_flag != 2)
  2806. sd->mdef2_rate += val;
  2807. break;
  2808. case SP_RESTART_FULL_RECOVER:
  2809. if(sd->state.lr_flag != 2)
  2810. sd->special_state.restart_full_recover = 1;
  2811. break;
  2812. case SP_NO_CASTCANCEL:
  2813. if(sd->state.lr_flag != 2)
  2814. sd->special_state.no_castcancel = 1;
  2815. break;
  2816. case SP_NO_CASTCANCEL2:
  2817. if(sd->state.lr_flag != 2)
  2818. sd->special_state.no_castcancel2 = 1;
  2819. break;
  2820. case SP_NO_SIZEFIX:
  2821. if(sd->state.lr_flag != 2)
  2822. sd->special_state.no_sizefix = 1;
  2823. break;
  2824. case SP_NO_MAGIC_DAMAGE:
  2825. if(sd->state.lr_flag == 2)
  2826. break;
  2827. val+= sd->special_state.no_magic_damage;
  2828. sd->special_state.no_magic_damage = cap_value(val,0,100);
  2829. break;
  2830. case SP_NO_WEAPON_DAMAGE:
  2831. if(sd->state.lr_flag == 2)
  2832. break;
  2833. val+= sd->special_state.no_weapon_damage;
  2834. sd->special_state.no_weapon_damage = cap_value(val,0,100);
  2835. break;
  2836. case SP_NO_MISC_DAMAGE:
  2837. if(sd->state.lr_flag == 2)
  2838. break;
  2839. val+= sd->special_state.no_misc_damage;
  2840. sd->special_state.no_misc_damage = cap_value(val,0,100);
  2841. break;
  2842. case SP_NO_GEMSTONE:
  2843. if(sd->state.lr_flag != 2 && sd->special_state.no_gemstone != 2)
  2844. sd->special_state.no_gemstone = 1;
  2845. break;
  2846. case SP_INTRAVISION: // Maya Purple Card effect allowing to see Hiding/Cloaking people [DracoRPG]
  2847. if(sd->state.lr_flag != 2) {
  2848. sd->special_state.intravision = 1;
  2849. clif_status_load(&sd->bl, EFST_CLAIRVOYANCE, 1);
  2850. }
  2851. break;
  2852. case SP_NO_KNOCKBACK:
  2853. if(sd->state.lr_flag != 2)
  2854. sd->special_state.no_knockback = 1;
  2855. break;
  2856. case SP_SPLASH_RANGE:
  2857. if(sd->bonus.splash_range < val)
  2858. sd->bonus.splash_range = val;
  2859. break;
  2860. case SP_SPLASH_ADD_RANGE:
  2861. sd->bonus.splash_add_range += val;
  2862. break;
  2863. case SP_SHORT_WEAPON_DAMAGE_RETURN:
  2864. if(sd->state.lr_flag != 2)
  2865. sd->bonus.short_weapon_damage_return += val;
  2866. break;
  2867. case SP_LONG_WEAPON_DAMAGE_RETURN:
  2868. if(sd->state.lr_flag != 2)
  2869. sd->bonus.long_weapon_damage_return += val;
  2870. break;
  2871. case SP_MAGIC_DAMAGE_RETURN: //AppleGirl Was Here
  2872. if(sd->state.lr_flag != 2)
  2873. sd->bonus.magic_damage_return += val;
  2874. break;
  2875. case SP_ALL_STATS: // [Valaris]
  2876. if(sd->state.lr_flag!=2) {
  2877. sd->param_bonus[SP_STR-SP_STR]+=val;
  2878. sd->param_bonus[SP_AGI-SP_STR]+=val;
  2879. sd->param_bonus[SP_VIT-SP_STR]+=val;
  2880. sd->param_bonus[SP_INT-SP_STR]+=val;
  2881. sd->param_bonus[SP_DEX-SP_STR]+=val;
  2882. sd->param_bonus[SP_LUK-SP_STR]+=val;
  2883. }
  2884. break;
  2885. case SP_AGI_VIT: // [Valaris]
  2886. if(sd->state.lr_flag!=2) {
  2887. sd->param_bonus[SP_AGI-SP_STR]+=val;
  2888. sd->param_bonus[SP_VIT-SP_STR]+=val;
  2889. }
  2890. break;
  2891. case SP_AGI_DEX_STR: // [Valaris]
  2892. if(sd->state.lr_flag!=2) {
  2893. sd->param_bonus[SP_AGI-SP_STR]+=val;
  2894. sd->param_bonus[SP_DEX-SP_STR]+=val;
  2895. sd->param_bonus[SP_STR-SP_STR]+=val;
  2896. }
  2897. break;
  2898. case SP_PERFECT_HIDE: // [Valaris]
  2899. if(sd->state.lr_flag!=2)
  2900. sd->special_state.perfect_hiding=1;
  2901. break;
  2902. case SP_UNBREAKABLE:
  2903. if(sd->state.lr_flag!=2)
  2904. sd->bonus.unbreakable += val;
  2905. break;
  2906. case SP_UNBREAKABLE_WEAPON:
  2907. if(sd->state.lr_flag != 2)
  2908. sd->bonus.unbreakable_equip |= EQP_WEAPON;
  2909. break;
  2910. case SP_UNBREAKABLE_ARMOR:
  2911. if(sd->state.lr_flag != 2)
  2912. sd->bonus.unbreakable_equip |= EQP_ARMOR;
  2913. break;
  2914. case SP_UNBREAKABLE_HELM:
  2915. if(sd->state.lr_flag != 2)
  2916. sd->bonus.unbreakable_equip |= EQP_HELM;
  2917. break;
  2918. case SP_UNBREAKABLE_SHIELD:
  2919. if(sd->state.lr_flag != 2)
  2920. sd->bonus.unbreakable_equip |= EQP_SHIELD;
  2921. break;
  2922. case SP_UNBREAKABLE_GARMENT:
  2923. if(sd->state.lr_flag != 2)
  2924. sd->bonus.unbreakable_equip |= EQP_GARMENT;
  2925. break;
  2926. case SP_UNBREAKABLE_SHOES:
  2927. if(sd->state.lr_flag != 2)
  2928. sd->bonus.unbreakable_equip |= EQP_SHOES;
  2929. break;
  2930. case SP_CLASSCHANGE: // [Valaris]
  2931. if(sd->state.lr_flag !=2)
  2932. sd->bonus.classchange=val;
  2933. break;
  2934. case SP_LONG_ATK_RATE:
  2935. if(sd->state.lr_flag != 2) //[Lupus] it should stack, too. As any other cards rate bonuses
  2936. sd->bonus.long_attack_atk_rate+=val;
  2937. break;
  2938. case SP_BREAK_WEAPON_RATE:
  2939. if(sd->state.lr_flag != 2)
  2940. sd->bonus.break_weapon_rate+=val;
  2941. break;
  2942. case SP_BREAK_ARMOR_RATE:
  2943. if(sd->state.lr_flag != 2)
  2944. sd->bonus.break_armor_rate+=val;
  2945. break;
  2946. case SP_ADD_STEAL_RATE:
  2947. if(sd->state.lr_flag != 2)
  2948. sd->bonus.add_steal_rate+=val;
  2949. break;
  2950. case SP_DELAYRATE:
  2951. if(sd->state.lr_flag != 2)
  2952. sd->delayrate+=val;
  2953. break;
  2954. case SP_CRIT_ATK_RATE:
  2955. if(sd->state.lr_flag != 2)
  2956. sd->bonus.crit_atk_rate += val;
  2957. break;
  2958. case SP_NO_REGEN:
  2959. if(sd->state.lr_flag != 2)
  2960. sd->regen.state.block|=val;
  2961. break;
  2962. case SP_UNSTRIPABLE_WEAPON:
  2963. if(sd->state.lr_flag != 2)
  2964. sd->bonus.unstripable_equip |= EQP_WEAPON;
  2965. break;
  2966. case SP_UNSTRIPABLE:
  2967. case SP_UNSTRIPABLE_ARMOR:
  2968. if(sd->state.lr_flag != 2)
  2969. sd->bonus.unstripable_equip |= EQP_ARMOR;
  2970. break;
  2971. case SP_UNSTRIPABLE_HELM:
  2972. if(sd->state.lr_flag != 2)
  2973. sd->bonus.unstripable_equip |= EQP_HELM;
  2974. break;
  2975. case SP_UNSTRIPABLE_SHIELD:
  2976. if(sd->state.lr_flag != 2)
  2977. sd->bonus.unstripable_equip |= EQP_SHIELD;
  2978. break;
  2979. case SP_HP_DRAIN_VALUE: // bonus bHPDrainValue,n;
  2980. if(!sd->state.lr_flag) {
  2981. sd->right_weapon.hp_drain_class[CLASS_NORMAL] += val;
  2982. sd->right_weapon.hp_drain_class[CLASS_BOSS] += val;
  2983. } else if(sd->state.lr_flag == 1) {
  2984. sd->left_weapon.hp_drain_class[CLASS_NORMAL] += val;
  2985. sd->left_weapon.hp_drain_class[CLASS_BOSS] += val;
  2986. }
  2987. break;
  2988. case SP_SP_DRAIN_VALUE: // bonus bSPDrainValue,n;
  2989. if(!sd->state.lr_flag) {
  2990. sd->right_weapon.sp_drain_class[CLASS_NORMAL] += val;
  2991. sd->right_weapon.sp_drain_class[CLASS_BOSS] += val;
  2992. } else if(sd->state.lr_flag == 1) {
  2993. sd->left_weapon.sp_drain_class[CLASS_NORMAL] += val;
  2994. sd->left_weapon.sp_drain_class[CLASS_BOSS] += val;
  2995. }
  2996. break;
  2997. case SP_SP_GAIN_VALUE:
  2998. if(!sd->state.lr_flag)
  2999. sd->bonus.sp_gain_value += val;
  3000. break;
  3001. case SP_HP_GAIN_VALUE:
  3002. if(!sd->state.lr_flag)
  3003. sd->bonus.hp_gain_value += val;
  3004. break;
  3005. case SP_LONG_SP_GAIN_VALUE:
  3006. if(!sd->state.lr_flag)
  3007. sd->bonus.long_sp_gain_value += val;
  3008. case SP_LONG_HP_GAIN_VALUE:
  3009. if(!sd->state.lr_flag)
  3010. sd->bonus.long_hp_gain_value += val;
  3011. break;
  3012. case SP_MAGIC_SP_GAIN_VALUE:
  3013. if(!sd->state.lr_flag)
  3014. sd->bonus.magic_sp_gain_value += val;
  3015. break;
  3016. case SP_MAGIC_HP_GAIN_VALUE:
  3017. if(!sd->state.lr_flag)
  3018. sd->bonus.magic_hp_gain_value += val;
  3019. break;
  3020. case SP_ADD_HEAL_RATE:
  3021. if(sd->state.lr_flag != 2)
  3022. sd->bonus.add_heal_rate += val;
  3023. break;
  3024. case SP_ADD_HEAL2_RATE:
  3025. if(sd->state.lr_flag != 2)
  3026. sd->bonus.add_heal2_rate += val;
  3027. break;
  3028. case SP_ADD_ITEM_HEAL_RATE:
  3029. if(sd->state.lr_flag != 2)
  3030. sd->bonus.itemhealrate2 += val;
  3031. break;
  3032. case SP_EMATK:
  3033. if(sd->state.lr_flag != 2)
  3034. sd->bonus.ematk += val;
  3035. break;
  3036. case SP_ADD_VARIABLECAST:
  3037. if (sd->state.lr_flag != 2)
  3038. sd->bonus.add_varcast += val;
  3039. break;
  3040. #ifdef RENEWAL_CAST
  3041. case SP_FIXCASTRATE:
  3042. if(sd->state.lr_flag != 2)
  3043. sd->bonus.fixcastrate = min(sd->bonus.fixcastrate,val);
  3044. break;
  3045. case SP_ADD_FIXEDCAST:
  3046. if(sd->state.lr_flag != 2)
  3047. sd->bonus.add_fixcast += val;
  3048. break;
  3049. case SP_CASTRATE:
  3050. case SP_VARCASTRATE:
  3051. if(sd->state.lr_flag != 2)
  3052. sd->bonus.varcastrate -= val;
  3053. break;
  3054. #else
  3055. case SP_ADD_FIXEDCAST:
  3056. case SP_FIXCASTRATE:
  3057. //ShowWarning("pc_bonus: non-RENEWAL_CAST doesn't support this bonus %d.\n", type);
  3058. break;
  3059. case SP_VARCASTRATE:
  3060. case SP_CASTRATE:
  3061. if(sd->state.lr_flag != 2)
  3062. sd->castrate += val;
  3063. break;
  3064. #endif
  3065. case SP_ADDMAXWEIGHT:
  3066. if (sd->state.lr_flag != 2)
  3067. sd->add_max_weight += val;
  3068. break;
  3069. case SP_ABSORB_DMG_MAXHP: // bonus bAbsorbDmgMaxHP,n;
  3070. sd->bonus.absorb_dmg_maxhp = max(sd->bonus.absorb_dmg_maxhp, val);
  3071. break;
  3072. case SP_CRITICAL_RANGEATK: // bonus bCriticalLong,n;
  3073. if (sd->state.lr_flag != 2)
  3074. sd->bonus.critical_rangeatk += val*10;
  3075. else
  3076. sd->bonus.arrow_cri += val*10;
  3077. break;
  3078. case SP_WEAPON_ATK_RATE:
  3079. if (sd->state.lr_flag != 2)
  3080. sd->bonus.weapon_atk_rate += val;
  3081. break;
  3082. case SP_WEAPON_MATK_RATE:
  3083. if (sd->state.lr_flag != 2)
  3084. sd->bonus.weapon_matk_rate += val;
  3085. break;
  3086. case SP_NO_MADO_FUEL:
  3087. if (sd->state.lr_flag != 2)
  3088. sd->special_state.no_mado_fuel = 1;
  3089. break;
  3090. case SP_NO_WALK_DELAY:
  3091. if (sd->state.lr_flag != 2)
  3092. sd->special_state.no_walk_delay = 1;
  3093. break;
  3094. default:
  3095. if (running_npc_stat_calc_event) {
  3096. ShowWarning("pc_bonus: unknown bonus type %d %d in OnPCStatCalcEvent!\n", type, val);
  3097. }
  3098. else if (current_equip_combo_pos > 0) {
  3099. ShowWarning("pc_bonus: unknown bonus type %d %d in a combo with item #%d\n", type, val, sd->inventory_data[pc_checkequip( sd, current_equip_combo_pos )]->nameid);
  3100. }
  3101. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  3102. ShowWarning("pc_bonus: unknown bonus type %d %d in item #%d\n", type, val, current_equip_card_id ? current_equip_card_id : sd->inventory_data[current_equip_item_index]->nameid);
  3103. }
  3104. else {
  3105. ShowWarning("pc_bonus: unknown bonus type %d %d in unknown usage. Report this!\n", type, val);
  3106. }
  3107. break;
  3108. }
  3109. }
  3110. /*==========================================
  3111. * Player bonus (type) with args type2 and val, called trough bonus2 (npc)
  3112. * format: bonus2 bBonusName,type2,val;
  3113. * @param sd
  3114. * @param type Bonus type used by bBonusName
  3115. * @param type2
  3116. * @param val Value that usually for rate or fixed value
  3117. *------------------------------------------*/
  3118. void pc_bonus2(struct map_session_data *sd,int type,int type2,int val)
  3119. {
  3120. nullpo_retv(sd);
  3121. switch(type){
  3122. case SP_ADDELE: // bonus2 bAddEle,e,x;
  3123. PC_BONUS_CHK_ELEMENT(type2,SP_ADDELE);
  3124. if(!sd->state.lr_flag || sd->state.lr_flag == 3)
  3125. sd->right_weapon.addele[type2]+=val;
  3126. else if(sd->state.lr_flag == 1)
  3127. sd->left_weapon.addele[type2]+=val;
  3128. else if(sd->state.lr_flag == 2)
  3129. sd->arrow_addele[type2]+=val;
  3130. break;
  3131. case SP_ADDRACE: // bonus2 bAddRace,r,x;
  3132. PC_BONUS_CHK_RACE(type2,SP_ADDRACE);
  3133. if(!sd->state.lr_flag || sd->state.lr_flag == 3)
  3134. sd->right_weapon.addrace[type2]+=val;
  3135. else if(sd->state.lr_flag == 1)
  3136. sd->left_weapon.addrace[type2]+=val;
  3137. else if(sd->state.lr_flag == 2)
  3138. sd->arrow_addrace[type2]+=val;
  3139. break;
  3140. case SP_ADDCLASS: // bonus2 bAddClass,c,x;
  3141. PC_BONUS_CHK_CLASS(type2,SP_ADDCLASS);
  3142. if(!sd->state.lr_flag || sd->state.lr_flag == 3)
  3143. sd->right_weapon.addclass[type2]+=val;
  3144. else if(sd->state.lr_flag == 1)
  3145. sd->left_weapon.addclass[type2]+=val;
  3146. else if(sd->state.lr_flag == 2)
  3147. sd->arrow_addclass[type2]+=val;
  3148. break;
  3149. case SP_ADDSIZE: // bonus2 bAddSize,s,x;
  3150. PC_BONUS_CHK_SIZE(type2,SP_ADDSIZE);
  3151. if(!sd->state.lr_flag || sd->state.lr_flag == 3)
  3152. sd->right_weapon.addsize[type2]+=val;
  3153. else if(sd->state.lr_flag == 1)
  3154. sd->left_weapon.addsize[type2]+=val;
  3155. else if(sd->state.lr_flag == 2)
  3156. sd->arrow_addsize[type2]+=val;
  3157. break;
  3158. case SP_SUBELE: // bonus2 bSubEle,e,x;
  3159. PC_BONUS_CHK_ELEMENT(type2,SP_SUBELE);
  3160. if(sd->state.lr_flag != 2)
  3161. sd->subele_script[type2] += val;
  3162. break;
  3163. case SP_SUBRACE: // bonus2 bSubRace,r,x;
  3164. PC_BONUS_CHK_RACE(type2,SP_SUBRACE);
  3165. if(sd->state.lr_flag != 2)
  3166. sd->subrace[type2]+=val;
  3167. break;
  3168. case SP_SUBCLASS: // bonus2 bSubClass,c,x;
  3169. PC_BONUS_CHK_CLASS(type2,SP_SUBCLASS);
  3170. if(sd->state.lr_flag != 2)
  3171. sd->subclass[type2]+=val;
  3172. break;
  3173. case SP_ADDEFF: // bonus2 bAddEff,eff,n;
  3174. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  3175. pc_bonus_addeff(sd->addeff, (sc_type)type2, sd->state.lr_flag != 2 ? val : 0, sd->state.lr_flag == 2 ? val : 0, 0, 0);
  3176. break;
  3177. case SP_ADDEFF2: // bonus2 bAddEff2,eff,n;
  3178. PC_BONUS_CHK_SC(type2,SP_ADDEFF2);
  3179. 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);
  3180. break;
  3181. case SP_RESEFF: // bonus2 bResEff,eff,n;
  3182. if(sd->state.lr_flag == 2)
  3183. break;
  3184. if (sd->reseff.size() == MAX_PC_BONUS) {
  3185. ShowWarning("pc_bonus2: SP_RESEFF: Reached max (%d) number of resistance bonuses per character!\n", MAX_PC_BONUS);
  3186. break;
  3187. }
  3188. pc_bonus_itembonus(sd->reseff, type2, val, true);
  3189. break;
  3190. case SP_MAGIC_ADDELE: // bonus2 bMagicAddEle,e,x;
  3191. PC_BONUS_CHK_ELEMENT(type2,SP_MAGIC_ADDELE);
  3192. if(sd->state.lr_flag != 2)
  3193. sd->magic_addele_script[type2] += val;
  3194. break;
  3195. case SP_MAGIC_ADDRACE: // bonus2 bMagicAddRace,r,x;
  3196. PC_BONUS_CHK_RACE(type2,SP_MAGIC_ADDRACE);
  3197. if(sd->state.lr_flag != 2)
  3198. sd->magic_addrace[type2]+=val;
  3199. break;
  3200. case SP_MAGIC_ADDCLASS: // bonus2 bMagicAddClass,c,x;
  3201. PC_BONUS_CHK_CLASS(type2,SP_MAGIC_ADDCLASS);
  3202. if(sd->state.lr_flag != 2)
  3203. sd->magic_addclass[type2]+=val;
  3204. break;
  3205. case SP_MAGIC_ADDSIZE: // bonus2 bMagicAddSize,s,x;
  3206. PC_BONUS_CHK_SIZE(type2,SP_MAGIC_ADDSIZE);
  3207. if(sd->state.lr_flag != 2)
  3208. sd->magic_addsize[type2]+=val;
  3209. break;
  3210. case SP_MAGIC_ATK_ELE: // bonus2 bMagicAtkEle,e,x;
  3211. PC_BONUS_CHK_ELEMENT(type2,SP_MAGIC_ATK_ELE);
  3212. if(sd->state.lr_flag != 2)
  3213. sd->magic_atk_ele[type2]+=val;
  3214. break;
  3215. case SP_ADD_DAMAGE_CLASS: { // bonus2 bAddDamageClass,mid,x;
  3216. struct weapon_data *wd = (sd->state.lr_flag ? &sd->left_weapon : &sd->right_weapon);
  3217. if (wd->add_dmg.size() == MAX_PC_BONUS) {
  3218. ShowWarning("pc_bonus2: SP_ADD_DAMAGE_CLASS: Reached max (%d) number of add class damage bonuses per character!\n", MAX_PC_BONUS);
  3219. break;
  3220. }
  3221. pc_bonus_itembonus(wd->add_dmg, type2, val, false);
  3222. }
  3223. break;
  3224. case SP_ADD_MAGIC_DAMAGE_CLASS: // bonus2 bAddMagicDamageClass,mid,x;
  3225. if(sd->state.lr_flag == 2)
  3226. break;
  3227. if (sd->add_mdmg.size() == MAX_PC_BONUS) {
  3228. ShowWarning("pc_bonus2: SP_ADD_MAGIC_DAMAGE_CLASS: Reached max (%d) number of add class magic dmg bonuses per character!\n", MAX_PC_BONUS);
  3229. break;
  3230. }
  3231. pc_bonus_itembonus(sd->add_mdmg, type2, val, false);
  3232. break;
  3233. case SP_ADD_DEF_MONSTER: // bonus2 bAddDefMonster,mid,x;
  3234. if(sd->state.lr_flag == 2)
  3235. break;
  3236. if (sd->add_def.size() == MAX_PC_BONUS) {
  3237. ShowWarning("pc_bonus2: SP_ADD_DEF_MONSTER: Reached max (%d) number of add class def bonuses per character!\n", MAX_PC_BONUS);
  3238. break;
  3239. }
  3240. pc_bonus_itembonus(sd->add_def, type2, val, false);
  3241. break;
  3242. case SP_ADD_MDEF_MONSTER: // bonus2 bAddMDefMonster,mid,x;
  3243. if(sd->state.lr_flag == 2)
  3244. break;
  3245. if (sd->add_mdef.size() == MAX_PC_BONUS) {
  3246. ShowWarning("pc_bonus2: SP_ADD_MDEF_MONSTER: Reached max (%d) number of add class mdef bonuses per character!\n", MAX_PC_BONUS);
  3247. break;
  3248. }
  3249. pc_bonus_itembonus(sd->add_mdef, type2, val, false);
  3250. break;
  3251. case SP_HP_DRAIN_RATE: // bonus2 bHPDrainRate,x,n;
  3252. if(!sd->state.lr_flag) {
  3253. sd->right_weapon.hp_drain_rate.rate += type2;
  3254. sd->right_weapon.hp_drain_rate.per += val;
  3255. }
  3256. else if(sd->state.lr_flag == 1) {
  3257. sd->left_weapon.hp_drain_rate.rate += type2;
  3258. sd->left_weapon.hp_drain_rate.per += val;
  3259. }
  3260. break;
  3261. case SP_SP_DRAIN_RATE: // bonus2 bSPDrainRate,x,n;
  3262. if(!sd->state.lr_flag) {
  3263. sd->right_weapon.sp_drain_rate.rate += type2;
  3264. sd->right_weapon.sp_drain_rate.per += val;
  3265. }
  3266. else if(sd->state.lr_flag == 1) {
  3267. sd->left_weapon.sp_drain_rate.rate += type2;
  3268. sd->left_weapon.sp_drain_rate.per += val;
  3269. }
  3270. break;
  3271. case SP_SP_VANISH_RATE: // bonus2 bSPVanishRate,x,n;
  3272. if(sd->state.lr_flag != 2) {
  3273. pc_bonus_addvanish(sd->sp_vanish, type2, val, BF_NORMAL);
  3274. }
  3275. break;
  3276. case SP_HP_VANISH_RATE: // bonus2 bHPVanishRate,x,n;
  3277. if(sd->state.lr_flag != 2) {
  3278. pc_bonus_addvanish(sd->hp_vanish, type2, val, BF_NORMAL);
  3279. }
  3280. break;
  3281. case SP_GET_ZENY_NUM: // bonus2 bGetZenyNum,x,n;
  3282. if(sd->state.lr_flag != 2 && sd->bonus.get_zeny_rate < val) {
  3283. sd->bonus.get_zeny_rate = val;
  3284. sd->bonus.get_zeny_num = type2;
  3285. }
  3286. break;
  3287. case SP_ADD_GET_ZENY_NUM: // bonus2 bAddGetZenyNum,x,n;
  3288. if(sd->state.lr_flag != 2) {
  3289. sd->bonus.get_zeny_rate += val;
  3290. sd->bonus.get_zeny_num += type2;
  3291. }
  3292. break;
  3293. case SP_WEAPON_COMA_ELE: // bonus2 bWeaponComaEle,e,n;
  3294. PC_BONUS_CHK_ELEMENT(type2,SP_WEAPON_COMA_ELE);
  3295. if(sd->state.lr_flag == 2)
  3296. break;
  3297. sd->weapon_coma_ele[type2] += val;
  3298. sd->special_state.bonus_coma = 1;
  3299. break;
  3300. case SP_WEAPON_COMA_RACE: // bonus2 bWeaponComaRace,r,n;
  3301. PC_BONUS_CHK_RACE(type2,SP_WEAPON_COMA_RACE);
  3302. if(sd->state.lr_flag == 2)
  3303. break;
  3304. sd->weapon_coma_race[type2] += val;
  3305. sd->special_state.bonus_coma = 1;
  3306. break;
  3307. case SP_WEAPON_COMA_CLASS: // bonus2 bWeaponComaClass,c,n;
  3308. PC_BONUS_CHK_CLASS(type2,SP_WEAPON_COMA_CLASS);
  3309. if(sd->state.lr_flag == 2)
  3310. break;
  3311. sd->weapon_coma_class[type2] += val;
  3312. sd->special_state.bonus_coma = 1;
  3313. break;
  3314. case SP_WEAPON_ATK: // bonus2 bWeaponAtk,w,n;
  3315. if(sd->state.lr_flag != 2)
  3316. sd->weapon_atk[type2]+=val;
  3317. break;
  3318. case SP_WEAPON_DAMAGE_RATE: // bonus2 bWeaponDamageRate,w,n;
  3319. if(sd->state.lr_flag != 2)
  3320. sd->weapon_damage_rate[type2]+=val;
  3321. break;
  3322. case SP_CRITICAL_ADDRACE: // bonus2 bCriticalAddRace,r,n;
  3323. PC_BONUS_CHK_RACE(type2,SP_CRITICAL_ADDRACE);
  3324. if(sd->state.lr_flag != 2)
  3325. sd->critaddrace[type2] += val*10;
  3326. break;
  3327. case SP_ADDEFF_WHENHIT: // bonus2 bAddEffWhenHit,eff,n;
  3328. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  3329. if(sd->state.lr_flag != 2)
  3330. pc_bonus_addeff(sd->addeff_atked, (sc_type)type2, val, 0, 0, 0);
  3331. break;
  3332. case SP_SKILL_ATK: // bonus2 bSkillAtk,sk,n;
  3333. if(sd->state.lr_flag == 2)
  3334. break;
  3335. if (sd->skillatk.size() == MAX_PC_BONUS) {
  3336. 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);
  3337. break;
  3338. }
  3339. pc_bonus_itembonus(sd->skillatk, type2, val, false);
  3340. break;
  3341. case SP_SKILL_HEAL: // bonus2 bSkillHeal,sk,n;
  3342. if(sd->state.lr_flag == 2)
  3343. break;
  3344. if (sd->skillheal.size() == MAX_PC_BONUS) { // Better mention this so the array length can be updated. [Skotlex]
  3345. 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);
  3346. break;
  3347. }
  3348. pc_bonus_itembonus(sd->skillheal, type2, val, false);
  3349. break;
  3350. case SP_SKILL_HEAL2: // bonus2 bSkillHeal2,sk,n;
  3351. if(sd->state.lr_flag == 2)
  3352. break;
  3353. if (sd->skillheal2.size() == MAX_PC_BONUS) { // Better mention this so the array length can be updated. [Skotlex]
  3354. 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);
  3355. break;
  3356. }
  3357. pc_bonus_itembonus(sd->skillheal2, type2, val, false);
  3358. break;
  3359. case SP_ADD_SKILL_BLOW: // bonus2 bAddSkillBlow,sk,n;
  3360. if(sd->state.lr_flag == 2)
  3361. break;
  3362. if (sd->skillblown.size() == MAX_PC_BONUS) { //Better mention this so the array length can be updated. [Skotlex]
  3363. 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);
  3364. break;
  3365. }
  3366. pc_bonus_itembonus(sd->skillblown, type2, val, false);
  3367. break;
  3368. case SP_HP_LOSS_RATE: // bonus2 bHPLossRate,n,t;
  3369. if(sd->state.lr_flag != 2) {
  3370. sd->hp_loss.value = type2;
  3371. sd->hp_loss.rate = val;
  3372. }
  3373. break;
  3374. case SP_HP_REGEN_RATE: // bonus2 bHPRegenRate,n,t;
  3375. if(sd->state.lr_flag != 2) {
  3376. sd->hp_regen.value = type2;
  3377. sd->hp_regen.rate = val;
  3378. }
  3379. break;
  3380. case SP_REGEN_PERCENT_HP: // bonus2 bRegenPercentHP,n,t;
  3381. if (sd->state.lr_flag != 2) {
  3382. sd->percent_hp_regen.value = type2;
  3383. sd->percent_hp_regen.rate = val;
  3384. }
  3385. break;
  3386. case SP_REGEN_PERCENT_SP: // bonus2 bRegenPercentSP,n,t;
  3387. if (sd->state.lr_flag != 2) {
  3388. sd->percent_sp_regen.value = type2;
  3389. sd->percent_sp_regen.rate = val;
  3390. }
  3391. break;
  3392. case SP_ADDRACE2: // bonus2 bAddRace2,mr,x;
  3393. PC_BONUS_CHK_RACE2(type2,SP_ADDRACE2);
  3394. if(sd->state.lr_flag != 2)
  3395. sd->right_weapon.addrace2[type2] += val;
  3396. else
  3397. sd->left_weapon.addrace2[type2] += val;
  3398. break;
  3399. case SP_SUBSIZE: // bonus2 bSubSize,s,x;
  3400. PC_BONUS_CHK_SIZE(type2,SP_SUBSIZE);
  3401. if(sd->state.lr_flag != 2)
  3402. sd->subsize[type2]+=val;
  3403. break;
  3404. case SP_SUBRACE2: // bonus2 bSubRace2,mr,x;
  3405. PC_BONUS_CHK_RACE2(type2,SP_SUBRACE2);
  3406. if(sd->state.lr_flag != 2)
  3407. sd->subrace2[type2]+=val;
  3408. break;
  3409. case SP_ADD_ITEM_HEAL_RATE: // bonus2 bAddItemHealRate,iid,n;
  3410. if(sd->state.lr_flag == 2)
  3411. break;
  3412. if (!itemdb_exists(type2)) {
  3413. ShowWarning("pc_bonus2: SP_ADD_ITEM_HEAL_RATE Invalid item with id %d\n", type2);
  3414. break;
  3415. }
  3416. if (sd->itemhealrate.size() == MAX_PC_BONUS) {
  3417. ShowWarning("pc_bonus2: SP_ADD_ITEM_HEAL_RATE: Reached max (%d) number of item heal bonuses per character!\n", MAX_PC_BONUS);
  3418. break;
  3419. }
  3420. pc_bonus_itembonus(sd->itemhealrate, type2, val, false);
  3421. break;
  3422. case SP_ADD_ITEMGROUP_HEAL_RATE: // bonus2 bAddItemGroupHealRate,ig,n;
  3423. if (sd->state.lr_flag == 2)
  3424. break;
  3425. if (!type2 || !itemdb_group_exists(type2)) {
  3426. ShowWarning("pc_bonus2: SP_ADD_ITEMGROUP_HEAL_RATE: Invalid item group with id %d\n", type2);
  3427. break;
  3428. }
  3429. if (sd->itemhealrate.size() == MAX_PC_BONUS) {
  3430. ShowWarning("pc_bonus2: SP_ADD_ITEMGROUP_HEAL_RATE: Reached max (%d) number of item heal bonuses per character!\n", MAX_PC_BONUS);
  3431. break;
  3432. }
  3433. pc_bonus_itembonus(sd->itemgrouphealrate, type2, val, false);
  3434. break;
  3435. case SP_EXP_ADDRACE: // bonus2 bExpAddRace,r,x;
  3436. PC_BONUS_CHK_RACE(type2,SP_EXP_ADDRACE);
  3437. if(sd->state.lr_flag != 2)
  3438. sd->expaddrace[type2]+=val;
  3439. break;
  3440. case SP_EXP_ADDCLASS: // bonus2 bExpAddClass,c,x;
  3441. PC_BONUS_CHK_CLASS(type2,SP_EXP_ADDCLASS);
  3442. if(sd->state.lr_flag != 2)
  3443. sd->expaddclass[type2]+=val;
  3444. break;
  3445. case SP_SP_GAIN_RACE: // bonus2 bSPGainRace,r,n;
  3446. PC_BONUS_CHK_RACE(type2,SP_SP_GAIN_RACE);
  3447. if(sd->state.lr_flag != 2)
  3448. sd->sp_gain_race[type2]+=val;
  3449. break;
  3450. case SP_ADD_MONSTER_DROP_ITEM: // bonus2 bAddMonsterDropItem,iid,n;
  3451. if (sd->state.lr_flag != 2)
  3452. pc_bonus_item_drop(sd->add_drop, type2, 0, CLASS_ALL, RC_NONE_, val);
  3453. break;
  3454. case SP_ADD_MONSTER_DROP_ITEMGROUP: // bonus2 bAddMonsterDropItemGroup,ig,n;
  3455. if (sd->state.lr_flag != 2)
  3456. pc_bonus_item_drop(sd->add_drop, 0, type2, CLASS_ALL, RC_NONE_, val);
  3457. break;
  3458. case SP_SP_LOSS_RATE: // bonus2 bSPLossRate,n,t;
  3459. if(sd->state.lr_flag != 2) {
  3460. sd->sp_loss.value = type2;
  3461. sd->sp_loss.rate = val;
  3462. }
  3463. break;
  3464. case SP_SP_REGEN_RATE: // bonus2 bSPRegenRate,n,t;
  3465. if(sd->state.lr_flag != 2) {
  3466. sd->sp_regen.value = type2;
  3467. sd->sp_regen.rate = val;
  3468. }
  3469. break;
  3470. case SP_HP_DRAIN_VALUE_RACE: // bonus2 bHPDrainValueRace,r,n;
  3471. PC_BONUS_CHK_RACE(type2,SP_HP_DRAIN_VALUE_RACE);
  3472. if(!sd->state.lr_flag) {
  3473. sd->right_weapon.hp_drain_race[type2] += val;
  3474. }
  3475. else if(sd->state.lr_flag == 1) {
  3476. sd->left_weapon.hp_drain_race[type2] += val;
  3477. }
  3478. break;
  3479. case SP_SP_DRAIN_VALUE_RACE: // bonus2 bSPDrainValueRace,r,n;
  3480. PC_BONUS_CHK_RACE(type2,SP_SP_DRAIN_VALUE_RACE);
  3481. if(!sd->state.lr_flag) {
  3482. sd->right_weapon.sp_drain_race[type2] += val;
  3483. }
  3484. else if(sd->state.lr_flag == 1) {
  3485. sd->left_weapon.sp_drain_race[type2] += val;
  3486. }
  3487. break;
  3488. case SP_HP_DRAIN_VALUE_CLASS: // bonus2 bHPDrainValueClass,c,n;
  3489. PC_BONUS_CHK_CLASS(type2,SP_HP_DRAIN_VALUE_CLASS);
  3490. if(!sd->state.lr_flag) {
  3491. sd->right_weapon.hp_drain_class[type2] += val;
  3492. }
  3493. else if(sd->state.lr_flag == 1) {
  3494. sd->left_weapon.hp_drain_class[type2] += val;
  3495. }
  3496. break;
  3497. case SP_SP_DRAIN_VALUE_CLASS: // bonus2 bSPDrainValueClass,c,n;
  3498. PC_BONUS_CHK_CLASS(type2,SP_SP_DRAIN_VALUE_CLASS);
  3499. if(!sd->state.lr_flag) {
  3500. sd->right_weapon.sp_drain_class[type2] += val;
  3501. }
  3502. else if(sd->state.lr_flag == 1) {
  3503. sd->left_weapon.sp_drain_class[type2] += val;
  3504. }
  3505. break;
  3506. case SP_IGNORE_MDEF_RACE_RATE: // bonus2 bIgnoreMdefRaceRate,r,n;
  3507. PC_BONUS_CHK_RACE(type2,SP_IGNORE_MDEF_RACE_RATE);
  3508. if(sd->state.lr_flag != 2)
  3509. sd->ignore_mdef_by_race[type2] += val;
  3510. break;
  3511. case SP_IGNORE_MDEF_CLASS_RATE: // bonus2 bIgnoreMdefClassRate,c,n;
  3512. PC_BONUS_CHK_CLASS(type2,SP_IGNORE_MDEF_CLASS_RATE);
  3513. if(sd->state.lr_flag != 2)
  3514. sd->ignore_mdef_by_class[type2] += val;
  3515. break;
  3516. case SP_IGNORE_DEF_RACE_RATE: // bonus2 bIgnoreDefRaceRate,r,n;
  3517. PC_BONUS_CHK_RACE(type2,SP_IGNORE_DEF_RACE_RATE);
  3518. if(sd->state.lr_flag != 2)
  3519. sd->ignore_def_by_race[type2] += val;
  3520. break;
  3521. case SP_IGNORE_DEF_CLASS_RATE: // bonus2 bIgnoreDefClassRate,r,n;
  3522. PC_BONUS_CHK_CLASS(type2, SP_IGNORE_DEF_CLASS_RATE);
  3523. if (sd->state.lr_flag != 2)
  3524. sd->ignore_def_by_class[type2] += val;
  3525. break;
  3526. case SP_SKILL_USE_SP_RATE: // bonus2 bSkillUseSPrate,sk,n;
  3527. if(sd->state.lr_flag == 2)
  3528. break;
  3529. if (sd->skillusesprate.size() == MAX_PC_BONUS) {
  3530. 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);
  3531. break;
  3532. }
  3533. pc_bonus_itembonus(sd->skillusesprate, type2, val, true);
  3534. break;
  3535. case SP_SKILL_DELAY:
  3536. if(sd->state.lr_flag == 2)
  3537. break;
  3538. if (sd->skilldelay.size() == MAX_PC_BONUS) {
  3539. 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);
  3540. break;
  3541. }
  3542. pc_bonus_itembonus(sd->skilldelay, type2, val, false);
  3543. break;
  3544. case SP_SKILL_COOLDOWN: // bonus2 bSkillCooldown,sk,t;
  3545. if(sd->state.lr_flag == 2)
  3546. break;
  3547. if (sd->skillcooldown.size() == MAX_PC_BONUS) {
  3548. 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);
  3549. break;
  3550. }
  3551. pc_bonus_itembonus(sd->skillcooldown, type2, val, false);
  3552. break;
  3553. case SP_SKILL_VARIABLECAST: // bonus2 bSkillVariableCast,sk,t;
  3554. if (sd->state.lr_flag == 2)
  3555. break;
  3556. if (sd->skillvarcast.size() == MAX_PC_BONUS) {
  3557. 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);
  3558. break;
  3559. }
  3560. pc_bonus_itembonus(sd->skillvarcast, type2, val, false);
  3561. break;
  3562. #ifdef RENEWAL_CAST
  3563. case SP_SKILL_FIXEDCAST: // bonus2 bSkillFixedCast,sk,t;
  3564. if(sd->state.lr_flag == 2)
  3565. break;
  3566. if (sd->skillfixcast.size() == MAX_PC_BONUS) {
  3567. 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);
  3568. break;
  3569. }
  3570. pc_bonus_itembonus(sd->skillfixcast, type2, val, false);
  3571. break;
  3572. case SP_CASTRATE: // bonus2 bCastrate,sk,n;
  3573. case SP_VARCASTRATE: // bonus2 bVariableCastrate,sk,n;
  3574. if(sd->state.lr_flag == 2)
  3575. break;
  3576. if (sd->skillcastrate.size() == MAX_PC_BONUS) {
  3577. ShowWarning("pc_bonus2: SP_VARCASTRATE: Reached max (%d) number of skills per character, bonus skill %d (%d%%) lost.\n", MAX_PC_BONUS, type2, val);
  3578. break;
  3579. }
  3580. pc_bonus_itembonus(sd->skillcastrate, type2, -val, true); // Send inversed value here
  3581. break;
  3582. case SP_FIXCASTRATE: // bonus2 bFixedCastrate,sk,n;
  3583. if(sd->state.lr_flag == 2)
  3584. break;
  3585. if (sd->skillfixcastrate.size() == MAX_PC_BONUS) {
  3586. ShowWarning("pc_bonus2: SP_FIXCASTRATE: Reached max (%d) number of skills per character, bonus skill %d (%d%%) lost.\n", MAX_PC_BONUS, type2, val);
  3587. break;
  3588. }
  3589. pc_bonus_itembonus(sd->skillfixcastrate, type2, -val, true); // Send inversed value here
  3590. break;
  3591. #else
  3592. case SP_SKILL_FIXEDCAST: // bonus2 bSkillFixedCast,sk,t;
  3593. case SP_FIXCASTRATE: // bonus2 bFixedCastrate,sk,n;
  3594. //ShowWarning("pc_bonus2: Non-RENEWAL_CAST doesn't support this bonus %d.\n", type);
  3595. break;
  3596. case SP_VARCASTRATE: // bonus2 bVariableCastrate,sk,n;
  3597. case SP_CASTRATE: // bonus2 bCastrate,sk,n;
  3598. if(sd->state.lr_flag == 2)
  3599. break;
  3600. if (sd->skillcastrate.size() == MAX_PC_BONUS) { //Better mention this so the array length can be updated. [Skotlex]
  3601. 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);
  3602. break;
  3603. }
  3604. pc_bonus_itembonus(sd->skillcastrate, type2, val, true);
  3605. break;
  3606. #endif
  3607. case SP_SKILL_USE_SP: // bonus2 bSkillUseSP,sk,n;
  3608. if(sd->state.lr_flag == 2)
  3609. break;
  3610. if (sd->skillusesp.size() == MAX_PC_BONUS) {
  3611. 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);
  3612. break;
  3613. }
  3614. pc_bonus_itembonus(sd->skillusesp, type2, val, false);
  3615. break;
  3616. case SP_SUB_SKILL: // bonus2 bSubSkill,sk,n;
  3617. if (sd->subskill.size() == MAX_PC_BONUS) {
  3618. 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);
  3619. break;
  3620. }
  3621. pc_bonus_itembonus(sd->subskill, type2, val, false);
  3622. break;
  3623. case SP_SUBDEF_ELE: // bonus2 bSubDefEle,e,x;
  3624. PC_BONUS_CHK_ELEMENT(type2,SP_SUBDEF_ELE);
  3625. sd->subdefele[type2] += val;
  3626. break;
  3627. case SP_COMA_CLASS: // bonus2 bComaClass,c,n;
  3628. PC_BONUS_CHK_CLASS(type2,SP_COMA_CLASS);
  3629. sd->coma_class[type2] += val;
  3630. sd->special_state.bonus_coma = 1;
  3631. break;
  3632. case SP_COMA_RACE: // bonus2 bComaRace,r,n;
  3633. PC_BONUS_CHK_RACE(type2,SP_COMA_RACE);
  3634. sd->coma_race[type2] += val;
  3635. sd->special_state.bonus_coma = 1;
  3636. break;
  3637. case SP_MAGIC_ADDRACE2: // bonus2 bMagicAddRace2,mr,n;
  3638. PC_BONUS_CHK_RACE2(type2, SP_MAGIC_ADDRACE2);
  3639. if(sd->state.lr_flag != 2)
  3640. sd->magic_addrace2[type2] += val;
  3641. break;
  3642. case SP_IGNORE_MDEF_RACE2_RATE: //bonus2 bIgnoreMdefRace2Rate,mr,n;
  3643. PC_BONUS_CHK_RACE2(type2, SP_IGNORE_MDEF_RACE2);
  3644. if (sd->state.lr_flag != 2)
  3645. sd->ignore_mdef_by_race2[type2] += val;
  3646. break;
  3647. case SP_DROP_ADDRACE: // bonus2 bDropAddRace,r,x;
  3648. PC_BONUS_CHK_RACE(type2, SP_DROP_ADDRACE);
  3649. if (sd->state.lr_flag != 2)
  3650. sd->dropaddrace[type2] += val;
  3651. break;
  3652. case SP_DROP_ADDCLASS: // bonus2 bDropAddClass,c,x;
  3653. PC_BONUS_CHK_CLASS(type2, SP_DROP_ADDCLASS);
  3654. if (sd->state.lr_flag != 2)
  3655. sd->dropaddclass[type2] += val;
  3656. break;
  3657. default:
  3658. if (running_npc_stat_calc_event) {
  3659. ShowWarning("pc_bonus2: unknown bonus type %d %d %d in OnPCStatCalcEvent!\n", type, type2, val);
  3660. }
  3661. else if (current_equip_combo_pos > 0) {
  3662. ShowWarning("pc_bonus2: unknown bonus type %d %d %d in a combo with item #%d\n", type, type2, val, sd->inventory_data[pc_checkequip( sd, current_equip_combo_pos )]->nameid);
  3663. }
  3664. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  3665. ShowWarning("pc_bonus2: unknown bonus type %d %d %d in item #%d\n", type, type2, val, current_equip_card_id ? current_equip_card_id : sd->inventory_data[current_equip_item_index]->nameid);
  3666. }
  3667. else {
  3668. ShowWarning("pc_bonus2: unknown bonus type %d %d %d in unknown usage. Report this!\n", type, type2, val);
  3669. }
  3670. break;
  3671. }
  3672. }
  3673. /**
  3674. * Gives item bonus to player for format: bonus3 bBonusName,type2,type3,val;
  3675. * @param sd
  3676. * @param type Bonus type used by bBonusName
  3677. * @param type2
  3678. * @param type3
  3679. * @param val Value that usually for rate or fixed value
  3680. */
  3681. void pc_bonus3(struct map_session_data *sd,int type,int type2,int type3,int val)
  3682. {
  3683. nullpo_retv(sd);
  3684. switch(type){
  3685. case SP_ADD_MONSTER_DROP_ITEM: // bonus3 bAddMonsterDropItem,iid,r,n;
  3686. if(sd->state.lr_flag != 2)
  3687. pc_bonus_item_drop(sd->add_drop, type2, 0, CLASS_NONE, type3, val);
  3688. break;
  3689. case SP_ADD_MONSTER_ID_DROP_ITEM: // bonus3 bAddMonsterIdDropItem,iid,mid,n;
  3690. if(sd->state.lr_flag != 2)
  3691. pc_bonus_item_drop(sd->add_drop, type2, 0, CLASS_NONE, -type3, val);
  3692. break;
  3693. case SP_ADD_CLASS_DROP_ITEM: // bonus3 bAddClassDropItem,iid,c,n;
  3694. if(sd->state.lr_flag != 2)
  3695. pc_bonus_item_drop(sd->add_drop, type2, 0, type3, RC_NONE_, val);
  3696. break;
  3697. case SP_AUTOSPELL: // bonus3 bAutoSpell,sk,y,n;
  3698. if(sd->state.lr_flag != 2)
  3699. {
  3700. int target = skill_get_inf(type2); //Support or Self (non-auto-target) skills should pick self.
  3701. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !skill_get_inf2(type2, INF2_NOTARGETSELF));
  3702. pc_bonus_autospell(sd->autospell, target?-type2:type2, type3, val, 0, current_equip_card_id);
  3703. }
  3704. break;
  3705. case SP_AUTOSPELL_WHENHIT: // bonus3 bAutoSpellWhenHit,sk,y,n;
  3706. if(sd->state.lr_flag != 2)
  3707. {
  3708. int target = skill_get_inf(type2); //Support or Self (non-auto-target) skills should pick self.
  3709. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !skill_get_inf2(type2, INF2_NOTARGETSELF));
  3710. pc_bonus_autospell(sd->autospell2, target?-type2:type2, type3, val, BF_NORMAL|BF_SKILL, current_equip_card_id);
  3711. }
  3712. break;
  3713. case SP_ADD_MONSTER_DROP_ITEMGROUP: // bonus3 bAddMonsterDropItemGroup,ig,r,n;
  3714. if (sd->state.lr_flag != 2)
  3715. pc_bonus_item_drop(sd->add_drop, 0, type2, CLASS_NONE, type3, val);
  3716. break;
  3717. case SP_ADD_CLASS_DROP_ITEMGROUP: // bonus3 bAddClassDropItemGroup,ig,c,n;
  3718. if (sd->state.lr_flag != 2)
  3719. pc_bonus_item_drop(sd->add_drop, 0, type2, type3, RC_NONE_, val);
  3720. break;
  3721. case SP_ADDEFF: // bonus3 bAddEff,eff,n,y;
  3722. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  3723. pc_bonus_addeff(sd->addeff, (sc_type)type2, sd->state.lr_flag != 2 ? type3 : 0, sd->state.lr_flag == 2 ? type3 : 0, val, 0);
  3724. break;
  3725. case SP_ADDEFF_WHENHIT: // bonus3 bAddEffWhenHit,eff,n,y;
  3726. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  3727. if(sd->state.lr_flag != 2)
  3728. pc_bonus_addeff(sd->addeff_atked, (sc_type)type2, type3, 0, val, 0);
  3729. break;
  3730. case SP_ADDEFF_ONSKILL: // bonus3 bAddEffOnSkill,sk,eff,n;
  3731. PC_BONUS_CHK_SC(type2,SP_ADDEFF_ONSKILL);
  3732. if( sd->state.lr_flag != 2 )
  3733. pc_bonus_addeff_onskill(sd->addeff_onskill, (sc_type)type3, val, type2, ATF_TARGET, 0);
  3734. break;
  3735. case SP_ADDELE: // bonus3 bAddEle,e,x,bf;
  3736. PC_BONUS_CHK_ELEMENT(type2,SP_ADDELE);
  3737. if (sd->state.lr_flag != 2)
  3738. pc_bonus_addele(sd, (unsigned char)type2, type3, val);
  3739. break;
  3740. case SP_SUBELE: // bonus3 bSubEle,e,x,bf;
  3741. PC_BONUS_CHK_ELEMENT(type2,SP_SUBELE);
  3742. if (sd->state.lr_flag != 2)
  3743. pc_bonus_subele(sd, (unsigned char)type2, type3, val);
  3744. break;
  3745. case SP_SP_VANISH_RACE_RATE: // bonus3 bSPVanishRaceRate,r,x,n;
  3746. PC_BONUS_CHK_RACE(type2,SP_SP_VANISH_RACE_RATE);
  3747. if(sd->state.lr_flag != 2) {
  3748. sd->sp_vanish_race[type2].rate += type3;
  3749. sd->sp_vanish_race[type2].per += val;
  3750. }
  3751. break;
  3752. case SP_HP_VANISH_RACE_RATE: // bonus3 bHPVanishRaceRate,r,x,n;
  3753. PC_BONUS_CHK_RACE(type2,SP_HP_VANISH_RACE_RATE);
  3754. if(sd->state.lr_flag != 2) {
  3755. sd->hp_vanish_race[type2].rate += type3;
  3756. sd->hp_vanish_race[type2].per += val;
  3757. }
  3758. break;
  3759. case SP_SP_VANISH_RATE: // bonus3 bSPVanishRate,x,n,bf;
  3760. if(sd->state.lr_flag != 2) {
  3761. pc_bonus_addvanish(sd->sp_vanish, type2, type3, val);
  3762. }
  3763. break;
  3764. case SP_HP_VANISH_RATE: // bonus3 bHPVanishRate,x,n,bf;
  3765. if(sd->state.lr_flag != 2) {
  3766. pc_bonus_addvanish(sd->hp_vanish, type2, type3, val);
  3767. }
  3768. break;
  3769. case SP_STATE_NORECOVER_RACE: // bonus3 bStateNoRecoverRace,r,x,t;
  3770. PC_BONUS_CHK_RACE(type2, SP_STATE_NORECOVER_RACE);
  3771. if (sd->state.lr_flag == 2)
  3772. break;
  3773. //! CONFIRM: Is it not stackable? Does not check max or min value?
  3774. //if (type3 > sd->norecover_state_race[type2].rate) {
  3775. // sd->norecover_state_race[type2].rate = type3;
  3776. // sd->norecover_state_race[type2].tick = val;
  3777. // break;
  3778. //}
  3779. sd->norecover_state_race[type2].rate = type3;
  3780. sd->norecover_state_race[type2].tick = val;
  3781. break;
  3782. default:
  3783. if (running_npc_stat_calc_event) {
  3784. ShowWarning("pc_bonus3: unknown bonus type %d %d %d %d in OnPCStatCalcEvent!\n", type, type2, type3, val);
  3785. }
  3786. else if (current_equip_combo_pos > 0) {
  3787. ShowWarning("pc_bonus3: unknown bonus type %d %d %d %d in a combo with item #%d\n", type, type2, type3, val, sd->inventory_data[pc_checkequip( sd, current_equip_combo_pos )]->nameid);
  3788. }
  3789. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  3790. ShowWarning("pc_bonus3: unknown bonus type %d %d %d %d in item #%d\n", type, type2, type3, val, current_equip_card_id ? current_equip_card_id : sd->inventory_data[current_equip_item_index]->nameid);
  3791. }
  3792. else {
  3793. ShowWarning("pc_bonus3: unknown bonus type %d %d %d %d in unknown usage. Report this!\n", type, type2, type3, val);
  3794. }
  3795. break;
  3796. }
  3797. }
  3798. /**
  3799. * Gives item bonus to player for format: bonus4 bBonusName,type2,type3,type4,val;
  3800. * @param sd
  3801. * @param type Bonus type used by bBonusName
  3802. * @param type2
  3803. * @param type3
  3804. * @param type4
  3805. * @param val Value that usually for rate or fixed value
  3806. */
  3807. void pc_bonus4(struct map_session_data *sd,int type,int type2,int type3,int type4,int val)
  3808. {
  3809. nullpo_retv(sd);
  3810. switch(type){
  3811. case SP_AUTOSPELL: // bonus4 bAutoSpell,sk,y,n,i;
  3812. if(sd->state.lr_flag != 2)
  3813. pc_bonus_autospell(sd->autospell, (val&1?type2:-type2), (val&2?-type3:type3), type4, 0, current_equip_card_id);
  3814. break;
  3815. case SP_AUTOSPELL_WHENHIT: // bonus4 bAutoSpellWhenHit,sk,y,n,i;
  3816. if(sd->state.lr_flag != 2)
  3817. pc_bonus_autospell(sd->autospell2, (val&1?type2:-type2), (val&2?-type3:type3), type4, BF_NORMAL|BF_SKILL, current_equip_card_id);
  3818. break;
  3819. case SP_AUTOSPELL_ONSKILL: // bonus4 bAutoSpellOnSkill,sk,x,y,n;
  3820. if(sd->state.lr_flag != 2)
  3821. {
  3822. int target = skill_get_inf(type3); //Support or Self (non-auto-target) skills should pick self.
  3823. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !skill_get_inf2(type3, INF2_NOTARGETSELF));
  3824. pc_bonus_autospell_onskill(sd->autospell3, type2, target?-type3:type3, type4, val, current_equip_card_id);
  3825. }
  3826. break;
  3827. case SP_ADDEFF: // bonus4 bAddEff,eff,n,y,t;
  3828. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  3829. pc_bonus_addeff(sd->addeff, (sc_type)type2, sd->state.lr_flag != 2 ? type3 : 0, sd->state.lr_flag == 2 ? type3 : 0, type4, val);
  3830. break;
  3831. case SP_ADDEFF_WHENHIT: // bonus4 bAddEffWhenHit,eff,n,y,t;
  3832. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  3833. if (sd->state.lr_flag != 2)
  3834. pc_bonus_addeff(sd->addeff_atked, (sc_type)type2, type3, 0, type4, val);
  3835. break;
  3836. case SP_ADDEFF_ONSKILL: // bonus4 bAddEffOnSkill,sk,eff,n,y;
  3837. PC_BONUS_CHK_SC(type3,SP_ADDEFF_ONSKILL);
  3838. if( sd->state.lr_flag != 2 )
  3839. pc_bonus_addeff_onskill(sd->addeff_onskill, (sc_type)type3, type4, type2, val, 0);
  3840. break;
  3841. case SP_SET_DEF_RACE: // bonus4 bSetDefRace,r,n,t,y;
  3842. PC_BONUS_CHK_RACE(type2,SP_SET_DEF_RACE);
  3843. if(sd->state.lr_flag == 2)
  3844. break;
  3845. sd->def_set_race[type2].rate = type3;
  3846. sd->def_set_race[type2].tick = type4;
  3847. sd->def_set_race[type2].value = val;
  3848. break;
  3849. case SP_SET_MDEF_RACE: // bonus4 bSetMDefRace,r,n,t,y;
  3850. PC_BONUS_CHK_RACE(type2,SP_SET_MDEF_RACE);
  3851. if(sd->state.lr_flag == 2)
  3852. break;
  3853. sd->mdef_set_race[type2].rate = type3;
  3854. sd->mdef_set_race[type2].tick = type4;
  3855. sd->mdef_set_race[type2].value = val;
  3856. break;
  3857. default:
  3858. if (running_npc_stat_calc_event) {
  3859. ShowWarning("pc_bonus4: unknown bonus type %d %d %d %d %d in OnPCStatCalcEvent!\n", type, type2, type3, type4, val);
  3860. }
  3861. else if (current_equip_combo_pos > 0) {
  3862. ShowWarning("pc_bonus4: unknown bonus type %d %d %d %d %d in a combo with item #%d\n", type, type2, type3, type4, val, sd->inventory_data[pc_checkequip( sd, current_equip_combo_pos )]->nameid);
  3863. }
  3864. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  3865. ShowWarning("pc_bonus4: unknown bonus type %d %d %d %d %d in item #%d\n", type, type2, type3, type4, val, current_equip_card_id ? current_equip_card_id : sd->inventory_data[current_equip_item_index]->nameid);
  3866. }
  3867. else {
  3868. ShowWarning("pc_bonus4: unknown bonus type %d %d %d %d %d in unknown usage. Report this!\n", type, type2, type3, type4, val);
  3869. }
  3870. break;
  3871. }
  3872. }
  3873. /**
  3874. * Gives item bonus to player for format: bonus5 bBonusName,type2,type3,type4,val;
  3875. * @param sd
  3876. * @param type Bonus type used by bBonusName
  3877. * @param type2
  3878. * @param type3
  3879. * @param type4
  3880. * @param val Value that usually for rate or fixed value
  3881. */
  3882. void pc_bonus5(struct map_session_data *sd,int type,int type2,int type3,int type4,int type5,int val)
  3883. {
  3884. nullpo_retv(sd);
  3885. switch(type){
  3886. case SP_AUTOSPELL: // bonus5 bAutoSpell,sk,y,n,bf,i;
  3887. if(sd->state.lr_flag != 2)
  3888. pc_bonus_autospell(sd->autospell, (val&1?type2:-type2), (val&2?-type3:type3), type4, type5, current_equip_card_id);
  3889. break;
  3890. case SP_AUTOSPELL_WHENHIT: // bonus5 bAutoSpellWhenHit,sk,y,n,bf,i;
  3891. if(sd->state.lr_flag != 2)
  3892. pc_bonus_autospell(sd->autospell2, (val&1?type2:-type2), (val&2?-type3:type3), type4, type5, current_equip_card_id);
  3893. break;
  3894. case SP_AUTOSPELL_ONSKILL: // bonus5 bAutoSpellOnSkill,sk,x,y,n,i;
  3895. if(sd->state.lr_flag != 2)
  3896. pc_bonus_autospell_onskill(sd->autospell3, type2, (val&1?-type3:type3), (val&2?-type4:type4), type5, current_equip_card_id);
  3897. break;
  3898. case SP_ADDEFF_ONSKILL: // bonus5 bAddEffOnSkill,sk,eff,n,y,t;
  3899. PC_BONUS_CHK_SC(type3,SP_ADDEFF_ONSKILL);
  3900. if( sd->state.lr_flag != 2 )
  3901. pc_bonus_addeff_onskill(sd->addeff_onskill, (sc_type)type3, type4, type2, type5, val);
  3902. break;
  3903. default:
  3904. if (running_npc_stat_calc_event) {
  3905. ShowWarning("pc_bonus5: unknown bonus type %d %d %d %d %d %d in OnPCStatCalcEvent!\n", type, type2, type3, type4, type5, val);
  3906. }
  3907. else if (current_equip_combo_pos > 0) {
  3908. ShowWarning("pc_bonus5: unknown bonus type %d %d %d %d %d %d in a combo with item #%d\n", type, type2, type3, type4, type5, val, sd->inventory_data[pc_checkequip( sd, current_equip_combo_pos )]->nameid);
  3909. }
  3910. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  3911. ShowWarning("pc_bonus5: unknown bonus type %d %d %d %d %d %d in item #%d\n", type, type2, type3, type4, type5, val, current_equip_card_id ? current_equip_card_id : sd->inventory_data[current_equip_item_index]->nameid);
  3912. }
  3913. else {
  3914. ShowWarning("pc_bonus5: unknown bonus type %d %d %d %d %d %d in unknown usage. Report this!\n", type, type2, type3, type4, type5, val);
  3915. }
  3916. break;
  3917. }
  3918. }
  3919. /*==========================================
  3920. * Grants a player a given skill. Flag values are:
  3921. * 0 - Grant permanent skill to be bound to skill tree
  3922. * 1 - Grant an item skill (temporary)
  3923. * 2 - Like 1, except the level granted can stack with previously learned level.
  3924. * 4 - Like 0, except the skill will ignore skill tree (saves through job changes and resets).
  3925. *------------------------------------------*/
  3926. bool pc_skill(struct map_session_data* sd, uint16 skill_id, int level, enum e_addskill_type type) {
  3927. uint16 idx = 0;
  3928. nullpo_ret(sd);
  3929. if (!skill_id || !(idx = skill_get_index(skill_id))) {
  3930. ShowError("pc_skill: Skill with id %d does not exist in the skill database\n", skill_id);
  3931. return false;
  3932. }
  3933. if (level > MAX_SKILL_LEVEL) {
  3934. ShowError("pc_skill: Skill level %d too high. Max lv supported is %d\n", level, MAX_SKILL_LEVEL);
  3935. return false;
  3936. }
  3937. if (type == ADDSKILL_TEMP_ADDLEVEL && sd->status.skill[idx].lv + level > MAX_SKILL_LEVEL) {
  3938. 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);
  3939. level = MAX_SKILL_LEVEL - sd->status.skill[idx].lv;
  3940. }
  3941. switch (type) {
  3942. case ADDSKILL_PERMANENT: //Set skill data overwriting whatever was there before.
  3943. sd->status.skill[idx].id = skill_id;
  3944. sd->status.skill[idx].lv = level;
  3945. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  3946. if (level == 0) { //Remove skill.
  3947. sd->status.skill[idx].id = 0;
  3948. clif_deleteskill(sd,skill_id);
  3949. } else
  3950. clif_addskill(sd,skill_id);
  3951. if (!skill_get_inf(skill_id)) //Only recalculate for passive skills.
  3952. status_calc_pc(sd, SCO_NONE);
  3953. break;
  3954. case ADDSKILL_TEMP: //Item bonus skill.
  3955. if (sd->status.skill[idx].id != 0) {
  3956. if (sd->status.skill[idx].lv >= level)
  3957. return true;
  3958. if (sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT) //Non-granted skill, store it's level.
  3959. sd->status.skill[idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[idx].lv;
  3960. } else {
  3961. sd->status.skill[idx].id = skill_id;
  3962. sd->status.skill[idx].flag = SKILL_FLAG_TEMPORARY;
  3963. }
  3964. sd->status.skill[idx].lv = level;
  3965. break;
  3966. case ADDSKILL_TEMP_ADDLEVEL: //Add skill bonus on top of what you had.
  3967. if (sd->status.skill[idx].id != 0) {
  3968. if (sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT)
  3969. sd->status.skill[idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[idx].lv; // Store previous level.
  3970. } else {
  3971. sd->status.skill[idx].id = skill_id;
  3972. sd->status.skill[idx].flag = SKILL_FLAG_TEMPORARY; //Set that this is a bonus skill.
  3973. }
  3974. sd->status.skill[idx].lv += level;
  3975. break;
  3976. case ADDSKILL_PERMANENT_GRANTED: //Permanent granted skills ignore the skill tree
  3977. sd->status.skill[idx].id = skill_id;
  3978. sd->status.skill[idx].lv = level;
  3979. sd->status.skill[idx].flag = SKILL_FLAG_PERM_GRANTED;
  3980. if (level == 0) { //Remove skill.
  3981. sd->status.skill[idx].id = 0;
  3982. clif_deleteskill(sd,skill_id);
  3983. } else
  3984. clif_addskill(sd,skill_id);
  3985. if (!skill_get_inf(skill_id)) //Only recalculate for passive skills.
  3986. status_calc_pc(sd, SCO_NONE);
  3987. break;
  3988. default:
  3989. return false;
  3990. }
  3991. return true;
  3992. }
  3993. /*==========================================
  3994. * Append a card to an item ?
  3995. *------------------------------------------*/
  3996. int pc_insert_card(struct map_session_data* sd, int idx_card, int idx_equip)
  3997. {
  3998. int i;
  3999. unsigned short nameid;
  4000. nullpo_ret(sd);
  4001. if( idx_equip < 0 || idx_equip >= MAX_INVENTORY || sd->inventory_data[idx_equip] == NULL )
  4002. return 0; //Invalid item index.
  4003. if( idx_card < 0 || idx_card >= MAX_INVENTORY || sd->inventory_data[idx_card] == NULL )
  4004. return 0; //Invalid card index.
  4005. if( sd->inventory.u.items_inventory[idx_equip].nameid <= 0 || sd->inventory.u.items_inventory[idx_equip].amount < 1 )
  4006. return 0; // target item missing
  4007. if( sd->inventory.u.items_inventory[idx_card].nameid <= 0 || sd->inventory.u.items_inventory[idx_card].amount < 1 )
  4008. return 0; // target card missing
  4009. if( sd->inventory_data[idx_equip]->type != IT_WEAPON && sd->inventory_data[idx_equip]->type != IT_ARMOR )
  4010. return 0; // only weapons and armor are allowed
  4011. if( sd->inventory_data[idx_card]->type != IT_CARD )
  4012. return 0; // must be a card
  4013. if( sd->inventory.u.items_inventory[idx_equip].identify == 0 )
  4014. return 0; // target must be identified
  4015. if( itemdb_isspecial(sd->inventory.u.items_inventory[idx_equip].card[0]) )
  4016. return 0; // card slots reserved for other purposes
  4017. if( (sd->inventory_data[idx_equip]->equip & sd->inventory_data[idx_card]->equip) == 0 )
  4018. return 0; // card cannot be compounded on this item type
  4019. if( sd->inventory_data[idx_equip]->type == IT_WEAPON && sd->inventory_data[idx_card]->equip == EQP_SHIELD )
  4020. return 0; // attempted to place shield card on left-hand weapon.
  4021. if( sd->inventory.u.items_inventory[idx_equip].equip != 0 )
  4022. return 0; // item must be unequipped
  4023. ARR_FIND( 0, sd->inventory_data[idx_equip]->slot, i, sd->inventory.u.items_inventory[idx_equip].card[i] == 0 );
  4024. if( i == sd->inventory_data[idx_equip]->slot )
  4025. return 0; // no free slots
  4026. // remember the card id to insert
  4027. nameid = sd->inventory.u.items_inventory[idx_card].nameid;
  4028. if( pc_delitem(sd,idx_card,1,1,0,LOG_TYPE_OTHER) == 1 )
  4029. {// failed
  4030. clif_insert_card(sd,idx_equip,idx_card,1);
  4031. }
  4032. else
  4033. {// success
  4034. log_pick_pc(sd, LOG_TYPE_OTHER, -1, &sd->inventory.u.items_inventory[idx_equip]);
  4035. sd->inventory.u.items_inventory[idx_equip].card[i] = nameid;
  4036. log_pick_pc(sd, LOG_TYPE_OTHER, 1, &sd->inventory.u.items_inventory[idx_equip]);
  4037. clif_insert_card(sd,idx_equip,idx_card,0);
  4038. }
  4039. return 0;
  4040. }
  4041. /**
  4042. * Returns the count of unidentified items with the option to identify too.
  4043. * @param sd: Player data
  4044. * @param identify_item: Whether or not to identify any unidentified items
  4045. * @return Unidentified items count
  4046. */
  4047. int pc_identifyall(struct map_session_data *sd, bool identify_item)
  4048. {
  4049. int unidentified_count = 0;
  4050. for (int i = 0; i < MAX_INVENTORY; i++) {
  4051. if (sd->inventory.u.items_inventory[i].nameid > 0 && sd->inventory.u.items_inventory[i].identify != 1) {
  4052. if (identify_item == true) {
  4053. sd->inventory.u.items_inventory[i].identify = 1;
  4054. clif_item_identified(sd,i,0);
  4055. }
  4056. unidentified_count++;
  4057. }
  4058. }
  4059. return unidentified_count;
  4060. }
  4061. //
  4062. // Items
  4063. //
  4064. /*==========================================
  4065. * Update buying value by skills
  4066. *------------------------------------------*/
  4067. int pc_modifybuyvalue(struct map_session_data *sd,int orig_value)
  4068. {
  4069. int skill,val = orig_value,rate1 = 0,rate2 = 0;
  4070. if((skill=pc_checkskill(sd,MC_DISCOUNT))>0) // merchant discount
  4071. rate1 = 5+skill*2-((skill==10)? 1:0);
  4072. if((skill=pc_checkskill(sd,RG_COMPULSION))>0) // rogue discount
  4073. rate2 = 5+skill*4;
  4074. if(rate1 < rate2) rate1 = rate2;
  4075. if(rate1)
  4076. val = (int)((double)orig_value*(double)(100-rate1)/100.);
  4077. if(val < battle_config.min_shop_buy)
  4078. val = battle_config.min_shop_buy;
  4079. return val;
  4080. }
  4081. /*==========================================
  4082. * Update selling value by skills
  4083. *------------------------------------------*/
  4084. int pc_modifysellvalue(struct map_session_data *sd,int orig_value)
  4085. {
  4086. int skill,val = orig_value,rate = 0;
  4087. if((skill=pc_checkskill(sd,MC_OVERCHARGE))>0) //OverCharge
  4088. rate = 5+skill*2-((skill==10)? 1:0);
  4089. if(rate)
  4090. val = (int)((double)orig_value*(double)(100+rate)/100.);
  4091. if (val < battle_config.min_shop_sell)
  4092. val = battle_config.min_shop_sell;
  4093. return val;
  4094. }
  4095. /*==========================================
  4096. * Checking if we have enough place on inventory for new item
  4097. * Make sure to take 30k as limit (for client I guess)
  4098. * @param sd
  4099. * @param nameid
  4100. * @param amount
  4101. * @return e_chkitem_result
  4102. *------------------------------------------*/
  4103. char pc_checkadditem(struct map_session_data *sd, unsigned short nameid, int amount)
  4104. {
  4105. int i;
  4106. struct item_data* data;
  4107. nullpo_ret(sd);
  4108. if(amount > MAX_AMOUNT)
  4109. return CHKADDITEM_OVERAMOUNT;
  4110. data = itemdb_search(nameid);
  4111. if(!itemdb_isstackable2(data))
  4112. return CHKADDITEM_NEW;
  4113. if( data->stack.inventory && amount > data->stack.amount )
  4114. return CHKADDITEM_OVERAMOUNT;
  4115. if (data->flag.guid)
  4116. return CHKADDITEM_NEW;
  4117. for(i=0;i<MAX_INVENTORY;i++){
  4118. // FIXME: This does not consider the checked item's cards, thus could check a wrong slot for stackability.
  4119. if(sd->inventory.u.items_inventory[i].nameid == nameid){
  4120. 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 ) )
  4121. return CHKADDITEM_OVERAMOUNT;
  4122. return CHKADDITEM_EXIST;
  4123. }
  4124. }
  4125. return CHKADDITEM_NEW;
  4126. }
  4127. /*==========================================
  4128. * Return number of available place in inventory
  4129. * Each non stackable item will reduce place by 1
  4130. * @param sd
  4131. * @return Number of empty slots
  4132. *------------------------------------------*/
  4133. uint8 pc_inventoryblank(struct map_session_data *sd)
  4134. {
  4135. uint8 i, b;
  4136. nullpo_ret(sd);
  4137. for(i = 0, b = 0; i < MAX_INVENTORY; i++){
  4138. if(sd->inventory.u.items_inventory[i].nameid == 0)
  4139. b++;
  4140. }
  4141. return b;
  4142. }
  4143. /**
  4144. * Attempts to remove zeny from player
  4145. * @param sd: Player
  4146. * @param zeny: Zeny removed
  4147. * @param type: Log type
  4148. * @param tsd: (optional) From who to log (if null take sd)
  4149. * @return 0: Success, 1: Failed (Removing negative Zeny or not enough Zeny), 2: Player not found
  4150. */
  4151. char pc_payzeny(struct map_session_data *sd, int zeny, enum e_log_pick_type type, struct map_session_data *tsd)
  4152. {
  4153. nullpo_retr(2,sd);
  4154. zeny = cap_value(zeny,-MAX_ZENY,MAX_ZENY); //prevent command UB
  4155. if( zeny < 0 )
  4156. {
  4157. ShowError("pc_payzeny: Paying negative Zeny (zeny=%d, account_id=%d, char_id=%d).\n", zeny, sd->status.account_id, sd->status.char_id);
  4158. return 1;
  4159. }
  4160. if( sd->status.zeny < zeny )
  4161. return 1; //Not enough.
  4162. sd->status.zeny -= zeny;
  4163. clif_updatestatus(sd,SP_ZENY);
  4164. if(!tsd) tsd = sd;
  4165. log_zeny(sd, type, tsd, -zeny);
  4166. if( zeny > 0 && sd->state.showzeny ) {
  4167. char output[255];
  4168. sprintf(output, "Removed %dz.", zeny);
  4169. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4170. }
  4171. return 0;
  4172. }
  4173. /**
  4174. * Attempts to give zeny to player
  4175. * @param sd: Player
  4176. * @param type: Log type
  4177. * @param tsd: (optional) From who to log (if null take sd)
  4178. * @return -1: Player not found, 0: Success, 1: Giving negative Zeny
  4179. */
  4180. char pc_getzeny(struct map_session_data *sd, int zeny, enum e_log_pick_type type, struct map_session_data *tsd)
  4181. {
  4182. nullpo_retr(-1,sd);
  4183. zeny = cap_value(zeny,-MAX_ZENY,MAX_ZENY); //prevent command UB
  4184. if( zeny < 0 )
  4185. {
  4186. ShowError("pc_getzeny: Obtaining negative Zeny (zeny=%d, account_id=%d, char_id=%d).\n", zeny, sd->status.account_id, sd->status.char_id);
  4187. return 1;
  4188. }
  4189. if( zeny > MAX_ZENY - sd->status.zeny )
  4190. zeny = MAX_ZENY - sd->status.zeny;
  4191. sd->status.zeny += zeny;
  4192. clif_updatestatus(sd,SP_ZENY);
  4193. if(!tsd) tsd = sd;
  4194. log_zeny(sd, type, tsd, zeny);
  4195. if( zeny > 0 && sd->state.showzeny ) {
  4196. char output[255];
  4197. sprintf(output, "Gained %dz.", zeny);
  4198. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4199. }
  4200. achievement_update_objective(sd, AG_GET_ZENY, 1, sd->status.zeny);
  4201. return 0;
  4202. }
  4203. /**
  4204. * Attempts to remove Cash Points from player
  4205. * @param sd: Player
  4206. * @param price: Total points (cash + kafra) the player has to pay
  4207. * @param points: Kafra points the player has to pay
  4208. * @param type: Log type
  4209. * @return -1: Not enough points, otherwise success (cash+points)
  4210. */
  4211. int pc_paycash(struct map_session_data *sd, int price, int points, e_log_pick_type type)
  4212. {
  4213. int cash;
  4214. nullpo_retr(-1,sd);
  4215. points = cap_value(points, 0, MAX_ZENY); //prevent command UB
  4216. cash = price-points;
  4217. if( sd->cashPoints < cash || sd->kafraPoints < points )
  4218. {
  4219. 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);
  4220. return -1;
  4221. }
  4222. if( cash ){
  4223. pc_setaccountreg(sd, add_str(CASHPOINT_VAR), sd->cashPoints - cash);
  4224. sd->cashPoints -= cash;
  4225. log_cash( sd, type, LOG_CASH_TYPE_CASH, -cash );
  4226. }
  4227. if( points ){
  4228. pc_setaccountreg(sd, add_str(KAFRAPOINT_VAR), sd->kafraPoints - points);
  4229. sd->kafraPoints -= points;
  4230. log_cash( sd, type, LOG_CASH_TYPE_KAFRA, -points );
  4231. }
  4232. if( battle_config.cashshop_show_points )
  4233. {
  4234. char output[CHAT_SIZE_MAX];
  4235. 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.
  4236. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4237. }
  4238. return cash+points;
  4239. }
  4240. /**
  4241. * Attempts to give Cash Points to player
  4242. * @param sd: Player
  4243. * @param cash: Cash points the player gets
  4244. * @param points: Kafra points the player gets
  4245. * @param type: Log type
  4246. * @return -1: Error, otherwise success (cash or points)
  4247. */
  4248. int pc_getcash(struct map_session_data *sd, int cash, int points, e_log_pick_type type)
  4249. {
  4250. char output[CHAT_SIZE_MAX];
  4251. nullpo_retr(-1,sd);
  4252. cash = cap_value(cash, 0, MAX_ZENY); //prevent command UB
  4253. points = cap_value(points, 0, MAX_ZENY); //prevent command UB
  4254. if( cash > 0 )
  4255. {
  4256. if( cash > MAX_ZENY-sd->cashPoints )
  4257. {
  4258. 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);
  4259. cash = MAX_ZENY-sd->cashPoints;
  4260. }
  4261. pc_setaccountreg(sd, add_str(CASHPOINT_VAR), sd->cashPoints+cash);
  4262. sd->cashPoints += cash;
  4263. log_cash( sd, type, LOG_CASH_TYPE_CASH, cash );
  4264. if( battle_config.cashshop_show_points )
  4265. {
  4266. sprintf(output, msg_txt(sd,505), cash, sd->cashPoints);
  4267. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4268. }
  4269. return cash;
  4270. }
  4271. if( points > 0 )
  4272. {
  4273. if( points > MAX_ZENY-sd->kafraPoints )
  4274. {
  4275. 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);
  4276. points = MAX_ZENY-sd->kafraPoints;
  4277. }
  4278. pc_setaccountreg(sd, add_str(KAFRAPOINT_VAR), sd->kafraPoints+points);
  4279. sd->kafraPoints += points;
  4280. log_cash( sd, type, LOG_CASH_TYPE_KAFRA, points );
  4281. if( battle_config.cashshop_show_points )
  4282. {
  4283. sprintf(output, msg_txt(sd,506), points, sd->kafraPoints);
  4284. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4285. }
  4286. return points;
  4287. }
  4288. return -1; //shouldn't happen but just in case
  4289. }
  4290. /**
  4291. * Searching a specified itemid in inventory and return his stored index
  4292. * @param sd Player
  4293. * @param nameid Find this Item!
  4294. * @return Stored index in inventory, or -1 if not found.
  4295. **/
  4296. short pc_search_inventory(struct map_session_data *sd, unsigned short nameid) {
  4297. short i;
  4298. nullpo_retr(-1, sd);
  4299. ARR_FIND( 0, MAX_INVENTORY, i, sd->inventory.u.items_inventory[i].nameid == nameid && (sd->inventory.u.items_inventory[i].amount > 0 || nameid == 0) );
  4300. return ( i < MAX_INVENTORY ) ? i : -1;
  4301. }
  4302. /** Attempt to add a new item to player inventory
  4303. * @param sd
  4304. * @param item_data
  4305. * @param amount
  4306. * @param log_type
  4307. * @return
  4308. * 0 = success
  4309. * 1 = invalid itemid not found or negative amount
  4310. * 2 = overweight
  4311. * 3 = ?
  4312. * 4 = no free place found
  4313. * 5 = max amount reached
  4314. * 6 = ?
  4315. * 7 = stack limitation
  4316. */
  4317. enum e_additem_result pc_additem(struct map_session_data *sd,struct item *item,int amount,e_log_pick_type log_type) {
  4318. struct item_data *id;
  4319. int16 i;
  4320. unsigned int w;
  4321. nullpo_retr(ADDITEM_INVALID, sd);
  4322. nullpo_retr(ADDITEM_INVALID, item);
  4323. if( item->nameid == 0 || amount <= 0 )
  4324. return ADDITEM_INVALID;
  4325. if( amount > MAX_AMOUNT )
  4326. return ADDITEM_OVERAMOUNT;
  4327. id = itemdb_search(item->nameid);
  4328. if( id->stack.inventory && amount > id->stack.amount )
  4329. {// item stack limitation
  4330. return ADDITEM_STACKLIMIT;
  4331. }
  4332. w = id->weight*amount;
  4333. if(sd->weight + w > sd->max_weight)
  4334. return ADDITEM_OVERWEIGHT;
  4335. i = MAX_INVENTORY;
  4336. if (id->flag.guid && !item->unique_id)
  4337. item->unique_id = pc_generate_unique_id(sd);
  4338. // Stackable | Non Rental
  4339. if( itemdb_isstackable2(id) && item->expire_time == 0 ) {
  4340. for( i = 0; i < MAX_INVENTORY; i++ ) {
  4341. if( sd->inventory.u.items_inventory[i].nameid == item->nameid &&
  4342. sd->inventory.u.items_inventory[i].bound == item->bound &&
  4343. sd->inventory.u.items_inventory[i].expire_time == 0 &&
  4344. sd->inventory.u.items_inventory[i].unique_id == item->unique_id &&
  4345. memcmp(&sd->inventory.u.items_inventory[i].card, &item->card, sizeof(item->card)) == 0 ) {
  4346. 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 ) )
  4347. return ADDITEM_OVERAMOUNT;
  4348. sd->inventory.u.items_inventory[i].amount += amount;
  4349. clif_additem(sd,i,amount,0);
  4350. break;
  4351. }
  4352. }
  4353. }
  4354. if (i >= MAX_INVENTORY) {
  4355. i = pc_search_inventory(sd,0);
  4356. if( i < 0 )
  4357. return ADDITEM_OVERITEM;
  4358. memcpy(&sd->inventory.u.items_inventory[i], item, sizeof(sd->inventory.u.items_inventory[0]));
  4359. // clear equip and favorite fields first, just in case
  4360. if( item->equip )
  4361. sd->inventory.u.items_inventory[i].equip = 0;
  4362. if( item->favorite )
  4363. sd->inventory.u.items_inventory[i].favorite = 0;
  4364. if( item->equipSwitch )
  4365. sd->inventory.u.items_inventory[i].equipSwitch = 0;
  4366. sd->inventory.u.items_inventory[i].amount = amount;
  4367. sd->inventory_data[i] = id;
  4368. sd->last_addeditem_index = i;
  4369. if (!itemdb_isstackable2(id) || id->flag.guid)
  4370. sd->inventory.u.items_inventory[i].unique_id = item->unique_id ? item->unique_id : pc_generate_unique_id(sd);
  4371. clif_additem(sd,i,amount,0);
  4372. }
  4373. log_pick_pc(sd, log_type, amount, &sd->inventory.u.items_inventory[i]);
  4374. sd->weight += w;
  4375. clif_updatestatus(sd,SP_WEIGHT);
  4376. //Auto-equip
  4377. if(id->flag.autoequip)
  4378. pc_equipitem(sd, i, id->equip);
  4379. /* rental item check */
  4380. if( item->expire_time ) {
  4381. if( time(NULL) > item->expire_time ) {
  4382. clif_rental_expired(sd->fd, i, sd->inventory.u.items_inventory[i].nameid);
  4383. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 1, 0, LOG_TYPE_OTHER);
  4384. } else {
  4385. unsigned int seconds = (unsigned int)( item->expire_time - time(NULL) );
  4386. clif_rental_time(sd->fd, sd->inventory.u.items_inventory[i].nameid, seconds);
  4387. pc_inventory_rental_add(sd, seconds);
  4388. }
  4389. }
  4390. achievement_update_objective(sd, AG_GET_ITEM, 1, id->value_sell);
  4391. pc_show_questinfo(sd);
  4392. return ADDITEM_SUCCESS;
  4393. }
  4394. /*==========================================
  4395. * Remove an item at index n from inventory by amount.
  4396. * @param sd
  4397. * @param n Item index in inventory
  4398. * @param amount
  4399. * @param type &1: Don't notify deletion; &2 Don't notify weight change; &4 Don't calculate status
  4400. * @param reason Delete reason
  4401. * @param log_type e_log_pick_type
  4402. * @return 1 - invalid itemid or negative amount; 0 - Success
  4403. *------------------------------------------*/
  4404. char pc_delitem(struct map_session_data *sd,int n,int amount,int type, short reason, e_log_pick_type log_type)
  4405. {
  4406. nullpo_retr(1, sd);
  4407. 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)
  4408. return 1;
  4409. log_pick_pc(sd, log_type, -amount, &sd->inventory.u.items_inventory[n]);
  4410. sd->inventory.u.items_inventory[n].amount -= amount;
  4411. sd->weight -= sd->inventory_data[n]->weight*amount ;
  4412. if( sd->inventory.u.items_inventory[n].amount <= 0 ){
  4413. if(sd->inventory.u.items_inventory[n].equip)
  4414. pc_unequipitem(sd,n,2|(!(type&4) ? 1 : 0));
  4415. memset(&sd->inventory.u.items_inventory[n],0,sizeof(sd->inventory.u.items_inventory[0]));
  4416. sd->inventory_data[n] = NULL;
  4417. }
  4418. if(!(type&1))
  4419. clif_delitem(sd,n,amount,reason);
  4420. if(!(type&2))
  4421. clif_updatestatus(sd,SP_WEIGHT);
  4422. pc_show_questinfo(sd);
  4423. return 0;
  4424. }
  4425. /*==========================================
  4426. * Attempt to drop an item.
  4427. * @param sd
  4428. * @param n Item index in inventory
  4429. * @param amount Amount of item
  4430. * @return False = fail; True = success
  4431. *------------------------------------------*/
  4432. bool pc_dropitem(struct map_session_data *sd,int n,int amount)
  4433. {
  4434. nullpo_retr(1, sd);
  4435. if(n < 0 || n >= MAX_INVENTORY)
  4436. return false;
  4437. if(amount <= 0)
  4438. return false;
  4439. if(sd->inventory.u.items_inventory[n].nameid <= 0 ||
  4440. sd->inventory.u.items_inventory[n].amount <= 0 ||
  4441. sd->inventory.u.items_inventory[n].amount < amount ||
  4442. sd->state.trading || sd->state.vending ||
  4443. !sd->inventory_data[n] //pc_delitem would fail on this case.
  4444. )
  4445. return false;
  4446. if( sd->inventory.u.items_inventory[n].equipSwitch )
  4447. return false;
  4448. if( map_getmapflag(sd->bl.m, MF_NODROP) )
  4449. {
  4450. clif_displaymessage (sd->fd, msg_txt(sd,271));
  4451. return false; //Can't drop items in nodrop mapflag maps.
  4452. }
  4453. if( !pc_candrop(sd,&sd->inventory.u.items_inventory[n]) )
  4454. {
  4455. clif_displaymessage (sd->fd, msg_txt(sd,263));
  4456. return false;
  4457. }
  4458. if (!map_addflooritem(&sd->inventory.u.items_inventory[n], amount, sd->bl.m, sd->bl.x, sd->bl.y, 0, 0, 0, 2, 0))
  4459. return false;
  4460. pc_delitem(sd, n, amount, 1, 0, LOG_TYPE_PICKDROP_PLAYER);
  4461. clif_dropitem(sd, n, amount);
  4462. return true;
  4463. }
  4464. /*==========================================
  4465. * Attempt to pick up an item.
  4466. * @param sd
  4467. * @param fitem Item that will be picked
  4468. * @return False = fail; True = success
  4469. *------------------------------------------*/
  4470. bool pc_takeitem(struct map_session_data *sd,struct flooritem_data *fitem)
  4471. {
  4472. int flag = 0;
  4473. t_tick tick = gettick();
  4474. struct party_data *p = NULL;
  4475. nullpo_ret(sd);
  4476. nullpo_ret(fitem);
  4477. if (!check_distance_bl(&fitem->bl, &sd->bl, 2) && sd->ud.skill_id!=BS_GREED)
  4478. return false; // Distance is too far
  4479. if (sd->sc.cant.pickup)
  4480. return false;
  4481. if (sd->status.party_id)
  4482. p = party_search(sd->status.party_id);
  4483. if (fitem->first_get_charid > 0 && fitem->first_get_charid != sd->status.char_id) {
  4484. struct map_session_data *first_sd = map_charid2sd(fitem->first_get_charid);
  4485. if (DIFF_TICK(tick,fitem->first_get_tick) < 0) {
  4486. if (!(p && p->party.item&1 &&
  4487. first_sd && first_sd->status.party_id == sd->status.party_id
  4488. ))
  4489. return false;
  4490. }
  4491. else if (fitem->second_get_charid > 0 && fitem->second_get_charid != sd->status.char_id) {
  4492. struct map_session_data *second_sd = map_charid2sd(fitem->second_get_charid);
  4493. if (DIFF_TICK(tick, fitem->second_get_tick) < 0) {
  4494. if (!(p && p->party.item&1 &&
  4495. ((first_sd && first_sd->status.party_id == sd->status.party_id) ||
  4496. (second_sd && second_sd->status.party_id == sd->status.party_id))
  4497. ))
  4498. return false;
  4499. }
  4500. else if (fitem->third_get_charid > 0 && fitem->third_get_charid != sd->status.char_id){
  4501. struct map_session_data *third_sd = map_charid2sd(fitem->third_get_charid);
  4502. if (DIFF_TICK(tick,fitem->third_get_tick) < 0) {
  4503. if(!(p && p->party.item&1 &&
  4504. ((first_sd && first_sd->status.party_id == sd->status.party_id) ||
  4505. (second_sd && second_sd->status.party_id == sd->status.party_id) ||
  4506. (third_sd && third_sd->status.party_id == sd->status.party_id))
  4507. ))
  4508. return false;
  4509. }
  4510. }
  4511. }
  4512. }
  4513. //This function takes care of giving the item to whoever should have it, considering party-share options.
  4514. if ((flag = party_share_loot(p,sd,&fitem->item, fitem->first_get_charid))) {
  4515. clif_additem(sd,0,0,flag);
  4516. return true;
  4517. }
  4518. //Display pickup animation.
  4519. pc_stop_attack(sd);
  4520. clif_takeitem(&sd->bl,&fitem->bl);
  4521. if (fitem->mob_id &&
  4522. (itemdb_search(fitem->item.nameid))->flag.broadcast &&
  4523. (!p || !(p->party.item&2)) // Somehow, if party's pickup distribution is 'Even Share', no announcemet
  4524. )
  4525. intif_broadcast_obtain_special_item(sd, fitem->item.nameid, fitem->mob_id, ITEMOBTAIN_TYPE_MONSTER_ITEM);
  4526. map_clearflooritem(&fitem->bl);
  4527. return true;
  4528. }
  4529. /*==========================================
  4530. * Check if item is usable.
  4531. * Return:
  4532. * false = no
  4533. * true = yes
  4534. *------------------------------------------*/
  4535. bool pc_isUseitem(struct map_session_data *sd,int n)
  4536. {
  4537. struct item_data *item;
  4538. unsigned short nameid;
  4539. nullpo_ret(sd);
  4540. item = sd->inventory_data[n];
  4541. nameid = sd->inventory.u.items_inventory[n].nameid;
  4542. if( item == NULL )
  4543. return false;
  4544. //Not consumable item
  4545. if( item->type != IT_HEALING && item->type != IT_USABLE && item->type != IT_CASH )
  4546. return false;
  4547. if (pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL))
  4548. return true;
  4549. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  4550. if(mapdata->flag[MF_NOITEMCONSUMPTION]) //consumable but mapflag prevent it
  4551. return false;
  4552. //Prevent mass item usage. [Skotlex]
  4553. if( DIFF_TICK(sd->canuseitem_tick,gettick()) > 0 ||
  4554. (itemdb_group_item_exists(IG_CASH_FOOD, nameid) && DIFF_TICK(sd->canusecashfood_tick,gettick()) > 0)
  4555. )
  4556. return false;
  4557. if( (item->item_usage.flag&NOUSE_SITTING) && (pc_issit(sd) == 1) && (pc_get_group_level(sd) < item->item_usage.override) ) {
  4558. clif_msg(sd,ITEM_NOUSE_SITTING);
  4559. return false; // You cannot use this item while sitting.
  4560. }
  4561. if (sd->state.storage_flag && item->type != IT_CASH) {
  4562. clif_messagecolor(&sd->bl, color_table[COLOR_RED], msg_txt(sd,388), false, SELF);
  4563. return false; // You cannot use this item while storage is open.
  4564. }
  4565. if (item->flag.dead_branch && (mapdata->flag[MF_NOBRANCH] || mapdata_flag_gvg2(mapdata)))
  4566. return false;
  4567. switch( nameid ) {
  4568. case ITEMID_WING_OF_FLY:
  4569. case ITEMID_GIANT_FLY_WING:
  4570. case ITEMID_N_FLY_WING:
  4571. if( mapdata->flag[MF_NOTELEPORT] || mapdata_flag_gvg2(mapdata) ) {
  4572. clif_skill_teleportmessage(sd,0);
  4573. return false;
  4574. }
  4575. if (nameid == ITEMID_GIANT_FLY_WING) {
  4576. struct party_data *pd = party_search(sd->status.party_id);
  4577. if (pd) {
  4578. int i;
  4579. ARR_FIND(0, MAX_PARTY, i, pd->data[i].sd == sd && pd->party.member[i].leader);
  4580. if (i == MAX_PARTY) { // User is not party leader
  4581. clif_msg(sd, ITEM_PARTY_MEMBER_NOT_SUMMONED);
  4582. break;
  4583. }
  4584. 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));
  4585. if (i == MAX_PARTY) { // No party members found on same map
  4586. clif_msg(sd, ITEM_PARTY_NO_MEMBER_IN_MAP);
  4587. break;
  4588. }
  4589. } else {
  4590. clif_msg(sd, ITEM_PARTY_MEMBER_NOT_SUMMONED);
  4591. break;
  4592. }
  4593. }
  4594. // Fall through
  4595. case ITEMID_WING_OF_BUTTERFLY:
  4596. case ITEMID_N_BUTTERFLY_WING:
  4597. case ITEMID_DUN_TELE_SCROLL1:
  4598. case ITEMID_DUN_TELE_SCROLL2:
  4599. case ITEMID_DUN_TELE_SCROLL3:
  4600. case ITEMID_WOB_RUNE:
  4601. case ITEMID_WOB_SCHWALTZ:
  4602. case ITEMID_WOB_RACHEL:
  4603. case ITEMID_WOB_LOCAL:
  4604. case ITEMID_SIEGE_TELEPORT_SCROLL:
  4605. if( sd->duel_group && !battle_config.duel_allow_teleport ) {
  4606. clif_displaymessage(sd->fd, msg_txt(sd,663));
  4607. return false;
  4608. }
  4609. if( mapdata->flag[MF_NORETURN] && nameid != ITEMID_WING_OF_FLY && nameid != ITEMID_GIANT_FLY_WING && nameid != ITEMID_N_FLY_WING )
  4610. return false;
  4611. break;
  4612. case ITEMID_MERCENARY_RED_POTION:
  4613. case ITEMID_MERCENARY_BLUE_POTION:
  4614. case ITEMID_M_CENTER_POTION:
  4615. case ITEMID_M_AWAKENING_POTION:
  4616. case ITEMID_M_BERSERK_POTION:
  4617. if( sd->md == NULL || sd->md->db == NULL )
  4618. return false;
  4619. if( sd->md->sc.data[SC_BERSERK] )
  4620. return false;
  4621. if( nameid == ITEMID_M_AWAKENING_POTION && sd->md->db->lv < 40 )
  4622. return false;
  4623. if( nameid == ITEMID_M_BERSERK_POTION && sd->md->db->lv < 80 )
  4624. return false;
  4625. break;
  4626. case ITEMID_NEURALIZER:
  4627. if( !mapdata->flag[MF_RESET] )
  4628. return false;
  4629. break;
  4630. }
  4631. if( itemdb_group_item_exists(IG_MERCENARY, nameid) && sd->md != NULL )
  4632. return false; // Mercenary Scrolls
  4633. if( item->flag.group || item->type == IT_CASH) { //safe check type cash disappear when overweight [Napster]
  4634. if( pc_is90overweight(sd) ) {
  4635. clif_msg(sd, ITEM_CANT_OBTAIN_WEIGHT);
  4636. return false;
  4637. }
  4638. if( !pc_inventoryblank(sd) ) {
  4639. clif_messagecolor(&sd->bl, color_table[COLOR_RED], msg_txt(sd, 732), false, SELF); //Item cannot be open when inventory is full
  4640. return false;
  4641. }
  4642. }
  4643. //Gender check
  4644. if(item->sex != 2 && sd->status.sex != item->sex)
  4645. return false;
  4646. //Required level check
  4647. if(item->elv && sd->status.base_level < (unsigned int)item->elv)
  4648. return false;
  4649. if(item->elvmax && sd->status.base_level > (unsigned int)item->elvmax)
  4650. return false;
  4651. //Not equipable by class. [Skotlex]
  4652. if (!(
  4653. (1ULL<<(sd->class_&MAPID_BASEMASK)) &
  4654. (item->class_base[sd->class_&JOBL_2_1?1:(sd->class_&JOBL_2_2?2:0)])
  4655. ))
  4656. return false;
  4657. if (sd->sc.count && (
  4658. sd->sc.data[SC_BERSERK] || sd->sc.data[SC_SATURDAYNIGHTFEVER] ||
  4659. (sd->sc.data[SC_GRAVITATION] && sd->sc.data[SC_GRAVITATION]->val3 == BCT_SELF) ||
  4660. sd->sc.data[SC_TRICKDEAD] ||
  4661. sd->sc.data[SC_HIDING] ||
  4662. sd->sc.data[SC__SHADOWFORM] ||
  4663. sd->sc.data[SC__INVISIBILITY] ||
  4664. sd->sc.data[SC__MANHOLE] ||
  4665. sd->sc.data[SC_DEEPSLEEP] ||
  4666. sd->sc.data[SC_CRYSTALIZE] ||
  4667. sd->sc.data[SC_KAGEHUMI] ||
  4668. (sd->sc.data[SC_NOCHAT] && sd->sc.data[SC_NOCHAT]->val1&MANNER_NOITEM) ||
  4669. sd->sc.data[SC_KINGS_GRACE] ||
  4670. sd->sc.data[SC_SUHIDE]))
  4671. return false;
  4672. if (!pc_isItemClass(sd,item))
  4673. return false;
  4674. //Dead Branch items
  4675. if( item->flag.dead_branch )
  4676. log_branch(sd);
  4677. return true;
  4678. }
  4679. /*==========================================
  4680. * Last checks to use an item.
  4681. * Return:
  4682. * 0 = fail
  4683. * 1 = success
  4684. *------------------------------------------*/
  4685. int pc_useitem(struct map_session_data *sd,int n)
  4686. {
  4687. t_tick tick = gettick();
  4688. int amount;
  4689. unsigned short nameid;
  4690. struct script_code *script;
  4691. struct item item;
  4692. struct item_data *id;
  4693. nullpo_ret(sd);
  4694. if (sd->npc_id) {
  4695. if (sd->progressbar.npc_id) {
  4696. clif_progressbar_abort(sd);
  4697. return 0; // First item use attempt cancels the progress bar
  4698. }
  4699. if( pc_hasprogress( sd, WIP_DISABLE_SKILLITEM ) || !sd->npc_item_flag ){
  4700. #ifdef RENEWAL
  4701. clif_msg( sd, WORK_IN_PROGRESS );
  4702. #endif
  4703. return 0;
  4704. }
  4705. }
  4706. item = sd->inventory.u.items_inventory[n];
  4707. id = sd->inventory_data[n];
  4708. if (item.nameid == 0 || item.amount <= 0)
  4709. return 0;
  4710. if( !pc_isUseitem(sd,n) )
  4711. return 0;
  4712. // Store information for later use before it is lost (via pc_delitem) [Paradox924X]
  4713. nameid = id->nameid;
  4714. if (nameid != ITEMID_NAUTHIZ && sd->sc.opt1 > 0 && sd->sc.opt1 != OPT1_STONEWAIT && sd->sc.opt1 != OPT1_BURNING)
  4715. return 0;
  4716. /* Items with delayed consume are not meant to work while in mounts except reins of mount(12622) */
  4717. if( id->flag.delay_consume ) {
  4718. if( nameid != ITEMID_REINS_OF_MOUNT && sd->sc.data[SC_ALL_RIDING] )
  4719. return 0;
  4720. else if( pc_issit(sd) )
  4721. return 0;
  4722. }
  4723. //Since most delay-consume items involve using a "skill-type" target cursor,
  4724. //perform a skill-use check before going through. [Skotlex]
  4725. //resurrection was picked as testing skill, as a non-offensive, generic skill, it will do.
  4726. //FIXME: Is this really needed here? It'll be checked in unit.cpp after all and this prevents skill items using when silenced [Inkfish]
  4727. if( id->flag.delay_consume && ( sd->ud.skilltimer != INVALID_TIMER /*|| !status_check_skilluse(&sd->bl, &sd->bl, ALL_RESURRECTION, 0)*/ ) )
  4728. return 0;
  4729. if( id->delay > 0 && !pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL) && pc_itemcd_check(sd, id, tick, n))
  4730. return 0;
  4731. /* on restricted maps the item is consumed but the effect is not used */
  4732. if (!pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL) && itemdb_isNoEquip(id,sd->bl.m)) {
  4733. clif_msg(sd,ITEM_CANT_USE_AREA); // This item cannot be used within this area
  4734. if( battle_config.allow_consume_restricted_item && !id->flag.delay_consume ) { //need confirmation for delayed consumption items
  4735. clif_useitemack(sd,n,item.amount-1,true);
  4736. pc_delitem(sd,n,1,1,0,LOG_TYPE_CONSUME);
  4737. }
  4738. return 0;/* regardless, effect is not run */
  4739. }
  4740. sd->itemid = item.nameid;
  4741. sd->itemindex = n;
  4742. if(sd->catch_target_class != PET_CATCH_FAIL) //Abort pet catching.
  4743. sd->catch_target_class = PET_CATCH_FAIL;
  4744. amount = item.amount;
  4745. script = id->script;
  4746. //Check if the item is to be consumed immediately [Skotlex]
  4747. if (id->flag.delay_consume)
  4748. clif_useitemack(sd, n, amount, true);
  4749. else
  4750. {
  4751. if( item.expire_time == 0 && nameid != ITEMID_REINS_OF_MOUNT )
  4752. {
  4753. clif_useitemack(sd, n, amount - 1, true);
  4754. pc_delitem(sd, n, 1, 1, 0, LOG_TYPE_CONSUME); // Rental Usable Items are not deleted until expiration
  4755. }
  4756. else
  4757. clif_useitemack(sd, n, 0, false);
  4758. }
  4759. if (item.card[0]==CARD0_CREATE && pc_famerank(MakeDWord(item.card[2],item.card[3]), MAPID_ALCHEMIST))
  4760. potion_flag = 2; // Famous player's potions have 50% more efficiency
  4761. //Update item use time.
  4762. sd->canuseitem_tick = tick + battle_config.item_use_interval;
  4763. if( itemdb_group_item_exists(IG_CASH_FOOD, nameid) )
  4764. sd->canusecashfood_tick = tick + battle_config.cashfood_use_interval;
  4765. run_script(script,0,sd->bl.id,fake_nd->bl.id);
  4766. potion_flag = 0;
  4767. return 1;
  4768. }
  4769. /**
  4770. * Add item on cart for given index.
  4771. * @param sd
  4772. * @param item
  4773. * @param amount
  4774. * @param log_type
  4775. * @return See pc.hpp::e_additem_result
  4776. */
  4777. enum e_additem_result pc_cart_additem(struct map_session_data *sd,struct item *item,int amount,e_log_pick_type log_type)
  4778. {
  4779. struct item_data *data;
  4780. int i,w;
  4781. nullpo_retr(ADDITEM_INVALID, sd);
  4782. nullpo_retr(ADDITEM_INVALID, item);
  4783. if(item->nameid == 0 || amount <= 0)
  4784. return ADDITEM_INVALID;
  4785. if (itemdb_ishatched_egg(item))
  4786. return ADDITEM_INVALID;
  4787. data = itemdb_search(item->nameid);
  4788. if( data->stack.cart && amount > data->stack.amount )
  4789. {// item stack limitation
  4790. return ADDITEM_STACKLIMIT;
  4791. }
  4792. if( !itemdb_cancartstore(item, pc_get_group_level(sd)) || (item->bound > BOUND_ACCOUNT && !pc_can_give_bounded_items(sd)))
  4793. { // Check item trade restrictions [Skotlex]
  4794. clif_displaymessage (sd->fd, msg_txt(sd,264));
  4795. return ADDITEM_INVALID;
  4796. }
  4797. if( (w = data->weight*amount) + sd->cart_weight > sd->cart_weight_max )
  4798. return ADDITEM_OVERWEIGHT;
  4799. i = MAX_CART;
  4800. if( itemdb_isstackable2(data) && !item->expire_time )
  4801. {
  4802. for (i = 0; i < MAX_CART; i++) {
  4803. if (sd->cart.u.items_cart[i].nameid == item->nameid
  4804. && sd->cart.u.items_cart[i].bound == item->bound
  4805. && sd->cart.u.items_cart[i].unique_id == item->unique_id
  4806. && memcmp(sd->cart.u.items_cart[i].card, item->card, sizeof(item->card)) == 0
  4807. )
  4808. break;
  4809. }
  4810. }
  4811. if( i < MAX_CART )
  4812. {// item already in cart, stack it
  4813. 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 ) )
  4814. return ADDITEM_OVERAMOUNT; // no slot
  4815. sd->cart.u.items_cart[i].amount += amount;
  4816. clif_cart_additem(sd,i,amount,0);
  4817. }
  4818. else
  4819. {// item not stackable or not present, add it
  4820. ARR_FIND( 0, MAX_CART, i, sd->cart.u.items_cart[i].nameid == 0 );
  4821. if( i == MAX_CART )
  4822. return ADDITEM_OVERAMOUNT; // no slot
  4823. memcpy(&sd->cart.u.items_cart[i],item,sizeof(sd->cart.u.items_cart[0]));
  4824. sd->cart.u.items_cart[i].id = 0;
  4825. sd->cart.u.items_cart[i].amount = amount;
  4826. sd->cart_num++;
  4827. clif_cart_additem(sd,i,amount,0);
  4828. }
  4829. sd->cart.u.items_cart[i].favorite = 0; // clear
  4830. sd->cart.u.items_cart[i].equipSwitch = 0;
  4831. log_pick_pc(sd, log_type, amount, &sd->cart.u.items_cart[i]);
  4832. sd->cart_weight += w;
  4833. clif_updatestatus(sd,SP_CARTINFO);
  4834. return ADDITEM_SUCCESS;
  4835. }
  4836. /*==========================================
  4837. * Delete item on cart for given index.
  4838. *------------------------------------------*/
  4839. void pc_cart_delitem(struct map_session_data *sd,int n,int amount,int type,e_log_pick_type log_type)
  4840. {
  4841. nullpo_retv(sd);
  4842. if(sd->cart.u.items_cart[n].nameid == 0 ||
  4843. sd->cart.u.items_cart[n].amount < amount)
  4844. return;
  4845. log_pick_pc(sd, log_type, -amount, &sd->cart.u.items_cart[n]);
  4846. sd->cart.u.items_cart[n].amount -= amount;
  4847. sd->cart_weight -= itemdb_weight(sd->cart.u.items_cart[n].nameid) * amount;
  4848. if(sd->cart.u.items_cart[n].amount <= 0) {
  4849. memset(&sd->cart.u.items_cart[n],0,sizeof(sd->cart.u.items_cart[0]));
  4850. sd->cart_num--;
  4851. }
  4852. if(!type) {
  4853. clif_cart_delitem(sd,n,amount);
  4854. clif_updatestatus(sd,SP_CARTINFO);
  4855. }
  4856. }
  4857. /*==========================================
  4858. * Transfer item from inventory to cart.
  4859. *------------------------------------------*/
  4860. void pc_putitemtocart(struct map_session_data *sd,int idx,int amount)
  4861. {
  4862. nullpo_retv(sd);
  4863. if (idx < 0 || idx >= MAX_INVENTORY) //Invalid index check [Skotlex]
  4864. return;
  4865. struct item *item_data = &sd->inventory.u.items_inventory[idx];
  4866. if( item_data->nameid == 0 || amount < 1 || item_data->amount < amount || sd->state.vending || sd->state.prevend )
  4867. return;
  4868. if( item_data->equipSwitch ){
  4869. clif_msg( sd, C_ITEM_EQUIP_SWITCH );
  4870. return;
  4871. }
  4872. enum e_additem_result flag = pc_cart_additem(sd,item_data,amount,LOG_TYPE_NONE);
  4873. if (flag == ADDITEM_SUCCESS)
  4874. pc_delitem(sd,idx,amount,0,5,LOG_TYPE_NONE);
  4875. else {
  4876. clif_cart_additem_ack(sd, (flag == ADDITEM_OVERAMOUNT) ? ADDITEM_TO_CART_FAIL_COUNT : ADDITEM_TO_CART_FAIL_WEIGHT);
  4877. clif_additem(sd, idx, amount, 0);
  4878. clif_delitem(sd, idx, amount, 0);
  4879. }
  4880. }
  4881. /*==========================================
  4882. * Get number of item in cart.
  4883. * Return:
  4884. -1 = itemid not found or no amount found
  4885. x = remaining itemid on cart after get
  4886. *------------------------------------------*/
  4887. int pc_cartitem_amount(struct map_session_data* sd, int idx, int amount)
  4888. {
  4889. struct item* item_data;
  4890. nullpo_retr(-1, sd);
  4891. item_data = &sd->cart.u.items_cart[idx];
  4892. if( item_data->nameid == 0 || item_data->amount == 0 )
  4893. return -1;
  4894. return item_data->amount - amount;
  4895. }
  4896. /*==========================================
  4897. * Retrieve an item at index idx from cart.
  4898. *------------------------------------------*/
  4899. void pc_getitemfromcart(struct map_session_data *sd,int idx,int amount)
  4900. {
  4901. nullpo_retv(sd);
  4902. if (idx < 0 || idx >= MAX_CART) //Invalid index check [Skotlex]
  4903. return;
  4904. struct item *item_data=&sd->cart.u.items_cart[idx];
  4905. if (item_data->nameid == 0 || amount < 1 || item_data->amount < amount || sd->state.vending || sd->state.prevend)
  4906. return;
  4907. enum e_additem_result flag = pc_additem(sd, item_data, amount, LOG_TYPE_NONE);
  4908. if (flag == ADDITEM_SUCCESS)
  4909. pc_cart_delitem(sd, idx, amount, 0, LOG_TYPE_NONE);
  4910. else {
  4911. clif_cart_delitem(sd, idx, amount);
  4912. clif_additem(sd, idx, amount, flag);
  4913. clif_cart_additem(sd, idx, amount, 0);
  4914. }
  4915. }
  4916. /*==========================================
  4917. * Bound Item Check
  4918. * Type:
  4919. * 1 Account Bound
  4920. * 2 Guild Bound
  4921. * 3 Party Bound
  4922. * 4 Character Bound
  4923. *------------------------------------------*/
  4924. int pc_bound_chk(TBL_PC *sd,enum bound_type type,int *idxlist)
  4925. {
  4926. int i = 0, j = 0;
  4927. for(i = 0; i < MAX_INVENTORY; i++) {
  4928. 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) {
  4929. idxlist[j] = i;
  4930. j++;
  4931. }
  4932. }
  4933. return j;
  4934. }
  4935. /*==========================================
  4936. * Display item stolen msg to player sd
  4937. *------------------------------------------*/
  4938. int pc_show_steal(struct block_list *bl,va_list ap)
  4939. {
  4940. struct map_session_data *sd;
  4941. int itemid;
  4942. struct item_data *item=NULL;
  4943. char output[100];
  4944. sd=va_arg(ap,struct map_session_data *);
  4945. itemid=va_arg(ap,int);
  4946. if((item=itemdb_exists(itemid))==NULL)
  4947. sprintf(output,"%s stole an Unknown Item (id: %i).",sd->status.name, itemid);
  4948. else
  4949. sprintf(output,"%s stole %s.",sd->status.name,item->jname);
  4950. clif_displaymessage( ((struct map_session_data *)bl)->fd, output);
  4951. return 0;
  4952. }
  4953. /**
  4954. * Steal an item from bl (mob).
  4955. * @param sd: Player data
  4956. * @param bl: Object to steal from
  4957. * @param skill_lv: Level of skill used
  4958. * @return True on success or false otherwise
  4959. */
  4960. bool pc_steal_item(struct map_session_data *sd,struct block_list *bl, uint16 skill_lv)
  4961. {
  4962. int i,itemid;
  4963. double rate;
  4964. unsigned char flag = 0;
  4965. struct status_data *sd_status, *md_status;
  4966. struct mob_data *md;
  4967. struct item tmp_item;
  4968. if(!sd || !bl || bl->type!=BL_MOB)
  4969. return false;
  4970. md = (TBL_MOB *)bl;
  4971. if(md->state.steal_flag == UCHAR_MAX || ( md->sc.opt1 && md->sc.opt1 != OPT1_BURNING ) ) //already stolen from / status change check
  4972. return false;
  4973. sd_status= status_get_status_data(&sd->bl);
  4974. md_status= status_get_status_data(bl);
  4975. if (md->master_id || status_has_mode(md_status, MD_STATUS_IMMUNE) || status_get_race2(&md->bl) == RC2_TREASURE ||
  4976. map_getmapflag(bl->m, MF_NOMOBLOOT) || // check noloot map flag [Lorky]
  4977. (battle_config.skill_steal_max_tries && //Reached limit of steal attempts. [Lupus]
  4978. md->state.steal_flag++ >= battle_config.skill_steal_max_tries)
  4979. ) { //Can't steal from
  4980. md->state.steal_flag = UCHAR_MAX;
  4981. return false;
  4982. }
  4983. // base skill success chance (percentual)
  4984. rate = (sd_status->dex - md_status->dex)/2 + skill_lv*6 + 4;
  4985. rate += sd->bonus.add_steal_rate;
  4986. if( rate < 1
  4987. #ifdef RENEWAL
  4988. || rnd()%100 >= rate
  4989. #endif
  4990. )
  4991. return false;
  4992. // Try dropping one item, in the order from first to last possible slot.
  4993. // Droprate is affected by the skill success rate.
  4994. for( i = 0; i < MAX_STEAL_DROP; i++ )
  4995. if( md->db->dropitem[i].nameid > 0 && !md->db->dropitem[i].steal_protected && itemdb_exists(md->db->dropitem[i].nameid) && rnd() % 10000 < md->db->dropitem[i].p
  4996. #ifndef RENEWAL
  4997. * rate/100.
  4998. #endif
  4999. )
  5000. break;
  5001. if( i == MAX_STEAL_DROP )
  5002. return false;
  5003. itemid = md->db->dropitem[i].nameid;
  5004. memset(&tmp_item,0,sizeof(tmp_item));
  5005. tmp_item.nameid = itemid;
  5006. tmp_item.amount = 1;
  5007. tmp_item.identify = itemdb_isidentified(itemid);
  5008. mob_setdropitem_option(&tmp_item, &md->db->dropitem[i]);
  5009. flag = pc_additem(sd,&tmp_item,1,LOG_TYPE_PICKDROP_PLAYER);
  5010. //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?
  5011. md->state.steal_flag = UCHAR_MAX; //you can't steal from this mob any more
  5012. if(flag) { //Failed to steal due to overweight
  5013. clif_additem(sd,0,0,flag);
  5014. return false;
  5015. }
  5016. if(battle_config.show_steal_in_same_party)
  5017. party_foreachsamemap(pc_show_steal,sd,AREA_SIZE,sd,tmp_item.nameid);
  5018. //Logs items, Stolen from mobs [Lupus]
  5019. log_pick_mob(md, LOG_TYPE_STEAL, -1, &tmp_item);
  5020. //A Rare Steal Global Announce by Lupus
  5021. if(md->db->dropitem[i].p<=battle_config.rare_drop_announce) {
  5022. struct item_data *i_data;
  5023. char message[128];
  5024. i_data = itemdb_search(itemid);
  5025. sprintf (message, msg_txt(sd,542), (sd->status.name[0])?sd->status.name :"GM", md->db->jname, i_data->jname, (float)md->db->dropitem[i].p/100);
  5026. //MSG: "'%s' stole %s's %s (chance: %0.02f%%)"
  5027. intif_broadcast(message, strlen(message) + 1, BC_DEFAULT);
  5028. }
  5029. return true;
  5030. }
  5031. /*==========================================
  5032. * Stole zeny from bl (mob)
  5033. * return
  5034. * 0 = fail
  5035. * 1 = success
  5036. *------------------------------------------*/
  5037. int pc_steal_coin(struct map_session_data *sd,struct block_list *target)
  5038. {
  5039. int rate, target_lv;
  5040. struct mob_data *md;
  5041. if(!sd || !target || target->type != BL_MOB)
  5042. return 0;
  5043. md = (TBL_MOB*)target;
  5044. target_lv = status_get_lv(target);
  5045. if (md->state.steal_coin_flag || md->sc.data[SC_STONE] || md->sc.data[SC_FREEZE] || status_bl_has_mode(target,MD_STATUS_IMMUNE) || status_get_race2(&md->bl) == RC2_TREASURE)
  5046. return 0;
  5047. rate = sd->battle_status.dex / 2 + 2 * (sd->status.base_level - target_lv) + (10 * pc_checkskill(sd, RG_STEALCOIN)) + sd->battle_status.luk / 2;
  5048. if(rnd()%1000 < rate)
  5049. {
  5050. // Zeny Steal Amount: (rnd() % (10 * target_lv + 1 - 8 * target_lv)) + 8 * target_lv
  5051. int amount = (rnd() % (2 * target_lv + 1)) + 8 * target_lv; // Reduced formula
  5052. pc_getzeny(sd, amount, LOG_TYPE_STEAL, NULL);
  5053. md->state.steal_coin_flag = 1;
  5054. return 1;
  5055. }
  5056. return 0;
  5057. }
  5058. /*==========================================
  5059. * Set's a player position.
  5060. * @param sd
  5061. * @param mapindex
  5062. * @param x
  5063. * @param y
  5064. * @param clrtype
  5065. * @return SETPOS_OK Success
  5066. * SETPOS_MAPINDEX Invalid map index
  5067. * SETPOS_NO_MAPSERVER Map not in this map-server, and failed to locate alternate map-server.
  5068. * SETPOS_AUTOTRADE Player is in autotrade state
  5069. *------------------------------------------*/
  5070. enum e_setpos pc_setpos(struct map_session_data* sd, unsigned short mapindex, int x, int y, clr_type clrtype)
  5071. {
  5072. nullpo_retr(SETPOS_OK,sd);
  5073. if( !mapindex || !mapindex_id2name(mapindex) ) {
  5074. ShowDebug("pc_setpos: Passed mapindex(%d) is invalid!\n", mapindex);
  5075. return SETPOS_MAPINDEX;
  5076. }
  5077. if ( sd->state.autotrade && (sd->vender_id || sd->buyer_id) ) // Player with autotrade just causes clif glitch! @ FIXME
  5078. return SETPOS_AUTOTRADE;
  5079. if( battle_config.revive_onwarp && pc_isdead(sd) ) { //Revive dead people before warping them
  5080. pc_setstand(sd, true);
  5081. pc_setrestartvalue(sd,1);
  5082. }
  5083. int16 m = map_mapindex2mapid(mapindex);
  5084. struct map_data *mapdata = map_getmapdata(m);
  5085. sd->state.changemap = (sd->mapindex != mapindex);
  5086. sd->state.warping = 1;
  5087. sd->state.workinprogress = WIP_DISABLE_NONE;
  5088. if( sd->state.changemap ) { // Misc map-changing settings
  5089. unsigned short curr_map_instance_id = map_getmapdata(sd->bl.m)->instance_id, new_map_instance_id = (mapdata ? mapdata->instance_id : 0);
  5090. if (curr_map_instance_id != new_map_instance_id) {
  5091. if (curr_map_instance_id) // Update instance timer for the map on leave
  5092. instance_delusers(curr_map_instance_id);
  5093. if (new_map_instance_id) // Update instance timer for the map on enter
  5094. instance_addusers(new_map_instance_id);
  5095. }
  5096. if (sd->bg_id && !mapdata->flag[MF_BATTLEGROUND]) // Moving to a map that isn't a Battlegrounds
  5097. bg_team_leave(sd, false, true);
  5098. sd->state.pmap = sd->bl.m;
  5099. if (sd->sc.count) { // Cancel some map related stuff.
  5100. if (sd->sc.data[SC_JAILED])
  5101. return SETPOS_MAPINDEX; //You may not get out!
  5102. status_change_end(&sd->bl, SC_BOSSMAPINFO, INVALID_TIMER);
  5103. status_change_end(&sd->bl, SC_WARM, INVALID_TIMER);
  5104. status_change_end(&sd->bl, SC_SUN_COMFORT, INVALID_TIMER);
  5105. status_change_end(&sd->bl, SC_MOON_COMFORT, INVALID_TIMER);
  5106. status_change_end(&sd->bl, SC_STAR_COMFORT, INVALID_TIMER);
  5107. status_change_end(&sd->bl, SC_MIRACLE, INVALID_TIMER);
  5108. if (sd->sc.data[SC_KNOWLEDGE]) {
  5109. struct status_change_entry *sce = sd->sc.data[SC_KNOWLEDGE];
  5110. if (sce->timer != INVALID_TIMER)
  5111. delete_timer(sce->timer, status_change_timer);
  5112. sce->timer = add_timer(gettick() + skill_get_time(SG_KNOWLEDGE, sce->val1), status_change_timer, sd->bl.id, SC_KNOWLEDGE);
  5113. }
  5114. status_change_end(&sd->bl, SC_PROPERTYWALK, INVALID_TIMER);
  5115. status_change_end(&sd->bl, SC_CLOAKING, INVALID_TIMER);
  5116. status_change_end(&sd->bl, SC_CLOAKINGEXCEED, INVALID_TIMER);
  5117. }
  5118. for(int i = 0; i < EQI_MAX; i++ ) {
  5119. if( sd->equip_index[i] >= 0 )
  5120. if( pc_isequip(sd,sd->equip_index[i]) )
  5121. pc_unequipitem(sd,sd->equip_index[i],2);
  5122. }
  5123. if (battle_config.clear_unit_onwarp&BL_PC)
  5124. skill_clear_unitgroup(&sd->bl);
  5125. party_send_dot_remove(sd); //minimap dot fix [Kevin]
  5126. guild_send_dot_remove(sd);
  5127. bg_send_dot_remove(sd);
  5128. if (sd->regen.state.gc)
  5129. sd->regen.state.gc = 0;
  5130. // make sure vending is allowed here
  5131. if (sd->state.vending && mapdata->flag[MF_NOVENDING]) {
  5132. clif_displaymessage (sd->fd, msg_txt(sd,276)); // "You can't open a shop on this map"
  5133. vending_closevending(sd);
  5134. }
  5135. channel_pcquit(sd,4); //quit map chan
  5136. }
  5137. if( m < 0 )
  5138. {
  5139. uint32 ip;
  5140. uint16 port;
  5141. struct script_state *st;
  5142. //if can't find any map-servers, just abort setting position.
  5143. if(!sd->mapindex || map_mapname2ipport(mapindex,&ip,&port))
  5144. return SETPOS_NO_MAPSERVER;
  5145. if (sd->npc_id){
  5146. npc_event_dequeue(sd,false);
  5147. st = sd->st;
  5148. }else{
  5149. st = nullptr;
  5150. }
  5151. npc_script_event(sd, NPCE_LOGOUT);
  5152. //remove from map, THEN change x/y coordinates
  5153. unit_remove_map_pc(sd,clrtype);
  5154. sd->mapindex = mapindex;
  5155. sd->bl.x=x;
  5156. sd->bl.y=y;
  5157. pc_clean_skilltree(sd);
  5158. chrif_save(sd, CSAVE_CHANGE_MAPSERV|CSAVE_INVENTORY|CSAVE_CART);
  5159. chrif_changemapserver(sd, ip, (short)port);
  5160. //Free session data from this map server [Kevin]
  5161. unit_free_pc(sd);
  5162. if( st ){
  5163. // Has to be done here, because otherwise unit_free_pc will free the stack already
  5164. st->state = END;
  5165. }
  5166. return SETPOS_OK;
  5167. }
  5168. if( x < 0 || x >= mapdata->xs || y < 0 || y >= mapdata->ys )
  5169. {
  5170. 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);
  5171. x = y = 0; // make it random
  5172. }
  5173. if( x == 0 && y == 0 ) { // pick a random walkable cell
  5174. int c=0;
  5175. do {
  5176. x = rnd()%(mapdata->xs-2)+1;
  5177. y = rnd()%(mapdata->ys-2)+1;
  5178. c++;
  5179. if(c > (mapdata->xs * mapdata->ys)*3){ //force out
  5180. 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));
  5181. return SETPOS_OK; //preventing warp
  5182. //break; //allow warp anyway
  5183. }
  5184. } while(map_getcell(m,x,y,CELL_CHKNOPASS) || (!battle_config.teleport_on_portal && npc_check_areanpc(1,m,x,y,1)));
  5185. }
  5186. if (sd->state.vending && map_getcell(m,x,y,CELL_CHKNOVENDING)) {
  5187. clif_displaymessage (sd->fd, msg_txt(sd,204)); // "You can't open a shop on this cell."
  5188. vending_closevending(sd);
  5189. }
  5190. if(sd->bl.prev != NULL){
  5191. unit_remove_map_pc(sd,clrtype);
  5192. clif_changemap(sd,m,x,y); // [MouseJstr]
  5193. } else if(sd->state.active) //Tag player for rewarping after map-loading is done. [Skotlex]
  5194. sd->state.rewarp = 1;
  5195. sd->mapindex = mapindex;
  5196. sd->bl.m = m;
  5197. sd->bl.x = sd->ud.to_x = x;
  5198. sd->bl.y = sd->ud.to_y = y;
  5199. if( sd->status.guild_id > 0 && mapdata->flag[MF_GVG_CASTLE] )
  5200. { // Increased guild castle regen [Valaris]
  5201. struct guild_castle *gc = guild_mapindex2gc(sd->mapindex);
  5202. if(gc && gc->guild_id == sd->status.guild_id)
  5203. sd->regen.state.gc = 1;
  5204. }
  5205. if( sd->status.pet_id > 0 && sd->pd && sd->pd->pet.intimate > 0 )
  5206. {
  5207. sd->pd->bl.m = m;
  5208. sd->pd->bl.x = sd->pd->ud.to_x = x;
  5209. sd->pd->bl.y = sd->pd->ud.to_y = y;
  5210. sd->pd->ud.dir = sd->ud.dir;
  5211. }
  5212. if( hom_is_active(sd->hd) )
  5213. {
  5214. sd->hd->bl.m = m;
  5215. sd->hd->bl.x = sd->hd->ud.to_x = x;
  5216. sd->hd->bl.y = sd->hd->ud.to_y = y;
  5217. sd->hd->ud.dir = sd->ud.dir;
  5218. }
  5219. if( sd->md )
  5220. {
  5221. sd->md->bl.m = m;
  5222. sd->md->bl.x = sd->md->ud.to_x = x;
  5223. sd->md->bl.y = sd->md->ud.to_y = y;
  5224. sd->md->ud.dir = sd->ud.dir;
  5225. }
  5226. if( sd->ed ) {
  5227. sd->ed->bl.m = m;
  5228. sd->ed->bl.x = sd->ed->ud.to_x = x;
  5229. sd->ed->bl.y = sd->ed->ud.to_y = y;
  5230. sd->ed->ud.dir = sd->ud.dir;
  5231. }
  5232. pc_cell_basilica(sd);
  5233. //check if we gonna be rewarped [lighta]
  5234. if(npc_check_areanpc(1,m,x,y,0)){
  5235. sd->count_rewarp++;
  5236. }
  5237. else
  5238. sd->count_rewarp = 0;
  5239. return SETPOS_OK;
  5240. }
  5241. /*==========================================
  5242. * Warp player sd to random location on current map.
  5243. * May fail if no walkable cell found (1000 attempts).
  5244. * Return:
  5245. * 0 = Success
  5246. * 1,2,3 = Fail
  5247. *------------------------------------------*/
  5248. char pc_randomwarp(struct map_session_data *sd, clr_type type)
  5249. {
  5250. int x,y,i=0;
  5251. nullpo_ret(sd);
  5252. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  5253. if (mapdata->flag[MF_NOTELEPORT]) //Teleport forbidden
  5254. return 3;
  5255. do {
  5256. x = rnd()%(mapdata->xs-2)+1;
  5257. y = rnd()%(mapdata->ys-2)+1;
  5258. } 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);
  5259. if (i < 1000)
  5260. return pc_setpos(sd,mapdata->index,x,y,type);
  5261. return 3;
  5262. }
  5263. /*==========================================
  5264. * Records a memo point at sd's current position
  5265. * pos - entry to replace, (-1: shift oldest entry out)
  5266. *------------------------------------------*/
  5267. bool pc_memo(struct map_session_data* sd, int pos)
  5268. {
  5269. int skill;
  5270. nullpo_ret(sd);
  5271. // check mapflags
  5272. 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) ) {
  5273. clif_skill_teleportmessage(sd, 1); // "Saved point cannot be memorized."
  5274. return false;
  5275. }
  5276. // check inputs
  5277. if( pos < -1 || pos >= MAX_MEMOPOINTS )
  5278. return false; // invalid input
  5279. // check required skill level
  5280. skill = pc_checkskill(sd, AL_WARP);
  5281. if( skill < 1 ) {
  5282. clif_skill_memomessage(sd,2); // "You haven't learned Warp."
  5283. return false;
  5284. }
  5285. if( skill < 2 || skill - 2 < pos ) {
  5286. clif_skill_memomessage(sd,1); // "Skill Level is not high enough."
  5287. return false;
  5288. }
  5289. if( pos == -1 )
  5290. {
  5291. uint8 i;
  5292. // prevent memo-ing the same map multiple times
  5293. ARR_FIND( 0, MAX_MEMOPOINTS, i, sd->status.memo_point[i].map == map_id2index(sd->bl.m) );
  5294. memmove(&sd->status.memo_point[1], &sd->status.memo_point[0], (u8min(i,MAX_MEMOPOINTS-1))*sizeof(struct point));
  5295. pos = 0;
  5296. }
  5297. if( map_getmapdata(sd->bl.m)->instance_id ) {
  5298. clif_displaymessage( sd->fd, msg_txt(sd,384) ); // You cannot create a memo in an instance.
  5299. return false;
  5300. }
  5301. sd->status.memo_point[pos].map = map_id2index(sd->bl.m);
  5302. sd->status.memo_point[pos].x = sd->bl.x;
  5303. sd->status.memo_point[pos].y = sd->bl.y;
  5304. clif_skill_memomessage(sd, 0);
  5305. return true;
  5306. }
  5307. //
  5308. // Skills
  5309. //
  5310. /**
  5311. * Get the skill current cooldown for player.
  5312. * (get the db base cooldown for skill + player specific cooldown)
  5313. * @param sd : player pointer
  5314. * @param id : skill id
  5315. * @param lv : skill lv
  5316. * @return player skill cooldown
  5317. */
  5318. int pc_get_skillcooldown(struct map_session_data *sd, uint16 skill_id, uint16 skill_lv) {
  5319. int cooldown = skill_get_cooldown(skill_id, skill_lv);
  5320. if (cooldown == 0)
  5321. return 0;
  5322. if (skill_id == SU_TUNABELLY && pc_checkskill(sd, SU_SPIRITOFSEA))
  5323. cooldown -= skill_get_time(SU_TUNABELLY, skill_lv);
  5324. for (auto &it : sd->skillcooldown) {
  5325. if (it.id == skill_id) {
  5326. cooldown += it.val;
  5327. cooldown = max(0, cooldown);
  5328. break;
  5329. }
  5330. }
  5331. return cooldown;
  5332. }
  5333. /*==========================================
  5334. * Return player sd skill_lv learned for given skill
  5335. *------------------------------------------*/
  5336. uint8 pc_checkskill(struct map_session_data *sd, uint16 skill_id)
  5337. {
  5338. uint16 idx = 0;
  5339. if (sd == NULL)
  5340. return 0;
  5341. if ((idx = skill_get_index(skill_id)) == 0) {
  5342. ShowError("pc_checkskill: Invalid skill id %d (char_id=%d).\n", skill_id, sd->status.char_id);
  5343. return 0;
  5344. }
  5345. if (SKILL_CHK_GUILD(skill_id) ) {
  5346. struct guild *g;
  5347. if( sd->status.guild_id>0 && (g=sd->guild)!=NULL)
  5348. return guild_checkskill(g,skill_id);
  5349. return 0;
  5350. }
  5351. return (sd->status.skill[idx].id == skill_id) ? sd->status.skill[idx].lv : 0;
  5352. }
  5353. /**
  5354. * Check if we still have the correct weapon to continue the skill (actually status)
  5355. * If not ending it
  5356. * @param sd
  5357. * @return 0:error, 1:check done
  5358. */
  5359. static void pc_checkallowskill(struct map_session_data *sd)
  5360. {
  5361. const enum sc_type scw_list[] = {
  5362. SC_TWOHANDQUICKEN,
  5363. SC_ONEHAND,
  5364. SC_AURABLADE,
  5365. SC_PARRYING,
  5366. SC_SPEARQUICKEN,
  5367. SC_ADRENALINE,
  5368. SC_ADRENALINE2,
  5369. SC_DANCING,
  5370. SC_GATLINGFEVER,
  5371. };
  5372. uint8 i;
  5373. nullpo_retv(sd);
  5374. if(!sd->sc.count)
  5375. return;
  5376. for (i = 0; i < ARRAYLENGTH(scw_list); i++)
  5377. { // Skills requiring specific weapon types
  5378. if( scw_list[i] == SC_DANCING && !battle_config.dancing_weaponswitch_fix )
  5379. continue;
  5380. if(sd->sc.data[scw_list[i]] &&
  5381. !pc_check_weapontype(sd,skill_get_weapontype(status_sc2skill(scw_list[i]))))
  5382. status_change_end(&sd->bl, scw_list[i], INVALID_TIMER);
  5383. }
  5384. if(sd->sc.data[SC_SPURT] && sd->status.weapon)
  5385. // Spurt requires bare hands (feet, in fact xD)
  5386. status_change_end(&sd->bl, SC_SPURT, INVALID_TIMER);
  5387. if(sd->status.shield <= 0) { // Skills requiring a shield
  5388. const enum sc_type scs_list[] = {
  5389. SC_AUTOGUARD,
  5390. SC_DEFENDER,
  5391. SC_REFLECTSHIELD,
  5392. SC_REFLECTDAMAGE
  5393. };
  5394. for (i = 0; i < ARRAYLENGTH(scs_list); i++)
  5395. if(sd->sc.data[scs_list[i]])
  5396. status_change_end(&sd->bl, scs_list[i], INVALID_TIMER);
  5397. }
  5398. }
  5399. /*==========================================
  5400. * Return equipped index of item on player sd at pos
  5401. * Return
  5402. * -1 : Nothing equipped
  5403. * idx : (this index could be used in inventory to found item_data)
  5404. *------------------------------------------*/
  5405. short pc_checkequip(struct map_session_data *sd,int pos, bool checkall)
  5406. {
  5407. uint8 i;
  5408. nullpo_retr(-1, sd);
  5409. for(i=0;i<EQI_MAX;i++){
  5410. if(pos & equip_bitmask[i]){
  5411. if( checkall && ( pos&~equip_bitmask[i] ) != 0 && sd->equip_index[i] == -1 ){
  5412. // Check all if any match is found
  5413. continue;
  5414. }
  5415. return sd->equip_index[i];
  5416. }
  5417. }
  5418. return -1;
  5419. }
  5420. /*==========================================
  5421. * Check if sd has nameid equipped somewhere
  5422. * @sd : the player session
  5423. * @nameid : id of the item to check
  5424. * @min : : see pc.hpp enum equip_index from ? to @max
  5425. * @max : see pc.hpp enum equip_index for @min to ?
  5426. * -return true,false
  5427. *------------------------------------------*/
  5428. bool pc_checkequip2(struct map_session_data *sd, unsigned short nameid, int min, int max)
  5429. {
  5430. int i;
  5431. for(i = min; i < max; i++) {
  5432. if(equip_bitmask[i]) {
  5433. int idx = sd->equip_index[i];
  5434. if (sd->inventory.u.items_inventory[idx].nameid == nameid)
  5435. return true;
  5436. }
  5437. }
  5438. return false;
  5439. }
  5440. /*==========================================
  5441. * Convert's from the client's lame Job ID system
  5442. * to the map server's 'makes sense' system. [Skotlex]
  5443. *------------------------------------------*/
  5444. int pc_jobid2mapid(unsigned short b_class)
  5445. {
  5446. switch(b_class)
  5447. {
  5448. //Novice And 1-1 Jobs
  5449. case JOB_NOVICE: return MAPID_NOVICE;
  5450. case JOB_SWORDMAN: return MAPID_SWORDMAN;
  5451. case JOB_MAGE: return MAPID_MAGE;
  5452. case JOB_ARCHER: return MAPID_ARCHER;
  5453. case JOB_ACOLYTE: return MAPID_ACOLYTE;
  5454. case JOB_MERCHANT: return MAPID_MERCHANT;
  5455. case JOB_THIEF: return MAPID_THIEF;
  5456. case JOB_TAEKWON: return MAPID_TAEKWON;
  5457. case JOB_WEDDING: return MAPID_WEDDING;
  5458. case JOB_GUNSLINGER: return MAPID_GUNSLINGER;
  5459. case JOB_NINJA: return MAPID_NINJA;
  5460. case JOB_XMAS: return MAPID_XMAS;
  5461. case JOB_SUMMER: return MAPID_SUMMER;
  5462. case JOB_HANBOK: return MAPID_HANBOK;
  5463. case JOB_GANGSI: return MAPID_GANGSI;
  5464. case JOB_OKTOBERFEST: return MAPID_OKTOBERFEST;
  5465. case JOB_SUMMER2: return MAPID_SUMMER2;
  5466. //2-1 Jobs
  5467. case JOB_SUPER_NOVICE: return MAPID_SUPER_NOVICE;
  5468. case JOB_KNIGHT: return MAPID_KNIGHT;
  5469. case JOB_WIZARD: return MAPID_WIZARD;
  5470. case JOB_HUNTER: return MAPID_HUNTER;
  5471. case JOB_PRIEST: return MAPID_PRIEST;
  5472. case JOB_BLACKSMITH: return MAPID_BLACKSMITH;
  5473. case JOB_ASSASSIN: return MAPID_ASSASSIN;
  5474. case JOB_STAR_GLADIATOR: return MAPID_STAR_GLADIATOR;
  5475. case JOB_KAGEROU:
  5476. case JOB_OBORO: return MAPID_KAGEROUOBORO;
  5477. case JOB_REBELLION: return MAPID_REBELLION;
  5478. case JOB_DEATH_KNIGHT: return MAPID_DEATH_KNIGHT;
  5479. //2-2 Jobs
  5480. case JOB_CRUSADER: return MAPID_CRUSADER;
  5481. case JOB_SAGE: return MAPID_SAGE;
  5482. case JOB_BARD:
  5483. case JOB_DANCER: return MAPID_BARDDANCER;
  5484. case JOB_MONK: return MAPID_MONK;
  5485. case JOB_ALCHEMIST: return MAPID_ALCHEMIST;
  5486. case JOB_ROGUE: return MAPID_ROGUE;
  5487. case JOB_SOUL_LINKER: return MAPID_SOUL_LINKER;
  5488. case JOB_DARK_COLLECTOR: return MAPID_DARK_COLLECTOR;
  5489. //Trans Novice And Trans 1-1 Jobs
  5490. case JOB_NOVICE_HIGH: return MAPID_NOVICE_HIGH;
  5491. case JOB_SWORDMAN_HIGH: return MAPID_SWORDMAN_HIGH;
  5492. case JOB_MAGE_HIGH: return MAPID_MAGE_HIGH;
  5493. case JOB_ARCHER_HIGH: return MAPID_ARCHER_HIGH;
  5494. case JOB_ACOLYTE_HIGH: return MAPID_ACOLYTE_HIGH;
  5495. case JOB_MERCHANT_HIGH: return MAPID_MERCHANT_HIGH;
  5496. case JOB_THIEF_HIGH: return MAPID_THIEF_HIGH;
  5497. //Trans 2-1 Jobs
  5498. case JOB_LORD_KNIGHT: return MAPID_LORD_KNIGHT;
  5499. case JOB_HIGH_WIZARD: return MAPID_HIGH_WIZARD;
  5500. case JOB_SNIPER: return MAPID_SNIPER;
  5501. case JOB_HIGH_PRIEST: return MAPID_HIGH_PRIEST;
  5502. case JOB_WHITESMITH: return MAPID_WHITESMITH;
  5503. case JOB_ASSASSIN_CROSS: return MAPID_ASSASSIN_CROSS;
  5504. //Trans 2-2 Jobs
  5505. case JOB_PALADIN: return MAPID_PALADIN;
  5506. case JOB_PROFESSOR: return MAPID_PROFESSOR;
  5507. case JOB_CLOWN:
  5508. case JOB_GYPSY: return MAPID_CLOWNGYPSY;
  5509. case JOB_CHAMPION: return MAPID_CHAMPION;
  5510. case JOB_CREATOR: return MAPID_CREATOR;
  5511. case JOB_STALKER: return MAPID_STALKER;
  5512. //Baby Novice And Baby 1-1 Jobs
  5513. case JOB_BABY: return MAPID_BABY;
  5514. case JOB_BABY_SWORDMAN: return MAPID_BABY_SWORDMAN;
  5515. case JOB_BABY_MAGE: return MAPID_BABY_MAGE;
  5516. case JOB_BABY_ARCHER: return MAPID_BABY_ARCHER;
  5517. case JOB_BABY_ACOLYTE: return MAPID_BABY_ACOLYTE;
  5518. case JOB_BABY_MERCHANT: return MAPID_BABY_MERCHANT;
  5519. case JOB_BABY_THIEF: return MAPID_BABY_THIEF;
  5520. case JOB_BABY_TAEKWON: return MAPID_BABY_TAEKWON;
  5521. case JOB_BABY_GUNSLINGER: return MAPID_BABY_GUNSLINGER;
  5522. case JOB_BABY_NINJA: return MAPID_BABY_NINJA;
  5523. case JOB_BABY_SUMMONER: return MAPID_BABY_SUMMONER;
  5524. //Baby 2-1 Jobs
  5525. case JOB_SUPER_BABY: return MAPID_SUPER_BABY;
  5526. case JOB_BABY_KNIGHT: return MAPID_BABY_KNIGHT;
  5527. case JOB_BABY_WIZARD: return MAPID_BABY_WIZARD;
  5528. case JOB_BABY_HUNTER: return MAPID_BABY_HUNTER;
  5529. case JOB_BABY_PRIEST: return MAPID_BABY_PRIEST;
  5530. case JOB_BABY_BLACKSMITH: return MAPID_BABY_BLACKSMITH;
  5531. case JOB_BABY_ASSASSIN: return MAPID_BABY_ASSASSIN;
  5532. case JOB_BABY_STAR_GLADIATOR: return MAPID_BABY_STAR_GLADIATOR;
  5533. case JOB_BABY_REBELLION: return MAPID_BABY_REBELLION;
  5534. case JOB_BABY_KAGEROU:
  5535. case JOB_BABY_OBORO: return MAPID_BABY_KAGEROUOBORO;
  5536. //Baby 2-2 Jobs
  5537. case JOB_BABY_CRUSADER: return MAPID_BABY_CRUSADER;
  5538. case JOB_BABY_SAGE: return MAPID_BABY_SAGE;
  5539. case JOB_BABY_BARD:
  5540. case JOB_BABY_DANCER: return MAPID_BABY_BARDDANCER;
  5541. case JOB_BABY_MONK: return MAPID_BABY_MONK;
  5542. case JOB_BABY_ALCHEMIST: return MAPID_BABY_ALCHEMIST;
  5543. case JOB_BABY_ROGUE: return MAPID_BABY_ROGUE;
  5544. case JOB_BABY_SOUL_LINKER: return MAPID_BABY_SOUL_LINKER;
  5545. //3-1 Jobs
  5546. case JOB_SUPER_NOVICE_E: return MAPID_SUPER_NOVICE_E;
  5547. case JOB_RUNE_KNIGHT: return MAPID_RUNE_KNIGHT;
  5548. case JOB_WARLOCK: return MAPID_WARLOCK;
  5549. case JOB_RANGER: return MAPID_RANGER;
  5550. case JOB_ARCH_BISHOP: return MAPID_ARCH_BISHOP;
  5551. case JOB_MECHANIC: return MAPID_MECHANIC;
  5552. case JOB_GUILLOTINE_CROSS: return MAPID_GUILLOTINE_CROSS;
  5553. case JOB_STAR_EMPEROR: return MAPID_STAR_EMPEROR;
  5554. //3-2 Jobs
  5555. case JOB_ROYAL_GUARD: return MAPID_ROYAL_GUARD;
  5556. case JOB_SORCERER: return MAPID_SORCERER;
  5557. case JOB_MINSTREL:
  5558. case JOB_WANDERER: return MAPID_MINSTRELWANDERER;
  5559. case JOB_SURA: return MAPID_SURA;
  5560. case JOB_GENETIC: return MAPID_GENETIC;
  5561. case JOB_SHADOW_CHASER: return MAPID_SHADOW_CHASER;
  5562. case JOB_SOUL_REAPER: return MAPID_SOUL_REAPER;
  5563. //Trans 3-1 Jobs
  5564. case JOB_RUNE_KNIGHT_T: return MAPID_RUNE_KNIGHT_T;
  5565. case JOB_WARLOCK_T: return MAPID_WARLOCK_T;
  5566. case JOB_RANGER_T: return MAPID_RANGER_T;
  5567. case JOB_ARCH_BISHOP_T: return MAPID_ARCH_BISHOP_T;
  5568. case JOB_MECHANIC_T: return MAPID_MECHANIC_T;
  5569. case JOB_GUILLOTINE_CROSS_T: return MAPID_GUILLOTINE_CROSS_T;
  5570. //Trans 3-2 Jobs
  5571. case JOB_ROYAL_GUARD_T: return MAPID_ROYAL_GUARD_T;
  5572. case JOB_SORCERER_T: return MAPID_SORCERER_T;
  5573. case JOB_MINSTREL_T:
  5574. case JOB_WANDERER_T: return MAPID_MINSTRELWANDERER_T;
  5575. case JOB_SURA_T: return MAPID_SURA_T;
  5576. case JOB_GENETIC_T: return MAPID_GENETIC_T;
  5577. case JOB_SHADOW_CHASER_T: return MAPID_SHADOW_CHASER_T;
  5578. //Baby 3-1 Jobs
  5579. case JOB_SUPER_BABY_E: return MAPID_SUPER_BABY_E;
  5580. case JOB_BABY_RUNE_KNIGHT: return MAPID_BABY_RUNE_KNIGHT;
  5581. case JOB_BABY_WARLOCK: return MAPID_BABY_WARLOCK;
  5582. case JOB_BABY_RANGER: return MAPID_BABY_RANGER;
  5583. case JOB_BABY_ARCH_BISHOP: return MAPID_BABY_ARCH_BISHOP;
  5584. case JOB_BABY_MECHANIC: return MAPID_BABY_MECHANIC;
  5585. case JOB_BABY_GUILLOTINE_CROSS: return MAPID_BABY_GUILLOTINE_CROSS;
  5586. case JOB_BABY_STAR_EMPEROR: return MAPID_BABY_STAR_EMPEROR;
  5587. //Baby 3-2 Jobs
  5588. case JOB_BABY_ROYAL_GUARD: return MAPID_BABY_ROYAL_GUARD;
  5589. case JOB_BABY_SORCERER: return MAPID_BABY_SORCERER;
  5590. case JOB_BABY_MINSTREL:
  5591. case JOB_BABY_WANDERER: return MAPID_BABY_MINSTRELWANDERER;
  5592. case JOB_BABY_SURA: return MAPID_BABY_SURA;
  5593. case JOB_BABY_GENETIC: return MAPID_BABY_GENETIC;
  5594. case JOB_BABY_SHADOW_CHASER: return MAPID_BABY_SHADOW_CHASER;
  5595. case JOB_BABY_SOUL_REAPER: return MAPID_BABY_SOUL_REAPER;
  5596. //Doram Jobs
  5597. case JOB_SUMMONER: return MAPID_SUMMONER;
  5598. default:
  5599. return -1;
  5600. }
  5601. }
  5602. //Reverts the map-style class id to the client-style one.
  5603. int pc_mapid2jobid(unsigned short class_, int sex)
  5604. {
  5605. switch(class_) {
  5606. //Novice And 1-1 Jobs
  5607. case MAPID_NOVICE: return JOB_NOVICE;
  5608. case MAPID_SWORDMAN: return JOB_SWORDMAN;
  5609. case MAPID_MAGE: return JOB_MAGE;
  5610. case MAPID_ARCHER: return JOB_ARCHER;
  5611. case MAPID_ACOLYTE: return JOB_ACOLYTE;
  5612. case MAPID_MERCHANT: return JOB_MERCHANT;
  5613. case MAPID_THIEF: return JOB_THIEF;
  5614. case MAPID_TAEKWON: return JOB_TAEKWON;
  5615. case MAPID_WEDDING: return JOB_WEDDING;
  5616. case MAPID_GUNSLINGER: return JOB_GUNSLINGER;
  5617. case MAPID_NINJA: return JOB_NINJA;
  5618. case MAPID_XMAS: return JOB_XMAS;
  5619. case MAPID_SUMMER: return JOB_SUMMER;
  5620. case MAPID_HANBOK: return JOB_HANBOK;
  5621. case MAPID_GANGSI: return JOB_GANGSI;
  5622. case MAPID_OKTOBERFEST: return JOB_OKTOBERFEST;
  5623. case MAPID_SUMMER2: return JOB_SUMMER2;
  5624. //2-1 Jobs
  5625. case MAPID_SUPER_NOVICE: return JOB_SUPER_NOVICE;
  5626. case MAPID_KNIGHT: return JOB_KNIGHT;
  5627. case MAPID_WIZARD: return JOB_WIZARD;
  5628. case MAPID_HUNTER: return JOB_HUNTER;
  5629. case MAPID_PRIEST: return JOB_PRIEST;
  5630. case MAPID_BLACKSMITH: return JOB_BLACKSMITH;
  5631. case MAPID_ASSASSIN: return JOB_ASSASSIN;
  5632. case MAPID_STAR_GLADIATOR: return JOB_STAR_GLADIATOR;
  5633. case MAPID_KAGEROUOBORO: return sex?JOB_KAGEROU:JOB_OBORO;
  5634. case MAPID_REBELLION: return JOB_REBELLION;
  5635. case MAPID_DEATH_KNIGHT: return JOB_DEATH_KNIGHT;
  5636. //2-2 Jobs
  5637. case MAPID_CRUSADER: return JOB_CRUSADER;
  5638. case MAPID_SAGE: return JOB_SAGE;
  5639. case MAPID_BARDDANCER: return sex?JOB_BARD:JOB_DANCER;
  5640. case MAPID_MONK: return JOB_MONK;
  5641. case MAPID_ALCHEMIST: return JOB_ALCHEMIST;
  5642. case MAPID_ROGUE: return JOB_ROGUE;
  5643. case MAPID_SOUL_LINKER: return JOB_SOUL_LINKER;
  5644. case MAPID_DARK_COLLECTOR: return JOB_DARK_COLLECTOR;
  5645. //Trans Novice And Trans 2-1 Jobs
  5646. case MAPID_NOVICE_HIGH: return JOB_NOVICE_HIGH;
  5647. case MAPID_SWORDMAN_HIGH: return JOB_SWORDMAN_HIGH;
  5648. case MAPID_MAGE_HIGH: return JOB_MAGE_HIGH;
  5649. case MAPID_ARCHER_HIGH: return JOB_ARCHER_HIGH;
  5650. case MAPID_ACOLYTE_HIGH: return JOB_ACOLYTE_HIGH;
  5651. case MAPID_MERCHANT_HIGH: return JOB_MERCHANT_HIGH;
  5652. case MAPID_THIEF_HIGH: return JOB_THIEF_HIGH;
  5653. //Trans 2-1 Jobs
  5654. case MAPID_LORD_KNIGHT: return JOB_LORD_KNIGHT;
  5655. case MAPID_HIGH_WIZARD: return JOB_HIGH_WIZARD;
  5656. case MAPID_SNIPER: return JOB_SNIPER;
  5657. case MAPID_HIGH_PRIEST: return JOB_HIGH_PRIEST;
  5658. case MAPID_WHITESMITH: return JOB_WHITESMITH;
  5659. case MAPID_ASSASSIN_CROSS: return JOB_ASSASSIN_CROSS;
  5660. //Trans 2-2 Jobs
  5661. case MAPID_PALADIN: return JOB_PALADIN;
  5662. case MAPID_PROFESSOR: return JOB_PROFESSOR;
  5663. case MAPID_CLOWNGYPSY: return sex?JOB_CLOWN:JOB_GYPSY;
  5664. case MAPID_CHAMPION: return JOB_CHAMPION;
  5665. case MAPID_CREATOR: return JOB_CREATOR;
  5666. case MAPID_STALKER: return JOB_STALKER;
  5667. //Baby Novice And Baby 1-1 Jobs
  5668. case MAPID_BABY: return JOB_BABY;
  5669. case MAPID_BABY_SWORDMAN: return JOB_BABY_SWORDMAN;
  5670. case MAPID_BABY_MAGE: return JOB_BABY_MAGE;
  5671. case MAPID_BABY_ARCHER: return JOB_BABY_ARCHER;
  5672. case MAPID_BABY_ACOLYTE: return JOB_BABY_ACOLYTE;
  5673. case MAPID_BABY_MERCHANT: return JOB_BABY_MERCHANT;
  5674. case MAPID_BABY_THIEF: return JOB_BABY_THIEF;
  5675. case MAPID_BABY_TAEKWON: return JOB_BABY_TAEKWON;
  5676. case MAPID_BABY_GUNSLINGER: return JOB_BABY_GUNSLINGER;
  5677. case MAPID_BABY_NINJA: return JOB_BABY_NINJA;
  5678. case MAPID_BABY_SUMMONER: return JOB_BABY_SUMMONER;
  5679. //Baby 2-1 Jobs
  5680. case MAPID_SUPER_BABY: return JOB_SUPER_BABY;
  5681. case MAPID_BABY_KNIGHT: return JOB_BABY_KNIGHT;
  5682. case MAPID_BABY_WIZARD: return JOB_BABY_WIZARD;
  5683. case MAPID_BABY_HUNTER: return JOB_BABY_HUNTER;
  5684. case MAPID_BABY_PRIEST: return JOB_BABY_PRIEST;
  5685. case MAPID_BABY_BLACKSMITH: return JOB_BABY_BLACKSMITH;
  5686. case MAPID_BABY_ASSASSIN: return JOB_BABY_ASSASSIN;
  5687. case MAPID_BABY_STAR_GLADIATOR: return JOB_BABY_STAR_GLADIATOR;
  5688. case MAPID_BABY_REBELLION: return JOB_BABY_REBELLION;
  5689. case MAPID_BABY_KAGEROUOBORO: return sex?JOB_BABY_KAGEROU:JOB_BABY_OBORO;
  5690. //Baby 2-2 Jobs
  5691. case MAPID_BABY_CRUSADER: return JOB_BABY_CRUSADER;
  5692. case MAPID_BABY_SAGE: return JOB_BABY_SAGE;
  5693. case MAPID_BABY_BARDDANCER: return sex?JOB_BABY_BARD:JOB_BABY_DANCER;
  5694. case MAPID_BABY_MONK: return JOB_BABY_MONK;
  5695. case MAPID_BABY_ALCHEMIST: return JOB_BABY_ALCHEMIST;
  5696. case MAPID_BABY_ROGUE: return JOB_BABY_ROGUE;
  5697. case MAPID_BABY_SOUL_LINKER: return JOB_BABY_SOUL_LINKER;
  5698. //3-1 Jobs
  5699. case MAPID_SUPER_NOVICE_E: return JOB_SUPER_NOVICE_E;
  5700. case MAPID_RUNE_KNIGHT: return JOB_RUNE_KNIGHT;
  5701. case MAPID_WARLOCK: return JOB_WARLOCK;
  5702. case MAPID_RANGER: return JOB_RANGER;
  5703. case MAPID_ARCH_BISHOP: return JOB_ARCH_BISHOP;
  5704. case MAPID_MECHANIC: return JOB_MECHANIC;
  5705. case MAPID_GUILLOTINE_CROSS: return JOB_GUILLOTINE_CROSS;
  5706. case MAPID_STAR_EMPEROR: return JOB_STAR_EMPEROR;
  5707. //3-2 Jobs
  5708. case MAPID_ROYAL_GUARD: return JOB_ROYAL_GUARD;
  5709. case MAPID_SORCERER: return JOB_SORCERER;
  5710. case MAPID_MINSTRELWANDERER: return sex?JOB_MINSTREL:JOB_WANDERER;
  5711. case MAPID_SURA: return JOB_SURA;
  5712. case MAPID_GENETIC: return JOB_GENETIC;
  5713. case MAPID_SHADOW_CHASER: return JOB_SHADOW_CHASER;
  5714. case MAPID_SOUL_REAPER: return JOB_SOUL_REAPER;
  5715. //Trans 3-1 Jobs
  5716. case MAPID_RUNE_KNIGHT_T: return JOB_RUNE_KNIGHT_T;
  5717. case MAPID_WARLOCK_T: return JOB_WARLOCK_T;
  5718. case MAPID_RANGER_T: return JOB_RANGER_T;
  5719. case MAPID_ARCH_BISHOP_T: return JOB_ARCH_BISHOP_T;
  5720. case MAPID_MECHANIC_T: return JOB_MECHANIC_T;
  5721. case MAPID_GUILLOTINE_CROSS_T: return JOB_GUILLOTINE_CROSS_T;
  5722. //Trans 3-2 Jobs
  5723. case MAPID_ROYAL_GUARD_T: return JOB_ROYAL_GUARD_T;
  5724. case MAPID_SORCERER_T: return JOB_SORCERER_T;
  5725. case MAPID_MINSTRELWANDERER_T: return sex?JOB_MINSTREL_T:JOB_WANDERER_T;
  5726. case MAPID_SURA_T: return JOB_SURA_T;
  5727. case MAPID_GENETIC_T: return JOB_GENETIC_T;
  5728. case MAPID_SHADOW_CHASER_T: return JOB_SHADOW_CHASER_T;
  5729. //Baby 3-1 Jobs
  5730. case MAPID_SUPER_BABY_E: return JOB_SUPER_BABY_E;
  5731. case MAPID_BABY_RUNE_KNIGHT: return JOB_BABY_RUNE_KNIGHT;
  5732. case MAPID_BABY_WARLOCK: return JOB_BABY_WARLOCK;
  5733. case MAPID_BABY_RANGER: return JOB_BABY_RANGER;
  5734. case MAPID_BABY_ARCH_BISHOP: return JOB_BABY_ARCH_BISHOP;
  5735. case MAPID_BABY_MECHANIC: return JOB_BABY_MECHANIC;
  5736. case MAPID_BABY_GUILLOTINE_CROSS: return JOB_BABY_GUILLOTINE_CROSS;
  5737. case MAPID_BABY_STAR_EMPEROR: return JOB_BABY_STAR_EMPEROR;
  5738. //Baby 3-2 Jobs
  5739. case MAPID_BABY_ROYAL_GUARD: return JOB_BABY_ROYAL_GUARD;
  5740. case MAPID_BABY_SORCERER: return JOB_BABY_SORCERER;
  5741. case MAPID_BABY_MINSTRELWANDERER: return sex?JOB_BABY_MINSTREL:JOB_BABY_WANDERER;
  5742. case MAPID_BABY_SURA: return JOB_BABY_SURA;
  5743. case MAPID_BABY_GENETIC: return JOB_BABY_GENETIC;
  5744. case MAPID_BABY_SHADOW_CHASER: return JOB_BABY_SHADOW_CHASER;
  5745. case MAPID_BABY_SOUL_REAPER: return JOB_BABY_SOUL_REAPER;
  5746. //Doram Jobs
  5747. case MAPID_SUMMONER: return JOB_SUMMONER;
  5748. default:
  5749. return -1;
  5750. }
  5751. }
  5752. /*====================================================
  5753. * This function return the name of the job (by [Yor])
  5754. *----------------------------------------------------*/
  5755. const char* job_name(int class_)
  5756. {
  5757. switch (class_) {
  5758. case JOB_NOVICE:
  5759. case JOB_SWORDMAN:
  5760. case JOB_MAGE:
  5761. case JOB_ARCHER:
  5762. case JOB_ACOLYTE:
  5763. case JOB_MERCHANT:
  5764. case JOB_THIEF:
  5765. return msg_txt(NULL,550 - JOB_NOVICE+class_);
  5766. case JOB_KNIGHT:
  5767. case JOB_PRIEST:
  5768. case JOB_WIZARD:
  5769. case JOB_BLACKSMITH:
  5770. case JOB_HUNTER:
  5771. case JOB_ASSASSIN:
  5772. return msg_txt(NULL,557 - JOB_KNIGHT+class_);
  5773. case JOB_KNIGHT2:
  5774. return msg_txt(NULL,557);
  5775. case JOB_CRUSADER:
  5776. case JOB_MONK:
  5777. case JOB_SAGE:
  5778. case JOB_ROGUE:
  5779. case JOB_ALCHEMIST:
  5780. case JOB_BARD:
  5781. case JOB_DANCER:
  5782. return msg_txt(NULL,563 - JOB_CRUSADER+class_);
  5783. case JOB_CRUSADER2:
  5784. return msg_txt(NULL,563);
  5785. case JOB_WEDDING:
  5786. case JOB_SUPER_NOVICE:
  5787. case JOB_GUNSLINGER:
  5788. case JOB_NINJA:
  5789. case JOB_XMAS:
  5790. return msg_txt(NULL,570 - JOB_WEDDING+class_);
  5791. case JOB_SUMMER:
  5792. case JOB_SUMMER2:
  5793. return msg_txt(NULL,621);
  5794. case JOB_HANBOK:
  5795. return msg_txt(NULL,694);
  5796. case JOB_OKTOBERFEST:
  5797. return msg_txt(NULL,696);
  5798. case JOB_NOVICE_HIGH:
  5799. case JOB_SWORDMAN_HIGH:
  5800. case JOB_MAGE_HIGH:
  5801. case JOB_ARCHER_HIGH:
  5802. case JOB_ACOLYTE_HIGH:
  5803. case JOB_MERCHANT_HIGH:
  5804. case JOB_THIEF_HIGH:
  5805. return msg_txt(NULL,575 - JOB_NOVICE_HIGH+class_);
  5806. case JOB_LORD_KNIGHT:
  5807. case JOB_HIGH_PRIEST:
  5808. case JOB_HIGH_WIZARD:
  5809. case JOB_WHITESMITH:
  5810. case JOB_SNIPER:
  5811. case JOB_ASSASSIN_CROSS:
  5812. return msg_txt(NULL,582 - JOB_LORD_KNIGHT+class_);
  5813. case JOB_LORD_KNIGHT2:
  5814. return msg_txt(NULL,582);
  5815. case JOB_PALADIN:
  5816. case JOB_CHAMPION:
  5817. case JOB_PROFESSOR:
  5818. case JOB_STALKER:
  5819. case JOB_CREATOR:
  5820. case JOB_CLOWN:
  5821. case JOB_GYPSY:
  5822. return msg_txt(NULL,588 - JOB_PALADIN + class_);
  5823. case JOB_PALADIN2:
  5824. return msg_txt(NULL,588);
  5825. case JOB_BABY:
  5826. case JOB_BABY_SWORDMAN:
  5827. case JOB_BABY_MAGE:
  5828. case JOB_BABY_ARCHER:
  5829. case JOB_BABY_ACOLYTE:
  5830. case JOB_BABY_MERCHANT:
  5831. case JOB_BABY_THIEF:
  5832. return msg_txt(NULL,595 - JOB_BABY + class_);
  5833. case JOB_BABY_KNIGHT:
  5834. case JOB_BABY_PRIEST:
  5835. case JOB_BABY_WIZARD:
  5836. case JOB_BABY_BLACKSMITH:
  5837. case JOB_BABY_HUNTER:
  5838. case JOB_BABY_ASSASSIN:
  5839. return msg_txt(NULL,602 - JOB_BABY_KNIGHT + class_);
  5840. case JOB_BABY_KNIGHT2:
  5841. return msg_txt(NULL,602);
  5842. case JOB_BABY_CRUSADER:
  5843. case JOB_BABY_MONK:
  5844. case JOB_BABY_SAGE:
  5845. case JOB_BABY_ROGUE:
  5846. case JOB_BABY_ALCHEMIST:
  5847. case JOB_BABY_BARD:
  5848. case JOB_BABY_DANCER:
  5849. return msg_txt(NULL,608 - JOB_BABY_CRUSADER + class_);
  5850. case JOB_BABY_CRUSADER2:
  5851. return msg_txt(NULL,608);
  5852. case JOB_SUPER_BABY:
  5853. return msg_txt(NULL,615);
  5854. case JOB_TAEKWON:
  5855. return msg_txt(NULL,616);
  5856. case JOB_STAR_GLADIATOR:
  5857. case JOB_STAR_GLADIATOR2:
  5858. return msg_txt(NULL,617);
  5859. case JOB_SOUL_LINKER:
  5860. return msg_txt(NULL,618);
  5861. case JOB_GANGSI:
  5862. case JOB_DEATH_KNIGHT:
  5863. case JOB_DARK_COLLECTOR:
  5864. return msg_txt(NULL,622 - JOB_GANGSI+class_);
  5865. case JOB_RUNE_KNIGHT:
  5866. case JOB_WARLOCK:
  5867. case JOB_RANGER:
  5868. case JOB_ARCH_BISHOP:
  5869. case JOB_MECHANIC:
  5870. case JOB_GUILLOTINE_CROSS:
  5871. return msg_txt(NULL,625 - JOB_RUNE_KNIGHT+class_);
  5872. case JOB_RUNE_KNIGHT_T:
  5873. case JOB_WARLOCK_T:
  5874. case JOB_RANGER_T:
  5875. case JOB_ARCH_BISHOP_T:
  5876. case JOB_MECHANIC_T:
  5877. case JOB_GUILLOTINE_CROSS_T:
  5878. return msg_txt(NULL,681 - JOB_RUNE_KNIGHT_T+class_);
  5879. case JOB_ROYAL_GUARD:
  5880. case JOB_SORCERER:
  5881. case JOB_MINSTREL:
  5882. case JOB_WANDERER:
  5883. case JOB_SURA:
  5884. case JOB_GENETIC:
  5885. case JOB_SHADOW_CHASER:
  5886. return msg_txt(NULL,631 - JOB_ROYAL_GUARD+class_);
  5887. case JOB_ROYAL_GUARD_T:
  5888. case JOB_SORCERER_T:
  5889. case JOB_MINSTREL_T:
  5890. case JOB_WANDERER_T:
  5891. case JOB_SURA_T:
  5892. case JOB_GENETIC_T:
  5893. case JOB_SHADOW_CHASER_T:
  5894. return msg_txt(NULL,687 - JOB_ROYAL_GUARD_T+class_);
  5895. case JOB_RUNE_KNIGHT2:
  5896. case JOB_RUNE_KNIGHT_T2:
  5897. return msg_txt(NULL,625);
  5898. case JOB_ROYAL_GUARD2:
  5899. case JOB_ROYAL_GUARD_T2:
  5900. return msg_txt(NULL,631);
  5901. case JOB_RANGER2:
  5902. case JOB_RANGER_T2:
  5903. return msg_txt(NULL,627);
  5904. case JOB_MECHANIC2:
  5905. case JOB_MECHANIC_T2:
  5906. return msg_txt(NULL,629);
  5907. case JOB_BABY_RUNE_KNIGHT:
  5908. case JOB_BABY_WARLOCK:
  5909. case JOB_BABY_RANGER:
  5910. case JOB_BABY_ARCH_BISHOP:
  5911. case JOB_BABY_MECHANIC:
  5912. case JOB_BABY_GUILLOTINE_CROSS:
  5913. case JOB_BABY_ROYAL_GUARD:
  5914. case JOB_BABY_SORCERER:
  5915. case JOB_BABY_MINSTREL:
  5916. case JOB_BABY_WANDERER:
  5917. case JOB_BABY_SURA:
  5918. case JOB_BABY_GENETIC:
  5919. case JOB_BABY_SHADOW_CHASER:
  5920. return msg_txt(NULL,638 - JOB_BABY_RUNE_KNIGHT+class_);
  5921. case JOB_BABY_RUNE_KNIGHT2:
  5922. return msg_txt(NULL,638);
  5923. case JOB_BABY_ROYAL_GUARD2:
  5924. return msg_txt(NULL,644);
  5925. case JOB_BABY_RANGER2:
  5926. return msg_txt(NULL,640);
  5927. case JOB_BABY_MECHANIC2:
  5928. return msg_txt(NULL,642);
  5929. case JOB_SUPER_NOVICE_E:
  5930. case JOB_SUPER_BABY_E:
  5931. return msg_txt(NULL,651 - JOB_SUPER_NOVICE_E+class_);
  5932. case JOB_KAGEROU:
  5933. case JOB_OBORO:
  5934. return msg_txt(NULL,653 - JOB_KAGEROU+class_);
  5935. case JOB_REBELLION:
  5936. return msg_txt(NULL,695);
  5937. case JOB_SUMMONER:
  5938. return msg_txt(NULL,697);
  5939. case JOB_BABY_SUMMONER:
  5940. return msg_txt(NULL,698);
  5941. case JOB_BABY_NINJA:
  5942. return msg_txt(NULL,699);
  5943. case JOB_BABY_KAGEROU:
  5944. case JOB_BABY_OBORO:
  5945. case JOB_BABY_TAEKWON:
  5946. case JOB_BABY_STAR_GLADIATOR:
  5947. case JOB_BABY_SOUL_LINKER:
  5948. case JOB_BABY_GUNSLINGER:
  5949. case JOB_BABY_REBELLION:
  5950. return msg_txt(NULL,753 - JOB_BABY_KAGEROU+class_);
  5951. case JOB_BABY_STAR_GLADIATOR2:
  5952. return msg_txt(NULL,756);
  5953. case JOB_STAR_EMPEROR:
  5954. case JOB_SOUL_REAPER:
  5955. case JOB_BABY_STAR_EMPEROR:
  5956. case JOB_BABY_SOUL_REAPER:
  5957. return msg_txt(NULL,782 - JOB_STAR_EMPEROR + class_);
  5958. case JOB_STAR_EMPEROR2:
  5959. return msg_txt(NULL,782);
  5960. case JOB_BABY_STAR_EMPEROR2:
  5961. return msg_txt(NULL,784);
  5962. default:
  5963. return msg_txt(NULL,655);
  5964. }
  5965. }
  5966. /*====================================================
  5967. * Timered function to make id follow a target.
  5968. * @id = bl.id (player only atm)
  5969. * target is define in sd->followtarget (bl.id)
  5970. * used by pc_follow
  5971. *----------------------------------------------------*/
  5972. TIMER_FUNC(pc_follow_timer){
  5973. struct map_session_data *sd;
  5974. struct block_list *tbl;
  5975. sd = map_id2sd(id);
  5976. nullpo_ret(sd);
  5977. if (sd->followtimer != tid){
  5978. ShowError("pc_follow_timer %d != %d\n",sd->followtimer,tid);
  5979. sd->followtimer = INVALID_TIMER;
  5980. return 0;
  5981. }
  5982. sd->followtimer = INVALID_TIMER;
  5983. tbl = map_id2bl(sd->followtarget);
  5984. if (tbl == NULL || pc_isdead(sd))
  5985. {
  5986. pc_stop_following(sd);
  5987. return 0;
  5988. }
  5989. // either player or target is currently detached from map blocks (could be teleporting),
  5990. // but still connected to this map, so we'll just increment the timer and check back later
  5991. if (sd->bl.prev != NULL && tbl->prev != NULL &&
  5992. sd->ud.skilltimer == INVALID_TIMER && sd->ud.attacktimer == INVALID_TIMER && sd->ud.walktimer == INVALID_TIMER)
  5993. {
  5994. if((sd->bl.m == tbl->m) && unit_can_reach_bl(&sd->bl,tbl, AREA_SIZE, 0, NULL, NULL)) {
  5995. if (!check_distance_bl(&sd->bl, tbl, 5))
  5996. unit_walktobl(&sd->bl, tbl, 5, 0);
  5997. } else
  5998. pc_setpos(sd, map_id2index(tbl->m), tbl->x, tbl->y, CLR_TELEPORT);
  5999. }
  6000. sd->followtimer = add_timer(
  6001. tick + 1000, // increase time a bit to loosen up map's load
  6002. pc_follow_timer, sd->bl.id, 0);
  6003. return 0;
  6004. }
  6005. int pc_stop_following (struct map_session_data *sd)
  6006. {
  6007. nullpo_ret(sd);
  6008. if (sd->followtimer != INVALID_TIMER) {
  6009. delete_timer(sd->followtimer,pc_follow_timer);
  6010. sd->followtimer = INVALID_TIMER;
  6011. }
  6012. sd->followtarget = -1;
  6013. sd->ud.target_to = 0;
  6014. unit_stop_walking(&sd->bl, 1);
  6015. return 0;
  6016. }
  6017. int pc_follow(struct map_session_data *sd,int target_id)
  6018. {
  6019. struct block_list *bl = map_id2bl(target_id);
  6020. if (bl == NULL /*|| bl->type != BL_PC*/)
  6021. return 1;
  6022. if (sd->followtimer != INVALID_TIMER)
  6023. pc_stop_following(sd);
  6024. sd->followtarget = target_id;
  6025. pc_follow_timer(INVALID_TIMER, gettick(), sd->bl.id, 0);
  6026. return 0;
  6027. }
  6028. int pc_checkbaselevelup(struct map_session_data *sd) {
  6029. unsigned int next = pc_nextbaseexp(sd);
  6030. if (!next || sd->status.base_exp < next || pc_is_maxbaselv(sd))
  6031. return 0;
  6032. do {
  6033. sd->status.base_exp -= next;
  6034. //Kyoki pointed out that the max overcarry exp is the exp needed for the previous level -1. [Skotlex]
  6035. 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 )
  6036. sd->status.base_exp = next-1;
  6037. next = pc_gets_status_point(sd->status.base_level);
  6038. sd->status.base_level++;
  6039. sd->status.status_point += next;
  6040. if( pc_is_maxbaselv(sd) ){
  6041. sd->status.base_exp = u32min(sd->status.base_exp,MAX_LEVEL_BASE_EXP);
  6042. break;
  6043. }
  6044. } while ((next=pc_nextbaseexp(sd)) > 0 && sd->status.base_exp >= next);
  6045. if (battle_config.pet_lv_rate && sd->pd) //<Skotlex> update pet's level
  6046. status_calc_pet(sd->pd,SCO_NONE);
  6047. clif_updatestatus(sd,SP_STATUSPOINT);
  6048. clif_updatestatus(sd,SP_BASELEVEL);
  6049. clif_updatestatus(sd,SP_BASEEXP);
  6050. clif_updatestatus(sd,SP_NEXTBASEEXP);
  6051. status_calc_pc(sd,SCO_FORCE);
  6052. status_percent_heal(&sd->bl,100,100);
  6053. if ((sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE) {
  6054. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_KYRIE),100,1,skill_get_time(PR_KYRIE,1));
  6055. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_IMPOSITIO),100,1,skill_get_time(PR_IMPOSITIO,1));
  6056. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_MAGNIFICAT),100,1,skill_get_time(PR_MAGNIFICAT,1));
  6057. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_GLORIA),100,1,skill_get_time(PR_GLORIA,1));
  6058. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_SUFFRAGIUM),100,1,skill_get_time(PR_SUFFRAGIUM,1));
  6059. if (sd->state.snovice_dead_flag)
  6060. sd->state.snovice_dead_flag = 0; //Reenable steelbody resurrection on dead.
  6061. } else if( (sd->class_&MAPID_BASEMASK) == MAPID_TAEKWON ) {
  6062. sc_start(&sd->bl,&sd->bl,status_skill2sc(AL_INCAGI),100,10,600000);
  6063. sc_start(&sd->bl,&sd->bl,status_skill2sc(AL_BLESSING),100,10,600000);
  6064. }
  6065. clif_misceffect(&sd->bl,0);
  6066. npc_script_event(sd, NPCE_BASELVUP); //LORDALFA - LVLUPEVENT
  6067. if(sd->status.party_id)
  6068. party_send_levelup(sd);
  6069. pc_baselevelchanged(sd);
  6070. achievement_update_objective(sd, AG_GOAL_LEVEL, 1, sd->status.base_level);
  6071. achievement_update_objective(sd, AG_GOAL_STATUS, 2, sd->status.base_level, sd->status.class_);
  6072. return 1;
  6073. }
  6074. void pc_baselevelchanged(struct map_session_data *sd) {
  6075. uint8 i;
  6076. for( i = 0; i < EQI_MAX; i++ ) {
  6077. if( sd->equip_index[i] >= 0 ) {
  6078. if( sd->inventory_data[ sd->equip_index[i] ]->elvmax && sd->status.base_level > (unsigned int)sd->inventory_data[ sd->equip_index[i] ]->elvmax )
  6079. pc_unequipitem(sd, sd->equip_index[i], 3);
  6080. }
  6081. }
  6082. pc_show_questinfo(sd);
  6083. }
  6084. int pc_checkjoblevelup(struct map_session_data *sd)
  6085. {
  6086. unsigned int next = pc_nextjobexp(sd);
  6087. nullpo_ret(sd);
  6088. if(!next || sd->status.job_exp < next || pc_is_maxjoblv(sd))
  6089. return 0;
  6090. do {
  6091. sd->status.job_exp -= next;
  6092. //Kyoki pointed out that the max overcarry exp is the exp needed for the previous level -1. [Skotlex]
  6093. 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 )
  6094. sd->status.job_exp = next-1;
  6095. sd->status.job_level ++;
  6096. sd->status.skill_point ++;
  6097. if( pc_is_maxjoblv(sd) ){
  6098. sd->status.job_exp = u32min(sd->status.job_exp,MAX_LEVEL_JOB_EXP);
  6099. break;
  6100. }
  6101. } while ((next=pc_nextjobexp(sd)) > 0 && sd->status.job_exp >= next);
  6102. clif_updatestatus(sd,SP_JOBLEVEL);
  6103. clif_updatestatus(sd,SP_JOBEXP);
  6104. clif_updatestatus(sd,SP_NEXTJOBEXP);
  6105. clif_updatestatus(sd,SP_SKILLPOINT);
  6106. status_calc_pc(sd,SCO_FORCE);
  6107. clif_misceffect(&sd->bl,1);
  6108. if (pc_checkskill(sd, SG_DEVIL) && pc_is_maxbaselv(sd))
  6109. clif_status_change(&sd->bl, EFST_DEVIL1, 1, 0, 0, 0, 1); //Permanent blind effect from SG_DEVIL.
  6110. npc_script_event(sd, NPCE_JOBLVUP);
  6111. achievement_update_objective(sd, AG_GOAL_LEVEL, 1, sd->status.job_level);
  6112. pc_show_questinfo(sd);
  6113. return 1;
  6114. }
  6115. /** Alters experiences calculation based on self bonuses that do not get even shared to the party.
  6116. * @param sd Player
  6117. * @param base_exp Base EXP before peronal bonuses
  6118. * @param job_exp Job EXP before peronal bonuses
  6119. * @param src Block list that affecting the exp calculation
  6120. */
  6121. static void pc_calcexp(struct map_session_data *sd, unsigned int *base_exp, unsigned int *job_exp, struct block_list *src)
  6122. {
  6123. int bonus = 0, vip_bonus_base = 0, vip_bonus_job = 0;
  6124. if (src) {
  6125. struct status_data *status = status_get_status_data(src);
  6126. if( sd->expaddrace[status->race] )
  6127. bonus += sd->expaddrace[status->race];
  6128. if( sd->expaddrace[RC_ALL] )
  6129. bonus += sd->expaddrace[RC_ALL];
  6130. if( sd->expaddclass[status->class_] )
  6131. bonus += sd->expaddclass[status->class_];
  6132. if( sd->expaddclass[CLASS_ALL] )
  6133. bonus += sd->expaddclass[CLASS_ALL];
  6134. if (battle_config.pk_mode &&
  6135. (int)(status_get_lv(src) - sd->status.base_level) >= 20)
  6136. bonus += 15; // pk_mode additional exp if monster >20 levels [Valaris]
  6137. if (src && src->type == BL_MOB && pc_isvip(sd)) { // EXP bonus for VIP player
  6138. vip_bonus_base = battle_config.vip_base_exp_increase;
  6139. vip_bonus_job = battle_config.vip_job_exp_increase;
  6140. }
  6141. }
  6142. // Give EXPBOOST for quests even if src is NULL.
  6143. if (sd->sc.data[SC_EXPBOOST]) {
  6144. bonus += sd->sc.data[SC_EXPBOOST]->val1;
  6145. if (battle_config.vip_bm_increase && pc_isvip(sd)) // Increase Battle Manual EXP rate for VIP
  6146. bonus += (sd->sc.data[SC_EXPBOOST]->val1 / battle_config.vip_bm_increase);
  6147. }
  6148. if (*base_exp) {
  6149. unsigned int exp = (unsigned int)(*base_exp + (double)*base_exp * (bonus + vip_bonus_base)/100.);
  6150. *base_exp = cap_value(exp, 1, UINT_MAX);
  6151. }
  6152. // Give JEXPBOOST for quests even if src is NULL.
  6153. if (sd->sc.data[SC_JEXPBOOST])
  6154. bonus += sd->sc.data[SC_JEXPBOOST]->val1;
  6155. if (*job_exp) {
  6156. unsigned int exp = (unsigned int)(*job_exp + (double)*job_exp * (bonus + vip_bonus_job)/100.);
  6157. *job_exp = cap_value(exp, 1, UINT_MAX);
  6158. }
  6159. return;
  6160. }
  6161. /**
  6162. * Show EXP gained by player in percentage by @showexp
  6163. * @param sd Player
  6164. * @param base_exp Base EXP gained/loss
  6165. * @param next_base_exp Base EXP needed for next base level
  6166. * @param job_exp Job EXP gained/loss
  6167. * @param next_job_exp Job EXP needed for next job level
  6168. * @param lost True:EXP penalty, lose EXP
  6169. **/
  6170. void pc_gainexp_disp(struct map_session_data *sd, unsigned int base_exp, unsigned int next_base_exp, unsigned int job_exp, unsigned int next_job_exp, bool lost) {
  6171. char output[CHAT_SIZE_MAX];
  6172. nullpo_retv(sd);
  6173. sprintf(output, msg_txt(sd,743), // Experience %s Base:%ld (%0.2f%%) Job:%ld (%0.2f%%)
  6174. (lost) ? msg_txt(sd,742) : msg_txt(sd,741),
  6175. (long)base_exp * (lost ? -1 : 1), (base_exp / (float)next_base_exp * 100 * (lost ? -1 : 1)),
  6176. (long)job_exp * (lost ? -1 : 1), (job_exp / (float)next_job_exp * 100 * (lost ? -1 : 1)));
  6177. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  6178. }
  6179. /**
  6180. * Give Base or Job EXP to player, then calculate remaining exp for next lvl
  6181. * @param sd Player
  6182. * @param src EXP source
  6183. * @param base_exp Base EXP gained
  6184. * @param base_exp Job EXP gained
  6185. * @param exp_flag 1: Quest EXP; 2: Param Exp (Ignore Guild EXP tax, EXP adjustments)
  6186. * @return
  6187. **/
  6188. void pc_gainexp(struct map_session_data *sd, struct block_list *src, unsigned int base_exp, unsigned int job_exp, uint8 exp_flag)
  6189. {
  6190. unsigned int nextb = 0, nextj = 0;
  6191. uint8 flag = 0; ///< 1: Base EXP given, 2: Job EXP given, 4: Max Base level, 8: Max Job Level
  6192. nullpo_retv(sd);
  6193. if(sd->bl.prev == NULL || pc_isdead(sd))
  6194. return;
  6195. if (!(exp_flag&2)) {
  6196. if (!battle_config.pvp_exp && map_getmapflag(sd->bl.m, MF_PVP)) // [MouseJstr]
  6197. return; // no exp on pvp maps
  6198. if (sd->status.guild_id>0)
  6199. base_exp -= guild_payexp(sd,base_exp);
  6200. }
  6201. flag = ((base_exp) ? 1 : 0) |
  6202. ((job_exp) ? 2 : 0) |
  6203. ((pc_is_maxbaselv(sd)) ? 4 : 0) |
  6204. ((pc_is_maxjoblv(sd)) ? 8 : 0);
  6205. if (!(exp_flag&2))
  6206. pc_calcexp(sd, &base_exp, &job_exp, src);
  6207. nextb = pc_nextbaseexp(sd);
  6208. nextj = pc_nextjobexp(sd);
  6209. if (flag&4){
  6210. if( sd->status.base_exp >= MAX_LEVEL_BASE_EXP )
  6211. base_exp = 0;
  6212. else if( sd->status.base_exp + base_exp >= MAX_LEVEL_BASE_EXP )
  6213. base_exp = MAX_LEVEL_BASE_EXP - sd->status.base_exp;
  6214. }
  6215. if (flag&8){
  6216. if( sd->status.job_exp >= MAX_LEVEL_JOB_EXP )
  6217. job_exp = 0;
  6218. else if( sd->status.job_exp + job_exp >= MAX_LEVEL_JOB_EXP )
  6219. job_exp = MAX_LEVEL_JOB_EXP - sd->status.job_exp;
  6220. }
  6221. if (!(exp_flag&2) && battle_config.max_exp_gain_rate && (base_exp || job_exp)) {
  6222. //Note that this value should never be greater than the original
  6223. //therefore no overflow checks are needed. [Skotlex]
  6224. if (nextb > 0) {
  6225. float nextbp = (float) base_exp / (float) nextb;
  6226. if (nextbp > battle_config.max_exp_gain_rate/1000.)
  6227. base_exp = (unsigned int)(battle_config.max_exp_gain_rate/1000.*nextb);
  6228. }
  6229. if (nextj > 0) {
  6230. float nextjp = (float) job_exp / (float) nextj;
  6231. if (nextjp > battle_config.max_exp_gain_rate/1000.)
  6232. job_exp = (unsigned int)(battle_config.max_exp_gain_rate/1000.*nextj);
  6233. }
  6234. }
  6235. // Give EXP for Base Level
  6236. if (base_exp) {
  6237. if ((uint64)sd->status.base_exp + base_exp > UINT32_MAX)
  6238. sd->status.base_exp = UINT32_MAX;
  6239. else
  6240. sd->status.base_exp += base_exp;
  6241. if (!pc_checkbaselevelup(sd))
  6242. clif_updatestatus(sd,SP_BASEEXP);
  6243. }
  6244. // Give EXP for Job Level
  6245. if (job_exp) {
  6246. if ((uint64)sd->status.job_exp + job_exp > UINT32_MAX)
  6247. sd->status.job_exp = UINT32_MAX;
  6248. else
  6249. sd->status.job_exp += job_exp;
  6250. if (!pc_checkjoblevelup(sd))
  6251. clif_updatestatus(sd,SP_JOBEXP);
  6252. }
  6253. if (flag&1)
  6254. clif_displayexp(sd, (flag&4) ? 0 : base_exp, SP_BASEEXP, exp_flag&1, false);
  6255. if (flag&2)
  6256. clif_displayexp(sd, (flag&8) ? 0 : job_exp, SP_JOBEXP, exp_flag&1, false);
  6257. if (sd->state.showexp && (base_exp || job_exp))
  6258. pc_gainexp_disp(sd, base_exp, nextb, job_exp, nextj, false);
  6259. }
  6260. /**
  6261. * Lost Base/Job EXP from a player
  6262. * @param sd Player
  6263. * @param base_exp Base EXP lost
  6264. * @param job_exp Job EXP lost
  6265. **/
  6266. void pc_lostexp(struct map_session_data *sd, unsigned int base_exp, unsigned int job_exp) {
  6267. nullpo_retv(sd);
  6268. if (base_exp) {
  6269. base_exp = u32min(sd->status.base_exp, base_exp);
  6270. sd->status.base_exp -= base_exp;
  6271. clif_displayexp(sd, base_exp, SP_BASEEXP, false, true);
  6272. clif_updatestatus(sd, SP_BASEEXP);
  6273. }
  6274. if (job_exp) {
  6275. job_exp = u32min(sd->status.job_exp, job_exp);
  6276. sd->status.job_exp -= job_exp;
  6277. clif_displayexp(sd, job_exp, SP_JOBEXP, false, true);
  6278. clif_updatestatus(sd, SP_JOBEXP);
  6279. }
  6280. if (sd->state.showexp && (base_exp || job_exp))
  6281. pc_gainexp_disp(sd, base_exp, pc_nextbaseexp(sd), job_exp, pc_nextjobexp(sd), true);
  6282. }
  6283. /**
  6284. * Returns max base level for this character's class.
  6285. * @param class_: Player's class
  6286. * @return Max Base Level
  6287. */
  6288. static unsigned int pc_class_maxbaselv(unsigned short class_) {
  6289. return job_info[pc_class2idx(class_)].max_level[0];
  6290. }
  6291. /**
  6292. * Returns max base level for this character.
  6293. * @param sd Player
  6294. * @return Max Base Level
  6295. **/
  6296. unsigned int pc_maxbaselv(struct map_session_data *sd){
  6297. return pc_class_maxbaselv(sd->status.class_);
  6298. }
  6299. /**
  6300. * Returns max job level for this character's class.
  6301. * @param class_: Player's class
  6302. * @return Max Job Level
  6303. */
  6304. static unsigned int pc_class_maxjoblv(unsigned short class_) {
  6305. return job_info[pc_class2idx(class_)].max_level[1];
  6306. }
  6307. /**
  6308. * Returns max job level for this character.
  6309. * @param sd Player
  6310. * @return Max Job Level
  6311. **/
  6312. unsigned int pc_maxjoblv(struct map_session_data *sd){
  6313. return pc_class_maxjoblv(sd->status.class_);
  6314. }
  6315. /**
  6316. * Check if player is reached max base level
  6317. * @param sd
  6318. * @return True if reached max level
  6319. **/
  6320. bool pc_is_maxbaselv(struct map_session_data *sd) {
  6321. nullpo_retr(false, sd);
  6322. return (sd->status.base_level >= pc_maxbaselv(sd));
  6323. }
  6324. /**
  6325. * Check if player is reached max base level
  6326. * @param sd
  6327. * @return True if reached max level
  6328. **/
  6329. bool pc_is_maxjoblv(struct map_session_data *sd) {
  6330. nullpo_retr(false, sd);
  6331. return (sd->status.job_level >= pc_maxjoblv(sd));
  6332. }
  6333. /**
  6334. * Base exp needed for player to level up.
  6335. * @param sd
  6336. * @return Base EXP needed for next base level
  6337. **/
  6338. unsigned int pc_nextbaseexp(struct map_session_data *sd){
  6339. nullpo_ret(sd);
  6340. if (sd->status.base_level == 0) // Is this something that possible?
  6341. return 0;
  6342. if (pc_is_maxbaselv(sd))
  6343. return MAX_LEVEL_BASE_EXP; // On max level, player's base EXP limit is 99,999,999
  6344. return job_info[pc_class2idx(sd->status.class_)].exp_table[0][sd->status.base_level-1];
  6345. }
  6346. /**
  6347. * Job exp needed for player to level up.
  6348. * @param sd
  6349. * @return Job EXP needed for next job level
  6350. **/
  6351. unsigned int pc_nextjobexp(struct map_session_data *sd){
  6352. nullpo_ret(sd);
  6353. if (sd->status.job_level == 0) // Is this something that possible?
  6354. return 0;
  6355. if (pc_is_maxjoblv(sd))
  6356. return MAX_LEVEL_JOB_EXP; // On max level, player's job EXP limit is 999,999,999
  6357. return job_info[pc_class2idx(sd->status.class_)].exp_table[1][sd->status.job_level-1];
  6358. }
  6359. /// Returns the value of the specified stat.
  6360. static int pc_getstat(struct map_session_data* sd, int type)
  6361. {
  6362. nullpo_retr(-1, sd);
  6363. switch( type ) {
  6364. case SP_STR: return sd->status.str;
  6365. case SP_AGI: return sd->status.agi;
  6366. case SP_VIT: return sd->status.vit;
  6367. case SP_INT: return sd->status.int_;
  6368. case SP_DEX: return sd->status.dex;
  6369. case SP_LUK: return sd->status.luk;
  6370. default:
  6371. return -1;
  6372. }
  6373. }
  6374. /// Sets the specified stat to the specified value.
  6375. /// Returns the new value.
  6376. static int pc_setstat(struct map_session_data* sd, int type, int val)
  6377. {
  6378. nullpo_retr(-1, sd);
  6379. switch( type ) {
  6380. case SP_STR: sd->status.str = val; break;
  6381. case SP_AGI: sd->status.agi = val; break;
  6382. case SP_VIT: sd->status.vit = val; break;
  6383. case SP_INT: sd->status.int_ = val; break;
  6384. case SP_DEX: sd->status.dex = val; break;
  6385. case SP_LUK: sd->status.luk = val; break;
  6386. default:
  6387. return -1;
  6388. }
  6389. return val;
  6390. }
  6391. // Calculates the number of status points PC gets when leveling up (from level to level+1)
  6392. int pc_gets_status_point(int level)
  6393. {
  6394. if (battle_config.use_statpoint_table) //Use values from "db/statpoint.txt"
  6395. return (statp[level+1] - statp[level]);
  6396. else //Default increase
  6397. return ((level+15) / 5);
  6398. }
  6399. #ifdef RENEWAL_STAT
  6400. /// Renewal status point cost formula
  6401. #define PC_STATUS_POINT_COST(low) (((low) < 100) ? (2 + ((low) - 1) / 10) : (16 + 4 * (((low) - 100) / 5)))
  6402. #else
  6403. /// Pre-Renewal status point cost formula
  6404. #define PC_STATUS_POINT_COST(low) (( 1 + ((low) + 9) / 10 ))
  6405. #endif
  6406. /// Returns the number of stat points needed to change the specified stat by val.
  6407. /// If val is negative, returns the number of stat points that would be needed to
  6408. /// raise the specified stat from (current value - val) to current value.
  6409. int pc_need_status_point(struct map_session_data* sd, int type, int val)
  6410. {
  6411. int low, high, sp = 0, max = 0;
  6412. if ( val == 0 )
  6413. return 0;
  6414. low = pc_getstat(sd,type);
  6415. max = pc_maxparameter(sd,(enum e_params)(type-SP_STR));
  6416. if ( low >= max && val > 0 )
  6417. return 0; // Official servers show '0' when max is reached
  6418. high = low + val;
  6419. if ( val < 0 )
  6420. SWAP(low, high);
  6421. for ( ; low < high; low++ )
  6422. sp += PC_STATUS_POINT_COST(low);
  6423. return sp;
  6424. }
  6425. /**
  6426. * Returns the value the specified stat can be increased by with the current
  6427. * amount of available status points for the current character's class.
  6428. *
  6429. * @param sd The target character.
  6430. * @param type Stat to verify.
  6431. * @return Maximum value the stat could grow by.
  6432. */
  6433. int pc_maxparameterincrease(struct map_session_data* sd, int type)
  6434. {
  6435. int base, final_val, status_points, max_param;
  6436. nullpo_ret(sd);
  6437. base = final_val = pc_getstat(sd, type);
  6438. status_points = sd->status.status_point;
  6439. max_param = pc_maxparameter(sd, (enum e_params)(type-SP_STR));
  6440. while (final_val <= max_param && status_points >= 0) {
  6441. status_points -= PC_STATUS_POINT_COST(final_val);
  6442. final_val++;
  6443. }
  6444. final_val--;
  6445. return (final_val > base ? final_val-base : 0);
  6446. }
  6447. /**
  6448. * Raises a stat by the specified amount.
  6449. *
  6450. * Obeys max_parameter limits.
  6451. * Subtracts status points according to the cost of the increased stat points.
  6452. *
  6453. * @param sd The target character.
  6454. * @param type The stat to change (see enum _sp)
  6455. * @param increase The stat increase (strictly positive) amount.
  6456. * @retval true if the stat was increased by any amount.
  6457. * @retval false if there were no changes.
  6458. */
  6459. bool pc_statusup(struct map_session_data* sd, int type, int increase)
  6460. {
  6461. int max_increase = 0, current = 0, needed_points = 0, final_value = 0;
  6462. nullpo_ret(sd);
  6463. // check conditions
  6464. if (type < SP_STR || type > SP_LUK || increase <= 0) {
  6465. clif_statusupack(sd, type, 0, 0);
  6466. return false;
  6467. }
  6468. // check limits
  6469. current = pc_getstat(sd, type);
  6470. max_increase = pc_maxparameterincrease(sd, type);
  6471. increase = cap_value(increase, 0, max_increase); // cap to the maximum status points available
  6472. if (increase <= 0 || current + increase > pc_maxparameter(sd, (enum e_params)(type-SP_STR))) {
  6473. clif_statusupack(sd, type, 0, 0);
  6474. return false;
  6475. }
  6476. // check status points
  6477. needed_points = pc_need_status_point(sd, type, increase);
  6478. if (needed_points < 0 || needed_points > sd->status.status_point) { // Sanity check
  6479. clif_statusupack(sd, type, 0, 0);
  6480. return false;
  6481. }
  6482. // set new values
  6483. final_value = pc_setstat(sd, type, current + increase);
  6484. sd->status.status_point -= needed_points;
  6485. status_calc_pc(sd,SCO_NONE);
  6486. // update increase cost indicator
  6487. clif_updatestatus(sd, SP_USTR + type-SP_STR);
  6488. // update statpoint count
  6489. clif_updatestatus(sd, SP_STATUSPOINT);
  6490. // update stat value
  6491. clif_statusupack(sd, type, 1, final_value); // required
  6492. if( final_value > 255 )
  6493. clif_updatestatus(sd, type); // send after the 'ack' to override the truncated value
  6494. achievement_update_objective(sd, AG_GOAL_STATUS, 1, final_value);
  6495. return true;
  6496. }
  6497. /**
  6498. * Raises a stat by the specified amount.
  6499. *
  6500. * Obeys max_parameter limits.
  6501. * Does not subtract status points for the cost of the modified stat points.
  6502. *
  6503. * @param sd The target character.
  6504. * @param type The stat to change (see enum _sp)
  6505. * @param val The stat increase (or decrease) amount.
  6506. * @return the stat increase amount.
  6507. * @retval 0 if no changes were made.
  6508. */
  6509. int pc_statusup2(struct map_session_data* sd, int type, int val)
  6510. {
  6511. int max, need;
  6512. nullpo_ret(sd);
  6513. if( type < SP_STR || type > SP_LUK )
  6514. {
  6515. clif_statusupack(sd,type,0,0);
  6516. return 0;
  6517. }
  6518. need = pc_need_status_point(sd,type,1);
  6519. max = pc_maxparameter(sd,(enum e_params)(type-SP_STR)); // set new value
  6520. val = pc_setstat(sd, type, cap_value(pc_getstat(sd,type) + val, 1, max));
  6521. status_calc_pc(sd,SCO_NONE);
  6522. // update increase cost indicator
  6523. if( need != pc_need_status_point(sd,type,1) )
  6524. clif_updatestatus(sd, SP_USTR + type-SP_STR);
  6525. // update stat value
  6526. clif_statusupack(sd,type,1,val); // required
  6527. if( val > 255 )
  6528. clif_updatestatus(sd,type); // send after the 'ack' to override the truncated value
  6529. return val;
  6530. }
  6531. /*==========================================
  6532. * Update skill_lv for player sd
  6533. * Skill point allocation
  6534. *------------------------------------------*/
  6535. void pc_skillup(struct map_session_data *sd,uint16 skill_id)
  6536. {
  6537. uint16 idx = skill_get_index(skill_id);
  6538. nullpo_retv(sd);
  6539. if (!idx) {
  6540. if (skill_id)
  6541. ShowError("pc_skillup: Player attempts to level up invalid skill '%d'\n", skill_id);
  6542. return;
  6543. }
  6544. // Level up guild skill
  6545. if (SKILL_CHK_GUILD(skill_id)) {
  6546. guild_skillup(sd, skill_id);
  6547. return;
  6548. }
  6549. // Level up homunculus skill
  6550. else if (sd->hd && SKILL_CHK_HOMUN(skill_id)) {
  6551. hom_skillup(sd->hd, skill_id);
  6552. return;
  6553. }
  6554. else {
  6555. if( sd->status.skill_point > 0 &&
  6556. sd->status.skill[idx].id &&
  6557. sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT && //Don't allow raising while you have granted skills. [Skotlex]
  6558. sd->status.skill[idx].lv < skill_tree_get_max(skill_id, sd->status.class_) )
  6559. {
  6560. int lv, range, upgradable;
  6561. sd->status.skill[idx].lv++;
  6562. sd->status.skill_point--;
  6563. if( !skill_get_inf(skill_id) )
  6564. status_calc_pc(sd,SCO_NONE); // Only recalculate for passive skills.
  6565. else if( sd->status.skill_point == 0 && pc_is_taekwon_ranker(sd) )
  6566. pc_calc_skilltree(sd); // Required to grant all TK Ranker skills.
  6567. else
  6568. pc_check_skilltree(sd); // Check if a new skill can Lvlup
  6569. lv = sd->status.skill[idx].lv;
  6570. range = skill_get_range2(&sd->bl, skill_id, lv, false);
  6571. upgradable = (lv < skill_tree_get_max(sd->status.skill[idx].id, sd->status.class_)) ? 1 : 0;
  6572. clif_skillup(sd,skill_id,lv,range,upgradable);
  6573. clif_updatestatus(sd,SP_SKILLPOINT);
  6574. if( skill_id == GN_REMODELING_CART ) /* cart weight info was updated by status_calc_pc */
  6575. clif_updatestatus(sd,SP_CARTINFO);
  6576. if (!pc_has_permission(sd, PC_PERM_ALL_SKILL)) // may skill everything at any time anyways, and this would cause a huge slowdown
  6577. clif_skillinfoblock(sd);
  6578. }
  6579. //else
  6580. // 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);
  6581. }
  6582. }
  6583. /*==========================================
  6584. * /allskill
  6585. *------------------------------------------*/
  6586. int pc_allskillup(struct map_session_data *sd)
  6587. {
  6588. int i;
  6589. nullpo_ret(sd);
  6590. for (i = 0; i < MAX_SKILL; i++) {
  6591. 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) {
  6592. sd->status.skill[i].lv = (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY) ? 0 : sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
  6593. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  6594. if (sd->status.skill[i].lv == 0)
  6595. sd->status.skill[i].id = 0;
  6596. }
  6597. }
  6598. if (!pc_grant_allskills(sd, true)) {
  6599. uint16 sk_id;
  6600. for (i = 0; i < MAX_SKILL_TREE && (sk_id = skill_tree[pc_class2idx(sd->status.class_)][i].skill_id) > 0;i++){
  6601. uint16 sk_idx = skill_get_index(sk_id);
  6602. if (sk_id == 0 || sk_idx == 0)
  6603. continue;
  6604. std::shared_ptr<s_skill_db> skill = skill_db.find(sk_id);
  6605. if (
  6606. (skill->inf2[INF2_ISQUEST] && !battle_config.quest_skill_learn) ||
  6607. ((skill->inf2[INF2_ISWEDDING] || skill->inf2[INF2_ISSPIRIT])) ||
  6608. sk_id == SG_DEVIL
  6609. )
  6610. continue; //Cannot be learned normally.
  6611. sd->status.skill[sk_idx].id = sk_id;
  6612. sd->status.skill[sk_idx].lv = skill_tree_get_max(sk_id, sd->status.class_); // celest
  6613. }
  6614. }
  6615. status_calc_pc(sd,SCO_NONE);
  6616. //Required because if you could level up all skills previously,
  6617. //the update will not be sent as only the lv variable changes.
  6618. clif_skillinfoblock(sd);
  6619. return 0;
  6620. }
  6621. /*==========================================
  6622. * /resetlvl
  6623. *------------------------------------------*/
  6624. int pc_resetlvl(struct map_session_data* sd,int type)
  6625. {
  6626. int i;
  6627. nullpo_ret(sd);
  6628. if (type != 3) //Also reset skills
  6629. pc_resetskill(sd, 0);
  6630. if(type == 1){
  6631. sd->status.skill_point=0;
  6632. sd->status.base_level=1;
  6633. sd->status.job_level=1;
  6634. sd->status.base_exp=0;
  6635. sd->status.job_exp=0;
  6636. if(sd->sc.option !=0)
  6637. sd->sc.option = 0;
  6638. sd->status.str=1;
  6639. sd->status.agi=1;
  6640. sd->status.vit=1;
  6641. sd->status.int_=1;
  6642. sd->status.dex=1;
  6643. sd->status.luk=1;
  6644. if(sd->status.class_ == JOB_NOVICE_HIGH) {
  6645. sd->status.status_point=100; // not 88 [celest]
  6646. // give platinum skills upon changing
  6647. pc_skill(sd,NV_FIRSTAID,1,ADDSKILL_PERMANENT);
  6648. pc_skill(sd,NV_TRICKDEAD,1,ADDSKILL_PERMANENT);
  6649. }
  6650. }
  6651. if(type == 2){
  6652. sd->status.skill_point=0;
  6653. sd->status.base_level=1;
  6654. sd->status.job_level=1;
  6655. sd->status.base_exp=0;
  6656. sd->status.job_exp=0;
  6657. }
  6658. if(type == 3){
  6659. sd->status.base_level=1;
  6660. sd->status.base_exp=0;
  6661. }
  6662. if(type == 4){
  6663. sd->status.job_level=1;
  6664. sd->status.job_exp=0;
  6665. }
  6666. clif_updatestatus(sd,SP_STATUSPOINT);
  6667. clif_updatestatus(sd,SP_STR);
  6668. clif_updatestatus(sd,SP_AGI);
  6669. clif_updatestatus(sd,SP_VIT);
  6670. clif_updatestatus(sd,SP_INT);
  6671. clif_updatestatus(sd,SP_DEX);
  6672. clif_updatestatus(sd,SP_LUK);
  6673. clif_updatestatus(sd,SP_BASELEVEL);
  6674. clif_updatestatus(sd,SP_JOBLEVEL);
  6675. clif_updatestatus(sd,SP_STATUSPOINT);
  6676. clif_updatestatus(sd,SP_BASEEXP);
  6677. clif_updatestatus(sd,SP_JOBEXP);
  6678. clif_updatestatus(sd,SP_NEXTBASEEXP);
  6679. clif_updatestatus(sd,SP_NEXTJOBEXP);
  6680. clif_updatestatus(sd,SP_SKILLPOINT);
  6681. clif_updatestatus(sd,SP_USTR); // Updates needed stat points - Valaris
  6682. clif_updatestatus(sd,SP_UAGI);
  6683. clif_updatestatus(sd,SP_UVIT);
  6684. clif_updatestatus(sd,SP_UINT);
  6685. clif_updatestatus(sd,SP_UDEX);
  6686. clif_updatestatus(sd,SP_ULUK); // End Addition
  6687. for(i=0;i<EQI_MAX;i++) { // unequip items that can't be equipped by base 1 [Valaris]
  6688. if(sd->equip_index[i] >= 0)
  6689. if(pc_isequip(sd,sd->equip_index[i]))
  6690. pc_unequipitem(sd,sd->equip_index[i],2);
  6691. }
  6692. if ((type == 1 || type == 2 || type == 3) && sd->status.party_id)
  6693. party_send_levelup(sd);
  6694. status_calc_pc(sd, SCO_FORCE);
  6695. clif_skillinfoblock(sd);
  6696. return 0;
  6697. }
  6698. /*==========================================
  6699. * /resetstate
  6700. *------------------------------------------*/
  6701. int pc_resetstate(struct map_session_data* sd)
  6702. {
  6703. nullpo_ret(sd);
  6704. if (battle_config.use_statpoint_table)
  6705. { // New statpoint table used here - Dexity
  6706. if (sd->status.base_level > MAX_LEVEL)
  6707. { //statp[] goes out of bounds, can't reset!
  6708. ShowError("pc_resetstate: Can't reset stats of %d:%d, the base level (%d) is greater than the max level supported (%d)\n",
  6709. sd->status.account_id, sd->status.char_id, sd->status.base_level, MAX_LEVEL);
  6710. return 0;
  6711. }
  6712. sd->status.status_point = statp[sd->status.base_level] + ( sd->class_&JOBL_UPPER ? 52 : 0 ); // extra 52+48=100 stat points
  6713. }
  6714. else
  6715. {
  6716. int add=0;
  6717. add += pc_need_status_point(sd, SP_STR, 1-pc_getstat(sd, SP_STR));
  6718. add += pc_need_status_point(sd, SP_AGI, 1-pc_getstat(sd, SP_AGI));
  6719. add += pc_need_status_point(sd, SP_VIT, 1-pc_getstat(sd, SP_VIT));
  6720. add += pc_need_status_point(sd, SP_INT, 1-pc_getstat(sd, SP_INT));
  6721. add += pc_need_status_point(sd, SP_DEX, 1-pc_getstat(sd, SP_DEX));
  6722. add += pc_need_status_point(sd, SP_LUK, 1-pc_getstat(sd, SP_LUK));
  6723. sd->status.status_point+=add;
  6724. }
  6725. pc_setstat(sd, SP_STR, 1);
  6726. pc_setstat(sd, SP_AGI, 1);
  6727. pc_setstat(sd, SP_VIT, 1);
  6728. pc_setstat(sd, SP_INT, 1);
  6729. pc_setstat(sd, SP_DEX, 1);
  6730. pc_setstat(sd, SP_LUK, 1);
  6731. clif_updatestatus(sd,SP_STR);
  6732. clif_updatestatus(sd,SP_AGI);
  6733. clif_updatestatus(sd,SP_VIT);
  6734. clif_updatestatus(sd,SP_INT);
  6735. clif_updatestatus(sd,SP_DEX);
  6736. clif_updatestatus(sd,SP_LUK);
  6737. clif_updatestatus(sd,SP_USTR); // Updates needed stat points - Valaris
  6738. clif_updatestatus(sd,SP_UAGI);
  6739. clif_updatestatus(sd,SP_UVIT);
  6740. clif_updatestatus(sd,SP_UINT);
  6741. clif_updatestatus(sd,SP_UDEX);
  6742. clif_updatestatus(sd,SP_ULUK); // End Addition
  6743. clif_updatestatus(sd,SP_STATUSPOINT);
  6744. if( sd->mission_mobid ) { //bugreport:2200
  6745. sd->mission_mobid = 0;
  6746. sd->mission_count = 0;
  6747. pc_setglobalreg(sd, add_str(TKMISSIONID_VAR), 0);
  6748. }
  6749. status_calc_pc(sd, SCO_NONE);
  6750. return 1;
  6751. }
  6752. /*==========================================
  6753. * /resetskill
  6754. * if flag&1, perform block resync and status_calc call.
  6755. * if flag&2, just count total amount of skill points used by player, do not really reset.
  6756. * if flag&4, just reset the skills if the player class is a bard/dancer type (for changesex.)
  6757. *------------------------------------------*/
  6758. int pc_resetskill(struct map_session_data* sd, int flag)
  6759. {
  6760. int i, skill_point=0;
  6761. nullpo_ret(sd);
  6762. if( flag&4 && (sd->class_&MAPID_UPPERMASK) != MAPID_BARDDANCER )
  6763. return 0;
  6764. if( !(flag&2) ) { //Remove stuff lost when resetting skills.
  6765. /**
  6766. * 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)
  6767. **/
  6768. if( pc_is_taekwon_ranker(sd) )
  6769. return 0;
  6770. if( pc_checkskill(sd, SG_DEVIL) && pc_is_maxjoblv(sd) )
  6771. clif_status_load(&sd->bl, EFST_DEVIL1, 0); //Remove perma blindness due to skill-reset. [Skotlex]
  6772. i = sd->sc.option;
  6773. if( i&OPTION_RIDING && pc_checkskill(sd, KN_RIDING) )
  6774. i &= ~OPTION_RIDING;
  6775. if( i&OPTION_FALCON && pc_checkskill(sd, HT_FALCON) )
  6776. i &= ~OPTION_FALCON;
  6777. if( i&OPTION_DRAGON && pc_checkskill(sd, RK_DRAGONTRAINING) )
  6778. i &= ~OPTION_DRAGON;
  6779. if( i&OPTION_WUG && pc_checkskill(sd, RA_WUGMASTERY) )
  6780. i &= ~OPTION_WUG;
  6781. if( i&OPTION_WUGRIDER && pc_checkskill(sd, RA_WUGRIDER) )
  6782. i &= ~OPTION_WUGRIDER;
  6783. if( i&OPTION_MADOGEAR && ( sd->class_&MAPID_THIRDMASK ) == MAPID_MECHANIC )
  6784. i &= ~OPTION_MADOGEAR;
  6785. #ifndef NEW_CARTS
  6786. if( i&OPTION_CART && pc_checkskill(sd, MC_PUSHCART) )
  6787. i &= ~OPTION_CART;
  6788. #else
  6789. if( sd->sc.data[SC_PUSH_CART] )
  6790. pc_setcart(sd, 0);
  6791. #endif
  6792. if( i != sd->sc.option )
  6793. pc_setoption(sd, i);
  6794. if( hom_is_active(sd->hd) && pc_checkskill(sd, AM_CALLHOMUN) )
  6795. hom_vaporize(sd, HOM_ST_ACTIVE);
  6796. if (sd->sc.data[SC_SPRITEMABLE] && pc_checkskill(sd, SU_SPRITEMABLE))
  6797. status_change_end(&sd->bl, SC_SPRITEMABLE, INVALID_TIMER);
  6798. }
  6799. for (const auto &skill : skill_db) {
  6800. uint16 skill_id = skill.second->nameid, idx = skill_get_index(skill_id);
  6801. uint8 lv = sd->status.skill[idx].lv;
  6802. if (lv == 0 || skill_id == 0)
  6803. continue;
  6804. if( skill.second->inf2[INF2_ISWEDDING] || skill.second->inf2[INF2_ISSPIRIT] ) //Avoid reseting wedding/linker skills.
  6805. continue;
  6806. // Don't reset trick dead if not a novice/baby
  6807. if( skill_id == NV_TRICKDEAD && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE )
  6808. {
  6809. sd->status.skill[idx].lv = 0;
  6810. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  6811. continue;
  6812. }
  6813. // do not reset basic skill
  6814. if (skill_id == NV_BASIC && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE )
  6815. continue;
  6816. if( sd->status.skill[idx].flag == SKILL_FLAG_PERM_GRANTED )
  6817. continue;
  6818. if( flag&4 && !skill_ischangesex(skill_id) )
  6819. continue;
  6820. if( skill.second->inf2[INF2_ISQUEST] && !battle_config.quest_skill_learn )
  6821. { //Only handle quest skills in a special way when you can't learn them manually
  6822. if( battle_config.quest_skill_reset && !(flag&2) )
  6823. { //Wipe them
  6824. sd->status.skill[idx].lv = 0;
  6825. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  6826. }
  6827. continue;
  6828. }
  6829. if( sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT )
  6830. skill_point += lv;
  6831. else
  6832. if( sd->status.skill[idx].flag >= SKILL_FLAG_REPLACED_LV_0 )
  6833. skill_point += (sd->status.skill[idx].flag - SKILL_FLAG_REPLACED_LV_0);
  6834. if( !(flag&2) )
  6835. {// reset
  6836. sd->status.skill[idx].lv = 0;
  6837. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  6838. }
  6839. }
  6840. if( flag&2 || !skill_point ) return skill_point;
  6841. sd->status.skill_point += skill_point;
  6842. if (flag&1) {
  6843. clif_updatestatus(sd,SP_SKILLPOINT);
  6844. clif_skillinfoblock(sd);
  6845. status_calc_pc(sd, SCO_FORCE);
  6846. }
  6847. return skill_point;
  6848. }
  6849. /*==========================================
  6850. * /resetfeel [Komurka]
  6851. *------------------------------------------*/
  6852. int pc_resetfeel(struct map_session_data* sd)
  6853. {
  6854. int i;
  6855. nullpo_ret(sd);
  6856. for (i=0; i<MAX_PC_FEELHATE; i++)
  6857. {
  6858. sd->feel_map[i].m = -1;
  6859. sd->feel_map[i].index = 0;
  6860. pc_setglobalreg(sd, add_str(sg_info[i].feel_var), 0);
  6861. }
  6862. return 0;
  6863. }
  6864. int pc_resethate(struct map_session_data* sd)
  6865. {
  6866. int i;
  6867. nullpo_ret(sd);
  6868. for (i=0; i<3; i++)
  6869. {
  6870. sd->hate_mob[i] = -1;
  6871. pc_setglobalreg(sd, add_str(sg_info[i].hate_var), 0);
  6872. }
  6873. return 0;
  6874. }
  6875. int pc_skillatk_bonus(struct map_session_data *sd, uint16 skill_id)
  6876. {
  6877. int bonus = 0;
  6878. nullpo_ret(sd);
  6879. skill_id = skill_dummy2skill_id(skill_id);
  6880. for (auto &it : sd->skillatk) {
  6881. if (it.id == skill_id) {
  6882. bonus += it.val;
  6883. break;
  6884. }
  6885. }
  6886. return bonus;
  6887. }
  6888. int pc_sub_skillatk_bonus(struct map_session_data *sd, uint16 skill_id)
  6889. {
  6890. int bonus = 0;
  6891. nullpo_ret(sd);
  6892. skill_id = skill_dummy2skill_id(skill_id);
  6893. for (auto &it : sd->subskill) {
  6894. if (it.id == skill_id) {
  6895. bonus += it.val;
  6896. break;
  6897. }
  6898. }
  6899. return bonus;
  6900. }
  6901. int pc_skillheal_bonus(struct map_session_data *sd, uint16 skill_id) {
  6902. int bonus = sd->bonus.add_heal_rate;
  6903. nullpo_ret(sd);
  6904. skill_id = skill_dummy2skill_id(skill_id);
  6905. if( bonus ) {
  6906. switch( skill_id ) {
  6907. case AL_HEAL: if( !(battle_config.skill_add_heal_rate&1) ) bonus = 0; break;
  6908. case PR_SANCTUARY: if( !(battle_config.skill_add_heal_rate&2) ) bonus = 0; break;
  6909. case AM_POTIONPITCHER: if( !(battle_config.skill_add_heal_rate&4) ) bonus = 0; break;
  6910. case CR_SLIMPITCHER: if( !(battle_config.skill_add_heal_rate&8) ) bonus = 0; break;
  6911. case BA_APPLEIDUN: if( !(battle_config.skill_add_heal_rate&16)) bonus = 0; break;
  6912. }
  6913. }
  6914. for (auto &it : sd->skillheal) {
  6915. if (it.id == skill_id) {
  6916. bonus += it.val;
  6917. break;
  6918. }
  6919. }
  6920. return bonus;
  6921. }
  6922. int pc_skillheal2_bonus(struct map_session_data *sd, uint16 skill_id) {
  6923. int bonus = sd->bonus.add_heal2_rate;
  6924. skill_id = skill_dummy2skill_id(skill_id);
  6925. for (auto &it : sd->skillheal2) {
  6926. if (it.id == skill_id) {
  6927. bonus += it.val;
  6928. break;
  6929. }
  6930. }
  6931. return bonus;
  6932. }
  6933. void pc_respawn(struct map_session_data* sd, clr_type clrtype)
  6934. {
  6935. if( !pc_isdead(sd) )
  6936. return; // not applicable
  6937. if( sd->bg_id && bg_member_respawn(sd) )
  6938. return; // member revived by battleground
  6939. pc_setstand(sd, true);
  6940. pc_setrestartvalue(sd,3);
  6941. if( pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, clrtype) != SETPOS_OK )
  6942. clif_resurrection(&sd->bl, 1); //If warping fails, send a normal stand up packet.
  6943. }
  6944. static TIMER_FUNC(pc_respawn_timer){
  6945. struct map_session_data *sd = map_id2sd(id);
  6946. if( sd != NULL )
  6947. {
  6948. sd->pvp_point=0;
  6949. sd->respawn_tid = INVALID_TIMER;
  6950. pc_respawn(sd,CLR_OUTSIGHT);
  6951. }
  6952. return 0;
  6953. }
  6954. /*==========================================
  6955. * Invoked when a player has received damage
  6956. *------------------------------------------*/
  6957. void pc_damage(struct map_session_data *sd,struct block_list *src,unsigned int hp, unsigned int sp)
  6958. {
  6959. if (sp) clif_updatestatus(sd,SP_SP);
  6960. if (hp) clif_updatestatus(sd,SP_HP);
  6961. else return;
  6962. if (!src)
  6963. return;
  6964. if( pc_issit(sd) ) {
  6965. pc_setstand(sd, true);
  6966. skill_sit(sd,0);
  6967. }
  6968. if (sd->progressbar.npc_id)
  6969. clif_progressbar_abort(sd);
  6970. if( sd->status.pet_id > 0 && sd->pd && battle_config.pet_damage_support )
  6971. pet_target_check(sd->pd,src,1);
  6972. if( sd->status.ele_id > 0 )
  6973. elemental_set_target(sd,src);
  6974. if(battle_config.prevent_logout_trigger&PLT_DAMAGE)
  6975. sd->canlog_tick = gettick();
  6976. }
  6977. TIMER_FUNC(pc_close_npc_timer){
  6978. TBL_PC *sd = map_id2sd(id);
  6979. if(sd) pc_close_npc(sd,data);
  6980. return 0;
  6981. }
  6982. /**
  6983. * Method to properly close a NPC for player and clear anything related.
  6984. * @param sd: Player attached
  6985. * @param flag: Method of closure
  6986. * 1: Produce a close button and end the NPC
  6987. * 2: End the NPC (best for no dialog windows)
  6988. */
  6989. void pc_close_npc(struct map_session_data *sd,int flag)
  6990. {
  6991. nullpo_retv(sd);
  6992. if (sd->npc_id || sd->npc_shopid) {
  6993. if (sd->state.using_fake_npc) {
  6994. clif_clearunit_single(sd->npc_id, CLR_OUTSIGHT, sd->fd);
  6995. sd->state.using_fake_npc = 0;
  6996. }
  6997. if (sd->st) {
  6998. if(sd->st->state == RUN){ //wait ending code execution
  6999. add_timer(gettick()+500,pc_close_npc_timer,sd->bl.id,flag);
  7000. return;
  7001. }
  7002. sd->st->state = ((flag==1 && sd->st->mes_active)?CLOSE:END);
  7003. sd->st->mes_active = 0;
  7004. }
  7005. sd->state.menu_or_input = 0;
  7006. sd->npc_menu = 0;
  7007. sd->npc_shopid = 0;
  7008. #ifdef SECURE_NPCTIMEOUT
  7009. sd->npc_idle_timer = INVALID_TIMER;
  7010. #endif
  7011. if (sd->st) {
  7012. if (sd->st->state == CLOSE) {
  7013. clif_scriptclose(sd, sd->npc_id);
  7014. clif_scriptclear(sd, sd->npc_id); // [Ind/Hercules]
  7015. sd->st->state = END; // Force to end now
  7016. }
  7017. if (sd->st->state == END) { // free attached scripts that are waiting
  7018. script_free_state(sd->st);
  7019. sd->st = NULL;
  7020. sd->npc_id = 0;
  7021. }
  7022. }
  7023. }
  7024. }
  7025. /*==========================================
  7026. * Invoked when a player has negative current hp
  7027. *------------------------------------------*/
  7028. int pc_dead(struct map_session_data *sd,struct block_list *src)
  7029. {
  7030. int i=0,k=0;
  7031. t_tick tick = gettick();
  7032. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  7033. // Activate Steel body if a super novice dies at 99+% exp [celest]
  7034. // Super Novices have no kill or die functions attached when saved by their angel
  7035. if (!sd->state.snovice_dead_flag && (sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE) {
  7036. unsigned int exp = pc_nextbaseexp(sd);
  7037. if( exp && get_percentage(sd->status.base_exp,exp) >= 99 ) {
  7038. sd->state.snovice_dead_flag = 1;
  7039. pc_setrestartvalue(sd,1);
  7040. status_percent_heal(&sd->bl, 100, 100);
  7041. clif_resurrection(&sd->bl, 1);
  7042. if(battle_config.pc_invincible_time)
  7043. pc_setinvincibletimer(sd, battle_config.pc_invincible_time);
  7044. sc_start(&sd->bl,&sd->bl,status_skill2sc(MO_STEELBODY),100,5,skill_get_time(MO_STEELBODY,5));
  7045. if(mapdata_flag_gvg2(mapdata))
  7046. pc_respawn_timer(INVALID_TIMER, gettick(), sd->bl.id, 0);
  7047. return 0;
  7048. }
  7049. }
  7050. for(k = 0; k < MAX_DEVOTION; k++) {
  7051. if (sd->devotion[k]){
  7052. struct map_session_data *devsd = map_id2sd(sd->devotion[k]);
  7053. if (devsd)
  7054. status_change_end(&devsd->bl, SC_DEVOTION, INVALID_TIMER);
  7055. sd->devotion[k] = 0;
  7056. }
  7057. }
  7058. if(sd->shadowform_id) { //if we were target of shadowform
  7059. status_change_end(map_id2bl(sd->shadowform_id), SC__SHADOWFORM, INVALID_TIMER);
  7060. sd->shadowform_id = 0; //should be remove on status end anyway
  7061. }
  7062. if(sd->status.pet_id > 0 && sd->pd) {
  7063. struct pet_data *pd = sd->pd;
  7064. if( !mapdata->flag[MF_NOEXPPENALTY] ) {
  7065. pet_set_intimate(pd, pd->pet.intimate + pd->get_pet_db()->die);
  7066. if( pd->pet.intimate <= PET_INTIMATE_NONE )
  7067. pet_set_intimate(pd, PET_INTIMATE_NONE);
  7068. clif_send_petdata(sd,sd->pd,1,pd->pet.intimate);
  7069. }
  7070. if( sd->pd->target_id ) // Unlock all targets...
  7071. pet_unlocktarget(sd->pd);
  7072. }
  7073. 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)
  7074. hom_vaporize(sd, HOM_ST_ACTIVE);
  7075. if( sd->md )
  7076. mercenary_delete(sd->md, 3); // Your mercenary soldier has ran away.
  7077. if( sd->ed )
  7078. elemental_delete(sd->ed);
  7079. // Leave duel if you die [LuzZza]
  7080. if(battle_config.duel_autoleave_when_die) {
  7081. if(sd->duel_group > 0)
  7082. duel_leave(sd->duel_group, sd);
  7083. if(sd->duel_invite > 0)
  7084. duel_reject(sd->duel_invite, sd);
  7085. }
  7086. pc_close_npc(sd,2); //close npc if we were using one
  7087. /* e.g. not killed thru pc_damage */
  7088. if( pc_issit(sd) ) {
  7089. clif_status_load(&sd->bl,EFST_SIT,0);
  7090. }
  7091. pc_setdead(sd);
  7092. clif_party_dead( sd );
  7093. pc_setglobalreg(sd, add_str(PCDIECOUNTER_VAR), sd->die_counter+1);
  7094. pc_setparam(sd, SP_KILLERRID, src?src->id:0);
  7095. //Reset menu skills/item skills
  7096. if ((sd->skillitem) != 0)
  7097. sd->skillitem = sd->skillitemlv = 0;
  7098. if ((sd->menuskill_id) != 0)
  7099. sd->menuskill_id = sd->menuskill_val = 0;
  7100. //Reset ticks.
  7101. sd->hp_loss.tick = sd->sp_loss.tick = sd->hp_regen.tick = sd->sp_regen.tick = 0;
  7102. if ( sd->spiritball !=0 )
  7103. pc_delspiritball(sd,sd->spiritball,0);
  7104. if (sd->spiritcharm_type != CHARM_TYPE_NONE && sd->spiritcharm > 0)
  7105. pc_delspiritcharm(sd,sd->spiritcharm,sd->spiritcharm_type);
  7106. if (src)
  7107. switch (src->type) {
  7108. case BL_MOB:
  7109. {
  7110. struct mob_data *md=(struct mob_data *)src;
  7111. if(md->target_id==sd->bl.id)
  7112. mob_unlocktarget(md,tick);
  7113. if(battle_config.mobs_level_up && md->status.hp &&
  7114. (unsigned int)md->level < pc_maxbaselv(sd) &&
  7115. !md->guardian_data && !md->special_state.ai// Guardians/summons should not level. [Skotlex]
  7116. ) { // monster level up [Valaris]
  7117. clif_misceffect(&md->bl,0);
  7118. md->level++;
  7119. status_calc_mob(md, SCO_NONE);
  7120. status_percent_heal(src,10,0);
  7121. if( battle_config.show_mob_info&4 )
  7122. {// update name with new level
  7123. clif_name_area(&md->bl);
  7124. }
  7125. }
  7126. src = battle_get_master(src); // Maybe Player Summon
  7127. }
  7128. break;
  7129. case BL_PET: //Pass on to master...
  7130. case BL_HOM:
  7131. case BL_MER:
  7132. src = battle_get_master(src);
  7133. break;
  7134. }
  7135. if (src && src->type == BL_PC) {
  7136. struct map_session_data *ssd = (struct map_session_data *)src;
  7137. pc_setparam(ssd, SP_KILLEDRID, sd->bl.id);
  7138. npc_script_event(ssd, NPCE_KILLPC);
  7139. if (battle_config.pk_mode&2) {
  7140. ssd->status.manner -= 5;
  7141. if(ssd->status.manner < 0)
  7142. sc_start(&sd->bl,src,SC_NOCHAT,100,0,0);
  7143. #if 0
  7144. // PK/Karma system code (not enabled yet) [celest]
  7145. // originally from Kade Online, so i don't know if any of these is correct ^^;
  7146. // note: karma is measured REVERSE, so more karma = more 'evil' / less honourable,
  7147. // karma going down = more 'good' / more honourable.
  7148. // The Karma System way...
  7149. if (sd->status.karma > ssd->status.karma) { // If player killed was more evil
  7150. sd->status.karma--;
  7151. ssd->status.karma--;
  7152. }
  7153. else if (sd->status.karma < ssd->status.karma) // If player killed was more good
  7154. ssd->status.karma++;
  7155. // or the PK System way...
  7156. if (sd->status.karma > 0) // player killed is dishonourable?
  7157. ssd->status.karma--; // honour points earned
  7158. sd->status.karma++; // honour points lost
  7159. // To-do: Receive exp on certain occasions
  7160. #endif
  7161. }
  7162. }
  7163. if(battle_config.bone_drop==2
  7164. || (battle_config.bone_drop==1 && mapdata->flag[MF_PVP]))
  7165. {
  7166. struct item item_tmp;
  7167. memset(&item_tmp,0,sizeof(item_tmp));
  7168. item_tmp.nameid=ITEMID_SKULL_;
  7169. item_tmp.identify=1;
  7170. item_tmp.card[0]=CARD0_CREATE;
  7171. item_tmp.card[1]=0;
  7172. item_tmp.card[2]=GetWord(sd->status.char_id,0); // CharId
  7173. item_tmp.card[3]=GetWord(sd->status.char_id,1);
  7174. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  7175. }
  7176. //Remove bonus_script when dead
  7177. pc_bonus_script_clear(sd,BSF_REM_ON_DEAD);
  7178. // changed penalty options, added death by player if pk_mode [Valaris]
  7179. if(battle_config.death_penalty_type
  7180. && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE // only novices will receive no penalty
  7181. && !sd->sc.data[SC_BABY] && !sd->sc.data[SC_LIFEINSURANCE]
  7182. && !mapdata->flag[MF_NOEXPPENALTY] && !mapdata_flag_gvg2(mapdata))
  7183. {
  7184. uint32 base_penalty = 0;
  7185. uint32 job_penalty = 0;
  7186. uint32 zeny_penalty = 0;
  7187. if (pc_isvip(sd)) { // EXP penalty for VIP
  7188. base_penalty = battle_config.vip_exp_penalty_base;
  7189. job_penalty = battle_config.vip_exp_penalty_job;
  7190. zeny_penalty = battle_config.vip_zeny_penalty;
  7191. } else {
  7192. base_penalty = battle_config.death_penalty_base;
  7193. job_penalty = battle_config.death_penalty_job;
  7194. zeny_penalty = battle_config.zeny_penalty;
  7195. }
  7196. if ((battle_config.death_penalty_maxlv&1 || !pc_is_maxbaselv(sd)) && base_penalty > 0) {
  7197. switch (battle_config.death_penalty_type) {
  7198. case 1: base_penalty = (uint32) ( pc_nextbaseexp(sd) * ( base_penalty / 10000. ) ); break;
  7199. case 2: base_penalty = (uint32) ( sd->status.base_exp * ( base_penalty / 10000. ) ); break;
  7200. }
  7201. if (base_penalty){ //recheck after altering to speedup
  7202. if (battle_config.pk_mode && src && src->type==BL_PC)
  7203. base_penalty *= 2;
  7204. base_penalty = u32min(sd->status.base_exp, base_penalty);
  7205. }
  7206. }
  7207. else
  7208. base_penalty = 0;
  7209. if ((battle_config.death_penalty_maxlv&2 || !pc_is_maxjoblv(sd)) && job_penalty > 0) {
  7210. switch (battle_config.death_penalty_type) {
  7211. case 1: job_penalty = (uint32) ( pc_nextjobexp(sd) * ( job_penalty / 10000. ) ); break;
  7212. case 2: job_penalty = (uint32) ( sd->status.job_exp * ( job_penalty /10000. ) ); break;
  7213. }
  7214. if (job_penalty) {
  7215. if (battle_config.pk_mode && src && src->type==BL_PC)
  7216. job_penalty *= 2;
  7217. job_penalty = u32min(sd->status.job_exp, job_penalty);
  7218. }
  7219. }
  7220. else
  7221. job_penalty = 0;
  7222. if (base_penalty || job_penalty)
  7223. pc_lostexp(sd, base_penalty, job_penalty);
  7224. if( zeny_penalty > 0 && !mapdata->flag[MF_NOZENYPENALTY]) {
  7225. zeny_penalty = (uint32)( sd->status.zeny * ( zeny_penalty / 10000. ) );
  7226. if(zeny_penalty)
  7227. pc_payzeny(sd, zeny_penalty, LOG_TYPE_PICKDROP_PLAYER, NULL);
  7228. }
  7229. }
  7230. if( mapdata->flag[MF_PVP_NIGHTMAREDROP] ) { // Moved this outside so it works when PVP isn't enabled and during pk mode [Ancyker]
  7231. for (const auto &it : mapdata->drop_list) {
  7232. int id = it.drop_id, per = it.drop_per;
  7233. enum e_nightmare_drop_type type = it.drop_type;
  7234. if(id == 0)
  7235. continue;
  7236. if(id == -1){
  7237. int eq_num=0,eq_n[MAX_INVENTORY];
  7238. memset(eq_n,0,sizeof(eq_n));
  7239. for(i=0;i<MAX_INVENTORY;i++) {
  7240. if( (type&NMDT_INVENTORY && !sd->inventory.u.items_inventory[i].equip)
  7241. || (type&NMDT_EQUIP && sd->inventory.u.items_inventory[i].equip)
  7242. || type&NMDT_ALL)
  7243. {
  7244. int l;
  7245. ARR_FIND( 0, MAX_INVENTORY, l, eq_n[l] <= 0 );
  7246. if( l < MAX_INVENTORY )
  7247. eq_n[l] = i;
  7248. eq_num++;
  7249. }
  7250. }
  7251. if(eq_num > 0){
  7252. int n = eq_n[rnd()%eq_num];
  7253. if(rnd()%10000 < per) {
  7254. if(sd->inventory.u.items_inventory[n].equip)
  7255. pc_unequipitem(sd,n,3);
  7256. pc_dropitem(sd,n,1);
  7257. }
  7258. }
  7259. }
  7260. else if(id > 0) {
  7261. for(i=0;i<MAX_INVENTORY;i++){
  7262. if(sd->inventory.u.items_inventory[i].nameid == id
  7263. && rnd()%10000 < per
  7264. && ((type&NMDT_INVENTORY && !sd->inventory.u.items_inventory[i].equip)
  7265. || (type&NMDT_EQUIP && sd->inventory.u.items_inventory[i].equip)
  7266. || type&NMDT_ALL) ){
  7267. if(sd->inventory.u.items_inventory[i].equip)
  7268. pc_unequipitem(sd,i,3);
  7269. pc_dropitem(sd,i,1);
  7270. break;
  7271. }
  7272. }
  7273. }
  7274. }
  7275. }
  7276. // pvp
  7277. // disable certain pvp functions on pk_mode [Valaris]
  7278. if( !battle_config.pk_mode && mapdata->flag[MF_PVP] && !mapdata->flag[MF_PVP_NOCALCRANK] ) {
  7279. sd->pvp_point -= 5;
  7280. sd->pvp_lost++;
  7281. if( src && src->type == BL_PC ) {
  7282. struct map_session_data *ssd = (struct map_session_data *)src;
  7283. ssd->pvp_point++;
  7284. ssd->pvp_won++;
  7285. }
  7286. if( sd->pvp_point < 0 ) {
  7287. sd->respawn_tid = add_timer(tick+1000, pc_respawn_timer,sd->bl.id,0);
  7288. return 1|8;
  7289. }
  7290. }
  7291. //GvG
  7292. if( mapdata_flag_gvg2(mapdata) ) {
  7293. sd->respawn_tid = add_timer(tick+1000, pc_respawn_timer, sd->bl.id, 0);
  7294. return 1|8;
  7295. }
  7296. else if( sd->bg_id ) {
  7297. std::shared_ptr<s_battleground_data> bg = util::umap_find(bg_team_db, sd->bg_id);
  7298. if (bg) {
  7299. if (bg->cemetery.map > 0) { // Respawn by BG
  7300. sd->respawn_tid = add_timer(tick + 1000, pc_respawn_timer, sd->bl.id, 0);
  7301. return 1|8;
  7302. }
  7303. }
  7304. }
  7305. //Reset "can log out" tick.
  7306. if( battle_config.prevent_logout )
  7307. sd->canlog_tick = gettick() - battle_config.prevent_logout;
  7308. return 1;
  7309. }
  7310. void pc_revive(struct map_session_data *sd,unsigned int hp, unsigned int sp) {
  7311. if(hp) clif_updatestatus(sd,SP_HP);
  7312. if(sp) clif_updatestatus(sd,SP_SP);
  7313. pc_setstand(sd, true);
  7314. if(battle_config.pc_invincible_time > 0)
  7315. pc_setinvincibletimer(sd, battle_config.pc_invincible_time);
  7316. if( sd->state.gmaster_flag ) {
  7317. guild_guildaura_refresh(sd,GD_LEADERSHIP,guild_checkskill(sd->guild,GD_LEADERSHIP));
  7318. guild_guildaura_refresh(sd,GD_GLORYWOUNDS,guild_checkskill(sd->guild,GD_GLORYWOUNDS));
  7319. guild_guildaura_refresh(sd,GD_SOULCOLD,guild_checkskill(sd->guild,GD_SOULCOLD));
  7320. guild_guildaura_refresh(sd,GD_HAWKEYES,guild_checkskill(sd->guild,GD_HAWKEYES));
  7321. }
  7322. }
  7323. bool pc_revive_item(struct map_session_data *sd) {
  7324. nullpo_retr(false, sd);
  7325. if (!pc_isdead(sd) || sd->respawn_tid != INVALID_TIMER)
  7326. return false;
  7327. if (sd->sc.data[SC_HELLPOWER]) // Cannot resurrect while under the effect of SC_HELLPOWER.
  7328. return false;
  7329. int16 item_position = itemdb_group_item_exists_pc(sd, IG_TOKEN_OF_SIEGFRIED);
  7330. uint8 hp = 100, sp = 100;
  7331. if (item_position < 0) {
  7332. if (sd->sc.data[SC_LIGHT_OF_REGENE]) {
  7333. hp = sd->sc.data[SC_LIGHT_OF_REGENE]->val2;
  7334. sp = 0;
  7335. }
  7336. else
  7337. return false;
  7338. }
  7339. if (!status_revive(&sd->bl, hp, sp))
  7340. return false;
  7341. if (item_position < 0)
  7342. status_change_end(&sd->bl, SC_LIGHT_OF_REGENE, INVALID_TIMER);
  7343. else
  7344. pc_delitem(sd, item_position, 1, 0, 1, LOG_TYPE_CONSUME);
  7345. clif_skill_nodamage(&sd->bl, &sd->bl, ALL_RESURRECTION, 4, 1);
  7346. return true;
  7347. }
  7348. // script
  7349. //
  7350. /*==========================================
  7351. * script reading pc status registry
  7352. *------------------------------------------*/
  7353. int64 pc_readparam(struct map_session_data* sd,int64 type)
  7354. {
  7355. int64 val = 0;
  7356. nullpo_ret(sd);
  7357. switch(type) {
  7358. case SP_SKILLPOINT: val = sd->status.skill_point; break;
  7359. case SP_STATUSPOINT: val = sd->status.status_point; break;
  7360. case SP_ZENY: val = sd->status.zeny; break;
  7361. case SP_BASELEVEL: val = sd->status.base_level; break;
  7362. case SP_JOBLEVEL: val = sd->status.job_level; break;
  7363. case SP_CLASS: val = sd->status.class_; break;
  7364. case SP_BASEJOB: val = pc_mapid2jobid(sd->class_&MAPID_UPPERMASK, sd->status.sex); break; //Base job, extracting upper type.
  7365. case SP_UPPER: val = sd->class_&JOBL_UPPER?1:(sd->class_&JOBL_BABY?2:0); break;
  7366. case SP_BASECLASS: val = pc_mapid2jobid(sd->class_&MAPID_BASEMASK, sd->status.sex); break; //Extract base class tree. [Skotlex]
  7367. case SP_SEX: val = sd->status.sex; break;
  7368. case SP_WEIGHT: val = sd->weight; break;
  7369. case SP_MAXWEIGHT: val = sd->max_weight; break;
  7370. case SP_BASEEXP: val = sd->status.base_exp; break;
  7371. case SP_JOBEXP: val = sd->status.job_exp; break;
  7372. case SP_NEXTBASEEXP: val = pc_nextbaseexp(sd); break;
  7373. case SP_NEXTJOBEXP: val = pc_nextjobexp(sd); break;
  7374. case SP_HP: val = sd->battle_status.hp; break;
  7375. case SP_MAXHP: val = sd->battle_status.max_hp; break;
  7376. case SP_SP: val = sd->battle_status.sp; break;
  7377. case SP_MAXSP: val = sd->battle_status.max_sp; break;
  7378. case SP_STR: val = sd->status.str; break;
  7379. case SP_AGI: val = sd->status.agi; break;
  7380. case SP_VIT: val = sd->status.vit; break;
  7381. case SP_INT: val = sd->status.int_; break;
  7382. case SP_DEX: val = sd->status.dex; break;
  7383. case SP_LUK: val = sd->status.luk; break;
  7384. case SP_KARMA: val = sd->status.karma; break;
  7385. case SP_MANNER: val = sd->status.manner; break;
  7386. case SP_FAME: val = sd->status.fame; break;
  7387. case SP_KILLERRID: val = sd->killerrid; break;
  7388. case SP_KILLEDRID: val = sd->killedrid; break;
  7389. case SP_KILLEDGID: val = sd->killedgid; break;
  7390. case SP_SITTING: val = pc_issit(sd)?1:0; break;
  7391. case SP_CHARMOVE: val = sd->status.character_moves; break;
  7392. case SP_CHARRENAME: val = sd->status.rename; break;
  7393. case SP_CHARFONT: val = sd->status.font; break;
  7394. case SP_BANK_VAULT: val = sd->bank_vault; break;
  7395. case SP_CASHPOINTS: val = sd->cashPoints; break;
  7396. case SP_KAFRAPOINTS: val = sd->kafraPoints; break;
  7397. case SP_ROULETTE_BRONZE: val = sd->roulette_point.bronze; break;
  7398. case SP_ROULETTE_SILVER: val = sd->roulette_point.silver; break;
  7399. case SP_ROULETTE_GOLD: val = sd->roulette_point.gold; break;
  7400. case SP_PCDIECOUNTER: val = sd->die_counter; break;
  7401. case SP_COOKMASTERY: val = sd->cook_mastery; break;
  7402. case SP_ACHIEVEMENT_LEVEL: val = sd->achievement_data.level; break;
  7403. case SP_CRITICAL: val = sd->battle_status.cri/10; break;
  7404. case SP_ASPD: val = (2000-sd->battle_status.amotion)/10; break;
  7405. case SP_BASE_ATK:
  7406. #ifdef RENEWAL
  7407. val = sd->bonus.eatk;
  7408. #else
  7409. val = sd->battle_status.batk;
  7410. #endif
  7411. break;
  7412. case SP_DEF1: val = sd->battle_status.def; break;
  7413. case SP_DEF2: val = sd->battle_status.def2; break;
  7414. case SP_MDEF1: val = sd->battle_status.mdef; break;
  7415. case SP_MDEF2: val = sd->battle_status.mdef2; break;
  7416. case SP_HIT: val = sd->battle_status.hit; break;
  7417. case SP_FLEE1: val = sd->battle_status.flee; break;
  7418. case SP_FLEE2: val = sd->battle_status.flee2; break;
  7419. case SP_DEFELE: val = sd->battle_status.def_ele; break;
  7420. case SP_MAXHPRATE: val = sd->hprate; break;
  7421. case SP_MAXSPRATE: val = sd->sprate; break;
  7422. case SP_SPRATE: val = sd->dsprate; break;
  7423. case SP_SPEED_RATE: val = sd->bonus.speed_rate; break;
  7424. case SP_SPEED_ADDRATE: val = sd->bonus.speed_add_rate; break;
  7425. case SP_ASPD_RATE:
  7426. #ifndef RENEWAL_ASPD
  7427. val = sd->battle_status.aspd_rate;
  7428. #else
  7429. val = sd->battle_status.aspd_rate2;
  7430. #endif
  7431. break;
  7432. case SP_HP_RECOV_RATE: val = sd->hprecov_rate; break;
  7433. case SP_SP_RECOV_RATE: val = sd->sprecov_rate; break;
  7434. case SP_CRITICAL_DEF: val = sd->bonus.critical_def; break;
  7435. case SP_NEAR_ATK_DEF: val = sd->bonus.near_attack_def_rate; break;
  7436. case SP_LONG_ATK_DEF: val = sd->bonus.long_attack_def_rate; break;
  7437. case SP_DOUBLE_RATE: val = sd->bonus.double_rate; break;
  7438. case SP_DOUBLE_ADD_RATE: val = sd->bonus.double_add_rate; break;
  7439. case SP_MATK_RATE: val = sd->matk_rate; break;
  7440. case SP_ATK_RATE: val = sd->bonus.atk_rate; break;
  7441. case SP_MAGIC_ATK_DEF: val = sd->bonus.magic_def_rate; break;
  7442. case SP_MISC_ATK_DEF: val = sd->bonus.misc_def_rate; break;
  7443. case SP_PERFECT_HIT_RATE:val = sd->bonus.perfect_hit; break;
  7444. case SP_PERFECT_HIT_ADD_RATE: val = sd->bonus.perfect_hit_add; break;
  7445. case SP_CRITICAL_RATE: val = sd->critical_rate; break;
  7446. case SP_HIT_RATE: val = sd->hit_rate; break;
  7447. case SP_FLEE_RATE: val = sd->flee_rate; break;
  7448. case SP_FLEE2_RATE: val = sd->flee2_rate; break;
  7449. case SP_DEF_RATE: val = sd->def_rate; break;
  7450. case SP_DEF2_RATE: val = sd->def2_rate; break;
  7451. case SP_MDEF_RATE: val = sd->mdef_rate; break;
  7452. case SP_MDEF2_RATE: val = sd->mdef2_rate; break;
  7453. case SP_RESTART_FULL_RECOVER: val = sd->special_state.restart_full_recover?1:0; break;
  7454. case SP_NO_CASTCANCEL: val = sd->special_state.no_castcancel?1:0; break;
  7455. case SP_NO_CASTCANCEL2: val = sd->special_state.no_castcancel2?1:0; break;
  7456. case SP_NO_SIZEFIX: val = sd->special_state.no_sizefix?1:0; break;
  7457. case SP_NO_MAGIC_DAMAGE: val = sd->special_state.no_magic_damage; break;
  7458. case SP_NO_WEAPON_DAMAGE:val = sd->special_state.no_weapon_damage; break;
  7459. case SP_NO_MISC_DAMAGE: val = sd->special_state.no_misc_damage; break;
  7460. case SP_NO_GEMSTONE: val = sd->special_state.no_gemstone?1:0; break;
  7461. case SP_INTRAVISION: val = sd->special_state.intravision?1:0; break;
  7462. case SP_NO_KNOCKBACK: val = sd->special_state.no_knockback?1:0; break;
  7463. case SP_NO_MADO_FUEL: val = sd->special_state.no_mado_fuel?1:0; break;
  7464. case SP_NO_WALK_DELAY: val = sd->special_state.no_walk_delay?1:0; break;
  7465. case SP_SPLASH_RANGE: val = sd->bonus.splash_range; break;
  7466. case SP_SPLASH_ADD_RANGE:val = sd->bonus.splash_add_range; break;
  7467. case SP_SHORT_WEAPON_DAMAGE_RETURN: val = sd->bonus.short_weapon_damage_return; break;
  7468. case SP_LONG_WEAPON_DAMAGE_RETURN: val = sd->bonus.long_weapon_damage_return; break;
  7469. case SP_MAGIC_DAMAGE_RETURN: val = sd->bonus.magic_damage_return; break;
  7470. case SP_PERFECT_HIDE: val = sd->special_state.perfect_hiding?1:0; break;
  7471. case SP_UNBREAKABLE: val = sd->bonus.unbreakable; break;
  7472. case SP_UNBREAKABLE_WEAPON: val = (sd->bonus.unbreakable_equip&EQP_WEAPON)?1:0; break;
  7473. case SP_UNBREAKABLE_ARMOR: val = (sd->bonus.unbreakable_equip&EQP_ARMOR)?1:0; break;
  7474. case SP_UNBREAKABLE_HELM: val = (sd->bonus.unbreakable_equip&EQP_HELM)?1:0; break;
  7475. case SP_UNBREAKABLE_SHIELD: val = (sd->bonus.unbreakable_equip&EQP_SHIELD)?1:0; break;
  7476. case SP_UNBREAKABLE_GARMENT: val = (sd->bonus.unbreakable_equip&EQP_GARMENT)?1:0; break;
  7477. case SP_UNBREAKABLE_SHOES: val = (sd->bonus.unbreakable_equip&EQP_SHOES)?1:0; break;
  7478. case SP_CLASSCHANGE: val = sd->bonus.classchange; break;
  7479. case SP_LONG_ATK_RATE: val = sd->bonus.long_attack_atk_rate; break;
  7480. case SP_BREAK_WEAPON_RATE: val = sd->bonus.break_weapon_rate; break;
  7481. case SP_BREAK_ARMOR_RATE: val = sd->bonus.break_armor_rate; break;
  7482. case SP_ADD_STEAL_RATE: val = sd->bonus.add_steal_rate; break;
  7483. case SP_DELAYRATE: val = sd->delayrate; break;
  7484. case SP_CRIT_ATK_RATE: val = sd->bonus.crit_atk_rate; break;
  7485. case SP_UNSTRIPABLE_WEAPON: val = (sd->bonus.unstripable_equip&EQP_WEAPON)?1:0; break;
  7486. case SP_UNSTRIPABLE:
  7487. case SP_UNSTRIPABLE_ARMOR:
  7488. val = (sd->bonus.unstripable_equip&EQP_ARMOR)?1:0;
  7489. break;
  7490. case SP_UNSTRIPABLE_HELM: val = (sd->bonus.unstripable_equip&EQP_HELM)?1:0; break;
  7491. case SP_UNSTRIPABLE_SHIELD: val = (sd->bonus.unstripable_equip&EQP_SHIELD)?1:0; break;
  7492. case SP_SP_GAIN_VALUE: val = sd->bonus.sp_gain_value; break;
  7493. case SP_HP_GAIN_VALUE: val = sd->bonus.hp_gain_value; break;
  7494. case SP_LONG_SP_GAIN_VALUE: val = sd->bonus.long_sp_gain_value; break;
  7495. case SP_LONG_HP_GAIN_VALUE: val = sd->bonus.long_hp_gain_value; break;
  7496. case SP_MAGIC_SP_GAIN_VALUE: val = sd->bonus.magic_sp_gain_value; break;
  7497. case SP_MAGIC_HP_GAIN_VALUE: val = sd->bonus.magic_hp_gain_value; break;
  7498. case SP_ADD_HEAL_RATE: val = sd->bonus.add_heal_rate; break;
  7499. case SP_ADD_HEAL2_RATE: val = sd->bonus.add_heal2_rate; break;
  7500. case SP_ADD_ITEM_HEAL_RATE: val = sd->bonus.itemhealrate2; break;
  7501. case SP_EMATK: val = sd->bonus.ematk; break;
  7502. case SP_FIXCASTRATE: val = sd->bonus.fixcastrate; break;
  7503. case SP_ADD_FIXEDCAST: val = sd->bonus.add_fixcast; break;
  7504. case SP_ADD_VARIABLECAST: val = sd->bonus.add_varcast; break;
  7505. case SP_CASTRATE:
  7506. case SP_VARCASTRATE:
  7507. #ifdef RENEWAL_CAST
  7508. val = sd->bonus.varcastrate; break;
  7509. #else
  7510. val = sd->castrate; break;
  7511. #endif
  7512. default:
  7513. ShowError("pc_readparam: Attempt to read unknown parameter '%lld'.\n", type);
  7514. return -1;
  7515. }
  7516. return val;
  7517. }
  7518. /*==========================================
  7519. * script set pc status registry
  7520. *------------------------------------------*/
  7521. bool pc_setparam(struct map_session_data *sd,int64 type,int64 val_tmp)
  7522. {
  7523. nullpo_retr(false,sd);
  7524. int val = static_cast<unsigned int>(val_tmp);
  7525. switch(type){
  7526. case SP_BASELEVEL:
  7527. if (val > pc_maxbaselv(sd)) //Capping to max
  7528. val = pc_maxbaselv(sd);
  7529. if (val > sd->status.base_level) {
  7530. int i = 0;
  7531. int stat=0;
  7532. for (i = 0; i < (int)(val - sd->status.base_level); i++)
  7533. stat += pc_gets_status_point(sd->status.base_level + i);
  7534. sd->status.status_point += stat;
  7535. }
  7536. sd->status.base_level = val;
  7537. sd->status.base_exp = 0;
  7538. // clif_updatestatus(sd, SP_BASELEVEL); // Gets updated at the bottom
  7539. clif_updatestatus(sd, SP_NEXTBASEEXP);
  7540. clif_updatestatus(sd, SP_STATUSPOINT);
  7541. clif_updatestatus(sd, SP_BASEEXP);
  7542. status_calc_pc(sd, SCO_FORCE);
  7543. if(sd->status.party_id)
  7544. party_send_levelup(sd);
  7545. break;
  7546. case SP_JOBLEVEL:
  7547. if (val >= sd->status.job_level) {
  7548. if (val > pc_maxjoblv(sd)) val = pc_maxjoblv(sd);
  7549. sd->status.skill_point += val - sd->status.job_level;
  7550. clif_updatestatus(sd, SP_SKILLPOINT);
  7551. }
  7552. sd->status.job_level = val;
  7553. sd->status.job_exp = 0;
  7554. // clif_updatestatus(sd, SP_JOBLEVEL); // Gets updated at the bottom
  7555. clif_updatestatus(sd, SP_NEXTJOBEXP);
  7556. clif_updatestatus(sd, SP_JOBEXP);
  7557. status_calc_pc(sd, SCO_FORCE);
  7558. break;
  7559. case SP_SKILLPOINT:
  7560. sd->status.skill_point = val;
  7561. break;
  7562. case SP_STATUSPOINT:
  7563. sd->status.status_point = val;
  7564. break;
  7565. case SP_ZENY:
  7566. if( val < 0 )
  7567. return false;// can't set negative zeny
  7568. log_zeny(sd, LOG_TYPE_SCRIPT, sd, -(sd->status.zeny - cap_value(val, 0, MAX_ZENY)));
  7569. sd->status.zeny = cap_value(val, 0, MAX_ZENY);
  7570. break;
  7571. case SP_BASEEXP:
  7572. {
  7573. val = cap_value(val, 0, INT_MAX);
  7574. if (val < sd->status.base_exp) // Lost
  7575. pc_lostexp(sd, sd->status.base_exp - val, 0);
  7576. else // Gained
  7577. pc_gainexp(sd, NULL, val - sd->status.base_exp, 0, 2);
  7578. }
  7579. return true;
  7580. case SP_JOBEXP:
  7581. {
  7582. val = cap_value(val, 0, INT_MAX);
  7583. if (val < sd->status.job_exp) // Lost
  7584. pc_lostexp(sd, 0, sd->status.job_exp - val);
  7585. else // Gained
  7586. pc_gainexp(sd, NULL, 0, val - sd->status.job_exp, 2);
  7587. }
  7588. return true;
  7589. case SP_SEX:
  7590. sd->status.sex = val ? SEX_MALE : SEX_FEMALE;
  7591. break;
  7592. case SP_WEIGHT:
  7593. sd->weight = val;
  7594. break;
  7595. case SP_MAXWEIGHT:
  7596. sd->max_weight = val;
  7597. break;
  7598. case SP_HP:
  7599. sd->battle_status.hp = cap_value(val, 1, (int)sd->battle_status.max_hp);
  7600. break;
  7601. case SP_MAXHP:
  7602. if (sd->status.base_level < 100)
  7603. sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp_lv99);
  7604. else if (sd->status.base_level < 151)
  7605. sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp_lv150);
  7606. else
  7607. sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp);
  7608. if( sd->battle_status.max_hp < sd->battle_status.hp )
  7609. {
  7610. sd->battle_status.hp = sd->battle_status.max_hp;
  7611. clif_updatestatus(sd, SP_HP);
  7612. }
  7613. break;
  7614. case SP_SP:
  7615. sd->battle_status.sp = cap_value(val, 0, (int)sd->battle_status.max_sp);
  7616. break;
  7617. case SP_MAXSP:
  7618. sd->battle_status.max_sp = cap_value(val, 1, battle_config.max_sp);
  7619. if( sd->battle_status.max_sp < sd->battle_status.sp )
  7620. {
  7621. sd->battle_status.sp = sd->battle_status.max_sp;
  7622. clif_updatestatus(sd, SP_SP);
  7623. }
  7624. break;
  7625. case SP_STR:
  7626. sd->status.str = cap_value(val, 1, pc_maxparameter(sd,PARAM_STR));
  7627. break;
  7628. case SP_AGI:
  7629. sd->status.agi = cap_value(val, 1, pc_maxparameter(sd,PARAM_AGI));
  7630. break;
  7631. case SP_VIT:
  7632. sd->status.vit = cap_value(val, 1, pc_maxparameter(sd,PARAM_VIT));
  7633. break;
  7634. case SP_INT:
  7635. sd->status.int_ = cap_value(val, 1, pc_maxparameter(sd,PARAM_INT));
  7636. break;
  7637. case SP_DEX:
  7638. sd->status.dex = cap_value(val, 1, pc_maxparameter(sd,PARAM_DEX));
  7639. break;
  7640. case SP_LUK:
  7641. sd->status.luk = cap_value(val, 1, pc_maxparameter(sd,PARAM_LUK));
  7642. break;
  7643. case SP_KARMA:
  7644. sd->status.karma = val;
  7645. break;
  7646. case SP_MANNER:
  7647. sd->status.manner = val;
  7648. if( val < 0 )
  7649. sc_start(NULL, &sd->bl, SC_NOCHAT, 100, 0, 0);
  7650. else {
  7651. status_change_end(&sd->bl, SC_NOCHAT, INVALID_TIMER);
  7652. clif_manner_message(sd, 5);
  7653. }
  7654. return true; // status_change_start/status_change_end already sends packets warning the client
  7655. case SP_FAME:
  7656. sd->status.fame = val;
  7657. break;
  7658. case SP_KILLERRID:
  7659. sd->killerrid = val;
  7660. return true;
  7661. case SP_KILLEDRID:
  7662. sd->killedrid = val;
  7663. return true;
  7664. case SP_KILLEDGID:
  7665. sd->killedgid = val;
  7666. return true;
  7667. case SP_CHARMOVE:
  7668. sd->status.character_moves = val;
  7669. return true;
  7670. case SP_CHARRENAME:
  7671. sd->status.rename = val;
  7672. return true;
  7673. case SP_CHARFONT:
  7674. sd->status.font = val;
  7675. clif_font(sd);
  7676. return true;
  7677. case SP_BANK_VAULT:
  7678. if (val < 0)
  7679. return false;
  7680. log_zeny(sd, LOG_TYPE_BANK, sd, -(sd->bank_vault - cap_value(val, 0, MAX_BANK_ZENY)));
  7681. sd->bank_vault = cap_value(val, 0, MAX_BANK_ZENY);
  7682. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  7683. return true;
  7684. case SP_ROULETTE_BRONZE:
  7685. sd->roulette_point.bronze = val;
  7686. pc_setreg2(sd, ROULETTE_BRONZE_VAR, sd->roulette_point.bronze);
  7687. return true;
  7688. case SP_ROULETTE_SILVER:
  7689. sd->roulette_point.silver = val;
  7690. pc_setreg2(sd, ROULETTE_SILVER_VAR, sd->roulette_point.silver);
  7691. return true;
  7692. case SP_ROULETTE_GOLD:
  7693. sd->roulette_point.gold = val;
  7694. pc_setreg2(sd, ROULETTE_GOLD_VAR, sd->roulette_point.gold);
  7695. return true;
  7696. case SP_CASHPOINTS:
  7697. if (val < 0)
  7698. return false;
  7699. if (!sd->state.connect_new)
  7700. log_cash(sd, LOG_TYPE_SCRIPT, LOG_CASH_TYPE_CASH, -(sd->cashPoints - cap_value(val, 0, MAX_ZENY)));
  7701. sd->cashPoints = cap_value(val, 0, MAX_ZENY);
  7702. pc_setaccountreg(sd, add_str(CASHPOINT_VAR), sd->cashPoints);
  7703. return true;
  7704. case SP_KAFRAPOINTS:
  7705. if (val < 0)
  7706. return false;
  7707. if (!sd->state.connect_new)
  7708. log_cash(sd, LOG_TYPE_SCRIPT, LOG_CASH_TYPE_KAFRA, -(sd->kafraPoints - cap_value(val, 0, MAX_ZENY)));
  7709. sd->kafraPoints = cap_value(val, 0, MAX_ZENY);
  7710. pc_setaccountreg(sd, add_str(KAFRAPOINT_VAR), sd->kafraPoints);
  7711. return true;
  7712. case SP_PCDIECOUNTER:
  7713. if (val < 0)
  7714. return false;
  7715. if (sd->die_counter == val)
  7716. return true;
  7717. sd->die_counter = val;
  7718. if (!sd->die_counter && (sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE)
  7719. status_calc_pc(sd, SCO_NONE); // Lost the bonus.
  7720. pc_setglobalreg(sd, add_str(PCDIECOUNTER_VAR), sd->die_counter);
  7721. return true;
  7722. case SP_COOKMASTERY:
  7723. if (val < 0)
  7724. return false;
  7725. if (sd->cook_mastery == val)
  7726. return true;
  7727. val = cap_value(val, 0, 1999);
  7728. sd->cook_mastery = val;
  7729. pc_setglobalreg(sd, add_str(COOKMASTERY_VAR), sd->cook_mastery);
  7730. return true;
  7731. default:
  7732. ShowError("pc_setparam: Attempted to set unknown parameter '%lld'.\n", type);
  7733. return false;
  7734. }
  7735. clif_updatestatus(sd,static_cast<int>(type));
  7736. return true;
  7737. }
  7738. /*==========================================
  7739. * HP/SP Healing. If flag is passed, the heal type is through clif_heal, otherwise update status.
  7740. *------------------------------------------*/
  7741. void pc_heal(struct map_session_data *sd,unsigned int hp,unsigned int sp, int type)
  7742. {
  7743. if (type&2) {
  7744. if (hp || type&4)
  7745. clif_heal(sd->fd,SP_HP,hp);
  7746. if (sp)
  7747. clif_heal(sd->fd,SP_SP,sp);
  7748. } else {
  7749. if(hp)
  7750. clif_updatestatus(sd,SP_HP);
  7751. if(sp)
  7752. clif_updatestatus(sd,SP_SP);
  7753. }
  7754. return;
  7755. }
  7756. /**
  7757. * Heal player HP and/or SP linearly. Calculate any bonus based on active statuses.
  7758. * @param sd: Player data
  7759. * @param itemid: Item ID
  7760. * @param hp: HP to heal
  7761. * @param sp: SP to heal
  7762. * @return Amount healed to an object
  7763. */
  7764. int pc_itemheal(struct map_session_data *sd, int itemid, int hp, int sp)
  7765. {
  7766. int bonus, tmp, penalty = 0;
  7767. if (hp) {
  7768. bonus = 100 + (sd->battle_status.vit << 1) + pc_checkskill(sd, SM_RECOVERY) * 10 + pc_checkskill(sd, AM_LEARNINGPOTION) * 5;
  7769. // A potion produced by an Alchemist in the Fame Top 10 gets +50% effect [DracoRPG]
  7770. if (potion_flag == 2) {
  7771. bonus += 50;
  7772. if (sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_ROGUE)
  7773. bonus += 100; // Receive an additional +100% effect from ranked potions to HP only
  7774. }
  7775. //All item bonuses.
  7776. bonus += sd->bonus.itemhealrate2;
  7777. //Item Group bonuses
  7778. bonus += pc_get_itemgroup_bonus(sd, itemid);
  7779. //Individual item bonuses.
  7780. for(const auto &it : sd->itemhealrate) {
  7781. if (it.id == itemid) {
  7782. bonus += it.val;
  7783. break;
  7784. }
  7785. }
  7786. // Recovery Potion
  7787. if (sd->sc.data[SC_INCHEALRATE])
  7788. bonus += sd->sc.data[SC_INCHEALRATE]->val1;
  7789. // 2014 Halloween Event : Pumpkin Bonus
  7790. if (sd->sc.data[SC_MTF_PUMPKIN] && itemid == ITEMID_PUMPKIN)
  7791. bonus += sd->sc.data[SC_MTF_PUMPKIN]->val1;
  7792. tmp = hp * bonus / 100; // Overflow check
  7793. if (bonus != 100 && tmp > hp)
  7794. hp = tmp;
  7795. }
  7796. if (sp) {
  7797. bonus = 100 + (sd->battle_status.int_ << 1) + pc_checkskill(sd, MG_SRECOVERY) * 10 + pc_checkskill(sd, AM_LEARNINGPOTION) * 5;
  7798. // A potion produced by an Alchemist in the Fame Top 10 gets +50% effect [DracoRPG]
  7799. if (potion_flag == 2)
  7800. bonus += 50;
  7801. tmp = sp * bonus / 100; // Overflow check
  7802. if (bonus != 100 && tmp > sp)
  7803. sp = tmp;
  7804. }
  7805. if (sd->sc.count) {
  7806. // Critical Wound and Death Hurt stack
  7807. if (sd->sc.data[SC_CRITICALWOUND])
  7808. penalty += sd->sc.data[SC_CRITICALWOUND]->val2;
  7809. if (sd->sc.data[SC_DEATHHURT])
  7810. penalty += 20;
  7811. if (sd->sc.data[SC_NORECOVER_STATE])
  7812. penalty = 100;
  7813. if (sd->sc.data[SC_VITALITYACTIVATION]) {
  7814. hp += hp / 2; // 1.5 times
  7815. sp -= sp / 2;
  7816. }
  7817. if (sd->sc.data[SC_WATER_INSIGNIA] && sd->sc.data[SC_WATER_INSIGNIA]->val1 == 2) {
  7818. hp += hp / 10;
  7819. sp += sp / 10;
  7820. }
  7821. if (penalty > 0) {
  7822. hp -= hp * penalty / 100;
  7823. sp -= sp * penalty / 100;
  7824. }
  7825. #ifdef RENEWAL
  7826. if (sd->sc.data[SC_EXTREMITYFIST2])
  7827. sp = 0;
  7828. #endif
  7829. if (sd->sc.data[SC_BITESCAR])
  7830. hp = 0;
  7831. }
  7832. return status_heal(&sd->bl, hp, sp, 1);
  7833. }
  7834. /*==========================================
  7835. * HP/SP Recovery
  7836. * Heal player hp nad/or sp by rate
  7837. *------------------------------------------*/
  7838. int pc_percentheal(struct map_session_data *sd,int hp,int sp)
  7839. {
  7840. nullpo_ret(sd);
  7841. if (hp > 100) hp = 100;
  7842. else if (hp <-100) hp = -100;
  7843. if (sp > 100) sp = 100;
  7844. else if (sp <-100) sp = -100;
  7845. if(hp >= 0 && sp >= 0) //Heal
  7846. return status_percent_heal(&sd->bl, hp, sp);
  7847. if(hp <= 0 && sp <= 0) //Damage (negative rates indicate % of max rather than current), and only kill target IF the specified amount is 100%
  7848. return status_percent_damage(NULL, &sd->bl, hp, sp, hp==-100);
  7849. //Crossed signs
  7850. if(hp) {
  7851. if(hp > 0)
  7852. status_percent_heal(&sd->bl, hp, 0);
  7853. else
  7854. status_percent_damage(NULL, &sd->bl, hp, 0, hp==-100);
  7855. }
  7856. if(sp) {
  7857. if(sp > 0)
  7858. status_percent_heal(&sd->bl, 0, sp);
  7859. else
  7860. status_percent_damage(NULL, &sd->bl, 0, sp, false);
  7861. }
  7862. return 0;
  7863. }
  7864. static int jobchange_killclone(struct block_list *bl, va_list ap)
  7865. {
  7866. struct mob_data *md;
  7867. int flag;
  7868. md = (struct mob_data *)bl;
  7869. nullpo_ret(md);
  7870. flag = va_arg(ap, int);
  7871. if (md->master_id && md->special_state.clone && md->master_id == flag)
  7872. status_kill(&md->bl);
  7873. return 1;
  7874. }
  7875. /**
  7876. * Called when player changes job
  7877. * Rewrote to make it tidider [Celest]
  7878. * @param sd
  7879. * @param job JOB ID. See enum e_job
  7880. * @param upper 1 - JOBL_UPPER; 2 - JOBL_BABY
  7881. * @return True if success, false if failed
  7882. **/
  7883. bool pc_jobchange(struct map_session_data *sd,int job, char upper)
  7884. {
  7885. int i, fame_flag = 0;
  7886. int b_class;
  7887. nullpo_retr(false,sd);
  7888. if (job < 0)
  7889. return false;
  7890. //Normalize job.
  7891. b_class = pc_jobid2mapid(job);
  7892. if (b_class == -1)
  7893. return false;
  7894. switch (upper) {
  7895. case 1:
  7896. b_class|= JOBL_UPPER;
  7897. break;
  7898. case 2:
  7899. b_class|= JOBL_BABY;
  7900. break;
  7901. }
  7902. //This will automatically adjust bard/dancer classes to the correct gender
  7903. //That is, if you try to jobchange into dancer, it will turn you to bard.
  7904. job = pc_mapid2jobid(b_class, sd->status.sex);
  7905. if (job == -1)
  7906. return false;
  7907. if ((unsigned short)b_class == sd->class_)
  7908. return false; //Nothing to change.
  7909. // changing from 1st to 2nd job
  7910. if ((b_class&JOBL_2) && !(sd->class_&JOBL_2) && (sd->class_&MAPID_UPPERMASK) != MAPID_SUPER_NOVICE) {
  7911. sd->change_level_2nd = sd->status.job_level;
  7912. pc_setglobalreg(sd, add_str(JOBCHANGE2ND_VAR), sd->change_level_2nd);
  7913. }
  7914. // changing from 2nd to 3rd job
  7915. else if((b_class&JOBL_THIRD) && !(sd->class_&JOBL_THIRD)) {
  7916. sd->change_level_3rd = sd->status.job_level;
  7917. pc_setglobalreg(sd, add_str(JOBCHANGE3RD_VAR), sd->change_level_3rd);
  7918. }
  7919. if(sd->cloneskill_idx > 0) {
  7920. if( sd->status.skill[sd->cloneskill_idx].flag == SKILL_FLAG_PLAGIARIZED ) {
  7921. sd->status.skill[sd->cloneskill_idx].id = 0;
  7922. sd->status.skill[sd->cloneskill_idx].lv = 0;
  7923. sd->status.skill[sd->cloneskill_idx].flag = SKILL_FLAG_PERMANENT;
  7924. clif_deleteskill(sd, static_cast<int>(pc_readglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM))));
  7925. }
  7926. sd->cloneskill_idx = 0;
  7927. pc_setglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM), 0);
  7928. pc_setglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM_LV), 0);
  7929. }
  7930. if(sd->reproduceskill_idx > 0) {
  7931. if( sd->status.skill[sd->reproduceskill_idx].flag == SKILL_FLAG_PLAGIARIZED ) {
  7932. sd->status.skill[sd->reproduceskill_idx].id = 0;
  7933. sd->status.skill[sd->reproduceskill_idx].lv = 0;
  7934. sd->status.skill[sd->reproduceskill_idx].flag = SKILL_FLAG_PERMANENT;
  7935. clif_deleteskill(sd, static_cast<int>(pc_readglobalreg(sd, add_str(SKILL_VAR_REPRODUCE))));
  7936. }
  7937. sd->reproduceskill_idx = 0;
  7938. pc_setglobalreg(sd, add_str(SKILL_VAR_REPRODUCE), 0);
  7939. pc_setglobalreg(sd, add_str(SKILL_VAR_REPRODUCE_LV), 0);
  7940. }
  7941. // Give or reduce transcendent status points
  7942. if( (b_class&JOBL_UPPER) && !(sd->class_&JOBL_UPPER) ){ // Change from a non t class to a t class -> give points
  7943. sd->status.status_point += battle_config.transcendent_status_points;
  7944. clif_updatestatus(sd,SP_STATUSPOINT);
  7945. }else if( !(b_class&JOBL_UPPER) && (sd->class_&JOBL_UPPER) ){ // Change from a t class to a non t class -> remove points
  7946. if( sd->status.status_point < battle_config.transcendent_status_points ){
  7947. // The player already used his bonus points, so we have to reset his status points
  7948. pc_resetstate(sd);
  7949. }
  7950. sd->status.status_point -= battle_config.transcendent_status_points;
  7951. clif_updatestatus(sd,SP_STATUSPOINT);
  7952. }
  7953. if ( (b_class&MAPID_UPPERMASK) != (sd->class_&MAPID_UPPERMASK) ) { //Things to remove when changing class tree.
  7954. const int class_ = pc_class2idx(sd->status.class_);
  7955. uint16 skill_id;
  7956. for(i = 0; i < MAX_SKILL_TREE && (skill_id = skill_tree[class_][i].skill_id) > 0; i++) {
  7957. //Remove status specific to your current tree skills.
  7958. enum sc_type sc = status_skill2sc(skill_id);
  7959. if (sc > SC_COMMON_MAX && sd->sc.data[sc])
  7960. status_change_end(&sd->bl, sc, INVALID_TIMER);
  7961. }
  7962. }
  7963. if( (sd->class_&MAPID_UPPERMASK) == MAPID_STAR_GLADIATOR && (b_class&MAPID_UPPERMASK) != MAPID_STAR_GLADIATOR) {
  7964. /* going off star glad lineage, reset feel to not store no-longer-used vars in the database */
  7965. pc_resetfeel(sd);
  7966. }
  7967. // Reset body style to 0 before changing job to avoid
  7968. // errors since not every job has a alternate outfit.
  7969. sd->status.body = 0;
  7970. clif_changelook(&sd->bl,LOOK_BODY2,0);
  7971. sd->status.class_ = job;
  7972. fame_flag = pc_famerank(sd->status.char_id,sd->class_&MAPID_UPPERMASK);
  7973. sd->class_ = (unsigned short)b_class;
  7974. sd->status.job_level=1;
  7975. sd->status.job_exp=0;
  7976. if (sd->status.base_level > pc_maxbaselv(sd)) {
  7977. sd->status.base_level = pc_maxbaselv(sd);
  7978. sd->status.base_exp=0;
  7979. pc_resetstate(sd);
  7980. clif_updatestatus(sd,SP_STATUSPOINT);
  7981. clif_updatestatus(sd,SP_BASELEVEL);
  7982. clif_updatestatus(sd,SP_BASEEXP);
  7983. clif_updatestatus(sd,SP_NEXTBASEEXP);
  7984. }
  7985. clif_updatestatus(sd,SP_JOBLEVEL);
  7986. clif_updatestatus(sd,SP_JOBEXP);
  7987. clif_updatestatus(sd,SP_NEXTJOBEXP);
  7988. for(i=0;i<EQI_MAX;i++) {
  7989. if(sd->equip_index[i] >= 0)
  7990. if(pc_isequip(sd,sd->equip_index[i]))
  7991. pc_unequipitem(sd,sd->equip_index[i],2); // unequip invalid item for class
  7992. }
  7993. //Change look, if disguised, you need to undisguise
  7994. //to correctly calculate new job sprite without
  7995. if (sd->disguise)
  7996. pc_disguise(sd, 0);
  7997. status_set_viewdata(&sd->bl, job);
  7998. clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_); // move sprite update to prevent client crashes with incompatible equipment [Valaris]
  7999. #if PACKETVER >= 20151001
  8000. clif_changelook(&sd->bl, LOOK_HAIR, sd->vd.hair_style); // Update player's head (only matters when switching to or from Doram)
  8001. #endif
  8002. if(sd->vd.cloth_color)
  8003. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  8004. /*
  8005. if(sd->vd.body_style)
  8006. clif_changelook(&sd->bl,LOOK_BODY2,sd->vd.body_style);
  8007. */
  8008. //Update skill tree.
  8009. pc_calc_skilltree(sd);
  8010. clif_skillinfoblock(sd);
  8011. if (sd->ed)
  8012. elemental_delete(sd->ed);
  8013. if (sd->state.vending)
  8014. vending_closevending(sd);
  8015. if (sd->state.buyingstore)
  8016. buyingstore_close(sd);
  8017. map_foreachinmap(jobchange_killclone, sd->bl.m, BL_MOB, sd->bl.id);
  8018. //Remove peco/cart/falcon
  8019. i = sd->sc.option;
  8020. if( i&OPTION_RIDING && !pc_checkskill(sd, KN_RIDING) )
  8021. i&=~OPTION_RIDING;
  8022. if( i&OPTION_FALCON && !pc_checkskill(sd, HT_FALCON) )
  8023. i&=~OPTION_FALCON;
  8024. if( i&OPTION_DRAGON && !pc_checkskill(sd,RK_DRAGONTRAINING) )
  8025. i&=~OPTION_DRAGON;
  8026. if( i&OPTION_WUGRIDER && !pc_checkskill(sd,RA_WUGMASTERY) )
  8027. i&=~OPTION_WUGRIDER;
  8028. if( i&OPTION_WUG && !pc_checkskill(sd,RA_WUGMASTERY) )
  8029. i&=~OPTION_WUG;
  8030. if( i&OPTION_MADOGEAR ) //You do not need a skill for this.
  8031. i&=~OPTION_MADOGEAR;
  8032. #ifndef NEW_CARTS
  8033. if( i&OPTION_CART && !pc_checkskill(sd, MC_PUSHCART) )
  8034. i&=~OPTION_CART;
  8035. #else
  8036. if( sd->sc.data[SC_PUSH_CART] && !pc_checkskill(sd, MC_PUSHCART) )
  8037. pc_setcart(sd, 0);
  8038. #endif
  8039. if(i != sd->sc.option)
  8040. pc_setoption(sd, i);
  8041. if(hom_is_active(sd->hd) && !pc_checkskill(sd, AM_CALLHOMUN))
  8042. hom_vaporize(sd, HOM_ST_ACTIVE);
  8043. if (sd->sc.data[SC_SPRITEMABLE] && !pc_checkskill(sd, SU_SPRITEMABLE))
  8044. status_change_end(&sd->bl, SC_SPRITEMABLE, INVALID_TIMER);
  8045. if(sd->status.manner < 0)
  8046. clif_changestatus(sd,SP_MANNER,sd->status.manner);
  8047. status_calc_pc(sd,SCO_FORCE);
  8048. pc_checkallowskill(sd);
  8049. pc_equiplookall(sd);
  8050. pc_show_questinfo(sd);
  8051. achievement_update_objective(sd, AG_JOB_CHANGE, 2, sd->status.base_level, job);
  8052. if( sd->status.party_id ){
  8053. struct party_data* p;
  8054. if( ( p = party_search( sd->status.party_id ) ) != NULL ){
  8055. ARR_FIND(0, MAX_PARTY, i, p->party.member[i].char_id == sd->status.char_id);
  8056. if( i < MAX_PARTY ){
  8057. p->party.member[i].class_ = sd->status.class_;
  8058. clif_party_job_and_level(sd);
  8059. }
  8060. }
  8061. }
  8062. chrif_save(sd, CSAVE_NORMAL);
  8063. //if you were previously famous, not anymore.
  8064. if (fame_flag)
  8065. chrif_buildfamelist();
  8066. else if (sd->status.fame > 0) {
  8067. //It may be that now they are famous?
  8068. switch (sd->class_&MAPID_UPPERMASK) {
  8069. case MAPID_BLACKSMITH:
  8070. case MAPID_ALCHEMIST:
  8071. case MAPID_TAEKWON:
  8072. chrif_buildfamelist();
  8073. break;
  8074. }
  8075. }
  8076. return true;
  8077. }
  8078. /*==========================================
  8079. * Tell client player sd has change equipement
  8080. *------------------------------------------*/
  8081. void pc_equiplookall(struct map_session_data *sd)
  8082. {
  8083. nullpo_retv(sd);
  8084. clif_changelook(&sd->bl,LOOK_WEAPON,0);
  8085. clif_changelook(&sd->bl,LOOK_SHOES,0);
  8086. clif_changelook(&sd->bl,LOOK_HEAD_BOTTOM,sd->status.head_bottom);
  8087. clif_changelook(&sd->bl,LOOK_HEAD_TOP,sd->status.head_top);
  8088. clif_changelook(&sd->bl,LOOK_HEAD_MID,sd->status.head_mid);
  8089. clif_changelook(&sd->bl,LOOK_ROBE, sd->status.robe);
  8090. }
  8091. /*==========================================
  8092. * Tell client player sd has change look (hair,equip...)
  8093. *------------------------------------------*/
  8094. void pc_changelook(struct map_session_data *sd,int type,int val) {
  8095. nullpo_retv(sd);
  8096. switch(type) {
  8097. case LOOK_HAIR: //Use the battle_config limits! [Skotlex]
  8098. val = cap_value(val, MIN_HAIR_STYLE, MAX_HAIR_STYLE);
  8099. if (sd->status.hair != val) {
  8100. sd->status.hair = val;
  8101. if (sd->status.guild_id) //Update Guild Window. [Skotlex]
  8102. intif_guild_change_memberinfo(sd->status.guild_id, sd->status.account_id, sd->status.char_id,
  8103. GMI_HAIR, &sd->status.hair, sizeof(sd->status.hair));
  8104. }
  8105. break;
  8106. case LOOK_WEAPON:
  8107. sd->status.weapon = val;
  8108. break;
  8109. case LOOK_HEAD_BOTTOM:
  8110. sd->status.head_bottom = val;
  8111. sd->setlook_head_bottom = val;
  8112. break;
  8113. case LOOK_HEAD_TOP:
  8114. sd->status.head_top = val;
  8115. sd->setlook_head_top = val;
  8116. break;
  8117. case LOOK_HEAD_MID:
  8118. sd->status.head_mid = val;
  8119. sd->setlook_head_mid = val;
  8120. break;
  8121. case LOOK_HAIR_COLOR: //Use the battle_config limits! [Skotlex]
  8122. val = cap_value(val, MIN_HAIR_COLOR, MAX_HAIR_COLOR);
  8123. if (sd->status.hair_color != val) {
  8124. sd->status.hair_color = val;
  8125. if (sd->status.guild_id) //Update Guild Window. [Skotlex]
  8126. intif_guild_change_memberinfo(sd->status.guild_id, sd->status.account_id, sd->status.char_id,
  8127. GMI_HAIR_COLOR, &sd->status.hair_color, sizeof(sd->status.hair_color));
  8128. }
  8129. break;
  8130. case LOOK_CLOTHES_COLOR: //Use the battle_config limits! [Skotlex]
  8131. val = cap_value(val, MIN_CLOTH_COLOR, MAX_CLOTH_COLOR);
  8132. sd->status.clothes_color = val;
  8133. break;
  8134. case LOOK_SHIELD:
  8135. sd->status.shield = val;
  8136. break;
  8137. case LOOK_SHOES:
  8138. break;
  8139. case LOOK_ROBE:
  8140. sd->status.robe = val;
  8141. sd->setlook_robe = val;
  8142. break;
  8143. case LOOK_BODY2:
  8144. val = cap_value(val, MIN_BODY_STYLE, MAX_BODY_STYLE);
  8145. sd->status.body = val;
  8146. break;
  8147. }
  8148. clif_changelook(&sd->bl, type, val);
  8149. }
  8150. /*==========================================
  8151. * Give an option (type) to player (sd) and display it to client
  8152. *------------------------------------------*/
  8153. void pc_setoption(struct map_session_data *sd,int type)
  8154. {
  8155. int p_type, new_look=0;
  8156. nullpo_retv(sd);
  8157. p_type = sd->sc.option;
  8158. //Option has to be changed client-side before the class sprite or it won't always work (eg: Wedding sprite) [Skotlex]
  8159. sd->sc.option=type;
  8160. clif_changeoption(&sd->bl);
  8161. if( (type&OPTION_RIDING && !(p_type&OPTION_RIDING)) || (type&OPTION_DRAGON && !(p_type&OPTION_DRAGON) && pc_checkskill(sd,RK_DRAGONTRAINING) > 0) )
  8162. { // Mounting
  8163. clif_status_load(&sd->bl,EFST_RIDING,1);
  8164. status_calc_pc(sd,SCO_NONE);
  8165. }
  8166. else if( (!(type&OPTION_RIDING) && p_type&OPTION_RIDING) || (!(type&OPTION_DRAGON) && p_type&OPTION_DRAGON && pc_checkskill(sd,RK_DRAGONTRAINING) > 0) )
  8167. { // Dismount
  8168. clif_status_load(&sd->bl,EFST_RIDING,0);
  8169. status_calc_pc(sd,SCO_NONE);
  8170. }
  8171. #ifndef NEW_CARTS
  8172. if( type&OPTION_CART && !( p_type&OPTION_CART ) ) { //Cart On
  8173. clif_cartlist(sd);
  8174. clif_updatestatus(sd, SP_CARTINFO);
  8175. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  8176. status_calc_pc(sd,SCO_NONE); //Apply speed penalty.
  8177. } else if( !( type&OPTION_CART ) && p_type&OPTION_CART ){ //Cart Off
  8178. clif_clearcart(sd->fd);
  8179. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  8180. status_calc_pc(sd,SCO_NONE); //Remove speed penalty.
  8181. }
  8182. #endif
  8183. if (type&OPTION_FALCON && !(p_type&OPTION_FALCON)) //Falcon ON
  8184. clif_status_load(&sd->bl,EFST_FALCON,1);
  8185. else if (!(type&OPTION_FALCON) && p_type&OPTION_FALCON) //Falcon OFF
  8186. clif_status_load(&sd->bl,EFST_FALCON,0);
  8187. if( (sd->class_&MAPID_THIRDMASK) == MAPID_RANGER ) {
  8188. if( type&OPTION_WUGRIDER && !(p_type&OPTION_WUGRIDER) ) { // Mounting
  8189. clif_status_load(&sd->bl,EFST_WUGRIDER,1);
  8190. status_calc_pc(sd,SCO_NONE);
  8191. } else if( !(type&OPTION_WUGRIDER) && p_type&OPTION_WUGRIDER ) { // Dismount
  8192. clif_status_load(&sd->bl,EFST_WUGRIDER,0);
  8193. status_calc_pc(sd,SCO_NONE);
  8194. }
  8195. }
  8196. if( (sd->class_&MAPID_THIRDMASK) == MAPID_MECHANIC ) {
  8197. if( type&OPTION_MADOGEAR && !(p_type&OPTION_MADOGEAR) ) {
  8198. static const sc_type statuses [] = { SC_MAXIMIZEPOWER, SC_OVERTHRUST, SC_WEAPONPERFECTION, SC_ADRENALINE, SC_CARTBOOST, SC_MELTDOWN, SC_MAXOVERTHRUST };
  8199. status_calc_pc(sd,SCO_NONE);
  8200. for (uint8 i = 0; i < ARRAYLENGTH(statuses); i++) {
  8201. int skill_id = status_sc2skill(statuses[i]);
  8202. if (skill_id > 0 && !skill_get_inf2(skill_id, INF2_ALLOWONMADO))
  8203. status_change_end(&sd->bl,statuses[i],INVALID_TIMER);
  8204. }
  8205. pc_bonus_script_clear(sd,BSF_REM_ON_MADOGEAR);
  8206. } else if( !(type&OPTION_MADOGEAR) && p_type&OPTION_MADOGEAR ) {
  8207. status_calc_pc(sd,SCO_NONE);
  8208. status_change_end(&sd->bl,SC_SHAPESHIFT,INVALID_TIMER);
  8209. status_change_end(&sd->bl,SC_HOVERING,INVALID_TIMER);
  8210. status_change_end(&sd->bl,SC_ACCELERATION,INVALID_TIMER);
  8211. status_change_end(&sd->bl,SC_OVERHEAT_LIMITPOINT,INVALID_TIMER);
  8212. status_change_end(&sd->bl,SC_OVERHEAT,INVALID_TIMER);
  8213. status_change_end(&sd->bl,SC_MAGNETICFIELD,INVALID_TIMER);
  8214. status_change_end(&sd->bl,SC_NEUTRALBARRIER_MASTER,INVALID_TIMER);
  8215. status_change_end(&sd->bl,SC_STEALTHFIELD_MASTER,INVALID_TIMER);
  8216. pc_bonus_script_clear(sd,BSF_REM_ON_MADOGEAR);
  8217. }
  8218. }
  8219. if (type&OPTION_FLYING && !(p_type&OPTION_FLYING))
  8220. new_look = JOB_STAR_GLADIATOR2;
  8221. else if (!(type&OPTION_FLYING) && p_type&OPTION_FLYING)
  8222. new_look = -1;
  8223. if (sd->disguise || !new_look)
  8224. return; //Disguises break sprite changes
  8225. if (new_look < 0) { //Restore normal look.
  8226. status_set_viewdata(&sd->bl, sd->status.class_);
  8227. new_look = sd->vd.class_;
  8228. }
  8229. pc_stop_attack(sd); //Stop attacking on new view change (to prevent wedding/santa attacks.
  8230. clif_changelook(&sd->bl,LOOK_BASE,new_look);
  8231. if (sd->vd.cloth_color)
  8232. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  8233. if( sd->vd.body_style )
  8234. clif_changelook(&sd->bl,LOOK_BODY2,sd->vd.body_style);
  8235. clif_skillinfoblock(sd); // Skill list needs to be updated after base change.
  8236. }
  8237. /**
  8238. * Give player a cart
  8239. * @param sd Player
  8240. * @param type 0:Remove cart, 1 ~ MAX_CARTS: Cart type
  8241. **/
  8242. bool pc_setcart(struct map_session_data *sd,int type) {
  8243. #ifndef NEW_CARTS
  8244. int cart[6] = {0x0000,OPTION_CART1,OPTION_CART2,OPTION_CART3,OPTION_CART4,OPTION_CART5};
  8245. int option;
  8246. #endif
  8247. nullpo_retr(false,sd);
  8248. if( type < 0 || type > MAX_CARTS )
  8249. return false;// Never trust the values sent by the client! [Skotlex]
  8250. if( pc_checkskill(sd,MC_PUSHCART) <= 0 && type != 0 )
  8251. return false;// Push cart is required
  8252. #ifdef NEW_CARTS
  8253. switch( type ) {
  8254. case 0:
  8255. if( !sd->sc.data[SC_PUSH_CART] )
  8256. return 0;
  8257. status_change_end(&sd->bl,SC_PUSH_CART,INVALID_TIMER);
  8258. clif_clearcart(sd->fd);
  8259. break;
  8260. default:/* everything else is an allowed ID so we can move on */
  8261. if( !sd->sc.data[SC_PUSH_CART] ) { /* first time, so fill cart data */
  8262. clif_cartlist(sd);
  8263. status_calc_cart_weight(sd, (e_status_calc_weight_opt)(CALCWT_ITEM|CALCWT_MAXBONUS|CALCWT_CARTSTATE));
  8264. }
  8265. clif_updatestatus(sd, SP_CARTINFO);
  8266. sc_start(&sd->bl, &sd->bl, SC_PUSH_CART, 100, type, 0);
  8267. break;
  8268. }
  8269. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  8270. status_calc_pc(sd,SCO_NONE); //Recalc speed penalty.
  8271. #else
  8272. // Update option
  8273. option = sd->sc.option;
  8274. option &= ~OPTION_CART;// clear cart bits
  8275. option |= cart[type]; // set cart
  8276. pc_setoption(sd, option);
  8277. #endif
  8278. return true;
  8279. }
  8280. /*==========================================
  8281. * Give player a falcon
  8282. *------------------------------------------*/
  8283. void pc_setfalcon(struct map_session_data* sd, int flag)
  8284. {
  8285. if( flag ){
  8286. if( pc_checkskill(sd,HT_FALCON)>0 ) // add falcon if he have the skill
  8287. pc_setoption(sd,sd->sc.option|OPTION_FALCON);
  8288. } else if( pc_isfalcon(sd) ){
  8289. pc_setoption(sd,sd->sc.option&~OPTION_FALCON); // remove falcon
  8290. }
  8291. }
  8292. /*==========================================
  8293. * Set player riding
  8294. *------------------------------------------*/
  8295. void pc_setriding(struct map_session_data* sd, int flag)
  8296. {
  8297. if( sd->sc.data[SC_ALL_RIDING] )
  8298. return;
  8299. if( flag ){
  8300. if( pc_checkskill(sd,KN_RIDING) > 0 ) // add peco
  8301. pc_setoption(sd, sd->sc.option|OPTION_RIDING);
  8302. } else if( pc_isriding(sd) ){
  8303. pc_setoption(sd, sd->sc.option&~OPTION_RIDING);
  8304. }
  8305. }
  8306. /*==========================================
  8307. * Give player a mado
  8308. *------------------------------------------*/
  8309. void pc_setmadogear(struct map_session_data* sd, int flag)
  8310. {
  8311. if( flag ){
  8312. if( pc_checkskill(sd,NC_MADOLICENCE) > 0 )
  8313. pc_setoption(sd, sd->sc.option|OPTION_MADOGEAR);
  8314. } else if( pc_ismadogear(sd) ){
  8315. pc_setoption(sd, sd->sc.option&~OPTION_MADOGEAR);
  8316. }
  8317. }
  8318. /*==========================================
  8319. * Check if player can drop an item
  8320. *------------------------------------------*/
  8321. bool pc_candrop(struct map_session_data *sd, struct item *item)
  8322. {
  8323. if( item && ((item->expire_time || (item->bound && !pc_can_give_bounded_items(sd))) || (itemdb_ishatched_egg(item))) )
  8324. return false;
  8325. if( !pc_can_give_items(sd) || sd->sc.cant.drop) //check if this GM level can drop items
  8326. return false;
  8327. return (itemdb_isdropable(item, pc_get_group_level(sd)));
  8328. }
  8329. /**
  8330. * Determines whether a player can attack based on status changes
  8331. * Why not use status_check_skilluse?
  8332. * "src MAY be null to indicate we shouldn't check it, this is a ground-based skill attack."
  8333. * Even ground-based attacks should be blocked by these statuses
  8334. * Called from unit_attack and unit_attack_timer_sub
  8335. * @retval true Can attack
  8336. **/
  8337. bool pc_can_attack( struct map_session_data *sd, int target_id ) {
  8338. nullpo_retr(false, sd);
  8339. if( pc_is90overweight(sd) || pc_isridingwug(sd) )
  8340. return false;
  8341. if (sd->state.block_action & PCBLOCK_ATTACK)
  8342. return false;
  8343. if( sd->sc.data[SC_BASILICA] ||
  8344. sd->sc.data[SC__SHADOWFORM] ||
  8345. sd->sc.data[SC_CURSEDCIRCLE_ATKER] ||
  8346. sd->sc.data[SC_CURSEDCIRCLE_TARGET] ||
  8347. sd->sc.data[SC_CRYSTALIZE] ||
  8348. sd->sc.data[SC_ALL_RIDING] || // The client doesn't let you, this is to make cheat-safe
  8349. sd->sc.data[SC_TRICKDEAD] ||
  8350. (sd->sc.data[SC_VOICEOFSIREN] && sd->sc.data[SC_VOICEOFSIREN]->val2 == target_id) ||
  8351. sd->sc.data[SC_BLADESTOP] ||
  8352. sd->sc.data[SC_DEEPSLEEP] ||
  8353. (sd->sc.data[SC_GRAVITATION] && sd->sc.data[SC_GRAVITATION]->val3 == BCT_SELF) ||
  8354. sd->sc.data[SC_KINGS_GRACE] )
  8355. return false;
  8356. return true;
  8357. }
  8358. /*==========================================
  8359. * Read '@type' variables (temporary numeric char reg)
  8360. *------------------------------------------*/
  8361. int64 pc_readreg(struct map_session_data* sd, int64 reg)
  8362. {
  8363. return i64db_i64get(sd->regs.vars, reg);
  8364. }
  8365. /*==========================================
  8366. * Set '@type' variables (temporary numeric char reg)
  8367. *------------------------------------------*/
  8368. bool pc_setreg(struct map_session_data* sd, int64 reg, int64 val)
  8369. {
  8370. uint32 index = script_getvaridx(reg);
  8371. nullpo_retr(false, sd);
  8372. if( val ) {
  8373. i64db_i64put(sd->regs.vars, reg, val);
  8374. if( index )
  8375. script_array_update(&sd->regs, reg, false);
  8376. } else {
  8377. i64db_remove(sd->regs.vars, reg);
  8378. if( index )
  8379. script_array_update(&sd->regs, reg, true);
  8380. }
  8381. return true;
  8382. }
  8383. /*==========================================
  8384. * Read '@type$' variables (temporary string char reg)
  8385. *------------------------------------------*/
  8386. char* pc_readregstr(struct map_session_data* sd, int64 reg)
  8387. {
  8388. struct script_reg_str *p = NULL;
  8389. p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg);
  8390. return p ? p->value : NULL;
  8391. }
  8392. /*==========================================
  8393. * Set '@type$' variables (temporary string char reg)
  8394. *------------------------------------------*/
  8395. bool pc_setregstr(struct map_session_data* sd, int64 reg, const char* str)
  8396. {
  8397. struct script_reg_str *p = NULL;
  8398. unsigned int index = script_getvaridx(reg);
  8399. DBData prev;
  8400. nullpo_retr(false, sd);
  8401. if( str[0] ) {
  8402. p = ers_alloc(str_reg_ers, struct script_reg_str);
  8403. p->value = aStrdup(str);
  8404. p->flag.type = 1;
  8405. if (sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev)) {
  8406. p = (struct script_reg_str *)db_data2ptr(&prev);
  8407. if( p->value )
  8408. aFree(p->value);
  8409. ers_free(str_reg_ers, p);
  8410. } else {
  8411. if( index )
  8412. script_array_update(&sd->regs, reg, false);
  8413. }
  8414. } else {
  8415. if (sd->regs.vars->remove(sd->regs.vars, db_i642key(reg), &prev)) {
  8416. p = (struct script_reg_str *)db_data2ptr(&prev);
  8417. if( p->value )
  8418. aFree(p->value);
  8419. ers_free(str_reg_ers, p);
  8420. if( index )
  8421. script_array_update(&sd->regs, reg, true);
  8422. }
  8423. }
  8424. return true;
  8425. }
  8426. /**
  8427. * Serves the following variable types:
  8428. * - 'type' (permanent numeric char reg)
  8429. * - '#type' (permanent numeric account reg)
  8430. * - '##type' (permanent numeric account reg2)
  8431. **/
  8432. int64 pc_readregistry(struct map_session_data *sd, int64 reg)
  8433. {
  8434. struct script_reg_num *p = NULL;
  8435. if (!sd->vars_ok) {
  8436. ShowError("pc_readregistry: Trying to read reg %s before it's been loaded!\n", get_str(script_getvarid(reg)));
  8437. //This really shouldn't happen, so it's possible the data was lost somewhere, we should request it again.
  8438. //intif->request_registry(sd,type==3?4:type);
  8439. set_eof(sd->fd);
  8440. return 0;
  8441. }
  8442. p = (struct script_reg_num *)i64db_get(sd->regs.vars, reg);
  8443. return p ? p->value : 0;
  8444. }
  8445. /**
  8446. * Serves the following variable types:
  8447. * - 'type$' (permanent str char reg)
  8448. * - '#type$' (permanent str account reg)
  8449. * - '##type$' (permanent str account reg2)
  8450. **/
  8451. char* pc_readregistry_str(struct map_session_data *sd, int64 reg)
  8452. {
  8453. struct script_reg_str *p = NULL;
  8454. if (!sd->vars_ok) {
  8455. ShowError("pc_readregistry_str: Trying to read reg %s before it's been loaded!\n", get_str(script_getvarid(reg)));
  8456. //This really shouldn't happen, so it's possible the data was lost somewhere, we should request it again.
  8457. //intif->request_registry(sd,type==3?4:type);
  8458. set_eof(sd->fd);
  8459. return NULL;
  8460. }
  8461. p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg);
  8462. return p ? p->value : NULL;
  8463. }
  8464. /**
  8465. * Serves the following variable types:
  8466. * - 'type' (permanent numeric char reg)
  8467. * - '#type' (permanent numeric account reg)
  8468. * - '##type' (permanent numeric account reg2)
  8469. **/
  8470. bool pc_setregistry(struct map_session_data *sd, int64 reg, int64 val)
  8471. {
  8472. struct script_reg_num *p = NULL;
  8473. const char *regname = get_str(script_getvarid(reg));
  8474. uint32 index = script_getvaridx(reg);
  8475. if ( !reg_load && !sd->vars_ok ) {
  8476. ShowError("pc_setregistry : refusing to set %s until vars are received.\n", regname);
  8477. return false;
  8478. }
  8479. if ((p = (struct script_reg_num *)i64db_get(sd->regs.vars, reg))) {
  8480. if( val ) {
  8481. if( !p->value && index ) /* its a entry that was deleted, so we reset array */
  8482. script_array_update(&sd->regs, reg, false);
  8483. p->value = val;
  8484. } else {
  8485. p->value = 0;
  8486. if( index )
  8487. script_array_update(&sd->regs, reg, true);
  8488. }
  8489. if (!reg_load)
  8490. p->flag.update = 1;/* either way, it will require either delete or replace */
  8491. } else if( val ) {
  8492. DBData prev;
  8493. if( index )
  8494. script_array_update(&sd->regs, reg, false);
  8495. p = ers_alloc(num_reg_ers, struct script_reg_num);
  8496. p->value = val;
  8497. if (!reg_load)
  8498. p->flag.update = 1;
  8499. if (sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev)) {
  8500. p = (struct script_reg_num *)db_data2ptr(&prev);
  8501. ers_free(num_reg_ers, p);
  8502. }
  8503. }
  8504. if (!reg_load && p)
  8505. sd->vars_dirty = true;
  8506. return true;
  8507. }
  8508. /**
  8509. * Serves the following variable types:
  8510. * - 'type$' (permanent str char reg)
  8511. * - '#type$' (permanent str account reg)
  8512. * - '##type$' (permanent str account reg2)
  8513. **/
  8514. bool pc_setregistry_str(struct map_session_data *sd, int64 reg, const char *val)
  8515. {
  8516. struct script_reg_str *p = NULL;
  8517. const char *regname = get_str(script_getvarid(reg));
  8518. unsigned int index = script_getvaridx(reg);
  8519. size_t vlen = 0;
  8520. if (!reg_load && !sd->vars_ok) {
  8521. ShowError("pc_setregistry_str : refusing to set %s until vars are received.\n", regname);
  8522. return false;
  8523. }
  8524. if ( !script_check_RegistryVariableLength(1, val, &vlen ) )
  8525. {
  8526. 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);
  8527. return false;
  8528. }
  8529. if( (p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg) ) ) {
  8530. if( val[0] ) {
  8531. if( p->value )
  8532. aFree(p->value);
  8533. else if ( index ) // an entry that was deleted, so we reset
  8534. script_array_update(&sd->regs, reg, false);
  8535. p->value = aStrdup(val);
  8536. } else {
  8537. if (p->value)
  8538. aFree(p->value);
  8539. p->value = NULL;
  8540. if( index )
  8541. script_array_update(&sd->regs, reg, true);
  8542. }
  8543. if( !reg_load )
  8544. p->flag.update = 1; // either way, it will require either delete or replace
  8545. } else if( val[0] ) {
  8546. DBData prev;
  8547. if( index )
  8548. script_array_update(&sd->regs, reg, false);
  8549. p = ers_alloc(str_reg_ers, struct script_reg_str);
  8550. p->value = aStrdup(val);
  8551. if( !reg_load )
  8552. p->flag.update = 1;
  8553. p->flag.type = 1;
  8554. if( sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev) ) {
  8555. p = (struct script_reg_str *)db_data2ptr(&prev);
  8556. if( p->value )
  8557. aFree(p->value);
  8558. ers_free(str_reg_ers, p);
  8559. }
  8560. }
  8561. if( !reg_load && p )
  8562. sd->vars_dirty = true;
  8563. return true;
  8564. }
  8565. /**
  8566. * Set value of player variable
  8567. * @param sd Player
  8568. * @param reg Variable name
  8569. * @param value
  8570. * @return True if success, false if failed.
  8571. **/
  8572. bool pc_setreg2(struct map_session_data *sd, const char *reg, int64 val) {
  8573. char prefix = reg[0];
  8574. nullpo_retr(false, sd);
  8575. if (reg[strlen(reg)-1] == '$') {
  8576. ShowError("pc_setreg2: Invalid variable scope '%s'\n", reg);
  8577. return false;
  8578. }
  8579. val = cap_value(val, INT_MIN, INT_MAX);
  8580. switch (prefix) {
  8581. case '.':
  8582. case '\'':
  8583. case '$':
  8584. ShowError("pc_setreg2: Invalid variable scope '%s'\n", reg);
  8585. return false;
  8586. case '@':
  8587. return pc_setreg(sd, add_str(reg), val);
  8588. case '#':
  8589. return (reg[1] == '#') ? pc_setaccountreg2(sd, add_str(reg), val) : pc_setaccountreg(sd, add_str(reg), val);
  8590. default:
  8591. return pc_setglobalreg(sd, add_str(reg), val);
  8592. }
  8593. return false;
  8594. }
  8595. /**
  8596. * Get value of player variable
  8597. * @param sd Player
  8598. * @param reg Variable name
  8599. * @return Variable value or 0 if failed.
  8600. **/
  8601. int64 pc_readreg2(struct map_session_data *sd, const char *reg) {
  8602. char prefix = reg[0];
  8603. nullpo_ret(sd);
  8604. if (reg[strlen(reg)-1] == '$') {
  8605. ShowError("pc_readreg2: Invalid variable scope '%s'\n", reg);
  8606. return 0;
  8607. }
  8608. switch (prefix) {
  8609. case '.':
  8610. case '\'':
  8611. case '$':
  8612. ShowError("pc_readreg2: Invalid variable scope '%s'\n", reg);
  8613. return 0;
  8614. case '@':
  8615. return pc_readreg(sd, add_str(reg));
  8616. case '#':
  8617. return (reg[1] == '#') ? pc_readaccountreg2(sd, add_str(reg)) : pc_readaccountreg(sd, add_str(reg));
  8618. default:
  8619. return pc_readglobalreg(sd, add_str(reg));
  8620. }
  8621. return 0;
  8622. }
  8623. /*==========================================
  8624. * Exec eventtimer for player sd (retrieved from map_session (id))
  8625. *------------------------------------------*/
  8626. static TIMER_FUNC(pc_eventtimer){
  8627. struct map_session_data *sd=map_id2sd(id);
  8628. char *p = (char *)data;
  8629. int i;
  8630. if(sd==NULL)
  8631. return 0;
  8632. ARR_FIND( 0, MAX_EVENTTIMER, i, sd->eventtimer[i] == tid );
  8633. if( i < MAX_EVENTTIMER )
  8634. {
  8635. sd->eventtimer[i] = INVALID_TIMER;
  8636. sd->eventcount--;
  8637. npc_event(sd,p,0);
  8638. }
  8639. else
  8640. ShowError("pc_eventtimer: no such event timer\n");
  8641. if (p) aFree(p);
  8642. return 0;
  8643. }
  8644. /*==========================================
  8645. * Add eventtimer for player sd ?
  8646. *------------------------------------------*/
  8647. bool pc_addeventtimer(struct map_session_data *sd,int tick,const char *name)
  8648. {
  8649. int i;
  8650. nullpo_retr(false,sd);
  8651. ARR_FIND( 0, MAX_EVENTTIMER, i, sd->eventtimer[i] == INVALID_TIMER );
  8652. if( i == MAX_EVENTTIMER )
  8653. return false;
  8654. sd->eventtimer[i] = add_timer(gettick()+tick, pc_eventtimer, sd->bl.id, (intptr_t)aStrdup(name));
  8655. sd->eventcount++;
  8656. return true;
  8657. }
  8658. /*==========================================
  8659. * Del eventtimer for player sd ?
  8660. *------------------------------------------*/
  8661. bool pc_deleventtimer(struct map_session_data *sd,const char *name)
  8662. {
  8663. char* p = NULL;
  8664. int i;
  8665. nullpo_retr(false,sd);
  8666. if (sd->eventcount == 0)
  8667. return false;
  8668. // find the named event timer
  8669. ARR_FIND( 0, MAX_EVENTTIMER, i,
  8670. sd->eventtimer[i] != INVALID_TIMER &&
  8671. (p = (char *)(get_timer(sd->eventtimer[i])->data)) != NULL &&
  8672. strcmp(p, name) == 0
  8673. );
  8674. if( i == MAX_EVENTTIMER )
  8675. return false; // not found
  8676. delete_timer(sd->eventtimer[i],pc_eventtimer);
  8677. sd->eventtimer[i] = INVALID_TIMER;
  8678. if(sd->eventcount > 0)
  8679. sd->eventcount--;
  8680. aFree(p);
  8681. return true;
  8682. }
  8683. /*==========================================
  8684. * Update eventtimer count for player sd
  8685. *------------------------------------------*/
  8686. void pc_addeventtimercount(struct map_session_data *sd,const char *name,int tick)
  8687. {
  8688. int i;
  8689. nullpo_retv(sd);
  8690. for(i=0;i<MAX_EVENTTIMER;i++)
  8691. if( sd->eventtimer[i] != INVALID_TIMER && strcmp(
  8692. (char *)(get_timer(sd->eventtimer[i])->data), name)==0 ){
  8693. addt_tickimer(sd->eventtimer[i],tick);
  8694. break;
  8695. }
  8696. }
  8697. /*==========================================
  8698. * Remove all eventtimer for player sd
  8699. *------------------------------------------*/
  8700. void pc_cleareventtimer(struct map_session_data *sd)
  8701. {
  8702. int i;
  8703. nullpo_retv(sd);
  8704. if (sd->eventcount == 0)
  8705. return;
  8706. for(i=0;i<MAX_EVENTTIMER;i++){
  8707. if( sd->eventtimer[i] != INVALID_TIMER ){
  8708. char *p = (char *)(get_timer(sd->eventtimer[i])->data);
  8709. delete_timer(sd->eventtimer[i],pc_eventtimer);
  8710. sd->eventtimer[i] = INVALID_TIMER;
  8711. if(sd->eventcount > 0) //avoid looping to max val
  8712. sd->eventcount--;
  8713. if (p) aFree(p);
  8714. }
  8715. }
  8716. }
  8717. /**
  8718. * Called when an item with combo is worn
  8719. * @param *sd
  8720. * @param *data struct item_data
  8721. * @return success numbers of succeed combo
  8722. */
  8723. static int pc_checkcombo(struct map_session_data *sd, struct item_data *data) {
  8724. uint16 i;
  8725. int success = 0;
  8726. for( i = 0; i < data->combos_count; i++ ) {
  8727. struct itemchk {
  8728. int idx;
  8729. unsigned short nameid;
  8730. short card[MAX_SLOTS];
  8731. } *combo_idx;
  8732. int idx, j;
  8733. int nb_itemCombo;
  8734. unsigned int pos = 0;
  8735. /* ensure this isn't a duplicate combo */
  8736. if( sd->combos.bonus != NULL ) {
  8737. int x;
  8738. ARR_FIND( 0, sd->combos.count, x, sd->combos.id[x] == data->combos[i]->id );
  8739. /* found a match, skip this combo */
  8740. if( x < sd->combos.count )
  8741. continue;
  8742. }
  8743. nb_itemCombo = data->combos[i]->count;
  8744. if(nb_itemCombo<2) //a combo with less then 2 item ?? how that possible
  8745. continue;
  8746. CREATE(combo_idx,struct itemchk,nb_itemCombo);
  8747. for(j=0; j < nb_itemCombo; j++){
  8748. combo_idx[j].idx=-1;
  8749. combo_idx[j].nameid=-1;
  8750. memset(combo_idx[j].card,-1,MAX_SLOTS);
  8751. }
  8752. for( j = 0; j < nb_itemCombo; j++ ) {
  8753. uint16 id = data->combos[i]->nameid[j], k;
  8754. bool found = false;
  8755. for( k = 0; k < EQI_MAX; k++ ) {
  8756. short index = sd->equip_index[k];
  8757. if( index < 0 )
  8758. continue;
  8759. if( pc_is_same_equip_index((enum equip_index)k, sd->equip_index, index) )
  8760. continue;
  8761. if (!sd->inventory_data[index] )
  8762. continue;
  8763. if ( itemdb_type(id) != IT_CARD ) {
  8764. if ( sd->inventory_data[index]->nameid != id )
  8765. continue;
  8766. if(j>0){ //check if this item not already used
  8767. bool do_continue = false; //used to continue that specific loop with some check that also use some loop
  8768. uint8 z;
  8769. for (z = 0; z < nb_itemCombo-1; z++)
  8770. if(combo_idx[z].idx == index && combo_idx[z].nameid == id) //we already have that index recorded
  8771. do_continue=true;
  8772. if(do_continue)
  8773. continue;
  8774. }
  8775. combo_idx[j].nameid = id;
  8776. combo_idx[j].idx = index;
  8777. pos |= sd->inventory.u.items_inventory[index].equip;
  8778. found = true;
  8779. break;
  8780. } else { //Cards and enchants
  8781. uint16 z;
  8782. if ( itemdb_isspecial(sd->inventory.u.items_inventory[index].card[0]) )
  8783. continue;
  8784. for (z = 0; z < MAX_SLOTS; z++) {
  8785. bool do_continue=false;
  8786. if (sd->inventory.u.items_inventory[index].card[z] != id)
  8787. continue;
  8788. if(j>0){
  8789. int c1, c2;
  8790. for (c1 = 0; c1 < nb_itemCombo-1; c1++){
  8791. if(combo_idx[c1].idx == index && combo_idx[c1].nameid == id){
  8792. for (c2 = 0; c2 < MAX_SLOTS; c2++){
  8793. if(combo_idx[c1].card[c2] == id){ //we already have that card recorded (at this same idx)
  8794. do_continue = true;
  8795. break;
  8796. }
  8797. }
  8798. }
  8799. }
  8800. }
  8801. if(do_continue)
  8802. continue;
  8803. combo_idx[j].nameid = id;
  8804. combo_idx[j].idx = index;
  8805. combo_idx[j].card[z] = id;
  8806. pos |= sd->inventory.u.items_inventory[index].equip;
  8807. found = true;
  8808. break;
  8809. }
  8810. }
  8811. }
  8812. if( !found )
  8813. break;/* we haven't found all the ids for this combo, so we can return */
  8814. }
  8815. aFree(combo_idx);
  8816. /* means we broke out of the count loop w/o finding all ids, we can move to the next combo */
  8817. if( j < nb_itemCombo )
  8818. continue;
  8819. /* we got here, means all items in the combo are matching */
  8820. idx = sd->combos.count;
  8821. if( sd->combos.bonus == NULL ) {
  8822. CREATE(sd->combos.bonus, struct script_code *, 1);
  8823. CREATE(sd->combos.id, unsigned short, 1);
  8824. CREATE(sd->combos.pos, unsigned int, 1);
  8825. sd->combos.count = 1;
  8826. } else {
  8827. RECREATE(sd->combos.bonus, struct script_code *, ++sd->combos.count);
  8828. RECREATE(sd->combos.id, unsigned short, sd->combos.count);
  8829. RECREATE(sd->combos.pos, unsigned int, sd->combos.count);
  8830. }
  8831. /* we simply copy the pointer */
  8832. sd->combos.bonus[idx] = data->combos[i]->script;
  8833. /* save this combo's id */
  8834. sd->combos.id[idx] = data->combos[i]->id;
  8835. /* save pos of combo*/
  8836. sd->combos.pos[idx] = pos;
  8837. success++;
  8838. }
  8839. return success;
  8840. }
  8841. /**
  8842. * Called when an item with combo is removed
  8843. * @param *sd
  8844. * @param *data struct item_data
  8845. * @return retval numbers of removed combo
  8846. */
  8847. static int pc_removecombo(struct map_session_data *sd, struct item_data *data ) {
  8848. int i, retval = 0;
  8849. if( sd->combos.bonus == NULL )
  8850. return 0;/* nothing to do here, player has no combos */
  8851. for( i = 0; i < data->combos_count; i++ ) {
  8852. /* check if this combo exists in this user */
  8853. int x = 0, cursor = 0, j;
  8854. ARR_FIND( 0, sd->combos.count, x, sd->combos.id[x] == data->combos[i]->id );
  8855. /* no match, skip this combo */
  8856. if(x >= sd->combos.count)
  8857. continue;
  8858. sd->combos.bonus[x] = NULL;
  8859. sd->combos.id[x] = 0;
  8860. sd->combos.pos[x] = 0;
  8861. retval++;
  8862. /* check if combo requirements still fit */
  8863. if( pc_checkcombo( sd, data ) )
  8864. continue;
  8865. /* move next value to empty slot */
  8866. for( j = 0, cursor = 0; j < sd->combos.count; j++ ) {
  8867. if( sd->combos.bonus[j] == NULL )
  8868. continue;
  8869. if( cursor != j ) {
  8870. sd->combos.bonus[cursor] = sd->combos.bonus[j];
  8871. sd->combos.id[cursor] = sd->combos.id[j];
  8872. sd->combos.pos[cursor] = sd->combos.pos[j];
  8873. }
  8874. cursor++;
  8875. }
  8876. /* it's empty, we can clear all the memory */
  8877. if( (sd->combos.count = cursor) == 0 ) {
  8878. aFree(sd->combos.bonus);
  8879. aFree(sd->combos.id);
  8880. aFree(sd->combos.pos);
  8881. sd->combos.bonus = NULL;
  8882. sd->combos.id = NULL;
  8883. sd->combos.pos = NULL;
  8884. return retval; /* we also can return at this point for we have no more combos to check */
  8885. }
  8886. }
  8887. return retval;
  8888. }
  8889. /**
  8890. * Load combo data(s) of player
  8891. * @param *sd
  8892. * @return ret numbers of succeed combo
  8893. */
  8894. int pc_load_combo(struct map_session_data *sd) {
  8895. int i, ret = 0;
  8896. for( i = 0; i < EQI_MAX; i++ ) {
  8897. struct item_data *id = NULL;
  8898. short idx = sd->equip_index[i];
  8899. if( idx < 0 || !(id = sd->inventory_data[idx] ) )
  8900. continue;
  8901. if( id->combos_count )
  8902. ret += pc_checkcombo(sd,id);
  8903. if(!itemdb_isspecial(sd->inventory.u.items_inventory[idx].card[0])) {
  8904. struct item_data *data;
  8905. int j;
  8906. for( j = 0; j < MAX_SLOTS; j++ ) {
  8907. if (!sd->inventory.u.items_inventory[idx].card[j])
  8908. continue;
  8909. if ( ( data = itemdb_exists(sd->inventory.u.items_inventory[idx].card[j]) ) != NULL ) {
  8910. if( data->combos_count )
  8911. ret += pc_checkcombo(sd,data);
  8912. }
  8913. }
  8914. }
  8915. }
  8916. return ret;
  8917. }
  8918. /*==========================================
  8919. * Equip item on player sd at req_pos from inventory index n
  8920. * return: false - fail; true - success
  8921. *------------------------------------------*/
  8922. bool pc_equipitem(struct map_session_data *sd,short n,int req_pos,bool equipswitch)
  8923. {
  8924. int i, pos, flag = 0, iflag;
  8925. struct item_data *id;
  8926. uint8 res = ITEM_EQUIP_ACK_OK;
  8927. short* equip_index;
  8928. nullpo_retr(false,sd);
  8929. if( n < 0 || n >= MAX_INVENTORY ) {
  8930. if( equipswitch ){
  8931. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  8932. }else{
  8933. clif_equipitemack(sd,0,0,ITEM_EQUIP_ACK_FAIL);
  8934. }
  8935. return false;
  8936. }
  8937. if( DIFF_TICK(sd->canequip_tick,gettick()) > 0 ) {
  8938. if( equipswitch ){
  8939. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  8940. }else{
  8941. clif_equipitemack(sd,n,0,ITEM_EQUIP_ACK_FAIL);
  8942. }
  8943. return false;
  8944. }
  8945. if (!(id = sd->inventory_data[n]))
  8946. return false;
  8947. pos = pc_equippoint(sd,n); //With a few exceptions, item should go in all specified slots.
  8948. if(battle_config.battle_log && !equipswitch)
  8949. ShowInfo("equip %hu (%d) %x:%x\n",sd->inventory.u.items_inventory[n].nameid,n,id?id->equip:0,req_pos);
  8950. if((res = pc_isequip(sd,n))) {
  8951. if( equipswitch ){
  8952. clif_equipswitch_add( sd, n, req_pos, res );
  8953. }else{
  8954. clif_equipitemack(sd,n,0,res); // fail
  8955. }
  8956. return false;
  8957. }
  8958. if( equipswitch && id->type == IT_AMMO ){
  8959. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  8960. return false;
  8961. }
  8962. if (!(pos&req_pos) || sd->inventory.u.items_inventory[n].equip != 0 || sd->inventory.u.items_inventory[n].attribute==1 ) { // [Valaris]
  8963. if( equipswitch ){
  8964. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  8965. }else{
  8966. clif_equipitemack(sd,n,0,ITEM_EQUIP_ACK_FAIL); // fail
  8967. }
  8968. return false;
  8969. }
  8970. if( sd->sc.count && (sd->sc.data[SC_BERSERK] || sd->sc.data[SC_SATURDAYNIGHTFEVER] ||
  8971. sd->sc.data[SC_KYOUGAKU] || (sd->sc.data[SC_PYROCLASTIC] && sd->inventory_data[n]->type == IT_WEAPON)) ) {
  8972. if( equipswitch ){
  8973. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  8974. }else{
  8975. clif_equipitemack(sd,n,0,ITEM_EQUIP_ACK_FAIL); //Fail
  8976. }
  8977. return false;
  8978. }
  8979. equip_index = equipswitch ? sd->equip_switch_index : sd->equip_index;
  8980. if ( !equipswitch && id->flag.bindOnEquip && !sd->inventory.u.items_inventory[n].bound) {
  8981. sd->inventory.u.items_inventory[n].bound = (char)battle_config.default_bind_on_equip;
  8982. clif_notify_bindOnEquip(sd,n);
  8983. }
  8984. if(pos == EQP_ACC) { //Accessories should only go in one of the two.
  8985. pos = req_pos&EQP_ACC;
  8986. if (pos == EQP_ACC) //User specified both slots.
  8987. pos = equip_index[EQI_ACC_R] >= 0 ? EQP_ACC_L : EQP_ACC_R;
  8988. for (i = 0; i < sd->inventory_data[n]->slot; i++) { // Accessories that have cards that force equip location
  8989. if (!sd->inventory.u.items_inventory[n].card[i])
  8990. continue;
  8991. struct item_data *card_data = itemdb_exists(sd->inventory.u.items_inventory[n].card[i]);
  8992. if (card_data) {
  8993. int card_pos = card_data->equip;
  8994. if (card_pos == EQP_ACC_L || card_pos == EQP_ACC_R) {
  8995. pos = card_pos; // Use the card's equip position
  8996. break;
  8997. }
  8998. }
  8999. }
  9000. } else if(pos == EQP_ARMS && id->equip == EQP_HAND_R) { //Dual wield capable weapon.
  9001. pos = (req_pos&EQP_ARMS);
  9002. if (pos == EQP_ARMS) //User specified both slots, pick one for them.
  9003. pos = equip_index[EQI_HAND_R] >= 0 ? EQP_HAND_L : EQP_HAND_R;
  9004. } else if(pos == EQP_SHADOW_ACC) { // Shadow System
  9005. pos = req_pos&EQP_SHADOW_ACC;
  9006. if (pos == EQP_SHADOW_ACC)
  9007. pos = equip_index[EQI_SHADOW_ACC_L] >= 0 ? EQP_SHADOW_ACC_R : EQP_SHADOW_ACC_L;
  9008. } else if(pos == EQP_SHADOW_ARMS && id->equip == EQP_SHADOW_WEAPON) {
  9009. pos = (req_pos&EQP_SHADOW_ARMS);
  9010. if( pos == EQP_SHADOW_ARMS )
  9011. pos = (equip_index[EQI_SHADOW_WEAPON] >= 0 ? EQP_SHADOW_SHIELD : EQP_SHADOW_WEAPON);
  9012. }
  9013. if (pos&EQP_HAND_R && battle_config.use_weapon_skill_range&BL_PC) {
  9014. //Update skill-block range database when weapon range changes. [Skotlex]
  9015. i = equip_index[EQI_HAND_R];
  9016. if (i < 0 || !sd->inventory_data[i]) //No data, or no weapon equipped
  9017. flag = 1;
  9018. else
  9019. flag = id->range != sd->inventory_data[i]->range;
  9020. }
  9021. if( equipswitch ){
  9022. for( i = 0; i < EQI_MAX; i++ ){
  9023. if( pos&equip_bitmask[i] ){
  9024. // If there was already an item assigned to this slot
  9025. if( sd->equip_switch_index[i] >= 0 ){
  9026. pc_equipswitch_remove( sd, sd->equip_switch_index[i] );
  9027. }
  9028. // Assign the new index to it
  9029. sd->equip_switch_index[i] = n;
  9030. }
  9031. }
  9032. sd->inventory.u.items_inventory[n].equipSwitch = pos;
  9033. clif_equipswitch_add( sd, n, pos, ITEM_EQUIP_ACK_OK );
  9034. return true;
  9035. }else{
  9036. for(i=0;i<EQI_MAX;i++) {
  9037. if(pos & equip_bitmask[i]) {
  9038. if(sd->equip_index[i] >= 0) //Slot taken, remove item from there.
  9039. pc_unequipitem(sd,sd->equip_index[i],2 | 4);
  9040. sd->equip_index[i] = n;
  9041. }
  9042. }
  9043. pc_equipswitch_remove(sd, n);
  9044. if(pos==EQP_AMMO) {
  9045. clif_arrowequip(sd,n);
  9046. clif_arrow_fail(sd,3);
  9047. }
  9048. else
  9049. clif_equipitemack(sd,n,pos,ITEM_EQUIP_ACK_OK);
  9050. sd->inventory.u.items_inventory[n].equip = pos;
  9051. }
  9052. if(pos & EQP_HAND_R) {
  9053. if(id)
  9054. sd->weapontype1 = id->look;
  9055. else
  9056. sd->weapontype1 = 0;
  9057. pc_calcweapontype(sd);
  9058. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  9059. }
  9060. if(pos & EQP_HAND_L) {
  9061. if(id) {
  9062. if(id->type == IT_WEAPON) {
  9063. sd->status.shield = 0;
  9064. sd->weapontype2 = id->look;
  9065. }
  9066. else
  9067. if(id->type == IT_ARMOR) {
  9068. sd->status.shield = id->look;
  9069. sd->weapontype2 = 0;
  9070. }
  9071. }
  9072. else
  9073. sd->status.shield = sd->weapontype2 = 0;
  9074. pc_calcweapontype(sd);
  9075. clif_changelook(&sd->bl,LOOK_SHIELD,sd->status.shield);
  9076. }
  9077. if(pos & EQP_SHOES)
  9078. clif_changelook(&sd->bl,LOOK_SHOES,0);
  9079. if (battle_config.ammo_unequip && (pos&EQP_ARMS) && id->type == IT_WEAPON) {
  9080. short idx = sd->equip_index[EQI_AMMO];
  9081. if (idx >= 0) {
  9082. switch (sd->inventory_data[idx]->look) {
  9083. case AMMO_ARROW:
  9084. if (id->look != W_BOW && id->look != W_MUSICAL && id->look != W_WHIP)
  9085. pc_unequipitem(sd, idx, 2 | 4);
  9086. break;
  9087. case AMMO_BULLET:
  9088. case AMMO_SHELL:
  9089. if (id->look != W_REVOLVER && id->look != W_RIFLE && id->look != W_GATLING && id->look != W_SHOTGUN
  9090. #ifdef RENEWAL
  9091. && id->look != W_GRENADE
  9092. #endif
  9093. )
  9094. pc_unequipitem(sd, idx, 2 | 4);
  9095. break;
  9096. #ifndef RENEWAL
  9097. case AMMO_GRENADE:
  9098. if (id->look != W_GRENADE)
  9099. pc_unequipitem(sd, idx, 2 | 4);
  9100. break;
  9101. #endif
  9102. }
  9103. }
  9104. }
  9105. pc_set_costume_view(sd);
  9106. pc_checkallowskill(sd); //Check if status changes should be halted.
  9107. iflag = sd->npc_item_flag;
  9108. /* check for combos (MUST be before status_calc_pc) */
  9109. if( id->combos_count )
  9110. pc_checkcombo(sd,id);
  9111. if(itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  9112. ; //No cards
  9113. else {
  9114. for( i = 0; i < MAX_SLOTS; i++ ) {
  9115. struct item_data *data;
  9116. if (!sd->inventory.u.items_inventory[n].card[i])
  9117. continue;
  9118. if ( ( data = itemdb_exists(sd->inventory.u.items_inventory[n].card[i]) ) != NULL ) {
  9119. if( data->combos_count )
  9120. pc_checkcombo(sd,data);
  9121. }
  9122. }
  9123. }
  9124. status_calc_pc(sd,SCO_NONE);
  9125. if (flag) //Update skill data
  9126. clif_skillinfoblock(sd);
  9127. //OnEquip script [Skotlex]
  9128. if (id) {
  9129. //only run the script if item isn't restricted
  9130. if (id->equip_script && (pc_has_permission(sd,PC_PERM_USE_ALL_EQUIPMENT) || !itemdb_isNoEquip(id,sd->bl.m)))
  9131. run_script(id->equip_script,0,sd->bl.id,fake_nd->bl.id);
  9132. if(itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  9133. ; //No cards
  9134. else {
  9135. for( i = 0; i < MAX_SLOTS; i++ ) {
  9136. struct item_data *data;
  9137. if (!sd->inventory.u.items_inventory[n].card[i])
  9138. continue;
  9139. if ( ( data = itemdb_exists(sd->inventory.u.items_inventory[n].card[i]) ) != NULL ) {
  9140. if (data->equip_script && (pc_has_permission(sd,PC_PERM_USE_ALL_EQUIPMENT) || !itemdb_isNoEquip(data,sd->bl.m)))
  9141. run_script(data->equip_script,0,sd->bl.id,fake_nd->bl.id);
  9142. }
  9143. }
  9144. }
  9145. }
  9146. sd->npc_item_flag = iflag;
  9147. return true;
  9148. }
  9149. /**
  9150. * Recalculate player status on unequip
  9151. * @param sd: Player data
  9152. * @param n: Item inventory index
  9153. * @param flag: Whether to recalculate a player's status or not
  9154. * @return True on success or false on failure
  9155. */
  9156. static void pc_unequipitem_sub(struct map_session_data *sd, int n, int flag) {
  9157. int i, iflag;
  9158. bool status_calc = false;
  9159. if (sd->state.autobonus&sd->inventory.u.items_inventory[n].equip)
  9160. sd->state.autobonus &= ~sd->inventory.u.items_inventory[n].equip; //Check for activated autobonus [Inkfish]
  9161. sd->inventory.u.items_inventory[n].equip = 0;
  9162. pc_checkallowskill(sd);
  9163. iflag = sd->npc_item_flag;
  9164. /* check for combos (MUST be before status_calc_pc) */
  9165. if (sd->inventory_data[n]) {
  9166. if (sd->inventory_data[n]->combos_count) {
  9167. if (pc_removecombo(sd, sd->inventory_data[n]))
  9168. status_calc = true;
  9169. }
  9170. if (itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  9171. ; //No cards
  9172. else {
  9173. for (i = 0; i < MAX_SLOTS; i++) {
  9174. struct item_data *data;
  9175. if (!sd->inventory.u.items_inventory[n].card[i])
  9176. continue;
  9177. if ((data = itemdb_exists(sd->inventory.u.items_inventory[n].card[i])) != NULL) {
  9178. if (data->combos_count) {
  9179. if (pc_removecombo(sd, data))
  9180. status_calc = true;
  9181. }
  9182. }
  9183. }
  9184. }
  9185. }
  9186. if (flag & 1 || status_calc) {
  9187. pc_checkallowskill(sd);
  9188. status_calc_pc(sd, SCO_NONE);
  9189. }
  9190. if (sd->sc.data[SC_SIGNUMCRUCIS] && !battle_check_undead(sd->battle_status.race, sd->battle_status.def_ele))
  9191. status_change_end(&sd->bl, SC_SIGNUMCRUCIS, INVALID_TIMER);
  9192. //OnUnEquip script [Skotlex]
  9193. if (sd->inventory_data[n]) {
  9194. if (sd->inventory_data[n]->unequip_script)
  9195. run_script(sd->inventory_data[n]->unequip_script, 0, sd->bl.id, fake_nd->bl.id);
  9196. if (itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  9197. ; //No cards
  9198. else {
  9199. for (i = 0; i < MAX_SLOTS; i++) {
  9200. struct item_data *data;
  9201. if (!sd->inventory.u.items_inventory[n].card[i])
  9202. continue;
  9203. if ((data = itemdb_exists(sd->inventory.u.items_inventory[n].card[i])) != NULL) {
  9204. if (data->unequip_script)
  9205. run_script(data->unequip_script, 0, sd->bl.id, fake_nd->bl.id);
  9206. }
  9207. }
  9208. }
  9209. }
  9210. sd->npc_item_flag = iflag;
  9211. }
  9212. /**
  9213. * Called when attempting to unequip an item from a player
  9214. * @param sd: Player data
  9215. * @param n: Item inventory index
  9216. * @param flag: Type of unequip
  9217. * 0 - only unequip
  9218. * 1 - calculate status after unequipping
  9219. * 2 - force unequip
  9220. * 4 - unequip by switching equipment
  9221. * @return True on success or false on failure
  9222. */
  9223. bool pc_unequipitem(struct map_session_data *sd, int n, int flag) {
  9224. int i, pos;
  9225. nullpo_retr(false,sd);
  9226. if (n < 0 || n >= MAX_INVENTORY) {
  9227. clif_unequipitemack(sd,0,0,0);
  9228. return false;
  9229. }
  9230. if (!(pos = sd->inventory.u.items_inventory[n].equip)) {
  9231. clif_unequipitemack(sd,n,0,0);
  9232. return false; //Nothing to unequip
  9233. }
  9234. // status change that makes player cannot unequip equipment
  9235. if (!(flag&2) && sd->sc.count &&
  9236. (sd->sc.data[SC_BERSERK] ||
  9237. sd->sc.data[SC_SATURDAYNIGHTFEVER] ||
  9238. sd->sc.data[SC__BLOODYLUST] ||
  9239. sd->sc.data[SC_KYOUGAKU] ||
  9240. (sd->sc.data[SC_PYROCLASTIC] &&
  9241. sd->inventory_data[n]->type == IT_WEAPON))) // can't switch weapon
  9242. {
  9243. clif_unequipitemack(sd,n,0,0);
  9244. return false;
  9245. }
  9246. if (battle_config.battle_log)
  9247. ShowInfo("unequip %d %x:%x\n",n,pc_equippoint(sd,n),pos);
  9248. for(i = 0; i < EQI_MAX; i++) {
  9249. if (pos & equip_bitmask[i])
  9250. sd->equip_index[i] = -1;
  9251. }
  9252. if(pos & EQP_HAND_R) {
  9253. sd->weapontype1 = 0;
  9254. sd->status.weapon = sd->weapontype2;
  9255. pc_calcweapontype(sd);
  9256. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  9257. if( !battle_config.dancing_weaponswitch_fix )
  9258. status_change_end(&sd->bl, SC_DANCING, INVALID_TIMER); // Unequipping => stop dancing.
  9259. #ifdef RENEWAL
  9260. if (battle_config.switch_remove_edp&2) {
  9261. #else
  9262. if (battle_config.switch_remove_edp&1) {
  9263. #endif
  9264. status_change_end(&sd->bl, SC_EDP, INVALID_TIMER);
  9265. }
  9266. }
  9267. if(pos & EQP_HAND_L) {
  9268. if (sd->status.shield && battle_getcurrentskill(&sd->bl) == LG_SHIELDSPELL)
  9269. unit_skillcastcancel(&sd->bl, 0); // Cancel Shield Spell if player swaps shields.
  9270. sd->status.shield = sd->weapontype2 = 0;
  9271. pc_calcweapontype(sd);
  9272. clif_changelook(&sd->bl,LOOK_SHIELD,sd->status.shield);
  9273. }
  9274. if(pos & EQP_SHOES)
  9275. clif_changelook(&sd->bl,LOOK_SHOES,0);
  9276. clif_unequipitemack(sd,n,pos,1);
  9277. pc_set_costume_view(sd);
  9278. status_change_end(&sd->bl,SC_HEAT_BARREL,INVALID_TIMER);
  9279. // On weapon change (right and left hand)
  9280. if ((pos & EQP_ARMS) && sd->inventory_data[n]->type == IT_WEAPON) {
  9281. if (battle_config.ammo_unequip && !(flag & 4)) {
  9282. switch (sd->inventory_data[n]->look) {
  9283. case W_BOW:
  9284. case W_MUSICAL:
  9285. case W_WHIP:
  9286. case W_REVOLVER:
  9287. case W_RIFLE:
  9288. case W_GATLING:
  9289. case W_SHOTGUN:
  9290. case W_GRENADE: {
  9291. short idx = sd->equip_index[EQI_AMMO];
  9292. if (idx >= 0) {
  9293. sd->equip_index[EQI_AMMO] = -1;
  9294. clif_unequipitemack(sd, idx, sd->inventory.u.items_inventory[idx].equip, 1);
  9295. pc_unequipitem_sub(sd, idx, 0);
  9296. }
  9297. }
  9298. break;
  9299. }
  9300. }
  9301. if (!sd->sc.data[SC_SEVENWIND] || sd->sc.data[SC_ASPERSIO]) //Check for seven wind (but not level seven!)
  9302. skill_enchant_elemental_end(&sd->bl, SC_NONE);
  9303. status_change_end(&sd->bl, SC_FEARBREEZE, INVALID_TIMER);
  9304. status_change_end(&sd->bl, SC_EXEEDBREAK, INVALID_TIMER);
  9305. }
  9306. // On armor change
  9307. if (pos & EQP_ARMOR) {
  9308. if (sd->sc.data[SC_HOVERING] && sd->inventory_data[n]->nameid == ITEMID_HOVERING_BOOSTER)
  9309. status_change_end(&sd->bl, SC_HOVERING, INVALID_TIMER);
  9310. //status_change_end(&sd->bl, SC_BENEDICTIO, INVALID_TIMER); // No longer is removed? Need confirmation
  9311. status_change_end(&sd->bl, SC_ARMOR_RESIST, INVALID_TIMER);
  9312. }
  9313. // On ammo change
  9314. 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_))
  9315. status_change_end(&sd->bl, SC_P_ALTER, INVALID_TIMER);
  9316. pc_unequipitem_sub(sd, n, flag);
  9317. return true;
  9318. }
  9319. int pc_equipswitch( struct map_session_data* sd, int index ){
  9320. // Get the target equip mask
  9321. int position = sd->inventory.u.items_inventory[index].equipSwitch;
  9322. // Get the currently equipped item
  9323. short equippedItem = pc_checkequip( sd, position, true );
  9324. // No item equipped at the target
  9325. if( equippedItem == -1 ){
  9326. // Remove it from the equip switch
  9327. pc_equipswitch_remove( sd, index );
  9328. pc_equipitem( sd, index, position );
  9329. return position;
  9330. }else{
  9331. std::map<int, int> unequipped;
  9332. int unequipped_position = 0;
  9333. // Unequip all items that interfere
  9334. for( int i = 0; i < EQI_MAX; i++ ){
  9335. int unequip_index = sd->equip_index[i];
  9336. if( unequip_index >= 0 && position & equip_bitmask[i] ){
  9337. struct item* unequip_item = &sd->inventory.u.items_inventory[unequip_index];
  9338. // Store the unequipped index and position mask for later
  9339. unequipped[unequip_index] = unequip_item->equip;
  9340. // Keep the position for later
  9341. unequipped_position |= unequip_item->equip;
  9342. // Unequip the item
  9343. pc_unequipitem( sd, unequip_index, 0 );
  9344. }
  9345. }
  9346. int all_position = position | unequipped_position;
  9347. // Equip everything that is hit by the mask
  9348. for( int i = 0; i < EQI_MAX; i++ ){
  9349. int exchange_index = sd->equip_switch_index[i];
  9350. if( exchange_index >= 0 && all_position & equip_bitmask[i] ){
  9351. struct item* exchange_item = &sd->inventory.u.items_inventory[exchange_index];
  9352. // Store the target position
  9353. int exchange_position = exchange_item->equipSwitch;
  9354. // Remove the item from equip switch
  9355. pc_equipswitch_remove( sd, exchange_index );
  9356. // Equip the item at the destinated position
  9357. pc_equipitem( sd, exchange_index, exchange_position );
  9358. }
  9359. }
  9360. // Place all unequipped items into the equip switch window
  9361. for( std::pair<int, int> pair : unequipped ){
  9362. int unequipped_index = pair.first;
  9363. int unequipped_position = pair.second;
  9364. // Rebuild the index cache
  9365. for( int i = 0; i < EQI_MAX; i++ ){
  9366. if( unequipped_position & equip_bitmask[i] ){
  9367. sd->equip_switch_index[i] = unequipped_index;
  9368. }
  9369. }
  9370. // Set the correct position mask
  9371. sd->inventory.u.items_inventory[unequipped_index].equipSwitch = unequipped_position;
  9372. // Notify the client
  9373. clif_equipswitch_add( sd, unequipped_index, unequipped_position, ITEM_EQUIP_ACK_OK );
  9374. }
  9375. return all_position;
  9376. }
  9377. }
  9378. void pc_equipswitch_remove( struct map_session_data* sd, int index ){
  9379. struct item* item = &sd->inventory.u.items_inventory[index];
  9380. if( !item->equipSwitch ){
  9381. return;
  9382. }
  9383. for( int i = 0; i < EQI_MAX; i++ ){
  9384. // If a match is found
  9385. if( sd->equip_switch_index[i] == index ){
  9386. // Remove it from the slot
  9387. sd->equip_switch_index[i] = -1;
  9388. }
  9389. }
  9390. // Send out one packet for all slots using the current item's mask
  9391. clif_equipswitch_remove( sd, index, item->equipSwitch, false );
  9392. item->equipSwitch = 0;
  9393. }
  9394. /*==========================================
  9395. * Checking if player (sd) has an invalid item
  9396. * and is unequiped on map load (item_noequip)
  9397. *------------------------------------------*/
  9398. void pc_checkitem(struct map_session_data *sd) {
  9399. int i, calc_flag = 0;
  9400. struct item* it;
  9401. nullpo_retv(sd);
  9402. if( sd->state.vending ) //Avoid reorganizing items when we are vending, as that leads to exploits (pointed out by End of Exam)
  9403. return;
  9404. pc_check_available_item(sd, ITMCHK_NONE); // Check for invalid(ated) items.
  9405. for( i = 0; i < MAX_INVENTORY; i++ ) {
  9406. it = &sd->inventory.u.items_inventory[i];
  9407. if( it->nameid == 0 )
  9408. continue;
  9409. if( !it->equip )
  9410. continue;
  9411. if( it->equip&~pc_equippoint(sd,i) ) {
  9412. pc_unequipitem(sd, i, 2);
  9413. calc_flag = 1;
  9414. continue;
  9415. }
  9416. 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) ) {
  9417. pc_unequipitem(sd, i, 2);
  9418. calc_flag = 1;
  9419. continue;
  9420. }
  9421. }
  9422. for( i = 0; i < MAX_INVENTORY; i++ ) {
  9423. it = &sd->inventory.u.items_inventory[i];
  9424. if( it->nameid == 0 )
  9425. continue;
  9426. if( !it->equipSwitch )
  9427. continue;
  9428. if( it->equipSwitch&~pc_equippoint(sd,i) ||
  9429. ( !pc_has_permission(sd, PC_PERM_USE_ALL_EQUIPMENT) && !battle_config.allow_equip_restricted_item && itemdb_isNoEquip(sd->inventory_data[i], sd->bl.m) ) ){
  9430. for( int j = 0; j < EQI_MAX; j++ ){
  9431. if( sd->equip_switch_index[j] == i ){
  9432. sd->equip_switch_index[j] = -1;
  9433. }
  9434. }
  9435. sd->inventory.u.items_inventory[i].equipSwitch = 0;
  9436. continue;
  9437. }
  9438. }
  9439. if( calc_flag && sd->state.active ) {
  9440. pc_checkallowskill(sd);
  9441. status_calc_pc(sd,SCO_NONE);
  9442. }
  9443. }
  9444. /*==========================================
  9445. * Checks for unavailable items and removes them.
  9446. * @param sd: Player data
  9447. * @param type Forced check:
  9448. * 1 - Inventory
  9449. * 2 - Cart
  9450. * 4 - Storage
  9451. *------------------------------------------*/
  9452. void pc_check_available_item(struct map_session_data *sd, uint8 type)
  9453. {
  9454. int i;
  9455. unsigned short nameid;
  9456. char output[256];
  9457. nullpo_retv(sd);
  9458. if (battle_config.item_check&ITMCHK_INVENTORY && type&ITMCHK_INVENTORY) { // Check for invalid(ated) items in inventory.
  9459. for(i = 0; i < MAX_INVENTORY; i++) {
  9460. nameid = sd->inventory.u.items_inventory[i].nameid;
  9461. if (!nameid)
  9462. continue;
  9463. if (!itemdb_available(nameid)) {
  9464. sprintf(output, msg_txt(sd, 709), nameid); // Item %hu has been removed from your inventory.
  9465. clif_displaymessage(sd->fd, output);
  9466. ShowWarning("Removed invalid/disabled item (ID: %hu, amount: %d) from inventory (char_id: %d).\n", nameid, sd->inventory.u.items_inventory[i].amount, sd->status.char_id);
  9467. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 4, 0, LOG_TYPE_OTHER);
  9468. continue;
  9469. }
  9470. if (!sd->inventory.u.items_inventory[i].unique_id && !itemdb_isstackable(nameid))
  9471. sd->inventory.u.items_inventory[i].unique_id = pc_generate_unique_id(sd);
  9472. }
  9473. }
  9474. if (battle_config.item_check&ITMCHK_CART && type&ITMCHK_CART) { // Check for invalid(ated) items in cart.
  9475. for(i = 0; i < MAX_CART; i++) {
  9476. nameid = sd->cart.u.items_cart[i].nameid;
  9477. if (!nameid)
  9478. continue;
  9479. if (!itemdb_available(nameid)) {
  9480. sprintf(output, msg_txt(sd, 710), nameid); // Item %hu has been removed from your cart.
  9481. clif_displaymessage(sd->fd, output);
  9482. ShowWarning("Removed invalid/disabled item (ID: %hu, amount: %d) from cart (char_id: %d).\n", nameid, sd->cart.u.items_cart[i].amount, sd->status.char_id);
  9483. pc_cart_delitem(sd, i, sd->cart.u.items_cart[i].amount, 0, LOG_TYPE_OTHER);
  9484. continue;
  9485. }
  9486. if (!sd->cart.u.items_cart[i].unique_id && !itemdb_isstackable(nameid))
  9487. sd->cart.u.items_cart[i].unique_id = pc_generate_unique_id(sd);
  9488. }
  9489. }
  9490. if (battle_config.item_check&ITMCHK_STORAGE && type&ITMCHK_STORAGE) { // Check for invalid(ated) items in storage.
  9491. for(i = 0; i < sd->storage.max_amount; i++) {
  9492. nameid = sd->storage.u.items_storage[i].nameid;
  9493. if (!nameid)
  9494. continue;
  9495. if (!itemdb_available(nameid)) {
  9496. sprintf(output, msg_txt(sd, 711), nameid); // Item %hu has been removed from your storage.
  9497. clif_displaymessage(sd->fd, output);
  9498. ShowWarning("Removed invalid/disabled item (ID: %hu, amount: %d) from storage (char_id: %d).\n", nameid, sd->storage.u.items_storage[i].amount, sd->status.char_id);
  9499. storage_delitem(sd, &sd->storage, i, sd->storage.u.items_storage[i].amount);
  9500. continue;
  9501. }
  9502. if (!sd->storage.u.items_storage[i].unique_id && !itemdb_isstackable(nameid))
  9503. sd->storage.u.items_storage[i].unique_id = pc_generate_unique_id(sd);
  9504. }
  9505. }
  9506. }
  9507. /*==========================================
  9508. * Update PVP rank for sd1 in cmp to sd2
  9509. *------------------------------------------*/
  9510. static int pc_calc_pvprank_sub(struct block_list *bl,va_list ap)
  9511. {
  9512. struct map_session_data *sd1,*sd2;
  9513. sd1=(struct map_session_data *)bl;
  9514. sd2=va_arg(ap,struct map_session_data *);
  9515. if( pc_isinvisible(sd1) || pc_isinvisible(sd2) )
  9516. {// cannot register pvp rank for hidden GMs
  9517. return 0;
  9518. }
  9519. if( sd1->pvp_point > sd2->pvp_point )
  9520. sd2->pvp_rank++;
  9521. return 0;
  9522. }
  9523. /*==========================================
  9524. * Calculate new rank beetween all present players (map_foreachinallarea)
  9525. * and display result
  9526. *------------------------------------------*/
  9527. int pc_calc_pvprank(struct map_session_data *sd)
  9528. {
  9529. int old = sd->pvp_rank;
  9530. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  9531. sd->pvp_rank=1;
  9532. map_foreachinmap(pc_calc_pvprank_sub,sd->bl.m,BL_PC,sd);
  9533. if(old!=sd->pvp_rank || sd->pvp_lastusers!=mapdata->users_pvp)
  9534. clif_pvpset(sd,sd->pvp_rank,sd->pvp_lastusers=mapdata->users_pvp,0);
  9535. return sd->pvp_rank;
  9536. }
  9537. /*==========================================
  9538. * Calculate next sd ranking calculation from config
  9539. *------------------------------------------*/
  9540. TIMER_FUNC(pc_calc_pvprank_timer){
  9541. struct map_session_data *sd;
  9542. sd=map_id2sd(id);
  9543. if(sd==NULL)
  9544. return 0;
  9545. sd->pvp_timer = INVALID_TIMER;
  9546. if( pc_isinvisible(sd) )
  9547. {// do not calculate the pvp rank for a hidden GM
  9548. return 0;
  9549. }
  9550. if( pc_calc_pvprank(sd) > 0 )
  9551. sd->pvp_timer = add_timer(gettick()+PVP_CALCRANK_INTERVAL,pc_calc_pvprank_timer,id,data);
  9552. return 0;
  9553. }
  9554. /*==========================================
  9555. * Checking if sd is married
  9556. * Return:
  9557. * partner_id = yes
  9558. * 0 = no
  9559. *------------------------------------------*/
  9560. int pc_ismarried(struct map_session_data *sd)
  9561. {
  9562. if(sd == NULL)
  9563. return -1;
  9564. if(sd->status.partner_id > 0)
  9565. return sd->status.partner_id;
  9566. else
  9567. return 0;
  9568. }
  9569. /*==========================================
  9570. * Marry player sd to player dstsd
  9571. * Return:
  9572. * false = fail
  9573. * true = success
  9574. *------------------------------------------*/
  9575. bool pc_marriage(struct map_session_data *sd,struct map_session_data *dstsd)
  9576. {
  9577. if(sd == NULL || dstsd == NULL ||
  9578. sd->status.partner_id > 0 || dstsd->status.partner_id > 0 ||
  9579. (sd->class_&JOBL_BABY) || (dstsd->class_&JOBL_BABY))
  9580. return false;
  9581. sd->status.partner_id = dstsd->status.char_id;
  9582. dstsd->status.partner_id = sd->status.char_id;
  9583. achievement_update_objective(sd, AG_MARRY, 1, 1);
  9584. achievement_update_objective(dstsd, AG_MARRY, 1, 1);
  9585. return true;
  9586. }
  9587. /*==========================================
  9588. * Divorce sd from its partner
  9589. * Return:
  9590. * false = fail
  9591. * true = success
  9592. *------------------------------------------*/
  9593. bool pc_divorce(struct map_session_data *sd)
  9594. {
  9595. struct map_session_data *p_sd;
  9596. int i;
  9597. if( sd == NULL || !pc_ismarried(sd) )
  9598. return false;
  9599. if( !sd->status.partner_id )
  9600. return false; // Char is not married
  9601. if( (p_sd = map_charid2sd(sd->status.partner_id)) == NULL )
  9602. { // Lets char server do the divorce
  9603. if( chrif_divorce(sd->status.char_id, sd->status.partner_id) )
  9604. return false; // No char server connected
  9605. return true;
  9606. }
  9607. // Both players online, lets do the divorce manually
  9608. sd->status.partner_id = 0;
  9609. p_sd->status.partner_id = 0;
  9610. for( i = 0; i < MAX_INVENTORY; i++ )
  9611. {
  9612. if( sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_M || sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_F )
  9613. pc_delitem(sd, i, 1, 0, 0, LOG_TYPE_OTHER);
  9614. if( p_sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_M || p_sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_F )
  9615. pc_delitem(p_sd, i, 1, 0, 0, LOG_TYPE_OTHER);
  9616. }
  9617. clif_divorced(sd, p_sd->status.name);
  9618. clif_divorced(p_sd, sd->status.name);
  9619. return true;
  9620. }
  9621. /**
  9622. * Get the partner map_session_data of a player
  9623. * @param sd : the husband|wife session
  9624. * @return partner session or NULL
  9625. */
  9626. struct map_session_data *pc_get_partner(struct map_session_data *sd){
  9627. if (!sd || !pc_ismarried(sd))
  9628. return NULL;
  9629. return map_charid2sd(sd->status.partner_id);
  9630. }
  9631. /**
  9632. * Get the father map_session_data of a player
  9633. * @param sd : the baby session
  9634. * @return father session or NULL
  9635. */
  9636. struct map_session_data *pc_get_father (struct map_session_data *sd){
  9637. if (!sd || !(sd->class_&JOBL_BABY) || !sd->status.father)
  9638. return NULL;
  9639. return map_charid2sd(sd->status.father);
  9640. }
  9641. /**
  9642. * Get the mother map_session_data of a player
  9643. * @param sd : the baby session
  9644. * @return mother session or NULL
  9645. */
  9646. struct map_session_data *pc_get_mother (struct map_session_data *sd){
  9647. if (!sd || !(sd->class_&JOBL_BABY) || !sd->status.mother)
  9648. return NULL;
  9649. return map_charid2sd(sd->status.mother);
  9650. }
  9651. /*==========================================
  9652. * Get sd children charid. (Need to be married)
  9653. *------------------------------------------*/
  9654. struct map_session_data *pc_get_child (struct map_session_data *sd)
  9655. {
  9656. if (!sd || !pc_ismarried(sd) || !sd->status.child)
  9657. // charid2sd returns NULL if not found
  9658. return NULL;
  9659. return map_charid2sd(sd->status.child);
  9660. }
  9661. /*==========================================
  9662. * Set player sd to bleed. (losing hp and/or sp each diff_tick)
  9663. *------------------------------------------*/
  9664. void pc_bleeding (struct map_session_data *sd, t_tick diff_tick)
  9665. {
  9666. int hp = 0, sp = 0;
  9667. if( pc_isdead(sd) )
  9668. return;
  9669. if (sd->hp_loss.value) {
  9670. sd->hp_loss.tick += diff_tick;
  9671. while (sd->hp_loss.tick >= sd->hp_loss.rate) {
  9672. hp += sd->hp_loss.value;
  9673. sd->hp_loss.tick -= sd->hp_loss.rate;
  9674. }
  9675. if(hp >= sd->battle_status.hp)
  9676. hp = sd->battle_status.hp-1; //Script drains cannot kill you.
  9677. }
  9678. if (sd->sp_loss.value) {
  9679. sd->sp_loss.tick += diff_tick;
  9680. while (sd->sp_loss.tick >= sd->sp_loss.rate) {
  9681. sp += sd->sp_loss.value;
  9682. sd->sp_loss.tick -= sd->sp_loss.rate;
  9683. }
  9684. }
  9685. if (hp > 0 || sp > 0)
  9686. status_zap(&sd->bl, hp, sp);
  9687. }
  9688. //Character regen. Flag is used to know which types of regen can take place.
  9689. //&1: HP regen
  9690. //&2: SP regen
  9691. void pc_regen (struct map_session_data *sd, t_tick diff_tick)
  9692. {
  9693. int hp = 0, sp = 0;
  9694. if (sd->hp_regen.value) {
  9695. sd->hp_regen.tick += diff_tick;
  9696. while (sd->hp_regen.tick >= sd->hp_regen.rate) {
  9697. hp += sd->hp_regen.value;
  9698. sd->hp_regen.tick -= sd->hp_regen.rate;
  9699. }
  9700. }
  9701. if (sd->sp_regen.value) {
  9702. sd->sp_regen.tick += diff_tick;
  9703. while (sd->sp_regen.tick >= sd->sp_regen.rate) {
  9704. sp += sd->sp_regen.value;
  9705. sd->sp_regen.tick -= sd->sp_regen.rate;
  9706. }
  9707. }
  9708. if (sd->percent_hp_regen.value) {
  9709. sd->percent_hp_regen.tick += diff_tick;
  9710. while (sd->percent_hp_regen.tick >= sd->percent_hp_regen.rate) {
  9711. hp += sd->status.max_hp * sd->percent_hp_regen.value / 100;
  9712. sd->percent_hp_regen.tick -= sd->percent_hp_regen.rate;
  9713. }
  9714. }
  9715. if (sd->percent_sp_regen.value) {
  9716. sd->percent_sp_regen.tick += diff_tick;
  9717. while (sd->percent_sp_regen.tick >= sd->percent_sp_regen.rate) {
  9718. sp += sd->status.max_sp * sd->percent_sp_regen.value / 100;
  9719. sd->percent_sp_regen.tick -= sd->percent_sp_regen.rate;
  9720. }
  9721. }
  9722. if (hp > 0 || sp > 0)
  9723. status_heal(&sd->bl, hp, sp, 0);
  9724. }
  9725. /*==========================================
  9726. * Memo player sd savepoint. (map,x,y)
  9727. *------------------------------------------*/
  9728. void pc_setsavepoint(struct map_session_data *sd, short mapindex,int x,int y)
  9729. {
  9730. nullpo_retv(sd);
  9731. sd->status.save_point.map = mapindex;
  9732. sd->status.save_point.x = x;
  9733. sd->status.save_point.y = y;
  9734. }
  9735. /*==========================================
  9736. * Save 1 player data at autosave interval
  9737. *------------------------------------------*/
  9738. static TIMER_FUNC(pc_autosave){
  9739. int interval;
  9740. struct s_mapiterator* iter;
  9741. struct map_session_data* sd;
  9742. static int last_save_id = 0, save_flag = 0;
  9743. if(save_flag == 2) //Someone was saved on last call, normal cycle
  9744. save_flag = 0;
  9745. else
  9746. save_flag = 1; //Noone was saved, so save first found char.
  9747. iter = mapit_getallusers();
  9748. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  9749. {
  9750. if (!sd->state.pc_loaded) // Player data hasn't fully loaded
  9751. continue;
  9752. if(sd->bl.id == last_save_id && save_flag != 1) {
  9753. save_flag = 1;
  9754. continue;
  9755. }
  9756. if(save_flag != 1) //Not our turn to save yet.
  9757. continue;
  9758. //Save char.
  9759. last_save_id = sd->bl.id;
  9760. save_flag = 2;
  9761. if (pc_isvip(sd)) // Check if we're still VIP
  9762. chrif_req_login_operation(1, sd->status.name, CHRIF_OP_LOGIN_VIP, 0, 1, 0);
  9763. chrif_save(sd, CSAVE_INVENTORY|CSAVE_CART);
  9764. break;
  9765. }
  9766. mapit_free(iter);
  9767. interval = autosave_interval/(map_usercount()+1);
  9768. if(interval < minsave_interval)
  9769. interval = minsave_interval;
  9770. add_timer(gettick()+interval,pc_autosave,0,0);
  9771. return 0;
  9772. }
  9773. static int pc_daynight_timer_sub(struct map_session_data *sd,va_list ap)
  9774. {
  9775. if (sd->state.night != night_flag && map_getmapflag(sd->bl.m, MF_NIGHTENABLED))
  9776. { //Night/day state does not match.
  9777. clif_status_load(&sd->bl, EFST_SKE, night_flag); //New night effect by dynamix [Skotlex]
  9778. sd->state.night = night_flag;
  9779. return 1;
  9780. }
  9781. return 0;
  9782. }
  9783. /*================================================
  9784. * timer to do the day [Yor]
  9785. * data: 0 = called by timer, 1 = gmcommand/script
  9786. *------------------------------------------------*/
  9787. TIMER_FUNC(map_day_timer){
  9788. char tmp_soutput[1024];
  9789. if (data == 0 && battle_config.day_duration <= 0) // if we want a day
  9790. return 0;
  9791. if (!night_flag)
  9792. return 0; //Already day.
  9793. night_flag = 0; // 0=day, 1=night [Yor]
  9794. map_foreachpc(pc_daynight_timer_sub);
  9795. strcpy(tmp_soutput, (data == 0) ? msg_txt(NULL,502) : msg_txt(NULL,60)); // The day has arrived!
  9796. intif_broadcast(tmp_soutput, strlen(tmp_soutput) + 1, BC_DEFAULT);
  9797. return 0;
  9798. }
  9799. /*================================================
  9800. * timer to do the night [Yor]
  9801. * data: 0 = called by timer, 1 = gmcommand/script
  9802. *------------------------------------------------*/
  9803. TIMER_FUNC(map_night_timer){
  9804. char tmp_soutput[1024];
  9805. if (data == 0 && battle_config.night_duration <= 0) // if we want a night
  9806. return 0;
  9807. if (night_flag)
  9808. return 0; //Already nigth.
  9809. night_flag = 1; // 0=day, 1=night [Yor]
  9810. map_foreachpc(pc_daynight_timer_sub);
  9811. strcpy(tmp_soutput, (data == 0) ? msg_txt(NULL,503) : msg_txt(NULL,59)); // The night has fallen...
  9812. intif_broadcast(tmp_soutput, strlen(tmp_soutput) + 1, BC_DEFAULT);
  9813. return 0;
  9814. }
  9815. /**
  9816. * Attempt to stand up a player
  9817. * @param sd
  9818. * @param force Ignore the check, ask player to stand up. Used in some cases like pc_damage(), pc_revive(), etc
  9819. * @return True if success, Fals if failed
  9820. */
  9821. bool pc_setstand(struct map_session_data *sd, bool force){
  9822. nullpo_ret(sd);
  9823. // Cannot stand yet
  9824. // TODO: Move to SCS_NOSTAND [Cydh]
  9825. if (!force && (sd->sc.data[SC_SITDOWN_FORCE] || sd->sc.data[SC_BANANA_BOMB_SITDOWN]))
  9826. return false;
  9827. status_change_end(&sd->bl, SC_TENSIONRELAX, INVALID_TIMER);
  9828. clif_status_load(&sd->bl,EFST_SIT,0);
  9829. clif_standing(&sd->bl); //Inform area PC is standing
  9830. //Reset sitting tick.
  9831. sd->ssregen.tick.hp = sd->ssregen.tick.sp = 0;
  9832. if( pc_isdead( sd ) ){
  9833. sd->state.dead_sit = sd->vd.dead_sit = 0;
  9834. clif_party_dead( sd );
  9835. }else{
  9836. sd->state.dead_sit = sd->vd.dead_sit = 0;
  9837. }
  9838. return true;
  9839. }
  9840. /**
  9841. * Calculate Overheat value
  9842. * @param sd: Player data
  9843. * @param heat: Amount of Heat to adjust
  9844. **/
  9845. void pc_overheat(struct map_session_data *sd, int16 heat) {
  9846. struct status_change_entry *sce = NULL;
  9847. int16 limit[] = { 150, 200, 280, 360, 450 };
  9848. uint16 skill_lv;
  9849. nullpo_retv(sd);
  9850. skill_lv = cap_value(pc_checkskill(sd, NC_MAINFRAME), 0, 4);
  9851. if ((sce = sd->sc.data[SC_OVERHEAT_LIMITPOINT])) {
  9852. sce->val1 += heat;
  9853. sce->val1 = cap_value(sce->val1, 0, 1000);
  9854. if (sd->sc.data[SC_OVERHEAT])
  9855. status_change_end(&sd->bl, SC_OVERHEAT, INVALID_TIMER);
  9856. if (sce->val1 > limit[skill_lv])
  9857. sc_start(&sd->bl, &sd->bl, SC_OVERHEAT, 100, sce->val1, 1000);
  9858. } else if (heat > 0)
  9859. sc_start(&sd->bl, &sd->bl, SC_OVERHEAT_LIMITPOINT, 100, heat, 1000);
  9860. }
  9861. /**
  9862. * Check if player is autolooting given itemID.
  9863. */
  9864. bool pc_isautolooting(struct map_session_data *sd, unsigned short nameid)
  9865. {
  9866. uint8 i = 0;
  9867. if (sd->state.autoloottype && sd->state.autoloottype&(1<<itemdb_type(nameid)))
  9868. return true;
  9869. if (!sd->state.autolooting)
  9870. return false;
  9871. if (sd->state.autolooting)
  9872. ARR_FIND(0, AUTOLOOTITEM_SIZE, i, sd->state.autolootid[i] == nameid);
  9873. return (i != AUTOLOOTITEM_SIZE);
  9874. }
  9875. /**
  9876. * Checks if player can use @/#command
  9877. * @param sd Player map session data
  9878. * @param command Command name without @/# and params
  9879. * @param type is it atcommand or charcommand
  9880. */
  9881. bool pc_can_use_command(struct map_session_data *sd, const char *command, AtCommandType type)
  9882. {
  9883. return pc_group_can_use_command(pc_get_group_id(sd), command, type);
  9884. }
  9885. /**
  9886. * Checks if commands used by a player should be logged
  9887. * according to their group setting.
  9888. * @param sd Player map session data
  9889. */
  9890. bool pc_should_log_commands(struct map_session_data *sd)
  9891. {
  9892. return pc_group_should_log_commands(pc_get_group_id(sd));
  9893. }
  9894. /**
  9895. * Spirit Charm expiration timer.
  9896. * @see TimerFunc
  9897. */
  9898. static TIMER_FUNC(pc_spiritcharm_timer){
  9899. struct map_session_data *sd;
  9900. int i;
  9901. if ((sd = (struct map_session_data *)map_id2sd(id)) == NULL || sd->bl.type != BL_PC)
  9902. return 1;
  9903. if (sd->spiritcharm <= 0) {
  9904. 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);
  9905. sd->spiritcharm = 0;
  9906. sd->spiritcharm_type = CHARM_TYPE_NONE;
  9907. return 0;
  9908. }
  9909. ARR_FIND(0, sd->spiritcharm, i, sd->spiritcharm_timer[i] == tid);
  9910. if (i == sd->spiritcharm) {
  9911. ShowError("pc_spiritcharm_timer: timer not found (aid=%d cid=%d tid=%d)\n", sd->status.account_id, sd->status.char_id, tid);
  9912. return 0;
  9913. }
  9914. sd->spiritcharm--;
  9915. if (i != sd->spiritcharm)
  9916. memmove(sd->spiritcharm_timer + i, sd->spiritcharm_timer + i + 1, (sd->spiritcharm - i) * sizeof(int));
  9917. sd->spiritcharm_timer[sd->spiritcharm] = INVALID_TIMER;
  9918. if (sd->spiritcharm <= 0)
  9919. sd->spiritcharm_type = CHARM_TYPE_NONE;
  9920. clif_spiritcharm(sd);
  9921. return 0;
  9922. }
  9923. /**
  9924. * Adds a spirit charm.
  9925. * @param sd: Target character
  9926. * @param interval: Duration
  9927. * @param max: Maximum amount of charms to add
  9928. * @param type: Charm type (@see spirit_charm_types)
  9929. */
  9930. void pc_addspiritcharm(struct map_session_data *sd, int interval, int max, int type)
  9931. {
  9932. int tid, i;
  9933. nullpo_retv(sd);
  9934. if (sd->spiritcharm_type != CHARM_TYPE_NONE && type != sd->spiritcharm_type)
  9935. pc_delspiritcharm(sd, sd->spiritcharm, sd->spiritcharm_type);
  9936. if (max > MAX_SPIRITCHARM)
  9937. max = MAX_SPIRITCHARM;
  9938. if (sd->spiritcharm < 0)
  9939. sd->spiritcharm = 0;
  9940. if (sd->spiritcharm && sd->spiritcharm >= max) {
  9941. if (sd->spiritcharm_timer[0] != INVALID_TIMER)
  9942. delete_timer(sd->spiritcharm_timer[0], pc_spiritcharm_timer);
  9943. sd->spiritcharm--;
  9944. if (sd->spiritcharm != 0)
  9945. memmove(sd->spiritcharm_timer + 0, sd->spiritcharm_timer + 1, (sd->spiritcharm) * sizeof(int));
  9946. sd->spiritcharm_timer[sd->spiritcharm] = INVALID_TIMER;
  9947. }
  9948. tid = add_timer(gettick() + interval, pc_spiritcharm_timer, sd->bl.id, 0);
  9949. 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);
  9950. if (i != sd->spiritcharm)
  9951. memmove(sd->spiritcharm_timer + i + 1, sd->spiritcharm_timer + i, (sd->spiritcharm - i) * sizeof(int));
  9952. sd->spiritcharm_timer[i] = tid;
  9953. sd->spiritcharm++;
  9954. sd->spiritcharm_type = type;
  9955. clif_spiritcharm(sd);
  9956. }
  9957. /**
  9958. * Removes one or more spirit charms.
  9959. * @param sd: The target character
  9960. * @param count: Amount of charms to remove
  9961. * @param type: Type of charm to remove
  9962. */
  9963. void pc_delspiritcharm(struct map_session_data *sd, int count, int type)
  9964. {
  9965. int i;
  9966. nullpo_retv(sd);
  9967. if (sd->spiritcharm_type != type)
  9968. return;
  9969. if (sd->spiritcharm <= 0) {
  9970. sd->spiritcharm = 0;
  9971. return;
  9972. }
  9973. if (count <= 0)
  9974. return;
  9975. if (count > sd->spiritcharm)
  9976. count = sd->spiritcharm;
  9977. sd->spiritcharm -= count;
  9978. if (count > MAX_SPIRITCHARM)
  9979. count = MAX_SPIRITCHARM;
  9980. for (i = 0; i < count; i++) {
  9981. if (sd->spiritcharm_timer[i] != INVALID_TIMER) {
  9982. delete_timer(sd->spiritcharm_timer[i], pc_spiritcharm_timer);
  9983. sd->spiritcharm_timer[i] = INVALID_TIMER;
  9984. }
  9985. }
  9986. for (i = count; i < MAX_SPIRITCHARM; i++) {
  9987. sd->spiritcharm_timer[i - count] = sd->spiritcharm_timer[i];
  9988. sd->spiritcharm_timer[i] = INVALID_TIMER;
  9989. }
  9990. if (sd->spiritcharm <= 0)
  9991. sd->spiritcharm_type = CHARM_TYPE_NONE;
  9992. clif_spiritcharm(sd);
  9993. }
  9994. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  9995. /**
  9996. * Renewal EXP/Item Drop rate modifier based on level penalty
  9997. * @param level_diff: Monster and Player level difference
  9998. * @param mob_class: Monster class
  9999. * @param mode: Monster mode
  10000. * @param type: 1 - EXP, 2 - Item Drop
  10001. * @return Penalty rate
  10002. */
  10003. int pc_level_penalty_mod(int level_diff, uint32 mob_class, enum e_mode mode, int type)
  10004. {
  10005. int rate = 100;
  10006. if (type == 2 && (mode&MD_FIXED_ITEMDROP))
  10007. return rate;
  10008. if (level_diff < 0)
  10009. level_diff = MAX_LEVEL + (~level_diff + 1);
  10010. if ((rate = level_penalty[type][mob_class][level_diff]) > 0) // Monster class found, return rate
  10011. return rate;
  10012. return 100; // Penalty not found, return default
  10013. }
  10014. #endif
  10015. int pc_split_str(char *str,char **val,int num)
  10016. {
  10017. int i;
  10018. for (i=0; i<num && str; i++){
  10019. val[i] = str;
  10020. str = strchr(str,',');
  10021. if (str && i<num-1) //Do not remove a trailing comma.
  10022. *str++=0;
  10023. }
  10024. return i;
  10025. }
  10026. int pc_split_atoi(char* str, int* val, char sep, int max)
  10027. {
  10028. int i,j;
  10029. for (i=0; i<max; i++) {
  10030. if (!str) break;
  10031. val[i] = atoi(str);
  10032. str = strchr(str,sep);
  10033. if (str)
  10034. *str++=0;
  10035. }
  10036. //Zero up the remaining.
  10037. for(j=i; j < max; j++)
  10038. val[j] = 0;
  10039. return i;
  10040. }
  10041. int pc_split_atoui(char* str, unsigned int* val, char sep, int max)
  10042. {
  10043. static int warning=0;
  10044. int i,j;
  10045. for (i=0; i<max; i++) {
  10046. double f;
  10047. if (!str) break;
  10048. f = atof(str);
  10049. if (f < 0)
  10050. val[i] = 0;
  10051. else if (f > UINT_MAX) {
  10052. val[i] = UINT_MAX;
  10053. if (!warning) {
  10054. warning = 1;
  10055. ShowWarning("pc_readdb (exp.txt): Required exp per level is capped to %u\n", UINT_MAX);
  10056. }
  10057. } else
  10058. val[i] = (unsigned int)f;
  10059. str = strchr(str,sep);
  10060. if (str)
  10061. *str++=0;
  10062. }
  10063. //Zero up the remaining.
  10064. for(j=i; j < max; j++)
  10065. val[j] = 0;
  10066. return i;
  10067. }
  10068. /*==========================================
  10069. * sub DB reading.
  10070. * Function used to read skill_tree.txt
  10071. *------------------------------------------*/
  10072. static bool pc_readdb_skilltree(char* fields[], int columns, int current)
  10073. {
  10074. uint32 baselv, joblv, baselv_max, joblv_max;
  10075. uint16 skill_id, skill_lv, skill_lv_max;
  10076. int idx, class_;
  10077. unsigned int i, offset, skill_idx;
  10078. class_ = atoi(fields[0]);
  10079. skill_id = (uint16)atoi(fields[1]);
  10080. skill_lv = (uint16)atoi(fields[2]);
  10081. if (columns == 5 + MAX_PC_SKILL_REQUIRE * 2) { // Base/Job level requirement extra columns
  10082. baselv = (uint32)atoi(fields[3]);
  10083. joblv = (uint32)atoi(fields[4]);
  10084. offset = 5;
  10085. }
  10086. else if (columns == 3 + MAX_PC_SKILL_REQUIRE * 2) {
  10087. baselv = joblv = 0;
  10088. offset = 3;
  10089. }
  10090. else {
  10091. ShowWarning("pc_readdb_skilltree: Invalid number of colums in skill %hu of job %d's tree.\n", skill_id, class_);
  10092. return false;
  10093. }
  10094. if(!pcdb_checkid(class_))
  10095. {
  10096. ShowWarning("pc_readdb_skilltree: Invalid job class %d specified.\n", class_);
  10097. return false;
  10098. }
  10099. idx = pc_class2idx(class_);
  10100. if (!skill_get_index(skill_id)) {
  10101. ShowWarning("pc_readdb_skilltree: Unable to load skill %hu into job %d's tree.\n", skill_id, class_);
  10102. return false;
  10103. }
  10104. if (skill_lv > (skill_lv_max = skill_get_max(skill_id))) {
  10105. ShowWarning("pc_readdb_skilltree: Skill %hu's level %hu exceeds job %d's max level %hu. Capping skill level.\n", skill_id, skill_lv, class_, skill_lv_max);
  10106. skill_lv = skill_lv_max;
  10107. }
  10108. if (baselv > (baselv_max = pc_class_maxbaselv(class_))) {
  10109. ShowWarning("pc_readdb_skilltree: Skill %hu's base level requirement %d exceeds job %d's max base level %d. Capping skill base level.\n", skill_id, baselv, class_, baselv_max);
  10110. baselv = baselv_max;
  10111. }
  10112. if (joblv > (joblv_max = pc_class_maxjoblv(class_))) {
  10113. ShowWarning("pc_readdb_skilltree: Skill %hu's job level requirement %d exceeds job %d's max job level %d. Capping skill job level.\n", skill_id, joblv, class_, joblv_max);
  10114. joblv = joblv_max;
  10115. }
  10116. //This is to avoid adding two lines for the same skill. [Skotlex]
  10117. ARR_FIND( 0, MAX_SKILL_TREE, skill_idx, skill_tree[idx][skill_idx].skill_id == 0 || skill_tree[idx][skill_idx].skill_id == skill_id );
  10118. if( skill_idx == MAX_SKILL_TREE )
  10119. {
  10120. ShowWarning("pc_readdb_skilltree: Unable to load skill %hu into job %d's tree. Maximum number of skills per job has been reached.\n", skill_id, class_);
  10121. return false;
  10122. }
  10123. else if(skill_tree[idx][skill_idx].skill_id)
  10124. {
  10125. ShowNotice("pc_readdb_skilltree: Overwriting skill %hu for job %d.\n", skill_id, class_);
  10126. }
  10127. skill_tree[idx][skill_idx].skill_id = skill_id;
  10128. skill_tree[idx][skill_idx].skill_lv = skill_lv;
  10129. skill_tree[idx][skill_idx].baselv = baselv;
  10130. skill_tree[idx][skill_idx].joblv = joblv;
  10131. for(i = 0; i < MAX_PC_SKILL_REQUIRE; i++)
  10132. {
  10133. skill_id = (uint16)atoi(fields[i * 2 + offset]);
  10134. skill_lv = (uint16)atoi(fields[i * 2 + offset + 1]);
  10135. if (skill_id == 0) {
  10136. if (skill_tree[idx][skill_idx].need[i].skill_id > 0) { // Remove pre-requisite
  10137. skill_tree[idx][skill_idx].need[i].skill_id = 0;
  10138. skill_tree[idx][skill_idx].need[i].skill_lv = 0;
  10139. }
  10140. continue;
  10141. }
  10142. if (skill_id > MAX_SKILL_ID || !skill_get_index(skill_id)) {
  10143. ShowWarning("pc_readdb_skilltree: Unable to load requirement skill %hu into job %d's tree.", skill_id, class_);
  10144. return false;
  10145. }
  10146. if (skill_lv > (skill_lv_max = skill_get_max(skill_id))) {
  10147. ShowWarning("pc_readdb_skilltree: Skill %hu's level %hu exceeds job %d's max level %hu. Capping skill level.\n", skill_id, skill_lv, class_, skill_lv_max);
  10148. skill_lv = skill_lv_max;
  10149. }
  10150. skill_tree[idx][skill_idx].need[i].skill_id = skill_id;
  10151. skill_tree[idx][skill_idx].need[i].skill_lv = skill_lv;
  10152. }
  10153. return true;
  10154. }
  10155. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  10156. static bool pc_readdb_levelpenalty(char* fields[], int columns, int current)
  10157. {
  10158. int type, class_, diff;
  10159. type = atoi(fields[0]); //1=experience, 2=item drop
  10160. class_ = atoi(fields[1]);
  10161. diff = atoi(fields[2]);
  10162. if( type != 1 && type != 2 ){
  10163. ShowWarning("pc_readdb_levelpenalty: Invalid type %d specified.\n", type);
  10164. return false;
  10165. }
  10166. if( !CHK_CLASS(class_) ){
  10167. ShowWarning("pc_readdb_levelpenalty: Invalid class %d specified.\n", class_);
  10168. return false;
  10169. }
  10170. diff = min(diff, MAX_LEVEL);
  10171. if( diff < 0 )
  10172. diff = min(MAX_LEVEL + ( ~(diff) + 1 ), MAX_LEVEL*2);
  10173. level_penalty[type][class_][diff] = atoi(fields[3]);
  10174. return true;
  10175. }
  10176. #endif
  10177. /** [Cydh]
  10178. * Calculates base hp of player. Reference: http://irowiki.org/wiki/Max_HP
  10179. * @param level Base level of player
  10180. * @param class_ Job ID @see enum e_job
  10181. * @return base_hp
  10182. */
  10183. static unsigned int pc_calc_basehp(uint16 level, uint16 class_) {
  10184. double base_hp;
  10185. uint16 i, idx = pc_class2idx(class_);
  10186. base_hp = 35 + level * (job_info[idx].hp_multiplicator/100.);
  10187. #ifndef RENEWAL
  10188. if(level >= 10 && (class_ == JOB_NINJA || class_ == JOB_GUNSLINGER)) base_hp += 90;
  10189. #endif
  10190. for (i = 2; i <= level; i++)
  10191. base_hp += floor(((job_info[idx].hp_factor/100.) * i) + 0.5); //Don't have round()
  10192. if (class_ == JOB_SUMMONER)
  10193. base_hp += floor((base_hp / 2) + 0.5);
  10194. return (unsigned int)base_hp;
  10195. }
  10196. /** [Playtester]
  10197. * Calculates base sp of player.
  10198. * @param level Base level of player
  10199. * @param class_ Job ID @see enum e_job
  10200. * @return base_sp
  10201. */
  10202. static unsigned int pc_calc_basesp(uint16 level, uint16 class_) {
  10203. double base_sp;
  10204. uint16 idx = pc_class2idx(class_);
  10205. base_sp = 10 + floor(level * (job_info[idx].sp_factor / 100.));
  10206. switch (class_) {
  10207. case JOB_NINJA:
  10208. if (level >= 10)
  10209. base_sp -= 22;
  10210. else
  10211. base_sp = 11 + 3*level;
  10212. break;
  10213. case JOB_GUNSLINGER:
  10214. if (level > 10)
  10215. base_sp -= 18;
  10216. else
  10217. base_sp = 9 + 3*level;
  10218. break;
  10219. case JOB_SUMMONER:
  10220. base_sp -= floor(base_sp / 2);
  10221. break;
  10222. }
  10223. return (unsigned int)base_sp;
  10224. }
  10225. //Reading job_db1.txt line, (class,weight,HPFactor,HPMultiplicator,SPFactor,aspd/lvl...)
  10226. static bool pc_readdb_job1(char* fields[], int columns, int current){
  10227. int idx, class_;
  10228. unsigned int i;
  10229. class_ = atoi(fields[0]);
  10230. if (!pcdb_checkid(class_)) {
  10231. ShowWarning("status_readdb_job1: Invalid job class %d specified.\n", class_);
  10232. return false;
  10233. }
  10234. idx = pc_class2idx(class_);
  10235. job_info[idx].max_weight_base = atoi(fields[1]);
  10236. job_info[idx].hp_factor = atoi(fields[2]);
  10237. job_info[idx].hp_multiplicator = atoi(fields[3]);
  10238. job_info[idx].sp_factor = atoi(fields[4]);
  10239. #ifdef RENEWAL_ASPD
  10240. for(i = 0; i <= MAX_WEAPON_TYPE; i++)
  10241. #else
  10242. for(i = 0; i < MAX_WEAPON_TYPE; i++)
  10243. #endif
  10244. {
  10245. job_info[idx].aspd_base[i] = atoi(fields[i+5]);
  10246. }
  10247. return true;
  10248. }
  10249. //Reading job_db2.txt line (class,JobLv1,JobLv2,JobLv3,...)
  10250. static bool pc_readdb_job2(char* fields[], int columns, int current)
  10251. {
  10252. int idx, class_, i;
  10253. class_ = atoi(fields[0]);
  10254. if(!pcdb_checkid(class_))
  10255. {
  10256. ShowWarning("status_readdb_job2: Invalid job class %d specified.\n", class_);
  10257. return false;
  10258. }
  10259. idx = pc_class2idx(class_);
  10260. for(i = 1; i < columns; i++)
  10261. {
  10262. job_info[idx].job_bonus[i-1] = atoi(fields[i]);
  10263. }
  10264. return true;
  10265. }
  10266. //Reading job_exp.txt line
  10267. //Max Level,Class list,Type (0 - Base Exp; 1 - Job Exp),Exp/lvl...
  10268. static bool pc_readdb_job_exp(char* fields[], int columns, int current)
  10269. {
  10270. int idx, i, type;
  10271. int job_id,job_count,jobs[CLASS_COUNT];
  10272. unsigned int ui, maxlvl;
  10273. maxlvl = atoi(fields[0]);
  10274. if(maxlvl > MAX_LEVEL || maxlvl<1){
  10275. ShowError("pc_readdb_job_exp: Invalid maxlevel %d specified.\n", maxlvl);
  10276. return false;
  10277. }
  10278. if((maxlvl+3) > columns){ //nb values = (maxlvl-startlvl)+1-index1stvalue
  10279. ShowError("pc_readdb_job_exp: Number of columns %d defined is too low for max level %d.\n",columns,maxlvl);
  10280. return false;
  10281. }
  10282. type = atoi(fields[2]);
  10283. if(type < 0 || type > 1){
  10284. ShowError("pc_readdb_job_exp: Invalid type %d specified.\n", type);
  10285. return false;
  10286. }
  10287. job_count = pc_split_atoi(fields[1],jobs,':',CLASS_COUNT);
  10288. if (job_count < 1)
  10289. return false;
  10290. job_id = jobs[0];
  10291. if(!pcdb_checkid(job_id)){
  10292. ShowError("pc_readdb_job_exp: Invalid job class %d specified.\n", job_id);
  10293. return false;
  10294. }
  10295. idx = pc_class2idx(job_id);
  10296. job_info[idx].max_level[type] = maxlvl;
  10297. for(i=0; i<maxlvl; i++)
  10298. job_info[idx].exp_table[type][i] = ((uint32) atoi(fields[3+i]));
  10299. //Reverse check in case the array has a bunch of trailing zeros... [Skotlex]
  10300. //The reasoning behind the -2 is this... if the max level is 5, then the array
  10301. //should look like this:
  10302. //0: x, 1: x, 2: x: 3: x 4: 0 <- last valid value is at 3.
  10303. while ((ui = job_info[idx].max_level[type]) >= 2 && job_info[idx].exp_table[type][ui-2] <= 0)
  10304. job_info[idx].max_level[type]--;
  10305. if (job_info[idx].max_level[type] < maxlvl) {
  10306. ShowWarning("pc_readdb_job_exp: Specified max %u for job %d, but that job's exp table only goes up to level %u.\n", maxlvl, job_id, job_info[idx].max_level[type]);
  10307. ShowInfo("Filling the missing values with the last exp entry.\n");
  10308. //Fill the requested values with the last entry.
  10309. ui = (job_info[idx].max_level[type] <= 2? 0: job_info[idx].max_level[type]-2);
  10310. for (; ui+2 < maxlvl; ui++)
  10311. job_info[idx].exp_table[type][ui] = job_info[idx].exp_table[type][ui-1];
  10312. job_info[idx].max_level[type] = maxlvl;
  10313. }
  10314. // ShowInfo("%s - Class %d: %d\n", type?"Job":"Base", job_id, job_info[idx].max_level[type]);
  10315. for (i = 1; i < job_count; i++) {
  10316. job_id = jobs[i];
  10317. if (!pcdb_checkid(job_id)) {
  10318. ShowError("pc_readdb_job_exp: Invalid job ID %d.\n", job_id);
  10319. continue;
  10320. }
  10321. idx = pc_class2idx(job_id);
  10322. memcpy(job_info[idx].exp_table[type], job_info[pc_class2idx(jobs[0])].exp_table[type], sizeof(job_info[pc_class2idx(jobs[0])].exp_table[type]));
  10323. job_info[idx].max_level[type] = maxlvl;
  10324. // ShowInfo("%s - Class %d: %u\n", type?"Job":"Base", job_id, job_info[idx].max_level[type]);
  10325. }
  10326. return true;
  10327. }
  10328. /**
  10329. * #ifdef HP_SP_TABLES, reads 'job_basehpsp_db.txt to replace hp/sp results from formula
  10330. * startlvl,endlvl,class,type,values...
  10331. */
  10332. #ifdef HP_SP_TABLES
  10333. static bool pc_readdb_job_basehpsp(char* fields[], int columns, int current)
  10334. {
  10335. int i, startlvl, endlvl;
  10336. int job_count,jobs[CLASS_COUNT];
  10337. short type;
  10338. startlvl = atoi(fields[0]);
  10339. if(startlvl<1){
  10340. ShowError("pc_readdb_job_basehpsp: Invalid start level %d specified.\n", startlvl);
  10341. return false;
  10342. }
  10343. endlvl = atoi(fields[1]);
  10344. if(endlvl<1 || endlvl<startlvl){
  10345. ShowError("pc_readdb_job_basehpsp: Invalid end level %d specified.\n", endlvl);
  10346. return false;
  10347. }
  10348. if((endlvl-startlvl+1+4) > columns){ //nb values = (maxlvl-startlvl)+1-index1stvalue
  10349. ShowError("pc_readdb_job_basehpsp: Number of columns %d (needs %d) defined is too low for start level %d, max level %d.\n",columns,(endlvl-startlvl+1+4),startlvl,endlvl);
  10350. return false;
  10351. }
  10352. type = atoi(fields[3]);
  10353. if(type < 0 || type > 1){
  10354. ShowError("pc_readdb_job_basehpsp: Invalid type %d specified.\n", type);
  10355. return false;
  10356. }
  10357. job_count = pc_split_atoi(fields[2],jobs,':',CLASS_COUNT);
  10358. if (job_count < 1)
  10359. return false;
  10360. for (i = 0; i < job_count; i++) {
  10361. int idx, job_id = jobs[i], use_endlvl;
  10362. if (!pcdb_checkid(job_id)) {
  10363. ShowError("pc_readdb_job_basehpsp: Invalid job class %d specified.\n", job_id);
  10364. return false;
  10365. }
  10366. idx = pc_class2idx(job_id);
  10367. if (startlvl > job_info[idx].max_level[0]) {
  10368. ShowError("pc_readdb_job_basehpsp: Invalid start level %d specified.\n", startlvl);
  10369. return false;
  10370. }
  10371. //Just read until available max level for this job, don't use MAX_LEVEL!
  10372. use_endlvl = endlvl;
  10373. if (use_endlvl > job_info[idx].max_level[0])
  10374. use_endlvl = job_info[idx].max_level[0];
  10375. if(type == 0) { //hp type
  10376. uint16 j;
  10377. for(j = 0; j < use_endlvl; j++) {
  10378. if (atoi(fields[j+4])) {
  10379. uint16 lvl_idx = startlvl-1+j;
  10380. job_info[idx].base_hp[lvl_idx] = atoi(fields[j+4]);
  10381. //Tells if this HP is lower than previous level (but not for 99->100)
  10382. if (lvl_idx-1 >= 0 && lvl_idx != 99 && job_info[idx].base_hp[lvl_idx] < job_info[idx].base_hp[lvl_idx-1])
  10383. ShowInfo("pc_readdb_job_basehpsp: HP value at entry %d col %d is lower than previous level (job=%d,lvl=%d,oldval=%d,val=%d).\n",
  10384. current,j+4,job_id,lvl_idx+1,job_info[idx].base_hp[lvl_idx-1],job_info[idx].base_hp[lvl_idx]);
  10385. }
  10386. }
  10387. }
  10388. else { //sp type
  10389. uint16 j;
  10390. for(j = 0; j < use_endlvl; j++) {
  10391. if (atoi(fields[j+4])) {
  10392. uint16 lvl_idx = startlvl-1+j;
  10393. job_info[idx].base_sp[lvl_idx] = atoi(fields[j+4]);
  10394. //Tells if this SP is lower than previous level (but not for 99->100)
  10395. if (lvl_idx-1 >= 0 && lvl_idx != 99 && job_info[idx].base_sp[lvl_idx] < job_info[idx].base_sp[lvl_idx-1])
  10396. ShowInfo("pc_readdb_job_basehpsp: SP value at entry %d col %d is lower than previous level (job=%d,lvl=%d,oldval=%d,val=%d).\n",
  10397. current,j+4,job_id,lvl_idx+1,job_info[idx].base_sp[lvl_idx-1],job_info[idx].base_sp[lvl_idx]);
  10398. }
  10399. }
  10400. }
  10401. }
  10402. return true;
  10403. }
  10404. #endif
  10405. /** [Cydh]
  10406. * Reads 'job_param_db.txt' to check max. param each job and store them to job_info[].max_param.*
  10407. */
  10408. static bool pc_readdb_job_param(char* fields[], int columns, int current)
  10409. {
  10410. int64 class_tmp;
  10411. int idx, class_;
  10412. uint16 str, agi, vit, int_, dex, luk;
  10413. script_get_constant(trim(fields[0]),&class_tmp);
  10414. class_ = static_cast<int>(class_tmp);
  10415. if ((idx = pc_class2idx(class_)) < 0) {
  10416. ShowError("pc_readdb_job_param: Invalid job '%s'. Skipping!",fields[0]);
  10417. return false;
  10418. }
  10419. str = cap_value(atoi(fields[1]),10,SHRT_MAX);
  10420. agi = atoi(fields[2]) ? cap_value(atoi(fields[2]),10,SHRT_MAX) : str;
  10421. vit = atoi(fields[3]) ? cap_value(atoi(fields[3]),10,SHRT_MAX) : str;
  10422. int_ = atoi(fields[4]) ? cap_value(atoi(fields[4]),10,SHRT_MAX) : str;
  10423. dex = atoi(fields[5]) ? cap_value(atoi(fields[5]),10,SHRT_MAX) : str;
  10424. luk = atoi(fields[6]) ? cap_value(atoi(fields[6]),10,SHRT_MAX) : str;
  10425. job_info[idx].max_param.str = str;
  10426. job_info[idx].max_param.agi = agi;
  10427. job_info[idx].max_param.vit = vit;
  10428. job_info[idx].max_param.int_ = int_;
  10429. job_info[idx].max_param.dex = dex;
  10430. job_info[idx].max_param.luk = luk;
  10431. return true;
  10432. }
  10433. /**
  10434. * Read job_noenter_map.txt
  10435. **/
  10436. static bool pc_readdb_job_noenter_map(char *str[], int columns, int current) {
  10437. int idx, class_ = -1;
  10438. int64 class_tmp;
  10439. if (ISDIGIT(str[0][0])) {
  10440. class_ = atoi(str[0]);
  10441. } else {
  10442. if (!script_get_constant(str[0], &class_tmp)) {
  10443. ShowError("pc_readdb_job_noenter_map: Invalid job %s specified.\n", str[0]);
  10444. return false;
  10445. }
  10446. class_ = static_cast<int>(class_tmp);
  10447. }
  10448. if (!pcdb_checkid(class_) || (idx = pc_class2idx(class_)) < 0) {
  10449. ShowError("pc_readdb_job_noenter_map: Invalid job %d specified.\n", class_);
  10450. return false;
  10451. }
  10452. job_info[idx].noenter_map.zone = atoi(str[1]);
  10453. job_info[idx].noenter_map.group_lv = atoi(str[2]);
  10454. return true;
  10455. }
  10456. static int pc_read_statsdb(const char *basedir, int last_s, bool silent){
  10457. int i=1;
  10458. char line[24000]; //FIXME this seem too big
  10459. FILE *fp;
  10460. sprintf(line, "%s/statpoint.txt", basedir);
  10461. fp=fopen(line,"r");
  10462. if(fp == NULL){
  10463. if(silent==0) ShowWarning("Can't read '" CL_WHITE "%s" CL_RESET "'... Generating DB.\n",line);
  10464. return max(last_s,i);
  10465. } else {
  10466. int entries=0;
  10467. while(fgets(line, sizeof(line), fp))
  10468. {
  10469. int stat;
  10470. trim(line);
  10471. if(line[0] == '\0' || (line[0]=='/' && line[1]=='/'))
  10472. continue;
  10473. if ((stat=strtoul(line,NULL,10))<0)
  10474. stat=0;
  10475. if (i > MAX_LEVEL)
  10476. break;
  10477. statp[i]=stat;
  10478. i++;
  10479. entries++;
  10480. }
  10481. fclose(fp);
  10482. ShowStatus("Done reading '" CL_WHITE "%d" CL_RESET "' entries in '" CL_WHITE "%s/%s" CL_RESET "'.\n", entries, basedir,"statpoint.txt");
  10483. }
  10484. return max(last_s,i);
  10485. }
  10486. /*==========================================
  10487. * pc DB reading.
  10488. * job_exp.txt - required experience values
  10489. * skill_tree.txt - skill tree for every class
  10490. * attr_fix.txt - elemental adjustment table
  10491. * job_db1.txt - job,weight,hp_factor,hp_multiplicator,sp_factor,aspds/lvl
  10492. * job_db2.txt - job,stats bonuses/lvl
  10493. * job_maxhpsp_db.txt - strtlvl,maxlvl,job,type,values/lvl (values=hp|sp)
  10494. *------------------------------------------*/
  10495. void pc_readdb(void) {
  10496. int i, k, s = 1;
  10497. const char* dbsubpath[] = {
  10498. "",
  10499. "/" DBIMPORT,
  10500. //add other path here
  10501. };
  10502. //reset
  10503. memset(job_info,0,sizeof(job_info)); // job_info table
  10504. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  10505. sv_readdb(db_path, DBPATH "level_penalty.txt", ',', 4, 4, -1, &pc_readdb_levelpenalty, 0);
  10506. sv_readdb(db_path, DBIMPORT"/level_penalty.txt", ',', 4, 4, -1, &pc_readdb_levelpenalty, 1);
  10507. for( k=1; k < 3; k++ ){ // fill in the blanks
  10508. int j;
  10509. for( j = 0; j < CLASS_ALL; j++ ){
  10510. int tmp = 0;
  10511. for( i = 0; i < MAX_LEVEL*2; i++ ){
  10512. if( i == MAX_LEVEL+1 )
  10513. tmp = level_penalty[k][j][0];// reset
  10514. if( level_penalty[k][j][i] > 0 )
  10515. tmp = level_penalty[k][j][i];
  10516. else
  10517. level_penalty[k][j][i] = tmp;
  10518. }
  10519. }
  10520. }
  10521. #endif
  10522. // reset then read statspoint
  10523. memset(statp,0,sizeof(statp));
  10524. for(i=0; i<ARRAYLENGTH(dbsubpath); i++){
  10525. uint8 n1 = (uint8)(strlen(db_path)+strlen(dbsubpath[i])+1);
  10526. uint8 n2 = (uint8)(strlen(db_path)+strlen(DBPATH)+strlen(dbsubpath[i])+1);
  10527. char* dbsubpath1 = (char*)aMalloc(n1+1);
  10528. char* dbsubpath2 = (char*)aMalloc(n2+1);
  10529. if(i==0) {
  10530. safesnprintf(dbsubpath1,n1,"%s%s",db_path,dbsubpath[i]);
  10531. safesnprintf(dbsubpath2,n2,"%s/%s%s",db_path,DBPATH,dbsubpath[i]);
  10532. }
  10533. else {
  10534. safesnprintf(dbsubpath1,n1,"%s%s",db_path,dbsubpath[i]);
  10535. safesnprintf(dbsubpath2,n1,"%s%s",db_path,dbsubpath[i]);
  10536. }
  10537. s = pc_read_statsdb(dbsubpath2,s,i > 0);
  10538. if (i == 0)
  10539. #ifdef RENEWAL_ASPD // Paths are hardcoded here to specifically pick the correct database
  10540. sv_readdb(dbsubpath1, "re/job_db1.txt",',',6+MAX_WEAPON_TYPE,6+MAX_WEAPON_TYPE,CLASS_COUNT,&pc_readdb_job1, false);
  10541. #else
  10542. sv_readdb(dbsubpath1, "pre-re/job_db1.txt",',',5+MAX_WEAPON_TYPE,5+MAX_WEAPON_TYPE,CLASS_COUNT,&pc_readdb_job1, false);
  10543. #endif
  10544. else
  10545. sv_readdb(dbsubpath1, "job_db1.txt",',',5+MAX_WEAPON_TYPE,6+MAX_WEAPON_TYPE,CLASS_COUNT,&pc_readdb_job1, true);
  10546. sv_readdb(dbsubpath1, "job_db2.txt",',',1,1+MAX_LEVEL,CLASS_COUNT,&pc_readdb_job2, i > 0);
  10547. sv_readdb(dbsubpath2, "job_exp.txt",',',4,1000+3,CLASS_COUNT*2,&pc_readdb_job_exp, i > 0); //support till 1000lvl
  10548. #ifdef HP_SP_TABLES
  10549. sv_readdb(dbsubpath2, "job_basehpsp_db.txt", ',', 4, 4+500, CLASS_COUNT*2, &pc_readdb_job_basehpsp, i > 0); //Make it support until lvl 500!
  10550. #endif
  10551. sv_readdb(dbsubpath2, "job_param_db.txt", ',', 2, PARAM_MAX+1, CLASS_COUNT, &pc_readdb_job_param, i > 0);
  10552. sv_readdb(dbsubpath2, "job_noenter_map.txt", ',', 3, 3, CLASS_COUNT, &pc_readdb_job_noenter_map, i > 0);
  10553. aFree(dbsubpath1);
  10554. aFree(dbsubpath2);
  10555. }
  10556. // Reset and read skilltree - needs to be read after pc_readdb_job_exp to get max base and job levels
  10557. memset(skill_tree, 0, sizeof(skill_tree));
  10558. sv_readdb(db_path, DBPATH"skill_tree.txt", ',', 3 + MAX_PC_SKILL_REQUIRE * 2, 5 + MAX_PC_SKILL_REQUIRE * 2, -1, &pc_readdb_skilltree, 0);
  10559. sv_readdb(db_path, DBIMPORT"/skill_tree.txt", ',', 3 + MAX_PC_SKILL_REQUIRE * 2, 5 + MAX_PC_SKILL_REQUIRE * 2, -1, &pc_readdb_skilltree, 1);
  10560. // generate the remaining parts of the db if necessary
  10561. k = battle_config.use_statpoint_table; //save setting
  10562. battle_config.use_statpoint_table = 0; //temporarily disable to force pc_gets_status_point use default values
  10563. statp[0] = 45; // seed value
  10564. for (; s <= MAX_LEVEL; s++)
  10565. statp[s] = statp[s-1] + pc_gets_status_point(s-1);
  10566. battle_config.use_statpoint_table = k; //restore setting
  10567. //Checking if all class have their data
  10568. for (i = 0; i < JOB_MAX; i++) {
  10569. int idx;
  10570. uint16 j;
  10571. if (!pcdb_checkid(i))
  10572. continue;
  10573. if (i == JOB_WEDDING || i == JOB_XMAS || i == JOB_SUMMER || i == JOB_HANBOK || i == JOB_OKTOBERFEST || i == JOB_SUMMER2)
  10574. continue; //Classes that do not need exp tables.
  10575. idx = pc_class2idx(i);
  10576. if (!job_info[idx].max_level[0])
  10577. ShowWarning("Class %s (%d) does not have a base exp table.\n", job_name(i), i);
  10578. if (!job_info[idx].max_level[1])
  10579. ShowWarning("Class %s (%d) does not have a job exp table.\n", job_name(i), i);
  10580. //Init and checking the empty value of Base HP/SP [Cydh]
  10581. for (j = 0; j < (job_info[idx].max_level[0] ? job_info[idx].max_level[0] : MAX_LEVEL); j++) {
  10582. if (job_info[idx].base_hp[j] == 0)
  10583. job_info[idx].base_hp[j] = pc_calc_basehp(j+1,i);
  10584. if (job_info[idx].base_sp[j] == 0)
  10585. job_info[idx].base_sp[j] = pc_calc_basesp(j+1,i);
  10586. }
  10587. }
  10588. }
  10589. // Read MOTD on startup. [Valaris]
  10590. int pc_read_motd(void)
  10591. {
  10592. FILE* fp;
  10593. // clear old MOTD
  10594. memset(motd_text, 0, sizeof(motd_text));
  10595. // read current MOTD
  10596. if( ( fp = fopen(motd_txt, "r") ) != NULL )
  10597. {
  10598. unsigned int entries = 0;
  10599. char buf[CHAT_SIZE_MAX];
  10600. while( entries < MOTD_LINE_SIZE && fgets(buf, CHAT_SIZE_MAX, fp) )
  10601. {
  10602. unsigned int lines = 0;
  10603. size_t len;
  10604. lines++;
  10605. if( buf[0] == '/' && buf[1] == '/' )
  10606. continue;
  10607. len = strlen(buf);
  10608. while( len && ( buf[len-1] == '\r' || buf[len-1] == '\n' ) ) // strip trailing EOL characters
  10609. len--;
  10610. if( len ) {
  10611. char * ptr;
  10612. buf[len] = 0;
  10613. if( ( ptr = strstr(buf, " :") ) != NULL && ptr-buf >= NAME_LENGTH ) // crashes newer clients
  10614. 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);
  10615. }
  10616. else {// empty line
  10617. buf[0] = ' ';
  10618. buf[1] = 0;
  10619. }
  10620. safestrncpy(motd_text[entries], buf, CHAT_SIZE_MAX);
  10621. entries++;
  10622. }
  10623. fclose(fp);
  10624. ShowStatus("Done reading '" CL_WHITE "%u" CL_RESET "' entries in '" CL_WHITE "%s" CL_RESET "'.\n", entries, motd_txt);
  10625. }
  10626. else
  10627. ShowWarning("File '" CL_WHITE "%s" CL_RESET "' not found.\n", motd_txt);
  10628. return 0;
  10629. }
  10630. void pc_itemcd_do(struct map_session_data *sd, bool load) {
  10631. int i,cursor = 0;
  10632. struct item_cd* cd = NULL;
  10633. if( load ) {
  10634. if( !(cd = (struct item_cd*)idb_get(itemcd_db, sd->status.char_id)) ) {
  10635. // no item cooldown is associated with this character
  10636. return;
  10637. }
  10638. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  10639. if( cd->nameid[i] && DIFF_TICK(gettick(),cd->tick[i]) < 0 ) {
  10640. sd->item_delay[cursor].tick = cd->tick[i];
  10641. sd->item_delay[cursor].nameid = cd->nameid[i];
  10642. cursor++;
  10643. }
  10644. }
  10645. idb_remove(itemcd_db,sd->status.char_id);
  10646. } else {
  10647. if( !(cd = (struct item_cd*)idb_get(itemcd_db,sd->status.char_id)) ) {
  10648. // create a new skill cooldown object for map storage
  10649. CREATE( cd, struct item_cd, 1 );
  10650. idb_put( itemcd_db, sd->status.char_id, cd );
  10651. }
  10652. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  10653. if( sd->item_delay[i].nameid && DIFF_TICK(gettick(),sd->item_delay[i].tick) < 0 ) {
  10654. cd->tick[cursor] = sd->item_delay[i].tick;
  10655. cd->nameid[cursor] = sd->item_delay[i].nameid;
  10656. cursor++;
  10657. }
  10658. }
  10659. }
  10660. return;
  10661. }
  10662. /**
  10663. * Add item delay to player's item delay data
  10664. * @param sd Player
  10665. * @param id Item data
  10666. * @param tick Current tick
  10667. * @param n Item index in inventory
  10668. * @return 0: No delay, can consume item.
  10669. * 1: Has delay, cancel consumption.
  10670. **/
  10671. uint8 pc_itemcd_add(struct map_session_data *sd, struct item_data *id, t_tick tick, unsigned short n) {
  10672. int i;
  10673. ARR_FIND(0, MAX_ITEMDELAYS, i, sd->item_delay[i].nameid == id->nameid );
  10674. if( i == MAX_ITEMDELAYS ) /* item not found. try first empty now */
  10675. ARR_FIND(0, MAX_ITEMDELAYS, i, !sd->item_delay[i].nameid );
  10676. if( i < MAX_ITEMDELAYS ) {
  10677. if( sd->item_delay[i].nameid ) {// found
  10678. if( DIFF_TICK(sd->item_delay[i].tick, tick) > 0 ) {
  10679. t_tick e_tick = DIFF_TICK(sd->item_delay[i].tick, tick)/1000;
  10680. char e_msg[CHAT_SIZE_MAX];
  10681. if( e_tick > 99 )
  10682. sprintf(e_msg,msg_txt(sd,379), // Item Failed. [%s] is cooling down. Wait %.1f minutes.
  10683. itemdb_jname(sd->item_delay[i].nameid), (double)e_tick / 60);
  10684. else
  10685. sprintf(e_msg,msg_txt(sd,380), // Item Failed. [%s] is cooling down. Wait %d seconds.
  10686. itemdb_jname(sd->item_delay[i].nameid), e_tick+1);
  10687. clif_messagecolor(&sd->bl,color_table[COLOR_YELLOW],e_msg,false,SELF);
  10688. return 1; // Delay has not expired yet
  10689. }
  10690. } else {// not yet used item (all slots are initially empty)
  10691. sd->item_delay[i].nameid = id->nameid;
  10692. }
  10693. if( !(id->nameid == ITEMID_REINS_OF_MOUNT && sd->sc.option&(OPTION_WUGRIDER|OPTION_RIDING|OPTION_DRAGON|OPTION_MADOGEAR)) )
  10694. sd->item_delay[i].tick = tick + sd->inventory_data[n]->delay;
  10695. } else {// should not happen
  10696. ShowError("pc_itemcd_add: Exceeded item delay array capacity! (nameid=%hu, char_id=%d)\n", id->nameid, sd->status.char_id);
  10697. }
  10698. //clean up used delays so we can give room for more
  10699. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  10700. if( DIFF_TICK(sd->item_delay[i].tick, tick) <= 0 ) {
  10701. sd->item_delay[i].tick = 0;
  10702. sd->item_delay[i].nameid = 0;
  10703. }
  10704. }
  10705. return 0;
  10706. }
  10707. /**
  10708. * Check if player has delay to reuse item
  10709. * @param sd Player
  10710. * @param id Item data
  10711. * @param tick Current tick
  10712. * @param n Item index in inventory
  10713. * @return 0: No delay, can consume item.
  10714. * 1: Has delay, cancel consumption.
  10715. **/
  10716. uint8 pc_itemcd_check(struct map_session_data *sd, struct item_data *id, t_tick tick, unsigned short n) {
  10717. struct status_change *sc = NULL;
  10718. nullpo_retr(0, sd);
  10719. nullpo_retr(0, id);
  10720. // Do normal delay assignment
  10721. if (id->delay_sc <= SC_NONE || id->delay_sc >= SC_MAX || !(sc = &sd->sc))
  10722. return pc_itemcd_add(sd, id, tick, n);
  10723. // Send reply of delay remains
  10724. if (sc->data[id->delay_sc]) {
  10725. const struct TimerData *timer = get_timer(sc->data[id->delay_sc]->timer);
  10726. clif_msg_value(sd, ITEM_REUSE_LIMIT, (int)(timer ? DIFF_TICK(timer->tick, tick) / 1000 : 99));
  10727. return 1;
  10728. }
  10729. sc_start(&sd->bl, &sd->bl, (sc_type)id->delay_sc, 100, id->nameid, id->delay);
  10730. return 0;
  10731. }
  10732. /**
  10733. * Clear the dmglog data from player
  10734. * @param sd
  10735. * @param md
  10736. **/
  10737. static void pc_clear_log_damage_sub(uint32 char_id, struct mob_data *md)
  10738. {
  10739. uint8 i;
  10740. ARR_FIND(0,DAMAGELOG_SIZE,i,md->dmglog[i].id == char_id);
  10741. if (i < DAMAGELOG_SIZE) {
  10742. md->dmglog[i].id = 0;
  10743. md->dmglog[i].dmg = 0;
  10744. md->dmglog[i].flag = 0;
  10745. }
  10746. }
  10747. /**
  10748. * Add log to player's dmglog
  10749. * @param sd
  10750. * @param id Monster's GID
  10751. **/
  10752. void pc_damage_log_add(struct map_session_data *sd, int id)
  10753. {
  10754. uint8 i = 0;
  10755. if (!sd || !id)
  10756. return;
  10757. //Only store new data, don't need to renew the old one with same id
  10758. ARR_FIND(0, DAMAGELOG_SIZE_PC, i, sd->dmglog[i] == id);
  10759. if (i < DAMAGELOG_SIZE_PC)
  10760. return;
  10761. for (i = 0; i < DAMAGELOG_SIZE_PC; i++) {
  10762. if (sd->dmglog[i] == 0) {
  10763. sd->dmglog[i] = id;
  10764. return;
  10765. }
  10766. }
  10767. }
  10768. /**
  10769. * Clear dmglog data from player
  10770. * @param sd
  10771. * @param id Monster's id
  10772. **/
  10773. void pc_damage_log_clear(struct map_session_data *sd, int id)
  10774. {
  10775. uint8 i;
  10776. struct mob_data *md = NULL;
  10777. if (!sd)
  10778. return;
  10779. if (!id) {
  10780. for (i = 0; i < DAMAGELOG_SIZE_PC; i++) {
  10781. if( !sd->dmglog[i] ) //skip the empty value
  10782. continue;
  10783. if ((md = map_id2md(sd->dmglog[i])))
  10784. pc_clear_log_damage_sub(sd->status.char_id,md);
  10785. sd->dmglog[i] = 0;
  10786. }
  10787. }
  10788. else {
  10789. if ((md = map_id2md(id)))
  10790. pc_clear_log_damage_sub(sd->status.char_id,md);
  10791. ARR_FIND(0,DAMAGELOG_SIZE_PC,i,sd->dmglog[i] == id); // find the id position
  10792. if (i < DAMAGELOG_SIZE_PC)
  10793. sd->dmglog[i] = 0;
  10794. }
  10795. }
  10796. /**
  10797. * Status change data arrived from char-server
  10798. * @param sd: Player data
  10799. */
  10800. void pc_scdata_received(struct map_session_data *sd) {
  10801. pc_inventory_rentals(sd); // Needed here to remove rentals that have Status Changes after chrif_load_scdata has finished
  10802. clif_weight_limit( sd );
  10803. if( pc_has_permission( sd, PC_PERM_ATTENDANCE ) && pc_attendance_enabled() && !pc_attendance_rewarded_today( sd ) ){
  10804. clif_ui_open( sd, OUT_UI_ATTENDANCE, pc_attendance_counter( sd ) );
  10805. }
  10806. sd->state.pc_loaded = true;
  10807. if (sd->state.connect_new == 0 && sd->fd) { // Character already loaded map! Gotta trigger LoadEndAck manually.
  10808. sd->state.connect_new = 1;
  10809. clif_parse_LoadEndAck(sd->fd, sd);
  10810. }
  10811. if (pc_iscarton(sd)) {
  10812. sd->cart_weight_max = 0; // Force a client refesh
  10813. status_calc_cart_weight(sd, (e_status_calc_weight_opt)(CALCWT_ITEM|CALCWT_MAXBONUS|CALCWT_CARTSTATE));
  10814. }
  10815. }
  10816. /**
  10817. * Check player account expiration time and rental item expirations
  10818. * @param sd: Player data
  10819. */
  10820. void pc_check_expiration(struct map_session_data *sd) {
  10821. #ifndef ENABLE_SC_SAVING
  10822. pc_inventory_rentals(sd); // Check here if Status Change saving is disabled
  10823. #endif
  10824. if (sd->expiration_time != 0) { //Don't display if it's unlimited or unknow value
  10825. time_t exp_time = sd->expiration_time;
  10826. char tmpstr[1024];
  10827. strftime(tmpstr,sizeof(tmpstr) - 1,msg_txt(sd,501),localtime(&exp_time)); // "Your account time limit is: %d-%m-%Y %H:%M:%S."
  10828. clif_wis_message(sd,wisp_server_name,tmpstr,strlen(tmpstr) + 1,0);
  10829. pc_expire_check(sd);
  10830. }
  10831. }
  10832. TIMER_FUNC(pc_expiration_timer){
  10833. struct map_session_data *sd = map_id2sd(id);
  10834. if( !sd ) return 0;
  10835. sd->expiration_tid = INVALID_TIMER;
  10836. if( sd->fd )
  10837. clif_authfail_fd(sd->fd,10);
  10838. map_quit(sd);
  10839. return 0;
  10840. }
  10841. TIMER_FUNC(pc_autotrade_timer){
  10842. struct map_session_data *sd = map_id2sd(id);
  10843. if (!sd)
  10844. return 0;
  10845. sd->autotrade_tid = INVALID_TIMER;
  10846. if (sd->state.autotrade&2)
  10847. vending_reopen(sd);
  10848. if (sd->state.autotrade&4)
  10849. buyingstore_reopen(sd);
  10850. if (!sd->vender_id && !sd->buyer_id) {
  10851. sd->state.autotrade = 0;
  10852. map_quit(sd);
  10853. }
  10854. return 0;
  10855. }
  10856. /* this timer exists only when a character with a expire timer > 24h is online */
  10857. /* it loops thru online players once an hour to check whether a new < 24h is available */
  10858. TIMER_FUNC(pc_global_expiration_timer){
  10859. struct s_mapiterator* iter;
  10860. struct map_session_data* sd;
  10861. iter = mapit_getallusers();
  10862. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  10863. if( sd->expiration_time )
  10864. pc_expire_check(sd);
  10865. mapit_free(iter);
  10866. return 0;
  10867. }
  10868. void pc_expire_check(struct map_session_data *sd) {
  10869. /* ongoing timer */
  10870. if( sd->expiration_tid != INVALID_TIMER )
  10871. return;
  10872. /* not within the next 24h, enable the global check */
  10873. if( sd->expiration_time > (time(NULL) + ((60 * 60) * 24)) ) {
  10874. /* global check not running, enable */
  10875. if( pc_expiration_tid == INVALID_TIMER ) /* Starts in 1h, repeats every hour */
  10876. pc_expiration_tid = add_timer_interval(gettick() + ((1000 * 60) * 60), pc_global_expiration_timer, 0, 0, ((1000 * 60) * 60));
  10877. return;
  10878. }
  10879. sd->expiration_tid = add_timer(gettick() + (unsigned int)(sd->expiration_time - time(NULL)) * 1000, pc_expiration_timer, sd->bl.id, 0);
  10880. }
  10881. /**
  10882. * Deposit some money to bank
  10883. * @param sd
  10884. * @param money Amount of money to deposit
  10885. **/
  10886. enum e_BANKING_DEPOSIT_ACK pc_bank_deposit(struct map_session_data *sd, int money) {
  10887. unsigned int limit_check = money + sd->bank_vault;
  10888. if( money <= 0 || limit_check > MAX_BANK_ZENY ) {
  10889. return BDA_OVERFLOW;
  10890. } else if ( money > sd->status.zeny ) {
  10891. return BDA_NO_MONEY;
  10892. }
  10893. if( pc_payzeny(sd,money, LOG_TYPE_BANK, NULL) )
  10894. return BDA_NO_MONEY;
  10895. sd->bank_vault += money;
  10896. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  10897. if( save_settings&CHARSAVE_BANK )
  10898. chrif_save(sd, CSAVE_NORMAL);
  10899. return BDA_SUCCESS;
  10900. }
  10901. /**
  10902. * Withdraw money from bank
  10903. * @param sd
  10904. * @param money Amount of money that will be withdrawn
  10905. **/
  10906. enum e_BANKING_WITHDRAW_ACK pc_bank_withdraw(struct map_session_data *sd, int money) {
  10907. unsigned int limit_check = money + sd->status.zeny;
  10908. if( money <= 0 ) {
  10909. return BWA_UNKNOWN_ERROR;
  10910. } else if ( money > sd->bank_vault ) {
  10911. return BWA_NO_MONEY;
  10912. } else if ( limit_check > MAX_ZENY ) {
  10913. /* no official response for this scenario exists. */
  10914. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,1495),false,SELF); //You can't withdraw that much money
  10915. return BWA_UNKNOWN_ERROR;
  10916. }
  10917. if( pc_getzeny(sd,money, LOG_TYPE_BANK, NULL) )
  10918. return BWA_NO_MONEY;
  10919. sd->bank_vault -= money;
  10920. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  10921. if( save_settings&CHARSAVE_BANK )
  10922. chrif_save(sd, CSAVE_NORMAL);
  10923. return BWA_SUCCESS;
  10924. }
  10925. /**
  10926. * Clear Crimson Marker data from caster
  10927. * @param sd: Player
  10928. **/
  10929. void pc_crimson_marker_clear(struct map_session_data *sd) {
  10930. uint8 i;
  10931. if (!sd)
  10932. return;
  10933. for (i = 0; i < MAX_SKILL_CRIMSON_MARKER; i++) {
  10934. struct block_list *bl = NULL;
  10935. if (sd->c_marker[i] && (bl = map_id2bl(sd->c_marker[i])))
  10936. status_change_end(bl,SC_C_MARKER,INVALID_TIMER);
  10937. sd->c_marker[i] = 0;
  10938. }
  10939. }
  10940. /**
  10941. * Show version to player
  10942. * @param sd: Player
  10943. **/
  10944. void pc_show_version(struct map_session_data *sd) {
  10945. const char* svn = get_svn_revision();
  10946. char buf[CHAT_SIZE_MAX];
  10947. if( svn[0] != UNKNOWN_VERSION )
  10948. sprintf(buf,msg_txt(sd,1295),"SVN: r",svn); //rAthena Version SVN: r%s
  10949. else {
  10950. const char* git = get_git_hash();
  10951. if( git[0] != UNKNOWN_VERSION )
  10952. sprintf(buf,msg_txt(sd,1295),"Git Hash: ",git); //rAthena Version Git Hash: %s
  10953. else
  10954. sprintf(buf,"%s",msg_txt(sd,1296)); //Cannot determine SVN/Git version.
  10955. }
  10956. clif_displaymessage(sd->fd,buf);
  10957. }
  10958. /**
  10959. * Run bonus_script on player
  10960. * @param sd
  10961. * @author [Cydh]
  10962. **/
  10963. void pc_bonus_script(struct map_session_data *sd) {
  10964. t_tick now = gettick();
  10965. struct linkdb_node *node = NULL, *next = NULL;
  10966. if (!sd || !(node = sd->bonus_script.head))
  10967. return;
  10968. while (node) {
  10969. struct s_bonus_script_entry *entry = NULL;
  10970. next = node->next;
  10971. if ((entry = (struct s_bonus_script_entry *)node->data)) {
  10972. // Only start timer for new bonus_script
  10973. if (entry->tid == INVALID_TIMER) {
  10974. if (entry->icon != EFST_BLANK) // Gives status icon if exist
  10975. clif_status_change(&sd->bl, entry->icon, 1, entry->tick, 1, 0, 0);
  10976. entry->tick += now;
  10977. entry->tid = add_timer(entry->tick, pc_bonus_script_timer, sd->bl.id, (intptr_t)entry);
  10978. }
  10979. if (entry->script)
  10980. run_script(entry->script, 0, sd->bl.id, 0);
  10981. else
  10982. ShowError("pc_bonus_script: The script has been removed somewhere. \"%s\"\n", StringBuf_Value(entry->script_buf));
  10983. }
  10984. node = next;
  10985. }
  10986. }
  10987. /**
  10988. * Add bonus_script to player
  10989. * @param sd Player
  10990. * @param script_str Script string
  10991. * @param dur Duration in ms
  10992. * @param icon EFST
  10993. * @param flag Flags @see enum e_bonus_script_flags
  10994. * @param type 0 - None, 1 - Buff, 2 - Debuff
  10995. * @return New created entry pointer or NULL if failed or NULL if duplicate fail
  10996. * @author [Cydh]
  10997. **/
  10998. struct s_bonus_script_entry *pc_bonus_script_add(struct map_session_data *sd, const char *script_str, t_tick dur, enum efst_types icon, uint16 flag, uint8 type) {
  10999. struct script_code *script = NULL;
  11000. struct linkdb_node *node = NULL;
  11001. struct s_bonus_script_entry *entry = NULL;
  11002. if (!sd)
  11003. return NULL;
  11004. if (!(script = parse_script(script_str, "bonus_script", 0, SCRIPT_IGNORE_EXTERNAL_BRACKETS))) {
  11005. ShowError("pc_bonus_script_add: Failed to parse script '%s' (CID:%d).\n", script_str, sd->status.char_id);
  11006. return NULL;
  11007. }
  11008. // Duplication checks
  11009. if ((node = sd->bonus_script.head)) {
  11010. while (node) {
  11011. entry = (struct s_bonus_script_entry *)node->data;
  11012. if (strcmpi(script_str, StringBuf_Value(entry->script_buf)) == 0) {
  11013. t_tick newdur = gettick() + dur;
  11014. if (flag&BSF_FORCE_REPLACE && entry->tick < newdur) { // Change duration
  11015. sett_tickimer(entry->tid, newdur);
  11016. script_free_code(script);
  11017. return NULL;
  11018. }
  11019. else if (flag&BSF_FORCE_DUPLICATE) // Allow duplicate
  11020. break;
  11021. else { // No duplicate bonus
  11022. script_free_code(script);
  11023. return NULL;
  11024. }
  11025. }
  11026. node = node->next;
  11027. }
  11028. }
  11029. CREATE(entry, struct s_bonus_script_entry, 1);
  11030. entry->script_buf = StringBuf_Malloc();
  11031. StringBuf_AppendStr(entry->script_buf, script_str);
  11032. entry->tid = INVALID_TIMER;
  11033. entry->flag = flag;
  11034. entry->icon = icon;
  11035. entry->tick = dur; // Use duration first, on run change to expire time
  11036. entry->type = type;
  11037. entry->script = script;
  11038. sd->bonus_script.count++;
  11039. return entry;
  11040. }
  11041. /**
  11042. * Remove bonus_script data from player
  11043. * @param sd: Target player
  11044. * @param list: Bonus script entry from player
  11045. * @author [Cydh]
  11046. **/
  11047. void pc_bonus_script_free_entry(struct map_session_data *sd, struct s_bonus_script_entry *entry) {
  11048. if (entry->tid != INVALID_TIMER)
  11049. delete_timer(entry->tid, pc_bonus_script_timer);
  11050. if (entry->script)
  11051. script_free_code(entry->script);
  11052. if (entry->script_buf)
  11053. StringBuf_Free(entry->script_buf);
  11054. if (sd) {
  11055. if (entry->icon != EFST_BLANK)
  11056. clif_status_load(&sd->bl, entry->icon, 0);
  11057. if (sd->bonus_script.count > 0)
  11058. sd->bonus_script.count--;
  11059. }
  11060. aFree(entry);
  11061. }
  11062. /**
  11063. * Do final process if no entry left
  11064. * @param sd
  11065. **/
  11066. static void inline pc_bonus_script_check_final(struct map_session_data *sd) {
  11067. if (sd->bonus_script.count == 0) {
  11068. if (sd->bonus_script.head && sd->bonus_script.head->data)
  11069. pc_bonus_script_free_entry(sd, (struct s_bonus_script_entry *)sd->bonus_script.head->data);
  11070. linkdb_final(&sd->bonus_script.head);
  11071. }
  11072. }
  11073. /**
  11074. * Timer for bonus_script
  11075. * @param tid
  11076. * @param tick
  11077. * @param id
  11078. * @param data
  11079. * @author [Cydh]
  11080. **/
  11081. TIMER_FUNC(pc_bonus_script_timer){
  11082. struct map_session_data *sd;
  11083. struct s_bonus_script_entry *entry = (struct s_bonus_script_entry *)data;
  11084. sd = map_id2sd(id);
  11085. if (!sd) {
  11086. ShowError("pc_bonus_script_timer: Null pointer id: %d tid: %d\n", id, tid);
  11087. return 0;
  11088. }
  11089. if (tid == INVALID_TIMER)
  11090. return 0;
  11091. if (!sd->bonus_script.head || entry == NULL) {
  11092. ShowError("pc_bonus_script_timer: Invalid entry pointer %p!\n", entry);
  11093. return 0;
  11094. }
  11095. linkdb_erase(&sd->bonus_script.head, (void *)((intptr_t)entry));
  11096. pc_bonus_script_free_entry(sd, entry);
  11097. pc_bonus_script_check_final(sd);
  11098. status_calc_pc(sd,SCO_NONE);
  11099. return 0;
  11100. }
  11101. /**
  11102. * Check then clear all active timer(s) of bonus_script data from player based on reason
  11103. * @param sd: Target player
  11104. * @param flag: Reason to remove the bonus_script. e_bonus_script_flags or e_bonus_script_types
  11105. * @author [Cydh]
  11106. **/
  11107. void pc_bonus_script_clear(struct map_session_data *sd, uint16 flag) {
  11108. struct linkdb_node *node = NULL;
  11109. uint16 count = 0;
  11110. if (!sd || !(node = sd->bonus_script.head))
  11111. return;
  11112. while (node) {
  11113. struct linkdb_node *next = node->next;
  11114. struct s_bonus_script_entry *entry = (struct s_bonus_script_entry *)node->data;
  11115. if (entry && (
  11116. (flag == BSF_PERMANENT) || // Remove all with permanent bonus
  11117. (!flag && !(entry->flag&BSF_PERMANENT)) || // Remove all WITHOUT permanent bonus
  11118. (flag&entry->flag) || // Matched flag
  11119. (flag&BSF_REM_BUFF && entry->type == 1) || // Remove buff
  11120. (flag&BSF_REM_DEBUFF && entry->type == 2) // Remove debuff
  11121. )
  11122. )
  11123. {
  11124. linkdb_erase(&sd->bonus_script.head, (void *)((intptr_t)entry));
  11125. pc_bonus_script_free_entry(sd, entry);
  11126. count++;
  11127. }
  11128. node = next;
  11129. }
  11130. pc_bonus_script_check_final(sd);
  11131. if (count && !(flag&BSF_REM_ON_LOGOUT)) //Don't need to do this if log out
  11132. status_calc_pc(sd,SCO_NONE);
  11133. }
  11134. /** [Cydh]
  11135. * Gives/removes SC_BASILICA when player steps in/out the cell with 'cell_basilica'
  11136. * @param sd: Target player
  11137. */
  11138. void pc_cell_basilica(struct map_session_data *sd) {
  11139. nullpo_retv(sd);
  11140. if (!map_getcell(sd->bl.m,sd->bl.x,sd->bl.y,CELL_CHKBASILICA)) {
  11141. if (sd->sc.data[SC_BASILICA])
  11142. status_change_end(&sd->bl,SC_BASILICA,INVALID_TIMER);
  11143. }
  11144. else if (!sd->sc.data[SC_BASILICA])
  11145. sc_start(&sd->bl,&sd->bl,SC_BASILICA,100,0,INFINITE_TICK);
  11146. }
  11147. /** [Cydh]
  11148. * Get maximum specified parameter for specified class
  11149. * @param class_: sd->class
  11150. * @param sex: sd->status.sex
  11151. * @param flag: parameter will be checked
  11152. * @return max_param
  11153. */
  11154. short pc_maxparameter(struct map_session_data *sd, enum e_params param) {
  11155. int idx = -1, class_ = sd->class_;
  11156. if ((idx = pc_class2idx(pc_mapid2jobid(class_,sd->status.sex))) >= 0) {
  11157. short max_param = 0;
  11158. switch (param) {
  11159. case PARAM_STR: max_param = job_info[idx].max_param.str; break;
  11160. case PARAM_AGI: max_param = job_info[idx].max_param.agi; break;
  11161. case PARAM_VIT: max_param = job_info[idx].max_param.vit; break;
  11162. case PARAM_INT: max_param = job_info[idx].max_param.int_; break;
  11163. case PARAM_DEX: max_param = job_info[idx].max_param.dex; break;
  11164. case PARAM_LUK: max_param = job_info[idx].max_param.luk; break;
  11165. }
  11166. if (max_param > 0)
  11167. return max_param;
  11168. }
  11169. // Always check babies first
  11170. if( class_ & JOBL_BABY ){
  11171. if( class_ & JOBL_THIRD ){
  11172. return battle_config.max_baby_third_parameter;
  11173. }else{
  11174. return battle_config.max_baby_parameter;
  11175. }
  11176. }
  11177. // Summoner
  11178. if( ( class_ & MAPID_BASEMASK ) == MAPID_SUMMONER ){
  11179. return battle_config.max_summoner_parameter;
  11180. }
  11181. // Extended classes
  11182. if( ( class_ & MAPID_UPPERMASK ) == MAPID_KAGEROUOBORO || ( class_ & MAPID_UPPERMASK ) == MAPID_REBELLION ){
  11183. return battle_config.max_extended_parameter;
  11184. }
  11185. // 3rd class
  11186. if( class_ & JOBL_THIRD ){
  11187. // Transcendent
  11188. if( class_ & JOBL_UPPER ){
  11189. return battle_config.max_third_trans_parameter;
  11190. }else{
  11191. return battle_config.max_third_parameter;
  11192. }
  11193. }
  11194. // Transcendent
  11195. if( class_ & JOBL_UPPER ){
  11196. return battle_config.max_trans_parameter;
  11197. }
  11198. return battle_config.max_parameter;
  11199. }
  11200. /**
  11201. * Get max ASPD for player based on Class
  11202. * @param sd Player
  11203. * @return ASPD
  11204. */
  11205. short pc_maxaspd(struct map_session_data *sd) {
  11206. nullpo_ret(sd);
  11207. return (( sd->class_&JOBL_THIRD) ? battle_config.max_third_aspd : (
  11208. ((sd->class_&MAPID_UPPERMASK) == MAPID_KAGEROUOBORO || (sd->class_&MAPID_UPPERMASK) == MAPID_REBELLION) ? battle_config.max_extended_aspd :
  11209. battle_config.max_aspd ));
  11210. }
  11211. /**
  11212. * Calculates total item-group related bonuses for the given item
  11213. * @param sd Player
  11214. * @param nameid Item ID
  11215. * @return Heal rate
  11216. **/
  11217. short pc_get_itemgroup_bonus(struct map_session_data* sd, unsigned short nameid) {
  11218. if (sd->itemgrouphealrate.empty())
  11219. return 0;
  11220. short bonus = 0;
  11221. for (const auto &it : sd->itemgrouphealrate) {
  11222. uint16 group_id = it.id, i;
  11223. struct s_item_group_db *group = NULL;
  11224. if (!group_id || !(group = itemdb_group_exists(group_id)))
  11225. continue;
  11226. for (i = 0; i < group->random[0].data_qty; i++) {
  11227. if (group->random[0].data[i].nameid == nameid) {
  11228. bonus += it.val;
  11229. break;
  11230. }
  11231. }
  11232. }
  11233. return bonus;
  11234. }
  11235. /**
  11236. * Calculates total item-group related bonuses for the given item group
  11237. * @param sd Player
  11238. * @param group_id Item Group ID
  11239. * @return Heal rate
  11240. **/
  11241. short pc_get_itemgroup_bonus_group(struct map_session_data* sd, uint16 group_id) {
  11242. if (sd->itemgrouphealrate.empty())
  11243. return 0;
  11244. for (const auto &it : sd->itemgrouphealrate) {
  11245. if (it.id == group_id)
  11246. return it.val;
  11247. }
  11248. return 0;
  11249. }
  11250. /**
  11251. * Check if player's equip index in same specified position, like for 2-Handed weapon & Heagdear (inc. costume)
  11252. * @param eqi Item EQI of enum equip_index
  11253. * @param *equip_index Player's equip_index[]
  11254. * @param index Known index item in inventory from sd->equip_index[] to compare with specified EQI in *equip_index
  11255. * @return True if item in same inventory index, False if doesn't
  11256. */
  11257. bool pc_is_same_equip_index(enum equip_index eqi, short *equip_index, short index) {
  11258. if (index < 0 || index >= MAX_INVENTORY)
  11259. return true;
  11260. // Dual weapon checks
  11261. if (eqi == EQI_HAND_R && equip_index[EQI_HAND_L] == index)
  11262. return true;
  11263. // Headgear with Mid & Low location
  11264. if (eqi == EQI_HEAD_MID && equip_index[EQI_HEAD_LOW] == index)
  11265. return true;
  11266. // Headgear with Top & Mid or Low location
  11267. if (eqi == EQI_HEAD_TOP && (equip_index[EQI_HEAD_MID] == index || equip_index[EQI_HEAD_LOW] == index))
  11268. return true;
  11269. // Headgear with Mid & Low location
  11270. if (eqi == EQI_COSTUME_HEAD_MID && equip_index[EQI_COSTUME_HEAD_LOW] == index)
  11271. return true;
  11272. // Headgear with Top & Mid or Low location
  11273. if (eqi == EQI_COSTUME_HEAD_TOP && (equip_index[EQI_COSTUME_HEAD_MID] == index || equip_index[EQI_COSTUME_HEAD_LOW] == index))
  11274. return true;
  11275. return false;
  11276. }
  11277. /**
  11278. * Generate Unique item ID for player
  11279. * @param sd : Player
  11280. * @return A generated Unique item ID
  11281. */
  11282. uint64 pc_generate_unique_id(struct map_session_data *sd) {
  11283. nullpo_ret(sd);
  11284. return ((uint64)sd->status.char_id << 32) | sd->status.uniqueitem_counter++;
  11285. }
  11286. /**
  11287. * Validating skill from player after logged on
  11288. * @param sd
  11289. **/
  11290. void pc_validate_skill(struct map_session_data *sd) {
  11291. if (sd) {
  11292. uint16 i = 0, count = 0;
  11293. struct s_skill tmp_skills[MAX_SKILL] = {{ 0 }};
  11294. memcpy(tmp_skills, sd->status.skill, sizeof(sd->status.skill));
  11295. memset(sd->status.skill, 0, sizeof(sd->status.skill));
  11296. for (i = 0; i < MAX_SKILL; i++) {
  11297. uint16 idx = 0;
  11298. if (tmp_skills[i].id == 0 || tmp_skills[i].lv == 0)
  11299. continue;
  11300. if ((idx = skill_get_index(tmp_skills[i].id))) {
  11301. memcpy(&sd->status.skill[idx], &tmp_skills[i], sizeof(tmp_skills[i]));
  11302. count++;
  11303. }
  11304. else
  11305. 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);
  11306. }
  11307. }
  11308. }
  11309. /**
  11310. * Toggle to remember if the questinfo is displayed yet or not.
  11311. * @param qi_display Display flag
  11312. * @param show If show is true and qi_display is 0, set qi_display to 1 and show the event bubble.
  11313. * If show is false and qi_display is 1, set qi_display to 0 and hide the event bubble.
  11314. **/
  11315. static void pc_show_questinfo_sub(struct map_session_data *sd, bool *qi_display, struct s_questinfo *qi, bool show) {
  11316. if (show) {
  11317. // Check if need to be displayed
  11318. if ((*qi_display) != 1) {
  11319. (*qi_display) = 1;
  11320. clif_quest_show_event(sd, &qi->nd->bl, qi->icon, qi->color);
  11321. }
  11322. }
  11323. else {
  11324. // Check if need to be hide
  11325. if ((*qi_display) != 0) {
  11326. (*qi_display) = 0;
  11327. #if PACKETVER >= 20120410
  11328. clif_quest_show_event(sd, &qi->nd->bl, QTYPE_NONE, QMARK_NONE);
  11329. #else
  11330. clif_quest_show_event(sd, &qi->nd->bl, QTYPE_QUEST, QMARK_NONE);
  11331. #endif
  11332. }
  11333. }
  11334. }
  11335. /**
  11336. * Show available NPC Quest / Event Icon Check [Kisuka]
  11337. * @param sd Player
  11338. **/
  11339. void pc_show_questinfo(struct map_session_data *sd) {
  11340. #if PACKETVER >= 20090218
  11341. nullpo_retv(sd);
  11342. if (sd->bl.m < 0 || sd->bl.m >= MAX_MAPINDEX)
  11343. return;
  11344. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  11345. nullpo_retv(mapdata);
  11346. if (mapdata->qi_data.empty())
  11347. return;
  11348. if (mapdata->qi_data.size() != sd->qi_count)
  11349. return; // init was not called yet
  11350. struct s_questinfo *qi = nullptr;
  11351. bool show;
  11352. for (int i = 0; i < mapdata->qi_data.size(); i++) {
  11353. qi = &mapdata->qi_data[i];
  11354. if (!qi)
  11355. continue;
  11356. if (!qi->condition)
  11357. show = true;
  11358. else {
  11359. if (achievement_check_condition(qi->condition, sd))
  11360. show = true;
  11361. else
  11362. show = false;
  11363. }
  11364. pc_show_questinfo_sub(sd, &sd->qi_display[i], qi, show);
  11365. }
  11366. #endif
  11367. }
  11368. /**
  11369. * Reinit the questinfo for player when changing map
  11370. * @param sd Player
  11371. **/
  11372. void pc_show_questinfo_reinit(struct map_session_data *sd) {
  11373. #if PACKETVER >= 20090218
  11374. nullpo_retv(sd);
  11375. if (sd->qi_display) {
  11376. aFree(sd->qi_display);
  11377. sd->qi_display = nullptr;
  11378. }
  11379. sd->qi_count = 0;
  11380. if (sd->bl.m < 0 || sd->bl.m >= MAX_MAPINDEX)
  11381. return;
  11382. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  11383. nullpo_retv(mapdata);
  11384. if (mapdata->qi_data.empty())
  11385. return;
  11386. CREATE(sd->qi_display, bool, (sd->qi_count = mapdata->qi_data.size()));
  11387. #endif
  11388. }
  11389. /**
  11390. * Check if a job is allowed to enter the map
  11391. * @param jobid Job ID see enum e_job or sd->status.class_
  11392. * @param m ID -an index- for direct indexing map[] array
  11393. * @return 1 if job is allowed, 0 otherwise
  11394. **/
  11395. bool pc_job_can_entermap(enum e_job jobid, int m, int group_lv) {
  11396. uint16 idx = 0;
  11397. // Map is other map server.
  11398. // !FIXME: Currently, a map-server doesn't recognized map's attributes on other server, so we assume it's fine to warp.
  11399. if (m < 0)
  11400. return true;
  11401. struct map_data *mapdata = map_getmapdata(m);
  11402. if (!mapdata->cell)
  11403. return false;
  11404. if (!pcdb_checkid(jobid))
  11405. return false;
  11406. idx = pc_class2idx(jobid);
  11407. if (!job_info[idx].noenter_map.zone || group_lv > job_info[idx].noenter_map.group_lv)
  11408. return true;
  11409. if ((job_info[idx].noenter_map.zone&1 && !mapdata_flag_vs2(mapdata)) || // Normal
  11410. (job_info[idx].noenter_map.zone&2 && mapdata->flag[MF_PVP]) || // PVP
  11411. (job_info[idx].noenter_map.zone&4 && mapdata_flag_gvg2_no_te(mapdata)) || // GVG
  11412. (job_info[idx].noenter_map.zone&8 && mapdata->flag[MF_BATTLEGROUND]) || // Battleground
  11413. (job_info[idx].noenter_map.zone&16 && mapdata_flag_gvg2_te(mapdata)) || // WOE:TE
  11414. (job_info[idx].noenter_map.zone&(mapdata->zone) && mapdata->flag[MF_RESTRICTED]) // Zone restriction
  11415. )
  11416. return false;
  11417. return true;
  11418. }
  11419. /**
  11420. * Tells client about player's costume view on mapchange for checking 'nocostume' mapflag.
  11421. * @param sd
  11422. **/
  11423. void pc_set_costume_view(struct map_session_data *sd) {
  11424. int i = -1, head_low = 0, head_mid = 0, head_top = 0, robe = 0;
  11425. struct item_data *id = NULL;
  11426. nullpo_retv(sd);
  11427. head_low = sd->status.head_bottom;
  11428. head_mid = sd->status.head_mid;
  11429. head_top = sd->status.head_top;
  11430. robe = sd->status.robe;
  11431. sd->status.head_bottom = sd->status.head_mid = sd->status.head_top = sd->status.robe = 0;
  11432. //Added check to prevent sending the same look on multiple slots ->
  11433. //causes client to redraw item on top of itself. (suggested by Lupus)
  11434. // Normal headgear checks
  11435. if ((i = sd->equip_index[EQI_HEAD_LOW]) != -1 && (id = sd->inventory_data[i])) {
  11436. if (!(id->equip&(EQP_HEAD_MID|EQP_HEAD_TOP)))
  11437. sd->status.head_bottom = id->look;
  11438. else
  11439. sd->status.head_bottom = 0;
  11440. }
  11441. if ((i = sd->equip_index[EQI_HEAD_MID]) != -1 && (id = sd->inventory_data[i])) {
  11442. if (!(id->equip&(EQP_HEAD_TOP)))
  11443. sd->status.head_mid = id->look;
  11444. else
  11445. sd->status.head_mid = 0;
  11446. }
  11447. if ((i = sd->equip_index[EQI_HEAD_TOP]) != -1 && (id = sd->inventory_data[i]))
  11448. sd->status.head_top = id->look;
  11449. if ((i = sd->equip_index[EQI_GARMENT]) != -1 && (id = sd->inventory_data[i]))
  11450. sd->status.robe = id->look;
  11451. // Costumes check
  11452. if (!map_getmapflag(sd->bl.m, MF_NOCOSTUME)) {
  11453. if ((i = sd->equip_index[EQI_COSTUME_HEAD_LOW]) != -1 && (id = sd->inventory_data[i])) {
  11454. if (!(id->equip&(EQP_COSTUME_HEAD_MID|EQP_COSTUME_HEAD_TOP)))
  11455. sd->status.head_bottom = id->look;
  11456. else
  11457. sd->status.head_bottom = 0;
  11458. }
  11459. if ((i = sd->equip_index[EQI_COSTUME_HEAD_MID]) != -1 && (id = sd->inventory_data[i])) {
  11460. if (!(id->equip&EQP_COSTUME_HEAD_TOP))
  11461. sd->status.head_mid = id->look;
  11462. else
  11463. sd->status.head_mid = 0;
  11464. }
  11465. if ((i = sd->equip_index[EQI_COSTUME_HEAD_TOP]) != -1 && (id = sd->inventory_data[i]))
  11466. sd->status.head_top = id->look;
  11467. if ((i = sd->equip_index[EQI_COSTUME_GARMENT]) != -1 && (id = sd->inventory_data[i]))
  11468. sd->status.robe = id->look;
  11469. }
  11470. if (sd->setlook_head_bottom)
  11471. sd->status.head_bottom = sd->setlook_head_bottom;
  11472. if (sd->setlook_head_mid)
  11473. sd->status.head_mid = sd->setlook_head_mid;
  11474. if (sd->setlook_head_top)
  11475. sd->status.head_top = sd->setlook_head_top;
  11476. if (sd->setlook_robe)
  11477. sd->status.robe = sd->setlook_robe;
  11478. if (head_low != sd->status.head_bottom)
  11479. clif_changelook(&sd->bl, LOOK_HEAD_BOTTOM, sd->status.head_bottom);
  11480. if (head_mid != sd->status.head_mid)
  11481. clif_changelook(&sd->bl, LOOK_HEAD_MID, sd->status.head_mid);
  11482. if (head_top != sd->status.head_top)
  11483. clif_changelook(&sd->bl, LOOK_HEAD_TOP, sd->status.head_top);
  11484. if (robe != sd->status.robe)
  11485. clif_changelook(&sd->bl, LOOK_ROBE, sd->status.robe);
  11486. }
  11487. std::shared_ptr<s_attendance_period> pc_attendance_period(){
  11488. uint32 date = date_get(DT_YYYYMMDD);
  11489. for( std::pair<const uint32,std::shared_ptr<s_attendance_period>>& pair : attendance_db ){
  11490. std::shared_ptr<s_attendance_period> period = pair.second;
  11491. if( period->start <= date && period->end >= date ){
  11492. return period;
  11493. }
  11494. }
  11495. return nullptr;
  11496. }
  11497. bool pc_attendance_enabled(){
  11498. // Check if the attendance feature is disabled
  11499. if( !battle_config.feature_attendance ){
  11500. return false;
  11501. }
  11502. // Check if there is a running attendance period
  11503. return pc_attendance_period() != nullptr;
  11504. }
  11505. static inline bool pc_attendance_rewarded_today( struct map_session_data* sd ){
  11506. return pc_readreg2( sd, ATTENDANCE_DATE_VAR ) >= date_get(DT_YYYYMMDD);
  11507. }
  11508. int32 pc_attendance_counter( struct map_session_data* sd ){
  11509. std::shared_ptr<s_attendance_period> period = pc_attendance_period();
  11510. // No running attendance period
  11511. if( period == nullptr ){
  11512. return 0;
  11513. }
  11514. // Get the counter for the current period
  11515. int counter = static_cast<int>(pc_readreg2( sd, ATTENDANCE_COUNT_VAR ));
  11516. // Check if we have a remaining counter from a previous period
  11517. if( counter > 0 && pc_readreg2( sd, ATTENDANCE_DATE_VAR ) < period->start ){
  11518. // Reset the counter to zero
  11519. pc_setreg2( sd, ATTENDANCE_COUNT_VAR, 0 );
  11520. return 0;
  11521. }
  11522. return 10 * counter + ( ( pc_attendance_rewarded_today(sd) ) ? 1 : 0 );
  11523. }
  11524. void pc_attendance_claim_reward( struct map_session_data* sd ){
  11525. // If the user's group does not have the permission
  11526. if( !pc_has_permission( sd, PC_PERM_ATTENDANCE ) ){
  11527. return;
  11528. }
  11529. // Check if the attendance feature is disabled
  11530. if( !pc_attendance_enabled() ){
  11531. return;
  11532. }
  11533. // Check if the user already got his reward today
  11534. if( pc_attendance_rewarded_today( sd ) ){
  11535. return;
  11536. }
  11537. int32 attendance_counter = static_cast<int32>(pc_readreg2( sd, ATTENDANCE_COUNT_VAR ));
  11538. attendance_counter += 1;
  11539. std::shared_ptr<s_attendance_period> period = pc_attendance_period();
  11540. if( period == nullptr ){
  11541. return;
  11542. }
  11543. if( period->rewards.size() < attendance_counter ){
  11544. return;
  11545. }
  11546. pc_setreg2( sd, ATTENDANCE_DATE_VAR, date_get(DT_YYYYMMDD) );
  11547. pc_setreg2( sd, ATTENDANCE_COUNT_VAR, attendance_counter );
  11548. if( save_settings&CHARSAVE_ATTENDANCE )
  11549. chrif_save(sd, CSAVE_NORMAL);
  11550. std::shared_ptr<s_attendance_reward> reward = period->rewards[attendance_counter - 1];
  11551. struct mail_message msg;
  11552. memset( &msg, 0, sizeof( struct mail_message ) );
  11553. msg.dest_id = sd->status.char_id;
  11554. safestrncpy( msg.send_name, msg_txt( sd, 788 ), NAME_LENGTH );
  11555. safesnprintf( msg.title, MAIL_TITLE_LENGTH, msg_txt( sd, 789 ), attendance_counter );
  11556. safesnprintf( msg.body, MAIL_BODY_LENGTH, msg_txt( sd, 790 ), attendance_counter );
  11557. msg.item[0].nameid = reward->item_id;
  11558. msg.item[0].amount = reward->amount;
  11559. msg.item[0].identify = 1;
  11560. msg.status = MAIL_NEW;
  11561. msg.type = MAIL_INBOX_NORMAL;
  11562. msg.timestamp = time(NULL);
  11563. intif_Mail_send(0, &msg);
  11564. clif_attendence_response( sd, attendance_counter );
  11565. }
  11566. /*==========================================
  11567. * pc Init/Terminate
  11568. *------------------------------------------*/
  11569. void do_final_pc(void) {
  11570. db_destroy(itemcd_db);
  11571. do_final_pc_groups();
  11572. ers_destroy(pc_sc_display_ers);
  11573. ers_destroy(num_reg_ers);
  11574. ers_destroy(str_reg_ers);
  11575. attendance_db.clear();
  11576. }
  11577. void do_init_pc(void) {
  11578. itemcd_db = idb_alloc(DB_OPT_RELEASE_DATA);
  11579. pc_readdb();
  11580. pc_read_motd(); // Read MOTD [Valaris]
  11581. attendance_db.load();
  11582. add_timer_func_list(pc_invincible_timer, "pc_invincible_timer");
  11583. add_timer_func_list(pc_eventtimer, "pc_eventtimer");
  11584. add_timer_func_list(pc_inventory_rental_end, "pc_inventory_rental_end");
  11585. add_timer_func_list(pc_calc_pvprank_timer, "pc_calc_pvprank_timer");
  11586. add_timer_func_list(pc_autosave, "pc_autosave");
  11587. add_timer_func_list(pc_spiritball_timer, "pc_spiritball_timer");
  11588. add_timer_func_list(pc_follow_timer, "pc_follow_timer");
  11589. add_timer_func_list(pc_endautobonus, "pc_endautobonus");
  11590. add_timer_func_list(pc_spiritcharm_timer, "pc_spiritcharm_timer");
  11591. add_timer_func_list(pc_global_expiration_timer, "pc_global_expiration_timer");
  11592. add_timer_func_list(pc_expiration_timer, "pc_expiration_timer");
  11593. add_timer_func_list(pc_autotrade_timer, "pc_autotrade_timer");
  11594. add_timer(gettick() + autosave_interval, pc_autosave, 0, 0);
  11595. // 0=day, 1=night [Yor]
  11596. night_flag = battle_config.night_at_start ? 1 : 0;
  11597. if (battle_config.day_duration > 0 && battle_config.night_duration > 0) {
  11598. int day_duration = battle_config.day_duration;
  11599. int night_duration = battle_config.night_duration;
  11600. // add night/day timer [Yor]
  11601. add_timer_func_list(map_day_timer, "map_day_timer");
  11602. add_timer_func_list(map_night_timer, "map_night_timer");
  11603. day_timer_tid = add_timer_interval(gettick() + (night_flag ? 0 : day_duration) + night_duration, map_day_timer, 0, 0, day_duration + night_duration);
  11604. night_timer_tid = add_timer_interval(gettick() + day_duration + (night_flag ? night_duration : 0), map_night_timer, 0, 0, day_duration + night_duration);
  11605. }
  11606. do_init_pc_groups();
  11607. pc_sc_display_ers = ers_new(sizeof(struct sc_display_entry), "pc.cpp:pc_sc_display_ers", ERS_OPT_FLEX_CHUNK);
  11608. num_reg_ers = ers_new(sizeof(struct script_reg_num), "pc.cpp:num_reg_ers", (ERSOptions)(ERS_OPT_CLEAN|ERS_OPT_FLEX_CHUNK));
  11609. str_reg_ers = ers_new(sizeof(struct script_reg_str), "pc.cpp:str_reg_ers", (ERSOptions)(ERS_OPT_CLEAN|ERS_OPT_FLEX_CHUNK));
  11610. ers_chunk_size(pc_sc_display_ers, 150);
  11611. ers_chunk_size(num_reg_ers, 300);
  11612. ers_chunk_size(str_reg_ers, 50);
  11613. }