pc.cpp 407 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287
  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. if (itemdb_ishatched_egg(item))
  610. return false;
  611. switch (shoptype) {
  612. case NPCTYPE_SHOP:
  613. if (item->bound && battle_config.allow_bound_sell&ISR_BOUND_SELLABLE && (
  614. item->bound != BOUND_GUILD ||
  615. (sd->guild && sd->status.char_id == sd->guild->member[0].char_id) ||
  616. (item->bound == BOUND_GUILD && !(battle_config.allow_bound_sell&ISR_BOUND_GUILDLEADER_ONLY))
  617. ))
  618. return true;
  619. break;
  620. case NPCTYPE_ITEMSHOP:
  621. if (item->bound && battle_config.allow_bound_sell&ISR_BOUND && (
  622. item->bound != BOUND_GUILD ||
  623. (sd->guild && sd->status.char_id == sd->guild->member[0].char_id) ||
  624. (item->bound == BOUND_GUILD && !(battle_config.allow_bound_sell&ISR_BOUND_GUILDLEADER_ONLY))
  625. ))
  626. return true;
  627. else if (!item->bound) {
  628. struct item_data *itd = itemdb_search(item->nameid);
  629. if (itd && itd->flag.trade_restriction&8 && battle_config.allow_bound_sell&ISR_SELLABLE)
  630. return true;
  631. }
  632. break;
  633. }
  634. if (!itemdb_cansell(item, pc_get_group_level(sd)))
  635. return false;
  636. if (item->bound && !pc_can_give_bounded_items(sd))
  637. return false; // Don't allow sale of bound items
  638. return true;
  639. }
  640. /**
  641. * Determines if player can give / drop / trade / vend items
  642. */
  643. bool pc_can_give_items(struct map_session_data *sd)
  644. {
  645. return pc_has_permission(sd, PC_PERM_TRADE);
  646. }
  647. /**
  648. * Determines if player can give / drop / trade / vend bounded items
  649. */
  650. bool pc_can_give_bounded_items(struct map_session_data *sd)
  651. {
  652. return pc_has_permission(sd, PC_PERM_TRADE_BOUNDED);
  653. }
  654. /*==========================================
  655. * Prepares character for saving.
  656. * @param sd
  657. *------------------------------------------*/
  658. void pc_makesavestatus(struct map_session_data *sd) {
  659. nullpo_retv(sd);
  660. if(!battle_config.save_clothcolor)
  661. sd->status.clothes_color = 0;
  662. // Since this is currently not officially released,
  663. // its best to have a forced option to not save body styles.
  664. if(!battle_config.save_body_style)
  665. sd->status.body = 0;
  666. //Only copy the Cart/Peco/Falcon options, the rest are handled via
  667. //status change load/saving. [Skotlex]
  668. #ifdef NEW_CARTS
  669. sd->status.option = sd->sc.option&(OPTION_INVISIBLE|OPTION_FALCON|OPTION_RIDING|OPTION_DRAGON|OPTION_WUG|OPTION_WUGRIDER|OPTION_MADOGEAR);
  670. #else
  671. sd->status.option = sd->sc.option&(OPTION_INVISIBLE|OPTION_CART|OPTION_FALCON|OPTION_RIDING|OPTION_DRAGON|OPTION_WUG|OPTION_WUGRIDER|OPTION_MADOGEAR);
  672. #endif
  673. if (sd->sc.data[SC_JAILED]) { //When Jailed, do not move last point.
  674. if(pc_isdead(sd)){
  675. pc_setrestartvalue(sd, 0);
  676. } else {
  677. sd->status.hp = sd->battle_status.hp;
  678. sd->status.sp = sd->battle_status.sp;
  679. }
  680. sd->status.last_point.map = sd->mapindex;
  681. sd->status.last_point.x = sd->bl.x;
  682. sd->status.last_point.y = sd->bl.y;
  683. return;
  684. }
  685. if(pc_isdead(sd)) {
  686. pc_setrestartvalue(sd, 0);
  687. memcpy(&sd->status.last_point,&sd->status.save_point,sizeof(sd->status.last_point));
  688. } else {
  689. sd->status.hp = sd->battle_status.hp;
  690. sd->status.sp = sd->battle_status.sp;
  691. sd->status.last_point.map = sd->mapindex;
  692. sd->status.last_point.x = sd->bl.x;
  693. sd->status.last_point.y = sd->bl.y;
  694. }
  695. if(map_getmapflag(sd->bl.m, MF_NOSAVE)) {
  696. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  697. if(mapdata->save.map)
  698. memcpy(&sd->status.last_point,&mapdata->save,sizeof(sd->status.last_point));
  699. else
  700. memcpy(&sd->status.last_point,&sd->status.save_point,sizeof(sd->status.last_point));
  701. }
  702. }
  703. /*==========================================
  704. * Off init ? Connection?
  705. *------------------------------------------*/
  706. 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) {
  707. nullpo_retv(sd);
  708. sd->bl.id = account_id;
  709. sd->status.account_id = account_id;
  710. sd->status.char_id = char_id;
  711. sd->status.sex = sex;
  712. sd->login_id1 = login_id1;
  713. sd->login_id2 = 0; // at this point, we can not know the value :(
  714. sd->client_tick = client_tick;
  715. sd->state.active = 0; //to be set to 1 after player is fully authed and loaded.
  716. sd->bl.type = BL_PC;
  717. if(battle_config.prevent_logout_trigger&PLT_LOGIN)
  718. sd->canlog_tick = gettick();
  719. //Required to prevent homunculus copuing a base speed of 0.
  720. sd->battle_status.speed = sd->base_status.speed = DEFAULT_WALK_SPEED;
  721. }
  722. /**
  723. * Get equip point for an equip
  724. * @param sd
  725. * @param id
  726. */
  727. int pc_equippoint_sub(struct map_session_data *sd,struct item_data* id){
  728. int ep = 0;
  729. nullpo_ret(sd);
  730. nullpo_ret(id);
  731. if (!itemdb_isequip2(id))
  732. return 0; //Not equippable by players.
  733. ep = id->equip;
  734. if(id->look == W_DAGGER || id->look == W_1HSWORD || id->look == W_1HAXE) {
  735. 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]
  736. if (ep == EQP_WEAPON)
  737. return EQP_ARMS;
  738. if (ep == EQP_SHADOW_WEAPON)
  739. return EQP_SHADOW_ARMS;
  740. }
  741. }
  742. return ep;
  743. }
  744. /**
  745. * Get equip point for an equip
  746. * @param sd
  747. * @param n Equip index in inventory
  748. */
  749. int pc_equippoint(struct map_session_data *sd,int n){
  750. nullpo_ret(sd);
  751. return pc_equippoint_sub(sd,sd->inventory_data[n]);
  752. }
  753. /**
  754. * Fill inventory_data with struct *item_data through inventory (fill with struct *item)
  755. * @param sd : player session
  756. * @return 0 sucess, 1:invalid sd
  757. */
  758. void pc_setinventorydata(struct map_session_data *sd)
  759. {
  760. uint8 i;
  761. nullpo_retv(sd);
  762. for(i = 0; i < MAX_INVENTORY; i++) {
  763. unsigned short id = sd->inventory.u.items_inventory[i].nameid;
  764. sd->inventory_data[i] = id?itemdb_search(id):NULL;
  765. }
  766. }
  767. /**
  768. * 'Calculates' weapon type
  769. * @param sd : Player
  770. */
  771. void pc_calcweapontype(struct map_session_data *sd)
  772. {
  773. nullpo_retv(sd);
  774. // single-hand
  775. if(sd->weapontype2 == W_FIST) {
  776. sd->status.weapon = sd->weapontype1;
  777. return;
  778. }
  779. if(sd->weapontype1 == W_FIST) {
  780. sd->status.weapon = sd->weapontype2;
  781. return;
  782. }
  783. // dual-wield
  784. sd->status.weapon = 0;
  785. switch (sd->weapontype1){
  786. case W_DAGGER:
  787. switch (sd->weapontype2) {
  788. case W_DAGGER: sd->status.weapon = W_DOUBLE_DD; break;
  789. case W_1HSWORD: sd->status.weapon = W_DOUBLE_DS; break;
  790. case W_1HAXE: sd->status.weapon = W_DOUBLE_DA; break;
  791. }
  792. break;
  793. case W_1HSWORD:
  794. switch (sd->weapontype2) {
  795. case W_DAGGER: sd->status.weapon = W_DOUBLE_DS; break;
  796. case W_1HSWORD: sd->status.weapon = W_DOUBLE_SS; break;
  797. case W_1HAXE: sd->status.weapon = W_DOUBLE_SA; break;
  798. }
  799. break;
  800. case W_1HAXE:
  801. switch (sd->weapontype2) {
  802. case W_DAGGER: sd->status.weapon = W_DOUBLE_DA; break;
  803. case W_1HSWORD: sd->status.weapon = W_DOUBLE_SA; break;
  804. case W_1HAXE: sd->status.weapon = W_DOUBLE_AA; break;
  805. }
  806. }
  807. // unknown, default to right hand type
  808. if (!sd->status.weapon)
  809. sd->status.weapon = sd->weapontype1;
  810. }
  811. /**
  812. * Set equip index
  813. * @param sd : Player
  814. */
  815. void pc_setequipindex(struct map_session_data *sd)
  816. {
  817. uint16 i;
  818. nullpo_retv(sd);
  819. for (i = 0; i < EQI_MAX; i++){
  820. sd->equip_index[i] = -1;
  821. sd->equip_switch_index[i] = -1;
  822. }
  823. for (i = 0; i < MAX_INVENTORY; i++) {
  824. if (sd->inventory.u.items_inventory[i].nameid <= 0)
  825. continue;
  826. if (sd->inventory.u.items_inventory[i].equip) {
  827. uint8 j;
  828. for (j = 0; j < EQI_MAX; j++)
  829. if (sd->inventory.u.items_inventory[i].equip & equip_bitmask[j])
  830. sd->equip_index[j] = i;
  831. if (sd->inventory.u.items_inventory[i].equip & EQP_HAND_R) {
  832. if (sd->inventory_data[i])
  833. sd->weapontype1 = sd->inventory_data[i]->look;
  834. else
  835. sd->weapontype1 = 0;
  836. }
  837. if( sd->inventory.u.items_inventory[i].equip & EQP_HAND_L ) {
  838. if( sd->inventory_data[i] && sd->inventory_data[i]->type == IT_WEAPON )
  839. sd->weapontype2 = sd->inventory_data[i]->look;
  840. else
  841. sd->weapontype2 = 0;
  842. }
  843. }
  844. if (sd->inventory.u.items_inventory[i].equipSwitch) {
  845. for (uint8 j = 0; j < EQI_MAX; j++) {
  846. if (sd->inventory.u.items_inventory[i].equipSwitch & equip_bitmask[j]) {
  847. sd->equip_switch_index[j] = i;
  848. }
  849. }
  850. }
  851. }
  852. pc_calcweapontype(sd);
  853. }
  854. //static int pc_isAllowedCardOn(struct map_session_data *sd,int s,int eqindex,int flag)
  855. //{
  856. // int i;
  857. // struct item *item = &sd->inventory.u.items_inventory[eqindex];
  858. // struct item_data *data;
  859. //
  860. // //Crafted/made/hatched items.
  861. // if (itemdb_isspecial(item->card[0]))
  862. // return 1;
  863. //
  864. // /* scan for enchant armor gems */
  865. // if( item->card[MAX_SLOTS - 1] && s < MAX_SLOTS - 1 )
  866. // s = MAX_SLOTS - 1;
  867. //
  868. // ARR_FIND( 0, s, i, item->card[i] && (data = itemdb_exists(item->card[i])) != NULL && data->flag.no_equip&flag );
  869. // return( i < s ) ? 0 : 1;
  870. //}
  871. /**
  872. * Check if an item is equiped by player
  873. * (Check if the itemid is equiped then search if that match the index in inventory (should be))
  874. * @param sd : player session
  875. * @param nameid : itemid
  876. * @return 1:yes, 0:no
  877. */
  878. bool pc_isequipped(struct map_session_data *sd, unsigned short nameid)
  879. {
  880. uint8 i;
  881. for( i = 0; i < EQI_MAX; i++ )
  882. {
  883. short index = sd->equip_index[i], j;
  884. if( index < 0 )
  885. continue;
  886. if( pc_is_same_equip_index((enum equip_index)i, sd->equip_index, index) )
  887. continue;
  888. if( !sd->inventory_data[index] )
  889. continue;
  890. if( sd->inventory_data[index]->nameid == nameid )
  891. return true;
  892. for( j = 0; j < sd->inventory_data[index]->slot; j++ ){
  893. if( sd->inventory.u.items_inventory[index].card[j] == nameid )
  894. return true;
  895. }
  896. }
  897. return false;
  898. }
  899. /**
  900. * Check adoption rules
  901. * @param p1_sd: Player 1
  902. * @param p2_sd: Player 2
  903. * @param b_sd: Player that will be adopted
  904. * @return ADOPT_ALLOWED - Sent message to Baby to accept or deny
  905. * ADOPT_ALREADY_ADOPTED - Already adopted
  906. * ADOPT_MARRIED_AND_PARTY - Need to be married and in the same party
  907. * ADOPT_EQUIP_RINGS - Need wedding rings equipped
  908. * ADOPT_NOT_NOVICE - Adoptee is not a Novice
  909. * ADOPT_CHARACTER_NOT_FOUND - Parent or Baby not found
  910. * ADOPT_MORE_CHILDREN - Cannot adopt more than 1 Baby (client message)
  911. * ADOPT_LEVEL_70 - Parents need to be level 70+ (client message)
  912. * ADOPT_MARRIED - Cannot adopt a married person (client message)
  913. */
  914. enum adopt_responses pc_try_adopt(struct map_session_data *p1_sd, struct map_session_data *p2_sd, struct map_session_data *b_sd)
  915. {
  916. if( !p1_sd || !p2_sd || !b_sd )
  917. return ADOPT_CHARACTER_NOT_FOUND;
  918. if( b_sd->status.father || b_sd->status.mother || b_sd->adopt_invite )
  919. return ADOPT_ALREADY_ADOPTED; // already adopted baby / in adopt request
  920. if( !p1_sd->status.partner_id || !p1_sd->status.party_id || p1_sd->status.party_id != b_sd->status.party_id )
  921. return ADOPT_MARRIED_AND_PARTY; // You need to be married and in party with baby to adopt
  922. if( p1_sd->status.partner_id != p2_sd->status.char_id || p2_sd->status.partner_id != p1_sd->status.char_id )
  923. return ADOPT_MARRIED_AND_PARTY; // Not married, wrong married
  924. if( p2_sd->status.party_id != p1_sd->status.party_id )
  925. return ADOPT_MARRIED_AND_PARTY; // Both parents need to be in the same party
  926. // Parents need to have their ring equipped
  927. if( !pc_isequipped(p1_sd, WEDDING_RING_M) && !pc_isequipped(p1_sd, WEDDING_RING_F) )
  928. return ADOPT_EQUIP_RINGS;
  929. if( !pc_isequipped(p2_sd, WEDDING_RING_M) && !pc_isequipped(p2_sd, WEDDING_RING_F) )
  930. return ADOPT_EQUIP_RINGS;
  931. // Already adopted a baby
  932. if( p1_sd->status.child || p2_sd->status.child ) {
  933. clif_Adopt_reply(p1_sd, ADOPT_REPLY_MORE_CHILDREN);
  934. return ADOPT_MORE_CHILDREN;
  935. }
  936. // Parents need at least lvl 70 to adopt
  937. if( p1_sd->status.base_level < 70 || p2_sd->status.base_level < 70 ) {
  938. clif_Adopt_reply(p1_sd, ADOPT_REPLY_LEVEL_70);
  939. return ADOPT_LEVEL_70;
  940. }
  941. if( b_sd->status.partner_id ) {
  942. clif_Adopt_reply(p1_sd, ADOPT_REPLY_MARRIED);
  943. return ADOPT_MARRIED;
  944. }
  945. 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 ) )
  946. return ADOPT_NOT_NOVICE;
  947. return ADOPT_ALLOWED;
  948. }
  949. /*==========================================
  950. * Adoption Process
  951. *------------------------------------------*/
  952. bool pc_adoption(struct map_session_data *p1_sd, struct map_session_data *p2_sd, struct map_session_data *b_sd)
  953. {
  954. int job, joblevel;
  955. unsigned int jobexp;
  956. if( pc_try_adopt(p1_sd, p2_sd, b_sd) != ADOPT_ALLOWED )
  957. return false;
  958. // Preserve current job levels and progress
  959. joblevel = b_sd->status.job_level;
  960. jobexp = b_sd->status.job_exp;
  961. job = pc_mapid2jobid(b_sd->class_|JOBL_BABY, b_sd->status.sex);
  962. if( job != -1 && pc_jobchange(b_sd, job, 0) )
  963. { // Success, proceed to configure parents and baby skills
  964. p1_sd->status.child = b_sd->status.char_id;
  965. p2_sd->status.child = b_sd->status.char_id;
  966. b_sd->status.father = p1_sd->status.char_id;
  967. b_sd->status.mother = p2_sd->status.char_id;
  968. // Restore progress
  969. b_sd->status.job_level = joblevel;
  970. clif_updatestatus(b_sd, SP_JOBLEVEL);
  971. b_sd->status.job_exp = jobexp;
  972. clif_updatestatus(b_sd, SP_JOBEXP);
  973. // Baby Skills
  974. pc_skill(b_sd, WE_BABY, 1, ADDSKILL_PERMANENT);
  975. pc_skill(b_sd, WE_CALLPARENT, 1, ADDSKILL_PERMANENT);
  976. pc_skill(b_sd, WE_CHEERUP, 1, ADDSKILL_PERMANENT);
  977. // Parents Skills
  978. pc_skill(p1_sd, WE_CALLBABY, 1, ADDSKILL_PERMANENT);
  979. pc_skill(p2_sd, WE_CALLBABY, 1, ADDSKILL_PERMANENT);
  980. chrif_save(p1_sd, CSAVE_NORMAL);
  981. chrif_save(p2_sd, CSAVE_NORMAL);
  982. chrif_save(b_sd, CSAVE_NORMAL);
  983. achievement_update_objective(b_sd, AG_BABY, 1, 1);
  984. achievement_update_objective(p1_sd, AG_BABY, 1, 2);
  985. achievement_update_objective(p2_sd, AG_BABY, 1, 2);
  986. return true;
  987. }
  988. return false; // Job Change Fail
  989. }
  990. /*==========================================
  991. * Check if player can use/equip selected item. Used by pc_isUseitem and pc_isequip
  992. Returns:
  993. false : Cannot use/equip
  994. true : Can use/equip
  995. * Credits:
  996. [Inkfish] for first idea
  997. [Haru] for third-classes extension
  998. [Cydh] finishing :D
  999. *------------------------------------------*/
  1000. static bool pc_isItemClass (struct map_session_data *sd, struct item_data* item) {
  1001. while (1) {
  1002. if (item->class_upper&ITEMJ_NORMAL && !(sd->class_&(JOBL_UPPER|JOBL_THIRD|JOBL_BABY))) //normal classes (no upper, no baby, no third)
  1003. break;
  1004. #ifndef RENEWAL
  1005. //allow third classes to use trans. class items
  1006. if (item->class_upper&ITEMJ_UPPER && sd->class_&(JOBL_UPPER|JOBL_THIRD)) //trans. classes
  1007. break;
  1008. //third-baby classes can use same item too
  1009. if (item->class_upper&ITEMJ_BABY && sd->class_&JOBL_BABY) //baby classes
  1010. break;
  1011. //don't need to decide specific rules for third-classes?
  1012. //items for third classes can be used for all third classes
  1013. if (item->class_upper&(ITEMJ_THIRD|ITEMJ_THIRD_TRANS|ITEMJ_THIRD_BABY) && sd->class_&JOBL_THIRD)
  1014. break;
  1015. #else
  1016. //trans. classes (exl. third-trans.)
  1017. if (item->class_upper&ITEMJ_UPPER && sd->class_&JOBL_UPPER && !(sd->class_&JOBL_THIRD))
  1018. break;
  1019. //baby classes (exl. third-baby)
  1020. if (item->class_upper&ITEMJ_BABY && sd->class_&JOBL_BABY && !(sd->class_&JOBL_THIRD))
  1021. break;
  1022. //third classes (exl. third-trans. and baby-third)
  1023. if (item->class_upper&ITEMJ_THIRD && sd->class_&JOBL_THIRD && !(sd->class_&(JOBL_UPPER|JOBL_BABY)))
  1024. break;
  1025. //trans-third classes
  1026. if (item->class_upper&ITEMJ_THIRD_TRANS && sd->class_&JOBL_THIRD && sd->class_&JOBL_UPPER)
  1027. break;
  1028. //third-baby classes
  1029. if (item->class_upper&ITEMJ_THIRD_BABY && sd->class_&JOBL_THIRD && sd->class_&JOBL_BABY)
  1030. break;
  1031. #endif
  1032. return false;
  1033. }
  1034. return true;
  1035. }
  1036. /*=================================================
  1037. * Checks if the player can equip the item at index n in inventory.
  1038. * @param sd
  1039. * @param n Item index in inventory
  1040. * @return ITEM_EQUIP_ACK_OK(0) if can be equipped, or ITEM_EQUIP_ACK_FAIL(1)/ITEM_EQUIP_ACK_FAILLEVEL(2) if can't
  1041. *------------------------------------------------*/
  1042. uint8 pc_isequip(struct map_session_data *sd,int n)
  1043. {
  1044. struct item_data *item;
  1045. nullpo_retr(ITEM_EQUIP_ACK_FAIL, sd);
  1046. item = sd->inventory_data[n];
  1047. if(pc_has_permission(sd, PC_PERM_USE_ALL_EQUIPMENT))
  1048. return ITEM_EQUIP_ACK_OK;
  1049. if(item == NULL)
  1050. return ITEM_EQUIP_ACK_FAIL;
  1051. if(item->elv && sd->status.base_level < (unsigned int)item->elv)
  1052. return ITEM_EQUIP_ACK_FAILLEVEL;
  1053. if(item->elvmax && sd->status.base_level > (unsigned int)item->elvmax)
  1054. return ITEM_EQUIP_ACK_FAILLEVEL;
  1055. if(item->sex != 2 && sd->status.sex != item->sex)
  1056. return ITEM_EQUIP_ACK_FAIL;
  1057. //fail to equip if item is restricted
  1058. if (!battle_config.allow_equip_restricted_item && itemdb_isNoEquip(item, sd->bl.m))
  1059. return ITEM_EQUIP_ACK_FAIL;
  1060. if (item->equip&EQP_AMMO) {
  1061. switch (item->look) {
  1062. case AMMO_ARROW:
  1063. if (battle_config.ammo_check_weapon && sd->status.weapon != W_BOW && sd->status.weapon != W_MUSICAL && sd->status.weapon != W_WHIP) {
  1064. clif_msg(sd, ITEM_NEED_BOW);
  1065. return ITEM_EQUIP_ACK_FAIL;
  1066. }
  1067. break;
  1068. case AMMO_THROWABLE_DAGGER:
  1069. if (!pc_checkskill(sd, AS_VENOMKNIFE))
  1070. return ITEM_EQUIP_ACK_FAIL;
  1071. break;
  1072. case AMMO_BULLET:
  1073. case AMMO_SHELL:
  1074. 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
  1075. #ifdef RENEWAL
  1076. && sd->status.weapon != W_GRENADE
  1077. #endif
  1078. ) {
  1079. clif_msg(sd, ITEM_BULLET_EQUIP_FAIL);
  1080. return ITEM_EQUIP_ACK_FAIL;
  1081. }
  1082. break;
  1083. #ifndef RENEWAL
  1084. case AMMO_GRENADE:
  1085. if (battle_config.ammo_check_weapon && sd->status.weapon != W_GRENADE) {
  1086. clif_msg(sd, ITEM_BULLET_EQUIP_FAIL);
  1087. return ITEM_EQUIP_ACK_FAIL;
  1088. }
  1089. break;
  1090. #endif
  1091. case AMMO_CANNONBALL:
  1092. if (!pc_ismadogear(sd) && (sd->status.class_ == JOB_MECHANIC_T || sd->status.class_ == JOB_MECHANIC)) {
  1093. clif_msg(sd, ITEM_NEED_MADOGEAR); // Item can only be used when Mado Gear is mounted.
  1094. return ITEM_EQUIP_ACK_FAIL;
  1095. }
  1096. if (sd->state.active && !pc_iscarton(sd) && //Check if sc data is already loaded
  1097. (sd->status.class_ == JOB_GENETIC_T || sd->status.class_ == JOB_GENETIC)) {
  1098. clif_msg(sd, ITEM_NEED_CART); // Only available when cart is mounted.
  1099. return ITEM_EQUIP_ACK_FAIL;
  1100. }
  1101. break;
  1102. }
  1103. }
  1104. if (sd->sc.count) {
  1105. if(item->equip & EQP_ARMS && item->type == IT_WEAPON && sd->sc.data[SC_STRIPWEAPON]) // Also works with left-hand weapons [DracoRPG]
  1106. return ITEM_EQUIP_ACK_FAIL;
  1107. if(item->equip & EQP_SHIELD && item->type == IT_ARMOR && sd->sc.data[SC_STRIPSHIELD])
  1108. return ITEM_EQUIP_ACK_FAIL;
  1109. if(item->equip & EQP_ARMOR && sd->sc.data[SC_STRIPARMOR])
  1110. return ITEM_EQUIP_ACK_FAIL;
  1111. if(item->equip & EQP_HEAD_TOP && sd->sc.data[SC_STRIPHELM])
  1112. return ITEM_EQUIP_ACK_FAIL;
  1113. if(item->equip & EQP_ACC && sd->sc.data[SC__STRIPACCESSORY])
  1114. return ITEM_EQUIP_ACK_FAIL;
  1115. if (item->equip & EQP_ARMS && sd->sc.data[SC__WEAKNESS])
  1116. return ITEM_EQUIP_ACK_FAIL;
  1117. if(item->equip && (sd->sc.data[SC_KYOUGAKU] || sd->sc.data[SC_SUHIDE]))
  1118. return ITEM_EQUIP_ACK_FAIL;
  1119. if (sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_SUPERNOVICE) {
  1120. //Spirit of Super Novice equip bonuses. [Skotlex]
  1121. if (sd->status.base_level > 90 && item->equip & EQP_HELM)
  1122. return ITEM_EQUIP_ACK_OK; //Can equip all helms
  1123. if (sd->status.base_level > 96 && item->equip & EQP_ARMS && item->type == IT_WEAPON && item->wlv == 4)
  1124. switch(item->look) { //In weapons, the look determines type of weapon.
  1125. case W_DAGGER: //All level 4 - Daggers
  1126. case W_1HSWORD: //All level 4 - 1H Swords
  1127. case W_1HAXE: //All level 4 - 1H Axes
  1128. case W_MACE: //All level 4 - 1H Maces
  1129. case W_STAFF: //All level 4 - 1H Staves
  1130. case W_2HSTAFF: //All level 4 - 2H Staves
  1131. return ITEM_EQUIP_ACK_OK;
  1132. }
  1133. }
  1134. }
  1135. //Not equipable by class. [Skotlex]
  1136. if (!(1ULL << (sd->class_&MAPID_BASEMASK)&item->class_base[(sd->class_&JOBL_2_1) ? 1 : ((sd->class_&JOBL_2_2) ? 2 : 0)]))
  1137. return ITEM_EQUIP_ACK_FAIL;
  1138. if (!pc_isItemClass(sd, item))
  1139. return ITEM_EQUIP_ACK_FAIL;
  1140. return ITEM_EQUIP_ACK_OK;
  1141. }
  1142. /*==========================================
  1143. * No problem with the session id
  1144. * set the status that has been sent from char server
  1145. *------------------------------------------*/
  1146. 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)
  1147. {
  1148. int i;
  1149. t_tick tick = gettick();
  1150. uint32 ip = session[sd->fd]->client_addr;
  1151. sd->login_id2 = login_id2;
  1152. sd->group_id = group_id;
  1153. /* load user permissions */
  1154. pc_group_pc_load(sd);
  1155. memcpy(&sd->status, st, sizeof(*st));
  1156. if (st->sex != sd->status.sex) {
  1157. clif_authfail_fd(sd->fd, 0);
  1158. return false;
  1159. }
  1160. //Set the map-server used job id. [Skotlex]
  1161. i = pc_jobid2mapid(sd->status.class_);
  1162. if (i == -1) { //Invalid class?
  1163. 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);
  1164. sd->status.class_ = JOB_NOVICE;
  1165. sd->class_ = MAPID_NOVICE;
  1166. } else
  1167. sd->class_ = i;
  1168. // Checks and fixes to character status data, that are required
  1169. // in case of configuration change or stuff, which cannot be
  1170. // checked on char-server.
  1171. sd->status.hair = cap_value(sd->status.hair,MIN_HAIR_STYLE,MAX_HAIR_STYLE);
  1172. sd->status.hair_color = cap_value(sd->status.hair_color,MIN_HAIR_COLOR,MAX_HAIR_COLOR);
  1173. sd->status.clothes_color = cap_value(sd->status.clothes_color,MIN_CLOTH_COLOR,MAX_CLOTH_COLOR);
  1174. sd->status.body = cap_value(sd->status.body,MIN_BODY_STYLE,MAX_BODY_STYLE);
  1175. //Initializations to null/0 unneeded since map_session_data was filled with 0 upon allocation.
  1176. sd->state.connect_new = 1;
  1177. sd->followtimer = INVALID_TIMER; // [MouseJstr]
  1178. sd->invincible_timer = INVALID_TIMER;
  1179. sd->npc_timer_id = INVALID_TIMER;
  1180. sd->pvp_timer = INVALID_TIMER;
  1181. sd->expiration_tid = INVALID_TIMER;
  1182. sd->autotrade_tid = INVALID_TIMER;
  1183. sd->respawn_tid = INVALID_TIMER;
  1184. #ifdef SECURE_NPCTIMEOUT
  1185. // Initialize to defaults/expected
  1186. sd->npc_idle_timer = INVALID_TIMER;
  1187. sd->npc_idle_tick = tick;
  1188. sd->npc_idle_type = NPCT_INPUT;
  1189. sd->state.ignoretimeout = false;
  1190. #endif
  1191. sd->canuseitem_tick = tick;
  1192. sd->canusecashfood_tick = tick;
  1193. sd->canequip_tick = tick;
  1194. sd->cantalk_tick = tick;
  1195. sd->canskill_tick = tick;
  1196. sd->cansendmail_tick = tick;
  1197. sd->idletime = last_tick;
  1198. for(i = 0; i < MAX_SPIRITBALL; i++)
  1199. sd->spirit_timer[i] = INVALID_TIMER;
  1200. if (battle_config.item_auto_get)
  1201. sd->state.autoloot = 10000;
  1202. if (battle_config.disp_experience)
  1203. sd->state.showexp = 1;
  1204. if (battle_config.disp_zeny)
  1205. sd->state.showzeny = 1;
  1206. #ifdef VIP_ENABLE
  1207. if (!battle_config.vip_disp_rate)
  1208. sd->vip.disableshowrate = 1;
  1209. #endif
  1210. if (!(battle_config.display_skill_fail&2))
  1211. sd->state.showdelay = 1;
  1212. memset(&sd->inventory, 0, sizeof(struct s_storage));
  1213. memset(&sd->cart, 0, sizeof(struct s_storage));
  1214. memset(&sd->storage, 0, sizeof(struct s_storage));
  1215. memset(&sd->premiumStorage, 0, sizeof(struct s_storage));
  1216. memset(&sd->equip_index, -1, sizeof(sd->equip_index));
  1217. memset(&sd->equip_switch_index, -1, sizeof(sd->equip_switch_index));
  1218. if( pc_isinvisible(sd) && !pc_can_use_command( sd, "hide", COMMAND_ATCOMMAND ) ){
  1219. sd->status.option &= ~OPTION_INVISIBLE;
  1220. }
  1221. status_change_init(&sd->bl);
  1222. sd->sc.option = sd->status.option; //This is the actual option used in battle.
  1223. unit_dataset(&sd->bl);
  1224. sd->guild_x = -1;
  1225. sd->guild_y = -1;
  1226. sd->delayed_damage = 0;
  1227. // Event Timers
  1228. for( i = 0; i < MAX_EVENTTIMER; i++ )
  1229. sd->eventtimer[i] = INVALID_TIMER;
  1230. // Rental Timer
  1231. sd->rental_timer = INVALID_TIMER;
  1232. for( i = 0; i < 3; i++ )
  1233. sd->hate_mob[i] = -1;
  1234. sd->quest_log = NULL;
  1235. sd->num_quests = 0;
  1236. sd->avail_quests = 0;
  1237. sd->save_quest = false;
  1238. sd->count_rewarp = 0;
  1239. sd->regs.vars = i64db_alloc(DB_OPT_BASE);
  1240. sd->regs.arrays = NULL;
  1241. sd->vars_dirty = false;
  1242. sd->vars_ok = false;
  1243. sd->vars_received = 0x0;
  1244. sd->qi_display = nullptr;
  1245. sd->qi_count = 0;
  1246. //warp player
  1247. if ((i=pc_setpos(sd,sd->status.last_point.map, sd->status.last_point.x, sd->status.last_point.y, CLR_OUTSIGHT)) != SETPOS_OK) {
  1248. 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);
  1249. // try warping to a default map instead (church graveyard)
  1250. if (pc_setpos(sd, mapindex_name2id(MAP_PRONTERA), 273, 354, CLR_OUTSIGHT) != SETPOS_OK) {
  1251. // if we fail again
  1252. clif_authfail_fd(sd->fd, 0);
  1253. return false;
  1254. }
  1255. }
  1256. clif_authok(sd);
  1257. //Prevent S. Novices from getting the no-death bonus just yet. [Skotlex]
  1258. sd->die_counter=-1;
  1259. //display login notice
  1260. ShowInfo("'" CL_WHITE "%s" CL_RESET "' logged in."
  1261. " (AID/CID: '" CL_WHITE "%d/%d" CL_RESET "',"
  1262. " IP: '" CL_WHITE "%d.%d.%d.%d" CL_RESET "',"
  1263. " Group '" CL_WHITE "%d" CL_RESET "').\n",
  1264. sd->status.name, sd->status.account_id, sd->status.char_id,
  1265. CONVIP(ip), sd->group_id);
  1266. // Send friends list
  1267. clif_friendslist_send(sd);
  1268. if( !changing_mapservers ) {
  1269. if (battle_config.display_version == 1)
  1270. pc_show_version(sd);
  1271. // Message of the Day [Valaris]
  1272. for(i=0; i < MOTD_LINE_SIZE && motd_text[i][0]; i++) {
  1273. if (battle_config.motd_type)
  1274. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], motd_text[i], false, SELF);
  1275. else
  1276. clif_displaymessage(sd->fd, motd_text[i]);
  1277. }
  1278. if (expiration_time != 0)
  1279. sd->expiration_time = expiration_time;
  1280. /**
  1281. * Fixes login-without-aura glitch (the screen won't blink at this point, don't worry :P)
  1282. **/
  1283. clif_changemap(sd,sd->bl.m,sd->bl.x,sd->bl.y);
  1284. }
  1285. pc_validate_skill(sd);
  1286. /* [Ind] */
  1287. sd->sc_display = NULL;
  1288. sd->sc_display_count = 0;
  1289. // Player has not yet received the CashShop list
  1290. sd->status.cashshop_sent = false;
  1291. sd->last_addeditem_index = -1;
  1292. sd->bonus_script.head = NULL;
  1293. sd->bonus_script.count = 0;
  1294. #if PACKETVER >= 20150513
  1295. sd->hatEffectIDs = NULL;
  1296. sd->hatEffectCount = 0;
  1297. #endif
  1298. sd->catch_target_class = PET_CATCH_FAIL;
  1299. // Check EXP overflow, since in previous revision EXP on Max Level can be more than 'official' Max EXP
  1300. if (pc_is_maxbaselv(sd) && sd->status.base_exp > MAX_LEVEL_BASE_EXP) {
  1301. sd->status.base_exp = MAX_LEVEL_BASE_EXP;
  1302. clif_updatestatus(sd, SP_BASEEXP);
  1303. }
  1304. if (pc_is_maxjoblv(sd) && sd->status.job_exp > MAX_LEVEL_JOB_EXP) {
  1305. sd->status.job_exp = MAX_LEVEL_JOB_EXP;
  1306. clif_updatestatus(sd, SP_JOBEXP);
  1307. }
  1308. // Request all registries (auth is considered completed whence they arrive)
  1309. intif_request_registry(sd,7);
  1310. return true;
  1311. }
  1312. /*==========================================
  1313. * Closes a connection because it failed to be authenticated from the char server.
  1314. *------------------------------------------*/
  1315. void pc_authfail(struct map_session_data *sd)
  1316. {
  1317. clif_authfail_fd(sd->fd, 0);
  1318. return;
  1319. }
  1320. /**
  1321. * Player register a bl as hatred
  1322. * @param sd : player session
  1323. * @param pos : hate position [0;2]
  1324. * @param bl : target bl
  1325. * @return false:failed, true:success
  1326. */
  1327. bool pc_set_hate_mob(struct map_session_data *sd, int pos, struct block_list *bl)
  1328. {
  1329. int class_;
  1330. if (!sd || !bl || pos < 0 || pos > 2)
  1331. return false;
  1332. if (sd->hate_mob[pos] != -1)
  1333. { //Can't change hate targets.
  1334. clif_hate_info(sd, pos, sd->hate_mob[pos], 0); //Display current
  1335. return false;
  1336. }
  1337. class_ = status_get_class(bl);
  1338. if (!pcdb_checkid(class_)) {
  1339. unsigned int max_hp = status_get_max_hp(bl);
  1340. if ((pos == 1 && max_hp < 6000) || (pos == 2 && max_hp < 20000))
  1341. return false;
  1342. if (pos != status_get_size(bl))
  1343. return false; //Wrong size
  1344. }
  1345. sd->hate_mob[pos] = class_;
  1346. pc_setglobalreg(sd, add_str(sg_info[pos].hate_var), class_+1);
  1347. clif_hate_info(sd, pos, class_, 1);
  1348. return true;
  1349. }
  1350. /*==========================================
  1351. * Invoked once after the char/account/account2 registry variables are received. [Skotlex]
  1352. * We didn't receive item information at this point so DO NOT attempt to do item operations here.
  1353. * See intif_parse_StorageReceived() for item operations [lighta]
  1354. *------------------------------------------*/
  1355. void pc_reg_received(struct map_session_data *sd)
  1356. {
  1357. uint8 i;
  1358. sd->vars_ok = true;
  1359. sd->change_level_2nd = static_cast<unsigned char>(pc_readglobalreg(sd, add_str(JOBCHANGE2ND_VAR)));
  1360. sd->change_level_3rd = static_cast<unsigned char>(pc_readglobalreg(sd, add_str(JOBCHANGE3RD_VAR)));
  1361. sd->die_counter = static_cast<int>(pc_readglobalreg(sd, add_str(PCDIECOUNTER_VAR)));
  1362. sd->langtype = static_cast<int>(pc_readaccountreg(sd, add_str(LANGTYPE_VAR)));
  1363. if (msg_checklangtype(sd->langtype,true) < 0)
  1364. sd->langtype = 0; //invalid langtype reset to default
  1365. // Cash shop
  1366. sd->cashPoints = static_cast<int>(pc_readaccountreg(sd, add_str(CASHPOINT_VAR)));
  1367. sd->kafraPoints = static_cast<int>(pc_readaccountreg(sd, add_str(KAFRAPOINT_VAR)));
  1368. // Cooking Exp
  1369. sd->cook_mastery = static_cast<short>(pc_readglobalreg(sd, add_str(COOKMASTERY_VAR)));
  1370. if( (sd->class_&MAPID_BASEMASK) == MAPID_TAEKWON )
  1371. { // Better check for class rather than skill to prevent "skill resets" from unsetting this
  1372. sd->mission_mobid = static_cast<short>(pc_readglobalreg(sd, add_str(TKMISSIONID_VAR)));
  1373. sd->mission_count = static_cast<unsigned char>(pc_readglobalreg(sd, add_str(TKMISSIONCOUNT_VAR)));
  1374. }
  1375. if (battle_config.feature_banking)
  1376. sd->bank_vault = static_cast<int>(pc_readreg2(sd, BANK_VAULT_VAR));
  1377. if (battle_config.feature_roulette) {
  1378. sd->roulette_point.bronze = static_cast<int>(pc_readreg2(sd, ROULETTE_BRONZE_VAR));
  1379. sd->roulette_point.silver = static_cast<int>(pc_readreg2(sd, ROULETTE_SILVER_VAR));
  1380. sd->roulette_point.gold = static_cast<int>(pc_readreg2(sd, ROULETTE_GOLD_VAR));
  1381. }
  1382. sd->roulette.prizeIdx = -1;
  1383. //SG map and mob read [Komurka]
  1384. for(i=0;i<MAX_PC_FEELHATE;i++) { //for now - someone need to make reading from txt/sql
  1385. uint16 j;
  1386. if ((j = static_cast<uint16>(pc_readglobalreg(sd, add_str(sg_info[i].feel_var)))) != 0) {
  1387. sd->feel_map[i].index = j;
  1388. sd->feel_map[i].m = map_mapindex2mapid(j);
  1389. } else {
  1390. sd->feel_map[i].index = 0;
  1391. sd->feel_map[i].m = -1;
  1392. }
  1393. sd->hate_mob[i] = static_cast<short>(pc_readglobalreg(sd, add_str(sg_info[i].hate_var)))-1;
  1394. }
  1395. if ((i = pc_checkskill(sd,RG_PLAGIARISM)) > 0) {
  1396. unsigned short skid = static_cast<unsigned short>(pc_readglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM)));
  1397. sd->cloneskill_idx = skill_get_index(skid);
  1398. if (sd->cloneskill_idx > 0) {
  1399. sd->status.skill[sd->cloneskill_idx].id = skid;
  1400. sd->status.skill[sd->cloneskill_idx].lv = static_cast<uint8>(pc_readglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM_LV)));
  1401. if (sd->status.skill[sd->cloneskill_idx].lv > i)
  1402. sd->status.skill[sd->cloneskill_idx].lv = i;
  1403. sd->status.skill[sd->cloneskill_idx].flag = SKILL_FLAG_PLAGIARIZED;
  1404. }
  1405. }
  1406. if ((i = pc_checkskill(sd,SC_REPRODUCE)) > 0) {
  1407. unsigned short skid = static_cast<unsigned short>(pc_readglobalreg(sd, add_str(SKILL_VAR_REPRODUCE)));
  1408. sd->reproduceskill_idx = skill_get_index(skid);
  1409. if (sd->reproduceskill_idx > 0) {
  1410. sd->status.skill[sd->reproduceskill_idx].id = skid;
  1411. sd->status.skill[sd->reproduceskill_idx].lv = static_cast<uint8>(pc_readglobalreg(sd, add_str(SKILL_VAR_REPRODUCE_LV)));
  1412. if (i < sd->status.skill[sd->reproduceskill_idx].lv)
  1413. sd->status.skill[sd->reproduceskill_idx].lv = i;
  1414. sd->status.skill[sd->reproduceskill_idx].flag = SKILL_FLAG_PLAGIARIZED;
  1415. }
  1416. }
  1417. //Weird... maybe registries were reloaded?
  1418. if (sd->state.active)
  1419. return;
  1420. sd->state.active = 1;
  1421. sd->state.pc_loaded = false; // Ensure inventory data and status data is loaded before we calculate player stats
  1422. intif_storage_request(sd,TABLE_STORAGE, 0, STOR_MODE_ALL); // Request storage data
  1423. intif_storage_request(sd,TABLE_CART, 0, STOR_MODE_ALL); // Request cart data
  1424. intif_storage_request(sd,TABLE_INVENTORY, 0, STOR_MODE_ALL); // Request inventory data
  1425. if (sd->status.party_id)
  1426. party_member_joined(sd);
  1427. if (sd->status.guild_id)
  1428. guild_member_joined(sd);
  1429. if( sd->status.clan_id )
  1430. clan_member_joined(sd);
  1431. // pet
  1432. if (sd->status.pet_id > 0)
  1433. intif_request_petdata(sd->status.account_id, sd->status.char_id, sd->status.pet_id);
  1434. // Homunculus [albator]
  1435. if( sd->status.hom_id > 0 )
  1436. intif_homunculus_requestload(sd->status.account_id, sd->status.hom_id);
  1437. if( sd->status.mer_id > 0 )
  1438. intif_mercenary_request(sd->status.mer_id, sd->status.char_id);
  1439. if( sd->status.ele_id > 0 )
  1440. intif_elemental_request(sd->status.ele_id, sd->status.char_id);
  1441. map_addiddb(&sd->bl);
  1442. map_delnickdb(sd->status.char_id, sd->status.name);
  1443. if (!chrif_auth_finished(sd))
  1444. ShowError("pc_reg_received: Failed to properly remove player %d:%d from logging db!\n", sd->status.account_id, sd->status.char_id);
  1445. chrif_skillcooldown_request(sd->status.account_id, sd->status.char_id);
  1446. chrif_bsdata_request(sd->status.char_id);
  1447. #ifdef VIP_ENABLE
  1448. sd->vip.time = 0;
  1449. sd->vip.enabled = 0;
  1450. chrif_req_login_operation(sd->status.account_id, sd->status.name, CHRIF_OP_LOGIN_VIP, 0, 1|8, 0); // request VIP information
  1451. #endif
  1452. intif_Mail_requestinbox(sd->status.char_id, 0, MAIL_INBOX_NORMAL); // MAIL SYSTEM - Request Mail Inbox
  1453. intif_request_questlog(sd);
  1454. if (battle_config.feature_achievement) {
  1455. sd->achievement_data.total_score = 0;
  1456. sd->achievement_data.level = 0;
  1457. sd->achievement_data.save = false;
  1458. sd->achievement_data.count = 0;
  1459. sd->achievement_data.incompleteCount = 0;
  1460. sd->achievement_data.achievements = NULL;
  1461. intif_request_achievements(sd->status.char_id);
  1462. }
  1463. if (sd->state.connect_new == 0 && sd->fd) { //Character already loaded map! Gotta trigger LoadEndAck manually.
  1464. sd->state.connect_new = 1;
  1465. clif_parse_LoadEndAck(sd->fd, sd);
  1466. if( pc_isinvisible( sd ) ){
  1467. sd->vd.class_ = JT_INVISIBLE;
  1468. clif_displaymessage( sd->fd, msg_txt( sd, 11 ) ); // Invisible: On
  1469. // decrement the number of pvp players on the map
  1470. map_getmapdata( sd->bl.m )->users_pvp--;
  1471. if( map_getmapflag( sd->bl.m, MF_PVP ) && !map_getmapflag( sd->bl.m, MF_PVP_NOCALCRANK ) && sd->pvp_timer != INVALID_TIMER ){
  1472. // unregister the player for ranking
  1473. delete_timer( sd->pvp_timer, pc_calc_pvprank_timer );
  1474. sd->pvp_timer = INVALID_TIMER;
  1475. }
  1476. clif_changeoption( &sd->bl );
  1477. }
  1478. }
  1479. channel_autojoin(sd);
  1480. }
  1481. static int pc_calc_skillpoint(struct map_session_data* sd)
  1482. {
  1483. uint16 i, skill_point = 0;
  1484. nullpo_ret(sd);
  1485. for(i = 1; i < MAX_SKILL; i++) {
  1486. if( sd->status.skill[i].id && sd->status.skill[i].lv > 0) {
  1487. std::shared_ptr<s_skill_db> skill = skill_db.find(sd->status.skill[i].id);
  1488. if ((!skill->inf2[INF2_ISQUEST] || battle_config.quest_skill_learn) &&
  1489. (!skill->inf2[INF2_ISWEDDING] || skill->inf2[INF2_ISSPIRIT]) //Do not count wedding/link skills. [Skotlex]
  1490. )
  1491. {
  1492. if(sd->status.skill[i].flag == SKILL_FLAG_PERMANENT)
  1493. skill_point += sd->status.skill[i].lv;
  1494. else if(sd->status.skill[i].flag >= SKILL_FLAG_REPLACED_LV_0)
  1495. skill_point += (sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0);
  1496. }
  1497. }
  1498. }
  1499. return skill_point;
  1500. }
  1501. static bool pc_grant_allskills(struct map_session_data *sd, bool addlv) {
  1502. if (!sd || !pc_has_permission(sd, PC_PERM_ALL_SKILL))
  1503. return false;
  1504. /**
  1505. * Dummy skills must NOT be added here otherwise they'll be displayed in the,
  1506. * skill tree and since they have no icons they'll give resource errors
  1507. * Get ALL skills except npc/guild ones. [Skotlex]
  1508. * Don't add SG_DEVIL [Komurka] and MO_TRIPLEATTACK and RG_SNATCHER [ultramage]
  1509. **/
  1510. for (const auto &skill : skill_db) {
  1511. uint16 skill_id = skill.second->nameid;
  1512. if (skill_id == 0 || skill.second->inf2[INF2_ISNPC]|| skill.second->inf2[INF2_ISGUILD])
  1513. continue;
  1514. switch (skill_id) {
  1515. case SM_SELFPROVOKE:
  1516. case AB_DUPLELIGHT_MELEE:
  1517. case AB_DUPLELIGHT_MAGIC:
  1518. case WL_CHAINLIGHTNING_ATK:
  1519. case WL_TETRAVORTEX_FIRE:
  1520. case WL_TETRAVORTEX_WATER:
  1521. case WL_TETRAVORTEX_WIND:
  1522. case WL_TETRAVORTEX_GROUND:
  1523. case WL_SUMMON_ATK_FIRE:
  1524. case WL_SUMMON_ATK_WIND:
  1525. case WL_SUMMON_ATK_WATER:
  1526. case WL_SUMMON_ATK_GROUND:
  1527. case LG_OVERBRAND_BRANDISH:
  1528. case LG_OVERBRAND_PLUSATK:
  1529. case WM_SEVERE_RAINSTORM_MELEE:
  1530. case RL_R_TRIP_PLUSATK:
  1531. case SG_DEVIL:
  1532. case MO_TRIPLEATTACK:
  1533. case RG_SNATCHER:
  1534. continue;
  1535. default:
  1536. {
  1537. uint8 lv = (uint8)skill_get_max(skill_id);
  1538. if (lv > 0) {
  1539. uint16 idx = skill_get_index(skill_id);
  1540. sd->status.skill[idx].id = skill_id;
  1541. if (addlv)
  1542. sd->status.skill[idx].lv = lv;
  1543. }
  1544. }
  1545. break;
  1546. }
  1547. }
  1548. return true;
  1549. }
  1550. /*==========================================
  1551. * Calculation of skill level.
  1552. * @param sd
  1553. *------------------------------------------*/
  1554. void pc_calc_skilltree(struct map_session_data *sd)
  1555. {
  1556. nullpo_retv(sd);
  1557. int job = pc_calc_skilltree_normalize_job(sd);
  1558. int class_ = pc_mapid2jobid(job, sd->status.sex);
  1559. if( class_ == -1 )
  1560. { //Unable to normalize job??
  1561. ShowError("pc_calc_skilltree: Unable to normalize job %d for character %s (%d:%d)\n", job, sd->status.name, sd->status.account_id, sd->status.char_id);
  1562. return;
  1563. }
  1564. class_ = pc_class2idx(class_);
  1565. for (const auto &skill : skill_db) {
  1566. uint16 skill_id = skill.second->nameid;
  1567. uint16 idx = skill_get_index(skill_id);
  1568. if( sd->status.skill[idx].flag != SKILL_FLAG_PLAGIARIZED && sd->status.skill[idx].flag != SKILL_FLAG_PERM_GRANTED ) //Don't touch these
  1569. sd->status.skill[idx].id = 0; //First clear skills.
  1570. /* permanent skills that must be re-checked */
  1571. if( sd->status.skill[idx].flag == SKILL_FLAG_PERM_GRANTED ) {
  1572. if (skill_id == 0) {
  1573. sd->status.skill[idx].id = 0;
  1574. sd->status.skill[idx].lv = 0;
  1575. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  1576. continue;
  1577. }
  1578. switch (skill_id) {
  1579. case NV_TRICKDEAD:
  1580. if( (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE ) {
  1581. sd->status.skill[idx].id = 0;
  1582. sd->status.skill[idx].lv = 0;
  1583. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  1584. }
  1585. break;
  1586. }
  1587. }
  1588. }
  1589. for (const auto &skill : skill_db) {
  1590. uint16 skill_id = skill.second->nameid;
  1591. uint16 idx = skill_get_index(skill_id);
  1592. // Restore original level of skills after deleting earned skills.
  1593. if( sd->status.skill[idx].flag != SKILL_FLAG_PERMANENT && sd->status.skill[idx].flag != SKILL_FLAG_PERM_GRANTED && sd->status.skill[idx].flag != SKILL_FLAG_PLAGIARIZED ) {
  1594. sd->status.skill[idx].lv = (sd->status.skill[idx].flag == SKILL_FLAG_TEMPORARY) ? 0 : sd->status.skill[idx].flag - SKILL_FLAG_REPLACED_LV_0;
  1595. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  1596. }
  1597. //Enable Bard/Dancer spirit linked skills.
  1598. if (skill_id < DC_HUMMING || skill_id > DC_SERVICEFORYOU)
  1599. continue;
  1600. if( sd->sc.count && sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_BARDDANCER ) {
  1601. //Link Dancer skills to bard.
  1602. if( sd->status.sex ) {
  1603. if( sd->status.skill[idx - 8].lv < 10 )
  1604. continue;
  1605. sd->status.skill[idx].id = skill_id;
  1606. sd->status.skill[idx].lv = sd->status.skill[idx - 8].lv; // Set the level to the same as the linking skill
  1607. sd->status.skill[idx].flag = SKILL_FLAG_TEMPORARY; // Tag it as a non-savable, non-uppable, bonus skill
  1608. }
  1609. //Link Bard skills to dancer.
  1610. else {
  1611. if( sd->status.skill[idx].lv < 10 )
  1612. continue;
  1613. sd->status.skill[idx - 8].id = skill_id - 8;
  1614. sd->status.skill[idx - 8].lv = sd->status.skill[idx].lv; // Set the level to the same as the linking skill
  1615. sd->status.skill[idx - 8].flag = SKILL_FLAG_TEMPORARY; // Tag it as a non-savable, non-uppable, bonus skill
  1616. }
  1617. }
  1618. }
  1619. // Removes Taekwon Ranker skill bonus
  1620. if ((sd->class_&MAPID_UPPERMASK) != MAPID_TAEKWON) {
  1621. uint16 c_ = pc_class2idx(JOB_TAEKWON);
  1622. for (uint16 i = 0; i < MAX_SKILL_TREE; i++) {
  1623. uint16 sk_id = skill_tree[c_][i].skill_id;
  1624. uint16 sk_idx = 0;
  1625. if (!sk_id || !(sk_idx = skill_get_index(skill_tree[c_][i].skill_id)))
  1626. continue;
  1627. if (sd->status.skill[sk_idx].flag != SKILL_FLAG_PLAGIARIZED && sd->status.skill[sk_idx].flag != SKILL_FLAG_PERM_GRANTED) {
  1628. if (sk_id == NV_BASIC || sk_id == NV_FIRSTAID || sk_id == WE_CALLBABY)
  1629. continue;
  1630. sd->status.skill[sk_idx].id = 0;
  1631. }
  1632. }
  1633. }
  1634. // Grant all skills
  1635. pc_grant_allskills(sd, false);
  1636. int flag;
  1637. do {
  1638. uint16 skid = 0;
  1639. flag = 0;
  1640. for (uint16 i = 0; i < MAX_SKILL_TREE && (skid = skill_tree[class_][i].skill_id) > 0; i++) {
  1641. bool fail = false;
  1642. uint16 sk_idx = skill_get_index(skid);
  1643. if (sd->status.skill[sk_idx].id)
  1644. continue; //Skill already known.
  1645. if (!battle_config.skillfree) {
  1646. // Checking required skills
  1647. for(uint8 j = 0; j < MAX_PC_SKILL_REQUIRE; j++) {
  1648. uint16 sk_need_id = skill_tree[class_][i].need[j].skill_id;
  1649. uint16 sk_need_idx = 0;
  1650. if (sk_need_id && (sk_need_idx = skill_get_index(sk_need_id))) {
  1651. short sk_need = sk_need_id;
  1652. 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)
  1653. sk_need = 0; //Not learned.
  1654. else if (sd->status.skill[sk_need_idx].flag >= SKILL_FLAG_REPLACED_LV_0) //Real learned level
  1655. sk_need = sd->status.skill[sk_need_idx].flag - SKILL_FLAG_REPLACED_LV_0;
  1656. else
  1657. sk_need = pc_checkskill(sd,sk_need_id);
  1658. if (sk_need < skill_tree[class_][i].need[j].skill_lv) {
  1659. fail = true;
  1660. break;
  1661. }
  1662. }
  1663. }
  1664. if (sd->status.base_level < skill_tree[class_][i].baselv) { //We need to get the actual class in this case
  1665. int c_ = pc_mapid2jobid(sd->class_, sd->status.sex);
  1666. c_ = pc_class2idx(c_);
  1667. if (class_ == c_ || (class_ != c_ && sd->status.base_level < skill_tree[class_][i].baselv))
  1668. fail = true; // base level requirement wasn't satisfied
  1669. }
  1670. if (sd->status.job_level < skill_tree[class_][i].joblv) { //We need to get the actual class in this case
  1671. int c_ = pc_mapid2jobid(sd->class_, sd->status.sex);
  1672. c_ = pc_class2idx(c_);
  1673. if (class_ == c_ || (class_ != c_ && sd->status.job_level < skill_tree[class_][i].joblv))
  1674. fail = true; // job level requirement wasn't satisfied
  1675. }
  1676. }
  1677. if (!fail) {
  1678. std::shared_ptr<s_skill_db> skill = skill_db.find(skid);
  1679. if (!sd->status.skill[sk_idx].lv && (
  1680. (skill->inf2[INF2_ISQUEST] && !battle_config.quest_skill_learn) ||
  1681. skill->inf2[INF2_ISWEDDING] ||
  1682. (skill->inf2[INF2_ISSPIRIT] && !sd->sc.data[SC_SPIRIT])
  1683. ))
  1684. continue; //Cannot be learned via normal means. Note this check DOES allows raising already known skills.
  1685. sd->status.skill[sk_idx].id = skid;
  1686. if(skill->inf2[INF2_ISSPIRIT]) { //Spirit skills cannot be learned, they will only show up on your tree when you get buffed.
  1687. sd->status.skill[sk_idx].lv = 1; // need to manually specify a skill level
  1688. sd->status.skill[sk_idx].flag = SKILL_FLAG_TEMPORARY; //So it is not saved, and tagged as a "bonus" skill.
  1689. }
  1690. flag = 1; // skill list has changed, perform another pass
  1691. }
  1692. }
  1693. } while(flag);
  1694. if( class_ > 0 && sd->status.skill_point == 0 && pc_is_taekwon_ranker(sd) ) {
  1695. uint16 skid = 0;
  1696. /* Taekwon Ranker Bonus Skill Tree
  1697. ============================================
  1698. - Grant All Taekwon Tree, but only as Bonus Skills in case they drop from ranking.
  1699. - (c > 0) to avoid grant Novice Skill Tree in case of Skill Reset (need more logic)
  1700. - (sd->status.skill_point == 0) to wait until all skill points are assigned to avoid problems with Job Change quest. */
  1701. for( uint16 i = 0; i < MAX_SKILL_TREE && (skid = skill_tree[class_][i].skill_id) > 0; i++ ) {
  1702. uint16 sk_idx = skill_get_index(skid);
  1703. if (sk_idx == 0)
  1704. continue;
  1705. if( skill_get_inf2_(skid, { INF2_ISQUEST, INF2_ISWEDDING }) )
  1706. continue; //Do not include Quest/Wedding skills.
  1707. if( sd->status.skill[sk_idx].id == 0 ) {
  1708. sd->status.skill[sk_idx].id = skid;
  1709. sd->status.skill[sk_idx].flag = SKILL_FLAG_TEMPORARY; // So it is not saved, and tagged as a "bonus" skill.
  1710. } else if( skid != NV_BASIC )
  1711. sd->status.skill[sk_idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[sk_idx].lv; // Remember original level
  1712. sd->status.skill[sk_idx].lv = skill_tree_get_max(skid, sd->status.class_);
  1713. }
  1714. }
  1715. }
  1716. //Checks if you can learn a new skill after having leveled up a skill.
  1717. static void pc_check_skilltree(struct map_session_data *sd)
  1718. {
  1719. int i, flag = 0;
  1720. int c = 0;
  1721. if (battle_config.skillfree)
  1722. return; //Function serves no purpose if this is set
  1723. i = pc_calc_skilltree_normalize_job(sd);
  1724. c = pc_mapid2jobid(i, sd->status.sex);
  1725. if (c == -1) { //Unable to normalize job??
  1726. 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);
  1727. return;
  1728. }
  1729. c = pc_class2idx(c);
  1730. do {
  1731. uint16 skid = 0;
  1732. flag = 0;
  1733. for (i = 0; i < MAX_SKILL_TREE && (skid = skill_tree[c][i].skill_id) > 0; i++ ) {
  1734. uint16 sk_idx = skill_get_index(skid);
  1735. bool fail = false;
  1736. uint8 j = 0;
  1737. if (sd->status.skill[sk_idx].id) //Already learned
  1738. continue;
  1739. // Checking required skills
  1740. for (j = 0; j < MAX_PC_SKILL_REQUIRE; j++) {
  1741. uint16 sk_need_id = skill_tree[c][i].need[j].skill_id;
  1742. uint16 sk_need_idx = 0;
  1743. if (sk_need_id && (sk_need_idx = skill_get_index(sk_need_id))) {
  1744. short sk_need = sk_need_id;
  1745. 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)
  1746. sk_need = 0; //Not learned.
  1747. else if (sd->status.skill[sk_need_idx].flag >= SKILL_FLAG_REPLACED_LV_0) //Real lerned level
  1748. sk_need = sd->status.skill[sk_need_idx].flag - SKILL_FLAG_REPLACED_LV_0;
  1749. else
  1750. sk_need = pc_checkskill(sd,sk_need_id);
  1751. if (sk_need < skill_tree[c][i].need[j].skill_lv) {
  1752. fail = true;
  1753. break;
  1754. }
  1755. }
  1756. }
  1757. if( fail )
  1758. continue;
  1759. if (sd->status.base_level < skill_tree[c][i].baselv || sd->status.job_level < skill_tree[c][i].joblv)
  1760. continue;
  1761. std::shared_ptr<s_skill_db> skill = skill_db.find(skid);
  1762. if( !sd->status.skill[sk_idx].lv && (
  1763. (skill->inf2[INF2_ISQUEST] && !battle_config.quest_skill_learn) ||
  1764. skill->inf2[INF2_ISWEDDING] ||
  1765. (skill->inf2[INF2_ISSPIRIT] && !sd->sc.data[SC_SPIRIT])
  1766. ) )
  1767. continue; //Cannot be learned via normal means.
  1768. sd->status.skill[sk_idx].id = skid;
  1769. flag = 1;
  1770. }
  1771. } while(flag);
  1772. }
  1773. // Make sure all the skills are in the correct condition
  1774. // before persisting to the backend.. [MouseJstr]
  1775. void pc_clean_skilltree(struct map_session_data *sd)
  1776. {
  1777. uint16 i;
  1778. for (i = 0; i < MAX_SKILL; i++){
  1779. if (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY || sd->status.skill[i].flag == SKILL_FLAG_PLAGIARIZED) {
  1780. sd->status.skill[i].id = 0;
  1781. sd->status.skill[i].lv = 0;
  1782. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1783. }
  1784. else if (sd->status.skill[i].flag >= SKILL_FLAG_REPLACED_LV_0){
  1785. sd->status.skill[i].lv = sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
  1786. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1787. }
  1788. }
  1789. }
  1790. int pc_calc_skilltree_normalize_job(struct map_session_data *sd)
  1791. {
  1792. int skill_point, novice_skills;
  1793. int c = sd->class_;
  1794. if (!battle_config.skillup_limit || pc_has_permission(sd, PC_PERM_ALL_SKILL))
  1795. return c;
  1796. skill_point = pc_calc_skillpoint(sd);
  1797. novice_skills = job_info[pc_class2idx(JOB_NOVICE)].max_level[1] - 1;
  1798. // limit 1st class and above to novice job levels
  1799. if(skill_point < novice_skills && (sd->class_&MAPID_BASEMASK) != MAPID_SUMMONER)
  1800. {
  1801. c = MAPID_NOVICE;
  1802. }
  1803. // limit 2nd class and above to first class job levels (super novices are exempt)
  1804. else if (sd->class_&JOBL_2 && (sd->class_&MAPID_UPPERMASK) != MAPID_SUPER_NOVICE)
  1805. {
  1806. // regenerate change_level_2nd
  1807. if (!sd->change_level_2nd)
  1808. {
  1809. if (sd->class_&JOBL_THIRD)
  1810. {
  1811. // if neither 2nd nor 3rd jobchange levels are known, we have to assume a default for 2nd
  1812. if (!sd->change_level_3rd)
  1813. sd->change_level_2nd = job_info[pc_class2idx(pc_mapid2jobid(sd->class_&MAPID_UPPERMASK, sd->status.sex))].max_level[1];
  1814. else
  1815. sd->change_level_2nd = 1 + skill_point + sd->status.skill_point
  1816. - (sd->status.job_level - 1)
  1817. - (sd->change_level_3rd - 1)
  1818. - novice_skills;
  1819. }
  1820. else
  1821. {
  1822. sd->change_level_2nd = 1 + skill_point + sd->status.skill_point
  1823. - (sd->status.job_level - 1)
  1824. - novice_skills;
  1825. }
  1826. pc_setglobalreg(sd, add_str(JOBCHANGE2ND_VAR), sd->change_level_2nd);
  1827. }
  1828. if (skill_point < novice_skills + (sd->change_level_2nd - 1))
  1829. {
  1830. c &= MAPID_BASEMASK;
  1831. }
  1832. // limit 3rd class to 2nd class/trans job levels
  1833. else if(sd->class_&JOBL_THIRD)
  1834. {
  1835. // regenerate change_level_3rd
  1836. if (!sd->change_level_3rd)
  1837. {
  1838. sd->change_level_3rd = 1 + skill_point + sd->status.skill_point
  1839. - (sd->status.job_level - 1)
  1840. - (sd->change_level_2nd - 1)
  1841. - novice_skills;
  1842. pc_setglobalreg(sd, add_str(JOBCHANGE3RD_VAR), sd->change_level_3rd);
  1843. }
  1844. if (skill_point < novice_skills + (sd->change_level_2nd - 1) + (sd->change_level_3rd - 1))
  1845. c &= MAPID_UPPERMASK;
  1846. }
  1847. }
  1848. // restore non-limiting flags
  1849. c |= sd->class_&(JOBL_UPPER|JOBL_BABY);
  1850. return c;
  1851. }
  1852. /*==========================================
  1853. * Updates the weight status
  1854. *------------------------------------------
  1855. * 1: overweight 50% for pre-renewal and 70% for renewal
  1856. * 2: overweight 90%
  1857. * It's assumed that SC_WEIGHT50 and SC_WEIGHT90 are only started/stopped here.
  1858. */
  1859. void pc_updateweightstatus(struct map_session_data *sd)
  1860. {
  1861. int old_overweight;
  1862. int new_overweight;
  1863. nullpo_retv(sd);
  1864. old_overweight = (sd->sc.data[SC_WEIGHT90]) ? 2 : (sd->sc.data[SC_WEIGHT50]) ? 1 : 0;
  1865. #ifdef RENEWAL
  1866. new_overweight = (pc_is90overweight(sd)) ? 2 : (pc_is70overweight(sd)) ? 1 : 0;
  1867. #else
  1868. new_overweight = (pc_is90overweight(sd)) ? 2 : (pc_is50overweight(sd)) ? 1 : 0;
  1869. #endif
  1870. if( old_overweight == new_overweight )
  1871. return; // no change
  1872. // stop old status change
  1873. if( old_overweight == 1 )
  1874. status_change_end(&sd->bl, SC_WEIGHT50, INVALID_TIMER);
  1875. else if( old_overweight == 2 )
  1876. status_change_end(&sd->bl, SC_WEIGHT90, INVALID_TIMER);
  1877. // start new status change
  1878. if( new_overweight == 1 )
  1879. sc_start(&sd->bl,&sd->bl, SC_WEIGHT50, 100, 0, 0);
  1880. else if( new_overweight == 2 )
  1881. sc_start(&sd->bl,&sd->bl, SC_WEIGHT90, 100, 0, 0);
  1882. // update overweight status
  1883. sd->regen.state.overweight = new_overweight;
  1884. }
  1885. int pc_disguise(struct map_session_data *sd, int class_)
  1886. {
  1887. if (!class_ && !sd->disguise)
  1888. return 0;
  1889. if (class_ && sd->disguise == class_)
  1890. return 0;
  1891. if(pc_isinvisible(sd))
  1892. { //Character is invisible. Stealth class-change. [Skotlex]
  1893. sd->disguise = class_; //viewdata is set on uncloaking.
  1894. return 2;
  1895. }
  1896. if (sd->bl.prev != NULL) {
  1897. pc_stop_walking(sd, 0);
  1898. clif_clearunit_area(&sd->bl, CLR_OUTSIGHT);
  1899. }
  1900. if (!class_) {
  1901. sd->disguise = 0;
  1902. class_ = sd->status.class_;
  1903. } else
  1904. sd->disguise=class_;
  1905. status_set_viewdata(&sd->bl, class_);
  1906. clif_changeoption(&sd->bl);
  1907. if (sd->bl.prev != NULL) {
  1908. clif_spawn(&sd->bl);
  1909. if (class_ == sd->status.class_ && pc_iscarton(sd))
  1910. { //It seems the cart info is lost on undisguise.
  1911. clif_cartlist(sd);
  1912. clif_updatestatus(sd,SP_CARTINFO);
  1913. }
  1914. if (sd->chatID) {
  1915. struct chat_data* cd;
  1916. if ((cd = (struct chat_data*)map_id2bl(sd->chatID)) != NULL)
  1917. clif_dispchat(cd,0);
  1918. }
  1919. }
  1920. return 1;
  1921. }
  1922. /// Show error message
  1923. #define PC_BONUS_SHOW_ERROR(type,type2,val) { ShowError("%s: %s: Invalid %s %d.\n",__FUNCTION__,#type,#type2,(val)); break; }
  1924. /// Check for valid Element, break & show error message if invalid Element
  1925. #define PC_BONUS_CHK_ELEMENT(ele,bonus) { if (!CHK_ELEMENT((ele))) { PC_BONUS_SHOW_ERROR((bonus),Element,(ele)); }}
  1926. /// Check for valid Race, break & show error message if invalid Race
  1927. #define PC_BONUS_CHK_RACE(rc,bonus) { if (!CHK_RACE((rc))) { PC_BONUS_SHOW_ERROR((bonus),Race,(rc)); }}
  1928. /// Check for valid Race2, break & show error message if invalid Race2
  1929. #define PC_BONUS_CHK_RACE2(rc2,bonus) { if (!CHK_RACE2((rc2))) { PC_BONUS_SHOW_ERROR((bonus),Race2,(rc2)); }}
  1930. /// Check for valid Class, break & show error message if invalid Class
  1931. #define PC_BONUS_CHK_CLASS(cl,bonus) { if (!CHK_CLASS((cl))) { PC_BONUS_SHOW_ERROR((bonus),Class,(cl)); }}
  1932. /// Check for valid Size, break & show error message if invalid Size
  1933. #define PC_BONUS_CHK_SIZE(sz,bonus) { if (!CHK_MOBSIZE((sz))) { PC_BONUS_SHOW_ERROR((bonus),Size,(sz)); }}
  1934. /// Check for valid SC, break & show error message if invalid SC
  1935. #define PC_BONUS_CHK_SC(sc,bonus) { if ((sc) <= SC_NONE || (sc) >= SC_MAX) { PC_BONUS_SHOW_ERROR((bonus),Effect,(sc)); }}
  1936. /**
  1937. * Add auto spell bonus for player while attacking/attacked
  1938. * @param spell: Spell array
  1939. * @param id: Skill to cast
  1940. * @param lv: Skill level
  1941. * @param rate: Success chance
  1942. * @param flag: Battle flag
  1943. * @param card_id: Used to prevent card stacking
  1944. */
  1945. static void pc_bonus_autospell(std::vector<s_autospell> &spell, short id, short lv, short rate, short flag, unsigned short card_id)
  1946. {
  1947. if (spell.size() == MAX_PC_BONUS) {
  1948. ShowWarning("pc_bonus_autospell: Reached max (%d) number of autospells per character!\n", MAX_PC_BONUS);
  1949. return;
  1950. }
  1951. if (!rate)
  1952. return;
  1953. if (!(flag&BF_RANGEMASK))
  1954. flag |= BF_SHORT | BF_LONG; //No range defined? Use both.
  1955. if (!(flag&BF_WEAPONMASK))
  1956. flag |= BF_WEAPON; //No attack type defined? Use weapon.
  1957. if (!(flag&BF_SKILLMASK)) {
  1958. if (flag&(BF_MAGIC | BF_MISC))
  1959. flag |= BF_SKILL; //These two would never trigger without BF_SKILL
  1960. if (flag&BF_WEAPON)
  1961. flag |= BF_NORMAL; //By default autospells should only trigger on normal weapon attacks.
  1962. }
  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. if (!battle_config.autospell_stacking && it.rate > 0 && rate > 0) // Stacking disabled
  1966. return;
  1967. it.rate = cap_value(it.rate + rate, -10000, 10000);
  1968. return;
  1969. }
  1970. }
  1971. struct s_autospell entry = {};
  1972. if (rate < -10000 || rate > 10000)
  1973. ShowWarning("pc_bonus_autospell: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  1974. entry.id = id;
  1975. entry.lv = lv;
  1976. entry.rate = cap_value(rate, -10000, 10000);
  1977. entry.flag = flag;
  1978. entry.card_id = card_id;
  1979. spell.push_back(entry);
  1980. }
  1981. /**
  1982. * Add auto spell bonus for player while using skills
  1983. * @param spell: Spell array
  1984. * @param src_skill: Trigger skill
  1985. * @param id: Support or target type
  1986. * @param lv: Skill level
  1987. * @param rate: Success chance
  1988. * @param card_id: Used to prevent card stacking
  1989. */
  1990. static void pc_bonus_autospell_onskill(std::vector<s_autospell> &spell, short src_skill, short id, short lv, short rate, unsigned short card_id)
  1991. {
  1992. if (spell.size() == MAX_PC_BONUS) {
  1993. ShowWarning("pc_bonus_autospell_onskill: Reached max (%d) number of autospells per character!\n", MAX_PC_BONUS);
  1994. return;
  1995. }
  1996. if (!rate)
  1997. return;
  1998. struct s_autospell entry = {};
  1999. if (rate < -10000 || rate > 10000)
  2000. ShowWarning("pc_bonus_onskill: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2001. entry.flag = src_skill;
  2002. entry.id = id;
  2003. entry.lv = lv;
  2004. entry.rate = cap_value(rate, -10000, 10000);
  2005. entry.card_id = card_id;
  2006. spell.push_back(entry);
  2007. }
  2008. /**
  2009. * Add inflict effect bonus for player while attacking/attacked
  2010. * @param effect: Effect array
  2011. * @param sc: SC/Effect type
  2012. * @param rate: Success chance
  2013. * @param arrow_rate: success chance if bonus comes from arrow-type item
  2014. * @param flag: Target flag
  2015. * @param duration: Duration. If 0 use default duration lookup for associated skill with level 7
  2016. */
  2017. 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)
  2018. {
  2019. if (effect.size() == MAX_PC_BONUS) {
  2020. ShowWarning("pc_bonus_addeff: Reached max (%d) number of add effects per character!\n", MAX_PC_BONUS);
  2021. return;
  2022. }
  2023. if (!(flag&(ATF_SHORT | ATF_LONG)))
  2024. flag |= ATF_SHORT | ATF_LONG; //Default range: both
  2025. if (!(flag&(ATF_TARGET | ATF_SELF)))
  2026. flag |= ATF_TARGET; //Default target: enemy.
  2027. if (!(flag&(ATF_WEAPON | ATF_MAGIC | ATF_MISC)))
  2028. flag |= ATF_WEAPON; //Default type: weapon.
  2029. if (!duration)
  2030. duration = (unsigned int)skill_get_time2(status_sc2skill(sc), 7);
  2031. for (auto &it : effect) {
  2032. if (it.sc == sc && it.flag == flag) {
  2033. it.rate = cap_value(it.rate + rate, -10000, 10000);
  2034. it.arrow_rate += arrow_rate;
  2035. it.duration = umax(it.duration, duration);
  2036. return;
  2037. }
  2038. }
  2039. struct s_addeffect entry = {};
  2040. if (rate < -10000 || rate > 10000)
  2041. ShowWarning("pc_bonus_addeff: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2042. entry.sc = sc;
  2043. entry.rate = cap_value(rate, -10000, 10000);
  2044. entry.arrow_rate = arrow_rate;
  2045. entry.flag = flag;
  2046. entry.duration = duration;
  2047. effect.push_back(entry);
  2048. }
  2049. /**
  2050. * Add inflict effect bonus for player while attacking using skill
  2051. * @param effect: Effect array
  2052. * @param sc: SC/Effect type
  2053. * @param rate: Success chance
  2054. * @param skill_id: Skill to cast
  2055. * @param target: Target type
  2056. * @param duration: Duration. If 0 use default duration lookup for associated skill with level 7
  2057. */
  2058. 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)
  2059. {
  2060. if (effect.size() == MAX_PC_BONUS) {
  2061. ShowWarning("pc_bonus_addeff_onskill: Reached max (%d) number of add effects per character!\n", MAX_PC_BONUS);
  2062. return;
  2063. }
  2064. if (!duration)
  2065. duration = (unsigned int)skill_get_time2(status_sc2skill(sc), 7);
  2066. for (auto &it : effect) {
  2067. if (it.sc == sc && it.skill_id == skill_id && it.target == target) {
  2068. it.rate = cap_value(it.rate + rate, -10000, 10000);
  2069. it.duration = umax(it.duration, duration);
  2070. return;
  2071. }
  2072. }
  2073. struct s_addeffectonskill entry = {};
  2074. if (rate < -10000 || rate > 10000)
  2075. ShowWarning("pc_bonus_addeff_onskill: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2076. entry.sc = sc;
  2077. entry.rate = cap_value(rate, -10000, 10000);
  2078. entry.skill_id = skill_id;
  2079. entry.target = target;
  2080. entry.duration = duration;
  2081. effect.push_back(entry);
  2082. }
  2083. /**
  2084. * Adjust/add drop rate modifier for player
  2085. * @param drop: Player's sd->add_drop (struct s_add_drop)
  2086. * @param nameid: item id that will be dropped
  2087. * @param group: group id
  2088. * @param class_: target class
  2089. * @param race: target race. if < 0, means monster_id
  2090. * @param rate: rate value: 1 ~ 10000. If < 0, it will be multiplied with mob level/10
  2091. */
  2092. static void pc_bonus_item_drop(std::vector<s_add_drop> &drop, unsigned short nameid, uint16 group, int class_, short race, int rate)
  2093. {
  2094. if (!nameid && !group) {
  2095. ShowWarning("pc_bonus_item_drop: No Item ID nor Item Group ID specified.\n");
  2096. return;
  2097. }
  2098. if (nameid && !itemdb_exists(nameid)) {
  2099. ShowWarning("pc_bonus_item_drop: Invalid item id %hu\n",nameid);
  2100. return;
  2101. }
  2102. if (group && !itemdb_group_exists(group)) {
  2103. ShowWarning("pc_bonus_item_drop: Invalid Item Group %hu\n",group);
  2104. return;
  2105. }
  2106. if (drop.size() == MAX_PC_BONUS) {
  2107. 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);
  2108. return;
  2109. }
  2110. //Apply config rate adjustment settings.
  2111. if (rate >= 0) { //Absolute drop.
  2112. if (battle_config.item_rate_adddrop != 100)
  2113. rate = rate*battle_config.item_rate_adddrop/100;
  2114. if (rate < battle_config.item_drop_adddrop_min)
  2115. rate = battle_config.item_drop_adddrop_min;
  2116. else if (rate > battle_config.item_drop_adddrop_max)
  2117. rate = battle_config.item_drop_adddrop_max;
  2118. } else { //Relative drop, max/min limits are applied at drop time.
  2119. if (battle_config.item_rate_adddrop != 100)
  2120. rate = rate*battle_config.item_rate_adddrop/100;
  2121. if (rate > -1)
  2122. rate = -1;
  2123. }
  2124. for (auto &it : drop) {
  2125. if (it.nameid == nameid && it.group == group && it.race == race && it.class_ == class_) {
  2126. if ((rate < 0 && it.rate < 0) || (rate > 0 && it.rate > 0)) //Adjust the rate if it has same classification
  2127. it.rate = cap_value(it.rate + rate, -10000, 10000);
  2128. return;
  2129. }
  2130. }
  2131. struct s_add_drop entry = {};
  2132. if (rate < -10000 || rate > 10000)
  2133. ShowWarning("pc_bonus_item_drop: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2134. entry.nameid = nameid;
  2135. entry.group = group;
  2136. entry.race = race;
  2137. entry.class_ = class_;
  2138. entry.rate = cap_value(rate, -10000, 10000);
  2139. drop.push_back(entry);
  2140. }
  2141. /**
  2142. * Add autobonus to player when attacking/attacked
  2143. * @param bonus: Bonus array
  2144. * @param script: Script to execute
  2145. * @param rate: Success chance
  2146. * @param dur: Duration
  2147. * @param flag: Battle flag/skill
  2148. * @param other_script: Secondary script to execute
  2149. * @param pos: Item equip position
  2150. * @param onskill: Skill used to trigger autobonus
  2151. * @return True on success or false otherwise
  2152. */
  2153. 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)
  2154. {
  2155. if (bonus.size() == MAX_PC_BONUS) {
  2156. ShowWarning("pc_addautobonus: Reached max (%d) number of autobonus per character!\n", MAX_PC_BONUS);
  2157. return false;
  2158. }
  2159. if (!onskill) {
  2160. if (!(flag&BF_RANGEMASK))
  2161. flag |= BF_SHORT | BF_LONG; //No range defined? Use both.
  2162. if (!(flag&BF_WEAPONMASK))
  2163. flag |= BF_WEAPON; //No attack type defined? Use weapon.
  2164. if (!(flag&BF_SKILLMASK)) {
  2165. if (flag&(BF_MAGIC | BF_MISC))
  2166. flag |= BF_SKILL; //These two would never trigger without BF_SKILL
  2167. if (flag&BF_WEAPON)
  2168. flag |= BF_NORMAL | BF_SKILL;
  2169. }
  2170. }
  2171. struct s_autobonus entry = {};
  2172. if (rate < -10000 || rate > 10000)
  2173. ShowWarning("pc_addautobonus: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2174. entry.rate = cap_value(rate, -10000, 10000);
  2175. entry.duration = dur;
  2176. entry.active = INVALID_TIMER;
  2177. entry.atk_type = flag;
  2178. entry.pos = pos;
  2179. entry.bonus_script = aStrdup(script);
  2180. entry.other_script = (other_script ? aStrdup(other_script) : NULL);
  2181. bonus.push_back(entry);
  2182. return true;
  2183. }
  2184. /**
  2185. * Remove an autobonus from player
  2186. * @param sd: Player data
  2187. * @param bonus: Autobonus array
  2188. * @param restore: Run script on clearing or not
  2189. */
  2190. void pc_delautobonus(struct map_session_data* sd, std::vector<s_autobonus> &bonus, bool restore)
  2191. {
  2192. nullpo_retv(sd);
  2193. std::vector<s_autobonus>::iterator it = bonus.begin();
  2194. while( it != bonus.end() ){
  2195. s_autobonus b = *it;
  2196. if (b.active != INVALID_TIMER) {
  2197. if (restore && (sd->state.autobonus&b.pos) == b.pos) {
  2198. if (b.bonus_script) {
  2199. unsigned int equip_pos_idx = 0;
  2200. // Create a list of all equipped positions to see if all items needed for the autobonus are still present [Playtester]
  2201. for (uint8 j = 0; j < EQI_MAX; j++) {
  2202. if (sd->equip_index[j] >= 0)
  2203. equip_pos_idx |= sd->inventory.u.items_inventory[sd->equip_index[j]].equip;
  2204. }
  2205. if ((equip_pos_idx&b.pos) == b.pos)
  2206. script_run_autobonus(b.bonus_script, sd, b.pos);
  2207. }
  2208. it++;
  2209. continue;
  2210. } else { // Logout / Unequipped an item with an activated bonus
  2211. delete_timer(b.active, pc_endautobonus);
  2212. b.active = INVALID_TIMER;
  2213. }
  2214. }
  2215. if (b.bonus_script)
  2216. aFree(b.bonus_script);
  2217. if (b.other_script)
  2218. aFree(b.other_script);
  2219. it = bonus.erase(it);
  2220. }
  2221. }
  2222. /**
  2223. * Execute autobonus on player
  2224. * @param sd: Player data
  2225. * @param autobonus: Autobonus to run
  2226. */
  2227. void pc_exeautobonus(struct map_session_data *sd, std::vector<s_autobonus> *bonus, struct s_autobonus *autobonus)
  2228. {
  2229. nullpo_retv(sd);
  2230. nullpo_retv(autobonus);
  2231. if (autobonus->active != INVALID_TIMER)
  2232. delete_timer(autobonus->active, pc_endautobonus);
  2233. if( autobonus->other_script )
  2234. {
  2235. int j;
  2236. unsigned int equip_pos_idx = 0;
  2237. //Create a list of all equipped positions to see if all items needed for the autobonus are still present [Playtester]
  2238. for(j = 0; j < EQI_MAX; j++) {
  2239. if(sd->equip_index[j] >= 0)
  2240. equip_pos_idx |= sd->inventory.u.items_inventory[sd->equip_index[j]].equip;
  2241. }
  2242. if((equip_pos_idx&autobonus->pos) == autobonus->pos)
  2243. script_run_autobonus(autobonus->other_script,sd,autobonus->pos);
  2244. }
  2245. autobonus->active = add_timer(gettick()+autobonus->duration, pc_endautobonus, sd->bl.id, (intptr_t)bonus);
  2246. sd->state.autobonus |= autobonus->pos;
  2247. status_calc_pc(sd,SCO_NONE);
  2248. }
  2249. /**
  2250. * Remove autobonus timer from player
  2251. */
  2252. TIMER_FUNC(pc_endautobonus){
  2253. struct map_session_data *sd = map_id2sd(id);
  2254. std::vector<s_autobonus> *bonus = (std::vector<s_autobonus> *)data;
  2255. nullpo_ret(sd);
  2256. nullpo_ret(bonus);
  2257. for( struct s_autobonus& autobonus : *bonus ){
  2258. if( autobonus.active == tid ){
  2259. autobonus.active = INVALID_TIMER;
  2260. sd->state.autobonus &= ~autobonus.pos;
  2261. break;
  2262. }
  2263. }
  2264. status_calc_pc(sd,SCO_NONE);
  2265. return 0;
  2266. }
  2267. /**
  2268. * Add element bonus to player when attacking
  2269. * @param sd: Player data
  2270. * @param ele: Element to adjust
  2271. * @param rate: Success chance
  2272. * @param flag: Battle flag
  2273. */
  2274. static void pc_bonus_addele(struct map_session_data* sd, unsigned char ele, short rate, short flag)
  2275. {
  2276. nullpo_retv(sd);
  2277. struct weapon_data *wd = (sd->state.lr_flag ? &sd->left_weapon : &sd->right_weapon);
  2278. if (wd->addele2.size() == MAX_PC_BONUS) {
  2279. ShowWarning("pc_bonus_addele: Reached max (%d) number of add element damage bonuses per character!\n", MAX_PC_BONUS);
  2280. return;
  2281. }
  2282. if (!(flag&BF_RANGEMASK))
  2283. flag |= BF_SHORT | BF_LONG;
  2284. if (!(flag&BF_WEAPONMASK))
  2285. flag |= BF_WEAPON;
  2286. if (!(flag&BF_SKILLMASK)) {
  2287. if (flag&(BF_MAGIC | BF_MISC))
  2288. flag |= BF_SKILL;
  2289. if (flag&BF_WEAPON)
  2290. flag |= BF_NORMAL | BF_SKILL;
  2291. }
  2292. for (auto &it : wd->addele2) {
  2293. if (it.ele == ele && it.flag == flag) {
  2294. it.rate = cap_value(it.rate + rate, -10000, 10000);
  2295. return;
  2296. }
  2297. }
  2298. struct s_addele2 entry = {};
  2299. if (rate < -10000 || rate > 10000)
  2300. ShowWarning("pc_bonus_addele: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2301. entry.ele = ele;
  2302. entry.rate = cap_value(rate, -10000, 10000);
  2303. entry.flag = flag;
  2304. wd->addele2.push_back(entry);
  2305. }
  2306. /**
  2307. * Reduce element bonus to player when attacking
  2308. * @param sd: Player data
  2309. * @param ele: Element to adjust
  2310. * @param rate: Success chance
  2311. * @param flag: Battle flag
  2312. */
  2313. static void pc_bonus_subele(struct map_session_data* sd, unsigned char ele, short rate, short flag)
  2314. {
  2315. nullpo_retv(sd);
  2316. if (sd->subele2.size() == MAX_PC_BONUS) {
  2317. ShowWarning("pc_bonus_subele: Reached max (%d) number of resist element damage bonuses per character!\n", MAX_PC_BONUS);
  2318. return;
  2319. }
  2320. if (!(flag&BF_RANGEMASK))
  2321. flag |= BF_SHORT | BF_LONG;
  2322. if (!(flag&BF_WEAPONMASK))
  2323. flag |= BF_WEAPON;
  2324. if (!(flag&BF_SKILLMASK)) {
  2325. if (flag&(BF_MAGIC | BF_MISC))
  2326. flag |= BF_SKILL;
  2327. if (flag&BF_WEAPON)
  2328. flag |= BF_NORMAL | BF_SKILL;
  2329. }
  2330. for (auto &it : sd->subele2) {
  2331. if (it.ele == ele && it.flag == flag) {
  2332. it.rate = cap_value(it.rate + rate, -10000, 10000);
  2333. return;
  2334. }
  2335. }
  2336. struct s_addele2 entry = {};
  2337. if (rate < -10000 || rate > 10000)
  2338. ShowWarning("pc_bonus_subele: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2339. entry.ele = ele;
  2340. entry.rate = cap_value(rate, -10000, 10000);
  2341. entry.flag = flag;
  2342. sd->subele2.push_back(entry);
  2343. }
  2344. /**
  2345. * General item bonus for player
  2346. * @param bonus: Bonus array
  2347. * @param id: Key
  2348. * @param val: Value
  2349. * @param cap_rate: If Value is a rate value that needs to be capped
  2350. */
  2351. static void pc_bonus_itembonus(std::vector<s_item_bonus> &bonus, uint16 id, int val, bool cap_rate)
  2352. {
  2353. for (auto &it : bonus) {
  2354. if (it.id == id) {
  2355. if (cap_rate)
  2356. it.val = cap_value(it.val + val, -10000, 10000);
  2357. else
  2358. it.val += val;
  2359. return;
  2360. }
  2361. }
  2362. struct s_item_bonus entry = {};
  2363. if (cap_rate && (val < -10000 || val > 10000)) {
  2364. ShowWarning("pc_bonus_itembonus: Item bonus val %d exceeds -10000~10000 range, capping.\n", val);
  2365. val = cap_value(val, -10000, 10000);
  2366. }
  2367. entry.id = id;
  2368. entry.val = val;
  2369. bonus.push_back(entry);
  2370. }
  2371. /**
  2372. * Remove HP/SP to player when attacking
  2373. * @param bonus: Bonus array
  2374. * @param rate: Success chance
  2375. * @param per: Percentage of HP/SP to vanish
  2376. * @param flag: Battle flag
  2377. */
  2378. static void pc_bonus_addvanish(std::vector<s_vanish_bonus> &bonus, int16 rate, int16 per, int flag) {
  2379. if (bonus.size() == MAX_PC_BONUS) {
  2380. ShowWarning("pc_bonus_addvanish: Reached max (%d) number of vanish damage bonuses per character!\n", MAX_PC_BONUS);
  2381. return;
  2382. }
  2383. if (!(flag&BF_RANGEMASK))
  2384. flag |= BF_SHORT | BF_LONG;
  2385. if (!(flag&BF_WEAPONMASK))
  2386. flag |= BF_WEAPON;
  2387. if (!(flag&BF_SKILLMASK)) {
  2388. if (flag&(BF_MAGIC | BF_MISC))
  2389. flag |= BF_SKILL;
  2390. if (flag&BF_WEAPON)
  2391. flag |= BF_NORMAL | BF_SKILL;
  2392. }
  2393. for (auto &it : bonus) {
  2394. if (it.flag == flag) {
  2395. it.rate = cap_value(it.rate + rate, -10000, 10000);
  2396. it.per += per;
  2397. return;
  2398. }
  2399. }
  2400. struct s_vanish_bonus entry = {};
  2401. if (rate < -10000 || rate > 10000)
  2402. ShowWarning("pc_bonus_addvanish: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2403. entry.rate = cap_value(rate, -10000, 10000);
  2404. entry.per = per;
  2405. entry.flag = flag;
  2406. bonus.push_back(entry);
  2407. }
  2408. /*==========================================
  2409. * Add a bonus(type) to player sd
  2410. * format: bonus bBonusName,val;
  2411. * @param sd
  2412. * @param type Bonus type used by bBonusName
  2413. * @param val Value that usually for rate or fixed value
  2414. *------------------------------------------*/
  2415. void pc_bonus(struct map_session_data *sd,int type,int val)
  2416. {
  2417. struct status_data *status;
  2418. int bonus;
  2419. nullpo_retv(sd);
  2420. status = &sd->base_status;
  2421. switch(type){
  2422. case SP_STR:
  2423. case SP_AGI:
  2424. case SP_VIT:
  2425. case SP_INT:
  2426. case SP_DEX:
  2427. case SP_LUK:
  2428. if(sd->state.lr_flag != 2)
  2429. sd->param_bonus[type-SP_STR]+=val;
  2430. break;
  2431. case SP_ATK1:
  2432. if(!sd->state.lr_flag) {
  2433. bonus = status->rhw.atk + val;
  2434. status->rhw.atk = cap_value(bonus, 0, USHRT_MAX);
  2435. }
  2436. else if(sd->state.lr_flag == 1) {
  2437. bonus = status->lhw.atk + val;
  2438. status->lhw.atk = cap_value(bonus, 0, USHRT_MAX);
  2439. }
  2440. break;
  2441. case SP_ATK2:
  2442. if(!sd->state.lr_flag) {
  2443. bonus = status->rhw.atk2 + val;
  2444. status->rhw.atk2 = cap_value(bonus, 0, USHRT_MAX);
  2445. }
  2446. else if(sd->state.lr_flag == 1) {
  2447. bonus = status->lhw.atk2 + val;
  2448. status->lhw.atk2 = cap_value(bonus, 0, USHRT_MAX);
  2449. }
  2450. break;
  2451. case SP_BASE_ATK:
  2452. if(sd->state.lr_flag != 2) {
  2453. #ifdef RENEWAL
  2454. bonus = sd->bonus.eatk + val;
  2455. sd->bonus.eatk = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2456. #else
  2457. bonus = status->batk + val;
  2458. status->batk = cap_value(bonus, 0, USHRT_MAX);
  2459. #endif
  2460. }
  2461. break;
  2462. case SP_DEF1:
  2463. if(sd->state.lr_flag != 2) {
  2464. bonus = status->def + val;
  2465. #ifdef RENEWAL
  2466. status->def = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2467. #else
  2468. status->def = cap_value(bonus, CHAR_MIN, CHAR_MAX);
  2469. #endif
  2470. }
  2471. break;
  2472. case SP_DEF2:
  2473. if(sd->state.lr_flag != 2) {
  2474. bonus = status->def2 + val;
  2475. status->def2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2476. }
  2477. break;
  2478. case SP_MDEF1:
  2479. if(sd->state.lr_flag != 2) {
  2480. bonus = status->mdef + val;
  2481. #ifdef RENEWAL
  2482. status->mdef = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2483. #else
  2484. status->mdef = cap_value(bonus, CHAR_MIN, CHAR_MAX);
  2485. #endif
  2486. if( sd->state.lr_flag == 3 ) {//Shield, used for royal guard
  2487. sd->bonus.shieldmdef += bonus;
  2488. }
  2489. }
  2490. break;
  2491. case SP_MDEF2:
  2492. if(sd->state.lr_flag != 2) {
  2493. bonus = status->mdef2 + val;
  2494. status->mdef2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2495. }
  2496. break;
  2497. case SP_HIT:
  2498. if(sd->state.lr_flag != 2) {
  2499. bonus = status->hit + val;
  2500. status->hit = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2501. } else
  2502. sd->bonus.arrow_hit+=val;
  2503. break;
  2504. case SP_FLEE1:
  2505. if(sd->state.lr_flag != 2) {
  2506. bonus = status->flee + val;
  2507. status->flee = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2508. }
  2509. break;
  2510. case SP_FLEE2:
  2511. if(sd->state.lr_flag != 2) {
  2512. bonus = status->flee2 + val*10;
  2513. status->flee2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2514. }
  2515. break;
  2516. case SP_CRITICAL:
  2517. if(sd->state.lr_flag != 2) {
  2518. bonus = status->cri + val*10;
  2519. status->cri = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2520. } else
  2521. sd->bonus.arrow_cri += val*10;
  2522. break;
  2523. case SP_ATKELE:
  2524. PC_BONUS_CHK_ELEMENT(val,SP_ATKELE);
  2525. switch (sd->state.lr_flag)
  2526. {
  2527. case 2:
  2528. switch (sd->status.weapon) {
  2529. case W_BOW:
  2530. case W_REVOLVER:
  2531. case W_RIFLE:
  2532. case W_GATLING:
  2533. case W_SHOTGUN:
  2534. case W_GRENADE:
  2535. //Become weapon element.
  2536. status->rhw.ele=val;
  2537. break;
  2538. default: //Become arrow element.
  2539. sd->bonus.arrow_ele=val;
  2540. break;
  2541. }
  2542. break;
  2543. case 1:
  2544. status->lhw.ele=val;
  2545. break;
  2546. default:
  2547. status->rhw.ele=val;
  2548. break;
  2549. }
  2550. break;
  2551. case SP_DEFELE:
  2552. PC_BONUS_CHK_ELEMENT(val,SP_DEFELE);
  2553. if(sd->state.lr_flag != 2)
  2554. status->def_ele=val;
  2555. break;
  2556. case SP_MAXHP:
  2557. if(sd->state.lr_flag == 2)
  2558. break;
  2559. sd->bonus.hp += val;
  2560. break;
  2561. case SP_MAXSP:
  2562. if(sd->state.lr_flag == 2)
  2563. break;
  2564. sd->bonus.sp += val;
  2565. break;
  2566. case SP_MAXHPRATE:
  2567. if(sd->state.lr_flag != 2)
  2568. sd->hprate+=val;
  2569. break;
  2570. case SP_MAXSPRATE:
  2571. if(sd->state.lr_flag != 2)
  2572. sd->sprate+=val;
  2573. break;
  2574. case SP_SPRATE:
  2575. if(sd->state.lr_flag != 2)
  2576. sd->dsprate+=val;
  2577. break;
  2578. case SP_ATTACKRANGE:
  2579. switch (sd->state.lr_flag) {
  2580. case 2:
  2581. switch (sd->status.weapon) {
  2582. case W_BOW:
  2583. case W_REVOLVER:
  2584. case W_RIFLE:
  2585. case W_GATLING:
  2586. case W_SHOTGUN:
  2587. case W_GRENADE:
  2588. status->rhw.range += val;
  2589. }
  2590. break;
  2591. case 1:
  2592. status->lhw.range += val;
  2593. break;
  2594. default:
  2595. status->rhw.range += val;
  2596. break;
  2597. }
  2598. break;
  2599. case SP_SPEED_RATE: //Non stackable increase
  2600. if(sd->state.lr_flag != 2)
  2601. sd->bonus.speed_rate = min(sd->bonus.speed_rate, -val);
  2602. break;
  2603. case SP_SPEED_ADDRATE: //Stackable increase
  2604. if(sd->state.lr_flag != 2)
  2605. sd->bonus.speed_add_rate -= val;
  2606. break;
  2607. case SP_ASPD: //Raw increase
  2608. if(sd->state.lr_flag != 2)
  2609. sd->bonus.aspd_add -= 10*val;
  2610. break;
  2611. case SP_ASPD_RATE: //Stackable increase - Made it linear as per rodatazone
  2612. if(sd->state.lr_flag != 2)
  2613. #ifndef RENEWAL_ASPD
  2614. status->aspd_rate -= 10*val;
  2615. #else
  2616. status->aspd_rate2 += val;
  2617. #endif
  2618. break;
  2619. case SP_HP_RECOV_RATE:
  2620. if(sd->state.lr_flag != 2)
  2621. sd->hprecov_rate += val;
  2622. break;
  2623. case SP_SP_RECOV_RATE:
  2624. if(sd->state.lr_flag != 2)
  2625. sd->sprecov_rate += val;
  2626. break;
  2627. case SP_CRITICAL_DEF:
  2628. if(sd->state.lr_flag != 2)
  2629. sd->bonus.critical_def += val;
  2630. break;
  2631. case SP_NEAR_ATK_DEF:
  2632. if(sd->state.lr_flag != 2)
  2633. sd->bonus.near_attack_def_rate += val;
  2634. break;
  2635. case SP_LONG_ATK_DEF:
  2636. if(sd->state.lr_flag != 2)
  2637. sd->bonus.long_attack_def_rate += val;
  2638. break;
  2639. case SP_DOUBLE_RATE:
  2640. if(sd->state.lr_flag == 0 && sd->bonus.double_rate < val)
  2641. sd->bonus.double_rate = val;
  2642. break;
  2643. case SP_DOUBLE_ADD_RATE:
  2644. if(sd->state.lr_flag == 0)
  2645. sd->bonus.double_add_rate += val;
  2646. break;
  2647. case SP_MATK_RATE:
  2648. if(sd->state.lr_flag != 2)
  2649. sd->matk_rate += val;
  2650. break;
  2651. case SP_IGNORE_DEF_ELE:
  2652. PC_BONUS_CHK_ELEMENT(val,SP_IGNORE_DEF_ELE);
  2653. if(!sd->state.lr_flag)
  2654. sd->right_weapon.ignore_def_ele |= 1<<val;
  2655. else if(sd->state.lr_flag == 1)
  2656. sd->left_weapon.ignore_def_ele |= 1<<val;
  2657. break;
  2658. case SP_IGNORE_DEF_RACE:
  2659. PC_BONUS_CHK_RACE(val,SP_IGNORE_DEF_RACE);
  2660. if(!sd->state.lr_flag)
  2661. sd->right_weapon.ignore_def_race |= 1<<val;
  2662. else if(sd->state.lr_flag == 1)
  2663. sd->left_weapon.ignore_def_race |= 1<<val;
  2664. break;
  2665. case SP_IGNORE_DEF_CLASS:
  2666. PC_BONUS_CHK_CLASS(val,SP_IGNORE_DEF_CLASS);
  2667. if(!sd->state.lr_flag)
  2668. sd->right_weapon.ignore_def_class |= 1<<val;
  2669. else if(sd->state.lr_flag == 1)
  2670. sd->left_weapon.ignore_def_class |= 1<<val;
  2671. break;
  2672. case SP_ATK_RATE:
  2673. if(sd->state.lr_flag != 2)
  2674. sd->bonus.atk_rate += val;
  2675. break;
  2676. case SP_MAGIC_ATK_DEF:
  2677. if(sd->state.lr_flag != 2)
  2678. sd->bonus.magic_def_rate += val;
  2679. break;
  2680. case SP_MISC_ATK_DEF:
  2681. if(sd->state.lr_flag != 2)
  2682. sd->bonus.misc_def_rate += val;
  2683. break;
  2684. case SP_IGNORE_MDEF_ELE:
  2685. PC_BONUS_CHK_ELEMENT(val,SP_IGNORE_MDEF_ELE);
  2686. if(sd->state.lr_flag != 2)
  2687. sd->bonus.ignore_mdef_ele |= 1<<val;
  2688. break;
  2689. case SP_IGNORE_MDEF_RACE:
  2690. PC_BONUS_CHK_RACE(val,SP_IGNORE_MDEF_RACE);
  2691. if(sd->state.lr_flag != 2)
  2692. sd->bonus.ignore_mdef_race |= 1<<val;
  2693. break;
  2694. case SP_PERFECT_HIT_RATE:
  2695. if(sd->state.lr_flag != 2 && sd->bonus.perfect_hit < val)
  2696. sd->bonus.perfect_hit = val;
  2697. break;
  2698. case SP_PERFECT_HIT_ADD_RATE:
  2699. if(sd->state.lr_flag != 2)
  2700. sd->bonus.perfect_hit_add += val;
  2701. break;
  2702. case SP_CRITICAL_RATE:
  2703. if(sd->state.lr_flag != 2)
  2704. sd->critical_rate+=val;
  2705. break;
  2706. case SP_DEF_RATIO_ATK_ELE:
  2707. PC_BONUS_CHK_ELEMENT(val,SP_DEF_RATIO_ATK_ELE);
  2708. if(!sd->state.lr_flag)
  2709. sd->right_weapon.def_ratio_atk_ele |= 1<<val;
  2710. else if(sd->state.lr_flag == 1)
  2711. sd->left_weapon.def_ratio_atk_ele |= 1<<val;
  2712. break;
  2713. case SP_DEF_RATIO_ATK_RACE:
  2714. PC_BONUS_CHK_RACE(val,SP_DEF_RATIO_ATK_RACE);
  2715. if(!sd->state.lr_flag)
  2716. sd->right_weapon.def_ratio_atk_race |= 1<<val;
  2717. else if(sd->state.lr_flag == 1)
  2718. sd->left_weapon.def_ratio_atk_race |= 1<<val;
  2719. break;
  2720. case SP_DEF_RATIO_ATK_CLASS:
  2721. PC_BONUS_CHK_CLASS(val,SP_DEF_RATIO_ATK_CLASS);
  2722. if(!sd->state.lr_flag)
  2723. sd->right_weapon.def_ratio_atk_class |= 1<<val;
  2724. else if(sd->state.lr_flag == 1)
  2725. sd->left_weapon.def_ratio_atk_class |= 1<<val;
  2726. break;
  2727. case SP_HIT_RATE:
  2728. if(sd->state.lr_flag != 2)
  2729. sd->hit_rate += val;
  2730. break;
  2731. case SP_FLEE_RATE:
  2732. if(sd->state.lr_flag != 2)
  2733. sd->flee_rate += val;
  2734. break;
  2735. case SP_FLEE2_RATE:
  2736. if(sd->state.lr_flag != 2)
  2737. sd->flee2_rate += val;
  2738. break;
  2739. case SP_DEF_RATE:
  2740. if(sd->state.lr_flag != 2)
  2741. sd->def_rate += val;
  2742. break;
  2743. case SP_DEF2_RATE:
  2744. if(sd->state.lr_flag != 2)
  2745. sd->def2_rate += val;
  2746. break;
  2747. case SP_MDEF_RATE:
  2748. if(sd->state.lr_flag != 2)
  2749. sd->mdef_rate += val;
  2750. break;
  2751. case SP_MDEF2_RATE:
  2752. if(sd->state.lr_flag != 2)
  2753. sd->mdef2_rate += val;
  2754. break;
  2755. case SP_RESTART_FULL_RECOVER:
  2756. if(sd->state.lr_flag != 2)
  2757. sd->special_state.restart_full_recover = 1;
  2758. break;
  2759. case SP_NO_CASTCANCEL:
  2760. if(sd->state.lr_flag != 2)
  2761. sd->special_state.no_castcancel = 1;
  2762. break;
  2763. case SP_NO_CASTCANCEL2:
  2764. if(sd->state.lr_flag != 2)
  2765. sd->special_state.no_castcancel2 = 1;
  2766. break;
  2767. case SP_NO_SIZEFIX:
  2768. if(sd->state.lr_flag != 2)
  2769. sd->special_state.no_sizefix = 1;
  2770. break;
  2771. case SP_NO_MAGIC_DAMAGE:
  2772. if(sd->state.lr_flag == 2)
  2773. break;
  2774. val+= sd->special_state.no_magic_damage;
  2775. sd->special_state.no_magic_damage = cap_value(val,0,100);
  2776. break;
  2777. case SP_NO_WEAPON_DAMAGE:
  2778. if(sd->state.lr_flag == 2)
  2779. break;
  2780. val+= sd->special_state.no_weapon_damage;
  2781. sd->special_state.no_weapon_damage = cap_value(val,0,100);
  2782. break;
  2783. case SP_NO_MISC_DAMAGE:
  2784. if(sd->state.lr_flag == 2)
  2785. break;
  2786. val+= sd->special_state.no_misc_damage;
  2787. sd->special_state.no_misc_damage = cap_value(val,0,100);
  2788. break;
  2789. case SP_NO_GEMSTONE:
  2790. if(sd->state.lr_flag != 2 && sd->special_state.no_gemstone != 2)
  2791. sd->special_state.no_gemstone = 1;
  2792. break;
  2793. case SP_INTRAVISION: // Maya Purple Card effect allowing to see Hiding/Cloaking people [DracoRPG]
  2794. if(sd->state.lr_flag != 2) {
  2795. sd->special_state.intravision = 1;
  2796. clif_status_load(&sd->bl, EFST_CLAIRVOYANCE, 1);
  2797. }
  2798. break;
  2799. case SP_NO_KNOCKBACK:
  2800. if(sd->state.lr_flag != 2)
  2801. sd->special_state.no_knockback = 1;
  2802. break;
  2803. case SP_SPLASH_RANGE:
  2804. if(sd->bonus.splash_range < val)
  2805. sd->bonus.splash_range = val;
  2806. break;
  2807. case SP_SPLASH_ADD_RANGE:
  2808. sd->bonus.splash_add_range += val;
  2809. break;
  2810. case SP_SHORT_WEAPON_DAMAGE_RETURN:
  2811. if(sd->state.lr_flag != 2)
  2812. sd->bonus.short_weapon_damage_return += val;
  2813. break;
  2814. case SP_LONG_WEAPON_DAMAGE_RETURN:
  2815. if(sd->state.lr_flag != 2)
  2816. sd->bonus.long_weapon_damage_return += val;
  2817. break;
  2818. case SP_MAGIC_DAMAGE_RETURN: //AppleGirl Was Here
  2819. if(sd->state.lr_flag != 2)
  2820. sd->bonus.magic_damage_return += val;
  2821. break;
  2822. case SP_ALL_STATS: // [Valaris]
  2823. if(sd->state.lr_flag!=2) {
  2824. sd->param_bonus[SP_STR-SP_STR]+=val;
  2825. sd->param_bonus[SP_AGI-SP_STR]+=val;
  2826. sd->param_bonus[SP_VIT-SP_STR]+=val;
  2827. sd->param_bonus[SP_INT-SP_STR]+=val;
  2828. sd->param_bonus[SP_DEX-SP_STR]+=val;
  2829. sd->param_bonus[SP_LUK-SP_STR]+=val;
  2830. }
  2831. break;
  2832. case SP_AGI_VIT: // [Valaris]
  2833. if(sd->state.lr_flag!=2) {
  2834. sd->param_bonus[SP_AGI-SP_STR]+=val;
  2835. sd->param_bonus[SP_VIT-SP_STR]+=val;
  2836. }
  2837. break;
  2838. case SP_AGI_DEX_STR: // [Valaris]
  2839. if(sd->state.lr_flag!=2) {
  2840. sd->param_bonus[SP_AGI-SP_STR]+=val;
  2841. sd->param_bonus[SP_DEX-SP_STR]+=val;
  2842. sd->param_bonus[SP_STR-SP_STR]+=val;
  2843. }
  2844. break;
  2845. case SP_PERFECT_HIDE: // [Valaris]
  2846. if(sd->state.lr_flag!=2)
  2847. sd->special_state.perfect_hiding=1;
  2848. break;
  2849. case SP_UNBREAKABLE:
  2850. if(sd->state.lr_flag!=2)
  2851. sd->bonus.unbreakable += val;
  2852. break;
  2853. case SP_UNBREAKABLE_WEAPON:
  2854. if(sd->state.lr_flag != 2)
  2855. sd->bonus.unbreakable_equip |= EQP_WEAPON;
  2856. break;
  2857. case SP_UNBREAKABLE_ARMOR:
  2858. if(sd->state.lr_flag != 2)
  2859. sd->bonus.unbreakable_equip |= EQP_ARMOR;
  2860. break;
  2861. case SP_UNBREAKABLE_HELM:
  2862. if(sd->state.lr_flag != 2)
  2863. sd->bonus.unbreakable_equip |= EQP_HELM;
  2864. break;
  2865. case SP_UNBREAKABLE_SHIELD:
  2866. if(sd->state.lr_flag != 2)
  2867. sd->bonus.unbreakable_equip |= EQP_SHIELD;
  2868. break;
  2869. case SP_UNBREAKABLE_GARMENT:
  2870. if(sd->state.lr_flag != 2)
  2871. sd->bonus.unbreakable_equip |= EQP_GARMENT;
  2872. break;
  2873. case SP_UNBREAKABLE_SHOES:
  2874. if(sd->state.lr_flag != 2)
  2875. sd->bonus.unbreakable_equip |= EQP_SHOES;
  2876. break;
  2877. case SP_CLASSCHANGE: // [Valaris]
  2878. if(sd->state.lr_flag !=2)
  2879. sd->bonus.classchange=val;
  2880. break;
  2881. case SP_LONG_ATK_RATE:
  2882. if(sd->state.lr_flag != 2) //[Lupus] it should stack, too. As any other cards rate bonuses
  2883. sd->bonus.long_attack_atk_rate+=val;
  2884. break;
  2885. case SP_BREAK_WEAPON_RATE:
  2886. if(sd->state.lr_flag != 2)
  2887. sd->bonus.break_weapon_rate+=val;
  2888. break;
  2889. case SP_BREAK_ARMOR_RATE:
  2890. if(sd->state.lr_flag != 2)
  2891. sd->bonus.break_armor_rate+=val;
  2892. break;
  2893. case SP_ADD_STEAL_RATE:
  2894. if(sd->state.lr_flag != 2)
  2895. sd->bonus.add_steal_rate+=val;
  2896. break;
  2897. case SP_DELAYRATE:
  2898. if(sd->state.lr_flag != 2)
  2899. sd->delayrate+=val;
  2900. break;
  2901. case SP_CRIT_ATK_RATE:
  2902. if(sd->state.lr_flag != 2)
  2903. sd->bonus.crit_atk_rate += val;
  2904. break;
  2905. case SP_NO_REGEN:
  2906. if(sd->state.lr_flag != 2)
  2907. sd->regen.state.block|=val;
  2908. break;
  2909. case SP_UNSTRIPABLE_WEAPON:
  2910. if(sd->state.lr_flag != 2)
  2911. sd->bonus.unstripable_equip |= EQP_WEAPON;
  2912. break;
  2913. case SP_UNSTRIPABLE:
  2914. case SP_UNSTRIPABLE_ARMOR:
  2915. if(sd->state.lr_flag != 2)
  2916. sd->bonus.unstripable_equip |= EQP_ARMOR;
  2917. break;
  2918. case SP_UNSTRIPABLE_HELM:
  2919. if(sd->state.lr_flag != 2)
  2920. sd->bonus.unstripable_equip |= EQP_HELM;
  2921. break;
  2922. case SP_UNSTRIPABLE_SHIELD:
  2923. if(sd->state.lr_flag != 2)
  2924. sd->bonus.unstripable_equip |= EQP_SHIELD;
  2925. break;
  2926. case SP_HP_DRAIN_VALUE: // bonus bHPDrainValue,n;
  2927. if(!sd->state.lr_flag) {
  2928. sd->right_weapon.hp_drain_class[CLASS_NORMAL] += val;
  2929. sd->right_weapon.hp_drain_class[CLASS_BOSS] += val;
  2930. } else if(sd->state.lr_flag == 1) {
  2931. sd->left_weapon.hp_drain_class[CLASS_NORMAL] += val;
  2932. sd->left_weapon.hp_drain_class[CLASS_BOSS] += val;
  2933. }
  2934. break;
  2935. case SP_SP_DRAIN_VALUE: // bonus bSPDrainValue,n;
  2936. if(!sd->state.lr_flag) {
  2937. sd->right_weapon.sp_drain_class[CLASS_NORMAL] += val;
  2938. sd->right_weapon.sp_drain_class[CLASS_BOSS] += val;
  2939. } else if(sd->state.lr_flag == 1) {
  2940. sd->left_weapon.sp_drain_class[CLASS_NORMAL] += val;
  2941. sd->left_weapon.sp_drain_class[CLASS_BOSS] += val;
  2942. }
  2943. break;
  2944. case SP_SP_GAIN_VALUE:
  2945. if(!sd->state.lr_flag)
  2946. sd->bonus.sp_gain_value += val;
  2947. break;
  2948. case SP_HP_GAIN_VALUE:
  2949. if(!sd->state.lr_flag)
  2950. sd->bonus.hp_gain_value += val;
  2951. break;
  2952. case SP_LONG_SP_GAIN_VALUE:
  2953. if(!sd->state.lr_flag)
  2954. sd->bonus.long_sp_gain_value += val;
  2955. case SP_LONG_HP_GAIN_VALUE:
  2956. if(!sd->state.lr_flag)
  2957. sd->bonus.long_hp_gain_value += val;
  2958. break;
  2959. case SP_MAGIC_SP_GAIN_VALUE:
  2960. if(!sd->state.lr_flag)
  2961. sd->bonus.magic_sp_gain_value += val;
  2962. break;
  2963. case SP_MAGIC_HP_GAIN_VALUE:
  2964. if(!sd->state.lr_flag)
  2965. sd->bonus.magic_hp_gain_value += val;
  2966. break;
  2967. case SP_ADD_HEAL_RATE:
  2968. if(sd->state.lr_flag != 2)
  2969. sd->bonus.add_heal_rate += val;
  2970. break;
  2971. case SP_ADD_HEAL2_RATE:
  2972. if(sd->state.lr_flag != 2)
  2973. sd->bonus.add_heal2_rate += val;
  2974. break;
  2975. case SP_ADD_ITEM_HEAL_RATE:
  2976. if(sd->state.lr_flag != 2)
  2977. sd->bonus.itemhealrate2 += val;
  2978. break;
  2979. case SP_EMATK:
  2980. if(sd->state.lr_flag != 2)
  2981. sd->bonus.ematk += val;
  2982. break;
  2983. case SP_ADD_VARIABLECAST:
  2984. if (sd->state.lr_flag != 2)
  2985. sd->bonus.add_varcast += val;
  2986. break;
  2987. #ifdef RENEWAL_CAST
  2988. case SP_FIXCASTRATE:
  2989. if(sd->state.lr_flag != 2)
  2990. sd->bonus.fixcastrate = min(sd->bonus.fixcastrate,val);
  2991. break;
  2992. case SP_ADD_FIXEDCAST:
  2993. if(sd->state.lr_flag != 2)
  2994. sd->bonus.add_fixcast += val;
  2995. break;
  2996. case SP_CASTRATE:
  2997. case SP_VARCASTRATE:
  2998. if(sd->state.lr_flag != 2)
  2999. sd->bonus.varcastrate -= val;
  3000. break;
  3001. #else
  3002. case SP_ADD_FIXEDCAST:
  3003. case SP_FIXCASTRATE:
  3004. //ShowWarning("pc_bonus: non-RENEWAL_CAST doesn't support this bonus %d.\n", type);
  3005. break;
  3006. case SP_VARCASTRATE:
  3007. case SP_CASTRATE:
  3008. if(sd->state.lr_flag != 2)
  3009. sd->castrate += val;
  3010. break;
  3011. #endif
  3012. case SP_ADDMAXWEIGHT:
  3013. if (sd->state.lr_flag != 2)
  3014. sd->add_max_weight += val;
  3015. break;
  3016. case SP_ABSORB_DMG_MAXHP: // bonus bAbsorbDmgMaxHP,n;
  3017. sd->bonus.absorb_dmg_maxhp = max(sd->bonus.absorb_dmg_maxhp, val);
  3018. break;
  3019. case SP_CRITICAL_RANGEATK: // bonus bCriticalLong,n;
  3020. if (sd->state.lr_flag != 2)
  3021. sd->bonus.critical_rangeatk += val*10;
  3022. else
  3023. sd->bonus.arrow_cri += val*10;
  3024. break;
  3025. case SP_WEAPON_ATK_RATE:
  3026. if (sd->state.lr_flag != 2)
  3027. sd->bonus.weapon_atk_rate += val;
  3028. break;
  3029. case SP_WEAPON_MATK_RATE:
  3030. if (sd->state.lr_flag != 2)
  3031. sd->bonus.weapon_matk_rate += val;
  3032. break;
  3033. case SP_NO_MADO_FUEL:
  3034. if (sd->state.lr_flag != 2)
  3035. sd->special_state.no_mado_fuel = 1;
  3036. break;
  3037. case SP_NO_WALK_DELAY:
  3038. if (sd->state.lr_flag != 2)
  3039. sd->special_state.no_walk_delay = 1;
  3040. break;
  3041. default:
  3042. if (running_npc_stat_calc_event) {
  3043. ShowWarning("pc_bonus: unknown bonus type %d %d in OnPCStatCalcEvent!\n", type, val);
  3044. }
  3045. else if (current_equip_combo_pos > 0) {
  3046. 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);
  3047. }
  3048. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  3049. 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);
  3050. }
  3051. else {
  3052. ShowWarning("pc_bonus: unknown bonus type %d %d in unknown usage. Report this!\n", type, val);
  3053. }
  3054. break;
  3055. }
  3056. }
  3057. /*==========================================
  3058. * Player bonus (type) with args type2 and val, called trough bonus2 (npc)
  3059. * format: bonus2 bBonusName,type2,val;
  3060. * @param sd
  3061. * @param type Bonus type used by bBonusName
  3062. * @param type2
  3063. * @param val Value that usually for rate or fixed value
  3064. *------------------------------------------*/
  3065. void pc_bonus2(struct map_session_data *sd,int type,int type2,int val)
  3066. {
  3067. nullpo_retv(sd);
  3068. switch(type){
  3069. case SP_ADDELE: // bonus2 bAddEle,e,x;
  3070. PC_BONUS_CHK_ELEMENT(type2,SP_ADDELE);
  3071. if(!sd->state.lr_flag || sd->state.lr_flag == 3)
  3072. sd->right_weapon.addele[type2]+=val;
  3073. else if(sd->state.lr_flag == 1)
  3074. sd->left_weapon.addele[type2]+=val;
  3075. else if(sd->state.lr_flag == 2)
  3076. sd->arrow_addele[type2]+=val;
  3077. break;
  3078. case SP_ADDRACE: // bonus2 bAddRace,r,x;
  3079. PC_BONUS_CHK_RACE(type2,SP_ADDRACE);
  3080. if(!sd->state.lr_flag || sd->state.lr_flag == 3)
  3081. sd->right_weapon.addrace[type2]+=val;
  3082. else if(sd->state.lr_flag == 1)
  3083. sd->left_weapon.addrace[type2]+=val;
  3084. else if(sd->state.lr_flag == 2)
  3085. sd->arrow_addrace[type2]+=val;
  3086. break;
  3087. case SP_ADDCLASS: // bonus2 bAddClass,c,x;
  3088. PC_BONUS_CHK_CLASS(type2,SP_ADDCLASS);
  3089. if(!sd->state.lr_flag || sd->state.lr_flag == 3)
  3090. sd->right_weapon.addclass[type2]+=val;
  3091. else if(sd->state.lr_flag == 1)
  3092. sd->left_weapon.addclass[type2]+=val;
  3093. else if(sd->state.lr_flag == 2)
  3094. sd->arrow_addclass[type2]+=val;
  3095. break;
  3096. case SP_ADDSIZE: // bonus2 bAddSize,s,x;
  3097. PC_BONUS_CHK_SIZE(type2,SP_ADDSIZE);
  3098. if(!sd->state.lr_flag || sd->state.lr_flag == 3)
  3099. sd->right_weapon.addsize[type2]+=val;
  3100. else if(sd->state.lr_flag == 1)
  3101. sd->left_weapon.addsize[type2]+=val;
  3102. else if(sd->state.lr_flag == 2)
  3103. sd->arrow_addsize[type2]+=val;
  3104. break;
  3105. case SP_SUBELE: // bonus2 bSubEle,e,x;
  3106. PC_BONUS_CHK_ELEMENT(type2,SP_SUBELE);
  3107. if(sd->state.lr_flag != 2)
  3108. sd->subele_script[type2] += val;
  3109. break;
  3110. case SP_SUBRACE: // bonus2 bSubRace,r,x;
  3111. PC_BONUS_CHK_RACE(type2,SP_SUBRACE);
  3112. if(sd->state.lr_flag != 2)
  3113. sd->subrace[type2]+=val;
  3114. break;
  3115. case SP_SUBCLASS: // bonus2 bSubClass,c,x;
  3116. PC_BONUS_CHK_CLASS(type2,SP_SUBCLASS);
  3117. if(sd->state.lr_flag != 2)
  3118. sd->subclass[type2]+=val;
  3119. break;
  3120. case SP_ADDEFF: // bonus2 bAddEff,eff,n;
  3121. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  3122. pc_bonus_addeff(sd->addeff, (sc_type)type2, sd->state.lr_flag != 2 ? val : 0, sd->state.lr_flag == 2 ? val : 0, 0, 0);
  3123. break;
  3124. case SP_ADDEFF2: // bonus2 bAddEff2,eff,n;
  3125. PC_BONUS_CHK_SC(type2,SP_ADDEFF2);
  3126. 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);
  3127. break;
  3128. case SP_RESEFF: // bonus2 bResEff,eff,n;
  3129. if(sd->state.lr_flag == 2)
  3130. break;
  3131. if (sd->reseff.size() == MAX_PC_BONUS) {
  3132. ShowWarning("pc_bonus2: SP_RESEFF: Reached max (%d) number of resistance bonuses per character!\n", MAX_PC_BONUS);
  3133. break;
  3134. }
  3135. pc_bonus_itembonus(sd->reseff, type2, val, true);
  3136. break;
  3137. case SP_MAGIC_ADDELE: // bonus2 bMagicAddEle,e,x;
  3138. PC_BONUS_CHK_ELEMENT(type2,SP_MAGIC_ADDELE);
  3139. if(sd->state.lr_flag != 2)
  3140. sd->magic_addele_script[type2] += val;
  3141. break;
  3142. case SP_MAGIC_ADDRACE: // bonus2 bMagicAddRace,r,x;
  3143. PC_BONUS_CHK_RACE(type2,SP_MAGIC_ADDRACE);
  3144. if(sd->state.lr_flag != 2)
  3145. sd->magic_addrace[type2]+=val;
  3146. break;
  3147. case SP_MAGIC_ADDCLASS: // bonus2 bMagicAddClass,c,x;
  3148. PC_BONUS_CHK_CLASS(type2,SP_MAGIC_ADDCLASS);
  3149. if(sd->state.lr_flag != 2)
  3150. sd->magic_addclass[type2]+=val;
  3151. break;
  3152. case SP_MAGIC_ADDSIZE: // bonus2 bMagicAddSize,s,x;
  3153. PC_BONUS_CHK_SIZE(type2,SP_MAGIC_ADDSIZE);
  3154. if(sd->state.lr_flag != 2)
  3155. sd->magic_addsize[type2]+=val;
  3156. break;
  3157. case SP_MAGIC_ATK_ELE: // bonus2 bMagicAtkEle,e,x;
  3158. PC_BONUS_CHK_ELEMENT(type2,SP_MAGIC_ATK_ELE);
  3159. if(sd->state.lr_flag != 2)
  3160. sd->magic_atk_ele[type2]+=val;
  3161. break;
  3162. case SP_ADD_DAMAGE_CLASS: { // bonus2 bAddDamageClass,mid,x;
  3163. struct weapon_data *wd = (sd->state.lr_flag ? &sd->left_weapon : &sd->right_weapon);
  3164. if (wd->add_dmg.size() == MAX_PC_BONUS) {
  3165. ShowWarning("pc_bonus2: SP_ADD_DAMAGE_CLASS: Reached max (%d) number of add class damage bonuses per character!\n", MAX_PC_BONUS);
  3166. break;
  3167. }
  3168. pc_bonus_itembonus(wd->add_dmg, type2, val, false);
  3169. }
  3170. break;
  3171. case SP_ADD_MAGIC_DAMAGE_CLASS: // bonus2 bAddMagicDamageClass,mid,x;
  3172. if(sd->state.lr_flag == 2)
  3173. break;
  3174. if (sd->add_mdmg.size() == MAX_PC_BONUS) {
  3175. ShowWarning("pc_bonus2: SP_ADD_MAGIC_DAMAGE_CLASS: Reached max (%d) number of add class magic dmg bonuses per character!\n", MAX_PC_BONUS);
  3176. break;
  3177. }
  3178. pc_bonus_itembonus(sd->add_mdmg, type2, val, false);
  3179. break;
  3180. case SP_ADD_DEF_MONSTER: // bonus2 bAddDefMonster,mid,x;
  3181. if(sd->state.lr_flag == 2)
  3182. break;
  3183. if (sd->add_def.size() == MAX_PC_BONUS) {
  3184. ShowWarning("pc_bonus2: SP_ADD_DEF_MONSTER: Reached max (%d) number of add class def bonuses per character!\n", MAX_PC_BONUS);
  3185. break;
  3186. }
  3187. pc_bonus_itembonus(sd->add_def, type2, val, false);
  3188. break;
  3189. case SP_ADD_MDEF_MONSTER: // bonus2 bAddMDefMonster,mid,x;
  3190. if(sd->state.lr_flag == 2)
  3191. break;
  3192. if (sd->add_mdef.size() == MAX_PC_BONUS) {
  3193. ShowWarning("pc_bonus2: SP_ADD_MDEF_MONSTER: Reached max (%d) number of add class mdef bonuses per character!\n", MAX_PC_BONUS);
  3194. break;
  3195. }
  3196. pc_bonus_itembonus(sd->add_mdef, type2, val, false);
  3197. break;
  3198. case SP_HP_DRAIN_RATE: // bonus2 bHPDrainRate,x,n;
  3199. if(!sd->state.lr_flag) {
  3200. sd->right_weapon.hp_drain_rate.rate += type2;
  3201. sd->right_weapon.hp_drain_rate.per += val;
  3202. }
  3203. else if(sd->state.lr_flag == 1) {
  3204. sd->left_weapon.hp_drain_rate.rate += type2;
  3205. sd->left_weapon.hp_drain_rate.per += val;
  3206. }
  3207. break;
  3208. case SP_SP_DRAIN_RATE: // bonus2 bSPDrainRate,x,n;
  3209. if(!sd->state.lr_flag) {
  3210. sd->right_weapon.sp_drain_rate.rate += type2;
  3211. sd->right_weapon.sp_drain_rate.per += val;
  3212. }
  3213. else if(sd->state.lr_flag == 1) {
  3214. sd->left_weapon.sp_drain_rate.rate += type2;
  3215. sd->left_weapon.sp_drain_rate.per += val;
  3216. }
  3217. break;
  3218. case SP_SP_VANISH_RATE: // bonus2 bSPVanishRate,x,n;
  3219. if(sd->state.lr_flag != 2) {
  3220. pc_bonus_addvanish(sd->sp_vanish, type2, val, BF_NORMAL);
  3221. }
  3222. break;
  3223. case SP_HP_VANISH_RATE: // bonus2 bHPVanishRate,x,n;
  3224. if(sd->state.lr_flag != 2) {
  3225. pc_bonus_addvanish(sd->hp_vanish, type2, val, BF_NORMAL);
  3226. }
  3227. break;
  3228. case SP_GET_ZENY_NUM: // bonus2 bGetZenyNum,x,n;
  3229. if(sd->state.lr_flag != 2 && sd->bonus.get_zeny_rate < val) {
  3230. sd->bonus.get_zeny_rate = val;
  3231. sd->bonus.get_zeny_num = type2;
  3232. }
  3233. break;
  3234. case SP_ADD_GET_ZENY_NUM: // bonus2 bAddGetZenyNum,x,n;
  3235. if(sd->state.lr_flag != 2) {
  3236. sd->bonus.get_zeny_rate += val;
  3237. sd->bonus.get_zeny_num += type2;
  3238. }
  3239. break;
  3240. case SP_WEAPON_COMA_ELE: // bonus2 bWeaponComaEle,e,n;
  3241. PC_BONUS_CHK_ELEMENT(type2,SP_WEAPON_COMA_ELE);
  3242. if(sd->state.lr_flag == 2)
  3243. break;
  3244. sd->weapon_coma_ele[type2] += val;
  3245. sd->special_state.bonus_coma = 1;
  3246. break;
  3247. case SP_WEAPON_COMA_RACE: // bonus2 bWeaponComaRace,r,n;
  3248. PC_BONUS_CHK_RACE(type2,SP_WEAPON_COMA_RACE);
  3249. if(sd->state.lr_flag == 2)
  3250. break;
  3251. sd->weapon_coma_race[type2] += val;
  3252. sd->special_state.bonus_coma = 1;
  3253. break;
  3254. case SP_WEAPON_COMA_CLASS: // bonus2 bWeaponComaClass,c,n;
  3255. PC_BONUS_CHK_CLASS(type2,SP_WEAPON_COMA_CLASS);
  3256. if(sd->state.lr_flag == 2)
  3257. break;
  3258. sd->weapon_coma_class[type2] += val;
  3259. sd->special_state.bonus_coma = 1;
  3260. break;
  3261. case SP_WEAPON_ATK: // bonus2 bWeaponAtk,w,n;
  3262. if(sd->state.lr_flag != 2)
  3263. sd->weapon_atk[type2]+=val;
  3264. break;
  3265. case SP_WEAPON_DAMAGE_RATE: // bonus2 bWeaponDamageRate,w,n;
  3266. if(sd->state.lr_flag != 2)
  3267. sd->weapon_damage_rate[type2]+=val;
  3268. break;
  3269. case SP_CRITICAL_ADDRACE: // bonus2 bCriticalAddRace,r,n;
  3270. PC_BONUS_CHK_RACE(type2,SP_CRITICAL_ADDRACE);
  3271. if(sd->state.lr_flag != 2)
  3272. sd->critaddrace[type2] += val*10;
  3273. break;
  3274. case SP_ADDEFF_WHENHIT: // bonus2 bAddEffWhenHit,eff,n;
  3275. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  3276. if(sd->state.lr_flag != 2)
  3277. pc_bonus_addeff(sd->addeff_atked, (sc_type)type2, val, 0, 0, 0);
  3278. break;
  3279. case SP_SKILL_ATK: // bonus2 bSkillAtk,sk,n;
  3280. if(sd->state.lr_flag == 2)
  3281. break;
  3282. if (sd->skillatk.size() == MAX_PC_BONUS) {
  3283. 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);
  3284. break;
  3285. }
  3286. pc_bonus_itembonus(sd->skillatk, type2, val, false);
  3287. break;
  3288. case SP_SKILL_HEAL: // bonus2 bSkillHeal,sk,n;
  3289. if(sd->state.lr_flag == 2)
  3290. break;
  3291. if (sd->skillheal.size() == MAX_PC_BONUS) { // Better mention this so the array length can be updated. [Skotlex]
  3292. 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);
  3293. break;
  3294. }
  3295. pc_bonus_itembonus(sd->skillheal, type2, val, false);
  3296. break;
  3297. case SP_SKILL_HEAL2: // bonus2 bSkillHeal2,sk,n;
  3298. if(sd->state.lr_flag == 2)
  3299. break;
  3300. if (sd->skillheal2.size() == MAX_PC_BONUS) { // Better mention this so the array length can be updated. [Skotlex]
  3301. 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);
  3302. break;
  3303. }
  3304. pc_bonus_itembonus(sd->skillheal2, type2, val, false);
  3305. break;
  3306. case SP_ADD_SKILL_BLOW: // bonus2 bAddSkillBlow,sk,n;
  3307. if(sd->state.lr_flag == 2)
  3308. break;
  3309. if (sd->skillblown.size() == MAX_PC_BONUS) { //Better mention this so the array length can be updated. [Skotlex]
  3310. 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);
  3311. break;
  3312. }
  3313. pc_bonus_itembonus(sd->skillblown, type2, val, false);
  3314. break;
  3315. case SP_HP_LOSS_RATE: // bonus2 bHPLossRate,n,t;
  3316. if(sd->state.lr_flag != 2) {
  3317. sd->hp_loss.value = type2;
  3318. sd->hp_loss.rate = val;
  3319. }
  3320. break;
  3321. case SP_HP_REGEN_RATE: // bonus2 bHPRegenRate,n,t;
  3322. if(sd->state.lr_flag != 2) {
  3323. sd->hp_regen.value = type2;
  3324. sd->hp_regen.rate = val;
  3325. }
  3326. break;
  3327. case SP_REGEN_PERCENT_HP: // bonus2 bRegenPercentHP,n,t;
  3328. if (sd->state.lr_flag != 2) {
  3329. sd->percent_hp_regen.value = type2;
  3330. sd->percent_hp_regen.rate = val;
  3331. }
  3332. break;
  3333. case SP_REGEN_PERCENT_SP: // bonus2 bRegenPercentSP,n,t;
  3334. if (sd->state.lr_flag != 2) {
  3335. sd->percent_sp_regen.value = type2;
  3336. sd->percent_sp_regen.rate = val;
  3337. }
  3338. break;
  3339. case SP_ADDRACE2: // bonus2 bAddRace2,mr,x;
  3340. PC_BONUS_CHK_RACE2(type2,SP_ADDRACE2);
  3341. if(sd->state.lr_flag != 2)
  3342. sd->right_weapon.addrace2[type2] += val;
  3343. else
  3344. sd->left_weapon.addrace2[type2] += val;
  3345. break;
  3346. case SP_SUBSIZE: // bonus2 bSubSize,s,x;
  3347. PC_BONUS_CHK_SIZE(type2,SP_SUBSIZE);
  3348. if(sd->state.lr_flag != 2)
  3349. sd->subsize[type2]+=val;
  3350. break;
  3351. case SP_SUBRACE2: // bonus2 bSubRace2,mr,x;
  3352. PC_BONUS_CHK_RACE2(type2,SP_SUBRACE2);
  3353. if(sd->state.lr_flag != 2)
  3354. sd->subrace2[type2]+=val;
  3355. break;
  3356. case SP_ADD_ITEM_HEAL_RATE: // bonus2 bAddItemHealRate,iid,n;
  3357. if(sd->state.lr_flag == 2)
  3358. break;
  3359. if (!itemdb_exists(type2)) {
  3360. ShowWarning("pc_bonus2: SP_ADD_ITEM_HEAL_RATE Invalid item with id %d\n", type2);
  3361. break;
  3362. }
  3363. if (sd->itemhealrate.size() == MAX_PC_BONUS) {
  3364. ShowWarning("pc_bonus2: SP_ADD_ITEM_HEAL_RATE: Reached max (%d) number of item heal bonuses per character!\n", MAX_PC_BONUS);
  3365. break;
  3366. }
  3367. pc_bonus_itembonus(sd->itemhealrate, type2, val, false);
  3368. break;
  3369. case SP_ADD_ITEMGROUP_HEAL_RATE: // bonus2 bAddItemGroupHealRate,ig,n;
  3370. if (sd->state.lr_flag == 2)
  3371. break;
  3372. if (!type2 || !itemdb_group_exists(type2)) {
  3373. ShowWarning("pc_bonus2: SP_ADD_ITEMGROUP_HEAL_RATE: Invalid item group with id %d\n", type2);
  3374. break;
  3375. }
  3376. if (sd->itemhealrate.size() == MAX_PC_BONUS) {
  3377. ShowWarning("pc_bonus2: SP_ADD_ITEMGROUP_HEAL_RATE: Reached max (%d) number of item heal bonuses per character!\n", MAX_PC_BONUS);
  3378. break;
  3379. }
  3380. pc_bonus_itembonus(sd->itemgrouphealrate, type2, val, false);
  3381. break;
  3382. case SP_EXP_ADDRACE: // bonus2 bExpAddRace,r,x;
  3383. PC_BONUS_CHK_RACE(type2,SP_EXP_ADDRACE);
  3384. if(sd->state.lr_flag != 2)
  3385. sd->expaddrace[type2]+=val;
  3386. break;
  3387. case SP_EXP_ADDCLASS: // bonus2 bExpAddClass,c,x;
  3388. PC_BONUS_CHK_CLASS(type2,SP_EXP_ADDCLASS);
  3389. if(sd->state.lr_flag != 2)
  3390. sd->expaddclass[type2]+=val;
  3391. break;
  3392. case SP_SP_GAIN_RACE: // bonus2 bSPGainRace,r,n;
  3393. PC_BONUS_CHK_RACE(type2,SP_SP_GAIN_RACE);
  3394. if(sd->state.lr_flag != 2)
  3395. sd->sp_gain_race[type2]+=val;
  3396. break;
  3397. case SP_ADD_MONSTER_DROP_ITEM: // bonus2 bAddMonsterDropItem,iid,n;
  3398. if (sd->state.lr_flag != 2)
  3399. pc_bonus_item_drop(sd->add_drop, type2, 0, CLASS_ALL, RC_NONE_, val);
  3400. break;
  3401. case SP_ADD_MONSTER_DROP_ITEMGROUP: // bonus2 bAddMonsterDropItemGroup,ig,n;
  3402. if (sd->state.lr_flag != 2)
  3403. pc_bonus_item_drop(sd->add_drop, 0, type2, CLASS_ALL, RC_NONE_, val);
  3404. break;
  3405. case SP_SP_LOSS_RATE: // bonus2 bSPLossRate,n,t;
  3406. if(sd->state.lr_flag != 2) {
  3407. sd->sp_loss.value = type2;
  3408. sd->sp_loss.rate = val;
  3409. }
  3410. break;
  3411. case SP_SP_REGEN_RATE: // bonus2 bSPRegenRate,n,t;
  3412. if(sd->state.lr_flag != 2) {
  3413. sd->sp_regen.value = type2;
  3414. sd->sp_regen.rate = val;
  3415. }
  3416. break;
  3417. case SP_HP_DRAIN_VALUE_RACE: // bonus2 bHPDrainValueRace,r,n;
  3418. PC_BONUS_CHK_RACE(type2,SP_HP_DRAIN_VALUE_RACE);
  3419. if(!sd->state.lr_flag) {
  3420. sd->right_weapon.hp_drain_race[type2] += val;
  3421. }
  3422. else if(sd->state.lr_flag == 1) {
  3423. sd->left_weapon.hp_drain_race[type2] += val;
  3424. }
  3425. break;
  3426. case SP_SP_DRAIN_VALUE_RACE: // bonus2 bSPDrainValueRace,r,n;
  3427. PC_BONUS_CHK_RACE(type2,SP_SP_DRAIN_VALUE_RACE);
  3428. if(!sd->state.lr_flag) {
  3429. sd->right_weapon.sp_drain_race[type2] += val;
  3430. }
  3431. else if(sd->state.lr_flag == 1) {
  3432. sd->left_weapon.sp_drain_race[type2] += val;
  3433. }
  3434. break;
  3435. case SP_HP_DRAIN_VALUE_CLASS: // bonus2 bHPDrainValueClass,c,n;
  3436. PC_BONUS_CHK_CLASS(type2,SP_HP_DRAIN_VALUE_CLASS);
  3437. if(!sd->state.lr_flag) {
  3438. sd->right_weapon.hp_drain_class[type2] += val;
  3439. }
  3440. else if(sd->state.lr_flag == 1) {
  3441. sd->left_weapon.hp_drain_class[type2] += val;
  3442. }
  3443. break;
  3444. case SP_SP_DRAIN_VALUE_CLASS: // bonus2 bSPDrainValueClass,c,n;
  3445. PC_BONUS_CHK_CLASS(type2,SP_SP_DRAIN_VALUE_CLASS);
  3446. if(!sd->state.lr_flag) {
  3447. sd->right_weapon.sp_drain_class[type2] += val;
  3448. }
  3449. else if(sd->state.lr_flag == 1) {
  3450. sd->left_weapon.sp_drain_class[type2] += val;
  3451. }
  3452. break;
  3453. case SP_IGNORE_MDEF_RACE_RATE: // bonus2 bIgnoreMdefRaceRate,r,n;
  3454. PC_BONUS_CHK_RACE(type2,SP_IGNORE_MDEF_RACE_RATE);
  3455. if(sd->state.lr_flag != 2)
  3456. sd->ignore_mdef_by_race[type2] += val;
  3457. break;
  3458. case SP_IGNORE_MDEF_CLASS_RATE: // bonus2 bIgnoreMdefClassRate,c,n;
  3459. PC_BONUS_CHK_CLASS(type2,SP_IGNORE_MDEF_CLASS_RATE);
  3460. if(sd->state.lr_flag != 2)
  3461. sd->ignore_mdef_by_class[type2] += val;
  3462. break;
  3463. case SP_IGNORE_DEF_RACE_RATE: // bonus2 bIgnoreDefRaceRate,r,n;
  3464. PC_BONUS_CHK_RACE(type2,SP_IGNORE_DEF_RACE_RATE);
  3465. if(sd->state.lr_flag != 2)
  3466. sd->ignore_def_by_race[type2] += val;
  3467. break;
  3468. case SP_IGNORE_DEF_CLASS_RATE: // bonus2 bIgnoreDefClassRate,r,n;
  3469. PC_BONUS_CHK_CLASS(type2, SP_IGNORE_DEF_CLASS_RATE);
  3470. if (sd->state.lr_flag != 2)
  3471. sd->ignore_def_by_class[type2] += val;
  3472. break;
  3473. case SP_SKILL_USE_SP_RATE: // bonus2 bSkillUseSPrate,sk,n;
  3474. if(sd->state.lr_flag == 2)
  3475. break;
  3476. if (sd->skillusesprate.size() == MAX_PC_BONUS) {
  3477. 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);
  3478. break;
  3479. }
  3480. pc_bonus_itembonus(sd->skillusesprate, type2, val, true);
  3481. break;
  3482. case SP_SKILL_DELAY:
  3483. if(sd->state.lr_flag == 2)
  3484. break;
  3485. if (sd->skilldelay.size() == MAX_PC_BONUS) {
  3486. 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);
  3487. break;
  3488. }
  3489. pc_bonus_itembonus(sd->skilldelay, type2, val, false);
  3490. break;
  3491. case SP_SKILL_COOLDOWN: // bonus2 bSkillCooldown,sk,t;
  3492. if(sd->state.lr_flag == 2)
  3493. break;
  3494. if (sd->skillcooldown.size() == MAX_PC_BONUS) {
  3495. 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);
  3496. break;
  3497. }
  3498. pc_bonus_itembonus(sd->skillcooldown, type2, val, false);
  3499. break;
  3500. case SP_SKILL_VARIABLECAST: // bonus2 bSkillVariableCast,sk,t;
  3501. if (sd->state.lr_flag == 2)
  3502. break;
  3503. if (sd->skillvarcast.size() == MAX_PC_BONUS) {
  3504. 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);
  3505. break;
  3506. }
  3507. pc_bonus_itembonus(sd->skillvarcast, type2, val, false);
  3508. break;
  3509. #ifdef RENEWAL_CAST
  3510. case SP_SKILL_FIXEDCAST: // bonus2 bSkillFixedCast,sk,t;
  3511. if(sd->state.lr_flag == 2)
  3512. break;
  3513. if (sd->skillfixcast.size() == MAX_PC_BONUS) {
  3514. 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);
  3515. break;
  3516. }
  3517. pc_bonus_itembonus(sd->skillfixcast, type2, val, false);
  3518. break;
  3519. case SP_CASTRATE: // bonus2 bCastrate,sk,n;
  3520. case SP_VARCASTRATE: // bonus2 bVariableCastrate,sk,n;
  3521. if(sd->state.lr_flag == 2)
  3522. break;
  3523. if (sd->skillcastrate.size() == MAX_PC_BONUS) {
  3524. ShowWarning("pc_bonus2: SP_VARCASTRATE: Reached max (%d) number of skills per character, bonus skill %d (%d%%) lost.\n", MAX_PC_BONUS, type2, val);
  3525. break;
  3526. }
  3527. pc_bonus_itembonus(sd->skillcastrate, type2, -val, true); // Send inversed value here
  3528. break;
  3529. case SP_FIXCASTRATE: // bonus2 bFixedCastrate,sk,n;
  3530. if(sd->state.lr_flag == 2)
  3531. break;
  3532. if (sd->skillfixcastrate.size() == MAX_PC_BONUS) {
  3533. ShowWarning("pc_bonus2: SP_FIXCASTRATE: Reached max (%d) number of skills per character, bonus skill %d (%d%%) lost.\n", MAX_PC_BONUS, type2, val);
  3534. break;
  3535. }
  3536. pc_bonus_itembonus(sd->skillfixcastrate, type2, -val, true); // Send inversed value here
  3537. break;
  3538. #else
  3539. case SP_SKILL_FIXEDCAST: // bonus2 bSkillFixedCast,sk,t;
  3540. case SP_FIXCASTRATE: // bonus2 bFixedCastrate,sk,n;
  3541. //ShowWarning("pc_bonus2: Non-RENEWAL_CAST doesn't support this bonus %d.\n", type);
  3542. break;
  3543. case SP_VARCASTRATE: // bonus2 bVariableCastrate,sk,n;
  3544. case SP_CASTRATE: // bonus2 bCastrate,sk,n;
  3545. if(sd->state.lr_flag == 2)
  3546. break;
  3547. if (sd->skillcastrate.size() == MAX_PC_BONUS) { //Better mention this so the array length can be updated. [Skotlex]
  3548. 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);
  3549. break;
  3550. }
  3551. pc_bonus_itembonus(sd->skillcastrate, type2, val, true);
  3552. break;
  3553. #endif
  3554. case SP_SKILL_USE_SP: // bonus2 bSkillUseSP,sk,n;
  3555. if(sd->state.lr_flag == 2)
  3556. break;
  3557. if (sd->skillusesp.size() == MAX_PC_BONUS) {
  3558. 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);
  3559. break;
  3560. }
  3561. pc_bonus_itembonus(sd->skillusesp, type2, val, false);
  3562. break;
  3563. case SP_SUB_SKILL: // bonus2 bSubSkill,sk,n;
  3564. if (sd->subskill.size() == MAX_PC_BONUS) {
  3565. 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);
  3566. break;
  3567. }
  3568. pc_bonus_itembonus(sd->subskill, type2, val, false);
  3569. break;
  3570. case SP_SUBDEF_ELE: // bonus2 bSubDefEle,e,x;
  3571. PC_BONUS_CHK_ELEMENT(type2,SP_SUBDEF_ELE);
  3572. sd->subdefele[type2] += val;
  3573. break;
  3574. case SP_COMA_CLASS: // bonus2 bComaClass,c,n;
  3575. PC_BONUS_CHK_CLASS(type2,SP_COMA_CLASS);
  3576. sd->coma_class[type2] += val;
  3577. sd->special_state.bonus_coma = 1;
  3578. break;
  3579. case SP_COMA_RACE: // bonus2 bComaRace,r,n;
  3580. PC_BONUS_CHK_RACE(type2,SP_COMA_RACE);
  3581. sd->coma_race[type2] += val;
  3582. sd->special_state.bonus_coma = 1;
  3583. break;
  3584. case SP_MAGIC_ADDRACE2: // bonus2 bMagicAddRace2,mr,n;
  3585. PC_BONUS_CHK_RACE2(type2, SP_MAGIC_ADDRACE2);
  3586. if(sd->state.lr_flag != 2)
  3587. sd->magic_addrace2[type2] += val;
  3588. break;
  3589. case SP_IGNORE_MDEF_RACE2_RATE: //bonus2 bIgnoreMdefRace2Rate,mr,n;
  3590. PC_BONUS_CHK_RACE2(type2, SP_IGNORE_MDEF_RACE2);
  3591. if (sd->state.lr_flag != 2)
  3592. sd->ignore_mdef_by_race2[type2] += val;
  3593. break;
  3594. case SP_DROP_ADDRACE: // bonus2 bDropAddRace,r,x;
  3595. PC_BONUS_CHK_RACE(type2, SP_DROP_ADDRACE);
  3596. if (sd->state.lr_flag != 2)
  3597. sd->dropaddrace[type2] += val;
  3598. break;
  3599. case SP_DROP_ADDCLASS: // bonus2 bDropAddClass,c,x;
  3600. PC_BONUS_CHK_CLASS(type2, SP_DROP_ADDCLASS);
  3601. if (sd->state.lr_flag != 2)
  3602. sd->dropaddclass[type2] += val;
  3603. break;
  3604. default:
  3605. if (running_npc_stat_calc_event) {
  3606. ShowWarning("pc_bonus2: unknown bonus type %d %d %d in OnPCStatCalcEvent!\n", type, type2, val);
  3607. }
  3608. else if (current_equip_combo_pos > 0) {
  3609. 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);
  3610. }
  3611. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  3612. 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);
  3613. }
  3614. else {
  3615. ShowWarning("pc_bonus2: unknown bonus type %d %d %d in unknown usage. Report this!\n", type, type2, val);
  3616. }
  3617. break;
  3618. }
  3619. }
  3620. /**
  3621. * Gives item bonus to player for format: bonus3 bBonusName,type2,type3,val;
  3622. * @param sd
  3623. * @param type Bonus type used by bBonusName
  3624. * @param type2
  3625. * @param type3
  3626. * @param val Value that usually for rate or fixed value
  3627. */
  3628. void pc_bonus3(struct map_session_data *sd,int type,int type2,int type3,int val)
  3629. {
  3630. nullpo_retv(sd);
  3631. switch(type){
  3632. case SP_ADD_MONSTER_DROP_ITEM: // bonus3 bAddMonsterDropItem,iid,r,n;
  3633. if(sd->state.lr_flag != 2)
  3634. pc_bonus_item_drop(sd->add_drop, type2, 0, CLASS_NONE, type3, val);
  3635. break;
  3636. case SP_ADD_MONSTER_ID_DROP_ITEM: // bonus3 bAddMonsterIdDropItem,iid,mid,n;
  3637. if(sd->state.lr_flag != 2)
  3638. pc_bonus_item_drop(sd->add_drop, type2, 0, CLASS_NONE, -type3, val);
  3639. break;
  3640. case SP_ADD_CLASS_DROP_ITEM: // bonus3 bAddClassDropItem,iid,c,n;
  3641. if(sd->state.lr_flag != 2)
  3642. pc_bonus_item_drop(sd->add_drop, type2, 0, type3, RC_NONE_, val);
  3643. break;
  3644. case SP_AUTOSPELL: // bonus3 bAutoSpell,sk,y,n;
  3645. if(sd->state.lr_flag != 2)
  3646. {
  3647. int target = skill_get_inf(type2); //Support or Self (non-auto-target) skills should pick self.
  3648. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !skill_get_inf2(type2, INF2_NOTARGETSELF));
  3649. pc_bonus_autospell(sd->autospell, target?-type2:type2, type3, val, 0, current_equip_card_id);
  3650. }
  3651. break;
  3652. case SP_AUTOSPELL_WHENHIT: // bonus3 bAutoSpellWhenHit,sk,y,n;
  3653. if(sd->state.lr_flag != 2)
  3654. {
  3655. int target = skill_get_inf(type2); //Support or Self (non-auto-target) skills should pick self.
  3656. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !skill_get_inf2(type2, INF2_NOTARGETSELF));
  3657. pc_bonus_autospell(sd->autospell2, target?-type2:type2, type3, val, BF_NORMAL|BF_SKILL, current_equip_card_id);
  3658. }
  3659. break;
  3660. case SP_ADD_MONSTER_DROP_ITEMGROUP: // bonus3 bAddMonsterDropItemGroup,ig,r,n;
  3661. if (sd->state.lr_flag != 2)
  3662. pc_bonus_item_drop(sd->add_drop, 0, type2, CLASS_NONE, type3, val);
  3663. break;
  3664. case SP_ADD_CLASS_DROP_ITEMGROUP: // bonus3 bAddClassDropItemGroup,ig,c,n;
  3665. if (sd->state.lr_flag != 2)
  3666. pc_bonus_item_drop(sd->add_drop, 0, type2, type3, RC_NONE_, val);
  3667. break;
  3668. case SP_ADDEFF: // bonus3 bAddEff,eff,n,y;
  3669. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  3670. pc_bonus_addeff(sd->addeff, (sc_type)type2, sd->state.lr_flag != 2 ? type3 : 0, sd->state.lr_flag == 2 ? type3 : 0, val, 0);
  3671. break;
  3672. case SP_ADDEFF_WHENHIT: // bonus3 bAddEffWhenHit,eff,n,y;
  3673. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  3674. if(sd->state.lr_flag != 2)
  3675. pc_bonus_addeff(sd->addeff_atked, (sc_type)type2, type3, 0, val, 0);
  3676. break;
  3677. case SP_ADDEFF_ONSKILL: // bonus3 bAddEffOnSkill,sk,eff,n;
  3678. PC_BONUS_CHK_SC(type2,SP_ADDEFF_ONSKILL);
  3679. if( sd->state.lr_flag != 2 )
  3680. pc_bonus_addeff_onskill(sd->addeff_onskill, (sc_type)type3, val, type2, ATF_TARGET, 0);
  3681. break;
  3682. case SP_ADDELE: // bonus3 bAddEle,e,x,bf;
  3683. PC_BONUS_CHK_ELEMENT(type2,SP_ADDELE);
  3684. if (sd->state.lr_flag != 2)
  3685. pc_bonus_addele(sd, (unsigned char)type2, type3, val);
  3686. break;
  3687. case SP_SUBELE: // bonus3 bSubEle,e,x,bf;
  3688. PC_BONUS_CHK_ELEMENT(type2,SP_SUBELE);
  3689. if (sd->state.lr_flag != 2)
  3690. pc_bonus_subele(sd, (unsigned char)type2, type3, val);
  3691. break;
  3692. case SP_SP_VANISH_RACE_RATE: // bonus3 bSPVanishRaceRate,r,x,n;
  3693. PC_BONUS_CHK_RACE(type2,SP_SP_VANISH_RACE_RATE);
  3694. if(sd->state.lr_flag != 2) {
  3695. sd->sp_vanish_race[type2].rate += type3;
  3696. sd->sp_vanish_race[type2].per += val;
  3697. }
  3698. break;
  3699. case SP_HP_VANISH_RACE_RATE: // bonus3 bHPVanishRaceRate,r,x,n;
  3700. PC_BONUS_CHK_RACE(type2,SP_HP_VANISH_RACE_RATE);
  3701. if(sd->state.lr_flag != 2) {
  3702. sd->hp_vanish_race[type2].rate += type3;
  3703. sd->hp_vanish_race[type2].per += val;
  3704. }
  3705. break;
  3706. case SP_SP_VANISH_RATE: // bonus3 bSPVanishRate,x,n,bf;
  3707. if(sd->state.lr_flag != 2) {
  3708. pc_bonus_addvanish(sd->sp_vanish, type2, type3, val);
  3709. }
  3710. break;
  3711. case SP_HP_VANISH_RATE: // bonus3 bHPVanishRate,x,n,bf;
  3712. if(sd->state.lr_flag != 2) {
  3713. pc_bonus_addvanish(sd->hp_vanish, type2, type3, val);
  3714. }
  3715. break;
  3716. case SP_STATE_NORECOVER_RACE: // bonus3 bStateNoRecoverRace,r,x,t;
  3717. PC_BONUS_CHK_RACE(type2, SP_STATE_NORECOVER_RACE);
  3718. if (sd->state.lr_flag == 2)
  3719. break;
  3720. //! CONFIRM: Is it not stackable? Does not check max or min value?
  3721. //if (type3 > sd->norecover_state_race[type2].rate) {
  3722. // sd->norecover_state_race[type2].rate = type3;
  3723. // sd->norecover_state_race[type2].tick = val;
  3724. // break;
  3725. //}
  3726. sd->norecover_state_race[type2].rate = type3;
  3727. sd->norecover_state_race[type2].tick = val;
  3728. break;
  3729. default:
  3730. if (running_npc_stat_calc_event) {
  3731. ShowWarning("pc_bonus3: unknown bonus type %d %d %d %d in OnPCStatCalcEvent!\n", type, type2, type3, val);
  3732. }
  3733. else if (current_equip_combo_pos > 0) {
  3734. 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);
  3735. }
  3736. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  3737. 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);
  3738. }
  3739. else {
  3740. ShowWarning("pc_bonus3: unknown bonus type %d %d %d %d in unknown usage. Report this!\n", type, type2, type3, val);
  3741. }
  3742. break;
  3743. }
  3744. }
  3745. /**
  3746. * Gives item bonus to player for format: bonus4 bBonusName,type2,type3,type4,val;
  3747. * @param sd
  3748. * @param type Bonus type used by bBonusName
  3749. * @param type2
  3750. * @param type3
  3751. * @param type4
  3752. * @param val Value that usually for rate or fixed value
  3753. */
  3754. void pc_bonus4(struct map_session_data *sd,int type,int type2,int type3,int type4,int val)
  3755. {
  3756. nullpo_retv(sd);
  3757. switch(type){
  3758. case SP_AUTOSPELL: // bonus4 bAutoSpell,sk,y,n,i;
  3759. if(sd->state.lr_flag != 2)
  3760. pc_bonus_autospell(sd->autospell, (val&1?type2:-type2), (val&2?-type3:type3), type4, 0, current_equip_card_id);
  3761. break;
  3762. case SP_AUTOSPELL_WHENHIT: // bonus4 bAutoSpellWhenHit,sk,y,n,i;
  3763. if(sd->state.lr_flag != 2)
  3764. pc_bonus_autospell(sd->autospell2, (val&1?type2:-type2), (val&2?-type3:type3), type4, BF_NORMAL|BF_SKILL, current_equip_card_id);
  3765. break;
  3766. case SP_AUTOSPELL_ONSKILL: // bonus4 bAutoSpellOnSkill,sk,x,y,n;
  3767. if(sd->state.lr_flag != 2)
  3768. {
  3769. int target = skill_get_inf(type3); //Support or Self (non-auto-target) skills should pick self.
  3770. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !skill_get_inf2(type3, INF2_NOTARGETSELF));
  3771. pc_bonus_autospell_onskill(sd->autospell3, type2, target?-type3:type3, type4, val, current_equip_card_id);
  3772. }
  3773. break;
  3774. case SP_ADDEFF: // bonus4 bAddEff,eff,n,y,t;
  3775. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  3776. pc_bonus_addeff(sd->addeff, (sc_type)type2, sd->state.lr_flag != 2 ? type3 : 0, sd->state.lr_flag == 2 ? type3 : 0, type4, val);
  3777. break;
  3778. case SP_ADDEFF_WHENHIT: // bonus4 bAddEffWhenHit,eff,n,y,t;
  3779. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  3780. if (sd->state.lr_flag != 2)
  3781. pc_bonus_addeff(sd->addeff_atked, (sc_type)type2, type3, 0, type4, val);
  3782. break;
  3783. case SP_ADDEFF_ONSKILL: // bonus4 bAddEffOnSkill,sk,eff,n,y;
  3784. PC_BONUS_CHK_SC(type3,SP_ADDEFF_ONSKILL);
  3785. if( sd->state.lr_flag != 2 )
  3786. pc_bonus_addeff_onskill(sd->addeff_onskill, (sc_type)type3, type4, type2, val, 0);
  3787. break;
  3788. case SP_SET_DEF_RACE: // bonus4 bSetDefRace,r,n,t,y;
  3789. PC_BONUS_CHK_RACE(type2,SP_SET_DEF_RACE);
  3790. if(sd->state.lr_flag == 2)
  3791. break;
  3792. sd->def_set_race[type2].rate = type3;
  3793. sd->def_set_race[type2].tick = type4;
  3794. sd->def_set_race[type2].value = val;
  3795. break;
  3796. case SP_SET_MDEF_RACE: // bonus4 bSetMDefRace,r,n,t,y;
  3797. PC_BONUS_CHK_RACE(type2,SP_SET_MDEF_RACE);
  3798. if(sd->state.lr_flag == 2)
  3799. break;
  3800. sd->mdef_set_race[type2].rate = type3;
  3801. sd->mdef_set_race[type2].tick = type4;
  3802. sd->mdef_set_race[type2].value = val;
  3803. break;
  3804. default:
  3805. if (running_npc_stat_calc_event) {
  3806. ShowWarning("pc_bonus4: unknown bonus type %d %d %d %d %d in OnPCStatCalcEvent!\n", type, type2, type3, type4, val);
  3807. }
  3808. else if (current_equip_combo_pos > 0) {
  3809. 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);
  3810. }
  3811. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  3812. 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);
  3813. }
  3814. else {
  3815. ShowWarning("pc_bonus4: unknown bonus type %d %d %d %d %d in unknown usage. Report this!\n", type, type2, type3, type4, val);
  3816. }
  3817. break;
  3818. }
  3819. }
  3820. /**
  3821. * Gives item bonus to player for format: bonus5 bBonusName,type2,type3,type4,val;
  3822. * @param sd
  3823. * @param type Bonus type used by bBonusName
  3824. * @param type2
  3825. * @param type3
  3826. * @param type4
  3827. * @param val Value that usually for rate or fixed value
  3828. */
  3829. void pc_bonus5(struct map_session_data *sd,int type,int type2,int type3,int type4,int type5,int val)
  3830. {
  3831. nullpo_retv(sd);
  3832. switch(type){
  3833. case SP_AUTOSPELL: // bonus5 bAutoSpell,sk,y,n,bf,i;
  3834. if(sd->state.lr_flag != 2)
  3835. pc_bonus_autospell(sd->autospell, (val&1?type2:-type2), (val&2?-type3:type3), type4, type5, current_equip_card_id);
  3836. break;
  3837. case SP_AUTOSPELL_WHENHIT: // bonus5 bAutoSpellWhenHit,sk,y,n,bf,i;
  3838. if(sd->state.lr_flag != 2)
  3839. pc_bonus_autospell(sd->autospell2, (val&1?type2:-type2), (val&2?-type3:type3), type4, type5, current_equip_card_id);
  3840. break;
  3841. case SP_AUTOSPELL_ONSKILL: // bonus5 bAutoSpellOnSkill,sk,x,y,n,i;
  3842. if(sd->state.lr_flag != 2)
  3843. pc_bonus_autospell_onskill(sd->autospell3, type2, (val&1?-type3:type3), (val&2?-type4:type4), type5, current_equip_card_id);
  3844. break;
  3845. case SP_ADDEFF_ONSKILL: // bonus5 bAddEffOnSkill,sk,eff,n,y,t;
  3846. PC_BONUS_CHK_SC(type3,SP_ADDEFF_ONSKILL);
  3847. if( sd->state.lr_flag != 2 )
  3848. pc_bonus_addeff_onskill(sd->addeff_onskill, (sc_type)type3, type4, type2, type5, val);
  3849. break;
  3850. default:
  3851. if (running_npc_stat_calc_event) {
  3852. ShowWarning("pc_bonus5: unknown bonus type %d %d %d %d %d %d in OnPCStatCalcEvent!\n", type, type2, type3, type4, type5, val);
  3853. }
  3854. else if (current_equip_combo_pos > 0) {
  3855. 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);
  3856. }
  3857. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  3858. 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);
  3859. }
  3860. else {
  3861. ShowWarning("pc_bonus5: unknown bonus type %d %d %d %d %d %d in unknown usage. Report this!\n", type, type2, type3, type4, type5, val);
  3862. }
  3863. break;
  3864. }
  3865. }
  3866. /*==========================================
  3867. * Grants a player a given skill. Flag values are:
  3868. * 0 - Grant permanent skill to be bound to skill tree
  3869. * 1 - Grant an item skill (temporary)
  3870. * 2 - Like 1, except the level granted can stack with previously learned level.
  3871. * 4 - Like 0, except the skill will ignore skill tree (saves through job changes and resets).
  3872. *------------------------------------------*/
  3873. bool pc_skill(struct map_session_data* sd, uint16 skill_id, int level, enum e_addskill_type type) {
  3874. uint16 idx = 0;
  3875. nullpo_ret(sd);
  3876. if (!skill_id || !(idx = skill_get_index(skill_id))) {
  3877. ShowError("pc_skill: Skill with id %d does not exist in the skill database\n", skill_id);
  3878. return false;
  3879. }
  3880. if (level > MAX_SKILL_LEVEL) {
  3881. ShowError("pc_skill: Skill level %d too high. Max lv supported is %d\n", level, MAX_SKILL_LEVEL);
  3882. return false;
  3883. }
  3884. if (type == ADDSKILL_TEMP_ADDLEVEL && sd->status.skill[idx].lv + level > MAX_SKILL_LEVEL) {
  3885. 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);
  3886. level = MAX_SKILL_LEVEL - sd->status.skill[idx].lv;
  3887. }
  3888. switch (type) {
  3889. case ADDSKILL_PERMANENT: //Set skill data overwriting whatever was there before.
  3890. sd->status.skill[idx].id = skill_id;
  3891. sd->status.skill[idx].lv = level;
  3892. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  3893. if (level == 0) { //Remove skill.
  3894. sd->status.skill[idx].id = 0;
  3895. clif_deleteskill(sd,skill_id);
  3896. } else
  3897. clif_addskill(sd,skill_id);
  3898. if (!skill_get_inf(skill_id)) //Only recalculate for passive skills.
  3899. status_calc_pc(sd, SCO_NONE);
  3900. break;
  3901. case ADDSKILL_TEMP: //Item bonus skill.
  3902. if (sd->status.skill[idx].id != 0) {
  3903. if (sd->status.skill[idx].lv >= level)
  3904. return true;
  3905. if (sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT) //Non-granted skill, store it's level.
  3906. sd->status.skill[idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[idx].lv;
  3907. } else {
  3908. sd->status.skill[idx].id = skill_id;
  3909. sd->status.skill[idx].flag = SKILL_FLAG_TEMPORARY;
  3910. }
  3911. sd->status.skill[idx].lv = level;
  3912. break;
  3913. case ADDSKILL_TEMP_ADDLEVEL: //Add skill bonus on top of what you had.
  3914. if (sd->status.skill[idx].id != 0) {
  3915. if (sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT)
  3916. sd->status.skill[idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[idx].lv; // Store previous level.
  3917. } else {
  3918. sd->status.skill[idx].id = skill_id;
  3919. sd->status.skill[idx].flag = SKILL_FLAG_TEMPORARY; //Set that this is a bonus skill.
  3920. }
  3921. sd->status.skill[idx].lv += level;
  3922. break;
  3923. case ADDSKILL_PERMANENT_GRANTED: //Permanent granted skills ignore the skill tree
  3924. sd->status.skill[idx].id = skill_id;
  3925. sd->status.skill[idx].lv = level;
  3926. sd->status.skill[idx].flag = SKILL_FLAG_PERM_GRANTED;
  3927. if (level == 0) { //Remove skill.
  3928. sd->status.skill[idx].id = 0;
  3929. clif_deleteskill(sd,skill_id);
  3930. } else
  3931. clif_addskill(sd,skill_id);
  3932. if (!skill_get_inf(skill_id)) //Only recalculate for passive skills.
  3933. status_calc_pc(sd, SCO_NONE);
  3934. break;
  3935. default:
  3936. return false;
  3937. }
  3938. return true;
  3939. }
  3940. /*==========================================
  3941. * Append a card to an item ?
  3942. *------------------------------------------*/
  3943. int pc_insert_card(struct map_session_data* sd, int idx_card, int idx_equip)
  3944. {
  3945. int i;
  3946. unsigned short nameid;
  3947. nullpo_ret(sd);
  3948. if( idx_equip < 0 || idx_equip >= MAX_INVENTORY || sd->inventory_data[idx_equip] == NULL )
  3949. return 0; //Invalid item index.
  3950. if( idx_card < 0 || idx_card >= MAX_INVENTORY || sd->inventory_data[idx_card] == NULL )
  3951. return 0; //Invalid card index.
  3952. if( sd->inventory.u.items_inventory[idx_equip].nameid <= 0 || sd->inventory.u.items_inventory[idx_equip].amount < 1 )
  3953. return 0; // target item missing
  3954. if( sd->inventory.u.items_inventory[idx_card].nameid <= 0 || sd->inventory.u.items_inventory[idx_card].amount < 1 )
  3955. return 0; // target card missing
  3956. if( sd->inventory_data[idx_equip]->type != IT_WEAPON && sd->inventory_data[idx_equip]->type != IT_ARMOR )
  3957. return 0; // only weapons and armor are allowed
  3958. if( sd->inventory_data[idx_card]->type != IT_CARD )
  3959. return 0; // must be a card
  3960. if( sd->inventory.u.items_inventory[idx_equip].identify == 0 )
  3961. return 0; // target must be identified
  3962. if( itemdb_isspecial(sd->inventory.u.items_inventory[idx_equip].card[0]) )
  3963. return 0; // card slots reserved for other purposes
  3964. if( (sd->inventory_data[idx_equip]->equip & sd->inventory_data[idx_card]->equip) == 0 )
  3965. return 0; // card cannot be compounded on this item type
  3966. if( sd->inventory_data[idx_equip]->type == IT_WEAPON && sd->inventory_data[idx_card]->equip == EQP_SHIELD )
  3967. return 0; // attempted to place shield card on left-hand weapon.
  3968. if( sd->inventory.u.items_inventory[idx_equip].equip != 0 )
  3969. return 0; // item must be unequipped
  3970. ARR_FIND( 0, sd->inventory_data[idx_equip]->slot, i, sd->inventory.u.items_inventory[idx_equip].card[i] == 0 );
  3971. if( i == sd->inventory_data[idx_equip]->slot )
  3972. return 0; // no free slots
  3973. // remember the card id to insert
  3974. nameid = sd->inventory.u.items_inventory[idx_card].nameid;
  3975. if( pc_delitem(sd,idx_card,1,1,0,LOG_TYPE_OTHER) == 1 )
  3976. {// failed
  3977. clif_insert_card(sd,idx_equip,idx_card,1);
  3978. }
  3979. else
  3980. {// success
  3981. log_pick_pc(sd, LOG_TYPE_OTHER, -1, &sd->inventory.u.items_inventory[idx_equip]);
  3982. sd->inventory.u.items_inventory[idx_equip].card[i] = nameid;
  3983. log_pick_pc(sd, LOG_TYPE_OTHER, 1, &sd->inventory.u.items_inventory[idx_equip]);
  3984. clif_insert_card(sd,idx_equip,idx_card,0);
  3985. }
  3986. return 0;
  3987. }
  3988. /**
  3989. * Returns the count of unidentified items with the option to identify too.
  3990. * @param sd: Player data
  3991. * @param identify_item: Whether or not to identify any unidentified items
  3992. * @return Unidentified items count
  3993. */
  3994. int pc_identifyall(struct map_session_data *sd, bool identify_item)
  3995. {
  3996. int unidentified_count = 0;
  3997. for (int i = 0; i < MAX_INVENTORY; i++) {
  3998. if (sd->inventory.u.items_inventory[i].nameid > 0 && sd->inventory.u.items_inventory[i].identify != 1) {
  3999. if (identify_item == true) {
  4000. sd->inventory.u.items_inventory[i].identify = 1;
  4001. clif_item_identified(sd,i,0);
  4002. }
  4003. unidentified_count++;
  4004. }
  4005. }
  4006. return unidentified_count;
  4007. }
  4008. //
  4009. // Items
  4010. //
  4011. /*==========================================
  4012. * Update buying value by skills
  4013. *------------------------------------------*/
  4014. int pc_modifybuyvalue(struct map_session_data *sd,int orig_value)
  4015. {
  4016. int skill,val = orig_value,rate1 = 0,rate2 = 0;
  4017. if((skill=pc_checkskill(sd,MC_DISCOUNT))>0) // merchant discount
  4018. rate1 = 5+skill*2-((skill==10)? 1:0);
  4019. if((skill=pc_checkskill(sd,RG_COMPULSION))>0) // rogue discount
  4020. rate2 = 5+skill*4;
  4021. if(rate1 < rate2) rate1 = rate2;
  4022. if(rate1)
  4023. val = (int)((double)orig_value*(double)(100-rate1)/100.);
  4024. if(val < battle_config.min_shop_buy)
  4025. val = battle_config.min_shop_buy;
  4026. return val;
  4027. }
  4028. /*==========================================
  4029. * Update selling value by skills
  4030. *------------------------------------------*/
  4031. int pc_modifysellvalue(struct map_session_data *sd,int orig_value)
  4032. {
  4033. int skill,val = orig_value,rate = 0;
  4034. if((skill=pc_checkskill(sd,MC_OVERCHARGE))>0) //OverCharge
  4035. rate = 5+skill*2-((skill==10)? 1:0);
  4036. if(rate)
  4037. val = (int)((double)orig_value*(double)(100+rate)/100.);
  4038. if (val < battle_config.min_shop_sell)
  4039. val = battle_config.min_shop_sell;
  4040. return val;
  4041. }
  4042. /*==========================================
  4043. * Checking if we have enough place on inventory for new item
  4044. * Make sure to take 30k as limit (for client I guess)
  4045. * @param sd
  4046. * @param nameid
  4047. * @param amount
  4048. * @return e_chkitem_result
  4049. *------------------------------------------*/
  4050. char pc_checkadditem(struct map_session_data *sd, unsigned short nameid, int amount)
  4051. {
  4052. int i;
  4053. struct item_data* data;
  4054. nullpo_ret(sd);
  4055. if(amount > MAX_AMOUNT)
  4056. return CHKADDITEM_OVERAMOUNT;
  4057. data = itemdb_search(nameid);
  4058. if(!itemdb_isstackable2(data))
  4059. return CHKADDITEM_NEW;
  4060. if( data->stack.inventory && amount > data->stack.amount )
  4061. return CHKADDITEM_OVERAMOUNT;
  4062. if (data->flag.guid)
  4063. return CHKADDITEM_NEW;
  4064. for(i=0;i<MAX_INVENTORY;i++){
  4065. // FIXME: This does not consider the checked item's cards, thus could check a wrong slot for stackability.
  4066. if(sd->inventory.u.items_inventory[i].nameid == nameid){
  4067. 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 ) )
  4068. return CHKADDITEM_OVERAMOUNT;
  4069. return CHKADDITEM_EXIST;
  4070. }
  4071. }
  4072. return CHKADDITEM_NEW;
  4073. }
  4074. /*==========================================
  4075. * Return number of available place in inventory
  4076. * Each non stackable item will reduce place by 1
  4077. * @param sd
  4078. * @return Number of empty slots
  4079. *------------------------------------------*/
  4080. uint8 pc_inventoryblank(struct map_session_data *sd)
  4081. {
  4082. uint8 i, b;
  4083. nullpo_ret(sd);
  4084. for(i = 0, b = 0; i < MAX_INVENTORY; i++){
  4085. if(sd->inventory.u.items_inventory[i].nameid == 0)
  4086. b++;
  4087. }
  4088. return b;
  4089. }
  4090. /**
  4091. * Attempts to remove zeny from player
  4092. * @param sd: Player
  4093. * @param zeny: Zeny removed
  4094. * @param type: Log type
  4095. * @param tsd: (optional) From who to log (if null take sd)
  4096. * @return 0: Success, 1: Failed (Removing negative Zeny or not enough Zeny), 2: Player not found
  4097. */
  4098. char pc_payzeny(struct map_session_data *sd, int zeny, enum e_log_pick_type type, struct map_session_data *tsd)
  4099. {
  4100. nullpo_retr(2,sd);
  4101. zeny = cap_value(zeny,-MAX_ZENY,MAX_ZENY); //prevent command UB
  4102. if( zeny < 0 )
  4103. {
  4104. ShowError("pc_payzeny: Paying negative Zeny (zeny=%d, account_id=%d, char_id=%d).\n", zeny, sd->status.account_id, sd->status.char_id);
  4105. return 1;
  4106. }
  4107. if( sd->status.zeny < zeny )
  4108. return 1; //Not enough.
  4109. sd->status.zeny -= zeny;
  4110. clif_updatestatus(sd,SP_ZENY);
  4111. if(!tsd) tsd = sd;
  4112. log_zeny(sd, type, tsd, -zeny);
  4113. if( zeny > 0 && sd->state.showzeny ) {
  4114. char output[255];
  4115. sprintf(output, "Removed %dz.", zeny);
  4116. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4117. }
  4118. return 0;
  4119. }
  4120. /**
  4121. * Attempts to give zeny to player
  4122. * @param sd: Player
  4123. * @param type: Log type
  4124. * @param tsd: (optional) From who to log (if null take sd)
  4125. * @return -1: Player not found, 0: Success, 1: Giving negative Zeny
  4126. */
  4127. char pc_getzeny(struct map_session_data *sd, int zeny, enum e_log_pick_type type, struct map_session_data *tsd)
  4128. {
  4129. nullpo_retr(-1,sd);
  4130. zeny = cap_value(zeny,-MAX_ZENY,MAX_ZENY); //prevent command UB
  4131. if( zeny < 0 )
  4132. {
  4133. ShowError("pc_getzeny: Obtaining negative Zeny (zeny=%d, account_id=%d, char_id=%d).\n", zeny, sd->status.account_id, sd->status.char_id);
  4134. return 1;
  4135. }
  4136. if( zeny > MAX_ZENY - sd->status.zeny )
  4137. zeny = MAX_ZENY - sd->status.zeny;
  4138. sd->status.zeny += zeny;
  4139. clif_updatestatus(sd,SP_ZENY);
  4140. if(!tsd) tsd = sd;
  4141. log_zeny(sd, type, tsd, zeny);
  4142. if( zeny > 0 && sd->state.showzeny ) {
  4143. char output[255];
  4144. sprintf(output, "Gained %dz.", zeny);
  4145. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4146. }
  4147. achievement_update_objective(sd, AG_GET_ZENY, 1, sd->status.zeny);
  4148. return 0;
  4149. }
  4150. /**
  4151. * Attempts to remove Cash Points from player
  4152. * @param sd: Player
  4153. * @param price: Total points (cash + kafra) the player has to pay
  4154. * @param points: Kafra points the player has to pay
  4155. * @param type: Log type
  4156. * @return -1: Not enough points, otherwise success (cash+points)
  4157. */
  4158. int pc_paycash(struct map_session_data *sd, int price, int points, e_log_pick_type type)
  4159. {
  4160. int cash;
  4161. nullpo_retr(-1,sd);
  4162. points = cap_value(points, 0, MAX_ZENY); //prevent command UB
  4163. cash = price-points;
  4164. if( sd->cashPoints < cash || sd->kafraPoints < points )
  4165. {
  4166. 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);
  4167. return -1;
  4168. }
  4169. if( cash ){
  4170. pc_setaccountreg(sd, add_str(CASHPOINT_VAR), sd->cashPoints - cash);
  4171. sd->cashPoints -= cash;
  4172. log_cash( sd, type, LOG_CASH_TYPE_CASH, -cash );
  4173. }
  4174. if( points ){
  4175. pc_setaccountreg(sd, add_str(KAFRAPOINT_VAR), sd->kafraPoints - points);
  4176. sd->kafraPoints -= points;
  4177. log_cash( sd, type, LOG_CASH_TYPE_KAFRA, -points );
  4178. }
  4179. if( battle_config.cashshop_show_points )
  4180. {
  4181. char output[CHAT_SIZE_MAX];
  4182. 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.
  4183. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4184. }
  4185. return cash+points;
  4186. }
  4187. /**
  4188. * Attempts to give Cash Points to player
  4189. * @param sd: Player
  4190. * @param cash: Cash points the player gets
  4191. * @param points: Kafra points the player gets
  4192. * @param type: Log type
  4193. * @return -1: Error, otherwise success (cash or points)
  4194. */
  4195. int pc_getcash(struct map_session_data *sd, int cash, int points, e_log_pick_type type)
  4196. {
  4197. char output[CHAT_SIZE_MAX];
  4198. nullpo_retr(-1,sd);
  4199. cash = cap_value(cash, 0, MAX_ZENY); //prevent command UB
  4200. points = cap_value(points, 0, MAX_ZENY); //prevent command UB
  4201. if( cash > 0 )
  4202. {
  4203. if( cash > MAX_ZENY-sd->cashPoints )
  4204. {
  4205. 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);
  4206. cash = MAX_ZENY-sd->cashPoints;
  4207. }
  4208. pc_setaccountreg(sd, add_str(CASHPOINT_VAR), sd->cashPoints+cash);
  4209. sd->cashPoints += cash;
  4210. log_cash( sd, type, LOG_CASH_TYPE_CASH, cash );
  4211. if( battle_config.cashshop_show_points )
  4212. {
  4213. sprintf(output, msg_txt(sd,505), cash, sd->cashPoints);
  4214. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4215. }
  4216. return cash;
  4217. }
  4218. if( points > 0 )
  4219. {
  4220. if( points > MAX_ZENY-sd->kafraPoints )
  4221. {
  4222. 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);
  4223. points = MAX_ZENY-sd->kafraPoints;
  4224. }
  4225. pc_setaccountreg(sd, add_str(KAFRAPOINT_VAR), sd->kafraPoints+points);
  4226. sd->kafraPoints += points;
  4227. log_cash( sd, type, LOG_CASH_TYPE_KAFRA, points );
  4228. if( battle_config.cashshop_show_points )
  4229. {
  4230. sprintf(output, msg_txt(sd,506), points, sd->kafraPoints);
  4231. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4232. }
  4233. return points;
  4234. }
  4235. return -1; //shouldn't happen but just in case
  4236. }
  4237. /**
  4238. * Searching a specified itemid in inventory and return his stored index
  4239. * @param sd Player
  4240. * @param nameid Find this Item!
  4241. * @return Stored index in inventory, or -1 if not found.
  4242. **/
  4243. short pc_search_inventory(struct map_session_data *sd, unsigned short nameid) {
  4244. short i;
  4245. nullpo_retr(-1, sd);
  4246. ARR_FIND( 0, MAX_INVENTORY, i, sd->inventory.u.items_inventory[i].nameid == nameid && (sd->inventory.u.items_inventory[i].amount > 0 || nameid == 0) );
  4247. return ( i < MAX_INVENTORY ) ? i : -1;
  4248. }
  4249. /** Attempt to add a new item to player inventory
  4250. * @param sd
  4251. * @param item_data
  4252. * @param amount
  4253. * @param log_type
  4254. * @return
  4255. * 0 = success
  4256. * 1 = invalid itemid not found or negative amount
  4257. * 2 = overweight
  4258. * 3 = ?
  4259. * 4 = no free place found
  4260. * 5 = max amount reached
  4261. * 6 = ?
  4262. * 7 = stack limitation
  4263. */
  4264. enum e_additem_result pc_additem(struct map_session_data *sd,struct item *item,int amount,e_log_pick_type log_type) {
  4265. struct item_data *id;
  4266. int16 i;
  4267. unsigned int w;
  4268. nullpo_retr(ADDITEM_INVALID, sd);
  4269. nullpo_retr(ADDITEM_INVALID, item);
  4270. if( item->nameid == 0 || amount <= 0 )
  4271. return ADDITEM_INVALID;
  4272. if( amount > MAX_AMOUNT )
  4273. return ADDITEM_OVERAMOUNT;
  4274. id = itemdb_search(item->nameid);
  4275. if( id->stack.inventory && amount > id->stack.amount )
  4276. {// item stack limitation
  4277. return ADDITEM_STACKLIMIT;
  4278. }
  4279. w = id->weight*amount;
  4280. if(sd->weight + w > sd->max_weight)
  4281. return ADDITEM_OVERWEIGHT;
  4282. i = MAX_INVENTORY;
  4283. if (id->flag.guid && !item->unique_id)
  4284. item->unique_id = pc_generate_unique_id(sd);
  4285. // Stackable | Non Rental
  4286. if( itemdb_isstackable2(id) && item->expire_time == 0 ) {
  4287. for( i = 0; i < MAX_INVENTORY; i++ ) {
  4288. if( sd->inventory.u.items_inventory[i].nameid == item->nameid &&
  4289. sd->inventory.u.items_inventory[i].bound == item->bound &&
  4290. sd->inventory.u.items_inventory[i].expire_time == 0 &&
  4291. sd->inventory.u.items_inventory[i].unique_id == item->unique_id &&
  4292. memcmp(&sd->inventory.u.items_inventory[i].card, &item->card, sizeof(item->card)) == 0 ) {
  4293. 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 ) )
  4294. return ADDITEM_OVERAMOUNT;
  4295. sd->inventory.u.items_inventory[i].amount += amount;
  4296. clif_additem(sd,i,amount,0);
  4297. break;
  4298. }
  4299. }
  4300. }
  4301. if (i >= MAX_INVENTORY) {
  4302. i = pc_search_inventory(sd,0);
  4303. if( i < 0 )
  4304. return ADDITEM_OVERITEM;
  4305. memcpy(&sd->inventory.u.items_inventory[i], item, sizeof(sd->inventory.u.items_inventory[0]));
  4306. // clear equip and favorite fields first, just in case
  4307. if( item->equip )
  4308. sd->inventory.u.items_inventory[i].equip = 0;
  4309. if( item->favorite )
  4310. sd->inventory.u.items_inventory[i].favorite = 0;
  4311. if( item->equipSwitch )
  4312. sd->inventory.u.items_inventory[i].equipSwitch = 0;
  4313. sd->inventory.u.items_inventory[i].amount = amount;
  4314. sd->inventory_data[i] = id;
  4315. sd->last_addeditem_index = i;
  4316. if (!itemdb_isstackable2(id) || id->flag.guid)
  4317. sd->inventory.u.items_inventory[i].unique_id = item->unique_id ? item->unique_id : pc_generate_unique_id(sd);
  4318. clif_additem(sd,i,amount,0);
  4319. }
  4320. log_pick_pc(sd, log_type, amount, &sd->inventory.u.items_inventory[i]);
  4321. sd->weight += w;
  4322. clif_updatestatus(sd,SP_WEIGHT);
  4323. //Auto-equip
  4324. if(id->flag.autoequip)
  4325. pc_equipitem(sd, i, id->equip);
  4326. /* rental item check */
  4327. if( item->expire_time ) {
  4328. if( time(NULL) > item->expire_time ) {
  4329. clif_rental_expired(sd->fd, i, sd->inventory.u.items_inventory[i].nameid);
  4330. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 1, 0, LOG_TYPE_OTHER);
  4331. } else {
  4332. unsigned int seconds = (unsigned int)( item->expire_time - time(NULL) );
  4333. clif_rental_time(sd->fd, sd->inventory.u.items_inventory[i].nameid, seconds);
  4334. pc_inventory_rental_add(sd, seconds);
  4335. }
  4336. }
  4337. achievement_update_objective(sd, AG_GET_ITEM, 1, id->value_sell);
  4338. pc_show_questinfo(sd);
  4339. return ADDITEM_SUCCESS;
  4340. }
  4341. /*==========================================
  4342. * Remove an item at index n from inventory by amount.
  4343. * @param sd
  4344. * @param n Item index in inventory
  4345. * @param amount
  4346. * @param type &1: Don't notify deletion; &2 Don't notify weight change; &4 Don't calculate status
  4347. * @param reason Delete reason
  4348. * @param log_type e_log_pick_type
  4349. * @return 1 - invalid itemid or negative amount; 0 - Success
  4350. *------------------------------------------*/
  4351. char pc_delitem(struct map_session_data *sd,int n,int amount,int type, short reason, e_log_pick_type log_type)
  4352. {
  4353. nullpo_retr(1, sd);
  4354. 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)
  4355. return 1;
  4356. log_pick_pc(sd, log_type, -amount, &sd->inventory.u.items_inventory[n]);
  4357. sd->inventory.u.items_inventory[n].amount -= amount;
  4358. sd->weight -= sd->inventory_data[n]->weight*amount ;
  4359. if( sd->inventory.u.items_inventory[n].amount <= 0 ){
  4360. if(sd->inventory.u.items_inventory[n].equip)
  4361. pc_unequipitem(sd,n,2|(!(type&4) ? 1 : 0));
  4362. memset(&sd->inventory.u.items_inventory[n],0,sizeof(sd->inventory.u.items_inventory[0]));
  4363. sd->inventory_data[n] = NULL;
  4364. }
  4365. if(!(type&1))
  4366. clif_delitem(sd,n,amount,reason);
  4367. if(!(type&2))
  4368. clif_updatestatus(sd,SP_WEIGHT);
  4369. pc_show_questinfo(sd);
  4370. return 0;
  4371. }
  4372. /*==========================================
  4373. * Attempt to drop an item.
  4374. * @param sd
  4375. * @param n Item index in inventory
  4376. * @param amount Amount of item
  4377. * @return False = fail; True = success
  4378. *------------------------------------------*/
  4379. bool pc_dropitem(struct map_session_data *sd,int n,int amount)
  4380. {
  4381. nullpo_retr(1, sd);
  4382. if(n < 0 || n >= MAX_INVENTORY)
  4383. return false;
  4384. if(amount <= 0)
  4385. return false;
  4386. if(sd->inventory.u.items_inventory[n].nameid <= 0 ||
  4387. sd->inventory.u.items_inventory[n].amount <= 0 ||
  4388. sd->inventory.u.items_inventory[n].amount < amount ||
  4389. sd->state.trading || sd->state.vending ||
  4390. !sd->inventory_data[n] //pc_delitem would fail on this case.
  4391. )
  4392. return false;
  4393. if( sd->inventory.u.items_inventory[n].equipSwitch )
  4394. return false;
  4395. if( map_getmapflag(sd->bl.m, MF_NODROP) )
  4396. {
  4397. clif_displaymessage (sd->fd, msg_txt(sd,271));
  4398. return false; //Can't drop items in nodrop mapflag maps.
  4399. }
  4400. if( !pc_candrop(sd,&sd->inventory.u.items_inventory[n]) )
  4401. {
  4402. clif_displaymessage (sd->fd, msg_txt(sd,263));
  4403. return false;
  4404. }
  4405. if (!map_addflooritem(&sd->inventory.u.items_inventory[n], amount, sd->bl.m, sd->bl.x, sd->bl.y, 0, 0, 0, 2, 0))
  4406. return false;
  4407. pc_delitem(sd, n, amount, 1, 0, LOG_TYPE_PICKDROP_PLAYER);
  4408. clif_dropitem(sd, n, amount);
  4409. return true;
  4410. }
  4411. /*==========================================
  4412. * Attempt to pick up an item.
  4413. * @param sd
  4414. * @param fitem Item that will be picked
  4415. * @return False = fail; True = success
  4416. *------------------------------------------*/
  4417. bool pc_takeitem(struct map_session_data *sd,struct flooritem_data *fitem)
  4418. {
  4419. int flag = 0;
  4420. t_tick tick = gettick();
  4421. struct party_data *p = NULL;
  4422. nullpo_ret(sd);
  4423. nullpo_ret(fitem);
  4424. if (!check_distance_bl(&fitem->bl, &sd->bl, 2) && sd->ud.skill_id!=BS_GREED)
  4425. return false; // Distance is too far
  4426. if (sd->sc.cant.pickup)
  4427. return false;
  4428. if (sd->status.party_id)
  4429. p = party_search(sd->status.party_id);
  4430. if (fitem->first_get_charid > 0 && fitem->first_get_charid != sd->status.char_id) {
  4431. struct map_session_data *first_sd = map_charid2sd(fitem->first_get_charid);
  4432. if (DIFF_TICK(tick,fitem->first_get_tick) < 0) {
  4433. if (!(p && p->party.item&1 &&
  4434. first_sd && first_sd->status.party_id == sd->status.party_id
  4435. ))
  4436. return false;
  4437. }
  4438. else if (fitem->second_get_charid > 0 && fitem->second_get_charid != sd->status.char_id) {
  4439. struct map_session_data *second_sd = map_charid2sd(fitem->second_get_charid);
  4440. if (DIFF_TICK(tick, fitem->second_get_tick) < 0) {
  4441. if (!(p && p->party.item&1 &&
  4442. ((first_sd && first_sd->status.party_id == sd->status.party_id) ||
  4443. (second_sd && second_sd->status.party_id == sd->status.party_id))
  4444. ))
  4445. return false;
  4446. }
  4447. else if (fitem->third_get_charid > 0 && fitem->third_get_charid != sd->status.char_id){
  4448. struct map_session_data *third_sd = map_charid2sd(fitem->third_get_charid);
  4449. if (DIFF_TICK(tick,fitem->third_get_tick) < 0) {
  4450. if(!(p && p->party.item&1 &&
  4451. ((first_sd && first_sd->status.party_id == sd->status.party_id) ||
  4452. (second_sd && second_sd->status.party_id == sd->status.party_id) ||
  4453. (third_sd && third_sd->status.party_id == sd->status.party_id))
  4454. ))
  4455. return false;
  4456. }
  4457. }
  4458. }
  4459. }
  4460. //This function takes care of giving the item to whoever should have it, considering party-share options.
  4461. if ((flag = party_share_loot(p,sd,&fitem->item, fitem->first_get_charid))) {
  4462. clif_additem(sd,0,0,flag);
  4463. return true;
  4464. }
  4465. //Display pickup animation.
  4466. pc_stop_attack(sd);
  4467. clif_takeitem(&sd->bl,&fitem->bl);
  4468. if (fitem->mob_id &&
  4469. (itemdb_search(fitem->item.nameid))->flag.broadcast &&
  4470. (!p || !(p->party.item&2)) // Somehow, if party's pickup distribution is 'Even Share', no announcemet
  4471. )
  4472. intif_broadcast_obtain_special_item(sd, fitem->item.nameid, fitem->mob_id, ITEMOBTAIN_TYPE_MONSTER_ITEM);
  4473. map_clearflooritem(&fitem->bl);
  4474. return true;
  4475. }
  4476. /*==========================================
  4477. * Check if item is usable.
  4478. * Return:
  4479. * false = no
  4480. * true = yes
  4481. *------------------------------------------*/
  4482. bool pc_isUseitem(struct map_session_data *sd,int n)
  4483. {
  4484. struct item_data *item;
  4485. unsigned short nameid;
  4486. nullpo_ret(sd);
  4487. item = sd->inventory_data[n];
  4488. nameid = sd->inventory.u.items_inventory[n].nameid;
  4489. if( item == NULL )
  4490. return false;
  4491. //Not consumable item
  4492. if( item->type != IT_HEALING && item->type != IT_USABLE && item->type != IT_CASH )
  4493. return false;
  4494. if (pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL))
  4495. return true;
  4496. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  4497. if(mapdata->flag[MF_NOITEMCONSUMPTION]) //consumable but mapflag prevent it
  4498. return false;
  4499. //Prevent mass item usage. [Skotlex]
  4500. if( DIFF_TICK(sd->canuseitem_tick,gettick()) > 0 ||
  4501. (itemdb_group_item_exists(IG_CASH_FOOD, nameid) && DIFF_TICK(sd->canusecashfood_tick,gettick()) > 0)
  4502. )
  4503. return false;
  4504. if( (item->item_usage.flag&NOUSE_SITTING) && (pc_issit(sd) == 1) && (pc_get_group_level(sd) < item->item_usage.override) ) {
  4505. clif_msg(sd,ITEM_NOUSE_SITTING);
  4506. return false; // You cannot use this item while sitting.
  4507. }
  4508. if (sd->state.storage_flag && item->type != IT_CASH) {
  4509. clif_messagecolor(&sd->bl, color_table[COLOR_RED], msg_txt(sd,388), false, SELF);
  4510. return false; // You cannot use this item while storage is open.
  4511. }
  4512. if (item->flag.dead_branch && (mapdata->flag[MF_NOBRANCH] || mapdata_flag_gvg2(mapdata)))
  4513. return false;
  4514. switch( nameid ) {
  4515. case ITEMID_WING_OF_FLY:
  4516. case ITEMID_GIANT_FLY_WING:
  4517. case ITEMID_N_FLY_WING:
  4518. if( mapdata->flag[MF_NOTELEPORT] || mapdata_flag_gvg2(mapdata) ) {
  4519. clif_skill_teleportmessage(sd,0);
  4520. return false;
  4521. }
  4522. if (nameid == ITEMID_GIANT_FLY_WING) {
  4523. struct party_data *pd = party_search(sd->status.party_id);
  4524. if (pd) {
  4525. int i;
  4526. ARR_FIND(0, MAX_PARTY, i, pd->data[i].sd == sd && pd->party.member[i].leader);
  4527. if (i == MAX_PARTY) { // User is not party leader
  4528. clif_msg(sd, ITEM_PARTY_MEMBER_NOT_SUMMONED);
  4529. break;
  4530. }
  4531. 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));
  4532. if (i == MAX_PARTY) { // No party members found on same map
  4533. clif_msg(sd, ITEM_PARTY_NO_MEMBER_IN_MAP);
  4534. break;
  4535. }
  4536. } else {
  4537. clif_msg(sd, ITEM_PARTY_MEMBER_NOT_SUMMONED);
  4538. break;
  4539. }
  4540. }
  4541. // Fall through
  4542. case ITEMID_WING_OF_BUTTERFLY:
  4543. case ITEMID_N_BUTTERFLY_WING:
  4544. case ITEMID_DUN_TELE_SCROLL1:
  4545. case ITEMID_DUN_TELE_SCROLL2:
  4546. case ITEMID_DUN_TELE_SCROLL3:
  4547. case ITEMID_WOB_RUNE:
  4548. case ITEMID_WOB_SCHWALTZ:
  4549. case ITEMID_WOB_RACHEL:
  4550. case ITEMID_WOB_LOCAL:
  4551. case ITEMID_SIEGE_TELEPORT_SCROLL:
  4552. if( sd->duel_group && !battle_config.duel_allow_teleport ) {
  4553. clif_displaymessage(sd->fd, msg_txt(sd,663));
  4554. return false;
  4555. }
  4556. if( mapdata->flag[MF_NORETURN] && nameid != ITEMID_WING_OF_FLY && nameid != ITEMID_GIANT_FLY_WING && nameid != ITEMID_N_FLY_WING )
  4557. return false;
  4558. break;
  4559. case ITEMID_MERCENARY_RED_POTION:
  4560. case ITEMID_MERCENARY_BLUE_POTION:
  4561. case ITEMID_M_CENTER_POTION:
  4562. case ITEMID_M_AWAKENING_POTION:
  4563. case ITEMID_M_BERSERK_POTION:
  4564. if( sd->md == NULL || sd->md->db == NULL )
  4565. return false;
  4566. if( sd->md->sc.data[SC_BERSERK] )
  4567. return false;
  4568. if( nameid == ITEMID_M_AWAKENING_POTION && sd->md->db->lv < 40 )
  4569. return false;
  4570. if( nameid == ITEMID_M_BERSERK_POTION && sd->md->db->lv < 80 )
  4571. return false;
  4572. break;
  4573. case ITEMID_NEURALIZER:
  4574. if( !mapdata->flag[MF_RESET] )
  4575. return false;
  4576. break;
  4577. }
  4578. if( itemdb_group_item_exists(IG_MERCENARY, nameid) && sd->md != NULL )
  4579. return false; // Mercenary Scrolls
  4580. if( item->flag.group || item->type == IT_CASH) { //safe check type cash disappear when overweight [Napster]
  4581. if( pc_is90overweight(sd) ) {
  4582. clif_msg(sd, ITEM_CANT_OBTAIN_WEIGHT);
  4583. return false;
  4584. }
  4585. if( !pc_inventoryblank(sd) ) {
  4586. clif_messagecolor(&sd->bl, color_table[COLOR_RED], msg_txt(sd, 732), false, SELF); //Item cannot be open when inventory is full
  4587. return false;
  4588. }
  4589. }
  4590. //Gender check
  4591. if(item->sex != 2 && sd->status.sex != item->sex)
  4592. return false;
  4593. //Required level check
  4594. if(item->elv && sd->status.base_level < (unsigned int)item->elv)
  4595. return false;
  4596. if(item->elvmax && sd->status.base_level > (unsigned int)item->elvmax)
  4597. return false;
  4598. //Not equipable by class. [Skotlex]
  4599. if (!(
  4600. (1ULL<<(sd->class_&MAPID_BASEMASK)) &
  4601. (item->class_base[sd->class_&JOBL_2_1?1:(sd->class_&JOBL_2_2?2:0)])
  4602. ))
  4603. return false;
  4604. if (sd->sc.count && (
  4605. sd->sc.data[SC_BERSERK] || sd->sc.data[SC_SATURDAYNIGHTFEVER] ||
  4606. (sd->sc.data[SC_GRAVITATION] && sd->sc.data[SC_GRAVITATION]->val3 == BCT_SELF) ||
  4607. sd->sc.data[SC_TRICKDEAD] ||
  4608. sd->sc.data[SC_HIDING] ||
  4609. sd->sc.data[SC__SHADOWFORM] ||
  4610. sd->sc.data[SC__INVISIBILITY] ||
  4611. sd->sc.data[SC__MANHOLE] ||
  4612. sd->sc.data[SC_DEEPSLEEP] ||
  4613. sd->sc.data[SC_CRYSTALIZE] ||
  4614. sd->sc.data[SC_KAGEHUMI] ||
  4615. (sd->sc.data[SC_NOCHAT] && sd->sc.data[SC_NOCHAT]->val1&MANNER_NOITEM) ||
  4616. sd->sc.data[SC_KINGS_GRACE] ||
  4617. sd->sc.data[SC_SUHIDE]))
  4618. return false;
  4619. if (!pc_isItemClass(sd,item))
  4620. return false;
  4621. //Dead Branch items
  4622. if( item->flag.dead_branch )
  4623. log_branch(sd);
  4624. return true;
  4625. }
  4626. /*==========================================
  4627. * Last checks to use an item.
  4628. * Return:
  4629. * 0 = fail
  4630. * 1 = success
  4631. *------------------------------------------*/
  4632. int pc_useitem(struct map_session_data *sd,int n)
  4633. {
  4634. t_tick tick = gettick();
  4635. int amount;
  4636. unsigned short nameid;
  4637. struct script_code *script;
  4638. struct item item;
  4639. struct item_data *id;
  4640. nullpo_ret(sd);
  4641. if (sd->npc_id) {
  4642. if (sd->progressbar.npc_id) {
  4643. clif_progressbar_abort(sd);
  4644. return 0; // First item use attempt cancels the progress bar
  4645. }
  4646. if( pc_hasprogress( sd, WIP_DISABLE_SKILLITEM ) || !sd->npc_item_flag ){
  4647. #ifdef RENEWAL
  4648. clif_msg( sd, WORK_IN_PROGRESS );
  4649. #endif
  4650. return 0;
  4651. }
  4652. }
  4653. item = sd->inventory.u.items_inventory[n];
  4654. id = sd->inventory_data[n];
  4655. if (item.nameid == 0 || item.amount <= 0)
  4656. return 0;
  4657. if( !pc_isUseitem(sd,n) )
  4658. return 0;
  4659. // Store information for later use before it is lost (via pc_delitem) [Paradox924X]
  4660. nameid = id->nameid;
  4661. if (nameid != ITEMID_NAUTHIZ && sd->sc.opt1 > 0 && sd->sc.opt1 != OPT1_STONEWAIT && sd->sc.opt1 != OPT1_BURNING)
  4662. return 0;
  4663. /* Items with delayed consume are not meant to work while in mounts except reins of mount(12622) */
  4664. if( id->flag.delay_consume ) {
  4665. if( nameid != ITEMID_REINS_OF_MOUNT && sd->sc.data[SC_ALL_RIDING] )
  4666. return 0;
  4667. else if( pc_issit(sd) )
  4668. return 0;
  4669. }
  4670. //Since most delay-consume items involve using a "skill-type" target cursor,
  4671. //perform a skill-use check before going through. [Skotlex]
  4672. //resurrection was picked as testing skill, as a non-offensive, generic skill, it will do.
  4673. //FIXME: Is this really needed here? It'll be checked in unit.cpp after all and this prevents skill items using when silenced [Inkfish]
  4674. if( id->flag.delay_consume && ( sd->ud.skilltimer != INVALID_TIMER /*|| !status_check_skilluse(&sd->bl, &sd->bl, ALL_RESURRECTION, 0)*/ ) )
  4675. return 0;
  4676. if( id->delay > 0 && !pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL) && pc_itemcd_check(sd, id, tick, n))
  4677. return 0;
  4678. /* on restricted maps the item is consumed but the effect is not used */
  4679. if (!pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL) && itemdb_isNoEquip(id,sd->bl.m)) {
  4680. clif_msg(sd,ITEM_CANT_USE_AREA); // This item cannot be used within this area
  4681. if( battle_config.allow_consume_restricted_item && !id->flag.delay_consume ) { //need confirmation for delayed consumption items
  4682. clif_useitemack(sd,n,item.amount-1,true);
  4683. pc_delitem(sd,n,1,1,0,LOG_TYPE_CONSUME);
  4684. }
  4685. return 0;/* regardless, effect is not run */
  4686. }
  4687. sd->itemid = item.nameid;
  4688. sd->itemindex = n;
  4689. if(sd->catch_target_class != PET_CATCH_FAIL) //Abort pet catching.
  4690. sd->catch_target_class = PET_CATCH_FAIL;
  4691. amount = item.amount;
  4692. script = id->script;
  4693. //Check if the item is to be consumed immediately [Skotlex]
  4694. if (id->flag.delay_consume)
  4695. clif_useitemack(sd, n, amount, true);
  4696. else
  4697. {
  4698. if( item.expire_time == 0 && nameid != ITEMID_REINS_OF_MOUNT )
  4699. {
  4700. clif_useitemack(sd, n, amount - 1, true);
  4701. pc_delitem(sd, n, 1, 1, 0, LOG_TYPE_CONSUME); // Rental Usable Items are not deleted until expiration
  4702. }
  4703. else
  4704. clif_useitemack(sd, n, 0, false);
  4705. }
  4706. if (item.card[0]==CARD0_CREATE && pc_famerank(MakeDWord(item.card[2],item.card[3]), MAPID_ALCHEMIST))
  4707. potion_flag = 2; // Famous player's potions have 50% more efficiency
  4708. //Update item use time.
  4709. sd->canuseitem_tick = tick + battle_config.item_use_interval;
  4710. if( itemdb_group_item_exists(IG_CASH_FOOD, nameid) )
  4711. sd->canusecashfood_tick = tick + battle_config.cashfood_use_interval;
  4712. run_script(script,0,sd->bl.id,fake_nd->bl.id);
  4713. potion_flag = 0;
  4714. return 1;
  4715. }
  4716. /**
  4717. * Add item on cart for given index.
  4718. * @param sd
  4719. * @param item
  4720. * @param amount
  4721. * @param log_type
  4722. * @return See pc.hpp::e_additem_result
  4723. */
  4724. enum e_additem_result pc_cart_additem(struct map_session_data *sd,struct item *item,int amount,e_log_pick_type log_type)
  4725. {
  4726. struct item_data *data;
  4727. int i,w;
  4728. nullpo_retr(ADDITEM_INVALID, sd);
  4729. nullpo_retr(ADDITEM_INVALID, item);
  4730. if(item->nameid == 0 || amount <= 0)
  4731. return ADDITEM_INVALID;
  4732. if (itemdb_ishatched_egg(item))
  4733. return ADDITEM_INVALID;
  4734. data = itemdb_search(item->nameid);
  4735. if( data->stack.cart && amount > data->stack.amount )
  4736. {// item stack limitation
  4737. return ADDITEM_STACKLIMIT;
  4738. }
  4739. if( !itemdb_cancartstore(item, pc_get_group_level(sd)) || (item->bound > BOUND_ACCOUNT && !pc_can_give_bounded_items(sd)))
  4740. { // Check item trade restrictions [Skotlex]
  4741. clif_displaymessage (sd->fd, msg_txt(sd,264));
  4742. return ADDITEM_INVALID;
  4743. }
  4744. if( (w = data->weight*amount) + sd->cart_weight > sd->cart_weight_max )
  4745. return ADDITEM_OVERWEIGHT;
  4746. i = MAX_CART;
  4747. if( itemdb_isstackable2(data) && !item->expire_time )
  4748. {
  4749. for (i = 0; i < MAX_CART; i++) {
  4750. if (sd->cart.u.items_cart[i].nameid == item->nameid
  4751. && sd->cart.u.items_cart[i].bound == item->bound
  4752. && sd->cart.u.items_cart[i].unique_id == item->unique_id
  4753. && memcmp(sd->cart.u.items_cart[i].card, item->card, sizeof(item->card)) == 0
  4754. )
  4755. break;
  4756. }
  4757. }
  4758. if( i < MAX_CART )
  4759. {// item already in cart, stack it
  4760. 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 ) )
  4761. return ADDITEM_OVERAMOUNT; // no slot
  4762. sd->cart.u.items_cart[i].amount += amount;
  4763. clif_cart_additem(sd,i,amount,0);
  4764. }
  4765. else
  4766. {// item not stackable or not present, add it
  4767. ARR_FIND( 0, MAX_CART, i, sd->cart.u.items_cart[i].nameid == 0 );
  4768. if( i == MAX_CART )
  4769. return ADDITEM_OVERAMOUNT; // no slot
  4770. memcpy(&sd->cart.u.items_cart[i],item,sizeof(sd->cart.u.items_cart[0]));
  4771. sd->cart.u.items_cart[i].id = 0;
  4772. sd->cart.u.items_cart[i].amount = amount;
  4773. sd->cart_num++;
  4774. clif_cart_additem(sd,i,amount,0);
  4775. }
  4776. sd->cart.u.items_cart[i].favorite = 0; // clear
  4777. sd->cart.u.items_cart[i].equipSwitch = 0;
  4778. log_pick_pc(sd, log_type, amount, &sd->cart.u.items_cart[i]);
  4779. sd->cart_weight += w;
  4780. clif_updatestatus(sd,SP_CARTINFO);
  4781. return ADDITEM_SUCCESS;
  4782. }
  4783. /*==========================================
  4784. * Delete item on cart for given index.
  4785. *------------------------------------------*/
  4786. void pc_cart_delitem(struct map_session_data *sd,int n,int amount,int type,e_log_pick_type log_type)
  4787. {
  4788. nullpo_retv(sd);
  4789. if(sd->cart.u.items_cart[n].nameid == 0 ||
  4790. sd->cart.u.items_cart[n].amount < amount)
  4791. return;
  4792. log_pick_pc(sd, log_type, -amount, &sd->cart.u.items_cart[n]);
  4793. sd->cart.u.items_cart[n].amount -= amount;
  4794. sd->cart_weight -= itemdb_weight(sd->cart.u.items_cart[n].nameid) * amount;
  4795. if(sd->cart.u.items_cart[n].amount <= 0) {
  4796. memset(&sd->cart.u.items_cart[n],0,sizeof(sd->cart.u.items_cart[0]));
  4797. sd->cart_num--;
  4798. }
  4799. if(!type) {
  4800. clif_cart_delitem(sd,n,amount);
  4801. clif_updatestatus(sd,SP_CARTINFO);
  4802. }
  4803. }
  4804. /*==========================================
  4805. * Transfer item from inventory to cart.
  4806. *------------------------------------------*/
  4807. void pc_putitemtocart(struct map_session_data *sd,int idx,int amount)
  4808. {
  4809. nullpo_retv(sd);
  4810. if (idx < 0 || idx >= MAX_INVENTORY) //Invalid index check [Skotlex]
  4811. return;
  4812. struct item *item_data = &sd->inventory.u.items_inventory[idx];
  4813. if( item_data->nameid == 0 || amount < 1 || item_data->amount < amount || sd->state.vending || sd->state.prevend )
  4814. return;
  4815. if( item_data->equipSwitch ){
  4816. clif_msg( sd, C_ITEM_EQUIP_SWITCH );
  4817. return;
  4818. }
  4819. enum e_additem_result flag = pc_cart_additem(sd,item_data,amount,LOG_TYPE_NONE);
  4820. if (flag == ADDITEM_SUCCESS)
  4821. pc_delitem(sd,idx,amount,0,5,LOG_TYPE_NONE);
  4822. else {
  4823. clif_cart_additem_ack(sd, (flag == ADDITEM_OVERAMOUNT) ? ADDITEM_TO_CART_FAIL_COUNT : ADDITEM_TO_CART_FAIL_WEIGHT);
  4824. clif_additem(sd, idx, amount, 0);
  4825. clif_delitem(sd, idx, amount, 0);
  4826. }
  4827. }
  4828. /*==========================================
  4829. * Get number of item in cart.
  4830. * Return:
  4831. -1 = itemid not found or no amount found
  4832. x = remaining itemid on cart after get
  4833. *------------------------------------------*/
  4834. int pc_cartitem_amount(struct map_session_data* sd, int idx, int amount)
  4835. {
  4836. struct item* item_data;
  4837. nullpo_retr(-1, sd);
  4838. item_data = &sd->cart.u.items_cart[idx];
  4839. if( item_data->nameid == 0 || item_data->amount == 0 )
  4840. return -1;
  4841. return item_data->amount - amount;
  4842. }
  4843. /*==========================================
  4844. * Retrieve an item at index idx from cart.
  4845. *------------------------------------------*/
  4846. void pc_getitemfromcart(struct map_session_data *sd,int idx,int amount)
  4847. {
  4848. nullpo_retv(sd);
  4849. if (idx < 0 || idx >= MAX_CART) //Invalid index check [Skotlex]
  4850. return;
  4851. struct item *item_data=&sd->cart.u.items_cart[idx];
  4852. if (item_data->nameid == 0 || amount < 1 || item_data->amount < amount || sd->state.vending || sd->state.prevend)
  4853. return;
  4854. enum e_additem_result flag = pc_additem(sd, item_data, amount, LOG_TYPE_NONE);
  4855. if (flag == ADDITEM_SUCCESS)
  4856. pc_cart_delitem(sd, idx, amount, 0, LOG_TYPE_NONE);
  4857. else {
  4858. clif_cart_delitem(sd, idx, amount);
  4859. clif_additem(sd, idx, amount, flag);
  4860. clif_cart_additem(sd, idx, amount, 0);
  4861. }
  4862. }
  4863. /*==========================================
  4864. * Bound Item Check
  4865. * Type:
  4866. * 1 Account Bound
  4867. * 2 Guild Bound
  4868. * 3 Party Bound
  4869. * 4 Character Bound
  4870. *------------------------------------------*/
  4871. int pc_bound_chk(TBL_PC *sd,enum bound_type type,int *idxlist)
  4872. {
  4873. int i = 0, j = 0;
  4874. for(i = 0; i < MAX_INVENTORY; i++) {
  4875. 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) {
  4876. idxlist[j] = i;
  4877. j++;
  4878. }
  4879. }
  4880. return j;
  4881. }
  4882. /*==========================================
  4883. * Display item stolen msg to player sd
  4884. *------------------------------------------*/
  4885. int pc_show_steal(struct block_list *bl,va_list ap)
  4886. {
  4887. struct map_session_data *sd;
  4888. int itemid;
  4889. struct item_data *item=NULL;
  4890. char output[100];
  4891. sd=va_arg(ap,struct map_session_data *);
  4892. itemid=va_arg(ap,int);
  4893. if((item=itemdb_exists(itemid))==NULL)
  4894. sprintf(output,"%s stole an Unknown Item (id: %i).",sd->status.name, itemid);
  4895. else
  4896. sprintf(output,"%s stole %s.",sd->status.name,item->jname);
  4897. clif_displaymessage( ((struct map_session_data *)bl)->fd, output);
  4898. return 0;
  4899. }
  4900. /**
  4901. * Steal an item from bl (mob).
  4902. * @param sd: Player data
  4903. * @param bl: Object to steal from
  4904. * @param skill_lv: Level of skill used
  4905. * @return True on success or false otherwise
  4906. */
  4907. bool pc_steal_item(struct map_session_data *sd,struct block_list *bl, uint16 skill_lv)
  4908. {
  4909. int i,itemid;
  4910. double rate;
  4911. unsigned char flag = 0;
  4912. struct status_data *sd_status, *md_status;
  4913. struct mob_data *md;
  4914. struct item tmp_item;
  4915. if(!sd || !bl || bl->type!=BL_MOB)
  4916. return false;
  4917. md = (TBL_MOB *)bl;
  4918. if(md->state.steal_flag == UCHAR_MAX || ( md->sc.opt1 && md->sc.opt1 != OPT1_BURNING ) ) //already stolen from / status change check
  4919. return false;
  4920. sd_status= status_get_status_data(&sd->bl);
  4921. md_status= status_get_status_data(bl);
  4922. if (md->master_id || status_has_mode(md_status, MD_STATUS_IMMUNE) || status_get_race2(&md->bl) == RC2_TREASURE ||
  4923. map_getmapflag(bl->m, MF_NOMOBLOOT) || // check noloot map flag [Lorky]
  4924. (battle_config.skill_steal_max_tries && //Reached limit of steal attempts. [Lupus]
  4925. md->state.steal_flag++ >= battle_config.skill_steal_max_tries)
  4926. ) { //Can't steal from
  4927. md->state.steal_flag = UCHAR_MAX;
  4928. return false;
  4929. }
  4930. // base skill success chance (percentual)
  4931. rate = (sd_status->dex - md_status->dex)/2 + skill_lv*6 + 4;
  4932. rate += sd->bonus.add_steal_rate;
  4933. if( rate < 1
  4934. #ifdef RENEWAL
  4935. || rnd()%100 >= rate
  4936. #endif
  4937. )
  4938. return false;
  4939. // Try dropping one item, in the order from first to last possible slot.
  4940. // Droprate is affected by the skill success rate.
  4941. for( i = 0; i < MAX_STEAL_DROP; i++ )
  4942. 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
  4943. #ifndef RENEWAL
  4944. * rate/100.
  4945. #endif
  4946. )
  4947. break;
  4948. if( i == MAX_STEAL_DROP )
  4949. return false;
  4950. itemid = md->db->dropitem[i].nameid;
  4951. memset(&tmp_item,0,sizeof(tmp_item));
  4952. tmp_item.nameid = itemid;
  4953. tmp_item.amount = 1;
  4954. tmp_item.identify = itemdb_isidentified(itemid);
  4955. mob_setdropitem_option(&tmp_item, &md->db->dropitem[i]);
  4956. flag = pc_additem(sd,&tmp_item,1,LOG_TYPE_PICKDROP_PLAYER);
  4957. //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?
  4958. md->state.steal_flag = UCHAR_MAX; //you can't steal from this mob any more
  4959. if(flag) { //Failed to steal due to overweight
  4960. clif_additem(sd,0,0,flag);
  4961. return false;
  4962. }
  4963. if(battle_config.show_steal_in_same_party)
  4964. party_foreachsamemap(pc_show_steal,sd,AREA_SIZE,sd,tmp_item.nameid);
  4965. //Logs items, Stolen from mobs [Lupus]
  4966. log_pick_mob(md, LOG_TYPE_STEAL, -1, &tmp_item);
  4967. //A Rare Steal Global Announce by Lupus
  4968. if(md->db->dropitem[i].p<=battle_config.rare_drop_announce) {
  4969. struct item_data *i_data;
  4970. char message[128];
  4971. i_data = itemdb_search(itemid);
  4972. 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);
  4973. //MSG: "'%s' stole %s's %s (chance: %0.02f%%)"
  4974. intif_broadcast(message, strlen(message) + 1, BC_DEFAULT);
  4975. }
  4976. return true;
  4977. }
  4978. /*==========================================
  4979. * Stole zeny from bl (mob)
  4980. * return
  4981. * 0 = fail
  4982. * 1 = success
  4983. *------------------------------------------*/
  4984. int pc_steal_coin(struct map_session_data *sd,struct block_list *target)
  4985. {
  4986. int rate, target_lv;
  4987. struct mob_data *md;
  4988. if(!sd || !target || target->type != BL_MOB)
  4989. return 0;
  4990. md = (TBL_MOB*)target;
  4991. target_lv = status_get_lv(target);
  4992. 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)
  4993. return 0;
  4994. rate = sd->battle_status.dex / 2 + 2 * (sd->status.base_level - target_lv) + (10 * pc_checkskill(sd, RG_STEALCOIN)) + sd->battle_status.luk / 2;
  4995. if(rnd()%1000 < rate)
  4996. {
  4997. // Zeny Steal Amount: (rnd() % (10 * target_lv + 1 - 8 * target_lv)) + 8 * target_lv
  4998. int amount = (rnd() % (2 * target_lv + 1)) + 8 * target_lv; // Reduced formula
  4999. pc_getzeny(sd, amount, LOG_TYPE_STEAL, NULL);
  5000. md->state.steal_coin_flag = 1;
  5001. return 1;
  5002. }
  5003. return 0;
  5004. }
  5005. /*==========================================
  5006. * Set's a player position.
  5007. * @param sd
  5008. * @param mapindex
  5009. * @param x
  5010. * @param y
  5011. * @param clrtype
  5012. * @return SETPOS_OK Success
  5013. * SETPOS_MAPINDEX Invalid map index
  5014. * SETPOS_NO_MAPSERVER Map not in this map-server, and failed to locate alternate map-server.
  5015. * SETPOS_AUTOTRADE Player is in autotrade state
  5016. *------------------------------------------*/
  5017. enum e_setpos pc_setpos(struct map_session_data* sd, unsigned short mapindex, int x, int y, clr_type clrtype)
  5018. {
  5019. nullpo_retr(SETPOS_OK,sd);
  5020. if( !mapindex || !mapindex_id2name(mapindex) ) {
  5021. ShowDebug("pc_setpos: Passed mapindex(%d) is invalid!\n", mapindex);
  5022. return SETPOS_MAPINDEX;
  5023. }
  5024. if ( sd->state.autotrade && (sd->vender_id || sd->buyer_id) ) // Player with autotrade just causes clif glitch! @ FIXME
  5025. return SETPOS_AUTOTRADE;
  5026. if( battle_config.revive_onwarp && pc_isdead(sd) ) { //Revive dead people before warping them
  5027. pc_setstand(sd, true);
  5028. pc_setrestartvalue(sd,1);
  5029. }
  5030. int16 m = map_mapindex2mapid(mapindex);
  5031. struct map_data *mapdata = map_getmapdata(m);
  5032. sd->state.changemap = (sd->mapindex != mapindex);
  5033. sd->state.warping = 1;
  5034. sd->state.workinprogress = WIP_DISABLE_NONE;
  5035. if( sd->state.changemap ) { // Misc map-changing settings
  5036. unsigned short curr_map_instance_id = map_getmapdata(sd->bl.m)->instance_id, new_map_instance_id = (mapdata ? mapdata->instance_id : 0);
  5037. if (curr_map_instance_id != new_map_instance_id) {
  5038. if (curr_map_instance_id) // Update instance timer for the map on leave
  5039. instance_delusers(curr_map_instance_id);
  5040. if (new_map_instance_id) // Update instance timer for the map on enter
  5041. instance_addusers(new_map_instance_id);
  5042. }
  5043. sd->state.pmap = sd->bl.m;
  5044. if (sd->sc.count) { // Cancel some map related stuff.
  5045. if (sd->sc.data[SC_JAILED])
  5046. return SETPOS_MAPINDEX; //You may not get out!
  5047. status_change_end(&sd->bl, SC_BOSSMAPINFO, INVALID_TIMER);
  5048. status_change_end(&sd->bl, SC_WARM, INVALID_TIMER);
  5049. status_change_end(&sd->bl, SC_SUN_COMFORT, INVALID_TIMER);
  5050. status_change_end(&sd->bl, SC_MOON_COMFORT, INVALID_TIMER);
  5051. status_change_end(&sd->bl, SC_STAR_COMFORT, INVALID_TIMER);
  5052. status_change_end(&sd->bl, SC_MIRACLE, INVALID_TIMER);
  5053. if (sd->sc.data[SC_KNOWLEDGE]) {
  5054. struct status_change_entry *sce = sd->sc.data[SC_KNOWLEDGE];
  5055. if (sce->timer != INVALID_TIMER)
  5056. delete_timer(sce->timer, status_change_timer);
  5057. sce->timer = add_timer(gettick() + skill_get_time(SG_KNOWLEDGE, sce->val1), status_change_timer, sd->bl.id, SC_KNOWLEDGE);
  5058. }
  5059. status_change_end(&sd->bl, SC_PROPERTYWALK, INVALID_TIMER);
  5060. status_change_end(&sd->bl, SC_CLOAKING, INVALID_TIMER);
  5061. status_change_end(&sd->bl, SC_CLOAKINGEXCEED, INVALID_TIMER);
  5062. }
  5063. for(int i = 0; i < EQI_MAX; i++ ) {
  5064. if( sd->equip_index[i] >= 0 )
  5065. if( pc_isequip(sd,sd->equip_index[i]) )
  5066. pc_unequipitem(sd,sd->equip_index[i],2);
  5067. }
  5068. if (battle_config.clear_unit_onwarp&BL_PC)
  5069. skill_clear_unitgroup(&sd->bl);
  5070. party_send_dot_remove(sd); //minimap dot fix [Kevin]
  5071. guild_send_dot_remove(sd);
  5072. bg_send_dot_remove(sd);
  5073. if (sd->regen.state.gc)
  5074. sd->regen.state.gc = 0;
  5075. // make sure vending is allowed here
  5076. if (sd->state.vending && mapdata->flag[MF_NOVENDING]) {
  5077. clif_displaymessage (sd->fd, msg_txt(sd,276)); // "You can't open a shop on this map"
  5078. vending_closevending(sd);
  5079. }
  5080. channel_pcquit(sd,4); //quit map chan
  5081. }
  5082. if( m < 0 )
  5083. {
  5084. uint32 ip;
  5085. uint16 port;
  5086. struct script_state *st;
  5087. //if can't find any map-servers, just abort setting position.
  5088. if(!sd->mapindex || map_mapname2ipport(mapindex,&ip,&port))
  5089. return SETPOS_NO_MAPSERVER;
  5090. if (sd->npc_id){
  5091. npc_event_dequeue(sd,false);
  5092. st = sd->st;
  5093. }else{
  5094. st = nullptr;
  5095. }
  5096. npc_script_event(sd, NPCE_LOGOUT);
  5097. //remove from map, THEN change x/y coordinates
  5098. unit_remove_map_pc(sd,clrtype);
  5099. sd->mapindex = mapindex;
  5100. sd->bl.x=x;
  5101. sd->bl.y=y;
  5102. pc_clean_skilltree(sd);
  5103. chrif_save(sd, CSAVE_CHANGE_MAPSERV|CSAVE_INVENTORY|CSAVE_CART);
  5104. chrif_changemapserver(sd, ip, (short)port);
  5105. //Free session data from this map server [Kevin]
  5106. unit_free_pc(sd);
  5107. if( st ){
  5108. // Has to be done here, because otherwise unit_free_pc will free the stack already
  5109. st->state = END;
  5110. }
  5111. return SETPOS_OK;
  5112. }
  5113. if( x < 0 || x >= mapdata->xs || y < 0 || y >= mapdata->ys )
  5114. {
  5115. 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);
  5116. x = y = 0; // make it random
  5117. }
  5118. if( x == 0 && y == 0 ) { // pick a random walkable cell
  5119. int c=0;
  5120. do {
  5121. x = rnd()%(mapdata->xs-2)+1;
  5122. y = rnd()%(mapdata->ys-2)+1;
  5123. c++;
  5124. if(c > (mapdata->xs * mapdata->ys)*3){ //force out
  5125. 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));
  5126. return SETPOS_OK; //preventing warp
  5127. //break; //allow warp anyway
  5128. }
  5129. } while(map_getcell(m,x,y,CELL_CHKNOPASS) || (!battle_config.teleport_on_portal && npc_check_areanpc(1,m,x,y,1)));
  5130. }
  5131. if (sd->state.vending && map_getcell(m,x,y,CELL_CHKNOVENDING)) {
  5132. clif_displaymessage (sd->fd, msg_txt(sd,204)); // "You can't open a shop on this cell."
  5133. vending_closevending(sd);
  5134. }
  5135. if(sd->bl.prev != NULL){
  5136. unit_remove_map_pc(sd,clrtype);
  5137. clif_changemap(sd,m,x,y); // [MouseJstr]
  5138. } else if(sd->state.active) //Tag player for rewarping after map-loading is done. [Skotlex]
  5139. sd->state.rewarp = 1;
  5140. sd->mapindex = mapindex;
  5141. sd->bl.m = m;
  5142. sd->bl.x = sd->ud.to_x = x;
  5143. sd->bl.y = sd->ud.to_y = y;
  5144. if( sd->status.guild_id > 0 && mapdata->flag[MF_GVG_CASTLE] )
  5145. { // Increased guild castle regen [Valaris]
  5146. struct guild_castle *gc = guild_mapindex2gc(sd->mapindex);
  5147. if(gc && gc->guild_id == sd->status.guild_id)
  5148. sd->regen.state.gc = 1;
  5149. }
  5150. if( sd->status.pet_id > 0 && sd->pd && sd->pd->pet.intimate > 0 )
  5151. {
  5152. sd->pd->bl.m = m;
  5153. sd->pd->bl.x = sd->pd->ud.to_x = x;
  5154. sd->pd->bl.y = sd->pd->ud.to_y = y;
  5155. sd->pd->ud.dir = sd->ud.dir;
  5156. }
  5157. if( hom_is_active(sd->hd) )
  5158. {
  5159. sd->hd->bl.m = m;
  5160. sd->hd->bl.x = sd->hd->ud.to_x = x;
  5161. sd->hd->bl.y = sd->hd->ud.to_y = y;
  5162. sd->hd->ud.dir = sd->ud.dir;
  5163. }
  5164. if( sd->md )
  5165. {
  5166. sd->md->bl.m = m;
  5167. sd->md->bl.x = sd->md->ud.to_x = x;
  5168. sd->md->bl.y = sd->md->ud.to_y = y;
  5169. sd->md->ud.dir = sd->ud.dir;
  5170. }
  5171. if( sd->ed ) {
  5172. sd->ed->bl.m = m;
  5173. sd->ed->bl.x = sd->ed->ud.to_x = x;
  5174. sd->ed->bl.y = sd->ed->ud.to_y = y;
  5175. sd->ed->ud.dir = sd->ud.dir;
  5176. }
  5177. pc_cell_basilica(sd);
  5178. //check if we gonna be rewarped [lighta]
  5179. if(npc_check_areanpc(1,m,x,y,0)){
  5180. sd->count_rewarp++;
  5181. }
  5182. else
  5183. sd->count_rewarp = 0;
  5184. return SETPOS_OK;
  5185. }
  5186. /*==========================================
  5187. * Warp player sd to random location on current map.
  5188. * May fail if no walkable cell found (1000 attempts).
  5189. * Return:
  5190. * 0 = Success
  5191. * 1,2,3 = Fail
  5192. *------------------------------------------*/
  5193. char pc_randomwarp(struct map_session_data *sd, clr_type type)
  5194. {
  5195. int x,y,i=0;
  5196. nullpo_ret(sd);
  5197. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  5198. if (mapdata->flag[MF_NOTELEPORT]) //Teleport forbidden
  5199. return 3;
  5200. do {
  5201. x = rnd()%(mapdata->xs-2)+1;
  5202. y = rnd()%(mapdata->ys-2)+1;
  5203. } 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);
  5204. if (i < 1000)
  5205. return pc_setpos(sd,mapdata->index,x,y,type);
  5206. return 3;
  5207. }
  5208. /*==========================================
  5209. * Records a memo point at sd's current position
  5210. * pos - entry to replace, (-1: shift oldest entry out)
  5211. *------------------------------------------*/
  5212. bool pc_memo(struct map_session_data* sd, int pos)
  5213. {
  5214. int skill;
  5215. nullpo_ret(sd);
  5216. // check mapflags
  5217. 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) ) {
  5218. clif_skill_teleportmessage(sd, 1); // "Saved point cannot be memorized."
  5219. return false;
  5220. }
  5221. // check inputs
  5222. if( pos < -1 || pos >= MAX_MEMOPOINTS )
  5223. return false; // invalid input
  5224. // check required skill level
  5225. skill = pc_checkskill(sd, AL_WARP);
  5226. if( skill < 1 ) {
  5227. clif_skill_memomessage(sd,2); // "You haven't learned Warp."
  5228. return false;
  5229. }
  5230. if( skill < 2 || skill - 2 < pos ) {
  5231. clif_skill_memomessage(sd,1); // "Skill Level is not high enough."
  5232. return false;
  5233. }
  5234. if( pos == -1 )
  5235. {
  5236. uint8 i;
  5237. // prevent memo-ing the same map multiple times
  5238. ARR_FIND( 0, MAX_MEMOPOINTS, i, sd->status.memo_point[i].map == map_id2index(sd->bl.m) );
  5239. memmove(&sd->status.memo_point[1], &sd->status.memo_point[0], (u8min(i,MAX_MEMOPOINTS-1))*sizeof(struct point));
  5240. pos = 0;
  5241. }
  5242. if( map_getmapdata(sd->bl.m)->instance_id ) {
  5243. clif_displaymessage( sd->fd, msg_txt(sd,384) ); // You cannot create a memo in an instance.
  5244. return false;
  5245. }
  5246. sd->status.memo_point[pos].map = map_id2index(sd->bl.m);
  5247. sd->status.memo_point[pos].x = sd->bl.x;
  5248. sd->status.memo_point[pos].y = sd->bl.y;
  5249. clif_skill_memomessage(sd, 0);
  5250. return true;
  5251. }
  5252. //
  5253. // Skills
  5254. //
  5255. /**
  5256. * Get the skill current cooldown for player.
  5257. * (get the db base cooldown for skill + player specific cooldown)
  5258. * @param sd : player pointer
  5259. * @param id : skill id
  5260. * @param lv : skill lv
  5261. * @return player skill cooldown
  5262. */
  5263. int pc_get_skillcooldown(struct map_session_data *sd, uint16 skill_id, uint16 skill_lv) {
  5264. int cooldown = skill_get_cooldown(skill_id, skill_lv);
  5265. if (cooldown == 0)
  5266. return 0;
  5267. if (skill_id == SU_TUNABELLY && pc_checkskill(sd, SU_SPIRITOFSEA))
  5268. cooldown -= skill_get_time(SU_TUNABELLY, skill_lv);
  5269. for (auto &it : sd->skillcooldown) {
  5270. if (it.id == skill_id) {
  5271. cooldown += it.val;
  5272. cooldown = max(0, cooldown);
  5273. break;
  5274. }
  5275. }
  5276. return cooldown;
  5277. }
  5278. /*==========================================
  5279. * Return player sd skill_lv learned for given skill
  5280. *------------------------------------------*/
  5281. uint8 pc_checkskill(struct map_session_data *sd, uint16 skill_id)
  5282. {
  5283. uint16 idx = 0;
  5284. if (sd == NULL)
  5285. return 0;
  5286. if ((idx = skill_get_index(skill_id)) == 0) {
  5287. ShowError("pc_checkskill: Invalid skill id %d (char_id=%d).\n", skill_id, sd->status.char_id);
  5288. return 0;
  5289. }
  5290. if (SKILL_CHK_GUILD(skill_id) ) {
  5291. struct guild *g;
  5292. if( sd->status.guild_id>0 && (g=sd->guild)!=NULL)
  5293. return guild_checkskill(g,skill_id);
  5294. return 0;
  5295. }
  5296. return (sd->status.skill[idx].id == skill_id) ? sd->status.skill[idx].lv : 0;
  5297. }
  5298. /**
  5299. * Check if we still have the correct weapon to continue the skill (actually status)
  5300. * If not ending it
  5301. * @param sd
  5302. * @return 0:error, 1:check done
  5303. */
  5304. static void pc_checkallowskill(struct map_session_data *sd)
  5305. {
  5306. const enum sc_type scw_list[] = {
  5307. SC_TWOHANDQUICKEN,
  5308. SC_ONEHAND,
  5309. SC_AURABLADE,
  5310. SC_PARRYING,
  5311. SC_SPEARQUICKEN,
  5312. SC_ADRENALINE,
  5313. SC_ADRENALINE2,
  5314. SC_DANCING,
  5315. SC_GATLINGFEVER,
  5316. };
  5317. uint8 i;
  5318. nullpo_retv(sd);
  5319. if(!sd->sc.count)
  5320. return;
  5321. for (i = 0; i < ARRAYLENGTH(scw_list); i++)
  5322. { // Skills requiring specific weapon types
  5323. if( scw_list[i] == SC_DANCING && !battle_config.dancing_weaponswitch_fix )
  5324. continue;
  5325. if(sd->sc.data[scw_list[i]] &&
  5326. !pc_check_weapontype(sd,skill_get_weapontype(status_sc2skill(scw_list[i]))))
  5327. status_change_end(&sd->bl, scw_list[i], INVALID_TIMER);
  5328. }
  5329. if(sd->sc.data[SC_SPURT] && sd->status.weapon)
  5330. // Spurt requires bare hands (feet, in fact xD)
  5331. status_change_end(&sd->bl, SC_SPURT, INVALID_TIMER);
  5332. if(sd->status.shield <= 0) { // Skills requiring a shield
  5333. const enum sc_type scs_list[] = {
  5334. SC_AUTOGUARD,
  5335. SC_DEFENDER,
  5336. SC_REFLECTSHIELD,
  5337. SC_REFLECTDAMAGE
  5338. };
  5339. for (i = 0; i < ARRAYLENGTH(scs_list); i++)
  5340. if(sd->sc.data[scs_list[i]])
  5341. status_change_end(&sd->bl, scs_list[i], INVALID_TIMER);
  5342. }
  5343. }
  5344. /*==========================================
  5345. * Return equipped index of item on player sd at pos
  5346. * Return
  5347. * -1 : Nothing equipped
  5348. * idx : (this index could be used in inventory to found item_data)
  5349. *------------------------------------------*/
  5350. short pc_checkequip(struct map_session_data *sd,int pos, bool checkall)
  5351. {
  5352. uint8 i;
  5353. nullpo_retr(-1, sd);
  5354. for(i=0;i<EQI_MAX;i++){
  5355. if(pos & equip_bitmask[i]){
  5356. if( checkall && ( pos&~equip_bitmask[i] ) != 0 && sd->equip_index[i] == -1 ){
  5357. // Check all if any match is found
  5358. continue;
  5359. }
  5360. return sd->equip_index[i];
  5361. }
  5362. }
  5363. return -1;
  5364. }
  5365. /*==========================================
  5366. * Check if sd has nameid equipped somewhere
  5367. * @sd : the player session
  5368. * @nameid : id of the item to check
  5369. * @min : : see pc.hpp enum equip_index from ? to @max
  5370. * @max : see pc.hpp enum equip_index for @min to ?
  5371. * -return true,false
  5372. *------------------------------------------*/
  5373. bool pc_checkequip2(struct map_session_data *sd, unsigned short nameid, int min, int max)
  5374. {
  5375. int i;
  5376. for(i = min; i < max; i++) {
  5377. if(equip_bitmask[i]) {
  5378. int idx = sd->equip_index[i];
  5379. if (sd->inventory.u.items_inventory[idx].nameid == nameid)
  5380. return true;
  5381. }
  5382. }
  5383. return false;
  5384. }
  5385. /*==========================================
  5386. * Convert's from the client's lame Job ID system
  5387. * to the map server's 'makes sense' system. [Skotlex]
  5388. *------------------------------------------*/
  5389. int pc_jobid2mapid(unsigned short b_class)
  5390. {
  5391. switch(b_class)
  5392. {
  5393. //Novice And 1-1 Jobs
  5394. case JOB_NOVICE: return MAPID_NOVICE;
  5395. case JOB_SWORDMAN: return MAPID_SWORDMAN;
  5396. case JOB_MAGE: return MAPID_MAGE;
  5397. case JOB_ARCHER: return MAPID_ARCHER;
  5398. case JOB_ACOLYTE: return MAPID_ACOLYTE;
  5399. case JOB_MERCHANT: return MAPID_MERCHANT;
  5400. case JOB_THIEF: return MAPID_THIEF;
  5401. case JOB_TAEKWON: return MAPID_TAEKWON;
  5402. case JOB_WEDDING: return MAPID_WEDDING;
  5403. case JOB_GUNSLINGER: return MAPID_GUNSLINGER;
  5404. case JOB_NINJA: return MAPID_NINJA;
  5405. case JOB_XMAS: return MAPID_XMAS;
  5406. case JOB_SUMMER: return MAPID_SUMMER;
  5407. case JOB_HANBOK: return MAPID_HANBOK;
  5408. case JOB_GANGSI: return MAPID_GANGSI;
  5409. case JOB_OKTOBERFEST: return MAPID_OKTOBERFEST;
  5410. case JOB_SUMMER2: return MAPID_SUMMER2;
  5411. //2-1 Jobs
  5412. case JOB_SUPER_NOVICE: return MAPID_SUPER_NOVICE;
  5413. case JOB_KNIGHT: return MAPID_KNIGHT;
  5414. case JOB_WIZARD: return MAPID_WIZARD;
  5415. case JOB_HUNTER: return MAPID_HUNTER;
  5416. case JOB_PRIEST: return MAPID_PRIEST;
  5417. case JOB_BLACKSMITH: return MAPID_BLACKSMITH;
  5418. case JOB_ASSASSIN: return MAPID_ASSASSIN;
  5419. case JOB_STAR_GLADIATOR: return MAPID_STAR_GLADIATOR;
  5420. case JOB_KAGEROU:
  5421. case JOB_OBORO: return MAPID_KAGEROUOBORO;
  5422. case JOB_REBELLION: return MAPID_REBELLION;
  5423. case JOB_DEATH_KNIGHT: return MAPID_DEATH_KNIGHT;
  5424. //2-2 Jobs
  5425. case JOB_CRUSADER: return MAPID_CRUSADER;
  5426. case JOB_SAGE: return MAPID_SAGE;
  5427. case JOB_BARD:
  5428. case JOB_DANCER: return MAPID_BARDDANCER;
  5429. case JOB_MONK: return MAPID_MONK;
  5430. case JOB_ALCHEMIST: return MAPID_ALCHEMIST;
  5431. case JOB_ROGUE: return MAPID_ROGUE;
  5432. case JOB_SOUL_LINKER: return MAPID_SOUL_LINKER;
  5433. case JOB_DARK_COLLECTOR: return MAPID_DARK_COLLECTOR;
  5434. //Trans Novice And Trans 1-1 Jobs
  5435. case JOB_NOVICE_HIGH: return MAPID_NOVICE_HIGH;
  5436. case JOB_SWORDMAN_HIGH: return MAPID_SWORDMAN_HIGH;
  5437. case JOB_MAGE_HIGH: return MAPID_MAGE_HIGH;
  5438. case JOB_ARCHER_HIGH: return MAPID_ARCHER_HIGH;
  5439. case JOB_ACOLYTE_HIGH: return MAPID_ACOLYTE_HIGH;
  5440. case JOB_MERCHANT_HIGH: return MAPID_MERCHANT_HIGH;
  5441. case JOB_THIEF_HIGH: return MAPID_THIEF_HIGH;
  5442. //Trans 2-1 Jobs
  5443. case JOB_LORD_KNIGHT: return MAPID_LORD_KNIGHT;
  5444. case JOB_HIGH_WIZARD: return MAPID_HIGH_WIZARD;
  5445. case JOB_SNIPER: return MAPID_SNIPER;
  5446. case JOB_HIGH_PRIEST: return MAPID_HIGH_PRIEST;
  5447. case JOB_WHITESMITH: return MAPID_WHITESMITH;
  5448. case JOB_ASSASSIN_CROSS: return MAPID_ASSASSIN_CROSS;
  5449. //Trans 2-2 Jobs
  5450. case JOB_PALADIN: return MAPID_PALADIN;
  5451. case JOB_PROFESSOR: return MAPID_PROFESSOR;
  5452. case JOB_CLOWN:
  5453. case JOB_GYPSY: return MAPID_CLOWNGYPSY;
  5454. case JOB_CHAMPION: return MAPID_CHAMPION;
  5455. case JOB_CREATOR: return MAPID_CREATOR;
  5456. case JOB_STALKER: return MAPID_STALKER;
  5457. //Baby Novice And Baby 1-1 Jobs
  5458. case JOB_BABY: return MAPID_BABY;
  5459. case JOB_BABY_SWORDMAN: return MAPID_BABY_SWORDMAN;
  5460. case JOB_BABY_MAGE: return MAPID_BABY_MAGE;
  5461. case JOB_BABY_ARCHER: return MAPID_BABY_ARCHER;
  5462. case JOB_BABY_ACOLYTE: return MAPID_BABY_ACOLYTE;
  5463. case JOB_BABY_MERCHANT: return MAPID_BABY_MERCHANT;
  5464. case JOB_BABY_THIEF: return MAPID_BABY_THIEF;
  5465. case JOB_BABY_TAEKWON: return MAPID_BABY_TAEKWON;
  5466. case JOB_BABY_GUNSLINGER: return MAPID_BABY_GUNSLINGER;
  5467. case JOB_BABY_NINJA: return MAPID_BABY_NINJA;
  5468. case JOB_BABY_SUMMONER: return MAPID_BABY_SUMMONER;
  5469. //Baby 2-1 Jobs
  5470. case JOB_SUPER_BABY: return MAPID_SUPER_BABY;
  5471. case JOB_BABY_KNIGHT: return MAPID_BABY_KNIGHT;
  5472. case JOB_BABY_WIZARD: return MAPID_BABY_WIZARD;
  5473. case JOB_BABY_HUNTER: return MAPID_BABY_HUNTER;
  5474. case JOB_BABY_PRIEST: return MAPID_BABY_PRIEST;
  5475. case JOB_BABY_BLACKSMITH: return MAPID_BABY_BLACKSMITH;
  5476. case JOB_BABY_ASSASSIN: return MAPID_BABY_ASSASSIN;
  5477. case JOB_BABY_STAR_GLADIATOR: return MAPID_BABY_STAR_GLADIATOR;
  5478. case JOB_BABY_REBELLION: return MAPID_BABY_REBELLION;
  5479. case JOB_BABY_KAGEROU:
  5480. case JOB_BABY_OBORO: return MAPID_BABY_KAGEROUOBORO;
  5481. //Baby 2-2 Jobs
  5482. case JOB_BABY_CRUSADER: return MAPID_BABY_CRUSADER;
  5483. case JOB_BABY_SAGE: return MAPID_BABY_SAGE;
  5484. case JOB_BABY_BARD:
  5485. case JOB_BABY_DANCER: return MAPID_BABY_BARDDANCER;
  5486. case JOB_BABY_MONK: return MAPID_BABY_MONK;
  5487. case JOB_BABY_ALCHEMIST: return MAPID_BABY_ALCHEMIST;
  5488. case JOB_BABY_ROGUE: return MAPID_BABY_ROGUE;
  5489. case JOB_BABY_SOUL_LINKER: return MAPID_BABY_SOUL_LINKER;
  5490. //3-1 Jobs
  5491. case JOB_SUPER_NOVICE_E: return MAPID_SUPER_NOVICE_E;
  5492. case JOB_RUNE_KNIGHT: return MAPID_RUNE_KNIGHT;
  5493. case JOB_WARLOCK: return MAPID_WARLOCK;
  5494. case JOB_RANGER: return MAPID_RANGER;
  5495. case JOB_ARCH_BISHOP: return MAPID_ARCH_BISHOP;
  5496. case JOB_MECHANIC: return MAPID_MECHANIC;
  5497. case JOB_GUILLOTINE_CROSS: return MAPID_GUILLOTINE_CROSS;
  5498. case JOB_STAR_EMPEROR: return MAPID_STAR_EMPEROR;
  5499. //3-2 Jobs
  5500. case JOB_ROYAL_GUARD: return MAPID_ROYAL_GUARD;
  5501. case JOB_SORCERER: return MAPID_SORCERER;
  5502. case JOB_MINSTREL:
  5503. case JOB_WANDERER: return MAPID_MINSTRELWANDERER;
  5504. case JOB_SURA: return MAPID_SURA;
  5505. case JOB_GENETIC: return MAPID_GENETIC;
  5506. case JOB_SHADOW_CHASER: return MAPID_SHADOW_CHASER;
  5507. case JOB_SOUL_REAPER: return MAPID_SOUL_REAPER;
  5508. //Trans 3-1 Jobs
  5509. case JOB_RUNE_KNIGHT_T: return MAPID_RUNE_KNIGHT_T;
  5510. case JOB_WARLOCK_T: return MAPID_WARLOCK_T;
  5511. case JOB_RANGER_T: return MAPID_RANGER_T;
  5512. case JOB_ARCH_BISHOP_T: return MAPID_ARCH_BISHOP_T;
  5513. case JOB_MECHANIC_T: return MAPID_MECHANIC_T;
  5514. case JOB_GUILLOTINE_CROSS_T: return MAPID_GUILLOTINE_CROSS_T;
  5515. //Trans 3-2 Jobs
  5516. case JOB_ROYAL_GUARD_T: return MAPID_ROYAL_GUARD_T;
  5517. case JOB_SORCERER_T: return MAPID_SORCERER_T;
  5518. case JOB_MINSTREL_T:
  5519. case JOB_WANDERER_T: return MAPID_MINSTRELWANDERER_T;
  5520. case JOB_SURA_T: return MAPID_SURA_T;
  5521. case JOB_GENETIC_T: return MAPID_GENETIC_T;
  5522. case JOB_SHADOW_CHASER_T: return MAPID_SHADOW_CHASER_T;
  5523. //Baby 3-1 Jobs
  5524. case JOB_SUPER_BABY_E: return MAPID_SUPER_BABY_E;
  5525. case JOB_BABY_RUNE_KNIGHT: return MAPID_BABY_RUNE_KNIGHT;
  5526. case JOB_BABY_WARLOCK: return MAPID_BABY_WARLOCK;
  5527. case JOB_BABY_RANGER: return MAPID_BABY_RANGER;
  5528. case JOB_BABY_ARCH_BISHOP: return MAPID_BABY_ARCH_BISHOP;
  5529. case JOB_BABY_MECHANIC: return MAPID_BABY_MECHANIC;
  5530. case JOB_BABY_GUILLOTINE_CROSS: return MAPID_BABY_GUILLOTINE_CROSS;
  5531. case JOB_BABY_STAR_EMPEROR: return MAPID_BABY_STAR_EMPEROR;
  5532. //Baby 3-2 Jobs
  5533. case JOB_BABY_ROYAL_GUARD: return MAPID_BABY_ROYAL_GUARD;
  5534. case JOB_BABY_SORCERER: return MAPID_BABY_SORCERER;
  5535. case JOB_BABY_MINSTREL:
  5536. case JOB_BABY_WANDERER: return MAPID_BABY_MINSTRELWANDERER;
  5537. case JOB_BABY_SURA: return MAPID_BABY_SURA;
  5538. case JOB_BABY_GENETIC: return MAPID_BABY_GENETIC;
  5539. case JOB_BABY_SHADOW_CHASER: return MAPID_BABY_SHADOW_CHASER;
  5540. case JOB_BABY_SOUL_REAPER: return MAPID_BABY_SOUL_REAPER;
  5541. //Doram Jobs
  5542. case JOB_SUMMONER: return MAPID_SUMMONER;
  5543. default:
  5544. return -1;
  5545. }
  5546. }
  5547. //Reverts the map-style class id to the client-style one.
  5548. int pc_mapid2jobid(unsigned short class_, int sex)
  5549. {
  5550. switch(class_) {
  5551. //Novice And 1-1 Jobs
  5552. case MAPID_NOVICE: return JOB_NOVICE;
  5553. case MAPID_SWORDMAN: return JOB_SWORDMAN;
  5554. case MAPID_MAGE: return JOB_MAGE;
  5555. case MAPID_ARCHER: return JOB_ARCHER;
  5556. case MAPID_ACOLYTE: return JOB_ACOLYTE;
  5557. case MAPID_MERCHANT: return JOB_MERCHANT;
  5558. case MAPID_THIEF: return JOB_THIEF;
  5559. case MAPID_TAEKWON: return JOB_TAEKWON;
  5560. case MAPID_WEDDING: return JOB_WEDDING;
  5561. case MAPID_GUNSLINGER: return JOB_GUNSLINGER;
  5562. case MAPID_NINJA: return JOB_NINJA;
  5563. case MAPID_XMAS: return JOB_XMAS;
  5564. case MAPID_SUMMER: return JOB_SUMMER;
  5565. case MAPID_HANBOK: return JOB_HANBOK;
  5566. case MAPID_GANGSI: return JOB_GANGSI;
  5567. case MAPID_OKTOBERFEST: return JOB_OKTOBERFEST;
  5568. case MAPID_SUMMER2: return JOB_SUMMER2;
  5569. //2-1 Jobs
  5570. case MAPID_SUPER_NOVICE: return JOB_SUPER_NOVICE;
  5571. case MAPID_KNIGHT: return JOB_KNIGHT;
  5572. case MAPID_WIZARD: return JOB_WIZARD;
  5573. case MAPID_HUNTER: return JOB_HUNTER;
  5574. case MAPID_PRIEST: return JOB_PRIEST;
  5575. case MAPID_BLACKSMITH: return JOB_BLACKSMITH;
  5576. case MAPID_ASSASSIN: return JOB_ASSASSIN;
  5577. case MAPID_STAR_GLADIATOR: return JOB_STAR_GLADIATOR;
  5578. case MAPID_KAGEROUOBORO: return sex?JOB_KAGEROU:JOB_OBORO;
  5579. case MAPID_REBELLION: return JOB_REBELLION;
  5580. case MAPID_DEATH_KNIGHT: return JOB_DEATH_KNIGHT;
  5581. //2-2 Jobs
  5582. case MAPID_CRUSADER: return JOB_CRUSADER;
  5583. case MAPID_SAGE: return JOB_SAGE;
  5584. case MAPID_BARDDANCER: return sex?JOB_BARD:JOB_DANCER;
  5585. case MAPID_MONK: return JOB_MONK;
  5586. case MAPID_ALCHEMIST: return JOB_ALCHEMIST;
  5587. case MAPID_ROGUE: return JOB_ROGUE;
  5588. case MAPID_SOUL_LINKER: return JOB_SOUL_LINKER;
  5589. case MAPID_DARK_COLLECTOR: return JOB_DARK_COLLECTOR;
  5590. //Trans Novice And Trans 2-1 Jobs
  5591. case MAPID_NOVICE_HIGH: return JOB_NOVICE_HIGH;
  5592. case MAPID_SWORDMAN_HIGH: return JOB_SWORDMAN_HIGH;
  5593. case MAPID_MAGE_HIGH: return JOB_MAGE_HIGH;
  5594. case MAPID_ARCHER_HIGH: return JOB_ARCHER_HIGH;
  5595. case MAPID_ACOLYTE_HIGH: return JOB_ACOLYTE_HIGH;
  5596. case MAPID_MERCHANT_HIGH: return JOB_MERCHANT_HIGH;
  5597. case MAPID_THIEF_HIGH: return JOB_THIEF_HIGH;
  5598. //Trans 2-1 Jobs
  5599. case MAPID_LORD_KNIGHT: return JOB_LORD_KNIGHT;
  5600. case MAPID_HIGH_WIZARD: return JOB_HIGH_WIZARD;
  5601. case MAPID_SNIPER: return JOB_SNIPER;
  5602. case MAPID_HIGH_PRIEST: return JOB_HIGH_PRIEST;
  5603. case MAPID_WHITESMITH: return JOB_WHITESMITH;
  5604. case MAPID_ASSASSIN_CROSS: return JOB_ASSASSIN_CROSS;
  5605. //Trans 2-2 Jobs
  5606. case MAPID_PALADIN: return JOB_PALADIN;
  5607. case MAPID_PROFESSOR: return JOB_PROFESSOR;
  5608. case MAPID_CLOWNGYPSY: return sex?JOB_CLOWN:JOB_GYPSY;
  5609. case MAPID_CHAMPION: return JOB_CHAMPION;
  5610. case MAPID_CREATOR: return JOB_CREATOR;
  5611. case MAPID_STALKER: return JOB_STALKER;
  5612. //Baby Novice And Baby 1-1 Jobs
  5613. case MAPID_BABY: return JOB_BABY;
  5614. case MAPID_BABY_SWORDMAN: return JOB_BABY_SWORDMAN;
  5615. case MAPID_BABY_MAGE: return JOB_BABY_MAGE;
  5616. case MAPID_BABY_ARCHER: return JOB_BABY_ARCHER;
  5617. case MAPID_BABY_ACOLYTE: return JOB_BABY_ACOLYTE;
  5618. case MAPID_BABY_MERCHANT: return JOB_BABY_MERCHANT;
  5619. case MAPID_BABY_THIEF: return JOB_BABY_THIEF;
  5620. case MAPID_BABY_TAEKWON: return JOB_BABY_TAEKWON;
  5621. case MAPID_BABY_GUNSLINGER: return JOB_BABY_GUNSLINGER;
  5622. case MAPID_BABY_NINJA: return JOB_BABY_NINJA;
  5623. case MAPID_BABY_SUMMONER: return JOB_BABY_SUMMONER;
  5624. //Baby 2-1 Jobs
  5625. case MAPID_SUPER_BABY: return JOB_SUPER_BABY;
  5626. case MAPID_BABY_KNIGHT: return JOB_BABY_KNIGHT;
  5627. case MAPID_BABY_WIZARD: return JOB_BABY_WIZARD;
  5628. case MAPID_BABY_HUNTER: return JOB_BABY_HUNTER;
  5629. case MAPID_BABY_PRIEST: return JOB_BABY_PRIEST;
  5630. case MAPID_BABY_BLACKSMITH: return JOB_BABY_BLACKSMITH;
  5631. case MAPID_BABY_ASSASSIN: return JOB_BABY_ASSASSIN;
  5632. case MAPID_BABY_STAR_GLADIATOR: return JOB_BABY_STAR_GLADIATOR;
  5633. case MAPID_BABY_REBELLION: return JOB_BABY_REBELLION;
  5634. case MAPID_BABY_KAGEROUOBORO: return sex?JOB_BABY_KAGEROU:JOB_BABY_OBORO;
  5635. //Baby 2-2 Jobs
  5636. case MAPID_BABY_CRUSADER: return JOB_BABY_CRUSADER;
  5637. case MAPID_BABY_SAGE: return JOB_BABY_SAGE;
  5638. case MAPID_BABY_BARDDANCER: return sex?JOB_BABY_BARD:JOB_BABY_DANCER;
  5639. case MAPID_BABY_MONK: return JOB_BABY_MONK;
  5640. case MAPID_BABY_ALCHEMIST: return JOB_BABY_ALCHEMIST;
  5641. case MAPID_BABY_ROGUE: return JOB_BABY_ROGUE;
  5642. case MAPID_BABY_SOUL_LINKER: return JOB_BABY_SOUL_LINKER;
  5643. //3-1 Jobs
  5644. case MAPID_SUPER_NOVICE_E: return JOB_SUPER_NOVICE_E;
  5645. case MAPID_RUNE_KNIGHT: return JOB_RUNE_KNIGHT;
  5646. case MAPID_WARLOCK: return JOB_WARLOCK;
  5647. case MAPID_RANGER: return JOB_RANGER;
  5648. case MAPID_ARCH_BISHOP: return JOB_ARCH_BISHOP;
  5649. case MAPID_MECHANIC: return JOB_MECHANIC;
  5650. case MAPID_GUILLOTINE_CROSS: return JOB_GUILLOTINE_CROSS;
  5651. case MAPID_STAR_EMPEROR: return JOB_STAR_EMPEROR;
  5652. //3-2 Jobs
  5653. case MAPID_ROYAL_GUARD: return JOB_ROYAL_GUARD;
  5654. case MAPID_SORCERER: return JOB_SORCERER;
  5655. case MAPID_MINSTRELWANDERER: return sex?JOB_MINSTREL:JOB_WANDERER;
  5656. case MAPID_SURA: return JOB_SURA;
  5657. case MAPID_GENETIC: return JOB_GENETIC;
  5658. case MAPID_SHADOW_CHASER: return JOB_SHADOW_CHASER;
  5659. case MAPID_SOUL_REAPER: return JOB_SOUL_REAPER;
  5660. //Trans 3-1 Jobs
  5661. case MAPID_RUNE_KNIGHT_T: return JOB_RUNE_KNIGHT_T;
  5662. case MAPID_WARLOCK_T: return JOB_WARLOCK_T;
  5663. case MAPID_RANGER_T: return JOB_RANGER_T;
  5664. case MAPID_ARCH_BISHOP_T: return JOB_ARCH_BISHOP_T;
  5665. case MAPID_MECHANIC_T: return JOB_MECHANIC_T;
  5666. case MAPID_GUILLOTINE_CROSS_T: return JOB_GUILLOTINE_CROSS_T;
  5667. //Trans 3-2 Jobs
  5668. case MAPID_ROYAL_GUARD_T: return JOB_ROYAL_GUARD_T;
  5669. case MAPID_SORCERER_T: return JOB_SORCERER_T;
  5670. case MAPID_MINSTRELWANDERER_T: return sex?JOB_MINSTREL_T:JOB_WANDERER_T;
  5671. case MAPID_SURA_T: return JOB_SURA_T;
  5672. case MAPID_GENETIC_T: return JOB_GENETIC_T;
  5673. case MAPID_SHADOW_CHASER_T: return JOB_SHADOW_CHASER_T;
  5674. //Baby 3-1 Jobs
  5675. case MAPID_SUPER_BABY_E: return JOB_SUPER_BABY_E;
  5676. case MAPID_BABY_RUNE_KNIGHT: return JOB_BABY_RUNE_KNIGHT;
  5677. case MAPID_BABY_WARLOCK: return JOB_BABY_WARLOCK;
  5678. case MAPID_BABY_RANGER: return JOB_BABY_RANGER;
  5679. case MAPID_BABY_ARCH_BISHOP: return JOB_BABY_ARCH_BISHOP;
  5680. case MAPID_BABY_MECHANIC: return JOB_BABY_MECHANIC;
  5681. case MAPID_BABY_GUILLOTINE_CROSS: return JOB_BABY_GUILLOTINE_CROSS;
  5682. case MAPID_BABY_STAR_EMPEROR: return JOB_BABY_STAR_EMPEROR;
  5683. //Baby 3-2 Jobs
  5684. case MAPID_BABY_ROYAL_GUARD: return JOB_BABY_ROYAL_GUARD;
  5685. case MAPID_BABY_SORCERER: return JOB_BABY_SORCERER;
  5686. case MAPID_BABY_MINSTRELWANDERER: return sex?JOB_BABY_MINSTREL:JOB_BABY_WANDERER;
  5687. case MAPID_BABY_SURA: return JOB_BABY_SURA;
  5688. case MAPID_BABY_GENETIC: return JOB_BABY_GENETIC;
  5689. case MAPID_BABY_SHADOW_CHASER: return JOB_BABY_SHADOW_CHASER;
  5690. case MAPID_BABY_SOUL_REAPER: return JOB_BABY_SOUL_REAPER;
  5691. //Doram Jobs
  5692. case MAPID_SUMMONER: return JOB_SUMMONER;
  5693. default:
  5694. return -1;
  5695. }
  5696. }
  5697. /*====================================================
  5698. * This function return the name of the job (by [Yor])
  5699. *----------------------------------------------------*/
  5700. const char* job_name(int class_)
  5701. {
  5702. switch (class_) {
  5703. case JOB_NOVICE:
  5704. case JOB_SWORDMAN:
  5705. case JOB_MAGE:
  5706. case JOB_ARCHER:
  5707. case JOB_ACOLYTE:
  5708. case JOB_MERCHANT:
  5709. case JOB_THIEF:
  5710. return msg_txt(NULL,550 - JOB_NOVICE+class_);
  5711. case JOB_KNIGHT:
  5712. case JOB_PRIEST:
  5713. case JOB_WIZARD:
  5714. case JOB_BLACKSMITH:
  5715. case JOB_HUNTER:
  5716. case JOB_ASSASSIN:
  5717. return msg_txt(NULL,557 - JOB_KNIGHT+class_);
  5718. case JOB_KNIGHT2:
  5719. return msg_txt(NULL,557);
  5720. case JOB_CRUSADER:
  5721. case JOB_MONK:
  5722. case JOB_SAGE:
  5723. case JOB_ROGUE:
  5724. case JOB_ALCHEMIST:
  5725. case JOB_BARD:
  5726. case JOB_DANCER:
  5727. return msg_txt(NULL,563 - JOB_CRUSADER+class_);
  5728. case JOB_CRUSADER2:
  5729. return msg_txt(NULL,563);
  5730. case JOB_WEDDING:
  5731. case JOB_SUPER_NOVICE:
  5732. case JOB_GUNSLINGER:
  5733. case JOB_NINJA:
  5734. case JOB_XMAS:
  5735. return msg_txt(NULL,570 - JOB_WEDDING+class_);
  5736. case JOB_SUMMER:
  5737. case JOB_SUMMER2:
  5738. return msg_txt(NULL,621);
  5739. case JOB_HANBOK:
  5740. return msg_txt(NULL,694);
  5741. case JOB_OKTOBERFEST:
  5742. return msg_txt(NULL,696);
  5743. case JOB_NOVICE_HIGH:
  5744. case JOB_SWORDMAN_HIGH:
  5745. case JOB_MAGE_HIGH:
  5746. case JOB_ARCHER_HIGH:
  5747. case JOB_ACOLYTE_HIGH:
  5748. case JOB_MERCHANT_HIGH:
  5749. case JOB_THIEF_HIGH:
  5750. return msg_txt(NULL,575 - JOB_NOVICE_HIGH+class_);
  5751. case JOB_LORD_KNIGHT:
  5752. case JOB_HIGH_PRIEST:
  5753. case JOB_HIGH_WIZARD:
  5754. case JOB_WHITESMITH:
  5755. case JOB_SNIPER:
  5756. case JOB_ASSASSIN_CROSS:
  5757. return msg_txt(NULL,582 - JOB_LORD_KNIGHT+class_);
  5758. case JOB_LORD_KNIGHT2:
  5759. return msg_txt(NULL,582);
  5760. case JOB_PALADIN:
  5761. case JOB_CHAMPION:
  5762. case JOB_PROFESSOR:
  5763. case JOB_STALKER:
  5764. case JOB_CREATOR:
  5765. case JOB_CLOWN:
  5766. case JOB_GYPSY:
  5767. return msg_txt(NULL,588 - JOB_PALADIN + class_);
  5768. case JOB_PALADIN2:
  5769. return msg_txt(NULL,588);
  5770. case JOB_BABY:
  5771. case JOB_BABY_SWORDMAN:
  5772. case JOB_BABY_MAGE:
  5773. case JOB_BABY_ARCHER:
  5774. case JOB_BABY_ACOLYTE:
  5775. case JOB_BABY_MERCHANT:
  5776. case JOB_BABY_THIEF:
  5777. return msg_txt(NULL,595 - JOB_BABY + class_);
  5778. case JOB_BABY_KNIGHT:
  5779. case JOB_BABY_PRIEST:
  5780. case JOB_BABY_WIZARD:
  5781. case JOB_BABY_BLACKSMITH:
  5782. case JOB_BABY_HUNTER:
  5783. case JOB_BABY_ASSASSIN:
  5784. return msg_txt(NULL,602 - JOB_BABY_KNIGHT + class_);
  5785. case JOB_BABY_KNIGHT2:
  5786. return msg_txt(NULL,602);
  5787. case JOB_BABY_CRUSADER:
  5788. case JOB_BABY_MONK:
  5789. case JOB_BABY_SAGE:
  5790. case JOB_BABY_ROGUE:
  5791. case JOB_BABY_ALCHEMIST:
  5792. case JOB_BABY_BARD:
  5793. case JOB_BABY_DANCER:
  5794. return msg_txt(NULL,608 - JOB_BABY_CRUSADER + class_);
  5795. case JOB_BABY_CRUSADER2:
  5796. return msg_txt(NULL,608);
  5797. case JOB_SUPER_BABY:
  5798. return msg_txt(NULL,615);
  5799. case JOB_TAEKWON:
  5800. return msg_txt(NULL,616);
  5801. case JOB_STAR_GLADIATOR:
  5802. case JOB_STAR_GLADIATOR2:
  5803. return msg_txt(NULL,617);
  5804. case JOB_SOUL_LINKER:
  5805. return msg_txt(NULL,618);
  5806. case JOB_GANGSI:
  5807. case JOB_DEATH_KNIGHT:
  5808. case JOB_DARK_COLLECTOR:
  5809. return msg_txt(NULL,622 - JOB_GANGSI+class_);
  5810. case JOB_RUNE_KNIGHT:
  5811. case JOB_WARLOCK:
  5812. case JOB_RANGER:
  5813. case JOB_ARCH_BISHOP:
  5814. case JOB_MECHANIC:
  5815. case JOB_GUILLOTINE_CROSS:
  5816. return msg_txt(NULL,625 - JOB_RUNE_KNIGHT+class_);
  5817. case JOB_RUNE_KNIGHT_T:
  5818. case JOB_WARLOCK_T:
  5819. case JOB_RANGER_T:
  5820. case JOB_ARCH_BISHOP_T:
  5821. case JOB_MECHANIC_T:
  5822. case JOB_GUILLOTINE_CROSS_T:
  5823. return msg_txt(NULL,681 - JOB_RUNE_KNIGHT_T+class_);
  5824. case JOB_ROYAL_GUARD:
  5825. case JOB_SORCERER:
  5826. case JOB_MINSTREL:
  5827. case JOB_WANDERER:
  5828. case JOB_SURA:
  5829. case JOB_GENETIC:
  5830. case JOB_SHADOW_CHASER:
  5831. return msg_txt(NULL,631 - JOB_ROYAL_GUARD+class_);
  5832. case JOB_ROYAL_GUARD_T:
  5833. case JOB_SORCERER_T:
  5834. case JOB_MINSTREL_T:
  5835. case JOB_WANDERER_T:
  5836. case JOB_SURA_T:
  5837. case JOB_GENETIC_T:
  5838. case JOB_SHADOW_CHASER_T:
  5839. return msg_txt(NULL,687 - JOB_ROYAL_GUARD_T+class_);
  5840. case JOB_RUNE_KNIGHT2:
  5841. case JOB_RUNE_KNIGHT_T2:
  5842. return msg_txt(NULL,625);
  5843. case JOB_ROYAL_GUARD2:
  5844. case JOB_ROYAL_GUARD_T2:
  5845. return msg_txt(NULL,631);
  5846. case JOB_RANGER2:
  5847. case JOB_RANGER_T2:
  5848. return msg_txt(NULL,627);
  5849. case JOB_MECHANIC2:
  5850. case JOB_MECHANIC_T2:
  5851. return msg_txt(NULL,629);
  5852. case JOB_BABY_RUNE_KNIGHT:
  5853. case JOB_BABY_WARLOCK:
  5854. case JOB_BABY_RANGER:
  5855. case JOB_BABY_ARCH_BISHOP:
  5856. case JOB_BABY_MECHANIC:
  5857. case JOB_BABY_GUILLOTINE_CROSS:
  5858. case JOB_BABY_ROYAL_GUARD:
  5859. case JOB_BABY_SORCERER:
  5860. case JOB_BABY_MINSTREL:
  5861. case JOB_BABY_WANDERER:
  5862. case JOB_BABY_SURA:
  5863. case JOB_BABY_GENETIC:
  5864. case JOB_BABY_SHADOW_CHASER:
  5865. return msg_txt(NULL,638 - JOB_BABY_RUNE_KNIGHT+class_);
  5866. case JOB_BABY_RUNE_KNIGHT2:
  5867. return msg_txt(NULL,638);
  5868. case JOB_BABY_ROYAL_GUARD2:
  5869. return msg_txt(NULL,644);
  5870. case JOB_BABY_RANGER2:
  5871. return msg_txt(NULL,640);
  5872. case JOB_BABY_MECHANIC2:
  5873. return msg_txt(NULL,642);
  5874. case JOB_SUPER_NOVICE_E:
  5875. case JOB_SUPER_BABY_E:
  5876. return msg_txt(NULL,651 - JOB_SUPER_NOVICE_E+class_);
  5877. case JOB_KAGEROU:
  5878. case JOB_OBORO:
  5879. return msg_txt(NULL,653 - JOB_KAGEROU+class_);
  5880. case JOB_REBELLION:
  5881. return msg_txt(NULL,695);
  5882. case JOB_SUMMONER:
  5883. return msg_txt(NULL,697);
  5884. case JOB_BABY_SUMMONER:
  5885. return msg_txt(NULL,698);
  5886. case JOB_BABY_NINJA:
  5887. return msg_txt(NULL,699);
  5888. case JOB_BABY_KAGEROU:
  5889. case JOB_BABY_OBORO:
  5890. case JOB_BABY_TAEKWON:
  5891. case JOB_BABY_STAR_GLADIATOR:
  5892. case JOB_BABY_SOUL_LINKER:
  5893. case JOB_BABY_GUNSLINGER:
  5894. case JOB_BABY_REBELLION:
  5895. return msg_txt(NULL,753 - JOB_BABY_KAGEROU+class_);
  5896. case JOB_BABY_STAR_GLADIATOR2:
  5897. return msg_txt(NULL,756);
  5898. case JOB_STAR_EMPEROR:
  5899. case JOB_SOUL_REAPER:
  5900. case JOB_BABY_STAR_EMPEROR:
  5901. case JOB_BABY_SOUL_REAPER:
  5902. return msg_txt(NULL,782 - JOB_STAR_EMPEROR + class_);
  5903. case JOB_STAR_EMPEROR2:
  5904. return msg_txt(NULL,782);
  5905. case JOB_BABY_STAR_EMPEROR2:
  5906. return msg_txt(NULL,784);
  5907. default:
  5908. return msg_txt(NULL,655);
  5909. }
  5910. }
  5911. /*====================================================
  5912. * Timered function to make id follow a target.
  5913. * @id = bl.id (player only atm)
  5914. * target is define in sd->followtarget (bl.id)
  5915. * used by pc_follow
  5916. *----------------------------------------------------*/
  5917. TIMER_FUNC(pc_follow_timer){
  5918. struct map_session_data *sd;
  5919. struct block_list *tbl;
  5920. sd = map_id2sd(id);
  5921. nullpo_ret(sd);
  5922. if (sd->followtimer != tid){
  5923. ShowError("pc_follow_timer %d != %d\n",sd->followtimer,tid);
  5924. sd->followtimer = INVALID_TIMER;
  5925. return 0;
  5926. }
  5927. sd->followtimer = INVALID_TIMER;
  5928. tbl = map_id2bl(sd->followtarget);
  5929. if (tbl == NULL || pc_isdead(sd))
  5930. {
  5931. pc_stop_following(sd);
  5932. return 0;
  5933. }
  5934. // either player or target is currently detached from map blocks (could be teleporting),
  5935. // but still connected to this map, so we'll just increment the timer and check back later
  5936. if (sd->bl.prev != NULL && tbl->prev != NULL &&
  5937. sd->ud.skilltimer == INVALID_TIMER && sd->ud.attacktimer == INVALID_TIMER && sd->ud.walktimer == INVALID_TIMER)
  5938. {
  5939. if((sd->bl.m == tbl->m) && unit_can_reach_bl(&sd->bl,tbl, AREA_SIZE, 0, NULL, NULL)) {
  5940. if (!check_distance_bl(&sd->bl, tbl, 5))
  5941. unit_walktobl(&sd->bl, tbl, 5, 0);
  5942. } else
  5943. pc_setpos(sd, map_id2index(tbl->m), tbl->x, tbl->y, CLR_TELEPORT);
  5944. }
  5945. sd->followtimer = add_timer(
  5946. tick + 1000, // increase time a bit to loosen up map's load
  5947. pc_follow_timer, sd->bl.id, 0);
  5948. return 0;
  5949. }
  5950. int pc_stop_following (struct map_session_data *sd)
  5951. {
  5952. nullpo_ret(sd);
  5953. if (sd->followtimer != INVALID_TIMER) {
  5954. delete_timer(sd->followtimer,pc_follow_timer);
  5955. sd->followtimer = INVALID_TIMER;
  5956. }
  5957. sd->followtarget = -1;
  5958. sd->ud.target_to = 0;
  5959. unit_stop_walking(&sd->bl, 1);
  5960. return 0;
  5961. }
  5962. int pc_follow(struct map_session_data *sd,int target_id)
  5963. {
  5964. struct block_list *bl = map_id2bl(target_id);
  5965. if (bl == NULL /*|| bl->type != BL_PC*/)
  5966. return 1;
  5967. if (sd->followtimer != INVALID_TIMER)
  5968. pc_stop_following(sd);
  5969. sd->followtarget = target_id;
  5970. pc_follow_timer(INVALID_TIMER, gettick(), sd->bl.id, 0);
  5971. return 0;
  5972. }
  5973. int pc_checkbaselevelup(struct map_session_data *sd) {
  5974. unsigned int next = pc_nextbaseexp(sd);
  5975. if (!next || sd->status.base_exp < next || pc_is_maxbaselv(sd))
  5976. return 0;
  5977. do {
  5978. sd->status.base_exp -= next;
  5979. //Kyoki pointed out that the max overcarry exp is the exp needed for the previous level -1. [Skotlex]
  5980. 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 )
  5981. sd->status.base_exp = next-1;
  5982. next = pc_gets_status_point(sd->status.base_level);
  5983. sd->status.base_level++;
  5984. sd->status.status_point += next;
  5985. if( pc_is_maxbaselv(sd) ){
  5986. sd->status.base_exp = u32min(sd->status.base_exp,MAX_LEVEL_BASE_EXP);
  5987. break;
  5988. }
  5989. } while ((next=pc_nextbaseexp(sd)) > 0 && sd->status.base_exp >= next);
  5990. if (battle_config.pet_lv_rate && sd->pd) //<Skotlex> update pet's level
  5991. status_calc_pet(sd->pd,SCO_NONE);
  5992. clif_updatestatus(sd,SP_STATUSPOINT);
  5993. clif_updatestatus(sd,SP_BASELEVEL);
  5994. clif_updatestatus(sd,SP_BASEEXP);
  5995. clif_updatestatus(sd,SP_NEXTBASEEXP);
  5996. status_calc_pc(sd,SCO_FORCE);
  5997. status_percent_heal(&sd->bl,100,100);
  5998. if ((sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE) {
  5999. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_KYRIE),100,1,skill_get_time(PR_KYRIE,1));
  6000. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_IMPOSITIO),100,1,skill_get_time(PR_IMPOSITIO,1));
  6001. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_MAGNIFICAT),100,1,skill_get_time(PR_MAGNIFICAT,1));
  6002. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_GLORIA),100,1,skill_get_time(PR_GLORIA,1));
  6003. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_SUFFRAGIUM),100,1,skill_get_time(PR_SUFFRAGIUM,1));
  6004. if (sd->state.snovice_dead_flag)
  6005. sd->state.snovice_dead_flag = 0; //Reenable steelbody resurrection on dead.
  6006. } else if( (sd->class_&MAPID_BASEMASK) == MAPID_TAEKWON ) {
  6007. sc_start(&sd->bl,&sd->bl,status_skill2sc(AL_INCAGI),100,10,600000);
  6008. sc_start(&sd->bl,&sd->bl,status_skill2sc(AL_BLESSING),100,10,600000);
  6009. }
  6010. clif_misceffect(&sd->bl,0);
  6011. npc_script_event(sd, NPCE_BASELVUP); //LORDALFA - LVLUPEVENT
  6012. if(sd->status.party_id)
  6013. party_send_levelup(sd);
  6014. pc_baselevelchanged(sd);
  6015. achievement_update_objective(sd, AG_GOAL_LEVEL, 1, sd->status.base_level);
  6016. achievement_update_objective(sd, AG_GOAL_STATUS, 2, sd->status.base_level, sd->status.class_);
  6017. return 1;
  6018. }
  6019. void pc_baselevelchanged(struct map_session_data *sd) {
  6020. uint8 i;
  6021. for( i = 0; i < EQI_MAX; i++ ) {
  6022. if( sd->equip_index[i] >= 0 ) {
  6023. if( sd->inventory_data[ sd->equip_index[i] ]->elvmax && sd->status.base_level > (unsigned int)sd->inventory_data[ sd->equip_index[i] ]->elvmax )
  6024. pc_unequipitem(sd, sd->equip_index[i], 3);
  6025. }
  6026. }
  6027. pc_show_questinfo(sd);
  6028. }
  6029. int pc_checkjoblevelup(struct map_session_data *sd)
  6030. {
  6031. unsigned int next = pc_nextjobexp(sd);
  6032. nullpo_ret(sd);
  6033. if(!next || sd->status.job_exp < next || pc_is_maxjoblv(sd))
  6034. return 0;
  6035. do {
  6036. sd->status.job_exp -= next;
  6037. //Kyoki pointed out that the max overcarry exp is the exp needed for the previous level -1. [Skotlex]
  6038. 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 )
  6039. sd->status.job_exp = next-1;
  6040. sd->status.job_level ++;
  6041. sd->status.skill_point ++;
  6042. if( pc_is_maxjoblv(sd) ){
  6043. sd->status.job_exp = u32min(sd->status.job_exp,MAX_LEVEL_JOB_EXP);
  6044. break;
  6045. }
  6046. } while ((next=pc_nextjobexp(sd)) > 0 && sd->status.job_exp >= next);
  6047. clif_updatestatus(sd,SP_JOBLEVEL);
  6048. clif_updatestatus(sd,SP_JOBEXP);
  6049. clif_updatestatus(sd,SP_NEXTJOBEXP);
  6050. clif_updatestatus(sd,SP_SKILLPOINT);
  6051. status_calc_pc(sd,SCO_FORCE);
  6052. clif_misceffect(&sd->bl,1);
  6053. if (pc_checkskill(sd, SG_DEVIL) && pc_is_maxbaselv(sd))
  6054. clif_status_change(&sd->bl, EFST_DEVIL1, 1, 0, 0, 0, 1); //Permanent blind effect from SG_DEVIL.
  6055. npc_script_event(sd, NPCE_JOBLVUP);
  6056. achievement_update_objective(sd, AG_GOAL_LEVEL, 1, sd->status.job_level);
  6057. pc_show_questinfo(sd);
  6058. return 1;
  6059. }
  6060. /** Alters experiences calculation based on self bonuses that do not get even shared to the party.
  6061. * @param sd Player
  6062. * @param base_exp Base EXP before peronal bonuses
  6063. * @param job_exp Job EXP before peronal bonuses
  6064. * @param src Block list that affecting the exp calculation
  6065. */
  6066. static void pc_calcexp(struct map_session_data *sd, unsigned int *base_exp, unsigned int *job_exp, struct block_list *src)
  6067. {
  6068. int bonus = 0, vip_bonus_base = 0, vip_bonus_job = 0;
  6069. if (src) {
  6070. struct status_data *status = status_get_status_data(src);
  6071. if( sd->expaddrace[status->race] )
  6072. bonus += sd->expaddrace[status->race];
  6073. if( sd->expaddrace[RC_ALL] )
  6074. bonus += sd->expaddrace[RC_ALL];
  6075. if( sd->expaddclass[status->class_] )
  6076. bonus += sd->expaddclass[status->class_];
  6077. if( sd->expaddclass[CLASS_ALL] )
  6078. bonus += sd->expaddclass[CLASS_ALL];
  6079. if (battle_config.pk_mode &&
  6080. (int)(status_get_lv(src) - sd->status.base_level) >= 20)
  6081. bonus += 15; // pk_mode additional exp if monster >20 levels [Valaris]
  6082. if (src && src->type == BL_MOB && pc_isvip(sd)) { // EXP bonus for VIP player
  6083. vip_bonus_base = battle_config.vip_base_exp_increase;
  6084. vip_bonus_job = battle_config.vip_job_exp_increase;
  6085. }
  6086. }
  6087. // Give EXPBOOST for quests even if src is NULL.
  6088. if (sd->sc.data[SC_EXPBOOST]) {
  6089. bonus += sd->sc.data[SC_EXPBOOST]->val1;
  6090. if (battle_config.vip_bm_increase && pc_isvip(sd)) // Increase Battle Manual EXP rate for VIP
  6091. bonus += (sd->sc.data[SC_EXPBOOST]->val1 / battle_config.vip_bm_increase);
  6092. }
  6093. if (*base_exp) {
  6094. unsigned int exp = (unsigned int)(*base_exp + (double)*base_exp * (bonus + vip_bonus_base)/100.);
  6095. *base_exp = cap_value(exp, 1, UINT_MAX);
  6096. }
  6097. // Give JEXPBOOST for quests even if src is NULL.
  6098. if (sd->sc.data[SC_JEXPBOOST])
  6099. bonus += sd->sc.data[SC_JEXPBOOST]->val1;
  6100. if (*job_exp) {
  6101. unsigned int exp = (unsigned int)(*job_exp + (double)*job_exp * (bonus + vip_bonus_job)/100.);
  6102. *job_exp = cap_value(exp, 1, UINT_MAX);
  6103. }
  6104. return;
  6105. }
  6106. /**
  6107. * Show EXP gained by player in percentage by @showexp
  6108. * @param sd Player
  6109. * @param base_exp Base EXP gained/loss
  6110. * @param next_base_exp Base EXP needed for next base level
  6111. * @param job_exp Job EXP gained/loss
  6112. * @param next_job_exp Job EXP needed for next job level
  6113. * @param lost True:EXP penalty, lose EXP
  6114. **/
  6115. 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) {
  6116. char output[CHAT_SIZE_MAX];
  6117. nullpo_retv(sd);
  6118. sprintf(output, msg_txt(sd,743), // Experience %s Base:%ld (%0.2f%%) Job:%ld (%0.2f%%)
  6119. (lost) ? msg_txt(sd,742) : msg_txt(sd,741),
  6120. (long)base_exp * (lost ? -1 : 1), (base_exp / (float)next_base_exp * 100 * (lost ? -1 : 1)),
  6121. (long)job_exp * (lost ? -1 : 1), (job_exp / (float)next_job_exp * 100 * (lost ? -1 : 1)));
  6122. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  6123. }
  6124. /**
  6125. * Give Base or Job EXP to player, then calculate remaining exp for next lvl
  6126. * @param sd Player
  6127. * @param src EXP source
  6128. * @param base_exp Base EXP gained
  6129. * @param base_exp Job EXP gained
  6130. * @param exp_flag 1: Quest EXP; 2: Param Exp (Ignore Guild EXP tax, EXP adjustments)
  6131. * @return
  6132. **/
  6133. void pc_gainexp(struct map_session_data *sd, struct block_list *src, unsigned int base_exp, unsigned int job_exp, uint8 exp_flag)
  6134. {
  6135. unsigned int nextb = 0, nextj = 0;
  6136. uint8 flag = 0; ///< 1: Base EXP given, 2: Job EXP given, 4: Max Base level, 8: Max Job Level
  6137. nullpo_retv(sd);
  6138. if(sd->bl.prev == NULL || pc_isdead(sd))
  6139. return;
  6140. if (!(exp_flag&2)) {
  6141. if (!battle_config.pvp_exp && map_getmapflag(sd->bl.m, MF_PVP)) // [MouseJstr]
  6142. return; // no exp on pvp maps
  6143. if (sd->status.guild_id>0)
  6144. base_exp -= guild_payexp(sd,base_exp);
  6145. }
  6146. flag = ((base_exp) ? 1 : 0) |
  6147. ((job_exp) ? 2 : 0) |
  6148. ((pc_is_maxbaselv(sd)) ? 4 : 0) |
  6149. ((pc_is_maxjoblv(sd)) ? 8 : 0);
  6150. if (!(exp_flag&2))
  6151. pc_calcexp(sd, &base_exp, &job_exp, src);
  6152. nextb = pc_nextbaseexp(sd);
  6153. nextj = pc_nextjobexp(sd);
  6154. if (flag&4){
  6155. if( sd->status.base_exp >= MAX_LEVEL_BASE_EXP )
  6156. base_exp = 0;
  6157. else if( sd->status.base_exp + base_exp >= MAX_LEVEL_BASE_EXP )
  6158. base_exp = MAX_LEVEL_BASE_EXP - sd->status.base_exp;
  6159. }
  6160. if (flag&8){
  6161. if( sd->status.job_exp >= MAX_LEVEL_JOB_EXP )
  6162. job_exp = 0;
  6163. else if( sd->status.job_exp + job_exp >= MAX_LEVEL_JOB_EXP )
  6164. job_exp = MAX_LEVEL_JOB_EXP - sd->status.job_exp;
  6165. }
  6166. if (!(exp_flag&2) && battle_config.max_exp_gain_rate && (base_exp || job_exp)) {
  6167. //Note that this value should never be greater than the original
  6168. //therefore no overflow checks are needed. [Skotlex]
  6169. if (nextb > 0) {
  6170. float nextbp = (float) base_exp / (float) nextb;
  6171. if (nextbp > battle_config.max_exp_gain_rate/1000.)
  6172. base_exp = (unsigned int)(battle_config.max_exp_gain_rate/1000.*nextb);
  6173. }
  6174. if (nextj > 0) {
  6175. float nextjp = (float) job_exp / (float) nextj;
  6176. if (nextjp > battle_config.max_exp_gain_rate/1000.)
  6177. job_exp = (unsigned int)(battle_config.max_exp_gain_rate/1000.*nextj);
  6178. }
  6179. }
  6180. // Give EXP for Base Level
  6181. if (base_exp) {
  6182. if ((uint64)sd->status.base_exp + base_exp > UINT32_MAX)
  6183. sd->status.base_exp = UINT32_MAX;
  6184. else
  6185. sd->status.base_exp += base_exp;
  6186. if (!pc_checkbaselevelup(sd))
  6187. clif_updatestatus(sd,SP_BASEEXP);
  6188. }
  6189. // Give EXP for Job Level
  6190. if (job_exp) {
  6191. if ((uint64)sd->status.job_exp + job_exp > UINT32_MAX)
  6192. sd->status.job_exp = UINT32_MAX;
  6193. else
  6194. sd->status.job_exp += job_exp;
  6195. if (!pc_checkjoblevelup(sd))
  6196. clif_updatestatus(sd,SP_JOBEXP);
  6197. }
  6198. if (flag&1)
  6199. clif_displayexp(sd, (flag&4) ? 0 : base_exp, SP_BASEEXP, exp_flag&1, false);
  6200. if (flag&2)
  6201. clif_displayexp(sd, (flag&8) ? 0 : job_exp, SP_JOBEXP, exp_flag&1, false);
  6202. if (sd->state.showexp && (base_exp || job_exp))
  6203. pc_gainexp_disp(sd, base_exp, nextb, job_exp, nextj, false);
  6204. }
  6205. /**
  6206. * Lost Base/Job EXP from a player
  6207. * @param sd Player
  6208. * @param base_exp Base EXP lost
  6209. * @param job_exp Job EXP lost
  6210. **/
  6211. void pc_lostexp(struct map_session_data *sd, unsigned int base_exp, unsigned int job_exp) {
  6212. nullpo_retv(sd);
  6213. if (base_exp) {
  6214. base_exp = u32min(sd->status.base_exp, base_exp);
  6215. sd->status.base_exp -= base_exp;
  6216. clif_displayexp(sd, base_exp, SP_BASEEXP, false, true);
  6217. clif_updatestatus(sd, SP_BASEEXP);
  6218. }
  6219. if (job_exp) {
  6220. job_exp = u32min(sd->status.job_exp, job_exp);
  6221. sd->status.job_exp -= job_exp;
  6222. clif_displayexp(sd, job_exp, SP_JOBEXP, false, true);
  6223. clif_updatestatus(sd, SP_JOBEXP);
  6224. }
  6225. if (sd->state.showexp && (base_exp || job_exp))
  6226. pc_gainexp_disp(sd, base_exp, pc_nextbaseexp(sd), job_exp, pc_nextjobexp(sd), true);
  6227. }
  6228. /**
  6229. * Returns max base level for this character's class.
  6230. * @param class_: Player's class
  6231. * @return Max Base Level
  6232. */
  6233. static unsigned int pc_class_maxbaselv(unsigned short class_) {
  6234. return job_info[pc_class2idx(class_)].max_level[0];
  6235. }
  6236. /**
  6237. * Returns max base level for this character.
  6238. * @param sd Player
  6239. * @return Max Base Level
  6240. **/
  6241. unsigned int pc_maxbaselv(struct map_session_data *sd){
  6242. return pc_class_maxbaselv(sd->status.class_);
  6243. }
  6244. /**
  6245. * Returns max job level for this character's class.
  6246. * @param class_: Player's class
  6247. * @return Max Job Level
  6248. */
  6249. static unsigned int pc_class_maxjoblv(unsigned short class_) {
  6250. return job_info[pc_class2idx(class_)].max_level[1];
  6251. }
  6252. /**
  6253. * Returns max job level for this character.
  6254. * @param sd Player
  6255. * @return Max Job Level
  6256. **/
  6257. unsigned int pc_maxjoblv(struct map_session_data *sd){
  6258. return pc_class_maxjoblv(sd->status.class_);
  6259. }
  6260. /**
  6261. * Check if player is reached max base level
  6262. * @param sd
  6263. * @return True if reached max level
  6264. **/
  6265. bool pc_is_maxbaselv(struct map_session_data *sd) {
  6266. nullpo_retr(false, sd);
  6267. return (sd->status.base_level >= pc_maxbaselv(sd));
  6268. }
  6269. /**
  6270. * Check if player is reached max base level
  6271. * @param sd
  6272. * @return True if reached max level
  6273. **/
  6274. bool pc_is_maxjoblv(struct map_session_data *sd) {
  6275. nullpo_retr(false, sd);
  6276. return (sd->status.job_level >= pc_maxjoblv(sd));
  6277. }
  6278. /**
  6279. * Base exp needed for player to level up.
  6280. * @param sd
  6281. * @return Base EXP needed for next base level
  6282. **/
  6283. unsigned int pc_nextbaseexp(struct map_session_data *sd){
  6284. nullpo_ret(sd);
  6285. if (sd->status.base_level == 0) // Is this something that possible?
  6286. return 0;
  6287. if (pc_is_maxbaselv(sd))
  6288. return MAX_LEVEL_BASE_EXP; // On max level, player's base EXP limit is 99,999,999
  6289. return job_info[pc_class2idx(sd->status.class_)].exp_table[0][sd->status.base_level-1];
  6290. }
  6291. /**
  6292. * Job exp needed for player to level up.
  6293. * @param sd
  6294. * @return Job EXP needed for next job level
  6295. **/
  6296. unsigned int pc_nextjobexp(struct map_session_data *sd){
  6297. nullpo_ret(sd);
  6298. if (sd->status.job_level == 0) // Is this something that possible?
  6299. return 0;
  6300. if (pc_is_maxjoblv(sd))
  6301. return MAX_LEVEL_JOB_EXP; // On max level, player's job EXP limit is 999,999,999
  6302. return job_info[pc_class2idx(sd->status.class_)].exp_table[1][sd->status.job_level-1];
  6303. }
  6304. /// Returns the value of the specified stat.
  6305. static int pc_getstat(struct map_session_data* sd, int type)
  6306. {
  6307. nullpo_retr(-1, sd);
  6308. switch( type ) {
  6309. case SP_STR: return sd->status.str;
  6310. case SP_AGI: return sd->status.agi;
  6311. case SP_VIT: return sd->status.vit;
  6312. case SP_INT: return sd->status.int_;
  6313. case SP_DEX: return sd->status.dex;
  6314. case SP_LUK: return sd->status.luk;
  6315. default:
  6316. return -1;
  6317. }
  6318. }
  6319. /// Sets the specified stat to the specified value.
  6320. /// Returns the new value.
  6321. static int pc_setstat(struct map_session_data* sd, int type, int val)
  6322. {
  6323. nullpo_retr(-1, sd);
  6324. switch( type ) {
  6325. case SP_STR: sd->status.str = val; break;
  6326. case SP_AGI: sd->status.agi = val; break;
  6327. case SP_VIT: sd->status.vit = val; break;
  6328. case SP_INT: sd->status.int_ = val; break;
  6329. case SP_DEX: sd->status.dex = val; break;
  6330. case SP_LUK: sd->status.luk = val; break;
  6331. default:
  6332. return -1;
  6333. }
  6334. return val;
  6335. }
  6336. // Calculates the number of status points PC gets when leveling up (from level to level+1)
  6337. int pc_gets_status_point(int level)
  6338. {
  6339. if (battle_config.use_statpoint_table) //Use values from "db/statpoint.txt"
  6340. return (statp[level+1] - statp[level]);
  6341. else //Default increase
  6342. return ((level+15) / 5);
  6343. }
  6344. #ifdef RENEWAL_STAT
  6345. /// Renewal status point cost formula
  6346. #define PC_STATUS_POINT_COST(low) (((low) < 100) ? (2 + ((low) - 1) / 10) : (16 + 4 * (((low) - 100) / 5)))
  6347. #else
  6348. /// Pre-Renewal status point cost formula
  6349. #define PC_STATUS_POINT_COST(low) (( 1 + ((low) + 9) / 10 ))
  6350. #endif
  6351. /// Returns the number of stat points needed to change the specified stat by val.
  6352. /// If val is negative, returns the number of stat points that would be needed to
  6353. /// raise the specified stat from (current value - val) to current value.
  6354. int pc_need_status_point(struct map_session_data* sd, int type, int val)
  6355. {
  6356. int low, high, sp = 0, max = 0;
  6357. if ( val == 0 )
  6358. return 0;
  6359. low = pc_getstat(sd,type);
  6360. max = pc_maxparameter(sd,(enum e_params)(type-SP_STR));
  6361. if ( low >= max && val > 0 )
  6362. return 0; // Official servers show '0' when max is reached
  6363. high = low + val;
  6364. if ( val < 0 )
  6365. SWAP(low, high);
  6366. for ( ; low < high; low++ )
  6367. sp += PC_STATUS_POINT_COST(low);
  6368. return sp;
  6369. }
  6370. /**
  6371. * Returns the value the specified stat can be increased by with the current
  6372. * amount of available status points for the current character's class.
  6373. *
  6374. * @param sd The target character.
  6375. * @param type Stat to verify.
  6376. * @return Maximum value the stat could grow by.
  6377. */
  6378. int pc_maxparameterincrease(struct map_session_data* sd, int type)
  6379. {
  6380. int base, final_val, status_points, max_param;
  6381. nullpo_ret(sd);
  6382. base = final_val = pc_getstat(sd, type);
  6383. status_points = sd->status.status_point;
  6384. max_param = pc_maxparameter(sd, (enum e_params)(type-SP_STR));
  6385. while (final_val <= max_param && status_points >= 0) {
  6386. status_points -= PC_STATUS_POINT_COST(final_val);
  6387. final_val++;
  6388. }
  6389. final_val--;
  6390. return (final_val > base ? final_val-base : 0);
  6391. }
  6392. /**
  6393. * Raises a stat by the specified amount.
  6394. *
  6395. * Obeys max_parameter limits.
  6396. * Subtracts status points according to the cost of the increased stat points.
  6397. *
  6398. * @param sd The target character.
  6399. * @param type The stat to change (see enum _sp)
  6400. * @param increase The stat increase (strictly positive) amount.
  6401. * @retval true if the stat was increased by any amount.
  6402. * @retval false if there were no changes.
  6403. */
  6404. bool pc_statusup(struct map_session_data* sd, int type, int increase)
  6405. {
  6406. int max_increase = 0, current = 0, needed_points = 0, final_value = 0;
  6407. nullpo_ret(sd);
  6408. // check conditions
  6409. if (type < SP_STR || type > SP_LUK || increase <= 0) {
  6410. clif_statusupack(sd, type, 0, 0);
  6411. return false;
  6412. }
  6413. // check limits
  6414. current = pc_getstat(sd, type);
  6415. max_increase = pc_maxparameterincrease(sd, type);
  6416. increase = cap_value(increase, 0, max_increase); // cap to the maximum status points available
  6417. if (increase <= 0 || current + increase > pc_maxparameter(sd, (enum e_params)(type-SP_STR))) {
  6418. clif_statusupack(sd, type, 0, 0);
  6419. return false;
  6420. }
  6421. // check status points
  6422. needed_points = pc_need_status_point(sd, type, increase);
  6423. if (needed_points < 0 || needed_points > sd->status.status_point) { // Sanity check
  6424. clif_statusupack(sd, type, 0, 0);
  6425. return false;
  6426. }
  6427. // set new values
  6428. final_value = pc_setstat(sd, type, current + increase);
  6429. sd->status.status_point -= needed_points;
  6430. status_calc_pc(sd,SCO_NONE);
  6431. // update increase cost indicator
  6432. clif_updatestatus(sd, SP_USTR + type-SP_STR);
  6433. // update statpoint count
  6434. clif_updatestatus(sd, SP_STATUSPOINT);
  6435. // update stat value
  6436. clif_statusupack(sd, type, 1, final_value); // required
  6437. if( final_value > 255 )
  6438. clif_updatestatus(sd, type); // send after the 'ack' to override the truncated value
  6439. achievement_update_objective(sd, AG_GOAL_STATUS, 1, final_value);
  6440. return true;
  6441. }
  6442. /**
  6443. * Raises a stat by the specified amount.
  6444. *
  6445. * Obeys max_parameter limits.
  6446. * Does not subtract status points for the cost of the modified stat points.
  6447. *
  6448. * @param sd The target character.
  6449. * @param type The stat to change (see enum _sp)
  6450. * @param val The stat increase (or decrease) amount.
  6451. * @return the stat increase amount.
  6452. * @retval 0 if no changes were made.
  6453. */
  6454. int pc_statusup2(struct map_session_data* sd, int type, int val)
  6455. {
  6456. int max, need;
  6457. nullpo_ret(sd);
  6458. if( type < SP_STR || type > SP_LUK )
  6459. {
  6460. clif_statusupack(sd,type,0,0);
  6461. return 0;
  6462. }
  6463. need = pc_need_status_point(sd,type,1);
  6464. max = pc_maxparameter(sd,(enum e_params)(type-SP_STR)); // set new value
  6465. val = pc_setstat(sd, type, cap_value(pc_getstat(sd,type) + val, 1, max));
  6466. status_calc_pc(sd,SCO_NONE);
  6467. // update increase cost indicator
  6468. if( need != pc_need_status_point(sd,type,1) )
  6469. clif_updatestatus(sd, SP_USTR + type-SP_STR);
  6470. // update stat value
  6471. clif_statusupack(sd,type,1,val); // required
  6472. if( val > 255 )
  6473. clif_updatestatus(sd,type); // send after the 'ack' to override the truncated value
  6474. return val;
  6475. }
  6476. /*==========================================
  6477. * Update skill_lv for player sd
  6478. * Skill point allocation
  6479. *------------------------------------------*/
  6480. void pc_skillup(struct map_session_data *sd,uint16 skill_id)
  6481. {
  6482. uint16 idx = skill_get_index(skill_id);
  6483. nullpo_retv(sd);
  6484. if (!idx) {
  6485. if (skill_id)
  6486. ShowError("pc_skillup: Player attempts to level up invalid skill '%d'\n", skill_id);
  6487. return;
  6488. }
  6489. // Level up guild skill
  6490. if (SKILL_CHK_GUILD(skill_id)) {
  6491. guild_skillup(sd, skill_id);
  6492. return;
  6493. }
  6494. // Level up homunculus skill
  6495. else if (sd->hd && SKILL_CHK_HOMUN(skill_id)) {
  6496. hom_skillup(sd->hd, skill_id);
  6497. return;
  6498. }
  6499. else {
  6500. if( sd->status.skill_point > 0 &&
  6501. sd->status.skill[idx].id &&
  6502. sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT && //Don't allow raising while you have granted skills. [Skotlex]
  6503. sd->status.skill[idx].lv < skill_tree_get_max(skill_id, sd->status.class_) )
  6504. {
  6505. int lv, range, upgradable;
  6506. sd->status.skill[idx].lv++;
  6507. sd->status.skill_point--;
  6508. if( !skill_get_inf(skill_id) )
  6509. status_calc_pc(sd,SCO_NONE); // Only recalculate for passive skills.
  6510. else if( sd->status.skill_point == 0 && pc_is_taekwon_ranker(sd) )
  6511. pc_calc_skilltree(sd); // Required to grant all TK Ranker skills.
  6512. else
  6513. pc_check_skilltree(sd); // Check if a new skill can Lvlup
  6514. lv = sd->status.skill[idx].lv;
  6515. range = skill_get_range2(&sd->bl, skill_id, lv, false);
  6516. upgradable = (lv < skill_tree_get_max(sd->status.skill[idx].id, sd->status.class_)) ? 1 : 0;
  6517. clif_skillup(sd,skill_id,lv,range,upgradable);
  6518. clif_updatestatus(sd,SP_SKILLPOINT);
  6519. if( skill_id == GN_REMODELING_CART ) /* cart weight info was updated by status_calc_pc */
  6520. clif_updatestatus(sd,SP_CARTINFO);
  6521. if (!pc_has_permission(sd, PC_PERM_ALL_SKILL)) // may skill everything at any time anyways, and this would cause a huge slowdown
  6522. clif_skillinfoblock(sd);
  6523. }
  6524. //else
  6525. // 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);
  6526. }
  6527. }
  6528. /*==========================================
  6529. * /allskill
  6530. *------------------------------------------*/
  6531. int pc_allskillup(struct map_session_data *sd)
  6532. {
  6533. int i;
  6534. nullpo_ret(sd);
  6535. for (i = 0; i < MAX_SKILL; i++) {
  6536. 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) {
  6537. sd->status.skill[i].lv = (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY) ? 0 : sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
  6538. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  6539. if (sd->status.skill[i].lv == 0)
  6540. sd->status.skill[i].id = 0;
  6541. }
  6542. }
  6543. if (!pc_grant_allskills(sd, true)) {
  6544. uint16 sk_id;
  6545. for (i = 0; i < MAX_SKILL_TREE && (sk_id = skill_tree[pc_class2idx(sd->status.class_)][i].skill_id) > 0;i++){
  6546. uint16 sk_idx = skill_get_index(sk_id);
  6547. if (sk_id == 0 || sk_idx == 0)
  6548. continue;
  6549. std::shared_ptr<s_skill_db> skill = skill_db.find(sk_id);
  6550. if (
  6551. (skill->inf2[INF2_ISQUEST] && !battle_config.quest_skill_learn) ||
  6552. ((skill->inf2[INF2_ISWEDDING] || skill->inf2[INF2_ISSPIRIT])) ||
  6553. sk_id == SG_DEVIL
  6554. )
  6555. continue; //Cannot be learned normally.
  6556. sd->status.skill[sk_idx].id = sk_id;
  6557. sd->status.skill[sk_idx].lv = skill_tree_get_max(sk_id, sd->status.class_); // celest
  6558. }
  6559. }
  6560. status_calc_pc(sd,SCO_NONE);
  6561. //Required because if you could level up all skills previously,
  6562. //the update will not be sent as only the lv variable changes.
  6563. clif_skillinfoblock(sd);
  6564. return 0;
  6565. }
  6566. /*==========================================
  6567. * /resetlvl
  6568. *------------------------------------------*/
  6569. int pc_resetlvl(struct map_session_data* sd,int type)
  6570. {
  6571. int i;
  6572. nullpo_ret(sd);
  6573. if (type != 3) //Also reset skills
  6574. pc_resetskill(sd, 0);
  6575. if(type == 1){
  6576. sd->status.skill_point=0;
  6577. sd->status.base_level=1;
  6578. sd->status.job_level=1;
  6579. sd->status.base_exp=0;
  6580. sd->status.job_exp=0;
  6581. if(sd->sc.option !=0)
  6582. sd->sc.option = 0;
  6583. sd->status.str=1;
  6584. sd->status.agi=1;
  6585. sd->status.vit=1;
  6586. sd->status.int_=1;
  6587. sd->status.dex=1;
  6588. sd->status.luk=1;
  6589. if(sd->status.class_ == JOB_NOVICE_HIGH) {
  6590. sd->status.status_point=100; // not 88 [celest]
  6591. // give platinum skills upon changing
  6592. pc_skill(sd,NV_FIRSTAID,1,ADDSKILL_PERMANENT);
  6593. pc_skill(sd,NV_TRICKDEAD,1,ADDSKILL_PERMANENT);
  6594. }
  6595. }
  6596. if(type == 2){
  6597. sd->status.skill_point=0;
  6598. sd->status.base_level=1;
  6599. sd->status.job_level=1;
  6600. sd->status.base_exp=0;
  6601. sd->status.job_exp=0;
  6602. }
  6603. if(type == 3){
  6604. sd->status.base_level=1;
  6605. sd->status.base_exp=0;
  6606. }
  6607. if(type == 4){
  6608. sd->status.job_level=1;
  6609. sd->status.job_exp=0;
  6610. }
  6611. clif_updatestatus(sd,SP_STATUSPOINT);
  6612. clif_updatestatus(sd,SP_STR);
  6613. clif_updatestatus(sd,SP_AGI);
  6614. clif_updatestatus(sd,SP_VIT);
  6615. clif_updatestatus(sd,SP_INT);
  6616. clif_updatestatus(sd,SP_DEX);
  6617. clif_updatestatus(sd,SP_LUK);
  6618. clif_updatestatus(sd,SP_BASELEVEL);
  6619. clif_updatestatus(sd,SP_JOBLEVEL);
  6620. clif_updatestatus(sd,SP_STATUSPOINT);
  6621. clif_updatestatus(sd,SP_BASEEXP);
  6622. clif_updatestatus(sd,SP_JOBEXP);
  6623. clif_updatestatus(sd,SP_NEXTBASEEXP);
  6624. clif_updatestatus(sd,SP_NEXTJOBEXP);
  6625. clif_updatestatus(sd,SP_SKILLPOINT);
  6626. clif_updatestatus(sd,SP_USTR); // Updates needed stat points - Valaris
  6627. clif_updatestatus(sd,SP_UAGI);
  6628. clif_updatestatus(sd,SP_UVIT);
  6629. clif_updatestatus(sd,SP_UINT);
  6630. clif_updatestatus(sd,SP_UDEX);
  6631. clif_updatestatus(sd,SP_ULUK); // End Addition
  6632. for(i=0;i<EQI_MAX;i++) { // unequip items that can't be equipped by base 1 [Valaris]
  6633. if(sd->equip_index[i] >= 0)
  6634. if(pc_isequip(sd,sd->equip_index[i]))
  6635. pc_unequipitem(sd,sd->equip_index[i],2);
  6636. }
  6637. if ((type == 1 || type == 2 || type == 3) && sd->status.party_id)
  6638. party_send_levelup(sd);
  6639. status_calc_pc(sd, SCO_FORCE);
  6640. clif_skillinfoblock(sd);
  6641. return 0;
  6642. }
  6643. /*==========================================
  6644. * /resetstate
  6645. *------------------------------------------*/
  6646. int pc_resetstate(struct map_session_data* sd)
  6647. {
  6648. nullpo_ret(sd);
  6649. if (battle_config.use_statpoint_table)
  6650. { // New statpoint table used here - Dexity
  6651. if (sd->status.base_level > MAX_LEVEL)
  6652. { //statp[] goes out of bounds, can't reset!
  6653. ShowError("pc_resetstate: Can't reset stats of %d:%d, the base level (%d) is greater than the max level supported (%d)\n",
  6654. sd->status.account_id, sd->status.char_id, sd->status.base_level, MAX_LEVEL);
  6655. return 0;
  6656. }
  6657. sd->status.status_point = statp[sd->status.base_level] + ( sd->class_&JOBL_UPPER ? 52 : 0 ); // extra 52+48=100 stat points
  6658. }
  6659. else
  6660. {
  6661. int add=0;
  6662. add += pc_need_status_point(sd, SP_STR, 1-pc_getstat(sd, SP_STR));
  6663. add += pc_need_status_point(sd, SP_AGI, 1-pc_getstat(sd, SP_AGI));
  6664. add += pc_need_status_point(sd, SP_VIT, 1-pc_getstat(sd, SP_VIT));
  6665. add += pc_need_status_point(sd, SP_INT, 1-pc_getstat(sd, SP_INT));
  6666. add += pc_need_status_point(sd, SP_DEX, 1-pc_getstat(sd, SP_DEX));
  6667. add += pc_need_status_point(sd, SP_LUK, 1-pc_getstat(sd, SP_LUK));
  6668. sd->status.status_point+=add;
  6669. }
  6670. pc_setstat(sd, SP_STR, 1);
  6671. pc_setstat(sd, SP_AGI, 1);
  6672. pc_setstat(sd, SP_VIT, 1);
  6673. pc_setstat(sd, SP_INT, 1);
  6674. pc_setstat(sd, SP_DEX, 1);
  6675. pc_setstat(sd, SP_LUK, 1);
  6676. clif_updatestatus(sd,SP_STR);
  6677. clif_updatestatus(sd,SP_AGI);
  6678. clif_updatestatus(sd,SP_VIT);
  6679. clif_updatestatus(sd,SP_INT);
  6680. clif_updatestatus(sd,SP_DEX);
  6681. clif_updatestatus(sd,SP_LUK);
  6682. clif_updatestatus(sd,SP_USTR); // Updates needed stat points - Valaris
  6683. clif_updatestatus(sd,SP_UAGI);
  6684. clif_updatestatus(sd,SP_UVIT);
  6685. clif_updatestatus(sd,SP_UINT);
  6686. clif_updatestatus(sd,SP_UDEX);
  6687. clif_updatestatus(sd,SP_ULUK); // End Addition
  6688. clif_updatestatus(sd,SP_STATUSPOINT);
  6689. if( sd->mission_mobid ) { //bugreport:2200
  6690. sd->mission_mobid = 0;
  6691. sd->mission_count = 0;
  6692. pc_setglobalreg(sd, add_str(TKMISSIONID_VAR), 0);
  6693. }
  6694. status_calc_pc(sd, SCO_NONE);
  6695. return 1;
  6696. }
  6697. /*==========================================
  6698. * /resetskill
  6699. * if flag&1, perform block resync and status_calc call.
  6700. * if flag&2, just count total amount of skill points used by player, do not really reset.
  6701. * if flag&4, just reset the skills if the player class is a bard/dancer type (for changesex.)
  6702. *------------------------------------------*/
  6703. int pc_resetskill(struct map_session_data* sd, int flag)
  6704. {
  6705. int i, skill_point=0;
  6706. nullpo_ret(sd);
  6707. if( flag&4 && (sd->class_&MAPID_UPPERMASK) != MAPID_BARDDANCER )
  6708. return 0;
  6709. if( !(flag&2) ) { //Remove stuff lost when resetting skills.
  6710. /**
  6711. * 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)
  6712. **/
  6713. if( pc_is_taekwon_ranker(sd) )
  6714. return 0;
  6715. if( pc_checkskill(sd, SG_DEVIL) && pc_is_maxjoblv(sd) )
  6716. clif_status_load(&sd->bl, EFST_DEVIL1, 0); //Remove perma blindness due to skill-reset. [Skotlex]
  6717. i = sd->sc.option;
  6718. if( i&OPTION_RIDING && pc_checkskill(sd, KN_RIDING) )
  6719. i &= ~OPTION_RIDING;
  6720. if( i&OPTION_FALCON && pc_checkskill(sd, HT_FALCON) )
  6721. i &= ~OPTION_FALCON;
  6722. if( i&OPTION_DRAGON && pc_checkskill(sd, RK_DRAGONTRAINING) )
  6723. i &= ~OPTION_DRAGON;
  6724. if( i&OPTION_WUG && pc_checkskill(sd, RA_WUGMASTERY) )
  6725. i &= ~OPTION_WUG;
  6726. if( i&OPTION_WUGRIDER && pc_checkskill(sd, RA_WUGRIDER) )
  6727. i &= ~OPTION_WUGRIDER;
  6728. if( i&OPTION_MADOGEAR && ( sd->class_&MAPID_THIRDMASK ) == MAPID_MECHANIC )
  6729. i &= ~OPTION_MADOGEAR;
  6730. #ifndef NEW_CARTS
  6731. if( i&OPTION_CART && pc_checkskill(sd, MC_PUSHCART) )
  6732. i &= ~OPTION_CART;
  6733. #else
  6734. if( sd->sc.data[SC_PUSH_CART] )
  6735. pc_setcart(sd, 0);
  6736. #endif
  6737. if( i != sd->sc.option )
  6738. pc_setoption(sd, i);
  6739. if( hom_is_active(sd->hd) && pc_checkskill(sd, AM_CALLHOMUN) )
  6740. hom_vaporize(sd, HOM_ST_ACTIVE);
  6741. if (sd->sc.data[SC_SPRITEMABLE] && pc_checkskill(sd, SU_SPRITEMABLE))
  6742. status_change_end(&sd->bl, SC_SPRITEMABLE, INVALID_TIMER);
  6743. }
  6744. for (const auto &skill : skill_db) {
  6745. uint16 skill_id = skill.second->nameid, idx = skill_get_index(skill_id);
  6746. uint8 lv = sd->status.skill[idx].lv;
  6747. if (lv == 0 || skill_id == 0)
  6748. continue;
  6749. if( skill.second->inf2[INF2_ISWEDDING] || skill.second->inf2[INF2_ISSPIRIT] ) //Avoid reseting wedding/linker skills.
  6750. continue;
  6751. // Don't reset trick dead if not a novice/baby
  6752. if( skill_id == NV_TRICKDEAD && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE )
  6753. {
  6754. sd->status.skill[idx].lv = 0;
  6755. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  6756. continue;
  6757. }
  6758. // do not reset basic skill
  6759. if (skill_id == NV_BASIC && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE )
  6760. continue;
  6761. if( sd->status.skill[idx].flag == SKILL_FLAG_PERM_GRANTED )
  6762. continue;
  6763. if( flag&4 && !skill_ischangesex(skill_id) )
  6764. continue;
  6765. if( skill.second->inf2[INF2_ISQUEST] && !battle_config.quest_skill_learn )
  6766. { //Only handle quest skills in a special way when you can't learn them manually
  6767. if( battle_config.quest_skill_reset && !(flag&2) )
  6768. { //Wipe them
  6769. sd->status.skill[idx].lv = 0;
  6770. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  6771. }
  6772. continue;
  6773. }
  6774. if( sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT )
  6775. skill_point += lv;
  6776. else
  6777. if( sd->status.skill[idx].flag >= SKILL_FLAG_REPLACED_LV_0 )
  6778. skill_point += (sd->status.skill[idx].flag - SKILL_FLAG_REPLACED_LV_0);
  6779. if( !(flag&2) )
  6780. {// reset
  6781. sd->status.skill[idx].lv = 0;
  6782. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  6783. }
  6784. }
  6785. if( flag&2 || !skill_point ) return skill_point;
  6786. sd->status.skill_point += skill_point;
  6787. if (flag&1) {
  6788. clif_updatestatus(sd,SP_SKILLPOINT);
  6789. clif_skillinfoblock(sd);
  6790. status_calc_pc(sd, SCO_FORCE);
  6791. }
  6792. return skill_point;
  6793. }
  6794. /*==========================================
  6795. * /resetfeel [Komurka]
  6796. *------------------------------------------*/
  6797. int pc_resetfeel(struct map_session_data* sd)
  6798. {
  6799. int i;
  6800. nullpo_ret(sd);
  6801. for (i=0; i<MAX_PC_FEELHATE; i++)
  6802. {
  6803. sd->feel_map[i].m = -1;
  6804. sd->feel_map[i].index = 0;
  6805. pc_setglobalreg(sd, add_str(sg_info[i].feel_var), 0);
  6806. }
  6807. return 0;
  6808. }
  6809. int pc_resethate(struct map_session_data* sd)
  6810. {
  6811. int i;
  6812. nullpo_ret(sd);
  6813. for (i=0; i<3; i++)
  6814. {
  6815. sd->hate_mob[i] = -1;
  6816. pc_setglobalreg(sd, add_str(sg_info[i].hate_var), 0);
  6817. }
  6818. return 0;
  6819. }
  6820. int pc_skillatk_bonus(struct map_session_data *sd, uint16 skill_id)
  6821. {
  6822. int bonus = 0;
  6823. nullpo_ret(sd);
  6824. skill_id = skill_dummy2skill_id(skill_id);
  6825. for (auto &it : sd->skillatk) {
  6826. if (it.id == skill_id) {
  6827. bonus += it.val;
  6828. break;
  6829. }
  6830. }
  6831. return bonus;
  6832. }
  6833. int pc_sub_skillatk_bonus(struct map_session_data *sd, uint16 skill_id)
  6834. {
  6835. int bonus = 0;
  6836. nullpo_ret(sd);
  6837. skill_id = skill_dummy2skill_id(skill_id);
  6838. for (auto &it : sd->subskill) {
  6839. if (it.id == skill_id) {
  6840. bonus += it.val;
  6841. break;
  6842. }
  6843. }
  6844. return bonus;
  6845. }
  6846. int pc_skillheal_bonus(struct map_session_data *sd, uint16 skill_id) {
  6847. int bonus = sd->bonus.add_heal_rate;
  6848. nullpo_ret(sd);
  6849. skill_id = skill_dummy2skill_id(skill_id);
  6850. if( bonus ) {
  6851. switch( skill_id ) {
  6852. case AL_HEAL: if( !(battle_config.skill_add_heal_rate&1) ) bonus = 0; break;
  6853. case PR_SANCTUARY: if( !(battle_config.skill_add_heal_rate&2) ) bonus = 0; break;
  6854. case AM_POTIONPITCHER: if( !(battle_config.skill_add_heal_rate&4) ) bonus = 0; break;
  6855. case CR_SLIMPITCHER: if( !(battle_config.skill_add_heal_rate&8) ) bonus = 0; break;
  6856. case BA_APPLEIDUN: if( !(battle_config.skill_add_heal_rate&16)) bonus = 0; break;
  6857. }
  6858. }
  6859. for (auto &it : sd->skillheal) {
  6860. if (it.id == skill_id) {
  6861. bonus += it.val;
  6862. break;
  6863. }
  6864. }
  6865. return bonus;
  6866. }
  6867. int pc_skillheal2_bonus(struct map_session_data *sd, uint16 skill_id) {
  6868. int bonus = sd->bonus.add_heal2_rate;
  6869. skill_id = skill_dummy2skill_id(skill_id);
  6870. for (auto &it : sd->skillheal2) {
  6871. if (it.id == skill_id) {
  6872. bonus += it.val;
  6873. break;
  6874. }
  6875. }
  6876. return bonus;
  6877. }
  6878. void pc_respawn(struct map_session_data* sd, clr_type clrtype)
  6879. {
  6880. if( !pc_isdead(sd) )
  6881. return; // not applicable
  6882. if( sd->bg_id && bg_member_respawn(sd) )
  6883. return; // member revived by battleground
  6884. pc_setstand(sd, true);
  6885. pc_setrestartvalue(sd,3);
  6886. if( pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, clrtype) != SETPOS_OK )
  6887. clif_resurrection(&sd->bl, 1); //If warping fails, send a normal stand up packet.
  6888. }
  6889. static TIMER_FUNC(pc_respawn_timer){
  6890. struct map_session_data *sd = map_id2sd(id);
  6891. if( sd != NULL )
  6892. {
  6893. sd->pvp_point=0;
  6894. sd->respawn_tid = INVALID_TIMER;
  6895. pc_respawn(sd,CLR_OUTSIGHT);
  6896. }
  6897. return 0;
  6898. }
  6899. /*==========================================
  6900. * Invoked when a player has received damage
  6901. *------------------------------------------*/
  6902. void pc_damage(struct map_session_data *sd,struct block_list *src,unsigned int hp, unsigned int sp)
  6903. {
  6904. if (sp) clif_updatestatus(sd,SP_SP);
  6905. if (hp) clif_updatestatus(sd,SP_HP);
  6906. else return;
  6907. if (!src)
  6908. return;
  6909. if( pc_issit(sd) ) {
  6910. pc_setstand(sd, true);
  6911. skill_sit(sd,0);
  6912. }
  6913. if (sd->progressbar.npc_id)
  6914. clif_progressbar_abort(sd);
  6915. if( sd->status.pet_id > 0 && sd->pd && battle_config.pet_damage_support )
  6916. pet_target_check(sd->pd,src,1);
  6917. if( sd->status.ele_id > 0 )
  6918. elemental_set_target(sd,src);
  6919. if(battle_config.prevent_logout_trigger&PLT_DAMAGE)
  6920. sd->canlog_tick = gettick();
  6921. }
  6922. TIMER_FUNC(pc_close_npc_timer){
  6923. TBL_PC *sd = map_id2sd(id);
  6924. if(sd) pc_close_npc(sd,data);
  6925. return 0;
  6926. }
  6927. /**
  6928. * Method to properly close a NPC for player and clear anything related.
  6929. * @param sd: Player attached
  6930. * @param flag: Method of closure
  6931. * 1: Produce a close button and end the NPC
  6932. * 2: End the NPC (best for no dialog windows)
  6933. */
  6934. void pc_close_npc(struct map_session_data *sd,int flag)
  6935. {
  6936. nullpo_retv(sd);
  6937. if (sd->npc_id || sd->npc_shopid) {
  6938. if (sd->state.using_fake_npc) {
  6939. clif_clearunit_single(sd->npc_id, CLR_OUTSIGHT, sd->fd);
  6940. sd->state.using_fake_npc = 0;
  6941. }
  6942. if (sd->st) {
  6943. if(sd->st->state == RUN){ //wait ending code execution
  6944. add_timer(gettick()+500,pc_close_npc_timer,sd->bl.id,flag);
  6945. return;
  6946. }
  6947. sd->st->state = ((flag==1 && sd->st->mes_active)?CLOSE:END);
  6948. sd->st->mes_active = 0;
  6949. }
  6950. sd->state.menu_or_input = 0;
  6951. sd->npc_menu = 0;
  6952. sd->npc_shopid = 0;
  6953. #ifdef SECURE_NPCTIMEOUT
  6954. sd->npc_idle_timer = INVALID_TIMER;
  6955. #endif
  6956. if (sd->st) {
  6957. if (sd->st->state == CLOSE) {
  6958. clif_scriptclose(sd, sd->npc_id);
  6959. clif_scriptclear(sd, sd->npc_id); // [Ind/Hercules]
  6960. sd->st->state = END; // Force to end now
  6961. }
  6962. if (sd->st->state == END) { // free attached scripts that are waiting
  6963. script_free_state(sd->st);
  6964. sd->st = NULL;
  6965. sd->npc_id = 0;
  6966. }
  6967. }
  6968. }
  6969. }
  6970. /*==========================================
  6971. * Invoked when a player has negative current hp
  6972. *------------------------------------------*/
  6973. int pc_dead(struct map_session_data *sd,struct block_list *src)
  6974. {
  6975. int i=0,k=0;
  6976. t_tick tick = gettick();
  6977. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  6978. // Activate Steel body if a super novice dies at 99+% exp [celest]
  6979. // Super Novices have no kill or die functions attached when saved by their angel
  6980. if (!sd->state.snovice_dead_flag && (sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE) {
  6981. unsigned int exp = pc_nextbaseexp(sd);
  6982. if( exp && get_percentage(sd->status.base_exp,exp) >= 99 ) {
  6983. sd->state.snovice_dead_flag = 1;
  6984. pc_setrestartvalue(sd,1);
  6985. status_percent_heal(&sd->bl, 100, 100);
  6986. clif_resurrection(&sd->bl, 1);
  6987. if(battle_config.pc_invincible_time)
  6988. pc_setinvincibletimer(sd, battle_config.pc_invincible_time);
  6989. sc_start(&sd->bl,&sd->bl,status_skill2sc(MO_STEELBODY),100,5,skill_get_time(MO_STEELBODY,5));
  6990. if(mapdata_flag_gvg2(mapdata))
  6991. pc_respawn_timer(INVALID_TIMER, gettick(), sd->bl.id, 0);
  6992. return 0;
  6993. }
  6994. }
  6995. for(k = 0; k < MAX_DEVOTION; k++) {
  6996. if (sd->devotion[k]){
  6997. struct map_session_data *devsd = map_id2sd(sd->devotion[k]);
  6998. if (devsd)
  6999. status_change_end(&devsd->bl, SC_DEVOTION, INVALID_TIMER);
  7000. sd->devotion[k] = 0;
  7001. }
  7002. }
  7003. if(sd->shadowform_id) { //if we were target of shadowform
  7004. status_change_end(map_id2bl(sd->shadowform_id), SC__SHADOWFORM, INVALID_TIMER);
  7005. sd->shadowform_id = 0; //should be remove on status end anyway
  7006. }
  7007. if(sd->status.pet_id > 0 && sd->pd) {
  7008. struct pet_data *pd = sd->pd;
  7009. if( !mapdata->flag[MF_NOEXPPENALTY] ) {
  7010. pet_set_intimate(pd, pd->pet.intimate + pd->get_pet_db()->die);
  7011. if( pd->pet.intimate <= PET_INTIMATE_NONE )
  7012. pet_set_intimate(pd, PET_INTIMATE_NONE);
  7013. clif_send_petdata(sd,sd->pd,1,pd->pet.intimate);
  7014. }
  7015. if( sd->pd->target_id ) // Unlock all targets...
  7016. pet_unlocktarget(sd->pd);
  7017. }
  7018. 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)
  7019. hom_vaporize(sd, HOM_ST_ACTIVE);
  7020. if( sd->md )
  7021. mercenary_delete(sd->md, 3); // Your mercenary soldier has ran away.
  7022. if( sd->ed )
  7023. elemental_delete(sd->ed);
  7024. // Leave duel if you die [LuzZza]
  7025. if(battle_config.duel_autoleave_when_die) {
  7026. if(sd->duel_group > 0)
  7027. duel_leave(sd->duel_group, sd);
  7028. if(sd->duel_invite > 0)
  7029. duel_reject(sd->duel_invite, sd);
  7030. }
  7031. pc_close_npc(sd,2); //close npc if we were using one
  7032. /* e.g. not killed thru pc_damage */
  7033. if( pc_issit(sd) ) {
  7034. clif_status_load(&sd->bl,EFST_SIT,0);
  7035. }
  7036. pc_setdead(sd);
  7037. clif_party_dead( sd );
  7038. pc_setglobalreg(sd, add_str(PCDIECOUNTER_VAR), sd->die_counter+1);
  7039. pc_setparam(sd, SP_KILLERRID, src?src->id:0);
  7040. //Reset menu skills/item skills
  7041. if ((sd->skillitem) != 0)
  7042. sd->skillitem = sd->skillitemlv = 0;
  7043. if ((sd->menuskill_id) != 0)
  7044. sd->menuskill_id = sd->menuskill_val = 0;
  7045. //Reset ticks.
  7046. sd->hp_loss.tick = sd->sp_loss.tick = sd->hp_regen.tick = sd->sp_regen.tick = 0;
  7047. if ( sd->spiritball !=0 )
  7048. pc_delspiritball(sd,sd->spiritball,0);
  7049. if (sd->spiritcharm_type != CHARM_TYPE_NONE && sd->spiritcharm > 0)
  7050. pc_delspiritcharm(sd,sd->spiritcharm,sd->spiritcharm_type);
  7051. if (src)
  7052. switch (src->type) {
  7053. case BL_MOB:
  7054. {
  7055. struct mob_data *md=(struct mob_data *)src;
  7056. if(md->target_id==sd->bl.id)
  7057. mob_unlocktarget(md,tick);
  7058. if(battle_config.mobs_level_up && md->status.hp &&
  7059. (unsigned int)md->level < pc_maxbaselv(sd) &&
  7060. !md->guardian_data && !md->special_state.ai// Guardians/summons should not level. [Skotlex]
  7061. ) { // monster level up [Valaris]
  7062. clif_misceffect(&md->bl,0);
  7063. md->level++;
  7064. status_calc_mob(md, SCO_NONE);
  7065. status_percent_heal(src,10,0);
  7066. if( battle_config.show_mob_info&4 )
  7067. {// update name with new level
  7068. clif_name_area(&md->bl);
  7069. }
  7070. }
  7071. src = battle_get_master(src); // Maybe Player Summon
  7072. }
  7073. break;
  7074. case BL_PET: //Pass on to master...
  7075. case BL_HOM:
  7076. case BL_MER:
  7077. src = battle_get_master(src);
  7078. break;
  7079. }
  7080. if (src && src->type == BL_PC) {
  7081. struct map_session_data *ssd = (struct map_session_data *)src;
  7082. pc_setparam(ssd, SP_KILLEDRID, sd->bl.id);
  7083. npc_script_event(ssd, NPCE_KILLPC);
  7084. if (battle_config.pk_mode&2) {
  7085. ssd->status.manner -= 5;
  7086. if(ssd->status.manner < 0)
  7087. sc_start(&sd->bl,src,SC_NOCHAT,100,0,0);
  7088. #if 0
  7089. // PK/Karma system code (not enabled yet) [celest]
  7090. // originally from Kade Online, so i don't know if any of these is correct ^^;
  7091. // note: karma is measured REVERSE, so more karma = more 'evil' / less honourable,
  7092. // karma going down = more 'good' / more honourable.
  7093. // The Karma System way...
  7094. if (sd->status.karma > ssd->status.karma) { // If player killed was more evil
  7095. sd->status.karma--;
  7096. ssd->status.karma--;
  7097. }
  7098. else if (sd->status.karma < ssd->status.karma) // If player killed was more good
  7099. ssd->status.karma++;
  7100. // or the PK System way...
  7101. if (sd->status.karma > 0) // player killed is dishonourable?
  7102. ssd->status.karma--; // honour points earned
  7103. sd->status.karma++; // honour points lost
  7104. // To-do: Receive exp on certain occasions
  7105. #endif
  7106. }
  7107. }
  7108. if(battle_config.bone_drop==2
  7109. || (battle_config.bone_drop==1 && mapdata->flag[MF_PVP]))
  7110. {
  7111. struct item item_tmp;
  7112. memset(&item_tmp,0,sizeof(item_tmp));
  7113. item_tmp.nameid=ITEMID_SKULL_;
  7114. item_tmp.identify=1;
  7115. item_tmp.card[0]=CARD0_CREATE;
  7116. item_tmp.card[1]=0;
  7117. item_tmp.card[2]=GetWord(sd->status.char_id,0); // CharId
  7118. item_tmp.card[3]=GetWord(sd->status.char_id,1);
  7119. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  7120. }
  7121. //Remove bonus_script when dead
  7122. pc_bonus_script_clear(sd,BSF_REM_ON_DEAD);
  7123. // changed penalty options, added death by player if pk_mode [Valaris]
  7124. if(battle_config.death_penalty_type
  7125. && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE // only novices will receive no penalty
  7126. && !sd->sc.data[SC_BABY] && !sd->sc.data[SC_LIFEINSURANCE]
  7127. && !mapdata->flag[MF_NOEXPPENALTY] && !mapdata_flag_gvg2(mapdata))
  7128. {
  7129. uint32 base_penalty = 0;
  7130. uint32 job_penalty = 0;
  7131. uint32 zeny_penalty = 0;
  7132. if (pc_isvip(sd)) { // EXP penalty for VIP
  7133. base_penalty = battle_config.vip_exp_penalty_base;
  7134. job_penalty = battle_config.vip_exp_penalty_job;
  7135. zeny_penalty = battle_config.vip_zeny_penalty;
  7136. } else {
  7137. base_penalty = battle_config.death_penalty_base;
  7138. job_penalty = battle_config.death_penalty_job;
  7139. zeny_penalty = battle_config.zeny_penalty;
  7140. }
  7141. if ((battle_config.death_penalty_maxlv&1 || !pc_is_maxbaselv(sd)) && base_penalty > 0) {
  7142. switch (battle_config.death_penalty_type) {
  7143. case 1: base_penalty = (uint32) ( pc_nextbaseexp(sd) * ( base_penalty / 10000. ) ); break;
  7144. case 2: base_penalty = (uint32) ( sd->status.base_exp * ( base_penalty / 10000. ) ); break;
  7145. }
  7146. if (base_penalty){ //recheck after altering to speedup
  7147. if (battle_config.pk_mode && src && src->type==BL_PC)
  7148. base_penalty *= 2;
  7149. base_penalty = u32min(sd->status.base_exp, base_penalty);
  7150. }
  7151. }
  7152. else
  7153. base_penalty = 0;
  7154. if ((battle_config.death_penalty_maxlv&2 || !pc_is_maxjoblv(sd)) && job_penalty > 0) {
  7155. switch (battle_config.death_penalty_type) {
  7156. case 1: job_penalty = (uint32) ( pc_nextjobexp(sd) * ( job_penalty / 10000. ) ); break;
  7157. case 2: job_penalty = (uint32) ( sd->status.job_exp * ( job_penalty /10000. ) ); break;
  7158. }
  7159. if (job_penalty) {
  7160. if (battle_config.pk_mode && src && src->type==BL_PC)
  7161. job_penalty *= 2;
  7162. job_penalty = u32min(sd->status.job_exp, job_penalty);
  7163. }
  7164. }
  7165. else
  7166. job_penalty = 0;
  7167. if (base_penalty || job_penalty)
  7168. pc_lostexp(sd, base_penalty, job_penalty);
  7169. if( zeny_penalty > 0 && !mapdata->flag[MF_NOZENYPENALTY]) {
  7170. zeny_penalty = (uint32)( sd->status.zeny * ( zeny_penalty / 10000. ) );
  7171. if(zeny_penalty)
  7172. pc_payzeny(sd, zeny_penalty, LOG_TYPE_PICKDROP_PLAYER, NULL);
  7173. }
  7174. }
  7175. if( mapdata->flag[MF_PVP_NIGHTMAREDROP] ) { // Moved this outside so it works when PVP isn't enabled and during pk mode [Ancyker]
  7176. for (const auto &it : mapdata->drop_list) {
  7177. int id = it.drop_id, per = it.drop_per;
  7178. enum e_nightmare_drop_type type = it.drop_type;
  7179. if(id == 0)
  7180. continue;
  7181. if(id == -1){
  7182. int eq_num=0,eq_n[MAX_INVENTORY];
  7183. memset(eq_n,0,sizeof(eq_n));
  7184. for(i=0;i<MAX_INVENTORY;i++) {
  7185. if( (type&NMDT_INVENTORY && !sd->inventory.u.items_inventory[i].equip)
  7186. || (type&NMDT_EQUIP && sd->inventory.u.items_inventory[i].equip)
  7187. || type&NMDT_ALL)
  7188. {
  7189. int l;
  7190. ARR_FIND( 0, MAX_INVENTORY, l, eq_n[l] <= 0 );
  7191. if( l < MAX_INVENTORY )
  7192. eq_n[l] = i;
  7193. eq_num++;
  7194. }
  7195. }
  7196. if(eq_num > 0){
  7197. int n = eq_n[rnd()%eq_num];
  7198. if(rnd()%10000 < per) {
  7199. if(sd->inventory.u.items_inventory[n].equip)
  7200. pc_unequipitem(sd,n,3);
  7201. pc_dropitem(sd,n,1);
  7202. }
  7203. }
  7204. }
  7205. else if(id > 0) {
  7206. for(i=0;i<MAX_INVENTORY;i++){
  7207. if(sd->inventory.u.items_inventory[i].nameid == id
  7208. && rnd()%10000 < per
  7209. && ((type&NMDT_INVENTORY && !sd->inventory.u.items_inventory[i].equip)
  7210. || (type&NMDT_EQUIP && sd->inventory.u.items_inventory[i].equip)
  7211. || type&NMDT_ALL) ){
  7212. if(sd->inventory.u.items_inventory[i].equip)
  7213. pc_unequipitem(sd,i,3);
  7214. pc_dropitem(sd,i,1);
  7215. break;
  7216. }
  7217. }
  7218. }
  7219. }
  7220. }
  7221. // pvp
  7222. // disable certain pvp functions on pk_mode [Valaris]
  7223. if( !battle_config.pk_mode && mapdata->flag[MF_PVP] && !mapdata->flag[MF_PVP_NOCALCRANK] ) {
  7224. sd->pvp_point -= 5;
  7225. sd->pvp_lost++;
  7226. if( src && src->type == BL_PC ) {
  7227. struct map_session_data *ssd = (struct map_session_data *)src;
  7228. ssd->pvp_point++;
  7229. ssd->pvp_won++;
  7230. }
  7231. if( sd->pvp_point < 0 ) {
  7232. sd->respawn_tid = add_timer(tick+1000, pc_respawn_timer,sd->bl.id,0);
  7233. return 1|8;
  7234. }
  7235. }
  7236. //GvG
  7237. if( mapdata_flag_gvg2(mapdata) ) {
  7238. sd->respawn_tid = add_timer(tick+1000, pc_respawn_timer, sd->bl.id, 0);
  7239. return 1|8;
  7240. }
  7241. else if( sd->bg_id ) {
  7242. struct battleground_data *bg = bg_team_search(sd->bg_id);
  7243. if( bg && bg->mapindex > 0 ) { // Respawn by BG
  7244. sd->respawn_tid = add_timer(tick+1000, pc_respawn_timer, sd->bl.id, 0);
  7245. return 1|8;
  7246. }
  7247. }
  7248. //Reset "can log out" tick.
  7249. if( battle_config.prevent_logout )
  7250. sd->canlog_tick = gettick() - battle_config.prevent_logout;
  7251. return 1;
  7252. }
  7253. void pc_revive(struct map_session_data *sd,unsigned int hp, unsigned int sp) {
  7254. if(hp) clif_updatestatus(sd,SP_HP);
  7255. if(sp) clif_updatestatus(sd,SP_SP);
  7256. pc_setstand(sd, true);
  7257. if(battle_config.pc_invincible_time > 0)
  7258. pc_setinvincibletimer(sd, battle_config.pc_invincible_time);
  7259. if( sd->state.gmaster_flag ) {
  7260. guild_guildaura_refresh(sd,GD_LEADERSHIP,guild_checkskill(sd->guild,GD_LEADERSHIP));
  7261. guild_guildaura_refresh(sd,GD_GLORYWOUNDS,guild_checkskill(sd->guild,GD_GLORYWOUNDS));
  7262. guild_guildaura_refresh(sd,GD_SOULCOLD,guild_checkskill(sd->guild,GD_SOULCOLD));
  7263. guild_guildaura_refresh(sd,GD_HAWKEYES,guild_checkskill(sd->guild,GD_HAWKEYES));
  7264. }
  7265. }
  7266. bool pc_revive_item(struct map_session_data *sd) {
  7267. nullpo_retr(false, sd);
  7268. if (!pc_isdead(sd) || sd->respawn_tid != INVALID_TIMER)
  7269. return false;
  7270. if (sd->sc.data[SC_HELLPOWER]) // Cannot resurrect while under the effect of SC_HELLPOWER.
  7271. return false;
  7272. int16 item_position = itemdb_group_item_exists_pc(sd, IG_TOKEN_OF_SIEGFRIED);
  7273. uint8 hp = 100, sp = 100;
  7274. if (item_position < 0) {
  7275. if (sd->sc.data[SC_LIGHT_OF_REGENE]) {
  7276. hp = sd->sc.data[SC_LIGHT_OF_REGENE]->val2;
  7277. sp = 0;
  7278. }
  7279. else
  7280. return false;
  7281. }
  7282. if (!status_revive(&sd->bl, hp, sp))
  7283. return false;
  7284. if (item_position < 0)
  7285. status_change_end(&sd->bl, SC_LIGHT_OF_REGENE, INVALID_TIMER);
  7286. else
  7287. pc_delitem(sd, item_position, 1, 0, 1, LOG_TYPE_CONSUME);
  7288. clif_skill_nodamage(&sd->bl, &sd->bl, ALL_RESURRECTION, 4, 1);
  7289. return true;
  7290. }
  7291. // script
  7292. //
  7293. /*==========================================
  7294. * script reading pc status registry
  7295. *------------------------------------------*/
  7296. int64 pc_readparam(struct map_session_data* sd,int64 type)
  7297. {
  7298. int64 val = 0;
  7299. nullpo_ret(sd);
  7300. switch(type) {
  7301. case SP_SKILLPOINT: val = sd->status.skill_point; break;
  7302. case SP_STATUSPOINT: val = sd->status.status_point; break;
  7303. case SP_ZENY: val = sd->status.zeny; break;
  7304. case SP_BASELEVEL: val = sd->status.base_level; break;
  7305. case SP_JOBLEVEL: val = sd->status.job_level; break;
  7306. case SP_CLASS: val = sd->status.class_; break;
  7307. case SP_BASEJOB: val = pc_mapid2jobid(sd->class_&MAPID_UPPERMASK, sd->status.sex); break; //Base job, extracting upper type.
  7308. case SP_UPPER: val = sd->class_&JOBL_UPPER?1:(sd->class_&JOBL_BABY?2:0); break;
  7309. case SP_BASECLASS: val = pc_mapid2jobid(sd->class_&MAPID_BASEMASK, sd->status.sex); break; //Extract base class tree. [Skotlex]
  7310. case SP_SEX: val = sd->status.sex; break;
  7311. case SP_WEIGHT: val = sd->weight; break;
  7312. case SP_MAXWEIGHT: val = sd->max_weight; break;
  7313. case SP_BASEEXP: val = sd->status.base_exp; break;
  7314. case SP_JOBEXP: val = sd->status.job_exp; break;
  7315. case SP_NEXTBASEEXP: val = pc_nextbaseexp(sd); break;
  7316. case SP_NEXTJOBEXP: val = pc_nextjobexp(sd); break;
  7317. case SP_HP: val = sd->battle_status.hp; break;
  7318. case SP_MAXHP: val = sd->battle_status.max_hp; break;
  7319. case SP_SP: val = sd->battle_status.sp; break;
  7320. case SP_MAXSP: val = sd->battle_status.max_sp; break;
  7321. case SP_STR: val = sd->status.str; break;
  7322. case SP_AGI: val = sd->status.agi; break;
  7323. case SP_VIT: val = sd->status.vit; break;
  7324. case SP_INT: val = sd->status.int_; break;
  7325. case SP_DEX: val = sd->status.dex; break;
  7326. case SP_LUK: val = sd->status.luk; break;
  7327. case SP_KARMA: val = sd->status.karma; break;
  7328. case SP_MANNER: val = sd->status.manner; break;
  7329. case SP_FAME: val = sd->status.fame; break;
  7330. case SP_KILLERRID: val = sd->killerrid; break;
  7331. case SP_KILLEDRID: val = sd->killedrid; break;
  7332. case SP_KILLEDGID: val = sd->killedgid; break;
  7333. case SP_SITTING: val = pc_issit(sd)?1:0; break;
  7334. case SP_CHARMOVE: val = sd->status.character_moves; break;
  7335. case SP_CHARRENAME: val = sd->status.rename; break;
  7336. case SP_CHARFONT: val = sd->status.font; break;
  7337. case SP_BANK_VAULT: val = sd->bank_vault; break;
  7338. case SP_CASHPOINTS: val = sd->cashPoints; break;
  7339. case SP_KAFRAPOINTS: val = sd->kafraPoints; break;
  7340. case SP_ROULETTE_BRONZE: val = sd->roulette_point.bronze; break;
  7341. case SP_ROULETTE_SILVER: val = sd->roulette_point.silver; break;
  7342. case SP_ROULETTE_GOLD: val = sd->roulette_point.gold; break;
  7343. case SP_PCDIECOUNTER: val = sd->die_counter; break;
  7344. case SP_COOKMASTERY: val = sd->cook_mastery; break;
  7345. case SP_ACHIEVEMENT_LEVEL: val = sd->achievement_data.level; break;
  7346. case SP_CRITICAL: val = sd->battle_status.cri/10; break;
  7347. case SP_ASPD: val = (2000-sd->battle_status.amotion)/10; break;
  7348. case SP_BASE_ATK:
  7349. #ifdef RENEWAL
  7350. val = sd->bonus.eatk;
  7351. #else
  7352. val = sd->battle_status.batk;
  7353. #endif
  7354. break;
  7355. case SP_DEF1: val = sd->battle_status.def; break;
  7356. case SP_DEF2: val = sd->battle_status.def2; break;
  7357. case SP_MDEF1: val = sd->battle_status.mdef; break;
  7358. case SP_MDEF2: val = sd->battle_status.mdef2; break;
  7359. case SP_HIT: val = sd->battle_status.hit; break;
  7360. case SP_FLEE1: val = sd->battle_status.flee; break;
  7361. case SP_FLEE2: val = sd->battle_status.flee2; break;
  7362. case SP_DEFELE: val = sd->battle_status.def_ele; break;
  7363. case SP_MAXHPRATE: val = sd->hprate; break;
  7364. case SP_MAXSPRATE: val = sd->sprate; break;
  7365. case SP_SPRATE: val = sd->dsprate; break;
  7366. case SP_SPEED_RATE: val = sd->bonus.speed_rate; break;
  7367. case SP_SPEED_ADDRATE: val = sd->bonus.speed_add_rate; break;
  7368. case SP_ASPD_RATE:
  7369. #ifndef RENEWAL_ASPD
  7370. val = sd->battle_status.aspd_rate;
  7371. #else
  7372. val = sd->battle_status.aspd_rate2;
  7373. #endif
  7374. break;
  7375. case SP_HP_RECOV_RATE: val = sd->hprecov_rate; break;
  7376. case SP_SP_RECOV_RATE: val = sd->sprecov_rate; break;
  7377. case SP_CRITICAL_DEF: val = sd->bonus.critical_def; break;
  7378. case SP_NEAR_ATK_DEF: val = sd->bonus.near_attack_def_rate; break;
  7379. case SP_LONG_ATK_DEF: val = sd->bonus.long_attack_def_rate; break;
  7380. case SP_DOUBLE_RATE: val = sd->bonus.double_rate; break;
  7381. case SP_DOUBLE_ADD_RATE: val = sd->bonus.double_add_rate; break;
  7382. case SP_MATK_RATE: val = sd->matk_rate; break;
  7383. case SP_ATK_RATE: val = sd->bonus.atk_rate; break;
  7384. case SP_MAGIC_ATK_DEF: val = sd->bonus.magic_def_rate; break;
  7385. case SP_MISC_ATK_DEF: val = sd->bonus.misc_def_rate; break;
  7386. case SP_PERFECT_HIT_RATE:val = sd->bonus.perfect_hit; break;
  7387. case SP_PERFECT_HIT_ADD_RATE: val = sd->bonus.perfect_hit_add; break;
  7388. case SP_CRITICAL_RATE: val = sd->critical_rate; break;
  7389. case SP_HIT_RATE: val = sd->hit_rate; break;
  7390. case SP_FLEE_RATE: val = sd->flee_rate; break;
  7391. case SP_FLEE2_RATE: val = sd->flee2_rate; break;
  7392. case SP_DEF_RATE: val = sd->def_rate; break;
  7393. case SP_DEF2_RATE: val = sd->def2_rate; break;
  7394. case SP_MDEF_RATE: val = sd->mdef_rate; break;
  7395. case SP_MDEF2_RATE: val = sd->mdef2_rate; break;
  7396. case SP_RESTART_FULL_RECOVER: val = sd->special_state.restart_full_recover?1:0; break;
  7397. case SP_NO_CASTCANCEL: val = sd->special_state.no_castcancel?1:0; break;
  7398. case SP_NO_CASTCANCEL2: val = sd->special_state.no_castcancel2?1:0; break;
  7399. case SP_NO_SIZEFIX: val = sd->special_state.no_sizefix?1:0; break;
  7400. case SP_NO_MAGIC_DAMAGE: val = sd->special_state.no_magic_damage; break;
  7401. case SP_NO_WEAPON_DAMAGE:val = sd->special_state.no_weapon_damage; break;
  7402. case SP_NO_MISC_DAMAGE: val = sd->special_state.no_misc_damage; break;
  7403. case SP_NO_GEMSTONE: val = sd->special_state.no_gemstone?1:0; break;
  7404. case SP_INTRAVISION: val = sd->special_state.intravision?1:0; break;
  7405. case SP_NO_KNOCKBACK: val = sd->special_state.no_knockback?1:0; break;
  7406. case SP_NO_MADO_FUEL: val = sd->special_state.no_mado_fuel?1:0; break;
  7407. case SP_NO_WALK_DELAY: val = sd->special_state.no_walk_delay?1:0; break;
  7408. case SP_SPLASH_RANGE: val = sd->bonus.splash_range; break;
  7409. case SP_SPLASH_ADD_RANGE:val = sd->bonus.splash_add_range; break;
  7410. case SP_SHORT_WEAPON_DAMAGE_RETURN: val = sd->bonus.short_weapon_damage_return; break;
  7411. case SP_LONG_WEAPON_DAMAGE_RETURN: val = sd->bonus.long_weapon_damage_return; break;
  7412. case SP_MAGIC_DAMAGE_RETURN: val = sd->bonus.magic_damage_return; break;
  7413. case SP_PERFECT_HIDE: val = sd->special_state.perfect_hiding?1:0; break;
  7414. case SP_UNBREAKABLE: val = sd->bonus.unbreakable; break;
  7415. case SP_UNBREAKABLE_WEAPON: val = (sd->bonus.unbreakable_equip&EQP_WEAPON)?1:0; break;
  7416. case SP_UNBREAKABLE_ARMOR: val = (sd->bonus.unbreakable_equip&EQP_ARMOR)?1:0; break;
  7417. case SP_UNBREAKABLE_HELM: val = (sd->bonus.unbreakable_equip&EQP_HELM)?1:0; break;
  7418. case SP_UNBREAKABLE_SHIELD: val = (sd->bonus.unbreakable_equip&EQP_SHIELD)?1:0; break;
  7419. case SP_UNBREAKABLE_GARMENT: val = (sd->bonus.unbreakable_equip&EQP_GARMENT)?1:0; break;
  7420. case SP_UNBREAKABLE_SHOES: val = (sd->bonus.unbreakable_equip&EQP_SHOES)?1:0; break;
  7421. case SP_CLASSCHANGE: val = sd->bonus.classchange; break;
  7422. case SP_LONG_ATK_RATE: val = sd->bonus.long_attack_atk_rate; break;
  7423. case SP_BREAK_WEAPON_RATE: val = sd->bonus.break_weapon_rate; break;
  7424. case SP_BREAK_ARMOR_RATE: val = sd->bonus.break_armor_rate; break;
  7425. case SP_ADD_STEAL_RATE: val = sd->bonus.add_steal_rate; break;
  7426. case SP_DELAYRATE: val = sd->delayrate; break;
  7427. case SP_CRIT_ATK_RATE: val = sd->bonus.crit_atk_rate; break;
  7428. case SP_UNSTRIPABLE_WEAPON: val = (sd->bonus.unstripable_equip&EQP_WEAPON)?1:0; break;
  7429. case SP_UNSTRIPABLE:
  7430. case SP_UNSTRIPABLE_ARMOR:
  7431. val = (sd->bonus.unstripable_equip&EQP_ARMOR)?1:0;
  7432. break;
  7433. case SP_UNSTRIPABLE_HELM: val = (sd->bonus.unstripable_equip&EQP_HELM)?1:0; break;
  7434. case SP_UNSTRIPABLE_SHIELD: val = (sd->bonus.unstripable_equip&EQP_SHIELD)?1:0; break;
  7435. case SP_SP_GAIN_VALUE: val = sd->bonus.sp_gain_value; break;
  7436. case SP_HP_GAIN_VALUE: val = sd->bonus.hp_gain_value; break;
  7437. case SP_LONG_SP_GAIN_VALUE: val = sd->bonus.long_sp_gain_value; break;
  7438. case SP_LONG_HP_GAIN_VALUE: val = sd->bonus.long_hp_gain_value; break;
  7439. case SP_MAGIC_SP_GAIN_VALUE: val = sd->bonus.magic_sp_gain_value; break;
  7440. case SP_MAGIC_HP_GAIN_VALUE: val = sd->bonus.magic_hp_gain_value; break;
  7441. case SP_ADD_HEAL_RATE: val = sd->bonus.add_heal_rate; break;
  7442. case SP_ADD_HEAL2_RATE: val = sd->bonus.add_heal2_rate; break;
  7443. case SP_ADD_ITEM_HEAL_RATE: val = sd->bonus.itemhealrate2; break;
  7444. case SP_EMATK: val = sd->bonus.ematk; break;
  7445. case SP_FIXCASTRATE: val = sd->bonus.fixcastrate; break;
  7446. case SP_ADD_FIXEDCAST: val = sd->bonus.add_fixcast; break;
  7447. case SP_ADD_VARIABLECAST: val = sd->bonus.add_varcast; break;
  7448. case SP_CASTRATE:
  7449. case SP_VARCASTRATE:
  7450. #ifdef RENEWAL_CAST
  7451. val = sd->bonus.varcastrate; break;
  7452. #else
  7453. val = sd->castrate; break;
  7454. #endif
  7455. default:
  7456. ShowError("pc_readparam: Attempt to read unknown parameter '%lld'.\n", type);
  7457. return -1;
  7458. }
  7459. return val;
  7460. }
  7461. /*==========================================
  7462. * script set pc status registry
  7463. *------------------------------------------*/
  7464. bool pc_setparam(struct map_session_data *sd,int64 type,int64 val_tmp)
  7465. {
  7466. nullpo_retr(false,sd);
  7467. int val = static_cast<unsigned int>(val_tmp);
  7468. switch(type){
  7469. case SP_BASELEVEL:
  7470. if (val > pc_maxbaselv(sd)) //Capping to max
  7471. val = pc_maxbaselv(sd);
  7472. if (val > sd->status.base_level) {
  7473. int i = 0;
  7474. int stat=0;
  7475. for (i = 0; i < (int)(val - sd->status.base_level); i++)
  7476. stat += pc_gets_status_point(sd->status.base_level + i);
  7477. sd->status.status_point += stat;
  7478. }
  7479. sd->status.base_level = val;
  7480. sd->status.base_exp = 0;
  7481. // clif_updatestatus(sd, SP_BASELEVEL); // Gets updated at the bottom
  7482. clif_updatestatus(sd, SP_NEXTBASEEXP);
  7483. clif_updatestatus(sd, SP_STATUSPOINT);
  7484. clif_updatestatus(sd, SP_BASEEXP);
  7485. status_calc_pc(sd, SCO_FORCE);
  7486. if(sd->status.party_id)
  7487. party_send_levelup(sd);
  7488. break;
  7489. case SP_JOBLEVEL:
  7490. if (val >= sd->status.job_level) {
  7491. if (val > pc_maxjoblv(sd)) val = pc_maxjoblv(sd);
  7492. sd->status.skill_point += val - sd->status.job_level;
  7493. clif_updatestatus(sd, SP_SKILLPOINT);
  7494. }
  7495. sd->status.job_level = val;
  7496. sd->status.job_exp = 0;
  7497. // clif_updatestatus(sd, SP_JOBLEVEL); // Gets updated at the bottom
  7498. clif_updatestatus(sd, SP_NEXTJOBEXP);
  7499. clif_updatestatus(sd, SP_JOBEXP);
  7500. status_calc_pc(sd, SCO_FORCE);
  7501. break;
  7502. case SP_SKILLPOINT:
  7503. sd->status.skill_point = val;
  7504. break;
  7505. case SP_STATUSPOINT:
  7506. sd->status.status_point = val;
  7507. break;
  7508. case SP_ZENY:
  7509. if( val < 0 )
  7510. return false;// can't set negative zeny
  7511. log_zeny(sd, LOG_TYPE_SCRIPT, sd, -(sd->status.zeny - cap_value(val, 0, MAX_ZENY)));
  7512. sd->status.zeny = cap_value(val, 0, MAX_ZENY);
  7513. break;
  7514. case SP_BASEEXP:
  7515. {
  7516. val = cap_value(val, 0, INT_MAX);
  7517. if (val < sd->status.base_exp) // Lost
  7518. pc_lostexp(sd, sd->status.base_exp - val, 0);
  7519. else // Gained
  7520. pc_gainexp(sd, NULL, val - sd->status.base_exp, 0, 2);
  7521. }
  7522. return true;
  7523. case SP_JOBEXP:
  7524. {
  7525. val = cap_value(val, 0, INT_MAX);
  7526. if (val < sd->status.job_exp) // Lost
  7527. pc_lostexp(sd, 0, sd->status.job_exp - val);
  7528. else // Gained
  7529. pc_gainexp(sd, NULL, 0, val - sd->status.job_exp, 2);
  7530. }
  7531. return true;
  7532. case SP_SEX:
  7533. sd->status.sex = val ? SEX_MALE : SEX_FEMALE;
  7534. break;
  7535. case SP_WEIGHT:
  7536. sd->weight = val;
  7537. break;
  7538. case SP_MAXWEIGHT:
  7539. sd->max_weight = val;
  7540. break;
  7541. case SP_HP:
  7542. sd->battle_status.hp = cap_value(val, 1, (int)sd->battle_status.max_hp);
  7543. break;
  7544. case SP_MAXHP:
  7545. if (sd->status.base_level < 100)
  7546. sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp_lv99);
  7547. else if (sd->status.base_level < 151)
  7548. sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp_lv150);
  7549. else
  7550. sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp);
  7551. if( sd->battle_status.max_hp < sd->battle_status.hp )
  7552. {
  7553. sd->battle_status.hp = sd->battle_status.max_hp;
  7554. clif_updatestatus(sd, SP_HP);
  7555. }
  7556. break;
  7557. case SP_SP:
  7558. sd->battle_status.sp = cap_value(val, 0, (int)sd->battle_status.max_sp);
  7559. break;
  7560. case SP_MAXSP:
  7561. sd->battle_status.max_sp = cap_value(val, 1, battle_config.max_sp);
  7562. if( sd->battle_status.max_sp < sd->battle_status.sp )
  7563. {
  7564. sd->battle_status.sp = sd->battle_status.max_sp;
  7565. clif_updatestatus(sd, SP_SP);
  7566. }
  7567. break;
  7568. case SP_STR:
  7569. sd->status.str = cap_value(val, 1, pc_maxparameter(sd,PARAM_STR));
  7570. break;
  7571. case SP_AGI:
  7572. sd->status.agi = cap_value(val, 1, pc_maxparameter(sd,PARAM_AGI));
  7573. break;
  7574. case SP_VIT:
  7575. sd->status.vit = cap_value(val, 1, pc_maxparameter(sd,PARAM_VIT));
  7576. break;
  7577. case SP_INT:
  7578. sd->status.int_ = cap_value(val, 1, pc_maxparameter(sd,PARAM_INT));
  7579. break;
  7580. case SP_DEX:
  7581. sd->status.dex = cap_value(val, 1, pc_maxparameter(sd,PARAM_DEX));
  7582. break;
  7583. case SP_LUK:
  7584. sd->status.luk = cap_value(val, 1, pc_maxparameter(sd,PARAM_LUK));
  7585. break;
  7586. case SP_KARMA:
  7587. sd->status.karma = val;
  7588. break;
  7589. case SP_MANNER:
  7590. sd->status.manner = val;
  7591. if( val < 0 )
  7592. sc_start(NULL, &sd->bl, SC_NOCHAT, 100, 0, 0);
  7593. else {
  7594. status_change_end(&sd->bl, SC_NOCHAT, INVALID_TIMER);
  7595. clif_manner_message(sd, 5);
  7596. }
  7597. return true; // status_change_start/status_change_end already sends packets warning the client
  7598. case SP_FAME:
  7599. sd->status.fame = val;
  7600. break;
  7601. case SP_KILLERRID:
  7602. sd->killerrid = val;
  7603. return true;
  7604. case SP_KILLEDRID:
  7605. sd->killedrid = val;
  7606. return true;
  7607. case SP_KILLEDGID:
  7608. sd->killedgid = val;
  7609. return true;
  7610. case SP_CHARMOVE:
  7611. sd->status.character_moves = val;
  7612. return true;
  7613. case SP_CHARRENAME:
  7614. sd->status.rename = val;
  7615. return true;
  7616. case SP_CHARFONT:
  7617. sd->status.font = val;
  7618. clif_font(sd);
  7619. return true;
  7620. case SP_BANK_VAULT:
  7621. if (val < 0)
  7622. return false;
  7623. log_zeny(sd, LOG_TYPE_BANK, sd, -(sd->bank_vault - cap_value(val, 0, MAX_BANK_ZENY)));
  7624. sd->bank_vault = cap_value(val, 0, MAX_BANK_ZENY);
  7625. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  7626. return true;
  7627. case SP_ROULETTE_BRONZE:
  7628. sd->roulette_point.bronze = val;
  7629. pc_setreg2(sd, ROULETTE_BRONZE_VAR, sd->roulette_point.bronze);
  7630. return true;
  7631. case SP_ROULETTE_SILVER:
  7632. sd->roulette_point.silver = val;
  7633. pc_setreg2(sd, ROULETTE_SILVER_VAR, sd->roulette_point.silver);
  7634. return true;
  7635. case SP_ROULETTE_GOLD:
  7636. sd->roulette_point.gold = val;
  7637. pc_setreg2(sd, ROULETTE_GOLD_VAR, sd->roulette_point.gold);
  7638. return true;
  7639. case SP_CASHPOINTS:
  7640. if (val < 0)
  7641. return false;
  7642. if (!sd->state.connect_new)
  7643. log_cash(sd, LOG_TYPE_SCRIPT, LOG_CASH_TYPE_CASH, -(sd->cashPoints - cap_value(val, 0, MAX_ZENY)));
  7644. sd->cashPoints = cap_value(val, 0, MAX_ZENY);
  7645. pc_setaccountreg(sd, add_str(CASHPOINT_VAR), sd->cashPoints);
  7646. return true;
  7647. case SP_KAFRAPOINTS:
  7648. if (val < 0)
  7649. return false;
  7650. if (!sd->state.connect_new)
  7651. log_cash(sd, LOG_TYPE_SCRIPT, LOG_CASH_TYPE_KAFRA, -(sd->kafraPoints - cap_value(val, 0, MAX_ZENY)));
  7652. sd->kafraPoints = cap_value(val, 0, MAX_ZENY);
  7653. pc_setaccountreg(sd, add_str(KAFRAPOINT_VAR), sd->kafraPoints);
  7654. return true;
  7655. case SP_PCDIECOUNTER:
  7656. if (val < 0)
  7657. return false;
  7658. if (sd->die_counter == val)
  7659. return true;
  7660. sd->die_counter = val;
  7661. if (!sd->die_counter && (sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE)
  7662. status_calc_pc(sd, SCO_NONE); // Lost the bonus.
  7663. pc_setglobalreg(sd, add_str(PCDIECOUNTER_VAR), sd->die_counter);
  7664. return true;
  7665. case SP_COOKMASTERY:
  7666. if (val < 0)
  7667. return false;
  7668. if (sd->cook_mastery == val)
  7669. return true;
  7670. val = cap_value(val, 0, 1999);
  7671. sd->cook_mastery = val;
  7672. pc_setglobalreg(sd, add_str(COOKMASTERY_VAR), sd->cook_mastery);
  7673. return true;
  7674. default:
  7675. ShowError("pc_setparam: Attempted to set unknown parameter '%lld'.\n", type);
  7676. return false;
  7677. }
  7678. clif_updatestatus(sd,static_cast<int>(type));
  7679. return true;
  7680. }
  7681. /*==========================================
  7682. * HP/SP Healing. If flag is passed, the heal type is through clif_heal, otherwise update status.
  7683. *------------------------------------------*/
  7684. void pc_heal(struct map_session_data *sd,unsigned int hp,unsigned int sp, int type)
  7685. {
  7686. if (type&2) {
  7687. if (hp || type&4)
  7688. clif_heal(sd->fd,SP_HP,hp);
  7689. if (sp)
  7690. clif_heal(sd->fd,SP_SP,sp);
  7691. } else {
  7692. if(hp)
  7693. clif_updatestatus(sd,SP_HP);
  7694. if(sp)
  7695. clif_updatestatus(sd,SP_SP);
  7696. }
  7697. return;
  7698. }
  7699. /**
  7700. * Heal player HP and/or SP linearly. Calculate any bonus based on active statuses.
  7701. * @param sd: Player data
  7702. * @param itemid: Item ID
  7703. * @param hp: HP to heal
  7704. * @param sp: SP to heal
  7705. * @return Amount healed to an object
  7706. */
  7707. int pc_itemheal(struct map_session_data *sd, int itemid, int hp, int sp)
  7708. {
  7709. int bonus, tmp, penalty = 0;
  7710. if (hp) {
  7711. bonus = 100 + (sd->battle_status.vit << 1) + pc_checkskill(sd, SM_RECOVERY) * 10 + pc_checkskill(sd, AM_LEARNINGPOTION) * 5;
  7712. // A potion produced by an Alchemist in the Fame Top 10 gets +50% effect [DracoRPG]
  7713. if (potion_flag == 2) {
  7714. bonus += 50;
  7715. if (sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_ROGUE)
  7716. bonus += 100; // Receive an additional +100% effect from ranked potions to HP only
  7717. }
  7718. //All item bonuses.
  7719. bonus += sd->bonus.itemhealrate2;
  7720. //Item Group bonuses
  7721. bonus += pc_get_itemgroup_bonus(sd, itemid);
  7722. //Individual item bonuses.
  7723. for(const auto &it : sd->itemhealrate) {
  7724. if (it.id == itemid) {
  7725. bonus += it.val;
  7726. break;
  7727. }
  7728. }
  7729. // Recovery Potion
  7730. if (sd->sc.data[SC_INCHEALRATE])
  7731. bonus += sd->sc.data[SC_INCHEALRATE]->val1;
  7732. // 2014 Halloween Event : Pumpkin Bonus
  7733. if (sd->sc.data[SC_MTF_PUMPKIN] && itemid == ITEMID_PUMPKIN)
  7734. bonus += sd->sc.data[SC_MTF_PUMPKIN]->val1;
  7735. tmp = hp * bonus / 100; // Overflow check
  7736. if (bonus != 100 && tmp > hp)
  7737. hp = tmp;
  7738. }
  7739. if (sp) {
  7740. bonus = 100 + (sd->battle_status.int_ << 1) + pc_checkskill(sd, MG_SRECOVERY) * 10 + pc_checkskill(sd, AM_LEARNINGPOTION) * 5;
  7741. // A potion produced by an Alchemist in the Fame Top 10 gets +50% effect [DracoRPG]
  7742. if (potion_flag == 2)
  7743. bonus += 50;
  7744. tmp = sp * bonus / 100; // Overflow check
  7745. if (bonus != 100 && tmp > sp)
  7746. sp = tmp;
  7747. }
  7748. if (sd->sc.count) {
  7749. // Critical Wound and Death Hurt stack
  7750. if (sd->sc.data[SC_CRITICALWOUND])
  7751. penalty += sd->sc.data[SC_CRITICALWOUND]->val2;
  7752. if (sd->sc.data[SC_DEATHHURT])
  7753. penalty += 20;
  7754. if (sd->sc.data[SC_NORECOVER_STATE])
  7755. penalty = 100;
  7756. if (sd->sc.data[SC_VITALITYACTIVATION]) {
  7757. hp += hp / 2; // 1.5 times
  7758. sp -= sp / 2;
  7759. }
  7760. if (sd->sc.data[SC_WATER_INSIGNIA] && sd->sc.data[SC_WATER_INSIGNIA]->val1 == 2) {
  7761. hp += hp / 10;
  7762. sp += sp / 10;
  7763. }
  7764. if (penalty > 0) {
  7765. hp -= hp * penalty / 100;
  7766. sp -= sp * penalty / 100;
  7767. }
  7768. #ifdef RENEWAL
  7769. if (sd->sc.data[SC_EXTREMITYFIST2])
  7770. sp = 0;
  7771. #endif
  7772. if (sd->sc.data[SC_BITESCAR])
  7773. hp = 0;
  7774. }
  7775. return status_heal(&sd->bl, hp, sp, 1);
  7776. }
  7777. /*==========================================
  7778. * HP/SP Recovery
  7779. * Heal player hp nad/or sp by rate
  7780. *------------------------------------------*/
  7781. int pc_percentheal(struct map_session_data *sd,int hp,int sp)
  7782. {
  7783. nullpo_ret(sd);
  7784. if (hp > 100) hp = 100;
  7785. else if (hp <-100) hp = -100;
  7786. if (sp > 100) sp = 100;
  7787. else if (sp <-100) sp = -100;
  7788. if(hp >= 0 && sp >= 0) //Heal
  7789. return status_percent_heal(&sd->bl, hp, sp);
  7790. if(hp <= 0 && sp <= 0) //Damage (negative rates indicate % of max rather than current), and only kill target IF the specified amount is 100%
  7791. return status_percent_damage(NULL, &sd->bl, hp, sp, hp==-100);
  7792. //Crossed signs
  7793. if(hp) {
  7794. if(hp > 0)
  7795. status_percent_heal(&sd->bl, hp, 0);
  7796. else
  7797. status_percent_damage(NULL, &sd->bl, hp, 0, hp==-100);
  7798. }
  7799. if(sp) {
  7800. if(sp > 0)
  7801. status_percent_heal(&sd->bl, 0, sp);
  7802. else
  7803. status_percent_damage(NULL, &sd->bl, 0, sp, false);
  7804. }
  7805. return 0;
  7806. }
  7807. static int jobchange_killclone(struct block_list *bl, va_list ap)
  7808. {
  7809. struct mob_data *md;
  7810. int flag;
  7811. md = (struct mob_data *)bl;
  7812. nullpo_ret(md);
  7813. flag = va_arg(ap, int);
  7814. if (md->master_id && md->special_state.clone && md->master_id == flag)
  7815. status_kill(&md->bl);
  7816. return 1;
  7817. }
  7818. /**
  7819. * Called when player changes job
  7820. * Rewrote to make it tidider [Celest]
  7821. * @param sd
  7822. * @param job JOB ID. See enum e_job
  7823. * @param upper 1 - JOBL_UPPER; 2 - JOBL_BABY
  7824. * @return True if success, false if failed
  7825. **/
  7826. bool pc_jobchange(struct map_session_data *sd,int job, char upper)
  7827. {
  7828. int i, fame_flag = 0;
  7829. int b_class;
  7830. nullpo_retr(false,sd);
  7831. if (job < 0)
  7832. return false;
  7833. //Normalize job.
  7834. b_class = pc_jobid2mapid(job);
  7835. if (b_class == -1)
  7836. return false;
  7837. switch (upper) {
  7838. case 1:
  7839. b_class|= JOBL_UPPER;
  7840. break;
  7841. case 2:
  7842. b_class|= JOBL_BABY;
  7843. break;
  7844. }
  7845. //This will automatically adjust bard/dancer classes to the correct gender
  7846. //That is, if you try to jobchange into dancer, it will turn you to bard.
  7847. job = pc_mapid2jobid(b_class, sd->status.sex);
  7848. if (job == -1)
  7849. return false;
  7850. if ((unsigned short)b_class == sd->class_)
  7851. return false; //Nothing to change.
  7852. // changing from 1st to 2nd job
  7853. if ((b_class&JOBL_2) && !(sd->class_&JOBL_2) && (sd->class_&MAPID_UPPERMASK) != MAPID_SUPER_NOVICE) {
  7854. sd->change_level_2nd = sd->status.job_level;
  7855. pc_setglobalreg(sd, add_str(JOBCHANGE2ND_VAR), sd->change_level_2nd);
  7856. }
  7857. // changing from 2nd to 3rd job
  7858. else if((b_class&JOBL_THIRD) && !(sd->class_&JOBL_THIRD)) {
  7859. sd->change_level_3rd = sd->status.job_level;
  7860. pc_setglobalreg(sd, add_str(JOBCHANGE3RD_VAR), sd->change_level_3rd);
  7861. }
  7862. if(sd->cloneskill_idx > 0) {
  7863. if( sd->status.skill[sd->cloneskill_idx].flag == SKILL_FLAG_PLAGIARIZED ) {
  7864. sd->status.skill[sd->cloneskill_idx].id = 0;
  7865. sd->status.skill[sd->cloneskill_idx].lv = 0;
  7866. sd->status.skill[sd->cloneskill_idx].flag = SKILL_FLAG_PERMANENT;
  7867. clif_deleteskill(sd, static_cast<int>(pc_readglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM))));
  7868. }
  7869. sd->cloneskill_idx = 0;
  7870. pc_setglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM), 0);
  7871. pc_setglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM_LV), 0);
  7872. }
  7873. if(sd->reproduceskill_idx > 0) {
  7874. if( sd->status.skill[sd->reproduceskill_idx].flag == SKILL_FLAG_PLAGIARIZED ) {
  7875. sd->status.skill[sd->reproduceskill_idx].id = 0;
  7876. sd->status.skill[sd->reproduceskill_idx].lv = 0;
  7877. sd->status.skill[sd->reproduceskill_idx].flag = SKILL_FLAG_PERMANENT;
  7878. clif_deleteskill(sd, static_cast<int>(pc_readglobalreg(sd, add_str(SKILL_VAR_REPRODUCE))));
  7879. }
  7880. sd->reproduceskill_idx = 0;
  7881. pc_setglobalreg(sd, add_str(SKILL_VAR_REPRODUCE), 0);
  7882. pc_setglobalreg(sd, add_str(SKILL_VAR_REPRODUCE_LV), 0);
  7883. }
  7884. // Give or reduce transcendent status points
  7885. if( (b_class&JOBL_UPPER) && !(sd->class_&JOBL_UPPER) ){ // Change from a non t class to a t class -> give points
  7886. sd->status.status_point += battle_config.transcendent_status_points;
  7887. clif_updatestatus(sd,SP_STATUSPOINT);
  7888. }else if( !(b_class&JOBL_UPPER) && (sd->class_&JOBL_UPPER) ){ // Change from a t class to a non t class -> remove points
  7889. if( sd->status.status_point < battle_config.transcendent_status_points ){
  7890. // The player already used his bonus points, so we have to reset his status points
  7891. pc_resetstate(sd);
  7892. }
  7893. sd->status.status_point -= battle_config.transcendent_status_points;
  7894. clif_updatestatus(sd,SP_STATUSPOINT);
  7895. }
  7896. if ( (b_class&MAPID_UPPERMASK) != (sd->class_&MAPID_UPPERMASK) ) { //Things to remove when changing class tree.
  7897. const int class_ = pc_class2idx(sd->status.class_);
  7898. uint16 skill_id;
  7899. for(i = 0; i < MAX_SKILL_TREE && (skill_id = skill_tree[class_][i].skill_id) > 0; i++) {
  7900. //Remove status specific to your current tree skills.
  7901. enum sc_type sc = status_skill2sc(skill_id);
  7902. if (sc > SC_COMMON_MAX && sd->sc.data[sc])
  7903. status_change_end(&sd->bl, sc, INVALID_TIMER);
  7904. }
  7905. }
  7906. if( (sd->class_&MAPID_UPPERMASK) == MAPID_STAR_GLADIATOR && (b_class&MAPID_UPPERMASK) != MAPID_STAR_GLADIATOR) {
  7907. /* going off star glad lineage, reset feel to not store no-longer-used vars in the database */
  7908. pc_resetfeel(sd);
  7909. }
  7910. // Reset body style to 0 before changing job to avoid
  7911. // errors since not every job has a alternate outfit.
  7912. sd->status.body = 0;
  7913. clif_changelook(&sd->bl,LOOK_BODY2,0);
  7914. sd->status.class_ = job;
  7915. fame_flag = pc_famerank(sd->status.char_id,sd->class_&MAPID_UPPERMASK);
  7916. sd->class_ = (unsigned short)b_class;
  7917. sd->status.job_level=1;
  7918. sd->status.job_exp=0;
  7919. if (sd->status.base_level > pc_maxbaselv(sd)) {
  7920. sd->status.base_level = pc_maxbaselv(sd);
  7921. sd->status.base_exp=0;
  7922. pc_resetstate(sd);
  7923. clif_updatestatus(sd,SP_STATUSPOINT);
  7924. clif_updatestatus(sd,SP_BASELEVEL);
  7925. clif_updatestatus(sd,SP_BASEEXP);
  7926. clif_updatestatus(sd,SP_NEXTBASEEXP);
  7927. }
  7928. clif_updatestatus(sd,SP_JOBLEVEL);
  7929. clif_updatestatus(sd,SP_JOBEXP);
  7930. clif_updatestatus(sd,SP_NEXTJOBEXP);
  7931. for(i=0;i<EQI_MAX;i++) {
  7932. if(sd->equip_index[i] >= 0)
  7933. if(pc_isequip(sd,sd->equip_index[i]))
  7934. pc_unequipitem(sd,sd->equip_index[i],2); // unequip invalid item for class
  7935. }
  7936. //Change look, if disguised, you need to undisguise
  7937. //to correctly calculate new job sprite without
  7938. if (sd->disguise)
  7939. pc_disguise(sd, 0);
  7940. status_set_viewdata(&sd->bl, job);
  7941. clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_); // move sprite update to prevent client crashes with incompatible equipment [Valaris]
  7942. #if PACKETVER >= 20151001
  7943. clif_changelook(&sd->bl, LOOK_HAIR, sd->vd.hair_style); // Update player's head (only matters when switching to or from Doram)
  7944. #endif
  7945. if(sd->vd.cloth_color)
  7946. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  7947. /*
  7948. if(sd->vd.body_style)
  7949. clif_changelook(&sd->bl,LOOK_BODY2,sd->vd.body_style);
  7950. */
  7951. //Update skill tree.
  7952. pc_calc_skilltree(sd);
  7953. clif_skillinfoblock(sd);
  7954. if (sd->ed)
  7955. elemental_delete(sd->ed);
  7956. if (sd->state.vending)
  7957. vending_closevending(sd);
  7958. if (sd->state.buyingstore)
  7959. buyingstore_close(sd);
  7960. map_foreachinmap(jobchange_killclone, sd->bl.m, BL_MOB, sd->bl.id);
  7961. //Remove peco/cart/falcon
  7962. i = sd->sc.option;
  7963. if( i&OPTION_RIDING && !pc_checkskill(sd, KN_RIDING) )
  7964. i&=~OPTION_RIDING;
  7965. if( i&OPTION_FALCON && !pc_checkskill(sd, HT_FALCON) )
  7966. i&=~OPTION_FALCON;
  7967. if( i&OPTION_DRAGON && !pc_checkskill(sd,RK_DRAGONTRAINING) )
  7968. i&=~OPTION_DRAGON;
  7969. if( i&OPTION_WUGRIDER && !pc_checkskill(sd,RA_WUGMASTERY) )
  7970. i&=~OPTION_WUGRIDER;
  7971. if( i&OPTION_WUG && !pc_checkskill(sd,RA_WUGMASTERY) )
  7972. i&=~OPTION_WUG;
  7973. if( i&OPTION_MADOGEAR ) //You do not need a skill for this.
  7974. i&=~OPTION_MADOGEAR;
  7975. #ifndef NEW_CARTS
  7976. if( i&OPTION_CART && !pc_checkskill(sd, MC_PUSHCART) )
  7977. i&=~OPTION_CART;
  7978. #else
  7979. if( sd->sc.data[SC_PUSH_CART] && !pc_checkskill(sd, MC_PUSHCART) )
  7980. pc_setcart(sd, 0);
  7981. #endif
  7982. if(i != sd->sc.option)
  7983. pc_setoption(sd, i);
  7984. if(hom_is_active(sd->hd) && !pc_checkskill(sd, AM_CALLHOMUN))
  7985. hom_vaporize(sd, HOM_ST_ACTIVE);
  7986. if (sd->sc.data[SC_SPRITEMABLE] && !pc_checkskill(sd, SU_SPRITEMABLE))
  7987. status_change_end(&sd->bl, SC_SPRITEMABLE, INVALID_TIMER);
  7988. if(sd->status.manner < 0)
  7989. clif_changestatus(sd,SP_MANNER,sd->status.manner);
  7990. status_calc_pc(sd,SCO_FORCE);
  7991. pc_checkallowskill(sd);
  7992. pc_equiplookall(sd);
  7993. pc_show_questinfo(sd);
  7994. achievement_update_objective(sd, AG_JOB_CHANGE, 2, sd->status.base_level, job);
  7995. if( sd->status.party_id ){
  7996. struct party_data* p;
  7997. if( ( p = party_search( sd->status.party_id ) ) != NULL ){
  7998. ARR_FIND(0, MAX_PARTY, i, p->party.member[i].char_id == sd->status.char_id);
  7999. if( i < MAX_PARTY ){
  8000. p->party.member[i].class_ = sd->status.class_;
  8001. clif_party_job_and_level(sd);
  8002. }
  8003. }
  8004. }
  8005. chrif_save(sd, CSAVE_NORMAL);
  8006. //if you were previously famous, not anymore.
  8007. if (fame_flag)
  8008. chrif_buildfamelist();
  8009. else if (sd->status.fame > 0) {
  8010. //It may be that now they are famous?
  8011. switch (sd->class_&MAPID_UPPERMASK) {
  8012. case MAPID_BLACKSMITH:
  8013. case MAPID_ALCHEMIST:
  8014. case MAPID_TAEKWON:
  8015. chrif_buildfamelist();
  8016. break;
  8017. }
  8018. }
  8019. return true;
  8020. }
  8021. /*==========================================
  8022. * Tell client player sd has change equipement
  8023. *------------------------------------------*/
  8024. void pc_equiplookall(struct map_session_data *sd)
  8025. {
  8026. nullpo_retv(sd);
  8027. clif_changelook(&sd->bl,LOOK_WEAPON,0);
  8028. clif_changelook(&sd->bl,LOOK_SHOES,0);
  8029. clif_changelook(&sd->bl,LOOK_HEAD_BOTTOM,sd->status.head_bottom);
  8030. clif_changelook(&sd->bl,LOOK_HEAD_TOP,sd->status.head_top);
  8031. clif_changelook(&sd->bl,LOOK_HEAD_MID,sd->status.head_mid);
  8032. clif_changelook(&sd->bl,LOOK_ROBE, sd->status.robe);
  8033. }
  8034. /*==========================================
  8035. * Tell client player sd has change look (hair,equip...)
  8036. *------------------------------------------*/
  8037. void pc_changelook(struct map_session_data *sd,int type,int val) {
  8038. nullpo_retv(sd);
  8039. switch(type) {
  8040. case LOOK_HAIR: //Use the battle_config limits! [Skotlex]
  8041. val = cap_value(val, MIN_HAIR_STYLE, MAX_HAIR_STYLE);
  8042. if (sd->status.hair != val) {
  8043. sd->status.hair = val;
  8044. if (sd->status.guild_id) //Update Guild Window. [Skotlex]
  8045. intif_guild_change_memberinfo(sd->status.guild_id, sd->status.account_id, sd->status.char_id,
  8046. GMI_HAIR, &sd->status.hair, sizeof(sd->status.hair));
  8047. }
  8048. break;
  8049. case LOOK_WEAPON:
  8050. sd->status.weapon = val;
  8051. break;
  8052. case LOOK_HEAD_BOTTOM:
  8053. sd->status.head_bottom = val;
  8054. sd->setlook_head_bottom = val;
  8055. break;
  8056. case LOOK_HEAD_TOP:
  8057. sd->status.head_top = val;
  8058. sd->setlook_head_top = val;
  8059. break;
  8060. case LOOK_HEAD_MID:
  8061. sd->status.head_mid = val;
  8062. sd->setlook_head_mid = val;
  8063. break;
  8064. case LOOK_HAIR_COLOR: //Use the battle_config limits! [Skotlex]
  8065. val = cap_value(val, MIN_HAIR_COLOR, MAX_HAIR_COLOR);
  8066. if (sd->status.hair_color != val) {
  8067. sd->status.hair_color = val;
  8068. if (sd->status.guild_id) //Update Guild Window. [Skotlex]
  8069. intif_guild_change_memberinfo(sd->status.guild_id, sd->status.account_id, sd->status.char_id,
  8070. GMI_HAIR_COLOR, &sd->status.hair_color, sizeof(sd->status.hair_color));
  8071. }
  8072. break;
  8073. case LOOK_CLOTHES_COLOR: //Use the battle_config limits! [Skotlex]
  8074. val = cap_value(val, MIN_CLOTH_COLOR, MAX_CLOTH_COLOR);
  8075. sd->status.clothes_color = val;
  8076. break;
  8077. case LOOK_SHIELD:
  8078. sd->status.shield = val;
  8079. break;
  8080. case LOOK_SHOES:
  8081. break;
  8082. case LOOK_ROBE:
  8083. sd->status.robe = val;
  8084. sd->setlook_robe = val;
  8085. break;
  8086. case LOOK_BODY2:
  8087. val = cap_value(val, MIN_BODY_STYLE, MAX_BODY_STYLE);
  8088. sd->status.body = val;
  8089. break;
  8090. }
  8091. clif_changelook(&sd->bl, type, val);
  8092. }
  8093. /*==========================================
  8094. * Give an option (type) to player (sd) and display it to client
  8095. *------------------------------------------*/
  8096. void pc_setoption(struct map_session_data *sd,int type)
  8097. {
  8098. int p_type, new_look=0;
  8099. nullpo_retv(sd);
  8100. p_type = sd->sc.option;
  8101. //Option has to be changed client-side before the class sprite or it won't always work (eg: Wedding sprite) [Skotlex]
  8102. sd->sc.option=type;
  8103. clif_changeoption(&sd->bl);
  8104. if( (type&OPTION_RIDING && !(p_type&OPTION_RIDING)) || (type&OPTION_DRAGON && !(p_type&OPTION_DRAGON) && pc_checkskill(sd,RK_DRAGONTRAINING) > 0) )
  8105. { // Mounting
  8106. clif_status_load(&sd->bl,EFST_RIDING,1);
  8107. status_calc_pc(sd,SCO_NONE);
  8108. }
  8109. else if( (!(type&OPTION_RIDING) && p_type&OPTION_RIDING) || (!(type&OPTION_DRAGON) && p_type&OPTION_DRAGON && pc_checkskill(sd,RK_DRAGONTRAINING) > 0) )
  8110. { // Dismount
  8111. clif_status_load(&sd->bl,EFST_RIDING,0);
  8112. status_calc_pc(sd,SCO_NONE);
  8113. }
  8114. #ifndef NEW_CARTS
  8115. if( type&OPTION_CART && !( p_type&OPTION_CART ) ) { //Cart On
  8116. clif_cartlist(sd);
  8117. clif_updatestatus(sd, SP_CARTINFO);
  8118. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  8119. status_calc_pc(sd,SCO_NONE); //Apply speed penalty.
  8120. } else if( !( type&OPTION_CART ) && p_type&OPTION_CART ){ //Cart Off
  8121. clif_clearcart(sd->fd);
  8122. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  8123. status_calc_pc(sd,SCO_NONE); //Remove speed penalty.
  8124. }
  8125. #endif
  8126. if (type&OPTION_FALCON && !(p_type&OPTION_FALCON)) //Falcon ON
  8127. clif_status_load(&sd->bl,EFST_FALCON,1);
  8128. else if (!(type&OPTION_FALCON) && p_type&OPTION_FALCON) //Falcon OFF
  8129. clif_status_load(&sd->bl,EFST_FALCON,0);
  8130. if( (sd->class_&MAPID_THIRDMASK) == MAPID_RANGER ) {
  8131. if( type&OPTION_WUGRIDER && !(p_type&OPTION_WUGRIDER) ) { // Mounting
  8132. clif_status_load(&sd->bl,EFST_WUGRIDER,1);
  8133. status_calc_pc(sd,SCO_NONE);
  8134. } else if( !(type&OPTION_WUGRIDER) && p_type&OPTION_WUGRIDER ) { // Dismount
  8135. clif_status_load(&sd->bl,EFST_WUGRIDER,0);
  8136. status_calc_pc(sd,SCO_NONE);
  8137. }
  8138. }
  8139. if( (sd->class_&MAPID_THIRDMASK) == MAPID_MECHANIC ) {
  8140. if( type&OPTION_MADOGEAR && !(p_type&OPTION_MADOGEAR) ) {
  8141. static const sc_type statuses [] = { SC_MAXIMIZEPOWER, SC_OVERTHRUST, SC_WEAPONPERFECTION, SC_ADRENALINE, SC_CARTBOOST, SC_MELTDOWN, SC_MAXOVERTHRUST };
  8142. status_calc_pc(sd,SCO_NONE);
  8143. for (uint8 i = 0; i < ARRAYLENGTH(statuses); i++) {
  8144. int skill_id = status_sc2skill(statuses[i]);
  8145. if (skill_id > 0 && !skill_get_inf2(skill_id, INF2_ALLOWONMADO))
  8146. status_change_end(&sd->bl,statuses[i],INVALID_TIMER);
  8147. }
  8148. pc_bonus_script_clear(sd,BSF_REM_ON_MADOGEAR);
  8149. } else if( !(type&OPTION_MADOGEAR) && p_type&OPTION_MADOGEAR ) {
  8150. status_calc_pc(sd,SCO_NONE);
  8151. status_change_end(&sd->bl,SC_SHAPESHIFT,INVALID_TIMER);
  8152. status_change_end(&sd->bl,SC_HOVERING,INVALID_TIMER);
  8153. status_change_end(&sd->bl,SC_ACCELERATION,INVALID_TIMER);
  8154. status_change_end(&sd->bl,SC_OVERHEAT_LIMITPOINT,INVALID_TIMER);
  8155. status_change_end(&sd->bl,SC_OVERHEAT,INVALID_TIMER);
  8156. status_change_end(&sd->bl,SC_MAGNETICFIELD,INVALID_TIMER);
  8157. status_change_end(&sd->bl,SC_NEUTRALBARRIER_MASTER,INVALID_TIMER);
  8158. status_change_end(&sd->bl,SC_STEALTHFIELD_MASTER,INVALID_TIMER);
  8159. pc_bonus_script_clear(sd,BSF_REM_ON_MADOGEAR);
  8160. }
  8161. }
  8162. if (type&OPTION_FLYING && !(p_type&OPTION_FLYING))
  8163. new_look = JOB_STAR_GLADIATOR2;
  8164. else if (!(type&OPTION_FLYING) && p_type&OPTION_FLYING)
  8165. new_look = -1;
  8166. if (sd->disguise || !new_look)
  8167. return; //Disguises break sprite changes
  8168. if (new_look < 0) { //Restore normal look.
  8169. status_set_viewdata(&sd->bl, sd->status.class_);
  8170. new_look = sd->vd.class_;
  8171. }
  8172. pc_stop_attack(sd); //Stop attacking on new view change (to prevent wedding/santa attacks.
  8173. clif_changelook(&sd->bl,LOOK_BASE,new_look);
  8174. if (sd->vd.cloth_color)
  8175. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  8176. if( sd->vd.body_style )
  8177. clif_changelook(&sd->bl,LOOK_BODY2,sd->vd.body_style);
  8178. clif_skillinfoblock(sd); // Skill list needs to be updated after base change.
  8179. }
  8180. /**
  8181. * Give player a cart
  8182. * @param sd Player
  8183. * @param type 0:Remove cart, 1 ~ MAX_CARTS: Cart type
  8184. **/
  8185. bool pc_setcart(struct map_session_data *sd,int type) {
  8186. #ifndef NEW_CARTS
  8187. int cart[6] = {0x0000,OPTION_CART1,OPTION_CART2,OPTION_CART3,OPTION_CART4,OPTION_CART5};
  8188. int option;
  8189. #endif
  8190. nullpo_retr(false,sd);
  8191. if( type < 0 || type > MAX_CARTS )
  8192. return false;// Never trust the values sent by the client! [Skotlex]
  8193. if( pc_checkskill(sd,MC_PUSHCART) <= 0 && type != 0 )
  8194. return false;// Push cart is required
  8195. #ifdef NEW_CARTS
  8196. switch( type ) {
  8197. case 0:
  8198. if( !sd->sc.data[SC_PUSH_CART] )
  8199. return 0;
  8200. status_change_end(&sd->bl,SC_PUSH_CART,INVALID_TIMER);
  8201. clif_clearcart(sd->fd);
  8202. break;
  8203. default:/* everything else is an allowed ID so we can move on */
  8204. if( !sd->sc.data[SC_PUSH_CART] ) { /* first time, so fill cart data */
  8205. clif_cartlist(sd);
  8206. status_calc_cart_weight(sd, (e_status_calc_weight_opt)(CALCWT_ITEM|CALCWT_MAXBONUS|CALCWT_CARTSTATE));
  8207. }
  8208. clif_updatestatus(sd, SP_CARTINFO);
  8209. sc_start(&sd->bl, &sd->bl, SC_PUSH_CART, 100, type, 0);
  8210. break;
  8211. }
  8212. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  8213. status_calc_pc(sd,SCO_NONE); //Recalc speed penalty.
  8214. #else
  8215. // Update option
  8216. option = sd->sc.option;
  8217. option &= ~OPTION_CART;// clear cart bits
  8218. option |= cart[type]; // set cart
  8219. pc_setoption(sd, option);
  8220. #endif
  8221. return true;
  8222. }
  8223. /*==========================================
  8224. * Give player a falcon
  8225. *------------------------------------------*/
  8226. void pc_setfalcon(struct map_session_data* sd, int flag)
  8227. {
  8228. if( flag ){
  8229. if( pc_checkskill(sd,HT_FALCON)>0 ) // add falcon if he have the skill
  8230. pc_setoption(sd,sd->sc.option|OPTION_FALCON);
  8231. } else if( pc_isfalcon(sd) ){
  8232. pc_setoption(sd,sd->sc.option&~OPTION_FALCON); // remove falcon
  8233. }
  8234. }
  8235. /*==========================================
  8236. * Set player riding
  8237. *------------------------------------------*/
  8238. void pc_setriding(struct map_session_data* sd, int flag)
  8239. {
  8240. if( sd->sc.data[SC_ALL_RIDING] )
  8241. return;
  8242. if( flag ){
  8243. if( pc_checkskill(sd,KN_RIDING) > 0 ) // add peco
  8244. pc_setoption(sd, sd->sc.option|OPTION_RIDING);
  8245. } else if( pc_isriding(sd) ){
  8246. pc_setoption(sd, sd->sc.option&~OPTION_RIDING);
  8247. }
  8248. }
  8249. /*==========================================
  8250. * Give player a mado
  8251. *------------------------------------------*/
  8252. void pc_setmadogear(struct map_session_data* sd, int flag)
  8253. {
  8254. if( flag ){
  8255. if( pc_checkskill(sd,NC_MADOLICENCE) > 0 )
  8256. pc_setoption(sd, sd->sc.option|OPTION_MADOGEAR);
  8257. } else if( pc_ismadogear(sd) ){
  8258. pc_setoption(sd, sd->sc.option&~OPTION_MADOGEAR);
  8259. }
  8260. }
  8261. /*==========================================
  8262. * Check if player can drop an item
  8263. *------------------------------------------*/
  8264. bool pc_candrop(struct map_session_data *sd, struct item *item)
  8265. {
  8266. if( item && ((item->expire_time || (item->bound && !pc_can_give_bounded_items(sd))) || (itemdb_ishatched_egg(item))) )
  8267. return false;
  8268. if( !pc_can_give_items(sd) || sd->sc.cant.drop) //check if this GM level can drop items
  8269. return false;
  8270. return (itemdb_isdropable(item, pc_get_group_level(sd)));
  8271. }
  8272. /**
  8273. * Determines whether a player can attack based on status changes
  8274. * Why not use status_check_skilluse?
  8275. * "src MAY be null to indicate we shouldn't check it, this is a ground-based skill attack."
  8276. * Even ground-based attacks should be blocked by these statuses
  8277. * Called from unit_attack and unit_attack_timer_sub
  8278. * @retval true Can attack
  8279. **/
  8280. bool pc_can_attack( struct map_session_data *sd, int target_id ) {
  8281. nullpo_retr(false, sd);
  8282. if( pc_is90overweight(sd) || pc_isridingwug(sd) )
  8283. return false;
  8284. if (sd->state.block_action & PCBLOCK_ATTACK)
  8285. return false;
  8286. if( sd->sc.data[SC_BASILICA] ||
  8287. sd->sc.data[SC__SHADOWFORM] ||
  8288. sd->sc.data[SC_CURSEDCIRCLE_ATKER] ||
  8289. sd->sc.data[SC_CURSEDCIRCLE_TARGET] ||
  8290. sd->sc.data[SC_CRYSTALIZE] ||
  8291. sd->sc.data[SC_ALL_RIDING] || // The client doesn't let you, this is to make cheat-safe
  8292. sd->sc.data[SC_TRICKDEAD] ||
  8293. (sd->sc.data[SC_VOICEOFSIREN] && sd->sc.data[SC_VOICEOFSIREN]->val2 == target_id) ||
  8294. sd->sc.data[SC_BLADESTOP] ||
  8295. sd->sc.data[SC_DEEPSLEEP] ||
  8296. (sd->sc.data[SC_GRAVITATION] && sd->sc.data[SC_GRAVITATION]->val3 == BCT_SELF) ||
  8297. sd->sc.data[SC_KINGS_GRACE] )
  8298. return false;
  8299. return true;
  8300. }
  8301. /*==========================================
  8302. * Read '@type' variables (temporary numeric char reg)
  8303. *------------------------------------------*/
  8304. int64 pc_readreg(struct map_session_data* sd, int64 reg)
  8305. {
  8306. return i64db_i64get(sd->regs.vars, reg);
  8307. }
  8308. /*==========================================
  8309. * Set '@type' variables (temporary numeric char reg)
  8310. *------------------------------------------*/
  8311. bool pc_setreg(struct map_session_data* sd, int64 reg, int64 val)
  8312. {
  8313. uint32 index = script_getvaridx(reg);
  8314. nullpo_retr(false, sd);
  8315. if( val ) {
  8316. i64db_i64put(sd->regs.vars, reg, val);
  8317. if( index )
  8318. script_array_update(&sd->regs, reg, false);
  8319. } else {
  8320. i64db_remove(sd->regs.vars, reg);
  8321. if( index )
  8322. script_array_update(&sd->regs, reg, true);
  8323. }
  8324. return true;
  8325. }
  8326. /*==========================================
  8327. * Read '@type$' variables (temporary string char reg)
  8328. *------------------------------------------*/
  8329. char* pc_readregstr(struct map_session_data* sd, int64 reg)
  8330. {
  8331. struct script_reg_str *p = NULL;
  8332. p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg);
  8333. return p ? p->value : NULL;
  8334. }
  8335. /*==========================================
  8336. * Set '@type$' variables (temporary string char reg)
  8337. *------------------------------------------*/
  8338. bool pc_setregstr(struct map_session_data* sd, int64 reg, const char* str)
  8339. {
  8340. struct script_reg_str *p = NULL;
  8341. unsigned int index = script_getvaridx(reg);
  8342. DBData prev;
  8343. nullpo_retr(false, sd);
  8344. if( str[0] ) {
  8345. p = ers_alloc(str_reg_ers, struct script_reg_str);
  8346. p->value = aStrdup(str);
  8347. p->flag.type = 1;
  8348. if (sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev)) {
  8349. p = (struct script_reg_str *)db_data2ptr(&prev);
  8350. if( p->value )
  8351. aFree(p->value);
  8352. ers_free(str_reg_ers, p);
  8353. } else {
  8354. if( index )
  8355. script_array_update(&sd->regs, reg, false);
  8356. }
  8357. } else {
  8358. if (sd->regs.vars->remove(sd->regs.vars, db_i642key(reg), &prev)) {
  8359. p = (struct script_reg_str *)db_data2ptr(&prev);
  8360. if( p->value )
  8361. aFree(p->value);
  8362. ers_free(str_reg_ers, p);
  8363. if( index )
  8364. script_array_update(&sd->regs, reg, true);
  8365. }
  8366. }
  8367. return true;
  8368. }
  8369. /**
  8370. * Serves the following variable types:
  8371. * - 'type' (permanent numeric char reg)
  8372. * - '#type' (permanent numeric account reg)
  8373. * - '##type' (permanent numeric account reg2)
  8374. **/
  8375. int64 pc_readregistry(struct map_session_data *sd, int64 reg)
  8376. {
  8377. struct script_reg_num *p = NULL;
  8378. if (!sd->vars_ok) {
  8379. ShowError("pc_readregistry: Trying to read reg %s before it's been loaded!\n", get_str(script_getvarid(reg)));
  8380. //This really shouldn't happen, so it's possible the data was lost somewhere, we should request it again.
  8381. //intif->request_registry(sd,type==3?4:type);
  8382. set_eof(sd->fd);
  8383. return 0;
  8384. }
  8385. p = (struct script_reg_num *)i64db_get(sd->regs.vars, reg);
  8386. return p ? p->value : 0;
  8387. }
  8388. /**
  8389. * Serves the following variable types:
  8390. * - 'type$' (permanent str char reg)
  8391. * - '#type$' (permanent str account reg)
  8392. * - '##type$' (permanent str account reg2)
  8393. **/
  8394. char* pc_readregistry_str(struct map_session_data *sd, int64 reg)
  8395. {
  8396. struct script_reg_str *p = NULL;
  8397. if (!sd->vars_ok) {
  8398. ShowError("pc_readregistry_str: Trying to read reg %s before it's been loaded!\n", get_str(script_getvarid(reg)));
  8399. //This really shouldn't happen, so it's possible the data was lost somewhere, we should request it again.
  8400. //intif->request_registry(sd,type==3?4:type);
  8401. set_eof(sd->fd);
  8402. return NULL;
  8403. }
  8404. p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg);
  8405. return p ? p->value : NULL;
  8406. }
  8407. /**
  8408. * Serves the following variable types:
  8409. * - 'type' (permanent numeric char reg)
  8410. * - '#type' (permanent numeric account reg)
  8411. * - '##type' (permanent numeric account reg2)
  8412. **/
  8413. int pc_setregistry(struct map_session_data *sd, int64 reg, int64 val)
  8414. {
  8415. struct script_reg_num *p = NULL;
  8416. const char *regname = get_str(script_getvarid(reg));
  8417. uint32 index = script_getvaridx(reg);
  8418. if ( !reg_load && !sd->vars_ok ) {
  8419. ShowError("pc_setregistry : refusing to set %s until vars are received.\n", regname);
  8420. return 0;
  8421. }
  8422. if ((p = (struct script_reg_num *)i64db_get(sd->regs.vars, reg))) {
  8423. if( val ) {
  8424. if( !p->value && index ) /* its a entry that was deleted, so we reset array */
  8425. script_array_update(&sd->regs, reg, false);
  8426. p->value = val;
  8427. } else {
  8428. p->value = 0;
  8429. if( index )
  8430. script_array_update(&sd->regs, reg, true);
  8431. }
  8432. if (!reg_load)
  8433. p->flag.update = 1;/* either way, it will require either delete or replace */
  8434. } else if( val ) {
  8435. DBData prev;
  8436. if( index )
  8437. script_array_update(&sd->regs, reg, false);
  8438. p = ers_alloc(num_reg_ers, struct script_reg_num);
  8439. p->value = val;
  8440. if (!reg_load)
  8441. p->flag.update = 1;
  8442. if (sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev)) {
  8443. p = (struct script_reg_num *)db_data2ptr(&prev);
  8444. ers_free(num_reg_ers, p);
  8445. }
  8446. }
  8447. if (!reg_load && p)
  8448. sd->vars_dirty = true;
  8449. return 1;
  8450. }
  8451. /**
  8452. * Serves the following variable types:
  8453. * - 'type$' (permanent str char reg)
  8454. * - '#type$' (permanent str account reg)
  8455. * - '##type$' (permanent str account reg2)
  8456. **/
  8457. int pc_setregistry_str(struct map_session_data *sd, int64 reg, const char *val)
  8458. {
  8459. struct script_reg_str *p = NULL;
  8460. const char *regname = get_str(script_getvarid(reg));
  8461. unsigned int index = script_getvaridx(reg);
  8462. size_t vlen = 0;
  8463. if (!reg_load && !sd->vars_ok) {
  8464. ShowError("pc_setregistry_str : refusing to set %s until vars are received.\n", regname);
  8465. return 0;
  8466. }
  8467. if ( !script_check_RegistryVariableLength(1, val, &vlen ) )
  8468. {
  8469. 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);
  8470. return 0;
  8471. }
  8472. if( (p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg) ) ) {
  8473. if( val[0] ) {
  8474. if( p->value )
  8475. aFree(p->value);
  8476. else if ( index ) // an entry that was deleted, so we reset
  8477. script_array_update(&sd->regs, reg, false);
  8478. p->value = aStrdup(val);
  8479. } else {
  8480. if (p->value)
  8481. aFree(p->value);
  8482. p->value = NULL;
  8483. if( index )
  8484. script_array_update(&sd->regs, reg, true);
  8485. }
  8486. if( !reg_load )
  8487. p->flag.update = 1; // either way, it will require either delete or replace
  8488. } else if( val[0] ) {
  8489. DBData prev;
  8490. if( index )
  8491. script_array_update(&sd->regs, reg, false);
  8492. p = ers_alloc(str_reg_ers, struct script_reg_str);
  8493. p->value = aStrdup(val);
  8494. if( !reg_load )
  8495. p->flag.update = 1;
  8496. p->flag.type = 1;
  8497. if( sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev) ) {
  8498. p = (struct script_reg_str *)db_data2ptr(&prev);
  8499. if( p->value )
  8500. aFree(p->value);
  8501. ers_free(str_reg_ers, p);
  8502. }
  8503. }
  8504. if( !reg_load && p )
  8505. sd->vars_dirty = true;
  8506. return 1;
  8507. }
  8508. /**
  8509. * Set value of player variable
  8510. * @param sd Player
  8511. * @param reg Variable name
  8512. * @param value
  8513. * @return True if success, false if failed.
  8514. **/
  8515. bool pc_setreg2(struct map_session_data *sd, const char *reg, int64 val) {
  8516. char prefix = reg[0];
  8517. nullpo_retr(false, sd);
  8518. if (reg[strlen(reg)-1] == '$') {
  8519. ShowError("pc_setreg2: Invalid variable scope '%s'\n", reg);
  8520. return false;
  8521. }
  8522. val = cap_value(val, INT_MIN, INT_MAX);
  8523. switch (prefix) {
  8524. case '.':
  8525. case '\'':
  8526. case '$':
  8527. ShowError("pc_setreg2: Invalid variable scope '%s'\n", reg);
  8528. return false;
  8529. case '@':
  8530. return pc_setreg(sd, add_str(reg), val);
  8531. case '#':
  8532. return (reg[1] == '#') ? pc_setaccountreg2(sd, add_str(reg), val) > 0 : pc_setaccountreg(sd, add_str(reg), val) > 0;
  8533. default:
  8534. return pc_setglobalreg(sd, add_str(reg), val) > 0;
  8535. }
  8536. return false;
  8537. }
  8538. /**
  8539. * Get value of player variable
  8540. * @param sd Player
  8541. * @param reg Variable name
  8542. * @return Variable value or 0 if failed.
  8543. **/
  8544. int64 pc_readreg2(struct map_session_data *sd, const char *reg) {
  8545. char prefix = reg[0];
  8546. nullpo_ret(sd);
  8547. if (reg[strlen(reg)-1] == '$') {
  8548. ShowError("pc_readreg2: Invalid variable scope '%s'\n", reg);
  8549. return 0;
  8550. }
  8551. switch (prefix) {
  8552. case '.':
  8553. case '\'':
  8554. case '$':
  8555. ShowError("pc_readreg2: Invalid variable scope '%s'\n", reg);
  8556. return 0;
  8557. case '@':
  8558. return pc_readreg(sd, add_str(reg));
  8559. case '#':
  8560. return (reg[1] == '#') ? pc_readaccountreg2(sd, add_str(reg)) : pc_readaccountreg(sd, add_str(reg));
  8561. default:
  8562. return pc_readglobalreg(sd, add_str(reg));
  8563. }
  8564. return 0;
  8565. }
  8566. /*==========================================
  8567. * Exec eventtimer for player sd (retrieved from map_session (id))
  8568. *------------------------------------------*/
  8569. static TIMER_FUNC(pc_eventtimer){
  8570. struct map_session_data *sd=map_id2sd(id);
  8571. char *p = (char *)data;
  8572. int i;
  8573. if(sd==NULL)
  8574. return 0;
  8575. ARR_FIND( 0, MAX_EVENTTIMER, i, sd->eventtimer[i] == tid );
  8576. if( i < MAX_EVENTTIMER )
  8577. {
  8578. sd->eventtimer[i] = INVALID_TIMER;
  8579. sd->eventcount--;
  8580. npc_event(sd,p,0);
  8581. }
  8582. else
  8583. ShowError("pc_eventtimer: no such event timer\n");
  8584. if (p) aFree(p);
  8585. return 0;
  8586. }
  8587. /*==========================================
  8588. * Add eventtimer for player sd ?
  8589. *------------------------------------------*/
  8590. bool pc_addeventtimer(struct map_session_data *sd,int tick,const char *name)
  8591. {
  8592. int i;
  8593. nullpo_retr(false,sd);
  8594. ARR_FIND( 0, MAX_EVENTTIMER, i, sd->eventtimer[i] == INVALID_TIMER );
  8595. if( i == MAX_EVENTTIMER )
  8596. return false;
  8597. sd->eventtimer[i] = add_timer(gettick()+tick, pc_eventtimer, sd->bl.id, (intptr_t)aStrdup(name));
  8598. sd->eventcount++;
  8599. return true;
  8600. }
  8601. /*==========================================
  8602. * Del eventtimer for player sd ?
  8603. *------------------------------------------*/
  8604. bool pc_deleventtimer(struct map_session_data *sd,const char *name)
  8605. {
  8606. char* p = NULL;
  8607. int i;
  8608. nullpo_retr(false,sd);
  8609. if (sd->eventcount == 0)
  8610. return false;
  8611. // find the named event timer
  8612. ARR_FIND( 0, MAX_EVENTTIMER, i,
  8613. sd->eventtimer[i] != INVALID_TIMER &&
  8614. (p = (char *)(get_timer(sd->eventtimer[i])->data)) != NULL &&
  8615. strcmp(p, name) == 0
  8616. );
  8617. if( i == MAX_EVENTTIMER )
  8618. return false; // not found
  8619. delete_timer(sd->eventtimer[i],pc_eventtimer);
  8620. sd->eventtimer[i] = INVALID_TIMER;
  8621. if(sd->eventcount > 0)
  8622. sd->eventcount--;
  8623. aFree(p);
  8624. return true;
  8625. }
  8626. /*==========================================
  8627. * Update eventtimer count for player sd
  8628. *------------------------------------------*/
  8629. void pc_addeventtimercount(struct map_session_data *sd,const char *name,int tick)
  8630. {
  8631. int i;
  8632. nullpo_retv(sd);
  8633. for(i=0;i<MAX_EVENTTIMER;i++)
  8634. if( sd->eventtimer[i] != INVALID_TIMER && strcmp(
  8635. (char *)(get_timer(sd->eventtimer[i])->data), name)==0 ){
  8636. addt_tickimer(sd->eventtimer[i],tick);
  8637. break;
  8638. }
  8639. }
  8640. /*==========================================
  8641. * Remove all eventtimer for player sd
  8642. *------------------------------------------*/
  8643. void pc_cleareventtimer(struct map_session_data *sd)
  8644. {
  8645. int i;
  8646. nullpo_retv(sd);
  8647. if (sd->eventcount == 0)
  8648. return;
  8649. for(i=0;i<MAX_EVENTTIMER;i++){
  8650. if( sd->eventtimer[i] != INVALID_TIMER ){
  8651. char *p = (char *)(get_timer(sd->eventtimer[i])->data);
  8652. delete_timer(sd->eventtimer[i],pc_eventtimer);
  8653. sd->eventtimer[i] = INVALID_TIMER;
  8654. if(sd->eventcount > 0) //avoid looping to max val
  8655. sd->eventcount--;
  8656. if (p) aFree(p);
  8657. }
  8658. }
  8659. }
  8660. /**
  8661. * Called when an item with combo is worn
  8662. * @param *sd
  8663. * @param *data struct item_data
  8664. * @return success numbers of succeed combo
  8665. */
  8666. static int pc_checkcombo(struct map_session_data *sd, struct item_data *data) {
  8667. uint16 i;
  8668. int success = 0;
  8669. for( i = 0; i < data->combos_count; i++ ) {
  8670. struct itemchk {
  8671. int idx;
  8672. unsigned short nameid;
  8673. short card[MAX_SLOTS];
  8674. } *combo_idx;
  8675. int idx, j;
  8676. int nb_itemCombo;
  8677. unsigned int pos = 0;
  8678. /* ensure this isn't a duplicate combo */
  8679. if( sd->combos.bonus != NULL ) {
  8680. int x;
  8681. ARR_FIND( 0, sd->combos.count, x, sd->combos.id[x] == data->combos[i]->id );
  8682. /* found a match, skip this combo */
  8683. if( x < sd->combos.count )
  8684. continue;
  8685. }
  8686. nb_itemCombo = data->combos[i]->count;
  8687. if(nb_itemCombo<2) //a combo with less then 2 item ?? how that possible
  8688. continue;
  8689. CREATE(combo_idx,struct itemchk,nb_itemCombo);
  8690. for(j=0; j < nb_itemCombo; j++){
  8691. combo_idx[j].idx=-1;
  8692. combo_idx[j].nameid=-1;
  8693. memset(combo_idx[j].card,-1,MAX_SLOTS);
  8694. }
  8695. for( j = 0; j < nb_itemCombo; j++ ) {
  8696. uint16 id = data->combos[i]->nameid[j], k;
  8697. bool found = false;
  8698. for( k = 0; k < EQI_MAX; k++ ) {
  8699. short index = sd->equip_index[k];
  8700. if( index < 0 )
  8701. continue;
  8702. if( pc_is_same_equip_index((enum equip_index)k, sd->equip_index, index) )
  8703. continue;
  8704. if (!sd->inventory_data[index] )
  8705. continue;
  8706. if ( itemdb_type(id) != IT_CARD ) {
  8707. if ( sd->inventory_data[index]->nameid != id )
  8708. continue;
  8709. if(j>0){ //check if this item not already used
  8710. bool do_continue = false; //used to continue that specific loop with some check that also use some loop
  8711. uint8 z;
  8712. for (z = 0; z < nb_itemCombo-1; z++)
  8713. if(combo_idx[z].idx == index && combo_idx[z].nameid == id) //we already have that index recorded
  8714. do_continue=true;
  8715. if(do_continue)
  8716. continue;
  8717. }
  8718. combo_idx[j].nameid = id;
  8719. combo_idx[j].idx = index;
  8720. pos |= sd->inventory.u.items_inventory[index].equip;
  8721. found = true;
  8722. break;
  8723. } else { //Cards and enchants
  8724. uint16 z;
  8725. if ( itemdb_isspecial(sd->inventory.u.items_inventory[index].card[0]) )
  8726. continue;
  8727. for (z = 0; z < MAX_SLOTS; z++) {
  8728. bool do_continue=false;
  8729. if (sd->inventory.u.items_inventory[index].card[z] != id)
  8730. continue;
  8731. if(j>0){
  8732. int c1, c2;
  8733. for (c1 = 0; c1 < nb_itemCombo-1; c1++){
  8734. if(combo_idx[c1].idx == index && combo_idx[c1].nameid == id){
  8735. for (c2 = 0; c2 < MAX_SLOTS; c2++){
  8736. if(combo_idx[c1].card[c2] == id){ //we already have that card recorded (at this same idx)
  8737. do_continue = true;
  8738. break;
  8739. }
  8740. }
  8741. }
  8742. }
  8743. }
  8744. if(do_continue)
  8745. continue;
  8746. combo_idx[j].nameid = id;
  8747. combo_idx[j].idx = index;
  8748. combo_idx[j].card[z] = id;
  8749. pos |= sd->inventory.u.items_inventory[index].equip;
  8750. found = true;
  8751. break;
  8752. }
  8753. }
  8754. }
  8755. if( !found )
  8756. break;/* we haven't found all the ids for this combo, so we can return */
  8757. }
  8758. aFree(combo_idx);
  8759. /* means we broke out of the count loop w/o finding all ids, we can move to the next combo */
  8760. if( j < nb_itemCombo )
  8761. continue;
  8762. /* we got here, means all items in the combo are matching */
  8763. idx = sd->combos.count;
  8764. if( sd->combos.bonus == NULL ) {
  8765. CREATE(sd->combos.bonus, struct script_code *, 1);
  8766. CREATE(sd->combos.id, unsigned short, 1);
  8767. CREATE(sd->combos.pos, unsigned int, 1);
  8768. sd->combos.count = 1;
  8769. } else {
  8770. RECREATE(sd->combos.bonus, struct script_code *, ++sd->combos.count);
  8771. RECREATE(sd->combos.id, unsigned short, sd->combos.count);
  8772. RECREATE(sd->combos.pos, unsigned int, sd->combos.count);
  8773. }
  8774. /* we simply copy the pointer */
  8775. sd->combos.bonus[idx] = data->combos[i]->script;
  8776. /* save this combo's id */
  8777. sd->combos.id[idx] = data->combos[i]->id;
  8778. /* save pos of combo*/
  8779. sd->combos.pos[idx] = pos;
  8780. success++;
  8781. }
  8782. return success;
  8783. }
  8784. /**
  8785. * Called when an item with combo is removed
  8786. * @param *sd
  8787. * @param *data struct item_data
  8788. * @return retval numbers of removed combo
  8789. */
  8790. static int pc_removecombo(struct map_session_data *sd, struct item_data *data ) {
  8791. int i, retval = 0;
  8792. if( sd->combos.bonus == NULL )
  8793. return 0;/* nothing to do here, player has no combos */
  8794. for( i = 0; i < data->combos_count; i++ ) {
  8795. /* check if this combo exists in this user */
  8796. int x = 0, cursor = 0, j;
  8797. ARR_FIND( 0, sd->combos.count, x, sd->combos.id[x] == data->combos[i]->id );
  8798. /* no match, skip this combo */
  8799. if(x >= sd->combos.count)
  8800. continue;
  8801. sd->combos.bonus[x] = NULL;
  8802. sd->combos.id[x] = 0;
  8803. sd->combos.pos[x] = 0;
  8804. retval++;
  8805. /* check if combo requirements still fit */
  8806. if( pc_checkcombo( sd, data ) )
  8807. continue;
  8808. /* move next value to empty slot */
  8809. for( j = 0, cursor = 0; j < sd->combos.count; j++ ) {
  8810. if( sd->combos.bonus[j] == NULL )
  8811. continue;
  8812. if( cursor != j ) {
  8813. sd->combos.bonus[cursor] = sd->combos.bonus[j];
  8814. sd->combos.id[cursor] = sd->combos.id[j];
  8815. sd->combos.pos[cursor] = sd->combos.pos[j];
  8816. }
  8817. cursor++;
  8818. }
  8819. /* it's empty, we can clear all the memory */
  8820. if( (sd->combos.count = cursor) == 0 ) {
  8821. aFree(sd->combos.bonus);
  8822. aFree(sd->combos.id);
  8823. aFree(sd->combos.pos);
  8824. sd->combos.bonus = NULL;
  8825. sd->combos.id = NULL;
  8826. sd->combos.pos = NULL;
  8827. return retval; /* we also can return at this point for we have no more combos to check */
  8828. }
  8829. }
  8830. return retval;
  8831. }
  8832. /**
  8833. * Load combo data(s) of player
  8834. * @param *sd
  8835. * @return ret numbers of succeed combo
  8836. */
  8837. int pc_load_combo(struct map_session_data *sd) {
  8838. int i, ret = 0;
  8839. for( i = 0; i < EQI_MAX; i++ ) {
  8840. struct item_data *id = NULL;
  8841. short idx = sd->equip_index[i];
  8842. if( idx < 0 || !(id = sd->inventory_data[idx] ) )
  8843. continue;
  8844. if( id->combos_count )
  8845. ret += pc_checkcombo(sd,id);
  8846. if(!itemdb_isspecial(sd->inventory.u.items_inventory[idx].card[0])) {
  8847. struct item_data *data;
  8848. int j;
  8849. for( j = 0; j < MAX_SLOTS; j++ ) {
  8850. if (!sd->inventory.u.items_inventory[idx].card[j])
  8851. continue;
  8852. if ( ( data = itemdb_exists(sd->inventory.u.items_inventory[idx].card[j]) ) != NULL ) {
  8853. if( data->combos_count )
  8854. ret += pc_checkcombo(sd,data);
  8855. }
  8856. }
  8857. }
  8858. }
  8859. return ret;
  8860. }
  8861. /*==========================================
  8862. * Equip item on player sd at req_pos from inventory index n
  8863. * return: false - fail; true - success
  8864. *------------------------------------------*/
  8865. bool pc_equipitem(struct map_session_data *sd,short n,int req_pos,bool equipswitch)
  8866. {
  8867. int i, pos, flag = 0, iflag;
  8868. struct item_data *id;
  8869. uint8 res = ITEM_EQUIP_ACK_OK;
  8870. short* equip_index;
  8871. nullpo_retr(false,sd);
  8872. if( n < 0 || n >= MAX_INVENTORY ) {
  8873. if( equipswitch ){
  8874. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  8875. }else{
  8876. clif_equipitemack(sd,0,0,ITEM_EQUIP_ACK_FAIL);
  8877. }
  8878. return false;
  8879. }
  8880. if( DIFF_TICK(sd->canequip_tick,gettick()) > 0 ) {
  8881. if( equipswitch ){
  8882. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  8883. }else{
  8884. clif_equipitemack(sd,n,0,ITEM_EQUIP_ACK_FAIL);
  8885. }
  8886. return false;
  8887. }
  8888. if (!(id = sd->inventory_data[n]))
  8889. return false;
  8890. pos = pc_equippoint(sd,n); //With a few exceptions, item should go in all specified slots.
  8891. if(battle_config.battle_log && !equipswitch)
  8892. ShowInfo("equip %hu (%d) %x:%x\n",sd->inventory.u.items_inventory[n].nameid,n,id?id->equip:0,req_pos);
  8893. if((res = pc_isequip(sd,n))) {
  8894. if( equipswitch ){
  8895. clif_equipswitch_add( sd, n, req_pos, res );
  8896. }else{
  8897. clif_equipitemack(sd,n,0,res); // fail
  8898. }
  8899. return false;
  8900. }
  8901. if( equipswitch && id->type == IT_AMMO ){
  8902. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  8903. return false;
  8904. }
  8905. if (!(pos&req_pos) || sd->inventory.u.items_inventory[n].equip != 0 || sd->inventory.u.items_inventory[n].attribute==1 ) { // [Valaris]
  8906. if( equipswitch ){
  8907. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  8908. }else{
  8909. clif_equipitemack(sd,n,0,ITEM_EQUIP_ACK_FAIL); // fail
  8910. }
  8911. return false;
  8912. }
  8913. if( sd->sc.count && (sd->sc.data[SC_BERSERK] || sd->sc.data[SC_SATURDAYNIGHTFEVER] ||
  8914. sd->sc.data[SC_KYOUGAKU] || (sd->sc.data[SC_PYROCLASTIC] && sd->inventory_data[n]->type == IT_WEAPON)) ) {
  8915. if( equipswitch ){
  8916. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  8917. }else{
  8918. clif_equipitemack(sd,n,0,ITEM_EQUIP_ACK_FAIL); //Fail
  8919. }
  8920. return false;
  8921. }
  8922. equip_index = equipswitch ? sd->equip_switch_index : sd->equip_index;
  8923. if ( !equipswitch && id->flag.bindOnEquip && !sd->inventory.u.items_inventory[n].bound) {
  8924. sd->inventory.u.items_inventory[n].bound = (char)battle_config.default_bind_on_equip;
  8925. clif_notify_bindOnEquip(sd,n);
  8926. }
  8927. if(pos == EQP_ACC) { //Accessories should only go in one of the two.
  8928. pos = req_pos&EQP_ACC;
  8929. if (pos == EQP_ACC) //User specified both slots.
  8930. pos = equip_index[EQI_ACC_R] >= 0 ? EQP_ACC_L : EQP_ACC_R;
  8931. for (i = 0; i < sd->inventory_data[n]->slot; i++) { // Accessories that have cards that force equip location
  8932. if (!sd->inventory.u.items_inventory[n].card[i])
  8933. continue;
  8934. struct item_data *card_data = itemdb_exists(sd->inventory.u.items_inventory[n].card[i]);
  8935. if (card_data) {
  8936. int card_pos = card_data->equip;
  8937. if (card_pos == EQP_ACC_L || card_pos == EQP_ACC_R) {
  8938. pos = card_pos; // Use the card's equip position
  8939. break;
  8940. }
  8941. }
  8942. }
  8943. } else if(pos == EQP_ARMS && id->equip == EQP_HAND_R) { //Dual wield capable weapon.
  8944. pos = (req_pos&EQP_ARMS);
  8945. if (pos == EQP_ARMS) //User specified both slots, pick one for them.
  8946. pos = equip_index[EQI_HAND_R] >= 0 ? EQP_HAND_L : EQP_HAND_R;
  8947. } else if(pos == EQP_SHADOW_ACC) { // Shadow System
  8948. pos = req_pos&EQP_SHADOW_ACC;
  8949. if (pos == EQP_SHADOW_ACC)
  8950. pos = equip_index[EQI_SHADOW_ACC_L] >= 0 ? EQP_SHADOW_ACC_R : EQP_SHADOW_ACC_L;
  8951. } else if(pos == EQP_SHADOW_ARMS && id->equip == EQP_SHADOW_WEAPON) {
  8952. pos = (req_pos&EQP_SHADOW_ARMS);
  8953. if( pos == EQP_SHADOW_ARMS )
  8954. pos = (equip_index[EQI_SHADOW_WEAPON] >= 0 ? EQP_SHADOW_SHIELD : EQP_SHADOW_WEAPON);
  8955. }
  8956. if (pos&EQP_HAND_R && battle_config.use_weapon_skill_range&BL_PC) {
  8957. //Update skill-block range database when weapon range changes. [Skotlex]
  8958. i = equip_index[EQI_HAND_R];
  8959. if (i < 0 || !sd->inventory_data[i]) //No data, or no weapon equipped
  8960. flag = 1;
  8961. else
  8962. flag = id->range != sd->inventory_data[i]->range;
  8963. }
  8964. if( equipswitch ){
  8965. for( i = 0; i < EQI_MAX; i++ ){
  8966. if( pos&equip_bitmask[i] ){
  8967. // If there was already an item assigned to this slot
  8968. if( sd->equip_switch_index[i] >= 0 ){
  8969. pc_equipswitch_remove( sd, sd->equip_switch_index[i] );
  8970. }
  8971. // Assign the new index to it
  8972. sd->equip_switch_index[i] = n;
  8973. }
  8974. }
  8975. sd->inventory.u.items_inventory[n].equipSwitch = pos;
  8976. clif_equipswitch_add( sd, n, pos, ITEM_EQUIP_ACK_OK );
  8977. return true;
  8978. }else{
  8979. for(i=0;i<EQI_MAX;i++) {
  8980. if(pos & equip_bitmask[i]) {
  8981. if(sd->equip_index[i] >= 0) //Slot taken, remove item from there.
  8982. pc_unequipitem(sd,sd->equip_index[i],2 | 4);
  8983. sd->equip_index[i] = n;
  8984. }
  8985. }
  8986. pc_equipswitch_remove(sd, n);
  8987. if(pos==EQP_AMMO) {
  8988. clif_arrowequip(sd,n);
  8989. clif_arrow_fail(sd,3);
  8990. }
  8991. else
  8992. clif_equipitemack(sd,n,pos,ITEM_EQUIP_ACK_OK);
  8993. sd->inventory.u.items_inventory[n].equip = pos;
  8994. }
  8995. if(pos & EQP_HAND_R) {
  8996. if(id)
  8997. sd->weapontype1 = id->look;
  8998. else
  8999. sd->weapontype1 = 0;
  9000. pc_calcweapontype(sd);
  9001. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  9002. }
  9003. if(pos & EQP_HAND_L) {
  9004. if(id) {
  9005. if(id->type == IT_WEAPON) {
  9006. sd->status.shield = 0;
  9007. sd->weapontype2 = id->look;
  9008. }
  9009. else
  9010. if(id->type == IT_ARMOR) {
  9011. sd->status.shield = id->look;
  9012. sd->weapontype2 = 0;
  9013. }
  9014. }
  9015. else
  9016. sd->status.shield = sd->weapontype2 = 0;
  9017. pc_calcweapontype(sd);
  9018. clif_changelook(&sd->bl,LOOK_SHIELD,sd->status.shield);
  9019. }
  9020. if(pos & EQP_SHOES)
  9021. clif_changelook(&sd->bl,LOOK_SHOES,0);
  9022. if (battle_config.ammo_unequip && (pos&EQP_ARMS) && id->type == IT_WEAPON) {
  9023. short idx = sd->equip_index[EQI_AMMO];
  9024. if (idx >= 0) {
  9025. switch (sd->inventory_data[idx]->look) {
  9026. case AMMO_ARROW:
  9027. if (id->look != W_BOW && id->look != W_MUSICAL && id->look != W_WHIP)
  9028. pc_unequipitem(sd, idx, 2 | 4);
  9029. break;
  9030. case AMMO_BULLET:
  9031. case AMMO_SHELL:
  9032. if (id->look != W_REVOLVER && id->look != W_RIFLE && id->look != W_GATLING && id->look != W_SHOTGUN
  9033. #ifdef RENEWAL
  9034. && id->look != W_GRENADE
  9035. #endif
  9036. )
  9037. pc_unequipitem(sd, idx, 2 | 4);
  9038. break;
  9039. #ifndef RENEWAL
  9040. case AMMO_GRENADE:
  9041. if (id->look != W_GRENADE)
  9042. pc_unequipitem(sd, idx, 2 | 4);
  9043. break;
  9044. #endif
  9045. }
  9046. }
  9047. }
  9048. pc_set_costume_view(sd);
  9049. pc_checkallowskill(sd); //Check if status changes should be halted.
  9050. iflag = sd->npc_item_flag;
  9051. /* check for combos (MUST be before status_calc_pc) */
  9052. if( id->combos_count )
  9053. pc_checkcombo(sd,id);
  9054. if(itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  9055. ; //No cards
  9056. else {
  9057. for( i = 0; i < MAX_SLOTS; i++ ) {
  9058. struct item_data *data;
  9059. if (!sd->inventory.u.items_inventory[n].card[i])
  9060. continue;
  9061. if ( ( data = itemdb_exists(sd->inventory.u.items_inventory[n].card[i]) ) != NULL ) {
  9062. if( data->combos_count )
  9063. pc_checkcombo(sd,data);
  9064. }
  9065. }
  9066. }
  9067. status_calc_pc(sd,SCO_NONE);
  9068. if (flag) //Update skill data
  9069. clif_skillinfoblock(sd);
  9070. //OnEquip script [Skotlex]
  9071. if (id) {
  9072. //only run the script if item isn't restricted
  9073. if (id->equip_script && (pc_has_permission(sd,PC_PERM_USE_ALL_EQUIPMENT) || !itemdb_isNoEquip(id,sd->bl.m)))
  9074. run_script(id->equip_script,0,sd->bl.id,fake_nd->bl.id);
  9075. if(itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  9076. ; //No cards
  9077. else {
  9078. for( i = 0; i < MAX_SLOTS; i++ ) {
  9079. struct item_data *data;
  9080. if (!sd->inventory.u.items_inventory[n].card[i])
  9081. continue;
  9082. if ( ( data = itemdb_exists(sd->inventory.u.items_inventory[n].card[i]) ) != NULL ) {
  9083. if (data->equip_script && (pc_has_permission(sd,PC_PERM_USE_ALL_EQUIPMENT) || !itemdb_isNoEquip(data,sd->bl.m)))
  9084. run_script(data->equip_script,0,sd->bl.id,fake_nd->bl.id);
  9085. }
  9086. }
  9087. }
  9088. }
  9089. sd->npc_item_flag = iflag;
  9090. return true;
  9091. }
  9092. /**
  9093. * Recalculate player status on unequip
  9094. * @param sd: Player data
  9095. * @param n: Item inventory index
  9096. * @param flag: Whether to recalculate a player's status or not
  9097. * @return True on success or false on failure
  9098. */
  9099. static void pc_unequipitem_sub(struct map_session_data *sd, int n, int flag) {
  9100. int i, iflag;
  9101. bool status_calc = false;
  9102. if (sd->state.autobonus&sd->inventory.u.items_inventory[n].equip)
  9103. sd->state.autobonus &= ~sd->inventory.u.items_inventory[n].equip; //Check for activated autobonus [Inkfish]
  9104. sd->inventory.u.items_inventory[n].equip = 0;
  9105. pc_checkallowskill(sd);
  9106. iflag = sd->npc_item_flag;
  9107. /* check for combos (MUST be before status_calc_pc) */
  9108. if (sd->inventory_data[n]) {
  9109. if (sd->inventory_data[n]->combos_count) {
  9110. if (pc_removecombo(sd, sd->inventory_data[n]))
  9111. status_calc = true;
  9112. }
  9113. if (itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  9114. ; //No cards
  9115. else {
  9116. for (i = 0; i < MAX_SLOTS; i++) {
  9117. struct item_data *data;
  9118. if (!sd->inventory.u.items_inventory[n].card[i])
  9119. continue;
  9120. if ((data = itemdb_exists(sd->inventory.u.items_inventory[n].card[i])) != NULL) {
  9121. if (data->combos_count) {
  9122. if (pc_removecombo(sd, data))
  9123. status_calc = true;
  9124. }
  9125. }
  9126. }
  9127. }
  9128. }
  9129. if (flag & 1 || status_calc) {
  9130. pc_checkallowskill(sd);
  9131. status_calc_pc(sd, SCO_NONE);
  9132. }
  9133. if (sd->sc.data[SC_SIGNUMCRUCIS] && !battle_check_undead(sd->battle_status.race, sd->battle_status.def_ele))
  9134. status_change_end(&sd->bl, SC_SIGNUMCRUCIS, INVALID_TIMER);
  9135. //OnUnEquip script [Skotlex]
  9136. if (sd->inventory_data[n]) {
  9137. if (sd->inventory_data[n]->unequip_script)
  9138. run_script(sd->inventory_data[n]->unequip_script, 0, sd->bl.id, fake_nd->bl.id);
  9139. if (itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  9140. ; //No cards
  9141. else {
  9142. for (i = 0; i < MAX_SLOTS; i++) {
  9143. struct item_data *data;
  9144. if (!sd->inventory.u.items_inventory[n].card[i])
  9145. continue;
  9146. if ((data = itemdb_exists(sd->inventory.u.items_inventory[n].card[i])) != NULL) {
  9147. if (data->unequip_script)
  9148. run_script(data->unequip_script, 0, sd->bl.id, fake_nd->bl.id);
  9149. }
  9150. }
  9151. }
  9152. }
  9153. sd->npc_item_flag = iflag;
  9154. }
  9155. /**
  9156. * Called when attempting to unequip an item from a player
  9157. * @param sd: Player data
  9158. * @param n: Item inventory index
  9159. * @param flag: Type of unequip
  9160. * 0 - only unequip
  9161. * 1 - calculate status after unequipping
  9162. * 2 - force unequip
  9163. * 4 - unequip by switching equipment
  9164. * @return True on success or false on failure
  9165. */
  9166. bool pc_unequipitem(struct map_session_data *sd, int n, int flag) {
  9167. int i, pos;
  9168. nullpo_retr(false,sd);
  9169. if (n < 0 || n >= MAX_INVENTORY) {
  9170. clif_unequipitemack(sd,0,0,0);
  9171. return false;
  9172. }
  9173. if (!(pos = sd->inventory.u.items_inventory[n].equip)) {
  9174. clif_unequipitemack(sd,n,0,0);
  9175. return false; //Nothing to unequip
  9176. }
  9177. // status change that makes player cannot unequip equipment
  9178. if (!(flag&2) && sd->sc.count &&
  9179. (sd->sc.data[SC_BERSERK] ||
  9180. sd->sc.data[SC_SATURDAYNIGHTFEVER] ||
  9181. sd->sc.data[SC__BLOODYLUST] ||
  9182. sd->sc.data[SC_KYOUGAKU] ||
  9183. (sd->sc.data[SC_PYROCLASTIC] &&
  9184. sd->inventory_data[n]->type == IT_WEAPON))) // can't switch weapon
  9185. {
  9186. clif_unequipitemack(sd,n,0,0);
  9187. return false;
  9188. }
  9189. if (battle_config.battle_log)
  9190. ShowInfo("unequip %d %x:%x\n",n,pc_equippoint(sd,n),pos);
  9191. for(i = 0; i < EQI_MAX; i++) {
  9192. if (pos & equip_bitmask[i])
  9193. sd->equip_index[i] = -1;
  9194. }
  9195. if(pos & EQP_HAND_R) {
  9196. sd->weapontype1 = 0;
  9197. sd->status.weapon = sd->weapontype2;
  9198. pc_calcweapontype(sd);
  9199. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  9200. if( !battle_config.dancing_weaponswitch_fix )
  9201. status_change_end(&sd->bl, SC_DANCING, INVALID_TIMER); // Unequipping => stop dancing.
  9202. #ifdef RENEWAL
  9203. if (battle_config.switch_remove_edp&2) {
  9204. #else
  9205. if (battle_config.switch_remove_edp&1) {
  9206. #endif
  9207. status_change_end(&sd->bl, SC_EDP, INVALID_TIMER);
  9208. }
  9209. }
  9210. if(pos & EQP_HAND_L) {
  9211. if (sd->status.shield && battle_getcurrentskill(&sd->bl) == LG_SHIELDSPELL)
  9212. unit_skillcastcancel(&sd->bl, 0); // Cancel Shield Spell if player swaps shields.
  9213. sd->status.shield = sd->weapontype2 = 0;
  9214. pc_calcweapontype(sd);
  9215. clif_changelook(&sd->bl,LOOK_SHIELD,sd->status.shield);
  9216. }
  9217. if(pos & EQP_SHOES)
  9218. clif_changelook(&sd->bl,LOOK_SHOES,0);
  9219. clif_unequipitemack(sd,n,pos,1);
  9220. pc_set_costume_view(sd);
  9221. status_change_end(&sd->bl,SC_HEAT_BARREL,INVALID_TIMER);
  9222. // On weapon change (right and left hand)
  9223. if ((pos & EQP_ARMS) && sd->inventory_data[n]->type == IT_WEAPON) {
  9224. if (battle_config.ammo_unequip && !(flag & 4)) {
  9225. switch (sd->inventory_data[n]->look) {
  9226. case W_BOW:
  9227. case W_MUSICAL:
  9228. case W_WHIP:
  9229. case W_REVOLVER:
  9230. case W_RIFLE:
  9231. case W_GATLING:
  9232. case W_SHOTGUN:
  9233. case W_GRENADE: {
  9234. short idx = sd->equip_index[EQI_AMMO];
  9235. if (idx >= 0) {
  9236. sd->equip_index[EQI_AMMO] = -1;
  9237. clif_unequipitemack(sd, idx, sd->inventory.u.items_inventory[idx].equip, 1);
  9238. pc_unequipitem_sub(sd, idx, 0);
  9239. }
  9240. }
  9241. break;
  9242. }
  9243. }
  9244. if (!sd->sc.data[SC_SEVENWIND] || sd->sc.data[SC_ASPERSIO]) //Check for seven wind (but not level seven!)
  9245. skill_enchant_elemental_end(&sd->bl, SC_NONE);
  9246. status_change_end(&sd->bl, SC_FEARBREEZE, INVALID_TIMER);
  9247. status_change_end(&sd->bl, SC_EXEEDBREAK, INVALID_TIMER);
  9248. }
  9249. // On armor change
  9250. if (pos & EQP_ARMOR) {
  9251. if (sd->sc.data[SC_HOVERING] && sd->inventory_data[n]->nameid == ITEMID_HOVERING_BOOSTER)
  9252. status_change_end(&sd->bl, SC_HOVERING, INVALID_TIMER);
  9253. //status_change_end(&sd->bl, SC_BENEDICTIO, INVALID_TIMER); // No longer is removed? Need confirmation
  9254. status_change_end(&sd->bl, SC_ARMOR_RESIST, INVALID_TIMER);
  9255. }
  9256. // On ammo change
  9257. 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_))
  9258. status_change_end(&sd->bl, SC_P_ALTER, INVALID_TIMER);
  9259. pc_unequipitem_sub(sd, n, flag);
  9260. return true;
  9261. }
  9262. int pc_equipswitch( struct map_session_data* sd, int index ){
  9263. // Get the target equip mask
  9264. int position = sd->inventory.u.items_inventory[index].equipSwitch;
  9265. // Get the currently equipped item
  9266. short equippedItem = pc_checkequip( sd, position, true );
  9267. // No item equipped at the target
  9268. if( equippedItem == -1 ){
  9269. // Remove it from the equip switch
  9270. pc_equipswitch_remove( sd, index );
  9271. pc_equipitem( sd, index, position );
  9272. return position;
  9273. }else{
  9274. std::map<int, int> unequipped;
  9275. int unequipped_position = 0;
  9276. // Unequip all items that interfere
  9277. for( int i = 0; i < EQI_MAX; i++ ){
  9278. int unequip_index = sd->equip_index[i];
  9279. if( unequip_index >= 0 && position & equip_bitmask[i] ){
  9280. struct item* unequip_item = &sd->inventory.u.items_inventory[unequip_index];
  9281. // Store the unequipped index and position mask for later
  9282. unequipped[unequip_index] = unequip_item->equip;
  9283. // Keep the position for later
  9284. unequipped_position |= unequip_item->equip;
  9285. // Unequip the item
  9286. pc_unequipitem( sd, unequip_index, 0 );
  9287. }
  9288. }
  9289. int all_position = position | unequipped_position;
  9290. // Equip everything that is hit by the mask
  9291. for( int i = 0; i < EQI_MAX; i++ ){
  9292. int exchange_index = sd->equip_switch_index[i];
  9293. if( exchange_index >= 0 && all_position & equip_bitmask[i] ){
  9294. struct item* exchange_item = &sd->inventory.u.items_inventory[exchange_index];
  9295. // Store the target position
  9296. int exchange_position = exchange_item->equipSwitch;
  9297. // Remove the item from equip switch
  9298. pc_equipswitch_remove( sd, exchange_index );
  9299. // Equip the item at the destinated position
  9300. pc_equipitem( sd, exchange_index, exchange_position );
  9301. }
  9302. }
  9303. // Place all unequipped items into the equip switch window
  9304. for( std::pair<int, int> pair : unequipped ){
  9305. int unequipped_index = pair.first;
  9306. int unequipped_position = pair.second;
  9307. // Rebuild the index cache
  9308. for( int i = 0; i < EQI_MAX; i++ ){
  9309. if( unequipped_position & equip_bitmask[i] ){
  9310. sd->equip_switch_index[i] = unequipped_index;
  9311. }
  9312. }
  9313. // Set the correct position mask
  9314. sd->inventory.u.items_inventory[unequipped_index].equipSwitch = unequipped_position;
  9315. // Notify the client
  9316. clif_equipswitch_add( sd, unequipped_index, unequipped_position, ITEM_EQUIP_ACK_OK );
  9317. }
  9318. return all_position;
  9319. }
  9320. }
  9321. void pc_equipswitch_remove( struct map_session_data* sd, int index ){
  9322. struct item* item = &sd->inventory.u.items_inventory[index];
  9323. if( !item->equipSwitch ){
  9324. return;
  9325. }
  9326. for( int i = 0; i < EQI_MAX; i++ ){
  9327. // If a match is found
  9328. if( sd->equip_switch_index[i] == index ){
  9329. // Remove it from the slot
  9330. sd->equip_switch_index[i] = -1;
  9331. }
  9332. }
  9333. // Send out one packet for all slots using the current item's mask
  9334. clif_equipswitch_remove( sd, index, item->equipSwitch, false );
  9335. item->equipSwitch = 0;
  9336. }
  9337. /*==========================================
  9338. * Checking if player (sd) has an invalid item
  9339. * and is unequiped on map load (item_noequip)
  9340. *------------------------------------------*/
  9341. void pc_checkitem(struct map_session_data *sd) {
  9342. int i, calc_flag = 0;
  9343. struct item* it;
  9344. nullpo_retv(sd);
  9345. if( sd->state.vending ) //Avoid reorganizing items when we are vending, as that leads to exploits (pointed out by End of Exam)
  9346. return;
  9347. pc_check_available_item(sd, ITMCHK_NONE); // Check for invalid(ated) items.
  9348. for( i = 0; i < MAX_INVENTORY; i++ ) {
  9349. it = &sd->inventory.u.items_inventory[i];
  9350. if( it->nameid == 0 )
  9351. continue;
  9352. if( !it->equip )
  9353. continue;
  9354. if( it->equip&~pc_equippoint(sd,i) ) {
  9355. pc_unequipitem(sd, i, 2);
  9356. calc_flag = 1;
  9357. continue;
  9358. }
  9359. 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) ) {
  9360. pc_unequipitem(sd, i, 2);
  9361. calc_flag = 1;
  9362. continue;
  9363. }
  9364. }
  9365. for( i = 0; i < MAX_INVENTORY; i++ ) {
  9366. it = &sd->inventory.u.items_inventory[i];
  9367. if( it->nameid == 0 )
  9368. continue;
  9369. if( !it->equipSwitch )
  9370. continue;
  9371. if( it->equipSwitch&~pc_equippoint(sd,i) ||
  9372. ( !pc_has_permission(sd, PC_PERM_USE_ALL_EQUIPMENT) && !battle_config.allow_equip_restricted_item && itemdb_isNoEquip(sd->inventory_data[i], sd->bl.m) ) ){
  9373. for( int j = 0; j < EQI_MAX; j++ ){
  9374. if( sd->equip_switch_index[j] == i ){
  9375. sd->equip_switch_index[j] = -1;
  9376. }
  9377. }
  9378. sd->inventory.u.items_inventory[i].equipSwitch = 0;
  9379. continue;
  9380. }
  9381. }
  9382. if( calc_flag && sd->state.active ) {
  9383. pc_checkallowskill(sd);
  9384. status_calc_pc(sd,SCO_NONE);
  9385. }
  9386. }
  9387. /*==========================================
  9388. * Checks for unavailable items and removes them.
  9389. * @param sd: Player data
  9390. * @param type Forced check:
  9391. * 1 - Inventory
  9392. * 2 - Cart
  9393. * 4 - Storage
  9394. *------------------------------------------*/
  9395. void pc_check_available_item(struct map_session_data *sd, uint8 type)
  9396. {
  9397. int i;
  9398. unsigned short nameid;
  9399. char output[256];
  9400. nullpo_retv(sd);
  9401. if (battle_config.item_check&ITMCHK_INVENTORY && type&ITMCHK_INVENTORY) { // Check for invalid(ated) items in inventory.
  9402. for(i = 0; i < MAX_INVENTORY; i++) {
  9403. nameid = sd->inventory.u.items_inventory[i].nameid;
  9404. if (!nameid)
  9405. continue;
  9406. if (!itemdb_available(nameid)) {
  9407. sprintf(output, msg_txt(sd, 709), nameid); // Item %hu has been removed from your inventory.
  9408. clif_displaymessage(sd->fd, output);
  9409. 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);
  9410. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 4, 0, LOG_TYPE_OTHER);
  9411. continue;
  9412. }
  9413. if (!sd->inventory.u.items_inventory[i].unique_id && !itemdb_isstackable(nameid))
  9414. sd->inventory.u.items_inventory[i].unique_id = pc_generate_unique_id(sd);
  9415. }
  9416. }
  9417. if (battle_config.item_check&ITMCHK_CART && type&ITMCHK_CART) { // Check for invalid(ated) items in cart.
  9418. for(i = 0; i < MAX_CART; i++) {
  9419. nameid = sd->cart.u.items_cart[i].nameid;
  9420. if (!nameid)
  9421. continue;
  9422. if (!itemdb_available(nameid)) {
  9423. sprintf(output, msg_txt(sd, 710), nameid); // Item %hu has been removed from your cart.
  9424. clif_displaymessage(sd->fd, output);
  9425. 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);
  9426. pc_cart_delitem(sd, i, sd->cart.u.items_cart[i].amount, 0, LOG_TYPE_OTHER);
  9427. continue;
  9428. }
  9429. if (!sd->cart.u.items_cart[i].unique_id && !itemdb_isstackable(nameid))
  9430. sd->cart.u.items_cart[i].unique_id = pc_generate_unique_id(sd);
  9431. }
  9432. }
  9433. if (battle_config.item_check&ITMCHK_STORAGE && type&ITMCHK_STORAGE) { // Check for invalid(ated) items in storage.
  9434. for(i = 0; i < sd->storage.max_amount; i++) {
  9435. nameid = sd->storage.u.items_storage[i].nameid;
  9436. if (!nameid)
  9437. continue;
  9438. if (!itemdb_available(nameid)) {
  9439. sprintf(output, msg_txt(sd, 711), nameid); // Item %hu has been removed from your storage.
  9440. clif_displaymessage(sd->fd, output);
  9441. 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);
  9442. storage_delitem(sd, &sd->storage, i, sd->storage.u.items_storage[i].amount);
  9443. continue;
  9444. }
  9445. if (!sd->storage.u.items_storage[i].unique_id && !itemdb_isstackable(nameid))
  9446. sd->storage.u.items_storage[i].unique_id = pc_generate_unique_id(sd);
  9447. }
  9448. }
  9449. }
  9450. /*==========================================
  9451. * Update PVP rank for sd1 in cmp to sd2
  9452. *------------------------------------------*/
  9453. static int pc_calc_pvprank_sub(struct block_list *bl,va_list ap)
  9454. {
  9455. struct map_session_data *sd1,*sd2;
  9456. sd1=(struct map_session_data *)bl;
  9457. sd2=va_arg(ap,struct map_session_data *);
  9458. if( pc_isinvisible(sd1) || pc_isinvisible(sd2) )
  9459. {// cannot register pvp rank for hidden GMs
  9460. return 0;
  9461. }
  9462. if( sd1->pvp_point > sd2->pvp_point )
  9463. sd2->pvp_rank++;
  9464. return 0;
  9465. }
  9466. /*==========================================
  9467. * Calculate new rank beetween all present players (map_foreachinallarea)
  9468. * and display result
  9469. *------------------------------------------*/
  9470. int pc_calc_pvprank(struct map_session_data *sd)
  9471. {
  9472. int old = sd->pvp_rank;
  9473. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  9474. sd->pvp_rank=1;
  9475. map_foreachinmap(pc_calc_pvprank_sub,sd->bl.m,BL_PC,sd);
  9476. if(old!=sd->pvp_rank || sd->pvp_lastusers!=mapdata->users_pvp)
  9477. clif_pvpset(sd,sd->pvp_rank,sd->pvp_lastusers=mapdata->users_pvp,0);
  9478. return sd->pvp_rank;
  9479. }
  9480. /*==========================================
  9481. * Calculate next sd ranking calculation from config
  9482. *------------------------------------------*/
  9483. TIMER_FUNC(pc_calc_pvprank_timer){
  9484. struct map_session_data *sd;
  9485. sd=map_id2sd(id);
  9486. if(sd==NULL)
  9487. return 0;
  9488. sd->pvp_timer = INVALID_TIMER;
  9489. if( pc_isinvisible(sd) )
  9490. {// do not calculate the pvp rank for a hidden GM
  9491. return 0;
  9492. }
  9493. if( pc_calc_pvprank(sd) > 0 )
  9494. sd->pvp_timer = add_timer(gettick()+PVP_CALCRANK_INTERVAL,pc_calc_pvprank_timer,id,data);
  9495. return 0;
  9496. }
  9497. /*==========================================
  9498. * Checking if sd is married
  9499. * Return:
  9500. * partner_id = yes
  9501. * 0 = no
  9502. *------------------------------------------*/
  9503. int pc_ismarried(struct map_session_data *sd)
  9504. {
  9505. if(sd == NULL)
  9506. return -1;
  9507. if(sd->status.partner_id > 0)
  9508. return sd->status.partner_id;
  9509. else
  9510. return 0;
  9511. }
  9512. /*==========================================
  9513. * Marry player sd to player dstsd
  9514. * Return:
  9515. * false = fail
  9516. * true = success
  9517. *------------------------------------------*/
  9518. bool pc_marriage(struct map_session_data *sd,struct map_session_data *dstsd)
  9519. {
  9520. if(sd == NULL || dstsd == NULL ||
  9521. sd->status.partner_id > 0 || dstsd->status.partner_id > 0 ||
  9522. (sd->class_&JOBL_BABY) || (dstsd->class_&JOBL_BABY))
  9523. return false;
  9524. sd->status.partner_id = dstsd->status.char_id;
  9525. dstsd->status.partner_id = sd->status.char_id;
  9526. achievement_update_objective(sd, AG_MARRY, 1, 1);
  9527. achievement_update_objective(dstsd, AG_MARRY, 1, 1);
  9528. return true;
  9529. }
  9530. /*==========================================
  9531. * Divorce sd from its partner
  9532. * Return:
  9533. * false = fail
  9534. * true = success
  9535. *------------------------------------------*/
  9536. bool pc_divorce(struct map_session_data *sd)
  9537. {
  9538. struct map_session_data *p_sd;
  9539. int i;
  9540. if( sd == NULL || !pc_ismarried(sd) )
  9541. return false;
  9542. if( !sd->status.partner_id )
  9543. return false; // Char is not married
  9544. if( (p_sd = map_charid2sd(sd->status.partner_id)) == NULL )
  9545. { // Lets char server do the divorce
  9546. if( chrif_divorce(sd->status.char_id, sd->status.partner_id) )
  9547. return false; // No char server connected
  9548. return true;
  9549. }
  9550. // Both players online, lets do the divorce manually
  9551. sd->status.partner_id = 0;
  9552. p_sd->status.partner_id = 0;
  9553. for( i = 0; i < MAX_INVENTORY; i++ )
  9554. {
  9555. if( sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_M || sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_F )
  9556. pc_delitem(sd, i, 1, 0, 0, LOG_TYPE_OTHER);
  9557. if( p_sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_M || p_sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_F )
  9558. pc_delitem(p_sd, i, 1, 0, 0, LOG_TYPE_OTHER);
  9559. }
  9560. clif_divorced(sd, p_sd->status.name);
  9561. clif_divorced(p_sd, sd->status.name);
  9562. return true;
  9563. }
  9564. /**
  9565. * Get the partner map_session_data of a player
  9566. * @param sd : the husband|wife session
  9567. * @return partner session or NULL
  9568. */
  9569. struct map_session_data *pc_get_partner(struct map_session_data *sd){
  9570. if (!sd || !pc_ismarried(sd))
  9571. return NULL;
  9572. return map_charid2sd(sd->status.partner_id);
  9573. }
  9574. /**
  9575. * Get the father map_session_data of a player
  9576. * @param sd : the baby session
  9577. * @return father session or NULL
  9578. */
  9579. struct map_session_data *pc_get_father (struct map_session_data *sd){
  9580. if (!sd || !(sd->class_&JOBL_BABY) || !sd->status.father)
  9581. return NULL;
  9582. return map_charid2sd(sd->status.father);
  9583. }
  9584. /**
  9585. * Get the mother map_session_data of a player
  9586. * @param sd : the baby session
  9587. * @return mother session or NULL
  9588. */
  9589. struct map_session_data *pc_get_mother (struct map_session_data *sd){
  9590. if (!sd || !(sd->class_&JOBL_BABY) || !sd->status.mother)
  9591. return NULL;
  9592. return map_charid2sd(sd->status.mother);
  9593. }
  9594. /*==========================================
  9595. * Get sd children charid. (Need to be married)
  9596. *------------------------------------------*/
  9597. struct map_session_data *pc_get_child (struct map_session_data *sd)
  9598. {
  9599. if (!sd || !pc_ismarried(sd) || !sd->status.child)
  9600. // charid2sd returns NULL if not found
  9601. return NULL;
  9602. return map_charid2sd(sd->status.child);
  9603. }
  9604. /*==========================================
  9605. * Set player sd to bleed. (losing hp and/or sp each diff_tick)
  9606. *------------------------------------------*/
  9607. void pc_bleeding (struct map_session_data *sd, t_tick diff_tick)
  9608. {
  9609. int hp = 0, sp = 0;
  9610. if( pc_isdead(sd) )
  9611. return;
  9612. if (sd->hp_loss.value) {
  9613. sd->hp_loss.tick += diff_tick;
  9614. while (sd->hp_loss.tick >= sd->hp_loss.rate) {
  9615. hp += sd->hp_loss.value;
  9616. sd->hp_loss.tick -= sd->hp_loss.rate;
  9617. }
  9618. if(hp >= sd->battle_status.hp)
  9619. hp = sd->battle_status.hp-1; //Script drains cannot kill you.
  9620. }
  9621. if (sd->sp_loss.value) {
  9622. sd->sp_loss.tick += diff_tick;
  9623. while (sd->sp_loss.tick >= sd->sp_loss.rate) {
  9624. sp += sd->sp_loss.value;
  9625. sd->sp_loss.tick -= sd->sp_loss.rate;
  9626. }
  9627. }
  9628. if (hp > 0 || sp > 0)
  9629. status_zap(&sd->bl, hp, sp);
  9630. }
  9631. //Character regen. Flag is used to know which types of regen can take place.
  9632. //&1: HP regen
  9633. //&2: SP regen
  9634. void pc_regen (struct map_session_data *sd, t_tick diff_tick)
  9635. {
  9636. int hp = 0, sp = 0;
  9637. if (sd->hp_regen.value) {
  9638. sd->hp_regen.tick += diff_tick;
  9639. while (sd->hp_regen.tick >= sd->hp_regen.rate) {
  9640. hp += sd->hp_regen.value;
  9641. sd->hp_regen.tick -= sd->hp_regen.rate;
  9642. }
  9643. }
  9644. if (sd->sp_regen.value) {
  9645. sd->sp_regen.tick += diff_tick;
  9646. while (sd->sp_regen.tick >= sd->sp_regen.rate) {
  9647. sp += sd->sp_regen.value;
  9648. sd->sp_regen.tick -= sd->sp_regen.rate;
  9649. }
  9650. }
  9651. if (sd->percent_hp_regen.value) {
  9652. sd->percent_hp_regen.tick += diff_tick;
  9653. while (sd->percent_hp_regen.tick >= sd->percent_hp_regen.rate) {
  9654. hp += sd->status.max_hp * sd->percent_hp_regen.value / 100;
  9655. sd->percent_hp_regen.tick -= sd->percent_hp_regen.rate;
  9656. }
  9657. }
  9658. if (sd->percent_sp_regen.value) {
  9659. sd->percent_sp_regen.tick += diff_tick;
  9660. while (sd->percent_sp_regen.tick >= sd->percent_sp_regen.rate) {
  9661. sp += sd->status.max_sp * sd->percent_sp_regen.value / 100;
  9662. sd->percent_sp_regen.tick -= sd->percent_sp_regen.rate;
  9663. }
  9664. }
  9665. if (hp > 0 || sp > 0)
  9666. status_heal(&sd->bl, hp, sp, 0);
  9667. }
  9668. /*==========================================
  9669. * Memo player sd savepoint. (map,x,y)
  9670. *------------------------------------------*/
  9671. void pc_setsavepoint(struct map_session_data *sd, short mapindex,int x,int y)
  9672. {
  9673. nullpo_retv(sd);
  9674. sd->status.save_point.map = mapindex;
  9675. sd->status.save_point.x = x;
  9676. sd->status.save_point.y = y;
  9677. }
  9678. /*==========================================
  9679. * Save 1 player data at autosave interval
  9680. *------------------------------------------*/
  9681. static TIMER_FUNC(pc_autosave){
  9682. int interval;
  9683. struct s_mapiterator* iter;
  9684. struct map_session_data* sd;
  9685. static int last_save_id = 0, save_flag = 0;
  9686. if(save_flag == 2) //Someone was saved on last call, normal cycle
  9687. save_flag = 0;
  9688. else
  9689. save_flag = 1; //Noone was saved, so save first found char.
  9690. iter = mapit_getallusers();
  9691. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  9692. {
  9693. if (!sd->state.pc_loaded) // Player data hasn't fully loaded
  9694. continue;
  9695. if(sd->bl.id == last_save_id && save_flag != 1) {
  9696. save_flag = 1;
  9697. continue;
  9698. }
  9699. if(save_flag != 1) //Not our turn to save yet.
  9700. continue;
  9701. //Save char.
  9702. last_save_id = sd->bl.id;
  9703. save_flag = 2;
  9704. if (pc_isvip(sd)) // Check if we're still VIP
  9705. chrif_req_login_operation(1, sd->status.name, CHRIF_OP_LOGIN_VIP, 0, 1, 0);
  9706. chrif_save(sd, CSAVE_INVENTORY|CSAVE_CART);
  9707. break;
  9708. }
  9709. mapit_free(iter);
  9710. interval = autosave_interval/(map_usercount()+1);
  9711. if(interval < minsave_interval)
  9712. interval = minsave_interval;
  9713. add_timer(gettick()+interval,pc_autosave,0,0);
  9714. return 0;
  9715. }
  9716. static int pc_daynight_timer_sub(struct map_session_data *sd,va_list ap)
  9717. {
  9718. if (sd->state.night != night_flag && map_getmapflag(sd->bl.m, MF_NIGHTENABLED))
  9719. { //Night/day state does not match.
  9720. clif_status_load(&sd->bl, EFST_SKE, night_flag); //New night effect by dynamix [Skotlex]
  9721. sd->state.night = night_flag;
  9722. return 1;
  9723. }
  9724. return 0;
  9725. }
  9726. /*================================================
  9727. * timer to do the day [Yor]
  9728. * data: 0 = called by timer, 1 = gmcommand/script
  9729. *------------------------------------------------*/
  9730. TIMER_FUNC(map_day_timer){
  9731. char tmp_soutput[1024];
  9732. if (data == 0 && battle_config.day_duration <= 0) // if we want a day
  9733. return 0;
  9734. if (!night_flag)
  9735. return 0; //Already day.
  9736. night_flag = 0; // 0=day, 1=night [Yor]
  9737. map_foreachpc(pc_daynight_timer_sub);
  9738. strcpy(tmp_soutput, (data == 0) ? msg_txt(NULL,502) : msg_txt(NULL,60)); // The day has arrived!
  9739. intif_broadcast(tmp_soutput, strlen(tmp_soutput) + 1, BC_DEFAULT);
  9740. return 0;
  9741. }
  9742. /*================================================
  9743. * timer to do the night [Yor]
  9744. * data: 0 = called by timer, 1 = gmcommand/script
  9745. *------------------------------------------------*/
  9746. TIMER_FUNC(map_night_timer){
  9747. char tmp_soutput[1024];
  9748. if (data == 0 && battle_config.night_duration <= 0) // if we want a night
  9749. return 0;
  9750. if (night_flag)
  9751. return 0; //Already nigth.
  9752. night_flag = 1; // 0=day, 1=night [Yor]
  9753. map_foreachpc(pc_daynight_timer_sub);
  9754. strcpy(tmp_soutput, (data == 0) ? msg_txt(NULL,503) : msg_txt(NULL,59)); // The night has fallen...
  9755. intif_broadcast(tmp_soutput, strlen(tmp_soutput) + 1, BC_DEFAULT);
  9756. return 0;
  9757. }
  9758. /**
  9759. * Attempt to stand up a player
  9760. * @param sd
  9761. * @param force Ignore the check, ask player to stand up. Used in some cases like pc_damage(), pc_revive(), etc
  9762. * @return True if success, Fals if failed
  9763. */
  9764. bool pc_setstand(struct map_session_data *sd, bool force){
  9765. nullpo_ret(sd);
  9766. // Cannot stand yet
  9767. // TODO: Move to SCS_NOSTAND [Cydh]
  9768. if (!force && (sd->sc.data[SC_SITDOWN_FORCE] || sd->sc.data[SC_BANANA_BOMB_SITDOWN]))
  9769. return false;
  9770. status_change_end(&sd->bl, SC_TENSIONRELAX, INVALID_TIMER);
  9771. clif_status_load(&sd->bl,EFST_SIT,0);
  9772. clif_standing(&sd->bl); //Inform area PC is standing
  9773. //Reset sitting tick.
  9774. sd->ssregen.tick.hp = sd->ssregen.tick.sp = 0;
  9775. if( pc_isdead( sd ) ){
  9776. sd->state.dead_sit = sd->vd.dead_sit = 0;
  9777. clif_party_dead( sd );
  9778. }else{
  9779. sd->state.dead_sit = sd->vd.dead_sit = 0;
  9780. }
  9781. return true;
  9782. }
  9783. /**
  9784. * Calculate Overheat value
  9785. * @param sd: Player data
  9786. * @param heat: Amount of Heat to adjust
  9787. **/
  9788. void pc_overheat(struct map_session_data *sd, int16 heat) {
  9789. struct status_change_entry *sce = NULL;
  9790. int16 limit[] = { 150, 200, 280, 360, 450 };
  9791. uint16 skill_lv;
  9792. nullpo_retv(sd);
  9793. skill_lv = cap_value(pc_checkskill(sd, NC_MAINFRAME), 0, 4);
  9794. if ((sce = sd->sc.data[SC_OVERHEAT_LIMITPOINT])) {
  9795. sce->val1 += heat;
  9796. sce->val1 = cap_value(sce->val1, 0, 1000);
  9797. if (sd->sc.data[SC_OVERHEAT])
  9798. status_change_end(&sd->bl, SC_OVERHEAT, INVALID_TIMER);
  9799. if (sce->val1 > limit[skill_lv])
  9800. sc_start(&sd->bl, &sd->bl, SC_OVERHEAT, 100, sce->val1, 1000);
  9801. } else if (heat > 0)
  9802. sc_start(&sd->bl, &sd->bl, SC_OVERHEAT_LIMITPOINT, 100, heat, 1000);
  9803. }
  9804. /**
  9805. * Check if player is autolooting given itemID.
  9806. */
  9807. bool pc_isautolooting(struct map_session_data *sd, unsigned short nameid)
  9808. {
  9809. uint8 i = 0;
  9810. if (sd->state.autoloottype && sd->state.autoloottype&(1<<itemdb_type(nameid)))
  9811. return true;
  9812. if (!sd->state.autolooting)
  9813. return false;
  9814. if (sd->state.autolooting)
  9815. ARR_FIND(0, AUTOLOOTITEM_SIZE, i, sd->state.autolootid[i] == nameid);
  9816. return (i != AUTOLOOTITEM_SIZE);
  9817. }
  9818. /**
  9819. * Checks if player can use @/#command
  9820. * @param sd Player map session data
  9821. * @param command Command name without @/# and params
  9822. * @param type is it atcommand or charcommand
  9823. */
  9824. bool pc_can_use_command(struct map_session_data *sd, const char *command, AtCommandType type)
  9825. {
  9826. return pc_group_can_use_command(pc_get_group_id(sd), command, type);
  9827. }
  9828. /**
  9829. * Checks if commands used by a player should be logged
  9830. * according to their group setting.
  9831. * @param sd Player map session data
  9832. */
  9833. bool pc_should_log_commands(struct map_session_data *sd)
  9834. {
  9835. return pc_group_should_log_commands(pc_get_group_id(sd));
  9836. }
  9837. /**
  9838. * Spirit Charm expiration timer.
  9839. * @see TimerFunc
  9840. */
  9841. static TIMER_FUNC(pc_spiritcharm_timer){
  9842. struct map_session_data *sd;
  9843. int i;
  9844. if ((sd = (struct map_session_data *)map_id2sd(id)) == NULL || sd->bl.type != BL_PC)
  9845. return 1;
  9846. if (sd->spiritcharm <= 0) {
  9847. 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);
  9848. sd->spiritcharm = 0;
  9849. sd->spiritcharm_type = CHARM_TYPE_NONE;
  9850. return 0;
  9851. }
  9852. ARR_FIND(0, sd->spiritcharm, i, sd->spiritcharm_timer[i] == tid);
  9853. if (i == sd->spiritcharm) {
  9854. ShowError("pc_spiritcharm_timer: timer not found (aid=%d cid=%d tid=%d)\n", sd->status.account_id, sd->status.char_id, tid);
  9855. return 0;
  9856. }
  9857. sd->spiritcharm--;
  9858. if (i != sd->spiritcharm)
  9859. memmove(sd->spiritcharm_timer + i, sd->spiritcharm_timer + i + 1, (sd->spiritcharm - i) * sizeof(int));
  9860. sd->spiritcharm_timer[sd->spiritcharm] = INVALID_TIMER;
  9861. if (sd->spiritcharm <= 0)
  9862. sd->spiritcharm_type = CHARM_TYPE_NONE;
  9863. clif_spiritcharm(sd);
  9864. return 0;
  9865. }
  9866. /**
  9867. * Adds a spirit charm.
  9868. * @param sd: Target character
  9869. * @param interval: Duration
  9870. * @param max: Maximum amount of charms to add
  9871. * @param type: Charm type (@see spirit_charm_types)
  9872. */
  9873. void pc_addspiritcharm(struct map_session_data *sd, int interval, int max, int type)
  9874. {
  9875. int tid, i;
  9876. nullpo_retv(sd);
  9877. if (sd->spiritcharm_type != CHARM_TYPE_NONE && type != sd->spiritcharm_type)
  9878. pc_delspiritcharm(sd, sd->spiritcharm, sd->spiritcharm_type);
  9879. if (max > MAX_SPIRITCHARM)
  9880. max = MAX_SPIRITCHARM;
  9881. if (sd->spiritcharm < 0)
  9882. sd->spiritcharm = 0;
  9883. if (sd->spiritcharm && sd->spiritcharm >= max) {
  9884. if (sd->spiritcharm_timer[0] != INVALID_TIMER)
  9885. delete_timer(sd->spiritcharm_timer[0], pc_spiritcharm_timer);
  9886. sd->spiritcharm--;
  9887. if (sd->spiritcharm != 0)
  9888. memmove(sd->spiritcharm_timer + 0, sd->spiritcharm_timer + 1, (sd->spiritcharm) * sizeof(int));
  9889. sd->spiritcharm_timer[sd->spiritcharm] = INVALID_TIMER;
  9890. }
  9891. tid = add_timer(gettick() + interval, pc_spiritcharm_timer, sd->bl.id, 0);
  9892. 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);
  9893. if (i != sd->spiritcharm)
  9894. memmove(sd->spiritcharm_timer + i + 1, sd->spiritcharm_timer + i, (sd->spiritcharm - i) * sizeof(int));
  9895. sd->spiritcharm_timer[i] = tid;
  9896. sd->spiritcharm++;
  9897. sd->spiritcharm_type = type;
  9898. clif_spiritcharm(sd);
  9899. }
  9900. /**
  9901. * Removes one or more spirit charms.
  9902. * @param sd: The target character
  9903. * @param count: Amount of charms to remove
  9904. * @param type: Type of charm to remove
  9905. */
  9906. void pc_delspiritcharm(struct map_session_data *sd, int count, int type)
  9907. {
  9908. int i;
  9909. nullpo_retv(sd);
  9910. if (sd->spiritcharm_type != type)
  9911. return;
  9912. if (sd->spiritcharm <= 0) {
  9913. sd->spiritcharm = 0;
  9914. return;
  9915. }
  9916. if (count <= 0)
  9917. return;
  9918. if (count > sd->spiritcharm)
  9919. count = sd->spiritcharm;
  9920. sd->spiritcharm -= count;
  9921. if (count > MAX_SPIRITCHARM)
  9922. count = MAX_SPIRITCHARM;
  9923. for (i = 0; i < count; i++) {
  9924. if (sd->spiritcharm_timer[i] != INVALID_TIMER) {
  9925. delete_timer(sd->spiritcharm_timer[i], pc_spiritcharm_timer);
  9926. sd->spiritcharm_timer[i] = INVALID_TIMER;
  9927. }
  9928. }
  9929. for (i = count; i < MAX_SPIRITCHARM; i++) {
  9930. sd->spiritcharm_timer[i - count] = sd->spiritcharm_timer[i];
  9931. sd->spiritcharm_timer[i] = INVALID_TIMER;
  9932. }
  9933. if (sd->spiritcharm <= 0)
  9934. sd->spiritcharm_type = CHARM_TYPE_NONE;
  9935. clif_spiritcharm(sd);
  9936. }
  9937. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  9938. /**
  9939. * Renewal EXP/Item Drop rate modifier based on level penalty
  9940. * @param level_diff: Monster and Player level difference
  9941. * @param mob_class: Monster class
  9942. * @param mode: Monster mode
  9943. * @param type: 1 - EXP, 2 - Item Drop
  9944. * @return Penalty rate
  9945. */
  9946. int pc_level_penalty_mod(int level_diff, uint32 mob_class, enum e_mode mode, int type)
  9947. {
  9948. int rate = 100;
  9949. if (type == 2 && (mode&MD_FIXED_ITEMDROP))
  9950. return rate;
  9951. if (level_diff < 0)
  9952. level_diff = MAX_LEVEL + (~level_diff + 1);
  9953. if ((rate = level_penalty[type][mob_class][level_diff]) > 0) // Monster class found, return rate
  9954. return rate;
  9955. return 100; // Penalty not found, return default
  9956. }
  9957. #endif
  9958. int pc_split_str(char *str,char **val,int num)
  9959. {
  9960. int i;
  9961. for (i=0; i<num && str; i++){
  9962. val[i] = str;
  9963. str = strchr(str,',');
  9964. if (str && i<num-1) //Do not remove a trailing comma.
  9965. *str++=0;
  9966. }
  9967. return i;
  9968. }
  9969. int pc_split_atoi(char* str, int* val, char sep, int max)
  9970. {
  9971. int i,j;
  9972. for (i=0; i<max; i++) {
  9973. if (!str) break;
  9974. val[i] = atoi(str);
  9975. str = strchr(str,sep);
  9976. if (str)
  9977. *str++=0;
  9978. }
  9979. //Zero up the remaining.
  9980. for(j=i; j < max; j++)
  9981. val[j] = 0;
  9982. return i;
  9983. }
  9984. int pc_split_atoui(char* str, unsigned int* val, char sep, int max)
  9985. {
  9986. static int warning=0;
  9987. int i,j;
  9988. for (i=0; i<max; i++) {
  9989. double f;
  9990. if (!str) break;
  9991. f = atof(str);
  9992. if (f < 0)
  9993. val[i] = 0;
  9994. else if (f > UINT_MAX) {
  9995. val[i] = UINT_MAX;
  9996. if (!warning) {
  9997. warning = 1;
  9998. ShowWarning("pc_readdb (exp.txt): Required exp per level is capped to %u\n", UINT_MAX);
  9999. }
  10000. } else
  10001. val[i] = (unsigned int)f;
  10002. str = strchr(str,sep);
  10003. if (str)
  10004. *str++=0;
  10005. }
  10006. //Zero up the remaining.
  10007. for(j=i; j < max; j++)
  10008. val[j] = 0;
  10009. return i;
  10010. }
  10011. /*==========================================
  10012. * sub DB reading.
  10013. * Function used to read skill_tree.txt
  10014. *------------------------------------------*/
  10015. static bool pc_readdb_skilltree(char* fields[], int columns, int current)
  10016. {
  10017. uint32 baselv, joblv, baselv_max, joblv_max;
  10018. uint16 skill_id, skill_lv, skill_lv_max;
  10019. int idx, class_;
  10020. unsigned int i, offset, skill_idx;
  10021. class_ = atoi(fields[0]);
  10022. skill_id = (uint16)atoi(fields[1]);
  10023. skill_lv = (uint16)atoi(fields[2]);
  10024. if (columns == 5 + MAX_PC_SKILL_REQUIRE * 2) { // Base/Job level requirement extra columns
  10025. baselv = (uint32)atoi(fields[3]);
  10026. joblv = (uint32)atoi(fields[4]);
  10027. offset = 5;
  10028. }
  10029. else if (columns == 3 + MAX_PC_SKILL_REQUIRE * 2) {
  10030. baselv = joblv = 0;
  10031. offset = 3;
  10032. }
  10033. else {
  10034. ShowWarning("pc_readdb_skilltree: Invalid number of colums in skill %hu of job %d's tree.\n", skill_id, class_);
  10035. return false;
  10036. }
  10037. if(!pcdb_checkid(class_))
  10038. {
  10039. ShowWarning("pc_readdb_skilltree: Invalid job class %d specified.\n", class_);
  10040. return false;
  10041. }
  10042. idx = pc_class2idx(class_);
  10043. if (!skill_get_index(skill_id)) {
  10044. ShowWarning("pc_readdb_skilltree: Unable to load skill %hu into job %d's tree.\n", skill_id, class_);
  10045. return false;
  10046. }
  10047. if (skill_lv > (skill_lv_max = skill_get_max(skill_id))) {
  10048. 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);
  10049. skill_lv = skill_lv_max;
  10050. }
  10051. if (baselv > (baselv_max = pc_class_maxbaselv(class_))) {
  10052. 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);
  10053. baselv = baselv_max;
  10054. }
  10055. if (joblv > (joblv_max = pc_class_maxjoblv(class_))) {
  10056. 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);
  10057. joblv = joblv_max;
  10058. }
  10059. //This is to avoid adding two lines for the same skill. [Skotlex]
  10060. 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 );
  10061. if( skill_idx == MAX_SKILL_TREE )
  10062. {
  10063. 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_);
  10064. return false;
  10065. }
  10066. else if(skill_tree[idx][skill_idx].skill_id)
  10067. {
  10068. ShowNotice("pc_readdb_skilltree: Overwriting skill %hu for job %d.\n", skill_id, class_);
  10069. }
  10070. skill_tree[idx][skill_idx].skill_id = skill_id;
  10071. skill_tree[idx][skill_idx].skill_lv = skill_lv;
  10072. skill_tree[idx][skill_idx].baselv = baselv;
  10073. skill_tree[idx][skill_idx].joblv = joblv;
  10074. for(i = 0; i < MAX_PC_SKILL_REQUIRE; i++)
  10075. {
  10076. skill_id = (uint16)atoi(fields[i * 2 + offset]);
  10077. skill_lv = (uint16)atoi(fields[i * 2 + offset + 1]);
  10078. if (skill_id == 0) {
  10079. if (skill_tree[idx][skill_idx].need[i].skill_id > 0) { // Remove pre-requisite
  10080. skill_tree[idx][skill_idx].need[i].skill_id = 0;
  10081. skill_tree[idx][skill_idx].need[i].skill_lv = 0;
  10082. }
  10083. continue;
  10084. }
  10085. if (skill_id > MAX_SKILL_ID || !skill_get_index(skill_id)) {
  10086. ShowWarning("pc_readdb_skilltree: Unable to load requirement skill %hu into job %d's tree.", skill_id, class_);
  10087. return false;
  10088. }
  10089. if (skill_lv > (skill_lv_max = skill_get_max(skill_id))) {
  10090. 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);
  10091. skill_lv = skill_lv_max;
  10092. }
  10093. skill_tree[idx][skill_idx].need[i].skill_id = skill_id;
  10094. skill_tree[idx][skill_idx].need[i].skill_lv = skill_lv;
  10095. }
  10096. return true;
  10097. }
  10098. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  10099. static bool pc_readdb_levelpenalty(char* fields[], int columns, int current)
  10100. {
  10101. int type, class_, diff;
  10102. type = atoi(fields[0]); //1=experience, 2=item drop
  10103. class_ = atoi(fields[1]);
  10104. diff = atoi(fields[2]);
  10105. if( type != 1 && type != 2 ){
  10106. ShowWarning("pc_readdb_levelpenalty: Invalid type %d specified.\n", type);
  10107. return false;
  10108. }
  10109. if( !CHK_CLASS(class_) ){
  10110. ShowWarning("pc_readdb_levelpenalty: Invalid class %d specified.\n", class_);
  10111. return false;
  10112. }
  10113. diff = min(diff, MAX_LEVEL);
  10114. if( diff < 0 )
  10115. diff = min(MAX_LEVEL + ( ~(diff) + 1 ), MAX_LEVEL*2);
  10116. level_penalty[type][class_][diff] = atoi(fields[3]);
  10117. return true;
  10118. }
  10119. #endif
  10120. /** [Cydh]
  10121. * Calculates base hp of player. Reference: http://irowiki.org/wiki/Max_HP
  10122. * @param level Base level of player
  10123. * @param class_ Job ID @see enum e_job
  10124. * @return base_hp
  10125. */
  10126. static unsigned int pc_calc_basehp(uint16 level, uint16 class_) {
  10127. double base_hp;
  10128. uint16 i, idx = pc_class2idx(class_);
  10129. base_hp = 35 + level * (job_info[idx].hp_multiplicator/100.);
  10130. #ifndef RENEWAL
  10131. if(level >= 10 && (class_ == JOB_NINJA || class_ == JOB_GUNSLINGER)) base_hp += 90;
  10132. #endif
  10133. for (i = 2; i <= level; i++)
  10134. base_hp += floor(((job_info[idx].hp_factor/100.) * i) + 0.5); //Don't have round()
  10135. if (class_ == JOB_SUMMONER)
  10136. base_hp += floor((base_hp / 2) + 0.5);
  10137. return (unsigned int)base_hp;
  10138. }
  10139. /** [Playtester]
  10140. * Calculates base sp of player.
  10141. * @param level Base level of player
  10142. * @param class_ Job ID @see enum e_job
  10143. * @return base_sp
  10144. */
  10145. static unsigned int pc_calc_basesp(uint16 level, uint16 class_) {
  10146. double base_sp;
  10147. uint16 idx = pc_class2idx(class_);
  10148. base_sp = 10 + floor(level * (job_info[idx].sp_factor / 100.));
  10149. switch (class_) {
  10150. case JOB_NINJA:
  10151. if (level >= 10)
  10152. base_sp -= 22;
  10153. else
  10154. base_sp = 11 + 3*level;
  10155. break;
  10156. case JOB_GUNSLINGER:
  10157. if (level > 10)
  10158. base_sp -= 18;
  10159. else
  10160. base_sp = 9 + 3*level;
  10161. break;
  10162. case JOB_SUMMONER:
  10163. base_sp -= floor(base_sp / 2);
  10164. break;
  10165. }
  10166. return (unsigned int)base_sp;
  10167. }
  10168. //Reading job_db1.txt line, (class,weight,HPFactor,HPMultiplicator,SPFactor,aspd/lvl...)
  10169. static bool pc_readdb_job1(char* fields[], int columns, int current){
  10170. int idx, class_;
  10171. unsigned int i;
  10172. class_ = atoi(fields[0]);
  10173. if (!pcdb_checkid(class_)) {
  10174. ShowWarning("status_readdb_job1: Invalid job class %d specified.\n", class_);
  10175. return false;
  10176. }
  10177. idx = pc_class2idx(class_);
  10178. job_info[idx].max_weight_base = atoi(fields[1]);
  10179. job_info[idx].hp_factor = atoi(fields[2]);
  10180. job_info[idx].hp_multiplicator = atoi(fields[3]);
  10181. job_info[idx].sp_factor = atoi(fields[4]);
  10182. #ifdef RENEWAL_ASPD
  10183. for(i = 0; i <= MAX_WEAPON_TYPE; i++)
  10184. #else
  10185. for(i = 0; i < MAX_WEAPON_TYPE; i++)
  10186. #endif
  10187. {
  10188. job_info[idx].aspd_base[i] = atoi(fields[i+5]);
  10189. }
  10190. return true;
  10191. }
  10192. //Reading job_db2.txt line (class,JobLv1,JobLv2,JobLv3,...)
  10193. static bool pc_readdb_job2(char* fields[], int columns, int current)
  10194. {
  10195. int idx, class_, i;
  10196. class_ = atoi(fields[0]);
  10197. if(!pcdb_checkid(class_))
  10198. {
  10199. ShowWarning("status_readdb_job2: Invalid job class %d specified.\n", class_);
  10200. return false;
  10201. }
  10202. idx = pc_class2idx(class_);
  10203. for(i = 1; i < columns; i++)
  10204. {
  10205. job_info[idx].job_bonus[i-1] = atoi(fields[i]);
  10206. }
  10207. return true;
  10208. }
  10209. //Reading job_exp.txt line
  10210. //Max Level,Class list,Type (0 - Base Exp; 1 - Job Exp),Exp/lvl...
  10211. static bool pc_readdb_job_exp(char* fields[], int columns, int current)
  10212. {
  10213. int idx, i, type;
  10214. int job_id,job_count,jobs[CLASS_COUNT];
  10215. unsigned int ui, maxlvl;
  10216. maxlvl = atoi(fields[0]);
  10217. if(maxlvl > MAX_LEVEL || maxlvl<1){
  10218. ShowError("pc_readdb_job_exp: Invalid maxlevel %d specified.\n", maxlvl);
  10219. return false;
  10220. }
  10221. if((maxlvl+3) > columns){ //nb values = (maxlvl-startlvl)+1-index1stvalue
  10222. ShowError("pc_readdb_job_exp: Number of columns %d defined is too low for max level %d.\n",columns,maxlvl);
  10223. return false;
  10224. }
  10225. type = atoi(fields[2]);
  10226. if(type < 0 || type > 1){
  10227. ShowError("pc_readdb_job_exp: Invalid type %d specified.\n", type);
  10228. return false;
  10229. }
  10230. job_count = pc_split_atoi(fields[1],jobs,':',CLASS_COUNT);
  10231. if (job_count < 1)
  10232. return false;
  10233. job_id = jobs[0];
  10234. if(!pcdb_checkid(job_id)){
  10235. ShowError("pc_readdb_job_exp: Invalid job class %d specified.\n", job_id);
  10236. return false;
  10237. }
  10238. idx = pc_class2idx(job_id);
  10239. job_info[idx].max_level[type] = maxlvl;
  10240. for(i=0; i<maxlvl; i++)
  10241. job_info[idx].exp_table[type][i] = ((uint32) atoi(fields[3+i]));
  10242. //Reverse check in case the array has a bunch of trailing zeros... [Skotlex]
  10243. //The reasoning behind the -2 is this... if the max level is 5, then the array
  10244. //should look like this:
  10245. //0: x, 1: x, 2: x: 3: x 4: 0 <- last valid value is at 3.
  10246. while ((ui = job_info[idx].max_level[type]) >= 2 && job_info[idx].exp_table[type][ui-2] <= 0)
  10247. job_info[idx].max_level[type]--;
  10248. if (job_info[idx].max_level[type] < maxlvl) {
  10249. 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]);
  10250. ShowInfo("Filling the missing values with the last exp entry.\n");
  10251. //Fill the requested values with the last entry.
  10252. ui = (job_info[idx].max_level[type] <= 2? 0: job_info[idx].max_level[type]-2);
  10253. for (; ui+2 < maxlvl; ui++)
  10254. job_info[idx].exp_table[type][ui] = job_info[idx].exp_table[type][ui-1];
  10255. job_info[idx].max_level[type] = maxlvl;
  10256. }
  10257. // ShowInfo("%s - Class %d: %d\n", type?"Job":"Base", job_id, job_info[idx].max_level[type]);
  10258. for (i = 1; i < job_count; i++) {
  10259. job_id = jobs[i];
  10260. if (!pcdb_checkid(job_id)) {
  10261. ShowError("pc_readdb_job_exp: Invalid job ID %d.\n", job_id);
  10262. continue;
  10263. }
  10264. idx = pc_class2idx(job_id);
  10265. 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]));
  10266. job_info[idx].max_level[type] = maxlvl;
  10267. // ShowInfo("%s - Class %d: %u\n", type?"Job":"Base", job_id, job_info[idx].max_level[type]);
  10268. }
  10269. return true;
  10270. }
  10271. /**
  10272. * #ifdef HP_SP_TABLES, reads 'job_basehpsp_db.txt to replace hp/sp results from formula
  10273. * startlvl,endlvl,class,type,values...
  10274. */
  10275. #ifdef HP_SP_TABLES
  10276. static bool pc_readdb_job_basehpsp(char* fields[], int columns, int current)
  10277. {
  10278. int i, startlvl, endlvl;
  10279. int job_count,jobs[CLASS_COUNT];
  10280. short type;
  10281. startlvl = atoi(fields[0]);
  10282. if(startlvl<1){
  10283. ShowError("pc_readdb_job_basehpsp: Invalid start level %d specified.\n", startlvl);
  10284. return false;
  10285. }
  10286. endlvl = atoi(fields[1]);
  10287. if(endlvl<1 || endlvl<startlvl){
  10288. ShowError("pc_readdb_job_basehpsp: Invalid end level %d specified.\n", endlvl);
  10289. return false;
  10290. }
  10291. if((endlvl-startlvl+1+4) > columns){ //nb values = (maxlvl-startlvl)+1-index1stvalue
  10292. 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);
  10293. return false;
  10294. }
  10295. type = atoi(fields[3]);
  10296. if(type < 0 || type > 1){
  10297. ShowError("pc_readdb_job_basehpsp: Invalid type %d specified.\n", type);
  10298. return false;
  10299. }
  10300. job_count = pc_split_atoi(fields[2],jobs,':',CLASS_COUNT);
  10301. if (job_count < 1)
  10302. return false;
  10303. for (i = 0; i < job_count; i++) {
  10304. int idx, job_id = jobs[i], use_endlvl;
  10305. if (!pcdb_checkid(job_id)) {
  10306. ShowError("pc_readdb_job_basehpsp: Invalid job class %d specified.\n", job_id);
  10307. return false;
  10308. }
  10309. idx = pc_class2idx(job_id);
  10310. if (startlvl > job_info[idx].max_level[0]) {
  10311. ShowError("pc_readdb_job_basehpsp: Invalid start level %d specified.\n", startlvl);
  10312. return false;
  10313. }
  10314. //Just read until available max level for this job, don't use MAX_LEVEL!
  10315. use_endlvl = endlvl;
  10316. if (use_endlvl > job_info[idx].max_level[0])
  10317. use_endlvl = job_info[idx].max_level[0];
  10318. if(type == 0) { //hp type
  10319. uint16 j;
  10320. for(j = 0; j < use_endlvl; j++) {
  10321. if (atoi(fields[j+4])) {
  10322. uint16 lvl_idx = startlvl-1+j;
  10323. job_info[idx].base_hp[lvl_idx] = atoi(fields[j+4]);
  10324. //Tells if this HP is lower than previous level (but not for 99->100)
  10325. if (lvl_idx-1 >= 0 && lvl_idx != 99 && job_info[idx].base_hp[lvl_idx] < job_info[idx].base_hp[lvl_idx-1])
  10326. 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",
  10327. current,j+4,job_id,lvl_idx+1,job_info[idx].base_hp[lvl_idx-1],job_info[idx].base_hp[lvl_idx]);
  10328. }
  10329. }
  10330. }
  10331. else { //sp type
  10332. uint16 j;
  10333. for(j = 0; j < use_endlvl; j++) {
  10334. if (atoi(fields[j+4])) {
  10335. uint16 lvl_idx = startlvl-1+j;
  10336. job_info[idx].base_sp[lvl_idx] = atoi(fields[j+4]);
  10337. //Tells if this SP is lower than previous level (but not for 99->100)
  10338. if (lvl_idx-1 >= 0 && lvl_idx != 99 && job_info[idx].base_sp[lvl_idx] < job_info[idx].base_sp[lvl_idx-1])
  10339. 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",
  10340. current,j+4,job_id,lvl_idx+1,job_info[idx].base_sp[lvl_idx-1],job_info[idx].base_sp[lvl_idx]);
  10341. }
  10342. }
  10343. }
  10344. }
  10345. return true;
  10346. }
  10347. #endif
  10348. /** [Cydh]
  10349. * Reads 'job_param_db.txt' to check max. param each job and store them to job_info[].max_param.*
  10350. */
  10351. static bool pc_readdb_job_param(char* fields[], int columns, int current)
  10352. {
  10353. int64 class_tmp;
  10354. int idx, class_;
  10355. uint16 str, agi, vit, int_, dex, luk;
  10356. script_get_constant(trim(fields[0]),&class_tmp);
  10357. class_ = static_cast<int>(class_tmp);
  10358. if ((idx = pc_class2idx(class_)) < 0) {
  10359. ShowError("pc_readdb_job_param: Invalid job '%s'. Skipping!",fields[0]);
  10360. return false;
  10361. }
  10362. str = cap_value(atoi(fields[1]),10,SHRT_MAX);
  10363. agi = atoi(fields[2]) ? cap_value(atoi(fields[2]),10,SHRT_MAX) : str;
  10364. vit = atoi(fields[3]) ? cap_value(atoi(fields[3]),10,SHRT_MAX) : str;
  10365. int_ = atoi(fields[4]) ? cap_value(atoi(fields[4]),10,SHRT_MAX) : str;
  10366. dex = atoi(fields[5]) ? cap_value(atoi(fields[5]),10,SHRT_MAX) : str;
  10367. luk = atoi(fields[6]) ? cap_value(atoi(fields[6]),10,SHRT_MAX) : str;
  10368. job_info[idx].max_param.str = str;
  10369. job_info[idx].max_param.agi = agi;
  10370. job_info[idx].max_param.vit = vit;
  10371. job_info[idx].max_param.int_ = int_;
  10372. job_info[idx].max_param.dex = dex;
  10373. job_info[idx].max_param.luk = luk;
  10374. return true;
  10375. }
  10376. /**
  10377. * Read job_noenter_map.txt
  10378. **/
  10379. static bool pc_readdb_job_noenter_map(char *str[], int columns, int current) {
  10380. int idx, class_ = -1;
  10381. int64 class_tmp;
  10382. if (ISDIGIT(str[0][0])) {
  10383. class_ = atoi(str[0]);
  10384. } else {
  10385. if (!script_get_constant(str[0], &class_tmp)) {
  10386. ShowError("pc_readdb_job_noenter_map: Invalid job %s specified.\n", str[0]);
  10387. return false;
  10388. }
  10389. class_ = static_cast<int>(class_tmp);
  10390. }
  10391. if (!pcdb_checkid(class_) || (idx = pc_class2idx(class_)) < 0) {
  10392. ShowError("pc_readdb_job_noenter_map: Invalid job %d specified.\n", class_);
  10393. return false;
  10394. }
  10395. job_info[idx].noenter_map.zone = atoi(str[1]);
  10396. job_info[idx].noenter_map.group_lv = atoi(str[2]);
  10397. return true;
  10398. }
  10399. static int pc_read_statsdb(const char *basedir, int last_s, bool silent){
  10400. int i=1;
  10401. char line[24000]; //FIXME this seem too big
  10402. FILE *fp;
  10403. sprintf(line, "%s/statpoint.txt", basedir);
  10404. fp=fopen(line,"r");
  10405. if(fp == NULL){
  10406. if(silent==0) ShowWarning("Can't read '" CL_WHITE "%s" CL_RESET "'... Generating DB.\n",line);
  10407. return max(last_s,i);
  10408. } else {
  10409. int entries=0;
  10410. while(fgets(line, sizeof(line), fp))
  10411. {
  10412. int stat;
  10413. trim(line);
  10414. if(line[0] == '\0' || (line[0]=='/' && line[1]=='/'))
  10415. continue;
  10416. if ((stat=strtoul(line,NULL,10))<0)
  10417. stat=0;
  10418. if (i > MAX_LEVEL)
  10419. break;
  10420. statp[i]=stat;
  10421. i++;
  10422. entries++;
  10423. }
  10424. fclose(fp);
  10425. ShowStatus("Done reading '" CL_WHITE "%d" CL_RESET "' entries in '" CL_WHITE "%s/%s" CL_RESET "'.\n", entries, basedir,"statpoint.txt");
  10426. }
  10427. return max(last_s,i);
  10428. }
  10429. /*==========================================
  10430. * pc DB reading.
  10431. * job_exp.txt - required experience values
  10432. * skill_tree.txt - skill tree for every class
  10433. * attr_fix.txt - elemental adjustment table
  10434. * job_db1.txt - job,weight,hp_factor,hp_multiplicator,sp_factor,aspds/lvl
  10435. * job_db2.txt - job,stats bonuses/lvl
  10436. * job_maxhpsp_db.txt - strtlvl,maxlvl,job,type,values/lvl (values=hp|sp)
  10437. *------------------------------------------*/
  10438. void pc_readdb(void) {
  10439. int i, k, s = 1;
  10440. const char* dbsubpath[] = {
  10441. "",
  10442. "/" DBIMPORT,
  10443. //add other path here
  10444. };
  10445. //reset
  10446. memset(job_info,0,sizeof(job_info)); // job_info table
  10447. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  10448. sv_readdb(db_path, DBPATH "level_penalty.txt", ',', 4, 4, -1, &pc_readdb_levelpenalty, 0);
  10449. sv_readdb(db_path, DBIMPORT"/level_penalty.txt", ',', 4, 4, -1, &pc_readdb_levelpenalty, 1);
  10450. for( k=1; k < 3; k++ ){ // fill in the blanks
  10451. int j;
  10452. for( j = 0; j < CLASS_ALL; j++ ){
  10453. int tmp = 0;
  10454. for( i = 0; i < MAX_LEVEL*2; i++ ){
  10455. if( i == MAX_LEVEL+1 )
  10456. tmp = level_penalty[k][j][0];// reset
  10457. if( level_penalty[k][j][i] > 0 )
  10458. tmp = level_penalty[k][j][i];
  10459. else
  10460. level_penalty[k][j][i] = tmp;
  10461. }
  10462. }
  10463. }
  10464. #endif
  10465. // reset then read statspoint
  10466. memset(statp,0,sizeof(statp));
  10467. for(i=0; i<ARRAYLENGTH(dbsubpath); i++){
  10468. uint8 n1 = (uint8)(strlen(db_path)+strlen(dbsubpath[i])+1);
  10469. uint8 n2 = (uint8)(strlen(db_path)+strlen(DBPATH)+strlen(dbsubpath[i])+1);
  10470. char* dbsubpath1 = (char*)aMalloc(n1+1);
  10471. char* dbsubpath2 = (char*)aMalloc(n2+1);
  10472. if(i==0) {
  10473. safesnprintf(dbsubpath1,n1,"%s%s",db_path,dbsubpath[i]);
  10474. safesnprintf(dbsubpath2,n2,"%s/%s%s",db_path,DBPATH,dbsubpath[i]);
  10475. }
  10476. else {
  10477. safesnprintf(dbsubpath1,n1,"%s%s",db_path,dbsubpath[i]);
  10478. safesnprintf(dbsubpath2,n1,"%s%s",db_path,dbsubpath[i]);
  10479. }
  10480. s = pc_read_statsdb(dbsubpath2,s,i > 0);
  10481. if (i == 0)
  10482. #ifdef RENEWAL_ASPD // Paths are hardcoded here to specifically pick the correct database
  10483. sv_readdb(dbsubpath1, "re/job_db1.txt",',',6+MAX_WEAPON_TYPE,6+MAX_WEAPON_TYPE,CLASS_COUNT,&pc_readdb_job1, false);
  10484. #else
  10485. sv_readdb(dbsubpath1, "pre-re/job_db1.txt",',',5+MAX_WEAPON_TYPE,5+MAX_WEAPON_TYPE,CLASS_COUNT,&pc_readdb_job1, false);
  10486. #endif
  10487. else
  10488. sv_readdb(dbsubpath1, "job_db1.txt",',',5+MAX_WEAPON_TYPE,6+MAX_WEAPON_TYPE,CLASS_COUNT,&pc_readdb_job1, true);
  10489. sv_readdb(dbsubpath1, "job_db2.txt",',',1,1+MAX_LEVEL,CLASS_COUNT,&pc_readdb_job2, i > 0);
  10490. sv_readdb(dbsubpath2, "job_exp.txt",',',4,1000+3,CLASS_COUNT*2,&pc_readdb_job_exp, i > 0); //support till 1000lvl
  10491. #ifdef HP_SP_TABLES
  10492. 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!
  10493. #endif
  10494. sv_readdb(dbsubpath2, "job_param_db.txt", ',', 2, PARAM_MAX+1, CLASS_COUNT, &pc_readdb_job_param, i > 0);
  10495. sv_readdb(dbsubpath2, "job_noenter_map.txt", ',', 3, 3, CLASS_COUNT, &pc_readdb_job_noenter_map, i > 0);
  10496. aFree(dbsubpath1);
  10497. aFree(dbsubpath2);
  10498. }
  10499. // Reset and read skilltree - needs to be read after pc_readdb_job_exp to get max base and job levels
  10500. memset(skill_tree, 0, sizeof(skill_tree));
  10501. 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);
  10502. 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);
  10503. // generate the remaining parts of the db if necessary
  10504. k = battle_config.use_statpoint_table; //save setting
  10505. battle_config.use_statpoint_table = 0; //temporarily disable to force pc_gets_status_point use default values
  10506. statp[0] = 45; // seed value
  10507. for (; s <= MAX_LEVEL; s++)
  10508. statp[s] = statp[s-1] + pc_gets_status_point(s-1);
  10509. battle_config.use_statpoint_table = k; //restore setting
  10510. //Checking if all class have their data
  10511. for (i = 0; i < JOB_MAX; i++) {
  10512. int idx;
  10513. uint16 j;
  10514. if (!pcdb_checkid(i))
  10515. continue;
  10516. if (i == JOB_WEDDING || i == JOB_XMAS || i == JOB_SUMMER || i == JOB_HANBOK || i == JOB_OKTOBERFEST || i == JOB_SUMMER2)
  10517. continue; //Classes that do not need exp tables.
  10518. idx = pc_class2idx(i);
  10519. if (!job_info[idx].max_level[0])
  10520. ShowWarning("Class %s (%d) does not have a base exp table.\n", job_name(i), i);
  10521. if (!job_info[idx].max_level[1])
  10522. ShowWarning("Class %s (%d) does not have a job exp table.\n", job_name(i), i);
  10523. //Init and checking the empty value of Base HP/SP [Cydh]
  10524. for (j = 0; j < (job_info[idx].max_level[0] ? job_info[idx].max_level[0] : MAX_LEVEL); j++) {
  10525. if (job_info[idx].base_hp[j] == 0)
  10526. job_info[idx].base_hp[j] = pc_calc_basehp(j+1,i);
  10527. if (job_info[idx].base_sp[j] == 0)
  10528. job_info[idx].base_sp[j] = pc_calc_basesp(j+1,i);
  10529. }
  10530. }
  10531. }
  10532. // Read MOTD on startup. [Valaris]
  10533. int pc_read_motd(void)
  10534. {
  10535. FILE* fp;
  10536. // clear old MOTD
  10537. memset(motd_text, 0, sizeof(motd_text));
  10538. // read current MOTD
  10539. if( ( fp = fopen(motd_txt, "r") ) != NULL )
  10540. {
  10541. unsigned int entries = 0;
  10542. char buf[CHAT_SIZE_MAX];
  10543. while( entries < MOTD_LINE_SIZE && fgets(buf, CHAT_SIZE_MAX, fp) )
  10544. {
  10545. unsigned int lines = 0;
  10546. size_t len;
  10547. lines++;
  10548. if( buf[0] == '/' && buf[1] == '/' )
  10549. continue;
  10550. len = strlen(buf);
  10551. while( len && ( buf[len-1] == '\r' || buf[len-1] == '\n' ) ) // strip trailing EOL characters
  10552. len--;
  10553. if( len ) {
  10554. char * ptr;
  10555. buf[len] = 0;
  10556. if( ( ptr = strstr(buf, " :") ) != NULL && ptr-buf >= NAME_LENGTH ) // crashes newer clients
  10557. 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);
  10558. }
  10559. else {// empty line
  10560. buf[0] = ' ';
  10561. buf[1] = 0;
  10562. }
  10563. safestrncpy(motd_text[entries], buf, CHAT_SIZE_MAX);
  10564. entries++;
  10565. }
  10566. fclose(fp);
  10567. ShowStatus("Done reading '" CL_WHITE "%u" CL_RESET "' entries in '" CL_WHITE "%s" CL_RESET "'.\n", entries, motd_txt);
  10568. }
  10569. else
  10570. ShowWarning("File '" CL_WHITE "%s" CL_RESET "' not found.\n", motd_txt);
  10571. return 0;
  10572. }
  10573. void pc_itemcd_do(struct map_session_data *sd, bool load) {
  10574. int i,cursor = 0;
  10575. struct item_cd* cd = NULL;
  10576. if( load ) {
  10577. if( !(cd = (struct item_cd*)idb_get(itemcd_db, sd->status.char_id)) ) {
  10578. // no item cooldown is associated with this character
  10579. return;
  10580. }
  10581. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  10582. if( cd->nameid[i] && DIFF_TICK(gettick(),cd->tick[i]) < 0 ) {
  10583. sd->item_delay[cursor].tick = cd->tick[i];
  10584. sd->item_delay[cursor].nameid = cd->nameid[i];
  10585. cursor++;
  10586. }
  10587. }
  10588. idb_remove(itemcd_db,sd->status.char_id);
  10589. } else {
  10590. if( !(cd = (struct item_cd*)idb_get(itemcd_db,sd->status.char_id)) ) {
  10591. // create a new skill cooldown object for map storage
  10592. CREATE( cd, struct item_cd, 1 );
  10593. idb_put( itemcd_db, sd->status.char_id, cd );
  10594. }
  10595. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  10596. if( sd->item_delay[i].nameid && DIFF_TICK(gettick(),sd->item_delay[i].tick) < 0 ) {
  10597. cd->tick[cursor] = sd->item_delay[i].tick;
  10598. cd->nameid[cursor] = sd->item_delay[i].nameid;
  10599. cursor++;
  10600. }
  10601. }
  10602. }
  10603. return;
  10604. }
  10605. /**
  10606. * Add item delay to player's item delay data
  10607. * @param sd Player
  10608. * @param id Item data
  10609. * @param tick Current tick
  10610. * @param n Item index in inventory
  10611. * @return 0: No delay, can consume item.
  10612. * 1: Has delay, cancel consumption.
  10613. **/
  10614. uint8 pc_itemcd_add(struct map_session_data *sd, struct item_data *id, t_tick tick, unsigned short n) {
  10615. int i;
  10616. ARR_FIND(0, MAX_ITEMDELAYS, i, sd->item_delay[i].nameid == id->nameid );
  10617. if( i == MAX_ITEMDELAYS ) /* item not found. try first empty now */
  10618. ARR_FIND(0, MAX_ITEMDELAYS, i, !sd->item_delay[i].nameid );
  10619. if( i < MAX_ITEMDELAYS ) {
  10620. if( sd->item_delay[i].nameid ) {// found
  10621. if( DIFF_TICK(sd->item_delay[i].tick, tick) > 0 ) {
  10622. t_tick e_tick = DIFF_TICK(sd->item_delay[i].tick, tick)/1000;
  10623. char e_msg[CHAT_SIZE_MAX];
  10624. if( e_tick > 99 )
  10625. sprintf(e_msg,msg_txt(sd,379), // Item Failed. [%s] is cooling down. Wait %.1f minutes.
  10626. itemdb_jname(sd->item_delay[i].nameid), (double)e_tick / 60);
  10627. else
  10628. sprintf(e_msg,msg_txt(sd,380), // Item Failed. [%s] is cooling down. Wait %d seconds.
  10629. itemdb_jname(sd->item_delay[i].nameid), e_tick+1);
  10630. clif_messagecolor(&sd->bl,color_table[COLOR_YELLOW],e_msg,false,SELF);
  10631. return 1; // Delay has not expired yet
  10632. }
  10633. } else {// not yet used item (all slots are initially empty)
  10634. sd->item_delay[i].nameid = id->nameid;
  10635. }
  10636. if( !(id->nameid == ITEMID_REINS_OF_MOUNT && sd->sc.option&(OPTION_WUGRIDER|OPTION_RIDING|OPTION_DRAGON|OPTION_MADOGEAR)) )
  10637. sd->item_delay[i].tick = tick + sd->inventory_data[n]->delay;
  10638. } else {// should not happen
  10639. ShowError("pc_itemcd_add: Exceeded item delay array capacity! (nameid=%hu, char_id=%d)\n", id->nameid, sd->status.char_id);
  10640. }
  10641. //clean up used delays so we can give room for more
  10642. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  10643. if( DIFF_TICK(sd->item_delay[i].tick, tick) <= 0 ) {
  10644. sd->item_delay[i].tick = 0;
  10645. sd->item_delay[i].nameid = 0;
  10646. }
  10647. }
  10648. return 0;
  10649. }
  10650. /**
  10651. * Check if player has delay to reuse item
  10652. * @param sd Player
  10653. * @param id Item data
  10654. * @param tick Current tick
  10655. * @param n Item index in inventory
  10656. * @return 0: No delay, can consume item.
  10657. * 1: Has delay, cancel consumption.
  10658. **/
  10659. uint8 pc_itemcd_check(struct map_session_data *sd, struct item_data *id, t_tick tick, unsigned short n) {
  10660. struct status_change *sc = NULL;
  10661. nullpo_retr(0, sd);
  10662. nullpo_retr(0, id);
  10663. // Do normal delay assignment
  10664. if (id->delay_sc <= SC_NONE || id->delay_sc >= SC_MAX || !(sc = &sd->sc))
  10665. return pc_itemcd_add(sd, id, tick, n);
  10666. // Send reply of delay remains
  10667. if (sc->data[id->delay_sc]) {
  10668. const struct TimerData *timer = get_timer(sc->data[id->delay_sc]->timer);
  10669. clif_msg_value(sd, ITEM_REUSE_LIMIT, (int)(timer ? DIFF_TICK(timer->tick, tick) / 1000 : 99));
  10670. return 1;
  10671. }
  10672. sc_start(&sd->bl, &sd->bl, (sc_type)id->delay_sc, 100, id->nameid, id->delay);
  10673. return 0;
  10674. }
  10675. /**
  10676. * Clear the dmglog data from player
  10677. * @param sd
  10678. * @param md
  10679. **/
  10680. static void pc_clear_log_damage_sub(uint32 char_id, struct mob_data *md)
  10681. {
  10682. uint8 i;
  10683. ARR_FIND(0,DAMAGELOG_SIZE,i,md->dmglog[i].id == char_id);
  10684. if (i < DAMAGELOG_SIZE) {
  10685. md->dmglog[i].id = 0;
  10686. md->dmglog[i].dmg = 0;
  10687. md->dmglog[i].flag = 0;
  10688. }
  10689. }
  10690. /**
  10691. * Add log to player's dmglog
  10692. * @param sd
  10693. * @param id Monster's GID
  10694. **/
  10695. void pc_damage_log_add(struct map_session_data *sd, int id)
  10696. {
  10697. uint8 i = 0;
  10698. if (!sd || !id)
  10699. return;
  10700. //Only store new data, don't need to renew the old one with same id
  10701. ARR_FIND(0, DAMAGELOG_SIZE_PC, i, sd->dmglog[i] == id);
  10702. if (i < DAMAGELOG_SIZE_PC)
  10703. return;
  10704. for (i = 0; i < DAMAGELOG_SIZE_PC; i++) {
  10705. if (sd->dmglog[i] == 0) {
  10706. sd->dmglog[i] = id;
  10707. return;
  10708. }
  10709. }
  10710. }
  10711. /**
  10712. * Clear dmglog data from player
  10713. * @param sd
  10714. * @param id Monster's id
  10715. **/
  10716. void pc_damage_log_clear(struct map_session_data *sd, int id)
  10717. {
  10718. uint8 i;
  10719. struct mob_data *md = NULL;
  10720. if (!sd)
  10721. return;
  10722. if (!id) {
  10723. for (i = 0; i < DAMAGELOG_SIZE_PC; i++) {
  10724. if( !sd->dmglog[i] ) //skip the empty value
  10725. continue;
  10726. if ((md = map_id2md(sd->dmglog[i])))
  10727. pc_clear_log_damage_sub(sd->status.char_id,md);
  10728. sd->dmglog[i] = 0;
  10729. }
  10730. }
  10731. else {
  10732. if ((md = map_id2md(id)))
  10733. pc_clear_log_damage_sub(sd->status.char_id,md);
  10734. ARR_FIND(0,DAMAGELOG_SIZE_PC,i,sd->dmglog[i] == id); // find the id position
  10735. if (i < DAMAGELOG_SIZE_PC)
  10736. sd->dmglog[i] = 0;
  10737. }
  10738. }
  10739. /**
  10740. * Status change data arrived from char-server
  10741. * @param sd: Player data
  10742. */
  10743. void pc_scdata_received(struct map_session_data *sd) {
  10744. pc_inventory_rentals(sd); // Needed here to remove rentals that have Status Changes after chrif_load_scdata has finished
  10745. clif_weight_limit( sd );
  10746. if( pc_has_permission( sd, PC_PERM_ATTENDANCE ) && pc_attendance_enabled() && !pc_attendance_rewarded_today( sd ) ){
  10747. clif_ui_open( sd, OUT_UI_ATTENDANCE, pc_attendance_counter( sd ) );
  10748. }
  10749. sd->state.pc_loaded = true;
  10750. if (sd->state.connect_new == 0 && sd->fd) { // Character already loaded map! Gotta trigger LoadEndAck manually.
  10751. sd->state.connect_new = 1;
  10752. clif_parse_LoadEndAck(sd->fd, sd);
  10753. }
  10754. if (pc_iscarton(sd)) {
  10755. sd->cart_weight_max = 0; // Force a client refesh
  10756. status_calc_cart_weight(sd, (e_status_calc_weight_opt)(CALCWT_ITEM|CALCWT_MAXBONUS|CALCWT_CARTSTATE));
  10757. }
  10758. }
  10759. /**
  10760. * Check player account expiration time and rental item expirations
  10761. * @param sd: Player data
  10762. */
  10763. void pc_check_expiration(struct map_session_data *sd) {
  10764. #ifndef ENABLE_SC_SAVING
  10765. pc_inventory_rentals(sd); // Check here if Status Change saving is disabled
  10766. #endif
  10767. if (sd->expiration_time != 0) { //Don't display if it's unlimited or unknow value
  10768. time_t exp_time = sd->expiration_time;
  10769. char tmpstr[1024];
  10770. strftime(tmpstr,sizeof(tmpstr) - 1,msg_txt(sd,501),localtime(&exp_time)); // "Your account time limit is: %d-%m-%Y %H:%M:%S."
  10771. clif_wis_message(sd,wisp_server_name,tmpstr,strlen(tmpstr) + 1,0);
  10772. pc_expire_check(sd);
  10773. }
  10774. }
  10775. TIMER_FUNC(pc_expiration_timer){
  10776. struct map_session_data *sd = map_id2sd(id);
  10777. if( !sd ) return 0;
  10778. sd->expiration_tid = INVALID_TIMER;
  10779. if( sd->fd )
  10780. clif_authfail_fd(sd->fd,10);
  10781. map_quit(sd);
  10782. return 0;
  10783. }
  10784. TIMER_FUNC(pc_autotrade_timer){
  10785. struct map_session_data *sd = map_id2sd(id);
  10786. if (!sd)
  10787. return 0;
  10788. sd->autotrade_tid = INVALID_TIMER;
  10789. if (sd->state.autotrade&2)
  10790. vending_reopen(sd);
  10791. if (sd->state.autotrade&4)
  10792. buyingstore_reopen(sd);
  10793. if (!sd->vender_id && !sd->buyer_id) {
  10794. sd->state.autotrade = 0;
  10795. map_quit(sd);
  10796. }
  10797. return 0;
  10798. }
  10799. /* this timer exists only when a character with a expire timer > 24h is online */
  10800. /* it loops thru online players once an hour to check whether a new < 24h is available */
  10801. TIMER_FUNC(pc_global_expiration_timer){
  10802. struct s_mapiterator* iter;
  10803. struct map_session_data* sd;
  10804. iter = mapit_getallusers();
  10805. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  10806. if( sd->expiration_time )
  10807. pc_expire_check(sd);
  10808. mapit_free(iter);
  10809. return 0;
  10810. }
  10811. void pc_expire_check(struct map_session_data *sd) {
  10812. /* ongoing timer */
  10813. if( sd->expiration_tid != INVALID_TIMER )
  10814. return;
  10815. /* not within the next 24h, enable the global check */
  10816. if( sd->expiration_time > (time(NULL) + ((60 * 60) * 24)) ) {
  10817. /* global check not running, enable */
  10818. if( pc_expiration_tid == INVALID_TIMER ) /* Starts in 1h, repeats every hour */
  10819. pc_expiration_tid = add_timer_interval(gettick() + ((1000 * 60) * 60), pc_global_expiration_timer, 0, 0, ((1000 * 60) * 60));
  10820. return;
  10821. }
  10822. sd->expiration_tid = add_timer(gettick() + (unsigned int)(sd->expiration_time - time(NULL)) * 1000, pc_expiration_timer, sd->bl.id, 0);
  10823. }
  10824. /**
  10825. * Deposit some money to bank
  10826. * @param sd
  10827. * @param money Amount of money to deposit
  10828. **/
  10829. enum e_BANKING_DEPOSIT_ACK pc_bank_deposit(struct map_session_data *sd, int money) {
  10830. unsigned int limit_check = money + sd->bank_vault;
  10831. if( money <= 0 || limit_check > MAX_BANK_ZENY ) {
  10832. return BDA_OVERFLOW;
  10833. } else if ( money > sd->status.zeny ) {
  10834. return BDA_NO_MONEY;
  10835. }
  10836. if( pc_payzeny(sd,money, LOG_TYPE_BANK, NULL) )
  10837. return BDA_NO_MONEY;
  10838. sd->bank_vault += money;
  10839. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  10840. if( save_settings&CHARSAVE_BANK )
  10841. chrif_save(sd, CSAVE_NORMAL);
  10842. return BDA_SUCCESS;
  10843. }
  10844. /**
  10845. * Withdraw money from bank
  10846. * @param sd
  10847. * @param money Amount of money that will be withdrawn
  10848. **/
  10849. enum e_BANKING_WITHDRAW_ACK pc_bank_withdraw(struct map_session_data *sd, int money) {
  10850. unsigned int limit_check = money + sd->status.zeny;
  10851. if( money <= 0 ) {
  10852. return BWA_UNKNOWN_ERROR;
  10853. } else if ( money > sd->bank_vault ) {
  10854. return BWA_NO_MONEY;
  10855. } else if ( limit_check > MAX_ZENY ) {
  10856. /* no official response for this scenario exists. */
  10857. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,1495),false,SELF); //You can't withdraw that much money
  10858. return BWA_UNKNOWN_ERROR;
  10859. }
  10860. if( pc_getzeny(sd,money, LOG_TYPE_BANK, NULL) )
  10861. return BWA_NO_MONEY;
  10862. sd->bank_vault -= money;
  10863. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  10864. if( save_settings&CHARSAVE_BANK )
  10865. chrif_save(sd, CSAVE_NORMAL);
  10866. return BWA_SUCCESS;
  10867. }
  10868. /**
  10869. * Clear Crimson Marker data from caster
  10870. * @param sd: Player
  10871. **/
  10872. void pc_crimson_marker_clear(struct map_session_data *sd) {
  10873. uint8 i;
  10874. if (!sd)
  10875. return;
  10876. for (i = 0; i < MAX_SKILL_CRIMSON_MARKER; i++) {
  10877. struct block_list *bl = NULL;
  10878. if (sd->c_marker[i] && (bl = map_id2bl(sd->c_marker[i])))
  10879. status_change_end(bl,SC_C_MARKER,INVALID_TIMER);
  10880. sd->c_marker[i] = 0;
  10881. }
  10882. }
  10883. /**
  10884. * Show version to player
  10885. * @param sd: Player
  10886. **/
  10887. void pc_show_version(struct map_session_data *sd) {
  10888. const char* svn = get_svn_revision();
  10889. char buf[CHAT_SIZE_MAX];
  10890. if( svn[0] != UNKNOWN_VERSION )
  10891. sprintf(buf,msg_txt(sd,1295),"SVN: r",svn); //rAthena Version SVN: r%s
  10892. else {
  10893. const char* git = get_git_hash();
  10894. if( git[0] != UNKNOWN_VERSION )
  10895. sprintf(buf,msg_txt(sd,1295),"Git Hash: ",git); //rAthena Version Git Hash: %s
  10896. else
  10897. sprintf(buf,"%s",msg_txt(sd,1296)); //Cannot determine SVN/Git version.
  10898. }
  10899. clif_displaymessage(sd->fd,buf);
  10900. }
  10901. /**
  10902. * Run bonus_script on player
  10903. * @param sd
  10904. * @author [Cydh]
  10905. **/
  10906. void pc_bonus_script(struct map_session_data *sd) {
  10907. t_tick now = gettick();
  10908. struct linkdb_node *node = NULL, *next = NULL;
  10909. if (!sd || !(node = sd->bonus_script.head))
  10910. return;
  10911. while (node) {
  10912. struct s_bonus_script_entry *entry = NULL;
  10913. next = node->next;
  10914. if ((entry = (struct s_bonus_script_entry *)node->data)) {
  10915. // Only start timer for new bonus_script
  10916. if (entry->tid == INVALID_TIMER) {
  10917. if (entry->icon != EFST_BLANK) // Gives status icon if exist
  10918. clif_status_change(&sd->bl, entry->icon, 1, entry->tick, 1, 0, 0);
  10919. entry->tick += now;
  10920. entry->tid = add_timer(entry->tick, pc_bonus_script_timer, sd->bl.id, (intptr_t)entry);
  10921. }
  10922. if (entry->script)
  10923. run_script(entry->script, 0, sd->bl.id, 0);
  10924. else
  10925. ShowError("pc_bonus_script: The script has been removed somewhere. \"%s\"\n", StringBuf_Value(entry->script_buf));
  10926. }
  10927. node = next;
  10928. }
  10929. }
  10930. /**
  10931. * Add bonus_script to player
  10932. * @param sd Player
  10933. * @param script_str Script string
  10934. * @param dur Duration in ms
  10935. * @param icon EFST
  10936. * @param flag Flags @see enum e_bonus_script_flags
  10937. * @param type 0 - None, 1 - Buff, 2 - Debuff
  10938. * @return New created entry pointer or NULL if failed or NULL if duplicate fail
  10939. * @author [Cydh]
  10940. **/
  10941. 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) {
  10942. struct script_code *script = NULL;
  10943. struct linkdb_node *node = NULL;
  10944. struct s_bonus_script_entry *entry = NULL;
  10945. if (!sd)
  10946. return NULL;
  10947. if (!(script = parse_script(script_str, "bonus_script", 0, SCRIPT_IGNORE_EXTERNAL_BRACKETS))) {
  10948. ShowError("pc_bonus_script_add: Failed to parse script '%s' (CID:%d).\n", script_str, sd->status.char_id);
  10949. return NULL;
  10950. }
  10951. // Duplication checks
  10952. if ((node = sd->bonus_script.head)) {
  10953. while (node) {
  10954. entry = (struct s_bonus_script_entry *)node->data;
  10955. if (strcmpi(script_str, StringBuf_Value(entry->script_buf)) == 0) {
  10956. t_tick newdur = gettick() + dur;
  10957. if (flag&BSF_FORCE_REPLACE && entry->tick < newdur) { // Change duration
  10958. sett_tickimer(entry->tid, newdur);
  10959. script_free_code(script);
  10960. return NULL;
  10961. }
  10962. else if (flag&BSF_FORCE_DUPLICATE) // Allow duplicate
  10963. break;
  10964. else { // No duplicate bonus
  10965. script_free_code(script);
  10966. return NULL;
  10967. }
  10968. }
  10969. node = node->next;
  10970. }
  10971. }
  10972. CREATE(entry, struct s_bonus_script_entry, 1);
  10973. entry->script_buf = StringBuf_Malloc();
  10974. StringBuf_AppendStr(entry->script_buf, script_str);
  10975. entry->tid = INVALID_TIMER;
  10976. entry->flag = flag;
  10977. entry->icon = icon;
  10978. entry->tick = dur; // Use duration first, on run change to expire time
  10979. entry->type = type;
  10980. entry->script = script;
  10981. sd->bonus_script.count++;
  10982. return entry;
  10983. }
  10984. /**
  10985. * Remove bonus_script data from player
  10986. * @param sd: Target player
  10987. * @param list: Bonus script entry from player
  10988. * @author [Cydh]
  10989. **/
  10990. void pc_bonus_script_free_entry(struct map_session_data *sd, struct s_bonus_script_entry *entry) {
  10991. if (entry->tid != INVALID_TIMER)
  10992. delete_timer(entry->tid, pc_bonus_script_timer);
  10993. if (entry->script)
  10994. script_free_code(entry->script);
  10995. if (entry->script_buf)
  10996. StringBuf_Free(entry->script_buf);
  10997. if (sd) {
  10998. if (entry->icon != EFST_BLANK)
  10999. clif_status_load(&sd->bl, entry->icon, 0);
  11000. if (sd->bonus_script.count > 0)
  11001. sd->bonus_script.count--;
  11002. }
  11003. aFree(entry);
  11004. }
  11005. /**
  11006. * Do final process if no entry left
  11007. * @param sd
  11008. **/
  11009. static void inline pc_bonus_script_check_final(struct map_session_data *sd) {
  11010. if (sd->bonus_script.count == 0) {
  11011. if (sd->bonus_script.head && sd->bonus_script.head->data)
  11012. pc_bonus_script_free_entry(sd, (struct s_bonus_script_entry *)sd->bonus_script.head->data);
  11013. linkdb_final(&sd->bonus_script.head);
  11014. }
  11015. }
  11016. /**
  11017. * Timer for bonus_script
  11018. * @param tid
  11019. * @param tick
  11020. * @param id
  11021. * @param data
  11022. * @author [Cydh]
  11023. **/
  11024. TIMER_FUNC(pc_bonus_script_timer){
  11025. struct map_session_data *sd;
  11026. struct s_bonus_script_entry *entry = (struct s_bonus_script_entry *)data;
  11027. sd = map_id2sd(id);
  11028. if (!sd) {
  11029. ShowError("pc_bonus_script_timer: Null pointer id: %d tid: %d\n", id, tid);
  11030. return 0;
  11031. }
  11032. if (tid == INVALID_TIMER)
  11033. return 0;
  11034. if (!sd->bonus_script.head || entry == NULL) {
  11035. ShowError("pc_bonus_script_timer: Invalid entry pointer %p!\n", entry);
  11036. return 0;
  11037. }
  11038. linkdb_erase(&sd->bonus_script.head, (void *)((intptr_t)entry));
  11039. pc_bonus_script_free_entry(sd, entry);
  11040. pc_bonus_script_check_final(sd);
  11041. status_calc_pc(sd,SCO_NONE);
  11042. return 0;
  11043. }
  11044. /**
  11045. * Check then clear all active timer(s) of bonus_script data from player based on reason
  11046. * @param sd: Target player
  11047. * @param flag: Reason to remove the bonus_script. e_bonus_script_flags or e_bonus_script_types
  11048. * @author [Cydh]
  11049. **/
  11050. void pc_bonus_script_clear(struct map_session_data *sd, uint16 flag) {
  11051. struct linkdb_node *node = NULL;
  11052. uint16 count = 0;
  11053. if (!sd || !(node = sd->bonus_script.head))
  11054. return;
  11055. while (node) {
  11056. struct linkdb_node *next = node->next;
  11057. struct s_bonus_script_entry *entry = (struct s_bonus_script_entry *)node->data;
  11058. if (entry && (
  11059. (flag == BSF_PERMANENT) || // Remove all with permanent bonus
  11060. (!flag && !(entry->flag&BSF_PERMANENT)) || // Remove all WITHOUT permanent bonus
  11061. (flag&entry->flag) || // Matched flag
  11062. (flag&BSF_REM_BUFF && entry->type == 1) || // Remove buff
  11063. (flag&BSF_REM_DEBUFF && entry->type == 2) // Remove debuff
  11064. )
  11065. )
  11066. {
  11067. linkdb_erase(&sd->bonus_script.head, (void *)((intptr_t)entry));
  11068. pc_bonus_script_free_entry(sd, entry);
  11069. count++;
  11070. }
  11071. node = next;
  11072. }
  11073. pc_bonus_script_check_final(sd);
  11074. if (count && !(flag&BSF_REM_ON_LOGOUT)) //Don't need to do this if log out
  11075. status_calc_pc(sd,SCO_NONE);
  11076. }
  11077. /** [Cydh]
  11078. * Gives/removes SC_BASILICA when player steps in/out the cell with 'cell_basilica'
  11079. * @param sd: Target player
  11080. */
  11081. void pc_cell_basilica(struct map_session_data *sd) {
  11082. nullpo_retv(sd);
  11083. if (!map_getcell(sd->bl.m,sd->bl.x,sd->bl.y,CELL_CHKBASILICA)) {
  11084. if (sd->sc.data[SC_BASILICA])
  11085. status_change_end(&sd->bl,SC_BASILICA,INVALID_TIMER);
  11086. }
  11087. else if (!sd->sc.data[SC_BASILICA])
  11088. sc_start(&sd->bl,&sd->bl,SC_BASILICA,100,0,INFINITE_TICK);
  11089. }
  11090. /** [Cydh]
  11091. * Get maximum specified parameter for specified class
  11092. * @param class_: sd->class
  11093. * @param sex: sd->status.sex
  11094. * @param flag: parameter will be checked
  11095. * @return max_param
  11096. */
  11097. short pc_maxparameter(struct map_session_data *sd, enum e_params param) {
  11098. int idx = -1, class_ = sd->class_;
  11099. if ((idx = pc_class2idx(pc_mapid2jobid(class_,sd->status.sex))) >= 0) {
  11100. short max_param = 0;
  11101. switch (param) {
  11102. case PARAM_STR: max_param = job_info[idx].max_param.str; break;
  11103. case PARAM_AGI: max_param = job_info[idx].max_param.agi; break;
  11104. case PARAM_VIT: max_param = job_info[idx].max_param.vit; break;
  11105. case PARAM_INT: max_param = job_info[idx].max_param.int_; break;
  11106. case PARAM_DEX: max_param = job_info[idx].max_param.dex; break;
  11107. case PARAM_LUK: max_param = job_info[idx].max_param.luk; break;
  11108. }
  11109. if (max_param > 0)
  11110. return max_param;
  11111. }
  11112. // Always check babies first
  11113. if( class_ & JOBL_BABY ){
  11114. if( class_ & JOBL_THIRD ){
  11115. return battle_config.max_baby_third_parameter;
  11116. }else{
  11117. return battle_config.max_baby_parameter;
  11118. }
  11119. }
  11120. // Summoner
  11121. if( ( class_ & MAPID_BASEMASK ) == MAPID_SUMMONER ){
  11122. return battle_config.max_summoner_parameter;
  11123. }
  11124. // Extended classes
  11125. if( ( class_ & MAPID_UPPERMASK ) == MAPID_KAGEROUOBORO || ( class_ & MAPID_UPPERMASK ) == MAPID_REBELLION ){
  11126. return battle_config.max_extended_parameter;
  11127. }
  11128. // 3rd class
  11129. if( class_ & JOBL_THIRD ){
  11130. // Transcendent
  11131. if( class_ & JOBL_UPPER ){
  11132. return battle_config.max_third_trans_parameter;
  11133. }else{
  11134. return battle_config.max_third_parameter;
  11135. }
  11136. }
  11137. // Transcendent
  11138. if( class_ & JOBL_UPPER ){
  11139. return battle_config.max_trans_parameter;
  11140. }
  11141. return battle_config.max_parameter;
  11142. }
  11143. /**
  11144. * Get max ASPD for player based on Class
  11145. * @param sd Player
  11146. * @return ASPD
  11147. */
  11148. short pc_maxaspd(struct map_session_data *sd) {
  11149. nullpo_ret(sd);
  11150. return (( sd->class_&JOBL_THIRD) ? battle_config.max_third_aspd : (
  11151. ((sd->class_&MAPID_UPPERMASK) == MAPID_KAGEROUOBORO || (sd->class_&MAPID_UPPERMASK) == MAPID_REBELLION) ? battle_config.max_extended_aspd :
  11152. battle_config.max_aspd ));
  11153. }
  11154. /**
  11155. * Calculates total item-group related bonuses for the given item
  11156. * @param sd Player
  11157. * @param nameid Item ID
  11158. * @return Heal rate
  11159. **/
  11160. short pc_get_itemgroup_bonus(struct map_session_data* sd, unsigned short nameid) {
  11161. if (sd->itemgrouphealrate.empty())
  11162. return 0;
  11163. short bonus = 0;
  11164. for (const auto &it : sd->itemgrouphealrate) {
  11165. uint16 group_id = it.id, i;
  11166. struct s_item_group_db *group = NULL;
  11167. if (!group_id || !(group = itemdb_group_exists(group_id)))
  11168. continue;
  11169. for (i = 0; i < group->random[0].data_qty; i++) {
  11170. if (group->random[0].data[i].nameid == nameid) {
  11171. bonus += it.val;
  11172. break;
  11173. }
  11174. }
  11175. }
  11176. return bonus;
  11177. }
  11178. /**
  11179. * Calculates total item-group related bonuses for the given item group
  11180. * @param sd Player
  11181. * @param group_id Item Group ID
  11182. * @return Heal rate
  11183. **/
  11184. short pc_get_itemgroup_bonus_group(struct map_session_data* sd, uint16 group_id) {
  11185. if (sd->itemgrouphealrate.empty())
  11186. return 0;
  11187. for (const auto &it : sd->itemgrouphealrate) {
  11188. if (it.id == group_id)
  11189. return it.val;
  11190. }
  11191. return 0;
  11192. }
  11193. /**
  11194. * Check if player's equip index in same specified position, like for 2-Handed weapon & Heagdear (inc. costume)
  11195. * @param eqi Item EQI of enum equip_index
  11196. * @param *equip_index Player's equip_index[]
  11197. * @param index Known index item in inventory from sd->equip_index[] to compare with specified EQI in *equip_index
  11198. * @return True if item in same inventory index, False if doesn't
  11199. */
  11200. bool pc_is_same_equip_index(enum equip_index eqi, short *equip_index, short index) {
  11201. if (index < 0 || index >= MAX_INVENTORY)
  11202. return true;
  11203. // Dual weapon checks
  11204. if (eqi == EQI_HAND_R && equip_index[EQI_HAND_L] == index)
  11205. return true;
  11206. // Headgear with Mid & Low location
  11207. if (eqi == EQI_HEAD_MID && equip_index[EQI_HEAD_LOW] == index)
  11208. return true;
  11209. // Headgear with Top & Mid or Low location
  11210. if (eqi == EQI_HEAD_TOP && (equip_index[EQI_HEAD_MID] == index || equip_index[EQI_HEAD_LOW] == index))
  11211. return true;
  11212. // Headgear with Mid & Low location
  11213. if (eqi == EQI_COSTUME_HEAD_MID && equip_index[EQI_COSTUME_HEAD_LOW] == index)
  11214. return true;
  11215. // Headgear with Top & Mid or Low location
  11216. if (eqi == EQI_COSTUME_HEAD_TOP && (equip_index[EQI_COSTUME_HEAD_MID] == index || equip_index[EQI_COSTUME_HEAD_LOW] == index))
  11217. return true;
  11218. return false;
  11219. }
  11220. /**
  11221. * Generate Unique item ID for player
  11222. * @param sd : Player
  11223. * @return A generated Unique item ID
  11224. */
  11225. uint64 pc_generate_unique_id(struct map_session_data *sd) {
  11226. nullpo_ret(sd);
  11227. return ((uint64)sd->status.char_id << 32) | sd->status.uniqueitem_counter++;
  11228. }
  11229. /**
  11230. * Validating skill from player after logged on
  11231. * @param sd
  11232. **/
  11233. void pc_validate_skill(struct map_session_data *sd) {
  11234. if (sd) {
  11235. uint16 i = 0, count = 0;
  11236. struct s_skill tmp_skills[MAX_SKILL] = {{ 0 }};
  11237. memcpy(tmp_skills, sd->status.skill, sizeof(sd->status.skill));
  11238. memset(sd->status.skill, 0, sizeof(sd->status.skill));
  11239. for (i = 0; i < MAX_SKILL; i++) {
  11240. uint16 idx = 0;
  11241. if (tmp_skills[i].id == 0 || tmp_skills[i].lv == 0)
  11242. continue;
  11243. if ((idx = skill_get_index(tmp_skills[i].id))) {
  11244. memcpy(&sd->status.skill[idx], &tmp_skills[i], sizeof(tmp_skills[i]));
  11245. count++;
  11246. }
  11247. else
  11248. 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);
  11249. }
  11250. }
  11251. }
  11252. /**
  11253. * Toggle to remember if the questinfo is displayed yet or not.
  11254. * @param qi_display Display flag
  11255. * @param show If show is true and qi_display is 0, set qi_display to 1 and show the event bubble.
  11256. * If show is false and qi_display is 1, set qi_display to 0 and hide the event bubble.
  11257. **/
  11258. static void pc_show_questinfo_sub(struct map_session_data *sd, bool *qi_display, struct s_questinfo *qi, bool show) {
  11259. if (show) {
  11260. // Check if need to be displayed
  11261. if ((*qi_display) != 1) {
  11262. (*qi_display) = 1;
  11263. clif_quest_show_event(sd, &qi->nd->bl, qi->icon, qi->color);
  11264. }
  11265. }
  11266. else {
  11267. // Check if need to be hide
  11268. if ((*qi_display) != 0) {
  11269. (*qi_display) = 0;
  11270. #if PACKETVER >= 20120410
  11271. clif_quest_show_event(sd, &qi->nd->bl, QTYPE_NONE, QMARK_NONE);
  11272. #else
  11273. clif_quest_show_event(sd, &qi->nd->bl, QTYPE_QUEST, QMARK_NONE);
  11274. #endif
  11275. }
  11276. }
  11277. }
  11278. /**
  11279. * Show available NPC Quest / Event Icon Check [Kisuka]
  11280. * @param sd Player
  11281. **/
  11282. void pc_show_questinfo(struct map_session_data *sd) {
  11283. #if PACKETVER >= 20090218
  11284. nullpo_retv(sd);
  11285. if (sd->bl.m < 0 || sd->bl.m >= MAX_MAPINDEX)
  11286. return;
  11287. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  11288. nullpo_retv(mapdata);
  11289. if (mapdata->qi_data.empty())
  11290. return;
  11291. if (mapdata->qi_data.size() != sd->qi_count)
  11292. return; // init was not called yet
  11293. struct s_questinfo *qi = nullptr;
  11294. bool show;
  11295. for (int i = 0; i < mapdata->qi_data.size(); i++) {
  11296. qi = &mapdata->qi_data[i];
  11297. if (!qi)
  11298. continue;
  11299. if (!qi->condition)
  11300. show = true;
  11301. else {
  11302. if (achievement_check_condition(qi->condition, sd))
  11303. show = true;
  11304. else
  11305. show = false;
  11306. }
  11307. pc_show_questinfo_sub(sd, &sd->qi_display[i], qi, show);
  11308. }
  11309. #endif
  11310. }
  11311. /**
  11312. * Reinit the questinfo for player when changing map
  11313. * @param sd Player
  11314. **/
  11315. void pc_show_questinfo_reinit(struct map_session_data *sd) {
  11316. #if PACKETVER >= 20090218
  11317. nullpo_retv(sd);
  11318. if (sd->qi_display) {
  11319. aFree(sd->qi_display);
  11320. sd->qi_display = nullptr;
  11321. }
  11322. sd->qi_count = 0;
  11323. if (sd->bl.m < 0 || sd->bl.m >= MAX_MAPINDEX)
  11324. return;
  11325. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  11326. nullpo_retv(mapdata);
  11327. if (mapdata->qi_data.empty())
  11328. return;
  11329. CREATE(sd->qi_display, bool, (sd->qi_count = mapdata->qi_data.size()));
  11330. #endif
  11331. }
  11332. /**
  11333. * Check if a job is allowed to enter the map
  11334. * @param jobid Job ID see enum e_job or sd->status.class_
  11335. * @param m ID -an index- for direct indexing map[] array
  11336. * @return 1 if job is allowed, 0 otherwise
  11337. **/
  11338. bool pc_job_can_entermap(enum e_job jobid, int m, int group_lv) {
  11339. uint16 idx = 0;
  11340. // Map is other map server.
  11341. // !FIXME: Currently, a map-server doesn't recognized map's attributes on other server, so we assume it's fine to warp.
  11342. if (m < 0)
  11343. return true;
  11344. struct map_data *mapdata = map_getmapdata(m);
  11345. if (!mapdata->cell)
  11346. return false;
  11347. if (!pcdb_checkid(jobid))
  11348. return false;
  11349. idx = pc_class2idx(jobid);
  11350. if (!job_info[idx].noenter_map.zone || group_lv > job_info[idx].noenter_map.group_lv)
  11351. return true;
  11352. if ((job_info[idx].noenter_map.zone&1 && !mapdata_flag_vs2(mapdata)) || // Normal
  11353. (job_info[idx].noenter_map.zone&2 && mapdata->flag[MF_PVP]) || // PVP
  11354. (job_info[idx].noenter_map.zone&4 && mapdata_flag_gvg2_no_te(mapdata)) || // GVG
  11355. (job_info[idx].noenter_map.zone&8 && mapdata->flag[MF_BATTLEGROUND]) || // Battleground
  11356. (job_info[idx].noenter_map.zone&16 && mapdata_flag_gvg2_te(mapdata)) || // WOE:TE
  11357. (job_info[idx].noenter_map.zone&(mapdata->zone) && mapdata->flag[MF_RESTRICTED]) // Zone restriction
  11358. )
  11359. return false;
  11360. return true;
  11361. }
  11362. /**
  11363. * Tells client about player's costume view on mapchange for checking 'nocostume' mapflag.
  11364. * @param sd
  11365. **/
  11366. void pc_set_costume_view(struct map_session_data *sd) {
  11367. int i = -1, head_low = 0, head_mid = 0, head_top = 0, robe = 0;
  11368. struct item_data *id = NULL;
  11369. nullpo_retv(sd);
  11370. head_low = sd->status.head_bottom;
  11371. head_mid = sd->status.head_mid;
  11372. head_top = sd->status.head_top;
  11373. robe = sd->status.robe;
  11374. sd->status.head_bottom = sd->status.head_mid = sd->status.head_top = sd->status.robe = 0;
  11375. //Added check to prevent sending the same look on multiple slots ->
  11376. //causes client to redraw item on top of itself. (suggested by Lupus)
  11377. // Normal headgear checks
  11378. if ((i = sd->equip_index[EQI_HEAD_LOW]) != -1 && (id = sd->inventory_data[i])) {
  11379. if (!(id->equip&(EQP_HEAD_MID|EQP_HEAD_TOP)))
  11380. sd->status.head_bottom = id->look;
  11381. else
  11382. sd->status.head_bottom = 0;
  11383. }
  11384. if ((i = sd->equip_index[EQI_HEAD_MID]) != -1 && (id = sd->inventory_data[i])) {
  11385. if (!(id->equip&(EQP_HEAD_TOP)))
  11386. sd->status.head_mid = id->look;
  11387. else
  11388. sd->status.head_mid = 0;
  11389. }
  11390. if ((i = sd->equip_index[EQI_HEAD_TOP]) != -1 && (id = sd->inventory_data[i]))
  11391. sd->status.head_top = id->look;
  11392. if ((i = sd->equip_index[EQI_GARMENT]) != -1 && (id = sd->inventory_data[i]))
  11393. sd->status.robe = id->look;
  11394. // Costumes check
  11395. if (!map_getmapflag(sd->bl.m, MF_NOCOSTUME)) {
  11396. if ((i = sd->equip_index[EQI_COSTUME_HEAD_LOW]) != -1 && (id = sd->inventory_data[i])) {
  11397. if (!(id->equip&(EQP_COSTUME_HEAD_MID|EQP_COSTUME_HEAD_TOP)))
  11398. sd->status.head_bottom = id->look;
  11399. else
  11400. sd->status.head_bottom = 0;
  11401. }
  11402. if ((i = sd->equip_index[EQI_COSTUME_HEAD_MID]) != -1 && (id = sd->inventory_data[i])) {
  11403. if (!(id->equip&EQP_COSTUME_HEAD_TOP))
  11404. sd->status.head_mid = id->look;
  11405. else
  11406. sd->status.head_mid = 0;
  11407. }
  11408. if ((i = sd->equip_index[EQI_COSTUME_HEAD_TOP]) != -1 && (id = sd->inventory_data[i]))
  11409. sd->status.head_top = id->look;
  11410. if ((i = sd->equip_index[EQI_COSTUME_GARMENT]) != -1 && (id = sd->inventory_data[i]))
  11411. sd->status.robe = id->look;
  11412. }
  11413. if (sd->setlook_head_bottom)
  11414. sd->status.head_bottom = sd->setlook_head_bottom;
  11415. if (sd->setlook_head_mid)
  11416. sd->status.head_mid = sd->setlook_head_mid;
  11417. if (sd->setlook_head_top)
  11418. sd->status.head_top = sd->setlook_head_top;
  11419. if (sd->setlook_robe)
  11420. sd->status.robe = sd->setlook_robe;
  11421. if (head_low != sd->status.head_bottom)
  11422. clif_changelook(&sd->bl, LOOK_HEAD_BOTTOM, sd->status.head_bottom);
  11423. if (head_mid != sd->status.head_mid)
  11424. clif_changelook(&sd->bl, LOOK_HEAD_MID, sd->status.head_mid);
  11425. if (head_top != sd->status.head_top)
  11426. clif_changelook(&sd->bl, LOOK_HEAD_TOP, sd->status.head_top);
  11427. if (robe != sd->status.robe)
  11428. clif_changelook(&sd->bl, LOOK_ROBE, sd->status.robe);
  11429. }
  11430. std::shared_ptr<s_attendance_period> pc_attendance_period(){
  11431. uint32 date = date_get(DT_YYYYMMDD);
  11432. for( std::pair<const uint32,std::shared_ptr<s_attendance_period>>& pair : attendance_db ){
  11433. std::shared_ptr<s_attendance_period> period = pair.second;
  11434. if( period->start <= date && period->end >= date ){
  11435. return period;
  11436. }
  11437. }
  11438. return nullptr;
  11439. }
  11440. bool pc_attendance_enabled(){
  11441. // Check if the attendance feature is disabled
  11442. if( !battle_config.feature_attendance ){
  11443. return false;
  11444. }
  11445. // Check if there is a running attendance period
  11446. return pc_attendance_period() != nullptr;
  11447. }
  11448. static inline bool pc_attendance_rewarded_today( struct map_session_data* sd ){
  11449. return pc_readreg2( sd, ATTENDANCE_DATE_VAR ) >= date_get(DT_YYYYMMDD);
  11450. }
  11451. int32 pc_attendance_counter( struct map_session_data* sd ){
  11452. std::shared_ptr<s_attendance_period> period = pc_attendance_period();
  11453. // No running attendance period
  11454. if( period == nullptr ){
  11455. return 0;
  11456. }
  11457. // Get the counter for the current period
  11458. int counter = static_cast<int>(pc_readreg2( sd, ATTENDANCE_COUNT_VAR ));
  11459. // Check if we have a remaining counter from a previous period
  11460. if( counter > 0 && pc_readreg2( sd, ATTENDANCE_DATE_VAR ) < period->start ){
  11461. // Reset the counter to zero
  11462. pc_setreg2( sd, ATTENDANCE_COUNT_VAR, 0 );
  11463. return 0;
  11464. }
  11465. return 10 * counter + ( ( pc_attendance_rewarded_today(sd) ) ? 1 : 0 );
  11466. }
  11467. void pc_attendance_claim_reward( struct map_session_data* sd ){
  11468. // If the user's group does not have the permission
  11469. if( !pc_has_permission( sd, PC_PERM_ATTENDANCE ) ){
  11470. return;
  11471. }
  11472. // Check if the attendance feature is disabled
  11473. if( !pc_attendance_enabled() ){
  11474. return;
  11475. }
  11476. // Check if the user already got his reward today
  11477. if( pc_attendance_rewarded_today( sd ) ){
  11478. return;
  11479. }
  11480. int32 attendance_counter = static_cast<int32>(pc_readreg2( sd, ATTENDANCE_COUNT_VAR ));
  11481. attendance_counter += 1;
  11482. std::shared_ptr<s_attendance_period> period = pc_attendance_period();
  11483. if( period == nullptr ){
  11484. return;
  11485. }
  11486. if( period->rewards.size() < attendance_counter ){
  11487. return;
  11488. }
  11489. pc_setreg2( sd, ATTENDANCE_DATE_VAR, date_get(DT_YYYYMMDD) );
  11490. pc_setreg2( sd, ATTENDANCE_COUNT_VAR, attendance_counter );
  11491. if( save_settings&CHARSAVE_ATTENDANCE )
  11492. chrif_save(sd, CSAVE_NORMAL);
  11493. std::shared_ptr<s_attendance_reward> reward = period->rewards[attendance_counter - 1];
  11494. struct mail_message msg;
  11495. memset( &msg, 0, sizeof( struct mail_message ) );
  11496. msg.dest_id = sd->status.char_id;
  11497. safestrncpy( msg.send_name, msg_txt( sd, 788 ), NAME_LENGTH );
  11498. safesnprintf( msg.title, MAIL_TITLE_LENGTH, msg_txt( sd, 789 ), attendance_counter );
  11499. safesnprintf( msg.body, MAIL_BODY_LENGTH, msg_txt( sd, 790 ), attendance_counter );
  11500. msg.item[0].nameid = reward->item_id;
  11501. msg.item[0].amount = reward->amount;
  11502. msg.item[0].identify = 1;
  11503. msg.status = MAIL_NEW;
  11504. msg.type = MAIL_INBOX_NORMAL;
  11505. msg.timestamp = time(NULL);
  11506. intif_Mail_send(0, &msg);
  11507. clif_attendence_response( sd, attendance_counter );
  11508. }
  11509. /*==========================================
  11510. * pc Init/Terminate
  11511. *------------------------------------------*/
  11512. void do_final_pc(void) {
  11513. db_destroy(itemcd_db);
  11514. do_final_pc_groups();
  11515. ers_destroy(pc_sc_display_ers);
  11516. ers_destroy(num_reg_ers);
  11517. ers_destroy(str_reg_ers);
  11518. attendance_db.clear();
  11519. }
  11520. void do_init_pc(void) {
  11521. itemcd_db = idb_alloc(DB_OPT_RELEASE_DATA);
  11522. pc_readdb();
  11523. pc_read_motd(); // Read MOTD [Valaris]
  11524. attendance_db.load();
  11525. add_timer_func_list(pc_invincible_timer, "pc_invincible_timer");
  11526. add_timer_func_list(pc_eventtimer, "pc_eventtimer");
  11527. add_timer_func_list(pc_inventory_rental_end, "pc_inventory_rental_end");
  11528. add_timer_func_list(pc_calc_pvprank_timer, "pc_calc_pvprank_timer");
  11529. add_timer_func_list(pc_autosave, "pc_autosave");
  11530. add_timer_func_list(pc_spiritball_timer, "pc_spiritball_timer");
  11531. add_timer_func_list(pc_follow_timer, "pc_follow_timer");
  11532. add_timer_func_list(pc_endautobonus, "pc_endautobonus");
  11533. add_timer_func_list(pc_spiritcharm_timer, "pc_spiritcharm_timer");
  11534. add_timer_func_list(pc_global_expiration_timer, "pc_global_expiration_timer");
  11535. add_timer_func_list(pc_expiration_timer, "pc_expiration_timer");
  11536. add_timer_func_list(pc_autotrade_timer, "pc_autotrade_timer");
  11537. add_timer(gettick() + autosave_interval, pc_autosave, 0, 0);
  11538. // 0=day, 1=night [Yor]
  11539. night_flag = battle_config.night_at_start ? 1 : 0;
  11540. if (battle_config.day_duration > 0 && battle_config.night_duration > 0) {
  11541. int day_duration = battle_config.day_duration;
  11542. int night_duration = battle_config.night_duration;
  11543. // add night/day timer [Yor]
  11544. add_timer_func_list(map_day_timer, "map_day_timer");
  11545. add_timer_func_list(map_night_timer, "map_night_timer");
  11546. day_timer_tid = add_timer_interval(gettick() + (night_flag ? 0 : day_duration) + night_duration, map_day_timer, 0, 0, day_duration + night_duration);
  11547. night_timer_tid = add_timer_interval(gettick() + day_duration + (night_flag ? night_duration : 0), map_night_timer, 0, 0, day_duration + night_duration);
  11548. }
  11549. do_init_pc_groups();
  11550. pc_sc_display_ers = ers_new(sizeof(struct sc_display_entry), "pc.cpp:pc_sc_display_ers", ERS_OPT_FLEX_CHUNK);
  11551. num_reg_ers = ers_new(sizeof(struct script_reg_num), "pc.cpp:num_reg_ers", (ERSOptions)(ERS_OPT_CLEAN|ERS_OPT_FLEX_CHUNK));
  11552. str_reg_ers = ers_new(sizeof(struct script_reg_str), "pc.cpp:str_reg_ers", (ERSOptions)(ERS_OPT_CLEAN|ERS_OPT_FLEX_CHUNK));
  11553. ers_chunk_size(pc_sc_display_ers, 150);
  11554. ers_chunk_size(num_reg_ers, 300);
  11555. ers_chunk_size(str_reg_ers, 50);
  11556. }