pc.cpp 405 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221
  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. struct s_attendance_reward{
  79. uint16 item_id;
  80. uint16 amount;
  81. };
  82. struct s_attendance_period{
  83. uint32 start;
  84. uint32 end;
  85. std::map<uint32,std::shared_ptr<struct s_attendance_reward>> rewards;
  86. };
  87. class AttendanceDatabase : public TypesafeYamlDatabase<uint32,s_attendance_period>{
  88. public:
  89. AttendanceDatabase() : TypesafeYamlDatabase( "ATTENDANCE_DB", 1 ){
  90. }
  91. const std::string getDefaultLocation();
  92. uint64 parseBodyNode( const YAML::Node& node );
  93. };
  94. const std::string AttendanceDatabase::getDefaultLocation(){
  95. return std::string(db_path) + "/attendance.yml";
  96. }
  97. /**
  98. * Reads and parses an entry from the attendance_db.
  99. * @param node: YAML node containing the entry.
  100. * @return count of successfully parsed rows
  101. */
  102. uint64 AttendanceDatabase::parseBodyNode(const YAML::Node &node){
  103. uint32 start;
  104. if( !this->asUInt32( node, "Start", start ) ){
  105. return 0;
  106. }
  107. std::shared_ptr<s_attendance_period> attendance_period = this->find( start );
  108. bool exists = attendance_period != nullptr;
  109. if( !exists ){
  110. if( !this->nodeExists( node, "End" ) ){
  111. this->invalidWarning( node, "Node \"End\" is missing.\n" );
  112. return 0;
  113. }
  114. if( !this->nodeExists( node, "Rewards" ) ){
  115. this->invalidWarning( node, "Node \"Rewards\" is missing.\n" );
  116. return 0;
  117. }
  118. attendance_period = std::make_shared<s_attendance_period>();
  119. attendance_period->start = start;
  120. }
  121. // If it does not exist yet, we need to check it for sure
  122. bool requiresCollisionDetection = !exists;
  123. if( this->nodeExists( node, "End" ) ){
  124. uint32 end;
  125. if( !this->asUInt32( node, "End", end ) ){
  126. return 0;
  127. }
  128. // If the period is outdated already, we do not even bother parsing
  129. if( end < date_get( DT_YYYYMMDD ) ){
  130. this->invalidWarning( node, "Node \"End\" date %u has already passed, skipping.\n", end );
  131. return 0;
  132. }
  133. if( !exists || attendance_period->end != end ){
  134. requiresCollisionDetection = true;
  135. attendance_period->end = end;
  136. }
  137. }
  138. // Collision detection
  139. if( requiresCollisionDetection ){
  140. bool collision = false;
  141. for( std::pair<const uint32,std::shared_ptr<s_attendance_period>>& pair : *this ){
  142. std::shared_ptr<s_attendance_period> period = pair.second;
  143. if( exists && period->start == attendance_period->start ){
  144. // Dont compare to yourself
  145. continue;
  146. }
  147. // Check if start is inside another period
  148. if( period->start <= attendance_period->start && start <= period->end ){
  149. this->invalidWarning( node, "Node \"Start\" period %u intersects with period %u-%u, skipping.\n", attendance_period->start, period->start, period->end );
  150. collision = true;
  151. break;
  152. }
  153. // Check if end is inside another period
  154. if( period->start <= attendance_period->end && attendance_period->end <= period->end ){
  155. this->invalidWarning( node, "Node \"End\" period %u intersects with period %u-%u.\n", attendance_period->start, period->start, period->end );
  156. collision = true;
  157. break;
  158. }
  159. }
  160. if( collision ){
  161. return 0;
  162. }
  163. }
  164. if( this->nodeExists( node, "Rewards" ) ){
  165. const YAML::Node& rewardsNode = node["Rewards"];
  166. for( const YAML::Node& rewardNode : rewardsNode ){
  167. uint32 day;
  168. if( !this->asUInt32( rewardNode, "Day", day ) ){
  169. continue;
  170. }
  171. day -= 1;
  172. std::shared_ptr<s_attendance_reward> reward = util::map_find( attendance_period->rewards, day );
  173. bool reward_exists = reward != nullptr;
  174. if( !reward_exists ){
  175. if( !this->nodeExists( rewardNode, "ItemId" ) ){
  176. this->invalidWarning( rewardNode, "Node \"ItemId\" is missing.\n" );
  177. return 0;
  178. }
  179. reward = std::make_shared<s_attendance_reward>();
  180. }
  181. if( this->nodeExists( rewardNode, "ItemId" ) ){
  182. uint16 item_id;
  183. if( !this->asUInt16( rewardNode, "ItemId", item_id ) ){
  184. continue;
  185. }
  186. if( item_id == 0 || !itemdb_exists( item_id ) ){
  187. ShowError( "pc_attendance_load: Unknown item ID %hu for day %d.\n", item_id, day + 1 );
  188. continue;
  189. }
  190. reward->item_id = item_id;
  191. }
  192. if( this->nodeExists( rewardNode, "Amount" ) ){
  193. uint16 amount;
  194. if( !this->asUInt16( rewardNode, "Amount", amount ) ){
  195. continue;
  196. }
  197. if( amount == 0 ){
  198. ShowWarning( "pc_attendance_load: Invalid reward count %hu for day %d. Defaulting to 1...\n", amount, day + 1 );
  199. amount = 1;
  200. }else if( amount > MAX_AMOUNT ){
  201. ShowError( "pc_attendance_load: Reward count %hu above maximum %hu for day %d. Defaulting to %hu...\n", amount, MAX_AMOUNT, day + 1, MAX_AMOUNT );
  202. amount = MAX_AMOUNT;
  203. }
  204. reward->amount = amount;
  205. }else{
  206. if( !reward_exists ){
  207. reward->amount = 1;
  208. }
  209. }
  210. if( !reward_exists ){
  211. attendance_period->rewards[day] = reward;
  212. }
  213. }
  214. bool missing_day = false;
  215. for( int day = 0; day < attendance_period->rewards.size(); day++ ){
  216. if( attendance_period->rewards.find( day ) == attendance_period->rewards.end() ){
  217. ShowError( "pc_attendance_load: Reward for day %d is missing.\n", day + 1 );
  218. missing_day = true;
  219. break;
  220. }
  221. }
  222. if( missing_day ){
  223. return 0;
  224. }
  225. }
  226. if( !exists ){
  227. this->put( start, attendance_period );
  228. }
  229. return 1;
  230. }
  231. AttendanceDatabase attendance_db;
  232. #define MOTD_LINE_SIZE 128
  233. static char motd_text[MOTD_LINE_SIZE][CHAT_SIZE_MAX]; // Message of the day buffer [Valaris]
  234. bool reg_load;
  235. /**
  236. * Translation table from athena equip index to aegis bitmask
  237. */
  238. unsigned int equip_bitmask[EQI_MAX] = {
  239. EQP_ACC_L, // EQI_ACC_L
  240. EQP_ACC_R, // EQI_ACC_R
  241. EQP_SHOES, // EQI_SHOES
  242. EQP_GARMENT, // EQI_GARMENT
  243. EQP_HEAD_LOW, // EQI_HEAD_LOW
  244. EQP_HEAD_MID, // EQI_HEAD_MID
  245. EQP_HEAD_TOP, // EQI_HEAD_TOP
  246. EQP_ARMOR, // EQI_ARMOR
  247. EQP_HAND_L, // EQI_HAND_L
  248. EQP_HAND_R, // EQI_HAND_R
  249. EQP_COSTUME_HEAD_TOP, // EQI_COSTUME_HEAD_TOP
  250. EQP_COSTUME_HEAD_MID, // EQI_COSTUME_HEAD_MID
  251. EQP_COSTUME_HEAD_LOW, // EQI_COSTUME_HEAD_LOW
  252. EQP_COSTUME_GARMENT, // EQI_COSTUME_GARMENT
  253. EQP_AMMO, // EQI_AMMO
  254. EQP_SHADOW_ARMOR, // EQI_SHADOW_ARMOR
  255. EQP_SHADOW_WEAPON, // EQI_SHADOW_WEAPON
  256. EQP_SHADOW_SHIELD, // EQI_SHADOW_SHIELD
  257. EQP_SHADOW_SHOES, // EQI_SHADOW_SHOES
  258. EQP_SHADOW_ACC_R, // EQI_SHADOW_ACC_R
  259. EQP_SHADOW_ACC_L // EQI_SHADOW_ACC_L
  260. };
  261. //Links related info to the sd->hate_mob[]/sd->feel_map[] entries
  262. const struct sg_data sg_info[MAX_PC_FEELHATE] = {
  263. { SG_SUN_ANGER, SG_SUN_BLESS, SG_SUN_COMFORT, "PC_FEEL_SUN", "PC_HATE_MOB_SUN", is_day_of_sun },
  264. { SG_MOON_ANGER, SG_MOON_BLESS, SG_MOON_COMFORT, "PC_FEEL_MOON", "PC_HATE_MOB_MOON", is_day_of_moon },
  265. { SG_STAR_ANGER, SG_STAR_BLESS, SG_STAR_COMFORT, "PC_FEEL_STAR", "PC_HATE_MOB_STAR", is_day_of_star }
  266. };
  267. void pc_set_reg_load( bool val ){
  268. reg_load = val;
  269. }
  270. /**
  271. * Item Cool Down Delay Saving
  272. * Struct item_cd is not a member of struct map_session_data
  273. * to keep cooldowns in memory between player log-ins.
  274. * All cooldowns are reset when server is restarted.
  275. **/
  276. DBMap* itemcd_db = NULL; // char_id -> struct item_cd
  277. struct item_cd {
  278. t_tick tick[MAX_ITEMDELAYS]; //tick
  279. unsigned short nameid[MAX_ITEMDELAYS]; //item id
  280. };
  281. /**
  282. * Converts a class to its array index for CLASS_COUNT defined arrays.
  283. * Note that it does not do a validity check for speed purposes, where parsing
  284. * player input make sure to use a pcdb_checkid first!
  285. * @param class_ Job ID see enum e_job
  286. * @return Class Index
  287. */
  288. int pc_class2idx(int class_) {
  289. if (class_ >= JOB_NOVICE_HIGH)
  290. return class_- JOB_NOVICE_HIGH+JOB_MAX_BASIC;
  291. return class_;
  292. }
  293. /**
  294. * Get player's group ID
  295. * @param sd
  296. * @return Group ID
  297. */
  298. int pc_get_group_id(struct map_session_data *sd) {
  299. return sd->group_id;
  300. }
  301. /** Get player's group Level
  302. * @param sd
  303. * @return Group Level
  304. */
  305. int pc_get_group_level(struct map_session_data *sd) {
  306. return sd->group_level;
  307. }
  308. static TIMER_FUNC(pc_invincible_timer){
  309. struct map_session_data *sd;
  310. if( (sd=(struct map_session_data *)map_id2sd(id)) == NULL || sd->bl.type!=BL_PC )
  311. return 1;
  312. if(sd->invincible_timer != tid){
  313. ShowError("invincible_timer %d != %d\n",sd->invincible_timer,tid);
  314. return 0;
  315. }
  316. sd->invincible_timer = INVALID_TIMER;
  317. skill_unit_move(&sd->bl,tick,1);
  318. return 0;
  319. }
  320. void pc_setinvincibletimer(struct map_session_data* sd, int val) {
  321. nullpo_retv(sd);
  322. if( sd->invincible_timer != INVALID_TIMER )
  323. delete_timer(sd->invincible_timer,pc_invincible_timer);
  324. sd->invincible_timer = add_timer(gettick()+val,pc_invincible_timer,sd->bl.id,0);
  325. }
  326. void pc_delinvincibletimer(struct map_session_data* sd)
  327. {
  328. nullpo_retv(sd);
  329. if( sd->invincible_timer != INVALID_TIMER )
  330. {
  331. delete_timer(sd->invincible_timer,pc_invincible_timer);
  332. sd->invincible_timer = INVALID_TIMER;
  333. skill_unit_move(&sd->bl,gettick(),1);
  334. }
  335. }
  336. static TIMER_FUNC(pc_spiritball_timer){
  337. struct map_session_data *sd;
  338. int i;
  339. if( (sd=(struct map_session_data *)map_id2sd(id)) == NULL || sd->bl.type!=BL_PC )
  340. return 1;
  341. if( sd->spiritball <= 0 )
  342. {
  343. 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);
  344. sd->spiritball = 0;
  345. return 0;
  346. }
  347. ARR_FIND(0, sd->spiritball, i, sd->spirit_timer[i] == tid);
  348. if( i == sd->spiritball )
  349. {
  350. ShowError("pc_spiritball_timer: timer not found (aid=%d cid=%d tid=%d)\n", sd->status.account_id, sd->status.char_id, tid);
  351. return 0;
  352. }
  353. sd->spiritball--;
  354. if( i != sd->spiritball )
  355. memmove(sd->spirit_timer+i, sd->spirit_timer+i+1, (sd->spiritball-i)*sizeof(int));
  356. sd->spirit_timer[sd->spiritball] = INVALID_TIMER;
  357. clif_spiritball(&sd->bl);
  358. return 0;
  359. }
  360. /**
  361. * Adds a spiritball to player for 'interval' ms
  362. * @param sd
  363. * @param interval
  364. * @param max
  365. */
  366. void pc_addspiritball(struct map_session_data *sd,int interval,int max)
  367. {
  368. int tid;
  369. uint8 i;
  370. nullpo_retv(sd);
  371. if(max > MAX_SPIRITBALL)
  372. max = MAX_SPIRITBALL;
  373. if(sd->spiritball < 0)
  374. sd->spiritball = 0;
  375. if( sd->spiritball && sd->spiritball >= max )
  376. {
  377. if(sd->spirit_timer[0] != INVALID_TIMER)
  378. delete_timer(sd->spirit_timer[0],pc_spiritball_timer);
  379. sd->spiritball--;
  380. if( sd->spiritball != 0 )
  381. memmove(sd->spirit_timer+0, sd->spirit_timer+1, (sd->spiritball)*sizeof(int));
  382. sd->spirit_timer[sd->spiritball] = INVALID_TIMER;
  383. }
  384. tid = add_timer(gettick()+interval, pc_spiritball_timer, sd->bl.id, 0);
  385. 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);
  386. if( i != sd->spiritball )
  387. memmove(sd->spirit_timer+i+1, sd->spirit_timer+i, (sd->spiritball-i)*sizeof(int));
  388. sd->spirit_timer[i] = tid;
  389. sd->spiritball++;
  390. if( (sd->class_&MAPID_THIRDMASK) == MAPID_ROYAL_GUARD )
  391. clif_millenniumshield(&sd->bl,sd->spiritball);
  392. else
  393. clif_spiritball(&sd->bl);
  394. }
  395. /**
  396. * Removes number of spiritball from player
  397. * @param sd
  398. * @param count
  399. * @param type 1 = doesn't give client effect
  400. */
  401. void pc_delspiritball(struct map_session_data *sd,int count,int type)
  402. {
  403. uint8 i;
  404. nullpo_retv(sd);
  405. if(sd->spiritball <= 0) {
  406. sd->spiritball = 0;
  407. return;
  408. }
  409. if(count == 0)
  410. return;
  411. if(count > sd->spiritball)
  412. count = sd->spiritball;
  413. sd->spiritball -= count;
  414. if(count > MAX_SPIRITBALL)
  415. count = MAX_SPIRITBALL;
  416. for(i=0;i<count;i++) {
  417. if(sd->spirit_timer[i] != INVALID_TIMER) {
  418. delete_timer(sd->spirit_timer[i],pc_spiritball_timer);
  419. sd->spirit_timer[i] = INVALID_TIMER;
  420. }
  421. }
  422. for(i=count;i<MAX_SPIRITBALL;i++) {
  423. sd->spirit_timer[i-count] = sd->spirit_timer[i];
  424. sd->spirit_timer[i] = INVALID_TIMER;
  425. }
  426. if(!type) {
  427. if( (sd->class_&MAPID_THIRDMASK) == MAPID_ROYAL_GUARD )
  428. clif_millenniumshield(&sd->bl,sd->spiritball);
  429. else
  430. clif_spiritball(&sd->bl);
  431. }
  432. }
  433. /**
  434. * Increases a player's fame points and displays a notice to him
  435. * @param sd Player
  436. * @param count Fame point
  437. */
  438. void pc_addfame(struct map_session_data *sd,int count)
  439. {
  440. enum e_rank ranktype;
  441. nullpo_retv(sd);
  442. sd->status.fame += count;
  443. if(sd->status.fame > MAX_FAME)
  444. sd->status.fame = MAX_FAME;
  445. switch(sd->class_&MAPID_UPPERMASK){
  446. case MAPID_BLACKSMITH: ranktype = RANK_BLACKSMITH; break;
  447. case MAPID_ALCHEMIST: ranktype = RANK_ALCHEMIST; break;
  448. case MAPID_TAEKWON: ranktype = RANK_TAEKWON; break;
  449. default:
  450. ShowWarning( "pc_addfame: Trying to add fame to class '%s'(%d).\n", job_name(sd->class_), sd->class_ );
  451. return;
  452. }
  453. clif_update_rankingpoint(sd,ranktype,count);
  454. chrif_updatefamelist(sd);
  455. }
  456. /**
  457. * Check whether a player ID is in the fame rankers list of its job, returns his/her position if so, 0 else
  458. * @param sd
  459. * @param job Job use enum e_mapid
  460. * @return Rank
  461. */
  462. unsigned char pc_famerank(uint32 char_id, int job)
  463. {
  464. uint8 i;
  465. switch(job){
  466. case MAPID_BLACKSMITH: // Blacksmith
  467. for(i = 0; i < MAX_FAME_LIST; i++){
  468. if(smith_fame_list[i].id == char_id)
  469. return i + 1;
  470. }
  471. break;
  472. case MAPID_ALCHEMIST: // Alchemist
  473. for(i = 0; i < MAX_FAME_LIST; i++){
  474. if(chemist_fame_list[i].id == char_id)
  475. return i + 1;
  476. }
  477. break;
  478. case MAPID_TAEKWON: // Taekwon
  479. for(i = 0; i < MAX_FAME_LIST; i++){
  480. if(taekwon_fame_list[i].id == char_id)
  481. return i + 1;
  482. }
  483. break;
  484. }
  485. return 0;
  486. }
  487. /**
  488. * Restart player's HP & SP value
  489. * @param sd
  490. * @param type Restart type: 1 - Normal Resurection
  491. */
  492. void pc_setrestartvalue(struct map_session_data *sd, char type) {
  493. struct status_data *status, *b_status;
  494. nullpo_retv(sd);
  495. b_status = &sd->base_status;
  496. status = &sd->battle_status;
  497. if (type&1) { //Normal resurrection
  498. status->hp = 1; //Otherwise status_heal may fail if dead.
  499. status_heal(&sd->bl, b_status->hp, 0, 1);
  500. if( status->sp < b_status->sp )
  501. status_set_sp(&sd->bl, b_status->sp, 1);
  502. } else { //Just for saving on the char-server (with values as if respawned)
  503. sd->status.hp = b_status->hp;
  504. sd->status.sp = (status->sp < b_status->sp)?b_status->sp:status->sp;
  505. }
  506. }
  507. /*==========================================
  508. Rental System
  509. *------------------------------------------*/
  510. /**
  511. * Ends a rental and removes the item/effect
  512. * @param tid: Tick ID
  513. * @param tick: Timer
  514. * @param id: Timer ID
  515. * @param data: Data
  516. * @return false - failure, true - success
  517. */
  518. TIMER_FUNC(pc_inventory_rental_end){
  519. struct map_session_data *sd = map_id2sd(id);
  520. if( sd == NULL )
  521. return 0;
  522. if( tid != sd->rental_timer ) {
  523. ShowError("pc_inventory_rental_end: invalid timer id.\n");
  524. return 0;
  525. }
  526. pc_inventory_rentals(sd);
  527. return 1;
  528. }
  529. /**
  530. * Removes the rental timer from the player
  531. * @param sd: Player data
  532. */
  533. void pc_inventory_rental_clear(struct map_session_data *sd)
  534. {
  535. if( sd->rental_timer != INVALID_TIMER ) {
  536. delete_timer(sd->rental_timer, pc_inventory_rental_end);
  537. sd->rental_timer = INVALID_TIMER;
  538. }
  539. }
  540. /**
  541. * Check for items in the player's inventory that are rental type
  542. * @param sd: Player data
  543. */
  544. void pc_inventory_rentals(struct map_session_data *sd)
  545. {
  546. int i, c = 0;
  547. unsigned int next_tick = UINT_MAX;
  548. for( i = 0; i < MAX_INVENTORY; i++ ) { // Check for Rentals on Inventory
  549. if( sd->inventory.u.items_inventory[i].nameid == 0 )
  550. continue; // Nothing here
  551. if( sd->inventory.u.items_inventory[i].expire_time == 0 )
  552. continue;
  553. if( sd->inventory.u.items_inventory[i].expire_time <= time(NULL) ) {
  554. if (sd->inventory_data[i]->unequip_script)
  555. run_script(sd->inventory_data[i]->unequip_script, 0, sd->bl.id, fake_nd->bl.id);
  556. clif_rental_expired(sd->fd, i, sd->inventory.u.items_inventory[i].nameid);
  557. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 0, 0, LOG_TYPE_OTHER);
  558. } else {
  559. unsigned int expire_tick = (unsigned int)(sd->inventory.u.items_inventory[i].expire_time - time(NULL));
  560. clif_rental_time(sd->fd, sd->inventory.u.items_inventory[i].nameid, (int)expire_tick);
  561. next_tick = umin(expire_tick * 1000U, next_tick);
  562. c++;
  563. }
  564. }
  565. 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
  566. sd->rental_timer = add_timer(gettick() + umin(next_tick,3600000), pc_inventory_rental_end, sd->bl.id, 0);
  567. else
  568. sd->rental_timer = INVALID_TIMER;
  569. }
  570. /**
  571. * Add a rental item to the player and adjusts the rental timer appropriately
  572. * @param sd: Player data
  573. * @param seconds: Rental time
  574. */
  575. void pc_inventory_rental_add(struct map_session_data *sd, unsigned int seconds)
  576. {
  577. t_tick tick = seconds * 1000;
  578. if( sd == NULL )
  579. return;
  580. if( sd->rental_timer != INVALID_TIMER ) {
  581. const struct TimerData * td;
  582. td = get_timer(sd->rental_timer);
  583. if( DIFF_TICK(td->tick, gettick()) > tick ) { // Update Timer as this one ends first than the current one
  584. pc_inventory_rental_clear(sd);
  585. sd->rental_timer = add_timer(gettick() + tick, pc_inventory_rental_end, sd->bl.id, 0);
  586. }
  587. } else
  588. sd->rental_timer = add_timer(gettick() + i64min(tick,3600000), pc_inventory_rental_end, sd->bl.id, 0);
  589. }
  590. /**
  591. * Check if the player can sell the current item
  592. * @param sd: map_session_data of the player
  593. * @param item: struct of the checking item
  594. * @param shoptype: NPC's sub type see enum npc_subtype
  595. * @return bool 'true' is sellable, 'false' otherwise
  596. */
  597. bool pc_can_sell_item(struct map_session_data *sd, struct item *item, enum npc_subtype shoptype) {
  598. if (sd == NULL || item == NULL)
  599. return false;
  600. if (item->equip > 0 || item->amount < 0)
  601. return false;
  602. if (battle_config.hide_fav_sell && item->favorite)
  603. return false; //Cannot sell favs (optional config)
  604. if (!battle_config.rental_transaction && item->expire_time)
  605. return false; // Cannot Sell Rental Items
  606. if( item->equipSwitch ){
  607. return false;
  608. }
  609. switch (shoptype) {
  610. case NPCTYPE_SHOP:
  611. if (item->bound && battle_config.allow_bound_sell&ISR_BOUND_SELLABLE && (
  612. item->bound != BOUND_GUILD ||
  613. (sd->guild && sd->status.char_id == sd->guild->member[0].char_id) ||
  614. (item->bound == BOUND_GUILD && !(battle_config.allow_bound_sell&ISR_BOUND_GUILDLEADER_ONLY))
  615. ))
  616. return true;
  617. break;
  618. case NPCTYPE_ITEMSHOP:
  619. if (item->bound && battle_config.allow_bound_sell&ISR_BOUND && (
  620. item->bound != BOUND_GUILD ||
  621. (sd->guild && sd->status.char_id == sd->guild->member[0].char_id) ||
  622. (item->bound == BOUND_GUILD && !(battle_config.allow_bound_sell&ISR_BOUND_GUILDLEADER_ONLY))
  623. ))
  624. return true;
  625. else if (!item->bound) {
  626. struct item_data *itd = itemdb_search(item->nameid);
  627. if (itd && itd->flag.trade_restriction&8 && battle_config.allow_bound_sell&ISR_SELLABLE)
  628. return true;
  629. }
  630. break;
  631. }
  632. if (!itemdb_cansell(item, pc_get_group_level(sd)))
  633. return false;
  634. if (item->bound && !pc_can_give_bounded_items(sd))
  635. return false; // Don't allow sale of bound items
  636. return true;
  637. }
  638. /**
  639. * Determines if player can give / drop / trade / vend items
  640. */
  641. bool pc_can_give_items(struct map_session_data *sd)
  642. {
  643. return pc_has_permission(sd, PC_PERM_TRADE);
  644. }
  645. /**
  646. * Determines if player can give / drop / trade / vend bounded items
  647. */
  648. bool pc_can_give_bounded_items(struct map_session_data *sd)
  649. {
  650. return pc_has_permission(sd, PC_PERM_TRADE_BOUNDED);
  651. }
  652. /*==========================================
  653. * Prepares character for saving.
  654. * @param sd
  655. *------------------------------------------*/
  656. void pc_makesavestatus(struct map_session_data *sd) {
  657. nullpo_retv(sd);
  658. if(!battle_config.save_clothcolor)
  659. sd->status.clothes_color = 0;
  660. // Since this is currently not officially released,
  661. // its best to have a forced option to not save body styles.
  662. if(!battle_config.save_body_style)
  663. sd->status.body = 0;
  664. //Only copy the Cart/Peco/Falcon options, the rest are handled via
  665. //status change load/saving. [Skotlex]
  666. #ifdef NEW_CARTS
  667. sd->status.option = sd->sc.option&(OPTION_INVISIBLE|OPTION_FALCON|OPTION_RIDING|OPTION_DRAGON|OPTION_WUG|OPTION_WUGRIDER|OPTION_MADOGEAR);
  668. #else
  669. sd->status.option = sd->sc.option&(OPTION_INVISIBLE|OPTION_CART|OPTION_FALCON|OPTION_RIDING|OPTION_DRAGON|OPTION_WUG|OPTION_WUGRIDER|OPTION_MADOGEAR);
  670. #endif
  671. if (sd->sc.data[SC_JAILED]) { //When Jailed, do not move last point.
  672. if(pc_isdead(sd)){
  673. pc_setrestartvalue(sd, 0);
  674. } else {
  675. sd->status.hp = sd->battle_status.hp;
  676. sd->status.sp = sd->battle_status.sp;
  677. }
  678. sd->status.last_point.map = sd->mapindex;
  679. sd->status.last_point.x = sd->bl.x;
  680. sd->status.last_point.y = sd->bl.y;
  681. return;
  682. }
  683. if(pc_isdead(sd)) {
  684. pc_setrestartvalue(sd, 0);
  685. memcpy(&sd->status.last_point,&sd->status.save_point,sizeof(sd->status.last_point));
  686. } else {
  687. sd->status.hp = sd->battle_status.hp;
  688. sd->status.sp = sd->battle_status.sp;
  689. sd->status.last_point.map = sd->mapindex;
  690. sd->status.last_point.x = sd->bl.x;
  691. sd->status.last_point.y = sd->bl.y;
  692. }
  693. if(map_getmapflag(sd->bl.m, MF_NOSAVE)) {
  694. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  695. if(mapdata->save.map)
  696. memcpy(&sd->status.last_point,&mapdata->save,sizeof(sd->status.last_point));
  697. else
  698. memcpy(&sd->status.last_point,&sd->status.save_point,sizeof(sd->status.last_point));
  699. }
  700. }
  701. /*==========================================
  702. * Off init ? Connection?
  703. *------------------------------------------*/
  704. 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) {
  705. nullpo_retv(sd);
  706. sd->bl.id = account_id;
  707. sd->status.account_id = account_id;
  708. sd->status.char_id = char_id;
  709. sd->status.sex = sex;
  710. sd->login_id1 = login_id1;
  711. sd->login_id2 = 0; // at this point, we can not know the value :(
  712. sd->client_tick = client_tick;
  713. sd->state.active = 0; //to be set to 1 after player is fully authed and loaded.
  714. sd->bl.type = BL_PC;
  715. if(battle_config.prevent_logout_trigger&PLT_LOGIN)
  716. sd->canlog_tick = gettick();
  717. //Required to prevent homunculus copuing a base speed of 0.
  718. sd->battle_status.speed = sd->base_status.speed = DEFAULT_WALK_SPEED;
  719. }
  720. /**
  721. * Get equip point for an equip
  722. * @param sd
  723. * @param id
  724. */
  725. int pc_equippoint_sub(struct map_session_data *sd,struct item_data* id){
  726. int ep = 0;
  727. nullpo_ret(sd);
  728. nullpo_ret(id);
  729. if (!itemdb_isequip2(id))
  730. return 0; //Not equippable by players.
  731. ep = id->equip;
  732. if(id->look == W_DAGGER || id->look == W_1HSWORD || id->look == W_1HAXE) {
  733. 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]
  734. if (ep == EQP_WEAPON)
  735. return EQP_ARMS;
  736. if (ep == EQP_SHADOW_WEAPON)
  737. return EQP_SHADOW_ARMS;
  738. }
  739. }
  740. return ep;
  741. }
  742. /**
  743. * Get equip point for an equip
  744. * @param sd
  745. * @param n Equip index in inventory
  746. */
  747. int pc_equippoint(struct map_session_data *sd,int n){
  748. nullpo_ret(sd);
  749. return pc_equippoint_sub(sd,sd->inventory_data[n]);
  750. }
  751. /**
  752. * Fill inventory_data with struct *item_data through inventory (fill with struct *item)
  753. * @param sd : player session
  754. * @return 0 sucess, 1:invalid sd
  755. */
  756. void pc_setinventorydata(struct map_session_data *sd)
  757. {
  758. uint8 i;
  759. nullpo_retv(sd);
  760. for(i = 0; i < MAX_INVENTORY; i++) {
  761. unsigned short id = sd->inventory.u.items_inventory[i].nameid;
  762. sd->inventory_data[i] = id?itemdb_search(id):NULL;
  763. }
  764. }
  765. /**
  766. * 'Calculates' weapon type
  767. * @param sd : Player
  768. */
  769. void pc_calcweapontype(struct map_session_data *sd)
  770. {
  771. nullpo_retv(sd);
  772. // single-hand
  773. if(sd->weapontype2 == W_FIST) {
  774. sd->status.weapon = sd->weapontype1;
  775. return;
  776. }
  777. if(sd->weapontype1 == W_FIST) {
  778. sd->status.weapon = sd->weapontype2;
  779. return;
  780. }
  781. // dual-wield
  782. sd->status.weapon = 0;
  783. switch (sd->weapontype1){
  784. case W_DAGGER:
  785. switch (sd->weapontype2) {
  786. case W_DAGGER: sd->status.weapon = W_DOUBLE_DD; break;
  787. case W_1HSWORD: sd->status.weapon = W_DOUBLE_DS; break;
  788. case W_1HAXE: sd->status.weapon = W_DOUBLE_DA; break;
  789. }
  790. break;
  791. case W_1HSWORD:
  792. switch (sd->weapontype2) {
  793. case W_DAGGER: sd->status.weapon = W_DOUBLE_DS; break;
  794. case W_1HSWORD: sd->status.weapon = W_DOUBLE_SS; break;
  795. case W_1HAXE: sd->status.weapon = W_DOUBLE_SA; break;
  796. }
  797. break;
  798. case W_1HAXE:
  799. switch (sd->weapontype2) {
  800. case W_DAGGER: sd->status.weapon = W_DOUBLE_DA; break;
  801. case W_1HSWORD: sd->status.weapon = W_DOUBLE_SA; break;
  802. case W_1HAXE: sd->status.weapon = W_DOUBLE_AA; break;
  803. }
  804. }
  805. // unknown, default to right hand type
  806. if (!sd->status.weapon)
  807. sd->status.weapon = sd->weapontype1;
  808. }
  809. /**
  810. * Set equip index
  811. * @param sd : Player
  812. */
  813. void pc_setequipindex(struct map_session_data *sd)
  814. {
  815. uint16 i;
  816. nullpo_retv(sd);
  817. for (i = 0; i < EQI_MAX; i++){
  818. sd->equip_index[i] = -1;
  819. sd->equip_switch_index[i] = -1;
  820. }
  821. for (i = 0; i < MAX_INVENTORY; i++) {
  822. if (sd->inventory.u.items_inventory[i].nameid <= 0)
  823. continue;
  824. if (sd->inventory.u.items_inventory[i].equip) {
  825. uint8 j;
  826. for (j = 0; j < EQI_MAX; j++)
  827. if (sd->inventory.u.items_inventory[i].equip & equip_bitmask[j])
  828. sd->equip_index[j] = i;
  829. if (sd->inventory.u.items_inventory[i].equip & EQP_HAND_R) {
  830. if (sd->inventory_data[i])
  831. sd->weapontype1 = sd->inventory_data[i]->look;
  832. else
  833. sd->weapontype1 = 0;
  834. }
  835. if( sd->inventory.u.items_inventory[i].equip & EQP_HAND_L ) {
  836. if( sd->inventory_data[i] && sd->inventory_data[i]->type == IT_WEAPON )
  837. sd->weapontype2 = sd->inventory_data[i]->look;
  838. else
  839. sd->weapontype2 = 0;
  840. }
  841. }
  842. if (sd->inventory.u.items_inventory[i].equipSwitch) {
  843. for (uint8 j = 0; j < EQI_MAX; j++) {
  844. if (sd->inventory.u.items_inventory[i].equipSwitch & equip_bitmask[j]) {
  845. sd->equip_switch_index[j] = i;
  846. }
  847. }
  848. }
  849. }
  850. pc_calcweapontype(sd);
  851. }
  852. //static int pc_isAllowedCardOn(struct map_session_data *sd,int s,int eqindex,int flag)
  853. //{
  854. // int i;
  855. // struct item *item = &sd->inventory.u.items_inventory[eqindex];
  856. // struct item_data *data;
  857. //
  858. // //Crafted/made/hatched items.
  859. // if (itemdb_isspecial(item->card[0]))
  860. // return 1;
  861. //
  862. // /* scan for enchant armor gems */
  863. // if( item->card[MAX_SLOTS - 1] && s < MAX_SLOTS - 1 )
  864. // s = MAX_SLOTS - 1;
  865. //
  866. // ARR_FIND( 0, s, i, item->card[i] && (data = itemdb_exists(item->card[i])) != NULL && data->flag.no_equip&flag );
  867. // return( i < s ) ? 0 : 1;
  868. //}
  869. /**
  870. * Check if an item is equiped by player
  871. * (Check if the itemid is equiped then search if that match the index in inventory (should be))
  872. * @param sd : player session
  873. * @param nameid : itemid
  874. * @return 1:yes, 0:no
  875. */
  876. bool pc_isequipped(struct map_session_data *sd, unsigned short nameid)
  877. {
  878. uint8 i;
  879. for( i = 0; i < EQI_MAX; i++ )
  880. {
  881. short index = sd->equip_index[i], j;
  882. if( index < 0 )
  883. continue;
  884. if( pc_is_same_equip_index((enum equip_index)i, sd->equip_index, index) )
  885. continue;
  886. if( !sd->inventory_data[index] )
  887. continue;
  888. if( sd->inventory_data[index]->nameid == nameid )
  889. return true;
  890. for( j = 0; j < sd->inventory_data[index]->slot; j++ ){
  891. if( sd->inventory.u.items_inventory[index].card[j] == nameid )
  892. return true;
  893. }
  894. }
  895. return false;
  896. }
  897. /**
  898. * Check adoption rules
  899. * @param p1_sd: Player 1
  900. * @param p2_sd: Player 2
  901. * @param b_sd: Player that will be adopted
  902. * @return ADOPT_ALLOWED - Sent message to Baby to accept or deny
  903. * ADOPT_ALREADY_ADOPTED - Already adopted
  904. * ADOPT_MARRIED_AND_PARTY - Need to be married and in the same party
  905. * ADOPT_EQUIP_RINGS - Need wedding rings equipped
  906. * ADOPT_NOT_NOVICE - Adoptee is not a Novice
  907. * ADOPT_CHARACTER_NOT_FOUND - Parent or Baby not found
  908. * ADOPT_MORE_CHILDREN - Cannot adopt more than 1 Baby (client message)
  909. * ADOPT_LEVEL_70 - Parents need to be level 70+ (client message)
  910. * ADOPT_MARRIED - Cannot adopt a married person (client message)
  911. */
  912. enum adopt_responses pc_try_adopt(struct map_session_data *p1_sd, struct map_session_data *p2_sd, struct map_session_data *b_sd)
  913. {
  914. if( !p1_sd || !p2_sd || !b_sd )
  915. return ADOPT_CHARACTER_NOT_FOUND;
  916. if( b_sd->status.father || b_sd->status.mother || b_sd->adopt_invite )
  917. return ADOPT_ALREADY_ADOPTED; // already adopted baby / in adopt request
  918. if( !p1_sd->status.partner_id || !p1_sd->status.party_id || p1_sd->status.party_id != b_sd->status.party_id )
  919. return ADOPT_MARRIED_AND_PARTY; // You need to be married and in party with baby to adopt
  920. if( p1_sd->status.partner_id != p2_sd->status.char_id || p2_sd->status.partner_id != p1_sd->status.char_id )
  921. return ADOPT_MARRIED_AND_PARTY; // Not married, wrong married
  922. if( p2_sd->status.party_id != p1_sd->status.party_id )
  923. return ADOPT_MARRIED_AND_PARTY; // Both parents need to be in the same party
  924. // Parents need to have their ring equipped
  925. if( !pc_isequipped(p1_sd, WEDDING_RING_M) && !pc_isequipped(p1_sd, WEDDING_RING_F) )
  926. return ADOPT_EQUIP_RINGS;
  927. if( !pc_isequipped(p2_sd, WEDDING_RING_M) && !pc_isequipped(p2_sd, WEDDING_RING_F) )
  928. return ADOPT_EQUIP_RINGS;
  929. // Already adopted a baby
  930. if( p1_sd->status.child || p2_sd->status.child ) {
  931. clif_Adopt_reply(p1_sd, ADOPT_REPLY_MORE_CHILDREN);
  932. return ADOPT_MORE_CHILDREN;
  933. }
  934. // Parents need at least lvl 70 to adopt
  935. if( p1_sd->status.base_level < 70 || p2_sd->status.base_level < 70 ) {
  936. clif_Adopt_reply(p1_sd, ADOPT_REPLY_LEVEL_70);
  937. return ADOPT_LEVEL_70;
  938. }
  939. if( b_sd->status.partner_id ) {
  940. clif_Adopt_reply(p1_sd, ADOPT_REPLY_MARRIED);
  941. return ADOPT_MARRIED;
  942. }
  943. 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 ) )
  944. return ADOPT_NOT_NOVICE;
  945. return ADOPT_ALLOWED;
  946. }
  947. /*==========================================
  948. * Adoption Process
  949. *------------------------------------------*/
  950. bool pc_adoption(struct map_session_data *p1_sd, struct map_session_data *p2_sd, struct map_session_data *b_sd)
  951. {
  952. int job, joblevel;
  953. unsigned int jobexp;
  954. if( pc_try_adopt(p1_sd, p2_sd, b_sd) != ADOPT_ALLOWED )
  955. return false;
  956. // Preserve current job levels and progress
  957. joblevel = b_sd->status.job_level;
  958. jobexp = b_sd->status.job_exp;
  959. job = pc_mapid2jobid(b_sd->class_|JOBL_BABY, b_sd->status.sex);
  960. if( job != -1 && pc_jobchange(b_sd, job, 0) )
  961. { // Success, proceed to configure parents and baby skills
  962. p1_sd->status.child = b_sd->status.char_id;
  963. p2_sd->status.child = b_sd->status.char_id;
  964. b_sd->status.father = p1_sd->status.char_id;
  965. b_sd->status.mother = p2_sd->status.char_id;
  966. // Restore progress
  967. b_sd->status.job_level = joblevel;
  968. clif_updatestatus(b_sd, SP_JOBLEVEL);
  969. b_sd->status.job_exp = jobexp;
  970. clif_updatestatus(b_sd, SP_JOBEXP);
  971. // Baby Skills
  972. pc_skill(b_sd, WE_BABY, 1, ADDSKILL_PERMANENT);
  973. pc_skill(b_sd, WE_CALLPARENT, 1, ADDSKILL_PERMANENT);
  974. pc_skill(b_sd, WE_CHEERUP, 1, ADDSKILL_PERMANENT);
  975. // Parents Skills
  976. pc_skill(p1_sd, WE_CALLBABY, 1, ADDSKILL_PERMANENT);
  977. pc_skill(p2_sd, WE_CALLBABY, 1, ADDSKILL_PERMANENT);
  978. chrif_save(p1_sd, CSAVE_NORMAL);
  979. chrif_save(p2_sd, CSAVE_NORMAL);
  980. chrif_save(b_sd, CSAVE_NORMAL);
  981. achievement_update_objective(b_sd, AG_BABY, 1, 1);
  982. achievement_update_objective(p1_sd, AG_BABY, 1, 2);
  983. achievement_update_objective(p2_sd, AG_BABY, 1, 2);
  984. return true;
  985. }
  986. return false; // Job Change Fail
  987. }
  988. /*==========================================
  989. * Check if player can use/equip selected item. Used by pc_isUseitem and pc_isequip
  990. Returns:
  991. false : Cannot use/equip
  992. true : Can use/equip
  993. * Credits:
  994. [Inkfish] for first idea
  995. [Haru] for third-classes extension
  996. [Cydh] finishing :D
  997. *------------------------------------------*/
  998. static bool pc_isItemClass (struct map_session_data *sd, struct item_data* item) {
  999. while (1) {
  1000. if (item->class_upper&ITEMJ_NORMAL && !(sd->class_&(JOBL_UPPER|JOBL_THIRD|JOBL_BABY))) //normal classes (no upper, no baby, no third)
  1001. break;
  1002. #ifndef RENEWAL
  1003. //allow third classes to use trans. class items
  1004. if (item->class_upper&ITEMJ_UPPER && sd->class_&(JOBL_UPPER|JOBL_THIRD)) //trans. classes
  1005. break;
  1006. //third-baby classes can use same item too
  1007. if (item->class_upper&ITEMJ_BABY && sd->class_&JOBL_BABY) //baby classes
  1008. break;
  1009. //don't need to decide specific rules for third-classes?
  1010. //items for third classes can be used for all third classes
  1011. if (item->class_upper&(ITEMJ_THIRD|ITEMJ_THIRD_TRANS|ITEMJ_THIRD_BABY) && sd->class_&JOBL_THIRD)
  1012. break;
  1013. #else
  1014. //trans. classes (exl. third-trans.)
  1015. if (item->class_upper&ITEMJ_UPPER && sd->class_&JOBL_UPPER && !(sd->class_&JOBL_THIRD))
  1016. break;
  1017. //baby classes (exl. third-baby)
  1018. if (item->class_upper&ITEMJ_BABY && sd->class_&JOBL_BABY && !(sd->class_&JOBL_THIRD))
  1019. break;
  1020. //third classes (exl. third-trans. and baby-third)
  1021. if (item->class_upper&ITEMJ_THIRD && sd->class_&JOBL_THIRD && !(sd->class_&(JOBL_UPPER|JOBL_BABY)))
  1022. break;
  1023. //trans-third classes
  1024. if (item->class_upper&ITEMJ_THIRD_TRANS && sd->class_&JOBL_THIRD && sd->class_&JOBL_UPPER)
  1025. break;
  1026. //third-baby classes
  1027. if (item->class_upper&ITEMJ_THIRD_BABY && sd->class_&JOBL_THIRD && sd->class_&JOBL_BABY)
  1028. break;
  1029. #endif
  1030. return false;
  1031. }
  1032. return true;
  1033. }
  1034. /*=================================================
  1035. * Checks if the player can equip the item at index n in inventory.
  1036. * @param sd
  1037. * @param n Item index in inventory
  1038. * @return ITEM_EQUIP_ACK_OK(0) if can be equipped, or ITEM_EQUIP_ACK_FAIL(1)/ITEM_EQUIP_ACK_FAILLEVEL(2) if can't
  1039. *------------------------------------------------*/
  1040. uint8 pc_isequip(struct map_session_data *sd,int n)
  1041. {
  1042. struct item_data *item;
  1043. nullpo_retr(ITEM_EQUIP_ACK_FAIL, sd);
  1044. item = sd->inventory_data[n];
  1045. if(pc_has_permission(sd, PC_PERM_USE_ALL_EQUIPMENT))
  1046. return ITEM_EQUIP_ACK_OK;
  1047. if(item == NULL)
  1048. return ITEM_EQUIP_ACK_FAIL;
  1049. if(item->elv && sd->status.base_level < (unsigned int)item->elv)
  1050. return ITEM_EQUIP_ACK_FAILLEVEL;
  1051. if(item->elvmax && sd->status.base_level > (unsigned int)item->elvmax)
  1052. return ITEM_EQUIP_ACK_FAILLEVEL;
  1053. if(item->sex != 2 && sd->status.sex != item->sex)
  1054. return ITEM_EQUIP_ACK_FAIL;
  1055. //fail to equip if item is restricted
  1056. if (!battle_config.allow_equip_restricted_item && itemdb_isNoEquip(item, sd->bl.m))
  1057. return ITEM_EQUIP_ACK_FAIL;
  1058. if (item->equip&EQP_AMMO) {
  1059. switch (item->look) {
  1060. case AMMO_ARROW:
  1061. if (battle_config.ammo_check_weapon && sd->status.weapon != W_BOW && sd->status.weapon != W_MUSICAL && sd->status.weapon != W_WHIP) {
  1062. clif_msg(sd, ITEM_NEED_BOW);
  1063. return ITEM_EQUIP_ACK_FAIL;
  1064. }
  1065. break;
  1066. case AMMO_THROWABLE_DAGGER:
  1067. if (!pc_checkskill(sd, AS_VENOMKNIFE))
  1068. return ITEM_EQUIP_ACK_FAIL;
  1069. break;
  1070. case AMMO_BULLET:
  1071. case AMMO_SHELL:
  1072. 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
  1073. #ifdef RENEWAL
  1074. && sd->status.weapon != W_GRENADE
  1075. #endif
  1076. ) {
  1077. clif_msg(sd, ITEM_BULLET_EQUIP_FAIL);
  1078. return ITEM_EQUIP_ACK_FAIL;
  1079. }
  1080. break;
  1081. #ifndef RENEWAL
  1082. case AMMO_GRENADE:
  1083. if (battle_config.ammo_check_weapon && sd->status.weapon != W_GRENADE) {
  1084. clif_msg(sd, ITEM_BULLET_EQUIP_FAIL);
  1085. return ITEM_EQUIP_ACK_FAIL;
  1086. }
  1087. break;
  1088. #endif
  1089. case AMMO_CANNONBALL:
  1090. if (!pc_ismadogear(sd) && (sd->status.class_ == JOB_MECHANIC_T || sd->status.class_ == JOB_MECHANIC)) {
  1091. clif_msg(sd, ITEM_NEED_MADOGEAR); // Item can only be used when Mado Gear is mounted.
  1092. return ITEM_EQUIP_ACK_FAIL;
  1093. }
  1094. if (sd->state.active && !pc_iscarton(sd) && //Check if sc data is already loaded
  1095. (sd->status.class_ == JOB_GENETIC_T || sd->status.class_ == JOB_GENETIC)) {
  1096. clif_msg(sd, ITEM_NEED_CART); // Only available when cart is mounted.
  1097. return ITEM_EQUIP_ACK_FAIL;
  1098. }
  1099. break;
  1100. }
  1101. }
  1102. if (sd->sc.count) {
  1103. if(item->equip & EQP_ARMS && item->type == IT_WEAPON && sd->sc.data[SC_STRIPWEAPON]) // Also works with left-hand weapons [DracoRPG]
  1104. return ITEM_EQUIP_ACK_FAIL;
  1105. if(item->equip & EQP_SHIELD && item->type == IT_ARMOR && sd->sc.data[SC_STRIPSHIELD])
  1106. return ITEM_EQUIP_ACK_FAIL;
  1107. if(item->equip & EQP_ARMOR && sd->sc.data[SC_STRIPARMOR])
  1108. return ITEM_EQUIP_ACK_FAIL;
  1109. if(item->equip & EQP_HEAD_TOP && sd->sc.data[SC_STRIPHELM])
  1110. return ITEM_EQUIP_ACK_FAIL;
  1111. if(item->equip & EQP_ACC && sd->sc.data[SC__STRIPACCESSORY])
  1112. return ITEM_EQUIP_ACK_FAIL;
  1113. if (item->equip & EQP_ARMS && sd->sc.data[SC__WEAKNESS])
  1114. return ITEM_EQUIP_ACK_FAIL;
  1115. if(item->equip && (sd->sc.data[SC_KYOUGAKU] || sd->sc.data[SC_SUHIDE]))
  1116. return ITEM_EQUIP_ACK_FAIL;
  1117. if (sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_SUPERNOVICE) {
  1118. //Spirit of Super Novice equip bonuses. [Skotlex]
  1119. if (sd->status.base_level > 90 && item->equip & EQP_HELM)
  1120. return ITEM_EQUIP_ACK_OK; //Can equip all helms
  1121. if (sd->status.base_level > 96 && item->equip & EQP_ARMS && item->type == IT_WEAPON && item->wlv == 4)
  1122. switch(item->look) { //In weapons, the look determines type of weapon.
  1123. case W_DAGGER: //All level 4 - Daggers
  1124. case W_1HSWORD: //All level 4 - 1H Swords
  1125. case W_1HAXE: //All level 4 - 1H Axes
  1126. case W_MACE: //All level 4 - 1H Maces
  1127. case W_STAFF: //All level 4 - 1H Staves
  1128. case W_2HSTAFF: //All level 4 - 2H Staves
  1129. return ITEM_EQUIP_ACK_OK;
  1130. }
  1131. }
  1132. }
  1133. //Not equipable by class. [Skotlex]
  1134. if (!(1ULL << (sd->class_&MAPID_BASEMASK)&item->class_base[(sd->class_&JOBL_2_1) ? 1 : ((sd->class_&JOBL_2_2) ? 2 : 0)]))
  1135. return ITEM_EQUIP_ACK_FAIL;
  1136. if (!pc_isItemClass(sd, item))
  1137. return ITEM_EQUIP_ACK_FAIL;
  1138. return ITEM_EQUIP_ACK_OK;
  1139. }
  1140. /*==========================================
  1141. * No problem with the session id
  1142. * set the status that has been sent from char server
  1143. *------------------------------------------*/
  1144. 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)
  1145. {
  1146. int i;
  1147. t_tick tick = gettick();
  1148. uint32 ip = session[sd->fd]->client_addr;
  1149. sd->login_id2 = login_id2;
  1150. sd->group_id = group_id;
  1151. /* load user permissions */
  1152. pc_group_pc_load(sd);
  1153. memcpy(&sd->status, st, sizeof(*st));
  1154. if (st->sex != sd->status.sex) {
  1155. clif_authfail_fd(sd->fd, 0);
  1156. return false;
  1157. }
  1158. //Set the map-server used job id. [Skotlex]
  1159. i = pc_jobid2mapid(sd->status.class_);
  1160. if (i == -1) { //Invalid class?
  1161. 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);
  1162. sd->status.class_ = JOB_NOVICE;
  1163. sd->class_ = MAPID_NOVICE;
  1164. } else
  1165. sd->class_ = i;
  1166. // Checks and fixes to character status data, that are required
  1167. // in case of configuration change or stuff, which cannot be
  1168. // checked on char-server.
  1169. sd->status.hair = cap_value(sd->status.hair,MIN_HAIR_STYLE,MAX_HAIR_STYLE);
  1170. sd->status.hair_color = cap_value(sd->status.hair_color,MIN_HAIR_COLOR,MAX_HAIR_COLOR);
  1171. sd->status.clothes_color = cap_value(sd->status.clothes_color,MIN_CLOTH_COLOR,MAX_CLOTH_COLOR);
  1172. sd->status.body = cap_value(sd->status.body,MIN_BODY_STYLE,MAX_BODY_STYLE);
  1173. //Initializations to null/0 unneeded since map_session_data was filled with 0 upon allocation.
  1174. sd->state.connect_new = 1;
  1175. sd->followtimer = INVALID_TIMER; // [MouseJstr]
  1176. sd->invincible_timer = INVALID_TIMER;
  1177. sd->npc_timer_id = INVALID_TIMER;
  1178. sd->pvp_timer = INVALID_TIMER;
  1179. sd->expiration_tid = INVALID_TIMER;
  1180. sd->autotrade_tid = INVALID_TIMER;
  1181. #ifdef SECURE_NPCTIMEOUT
  1182. // Initialize to defaults/expected
  1183. sd->npc_idle_timer = INVALID_TIMER;
  1184. sd->npc_idle_tick = tick;
  1185. sd->npc_idle_type = NPCT_INPUT;
  1186. sd->state.ignoretimeout = false;
  1187. #endif
  1188. sd->canuseitem_tick = tick;
  1189. sd->canusecashfood_tick = tick;
  1190. sd->canequip_tick = tick;
  1191. sd->cantalk_tick = tick;
  1192. sd->canskill_tick = tick;
  1193. sd->cansendmail_tick = tick;
  1194. sd->idletime = last_tick;
  1195. for(i = 0; i < MAX_SPIRITBALL; i++)
  1196. sd->spirit_timer[i] = INVALID_TIMER;
  1197. if (battle_config.item_auto_get)
  1198. sd->state.autoloot = 10000;
  1199. if (battle_config.disp_experience)
  1200. sd->state.showexp = 1;
  1201. if (battle_config.disp_zeny)
  1202. sd->state.showzeny = 1;
  1203. #ifdef VIP_ENABLE
  1204. if (!battle_config.vip_disp_rate)
  1205. sd->vip.disableshowrate = 1;
  1206. #endif
  1207. if (!(battle_config.display_skill_fail&2))
  1208. sd->state.showdelay = 1;
  1209. memset(&sd->inventory, 0, sizeof(struct s_storage));
  1210. memset(&sd->cart, 0, sizeof(struct s_storage));
  1211. memset(&sd->storage, 0, sizeof(struct s_storage));
  1212. memset(&sd->premiumStorage, 0, sizeof(struct s_storage));
  1213. memset(&sd->equip_index, -1, sizeof(sd->equip_index));
  1214. memset(&sd->equip_switch_index, -1, sizeof(sd->equip_switch_index));
  1215. if( pc_isinvisible(sd) && !pc_can_use_command( sd, "hide", COMMAND_ATCOMMAND ) ){
  1216. sd->status.option &= ~OPTION_INVISIBLE;
  1217. }
  1218. status_change_init(&sd->bl);
  1219. sd->sc.option = sd->status.option; //This is the actual option used in battle.
  1220. unit_dataset(&sd->bl);
  1221. sd->guild_x = -1;
  1222. sd->guild_y = -1;
  1223. sd->delayed_damage = 0;
  1224. // Event Timers
  1225. for( i = 0; i < MAX_EVENTTIMER; i++ )
  1226. sd->eventtimer[i] = INVALID_TIMER;
  1227. // Rental Timer
  1228. sd->rental_timer = INVALID_TIMER;
  1229. for( i = 0; i < 3; i++ )
  1230. sd->hate_mob[i] = -1;
  1231. sd->quest_log = NULL;
  1232. sd->num_quests = 0;
  1233. sd->avail_quests = 0;
  1234. sd->save_quest = false;
  1235. sd->count_rewarp = 0;
  1236. sd->regs.vars = i64db_alloc(DB_OPT_BASE);
  1237. sd->regs.arrays = NULL;
  1238. sd->vars_dirty = false;
  1239. sd->vars_ok = false;
  1240. sd->vars_received = 0x0;
  1241. sd->qi_display = NULL;
  1242. sd->qi_count = 0;
  1243. //warp player
  1244. if ((i=pc_setpos(sd,sd->status.last_point.map, sd->status.last_point.x, sd->status.last_point.y, CLR_OUTSIGHT)) != SETPOS_OK) {
  1245. 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);
  1246. // try warping to a default map instead (church graveyard)
  1247. if (pc_setpos(sd, mapindex_name2id(MAP_PRONTERA), 273, 354, CLR_OUTSIGHT) != SETPOS_OK) {
  1248. // if we fail again
  1249. clif_authfail_fd(sd->fd, 0);
  1250. return false;
  1251. }
  1252. }
  1253. clif_authok(sd);
  1254. //Prevent S. Novices from getting the no-death bonus just yet. [Skotlex]
  1255. sd->die_counter=-1;
  1256. //display login notice
  1257. ShowInfo("'" CL_WHITE "%s" CL_RESET "' logged in."
  1258. " (AID/CID: '" CL_WHITE "%d/%d" CL_RESET "',"
  1259. " IP: '" CL_WHITE "%d.%d.%d.%d" CL_RESET "',"
  1260. " Group '" CL_WHITE "%d" CL_RESET "').\n",
  1261. sd->status.name, sd->status.account_id, sd->status.char_id,
  1262. CONVIP(ip), sd->group_id);
  1263. // Send friends list
  1264. clif_friendslist_send(sd);
  1265. if( !changing_mapservers ) {
  1266. if (battle_config.display_version == 1)
  1267. pc_show_version(sd);
  1268. // Message of the Day [Valaris]
  1269. for(i=0; i < MOTD_LINE_SIZE && motd_text[i][0]; i++) {
  1270. if (battle_config.motd_type)
  1271. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], motd_text[i], false, SELF);
  1272. else
  1273. clif_displaymessage(sd->fd, motd_text[i]);
  1274. }
  1275. if (expiration_time != 0)
  1276. sd->expiration_time = expiration_time;
  1277. /**
  1278. * Fixes login-without-aura glitch (the screen won't blink at this point, don't worry :P)
  1279. **/
  1280. clif_changemap(sd,sd->bl.m,sd->bl.x,sd->bl.y);
  1281. }
  1282. pc_validate_skill(sd);
  1283. /* [Ind] */
  1284. sd->sc_display = NULL;
  1285. sd->sc_display_count = 0;
  1286. // Player has not yet received the CashShop list
  1287. sd->status.cashshop_sent = false;
  1288. sd->last_addeditem_index = -1;
  1289. sd->bonus_script.head = NULL;
  1290. sd->bonus_script.count = 0;
  1291. #if PACKETVER >= 20150513
  1292. sd->hatEffectIDs = NULL;
  1293. sd->hatEffectCount = 0;
  1294. #endif
  1295. sd->catch_target_class = PET_CATCH_FAIL;
  1296. // Check EXP overflow, since in previous revision EXP on Max Level can be more than 'official' Max EXP
  1297. if (pc_is_maxbaselv(sd) && sd->status.base_exp > MAX_LEVEL_BASE_EXP) {
  1298. sd->status.base_exp = MAX_LEVEL_BASE_EXP;
  1299. clif_updatestatus(sd, SP_BASEEXP);
  1300. }
  1301. if (pc_is_maxjoblv(sd) && sd->status.job_exp > MAX_LEVEL_JOB_EXP) {
  1302. sd->status.job_exp = MAX_LEVEL_JOB_EXP;
  1303. clif_updatestatus(sd, SP_JOBEXP);
  1304. }
  1305. // Request all registries (auth is considered completed whence they arrive)
  1306. intif_request_registry(sd,7);
  1307. return true;
  1308. }
  1309. /*==========================================
  1310. * Closes a connection because it failed to be authenticated from the char server.
  1311. *------------------------------------------*/
  1312. void pc_authfail(struct map_session_data *sd)
  1313. {
  1314. clif_authfail_fd(sd->fd, 0);
  1315. return;
  1316. }
  1317. /**
  1318. * Player register a bl as hatred
  1319. * @param sd : player session
  1320. * @param pos : hate position [0;2]
  1321. * @param bl : target bl
  1322. * @return false:failed, true:success
  1323. */
  1324. bool pc_set_hate_mob(struct map_session_data *sd, int pos, struct block_list *bl)
  1325. {
  1326. int class_;
  1327. if (!sd || !bl || pos < 0 || pos > 2)
  1328. return false;
  1329. if (sd->hate_mob[pos] != -1)
  1330. { //Can't change hate targets.
  1331. clif_hate_info(sd, pos, sd->hate_mob[pos], 0); //Display current
  1332. return false;
  1333. }
  1334. class_ = status_get_class(bl);
  1335. if (!pcdb_checkid(class_)) {
  1336. unsigned int max_hp = status_get_max_hp(bl);
  1337. if ((pos == 1 && max_hp < 6000) || (pos == 2 && max_hp < 20000))
  1338. return false;
  1339. if (pos != status_get_size(bl))
  1340. return false; //Wrong size
  1341. }
  1342. sd->hate_mob[pos] = class_;
  1343. pc_setglobalreg(sd, add_str(sg_info[pos].hate_var), class_+1);
  1344. clif_hate_info(sd, pos, class_, 1);
  1345. return true;
  1346. }
  1347. /*==========================================
  1348. * Invoked once after the char/account/account2 registry variables are received. [Skotlex]
  1349. * We didn't receive item information at this point so DO NOT attempt to do item operations here.
  1350. * See intif_parse_StorageReceived() for item operations [lighta]
  1351. *------------------------------------------*/
  1352. void pc_reg_received(struct map_session_data *sd)
  1353. {
  1354. uint8 i;
  1355. sd->vars_ok = true;
  1356. sd->change_level_2nd = pc_readglobalreg(sd, add_str(JOBCHANGE2ND_VAR));
  1357. sd->change_level_3rd = pc_readglobalreg(sd, add_str(JOBCHANGE3RD_VAR));
  1358. sd->die_counter = pc_readglobalreg(sd, add_str(PCDIECOUNTER_VAR));
  1359. sd->langtype = pc_readaccountreg(sd, add_str(LANGTYPE_VAR));
  1360. if (msg_checklangtype(sd->langtype,true) < 0)
  1361. sd->langtype = 0; //invalid langtype reset to default
  1362. // Cash shop
  1363. sd->cashPoints = pc_readaccountreg(sd, add_str(CASHPOINT_VAR));
  1364. sd->kafraPoints = pc_readaccountreg(sd, add_str(KAFRAPOINT_VAR));
  1365. // Cooking Exp
  1366. sd->cook_mastery = pc_readglobalreg(sd, add_str(COOKMASTERY_VAR));
  1367. if( (sd->class_&MAPID_BASEMASK) == MAPID_TAEKWON )
  1368. { // Better check for class rather than skill to prevent "skill resets" from unsetting this
  1369. sd->mission_mobid = pc_readglobalreg(sd, add_str(TKMISSIONID_VAR));
  1370. sd->mission_count = pc_readglobalreg(sd, add_str(TKMISSIONCOUNT_VAR));
  1371. }
  1372. if (battle_config.feature_banking)
  1373. sd->bank_vault = pc_readreg2(sd, BANK_VAULT_VAR);
  1374. if (battle_config.feature_roulette) {
  1375. sd->roulette_point.bronze = pc_readreg2(sd, ROULETTE_BRONZE_VAR);
  1376. sd->roulette_point.silver = pc_readreg2(sd, ROULETTE_SILVER_VAR);
  1377. sd->roulette_point.gold = pc_readreg2(sd, ROULETTE_GOLD_VAR);
  1378. }
  1379. sd->roulette.prizeIdx = -1;
  1380. //SG map and mob read [Komurka]
  1381. for(i=0;i<MAX_PC_FEELHATE;i++) { //for now - someone need to make reading from txt/sql
  1382. uint16 j;
  1383. if ((j = pc_readglobalreg(sd, add_str(sg_info[i].feel_var))) != 0) {
  1384. sd->feel_map[i].index = j;
  1385. sd->feel_map[i].m = map_mapindex2mapid(j);
  1386. } else {
  1387. sd->feel_map[i].index = 0;
  1388. sd->feel_map[i].m = -1;
  1389. }
  1390. sd->hate_mob[i] = pc_readglobalreg(sd, add_str(sg_info[i].hate_var))-1;
  1391. }
  1392. if ((i = pc_checkskill(sd,RG_PLAGIARISM)) > 0) {
  1393. unsigned short skid = pc_readglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM));
  1394. sd->cloneskill_idx = skill_get_index(skid);
  1395. if (sd->cloneskill_idx > 0) {
  1396. sd->status.skill[sd->cloneskill_idx].id = skid;
  1397. sd->status.skill[sd->cloneskill_idx].lv = pc_readglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM_LV));
  1398. if (sd->status.skill[sd->cloneskill_idx].lv > i)
  1399. sd->status.skill[sd->cloneskill_idx].lv = i;
  1400. sd->status.skill[sd->cloneskill_idx].flag = SKILL_FLAG_PLAGIARIZED;
  1401. }
  1402. }
  1403. if ((i = pc_checkskill(sd,SC_REPRODUCE)) > 0) {
  1404. unsigned short skid = pc_readglobalreg(sd, add_str(SKILL_VAR_REPRODUCE));
  1405. sd->reproduceskill_idx = skill_get_index(skid);
  1406. if (sd->reproduceskill_idx > 0) {
  1407. sd->status.skill[sd->reproduceskill_idx].id = skid;
  1408. sd->status.skill[sd->reproduceskill_idx].lv = pc_readglobalreg(sd, add_str(SKILL_VAR_REPRODUCE_LV));
  1409. if (i < sd->status.skill[sd->reproduceskill_idx].lv)
  1410. sd->status.skill[sd->reproduceskill_idx].lv = i;
  1411. sd->status.skill[sd->reproduceskill_idx].flag = SKILL_FLAG_PLAGIARIZED;
  1412. }
  1413. }
  1414. //Weird... maybe registries were reloaded?
  1415. if (sd->state.active)
  1416. return;
  1417. sd->state.active = 1;
  1418. sd->state.pc_loaded = false; // Ensure inventory data and status data is loaded before we calculate player stats
  1419. intif_storage_request(sd,TABLE_STORAGE, 0, STOR_MODE_ALL); // Request storage data
  1420. intif_storage_request(sd,TABLE_CART, 0, STOR_MODE_ALL); // Request cart data
  1421. intif_storage_request(sd,TABLE_INVENTORY, 0, STOR_MODE_ALL); // Request inventory data
  1422. if (sd->status.party_id)
  1423. party_member_joined(sd);
  1424. if (sd->status.guild_id)
  1425. guild_member_joined(sd);
  1426. if( sd->status.clan_id )
  1427. clan_member_joined(sd);
  1428. // pet
  1429. if (sd->status.pet_id > 0)
  1430. intif_request_petdata(sd->status.account_id, sd->status.char_id, sd->status.pet_id);
  1431. // Homunculus [albator]
  1432. if( sd->status.hom_id > 0 )
  1433. intif_homunculus_requestload(sd->status.account_id, sd->status.hom_id);
  1434. if( sd->status.mer_id > 0 )
  1435. intif_mercenary_request(sd->status.mer_id, sd->status.char_id);
  1436. if( sd->status.ele_id > 0 )
  1437. intif_elemental_request(sd->status.ele_id, sd->status.char_id);
  1438. map_addiddb(&sd->bl);
  1439. map_delnickdb(sd->status.char_id, sd->status.name);
  1440. if (!chrif_auth_finished(sd))
  1441. ShowError("pc_reg_received: Failed to properly remove player %d:%d from logging db!\n", sd->status.account_id, sd->status.char_id);
  1442. chrif_skillcooldown_request(sd->status.account_id, sd->status.char_id);
  1443. chrif_bsdata_request(sd->status.char_id);
  1444. #ifdef VIP_ENABLE
  1445. sd->vip.time = 0;
  1446. sd->vip.enabled = 0;
  1447. chrif_req_login_operation(sd->status.account_id, sd->status.name, CHRIF_OP_LOGIN_VIP, 0, 1|8, 0); // request VIP information
  1448. #endif
  1449. intif_Mail_requestinbox(sd->status.char_id, 0, MAIL_INBOX_NORMAL); // MAIL SYSTEM - Request Mail Inbox
  1450. intif_request_questlog(sd);
  1451. if (battle_config.feature_achievement) {
  1452. sd->achievement_data.total_score = 0;
  1453. sd->achievement_data.level = 0;
  1454. sd->achievement_data.save = false;
  1455. sd->achievement_data.count = 0;
  1456. sd->achievement_data.incompleteCount = 0;
  1457. sd->achievement_data.achievements = NULL;
  1458. intif_request_achievements(sd->status.char_id);
  1459. }
  1460. if (sd->state.connect_new == 0 && sd->fd) { //Character already loaded map! Gotta trigger LoadEndAck manually.
  1461. sd->state.connect_new = 1;
  1462. clif_parse_LoadEndAck(sd->fd, sd);
  1463. }
  1464. if( pc_isinvisible(sd) ) {
  1465. sd->vd.class_ = JT_INVISIBLE;
  1466. clif_displaymessage( sd->fd, msg_txt( sd, 11 ) ); // Invisible: On
  1467. // decrement the number of pvp players on the map
  1468. map_getmapdata(sd->bl.m)->users_pvp--;
  1469. if( map_getmapflag(sd->bl.m, MF_PVP) && !map_getmapflag(sd->bl.m, MF_PVP_NOCALCRANK) && sd->pvp_timer != INVALID_TIMER ){
  1470. // unregister the player for ranking
  1471. delete_timer( sd->pvp_timer, pc_calc_pvprank_timer );
  1472. sd->pvp_timer = INVALID_TIMER;
  1473. }
  1474. clif_changeoption( &sd->bl );
  1475. }
  1476. channel_autojoin(sd);
  1477. }
  1478. static int pc_calc_skillpoint(struct map_session_data* sd)
  1479. {
  1480. uint16 i, skill_point = 0;
  1481. nullpo_ret(sd);
  1482. for(i = 1; i < MAX_SKILL; i++) {
  1483. if( sd->status.skill[i].id && sd->status.skill[i].lv > 0) {
  1484. uint16 inf2 = skill_get_inf2(sd->status.skill[i].id);
  1485. if ((!(inf2&INF2_QUEST_SKILL) || battle_config.quest_skill_learn) &&
  1486. !(inf2&(INF2_WEDDING_SKILL|INF2_SPIRIT_SKILL)) //Do not count wedding/link skills. [Skotlex]
  1487. )
  1488. {
  1489. if(sd->status.skill[i].flag == SKILL_FLAG_PERMANENT)
  1490. skill_point += sd->status.skill[i].lv;
  1491. else if(sd->status.skill[i].flag >= SKILL_FLAG_REPLACED_LV_0)
  1492. skill_point += (sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0);
  1493. }
  1494. }
  1495. }
  1496. return skill_point;
  1497. }
  1498. static bool pc_grant_allskills(struct map_session_data *sd, bool addlv) {
  1499. uint16 i = 0;
  1500. if (!sd || !pc_has_permission(sd, PC_PERM_ALL_SKILL) || !SKILL_MAX_DB())
  1501. return false;
  1502. /**
  1503. * Dummy skills must NOT be added here otherwise they'll be displayed in the,
  1504. * skill tree and since they have no icons they'll give resource errors
  1505. * Get ALL skills except npc/guild ones. [Skotlex]
  1506. * Don't add SG_DEVIL [Komurka] and MO_TRIPLEATTACK and RG_SNATCHER [ultramage]
  1507. **/
  1508. for( i = 0; i < MAX_SKILL; i++ ) {
  1509. uint16 skill_id = skill_idx2id(i);
  1510. if (!skill_id || (skill_get_inf2(skill_id)&(INF2_NPC_SKILL|INF2_GUILD_SKILL)))
  1511. continue;
  1512. switch (skill_id) {
  1513. case SM_SELFPROVOKE:
  1514. case AB_DUPLELIGHT_MELEE:
  1515. case AB_DUPLELIGHT_MAGIC:
  1516. case WL_CHAINLIGHTNING_ATK:
  1517. case WL_TETRAVORTEX_FIRE:
  1518. case WL_TETRAVORTEX_WATER:
  1519. case WL_TETRAVORTEX_WIND:
  1520. case WL_TETRAVORTEX_GROUND:
  1521. case WL_SUMMON_ATK_FIRE:
  1522. case WL_SUMMON_ATK_WIND:
  1523. case WL_SUMMON_ATK_WATER:
  1524. case WL_SUMMON_ATK_GROUND:
  1525. case LG_OVERBRAND_BRANDISH:
  1526. case LG_OVERBRAND_PLUSATK:
  1527. case WM_SEVERE_RAINSTORM_MELEE:
  1528. case RL_R_TRIP_PLUSATK:
  1529. case SG_DEVIL:
  1530. case MO_TRIPLEATTACK:
  1531. case RG_SNATCHER:
  1532. continue;
  1533. default:
  1534. {
  1535. uint8 lv = (uint8)skill_get_max(skill_id);
  1536. if (lv > 0) {
  1537. sd->status.skill[i].id = skill_id;
  1538. if (addlv)
  1539. sd->status.skill[i].lv = lv;
  1540. }
  1541. }
  1542. break;
  1543. }
  1544. }
  1545. return true;
  1546. }
  1547. /*==========================================
  1548. * Calculation of skill level.
  1549. * @param sd
  1550. *------------------------------------------*/
  1551. void pc_calc_skilltree(struct map_session_data *sd)
  1552. {
  1553. int i, flag;
  1554. int c = 0;
  1555. nullpo_retv(sd);
  1556. i = pc_calc_skilltree_normalize_job(sd);
  1557. c = pc_mapid2jobid(i, sd->status.sex);
  1558. if( c == -1 )
  1559. { //Unable to normalize job??
  1560. ShowError("pc_calc_skilltree: Unable to normalize job %d for character %s (%d:%d)\n", i, sd->status.name, sd->status.account_id, sd->status.char_id);
  1561. return;
  1562. }
  1563. c = pc_class2idx(c);
  1564. for( i = 0; i < MAX_SKILL; i++ ) {
  1565. if( sd->status.skill[i].flag != SKILL_FLAG_PLAGIARIZED && sd->status.skill[i].flag != SKILL_FLAG_PERM_GRANTED ) //Don't touch these
  1566. sd->status.skill[i].id = 0; //First clear skills.
  1567. /* permanent skills that must be re-checked */
  1568. if( sd->status.skill[i].flag == SKILL_FLAG_PERM_GRANTED ) {
  1569. uint16 sk_id = skill_idx2id(i);
  1570. if (!sk_id) {
  1571. sd->status.skill[i].id = 0;
  1572. sd->status.skill[i].lv = 0;
  1573. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1574. continue;
  1575. }
  1576. switch (sk_id) {
  1577. case NV_TRICKDEAD:
  1578. if( (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE ) {
  1579. sd->status.skill[i].id = 0;
  1580. sd->status.skill[i].lv = 0;
  1581. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1582. }
  1583. break;
  1584. }
  1585. }
  1586. }
  1587. for( i = 0; i < MAX_SKILL; i++ ) {
  1588. uint16 skill_id = 0;
  1589. // Restore original level of skills after deleting earned skills.
  1590. 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 ) {
  1591. sd->status.skill[i].lv = (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY) ? 0 : sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
  1592. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1593. }
  1594. //Enable Bard/Dancer spirit linked skills.
  1595. if (!(skill_id = skill_idx2id(i)) || skill_id < DC_HUMMING || skill_id > DC_SERVICEFORYOU)
  1596. continue;
  1597. if( sd->sc.count && sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_BARDDANCER ) {
  1598. //Link Dancer skills to bard.
  1599. if( sd->status.sex ) {
  1600. if( sd->status.skill[i-8].lv < 10 )
  1601. continue;
  1602. sd->status.skill[i].id = skill_id;
  1603. sd->status.skill[i].lv = sd->status.skill[i-8].lv; // Set the level to the same as the linking skill
  1604. sd->status.skill[i].flag = SKILL_FLAG_TEMPORARY; // Tag it as a non-savable, non-uppable, bonus skill
  1605. }
  1606. //Link Bard skills to dancer.
  1607. else {
  1608. if( sd->status.skill[i].lv < 10 )
  1609. continue;
  1610. sd->status.skill[i-8].id = skill_id - 8;
  1611. sd->status.skill[i-8].lv = sd->status.skill[i].lv; // Set the level to the same as the linking skill
  1612. sd->status.skill[i-8].flag = SKILL_FLAG_TEMPORARY; // Tag it as a non-savable, non-uppable, bonus skill
  1613. }
  1614. }
  1615. }
  1616. // Removes Taekwon Ranker skill bonus
  1617. if ((sd->class_&MAPID_UPPERMASK) != MAPID_TAEKWON) {
  1618. uint16 c_ = pc_class2idx(JOB_TAEKWON);
  1619. for (i = 0; i < MAX_SKILL_TREE; i++) {
  1620. uint16 sk_id = skill_tree[c_][i].skill_id;
  1621. uint16 sk_idx = 0;
  1622. if (!sk_id || !(sk_idx = skill_get_index(skill_tree[c_][i].skill_id)))
  1623. continue;
  1624. if (sd->status.skill[sk_idx].flag != SKILL_FLAG_PLAGIARIZED && sd->status.skill[sk_idx].flag != SKILL_FLAG_PERM_GRANTED) {
  1625. if (sk_id == NV_BASIC || sk_id == NV_FIRSTAID || sk_id == WE_CALLBABY)
  1626. continue;
  1627. sd->status.skill[sk_idx].id = 0;
  1628. }
  1629. }
  1630. }
  1631. // Grant all skills
  1632. pc_grant_allskills(sd, false);
  1633. do {
  1634. uint16 skid = 0;
  1635. flag = 0;
  1636. for (i = 0; i < MAX_SKILL_TREE && (skid = skill_tree[c][i].skill_id) > 0; i++) {
  1637. bool fail = false;
  1638. uint16 sk_idx = skill_get_index(skid);
  1639. if (sd->status.skill[sk_idx].id)
  1640. continue; //Skill already known.
  1641. if (!battle_config.skillfree) {
  1642. uint8 j;
  1643. // Checking required skills
  1644. for(j = 0; j < MAX_PC_SKILL_REQUIRE; j++) {
  1645. uint16 sk_need_id = skill_tree[c][i].need[j].skill_id;
  1646. uint16 sk_need_idx = 0;
  1647. if (sk_need_id && (sk_need_idx = skill_get_index(sk_need_id))) {
  1648. short sk_need = sk_need_id;
  1649. 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)
  1650. sk_need = 0; //Not learned.
  1651. else if (sd->status.skill[sk_need_idx].flag >= SKILL_FLAG_REPLACED_LV_0) //Real learned level
  1652. sk_need = sd->status.skill[sk_need_idx].flag - SKILL_FLAG_REPLACED_LV_0;
  1653. else
  1654. sk_need = pc_checkskill(sd,sk_need_id);
  1655. if (sk_need < skill_tree[c][i].need[j].skill_lv) {
  1656. fail = true;
  1657. break;
  1658. }
  1659. }
  1660. }
  1661. if (sd->status.base_level < skill_tree[c][i].baselv) { //We need to get the actual class in this case
  1662. int class_ = pc_mapid2jobid(sd->class_, sd->status.sex);
  1663. class_ = pc_class2idx(class_);
  1664. if (class_ == c || (class_ != c && sd->status.base_level < skill_tree[class_][i].baselv))
  1665. fail = true; // base level requirement wasn't satisfied
  1666. }
  1667. if (sd->status.job_level < skill_tree[c][i].joblv) { //We need to get the actual class in this case
  1668. int class_ = pc_mapid2jobid(sd->class_, sd->status.sex);
  1669. class_ = pc_class2idx(class_);
  1670. if (class_ == c || (class_ != c && sd->status.job_level < skill_tree[class_][i].joblv))
  1671. fail = true; // job level requirement wasn't satisfied
  1672. }
  1673. }
  1674. if (!fail) {
  1675. int inf2 = skill_get_inf2(skid);
  1676. if (!sd->status.skill[sk_idx].lv && (
  1677. (inf2&INF2_QUEST_SKILL && !battle_config.quest_skill_learn) ||
  1678. inf2&INF2_WEDDING_SKILL ||
  1679. (inf2&INF2_SPIRIT_SKILL && !sd->sc.data[SC_SPIRIT])
  1680. ))
  1681. continue; //Cannot be learned via normal means. Note this check DOES allows raising already known skills.
  1682. sd->status.skill[sk_idx].id = skid;
  1683. if(inf2&INF2_SPIRIT_SKILL) { //Spirit skills cannot be learned, they will only show up on your tree when you get buffed.
  1684. sd->status.skill[sk_idx].lv = 1; // need to manually specify a skill level
  1685. sd->status.skill[sk_idx].flag = SKILL_FLAG_TEMPORARY; //So it is not saved, and tagged as a "bonus" skill.
  1686. }
  1687. flag = 1; // skill list has changed, perform another pass
  1688. }
  1689. }
  1690. } while(flag);
  1691. if( c > 0 && sd->status.skill_point == 0 && pc_is_taekwon_ranker(sd) ) {
  1692. unsigned short skid = 0;
  1693. /* Taekwon Ranker Bonus Skill Tree
  1694. ============================================
  1695. - Grant All Taekwon Tree, but only as Bonus Skills in case they drop from ranking.
  1696. - (c > 0) to avoid grant Novice Skill Tree in case of Skill Reset (need more logic)
  1697. - (sd->status.skill_point == 0) to wait until all skill points are assigned to avoid problems with Job Change quest. */
  1698. for( i = 0; i < MAX_SKILL_TREE && (skid = skill_tree[c][i].skill_id) > 0; i++ ) {
  1699. uint16 sk_idx = 0;
  1700. if (!(sk_idx = skill_get_index(skid)))
  1701. continue;
  1702. if( (skill_get_inf2(skid)&(INF2_QUEST_SKILL|INF2_WEDDING_SKILL)) )
  1703. continue; //Do not include Quest/Wedding skills.
  1704. if( sd->status.skill[sk_idx].id == 0 ) {
  1705. sd->status.skill[sk_idx].id = skid;
  1706. sd->status.skill[sk_idx].flag = SKILL_FLAG_TEMPORARY; // So it is not saved, and tagged as a "bonus" skill.
  1707. } else if( skid != NV_BASIC )
  1708. sd->status.skill[sk_idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[sk_idx].lv; // Remember original level
  1709. sd->status.skill[sk_idx].lv = skill_tree_get_max(skid, sd->status.class_);
  1710. }
  1711. }
  1712. }
  1713. //Checks if you can learn a new skill after having leveled up a skill.
  1714. static void pc_check_skilltree(struct map_session_data *sd)
  1715. {
  1716. int i, flag = 0;
  1717. int c = 0;
  1718. if (battle_config.skillfree)
  1719. return; //Function serves no purpose if this is set
  1720. i = pc_calc_skilltree_normalize_job(sd);
  1721. c = pc_mapid2jobid(i, sd->status.sex);
  1722. if (c == -1) { //Unable to normalize job??
  1723. 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);
  1724. return;
  1725. }
  1726. c = pc_class2idx(c);
  1727. do {
  1728. uint16 skid = 0;
  1729. flag = 0;
  1730. for (i = 0; i < MAX_SKILL_TREE && (skid = skill_tree[c][i].skill_id) > 0; i++ ) {
  1731. uint16 sk_idx = skill_get_index(skid);
  1732. bool fail = false;
  1733. uint8 j = 0;
  1734. if (sd->status.skill[sk_idx].id) //Already learned
  1735. continue;
  1736. // Checking required skills
  1737. for (j = 0; j < MAX_PC_SKILL_REQUIRE; j++) {
  1738. uint16 sk_need_id = skill_tree[c][i].need[j].skill_id;
  1739. uint16 sk_need_idx = 0;
  1740. if (sk_need_id && (sk_need_idx = skill_get_index(sk_need_id))) {
  1741. short sk_need = sk_need_id;
  1742. 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)
  1743. sk_need = 0; //Not learned.
  1744. else if (sd->status.skill[sk_need_idx].flag >= SKILL_FLAG_REPLACED_LV_0) //Real lerned level
  1745. sk_need = sd->status.skill[sk_need_idx].flag - SKILL_FLAG_REPLACED_LV_0;
  1746. else
  1747. sk_need = pc_checkskill(sd,sk_need_id);
  1748. if (sk_need < skill_tree[c][i].need[j].skill_lv) {
  1749. fail = true;
  1750. break;
  1751. }
  1752. }
  1753. }
  1754. if( fail )
  1755. continue;
  1756. if (sd->status.base_level < skill_tree[c][i].baselv || sd->status.job_level < skill_tree[c][i].joblv)
  1757. continue;
  1758. j = skill_get_inf2(skid);
  1759. if( !sd->status.skill[sk_idx].lv && (
  1760. (j&INF2_QUEST_SKILL && !battle_config.quest_skill_learn) ||
  1761. j&INF2_WEDDING_SKILL ||
  1762. (j&INF2_SPIRIT_SKILL && !sd->sc.data[SC_SPIRIT])
  1763. ) )
  1764. continue; //Cannot be learned via normal means.
  1765. sd->status.skill[sk_idx].id = skid;
  1766. flag = 1;
  1767. }
  1768. } while(flag);
  1769. }
  1770. // Make sure all the skills are in the correct condition
  1771. // before persisting to the backend.. [MouseJstr]
  1772. void pc_clean_skilltree(struct map_session_data *sd)
  1773. {
  1774. uint16 i;
  1775. for (i = 0; i < MAX_SKILL; i++){
  1776. if (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY || sd->status.skill[i].flag == SKILL_FLAG_PLAGIARIZED) {
  1777. sd->status.skill[i].id = 0;
  1778. sd->status.skill[i].lv = 0;
  1779. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1780. }
  1781. else if (sd->status.skill[i].flag >= SKILL_FLAG_REPLACED_LV_0){
  1782. sd->status.skill[i].lv = sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
  1783. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1784. }
  1785. }
  1786. }
  1787. int pc_calc_skilltree_normalize_job(struct map_session_data *sd)
  1788. {
  1789. int skill_point, novice_skills;
  1790. int c = sd->class_;
  1791. if (!battle_config.skillup_limit || pc_has_permission(sd, PC_PERM_ALL_SKILL))
  1792. return c;
  1793. skill_point = pc_calc_skillpoint(sd);
  1794. novice_skills = job_info[pc_class2idx(JOB_NOVICE)].max_level[1] - 1;
  1795. // limit 1st class and above to novice job levels
  1796. if(skill_point < novice_skills && (sd->class_&MAPID_BASEMASK) != MAPID_SUMMONER)
  1797. {
  1798. c = MAPID_NOVICE;
  1799. }
  1800. // limit 2nd class and above to first class job levels (super novices are exempt)
  1801. else if (sd->class_&JOBL_2 && (sd->class_&MAPID_UPPERMASK) != MAPID_SUPER_NOVICE)
  1802. {
  1803. // regenerate change_level_2nd
  1804. if (!sd->change_level_2nd)
  1805. {
  1806. if (sd->class_&JOBL_THIRD)
  1807. {
  1808. // if neither 2nd nor 3rd jobchange levels are known, we have to assume a default for 2nd
  1809. if (!sd->change_level_3rd)
  1810. sd->change_level_2nd = job_info[pc_class2idx(pc_mapid2jobid(sd->class_&MAPID_UPPERMASK, sd->status.sex))].max_level[1];
  1811. else
  1812. sd->change_level_2nd = 1 + skill_point + sd->status.skill_point
  1813. - (sd->status.job_level - 1)
  1814. - (sd->change_level_3rd - 1)
  1815. - novice_skills;
  1816. }
  1817. else
  1818. {
  1819. sd->change_level_2nd = 1 + skill_point + sd->status.skill_point
  1820. - (sd->status.job_level - 1)
  1821. - novice_skills;
  1822. }
  1823. pc_setglobalreg(sd, add_str(JOBCHANGE2ND_VAR), sd->change_level_2nd);
  1824. }
  1825. if (skill_point < novice_skills + (sd->change_level_2nd - 1))
  1826. {
  1827. c &= MAPID_BASEMASK;
  1828. }
  1829. // limit 3rd class to 2nd class/trans job levels
  1830. else if(sd->class_&JOBL_THIRD)
  1831. {
  1832. // regenerate change_level_3rd
  1833. if (!sd->change_level_3rd)
  1834. {
  1835. sd->change_level_3rd = 1 + skill_point + sd->status.skill_point
  1836. - (sd->status.job_level - 1)
  1837. - (sd->change_level_2nd - 1)
  1838. - novice_skills;
  1839. pc_setglobalreg(sd, add_str(JOBCHANGE3RD_VAR), sd->change_level_3rd);
  1840. }
  1841. if (skill_point < novice_skills + (sd->change_level_2nd - 1) + (sd->change_level_3rd - 1))
  1842. c &= MAPID_UPPERMASK;
  1843. }
  1844. }
  1845. // restore non-limiting flags
  1846. c |= sd->class_&(JOBL_UPPER|JOBL_BABY);
  1847. return c;
  1848. }
  1849. /*==========================================
  1850. * Updates the weight status
  1851. *------------------------------------------
  1852. * 1: overweight 50% for pre-renewal and 70% for renewal
  1853. * 2: overweight 90%
  1854. * It's assumed that SC_WEIGHT50 and SC_WEIGHT90 are only started/stopped here.
  1855. */
  1856. void pc_updateweightstatus(struct map_session_data *sd)
  1857. {
  1858. int old_overweight;
  1859. int new_overweight;
  1860. nullpo_retv(sd);
  1861. old_overweight = (sd->sc.data[SC_WEIGHT90]) ? 2 : (sd->sc.data[SC_WEIGHT50]) ? 1 : 0;
  1862. #ifdef RENEWAL
  1863. new_overweight = (pc_is90overweight(sd)) ? 2 : (pc_is70overweight(sd)) ? 1 : 0;
  1864. #else
  1865. new_overweight = (pc_is90overweight(sd)) ? 2 : (pc_is50overweight(sd)) ? 1 : 0;
  1866. #endif
  1867. if( old_overweight == new_overweight )
  1868. return; // no change
  1869. // stop old status change
  1870. if( old_overweight == 1 )
  1871. status_change_end(&sd->bl, SC_WEIGHT50, INVALID_TIMER);
  1872. else if( old_overweight == 2 )
  1873. status_change_end(&sd->bl, SC_WEIGHT90, INVALID_TIMER);
  1874. // start new status change
  1875. if( new_overweight == 1 )
  1876. sc_start(&sd->bl,&sd->bl, SC_WEIGHT50, 100, 0, 0);
  1877. else if( new_overweight == 2 )
  1878. sc_start(&sd->bl,&sd->bl, SC_WEIGHT90, 100, 0, 0);
  1879. // update overweight status
  1880. sd->regen.state.overweight = new_overweight;
  1881. }
  1882. int pc_disguise(struct map_session_data *sd, int class_)
  1883. {
  1884. if (!class_ && !sd->disguise)
  1885. return 0;
  1886. if (class_ && sd->disguise == class_)
  1887. return 0;
  1888. if(pc_isinvisible(sd))
  1889. { //Character is invisible. Stealth class-change. [Skotlex]
  1890. sd->disguise = class_; //viewdata is set on uncloaking.
  1891. return 2;
  1892. }
  1893. if (sd->bl.prev != NULL) {
  1894. pc_stop_walking(sd, 0);
  1895. clif_clearunit_area(&sd->bl, CLR_OUTSIGHT);
  1896. }
  1897. if (!class_) {
  1898. sd->disguise = 0;
  1899. class_ = sd->status.class_;
  1900. } else
  1901. sd->disguise=class_;
  1902. status_set_viewdata(&sd->bl, class_);
  1903. clif_changeoption(&sd->bl);
  1904. if (sd->bl.prev != NULL) {
  1905. clif_spawn(&sd->bl);
  1906. if (class_ == sd->status.class_ && pc_iscarton(sd))
  1907. { //It seems the cart info is lost on undisguise.
  1908. clif_cartlist(sd);
  1909. clif_updatestatus(sd,SP_CARTINFO);
  1910. }
  1911. if (sd->chatID) {
  1912. struct chat_data* cd;
  1913. if ((cd = (struct chat_data*)map_id2bl(sd->chatID)) != NULL)
  1914. clif_dispchat(cd,0);
  1915. }
  1916. }
  1917. return 1;
  1918. }
  1919. /// Show error message
  1920. #define PC_BONUS_SHOW_ERROR(type,type2,val) { ShowError("%s: %s: Invalid %s %d.\n",__FUNCTION__,#type,#type2,(val)); break; }
  1921. /// Check for valid Element, break & show error message if invalid Element
  1922. #define PC_BONUS_CHK_ELEMENT(ele,bonus) { if (!CHK_ELEMENT((ele))) { PC_BONUS_SHOW_ERROR((bonus),Element,(ele)); }}
  1923. /// Check for valid Race, break & show error message if invalid Race
  1924. #define PC_BONUS_CHK_RACE(rc,bonus) { if (!CHK_RACE((rc))) { PC_BONUS_SHOW_ERROR((bonus),Race,(rc)); }}
  1925. /// Check for valid Race2, break & show error message if invalid Race2
  1926. #define PC_BONUS_CHK_RACE2(rc2,bonus) { if (!CHK_RACE2((rc2))) { PC_BONUS_SHOW_ERROR((bonus),Race2,(rc2)); }}
  1927. /// Check for valid Class, break & show error message if invalid Class
  1928. #define PC_BONUS_CHK_CLASS(cl,bonus) { if (!CHK_CLASS((cl))) { PC_BONUS_SHOW_ERROR((bonus),Class,(cl)); }}
  1929. /// Check for valid Size, break & show error message if invalid Size
  1930. #define PC_BONUS_CHK_SIZE(sz,bonus) { if (!CHK_MOBSIZE((sz))) { PC_BONUS_SHOW_ERROR((bonus),Size,(sz)); }}
  1931. /// Check for valid SC, break & show error message if invalid SC
  1932. #define PC_BONUS_CHK_SC(sc,bonus) { if ((sc) <= SC_NONE || (sc) >= SC_MAX) { PC_BONUS_SHOW_ERROR((bonus),Effect,(sc)); }}
  1933. /**
  1934. * Add auto spell bonus for player while attacking/attacked
  1935. * @param spell: Spell array
  1936. * @param id: Skill to cast
  1937. * @param lv: Skill level
  1938. * @param rate: Success chance
  1939. * @param flag: Battle flag
  1940. * @param card_id: Used to prevent card stacking
  1941. */
  1942. static void pc_bonus_autospell(std::vector<s_autospell> &spell, short id, short lv, short rate, short flag, unsigned short card_id)
  1943. {
  1944. if (spell.size() == MAX_PC_BONUS) {
  1945. ShowWarning("pc_bonus_autospell: Reached max (%d) number of autospells per character!\n", MAX_PC_BONUS);
  1946. return;
  1947. }
  1948. if (!rate)
  1949. return;
  1950. if (!(flag&BF_RANGEMASK))
  1951. flag |= BF_SHORT | BF_LONG; //No range defined? Use both.
  1952. if (!(flag&BF_WEAPONMASK))
  1953. flag |= BF_WEAPON; //No attack type defined? Use weapon.
  1954. if (!(flag&BF_SKILLMASK)) {
  1955. if (flag&(BF_MAGIC | BF_MISC))
  1956. flag |= BF_SKILL; //These two would never trigger without BF_SKILL
  1957. if (flag&BF_WEAPON)
  1958. flag |= BF_NORMAL; //By default autospells should only trigger on normal weapon attacks.
  1959. }
  1960. if (!battle_config.autospell_stacking && rate > 0) // Stacking disabled, make a new entry
  1961. ;
  1962. else {
  1963. for (auto &it : spell) {
  1964. if ((it.card_id == card_id || it.rate < 0 || rate < 0) && it.id == id && it.lv == lv && it.flag == flag) {
  1965. it.rate = cap_value(it.rate + rate, -10000, 10000);
  1966. return;
  1967. }
  1968. }
  1969. }
  1970. struct s_autospell entry = {};
  1971. if (rate < -10000 || rate > 10000)
  1972. ShowWarning("pc_bonus_autospell: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  1973. entry.id = id;
  1974. entry.lv = lv;
  1975. entry.rate = cap_value(rate, -10000, 10000);
  1976. entry.flag = flag;
  1977. entry.card_id = card_id;
  1978. spell.push_back(entry);
  1979. }
  1980. /**
  1981. * Add auto spell bonus for player while using skills
  1982. * @param spell: Spell array
  1983. * @param src_skill: Trigger skill
  1984. * @param id: Support or target type
  1985. * @param lv: Skill level
  1986. * @param rate: Success chance
  1987. * @param card_id: Used to prevent card stacking
  1988. */
  1989. static void pc_bonus_autospell_onskill(std::vector<s_autospell> &spell, short src_skill, short id, short lv, short rate, unsigned short card_id)
  1990. {
  1991. if (spell.size() == MAX_PC_BONUS) {
  1992. ShowWarning("pc_bonus_autospell_onskill: Reached max (%d) number of autospells per character!\n", MAX_PC_BONUS);
  1993. return;
  1994. }
  1995. if (!rate)
  1996. return;
  1997. struct s_autospell entry = {};
  1998. if (rate < -10000 || rate > 10000)
  1999. ShowWarning("pc_bonus_onskill: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2000. entry.flag = src_skill;
  2001. entry.id = id;
  2002. entry.lv = lv;
  2003. entry.rate = cap_value(rate, -10000, 10000);
  2004. entry.card_id = card_id;
  2005. spell.push_back(entry);
  2006. }
  2007. /**
  2008. * Add inflict effect bonus for player while attacking/attacked
  2009. * @param effect: Effect array
  2010. * @param sc: SC/Effect type
  2011. * @param rate: Success chance
  2012. * @param arrow_rate: success chance if bonus comes from arrow-type item
  2013. * @param flag: Target flag
  2014. * @param duration: Duration. If 0 use default duration lookup for associated skill with level 7
  2015. */
  2016. 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)
  2017. {
  2018. if (effect.size() == MAX_PC_BONUS) {
  2019. ShowWarning("pc_bonus_addeff: Reached max (%d) number of add effects per character!\n", MAX_PC_BONUS);
  2020. return;
  2021. }
  2022. if (!(flag&(ATF_SHORT | ATF_LONG)))
  2023. flag |= ATF_SHORT | ATF_LONG; //Default range: both
  2024. if (!(flag&(ATF_TARGET | ATF_SELF)))
  2025. flag |= ATF_TARGET; //Default target: enemy.
  2026. if (!(flag&(ATF_WEAPON | ATF_MAGIC | ATF_MISC)))
  2027. flag |= ATF_WEAPON; //Default type: weapon.
  2028. if (!duration)
  2029. duration = (unsigned int)skill_get_time2(status_sc2skill(sc), 7);
  2030. for (auto &it : effect) {
  2031. if (it.sc == sc && it.flag == flag) {
  2032. it.rate = cap_value(it.rate + rate, -10000, 10000);
  2033. it.arrow_rate += arrow_rate;
  2034. it.duration = umax(it.duration, duration);
  2035. return;
  2036. }
  2037. }
  2038. struct s_addeffect entry = {};
  2039. if (rate < -10000 || rate > 10000)
  2040. ShowWarning("pc_bonus_addeff: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2041. entry.sc = sc;
  2042. entry.rate = cap_value(rate, -10000, 10000);
  2043. entry.arrow_rate = arrow_rate;
  2044. entry.flag = flag;
  2045. entry.duration = duration;
  2046. effect.push_back(entry);
  2047. }
  2048. /**
  2049. * Add inflict effect bonus for player while attacking using skill
  2050. * @param effect: Effect array
  2051. * @param sc: SC/Effect type
  2052. * @param rate: Success chance
  2053. * @param skill_id: Skill to cast
  2054. * @param target: Target type
  2055. * @param duration: Duration. If 0 use default duration lookup for associated skill with level 7
  2056. */
  2057. 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)
  2058. {
  2059. if (effect.size() == MAX_PC_BONUS) {
  2060. ShowWarning("pc_bonus_addeff_onskill: Reached max (%d) number of add effects per character!\n", MAX_PC_BONUS);
  2061. return;
  2062. }
  2063. if (!duration)
  2064. duration = (unsigned int)skill_get_time2(status_sc2skill(sc), 7);
  2065. for (auto &it : effect) {
  2066. if (it.sc == sc && it.skill_id == skill_id && it.target == target) {
  2067. it.rate = cap_value(it.rate + rate, -10000, 10000);
  2068. it.duration = umax(it.duration, duration);
  2069. return;
  2070. }
  2071. }
  2072. struct s_addeffectonskill entry = {};
  2073. if (rate < -10000 || rate > 10000)
  2074. ShowWarning("pc_bonus_addeff_onskill: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2075. entry.sc = sc;
  2076. entry.rate = cap_value(rate, -10000, 10000);
  2077. entry.skill_id = skill_id;
  2078. entry.target = target;
  2079. entry.duration = duration;
  2080. effect.push_back(entry);
  2081. }
  2082. /**
  2083. * Adjust/add drop rate modifier for player
  2084. * @param drop: Player's sd->add_drop (struct s_add_drop)
  2085. * @param nameid: item id that will be dropped
  2086. * @param group: group id
  2087. * @param class_: target class
  2088. * @param race: target race. if < 0, means monster_id
  2089. * @param rate: rate value: 1 ~ 10000. If < 0, it will be multiplied with mob level/10
  2090. */
  2091. static void pc_bonus_item_drop(std::vector<s_add_drop> &drop, unsigned short nameid, uint16 group, int class_, short race, int rate)
  2092. {
  2093. if (!nameid && !group) {
  2094. ShowWarning("pc_bonus_item_drop: No Item ID nor Item Group ID specified.\n");
  2095. return;
  2096. }
  2097. if (nameid && !itemdb_exists(nameid)) {
  2098. ShowWarning("pc_bonus_item_drop: Invalid item id %hu\n",nameid);
  2099. return;
  2100. }
  2101. if (group && !itemdb_group_exists(group)) {
  2102. ShowWarning("pc_bonus_item_drop: Invalid Item Group %hu\n",group);
  2103. return;
  2104. }
  2105. if (drop.size() == MAX_PC_BONUS) {
  2106. 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);
  2107. return;
  2108. }
  2109. //Apply config rate adjustment settings.
  2110. if (rate >= 0) { //Absolute drop.
  2111. if (battle_config.item_rate_adddrop != 100)
  2112. rate = rate*battle_config.item_rate_adddrop/100;
  2113. if (rate < battle_config.item_drop_adddrop_min)
  2114. rate = battle_config.item_drop_adddrop_min;
  2115. else if (rate > battle_config.item_drop_adddrop_max)
  2116. rate = battle_config.item_drop_adddrop_max;
  2117. } else { //Relative drop, max/min limits are applied at drop time.
  2118. if (battle_config.item_rate_adddrop != 100)
  2119. rate = rate*battle_config.item_rate_adddrop/100;
  2120. if (rate > -1)
  2121. rate = -1;
  2122. }
  2123. for (auto &it : drop) {
  2124. if (it.nameid == nameid && it.group == group && it.race == race && it.class_ == class_) {
  2125. if ((rate < 0 && it.rate < 0) || (rate > 0 && it.rate > 0)) //Adjust the rate if it has same classification
  2126. it.rate = cap_value(it.rate + rate, -10000, 10000);
  2127. return;
  2128. }
  2129. }
  2130. struct s_add_drop entry = {};
  2131. if (rate < -10000 || rate > 10000)
  2132. ShowWarning("pc_bonus_item_drop: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2133. entry.nameid = nameid;
  2134. entry.group = group;
  2135. entry.race = race;
  2136. entry.class_ = class_;
  2137. entry.rate = cap_value(rate, -10000, 10000);
  2138. drop.push_back(entry);
  2139. }
  2140. /**
  2141. * Add autobonus to player when attacking/attacked
  2142. * @param bonus: Bonus array
  2143. * @param script: Script to execute
  2144. * @param rate: Success chance
  2145. * @param dur: Duration
  2146. * @param flag: Battle flag/skill
  2147. * @param other_script: Secondary script to execute
  2148. * @param pos: Item equip position
  2149. * @param onskill: Skill used to trigger autobonus
  2150. * @return True on success or false otherwise
  2151. */
  2152. 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)
  2153. {
  2154. if (bonus.size() == MAX_PC_BONUS) {
  2155. ShowWarning("pc_addautobonus: Reached max (%d) number of autobonus per character!\n", MAX_PC_BONUS);
  2156. return false;
  2157. }
  2158. if (!onskill) {
  2159. if (!(flag&BF_RANGEMASK))
  2160. flag |= BF_SHORT | BF_LONG; //No range defined? Use both.
  2161. if (!(flag&BF_WEAPONMASK))
  2162. flag |= BF_WEAPON; //No attack type defined? Use weapon.
  2163. if (!(flag&BF_SKILLMASK)) {
  2164. if (flag&(BF_MAGIC | BF_MISC))
  2165. flag |= BF_SKILL; //These two would never trigger without BF_SKILL
  2166. if (flag&BF_WEAPON)
  2167. flag |= BF_NORMAL | BF_SKILL;
  2168. }
  2169. }
  2170. struct s_autobonus entry = {};
  2171. if (rate < -10000 || rate > 10000)
  2172. ShowWarning("pc_addautobonus: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2173. entry.rate = cap_value(rate, -10000, 10000);
  2174. entry.duration = dur;
  2175. entry.active = INVALID_TIMER;
  2176. entry.atk_type = flag;
  2177. entry.pos = pos;
  2178. entry.bonus_script = aStrdup(script);
  2179. entry.other_script = (other_script ? aStrdup(other_script) : NULL);
  2180. bonus.push_back(entry);
  2181. return true;
  2182. }
  2183. /**
  2184. * Remove an autobonus from player
  2185. * @param sd: Player data
  2186. * @param bonus: Autobonus array
  2187. * @param restore: Run script on clearing or not
  2188. */
  2189. void pc_delautobonus(struct map_session_data* sd, std::vector<s_autobonus> &bonus, bool restore)
  2190. {
  2191. nullpo_retv(sd);
  2192. std::vector<s_autobonus>::iterator it = bonus.begin();
  2193. while( it != bonus.end() ){
  2194. s_autobonus b = *it;
  2195. if (b.active != INVALID_TIMER) {
  2196. if (restore && (sd->state.autobonus&b.pos) == b.pos) {
  2197. if (b.bonus_script) {
  2198. unsigned int equip_pos_idx = 0;
  2199. // Create a list of all equipped positions to see if all items needed for the autobonus are still present [Playtester]
  2200. for (uint8 j = 0; j < EQI_MAX; j++) {
  2201. if (sd->equip_index[j] >= 0)
  2202. equip_pos_idx |= sd->inventory.u.items_inventory[sd->equip_index[j]].equip;
  2203. }
  2204. if ((equip_pos_idx&b.pos) == b.pos)
  2205. script_run_autobonus(b.bonus_script, sd, b.pos);
  2206. }
  2207. it++;
  2208. continue;
  2209. } else { // Logout / Unequipped an item with an activated bonus
  2210. delete_timer(b.active, pc_endautobonus);
  2211. b.active = INVALID_TIMER;
  2212. }
  2213. }
  2214. if (b.bonus_script)
  2215. aFree(b.bonus_script);
  2216. if (b.other_script)
  2217. aFree(b.other_script);
  2218. it = bonus.erase(it);
  2219. }
  2220. }
  2221. /**
  2222. * Execute autobonus on player
  2223. * @param sd: Player data
  2224. * @param autobonus: Autobonus to run
  2225. */
  2226. void pc_exeautobonus(struct map_session_data *sd, std::vector<s_autobonus> *bonus, struct s_autobonus *autobonus)
  2227. {
  2228. nullpo_retv(sd);
  2229. nullpo_retv(autobonus);
  2230. if (autobonus->active != INVALID_TIMER)
  2231. delete_timer(autobonus->active, pc_endautobonus);
  2232. if( autobonus->other_script )
  2233. {
  2234. int j;
  2235. unsigned int equip_pos_idx = 0;
  2236. //Create a list of all equipped positions to see if all items needed for the autobonus are still present [Playtester]
  2237. for(j = 0; j < EQI_MAX; j++) {
  2238. if(sd->equip_index[j] >= 0)
  2239. equip_pos_idx |= sd->inventory.u.items_inventory[sd->equip_index[j]].equip;
  2240. }
  2241. if((equip_pos_idx&autobonus->pos) == autobonus->pos)
  2242. script_run_autobonus(autobonus->other_script,sd,autobonus->pos);
  2243. }
  2244. autobonus->active = add_timer(gettick()+autobonus->duration, pc_endautobonus, sd->bl.id, (intptr_t)bonus);
  2245. sd->state.autobonus |= autobonus->pos;
  2246. status_calc_pc(sd,SCO_FORCE);
  2247. }
  2248. /**
  2249. * Remove autobonus timer from player
  2250. */
  2251. TIMER_FUNC(pc_endautobonus){
  2252. struct map_session_data *sd = map_id2sd(id);
  2253. std::vector<s_autobonus> *bonus = (std::vector<s_autobonus> *)data;
  2254. nullpo_ret(sd);
  2255. nullpo_ret(bonus);
  2256. for( struct s_autobonus& autobonus : *bonus ){
  2257. if( autobonus.active == tid ){
  2258. autobonus.active = INVALID_TIMER;
  2259. sd->state.autobonus &= ~autobonus.pos;
  2260. break;
  2261. }
  2262. }
  2263. status_calc_pc(sd,SCO_FORCE);
  2264. return 0;
  2265. }
  2266. /**
  2267. * Add element bonus to player when attacking
  2268. * @param sd: Player data
  2269. * @param ele: Element to adjust
  2270. * @param rate: Success chance
  2271. * @param flag: Battle flag
  2272. */
  2273. static void pc_bonus_addele(struct map_session_data* sd, unsigned char ele, short rate, short flag)
  2274. {
  2275. nullpo_retv(sd);
  2276. struct weapon_data *wd = (sd->state.lr_flag ? &sd->left_weapon : &sd->right_weapon);
  2277. if (wd->addele2.size() == MAX_PC_BONUS) {
  2278. ShowWarning("pc_bonus_addele: Reached max (%d) number of add element damage bonuses per character!\n", MAX_PC_BONUS);
  2279. return;
  2280. }
  2281. if (!(flag&BF_RANGEMASK))
  2282. flag |= BF_SHORT | BF_LONG;
  2283. if (!(flag&BF_WEAPONMASK))
  2284. flag |= BF_WEAPON;
  2285. if (!(flag&BF_SKILLMASK)) {
  2286. if (flag&(BF_MAGIC | BF_MISC))
  2287. flag |= BF_SKILL;
  2288. if (flag&BF_WEAPON)
  2289. flag |= BF_NORMAL | BF_SKILL;
  2290. }
  2291. for (auto &it : wd->addele2) {
  2292. if (it.ele == ele && it.flag == flag) {
  2293. it.rate = cap_value(it.rate + rate, -10000, 10000);
  2294. return;
  2295. }
  2296. }
  2297. struct s_addele2 entry = {};
  2298. if (rate < -10000 || rate > 10000)
  2299. ShowWarning("pc_bonus_addele: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2300. entry.ele = ele;
  2301. entry.rate = cap_value(rate, -10000, 10000);
  2302. entry.flag = flag;
  2303. wd->addele2.push_back(entry);
  2304. }
  2305. /**
  2306. * Reduce element bonus to player when attacking
  2307. * @param sd: Player data
  2308. * @param ele: Element to adjust
  2309. * @param rate: Success chance
  2310. * @param flag: Battle flag
  2311. */
  2312. static void pc_bonus_subele(struct map_session_data* sd, unsigned char ele, short rate, short flag)
  2313. {
  2314. nullpo_retv(sd);
  2315. if (sd->subele2.size() == MAX_PC_BONUS) {
  2316. ShowWarning("pc_bonus_subele: Reached max (%d) number of resist element damage bonuses per character!\n", MAX_PC_BONUS);
  2317. return;
  2318. }
  2319. if (!(flag&BF_RANGEMASK))
  2320. flag |= BF_SHORT | BF_LONG;
  2321. if (!(flag&BF_WEAPONMASK))
  2322. flag |= BF_WEAPON;
  2323. if (!(flag&BF_SKILLMASK)) {
  2324. if (flag&(BF_MAGIC | BF_MISC))
  2325. flag |= BF_SKILL;
  2326. if (flag&BF_WEAPON)
  2327. flag |= BF_NORMAL | BF_SKILL;
  2328. }
  2329. for (auto &it : sd->subele2) {
  2330. if (it.ele == ele && it.flag == flag) {
  2331. it.rate = cap_value(it.rate + rate, -10000, 10000);
  2332. return;
  2333. }
  2334. }
  2335. struct s_addele2 entry = {};
  2336. if (rate < -10000 || rate > 10000)
  2337. ShowWarning("pc_bonus_subele: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2338. entry.ele = ele;
  2339. entry.rate = cap_value(rate, -10000, 10000);
  2340. entry.flag = flag;
  2341. sd->subele2.push_back(entry);
  2342. }
  2343. /**
  2344. * General item bonus for player
  2345. * @param bonus: Bonus array
  2346. * @param id: Key
  2347. * @param val: Value
  2348. * @param cap_rate: If Value is a rate value that needs to be capped
  2349. */
  2350. static void pc_bonus_itembonus(std::vector<s_item_bonus> &bonus, uint16 id, int val, bool cap_rate)
  2351. {
  2352. for (auto &it : bonus) {
  2353. if (it.id == id) {
  2354. if (cap_rate)
  2355. it.val = cap_value(it.val + val, -10000, 10000);
  2356. else
  2357. it.val += val;
  2358. return;
  2359. }
  2360. }
  2361. struct s_item_bonus entry = {};
  2362. if (cap_rate && (val < -10000 || val > 10000)) {
  2363. ShowWarning("pc_bonus_itembonus: Item bonus val %d exceeds -10000~10000 range, capping.\n", val);
  2364. val = cap_value(val, -10000, 10000);
  2365. }
  2366. entry.id = id;
  2367. entry.val = val;
  2368. bonus.push_back(entry);
  2369. }
  2370. /**
  2371. * Remove HP/SP to player when attacking
  2372. * @param bonus: Bonus array
  2373. * @param rate: Success chance
  2374. * @param per: Percentage of HP/SP to vanish
  2375. * @param flag: Battle flag
  2376. */
  2377. static void pc_bonus_addvanish(std::vector<s_vanish_bonus> &bonus, int16 rate, int16 per, int flag) {
  2378. if (bonus.size() == MAX_PC_BONUS) {
  2379. ShowWarning("pc_bonus_addvanish: Reached max (%d) number of vanish damage bonuses per character!\n", MAX_PC_BONUS);
  2380. return;
  2381. }
  2382. if (!(flag&BF_RANGEMASK))
  2383. flag |= BF_SHORT | BF_LONG;
  2384. if (!(flag&BF_WEAPONMASK))
  2385. flag |= BF_WEAPON;
  2386. if (!(flag&BF_SKILLMASK)) {
  2387. if (flag&(BF_MAGIC | BF_MISC))
  2388. flag |= BF_SKILL;
  2389. if (flag&BF_WEAPON)
  2390. flag |= BF_NORMAL | BF_SKILL;
  2391. }
  2392. for (auto &it : bonus) {
  2393. if (it.flag == flag) {
  2394. it.rate = cap_value(it.rate + rate, -10000, 10000);
  2395. it.per += per;
  2396. return;
  2397. }
  2398. }
  2399. struct s_vanish_bonus entry = {};
  2400. if (rate < -10000 || rate > 10000)
  2401. ShowWarning("pc_bonus_addvanish: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2402. entry.rate = cap_value(rate, -10000, 10000);
  2403. entry.per = per;
  2404. entry.flag = flag;
  2405. bonus.push_back(entry);
  2406. }
  2407. /*==========================================
  2408. * Add a bonus(type) to player sd
  2409. * format: bonus bBonusName,val;
  2410. * @param sd
  2411. * @param type Bonus type used by bBonusName
  2412. * @param val Value that usually for rate or fixed value
  2413. *------------------------------------------*/
  2414. void pc_bonus(struct map_session_data *sd,int type,int val)
  2415. {
  2416. struct status_data *status;
  2417. int bonus;
  2418. nullpo_retv(sd);
  2419. status = &sd->base_status;
  2420. switch(type){
  2421. case SP_STR:
  2422. case SP_AGI:
  2423. case SP_VIT:
  2424. case SP_INT:
  2425. case SP_DEX:
  2426. case SP_LUK:
  2427. if(sd->state.lr_flag != 2)
  2428. sd->param_bonus[type-SP_STR]+=val;
  2429. break;
  2430. case SP_ATK1:
  2431. if(!sd->state.lr_flag) {
  2432. bonus = status->rhw.atk + val;
  2433. status->rhw.atk = cap_value(bonus, 0, USHRT_MAX);
  2434. }
  2435. else if(sd->state.lr_flag == 1) {
  2436. bonus = status->lhw.atk + val;
  2437. status->lhw.atk = cap_value(bonus, 0, USHRT_MAX);
  2438. }
  2439. break;
  2440. case SP_ATK2:
  2441. if(!sd->state.lr_flag) {
  2442. bonus = status->rhw.atk2 + val;
  2443. status->rhw.atk2 = cap_value(bonus, 0, USHRT_MAX);
  2444. }
  2445. else if(sd->state.lr_flag == 1) {
  2446. bonus = status->lhw.atk2 + val;
  2447. status->lhw.atk2 = cap_value(bonus, 0, USHRT_MAX);
  2448. }
  2449. break;
  2450. case SP_BASE_ATK:
  2451. if(sd->state.lr_flag != 2) {
  2452. #ifdef RENEWAL
  2453. bonus = sd->bonus.eatk + val;
  2454. sd->bonus.eatk = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2455. #else
  2456. bonus = status->batk + val;
  2457. status->batk = cap_value(bonus, 0, USHRT_MAX);
  2458. #endif
  2459. }
  2460. break;
  2461. case SP_DEF1:
  2462. if(sd->state.lr_flag != 2) {
  2463. bonus = status->def + val;
  2464. #ifdef RENEWAL
  2465. status->def = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2466. #else
  2467. status->def = cap_value(bonus, CHAR_MIN, CHAR_MAX);
  2468. #endif
  2469. }
  2470. break;
  2471. case SP_DEF2:
  2472. if(sd->state.lr_flag != 2) {
  2473. bonus = status->def2 + val;
  2474. status->def2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2475. }
  2476. break;
  2477. case SP_MDEF1:
  2478. if(sd->state.lr_flag != 2) {
  2479. bonus = status->mdef + val;
  2480. #ifdef RENEWAL
  2481. status->mdef = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2482. #else
  2483. status->mdef = cap_value(bonus, CHAR_MIN, CHAR_MAX);
  2484. #endif
  2485. if( sd->state.lr_flag == 3 ) {//Shield, used for royal guard
  2486. sd->bonus.shieldmdef += bonus;
  2487. }
  2488. }
  2489. break;
  2490. case SP_MDEF2:
  2491. if(sd->state.lr_flag != 2) {
  2492. bonus = status->mdef2 + val;
  2493. status->mdef2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2494. }
  2495. break;
  2496. case SP_HIT:
  2497. if(sd->state.lr_flag != 2) {
  2498. bonus = status->hit + val;
  2499. status->hit = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2500. } else
  2501. sd->bonus.arrow_hit+=val;
  2502. break;
  2503. case SP_FLEE1:
  2504. if(sd->state.lr_flag != 2) {
  2505. bonus = status->flee + val;
  2506. status->flee = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2507. }
  2508. break;
  2509. case SP_FLEE2:
  2510. if(sd->state.lr_flag != 2) {
  2511. bonus = status->flee2 + val*10;
  2512. status->flee2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2513. }
  2514. break;
  2515. case SP_CRITICAL:
  2516. if(sd->state.lr_flag != 2) {
  2517. bonus = status->cri + val*10;
  2518. status->cri = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2519. } else
  2520. sd->bonus.arrow_cri += val*10;
  2521. break;
  2522. case SP_ATKELE:
  2523. PC_BONUS_CHK_ELEMENT(val,SP_ATKELE);
  2524. switch (sd->state.lr_flag)
  2525. {
  2526. case 2:
  2527. switch (sd->status.weapon) {
  2528. case W_BOW:
  2529. case W_REVOLVER:
  2530. case W_RIFLE:
  2531. case W_GATLING:
  2532. case W_SHOTGUN:
  2533. case W_GRENADE:
  2534. //Become weapon element.
  2535. status->rhw.ele=val;
  2536. break;
  2537. default: //Become arrow element.
  2538. sd->bonus.arrow_ele=val;
  2539. break;
  2540. }
  2541. break;
  2542. case 1:
  2543. status->lhw.ele=val;
  2544. break;
  2545. default:
  2546. status->rhw.ele=val;
  2547. break;
  2548. }
  2549. break;
  2550. case SP_DEFELE:
  2551. PC_BONUS_CHK_ELEMENT(val,SP_DEFELE);
  2552. if(sd->state.lr_flag != 2)
  2553. status->def_ele=val;
  2554. break;
  2555. case SP_MAXHP:
  2556. if(sd->state.lr_flag == 2)
  2557. break;
  2558. sd->bonus.hp += val;
  2559. break;
  2560. case SP_MAXSP:
  2561. if(sd->state.lr_flag == 2)
  2562. break;
  2563. sd->bonus.sp += val;
  2564. break;
  2565. case SP_MAXHPRATE:
  2566. if(sd->state.lr_flag != 2)
  2567. sd->hprate+=val;
  2568. break;
  2569. case SP_MAXSPRATE:
  2570. if(sd->state.lr_flag != 2)
  2571. sd->sprate+=val;
  2572. break;
  2573. case SP_SPRATE:
  2574. if(sd->state.lr_flag != 2)
  2575. sd->dsprate+=val;
  2576. break;
  2577. case SP_ATTACKRANGE:
  2578. switch (sd->state.lr_flag) {
  2579. case 2:
  2580. switch (sd->status.weapon) {
  2581. case W_BOW:
  2582. case W_REVOLVER:
  2583. case W_RIFLE:
  2584. case W_GATLING:
  2585. case W_SHOTGUN:
  2586. case W_GRENADE:
  2587. status->rhw.range += val;
  2588. }
  2589. break;
  2590. case 1:
  2591. status->lhw.range += val;
  2592. break;
  2593. default:
  2594. status->rhw.range += val;
  2595. break;
  2596. }
  2597. break;
  2598. case SP_SPEED_RATE: //Non stackable increase
  2599. if(sd->state.lr_flag != 2)
  2600. sd->bonus.speed_rate = min(sd->bonus.speed_rate, -val);
  2601. break;
  2602. case SP_SPEED_ADDRATE: //Stackable increase
  2603. if(sd->state.lr_flag != 2)
  2604. sd->bonus.speed_add_rate -= val;
  2605. break;
  2606. case SP_ASPD: //Raw increase
  2607. if(sd->state.lr_flag != 2)
  2608. sd->bonus.aspd_add -= 10*val;
  2609. break;
  2610. case SP_ASPD_RATE: //Stackable increase - Made it linear as per rodatazone
  2611. if(sd->state.lr_flag != 2)
  2612. #ifndef RENEWAL_ASPD
  2613. status->aspd_rate -= 10*val;
  2614. #else
  2615. status->aspd_rate2 += val;
  2616. #endif
  2617. break;
  2618. case SP_HP_RECOV_RATE:
  2619. if(sd->state.lr_flag != 2)
  2620. sd->hprecov_rate += val;
  2621. break;
  2622. case SP_SP_RECOV_RATE:
  2623. if(sd->state.lr_flag != 2)
  2624. sd->sprecov_rate += val;
  2625. break;
  2626. case SP_CRITICAL_DEF:
  2627. if(sd->state.lr_flag != 2)
  2628. sd->bonus.critical_def += val;
  2629. break;
  2630. case SP_NEAR_ATK_DEF:
  2631. if(sd->state.lr_flag != 2)
  2632. sd->bonus.near_attack_def_rate += val;
  2633. break;
  2634. case SP_LONG_ATK_DEF:
  2635. if(sd->state.lr_flag != 2)
  2636. sd->bonus.long_attack_def_rate += val;
  2637. break;
  2638. case SP_DOUBLE_RATE:
  2639. if(sd->state.lr_flag == 0 && sd->bonus.double_rate < val)
  2640. sd->bonus.double_rate = val;
  2641. break;
  2642. case SP_DOUBLE_ADD_RATE:
  2643. if(sd->state.lr_flag == 0)
  2644. sd->bonus.double_add_rate += val;
  2645. break;
  2646. case SP_MATK_RATE:
  2647. if(sd->state.lr_flag != 2)
  2648. sd->matk_rate += val;
  2649. break;
  2650. case SP_IGNORE_DEF_ELE:
  2651. PC_BONUS_CHK_ELEMENT(val,SP_IGNORE_DEF_ELE);
  2652. if(!sd->state.lr_flag)
  2653. sd->right_weapon.ignore_def_ele |= 1<<val;
  2654. else if(sd->state.lr_flag == 1)
  2655. sd->left_weapon.ignore_def_ele |= 1<<val;
  2656. break;
  2657. case SP_IGNORE_DEF_RACE:
  2658. PC_BONUS_CHK_RACE(val,SP_IGNORE_DEF_RACE);
  2659. if(!sd->state.lr_flag)
  2660. sd->right_weapon.ignore_def_race |= 1<<val;
  2661. else if(sd->state.lr_flag == 1)
  2662. sd->left_weapon.ignore_def_race |= 1<<val;
  2663. break;
  2664. case SP_IGNORE_DEF_CLASS:
  2665. PC_BONUS_CHK_CLASS(val,SP_IGNORE_DEF_CLASS);
  2666. if(!sd->state.lr_flag)
  2667. sd->right_weapon.ignore_def_class |= 1<<val;
  2668. else if(sd->state.lr_flag == 1)
  2669. sd->left_weapon.ignore_def_class |= 1<<val;
  2670. break;
  2671. case SP_ATK_RATE:
  2672. if(sd->state.lr_flag != 2)
  2673. sd->bonus.atk_rate += val;
  2674. break;
  2675. case SP_MAGIC_ATK_DEF:
  2676. if(sd->state.lr_flag != 2)
  2677. sd->bonus.magic_def_rate += val;
  2678. break;
  2679. case SP_MISC_ATK_DEF:
  2680. if(sd->state.lr_flag != 2)
  2681. sd->bonus.misc_def_rate += val;
  2682. break;
  2683. case SP_IGNORE_MDEF_ELE:
  2684. PC_BONUS_CHK_ELEMENT(val,SP_IGNORE_MDEF_ELE);
  2685. if(sd->state.lr_flag != 2)
  2686. sd->bonus.ignore_mdef_ele |= 1<<val;
  2687. break;
  2688. case SP_IGNORE_MDEF_RACE:
  2689. PC_BONUS_CHK_RACE(val,SP_IGNORE_MDEF_RACE);
  2690. if(sd->state.lr_flag != 2)
  2691. sd->bonus.ignore_mdef_race |= 1<<val;
  2692. break;
  2693. case SP_PERFECT_HIT_RATE:
  2694. if(sd->state.lr_flag != 2 && sd->bonus.perfect_hit < val)
  2695. sd->bonus.perfect_hit = val;
  2696. break;
  2697. case SP_PERFECT_HIT_ADD_RATE:
  2698. if(sd->state.lr_flag != 2)
  2699. sd->bonus.perfect_hit_add += val;
  2700. break;
  2701. case SP_CRITICAL_RATE:
  2702. if(sd->state.lr_flag != 2)
  2703. sd->critical_rate+=val;
  2704. break;
  2705. case SP_DEF_RATIO_ATK_ELE:
  2706. PC_BONUS_CHK_ELEMENT(val,SP_DEF_RATIO_ATK_ELE);
  2707. if(!sd->state.lr_flag)
  2708. sd->right_weapon.def_ratio_atk_ele |= 1<<val;
  2709. else if(sd->state.lr_flag == 1)
  2710. sd->left_weapon.def_ratio_atk_ele |= 1<<val;
  2711. break;
  2712. case SP_DEF_RATIO_ATK_RACE:
  2713. PC_BONUS_CHK_RACE(val,SP_DEF_RATIO_ATK_RACE);
  2714. if(!sd->state.lr_flag)
  2715. sd->right_weapon.def_ratio_atk_race |= 1<<val;
  2716. else if(sd->state.lr_flag == 1)
  2717. sd->left_weapon.def_ratio_atk_race |= 1<<val;
  2718. break;
  2719. case SP_DEF_RATIO_ATK_CLASS:
  2720. PC_BONUS_CHK_CLASS(val,SP_DEF_RATIO_ATK_CLASS);
  2721. if(!sd->state.lr_flag)
  2722. sd->right_weapon.def_ratio_atk_class |= 1<<val;
  2723. else if(sd->state.lr_flag == 1)
  2724. sd->left_weapon.def_ratio_atk_class |= 1<<val;
  2725. break;
  2726. case SP_HIT_RATE:
  2727. if(sd->state.lr_flag != 2)
  2728. sd->hit_rate += val;
  2729. break;
  2730. case SP_FLEE_RATE:
  2731. if(sd->state.lr_flag != 2)
  2732. sd->flee_rate += val;
  2733. break;
  2734. case SP_FLEE2_RATE:
  2735. if(sd->state.lr_flag != 2)
  2736. sd->flee2_rate += val;
  2737. break;
  2738. case SP_DEF_RATE:
  2739. if(sd->state.lr_flag != 2)
  2740. sd->def_rate += val;
  2741. break;
  2742. case SP_DEF2_RATE:
  2743. if(sd->state.lr_flag != 2)
  2744. sd->def2_rate += val;
  2745. break;
  2746. case SP_MDEF_RATE:
  2747. if(sd->state.lr_flag != 2)
  2748. sd->mdef_rate += val;
  2749. break;
  2750. case SP_MDEF2_RATE:
  2751. if(sd->state.lr_flag != 2)
  2752. sd->mdef2_rate += val;
  2753. break;
  2754. case SP_RESTART_FULL_RECOVER:
  2755. if(sd->state.lr_flag != 2)
  2756. sd->special_state.restart_full_recover = 1;
  2757. break;
  2758. case SP_NO_CASTCANCEL:
  2759. if(sd->state.lr_flag != 2)
  2760. sd->special_state.no_castcancel = 1;
  2761. break;
  2762. case SP_NO_CASTCANCEL2:
  2763. if(sd->state.lr_flag != 2)
  2764. sd->special_state.no_castcancel2 = 1;
  2765. break;
  2766. case SP_NO_SIZEFIX:
  2767. if(sd->state.lr_flag != 2)
  2768. sd->special_state.no_sizefix = 1;
  2769. break;
  2770. case SP_NO_MAGIC_DAMAGE:
  2771. if(sd->state.lr_flag == 2)
  2772. break;
  2773. val+= sd->special_state.no_magic_damage;
  2774. sd->special_state.no_magic_damage = cap_value(val,0,100);
  2775. break;
  2776. case SP_NO_WEAPON_DAMAGE:
  2777. if(sd->state.lr_flag == 2)
  2778. break;
  2779. val+= sd->special_state.no_weapon_damage;
  2780. sd->special_state.no_weapon_damage = cap_value(val,0,100);
  2781. break;
  2782. case SP_NO_MISC_DAMAGE:
  2783. if(sd->state.lr_flag == 2)
  2784. break;
  2785. val+= sd->special_state.no_misc_damage;
  2786. sd->special_state.no_misc_damage = cap_value(val,0,100);
  2787. break;
  2788. case SP_NO_GEMSTONE:
  2789. if(sd->state.lr_flag != 2 && sd->special_state.no_gemstone != 2)
  2790. sd->special_state.no_gemstone = 1;
  2791. break;
  2792. case SP_INTRAVISION: // Maya Purple Card effect allowing to see Hiding/Cloaking people [DracoRPG]
  2793. if(sd->state.lr_flag != 2) {
  2794. sd->special_state.intravision = 1;
  2795. clif_status_load(&sd->bl, EFST_CLAIRVOYANCE, 1);
  2796. }
  2797. break;
  2798. case SP_NO_KNOCKBACK:
  2799. if(sd->state.lr_flag != 2)
  2800. sd->special_state.no_knockback = 1;
  2801. break;
  2802. case SP_SPLASH_RANGE:
  2803. if(sd->bonus.splash_range < val)
  2804. sd->bonus.splash_range = val;
  2805. break;
  2806. case SP_SPLASH_ADD_RANGE:
  2807. sd->bonus.splash_add_range += val;
  2808. break;
  2809. case SP_SHORT_WEAPON_DAMAGE_RETURN:
  2810. if(sd->state.lr_flag != 2)
  2811. sd->bonus.short_weapon_damage_return += val;
  2812. break;
  2813. case SP_LONG_WEAPON_DAMAGE_RETURN:
  2814. if(sd->state.lr_flag != 2)
  2815. sd->bonus.long_weapon_damage_return += val;
  2816. break;
  2817. case SP_MAGIC_DAMAGE_RETURN: //AppleGirl Was Here
  2818. if(sd->state.lr_flag != 2)
  2819. sd->bonus.magic_damage_return += val;
  2820. break;
  2821. case SP_ALL_STATS: // [Valaris]
  2822. if(sd->state.lr_flag!=2) {
  2823. sd->param_bonus[SP_STR-SP_STR]+=val;
  2824. sd->param_bonus[SP_AGI-SP_STR]+=val;
  2825. sd->param_bonus[SP_VIT-SP_STR]+=val;
  2826. sd->param_bonus[SP_INT-SP_STR]+=val;
  2827. sd->param_bonus[SP_DEX-SP_STR]+=val;
  2828. sd->param_bonus[SP_LUK-SP_STR]+=val;
  2829. }
  2830. break;
  2831. case SP_AGI_VIT: // [Valaris]
  2832. if(sd->state.lr_flag!=2) {
  2833. sd->param_bonus[SP_AGI-SP_STR]+=val;
  2834. sd->param_bonus[SP_VIT-SP_STR]+=val;
  2835. }
  2836. break;
  2837. case SP_AGI_DEX_STR: // [Valaris]
  2838. if(sd->state.lr_flag!=2) {
  2839. sd->param_bonus[SP_AGI-SP_STR]+=val;
  2840. sd->param_bonus[SP_DEX-SP_STR]+=val;
  2841. sd->param_bonus[SP_STR-SP_STR]+=val;
  2842. }
  2843. break;
  2844. case SP_PERFECT_HIDE: // [Valaris]
  2845. if(sd->state.lr_flag!=2)
  2846. sd->special_state.perfect_hiding=1;
  2847. break;
  2848. case SP_UNBREAKABLE:
  2849. if(sd->state.lr_flag!=2)
  2850. sd->bonus.unbreakable += val;
  2851. break;
  2852. case SP_UNBREAKABLE_WEAPON:
  2853. if(sd->state.lr_flag != 2)
  2854. sd->bonus.unbreakable_equip |= EQP_WEAPON;
  2855. break;
  2856. case SP_UNBREAKABLE_ARMOR:
  2857. if(sd->state.lr_flag != 2)
  2858. sd->bonus.unbreakable_equip |= EQP_ARMOR;
  2859. break;
  2860. case SP_UNBREAKABLE_HELM:
  2861. if(sd->state.lr_flag != 2)
  2862. sd->bonus.unbreakable_equip |= EQP_HELM;
  2863. break;
  2864. case SP_UNBREAKABLE_SHIELD:
  2865. if(sd->state.lr_flag != 2)
  2866. sd->bonus.unbreakable_equip |= EQP_SHIELD;
  2867. break;
  2868. case SP_UNBREAKABLE_GARMENT:
  2869. if(sd->state.lr_flag != 2)
  2870. sd->bonus.unbreakable_equip |= EQP_GARMENT;
  2871. break;
  2872. case SP_UNBREAKABLE_SHOES:
  2873. if(sd->state.lr_flag != 2)
  2874. sd->bonus.unbreakable_equip |= EQP_SHOES;
  2875. break;
  2876. case SP_CLASSCHANGE: // [Valaris]
  2877. if(sd->state.lr_flag !=2)
  2878. sd->bonus.classchange=val;
  2879. break;
  2880. case SP_LONG_ATK_RATE:
  2881. if(sd->state.lr_flag != 2) //[Lupus] it should stack, too. As any other cards rate bonuses
  2882. sd->bonus.long_attack_atk_rate+=val;
  2883. break;
  2884. case SP_BREAK_WEAPON_RATE:
  2885. if(sd->state.lr_flag != 2)
  2886. sd->bonus.break_weapon_rate+=val;
  2887. break;
  2888. case SP_BREAK_ARMOR_RATE:
  2889. if(sd->state.lr_flag != 2)
  2890. sd->bonus.break_armor_rate+=val;
  2891. break;
  2892. case SP_ADD_STEAL_RATE:
  2893. if(sd->state.lr_flag != 2)
  2894. sd->bonus.add_steal_rate+=val;
  2895. break;
  2896. case SP_DELAYRATE:
  2897. if(sd->state.lr_flag != 2)
  2898. sd->delayrate+=val;
  2899. break;
  2900. case SP_CRIT_ATK_RATE:
  2901. if(sd->state.lr_flag != 2)
  2902. sd->bonus.crit_atk_rate += val;
  2903. break;
  2904. case SP_NO_REGEN:
  2905. if(sd->state.lr_flag != 2)
  2906. sd->regen.state.block|=val;
  2907. break;
  2908. case SP_UNSTRIPABLE_WEAPON:
  2909. if(sd->state.lr_flag != 2)
  2910. sd->bonus.unstripable_equip |= EQP_WEAPON;
  2911. break;
  2912. case SP_UNSTRIPABLE:
  2913. case SP_UNSTRIPABLE_ARMOR:
  2914. if(sd->state.lr_flag != 2)
  2915. sd->bonus.unstripable_equip |= EQP_ARMOR;
  2916. break;
  2917. case SP_UNSTRIPABLE_HELM:
  2918. if(sd->state.lr_flag != 2)
  2919. sd->bonus.unstripable_equip |= EQP_HELM;
  2920. break;
  2921. case SP_UNSTRIPABLE_SHIELD:
  2922. if(sd->state.lr_flag != 2)
  2923. sd->bonus.unstripable_equip |= EQP_SHIELD;
  2924. break;
  2925. case SP_HP_DRAIN_VALUE: // bonus bHPDrainValue,n;
  2926. if(!sd->state.lr_flag) {
  2927. sd->right_weapon.hp_drain_class[CLASS_NORMAL] += val;
  2928. sd->right_weapon.hp_drain_class[CLASS_BOSS] += val;
  2929. } else if(sd->state.lr_flag == 1) {
  2930. sd->left_weapon.hp_drain_class[CLASS_NORMAL] += val;
  2931. sd->left_weapon.hp_drain_class[CLASS_BOSS] += val;
  2932. }
  2933. break;
  2934. case SP_SP_DRAIN_VALUE: // bonus bSPDrainValue,n;
  2935. if(!sd->state.lr_flag) {
  2936. sd->right_weapon.sp_drain_class[CLASS_NORMAL] += val;
  2937. sd->right_weapon.sp_drain_class[CLASS_BOSS] += val;
  2938. } else if(sd->state.lr_flag == 1) {
  2939. sd->left_weapon.sp_drain_class[CLASS_NORMAL] += val;
  2940. sd->left_weapon.sp_drain_class[CLASS_BOSS] += val;
  2941. }
  2942. break;
  2943. case SP_SP_GAIN_VALUE:
  2944. if(!sd->state.lr_flag)
  2945. sd->bonus.sp_gain_value += val;
  2946. break;
  2947. case SP_HP_GAIN_VALUE:
  2948. if(!sd->state.lr_flag)
  2949. sd->bonus.hp_gain_value += val;
  2950. break;
  2951. case SP_MAGIC_SP_GAIN_VALUE:
  2952. if(!sd->state.lr_flag)
  2953. sd->bonus.magic_sp_gain_value += val;
  2954. break;
  2955. case SP_MAGIC_HP_GAIN_VALUE:
  2956. if(!sd->state.lr_flag)
  2957. sd->bonus.magic_hp_gain_value += val;
  2958. break;
  2959. case SP_ADD_HEAL_RATE:
  2960. if(sd->state.lr_flag != 2)
  2961. sd->bonus.add_heal_rate += val;
  2962. break;
  2963. case SP_ADD_HEAL2_RATE:
  2964. if(sd->state.lr_flag != 2)
  2965. sd->bonus.add_heal2_rate += val;
  2966. break;
  2967. case SP_ADD_ITEM_HEAL_RATE:
  2968. if(sd->state.lr_flag != 2)
  2969. sd->bonus.itemhealrate2 += val;
  2970. break;
  2971. case SP_EMATK:
  2972. if(sd->state.lr_flag != 2)
  2973. sd->bonus.ematk += val;
  2974. break;
  2975. #ifdef RENEWAL_CAST
  2976. case SP_FIXCASTRATE:
  2977. if(sd->state.lr_flag != 2)
  2978. sd->bonus.fixcastrate = min(sd->bonus.fixcastrate,val);
  2979. break;
  2980. case SP_ADD_FIXEDCAST:
  2981. if(sd->state.lr_flag != 2)
  2982. sd->bonus.add_fixcast += val;
  2983. break;
  2984. case SP_CASTRATE:
  2985. case SP_VARCASTRATE:
  2986. if(sd->state.lr_flag != 2)
  2987. sd->bonus.varcastrate -= val;
  2988. break;
  2989. case SP_ADD_VARIABLECAST:
  2990. if(sd->state.lr_flag != 2)
  2991. sd->bonus.add_varcast += val;
  2992. break;
  2993. #else
  2994. case SP_ADD_FIXEDCAST:
  2995. case SP_FIXCASTRATE:
  2996. case SP_ADD_VARIABLECAST:
  2997. //ShowWarning("pc_bonus: non-RENEWAL_CAST doesn't support this bonus %d.\n", type);
  2998. break;
  2999. case SP_VARCASTRATE:
  3000. case SP_CASTRATE:
  3001. if(sd->state.lr_flag != 2)
  3002. sd->castrate += val;
  3003. break;
  3004. #endif
  3005. case SP_ADDMAXWEIGHT:
  3006. if (sd->state.lr_flag != 2)
  3007. sd->add_max_weight += val;
  3008. break;
  3009. case SP_ABSORB_DMG_MAXHP: // bonus bAbsorbDmgMaxHP,n;
  3010. sd->bonus.absorb_dmg_maxhp = max(sd->bonus.absorb_dmg_maxhp, val);
  3011. break;
  3012. case SP_CRITICAL_RANGEATK: // bonus bCriticalLong,n;
  3013. if (sd->state.lr_flag != 2)
  3014. sd->bonus.critical_rangeatk += val*10;
  3015. else
  3016. sd->bonus.arrow_cri += val*10;
  3017. break;
  3018. case SP_WEAPON_ATK_RATE:
  3019. if (sd->state.lr_flag != 2)
  3020. sd->bonus.weapon_atk_rate += val;
  3021. break;
  3022. case SP_WEAPON_MATK_RATE:
  3023. if (sd->state.lr_flag != 2)
  3024. sd->bonus.weapon_matk_rate += val;
  3025. break;
  3026. case SP_NO_MADO_FUEL:
  3027. if (sd->state.lr_flag != 2)
  3028. sd->special_state.no_mado_fuel = 1;
  3029. break;
  3030. case SP_NO_WALK_DELAY:
  3031. if (sd->state.lr_flag != 2)
  3032. sd->special_state.no_walk_delay = 1;
  3033. break;
  3034. default:
  3035. if (running_npc_stat_calc_event) {
  3036. ShowWarning("pc_bonus: unknown bonus type %d %d in OnPCStatCalcEvent!\n", type, val);
  3037. }
  3038. else if (current_equip_combo_pos > 0) {
  3039. 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);
  3040. }
  3041. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  3042. 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);
  3043. }
  3044. else {
  3045. ShowWarning("pc_bonus: unknown bonus type %d %d in unknown usage. Report this!\n", type, val);
  3046. }
  3047. break;
  3048. }
  3049. }
  3050. /*==========================================
  3051. * Player bonus (type) with args type2 and val, called trough bonus2 (npc)
  3052. * format: bonus2 bBonusName,type2,val;
  3053. * @param sd
  3054. * @param type Bonus type used by bBonusName
  3055. * @param type2
  3056. * @param val Value that usually for rate or fixed value
  3057. *------------------------------------------*/
  3058. void pc_bonus2(struct map_session_data *sd,int type,int type2,int val)
  3059. {
  3060. nullpo_retv(sd);
  3061. switch(type){
  3062. case SP_ADDELE: // bonus2 bAddEle,e,x;
  3063. PC_BONUS_CHK_ELEMENT(type2,SP_ADDELE);
  3064. if(!sd->state.lr_flag || sd->state.lr_flag == 3)
  3065. sd->right_weapon.addele[type2]+=val;
  3066. else if(sd->state.lr_flag == 1)
  3067. sd->left_weapon.addele[type2]+=val;
  3068. else if(sd->state.lr_flag == 2)
  3069. sd->arrow_addele[type2]+=val;
  3070. break;
  3071. case SP_ADDRACE: // bonus2 bAddRace,r,x;
  3072. PC_BONUS_CHK_RACE(type2,SP_ADDRACE);
  3073. if(!sd->state.lr_flag || sd->state.lr_flag == 3)
  3074. sd->right_weapon.addrace[type2]+=val;
  3075. else if(sd->state.lr_flag == 1)
  3076. sd->left_weapon.addrace[type2]+=val;
  3077. else if(sd->state.lr_flag == 2)
  3078. sd->arrow_addrace[type2]+=val;
  3079. break;
  3080. case SP_ADDCLASS: // bonus2 bAddClass,c,x;
  3081. PC_BONUS_CHK_CLASS(type2,SP_ADDCLASS);
  3082. if(!sd->state.lr_flag || sd->state.lr_flag == 3)
  3083. sd->right_weapon.addclass[type2]+=val;
  3084. else if(sd->state.lr_flag == 1)
  3085. sd->left_weapon.addclass[type2]+=val;
  3086. else if(sd->state.lr_flag == 2)
  3087. sd->arrow_addclass[type2]+=val;
  3088. break;
  3089. case SP_ADDSIZE: // bonus2 bAddSize,s,x;
  3090. PC_BONUS_CHK_SIZE(type2,SP_ADDSIZE);
  3091. if(!sd->state.lr_flag || sd->state.lr_flag == 3)
  3092. sd->right_weapon.addsize[type2]+=val;
  3093. else if(sd->state.lr_flag == 1)
  3094. sd->left_weapon.addsize[type2]+=val;
  3095. else if(sd->state.lr_flag == 2)
  3096. sd->arrow_addsize[type2]+=val;
  3097. break;
  3098. case SP_SUBELE: // bonus2 bSubEle,e,x;
  3099. PC_BONUS_CHK_ELEMENT(type2,SP_SUBELE);
  3100. if(sd->state.lr_flag != 2)
  3101. sd->subele_script[type2] += val;
  3102. break;
  3103. case SP_SUBRACE: // bonus2 bSubRace,r,x;
  3104. PC_BONUS_CHK_RACE(type2,SP_SUBRACE);
  3105. if(sd->state.lr_flag != 2)
  3106. sd->subrace[type2]+=val;
  3107. break;
  3108. case SP_SUBCLASS: // bonus2 bSubClass,c,x;
  3109. PC_BONUS_CHK_CLASS(type2,SP_SUBCLASS);
  3110. if(sd->state.lr_flag != 2)
  3111. sd->subclass[type2]+=val;
  3112. break;
  3113. case SP_ADDEFF: // bonus2 bAddEff,eff,n;
  3114. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  3115. pc_bonus_addeff(sd->addeff, (sc_type)type2, sd->state.lr_flag != 2 ? val : 0, sd->state.lr_flag == 2 ? val : 0, 0, 0);
  3116. break;
  3117. case SP_ADDEFF2: // bonus2 bAddEff2,eff,n;
  3118. PC_BONUS_CHK_SC(type2,SP_ADDEFF2);
  3119. 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);
  3120. break;
  3121. case SP_RESEFF: // bonus2 bResEff,eff,n;
  3122. if(sd->state.lr_flag == 2)
  3123. break;
  3124. if (sd->reseff.size() == MAX_PC_BONUS) {
  3125. ShowWarning("pc_bonus2: SP_RESEFF: Reached max (%d) number of resistance bonuses per character!\n", MAX_PC_BONUS);
  3126. break;
  3127. }
  3128. pc_bonus_itembonus(sd->reseff, type2, val, true);
  3129. break;
  3130. case SP_MAGIC_ADDELE: // bonus2 bMagicAddEle,e,x;
  3131. PC_BONUS_CHK_ELEMENT(type2,SP_MAGIC_ADDELE);
  3132. if(sd->state.lr_flag != 2)
  3133. sd->magic_addele_script[type2] += val;
  3134. break;
  3135. case SP_MAGIC_ADDRACE: // bonus2 bMagicAddRace,r,x;
  3136. PC_BONUS_CHK_RACE(type2,SP_MAGIC_ADDRACE);
  3137. if(sd->state.lr_flag != 2)
  3138. sd->magic_addrace[type2]+=val;
  3139. break;
  3140. case SP_MAGIC_ADDCLASS: // bonus2 bMagicAddClass,c,x;
  3141. PC_BONUS_CHK_CLASS(type2,SP_MAGIC_ADDCLASS);
  3142. if(sd->state.lr_flag != 2)
  3143. sd->magic_addclass[type2]+=val;
  3144. break;
  3145. case SP_MAGIC_ADDSIZE: // bonus2 bMagicAddSize,s,x;
  3146. PC_BONUS_CHK_SIZE(type2,SP_MAGIC_ADDSIZE);
  3147. if(sd->state.lr_flag != 2)
  3148. sd->magic_addsize[type2]+=val;
  3149. break;
  3150. case SP_MAGIC_ATK_ELE: // bonus2 bMagicAtkEle,e,x;
  3151. PC_BONUS_CHK_ELEMENT(type2,SP_MAGIC_ATK_ELE);
  3152. if(sd->state.lr_flag != 2)
  3153. sd->magic_atk_ele[type2]+=val;
  3154. break;
  3155. case SP_ADD_DAMAGE_CLASS: { // bonus2 bAddDamageClass,mid,x;
  3156. struct weapon_data *wd = (sd->state.lr_flag ? &sd->left_weapon : &sd->right_weapon);
  3157. if (wd->add_dmg.size() == MAX_PC_BONUS) {
  3158. ShowWarning("pc_bonus2: SP_ADD_DAMAGE_CLASS: Reached max (%d) number of add class damage bonuses per character!\n", MAX_PC_BONUS);
  3159. break;
  3160. }
  3161. pc_bonus_itembonus(wd->add_dmg, type2, val, false);
  3162. }
  3163. break;
  3164. case SP_ADD_MAGIC_DAMAGE_CLASS: // bonus2 bAddMagicDamageClass,mid,x;
  3165. if(sd->state.lr_flag == 2)
  3166. break;
  3167. if (sd->add_mdmg.size() == MAX_PC_BONUS) {
  3168. ShowWarning("pc_bonus2: SP_ADD_MAGIC_DAMAGE_CLASS: Reached max (%d) number of add class magic dmg bonuses per character!\n", MAX_PC_BONUS);
  3169. break;
  3170. }
  3171. pc_bonus_itembonus(sd->add_mdmg, type2, val, false);
  3172. break;
  3173. case SP_ADD_DEF_MONSTER: // bonus2 bAddDefMonster,mid,x;
  3174. if(sd->state.lr_flag == 2)
  3175. break;
  3176. if (sd->add_def.size() == MAX_PC_BONUS) {
  3177. ShowWarning("pc_bonus2: SP_ADD_DEF_MONSTER: Reached max (%d) number of add class def bonuses per character!\n", MAX_PC_BONUS);
  3178. break;
  3179. }
  3180. pc_bonus_itembonus(sd->add_def, type2, val, false);
  3181. break;
  3182. case SP_ADD_MDEF_MONSTER: // bonus2 bAddMDefMonster,mid,x;
  3183. if(sd->state.lr_flag == 2)
  3184. break;
  3185. if (sd->add_mdef.size() == MAX_PC_BONUS) {
  3186. ShowWarning("pc_bonus2: SP_ADD_MDEF_MONSTER: Reached max (%d) number of add class mdef bonuses per character!\n", MAX_PC_BONUS);
  3187. break;
  3188. }
  3189. pc_bonus_itembonus(sd->add_mdef, type2, val, false);
  3190. break;
  3191. case SP_HP_DRAIN_RATE: // bonus2 bHPDrainRate,x,n;
  3192. if(!sd->state.lr_flag) {
  3193. sd->right_weapon.hp_drain_rate.rate += type2;
  3194. sd->right_weapon.hp_drain_rate.per += val;
  3195. }
  3196. else if(sd->state.lr_flag == 1) {
  3197. sd->left_weapon.hp_drain_rate.rate += type2;
  3198. sd->left_weapon.hp_drain_rate.per += val;
  3199. }
  3200. break;
  3201. case SP_SP_DRAIN_RATE: // bonus2 bSPDrainRate,x,n;
  3202. if(!sd->state.lr_flag) {
  3203. sd->right_weapon.sp_drain_rate.rate += type2;
  3204. sd->right_weapon.sp_drain_rate.per += val;
  3205. }
  3206. else if(sd->state.lr_flag == 1) {
  3207. sd->left_weapon.sp_drain_rate.rate += type2;
  3208. sd->left_weapon.sp_drain_rate.per += val;
  3209. }
  3210. break;
  3211. case SP_SP_VANISH_RATE: // bonus2 bSPVanishRate,x,n;
  3212. if(sd->state.lr_flag != 2) {
  3213. pc_bonus_addvanish(sd->sp_vanish, type2, val, BF_NORMAL);
  3214. }
  3215. break;
  3216. case SP_HP_VANISH_RATE: // bonus2 bHPVanishRate,x,n;
  3217. if(sd->state.lr_flag != 2) {
  3218. pc_bonus_addvanish(sd->hp_vanish, type2, val, BF_NORMAL);
  3219. }
  3220. break;
  3221. case SP_GET_ZENY_NUM: // bonus2 bGetZenyNum,x,n;
  3222. if(sd->state.lr_flag != 2 && sd->bonus.get_zeny_rate < val) {
  3223. sd->bonus.get_zeny_rate = val;
  3224. sd->bonus.get_zeny_num = type2;
  3225. }
  3226. break;
  3227. case SP_ADD_GET_ZENY_NUM: // bonus2 bAddGetZenyNum,x,n;
  3228. if(sd->state.lr_flag != 2) {
  3229. sd->bonus.get_zeny_rate += val;
  3230. sd->bonus.get_zeny_num += type2;
  3231. }
  3232. break;
  3233. case SP_WEAPON_COMA_ELE: // bonus2 bWeaponComaEle,e,n;
  3234. PC_BONUS_CHK_ELEMENT(type2,SP_WEAPON_COMA_ELE);
  3235. if(sd->state.lr_flag == 2)
  3236. break;
  3237. sd->weapon_coma_ele[type2] += val;
  3238. sd->special_state.bonus_coma = 1;
  3239. break;
  3240. case SP_WEAPON_COMA_RACE: // bonus2 bWeaponComaRace,r,n;
  3241. PC_BONUS_CHK_RACE(type2,SP_WEAPON_COMA_RACE);
  3242. if(sd->state.lr_flag == 2)
  3243. break;
  3244. sd->weapon_coma_race[type2] += val;
  3245. sd->special_state.bonus_coma = 1;
  3246. break;
  3247. case SP_WEAPON_COMA_CLASS: // bonus2 bWeaponComaClass,c,n;
  3248. PC_BONUS_CHK_CLASS(type2,SP_WEAPON_COMA_CLASS);
  3249. if(sd->state.lr_flag == 2)
  3250. break;
  3251. sd->weapon_coma_class[type2] += val;
  3252. sd->special_state.bonus_coma = 1;
  3253. break;
  3254. case SP_WEAPON_ATK: // bonus2 bWeaponAtk,w,n;
  3255. if(sd->state.lr_flag != 2)
  3256. sd->weapon_atk[type2]+=val;
  3257. break;
  3258. case SP_WEAPON_DAMAGE_RATE: // bonus2 bWeaponDamageRate,w,n;
  3259. if(sd->state.lr_flag != 2)
  3260. sd->weapon_damage_rate[type2]+=val;
  3261. break;
  3262. case SP_CRITICAL_ADDRACE: // bonus2 bCriticalAddRace,r,n;
  3263. PC_BONUS_CHK_RACE(type2,SP_CRITICAL_ADDRACE);
  3264. if(sd->state.lr_flag != 2)
  3265. sd->critaddrace[type2] += val*10;
  3266. break;
  3267. case SP_ADDEFF_WHENHIT: // bonus2 bAddEffWhenHit,eff,n;
  3268. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  3269. if(sd->state.lr_flag != 2)
  3270. pc_bonus_addeff(sd->addeff_atked, (sc_type)type2, val, 0, 0, 0);
  3271. break;
  3272. case SP_SKILL_ATK: // bonus2 bSkillAtk,sk,n;
  3273. if(sd->state.lr_flag == 2)
  3274. break;
  3275. if (sd->skillatk.size() == MAX_PC_BONUS) {
  3276. 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);
  3277. break;
  3278. }
  3279. pc_bonus_itembonus(sd->skillatk, type2, val, false);
  3280. break;
  3281. case SP_SKILL_HEAL: // bonus2 bSkillHeal,sk,n;
  3282. if(sd->state.lr_flag == 2)
  3283. break;
  3284. if (sd->skillheal.size() == MAX_PC_BONUS) { // Better mention this so the array length can be updated. [Skotlex]
  3285. 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);
  3286. break;
  3287. }
  3288. pc_bonus_itembonus(sd->skillheal, type2, val, false);
  3289. break;
  3290. case SP_SKILL_HEAL2: // bonus2 bSkillHeal2,sk,n;
  3291. if(sd->state.lr_flag == 2)
  3292. break;
  3293. if (sd->skillheal2.size() == MAX_PC_BONUS) { // Better mention this so the array length can be updated. [Skotlex]
  3294. 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);
  3295. break;
  3296. }
  3297. pc_bonus_itembonus(sd->skillheal2, type2, val, false);
  3298. break;
  3299. case SP_ADD_SKILL_BLOW: // bonus2 bAddSkillBlow,sk,n;
  3300. if(sd->state.lr_flag == 2)
  3301. break;
  3302. if (sd->skillblown.size() == MAX_PC_BONUS) { //Better mention this so the array length can be updated. [Skotlex]
  3303. 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);
  3304. break;
  3305. }
  3306. pc_bonus_itembonus(sd->skillblown, type2, val, false);
  3307. break;
  3308. case SP_HP_LOSS_RATE: // bonus2 bHPLossRate,n,t;
  3309. if(sd->state.lr_flag != 2) {
  3310. sd->hp_loss.value = type2;
  3311. sd->hp_loss.rate = val;
  3312. }
  3313. break;
  3314. case SP_HP_REGEN_RATE: // bonus2 bHPRegenRate,n,t;
  3315. if(sd->state.lr_flag != 2) {
  3316. sd->hp_regen.value = type2;
  3317. sd->hp_regen.rate = val;
  3318. }
  3319. break;
  3320. case SP_REGEN_PERCENT_HP: // bonus2 bRegenPercentHP,n,t;
  3321. if (sd->state.lr_flag != 2) {
  3322. sd->percent_hp_regen.value = type2;
  3323. sd->percent_hp_regen.rate = val;
  3324. }
  3325. break;
  3326. case SP_REGEN_PERCENT_SP: // bonus2 bRegenPercentSP,n,t;
  3327. if (sd->state.lr_flag != 2) {
  3328. sd->percent_sp_regen.value = type2;
  3329. sd->percent_sp_regen.rate = val;
  3330. }
  3331. break;
  3332. case SP_ADDRACE2: // bonus2 bAddRace2,mr,x;
  3333. PC_BONUS_CHK_RACE2(type2,SP_ADDRACE2);
  3334. if(sd->state.lr_flag != 2)
  3335. sd->right_weapon.addrace2[type2] += val;
  3336. else
  3337. sd->left_weapon.addrace2[type2] += val;
  3338. break;
  3339. case SP_SUBSIZE: // bonus2 bSubSize,s,x;
  3340. PC_BONUS_CHK_SIZE(type2,SP_SUBSIZE);
  3341. if(sd->state.lr_flag != 2)
  3342. sd->subsize[type2]+=val;
  3343. break;
  3344. case SP_SUBRACE2: // bonus2 bSubRace2,mr,x;
  3345. PC_BONUS_CHK_RACE2(type2,SP_SUBRACE2);
  3346. if(sd->state.lr_flag != 2)
  3347. sd->subrace2[type2]+=val;
  3348. break;
  3349. case SP_ADD_ITEM_HEAL_RATE: // bonus2 bAddItemHealRate,iid,n;
  3350. if(sd->state.lr_flag == 2)
  3351. break;
  3352. if (!itemdb_exists(type2)) {
  3353. ShowWarning("pc_bonus2: SP_ADD_ITEM_HEAL_RATE Invalid item with id %d\n", type2);
  3354. break;
  3355. }
  3356. if (sd->itemhealrate.size() == MAX_PC_BONUS) {
  3357. ShowWarning("pc_bonus2: SP_ADD_ITEM_HEAL_RATE: Reached max (%d) number of item heal bonuses per character!\n", MAX_PC_BONUS);
  3358. break;
  3359. }
  3360. pc_bonus_itembonus(sd->itemhealrate, type2, val, false);
  3361. break;
  3362. case SP_ADD_ITEMGROUP_HEAL_RATE: // bonus2 bAddItemGroupHealRate,ig,n;
  3363. if (sd->state.lr_flag == 2)
  3364. break;
  3365. if (!type2 || !itemdb_group_exists(type2)) {
  3366. ShowWarning("pc_bonus2: SP_ADD_ITEMGROUP_HEAL_RATE: Invalid item group with id %d\n", type2);
  3367. break;
  3368. }
  3369. if (sd->itemhealrate.size() == MAX_PC_BONUS) {
  3370. ShowWarning("pc_bonus2: SP_ADD_ITEMGROUP_HEAL_RATE: Reached max (%d) number of item heal bonuses per character!\n", MAX_PC_BONUS);
  3371. break;
  3372. }
  3373. pc_bonus_itembonus(sd->itemgrouphealrate, type2, val, false);
  3374. break;
  3375. case SP_EXP_ADDRACE: // bonus2 bExpAddRace,r,x;
  3376. PC_BONUS_CHK_RACE(type2,SP_EXP_ADDRACE);
  3377. if(sd->state.lr_flag != 2)
  3378. sd->expaddrace[type2]+=val;
  3379. break;
  3380. case SP_EXP_ADDCLASS: // bonus2 bExpAddClass,c,x;
  3381. PC_BONUS_CHK_CLASS(type2,SP_EXP_ADDCLASS);
  3382. if(sd->state.lr_flag != 2)
  3383. sd->expaddclass[type2]+=val;
  3384. break;
  3385. case SP_SP_GAIN_RACE: // bonus2 bSPGainRace,r,n;
  3386. PC_BONUS_CHK_RACE(type2,SP_SP_GAIN_RACE);
  3387. if(sd->state.lr_flag != 2)
  3388. sd->sp_gain_race[type2]+=val;
  3389. break;
  3390. case SP_ADD_MONSTER_DROP_ITEM: // bonus2 bAddMonsterDropItem,iid,n;
  3391. if (sd->state.lr_flag != 2)
  3392. pc_bonus_item_drop(sd->add_drop, type2, 0, CLASS_ALL, RC_NONE_, val);
  3393. break;
  3394. case SP_ADD_MONSTER_DROP_ITEMGROUP: // bonus2 bAddMonsterDropItemGroup,ig,n;
  3395. if (sd->state.lr_flag != 2)
  3396. pc_bonus_item_drop(sd->add_drop, 0, type2, CLASS_ALL, RC_NONE_, val);
  3397. break;
  3398. case SP_SP_LOSS_RATE: // bonus2 bSPLossRate,n,t;
  3399. if(sd->state.lr_flag != 2) {
  3400. sd->sp_loss.value = type2;
  3401. sd->sp_loss.rate = val;
  3402. }
  3403. break;
  3404. case SP_SP_REGEN_RATE: // bonus2 bSPRegenRate,n,t;
  3405. if(sd->state.lr_flag != 2) {
  3406. sd->sp_regen.value = type2;
  3407. sd->sp_regen.rate = val;
  3408. }
  3409. break;
  3410. case SP_HP_DRAIN_VALUE_RACE: // bonus2 bHPDrainValueRace,r,n;
  3411. PC_BONUS_CHK_RACE(type2,SP_HP_DRAIN_VALUE_RACE);
  3412. if(!sd->state.lr_flag) {
  3413. sd->right_weapon.hp_drain_race[type2] += val;
  3414. }
  3415. else if(sd->state.lr_flag == 1) {
  3416. sd->left_weapon.hp_drain_race[type2] += val;
  3417. }
  3418. break;
  3419. case SP_SP_DRAIN_VALUE_RACE: // bonus2 bSPDrainValueRace,r,n;
  3420. PC_BONUS_CHK_RACE(type2,SP_SP_DRAIN_VALUE_RACE);
  3421. if(!sd->state.lr_flag) {
  3422. sd->right_weapon.sp_drain_race[type2] += val;
  3423. }
  3424. else if(sd->state.lr_flag == 1) {
  3425. sd->left_weapon.sp_drain_race[type2] += val;
  3426. }
  3427. break;
  3428. case SP_HP_DRAIN_VALUE_CLASS: // bonus2 bHPDrainValueClass,c,n;
  3429. PC_BONUS_CHK_CLASS(type2,SP_HP_DRAIN_VALUE_CLASS);
  3430. if(!sd->state.lr_flag) {
  3431. sd->right_weapon.hp_drain_class[type2] += val;
  3432. }
  3433. else if(sd->state.lr_flag == 1) {
  3434. sd->left_weapon.hp_drain_class[type2] += val;
  3435. }
  3436. break;
  3437. case SP_SP_DRAIN_VALUE_CLASS: // bonus2 bSPDrainValueClass,c,n;
  3438. PC_BONUS_CHK_CLASS(type2,SP_SP_DRAIN_VALUE_CLASS);
  3439. if(!sd->state.lr_flag) {
  3440. sd->right_weapon.sp_drain_class[type2] += val;
  3441. }
  3442. else if(sd->state.lr_flag == 1) {
  3443. sd->left_weapon.sp_drain_class[type2] += val;
  3444. }
  3445. break;
  3446. case SP_IGNORE_MDEF_RACE_RATE: // bonus2 bIgnoreMdefRaceRate,r,n;
  3447. PC_BONUS_CHK_RACE(type2,SP_IGNORE_MDEF_RACE_RATE);
  3448. if(sd->state.lr_flag != 2)
  3449. sd->ignore_mdef_by_race[type2] += val;
  3450. break;
  3451. case SP_IGNORE_MDEF_CLASS_RATE: // bonus2 bIgnoreMdefClassRate,c,n;
  3452. PC_BONUS_CHK_CLASS(type2,SP_IGNORE_MDEF_CLASS_RATE);
  3453. if(sd->state.lr_flag != 2)
  3454. sd->ignore_mdef_by_class[type2] += val;
  3455. break;
  3456. case SP_IGNORE_DEF_RACE_RATE: // bonus2 bIgnoreDefRaceRate,r,n;
  3457. PC_BONUS_CHK_RACE(type2,SP_IGNORE_DEF_RACE_RATE);
  3458. if(sd->state.lr_flag != 2)
  3459. sd->ignore_def_by_race[type2] += val;
  3460. break;
  3461. case SP_IGNORE_DEF_CLASS_RATE: // bonus2 bIgnoreDefClassRate,r,n;
  3462. PC_BONUS_CHK_CLASS(type2, SP_IGNORE_DEF_CLASS_RATE);
  3463. if (sd->state.lr_flag != 2)
  3464. sd->ignore_def_by_class[type2] += val;
  3465. break;
  3466. case SP_SKILL_USE_SP_RATE: // bonus2 bSkillUseSPrate,sk,n;
  3467. if(sd->state.lr_flag == 2)
  3468. break;
  3469. if (sd->skillusesprate.size() == MAX_PC_BONUS) {
  3470. 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);
  3471. break;
  3472. }
  3473. pc_bonus_itembonus(sd->skillusesprate, type2, val, true);
  3474. break;
  3475. case SP_SKILL_DELAY:
  3476. if(sd->state.lr_flag == 2)
  3477. break;
  3478. if (sd->skilldelay.size() == MAX_PC_BONUS) {
  3479. 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);
  3480. break;
  3481. }
  3482. pc_bonus_itembonus(sd->skilldelay, type2, val, false);
  3483. break;
  3484. case SP_SKILL_COOLDOWN: // bonus2 bSkillCooldown,sk,t;
  3485. if(sd->state.lr_flag == 2)
  3486. break;
  3487. if (sd->skillcooldown.size() == MAX_PC_BONUS) {
  3488. 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);
  3489. break;
  3490. }
  3491. pc_bonus_itembonus(sd->skillcooldown, type2, val, false);
  3492. break;
  3493. #ifdef RENEWAL_CAST
  3494. case SP_SKILL_FIXEDCAST: // bonus2 bSkillFixedCast,sk,t;
  3495. if(sd->state.lr_flag == 2)
  3496. break;
  3497. if (sd->skillfixcast.size() == MAX_PC_BONUS) {
  3498. 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);
  3499. break;
  3500. }
  3501. pc_bonus_itembonus(sd->skillfixcast, type2, val, false);
  3502. break;
  3503. case SP_SKILL_VARIABLECAST: // bonus2 bSkillVariableCast,sk,t;
  3504. if(sd->state.lr_flag == 2)
  3505. break;
  3506. if (sd->skillvarcast.size() == MAX_PC_BONUS) {
  3507. 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);
  3508. break;
  3509. }
  3510. pc_bonus_itembonus(sd->skillvarcast, type2, val, false);
  3511. break;
  3512. case SP_CASTRATE: // bonus2 bCastrate,sk,n;
  3513. case SP_VARCASTRATE: // bonus2 bVariableCastrate,sk,n;
  3514. if(sd->state.lr_flag == 2)
  3515. break;
  3516. if (sd->skillcastrate.size() == MAX_PC_BONUS) {
  3517. ShowWarning("pc_bonus2: SP_VARCASTRATE: Reached max (%d) number of skills per character, bonus skill %d (%d%%) lost.\n", MAX_PC_BONUS, type2, val);
  3518. break;
  3519. }
  3520. pc_bonus_itembonus(sd->skillcastrate, type2, -val, true); // Send inversed value here
  3521. break;
  3522. case SP_FIXCASTRATE: // bonus2 bFixedCastrate,sk,n;
  3523. if(sd->state.lr_flag == 2)
  3524. break;
  3525. if (sd->skillfixcastrate.size() == MAX_PC_BONUS) {
  3526. ShowWarning("pc_bonus2: SP_FIXCASTRATE: Reached max (%d) number of skills per character, bonus skill %d (%d%%) lost.\n", MAX_PC_BONUS, type2, val);
  3527. break;
  3528. }
  3529. pc_bonus_itembonus(sd->skillfixcastrate, type2, -val, true); // Send inversed value here
  3530. break;
  3531. #else
  3532. case SP_SKILL_FIXEDCAST: // bonus2 bSkillFixedCast,sk,t;
  3533. case SP_SKILL_VARIABLECAST: // bonus2 bSkillVariableCast,sk,t;
  3534. case SP_FIXCASTRATE: // bonus2 bFixedCastrate,sk,n;
  3535. //ShowWarning("pc_bonus2: Non-RENEWAL_CAST doesn't support this bonus %d.\n", type);
  3536. break;
  3537. case SP_VARCASTRATE: // bonus2 bVariableCastrate,sk,n;
  3538. case SP_CASTRATE: // bonus2 bCastrate,sk,n;
  3539. if(sd->state.lr_flag == 2)
  3540. break;
  3541. if (sd->skillcastrate.size() == MAX_PC_BONUS) { //Better mention this so the array length can be updated. [Skotlex]
  3542. 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);
  3543. break;
  3544. }
  3545. pc_bonus_itembonus(sd->skillcastrate, type2, val, true);
  3546. break;
  3547. #endif
  3548. case SP_SKILL_USE_SP: // bonus2 bSkillUseSP,sk,n;
  3549. if(sd->state.lr_flag == 2)
  3550. break;
  3551. if (sd->skillusesp.size() == MAX_PC_BONUS) {
  3552. 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);
  3553. break;
  3554. }
  3555. pc_bonus_itembonus(sd->skillusesp, type2, val, false);
  3556. break;
  3557. case SP_SUB_SKILL: // bonus2 bSubSkill,sk,n;
  3558. if (sd->subskill.size() == MAX_PC_BONUS) {
  3559. 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);
  3560. break;
  3561. }
  3562. pc_bonus_itembonus(sd->subskill, type2, val, false);
  3563. break;
  3564. case SP_SUBDEF_ELE: // bonus2 bSubDefEle,e,x;
  3565. PC_BONUS_CHK_ELEMENT(type2,SP_SUBDEF_ELE);
  3566. sd->subdefele[type2] += val;
  3567. break;
  3568. case SP_COMA_CLASS: // bonus2 bComaClass,c,n;
  3569. PC_BONUS_CHK_CLASS(type2,SP_COMA_CLASS);
  3570. sd->coma_class[type2] += val;
  3571. sd->special_state.bonus_coma = 1;
  3572. break;
  3573. case SP_COMA_RACE: // bonus2 bComaRace,r,n;
  3574. PC_BONUS_CHK_RACE(type2,SP_COMA_RACE);
  3575. sd->coma_race[type2] += val;
  3576. sd->special_state.bonus_coma = 1;
  3577. break;
  3578. case SP_MAGIC_ADDRACE2: // bonus2 bMagicAddRace2,mr,n;
  3579. PC_BONUS_CHK_RACE2(type2, SP_MAGIC_ADDRACE2);
  3580. if(sd->state.lr_flag != 2)
  3581. sd->magic_addrace2[type2] += val;
  3582. break;
  3583. case SP_IGNORE_MDEF_RACE2_RATE: //bonus2 bIgnoreMdefRace2Rate,mr,n;
  3584. PC_BONUS_CHK_RACE2(type2, SP_IGNORE_MDEF_RACE2);
  3585. if (sd->state.lr_flag != 2)
  3586. sd->ignore_mdef_by_race2[type2] += val;
  3587. break;
  3588. case SP_DROP_ADDRACE: // bonus2 bDropAddRace,r,x;
  3589. PC_BONUS_CHK_RACE(type2, SP_DROP_ADDRACE);
  3590. if (sd->state.lr_flag != 2)
  3591. sd->dropaddrace[type2] += val;
  3592. break;
  3593. case SP_DROP_ADDCLASS: // bonus2 bDropAddClass,c,x;
  3594. PC_BONUS_CHK_CLASS(type2, SP_DROP_ADDCLASS);
  3595. if (sd->state.lr_flag != 2)
  3596. sd->dropaddclass[type2] += val;
  3597. break;
  3598. default:
  3599. if (running_npc_stat_calc_event) {
  3600. ShowWarning("pc_bonus2: unknown bonus type %d %d %d in OnPCStatCalcEvent!\n", type, type2, val);
  3601. }
  3602. else if (current_equip_combo_pos > 0) {
  3603. 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);
  3604. }
  3605. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  3606. 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);
  3607. }
  3608. else {
  3609. ShowWarning("pc_bonus2: unknown bonus type %d %d %d in unknown usage. Report this!\n", type, type2, val);
  3610. }
  3611. break;
  3612. }
  3613. }
  3614. /**
  3615. * Gives item bonus to player for format: bonus3 bBonusName,type2,type3,val;
  3616. * @param sd
  3617. * @param type Bonus type used by bBonusName
  3618. * @param type2
  3619. * @param type3
  3620. * @param val Value that usually for rate or fixed value
  3621. */
  3622. void pc_bonus3(struct map_session_data *sd,int type,int type2,int type3,int val)
  3623. {
  3624. nullpo_retv(sd);
  3625. switch(type){
  3626. case SP_ADD_MONSTER_DROP_ITEM: // bonus3 bAddMonsterDropItem,iid,r,n;
  3627. if(sd->state.lr_flag != 2)
  3628. pc_bonus_item_drop(sd->add_drop, type2, 0, CLASS_NONE, type3, val);
  3629. break;
  3630. case SP_ADD_MONSTER_ID_DROP_ITEM: // bonus3 bAddMonsterIdDropItem,iid,mid,n;
  3631. if(sd->state.lr_flag != 2)
  3632. pc_bonus_item_drop(sd->add_drop, type2, 0, CLASS_NONE, -type3, val);
  3633. break;
  3634. case SP_ADD_CLASS_DROP_ITEM: // bonus3 bAddClassDropItem,iid,c,n;
  3635. if(sd->state.lr_flag != 2)
  3636. pc_bonus_item_drop(sd->add_drop, type2, 0, type3, RC_NONE_, val);
  3637. break;
  3638. case SP_AUTOSPELL: // bonus3 bAutoSpell,sk,y,n;
  3639. if(sd->state.lr_flag != 2)
  3640. {
  3641. int target = skill_get_inf(type2); //Support or Self (non-auto-target) skills should pick self.
  3642. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !(skill_get_inf2(type2)&INF2_NO_TARGET_SELF));
  3643. pc_bonus_autospell(sd->autospell, target?-type2:type2, type3, val, 0, current_equip_card_id);
  3644. }
  3645. break;
  3646. case SP_AUTOSPELL_WHENHIT: // bonus3 bAutoSpellWhenHit,sk,y,n;
  3647. if(sd->state.lr_flag != 2)
  3648. {
  3649. int target = skill_get_inf(type2); //Support or Self (non-auto-target) skills should pick self.
  3650. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !(skill_get_inf2(type2)&INF2_NO_TARGET_SELF));
  3651. pc_bonus_autospell(sd->autospell2, target?-type2:type2, type3, val, BF_NORMAL|BF_SKILL, current_equip_card_id);
  3652. }
  3653. break;
  3654. case SP_ADD_MONSTER_DROP_ITEMGROUP: // bonus3 bAddMonsterDropItemGroup,ig,r,n;
  3655. if (sd->state.lr_flag != 2)
  3656. pc_bonus_item_drop(sd->add_drop, 0, type2, CLASS_NONE, type3, val);
  3657. break;
  3658. case SP_ADD_CLASS_DROP_ITEMGROUP: // bonus3 bAddClassDropItemGroup,ig,c,n;
  3659. if (sd->state.lr_flag != 2)
  3660. pc_bonus_item_drop(sd->add_drop, 0, type2, type3, RC_NONE_, val);
  3661. break;
  3662. case SP_ADDEFF: // bonus3 bAddEff,eff,n,y;
  3663. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  3664. pc_bonus_addeff(sd->addeff, (sc_type)type2, sd->state.lr_flag != 2 ? type3 : 0, sd->state.lr_flag == 2 ? type3 : 0, val, 0);
  3665. break;
  3666. case SP_ADDEFF_WHENHIT: // bonus3 bAddEffWhenHit,eff,n,y;
  3667. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  3668. if(sd->state.lr_flag != 2)
  3669. pc_bonus_addeff(sd->addeff_atked, (sc_type)type2, type3, 0, val, 0);
  3670. break;
  3671. case SP_ADDEFF_ONSKILL: // bonus3 bAddEffOnSkill,sk,eff,n;
  3672. PC_BONUS_CHK_SC(type2,SP_ADDEFF_ONSKILL);
  3673. if( sd->state.lr_flag != 2 )
  3674. pc_bonus_addeff_onskill(sd->addeff_onskill, (sc_type)type3, val, type2, ATF_TARGET, 0);
  3675. break;
  3676. case SP_ADDELE: // bonus3 bAddEle,e,x,bf;
  3677. PC_BONUS_CHK_ELEMENT(type2,SP_ADDELE);
  3678. if (sd->state.lr_flag != 2)
  3679. pc_bonus_addele(sd, (unsigned char)type2, type3, val);
  3680. break;
  3681. case SP_SUBELE: // bonus3 bSubEle,e,x,bf;
  3682. PC_BONUS_CHK_ELEMENT(type2,SP_SUBELE);
  3683. if (sd->state.lr_flag != 2)
  3684. pc_bonus_subele(sd, (unsigned char)type2, type3, val);
  3685. break;
  3686. case SP_SP_VANISH_RACE_RATE: // bonus3 bSPVanishRaceRate,r,x,n;
  3687. PC_BONUS_CHK_RACE(type2,SP_SP_VANISH_RACE_RATE);
  3688. if(sd->state.lr_flag != 2) {
  3689. sd->sp_vanish_race[type2].rate += type3;
  3690. sd->sp_vanish_race[type2].per += val;
  3691. }
  3692. break;
  3693. case SP_HP_VANISH_RACE_RATE: // bonus3 bHPVanishRaceRate,r,x,n;
  3694. PC_BONUS_CHK_RACE(type2,SP_HP_VANISH_RACE_RATE);
  3695. if(sd->state.lr_flag != 2) {
  3696. sd->hp_vanish_race[type2].rate += type3;
  3697. sd->hp_vanish_race[type2].per += val;
  3698. }
  3699. break;
  3700. case SP_SP_VANISH_RATE: // bonus3 bSPVanishRate,x,n,bf;
  3701. if(sd->state.lr_flag != 2) {
  3702. pc_bonus_addvanish(sd->sp_vanish, type2, type3, val);
  3703. }
  3704. break;
  3705. case SP_HP_VANISH_RATE: // bonus3 bHPVanishRate,x,n,bf;
  3706. if(sd->state.lr_flag != 2) {
  3707. pc_bonus_addvanish(sd->hp_vanish, type2, type3, val);
  3708. }
  3709. break;
  3710. case SP_STATE_NORECOVER_RACE: // bonus3 bStateNoRecoverRace,r,x,t;
  3711. PC_BONUS_CHK_RACE(type2, SP_STATE_NORECOVER_RACE);
  3712. if (sd->state.lr_flag == 2)
  3713. break;
  3714. //! CONFIRM: Is it not stackable? Does not check max or min value?
  3715. //if (type3 > sd->norecover_state_race[type2].rate) {
  3716. // sd->norecover_state_race[type2].rate = type3;
  3717. // sd->norecover_state_race[type2].tick = val;
  3718. // break;
  3719. //}
  3720. sd->norecover_state_race[type2].rate = type3;
  3721. sd->norecover_state_race[type2].tick = val;
  3722. break;
  3723. default:
  3724. if (running_npc_stat_calc_event) {
  3725. ShowWarning("pc_bonus3: unknown bonus type %d %d %d %d in OnPCStatCalcEvent!\n", type, type2, type3, val);
  3726. }
  3727. else if (current_equip_combo_pos > 0) {
  3728. 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);
  3729. }
  3730. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  3731. 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);
  3732. }
  3733. else {
  3734. ShowWarning("pc_bonus3: unknown bonus type %d %d %d %d in unknown usage. Report this!\n", type, type2, type3, val);
  3735. }
  3736. break;
  3737. }
  3738. }
  3739. /**
  3740. * Gives item bonus to player for format: bonus4 bBonusName,type2,type3,type4,val;
  3741. * @param sd
  3742. * @param type Bonus type used by bBonusName
  3743. * @param type2
  3744. * @param type3
  3745. * @param type4
  3746. * @param val Value that usually for rate or fixed value
  3747. */
  3748. void pc_bonus4(struct map_session_data *sd,int type,int type2,int type3,int type4,int val)
  3749. {
  3750. nullpo_retv(sd);
  3751. switch(type){
  3752. case SP_AUTOSPELL: // bonus4 bAutoSpell,sk,y,n,i;
  3753. if(sd->state.lr_flag != 2)
  3754. pc_bonus_autospell(sd->autospell, (val&1?type2:-type2), (val&2?-type3:type3), type4, 0, current_equip_card_id);
  3755. break;
  3756. case SP_AUTOSPELL_WHENHIT: // bonus4 bAutoSpellWhenHit,sk,y,n,i;
  3757. if(sd->state.lr_flag != 2)
  3758. pc_bonus_autospell(sd->autospell2, (val&1?type2:-type2), (val&2?-type3:type3), type4, BF_NORMAL|BF_SKILL, current_equip_card_id);
  3759. break;
  3760. case SP_AUTOSPELL_ONSKILL: // bonus4 bAutoSpellOnSkill,sk,x,y,n;
  3761. if(sd->state.lr_flag != 2)
  3762. {
  3763. int target = skill_get_inf(type3); //Support or Self (non-auto-target) skills should pick self.
  3764. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !(skill_get_inf2(type3)&INF2_NO_TARGET_SELF));
  3765. pc_bonus_autospell_onskill(sd->autospell3, type2, target?-type3:type3, type4, val, current_equip_card_id);
  3766. }
  3767. break;
  3768. case SP_ADDEFF: // bonus4 bAddEff,eff,n,y,t;
  3769. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  3770. pc_bonus_addeff(sd->addeff, (sc_type)type2, sd->state.lr_flag != 2 ? type3 : 0, sd->state.lr_flag == 2 ? type3 : 0, type4, val);
  3771. break;
  3772. case SP_ADDEFF_WHENHIT: // bonus4 bAddEffWhenHit,eff,n,y,t;
  3773. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  3774. if (sd->state.lr_flag != 2)
  3775. pc_bonus_addeff(sd->addeff_atked, (sc_type)type2, type3, 0, type4, val);
  3776. break;
  3777. case SP_ADDEFF_ONSKILL: // bonus4 bAddEffOnSkill,sk,eff,n,y;
  3778. PC_BONUS_CHK_SC(type3,SP_ADDEFF_ONSKILL);
  3779. if( sd->state.lr_flag != 2 )
  3780. pc_bonus_addeff_onskill(sd->addeff_onskill, (sc_type)type3, type4, type2, val, 0);
  3781. break;
  3782. case SP_SET_DEF_RACE: // bonus4 bSetDefRace,r,n,t,y;
  3783. PC_BONUS_CHK_RACE(type2,SP_SET_DEF_RACE);
  3784. if(sd->state.lr_flag == 2)
  3785. break;
  3786. sd->def_set_race[type2].rate = type3;
  3787. sd->def_set_race[type2].tick = type4;
  3788. sd->def_set_race[type2].value = val;
  3789. break;
  3790. case SP_SET_MDEF_RACE: // bonus4 bSetMDefRace,r,n,t,y;
  3791. PC_BONUS_CHK_RACE(type2,SP_SET_MDEF_RACE);
  3792. if(sd->state.lr_flag == 2)
  3793. break;
  3794. sd->mdef_set_race[type2].rate = type3;
  3795. sd->mdef_set_race[type2].tick = type4;
  3796. sd->mdef_set_race[type2].value = val;
  3797. break;
  3798. default:
  3799. if (running_npc_stat_calc_event) {
  3800. ShowWarning("pc_bonus4: unknown bonus type %d %d %d %d %d in OnPCStatCalcEvent!\n", type, type2, type3, type4, val);
  3801. }
  3802. else if (current_equip_combo_pos > 0) {
  3803. 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);
  3804. }
  3805. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  3806. 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);
  3807. }
  3808. else {
  3809. ShowWarning("pc_bonus4: unknown bonus type %d %d %d %d %d in unknown usage. Report this!\n", type, type2, type3, type4, val);
  3810. }
  3811. break;
  3812. }
  3813. }
  3814. /**
  3815. * Gives item bonus to player for format: bonus5 bBonusName,type2,type3,type4,val;
  3816. * @param sd
  3817. * @param type Bonus type used by bBonusName
  3818. * @param type2
  3819. * @param type3
  3820. * @param type4
  3821. * @param val Value that usually for rate or fixed value
  3822. */
  3823. void pc_bonus5(struct map_session_data *sd,int type,int type2,int type3,int type4,int type5,int val)
  3824. {
  3825. nullpo_retv(sd);
  3826. switch(type){
  3827. case SP_AUTOSPELL: // bonus5 bAutoSpell,sk,y,n,bf,i;
  3828. if(sd->state.lr_flag != 2)
  3829. pc_bonus_autospell(sd->autospell, (val&1?type2:-type2), (val&2?-type3:type3), type4, type5, current_equip_card_id);
  3830. break;
  3831. case SP_AUTOSPELL_WHENHIT: // bonus5 bAutoSpellWhenHit,sk,y,n,bf,i;
  3832. if(sd->state.lr_flag != 2)
  3833. pc_bonus_autospell(sd->autospell2, (val&1?type2:-type2), (val&2?-type3:type3), type4, type5, current_equip_card_id);
  3834. break;
  3835. case SP_AUTOSPELL_ONSKILL: // bonus5 bAutoSpellOnSkill,sk,x,y,n,i;
  3836. if(sd->state.lr_flag != 2)
  3837. pc_bonus_autospell_onskill(sd->autospell3, type2, (val&1?-type3:type3), (val&2?-type4:type4), type5, current_equip_card_id);
  3838. break;
  3839. case SP_ADDEFF_ONSKILL: // bonus5 bAddEffOnSkill,sk,eff,n,y,t;
  3840. PC_BONUS_CHK_SC(type3,SP_ADDEFF_ONSKILL);
  3841. if( sd->state.lr_flag != 2 )
  3842. pc_bonus_addeff_onskill(sd->addeff_onskill, (sc_type)type3, type4, type2, type5, val);
  3843. break;
  3844. default:
  3845. if (running_npc_stat_calc_event) {
  3846. ShowWarning("pc_bonus5: unknown bonus type %d %d %d %d %d %d in OnPCStatCalcEvent!\n", type, type2, type3, type4, type5, val);
  3847. }
  3848. else if (current_equip_combo_pos > 0) {
  3849. 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);
  3850. }
  3851. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  3852. 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);
  3853. }
  3854. else {
  3855. ShowWarning("pc_bonus5: unknown bonus type %d %d %d %d %d %d in unknown usage. Report this!\n", type, type2, type3, type4, type5, val);
  3856. }
  3857. break;
  3858. }
  3859. }
  3860. /*==========================================
  3861. * Grants a player a given skill. Flag values are:
  3862. * 0 - Grant permanent skill to be bound to skill tree
  3863. * 1 - Grant an item skill (temporary)
  3864. * 2 - Like 1, except the level granted can stack with previously learned level.
  3865. * 4 - Like 0, except the skill will ignore skill tree (saves through job changes and resets).
  3866. *------------------------------------------*/
  3867. bool pc_skill(struct map_session_data* sd, uint16 skill_id, int level, enum e_addskill_type type) {
  3868. uint16 idx = 0;
  3869. nullpo_ret(sd);
  3870. if (!skill_id || !(idx = skill_get_index(skill_id))) {
  3871. ShowError("pc_skill: Skill with id %d does not exist in the skill database\n", skill_id);
  3872. return false;
  3873. }
  3874. if (level > MAX_SKILL_LEVEL) {
  3875. ShowError("pc_skill: Skill level %d too high. Max lv supported is %d\n", level, MAX_SKILL_LEVEL);
  3876. return false;
  3877. }
  3878. if (type == ADDSKILL_TEMP_ADDLEVEL && sd->status.skill[idx].lv + level > MAX_SKILL_LEVEL) {
  3879. 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);
  3880. level = MAX_SKILL_LEVEL - sd->status.skill[idx].lv;
  3881. }
  3882. switch (type) {
  3883. case ADDSKILL_PERMANENT: //Set skill data overwriting whatever was there before.
  3884. sd->status.skill[idx].id = skill_id;
  3885. sd->status.skill[idx].lv = level;
  3886. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  3887. if (level == 0) { //Remove skill.
  3888. sd->status.skill[idx].id = 0;
  3889. clif_deleteskill(sd,skill_id);
  3890. } else
  3891. clif_addskill(sd,skill_id);
  3892. if (!skill_get_inf(skill_id)) //Only recalculate for passive skills.
  3893. status_calc_pc(sd, SCO_NONE);
  3894. break;
  3895. case ADDSKILL_TEMP: //Item bonus skill.
  3896. if (sd->status.skill[idx].id != 0) {
  3897. if (sd->status.skill[idx].lv >= level)
  3898. return true;
  3899. if (sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT) //Non-granted skill, store it's level.
  3900. sd->status.skill[idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[idx].lv;
  3901. } else {
  3902. sd->status.skill[idx].id = skill_id;
  3903. sd->status.skill[idx].flag = SKILL_FLAG_TEMPORARY;
  3904. }
  3905. sd->status.skill[idx].lv = level;
  3906. break;
  3907. case ADDSKILL_TEMP_ADDLEVEL: //Add skill bonus on top of what you had.
  3908. if (sd->status.skill[idx].id != 0) {
  3909. if (sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT)
  3910. sd->status.skill[idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[idx].lv; // Store previous level.
  3911. } else {
  3912. sd->status.skill[idx].id = skill_id;
  3913. sd->status.skill[idx].flag = SKILL_FLAG_TEMPORARY; //Set that this is a bonus skill.
  3914. }
  3915. sd->status.skill[idx].lv += level;
  3916. break;
  3917. case ADDSKILL_PERMANENT_GRANTED: //Permanent granted skills ignore the skill tree
  3918. sd->status.skill[idx].id = skill_id;
  3919. sd->status.skill[idx].lv = level;
  3920. sd->status.skill[idx].flag = SKILL_FLAG_PERM_GRANTED;
  3921. if (level == 0) { //Remove skill.
  3922. sd->status.skill[idx].id = 0;
  3923. clif_deleteskill(sd,skill_id);
  3924. } else
  3925. clif_addskill(sd,skill_id);
  3926. if (!skill_get_inf(skill_id)) //Only recalculate for passive skills.
  3927. status_calc_pc(sd, SCO_NONE);
  3928. break;
  3929. default:
  3930. return false;
  3931. }
  3932. return true;
  3933. }
  3934. /*==========================================
  3935. * Append a card to an item ?
  3936. *------------------------------------------*/
  3937. int pc_insert_card(struct map_session_data* sd, int idx_card, int idx_equip)
  3938. {
  3939. int i;
  3940. unsigned short nameid;
  3941. nullpo_ret(sd);
  3942. if( idx_equip < 0 || idx_equip >= MAX_INVENTORY || sd->inventory_data[idx_equip] == NULL )
  3943. return 0; //Invalid item index.
  3944. if( idx_card < 0 || idx_card >= MAX_INVENTORY || sd->inventory_data[idx_card] == NULL )
  3945. return 0; //Invalid card index.
  3946. if( sd->inventory.u.items_inventory[idx_equip].nameid <= 0 || sd->inventory.u.items_inventory[idx_equip].amount < 1 )
  3947. return 0; // target item missing
  3948. if( sd->inventory.u.items_inventory[idx_card].nameid <= 0 || sd->inventory.u.items_inventory[idx_card].amount < 1 )
  3949. return 0; // target card missing
  3950. if( sd->inventory_data[idx_equip]->type != IT_WEAPON && sd->inventory_data[idx_equip]->type != IT_ARMOR )
  3951. return 0; // only weapons and armor are allowed
  3952. if( sd->inventory_data[idx_card]->type != IT_CARD )
  3953. return 0; // must be a card
  3954. if( sd->inventory.u.items_inventory[idx_equip].identify == 0 )
  3955. return 0; // target must be identified
  3956. if( itemdb_isspecial(sd->inventory.u.items_inventory[idx_equip].card[0]) )
  3957. return 0; // card slots reserved for other purposes
  3958. if( (sd->inventory_data[idx_equip]->equip & sd->inventory_data[idx_card]->equip) == 0 )
  3959. return 0; // card cannot be compounded on this item type
  3960. if( sd->inventory_data[idx_equip]->type == IT_WEAPON && sd->inventory_data[idx_card]->equip == EQP_SHIELD )
  3961. return 0; // attempted to place shield card on left-hand weapon.
  3962. if( sd->inventory.u.items_inventory[idx_equip].equip != 0 )
  3963. return 0; // item must be unequipped
  3964. ARR_FIND( 0, sd->inventory_data[idx_equip]->slot, i, sd->inventory.u.items_inventory[idx_equip].card[i] == 0 );
  3965. if( i == sd->inventory_data[idx_equip]->slot )
  3966. return 0; // no free slots
  3967. // remember the card id to insert
  3968. nameid = sd->inventory.u.items_inventory[idx_card].nameid;
  3969. if( pc_delitem(sd,idx_card,1,1,0,LOG_TYPE_OTHER) == 1 )
  3970. {// failed
  3971. clif_insert_card(sd,idx_equip,idx_card,1);
  3972. }
  3973. else
  3974. {// success
  3975. log_pick_pc(sd, LOG_TYPE_OTHER, -1, &sd->inventory.u.items_inventory[idx_equip]);
  3976. sd->inventory.u.items_inventory[idx_equip].card[i] = nameid;
  3977. log_pick_pc(sd, LOG_TYPE_OTHER, 1, &sd->inventory.u.items_inventory[idx_equip]);
  3978. clif_insert_card(sd,idx_equip,idx_card,0);
  3979. }
  3980. return 0;
  3981. }
  3982. /**
  3983. * Returns the count of unidentified items with the option to identify too.
  3984. * @param sd: Player data
  3985. * @param identify_item: Whether or not to identify any unidentified items
  3986. * @return Unidentified items count
  3987. */
  3988. int pc_identifyall(struct map_session_data *sd, bool identify_item)
  3989. {
  3990. int unidentified_count = 0;
  3991. for (int i = 0; i < MAX_INVENTORY; i++) {
  3992. if (sd->inventory.u.items_inventory[i].nameid > 0 && sd->inventory.u.items_inventory[i].identify != 1) {
  3993. if (identify_item == true) {
  3994. sd->inventory.u.items_inventory[i].identify = 1;
  3995. clif_item_identified(sd,i,0);
  3996. }
  3997. unidentified_count++;
  3998. }
  3999. }
  4000. return unidentified_count;
  4001. }
  4002. //
  4003. // Items
  4004. //
  4005. /*==========================================
  4006. * Update buying value by skills
  4007. *------------------------------------------*/
  4008. int pc_modifybuyvalue(struct map_session_data *sd,int orig_value)
  4009. {
  4010. int skill,val = orig_value,rate1 = 0,rate2 = 0;
  4011. if((skill=pc_checkskill(sd,MC_DISCOUNT))>0) // merchant discount
  4012. rate1 = 5+skill*2-((skill==10)? 1:0);
  4013. if((skill=pc_checkskill(sd,RG_COMPULSION))>0) // rogue discount
  4014. rate2 = 5+skill*4;
  4015. if(rate1 < rate2) rate1 = rate2;
  4016. if(rate1)
  4017. val = (int)((double)orig_value*(double)(100-rate1)/100.);
  4018. if(val < battle_config.min_shop_buy)
  4019. val = battle_config.min_shop_buy;
  4020. return val;
  4021. }
  4022. /*==========================================
  4023. * Update selling value by skills
  4024. *------------------------------------------*/
  4025. int pc_modifysellvalue(struct map_session_data *sd,int orig_value)
  4026. {
  4027. int skill,val = orig_value,rate = 0;
  4028. if((skill=pc_checkskill(sd,MC_OVERCHARGE))>0) //OverCharge
  4029. rate = 5+skill*2-((skill==10)? 1:0);
  4030. if(rate)
  4031. val = (int)((double)orig_value*(double)(100+rate)/100.);
  4032. if (val < battle_config.min_shop_sell)
  4033. val = battle_config.min_shop_sell;
  4034. return val;
  4035. }
  4036. /*==========================================
  4037. * Checking if we have enough place on inventory for new item
  4038. * Make sure to take 30k as limit (for client I guess)
  4039. * @param sd
  4040. * @param nameid
  4041. * @param amount
  4042. * @return e_chkitem_result
  4043. *------------------------------------------*/
  4044. char pc_checkadditem(struct map_session_data *sd, unsigned short nameid, int amount)
  4045. {
  4046. int i;
  4047. struct item_data* data;
  4048. nullpo_ret(sd);
  4049. if(amount > MAX_AMOUNT)
  4050. return CHKADDITEM_OVERAMOUNT;
  4051. data = itemdb_search(nameid);
  4052. if(!itemdb_isstackable2(data))
  4053. return CHKADDITEM_NEW;
  4054. if( data->stack.inventory && amount > data->stack.amount )
  4055. return CHKADDITEM_OVERAMOUNT;
  4056. for(i=0;i<MAX_INVENTORY;i++){
  4057. // FIXME: This does not consider the checked item's cards, thus could check a wrong slot for stackability.
  4058. if(sd->inventory.u.items_inventory[i].nameid == nameid){
  4059. 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 ) )
  4060. return CHKADDITEM_OVERAMOUNT;
  4061. return CHKADDITEM_EXIST;
  4062. }
  4063. }
  4064. return CHKADDITEM_NEW;
  4065. }
  4066. /*==========================================
  4067. * Return number of available place in inventory
  4068. * Each non stackable item will reduce place by 1
  4069. * @param sd
  4070. * @return Number of empty slots
  4071. *------------------------------------------*/
  4072. uint8 pc_inventoryblank(struct map_session_data *sd)
  4073. {
  4074. uint8 i, b;
  4075. nullpo_ret(sd);
  4076. for(i = 0, b = 0; i < MAX_INVENTORY; i++){
  4077. if(sd->inventory.u.items_inventory[i].nameid == 0)
  4078. b++;
  4079. }
  4080. return b;
  4081. }
  4082. /**
  4083. * Attempts to remove zeny from player
  4084. * @param sd: Player
  4085. * @param zeny: Zeny removed
  4086. * @param type: Log type
  4087. * @param tsd: (optional) From who to log (if null take sd)
  4088. * @return 0: Success, 1: Failed (Removing negative Zeny or not enough Zeny), 2: Player not found
  4089. */
  4090. char pc_payzeny(struct map_session_data *sd, int zeny, enum e_log_pick_type type, struct map_session_data *tsd)
  4091. {
  4092. nullpo_retr(2,sd);
  4093. zeny = cap_value(zeny,-MAX_ZENY,MAX_ZENY); //prevent command UB
  4094. if( zeny < 0 )
  4095. {
  4096. ShowError("pc_payzeny: Paying negative Zeny (zeny=%d, account_id=%d, char_id=%d).\n", zeny, sd->status.account_id, sd->status.char_id);
  4097. return 1;
  4098. }
  4099. if( sd->status.zeny < zeny )
  4100. return 1; //Not enough.
  4101. sd->status.zeny -= zeny;
  4102. clif_updatestatus(sd,SP_ZENY);
  4103. if(!tsd) tsd = sd;
  4104. log_zeny(sd, type, tsd, -zeny);
  4105. if( zeny > 0 && sd->state.showzeny ) {
  4106. char output[255];
  4107. sprintf(output, "Removed %dz.", zeny);
  4108. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4109. }
  4110. return 0;
  4111. }
  4112. /**
  4113. * Attempts to give zeny to player
  4114. * @param sd: Player
  4115. * @param type: Log type
  4116. * @param tsd: (optional) From who to log (if null take sd)
  4117. * @return -1: Player not found, 0: Success, 1: Giving negative Zeny
  4118. */
  4119. char pc_getzeny(struct map_session_data *sd, int zeny, enum e_log_pick_type type, struct map_session_data *tsd)
  4120. {
  4121. nullpo_retr(-1,sd);
  4122. zeny = cap_value(zeny,-MAX_ZENY,MAX_ZENY); //prevent command UB
  4123. if( zeny < 0 )
  4124. {
  4125. ShowError("pc_getzeny: Obtaining negative Zeny (zeny=%d, account_id=%d, char_id=%d).\n", zeny, sd->status.account_id, sd->status.char_id);
  4126. return 1;
  4127. }
  4128. if( zeny > MAX_ZENY - sd->status.zeny )
  4129. zeny = MAX_ZENY - sd->status.zeny;
  4130. sd->status.zeny += zeny;
  4131. clif_updatestatus(sd,SP_ZENY);
  4132. if(!tsd) tsd = sd;
  4133. log_zeny(sd, type, tsd, zeny);
  4134. if( zeny > 0 && sd->state.showzeny ) {
  4135. char output[255];
  4136. sprintf(output, "Gained %dz.", zeny);
  4137. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4138. }
  4139. achievement_update_objective(sd, AG_GET_ZENY, 1, sd->status.zeny);
  4140. return 0;
  4141. }
  4142. /**
  4143. * Attempts to remove Cash Points from player
  4144. * @param sd: Player
  4145. * @param price: Total points (cash + kafra) the player has to pay
  4146. * @param points: Kafra points the player has to pay
  4147. * @param type: Log type
  4148. * @return -1: Not enough points, otherwise success (cash+points)
  4149. */
  4150. int pc_paycash(struct map_session_data *sd, int price, int points, e_log_pick_type type)
  4151. {
  4152. int cash;
  4153. nullpo_retr(-1,sd);
  4154. points = cap_value(points, 0, MAX_ZENY); //prevent command UB
  4155. cash = price-points;
  4156. if( sd->cashPoints < cash || sd->kafraPoints < points )
  4157. {
  4158. 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);
  4159. return -1;
  4160. }
  4161. if( cash ){
  4162. pc_setaccountreg(sd, add_str(CASHPOINT_VAR), sd->cashPoints - cash);
  4163. sd->cashPoints -= cash;
  4164. log_cash( sd, type, LOG_CASH_TYPE_CASH, -cash );
  4165. }
  4166. if( points ){
  4167. pc_setaccountreg(sd, add_str(KAFRAPOINT_VAR), sd->kafraPoints - points);
  4168. sd->kafraPoints -= points;
  4169. log_cash( sd, type, LOG_CASH_TYPE_KAFRA, -points );
  4170. }
  4171. if( battle_config.cashshop_show_points )
  4172. {
  4173. char output[CHAT_SIZE_MAX];
  4174. 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.
  4175. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4176. }
  4177. return cash+points;
  4178. }
  4179. /**
  4180. * Attempts to give Cash Points to player
  4181. * @param sd: Player
  4182. * @param cash: Cash points the player gets
  4183. * @param points: Kafra points the player gets
  4184. * @param type: Log type
  4185. * @return -1: Error, otherwise success (cash or points)
  4186. */
  4187. int pc_getcash(struct map_session_data *sd, int cash, int points, e_log_pick_type type)
  4188. {
  4189. char output[CHAT_SIZE_MAX];
  4190. nullpo_retr(-1,sd);
  4191. cash = cap_value(cash, 0, MAX_ZENY); //prevent command UB
  4192. points = cap_value(points, 0, MAX_ZENY); //prevent command UB
  4193. if( cash > 0 )
  4194. {
  4195. if( cash > MAX_ZENY-sd->cashPoints )
  4196. {
  4197. 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);
  4198. cash = MAX_ZENY-sd->cashPoints;
  4199. }
  4200. pc_setaccountreg(sd, add_str(CASHPOINT_VAR), sd->cashPoints+cash);
  4201. sd->cashPoints += cash;
  4202. log_cash( sd, type, LOG_CASH_TYPE_CASH, cash );
  4203. if( battle_config.cashshop_show_points )
  4204. {
  4205. sprintf(output, msg_txt(sd,505), cash, sd->cashPoints);
  4206. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4207. }
  4208. return cash;
  4209. }
  4210. if( points > 0 )
  4211. {
  4212. if( points > MAX_ZENY-sd->kafraPoints )
  4213. {
  4214. 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);
  4215. points = MAX_ZENY-sd->kafraPoints;
  4216. }
  4217. pc_setaccountreg(sd, add_str(KAFRAPOINT_VAR), sd->kafraPoints+points);
  4218. sd->kafraPoints += points;
  4219. log_cash( sd, type, LOG_CASH_TYPE_KAFRA, points );
  4220. if( battle_config.cashshop_show_points )
  4221. {
  4222. sprintf(output, msg_txt(sd,506), points, sd->kafraPoints);
  4223. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4224. }
  4225. return points;
  4226. }
  4227. return -1; //shouldn't happen but just in case
  4228. }
  4229. /**
  4230. * Searching a specified itemid in inventory and return his stored index
  4231. * @param sd Player
  4232. * @param nameid Find this Item!
  4233. * @return Stored index in inventory, or -1 if not found.
  4234. **/
  4235. short pc_search_inventory(struct map_session_data *sd, unsigned short nameid) {
  4236. short i;
  4237. nullpo_retr(-1, sd);
  4238. ARR_FIND( 0, MAX_INVENTORY, i, sd->inventory.u.items_inventory[i].nameid == nameid && (sd->inventory.u.items_inventory[i].amount > 0 || nameid == 0) );
  4239. return ( i < MAX_INVENTORY ) ? i : -1;
  4240. }
  4241. /** Attempt to add a new item to player inventory
  4242. * @param sd
  4243. * @param item_data
  4244. * @param amount
  4245. * @param log_type
  4246. * @return
  4247. * 0 = success
  4248. * 1 = invalid itemid not found or negative amount
  4249. * 2 = overweight
  4250. * 3 = ?
  4251. * 4 = no free place found
  4252. * 5 = max amount reached
  4253. * 6 = ?
  4254. * 7 = stack limitation
  4255. */
  4256. enum e_additem_result pc_additem(struct map_session_data *sd,struct item *item,int amount,e_log_pick_type log_type) {
  4257. struct item_data *id;
  4258. int16 i;
  4259. unsigned int w;
  4260. nullpo_retr(ADDITEM_INVALID, sd);
  4261. nullpo_retr(ADDITEM_INVALID, item);
  4262. if( item->nameid == 0 || amount <= 0 )
  4263. return ADDITEM_INVALID;
  4264. if( amount > MAX_AMOUNT )
  4265. return ADDITEM_OVERAMOUNT;
  4266. id = itemdb_search(item->nameid);
  4267. if( id->stack.inventory && amount > id->stack.amount )
  4268. {// item stack limitation
  4269. return ADDITEM_STACKLIMIT;
  4270. }
  4271. w = id->weight*amount;
  4272. if(sd->weight + w > sd->max_weight)
  4273. return ADDITEM_OVERWEIGHT;
  4274. i = MAX_INVENTORY;
  4275. if (id->flag.guid && !item->unique_id)
  4276. item->unique_id = pc_generate_unique_id(sd);
  4277. // Stackable | Non Rental
  4278. if( itemdb_isstackable2(id) && item->expire_time == 0 ) {
  4279. for( i = 0; i < MAX_INVENTORY; i++ ) {
  4280. if( sd->inventory.u.items_inventory[i].nameid == item->nameid &&
  4281. sd->inventory.u.items_inventory[i].bound == item->bound &&
  4282. sd->inventory.u.items_inventory[i].expire_time == 0 &&
  4283. sd->inventory.u.items_inventory[i].unique_id == item->unique_id &&
  4284. memcmp(&sd->inventory.u.items_inventory[i].card, &item->card, sizeof(item->card)) == 0 ) {
  4285. 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 ) )
  4286. return ADDITEM_OVERAMOUNT;
  4287. sd->inventory.u.items_inventory[i].amount += amount;
  4288. clif_additem(sd,i,amount,0);
  4289. break;
  4290. }
  4291. }
  4292. }
  4293. if (i >= MAX_INVENTORY) {
  4294. i = pc_search_inventory(sd,0);
  4295. if( i < 0 )
  4296. return ADDITEM_OVERITEM;
  4297. memcpy(&sd->inventory.u.items_inventory[i], item, sizeof(sd->inventory.u.items_inventory[0]));
  4298. // clear equip and favorite fields first, just in case
  4299. if( item->equip )
  4300. sd->inventory.u.items_inventory[i].equip = 0;
  4301. if( item->favorite )
  4302. sd->inventory.u.items_inventory[i].favorite = 0;
  4303. if( item->equipSwitch )
  4304. sd->inventory.u.items_inventory[i].equipSwitch = 0;
  4305. sd->inventory.u.items_inventory[i].amount = amount;
  4306. sd->inventory_data[i] = id;
  4307. sd->last_addeditem_index = i;
  4308. if (!itemdb_isstackable2(id) || id->flag.guid)
  4309. sd->inventory.u.items_inventory[i].unique_id = item->unique_id ? item->unique_id : pc_generate_unique_id(sd);
  4310. clif_additem(sd,i,amount,0);
  4311. }
  4312. log_pick_pc(sd, log_type, amount, &sd->inventory.u.items_inventory[i]);
  4313. sd->weight += w;
  4314. clif_updatestatus(sd,SP_WEIGHT);
  4315. //Auto-equip
  4316. if(id->flag.autoequip)
  4317. pc_equipitem(sd, i, id->equip);
  4318. /* rental item check */
  4319. if( item->expire_time ) {
  4320. if( time(NULL) > item->expire_time ) {
  4321. clif_rental_expired(sd->fd, i, sd->inventory.u.items_inventory[i].nameid);
  4322. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 1, 0, LOG_TYPE_OTHER);
  4323. } else {
  4324. unsigned int seconds = (unsigned int)( item->expire_time - time(NULL) );
  4325. clif_rental_time(sd->fd, sd->inventory.u.items_inventory[i].nameid, seconds);
  4326. pc_inventory_rental_add(sd, seconds);
  4327. }
  4328. }
  4329. achievement_update_objective(sd, AG_GET_ITEM, 1, id->value_sell);
  4330. return ADDITEM_SUCCESS;
  4331. }
  4332. /*==========================================
  4333. * Remove an item at index n from inventory by amount.
  4334. * @param sd
  4335. * @param n Item index in inventory
  4336. * @param amount
  4337. * @param type &1: Don't notify deletion; &2 Don't notify weight change; &4 Don't calculate status
  4338. * @param reason Delete reason
  4339. * @param log_type e_log_pick_type
  4340. * @return 1 - invalid itemid or negative amount; 0 - Success
  4341. *------------------------------------------*/
  4342. char pc_delitem(struct map_session_data *sd,int n,int amount,int type, short reason, e_log_pick_type log_type)
  4343. {
  4344. nullpo_retr(1, sd);
  4345. 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)
  4346. return 1;
  4347. log_pick_pc(sd, log_type, -amount, &sd->inventory.u.items_inventory[n]);
  4348. sd->inventory.u.items_inventory[n].amount -= amount;
  4349. sd->weight -= sd->inventory_data[n]->weight*amount ;
  4350. if( sd->inventory.u.items_inventory[n].amount <= 0 ){
  4351. if(sd->inventory.u.items_inventory[n].equip)
  4352. pc_unequipitem(sd,n,2|(!(type&4) ? 1 : 0));
  4353. memset(&sd->inventory.u.items_inventory[n],0,sizeof(sd->inventory.u.items_inventory[0]));
  4354. sd->inventory_data[n] = NULL;
  4355. }
  4356. if(!(type&1))
  4357. clif_delitem(sd,n,amount,reason);
  4358. if(!(type&2))
  4359. clif_updatestatus(sd,SP_WEIGHT);
  4360. return 0;
  4361. }
  4362. /*==========================================
  4363. * Attempt to drop an item.
  4364. * @param sd
  4365. * @param n Item index in inventory
  4366. * @param amount Amount of item
  4367. * @return False = fail; True = success
  4368. *------------------------------------------*/
  4369. bool pc_dropitem(struct map_session_data *sd,int n,int amount)
  4370. {
  4371. nullpo_retr(1, sd);
  4372. if(n < 0 || n >= MAX_INVENTORY)
  4373. return false;
  4374. if(amount <= 0)
  4375. return false;
  4376. if(sd->inventory.u.items_inventory[n].nameid <= 0 ||
  4377. sd->inventory.u.items_inventory[n].amount <= 0 ||
  4378. sd->inventory.u.items_inventory[n].amount < amount ||
  4379. sd->state.trading || sd->state.vending ||
  4380. !sd->inventory_data[n] //pc_delitem would fail on this case.
  4381. )
  4382. return false;
  4383. if( sd->inventory.u.items_inventory[n].equipSwitch )
  4384. return false;
  4385. if( map_getmapflag(sd->bl.m, MF_NODROP) )
  4386. {
  4387. clif_displaymessage (sd->fd, msg_txt(sd,271));
  4388. return false; //Can't drop items in nodrop mapflag maps.
  4389. }
  4390. if( !pc_candrop(sd,&sd->inventory.u.items_inventory[n]) )
  4391. {
  4392. clif_displaymessage (sd->fd, msg_txt(sd,263));
  4393. return false;
  4394. }
  4395. if (!map_addflooritem(&sd->inventory.u.items_inventory[n], amount, sd->bl.m, sd->bl.x, sd->bl.y, 0, 0, 0, 2, 0))
  4396. return false;
  4397. pc_delitem(sd, n, amount, 1, 0, LOG_TYPE_PICKDROP_PLAYER);
  4398. clif_dropitem(sd, n, amount);
  4399. return true;
  4400. }
  4401. /*==========================================
  4402. * Attempt to pick up an item.
  4403. * @param sd
  4404. * @param fitem Item that will be picked
  4405. * @return False = fail; True = success
  4406. *------------------------------------------*/
  4407. bool pc_takeitem(struct map_session_data *sd,struct flooritem_data *fitem)
  4408. {
  4409. int flag = 0;
  4410. t_tick tick = gettick();
  4411. struct party_data *p = NULL;
  4412. nullpo_ret(sd);
  4413. nullpo_ret(fitem);
  4414. if (!check_distance_bl(&fitem->bl, &sd->bl, 2) && sd->ud.skill_id!=BS_GREED)
  4415. return false; // Distance is too far
  4416. if (sd->sc.cant.pickup)
  4417. return false;
  4418. if (sd->status.party_id)
  4419. p = party_search(sd->status.party_id);
  4420. if (fitem->first_get_charid > 0 && fitem->first_get_charid != sd->status.char_id) {
  4421. struct map_session_data *first_sd = map_charid2sd(fitem->first_get_charid);
  4422. if (DIFF_TICK(tick,fitem->first_get_tick) < 0) {
  4423. if (!(p && p->party.item&1 &&
  4424. first_sd && first_sd->status.party_id == sd->status.party_id
  4425. ))
  4426. return false;
  4427. }
  4428. else if (fitem->second_get_charid > 0 && fitem->second_get_charid != sd->status.char_id) {
  4429. struct map_session_data *second_sd = map_charid2sd(fitem->second_get_charid);
  4430. if (DIFF_TICK(tick, fitem->second_get_tick) < 0) {
  4431. if (!(p && p->party.item&1 &&
  4432. ((first_sd && first_sd->status.party_id == sd->status.party_id) ||
  4433. (second_sd && second_sd->status.party_id == sd->status.party_id))
  4434. ))
  4435. return false;
  4436. }
  4437. else if (fitem->third_get_charid > 0 && fitem->third_get_charid != sd->status.char_id){
  4438. struct map_session_data *third_sd = map_charid2sd(fitem->third_get_charid);
  4439. if (DIFF_TICK(tick,fitem->third_get_tick) < 0) {
  4440. if(!(p && p->party.item&1 &&
  4441. ((first_sd && first_sd->status.party_id == sd->status.party_id) ||
  4442. (second_sd && second_sd->status.party_id == sd->status.party_id) ||
  4443. (third_sd && third_sd->status.party_id == sd->status.party_id))
  4444. ))
  4445. return false;
  4446. }
  4447. }
  4448. }
  4449. }
  4450. //This function takes care of giving the item to whoever should have it, considering party-share options.
  4451. if ((flag = party_share_loot(p,sd,&fitem->item, fitem->first_get_charid))) {
  4452. clif_additem(sd,0,0,flag);
  4453. return true;
  4454. }
  4455. //Display pickup animation.
  4456. pc_stop_attack(sd);
  4457. clif_takeitem(&sd->bl,&fitem->bl);
  4458. if (fitem->mob_id &&
  4459. (itemdb_search(fitem->item.nameid))->flag.broadcast &&
  4460. (!p || !(p->party.item&2)) // Somehow, if party's pickup distribution is 'Even Share', no announcemet
  4461. )
  4462. intif_broadcast_obtain_special_item(sd, fitem->item.nameid, fitem->mob_id, ITEMOBTAIN_TYPE_MONSTER_ITEM);
  4463. map_clearflooritem(&fitem->bl);
  4464. return true;
  4465. }
  4466. /*==========================================
  4467. * Check if item is usable.
  4468. * Return:
  4469. * false = no
  4470. * true = yes
  4471. *------------------------------------------*/
  4472. bool pc_isUseitem(struct map_session_data *sd,int n)
  4473. {
  4474. struct item_data *item;
  4475. unsigned short nameid;
  4476. nullpo_ret(sd);
  4477. item = sd->inventory_data[n];
  4478. nameid = sd->inventory.u.items_inventory[n].nameid;
  4479. if( item == NULL )
  4480. return false;
  4481. //Not consumable item
  4482. if( item->type != IT_HEALING && item->type != IT_USABLE && item->type != IT_CASH )
  4483. return false;
  4484. if (pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL))
  4485. return true;
  4486. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  4487. if(mapdata->flag[MF_NOITEMCONSUMPTION]) //consumable but mapflag prevent it
  4488. return false;
  4489. //Prevent mass item usage. [Skotlex]
  4490. if( DIFF_TICK(sd->canuseitem_tick,gettick()) > 0 ||
  4491. (itemdb_group_item_exists(IG_CASH_FOOD, nameid) && DIFF_TICK(sd->canusecashfood_tick,gettick()) > 0)
  4492. )
  4493. return false;
  4494. if( (item->item_usage.flag&NOUSE_SITTING) && (pc_issit(sd) == 1) && (pc_get_group_level(sd) < item->item_usage.override) ) {
  4495. clif_msg(sd,ITEM_NOUSE_SITTING);
  4496. return false; // You cannot use this item while sitting.
  4497. }
  4498. if (sd->state.storage_flag && item->type != IT_CASH) {
  4499. clif_messagecolor(&sd->bl, color_table[COLOR_RED], msg_txt(sd,388), false, SELF);
  4500. return false; // You cannot use this item while storage is open.
  4501. }
  4502. if (item->flag.dead_branch && (mapdata->flag[MF_NOBRANCH] || mapdata_flag_gvg2(mapdata)))
  4503. return false;
  4504. switch( nameid ) {
  4505. case ITEMID_WING_OF_FLY:
  4506. case ITEMID_GIANT_FLY_WING:
  4507. case ITEMID_N_FLY_WING:
  4508. if( mapdata->flag[MF_NOTELEPORT] || mapdata_flag_gvg2(mapdata) ) {
  4509. clif_skill_teleportmessage(sd,0);
  4510. return false;
  4511. }
  4512. if (nameid == ITEMID_GIANT_FLY_WING) {
  4513. struct party_data *pd = party_search(sd->status.party_id);
  4514. if (pd) {
  4515. int i;
  4516. ARR_FIND(0, MAX_PARTY, i, pd->data[i].sd == sd && pd->party.member[i].leader);
  4517. if (i == MAX_PARTY) { // User is not party leader
  4518. clif_msg(sd, ITEM_PARTY_MEMBER_NOT_SUMMONED);
  4519. break;
  4520. }
  4521. 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));
  4522. if (i == MAX_PARTY) { // No party members found on same map
  4523. clif_msg(sd, ITEM_PARTY_NO_MEMBER_IN_MAP);
  4524. break;
  4525. }
  4526. } else {
  4527. clif_msg(sd, ITEM_PARTY_MEMBER_NOT_SUMMONED);
  4528. break;
  4529. }
  4530. }
  4531. // Fall through
  4532. case ITEMID_WING_OF_BUTTERFLY:
  4533. case ITEMID_N_BUTTERFLY_WING:
  4534. case ITEMID_DUN_TELE_SCROLL1:
  4535. case ITEMID_DUN_TELE_SCROLL2:
  4536. case ITEMID_DUN_TELE_SCROLL3:
  4537. case ITEMID_WOB_RUNE:
  4538. case ITEMID_WOB_SCHWALTZ:
  4539. case ITEMID_WOB_RACHEL:
  4540. case ITEMID_WOB_LOCAL:
  4541. case ITEMID_SIEGE_TELEPORT_SCROLL:
  4542. if( sd->duel_group && !battle_config.duel_allow_teleport ) {
  4543. clif_displaymessage(sd->fd, msg_txt(sd,663));
  4544. return false;
  4545. }
  4546. if( mapdata->flag[MF_NORETURN] && nameid != ITEMID_WING_OF_FLY && nameid != ITEMID_GIANT_FLY_WING && nameid != ITEMID_N_FLY_WING )
  4547. return false;
  4548. break;
  4549. case ITEMID_MERCENARY_RED_POTION:
  4550. case ITEMID_MERCENARY_BLUE_POTION:
  4551. case ITEMID_M_CENTER_POTION:
  4552. case ITEMID_M_AWAKENING_POTION:
  4553. case ITEMID_M_BERSERK_POTION:
  4554. if( sd->md == NULL || sd->md->db == NULL )
  4555. return false;
  4556. if( sd->md->sc.data[SC_BERSERK] )
  4557. return false;
  4558. if( nameid == ITEMID_M_AWAKENING_POTION && sd->md->db->lv < 40 )
  4559. return false;
  4560. if( nameid == ITEMID_M_BERSERK_POTION && sd->md->db->lv < 80 )
  4561. return false;
  4562. break;
  4563. case ITEMID_NEURALIZER:
  4564. if( !mapdata->flag[MF_RESET] )
  4565. return false;
  4566. break;
  4567. }
  4568. if( itemdb_group_item_exists(IG_MERCENARY, nameid) && sd->md != NULL )
  4569. return false; // Mercenary Scrolls
  4570. if( item->flag.group || item->type == IT_CASH) { //safe check type cash disappear when overweight [Napster]
  4571. if( pc_is90overweight(sd) ) {
  4572. clif_msg(sd, ITEM_CANT_OBTAIN_WEIGHT);
  4573. return false;
  4574. }
  4575. if( !pc_inventoryblank(sd) ) {
  4576. clif_messagecolor(&sd->bl, color_table[COLOR_RED], msg_txt(sd, 732), false, SELF); //Item cannot be open when inventory is full
  4577. return false;
  4578. }
  4579. }
  4580. //Gender check
  4581. if(item->sex != 2 && sd->status.sex != item->sex)
  4582. return false;
  4583. //Required level check
  4584. if(item->elv && sd->status.base_level < (unsigned int)item->elv)
  4585. return false;
  4586. if(item->elvmax && sd->status.base_level > (unsigned int)item->elvmax)
  4587. return false;
  4588. //Not equipable by class. [Skotlex]
  4589. if (!(
  4590. (1ULL<<(sd->class_&MAPID_BASEMASK)) &
  4591. (item->class_base[sd->class_&JOBL_2_1?1:(sd->class_&JOBL_2_2?2:0)])
  4592. ))
  4593. return false;
  4594. if (sd->sc.count && (
  4595. sd->sc.data[SC_BERSERK] || sd->sc.data[SC_SATURDAYNIGHTFEVER] ||
  4596. (sd->sc.data[SC_GRAVITATION] && sd->sc.data[SC_GRAVITATION]->val3 == BCT_SELF) ||
  4597. sd->sc.data[SC_TRICKDEAD] ||
  4598. sd->sc.data[SC_HIDING] ||
  4599. sd->sc.data[SC__SHADOWFORM] ||
  4600. sd->sc.data[SC__INVISIBILITY] ||
  4601. sd->sc.data[SC__MANHOLE] ||
  4602. sd->sc.data[SC_DEEPSLEEP] ||
  4603. sd->sc.data[SC_CRYSTALIZE] ||
  4604. sd->sc.data[SC_KAGEHUMI] ||
  4605. (sd->sc.data[SC_NOCHAT] && sd->sc.data[SC_NOCHAT]->val1&MANNER_NOITEM) ||
  4606. sd->sc.data[SC_KINGS_GRACE] ||
  4607. sd->sc.data[SC_SUHIDE]))
  4608. return false;
  4609. if (!pc_isItemClass(sd,item))
  4610. return false;
  4611. //Dead Branch items
  4612. if( item->flag.dead_branch )
  4613. log_branch(sd);
  4614. return true;
  4615. }
  4616. /*==========================================
  4617. * Last checks to use an item.
  4618. * Return:
  4619. * 0 = fail
  4620. * 1 = success
  4621. *------------------------------------------*/
  4622. int pc_useitem(struct map_session_data *sd,int n)
  4623. {
  4624. t_tick tick = gettick();
  4625. int amount;
  4626. unsigned short nameid;
  4627. struct script_code *script;
  4628. struct item item;
  4629. struct item_data *id;
  4630. nullpo_ret(sd);
  4631. if (sd->npc_id) {
  4632. if (sd->progressbar.npc_id) {
  4633. clif_progressbar_abort(sd);
  4634. return 0; // First item use attempt cancels the progress bar
  4635. }
  4636. if( pc_hasprogress( sd, WIP_DISABLE_SKILLITEM ) || !sd->npc_item_flag ){
  4637. #ifdef RENEWAL
  4638. clif_msg( sd, WORK_IN_PROGRESS );
  4639. #endif
  4640. return 0;
  4641. }
  4642. }
  4643. item = sd->inventory.u.items_inventory[n];
  4644. id = sd->inventory_data[n];
  4645. if (item.nameid == 0 || item.amount <= 0)
  4646. return 0;
  4647. if( !pc_isUseitem(sd,n) )
  4648. return 0;
  4649. // Store information for later use before it is lost (via pc_delitem) [Paradox924X]
  4650. nameid = id->nameid;
  4651. if (nameid != ITEMID_NAUTHIZ && sd->sc.opt1 > 0 && sd->sc.opt1 != OPT1_STONEWAIT && sd->sc.opt1 != OPT1_BURNING)
  4652. return 0;
  4653. /* Items with delayed consume are not meant to work while in mounts except reins of mount(12622) */
  4654. if( id->flag.delay_consume ) {
  4655. if( nameid != ITEMID_REINS_OF_MOUNT && sd->sc.data[SC_ALL_RIDING] )
  4656. return 0;
  4657. else if( pc_issit(sd) )
  4658. return 0;
  4659. }
  4660. //Since most delay-consume items involve using a "skill-type" target cursor,
  4661. //perform a skill-use check before going through. [Skotlex]
  4662. //resurrection was picked as testing skill, as a non-offensive, generic skill, it will do.
  4663. //FIXME: Is this really needed here? It'll be checked in unit.cpp after all and this prevents skill items using when silenced [Inkfish]
  4664. if( id->flag.delay_consume && ( sd->ud.skilltimer != INVALID_TIMER /*|| !status_check_skilluse(&sd->bl, &sd->bl, ALL_RESURRECTION, 0)*/ ) )
  4665. return 0;
  4666. if( id->delay > 0 && !pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL) && pc_itemcd_check(sd, id, tick, n))
  4667. return 0;
  4668. /* on restricted maps the item is consumed but the effect is not used */
  4669. if (!pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL) && itemdb_isNoEquip(id,sd->bl.m)) {
  4670. clif_msg(sd,ITEM_CANT_USE_AREA); // This item cannot be used within this area
  4671. if( battle_config.allow_consume_restricted_item && !id->flag.delay_consume ) { //need confirmation for delayed consumption items
  4672. clif_useitemack(sd,n,item.amount-1,true);
  4673. pc_delitem(sd,n,1,1,0,LOG_TYPE_CONSUME);
  4674. }
  4675. return 0;/* regardless, effect is not run */
  4676. }
  4677. sd->itemid = item.nameid;
  4678. sd->itemindex = n;
  4679. if(sd->catch_target_class != PET_CATCH_FAIL) //Abort pet catching.
  4680. sd->catch_target_class = PET_CATCH_FAIL;
  4681. amount = item.amount;
  4682. script = id->script;
  4683. //Check if the item is to be consumed immediately [Skotlex]
  4684. if (id->flag.delay_consume)
  4685. clif_useitemack(sd, n, amount, true);
  4686. else
  4687. {
  4688. if( item.expire_time == 0 && nameid != ITEMID_REINS_OF_MOUNT )
  4689. {
  4690. clif_useitemack(sd, n, amount - 1, true);
  4691. pc_delitem(sd, n, 1, 1, 0, LOG_TYPE_CONSUME); // Rental Usable Items are not deleted until expiration
  4692. }
  4693. else
  4694. clif_useitemack(sd, n, 0, false);
  4695. }
  4696. if (item.card[0]==CARD0_CREATE && pc_famerank(MakeDWord(item.card[2],item.card[3]), MAPID_ALCHEMIST))
  4697. potion_flag = 2; // Famous player's potions have 50% more efficiency
  4698. //Update item use time.
  4699. sd->canuseitem_tick = tick + battle_config.item_use_interval;
  4700. if( itemdb_group_item_exists(IG_CASH_FOOD, nameid) )
  4701. sd->canusecashfood_tick = tick + battle_config.cashfood_use_interval;
  4702. run_script(script,0,sd->bl.id,fake_nd->bl.id);
  4703. potion_flag = 0;
  4704. return 1;
  4705. }
  4706. /**
  4707. * Add item on cart for given index.
  4708. * @param sd
  4709. * @param item
  4710. * @param amount
  4711. * @param log_type
  4712. * @return See pc.hpp::e_additem_result
  4713. */
  4714. enum e_additem_result pc_cart_additem(struct map_session_data *sd,struct item *item,int amount,e_log_pick_type log_type)
  4715. {
  4716. struct item_data *data;
  4717. int i,w;
  4718. nullpo_retr(ADDITEM_INVALID, sd);
  4719. nullpo_retr(ADDITEM_INVALID, item);
  4720. if(item->nameid == 0 || amount <= 0)
  4721. return ADDITEM_INVALID;
  4722. data = itemdb_search(item->nameid);
  4723. if( data->stack.cart && amount > data->stack.amount )
  4724. {// item stack limitation
  4725. return ADDITEM_STACKLIMIT;
  4726. }
  4727. if( !itemdb_cancartstore(item, pc_get_group_level(sd)) || (item->bound > BOUND_ACCOUNT && !pc_can_give_bounded_items(sd)))
  4728. { // Check item trade restrictions [Skotlex]
  4729. clif_displaymessage (sd->fd, msg_txt(sd,264));
  4730. return ADDITEM_INVALID;
  4731. }
  4732. if( (w = data->weight*amount) + sd->cart_weight > sd->cart_weight_max )
  4733. return ADDITEM_OVERWEIGHT;
  4734. i = MAX_CART;
  4735. if( itemdb_isstackable2(data) && !item->expire_time )
  4736. {
  4737. for (i = 0; i < MAX_CART; i++) {
  4738. if (sd->cart.u.items_cart[i].nameid == item->nameid
  4739. && sd->cart.u.items_cart[i].bound == item->bound
  4740. && sd->cart.u.items_cart[i].unique_id == item->unique_id
  4741. && memcmp(sd->cart.u.items_cart[i].card, item->card, sizeof(item->card)) == 0
  4742. )
  4743. break;
  4744. }
  4745. }
  4746. if( i < MAX_CART )
  4747. {// item already in cart, stack it
  4748. 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 ) )
  4749. return ADDITEM_OVERAMOUNT; // no slot
  4750. sd->cart.u.items_cart[i].amount += amount;
  4751. clif_cart_additem(sd,i,amount,0);
  4752. }
  4753. else
  4754. {// item not stackable or not present, add it
  4755. ARR_FIND( 0, MAX_CART, i, sd->cart.u.items_cart[i].nameid == 0 );
  4756. if( i == MAX_CART )
  4757. return ADDITEM_OVERAMOUNT; // no slot
  4758. memcpy(&sd->cart.u.items_cart[i],item,sizeof(sd->cart.u.items_cart[0]));
  4759. sd->cart.u.items_cart[i].id = 0;
  4760. sd->cart.u.items_cart[i].amount = amount;
  4761. sd->cart_num++;
  4762. clif_cart_additem(sd,i,amount,0);
  4763. }
  4764. sd->cart.u.items_cart[i].favorite = 0; // clear
  4765. sd->cart.u.items_cart[i].equipSwitch = 0;
  4766. log_pick_pc(sd, log_type, amount, &sd->cart.u.items_cart[i]);
  4767. sd->cart_weight += w;
  4768. clif_updatestatus(sd,SP_CARTINFO);
  4769. return ADDITEM_SUCCESS;
  4770. }
  4771. /*==========================================
  4772. * Delete item on cart for given index.
  4773. *------------------------------------------*/
  4774. void pc_cart_delitem(struct map_session_data *sd,int n,int amount,int type,e_log_pick_type log_type)
  4775. {
  4776. nullpo_retv(sd);
  4777. if(sd->cart.u.items_cart[n].nameid == 0 ||
  4778. sd->cart.u.items_cart[n].amount < amount)
  4779. return;
  4780. log_pick_pc(sd, log_type, -amount, &sd->cart.u.items_cart[n]);
  4781. sd->cart.u.items_cart[n].amount -= amount;
  4782. sd->cart_weight -= itemdb_weight(sd->cart.u.items_cart[n].nameid) * amount;
  4783. if(sd->cart.u.items_cart[n].amount <= 0) {
  4784. memset(&sd->cart.u.items_cart[n],0,sizeof(sd->cart.u.items_cart[0]));
  4785. sd->cart_num--;
  4786. }
  4787. if(!type) {
  4788. clif_cart_delitem(sd,n,amount);
  4789. clif_updatestatus(sd,SP_CARTINFO);
  4790. }
  4791. }
  4792. /*==========================================
  4793. * Transfer item from inventory to cart.
  4794. *------------------------------------------*/
  4795. void pc_putitemtocart(struct map_session_data *sd,int idx,int amount)
  4796. {
  4797. nullpo_retv(sd);
  4798. if (idx < 0 || idx >= MAX_INVENTORY) //Invalid index check [Skotlex]
  4799. return;
  4800. struct item *item_data = &sd->inventory.u.items_inventory[idx];
  4801. if( item_data->nameid == 0 || amount < 1 || item_data->amount < amount || sd->state.vending || sd->state.prevend )
  4802. return;
  4803. if( item_data->equipSwitch ){
  4804. clif_msg( sd, C_ITEM_EQUIP_SWITCH );
  4805. return;
  4806. }
  4807. enum e_additem_result flag = pc_cart_additem(sd,item_data,amount,LOG_TYPE_NONE);
  4808. if (flag == ADDITEM_SUCCESS)
  4809. pc_delitem(sd,idx,amount,0,5,LOG_TYPE_NONE);
  4810. else {
  4811. clif_cart_additem_ack(sd, (flag == ADDITEM_OVERAMOUNT) ? ADDITEM_TO_CART_FAIL_COUNT : ADDITEM_TO_CART_FAIL_WEIGHT);
  4812. clif_additem(sd, idx, amount, 0);
  4813. clif_delitem(sd, idx, amount, 0);
  4814. }
  4815. }
  4816. /*==========================================
  4817. * Get number of item in cart.
  4818. * Return:
  4819. -1 = itemid not found or no amount found
  4820. x = remaining itemid on cart after get
  4821. *------------------------------------------*/
  4822. int pc_cartitem_amount(struct map_session_data* sd, int idx, int amount)
  4823. {
  4824. struct item* item_data;
  4825. nullpo_retr(-1, sd);
  4826. item_data = &sd->cart.u.items_cart[idx];
  4827. if( item_data->nameid == 0 || item_data->amount == 0 )
  4828. return -1;
  4829. return item_data->amount - amount;
  4830. }
  4831. /*==========================================
  4832. * Retrieve an item at index idx from cart.
  4833. *------------------------------------------*/
  4834. void pc_getitemfromcart(struct map_session_data *sd,int idx,int amount)
  4835. {
  4836. nullpo_retv(sd);
  4837. if (idx < 0 || idx >= MAX_CART) //Invalid index check [Skotlex]
  4838. return;
  4839. struct item *item_data=&sd->cart.u.items_cart[idx];
  4840. if (item_data->nameid == 0 || amount < 1 || item_data->amount < amount || sd->state.vending || sd->state.prevend)
  4841. return;
  4842. enum e_additem_result flag = pc_additem(sd, item_data, amount, LOG_TYPE_NONE);
  4843. if (flag == ADDITEM_SUCCESS)
  4844. pc_cart_delitem(sd, idx, amount, 0, LOG_TYPE_NONE);
  4845. else {
  4846. clif_cart_delitem(sd, idx, amount);
  4847. clif_additem(sd, idx, amount, flag);
  4848. clif_cart_additem(sd, idx, amount, 0);
  4849. }
  4850. }
  4851. /*==========================================
  4852. * Bound Item Check
  4853. * Type:
  4854. * 1 Account Bound
  4855. * 2 Guild Bound
  4856. * 3 Party Bound
  4857. * 4 Character Bound
  4858. *------------------------------------------*/
  4859. int pc_bound_chk(TBL_PC *sd,enum bound_type type,int *idxlist)
  4860. {
  4861. int i = 0, j = 0;
  4862. for(i = 0; i < MAX_INVENTORY; i++) {
  4863. 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) {
  4864. idxlist[j] = i;
  4865. j++;
  4866. }
  4867. }
  4868. return j;
  4869. }
  4870. /*==========================================
  4871. * Display item stolen msg to player sd
  4872. *------------------------------------------*/
  4873. int pc_show_steal(struct block_list *bl,va_list ap)
  4874. {
  4875. struct map_session_data *sd;
  4876. int itemid;
  4877. struct item_data *item=NULL;
  4878. char output[100];
  4879. sd=va_arg(ap,struct map_session_data *);
  4880. itemid=va_arg(ap,int);
  4881. if((item=itemdb_exists(itemid))==NULL)
  4882. sprintf(output,"%s stole an Unknown Item (id: %i).",sd->status.name, itemid);
  4883. else
  4884. sprintf(output,"%s stole %s.",sd->status.name,item->jname);
  4885. clif_displaymessage( ((struct map_session_data *)bl)->fd, output);
  4886. return 0;
  4887. }
  4888. /**
  4889. * Steal an item from bl (mob).
  4890. * @param sd: Player data
  4891. * @param bl: Object to steal from
  4892. * @param skill_lv: Level of skill used
  4893. * @return True on success or false otherwise
  4894. */
  4895. bool pc_steal_item(struct map_session_data *sd,struct block_list *bl, uint16 skill_lv)
  4896. {
  4897. int i,itemid;
  4898. double rate;
  4899. unsigned char flag = 0;
  4900. struct status_data *sd_status, *md_status;
  4901. struct mob_data *md;
  4902. struct item tmp_item;
  4903. if(!sd || !bl || bl->type!=BL_MOB)
  4904. return false;
  4905. md = (TBL_MOB *)bl;
  4906. if(md->state.steal_flag == UCHAR_MAX || ( md->sc.opt1 && md->sc.opt1 != OPT1_BURNING ) ) //already stolen from / status change check
  4907. return false;
  4908. sd_status= status_get_status_data(&sd->bl);
  4909. md_status= status_get_status_data(bl);
  4910. if (md->master_id || status_has_mode(md_status, MD_STATUS_IMMUNE) || status_get_race2(&md->bl) == RC2_TREASURE ||
  4911. map_getmapflag(bl->m, MF_NOMOBLOOT) || // check noloot map flag [Lorky]
  4912. (battle_config.skill_steal_max_tries && //Reached limit of steal attempts. [Lupus]
  4913. md->state.steal_flag++ >= battle_config.skill_steal_max_tries)
  4914. ) { //Can't steal from
  4915. md->state.steal_flag = UCHAR_MAX;
  4916. return false;
  4917. }
  4918. // base skill success chance (percentual)
  4919. rate = (sd_status->dex - md_status->dex)/2 + skill_lv*6 + 4;
  4920. rate += sd->bonus.add_steal_rate;
  4921. if( rate < 1
  4922. #ifdef RENEWAL
  4923. || rnd()%100 >= rate
  4924. #endif
  4925. )
  4926. return false;
  4927. // Try dropping one item, in the order from first to last possible slot.
  4928. // Droprate is affected by the skill success rate.
  4929. for( i = 0; i < MAX_STEAL_DROP; i++ )
  4930. 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
  4931. #ifndef RENEWAL
  4932. * rate/100.
  4933. #endif
  4934. )
  4935. break;
  4936. if( i == MAX_STEAL_DROP )
  4937. return false;
  4938. itemid = md->db->dropitem[i].nameid;
  4939. memset(&tmp_item,0,sizeof(tmp_item));
  4940. tmp_item.nameid = itemid;
  4941. tmp_item.amount = 1;
  4942. tmp_item.identify = itemdb_isidentified(itemid);
  4943. mob_setdropitem_option(&tmp_item, &md->db->dropitem[i]);
  4944. flag = pc_additem(sd,&tmp_item,1,LOG_TYPE_PICKDROP_PLAYER);
  4945. //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?
  4946. md->state.steal_flag = UCHAR_MAX; //you can't steal from this mob any more
  4947. if(flag) { //Failed to steal due to overweight
  4948. clif_additem(sd,0,0,flag);
  4949. return false;
  4950. }
  4951. if(battle_config.show_steal_in_same_party)
  4952. party_foreachsamemap(pc_show_steal,sd,AREA_SIZE,sd,tmp_item.nameid);
  4953. //Logs items, Stolen from mobs [Lupus]
  4954. log_pick_mob(md, LOG_TYPE_STEAL, -1, &tmp_item);
  4955. //A Rare Steal Global Announce by Lupus
  4956. if(md->db->dropitem[i].p<=battle_config.rare_drop_announce) {
  4957. struct item_data *i_data;
  4958. char message[128];
  4959. i_data = itemdb_search(itemid);
  4960. 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);
  4961. //MSG: "'%s' stole %s's %s (chance: %0.02f%%)"
  4962. intif_broadcast(message, strlen(message) + 1, BC_DEFAULT);
  4963. }
  4964. return true;
  4965. }
  4966. /*==========================================
  4967. * Stole zeny from bl (mob)
  4968. * return
  4969. * 0 = fail
  4970. * 1 = success
  4971. *------------------------------------------*/
  4972. int pc_steal_coin(struct map_session_data *sd,struct block_list *target)
  4973. {
  4974. int rate, target_lv;
  4975. struct mob_data *md;
  4976. if(!sd || !target || target->type != BL_MOB)
  4977. return 0;
  4978. md = (TBL_MOB*)target;
  4979. target_lv = status_get_lv(target);
  4980. 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)
  4981. return 0;
  4982. rate = sd->battle_status.dex / 2 + 2 * (sd->status.base_level - target_lv) + (10 * pc_checkskill(sd, RG_STEALCOIN)) + sd->battle_status.luk / 2;
  4983. if(rnd()%1000 < rate)
  4984. {
  4985. // Zeny Steal Amount: (rnd() % (10 * target_lv + 1 - 8 * target_lv)) + 8 * target_lv
  4986. int amount = (rnd() % (2 * target_lv + 1)) + 8 * target_lv; // Reduced formula
  4987. pc_getzeny(sd, amount, LOG_TYPE_STEAL, NULL);
  4988. md->state.steal_coin_flag = 1;
  4989. return 1;
  4990. }
  4991. return 0;
  4992. }
  4993. /*==========================================
  4994. * Set's a player position.
  4995. * @param sd
  4996. * @param mapindex
  4997. * @param x
  4998. * @param y
  4999. * @param clrtype
  5000. * @return SETPOS_OK Success
  5001. * SETPOS_MAPINDEX Invalid map index
  5002. * SETPOS_NO_MAPSERVER Map not in this map-server, and failed to locate alternate map-server.
  5003. * SETPOS_AUTOTRADE Player is in autotrade state
  5004. *------------------------------------------*/
  5005. enum e_setpos pc_setpos(struct map_session_data* sd, unsigned short mapindex, int x, int y, clr_type clrtype)
  5006. {
  5007. nullpo_retr(SETPOS_OK,sd);
  5008. if( !mapindex || !mapindex_id2name(mapindex) ) {
  5009. ShowDebug("pc_setpos: Passed mapindex(%d) is invalid!\n", mapindex);
  5010. return SETPOS_MAPINDEX;
  5011. }
  5012. if ( sd->state.autotrade && (sd->vender_id || sd->buyer_id) ) // Player with autotrade just causes clif glitch! @ FIXME
  5013. return SETPOS_AUTOTRADE;
  5014. if( battle_config.revive_onwarp && pc_isdead(sd) ) { //Revive dead people before warping them
  5015. pc_setstand(sd, true);
  5016. pc_setrestartvalue(sd,1);
  5017. }
  5018. int16 m = map_mapindex2mapid(mapindex);
  5019. struct map_data *mapdata = map_getmapdata(m);
  5020. sd->state.changemap = (sd->mapindex != mapindex);
  5021. sd->state.warping = 1;
  5022. sd->state.workinprogress = WIP_DISABLE_NONE;
  5023. if( sd->state.changemap ) { // Misc map-changing settings
  5024. unsigned short curr_map_instance_id = map_getmapdata(sd->bl.m)->instance_id, new_map_instance_id = (mapdata ? mapdata->instance_id : 0);
  5025. if (curr_map_instance_id != new_map_instance_id) {
  5026. if (curr_map_instance_id) // Update instance timer for the map on leave
  5027. instance_delusers(curr_map_instance_id);
  5028. if (new_map_instance_id) // Update instance timer for the map on enter
  5029. instance_addusers(new_map_instance_id);
  5030. }
  5031. sd->state.pmap = sd->bl.m;
  5032. if (sd->sc.count) { // Cancel some map related stuff.
  5033. if (sd->sc.data[SC_JAILED])
  5034. return SETPOS_MAPINDEX; //You may not get out!
  5035. status_change_end(&sd->bl, SC_BOSSMAPINFO, INVALID_TIMER);
  5036. status_change_end(&sd->bl, SC_WARM, INVALID_TIMER);
  5037. status_change_end(&sd->bl, SC_SUN_COMFORT, INVALID_TIMER);
  5038. status_change_end(&sd->bl, SC_MOON_COMFORT, INVALID_TIMER);
  5039. status_change_end(&sd->bl, SC_STAR_COMFORT, INVALID_TIMER);
  5040. status_change_end(&sd->bl, SC_MIRACLE, INVALID_TIMER);
  5041. if (sd->sc.data[SC_KNOWLEDGE]) {
  5042. struct status_change_entry *sce = sd->sc.data[SC_KNOWLEDGE];
  5043. if (sce->timer != INVALID_TIMER)
  5044. delete_timer(sce->timer, status_change_timer);
  5045. sce->timer = add_timer(gettick() + skill_get_time(SG_KNOWLEDGE, sce->val1), status_change_timer, sd->bl.id, SC_KNOWLEDGE);
  5046. }
  5047. status_change_end(&sd->bl, SC_PROPERTYWALK, INVALID_TIMER);
  5048. status_change_end(&sd->bl, SC_CLOAKING, INVALID_TIMER);
  5049. status_change_end(&sd->bl, SC_CLOAKINGEXCEED, INVALID_TIMER);
  5050. }
  5051. for(int i = 0; i < EQI_MAX; i++ ) {
  5052. if( sd->equip_index[i] >= 0 )
  5053. if( pc_isequip(sd,sd->equip_index[i]) )
  5054. pc_unequipitem(sd,sd->equip_index[i],2);
  5055. }
  5056. if (battle_config.clear_unit_onwarp&BL_PC)
  5057. skill_clear_unitgroup(&sd->bl);
  5058. party_send_dot_remove(sd); //minimap dot fix [Kevin]
  5059. guild_send_dot_remove(sd);
  5060. bg_send_dot_remove(sd);
  5061. if (sd->regen.state.gc)
  5062. sd->regen.state.gc = 0;
  5063. // make sure vending is allowed here
  5064. if (sd->state.vending && mapdata->flag[MF_NOVENDING]) {
  5065. clif_displaymessage (sd->fd, msg_txt(sd,276)); // "You can't open a shop on this map"
  5066. vending_closevending(sd);
  5067. }
  5068. channel_pcquit(sd,4); //quit map chan
  5069. }
  5070. if( m < 0 )
  5071. {
  5072. uint32 ip;
  5073. uint16 port;
  5074. struct script_state *st;
  5075. //if can't find any map-servers, just abort setting position.
  5076. if(!sd->mapindex || map_mapname2ipport(mapindex,&ip,&port))
  5077. return SETPOS_NO_MAPSERVER;
  5078. if (sd->npc_id){
  5079. npc_event_dequeue(sd,false);
  5080. st = sd->st;
  5081. }else{
  5082. st = nullptr;
  5083. }
  5084. npc_script_event(sd, NPCE_LOGOUT);
  5085. //remove from map, THEN change x/y coordinates
  5086. unit_remove_map_pc(sd,clrtype);
  5087. sd->mapindex = mapindex;
  5088. sd->bl.x=x;
  5089. sd->bl.y=y;
  5090. pc_clean_skilltree(sd);
  5091. chrif_save(sd, CSAVE_CHANGE_MAPSERV|CSAVE_INVENTORY|CSAVE_CART);
  5092. chrif_changemapserver(sd, ip, (short)port);
  5093. //Free session data from this map server [Kevin]
  5094. unit_free_pc(sd);
  5095. if( st ){
  5096. // Has to be done here, because otherwise unit_free_pc will free the stack already
  5097. st->state = END;
  5098. }
  5099. return SETPOS_OK;
  5100. }
  5101. if( x < 0 || x >= mapdata->xs || y < 0 || y >= mapdata->ys )
  5102. {
  5103. 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);
  5104. x = y = 0; // make it random
  5105. }
  5106. if( x == 0 && y == 0 ) { // pick a random walkable cell
  5107. int c=0;
  5108. do {
  5109. x = rnd()%(mapdata->xs-2)+1;
  5110. y = rnd()%(mapdata->ys-2)+1;
  5111. c++;
  5112. if(c > (mapdata->xs * mapdata->ys)*3){ //force out
  5113. 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));
  5114. return SETPOS_OK; //preventing warp
  5115. //break; //allow warp anyway
  5116. }
  5117. } while(map_getcell(m,x,y,CELL_CHKNOPASS) || (!battle_config.teleport_on_portal && npc_check_areanpc(1,m,x,y,1)));
  5118. }
  5119. if (sd->state.vending && map_getcell(m,x,y,CELL_CHKNOVENDING)) {
  5120. clif_displaymessage (sd->fd, msg_txt(sd,204)); // "You can't open a shop on this cell."
  5121. vending_closevending(sd);
  5122. }
  5123. if(sd->bl.prev != NULL){
  5124. unit_remove_map_pc(sd,clrtype);
  5125. clif_changemap(sd,m,x,y); // [MouseJstr]
  5126. } else if(sd->state.active) //Tag player for rewarping after map-loading is done. [Skotlex]
  5127. sd->state.rewarp = 1;
  5128. sd->mapindex = mapindex;
  5129. sd->bl.m = m;
  5130. sd->bl.x = sd->ud.to_x = x;
  5131. sd->bl.y = sd->ud.to_y = y;
  5132. if( sd->status.guild_id > 0 && mapdata->flag[MF_GVG_CASTLE] )
  5133. { // Increased guild castle regen [Valaris]
  5134. struct guild_castle *gc = guild_mapindex2gc(sd->mapindex);
  5135. if(gc && gc->guild_id == sd->status.guild_id)
  5136. sd->regen.state.gc = 1;
  5137. }
  5138. if( sd->status.pet_id > 0 && sd->pd && sd->pd->pet.intimate > 0 )
  5139. {
  5140. sd->pd->bl.m = m;
  5141. sd->pd->bl.x = sd->pd->ud.to_x = x;
  5142. sd->pd->bl.y = sd->pd->ud.to_y = y;
  5143. sd->pd->ud.dir = sd->ud.dir;
  5144. }
  5145. if( hom_is_active(sd->hd) )
  5146. {
  5147. sd->hd->bl.m = m;
  5148. sd->hd->bl.x = sd->hd->ud.to_x = x;
  5149. sd->hd->bl.y = sd->hd->ud.to_y = y;
  5150. sd->hd->ud.dir = sd->ud.dir;
  5151. }
  5152. if( sd->md )
  5153. {
  5154. sd->md->bl.m = m;
  5155. sd->md->bl.x = sd->md->ud.to_x = x;
  5156. sd->md->bl.y = sd->md->ud.to_y = y;
  5157. sd->md->ud.dir = sd->ud.dir;
  5158. }
  5159. if( sd->ed ) {
  5160. sd->ed->bl.m = m;
  5161. sd->ed->bl.x = sd->ed->ud.to_x = x;
  5162. sd->ed->bl.y = sd->ed->ud.to_y = y;
  5163. sd->ed->ud.dir = sd->ud.dir;
  5164. }
  5165. pc_cell_basilica(sd);
  5166. //check if we gonna be rewarped [lighta]
  5167. if(npc_check_areanpc(1,m,x,y,0)){
  5168. sd->count_rewarp++;
  5169. }
  5170. else
  5171. sd->count_rewarp = 0;
  5172. return SETPOS_OK;
  5173. }
  5174. /*==========================================
  5175. * Warp player sd to random location on current map.
  5176. * May fail if no walkable cell found (1000 attempts).
  5177. * Return:
  5178. * 0 = Success
  5179. * 1,2,3 = Fail
  5180. *------------------------------------------*/
  5181. char pc_randomwarp(struct map_session_data *sd, clr_type type)
  5182. {
  5183. int x,y,i=0;
  5184. nullpo_ret(sd);
  5185. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  5186. if (mapdata->flag[MF_NOTELEPORT]) //Teleport forbidden
  5187. return 3;
  5188. do {
  5189. x = rnd()%(mapdata->xs-2)+1;
  5190. y = rnd()%(mapdata->ys-2)+1;
  5191. } 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);
  5192. if (i < 1000)
  5193. return pc_setpos(sd,mapdata->index,x,y,type);
  5194. return 3;
  5195. }
  5196. /*==========================================
  5197. * Records a memo point at sd's current position
  5198. * pos - entry to replace, (-1: shift oldest entry out)
  5199. *------------------------------------------*/
  5200. bool pc_memo(struct map_session_data* sd, int pos)
  5201. {
  5202. int skill;
  5203. nullpo_ret(sd);
  5204. // check mapflags
  5205. 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) ) {
  5206. clif_skill_teleportmessage(sd, 1); // "Saved point cannot be memorized."
  5207. return false;
  5208. }
  5209. // check inputs
  5210. if( pos < -1 || pos >= MAX_MEMOPOINTS )
  5211. return false; // invalid input
  5212. // check required skill level
  5213. skill = pc_checkskill(sd, AL_WARP);
  5214. if( skill < 1 ) {
  5215. clif_skill_memomessage(sd,2); // "You haven't learned Warp."
  5216. return false;
  5217. }
  5218. if( skill < 2 || skill - 2 < pos ) {
  5219. clif_skill_memomessage(sd,1); // "Skill Level is not high enough."
  5220. return false;
  5221. }
  5222. if( pos == -1 )
  5223. {
  5224. uint8 i;
  5225. // prevent memo-ing the same map multiple times
  5226. ARR_FIND( 0, MAX_MEMOPOINTS, i, sd->status.memo_point[i].map == map_id2index(sd->bl.m) );
  5227. memmove(&sd->status.memo_point[1], &sd->status.memo_point[0], (u8min(i,MAX_MEMOPOINTS-1))*sizeof(struct point));
  5228. pos = 0;
  5229. }
  5230. if( map_getmapdata(sd->bl.m)->instance_id ) {
  5231. clif_displaymessage( sd->fd, msg_txt(sd,384) ); // You cannot create a memo in an instance.
  5232. return false;
  5233. }
  5234. sd->status.memo_point[pos].map = map_id2index(sd->bl.m);
  5235. sd->status.memo_point[pos].x = sd->bl.x;
  5236. sd->status.memo_point[pos].y = sd->bl.y;
  5237. clif_skill_memomessage(sd, 0);
  5238. return true;
  5239. }
  5240. //
  5241. // Skills
  5242. //
  5243. /**
  5244. * Get the skill current cooldown for player.
  5245. * (get the db base cooldown for skill + player specific cooldown)
  5246. * @param sd : player pointer
  5247. * @param id : skill id
  5248. * @param lv : skill lv
  5249. * @return player skill cooldown
  5250. */
  5251. int pc_get_skillcooldown(struct map_session_data *sd, uint16 skill_id, uint16 skill_lv) {
  5252. uint16 idx = skill_get_index(skill_id);
  5253. int cooldown = 0;
  5254. if (!idx) return 0;
  5255. if (skill_db[idx]->cooldown[skill_lv - 1])
  5256. cooldown = skill_db[idx]->cooldown[skill_lv - 1];
  5257. if (skill_id == SU_TUNABELLY && pc_checkskill(sd, SU_SPIRITOFSEA))
  5258. cooldown -= skill_get_time(SU_TUNABELLY, skill_lv);
  5259. for (auto &it : sd->skillcooldown) {
  5260. if (it.id == skill_id) {
  5261. cooldown += it.val;
  5262. cooldown = max(0, cooldown);
  5263. break;
  5264. }
  5265. }
  5266. return cooldown;
  5267. }
  5268. /*==========================================
  5269. * Return player sd skill_lv learned for given skill
  5270. *------------------------------------------*/
  5271. uint8 pc_checkskill(struct map_session_data *sd, uint16 skill_id)
  5272. {
  5273. uint16 idx = 0;
  5274. if (sd == NULL)
  5275. return 0;
  5276. if ((idx = skill_get_index(skill_id)) == 0) {
  5277. ShowError("pc_checkskill: Invalid skill id %d (char_id=%d).\n", skill_id, sd->status.char_id);
  5278. return 0;
  5279. }
  5280. if (SKILL_CHK_GUILD(skill_id) ) {
  5281. struct guild *g;
  5282. if( sd->status.guild_id>0 && (g=sd->guild)!=NULL)
  5283. return guild_checkskill(g,skill_id);
  5284. return 0;
  5285. }
  5286. return (sd->status.skill[idx].id == skill_id) ? sd->status.skill[idx].lv : 0;
  5287. }
  5288. /**
  5289. * Check if we still have the correct weapon to continue the skill (actually status)
  5290. * If not ending it
  5291. * @param sd
  5292. * @return 0:error, 1:check done
  5293. */
  5294. static void pc_checkallowskill(struct map_session_data *sd)
  5295. {
  5296. const enum sc_type scw_list[] = {
  5297. SC_TWOHANDQUICKEN,
  5298. SC_ONEHAND,
  5299. SC_AURABLADE,
  5300. SC_PARRYING,
  5301. SC_SPEARQUICKEN,
  5302. SC_ADRENALINE,
  5303. SC_ADRENALINE2,
  5304. SC_DANCING,
  5305. SC_GATLINGFEVER,
  5306. };
  5307. uint8 i;
  5308. nullpo_retv(sd);
  5309. if(!sd->sc.count)
  5310. return;
  5311. for (i = 0; i < ARRAYLENGTH(scw_list); i++)
  5312. { // Skills requiring specific weapon types
  5313. if( scw_list[i] == SC_DANCING && !battle_config.dancing_weaponswitch_fix )
  5314. continue;
  5315. if(sd->sc.data[scw_list[i]] &&
  5316. !pc_check_weapontype(sd,skill_get_weapontype(status_sc2skill(scw_list[i]))))
  5317. status_change_end(&sd->bl, scw_list[i], INVALID_TIMER);
  5318. }
  5319. if(sd->sc.data[SC_SPURT] && sd->status.weapon)
  5320. // Spurt requires bare hands (feet, in fact xD)
  5321. status_change_end(&sd->bl, SC_SPURT, INVALID_TIMER);
  5322. if(sd->status.shield <= 0) { // Skills requiring a shield
  5323. const enum sc_type scs_list[] = {
  5324. SC_AUTOGUARD,
  5325. SC_DEFENDER,
  5326. SC_REFLECTSHIELD,
  5327. SC_REFLECTDAMAGE
  5328. };
  5329. for (i = 0; i < ARRAYLENGTH(scs_list); i++)
  5330. if(sd->sc.data[scs_list[i]])
  5331. status_change_end(&sd->bl, scs_list[i], INVALID_TIMER);
  5332. }
  5333. }
  5334. /*==========================================
  5335. * Return equipped index of item on player sd at pos
  5336. * Return
  5337. * -1 : Nothing equipped
  5338. * idx : (this index could be used in inventory to found item_data)
  5339. *------------------------------------------*/
  5340. short pc_checkequip(struct map_session_data *sd,int pos, bool checkall)
  5341. {
  5342. uint8 i;
  5343. nullpo_retr(-1, sd);
  5344. for(i=0;i<EQI_MAX;i++){
  5345. if(pos & equip_bitmask[i]){
  5346. if( checkall && ( pos&~equip_bitmask[i] ) != 0 && sd->equip_index[i] == -1 ){
  5347. // Check all if any match is found
  5348. continue;
  5349. }
  5350. return sd->equip_index[i];
  5351. }
  5352. }
  5353. return -1;
  5354. }
  5355. /*==========================================
  5356. * Check if sd has nameid equipped somewhere
  5357. * @sd : the player session
  5358. * @nameid : id of the item to check
  5359. * @min : : see pc.hpp enum equip_index from ? to @max
  5360. * @max : see pc.hpp enum equip_index for @min to ?
  5361. * -return true,false
  5362. *------------------------------------------*/
  5363. bool pc_checkequip2(struct map_session_data *sd, unsigned short nameid, int min, int max)
  5364. {
  5365. int i;
  5366. for(i = min; i < max; i++) {
  5367. if(equip_bitmask[i]) {
  5368. int idx = sd->equip_index[i];
  5369. if (sd->inventory.u.items_inventory[idx].nameid == nameid)
  5370. return true;
  5371. }
  5372. }
  5373. return false;
  5374. }
  5375. /*==========================================
  5376. * Convert's from the client's lame Job ID system
  5377. * to the map server's 'makes sense' system. [Skotlex]
  5378. *------------------------------------------*/
  5379. int pc_jobid2mapid(unsigned short b_class)
  5380. {
  5381. switch(b_class)
  5382. {
  5383. //Novice And 1-1 Jobs
  5384. case JOB_NOVICE: return MAPID_NOVICE;
  5385. case JOB_SWORDMAN: return MAPID_SWORDMAN;
  5386. case JOB_MAGE: return MAPID_MAGE;
  5387. case JOB_ARCHER: return MAPID_ARCHER;
  5388. case JOB_ACOLYTE: return MAPID_ACOLYTE;
  5389. case JOB_MERCHANT: return MAPID_MERCHANT;
  5390. case JOB_THIEF: return MAPID_THIEF;
  5391. case JOB_TAEKWON: return MAPID_TAEKWON;
  5392. case JOB_WEDDING: return MAPID_WEDDING;
  5393. case JOB_GUNSLINGER: return MAPID_GUNSLINGER;
  5394. case JOB_NINJA: return MAPID_NINJA;
  5395. case JOB_XMAS: return MAPID_XMAS;
  5396. case JOB_SUMMER: return MAPID_SUMMER;
  5397. case JOB_HANBOK: return MAPID_HANBOK;
  5398. case JOB_GANGSI: return MAPID_GANGSI;
  5399. case JOB_OKTOBERFEST: return MAPID_OKTOBERFEST;
  5400. case JOB_SUMMER2: return MAPID_SUMMER2;
  5401. //2-1 Jobs
  5402. case JOB_SUPER_NOVICE: return MAPID_SUPER_NOVICE;
  5403. case JOB_KNIGHT: return MAPID_KNIGHT;
  5404. case JOB_WIZARD: return MAPID_WIZARD;
  5405. case JOB_HUNTER: return MAPID_HUNTER;
  5406. case JOB_PRIEST: return MAPID_PRIEST;
  5407. case JOB_BLACKSMITH: return MAPID_BLACKSMITH;
  5408. case JOB_ASSASSIN: return MAPID_ASSASSIN;
  5409. case JOB_STAR_GLADIATOR: return MAPID_STAR_GLADIATOR;
  5410. case JOB_KAGEROU:
  5411. case JOB_OBORO: return MAPID_KAGEROUOBORO;
  5412. case JOB_REBELLION: return MAPID_REBELLION;
  5413. case JOB_DEATH_KNIGHT: return MAPID_DEATH_KNIGHT;
  5414. //2-2 Jobs
  5415. case JOB_CRUSADER: return MAPID_CRUSADER;
  5416. case JOB_SAGE: return MAPID_SAGE;
  5417. case JOB_BARD:
  5418. case JOB_DANCER: return MAPID_BARDDANCER;
  5419. case JOB_MONK: return MAPID_MONK;
  5420. case JOB_ALCHEMIST: return MAPID_ALCHEMIST;
  5421. case JOB_ROGUE: return MAPID_ROGUE;
  5422. case JOB_SOUL_LINKER: return MAPID_SOUL_LINKER;
  5423. case JOB_DARK_COLLECTOR: return MAPID_DARK_COLLECTOR;
  5424. //Trans Novice And Trans 1-1 Jobs
  5425. case JOB_NOVICE_HIGH: return MAPID_NOVICE_HIGH;
  5426. case JOB_SWORDMAN_HIGH: return MAPID_SWORDMAN_HIGH;
  5427. case JOB_MAGE_HIGH: return MAPID_MAGE_HIGH;
  5428. case JOB_ARCHER_HIGH: return MAPID_ARCHER_HIGH;
  5429. case JOB_ACOLYTE_HIGH: return MAPID_ACOLYTE_HIGH;
  5430. case JOB_MERCHANT_HIGH: return MAPID_MERCHANT_HIGH;
  5431. case JOB_THIEF_HIGH: return MAPID_THIEF_HIGH;
  5432. //Trans 2-1 Jobs
  5433. case JOB_LORD_KNIGHT: return MAPID_LORD_KNIGHT;
  5434. case JOB_HIGH_WIZARD: return MAPID_HIGH_WIZARD;
  5435. case JOB_SNIPER: return MAPID_SNIPER;
  5436. case JOB_HIGH_PRIEST: return MAPID_HIGH_PRIEST;
  5437. case JOB_WHITESMITH: return MAPID_WHITESMITH;
  5438. case JOB_ASSASSIN_CROSS: return MAPID_ASSASSIN_CROSS;
  5439. //Trans 2-2 Jobs
  5440. case JOB_PALADIN: return MAPID_PALADIN;
  5441. case JOB_PROFESSOR: return MAPID_PROFESSOR;
  5442. case JOB_CLOWN:
  5443. case JOB_GYPSY: return MAPID_CLOWNGYPSY;
  5444. case JOB_CHAMPION: return MAPID_CHAMPION;
  5445. case JOB_CREATOR: return MAPID_CREATOR;
  5446. case JOB_STALKER: return MAPID_STALKER;
  5447. //Baby Novice And Baby 1-1 Jobs
  5448. case JOB_BABY: return MAPID_BABY;
  5449. case JOB_BABY_SWORDMAN: return MAPID_BABY_SWORDMAN;
  5450. case JOB_BABY_MAGE: return MAPID_BABY_MAGE;
  5451. case JOB_BABY_ARCHER: return MAPID_BABY_ARCHER;
  5452. case JOB_BABY_ACOLYTE: return MAPID_BABY_ACOLYTE;
  5453. case JOB_BABY_MERCHANT: return MAPID_BABY_MERCHANT;
  5454. case JOB_BABY_THIEF: return MAPID_BABY_THIEF;
  5455. case JOB_BABY_TAEKWON: return MAPID_BABY_TAEKWON;
  5456. case JOB_BABY_GUNSLINGER: return MAPID_BABY_GUNSLINGER;
  5457. case JOB_BABY_NINJA: return MAPID_BABY_NINJA;
  5458. case JOB_BABY_SUMMONER: return MAPID_BABY_SUMMONER;
  5459. //Baby 2-1 Jobs
  5460. case JOB_SUPER_BABY: return MAPID_SUPER_BABY;
  5461. case JOB_BABY_KNIGHT: return MAPID_BABY_KNIGHT;
  5462. case JOB_BABY_WIZARD: return MAPID_BABY_WIZARD;
  5463. case JOB_BABY_HUNTER: return MAPID_BABY_HUNTER;
  5464. case JOB_BABY_PRIEST: return MAPID_BABY_PRIEST;
  5465. case JOB_BABY_BLACKSMITH: return MAPID_BABY_BLACKSMITH;
  5466. case JOB_BABY_ASSASSIN: return MAPID_BABY_ASSASSIN;
  5467. case JOB_BABY_STAR_GLADIATOR: return MAPID_BABY_STAR_GLADIATOR;
  5468. case JOB_BABY_REBELLION: return MAPID_BABY_REBELLION;
  5469. case JOB_BABY_KAGEROU:
  5470. case JOB_BABY_OBORO: return MAPID_BABY_KAGEROUOBORO;
  5471. //Baby 2-2 Jobs
  5472. case JOB_BABY_CRUSADER: return MAPID_BABY_CRUSADER;
  5473. case JOB_BABY_SAGE: return MAPID_BABY_SAGE;
  5474. case JOB_BABY_BARD:
  5475. case JOB_BABY_DANCER: return MAPID_BABY_BARDDANCER;
  5476. case JOB_BABY_MONK: return MAPID_BABY_MONK;
  5477. case JOB_BABY_ALCHEMIST: return MAPID_BABY_ALCHEMIST;
  5478. case JOB_BABY_ROGUE: return MAPID_BABY_ROGUE;
  5479. case JOB_BABY_SOUL_LINKER: return MAPID_BABY_SOUL_LINKER;
  5480. //3-1 Jobs
  5481. case JOB_SUPER_NOVICE_E: return MAPID_SUPER_NOVICE_E;
  5482. case JOB_RUNE_KNIGHT: return MAPID_RUNE_KNIGHT;
  5483. case JOB_WARLOCK: return MAPID_WARLOCK;
  5484. case JOB_RANGER: return MAPID_RANGER;
  5485. case JOB_ARCH_BISHOP: return MAPID_ARCH_BISHOP;
  5486. case JOB_MECHANIC: return MAPID_MECHANIC;
  5487. case JOB_GUILLOTINE_CROSS: return MAPID_GUILLOTINE_CROSS;
  5488. case JOB_STAR_EMPEROR: return MAPID_STAR_EMPEROR;
  5489. //3-2 Jobs
  5490. case JOB_ROYAL_GUARD: return MAPID_ROYAL_GUARD;
  5491. case JOB_SORCERER: return MAPID_SORCERER;
  5492. case JOB_MINSTREL:
  5493. case JOB_WANDERER: return MAPID_MINSTRELWANDERER;
  5494. case JOB_SURA: return MAPID_SURA;
  5495. case JOB_GENETIC: return MAPID_GENETIC;
  5496. case JOB_SHADOW_CHASER: return MAPID_SHADOW_CHASER;
  5497. case JOB_SOUL_REAPER: return MAPID_SOUL_REAPER;
  5498. //Trans 3-1 Jobs
  5499. case JOB_RUNE_KNIGHT_T: return MAPID_RUNE_KNIGHT_T;
  5500. case JOB_WARLOCK_T: return MAPID_WARLOCK_T;
  5501. case JOB_RANGER_T: return MAPID_RANGER_T;
  5502. case JOB_ARCH_BISHOP_T: return MAPID_ARCH_BISHOP_T;
  5503. case JOB_MECHANIC_T: return MAPID_MECHANIC_T;
  5504. case JOB_GUILLOTINE_CROSS_T: return MAPID_GUILLOTINE_CROSS_T;
  5505. //Trans 3-2 Jobs
  5506. case JOB_ROYAL_GUARD_T: return MAPID_ROYAL_GUARD_T;
  5507. case JOB_SORCERER_T: return MAPID_SORCERER_T;
  5508. case JOB_MINSTREL_T:
  5509. case JOB_WANDERER_T: return MAPID_MINSTRELWANDERER_T;
  5510. case JOB_SURA_T: return MAPID_SURA_T;
  5511. case JOB_GENETIC_T: return MAPID_GENETIC_T;
  5512. case JOB_SHADOW_CHASER_T: return MAPID_SHADOW_CHASER_T;
  5513. //Baby 3-1 Jobs
  5514. case JOB_SUPER_BABY_E: return MAPID_SUPER_BABY_E;
  5515. case JOB_BABY_RUNE: return MAPID_BABY_RUNE;
  5516. case JOB_BABY_WARLOCK: return MAPID_BABY_WARLOCK;
  5517. case JOB_BABY_RANGER: return MAPID_BABY_RANGER;
  5518. case JOB_BABY_BISHOP: return MAPID_BABY_BISHOP;
  5519. case JOB_BABY_MECHANIC: return MAPID_BABY_MECHANIC;
  5520. case JOB_BABY_CROSS: return MAPID_BABY_CROSS;
  5521. case JOB_BABY_STAR_EMPEROR: return MAPID_BABY_STAR_EMPEROR;
  5522. //Baby 3-2 Jobs
  5523. case JOB_BABY_GUARD: return MAPID_BABY_GUARD;
  5524. case JOB_BABY_SORCERER: return MAPID_BABY_SORCERER;
  5525. case JOB_BABY_MINSTREL:
  5526. case JOB_BABY_WANDERER: return MAPID_BABY_MINSTRELWANDERER;
  5527. case JOB_BABY_SURA: return MAPID_BABY_SURA;
  5528. case JOB_BABY_GENETIC: return MAPID_BABY_GENETIC;
  5529. case JOB_BABY_CHASER: return MAPID_BABY_CHASER;
  5530. case JOB_BABY_SOUL_REAPER: return MAPID_BABY_SOUL_REAPER;
  5531. //Doram Jobs
  5532. case JOB_SUMMONER: return MAPID_SUMMONER;
  5533. default:
  5534. return -1;
  5535. }
  5536. }
  5537. //Reverts the map-style class id to the client-style one.
  5538. int pc_mapid2jobid(unsigned short class_, int sex)
  5539. {
  5540. switch(class_) {
  5541. //Novice And 1-1 Jobs
  5542. case MAPID_NOVICE: return JOB_NOVICE;
  5543. case MAPID_SWORDMAN: return JOB_SWORDMAN;
  5544. case MAPID_MAGE: return JOB_MAGE;
  5545. case MAPID_ARCHER: return JOB_ARCHER;
  5546. case MAPID_ACOLYTE: return JOB_ACOLYTE;
  5547. case MAPID_MERCHANT: return JOB_MERCHANT;
  5548. case MAPID_THIEF: return JOB_THIEF;
  5549. case MAPID_TAEKWON: return JOB_TAEKWON;
  5550. case MAPID_WEDDING: return JOB_WEDDING;
  5551. case MAPID_GUNSLINGER: return JOB_GUNSLINGER;
  5552. case MAPID_NINJA: return JOB_NINJA;
  5553. case MAPID_XMAS: return JOB_XMAS;
  5554. case MAPID_SUMMER: return JOB_SUMMER;
  5555. case MAPID_HANBOK: return JOB_HANBOK;
  5556. case MAPID_GANGSI: return JOB_GANGSI;
  5557. case MAPID_OKTOBERFEST: return JOB_OKTOBERFEST;
  5558. case MAPID_SUMMER2: return JOB_SUMMER2;
  5559. //2-1 Jobs
  5560. case MAPID_SUPER_NOVICE: return JOB_SUPER_NOVICE;
  5561. case MAPID_KNIGHT: return JOB_KNIGHT;
  5562. case MAPID_WIZARD: return JOB_WIZARD;
  5563. case MAPID_HUNTER: return JOB_HUNTER;
  5564. case MAPID_PRIEST: return JOB_PRIEST;
  5565. case MAPID_BLACKSMITH: return JOB_BLACKSMITH;
  5566. case MAPID_ASSASSIN: return JOB_ASSASSIN;
  5567. case MAPID_STAR_GLADIATOR: return JOB_STAR_GLADIATOR;
  5568. case MAPID_KAGEROUOBORO: return sex?JOB_KAGEROU:JOB_OBORO;
  5569. case MAPID_REBELLION: return JOB_REBELLION;
  5570. case MAPID_DEATH_KNIGHT: return JOB_DEATH_KNIGHT;
  5571. //2-2 Jobs
  5572. case MAPID_CRUSADER: return JOB_CRUSADER;
  5573. case MAPID_SAGE: return JOB_SAGE;
  5574. case MAPID_BARDDANCER: return sex?JOB_BARD:JOB_DANCER;
  5575. case MAPID_MONK: return JOB_MONK;
  5576. case MAPID_ALCHEMIST: return JOB_ALCHEMIST;
  5577. case MAPID_ROGUE: return JOB_ROGUE;
  5578. case MAPID_SOUL_LINKER: return JOB_SOUL_LINKER;
  5579. case MAPID_DARK_COLLECTOR: return JOB_DARK_COLLECTOR;
  5580. //Trans Novice And Trans 2-1 Jobs
  5581. case MAPID_NOVICE_HIGH: return JOB_NOVICE_HIGH;
  5582. case MAPID_SWORDMAN_HIGH: return JOB_SWORDMAN_HIGH;
  5583. case MAPID_MAGE_HIGH: return JOB_MAGE_HIGH;
  5584. case MAPID_ARCHER_HIGH: return JOB_ARCHER_HIGH;
  5585. case MAPID_ACOLYTE_HIGH: return JOB_ACOLYTE_HIGH;
  5586. case MAPID_MERCHANT_HIGH: return JOB_MERCHANT_HIGH;
  5587. case MAPID_THIEF_HIGH: return JOB_THIEF_HIGH;
  5588. //Trans 2-1 Jobs
  5589. case MAPID_LORD_KNIGHT: return JOB_LORD_KNIGHT;
  5590. case MAPID_HIGH_WIZARD: return JOB_HIGH_WIZARD;
  5591. case MAPID_SNIPER: return JOB_SNIPER;
  5592. case MAPID_HIGH_PRIEST: return JOB_HIGH_PRIEST;
  5593. case MAPID_WHITESMITH: return JOB_WHITESMITH;
  5594. case MAPID_ASSASSIN_CROSS: return JOB_ASSASSIN_CROSS;
  5595. //Trans 2-2 Jobs
  5596. case MAPID_PALADIN: return JOB_PALADIN;
  5597. case MAPID_PROFESSOR: return JOB_PROFESSOR;
  5598. case MAPID_CLOWNGYPSY: return sex?JOB_CLOWN:JOB_GYPSY;
  5599. case MAPID_CHAMPION: return JOB_CHAMPION;
  5600. case MAPID_CREATOR: return JOB_CREATOR;
  5601. case MAPID_STALKER: return JOB_STALKER;
  5602. //Baby Novice And Baby 1-1 Jobs
  5603. case MAPID_BABY: return JOB_BABY;
  5604. case MAPID_BABY_SWORDMAN: return JOB_BABY_SWORDMAN;
  5605. case MAPID_BABY_MAGE: return JOB_BABY_MAGE;
  5606. case MAPID_BABY_ARCHER: return JOB_BABY_ARCHER;
  5607. case MAPID_BABY_ACOLYTE: return JOB_BABY_ACOLYTE;
  5608. case MAPID_BABY_MERCHANT: return JOB_BABY_MERCHANT;
  5609. case MAPID_BABY_THIEF: return JOB_BABY_THIEF;
  5610. case MAPID_BABY_TAEKWON: return JOB_BABY_TAEKWON;
  5611. case MAPID_BABY_GUNSLINGER: return JOB_BABY_GUNSLINGER;
  5612. case MAPID_BABY_NINJA: return JOB_BABY_NINJA;
  5613. case MAPID_BABY_SUMMONER: return JOB_BABY_SUMMONER;
  5614. //Baby 2-1 Jobs
  5615. case MAPID_SUPER_BABY: return JOB_SUPER_BABY;
  5616. case MAPID_BABY_KNIGHT: return JOB_BABY_KNIGHT;
  5617. case MAPID_BABY_WIZARD: return JOB_BABY_WIZARD;
  5618. case MAPID_BABY_HUNTER: return JOB_BABY_HUNTER;
  5619. case MAPID_BABY_PRIEST: return JOB_BABY_PRIEST;
  5620. case MAPID_BABY_BLACKSMITH: return JOB_BABY_BLACKSMITH;
  5621. case MAPID_BABY_ASSASSIN: return JOB_BABY_ASSASSIN;
  5622. case MAPID_BABY_STAR_GLADIATOR: return JOB_BABY_STAR_GLADIATOR;
  5623. case MAPID_BABY_REBELLION: return JOB_BABY_REBELLION;
  5624. case MAPID_BABY_KAGEROUOBORO: return sex?JOB_BABY_KAGEROU:JOB_BABY_OBORO;
  5625. //Baby 2-2 Jobs
  5626. case MAPID_BABY_CRUSADER: return JOB_BABY_CRUSADER;
  5627. case MAPID_BABY_SAGE: return JOB_BABY_SAGE;
  5628. case MAPID_BABY_BARDDANCER: return sex?JOB_BABY_BARD:JOB_BABY_DANCER;
  5629. case MAPID_BABY_MONK: return JOB_BABY_MONK;
  5630. case MAPID_BABY_ALCHEMIST: return JOB_BABY_ALCHEMIST;
  5631. case MAPID_BABY_ROGUE: return JOB_BABY_ROGUE;
  5632. case MAPID_BABY_SOUL_LINKER: return JOB_BABY_SOUL_LINKER;
  5633. //3-1 Jobs
  5634. case MAPID_SUPER_NOVICE_E: return JOB_SUPER_NOVICE_E;
  5635. case MAPID_RUNE_KNIGHT: return JOB_RUNE_KNIGHT;
  5636. case MAPID_WARLOCK: return JOB_WARLOCK;
  5637. case MAPID_RANGER: return JOB_RANGER;
  5638. case MAPID_ARCH_BISHOP: return JOB_ARCH_BISHOP;
  5639. case MAPID_MECHANIC: return JOB_MECHANIC;
  5640. case MAPID_GUILLOTINE_CROSS: return JOB_GUILLOTINE_CROSS;
  5641. case MAPID_STAR_EMPEROR: return JOB_STAR_EMPEROR;
  5642. //3-2 Jobs
  5643. case MAPID_ROYAL_GUARD: return JOB_ROYAL_GUARD;
  5644. case MAPID_SORCERER: return JOB_SORCERER;
  5645. case MAPID_MINSTRELWANDERER: return sex?JOB_MINSTREL:JOB_WANDERER;
  5646. case MAPID_SURA: return JOB_SURA;
  5647. case MAPID_GENETIC: return JOB_GENETIC;
  5648. case MAPID_SHADOW_CHASER: return JOB_SHADOW_CHASER;
  5649. case MAPID_SOUL_REAPER: return JOB_SOUL_REAPER;
  5650. //Trans 3-1 Jobs
  5651. case MAPID_RUNE_KNIGHT_T: return JOB_RUNE_KNIGHT_T;
  5652. case MAPID_WARLOCK_T: return JOB_WARLOCK_T;
  5653. case MAPID_RANGER_T: return JOB_RANGER_T;
  5654. case MAPID_ARCH_BISHOP_T: return JOB_ARCH_BISHOP_T;
  5655. case MAPID_MECHANIC_T: return JOB_MECHANIC_T;
  5656. case MAPID_GUILLOTINE_CROSS_T: return JOB_GUILLOTINE_CROSS_T;
  5657. //Trans 3-2 Jobs
  5658. case MAPID_ROYAL_GUARD_T: return JOB_ROYAL_GUARD_T;
  5659. case MAPID_SORCERER_T: return JOB_SORCERER_T;
  5660. case MAPID_MINSTRELWANDERER_T: return sex?JOB_MINSTREL_T:JOB_WANDERER_T;
  5661. case MAPID_SURA_T: return JOB_SURA_T;
  5662. case MAPID_GENETIC_T: return JOB_GENETIC_T;
  5663. case MAPID_SHADOW_CHASER_T: return JOB_SHADOW_CHASER_T;
  5664. //Baby 3-1 Jobs
  5665. case MAPID_SUPER_BABY_E: return JOB_SUPER_BABY_E;
  5666. case MAPID_BABY_RUNE: return JOB_BABY_RUNE;
  5667. case MAPID_BABY_WARLOCK: return JOB_BABY_WARLOCK;
  5668. case MAPID_BABY_RANGER: return JOB_BABY_RANGER;
  5669. case MAPID_BABY_BISHOP: return JOB_BABY_BISHOP;
  5670. case MAPID_BABY_MECHANIC: return JOB_BABY_MECHANIC;
  5671. case MAPID_BABY_CROSS: return JOB_BABY_CROSS;
  5672. case MAPID_BABY_STAR_EMPEROR: return JOB_BABY_STAR_EMPEROR;
  5673. //Baby 3-2 Jobs
  5674. case MAPID_BABY_GUARD: return JOB_BABY_GUARD;
  5675. case MAPID_BABY_SORCERER: return JOB_BABY_SORCERER;
  5676. case MAPID_BABY_MINSTRELWANDERER: return sex?JOB_BABY_MINSTREL:JOB_BABY_WANDERER;
  5677. case MAPID_BABY_SURA: return JOB_BABY_SURA;
  5678. case MAPID_BABY_GENETIC: return JOB_BABY_GENETIC;
  5679. case MAPID_BABY_CHASER: return JOB_BABY_CHASER;
  5680. case MAPID_BABY_SOUL_REAPER: return JOB_BABY_SOUL_REAPER;
  5681. //Doram Jobs
  5682. case MAPID_SUMMONER: return JOB_SUMMONER;
  5683. default:
  5684. return -1;
  5685. }
  5686. }
  5687. /*====================================================
  5688. * This function return the name of the job (by [Yor])
  5689. *----------------------------------------------------*/
  5690. const char* job_name(int class_)
  5691. {
  5692. switch (class_) {
  5693. case JOB_NOVICE:
  5694. case JOB_SWORDMAN:
  5695. case JOB_MAGE:
  5696. case JOB_ARCHER:
  5697. case JOB_ACOLYTE:
  5698. case JOB_MERCHANT:
  5699. case JOB_THIEF:
  5700. return msg_txt(NULL,550 - JOB_NOVICE+class_);
  5701. case JOB_KNIGHT:
  5702. case JOB_PRIEST:
  5703. case JOB_WIZARD:
  5704. case JOB_BLACKSMITH:
  5705. case JOB_HUNTER:
  5706. case JOB_ASSASSIN:
  5707. return msg_txt(NULL,557 - JOB_KNIGHT+class_);
  5708. case JOB_KNIGHT2:
  5709. return msg_txt(NULL,557);
  5710. case JOB_CRUSADER:
  5711. case JOB_MONK:
  5712. case JOB_SAGE:
  5713. case JOB_ROGUE:
  5714. case JOB_ALCHEMIST:
  5715. case JOB_BARD:
  5716. case JOB_DANCER:
  5717. return msg_txt(NULL,563 - JOB_CRUSADER+class_);
  5718. case JOB_CRUSADER2:
  5719. return msg_txt(NULL,563);
  5720. case JOB_WEDDING:
  5721. case JOB_SUPER_NOVICE:
  5722. case JOB_GUNSLINGER:
  5723. case JOB_NINJA:
  5724. case JOB_XMAS:
  5725. return msg_txt(NULL,570 - JOB_WEDDING+class_);
  5726. case JOB_SUMMER:
  5727. case JOB_SUMMER2:
  5728. return msg_txt(NULL,621);
  5729. case JOB_HANBOK:
  5730. return msg_txt(NULL,694);
  5731. case JOB_OKTOBERFEST:
  5732. return msg_txt(NULL,696);
  5733. case JOB_NOVICE_HIGH:
  5734. case JOB_SWORDMAN_HIGH:
  5735. case JOB_MAGE_HIGH:
  5736. case JOB_ARCHER_HIGH:
  5737. case JOB_ACOLYTE_HIGH:
  5738. case JOB_MERCHANT_HIGH:
  5739. case JOB_THIEF_HIGH:
  5740. return msg_txt(NULL,575 - JOB_NOVICE_HIGH+class_);
  5741. case JOB_LORD_KNIGHT:
  5742. case JOB_HIGH_PRIEST:
  5743. case JOB_HIGH_WIZARD:
  5744. case JOB_WHITESMITH:
  5745. case JOB_SNIPER:
  5746. case JOB_ASSASSIN_CROSS:
  5747. return msg_txt(NULL,582 - JOB_LORD_KNIGHT+class_);
  5748. case JOB_LORD_KNIGHT2:
  5749. return msg_txt(NULL,582);
  5750. case JOB_PALADIN:
  5751. case JOB_CHAMPION:
  5752. case JOB_PROFESSOR:
  5753. case JOB_STALKER:
  5754. case JOB_CREATOR:
  5755. case JOB_CLOWN:
  5756. case JOB_GYPSY:
  5757. return msg_txt(NULL,588 - JOB_PALADIN + class_);
  5758. case JOB_PALADIN2:
  5759. return msg_txt(NULL,588);
  5760. case JOB_BABY:
  5761. case JOB_BABY_SWORDMAN:
  5762. case JOB_BABY_MAGE:
  5763. case JOB_BABY_ARCHER:
  5764. case JOB_BABY_ACOLYTE:
  5765. case JOB_BABY_MERCHANT:
  5766. case JOB_BABY_THIEF:
  5767. return msg_txt(NULL,595 - JOB_BABY + class_);
  5768. case JOB_BABY_KNIGHT:
  5769. case JOB_BABY_PRIEST:
  5770. case JOB_BABY_WIZARD:
  5771. case JOB_BABY_BLACKSMITH:
  5772. case JOB_BABY_HUNTER:
  5773. case JOB_BABY_ASSASSIN:
  5774. return msg_txt(NULL,602 - JOB_BABY_KNIGHT + class_);
  5775. case JOB_BABY_KNIGHT2:
  5776. return msg_txt(NULL,602);
  5777. case JOB_BABY_CRUSADER:
  5778. case JOB_BABY_MONK:
  5779. case JOB_BABY_SAGE:
  5780. case JOB_BABY_ROGUE:
  5781. case JOB_BABY_ALCHEMIST:
  5782. case JOB_BABY_BARD:
  5783. case JOB_BABY_DANCER:
  5784. return msg_txt(NULL,608 - JOB_BABY_CRUSADER + class_);
  5785. case JOB_BABY_CRUSADER2:
  5786. return msg_txt(NULL,608);
  5787. case JOB_SUPER_BABY:
  5788. return msg_txt(NULL,615);
  5789. case JOB_TAEKWON:
  5790. return msg_txt(NULL,616);
  5791. case JOB_STAR_GLADIATOR:
  5792. case JOB_STAR_GLADIATOR2:
  5793. return msg_txt(NULL,617);
  5794. case JOB_SOUL_LINKER:
  5795. return msg_txt(NULL,618);
  5796. case JOB_GANGSI:
  5797. case JOB_DEATH_KNIGHT:
  5798. case JOB_DARK_COLLECTOR:
  5799. return msg_txt(NULL,622 - JOB_GANGSI+class_);
  5800. case JOB_RUNE_KNIGHT:
  5801. case JOB_WARLOCK:
  5802. case JOB_RANGER:
  5803. case JOB_ARCH_BISHOP:
  5804. case JOB_MECHANIC:
  5805. case JOB_GUILLOTINE_CROSS:
  5806. return msg_txt(NULL,625 - JOB_RUNE_KNIGHT+class_);
  5807. case JOB_RUNE_KNIGHT_T:
  5808. case JOB_WARLOCK_T:
  5809. case JOB_RANGER_T:
  5810. case JOB_ARCH_BISHOP_T:
  5811. case JOB_MECHANIC_T:
  5812. case JOB_GUILLOTINE_CROSS_T:
  5813. return msg_txt(NULL,681 - JOB_RUNE_KNIGHT_T+class_);
  5814. case JOB_ROYAL_GUARD:
  5815. case JOB_SORCERER:
  5816. case JOB_MINSTREL:
  5817. case JOB_WANDERER:
  5818. case JOB_SURA:
  5819. case JOB_GENETIC:
  5820. case JOB_SHADOW_CHASER:
  5821. return msg_txt(NULL,631 - JOB_ROYAL_GUARD+class_);
  5822. case JOB_ROYAL_GUARD_T:
  5823. case JOB_SORCERER_T:
  5824. case JOB_MINSTREL_T:
  5825. case JOB_WANDERER_T:
  5826. case JOB_SURA_T:
  5827. case JOB_GENETIC_T:
  5828. case JOB_SHADOW_CHASER_T:
  5829. return msg_txt(NULL,687 - JOB_ROYAL_GUARD_T+class_);
  5830. case JOB_RUNE_KNIGHT2:
  5831. case JOB_RUNE_KNIGHT_T2:
  5832. return msg_txt(NULL,625);
  5833. case JOB_ROYAL_GUARD2:
  5834. case JOB_ROYAL_GUARD_T2:
  5835. return msg_txt(NULL,631);
  5836. case JOB_RANGER2:
  5837. case JOB_RANGER_T2:
  5838. return msg_txt(NULL,627);
  5839. case JOB_MECHANIC2:
  5840. case JOB_MECHANIC_T2:
  5841. return msg_txt(NULL,629);
  5842. case JOB_BABY_RUNE:
  5843. case JOB_BABY_WARLOCK:
  5844. case JOB_BABY_RANGER:
  5845. case JOB_BABY_BISHOP:
  5846. case JOB_BABY_MECHANIC:
  5847. case JOB_BABY_CROSS:
  5848. case JOB_BABY_GUARD:
  5849. case JOB_BABY_SORCERER:
  5850. case JOB_BABY_MINSTREL:
  5851. case JOB_BABY_WANDERER:
  5852. case JOB_BABY_SURA:
  5853. case JOB_BABY_GENETIC:
  5854. case JOB_BABY_CHASER:
  5855. return msg_txt(NULL,638 - JOB_BABY_RUNE+class_);
  5856. case JOB_BABY_RUNE2:
  5857. return msg_txt(NULL,638);
  5858. case JOB_BABY_GUARD2:
  5859. return msg_txt(NULL,644);
  5860. case JOB_BABY_RANGER2:
  5861. return msg_txt(NULL,640);
  5862. case JOB_BABY_MECHANIC2:
  5863. return msg_txt(NULL,642);
  5864. case JOB_SUPER_NOVICE_E:
  5865. case JOB_SUPER_BABY_E:
  5866. return msg_txt(NULL,651 - JOB_SUPER_NOVICE_E+class_);
  5867. case JOB_KAGEROU:
  5868. case JOB_OBORO:
  5869. return msg_txt(NULL,653 - JOB_KAGEROU+class_);
  5870. case JOB_REBELLION:
  5871. return msg_txt(NULL,695);
  5872. case JOB_SUMMONER:
  5873. return msg_txt(NULL,697);
  5874. case JOB_BABY_SUMMONER:
  5875. return msg_txt(NULL,698);
  5876. case JOB_BABY_NINJA:
  5877. return msg_txt(NULL,699);
  5878. case JOB_BABY_KAGEROU:
  5879. case JOB_BABY_OBORO:
  5880. case JOB_BABY_TAEKWON:
  5881. case JOB_BABY_STAR_GLADIATOR:
  5882. case JOB_BABY_SOUL_LINKER:
  5883. case JOB_BABY_GUNSLINGER:
  5884. case JOB_BABY_REBELLION:
  5885. return msg_txt(NULL,753 - JOB_BABY_KAGEROU+class_);
  5886. case JOB_BABY_STAR_GLADIATOR2:
  5887. return msg_txt(NULL,756);
  5888. case JOB_STAR_EMPEROR:
  5889. case JOB_SOUL_REAPER:
  5890. case JOB_BABY_STAR_EMPEROR:
  5891. case JOB_BABY_SOUL_REAPER:
  5892. return msg_txt(NULL,782 - JOB_STAR_EMPEROR + class_);
  5893. case JOB_STAR_EMPEROR2:
  5894. return msg_txt(NULL,782);
  5895. case JOB_BABY_STAR_EMPEROR2:
  5896. return msg_txt(NULL,784);
  5897. default:
  5898. return msg_txt(NULL,655);
  5899. }
  5900. }
  5901. /*====================================================
  5902. * Timered function to make id follow a target.
  5903. * @id = bl.id (player only atm)
  5904. * target is define in sd->followtarget (bl.id)
  5905. * used by pc_follow
  5906. *----------------------------------------------------*/
  5907. TIMER_FUNC(pc_follow_timer){
  5908. struct map_session_data *sd;
  5909. struct block_list *tbl;
  5910. sd = map_id2sd(id);
  5911. nullpo_ret(sd);
  5912. if (sd->followtimer != tid){
  5913. ShowError("pc_follow_timer %d != %d\n",sd->followtimer,tid);
  5914. sd->followtimer = INVALID_TIMER;
  5915. return 0;
  5916. }
  5917. sd->followtimer = INVALID_TIMER;
  5918. tbl = map_id2bl(sd->followtarget);
  5919. if (tbl == NULL || pc_isdead(sd))
  5920. {
  5921. pc_stop_following(sd);
  5922. return 0;
  5923. }
  5924. // either player or target is currently detached from map blocks (could be teleporting),
  5925. // but still connected to this map, so we'll just increment the timer and check back later
  5926. if (sd->bl.prev != NULL && tbl->prev != NULL &&
  5927. sd->ud.skilltimer == INVALID_TIMER && sd->ud.attacktimer == INVALID_TIMER && sd->ud.walktimer == INVALID_TIMER)
  5928. {
  5929. if((sd->bl.m == tbl->m) && unit_can_reach_bl(&sd->bl,tbl, AREA_SIZE, 0, NULL, NULL)) {
  5930. if (!check_distance_bl(&sd->bl, tbl, 5))
  5931. unit_walktobl(&sd->bl, tbl, 5, 0);
  5932. } else
  5933. pc_setpos(sd, map_id2index(tbl->m), tbl->x, tbl->y, CLR_TELEPORT);
  5934. }
  5935. sd->followtimer = add_timer(
  5936. tick + 1000, // increase time a bit to loosen up map's load
  5937. pc_follow_timer, sd->bl.id, 0);
  5938. return 0;
  5939. }
  5940. int pc_stop_following (struct map_session_data *sd)
  5941. {
  5942. nullpo_ret(sd);
  5943. if (sd->followtimer != INVALID_TIMER) {
  5944. delete_timer(sd->followtimer,pc_follow_timer);
  5945. sd->followtimer = INVALID_TIMER;
  5946. }
  5947. sd->followtarget = -1;
  5948. sd->ud.target_to = 0;
  5949. unit_stop_walking(&sd->bl, 1);
  5950. return 0;
  5951. }
  5952. int pc_follow(struct map_session_data *sd,int target_id)
  5953. {
  5954. struct block_list *bl = map_id2bl(target_id);
  5955. if (bl == NULL /*|| bl->type != BL_PC*/)
  5956. return 1;
  5957. if (sd->followtimer != INVALID_TIMER)
  5958. pc_stop_following(sd);
  5959. sd->followtarget = target_id;
  5960. pc_follow_timer(INVALID_TIMER, gettick(), sd->bl.id, 0);
  5961. return 0;
  5962. }
  5963. int pc_checkbaselevelup(struct map_session_data *sd) {
  5964. unsigned int next = pc_nextbaseexp(sd);
  5965. if (!next || sd->status.base_exp < next || pc_is_maxbaselv(sd))
  5966. return 0;
  5967. do {
  5968. sd->status.base_exp -= next;
  5969. //Kyoki pointed out that the max overcarry exp is the exp needed for the previous level -1. [Skotlex]
  5970. 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 )
  5971. sd->status.base_exp = next-1;
  5972. next = pc_gets_status_point(sd->status.base_level);
  5973. sd->status.base_level++;
  5974. sd->status.status_point += next;
  5975. if( pc_is_maxbaselv(sd) ){
  5976. sd->status.base_exp = u32min(sd->status.base_exp,MAX_LEVEL_BASE_EXP);
  5977. break;
  5978. }
  5979. } while ((next=pc_nextbaseexp(sd)) > 0 && sd->status.base_exp >= next);
  5980. if (battle_config.pet_lv_rate && sd->pd) //<Skotlex> update pet's level
  5981. status_calc_pet(sd->pd,SCO_NONE);
  5982. clif_updatestatus(sd,SP_STATUSPOINT);
  5983. clif_updatestatus(sd,SP_BASELEVEL);
  5984. clif_updatestatus(sd,SP_BASEEXP);
  5985. clif_updatestatus(sd,SP_NEXTBASEEXP);
  5986. status_calc_pc(sd,SCO_FORCE);
  5987. status_percent_heal(&sd->bl,100,100);
  5988. if ((sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE) {
  5989. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_KYRIE),100,1,skill_get_time(PR_KYRIE,1));
  5990. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_IMPOSITIO),100,1,skill_get_time(PR_IMPOSITIO,1));
  5991. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_MAGNIFICAT),100,1,skill_get_time(PR_MAGNIFICAT,1));
  5992. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_GLORIA),100,1,skill_get_time(PR_GLORIA,1));
  5993. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_SUFFRAGIUM),100,1,skill_get_time(PR_SUFFRAGIUM,1));
  5994. if (sd->state.snovice_dead_flag)
  5995. sd->state.snovice_dead_flag = 0; //Reenable steelbody resurrection on dead.
  5996. } else if( (sd->class_&MAPID_BASEMASK) == MAPID_TAEKWON ) {
  5997. sc_start(&sd->bl,&sd->bl,status_skill2sc(AL_INCAGI),100,10,600000);
  5998. sc_start(&sd->bl,&sd->bl,status_skill2sc(AL_BLESSING),100,10,600000);
  5999. }
  6000. clif_misceffect(&sd->bl,0);
  6001. npc_script_event(sd, NPCE_BASELVUP); //LORDALFA - LVLUPEVENT
  6002. if(sd->status.party_id)
  6003. party_send_levelup(sd);
  6004. pc_baselevelchanged(sd);
  6005. achievement_update_objective(sd, AG_GOAL_LEVEL, 1, sd->status.base_level);
  6006. achievement_update_objective(sd, AG_GOAL_STATUS, 2, sd->status.base_level, sd->status.class_);
  6007. return 1;
  6008. }
  6009. void pc_baselevelchanged(struct map_session_data *sd) {
  6010. uint8 i;
  6011. for( i = 0; i < EQI_MAX; i++ ) {
  6012. if( sd->equip_index[i] >= 0 ) {
  6013. if( sd->inventory_data[ sd->equip_index[i] ]->elvmax && sd->status.base_level > (unsigned int)sd->inventory_data[ sd->equip_index[i] ]->elvmax )
  6014. pc_unequipitem(sd, sd->equip_index[i], 3);
  6015. }
  6016. }
  6017. pc_show_questinfo(sd);
  6018. }
  6019. int pc_checkjoblevelup(struct map_session_data *sd)
  6020. {
  6021. unsigned int next = pc_nextjobexp(sd);
  6022. nullpo_ret(sd);
  6023. if(!next || sd->status.job_exp < next || pc_is_maxjoblv(sd))
  6024. return 0;
  6025. do {
  6026. sd->status.job_exp -= next;
  6027. //Kyoki pointed out that the max overcarry exp is the exp needed for the previous level -1. [Skotlex]
  6028. 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 )
  6029. sd->status.job_exp = next-1;
  6030. sd->status.job_level ++;
  6031. sd->status.skill_point ++;
  6032. if( pc_is_maxjoblv(sd) ){
  6033. sd->status.job_exp = u32min(sd->status.job_exp,MAX_LEVEL_JOB_EXP);
  6034. break;
  6035. }
  6036. } while ((next=pc_nextjobexp(sd)) > 0 && sd->status.job_exp >= next);
  6037. clif_updatestatus(sd,SP_JOBLEVEL);
  6038. clif_updatestatus(sd,SP_JOBEXP);
  6039. clif_updatestatus(sd,SP_NEXTJOBEXP);
  6040. clif_updatestatus(sd,SP_SKILLPOINT);
  6041. status_calc_pc(sd,SCO_FORCE);
  6042. clif_misceffect(&sd->bl,1);
  6043. if (pc_checkskill(sd, SG_DEVIL) && pc_is_maxbaselv(sd))
  6044. clif_status_change(&sd->bl, EFST_DEVIL1, 1, 0, 0, 0, 1); //Permanent blind effect from SG_DEVIL.
  6045. npc_script_event(sd, NPCE_JOBLVUP);
  6046. achievement_update_objective(sd, AG_GOAL_LEVEL, 1, sd->status.job_level);
  6047. return 1;
  6048. }
  6049. /** Alters experiences calculation based on self bonuses that do not get even shared to the party.
  6050. * @param sd Player
  6051. * @param base_exp Base EXP before peronal bonuses
  6052. * @param job_exp Job EXP before peronal bonuses
  6053. * @param src Block list that affecting the exp calculation
  6054. */
  6055. static void pc_calcexp(struct map_session_data *sd, unsigned int *base_exp, unsigned int *job_exp, struct block_list *src)
  6056. {
  6057. int bonus = 0, vip_bonus_base = 0, vip_bonus_job = 0;
  6058. if (src) {
  6059. struct status_data *status = status_get_status_data(src);
  6060. if( sd->expaddrace[status->race] )
  6061. bonus += sd->expaddrace[status->race];
  6062. if( sd->expaddrace[RC_ALL] )
  6063. bonus += sd->expaddrace[RC_ALL];
  6064. if( sd->expaddclass[status->class_] )
  6065. bonus += sd->expaddclass[status->class_];
  6066. if( sd->expaddclass[CLASS_ALL] )
  6067. bonus += sd->expaddclass[CLASS_ALL];
  6068. if (battle_config.pk_mode &&
  6069. (int)(status_get_lv(src) - sd->status.base_level) >= 20)
  6070. bonus += 15; // pk_mode additional exp if monster >20 levels [Valaris]
  6071. if (src && src->type == BL_MOB && pc_isvip(sd)) { // EXP bonus for VIP player
  6072. vip_bonus_base = battle_config.vip_base_exp_increase;
  6073. vip_bonus_job = battle_config.vip_job_exp_increase;
  6074. }
  6075. }
  6076. // Give EXPBOOST for quests even if src is NULL.
  6077. if (sd->sc.data[SC_EXPBOOST]) {
  6078. bonus += sd->sc.data[SC_EXPBOOST]->val1;
  6079. if (battle_config.vip_bm_increase && pc_isvip(sd)) // Increase Battle Manual EXP rate for VIP
  6080. bonus += (sd->sc.data[SC_EXPBOOST]->val1 / battle_config.vip_bm_increase);
  6081. }
  6082. if (*base_exp) {
  6083. unsigned int exp = (unsigned int)(*base_exp + (double)*base_exp * (bonus + vip_bonus_base)/100.);
  6084. *base_exp = cap_value(exp, 1, UINT_MAX);
  6085. }
  6086. // Give JEXPBOOST for quests even if src is NULL.
  6087. if (sd->sc.data[SC_JEXPBOOST])
  6088. bonus += sd->sc.data[SC_JEXPBOOST]->val1;
  6089. if (*job_exp) {
  6090. unsigned int exp = (unsigned int)(*job_exp + (double)*job_exp * (bonus + vip_bonus_job)/100.);
  6091. *job_exp = cap_value(exp, 1, UINT_MAX);
  6092. }
  6093. return;
  6094. }
  6095. /**
  6096. * Show EXP gained by player in percentage by @showexp
  6097. * @param sd Player
  6098. * @param base_exp Base EXP gained/loss
  6099. * @param next_base_exp Base EXP needed for next base level
  6100. * @param job_exp Job EXP gained/loss
  6101. * @param next_job_exp Job EXP needed for next job level
  6102. * @param lost True:EXP penalty, lose EXP
  6103. **/
  6104. 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) {
  6105. char output[CHAT_SIZE_MAX];
  6106. nullpo_retv(sd);
  6107. sprintf(output, msg_txt(sd,743), // Experience %s Base:%ld (%0.2f%%) Job:%ld (%0.2f%%)
  6108. (lost) ? msg_txt(sd,742) : msg_txt(sd,741),
  6109. (long)base_exp * (lost ? -1 : 1), (base_exp / (float)next_base_exp * 100 * (lost ? -1 : 1)),
  6110. (long)job_exp * (lost ? -1 : 1), (job_exp / (float)next_job_exp * 100 * (lost ? -1 : 1)));
  6111. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  6112. }
  6113. /**
  6114. * Give Base or Job EXP to player, then calculate remaining exp for next lvl
  6115. * @param sd Player
  6116. * @param src EXP source
  6117. * @param base_exp Base EXP gained
  6118. * @param base_exp Job EXP gained
  6119. * @param exp_flag 1: Quest EXP; 2: Param Exp (Ignore Guild EXP tax, EXP adjustments)
  6120. * @return
  6121. **/
  6122. void pc_gainexp(struct map_session_data *sd, struct block_list *src, unsigned int base_exp, unsigned int job_exp, uint8 exp_flag)
  6123. {
  6124. unsigned int nextb = 0, nextj = 0;
  6125. uint8 flag = 0; ///< 1: Base EXP given, 2: Job EXP given, 4: Max Base level, 8: Max Job Level
  6126. nullpo_retv(sd);
  6127. if(sd->bl.prev == NULL || pc_isdead(sd))
  6128. return;
  6129. if (!(exp_flag&2)) {
  6130. if (!battle_config.pvp_exp && map_getmapflag(sd->bl.m, MF_PVP)) // [MouseJstr]
  6131. return; // no exp on pvp maps
  6132. if (sd->status.guild_id>0)
  6133. base_exp -= guild_payexp(sd,base_exp);
  6134. }
  6135. flag = ((base_exp) ? 1 : 0) |
  6136. ((job_exp) ? 2 : 0) |
  6137. ((pc_is_maxbaselv(sd)) ? 4 : 0) |
  6138. ((pc_is_maxjoblv(sd)) ? 8 : 0);
  6139. if (!(exp_flag&2))
  6140. pc_calcexp(sd, &base_exp, &job_exp, src);
  6141. nextb = pc_nextbaseexp(sd);
  6142. nextj = pc_nextjobexp(sd);
  6143. if (flag&4){
  6144. if( sd->status.base_exp >= MAX_LEVEL_BASE_EXP )
  6145. base_exp = 0;
  6146. else if( sd->status.base_exp + base_exp >= MAX_LEVEL_BASE_EXP )
  6147. base_exp = MAX_LEVEL_BASE_EXP - sd->status.base_exp;
  6148. }
  6149. if (flag&8){
  6150. if( sd->status.job_exp >= MAX_LEVEL_JOB_EXP )
  6151. job_exp = 0;
  6152. else if( sd->status.job_exp + job_exp >= MAX_LEVEL_JOB_EXP )
  6153. job_exp = MAX_LEVEL_JOB_EXP - sd->status.job_exp;
  6154. }
  6155. if (!(exp_flag&2) && battle_config.max_exp_gain_rate && (base_exp || job_exp)) {
  6156. //Note that this value should never be greater than the original
  6157. //therefore no overflow checks are needed. [Skotlex]
  6158. if (nextb > 0) {
  6159. float nextbp = (float) base_exp / (float) nextb;
  6160. if (nextbp > battle_config.max_exp_gain_rate/1000.)
  6161. base_exp = (unsigned int)(battle_config.max_exp_gain_rate/1000.*nextb);
  6162. }
  6163. if (nextj > 0) {
  6164. float nextjp = (float) job_exp / (float) nextj;
  6165. if (nextjp > battle_config.max_exp_gain_rate/1000.)
  6166. job_exp = (unsigned int)(battle_config.max_exp_gain_rate/1000.*nextj);
  6167. }
  6168. }
  6169. // Give EXP for Base Level
  6170. if (base_exp) {
  6171. if ((uint64)sd->status.base_exp + base_exp > UINT32_MAX)
  6172. sd->status.base_exp = UINT32_MAX;
  6173. else
  6174. sd->status.base_exp += base_exp;
  6175. if (!pc_checkbaselevelup(sd))
  6176. clif_updatestatus(sd,SP_BASEEXP);
  6177. }
  6178. // Give EXP for Job Level
  6179. if (job_exp) {
  6180. if ((uint64)sd->status.job_exp + job_exp > UINT32_MAX)
  6181. sd->status.job_exp = UINT32_MAX;
  6182. else
  6183. sd->status.job_exp += job_exp;
  6184. if (!pc_checkjoblevelup(sd))
  6185. clif_updatestatus(sd,SP_JOBEXP);
  6186. }
  6187. if (flag&1)
  6188. clif_displayexp(sd, (flag&4) ? 0 : base_exp, SP_BASEEXP, exp_flag&1, false);
  6189. if (flag&2)
  6190. clif_displayexp(sd, (flag&8) ? 0 : job_exp, SP_JOBEXP, exp_flag&1, false);
  6191. if (sd->state.showexp && (base_exp || job_exp))
  6192. pc_gainexp_disp(sd, base_exp, nextb, job_exp, nextj, false);
  6193. }
  6194. /**
  6195. * Lost Base/Job EXP from a player
  6196. * @param sd Player
  6197. * @param base_exp Base EXP lost
  6198. * @param job_exp Job EXP lost
  6199. **/
  6200. void pc_lostexp(struct map_session_data *sd, unsigned int base_exp, unsigned int job_exp) {
  6201. nullpo_retv(sd);
  6202. if (base_exp) {
  6203. base_exp = u32min(sd->status.base_exp, base_exp);
  6204. sd->status.base_exp -= base_exp;
  6205. clif_displayexp(sd, base_exp, SP_BASEEXP, false, true);
  6206. clif_updatestatus(sd, SP_BASEEXP);
  6207. }
  6208. if (job_exp) {
  6209. job_exp = u32min(sd->status.job_exp, job_exp);
  6210. sd->status.job_exp -= job_exp;
  6211. clif_displayexp(sd, job_exp, SP_JOBEXP, false, true);
  6212. clif_updatestatus(sd, SP_JOBEXP);
  6213. }
  6214. if (sd->state.showexp && (base_exp || job_exp))
  6215. pc_gainexp_disp(sd, base_exp, pc_nextbaseexp(sd), job_exp, pc_nextjobexp(sd), true);
  6216. }
  6217. /**
  6218. * Returns max base level for this character's class.
  6219. * @param class_: Player's class
  6220. * @return Max Base Level
  6221. */
  6222. static unsigned int pc_class_maxbaselv(unsigned short class_) {
  6223. return job_info[pc_class2idx(class_)].max_level[0];
  6224. }
  6225. /**
  6226. * Returns max base level for this character.
  6227. * @param sd Player
  6228. * @return Max Base Level
  6229. **/
  6230. unsigned int pc_maxbaselv(struct map_session_data *sd){
  6231. return pc_class_maxbaselv(sd->status.class_);
  6232. }
  6233. /**
  6234. * Returns max job level for this character's class.
  6235. * @param class_: Player's class
  6236. * @return Max Job Level
  6237. */
  6238. static unsigned int pc_class_maxjoblv(unsigned short class_) {
  6239. return job_info[pc_class2idx(class_)].max_level[1];
  6240. }
  6241. /**
  6242. * Returns max job level for this character.
  6243. * @param sd Player
  6244. * @return Max Job Level
  6245. **/
  6246. unsigned int pc_maxjoblv(struct map_session_data *sd){
  6247. return pc_class_maxjoblv(sd->status.class_);
  6248. }
  6249. /**
  6250. * Check if player is reached max base level
  6251. * @param sd
  6252. * @return True if reached max level
  6253. **/
  6254. bool pc_is_maxbaselv(struct map_session_data *sd) {
  6255. nullpo_retr(false, sd);
  6256. return (sd->status.base_level >= pc_maxbaselv(sd));
  6257. }
  6258. /**
  6259. * Check if player is reached max base level
  6260. * @param sd
  6261. * @return True if reached max level
  6262. **/
  6263. bool pc_is_maxjoblv(struct map_session_data *sd) {
  6264. nullpo_retr(false, sd);
  6265. return (sd->status.job_level >= pc_maxjoblv(sd));
  6266. }
  6267. /**
  6268. * Base exp needed for player to level up.
  6269. * @param sd
  6270. * @return Base EXP needed for next base level
  6271. **/
  6272. unsigned int pc_nextbaseexp(struct map_session_data *sd){
  6273. nullpo_ret(sd);
  6274. if (sd->status.base_level == 0) // Is this something that possible?
  6275. return 0;
  6276. if (pc_is_maxbaselv(sd))
  6277. return MAX_LEVEL_BASE_EXP; // On max level, player's base EXP limit is 99,999,999
  6278. return job_info[pc_class2idx(sd->status.class_)].exp_table[0][sd->status.base_level-1];
  6279. }
  6280. /**
  6281. * Job exp needed for player to level up.
  6282. * @param sd
  6283. * @return Job EXP needed for next job level
  6284. **/
  6285. unsigned int pc_nextjobexp(struct map_session_data *sd){
  6286. nullpo_ret(sd);
  6287. if (sd->status.job_level == 0) // Is this something that possible?
  6288. return 0;
  6289. if (pc_is_maxjoblv(sd))
  6290. return MAX_LEVEL_JOB_EXP; // On max level, player's job EXP limit is 999,999,999
  6291. return job_info[pc_class2idx(sd->status.class_)].exp_table[1][sd->status.job_level-1];
  6292. }
  6293. /// Returns the value of the specified stat.
  6294. static int pc_getstat(struct map_session_data* sd, int type)
  6295. {
  6296. nullpo_retr(-1, sd);
  6297. switch( type ) {
  6298. case SP_STR: return sd->status.str;
  6299. case SP_AGI: return sd->status.agi;
  6300. case SP_VIT: return sd->status.vit;
  6301. case SP_INT: return sd->status.int_;
  6302. case SP_DEX: return sd->status.dex;
  6303. case SP_LUK: return sd->status.luk;
  6304. default:
  6305. return -1;
  6306. }
  6307. }
  6308. /// Sets the specified stat to the specified value.
  6309. /// Returns the new value.
  6310. static int pc_setstat(struct map_session_data* sd, int type, int val)
  6311. {
  6312. nullpo_retr(-1, sd);
  6313. switch( type ) {
  6314. case SP_STR: sd->status.str = val; break;
  6315. case SP_AGI: sd->status.agi = val; break;
  6316. case SP_VIT: sd->status.vit = val; break;
  6317. case SP_INT: sd->status.int_ = val; break;
  6318. case SP_DEX: sd->status.dex = val; break;
  6319. case SP_LUK: sd->status.luk = val; break;
  6320. default:
  6321. return -1;
  6322. }
  6323. return val;
  6324. }
  6325. // Calculates the number of status points PC gets when leveling up (from level to level+1)
  6326. int pc_gets_status_point(int level)
  6327. {
  6328. if (battle_config.use_statpoint_table) //Use values from "db/statpoint.txt"
  6329. return (statp[level+1] - statp[level]);
  6330. else //Default increase
  6331. return ((level+15) / 5);
  6332. }
  6333. #ifdef RENEWAL_STAT
  6334. /// Renewal status point cost formula
  6335. #define PC_STATUS_POINT_COST(low) (((low) < 100) ? (2 + ((low) - 1) / 10) : (16 + 4 * (((low) - 100) / 5)))
  6336. #else
  6337. /// Pre-Renewal status point cost formula
  6338. #define PC_STATUS_POINT_COST(low) (( 1 + ((low) + 9) / 10 ))
  6339. #endif
  6340. /// Returns the number of stat points needed to change the specified stat by val.
  6341. /// If val is negative, returns the number of stat points that would be needed to
  6342. /// raise the specified stat from (current value - val) to current value.
  6343. int pc_need_status_point(struct map_session_data* sd, int type, int val)
  6344. {
  6345. int low, high, sp = 0, max = 0;
  6346. if ( val == 0 )
  6347. return 0;
  6348. low = pc_getstat(sd,type);
  6349. max = pc_maxparameter(sd,(enum e_params)(type-SP_STR));
  6350. if ( low >= max && val > 0 )
  6351. return 0; // Official servers show '0' when max is reached
  6352. high = low + val;
  6353. if ( val < 0 )
  6354. SWAP(low, high);
  6355. for ( ; low < high; low++ )
  6356. sp += PC_STATUS_POINT_COST(low);
  6357. return sp;
  6358. }
  6359. /**
  6360. * Returns the value the specified stat can be increased by with the current
  6361. * amount of available status points for the current character's class.
  6362. *
  6363. * @param sd The target character.
  6364. * @param type Stat to verify.
  6365. * @return Maximum value the stat could grow by.
  6366. */
  6367. int pc_maxparameterincrease(struct map_session_data* sd, int type)
  6368. {
  6369. int base, final_val, status_points, max_param;
  6370. nullpo_ret(sd);
  6371. base = final_val = pc_getstat(sd, type);
  6372. status_points = sd->status.status_point;
  6373. max_param = pc_maxparameter(sd, (enum e_params)(type-SP_STR));
  6374. while (final_val <= max_param && status_points >= 0) {
  6375. status_points -= PC_STATUS_POINT_COST(final_val);
  6376. final_val++;
  6377. }
  6378. final_val--;
  6379. return (final_val > base ? final_val-base : 0);
  6380. }
  6381. /**
  6382. * Raises a stat by the specified amount.
  6383. *
  6384. * Obeys max_parameter limits.
  6385. * Subtracts status points according to the cost of the increased stat points.
  6386. *
  6387. * @param sd The target character.
  6388. * @param type The stat to change (see enum _sp)
  6389. * @param increase The stat increase (strictly positive) amount.
  6390. * @retval true if the stat was increased by any amount.
  6391. * @retval false if there were no changes.
  6392. */
  6393. bool pc_statusup(struct map_session_data* sd, int type, int increase)
  6394. {
  6395. int max_increase = 0, current = 0, needed_points = 0, final_value = 0;
  6396. nullpo_ret(sd);
  6397. // check conditions
  6398. if (type < SP_STR || type > SP_LUK || increase <= 0) {
  6399. clif_statusupack(sd, type, 0, 0);
  6400. return false;
  6401. }
  6402. // check limits
  6403. current = pc_getstat(sd, type);
  6404. max_increase = pc_maxparameterincrease(sd, type);
  6405. increase = cap_value(increase, 0, max_increase); // cap to the maximum status points available
  6406. if (increase <= 0 || current + increase > pc_maxparameter(sd, (enum e_params)(type-SP_STR))) {
  6407. clif_statusupack(sd, type, 0, 0);
  6408. return false;
  6409. }
  6410. // check status points
  6411. needed_points = pc_need_status_point(sd, type, increase);
  6412. if (needed_points < 0 || needed_points > sd->status.status_point) { // Sanity check
  6413. clif_statusupack(sd, type, 0, 0);
  6414. return false;
  6415. }
  6416. // set new values
  6417. final_value = pc_setstat(sd, type, current + increase);
  6418. sd->status.status_point -= needed_points;
  6419. status_calc_pc(sd,SCO_NONE);
  6420. // update increase cost indicator
  6421. clif_updatestatus(sd, SP_USTR + type-SP_STR);
  6422. // update statpoint count
  6423. clif_updatestatus(sd, SP_STATUSPOINT);
  6424. // update stat value
  6425. clif_statusupack(sd, type, 1, final_value); // required
  6426. if( final_value > 255 )
  6427. clif_updatestatus(sd, type); // send after the 'ack' to override the truncated value
  6428. achievement_update_objective(sd, AG_GOAL_STATUS, 1, final_value);
  6429. return true;
  6430. }
  6431. /**
  6432. * Raises a stat by the specified amount.
  6433. *
  6434. * Obeys max_parameter limits.
  6435. * Does not subtract status points for the cost of the modified stat points.
  6436. *
  6437. * @param sd The target character.
  6438. * @param type The stat to change (see enum _sp)
  6439. * @param val The stat increase (or decrease) amount.
  6440. * @return the stat increase amount.
  6441. * @retval 0 if no changes were made.
  6442. */
  6443. int pc_statusup2(struct map_session_data* sd, int type, int val)
  6444. {
  6445. int max, need;
  6446. nullpo_ret(sd);
  6447. if( type < SP_STR || type > SP_LUK )
  6448. {
  6449. clif_statusupack(sd,type,0,0);
  6450. return 0;
  6451. }
  6452. need = pc_need_status_point(sd,type,1);
  6453. max = pc_maxparameter(sd,(enum e_params)(type-SP_STR)); // set new value
  6454. val = pc_setstat(sd, type, cap_value(pc_getstat(sd,type) + val, 1, max));
  6455. status_calc_pc(sd,SCO_NONE);
  6456. // update increase cost indicator
  6457. if( need != pc_need_status_point(sd,type,1) )
  6458. clif_updatestatus(sd, SP_USTR + type-SP_STR);
  6459. // update stat value
  6460. clif_statusupack(sd,type,1,val); // required
  6461. if( val > 255 )
  6462. clif_updatestatus(sd,type); // send after the 'ack' to override the truncated value
  6463. return val;
  6464. }
  6465. /*==========================================
  6466. * Update skill_lv for player sd
  6467. * Skill point allocation
  6468. *------------------------------------------*/
  6469. void pc_skillup(struct map_session_data *sd,uint16 skill_id)
  6470. {
  6471. uint16 idx = skill_get_index(skill_id);
  6472. nullpo_retv(sd);
  6473. if (!idx) {
  6474. if (skill_id)
  6475. ShowError("pc_skillup: Player attempts to level up invalid skill '%d'\n", skill_id);
  6476. return;
  6477. }
  6478. // Level up guild skill
  6479. if (SKILL_CHK_GUILD(skill_id)) {
  6480. guild_skillup(sd, skill_id);
  6481. return;
  6482. }
  6483. // Level up homunculus skill
  6484. else if (sd->hd && SKILL_CHK_HOMUN(skill_id)) {
  6485. hom_skillup(sd->hd, skill_id);
  6486. return;
  6487. }
  6488. else {
  6489. if( sd->status.skill_point > 0 &&
  6490. sd->status.skill[idx].id &&
  6491. sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT && //Don't allow raising while you have granted skills. [Skotlex]
  6492. sd->status.skill[idx].lv < skill_tree_get_max(skill_id, sd->status.class_) )
  6493. {
  6494. int lv, range, upgradable;
  6495. sd->status.skill[idx].lv++;
  6496. sd->status.skill_point--;
  6497. if( !skill_get_inf(skill_id) )
  6498. status_calc_pc(sd,SCO_NONE); // Only recalculate for passive skills.
  6499. else if( sd->status.skill_point == 0 && pc_is_taekwon_ranker(sd) )
  6500. pc_calc_skilltree(sd); // Required to grant all TK Ranker skills.
  6501. else
  6502. pc_check_skilltree(sd); // Check if a new skill can Lvlup
  6503. lv = sd->status.skill[idx].lv;
  6504. range = skill_get_range2(&sd->bl, skill_id, lv, false);
  6505. upgradable = (lv < skill_tree_get_max(sd->status.skill[idx].id, sd->status.class_)) ? 1 : 0;
  6506. clif_skillup(sd,skill_id,lv,range,upgradable);
  6507. clif_updatestatus(sd,SP_SKILLPOINT);
  6508. if( skill_id == GN_REMODELING_CART ) /* cart weight info was updated by status_calc_pc */
  6509. clif_updatestatus(sd,SP_CARTINFO);
  6510. if (!pc_has_permission(sd, PC_PERM_ALL_SKILL)) // may skill everything at any time anyways, and this would cause a huge slowdown
  6511. clif_skillinfoblock(sd);
  6512. }
  6513. //else
  6514. // 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);
  6515. }
  6516. }
  6517. /*==========================================
  6518. * /allskill
  6519. *------------------------------------------*/
  6520. int pc_allskillup(struct map_session_data *sd)
  6521. {
  6522. int i;
  6523. nullpo_ret(sd);
  6524. for (i = 0; i < MAX_SKILL; i++) {
  6525. 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) {
  6526. sd->status.skill[i].lv = (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY) ? 0 : sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
  6527. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  6528. if (sd->status.skill[i].lv == 0)
  6529. sd->status.skill[i].id = 0;
  6530. }
  6531. }
  6532. if (!pc_grant_allskills(sd, true)) {
  6533. uint16 sk_id;
  6534. for (i = 0; i < MAX_SKILL_TREE && (sk_id = skill_tree[pc_class2idx(sd->status.class_)][i].skill_id) > 0;i++){
  6535. int inf2 = 0;
  6536. uint16 sk_idx = 0;
  6537. if (!sk_id || !(sk_idx = skill_get_index(sk_id)))
  6538. continue;
  6539. inf2 = skill_get_inf2(sk_id);
  6540. if (
  6541. (inf2&INF2_QUEST_SKILL && !battle_config.quest_skill_learn) ||
  6542. (inf2&(INF2_WEDDING_SKILL|INF2_SPIRIT_SKILL)) ||
  6543. sk_id == SG_DEVIL
  6544. )
  6545. continue; //Cannot be learned normally.
  6546. sd->status.skill[sk_idx].id = sk_id;
  6547. sd->status.skill[sk_idx].lv = skill_tree_get_max(sk_id, sd->status.class_); // celest
  6548. }
  6549. }
  6550. status_calc_pc(sd,SCO_NONE);
  6551. //Required because if you could level up all skills previously,
  6552. //the update will not be sent as only the lv variable changes.
  6553. clif_skillinfoblock(sd);
  6554. return 0;
  6555. }
  6556. /*==========================================
  6557. * /resetlvl
  6558. *------------------------------------------*/
  6559. int pc_resetlvl(struct map_session_data* sd,int type)
  6560. {
  6561. int i;
  6562. nullpo_ret(sd);
  6563. if (type != 3) //Also reset skills
  6564. pc_resetskill(sd, 0);
  6565. if(type == 1){
  6566. sd->status.skill_point=0;
  6567. sd->status.base_level=1;
  6568. sd->status.job_level=1;
  6569. sd->status.base_exp=0;
  6570. sd->status.job_exp=0;
  6571. if(sd->sc.option !=0)
  6572. sd->sc.option = 0;
  6573. sd->status.str=1;
  6574. sd->status.agi=1;
  6575. sd->status.vit=1;
  6576. sd->status.int_=1;
  6577. sd->status.dex=1;
  6578. sd->status.luk=1;
  6579. if(sd->status.class_ == JOB_NOVICE_HIGH) {
  6580. sd->status.status_point=100; // not 88 [celest]
  6581. // give platinum skills upon changing
  6582. pc_skill(sd,NV_FIRSTAID,1,ADDSKILL_PERMANENT);
  6583. pc_skill(sd,NV_TRICKDEAD,1,ADDSKILL_PERMANENT);
  6584. }
  6585. }
  6586. if(type == 2){
  6587. sd->status.skill_point=0;
  6588. sd->status.base_level=1;
  6589. sd->status.job_level=1;
  6590. sd->status.base_exp=0;
  6591. sd->status.job_exp=0;
  6592. }
  6593. if(type == 3){
  6594. sd->status.base_level=1;
  6595. sd->status.base_exp=0;
  6596. }
  6597. if(type == 4){
  6598. sd->status.job_level=1;
  6599. sd->status.job_exp=0;
  6600. }
  6601. clif_updatestatus(sd,SP_STATUSPOINT);
  6602. clif_updatestatus(sd,SP_STR);
  6603. clif_updatestatus(sd,SP_AGI);
  6604. clif_updatestatus(sd,SP_VIT);
  6605. clif_updatestatus(sd,SP_INT);
  6606. clif_updatestatus(sd,SP_DEX);
  6607. clif_updatestatus(sd,SP_LUK);
  6608. clif_updatestatus(sd,SP_BASELEVEL);
  6609. clif_updatestatus(sd,SP_JOBLEVEL);
  6610. clif_updatestatus(sd,SP_STATUSPOINT);
  6611. clif_updatestatus(sd,SP_BASEEXP);
  6612. clif_updatestatus(sd,SP_JOBEXP);
  6613. clif_updatestatus(sd,SP_NEXTBASEEXP);
  6614. clif_updatestatus(sd,SP_NEXTJOBEXP);
  6615. clif_updatestatus(sd,SP_SKILLPOINT);
  6616. clif_updatestatus(sd,SP_USTR); // Updates needed stat points - Valaris
  6617. clif_updatestatus(sd,SP_UAGI);
  6618. clif_updatestatus(sd,SP_UVIT);
  6619. clif_updatestatus(sd,SP_UINT);
  6620. clif_updatestatus(sd,SP_UDEX);
  6621. clif_updatestatus(sd,SP_ULUK); // End Addition
  6622. for(i=0;i<EQI_MAX;i++) { // unequip items that can't be equipped by base 1 [Valaris]
  6623. if(sd->equip_index[i] >= 0)
  6624. if(pc_isequip(sd,sd->equip_index[i]))
  6625. pc_unequipitem(sd,sd->equip_index[i],2);
  6626. }
  6627. if ((type == 1 || type == 2 || type == 3) && sd->status.party_id)
  6628. party_send_levelup(sd);
  6629. status_calc_pc(sd, SCO_FORCE);
  6630. clif_skillinfoblock(sd);
  6631. return 0;
  6632. }
  6633. /*==========================================
  6634. * /resetstate
  6635. *------------------------------------------*/
  6636. int pc_resetstate(struct map_session_data* sd)
  6637. {
  6638. nullpo_ret(sd);
  6639. if (battle_config.use_statpoint_table)
  6640. { // New statpoint table used here - Dexity
  6641. if (sd->status.base_level > MAX_LEVEL)
  6642. { //statp[] goes out of bounds, can't reset!
  6643. ShowError("pc_resetstate: Can't reset stats of %d:%d, the base level (%d) is greater than the max level supported (%d)\n",
  6644. sd->status.account_id, sd->status.char_id, sd->status.base_level, MAX_LEVEL);
  6645. return 0;
  6646. }
  6647. sd->status.status_point = statp[sd->status.base_level] + ( sd->class_&JOBL_UPPER ? 52 : 0 ); // extra 52+48=100 stat points
  6648. }
  6649. else
  6650. {
  6651. int add=0;
  6652. add += pc_need_status_point(sd, SP_STR, 1-pc_getstat(sd, SP_STR));
  6653. add += pc_need_status_point(sd, SP_AGI, 1-pc_getstat(sd, SP_AGI));
  6654. add += pc_need_status_point(sd, SP_VIT, 1-pc_getstat(sd, SP_VIT));
  6655. add += pc_need_status_point(sd, SP_INT, 1-pc_getstat(sd, SP_INT));
  6656. add += pc_need_status_point(sd, SP_DEX, 1-pc_getstat(sd, SP_DEX));
  6657. add += pc_need_status_point(sd, SP_LUK, 1-pc_getstat(sd, SP_LUK));
  6658. sd->status.status_point+=add;
  6659. }
  6660. pc_setstat(sd, SP_STR, 1);
  6661. pc_setstat(sd, SP_AGI, 1);
  6662. pc_setstat(sd, SP_VIT, 1);
  6663. pc_setstat(sd, SP_INT, 1);
  6664. pc_setstat(sd, SP_DEX, 1);
  6665. pc_setstat(sd, SP_LUK, 1);
  6666. clif_updatestatus(sd,SP_STR);
  6667. clif_updatestatus(sd,SP_AGI);
  6668. clif_updatestatus(sd,SP_VIT);
  6669. clif_updatestatus(sd,SP_INT);
  6670. clif_updatestatus(sd,SP_DEX);
  6671. clif_updatestatus(sd,SP_LUK);
  6672. clif_updatestatus(sd,SP_USTR); // Updates needed stat points - Valaris
  6673. clif_updatestatus(sd,SP_UAGI);
  6674. clif_updatestatus(sd,SP_UVIT);
  6675. clif_updatestatus(sd,SP_UINT);
  6676. clif_updatestatus(sd,SP_UDEX);
  6677. clif_updatestatus(sd,SP_ULUK); // End Addition
  6678. clif_updatestatus(sd,SP_STATUSPOINT);
  6679. if( sd->mission_mobid ) { //bugreport:2200
  6680. sd->mission_mobid = 0;
  6681. sd->mission_count = 0;
  6682. pc_setglobalreg(sd, add_str(TKMISSIONID_VAR), 0);
  6683. }
  6684. status_calc_pc(sd, SCO_NONE);
  6685. return 1;
  6686. }
  6687. /*==========================================
  6688. * /resetskill
  6689. * if flag&1, perform block resync and status_calc call.
  6690. * if flag&2, just count total amount of skill points used by player, do not really reset.
  6691. * if flag&4, just reset the skills if the player class is a bard/dancer type (for changesex.)
  6692. *------------------------------------------*/
  6693. int pc_resetskill(struct map_session_data* sd, int flag)
  6694. {
  6695. int i, skill_point=0;
  6696. nullpo_ret(sd);
  6697. if( flag&4 && (sd->class_&MAPID_UPPERMASK) != MAPID_BARDDANCER )
  6698. return 0;
  6699. if( !(flag&2) ) { //Remove stuff lost when resetting skills.
  6700. /**
  6701. * 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)
  6702. **/
  6703. if( pc_is_taekwon_ranker(sd) )
  6704. return 0;
  6705. if( pc_checkskill(sd, SG_DEVIL) && pc_is_maxjoblv(sd) )
  6706. clif_status_load(&sd->bl, EFST_DEVIL1, 0); //Remove perma blindness due to skill-reset. [Skotlex]
  6707. i = sd->sc.option;
  6708. if( i&OPTION_RIDING && pc_checkskill(sd, KN_RIDING) )
  6709. i &= ~OPTION_RIDING;
  6710. if( i&OPTION_FALCON && pc_checkskill(sd, HT_FALCON) )
  6711. i &= ~OPTION_FALCON;
  6712. if( i&OPTION_DRAGON && pc_checkskill(sd, RK_DRAGONTRAINING) )
  6713. i &= ~OPTION_DRAGON;
  6714. if( i&OPTION_WUG && pc_checkskill(sd, RA_WUGMASTERY) )
  6715. i &= ~OPTION_WUG;
  6716. if( i&OPTION_WUGRIDER && pc_checkskill(sd, RA_WUGRIDER) )
  6717. i &= ~OPTION_WUGRIDER;
  6718. if( i&OPTION_MADOGEAR && ( sd->class_&MAPID_THIRDMASK ) == MAPID_MECHANIC )
  6719. i &= ~OPTION_MADOGEAR;
  6720. #ifndef NEW_CARTS
  6721. if( i&OPTION_CART && pc_checkskill(sd, MC_PUSHCART) )
  6722. i &= ~OPTION_CART;
  6723. #else
  6724. if( sd->sc.data[SC_PUSH_CART] )
  6725. pc_setcart(sd, 0);
  6726. #endif
  6727. if( i != sd->sc.option )
  6728. pc_setoption(sd, i);
  6729. if( hom_is_active(sd->hd) && pc_checkskill(sd, AM_CALLHOMUN) )
  6730. hom_vaporize(sd, HOM_ST_ACTIVE);
  6731. if (sd->sc.data[SC_SPRITEMABLE] && pc_checkskill(sd, SU_SPRITEMABLE))
  6732. status_change_end(&sd->bl, SC_SPRITEMABLE, INVALID_TIMER);
  6733. }
  6734. for( i = 1; i < MAX_SKILL; i++ )
  6735. {
  6736. uint8 lv = sd->status.skill[i].lv;
  6737. int inf2;
  6738. uint16 skill_id = skill_idx2id(i);
  6739. if (lv == 0 || skill_id == 0)
  6740. continue;
  6741. inf2 = skill_get_inf2(skill_id);
  6742. if( inf2&(INF2_WEDDING_SKILL|INF2_SPIRIT_SKILL) ) //Avoid reseting wedding/linker skills.
  6743. continue;
  6744. // Don't reset trick dead if not a novice/baby
  6745. if( skill_id == NV_TRICKDEAD && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE )
  6746. {
  6747. sd->status.skill[i].lv = 0;
  6748. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  6749. continue;
  6750. }
  6751. // do not reset basic skill
  6752. if (skill_id == NV_BASIC && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE )
  6753. continue;
  6754. if( sd->status.skill[i].flag == SKILL_FLAG_PERM_GRANTED )
  6755. continue;
  6756. if( flag&4 && !skill_ischangesex(skill_id) )
  6757. continue;
  6758. if( inf2&INF2_QUEST_SKILL && !battle_config.quest_skill_learn )
  6759. { //Only handle quest skills in a special way when you can't learn them manually
  6760. if( battle_config.quest_skill_reset && !(flag&2) )
  6761. { //Wipe them
  6762. sd->status.skill[i].lv = 0;
  6763. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  6764. }
  6765. continue;
  6766. }
  6767. if( sd->status.skill[i].flag == SKILL_FLAG_PERMANENT )
  6768. skill_point += lv;
  6769. else
  6770. if( sd->status.skill[i].flag >= SKILL_FLAG_REPLACED_LV_0 )
  6771. skill_point += (sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0);
  6772. if( !(flag&2) )
  6773. {// reset
  6774. sd->status.skill[i].lv = 0;
  6775. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  6776. }
  6777. }
  6778. if( flag&2 || !skill_point ) return skill_point;
  6779. sd->status.skill_point += skill_point;
  6780. if (flag&1) {
  6781. clif_updatestatus(sd,SP_SKILLPOINT);
  6782. clif_skillinfoblock(sd);
  6783. status_calc_pc(sd, SCO_FORCE);
  6784. }
  6785. return skill_point;
  6786. }
  6787. /*==========================================
  6788. * /resetfeel [Komurka]
  6789. *------------------------------------------*/
  6790. int pc_resetfeel(struct map_session_data* sd)
  6791. {
  6792. int i;
  6793. nullpo_ret(sd);
  6794. for (i=0; i<MAX_PC_FEELHATE; i++)
  6795. {
  6796. sd->feel_map[i].m = -1;
  6797. sd->feel_map[i].index = 0;
  6798. pc_setglobalreg(sd, add_str(sg_info[i].feel_var), 0);
  6799. }
  6800. return 0;
  6801. }
  6802. int pc_resethate(struct map_session_data* sd)
  6803. {
  6804. int i;
  6805. nullpo_ret(sd);
  6806. for (i=0; i<3; i++)
  6807. {
  6808. sd->hate_mob[i] = -1;
  6809. pc_setglobalreg(sd, add_str(sg_info[i].hate_var), 0);
  6810. }
  6811. return 0;
  6812. }
  6813. int pc_skillatk_bonus(struct map_session_data *sd, uint16 skill_id)
  6814. {
  6815. int bonus = 0;
  6816. nullpo_ret(sd);
  6817. skill_id = skill_dummy2skill_id(skill_id);
  6818. for (auto &it : sd->skillatk) {
  6819. if (it.id == skill_id) {
  6820. bonus += it.val;
  6821. break;
  6822. }
  6823. }
  6824. return bonus;
  6825. }
  6826. int pc_sub_skillatk_bonus(struct map_session_data *sd, uint16 skill_id)
  6827. {
  6828. int bonus = 0;
  6829. nullpo_ret(sd);
  6830. skill_id = skill_dummy2skill_id(skill_id);
  6831. for (auto &it : sd->subskill) {
  6832. if (it.id == skill_id) {
  6833. bonus += it.val;
  6834. break;
  6835. }
  6836. }
  6837. return bonus;
  6838. }
  6839. int pc_skillheal_bonus(struct map_session_data *sd, uint16 skill_id) {
  6840. int bonus = sd->bonus.add_heal_rate;
  6841. nullpo_ret(sd);
  6842. skill_id = skill_dummy2skill_id(skill_id);
  6843. if( bonus ) {
  6844. switch( skill_id ) {
  6845. case AL_HEAL: if( !(battle_config.skill_add_heal_rate&1) ) bonus = 0; break;
  6846. case PR_SANCTUARY: if( !(battle_config.skill_add_heal_rate&2) ) bonus = 0; break;
  6847. case AM_POTIONPITCHER: if( !(battle_config.skill_add_heal_rate&4) ) bonus = 0; break;
  6848. case CR_SLIMPITCHER: if( !(battle_config.skill_add_heal_rate&8) ) bonus = 0; break;
  6849. case BA_APPLEIDUN: if( !(battle_config.skill_add_heal_rate&16)) bonus = 0; break;
  6850. }
  6851. }
  6852. for (auto &it : sd->skillheal) {
  6853. if (it.id == skill_id) {
  6854. bonus += it.val;
  6855. break;
  6856. }
  6857. }
  6858. return bonus;
  6859. }
  6860. int pc_skillheal2_bonus(struct map_session_data *sd, uint16 skill_id) {
  6861. int bonus = sd->bonus.add_heal2_rate;
  6862. skill_id = skill_dummy2skill_id(skill_id);
  6863. for (auto &it : sd->skillheal2) {
  6864. if (it.id == skill_id) {
  6865. bonus += it.val;
  6866. break;
  6867. }
  6868. }
  6869. return bonus;
  6870. }
  6871. void pc_respawn(struct map_session_data* sd, clr_type clrtype)
  6872. {
  6873. if( !pc_isdead(sd) )
  6874. return; // not applicable
  6875. if( sd->bg_id && bg_member_respawn(sd) )
  6876. return; // member revived by battleground
  6877. pc_setstand(sd, true);
  6878. pc_setrestartvalue(sd,3);
  6879. if( pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, clrtype) != SETPOS_OK )
  6880. clif_resurrection(&sd->bl, 1); //If warping fails, send a normal stand up packet.
  6881. }
  6882. static TIMER_FUNC(pc_respawn_timer){
  6883. struct map_session_data *sd = map_id2sd(id);
  6884. if( sd != NULL )
  6885. {
  6886. sd->pvp_point=0;
  6887. pc_respawn(sd,CLR_OUTSIGHT);
  6888. }
  6889. return 0;
  6890. }
  6891. /*==========================================
  6892. * Invoked when a player has received damage
  6893. *------------------------------------------*/
  6894. void pc_damage(struct map_session_data *sd,struct block_list *src,unsigned int hp, unsigned int sp)
  6895. {
  6896. if (sp) clif_updatestatus(sd,SP_SP);
  6897. if (hp) clif_updatestatus(sd,SP_HP);
  6898. else return;
  6899. if (!src)
  6900. return;
  6901. if( pc_issit(sd) ) {
  6902. pc_setstand(sd, true);
  6903. skill_sit(sd,0);
  6904. }
  6905. if (sd->progressbar.npc_id)
  6906. clif_progressbar_abort(sd);
  6907. if( sd->status.pet_id > 0 && sd->pd && battle_config.pet_damage_support )
  6908. pet_target_check(sd->pd,src,1);
  6909. if( sd->status.ele_id > 0 )
  6910. elemental_set_target(sd,src);
  6911. if(battle_config.prevent_logout_trigger&PLT_DAMAGE)
  6912. sd->canlog_tick = gettick();
  6913. }
  6914. TIMER_FUNC(pc_close_npc_timer){
  6915. TBL_PC *sd = map_id2sd(id);
  6916. if(sd) pc_close_npc(sd,data);
  6917. return 0;
  6918. }
  6919. /**
  6920. * Method to properly close a NPC for player and clear anything related.
  6921. * @param sd: Player attached
  6922. * @param flag: Method of closure
  6923. * 1: Produce a close button and end the NPC
  6924. * 2: End the NPC (best for no dialog windows)
  6925. */
  6926. void pc_close_npc(struct map_session_data *sd,int flag)
  6927. {
  6928. nullpo_retv(sd);
  6929. if (sd->npc_id || sd->npc_shopid) {
  6930. if (sd->state.using_fake_npc) {
  6931. clif_clearunit_single(sd->npc_id, CLR_OUTSIGHT, sd->fd);
  6932. sd->state.using_fake_npc = 0;
  6933. }
  6934. if (sd->st) {
  6935. if(sd->st->state == RUN){ //wait ending code execution
  6936. add_timer(gettick()+500,pc_close_npc_timer,sd->bl.id,flag);
  6937. return;
  6938. }
  6939. sd->st->state = ((flag==1 && sd->st->mes_active)?CLOSE:END);
  6940. sd->st->mes_active = 0;
  6941. }
  6942. sd->state.menu_or_input = 0;
  6943. sd->npc_menu = 0;
  6944. sd->npc_shopid = 0;
  6945. #ifdef SECURE_NPCTIMEOUT
  6946. sd->npc_idle_timer = INVALID_TIMER;
  6947. #endif
  6948. if (sd->st) {
  6949. if (sd->st->state == CLOSE) {
  6950. clif_scriptclose(sd, sd->npc_id);
  6951. clif_scriptclear(sd, sd->npc_id); // [Ind/Hercules]
  6952. sd->st->state = END; // Force to end now
  6953. }
  6954. if (sd->st->state == END) { // free attached scripts that are waiting
  6955. script_free_state(sd->st);
  6956. sd->st = NULL;
  6957. sd->npc_id = 0;
  6958. }
  6959. }
  6960. }
  6961. }
  6962. /*==========================================
  6963. * Invoked when a player has negative current hp
  6964. *------------------------------------------*/
  6965. int pc_dead(struct map_session_data *sd,struct block_list *src)
  6966. {
  6967. int i=0,k=0;
  6968. t_tick tick = gettick();
  6969. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  6970. // Activate Steel body if a super novice dies at 99+% exp [celest]
  6971. // Super Novices have no kill or die functions attached when saved by their angel
  6972. if (!sd->state.snovice_dead_flag && (sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE) {
  6973. unsigned int exp = pc_nextbaseexp(sd);
  6974. if( exp && get_percentage(sd->status.base_exp,exp) >= 99 ) {
  6975. sd->state.snovice_dead_flag = 1;
  6976. pc_setrestartvalue(sd,1);
  6977. status_percent_heal(&sd->bl, 100, 100);
  6978. clif_resurrection(&sd->bl, 1);
  6979. if(battle_config.pc_invincible_time)
  6980. pc_setinvincibletimer(sd, battle_config.pc_invincible_time);
  6981. sc_start(&sd->bl,&sd->bl,status_skill2sc(MO_STEELBODY),100,5,skill_get_time(MO_STEELBODY,5));
  6982. if(mapdata_flag_gvg2(mapdata))
  6983. pc_respawn_timer(INVALID_TIMER, gettick(), sd->bl.id, 0);
  6984. return 0;
  6985. }
  6986. }
  6987. for(k = 0; k < MAX_DEVOTION; k++) {
  6988. if (sd->devotion[k]){
  6989. struct map_session_data *devsd = map_id2sd(sd->devotion[k]);
  6990. if (devsd)
  6991. status_change_end(&devsd->bl, SC_DEVOTION, INVALID_TIMER);
  6992. sd->devotion[k] = 0;
  6993. }
  6994. }
  6995. if(sd->shadowform_id) { //if we were target of shadowform
  6996. status_change_end(map_id2bl(sd->shadowform_id), SC__SHADOWFORM, INVALID_TIMER);
  6997. sd->shadowform_id = 0; //should be remove on status end anyway
  6998. }
  6999. if(sd->status.pet_id > 0 && sd->pd) {
  7000. struct pet_data *pd = sd->pd;
  7001. if( !mapdata->flag[MF_NOEXPPENALTY] ) {
  7002. pet_set_intimate(pd, pd->pet.intimate + pd->get_pet_db()->die);
  7003. if( pd->pet.intimate < 0 )
  7004. pd->pet.intimate = 0;
  7005. clif_send_petdata(sd,sd->pd,1,pd->pet.intimate);
  7006. }
  7007. if( sd->pd->target_id ) // Unlock all targets...
  7008. pet_unlocktarget(sd->pd);
  7009. }
  7010. 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)
  7011. hom_vaporize(sd, HOM_ST_ACTIVE);
  7012. if( sd->md )
  7013. mercenary_delete(sd->md, 3); // Your mercenary soldier has ran away.
  7014. if( sd->ed )
  7015. elemental_delete(sd->ed);
  7016. // Leave duel if you die [LuzZza]
  7017. if(battle_config.duel_autoleave_when_die) {
  7018. if(sd->duel_group > 0)
  7019. duel_leave(sd->duel_group, sd);
  7020. if(sd->duel_invite > 0)
  7021. duel_reject(sd->duel_invite, sd);
  7022. }
  7023. pc_close_npc(sd,2); //close npc if we were using one
  7024. /* e.g. not killed thru pc_damage */
  7025. if( pc_issit(sd) ) {
  7026. clif_status_load(&sd->bl,EFST_SIT,0);
  7027. }
  7028. pc_setdead(sd);
  7029. clif_party_dead( sd );
  7030. pc_setglobalreg(sd, add_str(PCDIECOUNTER_VAR), sd->die_counter+1);
  7031. pc_setparam(sd, SP_KILLERRID, src?src->id:0);
  7032. //Reset menu skills/item skills
  7033. if ((sd->skillitem) != 0)
  7034. sd->skillitem = sd->skillitemlv = 0;
  7035. if ((sd->menuskill_id) != 0)
  7036. sd->menuskill_id = sd->menuskill_val = 0;
  7037. //Reset ticks.
  7038. sd->hp_loss.tick = sd->sp_loss.tick = sd->hp_regen.tick = sd->sp_regen.tick = 0;
  7039. if ( sd->spiritball !=0 )
  7040. pc_delspiritball(sd,sd->spiritball,0);
  7041. if (sd->spiritcharm_type != CHARM_TYPE_NONE && sd->spiritcharm > 0)
  7042. pc_delspiritcharm(sd,sd->spiritcharm,sd->spiritcharm_type);
  7043. if (src)
  7044. switch (src->type) {
  7045. case BL_MOB:
  7046. {
  7047. struct mob_data *md=(struct mob_data *)src;
  7048. if(md->target_id==sd->bl.id)
  7049. mob_unlocktarget(md,tick);
  7050. if(battle_config.mobs_level_up && md->status.hp &&
  7051. (unsigned int)md->level < pc_maxbaselv(sd) &&
  7052. !md->guardian_data && !md->special_state.ai// Guardians/summons should not level. [Skotlex]
  7053. ) { // monster level up [Valaris]
  7054. clif_misceffect(&md->bl,0);
  7055. md->level++;
  7056. status_calc_mob(md, SCO_NONE);
  7057. status_percent_heal(src,10,0);
  7058. if( battle_config.show_mob_info&4 )
  7059. {// update name with new level
  7060. clif_name_area(&md->bl);
  7061. }
  7062. }
  7063. src = battle_get_master(src); // Maybe Player Summon
  7064. }
  7065. break;
  7066. case BL_PET: //Pass on to master...
  7067. case BL_HOM:
  7068. case BL_MER:
  7069. src = battle_get_master(src);
  7070. break;
  7071. }
  7072. if (src && src->type == BL_PC) {
  7073. struct map_session_data *ssd = (struct map_session_data *)src;
  7074. pc_setparam(ssd, SP_KILLEDRID, sd->bl.id);
  7075. npc_script_event(ssd, NPCE_KILLPC);
  7076. if (battle_config.pk_mode&2) {
  7077. ssd->status.manner -= 5;
  7078. if(ssd->status.manner < 0)
  7079. sc_start(&sd->bl,src,SC_NOCHAT,100,0,0);
  7080. #if 0
  7081. // PK/Karma system code (not enabled yet) [celest]
  7082. // originally from Kade Online, so i don't know if any of these is correct ^^;
  7083. // note: karma is measured REVERSE, so more karma = more 'evil' / less honourable,
  7084. // karma going down = more 'good' / more honourable.
  7085. // The Karma System way...
  7086. if (sd->status.karma > ssd->status.karma) { // If player killed was more evil
  7087. sd->status.karma--;
  7088. ssd->status.karma--;
  7089. }
  7090. else if (sd->status.karma < ssd->status.karma) // If player killed was more good
  7091. ssd->status.karma++;
  7092. // or the PK System way...
  7093. if (sd->status.karma > 0) // player killed is dishonourable?
  7094. ssd->status.karma--; // honour points earned
  7095. sd->status.karma++; // honour points lost
  7096. // To-do: Receive exp on certain occasions
  7097. #endif
  7098. }
  7099. }
  7100. if(battle_config.bone_drop==2
  7101. || (battle_config.bone_drop==1 && mapdata->flag[MF_PVP]))
  7102. {
  7103. struct item item_tmp;
  7104. memset(&item_tmp,0,sizeof(item_tmp));
  7105. item_tmp.nameid=ITEMID_SKULL_;
  7106. item_tmp.identify=1;
  7107. item_tmp.card[0]=CARD0_CREATE;
  7108. item_tmp.card[1]=0;
  7109. item_tmp.card[2]=GetWord(sd->status.char_id,0); // CharId
  7110. item_tmp.card[3]=GetWord(sd->status.char_id,1);
  7111. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  7112. }
  7113. //Remove bonus_script when dead
  7114. pc_bonus_script_clear(sd,BSF_REM_ON_DEAD);
  7115. // changed penalty options, added death by player if pk_mode [Valaris]
  7116. if(battle_config.death_penalty_type
  7117. && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE // only novices will receive no penalty
  7118. && !sd->sc.data[SC_BABY] && !sd->sc.data[SC_LIFEINSURANCE]
  7119. && !mapdata->flag[MF_NOEXPPENALTY] && !mapdata_flag_gvg2(mapdata))
  7120. {
  7121. uint32 base_penalty = 0;
  7122. uint32 job_penalty = 0;
  7123. uint32 zeny_penalty = 0;
  7124. if (pc_isvip(sd)) { // EXP penalty for VIP
  7125. base_penalty = battle_config.vip_exp_penalty_base;
  7126. job_penalty = battle_config.vip_exp_penalty_job;
  7127. zeny_penalty = battle_config.vip_zeny_penalty;
  7128. } else {
  7129. base_penalty = battle_config.death_penalty_base;
  7130. job_penalty = battle_config.death_penalty_job;
  7131. zeny_penalty = battle_config.zeny_penalty;
  7132. }
  7133. if ((battle_config.death_penalty_maxlv&1 || !pc_is_maxbaselv(sd)) && base_penalty > 0) {
  7134. switch (battle_config.death_penalty_type) {
  7135. case 1: base_penalty = (uint32) ( pc_nextbaseexp(sd) * ( base_penalty / 10000. ) ); break;
  7136. case 2: base_penalty = (uint32) ( sd->status.base_exp * ( base_penalty / 10000. ) ); break;
  7137. }
  7138. if (base_penalty){ //recheck after altering to speedup
  7139. if (battle_config.pk_mode && src && src->type==BL_PC)
  7140. base_penalty *= 2;
  7141. base_penalty = u32min(sd->status.base_exp, base_penalty);
  7142. }
  7143. }
  7144. else
  7145. base_penalty = 0;
  7146. if ((battle_config.death_penalty_maxlv&2 || !pc_is_maxjoblv(sd)) && job_penalty > 0) {
  7147. switch (battle_config.death_penalty_type) {
  7148. case 1: job_penalty = (uint32) ( pc_nextjobexp(sd) * ( job_penalty / 10000. ) ); break;
  7149. case 2: job_penalty = (uint32) ( sd->status.job_exp * ( job_penalty /10000. ) ); break;
  7150. }
  7151. if (job_penalty) {
  7152. if (battle_config.pk_mode && src && src->type==BL_PC)
  7153. job_penalty *= 2;
  7154. job_penalty = u32min(sd->status.job_exp, job_penalty);
  7155. }
  7156. }
  7157. else
  7158. job_penalty = 0;
  7159. if (base_penalty || job_penalty)
  7160. pc_lostexp(sd, base_penalty, job_penalty);
  7161. if( zeny_penalty > 0 && !mapdata->flag[MF_NOZENYPENALTY]) {
  7162. zeny_penalty = (uint32)( sd->status.zeny * ( zeny_penalty / 10000. ) );
  7163. if(zeny_penalty)
  7164. pc_payzeny(sd, zeny_penalty, LOG_TYPE_PICKDROP_PLAYER, NULL);
  7165. }
  7166. }
  7167. if( mapdata->flag[MF_PVP_NIGHTMAREDROP] ) { // Moved this outside so it works when PVP isn't enabled and during pk mode [Ancyker]
  7168. for (const auto &it : mapdata->drop_list) {
  7169. int id = it.drop_id, per = it.drop_per;
  7170. enum e_nightmare_drop_type type = it.drop_type;
  7171. if(id == 0)
  7172. continue;
  7173. if(id == -1){
  7174. int eq_num=0,eq_n[MAX_INVENTORY];
  7175. memset(eq_n,0,sizeof(eq_n));
  7176. for(i=0;i<MAX_INVENTORY;i++) {
  7177. if( (type&NMDT_INVENTORY && !sd->inventory.u.items_inventory[i].equip)
  7178. || (type&NMDT_EQUIP && sd->inventory.u.items_inventory[i].equip)
  7179. || type&NMDT_ALL)
  7180. {
  7181. int l;
  7182. ARR_FIND( 0, MAX_INVENTORY, l, eq_n[l] <= 0 );
  7183. if( l < MAX_INVENTORY )
  7184. eq_n[l] = i;
  7185. eq_num++;
  7186. }
  7187. }
  7188. if(eq_num > 0){
  7189. int n = eq_n[rnd()%eq_num];
  7190. if(rnd()%10000 < per) {
  7191. if(sd->inventory.u.items_inventory[n].equip)
  7192. pc_unequipitem(sd,n,3);
  7193. pc_dropitem(sd,n,1);
  7194. }
  7195. }
  7196. }
  7197. else if(id > 0) {
  7198. for(i=0;i<MAX_INVENTORY;i++){
  7199. if(sd->inventory.u.items_inventory[i].nameid == id
  7200. && rnd()%10000 < per
  7201. && ((type&NMDT_INVENTORY && !sd->inventory.u.items_inventory[i].equip)
  7202. || (type&NMDT_EQUIP && sd->inventory.u.items_inventory[i].equip)
  7203. || type&NMDT_ALL) ){
  7204. if(sd->inventory.u.items_inventory[i].equip)
  7205. pc_unequipitem(sd,i,3);
  7206. pc_dropitem(sd,i,1);
  7207. break;
  7208. }
  7209. }
  7210. }
  7211. }
  7212. }
  7213. // pvp
  7214. // disable certain pvp functions on pk_mode [Valaris]
  7215. if( !battle_config.pk_mode && mapdata->flag[MF_PVP] && !mapdata->flag[MF_PVP_NOCALCRANK] ) {
  7216. sd->pvp_point -= 5;
  7217. sd->pvp_lost++;
  7218. if( src && src->type == BL_PC ) {
  7219. struct map_session_data *ssd = (struct map_session_data *)src;
  7220. ssd->pvp_point++;
  7221. ssd->pvp_won++;
  7222. }
  7223. if( sd->pvp_point < 0 ) {
  7224. add_timer(tick+1000, pc_respawn_timer,sd->bl.id,0);
  7225. return 1|8;
  7226. }
  7227. }
  7228. //GvG
  7229. if( mapdata_flag_gvg2(mapdata) ) {
  7230. add_timer(tick+1000, pc_respawn_timer, sd->bl.id, 0);
  7231. return 1|8;
  7232. }
  7233. else if( sd->bg_id ) {
  7234. struct battleground_data *bg = bg_team_search(sd->bg_id);
  7235. if( bg && bg->mapindex > 0 ) { // Respawn by BG
  7236. add_timer(tick+1000, pc_respawn_timer, sd->bl.id, 0);
  7237. return 1|8;
  7238. }
  7239. }
  7240. //Reset "can log out" tick.
  7241. if( battle_config.prevent_logout )
  7242. sd->canlog_tick = gettick() - battle_config.prevent_logout;
  7243. return 1;
  7244. }
  7245. void pc_revive(struct map_session_data *sd,unsigned int hp, unsigned int sp) {
  7246. if(hp) clif_updatestatus(sd,SP_HP);
  7247. if(sp) clif_updatestatus(sd,SP_SP);
  7248. pc_setstand(sd, true);
  7249. if(battle_config.pc_invincible_time > 0)
  7250. pc_setinvincibletimer(sd, battle_config.pc_invincible_time);
  7251. if( sd->state.gmaster_flag ) {
  7252. guild_guildaura_refresh(sd,GD_LEADERSHIP,guild_checkskill(sd->guild,GD_LEADERSHIP));
  7253. guild_guildaura_refresh(sd,GD_GLORYWOUNDS,guild_checkskill(sd->guild,GD_GLORYWOUNDS));
  7254. guild_guildaura_refresh(sd,GD_SOULCOLD,guild_checkskill(sd->guild,GD_SOULCOLD));
  7255. guild_guildaura_refresh(sd,GD_HAWKEYES,guild_checkskill(sd->guild,GD_HAWKEYES));
  7256. }
  7257. }
  7258. // script
  7259. //
  7260. /*==========================================
  7261. * script reading pc status registry
  7262. *------------------------------------------*/
  7263. int pc_readparam(struct map_session_data* sd,int type)
  7264. {
  7265. int val = 0;
  7266. nullpo_ret(sd);
  7267. switch(type) {
  7268. case SP_SKILLPOINT: val = sd->status.skill_point; break;
  7269. case SP_STATUSPOINT: val = sd->status.status_point; break;
  7270. case SP_ZENY: val = sd->status.zeny; break;
  7271. case SP_BASELEVEL: val = sd->status.base_level; break;
  7272. case SP_JOBLEVEL: val = sd->status.job_level; break;
  7273. case SP_CLASS: val = sd->status.class_; break;
  7274. case SP_BASEJOB: val = pc_mapid2jobid(sd->class_&MAPID_UPPERMASK, sd->status.sex); break; //Base job, extracting upper type.
  7275. case SP_UPPER: val = sd->class_&JOBL_UPPER?1:(sd->class_&JOBL_BABY?2:0); break;
  7276. case SP_BASECLASS: val = pc_mapid2jobid(sd->class_&MAPID_BASEMASK, sd->status.sex); break; //Extract base class tree. [Skotlex]
  7277. case SP_SEX: val = sd->status.sex; break;
  7278. case SP_WEIGHT: val = sd->weight; break;
  7279. case SP_MAXWEIGHT: val = sd->max_weight; break;
  7280. case SP_BASEEXP: val = sd->status.base_exp; break;
  7281. case SP_JOBEXP: val = sd->status.job_exp; break;
  7282. case SP_NEXTBASEEXP: val = pc_nextbaseexp(sd); break;
  7283. case SP_NEXTJOBEXP: val = pc_nextjobexp(sd); break;
  7284. case SP_HP: val = sd->battle_status.hp; break;
  7285. case SP_MAXHP: val = sd->battle_status.max_hp; break;
  7286. case SP_SP: val = sd->battle_status.sp; break;
  7287. case SP_MAXSP: val = sd->battle_status.max_sp; break;
  7288. case SP_STR: val = sd->status.str; break;
  7289. case SP_AGI: val = sd->status.agi; break;
  7290. case SP_VIT: val = sd->status.vit; break;
  7291. case SP_INT: val = sd->status.int_; break;
  7292. case SP_DEX: val = sd->status.dex; break;
  7293. case SP_LUK: val = sd->status.luk; break;
  7294. case SP_KARMA: val = sd->status.karma; break;
  7295. case SP_MANNER: val = sd->status.manner; break;
  7296. case SP_FAME: val = sd->status.fame; break;
  7297. case SP_KILLERRID: val = sd->killerrid; break;
  7298. case SP_KILLEDRID: val = sd->killedrid; break;
  7299. case SP_KILLEDGID: val = sd->killedgid; break;
  7300. case SP_SITTING: val = pc_issit(sd)?1:0; break;
  7301. case SP_CHARMOVE: val = sd->status.character_moves; break;
  7302. case SP_CHARRENAME: val = sd->status.rename; break;
  7303. case SP_CHARFONT: val = sd->status.font; break;
  7304. case SP_BANK_VAULT: val = sd->bank_vault; break;
  7305. case SP_CASHPOINTS: val = sd->cashPoints; break;
  7306. case SP_KAFRAPOINTS: val = sd->kafraPoints; break;
  7307. case SP_ROULETTE_BRONZE: val = sd->roulette_point.bronze; break;
  7308. case SP_ROULETTE_SILVER: val = sd->roulette_point.silver; break;
  7309. case SP_ROULETTE_GOLD: val = sd->roulette_point.gold; break;
  7310. case SP_PCDIECOUNTER: val = sd->die_counter; break;
  7311. case SP_COOKMASTERY: val = sd->cook_mastery; break;
  7312. case SP_CRITICAL: val = sd->battle_status.cri/10; break;
  7313. case SP_ASPD: val = (2000-sd->battle_status.amotion)/10; break;
  7314. case SP_BASE_ATK:
  7315. #ifdef RENEWAL
  7316. val = sd->bonus.eatk;
  7317. #else
  7318. val = sd->battle_status.batk;
  7319. #endif
  7320. break;
  7321. case SP_DEF1: val = sd->battle_status.def; break;
  7322. case SP_DEF2: val = sd->battle_status.def2; break;
  7323. case SP_MDEF1: val = sd->battle_status.mdef; break;
  7324. case SP_MDEF2: val = sd->battle_status.mdef2; break;
  7325. case SP_HIT: val = sd->battle_status.hit; break;
  7326. case SP_FLEE1: val = sd->battle_status.flee; break;
  7327. case SP_FLEE2: val = sd->battle_status.flee2; break;
  7328. case SP_DEFELE: val = sd->battle_status.def_ele; break;
  7329. case SP_MAXHPRATE: val = sd->hprate; break;
  7330. case SP_MAXSPRATE: val = sd->sprate; break;
  7331. case SP_SPRATE: val = sd->dsprate; break;
  7332. case SP_SPEED_RATE: val = sd->bonus.speed_rate; break;
  7333. case SP_SPEED_ADDRATE: val = sd->bonus.speed_add_rate; break;
  7334. case SP_ASPD_RATE:
  7335. #ifndef RENEWAL_ASPD
  7336. val = sd->battle_status.aspd_rate;
  7337. #else
  7338. val = sd->battle_status.aspd_rate2;
  7339. #endif
  7340. break;
  7341. case SP_HP_RECOV_RATE: val = sd->hprecov_rate; break;
  7342. case SP_SP_RECOV_RATE: val = sd->sprecov_rate; break;
  7343. case SP_CRITICAL_DEF: val = sd->bonus.critical_def; break;
  7344. case SP_NEAR_ATK_DEF: val = sd->bonus.near_attack_def_rate; break;
  7345. case SP_LONG_ATK_DEF: val = sd->bonus.long_attack_def_rate; break;
  7346. case SP_DOUBLE_RATE: val = sd->bonus.double_rate; break;
  7347. case SP_DOUBLE_ADD_RATE: val = sd->bonus.double_add_rate; break;
  7348. case SP_MATK_RATE: val = sd->matk_rate; break;
  7349. case SP_ATK_RATE: val = sd->bonus.atk_rate; break;
  7350. case SP_MAGIC_ATK_DEF: val = sd->bonus.magic_def_rate; break;
  7351. case SP_MISC_ATK_DEF: val = sd->bonus.misc_def_rate; break;
  7352. case SP_PERFECT_HIT_RATE:val = sd->bonus.perfect_hit; break;
  7353. case SP_PERFECT_HIT_ADD_RATE: val = sd->bonus.perfect_hit_add; break;
  7354. case SP_CRITICAL_RATE: val = sd->critical_rate; break;
  7355. case SP_HIT_RATE: val = sd->hit_rate; break;
  7356. case SP_FLEE_RATE: val = sd->flee_rate; break;
  7357. case SP_FLEE2_RATE: val = sd->flee2_rate; break;
  7358. case SP_DEF_RATE: val = sd->def_rate; break;
  7359. case SP_DEF2_RATE: val = sd->def2_rate; break;
  7360. case SP_MDEF_RATE: val = sd->mdef_rate; break;
  7361. case SP_MDEF2_RATE: val = sd->mdef2_rate; break;
  7362. case SP_RESTART_FULL_RECOVER: val = sd->special_state.restart_full_recover?1:0; break;
  7363. case SP_NO_CASTCANCEL: val = sd->special_state.no_castcancel?1:0; break;
  7364. case SP_NO_CASTCANCEL2: val = sd->special_state.no_castcancel2?1:0; break;
  7365. case SP_NO_SIZEFIX: val = sd->special_state.no_sizefix?1:0; break;
  7366. case SP_NO_MAGIC_DAMAGE: val = sd->special_state.no_magic_damage; break;
  7367. case SP_NO_WEAPON_DAMAGE:val = sd->special_state.no_weapon_damage; break;
  7368. case SP_NO_MISC_DAMAGE: val = sd->special_state.no_misc_damage; break;
  7369. case SP_NO_GEMSTONE: val = sd->special_state.no_gemstone?1:0; break;
  7370. case SP_INTRAVISION: val = sd->special_state.intravision?1:0; break;
  7371. case SP_NO_KNOCKBACK: val = sd->special_state.no_knockback?1:0; break;
  7372. case SP_NO_MADO_FUEL: val = sd->special_state.no_mado_fuel?1:0; break;
  7373. case SP_NO_WALK_DELAY: val = sd->special_state.no_walk_delay?1:0; break;
  7374. case SP_SPLASH_RANGE: val = sd->bonus.splash_range; break;
  7375. case SP_SPLASH_ADD_RANGE:val = sd->bonus.splash_add_range; break;
  7376. case SP_SHORT_WEAPON_DAMAGE_RETURN: val = sd->bonus.short_weapon_damage_return; break;
  7377. case SP_LONG_WEAPON_DAMAGE_RETURN: val = sd->bonus.long_weapon_damage_return; break;
  7378. case SP_MAGIC_DAMAGE_RETURN: val = sd->bonus.magic_damage_return; break;
  7379. case SP_PERFECT_HIDE: val = sd->special_state.perfect_hiding?1:0; break;
  7380. case SP_UNBREAKABLE: val = sd->bonus.unbreakable; break;
  7381. case SP_UNBREAKABLE_WEAPON: val = (sd->bonus.unbreakable_equip&EQP_WEAPON)?1:0; break;
  7382. case SP_UNBREAKABLE_ARMOR: val = (sd->bonus.unbreakable_equip&EQP_ARMOR)?1:0; break;
  7383. case SP_UNBREAKABLE_HELM: val = (sd->bonus.unbreakable_equip&EQP_HELM)?1:0; break;
  7384. case SP_UNBREAKABLE_SHIELD: val = (sd->bonus.unbreakable_equip&EQP_SHIELD)?1:0; break;
  7385. case SP_UNBREAKABLE_GARMENT: val = (sd->bonus.unbreakable_equip&EQP_GARMENT)?1:0; break;
  7386. case SP_UNBREAKABLE_SHOES: val = (sd->bonus.unbreakable_equip&EQP_SHOES)?1:0; break;
  7387. case SP_CLASSCHANGE: val = sd->bonus.classchange; break;
  7388. case SP_LONG_ATK_RATE: val = sd->bonus.long_attack_atk_rate; break;
  7389. case SP_BREAK_WEAPON_RATE: val = sd->bonus.break_weapon_rate; break;
  7390. case SP_BREAK_ARMOR_RATE: val = sd->bonus.break_armor_rate; break;
  7391. case SP_ADD_STEAL_RATE: val = sd->bonus.add_steal_rate; break;
  7392. case SP_DELAYRATE: val = sd->delayrate; break;
  7393. case SP_CRIT_ATK_RATE: val = sd->bonus.crit_atk_rate; break;
  7394. case SP_UNSTRIPABLE_WEAPON: val = (sd->bonus.unstripable_equip&EQP_WEAPON)?1:0; break;
  7395. case SP_UNSTRIPABLE:
  7396. case SP_UNSTRIPABLE_ARMOR:
  7397. val = (sd->bonus.unstripable_equip&EQP_ARMOR)?1:0;
  7398. break;
  7399. case SP_UNSTRIPABLE_HELM: val = (sd->bonus.unstripable_equip&EQP_HELM)?1:0; break;
  7400. case SP_UNSTRIPABLE_SHIELD: val = (sd->bonus.unstripable_equip&EQP_SHIELD)?1:0; break;
  7401. case SP_SP_GAIN_VALUE: val = sd->bonus.sp_gain_value; break;
  7402. case SP_HP_GAIN_VALUE: val = sd->bonus.hp_gain_value; break;
  7403. case SP_MAGIC_SP_GAIN_VALUE: val = sd->bonus.magic_sp_gain_value; break;
  7404. case SP_MAGIC_HP_GAIN_VALUE: val = sd->bonus.magic_hp_gain_value; break;
  7405. case SP_ADD_HEAL_RATE: val = sd->bonus.add_heal_rate; break;
  7406. case SP_ADD_HEAL2_RATE: val = sd->bonus.add_heal2_rate; break;
  7407. case SP_ADD_ITEM_HEAL_RATE: val = sd->bonus.itemhealrate2; break;
  7408. case SP_EMATK: val = sd->bonus.ematk; break;
  7409. case SP_FIXCASTRATE: val = sd->bonus.fixcastrate; break;
  7410. case SP_ADD_FIXEDCAST: val = sd->bonus.add_fixcast; break;
  7411. case SP_ADD_VARIABLECAST: val = sd->bonus.add_varcast; break;
  7412. case SP_CASTRATE:
  7413. case SP_VARCASTRATE:
  7414. #ifdef RENEWAL_CAST
  7415. val = sd->bonus.varcastrate; break;
  7416. #else
  7417. val = sd->castrate; break;
  7418. #endif
  7419. default:
  7420. ShowError("pc_readparam: Attempt to read unknown parameter '%d'.\n", type);
  7421. return -1;
  7422. }
  7423. return val;
  7424. }
  7425. /*==========================================
  7426. * script set pc status registry
  7427. *------------------------------------------*/
  7428. bool pc_setparam(struct map_session_data *sd,int type,int val)
  7429. {
  7430. nullpo_retr(false,sd);
  7431. switch(type){
  7432. case SP_BASELEVEL:
  7433. if ((unsigned int)val > pc_maxbaselv(sd)) //Capping to max
  7434. val = pc_maxbaselv(sd);
  7435. if ((unsigned int)val > sd->status.base_level) {
  7436. int i = 0;
  7437. int stat=0;
  7438. for (i = 0; i < (int)((unsigned int)val - sd->status.base_level); i++)
  7439. stat += pc_gets_status_point(sd->status.base_level + i);
  7440. sd->status.status_point += stat;
  7441. }
  7442. sd->status.base_level = (unsigned int)val;
  7443. sd->status.base_exp = 0;
  7444. // clif_updatestatus(sd, SP_BASELEVEL); // Gets updated at the bottom
  7445. clif_updatestatus(sd, SP_NEXTBASEEXP);
  7446. clif_updatestatus(sd, SP_STATUSPOINT);
  7447. clif_updatestatus(sd, SP_BASEEXP);
  7448. status_calc_pc(sd, SCO_FORCE);
  7449. if(sd->status.party_id)
  7450. party_send_levelup(sd);
  7451. break;
  7452. case SP_JOBLEVEL:
  7453. if ((unsigned int)val >= sd->status.job_level) {
  7454. if ((unsigned int)val > pc_maxjoblv(sd)) val = pc_maxjoblv(sd);
  7455. sd->status.skill_point += val - sd->status.job_level;
  7456. clif_updatestatus(sd, SP_SKILLPOINT);
  7457. }
  7458. sd->status.job_level = (unsigned int)val;
  7459. sd->status.job_exp = 0;
  7460. // clif_updatestatus(sd, SP_JOBLEVEL); // Gets updated at the bottom
  7461. clif_updatestatus(sd, SP_NEXTJOBEXP);
  7462. clif_updatestatus(sd, SP_JOBEXP);
  7463. status_calc_pc(sd, SCO_FORCE);
  7464. break;
  7465. case SP_SKILLPOINT:
  7466. sd->status.skill_point = val;
  7467. break;
  7468. case SP_STATUSPOINT:
  7469. sd->status.status_point = val;
  7470. break;
  7471. case SP_ZENY:
  7472. if( val < 0 )
  7473. return false;// can't set negative zeny
  7474. log_zeny(sd, LOG_TYPE_SCRIPT, sd, -(sd->status.zeny - cap_value(val, 0, MAX_ZENY)));
  7475. sd->status.zeny = cap_value(val, 0, MAX_ZENY);
  7476. break;
  7477. case SP_BASEEXP:
  7478. {
  7479. val = cap_value(val, 0, INT_MAX);
  7480. if ((unsigned int)val < sd->status.base_exp) // Lost
  7481. pc_lostexp(sd, sd->status.base_exp - val, 0);
  7482. else // Gained
  7483. pc_gainexp(sd, NULL, val - sd->status.base_exp, 0, 2);
  7484. }
  7485. return true;
  7486. case SP_JOBEXP:
  7487. {
  7488. val = cap_value(val, 0, INT_MAX);
  7489. if ((unsigned int)val < sd->status.job_exp) // Lost
  7490. pc_lostexp(sd, 0, sd->status.job_exp - val);
  7491. else // Gained
  7492. pc_gainexp(sd, NULL, 0, val - sd->status.job_exp, 2);
  7493. }
  7494. return true;
  7495. case SP_SEX:
  7496. sd->status.sex = val ? SEX_MALE : SEX_FEMALE;
  7497. break;
  7498. case SP_WEIGHT:
  7499. sd->weight = val;
  7500. break;
  7501. case SP_MAXWEIGHT:
  7502. sd->max_weight = val;
  7503. break;
  7504. case SP_HP:
  7505. sd->battle_status.hp = cap_value(val, 1, (int)sd->battle_status.max_hp);
  7506. break;
  7507. case SP_MAXHP:
  7508. if (sd->status.base_level < 100)
  7509. sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp_lv99);
  7510. else if (sd->status.base_level < 151)
  7511. sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp_lv150);
  7512. else
  7513. sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp);
  7514. if( sd->battle_status.max_hp < sd->battle_status.hp )
  7515. {
  7516. sd->battle_status.hp = sd->battle_status.max_hp;
  7517. clif_updatestatus(sd, SP_HP);
  7518. }
  7519. break;
  7520. case SP_SP:
  7521. sd->battle_status.sp = cap_value(val, 0, (int)sd->battle_status.max_sp);
  7522. break;
  7523. case SP_MAXSP:
  7524. sd->battle_status.max_sp = cap_value(val, 1, battle_config.max_sp);
  7525. if( sd->battle_status.max_sp < sd->battle_status.sp )
  7526. {
  7527. sd->battle_status.sp = sd->battle_status.max_sp;
  7528. clif_updatestatus(sd, SP_SP);
  7529. }
  7530. break;
  7531. case SP_STR:
  7532. sd->status.str = cap_value(val, 1, pc_maxparameter(sd,PARAM_STR));
  7533. break;
  7534. case SP_AGI:
  7535. sd->status.agi = cap_value(val, 1, pc_maxparameter(sd,PARAM_AGI));
  7536. break;
  7537. case SP_VIT:
  7538. sd->status.vit = cap_value(val, 1, pc_maxparameter(sd,PARAM_VIT));
  7539. break;
  7540. case SP_INT:
  7541. sd->status.int_ = cap_value(val, 1, pc_maxparameter(sd,PARAM_INT));
  7542. break;
  7543. case SP_DEX:
  7544. sd->status.dex = cap_value(val, 1, pc_maxparameter(sd,PARAM_DEX));
  7545. break;
  7546. case SP_LUK:
  7547. sd->status.luk = cap_value(val, 1, pc_maxparameter(sd,PARAM_LUK));
  7548. break;
  7549. case SP_KARMA:
  7550. sd->status.karma = val;
  7551. break;
  7552. case SP_MANNER:
  7553. sd->status.manner = val;
  7554. if( val < 0 )
  7555. sc_start(NULL, &sd->bl, SC_NOCHAT, 100, 0, 0);
  7556. else {
  7557. status_change_end(&sd->bl, SC_NOCHAT, INVALID_TIMER);
  7558. clif_manner_message(sd, 5);
  7559. }
  7560. return true; // status_change_start/status_change_end already sends packets warning the client
  7561. case SP_FAME:
  7562. sd->status.fame = val;
  7563. break;
  7564. case SP_KILLERRID:
  7565. sd->killerrid = val;
  7566. return true;
  7567. case SP_KILLEDRID:
  7568. sd->killedrid = val;
  7569. return true;
  7570. case SP_KILLEDGID:
  7571. sd->killedgid = val;
  7572. return true;
  7573. case SP_CHARMOVE:
  7574. sd->status.character_moves = val;
  7575. return true;
  7576. case SP_CHARRENAME:
  7577. sd->status.rename = val;
  7578. return true;
  7579. case SP_CHARFONT:
  7580. sd->status.font = val;
  7581. clif_font(sd);
  7582. return true;
  7583. case SP_BANK_VAULT:
  7584. if (val < 0)
  7585. return false;
  7586. log_zeny(sd, LOG_TYPE_BANK, sd, -(sd->bank_vault - cap_value(val, 0, MAX_BANK_ZENY)));
  7587. sd->bank_vault = cap_value(val, 0, MAX_BANK_ZENY);
  7588. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  7589. return true;
  7590. case SP_ROULETTE_BRONZE:
  7591. sd->roulette_point.bronze = val;
  7592. pc_setreg2(sd, ROULETTE_BRONZE_VAR, sd->roulette_point.bronze);
  7593. return true;
  7594. case SP_ROULETTE_SILVER:
  7595. sd->roulette_point.silver = val;
  7596. pc_setreg2(sd, ROULETTE_SILVER_VAR, sd->roulette_point.silver);
  7597. return true;
  7598. case SP_ROULETTE_GOLD:
  7599. sd->roulette_point.gold = val;
  7600. pc_setreg2(sd, ROULETTE_GOLD_VAR, sd->roulette_point.gold);
  7601. return true;
  7602. case SP_CASHPOINTS:
  7603. if (val < 0)
  7604. return false;
  7605. if (!sd->state.connect_new)
  7606. log_cash(sd, LOG_TYPE_SCRIPT, LOG_CASH_TYPE_CASH, -(sd->cashPoints - cap_value(val, 0, MAX_ZENY)));
  7607. sd->cashPoints = cap_value(val, 0, MAX_ZENY);
  7608. pc_setaccountreg(sd, add_str(CASHPOINT_VAR), sd->cashPoints);
  7609. return true;
  7610. case SP_KAFRAPOINTS:
  7611. if (val < 0)
  7612. return false;
  7613. if (!sd->state.connect_new)
  7614. log_cash(sd, LOG_TYPE_SCRIPT, LOG_CASH_TYPE_KAFRA, -(sd->kafraPoints - cap_value(val, 0, MAX_ZENY)));
  7615. sd->kafraPoints = cap_value(val, 0, MAX_ZENY);
  7616. pc_setaccountreg(sd, add_str(KAFRAPOINT_VAR), sd->kafraPoints);
  7617. return true;
  7618. case SP_PCDIECOUNTER:
  7619. if (val < 0)
  7620. return false;
  7621. if (sd->die_counter == val)
  7622. return true;
  7623. sd->die_counter = val;
  7624. if (!sd->die_counter && (sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE)
  7625. status_calc_pc(sd, SCO_NONE); // Lost the bonus.
  7626. pc_setglobalreg(sd, add_str(PCDIECOUNTER_VAR), sd->die_counter);
  7627. return true;
  7628. case SP_COOKMASTERY:
  7629. if (val < 0)
  7630. return false;
  7631. if (sd->cook_mastery == val)
  7632. return true;
  7633. val = cap_value(val, 0, 1999);
  7634. sd->cook_mastery = val;
  7635. pc_setglobalreg(sd, add_str(COOKMASTERY_VAR), sd->cook_mastery);
  7636. return true;
  7637. default:
  7638. ShowError("pc_setparam: Attempted to set unknown parameter '%d'.\n", type);
  7639. return false;
  7640. }
  7641. clif_updatestatus(sd,type);
  7642. return true;
  7643. }
  7644. /*==========================================
  7645. * HP/SP Healing. If flag is passed, the heal type is through clif_heal, otherwise update status.
  7646. *------------------------------------------*/
  7647. void pc_heal(struct map_session_data *sd,unsigned int hp,unsigned int sp, int type)
  7648. {
  7649. if (type&2) {
  7650. if (hp || type&4)
  7651. clif_heal(sd->fd,SP_HP,hp);
  7652. if (sp)
  7653. clif_heal(sd->fd,SP_SP,sp);
  7654. } else {
  7655. if(hp)
  7656. clif_updatestatus(sd,SP_HP);
  7657. if(sp)
  7658. clif_updatestatus(sd,SP_SP);
  7659. }
  7660. return;
  7661. }
  7662. /**
  7663. * Heal player HP and/or SP linearly. Calculate any bonus based on active statuses.
  7664. * @param sd: Player data
  7665. * @param itemid: Item ID
  7666. * @param hp: HP to heal
  7667. * @param sp: SP to heal
  7668. * @return Amount healed to an object
  7669. */
  7670. int pc_itemheal(struct map_session_data *sd, int itemid, int hp, int sp)
  7671. {
  7672. int bonus, tmp, penalty = 0;
  7673. if (hp) {
  7674. bonus = 100 + (sd->battle_status.vit << 1) + pc_checkskill(sd, SM_RECOVERY) * 10 + pc_checkskill(sd, AM_LEARNINGPOTION) * 5;
  7675. // A potion produced by an Alchemist in the Fame Top 10 gets +50% effect [DracoRPG]
  7676. if (potion_flag == 2) {
  7677. bonus += 50;
  7678. if (sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_ROGUE)
  7679. bonus += 100; // Receive an additional +100% effect from ranked potions to HP only
  7680. }
  7681. //All item bonuses.
  7682. bonus += sd->bonus.itemhealrate2;
  7683. //Item Group bonuses
  7684. bonus += pc_get_itemgroup_bonus(sd, itemid);
  7685. //Individual item bonuses.
  7686. for(const auto &it : sd->itemhealrate) {
  7687. if (it.id == itemid) {
  7688. bonus += it.val;
  7689. break;
  7690. }
  7691. }
  7692. // Recovery Potion
  7693. if (sd->sc.data[SC_INCHEALRATE])
  7694. bonus += sd->sc.data[SC_INCHEALRATE]->val1;
  7695. // 2014 Halloween Event : Pumpkin Bonus
  7696. if (sd->sc.data[SC_MTF_PUMPKIN] && itemid == ITEMID_PUMPKIN)
  7697. bonus += sd->sc.data[SC_MTF_PUMPKIN]->val1;
  7698. tmp = hp * bonus / 100; // Overflow check
  7699. if (bonus != 100 && tmp > hp)
  7700. hp = tmp;
  7701. }
  7702. if (sp) {
  7703. bonus = 100 + (sd->battle_status.int_ << 1) + pc_checkskill(sd, MG_SRECOVERY) * 10 + pc_checkskill(sd, AM_LEARNINGPOTION) * 5;
  7704. // A potion produced by an Alchemist in the Fame Top 10 gets +50% effect [DracoRPG]
  7705. if (potion_flag == 2)
  7706. bonus += 50;
  7707. tmp = sp * bonus / 100; // Overflow check
  7708. if (bonus != 100 && tmp > sp)
  7709. sp = tmp;
  7710. }
  7711. if (sd->sc.count) {
  7712. // Critical Wound and Death Hurt stack
  7713. if (sd->sc.data[SC_CRITICALWOUND])
  7714. penalty += sd->sc.data[SC_CRITICALWOUND]->val2;
  7715. if (sd->sc.data[SC_DEATHHURT])
  7716. penalty += 20;
  7717. if (sd->sc.data[SC_NORECOVER_STATE])
  7718. penalty = 100;
  7719. if (sd->sc.data[SC_VITALITYACTIVATION]) {
  7720. hp += hp / 2; // 1.5 times
  7721. sp -= sp / 2;
  7722. }
  7723. if (sd->sc.data[SC_WATER_INSIGNIA] && sd->sc.data[SC_WATER_INSIGNIA]->val1 == 2) {
  7724. hp += hp / 10;
  7725. sp += sp / 10;
  7726. }
  7727. if (penalty > 0) {
  7728. hp -= hp * penalty / 100;
  7729. sp -= sp * penalty / 100;
  7730. }
  7731. #ifdef RENEWAL
  7732. if (sd->sc.data[SC_EXTREMITYFIST2])
  7733. sp = 0;
  7734. #endif
  7735. if (sd->sc.data[SC_BITESCAR])
  7736. hp = 0;
  7737. }
  7738. return status_heal(&sd->bl, hp, sp, 1);
  7739. }
  7740. /*==========================================
  7741. * HP/SP Recovery
  7742. * Heal player hp nad/or sp by rate
  7743. *------------------------------------------*/
  7744. int pc_percentheal(struct map_session_data *sd,int hp,int sp)
  7745. {
  7746. nullpo_ret(sd);
  7747. if (hp > 100) hp = 100;
  7748. else if (hp <-100) hp = -100;
  7749. if (sp > 100) sp = 100;
  7750. else if (sp <-100) sp = -100;
  7751. if(hp >= 0 && sp >= 0) //Heal
  7752. return status_percent_heal(&sd->bl, hp, sp);
  7753. if(hp <= 0 && sp <= 0) //Damage (negative rates indicate % of max rather than current), and only kill target IF the specified amount is 100%
  7754. return status_percent_damage(NULL, &sd->bl, hp, sp, hp==-100);
  7755. //Crossed signs
  7756. if(hp) {
  7757. if(hp > 0)
  7758. status_percent_heal(&sd->bl, hp, 0);
  7759. else
  7760. status_percent_damage(NULL, &sd->bl, hp, 0, hp==-100);
  7761. }
  7762. if(sp) {
  7763. if(sp > 0)
  7764. status_percent_heal(&sd->bl, 0, sp);
  7765. else
  7766. status_percent_damage(NULL, &sd->bl, 0, sp, false);
  7767. }
  7768. return 0;
  7769. }
  7770. static int jobchange_killclone(struct block_list *bl, va_list ap)
  7771. {
  7772. struct mob_data *md;
  7773. int flag;
  7774. md = (struct mob_data *)bl;
  7775. nullpo_ret(md);
  7776. flag = va_arg(ap, int);
  7777. if (md->master_id && md->special_state.clone && md->master_id == flag)
  7778. status_kill(&md->bl);
  7779. return 1;
  7780. }
  7781. /**
  7782. * Called when player changes job
  7783. * Rewrote to make it tidider [Celest]
  7784. * @param sd
  7785. * @param job JOB ID. See enum e_job
  7786. * @param upper 1 - JOBL_UPPER; 2 - JOBL_BABY
  7787. * @return True if success, false if failed
  7788. **/
  7789. bool pc_jobchange(struct map_session_data *sd,int job, char upper)
  7790. {
  7791. int i, fame_flag = 0;
  7792. int b_class;
  7793. nullpo_retr(false,sd);
  7794. if (job < 0)
  7795. return false;
  7796. //Normalize job.
  7797. b_class = pc_jobid2mapid(job);
  7798. if (b_class == -1)
  7799. return false;
  7800. switch (upper) {
  7801. case 1:
  7802. b_class|= JOBL_UPPER;
  7803. break;
  7804. case 2:
  7805. b_class|= JOBL_BABY;
  7806. break;
  7807. }
  7808. //This will automatically adjust bard/dancer classes to the correct gender
  7809. //That is, if you try to jobchange into dancer, it will turn you to bard.
  7810. job = pc_mapid2jobid(b_class, sd->status.sex);
  7811. if (job == -1)
  7812. return false;
  7813. if ((unsigned short)b_class == sd->class_)
  7814. return false; //Nothing to change.
  7815. // changing from 1st to 2nd job
  7816. if ((b_class&JOBL_2) && !(sd->class_&JOBL_2) && (sd->class_&MAPID_UPPERMASK) != MAPID_SUPER_NOVICE) {
  7817. sd->change_level_2nd = sd->status.job_level;
  7818. pc_setglobalreg(sd, add_str(JOBCHANGE2ND_VAR), sd->change_level_2nd);
  7819. }
  7820. // changing from 2nd to 3rd job
  7821. else if((b_class&JOBL_THIRD) && !(sd->class_&JOBL_THIRD)) {
  7822. sd->change_level_3rd = sd->status.job_level;
  7823. pc_setglobalreg(sd, add_str(JOBCHANGE3RD_VAR), sd->change_level_3rd);
  7824. }
  7825. if(sd->cloneskill_idx > 0) {
  7826. if( sd->status.skill[sd->cloneskill_idx].flag == SKILL_FLAG_PLAGIARIZED ) {
  7827. sd->status.skill[sd->cloneskill_idx].id = 0;
  7828. sd->status.skill[sd->cloneskill_idx].lv = 0;
  7829. sd->status.skill[sd->cloneskill_idx].flag = SKILL_FLAG_PERMANENT;
  7830. clif_deleteskill(sd,pc_readglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM)));
  7831. }
  7832. sd->cloneskill_idx = 0;
  7833. pc_setglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM), 0);
  7834. pc_setglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM_LV), 0);
  7835. }
  7836. if(sd->reproduceskill_idx > 0) {
  7837. if( sd->status.skill[sd->reproduceskill_idx].flag == SKILL_FLAG_PLAGIARIZED ) {
  7838. sd->status.skill[sd->reproduceskill_idx].id = 0;
  7839. sd->status.skill[sd->reproduceskill_idx].lv = 0;
  7840. sd->status.skill[sd->reproduceskill_idx].flag = SKILL_FLAG_PERMANENT;
  7841. clif_deleteskill(sd,pc_readglobalreg(sd, add_str(SKILL_VAR_REPRODUCE)));
  7842. }
  7843. sd->reproduceskill_idx = 0;
  7844. pc_setglobalreg(sd, add_str(SKILL_VAR_REPRODUCE), 0);
  7845. pc_setglobalreg(sd, add_str(SKILL_VAR_REPRODUCE_LV), 0);
  7846. }
  7847. // Give or reduce transcendent status points
  7848. if( (b_class&JOBL_UPPER) && !(sd->class_&JOBL_UPPER) ){ // Change from a non t class to a t class -> give points
  7849. sd->status.status_point += battle_config.transcendent_status_points;
  7850. clif_updatestatus(sd,SP_STATUSPOINT);
  7851. }else if( !(b_class&JOBL_UPPER) && (sd->class_&JOBL_UPPER) ){ // Change from a t class to a non t class -> remove points
  7852. if( sd->status.status_point < battle_config.transcendent_status_points ){
  7853. // The player already used his bonus points, so we have to reset his status points
  7854. pc_resetstate(sd);
  7855. }
  7856. sd->status.status_point -= battle_config.transcendent_status_points;
  7857. clif_updatestatus(sd,SP_STATUSPOINT);
  7858. }
  7859. if ( (b_class&MAPID_UPPERMASK) != (sd->class_&MAPID_UPPERMASK) ) { //Things to remove when changing class tree.
  7860. const int class_ = pc_class2idx(sd->status.class_);
  7861. uint16 skill_id;
  7862. for(i = 0; i < MAX_SKILL_TREE && (skill_id = skill_tree[class_][i].skill_id) > 0; i++) {
  7863. //Remove status specific to your current tree skills.
  7864. enum sc_type sc = status_skill2sc(skill_id);
  7865. if (sc > SC_COMMON_MAX && sd->sc.data[sc])
  7866. status_change_end(&sd->bl, sc, INVALID_TIMER);
  7867. }
  7868. }
  7869. if( (sd->class_&MAPID_UPPERMASK) == MAPID_STAR_GLADIATOR && (b_class&MAPID_UPPERMASK) != MAPID_STAR_GLADIATOR) {
  7870. /* going off star glad lineage, reset feel to not store no-longer-used vars in the database */
  7871. pc_resetfeel(sd);
  7872. }
  7873. // Reset body style to 0 before changing job to avoid
  7874. // errors since not every job has a alternate outfit.
  7875. sd->status.body = 0;
  7876. clif_changelook(&sd->bl,LOOK_BODY2,0);
  7877. sd->status.class_ = job;
  7878. fame_flag = pc_famerank(sd->status.char_id,sd->class_&MAPID_UPPERMASK);
  7879. sd->class_ = (unsigned short)b_class;
  7880. sd->status.job_level=1;
  7881. sd->status.job_exp=0;
  7882. if (sd->status.base_level > pc_maxbaselv(sd)) {
  7883. sd->status.base_level = pc_maxbaselv(sd);
  7884. sd->status.base_exp=0;
  7885. pc_resetstate(sd);
  7886. clif_updatestatus(sd,SP_STATUSPOINT);
  7887. clif_updatestatus(sd,SP_BASELEVEL);
  7888. clif_updatestatus(sd,SP_BASEEXP);
  7889. clif_updatestatus(sd,SP_NEXTBASEEXP);
  7890. }
  7891. clif_updatestatus(sd,SP_JOBLEVEL);
  7892. clif_updatestatus(sd,SP_JOBEXP);
  7893. clif_updatestatus(sd,SP_NEXTJOBEXP);
  7894. for(i=0;i<EQI_MAX;i++) {
  7895. if(sd->equip_index[i] >= 0)
  7896. if(pc_isequip(sd,sd->equip_index[i]))
  7897. pc_unequipitem(sd,sd->equip_index[i],2); // unequip invalid item for class
  7898. }
  7899. //Change look, if disguised, you need to undisguise
  7900. //to correctly calculate new job sprite without
  7901. if (sd->disguise)
  7902. pc_disguise(sd, 0);
  7903. status_set_viewdata(&sd->bl, job);
  7904. clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_); // move sprite update to prevent client crashes with incompatible equipment [Valaris]
  7905. #if PACKETVER >= 20151001
  7906. clif_changelook(&sd->bl, LOOK_HAIR, sd->vd.hair_style); // Update player's head (only matters when switching to or from Doram)
  7907. #endif
  7908. if(sd->vd.cloth_color)
  7909. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  7910. /*
  7911. if(sd->vd.body_style)
  7912. clif_changelook(&sd->bl,LOOK_BODY2,sd->vd.body_style);
  7913. */
  7914. //Update skill tree.
  7915. pc_calc_skilltree(sd);
  7916. clif_skillinfoblock(sd);
  7917. if (sd->ed)
  7918. elemental_delete(sd->ed);
  7919. if (sd->state.vending)
  7920. vending_closevending(sd);
  7921. if (sd->state.buyingstore)
  7922. buyingstore_close(sd);
  7923. map_foreachinmap(jobchange_killclone, sd->bl.m, BL_MOB, sd->bl.id);
  7924. //Remove peco/cart/falcon
  7925. i = sd->sc.option;
  7926. if( i&OPTION_RIDING && !pc_checkskill(sd, KN_RIDING) )
  7927. i&=~OPTION_RIDING;
  7928. if( i&OPTION_FALCON && !pc_checkskill(sd, HT_FALCON) )
  7929. i&=~OPTION_FALCON;
  7930. if( i&OPTION_DRAGON && !pc_checkskill(sd,RK_DRAGONTRAINING) )
  7931. i&=~OPTION_DRAGON;
  7932. if( i&OPTION_WUGRIDER && !pc_checkskill(sd,RA_WUGMASTERY) )
  7933. i&=~OPTION_WUGRIDER;
  7934. if( i&OPTION_WUG && !pc_checkskill(sd,RA_WUGMASTERY) )
  7935. i&=~OPTION_WUG;
  7936. if( i&OPTION_MADOGEAR ) //You do not need a skill for this.
  7937. i&=~OPTION_MADOGEAR;
  7938. #ifndef NEW_CARTS
  7939. if( i&OPTION_CART && !pc_checkskill(sd, MC_PUSHCART) )
  7940. i&=~OPTION_CART;
  7941. #else
  7942. if( sd->sc.data[SC_PUSH_CART] && !pc_checkskill(sd, MC_PUSHCART) )
  7943. pc_setcart(sd, 0);
  7944. #endif
  7945. if(i != sd->sc.option)
  7946. pc_setoption(sd, i);
  7947. if(hom_is_active(sd->hd) && !pc_checkskill(sd, AM_CALLHOMUN))
  7948. hom_vaporize(sd, HOM_ST_ACTIVE);
  7949. if (sd->sc.data[SC_SPRITEMABLE] && !pc_checkskill(sd, SU_SPRITEMABLE))
  7950. status_change_end(&sd->bl, SC_SPRITEMABLE, INVALID_TIMER);
  7951. if(sd->status.manner < 0)
  7952. clif_changestatus(sd,SP_MANNER,sd->status.manner);
  7953. status_calc_pc(sd,SCO_FORCE);
  7954. pc_checkallowskill(sd);
  7955. pc_equiplookall(sd);
  7956. pc_show_questinfo(sd);
  7957. achievement_update_objective(sd, AG_JOB_CHANGE, 2, sd->status.base_level, job);
  7958. if( sd->status.party_id ){
  7959. struct party_data* p;
  7960. if( ( p = party_search( sd->status.party_id ) ) != NULL ){
  7961. ARR_FIND(0, MAX_PARTY, i, p->party.member[i].char_id == sd->status.char_id);
  7962. if( i < MAX_PARTY ){
  7963. p->party.member[i].class_ = sd->status.class_;
  7964. clif_party_job_and_level(sd);
  7965. }
  7966. }
  7967. }
  7968. chrif_save(sd, CSAVE_NORMAL);
  7969. //if you were previously famous, not anymore.
  7970. if (fame_flag)
  7971. chrif_buildfamelist();
  7972. else if (sd->status.fame > 0) {
  7973. //It may be that now they are famous?
  7974. switch (sd->class_&MAPID_UPPERMASK) {
  7975. case MAPID_BLACKSMITH:
  7976. case MAPID_ALCHEMIST:
  7977. case MAPID_TAEKWON:
  7978. chrif_buildfamelist();
  7979. break;
  7980. }
  7981. }
  7982. return true;
  7983. }
  7984. /*==========================================
  7985. * Tell client player sd has change equipement
  7986. *------------------------------------------*/
  7987. void pc_equiplookall(struct map_session_data *sd)
  7988. {
  7989. nullpo_retv(sd);
  7990. clif_changelook(&sd->bl,LOOK_WEAPON,0);
  7991. clif_changelook(&sd->bl,LOOK_SHOES,0);
  7992. clif_changelook(&sd->bl,LOOK_HEAD_BOTTOM,sd->status.head_bottom);
  7993. clif_changelook(&sd->bl,LOOK_HEAD_TOP,sd->status.head_top);
  7994. clif_changelook(&sd->bl,LOOK_HEAD_MID,sd->status.head_mid);
  7995. clif_changelook(&sd->bl,LOOK_ROBE, sd->status.robe);
  7996. }
  7997. /*==========================================
  7998. * Tell client player sd has change look (hair,equip...)
  7999. *------------------------------------------*/
  8000. void pc_changelook(struct map_session_data *sd,int type,int val) {
  8001. nullpo_retv(sd);
  8002. switch(type) {
  8003. case LOOK_HAIR: //Use the battle_config limits! [Skotlex]
  8004. val = cap_value(val, MIN_HAIR_STYLE, MAX_HAIR_STYLE);
  8005. if (sd->status.hair != val) {
  8006. sd->status.hair = val;
  8007. if (sd->status.guild_id) //Update Guild Window. [Skotlex]
  8008. intif_guild_change_memberinfo(sd->status.guild_id, sd->status.account_id, sd->status.char_id,
  8009. GMI_HAIR, &sd->status.hair, sizeof(sd->status.hair));
  8010. }
  8011. break;
  8012. case LOOK_WEAPON:
  8013. sd->status.weapon = val;
  8014. break;
  8015. case LOOK_HEAD_BOTTOM:
  8016. sd->status.head_bottom = val;
  8017. sd->setlook_head_bottom = val;
  8018. break;
  8019. case LOOK_HEAD_TOP:
  8020. sd->status.head_top = val;
  8021. sd->setlook_head_top = val;
  8022. break;
  8023. case LOOK_HEAD_MID:
  8024. sd->status.head_mid = val;
  8025. sd->setlook_head_mid = val;
  8026. break;
  8027. case LOOK_HAIR_COLOR: //Use the battle_config limits! [Skotlex]
  8028. val = cap_value(val, MIN_HAIR_COLOR, MAX_HAIR_COLOR);
  8029. if (sd->status.hair_color != val) {
  8030. sd->status.hair_color = val;
  8031. if (sd->status.guild_id) //Update Guild Window. [Skotlex]
  8032. intif_guild_change_memberinfo(sd->status.guild_id, sd->status.account_id, sd->status.char_id,
  8033. GMI_HAIR_COLOR, &sd->status.hair_color, sizeof(sd->status.hair_color));
  8034. }
  8035. break;
  8036. case LOOK_CLOTHES_COLOR: //Use the battle_config limits! [Skotlex]
  8037. val = cap_value(val, MIN_CLOTH_COLOR, MAX_CLOTH_COLOR);
  8038. sd->status.clothes_color = val;
  8039. break;
  8040. case LOOK_SHIELD:
  8041. sd->status.shield = val;
  8042. break;
  8043. case LOOK_SHOES:
  8044. break;
  8045. case LOOK_ROBE:
  8046. sd->status.robe = val;
  8047. sd->setlook_robe = val;
  8048. break;
  8049. case LOOK_BODY2:
  8050. val = cap_value(val, MIN_BODY_STYLE, MAX_BODY_STYLE);
  8051. sd->status.body = val;
  8052. break;
  8053. }
  8054. clif_changelook(&sd->bl, type, val);
  8055. }
  8056. /*==========================================
  8057. * Give an option (type) to player (sd) and display it to client
  8058. *------------------------------------------*/
  8059. void pc_setoption(struct map_session_data *sd,int type)
  8060. {
  8061. int p_type, new_look=0;
  8062. nullpo_retv(sd);
  8063. p_type = sd->sc.option;
  8064. //Option has to be changed client-side before the class sprite or it won't always work (eg: Wedding sprite) [Skotlex]
  8065. sd->sc.option=type;
  8066. clif_changeoption(&sd->bl);
  8067. if( (type&OPTION_RIDING && !(p_type&OPTION_RIDING)) || (type&OPTION_DRAGON && !(p_type&OPTION_DRAGON) && pc_checkskill(sd,RK_DRAGONTRAINING) > 0) )
  8068. { // Mounting
  8069. clif_status_load(&sd->bl,EFST_RIDING,1);
  8070. status_calc_pc(sd,SCO_NONE);
  8071. }
  8072. else if( (!(type&OPTION_RIDING) && p_type&OPTION_RIDING) || (!(type&OPTION_DRAGON) && p_type&OPTION_DRAGON && pc_checkskill(sd,RK_DRAGONTRAINING) > 0) )
  8073. { // Dismount
  8074. clif_status_load(&sd->bl,EFST_RIDING,0);
  8075. status_calc_pc(sd,SCO_NONE);
  8076. }
  8077. #ifndef NEW_CARTS
  8078. if( type&OPTION_CART && !( p_type&OPTION_CART ) ) { //Cart On
  8079. clif_cartlist(sd);
  8080. clif_updatestatus(sd, SP_CARTINFO);
  8081. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  8082. status_calc_pc(sd,SCO_NONE); //Apply speed penalty.
  8083. } else if( !( type&OPTION_CART ) && p_type&OPTION_CART ){ //Cart Off
  8084. clif_clearcart(sd->fd);
  8085. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  8086. status_calc_pc(sd,SCO_NONE); //Remove speed penalty.
  8087. }
  8088. #endif
  8089. if (type&OPTION_FALCON && !(p_type&OPTION_FALCON)) //Falcon ON
  8090. clif_status_load(&sd->bl,EFST_FALCON,1);
  8091. else if (!(type&OPTION_FALCON) && p_type&OPTION_FALCON) //Falcon OFF
  8092. clif_status_load(&sd->bl,EFST_FALCON,0);
  8093. if( (sd->class_&MAPID_THIRDMASK) == MAPID_RANGER ) {
  8094. if( type&OPTION_WUGRIDER && !(p_type&OPTION_WUGRIDER) ) { // Mounting
  8095. clif_status_load(&sd->bl,EFST_WUGRIDER,1);
  8096. status_calc_pc(sd,SCO_NONE);
  8097. } else if( !(type&OPTION_WUGRIDER) && p_type&OPTION_WUGRIDER ) { // Dismount
  8098. clif_status_load(&sd->bl,EFST_WUGRIDER,0);
  8099. status_calc_pc(sd,SCO_NONE);
  8100. }
  8101. }
  8102. if( (sd->class_&MAPID_THIRDMASK) == MAPID_MECHANIC ) {
  8103. if( type&OPTION_MADOGEAR && !(p_type&OPTION_MADOGEAR) ) {
  8104. static const sc_type statuses [] = { SC_MAXIMIZEPOWER, SC_OVERTHRUST, SC_WEAPONPERFECTION, SC_ADRENALINE, SC_CARTBOOST, SC_MELTDOWN, SC_MAXOVERTHRUST };
  8105. status_calc_pc(sd,SCO_NONE);
  8106. for (uint8 i = 0; i < ARRAYLENGTH(statuses); i++) {
  8107. int skill_id = status_sc2skill(statuses[i]);
  8108. if (skill_id >= 0 && !(skill_get_inf3(skill_id)&INF3_USABLE_MADO))
  8109. status_change_end(&sd->bl,statuses[i],INVALID_TIMER);
  8110. }
  8111. pc_bonus_script_clear(sd,BSF_REM_ON_MADOGEAR);
  8112. } else if( !(type&OPTION_MADOGEAR) && p_type&OPTION_MADOGEAR ) {
  8113. status_calc_pc(sd,SCO_NONE);
  8114. status_change_end(&sd->bl,SC_SHAPESHIFT,INVALID_TIMER);
  8115. status_change_end(&sd->bl,SC_HOVERING,INVALID_TIMER);
  8116. status_change_end(&sd->bl,SC_ACCELERATION,INVALID_TIMER);
  8117. status_change_end(&sd->bl,SC_OVERHEAT_LIMITPOINT,INVALID_TIMER);
  8118. status_change_end(&sd->bl,SC_OVERHEAT,INVALID_TIMER);
  8119. status_change_end(&sd->bl,SC_MAGNETICFIELD,INVALID_TIMER);
  8120. status_change_end(&sd->bl,SC_NEUTRALBARRIER_MASTER,INVALID_TIMER);
  8121. status_change_end(&sd->bl,SC_STEALTHFIELD_MASTER,INVALID_TIMER);
  8122. pc_bonus_script_clear(sd,BSF_REM_ON_MADOGEAR);
  8123. }
  8124. }
  8125. if (type&OPTION_FLYING && !(p_type&OPTION_FLYING))
  8126. new_look = JOB_STAR_GLADIATOR2;
  8127. else if (!(type&OPTION_FLYING) && p_type&OPTION_FLYING)
  8128. new_look = -1;
  8129. if (sd->disguise || !new_look)
  8130. return; //Disguises break sprite changes
  8131. if (new_look < 0) { //Restore normal look.
  8132. status_set_viewdata(&sd->bl, sd->status.class_);
  8133. new_look = sd->vd.class_;
  8134. }
  8135. pc_stop_attack(sd); //Stop attacking on new view change (to prevent wedding/santa attacks.
  8136. clif_changelook(&sd->bl,LOOK_BASE,new_look);
  8137. if (sd->vd.cloth_color)
  8138. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  8139. if( sd->vd.body_style )
  8140. clif_changelook(&sd->bl,LOOK_BODY2,sd->vd.body_style);
  8141. clif_skillinfoblock(sd); // Skill list needs to be updated after base change.
  8142. }
  8143. /**
  8144. * Give player a cart
  8145. * @param sd Player
  8146. * @param type 0:Remove cart, 1 ~ MAX_CARTS: Cart type
  8147. **/
  8148. bool pc_setcart(struct map_session_data *sd,int type) {
  8149. #ifndef NEW_CARTS
  8150. int cart[6] = {0x0000,OPTION_CART1,OPTION_CART2,OPTION_CART3,OPTION_CART4,OPTION_CART5};
  8151. int option;
  8152. #endif
  8153. nullpo_retr(false,sd);
  8154. if( type < 0 || type > MAX_CARTS )
  8155. return false;// Never trust the values sent by the client! [Skotlex]
  8156. if( pc_checkskill(sd,MC_PUSHCART) <= 0 && type != 0 )
  8157. return false;// Push cart is required
  8158. #ifdef NEW_CARTS
  8159. switch( type ) {
  8160. case 0:
  8161. if( !sd->sc.data[SC_PUSH_CART] )
  8162. return 0;
  8163. status_change_end(&sd->bl,SC_PUSH_CART,INVALID_TIMER);
  8164. clif_clearcart(sd->fd);
  8165. break;
  8166. default:/* everything else is an allowed ID so we can move on */
  8167. if( !sd->sc.data[SC_PUSH_CART] ) { /* first time, so fill cart data */
  8168. clif_cartlist(sd);
  8169. status_calc_cart_weight(sd, (e_status_calc_weight_opt)(CALCWT_ITEM|CALCWT_MAXBONUS|CALCWT_CARTSTATE));
  8170. }
  8171. clif_updatestatus(sd, SP_CARTINFO);
  8172. sc_start(&sd->bl, &sd->bl, SC_PUSH_CART, 100, type, 0);
  8173. break;
  8174. }
  8175. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  8176. status_calc_pc(sd,SCO_NONE); //Recalc speed penalty.
  8177. #else
  8178. // Update option
  8179. option = sd->sc.option;
  8180. option &= ~OPTION_CART;// clear cart bits
  8181. option |= cart[type]; // set cart
  8182. pc_setoption(sd, option);
  8183. #endif
  8184. return true;
  8185. }
  8186. /*==========================================
  8187. * Give player a falcon
  8188. *------------------------------------------*/
  8189. void pc_setfalcon(struct map_session_data* sd, int flag)
  8190. {
  8191. if( flag ){
  8192. if( pc_checkskill(sd,HT_FALCON)>0 ) // add falcon if he have the skill
  8193. pc_setoption(sd,sd->sc.option|OPTION_FALCON);
  8194. } else if( pc_isfalcon(sd) ){
  8195. pc_setoption(sd,sd->sc.option&~OPTION_FALCON); // remove falcon
  8196. }
  8197. }
  8198. /*==========================================
  8199. * Set player riding
  8200. *------------------------------------------*/
  8201. void pc_setriding(struct map_session_data* sd, int flag)
  8202. {
  8203. if( sd->sc.data[SC_ALL_RIDING] )
  8204. return;
  8205. if( flag ){
  8206. if( pc_checkskill(sd,KN_RIDING) > 0 ) // add peco
  8207. pc_setoption(sd, sd->sc.option|OPTION_RIDING);
  8208. } else if( pc_isriding(sd) ){
  8209. pc_setoption(sd, sd->sc.option&~OPTION_RIDING);
  8210. }
  8211. }
  8212. /*==========================================
  8213. * Give player a mado
  8214. *------------------------------------------*/
  8215. void pc_setmadogear(struct map_session_data* sd, int flag)
  8216. {
  8217. if( flag ){
  8218. if( pc_checkskill(sd,NC_MADOLICENCE) > 0 )
  8219. pc_setoption(sd, sd->sc.option|OPTION_MADOGEAR);
  8220. } else if( pc_ismadogear(sd) ){
  8221. pc_setoption(sd, sd->sc.option&~OPTION_MADOGEAR);
  8222. }
  8223. }
  8224. /*==========================================
  8225. * Check if player can drop an item
  8226. *------------------------------------------*/
  8227. bool pc_candrop(struct map_session_data *sd, struct item *item)
  8228. {
  8229. if( item && (item->expire_time || (item->bound && !pc_can_give_bounded_items(sd))) )
  8230. return false;
  8231. if( !pc_can_give_items(sd) || sd->sc.cant.drop) //check if this GM level can drop items
  8232. return false;
  8233. return (itemdb_isdropable(item, pc_get_group_level(sd)));
  8234. }
  8235. /**
  8236. * Determines whether a player can attack based on status changes
  8237. * Why not use status_check_skilluse?
  8238. * "src MAY be null to indicate we shouldn't check it, this is a ground-based skill attack."
  8239. * Even ground-based attacks should be blocked by these statuses
  8240. * Called from unit_attack and unit_attack_timer_sub
  8241. * @retval true Can attack
  8242. **/
  8243. bool pc_can_attack( struct map_session_data *sd, int target_id ) {
  8244. nullpo_retr(false, sd);
  8245. if( pc_is90overweight(sd) || pc_isridingwug(sd) )
  8246. return false;
  8247. if (sd->state.block_action & PCBLOCK_ATTACK)
  8248. return false;
  8249. if( sd->sc.data[SC_BASILICA] ||
  8250. sd->sc.data[SC__SHADOWFORM] ||
  8251. sd->sc.data[SC_CURSEDCIRCLE_ATKER] ||
  8252. sd->sc.data[SC_CURSEDCIRCLE_TARGET] ||
  8253. sd->sc.data[SC_CRYSTALIZE] ||
  8254. sd->sc.data[SC_ALL_RIDING] || // The client doesn't let you, this is to make cheat-safe
  8255. sd->sc.data[SC_TRICKDEAD] ||
  8256. (sd->sc.data[SC_VOICEOFSIREN] && sd->sc.data[SC_VOICEOFSIREN]->val2 == target_id) ||
  8257. sd->sc.data[SC_BLADESTOP] ||
  8258. sd->sc.data[SC_DEEPSLEEP] ||
  8259. (sd->sc.data[SC_GRAVITATION] && sd->sc.data[SC_GRAVITATION]->val3 == BCT_SELF) ||
  8260. sd->sc.data[SC_KINGS_GRACE] )
  8261. return false;
  8262. return true;
  8263. }
  8264. /*==========================================
  8265. * Read '@type' variables (temporary numeric char reg)
  8266. *------------------------------------------*/
  8267. int pc_readreg(struct map_session_data* sd, int64 reg)
  8268. {
  8269. return i64db_iget(sd->regs.vars, reg);
  8270. }
  8271. /*==========================================
  8272. * Set '@type' variables (temporary numeric char reg)
  8273. *------------------------------------------*/
  8274. bool pc_setreg(struct map_session_data* sd, int64 reg, int val)
  8275. {
  8276. unsigned int index = script_getvaridx(reg);
  8277. nullpo_retr(false, sd);
  8278. if( val ) {
  8279. i64db_iput(sd->regs.vars, reg, val);
  8280. if( index )
  8281. script_array_update(&sd->regs, reg, false);
  8282. } else {
  8283. i64db_remove(sd->regs.vars, reg);
  8284. if( index )
  8285. script_array_update(&sd->regs, reg, true);
  8286. }
  8287. return true;
  8288. }
  8289. /*==========================================
  8290. * Read '@type$' variables (temporary string char reg)
  8291. *------------------------------------------*/
  8292. char* pc_readregstr(struct map_session_data* sd, int64 reg)
  8293. {
  8294. struct script_reg_str *p = NULL;
  8295. p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg);
  8296. return p ? p->value : NULL;
  8297. }
  8298. /*==========================================
  8299. * Set '@type$' variables (temporary string char reg)
  8300. *------------------------------------------*/
  8301. bool pc_setregstr(struct map_session_data* sd, int64 reg, const char* str)
  8302. {
  8303. struct script_reg_str *p = NULL;
  8304. unsigned int index = script_getvaridx(reg);
  8305. DBData prev;
  8306. nullpo_retr(false, sd);
  8307. if( str[0] ) {
  8308. p = ers_alloc(str_reg_ers, struct script_reg_str);
  8309. p->value = aStrdup(str);
  8310. p->flag.type = 1;
  8311. if (sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev)) {
  8312. p = (struct script_reg_str *)db_data2ptr(&prev);
  8313. if( p->value )
  8314. aFree(p->value);
  8315. ers_free(str_reg_ers, p);
  8316. } else {
  8317. if( index )
  8318. script_array_update(&sd->regs, reg, false);
  8319. }
  8320. } else {
  8321. if (sd->regs.vars->remove(sd->regs.vars, db_i642key(reg), &prev)) {
  8322. p = (struct script_reg_str *)db_data2ptr(&prev);
  8323. if( p->value )
  8324. aFree(p->value);
  8325. ers_free(str_reg_ers, p);
  8326. if( index )
  8327. script_array_update(&sd->regs, reg, true);
  8328. }
  8329. }
  8330. return true;
  8331. }
  8332. /**
  8333. * Serves the following variable types:
  8334. * - 'type' (permanent numeric char reg)
  8335. * - '#type' (permanent numeric account reg)
  8336. * - '##type' (permanent numeric account reg2)
  8337. **/
  8338. int pc_readregistry(struct map_session_data *sd, int64 reg)
  8339. {
  8340. struct script_reg_num *p = NULL;
  8341. if (!sd->vars_ok) {
  8342. ShowError("pc_readregistry: Trying to read reg %s before it's been loaded!\n", get_str(script_getvarid(reg)));
  8343. //This really shouldn't happen, so it's possible the data was lost somewhere, we should request it again.
  8344. //intif->request_registry(sd,type==3?4:type);
  8345. set_eof(sd->fd);
  8346. return 0;
  8347. }
  8348. p = (struct script_reg_num *)i64db_get(sd->regs.vars, reg);
  8349. return p ? p->value : 0;
  8350. }
  8351. /**
  8352. * Serves the following variable types:
  8353. * - 'type$' (permanent str char reg)
  8354. * - '#type$' (permanent str account reg)
  8355. * - '##type$' (permanent str account reg2)
  8356. **/
  8357. char* pc_readregistry_str(struct map_session_data *sd, int64 reg)
  8358. {
  8359. struct script_reg_str *p = NULL;
  8360. if (!sd->vars_ok) {
  8361. ShowError("pc_readregistry_str: Trying to read reg %s before it's been loaded!\n", get_str(script_getvarid(reg)));
  8362. //This really shouldn't happen, so it's possible the data was lost somewhere, we should request it again.
  8363. //intif->request_registry(sd,type==3?4:type);
  8364. set_eof(sd->fd);
  8365. return NULL;
  8366. }
  8367. p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg);
  8368. return p ? p->value : NULL;
  8369. }
  8370. /**
  8371. * Serves the following variable types:
  8372. * - 'type' (permanent numeric char reg)
  8373. * - '#type' (permanent numeric account reg)
  8374. * - '##type' (permanent numeric account reg2)
  8375. **/
  8376. int pc_setregistry(struct map_session_data *sd, int64 reg, int val)
  8377. {
  8378. struct script_reg_num *p = NULL;
  8379. const char *regname = get_str(script_getvarid(reg));
  8380. unsigned int index = script_getvaridx(reg);
  8381. if ( !reg_load && !sd->vars_ok ) {
  8382. ShowError("pc_setregistry : refusing to set %s until vars are received.\n", regname);
  8383. return 0;
  8384. }
  8385. if ((p = (struct script_reg_num *)i64db_get(sd->regs.vars, reg))) {
  8386. if( val ) {
  8387. if( !p->value && index ) /* its a entry that was deleted, so we reset array */
  8388. script_array_update(&sd->regs, reg, false);
  8389. p->value = val;
  8390. } else {
  8391. p->value = 0;
  8392. if( index )
  8393. script_array_update(&sd->regs, reg, true);
  8394. }
  8395. if (!reg_load)
  8396. p->flag.update = 1;/* either way, it will require either delete or replace */
  8397. } else if( val ) {
  8398. DBData prev;
  8399. if( index )
  8400. script_array_update(&sd->regs, reg, false);
  8401. p = ers_alloc(num_reg_ers, struct script_reg_num);
  8402. p->value = val;
  8403. if (!reg_load)
  8404. p->flag.update = 1;
  8405. if (sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev)) {
  8406. p = (struct script_reg_num *)db_data2ptr(&prev);
  8407. ers_free(num_reg_ers, p);
  8408. }
  8409. }
  8410. if (!reg_load && p)
  8411. sd->vars_dirty = true;
  8412. return 1;
  8413. }
  8414. /**
  8415. * Serves the following variable types:
  8416. * - 'type$' (permanent str char reg)
  8417. * - '#type$' (permanent str account reg)
  8418. * - '##type$' (permanent str account reg2)
  8419. **/
  8420. int pc_setregistry_str(struct map_session_data *sd, int64 reg, const char *val)
  8421. {
  8422. struct script_reg_str *p = NULL;
  8423. const char *regname = get_str(script_getvarid(reg));
  8424. unsigned int index = script_getvaridx(reg);
  8425. size_t vlen = 0;
  8426. if (!reg_load && !sd->vars_ok) {
  8427. ShowError("pc_setregistry_str : refusing to set %s until vars are received.\n", regname);
  8428. return 0;
  8429. }
  8430. if ( !script_check_RegistryVariableLength(1, val, &vlen ) )
  8431. {
  8432. 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);
  8433. return 0;
  8434. }
  8435. if( (p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg) ) ) {
  8436. if( val[0] ) {
  8437. if( p->value )
  8438. aFree(p->value);
  8439. else if ( index ) // an entry that was deleted, so we reset
  8440. script_array_update(&sd->regs, reg, false);
  8441. p->value = aStrdup(val);
  8442. } else {
  8443. if (p->value)
  8444. aFree(p->value);
  8445. p->value = NULL;
  8446. if( index )
  8447. script_array_update(&sd->regs, reg, true);
  8448. }
  8449. if( !reg_load )
  8450. p->flag.update = 1; // either way, it will require either delete or replace
  8451. } else if( val[0] ) {
  8452. DBData prev;
  8453. if( index )
  8454. script_array_update(&sd->regs, reg, false);
  8455. p = ers_alloc(str_reg_ers, struct script_reg_str);
  8456. p->value = aStrdup(val);
  8457. if( !reg_load )
  8458. p->flag.update = 1;
  8459. p->flag.type = 1;
  8460. if( sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev) ) {
  8461. p = (struct script_reg_str *)db_data2ptr(&prev);
  8462. if( p->value )
  8463. aFree(p->value);
  8464. ers_free(str_reg_ers, p);
  8465. }
  8466. }
  8467. if( !reg_load && p )
  8468. sd->vars_dirty = true;
  8469. return 1;
  8470. }
  8471. /**
  8472. * Set value of player variable
  8473. * @param sd Player
  8474. * @param reg Variable name
  8475. * @param value
  8476. * @return True if success, false if failed.
  8477. **/
  8478. bool pc_setreg2(struct map_session_data *sd, const char *reg, int val) {
  8479. char prefix = reg[0];
  8480. nullpo_retr(false, sd);
  8481. if (reg[strlen(reg)-1] == '$') {
  8482. ShowError("pc_setreg2: Invalid variable scope '%s'\n", reg);
  8483. return false;
  8484. }
  8485. val = cap_value(val, INT_MIN, INT_MAX);
  8486. switch (prefix) {
  8487. case '.':
  8488. case '\'':
  8489. case '$':
  8490. ShowError("pc_setreg2: Invalid variable scope '%s'\n", reg);
  8491. return false;
  8492. case '@':
  8493. return pc_setreg(sd, add_str(reg), val);
  8494. case '#':
  8495. return (reg[1] == '#') ? pc_setaccountreg2(sd, add_str(reg), val) > 0 : pc_setaccountreg(sd, add_str(reg), val) > 0;
  8496. default:
  8497. return pc_setglobalreg(sd, add_str(reg), val) > 0;
  8498. }
  8499. return false;
  8500. }
  8501. /**
  8502. * Get value of player variable
  8503. * @param sd Player
  8504. * @param reg Variable name
  8505. * @return Variable value or 0 if failed.
  8506. **/
  8507. int pc_readreg2(struct map_session_data *sd, const char *reg) {
  8508. char prefix = reg[0];
  8509. nullpo_ret(sd);
  8510. if (reg[strlen(reg)-1] == '$') {
  8511. ShowError("pc_readreg2: Invalid variable scope '%s'\n", reg);
  8512. return 0;
  8513. }
  8514. switch (prefix) {
  8515. case '.':
  8516. case '\'':
  8517. case '$':
  8518. ShowError("pc_readreg2: Invalid variable scope '%s'\n", reg);
  8519. return 0;
  8520. case '@':
  8521. return pc_readreg(sd, add_str(reg));
  8522. case '#':
  8523. return (reg[1] == '#') ? pc_readaccountreg2(sd, add_str(reg)) : pc_readaccountreg(sd, add_str(reg));
  8524. default:
  8525. return pc_readglobalreg(sd, add_str(reg));
  8526. }
  8527. return 0;
  8528. }
  8529. /*==========================================
  8530. * Exec eventtimer for player sd (retrieved from map_session (id))
  8531. *------------------------------------------*/
  8532. static TIMER_FUNC(pc_eventtimer){
  8533. struct map_session_data *sd=map_id2sd(id);
  8534. char *p = (char *)data;
  8535. int i;
  8536. if(sd==NULL)
  8537. return 0;
  8538. ARR_FIND( 0, MAX_EVENTTIMER, i, sd->eventtimer[i] == tid );
  8539. if( i < MAX_EVENTTIMER )
  8540. {
  8541. sd->eventtimer[i] = INVALID_TIMER;
  8542. sd->eventcount--;
  8543. npc_event(sd,p,0);
  8544. }
  8545. else
  8546. ShowError("pc_eventtimer: no such event timer\n");
  8547. if (p) aFree(p);
  8548. return 0;
  8549. }
  8550. /*==========================================
  8551. * Add eventtimer for player sd ?
  8552. *------------------------------------------*/
  8553. bool pc_addeventtimer(struct map_session_data *sd,int tick,const char *name)
  8554. {
  8555. int i;
  8556. nullpo_retr(false,sd);
  8557. ARR_FIND( 0, MAX_EVENTTIMER, i, sd->eventtimer[i] == INVALID_TIMER );
  8558. if( i == MAX_EVENTTIMER )
  8559. return false;
  8560. sd->eventtimer[i] = add_timer(gettick()+tick, pc_eventtimer, sd->bl.id, (intptr_t)aStrdup(name));
  8561. sd->eventcount++;
  8562. return true;
  8563. }
  8564. /*==========================================
  8565. * Del eventtimer for player sd ?
  8566. *------------------------------------------*/
  8567. bool pc_deleventtimer(struct map_session_data *sd,const char *name)
  8568. {
  8569. char* p = NULL;
  8570. int i;
  8571. nullpo_retr(false,sd);
  8572. if (sd->eventcount == 0)
  8573. return false;
  8574. // find the named event timer
  8575. ARR_FIND( 0, MAX_EVENTTIMER, i,
  8576. sd->eventtimer[i] != INVALID_TIMER &&
  8577. (p = (char *)(get_timer(sd->eventtimer[i])->data)) != NULL &&
  8578. strcmp(p, name) == 0
  8579. );
  8580. if( i == MAX_EVENTTIMER )
  8581. return false; // not found
  8582. delete_timer(sd->eventtimer[i],pc_eventtimer);
  8583. sd->eventtimer[i] = INVALID_TIMER;
  8584. if(sd->eventcount > 0)
  8585. sd->eventcount--;
  8586. aFree(p);
  8587. return true;
  8588. }
  8589. /*==========================================
  8590. * Update eventtimer count for player sd
  8591. *------------------------------------------*/
  8592. void pc_addeventtimercount(struct map_session_data *sd,const char *name,int tick)
  8593. {
  8594. int i;
  8595. nullpo_retv(sd);
  8596. for(i=0;i<MAX_EVENTTIMER;i++)
  8597. if( sd->eventtimer[i] != INVALID_TIMER && strcmp(
  8598. (char *)(get_timer(sd->eventtimer[i])->data), name)==0 ){
  8599. addt_tickimer(sd->eventtimer[i],tick);
  8600. break;
  8601. }
  8602. }
  8603. /*==========================================
  8604. * Remove all eventtimer for player sd
  8605. *------------------------------------------*/
  8606. void pc_cleareventtimer(struct map_session_data *sd)
  8607. {
  8608. int i;
  8609. nullpo_retv(sd);
  8610. if (sd->eventcount == 0)
  8611. return;
  8612. for(i=0;i<MAX_EVENTTIMER;i++){
  8613. if( sd->eventtimer[i] != INVALID_TIMER ){
  8614. char *p = (char *)(get_timer(sd->eventtimer[i])->data);
  8615. delete_timer(sd->eventtimer[i],pc_eventtimer);
  8616. sd->eventtimer[i] = INVALID_TIMER;
  8617. if(sd->eventcount > 0) //avoid looping to max val
  8618. sd->eventcount--;
  8619. if (p) aFree(p);
  8620. }
  8621. }
  8622. }
  8623. /**
  8624. * Called when an item with combo is worn
  8625. * @param *sd
  8626. * @param *data struct item_data
  8627. * @return success numbers of succeed combo
  8628. */
  8629. static int pc_checkcombo(struct map_session_data *sd, struct item_data *data) {
  8630. uint16 i;
  8631. int success = 0;
  8632. for( i = 0; i < data->combos_count; i++ ) {
  8633. struct itemchk {
  8634. int idx;
  8635. unsigned short nameid;
  8636. short card[MAX_SLOTS];
  8637. } *combo_idx;
  8638. int idx, j;
  8639. int nb_itemCombo;
  8640. unsigned int pos = 0;
  8641. /* ensure this isn't a duplicate combo */
  8642. if( sd->combos.bonus != NULL ) {
  8643. int x;
  8644. ARR_FIND( 0, sd->combos.count, x, sd->combos.id[x] == data->combos[i]->id );
  8645. /* found a match, skip this combo */
  8646. if( x < sd->combos.count )
  8647. continue;
  8648. }
  8649. nb_itemCombo = data->combos[i]->count;
  8650. if(nb_itemCombo<2) //a combo with less then 2 item ?? how that possible
  8651. continue;
  8652. CREATE(combo_idx,struct itemchk,nb_itemCombo);
  8653. for(j=0; j < nb_itemCombo; j++){
  8654. combo_idx[j].idx=-1;
  8655. combo_idx[j].nameid=-1;
  8656. memset(combo_idx[j].card,-1,MAX_SLOTS);
  8657. }
  8658. for( j = 0; j < nb_itemCombo; j++ ) {
  8659. uint16 id = data->combos[i]->nameid[j], k;
  8660. bool found = false;
  8661. for( k = 0; k < EQI_MAX; k++ ) {
  8662. short index = sd->equip_index[k];
  8663. if( index < 0 )
  8664. continue;
  8665. if( pc_is_same_equip_index((enum equip_index)k, sd->equip_index, index) )
  8666. continue;
  8667. if (!sd->inventory_data[index] )
  8668. continue;
  8669. if ( itemdb_type(id) != IT_CARD ) {
  8670. if ( sd->inventory_data[index]->nameid != id )
  8671. continue;
  8672. if(j>0){ //check if this item not already used
  8673. bool do_continue = false; //used to continue that specific loop with some check that also use some loop
  8674. uint8 z;
  8675. for (z = 0; z < nb_itemCombo-1; z++)
  8676. if(combo_idx[z].idx == index && combo_idx[z].nameid == id) //we already have that index recorded
  8677. do_continue=true;
  8678. if(do_continue)
  8679. continue;
  8680. }
  8681. combo_idx[j].nameid = id;
  8682. combo_idx[j].idx = index;
  8683. pos |= sd->inventory.u.items_inventory[index].equip;
  8684. found = true;
  8685. break;
  8686. } else { //Cards and enchants
  8687. uint16 z;
  8688. if ( itemdb_isspecial(sd->inventory.u.items_inventory[index].card[0]) )
  8689. continue;
  8690. for (z = 0; z < MAX_SLOTS; z++) {
  8691. bool do_continue=false;
  8692. if (sd->inventory.u.items_inventory[index].card[z] != id)
  8693. continue;
  8694. if(j>0){
  8695. int c1, c2;
  8696. for (c1 = 0; c1 < nb_itemCombo-1; c1++){
  8697. if(combo_idx[c1].idx == index && combo_idx[c1].nameid == id){
  8698. for (c2 = 0; c2 < MAX_SLOTS; c2++){
  8699. if(combo_idx[c1].card[c2] == id){ //we already have that card recorded (at this same idx)
  8700. do_continue = true;
  8701. break;
  8702. }
  8703. }
  8704. }
  8705. }
  8706. }
  8707. if(do_continue)
  8708. continue;
  8709. combo_idx[j].nameid = id;
  8710. combo_idx[j].idx = index;
  8711. combo_idx[j].card[z] = id;
  8712. pos |= sd->inventory.u.items_inventory[index].equip;
  8713. found = true;
  8714. break;
  8715. }
  8716. }
  8717. }
  8718. if( !found )
  8719. break;/* we haven't found all the ids for this combo, so we can return */
  8720. }
  8721. aFree(combo_idx);
  8722. /* means we broke out of the count loop w/o finding all ids, we can move to the next combo */
  8723. if( j < nb_itemCombo )
  8724. continue;
  8725. /* we got here, means all items in the combo are matching */
  8726. idx = sd->combos.count;
  8727. if( sd->combos.bonus == NULL ) {
  8728. CREATE(sd->combos.bonus, struct script_code *, 1);
  8729. CREATE(sd->combos.id, unsigned short, 1);
  8730. CREATE(sd->combos.pos, unsigned int, 1);
  8731. sd->combos.count = 1;
  8732. } else {
  8733. RECREATE(sd->combos.bonus, struct script_code *, ++sd->combos.count);
  8734. RECREATE(sd->combos.id, unsigned short, sd->combos.count);
  8735. RECREATE(sd->combos.pos, unsigned int, sd->combos.count);
  8736. }
  8737. /* we simply copy the pointer */
  8738. sd->combos.bonus[idx] = data->combos[i]->script;
  8739. /* save this combo's id */
  8740. sd->combos.id[idx] = data->combos[i]->id;
  8741. /* save pos of combo*/
  8742. sd->combos.pos[idx] = pos;
  8743. success++;
  8744. }
  8745. return success;
  8746. }
  8747. /**
  8748. * Called when an item with combo is removed
  8749. * @param *sd
  8750. * @param *data struct item_data
  8751. * @return retval numbers of removed combo
  8752. */
  8753. static int pc_removecombo(struct map_session_data *sd, struct item_data *data ) {
  8754. int i, retval = 0;
  8755. if( sd->combos.bonus == NULL )
  8756. return 0;/* nothing to do here, player has no combos */
  8757. for( i = 0; i < data->combos_count; i++ ) {
  8758. /* check if this combo exists in this user */
  8759. int x = 0, cursor = 0, j;
  8760. ARR_FIND( 0, sd->combos.count, x, sd->combos.id[x] == data->combos[i]->id );
  8761. /* no match, skip this combo */
  8762. if(x >= sd->combos.count)
  8763. continue;
  8764. sd->combos.bonus[x] = NULL;
  8765. sd->combos.id[x] = 0;
  8766. sd->combos.pos[x] = 0;
  8767. retval++;
  8768. /* check if combo requirements still fit */
  8769. if( pc_checkcombo( sd, data ) )
  8770. continue;
  8771. /* move next value to empty slot */
  8772. for( j = 0, cursor = 0; j < sd->combos.count; j++ ) {
  8773. if( sd->combos.bonus[j] == NULL )
  8774. continue;
  8775. if( cursor != j ) {
  8776. sd->combos.bonus[cursor] = sd->combos.bonus[j];
  8777. sd->combos.id[cursor] = sd->combos.id[j];
  8778. sd->combos.pos[cursor] = sd->combos.pos[j];
  8779. }
  8780. cursor++;
  8781. }
  8782. /* it's empty, we can clear all the memory */
  8783. if( (sd->combos.count = cursor) == 0 ) {
  8784. aFree(sd->combos.bonus);
  8785. aFree(sd->combos.id);
  8786. aFree(sd->combos.pos);
  8787. sd->combos.bonus = NULL;
  8788. sd->combos.id = NULL;
  8789. sd->combos.pos = NULL;
  8790. return retval; /* we also can return at this point for we have no more combos to check */
  8791. }
  8792. }
  8793. return retval;
  8794. }
  8795. /**
  8796. * Load combo data(s) of player
  8797. * @param *sd
  8798. * @return ret numbers of succeed combo
  8799. */
  8800. int pc_load_combo(struct map_session_data *sd) {
  8801. int i, ret = 0;
  8802. for( i = 0; i < EQI_MAX; i++ ) {
  8803. struct item_data *id = NULL;
  8804. short idx = sd->equip_index[i];
  8805. if( idx < 0 || !(id = sd->inventory_data[idx] ) )
  8806. continue;
  8807. if( id->combos_count )
  8808. ret += pc_checkcombo(sd,id);
  8809. if(!itemdb_isspecial(sd->inventory.u.items_inventory[idx].card[0])) {
  8810. struct item_data *data;
  8811. int j;
  8812. for( j = 0; j < MAX_SLOTS; j++ ) {
  8813. if (!sd->inventory.u.items_inventory[idx].card[j])
  8814. continue;
  8815. if ( ( data = itemdb_exists(sd->inventory.u.items_inventory[idx].card[j]) ) != NULL ) {
  8816. if( data->combos_count )
  8817. ret += pc_checkcombo(sd,data);
  8818. }
  8819. }
  8820. }
  8821. }
  8822. return ret;
  8823. }
  8824. /*==========================================
  8825. * Equip item on player sd at req_pos from inventory index n
  8826. * return: false - fail; true - success
  8827. *------------------------------------------*/
  8828. bool pc_equipitem(struct map_session_data *sd,short n,int req_pos,bool equipswitch)
  8829. {
  8830. int i, pos, flag = 0, iflag;
  8831. struct item_data *id;
  8832. uint8 res = ITEM_EQUIP_ACK_OK;
  8833. short* equip_index;
  8834. nullpo_retr(false,sd);
  8835. if( n < 0 || n >= MAX_INVENTORY ) {
  8836. if( equipswitch ){
  8837. clif_equipswitch_add( sd, n, req_pos, true );
  8838. }else{
  8839. clif_equipitemack(sd,0,0,ITEM_EQUIP_ACK_FAIL);
  8840. }
  8841. return false;
  8842. }
  8843. if( DIFF_TICK(sd->canequip_tick,gettick()) > 0 ) {
  8844. if( equipswitch ){
  8845. clif_equipswitch_add( sd, n, req_pos, true );
  8846. }else{
  8847. clif_equipitemack(sd,n,0,ITEM_EQUIP_ACK_FAIL);
  8848. }
  8849. return false;
  8850. }
  8851. if (!(id = sd->inventory_data[n]))
  8852. return false;
  8853. pos = pc_equippoint(sd,n); //With a few exceptions, item should go in all specified slots.
  8854. if(battle_config.battle_log && !equipswitch)
  8855. ShowInfo("equip %hu (%d) %x:%x\n",sd->inventory.u.items_inventory[n].nameid,n,id?id->equip:0,req_pos);
  8856. if((res = pc_isequip(sd,n))) {
  8857. if( equipswitch ){
  8858. clif_equipswitch_add( sd, n, req_pos, true );
  8859. }else{
  8860. clif_equipitemack(sd,n,0,res); // fail
  8861. }
  8862. return false;
  8863. }
  8864. if( equipswitch && id->type == IT_AMMO ){
  8865. clif_equipswitch_add( sd, n, req_pos, true );
  8866. return false;
  8867. }
  8868. if (!(pos&req_pos) || sd->inventory.u.items_inventory[n].equip != 0 || sd->inventory.u.items_inventory[n].attribute==1 ) { // [Valaris]
  8869. if( equipswitch ){
  8870. clif_equipswitch_add( sd, n, req_pos, true );
  8871. }else{
  8872. clif_equipitemack(sd,n,0,ITEM_EQUIP_ACK_FAIL); // fail
  8873. }
  8874. return false;
  8875. }
  8876. if( sd->sc.count && (sd->sc.data[SC_BERSERK] || sd->sc.data[SC_SATURDAYNIGHTFEVER] ||
  8877. sd->sc.data[SC_KYOUGAKU] || (sd->sc.data[SC_PYROCLASTIC] && sd->inventory_data[n]->type == IT_WEAPON)) ) {
  8878. if( equipswitch ){
  8879. clif_equipswitch_add( sd, n, req_pos, true );
  8880. }else{
  8881. clif_equipitemack(sd,n,0,ITEM_EQUIP_ACK_FAIL); //Fail
  8882. }
  8883. return false;
  8884. }
  8885. equip_index = equipswitch ? sd->equip_switch_index : sd->equip_index;
  8886. if ( !equipswitch && id->flag.bindOnEquip && !sd->inventory.u.items_inventory[n].bound) {
  8887. sd->inventory.u.items_inventory[n].bound = (char)battle_config.default_bind_on_equip;
  8888. clif_notify_bindOnEquip(sd,n);
  8889. }
  8890. if(pos == EQP_ACC) { //Accessories should only go in one of the two.
  8891. pos = req_pos&EQP_ACC;
  8892. if (pos == EQP_ACC) //User specified both slots.
  8893. pos = equip_index[EQI_ACC_R] >= 0 ? EQP_ACC_L : EQP_ACC_R;
  8894. for (i = 0; i < sd->inventory_data[n]->slot; i++) { // Accessories that have cards that force equip location
  8895. if (!sd->inventory.u.items_inventory[n].card[i])
  8896. continue;
  8897. struct item_data *card_data = itemdb_exists(sd->inventory.u.items_inventory[n].card[i]);
  8898. if (card_data) {
  8899. int card_pos = card_data->equip;
  8900. if (card_pos == EQP_ACC_L || card_pos == EQP_ACC_R) {
  8901. pos = card_pos; // Use the card's equip position
  8902. break;
  8903. }
  8904. }
  8905. }
  8906. } else if(pos == EQP_ARMS && id->equip == EQP_HAND_R) { //Dual wield capable weapon.
  8907. pos = (req_pos&EQP_ARMS);
  8908. if (pos == EQP_ARMS) //User specified both slots, pick one for them.
  8909. pos = equip_index[EQI_HAND_R] >= 0 ? EQP_HAND_L : EQP_HAND_R;
  8910. } else if(pos == EQP_SHADOW_ACC) { // Shadow System
  8911. pos = req_pos&EQP_SHADOW_ACC;
  8912. if (pos == EQP_SHADOW_ACC)
  8913. pos = equip_index[EQI_SHADOW_ACC_L] >= 0 ? EQP_SHADOW_ACC_R : EQP_SHADOW_ACC_L;
  8914. } else if(pos == EQP_SHADOW_ARMS && id->equip == EQP_SHADOW_WEAPON) {
  8915. pos = (req_pos&EQP_SHADOW_ARMS);
  8916. if( pos == EQP_SHADOW_ARMS )
  8917. pos = (equip_index[EQI_SHADOW_WEAPON] >= 0 ? EQP_SHADOW_SHIELD : EQP_SHADOW_WEAPON);
  8918. }
  8919. if (pos&EQP_HAND_R && battle_config.use_weapon_skill_range&BL_PC) {
  8920. //Update skill-block range database when weapon range changes. [Skotlex]
  8921. i = equip_index[EQI_HAND_R];
  8922. if (i < 0 || !sd->inventory_data[i]) //No data, or no weapon equipped
  8923. flag = 1;
  8924. else
  8925. flag = id->range != sd->inventory_data[i]->range;
  8926. }
  8927. if( equipswitch ){
  8928. for( i = 0; i < EQI_MAX; i++ ){
  8929. if( pos&equip_bitmask[i] ){
  8930. // If there was already an item assigned to this slot
  8931. if( sd->equip_switch_index[i] >= 0 ){
  8932. pc_equipswitch_remove( sd, sd->equip_switch_index[i] );
  8933. }
  8934. // Assign the new index to it
  8935. sd->equip_switch_index[i] = n;
  8936. }
  8937. }
  8938. sd->inventory.u.items_inventory[n].equipSwitch = pos;
  8939. clif_equipswitch_add( sd, n, pos, false );
  8940. return true;
  8941. }else{
  8942. for(i=0;i<EQI_MAX;i++) {
  8943. if(pos & equip_bitmask[i]) {
  8944. if(sd->equip_index[i] >= 0) //Slot taken, remove item from there.
  8945. pc_unequipitem(sd,sd->equip_index[i],2 | 4);
  8946. sd->equip_index[i] = n;
  8947. }
  8948. }
  8949. pc_equipswitch_remove(sd, n);
  8950. if(pos==EQP_AMMO) {
  8951. clif_arrowequip(sd,n);
  8952. clif_arrow_fail(sd,3);
  8953. }
  8954. else
  8955. clif_equipitemack(sd,n,pos,ITEM_EQUIP_ACK_OK);
  8956. sd->inventory.u.items_inventory[n].equip = pos;
  8957. }
  8958. if(pos & EQP_HAND_R) {
  8959. if(id)
  8960. sd->weapontype1 = id->look;
  8961. else
  8962. sd->weapontype1 = 0;
  8963. pc_calcweapontype(sd);
  8964. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  8965. }
  8966. if(pos & EQP_HAND_L) {
  8967. if(id) {
  8968. if(id->type == IT_WEAPON) {
  8969. sd->status.shield = 0;
  8970. sd->weapontype2 = id->look;
  8971. }
  8972. else
  8973. if(id->type == IT_ARMOR) {
  8974. sd->status.shield = id->look;
  8975. sd->weapontype2 = 0;
  8976. }
  8977. }
  8978. else
  8979. sd->status.shield = sd->weapontype2 = 0;
  8980. pc_calcweapontype(sd);
  8981. clif_changelook(&sd->bl,LOOK_SHIELD,sd->status.shield);
  8982. }
  8983. if(pos & EQP_SHOES)
  8984. clif_changelook(&sd->bl,LOOK_SHOES,0);
  8985. if (battle_config.ammo_unequip && (pos&EQP_ARMS) && id->type == IT_WEAPON) {
  8986. short idx = sd->equip_index[EQI_AMMO];
  8987. if (idx >= 0) {
  8988. switch (sd->inventory_data[idx]->look) {
  8989. case AMMO_ARROW:
  8990. if (id->look != W_BOW && id->look != W_MUSICAL && id->look != W_WHIP)
  8991. pc_unequipitem(sd, idx, 2 | 4);
  8992. break;
  8993. case AMMO_BULLET:
  8994. case AMMO_SHELL:
  8995. if (id->look != W_REVOLVER && id->look != W_RIFLE && id->look != W_GATLING && id->look != W_SHOTGUN
  8996. #ifdef RENEWAL
  8997. && id->look != W_GRENADE
  8998. #endif
  8999. )
  9000. pc_unequipitem(sd, idx, 2 | 4);
  9001. break;
  9002. #ifndef RENEWAL
  9003. case AMMO_GRENADE:
  9004. if (id->look != W_GRENADE)
  9005. pc_unequipitem(sd, idx, 2 | 4);
  9006. break;
  9007. #endif
  9008. }
  9009. }
  9010. }
  9011. pc_set_costume_view(sd);
  9012. pc_checkallowskill(sd); //Check if status changes should be halted.
  9013. iflag = sd->npc_item_flag;
  9014. /* check for combos (MUST be before status_calc_pc) */
  9015. if( id->combos_count )
  9016. pc_checkcombo(sd,id);
  9017. if(itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  9018. ; //No cards
  9019. else {
  9020. for( i = 0; i < MAX_SLOTS; i++ ) {
  9021. struct item_data *data;
  9022. if (!sd->inventory.u.items_inventory[n].card[i])
  9023. continue;
  9024. if ( ( data = itemdb_exists(sd->inventory.u.items_inventory[n].card[i]) ) != NULL ) {
  9025. if( data->combos_count )
  9026. pc_checkcombo(sd,data);
  9027. }
  9028. }
  9029. }
  9030. status_calc_pc(sd,SCO_NONE);
  9031. if (flag) //Update skill data
  9032. clif_skillinfoblock(sd);
  9033. //OnEquip script [Skotlex]
  9034. if (id) {
  9035. //only run the script if item isn't restricted
  9036. if (id->equip_script && (pc_has_permission(sd,PC_PERM_USE_ALL_EQUIPMENT) || !itemdb_isNoEquip(id,sd->bl.m)))
  9037. run_script(id->equip_script,0,sd->bl.id,fake_nd->bl.id);
  9038. if(itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  9039. ; //No cards
  9040. else {
  9041. for( i = 0; i < MAX_SLOTS; i++ ) {
  9042. struct item_data *data;
  9043. if (!sd->inventory.u.items_inventory[n].card[i])
  9044. continue;
  9045. if ( ( data = itemdb_exists(sd->inventory.u.items_inventory[n].card[i]) ) != NULL ) {
  9046. if (data->equip_script && (pc_has_permission(sd,PC_PERM_USE_ALL_EQUIPMENT) || !itemdb_isNoEquip(data,sd->bl.m)))
  9047. run_script(data->equip_script,0,sd->bl.id,fake_nd->bl.id);
  9048. }
  9049. }
  9050. }
  9051. }
  9052. sd->npc_item_flag = iflag;
  9053. return true;
  9054. }
  9055. /**
  9056. * Recalculate player status on unequip
  9057. * @param sd: Player data
  9058. * @param n: Item inventory index
  9059. * @param flag: Whether to recalculate a player's status or not
  9060. * @return True on success or false on failure
  9061. */
  9062. static void pc_unequipitem_sub(struct map_session_data *sd, int n, int flag) {
  9063. int i, iflag;
  9064. bool status_calc = false;
  9065. if (sd->state.autobonus&sd->inventory.u.items_inventory[n].equip)
  9066. sd->state.autobonus &= ~sd->inventory.u.items_inventory[n].equip; //Check for activated autobonus [Inkfish]
  9067. sd->inventory.u.items_inventory[n].equip = 0;
  9068. pc_checkallowskill(sd);
  9069. iflag = sd->npc_item_flag;
  9070. /* check for combos (MUST be before status_calc_pc) */
  9071. if (sd->inventory_data[n]) {
  9072. if (sd->inventory_data[n]->combos_count) {
  9073. if (pc_removecombo(sd, sd->inventory_data[n]))
  9074. status_calc = true;
  9075. }
  9076. if (itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  9077. ; //No cards
  9078. else {
  9079. for (i = 0; i < MAX_SLOTS; i++) {
  9080. struct item_data *data;
  9081. if (!sd->inventory.u.items_inventory[n].card[i])
  9082. continue;
  9083. if ((data = itemdb_exists(sd->inventory.u.items_inventory[n].card[i])) != NULL) {
  9084. if (data->combos_count) {
  9085. if (pc_removecombo(sd, data))
  9086. status_calc = true;
  9087. }
  9088. }
  9089. }
  9090. }
  9091. }
  9092. if (status_calc)
  9093. status_calc_pc(sd, SCO_NONE);
  9094. if (sd->sc.data[SC_SIGNUMCRUCIS] && !battle_check_undead(sd->battle_status.race, sd->battle_status.def_ele))
  9095. status_change_end(&sd->bl, SC_SIGNUMCRUCIS, INVALID_TIMER);
  9096. //OnUnEquip script [Skotlex]
  9097. if (sd->inventory_data[n]) {
  9098. if (sd->inventory_data[n]->unequip_script)
  9099. run_script(sd->inventory_data[n]->unequip_script, 0, sd->bl.id, fake_nd->bl.id);
  9100. if (itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  9101. ; //No cards
  9102. else {
  9103. for (i = 0; i < MAX_SLOTS; i++) {
  9104. struct item_data *data;
  9105. if (!sd->inventory.u.items_inventory[n].card[i])
  9106. continue;
  9107. if ((data = itemdb_exists(sd->inventory.u.items_inventory[n].card[i])) != NULL) {
  9108. if (data->unequip_script)
  9109. run_script(data->unequip_script, 0, sd->bl.id, fake_nd->bl.id);
  9110. }
  9111. }
  9112. }
  9113. }
  9114. if (flag & 1)
  9115. status_calc_pc(sd, SCO_FORCE);
  9116. sd->npc_item_flag = iflag;
  9117. }
  9118. /**
  9119. * Called when attempting to unequip an item from a player
  9120. * @param sd: Player data
  9121. * @param n: Item inventory index
  9122. * @param flag: Type of unequip
  9123. * 0 - only unequip
  9124. * 1 - calculate status after unequipping
  9125. * 2 - force unequip
  9126. * 4 - unequip by switching equipment
  9127. * @return True on success or false on failure
  9128. */
  9129. bool pc_unequipitem(struct map_session_data *sd, int n, int flag) {
  9130. int i, pos;
  9131. nullpo_retr(false,sd);
  9132. if (n < 0 || n >= MAX_INVENTORY) {
  9133. clif_unequipitemack(sd,0,0,0);
  9134. return false;
  9135. }
  9136. if (!(pos = sd->inventory.u.items_inventory[n].equip)) {
  9137. clif_unequipitemack(sd,n,0,0);
  9138. return false; //Nothing to unequip
  9139. }
  9140. // status change that makes player cannot unequip equipment
  9141. if (!(flag&2) && sd->sc.count &&
  9142. (sd->sc.data[SC_BERSERK] ||
  9143. sd->sc.data[SC_SATURDAYNIGHTFEVER] ||
  9144. sd->sc.data[SC__BLOODYLUST] ||
  9145. sd->sc.data[SC_KYOUGAKU] ||
  9146. (sd->sc.data[SC_PYROCLASTIC] &&
  9147. sd->inventory_data[n]->type == IT_WEAPON))) // can't switch weapon
  9148. {
  9149. clif_unequipitemack(sd,n,0,0);
  9150. return false;
  9151. }
  9152. if (battle_config.battle_log)
  9153. ShowInfo("unequip %d %x:%x\n",n,pc_equippoint(sd,n),pos);
  9154. for(i = 0; i < EQI_MAX; i++) {
  9155. if (pos & equip_bitmask[i])
  9156. sd->equip_index[i] = -1;
  9157. }
  9158. if(pos & EQP_HAND_R) {
  9159. sd->weapontype1 = 0;
  9160. sd->status.weapon = sd->weapontype2;
  9161. pc_calcweapontype(sd);
  9162. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  9163. if( !battle_config.dancing_weaponswitch_fix )
  9164. status_change_end(&sd->bl, SC_DANCING, INVALID_TIMER); // Unequipping => stop dancing.
  9165. #ifdef RENEWAL
  9166. if (battle_config.switch_remove_edp&2) {
  9167. #else
  9168. if (battle_config.switch_remove_edp&1) {
  9169. #endif
  9170. status_change_end(&sd->bl, SC_EDP, INVALID_TIMER);
  9171. }
  9172. }
  9173. if(pos & EQP_HAND_L) {
  9174. if (sd->status.shield && battle_getcurrentskill(&sd->bl) == LG_SHIELDSPELL)
  9175. unit_skillcastcancel(&sd->bl, 0); // Cancel Shield Spell if player swaps shields.
  9176. sd->status.shield = sd->weapontype2 = 0;
  9177. pc_calcweapontype(sd);
  9178. clif_changelook(&sd->bl,LOOK_SHIELD,sd->status.shield);
  9179. }
  9180. if(pos & EQP_SHOES)
  9181. clif_changelook(&sd->bl,LOOK_SHOES,0);
  9182. clif_unequipitemack(sd,n,pos,1);
  9183. pc_set_costume_view(sd);
  9184. status_change_end(&sd->bl,SC_HEAT_BARREL,INVALID_TIMER);
  9185. // On weapon change (right and left hand)
  9186. if ((pos & EQP_ARMS) && sd->inventory_data[n]->type == IT_WEAPON) {
  9187. if (battle_config.ammo_unequip && !(flag & 4)) {
  9188. switch (sd->inventory_data[n]->look) {
  9189. case W_BOW:
  9190. case W_MUSICAL:
  9191. case W_WHIP:
  9192. case W_REVOLVER:
  9193. case W_RIFLE:
  9194. case W_GATLING:
  9195. case W_SHOTGUN:
  9196. case W_GRENADE: {
  9197. short idx = sd->equip_index[EQI_AMMO];
  9198. if (idx >= 0) {
  9199. sd->equip_index[EQI_AMMO] = -1;
  9200. clif_unequipitemack(sd, idx, sd->inventory.u.items_inventory[idx].equip, 1);
  9201. pc_unequipitem_sub(sd, idx, 0);
  9202. }
  9203. }
  9204. break;
  9205. }
  9206. }
  9207. if (!sd->sc.data[SC_SEVENWIND] || sd->sc.data[SC_ASPERSIO]) //Check for seven wind (but not level seven!)
  9208. skill_enchant_elemental_end(&sd->bl, SC_NONE);
  9209. status_change_end(&sd->bl, SC_FEARBREEZE, INVALID_TIMER);
  9210. status_change_end(&sd->bl, SC_EXEEDBREAK, INVALID_TIMER);
  9211. }
  9212. // On armor change
  9213. if (pos & EQP_ARMOR) {
  9214. if (sd->sc.data[SC_HOVERING] && sd->inventory_data[n]->nameid == ITEMID_HOVERING_BOOSTER)
  9215. status_change_end(&sd->bl, SC_HOVERING, INVALID_TIMER);
  9216. //status_change_end(&sd->bl, SC_BENEDICTIO, INVALID_TIMER); // No longer is removed? Need confirmation
  9217. status_change_end(&sd->bl, SC_ARMOR_RESIST, INVALID_TIMER);
  9218. }
  9219. // On ammo change
  9220. 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_))
  9221. status_change_end(&sd->bl, SC_P_ALTER, INVALID_TIMER);
  9222. pc_unequipitem_sub(sd, n, flag);
  9223. return true;
  9224. }
  9225. int pc_equipswitch( struct map_session_data* sd, int index ){
  9226. // Get the target equip mask
  9227. int position = sd->inventory.u.items_inventory[index].equipSwitch;
  9228. // Get the currently equipped item
  9229. short equippedItem = pc_checkequip( sd, position, true );
  9230. // No item equipped at the target
  9231. if( equippedItem == -1 ){
  9232. // Remove it from the equip switch
  9233. pc_equipswitch_remove( sd, index );
  9234. pc_equipitem( sd, index, position );
  9235. return position;
  9236. }else{
  9237. std::map<int, int> unequipped;
  9238. int unequipped_position = 0;
  9239. // Unequip all items that interfere
  9240. for( int i = 0; i < EQI_MAX; i++ ){
  9241. int unequip_index = sd->equip_index[i];
  9242. if( unequip_index >= 0 && position & equip_bitmask[i] ){
  9243. struct item* unequip_item = &sd->inventory.u.items_inventory[unequip_index];
  9244. // Store the unequipped index and position mask for later
  9245. unequipped[unequip_index] = unequip_item->equip;
  9246. // Keep the position for later
  9247. unequipped_position |= unequip_item->equip;
  9248. // Unequip the item
  9249. pc_unequipitem( sd, unequip_index, 0 );
  9250. }
  9251. }
  9252. int all_position = position | unequipped_position;
  9253. // Equip everything that is hit by the mask
  9254. for( int i = 0; i < EQI_MAX; i++ ){
  9255. int exchange_index = sd->equip_switch_index[i];
  9256. if( exchange_index >= 0 && all_position & equip_bitmask[i] ){
  9257. struct item* exchange_item = &sd->inventory.u.items_inventory[exchange_index];
  9258. // Store the target position
  9259. int exchange_position = exchange_item->equipSwitch;
  9260. // Remove the item from equip switch
  9261. pc_equipswitch_remove( sd, exchange_index );
  9262. // Equip the item at the destinated position
  9263. pc_equipitem( sd, exchange_index, exchange_position );
  9264. }
  9265. }
  9266. // Place all unequipped items into the equip switch window
  9267. for( std::pair<int, int> pair : unequipped ){
  9268. int unequipped_index = pair.first;
  9269. int unequipped_position = pair.second;
  9270. // Rebuild the index cache
  9271. for( int i = 0; i < EQI_MAX; i++ ){
  9272. if( unequipped_position & equip_bitmask[i] ){
  9273. sd->equip_switch_index[i] = unequipped_index;
  9274. }
  9275. }
  9276. // Set the correct position mask
  9277. sd->inventory.u.items_inventory[unequipped_index].equipSwitch = unequipped_position;
  9278. // Notify the client
  9279. clif_equipswitch_add( sd, unequipped_index, unequipped_position, false );
  9280. }
  9281. return all_position;
  9282. }
  9283. }
  9284. void pc_equipswitch_remove( struct map_session_data* sd, int index ){
  9285. struct item* item = &sd->inventory.u.items_inventory[index];
  9286. if( !item->equipSwitch ){
  9287. return;
  9288. }
  9289. for( int i = 0; i < EQI_MAX; i++ ){
  9290. // If a match is found
  9291. if( sd->equip_switch_index[i] == index ){
  9292. // Remove it from the slot
  9293. sd->equip_switch_index[i] = -1;
  9294. }
  9295. }
  9296. // Send out one packet for all slots using the current item's mask
  9297. clif_equipswitch_remove( sd, index, item->equipSwitch, false );
  9298. item->equipSwitch = 0;
  9299. }
  9300. /*==========================================
  9301. * Checking if player (sd) has an invalid item
  9302. * and is unequiped on map load (item_noequip)
  9303. *------------------------------------------*/
  9304. void pc_checkitem(struct map_session_data *sd) {
  9305. int i, calc_flag = 0;
  9306. struct item* it;
  9307. nullpo_retv(sd);
  9308. if( sd->state.vending ) //Avoid reorganizing items when we are vending, as that leads to exploits (pointed out by End of Exam)
  9309. return;
  9310. pc_check_available_item(sd, ITMCHK_NONE); // Check for invalid(ated) items.
  9311. for( i = 0; i < MAX_INVENTORY; i++ ) {
  9312. it = &sd->inventory.u.items_inventory[i];
  9313. if( it->nameid == 0 )
  9314. continue;
  9315. if( !it->equip )
  9316. continue;
  9317. if( it->equip&~pc_equippoint(sd,i) ) {
  9318. pc_unequipitem(sd, i, 2);
  9319. calc_flag = 1;
  9320. continue;
  9321. }
  9322. 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) ) {
  9323. pc_unequipitem(sd, i, 2);
  9324. calc_flag = 1;
  9325. continue;
  9326. }
  9327. }
  9328. for( i = 0; i < MAX_INVENTORY; i++ ) {
  9329. it = &sd->inventory.u.items_inventory[i];
  9330. if( it->nameid == 0 )
  9331. continue;
  9332. if( !it->equipSwitch )
  9333. continue;
  9334. if( it->equipSwitch&~pc_equippoint(sd,i) ||
  9335. ( !pc_has_permission(sd, PC_PERM_USE_ALL_EQUIPMENT) && !battle_config.allow_equip_restricted_item && itemdb_isNoEquip(sd->inventory_data[i], sd->bl.m) ) ){
  9336. for( int j = 0; j < EQI_MAX; j++ ){
  9337. if( sd->equip_switch_index[j] == i ){
  9338. sd->equip_switch_index[j] = -1;
  9339. }
  9340. }
  9341. sd->inventory.u.items_inventory[i].equipSwitch = 0;
  9342. continue;
  9343. }
  9344. }
  9345. if( calc_flag && sd->state.active ) {
  9346. pc_checkallowskill(sd);
  9347. status_calc_pc(sd,SCO_NONE);
  9348. }
  9349. }
  9350. /*==========================================
  9351. * Checks for unavailable items and removes them.
  9352. * @param sd: Player data
  9353. * @param type Forced check:
  9354. * 1 - Inventory
  9355. * 2 - Cart
  9356. * 4 - Storage
  9357. *------------------------------------------*/
  9358. void pc_check_available_item(struct map_session_data *sd, uint8 type)
  9359. {
  9360. int i;
  9361. unsigned short nameid;
  9362. char output[256];
  9363. nullpo_retv(sd);
  9364. if (battle_config.item_check&ITMCHK_INVENTORY && type&ITMCHK_INVENTORY) { // Check for invalid(ated) items in inventory.
  9365. for(i = 0; i < MAX_INVENTORY; i++) {
  9366. nameid = sd->inventory.u.items_inventory[i].nameid;
  9367. if (!nameid)
  9368. continue;
  9369. if (!itemdb_available(nameid)) {
  9370. sprintf(output, msg_txt(sd, 709), nameid); // Item %hu has been removed from your inventory.
  9371. clif_displaymessage(sd->fd, output);
  9372. 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);
  9373. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 4, 0, LOG_TYPE_OTHER);
  9374. continue;
  9375. }
  9376. if (!sd->inventory.u.items_inventory[i].unique_id && !itemdb_isstackable(nameid))
  9377. sd->inventory.u.items_inventory[i].unique_id = pc_generate_unique_id(sd);
  9378. }
  9379. }
  9380. if (battle_config.item_check&ITMCHK_CART && type&ITMCHK_CART) { // Check for invalid(ated) items in cart.
  9381. for(i = 0; i < MAX_CART; i++) {
  9382. nameid = sd->cart.u.items_cart[i].nameid;
  9383. if (!nameid)
  9384. continue;
  9385. if (!itemdb_available(nameid)) {
  9386. sprintf(output, msg_txt(sd, 710), nameid); // Item %hu has been removed from your cart.
  9387. clif_displaymessage(sd->fd, output);
  9388. 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);
  9389. pc_cart_delitem(sd, i, sd->cart.u.items_cart[i].amount, 0, LOG_TYPE_OTHER);
  9390. continue;
  9391. }
  9392. if (!sd->cart.u.items_cart[i].unique_id && !itemdb_isstackable(nameid))
  9393. sd->cart.u.items_cart[i].unique_id = pc_generate_unique_id(sd);
  9394. }
  9395. }
  9396. if (battle_config.item_check&ITMCHK_STORAGE && type&ITMCHK_STORAGE) { // Check for invalid(ated) items in storage.
  9397. for(i = 0; i < sd->storage.max_amount; i++) {
  9398. nameid = sd->storage.u.items_storage[i].nameid;
  9399. if (!nameid)
  9400. continue;
  9401. if (!itemdb_available(nameid)) {
  9402. sprintf(output, msg_txt(sd, 711), nameid); // Item %hu has been removed from your storage.
  9403. clif_displaymessage(sd->fd, output);
  9404. 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);
  9405. storage_delitem(sd, &sd->storage, i, sd->storage.u.items_storage[i].amount);
  9406. continue;
  9407. }
  9408. if (!sd->storage.u.items_storage[i].unique_id && !itemdb_isstackable(nameid))
  9409. sd->storage.u.items_storage[i].unique_id = pc_generate_unique_id(sd);
  9410. }
  9411. }
  9412. }
  9413. /*==========================================
  9414. * Update PVP rank for sd1 in cmp to sd2
  9415. *------------------------------------------*/
  9416. static int pc_calc_pvprank_sub(struct block_list *bl,va_list ap)
  9417. {
  9418. struct map_session_data *sd1,*sd2;
  9419. sd1=(struct map_session_data *)bl;
  9420. sd2=va_arg(ap,struct map_session_data *);
  9421. if( pc_isinvisible(sd1) || pc_isinvisible(sd2) )
  9422. {// cannot register pvp rank for hidden GMs
  9423. return 0;
  9424. }
  9425. if( sd1->pvp_point > sd2->pvp_point )
  9426. sd2->pvp_rank++;
  9427. return 0;
  9428. }
  9429. /*==========================================
  9430. * Calculate new rank beetween all present players (map_foreachinallarea)
  9431. * and display result
  9432. *------------------------------------------*/
  9433. int pc_calc_pvprank(struct map_session_data *sd)
  9434. {
  9435. int old = sd->pvp_rank;
  9436. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  9437. sd->pvp_rank=1;
  9438. map_foreachinmap(pc_calc_pvprank_sub,sd->bl.m,BL_PC,sd);
  9439. if(old!=sd->pvp_rank || sd->pvp_lastusers!=mapdata->users_pvp)
  9440. clif_pvpset(sd,sd->pvp_rank,sd->pvp_lastusers=mapdata->users_pvp,0);
  9441. return sd->pvp_rank;
  9442. }
  9443. /*==========================================
  9444. * Calculate next sd ranking calculation from config
  9445. *------------------------------------------*/
  9446. TIMER_FUNC(pc_calc_pvprank_timer){
  9447. struct map_session_data *sd;
  9448. sd=map_id2sd(id);
  9449. if(sd==NULL)
  9450. return 0;
  9451. sd->pvp_timer = INVALID_TIMER;
  9452. if( pc_isinvisible(sd) )
  9453. {// do not calculate the pvp rank for a hidden GM
  9454. return 0;
  9455. }
  9456. if( pc_calc_pvprank(sd) > 0 )
  9457. sd->pvp_timer = add_timer(gettick()+PVP_CALCRANK_INTERVAL,pc_calc_pvprank_timer,id,data);
  9458. return 0;
  9459. }
  9460. /*==========================================
  9461. * Checking if sd is married
  9462. * Return:
  9463. * partner_id = yes
  9464. * 0 = no
  9465. *------------------------------------------*/
  9466. int pc_ismarried(struct map_session_data *sd)
  9467. {
  9468. if(sd == NULL)
  9469. return -1;
  9470. if(sd->status.partner_id > 0)
  9471. return sd->status.partner_id;
  9472. else
  9473. return 0;
  9474. }
  9475. /*==========================================
  9476. * Marry player sd to player dstsd
  9477. * Return:
  9478. * false = fail
  9479. * true = success
  9480. *------------------------------------------*/
  9481. bool pc_marriage(struct map_session_data *sd,struct map_session_data *dstsd)
  9482. {
  9483. if(sd == NULL || dstsd == NULL ||
  9484. sd->status.partner_id > 0 || dstsd->status.partner_id > 0 ||
  9485. (sd->class_&JOBL_BABY) || (dstsd->class_&JOBL_BABY))
  9486. return false;
  9487. sd->status.partner_id = dstsd->status.char_id;
  9488. dstsd->status.partner_id = sd->status.char_id;
  9489. achievement_update_objective(sd, AG_MARRY, 1, 1);
  9490. achievement_update_objective(dstsd, AG_MARRY, 1, 1);
  9491. return true;
  9492. }
  9493. /*==========================================
  9494. * Divorce sd from its partner
  9495. * Return:
  9496. * false = fail
  9497. * true = success
  9498. *------------------------------------------*/
  9499. bool pc_divorce(struct map_session_data *sd)
  9500. {
  9501. struct map_session_data *p_sd;
  9502. int i;
  9503. if( sd == NULL || !pc_ismarried(sd) )
  9504. return false;
  9505. if( !sd->status.partner_id )
  9506. return false; // Char is not married
  9507. if( (p_sd = map_charid2sd(sd->status.partner_id)) == NULL )
  9508. { // Lets char server do the divorce
  9509. if( chrif_divorce(sd->status.char_id, sd->status.partner_id) )
  9510. return false; // No char server connected
  9511. return true;
  9512. }
  9513. // Both players online, lets do the divorce manually
  9514. sd->status.partner_id = 0;
  9515. p_sd->status.partner_id = 0;
  9516. for( i = 0; i < MAX_INVENTORY; i++ )
  9517. {
  9518. if( sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_M || sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_F )
  9519. pc_delitem(sd, i, 1, 0, 0, LOG_TYPE_OTHER);
  9520. if( p_sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_M || p_sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_F )
  9521. pc_delitem(p_sd, i, 1, 0, 0, LOG_TYPE_OTHER);
  9522. }
  9523. clif_divorced(sd, p_sd->status.name);
  9524. clif_divorced(p_sd, sd->status.name);
  9525. return true;
  9526. }
  9527. /**
  9528. * Get the partner map_session_data of a player
  9529. * @param sd : the husband|wife session
  9530. * @return partner session or NULL
  9531. */
  9532. struct map_session_data *pc_get_partner(struct map_session_data *sd){
  9533. if (!sd || !pc_ismarried(sd))
  9534. return NULL;
  9535. return map_charid2sd(sd->status.partner_id);
  9536. }
  9537. /**
  9538. * Get the father map_session_data of a player
  9539. * @param sd : the baby session
  9540. * @return father session or NULL
  9541. */
  9542. struct map_session_data *pc_get_father (struct map_session_data *sd){
  9543. if (!sd || !(sd->class_&JOBL_BABY) || !sd->status.father)
  9544. return NULL;
  9545. return map_charid2sd(sd->status.father);
  9546. }
  9547. /**
  9548. * Get the mother map_session_data of a player
  9549. * @param sd : the baby session
  9550. * @return mother session or NULL
  9551. */
  9552. struct map_session_data *pc_get_mother (struct map_session_data *sd){
  9553. if (!sd || !(sd->class_&JOBL_BABY) || !sd->status.mother)
  9554. return NULL;
  9555. return map_charid2sd(sd->status.mother);
  9556. }
  9557. /*==========================================
  9558. * Get sd children charid. (Need to be married)
  9559. *------------------------------------------*/
  9560. struct map_session_data *pc_get_child (struct map_session_data *sd)
  9561. {
  9562. if (!sd || !pc_ismarried(sd) || !sd->status.child)
  9563. // charid2sd returns NULL if not found
  9564. return NULL;
  9565. return map_charid2sd(sd->status.child);
  9566. }
  9567. /*==========================================
  9568. * Set player sd to bleed. (losing hp and/or sp each diff_tick)
  9569. *------------------------------------------*/
  9570. void pc_bleeding (struct map_session_data *sd, t_tick diff_tick)
  9571. {
  9572. int hp = 0, sp = 0;
  9573. if( pc_isdead(sd) )
  9574. return;
  9575. if (sd->hp_loss.value) {
  9576. sd->hp_loss.tick += diff_tick;
  9577. while (sd->hp_loss.tick >= sd->hp_loss.rate) {
  9578. hp += sd->hp_loss.value;
  9579. sd->hp_loss.tick -= sd->hp_loss.rate;
  9580. }
  9581. if(hp >= sd->battle_status.hp)
  9582. hp = sd->battle_status.hp-1; //Script drains cannot kill you.
  9583. }
  9584. if (sd->sp_loss.value) {
  9585. sd->sp_loss.tick += diff_tick;
  9586. while (sd->sp_loss.tick >= sd->sp_loss.rate) {
  9587. sp += sd->sp_loss.value;
  9588. sd->sp_loss.tick -= sd->sp_loss.rate;
  9589. }
  9590. }
  9591. if (hp > 0 || sp > 0)
  9592. status_zap(&sd->bl, hp, sp);
  9593. }
  9594. //Character regen. Flag is used to know which types of regen can take place.
  9595. //&1: HP regen
  9596. //&2: SP regen
  9597. void pc_regen (struct map_session_data *sd, t_tick diff_tick)
  9598. {
  9599. int hp = 0, sp = 0;
  9600. if (sd->hp_regen.value) {
  9601. sd->hp_regen.tick += diff_tick;
  9602. while (sd->hp_regen.tick >= sd->hp_regen.rate) {
  9603. hp += sd->hp_regen.value;
  9604. sd->hp_regen.tick -= sd->hp_regen.rate;
  9605. }
  9606. }
  9607. if (sd->sp_regen.value) {
  9608. sd->sp_regen.tick += diff_tick;
  9609. while (sd->sp_regen.tick >= sd->sp_regen.rate) {
  9610. sp += sd->sp_regen.value;
  9611. sd->sp_regen.tick -= sd->sp_regen.rate;
  9612. }
  9613. }
  9614. if (sd->percent_hp_regen.value) {
  9615. sd->percent_hp_regen.tick += diff_tick;
  9616. while (sd->percent_hp_regen.tick >= sd->percent_hp_regen.rate) {
  9617. hp += (sd->percent_hp_regen.value * sd->status.max_hp);
  9618. sd->percent_hp_regen.tick -= sd->percent_hp_regen.rate;
  9619. }
  9620. }
  9621. if (sd->percent_sp_regen.value) {
  9622. sd->percent_sp_regen.tick += diff_tick;
  9623. while (sd->percent_sp_regen.tick >= sd->percent_sp_regen.rate) {
  9624. sp += (sd->percent_sp_regen.value * sd->status.max_sp);
  9625. sd->percent_sp_regen.tick -= sd->percent_sp_regen.rate;
  9626. }
  9627. }
  9628. if (hp > 0 || sp > 0)
  9629. status_heal(&sd->bl, hp, sp, 0);
  9630. }
  9631. /*==========================================
  9632. * Memo player sd savepoint. (map,x,y)
  9633. *------------------------------------------*/
  9634. void pc_setsavepoint(struct map_session_data *sd, short mapindex,int x,int y)
  9635. {
  9636. nullpo_retv(sd);
  9637. sd->status.save_point.map = mapindex;
  9638. sd->status.save_point.x = x;
  9639. sd->status.save_point.y = y;
  9640. }
  9641. /*==========================================
  9642. * Save 1 player data at autosave interval
  9643. *------------------------------------------*/
  9644. static TIMER_FUNC(pc_autosave){
  9645. int interval;
  9646. struct s_mapiterator* iter;
  9647. struct map_session_data* sd;
  9648. static int last_save_id = 0, save_flag = 0;
  9649. if(save_flag == 2) //Someone was saved on last call, normal cycle
  9650. save_flag = 0;
  9651. else
  9652. save_flag = 1; //Noone was saved, so save first found char.
  9653. iter = mapit_getallusers();
  9654. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  9655. {
  9656. if (!sd->state.pc_loaded) // Player data hasn't fully loaded
  9657. continue;
  9658. if(sd->bl.id == last_save_id && save_flag != 1) {
  9659. save_flag = 1;
  9660. continue;
  9661. }
  9662. if(save_flag != 1) //Not our turn to save yet.
  9663. continue;
  9664. //Save char.
  9665. last_save_id = sd->bl.id;
  9666. save_flag = 2;
  9667. if (pc_isvip(sd)) // Check if we're still VIP
  9668. chrif_req_login_operation(1, sd->status.name, CHRIF_OP_LOGIN_VIP, 0, 1, 0);
  9669. chrif_save(sd, CSAVE_INVENTORY|CSAVE_CART);
  9670. break;
  9671. }
  9672. mapit_free(iter);
  9673. interval = autosave_interval/(map_usercount()+1);
  9674. if(interval < minsave_interval)
  9675. interval = minsave_interval;
  9676. add_timer(gettick()+interval,pc_autosave,0,0);
  9677. return 0;
  9678. }
  9679. static int pc_daynight_timer_sub(struct map_session_data *sd,va_list ap)
  9680. {
  9681. if (sd->state.night != night_flag && map_getmapflag(sd->bl.m, MF_NIGHTENABLED))
  9682. { //Night/day state does not match.
  9683. clif_status_load(&sd->bl, EFST_SKE, night_flag); //New night effect by dynamix [Skotlex]
  9684. sd->state.night = night_flag;
  9685. return 1;
  9686. }
  9687. return 0;
  9688. }
  9689. /*================================================
  9690. * timer to do the day [Yor]
  9691. * data: 0 = called by timer, 1 = gmcommand/script
  9692. *------------------------------------------------*/
  9693. TIMER_FUNC(map_day_timer){
  9694. char tmp_soutput[1024];
  9695. if (data == 0 && battle_config.day_duration <= 0) // if we want a day
  9696. return 0;
  9697. if (!night_flag)
  9698. return 0; //Already day.
  9699. night_flag = 0; // 0=day, 1=night [Yor]
  9700. map_foreachpc(pc_daynight_timer_sub);
  9701. strcpy(tmp_soutput, (data == 0) ? msg_txt(NULL,502) : msg_txt(NULL,60)); // The day has arrived!
  9702. intif_broadcast(tmp_soutput, strlen(tmp_soutput) + 1, BC_DEFAULT);
  9703. return 0;
  9704. }
  9705. /*================================================
  9706. * timer to do the night [Yor]
  9707. * data: 0 = called by timer, 1 = gmcommand/script
  9708. *------------------------------------------------*/
  9709. TIMER_FUNC(map_night_timer){
  9710. char tmp_soutput[1024];
  9711. if (data == 0 && battle_config.night_duration <= 0) // if we want a night
  9712. return 0;
  9713. if (night_flag)
  9714. return 0; //Already nigth.
  9715. night_flag = 1; // 0=day, 1=night [Yor]
  9716. map_foreachpc(pc_daynight_timer_sub);
  9717. strcpy(tmp_soutput, (data == 0) ? msg_txt(NULL,503) : msg_txt(NULL,59)); // The night has fallen...
  9718. intif_broadcast(tmp_soutput, strlen(tmp_soutput) + 1, BC_DEFAULT);
  9719. return 0;
  9720. }
  9721. /**
  9722. * Attempt to stand up a player
  9723. * @param sd
  9724. * @param force Ignore the check, ask player to stand up. Used in some cases like pc_damage(), pc_revive(), etc
  9725. * @return True if success, Fals if failed
  9726. */
  9727. bool pc_setstand(struct map_session_data *sd, bool force){
  9728. nullpo_ret(sd);
  9729. // Cannot stand yet
  9730. // TODO: Move to SCS_NOSTAND [Cydh]
  9731. if (!force && (sd->sc.data[SC_SITDOWN_FORCE] || sd->sc.data[SC_BANANA_BOMB_SITDOWN]))
  9732. return false;
  9733. status_change_end(&sd->bl, SC_TENSIONRELAX, INVALID_TIMER);
  9734. clif_status_load(&sd->bl,EFST_SIT,0);
  9735. clif_standing(&sd->bl); //Inform area PC is standing
  9736. //Reset sitting tick.
  9737. sd->ssregen.tick.hp = sd->ssregen.tick.sp = 0;
  9738. if( pc_isdead( sd ) ){
  9739. sd->state.dead_sit = sd->vd.dead_sit = 0;
  9740. clif_party_dead( sd );
  9741. }else{
  9742. sd->state.dead_sit = sd->vd.dead_sit = 0;
  9743. }
  9744. return true;
  9745. }
  9746. /**
  9747. * Calculate Overheat value
  9748. * @param sd: Player data
  9749. * @param heat: Amount of Heat to adjust
  9750. **/
  9751. void pc_overheat(struct map_session_data *sd, int16 heat) {
  9752. struct status_change_entry *sce = NULL;
  9753. int16 limit[] = { 150, 200, 280, 360, 450 };
  9754. uint16 skill_lv;
  9755. nullpo_retv(sd);
  9756. skill_lv = cap_value(pc_checkskill(sd, NC_MAINFRAME), 0, 4);
  9757. if ((sce = sd->sc.data[SC_OVERHEAT_LIMITPOINT])) {
  9758. sce->val1 += heat;
  9759. sce->val1 = cap_value(sce->val1, 0, 1000);
  9760. if (sd->sc.data[SC_OVERHEAT])
  9761. status_change_end(&sd->bl, SC_OVERHEAT, INVALID_TIMER);
  9762. if (sce->val1 > limit[skill_lv])
  9763. sc_start(&sd->bl, &sd->bl, SC_OVERHEAT, 100, sce->val1, 1000);
  9764. } else if (heat > 0)
  9765. sc_start(&sd->bl, &sd->bl, SC_OVERHEAT_LIMITPOINT, 100, heat, 1000);
  9766. }
  9767. /**
  9768. * Check if player is autolooting given itemID.
  9769. */
  9770. bool pc_isautolooting(struct map_session_data *sd, unsigned short nameid)
  9771. {
  9772. uint8 i = 0;
  9773. if (sd->state.autoloottype && sd->state.autoloottype&(1<<itemdb_type(nameid)))
  9774. return true;
  9775. if (!sd->state.autolooting)
  9776. return false;
  9777. if (sd->state.autolooting)
  9778. ARR_FIND(0, AUTOLOOTITEM_SIZE, i, sd->state.autolootid[i] == nameid);
  9779. return (i != AUTOLOOTITEM_SIZE);
  9780. }
  9781. /**
  9782. * Checks if player can use @/#command
  9783. * @param sd Player map session data
  9784. * @param command Command name without @/# and params
  9785. * @param type is it atcommand or charcommand
  9786. */
  9787. bool pc_can_use_command(struct map_session_data *sd, const char *command, AtCommandType type)
  9788. {
  9789. return pc_group_can_use_command(pc_get_group_id(sd), command, type);
  9790. }
  9791. /**
  9792. * Checks if commands used by a player should be logged
  9793. * according to their group setting.
  9794. * @param sd Player map session data
  9795. */
  9796. bool pc_should_log_commands(struct map_session_data *sd)
  9797. {
  9798. return pc_group_should_log_commands(pc_get_group_id(sd));
  9799. }
  9800. /**
  9801. * Spirit Charm expiration timer.
  9802. * @see TimerFunc
  9803. */
  9804. static TIMER_FUNC(pc_spiritcharm_timer){
  9805. struct map_session_data *sd;
  9806. int i;
  9807. if ((sd = (struct map_session_data *)map_id2sd(id)) == NULL || sd->bl.type != BL_PC)
  9808. return 1;
  9809. if (sd->spiritcharm <= 0) {
  9810. 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);
  9811. sd->spiritcharm = 0;
  9812. sd->spiritcharm_type = CHARM_TYPE_NONE;
  9813. return 0;
  9814. }
  9815. ARR_FIND(0, sd->spiritcharm, i, sd->spiritcharm_timer[i] == tid);
  9816. if (i == sd->spiritcharm) {
  9817. ShowError("pc_spiritcharm_timer: timer not found (aid=%d cid=%d tid=%d)\n", sd->status.account_id, sd->status.char_id, tid);
  9818. return 0;
  9819. }
  9820. sd->spiritcharm--;
  9821. if (i != sd->spiritcharm)
  9822. memmove(sd->spiritcharm_timer + i, sd->spiritcharm_timer + i + 1, (sd->spiritcharm - i) * sizeof(int));
  9823. sd->spiritcharm_timer[sd->spiritcharm] = INVALID_TIMER;
  9824. if (sd->spiritcharm <= 0)
  9825. sd->spiritcharm_type = CHARM_TYPE_NONE;
  9826. clif_spiritcharm(sd);
  9827. return 0;
  9828. }
  9829. /**
  9830. * Adds a spirit charm.
  9831. * @param sd: Target character
  9832. * @param interval: Duration
  9833. * @param max: Maximum amount of charms to add
  9834. * @param type: Charm type (@see spirit_charm_types)
  9835. */
  9836. void pc_addspiritcharm(struct map_session_data *sd, int interval, int max, int type)
  9837. {
  9838. int tid, i;
  9839. nullpo_retv(sd);
  9840. if (sd->spiritcharm_type != CHARM_TYPE_NONE && type != sd->spiritcharm_type)
  9841. pc_delspiritcharm(sd, sd->spiritcharm, sd->spiritcharm_type);
  9842. if (max > MAX_SPIRITCHARM)
  9843. max = MAX_SPIRITCHARM;
  9844. if (sd->spiritcharm < 0)
  9845. sd->spiritcharm = 0;
  9846. if (sd->spiritcharm && sd->spiritcharm >= max) {
  9847. if (sd->spiritcharm_timer[0] != INVALID_TIMER)
  9848. delete_timer(sd->spiritcharm_timer[0], pc_spiritcharm_timer);
  9849. sd->spiritcharm--;
  9850. if (sd->spiritcharm != 0)
  9851. memmove(sd->spiritcharm_timer + 0, sd->spiritcharm_timer + 1, (sd->spiritcharm) * sizeof(int));
  9852. sd->spiritcharm_timer[sd->spiritcharm] = INVALID_TIMER;
  9853. }
  9854. tid = add_timer(gettick() + interval, pc_spiritcharm_timer, sd->bl.id, 0);
  9855. 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);
  9856. if (i != sd->spiritcharm)
  9857. memmove(sd->spiritcharm_timer + i + 1, sd->spiritcharm_timer + i, (sd->spiritcharm - i) * sizeof(int));
  9858. sd->spiritcharm_timer[i] = tid;
  9859. sd->spiritcharm++;
  9860. sd->spiritcharm_type = type;
  9861. clif_spiritcharm(sd);
  9862. }
  9863. /**
  9864. * Removes one or more spirit charms.
  9865. * @param sd: The target character
  9866. * @param count: Amount of charms to remove
  9867. * @param type: Type of charm to remove
  9868. */
  9869. void pc_delspiritcharm(struct map_session_data *sd, int count, int type)
  9870. {
  9871. int i;
  9872. nullpo_retv(sd);
  9873. if (sd->spiritcharm_type != type)
  9874. return;
  9875. if (sd->spiritcharm <= 0) {
  9876. sd->spiritcharm = 0;
  9877. return;
  9878. }
  9879. if (count <= 0)
  9880. return;
  9881. if (count > sd->spiritcharm)
  9882. count = sd->spiritcharm;
  9883. sd->spiritcharm -= count;
  9884. if (count > MAX_SPIRITCHARM)
  9885. count = MAX_SPIRITCHARM;
  9886. for (i = 0; i < count; i++) {
  9887. if (sd->spiritcharm_timer[i] != INVALID_TIMER) {
  9888. delete_timer(sd->spiritcharm_timer[i], pc_spiritcharm_timer);
  9889. sd->spiritcharm_timer[i] = INVALID_TIMER;
  9890. }
  9891. }
  9892. for (i = count; i < MAX_SPIRITCHARM; i++) {
  9893. sd->spiritcharm_timer[i - count] = sd->spiritcharm_timer[i];
  9894. sd->spiritcharm_timer[i] = INVALID_TIMER;
  9895. }
  9896. if (sd->spiritcharm <= 0)
  9897. sd->spiritcharm_type = CHARM_TYPE_NONE;
  9898. clif_spiritcharm(sd);
  9899. }
  9900. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  9901. /**
  9902. * Renewal EXP/Item Drop rate modifier based on level penalty
  9903. * @param level_diff: Monster and Player level difference
  9904. * @param mob_class: Monster class
  9905. * @param mode: Monster mode
  9906. * @param type: 1 - EXP, 2 - Item Drop
  9907. * @return Penalty rate
  9908. */
  9909. int pc_level_penalty_mod(int level_diff, uint32 mob_class, enum e_mode mode, int type)
  9910. {
  9911. int rate = 100;
  9912. if (type == 2 && (mode&MD_FIXED_ITEMDROP))
  9913. return rate;
  9914. if (level_diff < 0)
  9915. level_diff = MAX_LEVEL + (~level_diff + 1);
  9916. if ((rate = level_penalty[type][mob_class][level_diff]) > 0) // Monster class found, return rate
  9917. return rate;
  9918. return 100; // Penalty not found, return default
  9919. }
  9920. #endif
  9921. int pc_split_str(char *str,char **val,int num)
  9922. {
  9923. int i;
  9924. for (i=0; i<num && str; i++){
  9925. val[i] = str;
  9926. str = strchr(str,',');
  9927. if (str && i<num-1) //Do not remove a trailing comma.
  9928. *str++=0;
  9929. }
  9930. return i;
  9931. }
  9932. int pc_split_atoi(char* str, int* val, char sep, int max)
  9933. {
  9934. int i,j;
  9935. for (i=0; i<max; i++) {
  9936. if (!str) break;
  9937. val[i] = atoi(str);
  9938. str = strchr(str,sep);
  9939. if (str)
  9940. *str++=0;
  9941. }
  9942. //Zero up the remaining.
  9943. for(j=i; j < max; j++)
  9944. val[j] = 0;
  9945. return i;
  9946. }
  9947. int pc_split_atoui(char* str, unsigned int* val, char sep, int max)
  9948. {
  9949. static int warning=0;
  9950. int i,j;
  9951. for (i=0; i<max; i++) {
  9952. double f;
  9953. if (!str) break;
  9954. f = atof(str);
  9955. if (f < 0)
  9956. val[i] = 0;
  9957. else if (f > UINT_MAX) {
  9958. val[i] = UINT_MAX;
  9959. if (!warning) {
  9960. warning = 1;
  9961. ShowWarning("pc_readdb (exp.txt): Required exp per level is capped to %u\n", UINT_MAX);
  9962. }
  9963. } else
  9964. val[i] = (unsigned int)f;
  9965. str = strchr(str,sep);
  9966. if (str)
  9967. *str++=0;
  9968. }
  9969. //Zero up the remaining.
  9970. for(j=i; j < max; j++)
  9971. val[j] = 0;
  9972. return i;
  9973. }
  9974. /*==========================================
  9975. * sub DB reading.
  9976. * Function used to read skill_tree.txt
  9977. *------------------------------------------*/
  9978. static bool pc_readdb_skilltree(char* fields[], int columns, int current)
  9979. {
  9980. uint32 baselv, joblv, baselv_max, joblv_max;
  9981. uint16 skill_id, skill_lv, skill_lv_max;
  9982. int idx, class_;
  9983. unsigned int i, offset, skill_idx;
  9984. class_ = atoi(fields[0]);
  9985. skill_id = (uint16)atoi(fields[1]);
  9986. skill_lv = (uint16)atoi(fields[2]);
  9987. if (columns == 5 + MAX_PC_SKILL_REQUIRE * 2) { // Base/Job level requirement extra columns
  9988. baselv = (uint32)atoi(fields[3]);
  9989. joblv = (uint32)atoi(fields[4]);
  9990. offset = 5;
  9991. }
  9992. else if (columns == 3 + MAX_PC_SKILL_REQUIRE * 2) {
  9993. baselv = joblv = 0;
  9994. offset = 3;
  9995. }
  9996. else {
  9997. ShowWarning("pc_readdb_skilltree: Invalid number of colums in skill %hu of job %d's tree.\n", skill_id, class_);
  9998. return false;
  9999. }
  10000. if(!pcdb_checkid(class_))
  10001. {
  10002. ShowWarning("pc_readdb_skilltree: Invalid job class %d specified.\n", class_);
  10003. return false;
  10004. }
  10005. idx = pc_class2idx(class_);
  10006. if (!skill_get_index(skill_id)) {
  10007. ShowWarning("pc_readdb_skilltree: Unable to load skill %hu into job %d's tree.", skill_id, class_);
  10008. return false;
  10009. }
  10010. if (skill_lv > (skill_lv_max = skill_get_max(skill_id))) {
  10011. 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);
  10012. skill_lv = skill_lv_max;
  10013. }
  10014. if (baselv > (baselv_max = pc_class_maxbaselv(class_))) {
  10015. 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);
  10016. baselv = baselv_max;
  10017. }
  10018. if (joblv > (joblv_max = pc_class_maxjoblv(class_))) {
  10019. 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);
  10020. joblv = joblv_max;
  10021. }
  10022. //This is to avoid adding two lines for the same skill. [Skotlex]
  10023. 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 );
  10024. if( skill_idx == MAX_SKILL_TREE )
  10025. {
  10026. 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_);
  10027. return false;
  10028. }
  10029. else if(skill_tree[idx][skill_idx].skill_id)
  10030. {
  10031. ShowNotice("pc_readdb_skilltree: Overwriting skill %hu for job %d.\n", skill_id, class_);
  10032. }
  10033. skill_tree[idx][skill_idx].skill_id = skill_id;
  10034. skill_tree[idx][skill_idx].skill_lv = skill_lv;
  10035. skill_tree[idx][skill_idx].baselv = baselv;
  10036. skill_tree[idx][skill_idx].joblv = joblv;
  10037. for(i = 0; i < MAX_PC_SKILL_REQUIRE; i++)
  10038. {
  10039. skill_id = (uint16)atoi(fields[i * 2 + offset]);
  10040. skill_lv = (uint16)atoi(fields[i * 2 + offset + 1]);
  10041. if (skill_id == 0) {
  10042. if (skill_tree[idx][skill_idx].need[i].skill_id > 0) { // Remove pre-requisite
  10043. skill_tree[idx][skill_idx].need[i].skill_id = 0;
  10044. skill_tree[idx][skill_idx].need[i].skill_lv = 0;
  10045. }
  10046. continue;
  10047. }
  10048. if (skill_id > MAX_SKILL_ID || !skill_get_index(skill_id)) {
  10049. ShowWarning("pc_readdb_skilltree: Unable to load requirement skill %hu into job %d's tree.", skill_id, class_);
  10050. return false;
  10051. }
  10052. if (skill_lv > (skill_lv_max = skill_get_max(skill_id))) {
  10053. 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);
  10054. skill_lv = skill_lv_max;
  10055. }
  10056. skill_tree[idx][skill_idx].need[i].skill_id = skill_id;
  10057. skill_tree[idx][skill_idx].need[i].skill_lv = skill_lv;
  10058. }
  10059. return true;
  10060. }
  10061. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  10062. static bool pc_readdb_levelpenalty(char* fields[], int columns, int current)
  10063. {
  10064. int type, class_, diff;
  10065. type = atoi(fields[0]); //1=experience, 2=item drop
  10066. class_ = atoi(fields[1]);
  10067. diff = atoi(fields[2]);
  10068. if( type != 1 && type != 2 ){
  10069. ShowWarning("pc_readdb_levelpenalty: Invalid type %d specified.\n", type);
  10070. return false;
  10071. }
  10072. if( !CHK_CLASS(class_) ){
  10073. ShowWarning("pc_readdb_levelpenalty: Invalid class %d specified.\n", class_);
  10074. return false;
  10075. }
  10076. diff = min(diff, MAX_LEVEL);
  10077. if( diff < 0 )
  10078. diff = min(MAX_LEVEL + ( ~(diff) + 1 ), MAX_LEVEL*2);
  10079. level_penalty[type][class_][diff] = atoi(fields[3]);
  10080. return true;
  10081. }
  10082. #endif
  10083. /** [Cydh]
  10084. * Calculates base hp of player. Reference: http://irowiki.org/wiki/Max_HP
  10085. * @param level Base level of player
  10086. * @param class_ Job ID @see enum e_job
  10087. * @return base_hp
  10088. */
  10089. static unsigned int pc_calc_basehp(uint16 level, uint16 class_) {
  10090. double base_hp;
  10091. uint16 i, idx = pc_class2idx(class_);
  10092. base_hp = 35 + level * (job_info[idx].hp_multiplicator/100.);
  10093. #ifndef RENEWAL
  10094. if(level >= 10 && (class_ == JOB_NINJA || class_ == JOB_GUNSLINGER)) base_hp += 90;
  10095. #endif
  10096. for (i = 2; i <= level; i++)
  10097. base_hp += floor(((job_info[idx].hp_factor/100.) * i) + 0.5); //Don't have round()
  10098. if (class_ == JOB_SUMMONER)
  10099. base_hp += floor((base_hp / 2) + 0.5);
  10100. return (unsigned int)base_hp;
  10101. }
  10102. /** [Playtester]
  10103. * Calculates base sp of player.
  10104. * @param level Base level of player
  10105. * @param class_ Job ID @see enum e_job
  10106. * @return base_sp
  10107. */
  10108. static unsigned int pc_calc_basesp(uint16 level, uint16 class_) {
  10109. double base_sp;
  10110. uint16 idx = pc_class2idx(class_);
  10111. base_sp = 10 + floor(level * (job_info[idx].sp_factor / 100.));
  10112. switch (class_) {
  10113. case JOB_NINJA:
  10114. if (level >= 10)
  10115. base_sp -= 22;
  10116. else
  10117. base_sp = 11 + 3*level;
  10118. break;
  10119. case JOB_GUNSLINGER:
  10120. if (level > 10)
  10121. base_sp -= 18;
  10122. else
  10123. base_sp = 9 + 3*level;
  10124. break;
  10125. case JOB_SUMMONER:
  10126. base_sp -= floor(base_sp / 2);
  10127. break;
  10128. }
  10129. return (unsigned int)base_sp;
  10130. }
  10131. //Reading job_db1.txt line, (class,weight,HPFactor,HPMultiplicator,SPFactor,aspd/lvl...)
  10132. static bool pc_readdb_job1(char* fields[], int columns, int current){
  10133. int idx, class_;
  10134. unsigned int i;
  10135. class_ = atoi(fields[0]);
  10136. if (!pcdb_checkid(class_)) {
  10137. ShowWarning("status_readdb_job1: Invalid job class %d specified.\n", class_);
  10138. return false;
  10139. }
  10140. idx = pc_class2idx(class_);
  10141. job_info[idx].max_weight_base = atoi(fields[1]);
  10142. job_info[idx].hp_factor = atoi(fields[2]);
  10143. job_info[idx].hp_multiplicator = atoi(fields[3]);
  10144. job_info[idx].sp_factor = atoi(fields[4]);
  10145. #ifdef RENEWAL_ASPD
  10146. for(i = 0; i <= MAX_WEAPON_TYPE; i++)
  10147. #else
  10148. for(i = 0; i < MAX_WEAPON_TYPE; i++)
  10149. #endif
  10150. {
  10151. job_info[idx].aspd_base[i] = atoi(fields[i+5]);
  10152. }
  10153. return true;
  10154. }
  10155. //Reading job_db2.txt line (class,JobLv1,JobLv2,JobLv3,...)
  10156. static bool pc_readdb_job2(char* fields[], int columns, int current)
  10157. {
  10158. int idx, class_, i;
  10159. class_ = atoi(fields[0]);
  10160. if(!pcdb_checkid(class_))
  10161. {
  10162. ShowWarning("status_readdb_job2: Invalid job class %d specified.\n", class_);
  10163. return false;
  10164. }
  10165. idx = pc_class2idx(class_);
  10166. for(i = 1; i < columns; i++)
  10167. {
  10168. job_info[idx].job_bonus[i-1] = atoi(fields[i]);
  10169. }
  10170. return true;
  10171. }
  10172. //Reading job_exp.txt line
  10173. //Max Level,Class list,Type (0 - Base Exp; 1 - Job Exp),Exp/lvl...
  10174. static bool pc_readdb_job_exp(char* fields[], int columns, int current)
  10175. {
  10176. int idx, i, type;
  10177. int job_id,job_count,jobs[CLASS_COUNT];
  10178. unsigned int ui, maxlvl;
  10179. maxlvl = atoi(fields[0]);
  10180. if(maxlvl > MAX_LEVEL || maxlvl<1){
  10181. ShowError("pc_readdb_job_exp: Invalid maxlevel %d specified.\n", maxlvl);
  10182. return false;
  10183. }
  10184. if((maxlvl+3) > columns){ //nb values = (maxlvl-startlvl)+1-index1stvalue
  10185. ShowError("pc_readdb_job_exp: Number of columns %d defined is too low for max level %d.\n",columns,maxlvl);
  10186. return false;
  10187. }
  10188. type = atoi(fields[2]);
  10189. if(type < 0 || type > 1){
  10190. ShowError("pc_readdb_job_exp: Invalid type %d specified.\n", type);
  10191. return false;
  10192. }
  10193. job_count = pc_split_atoi(fields[1],jobs,':',CLASS_COUNT);
  10194. if (job_count < 1)
  10195. return false;
  10196. job_id = jobs[0];
  10197. if(!pcdb_checkid(job_id)){
  10198. ShowError("pc_readdb_job_exp: Invalid job class %d specified.\n", job_id);
  10199. return false;
  10200. }
  10201. idx = pc_class2idx(job_id);
  10202. job_info[idx].max_level[type] = maxlvl;
  10203. for(i=0; i<maxlvl; i++)
  10204. job_info[idx].exp_table[type][i] = ((uint32) atoi(fields[3+i]));
  10205. //Reverse check in case the array has a bunch of trailing zeros... [Skotlex]
  10206. //The reasoning behind the -2 is this... if the max level is 5, then the array
  10207. //should look like this:
  10208. //0: x, 1: x, 2: x: 3: x 4: 0 <- last valid value is at 3.
  10209. while ((ui = job_info[idx].max_level[type]) >= 2 && job_info[idx].exp_table[type][ui-2] <= 0)
  10210. job_info[idx].max_level[type]--;
  10211. if (job_info[idx].max_level[type] < maxlvl) {
  10212. 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]);
  10213. ShowInfo("Filling the missing values with the last exp entry.\n");
  10214. //Fill the requested values with the last entry.
  10215. ui = (job_info[idx].max_level[type] <= 2? 0: job_info[idx].max_level[type]-2);
  10216. for (; ui+2 < maxlvl; ui++)
  10217. job_info[idx].exp_table[type][ui] = job_info[idx].exp_table[type][ui-1];
  10218. job_info[idx].max_level[type] = maxlvl;
  10219. }
  10220. // ShowInfo("%s - Class %d: %d\n", type?"Job":"Base", job_id, job_info[idx].max_level[type]);
  10221. for (i = 1; i < job_count; i++) {
  10222. job_id = jobs[i];
  10223. if (!pcdb_checkid(job_id)) {
  10224. ShowError("pc_readdb_job_exp: Invalid job ID %d.\n", job_id);
  10225. continue;
  10226. }
  10227. idx = pc_class2idx(job_id);
  10228. 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]));
  10229. job_info[idx].max_level[type] = maxlvl;
  10230. // ShowInfo("%s - Class %d: %u\n", type?"Job":"Base", job_id, job_info[idx].max_level[type]);
  10231. }
  10232. return true;
  10233. }
  10234. /**
  10235. * #ifdef HP_SP_TABLES, reads 'job_basehpsp_db.txt to replace hp/sp results from formula
  10236. * startlvl,endlvl,class,type,values...
  10237. */
  10238. #ifdef HP_SP_TABLES
  10239. static bool pc_readdb_job_basehpsp(char* fields[], int columns, int current)
  10240. {
  10241. int i, startlvl, endlvl;
  10242. int job_count,jobs[CLASS_COUNT];
  10243. short type;
  10244. startlvl = atoi(fields[0]);
  10245. if(startlvl<1){
  10246. ShowError("pc_readdb_job_basehpsp: Invalid start level %d specified.\n", startlvl);
  10247. return false;
  10248. }
  10249. endlvl = atoi(fields[1]);
  10250. if(endlvl<1 || endlvl<startlvl){
  10251. ShowError("pc_readdb_job_basehpsp: Invalid end level %d specified.\n", endlvl);
  10252. return false;
  10253. }
  10254. if((endlvl-startlvl+1+4) > columns){ //nb values = (maxlvl-startlvl)+1-index1stvalue
  10255. 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);
  10256. return false;
  10257. }
  10258. type = atoi(fields[3]);
  10259. if(type < 0 || type > 1){
  10260. ShowError("pc_readdb_job_basehpsp: Invalid type %d specified.\n", type);
  10261. return false;
  10262. }
  10263. job_count = pc_split_atoi(fields[2],jobs,':',CLASS_COUNT);
  10264. if (job_count < 1)
  10265. return false;
  10266. for (i = 0; i < job_count; i++) {
  10267. int idx, job_id = jobs[i], use_endlvl;
  10268. if (!pcdb_checkid(job_id)) {
  10269. ShowError("pc_readdb_job_basehpsp: Invalid job class %d specified.\n", job_id);
  10270. return false;
  10271. }
  10272. idx = pc_class2idx(job_id);
  10273. if (startlvl > job_info[idx].max_level[0]) {
  10274. ShowError("pc_readdb_job_basehpsp: Invalid start level %d specified.\n", startlvl);
  10275. return false;
  10276. }
  10277. //Just read until available max level for this job, don't use MAX_LEVEL!
  10278. use_endlvl = endlvl;
  10279. if (use_endlvl > job_info[idx].max_level[0])
  10280. use_endlvl = job_info[idx].max_level[0];
  10281. if(type == 0) { //hp type
  10282. uint16 j;
  10283. for(j = 0; j < use_endlvl; j++) {
  10284. if (atoi(fields[j+4])) {
  10285. uint16 lvl_idx = startlvl-1+j;
  10286. job_info[idx].base_hp[lvl_idx] = atoi(fields[j+4]);
  10287. //Tells if this HP is lower than previous level (but not for 99->100)
  10288. if (lvl_idx-1 >= 0 && lvl_idx != 99 && job_info[idx].base_hp[lvl_idx] < job_info[idx].base_hp[lvl_idx-1])
  10289. 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",
  10290. current,j+4,job_id,lvl_idx+1,job_info[idx].base_hp[lvl_idx-1],job_info[idx].base_hp[lvl_idx]);
  10291. }
  10292. }
  10293. }
  10294. else { //sp type
  10295. uint16 j;
  10296. for(j = 0; j < use_endlvl; j++) {
  10297. if (atoi(fields[j+4])) {
  10298. uint16 lvl_idx = startlvl-1+j;
  10299. job_info[idx].base_sp[lvl_idx] = atoi(fields[j+4]);
  10300. //Tells if this SP is lower than previous level (but not for 99->100)
  10301. if (lvl_idx-1 >= 0 && lvl_idx != 99 && job_info[idx].base_sp[lvl_idx] < job_info[idx].base_sp[lvl_idx-1])
  10302. 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",
  10303. current,j+4,job_id,lvl_idx+1,job_info[idx].base_sp[lvl_idx-1],job_info[idx].base_sp[lvl_idx]);
  10304. }
  10305. }
  10306. }
  10307. }
  10308. return true;
  10309. }
  10310. #endif
  10311. /** [Cydh]
  10312. * Reads 'job_param_db.txt' to check max. param each job and store them to job_info[].max_param.*
  10313. */
  10314. static bool pc_readdb_job_param(char* fields[], int columns, int current)
  10315. {
  10316. int idx, class_;
  10317. uint16 str, agi, vit, int_, dex, luk;
  10318. script_get_constant(trim(fields[0]),&class_);
  10319. if ((idx = pc_class2idx(class_)) < 0) {
  10320. ShowError("pc_readdb_job_param: Invalid job '%s'. Skipping!",fields[0]);
  10321. return false;
  10322. }
  10323. str = cap_value(atoi(fields[1]),10,SHRT_MAX);
  10324. agi = atoi(fields[2]) ? cap_value(atoi(fields[2]),10,SHRT_MAX) : str;
  10325. vit = atoi(fields[3]) ? cap_value(atoi(fields[3]),10,SHRT_MAX) : str;
  10326. int_ = atoi(fields[4]) ? cap_value(atoi(fields[4]),10,SHRT_MAX) : str;
  10327. dex = atoi(fields[5]) ? cap_value(atoi(fields[5]),10,SHRT_MAX) : str;
  10328. luk = atoi(fields[6]) ? cap_value(atoi(fields[6]),10,SHRT_MAX) : str;
  10329. job_info[idx].max_param.str = str;
  10330. job_info[idx].max_param.agi = agi;
  10331. job_info[idx].max_param.vit = vit;
  10332. job_info[idx].max_param.int_ = int_;
  10333. job_info[idx].max_param.dex = dex;
  10334. job_info[idx].max_param.luk = luk;
  10335. return true;
  10336. }
  10337. /**
  10338. * Read job_noenter_map.txt
  10339. **/
  10340. static bool pc_readdb_job_noenter_map(char *str[], int columns, int current) {
  10341. int idx, class_ = -1;
  10342. if (ISDIGIT(str[0][0])) {
  10343. class_ = atoi(str[0]);
  10344. } else {
  10345. if (!script_get_constant(str[0], &class_)) {
  10346. ShowError("pc_readdb_job_noenter_map: Invalid job %s specified.\n", str[0]);
  10347. return false;
  10348. }
  10349. }
  10350. if (!pcdb_checkid(class_) || (idx = pc_class2idx(class_)) < 0) {
  10351. ShowError("pc_readdb_job_noenter_map: Invalid job %d specified.\n", class_);
  10352. return false;
  10353. }
  10354. job_info[idx].noenter_map.zone = atoi(str[1]);
  10355. job_info[idx].noenter_map.group_lv = atoi(str[2]);
  10356. return true;
  10357. }
  10358. static int pc_read_statsdb(const char *basedir, int last_s, bool silent){
  10359. int i=1;
  10360. char line[24000]; //FIXME this seem too big
  10361. FILE *fp;
  10362. sprintf(line, "%s/statpoint.txt", basedir);
  10363. fp=fopen(line,"r");
  10364. if(fp == NULL){
  10365. if(silent==0) ShowWarning("Can't read '" CL_WHITE "%s" CL_RESET "'... Generating DB.\n",line);
  10366. return max(last_s,i);
  10367. } else {
  10368. int entries=0;
  10369. while(fgets(line, sizeof(line), fp))
  10370. {
  10371. int stat;
  10372. trim(line);
  10373. if(line[0] == '\0' || (line[0]=='/' && line[1]=='/'))
  10374. continue;
  10375. if ((stat=strtoul(line,NULL,10))<0)
  10376. stat=0;
  10377. if (i > MAX_LEVEL)
  10378. break;
  10379. statp[i]=stat;
  10380. i++;
  10381. entries++;
  10382. }
  10383. fclose(fp);
  10384. ShowStatus("Done reading '" CL_WHITE "%d" CL_RESET "' entries in '" CL_WHITE "%s/%s" CL_RESET "'.\n", entries, basedir,"statpoint.txt");
  10385. }
  10386. return max(last_s,i);
  10387. }
  10388. /*==========================================
  10389. * pc DB reading.
  10390. * job_exp.txt - required experience values
  10391. * skill_tree.txt - skill tree for every class
  10392. * attr_fix.txt - elemental adjustment table
  10393. * job_db1.txt - job,weight,hp_factor,hp_multiplicator,sp_factor,aspds/lvl
  10394. * job_db2.txt - job,stats bonuses/lvl
  10395. * job_maxhpsp_db.txt - strtlvl,maxlvl,job,type,values/lvl (values=hp|sp)
  10396. *------------------------------------------*/
  10397. void pc_readdb(void) {
  10398. int i, k, s = 1;
  10399. const char* dbsubpath[] = {
  10400. "",
  10401. "/" DBIMPORT,
  10402. //add other path here
  10403. };
  10404. //reset
  10405. memset(job_info,0,sizeof(job_info)); // job_info table
  10406. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  10407. sv_readdb(db_path, DBPATH "level_penalty.txt", ',', 4, 4, -1, &pc_readdb_levelpenalty, 0);
  10408. sv_readdb(db_path, DBIMPORT"/level_penalty.txt", ',', 4, 4, -1, &pc_readdb_levelpenalty, 1);
  10409. for( k=1; k < 3; k++ ){ // fill in the blanks
  10410. int j;
  10411. for( j = 0; j < CLASS_ALL; j++ ){
  10412. int tmp = 0;
  10413. for( i = 0; i < MAX_LEVEL*2; i++ ){
  10414. if( i == MAX_LEVEL+1 )
  10415. tmp = level_penalty[k][j][0];// reset
  10416. if( level_penalty[k][j][i] > 0 )
  10417. tmp = level_penalty[k][j][i];
  10418. else
  10419. level_penalty[k][j][i] = tmp;
  10420. }
  10421. }
  10422. }
  10423. #endif
  10424. // reset then read statspoint
  10425. memset(statp,0,sizeof(statp));
  10426. for(i=0; i<ARRAYLENGTH(dbsubpath); i++){
  10427. uint8 n1 = (uint8)(strlen(db_path)+strlen(dbsubpath[i])+1);
  10428. uint8 n2 = (uint8)(strlen(db_path)+strlen(DBPATH)+strlen(dbsubpath[i])+1);
  10429. char* dbsubpath1 = (char*)aMalloc(n1+1);
  10430. char* dbsubpath2 = (char*)aMalloc(n2+1);
  10431. if(i==0) {
  10432. safesnprintf(dbsubpath1,n1,"%s%s",db_path,dbsubpath[i]);
  10433. safesnprintf(dbsubpath2,n2,"%s/%s%s",db_path,DBPATH,dbsubpath[i]);
  10434. }
  10435. else {
  10436. safesnprintf(dbsubpath1,n1,"%s%s",db_path,dbsubpath[i]);
  10437. safesnprintf(dbsubpath2,n1,"%s%s",db_path,dbsubpath[i]);
  10438. }
  10439. s = pc_read_statsdb(dbsubpath2,s,i > 0);
  10440. if (i == 0)
  10441. #ifdef RENEWAL_ASPD
  10442. sv_readdb(dbsubpath1, DBPATH "job_db1.txt",',',6+MAX_WEAPON_TYPE,6+MAX_WEAPON_TYPE,CLASS_COUNT,&pc_readdb_job1, false);
  10443. #else
  10444. sv_readdb(dbsubpath1, DBPATH "job_db1.txt",',',5+MAX_WEAPON_TYPE,5+MAX_WEAPON_TYPE,CLASS_COUNT,&pc_readdb_job1, false);
  10445. #endif
  10446. else
  10447. sv_readdb(dbsubpath1, "job_db1.txt",',',5+MAX_WEAPON_TYPE,6+MAX_WEAPON_TYPE,CLASS_COUNT,&pc_readdb_job1, true);
  10448. sv_readdb(dbsubpath1, "job_db2.txt",',',1,1+MAX_LEVEL,CLASS_COUNT,&pc_readdb_job2, i > 0);
  10449. sv_readdb(dbsubpath2, "job_exp.txt",',',4,1000+3,CLASS_COUNT*2,&pc_readdb_job_exp, i > 0); //support till 1000lvl
  10450. #ifdef HP_SP_TABLES
  10451. 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!
  10452. #endif
  10453. sv_readdb(dbsubpath2, "job_param_db.txt", ',', 2, PARAM_MAX+1, CLASS_COUNT, &pc_readdb_job_param, i > 0);
  10454. sv_readdb(dbsubpath2, "job_noenter_map.txt", ',', 3, 3, CLASS_COUNT, &pc_readdb_job_noenter_map, i > 0);
  10455. aFree(dbsubpath1);
  10456. aFree(dbsubpath2);
  10457. }
  10458. // Reset and read skilltree - needs to be read after pc_readdb_job_exp to get max base and job levels
  10459. memset(skill_tree, 0, sizeof(skill_tree));
  10460. 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);
  10461. 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);
  10462. // generate the remaining parts of the db if necessary
  10463. k = battle_config.use_statpoint_table; //save setting
  10464. battle_config.use_statpoint_table = 0; //temporarily disable to force pc_gets_status_point use default values
  10465. statp[0] = 45; // seed value
  10466. for (; s <= MAX_LEVEL; s++)
  10467. statp[s] = statp[s-1] + pc_gets_status_point(s-1);
  10468. battle_config.use_statpoint_table = k; //restore setting
  10469. //Checking if all class have their data
  10470. for (i = 0; i < JOB_MAX; i++) {
  10471. int idx;
  10472. uint16 j;
  10473. if (!pcdb_checkid(i))
  10474. continue;
  10475. if (i == JOB_WEDDING || i == JOB_XMAS || i == JOB_SUMMER || i == JOB_HANBOK || i == JOB_OKTOBERFEST || i == JOB_SUMMER2)
  10476. continue; //Classes that do not need exp tables.
  10477. idx = pc_class2idx(i);
  10478. if (!job_info[idx].max_level[0])
  10479. ShowWarning("Class %s (%d) does not have a base exp table.\n", job_name(i), i);
  10480. if (!job_info[idx].max_level[1])
  10481. ShowWarning("Class %s (%d) does not have a job exp table.\n", job_name(i), i);
  10482. //Init and checking the empty value of Base HP/SP [Cydh]
  10483. for (j = 0; j < (job_info[idx].max_level[0] ? job_info[idx].max_level[0] : MAX_LEVEL); j++) {
  10484. if (job_info[idx].base_hp[j] == 0)
  10485. job_info[idx].base_hp[j] = pc_calc_basehp(j+1,i);
  10486. if (job_info[idx].base_sp[j] == 0)
  10487. job_info[idx].base_sp[j] = pc_calc_basesp(j+1,i);
  10488. }
  10489. }
  10490. }
  10491. // Read MOTD on startup. [Valaris]
  10492. int pc_read_motd(void)
  10493. {
  10494. FILE* fp;
  10495. // clear old MOTD
  10496. memset(motd_text, 0, sizeof(motd_text));
  10497. // read current MOTD
  10498. if( ( fp = fopen(motd_txt, "r") ) != NULL )
  10499. {
  10500. unsigned int entries = 0;
  10501. char buf[CHAT_SIZE_MAX];
  10502. while( entries < MOTD_LINE_SIZE && fgets(buf, CHAT_SIZE_MAX, fp) )
  10503. {
  10504. unsigned int lines = 0;
  10505. size_t len;
  10506. lines++;
  10507. if( buf[0] == '/' && buf[1] == '/' )
  10508. continue;
  10509. len = strlen(buf);
  10510. while( len && ( buf[len-1] == '\r' || buf[len-1] == '\n' ) ) // strip trailing EOL characters
  10511. len--;
  10512. if( len ) {
  10513. char * ptr;
  10514. buf[len] = 0;
  10515. if( ( ptr = strstr(buf, " :") ) != NULL && ptr-buf >= NAME_LENGTH ) // crashes newer clients
  10516. 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);
  10517. }
  10518. else {// empty line
  10519. buf[0] = ' ';
  10520. buf[1] = 0;
  10521. }
  10522. safestrncpy(motd_text[entries], buf, CHAT_SIZE_MAX);
  10523. entries++;
  10524. }
  10525. fclose(fp);
  10526. ShowStatus("Done reading '" CL_WHITE "%u" CL_RESET "' entries in '" CL_WHITE "%s" CL_RESET "'.\n", entries, motd_txt);
  10527. }
  10528. else
  10529. ShowWarning("File '" CL_WHITE "%s" CL_RESET "' not found.\n", motd_txt);
  10530. return 0;
  10531. }
  10532. void pc_itemcd_do(struct map_session_data *sd, bool load) {
  10533. int i,cursor = 0;
  10534. struct item_cd* cd = NULL;
  10535. if( load ) {
  10536. if( !(cd = (struct item_cd*)idb_get(itemcd_db, sd->status.char_id)) ) {
  10537. // no item cooldown is associated with this character
  10538. return;
  10539. }
  10540. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  10541. if( cd->nameid[i] && DIFF_TICK(gettick(),cd->tick[i]) < 0 ) {
  10542. sd->item_delay[cursor].tick = cd->tick[i];
  10543. sd->item_delay[cursor].nameid = cd->nameid[i];
  10544. cursor++;
  10545. }
  10546. }
  10547. idb_remove(itemcd_db,sd->status.char_id);
  10548. } else {
  10549. if( !(cd = (struct item_cd*)idb_get(itemcd_db,sd->status.char_id)) ) {
  10550. // create a new skill cooldown object for map storage
  10551. CREATE( cd, struct item_cd, 1 );
  10552. idb_put( itemcd_db, sd->status.char_id, cd );
  10553. }
  10554. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  10555. if( sd->item_delay[i].nameid && DIFF_TICK(gettick(),sd->item_delay[i].tick) < 0 ) {
  10556. cd->tick[cursor] = sd->item_delay[i].tick;
  10557. cd->nameid[cursor] = sd->item_delay[i].nameid;
  10558. cursor++;
  10559. }
  10560. }
  10561. }
  10562. return;
  10563. }
  10564. /**
  10565. * Add item delay to player's item delay data
  10566. * @param sd Player
  10567. * @param id Item data
  10568. * @param tick Current tick
  10569. * @param n Item index in inventory
  10570. * @return 0: No delay, can consume item.
  10571. * 1: Has delay, cancel consumption.
  10572. **/
  10573. uint8 pc_itemcd_add(struct map_session_data *sd, struct item_data *id, t_tick tick, unsigned short n) {
  10574. int i;
  10575. ARR_FIND(0, MAX_ITEMDELAYS, i, sd->item_delay[i].nameid == id->nameid );
  10576. if( i == MAX_ITEMDELAYS ) /* item not found. try first empty now */
  10577. ARR_FIND(0, MAX_ITEMDELAYS, i, !sd->item_delay[i].nameid );
  10578. if( i < MAX_ITEMDELAYS ) {
  10579. if( sd->item_delay[i].nameid ) {// found
  10580. if( DIFF_TICK(sd->item_delay[i].tick, tick) > 0 ) {
  10581. t_tick e_tick = DIFF_TICK(sd->item_delay[i].tick, tick)/1000;
  10582. char e_msg[CHAT_SIZE_MAX];
  10583. if( e_tick > 99 )
  10584. sprintf(e_msg,msg_txt(sd,379), // Item Failed. [%s] is cooling down. Wait %.1f minutes.
  10585. itemdb_jname(sd->item_delay[i].nameid), (double)e_tick / 60);
  10586. else
  10587. sprintf(e_msg,msg_txt(sd,380), // Item Failed. [%s] is cooling down. Wait %d seconds.
  10588. itemdb_jname(sd->item_delay[i].nameid), e_tick+1);
  10589. clif_messagecolor(&sd->bl,color_table[COLOR_YELLOW],e_msg,false,SELF);
  10590. return 1; // Delay has not expired yet
  10591. }
  10592. } else {// not yet used item (all slots are initially empty)
  10593. sd->item_delay[i].nameid = id->nameid;
  10594. }
  10595. if( !(id->nameid == ITEMID_REINS_OF_MOUNT && sd->sc.option&(OPTION_WUGRIDER|OPTION_RIDING|OPTION_DRAGON|OPTION_MADOGEAR)) )
  10596. sd->item_delay[i].tick = tick + sd->inventory_data[n]->delay;
  10597. } else {// should not happen
  10598. ShowError("pc_itemcd_add: Exceeded item delay array capacity! (nameid=%hu, char_id=%d)\n", id->nameid, sd->status.char_id);
  10599. }
  10600. //clean up used delays so we can give room for more
  10601. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  10602. if( DIFF_TICK(sd->item_delay[i].tick, tick) <= 0 ) {
  10603. sd->item_delay[i].tick = 0;
  10604. sd->item_delay[i].nameid = 0;
  10605. }
  10606. }
  10607. return 0;
  10608. }
  10609. /**
  10610. * Check if player has delay to reuse item
  10611. * @param sd Player
  10612. * @param id Item data
  10613. * @param tick Current tick
  10614. * @param n Item index in inventory
  10615. * @return 0: No delay, can consume item.
  10616. * 1: Has delay, cancel consumption.
  10617. **/
  10618. uint8 pc_itemcd_check(struct map_session_data *sd, struct item_data *id, t_tick tick, unsigned short n) {
  10619. struct status_change *sc = NULL;
  10620. nullpo_retr(0, sd);
  10621. nullpo_retr(0, id);
  10622. // Do normal delay assignment
  10623. if (id->delay_sc <= SC_NONE || id->delay_sc >= SC_MAX || !(sc = &sd->sc))
  10624. return pc_itemcd_add(sd, id, tick, n);
  10625. // Send reply of delay remains
  10626. if (sc->data[id->delay_sc]) {
  10627. const struct TimerData *timer = get_timer(sc->data[id->delay_sc]->timer);
  10628. clif_msg_value(sd, ITEM_REUSE_LIMIT, (int)(timer ? DIFF_TICK(timer->tick, tick) / 1000 : 99));
  10629. return 1;
  10630. }
  10631. sc_start(&sd->bl, &sd->bl, (sc_type)id->delay_sc, 100, id->nameid, id->delay);
  10632. return 0;
  10633. }
  10634. /**
  10635. * Clear the dmglog data from player
  10636. * @param sd
  10637. * @param md
  10638. **/
  10639. static void pc_clear_log_damage_sub(uint32 char_id, struct mob_data *md)
  10640. {
  10641. uint8 i;
  10642. ARR_FIND(0,DAMAGELOG_SIZE,i,md->dmglog[i].id == char_id);
  10643. if (i < DAMAGELOG_SIZE) {
  10644. md->dmglog[i].id = 0;
  10645. md->dmglog[i].dmg = 0;
  10646. md->dmglog[i].flag = 0;
  10647. }
  10648. }
  10649. /**
  10650. * Add log to player's dmglog
  10651. * @param sd
  10652. * @param id Monster's GID
  10653. **/
  10654. void pc_damage_log_add(struct map_session_data *sd, int id)
  10655. {
  10656. uint8 i = 0;
  10657. if (!sd || !id)
  10658. return;
  10659. //Only store new data, don't need to renew the old one with same id
  10660. ARR_FIND(0, DAMAGELOG_SIZE_PC, i, sd->dmglog[i] == id);
  10661. if (i < DAMAGELOG_SIZE_PC)
  10662. return;
  10663. for (i = 0; i < DAMAGELOG_SIZE_PC; i++) {
  10664. if (sd->dmglog[i] == 0) {
  10665. sd->dmglog[i] = id;
  10666. return;
  10667. }
  10668. }
  10669. }
  10670. /**
  10671. * Clear dmglog data from player
  10672. * @param sd
  10673. * @param id Monster's id
  10674. **/
  10675. void pc_damage_log_clear(struct map_session_data *sd, int id)
  10676. {
  10677. uint8 i;
  10678. struct mob_data *md = NULL;
  10679. if (!sd)
  10680. return;
  10681. if (!id) {
  10682. for (i = 0; i < DAMAGELOG_SIZE_PC; i++) {
  10683. if( !sd->dmglog[i] ) //skip the empty value
  10684. continue;
  10685. if ((md = map_id2md(sd->dmglog[i])))
  10686. pc_clear_log_damage_sub(sd->status.char_id,md);
  10687. sd->dmglog[i] = 0;
  10688. }
  10689. }
  10690. else {
  10691. if ((md = map_id2md(id)))
  10692. pc_clear_log_damage_sub(sd->status.char_id,md);
  10693. ARR_FIND(0,DAMAGELOG_SIZE_PC,i,sd->dmglog[i] == id); // find the id position
  10694. if (i < DAMAGELOG_SIZE_PC)
  10695. sd->dmglog[i] = 0;
  10696. }
  10697. }
  10698. /**
  10699. * Status change data arrived from char-server
  10700. * @param sd: Player data
  10701. */
  10702. void pc_scdata_received(struct map_session_data *sd) {
  10703. pc_inventory_rentals(sd); // Needed here to remove rentals that have Status Changes after chrif_load_scdata has finished
  10704. clif_weight_limit( sd );
  10705. if( pc_has_permission( sd, PC_PERM_ATTENDANCE ) && pc_attendance_enabled() && !pc_attendance_rewarded_today( sd ) ){
  10706. clif_ui_open( sd, OUT_UI_ATTENDANCE, pc_attendance_counter( sd ) );
  10707. }
  10708. sd->state.pc_loaded = true;
  10709. if (sd->state.connect_new == 0 && sd->fd) { // Character already loaded map! Gotta trigger LoadEndAck manually.
  10710. sd->state.connect_new = 1;
  10711. clif_parse_LoadEndAck(sd->fd, sd);
  10712. }
  10713. if (pc_iscarton(sd)) {
  10714. sd->cart_weight_max = 0; // Force a client refesh
  10715. status_calc_cart_weight(sd, (e_status_calc_weight_opt)(CALCWT_ITEM|CALCWT_MAXBONUS|CALCWT_CARTSTATE));
  10716. }
  10717. }
  10718. /**
  10719. * Check player account expiration time and rental item expirations
  10720. * @param sd: Player data
  10721. */
  10722. void pc_check_expiration(struct map_session_data *sd) {
  10723. #ifndef ENABLE_SC_SAVING
  10724. pc_inventory_rentals(sd); // Check here if Status Change saving is disabled
  10725. #endif
  10726. if (sd->expiration_time != 0) { //Don't display if it's unlimited or unknow value
  10727. time_t exp_time = sd->expiration_time;
  10728. char tmpstr[1024];
  10729. strftime(tmpstr,sizeof(tmpstr) - 1,msg_txt(sd,501),localtime(&exp_time)); // "Your account time limit is: %d-%m-%Y %H:%M:%S."
  10730. clif_wis_message(sd,wisp_server_name,tmpstr,strlen(tmpstr) + 1,0);
  10731. pc_expire_check(sd);
  10732. }
  10733. }
  10734. TIMER_FUNC(pc_expiration_timer){
  10735. struct map_session_data *sd = map_id2sd(id);
  10736. if( !sd ) return 0;
  10737. sd->expiration_tid = INVALID_TIMER;
  10738. if( sd->fd )
  10739. clif_authfail_fd(sd->fd,10);
  10740. map_quit(sd);
  10741. return 0;
  10742. }
  10743. TIMER_FUNC(pc_autotrade_timer){
  10744. struct map_session_data *sd = map_id2sd(id);
  10745. if (!sd)
  10746. return 0;
  10747. sd->autotrade_tid = INVALID_TIMER;
  10748. if (sd->state.autotrade&2)
  10749. vending_reopen(sd);
  10750. if (sd->state.autotrade&4)
  10751. buyingstore_reopen(sd);
  10752. if (!sd->vender_id && !sd->buyer_id) {
  10753. sd->state.autotrade = 0;
  10754. map_quit(sd);
  10755. }
  10756. return 0;
  10757. }
  10758. /* this timer exists only when a character with a expire timer > 24h is online */
  10759. /* it loops thru online players once an hour to check whether a new < 24h is available */
  10760. TIMER_FUNC(pc_global_expiration_timer){
  10761. struct s_mapiterator* iter;
  10762. struct map_session_data* sd;
  10763. iter = mapit_getallusers();
  10764. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  10765. if( sd->expiration_time )
  10766. pc_expire_check(sd);
  10767. mapit_free(iter);
  10768. return 0;
  10769. }
  10770. void pc_expire_check(struct map_session_data *sd) {
  10771. /* ongoing timer */
  10772. if( sd->expiration_tid != INVALID_TIMER )
  10773. return;
  10774. /* not within the next 24h, enable the global check */
  10775. if( sd->expiration_time > (time(NULL) + ((60 * 60) * 24)) ) {
  10776. /* global check not running, enable */
  10777. if( pc_expiration_tid == INVALID_TIMER ) /* Starts in 1h, repeats every hour */
  10778. pc_expiration_tid = add_timer_interval(gettick() + ((1000 * 60) * 60), pc_global_expiration_timer, 0, 0, ((1000 * 60) * 60));
  10779. return;
  10780. }
  10781. sd->expiration_tid = add_timer(gettick() + (unsigned int)(sd->expiration_time - time(NULL)) * 1000, pc_expiration_timer, sd->bl.id, 0);
  10782. }
  10783. /**
  10784. * Deposit some money to bank
  10785. * @param sd
  10786. * @param money Amount of money to deposit
  10787. **/
  10788. enum e_BANKING_DEPOSIT_ACK pc_bank_deposit(struct map_session_data *sd, int money) {
  10789. unsigned int limit_check = money + sd->bank_vault;
  10790. if( money <= 0 || limit_check > MAX_BANK_ZENY ) {
  10791. return BDA_OVERFLOW;
  10792. } else if ( money > sd->status.zeny ) {
  10793. return BDA_NO_MONEY;
  10794. }
  10795. if( pc_payzeny(sd,money, LOG_TYPE_BANK, NULL) )
  10796. return BDA_NO_MONEY;
  10797. sd->bank_vault += money;
  10798. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  10799. if( save_settings&CHARSAVE_BANK )
  10800. chrif_save(sd, CSAVE_NORMAL);
  10801. return BDA_SUCCESS;
  10802. }
  10803. /**
  10804. * Withdraw money from bank
  10805. * @param sd
  10806. * @param money Amount of money that will be withdrawn
  10807. **/
  10808. enum e_BANKING_WITHDRAW_ACK pc_bank_withdraw(struct map_session_data *sd, int money) {
  10809. unsigned int limit_check = money + sd->status.zeny;
  10810. if( money <= 0 ) {
  10811. return BWA_UNKNOWN_ERROR;
  10812. } else if ( money > sd->bank_vault ) {
  10813. return BWA_NO_MONEY;
  10814. } else if ( limit_check > MAX_ZENY ) {
  10815. /* no official response for this scenario exists. */
  10816. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,1495),false,SELF); //You can't withdraw that much money
  10817. return BWA_UNKNOWN_ERROR;
  10818. }
  10819. if( pc_getzeny(sd,money, LOG_TYPE_BANK, NULL) )
  10820. return BWA_NO_MONEY;
  10821. sd->bank_vault -= money;
  10822. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  10823. if( save_settings&CHARSAVE_BANK )
  10824. chrif_save(sd, CSAVE_NORMAL);
  10825. return BWA_SUCCESS;
  10826. }
  10827. /**
  10828. * Clear Crimson Marker data from caster
  10829. * @param sd: Player
  10830. **/
  10831. void pc_crimson_marker_clear(struct map_session_data *sd) {
  10832. uint8 i;
  10833. if (!sd)
  10834. return;
  10835. for (i = 0; i < MAX_SKILL_CRIMSON_MARKER; i++) {
  10836. struct block_list *bl = NULL;
  10837. if (sd->c_marker[i] && (bl = map_id2bl(sd->c_marker[i])))
  10838. status_change_end(bl,SC_C_MARKER,INVALID_TIMER);
  10839. sd->c_marker[i] = 0;
  10840. }
  10841. }
  10842. /**
  10843. * Show version to player
  10844. * @param sd: Player
  10845. **/
  10846. void pc_show_version(struct map_session_data *sd) {
  10847. const char* svn = get_svn_revision();
  10848. char buf[CHAT_SIZE_MAX];
  10849. if( svn[0] != UNKNOWN_VERSION )
  10850. sprintf(buf,msg_txt(sd,1295),"SVN: r",svn); //rAthena Version SVN: r%s
  10851. else {
  10852. const char* git = get_git_hash();
  10853. if( git[0] != UNKNOWN_VERSION )
  10854. sprintf(buf,msg_txt(sd,1295),"Git Hash: ",git); //rAthena Version Git Hash: %s
  10855. else
  10856. sprintf(buf,"%s",msg_txt(sd,1296)); //Cannot determine SVN/Git version.
  10857. }
  10858. clif_displaymessage(sd->fd,buf);
  10859. }
  10860. /**
  10861. * Run bonus_script on player
  10862. * @param sd
  10863. * @author [Cydh]
  10864. **/
  10865. void pc_bonus_script(struct map_session_data *sd) {
  10866. t_tick now = gettick();
  10867. struct linkdb_node *node = NULL, *next = NULL;
  10868. if (!sd || !(node = sd->bonus_script.head))
  10869. return;
  10870. while (node) {
  10871. struct s_bonus_script_entry *entry = NULL;
  10872. next = node->next;
  10873. if ((entry = (struct s_bonus_script_entry *)node->data)) {
  10874. // Only start timer for new bonus_script
  10875. if (entry->tid == INVALID_TIMER) {
  10876. if (entry->icon != EFST_BLANK) // Gives status icon if exist
  10877. clif_status_change(&sd->bl, entry->icon, 1, entry->tick, 1, 0, 0);
  10878. entry->tick += now;
  10879. entry->tid = add_timer(entry->tick, pc_bonus_script_timer, sd->bl.id, (intptr_t)entry);
  10880. }
  10881. if (entry->script)
  10882. run_script(entry->script, 0, sd->bl.id, 0);
  10883. else
  10884. ShowError("pc_bonus_script: The script has been removed somewhere. \"%s\"\n", StringBuf_Value(entry->script_buf));
  10885. }
  10886. node = next;
  10887. }
  10888. }
  10889. /**
  10890. * Add bonus_script to player
  10891. * @param sd Player
  10892. * @param script_str Script string
  10893. * @param dur Duration in ms
  10894. * @param icon EFST
  10895. * @param flag Flags @see enum e_bonus_script_flags
  10896. * @param type 0 - None, 1 - Buff, 2 - Debuff
  10897. * @return New created entry pointer or NULL if failed or NULL if duplicate fail
  10898. * @author [Cydh]
  10899. **/
  10900. 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) {
  10901. struct script_code *script = NULL;
  10902. struct linkdb_node *node = NULL;
  10903. struct s_bonus_script_entry *entry = NULL;
  10904. if (!sd)
  10905. return NULL;
  10906. if (!(script = parse_script(script_str, "bonus_script", 0, SCRIPT_IGNORE_EXTERNAL_BRACKETS))) {
  10907. ShowError("pc_bonus_script_add: Failed to parse script '%s' (CID:%d).\n", script_str, sd->status.char_id);
  10908. return NULL;
  10909. }
  10910. // Duplication checks
  10911. if ((node = sd->bonus_script.head)) {
  10912. while (node) {
  10913. entry = (struct s_bonus_script_entry *)node->data;
  10914. if (strcmpi(script_str, StringBuf_Value(entry->script_buf)) == 0) {
  10915. t_tick newdur = gettick() + dur;
  10916. if (flag&BSF_FORCE_REPLACE && entry->tick < newdur) { // Change duration
  10917. sett_tickimer(entry->tid, newdur);
  10918. script_free_code(script);
  10919. return NULL;
  10920. }
  10921. else if (flag&BSF_FORCE_DUPLICATE) // Allow duplicate
  10922. break;
  10923. else { // No duplicate bonus
  10924. script_free_code(script);
  10925. return NULL;
  10926. }
  10927. }
  10928. node = node->next;
  10929. }
  10930. }
  10931. CREATE(entry, struct s_bonus_script_entry, 1);
  10932. entry->script_buf = StringBuf_Malloc();
  10933. StringBuf_AppendStr(entry->script_buf, script_str);
  10934. entry->tid = INVALID_TIMER;
  10935. entry->flag = flag;
  10936. entry->icon = icon;
  10937. entry->tick = dur; // Use duration first, on run change to expire time
  10938. entry->type = type;
  10939. entry->script = script;
  10940. sd->bonus_script.count++;
  10941. return entry;
  10942. }
  10943. /**
  10944. * Remove bonus_script data from player
  10945. * @param sd: Target player
  10946. * @param list: Bonus script entry from player
  10947. * @author [Cydh]
  10948. **/
  10949. void pc_bonus_script_free_entry(struct map_session_data *sd, struct s_bonus_script_entry *entry) {
  10950. if (entry->tid != INVALID_TIMER)
  10951. delete_timer(entry->tid, pc_bonus_script_timer);
  10952. if (entry->script)
  10953. script_free_code(entry->script);
  10954. if (entry->script_buf)
  10955. StringBuf_Free(entry->script_buf);
  10956. if (sd) {
  10957. if (entry->icon != EFST_BLANK)
  10958. clif_status_load(&sd->bl, entry->icon, 0);
  10959. if (sd->bonus_script.count > 0)
  10960. sd->bonus_script.count--;
  10961. }
  10962. aFree(entry);
  10963. }
  10964. /**
  10965. * Do final process if no entry left
  10966. * @param sd
  10967. **/
  10968. static void inline pc_bonus_script_check_final(struct map_session_data *sd) {
  10969. if (sd->bonus_script.count == 0) {
  10970. if (sd->bonus_script.head && sd->bonus_script.head->data)
  10971. pc_bonus_script_free_entry(sd, (struct s_bonus_script_entry *)sd->bonus_script.head->data);
  10972. linkdb_final(&sd->bonus_script.head);
  10973. }
  10974. }
  10975. /**
  10976. * Timer for bonus_script
  10977. * @param tid
  10978. * @param tick
  10979. * @param id
  10980. * @param data
  10981. * @author [Cydh]
  10982. **/
  10983. TIMER_FUNC(pc_bonus_script_timer){
  10984. struct map_session_data *sd;
  10985. struct s_bonus_script_entry *entry = (struct s_bonus_script_entry *)data;
  10986. sd = map_id2sd(id);
  10987. if (!sd) {
  10988. ShowError("pc_bonus_script_timer: Null pointer id: %d tid: %d\n", id, tid);
  10989. return 0;
  10990. }
  10991. if (tid == INVALID_TIMER)
  10992. return 0;
  10993. if (!sd->bonus_script.head || entry == NULL) {
  10994. ShowError("pc_bonus_script_timer: Invalid entry pointer %p!\n", entry);
  10995. return 0;
  10996. }
  10997. linkdb_erase(&sd->bonus_script.head, (void *)((intptr_t)entry));
  10998. pc_bonus_script_free_entry(sd, entry);
  10999. pc_bonus_script_check_final(sd);
  11000. status_calc_pc(sd,SCO_NONE);
  11001. return 0;
  11002. }
  11003. /**
  11004. * Check then clear all active timer(s) of bonus_script data from player based on reason
  11005. * @param sd: Target player
  11006. * @param flag: Reason to remove the bonus_script. e_bonus_script_flags or e_bonus_script_types
  11007. * @author [Cydh]
  11008. **/
  11009. void pc_bonus_script_clear(struct map_session_data *sd, uint16 flag) {
  11010. struct linkdb_node *node = NULL;
  11011. uint16 count = 0;
  11012. if (!sd || !(node = sd->bonus_script.head))
  11013. return;
  11014. while (node) {
  11015. struct linkdb_node *next = node->next;
  11016. struct s_bonus_script_entry *entry = (struct s_bonus_script_entry *)node->data;
  11017. if (entry && (
  11018. (flag == BSF_PERMANENT) || // Remove all with permanent bonus
  11019. (!flag && !(entry->flag&BSF_PERMANENT)) || // Remove all WITHOUT permanent bonus
  11020. (flag&entry->flag) || // Matched flag
  11021. (flag&BSF_REM_BUFF && entry->type == 1) || // Remove buff
  11022. (flag&BSF_REM_DEBUFF && entry->type == 2) // Remove debuff
  11023. )
  11024. )
  11025. {
  11026. linkdb_erase(&sd->bonus_script.head, (void *)((intptr_t)entry));
  11027. pc_bonus_script_free_entry(sd, entry);
  11028. count++;
  11029. }
  11030. node = next;
  11031. }
  11032. pc_bonus_script_check_final(sd);
  11033. if (count && !(flag&BSF_REM_ON_LOGOUT)) //Don't need to do this if log out
  11034. status_calc_pc(sd,SCO_NONE);
  11035. }
  11036. /** [Cydh]
  11037. * Gives/removes SC_BASILICA when player steps in/out the cell with 'cell_basilica'
  11038. * @param sd: Target player
  11039. */
  11040. void pc_cell_basilica(struct map_session_data *sd) {
  11041. nullpo_retv(sd);
  11042. if (!map_getcell(sd->bl.m,sd->bl.x,sd->bl.y,CELL_CHKBASILICA)) {
  11043. if (sd->sc.data[SC_BASILICA])
  11044. status_change_end(&sd->bl,SC_BASILICA,INVALID_TIMER);
  11045. }
  11046. else if (!sd->sc.data[SC_BASILICA])
  11047. sc_start(&sd->bl,&sd->bl,SC_BASILICA,100,0,INFINITE_TICK);
  11048. }
  11049. /** [Cydh]
  11050. * Get maximum specified parameter for specified class
  11051. * @param class_: sd->class
  11052. * @param sex: sd->status.sex
  11053. * @param flag: parameter will be checked
  11054. * @return max_param
  11055. */
  11056. short pc_maxparameter(struct map_session_data *sd, enum e_params param) {
  11057. int idx = -1, class_ = sd->class_;
  11058. if ((idx = pc_class2idx(pc_mapid2jobid(class_,sd->status.sex))) >= 0) {
  11059. short max_param = 0;
  11060. switch (param) {
  11061. case PARAM_STR: max_param = job_info[idx].max_param.str; break;
  11062. case PARAM_AGI: max_param = job_info[idx].max_param.agi; break;
  11063. case PARAM_VIT: max_param = job_info[idx].max_param.vit; break;
  11064. case PARAM_INT: max_param = job_info[idx].max_param.int_; break;
  11065. case PARAM_DEX: max_param = job_info[idx].max_param.dex; break;
  11066. case PARAM_LUK: max_param = job_info[idx].max_param.luk; break;
  11067. }
  11068. if (max_param > 0)
  11069. return max_param;
  11070. }
  11071. return (class_&MAPID_BASEMASK) == MAPID_SUMMONER ? battle_config.max_summoner_parameter :
  11072. ((class_&MAPID_UPPERMASK) == MAPID_KAGEROUOBORO || (class_&MAPID_UPPERMASK) == MAPID_REBELLION) ? battle_config.max_extended_parameter :
  11073. ((class_&JOBL_THIRD) ? ((class_&JOBL_UPPER) ? battle_config.max_third_trans_parameter : ((class_&JOBL_BABY) ? battle_config.max_baby_third_parameter : battle_config.max_third_parameter)) :
  11074. ((class_&JOBL_BABY) ? battle_config.max_baby_parameter :
  11075. ((class_&JOBL_UPPER) ? battle_config.max_trans_parameter : battle_config.max_parameter)));
  11076. }
  11077. /**
  11078. * Get max ASPD for player based on Class
  11079. * @param sd Player
  11080. * @return ASPD
  11081. */
  11082. short pc_maxaspd(struct map_session_data *sd) {
  11083. nullpo_ret(sd);
  11084. return (( sd->class_&JOBL_THIRD) ? battle_config.max_third_aspd : (
  11085. ((sd->class_&MAPID_UPPERMASK) == MAPID_KAGEROUOBORO || (sd->class_&MAPID_UPPERMASK) == MAPID_REBELLION) ? battle_config.max_extended_aspd :
  11086. battle_config.max_aspd ));
  11087. }
  11088. /**
  11089. * Calculates total item-group related bonuses for the given item
  11090. * @param sd Player
  11091. * @param nameid Item ID
  11092. * @return Heal rate
  11093. **/
  11094. short pc_get_itemgroup_bonus(struct map_session_data* sd, unsigned short nameid) {
  11095. if (sd->itemgrouphealrate.empty())
  11096. return 0;
  11097. short bonus = 0;
  11098. for (const auto &it : sd->itemgrouphealrate) {
  11099. uint16 group_id = it.id, i;
  11100. struct s_item_group_db *group = NULL;
  11101. if (!group_id || !(group = itemdb_group_exists(group_id)))
  11102. continue;
  11103. for (i = 0; i < group->random[0].data_qty; i++) {
  11104. if (group->random[0].data[i].nameid == nameid) {
  11105. bonus += it.val;
  11106. break;
  11107. }
  11108. }
  11109. }
  11110. return bonus;
  11111. }
  11112. /**
  11113. * Calculates total item-group related bonuses for the given item group
  11114. * @param sd Player
  11115. * @param group_id Item Group ID
  11116. * @return Heal rate
  11117. **/
  11118. short pc_get_itemgroup_bonus_group(struct map_session_data* sd, uint16 group_id) {
  11119. if (sd->itemgrouphealrate.empty())
  11120. return 0;
  11121. for (const auto &it : sd->itemgrouphealrate) {
  11122. if (it.id == group_id)
  11123. return it.val;
  11124. }
  11125. return 0;
  11126. }
  11127. /**
  11128. * Check if player's equip index in same specified position, like for 2-Handed weapon & Heagdear (inc. costume)
  11129. * @param eqi Item EQI of enum equip_index
  11130. * @param *equip_index Player's equip_index[]
  11131. * @param index Known index item in inventory from sd->equip_index[] to compare with specified EQI in *equip_index
  11132. * @return True if item in same inventory index, False if doesn't
  11133. */
  11134. bool pc_is_same_equip_index(enum equip_index eqi, short *equip_index, short index) {
  11135. if (index < 0 || index >= MAX_INVENTORY)
  11136. return true;
  11137. // Dual weapon checks
  11138. if (eqi == EQI_HAND_R && equip_index[EQI_HAND_L] == index)
  11139. return true;
  11140. // Headgear with Mid & Low location
  11141. if (eqi == EQI_HEAD_MID && equip_index[EQI_HEAD_LOW] == index)
  11142. return true;
  11143. // Headgear with Top & Mid or Low location
  11144. if (eqi == EQI_HEAD_TOP && (equip_index[EQI_HEAD_MID] == index || equip_index[EQI_HEAD_LOW] == index))
  11145. return true;
  11146. // Headgear with Mid & Low location
  11147. if (eqi == EQI_COSTUME_HEAD_MID && equip_index[EQI_COSTUME_HEAD_LOW] == index)
  11148. return true;
  11149. // Headgear with Top & Mid or Low location
  11150. if (eqi == EQI_COSTUME_HEAD_TOP && (equip_index[EQI_COSTUME_HEAD_MID] == index || equip_index[EQI_COSTUME_HEAD_LOW] == index))
  11151. return true;
  11152. return false;
  11153. }
  11154. /**
  11155. * Generate Unique item ID for player
  11156. * @param sd : Player
  11157. * @return A generated Unique item ID
  11158. */
  11159. uint64 pc_generate_unique_id(struct map_session_data *sd) {
  11160. nullpo_ret(sd);
  11161. return ((uint64)sd->status.char_id << 32) | sd->status.uniqueitem_counter++;
  11162. }
  11163. /**
  11164. * Validating skill from player after logged on
  11165. * @param sd
  11166. **/
  11167. void pc_validate_skill(struct map_session_data *sd) {
  11168. if (sd) {
  11169. uint16 i = 0, count = 0;
  11170. struct s_skill tmp_skills[MAX_SKILL] = {{ 0 }};
  11171. memcpy(tmp_skills, sd->status.skill, sizeof(sd->status.skill));
  11172. memset(sd->status.skill, 0, sizeof(sd->status.skill));
  11173. for (i = 0; i < MAX_SKILL; i++) {
  11174. uint16 idx = 0;
  11175. if (tmp_skills[i].id == 0 || tmp_skills[i].lv == 0)
  11176. continue;
  11177. if ((idx = skill_get_index(tmp_skills[i].id))) {
  11178. memcpy(&sd->status.skill[idx], &tmp_skills[i], sizeof(tmp_skills[i]));
  11179. count++;
  11180. }
  11181. else
  11182. 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);
  11183. }
  11184. }
  11185. }
  11186. /**
  11187. * Toggle to remember if the questinfo is displayed yet or not.
  11188. * @param qi_display Display flag
  11189. * @param show If show is true and qi_display is 0, set qi_display to 1 and show the event bubble.
  11190. * If show is false and qi_display is 1, set qi_display to 0 and hide the event bubble.
  11191. **/
  11192. static void pc_show_questinfo_sub(struct map_session_data *sd, bool *qi_display, struct questinfo *qi, bool show) {
  11193. if (show) {
  11194. // Check if need to be displayed
  11195. if ((*qi_display) != 1) {
  11196. (*qi_display) = 1;
  11197. clif_quest_show_event(sd, &qi->nd->bl, qi->icon, qi->color);
  11198. }
  11199. }
  11200. else {
  11201. // Check if need to be hide
  11202. if ((*qi_display) != 0) {
  11203. (*qi_display) = 0;
  11204. #if PACKETVER >= 20120410
  11205. clif_quest_show_event(sd, &qi->nd->bl, 9999, 0);
  11206. #else
  11207. clif_quest_show_event(sd, &qi->nd->bl, 0, 0);
  11208. #endif
  11209. }
  11210. }
  11211. }
  11212. /**
  11213. * Show available NPC Quest / Event Icon Check [Kisuka]
  11214. * @param sd Player
  11215. **/
  11216. void pc_show_questinfo(struct map_session_data *sd) {
  11217. #if PACKETVER >= 20090218
  11218. struct questinfo *qi = NULL;
  11219. unsigned short i;
  11220. uint8 j;
  11221. int8 mystate = 0;
  11222. bool failed = false;
  11223. nullpo_retv(sd);
  11224. if (sd->bl.m < 0 || sd->bl.m >= MAX_MAPINDEX)
  11225. return;
  11226. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  11227. if (!mapdata->qi_count || !mapdata->qi_data)
  11228. return;
  11229. if (mapdata->qi_count != sd->qi_count)
  11230. return; // init was not called yet
  11231. for(i = 0; i < mapdata->qi_count; i++) {
  11232. qi = &mapdata->qi_data[i];
  11233. if (!qi)
  11234. continue;
  11235. if (quest_check(sd, qi->quest_id, HAVEQUEST) != -1) { // Check if quest is not started
  11236. pc_show_questinfo_sub(sd, &sd->qi_display[i], qi, false);
  11237. continue;
  11238. }
  11239. // Level range checks
  11240. if (sd->status.base_level < qi->min_level || sd->status.base_level > qi->max_level) {
  11241. pc_show_questinfo_sub(sd, &sd->qi_display[i], qi, false);
  11242. continue;
  11243. }
  11244. // Quest requirements
  11245. if (qi->req_count) {
  11246. failed = false;
  11247. for (j = 0; j < qi->req_count; j++) {
  11248. mystate = quest_check(sd, qi->req[j].quest_id, HAVEQUEST);
  11249. mystate = mystate + (mystate < 1);
  11250. if (mystate != qi->req[j].state) {
  11251. failed = true;
  11252. break;
  11253. }
  11254. }
  11255. if (failed) {
  11256. pc_show_questinfo_sub(sd, &sd->qi_display[i], qi, false);
  11257. continue;
  11258. }
  11259. }
  11260. // Job requirements
  11261. if (qi->jobid_count) {
  11262. failed = true;
  11263. for (j = 0; j < qi->jobid_count; j++) {
  11264. if (pc_mapid2jobid(sd->class_,sd->status.sex) == qi->jobid[j]) {
  11265. pc_show_questinfo_sub(sd, &sd->qi_display[i], qi, true);
  11266. failed = false;
  11267. break;
  11268. }
  11269. }
  11270. if (!failed)
  11271. continue;
  11272. pc_show_questinfo_sub(sd, &sd->qi_display[i], qi, false);
  11273. }
  11274. else {
  11275. pc_show_questinfo_sub(sd, &sd->qi_display[i], qi, true);
  11276. }
  11277. }
  11278. #endif
  11279. }
  11280. /**
  11281. * Reinit the questinfo for player when changing map
  11282. * @param sd Player
  11283. **/
  11284. void pc_show_questinfo_reinit(struct map_session_data *sd) {
  11285. #if PACKETVER >= 20090218
  11286. nullpo_retv(sd);
  11287. if (sd->qi_display) {
  11288. aFree(sd->qi_display);
  11289. sd->qi_display = NULL;
  11290. }
  11291. sd->qi_count = 0;
  11292. if (sd->bl.m < 0 || sd->bl.m >= MAX_MAPINDEX)
  11293. return;
  11294. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  11295. if (!mapdata->qi_count || !mapdata->qi_data)
  11296. return;
  11297. CREATE(sd->qi_display, bool, (sd->qi_count = mapdata->qi_count));
  11298. #endif
  11299. }
  11300. /**
  11301. * Check if a job is allowed to enter the map
  11302. * @param jobid Job ID see enum e_job or sd->status.class_
  11303. * @param m ID -an index- for direct indexing map[] array
  11304. * @return 1 if job is allowed, 0 otherwise
  11305. **/
  11306. bool pc_job_can_entermap(enum e_job jobid, int m, int group_lv) {
  11307. uint16 idx = 0;
  11308. // Map is other map server.
  11309. // !FIXME: Currently, a map-server doesn't recognized map's attributes on other server, so we assume it's fine to warp.
  11310. if (m < 0)
  11311. return true;
  11312. struct map_data *mapdata = map_getmapdata(m);
  11313. if (!mapdata->cell)
  11314. return false;
  11315. if (!pcdb_checkid(jobid))
  11316. return false;
  11317. idx = pc_class2idx(jobid);
  11318. if (!job_info[idx].noenter_map.zone || group_lv > job_info[idx].noenter_map.group_lv)
  11319. return true;
  11320. if ((job_info[idx].noenter_map.zone&1 && !mapdata_flag_vs2(mapdata)) || // Normal
  11321. (job_info[idx].noenter_map.zone&2 && mapdata->flag[MF_PVP]) || // PVP
  11322. (job_info[idx].noenter_map.zone&4 && mapdata_flag_gvg2_no_te(mapdata)) || // GVG
  11323. (job_info[idx].noenter_map.zone&8 && mapdata->flag[MF_BATTLEGROUND]) || // Battleground
  11324. (job_info[idx].noenter_map.zone&16 && mapdata_flag_gvg2_te(mapdata)) || // WOE:TE
  11325. (job_info[idx].noenter_map.zone&(8*mapdata->zone) && mapdata->flag[MF_RESTRICTED]) // Zone restriction
  11326. )
  11327. return false;
  11328. return true;
  11329. }
  11330. /**
  11331. * Tells client about player's costume view on mapchange for checking 'nocostume' mapflag.
  11332. * @param sd
  11333. **/
  11334. void pc_set_costume_view(struct map_session_data *sd) {
  11335. int i = -1, head_low = 0, head_mid = 0, head_top = 0, robe = 0;
  11336. struct item_data *id = NULL;
  11337. nullpo_retv(sd);
  11338. head_low = sd->status.head_bottom;
  11339. head_mid = sd->status.head_mid;
  11340. head_top = sd->status.head_top;
  11341. robe = sd->status.robe;
  11342. sd->status.head_bottom = sd->status.head_mid = sd->status.head_top = sd->status.robe = 0;
  11343. //Added check to prevent sending the same look on multiple slots ->
  11344. //causes client to redraw item on top of itself. (suggested by Lupus)
  11345. // Normal headgear checks
  11346. if ((i = sd->equip_index[EQI_HEAD_LOW]) != -1 && (id = sd->inventory_data[i])) {
  11347. if (!(id->equip&(EQP_HEAD_MID|EQP_HEAD_TOP)))
  11348. sd->status.head_bottom = id->look;
  11349. else
  11350. sd->status.head_bottom = 0;
  11351. }
  11352. if ((i = sd->equip_index[EQI_HEAD_MID]) != -1 && (id = sd->inventory_data[i])) {
  11353. if (!(id->equip&(EQP_HEAD_TOP)))
  11354. sd->status.head_mid = id->look;
  11355. else
  11356. sd->status.head_mid = 0;
  11357. }
  11358. if ((i = sd->equip_index[EQI_HEAD_TOP]) != -1 && (id = sd->inventory_data[i]))
  11359. sd->status.head_top = id->look;
  11360. if ((i = sd->equip_index[EQI_GARMENT]) != -1 && (id = sd->inventory_data[i]))
  11361. sd->status.robe = id->look;
  11362. // Costumes check
  11363. if (!map_getmapflag(sd->bl.m, MF_NOCOSTUME)) {
  11364. if ((i = sd->equip_index[EQI_COSTUME_HEAD_LOW]) != -1 && (id = sd->inventory_data[i])) {
  11365. if (!(id->equip&(EQP_COSTUME_HEAD_MID|EQP_COSTUME_HEAD_TOP)))
  11366. sd->status.head_bottom = id->look;
  11367. else
  11368. sd->status.head_bottom = 0;
  11369. }
  11370. if ((i = sd->equip_index[EQI_COSTUME_HEAD_MID]) != -1 && (id = sd->inventory_data[i])) {
  11371. if (!(id->equip&EQP_COSTUME_HEAD_TOP))
  11372. sd->status.head_mid = id->look;
  11373. else
  11374. sd->status.head_mid = 0;
  11375. }
  11376. if ((i = sd->equip_index[EQI_COSTUME_HEAD_TOP]) != -1 && (id = sd->inventory_data[i]))
  11377. sd->status.head_top = id->look;
  11378. if ((i = sd->equip_index[EQI_COSTUME_GARMENT]) != -1 && (id = sd->inventory_data[i]))
  11379. sd->status.robe = id->look;
  11380. }
  11381. if (sd->setlook_head_bottom)
  11382. sd->status.head_bottom = sd->setlook_head_bottom;
  11383. if (sd->setlook_head_mid)
  11384. sd->status.head_mid = sd->setlook_head_mid;
  11385. if (sd->setlook_head_top)
  11386. sd->status.head_top = sd->setlook_head_top;
  11387. if (sd->setlook_robe)
  11388. sd->status.robe = sd->setlook_robe;
  11389. if (head_low != sd->status.head_bottom)
  11390. clif_changelook(&sd->bl, LOOK_HEAD_BOTTOM, sd->status.head_bottom);
  11391. if (head_mid != sd->status.head_mid)
  11392. clif_changelook(&sd->bl, LOOK_HEAD_MID, sd->status.head_mid);
  11393. if (head_top != sd->status.head_top)
  11394. clif_changelook(&sd->bl, LOOK_HEAD_TOP, sd->status.head_top);
  11395. if (robe != sd->status.robe)
  11396. clif_changelook(&sd->bl, LOOK_ROBE, sd->status.robe);
  11397. }
  11398. std::shared_ptr<s_attendance_period> pc_attendance_period(){
  11399. uint32 date = date_get(DT_YYYYMMDD);
  11400. for( std::pair<const uint32,std::shared_ptr<s_attendance_period>>& pair : attendance_db ){
  11401. std::shared_ptr<s_attendance_period> period = pair.second;
  11402. if( period->start <= date && period->end >= date ){
  11403. return period;
  11404. }
  11405. }
  11406. return nullptr;
  11407. }
  11408. bool pc_attendance_enabled(){
  11409. // Check if the attendance feature is disabled
  11410. if( !battle_config.feature_attendance ){
  11411. return false;
  11412. }
  11413. // Check if there is a running attendance period
  11414. return pc_attendance_period() != nullptr;
  11415. }
  11416. static inline bool pc_attendance_rewarded_today( struct map_session_data* sd ){
  11417. return pc_readreg2( sd, ATTENDANCE_DATE_VAR ) >= date_get(DT_YYYYMMDD);
  11418. }
  11419. int32 pc_attendance_counter( struct map_session_data* sd ){
  11420. std::shared_ptr<s_attendance_period> period = pc_attendance_period();
  11421. // No running attendance period
  11422. if( period == nullptr ){
  11423. return 0;
  11424. }
  11425. // Get the counter for the current period
  11426. int counter = pc_readreg2( sd, ATTENDANCE_COUNT_VAR );
  11427. // Check if we have a remaining counter from a previous period
  11428. if( counter > 0 && pc_readreg2( sd, ATTENDANCE_DATE_VAR ) < period->start ){
  11429. // Reset the counter to zero
  11430. pc_setreg2( sd, ATTENDANCE_COUNT_VAR, 0 );
  11431. return 0;
  11432. }
  11433. return 10 * counter + ( ( pc_attendance_rewarded_today(sd) ) ? 1 : 0 );
  11434. }
  11435. void pc_attendance_claim_reward( struct map_session_data* sd ){
  11436. // If the user's group does not have the permission
  11437. if( !pc_has_permission( sd, PC_PERM_ATTENDANCE ) ){
  11438. return;
  11439. }
  11440. // Check if the attendance feature is disabled
  11441. if( !pc_attendance_enabled() ){
  11442. return;
  11443. }
  11444. // Check if the user already got his reward today
  11445. if( pc_attendance_rewarded_today( sd ) ){
  11446. return;
  11447. }
  11448. int32 attendance_counter = pc_readreg2( sd, ATTENDANCE_COUNT_VAR );
  11449. attendance_counter += 1;
  11450. std::shared_ptr<s_attendance_period> period = pc_attendance_period();
  11451. if( period == nullptr ){
  11452. return;
  11453. }
  11454. if( period->rewards.size() < attendance_counter ){
  11455. return;
  11456. }
  11457. pc_setreg2( sd, ATTENDANCE_DATE_VAR, date_get(DT_YYYYMMDD) );
  11458. pc_setreg2( sd, ATTENDANCE_COUNT_VAR, attendance_counter );
  11459. if( save_settings&CHARSAVE_ATTENDANCE )
  11460. chrif_save(sd, CSAVE_NORMAL);
  11461. std::shared_ptr<s_attendance_reward> reward = period->rewards[attendance_counter - 1];
  11462. struct mail_message msg;
  11463. memset( &msg, 0, sizeof( struct mail_message ) );
  11464. msg.dest_id = sd->status.char_id;
  11465. safestrncpy( msg.send_name, msg_txt( sd, 788 ), NAME_LENGTH );
  11466. safesnprintf( msg.title, MAIL_TITLE_LENGTH, msg_txt( sd, 789 ), attendance_counter );
  11467. safesnprintf( msg.body, MAIL_BODY_LENGTH, msg_txt( sd, 790 ), attendance_counter );
  11468. msg.item[0].nameid = reward->item_id;
  11469. msg.item[0].amount = reward->amount;
  11470. msg.item[0].identify = 1;
  11471. msg.status = MAIL_NEW;
  11472. msg.type = MAIL_INBOX_NORMAL;
  11473. msg.timestamp = time(NULL);
  11474. intif_Mail_send(0, &msg);
  11475. clif_attendence_response( sd, attendance_counter );
  11476. }
  11477. /*==========================================
  11478. * pc Init/Terminate
  11479. *------------------------------------------*/
  11480. void do_final_pc(void) {
  11481. db_destroy(itemcd_db);
  11482. do_final_pc_groups();
  11483. ers_destroy(pc_sc_display_ers);
  11484. ers_destroy(num_reg_ers);
  11485. ers_destroy(str_reg_ers);
  11486. attendance_db.clear();
  11487. }
  11488. void do_init_pc(void) {
  11489. itemcd_db = idb_alloc(DB_OPT_RELEASE_DATA);
  11490. pc_readdb();
  11491. pc_read_motd(); // Read MOTD [Valaris]
  11492. attendance_db.load();
  11493. add_timer_func_list(pc_invincible_timer, "pc_invincible_timer");
  11494. add_timer_func_list(pc_eventtimer, "pc_eventtimer");
  11495. add_timer_func_list(pc_inventory_rental_end, "pc_inventory_rental_end");
  11496. add_timer_func_list(pc_calc_pvprank_timer, "pc_calc_pvprank_timer");
  11497. add_timer_func_list(pc_autosave, "pc_autosave");
  11498. add_timer_func_list(pc_spiritball_timer, "pc_spiritball_timer");
  11499. add_timer_func_list(pc_follow_timer, "pc_follow_timer");
  11500. add_timer_func_list(pc_endautobonus, "pc_endautobonus");
  11501. add_timer_func_list(pc_spiritcharm_timer, "pc_spiritcharm_timer");
  11502. add_timer_func_list(pc_global_expiration_timer, "pc_global_expiration_timer");
  11503. add_timer_func_list(pc_expiration_timer, "pc_expiration_timer");
  11504. add_timer_func_list(pc_autotrade_timer, "pc_autotrade_timer");
  11505. add_timer(gettick() + autosave_interval, pc_autosave, 0, 0);
  11506. // 0=day, 1=night [Yor]
  11507. night_flag = battle_config.night_at_start ? 1 : 0;
  11508. if (battle_config.day_duration > 0 && battle_config.night_duration > 0) {
  11509. int day_duration = battle_config.day_duration;
  11510. int night_duration = battle_config.night_duration;
  11511. // add night/day timer [Yor]
  11512. add_timer_func_list(map_day_timer, "map_day_timer");
  11513. add_timer_func_list(map_night_timer, "map_night_timer");
  11514. day_timer_tid = add_timer_interval(gettick() + (night_flag ? 0 : day_duration) + night_duration, map_day_timer, 0, 0, day_duration + night_duration);
  11515. night_timer_tid = add_timer_interval(gettick() + day_duration + (night_flag ? night_duration : 0), map_night_timer, 0, 0, day_duration + night_duration);
  11516. }
  11517. do_init_pc_groups();
  11518. pc_sc_display_ers = ers_new(sizeof(struct sc_display_entry), "pc.cpp:pc_sc_display_ers", ERS_OPT_FLEX_CHUNK);
  11519. num_reg_ers = ers_new(sizeof(struct script_reg_num), "pc.cpp:num_reg_ers", (ERSOptions)(ERS_OPT_CLEAN|ERS_OPT_FLEX_CHUNK));
  11520. str_reg_ers = ers_new(sizeof(struct script_reg_str), "pc.cpp:str_reg_ers", (ERSOptions)(ERS_OPT_CLEAN|ERS_OPT_FLEX_CHUNK));
  11521. ers_chunk_size(pc_sc_display_ers, 150);
  11522. ers_chunk_size(num_reg_ers, 300);
  11523. ers_chunk_size(str_reg_ers, 50);
  11524. }