pc.cpp 412 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452
  1. // Copyright (c) rAthena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #include "pc.hpp"
  4. #include <map>
  5. #include <math.h>
  6. #include <stdlib.h>
  7. #include <yaml-cpp/yaml.h>
  8. #include "../common/cbasetypes.hpp"
  9. #include "../common/core.hpp" // get_svn_revision()
  10. #include "../common/database.hpp"
  11. #include "../common/ers.hpp" // ers_destroy
  12. #include "../common/malloc.hpp"
  13. #include "../common/mmo.hpp" //NAME_LENGTH
  14. #include "../common/nullpo.hpp"
  15. #include "../common/random.hpp"
  16. #include "../common/showmsg.hpp"
  17. #include "../common/socket.hpp" // session[]
  18. #include "../common/strlib.hpp" // safestrncpy()
  19. #include "../common/timer.hpp"
  20. #include "../common/utilities.hpp"
  21. #include "../common/utils.hpp"
  22. #include "achievement.hpp"
  23. #include "atcommand.hpp" // get_atcommand_level()
  24. #include "battle.hpp" // battle_config
  25. #include "battleground.hpp"
  26. #include "buyingstore.hpp" // struct s_buyingstore
  27. #include "channel.hpp"
  28. #include "chat.hpp"
  29. #include "chrif.hpp"
  30. #include "clan.hpp"
  31. #include "clif.hpp"
  32. #include "date.hpp" // is_day_of_*()
  33. #include "duel.hpp"
  34. #include "elemental.hpp"
  35. #include "guild.hpp"
  36. #include "homunculus.hpp"
  37. #include "instance.hpp"
  38. #include "intif.hpp"
  39. #include "itemdb.hpp" // MAX_ITEMGROUP
  40. #include "log.hpp"
  41. #include "map.hpp"
  42. #include "mercenary.hpp"
  43. #include "mob.hpp"
  44. #include "npc.hpp"
  45. #include "party.hpp" // party_search()
  46. #include "pc_groups.hpp"
  47. #include "pet.hpp" // pet_unlocktarget()
  48. #include "quest.hpp"
  49. #include "script.hpp" // struct script_reg, struct script_regstr
  50. #include "searchstore.hpp" // struct s_search_store_info
  51. #include "status.hpp" // OPTION_*, struct weapon_atk
  52. #include "storage.hpp"
  53. #include "unit.hpp" // unit_stop_attack(), unit_stop_walking()
  54. #include "vending.hpp" // struct s_vending
  55. using namespace rathena;
  56. int pc_split_atoui(char* str, unsigned int* val, char sep, int max);
  57. static inline bool pc_attendance_rewarded_today( struct map_session_data* sd );
  58. #define PVP_CALCRANK_INTERVAL 1000 // PVP calculation interval
  59. #define MAX_LEVEL_BASE_EXP 99999999 ///< Max Base EXP for player on Max Base Level
  60. #define MAX_LEVEL_JOB_EXP 999999999 ///< Max Job EXP for player on Max Job Level
  61. static unsigned int statp[MAX_LEVEL+1];
  62. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  63. static unsigned int level_penalty[3][CLASS_MAX][MAX_LEVEL*2+1];
  64. #endif
  65. // h-files are for declarations, not for implementations... [Shinomori]
  66. struct skill_tree_entry skill_tree[CLASS_COUNT][MAX_SKILL_TREE];
  67. // timer for night.day implementation
  68. int day_timer_tid = INVALID_TIMER;
  69. int night_timer_tid = INVALID_TIMER;
  70. struct eri *pc_sc_display_ers = NULL;
  71. struct eri *num_reg_ers;
  72. struct eri *str_reg_ers;
  73. int pc_expiration_tid = INVALID_TIMER;
  74. struct fame_list smith_fame_list[MAX_FAME_LIST];
  75. struct fame_list chemist_fame_list[MAX_FAME_LIST];
  76. struct fame_list taekwon_fame_list[MAX_FAME_LIST];
  77. struct s_job_info job_info[CLASS_COUNT];
  78. const std::string AttendanceDatabase::getDefaultLocation(){
  79. return std::string(db_path) + "/attendance.yml";
  80. }
  81. /**
  82. * Reads and parses an entry from the attendance_db.
  83. * @param node: YAML node containing the entry.
  84. * @return count of successfully parsed rows
  85. */
  86. uint64 AttendanceDatabase::parseBodyNode(const YAML::Node &node){
  87. uint32 start;
  88. if( !this->asUInt32( node, "Start", start ) ){
  89. return 0;
  90. }
  91. std::shared_ptr<s_attendance_period> attendance_period = this->find( start );
  92. bool exists = attendance_period != nullptr;
  93. if( !exists ){
  94. if( !this->nodeExists( node, "End" ) ){
  95. this->invalidWarning( node, "Node \"End\" is missing.\n" );
  96. return 0;
  97. }
  98. if( !this->nodeExists( node, "Rewards" ) ){
  99. this->invalidWarning( node, "Node \"Rewards\" is missing.\n" );
  100. return 0;
  101. }
  102. attendance_period = std::make_shared<s_attendance_period>();
  103. attendance_period->start = start;
  104. }
  105. // If it does not exist yet, we need to check it for sure
  106. bool requiresCollisionDetection = !exists;
  107. if( this->nodeExists( node, "End" ) ){
  108. uint32 end;
  109. if( !this->asUInt32( node, "End", end ) ){
  110. return 0;
  111. }
  112. // If the period is outdated already, we do not even bother parsing
  113. if( end < date_get( DT_YYYYMMDD ) ){
  114. this->invalidWarning( node, "Node \"End\" date %u has already passed, skipping.\n", end );
  115. return 0;
  116. }
  117. if( !exists || attendance_period->end != end ){
  118. requiresCollisionDetection = true;
  119. attendance_period->end = end;
  120. }
  121. }
  122. // Collision detection
  123. if( requiresCollisionDetection ){
  124. bool collision = false;
  125. for( std::pair<const uint32,std::shared_ptr<s_attendance_period>>& pair : *this ){
  126. std::shared_ptr<s_attendance_period> period = pair.second;
  127. if( exists && period->start == attendance_period->start ){
  128. // Dont compare to yourself
  129. continue;
  130. }
  131. // Check if start is inside another period
  132. if( period->start <= attendance_period->start && start <= period->end ){
  133. this->invalidWarning( node, "Node \"Start\" period %u intersects with period %u-%u, skipping.\n", attendance_period->start, period->start, period->end );
  134. collision = true;
  135. break;
  136. }
  137. // Check if end is inside another period
  138. if( period->start <= attendance_period->end && attendance_period->end <= period->end ){
  139. this->invalidWarning( node, "Node \"End\" period %u intersects with period %u-%u.\n", attendance_period->start, period->start, period->end );
  140. collision = true;
  141. break;
  142. }
  143. }
  144. if( collision ){
  145. return 0;
  146. }
  147. }
  148. if( this->nodeExists( node, "Rewards" ) ){
  149. const YAML::Node& rewardsNode = node["Rewards"];
  150. for( const YAML::Node& rewardNode : rewardsNode ){
  151. uint32 day;
  152. if( !this->asUInt32( rewardNode, "Day", day ) ){
  153. continue;
  154. }
  155. day -= 1;
  156. std::shared_ptr<s_attendance_reward> reward = util::map_find( attendance_period->rewards, day );
  157. bool reward_exists = reward != nullptr;
  158. if( !reward_exists ){
  159. if( !this->nodeExists( rewardNode, "ItemId" ) ){
  160. this->invalidWarning( rewardNode, "Node \"ItemId\" is missing.\n" );
  161. return 0;
  162. }
  163. reward = std::make_shared<s_attendance_reward>();
  164. }
  165. if( this->nodeExists( rewardNode, "ItemId" ) ){
  166. uint16 item_id;
  167. if( !this->asUInt16( rewardNode, "ItemId", item_id ) ){
  168. continue;
  169. }
  170. if( item_id == 0 || !itemdb_exists( item_id ) ){
  171. ShowError( "pc_attendance_load: Unknown item ID %hu for day %d.\n", item_id, day + 1 );
  172. continue;
  173. }
  174. reward->item_id = item_id;
  175. }
  176. if( this->nodeExists( rewardNode, "Amount" ) ){
  177. uint16 amount;
  178. if( !this->asUInt16( rewardNode, "Amount", amount ) ){
  179. continue;
  180. }
  181. if( amount == 0 ){
  182. ShowWarning( "pc_attendance_load: Invalid reward count %hu for day %d. Defaulting to 1...\n", amount, day + 1 );
  183. amount = 1;
  184. }else if( amount > MAX_AMOUNT ){
  185. ShowError( "pc_attendance_load: Reward count %hu above maximum %hu for day %d. Defaulting to %hu...\n", amount, MAX_AMOUNT, day + 1, MAX_AMOUNT );
  186. amount = MAX_AMOUNT;
  187. }
  188. reward->amount = amount;
  189. }else{
  190. if( !reward_exists ){
  191. reward->amount = 1;
  192. }
  193. }
  194. if( !reward_exists ){
  195. attendance_period->rewards[day] = reward;
  196. }
  197. }
  198. bool missing_day = false;
  199. for( int day = 0; day < attendance_period->rewards.size(); day++ ){
  200. if( attendance_period->rewards.find( day ) == attendance_period->rewards.end() ){
  201. ShowError( "pc_attendance_load: Reward for day %d is missing.\n", day + 1 );
  202. missing_day = true;
  203. break;
  204. }
  205. }
  206. if( missing_day ){
  207. return 0;
  208. }
  209. }
  210. if( !exists ){
  211. this->put( start, attendance_period );
  212. }
  213. return 1;
  214. }
  215. AttendanceDatabase attendance_db;
  216. #define MOTD_LINE_SIZE 128
  217. static char motd_text[MOTD_LINE_SIZE][CHAT_SIZE_MAX]; // Message of the day buffer [Valaris]
  218. bool reg_load;
  219. /**
  220. * Translation table from athena equip index to aegis bitmask
  221. */
  222. unsigned int equip_bitmask[EQI_MAX] = {
  223. EQP_ACC_L, // EQI_ACC_L
  224. EQP_ACC_R, // EQI_ACC_R
  225. EQP_SHOES, // EQI_SHOES
  226. EQP_GARMENT, // EQI_GARMENT
  227. EQP_HEAD_LOW, // EQI_HEAD_LOW
  228. EQP_HEAD_MID, // EQI_HEAD_MID
  229. EQP_HEAD_TOP, // EQI_HEAD_TOP
  230. EQP_ARMOR, // EQI_ARMOR
  231. EQP_HAND_L, // EQI_HAND_L
  232. EQP_HAND_R, // EQI_HAND_R
  233. EQP_COSTUME_HEAD_TOP, // EQI_COSTUME_HEAD_TOP
  234. EQP_COSTUME_HEAD_MID, // EQI_COSTUME_HEAD_MID
  235. EQP_COSTUME_HEAD_LOW, // EQI_COSTUME_HEAD_LOW
  236. EQP_COSTUME_GARMENT, // EQI_COSTUME_GARMENT
  237. EQP_AMMO, // EQI_AMMO
  238. EQP_SHADOW_ARMOR, // EQI_SHADOW_ARMOR
  239. EQP_SHADOW_WEAPON, // EQI_SHADOW_WEAPON
  240. EQP_SHADOW_SHIELD, // EQI_SHADOW_SHIELD
  241. EQP_SHADOW_SHOES, // EQI_SHADOW_SHOES
  242. EQP_SHADOW_ACC_R, // EQI_SHADOW_ACC_R
  243. EQP_SHADOW_ACC_L // EQI_SHADOW_ACC_L
  244. };
  245. //Links related info to the sd->hate_mob[]/sd->feel_map[] entries
  246. const struct sg_data sg_info[MAX_PC_FEELHATE] = {
  247. { SG_SUN_ANGER, SG_SUN_BLESS, SG_SUN_COMFORT, "PC_FEEL_SUN", "PC_HATE_MOB_SUN", is_day_of_sun },
  248. { SG_MOON_ANGER, SG_MOON_BLESS, SG_MOON_COMFORT, "PC_FEEL_MOON", "PC_HATE_MOB_MOON", is_day_of_moon },
  249. { SG_STAR_ANGER, SG_STAR_BLESS, SG_STAR_COMFORT, "PC_FEEL_STAR", "PC_HATE_MOB_STAR", is_day_of_star }
  250. };
  251. void pc_set_reg_load( bool val ){
  252. reg_load = val;
  253. }
  254. /**
  255. * Item Cool Down Delay Saving
  256. * Struct item_cd is not a member of struct map_session_data
  257. * to keep cooldowns in memory between player log-ins.
  258. * All cooldowns are reset when server is restarted.
  259. **/
  260. DBMap* itemcd_db = NULL; // char_id -> struct item_cd
  261. struct item_cd {
  262. t_tick tick[MAX_ITEMDELAYS]; //tick
  263. unsigned short nameid[MAX_ITEMDELAYS]; //item id
  264. };
  265. /**
  266. * Converts a class to its array index for CLASS_COUNT defined arrays.
  267. * Note that it does not do a validity check for speed purposes, where parsing
  268. * player input make sure to use a pcdb_checkid first!
  269. * @param class_ Job ID see enum e_job
  270. * @return Class Index
  271. */
  272. int pc_class2idx(int class_) {
  273. if (class_ >= JOB_NOVICE_HIGH)
  274. return class_- JOB_NOVICE_HIGH+JOB_MAX_BASIC;
  275. return class_;
  276. }
  277. /**
  278. * Get player's group ID
  279. * @param sd
  280. * @return Group ID
  281. */
  282. int pc_get_group_id(struct map_session_data *sd) {
  283. return sd->group_id;
  284. }
  285. /** Get player's group Level
  286. * @param sd
  287. * @return Group Level
  288. */
  289. int pc_get_group_level(struct map_session_data *sd) {
  290. return sd->group_level;
  291. }
  292. static TIMER_FUNC(pc_invincible_timer){
  293. struct map_session_data *sd;
  294. if( (sd=(struct map_session_data *)map_id2sd(id)) == NULL || sd->bl.type!=BL_PC )
  295. return 1;
  296. if(sd->invincible_timer != tid){
  297. ShowError("invincible_timer %d != %d\n",sd->invincible_timer,tid);
  298. return 0;
  299. }
  300. sd->invincible_timer = INVALID_TIMER;
  301. skill_unit_move(&sd->bl,tick,1);
  302. return 0;
  303. }
  304. void pc_setinvincibletimer(struct map_session_data* sd, int val) {
  305. nullpo_retv(sd);
  306. if( sd->invincible_timer != INVALID_TIMER )
  307. delete_timer(sd->invincible_timer,pc_invincible_timer);
  308. sd->invincible_timer = add_timer(gettick()+val,pc_invincible_timer,sd->bl.id,0);
  309. }
  310. void pc_delinvincibletimer(struct map_session_data* sd)
  311. {
  312. nullpo_retv(sd);
  313. if( sd->invincible_timer != INVALID_TIMER )
  314. {
  315. delete_timer(sd->invincible_timer,pc_invincible_timer);
  316. sd->invincible_timer = INVALID_TIMER;
  317. skill_unit_move(&sd->bl,gettick(),1);
  318. }
  319. }
  320. static TIMER_FUNC(pc_spiritball_timer){
  321. struct map_session_data *sd;
  322. int i;
  323. if( (sd=(struct map_session_data *)map_id2sd(id)) == NULL || sd->bl.type!=BL_PC )
  324. return 1;
  325. if( sd->spiritball <= 0 )
  326. {
  327. ShowError("pc_spiritball_timer: %d spiritball's available. (aid=%d cid=%d tid=%d)\n", sd->spiritball, sd->status.account_id, sd->status.char_id, tid);
  328. sd->spiritball = 0;
  329. return 0;
  330. }
  331. ARR_FIND(0, sd->spiritball, i, sd->spirit_timer[i] == tid);
  332. if( i == sd->spiritball )
  333. {
  334. ShowError("pc_spiritball_timer: timer not found (aid=%d cid=%d tid=%d)\n", sd->status.account_id, sd->status.char_id, tid);
  335. return 0;
  336. }
  337. sd->spiritball--;
  338. if( i != sd->spiritball )
  339. memmove(sd->spirit_timer+i, sd->spirit_timer+i+1, (sd->spiritball-i)*sizeof(int));
  340. sd->spirit_timer[sd->spiritball] = INVALID_TIMER;
  341. clif_spiritball(&sd->bl);
  342. return 0;
  343. }
  344. /**
  345. * Adds a spiritball to player for 'interval' ms
  346. * @param sd
  347. * @param interval
  348. * @param max
  349. */
  350. void pc_addspiritball(struct map_session_data *sd,int interval,int max)
  351. {
  352. int tid;
  353. uint8 i;
  354. nullpo_retv(sd);
  355. if(max > MAX_SPIRITBALL)
  356. max = MAX_SPIRITBALL;
  357. if(sd->spiritball < 0)
  358. sd->spiritball = 0;
  359. if( sd->spiritball && sd->spiritball >= max )
  360. {
  361. if(sd->spirit_timer[0] != INVALID_TIMER)
  362. delete_timer(sd->spirit_timer[0],pc_spiritball_timer);
  363. sd->spiritball--;
  364. if( sd->spiritball != 0 )
  365. memmove(sd->spirit_timer+0, sd->spirit_timer+1, (sd->spiritball)*sizeof(int));
  366. sd->spirit_timer[sd->spiritball] = INVALID_TIMER;
  367. }
  368. tid = add_timer(gettick()+interval, pc_spiritball_timer, sd->bl.id, 0);
  369. ARR_FIND(0, sd->spiritball, i, sd->spirit_timer[i] == INVALID_TIMER || DIFF_TICK(get_timer(tid)->tick, get_timer(sd->spirit_timer[i])->tick) < 0);
  370. if( i != sd->spiritball )
  371. memmove(sd->spirit_timer+i+1, sd->spirit_timer+i, (sd->spiritball-i)*sizeof(int));
  372. sd->spirit_timer[i] = tid;
  373. sd->spiritball++;
  374. if( (sd->class_&MAPID_THIRDMASK) == MAPID_ROYAL_GUARD )
  375. clif_millenniumshield(&sd->bl,sd->spiritball);
  376. else
  377. clif_spiritball(&sd->bl);
  378. }
  379. /**
  380. * Removes number of spiritball from player
  381. * @param sd
  382. * @param count
  383. * @param type 1 = doesn't give client effect
  384. */
  385. void pc_delspiritball(struct map_session_data *sd,int count,int type)
  386. {
  387. uint8 i;
  388. nullpo_retv(sd);
  389. if(sd->spiritball <= 0) {
  390. sd->spiritball = 0;
  391. return;
  392. }
  393. if(count == 0)
  394. return;
  395. if(count > sd->spiritball)
  396. count = sd->spiritball;
  397. sd->spiritball -= count;
  398. if(count > MAX_SPIRITBALL)
  399. count = MAX_SPIRITBALL;
  400. for(i=0;i<count;i++) {
  401. if(sd->spirit_timer[i] != INVALID_TIMER) {
  402. delete_timer(sd->spirit_timer[i],pc_spiritball_timer);
  403. sd->spirit_timer[i] = INVALID_TIMER;
  404. }
  405. }
  406. for(i=count;i<MAX_SPIRITBALL;i++) {
  407. sd->spirit_timer[i-count] = sd->spirit_timer[i];
  408. sd->spirit_timer[i] = INVALID_TIMER;
  409. }
  410. if(!type) {
  411. if( (sd->class_&MAPID_THIRDMASK) == MAPID_ROYAL_GUARD )
  412. clif_millenniumshield(&sd->bl,sd->spiritball);
  413. else
  414. clif_spiritball(&sd->bl);
  415. }
  416. }
  417. static TIMER_FUNC(pc_soulball_timer)
  418. {
  419. struct map_session_data *sd;
  420. if ((sd = (struct map_session_data *)map_id2sd(id)) == nullptr || sd->bl.type != BL_PC)
  421. return 1;
  422. if (sd->soulball <= 0) {
  423. ShowError("pc_soulball_timer: %d soulball's available. (aid=%d tid=%d)\n", sd->soulball, sd->status.account_id, tid);
  424. sd->soulball = 0;
  425. return 0;
  426. }
  427. int i;
  428. ARR_FIND(0, sd->soulball, i, sd->soul_timer[i] == tid);
  429. if (i == sd->soulball) {
  430. ShowError("pc_soulball_timer: timer not found (aid=%d tid=%d)\n", sd->status.account_id, tid);
  431. return 0;
  432. }
  433. sd->soulball--;
  434. if (i != sd->soulball)
  435. memmove(sd->soul_timer + i, sd->soul_timer + i + 1, (sd->soulball - i) * sizeof(int));
  436. sd->soul_timer[sd->soulball] = INVALID_TIMER;
  437. clif_soulball(sd);
  438. return 0;
  439. }
  440. /**
  441. * Adds a soulball to player for 'interval' ms
  442. * @param sd: Player data
  443. * @param interval: Duration
  444. * @param max: Max amount of soulballs
  445. */
  446. int pc_addsoulball(struct map_session_data *sd, int interval, int max)
  447. {
  448. nullpo_ret(sd);
  449. max = min(max, MAX_SOUL_BALL);
  450. sd->soulball = cap_value(sd->soulball, 0, MAX_SOUL_BALL);
  451. if (sd->soulball && sd->soulball >= max) {
  452. if (sd->soul_timer[0] != INVALID_TIMER)
  453. delete_timer(sd->soul_timer[0], pc_soulball_timer);
  454. sd->soulball--;
  455. if (sd->soulball != 0)
  456. memmove(sd->soul_timer + 0, sd->soul_timer + 1, (sd->soulball) * sizeof(int));
  457. sd->soul_timer[sd->soulball] = INVALID_TIMER;
  458. }
  459. if (interval > 0) {
  460. int tid = add_timer(gettick() + interval, pc_soulball_timer, sd->bl.id, 0), i;
  461. ARR_FIND(0, sd->soulball, i, sd->soul_timer[i] == INVALID_TIMER || DIFF_TICK(get_timer(tid)->tick, get_timer(sd->soul_timer[i])->tick) < 0);
  462. if (i != sd->soulball)
  463. memmove(sd->soul_timer + i + 1, sd->soul_timer + i, (sd->soulball - i) * sizeof(int));
  464. sd->soul_timer[i] = tid;
  465. }
  466. sd->soulball++;
  467. clif_soulball(sd);
  468. return 0;
  469. }
  470. /**
  471. * Removes number of soulball from player
  472. * @param sd: Player data
  473. * @param count: Amount to remove
  474. * @param type: 1 = doesn't give client effect
  475. */
  476. int pc_delsoulball(struct map_session_data *sd, int count, int type)
  477. {
  478. nullpo_ret(sd);
  479. if (sd->soulball <= 0) {
  480. sd->soulball = 0;
  481. return 0;
  482. }
  483. if (count <= 0)
  484. return 0;
  485. if (count > sd->soulball)
  486. count = sd->soulball;
  487. sd->soulball -= count;
  488. if (count > MAX_SKILL_LEVEL)
  489. count = MAX_SKILL_LEVEL;
  490. for (int i = 0; i < count; i++) {
  491. if (sd->soul_timer[i] != INVALID_TIMER) {
  492. delete_timer(sd->soul_timer[i], pc_soulball_timer);
  493. sd->soul_timer[i] = INVALID_TIMER;
  494. }
  495. }
  496. for (int i = count; i < MAX_SKILL_LEVEL; i++) {
  497. sd->soul_timer[i - count] = sd->soul_timer[i];
  498. sd->soul_timer[i] = INVALID_TIMER;
  499. }
  500. if (!type)
  501. clif_soulball(sd);
  502. return 0;
  503. }
  504. /**
  505. * Increases a player's fame points and displays a notice to him
  506. * @param sd Player
  507. * @param count Fame point
  508. */
  509. void pc_addfame(struct map_session_data *sd,int count)
  510. {
  511. enum e_rank ranktype;
  512. nullpo_retv(sd);
  513. sd->status.fame += count;
  514. if(sd->status.fame > MAX_FAME)
  515. sd->status.fame = MAX_FAME;
  516. switch(sd->class_&MAPID_UPPERMASK){
  517. case MAPID_BLACKSMITH: ranktype = RANK_BLACKSMITH; break;
  518. case MAPID_ALCHEMIST: ranktype = RANK_ALCHEMIST; break;
  519. case MAPID_TAEKWON: ranktype = RANK_TAEKWON; break;
  520. default:
  521. ShowWarning( "pc_addfame: Trying to add fame to class '%s'(%d).\n", job_name(sd->class_), sd->class_ );
  522. return;
  523. }
  524. clif_update_rankingpoint(sd,ranktype,count);
  525. chrif_updatefamelist(sd);
  526. }
  527. /**
  528. * Check whether a player ID is in the fame rankers list of its job, returns his/her position if so, 0 else
  529. * @param sd
  530. * @param job Job use enum e_mapid
  531. * @return Rank
  532. */
  533. unsigned char pc_famerank(uint32 char_id, int job)
  534. {
  535. uint8 i;
  536. switch(job){
  537. case MAPID_BLACKSMITH: // Blacksmith
  538. for(i = 0; i < MAX_FAME_LIST; i++){
  539. if(smith_fame_list[i].id == char_id)
  540. return i + 1;
  541. }
  542. break;
  543. case MAPID_ALCHEMIST: // Alchemist
  544. for(i = 0; i < MAX_FAME_LIST; i++){
  545. if(chemist_fame_list[i].id == char_id)
  546. return i + 1;
  547. }
  548. break;
  549. case MAPID_TAEKWON: // Taekwon
  550. for(i = 0; i < MAX_FAME_LIST; i++){
  551. if(taekwon_fame_list[i].id == char_id)
  552. return i + 1;
  553. }
  554. break;
  555. }
  556. return 0;
  557. }
  558. /**
  559. * Restart player's HP & SP value
  560. * @param sd
  561. * @param type Restart type: 1 - Normal Resurection
  562. */
  563. void pc_setrestartvalue(struct map_session_data *sd, char type) {
  564. struct status_data *status, *b_status;
  565. nullpo_retv(sd);
  566. b_status = &sd->base_status;
  567. status = &sd->battle_status;
  568. if (type&1) { //Normal resurrection
  569. status->hp = 1; //Otherwise status_heal may fail if dead.
  570. status_heal(&sd->bl, b_status->hp, 0, 1);
  571. if( status->sp < b_status->sp )
  572. status_set_sp(&sd->bl, b_status->sp, 1);
  573. } else { //Just for saving on the char-server (with values as if respawned)
  574. sd->status.hp = b_status->hp;
  575. sd->status.sp = (status->sp < b_status->sp)?b_status->sp:status->sp;
  576. }
  577. }
  578. /*==========================================
  579. Rental System
  580. *------------------------------------------*/
  581. /**
  582. * Ends a rental and removes the item/effect
  583. * @param tid: Tick ID
  584. * @param tick: Timer
  585. * @param id: Timer ID
  586. * @param data: Data
  587. * @return false - failure, true - success
  588. */
  589. TIMER_FUNC(pc_inventory_rental_end){
  590. struct map_session_data *sd = map_id2sd(id);
  591. if( sd == NULL )
  592. return 0;
  593. if( tid != sd->rental_timer ) {
  594. ShowError("pc_inventory_rental_end: invalid timer id.\n");
  595. return 0;
  596. }
  597. pc_inventory_rentals(sd);
  598. return 1;
  599. }
  600. /**
  601. * Removes the rental timer from the player
  602. * @param sd: Player data
  603. */
  604. void pc_inventory_rental_clear(struct map_session_data *sd)
  605. {
  606. if( sd->rental_timer != INVALID_TIMER ) {
  607. delete_timer(sd->rental_timer, pc_inventory_rental_end);
  608. sd->rental_timer = INVALID_TIMER;
  609. }
  610. }
  611. /**
  612. * Check for items in the player's inventory that are rental type
  613. * @param sd: Player data
  614. */
  615. void pc_inventory_rentals(struct map_session_data *sd)
  616. {
  617. int i, c = 0;
  618. unsigned int next_tick = UINT_MAX;
  619. for( i = 0; i < MAX_INVENTORY; i++ ) { // Check for Rentals on Inventory
  620. if( sd->inventory.u.items_inventory[i].nameid == 0 )
  621. continue; // Nothing here
  622. if( sd->inventory.u.items_inventory[i].expire_time == 0 )
  623. continue;
  624. if( sd->inventory.u.items_inventory[i].expire_time <= time(NULL) ) {
  625. if (sd->inventory_data[i]->unequip_script)
  626. run_script(sd->inventory_data[i]->unequip_script, 0, sd->bl.id, fake_nd->bl.id);
  627. clif_rental_expired(sd->fd, i, sd->inventory.u.items_inventory[i].nameid);
  628. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 0, 0, LOG_TYPE_OTHER);
  629. } else {
  630. unsigned int expire_tick = (unsigned int)(sd->inventory.u.items_inventory[i].expire_time - time(NULL));
  631. clif_rental_time(sd->fd, sd->inventory.u.items_inventory[i].nameid, (int)expire_tick);
  632. next_tick = umin(expire_tick * 1000U, next_tick);
  633. c++;
  634. }
  635. }
  636. 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
  637. sd->rental_timer = add_timer(gettick() + umin(next_tick,3600000), pc_inventory_rental_end, sd->bl.id, 0);
  638. else
  639. sd->rental_timer = INVALID_TIMER;
  640. }
  641. /**
  642. * Add a rental item to the player and adjusts the rental timer appropriately
  643. * @param sd: Player data
  644. * @param seconds: Rental time
  645. */
  646. void pc_inventory_rental_add(struct map_session_data *sd, unsigned int seconds)
  647. {
  648. t_tick tick = seconds * 1000;
  649. if( sd == NULL )
  650. return;
  651. if( sd->rental_timer != INVALID_TIMER ) {
  652. const struct TimerData * td;
  653. td = get_timer(sd->rental_timer);
  654. if( DIFF_TICK(td->tick, gettick()) > tick ) { // Update Timer as this one ends first than the current one
  655. pc_inventory_rental_clear(sd);
  656. sd->rental_timer = add_timer(gettick() + tick, pc_inventory_rental_end, sd->bl.id, 0);
  657. }
  658. } else
  659. sd->rental_timer = add_timer(gettick() + i64min(tick,3600000), pc_inventory_rental_end, sd->bl.id, 0);
  660. }
  661. /**
  662. * Check if the player can sell the current item
  663. * @param sd: map_session_data of the player
  664. * @param item: struct of the checking item
  665. * @param shoptype: NPC's sub type see enum npc_subtype
  666. * @return bool 'true' is sellable, 'false' otherwise
  667. */
  668. bool pc_can_sell_item(struct map_session_data *sd, struct item *item, enum npc_subtype shoptype) {
  669. if (sd == NULL || item == NULL)
  670. return false;
  671. if (item->equip > 0 || item->amount < 0)
  672. return false;
  673. if (battle_config.hide_fav_sell && item->favorite)
  674. return false; //Cannot sell favs (optional config)
  675. if (!battle_config.rental_transaction && item->expire_time)
  676. return false; // Cannot Sell Rental Items
  677. if( item->equipSwitch ){
  678. return false;
  679. }
  680. if (itemdb_ishatched_egg(item))
  681. return false;
  682. switch (shoptype) {
  683. case NPCTYPE_SHOP:
  684. if (item->bound && battle_config.allow_bound_sell&ISR_BOUND_SELLABLE && (
  685. item->bound != BOUND_GUILD ||
  686. (sd->guild && sd->status.char_id == sd->guild->member[0].char_id) ||
  687. (item->bound == BOUND_GUILD && !(battle_config.allow_bound_sell&ISR_BOUND_GUILDLEADER_ONLY))
  688. ))
  689. return true;
  690. break;
  691. case NPCTYPE_ITEMSHOP:
  692. if (item->bound && battle_config.allow_bound_sell&ISR_BOUND && (
  693. item->bound != BOUND_GUILD ||
  694. (sd->guild && sd->status.char_id == sd->guild->member[0].char_id) ||
  695. (item->bound == BOUND_GUILD && !(battle_config.allow_bound_sell&ISR_BOUND_GUILDLEADER_ONLY))
  696. ))
  697. return true;
  698. else if (!item->bound) {
  699. struct item_data *itd = itemdb_search(item->nameid);
  700. if (itd && itd->flag.trade_restriction&8 && battle_config.allow_bound_sell&ISR_SELLABLE)
  701. return true;
  702. }
  703. break;
  704. }
  705. if (!itemdb_cansell(item, pc_get_group_level(sd)))
  706. return false;
  707. if (item->bound && !pc_can_give_bounded_items(sd))
  708. return false; // Don't allow sale of bound items
  709. return true;
  710. }
  711. /**
  712. * Determines if player can give / drop / trade / vend items
  713. */
  714. bool pc_can_give_items(struct map_session_data *sd)
  715. {
  716. return pc_has_permission(sd, PC_PERM_TRADE);
  717. }
  718. /**
  719. * Determines if player can give / drop / trade / vend bounded items
  720. */
  721. bool pc_can_give_bounded_items(struct map_session_data *sd)
  722. {
  723. return pc_has_permission(sd, PC_PERM_TRADE_BOUNDED);
  724. }
  725. /*==========================================
  726. * Prepares character for saving.
  727. * @param sd
  728. *------------------------------------------*/
  729. void pc_makesavestatus(struct map_session_data *sd) {
  730. nullpo_retv(sd);
  731. if(!battle_config.save_clothcolor)
  732. sd->status.clothes_color = 0;
  733. // Since this is currently not officially released,
  734. // its best to have a forced option to not save body styles.
  735. if(!battle_config.save_body_style)
  736. sd->status.body = 0;
  737. //Only copy the Cart/Peco/Falcon options, the rest are handled via
  738. //status change load/saving. [Skotlex]
  739. #ifdef NEW_CARTS
  740. sd->status.option = sd->sc.option&(OPTION_INVISIBLE|OPTION_FALCON|OPTION_RIDING|OPTION_DRAGON|OPTION_WUG|OPTION_WUGRIDER|OPTION_MADOGEAR);
  741. #else
  742. sd->status.option = sd->sc.option&(OPTION_INVISIBLE|OPTION_CART|OPTION_FALCON|OPTION_RIDING|OPTION_DRAGON|OPTION_WUG|OPTION_WUGRIDER|OPTION_MADOGEAR);
  743. #endif
  744. if (sd->sc.data[SC_JAILED]) { //When Jailed, do not move last point.
  745. if(pc_isdead(sd)){
  746. pc_setrestartvalue(sd, 0);
  747. } else {
  748. sd->status.hp = sd->battle_status.hp;
  749. sd->status.sp = sd->battle_status.sp;
  750. }
  751. sd->status.last_point.map = sd->mapindex;
  752. sd->status.last_point.x = sd->bl.x;
  753. sd->status.last_point.y = sd->bl.y;
  754. return;
  755. }
  756. if(pc_isdead(sd)) {
  757. pc_setrestartvalue(sd, 0);
  758. memcpy(&sd->status.last_point,&sd->status.save_point,sizeof(sd->status.last_point));
  759. } else {
  760. sd->status.hp = sd->battle_status.hp;
  761. sd->status.sp = sd->battle_status.sp;
  762. sd->status.last_point.map = sd->mapindex;
  763. sd->status.last_point.x = sd->bl.x;
  764. sd->status.last_point.y = sd->bl.y;
  765. }
  766. if(map_getmapflag(sd->bl.m, MF_NOSAVE)) {
  767. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  768. if(mapdata->save.map)
  769. memcpy(&sd->status.last_point,&mapdata->save,sizeof(sd->status.last_point));
  770. else
  771. memcpy(&sd->status.last_point,&sd->status.save_point,sizeof(sd->status.last_point));
  772. }
  773. }
  774. /*==========================================
  775. * Off init ? Connection?
  776. *------------------------------------------*/
  777. 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) {
  778. nullpo_retv(sd);
  779. sd->bl.id = account_id;
  780. sd->status.account_id = account_id;
  781. sd->status.char_id = char_id;
  782. sd->status.sex = sex;
  783. sd->login_id1 = login_id1;
  784. sd->login_id2 = 0; // at this point, we can not know the value :(
  785. sd->client_tick = client_tick;
  786. sd->state.active = 0; //to be set to 1 after player is fully authed and loaded.
  787. sd->bl.type = BL_PC;
  788. if(battle_config.prevent_logout_trigger&PLT_LOGIN)
  789. sd->canlog_tick = gettick();
  790. //Required to prevent homunculus copuing a base speed of 0.
  791. sd->battle_status.speed = sd->base_status.speed = DEFAULT_WALK_SPEED;
  792. }
  793. /**
  794. * Get equip point for an equip
  795. * @param sd
  796. * @param id
  797. */
  798. int pc_equippoint_sub(struct map_session_data *sd,struct item_data* id){
  799. int ep = 0;
  800. nullpo_ret(sd);
  801. nullpo_ret(id);
  802. if (!itemdb_isequip2(id))
  803. return 0; //Not equippable by players.
  804. ep = id->equip;
  805. if(id->look == W_DAGGER || id->look == W_1HSWORD || id->look == W_1HAXE) {
  806. 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]
  807. if (ep == EQP_WEAPON)
  808. return EQP_ARMS;
  809. if (ep == EQP_SHADOW_WEAPON)
  810. return EQP_SHADOW_ARMS;
  811. }
  812. }
  813. return ep;
  814. }
  815. /**
  816. * Get equip point for an equip
  817. * @param sd
  818. * @param n Equip index in inventory
  819. */
  820. int pc_equippoint(struct map_session_data *sd,int n){
  821. nullpo_ret(sd);
  822. return pc_equippoint_sub(sd,sd->inventory_data[n]);
  823. }
  824. /**
  825. * Fill inventory_data with struct *item_data through inventory (fill with struct *item)
  826. * @param sd : player session
  827. * @return 0 sucess, 1:invalid sd
  828. */
  829. void pc_setinventorydata(struct map_session_data *sd)
  830. {
  831. uint8 i;
  832. nullpo_retv(sd);
  833. for(i = 0; i < MAX_INVENTORY; i++) {
  834. unsigned short id = sd->inventory.u.items_inventory[i].nameid;
  835. sd->inventory_data[i] = id?itemdb_search(id):NULL;
  836. }
  837. }
  838. /**
  839. * 'Calculates' weapon type
  840. * @param sd : Player
  841. */
  842. void pc_calcweapontype(struct map_session_data *sd)
  843. {
  844. nullpo_retv(sd);
  845. // single-hand
  846. if(sd->weapontype2 == W_FIST) {
  847. sd->status.weapon = sd->weapontype1;
  848. return;
  849. }
  850. if(sd->weapontype1 == W_FIST) {
  851. sd->status.weapon = sd->weapontype2;
  852. return;
  853. }
  854. // dual-wield
  855. sd->status.weapon = 0;
  856. switch (sd->weapontype1){
  857. case W_DAGGER:
  858. switch (sd->weapontype2) {
  859. case W_DAGGER: sd->status.weapon = W_DOUBLE_DD; break;
  860. case W_1HSWORD: sd->status.weapon = W_DOUBLE_DS; break;
  861. case W_1HAXE: sd->status.weapon = W_DOUBLE_DA; break;
  862. }
  863. break;
  864. case W_1HSWORD:
  865. switch (sd->weapontype2) {
  866. case W_DAGGER: sd->status.weapon = W_DOUBLE_DS; break;
  867. case W_1HSWORD: sd->status.weapon = W_DOUBLE_SS; break;
  868. case W_1HAXE: sd->status.weapon = W_DOUBLE_SA; break;
  869. }
  870. break;
  871. case W_1HAXE:
  872. switch (sd->weapontype2) {
  873. case W_DAGGER: sd->status.weapon = W_DOUBLE_DA; break;
  874. case W_1HSWORD: sd->status.weapon = W_DOUBLE_SA; break;
  875. case W_1HAXE: sd->status.weapon = W_DOUBLE_AA; break;
  876. }
  877. }
  878. // unknown, default to right hand type
  879. if (!sd->status.weapon)
  880. sd->status.weapon = sd->weapontype1;
  881. }
  882. /**
  883. * Set equip index
  884. * @param sd : Player
  885. */
  886. void pc_setequipindex(struct map_session_data *sd)
  887. {
  888. uint16 i;
  889. nullpo_retv(sd);
  890. for (i = 0; i < EQI_MAX; i++){
  891. sd->equip_index[i] = -1;
  892. sd->equip_switch_index[i] = -1;
  893. }
  894. for (i = 0; i < MAX_INVENTORY; i++) {
  895. if (sd->inventory.u.items_inventory[i].nameid <= 0)
  896. continue;
  897. if (sd->inventory.u.items_inventory[i].equip) {
  898. uint8 j;
  899. for (j = 0; j < EQI_MAX; j++)
  900. if (sd->inventory.u.items_inventory[i].equip & equip_bitmask[j])
  901. sd->equip_index[j] = i;
  902. if (sd->inventory.u.items_inventory[i].equip & EQP_HAND_R) {
  903. if (sd->inventory_data[i])
  904. sd->weapontype1 = sd->inventory_data[i]->look;
  905. else
  906. sd->weapontype1 = 0;
  907. }
  908. if( sd->inventory.u.items_inventory[i].equip & EQP_HAND_L ) {
  909. if( sd->inventory_data[i] && sd->inventory_data[i]->type == IT_WEAPON )
  910. sd->weapontype2 = sd->inventory_data[i]->look;
  911. else
  912. sd->weapontype2 = 0;
  913. }
  914. }
  915. if (sd->inventory.u.items_inventory[i].equipSwitch) {
  916. for (uint8 j = 0; j < EQI_MAX; j++) {
  917. if (sd->inventory.u.items_inventory[i].equipSwitch & equip_bitmask[j]) {
  918. sd->equip_switch_index[j] = i;
  919. }
  920. }
  921. }
  922. }
  923. pc_calcweapontype(sd);
  924. }
  925. //static int pc_isAllowedCardOn(struct map_session_data *sd,int s,int eqindex,int flag)
  926. //{
  927. // int i;
  928. // struct item *item = &sd->inventory.u.items_inventory[eqindex];
  929. // struct item_data *data;
  930. //
  931. // //Crafted/made/hatched items.
  932. // if (itemdb_isspecial(item->card[0]))
  933. // return 1;
  934. //
  935. // /* scan for enchant armor gems */
  936. // if( item->card[MAX_SLOTS - 1] && s < MAX_SLOTS - 1 )
  937. // s = MAX_SLOTS - 1;
  938. //
  939. // ARR_FIND( 0, s, i, item->card[i] && (data = itemdb_exists(item->card[i])) != NULL && data->flag.no_equip&flag );
  940. // return( i < s ) ? 0 : 1;
  941. //}
  942. /**
  943. * Check if an item is equiped by player
  944. * (Check if the itemid is equiped then search if that match the index in inventory (should be))
  945. * @param sd : player session
  946. * @param nameid : itemid
  947. * @return 1:yes, 0:no
  948. */
  949. bool pc_isequipped(struct map_session_data *sd, unsigned short nameid)
  950. {
  951. uint8 i;
  952. for( i = 0; i < EQI_MAX; i++ )
  953. {
  954. short index = sd->equip_index[i], j;
  955. if( index < 0 )
  956. continue;
  957. if( pc_is_same_equip_index((enum equip_index)i, sd->equip_index, index) )
  958. continue;
  959. if( !sd->inventory_data[index] )
  960. continue;
  961. if( sd->inventory_data[index]->nameid == nameid )
  962. return true;
  963. for( j = 0; j < sd->inventory_data[index]->slot; j++ ){
  964. if( sd->inventory.u.items_inventory[index].card[j] == nameid )
  965. return true;
  966. }
  967. }
  968. return false;
  969. }
  970. /**
  971. * Check adoption rules
  972. * @param p1_sd: Player 1
  973. * @param p2_sd: Player 2
  974. * @param b_sd: Player that will be adopted
  975. * @return ADOPT_ALLOWED - Sent message to Baby to accept or deny
  976. * ADOPT_ALREADY_ADOPTED - Already adopted
  977. * ADOPT_MARRIED_AND_PARTY - Need to be married and in the same party
  978. * ADOPT_EQUIP_RINGS - Need wedding rings equipped
  979. * ADOPT_NOT_NOVICE - Adoptee is not a Novice
  980. * ADOPT_CHARACTER_NOT_FOUND - Parent or Baby not found
  981. * ADOPT_MORE_CHILDREN - Cannot adopt more than 1 Baby (client message)
  982. * ADOPT_LEVEL_70 - Parents need to be level 70+ (client message)
  983. * ADOPT_MARRIED - Cannot adopt a married person (client message)
  984. */
  985. enum adopt_responses pc_try_adopt(struct map_session_data *p1_sd, struct map_session_data *p2_sd, struct map_session_data *b_sd)
  986. {
  987. if( !p1_sd || !p2_sd || !b_sd )
  988. return ADOPT_CHARACTER_NOT_FOUND;
  989. if( b_sd->status.father || b_sd->status.mother || b_sd->adopt_invite )
  990. return ADOPT_ALREADY_ADOPTED; // already adopted baby / in adopt request
  991. if( !p1_sd->status.partner_id || !p1_sd->status.party_id || p1_sd->status.party_id != b_sd->status.party_id )
  992. return ADOPT_MARRIED_AND_PARTY; // You need to be married and in party with baby to adopt
  993. if( p1_sd->status.partner_id != p2_sd->status.char_id || p2_sd->status.partner_id != p1_sd->status.char_id )
  994. return ADOPT_MARRIED_AND_PARTY; // Not married, wrong married
  995. if( p2_sd->status.party_id != p1_sd->status.party_id )
  996. return ADOPT_MARRIED_AND_PARTY; // Both parents need to be in the same party
  997. // Parents need to have their ring equipped
  998. if( !pc_isequipped(p1_sd, WEDDING_RING_M) && !pc_isequipped(p1_sd, WEDDING_RING_F) )
  999. return ADOPT_EQUIP_RINGS;
  1000. if( !pc_isequipped(p2_sd, WEDDING_RING_M) && !pc_isequipped(p2_sd, WEDDING_RING_F) )
  1001. return ADOPT_EQUIP_RINGS;
  1002. // Already adopted a baby
  1003. if( p1_sd->status.child || p2_sd->status.child ) {
  1004. clif_Adopt_reply(p1_sd, ADOPT_REPLY_MORE_CHILDREN);
  1005. return ADOPT_MORE_CHILDREN;
  1006. }
  1007. // Parents need at least lvl 70 to adopt
  1008. if( p1_sd->status.base_level < 70 || p2_sd->status.base_level < 70 ) {
  1009. clif_Adopt_reply(p1_sd, ADOPT_REPLY_LEVEL_70);
  1010. return ADOPT_LEVEL_70;
  1011. }
  1012. if( b_sd->status.partner_id ) {
  1013. clif_Adopt_reply(p1_sd, ADOPT_REPLY_MARRIED);
  1014. return ADOPT_MARRIED;
  1015. }
  1016. 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 ) )
  1017. return ADOPT_NOT_NOVICE;
  1018. return ADOPT_ALLOWED;
  1019. }
  1020. /*==========================================
  1021. * Adoption Process
  1022. *------------------------------------------*/
  1023. bool pc_adoption(struct map_session_data *p1_sd, struct map_session_data *p2_sd, struct map_session_data *b_sd)
  1024. {
  1025. int job, joblevel;
  1026. unsigned int jobexp;
  1027. if( pc_try_adopt(p1_sd, p2_sd, b_sd) != ADOPT_ALLOWED )
  1028. return false;
  1029. // Preserve current job levels and progress
  1030. joblevel = b_sd->status.job_level;
  1031. jobexp = b_sd->status.job_exp;
  1032. job = pc_mapid2jobid(b_sd->class_|JOBL_BABY, b_sd->status.sex);
  1033. if( job != -1 && pc_jobchange(b_sd, job, 0) )
  1034. { // Success, proceed to configure parents and baby skills
  1035. p1_sd->status.child = b_sd->status.char_id;
  1036. p2_sd->status.child = b_sd->status.char_id;
  1037. b_sd->status.father = p1_sd->status.char_id;
  1038. b_sd->status.mother = p2_sd->status.char_id;
  1039. // Restore progress
  1040. b_sd->status.job_level = joblevel;
  1041. clif_updatestatus(b_sd, SP_JOBLEVEL);
  1042. b_sd->status.job_exp = jobexp;
  1043. clif_updatestatus(b_sd, SP_JOBEXP);
  1044. // Baby Skills
  1045. pc_skill(b_sd, WE_BABY, 1, ADDSKILL_PERMANENT);
  1046. pc_skill(b_sd, WE_CALLPARENT, 1, ADDSKILL_PERMANENT);
  1047. pc_skill(b_sd, WE_CHEERUP, 1, ADDSKILL_PERMANENT);
  1048. // Parents Skills
  1049. pc_skill(p1_sd, WE_CALLBABY, 1, ADDSKILL_PERMANENT);
  1050. pc_skill(p2_sd, WE_CALLBABY, 1, ADDSKILL_PERMANENT);
  1051. chrif_save(p1_sd, CSAVE_NORMAL);
  1052. chrif_save(p2_sd, CSAVE_NORMAL);
  1053. chrif_save(b_sd, CSAVE_NORMAL);
  1054. achievement_update_objective(b_sd, AG_BABY, 1, 1);
  1055. achievement_update_objective(p1_sd, AG_BABY, 1, 2);
  1056. achievement_update_objective(p2_sd, AG_BABY, 1, 2);
  1057. return true;
  1058. }
  1059. return false; // Job Change Fail
  1060. }
  1061. /*==========================================
  1062. * Check if player can use/equip selected item. Used by pc_isUseitem and pc_isequip
  1063. Returns:
  1064. false : Cannot use/equip
  1065. true : Can use/equip
  1066. * Credits:
  1067. [Inkfish] for first idea
  1068. [Haru] for third-classes extension
  1069. [Cydh] finishing :D
  1070. *------------------------------------------*/
  1071. static bool pc_isItemClass (struct map_session_data *sd, struct item_data* item) {
  1072. while (1) {
  1073. if (item->class_upper&ITEMJ_NORMAL && !(sd->class_&(JOBL_UPPER|JOBL_THIRD|JOBL_BABY))) //normal classes (no upper, no baby, no third)
  1074. break;
  1075. #ifndef RENEWAL
  1076. //allow third classes to use trans. class items
  1077. if (item->class_upper&ITEMJ_UPPER && sd->class_&(JOBL_UPPER|JOBL_THIRD)) //trans. classes
  1078. break;
  1079. //third-baby classes can use same item too
  1080. if (item->class_upper&ITEMJ_BABY && sd->class_&JOBL_BABY) //baby classes
  1081. break;
  1082. //don't need to decide specific rules for third-classes?
  1083. //items for third classes can be used for all third classes
  1084. if (item->class_upper&(ITEMJ_THIRD|ITEMJ_THIRD_TRANS|ITEMJ_THIRD_BABY) && sd->class_&JOBL_THIRD)
  1085. break;
  1086. #else
  1087. //trans. classes (exl. third-trans.)
  1088. if (item->class_upper&ITEMJ_UPPER && sd->class_&JOBL_UPPER && !(sd->class_&JOBL_THIRD))
  1089. break;
  1090. //baby classes (exl. third-baby)
  1091. if (item->class_upper&ITEMJ_BABY && sd->class_&JOBL_BABY && !(sd->class_&JOBL_THIRD))
  1092. break;
  1093. //third classes (exl. third-trans. and baby-third)
  1094. if (item->class_upper&ITEMJ_THIRD && sd->class_&JOBL_THIRD && !(sd->class_&(JOBL_UPPER|JOBL_BABY)))
  1095. break;
  1096. //trans-third classes
  1097. if (item->class_upper&ITEMJ_THIRD_TRANS && sd->class_&JOBL_THIRD && sd->class_&JOBL_UPPER)
  1098. break;
  1099. //third-baby classes
  1100. if (item->class_upper&ITEMJ_THIRD_BABY && sd->class_&JOBL_THIRD && sd->class_&JOBL_BABY)
  1101. break;
  1102. #endif
  1103. return false;
  1104. }
  1105. return true;
  1106. }
  1107. /*=================================================
  1108. * Checks if the player can equip the item at index n in inventory.
  1109. * @param sd
  1110. * @param n Item index in inventory
  1111. * @return ITEM_EQUIP_ACK_OK(0) if can be equipped, or ITEM_EQUIP_ACK_FAIL(1)/ITEM_EQUIP_ACK_FAILLEVEL(2) if can't
  1112. *------------------------------------------------*/
  1113. uint8 pc_isequip(struct map_session_data *sd,int n)
  1114. {
  1115. struct item_data *item;
  1116. nullpo_retr(ITEM_EQUIP_ACK_FAIL, sd);
  1117. item = sd->inventory_data[n];
  1118. if(pc_has_permission(sd, PC_PERM_USE_ALL_EQUIPMENT))
  1119. return ITEM_EQUIP_ACK_OK;
  1120. if(item == NULL)
  1121. return ITEM_EQUIP_ACK_FAIL;
  1122. if(item->elv && sd->status.base_level < (unsigned int)item->elv)
  1123. return ITEM_EQUIP_ACK_FAILLEVEL;
  1124. if(item->elvmax && sd->status.base_level > (unsigned int)item->elvmax)
  1125. return ITEM_EQUIP_ACK_FAILLEVEL;
  1126. if(item->sex != 2 && sd->status.sex != item->sex)
  1127. return ITEM_EQUIP_ACK_FAIL;
  1128. //fail to equip if item is restricted
  1129. if (!battle_config.allow_equip_restricted_item && itemdb_isNoEquip(item, sd->bl.m))
  1130. return ITEM_EQUIP_ACK_FAIL;
  1131. if (item->equip&EQP_AMMO) {
  1132. switch (item->look) {
  1133. case AMMO_ARROW:
  1134. if (battle_config.ammo_check_weapon && sd->status.weapon != W_BOW && sd->status.weapon != W_MUSICAL && sd->status.weapon != W_WHIP) {
  1135. clif_msg(sd, ITEM_NEED_BOW);
  1136. return ITEM_EQUIP_ACK_FAIL;
  1137. }
  1138. break;
  1139. case AMMO_THROWABLE_DAGGER:
  1140. if (!pc_checkskill(sd, AS_VENOMKNIFE))
  1141. return ITEM_EQUIP_ACK_FAIL;
  1142. break;
  1143. case AMMO_BULLET:
  1144. case AMMO_SHELL:
  1145. 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
  1146. #ifdef RENEWAL
  1147. && sd->status.weapon != W_GRENADE
  1148. #endif
  1149. ) {
  1150. clif_msg(sd, ITEM_BULLET_EQUIP_FAIL);
  1151. return ITEM_EQUIP_ACK_FAIL;
  1152. }
  1153. break;
  1154. #ifndef RENEWAL
  1155. case AMMO_GRENADE:
  1156. if (battle_config.ammo_check_weapon && sd->status.weapon != W_GRENADE) {
  1157. clif_msg(sd, ITEM_BULLET_EQUIP_FAIL);
  1158. return ITEM_EQUIP_ACK_FAIL;
  1159. }
  1160. break;
  1161. #endif
  1162. case AMMO_CANNONBALL:
  1163. if (!pc_ismadogear(sd) && (sd->status.class_ == JOB_MECHANIC_T || sd->status.class_ == JOB_MECHANIC)) {
  1164. clif_msg(sd, ITEM_NEED_MADOGEAR); // Item can only be used when Mado Gear is mounted.
  1165. return ITEM_EQUIP_ACK_FAIL;
  1166. }
  1167. if (sd->state.active && !pc_iscarton(sd) && //Check if sc data is already loaded
  1168. (sd->status.class_ == JOB_GENETIC_T || sd->status.class_ == JOB_GENETIC)) {
  1169. clif_msg(sd, ITEM_NEED_CART); // Only available when cart is mounted.
  1170. return ITEM_EQUIP_ACK_FAIL;
  1171. }
  1172. break;
  1173. }
  1174. }
  1175. if (sd->sc.count) {
  1176. if(item->equip & EQP_ARMS && item->type == IT_WEAPON && sd->sc.data[SC_STRIPWEAPON]) // Also works with left-hand weapons [DracoRPG]
  1177. return ITEM_EQUIP_ACK_FAIL;
  1178. if(item->equip & EQP_SHIELD && item->type == IT_ARMOR && sd->sc.data[SC_STRIPSHIELD])
  1179. return ITEM_EQUIP_ACK_FAIL;
  1180. if(item->equip & EQP_ARMOR && sd->sc.data[SC_STRIPARMOR])
  1181. return ITEM_EQUIP_ACK_FAIL;
  1182. if(item->equip & EQP_HEAD_TOP && sd->sc.data[SC_STRIPHELM])
  1183. return ITEM_EQUIP_ACK_FAIL;
  1184. if(item->equip & EQP_ACC && sd->sc.data[SC__STRIPACCESSORY])
  1185. return ITEM_EQUIP_ACK_FAIL;
  1186. if (item->equip & EQP_ARMS && sd->sc.data[SC__WEAKNESS])
  1187. return ITEM_EQUIP_ACK_FAIL;
  1188. if(item->equip && (sd->sc.data[SC_KYOUGAKU] || sd->sc.data[SC_SUHIDE]))
  1189. return ITEM_EQUIP_ACK_FAIL;
  1190. if (sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_SUPERNOVICE) {
  1191. //Spirit of Super Novice equip bonuses. [Skotlex]
  1192. if (sd->status.base_level > 90 && item->equip & EQP_HELM)
  1193. return ITEM_EQUIP_ACK_OK; //Can equip all helms
  1194. if (sd->status.base_level > 96 && item->equip & EQP_ARMS && item->type == IT_WEAPON && item->wlv == 4)
  1195. switch(item->look) { //In weapons, the look determines type of weapon.
  1196. case W_DAGGER: //All level 4 - Daggers
  1197. case W_1HSWORD: //All level 4 - 1H Swords
  1198. case W_1HAXE: //All level 4 - 1H Axes
  1199. case W_MACE: //All level 4 - 1H Maces
  1200. case W_STAFF: //All level 4 - 1H Staves
  1201. case W_2HSTAFF: //All level 4 - 2H Staves
  1202. return ITEM_EQUIP_ACK_OK;
  1203. }
  1204. }
  1205. }
  1206. //Not equipable by class. [Skotlex]
  1207. if (!(1ULL << (sd->class_&MAPID_BASEMASK)&item->class_base[(sd->class_&JOBL_2_1) ? 1 : ((sd->class_&JOBL_2_2) ? 2 : 0)]))
  1208. return ITEM_EQUIP_ACK_FAIL;
  1209. if (!pc_isItemClass(sd, item))
  1210. return ITEM_EQUIP_ACK_FAIL;
  1211. return ITEM_EQUIP_ACK_OK;
  1212. }
  1213. /*==========================================
  1214. * No problem with the session id
  1215. * set the status that has been sent from char server
  1216. *------------------------------------------*/
  1217. 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)
  1218. {
  1219. int i;
  1220. t_tick tick = gettick();
  1221. uint32 ip = session[sd->fd]->client_addr;
  1222. sd->login_id2 = login_id2;
  1223. sd->group_id = group_id;
  1224. /* load user permissions */
  1225. pc_group_pc_load(sd);
  1226. memcpy(&sd->status, st, sizeof(*st));
  1227. if (st->sex != sd->status.sex) {
  1228. clif_authfail_fd(sd->fd, 0);
  1229. return false;
  1230. }
  1231. //Set the map-server used job id. [Skotlex]
  1232. i = pc_jobid2mapid(sd->status.class_);
  1233. if (i == -1) { //Invalid class?
  1234. 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);
  1235. sd->status.class_ = JOB_NOVICE;
  1236. sd->class_ = MAPID_NOVICE;
  1237. } else
  1238. sd->class_ = i;
  1239. // Checks and fixes to character status data, that are required
  1240. // in case of configuration change or stuff, which cannot be
  1241. // checked on char-server.
  1242. sd->status.hair = cap_value(sd->status.hair,MIN_HAIR_STYLE,MAX_HAIR_STYLE);
  1243. sd->status.hair_color = cap_value(sd->status.hair_color,MIN_HAIR_COLOR,MAX_HAIR_COLOR);
  1244. sd->status.clothes_color = cap_value(sd->status.clothes_color,MIN_CLOTH_COLOR,MAX_CLOTH_COLOR);
  1245. sd->status.body = cap_value(sd->status.body,MIN_BODY_STYLE,MAX_BODY_STYLE);
  1246. //Initializations to null/0 unneeded since map_session_data was filled with 0 upon allocation.
  1247. sd->state.connect_new = 1;
  1248. sd->followtimer = INVALID_TIMER; // [MouseJstr]
  1249. sd->invincible_timer = INVALID_TIMER;
  1250. sd->npc_timer_id = INVALID_TIMER;
  1251. sd->pvp_timer = INVALID_TIMER;
  1252. sd->expiration_tid = INVALID_TIMER;
  1253. sd->autotrade_tid = INVALID_TIMER;
  1254. sd->respawn_tid = INVALID_TIMER;
  1255. #ifdef SECURE_NPCTIMEOUT
  1256. // Initialize to defaults/expected
  1257. sd->npc_idle_timer = INVALID_TIMER;
  1258. sd->npc_idle_tick = tick;
  1259. sd->npc_idle_type = NPCT_INPUT;
  1260. sd->state.ignoretimeout = false;
  1261. #endif
  1262. sd->canuseitem_tick = tick;
  1263. sd->canusecashfood_tick = tick;
  1264. sd->canequip_tick = tick;
  1265. sd->cantalk_tick = tick;
  1266. sd->canskill_tick = tick;
  1267. sd->cansendmail_tick = tick;
  1268. sd->idletime = last_tick;
  1269. for(i = 0; i < MAX_SPIRITBALL; i++)
  1270. sd->spirit_timer[i] = INVALID_TIMER;
  1271. for (i = 0; i < MAX_SOUL_BALL; i++)
  1272. sd->soul_timer[i] = INVALID_TIMER;
  1273. if (battle_config.item_auto_get)
  1274. sd->state.autoloot = 10000;
  1275. if (battle_config.disp_experience)
  1276. sd->state.showexp = 1;
  1277. if (battle_config.disp_zeny)
  1278. sd->state.showzeny = 1;
  1279. #ifdef VIP_ENABLE
  1280. if (!battle_config.vip_disp_rate)
  1281. sd->vip.disableshowrate = 1;
  1282. #endif
  1283. if (!(battle_config.display_skill_fail&2))
  1284. sd->state.showdelay = 1;
  1285. memset(&sd->inventory, 0, sizeof(struct s_storage));
  1286. memset(&sd->cart, 0, sizeof(struct s_storage));
  1287. memset(&sd->storage, 0, sizeof(struct s_storage));
  1288. memset(&sd->premiumStorage, 0, sizeof(struct s_storage));
  1289. memset(&sd->equip_index, -1, sizeof(sd->equip_index));
  1290. memset(&sd->equip_switch_index, -1, sizeof(sd->equip_switch_index));
  1291. if( pc_isinvisible(sd) && !pc_can_use_command( sd, "hide", COMMAND_ATCOMMAND ) ){
  1292. sd->status.option &= ~OPTION_INVISIBLE;
  1293. }
  1294. status_change_init(&sd->bl);
  1295. sd->sc.option = sd->status.option; //This is the actual option used in battle.
  1296. unit_dataset(&sd->bl);
  1297. sd->guild_x = -1;
  1298. sd->guild_y = -1;
  1299. sd->delayed_damage = 0;
  1300. // Event Timers
  1301. for( i = 0; i < MAX_EVENTTIMER; i++ )
  1302. sd->eventtimer[i] = INVALID_TIMER;
  1303. // Rental Timer
  1304. sd->rental_timer = INVALID_TIMER;
  1305. for( i = 0; i < 3; i++ )
  1306. sd->hate_mob[i] = -1;
  1307. sd->quest_log = NULL;
  1308. sd->num_quests = 0;
  1309. sd->avail_quests = 0;
  1310. sd->save_quest = false;
  1311. sd->count_rewarp = 0;
  1312. sd->regs.vars = i64db_alloc(DB_OPT_BASE);
  1313. sd->regs.arrays = NULL;
  1314. sd->vars_dirty = false;
  1315. sd->vars_ok = false;
  1316. sd->vars_received = 0x0;
  1317. sd->qi_display = nullptr;
  1318. sd->qi_count = 0;
  1319. //warp player
  1320. if ((i=pc_setpos(sd,sd->status.last_point.map, sd->status.last_point.x, sd->status.last_point.y, CLR_OUTSIGHT)) != SETPOS_OK) {
  1321. 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);
  1322. // try warping to a default map instead (church graveyard)
  1323. if (pc_setpos(sd, mapindex_name2id(MAP_PRONTERA), 273, 354, CLR_OUTSIGHT) != SETPOS_OK) {
  1324. // if we fail again
  1325. clif_authfail_fd(sd->fd, 0);
  1326. return false;
  1327. }
  1328. }
  1329. clif_authok(sd);
  1330. //Prevent S. Novices from getting the no-death bonus just yet. [Skotlex]
  1331. sd->die_counter=-1;
  1332. //display login notice
  1333. ShowInfo("'" CL_WHITE "%s" CL_RESET "' logged in."
  1334. " (AID/CID: '" CL_WHITE "%d/%d" CL_RESET "',"
  1335. " IP: '" CL_WHITE "%d.%d.%d.%d" CL_RESET "',"
  1336. " Group '" CL_WHITE "%d" CL_RESET "').\n",
  1337. sd->status.name, sd->status.account_id, sd->status.char_id,
  1338. CONVIP(ip), sd->group_id);
  1339. // Send friends list
  1340. clif_friendslist_send(sd);
  1341. if( !changing_mapservers ) {
  1342. if (battle_config.display_version == 1)
  1343. pc_show_version(sd);
  1344. // Message of the Day [Valaris]
  1345. for(i=0; i < MOTD_LINE_SIZE && motd_text[i][0]; i++) {
  1346. if (battle_config.motd_type)
  1347. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], motd_text[i], false, SELF);
  1348. else
  1349. clif_displaymessage(sd->fd, motd_text[i]);
  1350. }
  1351. if (expiration_time != 0)
  1352. sd->expiration_time = expiration_time;
  1353. /**
  1354. * Fixes login-without-aura glitch (the screen won't blink at this point, don't worry :P)
  1355. **/
  1356. clif_changemap(sd,sd->bl.m,sd->bl.x,sd->bl.y);
  1357. }
  1358. pc_validate_skill(sd);
  1359. /* [Ind] */
  1360. sd->sc_display = NULL;
  1361. sd->sc_display_count = 0;
  1362. // Player has not yet received the CashShop list
  1363. sd->status.cashshop_sent = false;
  1364. sd->last_addeditem_index = -1;
  1365. sd->bonus_script.head = NULL;
  1366. sd->bonus_script.count = 0;
  1367. // Initialize BG queue pointer
  1368. sd->bg_queue = nullptr;
  1369. sd->bg_queue_accept_state = false;
  1370. #if PACKETVER >= 20150513
  1371. sd->hatEffectIDs = NULL;
  1372. sd->hatEffectCount = 0;
  1373. #endif
  1374. sd->catch_target_class = PET_CATCH_FAIL;
  1375. // Check EXP overflow, since in previous revision EXP on Max Level can be more than 'official' Max EXP
  1376. if (pc_is_maxbaselv(sd) && sd->status.base_exp > MAX_LEVEL_BASE_EXP) {
  1377. sd->status.base_exp = MAX_LEVEL_BASE_EXP;
  1378. clif_updatestatus(sd, SP_BASEEXP);
  1379. }
  1380. if (pc_is_maxjoblv(sd) && sd->status.job_exp > MAX_LEVEL_JOB_EXP) {
  1381. sd->status.job_exp = MAX_LEVEL_JOB_EXP;
  1382. clif_updatestatus(sd, SP_JOBEXP);
  1383. }
  1384. // Request all registries (auth is considered completed whence they arrive)
  1385. intif_request_registry(sd,7);
  1386. return true;
  1387. }
  1388. /*==========================================
  1389. * Closes a connection because it failed to be authenticated from the char server.
  1390. *------------------------------------------*/
  1391. void pc_authfail(struct map_session_data *sd)
  1392. {
  1393. clif_authfail_fd(sd->fd, 0);
  1394. return;
  1395. }
  1396. /**
  1397. * Player register a bl as hatred
  1398. * @param sd : player session
  1399. * @param pos : hate position [0;2]
  1400. * @param bl : target bl
  1401. * @return false:failed, true:success
  1402. */
  1403. bool pc_set_hate_mob(struct map_session_data *sd, int pos, struct block_list *bl)
  1404. {
  1405. int class_;
  1406. if (!sd || !bl || pos < 0 || pos > 2)
  1407. return false;
  1408. if (sd->hate_mob[pos] != -1)
  1409. { //Can't change hate targets.
  1410. clif_hate_info(sd, pos, sd->hate_mob[pos], 0); //Display current
  1411. return false;
  1412. }
  1413. class_ = status_get_class(bl);
  1414. if (!pcdb_checkid(class_)) {
  1415. unsigned int max_hp = status_get_max_hp(bl);
  1416. if ((pos == 1 && max_hp < 6000) || (pos == 2 && max_hp < 20000))
  1417. return false;
  1418. if (pos != status_get_size(bl))
  1419. return false; //Wrong size
  1420. }
  1421. sd->hate_mob[pos] = class_;
  1422. pc_setglobalreg(sd, add_str(sg_info[pos].hate_var), class_+1);
  1423. clif_hate_info(sd, pos, class_, 1);
  1424. return true;
  1425. }
  1426. /*==========================================
  1427. * Invoked once after the char/account/account2 registry variables are received. [Skotlex]
  1428. * We didn't receive item information at this point so DO NOT attempt to do item operations here.
  1429. * See intif_parse_StorageReceived() for item operations [lighta]
  1430. *------------------------------------------*/
  1431. void pc_reg_received(struct map_session_data *sd)
  1432. {
  1433. uint8 i;
  1434. sd->vars_ok = true;
  1435. sd->change_level_2nd = static_cast<unsigned char>(pc_readglobalreg(sd, add_str(JOBCHANGE2ND_VAR)));
  1436. sd->change_level_3rd = static_cast<unsigned char>(pc_readglobalreg(sd, add_str(JOBCHANGE3RD_VAR)));
  1437. sd->die_counter = static_cast<int>(pc_readglobalreg(sd, add_str(PCDIECOUNTER_VAR)));
  1438. sd->langtype = static_cast<int>(pc_readaccountreg(sd, add_str(LANGTYPE_VAR)));
  1439. if (msg_checklangtype(sd->langtype,true) < 0)
  1440. sd->langtype = 0; //invalid langtype reset to default
  1441. // Cash shop
  1442. sd->cashPoints = static_cast<int>(pc_readaccountreg(sd, add_str(CASHPOINT_VAR)));
  1443. sd->kafraPoints = static_cast<int>(pc_readaccountreg(sd, add_str(KAFRAPOINT_VAR)));
  1444. // Cooking Exp
  1445. sd->cook_mastery = static_cast<short>(pc_readglobalreg(sd, add_str(COOKMASTERY_VAR)));
  1446. if( (sd->class_&MAPID_BASEMASK) == MAPID_TAEKWON )
  1447. { // Better check for class rather than skill to prevent "skill resets" from unsetting this
  1448. sd->mission_mobid = static_cast<short>(pc_readglobalreg(sd, add_str(TKMISSIONID_VAR)));
  1449. sd->mission_count = static_cast<unsigned char>(pc_readglobalreg(sd, add_str(TKMISSIONCOUNT_VAR)));
  1450. }
  1451. if (battle_config.feature_banking)
  1452. sd->bank_vault = static_cast<int>(pc_readreg2(sd, BANK_VAULT_VAR));
  1453. if (battle_config.feature_roulette) {
  1454. sd->roulette_point.bronze = static_cast<int>(pc_readreg2(sd, ROULETTE_BRONZE_VAR));
  1455. sd->roulette_point.silver = static_cast<int>(pc_readreg2(sd, ROULETTE_SILVER_VAR));
  1456. sd->roulette_point.gold = static_cast<int>(pc_readreg2(sd, ROULETTE_GOLD_VAR));
  1457. }
  1458. sd->roulette.prizeIdx = -1;
  1459. //SG map and mob read [Komurka]
  1460. for(i=0;i<MAX_PC_FEELHATE;i++) { //for now - someone need to make reading from txt/sql
  1461. uint16 j;
  1462. if ((j = static_cast<uint16>(pc_readglobalreg(sd, add_str(sg_info[i].feel_var)))) != 0) {
  1463. sd->feel_map[i].index = j;
  1464. sd->feel_map[i].m = map_mapindex2mapid(j);
  1465. } else {
  1466. sd->feel_map[i].index = 0;
  1467. sd->feel_map[i].m = -1;
  1468. }
  1469. sd->hate_mob[i] = static_cast<short>(pc_readglobalreg(sd, add_str(sg_info[i].hate_var)))-1;
  1470. }
  1471. if ((i = pc_checkskill(sd,RG_PLAGIARISM)) > 0) {
  1472. unsigned short skid = static_cast<unsigned short>(pc_readglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM)));
  1473. sd->cloneskill_idx = skill_get_index(skid);
  1474. if (sd->cloneskill_idx > 0) {
  1475. sd->status.skill[sd->cloneskill_idx].id = skid;
  1476. sd->status.skill[sd->cloneskill_idx].lv = static_cast<uint8>(pc_readglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM_LV)));
  1477. if (sd->status.skill[sd->cloneskill_idx].lv > i)
  1478. sd->status.skill[sd->cloneskill_idx].lv = i;
  1479. sd->status.skill[sd->cloneskill_idx].flag = SKILL_FLAG_PLAGIARIZED;
  1480. }
  1481. }
  1482. if ((i = pc_checkskill(sd,SC_REPRODUCE)) > 0) {
  1483. unsigned short skid = static_cast<unsigned short>(pc_readglobalreg(sd, add_str(SKILL_VAR_REPRODUCE)));
  1484. sd->reproduceskill_idx = skill_get_index(skid);
  1485. if (sd->reproduceskill_idx > 0) {
  1486. sd->status.skill[sd->reproduceskill_idx].id = skid;
  1487. sd->status.skill[sd->reproduceskill_idx].lv = static_cast<uint8>(pc_readglobalreg(sd, add_str(SKILL_VAR_REPRODUCE_LV)));
  1488. if (i < sd->status.skill[sd->reproduceskill_idx].lv)
  1489. sd->status.skill[sd->reproduceskill_idx].lv = i;
  1490. sd->status.skill[sd->reproduceskill_idx].flag = SKILL_FLAG_PLAGIARIZED;
  1491. }
  1492. }
  1493. //Weird... maybe registries were reloaded?
  1494. if (sd->state.active)
  1495. return;
  1496. sd->state.active = 1;
  1497. sd->state.pc_loaded = false; // Ensure inventory data and status data is loaded before we calculate player stats
  1498. intif_storage_request(sd,TABLE_STORAGE, 0, STOR_MODE_ALL); // Request storage data
  1499. intif_storage_request(sd,TABLE_CART, 0, STOR_MODE_ALL); // Request cart data
  1500. intif_storage_request(sd,TABLE_INVENTORY, 0, STOR_MODE_ALL); // Request inventory data
  1501. if (sd->status.party_id)
  1502. party_member_joined(sd);
  1503. if (sd->status.guild_id)
  1504. guild_member_joined(sd);
  1505. if( sd->status.clan_id )
  1506. clan_member_joined(sd);
  1507. // pet
  1508. if (sd->status.pet_id > 0)
  1509. intif_request_petdata(sd->status.account_id, sd->status.char_id, sd->status.pet_id);
  1510. // Homunculus [albator]
  1511. if( sd->status.hom_id > 0 )
  1512. intif_homunculus_requestload(sd->status.account_id, sd->status.hom_id);
  1513. if( sd->status.mer_id > 0 )
  1514. intif_mercenary_request(sd->status.mer_id, sd->status.char_id);
  1515. if( sd->status.ele_id > 0 )
  1516. intif_elemental_request(sd->status.ele_id, sd->status.char_id);
  1517. map_addiddb(&sd->bl);
  1518. map_delnickdb(sd->status.char_id, sd->status.name);
  1519. if (!chrif_auth_finished(sd))
  1520. ShowError("pc_reg_received: Failed to properly remove player %d:%d from logging db!\n", sd->status.account_id, sd->status.char_id);
  1521. chrif_skillcooldown_request(sd->status.account_id, sd->status.char_id);
  1522. chrif_bsdata_request(sd->status.char_id);
  1523. #ifdef VIP_ENABLE
  1524. sd->vip.time = 0;
  1525. sd->vip.enabled = 0;
  1526. chrif_req_login_operation(sd->status.account_id, sd->status.name, CHRIF_OP_LOGIN_VIP, 0, 1|8, 0); // request VIP information
  1527. #endif
  1528. intif_Mail_requestinbox(sd->status.char_id, 0, MAIL_INBOX_NORMAL); // MAIL SYSTEM - Request Mail Inbox
  1529. intif_request_questlog(sd);
  1530. if (battle_config.feature_achievement) {
  1531. sd->achievement_data.total_score = 0;
  1532. sd->achievement_data.level = 0;
  1533. sd->achievement_data.save = false;
  1534. sd->achievement_data.count = 0;
  1535. sd->achievement_data.incompleteCount = 0;
  1536. sd->achievement_data.achievements = NULL;
  1537. intif_request_achievements(sd->status.char_id);
  1538. }
  1539. if (sd->state.connect_new == 0 && sd->fd) { //Character already loaded map! Gotta trigger LoadEndAck manually.
  1540. sd->state.connect_new = 1;
  1541. clif_parse_LoadEndAck(sd->fd, sd);
  1542. if( pc_isinvisible( sd ) ){
  1543. sd->vd.class_ = JT_INVISIBLE;
  1544. clif_displaymessage( sd->fd, msg_txt( sd, 11 ) ); // Invisible: On
  1545. // decrement the number of pvp players on the map
  1546. map_getmapdata( sd->bl.m )->users_pvp--;
  1547. if( map_getmapflag( sd->bl.m, MF_PVP ) && !map_getmapflag( sd->bl.m, MF_PVP_NOCALCRANK ) && sd->pvp_timer != INVALID_TIMER ){
  1548. // unregister the player for ranking
  1549. delete_timer( sd->pvp_timer, pc_calc_pvprank_timer );
  1550. sd->pvp_timer = INVALID_TIMER;
  1551. }
  1552. clif_changeoption( &sd->bl );
  1553. }
  1554. }
  1555. channel_autojoin(sd);
  1556. }
  1557. static int pc_calc_skillpoint(struct map_session_data* sd)
  1558. {
  1559. uint16 i, skill_point = 0;
  1560. nullpo_ret(sd);
  1561. for(i = 1; i < MAX_SKILL; i++) {
  1562. if( sd->status.skill[i].id && sd->status.skill[i].lv > 0) {
  1563. std::shared_ptr<s_skill_db> skill = skill_db.find(sd->status.skill[i].id);
  1564. if ((!skill->inf2[INF2_ISQUEST] || battle_config.quest_skill_learn) &&
  1565. (!skill->inf2[INF2_ISWEDDING] || skill->inf2[INF2_ISSPIRIT]) //Do not count wedding/link skills. [Skotlex]
  1566. )
  1567. {
  1568. if(sd->status.skill[i].flag == SKILL_FLAG_PERMANENT)
  1569. skill_point += sd->status.skill[i].lv;
  1570. else if(sd->status.skill[i].flag >= SKILL_FLAG_REPLACED_LV_0)
  1571. skill_point += (sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0);
  1572. }
  1573. }
  1574. }
  1575. return skill_point;
  1576. }
  1577. static bool pc_grant_allskills(struct map_session_data *sd, bool addlv) {
  1578. if (!sd || !pc_has_permission(sd, PC_PERM_ALL_SKILL))
  1579. return false;
  1580. /**
  1581. * Dummy skills must NOT be added here otherwise they'll be displayed in the,
  1582. * skill tree and since they have no icons they'll give resource errors
  1583. * Get ALL skills except npc/guild ones. [Skotlex]
  1584. * Don't add SG_DEVIL [Komurka] and MO_TRIPLEATTACK and RG_SNATCHER [ultramage]
  1585. **/
  1586. for (const auto &skill : skill_db) {
  1587. uint16 skill_id = skill.second->nameid;
  1588. if (skill_id == 0 || skill.second->inf2[INF2_ISNPC]|| skill.second->inf2[INF2_ISGUILD])
  1589. continue;
  1590. switch (skill_id) {
  1591. case SM_SELFPROVOKE:
  1592. case AB_DUPLELIGHT_MELEE:
  1593. case AB_DUPLELIGHT_MAGIC:
  1594. case WL_CHAINLIGHTNING_ATK:
  1595. case WL_TETRAVORTEX_FIRE:
  1596. case WL_TETRAVORTEX_WATER:
  1597. case WL_TETRAVORTEX_WIND:
  1598. case WL_TETRAVORTEX_GROUND:
  1599. case WL_SUMMON_ATK_FIRE:
  1600. case WL_SUMMON_ATK_WIND:
  1601. case WL_SUMMON_ATK_WATER:
  1602. case WL_SUMMON_ATK_GROUND:
  1603. case LG_OVERBRAND_BRANDISH:
  1604. case LG_OVERBRAND_PLUSATK:
  1605. case WM_SEVERE_RAINSTORM_MELEE:
  1606. case RL_R_TRIP_PLUSATK:
  1607. case SG_DEVIL:
  1608. case MO_TRIPLEATTACK:
  1609. case RG_SNATCHER:
  1610. continue;
  1611. default:
  1612. {
  1613. uint8 lv = (uint8)skill_get_max(skill_id);
  1614. if (lv > 0) {
  1615. uint16 idx = skill_get_index(skill_id);
  1616. sd->status.skill[idx].id = skill_id;
  1617. if (addlv)
  1618. sd->status.skill[idx].lv = lv;
  1619. }
  1620. }
  1621. break;
  1622. }
  1623. }
  1624. return true;
  1625. }
  1626. /*==========================================
  1627. * Calculation of skill level.
  1628. * @param sd
  1629. *------------------------------------------*/
  1630. void pc_calc_skilltree(struct map_session_data *sd)
  1631. {
  1632. nullpo_retv(sd);
  1633. int job = pc_calc_skilltree_normalize_job(sd);
  1634. int class_ = pc_mapid2jobid(job, sd->status.sex);
  1635. if( class_ == -1 )
  1636. { //Unable to normalize job??
  1637. 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);
  1638. return;
  1639. }
  1640. class_ = pc_class2idx(class_);
  1641. for (const auto &skill : skill_db) {
  1642. uint16 skill_id = skill.second->nameid;
  1643. uint16 idx = skill_get_index(skill_id);
  1644. if( sd->status.skill[idx].flag != SKILL_FLAG_PLAGIARIZED && sd->status.skill[idx].flag != SKILL_FLAG_PERM_GRANTED ) //Don't touch these
  1645. sd->status.skill[idx].id = 0; //First clear skills.
  1646. /* permanent skills that must be re-checked */
  1647. if( sd->status.skill[idx].flag == SKILL_FLAG_PERM_GRANTED ) {
  1648. if (skill_id == 0) {
  1649. sd->status.skill[idx].id = 0;
  1650. sd->status.skill[idx].lv = 0;
  1651. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  1652. continue;
  1653. }
  1654. switch (skill_id) {
  1655. case NV_TRICKDEAD:
  1656. if( (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE ) {
  1657. sd->status.skill[idx].id = 0;
  1658. sd->status.skill[idx].lv = 0;
  1659. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  1660. }
  1661. break;
  1662. }
  1663. }
  1664. }
  1665. for (const auto &skill : skill_db) {
  1666. uint16 skill_id = skill.second->nameid;
  1667. uint16 idx = skill_get_index(skill_id);
  1668. // Restore original level of skills after deleting earned skills.
  1669. 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 ) {
  1670. sd->status.skill[idx].lv = (sd->status.skill[idx].flag == SKILL_FLAG_TEMPORARY) ? 0 : sd->status.skill[idx].flag - SKILL_FLAG_REPLACED_LV_0;
  1671. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  1672. }
  1673. //Enable Bard/Dancer spirit linked skills.
  1674. if (skill_id < DC_HUMMING || skill_id > DC_SERVICEFORYOU)
  1675. continue;
  1676. if( sd->sc.count && sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_BARDDANCER ) {
  1677. //Link Dancer skills to bard.
  1678. if( sd->status.sex ) {
  1679. if( sd->status.skill[idx - 8].lv < 10 )
  1680. continue;
  1681. sd->status.skill[idx].id = skill_id;
  1682. sd->status.skill[idx].lv = sd->status.skill[idx - 8].lv; // Set the level to the same as the linking skill
  1683. sd->status.skill[idx].flag = SKILL_FLAG_TEMPORARY; // Tag it as a non-savable, non-uppable, bonus skill
  1684. }
  1685. //Link Bard skills to dancer.
  1686. else {
  1687. if( sd->status.skill[idx].lv < 10 )
  1688. continue;
  1689. sd->status.skill[idx - 8].id = skill_id - 8;
  1690. sd->status.skill[idx - 8].lv = sd->status.skill[idx].lv; // Set the level to the same as the linking skill
  1691. sd->status.skill[idx - 8].flag = SKILL_FLAG_TEMPORARY; // Tag it as a non-savable, non-uppable, bonus skill
  1692. }
  1693. }
  1694. }
  1695. // Removes Taekwon Ranker skill bonus
  1696. if ((sd->class_&MAPID_UPPERMASK) != MAPID_TAEKWON) {
  1697. uint16 c_ = pc_class2idx(JOB_TAEKWON);
  1698. for (uint16 i = 0; i < MAX_SKILL_TREE; i++) {
  1699. uint16 sk_id = skill_tree[c_][i].skill_id;
  1700. uint16 sk_idx = 0;
  1701. if (!sk_id || !(sk_idx = skill_get_index(skill_tree[c_][i].skill_id)))
  1702. continue;
  1703. if (sd->status.skill[sk_idx].flag != SKILL_FLAG_PLAGIARIZED && sd->status.skill[sk_idx].flag != SKILL_FLAG_PERM_GRANTED) {
  1704. if (sk_id == NV_BASIC || sk_id == NV_FIRSTAID || sk_id == WE_CALLBABY)
  1705. continue;
  1706. sd->status.skill[sk_idx].id = 0;
  1707. }
  1708. }
  1709. }
  1710. // Grant all skills
  1711. pc_grant_allskills(sd, false);
  1712. int flag;
  1713. do {
  1714. uint16 skid = 0;
  1715. flag = 0;
  1716. for (uint16 i = 0; i < MAX_SKILL_TREE && (skid = skill_tree[class_][i].skill_id) > 0; i++) {
  1717. bool fail = false;
  1718. uint16 sk_idx = skill_get_index(skid);
  1719. if (sd->status.skill[sk_idx].id)
  1720. continue; //Skill already known.
  1721. if (!battle_config.skillfree) {
  1722. // Checking required skills
  1723. for(uint8 j = 0; j < MAX_PC_SKILL_REQUIRE; j++) {
  1724. uint16 sk_need_id = skill_tree[class_][i].need[j].skill_id;
  1725. uint16 sk_need_idx = 0;
  1726. if (sk_need_id && (sk_need_idx = skill_get_index(sk_need_id))) {
  1727. short sk_need = sk_need_id;
  1728. 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)
  1729. sk_need = 0; //Not learned.
  1730. else if (sd->status.skill[sk_need_idx].flag >= SKILL_FLAG_REPLACED_LV_0) //Real learned level
  1731. sk_need = sd->status.skill[sk_need_idx].flag - SKILL_FLAG_REPLACED_LV_0;
  1732. else
  1733. sk_need = pc_checkskill(sd,sk_need_id);
  1734. if (sk_need < skill_tree[class_][i].need[j].skill_lv) {
  1735. fail = true;
  1736. break;
  1737. }
  1738. }
  1739. }
  1740. if (sd->status.base_level < skill_tree[class_][i].baselv) { //We need to get the actual class in this case
  1741. int c_ = pc_mapid2jobid(sd->class_, sd->status.sex);
  1742. c_ = pc_class2idx(c_);
  1743. if (class_ == c_ || (class_ != c_ && sd->status.base_level < skill_tree[class_][i].baselv))
  1744. fail = true; // base level requirement wasn't satisfied
  1745. }
  1746. if (sd->status.job_level < skill_tree[class_][i].joblv) { //We need to get the actual class in this case
  1747. int c_ = pc_mapid2jobid(sd->class_, sd->status.sex);
  1748. c_ = pc_class2idx(c_);
  1749. if (class_ == c_ || (class_ != c_ && sd->status.job_level < skill_tree[class_][i].joblv))
  1750. fail = true; // job level requirement wasn't satisfied
  1751. }
  1752. }
  1753. if (!fail) {
  1754. std::shared_ptr<s_skill_db> skill = skill_db.find(skid);
  1755. if (!sd->status.skill[sk_idx].lv && (
  1756. (skill->inf2[INF2_ISQUEST] && !battle_config.quest_skill_learn) ||
  1757. skill->inf2[INF2_ISWEDDING] ||
  1758. (skill->inf2[INF2_ISSPIRIT] && !sd->sc.data[SC_SPIRIT])
  1759. ))
  1760. continue; //Cannot be learned via normal means. Note this check DOES allows raising already known skills.
  1761. sd->status.skill[sk_idx].id = skid;
  1762. if(skill->inf2[INF2_ISSPIRIT]) { //Spirit skills cannot be learned, they will only show up on your tree when you get buffed.
  1763. sd->status.skill[sk_idx].lv = 1; // need to manually specify a skill level
  1764. sd->status.skill[sk_idx].flag = SKILL_FLAG_TEMPORARY; //So it is not saved, and tagged as a "bonus" skill.
  1765. }
  1766. flag = 1; // skill list has changed, perform another pass
  1767. }
  1768. }
  1769. } while(flag);
  1770. if( class_ > 0 && sd->status.skill_point == 0 && pc_is_taekwon_ranker(sd) ) {
  1771. uint16 skid = 0;
  1772. /* Taekwon Ranker Bonus Skill Tree
  1773. ============================================
  1774. - Grant All Taekwon Tree, but only as Bonus Skills in case they drop from ranking.
  1775. - (c > 0) to avoid grant Novice Skill Tree in case of Skill Reset (need more logic)
  1776. - (sd->status.skill_point == 0) to wait until all skill points are assigned to avoid problems with Job Change quest. */
  1777. for( uint16 i = 0; i < MAX_SKILL_TREE && (skid = skill_tree[class_][i].skill_id) > 0; i++ ) {
  1778. uint16 sk_idx = skill_get_index(skid);
  1779. if (sk_idx == 0)
  1780. continue;
  1781. if( skill_get_inf2_(skid, { INF2_ISQUEST, INF2_ISWEDDING }) )
  1782. continue; //Do not include Quest/Wedding skills.
  1783. if( sd->status.skill[sk_idx].id == 0 ) {
  1784. sd->status.skill[sk_idx].id = skid;
  1785. sd->status.skill[sk_idx].flag = SKILL_FLAG_TEMPORARY; // So it is not saved, and tagged as a "bonus" skill.
  1786. } else if( skid != NV_BASIC )
  1787. sd->status.skill[sk_idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[sk_idx].lv; // Remember original level
  1788. sd->status.skill[sk_idx].lv = skill_tree_get_max(skid, sd->status.class_);
  1789. }
  1790. }
  1791. }
  1792. //Checks if you can learn a new skill after having leveled up a skill.
  1793. static void pc_check_skilltree(struct map_session_data *sd)
  1794. {
  1795. int i, flag = 0;
  1796. int c = 0;
  1797. if (battle_config.skillfree)
  1798. return; //Function serves no purpose if this is set
  1799. i = pc_calc_skilltree_normalize_job(sd);
  1800. c = pc_mapid2jobid(i, sd->status.sex);
  1801. if (c == -1) { //Unable to normalize job??
  1802. 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);
  1803. return;
  1804. }
  1805. c = pc_class2idx(c);
  1806. do {
  1807. uint16 skid = 0;
  1808. flag = 0;
  1809. for (i = 0; i < MAX_SKILL_TREE && (skid = skill_tree[c][i].skill_id) > 0; i++ ) {
  1810. uint16 sk_idx = skill_get_index(skid);
  1811. bool fail = false;
  1812. uint8 j = 0;
  1813. if (sd->status.skill[sk_idx].id) //Already learned
  1814. continue;
  1815. // Checking required skills
  1816. for (j = 0; j < MAX_PC_SKILL_REQUIRE; j++) {
  1817. uint16 sk_need_id = skill_tree[c][i].need[j].skill_id;
  1818. uint16 sk_need_idx = 0;
  1819. if (sk_need_id && (sk_need_idx = skill_get_index(sk_need_id))) {
  1820. short sk_need = sk_need_id;
  1821. 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)
  1822. sk_need = 0; //Not learned.
  1823. else if (sd->status.skill[sk_need_idx].flag >= SKILL_FLAG_REPLACED_LV_0) //Real lerned level
  1824. sk_need = sd->status.skill[sk_need_idx].flag - SKILL_FLAG_REPLACED_LV_0;
  1825. else
  1826. sk_need = pc_checkskill(sd,sk_need_id);
  1827. if (sk_need < skill_tree[c][i].need[j].skill_lv) {
  1828. fail = true;
  1829. break;
  1830. }
  1831. }
  1832. }
  1833. if( fail )
  1834. continue;
  1835. if (sd->status.base_level < skill_tree[c][i].baselv || sd->status.job_level < skill_tree[c][i].joblv)
  1836. continue;
  1837. std::shared_ptr<s_skill_db> skill = skill_db.find(skid);
  1838. if( !sd->status.skill[sk_idx].lv && (
  1839. (skill->inf2[INF2_ISQUEST] && !battle_config.quest_skill_learn) ||
  1840. skill->inf2[INF2_ISWEDDING] ||
  1841. (skill->inf2[INF2_ISSPIRIT] && !sd->sc.data[SC_SPIRIT])
  1842. ) )
  1843. continue; //Cannot be learned via normal means.
  1844. sd->status.skill[sk_idx].id = skid;
  1845. flag = 1;
  1846. }
  1847. } while(flag);
  1848. }
  1849. // Make sure all the skills are in the correct condition
  1850. // before persisting to the backend.. [MouseJstr]
  1851. void pc_clean_skilltree(struct map_session_data *sd)
  1852. {
  1853. uint16 i;
  1854. for (i = 0; i < MAX_SKILL; i++){
  1855. if (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY || sd->status.skill[i].flag == SKILL_FLAG_PLAGIARIZED) {
  1856. sd->status.skill[i].id = 0;
  1857. sd->status.skill[i].lv = 0;
  1858. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1859. }
  1860. else if (sd->status.skill[i].flag >= SKILL_FLAG_REPLACED_LV_0){
  1861. sd->status.skill[i].lv = sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
  1862. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  1863. }
  1864. }
  1865. }
  1866. int pc_calc_skilltree_normalize_job(struct map_session_data *sd)
  1867. {
  1868. int skill_point, novice_skills;
  1869. int c = sd->class_;
  1870. if (!battle_config.skillup_limit || pc_has_permission(sd, PC_PERM_ALL_SKILL))
  1871. return c;
  1872. skill_point = pc_calc_skillpoint(sd);
  1873. novice_skills = job_info[pc_class2idx(JOB_NOVICE)].max_level[1] - 1;
  1874. // limit 1st class and above to novice job levels
  1875. if(skill_point < novice_skills && (sd->class_&MAPID_BASEMASK) != MAPID_SUMMONER)
  1876. {
  1877. c = MAPID_NOVICE;
  1878. }
  1879. // limit 2nd class and above to first class job levels (super novices are exempt)
  1880. else if (sd->class_&JOBL_2 && (sd->class_&MAPID_UPPERMASK) != MAPID_SUPER_NOVICE)
  1881. {
  1882. // regenerate change_level_2nd
  1883. if (!sd->change_level_2nd)
  1884. {
  1885. if (sd->class_&JOBL_THIRD)
  1886. {
  1887. // if neither 2nd nor 3rd jobchange levels are known, we have to assume a default for 2nd
  1888. if (!sd->change_level_3rd)
  1889. sd->change_level_2nd = job_info[pc_class2idx(pc_mapid2jobid(sd->class_&MAPID_UPPERMASK, sd->status.sex))].max_level[1];
  1890. else
  1891. sd->change_level_2nd = 1 + skill_point + sd->status.skill_point
  1892. - (sd->status.job_level - 1)
  1893. - (sd->change_level_3rd - 1)
  1894. - novice_skills;
  1895. }
  1896. else
  1897. {
  1898. sd->change_level_2nd = 1 + skill_point + sd->status.skill_point
  1899. - (sd->status.job_level - 1)
  1900. - novice_skills;
  1901. }
  1902. pc_setglobalreg(sd, add_str(JOBCHANGE2ND_VAR), sd->change_level_2nd);
  1903. }
  1904. if (skill_point < novice_skills + (sd->change_level_2nd - 1))
  1905. {
  1906. c &= MAPID_BASEMASK;
  1907. }
  1908. // limit 3rd class to 2nd class/trans job levels
  1909. else if(sd->class_&JOBL_THIRD)
  1910. {
  1911. // regenerate change_level_3rd
  1912. if (!sd->change_level_3rd)
  1913. {
  1914. sd->change_level_3rd = 1 + skill_point + sd->status.skill_point
  1915. - (sd->status.job_level - 1)
  1916. - (sd->change_level_2nd - 1)
  1917. - novice_skills;
  1918. pc_setglobalreg(sd, add_str(JOBCHANGE3RD_VAR), sd->change_level_3rd);
  1919. }
  1920. if (skill_point < novice_skills + (sd->change_level_2nd - 1) + (sd->change_level_3rd - 1))
  1921. c &= MAPID_UPPERMASK;
  1922. }
  1923. }
  1924. // restore non-limiting flags
  1925. c |= sd->class_&(JOBL_UPPER|JOBL_BABY);
  1926. return c;
  1927. }
  1928. /*==========================================
  1929. * Updates the weight status
  1930. *------------------------------------------
  1931. * 1: overweight 50% for pre-renewal and 70% for renewal
  1932. * 2: overweight 90%
  1933. * It's assumed that SC_WEIGHT50 and SC_WEIGHT90 are only started/stopped here.
  1934. */
  1935. void pc_updateweightstatus(struct map_session_data *sd)
  1936. {
  1937. int old_overweight;
  1938. int new_overweight;
  1939. nullpo_retv(sd);
  1940. old_overweight = (sd->sc.data[SC_WEIGHT90]) ? 2 : (sd->sc.data[SC_WEIGHT50]) ? 1 : 0;
  1941. #ifdef RENEWAL
  1942. new_overweight = (pc_is90overweight(sd)) ? 2 : (pc_is70overweight(sd)) ? 1 : 0;
  1943. #else
  1944. new_overweight = (pc_is90overweight(sd)) ? 2 : (pc_is50overweight(sd)) ? 1 : 0;
  1945. #endif
  1946. if( old_overweight == new_overweight )
  1947. return; // no change
  1948. // stop old status change
  1949. if( old_overweight == 1 )
  1950. status_change_end(&sd->bl, SC_WEIGHT50, INVALID_TIMER);
  1951. else if( old_overweight == 2 )
  1952. status_change_end(&sd->bl, SC_WEIGHT90, INVALID_TIMER);
  1953. // start new status change
  1954. if( new_overweight == 1 )
  1955. sc_start(&sd->bl,&sd->bl, SC_WEIGHT50, 100, 0, 0);
  1956. else if( new_overweight == 2 )
  1957. sc_start(&sd->bl,&sd->bl, SC_WEIGHT90, 100, 0, 0);
  1958. // update overweight status
  1959. sd->regen.state.overweight = new_overweight;
  1960. }
  1961. int pc_disguise(struct map_session_data *sd, int class_)
  1962. {
  1963. if (!class_ && !sd->disguise)
  1964. return 0;
  1965. if (class_ && sd->disguise == class_)
  1966. return 0;
  1967. if(pc_isinvisible(sd))
  1968. { //Character is invisible. Stealth class-change. [Skotlex]
  1969. sd->disguise = class_; //viewdata is set on uncloaking.
  1970. return 2;
  1971. }
  1972. if (sd->bl.prev != NULL) {
  1973. pc_stop_walking(sd, 0);
  1974. clif_clearunit_area(&sd->bl, CLR_OUTSIGHT);
  1975. }
  1976. if (!class_) {
  1977. sd->disguise = 0;
  1978. class_ = sd->status.class_;
  1979. } else
  1980. sd->disguise=class_;
  1981. status_set_viewdata(&sd->bl, class_);
  1982. clif_changeoption(&sd->bl);
  1983. if (sd->bl.prev != NULL) {
  1984. clif_spawn(&sd->bl);
  1985. if (class_ == sd->status.class_ && pc_iscarton(sd))
  1986. { //It seems the cart info is lost on undisguise.
  1987. clif_cartlist(sd);
  1988. clif_updatestatus(sd,SP_CARTINFO);
  1989. }
  1990. if (sd->chatID) {
  1991. struct chat_data* cd;
  1992. if ((cd = (struct chat_data*)map_id2bl(sd->chatID)) != NULL)
  1993. clif_dispchat(cd,0);
  1994. }
  1995. }
  1996. return 1;
  1997. }
  1998. /// Show error message
  1999. #define PC_BONUS_SHOW_ERROR(type,type2,val) { ShowError("%s: %s: Invalid %s %d.\n",__FUNCTION__,#type,#type2,(val)); break; }
  2000. /// Check for valid Element, break & show error message if invalid Element
  2001. #define PC_BONUS_CHK_ELEMENT(ele,bonus) { if (!CHK_ELEMENT((ele))) { PC_BONUS_SHOW_ERROR((bonus),Element,(ele)); }}
  2002. /// Check for valid Race, break & show error message if invalid Race
  2003. #define PC_BONUS_CHK_RACE(rc,bonus) { if (!CHK_RACE((rc))) { PC_BONUS_SHOW_ERROR((bonus),Race,(rc)); }}
  2004. /// Check for valid Race2, break & show error message if invalid Race2
  2005. #define PC_BONUS_CHK_RACE2(rc2,bonus) { if (!CHK_RACE2((rc2))) { PC_BONUS_SHOW_ERROR((bonus),Race2,(rc2)); }}
  2006. /// Check for valid Class, break & show error message if invalid Class
  2007. #define PC_BONUS_CHK_CLASS(cl,bonus) { if (!CHK_CLASS((cl))) { PC_BONUS_SHOW_ERROR((bonus),Class,(cl)); }}
  2008. /// Check for valid Size, break & show error message if invalid Size
  2009. #define PC_BONUS_CHK_SIZE(sz,bonus) { if (!CHK_MOBSIZE((sz))) { PC_BONUS_SHOW_ERROR((bonus),Size,(sz)); }}
  2010. /// Check for valid SC, break & show error message if invalid SC
  2011. #define PC_BONUS_CHK_SC(sc,bonus) { if ((sc) <= SC_NONE || (sc) >= SC_MAX) { PC_BONUS_SHOW_ERROR((bonus),Effect,(sc)); }}
  2012. /**
  2013. * Add auto spell bonus for player while attacking/attacked
  2014. * @param spell: Spell array
  2015. * @param id: Skill to cast
  2016. * @param lv: Skill level
  2017. * @param rate: Success chance
  2018. * @param flag: Battle flag
  2019. * @param card_id: Used to prevent card stacking
  2020. */
  2021. static void pc_bonus_autospell(std::vector<s_autospell> &spell, short id, short lv, short rate, short flag, unsigned short card_id)
  2022. {
  2023. if (spell.size() == MAX_PC_BONUS) {
  2024. ShowWarning("pc_bonus_autospell: Reached max (%d) number of autospells per character!\n", MAX_PC_BONUS);
  2025. return;
  2026. }
  2027. if (!rate)
  2028. return;
  2029. if (!(flag&BF_RANGEMASK))
  2030. flag |= BF_SHORT | BF_LONG; //No range defined? Use both.
  2031. if (!(flag&BF_WEAPONMASK))
  2032. flag |= BF_WEAPON; //No attack type defined? Use weapon.
  2033. if (!(flag&BF_SKILLMASK)) {
  2034. if (flag&(BF_MAGIC | BF_MISC))
  2035. flag |= BF_SKILL; //These two would never trigger without BF_SKILL
  2036. if (flag&BF_WEAPON)
  2037. flag |= BF_NORMAL; //By default autospells should only trigger on normal weapon attacks.
  2038. }
  2039. for (auto &it : spell) {
  2040. if ((it.card_id == card_id || it.rate < 0 || rate < 0) && it.id == id && it.lv == lv && it.flag == flag) {
  2041. if (!battle_config.autospell_stacking && it.rate > 0 && rate > 0) // Stacking disabled
  2042. return;
  2043. it.rate = cap_value(it.rate + rate, -10000, 10000);
  2044. return;
  2045. }
  2046. }
  2047. struct s_autospell entry = {};
  2048. if (rate < -10000 || rate > 10000)
  2049. ShowWarning("pc_bonus_autospell: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2050. entry.id = id;
  2051. entry.lv = lv;
  2052. entry.rate = cap_value(rate, -10000, 10000);
  2053. entry.flag = flag;
  2054. entry.card_id = card_id;
  2055. spell.push_back(entry);
  2056. }
  2057. /**
  2058. * Add auto spell bonus for player while using skills
  2059. * @param spell: Spell array
  2060. * @param src_skill: Trigger skill
  2061. * @param id: Support or target type
  2062. * @param lv: Skill level
  2063. * @param rate: Success chance
  2064. * @param card_id: Used to prevent card stacking
  2065. */
  2066. static void pc_bonus_autospell_onskill(std::vector<s_autospell> &spell, short src_skill, short id, short lv, short rate, unsigned short card_id)
  2067. {
  2068. if (spell.size() == MAX_PC_BONUS) {
  2069. ShowWarning("pc_bonus_autospell_onskill: Reached max (%d) number of autospells per character!\n", MAX_PC_BONUS);
  2070. return;
  2071. }
  2072. if (!rate)
  2073. return;
  2074. struct s_autospell entry = {};
  2075. if (rate < -10000 || rate > 10000)
  2076. ShowWarning("pc_bonus_onskill: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2077. entry.flag = src_skill;
  2078. entry.id = id;
  2079. entry.lv = lv;
  2080. entry.rate = cap_value(rate, -10000, 10000);
  2081. entry.card_id = card_id;
  2082. spell.push_back(entry);
  2083. }
  2084. /**
  2085. * Add inflict effect bonus for player while attacking/attacked
  2086. * @param effect: Effect array
  2087. * @param sc: SC/Effect type
  2088. * @param rate: Success chance
  2089. * @param arrow_rate: success chance if bonus comes from arrow-type item
  2090. * @param flag: Target flag
  2091. * @param duration: Duration. If 0 use default duration lookup for associated skill with level 7
  2092. */
  2093. 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)
  2094. {
  2095. if (effect.size() == MAX_PC_BONUS) {
  2096. ShowWarning("pc_bonus_addeff: Reached max (%d) number of add effects per character!\n", MAX_PC_BONUS);
  2097. return;
  2098. }
  2099. if (!(flag&(ATF_SHORT | ATF_LONG)))
  2100. flag |= ATF_SHORT | ATF_LONG; //Default range: both
  2101. if (!(flag&(ATF_TARGET | ATF_SELF)))
  2102. flag |= ATF_TARGET; //Default target: enemy.
  2103. if (!(flag&(ATF_WEAPON | ATF_MAGIC | ATF_MISC)))
  2104. flag |= ATF_WEAPON; //Default type: weapon.
  2105. if (!duration)
  2106. duration = (unsigned int)skill_get_time2(status_sc2skill(sc), 7);
  2107. for (auto &it : effect) {
  2108. if (it.sc == sc && it.flag == flag) {
  2109. it.rate = cap_value(it.rate + rate, -10000, 10000);
  2110. it.arrow_rate += arrow_rate;
  2111. it.duration = umax(it.duration, duration);
  2112. return;
  2113. }
  2114. }
  2115. struct s_addeffect entry = {};
  2116. if (rate < -10000 || rate > 10000)
  2117. ShowWarning("pc_bonus_addeff: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2118. entry.sc = sc;
  2119. entry.rate = cap_value(rate, -10000, 10000);
  2120. entry.arrow_rate = arrow_rate;
  2121. entry.flag = flag;
  2122. entry.duration = duration;
  2123. effect.push_back(entry);
  2124. }
  2125. /**
  2126. * Add inflict effect bonus for player while attacking using skill
  2127. * @param effect: Effect array
  2128. * @param sc: SC/Effect type
  2129. * @param rate: Success chance
  2130. * @param skill_id: Skill to cast
  2131. * @param target: Target type
  2132. * @param duration: Duration. If 0 use default duration lookup for associated skill with level 7
  2133. */
  2134. 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)
  2135. {
  2136. if (effect.size() == MAX_PC_BONUS) {
  2137. ShowWarning("pc_bonus_addeff_onskill: Reached max (%d) number of add effects per character!\n", MAX_PC_BONUS);
  2138. return;
  2139. }
  2140. if (!duration)
  2141. duration = (unsigned int)skill_get_time2(status_sc2skill(sc), 7);
  2142. for (auto &it : effect) {
  2143. if (it.sc == sc && it.skill_id == skill_id && it.target == target) {
  2144. it.rate = cap_value(it.rate + rate, -10000, 10000);
  2145. it.duration = umax(it.duration, duration);
  2146. return;
  2147. }
  2148. }
  2149. struct s_addeffectonskill entry = {};
  2150. if (rate < -10000 || rate > 10000)
  2151. ShowWarning("pc_bonus_addeff_onskill: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2152. entry.sc = sc;
  2153. entry.rate = cap_value(rate, -10000, 10000);
  2154. entry.skill_id = skill_id;
  2155. entry.target = target;
  2156. entry.duration = duration;
  2157. effect.push_back(entry);
  2158. }
  2159. /**
  2160. * Adjust/add drop rate modifier for player
  2161. * @param drop: Player's sd->add_drop (struct s_add_drop)
  2162. * @param nameid: item id that will be dropped
  2163. * @param group: group id
  2164. * @param class_: target class
  2165. * @param race: target race. if < 0, means monster_id
  2166. * @param rate: rate value: 1 ~ 10000. If < 0, it will be multiplied with mob level/10
  2167. */
  2168. static void pc_bonus_item_drop(std::vector<s_add_drop> &drop, unsigned short nameid, uint16 group, int class_, short race, int rate)
  2169. {
  2170. if (!nameid && !group) {
  2171. ShowWarning("pc_bonus_item_drop: No Item ID nor Item Group ID specified.\n");
  2172. return;
  2173. }
  2174. if (nameid && !itemdb_exists(nameid)) {
  2175. ShowWarning("pc_bonus_item_drop: Invalid item id %hu\n",nameid);
  2176. return;
  2177. }
  2178. if (group && !itemdb_group_exists(group)) {
  2179. ShowWarning("pc_bonus_item_drop: Invalid Item Group %hu\n",group);
  2180. return;
  2181. }
  2182. if (drop.size() == MAX_PC_BONUS) {
  2183. 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);
  2184. return;
  2185. }
  2186. //Apply config rate adjustment settings.
  2187. if (rate >= 0) { //Absolute drop.
  2188. if (battle_config.item_rate_adddrop != 100)
  2189. rate = rate*battle_config.item_rate_adddrop/100;
  2190. if (rate < battle_config.item_drop_adddrop_min)
  2191. rate = battle_config.item_drop_adddrop_min;
  2192. else if (rate > battle_config.item_drop_adddrop_max)
  2193. rate = battle_config.item_drop_adddrop_max;
  2194. } else { //Relative drop, max/min limits are applied at drop time.
  2195. if (battle_config.item_rate_adddrop != 100)
  2196. rate = rate*battle_config.item_rate_adddrop/100;
  2197. if (rate > -1)
  2198. rate = -1;
  2199. }
  2200. for (auto &it : drop) {
  2201. if (it.nameid == nameid && it.group == group && it.race == race && it.class_ == class_) {
  2202. if ((rate < 0 && it.rate < 0) || (rate > 0 && it.rate > 0)) //Adjust the rate if it has same classification
  2203. it.rate = cap_value(it.rate + rate, -10000, 10000);
  2204. return;
  2205. }
  2206. }
  2207. struct s_add_drop entry = {};
  2208. if (rate < -10000 || rate > 10000)
  2209. ShowWarning("pc_bonus_item_drop: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2210. entry.nameid = nameid;
  2211. entry.group = group;
  2212. entry.race = race;
  2213. entry.class_ = class_;
  2214. entry.rate = cap_value(rate, -10000, 10000);
  2215. drop.push_back(entry);
  2216. }
  2217. /**
  2218. * Add autobonus to player when attacking/attacked
  2219. * @param bonus: Bonus array
  2220. * @param script: Script to execute
  2221. * @param rate: Success chance
  2222. * @param dur: Duration
  2223. * @param flag: Battle flag/skill
  2224. * @param other_script: Secondary script to execute
  2225. * @param pos: Item equip position
  2226. * @param onskill: Skill used to trigger autobonus
  2227. * @return True on success or false otherwise
  2228. */
  2229. 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)
  2230. {
  2231. if (bonus.size() == MAX_PC_BONUS) {
  2232. ShowWarning("pc_addautobonus: Reached max (%d) number of autobonus per character!\n", MAX_PC_BONUS);
  2233. return false;
  2234. }
  2235. if (!onskill) {
  2236. if (!(flag&BF_RANGEMASK))
  2237. flag |= BF_SHORT | BF_LONG; //No range defined? Use both.
  2238. if (!(flag&BF_WEAPONMASK))
  2239. flag |= BF_WEAPON; //No attack type defined? Use weapon.
  2240. if (!(flag&BF_SKILLMASK)) {
  2241. if (flag&(BF_MAGIC | BF_MISC))
  2242. flag |= BF_SKILL; //These two would never trigger without BF_SKILL
  2243. if (flag&BF_WEAPON)
  2244. flag |= BF_NORMAL | BF_SKILL;
  2245. }
  2246. }
  2247. struct s_autobonus entry = {};
  2248. if (rate < -10000 || rate > 10000)
  2249. ShowWarning("pc_addautobonus: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2250. entry.rate = cap_value(rate, -10000, 10000);
  2251. entry.duration = dur;
  2252. entry.active = INVALID_TIMER;
  2253. entry.atk_type = flag;
  2254. entry.pos = pos;
  2255. entry.bonus_script = aStrdup(script);
  2256. entry.other_script = (other_script ? aStrdup(other_script) : NULL);
  2257. bonus.push_back(entry);
  2258. return true;
  2259. }
  2260. /**
  2261. * Remove an autobonus from player
  2262. * @param sd: Player data
  2263. * @param bonus: Autobonus array
  2264. * @param restore: Run script on clearing or not
  2265. */
  2266. void pc_delautobonus(struct map_session_data* sd, std::vector<s_autobonus> &bonus, bool restore)
  2267. {
  2268. nullpo_retv(sd);
  2269. std::vector<s_autobonus>::iterator it = bonus.begin();
  2270. while( it != bonus.end() ){
  2271. s_autobonus b = *it;
  2272. if (b.active != INVALID_TIMER) {
  2273. if (restore && (sd->state.autobonus&b.pos) == b.pos) {
  2274. if (b.bonus_script) {
  2275. unsigned int equip_pos_idx = 0;
  2276. // Create a list of all equipped positions to see if all items needed for the autobonus are still present [Playtester]
  2277. for (uint8 j = 0; j < EQI_MAX; j++) {
  2278. if (sd->equip_index[j] >= 0)
  2279. equip_pos_idx |= sd->inventory.u.items_inventory[sd->equip_index[j]].equip;
  2280. }
  2281. if ((equip_pos_idx&b.pos) == b.pos)
  2282. script_run_autobonus(b.bonus_script, sd, b.pos);
  2283. }
  2284. it++;
  2285. continue;
  2286. } else { // Logout / Unequipped an item with an activated bonus
  2287. delete_timer(b.active, pc_endautobonus);
  2288. b.active = INVALID_TIMER;
  2289. }
  2290. }
  2291. if (b.bonus_script)
  2292. aFree(b.bonus_script);
  2293. if (b.other_script)
  2294. aFree(b.other_script);
  2295. it = bonus.erase(it);
  2296. }
  2297. }
  2298. /**
  2299. * Execute autobonus on player
  2300. * @param sd: Player data
  2301. * @param autobonus: Autobonus to run
  2302. */
  2303. void pc_exeautobonus(struct map_session_data *sd, std::vector<s_autobonus> *bonus, struct s_autobonus *autobonus)
  2304. {
  2305. nullpo_retv(sd);
  2306. nullpo_retv(autobonus);
  2307. if (autobonus->active != INVALID_TIMER)
  2308. delete_timer(autobonus->active, pc_endautobonus);
  2309. if( autobonus->other_script )
  2310. {
  2311. int j;
  2312. unsigned int equip_pos_idx = 0;
  2313. //Create a list of all equipped positions to see if all items needed for the autobonus are still present [Playtester]
  2314. for(j = 0; j < EQI_MAX; j++) {
  2315. if(sd->equip_index[j] >= 0)
  2316. equip_pos_idx |= sd->inventory.u.items_inventory[sd->equip_index[j]].equip;
  2317. }
  2318. if((equip_pos_idx&autobonus->pos) == autobonus->pos)
  2319. script_run_autobonus(autobonus->other_script,sd,autobonus->pos);
  2320. }
  2321. autobonus->active = add_timer(gettick()+autobonus->duration, pc_endautobonus, sd->bl.id, (intptr_t)bonus);
  2322. sd->state.autobonus |= autobonus->pos;
  2323. status_calc_pc(sd,SCO_NONE);
  2324. }
  2325. /**
  2326. * Remove autobonus timer from player
  2327. */
  2328. TIMER_FUNC(pc_endautobonus){
  2329. struct map_session_data *sd = map_id2sd(id);
  2330. std::vector<s_autobonus> *bonus = (std::vector<s_autobonus> *)data;
  2331. nullpo_ret(sd);
  2332. nullpo_ret(bonus);
  2333. for( struct s_autobonus& autobonus : *bonus ){
  2334. if( autobonus.active == tid ){
  2335. autobonus.active = INVALID_TIMER;
  2336. sd->state.autobonus &= ~autobonus.pos;
  2337. break;
  2338. }
  2339. }
  2340. status_calc_pc(sd,SCO_NONE);
  2341. return 0;
  2342. }
  2343. /**
  2344. * Add element bonus to player when attacking
  2345. * @param sd: Player data
  2346. * @param ele: Element to adjust
  2347. * @param rate: Success chance
  2348. * @param flag: Battle flag
  2349. */
  2350. static void pc_bonus_addele(struct map_session_data* sd, unsigned char ele, short rate, short flag)
  2351. {
  2352. nullpo_retv(sd);
  2353. struct weapon_data *wd = (sd->state.lr_flag ? &sd->left_weapon : &sd->right_weapon);
  2354. if (wd->addele2.size() == MAX_PC_BONUS) {
  2355. ShowWarning("pc_bonus_addele: Reached max (%d) number of add element damage bonuses per character!\n", MAX_PC_BONUS);
  2356. return;
  2357. }
  2358. if (!(flag&BF_RANGEMASK))
  2359. flag |= BF_SHORT | BF_LONG;
  2360. if (!(flag&BF_WEAPONMASK))
  2361. flag |= BF_WEAPON;
  2362. if (!(flag&BF_SKILLMASK)) {
  2363. if (flag&(BF_MAGIC | BF_MISC))
  2364. flag |= BF_SKILL;
  2365. if (flag&BF_WEAPON)
  2366. flag |= BF_NORMAL | BF_SKILL;
  2367. }
  2368. for (auto &it : wd->addele2) {
  2369. if (it.ele == ele && it.flag == flag) {
  2370. it.rate = cap_value(it.rate + rate, -10000, 10000);
  2371. return;
  2372. }
  2373. }
  2374. struct s_addele2 entry = {};
  2375. if (rate < -10000 || rate > 10000)
  2376. ShowWarning("pc_bonus_addele: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2377. entry.ele = ele;
  2378. entry.rate = cap_value(rate, -10000, 10000);
  2379. entry.flag = flag;
  2380. wd->addele2.push_back(entry);
  2381. }
  2382. /**
  2383. * Reduce element bonus to player when attacking
  2384. * @param sd: Player data
  2385. * @param ele: Element to adjust
  2386. * @param rate: Success chance
  2387. * @param flag: Battle flag
  2388. */
  2389. static void pc_bonus_subele(struct map_session_data* sd, unsigned char ele, short rate, short flag)
  2390. {
  2391. nullpo_retv(sd);
  2392. if (sd->subele2.size() == MAX_PC_BONUS) {
  2393. ShowWarning("pc_bonus_subele: Reached max (%d) number of resist element damage bonuses per character!\n", MAX_PC_BONUS);
  2394. return;
  2395. }
  2396. if (!(flag&BF_RANGEMASK))
  2397. flag |= BF_SHORT | BF_LONG;
  2398. if (!(flag&BF_WEAPONMASK))
  2399. flag |= BF_WEAPON;
  2400. if (!(flag&BF_SKILLMASK)) {
  2401. if (flag&(BF_MAGIC | BF_MISC))
  2402. flag |= BF_SKILL;
  2403. if (flag&BF_WEAPON)
  2404. flag |= BF_NORMAL | BF_SKILL;
  2405. }
  2406. for (auto &it : sd->subele2) {
  2407. if (it.ele == ele && it.flag == flag) {
  2408. it.rate = cap_value(it.rate + rate, -10000, 10000);
  2409. return;
  2410. }
  2411. }
  2412. struct s_addele2 entry = {};
  2413. if (rate < -10000 || rate > 10000)
  2414. ShowWarning("pc_bonus_subele: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2415. entry.ele = ele;
  2416. entry.rate = cap_value(rate, -10000, 10000);
  2417. entry.flag = flag;
  2418. sd->subele2.push_back(entry);
  2419. }
  2420. /**
  2421. * General item bonus for player
  2422. * @param bonus: Bonus array
  2423. * @param id: Key
  2424. * @param val: Value
  2425. * @param cap_rate: If Value is a rate value that needs to be capped
  2426. */
  2427. static void pc_bonus_itembonus(std::vector<s_item_bonus> &bonus, uint16 id, int val, bool cap_rate)
  2428. {
  2429. for (auto &it : bonus) {
  2430. if (it.id == id) {
  2431. if (cap_rate)
  2432. it.val = cap_value(it.val + val, -10000, 10000);
  2433. else
  2434. it.val += val;
  2435. return;
  2436. }
  2437. }
  2438. struct s_item_bonus entry = {};
  2439. if (cap_rate && (val < -10000 || val > 10000)) {
  2440. ShowWarning("pc_bonus_itembonus: Item bonus val %d exceeds -10000~10000 range, capping.\n", val);
  2441. val = cap_value(val, -10000, 10000);
  2442. }
  2443. entry.id = id;
  2444. entry.val = val;
  2445. bonus.push_back(entry);
  2446. }
  2447. /**
  2448. * Remove HP/SP to player when attacking
  2449. * @param bonus: Bonus array
  2450. * @param rate: Success chance
  2451. * @param per: Percentage of HP/SP to vanish
  2452. * @param flag: Battle flag
  2453. */
  2454. static void pc_bonus_addvanish(std::vector<s_vanish_bonus> &bonus, int16 rate, int16 per, int flag) {
  2455. if (bonus.size() == MAX_PC_BONUS) {
  2456. ShowWarning("pc_bonus_addvanish: Reached max (%d) number of vanish damage bonuses per character!\n", MAX_PC_BONUS);
  2457. return;
  2458. }
  2459. if (!(flag&BF_RANGEMASK))
  2460. flag |= BF_SHORT | BF_LONG;
  2461. if (!(flag&BF_WEAPONMASK))
  2462. flag |= BF_WEAPON;
  2463. if (!(flag&BF_SKILLMASK)) {
  2464. if (flag&(BF_MAGIC | BF_MISC))
  2465. flag |= BF_SKILL;
  2466. if (flag&BF_WEAPON)
  2467. flag |= BF_NORMAL | BF_SKILL;
  2468. }
  2469. for (auto &it : bonus) {
  2470. if (it.flag == flag) {
  2471. it.rate = cap_value(it.rate + rate, -10000, 10000);
  2472. it.per += per;
  2473. return;
  2474. }
  2475. }
  2476. struct s_vanish_bonus entry = {};
  2477. if (rate < -10000 || rate > 10000)
  2478. ShowWarning("pc_bonus_addvanish: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2479. entry.rate = cap_value(rate, -10000, 10000);
  2480. entry.per = per;
  2481. entry.flag = flag;
  2482. bonus.push_back(entry);
  2483. }
  2484. /*==========================================
  2485. * Add a bonus(type) to player sd
  2486. * format: bonus bBonusName,val;
  2487. * @param sd
  2488. * @param type Bonus type used by bBonusName
  2489. * @param val Value that usually for rate or fixed value
  2490. *------------------------------------------*/
  2491. void pc_bonus(struct map_session_data *sd,int type,int val)
  2492. {
  2493. struct status_data *status;
  2494. int bonus;
  2495. nullpo_retv(sd);
  2496. status = &sd->base_status;
  2497. switch(type){
  2498. case SP_STR:
  2499. case SP_AGI:
  2500. case SP_VIT:
  2501. case SP_INT:
  2502. case SP_DEX:
  2503. case SP_LUK:
  2504. if(sd->state.lr_flag != 2)
  2505. sd->param_bonus[type-SP_STR]+=val;
  2506. break;
  2507. case SP_ATK1:
  2508. if(!sd->state.lr_flag) {
  2509. bonus = status->rhw.atk + val;
  2510. status->rhw.atk = cap_value(bonus, 0, USHRT_MAX);
  2511. }
  2512. else if(sd->state.lr_flag == 1) {
  2513. bonus = status->lhw.atk + val;
  2514. status->lhw.atk = cap_value(bonus, 0, USHRT_MAX);
  2515. }
  2516. break;
  2517. case SP_ATK2:
  2518. if(!sd->state.lr_flag) {
  2519. bonus = status->rhw.atk2 + val;
  2520. status->rhw.atk2 = cap_value(bonus, 0, USHRT_MAX);
  2521. }
  2522. else if(sd->state.lr_flag == 1) {
  2523. bonus = status->lhw.atk2 + val;
  2524. status->lhw.atk2 = cap_value(bonus, 0, USHRT_MAX);
  2525. }
  2526. break;
  2527. case SP_BASE_ATK:
  2528. if(sd->state.lr_flag != 2) {
  2529. #ifdef RENEWAL
  2530. bonus = sd->bonus.eatk + val;
  2531. sd->bonus.eatk = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2532. #else
  2533. bonus = status->batk + val;
  2534. status->batk = cap_value(bonus, 0, USHRT_MAX);
  2535. #endif
  2536. }
  2537. break;
  2538. case SP_DEF1:
  2539. if(sd->state.lr_flag != 2) {
  2540. bonus = status->def + val;
  2541. #ifdef RENEWAL
  2542. status->def = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2543. #else
  2544. status->def = cap_value(bonus, CHAR_MIN, CHAR_MAX);
  2545. #endif
  2546. }
  2547. break;
  2548. case SP_DEF2:
  2549. if(sd->state.lr_flag != 2) {
  2550. bonus = status->def2 + val;
  2551. status->def2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2552. }
  2553. break;
  2554. case SP_MDEF1:
  2555. if(sd->state.lr_flag != 2) {
  2556. bonus = status->mdef + val;
  2557. #ifdef RENEWAL
  2558. status->mdef = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2559. #else
  2560. status->mdef = cap_value(bonus, CHAR_MIN, CHAR_MAX);
  2561. #endif
  2562. if( sd->state.lr_flag == 3 ) {//Shield, used for royal guard
  2563. sd->bonus.shieldmdef += bonus;
  2564. }
  2565. }
  2566. break;
  2567. case SP_MDEF2:
  2568. if(sd->state.lr_flag != 2) {
  2569. bonus = status->mdef2 + val;
  2570. status->mdef2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2571. }
  2572. break;
  2573. case SP_HIT:
  2574. if(sd->state.lr_flag != 2) {
  2575. bonus = status->hit + val;
  2576. status->hit = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2577. } else
  2578. sd->bonus.arrow_hit+=val;
  2579. break;
  2580. case SP_FLEE1:
  2581. if(sd->state.lr_flag != 2) {
  2582. bonus = status->flee + val;
  2583. status->flee = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2584. }
  2585. break;
  2586. case SP_FLEE2:
  2587. if(sd->state.lr_flag != 2) {
  2588. bonus = status->flee2 + val*10;
  2589. status->flee2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2590. }
  2591. break;
  2592. case SP_CRITICAL:
  2593. if(sd->state.lr_flag != 2) {
  2594. bonus = status->cri + val*10;
  2595. status->cri = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  2596. } else
  2597. sd->bonus.arrow_cri += val*10;
  2598. break;
  2599. case SP_ATKELE:
  2600. PC_BONUS_CHK_ELEMENT(val,SP_ATKELE);
  2601. switch (sd->state.lr_flag)
  2602. {
  2603. case 2:
  2604. switch (sd->status.weapon) {
  2605. case W_BOW:
  2606. case W_REVOLVER:
  2607. case W_RIFLE:
  2608. case W_GATLING:
  2609. case W_SHOTGUN:
  2610. case W_GRENADE:
  2611. //Become weapon element.
  2612. status->rhw.ele=val;
  2613. break;
  2614. default: //Become arrow element.
  2615. sd->bonus.arrow_ele=val;
  2616. break;
  2617. }
  2618. break;
  2619. case 1:
  2620. status->lhw.ele=val;
  2621. break;
  2622. default:
  2623. status->rhw.ele=val;
  2624. break;
  2625. }
  2626. break;
  2627. case SP_DEFELE:
  2628. PC_BONUS_CHK_ELEMENT(val,SP_DEFELE);
  2629. if(sd->state.lr_flag != 2)
  2630. status->def_ele=val;
  2631. break;
  2632. case SP_MAXHP:
  2633. if(sd->state.lr_flag == 2)
  2634. break;
  2635. sd->bonus.hp += val;
  2636. break;
  2637. case SP_MAXSP:
  2638. if(sd->state.lr_flag == 2)
  2639. break;
  2640. sd->bonus.sp += val;
  2641. break;
  2642. case SP_MAXHPRATE:
  2643. if(sd->state.lr_flag != 2)
  2644. sd->hprate+=val;
  2645. break;
  2646. case SP_MAXSPRATE:
  2647. if(sd->state.lr_flag != 2)
  2648. sd->sprate+=val;
  2649. break;
  2650. case SP_SPRATE:
  2651. if(sd->state.lr_flag != 2)
  2652. sd->dsprate+=val;
  2653. break;
  2654. case SP_ATTACKRANGE:
  2655. switch (sd->state.lr_flag) {
  2656. case 2:
  2657. switch (sd->status.weapon) {
  2658. case W_BOW:
  2659. case W_REVOLVER:
  2660. case W_RIFLE:
  2661. case W_GATLING:
  2662. case W_SHOTGUN:
  2663. case W_GRENADE:
  2664. status->rhw.range += val;
  2665. }
  2666. break;
  2667. case 1:
  2668. status->lhw.range += val;
  2669. break;
  2670. default:
  2671. status->rhw.range += val;
  2672. break;
  2673. }
  2674. break;
  2675. case SP_SPEED_RATE: //Non stackable increase
  2676. if(sd->state.lr_flag != 2)
  2677. sd->bonus.speed_rate = min(sd->bonus.speed_rate, -val);
  2678. break;
  2679. case SP_SPEED_ADDRATE: //Stackable increase
  2680. if(sd->state.lr_flag != 2)
  2681. sd->bonus.speed_add_rate -= val;
  2682. break;
  2683. case SP_ASPD: //Raw increase
  2684. if(sd->state.lr_flag != 2)
  2685. sd->bonus.aspd_add -= 10*val;
  2686. break;
  2687. case SP_ASPD_RATE: //Stackable increase - Made it linear as per rodatazone
  2688. if(sd->state.lr_flag != 2)
  2689. #ifndef RENEWAL_ASPD
  2690. status->aspd_rate -= 10*val;
  2691. #else
  2692. status->aspd_rate2 += val;
  2693. #endif
  2694. break;
  2695. case SP_HP_RECOV_RATE:
  2696. if(sd->state.lr_flag != 2)
  2697. sd->hprecov_rate += val;
  2698. break;
  2699. case SP_SP_RECOV_RATE:
  2700. if(sd->state.lr_flag != 2)
  2701. sd->sprecov_rate += val;
  2702. break;
  2703. case SP_CRITICAL_DEF:
  2704. if(sd->state.lr_flag != 2)
  2705. sd->bonus.critical_def += val;
  2706. break;
  2707. case SP_NEAR_ATK_DEF:
  2708. if(sd->state.lr_flag != 2)
  2709. sd->bonus.near_attack_def_rate += val;
  2710. break;
  2711. case SP_LONG_ATK_DEF:
  2712. if(sd->state.lr_flag != 2)
  2713. sd->bonus.long_attack_def_rate += val;
  2714. break;
  2715. case SP_DOUBLE_RATE:
  2716. if(sd->state.lr_flag == 0 && sd->bonus.double_rate < val)
  2717. sd->bonus.double_rate = val;
  2718. break;
  2719. case SP_DOUBLE_ADD_RATE:
  2720. if(sd->state.lr_flag == 0)
  2721. sd->bonus.double_add_rate += val;
  2722. break;
  2723. case SP_MATK_RATE:
  2724. if(sd->state.lr_flag != 2)
  2725. sd->matk_rate += val;
  2726. break;
  2727. case SP_IGNORE_DEF_ELE:
  2728. PC_BONUS_CHK_ELEMENT(val,SP_IGNORE_DEF_ELE);
  2729. if(!sd->state.lr_flag)
  2730. sd->right_weapon.ignore_def_ele |= 1<<val;
  2731. else if(sd->state.lr_flag == 1)
  2732. sd->left_weapon.ignore_def_ele |= 1<<val;
  2733. break;
  2734. case SP_IGNORE_DEF_RACE:
  2735. PC_BONUS_CHK_RACE(val,SP_IGNORE_DEF_RACE);
  2736. if(!sd->state.lr_flag)
  2737. sd->right_weapon.ignore_def_race |= 1<<val;
  2738. else if(sd->state.lr_flag == 1)
  2739. sd->left_weapon.ignore_def_race |= 1<<val;
  2740. break;
  2741. case SP_IGNORE_DEF_CLASS:
  2742. PC_BONUS_CHK_CLASS(val,SP_IGNORE_DEF_CLASS);
  2743. if(!sd->state.lr_flag)
  2744. sd->right_weapon.ignore_def_class |= 1<<val;
  2745. else if(sd->state.lr_flag == 1)
  2746. sd->left_weapon.ignore_def_class |= 1<<val;
  2747. break;
  2748. case SP_ATK_RATE:
  2749. if(sd->state.lr_flag != 2)
  2750. sd->bonus.atk_rate += val;
  2751. break;
  2752. case SP_MAGIC_ATK_DEF:
  2753. if(sd->state.lr_flag != 2)
  2754. sd->bonus.magic_def_rate += val;
  2755. break;
  2756. case SP_MISC_ATK_DEF:
  2757. if(sd->state.lr_flag != 2)
  2758. sd->bonus.misc_def_rate += val;
  2759. break;
  2760. case SP_IGNORE_MDEF_ELE:
  2761. PC_BONUS_CHK_ELEMENT(val,SP_IGNORE_MDEF_ELE);
  2762. if(sd->state.lr_flag != 2)
  2763. sd->bonus.ignore_mdef_ele |= 1<<val;
  2764. break;
  2765. case SP_IGNORE_MDEF_RACE:
  2766. PC_BONUS_CHK_RACE(val,SP_IGNORE_MDEF_RACE);
  2767. if(sd->state.lr_flag != 2)
  2768. sd->bonus.ignore_mdef_race |= 1<<val;
  2769. break;
  2770. case SP_PERFECT_HIT_RATE:
  2771. if(sd->state.lr_flag != 2 && sd->bonus.perfect_hit < val)
  2772. sd->bonus.perfect_hit = val;
  2773. break;
  2774. case SP_PERFECT_HIT_ADD_RATE:
  2775. if(sd->state.lr_flag != 2)
  2776. sd->bonus.perfect_hit_add += val;
  2777. break;
  2778. case SP_CRITICAL_RATE:
  2779. if(sd->state.lr_flag != 2)
  2780. sd->critical_rate+=val;
  2781. break;
  2782. case SP_DEF_RATIO_ATK_ELE:
  2783. PC_BONUS_CHK_ELEMENT(val,SP_DEF_RATIO_ATK_ELE);
  2784. if(!sd->state.lr_flag)
  2785. sd->right_weapon.def_ratio_atk_ele |= 1<<val;
  2786. else if(sd->state.lr_flag == 1)
  2787. sd->left_weapon.def_ratio_atk_ele |= 1<<val;
  2788. break;
  2789. case SP_DEF_RATIO_ATK_RACE:
  2790. PC_BONUS_CHK_RACE(val,SP_DEF_RATIO_ATK_RACE);
  2791. if(!sd->state.lr_flag)
  2792. sd->right_weapon.def_ratio_atk_race |= 1<<val;
  2793. else if(sd->state.lr_flag == 1)
  2794. sd->left_weapon.def_ratio_atk_race |= 1<<val;
  2795. break;
  2796. case SP_DEF_RATIO_ATK_CLASS:
  2797. PC_BONUS_CHK_CLASS(val,SP_DEF_RATIO_ATK_CLASS);
  2798. if(!sd->state.lr_flag)
  2799. sd->right_weapon.def_ratio_atk_class |= 1<<val;
  2800. else if(sd->state.lr_flag == 1)
  2801. sd->left_weapon.def_ratio_atk_class |= 1<<val;
  2802. break;
  2803. case SP_HIT_RATE:
  2804. if(sd->state.lr_flag != 2)
  2805. sd->hit_rate += val;
  2806. break;
  2807. case SP_FLEE_RATE:
  2808. if(sd->state.lr_flag != 2)
  2809. sd->flee_rate += val;
  2810. break;
  2811. case SP_FLEE2_RATE:
  2812. if(sd->state.lr_flag != 2)
  2813. sd->flee2_rate += val;
  2814. break;
  2815. case SP_DEF_RATE:
  2816. if(sd->state.lr_flag != 2)
  2817. sd->def_rate += val;
  2818. break;
  2819. case SP_DEF2_RATE:
  2820. if(sd->state.lr_flag != 2)
  2821. sd->def2_rate += val;
  2822. break;
  2823. case SP_MDEF_RATE:
  2824. if(sd->state.lr_flag != 2)
  2825. sd->mdef_rate += val;
  2826. break;
  2827. case SP_MDEF2_RATE:
  2828. if(sd->state.lr_flag != 2)
  2829. sd->mdef2_rate += val;
  2830. break;
  2831. case SP_RESTART_FULL_RECOVER:
  2832. if(sd->state.lr_flag != 2)
  2833. sd->special_state.restart_full_recover = 1;
  2834. break;
  2835. case SP_NO_CASTCANCEL:
  2836. if(sd->state.lr_flag != 2)
  2837. sd->special_state.no_castcancel = 1;
  2838. break;
  2839. case SP_NO_CASTCANCEL2:
  2840. if(sd->state.lr_flag != 2)
  2841. sd->special_state.no_castcancel2 = 1;
  2842. break;
  2843. case SP_NO_SIZEFIX:
  2844. if(sd->state.lr_flag != 2)
  2845. sd->special_state.no_sizefix = 1;
  2846. break;
  2847. case SP_NO_MAGIC_DAMAGE:
  2848. if(sd->state.lr_flag == 2)
  2849. break;
  2850. val+= sd->special_state.no_magic_damage;
  2851. sd->special_state.no_magic_damage = cap_value(val,0,100);
  2852. break;
  2853. case SP_NO_WEAPON_DAMAGE:
  2854. if(sd->state.lr_flag == 2)
  2855. break;
  2856. val+= sd->special_state.no_weapon_damage;
  2857. sd->special_state.no_weapon_damage = cap_value(val,0,100);
  2858. break;
  2859. case SP_NO_MISC_DAMAGE:
  2860. if(sd->state.lr_flag == 2)
  2861. break;
  2862. val+= sd->special_state.no_misc_damage;
  2863. sd->special_state.no_misc_damage = cap_value(val,0,100);
  2864. break;
  2865. case SP_NO_GEMSTONE:
  2866. if(sd->state.lr_flag != 2 && sd->special_state.no_gemstone != 2)
  2867. sd->special_state.no_gemstone = 1;
  2868. break;
  2869. case SP_INTRAVISION: // Maya Purple Card effect allowing to see Hiding/Cloaking people [DracoRPG]
  2870. if(sd->state.lr_flag != 2) {
  2871. sd->special_state.intravision = 1;
  2872. clif_status_load(&sd->bl, EFST_CLAIRVOYANCE, 1);
  2873. }
  2874. break;
  2875. case SP_NO_KNOCKBACK:
  2876. if(sd->state.lr_flag != 2)
  2877. sd->special_state.no_knockback = 1;
  2878. break;
  2879. case SP_SPLASH_RANGE:
  2880. if(sd->bonus.splash_range < val)
  2881. sd->bonus.splash_range = val;
  2882. break;
  2883. case SP_SPLASH_ADD_RANGE:
  2884. sd->bonus.splash_add_range += val;
  2885. break;
  2886. case SP_SHORT_WEAPON_DAMAGE_RETURN:
  2887. if(sd->state.lr_flag != 2)
  2888. sd->bonus.short_weapon_damage_return += val;
  2889. break;
  2890. case SP_LONG_WEAPON_DAMAGE_RETURN:
  2891. if(sd->state.lr_flag != 2)
  2892. sd->bonus.long_weapon_damage_return += val;
  2893. break;
  2894. case SP_MAGIC_DAMAGE_RETURN: //AppleGirl Was Here
  2895. if(sd->state.lr_flag != 2)
  2896. sd->bonus.magic_damage_return += val;
  2897. break;
  2898. case SP_ALL_STATS: // [Valaris]
  2899. if(sd->state.lr_flag!=2) {
  2900. sd->param_bonus[SP_STR-SP_STR]+=val;
  2901. sd->param_bonus[SP_AGI-SP_STR]+=val;
  2902. sd->param_bonus[SP_VIT-SP_STR]+=val;
  2903. sd->param_bonus[SP_INT-SP_STR]+=val;
  2904. sd->param_bonus[SP_DEX-SP_STR]+=val;
  2905. sd->param_bonus[SP_LUK-SP_STR]+=val;
  2906. }
  2907. break;
  2908. case SP_AGI_VIT: // [Valaris]
  2909. if(sd->state.lr_flag!=2) {
  2910. sd->param_bonus[SP_AGI-SP_STR]+=val;
  2911. sd->param_bonus[SP_VIT-SP_STR]+=val;
  2912. }
  2913. break;
  2914. case SP_AGI_DEX_STR: // [Valaris]
  2915. if(sd->state.lr_flag!=2) {
  2916. sd->param_bonus[SP_AGI-SP_STR]+=val;
  2917. sd->param_bonus[SP_DEX-SP_STR]+=val;
  2918. sd->param_bonus[SP_STR-SP_STR]+=val;
  2919. }
  2920. break;
  2921. case SP_PERFECT_HIDE: // [Valaris]
  2922. if(sd->state.lr_flag!=2)
  2923. sd->special_state.perfect_hiding=1;
  2924. break;
  2925. case SP_UNBREAKABLE:
  2926. if(sd->state.lr_flag!=2)
  2927. sd->bonus.unbreakable += val;
  2928. break;
  2929. case SP_UNBREAKABLE_WEAPON:
  2930. if(sd->state.lr_flag != 2)
  2931. sd->bonus.unbreakable_equip |= EQP_WEAPON;
  2932. break;
  2933. case SP_UNBREAKABLE_ARMOR:
  2934. if(sd->state.lr_flag != 2)
  2935. sd->bonus.unbreakable_equip |= EQP_ARMOR;
  2936. break;
  2937. case SP_UNBREAKABLE_HELM:
  2938. if(sd->state.lr_flag != 2)
  2939. sd->bonus.unbreakable_equip |= EQP_HELM;
  2940. break;
  2941. case SP_UNBREAKABLE_SHIELD:
  2942. if(sd->state.lr_flag != 2)
  2943. sd->bonus.unbreakable_equip |= EQP_SHIELD;
  2944. break;
  2945. case SP_UNBREAKABLE_GARMENT:
  2946. if(sd->state.lr_flag != 2)
  2947. sd->bonus.unbreakable_equip |= EQP_GARMENT;
  2948. break;
  2949. case SP_UNBREAKABLE_SHOES:
  2950. if(sd->state.lr_flag != 2)
  2951. sd->bonus.unbreakable_equip |= EQP_SHOES;
  2952. break;
  2953. case SP_CLASSCHANGE: // [Valaris]
  2954. if(sd->state.lr_flag !=2)
  2955. sd->bonus.classchange=val;
  2956. break;
  2957. case SP_LONG_ATK_RATE:
  2958. if(sd->state.lr_flag != 2) //[Lupus] it should stack, too. As any other cards rate bonuses
  2959. sd->bonus.long_attack_atk_rate+=val;
  2960. break;
  2961. case SP_BREAK_WEAPON_RATE:
  2962. if(sd->state.lr_flag != 2)
  2963. sd->bonus.break_weapon_rate+=val;
  2964. break;
  2965. case SP_BREAK_ARMOR_RATE:
  2966. if(sd->state.lr_flag != 2)
  2967. sd->bonus.break_armor_rate+=val;
  2968. break;
  2969. case SP_ADD_STEAL_RATE:
  2970. if(sd->state.lr_flag != 2)
  2971. sd->bonus.add_steal_rate+=val;
  2972. break;
  2973. case SP_DELAYRATE:
  2974. if(sd->state.lr_flag != 2)
  2975. sd->delayrate+=val;
  2976. break;
  2977. case SP_CRIT_ATK_RATE:
  2978. if(sd->state.lr_flag != 2)
  2979. sd->bonus.crit_atk_rate += val;
  2980. break;
  2981. case SP_NO_REGEN:
  2982. if(sd->state.lr_flag != 2)
  2983. sd->regen.state.block|=val;
  2984. break;
  2985. case SP_UNSTRIPABLE_WEAPON:
  2986. if(sd->state.lr_flag != 2)
  2987. sd->bonus.unstripable_equip |= EQP_WEAPON;
  2988. break;
  2989. case SP_UNSTRIPABLE:
  2990. case SP_UNSTRIPABLE_ARMOR:
  2991. if(sd->state.lr_flag != 2)
  2992. sd->bonus.unstripable_equip |= EQP_ARMOR;
  2993. break;
  2994. case SP_UNSTRIPABLE_HELM:
  2995. if(sd->state.lr_flag != 2)
  2996. sd->bonus.unstripable_equip |= EQP_HELM;
  2997. break;
  2998. case SP_UNSTRIPABLE_SHIELD:
  2999. if(sd->state.lr_flag != 2)
  3000. sd->bonus.unstripable_equip |= EQP_SHIELD;
  3001. break;
  3002. case SP_HP_DRAIN_VALUE: // bonus bHPDrainValue,n;
  3003. if(!sd->state.lr_flag) {
  3004. sd->right_weapon.hp_drain_class[CLASS_NORMAL] += val;
  3005. sd->right_weapon.hp_drain_class[CLASS_BOSS] += val;
  3006. } else if(sd->state.lr_flag == 1) {
  3007. sd->left_weapon.hp_drain_class[CLASS_NORMAL] += val;
  3008. sd->left_weapon.hp_drain_class[CLASS_BOSS] += val;
  3009. }
  3010. break;
  3011. case SP_SP_DRAIN_VALUE: // bonus bSPDrainValue,n;
  3012. if(!sd->state.lr_flag) {
  3013. sd->right_weapon.sp_drain_class[CLASS_NORMAL] += val;
  3014. sd->right_weapon.sp_drain_class[CLASS_BOSS] += val;
  3015. } else if(sd->state.lr_flag == 1) {
  3016. sd->left_weapon.sp_drain_class[CLASS_NORMAL] += val;
  3017. sd->left_weapon.sp_drain_class[CLASS_BOSS] += val;
  3018. }
  3019. break;
  3020. case SP_SP_GAIN_VALUE:
  3021. if(!sd->state.lr_flag)
  3022. sd->bonus.sp_gain_value += val;
  3023. break;
  3024. case SP_HP_GAIN_VALUE:
  3025. if(!sd->state.lr_flag)
  3026. sd->bonus.hp_gain_value += val;
  3027. break;
  3028. case SP_LONG_SP_GAIN_VALUE:
  3029. if(!sd->state.lr_flag)
  3030. sd->bonus.long_sp_gain_value += val;
  3031. case SP_LONG_HP_GAIN_VALUE:
  3032. if(!sd->state.lr_flag)
  3033. sd->bonus.long_hp_gain_value += val;
  3034. break;
  3035. case SP_MAGIC_SP_GAIN_VALUE:
  3036. if(!sd->state.lr_flag)
  3037. sd->bonus.magic_sp_gain_value += val;
  3038. break;
  3039. case SP_MAGIC_HP_GAIN_VALUE:
  3040. if(!sd->state.lr_flag)
  3041. sd->bonus.magic_hp_gain_value += val;
  3042. break;
  3043. case SP_ADD_HEAL_RATE:
  3044. if(sd->state.lr_flag != 2)
  3045. sd->bonus.add_heal_rate += val;
  3046. break;
  3047. case SP_ADD_HEAL2_RATE:
  3048. if(sd->state.lr_flag != 2)
  3049. sd->bonus.add_heal2_rate += val;
  3050. break;
  3051. case SP_ADD_ITEM_HEAL_RATE:
  3052. if(sd->state.lr_flag != 2)
  3053. sd->bonus.itemhealrate2 += val;
  3054. break;
  3055. case SP_EMATK:
  3056. if(sd->state.lr_flag != 2)
  3057. sd->bonus.ematk += val;
  3058. break;
  3059. case SP_ADD_VARIABLECAST:
  3060. if (sd->state.lr_flag != 2)
  3061. sd->bonus.add_varcast += val;
  3062. break;
  3063. #ifdef RENEWAL_CAST
  3064. case SP_FIXCASTRATE:
  3065. if(sd->state.lr_flag != 2)
  3066. sd->bonus.fixcastrate = min(sd->bonus.fixcastrate,val);
  3067. break;
  3068. case SP_ADD_FIXEDCAST:
  3069. if(sd->state.lr_flag != 2)
  3070. sd->bonus.add_fixcast += val;
  3071. break;
  3072. case SP_CASTRATE:
  3073. case SP_VARCASTRATE:
  3074. if(sd->state.lr_flag != 2)
  3075. sd->bonus.varcastrate -= val;
  3076. break;
  3077. #else
  3078. case SP_ADD_FIXEDCAST:
  3079. case SP_FIXCASTRATE:
  3080. //ShowWarning("pc_bonus: non-RENEWAL_CAST doesn't support this bonus %d.\n", type);
  3081. break;
  3082. case SP_VARCASTRATE:
  3083. case SP_CASTRATE:
  3084. if(sd->state.lr_flag != 2)
  3085. sd->castrate += val;
  3086. break;
  3087. #endif
  3088. case SP_ADDMAXWEIGHT:
  3089. if (sd->state.lr_flag != 2)
  3090. sd->add_max_weight += val;
  3091. break;
  3092. case SP_ABSORB_DMG_MAXHP: // bonus bAbsorbDmgMaxHP,n;
  3093. sd->bonus.absorb_dmg_maxhp = max(sd->bonus.absorb_dmg_maxhp, val);
  3094. break;
  3095. case SP_CRITICAL_RANGEATK: // bonus bCriticalLong,n;
  3096. if (sd->state.lr_flag != 2)
  3097. sd->bonus.critical_rangeatk += val*10;
  3098. else
  3099. sd->bonus.arrow_cri += val*10;
  3100. break;
  3101. case SP_WEAPON_ATK_RATE:
  3102. if (sd->state.lr_flag != 2)
  3103. sd->bonus.weapon_atk_rate += val;
  3104. break;
  3105. case SP_WEAPON_MATK_RATE:
  3106. if (sd->state.lr_flag != 2)
  3107. sd->bonus.weapon_matk_rate += val;
  3108. break;
  3109. case SP_NO_MADO_FUEL:
  3110. if (sd->state.lr_flag != 2)
  3111. sd->special_state.no_mado_fuel = 1;
  3112. break;
  3113. case SP_NO_WALK_DELAY:
  3114. if (sd->state.lr_flag != 2)
  3115. sd->special_state.no_walk_delay = 1;
  3116. break;
  3117. default:
  3118. if (running_npc_stat_calc_event) {
  3119. ShowWarning("pc_bonus: unknown bonus type %d %d in OnPCStatCalcEvent!\n", type, val);
  3120. }
  3121. else if (current_equip_combo_pos > 0) {
  3122. 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);
  3123. }
  3124. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  3125. 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);
  3126. }
  3127. else {
  3128. ShowWarning("pc_bonus: unknown bonus type %d %d in unknown usage. Report this!\n", type, val);
  3129. }
  3130. break;
  3131. }
  3132. }
  3133. /*==========================================
  3134. * Player bonus (type) with args type2 and val, called trough bonus2 (npc)
  3135. * format: bonus2 bBonusName,type2,val;
  3136. * @param sd
  3137. * @param type Bonus type used by bBonusName
  3138. * @param type2
  3139. * @param val Value that usually for rate or fixed value
  3140. *------------------------------------------*/
  3141. void pc_bonus2(struct map_session_data *sd,int type,int type2,int val)
  3142. {
  3143. nullpo_retv(sd);
  3144. switch(type){
  3145. case SP_ADDELE: // bonus2 bAddEle,e,x;
  3146. PC_BONUS_CHK_ELEMENT(type2,SP_ADDELE);
  3147. if(!sd->state.lr_flag || sd->state.lr_flag == 3)
  3148. sd->right_weapon.addele[type2]+=val;
  3149. else if(sd->state.lr_flag == 1)
  3150. sd->left_weapon.addele[type2]+=val;
  3151. else if(sd->state.lr_flag == 2)
  3152. sd->arrow_addele[type2]+=val;
  3153. break;
  3154. case SP_ADDRACE: // bonus2 bAddRace,r,x;
  3155. PC_BONUS_CHK_RACE(type2,SP_ADDRACE);
  3156. if(!sd->state.lr_flag || sd->state.lr_flag == 3)
  3157. sd->right_weapon.addrace[type2]+=val;
  3158. else if(sd->state.lr_flag == 1)
  3159. sd->left_weapon.addrace[type2]+=val;
  3160. else if(sd->state.lr_flag == 2)
  3161. sd->arrow_addrace[type2]+=val;
  3162. break;
  3163. case SP_ADDCLASS: // bonus2 bAddClass,c,x;
  3164. PC_BONUS_CHK_CLASS(type2,SP_ADDCLASS);
  3165. if(!sd->state.lr_flag || sd->state.lr_flag == 3)
  3166. sd->right_weapon.addclass[type2]+=val;
  3167. else if(sd->state.lr_flag == 1)
  3168. sd->left_weapon.addclass[type2]+=val;
  3169. else if(sd->state.lr_flag == 2)
  3170. sd->arrow_addclass[type2]+=val;
  3171. break;
  3172. case SP_ADDSIZE: // bonus2 bAddSize,s,x;
  3173. PC_BONUS_CHK_SIZE(type2,SP_ADDSIZE);
  3174. if(!sd->state.lr_flag || sd->state.lr_flag == 3)
  3175. sd->right_weapon.addsize[type2]+=val;
  3176. else if(sd->state.lr_flag == 1)
  3177. sd->left_weapon.addsize[type2]+=val;
  3178. else if(sd->state.lr_flag == 2)
  3179. sd->arrow_addsize[type2]+=val;
  3180. break;
  3181. case SP_SUBELE: // bonus2 bSubEle,e,x;
  3182. PC_BONUS_CHK_ELEMENT(type2,SP_SUBELE);
  3183. if(sd->state.lr_flag != 2)
  3184. sd->subele_script[type2] += val;
  3185. break;
  3186. case SP_SUBRACE: // bonus2 bSubRace,r,x;
  3187. PC_BONUS_CHK_RACE(type2,SP_SUBRACE);
  3188. if(sd->state.lr_flag != 2)
  3189. sd->subrace[type2]+=val;
  3190. break;
  3191. case SP_SUBCLASS: // bonus2 bSubClass,c,x;
  3192. PC_BONUS_CHK_CLASS(type2,SP_SUBCLASS);
  3193. if(sd->state.lr_flag != 2)
  3194. sd->subclass[type2]+=val;
  3195. break;
  3196. case SP_ADDEFF: // bonus2 bAddEff,eff,n;
  3197. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  3198. pc_bonus_addeff(sd->addeff, (sc_type)type2, sd->state.lr_flag != 2 ? val : 0, sd->state.lr_flag == 2 ? val : 0, 0, 0);
  3199. break;
  3200. case SP_ADDEFF2: // bonus2 bAddEff2,eff,n;
  3201. PC_BONUS_CHK_SC(type2,SP_ADDEFF2);
  3202. 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);
  3203. break;
  3204. case SP_RESEFF: // bonus2 bResEff,eff,n;
  3205. if(sd->state.lr_flag == 2)
  3206. break;
  3207. if (sd->reseff.size() == MAX_PC_BONUS) {
  3208. ShowWarning("pc_bonus2: SP_RESEFF: Reached max (%d) number of resistance bonuses per character!\n", MAX_PC_BONUS);
  3209. break;
  3210. }
  3211. pc_bonus_itembonus(sd->reseff, type2, val, true);
  3212. break;
  3213. case SP_MAGIC_ADDELE: // bonus2 bMagicAddEle,e,x;
  3214. PC_BONUS_CHK_ELEMENT(type2,SP_MAGIC_ADDELE);
  3215. if(sd->state.lr_flag != 2)
  3216. sd->magic_addele_script[type2] += val;
  3217. break;
  3218. case SP_MAGIC_ADDRACE: // bonus2 bMagicAddRace,r,x;
  3219. PC_BONUS_CHK_RACE(type2,SP_MAGIC_ADDRACE);
  3220. if(sd->state.lr_flag != 2)
  3221. sd->magic_addrace[type2]+=val;
  3222. break;
  3223. case SP_MAGIC_ADDCLASS: // bonus2 bMagicAddClass,c,x;
  3224. PC_BONUS_CHK_CLASS(type2,SP_MAGIC_ADDCLASS);
  3225. if(sd->state.lr_flag != 2)
  3226. sd->magic_addclass[type2]+=val;
  3227. break;
  3228. case SP_MAGIC_ADDSIZE: // bonus2 bMagicAddSize,s,x;
  3229. PC_BONUS_CHK_SIZE(type2,SP_MAGIC_ADDSIZE);
  3230. if(sd->state.lr_flag != 2)
  3231. sd->magic_addsize[type2]+=val;
  3232. break;
  3233. case SP_MAGIC_ATK_ELE: // bonus2 bMagicAtkEle,e,x;
  3234. PC_BONUS_CHK_ELEMENT(type2,SP_MAGIC_ATK_ELE);
  3235. if(sd->state.lr_flag != 2)
  3236. sd->magic_atk_ele[type2]+=val;
  3237. break;
  3238. case SP_ADD_DAMAGE_CLASS: { // bonus2 bAddDamageClass,mid,x;
  3239. struct weapon_data *wd = (sd->state.lr_flag ? &sd->left_weapon : &sd->right_weapon);
  3240. if (wd->add_dmg.size() == MAX_PC_BONUS) {
  3241. ShowWarning("pc_bonus2: SP_ADD_DAMAGE_CLASS: Reached max (%d) number of add class damage bonuses per character!\n", MAX_PC_BONUS);
  3242. break;
  3243. }
  3244. pc_bonus_itembonus(wd->add_dmg, type2, val, false);
  3245. }
  3246. break;
  3247. case SP_ADD_MAGIC_DAMAGE_CLASS: // bonus2 bAddMagicDamageClass,mid,x;
  3248. if(sd->state.lr_flag == 2)
  3249. break;
  3250. if (sd->add_mdmg.size() == MAX_PC_BONUS) {
  3251. ShowWarning("pc_bonus2: SP_ADD_MAGIC_DAMAGE_CLASS: Reached max (%d) number of add class magic dmg bonuses per character!\n", MAX_PC_BONUS);
  3252. break;
  3253. }
  3254. pc_bonus_itembonus(sd->add_mdmg, type2, val, false);
  3255. break;
  3256. case SP_ADD_DEF_MONSTER: // bonus2 bAddDefMonster,mid,x;
  3257. if(sd->state.lr_flag == 2)
  3258. break;
  3259. if (sd->add_def.size() == MAX_PC_BONUS) {
  3260. ShowWarning("pc_bonus2: SP_ADD_DEF_MONSTER: Reached max (%d) number of add class def bonuses per character!\n", MAX_PC_BONUS);
  3261. break;
  3262. }
  3263. pc_bonus_itembonus(sd->add_def, type2, val, false);
  3264. break;
  3265. case SP_ADD_MDEF_MONSTER: // bonus2 bAddMDefMonster,mid,x;
  3266. if(sd->state.lr_flag == 2)
  3267. break;
  3268. if (sd->add_mdef.size() == MAX_PC_BONUS) {
  3269. ShowWarning("pc_bonus2: SP_ADD_MDEF_MONSTER: Reached max (%d) number of add class mdef bonuses per character!\n", MAX_PC_BONUS);
  3270. break;
  3271. }
  3272. pc_bonus_itembonus(sd->add_mdef, type2, val, false);
  3273. break;
  3274. case SP_HP_DRAIN_RATE: // bonus2 bHPDrainRate,x,n;
  3275. if(!sd->state.lr_flag) {
  3276. sd->right_weapon.hp_drain_rate.rate += type2;
  3277. sd->right_weapon.hp_drain_rate.per += val;
  3278. }
  3279. else if(sd->state.lr_flag == 1) {
  3280. sd->left_weapon.hp_drain_rate.rate += type2;
  3281. sd->left_weapon.hp_drain_rate.per += val;
  3282. }
  3283. break;
  3284. case SP_SP_DRAIN_RATE: // bonus2 bSPDrainRate,x,n;
  3285. if(!sd->state.lr_flag) {
  3286. sd->right_weapon.sp_drain_rate.rate += type2;
  3287. sd->right_weapon.sp_drain_rate.per += val;
  3288. }
  3289. else if(sd->state.lr_flag == 1) {
  3290. sd->left_weapon.sp_drain_rate.rate += type2;
  3291. sd->left_weapon.sp_drain_rate.per += val;
  3292. }
  3293. break;
  3294. case SP_SP_VANISH_RATE: // bonus2 bSPVanishRate,x,n;
  3295. if(sd->state.lr_flag != 2) {
  3296. pc_bonus_addvanish(sd->sp_vanish, type2, val, BF_NORMAL);
  3297. }
  3298. break;
  3299. case SP_HP_VANISH_RATE: // bonus2 bHPVanishRate,x,n;
  3300. if(sd->state.lr_flag != 2) {
  3301. pc_bonus_addvanish(sd->hp_vanish, type2, val, BF_NORMAL);
  3302. }
  3303. break;
  3304. case SP_GET_ZENY_NUM: // bonus2 bGetZenyNum,x,n;
  3305. if(sd->state.lr_flag != 2 && sd->bonus.get_zeny_rate < val) {
  3306. sd->bonus.get_zeny_rate = val;
  3307. sd->bonus.get_zeny_num = type2;
  3308. }
  3309. break;
  3310. case SP_ADD_GET_ZENY_NUM: // bonus2 bAddGetZenyNum,x,n;
  3311. if(sd->state.lr_flag != 2) {
  3312. sd->bonus.get_zeny_rate += val;
  3313. sd->bonus.get_zeny_num += type2;
  3314. }
  3315. break;
  3316. case SP_WEAPON_COMA_ELE: // bonus2 bWeaponComaEle,e,n;
  3317. PC_BONUS_CHK_ELEMENT(type2,SP_WEAPON_COMA_ELE);
  3318. if(sd->state.lr_flag == 2)
  3319. break;
  3320. sd->weapon_coma_ele[type2] += val;
  3321. sd->special_state.bonus_coma = 1;
  3322. break;
  3323. case SP_WEAPON_COMA_RACE: // bonus2 bWeaponComaRace,r,n;
  3324. PC_BONUS_CHK_RACE(type2,SP_WEAPON_COMA_RACE);
  3325. if(sd->state.lr_flag == 2)
  3326. break;
  3327. sd->weapon_coma_race[type2] += val;
  3328. sd->special_state.bonus_coma = 1;
  3329. break;
  3330. case SP_WEAPON_COMA_CLASS: // bonus2 bWeaponComaClass,c,n;
  3331. PC_BONUS_CHK_CLASS(type2,SP_WEAPON_COMA_CLASS);
  3332. if(sd->state.lr_flag == 2)
  3333. break;
  3334. sd->weapon_coma_class[type2] += val;
  3335. sd->special_state.bonus_coma = 1;
  3336. break;
  3337. case SP_WEAPON_ATK: // bonus2 bWeaponAtk,w,n;
  3338. if(sd->state.lr_flag != 2)
  3339. sd->weapon_atk[type2]+=val;
  3340. break;
  3341. case SP_WEAPON_DAMAGE_RATE: // bonus2 bWeaponDamageRate,w,n;
  3342. if(sd->state.lr_flag != 2)
  3343. sd->weapon_damage_rate[type2]+=val;
  3344. break;
  3345. case SP_CRITICAL_ADDRACE: // bonus2 bCriticalAddRace,r,n;
  3346. PC_BONUS_CHK_RACE(type2,SP_CRITICAL_ADDRACE);
  3347. if(sd->state.lr_flag != 2)
  3348. sd->critaddrace[type2] += val*10;
  3349. break;
  3350. case SP_ADDEFF_WHENHIT: // bonus2 bAddEffWhenHit,eff,n;
  3351. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  3352. if(sd->state.lr_flag != 2)
  3353. pc_bonus_addeff(sd->addeff_atked, (sc_type)type2, val, 0, 0, 0);
  3354. break;
  3355. case SP_SKILL_ATK: // bonus2 bSkillAtk,sk,n;
  3356. if(sd->state.lr_flag == 2)
  3357. break;
  3358. if (sd->skillatk.size() == MAX_PC_BONUS) {
  3359. 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);
  3360. break;
  3361. }
  3362. pc_bonus_itembonus(sd->skillatk, type2, val, false);
  3363. break;
  3364. case SP_SKILL_HEAL: // bonus2 bSkillHeal,sk,n;
  3365. if(sd->state.lr_flag == 2)
  3366. break;
  3367. if (sd->skillheal.size() == MAX_PC_BONUS) { // Better mention this so the array length can be updated. [Skotlex]
  3368. 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);
  3369. break;
  3370. }
  3371. pc_bonus_itembonus(sd->skillheal, type2, val, false);
  3372. break;
  3373. case SP_SKILL_HEAL2: // bonus2 bSkillHeal2,sk,n;
  3374. if(sd->state.lr_flag == 2)
  3375. break;
  3376. if (sd->skillheal2.size() == MAX_PC_BONUS) { // Better mention this so the array length can be updated. [Skotlex]
  3377. 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);
  3378. break;
  3379. }
  3380. pc_bonus_itembonus(sd->skillheal2, type2, val, false);
  3381. break;
  3382. case SP_ADD_SKILL_BLOW: // bonus2 bAddSkillBlow,sk,n;
  3383. if(sd->state.lr_flag == 2)
  3384. break;
  3385. if (sd->skillblown.size() == MAX_PC_BONUS) { //Better mention this so the array length can be updated. [Skotlex]
  3386. 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);
  3387. break;
  3388. }
  3389. pc_bonus_itembonus(sd->skillblown, type2, val, false);
  3390. break;
  3391. case SP_HP_LOSS_RATE: // bonus2 bHPLossRate,n,t;
  3392. if(sd->state.lr_flag != 2) {
  3393. sd->hp_loss.value = type2;
  3394. sd->hp_loss.rate = val;
  3395. }
  3396. break;
  3397. case SP_HP_REGEN_RATE: // bonus2 bHPRegenRate,n,t;
  3398. if(sd->state.lr_flag != 2) {
  3399. sd->hp_regen.value = type2;
  3400. sd->hp_regen.rate = val;
  3401. }
  3402. break;
  3403. case SP_REGEN_PERCENT_HP: // bonus2 bRegenPercentHP,n,t;
  3404. if (sd->state.lr_flag != 2) {
  3405. sd->percent_hp_regen.value = type2;
  3406. sd->percent_hp_regen.rate = val;
  3407. }
  3408. break;
  3409. case SP_REGEN_PERCENT_SP: // bonus2 bRegenPercentSP,n,t;
  3410. if (sd->state.lr_flag != 2) {
  3411. sd->percent_sp_regen.value = type2;
  3412. sd->percent_sp_regen.rate = val;
  3413. }
  3414. break;
  3415. case SP_ADDRACE2: // bonus2 bAddRace2,mr,x;
  3416. PC_BONUS_CHK_RACE2(type2,SP_ADDRACE2);
  3417. if(sd->state.lr_flag != 2)
  3418. sd->right_weapon.addrace2[type2] += val;
  3419. else
  3420. sd->left_weapon.addrace2[type2] += val;
  3421. break;
  3422. case SP_SUBSIZE: // bonus2 bSubSize,s,x;
  3423. PC_BONUS_CHK_SIZE(type2,SP_SUBSIZE);
  3424. if(sd->state.lr_flag != 2)
  3425. sd->subsize[type2]+=val;
  3426. break;
  3427. case SP_SUBRACE2: // bonus2 bSubRace2,mr,x;
  3428. PC_BONUS_CHK_RACE2(type2,SP_SUBRACE2);
  3429. if(sd->state.lr_flag != 2)
  3430. sd->subrace2[type2]+=val;
  3431. break;
  3432. case SP_ADD_ITEM_HEAL_RATE: // bonus2 bAddItemHealRate,iid,n;
  3433. if(sd->state.lr_flag == 2)
  3434. break;
  3435. if (!itemdb_exists(type2)) {
  3436. ShowWarning("pc_bonus2: SP_ADD_ITEM_HEAL_RATE Invalid item with id %d\n", type2);
  3437. break;
  3438. }
  3439. if (sd->itemhealrate.size() == MAX_PC_BONUS) {
  3440. ShowWarning("pc_bonus2: SP_ADD_ITEM_HEAL_RATE: Reached max (%d) number of item heal bonuses per character!\n", MAX_PC_BONUS);
  3441. break;
  3442. }
  3443. pc_bonus_itembonus(sd->itemhealrate, type2, val, false);
  3444. break;
  3445. case SP_ADD_ITEMGROUP_HEAL_RATE: // bonus2 bAddItemGroupHealRate,ig,n;
  3446. if (sd->state.lr_flag == 2)
  3447. break;
  3448. if (!type2 || !itemdb_group_exists(type2)) {
  3449. ShowWarning("pc_bonus2: SP_ADD_ITEMGROUP_HEAL_RATE: Invalid item group with id %d\n", type2);
  3450. break;
  3451. }
  3452. if (sd->itemhealrate.size() == MAX_PC_BONUS) {
  3453. ShowWarning("pc_bonus2: SP_ADD_ITEMGROUP_HEAL_RATE: Reached max (%d) number of item heal bonuses per character!\n", MAX_PC_BONUS);
  3454. break;
  3455. }
  3456. pc_bonus_itembonus(sd->itemgrouphealrate, type2, val, false);
  3457. break;
  3458. case SP_EXP_ADDRACE: // bonus2 bExpAddRace,r,x;
  3459. PC_BONUS_CHK_RACE(type2,SP_EXP_ADDRACE);
  3460. if(sd->state.lr_flag != 2)
  3461. sd->expaddrace[type2]+=val;
  3462. break;
  3463. case SP_EXP_ADDCLASS: // bonus2 bExpAddClass,c,x;
  3464. PC_BONUS_CHK_CLASS(type2,SP_EXP_ADDCLASS);
  3465. if(sd->state.lr_flag != 2)
  3466. sd->expaddclass[type2]+=val;
  3467. break;
  3468. case SP_SP_GAIN_RACE: // bonus2 bSPGainRace,r,n;
  3469. PC_BONUS_CHK_RACE(type2,SP_SP_GAIN_RACE);
  3470. if(sd->state.lr_flag != 2)
  3471. sd->sp_gain_race[type2]+=val;
  3472. break;
  3473. case SP_ADD_MONSTER_DROP_ITEM: // bonus2 bAddMonsterDropItem,iid,n;
  3474. if (sd->state.lr_flag != 2)
  3475. pc_bonus_item_drop(sd->add_drop, type2, 0, CLASS_ALL, RC_NONE_, val);
  3476. break;
  3477. case SP_ADD_MONSTER_DROP_ITEMGROUP: // bonus2 bAddMonsterDropItemGroup,ig,n;
  3478. if (sd->state.lr_flag != 2)
  3479. pc_bonus_item_drop(sd->add_drop, 0, type2, CLASS_ALL, RC_NONE_, val);
  3480. break;
  3481. case SP_SP_LOSS_RATE: // bonus2 bSPLossRate,n,t;
  3482. if(sd->state.lr_flag != 2) {
  3483. sd->sp_loss.value = type2;
  3484. sd->sp_loss.rate = val;
  3485. }
  3486. break;
  3487. case SP_SP_REGEN_RATE: // bonus2 bSPRegenRate,n,t;
  3488. if(sd->state.lr_flag != 2) {
  3489. sd->sp_regen.value = type2;
  3490. sd->sp_regen.rate = val;
  3491. }
  3492. break;
  3493. case SP_HP_DRAIN_VALUE_RACE: // bonus2 bHPDrainValueRace,r,n;
  3494. PC_BONUS_CHK_RACE(type2,SP_HP_DRAIN_VALUE_RACE);
  3495. if(!sd->state.lr_flag) {
  3496. sd->right_weapon.hp_drain_race[type2] += val;
  3497. }
  3498. else if(sd->state.lr_flag == 1) {
  3499. sd->left_weapon.hp_drain_race[type2] += val;
  3500. }
  3501. break;
  3502. case SP_SP_DRAIN_VALUE_RACE: // bonus2 bSPDrainValueRace,r,n;
  3503. PC_BONUS_CHK_RACE(type2,SP_SP_DRAIN_VALUE_RACE);
  3504. if(!sd->state.lr_flag) {
  3505. sd->right_weapon.sp_drain_race[type2] += val;
  3506. }
  3507. else if(sd->state.lr_flag == 1) {
  3508. sd->left_weapon.sp_drain_race[type2] += val;
  3509. }
  3510. break;
  3511. case SP_HP_DRAIN_VALUE_CLASS: // bonus2 bHPDrainValueClass,c,n;
  3512. PC_BONUS_CHK_CLASS(type2,SP_HP_DRAIN_VALUE_CLASS);
  3513. if(!sd->state.lr_flag) {
  3514. sd->right_weapon.hp_drain_class[type2] += val;
  3515. }
  3516. else if(sd->state.lr_flag == 1) {
  3517. sd->left_weapon.hp_drain_class[type2] += val;
  3518. }
  3519. break;
  3520. case SP_SP_DRAIN_VALUE_CLASS: // bonus2 bSPDrainValueClass,c,n;
  3521. PC_BONUS_CHK_CLASS(type2,SP_SP_DRAIN_VALUE_CLASS);
  3522. if(!sd->state.lr_flag) {
  3523. sd->right_weapon.sp_drain_class[type2] += val;
  3524. }
  3525. else if(sd->state.lr_flag == 1) {
  3526. sd->left_weapon.sp_drain_class[type2] += val;
  3527. }
  3528. break;
  3529. case SP_IGNORE_MDEF_RACE_RATE: // bonus2 bIgnoreMdefRaceRate,r,n;
  3530. PC_BONUS_CHK_RACE(type2,SP_IGNORE_MDEF_RACE_RATE);
  3531. if(sd->state.lr_flag != 2)
  3532. sd->ignore_mdef_by_race[type2] += val;
  3533. break;
  3534. case SP_IGNORE_MDEF_CLASS_RATE: // bonus2 bIgnoreMdefClassRate,c,n;
  3535. PC_BONUS_CHK_CLASS(type2,SP_IGNORE_MDEF_CLASS_RATE);
  3536. if(sd->state.lr_flag != 2)
  3537. sd->ignore_mdef_by_class[type2] += val;
  3538. break;
  3539. case SP_IGNORE_DEF_RACE_RATE: // bonus2 bIgnoreDefRaceRate,r,n;
  3540. PC_BONUS_CHK_RACE(type2,SP_IGNORE_DEF_RACE_RATE);
  3541. if(sd->state.lr_flag != 2)
  3542. sd->ignore_def_by_race[type2] += val;
  3543. break;
  3544. case SP_IGNORE_DEF_CLASS_RATE: // bonus2 bIgnoreDefClassRate,r,n;
  3545. PC_BONUS_CHK_CLASS(type2, SP_IGNORE_DEF_CLASS_RATE);
  3546. if (sd->state.lr_flag != 2)
  3547. sd->ignore_def_by_class[type2] += val;
  3548. break;
  3549. case SP_SKILL_USE_SP_RATE: // bonus2 bSkillUseSPrate,sk,n;
  3550. if(sd->state.lr_flag == 2)
  3551. break;
  3552. if (sd->skillusesprate.size() == MAX_PC_BONUS) {
  3553. 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);
  3554. break;
  3555. }
  3556. pc_bonus_itembonus(sd->skillusesprate, type2, val, true);
  3557. break;
  3558. case SP_SKILL_DELAY:
  3559. if(sd->state.lr_flag == 2)
  3560. break;
  3561. if (sd->skilldelay.size() == MAX_PC_BONUS) {
  3562. 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);
  3563. break;
  3564. }
  3565. pc_bonus_itembonus(sd->skilldelay, type2, val, false);
  3566. break;
  3567. case SP_SKILL_COOLDOWN: // bonus2 bSkillCooldown,sk,t;
  3568. if(sd->state.lr_flag == 2)
  3569. break;
  3570. if (sd->skillcooldown.size() == MAX_PC_BONUS) {
  3571. 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);
  3572. break;
  3573. }
  3574. pc_bonus_itembonus(sd->skillcooldown, type2, val, false);
  3575. break;
  3576. case SP_SKILL_VARIABLECAST: // bonus2 bSkillVariableCast,sk,t;
  3577. if (sd->state.lr_flag == 2)
  3578. break;
  3579. if (sd->skillvarcast.size() == MAX_PC_BONUS) {
  3580. 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);
  3581. break;
  3582. }
  3583. pc_bonus_itembonus(sd->skillvarcast, type2, val, false);
  3584. break;
  3585. #ifdef RENEWAL_CAST
  3586. case SP_SKILL_FIXEDCAST: // bonus2 bSkillFixedCast,sk,t;
  3587. if(sd->state.lr_flag == 2)
  3588. break;
  3589. if (sd->skillfixcast.size() == MAX_PC_BONUS) {
  3590. 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);
  3591. break;
  3592. }
  3593. pc_bonus_itembonus(sd->skillfixcast, type2, val, false);
  3594. break;
  3595. case SP_CASTRATE: // bonus2 bCastrate,sk,n;
  3596. case SP_VARCASTRATE: // bonus2 bVariableCastrate,sk,n;
  3597. if(sd->state.lr_flag == 2)
  3598. break;
  3599. if (sd->skillcastrate.size() == MAX_PC_BONUS) {
  3600. ShowWarning("pc_bonus2: SP_VARCASTRATE: Reached max (%d) number of skills per character, bonus skill %d (%d%%) lost.\n", MAX_PC_BONUS, type2, val);
  3601. break;
  3602. }
  3603. pc_bonus_itembonus(sd->skillcastrate, type2, -val, true); // Send inversed value here
  3604. break;
  3605. case SP_FIXCASTRATE: // bonus2 bFixedCastrate,sk,n;
  3606. if(sd->state.lr_flag == 2)
  3607. break;
  3608. if (sd->skillfixcastrate.size() == MAX_PC_BONUS) {
  3609. ShowWarning("pc_bonus2: SP_FIXCASTRATE: Reached max (%d) number of skills per character, bonus skill %d (%d%%) lost.\n", MAX_PC_BONUS, type2, val);
  3610. break;
  3611. }
  3612. pc_bonus_itembonus(sd->skillfixcastrate, type2, -val, true); // Send inversed value here
  3613. break;
  3614. #else
  3615. case SP_SKILL_FIXEDCAST: // bonus2 bSkillFixedCast,sk,t;
  3616. case SP_FIXCASTRATE: // bonus2 bFixedCastrate,sk,n;
  3617. //ShowWarning("pc_bonus2: Non-RENEWAL_CAST doesn't support this bonus %d.\n", type);
  3618. break;
  3619. case SP_VARCASTRATE: // bonus2 bVariableCastrate,sk,n;
  3620. case SP_CASTRATE: // bonus2 bCastrate,sk,n;
  3621. if(sd->state.lr_flag == 2)
  3622. break;
  3623. if (sd->skillcastrate.size() == MAX_PC_BONUS) { //Better mention this so the array length can be updated. [Skotlex]
  3624. 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);
  3625. break;
  3626. }
  3627. pc_bonus_itembonus(sd->skillcastrate, type2, val, true);
  3628. break;
  3629. #endif
  3630. case SP_SKILL_USE_SP: // bonus2 bSkillUseSP,sk,n;
  3631. if(sd->state.lr_flag == 2)
  3632. break;
  3633. if (sd->skillusesp.size() == MAX_PC_BONUS) {
  3634. 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);
  3635. break;
  3636. }
  3637. pc_bonus_itembonus(sd->skillusesp, type2, val, false);
  3638. break;
  3639. case SP_SUB_SKILL: // bonus2 bSubSkill,sk,n;
  3640. if (sd->subskill.size() == MAX_PC_BONUS) {
  3641. 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);
  3642. break;
  3643. }
  3644. pc_bonus_itembonus(sd->subskill, type2, val, false);
  3645. break;
  3646. case SP_SUBDEF_ELE: // bonus2 bSubDefEle,e,x;
  3647. PC_BONUS_CHK_ELEMENT(type2,SP_SUBDEF_ELE);
  3648. sd->subdefele[type2] += val;
  3649. break;
  3650. case SP_COMA_CLASS: // bonus2 bComaClass,c,n;
  3651. PC_BONUS_CHK_CLASS(type2,SP_COMA_CLASS);
  3652. sd->coma_class[type2] += val;
  3653. sd->special_state.bonus_coma = 1;
  3654. break;
  3655. case SP_COMA_RACE: // bonus2 bComaRace,r,n;
  3656. PC_BONUS_CHK_RACE(type2,SP_COMA_RACE);
  3657. sd->coma_race[type2] += val;
  3658. sd->special_state.bonus_coma = 1;
  3659. break;
  3660. case SP_MAGIC_ADDRACE2: // bonus2 bMagicAddRace2,mr,n;
  3661. PC_BONUS_CHK_RACE2(type2, SP_MAGIC_ADDRACE2);
  3662. if(sd->state.lr_flag != 2)
  3663. sd->magic_addrace2[type2] += val;
  3664. break;
  3665. case SP_IGNORE_MDEF_RACE2_RATE: //bonus2 bIgnoreMdefRace2Rate,mr,n;
  3666. PC_BONUS_CHK_RACE2(type2, SP_IGNORE_MDEF_RACE2);
  3667. if (sd->state.lr_flag != 2)
  3668. sd->ignore_mdef_by_race2[type2] += val;
  3669. break;
  3670. case SP_DROP_ADDRACE: // bonus2 bDropAddRace,r,x;
  3671. PC_BONUS_CHK_RACE(type2, SP_DROP_ADDRACE);
  3672. if (sd->state.lr_flag != 2)
  3673. sd->dropaddrace[type2] += val;
  3674. break;
  3675. case SP_DROP_ADDCLASS: // bonus2 bDropAddClass,c,x;
  3676. PC_BONUS_CHK_CLASS(type2, SP_DROP_ADDCLASS);
  3677. if (sd->state.lr_flag != 2)
  3678. sd->dropaddclass[type2] += val;
  3679. break;
  3680. default:
  3681. if (running_npc_stat_calc_event) {
  3682. ShowWarning("pc_bonus2: unknown bonus type %d %d %d in OnPCStatCalcEvent!\n", type, type2, val);
  3683. }
  3684. else if (current_equip_combo_pos > 0) {
  3685. 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);
  3686. }
  3687. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  3688. 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);
  3689. }
  3690. else {
  3691. ShowWarning("pc_bonus2: unknown bonus type %d %d %d in unknown usage. Report this!\n", type, type2, val);
  3692. }
  3693. break;
  3694. }
  3695. }
  3696. /**
  3697. * Gives item bonus to player for format: bonus3 bBonusName,type2,type3,val;
  3698. * @param sd
  3699. * @param type Bonus type used by bBonusName
  3700. * @param type2
  3701. * @param type3
  3702. * @param val Value that usually for rate or fixed value
  3703. */
  3704. void pc_bonus3(struct map_session_data *sd,int type,int type2,int type3,int val)
  3705. {
  3706. nullpo_retv(sd);
  3707. switch(type){
  3708. case SP_ADD_MONSTER_DROP_ITEM: // bonus3 bAddMonsterDropItem,iid,r,n;
  3709. if(sd->state.lr_flag != 2)
  3710. pc_bonus_item_drop(sd->add_drop, type2, 0, CLASS_NONE, type3, val);
  3711. break;
  3712. case SP_ADD_MONSTER_ID_DROP_ITEM: // bonus3 bAddMonsterIdDropItem,iid,mid,n;
  3713. if(sd->state.lr_flag != 2)
  3714. pc_bonus_item_drop(sd->add_drop, type2, 0, CLASS_NONE, -type3, val);
  3715. break;
  3716. case SP_ADD_CLASS_DROP_ITEM: // bonus3 bAddClassDropItem,iid,c,n;
  3717. if(sd->state.lr_flag != 2)
  3718. pc_bonus_item_drop(sd->add_drop, type2, 0, type3, RC_NONE_, val);
  3719. break;
  3720. case SP_AUTOSPELL: // bonus3 bAutoSpell,sk,y,n;
  3721. if(sd->state.lr_flag != 2)
  3722. {
  3723. int target = skill_get_inf(type2); //Support or Self (non-auto-target) skills should pick self.
  3724. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !skill_get_inf2(type2, INF2_NOTARGETSELF));
  3725. pc_bonus_autospell(sd->autospell, target?-type2:type2, type3, val, 0, current_equip_card_id);
  3726. }
  3727. break;
  3728. case SP_AUTOSPELL_WHENHIT: // bonus3 bAutoSpellWhenHit,sk,y,n;
  3729. if(sd->state.lr_flag != 2)
  3730. {
  3731. int target = skill_get_inf(type2); //Support or Self (non-auto-target) skills should pick self.
  3732. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !skill_get_inf2(type2, INF2_NOTARGETSELF));
  3733. pc_bonus_autospell(sd->autospell2, target?-type2:type2, type3, val, BF_NORMAL|BF_SKILL, current_equip_card_id);
  3734. }
  3735. break;
  3736. case SP_ADD_MONSTER_DROP_ITEMGROUP: // bonus3 bAddMonsterDropItemGroup,ig,r,n;
  3737. if (sd->state.lr_flag != 2)
  3738. pc_bonus_item_drop(sd->add_drop, 0, type2, CLASS_NONE, type3, val);
  3739. break;
  3740. case SP_ADD_CLASS_DROP_ITEMGROUP: // bonus3 bAddClassDropItemGroup,ig,c,n;
  3741. if (sd->state.lr_flag != 2)
  3742. pc_bonus_item_drop(sd->add_drop, 0, type2, type3, RC_NONE_, val);
  3743. break;
  3744. case SP_ADDEFF: // bonus3 bAddEff,eff,n,y;
  3745. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  3746. pc_bonus_addeff(sd->addeff, (sc_type)type2, sd->state.lr_flag != 2 ? type3 : 0, sd->state.lr_flag == 2 ? type3 : 0, val, 0);
  3747. break;
  3748. case SP_ADDEFF_WHENHIT: // bonus3 bAddEffWhenHit,eff,n,y;
  3749. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  3750. if(sd->state.lr_flag != 2)
  3751. pc_bonus_addeff(sd->addeff_atked, (sc_type)type2, type3, 0, val, 0);
  3752. break;
  3753. case SP_ADDEFF_ONSKILL: // bonus3 bAddEffOnSkill,sk,eff,n;
  3754. PC_BONUS_CHK_SC(type2,SP_ADDEFF_ONSKILL);
  3755. if( sd->state.lr_flag != 2 )
  3756. pc_bonus_addeff_onskill(sd->addeff_onskill, (sc_type)type3, val, type2, ATF_TARGET, 0);
  3757. break;
  3758. case SP_ADDELE: // bonus3 bAddEle,e,x,bf;
  3759. PC_BONUS_CHK_ELEMENT(type2,SP_ADDELE);
  3760. if (sd->state.lr_flag != 2)
  3761. pc_bonus_addele(sd, (unsigned char)type2, type3, val);
  3762. break;
  3763. case SP_SUBELE: // bonus3 bSubEle,e,x,bf;
  3764. PC_BONUS_CHK_ELEMENT(type2,SP_SUBELE);
  3765. if (sd->state.lr_flag != 2)
  3766. pc_bonus_subele(sd, (unsigned char)type2, type3, val);
  3767. break;
  3768. case SP_SP_VANISH_RACE_RATE: // bonus3 bSPVanishRaceRate,r,x,n;
  3769. PC_BONUS_CHK_RACE(type2,SP_SP_VANISH_RACE_RATE);
  3770. if(sd->state.lr_flag != 2) {
  3771. sd->sp_vanish_race[type2].rate += type3;
  3772. sd->sp_vanish_race[type2].per += val;
  3773. }
  3774. break;
  3775. case SP_HP_VANISH_RACE_RATE: // bonus3 bHPVanishRaceRate,r,x,n;
  3776. PC_BONUS_CHK_RACE(type2,SP_HP_VANISH_RACE_RATE);
  3777. if(sd->state.lr_flag != 2) {
  3778. sd->hp_vanish_race[type2].rate += type3;
  3779. sd->hp_vanish_race[type2].per += val;
  3780. }
  3781. break;
  3782. case SP_SP_VANISH_RATE: // bonus3 bSPVanishRate,x,n,bf;
  3783. if(sd->state.lr_flag != 2) {
  3784. pc_bonus_addvanish(sd->sp_vanish, type2, type3, val);
  3785. }
  3786. break;
  3787. case SP_HP_VANISH_RATE: // bonus3 bHPVanishRate,x,n,bf;
  3788. if(sd->state.lr_flag != 2) {
  3789. pc_bonus_addvanish(sd->hp_vanish, type2, type3, val);
  3790. }
  3791. break;
  3792. case SP_STATE_NORECOVER_RACE: // bonus3 bStateNoRecoverRace,r,x,t;
  3793. PC_BONUS_CHK_RACE(type2, SP_STATE_NORECOVER_RACE);
  3794. if (sd->state.lr_flag == 2)
  3795. break;
  3796. //! CONFIRM: Is it not stackable? Does not check max or min value?
  3797. //if (type3 > sd->norecover_state_race[type2].rate) {
  3798. // sd->norecover_state_race[type2].rate = type3;
  3799. // sd->norecover_state_race[type2].tick = val;
  3800. // break;
  3801. //}
  3802. sd->norecover_state_race[type2].rate = type3;
  3803. sd->norecover_state_race[type2].tick = val;
  3804. break;
  3805. default:
  3806. if (running_npc_stat_calc_event) {
  3807. ShowWarning("pc_bonus3: unknown bonus type %d %d %d %d in OnPCStatCalcEvent!\n", type, type2, type3, val);
  3808. }
  3809. else if (current_equip_combo_pos > 0) {
  3810. 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);
  3811. }
  3812. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  3813. 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);
  3814. }
  3815. else {
  3816. ShowWarning("pc_bonus3: unknown bonus type %d %d %d %d in unknown usage. Report this!\n", type, type2, type3, val);
  3817. }
  3818. break;
  3819. }
  3820. }
  3821. /**
  3822. * Gives item bonus to player for format: bonus4 bBonusName,type2,type3,type4,val;
  3823. * @param sd
  3824. * @param type Bonus type used by bBonusName
  3825. * @param type2
  3826. * @param type3
  3827. * @param type4
  3828. * @param val Value that usually for rate or fixed value
  3829. */
  3830. void pc_bonus4(struct map_session_data *sd,int type,int type2,int type3,int type4,int val)
  3831. {
  3832. nullpo_retv(sd);
  3833. switch(type){
  3834. case SP_AUTOSPELL: // bonus4 bAutoSpell,sk,y,n,i;
  3835. if(sd->state.lr_flag != 2)
  3836. pc_bonus_autospell(sd->autospell, (val&1?type2:-type2), (val&2?-type3:type3), type4, 0, current_equip_card_id);
  3837. break;
  3838. case SP_AUTOSPELL_WHENHIT: // bonus4 bAutoSpellWhenHit,sk,y,n,i;
  3839. if(sd->state.lr_flag != 2)
  3840. pc_bonus_autospell(sd->autospell2, (val&1?type2:-type2), (val&2?-type3:type3), type4, BF_NORMAL|BF_SKILL, current_equip_card_id);
  3841. break;
  3842. case SP_AUTOSPELL_ONSKILL: // bonus4 bAutoSpellOnSkill,sk,x,y,n;
  3843. if(sd->state.lr_flag != 2)
  3844. {
  3845. int target = skill_get_inf(type3); //Support or Self (non-auto-target) skills should pick self.
  3846. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !skill_get_inf2(type3, INF2_NOTARGETSELF));
  3847. pc_bonus_autospell_onskill(sd->autospell3, type2, target?-type3:type3, type4, val, current_equip_card_id);
  3848. }
  3849. break;
  3850. case SP_ADDEFF: // bonus4 bAddEff,eff,n,y,t;
  3851. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  3852. pc_bonus_addeff(sd->addeff, (sc_type)type2, sd->state.lr_flag != 2 ? type3 : 0, sd->state.lr_flag == 2 ? type3 : 0, type4, val);
  3853. break;
  3854. case SP_ADDEFF_WHENHIT: // bonus4 bAddEffWhenHit,eff,n,y,t;
  3855. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  3856. if (sd->state.lr_flag != 2)
  3857. pc_bonus_addeff(sd->addeff_atked, (sc_type)type2, type3, 0, type4, val);
  3858. break;
  3859. case SP_ADDEFF_ONSKILL: // bonus4 bAddEffOnSkill,sk,eff,n,y;
  3860. PC_BONUS_CHK_SC(type3,SP_ADDEFF_ONSKILL);
  3861. if( sd->state.lr_flag != 2 )
  3862. pc_bonus_addeff_onskill(sd->addeff_onskill, (sc_type)type3, type4, type2, val, 0);
  3863. break;
  3864. case SP_SET_DEF_RACE: // bonus4 bSetDefRace,r,n,t,y;
  3865. PC_BONUS_CHK_RACE(type2,SP_SET_DEF_RACE);
  3866. if(sd->state.lr_flag == 2)
  3867. break;
  3868. sd->def_set_race[type2].rate = type3;
  3869. sd->def_set_race[type2].tick = type4;
  3870. sd->def_set_race[type2].value = val;
  3871. break;
  3872. case SP_SET_MDEF_RACE: // bonus4 bSetMDefRace,r,n,t,y;
  3873. PC_BONUS_CHK_RACE(type2,SP_SET_MDEF_RACE);
  3874. if(sd->state.lr_flag == 2)
  3875. break;
  3876. sd->mdef_set_race[type2].rate = type3;
  3877. sd->mdef_set_race[type2].tick = type4;
  3878. sd->mdef_set_race[type2].value = val;
  3879. break;
  3880. default:
  3881. if (running_npc_stat_calc_event) {
  3882. ShowWarning("pc_bonus4: unknown bonus type %d %d %d %d %d in OnPCStatCalcEvent!\n", type, type2, type3, type4, val);
  3883. }
  3884. else if (current_equip_combo_pos > 0) {
  3885. 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);
  3886. }
  3887. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  3888. 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);
  3889. }
  3890. else {
  3891. ShowWarning("pc_bonus4: unknown bonus type %d %d %d %d %d in unknown usage. Report this!\n", type, type2, type3, type4, val);
  3892. }
  3893. break;
  3894. }
  3895. }
  3896. /**
  3897. * Gives item bonus to player for format: bonus5 bBonusName,type2,type3,type4,val;
  3898. * @param sd
  3899. * @param type Bonus type used by bBonusName
  3900. * @param type2
  3901. * @param type3
  3902. * @param type4
  3903. * @param val Value that usually for rate or fixed value
  3904. */
  3905. void pc_bonus5(struct map_session_data *sd,int type,int type2,int type3,int type4,int type5,int val)
  3906. {
  3907. nullpo_retv(sd);
  3908. switch(type){
  3909. case SP_AUTOSPELL: // bonus5 bAutoSpell,sk,y,n,bf,i;
  3910. if(sd->state.lr_flag != 2)
  3911. pc_bonus_autospell(sd->autospell, (val&1?type2:-type2), (val&2?-type3:type3), type4, type5, current_equip_card_id);
  3912. break;
  3913. case SP_AUTOSPELL_WHENHIT: // bonus5 bAutoSpellWhenHit,sk,y,n,bf,i;
  3914. if(sd->state.lr_flag != 2)
  3915. pc_bonus_autospell(sd->autospell2, (val&1?type2:-type2), (val&2?-type3:type3), type4, type5, current_equip_card_id);
  3916. break;
  3917. case SP_AUTOSPELL_ONSKILL: // bonus5 bAutoSpellOnSkill,sk,x,y,n,i;
  3918. if(sd->state.lr_flag != 2)
  3919. pc_bonus_autospell_onskill(sd->autospell3, type2, (val&1?-type3:type3), (val&2?-type4:type4), type5, current_equip_card_id);
  3920. break;
  3921. case SP_ADDEFF_ONSKILL: // bonus5 bAddEffOnSkill,sk,eff,n,y,t;
  3922. PC_BONUS_CHK_SC(type3,SP_ADDEFF_ONSKILL);
  3923. if( sd->state.lr_flag != 2 )
  3924. pc_bonus_addeff_onskill(sd->addeff_onskill, (sc_type)type3, type4, type2, type5, val);
  3925. break;
  3926. default:
  3927. if (running_npc_stat_calc_event) {
  3928. ShowWarning("pc_bonus5: unknown bonus type %d %d %d %d %d %d in OnPCStatCalcEvent!\n", type, type2, type3, type4, type5, val);
  3929. }
  3930. else if (current_equip_combo_pos > 0) {
  3931. 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);
  3932. }
  3933. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  3934. 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);
  3935. }
  3936. else {
  3937. ShowWarning("pc_bonus5: unknown bonus type %d %d %d %d %d %d in unknown usage. Report this!\n", type, type2, type3, type4, type5, val);
  3938. }
  3939. break;
  3940. }
  3941. }
  3942. /*==========================================
  3943. * Grants a player a given skill. Flag values are:
  3944. * 0 - Grant permanent skill to be bound to skill tree
  3945. * 1 - Grant an item skill (temporary)
  3946. * 2 - Like 1, except the level granted can stack with previously learned level.
  3947. * 4 - Like 0, except the skill will ignore skill tree (saves through job changes and resets).
  3948. *------------------------------------------*/
  3949. bool pc_skill(struct map_session_data* sd, uint16 skill_id, int level, enum e_addskill_type type) {
  3950. uint16 idx = 0;
  3951. nullpo_ret(sd);
  3952. if (!skill_id || !(idx = skill_get_index(skill_id))) {
  3953. ShowError("pc_skill: Skill with id %d does not exist in the skill database\n", skill_id);
  3954. return false;
  3955. }
  3956. if (level > MAX_SKILL_LEVEL) {
  3957. ShowError("pc_skill: Skill level %d too high. Max lv supported is %d\n", level, MAX_SKILL_LEVEL);
  3958. return false;
  3959. }
  3960. if (type == ADDSKILL_TEMP_ADDLEVEL && sd->status.skill[idx].lv + level > MAX_SKILL_LEVEL) {
  3961. 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);
  3962. level = MAX_SKILL_LEVEL - sd->status.skill[idx].lv;
  3963. }
  3964. switch (type) {
  3965. case ADDSKILL_PERMANENT: //Set skill data overwriting whatever was there before.
  3966. sd->status.skill[idx].id = skill_id;
  3967. sd->status.skill[idx].lv = level;
  3968. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  3969. if (level == 0) { //Remove skill.
  3970. sd->status.skill[idx].id = 0;
  3971. clif_deleteskill(sd,skill_id);
  3972. } else
  3973. clif_addskill(sd,skill_id);
  3974. if (!skill_get_inf(skill_id)) //Only recalculate for passive skills.
  3975. status_calc_pc(sd, SCO_NONE);
  3976. break;
  3977. case ADDSKILL_TEMP: //Item bonus skill.
  3978. if (sd->status.skill[idx].id != 0) {
  3979. if (sd->status.skill[idx].lv >= level)
  3980. return true;
  3981. if (sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT) //Non-granted skill, store it's level.
  3982. sd->status.skill[idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[idx].lv;
  3983. } else {
  3984. sd->status.skill[idx].id = skill_id;
  3985. sd->status.skill[idx].flag = SKILL_FLAG_TEMPORARY;
  3986. }
  3987. sd->status.skill[idx].lv = level;
  3988. break;
  3989. case ADDSKILL_TEMP_ADDLEVEL: //Add skill bonus on top of what you had.
  3990. if (sd->status.skill[idx].id != 0) {
  3991. if (sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT)
  3992. sd->status.skill[idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[idx].lv; // Store previous level.
  3993. } else {
  3994. sd->status.skill[idx].id = skill_id;
  3995. sd->status.skill[idx].flag = SKILL_FLAG_TEMPORARY; //Set that this is a bonus skill.
  3996. }
  3997. sd->status.skill[idx].lv += level;
  3998. break;
  3999. case ADDSKILL_PERMANENT_GRANTED: //Permanent granted skills ignore the skill tree
  4000. sd->status.skill[idx].id = skill_id;
  4001. sd->status.skill[idx].lv = level;
  4002. sd->status.skill[idx].flag = SKILL_FLAG_PERM_GRANTED;
  4003. if (level == 0) { //Remove skill.
  4004. sd->status.skill[idx].id = 0;
  4005. clif_deleteskill(sd,skill_id);
  4006. } else
  4007. clif_addskill(sd,skill_id);
  4008. if (!skill_get_inf(skill_id)) //Only recalculate for passive skills.
  4009. status_calc_pc(sd, SCO_NONE);
  4010. break;
  4011. default:
  4012. return false;
  4013. }
  4014. return true;
  4015. }
  4016. /*==========================================
  4017. * Append a card to an item ?
  4018. *------------------------------------------*/
  4019. int pc_insert_card(struct map_session_data* sd, int idx_card, int idx_equip)
  4020. {
  4021. int i;
  4022. unsigned short nameid;
  4023. nullpo_ret(sd);
  4024. if( idx_equip < 0 || idx_equip >= MAX_INVENTORY || sd->inventory_data[idx_equip] == NULL )
  4025. return 0; //Invalid item index.
  4026. if( idx_card < 0 || idx_card >= MAX_INVENTORY || sd->inventory_data[idx_card] == NULL )
  4027. return 0; //Invalid card index.
  4028. if( sd->inventory.u.items_inventory[idx_equip].nameid <= 0 || sd->inventory.u.items_inventory[idx_equip].amount < 1 )
  4029. return 0; // target item missing
  4030. if( sd->inventory.u.items_inventory[idx_card].nameid <= 0 || sd->inventory.u.items_inventory[idx_card].amount < 1 )
  4031. return 0; // target card missing
  4032. if( sd->inventory_data[idx_equip]->type != IT_WEAPON && sd->inventory_data[idx_equip]->type != IT_ARMOR )
  4033. return 0; // only weapons and armor are allowed
  4034. if( sd->inventory_data[idx_card]->type != IT_CARD )
  4035. return 0; // must be a card
  4036. if( sd->inventory.u.items_inventory[idx_equip].identify == 0 )
  4037. return 0; // target must be identified
  4038. if( itemdb_isspecial(sd->inventory.u.items_inventory[idx_equip].card[0]) )
  4039. return 0; // card slots reserved for other purposes
  4040. if( (sd->inventory_data[idx_equip]->equip & sd->inventory_data[idx_card]->equip) == 0 )
  4041. return 0; // card cannot be compounded on this item type
  4042. if( sd->inventory_data[idx_equip]->type == IT_WEAPON && sd->inventory_data[idx_card]->equip == EQP_SHIELD )
  4043. return 0; // attempted to place shield card on left-hand weapon.
  4044. if( sd->inventory.u.items_inventory[idx_equip].equip != 0 )
  4045. return 0; // item must be unequipped
  4046. ARR_FIND( 0, sd->inventory_data[idx_equip]->slot, i, sd->inventory.u.items_inventory[idx_equip].card[i] == 0 );
  4047. if( i == sd->inventory_data[idx_equip]->slot )
  4048. return 0; // no free slots
  4049. // remember the card id to insert
  4050. nameid = sd->inventory.u.items_inventory[idx_card].nameid;
  4051. if( pc_delitem(sd,idx_card,1,1,0,LOG_TYPE_OTHER) == 1 )
  4052. {// failed
  4053. clif_insert_card(sd,idx_equip,idx_card,1);
  4054. }
  4055. else
  4056. {// success
  4057. log_pick_pc(sd, LOG_TYPE_OTHER, -1, &sd->inventory.u.items_inventory[idx_equip]);
  4058. sd->inventory.u.items_inventory[idx_equip].card[i] = nameid;
  4059. log_pick_pc(sd, LOG_TYPE_OTHER, 1, &sd->inventory.u.items_inventory[idx_equip]);
  4060. clif_insert_card(sd,idx_equip,idx_card,0);
  4061. }
  4062. return 0;
  4063. }
  4064. /**
  4065. * Returns the count of unidentified items with the option to identify too.
  4066. * @param sd: Player data
  4067. * @param identify_item: Whether or not to identify any unidentified items
  4068. * @return Unidentified items count
  4069. */
  4070. int pc_identifyall(struct map_session_data *sd, bool identify_item)
  4071. {
  4072. int unidentified_count = 0;
  4073. for (int i = 0; i < MAX_INVENTORY; i++) {
  4074. if (sd->inventory.u.items_inventory[i].nameid > 0 && sd->inventory.u.items_inventory[i].identify != 1) {
  4075. if (identify_item == true) {
  4076. sd->inventory.u.items_inventory[i].identify = 1;
  4077. clif_item_identified(sd,i,0);
  4078. }
  4079. unidentified_count++;
  4080. }
  4081. }
  4082. return unidentified_count;
  4083. }
  4084. //
  4085. // Items
  4086. //
  4087. /*==========================================
  4088. * Update buying value by skills
  4089. *------------------------------------------*/
  4090. int pc_modifybuyvalue(struct map_session_data *sd,int orig_value)
  4091. {
  4092. int skill,val = orig_value,rate1 = 0,rate2 = 0;
  4093. if((skill=pc_checkskill(sd,MC_DISCOUNT))>0) // merchant discount
  4094. rate1 = 5+skill*2-((skill==10)? 1:0);
  4095. if((skill=pc_checkskill(sd,RG_COMPULSION))>0) // rogue discount
  4096. rate2 = 5+skill*4;
  4097. if(rate1 < rate2) rate1 = rate2;
  4098. if(rate1)
  4099. val = (int)((double)orig_value*(double)(100-rate1)/100.);
  4100. if(val < battle_config.min_shop_buy)
  4101. val = battle_config.min_shop_buy;
  4102. return val;
  4103. }
  4104. /*==========================================
  4105. * Update selling value by skills
  4106. *------------------------------------------*/
  4107. int pc_modifysellvalue(struct map_session_data *sd,int orig_value)
  4108. {
  4109. int skill,val = orig_value,rate = 0;
  4110. if((skill=pc_checkskill(sd,MC_OVERCHARGE))>0) //OverCharge
  4111. rate = 5+skill*2-((skill==10)? 1:0);
  4112. if(rate)
  4113. val = (int)((double)orig_value*(double)(100+rate)/100.);
  4114. if (val < battle_config.min_shop_sell)
  4115. val = battle_config.min_shop_sell;
  4116. return val;
  4117. }
  4118. /*==========================================
  4119. * Checking if we have enough place on inventory for new item
  4120. * Make sure to take 30k as limit (for client I guess)
  4121. * @param sd
  4122. * @param nameid
  4123. * @param amount
  4124. * @return e_chkitem_result
  4125. *------------------------------------------*/
  4126. char pc_checkadditem(struct map_session_data *sd, unsigned short nameid, int amount)
  4127. {
  4128. int i;
  4129. struct item_data* data;
  4130. nullpo_ret(sd);
  4131. if(amount > MAX_AMOUNT)
  4132. return CHKADDITEM_OVERAMOUNT;
  4133. data = itemdb_search(nameid);
  4134. if(!itemdb_isstackable2(data))
  4135. return CHKADDITEM_NEW;
  4136. if( data->stack.inventory && amount > data->stack.amount )
  4137. return CHKADDITEM_OVERAMOUNT;
  4138. if (data->flag.guid)
  4139. return CHKADDITEM_NEW;
  4140. for(i=0;i<MAX_INVENTORY;i++){
  4141. // FIXME: This does not consider the checked item's cards, thus could check a wrong slot for stackability.
  4142. if(sd->inventory.u.items_inventory[i].nameid == nameid){
  4143. 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 ) )
  4144. return CHKADDITEM_OVERAMOUNT;
  4145. return CHKADDITEM_EXIST;
  4146. }
  4147. }
  4148. return CHKADDITEM_NEW;
  4149. }
  4150. /*==========================================
  4151. * Return number of available place in inventory
  4152. * Each non stackable item will reduce place by 1
  4153. * @param sd
  4154. * @return Number of empty slots
  4155. *------------------------------------------*/
  4156. uint8 pc_inventoryblank(struct map_session_data *sd)
  4157. {
  4158. uint8 i, b;
  4159. nullpo_ret(sd);
  4160. for(i = 0, b = 0; i < MAX_INVENTORY; i++){
  4161. if(sd->inventory.u.items_inventory[i].nameid == 0)
  4162. b++;
  4163. }
  4164. return b;
  4165. }
  4166. /**
  4167. * Attempts to remove zeny from player
  4168. * @param sd: Player
  4169. * @param zeny: Zeny removed
  4170. * @param type: Log type
  4171. * @param tsd: (optional) From who to log (if null take sd)
  4172. * @return 0: Success, 1: Failed (Removing negative Zeny or not enough Zeny), 2: Player not found
  4173. */
  4174. char pc_payzeny(struct map_session_data *sd, int zeny, enum e_log_pick_type type, struct map_session_data *tsd)
  4175. {
  4176. nullpo_retr(2,sd);
  4177. zeny = cap_value(zeny,-MAX_ZENY,MAX_ZENY); //prevent command UB
  4178. if( zeny < 0 )
  4179. {
  4180. ShowError("pc_payzeny: Paying negative Zeny (zeny=%d, account_id=%d, char_id=%d).\n", zeny, sd->status.account_id, sd->status.char_id);
  4181. return 1;
  4182. }
  4183. if( sd->status.zeny < zeny )
  4184. return 1; //Not enough.
  4185. sd->status.zeny -= zeny;
  4186. clif_updatestatus(sd,SP_ZENY);
  4187. if(!tsd) tsd = sd;
  4188. log_zeny(sd, type, tsd, -zeny);
  4189. if( zeny > 0 && sd->state.showzeny ) {
  4190. char output[255];
  4191. sprintf(output, "Removed %dz.", zeny);
  4192. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4193. }
  4194. return 0;
  4195. }
  4196. /**
  4197. * Attempts to give zeny to player
  4198. * @param sd: Player
  4199. * @param type: Log type
  4200. * @param tsd: (optional) From who to log (if null take sd)
  4201. * @return -1: Player not found, 0: Success, 1: Giving negative Zeny
  4202. */
  4203. char pc_getzeny(struct map_session_data *sd, int zeny, enum e_log_pick_type type, struct map_session_data *tsd)
  4204. {
  4205. nullpo_retr(-1,sd);
  4206. zeny = cap_value(zeny,-MAX_ZENY,MAX_ZENY); //prevent command UB
  4207. if( zeny < 0 )
  4208. {
  4209. ShowError("pc_getzeny: Obtaining negative Zeny (zeny=%d, account_id=%d, char_id=%d).\n", zeny, sd->status.account_id, sd->status.char_id);
  4210. return 1;
  4211. }
  4212. if( zeny > MAX_ZENY - sd->status.zeny )
  4213. zeny = MAX_ZENY - sd->status.zeny;
  4214. sd->status.zeny += zeny;
  4215. clif_updatestatus(sd,SP_ZENY);
  4216. if(!tsd) tsd = sd;
  4217. log_zeny(sd, type, tsd, zeny);
  4218. if( zeny > 0 && sd->state.showzeny ) {
  4219. char output[255];
  4220. sprintf(output, "Gained %dz.", zeny);
  4221. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4222. }
  4223. achievement_update_objective(sd, AG_GET_ZENY, 1, sd->status.zeny);
  4224. return 0;
  4225. }
  4226. /**
  4227. * Attempts to remove Cash Points from player
  4228. * @param sd: Player
  4229. * @param price: Total points (cash + kafra) the player has to pay
  4230. * @param points: Kafra points the player has to pay
  4231. * @param type: Log type
  4232. * @return -1: Not enough points, otherwise success (cash+points)
  4233. */
  4234. int pc_paycash(struct map_session_data *sd, int price, int points, e_log_pick_type type)
  4235. {
  4236. int cash;
  4237. nullpo_retr(-1,sd);
  4238. points = cap_value(points, 0, MAX_ZENY); //prevent command UB
  4239. cash = price-points;
  4240. if( sd->cashPoints < cash || sd->kafraPoints < points )
  4241. {
  4242. 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);
  4243. return -1;
  4244. }
  4245. if( cash ){
  4246. pc_setaccountreg(sd, add_str(CASHPOINT_VAR), sd->cashPoints - cash);
  4247. sd->cashPoints -= cash;
  4248. log_cash( sd, type, LOG_CASH_TYPE_CASH, -cash );
  4249. }
  4250. if( points ){
  4251. pc_setaccountreg(sd, add_str(KAFRAPOINT_VAR), sd->kafraPoints - points);
  4252. sd->kafraPoints -= points;
  4253. log_cash( sd, type, LOG_CASH_TYPE_KAFRA, -points );
  4254. }
  4255. if( battle_config.cashshop_show_points )
  4256. {
  4257. char output[CHAT_SIZE_MAX];
  4258. 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.
  4259. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4260. }
  4261. return cash+points;
  4262. }
  4263. /**
  4264. * Attempts to give Cash Points to player
  4265. * @param sd: Player
  4266. * @param cash: Cash points the player gets
  4267. * @param points: Kafra points the player gets
  4268. * @param type: Log type
  4269. * @return -1: Error, otherwise success (cash or points)
  4270. */
  4271. int pc_getcash(struct map_session_data *sd, int cash, int points, e_log_pick_type type)
  4272. {
  4273. char output[CHAT_SIZE_MAX];
  4274. nullpo_retr(-1,sd);
  4275. cash = cap_value(cash, 0, MAX_ZENY); //prevent command UB
  4276. points = cap_value(points, 0, MAX_ZENY); //prevent command UB
  4277. if( cash > 0 )
  4278. {
  4279. if( cash > MAX_ZENY-sd->cashPoints )
  4280. {
  4281. 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);
  4282. cash = MAX_ZENY-sd->cashPoints;
  4283. }
  4284. pc_setaccountreg(sd, add_str(CASHPOINT_VAR), sd->cashPoints+cash);
  4285. sd->cashPoints += cash;
  4286. log_cash( sd, type, LOG_CASH_TYPE_CASH, cash );
  4287. if( battle_config.cashshop_show_points )
  4288. {
  4289. sprintf(output, msg_txt(sd,505), cash, sd->cashPoints);
  4290. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4291. }
  4292. return cash;
  4293. }
  4294. if( points > 0 )
  4295. {
  4296. if( points > MAX_ZENY-sd->kafraPoints )
  4297. {
  4298. 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);
  4299. points = MAX_ZENY-sd->kafraPoints;
  4300. }
  4301. pc_setaccountreg(sd, add_str(KAFRAPOINT_VAR), sd->kafraPoints+points);
  4302. sd->kafraPoints += points;
  4303. log_cash( sd, type, LOG_CASH_TYPE_KAFRA, points );
  4304. if( battle_config.cashshop_show_points )
  4305. {
  4306. sprintf(output, msg_txt(sd,506), points, sd->kafraPoints);
  4307. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4308. }
  4309. return points;
  4310. }
  4311. return -1; //shouldn't happen but just in case
  4312. }
  4313. /**
  4314. * Searching a specified itemid in inventory and return his stored index
  4315. * @param sd Player
  4316. * @param nameid Find this Item!
  4317. * @return Stored index in inventory, or -1 if not found.
  4318. **/
  4319. short pc_search_inventory(struct map_session_data *sd, unsigned short nameid) {
  4320. short i;
  4321. nullpo_retr(-1, sd);
  4322. ARR_FIND( 0, MAX_INVENTORY, i, sd->inventory.u.items_inventory[i].nameid == nameid && (sd->inventory.u.items_inventory[i].amount > 0 || nameid == 0) );
  4323. return ( i < MAX_INVENTORY ) ? i : -1;
  4324. }
  4325. /** Attempt to add a new item to player inventory
  4326. * @param sd
  4327. * @param item_data
  4328. * @param amount
  4329. * @param log_type
  4330. * @return
  4331. * 0 = success
  4332. * 1 = invalid itemid not found or negative amount
  4333. * 2 = overweight
  4334. * 3 = ?
  4335. * 4 = no free place found
  4336. * 5 = max amount reached
  4337. * 6 = ?
  4338. * 7 = stack limitation
  4339. */
  4340. enum e_additem_result pc_additem(struct map_session_data *sd,struct item *item,int amount,e_log_pick_type log_type) {
  4341. struct item_data *id;
  4342. int16 i;
  4343. unsigned int w;
  4344. nullpo_retr(ADDITEM_INVALID, sd);
  4345. nullpo_retr(ADDITEM_INVALID, item);
  4346. if( item->nameid == 0 || amount <= 0 )
  4347. return ADDITEM_INVALID;
  4348. if( amount > MAX_AMOUNT )
  4349. return ADDITEM_OVERAMOUNT;
  4350. id = itemdb_search(item->nameid);
  4351. if( id->stack.inventory && amount > id->stack.amount )
  4352. {// item stack limitation
  4353. return ADDITEM_STACKLIMIT;
  4354. }
  4355. w = id->weight*amount;
  4356. if(sd->weight + w > sd->max_weight)
  4357. return ADDITEM_OVERWEIGHT;
  4358. i = MAX_INVENTORY;
  4359. if (id->flag.guid && !item->unique_id)
  4360. item->unique_id = pc_generate_unique_id(sd);
  4361. // Stackable | Non Rental
  4362. if( itemdb_isstackable2(id) && item->expire_time == 0 ) {
  4363. for( i = 0; i < MAX_INVENTORY; i++ ) {
  4364. if( sd->inventory.u.items_inventory[i].nameid == item->nameid &&
  4365. sd->inventory.u.items_inventory[i].bound == item->bound &&
  4366. sd->inventory.u.items_inventory[i].expire_time == 0 &&
  4367. sd->inventory.u.items_inventory[i].unique_id == item->unique_id &&
  4368. memcmp(&sd->inventory.u.items_inventory[i].card, &item->card, sizeof(item->card)) == 0 ) {
  4369. 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 ) )
  4370. return ADDITEM_OVERAMOUNT;
  4371. sd->inventory.u.items_inventory[i].amount += amount;
  4372. clif_additem(sd,i,amount,0);
  4373. break;
  4374. }
  4375. }
  4376. }
  4377. if (i >= MAX_INVENTORY) {
  4378. i = pc_search_inventory(sd,0);
  4379. if( i < 0 )
  4380. return ADDITEM_OVERITEM;
  4381. memcpy(&sd->inventory.u.items_inventory[i], item, sizeof(sd->inventory.u.items_inventory[0]));
  4382. // clear equip and favorite fields first, just in case
  4383. if( item->equip )
  4384. sd->inventory.u.items_inventory[i].equip = 0;
  4385. if( item->favorite )
  4386. sd->inventory.u.items_inventory[i].favorite = 0;
  4387. if( item->equipSwitch )
  4388. sd->inventory.u.items_inventory[i].equipSwitch = 0;
  4389. sd->inventory.u.items_inventory[i].amount = amount;
  4390. sd->inventory_data[i] = id;
  4391. sd->last_addeditem_index = i;
  4392. if (!itemdb_isstackable2(id) || id->flag.guid)
  4393. sd->inventory.u.items_inventory[i].unique_id = item->unique_id ? item->unique_id : pc_generate_unique_id(sd);
  4394. clif_additem(sd,i,amount,0);
  4395. }
  4396. log_pick_pc(sd, log_type, amount, &sd->inventory.u.items_inventory[i]);
  4397. sd->weight += w;
  4398. clif_updatestatus(sd,SP_WEIGHT);
  4399. //Auto-equip
  4400. if(id->flag.autoequip)
  4401. pc_equipitem(sd, i, id->equip);
  4402. /* rental item check */
  4403. if( item->expire_time ) {
  4404. if( time(NULL) > item->expire_time ) {
  4405. clif_rental_expired(sd->fd, i, sd->inventory.u.items_inventory[i].nameid);
  4406. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 1, 0, LOG_TYPE_OTHER);
  4407. } else {
  4408. unsigned int seconds = (unsigned int)( item->expire_time - time(NULL) );
  4409. clif_rental_time(sd->fd, sd->inventory.u.items_inventory[i].nameid, seconds);
  4410. pc_inventory_rental_add(sd, seconds);
  4411. }
  4412. }
  4413. achievement_update_objective(sd, AG_GET_ITEM, 1, id->value_sell);
  4414. pc_show_questinfo(sd);
  4415. return ADDITEM_SUCCESS;
  4416. }
  4417. /*==========================================
  4418. * Remove an item at index n from inventory by amount.
  4419. * @param sd
  4420. * @param n Item index in inventory
  4421. * @param amount
  4422. * @param type &1: Don't notify deletion; &2 Don't notify weight change; &4 Don't calculate status
  4423. * @param reason Delete reason
  4424. * @param log_type e_log_pick_type
  4425. * @return 1 - invalid itemid or negative amount; 0 - Success
  4426. *------------------------------------------*/
  4427. char pc_delitem(struct map_session_data *sd,int n,int amount,int type, short reason, e_log_pick_type log_type)
  4428. {
  4429. nullpo_retr(1, sd);
  4430. 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)
  4431. return 1;
  4432. log_pick_pc(sd, log_type, -amount, &sd->inventory.u.items_inventory[n]);
  4433. sd->inventory.u.items_inventory[n].amount -= amount;
  4434. sd->weight -= sd->inventory_data[n]->weight*amount ;
  4435. if( sd->inventory.u.items_inventory[n].amount <= 0 ){
  4436. if(sd->inventory.u.items_inventory[n].equip)
  4437. pc_unequipitem(sd,n,2|(!(type&4) ? 1 : 0));
  4438. memset(&sd->inventory.u.items_inventory[n],0,sizeof(sd->inventory.u.items_inventory[0]));
  4439. sd->inventory_data[n] = NULL;
  4440. }
  4441. if(!(type&1))
  4442. clif_delitem(sd,n,amount,reason);
  4443. if(!(type&2))
  4444. clif_updatestatus(sd,SP_WEIGHT);
  4445. pc_show_questinfo(sd);
  4446. return 0;
  4447. }
  4448. /*==========================================
  4449. * Attempt to drop an item.
  4450. * @param sd
  4451. * @param n Item index in inventory
  4452. * @param amount Amount of item
  4453. * @return False = fail; True = success
  4454. *------------------------------------------*/
  4455. bool pc_dropitem(struct map_session_data *sd,int n,int amount)
  4456. {
  4457. nullpo_retr(1, sd);
  4458. if(n < 0 || n >= MAX_INVENTORY)
  4459. return false;
  4460. if(amount <= 0)
  4461. return false;
  4462. if(sd->inventory.u.items_inventory[n].nameid <= 0 ||
  4463. sd->inventory.u.items_inventory[n].amount <= 0 ||
  4464. sd->inventory.u.items_inventory[n].amount < amount ||
  4465. sd->state.trading || sd->state.vending ||
  4466. !sd->inventory_data[n] //pc_delitem would fail on this case.
  4467. )
  4468. return false;
  4469. if( sd->inventory.u.items_inventory[n].equipSwitch )
  4470. return false;
  4471. if( map_getmapflag(sd->bl.m, MF_NODROP) )
  4472. {
  4473. clif_displaymessage (sd->fd, msg_txt(sd,271));
  4474. return false; //Can't drop items in nodrop mapflag maps.
  4475. }
  4476. if( !pc_candrop(sd,&sd->inventory.u.items_inventory[n]) )
  4477. {
  4478. clif_displaymessage (sd->fd, msg_txt(sd,263));
  4479. return false;
  4480. }
  4481. if (!map_addflooritem(&sd->inventory.u.items_inventory[n], amount, sd->bl.m, sd->bl.x, sd->bl.y, 0, 0, 0, 2, 0))
  4482. return false;
  4483. pc_delitem(sd, n, amount, 1, 0, LOG_TYPE_PICKDROP_PLAYER);
  4484. clif_dropitem(sd, n, amount);
  4485. return true;
  4486. }
  4487. /*==========================================
  4488. * Attempt to pick up an item.
  4489. * @param sd
  4490. * @param fitem Item that will be picked
  4491. * @return False = fail; True = success
  4492. *------------------------------------------*/
  4493. bool pc_takeitem(struct map_session_data *sd,struct flooritem_data *fitem)
  4494. {
  4495. int flag = 0;
  4496. t_tick tick = gettick();
  4497. struct party_data *p = NULL;
  4498. nullpo_ret(sd);
  4499. nullpo_ret(fitem);
  4500. if (!check_distance_bl(&fitem->bl, &sd->bl, 2) && sd->ud.skill_id!=BS_GREED)
  4501. return false; // Distance is too far
  4502. if (sd->sc.cant.pickup)
  4503. return false;
  4504. if (sd->status.party_id)
  4505. p = party_search(sd->status.party_id);
  4506. if (fitem->first_get_charid > 0 && fitem->first_get_charid != sd->status.char_id) {
  4507. struct map_session_data *first_sd = map_charid2sd(fitem->first_get_charid);
  4508. if (DIFF_TICK(tick,fitem->first_get_tick) < 0) {
  4509. if (!(p && p->party.item&1 &&
  4510. first_sd && first_sd->status.party_id == sd->status.party_id
  4511. ))
  4512. return false;
  4513. }
  4514. else if (fitem->second_get_charid > 0 && fitem->second_get_charid != sd->status.char_id) {
  4515. struct map_session_data *second_sd = map_charid2sd(fitem->second_get_charid);
  4516. if (DIFF_TICK(tick, fitem->second_get_tick) < 0) {
  4517. if (!(p && p->party.item&1 &&
  4518. ((first_sd && first_sd->status.party_id == sd->status.party_id) ||
  4519. (second_sd && second_sd->status.party_id == sd->status.party_id))
  4520. ))
  4521. return false;
  4522. }
  4523. else if (fitem->third_get_charid > 0 && fitem->third_get_charid != sd->status.char_id){
  4524. struct map_session_data *third_sd = map_charid2sd(fitem->third_get_charid);
  4525. if (DIFF_TICK(tick,fitem->third_get_tick) < 0) {
  4526. if(!(p && p->party.item&1 &&
  4527. ((first_sd && first_sd->status.party_id == sd->status.party_id) ||
  4528. (second_sd && second_sd->status.party_id == sd->status.party_id) ||
  4529. (third_sd && third_sd->status.party_id == sd->status.party_id))
  4530. ))
  4531. return false;
  4532. }
  4533. }
  4534. }
  4535. }
  4536. //This function takes care of giving the item to whoever should have it, considering party-share options.
  4537. if ((flag = party_share_loot(p,sd,&fitem->item, fitem->first_get_charid))) {
  4538. clif_additem(sd,0,0,flag);
  4539. return true;
  4540. }
  4541. //Display pickup animation.
  4542. pc_stop_attack(sd);
  4543. clif_takeitem(&sd->bl,&fitem->bl);
  4544. if (fitem->mob_id &&
  4545. (itemdb_search(fitem->item.nameid))->flag.broadcast &&
  4546. (!p || !(p->party.item&2)) // Somehow, if party's pickup distribution is 'Even Share', no announcemet
  4547. )
  4548. intif_broadcast_obtain_special_item(sd, fitem->item.nameid, fitem->mob_id, ITEMOBTAIN_TYPE_MONSTER_ITEM);
  4549. map_clearflooritem(&fitem->bl);
  4550. return true;
  4551. }
  4552. /*==========================================
  4553. * Check if item is usable.
  4554. * Return:
  4555. * false = no
  4556. * true = yes
  4557. *------------------------------------------*/
  4558. bool pc_isUseitem(struct map_session_data *sd,int n)
  4559. {
  4560. struct item_data *item;
  4561. unsigned short nameid;
  4562. nullpo_ret(sd);
  4563. item = sd->inventory_data[n];
  4564. nameid = sd->inventory.u.items_inventory[n].nameid;
  4565. if( item == NULL )
  4566. return false;
  4567. //Not consumable item
  4568. if( item->type != IT_HEALING && item->type != IT_USABLE && item->type != IT_CASH )
  4569. return false;
  4570. if (pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL))
  4571. return true;
  4572. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  4573. if(mapdata->flag[MF_NOITEMCONSUMPTION]) //consumable but mapflag prevent it
  4574. return false;
  4575. //Prevent mass item usage. [Skotlex]
  4576. if( DIFF_TICK(sd->canuseitem_tick,gettick()) > 0 ||
  4577. (itemdb_group_item_exists(IG_CASH_FOOD, nameid) && DIFF_TICK(sd->canusecashfood_tick,gettick()) > 0)
  4578. )
  4579. return false;
  4580. if( (item->item_usage.flag&NOUSE_SITTING) && (pc_issit(sd) == 1) && (pc_get_group_level(sd) < item->item_usage.override) ) {
  4581. clif_msg(sd,ITEM_NOUSE_SITTING);
  4582. return false; // You cannot use this item while sitting.
  4583. }
  4584. if (sd->state.storage_flag && item->type != IT_CASH) {
  4585. clif_messagecolor(&sd->bl, color_table[COLOR_RED], msg_txt(sd,388), false, SELF);
  4586. return false; // You cannot use this item while storage is open.
  4587. }
  4588. if (item->flag.dead_branch && (mapdata->flag[MF_NOBRANCH] || mapdata_flag_gvg2(mapdata)))
  4589. return false;
  4590. switch( nameid ) {
  4591. case ITEMID_WING_OF_FLY:
  4592. case ITEMID_GIANT_FLY_WING:
  4593. case ITEMID_N_FLY_WING:
  4594. if( mapdata->flag[MF_NOTELEPORT] || mapdata_flag_gvg2(mapdata) ) {
  4595. clif_skill_teleportmessage(sd,0);
  4596. return false;
  4597. }
  4598. if (nameid == ITEMID_GIANT_FLY_WING) {
  4599. struct party_data *pd = party_search(sd->status.party_id);
  4600. if (pd) {
  4601. int i;
  4602. ARR_FIND(0, MAX_PARTY, i, pd->data[i].sd == sd && pd->party.member[i].leader);
  4603. if (i == MAX_PARTY) { // User is not party leader
  4604. clif_msg(sd, ITEM_PARTY_MEMBER_NOT_SUMMONED);
  4605. break;
  4606. }
  4607. 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));
  4608. if (i == MAX_PARTY) { // No party members found on same map
  4609. clif_msg(sd, ITEM_PARTY_NO_MEMBER_IN_MAP);
  4610. break;
  4611. }
  4612. } else {
  4613. clif_msg(sd, ITEM_PARTY_MEMBER_NOT_SUMMONED);
  4614. break;
  4615. }
  4616. }
  4617. // Fall through
  4618. case ITEMID_WING_OF_BUTTERFLY:
  4619. case ITEMID_N_BUTTERFLY_WING:
  4620. case ITEMID_DUN_TELE_SCROLL1:
  4621. case ITEMID_DUN_TELE_SCROLL2:
  4622. case ITEMID_DUN_TELE_SCROLL3:
  4623. case ITEMID_WOB_RUNE:
  4624. case ITEMID_WOB_SCHWALTZ:
  4625. case ITEMID_WOB_RACHEL:
  4626. case ITEMID_WOB_LOCAL:
  4627. case ITEMID_SIEGE_TELEPORT_SCROLL:
  4628. if( sd->duel_group && !battle_config.duel_allow_teleport ) {
  4629. clif_displaymessage(sd->fd, msg_txt(sd,663));
  4630. return false;
  4631. }
  4632. if( mapdata->flag[MF_NORETURN] && nameid != ITEMID_WING_OF_FLY && nameid != ITEMID_GIANT_FLY_WING && nameid != ITEMID_N_FLY_WING )
  4633. return false;
  4634. break;
  4635. case ITEMID_MERCENARY_RED_POTION:
  4636. case ITEMID_MERCENARY_BLUE_POTION:
  4637. case ITEMID_M_CENTER_POTION:
  4638. case ITEMID_M_AWAKENING_POTION:
  4639. case ITEMID_M_BERSERK_POTION:
  4640. if( sd->md == NULL || sd->md->db == NULL )
  4641. return false;
  4642. if( sd->md->sc.data[SC_BERSERK] )
  4643. return false;
  4644. if( nameid == ITEMID_M_AWAKENING_POTION && sd->md->db->lv < 40 )
  4645. return false;
  4646. if( nameid == ITEMID_M_BERSERK_POTION && sd->md->db->lv < 80 )
  4647. return false;
  4648. break;
  4649. case ITEMID_NEURALIZER:
  4650. if( !mapdata->flag[MF_RESET] )
  4651. return false;
  4652. break;
  4653. }
  4654. if( itemdb_group_item_exists(IG_MERCENARY, nameid) && sd->md != NULL )
  4655. return false; // Mercenary Scrolls
  4656. if( item->flag.group || item->type == IT_CASH) { //safe check type cash disappear when overweight [Napster]
  4657. if( pc_is90overweight(sd) ) {
  4658. clif_msg(sd, ITEM_CANT_OBTAIN_WEIGHT);
  4659. return false;
  4660. }
  4661. if( !pc_inventoryblank(sd) ) {
  4662. clif_messagecolor(&sd->bl, color_table[COLOR_RED], msg_txt(sd, 732), false, SELF); //Item cannot be open when inventory is full
  4663. return false;
  4664. }
  4665. }
  4666. //Gender check
  4667. if(item->sex != 2 && sd->status.sex != item->sex)
  4668. return false;
  4669. //Required level check
  4670. if(item->elv && sd->status.base_level < (unsigned int)item->elv)
  4671. return false;
  4672. if(item->elvmax && sd->status.base_level > (unsigned int)item->elvmax)
  4673. return false;
  4674. //Not equipable by class. [Skotlex]
  4675. if (!(
  4676. (1ULL<<(sd->class_&MAPID_BASEMASK)) &
  4677. (item->class_base[sd->class_&JOBL_2_1?1:(sd->class_&JOBL_2_2?2:0)])
  4678. ))
  4679. return false;
  4680. if (sd->sc.count && (
  4681. sd->sc.data[SC_BERSERK] || sd->sc.data[SC_SATURDAYNIGHTFEVER] ||
  4682. (sd->sc.data[SC_GRAVITATION] && sd->sc.data[SC_GRAVITATION]->val3 == BCT_SELF) ||
  4683. sd->sc.data[SC_TRICKDEAD] ||
  4684. sd->sc.data[SC_HIDING] ||
  4685. sd->sc.data[SC__SHADOWFORM] ||
  4686. sd->sc.data[SC__INVISIBILITY] ||
  4687. sd->sc.data[SC__MANHOLE] ||
  4688. sd->sc.data[SC_DEEPSLEEP] ||
  4689. sd->sc.data[SC_CRYSTALIZE] ||
  4690. sd->sc.data[SC_KAGEHUMI] ||
  4691. (sd->sc.data[SC_NOCHAT] && sd->sc.data[SC_NOCHAT]->val1&MANNER_NOITEM) ||
  4692. sd->sc.data[SC_KINGS_GRACE] ||
  4693. sd->sc.data[SC_SUHIDE]))
  4694. return false;
  4695. if (!pc_isItemClass(sd,item))
  4696. return false;
  4697. //Dead Branch items
  4698. if( item->flag.dead_branch )
  4699. log_branch(sd);
  4700. return true;
  4701. }
  4702. /*==========================================
  4703. * Last checks to use an item.
  4704. * Return:
  4705. * 0 = fail
  4706. * 1 = success
  4707. *------------------------------------------*/
  4708. int pc_useitem(struct map_session_data *sd,int n)
  4709. {
  4710. t_tick tick = gettick();
  4711. int amount;
  4712. unsigned short nameid;
  4713. struct script_code *script;
  4714. struct item item;
  4715. struct item_data *id;
  4716. nullpo_ret(sd);
  4717. if (sd->npc_id) {
  4718. if (sd->progressbar.npc_id) {
  4719. clif_progressbar_abort(sd);
  4720. return 0; // First item use attempt cancels the progress bar
  4721. }
  4722. if( pc_hasprogress( sd, WIP_DISABLE_SKILLITEM ) || !sd->npc_item_flag ){
  4723. #ifdef RENEWAL
  4724. clif_msg( sd, WORK_IN_PROGRESS );
  4725. #endif
  4726. return 0;
  4727. }
  4728. }
  4729. item = sd->inventory.u.items_inventory[n];
  4730. id = sd->inventory_data[n];
  4731. if (item.nameid == 0 || item.amount <= 0)
  4732. return 0;
  4733. if( !pc_isUseitem(sd,n) )
  4734. return 0;
  4735. // Store information for later use before it is lost (via pc_delitem) [Paradox924X]
  4736. nameid = id->nameid;
  4737. if (nameid != ITEMID_NAUTHIZ && sd->sc.opt1 > 0 && sd->sc.opt1 != OPT1_STONEWAIT && sd->sc.opt1 != OPT1_BURNING)
  4738. return 0;
  4739. /* Items with delayed consume are not meant to work while in mounts except reins of mount(12622) */
  4740. if( id->flag.delay_consume ) {
  4741. if( nameid != ITEMID_REINS_OF_MOUNT && sd->sc.data[SC_ALL_RIDING] )
  4742. return 0;
  4743. else if( pc_issit(sd) )
  4744. return 0;
  4745. }
  4746. //Since most delay-consume items involve using a "skill-type" target cursor,
  4747. //perform a skill-use check before going through. [Skotlex]
  4748. //resurrection was picked as testing skill, as a non-offensive, generic skill, it will do.
  4749. //FIXME: Is this really needed here? It'll be checked in unit.cpp after all and this prevents skill items using when silenced [Inkfish]
  4750. if( id->flag.delay_consume && ( sd->ud.skilltimer != INVALID_TIMER /*|| !status_check_skilluse(&sd->bl, &sd->bl, ALL_RESURRECTION, 0)*/ ) )
  4751. return 0;
  4752. if( id->delay > 0 && !pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL) && pc_itemcd_check(sd, id, tick, n))
  4753. return 0;
  4754. /* on restricted maps the item is consumed but the effect is not used */
  4755. if (!pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL) && itemdb_isNoEquip(id,sd->bl.m)) {
  4756. clif_msg(sd,ITEM_CANT_USE_AREA); // This item cannot be used within this area
  4757. if( battle_config.allow_consume_restricted_item && !id->flag.delay_consume ) { //need confirmation for delayed consumption items
  4758. clif_useitemack(sd,n,item.amount-1,true);
  4759. pc_delitem(sd,n,1,1,0,LOG_TYPE_CONSUME);
  4760. }
  4761. return 0;/* regardless, effect is not run */
  4762. }
  4763. sd->itemid = item.nameid;
  4764. sd->itemindex = n;
  4765. if(sd->catch_target_class != PET_CATCH_FAIL) //Abort pet catching.
  4766. sd->catch_target_class = PET_CATCH_FAIL;
  4767. amount = item.amount;
  4768. script = id->script;
  4769. //Check if the item is to be consumed immediately [Skotlex]
  4770. if (id->flag.delay_consume)
  4771. clif_useitemack(sd, n, amount, true);
  4772. else
  4773. {
  4774. if( item.expire_time == 0 && nameid != ITEMID_REINS_OF_MOUNT )
  4775. {
  4776. clif_useitemack(sd, n, amount - 1, true);
  4777. pc_delitem(sd, n, 1, 1, 0, LOG_TYPE_CONSUME); // Rental Usable Items are not deleted until expiration
  4778. }
  4779. else
  4780. clif_useitemack(sd, n, 0, false);
  4781. }
  4782. if (item.card[0]==CARD0_CREATE && pc_famerank(MakeDWord(item.card[2],item.card[3]), MAPID_ALCHEMIST))
  4783. potion_flag = 2; // Famous player's potions have 50% more efficiency
  4784. //Update item use time.
  4785. sd->canuseitem_tick = tick + battle_config.item_use_interval;
  4786. if( itemdb_group_item_exists(IG_CASH_FOOD, nameid) )
  4787. sd->canusecashfood_tick = tick + battle_config.cashfood_use_interval;
  4788. run_script(script,0,sd->bl.id,fake_nd->bl.id);
  4789. potion_flag = 0;
  4790. return 1;
  4791. }
  4792. /**
  4793. * Add item on cart for given index.
  4794. * @param sd
  4795. * @param item
  4796. * @param amount
  4797. * @param log_type
  4798. * @return See pc.hpp::e_additem_result
  4799. */
  4800. enum e_additem_result pc_cart_additem(struct map_session_data *sd,struct item *item,int amount,e_log_pick_type log_type)
  4801. {
  4802. struct item_data *data;
  4803. int i,w;
  4804. nullpo_retr(ADDITEM_INVALID, sd);
  4805. nullpo_retr(ADDITEM_INVALID, item);
  4806. if(item->nameid == 0 || amount <= 0)
  4807. return ADDITEM_INVALID;
  4808. if (itemdb_ishatched_egg(item))
  4809. return ADDITEM_INVALID;
  4810. data = itemdb_search(item->nameid);
  4811. if( data->stack.cart && amount > data->stack.amount )
  4812. {// item stack limitation
  4813. return ADDITEM_STACKLIMIT;
  4814. }
  4815. if( !itemdb_cancartstore(item, pc_get_group_level(sd)) || (item->bound > BOUND_ACCOUNT && !pc_can_give_bounded_items(sd)))
  4816. { // Check item trade restrictions [Skotlex]
  4817. clif_displaymessage (sd->fd, msg_txt(sd,264));
  4818. return ADDITEM_INVALID;
  4819. }
  4820. if( (w = data->weight*amount) + sd->cart_weight > sd->cart_weight_max )
  4821. return ADDITEM_OVERWEIGHT;
  4822. i = MAX_CART;
  4823. if( itemdb_isstackable2(data) && !item->expire_time )
  4824. {
  4825. for (i = 0; i < MAX_CART; i++) {
  4826. if (sd->cart.u.items_cart[i].nameid == item->nameid
  4827. && sd->cart.u.items_cart[i].bound == item->bound
  4828. && sd->cart.u.items_cart[i].unique_id == item->unique_id
  4829. && memcmp(sd->cart.u.items_cart[i].card, item->card, sizeof(item->card)) == 0
  4830. )
  4831. break;
  4832. }
  4833. }
  4834. if( i < MAX_CART )
  4835. {// item already in cart, stack it
  4836. 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 ) )
  4837. return ADDITEM_OVERAMOUNT; // no slot
  4838. sd->cart.u.items_cart[i].amount += amount;
  4839. clif_cart_additem(sd,i,amount,0);
  4840. }
  4841. else
  4842. {// item not stackable or not present, add it
  4843. ARR_FIND( 0, MAX_CART, i, sd->cart.u.items_cart[i].nameid == 0 );
  4844. if( i == MAX_CART )
  4845. return ADDITEM_OVERAMOUNT; // no slot
  4846. memcpy(&sd->cart.u.items_cart[i],item,sizeof(sd->cart.u.items_cart[0]));
  4847. sd->cart.u.items_cart[i].id = 0;
  4848. sd->cart.u.items_cart[i].amount = amount;
  4849. sd->cart_num++;
  4850. clif_cart_additem(sd,i,amount,0);
  4851. }
  4852. sd->cart.u.items_cart[i].favorite = 0; // clear
  4853. sd->cart.u.items_cart[i].equipSwitch = 0;
  4854. log_pick_pc(sd, log_type, amount, &sd->cart.u.items_cart[i]);
  4855. sd->cart_weight += w;
  4856. clif_updatestatus(sd,SP_CARTINFO);
  4857. return ADDITEM_SUCCESS;
  4858. }
  4859. /*==========================================
  4860. * Delete item on cart for given index.
  4861. *------------------------------------------*/
  4862. void pc_cart_delitem(struct map_session_data *sd,int n,int amount,int type,e_log_pick_type log_type)
  4863. {
  4864. nullpo_retv(sd);
  4865. if(sd->cart.u.items_cart[n].nameid == 0 ||
  4866. sd->cart.u.items_cart[n].amount < amount)
  4867. return;
  4868. log_pick_pc(sd, log_type, -amount, &sd->cart.u.items_cart[n]);
  4869. sd->cart.u.items_cart[n].amount -= amount;
  4870. sd->cart_weight -= itemdb_weight(sd->cart.u.items_cart[n].nameid) * amount;
  4871. if(sd->cart.u.items_cart[n].amount <= 0) {
  4872. memset(&sd->cart.u.items_cart[n],0,sizeof(sd->cart.u.items_cart[0]));
  4873. sd->cart_num--;
  4874. }
  4875. if(!type) {
  4876. clif_cart_delitem(sd,n,amount);
  4877. clif_updatestatus(sd,SP_CARTINFO);
  4878. }
  4879. }
  4880. /*==========================================
  4881. * Transfer item from inventory to cart.
  4882. *------------------------------------------*/
  4883. void pc_putitemtocart(struct map_session_data *sd,int idx,int amount)
  4884. {
  4885. nullpo_retv(sd);
  4886. if (idx < 0 || idx >= MAX_INVENTORY) //Invalid index check [Skotlex]
  4887. return;
  4888. struct item *item_data = &sd->inventory.u.items_inventory[idx];
  4889. if( item_data->nameid == 0 || amount < 1 || item_data->amount < amount || sd->state.vending || sd->state.prevend )
  4890. return;
  4891. if( item_data->equipSwitch ){
  4892. clif_msg( sd, C_ITEM_EQUIP_SWITCH );
  4893. return;
  4894. }
  4895. enum e_additem_result flag = pc_cart_additem(sd,item_data,amount,LOG_TYPE_NONE);
  4896. if (flag == ADDITEM_SUCCESS)
  4897. pc_delitem(sd,idx,amount,0,5,LOG_TYPE_NONE);
  4898. else {
  4899. clif_cart_additem_ack(sd, (flag == ADDITEM_OVERAMOUNT) ? ADDITEM_TO_CART_FAIL_COUNT : ADDITEM_TO_CART_FAIL_WEIGHT);
  4900. clif_additem(sd, idx, amount, 0);
  4901. clif_delitem(sd, idx, amount, 0);
  4902. }
  4903. }
  4904. /*==========================================
  4905. * Get number of item in cart.
  4906. * Return:
  4907. -1 = itemid not found or no amount found
  4908. x = remaining itemid on cart after get
  4909. *------------------------------------------*/
  4910. int pc_cartitem_amount(struct map_session_data* sd, int idx, int amount)
  4911. {
  4912. struct item* item_data;
  4913. nullpo_retr(-1, sd);
  4914. item_data = &sd->cart.u.items_cart[idx];
  4915. if( item_data->nameid == 0 || item_data->amount == 0 )
  4916. return -1;
  4917. return item_data->amount - amount;
  4918. }
  4919. /*==========================================
  4920. * Retrieve an item at index idx from cart.
  4921. *------------------------------------------*/
  4922. void pc_getitemfromcart(struct map_session_data *sd,int idx,int amount)
  4923. {
  4924. nullpo_retv(sd);
  4925. if (idx < 0 || idx >= MAX_CART) //Invalid index check [Skotlex]
  4926. return;
  4927. struct item *item_data=&sd->cart.u.items_cart[idx];
  4928. if (item_data->nameid == 0 || amount < 1 || item_data->amount < amount || sd->state.vending || sd->state.prevend)
  4929. return;
  4930. enum e_additem_result flag = pc_additem(sd, item_data, amount, LOG_TYPE_NONE);
  4931. if (flag == ADDITEM_SUCCESS)
  4932. pc_cart_delitem(sd, idx, amount, 0, LOG_TYPE_NONE);
  4933. else {
  4934. clif_cart_delitem(sd, idx, amount);
  4935. clif_additem(sd, idx, amount, flag);
  4936. clif_cart_additem(sd, idx, amount, 0);
  4937. }
  4938. }
  4939. /*==========================================
  4940. * Bound Item Check
  4941. * Type:
  4942. * 1 Account Bound
  4943. * 2 Guild Bound
  4944. * 3 Party Bound
  4945. * 4 Character Bound
  4946. *------------------------------------------*/
  4947. int pc_bound_chk(TBL_PC *sd,enum bound_type type,int *idxlist)
  4948. {
  4949. int i = 0, j = 0;
  4950. for(i = 0; i < MAX_INVENTORY; i++) {
  4951. 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) {
  4952. idxlist[j] = i;
  4953. j++;
  4954. }
  4955. }
  4956. return j;
  4957. }
  4958. /*==========================================
  4959. * Display item stolen msg to player sd
  4960. *------------------------------------------*/
  4961. int pc_show_steal(struct block_list *bl,va_list ap)
  4962. {
  4963. struct map_session_data *sd;
  4964. int itemid;
  4965. struct item_data *item=NULL;
  4966. char output[100];
  4967. sd=va_arg(ap,struct map_session_data *);
  4968. itemid=va_arg(ap,int);
  4969. if((item=itemdb_exists(itemid))==NULL)
  4970. sprintf(output,"%s stole an Unknown Item (id: %i).",sd->status.name, itemid);
  4971. else
  4972. sprintf(output,"%s stole %s.",sd->status.name,item->jname);
  4973. clif_displaymessage( ((struct map_session_data *)bl)->fd, output);
  4974. return 0;
  4975. }
  4976. /**
  4977. * Steal an item from bl (mob).
  4978. * @param sd: Player data
  4979. * @param bl: Object to steal from
  4980. * @param skill_lv: Level of skill used
  4981. * @return True on success or false otherwise
  4982. */
  4983. bool pc_steal_item(struct map_session_data *sd,struct block_list *bl, uint16 skill_lv)
  4984. {
  4985. int i,itemid;
  4986. double rate;
  4987. unsigned char flag = 0;
  4988. struct status_data *sd_status, *md_status;
  4989. struct mob_data *md;
  4990. struct item tmp_item;
  4991. if(!sd || !bl || bl->type!=BL_MOB)
  4992. return false;
  4993. md = (TBL_MOB *)bl;
  4994. if(md->state.steal_flag == UCHAR_MAX || ( md->sc.opt1 && md->sc.opt1 != OPT1_BURNING ) ) //already stolen from / status change check
  4995. return false;
  4996. sd_status= status_get_status_data(&sd->bl);
  4997. md_status= status_get_status_data(bl);
  4998. if (md->master_id || status_has_mode(md_status, MD_STATUS_IMMUNE) || status_get_race2(&md->bl) == RC2_TREASURE ||
  4999. map_getmapflag(bl->m, MF_NOMOBLOOT) || // check noloot map flag [Lorky]
  5000. (battle_config.skill_steal_max_tries && //Reached limit of steal attempts. [Lupus]
  5001. md->state.steal_flag++ >= battle_config.skill_steal_max_tries)
  5002. ) { //Can't steal from
  5003. md->state.steal_flag = UCHAR_MAX;
  5004. return false;
  5005. }
  5006. // base skill success chance (percentual)
  5007. rate = (sd_status->dex - md_status->dex)/2 + skill_lv*6 + 4;
  5008. rate += sd->bonus.add_steal_rate;
  5009. if( rate < 1
  5010. #ifdef RENEWAL
  5011. || rnd()%100 >= rate
  5012. #endif
  5013. )
  5014. return false;
  5015. // Try dropping one item, in the order from first to last possible slot.
  5016. // Droprate is affected by the skill success rate.
  5017. for( i = 0; i < MAX_STEAL_DROP; i++ )
  5018. 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
  5019. #ifndef RENEWAL
  5020. * rate/100.
  5021. #endif
  5022. )
  5023. break;
  5024. if( i == MAX_STEAL_DROP )
  5025. return false;
  5026. itemid = md->db->dropitem[i].nameid;
  5027. memset(&tmp_item,0,sizeof(tmp_item));
  5028. tmp_item.nameid = itemid;
  5029. tmp_item.amount = 1;
  5030. tmp_item.identify = itemdb_isidentified(itemid);
  5031. mob_setdropitem_option(&tmp_item, &md->db->dropitem[i]);
  5032. flag = pc_additem(sd,&tmp_item,1,LOG_TYPE_PICKDROP_PLAYER);
  5033. //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?
  5034. md->state.steal_flag = UCHAR_MAX; //you can't steal from this mob any more
  5035. if(flag) { //Failed to steal due to overweight
  5036. clif_additem(sd,0,0,flag);
  5037. return false;
  5038. }
  5039. if(battle_config.show_steal_in_same_party)
  5040. party_foreachsamemap(pc_show_steal,sd,AREA_SIZE,sd,tmp_item.nameid);
  5041. //Logs items, Stolen from mobs [Lupus]
  5042. log_pick_mob(md, LOG_TYPE_STEAL, -1, &tmp_item);
  5043. //A Rare Steal Global Announce by Lupus
  5044. if(md->db->dropitem[i].p<=battle_config.rare_drop_announce) {
  5045. struct item_data *i_data;
  5046. char message[128];
  5047. i_data = itemdb_search(itemid);
  5048. 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);
  5049. //MSG: "'%s' stole %s's %s (chance: %0.02f%%)"
  5050. intif_broadcast(message, strlen(message) + 1, BC_DEFAULT);
  5051. }
  5052. return true;
  5053. }
  5054. /*==========================================
  5055. * Stole zeny from bl (mob)
  5056. * return
  5057. * 0 = fail
  5058. * 1 = success
  5059. *------------------------------------------*/
  5060. int pc_steal_coin(struct map_session_data *sd,struct block_list *target)
  5061. {
  5062. int rate, target_lv;
  5063. struct mob_data *md;
  5064. if(!sd || !target || target->type != BL_MOB)
  5065. return 0;
  5066. md = (TBL_MOB*)target;
  5067. target_lv = status_get_lv(target);
  5068. 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)
  5069. return 0;
  5070. rate = sd->battle_status.dex / 2 + 2 * (sd->status.base_level - target_lv) + (10 * pc_checkskill(sd, RG_STEALCOIN)) + sd->battle_status.luk / 2;
  5071. if(rnd()%1000 < rate)
  5072. {
  5073. // Zeny Steal Amount: (rnd() % (10 * target_lv + 1 - 8 * target_lv)) + 8 * target_lv
  5074. int amount = (rnd() % (2 * target_lv + 1)) + 8 * target_lv; // Reduced formula
  5075. pc_getzeny(sd, amount, LOG_TYPE_STEAL, NULL);
  5076. md->state.steal_coin_flag = 1;
  5077. return 1;
  5078. }
  5079. return 0;
  5080. }
  5081. /*==========================================
  5082. * Set's a player position.
  5083. * @param sd
  5084. * @param mapindex
  5085. * @param x
  5086. * @param y
  5087. * @param clrtype
  5088. * @return SETPOS_OK Success
  5089. * SETPOS_MAPINDEX Invalid map index
  5090. * SETPOS_NO_MAPSERVER Map not in this map-server, and failed to locate alternate map-server.
  5091. * SETPOS_AUTOTRADE Player is in autotrade state
  5092. *------------------------------------------*/
  5093. enum e_setpos pc_setpos(struct map_session_data* sd, unsigned short mapindex, int x, int y, clr_type clrtype)
  5094. {
  5095. nullpo_retr(SETPOS_OK,sd);
  5096. if( !mapindex || !mapindex_id2name(mapindex) ) {
  5097. ShowDebug("pc_setpos: Passed mapindex(%d) is invalid!\n", mapindex);
  5098. return SETPOS_MAPINDEX;
  5099. }
  5100. if ( sd->state.autotrade && (sd->vender_id || sd->buyer_id) ) // Player with autotrade just causes clif glitch! @ FIXME
  5101. return SETPOS_AUTOTRADE;
  5102. if( battle_config.revive_onwarp && pc_isdead(sd) ) { //Revive dead people before warping them
  5103. pc_setstand(sd, true);
  5104. pc_setrestartvalue(sd,1);
  5105. }
  5106. int16 m = map_mapindex2mapid(mapindex);
  5107. struct map_data *mapdata = map_getmapdata(m);
  5108. sd->state.changemap = (sd->mapindex != mapindex);
  5109. sd->state.warping = 1;
  5110. sd->state.workinprogress = WIP_DISABLE_NONE;
  5111. if( sd->state.changemap ) { // Misc map-changing settings
  5112. unsigned short curr_map_instance_id = map_getmapdata(sd->bl.m)->instance_id, new_map_instance_id = (mapdata ? mapdata->instance_id : 0);
  5113. if (curr_map_instance_id != new_map_instance_id) {
  5114. if (curr_map_instance_id) // Update instance timer for the map on leave
  5115. instance_delusers(curr_map_instance_id);
  5116. if (new_map_instance_id) // Update instance timer for the map on enter
  5117. instance_addusers(new_map_instance_id);
  5118. }
  5119. if (sd->bg_id && !mapdata->flag[MF_BATTLEGROUND]) // Moving to a map that isn't a Battlegrounds
  5120. bg_team_leave(sd, false, true);
  5121. sd->state.pmap = sd->bl.m;
  5122. if (sd->sc.count) { // Cancel some map related stuff.
  5123. if (sd->sc.data[SC_JAILED])
  5124. return SETPOS_MAPINDEX; //You may not get out!
  5125. status_change_end(&sd->bl, SC_BOSSMAPINFO, INVALID_TIMER);
  5126. status_change_end(&sd->bl, SC_WARM, INVALID_TIMER);
  5127. status_change_end(&sd->bl, SC_SUN_COMFORT, INVALID_TIMER);
  5128. status_change_end(&sd->bl, SC_MOON_COMFORT, INVALID_TIMER);
  5129. status_change_end(&sd->bl, SC_STAR_COMFORT, INVALID_TIMER);
  5130. status_change_end(&sd->bl, SC_MIRACLE, INVALID_TIMER);
  5131. if (sd->sc.data[SC_KNOWLEDGE]) {
  5132. struct status_change_entry *sce = sd->sc.data[SC_KNOWLEDGE];
  5133. if (sce->timer != INVALID_TIMER)
  5134. delete_timer(sce->timer, status_change_timer);
  5135. sce->timer = add_timer(gettick() + skill_get_time(SG_KNOWLEDGE, sce->val1), status_change_timer, sd->bl.id, SC_KNOWLEDGE);
  5136. }
  5137. status_change_end(&sd->bl, SC_PROPERTYWALK, INVALID_TIMER);
  5138. status_change_end(&sd->bl, SC_CLOAKING, INVALID_TIMER);
  5139. status_change_end(&sd->bl, SC_CLOAKINGEXCEED, INVALID_TIMER);
  5140. }
  5141. for(int i = 0; i < EQI_MAX; i++ ) {
  5142. if( sd->equip_index[i] >= 0 )
  5143. if( pc_isequip(sd,sd->equip_index[i]) )
  5144. pc_unequipitem(sd,sd->equip_index[i],2);
  5145. }
  5146. if (battle_config.clear_unit_onwarp&BL_PC)
  5147. skill_clear_unitgroup(&sd->bl);
  5148. party_send_dot_remove(sd); //minimap dot fix [Kevin]
  5149. guild_send_dot_remove(sd);
  5150. bg_send_dot_remove(sd);
  5151. if (sd->regen.state.gc)
  5152. sd->regen.state.gc = 0;
  5153. // make sure vending is allowed here
  5154. if (sd->state.vending && mapdata->flag[MF_NOVENDING]) {
  5155. clif_displaymessage (sd->fd, msg_txt(sd,276)); // "You can't open a shop on this map"
  5156. vending_closevending(sd);
  5157. }
  5158. channel_pcquit(sd,4); //quit map chan
  5159. // Remove Cloaked NPC if changing to another map
  5160. for (auto it = sd->cloaked_npc.begin(); it != sd->cloaked_npc.end(); ++it) {
  5161. block_list *npc_bl = map_id2bl(*it);
  5162. if (npc_bl && npc_bl->m != m) {
  5163. sd->cloaked_npc.erase(it);
  5164. break;
  5165. }
  5166. }
  5167. }
  5168. if( m < 0 )
  5169. {
  5170. uint32 ip;
  5171. uint16 port;
  5172. struct script_state *st;
  5173. //if can't find any map-servers, just abort setting position.
  5174. if(!sd->mapindex || map_mapname2ipport(mapindex,&ip,&port))
  5175. return SETPOS_NO_MAPSERVER;
  5176. if (sd->npc_id){
  5177. npc_event_dequeue(sd,false);
  5178. st = sd->st;
  5179. }else{
  5180. st = nullptr;
  5181. }
  5182. npc_script_event(sd, NPCE_LOGOUT);
  5183. //remove from map, THEN change x/y coordinates
  5184. unit_remove_map_pc(sd,clrtype);
  5185. sd->mapindex = mapindex;
  5186. sd->bl.x=x;
  5187. sd->bl.y=y;
  5188. pc_clean_skilltree(sd);
  5189. chrif_save(sd, CSAVE_CHANGE_MAPSERV|CSAVE_INVENTORY|CSAVE_CART);
  5190. chrif_changemapserver(sd, ip, (short)port);
  5191. //Free session data from this map server [Kevin]
  5192. unit_free_pc(sd);
  5193. if( st ){
  5194. // Has to be done here, because otherwise unit_free_pc will free the stack already
  5195. st->state = END;
  5196. }
  5197. return SETPOS_OK;
  5198. }
  5199. if( x < 0 || x >= mapdata->xs || y < 0 || y >= mapdata->ys )
  5200. {
  5201. 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);
  5202. x = y = 0; // make it random
  5203. }
  5204. if( x == 0 && y == 0 ) { // pick a random walkable cell
  5205. int c=0;
  5206. do {
  5207. x = rnd()%(mapdata->xs-2)+1;
  5208. y = rnd()%(mapdata->ys-2)+1;
  5209. c++;
  5210. if(c > (mapdata->xs * mapdata->ys)*3){ //force out
  5211. 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));
  5212. return SETPOS_OK; //preventing warp
  5213. //break; //allow warp anyway
  5214. }
  5215. } while(map_getcell(m,x,y,CELL_CHKNOPASS) || (!battle_config.teleport_on_portal && npc_check_areanpc(1,m,x,y,1)));
  5216. }
  5217. if (sd->state.vending && map_getcell(m,x,y,CELL_CHKNOVENDING)) {
  5218. clif_displaymessage (sd->fd, msg_txt(sd,204)); // "You can't open a shop on this cell."
  5219. vending_closevending(sd);
  5220. }
  5221. if(sd->bl.prev != NULL){
  5222. unit_remove_map_pc(sd,clrtype);
  5223. clif_changemap(sd,m,x,y); // [MouseJstr]
  5224. } else if(sd->state.active) //Tag player for rewarping after map-loading is done. [Skotlex]
  5225. sd->state.rewarp = 1;
  5226. sd->mapindex = mapindex;
  5227. sd->bl.m = m;
  5228. sd->bl.x = sd->ud.to_x = x;
  5229. sd->bl.y = sd->ud.to_y = y;
  5230. if( sd->status.guild_id > 0 && mapdata->flag[MF_GVG_CASTLE] )
  5231. { // Increased guild castle regen [Valaris]
  5232. struct guild_castle *gc = guild_mapindex2gc(sd->mapindex);
  5233. if(gc && gc->guild_id == sd->status.guild_id)
  5234. sd->regen.state.gc = 1;
  5235. }
  5236. if( sd->status.pet_id > 0 && sd->pd && sd->pd->pet.intimate > 0 )
  5237. {
  5238. sd->pd->bl.m = m;
  5239. sd->pd->bl.x = sd->pd->ud.to_x = x;
  5240. sd->pd->bl.y = sd->pd->ud.to_y = y;
  5241. sd->pd->ud.dir = sd->ud.dir;
  5242. }
  5243. if( hom_is_active(sd->hd) )
  5244. {
  5245. sd->hd->bl.m = m;
  5246. sd->hd->bl.x = sd->hd->ud.to_x = x;
  5247. sd->hd->bl.y = sd->hd->ud.to_y = y;
  5248. sd->hd->ud.dir = sd->ud.dir;
  5249. }
  5250. if( sd->md )
  5251. {
  5252. sd->md->bl.m = m;
  5253. sd->md->bl.x = sd->md->ud.to_x = x;
  5254. sd->md->bl.y = sd->md->ud.to_y = y;
  5255. sd->md->ud.dir = sd->ud.dir;
  5256. }
  5257. if( sd->ed ) {
  5258. sd->ed->bl.m = m;
  5259. sd->ed->bl.x = sd->ed->ud.to_x = x;
  5260. sd->ed->bl.y = sd->ed->ud.to_y = y;
  5261. sd->ed->ud.dir = sd->ud.dir;
  5262. }
  5263. pc_cell_basilica(sd);
  5264. //check if we gonna be rewarped [lighta]
  5265. if(npc_check_areanpc(1,m,x,y,0)){
  5266. sd->count_rewarp++;
  5267. }
  5268. else
  5269. sd->count_rewarp = 0;
  5270. return SETPOS_OK;
  5271. }
  5272. /*==========================================
  5273. * Warp player sd to random location on current map.
  5274. * May fail if no walkable cell found (1000 attempts).
  5275. * Return:
  5276. * 0 = Success
  5277. * 1,2,3 = Fail
  5278. *------------------------------------------*/
  5279. char pc_randomwarp(struct map_session_data *sd, clr_type type)
  5280. {
  5281. int x,y,i=0;
  5282. nullpo_ret(sd);
  5283. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  5284. if (mapdata->flag[MF_NOTELEPORT]) //Teleport forbidden
  5285. return 3;
  5286. do {
  5287. x = rnd()%(mapdata->xs-2)+1;
  5288. y = rnd()%(mapdata->ys-2)+1;
  5289. } 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);
  5290. if (i < 1000)
  5291. return pc_setpos(sd,mapdata->index,x,y,type);
  5292. return 3;
  5293. }
  5294. /*==========================================
  5295. * Records a memo point at sd's current position
  5296. * pos - entry to replace, (-1: shift oldest entry out)
  5297. *------------------------------------------*/
  5298. bool pc_memo(struct map_session_data* sd, int pos)
  5299. {
  5300. int skill;
  5301. nullpo_ret(sd);
  5302. // check mapflags
  5303. 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) ) {
  5304. clif_skill_teleportmessage(sd, 1); // "Saved point cannot be memorized."
  5305. return false;
  5306. }
  5307. // check inputs
  5308. if( pos < -1 || pos >= MAX_MEMOPOINTS )
  5309. return false; // invalid input
  5310. // check required skill level
  5311. skill = pc_checkskill(sd, AL_WARP);
  5312. if( skill < 1 ) {
  5313. clif_skill_memomessage(sd,2); // "You haven't learned Warp."
  5314. return false;
  5315. }
  5316. if( skill < 2 || skill - 2 < pos ) {
  5317. clif_skill_memomessage(sd,1); // "Skill Level is not high enough."
  5318. return false;
  5319. }
  5320. if( pos == -1 )
  5321. {
  5322. uint8 i;
  5323. // prevent memo-ing the same map multiple times
  5324. ARR_FIND( 0, MAX_MEMOPOINTS, i, sd->status.memo_point[i].map == map_id2index(sd->bl.m) );
  5325. memmove(&sd->status.memo_point[1], &sd->status.memo_point[0], (u8min(i,MAX_MEMOPOINTS-1))*sizeof(struct point));
  5326. pos = 0;
  5327. }
  5328. if( map_getmapdata(sd->bl.m)->instance_id ) {
  5329. clif_displaymessage( sd->fd, msg_txt(sd,384) ); // You cannot create a memo in an instance.
  5330. return false;
  5331. }
  5332. sd->status.memo_point[pos].map = map_id2index(sd->bl.m);
  5333. sd->status.memo_point[pos].x = sd->bl.x;
  5334. sd->status.memo_point[pos].y = sd->bl.y;
  5335. clif_skill_memomessage(sd, 0);
  5336. return true;
  5337. }
  5338. //
  5339. // Skills
  5340. //
  5341. /**
  5342. * Get the skill current cooldown for player.
  5343. * (get the db base cooldown for skill + player specific cooldown)
  5344. * @param sd : player pointer
  5345. * @param id : skill id
  5346. * @param lv : skill lv
  5347. * @return player skill cooldown
  5348. */
  5349. int pc_get_skillcooldown(struct map_session_data *sd, uint16 skill_id, uint16 skill_lv) {
  5350. if (skill_id == SJ_NOVAEXPLOSING) {
  5351. struct status_change *sc = status_get_sc(&sd->bl);
  5352. if (sc && sc->data[SC_DIMENSION])
  5353. return 0;
  5354. }
  5355. int cooldown = skill_get_cooldown(skill_id, skill_lv);
  5356. if (cooldown == 0)
  5357. return 0;
  5358. if (skill_id == SU_TUNABELLY && pc_checkskill(sd, SU_SPIRITOFSEA))
  5359. cooldown -= skill_get_time(SU_TUNABELLY, skill_lv);
  5360. for (auto &it : sd->skillcooldown) {
  5361. if (it.id == skill_id) {
  5362. cooldown += it.val;
  5363. cooldown = max(0, cooldown);
  5364. break;
  5365. }
  5366. }
  5367. return cooldown;
  5368. }
  5369. /*==========================================
  5370. * Return player sd skill_lv learned for given skill
  5371. *------------------------------------------*/
  5372. uint8 pc_checkskill(struct map_session_data *sd, uint16 skill_id)
  5373. {
  5374. uint16 idx = 0;
  5375. if (sd == NULL)
  5376. return 0;
  5377. if ((idx = skill_get_index(skill_id)) == 0) {
  5378. ShowError("pc_checkskill: Invalid skill id %d (char_id=%d).\n", skill_id, sd->status.char_id);
  5379. return 0;
  5380. }
  5381. if (SKILL_CHK_GUILD(skill_id) ) {
  5382. struct guild *g;
  5383. if( sd->status.guild_id>0 && (g=sd->guild)!=NULL)
  5384. return guild_checkskill(g,skill_id);
  5385. return 0;
  5386. }
  5387. return (sd->status.skill[idx].id == skill_id) ? sd->status.skill[idx].lv : 0;
  5388. }
  5389. /**
  5390. * Check if we still have the correct weapon to continue the skill (actually status)
  5391. * If not ending it
  5392. * @param sd
  5393. * @return 0:error, 1:check done
  5394. */
  5395. static void pc_checkallowskill(struct map_session_data *sd)
  5396. {
  5397. const enum sc_type scw_list[] = {
  5398. SC_TWOHANDQUICKEN,
  5399. SC_ONEHAND,
  5400. SC_AURABLADE,
  5401. SC_PARRYING,
  5402. SC_SPEARQUICKEN,
  5403. SC_ADRENALINE,
  5404. SC_ADRENALINE2,
  5405. SC_DANCING,
  5406. SC_GATLINGFEVER,
  5407. };
  5408. uint8 i;
  5409. nullpo_retv(sd);
  5410. if(!sd->sc.count)
  5411. return;
  5412. for (i = 0; i < ARRAYLENGTH(scw_list); i++)
  5413. { // Skills requiring specific weapon types
  5414. if( scw_list[i] == SC_DANCING && !battle_config.dancing_weaponswitch_fix )
  5415. continue;
  5416. if(sd->sc.data[scw_list[i]] &&
  5417. !pc_check_weapontype(sd,skill_get_weapontype(status_sc2skill(scw_list[i]))))
  5418. status_change_end(&sd->bl, scw_list[i], INVALID_TIMER);
  5419. }
  5420. if(sd->sc.data[SC_SPURT] && sd->status.weapon)
  5421. // Spurt requires bare hands (feet, in fact xD)
  5422. status_change_end(&sd->bl, SC_SPURT, INVALID_TIMER);
  5423. if(sd->status.shield <= 0) { // Skills requiring a shield
  5424. const enum sc_type scs_list[] = {
  5425. SC_AUTOGUARD,
  5426. SC_DEFENDER,
  5427. SC_REFLECTSHIELD,
  5428. SC_REFLECTDAMAGE
  5429. };
  5430. for (i = 0; i < ARRAYLENGTH(scs_list); i++)
  5431. if(sd->sc.data[scs_list[i]])
  5432. status_change_end(&sd->bl, scs_list[i], INVALID_TIMER);
  5433. }
  5434. }
  5435. /*==========================================
  5436. * Return equipped index of item on player sd at pos
  5437. * Return
  5438. * -1 : Nothing equipped
  5439. * idx : (this index could be used in inventory to found item_data)
  5440. *------------------------------------------*/
  5441. short pc_checkequip(struct map_session_data *sd,int pos, bool checkall)
  5442. {
  5443. uint8 i;
  5444. nullpo_retr(-1, sd);
  5445. for(i=0;i<EQI_MAX;i++){
  5446. if(pos & equip_bitmask[i]){
  5447. if( checkall && ( pos&~equip_bitmask[i] ) != 0 && sd->equip_index[i] == -1 ){
  5448. // Check all if any match is found
  5449. continue;
  5450. }
  5451. return sd->equip_index[i];
  5452. }
  5453. }
  5454. return -1;
  5455. }
  5456. /*==========================================
  5457. * Check if sd has nameid equipped somewhere
  5458. * @sd : the player session
  5459. * @nameid : id of the item to check
  5460. * @min : : see pc.hpp enum equip_index from ? to @max
  5461. * @max : see pc.hpp enum equip_index for @min to ?
  5462. * -return true,false
  5463. *------------------------------------------*/
  5464. bool pc_checkequip2(struct map_session_data *sd, unsigned short nameid, int min, int max)
  5465. {
  5466. int i;
  5467. for(i = min; i < max; i++) {
  5468. if(equip_bitmask[i]) {
  5469. int idx = sd->equip_index[i];
  5470. if (sd->inventory.u.items_inventory[idx].nameid == nameid)
  5471. return true;
  5472. }
  5473. }
  5474. return false;
  5475. }
  5476. /*==========================================
  5477. * Convert's from the client's lame Job ID system
  5478. * to the map server's 'makes sense' system. [Skotlex]
  5479. *------------------------------------------*/
  5480. int pc_jobid2mapid(unsigned short b_class)
  5481. {
  5482. switch(b_class)
  5483. {
  5484. //Novice And 1-1 Jobs
  5485. case JOB_NOVICE: return MAPID_NOVICE;
  5486. case JOB_SWORDMAN: return MAPID_SWORDMAN;
  5487. case JOB_MAGE: return MAPID_MAGE;
  5488. case JOB_ARCHER: return MAPID_ARCHER;
  5489. case JOB_ACOLYTE: return MAPID_ACOLYTE;
  5490. case JOB_MERCHANT: return MAPID_MERCHANT;
  5491. case JOB_THIEF: return MAPID_THIEF;
  5492. case JOB_TAEKWON: return MAPID_TAEKWON;
  5493. case JOB_WEDDING: return MAPID_WEDDING;
  5494. case JOB_GUNSLINGER: return MAPID_GUNSLINGER;
  5495. case JOB_NINJA: return MAPID_NINJA;
  5496. case JOB_XMAS: return MAPID_XMAS;
  5497. case JOB_SUMMER: return MAPID_SUMMER;
  5498. case JOB_HANBOK: return MAPID_HANBOK;
  5499. case JOB_GANGSI: return MAPID_GANGSI;
  5500. case JOB_OKTOBERFEST: return MAPID_OKTOBERFEST;
  5501. case JOB_SUMMER2: return MAPID_SUMMER2;
  5502. //2-1 Jobs
  5503. case JOB_SUPER_NOVICE: return MAPID_SUPER_NOVICE;
  5504. case JOB_KNIGHT: return MAPID_KNIGHT;
  5505. case JOB_WIZARD: return MAPID_WIZARD;
  5506. case JOB_HUNTER: return MAPID_HUNTER;
  5507. case JOB_PRIEST: return MAPID_PRIEST;
  5508. case JOB_BLACKSMITH: return MAPID_BLACKSMITH;
  5509. case JOB_ASSASSIN: return MAPID_ASSASSIN;
  5510. case JOB_STAR_GLADIATOR: return MAPID_STAR_GLADIATOR;
  5511. case JOB_KAGEROU:
  5512. case JOB_OBORO: return MAPID_KAGEROUOBORO;
  5513. case JOB_REBELLION: return MAPID_REBELLION;
  5514. case JOB_DEATH_KNIGHT: return MAPID_DEATH_KNIGHT;
  5515. //2-2 Jobs
  5516. case JOB_CRUSADER: return MAPID_CRUSADER;
  5517. case JOB_SAGE: return MAPID_SAGE;
  5518. case JOB_BARD:
  5519. case JOB_DANCER: return MAPID_BARDDANCER;
  5520. case JOB_MONK: return MAPID_MONK;
  5521. case JOB_ALCHEMIST: return MAPID_ALCHEMIST;
  5522. case JOB_ROGUE: return MAPID_ROGUE;
  5523. case JOB_SOUL_LINKER: return MAPID_SOUL_LINKER;
  5524. case JOB_DARK_COLLECTOR: return MAPID_DARK_COLLECTOR;
  5525. //Trans Novice And Trans 1-1 Jobs
  5526. case JOB_NOVICE_HIGH: return MAPID_NOVICE_HIGH;
  5527. case JOB_SWORDMAN_HIGH: return MAPID_SWORDMAN_HIGH;
  5528. case JOB_MAGE_HIGH: return MAPID_MAGE_HIGH;
  5529. case JOB_ARCHER_HIGH: return MAPID_ARCHER_HIGH;
  5530. case JOB_ACOLYTE_HIGH: return MAPID_ACOLYTE_HIGH;
  5531. case JOB_MERCHANT_HIGH: return MAPID_MERCHANT_HIGH;
  5532. case JOB_THIEF_HIGH: return MAPID_THIEF_HIGH;
  5533. //Trans 2-1 Jobs
  5534. case JOB_LORD_KNIGHT: return MAPID_LORD_KNIGHT;
  5535. case JOB_HIGH_WIZARD: return MAPID_HIGH_WIZARD;
  5536. case JOB_SNIPER: return MAPID_SNIPER;
  5537. case JOB_HIGH_PRIEST: return MAPID_HIGH_PRIEST;
  5538. case JOB_WHITESMITH: return MAPID_WHITESMITH;
  5539. case JOB_ASSASSIN_CROSS: return MAPID_ASSASSIN_CROSS;
  5540. //Trans 2-2 Jobs
  5541. case JOB_PALADIN: return MAPID_PALADIN;
  5542. case JOB_PROFESSOR: return MAPID_PROFESSOR;
  5543. case JOB_CLOWN:
  5544. case JOB_GYPSY: return MAPID_CLOWNGYPSY;
  5545. case JOB_CHAMPION: return MAPID_CHAMPION;
  5546. case JOB_CREATOR: return MAPID_CREATOR;
  5547. case JOB_STALKER: return MAPID_STALKER;
  5548. //Baby Novice And Baby 1-1 Jobs
  5549. case JOB_BABY: return MAPID_BABY;
  5550. case JOB_BABY_SWORDMAN: return MAPID_BABY_SWORDMAN;
  5551. case JOB_BABY_MAGE: return MAPID_BABY_MAGE;
  5552. case JOB_BABY_ARCHER: return MAPID_BABY_ARCHER;
  5553. case JOB_BABY_ACOLYTE: return MAPID_BABY_ACOLYTE;
  5554. case JOB_BABY_MERCHANT: return MAPID_BABY_MERCHANT;
  5555. case JOB_BABY_THIEF: return MAPID_BABY_THIEF;
  5556. case JOB_BABY_TAEKWON: return MAPID_BABY_TAEKWON;
  5557. case JOB_BABY_GUNSLINGER: return MAPID_BABY_GUNSLINGER;
  5558. case JOB_BABY_NINJA: return MAPID_BABY_NINJA;
  5559. case JOB_BABY_SUMMONER: return MAPID_BABY_SUMMONER;
  5560. //Baby 2-1 Jobs
  5561. case JOB_SUPER_BABY: return MAPID_SUPER_BABY;
  5562. case JOB_BABY_KNIGHT: return MAPID_BABY_KNIGHT;
  5563. case JOB_BABY_WIZARD: return MAPID_BABY_WIZARD;
  5564. case JOB_BABY_HUNTER: return MAPID_BABY_HUNTER;
  5565. case JOB_BABY_PRIEST: return MAPID_BABY_PRIEST;
  5566. case JOB_BABY_BLACKSMITH: return MAPID_BABY_BLACKSMITH;
  5567. case JOB_BABY_ASSASSIN: return MAPID_BABY_ASSASSIN;
  5568. case JOB_BABY_STAR_GLADIATOR: return MAPID_BABY_STAR_GLADIATOR;
  5569. case JOB_BABY_REBELLION: return MAPID_BABY_REBELLION;
  5570. case JOB_BABY_KAGEROU:
  5571. case JOB_BABY_OBORO: return MAPID_BABY_KAGEROUOBORO;
  5572. //Baby 2-2 Jobs
  5573. case JOB_BABY_CRUSADER: return MAPID_BABY_CRUSADER;
  5574. case JOB_BABY_SAGE: return MAPID_BABY_SAGE;
  5575. case JOB_BABY_BARD:
  5576. case JOB_BABY_DANCER: return MAPID_BABY_BARDDANCER;
  5577. case JOB_BABY_MONK: return MAPID_BABY_MONK;
  5578. case JOB_BABY_ALCHEMIST: return MAPID_BABY_ALCHEMIST;
  5579. case JOB_BABY_ROGUE: return MAPID_BABY_ROGUE;
  5580. case JOB_BABY_SOUL_LINKER: return MAPID_BABY_SOUL_LINKER;
  5581. //3-1 Jobs
  5582. case JOB_SUPER_NOVICE_E: return MAPID_SUPER_NOVICE_E;
  5583. case JOB_RUNE_KNIGHT: return MAPID_RUNE_KNIGHT;
  5584. case JOB_WARLOCK: return MAPID_WARLOCK;
  5585. case JOB_RANGER: return MAPID_RANGER;
  5586. case JOB_ARCH_BISHOP: return MAPID_ARCH_BISHOP;
  5587. case JOB_MECHANIC: return MAPID_MECHANIC;
  5588. case JOB_GUILLOTINE_CROSS: return MAPID_GUILLOTINE_CROSS;
  5589. case JOB_STAR_EMPEROR: return MAPID_STAR_EMPEROR;
  5590. //3-2 Jobs
  5591. case JOB_ROYAL_GUARD: return MAPID_ROYAL_GUARD;
  5592. case JOB_SORCERER: return MAPID_SORCERER;
  5593. case JOB_MINSTREL:
  5594. case JOB_WANDERER: return MAPID_MINSTRELWANDERER;
  5595. case JOB_SURA: return MAPID_SURA;
  5596. case JOB_GENETIC: return MAPID_GENETIC;
  5597. case JOB_SHADOW_CHASER: return MAPID_SHADOW_CHASER;
  5598. case JOB_SOUL_REAPER: return MAPID_SOUL_REAPER;
  5599. //Trans 3-1 Jobs
  5600. case JOB_RUNE_KNIGHT_T: return MAPID_RUNE_KNIGHT_T;
  5601. case JOB_WARLOCK_T: return MAPID_WARLOCK_T;
  5602. case JOB_RANGER_T: return MAPID_RANGER_T;
  5603. case JOB_ARCH_BISHOP_T: return MAPID_ARCH_BISHOP_T;
  5604. case JOB_MECHANIC_T: return MAPID_MECHANIC_T;
  5605. case JOB_GUILLOTINE_CROSS_T: return MAPID_GUILLOTINE_CROSS_T;
  5606. //Trans 3-2 Jobs
  5607. case JOB_ROYAL_GUARD_T: return MAPID_ROYAL_GUARD_T;
  5608. case JOB_SORCERER_T: return MAPID_SORCERER_T;
  5609. case JOB_MINSTREL_T:
  5610. case JOB_WANDERER_T: return MAPID_MINSTRELWANDERER_T;
  5611. case JOB_SURA_T: return MAPID_SURA_T;
  5612. case JOB_GENETIC_T: return MAPID_GENETIC_T;
  5613. case JOB_SHADOW_CHASER_T: return MAPID_SHADOW_CHASER_T;
  5614. //Baby 3-1 Jobs
  5615. case JOB_SUPER_BABY_E: return MAPID_SUPER_BABY_E;
  5616. case JOB_BABY_RUNE_KNIGHT: return MAPID_BABY_RUNE_KNIGHT;
  5617. case JOB_BABY_WARLOCK: return MAPID_BABY_WARLOCK;
  5618. case JOB_BABY_RANGER: return MAPID_BABY_RANGER;
  5619. case JOB_BABY_ARCH_BISHOP: return MAPID_BABY_ARCH_BISHOP;
  5620. case JOB_BABY_MECHANIC: return MAPID_BABY_MECHANIC;
  5621. case JOB_BABY_GUILLOTINE_CROSS: return MAPID_BABY_GUILLOTINE_CROSS;
  5622. case JOB_BABY_STAR_EMPEROR: return MAPID_BABY_STAR_EMPEROR;
  5623. //Baby 3-2 Jobs
  5624. case JOB_BABY_ROYAL_GUARD: return MAPID_BABY_ROYAL_GUARD;
  5625. case JOB_BABY_SORCERER: return MAPID_BABY_SORCERER;
  5626. case JOB_BABY_MINSTREL:
  5627. case JOB_BABY_WANDERER: return MAPID_BABY_MINSTRELWANDERER;
  5628. case JOB_BABY_SURA: return MAPID_BABY_SURA;
  5629. case JOB_BABY_GENETIC: return MAPID_BABY_GENETIC;
  5630. case JOB_BABY_SHADOW_CHASER: return MAPID_BABY_SHADOW_CHASER;
  5631. case JOB_BABY_SOUL_REAPER: return MAPID_BABY_SOUL_REAPER;
  5632. //Doram Jobs
  5633. case JOB_SUMMONER: return MAPID_SUMMONER;
  5634. default:
  5635. return -1;
  5636. }
  5637. }
  5638. //Reverts the map-style class id to the client-style one.
  5639. int pc_mapid2jobid(unsigned short class_, int sex)
  5640. {
  5641. switch(class_) {
  5642. //Novice And 1-1 Jobs
  5643. case MAPID_NOVICE: return JOB_NOVICE;
  5644. case MAPID_SWORDMAN: return JOB_SWORDMAN;
  5645. case MAPID_MAGE: return JOB_MAGE;
  5646. case MAPID_ARCHER: return JOB_ARCHER;
  5647. case MAPID_ACOLYTE: return JOB_ACOLYTE;
  5648. case MAPID_MERCHANT: return JOB_MERCHANT;
  5649. case MAPID_THIEF: return JOB_THIEF;
  5650. case MAPID_TAEKWON: return JOB_TAEKWON;
  5651. case MAPID_WEDDING: return JOB_WEDDING;
  5652. case MAPID_GUNSLINGER: return JOB_GUNSLINGER;
  5653. case MAPID_NINJA: return JOB_NINJA;
  5654. case MAPID_XMAS: return JOB_XMAS;
  5655. case MAPID_SUMMER: return JOB_SUMMER;
  5656. case MAPID_HANBOK: return JOB_HANBOK;
  5657. case MAPID_GANGSI: return JOB_GANGSI;
  5658. case MAPID_OKTOBERFEST: return JOB_OKTOBERFEST;
  5659. case MAPID_SUMMER2: return JOB_SUMMER2;
  5660. //2-1 Jobs
  5661. case MAPID_SUPER_NOVICE: return JOB_SUPER_NOVICE;
  5662. case MAPID_KNIGHT: return JOB_KNIGHT;
  5663. case MAPID_WIZARD: return JOB_WIZARD;
  5664. case MAPID_HUNTER: return JOB_HUNTER;
  5665. case MAPID_PRIEST: return JOB_PRIEST;
  5666. case MAPID_BLACKSMITH: return JOB_BLACKSMITH;
  5667. case MAPID_ASSASSIN: return JOB_ASSASSIN;
  5668. case MAPID_STAR_GLADIATOR: return JOB_STAR_GLADIATOR;
  5669. case MAPID_KAGEROUOBORO: return sex?JOB_KAGEROU:JOB_OBORO;
  5670. case MAPID_REBELLION: return JOB_REBELLION;
  5671. case MAPID_DEATH_KNIGHT: return JOB_DEATH_KNIGHT;
  5672. //2-2 Jobs
  5673. case MAPID_CRUSADER: return JOB_CRUSADER;
  5674. case MAPID_SAGE: return JOB_SAGE;
  5675. case MAPID_BARDDANCER: return sex?JOB_BARD:JOB_DANCER;
  5676. case MAPID_MONK: return JOB_MONK;
  5677. case MAPID_ALCHEMIST: return JOB_ALCHEMIST;
  5678. case MAPID_ROGUE: return JOB_ROGUE;
  5679. case MAPID_SOUL_LINKER: return JOB_SOUL_LINKER;
  5680. case MAPID_DARK_COLLECTOR: return JOB_DARK_COLLECTOR;
  5681. //Trans Novice And Trans 2-1 Jobs
  5682. case MAPID_NOVICE_HIGH: return JOB_NOVICE_HIGH;
  5683. case MAPID_SWORDMAN_HIGH: return JOB_SWORDMAN_HIGH;
  5684. case MAPID_MAGE_HIGH: return JOB_MAGE_HIGH;
  5685. case MAPID_ARCHER_HIGH: return JOB_ARCHER_HIGH;
  5686. case MAPID_ACOLYTE_HIGH: return JOB_ACOLYTE_HIGH;
  5687. case MAPID_MERCHANT_HIGH: return JOB_MERCHANT_HIGH;
  5688. case MAPID_THIEF_HIGH: return JOB_THIEF_HIGH;
  5689. //Trans 2-1 Jobs
  5690. case MAPID_LORD_KNIGHT: return JOB_LORD_KNIGHT;
  5691. case MAPID_HIGH_WIZARD: return JOB_HIGH_WIZARD;
  5692. case MAPID_SNIPER: return JOB_SNIPER;
  5693. case MAPID_HIGH_PRIEST: return JOB_HIGH_PRIEST;
  5694. case MAPID_WHITESMITH: return JOB_WHITESMITH;
  5695. case MAPID_ASSASSIN_CROSS: return JOB_ASSASSIN_CROSS;
  5696. //Trans 2-2 Jobs
  5697. case MAPID_PALADIN: return JOB_PALADIN;
  5698. case MAPID_PROFESSOR: return JOB_PROFESSOR;
  5699. case MAPID_CLOWNGYPSY: return sex?JOB_CLOWN:JOB_GYPSY;
  5700. case MAPID_CHAMPION: return JOB_CHAMPION;
  5701. case MAPID_CREATOR: return JOB_CREATOR;
  5702. case MAPID_STALKER: return JOB_STALKER;
  5703. //Baby Novice And Baby 1-1 Jobs
  5704. case MAPID_BABY: return JOB_BABY;
  5705. case MAPID_BABY_SWORDMAN: return JOB_BABY_SWORDMAN;
  5706. case MAPID_BABY_MAGE: return JOB_BABY_MAGE;
  5707. case MAPID_BABY_ARCHER: return JOB_BABY_ARCHER;
  5708. case MAPID_BABY_ACOLYTE: return JOB_BABY_ACOLYTE;
  5709. case MAPID_BABY_MERCHANT: return JOB_BABY_MERCHANT;
  5710. case MAPID_BABY_THIEF: return JOB_BABY_THIEF;
  5711. case MAPID_BABY_TAEKWON: return JOB_BABY_TAEKWON;
  5712. case MAPID_BABY_GUNSLINGER: return JOB_BABY_GUNSLINGER;
  5713. case MAPID_BABY_NINJA: return JOB_BABY_NINJA;
  5714. case MAPID_BABY_SUMMONER: return JOB_BABY_SUMMONER;
  5715. //Baby 2-1 Jobs
  5716. case MAPID_SUPER_BABY: return JOB_SUPER_BABY;
  5717. case MAPID_BABY_KNIGHT: return JOB_BABY_KNIGHT;
  5718. case MAPID_BABY_WIZARD: return JOB_BABY_WIZARD;
  5719. case MAPID_BABY_HUNTER: return JOB_BABY_HUNTER;
  5720. case MAPID_BABY_PRIEST: return JOB_BABY_PRIEST;
  5721. case MAPID_BABY_BLACKSMITH: return JOB_BABY_BLACKSMITH;
  5722. case MAPID_BABY_ASSASSIN: return JOB_BABY_ASSASSIN;
  5723. case MAPID_BABY_STAR_GLADIATOR: return JOB_BABY_STAR_GLADIATOR;
  5724. case MAPID_BABY_REBELLION: return JOB_BABY_REBELLION;
  5725. case MAPID_BABY_KAGEROUOBORO: return sex?JOB_BABY_KAGEROU:JOB_BABY_OBORO;
  5726. //Baby 2-2 Jobs
  5727. case MAPID_BABY_CRUSADER: return JOB_BABY_CRUSADER;
  5728. case MAPID_BABY_SAGE: return JOB_BABY_SAGE;
  5729. case MAPID_BABY_BARDDANCER: return sex?JOB_BABY_BARD:JOB_BABY_DANCER;
  5730. case MAPID_BABY_MONK: return JOB_BABY_MONK;
  5731. case MAPID_BABY_ALCHEMIST: return JOB_BABY_ALCHEMIST;
  5732. case MAPID_BABY_ROGUE: return JOB_BABY_ROGUE;
  5733. case MAPID_BABY_SOUL_LINKER: return JOB_BABY_SOUL_LINKER;
  5734. //3-1 Jobs
  5735. case MAPID_SUPER_NOVICE_E: return JOB_SUPER_NOVICE_E;
  5736. case MAPID_RUNE_KNIGHT: return JOB_RUNE_KNIGHT;
  5737. case MAPID_WARLOCK: return JOB_WARLOCK;
  5738. case MAPID_RANGER: return JOB_RANGER;
  5739. case MAPID_ARCH_BISHOP: return JOB_ARCH_BISHOP;
  5740. case MAPID_MECHANIC: return JOB_MECHANIC;
  5741. case MAPID_GUILLOTINE_CROSS: return JOB_GUILLOTINE_CROSS;
  5742. case MAPID_STAR_EMPEROR: return JOB_STAR_EMPEROR;
  5743. //3-2 Jobs
  5744. case MAPID_ROYAL_GUARD: return JOB_ROYAL_GUARD;
  5745. case MAPID_SORCERER: return JOB_SORCERER;
  5746. case MAPID_MINSTRELWANDERER: return sex?JOB_MINSTREL:JOB_WANDERER;
  5747. case MAPID_SURA: return JOB_SURA;
  5748. case MAPID_GENETIC: return JOB_GENETIC;
  5749. case MAPID_SHADOW_CHASER: return JOB_SHADOW_CHASER;
  5750. case MAPID_SOUL_REAPER: return JOB_SOUL_REAPER;
  5751. //Trans 3-1 Jobs
  5752. case MAPID_RUNE_KNIGHT_T: return JOB_RUNE_KNIGHT_T;
  5753. case MAPID_WARLOCK_T: return JOB_WARLOCK_T;
  5754. case MAPID_RANGER_T: return JOB_RANGER_T;
  5755. case MAPID_ARCH_BISHOP_T: return JOB_ARCH_BISHOP_T;
  5756. case MAPID_MECHANIC_T: return JOB_MECHANIC_T;
  5757. case MAPID_GUILLOTINE_CROSS_T: return JOB_GUILLOTINE_CROSS_T;
  5758. //Trans 3-2 Jobs
  5759. case MAPID_ROYAL_GUARD_T: return JOB_ROYAL_GUARD_T;
  5760. case MAPID_SORCERER_T: return JOB_SORCERER_T;
  5761. case MAPID_MINSTRELWANDERER_T: return sex?JOB_MINSTREL_T:JOB_WANDERER_T;
  5762. case MAPID_SURA_T: return JOB_SURA_T;
  5763. case MAPID_GENETIC_T: return JOB_GENETIC_T;
  5764. case MAPID_SHADOW_CHASER_T: return JOB_SHADOW_CHASER_T;
  5765. //Baby 3-1 Jobs
  5766. case MAPID_SUPER_BABY_E: return JOB_SUPER_BABY_E;
  5767. case MAPID_BABY_RUNE_KNIGHT: return JOB_BABY_RUNE_KNIGHT;
  5768. case MAPID_BABY_WARLOCK: return JOB_BABY_WARLOCK;
  5769. case MAPID_BABY_RANGER: return JOB_BABY_RANGER;
  5770. case MAPID_BABY_ARCH_BISHOP: return JOB_BABY_ARCH_BISHOP;
  5771. case MAPID_BABY_MECHANIC: return JOB_BABY_MECHANIC;
  5772. case MAPID_BABY_GUILLOTINE_CROSS: return JOB_BABY_GUILLOTINE_CROSS;
  5773. case MAPID_BABY_STAR_EMPEROR: return JOB_BABY_STAR_EMPEROR;
  5774. //Baby 3-2 Jobs
  5775. case MAPID_BABY_ROYAL_GUARD: return JOB_BABY_ROYAL_GUARD;
  5776. case MAPID_BABY_SORCERER: return JOB_BABY_SORCERER;
  5777. case MAPID_BABY_MINSTRELWANDERER: return sex?JOB_BABY_MINSTREL:JOB_BABY_WANDERER;
  5778. case MAPID_BABY_SURA: return JOB_BABY_SURA;
  5779. case MAPID_BABY_GENETIC: return JOB_BABY_GENETIC;
  5780. case MAPID_BABY_SHADOW_CHASER: return JOB_BABY_SHADOW_CHASER;
  5781. case MAPID_BABY_SOUL_REAPER: return JOB_BABY_SOUL_REAPER;
  5782. //Doram Jobs
  5783. case MAPID_SUMMONER: return JOB_SUMMONER;
  5784. default:
  5785. return -1;
  5786. }
  5787. }
  5788. /*====================================================
  5789. * This function return the name of the job (by [Yor])
  5790. *----------------------------------------------------*/
  5791. const char* job_name(int class_)
  5792. {
  5793. switch (class_) {
  5794. case JOB_NOVICE:
  5795. case JOB_SWORDMAN:
  5796. case JOB_MAGE:
  5797. case JOB_ARCHER:
  5798. case JOB_ACOLYTE:
  5799. case JOB_MERCHANT:
  5800. case JOB_THIEF:
  5801. return msg_txt(NULL,550 - JOB_NOVICE+class_);
  5802. case JOB_KNIGHT:
  5803. case JOB_PRIEST:
  5804. case JOB_WIZARD:
  5805. case JOB_BLACKSMITH:
  5806. case JOB_HUNTER:
  5807. case JOB_ASSASSIN:
  5808. return msg_txt(NULL,557 - JOB_KNIGHT+class_);
  5809. case JOB_KNIGHT2:
  5810. return msg_txt(NULL,557);
  5811. case JOB_CRUSADER:
  5812. case JOB_MONK:
  5813. case JOB_SAGE:
  5814. case JOB_ROGUE:
  5815. case JOB_ALCHEMIST:
  5816. case JOB_BARD:
  5817. case JOB_DANCER:
  5818. return msg_txt(NULL,563 - JOB_CRUSADER+class_);
  5819. case JOB_CRUSADER2:
  5820. return msg_txt(NULL,563);
  5821. case JOB_WEDDING:
  5822. case JOB_SUPER_NOVICE:
  5823. case JOB_GUNSLINGER:
  5824. case JOB_NINJA:
  5825. case JOB_XMAS:
  5826. return msg_txt(NULL,570 - JOB_WEDDING+class_);
  5827. case JOB_SUMMER:
  5828. case JOB_SUMMER2:
  5829. return msg_txt(NULL,621);
  5830. case JOB_HANBOK:
  5831. return msg_txt(NULL,694);
  5832. case JOB_OKTOBERFEST:
  5833. return msg_txt(NULL,696);
  5834. case JOB_NOVICE_HIGH:
  5835. case JOB_SWORDMAN_HIGH:
  5836. case JOB_MAGE_HIGH:
  5837. case JOB_ARCHER_HIGH:
  5838. case JOB_ACOLYTE_HIGH:
  5839. case JOB_MERCHANT_HIGH:
  5840. case JOB_THIEF_HIGH:
  5841. return msg_txt(NULL,575 - JOB_NOVICE_HIGH+class_);
  5842. case JOB_LORD_KNIGHT:
  5843. case JOB_HIGH_PRIEST:
  5844. case JOB_HIGH_WIZARD:
  5845. case JOB_WHITESMITH:
  5846. case JOB_SNIPER:
  5847. case JOB_ASSASSIN_CROSS:
  5848. return msg_txt(NULL,582 - JOB_LORD_KNIGHT+class_);
  5849. case JOB_LORD_KNIGHT2:
  5850. return msg_txt(NULL,582);
  5851. case JOB_PALADIN:
  5852. case JOB_CHAMPION:
  5853. case JOB_PROFESSOR:
  5854. case JOB_STALKER:
  5855. case JOB_CREATOR:
  5856. case JOB_CLOWN:
  5857. case JOB_GYPSY:
  5858. return msg_txt(NULL,588 - JOB_PALADIN + class_);
  5859. case JOB_PALADIN2:
  5860. return msg_txt(NULL,588);
  5861. case JOB_BABY:
  5862. case JOB_BABY_SWORDMAN:
  5863. case JOB_BABY_MAGE:
  5864. case JOB_BABY_ARCHER:
  5865. case JOB_BABY_ACOLYTE:
  5866. case JOB_BABY_MERCHANT:
  5867. case JOB_BABY_THIEF:
  5868. return msg_txt(NULL,595 - JOB_BABY + class_);
  5869. case JOB_BABY_KNIGHT:
  5870. case JOB_BABY_PRIEST:
  5871. case JOB_BABY_WIZARD:
  5872. case JOB_BABY_BLACKSMITH:
  5873. case JOB_BABY_HUNTER:
  5874. case JOB_BABY_ASSASSIN:
  5875. return msg_txt(NULL,602 - JOB_BABY_KNIGHT + class_);
  5876. case JOB_BABY_KNIGHT2:
  5877. return msg_txt(NULL,602);
  5878. case JOB_BABY_CRUSADER:
  5879. case JOB_BABY_MONK:
  5880. case JOB_BABY_SAGE:
  5881. case JOB_BABY_ROGUE:
  5882. case JOB_BABY_ALCHEMIST:
  5883. case JOB_BABY_BARD:
  5884. case JOB_BABY_DANCER:
  5885. return msg_txt(NULL,608 - JOB_BABY_CRUSADER + class_);
  5886. case JOB_BABY_CRUSADER2:
  5887. return msg_txt(NULL,608);
  5888. case JOB_SUPER_BABY:
  5889. return msg_txt(NULL,615);
  5890. case JOB_TAEKWON:
  5891. return msg_txt(NULL,616);
  5892. case JOB_STAR_GLADIATOR:
  5893. case JOB_STAR_GLADIATOR2:
  5894. return msg_txt(NULL,617);
  5895. case JOB_SOUL_LINKER:
  5896. return msg_txt(NULL,618);
  5897. case JOB_GANGSI:
  5898. case JOB_DEATH_KNIGHT:
  5899. case JOB_DARK_COLLECTOR:
  5900. return msg_txt(NULL,622 - JOB_GANGSI+class_);
  5901. case JOB_RUNE_KNIGHT:
  5902. case JOB_WARLOCK:
  5903. case JOB_RANGER:
  5904. case JOB_ARCH_BISHOP:
  5905. case JOB_MECHANIC:
  5906. case JOB_GUILLOTINE_CROSS:
  5907. return msg_txt(NULL,625 - JOB_RUNE_KNIGHT+class_);
  5908. case JOB_RUNE_KNIGHT_T:
  5909. case JOB_WARLOCK_T:
  5910. case JOB_RANGER_T:
  5911. case JOB_ARCH_BISHOP_T:
  5912. case JOB_MECHANIC_T:
  5913. case JOB_GUILLOTINE_CROSS_T:
  5914. return msg_txt(NULL,681 - JOB_RUNE_KNIGHT_T+class_);
  5915. case JOB_ROYAL_GUARD:
  5916. case JOB_SORCERER:
  5917. case JOB_MINSTREL:
  5918. case JOB_WANDERER:
  5919. case JOB_SURA:
  5920. case JOB_GENETIC:
  5921. case JOB_SHADOW_CHASER:
  5922. return msg_txt(NULL,631 - JOB_ROYAL_GUARD+class_);
  5923. case JOB_ROYAL_GUARD_T:
  5924. case JOB_SORCERER_T:
  5925. case JOB_MINSTREL_T:
  5926. case JOB_WANDERER_T:
  5927. case JOB_SURA_T:
  5928. case JOB_GENETIC_T:
  5929. case JOB_SHADOW_CHASER_T:
  5930. return msg_txt(NULL,687 - JOB_ROYAL_GUARD_T+class_);
  5931. case JOB_RUNE_KNIGHT2:
  5932. case JOB_RUNE_KNIGHT_T2:
  5933. return msg_txt(NULL,625);
  5934. case JOB_ROYAL_GUARD2:
  5935. case JOB_ROYAL_GUARD_T2:
  5936. return msg_txt(NULL,631);
  5937. case JOB_RANGER2:
  5938. case JOB_RANGER_T2:
  5939. return msg_txt(NULL,627);
  5940. case JOB_MECHANIC2:
  5941. case JOB_MECHANIC_T2:
  5942. return msg_txt(NULL,629);
  5943. case JOB_BABY_RUNE_KNIGHT:
  5944. case JOB_BABY_WARLOCK:
  5945. case JOB_BABY_RANGER:
  5946. case JOB_BABY_ARCH_BISHOP:
  5947. case JOB_BABY_MECHANIC:
  5948. case JOB_BABY_GUILLOTINE_CROSS:
  5949. case JOB_BABY_ROYAL_GUARD:
  5950. case JOB_BABY_SORCERER:
  5951. case JOB_BABY_MINSTREL:
  5952. case JOB_BABY_WANDERER:
  5953. case JOB_BABY_SURA:
  5954. case JOB_BABY_GENETIC:
  5955. case JOB_BABY_SHADOW_CHASER:
  5956. return msg_txt(NULL,638 - JOB_BABY_RUNE_KNIGHT+class_);
  5957. case JOB_BABY_RUNE_KNIGHT2:
  5958. return msg_txt(NULL,638);
  5959. case JOB_BABY_ROYAL_GUARD2:
  5960. return msg_txt(NULL,644);
  5961. case JOB_BABY_RANGER2:
  5962. return msg_txt(NULL,640);
  5963. case JOB_BABY_MECHANIC2:
  5964. return msg_txt(NULL,642);
  5965. case JOB_SUPER_NOVICE_E:
  5966. case JOB_SUPER_BABY_E:
  5967. return msg_txt(NULL,651 - JOB_SUPER_NOVICE_E+class_);
  5968. case JOB_KAGEROU:
  5969. case JOB_OBORO:
  5970. return msg_txt(NULL,653 - JOB_KAGEROU+class_);
  5971. case JOB_REBELLION:
  5972. return msg_txt(NULL,695);
  5973. case JOB_SUMMONER:
  5974. return msg_txt(NULL,697);
  5975. case JOB_BABY_SUMMONER:
  5976. return msg_txt(NULL,698);
  5977. case JOB_BABY_NINJA:
  5978. return msg_txt(NULL,699);
  5979. case JOB_BABY_KAGEROU:
  5980. case JOB_BABY_OBORO:
  5981. case JOB_BABY_TAEKWON:
  5982. case JOB_BABY_STAR_GLADIATOR:
  5983. case JOB_BABY_SOUL_LINKER:
  5984. case JOB_BABY_GUNSLINGER:
  5985. case JOB_BABY_REBELLION:
  5986. return msg_txt(NULL,753 - JOB_BABY_KAGEROU+class_);
  5987. case JOB_BABY_STAR_GLADIATOR2:
  5988. return msg_txt(NULL,756);
  5989. case JOB_STAR_EMPEROR:
  5990. case JOB_SOUL_REAPER:
  5991. case JOB_BABY_STAR_EMPEROR:
  5992. case JOB_BABY_SOUL_REAPER:
  5993. return msg_txt(NULL,782 - JOB_STAR_EMPEROR + class_);
  5994. case JOB_STAR_EMPEROR2:
  5995. return msg_txt(NULL,782);
  5996. case JOB_BABY_STAR_EMPEROR2:
  5997. return msg_txt(NULL,784);
  5998. default:
  5999. return msg_txt(NULL,655);
  6000. }
  6001. }
  6002. /*====================================================
  6003. * Timered function to make id follow a target.
  6004. * @id = bl.id (player only atm)
  6005. * target is define in sd->followtarget (bl.id)
  6006. * used by pc_follow
  6007. *----------------------------------------------------*/
  6008. TIMER_FUNC(pc_follow_timer){
  6009. struct map_session_data *sd;
  6010. struct block_list *tbl;
  6011. sd = map_id2sd(id);
  6012. nullpo_ret(sd);
  6013. if (sd->followtimer != tid){
  6014. ShowError("pc_follow_timer %d != %d\n",sd->followtimer,tid);
  6015. sd->followtimer = INVALID_TIMER;
  6016. return 0;
  6017. }
  6018. sd->followtimer = INVALID_TIMER;
  6019. tbl = map_id2bl(sd->followtarget);
  6020. if (tbl == NULL || pc_isdead(sd))
  6021. {
  6022. pc_stop_following(sd);
  6023. return 0;
  6024. }
  6025. // either player or target is currently detached from map blocks (could be teleporting),
  6026. // but still connected to this map, so we'll just increment the timer and check back later
  6027. if (sd->bl.prev != NULL && tbl->prev != NULL &&
  6028. sd->ud.skilltimer == INVALID_TIMER && sd->ud.attacktimer == INVALID_TIMER && sd->ud.walktimer == INVALID_TIMER)
  6029. {
  6030. if((sd->bl.m == tbl->m) && unit_can_reach_bl(&sd->bl,tbl, AREA_SIZE, 0, NULL, NULL)) {
  6031. if (!check_distance_bl(&sd->bl, tbl, 5))
  6032. unit_walktobl(&sd->bl, tbl, 5, 0);
  6033. } else
  6034. pc_setpos(sd, map_id2index(tbl->m), tbl->x, tbl->y, CLR_TELEPORT);
  6035. }
  6036. sd->followtimer = add_timer(
  6037. tick + 1000, // increase time a bit to loosen up map's load
  6038. pc_follow_timer, sd->bl.id, 0);
  6039. return 0;
  6040. }
  6041. int pc_stop_following (struct map_session_data *sd)
  6042. {
  6043. nullpo_ret(sd);
  6044. if (sd->followtimer != INVALID_TIMER) {
  6045. delete_timer(sd->followtimer,pc_follow_timer);
  6046. sd->followtimer = INVALID_TIMER;
  6047. }
  6048. sd->followtarget = -1;
  6049. sd->ud.target_to = 0;
  6050. unit_stop_walking(&sd->bl, 1);
  6051. return 0;
  6052. }
  6053. int pc_follow(struct map_session_data *sd,int target_id)
  6054. {
  6055. struct block_list *bl = map_id2bl(target_id);
  6056. if (bl == NULL /*|| bl->type != BL_PC*/)
  6057. return 1;
  6058. if (sd->followtimer != INVALID_TIMER)
  6059. pc_stop_following(sd);
  6060. sd->followtarget = target_id;
  6061. pc_follow_timer(INVALID_TIMER, gettick(), sd->bl.id, 0);
  6062. return 0;
  6063. }
  6064. int pc_checkbaselevelup(struct map_session_data *sd) {
  6065. unsigned int next = pc_nextbaseexp(sd);
  6066. if (!next || sd->status.base_exp < next || pc_is_maxbaselv(sd))
  6067. return 0;
  6068. do {
  6069. sd->status.base_exp -= next;
  6070. //Kyoki pointed out that the max overcarry exp is the exp needed for the previous level -1. [Skotlex]
  6071. 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 )
  6072. sd->status.base_exp = next-1;
  6073. next = pc_gets_status_point(sd->status.base_level);
  6074. sd->status.base_level++;
  6075. sd->status.status_point += next;
  6076. if( pc_is_maxbaselv(sd) ){
  6077. sd->status.base_exp = u32min(sd->status.base_exp,MAX_LEVEL_BASE_EXP);
  6078. break;
  6079. }
  6080. } while ((next=pc_nextbaseexp(sd)) > 0 && sd->status.base_exp >= next);
  6081. if (battle_config.pet_lv_rate && sd->pd) //<Skotlex> update pet's level
  6082. status_calc_pet(sd->pd,SCO_NONE);
  6083. clif_updatestatus(sd,SP_STATUSPOINT);
  6084. clif_updatestatus(sd,SP_BASELEVEL);
  6085. clif_updatestatus(sd,SP_BASEEXP);
  6086. clif_updatestatus(sd,SP_NEXTBASEEXP);
  6087. status_calc_pc(sd,SCO_FORCE);
  6088. status_percent_heal(&sd->bl,100,100);
  6089. if ((sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE) {
  6090. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_KYRIE),100,1,skill_get_time(PR_KYRIE,1));
  6091. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_IMPOSITIO),100,1,skill_get_time(PR_IMPOSITIO,1));
  6092. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_MAGNIFICAT),100,1,skill_get_time(PR_MAGNIFICAT,1));
  6093. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_GLORIA),100,1,skill_get_time(PR_GLORIA,1));
  6094. sc_start(&sd->bl,&sd->bl,status_skill2sc(PR_SUFFRAGIUM),100,1,skill_get_time(PR_SUFFRAGIUM,1));
  6095. if (sd->state.snovice_dead_flag)
  6096. sd->state.snovice_dead_flag = 0; //Reenable steelbody resurrection on dead.
  6097. } else if( (sd->class_&MAPID_BASEMASK) == MAPID_TAEKWON ) {
  6098. sc_start(&sd->bl,&sd->bl,status_skill2sc(AL_INCAGI),100,10,600000);
  6099. sc_start(&sd->bl,&sd->bl,status_skill2sc(AL_BLESSING),100,10,600000);
  6100. }
  6101. clif_misceffect(&sd->bl,0);
  6102. npc_script_event(sd, NPCE_BASELVUP); //LORDALFA - LVLUPEVENT
  6103. if(sd->status.party_id)
  6104. party_send_levelup(sd);
  6105. pc_baselevelchanged(sd);
  6106. achievement_update_objective(sd, AG_GOAL_LEVEL, 1, sd->status.base_level);
  6107. achievement_update_objective(sd, AG_GOAL_STATUS, 2, sd->status.base_level, sd->status.class_);
  6108. return 1;
  6109. }
  6110. void pc_baselevelchanged(struct map_session_data *sd) {
  6111. uint8 i;
  6112. for( i = 0; i < EQI_MAX; i++ ) {
  6113. if( sd->equip_index[i] >= 0 ) {
  6114. if( sd->inventory_data[ sd->equip_index[i] ]->elvmax && sd->status.base_level > (unsigned int)sd->inventory_data[ sd->equip_index[i] ]->elvmax )
  6115. pc_unequipitem(sd, sd->equip_index[i], 3);
  6116. }
  6117. }
  6118. pc_show_questinfo(sd);
  6119. }
  6120. int pc_checkjoblevelup(struct map_session_data *sd)
  6121. {
  6122. unsigned int next = pc_nextjobexp(sd);
  6123. nullpo_ret(sd);
  6124. if(!next || sd->status.job_exp < next || pc_is_maxjoblv(sd))
  6125. return 0;
  6126. do {
  6127. sd->status.job_exp -= next;
  6128. //Kyoki pointed out that the max overcarry exp is the exp needed for the previous level -1. [Skotlex]
  6129. 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 )
  6130. sd->status.job_exp = next-1;
  6131. sd->status.job_level ++;
  6132. sd->status.skill_point ++;
  6133. if( pc_is_maxjoblv(sd) ){
  6134. sd->status.job_exp = u32min(sd->status.job_exp,MAX_LEVEL_JOB_EXP);
  6135. break;
  6136. }
  6137. } while ((next=pc_nextjobexp(sd)) > 0 && sd->status.job_exp >= next);
  6138. clif_updatestatus(sd,SP_JOBLEVEL);
  6139. clif_updatestatus(sd,SP_JOBEXP);
  6140. clif_updatestatus(sd,SP_NEXTJOBEXP);
  6141. clif_updatestatus(sd,SP_SKILLPOINT);
  6142. status_calc_pc(sd,SCO_FORCE);
  6143. clif_misceffect(&sd->bl,1);
  6144. if (pc_checkskill(sd, SG_DEVIL) && ((sd->class_&MAPID_THIRDMASK) == MAPID_STAR_EMPEROR || pc_is_maxjoblv(sd)) )
  6145. clif_status_change(&sd->bl, EFST_DEVIL1, 1, 0, 0, 0, 1); //Permanent blind effect from SG_DEVIL.
  6146. npc_script_event(sd, NPCE_JOBLVUP);
  6147. achievement_update_objective(sd, AG_GOAL_LEVEL, 1, sd->status.job_level);
  6148. pc_show_questinfo(sd);
  6149. return 1;
  6150. }
  6151. /** Alters experiences calculation based on self bonuses that do not get even shared to the party.
  6152. * @param sd Player
  6153. * @param base_exp Base EXP before peronal bonuses
  6154. * @param job_exp Job EXP before peronal bonuses
  6155. * @param src Block list that affecting the exp calculation
  6156. */
  6157. static void pc_calcexp(struct map_session_data *sd, unsigned int *base_exp, unsigned int *job_exp, struct block_list *src)
  6158. {
  6159. int bonus = 0, vip_bonus_base = 0, vip_bonus_job = 0;
  6160. if (src) {
  6161. struct status_data *status = status_get_status_data(src);
  6162. if( sd->expaddrace[status->race] )
  6163. bonus += sd->expaddrace[status->race];
  6164. if( sd->expaddrace[RC_ALL] )
  6165. bonus += sd->expaddrace[RC_ALL];
  6166. if( sd->expaddclass[status->class_] )
  6167. bonus += sd->expaddclass[status->class_];
  6168. if( sd->expaddclass[CLASS_ALL] )
  6169. bonus += sd->expaddclass[CLASS_ALL];
  6170. if (battle_config.pk_mode &&
  6171. (int)(status_get_lv(src) - sd->status.base_level) >= 20)
  6172. bonus += 15; // pk_mode additional exp if monster >20 levels [Valaris]
  6173. if (src && src->type == BL_MOB && pc_isvip(sd)) { // EXP bonus for VIP player
  6174. vip_bonus_base = battle_config.vip_base_exp_increase;
  6175. vip_bonus_job = battle_config.vip_job_exp_increase;
  6176. }
  6177. }
  6178. // Give EXPBOOST for quests even if src is NULL.
  6179. if (sd->sc.data[SC_EXPBOOST]) {
  6180. bonus += sd->sc.data[SC_EXPBOOST]->val1;
  6181. if (battle_config.vip_bm_increase && pc_isvip(sd)) // Increase Battle Manual EXP rate for VIP
  6182. bonus += (sd->sc.data[SC_EXPBOOST]->val1 / battle_config.vip_bm_increase);
  6183. }
  6184. if (*base_exp) {
  6185. unsigned int exp = (unsigned int)(*base_exp + (double)*base_exp * (bonus + vip_bonus_base)/100.);
  6186. *base_exp = cap_value(exp, 1, UINT_MAX);
  6187. }
  6188. // Give JEXPBOOST for quests even if src is NULL.
  6189. if (sd->sc.data[SC_JEXPBOOST])
  6190. bonus += sd->sc.data[SC_JEXPBOOST]->val1;
  6191. if (*job_exp) {
  6192. unsigned int exp = (unsigned int)(*job_exp + (double)*job_exp * (bonus + vip_bonus_job)/100.);
  6193. *job_exp = cap_value(exp, 1, UINT_MAX);
  6194. }
  6195. return;
  6196. }
  6197. /**
  6198. * Show EXP gained by player in percentage by @showexp
  6199. * @param sd Player
  6200. * @param base_exp Base EXP gained/loss
  6201. * @param next_base_exp Base EXP needed for next base level
  6202. * @param job_exp Job EXP gained/loss
  6203. * @param next_job_exp Job EXP needed for next job level
  6204. * @param lost True:EXP penalty, lose EXP
  6205. **/
  6206. 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) {
  6207. char output[CHAT_SIZE_MAX];
  6208. nullpo_retv(sd);
  6209. sprintf(output, msg_txt(sd,743), // Experience %s Base:%ld (%0.2f%%) Job:%ld (%0.2f%%)
  6210. (lost) ? msg_txt(sd,742) : msg_txt(sd,741),
  6211. (long)base_exp * (lost ? -1 : 1), (base_exp / (float)next_base_exp * 100 * (lost ? -1 : 1)),
  6212. (long)job_exp * (lost ? -1 : 1), (job_exp / (float)next_job_exp * 100 * (lost ? -1 : 1)));
  6213. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  6214. }
  6215. /**
  6216. * Give Base or Job EXP to player, then calculate remaining exp for next lvl
  6217. * @param sd Player
  6218. * @param src EXP source
  6219. * @param base_exp Base EXP gained
  6220. * @param base_exp Job EXP gained
  6221. * @param exp_flag 1: Quest EXP; 2: Param Exp (Ignore Guild EXP tax, EXP adjustments)
  6222. * @return
  6223. **/
  6224. void pc_gainexp(struct map_session_data *sd, struct block_list *src, unsigned int base_exp, unsigned int job_exp, uint8 exp_flag)
  6225. {
  6226. unsigned int nextb = 0, nextj = 0;
  6227. uint8 flag = 0; ///< 1: Base EXP given, 2: Job EXP given, 4: Max Base level, 8: Max Job Level
  6228. nullpo_retv(sd);
  6229. if(sd->bl.prev == NULL || pc_isdead(sd))
  6230. return;
  6231. if (!(exp_flag&2)) {
  6232. if (!battle_config.pvp_exp && map_getmapflag(sd->bl.m, MF_PVP)) // [MouseJstr]
  6233. return; // no exp on pvp maps
  6234. if (sd->status.guild_id>0)
  6235. base_exp -= guild_payexp(sd,base_exp);
  6236. }
  6237. flag = ((base_exp) ? 1 : 0) |
  6238. ((job_exp) ? 2 : 0) |
  6239. ((pc_is_maxbaselv(sd)) ? 4 : 0) |
  6240. ((pc_is_maxjoblv(sd)) ? 8 : 0);
  6241. if (!(exp_flag&2))
  6242. pc_calcexp(sd, &base_exp, &job_exp, src);
  6243. nextb = pc_nextbaseexp(sd);
  6244. nextj = pc_nextjobexp(sd);
  6245. if (flag&4){
  6246. if( sd->status.base_exp >= MAX_LEVEL_BASE_EXP )
  6247. base_exp = 0;
  6248. else if( sd->status.base_exp + base_exp >= MAX_LEVEL_BASE_EXP )
  6249. base_exp = MAX_LEVEL_BASE_EXP - sd->status.base_exp;
  6250. }
  6251. if (flag&8){
  6252. if( sd->status.job_exp >= MAX_LEVEL_JOB_EXP )
  6253. job_exp = 0;
  6254. else if( sd->status.job_exp + job_exp >= MAX_LEVEL_JOB_EXP )
  6255. job_exp = MAX_LEVEL_JOB_EXP - sd->status.job_exp;
  6256. }
  6257. if (!(exp_flag&2) && battle_config.max_exp_gain_rate && (base_exp || job_exp)) {
  6258. //Note that this value should never be greater than the original
  6259. //therefore no overflow checks are needed. [Skotlex]
  6260. if (nextb > 0) {
  6261. float nextbp = (float) base_exp / (float) nextb;
  6262. if (nextbp > battle_config.max_exp_gain_rate/1000.)
  6263. base_exp = (unsigned int)(battle_config.max_exp_gain_rate/1000.*nextb);
  6264. }
  6265. if (nextj > 0) {
  6266. float nextjp = (float) job_exp / (float) nextj;
  6267. if (nextjp > battle_config.max_exp_gain_rate/1000.)
  6268. job_exp = (unsigned int)(battle_config.max_exp_gain_rate/1000.*nextj);
  6269. }
  6270. }
  6271. // Give EXP for Base Level
  6272. if (base_exp) {
  6273. if ((uint64)sd->status.base_exp + base_exp > UINT32_MAX)
  6274. sd->status.base_exp = UINT32_MAX;
  6275. else
  6276. sd->status.base_exp += base_exp;
  6277. if (!pc_checkbaselevelup(sd))
  6278. clif_updatestatus(sd,SP_BASEEXP);
  6279. }
  6280. // Give EXP for Job Level
  6281. if (job_exp) {
  6282. if ((uint64)sd->status.job_exp + job_exp > UINT32_MAX)
  6283. sd->status.job_exp = UINT32_MAX;
  6284. else
  6285. sd->status.job_exp += job_exp;
  6286. if (!pc_checkjoblevelup(sd))
  6287. clif_updatestatus(sd,SP_JOBEXP);
  6288. }
  6289. if (flag&1)
  6290. clif_displayexp(sd, (flag&4) ? 0 : base_exp, SP_BASEEXP, exp_flag&1, false);
  6291. if (flag&2)
  6292. clif_displayexp(sd, (flag&8) ? 0 : job_exp, SP_JOBEXP, exp_flag&1, false);
  6293. if (sd->state.showexp && (base_exp || job_exp))
  6294. pc_gainexp_disp(sd, base_exp, nextb, job_exp, nextj, false);
  6295. }
  6296. /**
  6297. * Lost Base/Job EXP from a player
  6298. * @param sd Player
  6299. * @param base_exp Base EXP lost
  6300. * @param job_exp Job EXP lost
  6301. **/
  6302. void pc_lostexp(struct map_session_data *sd, unsigned int base_exp, unsigned int job_exp) {
  6303. nullpo_retv(sd);
  6304. if (base_exp) {
  6305. base_exp = u32min(sd->status.base_exp, base_exp);
  6306. sd->status.base_exp -= base_exp;
  6307. clif_displayexp(sd, base_exp, SP_BASEEXP, false, true);
  6308. clif_updatestatus(sd, SP_BASEEXP);
  6309. }
  6310. if (job_exp) {
  6311. job_exp = u32min(sd->status.job_exp, job_exp);
  6312. sd->status.job_exp -= job_exp;
  6313. clif_displayexp(sd, job_exp, SP_JOBEXP, false, true);
  6314. clif_updatestatus(sd, SP_JOBEXP);
  6315. }
  6316. if (sd->state.showexp && (base_exp || job_exp))
  6317. pc_gainexp_disp(sd, base_exp, pc_nextbaseexp(sd), job_exp, pc_nextjobexp(sd), true);
  6318. }
  6319. /**
  6320. * Returns max base level for this character's class.
  6321. * @param class_: Player's class
  6322. * @return Max Base Level
  6323. */
  6324. static unsigned int pc_class_maxbaselv(unsigned short class_) {
  6325. return job_info[pc_class2idx(class_)].max_level[0];
  6326. }
  6327. /**
  6328. * Returns max base level for this character.
  6329. * @param sd Player
  6330. * @return Max Base Level
  6331. **/
  6332. unsigned int pc_maxbaselv(struct map_session_data *sd){
  6333. return pc_class_maxbaselv(sd->status.class_);
  6334. }
  6335. /**
  6336. * Returns max job level for this character's class.
  6337. * @param class_: Player's class
  6338. * @return Max Job Level
  6339. */
  6340. static unsigned int pc_class_maxjoblv(unsigned short class_) {
  6341. return job_info[pc_class2idx(class_)].max_level[1];
  6342. }
  6343. /**
  6344. * Returns max job level for this character.
  6345. * @param sd Player
  6346. * @return Max Job Level
  6347. **/
  6348. unsigned int pc_maxjoblv(struct map_session_data *sd){
  6349. return pc_class_maxjoblv(sd->status.class_);
  6350. }
  6351. /**
  6352. * Check if player is reached max base level
  6353. * @param sd
  6354. * @return True if reached max level
  6355. **/
  6356. bool pc_is_maxbaselv(struct map_session_data *sd) {
  6357. nullpo_retr(false, sd);
  6358. return (sd->status.base_level >= pc_maxbaselv(sd));
  6359. }
  6360. /**
  6361. * Check if player is reached max base level
  6362. * @param sd
  6363. * @return True if reached max level
  6364. **/
  6365. bool pc_is_maxjoblv(struct map_session_data *sd) {
  6366. nullpo_retr(false, sd);
  6367. return (sd->status.job_level >= pc_maxjoblv(sd));
  6368. }
  6369. /**
  6370. * Base exp needed for player to level up.
  6371. * @param sd
  6372. * @return Base EXP needed for next base level
  6373. **/
  6374. unsigned int pc_nextbaseexp(struct map_session_data *sd){
  6375. nullpo_ret(sd);
  6376. if (sd->status.base_level == 0) // Is this something that possible?
  6377. return 0;
  6378. if (pc_is_maxbaselv(sd))
  6379. return MAX_LEVEL_BASE_EXP; // On max level, player's base EXP limit is 99,999,999
  6380. return job_info[pc_class2idx(sd->status.class_)].exp_table[0][sd->status.base_level-1];
  6381. }
  6382. /**
  6383. * Job exp needed for player to level up.
  6384. * @param sd
  6385. * @return Job EXP needed for next job level
  6386. **/
  6387. unsigned int pc_nextjobexp(struct map_session_data *sd){
  6388. nullpo_ret(sd);
  6389. if (sd->status.job_level == 0) // Is this something that possible?
  6390. return 0;
  6391. if (pc_is_maxjoblv(sd))
  6392. return MAX_LEVEL_JOB_EXP; // On max level, player's job EXP limit is 999,999,999
  6393. return job_info[pc_class2idx(sd->status.class_)].exp_table[1][sd->status.job_level-1];
  6394. }
  6395. /// Returns the value of the specified stat.
  6396. static int pc_getstat(struct map_session_data* sd, int type)
  6397. {
  6398. nullpo_retr(-1, sd);
  6399. switch( type ) {
  6400. case SP_STR: return sd->status.str;
  6401. case SP_AGI: return sd->status.agi;
  6402. case SP_VIT: return sd->status.vit;
  6403. case SP_INT: return sd->status.int_;
  6404. case SP_DEX: return sd->status.dex;
  6405. case SP_LUK: return sd->status.luk;
  6406. default:
  6407. return -1;
  6408. }
  6409. }
  6410. /// Sets the specified stat to the specified value.
  6411. /// Returns the new value.
  6412. static int pc_setstat(struct map_session_data* sd, int type, int val)
  6413. {
  6414. nullpo_retr(-1, sd);
  6415. switch( type ) {
  6416. case SP_STR: sd->status.str = val; break;
  6417. case SP_AGI: sd->status.agi = val; break;
  6418. case SP_VIT: sd->status.vit = val; break;
  6419. case SP_INT: sd->status.int_ = val; break;
  6420. case SP_DEX: sd->status.dex = val; break;
  6421. case SP_LUK: sd->status.luk = val; break;
  6422. default:
  6423. return -1;
  6424. }
  6425. return val;
  6426. }
  6427. // Calculates the number of status points PC gets when leveling up (from level to level+1)
  6428. int pc_gets_status_point(int level)
  6429. {
  6430. if (battle_config.use_statpoint_table) //Use values from "db/statpoint.txt"
  6431. return (statp[level+1] - statp[level]);
  6432. else //Default increase
  6433. return ((level+15) / 5);
  6434. }
  6435. #ifdef RENEWAL_STAT
  6436. /// Renewal status point cost formula
  6437. #define PC_STATUS_POINT_COST(low) (((low) < 100) ? (2 + ((low) - 1) / 10) : (16 + 4 * (((low) - 100) / 5)))
  6438. #else
  6439. /// Pre-Renewal status point cost formula
  6440. #define PC_STATUS_POINT_COST(low) (( 1 + ((low) + 9) / 10 ))
  6441. #endif
  6442. /// Returns the number of stat points needed to change the specified stat by val.
  6443. /// If val is negative, returns the number of stat points that would be needed to
  6444. /// raise the specified stat from (current value - val) to current value.
  6445. int pc_need_status_point(struct map_session_data* sd, int type, int val)
  6446. {
  6447. int low, high, sp = 0, max = 0;
  6448. if ( val == 0 )
  6449. return 0;
  6450. low = pc_getstat(sd,type);
  6451. max = pc_maxparameter(sd,(enum e_params)(type-SP_STR));
  6452. if ( low >= max && val > 0 )
  6453. return 0; // Official servers show '0' when max is reached
  6454. high = low + val;
  6455. if ( val < 0 )
  6456. SWAP(low, high);
  6457. for ( ; low < high; low++ )
  6458. sp += PC_STATUS_POINT_COST(low);
  6459. return sp;
  6460. }
  6461. /**
  6462. * Returns the value the specified stat can be increased by with the current
  6463. * amount of available status points for the current character's class.
  6464. *
  6465. * @param sd The target character.
  6466. * @param type Stat to verify.
  6467. * @return Maximum value the stat could grow by.
  6468. */
  6469. int pc_maxparameterincrease(struct map_session_data* sd, int type)
  6470. {
  6471. int base, final_val, status_points, max_param;
  6472. nullpo_ret(sd);
  6473. base = final_val = pc_getstat(sd, type);
  6474. status_points = sd->status.status_point;
  6475. max_param = pc_maxparameter(sd, (enum e_params)(type-SP_STR));
  6476. while (final_val <= max_param && status_points >= 0) {
  6477. status_points -= PC_STATUS_POINT_COST(final_val);
  6478. final_val++;
  6479. }
  6480. final_val--;
  6481. return (final_val > base ? final_val-base : 0);
  6482. }
  6483. /**
  6484. * Raises a stat by the specified amount.
  6485. *
  6486. * Obeys max_parameter limits.
  6487. * Subtracts status points according to the cost of the increased stat points.
  6488. *
  6489. * @param sd The target character.
  6490. * @param type The stat to change (see enum _sp)
  6491. * @param increase The stat increase (strictly positive) amount.
  6492. * @retval true if the stat was increased by any amount.
  6493. * @retval false if there were no changes.
  6494. */
  6495. bool pc_statusup(struct map_session_data* sd, int type, int increase)
  6496. {
  6497. int max_increase = 0, current = 0, needed_points = 0, final_value = 0;
  6498. nullpo_ret(sd);
  6499. // check conditions
  6500. if (type < SP_STR || type > SP_LUK || increase <= 0) {
  6501. clif_statusupack(sd, type, 0, 0);
  6502. return false;
  6503. }
  6504. // check limits
  6505. current = pc_getstat(sd, type);
  6506. max_increase = pc_maxparameterincrease(sd, type);
  6507. increase = cap_value(increase, 0, max_increase); // cap to the maximum status points available
  6508. if (increase <= 0 || current + increase > pc_maxparameter(sd, (enum e_params)(type-SP_STR))) {
  6509. clif_statusupack(sd, type, 0, 0);
  6510. return false;
  6511. }
  6512. // check status points
  6513. needed_points = pc_need_status_point(sd, type, increase);
  6514. if (needed_points < 0 || needed_points > sd->status.status_point) { // Sanity check
  6515. clif_statusupack(sd, type, 0, 0);
  6516. return false;
  6517. }
  6518. // set new values
  6519. final_value = pc_setstat(sd, type, current + increase);
  6520. sd->status.status_point -= needed_points;
  6521. status_calc_pc(sd,SCO_NONE);
  6522. // update increase cost indicator
  6523. clif_updatestatus(sd, SP_USTR + type-SP_STR);
  6524. // update statpoint count
  6525. clif_updatestatus(sd, SP_STATUSPOINT);
  6526. // update stat value
  6527. clif_statusupack(sd, type, 1, final_value); // required
  6528. if( final_value > 255 )
  6529. clif_updatestatus(sd, type); // send after the 'ack' to override the truncated value
  6530. achievement_update_objective(sd, AG_GOAL_STATUS, 1, final_value);
  6531. return true;
  6532. }
  6533. /**
  6534. * Raises a stat by the specified amount.
  6535. *
  6536. * Obeys max_parameter limits.
  6537. * Does not subtract status points for the cost of the modified stat points.
  6538. *
  6539. * @param sd The target character.
  6540. * @param type The stat to change (see enum _sp)
  6541. * @param val The stat increase (or decrease) amount.
  6542. * @return the stat increase amount.
  6543. * @retval 0 if no changes were made.
  6544. */
  6545. int pc_statusup2(struct map_session_data* sd, int type, int val)
  6546. {
  6547. int max, need;
  6548. nullpo_ret(sd);
  6549. if( type < SP_STR || type > SP_LUK )
  6550. {
  6551. clif_statusupack(sd,type,0,0);
  6552. return 0;
  6553. }
  6554. need = pc_need_status_point(sd,type,1);
  6555. max = pc_maxparameter(sd,(enum e_params)(type-SP_STR)); // set new value
  6556. val = pc_setstat(sd, type, cap_value(pc_getstat(sd,type) + val, 1, max));
  6557. status_calc_pc(sd,SCO_NONE);
  6558. // update increase cost indicator
  6559. if( need != pc_need_status_point(sd,type,1) )
  6560. clif_updatestatus(sd, SP_USTR + type-SP_STR);
  6561. // update stat value
  6562. clif_statusupack(sd,type,1,val); // required
  6563. if( val > 255 )
  6564. clif_updatestatus(sd,type); // send after the 'ack' to override the truncated value
  6565. return val;
  6566. }
  6567. /*==========================================
  6568. * Update skill_lv for player sd
  6569. * Skill point allocation
  6570. *------------------------------------------*/
  6571. void pc_skillup(struct map_session_data *sd,uint16 skill_id)
  6572. {
  6573. uint16 idx = skill_get_index(skill_id);
  6574. nullpo_retv(sd);
  6575. if (!idx) {
  6576. if (skill_id)
  6577. ShowError("pc_skillup: Player attempts to level up invalid skill '%d'\n", skill_id);
  6578. return;
  6579. }
  6580. // Level up guild skill
  6581. if (SKILL_CHK_GUILD(skill_id)) {
  6582. guild_skillup(sd, skill_id);
  6583. return;
  6584. }
  6585. // Level up homunculus skill
  6586. else if (sd->hd && SKILL_CHK_HOMUN(skill_id)) {
  6587. hom_skillup(sd->hd, skill_id);
  6588. return;
  6589. }
  6590. else {
  6591. if( sd->status.skill_point > 0 &&
  6592. sd->status.skill[idx].id &&
  6593. sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT && //Don't allow raising while you have granted skills. [Skotlex]
  6594. sd->status.skill[idx].lv < skill_tree_get_max(skill_id, sd->status.class_) )
  6595. {
  6596. int lv, range, upgradable;
  6597. sd->status.skill[idx].lv++;
  6598. sd->status.skill_point--;
  6599. if( !skill_get_inf(skill_id) )
  6600. status_calc_pc(sd,SCO_NONE); // Only recalculate for passive skills.
  6601. else if( sd->status.skill_point == 0 && pc_is_taekwon_ranker(sd) )
  6602. pc_calc_skilltree(sd); // Required to grant all TK Ranker skills.
  6603. else
  6604. pc_check_skilltree(sd); // Check if a new skill can Lvlup
  6605. lv = sd->status.skill[idx].lv;
  6606. range = skill_get_range2(&sd->bl, skill_id, lv, false);
  6607. upgradable = (lv < skill_tree_get_max(sd->status.skill[idx].id, sd->status.class_)) ? 1 : 0;
  6608. clif_skillup(sd,skill_id,lv,range,upgradable);
  6609. clif_updatestatus(sd,SP_SKILLPOINT);
  6610. if( skill_id == GN_REMODELING_CART ) /* cart weight info was updated by status_calc_pc */
  6611. clif_updatestatus(sd,SP_CARTINFO);
  6612. if (pc_checkskill(sd, SG_DEVIL) && ((sd->class_&MAPID_THIRDMASK) == MAPID_STAR_EMPEROR || pc_is_maxjoblv(sd)))
  6613. clif_status_change(&sd->bl, EFST_DEVIL1, 1, 0, 0, 0, 1); //Permanent blind effect from SG_DEVIL.
  6614. if (!pc_has_permission(sd, PC_PERM_ALL_SKILL)) // may skill everything at any time anyways, and this would cause a huge slowdown
  6615. clif_skillinfoblock(sd);
  6616. }
  6617. //else
  6618. // 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);
  6619. }
  6620. }
  6621. /*==========================================
  6622. * /allskill
  6623. *------------------------------------------*/
  6624. int pc_allskillup(struct map_session_data *sd)
  6625. {
  6626. int i;
  6627. nullpo_ret(sd);
  6628. for (i = 0; i < MAX_SKILL; i++) {
  6629. 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) {
  6630. sd->status.skill[i].lv = (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY) ? 0 : sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
  6631. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  6632. if (sd->status.skill[i].lv == 0)
  6633. sd->status.skill[i].id = 0;
  6634. }
  6635. }
  6636. if (!pc_grant_allskills(sd, true)) {
  6637. uint16 sk_id;
  6638. for (i = 0; i < MAX_SKILL_TREE && (sk_id = skill_tree[pc_class2idx(sd->status.class_)][i].skill_id) > 0;i++){
  6639. uint16 sk_idx = skill_get_index(sk_id);
  6640. if (sk_id == 0 || sk_idx == 0)
  6641. continue;
  6642. std::shared_ptr<s_skill_db> skill = skill_db.find(sk_id);
  6643. if (
  6644. (skill->inf2[INF2_ISQUEST] && !battle_config.quest_skill_learn) ||
  6645. ((skill->inf2[INF2_ISWEDDING] || skill->inf2[INF2_ISSPIRIT])) ||
  6646. sk_id == SG_DEVIL
  6647. )
  6648. continue; //Cannot be learned normally.
  6649. sd->status.skill[sk_idx].id = sk_id;
  6650. sd->status.skill[sk_idx].lv = skill_tree_get_max(sk_id, sd->status.class_); // celest
  6651. }
  6652. }
  6653. status_calc_pc(sd,SCO_NONE);
  6654. //Required because if you could level up all skills previously,
  6655. //the update will not be sent as only the lv variable changes.
  6656. clif_skillinfoblock(sd);
  6657. return 0;
  6658. }
  6659. /*==========================================
  6660. * /resetlvl
  6661. *------------------------------------------*/
  6662. int pc_resetlvl(struct map_session_data* sd,int type)
  6663. {
  6664. int i;
  6665. nullpo_ret(sd);
  6666. if (type != 3) //Also reset skills
  6667. pc_resetskill(sd, 0);
  6668. if(type == 1){
  6669. sd->status.skill_point=0;
  6670. sd->status.base_level=1;
  6671. sd->status.job_level=1;
  6672. sd->status.base_exp=0;
  6673. sd->status.job_exp=0;
  6674. if(sd->sc.option !=0)
  6675. sd->sc.option = 0;
  6676. sd->status.str=1;
  6677. sd->status.agi=1;
  6678. sd->status.vit=1;
  6679. sd->status.int_=1;
  6680. sd->status.dex=1;
  6681. sd->status.luk=1;
  6682. if(sd->status.class_ == JOB_NOVICE_HIGH) {
  6683. sd->status.status_point=100; // not 88 [celest]
  6684. // give platinum skills upon changing
  6685. pc_skill(sd,NV_FIRSTAID,1,ADDSKILL_PERMANENT);
  6686. pc_skill(sd,NV_TRICKDEAD,1,ADDSKILL_PERMANENT);
  6687. }
  6688. }
  6689. if(type == 2){
  6690. sd->status.skill_point=0;
  6691. sd->status.base_level=1;
  6692. sd->status.job_level=1;
  6693. sd->status.base_exp=0;
  6694. sd->status.job_exp=0;
  6695. }
  6696. if(type == 3){
  6697. sd->status.base_level=1;
  6698. sd->status.base_exp=0;
  6699. }
  6700. if(type == 4){
  6701. sd->status.job_level=1;
  6702. sd->status.job_exp=0;
  6703. }
  6704. clif_updatestatus(sd,SP_STATUSPOINT);
  6705. clif_updatestatus(sd,SP_STR);
  6706. clif_updatestatus(sd,SP_AGI);
  6707. clif_updatestatus(sd,SP_VIT);
  6708. clif_updatestatus(sd,SP_INT);
  6709. clif_updatestatus(sd,SP_DEX);
  6710. clif_updatestatus(sd,SP_LUK);
  6711. clif_updatestatus(sd,SP_BASELEVEL);
  6712. clif_updatestatus(sd,SP_JOBLEVEL);
  6713. clif_updatestatus(sd,SP_STATUSPOINT);
  6714. clif_updatestatus(sd,SP_BASEEXP);
  6715. clif_updatestatus(sd,SP_JOBEXP);
  6716. clif_updatestatus(sd,SP_NEXTBASEEXP);
  6717. clif_updatestatus(sd,SP_NEXTJOBEXP);
  6718. clif_updatestatus(sd,SP_SKILLPOINT);
  6719. clif_updatestatus(sd,SP_USTR); // Updates needed stat points - Valaris
  6720. clif_updatestatus(sd,SP_UAGI);
  6721. clif_updatestatus(sd,SP_UVIT);
  6722. clif_updatestatus(sd,SP_UINT);
  6723. clif_updatestatus(sd,SP_UDEX);
  6724. clif_updatestatus(sd,SP_ULUK); // End Addition
  6725. for(i=0;i<EQI_MAX;i++) { // unequip items that can't be equipped by base 1 [Valaris]
  6726. if(sd->equip_index[i] >= 0)
  6727. if(pc_isequip(sd,sd->equip_index[i]))
  6728. pc_unequipitem(sd,sd->equip_index[i],2);
  6729. }
  6730. if ((type == 1 || type == 2 || type == 3) && sd->status.party_id)
  6731. party_send_levelup(sd);
  6732. status_calc_pc(sd, SCO_FORCE);
  6733. clif_skillinfoblock(sd);
  6734. return 0;
  6735. }
  6736. /*==========================================
  6737. * /resetstate
  6738. *------------------------------------------*/
  6739. int pc_resetstate(struct map_session_data* sd)
  6740. {
  6741. nullpo_ret(sd);
  6742. if (battle_config.use_statpoint_table)
  6743. { // New statpoint table used here - Dexity
  6744. if (sd->status.base_level > MAX_LEVEL)
  6745. { //statp[] goes out of bounds, can't reset!
  6746. ShowError("pc_resetstate: Can't reset stats of %d:%d, the base level (%d) is greater than the max level supported (%d)\n",
  6747. sd->status.account_id, sd->status.char_id, sd->status.base_level, MAX_LEVEL);
  6748. return 0;
  6749. }
  6750. sd->status.status_point = statp[sd->status.base_level] + ( sd->class_&JOBL_UPPER ? 52 : 0 ); // extra 52+48=100 stat points
  6751. }
  6752. else
  6753. {
  6754. int add=0;
  6755. add += pc_need_status_point(sd, SP_STR, 1-pc_getstat(sd, SP_STR));
  6756. add += pc_need_status_point(sd, SP_AGI, 1-pc_getstat(sd, SP_AGI));
  6757. add += pc_need_status_point(sd, SP_VIT, 1-pc_getstat(sd, SP_VIT));
  6758. add += pc_need_status_point(sd, SP_INT, 1-pc_getstat(sd, SP_INT));
  6759. add += pc_need_status_point(sd, SP_DEX, 1-pc_getstat(sd, SP_DEX));
  6760. add += pc_need_status_point(sd, SP_LUK, 1-pc_getstat(sd, SP_LUK));
  6761. sd->status.status_point+=add;
  6762. }
  6763. pc_setstat(sd, SP_STR, 1);
  6764. pc_setstat(sd, SP_AGI, 1);
  6765. pc_setstat(sd, SP_VIT, 1);
  6766. pc_setstat(sd, SP_INT, 1);
  6767. pc_setstat(sd, SP_DEX, 1);
  6768. pc_setstat(sd, SP_LUK, 1);
  6769. clif_updatestatus(sd,SP_STR);
  6770. clif_updatestatus(sd,SP_AGI);
  6771. clif_updatestatus(sd,SP_VIT);
  6772. clif_updatestatus(sd,SP_INT);
  6773. clif_updatestatus(sd,SP_DEX);
  6774. clif_updatestatus(sd,SP_LUK);
  6775. clif_updatestatus(sd,SP_USTR); // Updates needed stat points - Valaris
  6776. clif_updatestatus(sd,SP_UAGI);
  6777. clif_updatestatus(sd,SP_UVIT);
  6778. clif_updatestatus(sd,SP_UINT);
  6779. clif_updatestatus(sd,SP_UDEX);
  6780. clif_updatestatus(sd,SP_ULUK); // End Addition
  6781. clif_updatestatus(sd,SP_STATUSPOINT);
  6782. if( sd->mission_mobid ) { //bugreport:2200
  6783. sd->mission_mobid = 0;
  6784. sd->mission_count = 0;
  6785. pc_setglobalreg(sd, add_str(TKMISSIONID_VAR), 0);
  6786. }
  6787. status_calc_pc(sd, SCO_NONE);
  6788. return 1;
  6789. }
  6790. /*==========================================
  6791. * /resetskill
  6792. * if flag&1, perform block resync and status_calc call.
  6793. * if flag&2, just count total amount of skill points used by player, do not really reset.
  6794. * if flag&4, just reset the skills if the player class is a bard/dancer type (for changesex.)
  6795. *------------------------------------------*/
  6796. int pc_resetskill(struct map_session_data* sd, int flag)
  6797. {
  6798. int i, skill_point=0;
  6799. nullpo_ret(sd);
  6800. if( flag&4 && (sd->class_&MAPID_UPPERMASK) != MAPID_BARDDANCER )
  6801. return 0;
  6802. if( !(flag&2) ) { //Remove stuff lost when resetting skills.
  6803. /**
  6804. * 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)
  6805. **/
  6806. if( pc_is_taekwon_ranker(sd) )
  6807. return 0;
  6808. if( pc_checkskill(sd, SG_DEVIL) && ((sd->class_&MAPID_THIRDMASK) == MAPID_STAR_EMPEROR || pc_is_maxjoblv(sd)) )
  6809. clif_status_load(&sd->bl, EFST_DEVIL1, 0); //Remove perma blindness due to skill-reset. [Skotlex]
  6810. i = sd->sc.option;
  6811. if( i&OPTION_RIDING && pc_checkskill(sd, KN_RIDING) )
  6812. i &= ~OPTION_RIDING;
  6813. if( i&OPTION_FALCON && pc_checkskill(sd, HT_FALCON) )
  6814. i &= ~OPTION_FALCON;
  6815. if( i&OPTION_DRAGON && pc_checkskill(sd, RK_DRAGONTRAINING) )
  6816. i &= ~OPTION_DRAGON;
  6817. if( i&OPTION_WUG && pc_checkskill(sd, RA_WUGMASTERY) )
  6818. i &= ~OPTION_WUG;
  6819. if( i&OPTION_WUGRIDER && pc_checkskill(sd, RA_WUGRIDER) )
  6820. i &= ~OPTION_WUGRIDER;
  6821. if( i&OPTION_MADOGEAR && ( sd->class_&MAPID_THIRDMASK ) == MAPID_MECHANIC )
  6822. i &= ~OPTION_MADOGEAR;
  6823. #ifndef NEW_CARTS
  6824. if( i&OPTION_CART && pc_checkskill(sd, MC_PUSHCART) )
  6825. i &= ~OPTION_CART;
  6826. #else
  6827. if( sd->sc.data[SC_PUSH_CART] )
  6828. pc_setcart(sd, 0);
  6829. #endif
  6830. if( i != sd->sc.option )
  6831. pc_setoption(sd, i);
  6832. if( hom_is_active(sd->hd) && pc_checkskill(sd, AM_CALLHOMUN) )
  6833. hom_vaporize(sd, HOM_ST_ACTIVE);
  6834. if (sd->sc.data[SC_SPRITEMABLE] && pc_checkskill(sd, SU_SPRITEMABLE))
  6835. status_change_end(&sd->bl, SC_SPRITEMABLE, INVALID_TIMER);
  6836. }
  6837. for (const auto &skill : skill_db) {
  6838. uint16 skill_id = skill.second->nameid, idx = skill_get_index(skill_id);
  6839. uint8 lv = sd->status.skill[idx].lv;
  6840. if (lv == 0 || skill_id == 0)
  6841. continue;
  6842. if( skill.second->inf2[INF2_ISWEDDING] || skill.second->inf2[INF2_ISSPIRIT] ) //Avoid reseting wedding/linker skills.
  6843. continue;
  6844. // Don't reset trick dead if not a novice/baby
  6845. if( skill_id == NV_TRICKDEAD && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE )
  6846. {
  6847. sd->status.skill[idx].lv = 0;
  6848. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  6849. continue;
  6850. }
  6851. // do not reset basic skill
  6852. if (skill_id == NV_BASIC && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE )
  6853. continue;
  6854. if( sd->status.skill[idx].flag == SKILL_FLAG_PERM_GRANTED )
  6855. continue;
  6856. if( flag&4 && !skill_ischangesex(skill_id) )
  6857. continue;
  6858. if( skill.second->inf2[INF2_ISQUEST] && !battle_config.quest_skill_learn )
  6859. { //Only handle quest skills in a special way when you can't learn them manually
  6860. if( battle_config.quest_skill_reset && !(flag&2) )
  6861. { //Wipe them
  6862. sd->status.skill[idx].lv = 0;
  6863. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  6864. }
  6865. continue;
  6866. }
  6867. if( sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT )
  6868. skill_point += lv;
  6869. else
  6870. if( sd->status.skill[idx].flag >= SKILL_FLAG_REPLACED_LV_0 )
  6871. skill_point += (sd->status.skill[idx].flag - SKILL_FLAG_REPLACED_LV_0);
  6872. if( !(flag&2) )
  6873. {// reset
  6874. sd->status.skill[idx].lv = 0;
  6875. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  6876. }
  6877. }
  6878. if( flag&2 || !skill_point ) return skill_point;
  6879. sd->status.skill_point += skill_point;
  6880. if (flag&1) {
  6881. clif_updatestatus(sd,SP_SKILLPOINT);
  6882. clif_skillinfoblock(sd);
  6883. status_calc_pc(sd, SCO_FORCE);
  6884. }
  6885. return skill_point;
  6886. }
  6887. /*==========================================
  6888. * /resetfeel [Komurka]
  6889. *------------------------------------------*/
  6890. int pc_resetfeel(struct map_session_data* sd)
  6891. {
  6892. int i;
  6893. nullpo_ret(sd);
  6894. for (i=0; i<MAX_PC_FEELHATE; i++)
  6895. {
  6896. sd->feel_map[i].m = -1;
  6897. sd->feel_map[i].index = 0;
  6898. pc_setglobalreg(sd, add_str(sg_info[i].feel_var), 0);
  6899. }
  6900. return 0;
  6901. }
  6902. int pc_resethate(struct map_session_data* sd)
  6903. {
  6904. int i;
  6905. nullpo_ret(sd);
  6906. for (i=0; i<MAX_PC_FEELHATE; i++)
  6907. {
  6908. sd->hate_mob[i] = -1;
  6909. pc_setglobalreg(sd, add_str(sg_info[i].hate_var), 0);
  6910. }
  6911. return 0;
  6912. }
  6913. int pc_skillatk_bonus(struct map_session_data *sd, uint16 skill_id)
  6914. {
  6915. int bonus = 0;
  6916. nullpo_ret(sd);
  6917. skill_id = skill_dummy2skill_id(skill_id);
  6918. for (auto &it : sd->skillatk) {
  6919. if (it.id == skill_id) {
  6920. bonus += it.val;
  6921. break;
  6922. }
  6923. }
  6924. return bonus;
  6925. }
  6926. int pc_sub_skillatk_bonus(struct map_session_data *sd, uint16 skill_id)
  6927. {
  6928. int bonus = 0;
  6929. nullpo_ret(sd);
  6930. skill_id = skill_dummy2skill_id(skill_id);
  6931. for (auto &it : sd->subskill) {
  6932. if (it.id == skill_id) {
  6933. bonus += it.val;
  6934. break;
  6935. }
  6936. }
  6937. return bonus;
  6938. }
  6939. int pc_skillheal_bonus(struct map_session_data *sd, uint16 skill_id) {
  6940. int bonus = sd->bonus.add_heal_rate;
  6941. nullpo_ret(sd);
  6942. skill_id = skill_dummy2skill_id(skill_id);
  6943. if( bonus ) {
  6944. switch( skill_id ) {
  6945. case AL_HEAL: if( !(battle_config.skill_add_heal_rate&1) ) bonus = 0; break;
  6946. case PR_SANCTUARY: if( !(battle_config.skill_add_heal_rate&2) ) bonus = 0; break;
  6947. case AM_POTIONPITCHER: if( !(battle_config.skill_add_heal_rate&4) ) bonus = 0; break;
  6948. case CR_SLIMPITCHER: if( !(battle_config.skill_add_heal_rate&8) ) bonus = 0; break;
  6949. case BA_APPLEIDUN: if( !(battle_config.skill_add_heal_rate&16)) bonus = 0; break;
  6950. }
  6951. }
  6952. for (auto &it : sd->skillheal) {
  6953. if (it.id == skill_id) {
  6954. bonus += it.val;
  6955. break;
  6956. }
  6957. }
  6958. return bonus;
  6959. }
  6960. int pc_skillheal2_bonus(struct map_session_data *sd, uint16 skill_id) {
  6961. int bonus = sd->bonus.add_heal2_rate;
  6962. skill_id = skill_dummy2skill_id(skill_id);
  6963. for (auto &it : sd->skillheal2) {
  6964. if (it.id == skill_id) {
  6965. bonus += it.val;
  6966. break;
  6967. }
  6968. }
  6969. return bonus;
  6970. }
  6971. void pc_respawn(struct map_session_data* sd, clr_type clrtype)
  6972. {
  6973. if( !pc_isdead(sd) )
  6974. return; // not applicable
  6975. if( sd->bg_id && bg_member_respawn(sd) )
  6976. return; // member revived by battleground
  6977. pc_setstand(sd, true);
  6978. pc_setrestartvalue(sd,3);
  6979. if( pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, clrtype) != SETPOS_OK )
  6980. clif_resurrection(&sd->bl, 1); //If warping fails, send a normal stand up packet.
  6981. }
  6982. static TIMER_FUNC(pc_respawn_timer){
  6983. struct map_session_data *sd = map_id2sd(id);
  6984. if( sd != NULL )
  6985. {
  6986. sd->pvp_point=0;
  6987. sd->respawn_tid = INVALID_TIMER;
  6988. pc_respawn(sd,CLR_OUTSIGHT);
  6989. }
  6990. return 0;
  6991. }
  6992. /*==========================================
  6993. * Invoked when a player has received damage
  6994. *------------------------------------------*/
  6995. void pc_damage(struct map_session_data *sd,struct block_list *src,unsigned int hp, unsigned int sp)
  6996. {
  6997. if (sp) clif_updatestatus(sd,SP_SP);
  6998. if (hp) clif_updatestatus(sd,SP_HP);
  6999. else return;
  7000. if (!src)
  7001. return;
  7002. if( pc_issit(sd) ) {
  7003. pc_setstand(sd, true);
  7004. skill_sit(sd,0);
  7005. }
  7006. if (sd->progressbar.npc_id)
  7007. clif_progressbar_abort(sd);
  7008. if( sd->status.pet_id > 0 && sd->pd && battle_config.pet_damage_support )
  7009. pet_target_check(sd->pd,src,1);
  7010. if( sd->status.ele_id > 0 )
  7011. elemental_set_target(sd,src);
  7012. if(battle_config.prevent_logout_trigger&PLT_DAMAGE)
  7013. sd->canlog_tick = gettick();
  7014. }
  7015. TIMER_FUNC(pc_close_npc_timer){
  7016. TBL_PC *sd = map_id2sd(id);
  7017. if(sd) pc_close_npc(sd,data);
  7018. return 0;
  7019. }
  7020. /**
  7021. * Method to properly close a NPC for player and clear anything related.
  7022. * @param sd: Player attached
  7023. * @param flag: Method of closure
  7024. * 1: Produce a close button and end the NPC
  7025. * 2: End the NPC (best for no dialog windows)
  7026. */
  7027. void pc_close_npc(struct map_session_data *sd,int flag)
  7028. {
  7029. nullpo_retv(sd);
  7030. if (sd->npc_id || sd->npc_shopid) {
  7031. if (sd->state.using_fake_npc) {
  7032. clif_clearunit_single(sd->npc_id, CLR_OUTSIGHT, sd->fd);
  7033. sd->state.using_fake_npc = 0;
  7034. }
  7035. if (sd->st) {
  7036. if(sd->st->state == RUN){ //wait ending code execution
  7037. add_timer(gettick()+500,pc_close_npc_timer,sd->bl.id,flag);
  7038. return;
  7039. }
  7040. sd->st->state = ((flag==1 && sd->st->mes_active)?CLOSE:END);
  7041. sd->st->mes_active = 0;
  7042. }
  7043. sd->state.menu_or_input = 0;
  7044. sd->npc_menu = 0;
  7045. sd->npc_shopid = 0;
  7046. #ifdef SECURE_NPCTIMEOUT
  7047. sd->npc_idle_timer = INVALID_TIMER;
  7048. #endif
  7049. if (sd->st) {
  7050. if (sd->st->state == CLOSE) {
  7051. clif_scriptclose(sd, sd->npc_id);
  7052. clif_scriptclear(sd, sd->npc_id); // [Ind/Hercules]
  7053. sd->st->state = END; // Force to end now
  7054. }
  7055. if (sd->st->state == END) { // free attached scripts that are waiting
  7056. script_free_state(sd->st);
  7057. sd->st = NULL;
  7058. sd->npc_id = 0;
  7059. }
  7060. }
  7061. }
  7062. }
  7063. /*==========================================
  7064. * Invoked when a player has negative current hp
  7065. *------------------------------------------*/
  7066. int pc_dead(struct map_session_data *sd,struct block_list *src)
  7067. {
  7068. int i=0,k=0;
  7069. t_tick tick = gettick();
  7070. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  7071. // Activate Steel body if a super novice dies at 99+% exp [celest]
  7072. // Super Novices have no kill or die functions attached when saved by their angel
  7073. if (!sd->state.snovice_dead_flag && (sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE) {
  7074. unsigned int exp = pc_nextbaseexp(sd);
  7075. if( exp && get_percentage(sd->status.base_exp,exp) >= 99 ) {
  7076. sd->state.snovice_dead_flag = 1;
  7077. pc_setrestartvalue(sd,1);
  7078. status_percent_heal(&sd->bl, 100, 100);
  7079. clif_resurrection(&sd->bl, 1);
  7080. if(battle_config.pc_invincible_time)
  7081. pc_setinvincibletimer(sd, battle_config.pc_invincible_time);
  7082. sc_start(&sd->bl,&sd->bl,status_skill2sc(MO_STEELBODY),100,5,skill_get_time(MO_STEELBODY,5));
  7083. if(mapdata_flag_gvg2(mapdata))
  7084. pc_respawn_timer(INVALID_TIMER, gettick(), sd->bl.id, 0);
  7085. return 0;
  7086. }
  7087. }
  7088. for(k = 0; k < MAX_DEVOTION; k++) {
  7089. if (sd->devotion[k]){
  7090. struct map_session_data *devsd = map_id2sd(sd->devotion[k]);
  7091. if (devsd)
  7092. status_change_end(&devsd->bl, SC_DEVOTION, INVALID_TIMER);
  7093. sd->devotion[k] = 0;
  7094. }
  7095. }
  7096. for (k = 0; k < MAX_STELLAR_MARKS; k++) {
  7097. if (sd->stellar_mark[k]) {
  7098. struct map_session_data *smarksd = map_id2sd(sd->stellar_mark[k]);
  7099. if (smarksd)
  7100. status_change_end(&smarksd->bl, SC_FLASHKICK, INVALID_TIMER);
  7101. sd->stellar_mark[k] = 0;
  7102. }
  7103. }
  7104. for (k = 0; k < MAX_UNITED_SOULS; k++) {
  7105. if (sd->united_soul[k]) {
  7106. struct map_session_data *usoulsd = map_id2sd(sd->united_soul[k]);
  7107. if (usoulsd)
  7108. status_change_end(&usoulsd->bl, SC_SOULUNITY, INVALID_TIMER);
  7109. sd->united_soul[k] = 0;
  7110. }
  7111. }
  7112. if(sd->shadowform_id) { //if we were target of shadowform
  7113. status_change_end(map_id2bl(sd->shadowform_id), SC__SHADOWFORM, INVALID_TIMER);
  7114. sd->shadowform_id = 0; //should be remove on status end anyway
  7115. }
  7116. if(sd->status.pet_id > 0 && sd->pd) {
  7117. struct pet_data *pd = sd->pd;
  7118. if( !mapdata->flag[MF_NOEXPPENALTY] ) {
  7119. pet_set_intimate(pd, pd->pet.intimate + pd->get_pet_db()->die);
  7120. if( pd->pet.intimate <= PET_INTIMATE_NONE )
  7121. pet_set_intimate(pd, PET_INTIMATE_NONE);
  7122. clif_send_petdata(sd,sd->pd,1,pd->pet.intimate);
  7123. }
  7124. if( sd->pd->target_id ) // Unlock all targets...
  7125. pet_unlocktarget(sd->pd);
  7126. }
  7127. 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)
  7128. hom_vaporize(sd, HOM_ST_ACTIVE);
  7129. if( sd->md )
  7130. mercenary_delete(sd->md, 3); // Your mercenary soldier has ran away.
  7131. if( sd->ed )
  7132. elemental_delete(sd->ed);
  7133. // Leave duel if you die [LuzZza]
  7134. if(battle_config.duel_autoleave_when_die) {
  7135. if(sd->duel_group > 0)
  7136. duel_leave(sd->duel_group, sd);
  7137. if(sd->duel_invite > 0)
  7138. duel_reject(sd->duel_invite, sd);
  7139. }
  7140. pc_close_npc(sd,2); //close npc if we were using one
  7141. /* e.g. not killed thru pc_damage */
  7142. if( pc_issit(sd) ) {
  7143. clif_status_load(&sd->bl,EFST_SIT,0);
  7144. }
  7145. pc_setdead(sd);
  7146. clif_party_dead( sd );
  7147. pc_setglobalreg(sd, add_str(PCDIECOUNTER_VAR), sd->die_counter+1);
  7148. pc_setparam(sd, SP_KILLERRID, src?src->id:0);
  7149. //Reset menu skills/item skills
  7150. if ((sd->skillitem) != 0)
  7151. sd->skillitem = sd->skillitemlv = 0;
  7152. if ((sd->menuskill_id) != 0)
  7153. sd->menuskill_id = sd->menuskill_val = 0;
  7154. //Reset ticks.
  7155. sd->hp_loss.tick = sd->sp_loss.tick = sd->hp_regen.tick = sd->sp_regen.tick = 0;
  7156. if ( sd->spiritball !=0 )
  7157. pc_delspiritball(sd,sd->spiritball,0);
  7158. if (sd->soulball != 0)
  7159. pc_delsoulball(sd, sd->soulball, 0);
  7160. if (sd->spiritcharm_type != CHARM_TYPE_NONE && sd->spiritcharm > 0)
  7161. pc_delspiritcharm(sd,sd->spiritcharm,sd->spiritcharm_type);
  7162. if (src)
  7163. switch (src->type) {
  7164. case BL_MOB:
  7165. {
  7166. struct mob_data *md=(struct mob_data *)src;
  7167. if(md->target_id==sd->bl.id)
  7168. mob_unlocktarget(md,tick);
  7169. if(battle_config.mobs_level_up && md->status.hp &&
  7170. (unsigned int)md->level < pc_maxbaselv(sd) &&
  7171. !md->guardian_data && !md->special_state.ai// Guardians/summons should not level. [Skotlex]
  7172. ) { // monster level up [Valaris]
  7173. clif_misceffect(&md->bl,0);
  7174. md->level++;
  7175. status_calc_mob(md, SCO_NONE);
  7176. status_percent_heal(src,10,0);
  7177. if( battle_config.show_mob_info&4 )
  7178. {// update name with new level
  7179. clif_name_area(&md->bl);
  7180. }
  7181. }
  7182. src = battle_get_master(src); // Maybe Player Summon
  7183. }
  7184. break;
  7185. case BL_PET: //Pass on to master...
  7186. case BL_HOM:
  7187. case BL_MER:
  7188. src = battle_get_master(src);
  7189. break;
  7190. }
  7191. if (src && src->type == BL_PC) {
  7192. struct map_session_data *ssd = (struct map_session_data *)src;
  7193. pc_setparam(ssd, SP_KILLEDRID, sd->bl.id);
  7194. npc_script_event(ssd, NPCE_KILLPC);
  7195. if (battle_config.pk_mode&2) {
  7196. ssd->status.manner -= 5;
  7197. if(ssd->status.manner < 0)
  7198. sc_start(&sd->bl,src,SC_NOCHAT,100,0,0);
  7199. #if 0
  7200. // PK/Karma system code (not enabled yet) [celest]
  7201. // originally from Kade Online, so i don't know if any of these is correct ^^;
  7202. // note: karma is measured REVERSE, so more karma = more 'evil' / less honourable,
  7203. // karma going down = more 'good' / more honourable.
  7204. // The Karma System way...
  7205. if (sd->status.karma > ssd->status.karma) { // If player killed was more evil
  7206. sd->status.karma--;
  7207. ssd->status.karma--;
  7208. }
  7209. else if (sd->status.karma < ssd->status.karma) // If player killed was more good
  7210. ssd->status.karma++;
  7211. // or the PK System way...
  7212. if (sd->status.karma > 0) // player killed is dishonourable?
  7213. ssd->status.karma--; // honour points earned
  7214. sd->status.karma++; // honour points lost
  7215. // To-do: Receive exp on certain occasions
  7216. #endif
  7217. }
  7218. }
  7219. if(battle_config.bone_drop==2
  7220. || (battle_config.bone_drop==1 && mapdata->flag[MF_PVP]))
  7221. {
  7222. struct item item_tmp;
  7223. memset(&item_tmp,0,sizeof(item_tmp));
  7224. item_tmp.nameid=ITEMID_SKULL_;
  7225. item_tmp.identify=1;
  7226. item_tmp.card[0]=CARD0_CREATE;
  7227. item_tmp.card[1]=0;
  7228. item_tmp.card[2]=GetWord(sd->status.char_id,0); // CharId
  7229. item_tmp.card[3]=GetWord(sd->status.char_id,1);
  7230. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  7231. }
  7232. //Remove bonus_script when dead
  7233. pc_bonus_script_clear(sd,BSF_REM_ON_DEAD);
  7234. // changed penalty options, added death by player if pk_mode [Valaris]
  7235. if(battle_config.death_penalty_type
  7236. && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE // only novices will receive no penalty
  7237. && !sd->sc.data[SC_BABY] && !sd->sc.data[SC_LIFEINSURANCE]
  7238. && !mapdata->flag[MF_NOEXPPENALTY] && !mapdata_flag_gvg2(mapdata))
  7239. {
  7240. uint32 base_penalty = 0;
  7241. uint32 job_penalty = 0;
  7242. uint32 zeny_penalty = 0;
  7243. if (pc_isvip(sd)) { // EXP penalty for VIP
  7244. base_penalty = battle_config.vip_exp_penalty_base;
  7245. job_penalty = battle_config.vip_exp_penalty_job;
  7246. zeny_penalty = battle_config.vip_zeny_penalty;
  7247. } else {
  7248. base_penalty = battle_config.death_penalty_base;
  7249. job_penalty = battle_config.death_penalty_job;
  7250. zeny_penalty = battle_config.zeny_penalty;
  7251. }
  7252. if ((battle_config.death_penalty_maxlv&1 || !pc_is_maxbaselv(sd)) && base_penalty > 0) {
  7253. switch (battle_config.death_penalty_type) {
  7254. case 1: base_penalty = (uint32) ( pc_nextbaseexp(sd) * ( base_penalty / 10000. ) ); break;
  7255. case 2: base_penalty = (uint32) ( sd->status.base_exp * ( base_penalty / 10000. ) ); break;
  7256. }
  7257. if (base_penalty){ //recheck after altering to speedup
  7258. if (battle_config.pk_mode && src && src->type==BL_PC)
  7259. base_penalty *= 2;
  7260. base_penalty = u32min(sd->status.base_exp, base_penalty);
  7261. }
  7262. }
  7263. else
  7264. base_penalty = 0;
  7265. if ((battle_config.death_penalty_maxlv&2 || !pc_is_maxjoblv(sd)) && job_penalty > 0) {
  7266. switch (battle_config.death_penalty_type) {
  7267. case 1: job_penalty = (uint32) ( pc_nextjobexp(sd) * ( job_penalty / 10000. ) ); break;
  7268. case 2: job_penalty = (uint32) ( sd->status.job_exp * ( job_penalty /10000. ) ); break;
  7269. }
  7270. if (job_penalty) {
  7271. if (battle_config.pk_mode && src && src->type==BL_PC)
  7272. job_penalty *= 2;
  7273. job_penalty = u32min(sd->status.job_exp, job_penalty);
  7274. }
  7275. }
  7276. else
  7277. job_penalty = 0;
  7278. if (base_penalty || job_penalty)
  7279. pc_lostexp(sd, base_penalty, job_penalty);
  7280. if( zeny_penalty > 0 && !mapdata->flag[MF_NOZENYPENALTY]) {
  7281. zeny_penalty = (uint32)( sd->status.zeny * ( zeny_penalty / 10000. ) );
  7282. if(zeny_penalty)
  7283. pc_payzeny(sd, zeny_penalty, LOG_TYPE_PICKDROP_PLAYER, NULL);
  7284. }
  7285. }
  7286. if( mapdata->flag[MF_PVP_NIGHTMAREDROP] ) { // Moved this outside so it works when PVP isn't enabled and during pk mode [Ancyker]
  7287. for (const auto &it : mapdata->drop_list) {
  7288. int id = it.drop_id, per = it.drop_per;
  7289. enum e_nightmare_drop_type type = it.drop_type;
  7290. if(id == 0)
  7291. continue;
  7292. if(id == -1){
  7293. int eq_num=0,eq_n[MAX_INVENTORY];
  7294. memset(eq_n,0,sizeof(eq_n));
  7295. for(i=0;i<MAX_INVENTORY;i++) {
  7296. if( (type&NMDT_INVENTORY && !sd->inventory.u.items_inventory[i].equip)
  7297. || (type&NMDT_EQUIP && sd->inventory.u.items_inventory[i].equip)
  7298. || type&NMDT_ALL)
  7299. {
  7300. int l;
  7301. ARR_FIND( 0, MAX_INVENTORY, l, eq_n[l] <= 0 );
  7302. if( l < MAX_INVENTORY )
  7303. eq_n[l] = i;
  7304. eq_num++;
  7305. }
  7306. }
  7307. if(eq_num > 0){
  7308. int n = eq_n[rnd()%eq_num];
  7309. if(rnd()%10000 < per) {
  7310. if(sd->inventory.u.items_inventory[n].equip)
  7311. pc_unequipitem(sd,n,3);
  7312. pc_dropitem(sd,n,1);
  7313. }
  7314. }
  7315. }
  7316. else if(id > 0) {
  7317. for(i=0;i<MAX_INVENTORY;i++){
  7318. if(sd->inventory.u.items_inventory[i].nameid == id
  7319. && rnd()%10000 < per
  7320. && ((type&NMDT_INVENTORY && !sd->inventory.u.items_inventory[i].equip)
  7321. || (type&NMDT_EQUIP && sd->inventory.u.items_inventory[i].equip)
  7322. || type&NMDT_ALL) ){
  7323. if(sd->inventory.u.items_inventory[i].equip)
  7324. pc_unequipitem(sd,i,3);
  7325. pc_dropitem(sd,i,1);
  7326. break;
  7327. }
  7328. }
  7329. }
  7330. }
  7331. }
  7332. // pvp
  7333. // disable certain pvp functions on pk_mode [Valaris]
  7334. if( !battle_config.pk_mode && mapdata->flag[MF_PVP] && !mapdata->flag[MF_PVP_NOCALCRANK] ) {
  7335. sd->pvp_point -= 5;
  7336. sd->pvp_lost++;
  7337. if( src && src->type == BL_PC ) {
  7338. struct map_session_data *ssd = (struct map_session_data *)src;
  7339. ssd->pvp_point++;
  7340. ssd->pvp_won++;
  7341. }
  7342. if( sd->pvp_point < 0 ) {
  7343. sd->respawn_tid = add_timer(tick+1000, pc_respawn_timer,sd->bl.id,0);
  7344. return 1|8;
  7345. }
  7346. }
  7347. //GvG
  7348. if( mapdata_flag_gvg2(mapdata) ) {
  7349. sd->respawn_tid = add_timer(tick+1000, pc_respawn_timer, sd->bl.id, 0);
  7350. return 1|8;
  7351. }
  7352. else if( sd->bg_id ) {
  7353. std::shared_ptr<s_battleground_data> bg = util::umap_find(bg_team_db, sd->bg_id);
  7354. if (bg) {
  7355. if (bg->cemetery.map > 0) { // Respawn by BG
  7356. sd->respawn_tid = add_timer(tick + 1000, pc_respawn_timer, sd->bl.id, 0);
  7357. return 1|8;
  7358. }
  7359. }
  7360. }
  7361. //Reset "can log out" tick.
  7362. if( battle_config.prevent_logout )
  7363. sd->canlog_tick = gettick() - battle_config.prevent_logout;
  7364. return 1;
  7365. }
  7366. void pc_revive(struct map_session_data *sd,unsigned int hp, unsigned int sp) {
  7367. if(hp) clif_updatestatus(sd,SP_HP);
  7368. if(sp) clif_updatestatus(sd,SP_SP);
  7369. pc_setstand(sd, true);
  7370. if(battle_config.pc_invincible_time > 0)
  7371. pc_setinvincibletimer(sd, battle_config.pc_invincible_time);
  7372. if( sd->state.gmaster_flag ) {
  7373. guild_guildaura_refresh(sd,GD_LEADERSHIP,guild_checkskill(sd->guild,GD_LEADERSHIP));
  7374. guild_guildaura_refresh(sd,GD_GLORYWOUNDS,guild_checkskill(sd->guild,GD_GLORYWOUNDS));
  7375. guild_guildaura_refresh(sd,GD_SOULCOLD,guild_checkskill(sd->guild,GD_SOULCOLD));
  7376. guild_guildaura_refresh(sd,GD_HAWKEYES,guild_checkskill(sd->guild,GD_HAWKEYES));
  7377. }
  7378. }
  7379. bool pc_revive_item(struct map_session_data *sd) {
  7380. nullpo_retr(false, sd);
  7381. if (!pc_isdead(sd) || sd->respawn_tid != INVALID_TIMER)
  7382. return false;
  7383. if (sd->sc.data[SC_HELLPOWER]) // Cannot resurrect while under the effect of SC_HELLPOWER.
  7384. return false;
  7385. int16 item_position = itemdb_group_item_exists_pc(sd, IG_TOKEN_OF_SIEGFRIED);
  7386. uint8 hp = 100, sp = 100;
  7387. if (item_position < 0) {
  7388. if (sd->sc.data[SC_LIGHT_OF_REGENE]) {
  7389. hp = sd->sc.data[SC_LIGHT_OF_REGENE]->val2;
  7390. sp = 0;
  7391. }
  7392. else
  7393. return false;
  7394. }
  7395. if (!status_revive(&sd->bl, hp, sp))
  7396. return false;
  7397. if (item_position < 0)
  7398. status_change_end(&sd->bl, SC_LIGHT_OF_REGENE, INVALID_TIMER);
  7399. else
  7400. pc_delitem(sd, item_position, 1, 0, 1, LOG_TYPE_CONSUME);
  7401. clif_skill_nodamage(&sd->bl, &sd->bl, ALL_RESURRECTION, 4, 1);
  7402. return true;
  7403. }
  7404. // script
  7405. //
  7406. /*==========================================
  7407. * script reading pc status registry
  7408. *------------------------------------------*/
  7409. int64 pc_readparam(struct map_session_data* sd,int64 type)
  7410. {
  7411. int64 val = 0;
  7412. nullpo_ret(sd);
  7413. switch(type) {
  7414. case SP_SKILLPOINT: val = sd->status.skill_point; break;
  7415. case SP_STATUSPOINT: val = sd->status.status_point; break;
  7416. case SP_ZENY: val = sd->status.zeny; break;
  7417. case SP_BASELEVEL: val = sd->status.base_level; break;
  7418. case SP_JOBLEVEL: val = sd->status.job_level; break;
  7419. case SP_CLASS: val = sd->status.class_; break;
  7420. case SP_BASEJOB: val = pc_mapid2jobid(sd->class_&MAPID_UPPERMASK, sd->status.sex); break; //Base job, extracting upper type.
  7421. case SP_UPPER: val = sd->class_&JOBL_UPPER?1:(sd->class_&JOBL_BABY?2:0); break;
  7422. case SP_BASECLASS: val = pc_mapid2jobid(sd->class_&MAPID_BASEMASK, sd->status.sex); break; //Extract base class tree. [Skotlex]
  7423. case SP_SEX: val = sd->status.sex; break;
  7424. case SP_WEIGHT: val = sd->weight; break;
  7425. case SP_MAXWEIGHT: val = sd->max_weight; break;
  7426. case SP_BASEEXP: val = sd->status.base_exp; break;
  7427. case SP_JOBEXP: val = sd->status.job_exp; break;
  7428. case SP_NEXTBASEEXP: val = pc_nextbaseexp(sd); break;
  7429. case SP_NEXTJOBEXP: val = pc_nextjobexp(sd); break;
  7430. case SP_HP: val = sd->battle_status.hp; break;
  7431. case SP_MAXHP: val = sd->battle_status.max_hp; break;
  7432. case SP_SP: val = sd->battle_status.sp; break;
  7433. case SP_MAXSP: val = sd->battle_status.max_sp; break;
  7434. case SP_STR: val = sd->status.str; break;
  7435. case SP_AGI: val = sd->status.agi; break;
  7436. case SP_VIT: val = sd->status.vit; break;
  7437. case SP_INT: val = sd->status.int_; break;
  7438. case SP_DEX: val = sd->status.dex; break;
  7439. case SP_LUK: val = sd->status.luk; break;
  7440. case SP_KARMA: val = sd->status.karma; break;
  7441. case SP_MANNER: val = sd->status.manner; break;
  7442. case SP_FAME: val = sd->status.fame; break;
  7443. case SP_KILLERRID: val = sd->killerrid; break;
  7444. case SP_KILLEDRID: val = sd->killedrid; break;
  7445. case SP_KILLEDGID: val = sd->killedgid; break;
  7446. case SP_SITTING: val = pc_issit(sd)?1:0; break;
  7447. case SP_CHARMOVE: val = sd->status.character_moves; break;
  7448. case SP_CHARRENAME: val = sd->status.rename; break;
  7449. case SP_CHARFONT: val = sd->status.font; break;
  7450. case SP_BANK_VAULT: val = sd->bank_vault; break;
  7451. case SP_CASHPOINTS: val = sd->cashPoints; break;
  7452. case SP_KAFRAPOINTS: val = sd->kafraPoints; break;
  7453. case SP_ROULETTE_BRONZE: val = sd->roulette_point.bronze; break;
  7454. case SP_ROULETTE_SILVER: val = sd->roulette_point.silver; break;
  7455. case SP_ROULETTE_GOLD: val = sd->roulette_point.gold; break;
  7456. case SP_PCDIECOUNTER: val = sd->die_counter; break;
  7457. case SP_COOKMASTERY: val = sd->cook_mastery; break;
  7458. case SP_ACHIEVEMENT_LEVEL: val = sd->achievement_data.level; break;
  7459. case SP_CRITICAL: val = sd->battle_status.cri/10; break;
  7460. case SP_ASPD: val = (2000-sd->battle_status.amotion)/10; break;
  7461. case SP_BASE_ATK:
  7462. #ifdef RENEWAL
  7463. val = sd->bonus.eatk;
  7464. #else
  7465. val = sd->battle_status.batk;
  7466. #endif
  7467. break;
  7468. case SP_DEF1: val = sd->battle_status.def; break;
  7469. case SP_DEF2: val = sd->battle_status.def2; break;
  7470. case SP_MDEF1: val = sd->battle_status.mdef; break;
  7471. case SP_MDEF2: val = sd->battle_status.mdef2; break;
  7472. case SP_HIT: val = sd->battle_status.hit; break;
  7473. case SP_FLEE1: val = sd->battle_status.flee; break;
  7474. case SP_FLEE2: val = sd->battle_status.flee2; break;
  7475. case SP_DEFELE: val = sd->battle_status.def_ele; break;
  7476. case SP_MAXHPRATE: val = sd->hprate; break;
  7477. case SP_MAXSPRATE: val = sd->sprate; break;
  7478. case SP_SPRATE: val = sd->dsprate; break;
  7479. case SP_SPEED_RATE: val = sd->bonus.speed_rate; break;
  7480. case SP_SPEED_ADDRATE: val = sd->bonus.speed_add_rate; break;
  7481. case SP_ASPD_RATE:
  7482. #ifndef RENEWAL_ASPD
  7483. val = sd->battle_status.aspd_rate;
  7484. #else
  7485. val = sd->battle_status.aspd_rate2;
  7486. #endif
  7487. break;
  7488. case SP_HP_RECOV_RATE: val = sd->hprecov_rate; break;
  7489. case SP_SP_RECOV_RATE: val = sd->sprecov_rate; break;
  7490. case SP_CRITICAL_DEF: val = sd->bonus.critical_def; break;
  7491. case SP_NEAR_ATK_DEF: val = sd->bonus.near_attack_def_rate; break;
  7492. case SP_LONG_ATK_DEF: val = sd->bonus.long_attack_def_rate; break;
  7493. case SP_DOUBLE_RATE: val = sd->bonus.double_rate; break;
  7494. case SP_DOUBLE_ADD_RATE: val = sd->bonus.double_add_rate; break;
  7495. case SP_MATK_RATE: val = sd->matk_rate; break;
  7496. case SP_ATK_RATE: val = sd->bonus.atk_rate; break;
  7497. case SP_MAGIC_ATK_DEF: val = sd->bonus.magic_def_rate; break;
  7498. case SP_MISC_ATK_DEF: val = sd->bonus.misc_def_rate; break;
  7499. case SP_PERFECT_HIT_RATE:val = sd->bonus.perfect_hit; break;
  7500. case SP_PERFECT_HIT_ADD_RATE: val = sd->bonus.perfect_hit_add; break;
  7501. case SP_CRITICAL_RATE: val = sd->critical_rate; break;
  7502. case SP_HIT_RATE: val = sd->hit_rate; break;
  7503. case SP_FLEE_RATE: val = sd->flee_rate; break;
  7504. case SP_FLEE2_RATE: val = sd->flee2_rate; break;
  7505. case SP_DEF_RATE: val = sd->def_rate; break;
  7506. case SP_DEF2_RATE: val = sd->def2_rate; break;
  7507. case SP_MDEF_RATE: val = sd->mdef_rate; break;
  7508. case SP_MDEF2_RATE: val = sd->mdef2_rate; break;
  7509. case SP_RESTART_FULL_RECOVER: val = sd->special_state.restart_full_recover?1:0; break;
  7510. case SP_NO_CASTCANCEL: val = sd->special_state.no_castcancel?1:0; break;
  7511. case SP_NO_CASTCANCEL2: val = sd->special_state.no_castcancel2?1:0; break;
  7512. case SP_NO_SIZEFIX: val = sd->special_state.no_sizefix?1:0; break;
  7513. case SP_NO_MAGIC_DAMAGE: val = sd->special_state.no_magic_damage; break;
  7514. case SP_NO_WEAPON_DAMAGE:val = sd->special_state.no_weapon_damage; break;
  7515. case SP_NO_MISC_DAMAGE: val = sd->special_state.no_misc_damage; break;
  7516. case SP_NO_GEMSTONE: val = sd->special_state.no_gemstone?1:0; break;
  7517. case SP_INTRAVISION: val = sd->special_state.intravision?1:0; break;
  7518. case SP_NO_KNOCKBACK: val = sd->special_state.no_knockback?1:0; break;
  7519. case SP_NO_MADO_FUEL: val = sd->special_state.no_mado_fuel?1:0; break;
  7520. case SP_NO_WALK_DELAY: val = sd->special_state.no_walk_delay?1:0; break;
  7521. case SP_SPLASH_RANGE: val = sd->bonus.splash_range; break;
  7522. case SP_SPLASH_ADD_RANGE:val = sd->bonus.splash_add_range; break;
  7523. case SP_SHORT_WEAPON_DAMAGE_RETURN: val = sd->bonus.short_weapon_damage_return; break;
  7524. case SP_LONG_WEAPON_DAMAGE_RETURN: val = sd->bonus.long_weapon_damage_return; break;
  7525. case SP_MAGIC_DAMAGE_RETURN: val = sd->bonus.magic_damage_return; break;
  7526. case SP_PERFECT_HIDE: val = sd->special_state.perfect_hiding?1:0; break;
  7527. case SP_UNBREAKABLE: val = sd->bonus.unbreakable; break;
  7528. case SP_UNBREAKABLE_WEAPON: val = (sd->bonus.unbreakable_equip&EQP_WEAPON)?1:0; break;
  7529. case SP_UNBREAKABLE_ARMOR: val = (sd->bonus.unbreakable_equip&EQP_ARMOR)?1:0; break;
  7530. case SP_UNBREAKABLE_HELM: val = (sd->bonus.unbreakable_equip&EQP_HELM)?1:0; break;
  7531. case SP_UNBREAKABLE_SHIELD: val = (sd->bonus.unbreakable_equip&EQP_SHIELD)?1:0; break;
  7532. case SP_UNBREAKABLE_GARMENT: val = (sd->bonus.unbreakable_equip&EQP_GARMENT)?1:0; break;
  7533. case SP_UNBREAKABLE_SHOES: val = (sd->bonus.unbreakable_equip&EQP_SHOES)?1:0; break;
  7534. case SP_CLASSCHANGE: val = sd->bonus.classchange; break;
  7535. case SP_LONG_ATK_RATE: val = sd->bonus.long_attack_atk_rate; break;
  7536. case SP_BREAK_WEAPON_RATE: val = sd->bonus.break_weapon_rate; break;
  7537. case SP_BREAK_ARMOR_RATE: val = sd->bonus.break_armor_rate; break;
  7538. case SP_ADD_STEAL_RATE: val = sd->bonus.add_steal_rate; break;
  7539. case SP_DELAYRATE: val = sd->delayrate; break;
  7540. case SP_CRIT_ATK_RATE: val = sd->bonus.crit_atk_rate; break;
  7541. case SP_UNSTRIPABLE_WEAPON: val = (sd->bonus.unstripable_equip&EQP_WEAPON)?1:0; break;
  7542. case SP_UNSTRIPABLE:
  7543. case SP_UNSTRIPABLE_ARMOR:
  7544. val = (sd->bonus.unstripable_equip&EQP_ARMOR)?1:0;
  7545. break;
  7546. case SP_UNSTRIPABLE_HELM: val = (sd->bonus.unstripable_equip&EQP_HELM)?1:0; break;
  7547. case SP_UNSTRIPABLE_SHIELD: val = (sd->bonus.unstripable_equip&EQP_SHIELD)?1:0; break;
  7548. case SP_SP_GAIN_VALUE: val = sd->bonus.sp_gain_value; break;
  7549. case SP_HP_GAIN_VALUE: val = sd->bonus.hp_gain_value; break;
  7550. case SP_LONG_SP_GAIN_VALUE: val = sd->bonus.long_sp_gain_value; break;
  7551. case SP_LONG_HP_GAIN_VALUE: val = sd->bonus.long_hp_gain_value; break;
  7552. case SP_MAGIC_SP_GAIN_VALUE: val = sd->bonus.magic_sp_gain_value; break;
  7553. case SP_MAGIC_HP_GAIN_VALUE: val = sd->bonus.magic_hp_gain_value; break;
  7554. case SP_ADD_HEAL_RATE: val = sd->bonus.add_heal_rate; break;
  7555. case SP_ADD_HEAL2_RATE: val = sd->bonus.add_heal2_rate; break;
  7556. case SP_ADD_ITEM_HEAL_RATE: val = sd->bonus.itemhealrate2; break;
  7557. case SP_EMATK: val = sd->bonus.ematk; break;
  7558. case SP_FIXCASTRATE: val = sd->bonus.fixcastrate; break;
  7559. case SP_ADD_FIXEDCAST: val = sd->bonus.add_fixcast; break;
  7560. case SP_ADD_VARIABLECAST: val = sd->bonus.add_varcast; break;
  7561. case SP_CASTRATE:
  7562. case SP_VARCASTRATE:
  7563. #ifdef RENEWAL_CAST
  7564. val = sd->bonus.varcastrate; break;
  7565. #else
  7566. val = sd->castrate; break;
  7567. #endif
  7568. default:
  7569. ShowError("pc_readparam: Attempt to read unknown parameter '%lld'.\n", type);
  7570. return -1;
  7571. }
  7572. return val;
  7573. }
  7574. /*==========================================
  7575. * script set pc status registry
  7576. *------------------------------------------*/
  7577. bool pc_setparam(struct map_session_data *sd,int64 type,int64 val_tmp)
  7578. {
  7579. nullpo_retr(false,sd);
  7580. int val = static_cast<unsigned int>(val_tmp);
  7581. switch(type){
  7582. case SP_BASELEVEL:
  7583. if (val > pc_maxbaselv(sd)) //Capping to max
  7584. val = pc_maxbaselv(sd);
  7585. if (val > sd->status.base_level) {
  7586. int i = 0;
  7587. int stat=0;
  7588. for (i = 0; i < (int)(val - sd->status.base_level); i++)
  7589. stat += pc_gets_status_point(sd->status.base_level + i);
  7590. sd->status.status_point += stat;
  7591. }
  7592. sd->status.base_level = val;
  7593. sd->status.base_exp = 0;
  7594. // clif_updatestatus(sd, SP_BASELEVEL); // Gets updated at the bottom
  7595. clif_updatestatus(sd, SP_NEXTBASEEXP);
  7596. clif_updatestatus(sd, SP_STATUSPOINT);
  7597. clif_updatestatus(sd, SP_BASEEXP);
  7598. status_calc_pc(sd, SCO_FORCE);
  7599. if(sd->status.party_id)
  7600. party_send_levelup(sd);
  7601. break;
  7602. case SP_JOBLEVEL:
  7603. if (val >= sd->status.job_level) {
  7604. if (val > pc_maxjoblv(sd)) val = pc_maxjoblv(sd);
  7605. sd->status.skill_point += val - sd->status.job_level;
  7606. clif_updatestatus(sd, SP_SKILLPOINT);
  7607. }
  7608. sd->status.job_level = val;
  7609. sd->status.job_exp = 0;
  7610. // clif_updatestatus(sd, SP_JOBLEVEL); // Gets updated at the bottom
  7611. clif_updatestatus(sd, SP_NEXTJOBEXP);
  7612. clif_updatestatus(sd, SP_JOBEXP);
  7613. status_calc_pc(sd, SCO_FORCE);
  7614. break;
  7615. case SP_SKILLPOINT:
  7616. sd->status.skill_point = val;
  7617. break;
  7618. case SP_STATUSPOINT:
  7619. sd->status.status_point = val;
  7620. break;
  7621. case SP_ZENY:
  7622. if( val < 0 )
  7623. return false;// can't set negative zeny
  7624. log_zeny(sd, LOG_TYPE_SCRIPT, sd, -(sd->status.zeny - cap_value(val, 0, MAX_ZENY)));
  7625. sd->status.zeny = cap_value(val, 0, MAX_ZENY);
  7626. break;
  7627. case SP_BASEEXP:
  7628. {
  7629. val = cap_value(val, 0, INT_MAX);
  7630. if (val < sd->status.base_exp) // Lost
  7631. pc_lostexp(sd, sd->status.base_exp - val, 0);
  7632. else // Gained
  7633. pc_gainexp(sd, NULL, val - sd->status.base_exp, 0, 2);
  7634. }
  7635. return true;
  7636. case SP_JOBEXP:
  7637. {
  7638. val = cap_value(val, 0, INT_MAX);
  7639. if (val < sd->status.job_exp) // Lost
  7640. pc_lostexp(sd, 0, sd->status.job_exp - val);
  7641. else // Gained
  7642. pc_gainexp(sd, NULL, 0, val - sd->status.job_exp, 2);
  7643. }
  7644. return true;
  7645. case SP_SEX:
  7646. sd->status.sex = val ? SEX_MALE : SEX_FEMALE;
  7647. break;
  7648. case SP_WEIGHT:
  7649. sd->weight = val;
  7650. break;
  7651. case SP_MAXWEIGHT:
  7652. sd->max_weight = val;
  7653. break;
  7654. case SP_HP:
  7655. sd->battle_status.hp = cap_value(val, 1, (int)sd->battle_status.max_hp);
  7656. break;
  7657. case SP_MAXHP:
  7658. if (sd->status.base_level < 100)
  7659. sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp_lv99);
  7660. else if (sd->status.base_level < 151)
  7661. sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp_lv150);
  7662. else
  7663. sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp);
  7664. if( sd->battle_status.max_hp < sd->battle_status.hp )
  7665. {
  7666. sd->battle_status.hp = sd->battle_status.max_hp;
  7667. clif_updatestatus(sd, SP_HP);
  7668. }
  7669. break;
  7670. case SP_SP:
  7671. sd->battle_status.sp = cap_value(val, 0, (int)sd->battle_status.max_sp);
  7672. break;
  7673. case SP_MAXSP:
  7674. sd->battle_status.max_sp = cap_value(val, 1, battle_config.max_sp);
  7675. if( sd->battle_status.max_sp < sd->battle_status.sp )
  7676. {
  7677. sd->battle_status.sp = sd->battle_status.max_sp;
  7678. clif_updatestatus(sd, SP_SP);
  7679. }
  7680. break;
  7681. case SP_STR:
  7682. sd->status.str = cap_value(val, 1, pc_maxparameter(sd,PARAM_STR));
  7683. break;
  7684. case SP_AGI:
  7685. sd->status.agi = cap_value(val, 1, pc_maxparameter(sd,PARAM_AGI));
  7686. break;
  7687. case SP_VIT:
  7688. sd->status.vit = cap_value(val, 1, pc_maxparameter(sd,PARAM_VIT));
  7689. break;
  7690. case SP_INT:
  7691. sd->status.int_ = cap_value(val, 1, pc_maxparameter(sd,PARAM_INT));
  7692. break;
  7693. case SP_DEX:
  7694. sd->status.dex = cap_value(val, 1, pc_maxparameter(sd,PARAM_DEX));
  7695. break;
  7696. case SP_LUK:
  7697. sd->status.luk = cap_value(val, 1, pc_maxparameter(sd,PARAM_LUK));
  7698. break;
  7699. case SP_KARMA:
  7700. sd->status.karma = val;
  7701. break;
  7702. case SP_MANNER:
  7703. sd->status.manner = val;
  7704. if( val < 0 )
  7705. sc_start(NULL, &sd->bl, SC_NOCHAT, 100, 0, 0);
  7706. else {
  7707. status_change_end(&sd->bl, SC_NOCHAT, INVALID_TIMER);
  7708. clif_manner_message(sd, 5);
  7709. }
  7710. return true; // status_change_start/status_change_end already sends packets warning the client
  7711. case SP_FAME:
  7712. sd->status.fame = val;
  7713. break;
  7714. case SP_KILLERRID:
  7715. sd->killerrid = val;
  7716. return true;
  7717. case SP_KILLEDRID:
  7718. sd->killedrid = val;
  7719. return true;
  7720. case SP_KILLEDGID:
  7721. sd->killedgid = val;
  7722. return true;
  7723. case SP_CHARMOVE:
  7724. sd->status.character_moves = val;
  7725. return true;
  7726. case SP_CHARRENAME:
  7727. sd->status.rename = val;
  7728. return true;
  7729. case SP_CHARFONT:
  7730. sd->status.font = val;
  7731. clif_font(sd);
  7732. return true;
  7733. case SP_BANK_VAULT:
  7734. if (val < 0)
  7735. return false;
  7736. log_zeny(sd, LOG_TYPE_BANK, sd, -(sd->bank_vault - cap_value(val, 0, MAX_BANK_ZENY)));
  7737. sd->bank_vault = cap_value(val, 0, MAX_BANK_ZENY);
  7738. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  7739. return true;
  7740. case SP_ROULETTE_BRONZE:
  7741. sd->roulette_point.bronze = val;
  7742. pc_setreg2(sd, ROULETTE_BRONZE_VAR, sd->roulette_point.bronze);
  7743. return true;
  7744. case SP_ROULETTE_SILVER:
  7745. sd->roulette_point.silver = val;
  7746. pc_setreg2(sd, ROULETTE_SILVER_VAR, sd->roulette_point.silver);
  7747. return true;
  7748. case SP_ROULETTE_GOLD:
  7749. sd->roulette_point.gold = val;
  7750. pc_setreg2(sd, ROULETTE_GOLD_VAR, sd->roulette_point.gold);
  7751. return true;
  7752. case SP_CASHPOINTS:
  7753. if (val < 0)
  7754. return false;
  7755. if (!sd->state.connect_new)
  7756. log_cash(sd, LOG_TYPE_SCRIPT, LOG_CASH_TYPE_CASH, -(sd->cashPoints - cap_value(val, 0, MAX_ZENY)));
  7757. sd->cashPoints = cap_value(val, 0, MAX_ZENY);
  7758. pc_setaccountreg(sd, add_str(CASHPOINT_VAR), sd->cashPoints);
  7759. return true;
  7760. case SP_KAFRAPOINTS:
  7761. if (val < 0)
  7762. return false;
  7763. if (!sd->state.connect_new)
  7764. log_cash(sd, LOG_TYPE_SCRIPT, LOG_CASH_TYPE_KAFRA, -(sd->kafraPoints - cap_value(val, 0, MAX_ZENY)));
  7765. sd->kafraPoints = cap_value(val, 0, MAX_ZENY);
  7766. pc_setaccountreg(sd, add_str(KAFRAPOINT_VAR), sd->kafraPoints);
  7767. return true;
  7768. case SP_PCDIECOUNTER:
  7769. if (val < 0)
  7770. return false;
  7771. if (sd->die_counter == val)
  7772. return true;
  7773. sd->die_counter = val;
  7774. if (!sd->die_counter && (sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE)
  7775. status_calc_pc(sd, SCO_NONE); // Lost the bonus.
  7776. pc_setglobalreg(sd, add_str(PCDIECOUNTER_VAR), sd->die_counter);
  7777. return true;
  7778. case SP_COOKMASTERY:
  7779. if (val < 0)
  7780. return false;
  7781. if (sd->cook_mastery == val)
  7782. return true;
  7783. val = cap_value(val, 0, 1999);
  7784. sd->cook_mastery = val;
  7785. pc_setglobalreg(sd, add_str(COOKMASTERY_VAR), sd->cook_mastery);
  7786. return true;
  7787. default:
  7788. ShowError("pc_setparam: Attempted to set unknown parameter '%lld'.\n", type);
  7789. return false;
  7790. }
  7791. clif_updatestatus(sd,static_cast<int>(type));
  7792. return true;
  7793. }
  7794. /*==========================================
  7795. * HP/SP Healing. If flag is passed, the heal type is through clif_heal, otherwise update status.
  7796. *------------------------------------------*/
  7797. void pc_heal(struct map_session_data *sd,unsigned int hp,unsigned int sp, int type)
  7798. {
  7799. if (type&2) {
  7800. if (hp || type&4)
  7801. clif_heal(sd->fd,SP_HP,hp);
  7802. if (sp)
  7803. clif_heal(sd->fd,SP_SP,sp);
  7804. } else {
  7805. if(hp)
  7806. clif_updatestatus(sd,SP_HP);
  7807. if(sp)
  7808. clif_updatestatus(sd,SP_SP);
  7809. }
  7810. return;
  7811. }
  7812. /**
  7813. * Heal player HP and/or SP linearly. Calculate any bonus based on active statuses.
  7814. * @param sd: Player data
  7815. * @param itemid: Item ID
  7816. * @param hp: HP to heal
  7817. * @param sp: SP to heal
  7818. * @return Amount healed to an object
  7819. */
  7820. int pc_itemheal(struct map_session_data *sd, int itemid, int hp, int sp)
  7821. {
  7822. int bonus, tmp, penalty = 0;
  7823. if (hp) {
  7824. bonus = 100 + (sd->battle_status.vit << 1) + pc_checkskill(sd, SM_RECOVERY) * 10 + pc_checkskill(sd, AM_LEARNINGPOTION) * 5;
  7825. // A potion produced by an Alchemist in the Fame Top 10 gets +50% effect [DracoRPG]
  7826. if (potion_flag == 2) {
  7827. bonus += 50;
  7828. if (sd->sc.data[SC_SPIRIT] && sd->sc.data[SC_SPIRIT]->val2 == SL_ROGUE)
  7829. bonus += 100; // Receive an additional +100% effect from ranked potions to HP only
  7830. }
  7831. //All item bonuses.
  7832. bonus += sd->bonus.itemhealrate2;
  7833. //Item Group bonuses
  7834. bonus += pc_get_itemgroup_bonus(sd, itemid);
  7835. //Individual item bonuses.
  7836. for(const auto &it : sd->itemhealrate) {
  7837. if (it.id == itemid) {
  7838. bonus += it.val;
  7839. break;
  7840. }
  7841. }
  7842. // Recovery Potion
  7843. if (sd->sc.data[SC_INCHEALRATE])
  7844. bonus += sd->sc.data[SC_INCHEALRATE]->val1;
  7845. // 2014 Halloween Event : Pumpkin Bonus
  7846. if (sd->sc.data[SC_MTF_PUMPKIN] && itemid == ITEMID_PUMPKIN)
  7847. bonus += sd->sc.data[SC_MTF_PUMPKIN]->val1;
  7848. tmp = hp * bonus / 100; // Overflow check
  7849. if (bonus != 100 && tmp > hp)
  7850. hp = tmp;
  7851. }
  7852. if (sp) {
  7853. bonus = 100 + (sd->battle_status.int_ << 1) + pc_checkskill(sd, MG_SRECOVERY) * 10 + pc_checkskill(sd, AM_LEARNINGPOTION) * 5;
  7854. // A potion produced by an Alchemist in the Fame Top 10 gets +50% effect [DracoRPG]
  7855. if (potion_flag == 2)
  7856. bonus += 50;
  7857. tmp = sp * bonus / 100; // Overflow check
  7858. if (bonus != 100 && tmp > sp)
  7859. sp = tmp;
  7860. }
  7861. if (sd->sc.count) {
  7862. // Critical Wound and Death Hurt stack
  7863. if (sd->sc.data[SC_CRITICALWOUND])
  7864. penalty += sd->sc.data[SC_CRITICALWOUND]->val2;
  7865. if (sd->sc.data[SC_DEATHHURT])
  7866. penalty += 20;
  7867. if (sd->sc.data[SC_NORECOVER_STATE])
  7868. penalty = 100;
  7869. if (sd->sc.data[SC_VITALITYACTIVATION]) {
  7870. hp += hp / 2; // 1.5 times
  7871. sp -= sp / 2;
  7872. }
  7873. if (sd->sc.data[SC_WATER_INSIGNIA] && sd->sc.data[SC_WATER_INSIGNIA]->val1 == 2) {
  7874. hp += hp / 10;
  7875. sp += sp / 10;
  7876. }
  7877. #ifdef RENEWAL
  7878. if (sd->sc.data[SC_APPLEIDUN])
  7879. hp += sd->sc.data[SC_APPLEIDUN]->val3 / 100;
  7880. #endif
  7881. if (penalty > 0) {
  7882. hp -= hp * penalty / 100;
  7883. sp -= sp * penalty / 100;
  7884. }
  7885. #ifdef RENEWAL
  7886. if (sd->sc.data[SC_EXTREMITYFIST2])
  7887. sp = 0;
  7888. #endif
  7889. if (sd->sc.data[SC_BITESCAR])
  7890. hp = 0;
  7891. }
  7892. return status_heal(&sd->bl, hp, sp, 1);
  7893. }
  7894. /*==========================================
  7895. * HP/SP Recovery
  7896. * Heal player hp nad/or sp by rate
  7897. *------------------------------------------*/
  7898. int pc_percentheal(struct map_session_data *sd,int hp,int sp)
  7899. {
  7900. nullpo_ret(sd);
  7901. if (hp > 100) hp = 100;
  7902. else if (hp <-100) hp = -100;
  7903. if (sp > 100) sp = 100;
  7904. else if (sp <-100) sp = -100;
  7905. if(hp >= 0 && sp >= 0) //Heal
  7906. return status_percent_heal(&sd->bl, hp, sp);
  7907. if(hp <= 0 && sp <= 0) //Damage (negative rates indicate % of max rather than current), and only kill target IF the specified amount is 100%
  7908. return status_percent_damage(NULL, &sd->bl, hp, sp, hp==-100);
  7909. //Crossed signs
  7910. if(hp) {
  7911. if(hp > 0)
  7912. status_percent_heal(&sd->bl, hp, 0);
  7913. else
  7914. status_percent_damage(NULL, &sd->bl, hp, 0, hp==-100);
  7915. }
  7916. if(sp) {
  7917. if(sp > 0)
  7918. status_percent_heal(&sd->bl, 0, sp);
  7919. else
  7920. status_percent_damage(NULL, &sd->bl, 0, sp, false);
  7921. }
  7922. return 0;
  7923. }
  7924. static int jobchange_killclone(struct block_list *bl, va_list ap)
  7925. {
  7926. struct mob_data *md;
  7927. int flag;
  7928. md = (struct mob_data *)bl;
  7929. nullpo_ret(md);
  7930. flag = va_arg(ap, int);
  7931. if (md->master_id && md->special_state.clone && md->master_id == flag)
  7932. status_kill(&md->bl);
  7933. return 1;
  7934. }
  7935. /**
  7936. * Called when player changes job
  7937. * Rewrote to make it tidider [Celest]
  7938. * @param sd
  7939. * @param job JOB ID. See enum e_job
  7940. * @param upper 1 - JOBL_UPPER; 2 - JOBL_BABY
  7941. * @return True if success, false if failed
  7942. **/
  7943. bool pc_jobchange(struct map_session_data *sd,int job, char upper)
  7944. {
  7945. int i, fame_flag = 0;
  7946. int b_class;
  7947. nullpo_retr(false,sd);
  7948. if (job < 0)
  7949. return false;
  7950. //Normalize job.
  7951. b_class = pc_jobid2mapid(job);
  7952. if (b_class == -1)
  7953. return false;
  7954. switch (upper) {
  7955. case 1:
  7956. b_class|= JOBL_UPPER;
  7957. break;
  7958. case 2:
  7959. b_class|= JOBL_BABY;
  7960. break;
  7961. }
  7962. //This will automatically adjust bard/dancer classes to the correct gender
  7963. //That is, if you try to jobchange into dancer, it will turn you to bard.
  7964. job = pc_mapid2jobid(b_class, sd->status.sex);
  7965. if (job == -1)
  7966. return false;
  7967. if ((unsigned short)b_class == sd->class_)
  7968. return false; //Nothing to change.
  7969. // changing from 1st to 2nd job
  7970. if ((b_class&JOBL_2) && !(sd->class_&JOBL_2) && (sd->class_&MAPID_UPPERMASK) != MAPID_SUPER_NOVICE) {
  7971. sd->change_level_2nd = sd->status.job_level;
  7972. pc_setglobalreg(sd, add_str(JOBCHANGE2ND_VAR), sd->change_level_2nd);
  7973. }
  7974. // changing from 2nd to 3rd job
  7975. else if((b_class&JOBL_THIRD) && !(sd->class_&JOBL_THIRD)) {
  7976. sd->change_level_3rd = sd->status.job_level;
  7977. pc_setglobalreg(sd, add_str(JOBCHANGE3RD_VAR), sd->change_level_3rd);
  7978. }
  7979. if(sd->cloneskill_idx > 0) {
  7980. if( sd->status.skill[sd->cloneskill_idx].flag == SKILL_FLAG_PLAGIARIZED ) {
  7981. sd->status.skill[sd->cloneskill_idx].id = 0;
  7982. sd->status.skill[sd->cloneskill_idx].lv = 0;
  7983. sd->status.skill[sd->cloneskill_idx].flag = SKILL_FLAG_PERMANENT;
  7984. clif_deleteskill(sd, static_cast<int>(pc_readglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM))));
  7985. }
  7986. sd->cloneskill_idx = 0;
  7987. pc_setglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM), 0);
  7988. pc_setglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM_LV), 0);
  7989. }
  7990. if(sd->reproduceskill_idx > 0) {
  7991. if( sd->status.skill[sd->reproduceskill_idx].flag == SKILL_FLAG_PLAGIARIZED ) {
  7992. sd->status.skill[sd->reproduceskill_idx].id = 0;
  7993. sd->status.skill[sd->reproduceskill_idx].lv = 0;
  7994. sd->status.skill[sd->reproduceskill_idx].flag = SKILL_FLAG_PERMANENT;
  7995. clif_deleteskill(sd, static_cast<int>(pc_readglobalreg(sd, add_str(SKILL_VAR_REPRODUCE))));
  7996. }
  7997. sd->reproduceskill_idx = 0;
  7998. pc_setglobalreg(sd, add_str(SKILL_VAR_REPRODUCE), 0);
  7999. pc_setglobalreg(sd, add_str(SKILL_VAR_REPRODUCE_LV), 0);
  8000. }
  8001. // Give or reduce transcendent status points
  8002. if( (b_class&JOBL_UPPER) && !(sd->class_&JOBL_UPPER) ){ // Change from a non t class to a t class -> give points
  8003. sd->status.status_point += battle_config.transcendent_status_points;
  8004. clif_updatestatus(sd,SP_STATUSPOINT);
  8005. }else if( !(b_class&JOBL_UPPER) && (sd->class_&JOBL_UPPER) ){ // Change from a t class to a non t class -> remove points
  8006. if( sd->status.status_point < battle_config.transcendent_status_points ){
  8007. // The player already used his bonus points, so we have to reset his status points
  8008. pc_resetstate(sd);
  8009. }
  8010. sd->status.status_point -= battle_config.transcendent_status_points;
  8011. clif_updatestatus(sd,SP_STATUSPOINT);
  8012. }
  8013. if ( (b_class&MAPID_UPPERMASK) != (sd->class_&MAPID_UPPERMASK) ) { //Things to remove when changing class tree.
  8014. const int class_ = pc_class2idx(sd->status.class_);
  8015. uint16 skill_id;
  8016. for(i = 0; i < MAX_SKILL_TREE && (skill_id = skill_tree[class_][i].skill_id) > 0; i++) {
  8017. //Remove status specific to your current tree skills.
  8018. enum sc_type sc = status_skill2sc(skill_id);
  8019. if (sc > SC_COMMON_MAX && sd->sc.data[sc])
  8020. status_change_end(&sd->bl, sc, INVALID_TIMER);
  8021. }
  8022. }
  8023. if( (sd->class_&MAPID_UPPERMASK) == MAPID_STAR_GLADIATOR && (b_class&MAPID_UPPERMASK) != MAPID_STAR_GLADIATOR) {
  8024. /* going off star glad lineage, reset feel to not store no-longer-used vars in the database */
  8025. pc_resetfeel(sd);
  8026. }
  8027. // Reset body style to 0 before changing job to avoid
  8028. // errors since not every job has a alternate outfit.
  8029. sd->status.body = 0;
  8030. clif_changelook(&sd->bl,LOOK_BODY2,0);
  8031. sd->status.class_ = job;
  8032. fame_flag = pc_famerank(sd->status.char_id,sd->class_&MAPID_UPPERMASK);
  8033. sd->class_ = (unsigned short)b_class;
  8034. sd->status.job_level=1;
  8035. sd->status.job_exp=0;
  8036. if (sd->status.base_level > pc_maxbaselv(sd)) {
  8037. sd->status.base_level = pc_maxbaselv(sd);
  8038. sd->status.base_exp=0;
  8039. pc_resetstate(sd);
  8040. clif_updatestatus(sd,SP_STATUSPOINT);
  8041. clif_updatestatus(sd,SP_BASELEVEL);
  8042. clif_updatestatus(sd,SP_BASEEXP);
  8043. clif_updatestatus(sd,SP_NEXTBASEEXP);
  8044. }
  8045. clif_updatestatus(sd,SP_JOBLEVEL);
  8046. clif_updatestatus(sd,SP_JOBEXP);
  8047. clif_updatestatus(sd,SP_NEXTJOBEXP);
  8048. for(i=0;i<EQI_MAX;i++) {
  8049. if(sd->equip_index[i] >= 0)
  8050. if(pc_isequip(sd,sd->equip_index[i]))
  8051. pc_unequipitem(sd,sd->equip_index[i],2); // unequip invalid item for class
  8052. }
  8053. //Change look, if disguised, you need to undisguise
  8054. //to correctly calculate new job sprite without
  8055. if (sd->disguise)
  8056. pc_disguise(sd, 0);
  8057. status_set_viewdata(&sd->bl, job);
  8058. clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_); // move sprite update to prevent client crashes with incompatible equipment [Valaris]
  8059. #if PACKETVER >= 20151001
  8060. clif_changelook(&sd->bl, LOOK_HAIR, sd->vd.hair_style); // Update player's head (only matters when switching to or from Doram)
  8061. #endif
  8062. if(sd->vd.cloth_color)
  8063. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  8064. /*
  8065. if(sd->vd.body_style)
  8066. clif_changelook(&sd->bl,LOOK_BODY2,sd->vd.body_style);
  8067. */
  8068. //Update skill tree.
  8069. pc_calc_skilltree(sd);
  8070. clif_skillinfoblock(sd);
  8071. if (sd->ed)
  8072. elemental_delete(sd->ed);
  8073. if (sd->state.vending)
  8074. vending_closevending(sd);
  8075. if (sd->state.buyingstore)
  8076. buyingstore_close(sd);
  8077. map_foreachinmap(jobchange_killclone, sd->bl.m, BL_MOB, sd->bl.id);
  8078. //Remove peco/cart/falcon
  8079. i = sd->sc.option;
  8080. if( i&OPTION_RIDING && !pc_checkskill(sd, KN_RIDING) )
  8081. i&=~OPTION_RIDING;
  8082. if( i&OPTION_FALCON && !pc_checkskill(sd, HT_FALCON) )
  8083. i&=~OPTION_FALCON;
  8084. if( i&OPTION_DRAGON && !pc_checkskill(sd,RK_DRAGONTRAINING) )
  8085. i&=~OPTION_DRAGON;
  8086. if( i&OPTION_WUGRIDER && !pc_checkskill(sd,RA_WUGMASTERY) )
  8087. i&=~OPTION_WUGRIDER;
  8088. if( i&OPTION_WUG && !pc_checkskill(sd,RA_WUGMASTERY) )
  8089. i&=~OPTION_WUG;
  8090. if( i&OPTION_MADOGEAR ) //You do not need a skill for this.
  8091. i&=~OPTION_MADOGEAR;
  8092. #ifndef NEW_CARTS
  8093. if( i&OPTION_CART && !pc_checkskill(sd, MC_PUSHCART) )
  8094. i&=~OPTION_CART;
  8095. #else
  8096. if( sd->sc.data[SC_PUSH_CART] && !pc_checkskill(sd, MC_PUSHCART) )
  8097. pc_setcart(sd, 0);
  8098. #endif
  8099. if(i != sd->sc.option)
  8100. pc_setoption(sd, i);
  8101. if(hom_is_active(sd->hd) && !pc_checkskill(sd, AM_CALLHOMUN))
  8102. hom_vaporize(sd, HOM_ST_ACTIVE);
  8103. if (sd->sc.data[SC_SPRITEMABLE] && !pc_checkskill(sd, SU_SPRITEMABLE))
  8104. status_change_end(&sd->bl, SC_SPRITEMABLE, INVALID_TIMER);
  8105. if(sd->status.manner < 0)
  8106. clif_changestatus(sd,SP_MANNER,sd->status.manner);
  8107. status_calc_pc(sd,SCO_FORCE);
  8108. pc_checkallowskill(sd);
  8109. pc_equiplookall(sd);
  8110. pc_show_questinfo(sd);
  8111. achievement_update_objective(sd, AG_JOB_CHANGE, 2, sd->status.base_level, job);
  8112. if( sd->status.party_id ){
  8113. struct party_data* p;
  8114. if( ( p = party_search( sd->status.party_id ) ) != NULL ){
  8115. ARR_FIND(0, MAX_PARTY, i, p->party.member[i].char_id == sd->status.char_id);
  8116. if( i < MAX_PARTY ){
  8117. p->party.member[i].class_ = sd->status.class_;
  8118. clif_party_job_and_level(sd);
  8119. }
  8120. }
  8121. }
  8122. chrif_save(sd, CSAVE_NORMAL);
  8123. //if you were previously famous, not anymore.
  8124. if (fame_flag)
  8125. chrif_buildfamelist();
  8126. else if (sd->status.fame > 0) {
  8127. //It may be that now they are famous?
  8128. switch (sd->class_&MAPID_UPPERMASK) {
  8129. case MAPID_BLACKSMITH:
  8130. case MAPID_ALCHEMIST:
  8131. case MAPID_TAEKWON:
  8132. chrif_buildfamelist();
  8133. break;
  8134. }
  8135. }
  8136. return true;
  8137. }
  8138. /*==========================================
  8139. * Tell client player sd has change equipement
  8140. *------------------------------------------*/
  8141. void pc_equiplookall(struct map_session_data *sd)
  8142. {
  8143. nullpo_retv(sd);
  8144. clif_changelook(&sd->bl,LOOK_WEAPON,0);
  8145. clif_changelook(&sd->bl,LOOK_SHOES,0);
  8146. clif_changelook(&sd->bl,LOOK_HEAD_BOTTOM,sd->status.head_bottom);
  8147. clif_changelook(&sd->bl,LOOK_HEAD_TOP,sd->status.head_top);
  8148. clif_changelook(&sd->bl,LOOK_HEAD_MID,sd->status.head_mid);
  8149. clif_changelook(&sd->bl,LOOK_ROBE, sd->status.robe);
  8150. }
  8151. /*==========================================
  8152. * Tell client player sd has change look (hair,equip...)
  8153. *------------------------------------------*/
  8154. void pc_changelook(struct map_session_data *sd,int type,int val) {
  8155. nullpo_retv(sd);
  8156. switch(type) {
  8157. case LOOK_HAIR: //Use the battle_config limits! [Skotlex]
  8158. val = cap_value(val, MIN_HAIR_STYLE, MAX_HAIR_STYLE);
  8159. if (sd->status.hair != val) {
  8160. sd->status.hair = val;
  8161. if (sd->status.guild_id) //Update Guild Window. [Skotlex]
  8162. intif_guild_change_memberinfo(sd->status.guild_id, sd->status.account_id, sd->status.char_id,
  8163. GMI_HAIR, &sd->status.hair, sizeof(sd->status.hair));
  8164. }
  8165. break;
  8166. case LOOK_WEAPON:
  8167. sd->status.weapon = val;
  8168. break;
  8169. case LOOK_HEAD_BOTTOM:
  8170. sd->status.head_bottom = val;
  8171. sd->setlook_head_bottom = val;
  8172. break;
  8173. case LOOK_HEAD_TOP:
  8174. sd->status.head_top = val;
  8175. sd->setlook_head_top = val;
  8176. break;
  8177. case LOOK_HEAD_MID:
  8178. sd->status.head_mid = val;
  8179. sd->setlook_head_mid = val;
  8180. break;
  8181. case LOOK_HAIR_COLOR: //Use the battle_config limits! [Skotlex]
  8182. val = cap_value(val, MIN_HAIR_COLOR, MAX_HAIR_COLOR);
  8183. if (sd->status.hair_color != val) {
  8184. sd->status.hair_color = val;
  8185. if (sd->status.guild_id) //Update Guild Window. [Skotlex]
  8186. intif_guild_change_memberinfo(sd->status.guild_id, sd->status.account_id, sd->status.char_id,
  8187. GMI_HAIR_COLOR, &sd->status.hair_color, sizeof(sd->status.hair_color));
  8188. }
  8189. break;
  8190. case LOOK_CLOTHES_COLOR: //Use the battle_config limits! [Skotlex]
  8191. val = cap_value(val, MIN_CLOTH_COLOR, MAX_CLOTH_COLOR);
  8192. sd->status.clothes_color = val;
  8193. break;
  8194. case LOOK_SHIELD:
  8195. sd->status.shield = val;
  8196. break;
  8197. case LOOK_SHOES:
  8198. break;
  8199. case LOOK_ROBE:
  8200. sd->status.robe = val;
  8201. sd->setlook_robe = val;
  8202. break;
  8203. case LOOK_BODY2:
  8204. val = cap_value(val, MIN_BODY_STYLE, MAX_BODY_STYLE);
  8205. sd->status.body = val;
  8206. break;
  8207. }
  8208. clif_changelook(&sd->bl, type, val);
  8209. }
  8210. /*==========================================
  8211. * Give an option (type) to player (sd) and display it to client
  8212. *------------------------------------------*/
  8213. void pc_setoption(struct map_session_data *sd,int type)
  8214. {
  8215. int p_type, new_look=0;
  8216. nullpo_retv(sd);
  8217. p_type = sd->sc.option;
  8218. //Option has to be changed client-side before the class sprite or it won't always work (eg: Wedding sprite) [Skotlex]
  8219. sd->sc.option=type;
  8220. clif_changeoption(&sd->bl);
  8221. if( (type&OPTION_RIDING && !(p_type&OPTION_RIDING)) || (type&OPTION_DRAGON && !(p_type&OPTION_DRAGON) && pc_checkskill(sd,RK_DRAGONTRAINING) > 0) )
  8222. { // Mounting
  8223. clif_status_load(&sd->bl,EFST_RIDING,1);
  8224. status_calc_pc(sd,SCO_NONE);
  8225. }
  8226. else if( (!(type&OPTION_RIDING) && p_type&OPTION_RIDING) || (!(type&OPTION_DRAGON) && p_type&OPTION_DRAGON && pc_checkskill(sd,RK_DRAGONTRAINING) > 0) )
  8227. { // Dismount
  8228. clif_status_load(&sd->bl,EFST_RIDING,0);
  8229. status_calc_pc(sd,SCO_NONE);
  8230. }
  8231. #ifndef NEW_CARTS
  8232. if( type&OPTION_CART && !( p_type&OPTION_CART ) ) { //Cart On
  8233. clif_cartlist(sd);
  8234. clif_updatestatus(sd, SP_CARTINFO);
  8235. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  8236. status_calc_pc(sd,SCO_NONE); //Apply speed penalty.
  8237. } else if( !( type&OPTION_CART ) && p_type&OPTION_CART ){ //Cart Off
  8238. clif_clearcart(sd->fd);
  8239. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  8240. status_calc_pc(sd,SCO_NONE); //Remove speed penalty.
  8241. }
  8242. #endif
  8243. if (type&OPTION_FALCON && !(p_type&OPTION_FALCON)) //Falcon ON
  8244. clif_status_load(&sd->bl,EFST_FALCON,1);
  8245. else if (!(type&OPTION_FALCON) && p_type&OPTION_FALCON) //Falcon OFF
  8246. clif_status_load(&sd->bl,EFST_FALCON,0);
  8247. if( (sd->class_&MAPID_THIRDMASK) == MAPID_RANGER ) {
  8248. if( type&OPTION_WUGRIDER && !(p_type&OPTION_WUGRIDER) ) { // Mounting
  8249. clif_status_load(&sd->bl,EFST_WUGRIDER,1);
  8250. status_calc_pc(sd,SCO_NONE);
  8251. } else if( !(type&OPTION_WUGRIDER) && p_type&OPTION_WUGRIDER ) { // Dismount
  8252. clif_status_load(&sd->bl,EFST_WUGRIDER,0);
  8253. status_calc_pc(sd,SCO_NONE);
  8254. }
  8255. }
  8256. if( (sd->class_&MAPID_THIRDMASK) == MAPID_MECHANIC ) {
  8257. if( type&OPTION_MADOGEAR && !(p_type&OPTION_MADOGEAR) ) {
  8258. static const sc_type statuses [] = { SC_MAXIMIZEPOWER, SC_OVERTHRUST, SC_WEAPONPERFECTION, SC_ADRENALINE, SC_CARTBOOST, SC_MELTDOWN, SC_MAXOVERTHRUST };
  8259. status_calc_pc(sd,SCO_NONE);
  8260. for (uint8 i = 0; i < ARRAYLENGTH(statuses); i++) {
  8261. int skill_id = status_sc2skill(statuses[i]);
  8262. if (skill_id > 0 && !skill_get_inf2(skill_id, INF2_ALLOWONMADO))
  8263. status_change_end(&sd->bl,statuses[i],INVALID_TIMER);
  8264. }
  8265. pc_bonus_script_clear(sd,BSF_REM_ON_MADOGEAR);
  8266. } else if( !(type&OPTION_MADOGEAR) && p_type&OPTION_MADOGEAR ) {
  8267. status_calc_pc(sd,SCO_NONE);
  8268. status_change_end(&sd->bl,SC_SHAPESHIFT,INVALID_TIMER);
  8269. status_change_end(&sd->bl,SC_HOVERING,INVALID_TIMER);
  8270. status_change_end(&sd->bl,SC_ACCELERATION,INVALID_TIMER);
  8271. status_change_end(&sd->bl,SC_OVERHEAT_LIMITPOINT,INVALID_TIMER);
  8272. status_change_end(&sd->bl,SC_OVERHEAT,INVALID_TIMER);
  8273. status_change_end(&sd->bl,SC_MAGNETICFIELD,INVALID_TIMER);
  8274. status_change_end(&sd->bl,SC_NEUTRALBARRIER_MASTER,INVALID_TIMER);
  8275. status_change_end(&sd->bl,SC_STEALTHFIELD_MASTER,INVALID_TIMER);
  8276. pc_bonus_script_clear(sd,BSF_REM_ON_MADOGEAR);
  8277. }
  8278. }
  8279. if (type&OPTION_FLYING && !(p_type&OPTION_FLYING))
  8280. new_look = JOB_STAR_GLADIATOR2;
  8281. else if (!(type&OPTION_FLYING) && p_type&OPTION_FLYING)
  8282. new_look = -1;
  8283. if (sd->disguise || !new_look)
  8284. return; //Disguises break sprite changes
  8285. if (new_look < 0) { //Restore normal look.
  8286. status_set_viewdata(&sd->bl, sd->status.class_);
  8287. new_look = sd->vd.class_;
  8288. }
  8289. pc_stop_attack(sd); //Stop attacking on new view change (to prevent wedding/santa attacks.
  8290. clif_changelook(&sd->bl,LOOK_BASE,new_look);
  8291. if (sd->vd.cloth_color)
  8292. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  8293. if( sd->vd.body_style )
  8294. clif_changelook(&sd->bl,LOOK_BODY2,sd->vd.body_style);
  8295. clif_skillinfoblock(sd); // Skill list needs to be updated after base change.
  8296. }
  8297. /**
  8298. * Give player a cart
  8299. * @param sd Player
  8300. * @param type 0:Remove cart, 1 ~ MAX_CARTS: Cart type
  8301. **/
  8302. bool pc_setcart(struct map_session_data *sd,int type) {
  8303. #ifndef NEW_CARTS
  8304. int cart[6] = {0x0000,OPTION_CART1,OPTION_CART2,OPTION_CART3,OPTION_CART4,OPTION_CART5};
  8305. int option;
  8306. #endif
  8307. nullpo_retr(false,sd);
  8308. if( type < 0 || type > MAX_CARTS )
  8309. return false;// Never trust the values sent by the client! [Skotlex]
  8310. if( pc_checkskill(sd,MC_PUSHCART) <= 0 && type != 0 )
  8311. return false;// Push cart is required
  8312. #ifdef NEW_CARTS
  8313. switch( type ) {
  8314. case 0:
  8315. if( !sd->sc.data[SC_PUSH_CART] )
  8316. return 0;
  8317. status_change_end(&sd->bl,SC_PUSH_CART,INVALID_TIMER);
  8318. clif_clearcart(sd->fd);
  8319. break;
  8320. default:/* everything else is an allowed ID so we can move on */
  8321. if( !sd->sc.data[SC_PUSH_CART] ) { /* first time, so fill cart data */
  8322. clif_cartlist(sd);
  8323. status_calc_cart_weight(sd, (e_status_calc_weight_opt)(CALCWT_ITEM|CALCWT_MAXBONUS|CALCWT_CARTSTATE));
  8324. }
  8325. clif_updatestatus(sd, SP_CARTINFO);
  8326. sc_start(&sd->bl, &sd->bl, SC_PUSH_CART, 100, type, 0);
  8327. break;
  8328. }
  8329. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  8330. status_calc_pc(sd,SCO_NONE); //Recalc speed penalty.
  8331. #else
  8332. // Update option
  8333. option = sd->sc.option;
  8334. option &= ~OPTION_CART;// clear cart bits
  8335. option |= cart[type]; // set cart
  8336. pc_setoption(sd, option);
  8337. #endif
  8338. return true;
  8339. }
  8340. /*==========================================
  8341. * Give player a falcon
  8342. *------------------------------------------*/
  8343. void pc_setfalcon(struct map_session_data* sd, int flag)
  8344. {
  8345. if( flag ){
  8346. if( pc_checkskill(sd,HT_FALCON)>0 ) // add falcon if he have the skill
  8347. pc_setoption(sd,sd->sc.option|OPTION_FALCON);
  8348. } else if( pc_isfalcon(sd) ){
  8349. pc_setoption(sd,sd->sc.option&~OPTION_FALCON); // remove falcon
  8350. }
  8351. }
  8352. /*==========================================
  8353. * Set player riding
  8354. *------------------------------------------*/
  8355. void pc_setriding(struct map_session_data* sd, int flag)
  8356. {
  8357. if( sd->sc.data[SC_ALL_RIDING] )
  8358. return;
  8359. if( flag ){
  8360. if( pc_checkskill(sd,KN_RIDING) > 0 ) // add peco
  8361. pc_setoption(sd, sd->sc.option|OPTION_RIDING);
  8362. } else if( pc_isriding(sd) ){
  8363. pc_setoption(sd, sd->sc.option&~OPTION_RIDING);
  8364. }
  8365. }
  8366. /*==========================================
  8367. * Give player a mado
  8368. *------------------------------------------*/
  8369. void pc_setmadogear(struct map_session_data* sd, int flag)
  8370. {
  8371. if( flag ){
  8372. if( pc_checkskill(sd,NC_MADOLICENCE) > 0 )
  8373. pc_setoption(sd, sd->sc.option|OPTION_MADOGEAR);
  8374. } else if( pc_ismadogear(sd) ){
  8375. pc_setoption(sd, sd->sc.option&~OPTION_MADOGEAR);
  8376. }
  8377. }
  8378. /*==========================================
  8379. * Check if player can drop an item
  8380. *------------------------------------------*/
  8381. bool pc_candrop(struct map_session_data *sd, struct item *item)
  8382. {
  8383. if( item && ((item->expire_time || (item->bound && !pc_can_give_bounded_items(sd))) || (itemdb_ishatched_egg(item))) )
  8384. return false;
  8385. if( !pc_can_give_items(sd) || sd->sc.cant.drop) //check if this GM level can drop items
  8386. return false;
  8387. return (itemdb_isdropable(item, pc_get_group_level(sd)));
  8388. }
  8389. /**
  8390. * Determines whether a player can attack based on status changes
  8391. * Why not use status_check_skilluse?
  8392. * "src MAY be null to indicate we shouldn't check it, this is a ground-based skill attack."
  8393. * Even ground-based attacks should be blocked by these statuses
  8394. * Called from unit_attack and unit_attack_timer_sub
  8395. * @retval true Can attack
  8396. **/
  8397. bool pc_can_attack( struct map_session_data *sd, int target_id ) {
  8398. nullpo_retr(false, sd);
  8399. if( pc_is90overweight(sd) || pc_isridingwug(sd) )
  8400. return false;
  8401. if (sd->state.block_action & PCBLOCK_ATTACK)
  8402. return false;
  8403. if(
  8404. #ifdef RENEWAL
  8405. sd->sc.data[SC_BASILICA_CELL] ||
  8406. #else
  8407. sd->sc.data[SC_BASILICA] ||
  8408. #endif
  8409. sd->sc.data[SC__SHADOWFORM] ||
  8410. sd->sc.data[SC_CURSEDCIRCLE_ATKER] ||
  8411. sd->sc.data[SC_CURSEDCIRCLE_TARGET] ||
  8412. sd->sc.data[SC_CRYSTALIZE] ||
  8413. sd->sc.data[SC_ALL_RIDING] || // The client doesn't let you, this is to make cheat-safe
  8414. sd->sc.data[SC_TRICKDEAD] ||
  8415. (sd->sc.data[SC_VOICEOFSIREN] && sd->sc.data[SC_VOICEOFSIREN]->val2 == target_id) ||
  8416. sd->sc.data[SC_BLADESTOP] ||
  8417. sd->sc.data[SC_DEEPSLEEP] ||
  8418. (sd->sc.data[SC_GRAVITATION] && sd->sc.data[SC_GRAVITATION]->val3 == BCT_SELF) ||
  8419. sd->sc.data[SC_KINGS_GRACE] )
  8420. return false;
  8421. return true;
  8422. }
  8423. /*==========================================
  8424. * Read '@type' variables (temporary numeric char reg)
  8425. *------------------------------------------*/
  8426. int64 pc_readreg(struct map_session_data* sd, int64 reg)
  8427. {
  8428. return i64db_i64get(sd->regs.vars, reg);
  8429. }
  8430. /*==========================================
  8431. * Set '@type' variables (temporary numeric char reg)
  8432. *------------------------------------------*/
  8433. bool pc_setreg(struct map_session_data* sd, int64 reg, int64 val)
  8434. {
  8435. uint32 index = script_getvaridx(reg);
  8436. nullpo_retr(false, sd);
  8437. if( val ) {
  8438. i64db_i64put(sd->regs.vars, reg, val);
  8439. if( index )
  8440. script_array_update(&sd->regs, reg, false);
  8441. } else {
  8442. i64db_remove(sd->regs.vars, reg);
  8443. if( index )
  8444. script_array_update(&sd->regs, reg, true);
  8445. }
  8446. return true;
  8447. }
  8448. /*==========================================
  8449. * Read '@type$' variables (temporary string char reg)
  8450. *------------------------------------------*/
  8451. char* pc_readregstr(struct map_session_data* sd, int64 reg)
  8452. {
  8453. struct script_reg_str *p = NULL;
  8454. p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg);
  8455. return p ? p->value : NULL;
  8456. }
  8457. /*==========================================
  8458. * Set '@type$' variables (temporary string char reg)
  8459. *------------------------------------------*/
  8460. bool pc_setregstr(struct map_session_data* sd, int64 reg, const char* str)
  8461. {
  8462. struct script_reg_str *p = NULL;
  8463. unsigned int index = script_getvaridx(reg);
  8464. DBData prev;
  8465. nullpo_retr(false, sd);
  8466. if( str[0] ) {
  8467. p = ers_alloc(str_reg_ers, struct script_reg_str);
  8468. p->value = aStrdup(str);
  8469. p->flag.type = 1;
  8470. if (sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev)) {
  8471. p = (struct script_reg_str *)db_data2ptr(&prev);
  8472. if( p->value )
  8473. aFree(p->value);
  8474. ers_free(str_reg_ers, p);
  8475. } else {
  8476. if( index )
  8477. script_array_update(&sd->regs, reg, false);
  8478. }
  8479. } else {
  8480. if (sd->regs.vars->remove(sd->regs.vars, db_i642key(reg), &prev)) {
  8481. p = (struct script_reg_str *)db_data2ptr(&prev);
  8482. if( p->value )
  8483. aFree(p->value);
  8484. ers_free(str_reg_ers, p);
  8485. if( index )
  8486. script_array_update(&sd->regs, reg, true);
  8487. }
  8488. }
  8489. return true;
  8490. }
  8491. /**
  8492. * Serves the following variable types:
  8493. * - 'type' (permanent numeric char reg)
  8494. * - '#type' (permanent numeric account reg)
  8495. * - '##type' (permanent numeric account reg2)
  8496. **/
  8497. int64 pc_readregistry(struct map_session_data *sd, int64 reg)
  8498. {
  8499. struct script_reg_num *p = NULL;
  8500. if (!sd->vars_ok) {
  8501. ShowError("pc_readregistry: Trying to read reg %s before it's been loaded!\n", get_str(script_getvarid(reg)));
  8502. //This really shouldn't happen, so it's possible the data was lost somewhere, we should request it again.
  8503. //intif->request_registry(sd,type==3?4:type);
  8504. set_eof(sd->fd);
  8505. return 0;
  8506. }
  8507. p = (struct script_reg_num *)i64db_get(sd->regs.vars, reg);
  8508. return p ? p->value : 0;
  8509. }
  8510. /**
  8511. * Serves the following variable types:
  8512. * - 'type$' (permanent str char reg)
  8513. * - '#type$' (permanent str account reg)
  8514. * - '##type$' (permanent str account reg2)
  8515. **/
  8516. char* pc_readregistry_str(struct map_session_data *sd, int64 reg)
  8517. {
  8518. struct script_reg_str *p = NULL;
  8519. if (!sd->vars_ok) {
  8520. ShowError("pc_readregistry_str: Trying to read reg %s before it's been loaded!\n", get_str(script_getvarid(reg)));
  8521. //This really shouldn't happen, so it's possible the data was lost somewhere, we should request it again.
  8522. //intif->request_registry(sd,type==3?4:type);
  8523. set_eof(sd->fd);
  8524. return NULL;
  8525. }
  8526. p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg);
  8527. return p ? p->value : NULL;
  8528. }
  8529. /**
  8530. * Serves the following variable types:
  8531. * - 'type' (permanent numeric char reg)
  8532. * - '#type' (permanent numeric account reg)
  8533. * - '##type' (permanent numeric account reg2)
  8534. **/
  8535. bool pc_setregistry(struct map_session_data *sd, int64 reg, int64 val)
  8536. {
  8537. struct script_reg_num *p = NULL;
  8538. const char *regname = get_str(script_getvarid(reg));
  8539. uint32 index = script_getvaridx(reg);
  8540. if ( !reg_load && !sd->vars_ok ) {
  8541. ShowError("pc_setregistry : refusing to set %s until vars are received.\n", regname);
  8542. return false;
  8543. }
  8544. if ((p = (struct script_reg_num *)i64db_get(sd->regs.vars, reg))) {
  8545. if( val ) {
  8546. if( !p->value && index ) /* its a entry that was deleted, so we reset array */
  8547. script_array_update(&sd->regs, reg, false);
  8548. p->value = val;
  8549. } else {
  8550. p->value = 0;
  8551. if( index )
  8552. script_array_update(&sd->regs, reg, true);
  8553. }
  8554. if (!reg_load)
  8555. p->flag.update = 1;/* either way, it will require either delete or replace */
  8556. } else if( val ) {
  8557. DBData prev;
  8558. if( index )
  8559. script_array_update(&sd->regs, reg, false);
  8560. p = ers_alloc(num_reg_ers, struct script_reg_num);
  8561. p->value = val;
  8562. if (!reg_load)
  8563. p->flag.update = 1;
  8564. if (sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev)) {
  8565. p = (struct script_reg_num *)db_data2ptr(&prev);
  8566. ers_free(num_reg_ers, p);
  8567. }
  8568. }
  8569. if (!reg_load && p)
  8570. sd->vars_dirty = true;
  8571. return true;
  8572. }
  8573. /**
  8574. * Serves the following variable types:
  8575. * - 'type$' (permanent str char reg)
  8576. * - '#type$' (permanent str account reg)
  8577. * - '##type$' (permanent str account reg2)
  8578. **/
  8579. bool pc_setregistry_str(struct map_session_data *sd, int64 reg, const char *val)
  8580. {
  8581. struct script_reg_str *p = NULL;
  8582. const char *regname = get_str(script_getvarid(reg));
  8583. unsigned int index = script_getvaridx(reg);
  8584. size_t vlen = 0;
  8585. if (!reg_load && !sd->vars_ok) {
  8586. ShowError("pc_setregistry_str : refusing to set %s until vars are received.\n", regname);
  8587. return false;
  8588. }
  8589. if ( !script_check_RegistryVariableLength(1, val, &vlen ) )
  8590. {
  8591. 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);
  8592. return false;
  8593. }
  8594. if( (p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg) ) ) {
  8595. if( val[0] ) {
  8596. if( p->value )
  8597. aFree(p->value);
  8598. else if ( index ) // an entry that was deleted, so we reset
  8599. script_array_update(&sd->regs, reg, false);
  8600. p->value = aStrdup(val);
  8601. } else {
  8602. if (p->value)
  8603. aFree(p->value);
  8604. p->value = NULL;
  8605. if( index )
  8606. script_array_update(&sd->regs, reg, true);
  8607. }
  8608. if( !reg_load )
  8609. p->flag.update = 1; // either way, it will require either delete or replace
  8610. } else if( val[0] ) {
  8611. DBData prev;
  8612. if( index )
  8613. script_array_update(&sd->regs, reg, false);
  8614. p = ers_alloc(str_reg_ers, struct script_reg_str);
  8615. p->value = aStrdup(val);
  8616. if( !reg_load )
  8617. p->flag.update = 1;
  8618. p->flag.type = 1;
  8619. if( sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev) ) {
  8620. p = (struct script_reg_str *)db_data2ptr(&prev);
  8621. if( p->value )
  8622. aFree(p->value);
  8623. ers_free(str_reg_ers, p);
  8624. }
  8625. }
  8626. if( !reg_load && p )
  8627. sd->vars_dirty = true;
  8628. return true;
  8629. }
  8630. /**
  8631. * Set value of player variable
  8632. * @param sd Player
  8633. * @param reg Variable name
  8634. * @param value
  8635. * @return True if success, false if failed.
  8636. **/
  8637. bool pc_setreg2(struct map_session_data *sd, const char *reg, int64 val) {
  8638. char prefix = reg[0];
  8639. nullpo_retr(false, sd);
  8640. if (reg[strlen(reg)-1] == '$') {
  8641. ShowError("pc_setreg2: Invalid variable scope '%s'\n", reg);
  8642. return false;
  8643. }
  8644. val = cap_value(val, INT_MIN, INT_MAX);
  8645. switch (prefix) {
  8646. case '.':
  8647. case '\'':
  8648. case '$':
  8649. ShowError("pc_setreg2: Invalid variable scope '%s'\n", reg);
  8650. return false;
  8651. case '@':
  8652. return pc_setreg(sd, add_str(reg), val);
  8653. case '#':
  8654. return (reg[1] == '#') ? pc_setaccountreg2(sd, add_str(reg), val) : pc_setaccountreg(sd, add_str(reg), val);
  8655. default:
  8656. return pc_setglobalreg(sd, add_str(reg), val);
  8657. }
  8658. return false;
  8659. }
  8660. /**
  8661. * Get value of player variable
  8662. * @param sd Player
  8663. * @param reg Variable name
  8664. * @return Variable value or 0 if failed.
  8665. **/
  8666. int64 pc_readreg2(struct map_session_data *sd, const char *reg) {
  8667. char prefix = reg[0];
  8668. nullpo_ret(sd);
  8669. if (reg[strlen(reg)-1] == '$') {
  8670. ShowError("pc_readreg2: Invalid variable scope '%s'\n", reg);
  8671. return 0;
  8672. }
  8673. switch (prefix) {
  8674. case '.':
  8675. case '\'':
  8676. case '$':
  8677. ShowError("pc_readreg2: Invalid variable scope '%s'\n", reg);
  8678. return 0;
  8679. case '@':
  8680. return pc_readreg(sd, add_str(reg));
  8681. case '#':
  8682. return (reg[1] == '#') ? pc_readaccountreg2(sd, add_str(reg)) : pc_readaccountreg(sd, add_str(reg));
  8683. default:
  8684. return pc_readglobalreg(sd, add_str(reg));
  8685. }
  8686. return 0;
  8687. }
  8688. /*==========================================
  8689. * Exec eventtimer for player sd (retrieved from map_session (id))
  8690. *------------------------------------------*/
  8691. static TIMER_FUNC(pc_eventtimer){
  8692. struct map_session_data *sd=map_id2sd(id);
  8693. char *p = (char *)data;
  8694. int i;
  8695. if(sd==NULL)
  8696. return 0;
  8697. ARR_FIND( 0, MAX_EVENTTIMER, i, sd->eventtimer[i] == tid );
  8698. if( i < MAX_EVENTTIMER )
  8699. {
  8700. sd->eventtimer[i] = INVALID_TIMER;
  8701. sd->eventcount--;
  8702. npc_event(sd,p,0);
  8703. }
  8704. else
  8705. ShowError("pc_eventtimer: no such event timer\n");
  8706. if (p) aFree(p);
  8707. return 0;
  8708. }
  8709. /*==========================================
  8710. * Add eventtimer for player sd ?
  8711. *------------------------------------------*/
  8712. bool pc_addeventtimer(struct map_session_data *sd,int tick,const char *name)
  8713. {
  8714. int i;
  8715. nullpo_retr(false,sd);
  8716. ARR_FIND( 0, MAX_EVENTTIMER, i, sd->eventtimer[i] == INVALID_TIMER );
  8717. if( i == MAX_EVENTTIMER )
  8718. return false;
  8719. sd->eventtimer[i] = add_timer(gettick()+tick, pc_eventtimer, sd->bl.id, (intptr_t)aStrdup(name));
  8720. sd->eventcount++;
  8721. return true;
  8722. }
  8723. /*==========================================
  8724. * Del eventtimer for player sd ?
  8725. *------------------------------------------*/
  8726. bool pc_deleventtimer(struct map_session_data *sd,const char *name)
  8727. {
  8728. char* p = NULL;
  8729. int i;
  8730. nullpo_retr(false,sd);
  8731. if (sd->eventcount == 0)
  8732. return false;
  8733. // find the named event timer
  8734. ARR_FIND( 0, MAX_EVENTTIMER, i,
  8735. sd->eventtimer[i] != INVALID_TIMER &&
  8736. (p = (char *)(get_timer(sd->eventtimer[i])->data)) != NULL &&
  8737. strcmp(p, name) == 0
  8738. );
  8739. if( i == MAX_EVENTTIMER )
  8740. return false; // not found
  8741. delete_timer(sd->eventtimer[i],pc_eventtimer);
  8742. sd->eventtimer[i] = INVALID_TIMER;
  8743. if(sd->eventcount > 0)
  8744. sd->eventcount--;
  8745. aFree(p);
  8746. return true;
  8747. }
  8748. /*==========================================
  8749. * Update eventtimer count for player sd
  8750. *------------------------------------------*/
  8751. void pc_addeventtimercount(struct map_session_data *sd,const char *name,int tick)
  8752. {
  8753. int i;
  8754. nullpo_retv(sd);
  8755. for(i=0;i<MAX_EVENTTIMER;i++)
  8756. if( sd->eventtimer[i] != INVALID_TIMER && strcmp(
  8757. (char *)(get_timer(sd->eventtimer[i])->data), name)==0 ){
  8758. addt_tickimer(sd->eventtimer[i],tick);
  8759. break;
  8760. }
  8761. }
  8762. /*==========================================
  8763. * Remove all eventtimer for player sd
  8764. *------------------------------------------*/
  8765. void pc_cleareventtimer(struct map_session_data *sd)
  8766. {
  8767. int i;
  8768. nullpo_retv(sd);
  8769. if (sd->eventcount == 0)
  8770. return;
  8771. for(i=0;i<MAX_EVENTTIMER;i++){
  8772. if( sd->eventtimer[i] != INVALID_TIMER ){
  8773. char *p = (char *)(get_timer(sd->eventtimer[i])->data);
  8774. delete_timer(sd->eventtimer[i],pc_eventtimer);
  8775. sd->eventtimer[i] = INVALID_TIMER;
  8776. if(sd->eventcount > 0) //avoid looping to max val
  8777. sd->eventcount--;
  8778. if (p) aFree(p);
  8779. }
  8780. }
  8781. }
  8782. /**
  8783. * Called when an item with combo is worn
  8784. * @param *sd
  8785. * @param *data struct item_data
  8786. * @return success numbers of succeed combo
  8787. */
  8788. static int pc_checkcombo(struct map_session_data *sd, struct item_data *data) {
  8789. uint16 i;
  8790. int success = 0;
  8791. for( i = 0; i < data->combos_count; i++ ) {
  8792. struct itemchk {
  8793. int idx;
  8794. unsigned short nameid;
  8795. short card[MAX_SLOTS];
  8796. } *combo_idx;
  8797. int idx, j;
  8798. int nb_itemCombo;
  8799. unsigned int pos = 0;
  8800. /* ensure this isn't a duplicate combo */
  8801. if( sd->combos.bonus != NULL ) {
  8802. int x;
  8803. ARR_FIND( 0, sd->combos.count, x, sd->combos.id[x] == data->combos[i]->id );
  8804. /* found a match, skip this combo */
  8805. if( x < sd->combos.count )
  8806. continue;
  8807. }
  8808. nb_itemCombo = data->combos[i]->count;
  8809. if(nb_itemCombo<2) //a combo with less then 2 item ?? how that possible
  8810. continue;
  8811. CREATE(combo_idx,struct itemchk,nb_itemCombo);
  8812. for(j=0; j < nb_itemCombo; j++){
  8813. combo_idx[j].idx=-1;
  8814. combo_idx[j].nameid=-1;
  8815. memset(combo_idx[j].card,-1,MAX_SLOTS);
  8816. }
  8817. for( j = 0; j < nb_itemCombo; j++ ) {
  8818. uint16 id = data->combos[i]->nameid[j], k;
  8819. bool found = false;
  8820. for( k = 0; k < EQI_MAX; k++ ) {
  8821. short index = sd->equip_index[k];
  8822. if( index < 0 )
  8823. continue;
  8824. if( pc_is_same_equip_index((enum equip_index)k, sd->equip_index, index) )
  8825. continue;
  8826. if (!sd->inventory_data[index] )
  8827. continue;
  8828. if ( itemdb_type(id) != IT_CARD ) {
  8829. if ( sd->inventory_data[index]->nameid != id )
  8830. continue;
  8831. if(j>0){ //check if this item not already used
  8832. bool do_continue = false; //used to continue that specific loop with some check that also use some loop
  8833. uint8 z;
  8834. for (z = 0; z < nb_itemCombo-1; z++)
  8835. if(combo_idx[z].idx == index && combo_idx[z].nameid == id) //we already have that index recorded
  8836. do_continue=true;
  8837. if(do_continue)
  8838. continue;
  8839. }
  8840. combo_idx[j].nameid = id;
  8841. combo_idx[j].idx = index;
  8842. pos |= sd->inventory.u.items_inventory[index].equip;
  8843. found = true;
  8844. break;
  8845. } else { //Cards and enchants
  8846. uint16 z;
  8847. if ( itemdb_isspecial(sd->inventory.u.items_inventory[index].card[0]) )
  8848. continue;
  8849. for (z = 0; z < MAX_SLOTS; z++) {
  8850. bool do_continue=false;
  8851. if (sd->inventory.u.items_inventory[index].card[z] != id)
  8852. continue;
  8853. if(j>0){
  8854. int c1, c2;
  8855. for (c1 = 0; c1 < nb_itemCombo-1; c1++){
  8856. if(combo_idx[c1].idx == index && combo_idx[c1].nameid == id){
  8857. for (c2 = 0; c2 < MAX_SLOTS; c2++){
  8858. if(combo_idx[c1].card[c2] == id){ //we already have that card recorded (at this same idx)
  8859. do_continue = true;
  8860. break;
  8861. }
  8862. }
  8863. }
  8864. }
  8865. }
  8866. if(do_continue)
  8867. continue;
  8868. combo_idx[j].nameid = id;
  8869. combo_idx[j].idx = index;
  8870. combo_idx[j].card[z] = id;
  8871. pos |= sd->inventory.u.items_inventory[index].equip;
  8872. found = true;
  8873. break;
  8874. }
  8875. }
  8876. }
  8877. if( !found )
  8878. break;/* we haven't found all the ids for this combo, so we can return */
  8879. }
  8880. aFree(combo_idx);
  8881. /* means we broke out of the count loop w/o finding all ids, we can move to the next combo */
  8882. if( j < nb_itemCombo )
  8883. continue;
  8884. /* we got here, means all items in the combo are matching */
  8885. idx = sd->combos.count;
  8886. if( sd->combos.bonus == NULL ) {
  8887. CREATE(sd->combos.bonus, struct script_code *, 1);
  8888. CREATE(sd->combos.id, unsigned short, 1);
  8889. CREATE(sd->combos.pos, unsigned int, 1);
  8890. sd->combos.count = 1;
  8891. } else {
  8892. RECREATE(sd->combos.bonus, struct script_code *, ++sd->combos.count);
  8893. RECREATE(sd->combos.id, unsigned short, sd->combos.count);
  8894. RECREATE(sd->combos.pos, unsigned int, sd->combos.count);
  8895. }
  8896. /* we simply copy the pointer */
  8897. sd->combos.bonus[idx] = data->combos[i]->script;
  8898. /* save this combo's id */
  8899. sd->combos.id[idx] = data->combos[i]->id;
  8900. /* save pos of combo*/
  8901. sd->combos.pos[idx] = pos;
  8902. success++;
  8903. }
  8904. return success;
  8905. }
  8906. /**
  8907. * Called when an item with combo is removed
  8908. * @param *sd
  8909. * @param *data struct item_data
  8910. * @return retval numbers of removed combo
  8911. */
  8912. static int pc_removecombo(struct map_session_data *sd, struct item_data *data ) {
  8913. int i, retval = 0;
  8914. if( sd->combos.bonus == NULL )
  8915. return 0;/* nothing to do here, player has no combos */
  8916. for( i = 0; i < data->combos_count; i++ ) {
  8917. /* check if this combo exists in this user */
  8918. int x = 0, cursor = 0, j;
  8919. ARR_FIND( 0, sd->combos.count, x, sd->combos.id[x] == data->combos[i]->id );
  8920. /* no match, skip this combo */
  8921. if(x >= sd->combos.count)
  8922. continue;
  8923. sd->combos.bonus[x] = NULL;
  8924. sd->combos.id[x] = 0;
  8925. sd->combos.pos[x] = 0;
  8926. retval++;
  8927. /* check if combo requirements still fit */
  8928. if( pc_checkcombo( sd, data ) )
  8929. continue;
  8930. /* move next value to empty slot */
  8931. for( j = 0, cursor = 0; j < sd->combos.count; j++ ) {
  8932. if( sd->combos.bonus[j] == NULL )
  8933. continue;
  8934. if( cursor != j ) {
  8935. sd->combos.bonus[cursor] = sd->combos.bonus[j];
  8936. sd->combos.id[cursor] = sd->combos.id[j];
  8937. sd->combos.pos[cursor] = sd->combos.pos[j];
  8938. }
  8939. cursor++;
  8940. }
  8941. /* it's empty, we can clear all the memory */
  8942. if( (sd->combos.count = cursor) == 0 ) {
  8943. aFree(sd->combos.bonus);
  8944. aFree(sd->combos.id);
  8945. aFree(sd->combos.pos);
  8946. sd->combos.bonus = NULL;
  8947. sd->combos.id = NULL;
  8948. sd->combos.pos = NULL;
  8949. return retval; /* we also can return at this point for we have no more combos to check */
  8950. }
  8951. }
  8952. return retval;
  8953. }
  8954. /**
  8955. * Load combo data(s) of player
  8956. * @param *sd
  8957. * @return ret numbers of succeed combo
  8958. */
  8959. int pc_load_combo(struct map_session_data *sd) {
  8960. int i, ret = 0;
  8961. for( i = 0; i < EQI_MAX; i++ ) {
  8962. struct item_data *id = NULL;
  8963. short idx = sd->equip_index[i];
  8964. if( idx < 0 || !(id = sd->inventory_data[idx] ) )
  8965. continue;
  8966. if( id->combos_count )
  8967. ret += pc_checkcombo(sd,id);
  8968. if(!itemdb_isspecial(sd->inventory.u.items_inventory[idx].card[0])) {
  8969. struct item_data *data;
  8970. int j;
  8971. for( j = 0; j < MAX_SLOTS; j++ ) {
  8972. if (!sd->inventory.u.items_inventory[idx].card[j])
  8973. continue;
  8974. if ( ( data = itemdb_exists(sd->inventory.u.items_inventory[idx].card[j]) ) != NULL ) {
  8975. if( data->combos_count )
  8976. ret += pc_checkcombo(sd,data);
  8977. }
  8978. }
  8979. }
  8980. }
  8981. return ret;
  8982. }
  8983. /*==========================================
  8984. * Equip item on player sd at req_pos from inventory index n
  8985. * return: false - fail; true - success
  8986. *------------------------------------------*/
  8987. bool pc_equipitem(struct map_session_data *sd,short n,int req_pos,bool equipswitch)
  8988. {
  8989. int i, pos, flag = 0, iflag;
  8990. struct item_data *id;
  8991. uint8 res = ITEM_EQUIP_ACK_OK;
  8992. short* equip_index;
  8993. nullpo_retr(false,sd);
  8994. if( n < 0 || n >= MAX_INVENTORY ) {
  8995. if( equipswitch ){
  8996. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  8997. }else{
  8998. clif_equipitemack(sd,0,0,ITEM_EQUIP_ACK_FAIL);
  8999. }
  9000. return false;
  9001. }
  9002. if( DIFF_TICK(sd->canequip_tick,gettick()) > 0 ) {
  9003. if( equipswitch ){
  9004. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  9005. }else{
  9006. clif_equipitemack(sd,n,0,ITEM_EQUIP_ACK_FAIL);
  9007. }
  9008. return false;
  9009. }
  9010. if (!(id = sd->inventory_data[n]))
  9011. return false;
  9012. pos = pc_equippoint(sd,n); //With a few exceptions, item should go in all specified slots.
  9013. if(battle_config.battle_log && !equipswitch)
  9014. ShowInfo("equip %hu (%d) %x:%x\n",sd->inventory.u.items_inventory[n].nameid,n,id?id->equip:0,req_pos);
  9015. if((res = pc_isequip(sd,n))) {
  9016. if( equipswitch ){
  9017. clif_equipswitch_add( sd, n, req_pos, res );
  9018. }else{
  9019. clif_equipitemack(sd,n,0,res); // fail
  9020. }
  9021. return false;
  9022. }
  9023. if( equipswitch && id->type == IT_AMMO ){
  9024. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  9025. return false;
  9026. }
  9027. if (!(pos&req_pos) || sd->inventory.u.items_inventory[n].equip != 0 || sd->inventory.u.items_inventory[n].attribute==1 ) { // [Valaris]
  9028. if( equipswitch ){
  9029. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  9030. }else{
  9031. clif_equipitemack(sd,n,0,ITEM_EQUIP_ACK_FAIL); // fail
  9032. }
  9033. return false;
  9034. }
  9035. if( sd->sc.count && (sd->sc.data[SC_BERSERK] || sd->sc.data[SC_SATURDAYNIGHTFEVER] ||
  9036. sd->sc.data[SC_KYOUGAKU] || (sd->sc.data[SC_PYROCLASTIC] && sd->inventory_data[n]->type == IT_WEAPON)) ) {
  9037. if( equipswitch ){
  9038. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  9039. }else{
  9040. clif_equipitemack(sd,n,0,ITEM_EQUIP_ACK_FAIL); //Fail
  9041. }
  9042. return false;
  9043. }
  9044. equip_index = equipswitch ? sd->equip_switch_index : sd->equip_index;
  9045. if ( !equipswitch && id->flag.bindOnEquip && !sd->inventory.u.items_inventory[n].bound) {
  9046. sd->inventory.u.items_inventory[n].bound = (char)battle_config.default_bind_on_equip;
  9047. clif_notify_bindOnEquip(sd,n);
  9048. }
  9049. if(pos == EQP_ACC) { //Accessories should only go in one of the two.
  9050. pos = req_pos&EQP_ACC;
  9051. if (pos == EQP_ACC) //User specified both slots.
  9052. pos = equip_index[EQI_ACC_R] >= 0 ? EQP_ACC_L : EQP_ACC_R;
  9053. for (i = 0; i < sd->inventory_data[n]->slot; i++) { // Accessories that have cards that force equip location
  9054. if (!sd->inventory.u.items_inventory[n].card[i])
  9055. continue;
  9056. struct item_data *card_data = itemdb_exists(sd->inventory.u.items_inventory[n].card[i]);
  9057. if (card_data) {
  9058. int card_pos = card_data->equip;
  9059. if (card_pos == EQP_ACC_L || card_pos == EQP_ACC_R) {
  9060. pos = card_pos; // Use the card's equip position
  9061. break;
  9062. }
  9063. }
  9064. }
  9065. } else if(pos == EQP_ARMS && id->equip == EQP_HAND_R) { //Dual wield capable weapon.
  9066. pos = (req_pos&EQP_ARMS);
  9067. if (pos == EQP_ARMS) //User specified both slots, pick one for them.
  9068. pos = equip_index[EQI_HAND_R] >= 0 ? EQP_HAND_L : EQP_HAND_R;
  9069. } else if(pos == EQP_SHADOW_ACC) { // Shadow System
  9070. pos = req_pos&EQP_SHADOW_ACC;
  9071. if (pos == EQP_SHADOW_ACC)
  9072. pos = equip_index[EQI_SHADOW_ACC_L] >= 0 ? EQP_SHADOW_ACC_R : EQP_SHADOW_ACC_L;
  9073. } else if(pos == EQP_SHADOW_ARMS && id->equip == EQP_SHADOW_WEAPON) {
  9074. pos = (req_pos&EQP_SHADOW_ARMS);
  9075. if( pos == EQP_SHADOW_ARMS )
  9076. pos = (equip_index[EQI_SHADOW_WEAPON] >= 0 ? EQP_SHADOW_SHIELD : EQP_SHADOW_WEAPON);
  9077. }
  9078. if (pos&EQP_HAND_R && battle_config.use_weapon_skill_range&BL_PC) {
  9079. //Update skill-block range database when weapon range changes. [Skotlex]
  9080. i = equip_index[EQI_HAND_R];
  9081. if (i < 0 || !sd->inventory_data[i]) //No data, or no weapon equipped
  9082. flag = 1;
  9083. else
  9084. flag = id->range != sd->inventory_data[i]->range;
  9085. }
  9086. if( equipswitch ){
  9087. for( i = 0; i < EQI_MAX; i++ ){
  9088. if( pos&equip_bitmask[i] ){
  9089. // If there was already an item assigned to this slot
  9090. if( sd->equip_switch_index[i] >= 0 ){
  9091. pc_equipswitch_remove( sd, sd->equip_switch_index[i] );
  9092. }
  9093. // Assign the new index to it
  9094. sd->equip_switch_index[i] = n;
  9095. }
  9096. }
  9097. sd->inventory.u.items_inventory[n].equipSwitch = pos;
  9098. clif_equipswitch_add( sd, n, pos, ITEM_EQUIP_ACK_OK );
  9099. return true;
  9100. }else{
  9101. for(i=0;i<EQI_MAX;i++) {
  9102. if(pos & equip_bitmask[i]) {
  9103. if(sd->equip_index[i] >= 0) //Slot taken, remove item from there.
  9104. pc_unequipitem(sd,sd->equip_index[i],2 | 4);
  9105. sd->equip_index[i] = n;
  9106. }
  9107. }
  9108. pc_equipswitch_remove(sd, n);
  9109. if(pos==EQP_AMMO) {
  9110. clif_arrowequip(sd,n);
  9111. clif_arrow_fail(sd,3);
  9112. }
  9113. else
  9114. clif_equipitemack(sd,n,pos,ITEM_EQUIP_ACK_OK);
  9115. sd->inventory.u.items_inventory[n].equip = pos;
  9116. }
  9117. if(pos & EQP_HAND_R) {
  9118. if(id)
  9119. sd->weapontype1 = id->look;
  9120. else
  9121. sd->weapontype1 = 0;
  9122. pc_calcweapontype(sd);
  9123. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  9124. }
  9125. if(pos & EQP_HAND_L) {
  9126. if(id) {
  9127. if(id->type == IT_WEAPON) {
  9128. sd->status.shield = 0;
  9129. sd->weapontype2 = id->look;
  9130. }
  9131. else
  9132. if(id->type == IT_ARMOR) {
  9133. sd->status.shield = id->look;
  9134. sd->weapontype2 = 0;
  9135. }
  9136. }
  9137. else
  9138. sd->status.shield = sd->weapontype2 = 0;
  9139. pc_calcweapontype(sd);
  9140. clif_changelook(&sd->bl,LOOK_SHIELD,sd->status.shield);
  9141. }
  9142. if(pos & EQP_SHOES)
  9143. clif_changelook(&sd->bl,LOOK_SHOES,0);
  9144. if (battle_config.ammo_unequip && (pos&EQP_ARMS) && id->type == IT_WEAPON) {
  9145. short idx = sd->equip_index[EQI_AMMO];
  9146. if (idx >= 0) {
  9147. switch (sd->inventory_data[idx]->look) {
  9148. case AMMO_ARROW:
  9149. if (id->look != W_BOW && id->look != W_MUSICAL && id->look != W_WHIP)
  9150. pc_unequipitem(sd, idx, 2 | 4);
  9151. break;
  9152. case AMMO_BULLET:
  9153. case AMMO_SHELL:
  9154. if (id->look != W_REVOLVER && id->look != W_RIFLE && id->look != W_GATLING && id->look != W_SHOTGUN
  9155. #ifdef RENEWAL
  9156. && id->look != W_GRENADE
  9157. #endif
  9158. )
  9159. pc_unequipitem(sd, idx, 2 | 4);
  9160. break;
  9161. #ifndef RENEWAL
  9162. case AMMO_GRENADE:
  9163. if (id->look != W_GRENADE)
  9164. pc_unequipitem(sd, idx, 2 | 4);
  9165. break;
  9166. #endif
  9167. }
  9168. }
  9169. }
  9170. pc_set_costume_view(sd);
  9171. pc_checkallowskill(sd); //Check if status changes should be halted.
  9172. iflag = sd->npc_item_flag;
  9173. /* check for combos (MUST be before status_calc_pc) */
  9174. if( id->combos_count )
  9175. pc_checkcombo(sd,id);
  9176. if(itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  9177. ; //No cards
  9178. else {
  9179. for( i = 0; i < MAX_SLOTS; i++ ) {
  9180. struct item_data *data;
  9181. if (!sd->inventory.u.items_inventory[n].card[i])
  9182. continue;
  9183. if ( ( data = itemdb_exists(sd->inventory.u.items_inventory[n].card[i]) ) != NULL ) {
  9184. if( data->combos_count )
  9185. pc_checkcombo(sd,data);
  9186. }
  9187. }
  9188. }
  9189. status_calc_pc(sd,SCO_NONE);
  9190. if (flag) //Update skill data
  9191. clif_skillinfoblock(sd);
  9192. //OnEquip script [Skotlex]
  9193. if (id) {
  9194. //only run the script if item isn't restricted
  9195. if (id->equip_script && (pc_has_permission(sd,PC_PERM_USE_ALL_EQUIPMENT) || !itemdb_isNoEquip(id,sd->bl.m)))
  9196. run_script(id->equip_script,0,sd->bl.id,fake_nd->bl.id);
  9197. if(itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  9198. ; //No cards
  9199. else {
  9200. for( i = 0; i < MAX_SLOTS; i++ ) {
  9201. struct item_data *data;
  9202. if (!sd->inventory.u.items_inventory[n].card[i])
  9203. continue;
  9204. if ( ( data = itemdb_exists(sd->inventory.u.items_inventory[n].card[i]) ) != NULL ) {
  9205. if (data->equip_script && (pc_has_permission(sd,PC_PERM_USE_ALL_EQUIPMENT) || !itemdb_isNoEquip(data,sd->bl.m)))
  9206. run_script(data->equip_script,0,sd->bl.id,fake_nd->bl.id);
  9207. }
  9208. }
  9209. }
  9210. }
  9211. sd->npc_item_flag = iflag;
  9212. return true;
  9213. }
  9214. /**
  9215. * Recalculate player status on unequip
  9216. * @param sd: Player data
  9217. * @param n: Item inventory index
  9218. * @param flag: Whether to recalculate a player's status or not
  9219. * @return True on success or false on failure
  9220. */
  9221. static void pc_unequipitem_sub(struct map_session_data *sd, int n, int flag) {
  9222. int i, iflag;
  9223. bool status_calc = false;
  9224. if (sd->state.autobonus&sd->inventory.u.items_inventory[n].equip)
  9225. sd->state.autobonus &= ~sd->inventory.u.items_inventory[n].equip; //Check for activated autobonus [Inkfish]
  9226. sd->inventory.u.items_inventory[n].equip = 0;
  9227. pc_checkallowskill(sd);
  9228. iflag = sd->npc_item_flag;
  9229. /* check for combos (MUST be before status_calc_pc) */
  9230. if (sd->inventory_data[n]) {
  9231. if (sd->inventory_data[n]->combos_count) {
  9232. if (pc_removecombo(sd, sd->inventory_data[n]))
  9233. status_calc = true;
  9234. }
  9235. if (itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  9236. ; //No cards
  9237. else {
  9238. for (i = 0; i < MAX_SLOTS; i++) {
  9239. struct item_data *data;
  9240. if (!sd->inventory.u.items_inventory[n].card[i])
  9241. continue;
  9242. if ((data = itemdb_exists(sd->inventory.u.items_inventory[n].card[i])) != NULL) {
  9243. if (data->combos_count) {
  9244. if (pc_removecombo(sd, data))
  9245. status_calc = true;
  9246. }
  9247. }
  9248. }
  9249. }
  9250. }
  9251. if (flag & 1 || status_calc) {
  9252. pc_checkallowskill(sd);
  9253. status_calc_pc(sd, SCO_NONE);
  9254. }
  9255. if (sd->sc.data[SC_SIGNUMCRUCIS] && !battle_check_undead(sd->battle_status.race, sd->battle_status.def_ele))
  9256. status_change_end(&sd->bl, SC_SIGNUMCRUCIS, INVALID_TIMER);
  9257. //OnUnEquip script [Skotlex]
  9258. if (sd->inventory_data[n]) {
  9259. if (sd->inventory_data[n]->unequip_script)
  9260. run_script(sd->inventory_data[n]->unequip_script, 0, sd->bl.id, fake_nd->bl.id);
  9261. if (itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  9262. ; //No cards
  9263. else {
  9264. for (i = 0; i < MAX_SLOTS; i++) {
  9265. struct item_data *data;
  9266. if (!sd->inventory.u.items_inventory[n].card[i])
  9267. continue;
  9268. if ((data = itemdb_exists(sd->inventory.u.items_inventory[n].card[i])) != NULL) {
  9269. if (data->unequip_script)
  9270. run_script(data->unequip_script, 0, sd->bl.id, fake_nd->bl.id);
  9271. }
  9272. }
  9273. }
  9274. }
  9275. sd->npc_item_flag = iflag;
  9276. }
  9277. /**
  9278. * Called when attempting to unequip an item from a player
  9279. * @param sd: Player data
  9280. * @param n: Item inventory index
  9281. * @param flag: Type of unequip
  9282. * 0 - only unequip
  9283. * 1 - calculate status after unequipping
  9284. * 2 - force unequip
  9285. * 4 - unequip by switching equipment
  9286. * @return True on success or false on failure
  9287. */
  9288. bool pc_unequipitem(struct map_session_data *sd, int n, int flag) {
  9289. int i, pos;
  9290. nullpo_retr(false,sd);
  9291. if (n < 0 || n >= MAX_INVENTORY) {
  9292. clif_unequipitemack(sd,0,0,0);
  9293. return false;
  9294. }
  9295. if (!(pos = sd->inventory.u.items_inventory[n].equip)) {
  9296. clif_unequipitemack(sd,n,0,0);
  9297. return false; //Nothing to unequip
  9298. }
  9299. // status change that makes player cannot unequip equipment
  9300. if (!(flag&2) && sd->sc.count &&
  9301. (sd->sc.data[SC_BERSERK] ||
  9302. sd->sc.data[SC_SATURDAYNIGHTFEVER] ||
  9303. sd->sc.data[SC__BLOODYLUST] ||
  9304. sd->sc.data[SC_KYOUGAKU] ||
  9305. (sd->sc.data[SC_PYROCLASTIC] &&
  9306. sd->inventory_data[n]->type == IT_WEAPON))) // can't switch weapon
  9307. {
  9308. clif_unequipitemack(sd,n,0,0);
  9309. return false;
  9310. }
  9311. if (battle_config.battle_log)
  9312. ShowInfo("unequip %d %x:%x\n",n,pc_equippoint(sd,n),pos);
  9313. for(i = 0; i < EQI_MAX; i++) {
  9314. if (pos & equip_bitmask[i])
  9315. sd->equip_index[i] = -1;
  9316. }
  9317. if(pos & EQP_HAND_R) {
  9318. sd->weapontype1 = 0;
  9319. sd->status.weapon = sd->weapontype2;
  9320. pc_calcweapontype(sd);
  9321. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  9322. if( !battle_config.dancing_weaponswitch_fix )
  9323. status_change_end(&sd->bl, SC_DANCING, INVALID_TIMER); // Unequipping => stop dancing.
  9324. #ifdef RENEWAL
  9325. if (battle_config.switch_remove_edp&2) {
  9326. #else
  9327. if (battle_config.switch_remove_edp&1) {
  9328. #endif
  9329. status_change_end(&sd->bl, SC_EDP, INVALID_TIMER);
  9330. }
  9331. }
  9332. if(pos & EQP_HAND_L) {
  9333. if (sd->status.shield && battle_getcurrentskill(&sd->bl) == LG_SHIELDSPELL)
  9334. unit_skillcastcancel(&sd->bl, 0); // Cancel Shield Spell if player swaps shields.
  9335. sd->status.shield = sd->weapontype2 = 0;
  9336. pc_calcweapontype(sd);
  9337. clif_changelook(&sd->bl,LOOK_SHIELD,sd->status.shield);
  9338. }
  9339. if(pos & EQP_SHOES)
  9340. clif_changelook(&sd->bl,LOOK_SHOES,0);
  9341. clif_unequipitemack(sd,n,pos,1);
  9342. pc_set_costume_view(sd);
  9343. status_change_end(&sd->bl,SC_HEAT_BARREL,INVALID_TIMER);
  9344. // On weapon change (right and left hand)
  9345. if ((pos & EQP_ARMS) && sd->inventory_data[n]->type == IT_WEAPON) {
  9346. if (battle_config.ammo_unequip && !(flag & 4)) {
  9347. switch (sd->inventory_data[n]->look) {
  9348. case W_BOW:
  9349. case W_MUSICAL:
  9350. case W_WHIP:
  9351. case W_REVOLVER:
  9352. case W_RIFLE:
  9353. case W_GATLING:
  9354. case W_SHOTGUN:
  9355. case W_GRENADE: {
  9356. short idx = sd->equip_index[EQI_AMMO];
  9357. if (idx >= 0) {
  9358. sd->equip_index[EQI_AMMO] = -1;
  9359. clif_unequipitemack(sd, idx, sd->inventory.u.items_inventory[idx].equip, 1);
  9360. pc_unequipitem_sub(sd, idx, 0);
  9361. }
  9362. }
  9363. break;
  9364. }
  9365. }
  9366. if (!sd->sc.data[SC_SEVENWIND] || sd->sc.data[SC_ASPERSIO]) //Check for seven wind (but not level seven!)
  9367. skill_enchant_elemental_end(&sd->bl, SC_NONE);
  9368. status_change_end(&sd->bl, SC_FEARBREEZE, INVALID_TIMER);
  9369. status_change_end(&sd->bl, SC_EXEEDBREAK, INVALID_TIMER);
  9370. }
  9371. // On armor change
  9372. if (pos & EQP_ARMOR) {
  9373. if (sd->sc.data[SC_HOVERING] && sd->inventory_data[n]->nameid == ITEMID_HOVERING_BOOSTER)
  9374. status_change_end(&sd->bl, SC_HOVERING, INVALID_TIMER);
  9375. //status_change_end(&sd->bl, SC_BENEDICTIO, INVALID_TIMER); // No longer is removed? Need confirmation
  9376. status_change_end(&sd->bl, SC_ARMOR_RESIST, INVALID_TIMER);
  9377. }
  9378. // On equipment change
  9379. #ifndef RENEWAL
  9380. if (!(flag&4))
  9381. status_change_end(&sd->bl, SC_CONCENTRATION, INVALID_TIMER);
  9382. #endif
  9383. // On ammo change
  9384. 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_))
  9385. status_change_end(&sd->bl, SC_P_ALTER, INVALID_TIMER);
  9386. pc_unequipitem_sub(sd, n, flag);
  9387. return true;
  9388. }
  9389. int pc_equipswitch( struct map_session_data* sd, int index ){
  9390. // Get the target equip mask
  9391. int position = sd->inventory.u.items_inventory[index].equipSwitch;
  9392. // Get the currently equipped item
  9393. short equippedItem = pc_checkequip( sd, position, true );
  9394. // No item equipped at the target
  9395. if( equippedItem == -1 ){
  9396. // Remove it from the equip switch
  9397. pc_equipswitch_remove( sd, index );
  9398. pc_equipitem( sd, index, position );
  9399. return position;
  9400. }else{
  9401. std::map<int, int> unequipped;
  9402. int unequipped_position = 0;
  9403. // Unequip all items that interfere
  9404. for( int i = 0; i < EQI_MAX; i++ ){
  9405. int unequip_index = sd->equip_index[i];
  9406. if( unequip_index >= 0 && position & equip_bitmask[i] ){
  9407. struct item* unequip_item = &sd->inventory.u.items_inventory[unequip_index];
  9408. // Store the unequipped index and position mask for later
  9409. unequipped[unequip_index] = unequip_item->equip;
  9410. // Keep the position for later
  9411. unequipped_position |= unequip_item->equip;
  9412. // Unequip the item
  9413. pc_unequipitem( sd, unequip_index, 0 );
  9414. }
  9415. }
  9416. int all_position = position | unequipped_position;
  9417. // Equip everything that is hit by the mask
  9418. for( int i = 0; i < EQI_MAX; i++ ){
  9419. int exchange_index = sd->equip_switch_index[i];
  9420. if( exchange_index >= 0 && all_position & equip_bitmask[i] ){
  9421. struct item* exchange_item = &sd->inventory.u.items_inventory[exchange_index];
  9422. // Store the target position
  9423. int exchange_position = exchange_item->equipSwitch;
  9424. // Remove the item from equip switch
  9425. pc_equipswitch_remove( sd, exchange_index );
  9426. // Equip the item at the destinated position
  9427. pc_equipitem( sd, exchange_index, exchange_position );
  9428. }
  9429. }
  9430. // Place all unequipped items into the equip switch window
  9431. for( std::pair<int, int> pair : unequipped ){
  9432. int unequipped_index = pair.first;
  9433. int unequipped_position = pair.second;
  9434. // Rebuild the index cache
  9435. for( int i = 0; i < EQI_MAX; i++ ){
  9436. if( unequipped_position & equip_bitmask[i] ){
  9437. sd->equip_switch_index[i] = unequipped_index;
  9438. }
  9439. }
  9440. // Set the correct position mask
  9441. sd->inventory.u.items_inventory[unequipped_index].equipSwitch = unequipped_position;
  9442. // Notify the client
  9443. clif_equipswitch_add( sd, unequipped_index, unequipped_position, ITEM_EQUIP_ACK_OK );
  9444. }
  9445. return all_position;
  9446. }
  9447. }
  9448. void pc_equipswitch_remove( struct map_session_data* sd, int index ){
  9449. struct item* item = &sd->inventory.u.items_inventory[index];
  9450. if( !item->equipSwitch ){
  9451. return;
  9452. }
  9453. for( int i = 0; i < EQI_MAX; i++ ){
  9454. // If a match is found
  9455. if( sd->equip_switch_index[i] == index ){
  9456. // Remove it from the slot
  9457. sd->equip_switch_index[i] = -1;
  9458. }
  9459. }
  9460. // Send out one packet for all slots using the current item's mask
  9461. clif_equipswitch_remove( sd, index, item->equipSwitch, false );
  9462. item->equipSwitch = 0;
  9463. }
  9464. /*==========================================
  9465. * Checking if player (sd) has an invalid item
  9466. * and is unequiped on map load (item_noequip)
  9467. *------------------------------------------*/
  9468. void pc_checkitem(struct map_session_data *sd) {
  9469. int i, calc_flag = 0;
  9470. struct item* it;
  9471. nullpo_retv(sd);
  9472. if( sd->state.vending ) //Avoid reorganizing items when we are vending, as that leads to exploits (pointed out by End of Exam)
  9473. return;
  9474. pc_check_available_item(sd, ITMCHK_NONE); // Check for invalid(ated) items.
  9475. for( i = 0; i < MAX_INVENTORY; i++ ) {
  9476. it = &sd->inventory.u.items_inventory[i];
  9477. if( it->nameid == 0 )
  9478. continue;
  9479. if( !it->equip )
  9480. continue;
  9481. if( it->equip&~pc_equippoint(sd,i) ) {
  9482. pc_unequipitem(sd, i, 2);
  9483. calc_flag = 1;
  9484. continue;
  9485. }
  9486. 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) ) {
  9487. pc_unequipitem(sd, i, 2);
  9488. calc_flag = 1;
  9489. continue;
  9490. }
  9491. }
  9492. for( i = 0; i < MAX_INVENTORY; i++ ) {
  9493. it = &sd->inventory.u.items_inventory[i];
  9494. if( it->nameid == 0 )
  9495. continue;
  9496. if( !it->equipSwitch )
  9497. continue;
  9498. if( it->equipSwitch&~pc_equippoint(sd,i) ||
  9499. ( !pc_has_permission(sd, PC_PERM_USE_ALL_EQUIPMENT) && !battle_config.allow_equip_restricted_item && itemdb_isNoEquip(sd->inventory_data[i], sd->bl.m) ) ){
  9500. for( int j = 0; j < EQI_MAX; j++ ){
  9501. if( sd->equip_switch_index[j] == i ){
  9502. sd->equip_switch_index[j] = -1;
  9503. }
  9504. }
  9505. sd->inventory.u.items_inventory[i].equipSwitch = 0;
  9506. continue;
  9507. }
  9508. }
  9509. if( calc_flag && sd->state.active ) {
  9510. pc_checkallowskill(sd);
  9511. status_calc_pc(sd,SCO_NONE);
  9512. }
  9513. }
  9514. /*==========================================
  9515. * Checks for unavailable items and removes them.
  9516. * @param sd: Player data
  9517. * @param type Forced check:
  9518. * 1 - Inventory
  9519. * 2 - Cart
  9520. * 4 - Storage
  9521. *------------------------------------------*/
  9522. void pc_check_available_item(struct map_session_data *sd, uint8 type)
  9523. {
  9524. int i;
  9525. unsigned short nameid;
  9526. char output[256];
  9527. nullpo_retv(sd);
  9528. if (battle_config.item_check&ITMCHK_INVENTORY && type&ITMCHK_INVENTORY) { // Check for invalid(ated) items in inventory.
  9529. for(i = 0; i < MAX_INVENTORY; i++) {
  9530. nameid = sd->inventory.u.items_inventory[i].nameid;
  9531. if (!nameid)
  9532. continue;
  9533. if (!itemdb_available(nameid)) {
  9534. sprintf(output, msg_txt(sd, 709), nameid); // Item %hu has been removed from your inventory.
  9535. clif_displaymessage(sd->fd, output);
  9536. 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);
  9537. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 4, 0, LOG_TYPE_OTHER);
  9538. continue;
  9539. }
  9540. if (!sd->inventory.u.items_inventory[i].unique_id && !itemdb_isstackable(nameid))
  9541. sd->inventory.u.items_inventory[i].unique_id = pc_generate_unique_id(sd);
  9542. }
  9543. }
  9544. if (battle_config.item_check&ITMCHK_CART && type&ITMCHK_CART) { // Check for invalid(ated) items in cart.
  9545. for(i = 0; i < MAX_CART; i++) {
  9546. nameid = sd->cart.u.items_cart[i].nameid;
  9547. if (!nameid)
  9548. continue;
  9549. if (!itemdb_available(nameid)) {
  9550. sprintf(output, msg_txt(sd, 710), nameid); // Item %hu has been removed from your cart.
  9551. clif_displaymessage(sd->fd, output);
  9552. 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);
  9553. pc_cart_delitem(sd, i, sd->cart.u.items_cart[i].amount, 0, LOG_TYPE_OTHER);
  9554. continue;
  9555. }
  9556. if (!sd->cart.u.items_cart[i].unique_id && !itemdb_isstackable(nameid))
  9557. sd->cart.u.items_cart[i].unique_id = pc_generate_unique_id(sd);
  9558. }
  9559. }
  9560. if (battle_config.item_check&ITMCHK_STORAGE && type&ITMCHK_STORAGE) { // Check for invalid(ated) items in storage.
  9561. for(i = 0; i < sd->storage.max_amount; i++) {
  9562. nameid = sd->storage.u.items_storage[i].nameid;
  9563. if (!nameid)
  9564. continue;
  9565. if (!itemdb_available(nameid)) {
  9566. sprintf(output, msg_txt(sd, 711), nameid); // Item %hu has been removed from your storage.
  9567. clif_displaymessage(sd->fd, output);
  9568. 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);
  9569. storage_delitem(sd, &sd->storage, i, sd->storage.u.items_storage[i].amount);
  9570. continue;
  9571. }
  9572. if (!sd->storage.u.items_storage[i].unique_id && !itemdb_isstackable(nameid))
  9573. sd->storage.u.items_storage[i].unique_id = pc_generate_unique_id(sd);
  9574. }
  9575. }
  9576. }
  9577. /*==========================================
  9578. * Update PVP rank for sd1 in cmp to sd2
  9579. *------------------------------------------*/
  9580. static int pc_calc_pvprank_sub(struct block_list *bl,va_list ap)
  9581. {
  9582. struct map_session_data *sd1,*sd2;
  9583. sd1=(struct map_session_data *)bl;
  9584. sd2=va_arg(ap,struct map_session_data *);
  9585. if( pc_isinvisible(sd1) || pc_isinvisible(sd2) )
  9586. {// cannot register pvp rank for hidden GMs
  9587. return 0;
  9588. }
  9589. if( sd1->pvp_point > sd2->pvp_point )
  9590. sd2->pvp_rank++;
  9591. return 0;
  9592. }
  9593. /*==========================================
  9594. * Calculate new rank beetween all present players (map_foreachinallarea)
  9595. * and display result
  9596. *------------------------------------------*/
  9597. int pc_calc_pvprank(struct map_session_data *sd)
  9598. {
  9599. int old = sd->pvp_rank;
  9600. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  9601. sd->pvp_rank=1;
  9602. map_foreachinmap(pc_calc_pvprank_sub,sd->bl.m,BL_PC,sd);
  9603. if(old!=sd->pvp_rank || sd->pvp_lastusers!=mapdata->users_pvp)
  9604. clif_pvpset(sd,sd->pvp_rank,sd->pvp_lastusers=mapdata->users_pvp,0);
  9605. return sd->pvp_rank;
  9606. }
  9607. /*==========================================
  9608. * Calculate next sd ranking calculation from config
  9609. *------------------------------------------*/
  9610. TIMER_FUNC(pc_calc_pvprank_timer){
  9611. struct map_session_data *sd;
  9612. sd=map_id2sd(id);
  9613. if(sd==NULL)
  9614. return 0;
  9615. sd->pvp_timer = INVALID_TIMER;
  9616. if( pc_isinvisible(sd) )
  9617. {// do not calculate the pvp rank for a hidden GM
  9618. return 0;
  9619. }
  9620. if( pc_calc_pvprank(sd) > 0 )
  9621. sd->pvp_timer = add_timer(gettick()+PVP_CALCRANK_INTERVAL,pc_calc_pvprank_timer,id,data);
  9622. return 0;
  9623. }
  9624. /*==========================================
  9625. * Checking if sd is married
  9626. * Return:
  9627. * partner_id = yes
  9628. * 0 = no
  9629. *------------------------------------------*/
  9630. int pc_ismarried(struct map_session_data *sd)
  9631. {
  9632. if(sd == NULL)
  9633. return -1;
  9634. if(sd->status.partner_id > 0)
  9635. return sd->status.partner_id;
  9636. else
  9637. return 0;
  9638. }
  9639. /*==========================================
  9640. * Marry player sd to player dstsd
  9641. * Return:
  9642. * false = fail
  9643. * true = success
  9644. *------------------------------------------*/
  9645. bool pc_marriage(struct map_session_data *sd,struct map_session_data *dstsd)
  9646. {
  9647. if(sd == NULL || dstsd == NULL ||
  9648. sd->status.partner_id > 0 || dstsd->status.partner_id > 0 ||
  9649. (sd->class_&JOBL_BABY) || (dstsd->class_&JOBL_BABY))
  9650. return false;
  9651. sd->status.partner_id = dstsd->status.char_id;
  9652. dstsd->status.partner_id = sd->status.char_id;
  9653. achievement_update_objective(sd, AG_MARRY, 1, 1);
  9654. achievement_update_objective(dstsd, AG_MARRY, 1, 1);
  9655. return true;
  9656. }
  9657. /*==========================================
  9658. * Divorce sd from its partner
  9659. * Return:
  9660. * false = fail
  9661. * true = success
  9662. *------------------------------------------*/
  9663. bool pc_divorce(struct map_session_data *sd)
  9664. {
  9665. struct map_session_data *p_sd;
  9666. int i;
  9667. if( sd == NULL || !pc_ismarried(sd) )
  9668. return false;
  9669. if( !sd->status.partner_id )
  9670. return false; // Char is not married
  9671. if( (p_sd = map_charid2sd(sd->status.partner_id)) == NULL )
  9672. { // Lets char server do the divorce
  9673. if( chrif_divorce(sd->status.char_id, sd->status.partner_id) )
  9674. return false; // No char server connected
  9675. return true;
  9676. }
  9677. // Both players online, lets do the divorce manually
  9678. sd->status.partner_id = 0;
  9679. p_sd->status.partner_id = 0;
  9680. for( i = 0; i < MAX_INVENTORY; i++ )
  9681. {
  9682. if( sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_M || sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_F )
  9683. pc_delitem(sd, i, 1, 0, 0, LOG_TYPE_OTHER);
  9684. if( p_sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_M || p_sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_F )
  9685. pc_delitem(p_sd, i, 1, 0, 0, LOG_TYPE_OTHER);
  9686. }
  9687. clif_divorced(sd, p_sd->status.name);
  9688. clif_divorced(p_sd, sd->status.name);
  9689. return true;
  9690. }
  9691. /**
  9692. * Get the partner map_session_data of a player
  9693. * @param sd : the husband|wife session
  9694. * @return partner session or NULL
  9695. */
  9696. struct map_session_data *pc_get_partner(struct map_session_data *sd){
  9697. if (!sd || !pc_ismarried(sd))
  9698. return NULL;
  9699. return map_charid2sd(sd->status.partner_id);
  9700. }
  9701. /**
  9702. * Get the father map_session_data of a player
  9703. * @param sd : the baby session
  9704. * @return father session or NULL
  9705. */
  9706. struct map_session_data *pc_get_father (struct map_session_data *sd){
  9707. if (!sd || !(sd->class_&JOBL_BABY) || !sd->status.father)
  9708. return NULL;
  9709. return map_charid2sd(sd->status.father);
  9710. }
  9711. /**
  9712. * Get the mother map_session_data of a player
  9713. * @param sd : the baby session
  9714. * @return mother session or NULL
  9715. */
  9716. struct map_session_data *pc_get_mother (struct map_session_data *sd){
  9717. if (!sd || !(sd->class_&JOBL_BABY) || !sd->status.mother)
  9718. return NULL;
  9719. return map_charid2sd(sd->status.mother);
  9720. }
  9721. /*==========================================
  9722. * Get sd children charid. (Need to be married)
  9723. *------------------------------------------*/
  9724. struct map_session_data *pc_get_child (struct map_session_data *sd)
  9725. {
  9726. if (!sd || !pc_ismarried(sd) || !sd->status.child)
  9727. // charid2sd returns NULL if not found
  9728. return NULL;
  9729. return map_charid2sd(sd->status.child);
  9730. }
  9731. /*==========================================
  9732. * Set player sd to bleed. (losing hp and/or sp each diff_tick)
  9733. *------------------------------------------*/
  9734. void pc_bleeding (struct map_session_data *sd, t_tick diff_tick)
  9735. {
  9736. int hp = 0, sp = 0;
  9737. if( pc_isdead(sd) )
  9738. return;
  9739. if (sd->hp_loss.value) {
  9740. sd->hp_loss.tick += diff_tick;
  9741. while (sd->hp_loss.tick >= sd->hp_loss.rate) {
  9742. hp += sd->hp_loss.value;
  9743. sd->hp_loss.tick -= sd->hp_loss.rate;
  9744. }
  9745. if(hp >= sd->battle_status.hp)
  9746. hp = sd->battle_status.hp-1; //Script drains cannot kill you.
  9747. }
  9748. if (sd->sp_loss.value) {
  9749. sd->sp_loss.tick += diff_tick;
  9750. while (sd->sp_loss.tick >= sd->sp_loss.rate) {
  9751. sp += sd->sp_loss.value;
  9752. sd->sp_loss.tick -= sd->sp_loss.rate;
  9753. }
  9754. }
  9755. if (hp > 0 || sp > 0)
  9756. status_zap(&sd->bl, hp, sp);
  9757. }
  9758. //Character regen. Flag is used to know which types of regen can take place.
  9759. //&1: HP regen
  9760. //&2: SP regen
  9761. void pc_regen (struct map_session_data *sd, t_tick diff_tick)
  9762. {
  9763. int hp = 0, sp = 0;
  9764. if (sd->hp_regen.value) {
  9765. sd->hp_regen.tick += diff_tick;
  9766. while (sd->hp_regen.tick >= sd->hp_regen.rate) {
  9767. hp += sd->hp_regen.value;
  9768. sd->hp_regen.tick -= sd->hp_regen.rate;
  9769. }
  9770. }
  9771. if (sd->sp_regen.value) {
  9772. sd->sp_regen.tick += diff_tick;
  9773. while (sd->sp_regen.tick >= sd->sp_regen.rate) {
  9774. sp += sd->sp_regen.value;
  9775. sd->sp_regen.tick -= sd->sp_regen.rate;
  9776. }
  9777. }
  9778. if (sd->percent_hp_regen.value) {
  9779. sd->percent_hp_regen.tick += diff_tick;
  9780. while (sd->percent_hp_regen.tick >= sd->percent_hp_regen.rate) {
  9781. hp += sd->status.max_hp * sd->percent_hp_regen.value / 100;
  9782. sd->percent_hp_regen.tick -= sd->percent_hp_regen.rate;
  9783. }
  9784. }
  9785. if (sd->percent_sp_regen.value) {
  9786. sd->percent_sp_regen.tick += diff_tick;
  9787. while (sd->percent_sp_regen.tick >= sd->percent_sp_regen.rate) {
  9788. sp += sd->status.max_sp * sd->percent_sp_regen.value / 100;
  9789. sd->percent_sp_regen.tick -= sd->percent_sp_regen.rate;
  9790. }
  9791. }
  9792. if (hp > 0 || sp > 0)
  9793. status_heal(&sd->bl, hp, sp, 0);
  9794. }
  9795. /*==========================================
  9796. * Memo player sd savepoint. (map,x,y)
  9797. *------------------------------------------*/
  9798. void pc_setsavepoint(struct map_session_data *sd, short mapindex,int x,int y)
  9799. {
  9800. nullpo_retv(sd);
  9801. sd->status.save_point.map = mapindex;
  9802. sd->status.save_point.x = x;
  9803. sd->status.save_point.y = y;
  9804. }
  9805. /*==========================================
  9806. * Save 1 player data at autosave interval
  9807. *------------------------------------------*/
  9808. static TIMER_FUNC(pc_autosave){
  9809. int interval;
  9810. struct s_mapiterator* iter;
  9811. struct map_session_data* sd;
  9812. static int last_save_id = 0, save_flag = 0;
  9813. if(save_flag == 2) //Someone was saved on last call, normal cycle
  9814. save_flag = 0;
  9815. else
  9816. save_flag = 1; //Noone was saved, so save first found char.
  9817. iter = mapit_getallusers();
  9818. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  9819. {
  9820. if (!sd->state.pc_loaded) // Player data hasn't fully loaded
  9821. continue;
  9822. if(sd->bl.id == last_save_id && save_flag != 1) {
  9823. save_flag = 1;
  9824. continue;
  9825. }
  9826. if(save_flag != 1) //Not our turn to save yet.
  9827. continue;
  9828. //Save char.
  9829. last_save_id = sd->bl.id;
  9830. save_flag = 2;
  9831. if (pc_isvip(sd)) // Check if we're still VIP
  9832. chrif_req_login_operation(1, sd->status.name, CHRIF_OP_LOGIN_VIP, 0, 1, 0);
  9833. chrif_save(sd, CSAVE_INVENTORY|CSAVE_CART);
  9834. break;
  9835. }
  9836. mapit_free(iter);
  9837. interval = autosave_interval/(map_usercount()+1);
  9838. if(interval < minsave_interval)
  9839. interval = minsave_interval;
  9840. add_timer(gettick()+interval,pc_autosave,0,0);
  9841. return 0;
  9842. }
  9843. static int pc_daynight_timer_sub(struct map_session_data *sd,va_list ap)
  9844. {
  9845. if (sd->state.night != night_flag && map_getmapflag(sd->bl.m, MF_NIGHTENABLED))
  9846. { //Night/day state does not match.
  9847. clif_status_load(&sd->bl, EFST_SKE, night_flag); //New night effect by dynamix [Skotlex]
  9848. sd->state.night = night_flag;
  9849. return 1;
  9850. }
  9851. return 0;
  9852. }
  9853. /*================================================
  9854. * timer to do the day [Yor]
  9855. * data: 0 = called by timer, 1 = gmcommand/script
  9856. *------------------------------------------------*/
  9857. TIMER_FUNC(map_day_timer){
  9858. char tmp_soutput[1024];
  9859. if (data == 0 && battle_config.day_duration <= 0) // if we want a day
  9860. return 0;
  9861. if (!night_flag)
  9862. return 0; //Already day.
  9863. night_flag = 0; // 0=day, 1=night [Yor]
  9864. map_foreachpc(pc_daynight_timer_sub);
  9865. strcpy(tmp_soutput, (data == 0) ? msg_txt(NULL,502) : msg_txt(NULL,60)); // The day has arrived!
  9866. intif_broadcast(tmp_soutput, strlen(tmp_soutput) + 1, BC_DEFAULT);
  9867. return 0;
  9868. }
  9869. /*================================================
  9870. * timer to do the night [Yor]
  9871. * data: 0 = called by timer, 1 = gmcommand/script
  9872. *------------------------------------------------*/
  9873. TIMER_FUNC(map_night_timer){
  9874. char tmp_soutput[1024];
  9875. if (data == 0 && battle_config.night_duration <= 0) // if we want a night
  9876. return 0;
  9877. if (night_flag)
  9878. return 0; //Already nigth.
  9879. night_flag = 1; // 0=day, 1=night [Yor]
  9880. map_foreachpc(pc_daynight_timer_sub);
  9881. strcpy(tmp_soutput, (data == 0) ? msg_txt(NULL,503) : msg_txt(NULL,59)); // The night has fallen...
  9882. intif_broadcast(tmp_soutput, strlen(tmp_soutput) + 1, BC_DEFAULT);
  9883. return 0;
  9884. }
  9885. /**
  9886. * Attempt to stand up a player
  9887. * @param sd
  9888. * @param force Ignore the check, ask player to stand up. Used in some cases like pc_damage(), pc_revive(), etc
  9889. * @return True if success, Fals if failed
  9890. */
  9891. bool pc_setstand(struct map_session_data *sd, bool force){
  9892. nullpo_ret(sd);
  9893. // Cannot stand yet
  9894. // TODO: Move to SCS_NOSTAND [Cydh]
  9895. if (!force && (sd->sc.data[SC_SITDOWN_FORCE] || sd->sc.data[SC_BANANA_BOMB_SITDOWN]))
  9896. return false;
  9897. status_change_end(&sd->bl, SC_TENSIONRELAX, INVALID_TIMER);
  9898. clif_status_load(&sd->bl,EFST_SIT,0);
  9899. clif_standing(&sd->bl); //Inform area PC is standing
  9900. //Reset sitting tick.
  9901. sd->ssregen.tick.hp = sd->ssregen.tick.sp = 0;
  9902. if( pc_isdead( sd ) ){
  9903. sd->state.dead_sit = sd->vd.dead_sit = 0;
  9904. clif_party_dead( sd );
  9905. }else{
  9906. sd->state.dead_sit = sd->vd.dead_sit = 0;
  9907. }
  9908. return true;
  9909. }
  9910. /**
  9911. * Calculate Overheat value
  9912. * @param sd: Player data
  9913. * @param heat: Amount of Heat to adjust
  9914. **/
  9915. void pc_overheat(struct map_session_data *sd, int16 heat) {
  9916. struct status_change_entry *sce = NULL;
  9917. int16 limit[] = { 150, 200, 280, 360, 450 };
  9918. uint16 skill_lv;
  9919. nullpo_retv(sd);
  9920. skill_lv = cap_value(pc_checkskill(sd, NC_MAINFRAME), 0, 4);
  9921. if ((sce = sd->sc.data[SC_OVERHEAT_LIMITPOINT])) {
  9922. sce->val1 += heat;
  9923. sce->val1 = cap_value(sce->val1, 0, 1000);
  9924. if (sd->sc.data[SC_OVERHEAT])
  9925. status_change_end(&sd->bl, SC_OVERHEAT, INVALID_TIMER);
  9926. if (sce->val1 > limit[skill_lv])
  9927. sc_start(&sd->bl, &sd->bl, SC_OVERHEAT, 100, sce->val1, 1000);
  9928. } else if (heat > 0)
  9929. sc_start(&sd->bl, &sd->bl, SC_OVERHEAT_LIMITPOINT, 100, heat, 1000);
  9930. }
  9931. /**
  9932. * Check if player is autolooting given itemID.
  9933. */
  9934. bool pc_isautolooting(struct map_session_data *sd, unsigned short nameid)
  9935. {
  9936. uint8 i = 0;
  9937. if (sd->state.autoloottype && sd->state.autoloottype&(1<<itemdb_type(nameid)))
  9938. return true;
  9939. if (!sd->state.autolooting)
  9940. return false;
  9941. if (sd->state.autolooting)
  9942. ARR_FIND(0, AUTOLOOTITEM_SIZE, i, sd->state.autolootid[i] == nameid);
  9943. return (i != AUTOLOOTITEM_SIZE);
  9944. }
  9945. /**
  9946. * Checks if player can use @/#command
  9947. * @param sd Player map session data
  9948. * @param command Command name without @/# and params
  9949. * @param type is it atcommand or charcommand
  9950. */
  9951. bool pc_can_use_command(struct map_session_data *sd, const char *command, AtCommandType type)
  9952. {
  9953. return pc_group_can_use_command(pc_get_group_id(sd), command, type);
  9954. }
  9955. /**
  9956. * Checks if commands used by a player should be logged
  9957. * according to their group setting.
  9958. * @param sd Player map session data
  9959. */
  9960. bool pc_should_log_commands(struct map_session_data *sd)
  9961. {
  9962. return pc_group_should_log_commands(pc_get_group_id(sd));
  9963. }
  9964. /**
  9965. * Spirit Charm expiration timer.
  9966. * @see TimerFunc
  9967. */
  9968. static TIMER_FUNC(pc_spiritcharm_timer){
  9969. struct map_session_data *sd;
  9970. int i;
  9971. if ((sd = (struct map_session_data *)map_id2sd(id)) == NULL || sd->bl.type != BL_PC)
  9972. return 1;
  9973. if (sd->spiritcharm <= 0) {
  9974. 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);
  9975. sd->spiritcharm = 0;
  9976. sd->spiritcharm_type = CHARM_TYPE_NONE;
  9977. return 0;
  9978. }
  9979. ARR_FIND(0, sd->spiritcharm, i, sd->spiritcharm_timer[i] == tid);
  9980. if (i == sd->spiritcharm) {
  9981. ShowError("pc_spiritcharm_timer: timer not found (aid=%d cid=%d tid=%d)\n", sd->status.account_id, sd->status.char_id, tid);
  9982. return 0;
  9983. }
  9984. sd->spiritcharm--;
  9985. if (i != sd->spiritcharm)
  9986. memmove(sd->spiritcharm_timer + i, sd->spiritcharm_timer + i + 1, (sd->spiritcharm - i) * sizeof(int));
  9987. sd->spiritcharm_timer[sd->spiritcharm] = INVALID_TIMER;
  9988. if (sd->spiritcharm <= 0)
  9989. sd->spiritcharm_type = CHARM_TYPE_NONE;
  9990. clif_spiritcharm(sd);
  9991. return 0;
  9992. }
  9993. /**
  9994. * Adds a spirit charm.
  9995. * @param sd: Target character
  9996. * @param interval: Duration
  9997. * @param max: Maximum amount of charms to add
  9998. * @param type: Charm type (@see spirit_charm_types)
  9999. */
  10000. void pc_addspiritcharm(struct map_session_data *sd, int interval, int max, int type)
  10001. {
  10002. int tid, i;
  10003. nullpo_retv(sd);
  10004. if (sd->spiritcharm_type != CHARM_TYPE_NONE && type != sd->spiritcharm_type)
  10005. pc_delspiritcharm(sd, sd->spiritcharm, sd->spiritcharm_type);
  10006. if (max > MAX_SPIRITCHARM)
  10007. max = MAX_SPIRITCHARM;
  10008. if (sd->spiritcharm < 0)
  10009. sd->spiritcharm = 0;
  10010. if (sd->spiritcharm && sd->spiritcharm >= max) {
  10011. if (sd->spiritcharm_timer[0] != INVALID_TIMER)
  10012. delete_timer(sd->spiritcharm_timer[0], pc_spiritcharm_timer);
  10013. sd->spiritcharm--;
  10014. if (sd->spiritcharm != 0)
  10015. memmove(sd->spiritcharm_timer + 0, sd->spiritcharm_timer + 1, (sd->spiritcharm) * sizeof(int));
  10016. sd->spiritcharm_timer[sd->spiritcharm] = INVALID_TIMER;
  10017. }
  10018. tid = add_timer(gettick() + interval, pc_spiritcharm_timer, sd->bl.id, 0);
  10019. 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);
  10020. if (i != sd->spiritcharm)
  10021. memmove(sd->spiritcharm_timer + i + 1, sd->spiritcharm_timer + i, (sd->spiritcharm - i) * sizeof(int));
  10022. sd->spiritcharm_timer[i] = tid;
  10023. sd->spiritcharm++;
  10024. sd->spiritcharm_type = type;
  10025. clif_spiritcharm(sd);
  10026. }
  10027. /**
  10028. * Removes one or more spirit charms.
  10029. * @param sd: The target character
  10030. * @param count: Amount of charms to remove
  10031. * @param type: Type of charm to remove
  10032. */
  10033. void pc_delspiritcharm(struct map_session_data *sd, int count, int type)
  10034. {
  10035. int i;
  10036. nullpo_retv(sd);
  10037. if (sd->spiritcharm_type != type)
  10038. return;
  10039. if (sd->spiritcharm <= 0) {
  10040. sd->spiritcharm = 0;
  10041. return;
  10042. }
  10043. if (count <= 0)
  10044. return;
  10045. if (count > sd->spiritcharm)
  10046. count = sd->spiritcharm;
  10047. sd->spiritcharm -= count;
  10048. if (count > MAX_SPIRITCHARM)
  10049. count = MAX_SPIRITCHARM;
  10050. for (i = 0; i < count; i++) {
  10051. if (sd->spiritcharm_timer[i] != INVALID_TIMER) {
  10052. delete_timer(sd->spiritcharm_timer[i], pc_spiritcharm_timer);
  10053. sd->spiritcharm_timer[i] = INVALID_TIMER;
  10054. }
  10055. }
  10056. for (i = count; i < MAX_SPIRITCHARM; i++) {
  10057. sd->spiritcharm_timer[i - count] = sd->spiritcharm_timer[i];
  10058. sd->spiritcharm_timer[i] = INVALID_TIMER;
  10059. }
  10060. if (sd->spiritcharm <= 0)
  10061. sd->spiritcharm_type = CHARM_TYPE_NONE;
  10062. clif_spiritcharm(sd);
  10063. }
  10064. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  10065. /**
  10066. * Renewal EXP/Item Drop rate modifier based on level penalty
  10067. * @param level_diff: Monster and Player level difference
  10068. * @param mob_class: Monster class
  10069. * @param mode: Monster mode
  10070. * @param type: 1 - EXP, 2 - Item Drop
  10071. * @return Penalty rate
  10072. */
  10073. int pc_level_penalty_mod(int level_diff, uint32 mob_class, enum e_mode mode, int type)
  10074. {
  10075. int rate = 100;
  10076. if (type == 2 && (mode&MD_FIXED_ITEMDROP))
  10077. return rate;
  10078. if (level_diff < 0)
  10079. level_diff = MAX_LEVEL + (~level_diff + 1);
  10080. if ((rate = level_penalty[type][mob_class][level_diff]) > 0) // Monster class found, return rate
  10081. return rate;
  10082. return 100; // Penalty not found, return default
  10083. }
  10084. #endif
  10085. int pc_split_str(char *str,char **val,int num)
  10086. {
  10087. int i;
  10088. for (i=0; i<num && str; i++){
  10089. val[i] = str;
  10090. str = strchr(str,',');
  10091. if (str && i<num-1) //Do not remove a trailing comma.
  10092. *str++=0;
  10093. }
  10094. return i;
  10095. }
  10096. int pc_split_atoi(char* str, int* val, char sep, int max)
  10097. {
  10098. int i,j;
  10099. for (i=0; i<max; i++) {
  10100. if (!str) break;
  10101. val[i] = atoi(str);
  10102. str = strchr(str,sep);
  10103. if (str)
  10104. *str++=0;
  10105. }
  10106. //Zero up the remaining.
  10107. for(j=i; j < max; j++)
  10108. val[j] = 0;
  10109. return i;
  10110. }
  10111. int pc_split_atoui(char* str, unsigned int* val, char sep, int max)
  10112. {
  10113. static int warning=0;
  10114. int i,j;
  10115. for (i=0; i<max; i++) {
  10116. double f;
  10117. if (!str) break;
  10118. f = atof(str);
  10119. if (f < 0)
  10120. val[i] = 0;
  10121. else if (f > UINT_MAX) {
  10122. val[i] = UINT_MAX;
  10123. if (!warning) {
  10124. warning = 1;
  10125. ShowWarning("pc_readdb (exp.txt): Required exp per level is capped to %u\n", UINT_MAX);
  10126. }
  10127. } else
  10128. val[i] = (unsigned int)f;
  10129. str = strchr(str,sep);
  10130. if (str)
  10131. *str++=0;
  10132. }
  10133. //Zero up the remaining.
  10134. for(j=i; j < max; j++)
  10135. val[j] = 0;
  10136. return i;
  10137. }
  10138. /*==========================================
  10139. * sub DB reading.
  10140. * Function used to read skill_tree.txt
  10141. *------------------------------------------*/
  10142. static bool pc_readdb_skilltree(char* fields[], int columns, int current)
  10143. {
  10144. uint32 baselv, joblv, baselv_max, joblv_max;
  10145. uint16 skill_id, skill_lv, skill_lv_max;
  10146. int idx, class_;
  10147. unsigned int i, offset, skill_idx;
  10148. class_ = atoi(fields[0]);
  10149. skill_id = (uint16)atoi(fields[1]);
  10150. skill_lv = (uint16)atoi(fields[2]);
  10151. if (columns == 5 + MAX_PC_SKILL_REQUIRE * 2) { // Base/Job level requirement extra columns
  10152. baselv = (uint32)atoi(fields[3]);
  10153. joblv = (uint32)atoi(fields[4]);
  10154. offset = 5;
  10155. }
  10156. else if (columns == 3 + MAX_PC_SKILL_REQUIRE * 2) {
  10157. baselv = joblv = 0;
  10158. offset = 3;
  10159. }
  10160. else {
  10161. ShowWarning("pc_readdb_skilltree: Invalid number of colums in skill %hu of job %d's tree.\n", skill_id, class_);
  10162. return false;
  10163. }
  10164. if(!pcdb_checkid(class_))
  10165. {
  10166. ShowWarning("pc_readdb_skilltree: Invalid job class %d specified.\n", class_);
  10167. return false;
  10168. }
  10169. idx = pc_class2idx(class_);
  10170. if (!skill_get_index(skill_id)) {
  10171. ShowWarning("pc_readdb_skilltree: Unable to load skill %hu into job %d's tree.\n", skill_id, class_);
  10172. return false;
  10173. }
  10174. if (skill_lv > (skill_lv_max = skill_get_max(skill_id))) {
  10175. 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);
  10176. skill_lv = skill_lv_max;
  10177. }
  10178. if (baselv > (baselv_max = pc_class_maxbaselv(class_))) {
  10179. 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);
  10180. baselv = baselv_max;
  10181. }
  10182. if (joblv > (joblv_max = pc_class_maxjoblv(class_))) {
  10183. 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);
  10184. joblv = joblv_max;
  10185. }
  10186. //This is to avoid adding two lines for the same skill. [Skotlex]
  10187. 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 );
  10188. if( skill_idx == MAX_SKILL_TREE )
  10189. {
  10190. 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_);
  10191. return false;
  10192. }
  10193. else if(skill_tree[idx][skill_idx].skill_id)
  10194. {
  10195. ShowNotice("pc_readdb_skilltree: Overwriting skill %hu for job %d.\n", skill_id, class_);
  10196. }
  10197. skill_tree[idx][skill_idx].skill_id = skill_id;
  10198. skill_tree[idx][skill_idx].skill_lv = skill_lv;
  10199. skill_tree[idx][skill_idx].baselv = baselv;
  10200. skill_tree[idx][skill_idx].joblv = joblv;
  10201. for(i = 0; i < MAX_PC_SKILL_REQUIRE; i++)
  10202. {
  10203. skill_id = (uint16)atoi(fields[i * 2 + offset]);
  10204. skill_lv = (uint16)atoi(fields[i * 2 + offset + 1]);
  10205. if (skill_id == 0) {
  10206. if (skill_tree[idx][skill_idx].need[i].skill_id > 0) { // Remove pre-requisite
  10207. skill_tree[idx][skill_idx].need[i].skill_id = 0;
  10208. skill_tree[idx][skill_idx].need[i].skill_lv = 0;
  10209. }
  10210. continue;
  10211. }
  10212. if (skill_id > MAX_SKILL_ID || !skill_get_index(skill_id)) {
  10213. ShowWarning("pc_readdb_skilltree: Unable to load requirement skill %hu into job %d's tree.", skill_id, class_);
  10214. return false;
  10215. }
  10216. if (skill_lv > (skill_lv_max = skill_get_max(skill_id))) {
  10217. 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);
  10218. skill_lv = skill_lv_max;
  10219. }
  10220. skill_tree[idx][skill_idx].need[i].skill_id = skill_id;
  10221. skill_tree[idx][skill_idx].need[i].skill_lv = skill_lv;
  10222. }
  10223. return true;
  10224. }
  10225. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  10226. static bool pc_readdb_levelpenalty(char* fields[], int columns, int current)
  10227. {
  10228. int type, class_, diff;
  10229. type = atoi(fields[0]); //1=experience, 2=item drop
  10230. class_ = atoi(fields[1]);
  10231. diff = atoi(fields[2]);
  10232. if( type != 1 && type != 2 ){
  10233. ShowWarning("pc_readdb_levelpenalty: Invalid type %d specified.\n", type);
  10234. return false;
  10235. }
  10236. if( !CHK_CLASS(class_) ){
  10237. ShowWarning("pc_readdb_levelpenalty: Invalid class %d specified.\n", class_);
  10238. return false;
  10239. }
  10240. diff = min(diff, MAX_LEVEL);
  10241. if( diff < 0 )
  10242. diff = min(MAX_LEVEL + ( ~(diff) + 1 ), MAX_LEVEL*2);
  10243. level_penalty[type][class_][diff] = atoi(fields[3]);
  10244. return true;
  10245. }
  10246. #endif
  10247. /** [Cydh]
  10248. * Calculates base hp of player. Reference: http://irowiki.org/wiki/Max_HP
  10249. * @param level Base level of player
  10250. * @param class_ Job ID @see enum e_job
  10251. * @return base_hp
  10252. */
  10253. static unsigned int pc_calc_basehp(uint16 level, uint16 class_) {
  10254. double base_hp;
  10255. uint16 i, idx = pc_class2idx(class_);
  10256. base_hp = 35 + level * (job_info[idx].hp_multiplicator/100.);
  10257. #ifndef RENEWAL
  10258. if(level >= 10 && (class_ == JOB_NINJA || class_ == JOB_GUNSLINGER)) base_hp += 90;
  10259. #endif
  10260. for (i = 2; i <= level; i++)
  10261. base_hp += floor(((job_info[idx].hp_factor/100.) * i) + 0.5); //Don't have round()
  10262. if (class_ == JOB_SUMMONER)
  10263. base_hp += floor((base_hp / 2) + 0.5);
  10264. return (unsigned int)base_hp;
  10265. }
  10266. /** [Playtester]
  10267. * Calculates base sp of player.
  10268. * @param level Base level of player
  10269. * @param class_ Job ID @see enum e_job
  10270. * @return base_sp
  10271. */
  10272. static unsigned int pc_calc_basesp(uint16 level, uint16 class_) {
  10273. double base_sp;
  10274. uint16 idx = pc_class2idx(class_);
  10275. base_sp = 10 + floor(level * (job_info[idx].sp_factor / 100.));
  10276. switch (class_) {
  10277. case JOB_NINJA:
  10278. if (level >= 10)
  10279. base_sp -= 22;
  10280. else
  10281. base_sp = 11 + 3*level;
  10282. break;
  10283. case JOB_GUNSLINGER:
  10284. if (level > 10)
  10285. base_sp -= 18;
  10286. else
  10287. base_sp = 9 + 3*level;
  10288. break;
  10289. case JOB_SUMMONER:
  10290. base_sp -= floor(base_sp / 2);
  10291. break;
  10292. }
  10293. return (unsigned int)base_sp;
  10294. }
  10295. //Reading job_db1.txt line, (class,weight,HPFactor,HPMultiplicator,SPFactor,aspd/lvl...)
  10296. static bool pc_readdb_job1(char* fields[], int columns, int current){
  10297. int idx, class_;
  10298. unsigned int i;
  10299. class_ = atoi(fields[0]);
  10300. if (!pcdb_checkid(class_)) {
  10301. ShowWarning("status_readdb_job1: Invalid job class %d specified.\n", class_);
  10302. return false;
  10303. }
  10304. idx = pc_class2idx(class_);
  10305. job_info[idx].max_weight_base = atoi(fields[1]);
  10306. job_info[idx].hp_factor = atoi(fields[2]);
  10307. job_info[idx].hp_multiplicator = atoi(fields[3]);
  10308. job_info[idx].sp_factor = atoi(fields[4]);
  10309. #ifdef RENEWAL_ASPD
  10310. for(i = 0; i <= MAX_WEAPON_TYPE; i++)
  10311. #else
  10312. for(i = 0; i < MAX_WEAPON_TYPE; i++)
  10313. #endif
  10314. {
  10315. job_info[idx].aspd_base[i] = atoi(fields[i+5]);
  10316. }
  10317. return true;
  10318. }
  10319. //Reading job_db2.txt line (class,JobLv1,JobLv2,JobLv3,...)
  10320. static bool pc_readdb_job2(char* fields[], int columns, int current)
  10321. {
  10322. int idx, class_, i;
  10323. class_ = atoi(fields[0]);
  10324. if(!pcdb_checkid(class_))
  10325. {
  10326. ShowWarning("status_readdb_job2: Invalid job class %d specified.\n", class_);
  10327. return false;
  10328. }
  10329. idx = pc_class2idx(class_);
  10330. for(i = 1; i < columns; i++)
  10331. {
  10332. job_info[idx].job_bonus[i-1] = atoi(fields[i]);
  10333. }
  10334. return true;
  10335. }
  10336. //Reading job_exp.txt line
  10337. //Max Level,Class list,Type (0 - Base Exp; 1 - Job Exp),Exp/lvl...
  10338. static bool pc_readdb_job_exp(char* fields[], int columns, int current)
  10339. {
  10340. int idx, i, type;
  10341. int job_id,job_count,jobs[CLASS_COUNT];
  10342. unsigned int ui, maxlvl;
  10343. maxlvl = atoi(fields[0]);
  10344. if(maxlvl > MAX_LEVEL || maxlvl<1){
  10345. ShowError("pc_readdb_job_exp: Invalid maxlevel %d specified.\n", maxlvl);
  10346. return false;
  10347. }
  10348. if((maxlvl+3) > columns){ //nb values = (maxlvl-startlvl)+1-index1stvalue
  10349. ShowError("pc_readdb_job_exp: Number of columns %d defined is too low for max level %d.\n",columns,maxlvl);
  10350. return false;
  10351. }
  10352. type = atoi(fields[2]);
  10353. if(type < 0 || type > 1){
  10354. ShowError("pc_readdb_job_exp: Invalid type %d specified.\n", type);
  10355. return false;
  10356. }
  10357. job_count = pc_split_atoi(fields[1],jobs,':',CLASS_COUNT);
  10358. if (job_count < 1)
  10359. return false;
  10360. job_id = jobs[0];
  10361. if(!pcdb_checkid(job_id)){
  10362. ShowError("pc_readdb_job_exp: Invalid job class %d specified.\n", job_id);
  10363. return false;
  10364. }
  10365. idx = pc_class2idx(job_id);
  10366. job_info[idx].max_level[type] = maxlvl;
  10367. for(i=0; i<maxlvl; i++)
  10368. job_info[idx].exp_table[type][i] = ((uint32) atoi(fields[3+i]));
  10369. //Reverse check in case the array has a bunch of trailing zeros... [Skotlex]
  10370. //The reasoning behind the -2 is this... if the max level is 5, then the array
  10371. //should look like this:
  10372. //0: x, 1: x, 2: x: 3: x 4: 0 <- last valid value is at 3.
  10373. while ((ui = job_info[idx].max_level[type]) >= 2 && job_info[idx].exp_table[type][ui-2] <= 0)
  10374. job_info[idx].max_level[type]--;
  10375. if (job_info[idx].max_level[type] < maxlvl) {
  10376. 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]);
  10377. ShowInfo("Filling the missing values with the last exp entry.\n");
  10378. //Fill the requested values with the last entry.
  10379. ui = (job_info[idx].max_level[type] <= 2? 0: job_info[idx].max_level[type]-2);
  10380. for (; ui+2 < maxlvl; ui++)
  10381. job_info[idx].exp_table[type][ui] = job_info[idx].exp_table[type][ui-1];
  10382. job_info[idx].max_level[type] = maxlvl;
  10383. }
  10384. // ShowInfo("%s - Class %d: %d\n", type?"Job":"Base", job_id, job_info[idx].max_level[type]);
  10385. for (i = 1; i < job_count; i++) {
  10386. job_id = jobs[i];
  10387. if (!pcdb_checkid(job_id)) {
  10388. ShowError("pc_readdb_job_exp: Invalid job ID %d.\n", job_id);
  10389. continue;
  10390. }
  10391. idx = pc_class2idx(job_id);
  10392. 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]));
  10393. job_info[idx].max_level[type] = maxlvl;
  10394. // ShowInfo("%s - Class %d: %u\n", type?"Job":"Base", job_id, job_info[idx].max_level[type]);
  10395. }
  10396. return true;
  10397. }
  10398. /**
  10399. * #ifdef HP_SP_TABLES, reads 'job_basehpsp_db.txt to replace hp/sp results from formula
  10400. * startlvl,endlvl,class,type,values...
  10401. */
  10402. #ifdef HP_SP_TABLES
  10403. static bool pc_readdb_job_basehpsp(char* fields[], int columns, int current)
  10404. {
  10405. int i, startlvl, endlvl;
  10406. int job_count,jobs[CLASS_COUNT];
  10407. short type;
  10408. startlvl = atoi(fields[0]);
  10409. if(startlvl<1){
  10410. ShowError("pc_readdb_job_basehpsp: Invalid start level %d specified.\n", startlvl);
  10411. return false;
  10412. }
  10413. endlvl = atoi(fields[1]);
  10414. if(endlvl<1 || endlvl<startlvl){
  10415. ShowError("pc_readdb_job_basehpsp: Invalid end level %d specified.\n", endlvl);
  10416. return false;
  10417. }
  10418. if((endlvl-startlvl+1+4) > columns){ //nb values = (maxlvl-startlvl)+1-index1stvalue
  10419. 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);
  10420. return false;
  10421. }
  10422. type = atoi(fields[3]);
  10423. if(type < 0 || type > 1){
  10424. ShowError("pc_readdb_job_basehpsp: Invalid type %d specified.\n", type);
  10425. return false;
  10426. }
  10427. job_count = pc_split_atoi(fields[2],jobs,':',CLASS_COUNT);
  10428. if (job_count < 1)
  10429. return false;
  10430. for (i = 0; i < job_count; i++) {
  10431. int idx, job_id = jobs[i], use_endlvl;
  10432. if (!pcdb_checkid(job_id)) {
  10433. ShowError("pc_readdb_job_basehpsp: Invalid job class %d specified.\n", job_id);
  10434. return false;
  10435. }
  10436. idx = pc_class2idx(job_id);
  10437. if (startlvl > job_info[idx].max_level[0]) {
  10438. ShowError("pc_readdb_job_basehpsp: Invalid start level %d specified.\n", startlvl);
  10439. return false;
  10440. }
  10441. //Just read until available max level for this job, don't use MAX_LEVEL!
  10442. use_endlvl = endlvl;
  10443. if (use_endlvl > job_info[idx].max_level[0])
  10444. use_endlvl = job_info[idx].max_level[0];
  10445. if(type == 0) { //hp type
  10446. uint16 j;
  10447. for(j = 0; j < use_endlvl; j++) {
  10448. if (atoi(fields[j+4])) {
  10449. uint16 lvl_idx = startlvl-1+j;
  10450. job_info[idx].base_hp[lvl_idx] = atoi(fields[j+4]);
  10451. //Tells if this HP is lower than previous level (but not for 99->100)
  10452. if (lvl_idx-1 >= 0 && lvl_idx != 99 && job_info[idx].base_hp[lvl_idx] < job_info[idx].base_hp[lvl_idx-1])
  10453. 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",
  10454. current,j+4,job_id,lvl_idx+1,job_info[idx].base_hp[lvl_idx-1],job_info[idx].base_hp[lvl_idx]);
  10455. }
  10456. }
  10457. }
  10458. else { //sp type
  10459. uint16 j;
  10460. for(j = 0; j < use_endlvl; j++) {
  10461. if (atoi(fields[j+4])) {
  10462. uint16 lvl_idx = startlvl-1+j;
  10463. job_info[idx].base_sp[lvl_idx] = atoi(fields[j+4]);
  10464. //Tells if this SP is lower than previous level (but not for 99->100)
  10465. if (lvl_idx-1 >= 0 && lvl_idx != 99 && job_info[idx].base_sp[lvl_idx] < job_info[idx].base_sp[lvl_idx-1])
  10466. 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",
  10467. current,j+4,job_id,lvl_idx+1,job_info[idx].base_sp[lvl_idx-1],job_info[idx].base_sp[lvl_idx]);
  10468. }
  10469. }
  10470. }
  10471. }
  10472. return true;
  10473. }
  10474. #endif
  10475. /** [Cydh]
  10476. * Reads 'job_param_db.txt' to check max. param each job and store them to job_info[].max_param.*
  10477. */
  10478. static bool pc_readdb_job_param(char* fields[], int columns, int current)
  10479. {
  10480. int64 class_tmp;
  10481. int idx, class_;
  10482. uint16 str, agi, vit, int_, dex, luk;
  10483. script_get_constant(trim(fields[0]),&class_tmp);
  10484. class_ = static_cast<int>(class_tmp);
  10485. if ((idx = pc_class2idx(class_)) < 0) {
  10486. ShowError("pc_readdb_job_param: Invalid job '%s'. Skipping!",fields[0]);
  10487. return false;
  10488. }
  10489. str = cap_value(atoi(fields[1]),10,SHRT_MAX);
  10490. agi = atoi(fields[2]) ? cap_value(atoi(fields[2]),10,SHRT_MAX) : str;
  10491. vit = atoi(fields[3]) ? cap_value(atoi(fields[3]),10,SHRT_MAX) : str;
  10492. int_ = atoi(fields[4]) ? cap_value(atoi(fields[4]),10,SHRT_MAX) : str;
  10493. dex = atoi(fields[5]) ? cap_value(atoi(fields[5]),10,SHRT_MAX) : str;
  10494. luk = atoi(fields[6]) ? cap_value(atoi(fields[6]),10,SHRT_MAX) : str;
  10495. job_info[idx].max_param.str = str;
  10496. job_info[idx].max_param.agi = agi;
  10497. job_info[idx].max_param.vit = vit;
  10498. job_info[idx].max_param.int_ = int_;
  10499. job_info[idx].max_param.dex = dex;
  10500. job_info[idx].max_param.luk = luk;
  10501. return true;
  10502. }
  10503. /**
  10504. * Read job_noenter_map.txt
  10505. **/
  10506. static bool pc_readdb_job_noenter_map(char *str[], int columns, int current) {
  10507. int idx, class_ = -1;
  10508. int64 class_tmp;
  10509. if (ISDIGIT(str[0][0])) {
  10510. class_ = atoi(str[0]);
  10511. } else {
  10512. if (!script_get_constant(str[0], &class_tmp)) {
  10513. ShowError("pc_readdb_job_noenter_map: Invalid job %s specified.\n", str[0]);
  10514. return false;
  10515. }
  10516. class_ = static_cast<int>(class_tmp);
  10517. }
  10518. if (!pcdb_checkid(class_) || (idx = pc_class2idx(class_)) < 0) {
  10519. ShowError("pc_readdb_job_noenter_map: Invalid job %d specified.\n", class_);
  10520. return false;
  10521. }
  10522. job_info[idx].noenter_map.zone = atoi(str[1]);
  10523. job_info[idx].noenter_map.group_lv = atoi(str[2]);
  10524. return true;
  10525. }
  10526. static int pc_read_statsdb(const char *basedir, int last_s, bool silent){
  10527. int i=1;
  10528. char line[24000]; //FIXME this seem too big
  10529. FILE *fp;
  10530. sprintf(line, "%s/statpoint.txt", basedir);
  10531. fp=fopen(line,"r");
  10532. if(fp == NULL){
  10533. if(silent==0) ShowWarning("Can't read '" CL_WHITE "%s" CL_RESET "'... Generating DB.\n",line);
  10534. return max(last_s,i);
  10535. } else {
  10536. int entries=0;
  10537. while(fgets(line, sizeof(line), fp))
  10538. {
  10539. int stat;
  10540. trim(line);
  10541. if(line[0] == '\0' || (line[0]=='/' && line[1]=='/'))
  10542. continue;
  10543. if ((stat=strtoul(line,NULL,10))<0)
  10544. stat=0;
  10545. if (i > MAX_LEVEL)
  10546. break;
  10547. statp[i]=stat;
  10548. i++;
  10549. entries++;
  10550. }
  10551. fclose(fp);
  10552. ShowStatus("Done reading '" CL_WHITE "%d" CL_RESET "' entries in '" CL_WHITE "%s/%s" CL_RESET "'.\n", entries, basedir,"statpoint.txt");
  10553. }
  10554. return max(last_s,i);
  10555. }
  10556. /*==========================================
  10557. * pc DB reading.
  10558. * job_exp.txt - required experience values
  10559. * skill_tree.txt - skill tree for every class
  10560. * attr_fix.txt - elemental adjustment table
  10561. * job_db1.txt - job,weight,hp_factor,hp_multiplicator,sp_factor,aspds/lvl
  10562. * job_db2.txt - job,stats bonuses/lvl
  10563. * job_maxhpsp_db.txt - strtlvl,maxlvl,job,type,values/lvl (values=hp|sp)
  10564. *------------------------------------------*/
  10565. void pc_readdb(void) {
  10566. int i, k, s = 1;
  10567. const char* dbsubpath[] = {
  10568. "",
  10569. "/" DBIMPORT,
  10570. //add other path here
  10571. };
  10572. //reset
  10573. memset(job_info,0,sizeof(job_info)); // job_info table
  10574. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  10575. sv_readdb(db_path, DBPATH "level_penalty.txt", ',', 4, 4, -1, &pc_readdb_levelpenalty, 0);
  10576. sv_readdb(db_path, DBIMPORT"/level_penalty.txt", ',', 4, 4, -1, &pc_readdb_levelpenalty, 1);
  10577. for( k=1; k < 3; k++ ){ // fill in the blanks
  10578. int j;
  10579. for( j = 0; j < CLASS_ALL; j++ ){
  10580. int tmp = 0;
  10581. for( i = 0; i < MAX_LEVEL*2; i++ ){
  10582. if( i == MAX_LEVEL+1 )
  10583. tmp = level_penalty[k][j][0];// reset
  10584. if( level_penalty[k][j][i] > 0 )
  10585. tmp = level_penalty[k][j][i];
  10586. else
  10587. level_penalty[k][j][i] = tmp;
  10588. }
  10589. }
  10590. }
  10591. #endif
  10592. // reset then read statspoint
  10593. memset(statp,0,sizeof(statp));
  10594. for(i=0; i<ARRAYLENGTH(dbsubpath); i++){
  10595. uint8 n1 = (uint8)(strlen(db_path)+strlen(dbsubpath[i])+1);
  10596. uint8 n2 = (uint8)(strlen(db_path)+strlen(DBPATH)+strlen(dbsubpath[i])+1);
  10597. char* dbsubpath1 = (char*)aMalloc(n1+1);
  10598. char* dbsubpath2 = (char*)aMalloc(n2+1);
  10599. if(i==0) {
  10600. safesnprintf(dbsubpath1,n1,"%s%s",db_path,dbsubpath[i]);
  10601. safesnprintf(dbsubpath2,n2,"%s/%s%s",db_path,DBPATH,dbsubpath[i]);
  10602. }
  10603. else {
  10604. safesnprintf(dbsubpath1,n1,"%s%s",db_path,dbsubpath[i]);
  10605. safesnprintf(dbsubpath2,n1,"%s%s",db_path,dbsubpath[i]);
  10606. }
  10607. s = pc_read_statsdb(dbsubpath2,s,i > 0);
  10608. if (i == 0)
  10609. #ifdef RENEWAL_ASPD // Paths are hardcoded here to specifically pick the correct database
  10610. sv_readdb(dbsubpath1, "re/job_db1.txt",',',6+MAX_WEAPON_TYPE,6+MAX_WEAPON_TYPE,CLASS_COUNT,&pc_readdb_job1, false);
  10611. #else
  10612. sv_readdb(dbsubpath1, "pre-re/job_db1.txt",',',5+MAX_WEAPON_TYPE,5+MAX_WEAPON_TYPE,CLASS_COUNT,&pc_readdb_job1, false);
  10613. #endif
  10614. else
  10615. sv_readdb(dbsubpath1, "job_db1.txt",',',5+MAX_WEAPON_TYPE,6+MAX_WEAPON_TYPE,CLASS_COUNT,&pc_readdb_job1, true);
  10616. sv_readdb(dbsubpath1, "job_db2.txt",',',1,1+MAX_LEVEL,CLASS_COUNT,&pc_readdb_job2, i > 0);
  10617. sv_readdb(dbsubpath2, "job_exp.txt",',',4,1000+3,CLASS_COUNT*2,&pc_readdb_job_exp, i > 0); //support till 1000lvl
  10618. #ifdef HP_SP_TABLES
  10619. 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!
  10620. #endif
  10621. sv_readdb(dbsubpath2, "job_param_db.txt", ',', 2, PARAM_MAX+1, CLASS_COUNT, &pc_readdb_job_param, i > 0);
  10622. sv_readdb(dbsubpath2, "job_noenter_map.txt", ',', 3, 3, CLASS_COUNT, &pc_readdb_job_noenter_map, i > 0);
  10623. aFree(dbsubpath1);
  10624. aFree(dbsubpath2);
  10625. }
  10626. // Reset and read skilltree - needs to be read after pc_readdb_job_exp to get max base and job levels
  10627. memset(skill_tree, 0, sizeof(skill_tree));
  10628. 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);
  10629. 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);
  10630. // generate the remaining parts of the db if necessary
  10631. k = battle_config.use_statpoint_table; //save setting
  10632. battle_config.use_statpoint_table = 0; //temporarily disable to force pc_gets_status_point use default values
  10633. statp[0] = 45; // seed value
  10634. for (; s <= MAX_LEVEL; s++)
  10635. statp[s] = statp[s-1] + pc_gets_status_point(s-1);
  10636. battle_config.use_statpoint_table = k; //restore setting
  10637. //Checking if all class have their data
  10638. for (i = 0; i < JOB_MAX; i++) {
  10639. int idx;
  10640. uint16 j;
  10641. if (!pcdb_checkid(i))
  10642. continue;
  10643. if (i == JOB_WEDDING || i == JOB_XMAS || i == JOB_SUMMER || i == JOB_HANBOK || i == JOB_OKTOBERFEST || i == JOB_SUMMER2)
  10644. continue; //Classes that do not need exp tables.
  10645. idx = pc_class2idx(i);
  10646. if (!job_info[idx].max_level[0])
  10647. ShowWarning("Class %s (%d) does not have a base exp table.\n", job_name(i), i);
  10648. if (!job_info[idx].max_level[1])
  10649. ShowWarning("Class %s (%d) does not have a job exp table.\n", job_name(i), i);
  10650. //Init and checking the empty value of Base HP/SP [Cydh]
  10651. for (j = 0; j < (job_info[idx].max_level[0] ? job_info[idx].max_level[0] : MAX_LEVEL); j++) {
  10652. if (job_info[idx].base_hp[j] == 0)
  10653. job_info[idx].base_hp[j] = pc_calc_basehp(j+1,i);
  10654. if (job_info[idx].base_sp[j] == 0)
  10655. job_info[idx].base_sp[j] = pc_calc_basesp(j+1,i);
  10656. }
  10657. }
  10658. }
  10659. // Read MOTD on startup. [Valaris]
  10660. int pc_read_motd(void)
  10661. {
  10662. FILE* fp;
  10663. // clear old MOTD
  10664. memset(motd_text, 0, sizeof(motd_text));
  10665. // read current MOTD
  10666. if( ( fp = fopen(motd_txt, "r") ) != NULL )
  10667. {
  10668. unsigned int entries = 0;
  10669. char buf[CHAT_SIZE_MAX];
  10670. while( entries < MOTD_LINE_SIZE && fgets(buf, CHAT_SIZE_MAX, fp) )
  10671. {
  10672. unsigned int lines = 0;
  10673. size_t len;
  10674. lines++;
  10675. if( buf[0] == '/' && buf[1] == '/' )
  10676. continue;
  10677. len = strlen(buf);
  10678. while( len && ( buf[len-1] == '\r' || buf[len-1] == '\n' ) ) // strip trailing EOL characters
  10679. len--;
  10680. if( len ) {
  10681. char * ptr;
  10682. buf[len] = 0;
  10683. if( ( ptr = strstr(buf, " :") ) != NULL && ptr-buf >= NAME_LENGTH ) // crashes newer clients
  10684. 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);
  10685. }
  10686. else {// empty line
  10687. buf[0] = ' ';
  10688. buf[1] = 0;
  10689. }
  10690. safestrncpy(motd_text[entries], buf, CHAT_SIZE_MAX);
  10691. entries++;
  10692. }
  10693. fclose(fp);
  10694. ShowStatus("Done reading '" CL_WHITE "%u" CL_RESET "' entries in '" CL_WHITE "%s" CL_RESET "'.\n", entries, motd_txt);
  10695. }
  10696. else
  10697. ShowWarning("File '" CL_WHITE "%s" CL_RESET "' not found.\n", motd_txt);
  10698. return 0;
  10699. }
  10700. void pc_itemcd_do(struct map_session_data *sd, bool load) {
  10701. int i,cursor = 0;
  10702. struct item_cd* cd = NULL;
  10703. if( load ) {
  10704. if( !(cd = (struct item_cd*)idb_get(itemcd_db, sd->status.char_id)) ) {
  10705. // no item cooldown is associated with this character
  10706. return;
  10707. }
  10708. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  10709. if( cd->nameid[i] && DIFF_TICK(gettick(),cd->tick[i]) < 0 ) {
  10710. sd->item_delay[cursor].tick = cd->tick[i];
  10711. sd->item_delay[cursor].nameid = cd->nameid[i];
  10712. cursor++;
  10713. }
  10714. }
  10715. idb_remove(itemcd_db,sd->status.char_id);
  10716. } else {
  10717. if( !(cd = (struct item_cd*)idb_get(itemcd_db,sd->status.char_id)) ) {
  10718. // create a new skill cooldown object for map storage
  10719. CREATE( cd, struct item_cd, 1 );
  10720. idb_put( itemcd_db, sd->status.char_id, cd );
  10721. }
  10722. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  10723. if( sd->item_delay[i].nameid && DIFF_TICK(gettick(),sd->item_delay[i].tick) < 0 ) {
  10724. cd->tick[cursor] = sd->item_delay[i].tick;
  10725. cd->nameid[cursor] = sd->item_delay[i].nameid;
  10726. cursor++;
  10727. }
  10728. }
  10729. }
  10730. return;
  10731. }
  10732. /**
  10733. * Add item delay to player's item delay data
  10734. * @param sd Player
  10735. * @param id Item data
  10736. * @param tick Current tick
  10737. * @param n Item index in inventory
  10738. * @return 0: No delay, can consume item.
  10739. * 1: Has delay, cancel consumption.
  10740. **/
  10741. uint8 pc_itemcd_add(struct map_session_data *sd, struct item_data *id, t_tick tick, unsigned short n) {
  10742. int i;
  10743. ARR_FIND(0, MAX_ITEMDELAYS, i, sd->item_delay[i].nameid == id->nameid );
  10744. if( i == MAX_ITEMDELAYS ) /* item not found. try first empty now */
  10745. ARR_FIND(0, MAX_ITEMDELAYS, i, !sd->item_delay[i].nameid );
  10746. if( i < MAX_ITEMDELAYS ) {
  10747. if( sd->item_delay[i].nameid ) {// found
  10748. if( DIFF_TICK(sd->item_delay[i].tick, tick) > 0 ) {
  10749. t_tick e_tick = DIFF_TICK(sd->item_delay[i].tick, tick)/1000;
  10750. char e_msg[CHAT_SIZE_MAX];
  10751. if( e_tick > 99 )
  10752. sprintf(e_msg,msg_txt(sd,379), // Item Failed. [%s] is cooling down. Wait %.1f minutes.
  10753. itemdb_jname(sd->item_delay[i].nameid), (double)e_tick / 60);
  10754. else
  10755. sprintf(e_msg,msg_txt(sd,380), // Item Failed. [%s] is cooling down. Wait %d seconds.
  10756. itemdb_jname(sd->item_delay[i].nameid), e_tick+1);
  10757. clif_messagecolor(&sd->bl,color_table[COLOR_YELLOW],e_msg,false,SELF);
  10758. return 1; // Delay has not expired yet
  10759. }
  10760. } else {// not yet used item (all slots are initially empty)
  10761. sd->item_delay[i].nameid = id->nameid;
  10762. }
  10763. if( !(id->nameid == ITEMID_REINS_OF_MOUNT && sd->sc.option&(OPTION_WUGRIDER|OPTION_RIDING|OPTION_DRAGON|OPTION_MADOGEAR)) )
  10764. sd->item_delay[i].tick = tick + sd->inventory_data[n]->delay;
  10765. } else {// should not happen
  10766. ShowError("pc_itemcd_add: Exceeded item delay array capacity! (nameid=%hu, char_id=%d)\n", id->nameid, sd->status.char_id);
  10767. }
  10768. //clean up used delays so we can give room for more
  10769. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  10770. if( DIFF_TICK(sd->item_delay[i].tick, tick) <= 0 ) {
  10771. sd->item_delay[i].tick = 0;
  10772. sd->item_delay[i].nameid = 0;
  10773. }
  10774. }
  10775. return 0;
  10776. }
  10777. /**
  10778. * Check if player has delay to reuse item
  10779. * @param sd Player
  10780. * @param id Item data
  10781. * @param tick Current tick
  10782. * @param n Item index in inventory
  10783. * @return 0: No delay, can consume item.
  10784. * 1: Has delay, cancel consumption.
  10785. **/
  10786. uint8 pc_itemcd_check(struct map_session_data *sd, struct item_data *id, t_tick tick, unsigned short n) {
  10787. struct status_change *sc = NULL;
  10788. nullpo_retr(0, sd);
  10789. nullpo_retr(0, id);
  10790. // Do normal delay assignment
  10791. if (id->delay_sc <= SC_NONE || id->delay_sc >= SC_MAX || !(sc = &sd->sc))
  10792. return pc_itemcd_add(sd, id, tick, n);
  10793. // Send reply of delay remains
  10794. if (sc->data[id->delay_sc]) {
  10795. const struct TimerData *timer = get_timer(sc->data[id->delay_sc]->timer);
  10796. clif_msg_value(sd, ITEM_REUSE_LIMIT, (int)(timer ? DIFF_TICK(timer->tick, tick) / 1000 : 99));
  10797. return 1;
  10798. }
  10799. sc_start(&sd->bl, &sd->bl, (sc_type)id->delay_sc, 100, id->nameid, id->delay);
  10800. return 0;
  10801. }
  10802. /**
  10803. * Clear the dmglog data from player
  10804. * @param sd
  10805. * @param md
  10806. **/
  10807. static void pc_clear_log_damage_sub(uint32 char_id, struct mob_data *md)
  10808. {
  10809. uint8 i;
  10810. ARR_FIND(0,DAMAGELOG_SIZE,i,md->dmglog[i].id == char_id);
  10811. if (i < DAMAGELOG_SIZE) {
  10812. md->dmglog[i].id = 0;
  10813. md->dmglog[i].dmg = 0;
  10814. md->dmglog[i].flag = 0;
  10815. }
  10816. }
  10817. /**
  10818. * Add log to player's dmglog
  10819. * @param sd
  10820. * @param id Monster's GID
  10821. **/
  10822. void pc_damage_log_add(struct map_session_data *sd, int id)
  10823. {
  10824. uint8 i = 0;
  10825. if (!sd || !id)
  10826. return;
  10827. //Only store new data, don't need to renew the old one with same id
  10828. ARR_FIND(0, DAMAGELOG_SIZE_PC, i, sd->dmglog[i] == id);
  10829. if (i < DAMAGELOG_SIZE_PC)
  10830. return;
  10831. for (i = 0; i < DAMAGELOG_SIZE_PC; i++) {
  10832. if (sd->dmglog[i] == 0) {
  10833. sd->dmglog[i] = id;
  10834. return;
  10835. }
  10836. }
  10837. }
  10838. /**
  10839. * Clear dmglog data from player
  10840. * @param sd
  10841. * @param id Monster's id
  10842. **/
  10843. void pc_damage_log_clear(struct map_session_data *sd, int id)
  10844. {
  10845. uint8 i;
  10846. struct mob_data *md = NULL;
  10847. if (!sd)
  10848. return;
  10849. if (!id) {
  10850. for (i = 0; i < DAMAGELOG_SIZE_PC; i++) {
  10851. if( !sd->dmglog[i] ) //skip the empty value
  10852. continue;
  10853. if ((md = map_id2md(sd->dmglog[i])))
  10854. pc_clear_log_damage_sub(sd->status.char_id,md);
  10855. sd->dmglog[i] = 0;
  10856. }
  10857. }
  10858. else {
  10859. if ((md = map_id2md(id)))
  10860. pc_clear_log_damage_sub(sd->status.char_id,md);
  10861. ARR_FIND(0,DAMAGELOG_SIZE_PC,i,sd->dmglog[i] == id); // find the id position
  10862. if (i < DAMAGELOG_SIZE_PC)
  10863. sd->dmglog[i] = 0;
  10864. }
  10865. }
  10866. /**
  10867. * Status change data arrived from char-server
  10868. * @param sd: Player data
  10869. */
  10870. void pc_scdata_received(struct map_session_data *sd) {
  10871. pc_inventory_rentals(sd); // Needed here to remove rentals that have Status Changes after chrif_load_scdata has finished
  10872. clif_weight_limit( sd );
  10873. if( pc_has_permission( sd, PC_PERM_ATTENDANCE ) && pc_attendance_enabled() && !pc_attendance_rewarded_today( sd ) ){
  10874. clif_ui_open( sd, OUT_UI_ATTENDANCE, pc_attendance_counter( sd ) );
  10875. }
  10876. sd->state.pc_loaded = true;
  10877. if (sd->state.connect_new == 0 && sd->fd) { // Character already loaded map! Gotta trigger LoadEndAck manually.
  10878. sd->state.connect_new = 1;
  10879. clif_parse_LoadEndAck(sd->fd, sd);
  10880. }
  10881. if (pc_iscarton(sd)) {
  10882. sd->cart_weight_max = 0; // Force a client refesh
  10883. status_calc_cart_weight(sd, (e_status_calc_weight_opt)(CALCWT_ITEM|CALCWT_MAXBONUS|CALCWT_CARTSTATE));
  10884. }
  10885. }
  10886. /**
  10887. * Check player account expiration time and rental item expirations
  10888. * @param sd: Player data
  10889. */
  10890. void pc_check_expiration(struct map_session_data *sd) {
  10891. #ifndef ENABLE_SC_SAVING
  10892. pc_inventory_rentals(sd); // Check here if Status Change saving is disabled
  10893. #endif
  10894. if (sd->expiration_time != 0) { //Don't display if it's unlimited or unknow value
  10895. time_t exp_time = sd->expiration_time;
  10896. char tmpstr[1024];
  10897. strftime(tmpstr,sizeof(tmpstr) - 1,msg_txt(sd,501),localtime(&exp_time)); // "Your account time limit is: %d-%m-%Y %H:%M:%S."
  10898. clif_wis_message(sd,wisp_server_name,tmpstr,strlen(tmpstr) + 1,0);
  10899. pc_expire_check(sd);
  10900. }
  10901. }
  10902. TIMER_FUNC(pc_expiration_timer){
  10903. struct map_session_data *sd = map_id2sd(id);
  10904. if( !sd ) return 0;
  10905. sd->expiration_tid = INVALID_TIMER;
  10906. if( sd->fd )
  10907. clif_authfail_fd(sd->fd,10);
  10908. map_quit(sd);
  10909. return 0;
  10910. }
  10911. TIMER_FUNC(pc_autotrade_timer){
  10912. struct map_session_data *sd = map_id2sd(id);
  10913. if (!sd)
  10914. return 0;
  10915. sd->autotrade_tid = INVALID_TIMER;
  10916. if (sd->state.autotrade&2)
  10917. vending_reopen(sd);
  10918. if (sd->state.autotrade&4)
  10919. buyingstore_reopen(sd);
  10920. if (!sd->vender_id && !sd->buyer_id) {
  10921. sd->state.autotrade = 0;
  10922. map_quit(sd);
  10923. }
  10924. return 0;
  10925. }
  10926. /* this timer exists only when a character with a expire timer > 24h is online */
  10927. /* it loops thru online players once an hour to check whether a new < 24h is available */
  10928. TIMER_FUNC(pc_global_expiration_timer){
  10929. struct s_mapiterator* iter;
  10930. struct map_session_data* sd;
  10931. iter = mapit_getallusers();
  10932. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  10933. if( sd->expiration_time )
  10934. pc_expire_check(sd);
  10935. mapit_free(iter);
  10936. return 0;
  10937. }
  10938. void pc_expire_check(struct map_session_data *sd) {
  10939. /* ongoing timer */
  10940. if( sd->expiration_tid != INVALID_TIMER )
  10941. return;
  10942. /* not within the next 24h, enable the global check */
  10943. if( sd->expiration_time > (time(NULL) + ((60 * 60) * 24)) ) {
  10944. /* global check not running, enable */
  10945. if( pc_expiration_tid == INVALID_TIMER ) /* Starts in 1h, repeats every hour */
  10946. pc_expiration_tid = add_timer_interval(gettick() + ((1000 * 60) * 60), pc_global_expiration_timer, 0, 0, ((1000 * 60) * 60));
  10947. return;
  10948. }
  10949. sd->expiration_tid = add_timer(gettick() + (unsigned int)(sd->expiration_time - time(NULL)) * 1000, pc_expiration_timer, sd->bl.id, 0);
  10950. }
  10951. /**
  10952. * Deposit some money to bank
  10953. * @param sd
  10954. * @param money Amount of money to deposit
  10955. **/
  10956. enum e_BANKING_DEPOSIT_ACK pc_bank_deposit(struct map_session_data *sd, int money) {
  10957. unsigned int limit_check = money + sd->bank_vault;
  10958. if( money <= 0 || limit_check > MAX_BANK_ZENY ) {
  10959. return BDA_OVERFLOW;
  10960. } else if ( money > sd->status.zeny ) {
  10961. return BDA_NO_MONEY;
  10962. }
  10963. if( pc_payzeny(sd,money, LOG_TYPE_BANK, NULL) )
  10964. return BDA_NO_MONEY;
  10965. sd->bank_vault += money;
  10966. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  10967. if( save_settings&CHARSAVE_BANK )
  10968. chrif_save(sd, CSAVE_NORMAL);
  10969. return BDA_SUCCESS;
  10970. }
  10971. /**
  10972. * Withdraw money from bank
  10973. * @param sd
  10974. * @param money Amount of money that will be withdrawn
  10975. **/
  10976. enum e_BANKING_WITHDRAW_ACK pc_bank_withdraw(struct map_session_data *sd, int money) {
  10977. unsigned int limit_check = money + sd->status.zeny;
  10978. if( money <= 0 ) {
  10979. return BWA_UNKNOWN_ERROR;
  10980. } else if ( money > sd->bank_vault ) {
  10981. return BWA_NO_MONEY;
  10982. } else if ( limit_check > MAX_ZENY ) {
  10983. /* no official response for this scenario exists. */
  10984. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,1495),false,SELF); //You can't withdraw that much money
  10985. return BWA_UNKNOWN_ERROR;
  10986. }
  10987. if( pc_getzeny(sd,money, LOG_TYPE_BANK, NULL) )
  10988. return BWA_NO_MONEY;
  10989. sd->bank_vault -= money;
  10990. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  10991. if( save_settings&CHARSAVE_BANK )
  10992. chrif_save(sd, CSAVE_NORMAL);
  10993. return BWA_SUCCESS;
  10994. }
  10995. /**
  10996. * Clear Crimson Marker data from caster
  10997. * @param sd: Player
  10998. **/
  10999. void pc_crimson_marker_clear(struct map_session_data *sd) {
  11000. uint8 i;
  11001. if (!sd)
  11002. return;
  11003. for (i = 0; i < MAX_SKILL_CRIMSON_MARKER; i++) {
  11004. struct block_list *bl = NULL;
  11005. if (sd->c_marker[i] && (bl = map_id2bl(sd->c_marker[i])))
  11006. status_change_end(bl,SC_C_MARKER,INVALID_TIMER);
  11007. sd->c_marker[i] = 0;
  11008. }
  11009. }
  11010. /**
  11011. * Show version to player
  11012. * @param sd: Player
  11013. **/
  11014. void pc_show_version(struct map_session_data *sd) {
  11015. const char* svn = get_svn_revision();
  11016. char buf[CHAT_SIZE_MAX];
  11017. if( svn[0] != UNKNOWN_VERSION )
  11018. sprintf(buf,msg_txt(sd,1295),"SVN: r",svn); //rAthena Version SVN: r%s
  11019. else {
  11020. const char* git = get_git_hash();
  11021. if( git[0] != UNKNOWN_VERSION )
  11022. sprintf(buf,msg_txt(sd,1295),"Git Hash: ",git); //rAthena Version Git Hash: %s
  11023. else
  11024. sprintf(buf,"%s",msg_txt(sd,1296)); //Cannot determine SVN/Git version.
  11025. }
  11026. clif_displaymessage(sd->fd,buf);
  11027. }
  11028. /**
  11029. * Run bonus_script on player
  11030. * @param sd
  11031. * @author [Cydh]
  11032. **/
  11033. void pc_bonus_script(struct map_session_data *sd) {
  11034. t_tick now = gettick();
  11035. struct linkdb_node *node = NULL, *next = NULL;
  11036. if (!sd || !(node = sd->bonus_script.head))
  11037. return;
  11038. while (node) {
  11039. struct s_bonus_script_entry *entry = NULL;
  11040. next = node->next;
  11041. if ((entry = (struct s_bonus_script_entry *)node->data)) {
  11042. // Only start timer for new bonus_script
  11043. if (entry->tid == INVALID_TIMER) {
  11044. if (entry->icon != EFST_BLANK) // Gives status icon if exist
  11045. clif_status_change(&sd->bl, entry->icon, 1, entry->tick, 1, 0, 0);
  11046. entry->tick += now;
  11047. entry->tid = add_timer(entry->tick, pc_bonus_script_timer, sd->bl.id, (intptr_t)entry);
  11048. }
  11049. if (entry->script)
  11050. run_script(entry->script, 0, sd->bl.id, 0);
  11051. else
  11052. ShowError("pc_bonus_script: The script has been removed somewhere. \"%s\"\n", StringBuf_Value(entry->script_buf));
  11053. }
  11054. node = next;
  11055. }
  11056. }
  11057. /**
  11058. * Add bonus_script to player
  11059. * @param sd Player
  11060. * @param script_str Script string
  11061. * @param dur Duration in ms
  11062. * @param icon EFST
  11063. * @param flag Flags @see enum e_bonus_script_flags
  11064. * @param type 0 - None, 1 - Buff, 2 - Debuff
  11065. * @return New created entry pointer or NULL if failed or NULL if duplicate fail
  11066. * @author [Cydh]
  11067. **/
  11068. 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) {
  11069. struct script_code *script = NULL;
  11070. struct linkdb_node *node = NULL;
  11071. struct s_bonus_script_entry *entry = NULL;
  11072. if (!sd)
  11073. return NULL;
  11074. if (!(script = parse_script(script_str, "bonus_script", 0, SCRIPT_IGNORE_EXTERNAL_BRACKETS))) {
  11075. ShowError("pc_bonus_script_add: Failed to parse script '%s' (CID:%d).\n", script_str, sd->status.char_id);
  11076. return NULL;
  11077. }
  11078. // Duplication checks
  11079. if ((node = sd->bonus_script.head)) {
  11080. while (node) {
  11081. entry = (struct s_bonus_script_entry *)node->data;
  11082. if (strcmpi(script_str, StringBuf_Value(entry->script_buf)) == 0) {
  11083. t_tick newdur = gettick() + dur;
  11084. if (flag&BSF_FORCE_REPLACE && entry->tick < newdur) { // Change duration
  11085. sett_tickimer(entry->tid, newdur);
  11086. script_free_code(script);
  11087. return NULL;
  11088. }
  11089. else if (flag&BSF_FORCE_DUPLICATE) // Allow duplicate
  11090. break;
  11091. else { // No duplicate bonus
  11092. script_free_code(script);
  11093. return NULL;
  11094. }
  11095. }
  11096. node = node->next;
  11097. }
  11098. }
  11099. CREATE(entry, struct s_bonus_script_entry, 1);
  11100. entry->script_buf = StringBuf_Malloc();
  11101. StringBuf_AppendStr(entry->script_buf, script_str);
  11102. entry->tid = INVALID_TIMER;
  11103. entry->flag = flag;
  11104. entry->icon = icon;
  11105. entry->tick = dur; // Use duration first, on run change to expire time
  11106. entry->type = type;
  11107. entry->script = script;
  11108. sd->bonus_script.count++;
  11109. return entry;
  11110. }
  11111. /**
  11112. * Remove bonus_script data from player
  11113. * @param sd: Target player
  11114. * @param list: Bonus script entry from player
  11115. * @author [Cydh]
  11116. **/
  11117. void pc_bonus_script_free_entry(struct map_session_data *sd, struct s_bonus_script_entry *entry) {
  11118. if (entry->tid != INVALID_TIMER)
  11119. delete_timer(entry->tid, pc_bonus_script_timer);
  11120. if (entry->script)
  11121. script_free_code(entry->script);
  11122. if (entry->script_buf)
  11123. StringBuf_Free(entry->script_buf);
  11124. if (sd) {
  11125. if (entry->icon != EFST_BLANK)
  11126. clif_status_load(&sd->bl, entry->icon, 0);
  11127. if (sd->bonus_script.count > 0)
  11128. sd->bonus_script.count--;
  11129. }
  11130. aFree(entry);
  11131. }
  11132. /**
  11133. * Do final process if no entry left
  11134. * @param sd
  11135. **/
  11136. static void inline pc_bonus_script_check_final(struct map_session_data *sd) {
  11137. if (sd->bonus_script.count == 0) {
  11138. if (sd->bonus_script.head && sd->bonus_script.head->data)
  11139. pc_bonus_script_free_entry(sd, (struct s_bonus_script_entry *)sd->bonus_script.head->data);
  11140. linkdb_final(&sd->bonus_script.head);
  11141. }
  11142. }
  11143. /**
  11144. * Timer for bonus_script
  11145. * @param tid
  11146. * @param tick
  11147. * @param id
  11148. * @param data
  11149. * @author [Cydh]
  11150. **/
  11151. TIMER_FUNC(pc_bonus_script_timer){
  11152. struct map_session_data *sd;
  11153. struct s_bonus_script_entry *entry = (struct s_bonus_script_entry *)data;
  11154. sd = map_id2sd(id);
  11155. if (!sd) {
  11156. ShowError("pc_bonus_script_timer: Null pointer id: %d tid: %d\n", id, tid);
  11157. return 0;
  11158. }
  11159. if (tid == INVALID_TIMER)
  11160. return 0;
  11161. if (!sd->bonus_script.head || entry == NULL) {
  11162. ShowError("pc_bonus_script_timer: Invalid entry pointer %p!\n", entry);
  11163. return 0;
  11164. }
  11165. linkdb_erase(&sd->bonus_script.head, (void *)((intptr_t)entry));
  11166. pc_bonus_script_free_entry(sd, entry);
  11167. pc_bonus_script_check_final(sd);
  11168. status_calc_pc(sd,SCO_NONE);
  11169. return 0;
  11170. }
  11171. /**
  11172. * Check then clear all active timer(s) of bonus_script data from player based on reason
  11173. * @param sd: Target player
  11174. * @param flag: Reason to remove the bonus_script. e_bonus_script_flags or e_bonus_script_types
  11175. * @author [Cydh]
  11176. **/
  11177. void pc_bonus_script_clear(struct map_session_data *sd, uint16 flag) {
  11178. struct linkdb_node *node = NULL;
  11179. uint16 count = 0;
  11180. if (!sd || !(node = sd->bonus_script.head))
  11181. return;
  11182. while (node) {
  11183. struct linkdb_node *next = node->next;
  11184. struct s_bonus_script_entry *entry = (struct s_bonus_script_entry *)node->data;
  11185. if (entry && (
  11186. (flag == BSF_PERMANENT) || // Remove all with permanent bonus
  11187. (!flag && !(entry->flag&BSF_PERMANENT)) || // Remove all WITHOUT permanent bonus
  11188. (flag&entry->flag) || // Matched flag
  11189. (flag&BSF_REM_BUFF && entry->type == 1) || // Remove buff
  11190. (flag&BSF_REM_DEBUFF && entry->type == 2) // Remove debuff
  11191. )
  11192. )
  11193. {
  11194. linkdb_erase(&sd->bonus_script.head, (void *)((intptr_t)entry));
  11195. pc_bonus_script_free_entry(sd, entry);
  11196. count++;
  11197. }
  11198. node = next;
  11199. }
  11200. pc_bonus_script_check_final(sd);
  11201. if (count && !(flag&BSF_REM_ON_LOGOUT)) //Don't need to do this if log out
  11202. status_calc_pc(sd,SCO_NONE);
  11203. }
  11204. /** [Cydh]
  11205. * Gives/removes SC_BASILICA when player steps in/out the cell with 'cell_basilica'
  11206. * @param sd: Target player
  11207. */
  11208. void pc_cell_basilica(struct map_session_data *sd) {
  11209. nullpo_retv(sd);
  11210. #ifdef RENEWAL
  11211. enum sc_type type = SC_BASILICA_CELL;
  11212. #else
  11213. enum sc_type type = SC_BASILICA;
  11214. #endif
  11215. if (!map_getcell(sd->bl.m,sd->bl.x,sd->bl.y,CELL_CHKBASILICA)) {
  11216. if (sd->sc.data[type])
  11217. status_change_end(&sd->bl, type,INVALID_TIMER);
  11218. }
  11219. else if (!sd->sc.data[type])
  11220. sc_start(&sd->bl,&sd->bl, type,100,0,INFINITE_TICK);
  11221. }
  11222. /** [Cydh]
  11223. * Get maximum specified parameter for specified class
  11224. * @param class_: sd->class
  11225. * @param sex: sd->status.sex
  11226. * @param flag: parameter will be checked
  11227. * @return max_param
  11228. */
  11229. short pc_maxparameter(struct map_session_data *sd, enum e_params param) {
  11230. int idx = -1, class_ = sd->class_;
  11231. if ((idx = pc_class2idx(pc_mapid2jobid(class_,sd->status.sex))) >= 0) {
  11232. short max_param = 0;
  11233. switch (param) {
  11234. case PARAM_STR: max_param = job_info[idx].max_param.str; break;
  11235. case PARAM_AGI: max_param = job_info[idx].max_param.agi; break;
  11236. case PARAM_VIT: max_param = job_info[idx].max_param.vit; break;
  11237. case PARAM_INT: max_param = job_info[idx].max_param.int_; break;
  11238. case PARAM_DEX: max_param = job_info[idx].max_param.dex; break;
  11239. case PARAM_LUK: max_param = job_info[idx].max_param.luk; break;
  11240. }
  11241. if (max_param > 0)
  11242. return max_param;
  11243. }
  11244. // Always check babies first
  11245. if( class_ & JOBL_BABY ){
  11246. if( class_ & JOBL_THIRD ){
  11247. return battle_config.max_baby_third_parameter;
  11248. }else{
  11249. return battle_config.max_baby_parameter;
  11250. }
  11251. }
  11252. // Summoner
  11253. if( ( class_ & MAPID_BASEMASK ) == MAPID_SUMMONER ){
  11254. return battle_config.max_summoner_parameter;
  11255. }
  11256. // Extended classes
  11257. if( ( class_ & MAPID_UPPERMASK ) == MAPID_KAGEROUOBORO || ( class_ & MAPID_UPPERMASK ) == MAPID_REBELLION ){
  11258. return battle_config.max_extended_parameter;
  11259. }
  11260. // 3rd class
  11261. if( class_ & JOBL_THIRD ){
  11262. // Transcendent
  11263. if( class_ & JOBL_UPPER ){
  11264. return battle_config.max_third_trans_parameter;
  11265. }else{
  11266. return battle_config.max_third_parameter;
  11267. }
  11268. }
  11269. // Transcendent
  11270. if( class_ & JOBL_UPPER ){
  11271. return battle_config.max_trans_parameter;
  11272. }
  11273. return battle_config.max_parameter;
  11274. }
  11275. /**
  11276. * Get max ASPD for player based on Class
  11277. * @param sd Player
  11278. * @return ASPD
  11279. */
  11280. short pc_maxaspd(struct map_session_data *sd) {
  11281. nullpo_ret(sd);
  11282. return (( sd->class_&JOBL_THIRD) ? battle_config.max_third_aspd : (
  11283. ((sd->class_&MAPID_UPPERMASK) == MAPID_KAGEROUOBORO || (sd->class_&MAPID_UPPERMASK) == MAPID_REBELLION) ? battle_config.max_extended_aspd :
  11284. battle_config.max_aspd ));
  11285. }
  11286. /**
  11287. * Calculates total item-group related bonuses for the given item
  11288. * @param sd Player
  11289. * @param nameid Item ID
  11290. * @return Heal rate
  11291. **/
  11292. short pc_get_itemgroup_bonus(struct map_session_data* sd, unsigned short nameid) {
  11293. if (sd->itemgrouphealrate.empty())
  11294. return 0;
  11295. short bonus = 0;
  11296. for (const auto &it : sd->itemgrouphealrate) {
  11297. uint16 group_id = it.id, i;
  11298. struct s_item_group_db *group = NULL;
  11299. if (!group_id || !(group = itemdb_group_exists(group_id)))
  11300. continue;
  11301. for (i = 0; i < group->random[0].data_qty; i++) {
  11302. if (group->random[0].data[i].nameid == nameid) {
  11303. bonus += it.val;
  11304. break;
  11305. }
  11306. }
  11307. }
  11308. return bonus;
  11309. }
  11310. /**
  11311. * Calculates total item-group related bonuses for the given item group
  11312. * @param sd Player
  11313. * @param group_id Item Group ID
  11314. * @return Heal rate
  11315. **/
  11316. short pc_get_itemgroup_bonus_group(struct map_session_data* sd, uint16 group_id) {
  11317. if (sd->itemgrouphealrate.empty())
  11318. return 0;
  11319. for (const auto &it : sd->itemgrouphealrate) {
  11320. if (it.id == group_id)
  11321. return it.val;
  11322. }
  11323. return 0;
  11324. }
  11325. /**
  11326. * Check if player's equip index in same specified position, like for 2-Handed weapon & Heagdear (inc. costume)
  11327. * @param eqi Item EQI of enum equip_index
  11328. * @param *equip_index Player's equip_index[]
  11329. * @param index Known index item in inventory from sd->equip_index[] to compare with specified EQI in *equip_index
  11330. * @return True if item in same inventory index, False if doesn't
  11331. */
  11332. bool pc_is_same_equip_index(enum equip_index eqi, short *equip_index, short index) {
  11333. if (index < 0 || index >= MAX_INVENTORY)
  11334. return true;
  11335. // Dual weapon checks
  11336. if (eqi == EQI_HAND_R && equip_index[EQI_HAND_L] == index)
  11337. return true;
  11338. // Headgear with Mid & Low location
  11339. if (eqi == EQI_HEAD_MID && equip_index[EQI_HEAD_LOW] == index)
  11340. return true;
  11341. // Headgear with Top & Mid or Low location
  11342. if (eqi == EQI_HEAD_TOP && (equip_index[EQI_HEAD_MID] == index || equip_index[EQI_HEAD_LOW] == index))
  11343. return true;
  11344. // Headgear with Mid & Low location
  11345. if (eqi == EQI_COSTUME_HEAD_MID && equip_index[EQI_COSTUME_HEAD_LOW] == index)
  11346. return true;
  11347. // Headgear with Top & Mid or Low location
  11348. if (eqi == EQI_COSTUME_HEAD_TOP && (equip_index[EQI_COSTUME_HEAD_MID] == index || equip_index[EQI_COSTUME_HEAD_LOW] == index))
  11349. return true;
  11350. return false;
  11351. }
  11352. /**
  11353. * Generate Unique item ID for player
  11354. * @param sd : Player
  11355. * @return A generated Unique item ID
  11356. */
  11357. uint64 pc_generate_unique_id(struct map_session_data *sd) {
  11358. nullpo_ret(sd);
  11359. return ((uint64)sd->status.char_id << 32) | sd->status.uniqueitem_counter++;
  11360. }
  11361. /**
  11362. * Validating skill from player after logged on
  11363. * @param sd
  11364. **/
  11365. void pc_validate_skill(struct map_session_data *sd) {
  11366. if (sd) {
  11367. uint16 i = 0, count = 0;
  11368. struct s_skill tmp_skills[MAX_SKILL] = {{ 0 }};
  11369. memcpy(tmp_skills, sd->status.skill, sizeof(sd->status.skill));
  11370. memset(sd->status.skill, 0, sizeof(sd->status.skill));
  11371. for (i = 0; i < MAX_SKILL; i++) {
  11372. uint16 idx = 0;
  11373. if (tmp_skills[i].id == 0 || tmp_skills[i].lv == 0)
  11374. continue;
  11375. if ((idx = skill_get_index(tmp_skills[i].id))) {
  11376. memcpy(&sd->status.skill[idx], &tmp_skills[i], sizeof(tmp_skills[i]));
  11377. count++;
  11378. }
  11379. else
  11380. 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);
  11381. }
  11382. }
  11383. }
  11384. /**
  11385. * Toggle to remember if the questinfo is displayed yet or not.
  11386. * @param qi_display Display flag
  11387. * @param show If show is true and qi_display is 0, set qi_display to 1 and show the event bubble.
  11388. * If show is false and qi_display is 1, set qi_display to 0 and hide the event bubble.
  11389. **/
  11390. static void pc_show_questinfo_sub(struct map_session_data *sd, bool *qi_display, struct s_questinfo *qi, bool show) {
  11391. if (show) {
  11392. // Check if need to be displayed
  11393. if ((*qi_display) != 1) {
  11394. (*qi_display) = 1;
  11395. clif_quest_show_event(sd, &qi->nd->bl, qi->icon, qi->color);
  11396. }
  11397. }
  11398. else {
  11399. // Check if need to be hide
  11400. if ((*qi_display) != 0) {
  11401. (*qi_display) = 0;
  11402. #if PACKETVER >= 20120410
  11403. clif_quest_show_event(sd, &qi->nd->bl, QTYPE_NONE, QMARK_NONE);
  11404. #else
  11405. clif_quest_show_event(sd, &qi->nd->bl, QTYPE_QUEST, QMARK_NONE);
  11406. #endif
  11407. }
  11408. }
  11409. }
  11410. /**
  11411. * Show available NPC Quest / Event Icon Check [Kisuka]
  11412. * @param sd Player
  11413. **/
  11414. void pc_show_questinfo(struct map_session_data *sd) {
  11415. #if PACKETVER >= 20090218
  11416. nullpo_retv(sd);
  11417. if (sd->bl.m < 0 || sd->bl.m >= MAX_MAPINDEX)
  11418. return;
  11419. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  11420. nullpo_retv(mapdata);
  11421. if (mapdata->qi_data.empty())
  11422. return;
  11423. if (mapdata->qi_data.size() != sd->qi_count)
  11424. return; // init was not called yet
  11425. struct s_questinfo *qi = nullptr;
  11426. bool show;
  11427. for (int i = 0; i < mapdata->qi_data.size(); i++) {
  11428. qi = &mapdata->qi_data[i];
  11429. if (!qi)
  11430. continue;
  11431. if (!qi->condition)
  11432. show = true;
  11433. else {
  11434. if (achievement_check_condition(qi->condition, sd))
  11435. show = true;
  11436. else
  11437. show = false;
  11438. }
  11439. pc_show_questinfo_sub(sd, &sd->qi_display[i], qi, show);
  11440. }
  11441. #endif
  11442. }
  11443. /**
  11444. * Reinit the questinfo for player when changing map
  11445. * @param sd Player
  11446. **/
  11447. void pc_show_questinfo_reinit(struct map_session_data *sd) {
  11448. #if PACKETVER >= 20090218
  11449. nullpo_retv(sd);
  11450. if (sd->qi_display) {
  11451. aFree(sd->qi_display);
  11452. sd->qi_display = nullptr;
  11453. }
  11454. sd->qi_count = 0;
  11455. if (sd->bl.m < 0 || sd->bl.m >= MAX_MAPINDEX)
  11456. return;
  11457. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  11458. nullpo_retv(mapdata);
  11459. if (mapdata->qi_data.empty())
  11460. return;
  11461. CREATE(sd->qi_display, bool, (sd->qi_count = mapdata->qi_data.size()));
  11462. #endif
  11463. }
  11464. /**
  11465. * Check if a job is allowed to enter the map
  11466. * @param jobid Job ID see enum e_job or sd->status.class_
  11467. * @param m ID -an index- for direct indexing map[] array
  11468. * @return 1 if job is allowed, 0 otherwise
  11469. **/
  11470. bool pc_job_can_entermap(enum e_job jobid, int m, int group_lv) {
  11471. uint16 idx = 0;
  11472. // Map is other map server.
  11473. // !FIXME: Currently, a map-server doesn't recognized map's attributes on other server, so we assume it's fine to warp.
  11474. if (m < 0)
  11475. return true;
  11476. struct map_data *mapdata = map_getmapdata(m);
  11477. if (!mapdata->cell)
  11478. return false;
  11479. if (!pcdb_checkid(jobid))
  11480. return false;
  11481. idx = pc_class2idx(jobid);
  11482. if (!job_info[idx].noenter_map.zone || group_lv > job_info[idx].noenter_map.group_lv)
  11483. return true;
  11484. if ((job_info[idx].noenter_map.zone&1 && !mapdata_flag_vs2(mapdata)) || // Normal
  11485. (job_info[idx].noenter_map.zone&2 && mapdata->flag[MF_PVP]) || // PVP
  11486. (job_info[idx].noenter_map.zone&4 && mapdata_flag_gvg2_no_te(mapdata)) || // GVG
  11487. (job_info[idx].noenter_map.zone&8 && mapdata->flag[MF_BATTLEGROUND]) || // Battleground
  11488. (job_info[idx].noenter_map.zone&16 && mapdata_flag_gvg2_te(mapdata)) || // WOE:TE
  11489. (job_info[idx].noenter_map.zone&(mapdata->zone) && mapdata->flag[MF_RESTRICTED]) // Zone restriction
  11490. )
  11491. return false;
  11492. return true;
  11493. }
  11494. /**
  11495. * Tells client about player's costume view on mapchange for checking 'nocostume' mapflag.
  11496. * @param sd
  11497. **/
  11498. void pc_set_costume_view(struct map_session_data *sd) {
  11499. int i = -1, head_low = 0, head_mid = 0, head_top = 0, robe = 0;
  11500. struct item_data *id = NULL;
  11501. nullpo_retv(sd);
  11502. head_low = sd->status.head_bottom;
  11503. head_mid = sd->status.head_mid;
  11504. head_top = sd->status.head_top;
  11505. robe = sd->status.robe;
  11506. sd->status.head_bottom = sd->status.head_mid = sd->status.head_top = sd->status.robe = 0;
  11507. //Added check to prevent sending the same look on multiple slots ->
  11508. //causes client to redraw item on top of itself. (suggested by Lupus)
  11509. // Normal headgear checks
  11510. if ((i = sd->equip_index[EQI_HEAD_LOW]) != -1 && (id = sd->inventory_data[i])) {
  11511. if (!(id->equip&(EQP_HEAD_MID|EQP_HEAD_TOP)))
  11512. sd->status.head_bottom = id->look;
  11513. else
  11514. sd->status.head_bottom = 0;
  11515. }
  11516. if ((i = sd->equip_index[EQI_HEAD_MID]) != -1 && (id = sd->inventory_data[i])) {
  11517. if (!(id->equip&(EQP_HEAD_TOP)))
  11518. sd->status.head_mid = id->look;
  11519. else
  11520. sd->status.head_mid = 0;
  11521. }
  11522. if ((i = sd->equip_index[EQI_HEAD_TOP]) != -1 && (id = sd->inventory_data[i]))
  11523. sd->status.head_top = id->look;
  11524. if ((i = sd->equip_index[EQI_GARMENT]) != -1 && (id = sd->inventory_data[i]))
  11525. sd->status.robe = id->look;
  11526. // Costumes check
  11527. if (!map_getmapflag(sd->bl.m, MF_NOCOSTUME)) {
  11528. if ((i = sd->equip_index[EQI_COSTUME_HEAD_LOW]) != -1 && (id = sd->inventory_data[i])) {
  11529. if (!(id->equip&(EQP_COSTUME_HEAD_MID|EQP_COSTUME_HEAD_TOP)))
  11530. sd->status.head_bottom = id->look;
  11531. else
  11532. sd->status.head_bottom = 0;
  11533. }
  11534. if ((i = sd->equip_index[EQI_COSTUME_HEAD_MID]) != -1 && (id = sd->inventory_data[i])) {
  11535. if (!(id->equip&EQP_COSTUME_HEAD_TOP))
  11536. sd->status.head_mid = id->look;
  11537. else
  11538. sd->status.head_mid = 0;
  11539. }
  11540. if ((i = sd->equip_index[EQI_COSTUME_HEAD_TOP]) != -1 && (id = sd->inventory_data[i]))
  11541. sd->status.head_top = id->look;
  11542. if ((i = sd->equip_index[EQI_COSTUME_GARMENT]) != -1 && (id = sd->inventory_data[i]))
  11543. sd->status.robe = id->look;
  11544. }
  11545. if (sd->setlook_head_bottom)
  11546. sd->status.head_bottom = sd->setlook_head_bottom;
  11547. if (sd->setlook_head_mid)
  11548. sd->status.head_mid = sd->setlook_head_mid;
  11549. if (sd->setlook_head_top)
  11550. sd->status.head_top = sd->setlook_head_top;
  11551. if (sd->setlook_robe)
  11552. sd->status.robe = sd->setlook_robe;
  11553. if (head_low != sd->status.head_bottom)
  11554. clif_changelook(&sd->bl, LOOK_HEAD_BOTTOM, sd->status.head_bottom);
  11555. if (head_mid != sd->status.head_mid)
  11556. clif_changelook(&sd->bl, LOOK_HEAD_MID, sd->status.head_mid);
  11557. if (head_top != sd->status.head_top)
  11558. clif_changelook(&sd->bl, LOOK_HEAD_TOP, sd->status.head_top);
  11559. if (robe != sd->status.robe)
  11560. clif_changelook(&sd->bl, LOOK_ROBE, sd->status.robe);
  11561. }
  11562. std::shared_ptr<s_attendance_period> pc_attendance_period(){
  11563. uint32 date = date_get(DT_YYYYMMDD);
  11564. for( std::pair<const uint32,std::shared_ptr<s_attendance_period>>& pair : attendance_db ){
  11565. std::shared_ptr<s_attendance_period> period = pair.second;
  11566. if( period->start <= date && period->end >= date ){
  11567. return period;
  11568. }
  11569. }
  11570. return nullptr;
  11571. }
  11572. bool pc_attendance_enabled(){
  11573. // Check if the attendance feature is disabled
  11574. if( !battle_config.feature_attendance ){
  11575. return false;
  11576. }
  11577. // Check if there is a running attendance period
  11578. return pc_attendance_period() != nullptr;
  11579. }
  11580. static inline bool pc_attendance_rewarded_today( struct map_session_data* sd ){
  11581. return pc_readreg2( sd, ATTENDANCE_DATE_VAR ) >= date_get(DT_YYYYMMDD);
  11582. }
  11583. int32 pc_attendance_counter( struct map_session_data* sd ){
  11584. std::shared_ptr<s_attendance_period> period = pc_attendance_period();
  11585. // No running attendance period
  11586. if( period == nullptr ){
  11587. return 0;
  11588. }
  11589. // Get the counter for the current period
  11590. int counter = static_cast<int>(pc_readreg2( sd, ATTENDANCE_COUNT_VAR ));
  11591. // Check if we have a remaining counter from a previous period
  11592. if( counter > 0 && pc_readreg2( sd, ATTENDANCE_DATE_VAR ) < period->start ){
  11593. // Reset the counter to zero
  11594. pc_setreg2( sd, ATTENDANCE_COUNT_VAR, 0 );
  11595. return 0;
  11596. }
  11597. return 10 * counter + ( ( pc_attendance_rewarded_today(sd) ) ? 1 : 0 );
  11598. }
  11599. void pc_attendance_claim_reward( struct map_session_data* sd ){
  11600. // If the user's group does not have the permission
  11601. if( !pc_has_permission( sd, PC_PERM_ATTENDANCE ) ){
  11602. return;
  11603. }
  11604. // Check if the attendance feature is disabled
  11605. if( !pc_attendance_enabled() ){
  11606. return;
  11607. }
  11608. // Check if the user already got his reward today
  11609. if( pc_attendance_rewarded_today( sd ) ){
  11610. return;
  11611. }
  11612. int32 attendance_counter = static_cast<int32>(pc_readreg2( sd, ATTENDANCE_COUNT_VAR ));
  11613. attendance_counter += 1;
  11614. std::shared_ptr<s_attendance_period> period = pc_attendance_period();
  11615. if( period == nullptr ){
  11616. return;
  11617. }
  11618. if( period->rewards.size() < attendance_counter ){
  11619. return;
  11620. }
  11621. pc_setreg2( sd, ATTENDANCE_DATE_VAR, date_get(DT_YYYYMMDD) );
  11622. pc_setreg2( sd, ATTENDANCE_COUNT_VAR, attendance_counter );
  11623. if( save_settings&CHARSAVE_ATTENDANCE )
  11624. chrif_save(sd, CSAVE_NORMAL);
  11625. std::shared_ptr<s_attendance_reward> reward = period->rewards[attendance_counter - 1];
  11626. struct mail_message msg;
  11627. memset( &msg, 0, sizeof( struct mail_message ) );
  11628. msg.dest_id = sd->status.char_id;
  11629. safestrncpy( msg.send_name, msg_txt( sd, 788 ), NAME_LENGTH );
  11630. safesnprintf( msg.title, MAIL_TITLE_LENGTH, msg_txt( sd, 789 ), attendance_counter );
  11631. safesnprintf( msg.body, MAIL_BODY_LENGTH, msg_txt( sd, 790 ), attendance_counter );
  11632. msg.item[0].nameid = reward->item_id;
  11633. msg.item[0].amount = reward->amount;
  11634. msg.item[0].identify = 1;
  11635. msg.status = MAIL_NEW;
  11636. msg.type = MAIL_INBOX_NORMAL;
  11637. msg.timestamp = time(NULL);
  11638. intif_Mail_send(0, &msg);
  11639. clif_attendence_response( sd, attendance_counter );
  11640. }
  11641. /*==========================================
  11642. * pc Init/Terminate
  11643. *------------------------------------------*/
  11644. void do_final_pc(void) {
  11645. db_destroy(itemcd_db);
  11646. do_final_pc_groups();
  11647. ers_destroy(pc_sc_display_ers);
  11648. ers_destroy(num_reg_ers);
  11649. ers_destroy(str_reg_ers);
  11650. attendance_db.clear();
  11651. }
  11652. void do_init_pc(void) {
  11653. itemcd_db = idb_alloc(DB_OPT_RELEASE_DATA);
  11654. pc_readdb();
  11655. pc_read_motd(); // Read MOTD [Valaris]
  11656. attendance_db.load();
  11657. add_timer_func_list(pc_invincible_timer, "pc_invincible_timer");
  11658. add_timer_func_list(pc_eventtimer, "pc_eventtimer");
  11659. add_timer_func_list(pc_inventory_rental_end, "pc_inventory_rental_end");
  11660. add_timer_func_list(pc_calc_pvprank_timer, "pc_calc_pvprank_timer");
  11661. add_timer_func_list(pc_autosave, "pc_autosave");
  11662. add_timer_func_list(pc_spiritball_timer, "pc_spiritball_timer");
  11663. add_timer_func_list(pc_soulball_timer, "pc_soulball_timer");
  11664. add_timer_func_list(pc_follow_timer, "pc_follow_timer");
  11665. add_timer_func_list(pc_endautobonus, "pc_endautobonus");
  11666. add_timer_func_list(pc_spiritcharm_timer, "pc_spiritcharm_timer");
  11667. add_timer_func_list(pc_global_expiration_timer, "pc_global_expiration_timer");
  11668. add_timer_func_list(pc_expiration_timer, "pc_expiration_timer");
  11669. add_timer_func_list(pc_autotrade_timer, "pc_autotrade_timer");
  11670. add_timer(gettick() + autosave_interval, pc_autosave, 0, 0);
  11671. // 0=day, 1=night [Yor]
  11672. night_flag = battle_config.night_at_start ? 1 : 0;
  11673. if (battle_config.day_duration > 0 && battle_config.night_duration > 0) {
  11674. int day_duration = battle_config.day_duration;
  11675. int night_duration = battle_config.night_duration;
  11676. // add night/day timer [Yor]
  11677. add_timer_func_list(map_day_timer, "map_day_timer");
  11678. add_timer_func_list(map_night_timer, "map_night_timer");
  11679. day_timer_tid = add_timer_interval(gettick() + (night_flag ? 0 : day_duration) + night_duration, map_day_timer, 0, 0, day_duration + night_duration);
  11680. night_timer_tid = add_timer_interval(gettick() + day_duration + (night_flag ? night_duration : 0), map_night_timer, 0, 0, day_duration + night_duration);
  11681. }
  11682. do_init_pc_groups();
  11683. pc_sc_display_ers = ers_new(sizeof(struct sc_display_entry), "pc.cpp:pc_sc_display_ers", ERS_OPT_FLEX_CHUNK);
  11684. num_reg_ers = ers_new(sizeof(struct script_reg_num), "pc.cpp:num_reg_ers", (ERSOptions)(ERS_OPT_CLEAN|ERS_OPT_FLEX_CHUNK));
  11685. str_reg_ers = ers_new(sizeof(struct script_reg_str), "pc.cpp:str_reg_ers", (ERSOptions)(ERS_OPT_CLEAN|ERS_OPT_FLEX_CHUNK));
  11686. ers_chunk_size(pc_sc_display_ers, 150);
  11687. ers_chunk_size(num_reg_ers, 300);
  11688. ers_chunk_size(str_reg_ers, 50);
  11689. }